]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_fusemount.c
Implement unaligned read support for fuse.
[bcachefs-tools-debian] / cmd_fusemount.c
1 #include <errno.h>
2 #include <float.h>
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <sys/statvfs.h>
6
7 #include <fuse_lowlevel.h>
8
9 #include "cmds.h"
10 #include "libbcachefs.h"
11 #include "tools-util.h"
12
13 #include "libbcachefs/bcachefs.h"
14 #include "libbcachefs/alloc_foreground.h"
15 #include "libbcachefs/btree_iter.h"
16 #include "libbcachefs/buckets.h"
17 #include "libbcachefs/dirent.h"
18 #include "libbcachefs/error.h"
19 #include "libbcachefs/fs-common.h"
20 #include "libbcachefs/inode.h"
21 #include "libbcachefs/io.h"
22 #include "libbcachefs/opts.h"
23 #include "libbcachefs/super.h"
24
25 /* mode_to_type(): */
26 #include "libbcachefs/fs.h"
27
28 #include <linux/dcache.h>
29
30 /* XXX cut and pasted from fsck.c */
31 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
32
33 static inline u64 map_root_ino(u64 ino)
34 {
35         return ino == 1 ? 4096 : ino;
36 }
37
38 static inline u64 unmap_root_ino(u64 ino)
39 {
40         return ino == 4096 ? 1 : ino;
41 }
42
43 static struct stat inode_to_stat(struct bch_fs *c,
44                                  struct bch_inode_unpacked *bi)
45 {
46         return (struct stat) {
47                 .st_ino         = bi->bi_inum,
48                 .st_size        = bi->bi_size,
49                 .st_mode        = bi->bi_mode,
50                 .st_uid         = bi->bi_uid,
51                 .st_gid         = bi->bi_gid,
52                 .st_nlink       = bch2_inode_nlink_get(bi),
53                 .st_rdev        = bi->bi_dev,
54                 .st_blksize     = block_bytes(c),
55                 .st_blocks      = bi->bi_sectors,
56                 .st_atim        = bch2_time_to_timespec(c, bi->bi_atime),
57                 .st_mtim        = bch2_time_to_timespec(c, bi->bi_mtime),
58                 .st_ctim        = bch2_time_to_timespec(c, bi->bi_ctime),
59         };
60 }
61
62 static struct fuse_entry_param inode_to_entry(struct bch_fs *c,
63                                               struct bch_inode_unpacked *bi)
64 {
65         return (struct fuse_entry_param) {
66                 .ino            = bi->bi_inum,
67                 .generation     = bi->bi_generation,
68                 .attr           = inode_to_stat(c, bi),
69                 .attr_timeout   = DBL_MAX,
70                 .entry_timeout  = DBL_MAX,
71         };
72 }
73
74 static void bcachefs_fuse_init(void *arg, struct fuse_conn_info *conn)
75 {
76         if (conn->capable & FUSE_CAP_WRITEBACK_CACHE) {
77                 fuse_log(FUSE_LOG_DEBUG, "fuse_init: activating writeback\n");
78                 conn->want |= FUSE_CAP_WRITEBACK_CACHE;
79         } else
80                 fuse_log(FUSE_LOG_DEBUG, "fuse_init: writeback not capable\n");
81
82         //conn->want |= FUSE_CAP_POSIX_ACL;
83 }
84
85 static void bcachefs_fuse_destroy(void *arg)
86 {
87         struct bch_fs *c = arg;
88
89         bch2_fs_stop(c);
90 }
91
92 static void bcachefs_fuse_lookup(fuse_req_t req, fuse_ino_t dir,
93                                  const char *name)
94 {
95         struct bch_fs *c = fuse_req_userdata(req);
96         struct bch_inode_unpacked bi;
97         struct qstr qstr = QSTR(name);
98         u64 inum;
99         int ret;
100
101         dir = map_root_ino(dir);
102
103         ret = bch2_inode_find_by_inum(c, dir, &bi);
104         if (ret) {
105                 fuse_reply_err(req, -ret);
106                 return;
107         }
108
109         struct bch_hash_info hash_info = bch2_hash_info_init(c, &bi);
110
111         inum = bch2_dirent_lookup(c, dir, &hash_info, &qstr);
112         if (!inum) {
113                 ret = -ENOENT;
114                 goto err;
115         }
116
117         ret = bch2_inode_find_by_inum(c, inum, &bi);
118         if (ret)
119                 goto err;
120
121         bi.bi_inum = unmap_root_ino(bi.bi_inum);
122
123         struct fuse_entry_param e = inode_to_entry(c, &bi);
124         fuse_reply_entry(req, &e);
125         return;
126 err:
127         fuse_reply_err(req, -ret);
128 }
129
130 static void bcachefs_fuse_getattr(fuse_req_t req, fuse_ino_t inum,
131                                   struct fuse_file_info *fi)
132 {
133         struct bch_fs *c = fuse_req_userdata(req);
134         struct bch_inode_unpacked bi;
135         struct stat attr;
136         int ret;
137
138         inum = map_root_ino(inum);
139
140         ret = bch2_inode_find_by_inum(c, inum, &bi);
141         if (ret) {
142                 fuse_reply_err(req, -ret);
143                 return;
144         }
145
146         bi.bi_inum = unmap_root_ino(bi.bi_inum);
147
148         attr = inode_to_stat(c, &bi);
149         fuse_reply_attr(req, &attr, DBL_MAX);
150 }
151
152 static void bcachefs_fuse_setattr(fuse_req_t req, fuse_ino_t inum,
153                                   struct stat *attr, int to_set,
154                                   struct fuse_file_info *fi)
155 {
156         struct bch_fs *c = fuse_req_userdata(req);
157         struct bch_inode_unpacked inode_u;
158         struct btree_trans trans;
159         struct btree_iter *iter;
160         u64 now;
161         int ret;
162
163         inum = map_root_ino(inum);
164
165         bch2_trans_init(&trans, c, 0, 0);
166 retry:
167         bch2_trans_begin(&trans);
168         now = bch2_current_time(c);
169
170         iter = bch2_inode_peek(&trans, &inode_u, inum, BTREE_ITER_INTENT);
171         ret = PTR_ERR_OR_ZERO(iter);
172         if (ret)
173                 goto err;
174
175         if (to_set & FUSE_SET_ATTR_MODE)
176                 inode_u.bi_mode = attr->st_mode;
177         if (to_set & FUSE_SET_ATTR_UID)
178                 inode_u.bi_uid  = attr->st_uid;
179         if (to_set & FUSE_SET_ATTR_GID)
180                 inode_u.bi_gid  = attr->st_gid;
181         if (to_set & FUSE_SET_ATTR_SIZE)
182                 inode_u.bi_size = attr->st_size;
183         if (to_set & FUSE_SET_ATTR_ATIME)
184                 inode_u.bi_atime = timespec_to_bch2_time(c, attr->st_atim);
185         if (to_set & FUSE_SET_ATTR_MTIME)
186                 inode_u.bi_mtime = timespec_to_bch2_time(c, attr->st_mtim);
187         if (to_set & FUSE_SET_ATTR_ATIME_NOW)
188                 inode_u.bi_atime = now;
189         if (to_set & FUSE_SET_ATTR_MTIME_NOW)
190                 inode_u.bi_mtime = now;
191         /* TODO: CTIME? */
192
193         ret   = bch2_inode_write(&trans, iter, &inode_u) ?:
194                 bch2_trans_commit(&trans, NULL, NULL,
195                                   BTREE_INSERT_ATOMIC|
196                                   BTREE_INSERT_NOFAIL);
197 err:
198         if (ret == -EINTR)
199                 goto retry;
200
201         bch2_trans_exit(&trans);
202
203         if (!ret) {
204                 *attr = inode_to_stat(c, &inode_u);
205                 fuse_reply_attr(req, attr, DBL_MAX);
206         } else {
207                 fuse_reply_err(req, -ret);
208         }
209 }
210
211 static void bcachefs_fuse_readlink(fuse_req_t req, fuse_ino_t inum)
212 {
213         //struct bch_fs *c = fuse_req_userdata(req);
214
215         //char *link = malloc();
216
217         //fuse_reply_readlink(req, link);
218 }
219
220 static int do_create(struct bch_fs *c, u64 dir,
221                      const char *name, mode_t mode, dev_t rdev,
222                      struct bch_inode_unpacked *new_inode)
223 {
224         struct qstr qstr = QSTR(name);
225         struct bch_inode_unpacked dir_u;
226
227         dir = map_root_ino(dir);
228
229         bch2_inode_init_early(c, new_inode);
230
231         return bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
232                         bch2_create_trans(&trans,
233                                 dir, &dir_u,
234                                 new_inode, &qstr,
235                                 0, 0, mode, rdev, NULL, NULL));
236 }
237
238 static void bcachefs_fuse_mknod(fuse_req_t req, fuse_ino_t dir,
239                                 const char *name, mode_t mode,
240                                 dev_t rdev)
241 {
242         struct bch_fs *c = fuse_req_userdata(req);
243         struct bch_inode_unpacked new_inode;
244         int ret;
245
246         ret = do_create(c, dir, name, mode, rdev, &new_inode);
247         if (ret)
248                 goto err;
249
250         struct fuse_entry_param e = inode_to_entry(c, &new_inode);
251         fuse_reply_entry(req, &e);
252         return;
253 err:
254         fuse_reply_err(req, -ret);
255 }
256
257 static void bcachefs_fuse_mkdir(fuse_req_t req, fuse_ino_t dir,
258                                 const char *name, mode_t mode)
259 {
260         bcachefs_fuse_mknod(req, dir, name, mode, 0);
261 }
262
263 static void bcachefs_fuse_unlink(fuse_req_t req, fuse_ino_t dir,
264                                  const char *name)
265 {
266         struct bch_fs *c = fuse_req_userdata(req);
267         struct bch_inode_unpacked dir_u, inode_u;
268         struct qstr qstr = QSTR(name);
269         int ret;
270
271         dir = map_root_ino(dir);
272
273         ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC|BTREE_INSERT_NOFAIL,
274                             bch2_unlink_trans(&trans, dir, &dir_u,
275                                               &inode_u, &qstr));
276
277         fuse_reply_err(req, -ret);
278 }
279
280 static void bcachefs_fuse_rmdir(fuse_req_t req, fuse_ino_t dir,
281                                 const char *name)
282 {
283         dir = map_root_ino(dir);
284
285         bcachefs_fuse_unlink(req, dir, name);
286 }
287
288 #if 0
289 static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
290                                   fuse_ino_t parent, const char *name)
291 {
292         struct bch_fs *c = fuse_req_userdata(req);
293 }
294 #endif
295
296 static void bcachefs_fuse_rename(fuse_req_t req,
297                                  fuse_ino_t src_dir, const char *srcname,
298                                  fuse_ino_t dst_dir, const char *dstname,
299                                  unsigned flags)
300 {
301         struct bch_fs *c = fuse_req_userdata(req);
302         struct bch_inode_unpacked dst_dir_u, src_dir_u;
303         struct bch_inode_unpacked src_inode_u, dst_inode_u;
304         struct qstr dst_name = QSTR(srcname);
305         struct qstr src_name = QSTR(dstname);
306         int ret;
307
308         src_dir = map_root_ino(src_dir);
309         dst_dir = map_root_ino(dst_dir);
310
311         /* XXX handle overwrites */
312         ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
313                 bch2_rename_trans(&trans,
314                                   src_dir, &src_dir_u,
315                                   dst_dir, &dst_dir_u,
316                                   &src_inode_u, &dst_inode_u,
317                                   &src_name, &dst_name,
318                                   BCH_RENAME));
319
320         fuse_reply_err(req, -ret);
321 }
322
323 static void bcachefs_fuse_link(fuse_req_t req, fuse_ino_t inum,
324                                fuse_ino_t newparent, const char *newname)
325 {
326         struct bch_fs *c = fuse_req_userdata(req);
327         struct bch_inode_unpacked inode_u;
328         struct qstr qstr = QSTR(newname);
329         int ret;
330
331         ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
332                             bch2_link_trans(&trans, newparent,
333                                             inum, &inode_u, &qstr));
334
335         if (!ret) {
336                 struct fuse_entry_param e = inode_to_entry(c, &inode_u);
337                 fuse_reply_entry(req, &e);
338         } else {
339                 fuse_reply_err(req, -ret);
340         }
341 }
342
343 static void bcachefs_fuse_open(fuse_req_t req, fuse_ino_t inum,
344                                struct fuse_file_info *fi)
345 {
346         fi->direct_io           = false;
347         fi->keep_cache          = true;
348         fi->cache_readdir       = true;
349
350         fuse_reply_open(req, fi);
351 }
352
353 static void userbio_init(struct bio *bio, struct bio_vec *bv,
354                          void *buf, size_t size)
355 {
356         bio_init(bio, bv, 1);
357         bio->bi_iter.bi_size    = size;
358         bv->bv_page             = buf;
359         bv->bv_len              = size;
360         bv->bv_offset           = 0;
361 }
362
363 static int get_inode_io_opts(struct bch_fs *c, u64 inum,
364                              struct bch_io_opts *opts)
365 {
366         struct bch_inode_unpacked inode;
367         if (bch2_inode_find_by_inum(c, inum, &inode))
368                 return -EINVAL;
369
370         *opts = bch2_opts_to_inode_opts(c->opts);
371         bch2_io_opts_apply(opts, bch2_inode_opts_get(&inode));
372         return 0;
373 }
374
375 static void bcachefs_fuse_read_endio(struct bio *bio)
376 {
377         closure_put(bio->bi_private);
378 }
379
380 static int read_aligned(struct bch_fs *c, fuse_ino_t inum, size_t aligned_size,
381                         off_t aligned_offset, void *buf)
382 {
383         BUG_ON(aligned_size & (block_bytes(c) - 1));
384         BUG_ON(aligned_offset & (block_bytes(c) - 1));
385
386         struct bch_io_opts io_opts;
387         if (get_inode_io_opts(c, inum, &io_opts))
388                 return -ENOENT;
389
390         struct bch_read_bio rbio;
391         struct bio_vec bv;
392         userbio_init(&rbio.bio, &bv, buf, aligned_size);
393         bio_set_op_attrs(&rbio.bio, REQ_OP_READ, REQ_SYNC);
394         rbio.bio.bi_iter.bi_sector      = aligned_offset >> 9;
395
396         struct closure cl;
397         closure_init_stack(&cl);
398
399         closure_get(&cl);
400         rbio.bio.bi_end_io              = bcachefs_fuse_read_endio;
401         rbio.bio.bi_private             = &cl;
402
403         bch2_read(c, rbio_init(&rbio.bio, io_opts), inum);
404
405         closure_sync(&cl);
406
407         return -blk_status_to_errno(rbio.bio.bi_status);
408 }
409
410 static void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t inum,
411                                size_t size, off_t offset,
412                                struct fuse_file_info *fi)
413 {
414         struct bch_fs *c = fuse_req_userdata(req);
415
416         /* Handle unaligned start and end */
417         /* TODO: align to block_bytes, sector size, or page size? */
418         BUG_ON(offset < 0);
419         off_t aligned_start = round_down(offset, block_bytes(c));
420         unsigned aligned_offset = offset - aligned_start;
421
422         off_t aligned_end = round_up(offset + size, block_bytes(c));
423         size_t aligned_size = aligned_end - aligned_start;
424
425         void *buf = aligned_alloc(PAGE_SIZE, aligned_size);
426         if (!buf) {
427                 fuse_reply_err(req, ENOMEM);
428                 return;
429         }
430
431         int ret = read_aligned(c, inum, aligned_size, aligned_start, buf);
432
433         if (likely(!ret))
434                 fuse_reply_buf(req, buf + aligned_offset, size);
435         else
436                 fuse_reply_err(req, -ret);
437
438         free(buf);
439 }
440
441 static int write_set_inode(struct bch_fs *c, fuse_ino_t inum, off_t new_size)
442 {
443         struct btree_trans trans;
444         struct btree_iter *iter;
445         struct bch_inode_unpacked inode_u;
446         int ret = 0;
447         u64 now;
448
449         bch2_trans_init(&trans, c, 0, 0);
450 retry:
451         bch2_trans_begin(&trans);
452         now = bch2_current_time(c);
453
454         iter = bch2_inode_peek(&trans, &inode_u, inum, BTREE_ITER_INTENT);
455         ret = PTR_ERR_OR_ZERO(iter);
456         if (ret)
457                 goto err;
458
459         inode_u.bi_size = max_t(u64, inode_u.bi_size, new_size);
460         inode_u.bi_mtime = now;
461         inode_u.bi_ctime = now;
462
463         ret = bch2_inode_write(&trans, iter, &inode_u);
464         if (ret)
465                 goto err;
466
467         ret = bch2_trans_commit(&trans, NULL, NULL,
468                                 BTREE_INSERT_ATOMIC|BTREE_INSERT_NOFAIL);
469
470 err:
471         if (ret == -EINTR)
472                 goto retry;
473
474         bch2_trans_exit(&trans);
475         return ret;
476 }
477
478 static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t inum,
479                                 const char *buf, size_t size,
480                                 off_t offset,
481                                 struct fuse_file_info *fi)
482 {
483         struct bch_fs *c        = fuse_req_userdata(req);
484         struct bch_io_opts      io_opts;
485         struct bch_write_op     op;
486         struct bio_vec          bv;
487         struct closure          cl;
488
489         if ((size|offset) & (block_bytes(c) - 1)) {
490                 fuse_log(FUSE_LOG_DEBUG,
491                          "bcachefs_fuse_write: unaligned io not supported.\n");
492                 fuse_reply_err(req, EINVAL);
493                 return;
494         }
495
496         closure_init_stack(&cl);
497
498         if (get_inode_io_opts(c, inum, &io_opts)) {
499                 fuse_reply_err(req, ENOENT);
500                 return;
501         }
502
503         bch2_write_op_init(&op, c, io_opts);
504         op.write_point  = writepoint_hashed(0);
505         op.nr_replicas  = io_opts.data_replicas;
506         op.target       = io_opts.foreground_target;
507         op.pos          = POS(inum, offset >> 9);
508
509         userbio_init(&op.wbio.bio, &bv, (void *) buf, size);
510         bio_set_op_attrs(&op.wbio.bio, REQ_OP_WRITE, REQ_SYNC);
511
512         if (bch2_disk_reservation_get(c, &op.res, size >> 9,
513                                       op.nr_replicas, 0)) {
514                 /* XXX: use check_range_allocated like dio write path */
515                 fuse_reply_err(req, ENOSPC);
516                 return;
517         }
518
519         closure_call(&op.cl, bch2_write, NULL, &cl);
520         closure_sync(&cl);
521
522         /*
523          * Update inode data.
524          * TODO: could possibly do asynchronously.
525          * TODO: could also possibly do atomically with the extents.
526          */
527         if (!op.error)
528                 op.error = write_set_inode(c, inum, offset + size);
529
530         if (!op.error) {
531                 BUG_ON(op.written == 0);
532                 fuse_reply_write(req, (size_t) op.written << 9);
533         } else {
534                 BUG_ON(!op.error);
535                 fuse_reply_err(req, -op.error);
536         }
537 }
538
539 #if 0
540 /*
541  * FUSE flush is essentially the close() call, however it is not guaranteed
542  * that one flush happens per open/create.
543  *
544  * It doesn't have to do anything, and is mostly relevant for NFS-style
545  * filesystems where close has some relationship to caching.
546  */
547 static void bcachefs_fuse_flush(fuse_req_t req, fuse_ino_t inum,
548                                 struct fuse_file_info *fi)
549 {
550         struct bch_fs *c = fuse_req_userdata(req);
551 }
552
553 static void bcachefs_fuse_release(fuse_req_t req, fuse_ino_t inum,
554                                   struct fuse_file_info *fi)
555 {
556         struct bch_fs *c = fuse_req_userdata(req);
557 }
558
559 static void bcachefs_fuse_fsync(fuse_req_t req, fuse_ino_t inum, int datasync,
560                                 struct fuse_file_info *fi)
561 {
562         struct bch_fs *c = fuse_req_userdata(req);
563 }
564
565 static void bcachefs_fuse_opendir(fuse_req_t req, fuse_ino_t inum,
566                                   struct fuse_file_info *fi)
567 {
568         struct bch_fs *c = fuse_req_userdata(req);
569 }
570 #endif
571
572 struct fuse_dir_entry {
573         u64             ino;
574         unsigned        type;
575         char            name[0];
576 };
577
578 struct fuse_dir_context {
579         struct dir_context      ctx;
580         fuse_req_t              req;
581         char                    *buf;
582         size_t                  bufsize;
583
584         struct fuse_dir_entry   *prev;
585 };
586
587 static int fuse_send_dir_entry(struct fuse_dir_context *ctx, loff_t pos)
588 {
589         struct fuse_dir_entry *de = ctx->prev;
590         ctx->prev = NULL;
591
592         struct stat statbuf = {
593                 .st_ino         = unmap_root_ino(de->ino),
594                 .st_mode        = de->type << 12,
595         };
596
597         size_t len = fuse_add_direntry(ctx->req, ctx->buf, ctx->bufsize,
598                                        de->name, &statbuf, pos);
599
600         free(de);
601
602         if (len > ctx->bufsize)
603                 return -EINVAL;
604
605         ctx->buf        += len;
606         ctx->bufsize    -= len;
607
608         return 0;
609 }
610
611 static int fuse_filldir(struct dir_context *_ctx,
612                         const char *name, int namelen,
613                         loff_t pos, u64 ino, unsigned type)
614 {
615         struct fuse_dir_context *ctx =
616                 container_of(_ctx, struct fuse_dir_context, ctx);
617
618         fuse_log(FUSE_LOG_DEBUG, "fuse_filldir(ctx={.ctx={.pos=%llu}}, "
619                  "name=%s, namelen=%d, pos=%lld, dir=%llu, type=%u)\n",
620                  ctx->ctx.pos, name, namelen, pos, ino, type);
621
622         /*
623          * We have to emit directory entries after reading the next entry,
624          * because the previous entry contains a pointer to next.
625          */
626         if (ctx->prev) {
627                 int ret = fuse_send_dir_entry(ctx, pos);
628                 if (ret)
629                         return ret;
630         }
631
632         struct fuse_dir_entry *cur = malloc(sizeof *cur + namelen + 1);
633         cur->ino = ino;
634         cur->type = type;
635         memcpy(cur->name, name, namelen);
636         cur->name[namelen] = 0;
637
638         ctx->prev = cur;
639
640         return 0;
641 }
642
643 static bool handle_dots(struct fuse_dir_context *ctx, fuse_ino_t dir)
644 {
645         int ret = 0;
646
647         if (ctx->ctx.pos == 0) {
648                 ret = fuse_filldir(&ctx->ctx, ".", 1, ctx->ctx.pos,
649                                    unmap_root_ino(dir), DT_DIR);
650                 if (ret < 0)
651                         return false;
652                 ctx->ctx.pos = 1;
653         }
654
655         if (ctx->ctx.pos == 1) {
656                 ret = fuse_filldir(&ctx->ctx, "..", 2, ctx->ctx.pos,
657                                    /*TODO: parent*/ 1, DT_DIR);
658                 if (ret < 0)
659                         return false;
660                 ctx->ctx.pos = 2;
661         }
662
663         return true;
664 }
665
666 static void bcachefs_fuse_readdir(fuse_req_t req, fuse_ino_t dir,
667                                   size_t size, off_t off,
668                                   struct fuse_file_info *fi)
669 {
670         struct bch_fs *c = fuse_req_userdata(req);
671         struct bch_inode_unpacked bi;
672         char *buf = calloc(size, 1);
673         struct fuse_dir_context ctx = {
674                 .ctx.actor      = fuse_filldir,
675                 .ctx.pos        = off,
676                 .req            = req,
677                 .buf            = buf,
678                 .bufsize        = size,
679         };
680         int ret = 0;
681
682         fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir(dir=%llu, size=%zu, "
683                  "off=%lld)\n", dir, size, off);
684
685         dir = map_root_ino(dir);
686
687         ret = bch2_inode_find_by_inum(c, dir, &bi);
688         if (ret)
689                 goto reply;
690
691         if (!S_ISDIR(bi.bi_mode)) {
692                 ret = -ENOTDIR;
693                 goto reply;
694         }
695
696         if (!handle_dots(&ctx, dir))
697                 goto reply;
698
699         ret = bch2_readdir(c, dir, &ctx.ctx);
700
701 reply:
702         /*
703          * If we have something to send, the error above doesn't matter.
704          *
705          * Alternatively, if this send fails, but we previously sent something,
706          * then this is a success.
707          */
708         if (ctx.prev) {
709                 ret = fuse_send_dir_entry(&ctx, ctx.ctx.pos);
710                 if (ret && ctx.buf != buf)
711                         ret = 0;
712         }
713
714         if (!ret) {
715                 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir reply %zd\n",
716                                         ctx.buf - buf);
717                 fuse_reply_buf(req, buf, ctx.buf - buf);
718         } else {
719                 fuse_reply_err(req, -ret);
720         }
721
722         free(buf);
723 }
724
725 #if 0
726 static void bcachefs_fuse_readdirplus(fuse_req_t req, fuse_ino_t dir,
727                                       size_t size, off_t off,
728                                       struct fuse_file_info *fi)
729 {
730
731 }
732
733 static void bcachefs_fuse_releasedir(fuse_req_t req, fuse_ino_t inum,
734                                      struct fuse_file_info *fi)
735 {
736         struct bch_fs *c = fuse_req_userdata(req);
737 }
738
739 static void bcachefs_fuse_fsyncdir(fuse_req_t req, fuse_ino_t inum, int datasync,
740                                    struct fuse_file_info *fi)
741 {
742         struct bch_fs *c = fuse_req_userdata(req);
743 }
744 #endif
745
746 static void bcachefs_fuse_statfs(fuse_req_t req, fuse_ino_t inum)
747 {
748         struct bch_fs *c = fuse_req_userdata(req);
749         struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
750         unsigned shift = c->block_bits;
751         struct statvfs statbuf = {
752                 .f_bsize        = block_bytes(c),
753                 .f_frsize       = block_bytes(c),
754                 .f_blocks       = usage.capacity >> shift,
755                 .f_bfree        = (usage.capacity - usage.used) >> shift,
756                 //.f_bavail     = statbuf.f_bfree,
757                 .f_files        = usage.nr_inodes,
758                 .f_ffree        = U64_MAX,
759                 .f_namemax      = BCH_NAME_MAX,
760         };
761
762         fuse_reply_statfs(req, &statbuf);
763 }
764
765 #if 0
766 static void bcachefs_fuse_setxattr(fuse_req_t req, fuse_ino_t inum,
767                                    const char *name, const char *value,
768                                    size_t size, int flags)
769 {
770         struct bch_fs *c = fuse_req_userdata(req);
771 }
772
773 static void bcachefs_fuse_getxattr(fuse_req_t req, fuse_ino_t inum,
774                                    const char *name, size_t size)
775 {
776         struct bch_fs *c = fuse_req_userdata(req);
777
778         fuse_reply_xattr(req, );
779 }
780
781 static void bcachefs_fuse_listxattr(fuse_req_t req, fuse_ino_t inum, size_t size)
782 {
783         struct bch_fs *c = fuse_req_userdata(req);
784 }
785
786 static void bcachefs_fuse_removexattr(fuse_req_t req, fuse_ino_t inum,
787                                       const char *name)
788 {
789         struct bch_fs *c = fuse_req_userdata(req);
790 }
791 #endif
792
793 static void bcachefs_fuse_create(fuse_req_t req, fuse_ino_t dir,
794                                  const char *name, mode_t mode,
795                                  struct fuse_file_info *fi)
796 {
797         struct bch_fs *c = fuse_req_userdata(req);
798         struct bch_inode_unpacked new_inode;
799         int ret;
800
801         ret = do_create(c, dir, name, mode, 0, &new_inode);
802         if (ret)
803                 goto err;
804
805         struct fuse_entry_param e = inode_to_entry(c, &new_inode);
806         fuse_reply_create(req, &e, fi);
807         return;
808 err:
809         fuse_reply_err(req, -ret);
810
811 }
812
813 #if 0
814 static void bcachefs_fuse_write_buf(fuse_req_t req, fuse_ino_t inum,
815                                     struct fuse_bufvec *bufv, off_t off,
816                                     struct fuse_file_info *fi)
817 {
818         struct bch_fs *c = fuse_req_userdata(req);
819 }
820
821 static void bcachefs_fuse_fallocate(fuse_req_t req, fuse_ino_t inum, int mode,
822                                     off_t offset, off_t length,
823                                     struct fuse_file_info *fi)
824 {
825         struct bch_fs *c = fuse_req_userdata(req);
826 }
827 #endif
828
829 static const struct fuse_lowlevel_ops bcachefs_fuse_ops = {
830         .init           = bcachefs_fuse_init,
831         .destroy        = bcachefs_fuse_destroy,
832         .lookup         = bcachefs_fuse_lookup,
833         .getattr        = bcachefs_fuse_getattr,
834         .setattr        = bcachefs_fuse_setattr,
835         .readlink       = bcachefs_fuse_readlink,
836         .mknod          = bcachefs_fuse_mknod,
837         .mkdir          = bcachefs_fuse_mkdir,
838         .unlink         = bcachefs_fuse_unlink,
839         .rmdir          = bcachefs_fuse_rmdir,
840         //.symlink      = bcachefs_fuse_symlink,
841         .rename         = bcachefs_fuse_rename,
842         .link           = bcachefs_fuse_link,
843         .open           = bcachefs_fuse_open,
844         .read           = bcachefs_fuse_read,
845         .write          = bcachefs_fuse_write,
846         //.flush        = bcachefs_fuse_flush,
847         //.release      = bcachefs_fuse_release,
848         //.fsync        = bcachefs_fuse_fsync,
849         //.opendir      = bcachefs_fuse_opendir,
850         .readdir        = bcachefs_fuse_readdir,
851         //.readdirplus  = bcachefs_fuse_readdirplus,
852         //.releasedir   = bcachefs_fuse_releasedir,
853         //.fsyncdir     = bcachefs_fuse_fsyncdir,
854         .statfs         = bcachefs_fuse_statfs,
855         //.setxattr     = bcachefs_fuse_setxattr,
856         //.getxattr     = bcachefs_fuse_getxattr,
857         //.listxattr    = bcachefs_fuse_listxattr,
858         //.removexattr  = bcachefs_fuse_removexattr,
859         .create         = bcachefs_fuse_create,
860
861         /* posix locks: */
862 #if 0
863         .getlk          = bcachefs_fuse_getlk,
864         .setlk          = bcachefs_fuse_setlk,
865 #endif
866         //.write_buf    = bcachefs_fuse_write_buf,
867         //.fallocate    = bcachefs_fuse_fallocate,
868
869 };
870
871 /*
872  * Setup and command parsing.
873  */
874
875 struct bf_context {
876         char            *devices_str;
877         char            **devices;
878         int             nr_devices;
879 };
880
881 static void bf_context_free(struct bf_context *ctx)
882 {
883         int i;
884
885         free(ctx->devices_str);
886         for (i = 0; i < ctx->nr_devices; ++i)
887                 free(ctx->devices[i]);
888         free(ctx->devices);
889 }
890
891 static struct fuse_opt bf_opts[] = {
892         FUSE_OPT_END
893 };
894
895 /*
896  * Fuse option parsing helper -- returning 0 means we consumed the argument, 1
897  * means we did not.
898  */
899 static int bf_opt_proc(void *data, const char *arg, int key,
900     struct fuse_args *outargs)
901 {
902         struct bf_context *ctx = data;
903
904         switch (key) {
905         case FUSE_OPT_KEY_NONOPT:
906                 /* Just extract the first non-option string. */
907                 if (!ctx->devices_str) {
908                         ctx->devices_str = strdup(arg);
909                         return 0;
910                 }
911                 return 1;
912         }
913
914         return 1;
915 }
916
917 /*
918  * dev1:dev2 -> [ dev1, dev2 ]
919  * dev       -> [ dev ]
920  */
921 static void tokenize_devices(struct bf_context *ctx)
922 {
923         char *devices_str = strdup(ctx->devices_str);
924         char *devices_tmp = devices_str;
925         char **devices = NULL;
926         int nr = 0;
927         char *dev = NULL;
928
929         while ((dev = strsep(&devices_tmp, ":"))) {
930                 if (strlen(dev) > 0) {
931                         devices = realloc(devices, (nr + 1) * sizeof *devices);
932                         devices[nr] = strdup(dev);
933                         nr++;
934                 }
935         }
936
937         if (!devices) {
938                 devices = malloc(sizeof *devices);
939                 devices[0] = strdup(ctx->devices_str);
940                 nr = 1;
941         }
942
943         ctx->devices = devices;
944         ctx->nr_devices = nr;
945
946         free(devices_str);
947 }
948
949 static void usage(char *argv[])
950 {
951         printf("Usage: %s fusemount [options] <dev>[:dev2:...] <mountpoint>\n",
952                argv[0]);
953         printf("\n");
954 }
955
956 int cmd_fusemount(int argc, char *argv[])
957 {
958         struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
959         struct bch_opts bch_opts = bch2_opts_empty();
960         struct bf_context ctx = { 0 };
961         struct bch_fs *c = NULL;
962         int ret = 0, i;
963
964         /* Parse arguments. */
965         if (fuse_opt_parse(&args, &ctx, bf_opts, bf_opt_proc) < 0)
966                 die("fuse_opt_parse err: %m");
967
968         struct fuse_cmdline_opts fuse_opts;
969         if (fuse_parse_cmdline(&args, &fuse_opts) < 0)
970                 die("fuse_parse_cmdline err: %m");
971
972         if (fuse_opts.show_help) {
973                 usage(argv);
974                 fuse_cmdline_help();
975                 fuse_lowlevel_help();
976                 ret = 0;
977                 goto out;
978         }
979         if (fuse_opts.show_version) {
980                 /* TODO: Show bcachefs version. */
981                 printf("FUSE library version %s\n", fuse_pkgversion());
982                 fuse_lowlevel_version();
983                 ret = 0;
984                 goto out;
985         }
986         if (!fuse_opts.mountpoint) {
987                 usage(argv);
988                 printf("Please supply a mountpoint.\n");
989                 ret = 1;
990                 goto out;
991         }
992         if (!ctx.devices_str) {
993                 usage(argv);
994                 printf("Please specify a device or device1:device2:...\n");
995                 ret = 1;
996                 goto out;
997         }
998         tokenize_devices(&ctx);
999
1000         /* Open bch */
1001         printf("Opening bcachefs filesystem on:\n");
1002         for (i = 0; i < ctx.nr_devices; ++i)
1003                 printf("\t%s\n", ctx.devices[i]);
1004
1005         c = bch2_fs_open(ctx.devices, ctx.nr_devices, bch_opts);
1006         if (IS_ERR(c))
1007                 die("error opening %s: %s", ctx.devices_str,
1008                     strerror(-PTR_ERR(c)));
1009
1010         /* Fuse */
1011         struct fuse_session *se =
1012                 fuse_session_new(&args, &bcachefs_fuse_ops,
1013                                  sizeof(bcachefs_fuse_ops), c);
1014         if (!se)
1015                 die("fuse_lowlevel_new err: %m");
1016
1017         if (fuse_set_signal_handlers(se) < 0)
1018                 die("fuse_set_signal_handlers err: %m");
1019
1020         if (fuse_session_mount(se, fuse_opts.mountpoint))
1021                 die("fuse_mount err: %m");
1022
1023         fuse_daemonize(fuse_opts.foreground);
1024
1025         ret = fuse_session_loop(se);
1026
1027         /* Cleanup */
1028         fuse_session_unmount(se);
1029         fuse_remove_signal_handlers(se);
1030         fuse_session_destroy(se);
1031
1032 out:
1033         free(fuse_opts.mountpoint);
1034         fuse_opt_free_args(&args);
1035         bf_context_free(&ctx);
1036
1037         return ret ? 1 : 0;
1038 }