summaryrefslogtreecommitdiff
path: root/cmd_fusemount.c
blob: d81f31884cb27a2704cd898ff688fd51f5f139c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
#ifdef BCACHEFS_FUSE

#include <errno.h>
#include <float.h>
#include <getopt.h>
#include <stdio.h>
#include <sys/statvfs.h>

#include <fuse_lowlevel.h>

#include "cmds.h"
#include "libbcachefs.h"
#include "tools-util.h"

#include "libbcachefs/bcachefs.h"
#include "libbcachefs/alloc_foreground.h"
#include "libbcachefs/btree_iter.h"
#include "libbcachefs/buckets.h"
#include "libbcachefs/dirent.h"
#include "libbcachefs/errcode.h"
#include "libbcachefs/error.h"
#include "libbcachefs/fs-common.h"
#include "libbcachefs/inode.h"
#include "libbcachefs/io_read.h"
#include "libbcachefs/io_write.h"
#include "libbcachefs/opts.h"
#include "libbcachefs/super.h"

/* mode_to_type(): */
#include "libbcachefs/fs.h"

#include <linux/dcache.h>

/* XXX cut and pasted from fsck.c */
#define QSTR(n) { { { .len = strlen(n) } }, .name = n }

/* used by write_aligned function for waiting on bch2_write closure */
struct write_aligned_op_t {
        struct closure cl;

        /* must be last: */
        struct bch_write_op             op;
};


static inline subvol_inum map_root_ino(u64 ino)
{
	return (subvol_inum) { 1, ino == 1 ? 4096 : ino };
}

static inline u64 unmap_root_ino(u64 ino)
{
	return ino == 4096 ? 1 : ino;
}

static struct stat inode_to_stat(struct bch_fs *c,
				 struct bch_inode_unpacked *bi)
{
	return (struct stat) {
		.st_ino		= unmap_root_ino(bi->bi_inum),
		.st_size	= bi->bi_size,
		.st_mode	= bi->bi_mode,
		.st_uid		= bi->bi_uid,
		.st_gid		= bi->bi_gid,
		.st_nlink	= bch2_inode_nlink_get(bi),
		.st_rdev	= bi->bi_dev,
		.st_blksize	= block_bytes(c),
		.st_blocks	= bi->bi_sectors,
		.st_atim	= bch2_time_to_timespec(c, bi->bi_atime),
		.st_mtim	= bch2_time_to_timespec(c, bi->bi_mtime),
		.st_ctim	= bch2_time_to_timespec(c, bi->bi_ctime),
	};
}

static struct fuse_entry_param inode_to_entry(struct bch_fs *c,
					      struct bch_inode_unpacked *bi)
{
	return (struct fuse_entry_param) {
		.ino		= unmap_root_ino(bi->bi_inum),
		.generation	= bi->bi_generation,
		.attr		= inode_to_stat(c, bi),
		.attr_timeout	= DBL_MAX,
		.entry_timeout	= DBL_MAX,
	};
}

static void bcachefs_fuse_init(void *arg, struct fuse_conn_info *conn)
{
	if (conn->capable & FUSE_CAP_WRITEBACK_CACHE) {
		fuse_log(FUSE_LOG_DEBUG, "fuse_init: activating writeback\n");
		conn->want |= FUSE_CAP_WRITEBACK_CACHE;
	} else
		fuse_log(FUSE_LOG_DEBUG, "fuse_init: writeback not capable\n");

	//conn->want |= FUSE_CAP_POSIX_ACL;
}

static void bcachefs_fuse_destroy(void *arg)
{
	struct bch_fs *c = arg;

	bch2_fs_stop(c);
}

static void bcachefs_fuse_lookup(fuse_req_t req, fuse_ino_t dir_ino,
				 const char *name)
{
	subvol_inum dir = map_root_ino(dir_ino);
	struct bch_fs *c = fuse_req_userdata(req);
	struct bch_inode_unpacked bi;
	struct qstr qstr = QSTR(name);
	subvol_inum inum;
	int ret;

	fuse_log(FUSE_LOG_DEBUG, "fuse_lookup(dir=%llu name=%s)\n",
		 dir.inum, name);

	ret = bch2_inode_find_by_inum(c, dir, &bi);
	if (ret) {
		fuse_reply_err(req, -ret);
		return;
	}

	struct bch_hash_info hash_info = bch2_hash_info_init(c, &bi);

	ret = bch2_dirent_lookup(c, dir, &hash_info, &qstr, &inum);
	if (ret) {
		struct fuse_entry_param e = {
			.attr_timeout	= DBL_MAX,
			.entry_timeout	= DBL_MAX,
		};
		fuse_reply_entry(req, &e);
		return;
	}

	ret = bch2_inode_find_by_inum(c, inum, &bi);
	if (ret)
		goto err;

	fuse_log(FUSE_LOG_DEBUG, "fuse_lookup ret(inum=%llu)\n",
		 bi.bi_inum);

	struct fuse_entry_param e = inode_to_entry(c, &bi);
	fuse_reply_entry(req, &e);
	return;
err:
	fuse_log(FUSE_LOG_DEBUG, "fuse_lookup error %i\n", ret);
	fuse_reply_err(req, -ret);
}

static void bcachefs_fuse_getattr(fuse_req_t req, fuse_ino_t ino,
				  struct fuse_file_info *fi)
{
	subvol_inum inum = map_root_ino(ino);
	struct bch_fs *c = fuse_req_userdata(req);
	struct bch_inode_unpacked bi;
	struct stat attr;

	fuse_log(FUSE_LOG_DEBUG, "fuse_getattr(inum=%llu)\n", inum.inum);

	int ret = bch2_inode_find_by_inum(c, inum, &bi);
	if (ret) {
		fuse_log(FUSE_LOG_DEBUG, "fuse_getattr error %i\n", ret);
		fuse_reply_err(req, -ret);
		return;
	}

	fuse_log(FUSE_LOG_DEBUG, "fuse_getattr success\n");

	attr = inode_to_stat(c, &bi);
	fuse_reply_attr(req, &attr, DBL_MAX);
}

static void bcachefs_fuse_setattr(fuse_req_t req, fuse_ino_t ino,
				  struct stat *attr, int to_set,
				  struct fuse_file_info *fi)
{
	struct bch_fs *c = fuse_req_userdata(req);
	struct bch_inode_unpacked inode_u;
	struct btree_trans *trans;
	struct btree_iter iter;
	u64 now;
	int ret;

	subvol_inum inum = map_root_ino(ino);

	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_setattr(%llu, %x)\n", inum.inum, to_set);

	trans = bch2_trans_get(c);
retry:
	bch2_trans_begin(trans);
	now = bch2_current_time(c);

	ret = bch2_inode_peek(trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
	if (ret)
		goto err;

	if (to_set & FUSE_SET_ATTR_MODE)
		inode_u.bi_mode	= attr->st_mode;
	if (to_set & FUSE_SET_ATTR_UID)
		inode_u.bi_uid	= attr->st_uid;
	if (to_set & FUSE_SET_ATTR_GID)
		inode_u.bi_gid	= attr->st_gid;
	if (to_set & FUSE_SET_ATTR_SIZE)
		inode_u.bi_size	= attr->st_size;
	if (to_set & FUSE_SET_ATTR_ATIME)
		inode_u.bi_atime = timespec_to_bch2_time(c, attr->st_atim);
	if (to_set & FUSE_SET_ATTR_MTIME)
		inode_u.bi_mtime = timespec_to_bch2_time(c, attr->st_mtim);
	if (to_set & FUSE_SET_ATTR_ATIME_NOW)
		inode_u.bi_atime = now;
	if (to_set & FUSE_SET_ATTR_MTIME_NOW)
		inode_u.bi_mtime = now;
	/* TODO: CTIME? */

	ret   = bch2_inode_write(trans, &iter, &inode_u) ?:
		bch2_trans_commit(trans, NULL, NULL,
				  BCH_TRANS_COMMIT_no_enospc);
err:
        bch2_trans_iter_exit(trans, &iter);
	if (ret == -EINTR)
		goto retry;

	bch2_trans_put(trans);

	if (!ret) {
		*attr = inode_to_stat(c, &inode_u);
		fuse_reply_attr(req, attr, DBL_MAX);
	} else {
		fuse_reply_err(req, -ret);
	}
}

static int do_create(struct bch_fs *c, subvol_inum dir,
		     const char *name, mode_t mode, dev_t rdev,
		     struct bch_inode_unpacked *new_inode)
{
	struct qstr qstr = QSTR(name);
	struct bch_inode_unpacked dir_u;
	uid_t uid = 0;
	gid_t gid = 0;

	bch2_inode_init_early(c, new_inode);

	return bch2_trans_do(c, NULL, NULL, 0,
			bch2_create_trans(trans,
				dir, &dir_u,
				new_inode, &qstr,
				uid, gid, mode, rdev, NULL, NULL,
				(subvol_inum) { 0 }, 0));
}

static void bcachefs_fuse_mknod(fuse_req_t req, fuse_ino_t dir_ino,
				const char *name, mode_t mode,
				dev_t rdev)
{
	subvol_inum dir = map_root_ino(dir_ino);
	struct bch_fs *c = fuse_req_userdata(req);
	struct bch_inode_unpacked new_inode;
	int ret;

	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_mknod(%llu, %s, %x, %x)\n",
		 dir.inum, name, mode, rdev);

	ret = do_create(c, dir, name, mode, rdev, &new_inode);
	if (ret)
		goto err;

	struct fuse_entry_param e = inode_to_entry(c, &new_inode);
	fuse_reply_entry(req, &e);
	return;
err:
	fuse_reply_err(req, -ret);
}

static void bcachefs_fuse_mkdir(fuse_req_t req, fuse_ino_t dir,
				const char *name, mode_t mode)
{
	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_mkdir(%llu, %s, %x)\n",
		 dir, name, mode);

	BUG_ON(mode & S_IFMT);

	mode |= S_IFDIR;
	bcachefs_fuse_mknod(req, dir, name, mode, 0);
}

static void bcachefs_fuse_unlink(fuse_req_t req, fuse_ino_t dir_ino,
				 const char *name)
{
	struct bch_fs *c = fuse_req_userdata(req);
	struct bch_inode_unpacked dir_u, inode_u;
	struct qstr qstr = QSTR(name);
	subvol_inum dir = map_root_ino(dir_ino);

	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_unlink(%llu, %s)\n", dir.inum, name);

	int ret = bch2_trans_do(c, NULL, NULL,
				BCH_TRANS_COMMIT_no_enospc,
			    bch2_unlink_trans(trans, dir, &dir_u,
					      &inode_u, &qstr, false));

	fuse_reply_err(req, -ret);
}

static void bcachefs_fuse_rmdir(fuse_req_t req, fuse_ino_t dir,
				const char *name)
{
	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_rmdir(%llu, %s)\n", dir, name);

	bcachefs_fuse_unlink(req, dir, name);
}

static void bcachefs_fuse_rename(fuse_req_t req,
				 fuse_ino_t src_dir_ino, const char *srcname,
				 fuse_ino_t dst_dir_ino, const char *dstname,
				 unsigned flags)
{
	struct bch_fs *c = fuse_req_userdata(req);
	struct bch_inode_unpacked dst_dir_u, src_dir_u;
	struct bch_inode_unpacked src_inode_u, dst_inode_u;
	struct qstr dst_name = QSTR(srcname);
	struct qstr src_name = QSTR(dstname);
	subvol_inum src_dir = map_root_ino(src_dir_ino);
	subvol_inum dst_dir = map_root_ino(dst_dir_ino);
	int ret;

	fuse_log(FUSE_LOG_DEBUG,
		 "bcachefs_fuse_rename(%llu, %s, %llu, %s, %x)\n",
		 src_dir.inum, srcname, dst_dir.inum, dstname, flags);

	/* XXX handle overwrites */
	ret = bch2_trans_do(c, NULL, NULL, 0,
		bch2_rename_trans(trans,
				  src_dir, &src_dir_u,
				  dst_dir, &dst_dir_u,
				  &src_inode_u, &dst_inode_u,
				  &src_name, &dst_name,
				  BCH_RENAME));

	fuse_reply_err(req, -ret);
}

static void bcachefs_fuse_link(fuse_req_t req, fuse_ino_t ino,
			       fuse_ino_t newparent_ino, const char *newname)
{
	struct bch_fs *c = fuse_req_userdata(req);
	struct bch_inode_unpacked dir_u, inode_u;
	struct qstr qstr = QSTR(newname);
	subvol_inum newparent	= map_root_ino(newparent_ino);
	subvol_inum inum	= map_root_ino(ino);
	int ret;

	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_link(%llu, %llu, %s)\n",
		 inum.inum, newparent.inum, newname);

	ret = bch2_trans_do(c, NULL, NULL, 0,
			    bch2_link_trans(trans, newparent, &dir_u,
					    inum, &inode_u, &qstr));

	if (!ret) {
		struct fuse_entry_param e = inode_to_entry(c, &inode_u);
		fuse_reply_entry(req, &e);
	} else {
		fuse_reply_err(req, -ret);
	}
}

static void bcachefs_fuse_open(fuse_req_t req, fuse_ino_t inum,
			       struct fuse_file_info *fi)
{
	fi->direct_io		= false;
	fi->keep_cache		= true;
	fi->cache_readdir	= true;

	fuse_reply_open(req, fi);
}

static void userbio_init(struct bio *bio, struct bio_vec *bv,
			 void *buf, size_t size)
{
	bio_init(bio, NULL, bv, 1, 0);
	bio->bi_iter.bi_size	= size;
	bv->bv_page		= buf;
	bv->bv_len		= size;
	bv->bv_offset		= 0;
}

static int get_inode_io_opts(struct bch_fs *c, subvol_inum inum, struct bch_io_opts *opts)
{
	struct bch_inode_unpacked inode;
	if (bch2_inode_find_by_inum(c, inum, &inode))
		return -EINVAL;

	bch2_inode_opts_get(opts, c, &inode);
	return 0;
}

static void bcachefs_fuse_read_endio(struct bio *bio)
{
	closure_put(bio->bi_private);
}


static void bcachefs_fuse_write_endio(struct bch_write_op *op)
{
       struct write_aligned_op_t *w = container_of(op,struct write_aligned_op_t,op);
       closure_put(&w->cl);
}


struct fuse_align_io {
	off_t		start;
	size_t		pad_start;
	off_t		end;
	size_t		pad_end;
	size_t		size;
};

/* Handle unaligned start and end */
/* TODO: align to block_bytes, sector size, or page size? */
static struct fuse_align_io align_io(const struct bch_fs *c, size_t size,
				     off_t offset)
{
	struct fuse_align_io align;

	BUG_ON(offset < 0);

	align.start = round_down(offset, block_bytes(c));
	align.pad_start = offset - align.start;

	off_t end = offset + size;
	align.end = round_up(end, block_bytes(c));
	align.pad_end = align.end - end;

	align.size = align.end - align.start;

	return align;
}

/*
 * Given an aligned number of bytes transferred, figure out how many unaligned
 * bytes were transferred.
 */
static size_t align_fix_up_bytes(const struct fuse_align_io *align,
				 size_t align_bytes)
{
	size_t bytes = 0;

	if (align_bytes > align->pad_start) {
		bytes = align_bytes - align->pad_start;
		bytes = bytes > align->pad_end ? bytes - align->pad_end : 0;
	}

	return bytes;
}

/*
 * Read aligned data.
 */
static int read_aligned(struct bch_fs *c, subvol_inum inum, size_t aligned_size,
			off_t aligned_offset, void *buf)
{
	BUG_ON(aligned_size & (block_bytes(c) - 1));
	BUG_ON(aligned_offset & (block_bytes(c) - 1));

	struct bch_io_opts io_opts;
	if (get_inode_io_opts(c, inum, &io_opts))
		return -ENOENT;

	struct bch_read_bio rbio;
	struct bio_vec bv;
	userbio_init(&rbio.bio, &bv, buf, aligned_size);
	bio_set_op_attrs(&rbio.bio, REQ_OP_READ, REQ_SYNC);
	rbio.bio.bi_iter.bi_sector	= aligned_offset >> 9;

	struct closure cl;
	closure_init_stack(&cl);

	closure_get(&cl);
	rbio.bio.bi_end_io		= bcachefs_fuse_read_endio;
	rbio.bio.bi_private		= &cl;

	bch2_read(c, rbio_init(&rbio.bio, io_opts), inum);

	closure_sync(&cl);

	return -blk_status_to_errno(rbio.bio.bi_status);
}

static void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t ino,
			       size_t size, off_t offset,
			       struct fuse_file_info *fi)
{
	subvol_inum inum = map_root_ino(ino);
	struct bch_fs *c = fuse_req_userdata(req);

	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_read(%llu, %zd, %lld)\n",
		 inum, size, offset);

	/* Check inode size. */
	struct bch_inode_unpacked bi;
	int ret = bch2_inode_find_by_inum(c, inum, &bi);
	if (ret) {
		fuse_reply_err(req, -ret);
		return;
	}

	off_t end = min_t(u64, bi.bi_size, offset + size);
	if (end <= offset) {
		fuse_reply_buf(req, NULL, 0);
		return;
	}
	size = end - offset;

	struct fuse_align_io align = align_io(c, size, offset);

	void *buf = aligned_alloc(PAGE_SIZE, align.size);
	if (!buf) {
		fuse_reply_err(req, ENOMEM);
		return;
	}

	ret = read_aligned(c, inum, align.size, align.start, buf);

	if (likely(!ret))
		fuse_reply_buf(req, buf + align.pad_start, size);
	else
		fuse_reply_err(req, -ret);

	free(buf);
}

static int inode_update_times(struct bch_fs *c, subvol_inum inum)
{
	struct btree_trans *trans;
	struct btree_iter iter;
	struct bch_inode_unpacked inode_u;
	int ret = 0;
	u64 now;

	trans = bch2_trans_get(c);
retry:
	bch2_trans_begin(trans);
	now = bch2_current_time(c);

	ret = bch2_inode_peek(trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
	if (ret)
		goto err;

	inode_u.bi_mtime = now;
	inode_u.bi_ctime = now;

	ret = bch2_inode_write(trans, &iter, &inode_u);
	if (ret)
		goto err;

	ret = bch2_trans_commit(trans, NULL, NULL,
				BCH_TRANS_COMMIT_no_enospc);
err:
        bch2_trans_iter_exit(trans, &iter);
	if (ret == -EINTR)
		goto retry;

	bch2_trans_put(trans);
	return ret;
}

static int write_aligned(struct bch_fs *c, subvol_inum inum,
			 struct bch_io_opts io_opts, void *buf,
			 size_t aligned_size, off_t aligned_offset,
			 off_t new_i_size, size_t *written_out)
{

	struct write_aligned_op_t w = { 0 }
;
	struct bch_write_op	*op = &w.op;
	struct bio_vec		bv;

	BUG_ON(aligned_size & (block_bytes(c) - 1));
	BUG_ON(aligned_offset & (block_bytes(c) - 1));

	*written_out = 0;

	closure_init_stack(&w.cl);

	bch2_write_op_init(op, c, io_opts); /* XXX reads from op?! */
	op->write_point	= writepoint_hashed(0);
	op->nr_replicas	= io_opts.data_replicas;
	op->target	= io_opts.foreground_target;
	op->subvol	= inum.subvol;
	op->pos		= POS(inum.inum, aligned_offset >> 9);
	op->new_i_size	= new_i_size;
	op->end_io = bcachefs_fuse_write_endio;

	userbio_init(&op->wbio.bio, &bv, buf, aligned_size);
	bio_set_op_attrs(&op->wbio.bio, REQ_OP_WRITE, REQ_SYNC);

	if (bch2_disk_reservation_get(c, &op->res, aligned_size >> 9,
				      op->nr_replicas, 0)) {
		/* XXX: use check_range_allocated like dio write path */
		return -ENOSPC;
	}

	closure_get(&w.cl);

	closure_call(&op->cl, bch2_write, NULL, NULL);

	closure_sync(&w.cl);

	if (!op->error)
		*written_out = op->written << 9;

	return op->error;
}

static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t ino,
				const char *buf, size_t size,
				off_t offset,
				struct fuse_file_info *fi)
{
	subvol_inum inum = map_root_ino(ino);
	struct bch_fs *c	= fuse_req_userdata(req);
	struct bch_io_opts	io_opts;
	size_t			aligned_written;
	int			ret = 0;

	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write(%llu, %zd, %lld)\n",
		 inum, size, offset);

	struct fuse_align_io align = align_io(c, size, offset);
	void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
	BUG_ON(!aligned_buf);

	if (get_inode_io_opts(c, inum, &io_opts)) {
		ret = -ENOENT;
		goto err;
	}

	/* Realign the data and read in start and end, if needed */

	/* Read partial start data. */
	if (align.pad_start) {
		memset(aligned_buf, 0, block_bytes(c));

		ret = read_aligned(c, inum, block_bytes(c), align.start,
				   aligned_buf);
		if (ret)
			goto err;
	}

	/*
	 * Read partial end data. If the whole write fits in one block, the
	 * start data and the end data are the same so this isn't needed.
	 */
	if (align.pad_end &&
	    !(align.pad_start && align.size == block_bytes(c))) {
		off_t partial_end_start = align.end - block_bytes(c);
		size_t buf_offset = align.size - block_bytes(c);

		memset(aligned_buf + buf_offset, 0, block_bytes(c));

		ret = read_aligned(c, inum, block_bytes(c), partial_end_start,
				   aligned_buf + buf_offset);
		if (ret)
			goto err;
	}

	/* Overlay what we want to write. */
	memcpy(aligned_buf + align.pad_start, buf, size);

	/* Actually write. */
	ret = write_aligned(c, inum, io_opts, aligned_buf,
			    align.size, align.start,
			    offset + size, &aligned_written);

	/* Figure out how many unaligned bytes were written. */
	size_t written = align_fix_up_bytes(&align, aligned_written);
	BUG_ON(written > size);

	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write: wrote %zd bytes\n",
		 written);

	if (written > 0)
		ret = 0;

	/*
	 * Update inode times.
	 * TODO: Integrate with bch2_extent_update()
	 */
	if (!ret)
		ret = inode_update_times(c, inum);

	if (!ret) {
		BUG_ON(written == 0);
		fuse_reply_write(req, written);
		free(aligned_buf);
		return;
	}

err:
	fuse_reply_err(req, -ret);
	free(aligned_buf);
}

static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
				  fuse_ino_t dir_ino, const char *name)
{
	subvol_inum dir = map_root_ino(dir_ino);
	struct bch_fs *c = fuse_req_userdata(req);
	struct bch_inode_unpacked new_inode;
	size_t link_len = strlen(link);
	int ret;

	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_symlink(%s, %llu, %s)\n",
		 link, dir.inum, name);

	ret = do_create(c, dir, name, S_IFLNK|S_IRWXUGO, 0, &new_inode);
	if (ret)
		goto err;

	struct bch_io_opts io_opts;
	ret = get_inode_io_opts(c, dir, &io_opts);
	if (ret)
		goto err;

	struct fuse_align_io align = align_io(c, link_len + 1, 0);

	void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
	BUG_ON(!aligned_buf);

	memset(aligned_buf, 0, align.size);
	memcpy(aligned_buf, link, link_len); /* already terminated */

	subvol_inum inum = (subvol_inum) { dir.subvol, new_inode.bi_inum };

	size_t aligned_written;
	ret = write_aligned(c, inum, io_opts, aligned_buf,
			    align.size, align.start, link_len + 1,
			    &aligned_written);
	free(aligned_buf);

	if (ret)
		goto err;

	size_t written = align_fix_up_bytes(&align, aligned_written);
	BUG_ON(written != link_len + 1); // TODO: handle short

	ret = inode_update_times(c, inum);
	if (ret)
		goto err;

	new_inode.bi_size = written;

	struct fuse_entry_param e = inode_to_entry(c, &new_inode);
	fuse_reply_entry(req, &e);
	return;

err:
	fuse_reply_err(req, -ret);
}

static void bcachefs_fuse_readlink(fuse_req_t req, fuse_ino_t ino)
{
	subvol_inum inum = map_root_ino(ino);
	struct bch_fs *c = fuse_req_userdata(req);
	char *buf = NULL;

	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readlink(%llu)\n", inum.inum);

	struct bch_inode_unpacked bi;
	int ret = bch2_inode_find_by_inum(c, inum, &bi);
	if (ret)
		goto err;

	struct fuse_align_io align = align_io(c, bi.bi_size, 0);

	ret = -ENOMEM;
	buf = aligned_alloc(PAGE_SIZE, align.size);
	if (!buf)
		goto err;

	ret = read_aligned(c, inum, align.size, align.start, buf);
	if (ret)
		goto err;

	BUG_ON(buf[align.size - 1] != 0);

	fuse_reply_readlink(req, buf);

err:
	if (ret)
		fuse_reply_err(req, -ret);

	free(buf);
}

#if 0
/*
 * FUSE flush is essentially the close() call, however it is not guaranteed
 * that one flush happens per open/create.
 *
 * It doesn't have to do anything, and is mostly relevant for NFS-style
 * filesystems where close has some relationship to caching.
 */
static void bcachefs_fuse_flush(fuse_req_t req, fuse_ino_t inum,
				struct fuse_file_info *fi)
{
	struct bch_fs *c = fuse_req_userdata(req);
}

static void bcachefs_fuse_release(fuse_req_t req, fuse_ino_t inum,
				  struct fuse_file_info *fi)
{
	struct bch_fs *c = fuse_req_userdata(req);
}

static void bcachefs_fuse_fsync(fuse_req_t req, fuse_ino_t inum, int datasync,
				struct fuse_file_info *fi)
{
	struct bch_fs *c = fuse_req_userdata(req);
}

static void bcachefs_fuse_opendir(fuse_req_t req, fuse_ino_t inum,
				  struct fuse_file_info *fi)
{
	struct bch_fs *c = fuse_req_userdata(req);
}
#endif

struct fuse_dir_context {
	struct dir_context	ctx;
	fuse_req_t		req;
	char			*buf;
	size_t			bufsize;
};

struct fuse_dirent {
	uint64_t	ino;
	uint64_t	off;
	uint32_t	namelen;
	uint32_t	type;
	char name[];
};

#define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name)
#define FUSE_DIRENT_ALIGN(x) \
	(((x) + sizeof(uint64_t) - 1) & ~(sizeof(uint64_t) - 1))

static size_t fuse_add_direntry2(char *buf, size_t bufsize,
				 const char *name, int namelen,
				 const struct stat *stbuf, off_t off)
{
	size_t entlen		= FUSE_NAME_OFFSET + namelen;
	size_t entlen_padded	= FUSE_DIRENT_ALIGN(entlen);
	struct fuse_dirent *dirent = (struct fuse_dirent *) buf;

	if ((buf == NULL) || (entlen_padded > bufsize))
		return entlen_padded;

	dirent->ino = stbuf->st_ino;
	dirent->off = off;
	dirent->namelen = namelen;
	dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
	memcpy(dirent->name, name, namelen);
	memset(dirent->name + namelen, 0, entlen_padded - entlen);

	return entlen_padded;
}

static int fuse_filldir(struct dir_context *_ctx,
			const char *name, int namelen,
			loff_t pos, u64 ino, unsigned type)
{
	struct fuse_dir_context *ctx =
		container_of(_ctx, struct fuse_dir_context, ctx);

	struct stat statbuf = {
		.st_ino		= unmap_root_ino(ino),
		.st_mode	= type << 12,
	};

	fuse_log(FUSE_LOG_DEBUG, "fuse_filldir(name=%s inum=%llu pos=%llu)\n",
		 name, statbuf.st_ino, pos);

	size_t len = fuse_add_direntry2(ctx->buf,
					ctx->bufsize,
					name,
					namelen,
					&statbuf,
					pos + 1);

	if (len > ctx->bufsize)
		return -1;

	ctx->buf	+= len;
	ctx->bufsize	-= len;

	return 0;
}

static bool handle_dots(struct fuse_dir_context *ctx, fuse_ino_t dir)
{
	if (ctx->ctx.pos == 0) {
		if (fuse_filldir(&ctx->ctx, ".", 1, ctx->ctx.pos,
				 dir, DT_DIR) < 0)
			return false;
		ctx->ctx.pos = 1;
	}

	if (ctx->ctx.pos == 1) {
		if (fuse_filldir(&ctx->ctx, "..", 2, ctx->ctx.pos,
				 /*TODO: parent*/ 1, DT_DIR) < 0)
			return false;
		ctx->ctx.pos = 2;
	}

	return true;
}

static void bcachefs_fuse_readdir(fuse_req_t req, fuse_ino_t dir_ino,
				  size_t size, off_t off,
				  struct fuse_file_info *fi)
{
	subvol_inum dir = map_root_ino(dir_ino);
	struct bch_fs *c = fuse_req_userdata(req);
	struct bch_inode_unpacked bi;
	char *buf = calloc(size, 1);
	struct fuse_dir_context ctx = {
		.ctx.actor	= fuse_filldir,
		.ctx.pos	= off,
		.req		= req,
		.buf		= buf,
		.bufsize	= size,
	};
	int ret = 0;

	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir(dir=%llu, size=%zu, "
		 "off=%lld)\n", dir.inum, size, off);

	ret = bch2_inode_find_by_inum(c, dir, &bi);
	if (ret)
		goto reply;

	if (!S_ISDIR(bi.bi_mode)) {
		ret = -ENOTDIR;
		goto reply;
	}

	if (!handle_dots(&ctx, dir.inum))
		goto reply;

	ret = bch2_readdir(c, dir, &ctx.ctx);
reply:
	if (!ret) {
		fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir reply %zd\n",
					ctx.buf - buf);
		fuse_reply_buf(req, buf, ctx.buf - buf);
	} else {
		fuse_reply_err(req, -ret);
	}

	free(buf);
}

#if 0
static void bcachefs_fuse_readdirplus(fuse_req_t req, fuse_ino_t dir,
				      size_t size, off_t off,
				      struct fuse_file_info *fi)
{

}

static void bcachefs_fuse_releasedir(fuse_req_t req, fuse_ino_t inum,
				     struct fuse_file_info *fi)
{
	struct bch_fs *c = fuse_req_userdata(req);
}

static void bcachefs_fuse_fsyncdir(fuse_req_t req, fuse_ino_t inum, int datasync,
				   struct fuse_file_info *fi)
{
	struct bch_fs *c = fuse_req_userdata(req);
}
#endif

static void bcachefs_fuse_statfs(fuse_req_t req, fuse_ino_t inum)
{
	struct bch_fs *c = fuse_req_userdata(req);
	struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
	unsigned shift = c->block_bits;
	struct statvfs statbuf = {
		.f_bsize	= block_bytes(c),
		.f_frsize	= block_bytes(c),
		.f_blocks	= usage.capacity >> shift,
		.f_bfree	= (usage.capacity - usage.used) >> shift,
		//.f_bavail	= statbuf.f_bfree,
		.f_files	= usage.nr_inodes,
		.f_ffree	= U64_MAX,
		.f_namemax	= BCH_NAME_MAX,
	};

	fuse_reply_statfs(req, &statbuf);
}

#if 0
static void bcachefs_fuse_setxattr(fuse_req_t req, fuse_ino_t inum,
				   const char *name, const char *value,
				   size_t size, int flags)
{
	struct bch_fs *c = fuse_req_userdata(req);
}

static void bcachefs_fuse_getxattr(fuse_req_t req, fuse_ino_t inum,
				   const char *name, size_t size)
{
	struct bch_fs *c = fuse_req_userdata(req);

	fuse_reply_xattr(req, );
}

static void bcachefs_fuse_listxattr(fuse_req_t req, fuse_ino_t inum, size_t size)
{
	struct bch_fs *c = fuse_req_userdata(req);
}

static void bcachefs_fuse_removexattr(fuse_req_t req, fuse_ino_t inum,
				      const char *name)
{
	struct bch_fs *c = fuse_req_userdata(req);
}
#endif

static void bcachefs_fuse_create(fuse_req_t req, fuse_ino_t dir_ino,
				 const char *name, mode_t mode,
				 struct fuse_file_info *fi)
{
	subvol_inum dir = map_root_ino(dir_ino);
	struct bch_fs *c = fuse_req_userdata(req);
	struct bch_inode_unpacked new_inode;
	int ret;

	fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_create(%llu, %s, %x)\n",
		 dir.inum, name, mode);

	ret = do_create(c, dir, name, mode, 0, &new_inode);
	if (ret)
		goto err;

	struct fuse_entry_param e = inode_to_entry(c, &new_inode);
	fuse_reply_create(req, &e, fi);
	return;
err:
	fuse_reply_err(req, -ret);
}

#if 0
static void bcachefs_fuse_write_buf(fuse_req_t req, fuse_ino_t inum,
				    struct fuse_bufvec *bufv, off_t off,
				    struct fuse_file_info *fi)
{
	struct bch_fs *c = fuse_req_userdata(req);
}

static void bcachefs_fuse_fallocate(fuse_req_t req, fuse_ino_t inum, int mode,
				    off_t offset, off_t length,
				    struct fuse_file_info *fi)
{
	struct bch_fs *c = fuse_req_userdata(req);
}
#endif

static const struct fuse_lowlevel_ops bcachefs_fuse_ops = {
	.init		= bcachefs_fuse_init,
	.destroy	= bcachefs_fuse_destroy,
	.lookup		= bcachefs_fuse_lookup,
	.getattr	= bcachefs_fuse_getattr,
	.setattr	= bcachefs_fuse_setattr,
	.readlink	= bcachefs_fuse_readlink,
	.mknod		= bcachefs_fuse_mknod,
	.mkdir		= bcachefs_fuse_mkdir,
	.unlink		= bcachefs_fuse_unlink,
	.rmdir		= bcachefs_fuse_rmdir,
	.symlink	= bcachefs_fuse_symlink,
	.rename		= bcachefs_fuse_rename,
	.link		= bcachefs_fuse_link,
	.open		= bcachefs_fuse_open,
	.read		= bcachefs_fuse_read,
	.write		= bcachefs_fuse_write,
	//.flush	= bcachefs_fuse_flush,
	//.release	= bcachefs_fuse_release,
	//.fsync	= bcachefs_fuse_fsync,
	//.opendir	= bcachefs_fuse_opendir,
	.readdir	= bcachefs_fuse_readdir,
	//.readdirplus	= bcachefs_fuse_readdirplus,
	//.releasedir	= bcachefs_fuse_releasedir,
	//.fsyncdir	= bcachefs_fuse_fsyncdir,
	.statfs		= bcachefs_fuse_statfs,
	//.setxattr	= bcachefs_fuse_setxattr,
	//.getxattr	= bcachefs_fuse_getxattr,
	//.listxattr	= bcachefs_fuse_listxattr,
	//.removexattr	= bcachefs_fuse_removexattr,
	.create		= bcachefs_fuse_create,

	/* posix locks: */
#if 0
	.getlk		= bcachefs_fuse_getlk,
	.setlk		= bcachefs_fuse_setlk,
#endif
	//.write_buf	= bcachefs_fuse_write_buf,
	//.fallocate	= bcachefs_fuse_fallocate,

};

/*
 * Setup and command parsing.
 */

struct bf_context {
	char            *devices_str;
	char            **devices;
	int             nr_devices;
};

static void bf_context_free(struct bf_context *ctx)
{
	int i;

	free(ctx->devices_str);
	for (i = 0; i < ctx->nr_devices; ++i)
		free(ctx->devices[i]);
	free(ctx->devices);
}

static struct fuse_opt bf_opts[] = {
	FUSE_OPT_END
};

/*
 * Fuse option parsing helper -- returning 0 means we consumed the argument, 1
 * means we did not.
 */
static int bf_opt_proc(void *data, const char *arg, int key,
    struct fuse_args *outargs)
{
	struct bf_context *ctx = data;

	switch (key) {
	case FUSE_OPT_KEY_NONOPT:
		/* Just extract the first non-option string. */
		if (!ctx->devices_str) {
			ctx->devices_str = strdup(arg);
			return 0;
		}
		return 1;
	}

	return 1;
}

/*
 * dev1:dev2 -> [ dev1, dev2 ]
 * dev	     -> [ dev ]
 */
static void tokenize_devices(struct bf_context *ctx)
{
	char *devices_str = strdup(ctx->devices_str);
	char *devices_tmp = devices_str;
	char **devices = NULL;
	int nr = 0;
	char *dev = NULL;

	while ((dev = strsep(&devices_tmp, ":"))) {
		if (strlen(dev) > 0) {
			devices = realloc(devices, (nr + 1) * sizeof *devices);
			devices[nr] = strdup(dev);
			nr++;
		}
	}

	if (!devices) {
		devices = malloc(sizeof *devices);
		devices[0] = strdup(ctx->devices_str);
		nr = 1;
	}

	ctx->devices = devices;
	ctx->nr_devices = nr;

	free(devices_str);
}

static void usage(char *argv[])
{
	printf("Usage: %s fusemount [options] <dev>[:dev2:...] <mountpoint>\n",
	       argv[0]);
	printf("\n");
}

int cmd_fusemount(int argc, char *argv[])
{
	struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
	struct bch_opts bch_opts = bch2_opts_empty();
	struct bf_context ctx = { 0 };
	struct bch_fs *c = NULL;
	int ret = 0, i;

	/* Parse arguments. */
	if (fuse_opt_parse(&args, &ctx, bf_opts, bf_opt_proc) < 0)
		die("fuse_opt_parse err: %m");

	struct fuse_cmdline_opts fuse_opts;
	if (fuse_parse_cmdline(&args, &fuse_opts) < 0)
		die("fuse_parse_cmdline err: %m");

	if (fuse_opts.show_help) {
		usage(argv);
		fuse_cmdline_help();
		fuse_lowlevel_help();
		ret = 0;
		goto out;
	}
	if (fuse_opts.show_version) {
		/* TODO: Show bcachefs version. */
		printf("FUSE library version %s\n", fuse_pkgversion());
		fuse_lowlevel_version();
		ret = 0;
		goto out;
	}
	if (!fuse_opts.mountpoint) {
		usage(argv);
		printf("Please supply a mountpoint.\n");
		ret = 1;
		goto out;
	}
	if (!ctx.devices_str) {
		usage(argv);
		printf("Please specify a device or device1:device2:...\n");
		ret = 1;
		goto out;
	}
	tokenize_devices(&ctx);

	struct printbuf fsname = PRINTBUF;
	prt_printf(&fsname, "fsname=");
	for (i = 0; i < ctx.nr_devices; ++i) {
		if (i)
			prt_str(&fsname, ":");
		prt_str(&fsname, ctx.devices[i]);
	}

	fuse_opt_add_arg(&args, "-o");
	fuse_opt_add_arg(&args, fsname.buf);

	/* Open bch */
	printf("Opening bcachefs filesystem on:\n");
	for (i = 0; i < ctx.nr_devices; ++i)
                printf("\t%s\n", ctx.devices[i]);

	c = bch2_fs_open(ctx.devices, ctx.nr_devices, bch_opts);
	if (IS_ERR(c))
		die("error opening %s: %s", ctx.devices_str,
		    bch2_err_str(PTR_ERR(c)));

	/* Fuse */
	struct fuse_session *se =
		fuse_session_new(&args, &bcachefs_fuse_ops,
				 sizeof(bcachefs_fuse_ops), c);
	if (!se)
		die("fuse_lowlevel_new err: %m");

	if (fuse_set_signal_handlers(se) < 0)
		die("fuse_set_signal_handlers err: %m");

	if (fuse_session_mount(se, fuse_opts.mountpoint))
		die("fuse_mount err: %m");

	/* This print statement is a trigger for tests. */
	printf("Fuse mount initialized.\n");

	if (fuse_opts.foreground == 0){
		printf("Fuse forcing to foreground mode, due gcc constructors usage.\n");
		fuse_opts.foreground = 1;
	}

	fuse_daemonize(fuse_opts.foreground);

	ret = fuse_session_loop(se);

	/* Cleanup */
	fuse_session_unmount(se);
	fuse_remove_signal_handlers(se);
	fuse_session_destroy(se);

out:
	free(fuse_opts.mountpoint);
	fuse_opt_free_args(&args);
	bf_context_free(&ctx);

	return ret ? 1 : 0;
}

#endif /* BCACHEFS_FUSE */