]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_fusemount.c
Make fuse read and write work (for aligned writes).
[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 void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t inum,
381                                size_t size, off_t offset,
382                                struct fuse_file_info *fi)
383 {
384         struct bch_fs *c = fuse_req_userdata(req);
385
386         if ((size|offset) & (block_bytes(c) - 1)) {
387                 fuse_log(FUSE_LOG_DEBUG,
388                          "bcachefs_fuse_read: unaligned io not supported.\n");
389                 fuse_reply_err(req, EINVAL);
390                 return;
391         }
392
393         struct bch_io_opts io_opts;
394         if (get_inode_io_opts(c, inum, &io_opts)) {
395                 fuse_reply_err(req, ENOENT);
396                 return;
397         }
398
399         void *buf = aligned_alloc(max(PAGE_SIZE, size), size);
400         if (!buf) {
401                 fuse_reply_err(req, ENOMEM);
402                 return;
403         }
404
405         struct bch_read_bio     rbio;
406         struct bio_vec          bv;
407         struct closure          cl;
408
409         closure_init_stack(&cl);
410         userbio_init(&rbio.bio, &bv, buf, size);
411         bio_set_op_attrs(&rbio.bio, REQ_OP_READ, REQ_SYNC);
412         rbio.bio.bi_iter.bi_sector      = offset >> 9;
413         closure_get(&cl);
414         rbio.bio.bi_end_io              = bcachefs_fuse_read_endio;
415         rbio.bio.bi_private             = &cl;
416
417         bch2_read(c, rbio_init(&rbio.bio, io_opts), inum);
418
419         closure_sync(&cl);
420
421         if (likely(!rbio.bio.bi_status)) {
422                 fuse_reply_buf(req, buf, size);
423         } else {
424                 fuse_reply_err(req, -blk_status_to_errno(rbio.bio.bi_status));
425         }
426
427         free(buf);
428 }
429
430 static int write_set_inode(struct bch_fs *c, fuse_ino_t inum, off_t new_size)
431 {
432         struct btree_trans trans;
433         struct btree_iter *iter;
434         struct bch_inode_unpacked inode_u;
435         int ret = 0;
436         u64 now;
437
438         bch2_trans_init(&trans, c, 0, 0);
439 retry:
440         bch2_trans_begin(&trans);
441         now = bch2_current_time(c);
442
443         iter = bch2_inode_peek(&trans, &inode_u, inum, BTREE_ITER_INTENT);
444         ret = PTR_ERR_OR_ZERO(iter);
445         if (ret)
446                 goto err;
447
448         inode_u.bi_size = max_t(u64, inode_u.bi_size, new_size);
449         inode_u.bi_mtime = now;
450         inode_u.bi_ctime = now;
451
452         ret = bch2_inode_write(&trans, iter, &inode_u);
453         if (ret)
454                 goto err;
455
456         ret = bch2_trans_commit(&trans, NULL, NULL,
457                                 BTREE_INSERT_ATOMIC|BTREE_INSERT_NOFAIL);
458
459 err:
460         if (ret == -EINTR)
461                 goto retry;
462
463         bch2_trans_exit(&trans);
464         return ret;
465 }
466
467 static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t inum,
468                                 const char *buf, size_t size,
469                                 off_t offset,
470                                 struct fuse_file_info *fi)
471 {
472         struct bch_fs *c        = fuse_req_userdata(req);
473         struct bch_io_opts      io_opts;
474         struct bch_write_op     op;
475         struct bio_vec          bv;
476         struct closure          cl;
477
478         if ((size|offset) & (block_bytes(c) - 1)) {
479                 fuse_log(FUSE_LOG_DEBUG,
480                          "bcachefs_fuse_write: unaligned io not supported.\n");
481                 fuse_reply_err(req, EINVAL);
482                 return;
483         }
484
485         closure_init_stack(&cl);
486
487         if (get_inode_io_opts(c, inum, &io_opts)) {
488                 fuse_reply_err(req, ENOENT);
489                 return;
490         }
491
492         bch2_write_op_init(&op, c, io_opts);
493         op.write_point  = writepoint_hashed(0);
494         op.nr_replicas  = io_opts.data_replicas;
495         op.target       = io_opts.foreground_target;
496         op.pos          = POS(inum, offset >> 9);
497
498         userbio_init(&op.wbio.bio, &bv, (void *) buf, size);
499         bio_set_op_attrs(&op.wbio.bio, REQ_OP_WRITE, REQ_SYNC);
500
501         if (bch2_disk_reservation_get(c, &op.res, size >> 9,
502                                       op.nr_replicas, 0)) {
503                 /* XXX: use check_range_allocated like dio write path */
504                 fuse_reply_err(req, ENOSPC);
505                 return;
506         }
507
508         closure_call(&op.cl, bch2_write, NULL, &cl);
509         closure_sync(&cl);
510
511         /*
512          * Update inode data.
513          * TODO: could possibly do asynchronously.
514          * TODO: could also possibly do atomically with the extents.
515          */
516         if (!op.error)
517                 op.error = write_set_inode(c, inum, offset + size);
518
519         if (!op.error) {
520                 BUG_ON(op.written == 0);
521                 fuse_reply_write(req, (size_t) op.written << 9);
522         } else {
523                 BUG_ON(!op.error);
524                 fuse_reply_err(req, -op.error);
525         }
526 }
527
528 #if 0
529 /*
530  * FUSE flush is essentially the close() call, however it is not guaranteed
531  * that one flush happens per open/create.
532  *
533  * It doesn't have to do anything, and is mostly relevant for NFS-style
534  * filesystems where close has some relationship to caching.
535  */
536 static void bcachefs_fuse_flush(fuse_req_t req, fuse_ino_t inum,
537                                 struct fuse_file_info *fi)
538 {
539         struct bch_fs *c = fuse_req_userdata(req);
540 }
541
542 static void bcachefs_fuse_release(fuse_req_t req, fuse_ino_t inum,
543                                   struct fuse_file_info *fi)
544 {
545         struct bch_fs *c = fuse_req_userdata(req);
546 }
547
548 static void bcachefs_fuse_fsync(fuse_req_t req, fuse_ino_t inum, int datasync,
549                                 struct fuse_file_info *fi)
550 {
551         struct bch_fs *c = fuse_req_userdata(req);
552 }
553
554 static void bcachefs_fuse_opendir(fuse_req_t req, fuse_ino_t inum,
555                                   struct fuse_file_info *fi)
556 {
557         struct bch_fs *c = fuse_req_userdata(req);
558 }
559 #endif
560
561 struct fuse_dir_entry {
562         u64             ino;
563         unsigned        type;
564         char            name[0];
565 };
566
567 struct fuse_dir_context {
568         struct dir_context      ctx;
569         fuse_req_t              req;
570         char                    *buf;
571         size_t                  bufsize;
572
573         struct fuse_dir_entry   *prev;
574 };
575
576 static int fuse_send_dir_entry(struct fuse_dir_context *ctx, loff_t pos)
577 {
578         struct fuse_dir_entry *de = ctx->prev;
579         ctx->prev = NULL;
580
581         struct stat statbuf = {
582                 .st_ino         = unmap_root_ino(de->ino),
583                 .st_mode        = de->type << 12,
584         };
585
586         size_t len = fuse_add_direntry(ctx->req, ctx->buf, ctx->bufsize,
587                                        de->name, &statbuf, pos);
588
589         free(de);
590
591         if (len > ctx->bufsize)
592                 return -EINVAL;
593
594         ctx->buf        += len;
595         ctx->bufsize    -= len;
596
597         return 0;
598 }
599
600 static int fuse_filldir(struct dir_context *_ctx,
601                         const char *name, int namelen,
602                         loff_t pos, u64 ino, unsigned type)
603 {
604         struct fuse_dir_context *ctx =
605                 container_of(_ctx, struct fuse_dir_context, ctx);
606
607         fuse_log(FUSE_LOG_DEBUG, "fuse_filldir(ctx={.ctx={.pos=%llu}}, "
608                  "name=%s, namelen=%d, pos=%lld, dir=%llu, type=%u)\n",
609                  ctx->ctx.pos, name, namelen, pos, ino, type);
610
611         /*
612          * We have to emit directory entries after reading the next entry,
613          * because the previous entry contains a pointer to next.
614          */
615         if (ctx->prev) {
616                 int ret = fuse_send_dir_entry(ctx, pos);
617                 if (ret)
618                         return ret;
619         }
620
621         struct fuse_dir_entry *cur = malloc(sizeof *cur + namelen + 1);
622         cur->ino = ino;
623         cur->type = type;
624         memcpy(cur->name, name, namelen);
625         cur->name[namelen] = 0;
626
627         ctx->prev = cur;
628
629         return 0;
630 }
631
632 static bool handle_dots(struct fuse_dir_context *ctx, fuse_ino_t dir)
633 {
634         int ret = 0;
635
636         if (ctx->ctx.pos == 0) {
637                 ret = fuse_filldir(&ctx->ctx, ".", 1, ctx->ctx.pos,
638                                    unmap_root_ino(dir), DT_DIR);
639                 if (ret < 0)
640                         return false;
641                 ctx->ctx.pos = 1;
642         }
643
644         if (ctx->ctx.pos == 1) {
645                 ret = fuse_filldir(&ctx->ctx, "..", 2, ctx->ctx.pos,
646                                    /*TODO: parent*/ 1, DT_DIR);
647                 if (ret < 0)
648                         return false;
649                 ctx->ctx.pos = 2;
650         }
651
652         return true;
653 }
654
655 static void bcachefs_fuse_readdir(fuse_req_t req, fuse_ino_t dir,
656                                   size_t size, off_t off,
657                                   struct fuse_file_info *fi)
658 {
659         struct bch_fs *c = fuse_req_userdata(req);
660         struct bch_inode_unpacked bi;
661         char *buf = calloc(size, 1);
662         struct fuse_dir_context ctx = {
663                 .ctx.actor      = fuse_filldir,
664                 .ctx.pos        = off,
665                 .req            = req,
666                 .buf            = buf,
667                 .bufsize        = size,
668         };
669         int ret = 0;
670
671         fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir(dir=%llu, size=%zu, "
672                  "off=%lld)\n", dir, size, off);
673
674         dir = map_root_ino(dir);
675
676         ret = bch2_inode_find_by_inum(c, dir, &bi);
677         if (ret)
678                 goto reply;
679
680         if (!S_ISDIR(bi.bi_mode)) {
681                 ret = -ENOTDIR;
682                 goto reply;
683         }
684
685         if (!handle_dots(&ctx, dir))
686                 goto reply;
687
688         ret = bch2_readdir(c, dir, &ctx.ctx);
689
690 reply:
691         /*
692          * If we have something to send, the error above doesn't matter.
693          *
694          * Alternatively, if this send fails, but we previously sent something,
695          * then this is a success.
696          */
697         if (ctx.prev) {
698                 ret = fuse_send_dir_entry(&ctx, ctx.ctx.pos);
699                 if (ret && ctx.buf != buf)
700                         ret = 0;
701         }
702
703         if (!ret) {
704                 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir reply %zd\n",
705                                         ctx.buf - buf);
706                 fuse_reply_buf(req, buf, ctx.buf - buf);
707         } else {
708                 fuse_reply_err(req, -ret);
709         }
710
711         free(buf);
712 }
713
714 #if 0
715 static void bcachefs_fuse_readdirplus(fuse_req_t req, fuse_ino_t dir,
716                                       size_t size, off_t off,
717                                       struct fuse_file_info *fi)
718 {
719
720 }
721
722 static void bcachefs_fuse_releasedir(fuse_req_t req, fuse_ino_t inum,
723                                      struct fuse_file_info *fi)
724 {
725         struct bch_fs *c = fuse_req_userdata(req);
726 }
727
728 static void bcachefs_fuse_fsyncdir(fuse_req_t req, fuse_ino_t inum, int datasync,
729                                    struct fuse_file_info *fi)
730 {
731         struct bch_fs *c = fuse_req_userdata(req);
732 }
733 #endif
734
735 static void bcachefs_fuse_statfs(fuse_req_t req, fuse_ino_t inum)
736 {
737         struct bch_fs *c = fuse_req_userdata(req);
738         struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
739         unsigned shift = c->block_bits;
740         struct statvfs statbuf = {
741                 .f_bsize        = block_bytes(c),
742                 .f_frsize       = block_bytes(c),
743                 .f_blocks       = usage.capacity >> shift,
744                 .f_bfree        = (usage.capacity - usage.used) >> shift,
745                 //.f_bavail     = statbuf.f_bfree,
746                 .f_files        = usage.nr_inodes,
747                 .f_ffree        = U64_MAX,
748                 .f_namemax      = BCH_NAME_MAX,
749         };
750
751         fuse_reply_statfs(req, &statbuf);
752 }
753
754 #if 0
755 static void bcachefs_fuse_setxattr(fuse_req_t req, fuse_ino_t inum,
756                                    const char *name, const char *value,
757                                    size_t size, int flags)
758 {
759         struct bch_fs *c = fuse_req_userdata(req);
760 }
761
762 static void bcachefs_fuse_getxattr(fuse_req_t req, fuse_ino_t inum,
763                                    const char *name, size_t size)
764 {
765         struct bch_fs *c = fuse_req_userdata(req);
766
767         fuse_reply_xattr(req, );
768 }
769
770 static void bcachefs_fuse_listxattr(fuse_req_t req, fuse_ino_t inum, size_t size)
771 {
772         struct bch_fs *c = fuse_req_userdata(req);
773 }
774
775 static void bcachefs_fuse_removexattr(fuse_req_t req, fuse_ino_t inum,
776                                       const char *name)
777 {
778         struct bch_fs *c = fuse_req_userdata(req);
779 }
780 #endif
781
782 static void bcachefs_fuse_create(fuse_req_t req, fuse_ino_t dir,
783                                  const char *name, mode_t mode,
784                                  struct fuse_file_info *fi)
785 {
786         struct bch_fs *c = fuse_req_userdata(req);
787         struct bch_inode_unpacked new_inode;
788         int ret;
789
790         ret = do_create(c, dir, name, mode, 0, &new_inode);
791         if (ret)
792                 goto err;
793
794         struct fuse_entry_param e = inode_to_entry(c, &new_inode);
795         fuse_reply_create(req, &e, fi);
796         return;
797 err:
798         fuse_reply_err(req, -ret);
799
800 }
801
802 #if 0
803 static void bcachefs_fuse_write_buf(fuse_req_t req, fuse_ino_t inum,
804                                     struct fuse_bufvec *bufv, off_t off,
805                                     struct fuse_file_info *fi)
806 {
807         struct bch_fs *c = fuse_req_userdata(req);
808 }
809
810 static void bcachefs_fuse_fallocate(fuse_req_t req, fuse_ino_t inum, int mode,
811                                     off_t offset, off_t length,
812                                     struct fuse_file_info *fi)
813 {
814         struct bch_fs *c = fuse_req_userdata(req);
815 }
816 #endif
817
818 static const struct fuse_lowlevel_ops bcachefs_fuse_ops = {
819         .init           = bcachefs_fuse_init,
820         .destroy        = bcachefs_fuse_destroy,
821         .lookup         = bcachefs_fuse_lookup,
822         .getattr        = bcachefs_fuse_getattr,
823         .setattr        = bcachefs_fuse_setattr,
824         .readlink       = bcachefs_fuse_readlink,
825         .mknod          = bcachefs_fuse_mknod,
826         .mkdir          = bcachefs_fuse_mkdir,
827         .unlink         = bcachefs_fuse_unlink,
828         .rmdir          = bcachefs_fuse_rmdir,
829         //.symlink      = bcachefs_fuse_symlink,
830         .rename         = bcachefs_fuse_rename,
831         .link           = bcachefs_fuse_link,
832         .open           = bcachefs_fuse_open,
833         .read           = bcachefs_fuse_read,
834         .write          = bcachefs_fuse_write,
835         //.flush        = bcachefs_fuse_flush,
836         //.release      = bcachefs_fuse_release,
837         //.fsync        = bcachefs_fuse_fsync,
838         //.opendir      = bcachefs_fuse_opendir,
839         .readdir        = bcachefs_fuse_readdir,
840         //.readdirplus  = bcachefs_fuse_readdirplus,
841         //.releasedir   = bcachefs_fuse_releasedir,
842         //.fsyncdir     = bcachefs_fuse_fsyncdir,
843         .statfs         = bcachefs_fuse_statfs,
844         //.setxattr     = bcachefs_fuse_setxattr,
845         //.getxattr     = bcachefs_fuse_getxattr,
846         //.listxattr    = bcachefs_fuse_listxattr,
847         //.removexattr  = bcachefs_fuse_removexattr,
848         .create         = bcachefs_fuse_create,
849
850         /* posix locks: */
851 #if 0
852         .getlk          = bcachefs_fuse_getlk,
853         .setlk          = bcachefs_fuse_setlk,
854 #endif
855         //.write_buf    = bcachefs_fuse_write_buf,
856         //.fallocate    = bcachefs_fuse_fallocate,
857
858 };
859
860 /*
861  * Setup and command parsing.
862  */
863
864 struct bf_context {
865         char            *devices_str;
866         char            **devices;
867         int             nr_devices;
868 };
869
870 static void bf_context_free(struct bf_context *ctx)
871 {
872         int i;
873
874         free(ctx->devices_str);
875         for (i = 0; i < ctx->nr_devices; ++i)
876                 free(ctx->devices[i]);
877         free(ctx->devices);
878 }
879
880 static struct fuse_opt bf_opts[] = {
881         FUSE_OPT_END
882 };
883
884 /*
885  * Fuse option parsing helper -- returning 0 means we consumed the argument, 1
886  * means we did not.
887  */
888 static int bf_opt_proc(void *data, const char *arg, int key,
889     struct fuse_args *outargs)
890 {
891         struct bf_context *ctx = data;
892
893         switch (key) {
894         case FUSE_OPT_KEY_NONOPT:
895                 /* Just extract the first non-option string. */
896                 if (!ctx->devices_str) {
897                         ctx->devices_str = strdup(arg);
898                         return 0;
899                 }
900                 return 1;
901         }
902
903         return 1;
904 }
905
906 /*
907  * dev1:dev2 -> [ dev1, dev2 ]
908  * dev       -> [ dev ]
909  */
910 static void tokenize_devices(struct bf_context *ctx)
911 {
912         char *devices_str = strdup(ctx->devices_str);
913         char *devices_tmp = devices_str;
914         char **devices = NULL;
915         int nr = 0;
916         char *dev = NULL;
917
918         while ((dev = strsep(&devices_tmp, ":"))) {
919                 if (strlen(dev) > 0) {
920                         devices = realloc(devices, (nr + 1) * sizeof *devices);
921                         devices[nr] = strdup(dev);
922                         nr++;
923                 }
924         }
925
926         if (!devices) {
927                 devices = malloc(sizeof *devices);
928                 devices[0] = strdup(ctx->devices_str);
929                 nr = 1;
930         }
931
932         ctx->devices = devices;
933         ctx->nr_devices = nr;
934
935         free(devices_str);
936 }
937
938 static void usage(char *argv[])
939 {
940         printf("Usage: %s fusemount [options] <dev>[:dev2:...] <mountpoint>\n",
941                argv[0]);
942         printf("\n");
943 }
944
945 int cmd_fusemount(int argc, char *argv[])
946 {
947         struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
948         struct bch_opts bch_opts = bch2_opts_empty();
949         struct bf_context ctx = { 0 };
950         struct bch_fs *c = NULL;
951         int ret = 0, i;
952
953         /* Parse arguments. */
954         if (fuse_opt_parse(&args, &ctx, bf_opts, bf_opt_proc) < 0)
955                 die("fuse_opt_parse err: %m");
956
957         struct fuse_cmdline_opts fuse_opts;
958         if (fuse_parse_cmdline(&args, &fuse_opts) < 0)
959                 die("fuse_parse_cmdline err: %m");
960
961         if (fuse_opts.show_help) {
962                 usage(argv);
963                 fuse_cmdline_help();
964                 fuse_lowlevel_help();
965                 ret = 0;
966                 goto out;
967         }
968         if (fuse_opts.show_version) {
969                 /* TODO: Show bcachefs version. */
970                 printf("FUSE library version %s\n", fuse_pkgversion());
971                 fuse_lowlevel_version();
972                 ret = 0;
973                 goto out;
974         }
975         if (!fuse_opts.mountpoint) {
976                 usage(argv);
977                 printf("Please supply a mountpoint.\n");
978                 ret = 1;
979                 goto out;
980         }
981         if (!ctx.devices_str) {
982                 usage(argv);
983                 printf("Please specify a device or device1:device2:...\n");
984                 ret = 1;
985                 goto out;
986         }
987         tokenize_devices(&ctx);
988
989         /* Open bch */
990         printf("Opening bcachefs filesystem on:\n");
991         for (i = 0; i < ctx.nr_devices; ++i)
992                 printf("\t%s\n", ctx.devices[i]);
993
994         c = bch2_fs_open(ctx.devices, ctx.nr_devices, bch_opts);
995         if (IS_ERR(c))
996                 die("error opening %s: %s", ctx.devices_str,
997                     strerror(-PTR_ERR(c)));
998
999         /* Fuse */
1000         struct fuse_session *se =
1001                 fuse_session_new(&args, &bcachefs_fuse_ops,
1002                                  sizeof(bcachefs_fuse_ops), c);
1003         if (!se)
1004                 die("fuse_lowlevel_new err: %m");
1005
1006         if (fuse_set_signal_handlers(se) < 0)
1007                 die("fuse_set_signal_handlers err: %m");
1008
1009         if (fuse_session_mount(se, fuse_opts.mountpoint))
1010                 die("fuse_mount err: %m");
1011
1012         fuse_daemonize(fuse_opts.foreground);
1013
1014         ret = fuse_session_loop(se);
1015
1016         /* Cleanup */
1017         fuse_session_unmount(se);
1018         fuse_remove_signal_handlers(se);
1019         fuse_session_destroy(se);
1020
1021 out:
1022         free(fuse_opts.mountpoint);
1023         fuse_opt_free_args(&args);
1024         bf_context_free(&ctx);
1025
1026         return ret ? 1 : 0;
1027 }