7 #include <sys/statvfs.h>
9 #include <fuse_lowlevel.h>
12 #include "libbcachefs.h"
13 #include "tools-util.h"
15 #include "libbcachefs/bcachefs.h"
16 #include "libbcachefs/alloc_foreground.h"
17 #include "libbcachefs/btree_iter.h"
18 #include "libbcachefs/buckets.h"
19 #include "libbcachefs/dirent.h"
20 #include "libbcachefs/errcode.h"
21 #include "libbcachefs/error.h"
22 #include "libbcachefs/fs-common.h"
23 #include "libbcachefs/inode.h"
24 #include "libbcachefs/io_read.h"
25 #include "libbcachefs/io_write.h"
26 #include "libbcachefs/opts.h"
27 #include "libbcachefs/super.h"
30 #include "libbcachefs/fs.h"
32 #include <linux/dcache.h>
34 /* XXX cut and pasted from fsck.c */
35 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
37 static inline subvol_inum map_root_ino(u64 ino)
39 return (subvol_inum) { 1, ino == 1 ? 4096 : ino };
42 static inline u64 unmap_root_ino(u64 ino)
44 return ino == 4096 ? 1 : ino;
47 static struct stat inode_to_stat(struct bch_fs *c,
48 struct bch_inode_unpacked *bi)
50 return (struct stat) {
51 .st_ino = unmap_root_ino(bi->bi_inum),
52 .st_size = bi->bi_size,
53 .st_mode = bi->bi_mode,
56 .st_nlink = bch2_inode_nlink_get(bi),
57 .st_rdev = bi->bi_dev,
58 .st_blksize = block_bytes(c),
59 .st_blocks = bi->bi_sectors,
60 .st_atim = bch2_time_to_timespec(c, bi->bi_atime),
61 .st_mtim = bch2_time_to_timespec(c, bi->bi_mtime),
62 .st_ctim = bch2_time_to_timespec(c, bi->bi_ctime),
66 static struct fuse_entry_param inode_to_entry(struct bch_fs *c,
67 struct bch_inode_unpacked *bi)
69 return (struct fuse_entry_param) {
70 .ino = unmap_root_ino(bi->bi_inum),
71 .generation = bi->bi_generation,
72 .attr = inode_to_stat(c, bi),
73 .attr_timeout = DBL_MAX,
74 .entry_timeout = DBL_MAX,
78 static void bcachefs_fuse_init(void *arg, struct fuse_conn_info *conn)
80 if (conn->capable & FUSE_CAP_WRITEBACK_CACHE) {
81 fuse_log(FUSE_LOG_DEBUG, "fuse_init: activating writeback\n");
82 conn->want |= FUSE_CAP_WRITEBACK_CACHE;
84 fuse_log(FUSE_LOG_DEBUG, "fuse_init: writeback not capable\n");
86 //conn->want |= FUSE_CAP_POSIX_ACL;
89 static void bcachefs_fuse_destroy(void *arg)
91 struct bch_fs *c = arg;
96 static void bcachefs_fuse_lookup(fuse_req_t req, fuse_ino_t dir_ino,
99 subvol_inum dir = map_root_ino(dir_ino);
100 struct bch_fs *c = fuse_req_userdata(req);
101 struct bch_inode_unpacked bi;
102 struct qstr qstr = QSTR(name);
106 fuse_log(FUSE_LOG_DEBUG, "fuse_lookup(dir=%llu name=%s)\n",
109 ret = bch2_inode_find_by_inum(c, dir, &bi);
111 fuse_reply_err(req, -ret);
115 struct bch_hash_info hash_info = bch2_hash_info_init(c, &bi);
117 ret = bch2_dirent_lookup(c, dir, &hash_info, &qstr, &inum);
119 struct fuse_entry_param e = {
120 .attr_timeout = DBL_MAX,
121 .entry_timeout = DBL_MAX,
123 fuse_reply_entry(req, &e);
127 ret = bch2_inode_find_by_inum(c, inum, &bi);
131 fuse_log(FUSE_LOG_DEBUG, "fuse_lookup ret(inum=%llu)\n",
134 struct fuse_entry_param e = inode_to_entry(c, &bi);
135 fuse_reply_entry(req, &e);
138 fuse_log(FUSE_LOG_DEBUG, "fuse_lookup error %i\n", ret);
139 fuse_reply_err(req, -ret);
142 static void bcachefs_fuse_getattr(fuse_req_t req, fuse_ino_t ino,
143 struct fuse_file_info *fi)
145 subvol_inum inum = map_root_ino(ino);
146 struct bch_fs *c = fuse_req_userdata(req);
147 struct bch_inode_unpacked bi;
150 fuse_log(FUSE_LOG_DEBUG, "fuse_getattr(inum=%llu)\n", inum.inum);
152 int ret = bch2_inode_find_by_inum(c, inum, &bi);
154 fuse_log(FUSE_LOG_DEBUG, "fuse_getattr error %i\n", ret);
155 fuse_reply_err(req, -ret);
159 fuse_log(FUSE_LOG_DEBUG, "fuse_getattr success\n");
161 attr = inode_to_stat(c, &bi);
162 fuse_reply_attr(req, &attr, DBL_MAX);
165 static void bcachefs_fuse_setattr(fuse_req_t req, fuse_ino_t ino,
166 struct stat *attr, int to_set,
167 struct fuse_file_info *fi)
169 struct bch_fs *c = fuse_req_userdata(req);
170 struct bch_inode_unpacked inode_u;
171 struct btree_trans *trans;
172 struct btree_iter iter;
176 subvol_inum inum = map_root_ino(ino);
178 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_setattr(%llu, %x)\n", inum.inum, to_set);
180 trans = bch2_trans_get(c);
182 bch2_trans_begin(trans);
183 now = bch2_current_time(c);
185 ret = bch2_inode_peek(trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
189 if (to_set & FUSE_SET_ATTR_MODE)
190 inode_u.bi_mode = attr->st_mode;
191 if (to_set & FUSE_SET_ATTR_UID)
192 inode_u.bi_uid = attr->st_uid;
193 if (to_set & FUSE_SET_ATTR_GID)
194 inode_u.bi_gid = attr->st_gid;
195 if (to_set & FUSE_SET_ATTR_SIZE)
196 inode_u.bi_size = attr->st_size;
197 if (to_set & FUSE_SET_ATTR_ATIME)
198 inode_u.bi_atime = timespec_to_bch2_time(c, attr->st_atim);
199 if (to_set & FUSE_SET_ATTR_MTIME)
200 inode_u.bi_mtime = timespec_to_bch2_time(c, attr->st_mtim);
201 if (to_set & FUSE_SET_ATTR_ATIME_NOW)
202 inode_u.bi_atime = now;
203 if (to_set & FUSE_SET_ATTR_MTIME_NOW)
204 inode_u.bi_mtime = now;
207 ret = bch2_inode_write(trans, &iter, &inode_u) ?:
208 bch2_trans_commit(trans, NULL, NULL,
209 BCH_TRANS_COMMIT_no_enospc);
211 bch2_trans_iter_exit(trans, &iter);
215 bch2_trans_put(trans);
218 *attr = inode_to_stat(c, &inode_u);
219 fuse_reply_attr(req, attr, DBL_MAX);
221 fuse_reply_err(req, -ret);
225 static int do_create(struct bch_fs *c, subvol_inum dir,
226 const char *name, mode_t mode, dev_t rdev,
227 struct bch_inode_unpacked *new_inode)
229 struct qstr qstr = QSTR(name);
230 struct bch_inode_unpacked dir_u;
234 bch2_inode_init_early(c, new_inode);
236 return bch2_trans_do(c, NULL, NULL, 0,
237 bch2_create_trans(trans,
240 uid, gid, mode, rdev, NULL, NULL,
241 (subvol_inum) { 0 }, 0));
244 static void bcachefs_fuse_mknod(fuse_req_t req, fuse_ino_t dir_ino,
245 const char *name, mode_t mode,
248 subvol_inum dir = map_root_ino(dir_ino);
249 struct bch_fs *c = fuse_req_userdata(req);
250 struct bch_inode_unpacked new_inode;
253 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_mknod(%llu, %s, %x, %x)\n",
254 dir.inum, name, mode, rdev);
256 ret = do_create(c, dir, name, mode, rdev, &new_inode);
260 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
261 fuse_reply_entry(req, &e);
264 fuse_reply_err(req, -ret);
267 static void bcachefs_fuse_mkdir(fuse_req_t req, fuse_ino_t dir,
268 const char *name, mode_t mode)
270 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_mkdir(%llu, %s, %x)\n",
273 BUG_ON(mode & S_IFMT);
276 bcachefs_fuse_mknod(req, dir, name, mode, 0);
279 static void bcachefs_fuse_unlink(fuse_req_t req, fuse_ino_t dir_ino,
282 struct bch_fs *c = fuse_req_userdata(req);
283 struct bch_inode_unpacked dir_u, inode_u;
284 struct qstr qstr = QSTR(name);
285 subvol_inum dir = map_root_ino(dir_ino);
287 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_unlink(%llu, %s)\n", dir.inum, name);
289 int ret = bch2_trans_do(c, NULL, NULL,
290 BCH_TRANS_COMMIT_no_enospc,
291 bch2_unlink_trans(trans, dir, &dir_u,
292 &inode_u, &qstr, false));
294 fuse_reply_err(req, -ret);
297 static void bcachefs_fuse_rmdir(fuse_req_t req, fuse_ino_t dir,
300 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_rmdir(%llu, %s)\n", dir, name);
302 bcachefs_fuse_unlink(req, dir, name);
305 static void bcachefs_fuse_rename(fuse_req_t req,
306 fuse_ino_t src_dir_ino, const char *srcname,
307 fuse_ino_t dst_dir_ino, const char *dstname,
310 struct bch_fs *c = fuse_req_userdata(req);
311 struct bch_inode_unpacked dst_dir_u, src_dir_u;
312 struct bch_inode_unpacked src_inode_u, dst_inode_u;
313 struct qstr dst_name = QSTR(srcname);
314 struct qstr src_name = QSTR(dstname);
315 subvol_inum src_dir = map_root_ino(src_dir_ino);
316 subvol_inum dst_dir = map_root_ino(dst_dir_ino);
319 fuse_log(FUSE_LOG_DEBUG,
320 "bcachefs_fuse_rename(%llu, %s, %llu, %s, %x)\n",
321 src_dir.inum, srcname, dst_dir.inum, dstname, flags);
323 /* XXX handle overwrites */
324 ret = bch2_trans_do(c, NULL, NULL, 0,
325 bch2_rename_trans(trans,
328 &src_inode_u, &dst_inode_u,
329 &src_name, &dst_name,
332 fuse_reply_err(req, -ret);
335 static void bcachefs_fuse_link(fuse_req_t req, fuse_ino_t ino,
336 fuse_ino_t newparent_ino, const char *newname)
338 struct bch_fs *c = fuse_req_userdata(req);
339 struct bch_inode_unpacked dir_u, inode_u;
340 struct qstr qstr = QSTR(newname);
341 subvol_inum newparent = map_root_ino(newparent_ino);
342 subvol_inum inum = map_root_ino(ino);
345 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_link(%llu, %llu, %s)\n",
346 inum, newparent.inum, newname);
348 ret = bch2_trans_do(c, NULL, NULL, 0,
349 bch2_link_trans(trans, newparent, &dir_u,
350 inum, &inode_u, &qstr));
353 struct fuse_entry_param e = inode_to_entry(c, &inode_u);
354 fuse_reply_entry(req, &e);
356 fuse_reply_err(req, -ret);
360 static void bcachefs_fuse_open(fuse_req_t req, fuse_ino_t inum,
361 struct fuse_file_info *fi)
363 fi->direct_io = false;
364 fi->keep_cache = true;
365 fi->cache_readdir = true;
367 fuse_reply_open(req, fi);
370 static void userbio_init(struct bio *bio, struct bio_vec *bv,
371 void *buf, size_t size)
373 bio_init(bio, NULL, bv, 1, 0);
374 bio->bi_iter.bi_size = size;
380 static int get_inode_io_opts(struct bch_fs *c, subvol_inum inum, struct bch_io_opts *opts)
382 struct bch_inode_unpacked inode;
383 if (bch2_inode_find_by_inum(c, inum, &inode))
386 bch2_inode_opts_get(opts, c, &inode);
390 static void bcachefs_fuse_read_endio(struct bio *bio)
392 closure_put(bio->bi_private);
395 struct fuse_align_io {
403 /* Handle unaligned start and end */
404 /* TODO: align to block_bytes, sector size, or page size? */
405 static struct fuse_align_io align_io(const struct bch_fs *c, size_t size,
408 struct fuse_align_io align;
412 align.start = round_down(offset, block_bytes(c));
413 align.pad_start = offset - align.start;
415 off_t end = offset + size;
416 align.end = round_up(end, block_bytes(c));
417 align.pad_end = align.end - end;
419 align.size = align.end - align.start;
425 * Given an aligned number of bytes transferred, figure out how many unaligned
426 * bytes were transferred.
428 static size_t align_fix_up_bytes(const struct fuse_align_io *align,
433 if (align_bytes > align->pad_start) {
434 bytes = align_bytes - align->pad_start;
435 bytes = bytes > align->pad_end ? bytes - align->pad_end : 0;
444 static int read_aligned(struct bch_fs *c, subvol_inum inum, size_t aligned_size,
445 off_t aligned_offset, void *buf)
447 BUG_ON(aligned_size & (block_bytes(c) - 1));
448 BUG_ON(aligned_offset & (block_bytes(c) - 1));
450 struct bch_io_opts io_opts;
451 if (get_inode_io_opts(c, inum, &io_opts))
454 struct bch_read_bio rbio;
456 userbio_init(&rbio.bio, &bv, buf, aligned_size);
457 bio_set_op_attrs(&rbio.bio, REQ_OP_READ, REQ_SYNC);
458 rbio.bio.bi_iter.bi_sector = aligned_offset >> 9;
461 closure_init_stack(&cl);
464 rbio.bio.bi_end_io = bcachefs_fuse_read_endio;
465 rbio.bio.bi_private = &cl;
467 bch2_read(c, rbio_init(&rbio.bio, io_opts), inum);
471 return -blk_status_to_errno(rbio.bio.bi_status);
474 static void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t ino,
475 size_t size, off_t offset,
476 struct fuse_file_info *fi)
478 subvol_inum inum = map_root_ino(ino);
479 struct bch_fs *c = fuse_req_userdata(req);
481 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_read(%llu, %zd, %lld)\n",
484 /* Check inode size. */
485 struct bch_inode_unpacked bi;
486 int ret = bch2_inode_find_by_inum(c, inum, &bi);
488 fuse_reply_err(req, -ret);
492 off_t end = min_t(u64, bi.bi_size, offset + size);
494 fuse_reply_buf(req, NULL, 0);
499 struct fuse_align_io align = align_io(c, size, offset);
501 void *buf = aligned_alloc(PAGE_SIZE, align.size);
503 fuse_reply_err(req, ENOMEM);
507 ret = read_aligned(c, inum, align.size, align.start, buf);
510 fuse_reply_buf(req, buf + align.pad_start, size);
512 fuse_reply_err(req, -ret);
517 static int inode_update_times(struct bch_fs *c, subvol_inum inum)
519 struct btree_trans *trans;
520 struct btree_iter iter;
521 struct bch_inode_unpacked inode_u;
525 trans = bch2_trans_get(c);
527 bch2_trans_begin(trans);
528 now = bch2_current_time(c);
530 ret = bch2_inode_peek(trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
534 inode_u.bi_mtime = now;
535 inode_u.bi_ctime = now;
537 ret = bch2_inode_write(trans, &iter, &inode_u);
541 ret = bch2_trans_commit(trans, NULL, NULL,
542 BCH_TRANS_COMMIT_no_enospc);
544 bch2_trans_iter_exit(trans, &iter);
548 bch2_trans_put(trans);
552 static int write_aligned(struct bch_fs *c, subvol_inum inum,
553 struct bch_io_opts io_opts, void *buf,
554 size_t aligned_size, off_t aligned_offset,
555 off_t new_i_size, size_t *written_out)
557 struct bch_write_op op = { 0 };
561 BUG_ON(aligned_size & (block_bytes(c) - 1));
562 BUG_ON(aligned_offset & (block_bytes(c) - 1));
566 closure_init_stack(&cl);
568 bch2_write_op_init(&op, c, io_opts); /* XXX reads from op?! */
569 op.write_point = writepoint_hashed(0);
570 op.nr_replicas = io_opts.data_replicas;
571 op.target = io_opts.foreground_target;
572 op.subvol = inum.subvol;
573 op.pos = POS(inum.inum, aligned_offset >> 9);
574 op.new_i_size = new_i_size;
576 userbio_init(&op.wbio.bio, &bv, buf, aligned_size);
577 bio_set_op_attrs(&op.wbio.bio, REQ_OP_WRITE, REQ_SYNC);
579 if (bch2_disk_reservation_get(c, &op.res, aligned_size >> 9,
580 op.nr_replicas, 0)) {
581 /* XXX: use check_range_allocated like dio write path */
585 closure_call(&op.cl, bch2_write, NULL, &cl);
589 *written_out = op.written << 9;
594 static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t ino,
595 const char *buf, size_t size,
597 struct fuse_file_info *fi)
599 subvol_inum inum = map_root_ino(ino);
600 struct bch_fs *c = fuse_req_userdata(req);
601 struct bch_io_opts io_opts;
602 size_t aligned_written;
605 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write(%llu, %zd, %lld)\n",
608 struct fuse_align_io align = align_io(c, size, offset);
609 void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
610 BUG_ON(!aligned_buf);
612 if (get_inode_io_opts(c, inum, &io_opts)) {
617 /* Realign the data and read in start and end, if needed */
619 /* Read partial start data. */
620 if (align.pad_start) {
621 memset(aligned_buf, 0, block_bytes(c));
623 ret = read_aligned(c, inum, block_bytes(c), align.start,
630 * Read partial end data. If the whole write fits in one block, the
631 * start data and the end data are the same so this isn't needed.
634 !(align.pad_start && align.size == block_bytes(c))) {
635 off_t partial_end_start = align.end - block_bytes(c);
636 size_t buf_offset = align.size - block_bytes(c);
638 memset(aligned_buf + buf_offset, 0, block_bytes(c));
640 ret = read_aligned(c, inum, block_bytes(c), partial_end_start,
641 aligned_buf + buf_offset);
646 /* Overlay what we want to write. */
647 memcpy(aligned_buf + align.pad_start, buf, size);
649 /* Actually write. */
650 ret = write_aligned(c, inum, io_opts, aligned_buf,
651 align.size, align.start,
652 offset + size, &aligned_written);
654 /* Figure out how many unaligned bytes were written. */
655 size_t written = align_fix_up_bytes(&align, aligned_written);
656 BUG_ON(written > size);
658 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write: wrote %zd bytes\n",
665 * Update inode times.
666 * TODO: Integrate with bch2_extent_update()
669 ret = inode_update_times(c, inum);
672 BUG_ON(written == 0);
673 fuse_reply_write(req, written);
679 fuse_reply_err(req, -ret);
683 static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
684 fuse_ino_t dir_ino, const char *name)
686 subvol_inum dir = map_root_ino(dir_ino);
687 struct bch_fs *c = fuse_req_userdata(req);
688 struct bch_inode_unpacked new_inode;
689 size_t link_len = strlen(link);
692 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_symlink(%s, %llu, %s)\n",
693 link, dir.inum, name);
695 ret = do_create(c, dir, name, S_IFLNK|S_IRWXUGO, 0, &new_inode);
699 struct bch_io_opts io_opts;
700 ret = get_inode_io_opts(c, dir, &io_opts);
704 struct fuse_align_io align = align_io(c, link_len + 1, 0);
706 void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
707 BUG_ON(!aligned_buf);
709 memset(aligned_buf, 0, align.size);
710 memcpy(aligned_buf, link, link_len); /* already terminated */
712 subvol_inum inum = (subvol_inum) { dir.subvol, new_inode.bi_inum };
714 size_t aligned_written;
715 ret = write_aligned(c, inum, io_opts, aligned_buf,
716 align.size, align.start, link_len + 1,
723 size_t written = align_fix_up_bytes(&align, aligned_written);
724 BUG_ON(written != link_len + 1); // TODO: handle short
726 ret = inode_update_times(c, inum);
730 new_inode.bi_size = written;
732 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
733 fuse_reply_entry(req, &e);
737 fuse_reply_err(req, -ret);
740 static void bcachefs_fuse_readlink(fuse_req_t req, fuse_ino_t ino)
742 subvol_inum inum = map_root_ino(ino);
743 struct bch_fs *c = fuse_req_userdata(req);
746 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readlink(%llu)\n", inum.inum);
748 struct bch_inode_unpacked bi;
749 int ret = bch2_inode_find_by_inum(c, inum, &bi);
753 struct fuse_align_io align = align_io(c, bi.bi_size, 0);
756 buf = aligned_alloc(PAGE_SIZE, align.size);
760 ret = read_aligned(c, inum, align.size, align.start, buf);
764 BUG_ON(buf[align.size - 1] != 0);
766 fuse_reply_readlink(req, buf);
770 fuse_reply_err(req, -ret);
777 * FUSE flush is essentially the close() call, however it is not guaranteed
778 * that one flush happens per open/create.
780 * It doesn't have to do anything, and is mostly relevant for NFS-style
781 * filesystems where close has some relationship to caching.
783 static void bcachefs_fuse_flush(fuse_req_t req, fuse_ino_t inum,
784 struct fuse_file_info *fi)
786 struct bch_fs *c = fuse_req_userdata(req);
789 static void bcachefs_fuse_release(fuse_req_t req, fuse_ino_t inum,
790 struct fuse_file_info *fi)
792 struct bch_fs *c = fuse_req_userdata(req);
795 static void bcachefs_fuse_fsync(fuse_req_t req, fuse_ino_t inum, int datasync,
796 struct fuse_file_info *fi)
798 struct bch_fs *c = fuse_req_userdata(req);
801 static void bcachefs_fuse_opendir(fuse_req_t req, fuse_ino_t inum,
802 struct fuse_file_info *fi)
804 struct bch_fs *c = fuse_req_userdata(req);
808 struct fuse_dir_context {
809 struct dir_context ctx;
823 #define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name)
824 #define FUSE_DIRENT_ALIGN(x) \
825 (((x) + sizeof(uint64_t) - 1) & ~(sizeof(uint64_t) - 1))
827 static size_t fuse_add_direntry2(char *buf, size_t bufsize,
828 const char *name, int namelen,
829 const struct stat *stbuf, off_t off)
831 size_t entlen = FUSE_NAME_OFFSET + namelen;
832 size_t entlen_padded = FUSE_DIRENT_ALIGN(entlen);
833 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
835 if ((buf == NULL) || (entlen_padded > bufsize))
836 return entlen_padded;
838 dirent->ino = stbuf->st_ino;
840 dirent->namelen = namelen;
841 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
842 memcpy(dirent->name, name, namelen);
843 memset(dirent->name + namelen, 0, entlen_padded - entlen);
845 return entlen_padded;
848 static int fuse_filldir(struct dir_context *_ctx,
849 const char *name, int namelen,
850 loff_t pos, u64 ino, unsigned type)
852 struct fuse_dir_context *ctx =
853 container_of(_ctx, struct fuse_dir_context, ctx);
855 struct stat statbuf = {
856 .st_ino = unmap_root_ino(ino),
857 .st_mode = type << 12,
860 fuse_log(FUSE_LOG_DEBUG, "fuse_filldir(name=%s inum=%llu pos=%llu)\n",
861 name, statbuf.st_ino, pos);
863 size_t len = fuse_add_direntry2(ctx->buf,
870 if (len > ctx->bufsize)
879 static bool handle_dots(struct fuse_dir_context *ctx, fuse_ino_t dir)
881 if (ctx->ctx.pos == 0) {
882 if (fuse_filldir(&ctx->ctx, ".", 1, ctx->ctx.pos,
888 if (ctx->ctx.pos == 1) {
889 if (fuse_filldir(&ctx->ctx, "..", 2, ctx->ctx.pos,
890 /*TODO: parent*/ 1, DT_DIR) < 0)
898 static void bcachefs_fuse_readdir(fuse_req_t req, fuse_ino_t dir_ino,
899 size_t size, off_t off,
900 struct fuse_file_info *fi)
902 subvol_inum dir = map_root_ino(dir_ino);
903 struct bch_fs *c = fuse_req_userdata(req);
904 struct bch_inode_unpacked bi;
905 char *buf = calloc(size, 1);
906 struct fuse_dir_context ctx = {
907 .ctx.actor = fuse_filldir,
915 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir(dir=%llu, size=%zu, "
916 "off=%lld)\n", dir.inum, size, off);
918 ret = bch2_inode_find_by_inum(c, dir, &bi);
922 if (!S_ISDIR(bi.bi_mode)) {
927 if (!handle_dots(&ctx, dir.inum))
930 ret = bch2_readdir(c, dir, &ctx.ctx);
933 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir reply %zd\n",
935 fuse_reply_buf(req, buf, ctx.buf - buf);
937 fuse_reply_err(req, -ret);
944 static void bcachefs_fuse_readdirplus(fuse_req_t req, fuse_ino_t dir,
945 size_t size, off_t off,
946 struct fuse_file_info *fi)
951 static void bcachefs_fuse_releasedir(fuse_req_t req, fuse_ino_t inum,
952 struct fuse_file_info *fi)
954 struct bch_fs *c = fuse_req_userdata(req);
957 static void bcachefs_fuse_fsyncdir(fuse_req_t req, fuse_ino_t inum, int datasync,
958 struct fuse_file_info *fi)
960 struct bch_fs *c = fuse_req_userdata(req);
964 static void bcachefs_fuse_statfs(fuse_req_t req, fuse_ino_t inum)
966 struct bch_fs *c = fuse_req_userdata(req);
967 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
968 unsigned shift = c->block_bits;
969 struct statvfs statbuf = {
970 .f_bsize = block_bytes(c),
971 .f_frsize = block_bytes(c),
972 .f_blocks = usage.capacity >> shift,
973 .f_bfree = (usage.capacity - usage.used) >> shift,
974 //.f_bavail = statbuf.f_bfree,
975 .f_files = usage.nr_inodes,
977 .f_namemax = BCH_NAME_MAX,
980 fuse_reply_statfs(req, &statbuf);
984 static void bcachefs_fuse_setxattr(fuse_req_t req, fuse_ino_t inum,
985 const char *name, const char *value,
986 size_t size, int flags)
988 struct bch_fs *c = fuse_req_userdata(req);
991 static void bcachefs_fuse_getxattr(fuse_req_t req, fuse_ino_t inum,
992 const char *name, size_t size)
994 struct bch_fs *c = fuse_req_userdata(req);
996 fuse_reply_xattr(req, );
999 static void bcachefs_fuse_listxattr(fuse_req_t req, fuse_ino_t inum, size_t size)
1001 struct bch_fs *c = fuse_req_userdata(req);
1004 static void bcachefs_fuse_removexattr(fuse_req_t req, fuse_ino_t inum,
1007 struct bch_fs *c = fuse_req_userdata(req);
1011 static void bcachefs_fuse_create(fuse_req_t req, fuse_ino_t dir_ino,
1012 const char *name, mode_t mode,
1013 struct fuse_file_info *fi)
1015 subvol_inum dir = map_root_ino(dir_ino);
1016 struct bch_fs *c = fuse_req_userdata(req);
1017 struct bch_inode_unpacked new_inode;
1020 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_create(%llu, %s, %x)\n",
1021 dir.inum, name, mode);
1023 ret = do_create(c, dir, name, mode, 0, &new_inode);
1027 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
1028 fuse_reply_create(req, &e, fi);
1031 fuse_reply_err(req, -ret);
1035 static void bcachefs_fuse_write_buf(fuse_req_t req, fuse_ino_t inum,
1036 struct fuse_bufvec *bufv, off_t off,
1037 struct fuse_file_info *fi)
1039 struct bch_fs *c = fuse_req_userdata(req);
1042 static void bcachefs_fuse_fallocate(fuse_req_t req, fuse_ino_t inum, int mode,
1043 off_t offset, off_t length,
1044 struct fuse_file_info *fi)
1046 struct bch_fs *c = fuse_req_userdata(req);
1050 static const struct fuse_lowlevel_ops bcachefs_fuse_ops = {
1051 .init = bcachefs_fuse_init,
1052 .destroy = bcachefs_fuse_destroy,
1053 .lookup = bcachefs_fuse_lookup,
1054 .getattr = bcachefs_fuse_getattr,
1055 .setattr = bcachefs_fuse_setattr,
1056 .readlink = bcachefs_fuse_readlink,
1057 .mknod = bcachefs_fuse_mknod,
1058 .mkdir = bcachefs_fuse_mkdir,
1059 .unlink = bcachefs_fuse_unlink,
1060 .rmdir = bcachefs_fuse_rmdir,
1061 .symlink = bcachefs_fuse_symlink,
1062 .rename = bcachefs_fuse_rename,
1063 .link = bcachefs_fuse_link,
1064 .open = bcachefs_fuse_open,
1065 .read = bcachefs_fuse_read,
1066 .write = bcachefs_fuse_write,
1067 //.flush = bcachefs_fuse_flush,
1068 //.release = bcachefs_fuse_release,
1069 //.fsync = bcachefs_fuse_fsync,
1070 //.opendir = bcachefs_fuse_opendir,
1071 .readdir = bcachefs_fuse_readdir,
1072 //.readdirplus = bcachefs_fuse_readdirplus,
1073 //.releasedir = bcachefs_fuse_releasedir,
1074 //.fsyncdir = bcachefs_fuse_fsyncdir,
1075 .statfs = bcachefs_fuse_statfs,
1076 //.setxattr = bcachefs_fuse_setxattr,
1077 //.getxattr = bcachefs_fuse_getxattr,
1078 //.listxattr = bcachefs_fuse_listxattr,
1079 //.removexattr = bcachefs_fuse_removexattr,
1080 .create = bcachefs_fuse_create,
1084 .getlk = bcachefs_fuse_getlk,
1085 .setlk = bcachefs_fuse_setlk,
1087 //.write_buf = bcachefs_fuse_write_buf,
1088 //.fallocate = bcachefs_fuse_fallocate,
1093 * Setup and command parsing.
1102 static void bf_context_free(struct bf_context *ctx)
1106 free(ctx->devices_str);
1107 for (i = 0; i < ctx->nr_devices; ++i)
1108 free(ctx->devices[i]);
1112 static struct fuse_opt bf_opts[] = {
1117 * Fuse option parsing helper -- returning 0 means we consumed the argument, 1
1120 static int bf_opt_proc(void *data, const char *arg, int key,
1121 struct fuse_args *outargs)
1123 struct bf_context *ctx = data;
1126 case FUSE_OPT_KEY_NONOPT:
1127 /* Just extract the first non-option string. */
1128 if (!ctx->devices_str) {
1129 ctx->devices_str = strdup(arg);
1139 * dev1:dev2 -> [ dev1, dev2 ]
1142 static void tokenize_devices(struct bf_context *ctx)
1144 char *devices_str = strdup(ctx->devices_str);
1145 char *devices_tmp = devices_str;
1146 char **devices = NULL;
1150 while ((dev = strsep(&devices_tmp, ":"))) {
1151 if (strlen(dev) > 0) {
1152 devices = realloc(devices, (nr + 1) * sizeof *devices);
1153 devices[nr] = strdup(dev);
1159 devices = malloc(sizeof *devices);
1160 devices[0] = strdup(ctx->devices_str);
1164 ctx->devices = devices;
1165 ctx->nr_devices = nr;
1170 static void usage(char *argv[])
1172 printf("Usage: %s fusemount [options] <dev>[:dev2:...] <mountpoint>\n",
1177 int cmd_fusemount(int argc, char *argv[])
1179 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
1180 struct bch_opts bch_opts = bch2_opts_empty();
1181 struct bf_context ctx = { 0 };
1182 struct bch_fs *c = NULL;
1185 /* Parse arguments. */
1186 if (fuse_opt_parse(&args, &ctx, bf_opts, bf_opt_proc) < 0)
1187 die("fuse_opt_parse err: %m");
1189 struct fuse_cmdline_opts fuse_opts;
1190 if (fuse_parse_cmdline(&args, &fuse_opts) < 0)
1191 die("fuse_parse_cmdline err: %m");
1193 if (fuse_opts.show_help) {
1195 fuse_cmdline_help();
1196 fuse_lowlevel_help();
1200 if (fuse_opts.show_version) {
1201 /* TODO: Show bcachefs version. */
1202 printf("FUSE library version %s\n", fuse_pkgversion());
1203 fuse_lowlevel_version();
1207 if (!fuse_opts.mountpoint) {
1209 printf("Please supply a mountpoint.\n");
1213 if (!ctx.devices_str) {
1215 printf("Please specify a device or device1:device2:...\n");
1219 tokenize_devices(&ctx);
1221 struct printbuf fsname = PRINTBUF;
1222 prt_printf(&fsname, "fsname=");
1223 for (i = 0; i < ctx.nr_devices; ++i) {
1225 prt_str(&fsname, ":");
1226 prt_str(&fsname, ctx.devices[i]);
1229 fuse_opt_add_arg(&args, "-o");
1230 fuse_opt_add_arg(&args, fsname.buf);
1233 printf("Opening bcachefs filesystem on:\n");
1234 for (i = 0; i < ctx.nr_devices; ++i)
1235 printf("\t%s\n", ctx.devices[i]);
1237 c = bch2_fs_open(ctx.devices, ctx.nr_devices, bch_opts);
1239 die("error opening %s: %s", ctx.devices_str,
1240 bch2_err_str(PTR_ERR(c)));
1243 struct fuse_session *se =
1244 fuse_session_new(&args, &bcachefs_fuse_ops,
1245 sizeof(bcachefs_fuse_ops), c);
1247 die("fuse_lowlevel_new err: %m");
1249 if (fuse_set_signal_handlers(se) < 0)
1250 die("fuse_set_signal_handlers err: %m");
1252 if (fuse_session_mount(se, fuse_opts.mountpoint))
1253 die("fuse_mount err: %m");
1255 /* This print statement is a trigger for tests. */
1256 printf("Fuse mount initialized.\n");
1258 fuse_daemonize(fuse_opts.foreground);
1260 ret = fuse_session_loop(se);
1263 fuse_session_unmount(se);
1264 fuse_remove_signal_handlers(se);
1265 fuse_session_destroy(se);
1268 free(fuse_opts.mountpoint);
1269 fuse_opt_free_args(&args);
1270 bf_context_free(&ctx);
1275 #endif /* BCACHEFS_FUSE */