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 = unmap_root_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) {
66 .ino = unmap_root_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,
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 fuse_log(FUSE_LOG_DEBUG, "fuse_lookup(dir=%llu name=%s)\n",
104 dir = map_root_ino(dir);
106 ret = bch2_inode_find_by_inum(c, dir, &bi);
108 fuse_reply_err(req, -ret);
112 struct bch_hash_info hash_info = bch2_hash_info_init(c, &bi);
114 inum = bch2_dirent_lookup(c, dir, &hash_info, &qstr);
116 struct fuse_entry_param e = {
117 .attr_timeout = DBL_MAX,
118 .entry_timeout = DBL_MAX,
120 fuse_reply_entry(req, &e);
124 ret = bch2_inode_find_by_inum(c, inum, &bi);
128 fuse_log(FUSE_LOG_DEBUG, "fuse_lookup ret(inum=%llu)\n",
131 struct fuse_entry_param e = inode_to_entry(c, &bi);
132 fuse_reply_entry(req, &e);
135 fuse_log(FUSE_LOG_DEBUG, "fuse_lookup error %i\n", ret);
136 fuse_reply_err(req, -ret);
139 static void bcachefs_fuse_getattr(fuse_req_t req, fuse_ino_t inum,
140 struct fuse_file_info *fi)
142 struct bch_fs *c = fuse_req_userdata(req);
143 struct bch_inode_unpacked bi;
147 fuse_log(FUSE_LOG_DEBUG, "fuse_getattr(inum=%llu)\n",
150 inum = map_root_ino(inum);
152 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 inum,
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 inum = map_root_ino(inum);
178 bch2_trans_init(&trans, c, 0, 0);
180 bch2_trans_begin(&trans);
181 now = bch2_current_time(c);
183 iter = bch2_inode_peek(&trans, &inode_u, inum, BTREE_ITER_INTENT);
184 ret = PTR_ERR_OR_ZERO(iter);
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,
209 BTREE_INSERT_NOFAIL);
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, u64 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;
231 dir = map_root_ino(dir);
233 bch2_inode_init_early(c, new_inode);
235 return bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
236 bch2_create_trans(&trans,
239 0, 0, mode, rdev, NULL, NULL));
242 static void bcachefs_fuse_mknod(fuse_req_t req, fuse_ino_t dir,
243 const char *name, mode_t mode,
246 struct bch_fs *c = fuse_req_userdata(req);
247 struct bch_inode_unpacked new_inode;
250 ret = do_create(c, dir, name, mode, rdev, &new_inode);
254 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
255 fuse_reply_entry(req, &e);
258 fuse_reply_err(req, -ret);
261 static void bcachefs_fuse_mkdir(fuse_req_t req, fuse_ino_t dir,
262 const char *name, mode_t mode)
264 BUG_ON(mode & S_IFMT);
267 bcachefs_fuse_mknod(req, dir, name, mode, 0);
270 static void bcachefs_fuse_unlink(fuse_req_t req, fuse_ino_t dir,
273 struct bch_fs *c = fuse_req_userdata(req);
274 struct bch_inode_unpacked dir_u, inode_u;
275 struct qstr qstr = QSTR(name);
278 dir = map_root_ino(dir);
280 ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC|BTREE_INSERT_NOFAIL,
281 bch2_unlink_trans(&trans, dir, &dir_u,
284 fuse_reply_err(req, -ret);
287 static void bcachefs_fuse_rmdir(fuse_req_t req, fuse_ino_t dir,
290 dir = map_root_ino(dir);
292 bcachefs_fuse_unlink(req, dir, name);
295 static void bcachefs_fuse_rename(fuse_req_t req,
296 fuse_ino_t src_dir, const char *srcname,
297 fuse_ino_t dst_dir, const char *dstname,
300 struct bch_fs *c = fuse_req_userdata(req);
301 struct bch_inode_unpacked dst_dir_u, src_dir_u;
302 struct bch_inode_unpacked src_inode_u, dst_inode_u;
303 struct qstr dst_name = QSTR(srcname);
304 struct qstr src_name = QSTR(dstname);
307 src_dir = map_root_ino(src_dir);
308 dst_dir = map_root_ino(dst_dir);
310 /* XXX handle overwrites */
311 ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
312 bch2_rename_trans(&trans,
315 &src_inode_u, &dst_inode_u,
316 &src_name, &dst_name,
319 fuse_reply_err(req, -ret);
322 static void bcachefs_fuse_link(fuse_req_t req, fuse_ino_t inum,
323 fuse_ino_t newparent, const char *newname)
325 struct bch_fs *c = fuse_req_userdata(req);
326 struct bch_inode_unpacked inode_u;
327 struct qstr qstr = QSTR(newname);
330 newparent = map_root_ino(newparent);
332 ret = bch2_trans_do(c, NULL, BTREE_INSERT_ATOMIC,
333 bch2_link_trans(&trans, newparent,
334 inum, &inode_u, &qstr));
337 struct fuse_entry_param e = inode_to_entry(c, &inode_u);
338 fuse_reply_entry(req, &e);
340 fuse_reply_err(req, -ret);
344 static void bcachefs_fuse_open(fuse_req_t req, fuse_ino_t inum,
345 struct fuse_file_info *fi)
347 fi->direct_io = false;
348 fi->keep_cache = true;
349 fi->cache_readdir = true;
351 fuse_reply_open(req, fi);
354 static void userbio_init(struct bio *bio, struct bio_vec *bv,
355 void *buf, size_t size)
357 bio_init(bio, bv, 1);
358 bio->bi_iter.bi_size = size;
364 static int get_inode_io_opts(struct bch_fs *c, u64 inum,
365 struct bch_io_opts *opts)
367 struct bch_inode_unpacked inode;
368 if (bch2_inode_find_by_inum(c, inum, &inode))
371 *opts = bch2_opts_to_inode_opts(c->opts);
372 bch2_io_opts_apply(opts, bch2_inode_opts_get(&inode));
376 static void bcachefs_fuse_read_endio(struct bio *bio)
378 closure_put(bio->bi_private);
381 struct fuse_align_io {
389 /* Handle unaligned start and end */
390 /* TODO: align to block_bytes, sector size, or page size? */
391 static struct fuse_align_io align_io(const struct bch_fs *c, size_t size,
394 struct fuse_align_io align;
398 align.start = round_down(offset, block_bytes(c));
399 align.pad_start = offset - align.start;
401 off_t end = offset + size;
402 align.end = round_up(end, block_bytes(c));
403 align.pad_end = align.end - end;
405 align.size = align.end - align.start;
411 * Given an aligned number of bytes transferred, figure out how many unaligned
412 * bytes were transferred.
414 static size_t align_fix_up_bytes(const struct fuse_align_io *align,
419 if (align_bytes > align->pad_start) {
420 bytes = align_bytes - align->pad_start;
421 bytes = bytes > align->pad_end ? bytes - align->pad_end : 0;
430 static int read_aligned(struct bch_fs *c, fuse_ino_t inum, size_t aligned_size,
431 off_t aligned_offset, void *buf)
433 BUG_ON(aligned_size & (block_bytes(c) - 1));
434 BUG_ON(aligned_offset & (block_bytes(c) - 1));
436 struct bch_io_opts io_opts;
437 if (get_inode_io_opts(c, inum, &io_opts))
440 struct bch_read_bio rbio;
442 userbio_init(&rbio.bio, &bv, buf, aligned_size);
443 bio_set_op_attrs(&rbio.bio, REQ_OP_READ, REQ_SYNC);
444 rbio.bio.bi_iter.bi_sector = aligned_offset >> 9;
447 closure_init_stack(&cl);
450 rbio.bio.bi_end_io = bcachefs_fuse_read_endio;
451 rbio.bio.bi_private = &cl;
453 bch2_read(c, rbio_init(&rbio.bio, io_opts), inum);
457 return -blk_status_to_errno(rbio.bio.bi_status);
460 static void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t inum,
461 size_t size, off_t offset,
462 struct fuse_file_info *fi)
464 struct bch_fs *c = fuse_req_userdata(req);
466 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_read(%llu, %zd, %lld)\n",
469 /* Check inode size. */
470 struct bch_inode_unpacked bi;
471 int ret = bch2_inode_find_by_inum(c, inum, &bi);
473 fuse_reply_err(req, -ret);
477 off_t end = min_t(u64, bi.bi_size, offset + size);
479 fuse_reply_buf(req, NULL, 0);
484 struct fuse_align_io align = align_io(c, size, offset);
486 void *buf = aligned_alloc(PAGE_SIZE, align.size);
488 fuse_reply_err(req, ENOMEM);
492 ret = read_aligned(c, inum, align.size, align.start, buf);
495 fuse_reply_buf(req, buf + align.pad_start, size);
497 fuse_reply_err(req, -ret);
502 static int inode_update_times(struct bch_fs *c, fuse_ino_t inum)
504 struct btree_trans trans;
505 struct btree_iter *iter;
506 struct bch_inode_unpacked inode_u;
510 bch2_trans_init(&trans, c, 0, 0);
512 bch2_trans_begin(&trans);
513 now = bch2_current_time(c);
515 iter = bch2_inode_peek(&trans, &inode_u, inum, BTREE_ITER_INTENT);
516 ret = PTR_ERR_OR_ZERO(iter);
520 inode_u.bi_mtime = now;
521 inode_u.bi_ctime = now;
523 ret = bch2_inode_write(&trans, iter, &inode_u);
527 ret = bch2_trans_commit(&trans, NULL, NULL,
528 BTREE_INSERT_ATOMIC|BTREE_INSERT_NOFAIL);
534 bch2_trans_exit(&trans);
538 static int write_aligned(struct bch_fs *c, fuse_ino_t inum,
539 struct bch_io_opts io_opts, void *buf,
540 size_t aligned_size, off_t aligned_offset,
541 off_t new_i_size, size_t *written_out)
543 struct bch_write_op op = { 0 };
547 BUG_ON(aligned_size & (block_bytes(c) - 1));
548 BUG_ON(aligned_offset & (block_bytes(c) - 1));
552 closure_init_stack(&cl);
554 bch2_write_op_init(&op, c, io_opts); /* XXX reads from op?! */
555 op.write_point = writepoint_hashed(0);
556 op.nr_replicas = io_opts.data_replicas;
557 op.target = io_opts.foreground_target;
558 op.pos = POS(inum, aligned_offset >> 9);
559 op.new_i_size = new_i_size;
561 userbio_init(&op.wbio.bio, &bv, buf, aligned_size);
562 bio_set_op_attrs(&op.wbio.bio, REQ_OP_WRITE, REQ_SYNC);
564 if (bch2_disk_reservation_get(c, &op.res, aligned_size >> 9,
565 op.nr_replicas, 0)) {
566 /* XXX: use check_range_allocated like dio write path */
570 closure_call(&op.cl, bch2_write, NULL, &cl);
574 *written_out = op.written << 9;
579 static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t inum,
580 const char *buf, size_t size,
582 struct fuse_file_info *fi)
584 struct bch_fs *c = fuse_req_userdata(req);
585 struct bch_io_opts io_opts;
586 size_t aligned_written;
589 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write(%llu, %zd, %lld)\n",
592 struct fuse_align_io align = align_io(c, size, offset);
594 if (get_inode_io_opts(c, inum, &io_opts)) {
599 /* Realign the data and read in start and end, if needed */
600 void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
602 /* Read partial start data. */
603 if (align.pad_start) {
604 memset(aligned_buf, 0, block_bytes(c));
606 ret = read_aligned(c, inum, block_bytes(c), align.start,
613 * Read partial end data. If the whole write fits in one block, the
614 * start data and the end data are the same so this isn't needed.
617 !(align.pad_start && align.size == block_bytes(c))) {
618 off_t partial_end_start = align.end - block_bytes(c);
619 size_t buf_offset = align.size - block_bytes(c);
621 memset(aligned_buf + buf_offset, 0, block_bytes(c));
623 ret = read_aligned(c, inum, block_bytes(c), partial_end_start,
624 aligned_buf + buf_offset);
629 /* Overlay what we want to write. */
630 memcpy(aligned_buf + align.pad_start, buf, size);
632 /* Actually write. */
633 ret = write_aligned(c, inum, io_opts, aligned_buf,
634 align.size, align.start,
635 offset + size, &aligned_written);
637 /* Figure out how many unaligned bytes were written. */
638 size_t written = align_fix_up_bytes(&align, aligned_written);
639 BUG_ON(written > size);
641 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write: wrote %zd bytes\n",
648 * Update inode times.
649 * TODO: Integrate with bch2_extent_update()
652 ret = inode_update_times(c, inum);
655 BUG_ON(written == 0);
656 fuse_reply_write(req, written);
661 fuse_reply_err(req, -ret);
664 static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
665 fuse_ino_t dir, const char *name)
667 struct bch_fs *c = fuse_req_userdata(req);
668 struct bch_inode_unpacked new_inode;
669 size_t link_len = strlen(link);
672 dir = map_root_ino(dir);
674 ret = do_create(c, dir, name, S_IFLNK|S_IRWXUGO, 0, &new_inode);
678 struct bch_io_opts io_opts;
679 ret = get_inode_io_opts(c, new_inode.bi_inum, &io_opts);
683 struct fuse_align_io align = align_io(c, link_len + 1, 0);
685 void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
686 memset(aligned_buf, 0, align.size);
687 memcpy(aligned_buf, link, link_len); /* already terminated */
689 size_t aligned_written;
690 ret = write_aligned(c, new_inode.bi_inum, io_opts, aligned_buf,
691 align.size, align.start, link_len + 1,
698 size_t written = align_fix_up_bytes(&align, aligned_written);
699 BUG_ON(written != link_len + 1); // TODO: handle short
701 ret = inode_update_times(c, new_inode.bi_inum);
705 new_inode.bi_size = written;
707 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
708 fuse_reply_entry(req, &e);
712 fuse_reply_err(req, -ret);
715 static void bcachefs_fuse_readlink(fuse_req_t req, fuse_ino_t inum)
717 struct bch_fs *c = fuse_req_userdata(req);
720 struct bch_inode_unpacked bi;
721 int ret = bch2_inode_find_by_inum(c, inum, &bi);
725 struct fuse_align_io align = align_io(c, bi.bi_size, 0);
728 buf = aligned_alloc(PAGE_SIZE, align.size);
732 ret = read_aligned(c, inum, align.size, align.start, buf);
736 BUG_ON(buf[align.size - 1] != 0);
738 fuse_reply_readlink(req, buf);
742 fuse_reply_err(req, -ret);
749 * FUSE flush is essentially the close() call, however it is not guaranteed
750 * that one flush happens per open/create.
752 * It doesn't have to do anything, and is mostly relevant for NFS-style
753 * filesystems where close has some relationship to caching.
755 static void bcachefs_fuse_flush(fuse_req_t req, fuse_ino_t inum,
756 struct fuse_file_info *fi)
758 struct bch_fs *c = fuse_req_userdata(req);
761 static void bcachefs_fuse_release(fuse_req_t req, fuse_ino_t inum,
762 struct fuse_file_info *fi)
764 struct bch_fs *c = fuse_req_userdata(req);
767 static void bcachefs_fuse_fsync(fuse_req_t req, fuse_ino_t inum, int datasync,
768 struct fuse_file_info *fi)
770 struct bch_fs *c = fuse_req_userdata(req);
773 static void bcachefs_fuse_opendir(fuse_req_t req, fuse_ino_t inum,
774 struct fuse_file_info *fi)
776 struct bch_fs *c = fuse_req_userdata(req);
780 struct fuse_dir_context {
781 struct dir_context ctx;
795 #define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name)
796 #define FUSE_DIRENT_ALIGN(x) \
797 (((x) + sizeof(uint64_t) - 1) & ~(sizeof(uint64_t) - 1))
799 static size_t fuse_add_direntry2(char *buf, size_t bufsize,
800 const char *name, int namelen,
801 const struct stat *stbuf, off_t off)
803 size_t entlen = FUSE_NAME_OFFSET + namelen;
804 size_t entlen_padded = FUSE_DIRENT_ALIGN(entlen);
805 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
807 if ((buf == NULL) || (entlen_padded > bufsize))
808 return entlen_padded;
810 dirent->ino = stbuf->st_ino;
812 dirent->namelen = namelen;
813 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
814 memcpy(dirent->name, name, namelen);
815 memset(dirent->name + namelen, 0, entlen_padded - entlen);
817 return entlen_padded;
820 static int fuse_filldir(struct dir_context *_ctx,
821 const char *name, int namelen,
822 loff_t pos, u64 ino, unsigned type)
824 struct fuse_dir_context *ctx =
825 container_of(_ctx, struct fuse_dir_context, ctx);
827 struct stat statbuf = {
828 .st_ino = unmap_root_ino(ino),
829 .st_mode = type << 12,
832 fuse_log(FUSE_LOG_DEBUG, "fuse_filldir(name=%s inum=%llu pos=%llu)\n",
833 name, statbuf.st_ino, pos);
835 size_t len = fuse_add_direntry2(ctx->buf,
842 if (len > ctx->bufsize)
850 static bool handle_dots(struct fuse_dir_context *ctx, fuse_ino_t dir)
852 if (ctx->ctx.pos == 0) {
853 if (fuse_filldir(&ctx->ctx, ".", 1, ctx->ctx.pos,
859 if (ctx->ctx.pos == 1) {
860 if (fuse_filldir(&ctx->ctx, "..", 2, ctx->ctx.pos,
861 /*TODO: parent*/ 1, DT_DIR) < 0)
869 static void bcachefs_fuse_readdir(fuse_req_t req, fuse_ino_t dir,
870 size_t size, off_t off,
871 struct fuse_file_info *fi)
873 struct bch_fs *c = fuse_req_userdata(req);
874 struct bch_inode_unpacked bi;
875 char *buf = calloc(size, 1);
876 struct fuse_dir_context ctx = {
877 .ctx.actor = fuse_filldir,
885 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir(dir=%llu, size=%zu, "
886 "off=%lld)\n", dir, size, off);
888 dir = map_root_ino(dir);
890 ret = bch2_inode_find_by_inum(c, dir, &bi);
894 if (!S_ISDIR(bi.bi_mode)) {
899 if (!handle_dots(&ctx, dir))
902 ret = bch2_readdir(c, dir, &ctx.ctx);
905 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir reply %zd\n",
907 fuse_reply_buf(req, buf, ctx.buf - buf);
909 fuse_reply_err(req, -ret);
916 static void bcachefs_fuse_readdirplus(fuse_req_t req, fuse_ino_t dir,
917 size_t size, off_t off,
918 struct fuse_file_info *fi)
923 static void bcachefs_fuse_releasedir(fuse_req_t req, fuse_ino_t inum,
924 struct fuse_file_info *fi)
926 struct bch_fs *c = fuse_req_userdata(req);
929 static void bcachefs_fuse_fsyncdir(fuse_req_t req, fuse_ino_t inum, int datasync,
930 struct fuse_file_info *fi)
932 struct bch_fs *c = fuse_req_userdata(req);
936 static void bcachefs_fuse_statfs(fuse_req_t req, fuse_ino_t inum)
938 struct bch_fs *c = fuse_req_userdata(req);
939 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
940 unsigned shift = c->block_bits;
941 struct statvfs statbuf = {
942 .f_bsize = block_bytes(c),
943 .f_frsize = block_bytes(c),
944 .f_blocks = usage.capacity >> shift,
945 .f_bfree = (usage.capacity - usage.used) >> shift,
946 //.f_bavail = statbuf.f_bfree,
947 .f_files = usage.nr_inodes,
949 .f_namemax = BCH_NAME_MAX,
952 fuse_reply_statfs(req, &statbuf);
956 static void bcachefs_fuse_setxattr(fuse_req_t req, fuse_ino_t inum,
957 const char *name, const char *value,
958 size_t size, int flags)
960 struct bch_fs *c = fuse_req_userdata(req);
963 static void bcachefs_fuse_getxattr(fuse_req_t req, fuse_ino_t inum,
964 const char *name, size_t size)
966 struct bch_fs *c = fuse_req_userdata(req);
968 fuse_reply_xattr(req, );
971 static void bcachefs_fuse_listxattr(fuse_req_t req, fuse_ino_t inum, size_t size)
973 struct bch_fs *c = fuse_req_userdata(req);
976 static void bcachefs_fuse_removexattr(fuse_req_t req, fuse_ino_t inum,
979 struct bch_fs *c = fuse_req_userdata(req);
983 static void bcachefs_fuse_create(fuse_req_t req, fuse_ino_t dir,
984 const char *name, mode_t mode,
985 struct fuse_file_info *fi)
987 struct bch_fs *c = fuse_req_userdata(req);
988 struct bch_inode_unpacked new_inode;
991 ret = do_create(c, dir, name, mode, 0, &new_inode);
995 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
996 fuse_reply_create(req, &e, fi);
999 fuse_reply_err(req, -ret);
1004 static void bcachefs_fuse_write_buf(fuse_req_t req, fuse_ino_t inum,
1005 struct fuse_bufvec *bufv, off_t off,
1006 struct fuse_file_info *fi)
1008 struct bch_fs *c = fuse_req_userdata(req);
1011 static void bcachefs_fuse_fallocate(fuse_req_t req, fuse_ino_t inum, int mode,
1012 off_t offset, off_t length,
1013 struct fuse_file_info *fi)
1015 struct bch_fs *c = fuse_req_userdata(req);
1019 static const struct fuse_lowlevel_ops bcachefs_fuse_ops = {
1020 .init = bcachefs_fuse_init,
1021 .destroy = bcachefs_fuse_destroy,
1022 .lookup = bcachefs_fuse_lookup,
1023 .getattr = bcachefs_fuse_getattr,
1024 .setattr = bcachefs_fuse_setattr,
1025 .readlink = bcachefs_fuse_readlink,
1026 .mknod = bcachefs_fuse_mknod,
1027 .mkdir = bcachefs_fuse_mkdir,
1028 .unlink = bcachefs_fuse_unlink,
1029 .rmdir = bcachefs_fuse_rmdir,
1030 .symlink = bcachefs_fuse_symlink,
1031 .rename = bcachefs_fuse_rename,
1032 .link = bcachefs_fuse_link,
1033 .open = bcachefs_fuse_open,
1034 .read = bcachefs_fuse_read,
1035 .write = bcachefs_fuse_write,
1036 //.flush = bcachefs_fuse_flush,
1037 //.release = bcachefs_fuse_release,
1038 //.fsync = bcachefs_fuse_fsync,
1039 //.opendir = bcachefs_fuse_opendir,
1040 .readdir = bcachefs_fuse_readdir,
1041 //.readdirplus = bcachefs_fuse_readdirplus,
1042 //.releasedir = bcachefs_fuse_releasedir,
1043 //.fsyncdir = bcachefs_fuse_fsyncdir,
1044 .statfs = bcachefs_fuse_statfs,
1045 //.setxattr = bcachefs_fuse_setxattr,
1046 //.getxattr = bcachefs_fuse_getxattr,
1047 //.listxattr = bcachefs_fuse_listxattr,
1048 //.removexattr = bcachefs_fuse_removexattr,
1049 .create = bcachefs_fuse_create,
1053 .getlk = bcachefs_fuse_getlk,
1054 .setlk = bcachefs_fuse_setlk,
1056 //.write_buf = bcachefs_fuse_write_buf,
1057 //.fallocate = bcachefs_fuse_fallocate,
1062 * Setup and command parsing.
1071 static void bf_context_free(struct bf_context *ctx)
1075 free(ctx->devices_str);
1076 for (i = 0; i < ctx->nr_devices; ++i)
1077 free(ctx->devices[i]);
1081 static struct fuse_opt bf_opts[] = {
1086 * Fuse option parsing helper -- returning 0 means we consumed the argument, 1
1089 static int bf_opt_proc(void *data, const char *arg, int key,
1090 struct fuse_args *outargs)
1092 struct bf_context *ctx = data;
1095 case FUSE_OPT_KEY_NONOPT:
1096 /* Just extract the first non-option string. */
1097 if (!ctx->devices_str) {
1098 ctx->devices_str = strdup(arg);
1108 * dev1:dev2 -> [ dev1, dev2 ]
1111 static void tokenize_devices(struct bf_context *ctx)
1113 char *devices_str = strdup(ctx->devices_str);
1114 char *devices_tmp = devices_str;
1115 char **devices = NULL;
1119 while ((dev = strsep(&devices_tmp, ":"))) {
1120 if (strlen(dev) > 0) {
1121 devices = realloc(devices, (nr + 1) * sizeof *devices);
1122 devices[nr] = strdup(dev);
1128 devices = malloc(sizeof *devices);
1129 devices[0] = strdup(ctx->devices_str);
1133 ctx->devices = devices;
1134 ctx->nr_devices = nr;
1139 static void usage(char *argv[])
1141 printf("Usage: %s fusemount [options] <dev>[:dev2:...] <mountpoint>\n",
1146 int cmd_fusemount(int argc, char *argv[])
1148 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
1149 struct bch_opts bch_opts = bch2_opts_empty();
1150 struct bf_context ctx = { 0 };
1151 struct bch_fs *c = NULL;
1154 /* Parse arguments. */
1155 if (fuse_opt_parse(&args, &ctx, bf_opts, bf_opt_proc) < 0)
1156 die("fuse_opt_parse err: %m");
1158 struct fuse_cmdline_opts fuse_opts;
1159 if (fuse_parse_cmdline(&args, &fuse_opts) < 0)
1160 die("fuse_parse_cmdline err: %m");
1162 if (fuse_opts.show_help) {
1164 fuse_cmdline_help();
1165 fuse_lowlevel_help();
1169 if (fuse_opts.show_version) {
1170 /* TODO: Show bcachefs version. */
1171 printf("FUSE library version %s\n", fuse_pkgversion());
1172 fuse_lowlevel_version();
1176 if (!fuse_opts.mountpoint) {
1178 printf("Please supply a mountpoint.\n");
1182 if (!ctx.devices_str) {
1184 printf("Please specify a device or device1:device2:...\n");
1188 tokenize_devices(&ctx);
1191 printf("Opening bcachefs filesystem on:\n");
1192 for (i = 0; i < ctx.nr_devices; ++i)
1193 printf("\t%s\n", ctx.devices[i]);
1195 c = bch2_fs_open(ctx.devices, ctx.nr_devices, bch_opts);
1197 die("error opening %s: %s", ctx.devices_str,
1198 strerror(-PTR_ERR(c)));
1201 struct fuse_session *se =
1202 fuse_session_new(&args, &bcachefs_fuse_ops,
1203 sizeof(bcachefs_fuse_ops), c);
1205 die("fuse_lowlevel_new err: %m");
1207 if (fuse_set_signal_handlers(se) < 0)
1208 die("fuse_set_signal_handlers err: %m");
1210 if (fuse_session_mount(se, fuse_opts.mountpoint))
1211 die("fuse_mount err: %m");
1213 fuse_daemonize(fuse_opts.foreground);
1215 ret = fuse_session_loop(se);
1218 fuse_session_unmount(se);
1219 fuse_remove_signal_handlers(se);
1220 fuse_session_destroy(se);
1223 free(fuse_opts.mountpoint);
1224 fuse_opt_free_args(&args);
1225 bf_context_free(&ctx);