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