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.h"
25 #include "libbcachefs/opts.h"
26 #include "libbcachefs/super.h"
29 #include "libbcachefs/fs.h"
31 #include <linux/dcache.h>
33 /* XXX cut and pasted from fsck.c */
34 #define QSTR(n) { { { .len = strlen(n) } }, .name = n }
36 static inline subvol_inum map_root_ino(u64 ino)
38 return (subvol_inum) { 1, ino == 1 ? 4096 : ino };
41 static inline u64 unmap_root_ino(u64 ino)
43 return ino == 4096 ? 1 : ino;
46 static struct stat inode_to_stat(struct bch_fs *c,
47 struct bch_inode_unpacked *bi)
49 return (struct stat) {
50 .st_ino = unmap_root_ino(bi->bi_inum),
51 .st_size = bi->bi_size,
52 .st_mode = bi->bi_mode,
55 .st_nlink = bch2_inode_nlink_get(bi),
56 .st_rdev = bi->bi_dev,
57 .st_blksize = block_bytes(c),
58 .st_blocks = bi->bi_sectors,
59 .st_atim = bch2_time_to_timespec(c, bi->bi_atime),
60 .st_mtim = bch2_time_to_timespec(c, bi->bi_mtime),
61 .st_ctim = bch2_time_to_timespec(c, bi->bi_ctime),
65 static struct fuse_entry_param inode_to_entry(struct bch_fs *c,
66 struct bch_inode_unpacked *bi)
68 return (struct fuse_entry_param) {
69 .ino = unmap_root_ino(bi->bi_inum),
70 .generation = bi->bi_generation,
71 .attr = inode_to_stat(c, bi),
72 .attr_timeout = DBL_MAX,
73 .entry_timeout = DBL_MAX,
77 static void bcachefs_fuse_init(void *arg, struct fuse_conn_info *conn)
79 if (conn->capable & FUSE_CAP_WRITEBACK_CACHE) {
80 fuse_log(FUSE_LOG_DEBUG, "fuse_init: activating writeback\n");
81 conn->want |= FUSE_CAP_WRITEBACK_CACHE;
83 fuse_log(FUSE_LOG_DEBUG, "fuse_init: writeback not capable\n");
85 //conn->want |= FUSE_CAP_POSIX_ACL;
88 static void bcachefs_fuse_destroy(void *arg)
90 struct bch_fs *c = arg;
95 static void bcachefs_fuse_lookup(fuse_req_t req, fuse_ino_t dir_ino,
98 subvol_inum dir = map_root_ino(dir_ino);
99 struct bch_fs *c = fuse_req_userdata(req);
100 struct bch_inode_unpacked bi;
101 struct qstr qstr = QSTR(name);
105 fuse_log(FUSE_LOG_DEBUG, "fuse_lookup(dir=%llu name=%s)\n",
108 ret = bch2_inode_find_by_inum(c, dir, &bi);
110 fuse_reply_err(req, -ret);
114 struct bch_hash_info hash_info = bch2_hash_info_init(c, &bi);
116 ret = bch2_dirent_lookup(c, dir, &hash_info, &qstr, &inum);
118 struct fuse_entry_param e = {
119 .attr_timeout = DBL_MAX,
120 .entry_timeout = DBL_MAX,
122 fuse_reply_entry(req, &e);
126 ret = bch2_inode_find_by_inum(c, inum, &bi);
130 fuse_log(FUSE_LOG_DEBUG, "fuse_lookup ret(inum=%llu)\n",
133 struct fuse_entry_param e = inode_to_entry(c, &bi);
134 fuse_reply_entry(req, &e);
137 fuse_log(FUSE_LOG_DEBUG, "fuse_lookup error %i\n", ret);
138 fuse_reply_err(req, -ret);
141 static void bcachefs_fuse_getattr(fuse_req_t req, fuse_ino_t ino,
142 struct fuse_file_info *fi)
144 subvol_inum inum = map_root_ino(ino);
145 struct bch_fs *c = fuse_req_userdata(req);
146 struct bch_inode_unpacked bi;
149 fuse_log(FUSE_LOG_DEBUG, "fuse_getattr(inum=%llu)\n", inum.inum);
151 int ret = bch2_inode_find_by_inum(c, inum, &bi);
153 fuse_log(FUSE_LOG_DEBUG, "fuse_getattr error %i\n", ret);
154 fuse_reply_err(req, -ret);
158 fuse_log(FUSE_LOG_DEBUG, "fuse_getattr success\n");
160 attr = inode_to_stat(c, &bi);
161 fuse_reply_attr(req, &attr, DBL_MAX);
164 static void bcachefs_fuse_setattr(fuse_req_t req, fuse_ino_t ino,
165 struct stat *attr, int to_set,
166 struct fuse_file_info *fi)
168 struct bch_fs *c = fuse_req_userdata(req);
169 struct bch_inode_unpacked inode_u;
170 struct btree_trans trans;
171 struct btree_iter iter;
175 subvol_inum inum = map_root_ino(ino);
177 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_setattr(%llu, %x)\n", inum.inum, to_set);
179 bch2_trans_init(&trans, c, 0, 0);
181 bch2_trans_begin(&trans);
182 now = bch2_current_time(c);
184 ret = bch2_inode_peek(&trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
188 if (to_set & FUSE_SET_ATTR_MODE)
189 inode_u.bi_mode = attr->st_mode;
190 if (to_set & FUSE_SET_ATTR_UID)
191 inode_u.bi_uid = attr->st_uid;
192 if (to_set & FUSE_SET_ATTR_GID)
193 inode_u.bi_gid = attr->st_gid;
194 if (to_set & FUSE_SET_ATTR_SIZE)
195 inode_u.bi_size = attr->st_size;
196 if (to_set & FUSE_SET_ATTR_ATIME)
197 inode_u.bi_atime = timespec_to_bch2_time(c, attr->st_atim);
198 if (to_set & FUSE_SET_ATTR_MTIME)
199 inode_u.bi_mtime = timespec_to_bch2_time(c, attr->st_mtim);
200 if (to_set & FUSE_SET_ATTR_ATIME_NOW)
201 inode_u.bi_atime = now;
202 if (to_set & FUSE_SET_ATTR_MTIME_NOW)
203 inode_u.bi_mtime = now;
206 ret = bch2_inode_write(&trans, &iter, &inode_u) ?:
207 bch2_trans_commit(&trans, NULL, NULL,
208 BTREE_INSERT_NOFAIL);
210 bch2_trans_iter_exit(&trans, &iter);
214 bch2_trans_exit(&trans);
217 *attr = inode_to_stat(c, &inode_u);
218 fuse_reply_attr(req, attr, DBL_MAX);
220 fuse_reply_err(req, -ret);
224 static int do_create(struct bch_fs *c, subvol_inum dir,
225 const char *name, mode_t mode, dev_t rdev,
226 struct bch_inode_unpacked *new_inode)
228 struct qstr qstr = QSTR(name);
229 struct bch_inode_unpacked dir_u;
233 bch2_inode_init_early(c, new_inode);
235 return bch2_trans_do(c, NULL, NULL, 0,
236 bch2_create_trans(&trans,
239 uid, gid, mode, rdev, NULL, NULL,
240 (subvol_inum) { 0 }, 0));
243 static void bcachefs_fuse_mknod(fuse_req_t req, fuse_ino_t dir_ino,
244 const char *name, mode_t mode,
247 subvol_inum dir = map_root_ino(dir_ino);
248 struct bch_fs *c = fuse_req_userdata(req);
249 struct bch_inode_unpacked new_inode;
252 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_mknod(%llu, %s, %x, %x)\n",
253 dir.inum, name, mode, rdev);
255 ret = do_create(c, dir, name, mode, rdev, &new_inode);
259 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
260 fuse_reply_entry(req, &e);
263 fuse_reply_err(req, -ret);
266 static void bcachefs_fuse_mkdir(fuse_req_t req, fuse_ino_t dir,
267 const char *name, mode_t mode)
269 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_mkdir(%llu, %s, %x)\n",
272 BUG_ON(mode & S_IFMT);
275 bcachefs_fuse_mknod(req, dir, name, mode, 0);
278 static void bcachefs_fuse_unlink(fuse_req_t req, fuse_ino_t dir_ino,
281 struct bch_fs *c = fuse_req_userdata(req);
282 struct bch_inode_unpacked dir_u, inode_u;
283 struct qstr qstr = QSTR(name);
284 subvol_inum dir = map_root_ino(dir_ino);
286 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_unlink(%llu, %s)\n", dir.inum, name);
288 int ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL,
289 bch2_unlink_trans(&trans, dir, &dir_u,
290 &inode_u, &qstr, false));
292 fuse_reply_err(req, -ret);
295 static void bcachefs_fuse_rmdir(fuse_req_t req, fuse_ino_t dir,
298 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_rmdir(%llu, %s)\n", dir, name);
300 bcachefs_fuse_unlink(req, dir, name);
303 static void bcachefs_fuse_rename(fuse_req_t req,
304 fuse_ino_t src_dir_ino, const char *srcname,
305 fuse_ino_t dst_dir_ino, const char *dstname,
308 struct bch_fs *c = fuse_req_userdata(req);
309 struct bch_inode_unpacked dst_dir_u, src_dir_u;
310 struct bch_inode_unpacked src_inode_u, dst_inode_u;
311 struct qstr dst_name = QSTR(srcname);
312 struct qstr src_name = QSTR(dstname);
313 subvol_inum src_dir = map_root_ino(src_dir_ino);
314 subvol_inum dst_dir = map_root_ino(dst_dir_ino);
317 fuse_log(FUSE_LOG_DEBUG,
318 "bcachefs_fuse_rename(%llu, %s, %llu, %s, %x)\n",
319 src_dir.inum, srcname, dst_dir.inum, dstname, flags);
321 /* XXX handle overwrites */
322 ret = bch2_trans_do(c, NULL, NULL, 0,
323 bch2_rename_trans(&trans,
326 &src_inode_u, &dst_inode_u,
327 &src_name, &dst_name,
330 fuse_reply_err(req, -ret);
333 static void bcachefs_fuse_link(fuse_req_t req, fuse_ino_t ino,
334 fuse_ino_t newparent_ino, const char *newname)
336 struct bch_fs *c = fuse_req_userdata(req);
337 struct bch_inode_unpacked dir_u, inode_u;
338 struct qstr qstr = QSTR(newname);
339 subvol_inum newparent = map_root_ino(newparent_ino);
340 subvol_inum inum = map_root_ino(ino);
343 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_link(%llu, %llu, %s)\n",
344 inum, newparent.inum, newname);
346 ret = bch2_trans_do(c, NULL, NULL, 0,
347 bch2_link_trans(&trans, newparent, &dir_u,
348 inum, &inode_u, &qstr));
351 struct fuse_entry_param e = inode_to_entry(c, &inode_u);
352 fuse_reply_entry(req, &e);
354 fuse_reply_err(req, -ret);
358 static void bcachefs_fuse_open(fuse_req_t req, fuse_ino_t inum,
359 struct fuse_file_info *fi)
361 fi->direct_io = false;
362 fi->keep_cache = true;
363 fi->cache_readdir = true;
365 fuse_reply_open(req, fi);
368 static void userbio_init(struct bio *bio, struct bio_vec *bv,
369 void *buf, size_t size)
371 bio_init(bio, NULL, bv, 1, 0);
372 bio->bi_iter.bi_size = size;
378 static int get_inode_io_opts(struct bch_fs *c, subvol_inum inum, struct bch_io_opts *opts)
380 struct bch_inode_unpacked inode;
381 if (bch2_inode_find_by_inum(c, inum, &inode))
384 bch2_inode_opts_get(opts, c, &inode);
388 static void bcachefs_fuse_read_endio(struct bio *bio)
390 closure_put(bio->bi_private);
393 struct fuse_align_io {
401 /* Handle unaligned start and end */
402 /* TODO: align to block_bytes, sector size, or page size? */
403 static struct fuse_align_io align_io(const struct bch_fs *c, size_t size,
406 struct fuse_align_io align;
410 align.start = round_down(offset, block_bytes(c));
411 align.pad_start = offset - align.start;
413 off_t end = offset + size;
414 align.end = round_up(end, block_bytes(c));
415 align.pad_end = align.end - end;
417 align.size = align.end - align.start;
423 * Given an aligned number of bytes transferred, figure out how many unaligned
424 * bytes were transferred.
426 static size_t align_fix_up_bytes(const struct fuse_align_io *align,
431 if (align_bytes > align->pad_start) {
432 bytes = align_bytes - align->pad_start;
433 bytes = bytes > align->pad_end ? bytes - align->pad_end : 0;
442 static int read_aligned(struct bch_fs *c, subvol_inum inum, size_t aligned_size,
443 off_t aligned_offset, void *buf)
445 BUG_ON(aligned_size & (block_bytes(c) - 1));
446 BUG_ON(aligned_offset & (block_bytes(c) - 1));
448 struct bch_io_opts io_opts;
449 if (get_inode_io_opts(c, inum, &io_opts))
452 struct bch_read_bio rbio;
454 userbio_init(&rbio.bio, &bv, buf, aligned_size);
455 bio_set_op_attrs(&rbio.bio, REQ_OP_READ, REQ_SYNC);
456 rbio.bio.bi_iter.bi_sector = aligned_offset >> 9;
459 closure_init_stack(&cl);
462 rbio.bio.bi_end_io = bcachefs_fuse_read_endio;
463 rbio.bio.bi_private = &cl;
465 bch2_read(c, rbio_init(&rbio.bio, io_opts), inum);
469 return -blk_status_to_errno(rbio.bio.bi_status);
472 static void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t ino,
473 size_t size, off_t offset,
474 struct fuse_file_info *fi)
476 subvol_inum inum = map_root_ino(ino);
477 struct bch_fs *c = fuse_req_userdata(req);
479 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_read(%llu, %zd, %lld)\n",
482 /* Check inode size. */
483 struct bch_inode_unpacked bi;
484 int ret = bch2_inode_find_by_inum(c, inum, &bi);
486 fuse_reply_err(req, -ret);
490 off_t end = min_t(u64, bi.bi_size, offset + size);
492 fuse_reply_buf(req, NULL, 0);
497 struct fuse_align_io align = align_io(c, size, offset);
499 void *buf = aligned_alloc(PAGE_SIZE, align.size);
501 fuse_reply_err(req, ENOMEM);
505 ret = read_aligned(c, inum, align.size, align.start, buf);
508 fuse_reply_buf(req, buf + align.pad_start, size);
510 fuse_reply_err(req, -ret);
515 static int inode_update_times(struct bch_fs *c, subvol_inum inum)
517 struct btree_trans trans;
518 struct btree_iter iter;
519 struct bch_inode_unpacked inode_u;
523 bch2_trans_init(&trans, c, 0, 0);
525 bch2_trans_begin(&trans);
526 now = bch2_current_time(c);
528 ret = bch2_inode_peek(&trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
532 inode_u.bi_mtime = now;
533 inode_u.bi_ctime = now;
535 ret = bch2_inode_write(&trans, &iter, &inode_u);
539 ret = bch2_trans_commit(&trans, NULL, NULL,
540 BTREE_INSERT_NOFAIL);
543 bch2_trans_iter_exit(&trans, &iter);
547 bch2_trans_exit(&trans);
551 static int write_aligned(struct bch_fs *c, subvol_inum inum,
552 struct bch_io_opts io_opts, void *buf,
553 size_t aligned_size, off_t aligned_offset,
554 off_t new_i_size, size_t *written_out)
556 struct bch_write_op op = { 0 };
560 BUG_ON(aligned_size & (block_bytes(c) - 1));
561 BUG_ON(aligned_offset & (block_bytes(c) - 1));
565 closure_init_stack(&cl);
567 bch2_write_op_init(&op, c, io_opts); /* XXX reads from op?! */
568 op.write_point = writepoint_hashed(0);
569 op.nr_replicas = io_opts.data_replicas;
570 op.target = io_opts.foreground_target;
571 op.subvol = inum.subvol;
572 op.pos = POS(inum.inum, aligned_offset >> 9);
573 op.new_i_size = new_i_size;
575 userbio_init(&op.wbio.bio, &bv, buf, aligned_size);
576 bio_set_op_attrs(&op.wbio.bio, REQ_OP_WRITE, REQ_SYNC);
578 if (bch2_disk_reservation_get(c, &op.res, aligned_size >> 9,
579 op.nr_replicas, 0)) {
580 /* XXX: use check_range_allocated like dio write path */
584 closure_call(&op.cl, bch2_write, NULL, &cl);
588 *written_out = op.written << 9;
593 static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t ino,
594 const char *buf, size_t size,
596 struct fuse_file_info *fi)
598 subvol_inum inum = map_root_ino(ino);
599 struct bch_fs *c = fuse_req_userdata(req);
600 struct bch_io_opts io_opts;
601 size_t aligned_written;
604 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write(%llu, %zd, %lld)\n",
607 struct fuse_align_io align = align_io(c, size, offset);
608 void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
609 BUG_ON(!aligned_buf);
611 if (get_inode_io_opts(c, inum, &io_opts)) {
616 /* Realign the data and read in start and end, if needed */
618 /* Read partial start data. */
619 if (align.pad_start) {
620 memset(aligned_buf, 0, block_bytes(c));
622 ret = read_aligned(c, inum, block_bytes(c), align.start,
629 * Read partial end data. If the whole write fits in one block, the
630 * start data and the end data are the same so this isn't needed.
633 !(align.pad_start && align.size == block_bytes(c))) {
634 off_t partial_end_start = align.end - block_bytes(c);
635 size_t buf_offset = align.size - block_bytes(c);
637 memset(aligned_buf + buf_offset, 0, block_bytes(c));
639 ret = read_aligned(c, inum, block_bytes(c), partial_end_start,
640 aligned_buf + buf_offset);
645 /* Overlay what we want to write. */
646 memcpy(aligned_buf + align.pad_start, buf, size);
648 /* Actually write. */
649 ret = write_aligned(c, inum, io_opts, aligned_buf,
650 align.size, align.start,
651 offset + size, &aligned_written);
653 /* Figure out how many unaligned bytes were written. */
654 size_t written = align_fix_up_bytes(&align, aligned_written);
655 BUG_ON(written > size);
657 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write: wrote %zd bytes\n",
664 * Update inode times.
665 * TODO: Integrate with bch2_extent_update()
668 ret = inode_update_times(c, inum);
671 BUG_ON(written == 0);
672 fuse_reply_write(req, written);
678 fuse_reply_err(req, -ret);
682 static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
683 fuse_ino_t dir_ino, const char *name)
685 subvol_inum dir = map_root_ino(dir_ino);
686 struct bch_fs *c = fuse_req_userdata(req);
687 struct bch_inode_unpacked new_inode;
688 size_t link_len = strlen(link);
691 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_symlink(%s, %llu, %s)\n",
692 link, dir.inum, name);
694 ret = do_create(c, dir, name, S_IFLNK|S_IRWXUGO, 0, &new_inode);
698 struct bch_io_opts io_opts;
699 ret = get_inode_io_opts(c, dir, &io_opts);
703 struct fuse_align_io align = align_io(c, link_len + 1, 0);
705 void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
706 BUG_ON(!aligned_buf);
708 memset(aligned_buf, 0, align.size);
709 memcpy(aligned_buf, link, link_len); /* already terminated */
711 subvol_inum inum = (subvol_inum) { dir.subvol, new_inode.bi_inum };
713 size_t aligned_written;
714 ret = write_aligned(c, inum, io_opts, aligned_buf,
715 align.size, align.start, link_len + 1,
722 size_t written = align_fix_up_bytes(&align, aligned_written);
723 BUG_ON(written != link_len + 1); // TODO: handle short
725 ret = inode_update_times(c, inum);
729 new_inode.bi_size = written;
731 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
732 fuse_reply_entry(req, &e);
736 fuse_reply_err(req, -ret);
739 static void bcachefs_fuse_readlink(fuse_req_t req, fuse_ino_t ino)
741 subvol_inum inum = map_root_ino(ino);
742 struct bch_fs *c = fuse_req_userdata(req);
745 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readlink(%llu)\n", inum.inum);
747 struct bch_inode_unpacked bi;
748 int ret = bch2_inode_find_by_inum(c, inum, &bi);
752 struct fuse_align_io align = align_io(c, bi.bi_size, 0);
755 buf = aligned_alloc(PAGE_SIZE, align.size);
759 ret = read_aligned(c, inum, align.size, align.start, buf);
763 BUG_ON(buf[align.size - 1] != 0);
765 fuse_reply_readlink(req, buf);
769 fuse_reply_err(req, -ret);
776 * FUSE flush is essentially the close() call, however it is not guaranteed
777 * that one flush happens per open/create.
779 * It doesn't have to do anything, and is mostly relevant for NFS-style
780 * filesystems where close has some relationship to caching.
782 static void bcachefs_fuse_flush(fuse_req_t req, fuse_ino_t inum,
783 struct fuse_file_info *fi)
785 struct bch_fs *c = fuse_req_userdata(req);
788 static void bcachefs_fuse_release(fuse_req_t req, fuse_ino_t inum,
789 struct fuse_file_info *fi)
791 struct bch_fs *c = fuse_req_userdata(req);
794 static void bcachefs_fuse_fsync(fuse_req_t req, fuse_ino_t inum, int datasync,
795 struct fuse_file_info *fi)
797 struct bch_fs *c = fuse_req_userdata(req);
800 static void bcachefs_fuse_opendir(fuse_req_t req, fuse_ino_t inum,
801 struct fuse_file_info *fi)
803 struct bch_fs *c = fuse_req_userdata(req);
807 struct fuse_dir_context {
808 struct dir_context ctx;
822 #define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name)
823 #define FUSE_DIRENT_ALIGN(x) \
824 (((x) + sizeof(uint64_t) - 1) & ~(sizeof(uint64_t) - 1))
826 static size_t fuse_add_direntry2(char *buf, size_t bufsize,
827 const char *name, int namelen,
828 const struct stat *stbuf, off_t off)
830 size_t entlen = FUSE_NAME_OFFSET + namelen;
831 size_t entlen_padded = FUSE_DIRENT_ALIGN(entlen);
832 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
834 if ((buf == NULL) || (entlen_padded > bufsize))
835 return entlen_padded;
837 dirent->ino = stbuf->st_ino;
839 dirent->namelen = namelen;
840 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
841 memcpy(dirent->name, name, namelen);
842 memset(dirent->name + namelen, 0, entlen_padded - entlen);
844 return entlen_padded;
847 static int fuse_filldir(struct dir_context *_ctx,
848 const char *name, int namelen,
849 loff_t pos, u64 ino, unsigned type)
851 struct fuse_dir_context *ctx =
852 container_of(_ctx, struct fuse_dir_context, ctx);
854 struct stat statbuf = {
855 .st_ino = unmap_root_ino(ino),
856 .st_mode = type << 12,
859 fuse_log(FUSE_LOG_DEBUG, "fuse_filldir(name=%s inum=%llu pos=%llu)\n",
860 name, statbuf.st_ino, pos);
862 size_t len = fuse_add_direntry2(ctx->buf,
869 if (len > ctx->bufsize)
878 static bool handle_dots(struct fuse_dir_context *ctx, fuse_ino_t dir)
880 if (ctx->ctx.pos == 0) {
881 if (fuse_filldir(&ctx->ctx, ".", 1, ctx->ctx.pos,
887 if (ctx->ctx.pos == 1) {
888 if (fuse_filldir(&ctx->ctx, "..", 2, ctx->ctx.pos,
889 /*TODO: parent*/ 1, DT_DIR) < 0)
897 static void bcachefs_fuse_readdir(fuse_req_t req, fuse_ino_t dir_ino,
898 size_t size, off_t off,
899 struct fuse_file_info *fi)
901 subvol_inum dir = map_root_ino(dir_ino);
902 struct bch_fs *c = fuse_req_userdata(req);
903 struct bch_inode_unpacked bi;
904 char *buf = calloc(size, 1);
905 struct fuse_dir_context ctx = {
906 .ctx.actor = fuse_filldir,
914 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir(dir=%llu, size=%zu, "
915 "off=%lld)\n", dir.inum, size, off);
917 ret = bch2_inode_find_by_inum(c, dir, &bi);
921 if (!S_ISDIR(bi.bi_mode)) {
926 if (!handle_dots(&ctx, dir.inum))
929 ret = bch2_readdir(c, dir, &ctx.ctx);
932 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir reply %zd\n",
934 fuse_reply_buf(req, buf, ctx.buf - buf);
936 fuse_reply_err(req, -ret);
943 static void bcachefs_fuse_readdirplus(fuse_req_t req, fuse_ino_t dir,
944 size_t size, off_t off,
945 struct fuse_file_info *fi)
950 static void bcachefs_fuse_releasedir(fuse_req_t req, fuse_ino_t inum,
951 struct fuse_file_info *fi)
953 struct bch_fs *c = fuse_req_userdata(req);
956 static void bcachefs_fuse_fsyncdir(fuse_req_t req, fuse_ino_t inum, int datasync,
957 struct fuse_file_info *fi)
959 struct bch_fs *c = fuse_req_userdata(req);
963 static void bcachefs_fuse_statfs(fuse_req_t req, fuse_ino_t inum)
965 struct bch_fs *c = fuse_req_userdata(req);
966 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
967 unsigned shift = c->block_bits;
968 struct statvfs statbuf = {
969 .f_bsize = block_bytes(c),
970 .f_frsize = block_bytes(c),
971 .f_blocks = usage.capacity >> shift,
972 .f_bfree = (usage.capacity - usage.used) >> shift,
973 //.f_bavail = statbuf.f_bfree,
974 .f_files = usage.nr_inodes,
976 .f_namemax = BCH_NAME_MAX,
979 fuse_reply_statfs(req, &statbuf);
983 static void bcachefs_fuse_setxattr(fuse_req_t req, fuse_ino_t inum,
984 const char *name, const char *value,
985 size_t size, int flags)
987 struct bch_fs *c = fuse_req_userdata(req);
990 static void bcachefs_fuse_getxattr(fuse_req_t req, fuse_ino_t inum,
991 const char *name, size_t size)
993 struct bch_fs *c = fuse_req_userdata(req);
995 fuse_reply_xattr(req, );
998 static void bcachefs_fuse_listxattr(fuse_req_t req, fuse_ino_t inum, size_t size)
1000 struct bch_fs *c = fuse_req_userdata(req);
1003 static void bcachefs_fuse_removexattr(fuse_req_t req, fuse_ino_t inum,
1006 struct bch_fs *c = fuse_req_userdata(req);
1010 static void bcachefs_fuse_create(fuse_req_t req, fuse_ino_t dir_ino,
1011 const char *name, mode_t mode,
1012 struct fuse_file_info *fi)
1014 subvol_inum dir = map_root_ino(dir_ino);
1015 struct bch_fs *c = fuse_req_userdata(req);
1016 struct bch_inode_unpacked new_inode;
1019 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_create(%llu, %s, %x)\n",
1020 dir.inum, name, mode);
1022 ret = do_create(c, dir, name, mode, 0, &new_inode);
1026 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
1027 fuse_reply_create(req, &e, fi);
1030 fuse_reply_err(req, -ret);
1034 static void bcachefs_fuse_write_buf(fuse_req_t req, fuse_ino_t inum,
1035 struct fuse_bufvec *bufv, off_t off,
1036 struct fuse_file_info *fi)
1038 struct bch_fs *c = fuse_req_userdata(req);
1041 static void bcachefs_fuse_fallocate(fuse_req_t req, fuse_ino_t inum, int mode,
1042 off_t offset, off_t length,
1043 struct fuse_file_info *fi)
1045 struct bch_fs *c = fuse_req_userdata(req);
1049 static const struct fuse_lowlevel_ops bcachefs_fuse_ops = {
1050 .init = bcachefs_fuse_init,
1051 .destroy = bcachefs_fuse_destroy,
1052 .lookup = bcachefs_fuse_lookup,
1053 .getattr = bcachefs_fuse_getattr,
1054 .setattr = bcachefs_fuse_setattr,
1055 .readlink = bcachefs_fuse_readlink,
1056 .mknod = bcachefs_fuse_mknod,
1057 .mkdir = bcachefs_fuse_mkdir,
1058 .unlink = bcachefs_fuse_unlink,
1059 .rmdir = bcachefs_fuse_rmdir,
1060 .symlink = bcachefs_fuse_symlink,
1061 .rename = bcachefs_fuse_rename,
1062 .link = bcachefs_fuse_link,
1063 .open = bcachefs_fuse_open,
1064 .read = bcachefs_fuse_read,
1065 .write = bcachefs_fuse_write,
1066 //.flush = bcachefs_fuse_flush,
1067 //.release = bcachefs_fuse_release,
1068 //.fsync = bcachefs_fuse_fsync,
1069 //.opendir = bcachefs_fuse_opendir,
1070 .readdir = bcachefs_fuse_readdir,
1071 //.readdirplus = bcachefs_fuse_readdirplus,
1072 //.releasedir = bcachefs_fuse_releasedir,
1073 //.fsyncdir = bcachefs_fuse_fsyncdir,
1074 .statfs = bcachefs_fuse_statfs,
1075 //.setxattr = bcachefs_fuse_setxattr,
1076 //.getxattr = bcachefs_fuse_getxattr,
1077 //.listxattr = bcachefs_fuse_listxattr,
1078 //.removexattr = bcachefs_fuse_removexattr,
1079 .create = bcachefs_fuse_create,
1083 .getlk = bcachefs_fuse_getlk,
1084 .setlk = bcachefs_fuse_setlk,
1086 //.write_buf = bcachefs_fuse_write_buf,
1087 //.fallocate = bcachefs_fuse_fallocate,
1092 * Setup and command parsing.
1101 static void bf_context_free(struct bf_context *ctx)
1105 free(ctx->devices_str);
1106 for (i = 0; i < ctx->nr_devices; ++i)
1107 free(ctx->devices[i]);
1111 static struct fuse_opt bf_opts[] = {
1116 * Fuse option parsing helper -- returning 0 means we consumed the argument, 1
1119 static int bf_opt_proc(void *data, const char *arg, int key,
1120 struct fuse_args *outargs)
1122 struct bf_context *ctx = data;
1125 case FUSE_OPT_KEY_NONOPT:
1126 /* Just extract the first non-option string. */
1127 if (!ctx->devices_str) {
1128 ctx->devices_str = strdup(arg);
1138 * dev1:dev2 -> [ dev1, dev2 ]
1141 static void tokenize_devices(struct bf_context *ctx)
1143 char *devices_str = strdup(ctx->devices_str);
1144 char *devices_tmp = devices_str;
1145 char **devices = NULL;
1149 while ((dev = strsep(&devices_tmp, ":"))) {
1150 if (strlen(dev) > 0) {
1151 devices = realloc(devices, (nr + 1) * sizeof *devices);
1152 devices[nr] = strdup(dev);
1158 devices = malloc(sizeof *devices);
1159 devices[0] = strdup(ctx->devices_str);
1163 ctx->devices = devices;
1164 ctx->nr_devices = nr;
1169 static void usage(char *argv[])
1171 printf("Usage: %s fusemount [options] <dev>[:dev2:...] <mountpoint>\n",
1176 int cmd_fusemount(int argc, char *argv[])
1178 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
1179 struct bch_opts bch_opts = bch2_opts_empty();
1180 struct bf_context ctx = { 0 };
1181 struct bch_fs *c = NULL;
1184 /* Parse arguments. */
1185 if (fuse_opt_parse(&args, &ctx, bf_opts, bf_opt_proc) < 0)
1186 die("fuse_opt_parse err: %m");
1188 struct fuse_cmdline_opts fuse_opts;
1189 if (fuse_parse_cmdline(&args, &fuse_opts) < 0)
1190 die("fuse_parse_cmdline err: %m");
1192 if (fuse_opts.show_help) {
1194 fuse_cmdline_help();
1195 fuse_lowlevel_help();
1199 if (fuse_opts.show_version) {
1200 /* TODO: Show bcachefs version. */
1201 printf("FUSE library version %s\n", fuse_pkgversion());
1202 fuse_lowlevel_version();
1206 if (!fuse_opts.mountpoint) {
1208 printf("Please supply a mountpoint.\n");
1212 if (!ctx.devices_str) {
1214 printf("Please specify a device or device1:device2:...\n");
1218 tokenize_devices(&ctx);
1220 struct printbuf fsname = PRINTBUF;
1221 prt_printf(&fsname, "fsname=");
1222 for (i = 0; i < ctx.nr_devices; ++i) {
1224 prt_str(&fsname, ":");
1225 prt_str(&fsname, ctx.devices[i]);
1228 fuse_opt_add_arg(&args, "-o");
1229 fuse_opt_add_arg(&args, fsname.buf);
1232 printf("Opening bcachefs filesystem on:\n");
1233 for (i = 0; i < ctx.nr_devices; ++i)
1234 printf("\t%s\n", ctx.devices[i]);
1236 c = bch2_fs_open(ctx.devices, ctx.nr_devices, bch_opts);
1238 die("error opening %s: %s", ctx.devices_str,
1239 bch2_err_str(PTR_ERR(c)));
1242 struct fuse_session *se =
1243 fuse_session_new(&args, &bcachefs_fuse_ops,
1244 sizeof(bcachefs_fuse_ops), c);
1246 die("fuse_lowlevel_new err: %m");
1248 if (fuse_set_signal_handlers(se) < 0)
1249 die("fuse_set_signal_handlers err: %m");
1251 if (fuse_session_mount(se, fuse_opts.mountpoint))
1252 die("fuse_mount err: %m");
1254 /* This print statement is a trigger for tests. */
1255 printf("Fuse mount initialized.\n");
1257 fuse_daemonize(fuse_opts.foreground);
1259 ret = fuse_session_loop(se);
1262 fuse_session_unmount(se);
1263 fuse_remove_signal_handlers(se);
1264 fuse_session_destroy(se);
1267 free(fuse_opts.mountpoint);
1268 fuse_opt_free_args(&args);
1269 bf_context_free(&ctx);
1274 #endif /* BCACHEFS_FUSE */