5 #include <sys/statvfs.h>
7 #include <fuse_lowlevel.h>
10 #include "libbcachefs.h"
11 #include "tools-util.h"
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"
26 #include "libbcachefs/fs.h"
28 #include <linux/dcache.h>
30 /* XXX cut and pasted from fsck.c */
31 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
33 static inline u64 map_root_ino(u64 ino)
35 return ino == 1 ? 4096 : ino;
38 static inline u64 unmap_root_ino(u64 ino)
40 return ino == 4096 ? 1 : ino;
43 static struct stat inode_to_stat(struct bch_fs *c,
44 struct bch_inode_unpacked *bi)
46 return (struct stat) {
47 .st_ino = bi->bi_inum,
48 .st_size = bi->bi_size,
49 .st_mode = bi->bi_mode,
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),
62 static struct fuse_entry_param inode_to_entry(struct bch_fs *c,
63 struct bch_inode_unpacked *bi)
65 return (struct fuse_entry_param) {
67 .generation = bi->bi_generation,
68 .attr = inode_to_stat(c, bi),
69 .attr_timeout = DBL_MAX,
70 .entry_timeout = DBL_MAX,
74 static void bcachefs_fuse_init(void *arg, struct fuse_conn_info *conn)
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;
80 fuse_log(FUSE_LOG_DEBUG, "fuse_init: writeback not capable\n");
82 //conn->want |= FUSE_CAP_POSIX_ACL;
85 static void bcachefs_fuse_destroy(void *arg)
87 struct bch_fs *c = arg;
92 static void bcachefs_fuse_lookup(fuse_req_t req, fuse_ino_t dir,
95 struct bch_fs *c = fuse_req_userdata(req);
96 struct bch_inode_unpacked bi;
97 struct qstr qstr = QSTR(name);
101 dir = map_root_ino(dir);
103 ret = bch2_inode_find_by_inum(c, dir, &bi);
105 fuse_reply_err(req, -ret);
109 struct bch_hash_info hash_info = bch2_hash_info_init(c, &bi);
111 inum = bch2_dirent_lookup(c, dir, &hash_info, &qstr);
117 ret = bch2_inode_find_by_inum(c, inum, &bi);
121 bi.bi_inum = unmap_root_ino(bi.bi_inum);
123 struct fuse_entry_param e = inode_to_entry(c, &bi);
124 fuse_reply_entry(req, &e);
127 fuse_reply_err(req, -ret);
130 static void bcachefs_fuse_getattr(fuse_req_t req, fuse_ino_t inum,
131 struct fuse_file_info *fi)
133 struct bch_fs *c = fuse_req_userdata(req);
134 struct bch_inode_unpacked bi;
138 inum = map_root_ino(inum);
140 ret = bch2_inode_find_by_inum(c, inum, &bi);
142 fuse_reply_err(req, -ret);
146 bi.bi_inum = unmap_root_ino(bi.bi_inum);
148 attr = inode_to_stat(c, &bi);
149 fuse_reply_attr(req, &attr, DBL_MAX);
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)
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;
163 inum = map_root_ino(inum);
165 bch2_trans_init(&trans, c, 0, 0);
167 bch2_trans_begin(&trans);
168 now = bch2_current_time(c);
170 iter = bch2_inode_peek(&trans, &inode_u, inum, BTREE_ITER_INTENT);
171 ret = PTR_ERR_OR_ZERO(iter);
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;
193 ret = bch2_inode_write(&trans, iter, &inode_u) ?:
194 bch2_trans_commit(&trans, NULL, NULL,
196 BTREE_INSERT_NOFAIL);
201 bch2_trans_exit(&trans);
204 *attr = inode_to_stat(c, &inode_u);
205 fuse_reply_attr(req, attr, DBL_MAX);
207 fuse_reply_err(req, -ret);
211 static void bcachefs_fuse_readlink(fuse_req_t req, fuse_ino_t inum)
213 //struct bch_fs *c = fuse_req_userdata(req);
215 //char *link = malloc();
217 //fuse_reply_readlink(req, link);
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)
224 struct qstr qstr = QSTR(name);
225 struct bch_inode_unpacked dir_u;
227 dir = map_root_ino(dir);
229 bch2_inode_init_early(c, new_inode);
231 return bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
232 bch2_create_trans(&trans,
235 0, 0, mode, rdev, NULL, NULL));
238 static void bcachefs_fuse_mknod(fuse_req_t req, fuse_ino_t dir,
239 const char *name, mode_t mode,
242 struct bch_fs *c = fuse_req_userdata(req);
243 struct bch_inode_unpacked new_inode;
246 ret = do_create(c, dir, name, mode, rdev, &new_inode);
250 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
251 fuse_reply_entry(req, &e);
254 fuse_reply_err(req, -ret);
257 static void bcachefs_fuse_mkdir(fuse_req_t req, fuse_ino_t dir,
258 const char *name, mode_t mode)
260 bcachefs_fuse_mknod(req, dir, name, mode, 0);
263 static void bcachefs_fuse_unlink(fuse_req_t req, fuse_ino_t dir,
266 struct bch_fs *c = fuse_req_userdata(req);
267 struct bch_inode_unpacked dir_u, inode_u;
268 struct qstr qstr = QSTR(name);
271 dir = map_root_ino(dir);
273 ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC|BTREE_INSERT_NOFAIL,
274 bch2_unlink_trans(&trans, dir, &dir_u,
277 fuse_reply_err(req, -ret);
280 static void bcachefs_fuse_rmdir(fuse_req_t req, fuse_ino_t dir,
283 dir = map_root_ino(dir);
285 bcachefs_fuse_unlink(req, dir, name);
289 static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
290 fuse_ino_t parent, const char *name)
292 struct bch_fs *c = fuse_req_userdata(req);
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,
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);
308 src_dir = map_root_ino(src_dir);
309 dst_dir = map_root_ino(dst_dir);
311 /* XXX handle overwrites */
312 ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
313 bch2_rename_trans(&trans,
316 &src_inode_u, &dst_inode_u,
317 &src_name, &dst_name,
320 fuse_reply_err(req, -ret);
323 static void bcachefs_fuse_link(fuse_req_t req, fuse_ino_t inum,
324 fuse_ino_t newparent, const char *newname)
326 struct bch_fs *c = fuse_req_userdata(req);
327 struct bch_inode_unpacked inode_u;
328 struct qstr qstr = QSTR(newname);
331 ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
332 bch2_link_trans(&trans, newparent,
333 inum, &inode_u, &qstr));
336 struct fuse_entry_param e = inode_to_entry(c, &inode_u);
337 fuse_reply_entry(req, &e);
339 fuse_reply_err(req, -ret);
343 static void bcachefs_fuse_open(fuse_req_t req, fuse_ino_t inum,
344 struct fuse_file_info *fi)
346 fi->direct_io = false;
347 fi->keep_cache = true;
348 fi->cache_readdir = true;
350 fuse_reply_open(req, fi);
353 static void userbio_init(struct bio *bio, struct bio_vec *bv,
354 void *buf, size_t size)
356 bio_init(bio, bv, 1);
357 bio->bi_iter.bi_size = size;
363 static int get_inode_io_opts(struct bch_fs *c, u64 inum,
364 struct bch_io_opts *opts)
366 struct bch_inode_unpacked inode;
367 if (bch2_inode_find_by_inum(c, inum, &inode))
370 *opts = bch2_opts_to_inode_opts(c->opts);
371 bch2_io_opts_apply(opts, bch2_inode_opts_get(&inode));
375 static void bcachefs_fuse_read_endio(struct bio *bio)
377 closure_put(bio->bi_private);
380 struct fuse_align_io {
388 /* Handle unaligned start and end */
389 /* TODO: align to block_bytes, sector size, or page size? */
390 static void align_io(struct fuse_align_io *align, const struct bch_fs *c,
391 size_t size, off_t offset)
395 align->start = round_down(offset, block_bytes(c));
396 align->pad_start = offset - align->start;
398 off_t end = offset + size;
399 align->end = round_up(end, block_bytes(c));
400 align->pad_end = align->end - end;
402 align->size = align->end - align->start;
406 * Given an aligned number of bytes transferred, figure out how many unaligned
407 * bytes were transferred.
409 static size_t align_fix_up_bytes(const struct fuse_align_io *align,
414 if (align_bytes > align->pad_start) {
415 bytes = align_bytes - align->pad_start;
416 bytes = bytes > align->pad_end ? bytes - align->pad_end : 0;
425 static int read_aligned(struct bch_fs *c, fuse_ino_t inum, size_t aligned_size,
426 off_t aligned_offset, void *buf)
428 BUG_ON(aligned_size & (block_bytes(c) - 1));
429 BUG_ON(aligned_offset & (block_bytes(c) - 1));
431 struct bch_io_opts io_opts;
432 if (get_inode_io_opts(c, inum, &io_opts))
435 struct bch_read_bio rbio;
437 userbio_init(&rbio.bio, &bv, buf, aligned_size);
438 bio_set_op_attrs(&rbio.bio, REQ_OP_READ, REQ_SYNC);
439 rbio.bio.bi_iter.bi_sector = aligned_offset >> 9;
442 closure_init_stack(&cl);
445 rbio.bio.bi_end_io = bcachefs_fuse_read_endio;
446 rbio.bio.bi_private = &cl;
448 bch2_read(c, rbio_init(&rbio.bio, io_opts), inum);
452 return -blk_status_to_errno(rbio.bio.bi_status);
455 static void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t inum,
456 size_t size, off_t offset,
457 struct fuse_file_info *fi)
459 struct bch_fs *c = fuse_req_userdata(req);
460 struct fuse_align_io align;
462 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_read(%llu, %zd, %lld)\n",
465 /* Check inode size. */
466 struct bch_inode_unpacked bi;
467 int ret = bch2_inode_find_by_inum(c, inum, &bi);
469 fuse_reply_err(req, -ret);
473 off_t end = min_t(u64, bi.bi_size, offset + size);
475 fuse_reply_buf(req, NULL, 0);
480 align_io(&align, c, size, offset);
482 void *buf = aligned_alloc(PAGE_SIZE, align.size);
484 fuse_reply_err(req, ENOMEM);
488 ret = read_aligned(c, inum, align.size, align.start, buf);
491 fuse_reply_buf(req, buf + align.pad_start, size);
493 fuse_reply_err(req, -ret);
498 static int write_set_inode(struct bch_fs *c, fuse_ino_t inum, off_t new_size)
500 struct btree_trans trans;
501 struct btree_iter *iter;
502 struct bch_inode_unpacked inode_u;
506 bch2_trans_init(&trans, c, 0, 0);
508 bch2_trans_begin(&trans);
509 now = bch2_current_time(c);
511 iter = bch2_inode_peek(&trans, &inode_u, inum, BTREE_ITER_INTENT);
512 ret = PTR_ERR_OR_ZERO(iter);
516 inode_u.bi_size = max_t(u64, inode_u.bi_size, new_size);
517 inode_u.bi_mtime = now;
518 inode_u.bi_ctime = now;
520 ret = bch2_inode_write(&trans, iter, &inode_u);
524 ret = bch2_trans_commit(&trans, NULL, NULL,
525 BTREE_INSERT_ATOMIC|BTREE_INSERT_NOFAIL);
531 bch2_trans_exit(&trans);
535 static int write_aligned(struct bch_fs *c, fuse_ino_t inum,
536 struct bch_io_opts io_opts, void *buf,
537 size_t aligned_size, off_t aligned_offset,
540 struct bch_write_op op = { 0 };
544 BUG_ON(aligned_size & (block_bytes(c) - 1));
545 BUG_ON(aligned_offset & (block_bytes(c) - 1));
549 closure_init_stack(&cl);
551 bch2_write_op_init(&op, c, io_opts); /* XXX reads from op?! */
552 op.write_point = writepoint_hashed(0);
553 op.nr_replicas = io_opts.data_replicas;
554 op.target = io_opts.foreground_target;
555 op.pos = POS(inum, aligned_offset >> 9);
557 userbio_init(&op.wbio.bio, &bv, buf, aligned_size);
558 bio_set_op_attrs(&op.wbio.bio, REQ_OP_WRITE, REQ_SYNC);
560 if (bch2_disk_reservation_get(c, &op.res, aligned_size >> 9,
561 op.nr_replicas, 0)) {
562 /* XXX: use check_range_allocated like dio write path */
566 closure_call(&op.cl, bch2_write, NULL, &cl);
570 *written_out = op.written << 9;
575 static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t inum,
576 const char *buf, size_t size,
578 struct fuse_file_info *fi)
580 struct bch_fs *c = fuse_req_userdata(req);
581 struct bch_io_opts io_opts;
582 struct fuse_align_io align;
583 size_t aligned_written;
586 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write(%llu, %zd, %lld)\n",
589 align_io(&align, c, size, offset);
591 if (get_inode_io_opts(c, inum, &io_opts)) {
596 /* Realign the data and read in start and end, if needed */
597 void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
599 /* Read partial start data. */
600 if (align.pad_start) {
601 memset(aligned_buf, 0, block_bytes(c));
603 ret = read_aligned(c, inum, block_bytes(c), align.start,
610 * Read partial end data. If the whole write fits in one block, the
611 * start data and the end data are the same so this isn't needed.
614 !(align.pad_start && align.size == block_bytes(c))) {
615 off_t partial_end_start = align.end - block_bytes(c);
616 size_t buf_offset = align.size - block_bytes(c);
618 memset(aligned_buf + buf_offset, 0, block_bytes(c));
620 ret = read_aligned(c, inum, block_bytes(c), partial_end_start,
621 aligned_buf + buf_offset);
626 /* Overlay what we want to write. */
627 memcpy(aligned_buf + align.pad_start, buf, size);
629 /* Actually write. */
630 ret = write_aligned(c, inum, io_opts, aligned_buf,
631 align.size, align.start, &aligned_written);
633 /* Figure out how many unaligned bytes were written. */
634 size_t written = align_fix_up_bytes(&align, aligned_written);
635 BUG_ON(written > size);
637 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write: wrote %zd bytes\n",
645 * TODO: Integrate with bch2_extent_update()
648 ret = write_set_inode(c, inum, offset + written);
651 BUG_ON(written == 0);
652 fuse_reply_write(req, written);
657 fuse_reply_err(req, -ret);
662 * FUSE flush is essentially the close() call, however it is not guaranteed
663 * that one flush happens per open/create.
665 * It doesn't have to do anything, and is mostly relevant for NFS-style
666 * filesystems where close has some relationship to caching.
668 static void bcachefs_fuse_flush(fuse_req_t req, fuse_ino_t inum,
669 struct fuse_file_info *fi)
671 struct bch_fs *c = fuse_req_userdata(req);
674 static void bcachefs_fuse_release(fuse_req_t req, fuse_ino_t inum,
675 struct fuse_file_info *fi)
677 struct bch_fs *c = fuse_req_userdata(req);
680 static void bcachefs_fuse_fsync(fuse_req_t req, fuse_ino_t inum, int datasync,
681 struct fuse_file_info *fi)
683 struct bch_fs *c = fuse_req_userdata(req);
686 static void bcachefs_fuse_opendir(fuse_req_t req, fuse_ino_t inum,
687 struct fuse_file_info *fi)
689 struct bch_fs *c = fuse_req_userdata(req);
693 struct fuse_dir_entry {
699 struct fuse_dir_context {
700 struct dir_context ctx;
705 struct fuse_dir_entry *prev;
708 static int fuse_send_dir_entry(struct fuse_dir_context *ctx, loff_t pos)
710 struct fuse_dir_entry *de = ctx->prev;
713 struct stat statbuf = {
714 .st_ino = unmap_root_ino(de->ino),
715 .st_mode = de->type << 12,
718 size_t len = fuse_add_direntry(ctx->req, ctx->buf, ctx->bufsize,
719 de->name, &statbuf, pos);
723 if (len > ctx->bufsize)
732 static int fuse_filldir(struct dir_context *_ctx,
733 const char *name, int namelen,
734 loff_t pos, u64 ino, unsigned type)
736 struct fuse_dir_context *ctx =
737 container_of(_ctx, struct fuse_dir_context, ctx);
739 fuse_log(FUSE_LOG_DEBUG, "fuse_filldir(ctx={.ctx={.pos=%llu}}, "
740 "name=%s, namelen=%d, pos=%lld, dir=%llu, type=%u)\n",
741 ctx->ctx.pos, name, namelen, pos, ino, type);
744 * We have to emit directory entries after reading the next entry,
745 * because the previous entry contains a pointer to next.
748 int ret = fuse_send_dir_entry(ctx, pos);
753 struct fuse_dir_entry *cur = malloc(sizeof *cur + namelen + 1);
756 memcpy(cur->name, name, namelen);
757 cur->name[namelen] = 0;
764 static bool handle_dots(struct fuse_dir_context *ctx, fuse_ino_t dir)
768 if (ctx->ctx.pos == 0) {
769 ret = fuse_filldir(&ctx->ctx, ".", 1, ctx->ctx.pos,
770 unmap_root_ino(dir), DT_DIR);
776 if (ctx->ctx.pos == 1) {
777 ret = fuse_filldir(&ctx->ctx, "..", 2, ctx->ctx.pos,
778 /*TODO: parent*/ 1, DT_DIR);
787 static void bcachefs_fuse_readdir(fuse_req_t req, fuse_ino_t dir,
788 size_t size, off_t off,
789 struct fuse_file_info *fi)
791 struct bch_fs *c = fuse_req_userdata(req);
792 struct bch_inode_unpacked bi;
793 char *buf = calloc(size, 1);
794 struct fuse_dir_context ctx = {
795 .ctx.actor = fuse_filldir,
803 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir(dir=%llu, size=%zu, "
804 "off=%lld)\n", dir, size, off);
806 dir = map_root_ino(dir);
808 ret = bch2_inode_find_by_inum(c, dir, &bi);
812 if (!S_ISDIR(bi.bi_mode)) {
817 if (!handle_dots(&ctx, dir))
820 ret = bch2_readdir(c, dir, &ctx.ctx);
824 * If we have something to send, the error above doesn't matter.
826 * Alternatively, if this send fails, but we previously sent something,
827 * then this is a success.
830 ret = fuse_send_dir_entry(&ctx, ctx.ctx.pos);
831 if (ret && ctx.buf != buf)
836 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir reply %zd\n",
838 fuse_reply_buf(req, buf, ctx.buf - buf);
840 fuse_reply_err(req, -ret);
847 static void bcachefs_fuse_readdirplus(fuse_req_t req, fuse_ino_t dir,
848 size_t size, off_t off,
849 struct fuse_file_info *fi)
854 static void bcachefs_fuse_releasedir(fuse_req_t req, fuse_ino_t inum,
855 struct fuse_file_info *fi)
857 struct bch_fs *c = fuse_req_userdata(req);
860 static void bcachefs_fuse_fsyncdir(fuse_req_t req, fuse_ino_t inum, int datasync,
861 struct fuse_file_info *fi)
863 struct bch_fs *c = fuse_req_userdata(req);
867 static void bcachefs_fuse_statfs(fuse_req_t req, fuse_ino_t inum)
869 struct bch_fs *c = fuse_req_userdata(req);
870 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
871 unsigned shift = c->block_bits;
872 struct statvfs statbuf = {
873 .f_bsize = block_bytes(c),
874 .f_frsize = block_bytes(c),
875 .f_blocks = usage.capacity >> shift,
876 .f_bfree = (usage.capacity - usage.used) >> shift,
877 //.f_bavail = statbuf.f_bfree,
878 .f_files = usage.nr_inodes,
880 .f_namemax = BCH_NAME_MAX,
883 fuse_reply_statfs(req, &statbuf);
887 static void bcachefs_fuse_setxattr(fuse_req_t req, fuse_ino_t inum,
888 const char *name, const char *value,
889 size_t size, int flags)
891 struct bch_fs *c = fuse_req_userdata(req);
894 static void bcachefs_fuse_getxattr(fuse_req_t req, fuse_ino_t inum,
895 const char *name, size_t size)
897 struct bch_fs *c = fuse_req_userdata(req);
899 fuse_reply_xattr(req, );
902 static void bcachefs_fuse_listxattr(fuse_req_t req, fuse_ino_t inum, size_t size)
904 struct bch_fs *c = fuse_req_userdata(req);
907 static void bcachefs_fuse_removexattr(fuse_req_t req, fuse_ino_t inum,
910 struct bch_fs *c = fuse_req_userdata(req);
914 static void bcachefs_fuse_create(fuse_req_t req, fuse_ino_t dir,
915 const char *name, mode_t mode,
916 struct fuse_file_info *fi)
918 struct bch_fs *c = fuse_req_userdata(req);
919 struct bch_inode_unpacked new_inode;
922 ret = do_create(c, dir, name, mode, 0, &new_inode);
926 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
927 fuse_reply_create(req, &e, fi);
930 fuse_reply_err(req, -ret);
935 static void bcachefs_fuse_write_buf(fuse_req_t req, fuse_ino_t inum,
936 struct fuse_bufvec *bufv, off_t off,
937 struct fuse_file_info *fi)
939 struct bch_fs *c = fuse_req_userdata(req);
942 static void bcachefs_fuse_fallocate(fuse_req_t req, fuse_ino_t inum, int mode,
943 off_t offset, off_t length,
944 struct fuse_file_info *fi)
946 struct bch_fs *c = fuse_req_userdata(req);
950 static const struct fuse_lowlevel_ops bcachefs_fuse_ops = {
951 .init = bcachefs_fuse_init,
952 .destroy = bcachefs_fuse_destroy,
953 .lookup = bcachefs_fuse_lookup,
954 .getattr = bcachefs_fuse_getattr,
955 .setattr = bcachefs_fuse_setattr,
956 .readlink = bcachefs_fuse_readlink,
957 .mknod = bcachefs_fuse_mknod,
958 .mkdir = bcachefs_fuse_mkdir,
959 .unlink = bcachefs_fuse_unlink,
960 .rmdir = bcachefs_fuse_rmdir,
961 //.symlink = bcachefs_fuse_symlink,
962 .rename = bcachefs_fuse_rename,
963 .link = bcachefs_fuse_link,
964 .open = bcachefs_fuse_open,
965 .read = bcachefs_fuse_read,
966 .write = bcachefs_fuse_write,
967 //.flush = bcachefs_fuse_flush,
968 //.release = bcachefs_fuse_release,
969 //.fsync = bcachefs_fuse_fsync,
970 //.opendir = bcachefs_fuse_opendir,
971 .readdir = bcachefs_fuse_readdir,
972 //.readdirplus = bcachefs_fuse_readdirplus,
973 //.releasedir = bcachefs_fuse_releasedir,
974 //.fsyncdir = bcachefs_fuse_fsyncdir,
975 .statfs = bcachefs_fuse_statfs,
976 //.setxattr = bcachefs_fuse_setxattr,
977 //.getxattr = bcachefs_fuse_getxattr,
978 //.listxattr = bcachefs_fuse_listxattr,
979 //.removexattr = bcachefs_fuse_removexattr,
980 .create = bcachefs_fuse_create,
984 .getlk = bcachefs_fuse_getlk,
985 .setlk = bcachefs_fuse_setlk,
987 //.write_buf = bcachefs_fuse_write_buf,
988 //.fallocate = bcachefs_fuse_fallocate,
993 * Setup and command parsing.
1002 static void bf_context_free(struct bf_context *ctx)
1006 free(ctx->devices_str);
1007 for (i = 0; i < ctx->nr_devices; ++i)
1008 free(ctx->devices[i]);
1012 static struct fuse_opt bf_opts[] = {
1017 * Fuse option parsing helper -- returning 0 means we consumed the argument, 1
1020 static int bf_opt_proc(void *data, const char *arg, int key,
1021 struct fuse_args *outargs)
1023 struct bf_context *ctx = data;
1026 case FUSE_OPT_KEY_NONOPT:
1027 /* Just extract the first non-option string. */
1028 if (!ctx->devices_str) {
1029 ctx->devices_str = strdup(arg);
1039 * dev1:dev2 -> [ dev1, dev2 ]
1042 static void tokenize_devices(struct bf_context *ctx)
1044 char *devices_str = strdup(ctx->devices_str);
1045 char *devices_tmp = devices_str;
1046 char **devices = NULL;
1050 while ((dev = strsep(&devices_tmp, ":"))) {
1051 if (strlen(dev) > 0) {
1052 devices = realloc(devices, (nr + 1) * sizeof *devices);
1053 devices[nr] = strdup(dev);
1059 devices = malloc(sizeof *devices);
1060 devices[0] = strdup(ctx->devices_str);
1064 ctx->devices = devices;
1065 ctx->nr_devices = nr;
1070 static void usage(char *argv[])
1072 printf("Usage: %s fusemount [options] <dev>[:dev2:...] <mountpoint>\n",
1077 int cmd_fusemount(int argc, char *argv[])
1079 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
1080 struct bch_opts bch_opts = bch2_opts_empty();
1081 struct bf_context ctx = { 0 };
1082 struct bch_fs *c = NULL;
1085 /* Parse arguments. */
1086 if (fuse_opt_parse(&args, &ctx, bf_opts, bf_opt_proc) < 0)
1087 die("fuse_opt_parse err: %m");
1089 struct fuse_cmdline_opts fuse_opts;
1090 if (fuse_parse_cmdline(&args, &fuse_opts) < 0)
1091 die("fuse_parse_cmdline err: %m");
1093 if (fuse_opts.show_help) {
1095 fuse_cmdline_help();
1096 fuse_lowlevel_help();
1100 if (fuse_opts.show_version) {
1101 /* TODO: Show bcachefs version. */
1102 printf("FUSE library version %s\n", fuse_pkgversion());
1103 fuse_lowlevel_version();
1107 if (!fuse_opts.mountpoint) {
1109 printf("Please supply a mountpoint.\n");
1113 if (!ctx.devices_str) {
1115 printf("Please specify a device or device1:device2:...\n");
1119 tokenize_devices(&ctx);
1122 printf("Opening bcachefs filesystem on:\n");
1123 for (i = 0; i < ctx.nr_devices; ++i)
1124 printf("\t%s\n", ctx.devices[i]);
1126 c = bch2_fs_open(ctx.devices, ctx.nr_devices, bch_opts);
1128 die("error opening %s: %s", ctx.devices_str,
1129 strerror(-PTR_ERR(c)));
1132 struct fuse_session *se =
1133 fuse_session_new(&args, &bcachefs_fuse_ops,
1134 sizeof(bcachefs_fuse_ops), c);
1136 die("fuse_lowlevel_new err: %m");
1138 if (fuse_set_signal_handlers(se) < 0)
1139 die("fuse_set_signal_handlers err: %m");
1141 if (fuse_session_mount(se, fuse_opts.mountpoint))
1142 die("fuse_mount err: %m");
1144 fuse_daemonize(fuse_opts.foreground);
1146 ret = fuse_session_loop(se);
1149 fuse_session_unmount(se);
1150 fuse_remove_signal_handlers(se);
1151 fuse_session_destroy(se);
1154 free(fuse_opts.mountpoint);
1155 fuse_opt_free_args(&args);
1156 bf_context_free(&ctx);