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 u64 map_root_ino(u64 ino)
38 return 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,
98 struct bch_fs *c = fuse_req_userdata(req);
99 struct bch_inode_unpacked bi;
100 struct qstr qstr = QSTR(name);
104 fuse_log(FUSE_LOG_DEBUG, "fuse_lookup(dir=%llu name=%s)\n",
107 dir = map_root_ino(dir);
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 inum = bch2_dirent_lookup(c, dir, &hash_info, &qstr);
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 inum,
143 struct fuse_file_info *fi)
145 struct bch_fs *c = fuse_req_userdata(req);
146 struct bch_inode_unpacked bi;
150 fuse_log(FUSE_LOG_DEBUG, "fuse_getattr(inum=%llu)\n",
153 inum = map_root_ino(inum);
155 ret = bch2_inode_find_by_inum(c, inum, &bi);
157 fuse_log(FUSE_LOG_DEBUG, "fuse_getattr error %i\n", ret);
158 fuse_reply_err(req, -ret);
162 fuse_log(FUSE_LOG_DEBUG, "fuse_getattr success\n");
164 attr = inode_to_stat(c, &bi);
165 fuse_reply_attr(req, &attr, DBL_MAX);
168 static void bcachefs_fuse_setattr(fuse_req_t req, fuse_ino_t inum,
169 struct stat *attr, int to_set,
170 struct fuse_file_info *fi)
172 struct bch_fs *c = fuse_req_userdata(req);
173 struct bch_inode_unpacked inode_u;
174 struct btree_trans trans;
175 struct btree_iter iter;
179 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_setattr(%llu, %x)\n",
182 inum = map_root_ino(inum);
184 bch2_trans_init(&trans, c, 0, 0);
186 bch2_trans_begin(&trans);
187 now = bch2_current_time(c);
189 ret = bch2_inode_peek(&trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
193 if (to_set & FUSE_SET_ATTR_MODE)
194 inode_u.bi_mode = attr->st_mode;
195 if (to_set & FUSE_SET_ATTR_UID)
196 inode_u.bi_uid = attr->st_uid;
197 if (to_set & FUSE_SET_ATTR_GID)
198 inode_u.bi_gid = attr->st_gid;
199 if (to_set & FUSE_SET_ATTR_SIZE)
200 inode_u.bi_size = attr->st_size;
201 if (to_set & FUSE_SET_ATTR_ATIME)
202 inode_u.bi_atime = timespec_to_bch2_time(c, attr->st_atim);
203 if (to_set & FUSE_SET_ATTR_MTIME)
204 inode_u.bi_mtime = timespec_to_bch2_time(c, attr->st_mtim);
205 if (to_set & FUSE_SET_ATTR_ATIME_NOW)
206 inode_u.bi_atime = now;
207 if (to_set & FUSE_SET_ATTR_MTIME_NOW)
208 inode_u.bi_mtime = now;
211 ret = bch2_inode_write(&trans, &iter, &inode_u) ?:
212 bch2_trans_commit(&trans, NULL, NULL,
213 BTREE_INSERT_NOFAIL);
215 bch2_trans_iter_exit(&trans, &iter);
219 bch2_trans_exit(&trans);
222 *attr = inode_to_stat(c, &inode_u);
223 fuse_reply_attr(req, attr, DBL_MAX);
225 fuse_reply_err(req, -ret);
229 static int do_create(struct bch_fs *c, u64 dir,
230 const char *name, mode_t mode, dev_t rdev,
231 struct bch_inode_unpacked *new_inode)
233 struct qstr qstr = QSTR(name);
234 struct bch_inode_unpacked dir_u;
236 dir = map_root_ino(dir);
238 bch2_inode_init_early(c, new_inode);
240 return bch2_trans_do(c, NULL, NULL, 0,
241 bch2_create_trans(&trans,
244 0, 0, mode, rdev, NULL, NULL));
247 static void bcachefs_fuse_mknod(fuse_req_t req, fuse_ino_t dir,
248 const char *name, mode_t mode,
251 struct bch_fs *c = fuse_req_userdata(req);
252 struct bch_inode_unpacked new_inode;
255 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_mknod(%llu, %s, %x, %x)\n",
256 dir, name, mode, rdev);
257 ret = do_create(c, dir, name, mode, rdev, &new_inode);
261 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
262 fuse_reply_entry(req, &e);
265 fuse_reply_err(req, -ret);
268 static void bcachefs_fuse_mkdir(fuse_req_t req, fuse_ino_t dir,
269 const char *name, mode_t mode)
271 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_mkdir(%llu, %s, %x)\n",
274 BUG_ON(mode & S_IFMT);
277 bcachefs_fuse_mknod(req, dir, name, mode, 0);
280 static void bcachefs_fuse_unlink(fuse_req_t req, fuse_ino_t dir,
283 struct bch_fs *c = fuse_req_userdata(req);
284 struct bch_inode_unpacked dir_u, inode_u;
285 struct qstr qstr = QSTR(name);
288 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_unlink(%llu, %s)\n", dir, name);
290 dir = map_root_ino(dir);
292 ret = bch2_trans_do(c, NULL, NULL, BTREE_INSERT_NOFAIL,
293 bch2_unlink_trans(&trans, dir, &dir_u,
296 fuse_reply_err(req, -ret);
299 static void bcachefs_fuse_rmdir(fuse_req_t req, fuse_ino_t dir,
302 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_rmdir(%llu, %s)\n", dir, name);
304 dir = map_root_ino(dir);
306 bcachefs_fuse_unlink(req, dir, name);
309 static void bcachefs_fuse_rename(fuse_req_t req,
310 fuse_ino_t src_dir, const char *srcname,
311 fuse_ino_t dst_dir, const char *dstname,
314 struct bch_fs *c = fuse_req_userdata(req);
315 struct bch_inode_unpacked dst_dir_u, src_dir_u;
316 struct bch_inode_unpacked src_inode_u, dst_inode_u;
317 struct qstr dst_name = QSTR(srcname);
318 struct qstr src_name = QSTR(dstname);
321 fuse_log(FUSE_LOG_DEBUG,
322 "bcachefs_fuse_rename(%llu, %s, %llu, %s, %x)\n",
323 src_dir, srcname, dst_dir, dstname, flags);
325 src_dir = map_root_ino(src_dir);
326 dst_dir = map_root_ino(dst_dir);
328 /* XXX handle overwrites */
329 ret = bch2_trans_do(c, NULL, NULL, 0,
330 bch2_rename_trans(&trans,
333 &src_inode_u, &dst_inode_u,
334 &src_name, &dst_name,
337 fuse_reply_err(req, -ret);
340 static void bcachefs_fuse_link(fuse_req_t req, fuse_ino_t inum,
341 fuse_ino_t newparent, const char *newname)
343 struct bch_fs *c = fuse_req_userdata(req);
344 struct bch_inode_unpacked dir_u, inode_u;
345 struct qstr qstr = QSTR(newname);
348 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_link(%llu, %llu, %s)\n",
349 inum, newparent, newname);
351 newparent = map_root_ino(newparent);
353 ret = bch2_trans_do(c, NULL, NULL, 0,
354 bch2_link_trans(&trans, newparent,
355 inum, &dir_u, &inode_u, &qstr));
358 struct fuse_entry_param e = inode_to_entry(c, &inode_u);
359 fuse_reply_entry(req, &e);
361 fuse_reply_err(req, -ret);
365 static void bcachefs_fuse_open(fuse_req_t req, fuse_ino_t inum,
366 struct fuse_file_info *fi)
368 fi->direct_io = false;
369 fi->keep_cache = true;
370 fi->cache_readdir = true;
372 fuse_reply_open(req, fi);
375 static void userbio_init(struct bio *bio, struct bio_vec *bv,
376 void *buf, size_t size)
378 bio_init(bio, bv, 1);
379 bio->bi_iter.bi_size = size;
385 static int get_inode_io_opts(struct bch_fs *c, u64 inum,
386 struct bch_io_opts *opts)
388 struct bch_inode_unpacked inode;
389 if (bch2_inode_find_by_inum(c, inum, &inode))
392 *opts = bch2_opts_to_inode_opts(c->opts);
393 bch2_io_opts_apply(opts, bch2_inode_opts_get(&inode));
397 static void bcachefs_fuse_read_endio(struct bio *bio)
399 closure_put(bio->bi_private);
402 struct fuse_align_io {
410 /* Handle unaligned start and end */
411 /* TODO: align to block_bytes, sector size, or page size? */
412 static struct fuse_align_io align_io(const struct bch_fs *c, size_t size,
415 struct fuse_align_io align;
419 align.start = round_down(offset, block_bytes(c));
420 align.pad_start = offset - align.start;
422 off_t end = offset + size;
423 align.end = round_up(end, block_bytes(c));
424 align.pad_end = align.end - end;
426 align.size = align.end - align.start;
432 * Given an aligned number of bytes transferred, figure out how many unaligned
433 * bytes were transferred.
435 static size_t align_fix_up_bytes(const struct fuse_align_io *align,
440 if (align_bytes > align->pad_start) {
441 bytes = align_bytes - align->pad_start;
442 bytes = bytes > align->pad_end ? bytes - align->pad_end : 0;
451 static int read_aligned(struct bch_fs *c, fuse_ino_t inum, size_t aligned_size,
452 off_t aligned_offset, void *buf)
454 BUG_ON(aligned_size & (block_bytes(c) - 1));
455 BUG_ON(aligned_offset & (block_bytes(c) - 1));
457 struct bch_io_opts io_opts;
458 if (get_inode_io_opts(c, inum, &io_opts))
461 struct bch_read_bio rbio;
463 userbio_init(&rbio.bio, &bv, buf, aligned_size);
464 bio_set_op_attrs(&rbio.bio, REQ_OP_READ, REQ_SYNC);
465 rbio.bio.bi_iter.bi_sector = aligned_offset >> 9;
468 closure_init_stack(&cl);
471 rbio.bio.bi_end_io = bcachefs_fuse_read_endio;
472 rbio.bio.bi_private = &cl;
474 bch2_read(c, rbio_init(&rbio.bio, io_opts), inum);
478 return -blk_status_to_errno(rbio.bio.bi_status);
481 static void bcachefs_fuse_read(fuse_req_t req, fuse_ino_t inum,
482 size_t size, off_t offset,
483 struct fuse_file_info *fi)
485 struct bch_fs *c = fuse_req_userdata(req);
487 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_read(%llu, %zd, %lld)\n",
490 /* Check inode size. */
491 struct bch_inode_unpacked bi;
492 int ret = bch2_inode_find_by_inum(c, inum, &bi);
494 fuse_reply_err(req, -ret);
498 off_t end = min_t(u64, bi.bi_size, offset + size);
500 fuse_reply_buf(req, NULL, 0);
505 struct fuse_align_io align = align_io(c, size, offset);
507 void *buf = aligned_alloc(PAGE_SIZE, align.size);
509 fuse_reply_err(req, ENOMEM);
513 ret = read_aligned(c, inum, align.size, align.start, buf);
516 fuse_reply_buf(req, buf + align.pad_start, size);
518 fuse_reply_err(req, -ret);
523 static int inode_update_times(struct bch_fs *c, fuse_ino_t inum)
525 struct btree_trans trans;
526 struct btree_iter iter;
527 struct bch_inode_unpacked inode_u;
531 bch2_trans_init(&trans, c, 0, 0);
533 bch2_trans_begin(&trans);
534 now = bch2_current_time(c);
536 ret = bch2_inode_peek(&trans, &iter, &inode_u, inum, BTREE_ITER_INTENT);
540 inode_u.bi_mtime = now;
541 inode_u.bi_ctime = now;
543 ret = bch2_inode_write(&trans, &iter, &inode_u);
547 ret = bch2_trans_commit(&trans, NULL, NULL,
548 BTREE_INSERT_NOFAIL);
551 bch2_trans_iter_exit(&trans, &iter);
555 bch2_trans_exit(&trans);
559 static int write_aligned(struct bch_fs *c, fuse_ino_t inum,
560 struct bch_io_opts io_opts, void *buf,
561 size_t aligned_size, off_t aligned_offset,
562 off_t new_i_size, size_t *written_out)
564 struct bch_write_op op = { 0 };
568 BUG_ON(aligned_size & (block_bytes(c) - 1));
569 BUG_ON(aligned_offset & (block_bytes(c) - 1));
573 closure_init_stack(&cl);
575 bch2_write_op_init(&op, c, io_opts); /* XXX reads from op?! */
576 op.write_point = writepoint_hashed(0);
577 op.nr_replicas = io_opts.data_replicas;
578 op.target = io_opts.foreground_target;
579 op.pos = POS(inum, aligned_offset >> 9);
580 op.new_i_size = new_i_size;
582 userbio_init(&op.wbio.bio, &bv, buf, aligned_size);
583 bio_set_op_attrs(&op.wbio.bio, REQ_OP_WRITE, REQ_SYNC);
585 if (bch2_disk_reservation_get(c, &op.res, aligned_size >> 9,
586 op.nr_replicas, 0)) {
587 /* XXX: use check_range_allocated like dio write path */
591 closure_call(&op.cl, bch2_write, NULL, &cl);
595 *written_out = op.written << 9;
600 static void bcachefs_fuse_write(fuse_req_t req, fuse_ino_t inum,
601 const char *buf, size_t size,
603 struct fuse_file_info *fi)
605 struct bch_fs *c = fuse_req_userdata(req);
606 struct bch_io_opts io_opts;
607 size_t aligned_written;
610 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write(%llu, %zd, %lld)\n",
613 struct fuse_align_io align = align_io(c, size, offset);
614 void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
615 BUG_ON(!aligned_buf);
617 if (get_inode_io_opts(c, inum, &io_opts)) {
622 /* Realign the data and read in start and end, if needed */
624 /* Read partial start data. */
625 if (align.pad_start) {
626 memset(aligned_buf, 0, block_bytes(c));
628 ret = read_aligned(c, inum, block_bytes(c), align.start,
635 * Read partial end data. If the whole write fits in one block, the
636 * start data and the end data are the same so this isn't needed.
639 !(align.pad_start && align.size == block_bytes(c))) {
640 off_t partial_end_start = align.end - block_bytes(c);
641 size_t buf_offset = align.size - block_bytes(c);
643 memset(aligned_buf + buf_offset, 0, block_bytes(c));
645 ret = read_aligned(c, inum, block_bytes(c), partial_end_start,
646 aligned_buf + buf_offset);
651 /* Overlay what we want to write. */
652 memcpy(aligned_buf + align.pad_start, buf, size);
654 /* Actually write. */
655 ret = write_aligned(c, inum, io_opts, aligned_buf,
656 align.size, align.start,
657 offset + size, &aligned_written);
659 /* Figure out how many unaligned bytes were written. */
660 size_t written = align_fix_up_bytes(&align, aligned_written);
661 BUG_ON(written > size);
663 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_write: wrote %zd bytes\n",
670 * Update inode times.
671 * TODO: Integrate with bch2_extent_update()
674 ret = inode_update_times(c, inum);
677 BUG_ON(written == 0);
678 fuse_reply_write(req, written);
684 fuse_reply_err(req, -ret);
688 static void bcachefs_fuse_symlink(fuse_req_t req, const char *link,
689 fuse_ino_t dir, const char *name)
691 struct bch_fs *c = fuse_req_userdata(req);
692 struct bch_inode_unpacked new_inode;
693 size_t link_len = strlen(link);
696 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_symlink(%s, %llu, %s)\n",
699 dir = map_root_ino(dir);
701 ret = do_create(c, dir, name, S_IFLNK|S_IRWXUGO, 0, &new_inode);
705 struct bch_io_opts io_opts;
706 ret = get_inode_io_opts(c, new_inode.bi_inum, &io_opts);
710 struct fuse_align_io align = align_io(c, link_len + 1, 0);
712 void *aligned_buf = aligned_alloc(PAGE_SIZE, align.size);
713 BUG_ON(!aligned_buf);
715 memset(aligned_buf, 0, align.size);
716 memcpy(aligned_buf, link, link_len); /* already terminated */
718 size_t aligned_written;
719 ret = write_aligned(c, new_inode.bi_inum, io_opts, aligned_buf,
720 align.size, align.start, link_len + 1,
727 size_t written = align_fix_up_bytes(&align, aligned_written);
728 BUG_ON(written != link_len + 1); // TODO: handle short
730 ret = inode_update_times(c, new_inode.bi_inum);
734 new_inode.bi_size = written;
736 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
737 fuse_reply_entry(req, &e);
741 fuse_reply_err(req, -ret);
744 static void bcachefs_fuse_readlink(fuse_req_t req, fuse_ino_t inum)
746 struct bch_fs *c = fuse_req_userdata(req);
749 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readlink(%llu)\n", inum);
751 struct bch_inode_unpacked bi;
752 int ret = bch2_inode_find_by_inum(c, inum, &bi);
756 struct fuse_align_io align = align_io(c, bi.bi_size, 0);
759 buf = aligned_alloc(PAGE_SIZE, align.size);
763 ret = read_aligned(c, inum, align.size, align.start, buf);
767 BUG_ON(buf[align.size - 1] != 0);
769 fuse_reply_readlink(req, buf);
773 fuse_reply_err(req, -ret);
780 * FUSE flush is essentially the close() call, however it is not guaranteed
781 * that one flush happens per open/create.
783 * It doesn't have to do anything, and is mostly relevant for NFS-style
784 * filesystems where close has some relationship to caching.
786 static void bcachefs_fuse_flush(fuse_req_t req, fuse_ino_t inum,
787 struct fuse_file_info *fi)
789 struct bch_fs *c = fuse_req_userdata(req);
792 static void bcachefs_fuse_release(fuse_req_t req, fuse_ino_t inum,
793 struct fuse_file_info *fi)
795 struct bch_fs *c = fuse_req_userdata(req);
798 static void bcachefs_fuse_fsync(fuse_req_t req, fuse_ino_t inum, int datasync,
799 struct fuse_file_info *fi)
801 struct bch_fs *c = fuse_req_userdata(req);
804 static void bcachefs_fuse_opendir(fuse_req_t req, fuse_ino_t inum,
805 struct fuse_file_info *fi)
807 struct bch_fs *c = fuse_req_userdata(req);
811 struct fuse_dir_context {
812 struct dir_context ctx;
826 #define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name)
827 #define FUSE_DIRENT_ALIGN(x) \
828 (((x) + sizeof(uint64_t) - 1) & ~(sizeof(uint64_t) - 1))
830 static size_t fuse_add_direntry2(char *buf, size_t bufsize,
831 const char *name, int namelen,
832 const struct stat *stbuf, off_t off)
834 size_t entlen = FUSE_NAME_OFFSET + namelen;
835 size_t entlen_padded = FUSE_DIRENT_ALIGN(entlen);
836 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
838 if ((buf == NULL) || (entlen_padded > bufsize))
839 return entlen_padded;
841 dirent->ino = stbuf->st_ino;
843 dirent->namelen = namelen;
844 dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
845 memcpy(dirent->name, name, namelen);
846 memset(dirent->name + namelen, 0, entlen_padded - entlen);
848 return entlen_padded;
851 static int fuse_filldir(struct dir_context *_ctx,
852 const char *name, int namelen,
853 loff_t pos, u64 ino, unsigned type)
855 struct fuse_dir_context *ctx =
856 container_of(_ctx, struct fuse_dir_context, ctx);
858 struct stat statbuf = {
859 .st_ino = unmap_root_ino(ino),
860 .st_mode = type << 12,
863 fuse_log(FUSE_LOG_DEBUG, "fuse_filldir(name=%s inum=%llu pos=%llu)\n",
864 name, statbuf.st_ino, pos);
866 size_t len = fuse_add_direntry2(ctx->buf,
873 if (len > ctx->bufsize)
882 static bool handle_dots(struct fuse_dir_context *ctx, fuse_ino_t dir)
884 if (ctx->ctx.pos == 0) {
885 if (fuse_filldir(&ctx->ctx, ".", 1, ctx->ctx.pos,
891 if (ctx->ctx.pos == 1) {
892 if (fuse_filldir(&ctx->ctx, "..", 2, ctx->ctx.pos,
893 /*TODO: parent*/ 1, DT_DIR) < 0)
901 static void bcachefs_fuse_readdir(fuse_req_t req, fuse_ino_t dir,
902 size_t size, off_t off,
903 struct fuse_file_info *fi)
905 struct bch_fs *c = fuse_req_userdata(req);
906 struct bch_inode_unpacked bi;
907 char *buf = calloc(size, 1);
908 struct fuse_dir_context ctx = {
909 .ctx.actor = fuse_filldir,
917 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir(dir=%llu, size=%zu, "
918 "off=%lld)\n", dir, size, off);
920 dir = map_root_ino(dir);
922 ret = bch2_inode_find_by_inum(c, dir, &bi);
926 if (!S_ISDIR(bi.bi_mode)) {
931 if (!handle_dots(&ctx, dir))
934 ret = bch2_readdir(c, dir, &ctx.ctx);
937 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_readdir reply %zd\n",
939 fuse_reply_buf(req, buf, ctx.buf - buf);
941 fuse_reply_err(req, -ret);
948 static void bcachefs_fuse_readdirplus(fuse_req_t req, fuse_ino_t dir,
949 size_t size, off_t off,
950 struct fuse_file_info *fi)
955 static void bcachefs_fuse_releasedir(fuse_req_t req, fuse_ino_t inum,
956 struct fuse_file_info *fi)
958 struct bch_fs *c = fuse_req_userdata(req);
961 static void bcachefs_fuse_fsyncdir(fuse_req_t req, fuse_ino_t inum, int datasync,
962 struct fuse_file_info *fi)
964 struct bch_fs *c = fuse_req_userdata(req);
968 static void bcachefs_fuse_statfs(fuse_req_t req, fuse_ino_t inum)
970 struct bch_fs *c = fuse_req_userdata(req);
971 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
972 unsigned shift = c->block_bits;
973 struct statvfs statbuf = {
974 .f_bsize = block_bytes(c),
975 .f_frsize = block_bytes(c),
976 .f_blocks = usage.capacity >> shift,
977 .f_bfree = (usage.capacity - usage.used) >> shift,
978 //.f_bavail = statbuf.f_bfree,
979 .f_files = usage.nr_inodes,
981 .f_namemax = BCH_NAME_MAX,
984 fuse_reply_statfs(req, &statbuf);
988 static void bcachefs_fuse_setxattr(fuse_req_t req, fuse_ino_t inum,
989 const char *name, const char *value,
990 size_t size, int flags)
992 struct bch_fs *c = fuse_req_userdata(req);
995 static void bcachefs_fuse_getxattr(fuse_req_t req, fuse_ino_t inum,
996 const char *name, size_t size)
998 struct bch_fs *c = fuse_req_userdata(req);
1000 fuse_reply_xattr(req, );
1003 static void bcachefs_fuse_listxattr(fuse_req_t req, fuse_ino_t inum, size_t size)
1005 struct bch_fs *c = fuse_req_userdata(req);
1008 static void bcachefs_fuse_removexattr(fuse_req_t req, fuse_ino_t inum,
1011 struct bch_fs *c = fuse_req_userdata(req);
1015 static void bcachefs_fuse_create(fuse_req_t req, fuse_ino_t dir,
1016 const char *name, mode_t mode,
1017 struct fuse_file_info *fi)
1019 struct bch_fs *c = fuse_req_userdata(req);
1020 struct bch_inode_unpacked new_inode;
1023 fuse_log(FUSE_LOG_DEBUG, "bcachefs_fuse_create(%llu, %s, %x)\n",
1026 ret = do_create(c, dir, name, mode, 0, &new_inode);
1030 struct fuse_entry_param e = inode_to_entry(c, &new_inode);
1031 fuse_reply_create(req, &e, fi);
1034 fuse_reply_err(req, -ret);
1039 static void bcachefs_fuse_write_buf(fuse_req_t req, fuse_ino_t inum,
1040 struct fuse_bufvec *bufv, off_t off,
1041 struct fuse_file_info *fi)
1043 struct bch_fs *c = fuse_req_userdata(req);
1046 static void bcachefs_fuse_fallocate(fuse_req_t req, fuse_ino_t inum, int mode,
1047 off_t offset, off_t length,
1048 struct fuse_file_info *fi)
1050 struct bch_fs *c = fuse_req_userdata(req);
1054 static const struct fuse_lowlevel_ops bcachefs_fuse_ops = {
1055 .init = bcachefs_fuse_init,
1056 .destroy = bcachefs_fuse_destroy,
1057 .lookup = bcachefs_fuse_lookup,
1058 .getattr = bcachefs_fuse_getattr,
1059 .setattr = bcachefs_fuse_setattr,
1060 .readlink = bcachefs_fuse_readlink,
1061 .mknod = bcachefs_fuse_mknod,
1062 .mkdir = bcachefs_fuse_mkdir,
1063 .unlink = bcachefs_fuse_unlink,
1064 .rmdir = bcachefs_fuse_rmdir,
1065 .symlink = bcachefs_fuse_symlink,
1066 .rename = bcachefs_fuse_rename,
1067 .link = bcachefs_fuse_link,
1068 .open = bcachefs_fuse_open,
1069 .read = bcachefs_fuse_read,
1070 .write = bcachefs_fuse_write,
1071 //.flush = bcachefs_fuse_flush,
1072 //.release = bcachefs_fuse_release,
1073 //.fsync = bcachefs_fuse_fsync,
1074 //.opendir = bcachefs_fuse_opendir,
1075 .readdir = bcachefs_fuse_readdir,
1076 //.readdirplus = bcachefs_fuse_readdirplus,
1077 //.releasedir = bcachefs_fuse_releasedir,
1078 //.fsyncdir = bcachefs_fuse_fsyncdir,
1079 .statfs = bcachefs_fuse_statfs,
1080 //.setxattr = bcachefs_fuse_setxattr,
1081 //.getxattr = bcachefs_fuse_getxattr,
1082 //.listxattr = bcachefs_fuse_listxattr,
1083 //.removexattr = bcachefs_fuse_removexattr,
1084 .create = bcachefs_fuse_create,
1088 .getlk = bcachefs_fuse_getlk,
1089 .setlk = bcachefs_fuse_setlk,
1091 //.write_buf = bcachefs_fuse_write_buf,
1092 //.fallocate = bcachefs_fuse_fallocate,
1097 * Setup and command parsing.
1106 static void bf_context_free(struct bf_context *ctx)
1110 free(ctx->devices_str);
1111 for (i = 0; i < ctx->nr_devices; ++i)
1112 free(ctx->devices[i]);
1116 static struct fuse_opt bf_opts[] = {
1121 * Fuse option parsing helper -- returning 0 means we consumed the argument, 1
1124 static int bf_opt_proc(void *data, const char *arg, int key,
1125 struct fuse_args *outargs)
1127 struct bf_context *ctx = data;
1130 case FUSE_OPT_KEY_NONOPT:
1131 /* Just extract the first non-option string. */
1132 if (!ctx->devices_str) {
1133 ctx->devices_str = strdup(arg);
1143 * dev1:dev2 -> [ dev1, dev2 ]
1146 static void tokenize_devices(struct bf_context *ctx)
1148 char *devices_str = strdup(ctx->devices_str);
1149 char *devices_tmp = devices_str;
1150 char **devices = NULL;
1154 while ((dev = strsep(&devices_tmp, ":"))) {
1155 if (strlen(dev) > 0) {
1156 devices = realloc(devices, (nr + 1) * sizeof *devices);
1157 devices[nr] = strdup(dev);
1163 devices = malloc(sizeof *devices);
1164 devices[0] = strdup(ctx->devices_str);
1168 ctx->devices = devices;
1169 ctx->nr_devices = nr;
1174 static void usage(char *argv[])
1176 printf("Usage: %s fusemount [options] <dev>[:dev2:...] <mountpoint>\n",
1181 int cmd_fusemount(int argc, char *argv[])
1183 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
1184 struct bch_opts bch_opts = bch2_opts_empty();
1185 struct bf_context ctx = { 0 };
1186 struct bch_fs *c = NULL;
1189 /* Parse arguments. */
1190 if (fuse_opt_parse(&args, &ctx, bf_opts, bf_opt_proc) < 0)
1191 die("fuse_opt_parse err: %m");
1193 struct fuse_cmdline_opts fuse_opts;
1194 if (fuse_parse_cmdline(&args, &fuse_opts) < 0)
1195 die("fuse_parse_cmdline err: %m");
1197 if (fuse_opts.show_help) {
1199 fuse_cmdline_help();
1200 fuse_lowlevel_help();
1204 if (fuse_opts.show_version) {
1205 /* TODO: Show bcachefs version. */
1206 printf("FUSE library version %s\n", fuse_pkgversion());
1207 fuse_lowlevel_version();
1211 if (!fuse_opts.mountpoint) {
1213 printf("Please supply a mountpoint.\n");
1217 if (!ctx.devices_str) {
1219 printf("Please specify a device or device1:device2:...\n");
1223 tokenize_devices(&ctx);
1226 printf("Opening bcachefs filesystem on:\n");
1227 for (i = 0; i < ctx.nr_devices; ++i)
1228 printf("\t%s\n", ctx.devices[i]);
1230 c = bch2_fs_open(ctx.devices, ctx.nr_devices, bch_opts);
1232 die("error opening %s: %s", ctx.devices_str,
1233 bch2_err_str(PTR_ERR(c)));
1236 struct fuse_session *se =
1237 fuse_session_new(&args, &bcachefs_fuse_ops,
1238 sizeof(bcachefs_fuse_ops), c);
1240 die("fuse_lowlevel_new err: %m");
1242 if (fuse_set_signal_handlers(se) < 0)
1243 die("fuse_set_signal_handlers err: %m");
1245 if (fuse_session_mount(se, fuse_opts.mountpoint))
1246 die("fuse_mount err: %m");
1248 /* This print statement is a trigger for tests. */
1249 printf("Fuse mount initialized.\n");
1251 fuse_daemonize(fuse_opts.foreground);
1253 ret = fuse_session_loop(se);
1256 fuse_session_unmount(se);
1257 fuse_remove_signal_handlers(se);
1258 fuse_session_destroy(se);
1261 free(fuse_opts.mountpoint);
1262 fuse_opt_free_args(&args);
1263 bf_context_free(&ctx);
1268 #endif /* BCACHEFS_FUSE */