bcachefs: bch2_inode_opts_get()
authorKent Overstreet <kent.overstreet@linux.dev>
Thu, 24 Nov 2022 01:14:55 +0000 (20:14 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:49 +0000 (17:09 -0400)
This improves io_opts() and makes it a non-inline function - it's big
enough that it probably shouldn't be.

Also, bch_io_opts no longer needs fields for whether options are
defined, so we can slim it down a bit.

We'd like to stop passing around the full bch_io_opts, but that'll be
tricky because of bch2_rebalance_add_key().

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/fs-io.c
fs/bcachefs/inode.c
fs/bcachefs/inode.h
fs/bcachefs/move.c
fs/bcachefs/opts.c
fs/bcachefs/opts.h

index 0bb8b39..25094e0 100644 (file)
@@ -1102,12 +1102,14 @@ void bch2_readahead(struct readahead_control *ractl)
 {
        struct bch_inode_info *inode = to_bch_ei(ractl->mapping->host);
        struct bch_fs *c = inode->v.i_sb->s_fs_info;
-       struct bch_io_opts opts = io_opts(c, &inode->ei_inode);
+       struct bch_io_opts opts;
        struct btree_trans trans;
        struct page *page;
        struct readpages_iter readpages_iter;
        int ret;
 
+       bch2_inode_opts_get(&opts, c, &inode->ei_inode);
+
        ret = readpages_iter_init(&readpages_iter, ractl);
        BUG_ON(ret);
 
@@ -1170,11 +1172,14 @@ static int bch2_read_single_page(struct page *page,
        struct bch_inode_info *inode = to_bch_ei(mapping->host);
        struct bch_fs *c = inode->v.i_sb->s_fs_info;
        struct bch_read_bio *rbio;
+       struct bch_io_opts opts;
        int ret;
        DECLARE_COMPLETION_ONSTACK(done);
 
+       bch2_inode_opts_get(&opts, c, &inode->ei_inode);
+
        rbio = rbio_init(bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS, &c->bio_read),
-                        io_opts(c, &inode->ei_inode));
+                        opts);
        rbio->bio.bi_private = &done;
        rbio->bio.bi_end_io = bch2_read_single_page_end_io;
 
@@ -1211,9 +1216,10 @@ struct bch_writepage_state {
 static inline struct bch_writepage_state bch_writepage_state_init(struct bch_fs *c,
                                                                  struct bch_inode_info *inode)
 {
-       return (struct bch_writepage_state) {
-               .opts = io_opts(c, &inode->ei_inode)
-       };
+       struct bch_writepage_state ret = { 0 };
+
+       bch2_inode_opts_get(&ret.opts, c, &inode->ei_inode);
+       return ret;
 }
 
 static void bch2_writepage_io_done(struct bch_write_op *op)
@@ -1879,7 +1885,7 @@ static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter)
        struct file *file = req->ki_filp;
        struct bch_inode_info *inode = file_bch_inode(file);
        struct bch_fs *c = inode->v.i_sb->s_fs_info;
-       struct bch_io_opts opts = io_opts(c, &inode->ei_inode);
+       struct bch_io_opts opts;
        struct dio_read *dio;
        struct bio *bio;
        loff_t offset = req->ki_pos;
@@ -1887,6 +1893,8 @@ static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter)
        size_t shorten;
        ssize_t ret;
 
+       bch2_inode_opts_get(&opts, c, &inode->ei_inode);
+
        if ((offset|iter->count) & (block_bytes(c) - 1))
                return -EINVAL;
 
@@ -2224,11 +2232,14 @@ static __always_inline long bch2_dio_write_loop(struct dio_write *dio)
        struct kiocb *req = dio->req;
        struct address_space *mapping = dio->mapping;
        struct bch_inode_info *inode = dio->inode;
+       struct bch_io_opts opts;
        struct bio *bio = &dio->op.wbio.bio;
        unsigned unaligned, iter_count;
        bool sync = dio->sync, dropped_locks;
        long ret;
 
+       bch2_inode_opts_get(&opts, c, &inode->ei_inode);
+
        while (1) {
                iter_count = dio->iter.count;
 
@@ -2276,7 +2287,7 @@ static __always_inline long bch2_dio_write_loop(struct dio_write *dio)
                        goto err;
                }
 
-               bch2_write_op_init(&dio->op, c, io_opts(c, &inode->ei_inode));
+               bch2_write_op_init(&dio->op, c, opts);
                dio->op.end_io          = sync
                        ? NULL
                        : bch2_dio_write_loop_async;
@@ -3055,9 +3066,10 @@ static int __bchfs_fallocate(struct bch_inode_info *inode, int mode,
        struct btree_trans trans;
        struct btree_iter iter;
        struct bpos end_pos = POS(inode->v.i_ino, end_sector);
-       unsigned replicas = io_opts(c, &inode->ei_inode).data_replicas;
+       struct bch_io_opts opts;
        int ret = 0;
 
+       bch2_inode_opts_get(&opts, c, &inode->ei_inode);
        bch2_trans_init(&trans, c, BTREE_ITER_MAX, 512);
 
        bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
@@ -3088,7 +3100,7 @@ static int __bchfs_fallocate(struct bch_inode_info *inode, int mode,
 
                /* already reserved */
                if (k.k->type == KEY_TYPE_reservation &&
-                   bkey_s_c_to_reservation(k).v->nr_replicas >= replicas) {
+                   bkey_s_c_to_reservation(k).v->nr_replicas >= opts.data_replicas) {
                        bch2_btree_iter_advance(&iter);
                        continue;
                }
@@ -3118,10 +3130,10 @@ static int __bchfs_fallocate(struct bch_inode_info *inode, int mode,
                                goto bkey_err;
                }
 
-               if (reservation.v.nr_replicas < replicas ||
+               if (reservation.v.nr_replicas < opts.data_replicas ||
                    bch2_bkey_sectors_compressed(k)) {
                        ret = bch2_disk_reservation_get(c, &disk_res, sectors,
-                                                       replicas, 0);
+                                                       opts.data_replicas, 0);
                        if (unlikely(ret))
                                goto bkey_err;
 
index 6e7ba2e..9eeabe7 100644 (file)
@@ -768,3 +768,11 @@ struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *inode)
 #undef x
        return ret;
 }
+
+void bch2_inode_opts_get(struct bch_io_opts *opts, struct bch_fs *c,
+                        struct bch_inode_unpacked *inode)
+{
+#define x(_name, _bits)                opts->_name = inode_opt_get(c, inode, _name);
+       BCH_INODE_OPTS()
+#undef x
+}
index 27744f7..da78ed0 100644 (file)
@@ -98,17 +98,8 @@ int bch2_inode_find_by_inum_trans(struct btree_trans *, subvol_inum,
 int bch2_inode_find_by_inum(struct bch_fs *, subvol_inum,
                            struct bch_inode_unpacked *);
 
-static inline struct bch_io_opts bch2_inode_opts_get(struct bch_inode_unpacked *inode)
-{
-       struct bch_io_opts ret = { 0 };
-
-#define x(_name, _bits)                                        \
-       if (inode->bi_##_name)                                          \
-               opt_set(ret, _name, inode->bi_##_name - 1);
-       BCH_INODE_OPTS()
-#undef x
-       return ret;
-}
+#define inode_opt_get(_c, _inode, _name)                       \
+       ((_inode)->bi_##_name ? (_inode)->bi_##_name - 1 : (_c)->opts._name)
 
 static inline void bch2_inode_opt_set(struct bch_inode_unpacked *inode,
                                      enum inode_opt_id id, u64 v)
@@ -139,15 +130,6 @@ static inline u64 bch2_inode_opt_get(struct bch_inode_unpacked *inode,
        }
 }
 
-static inline struct bch_io_opts
-io_opts(struct bch_fs *c, struct bch_inode_unpacked *inode)
-{
-       struct bch_io_opts opts = bch2_opts_to_inode_opts(c->opts);
-
-       bch2_io_opts_apply(&opts, bch2_inode_opts_get(inode));
-       return opts;
-}
-
 static inline u8 mode_to_type(umode_t mode)
 {
        return (mode >> 12) & 15;
@@ -188,5 +170,7 @@ int bch2_inode_nlink_inc(struct bch_inode_unpacked *);
 void bch2_inode_nlink_dec(struct btree_trans *, struct bch_inode_unpacked *);
 
 struct bch_opts bch2_inode_opts_to_opts(struct bch_inode_unpacked *);
+void bch2_inode_opts_get(struct bch_io_opts *, struct bch_fs *,
+                        struct bch_inode_unpacked *);
 
 #endif /* _BCACHEFS_INODE_H */
index 848a415..65c3af1 100644 (file)
@@ -465,7 +465,7 @@ static int __bch2_move_data(struct moving_context *ctxt,
                                continue;
 
                        if (!ret)
-                               bch2_io_opts_apply(&io_opts, bch2_inode_opts_get(&inode));
+                               bch2_inode_opts_get(&io_opts, c, &inode);
 
                        cur_inum = k.k->p.inode;
                }
index 9c49d54..04e2989 100644 (file)
@@ -531,22 +531,11 @@ void bch2_opt_set_sb(struct bch_fs *c, const struct bch_option *opt, u64 v)
 
 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts src)
 {
-       struct bch_io_opts ret = { 0 };
-#define x(_name, _bits)                                        \
-       if (opt_defined(src, _name))                                    \
-               opt_set(ret, _name, src._name);
-       BCH_INODE_OPTS()
-#undef x
-       return ret;
-}
-
-void bch2_io_opts_apply(struct bch_io_opts *dst, struct bch_io_opts src)
-{
-#define x(_name, _bits)                                        \
-       if (opt_defined(src, _name))                                    \
-               opt_set(*dst, _name, src._name);
+       return (struct bch_io_opts) {
+#define x(_name, _bits)        ._name = src._name,
        BCH_INODE_OPTS()
 #undef x
+       };
 }
 
 bool bch2_opt_is_inode_opt(enum bch_opt_id id)
index a32a7ab..c602517 100644 (file)
@@ -503,17 +503,12 @@ int bch2_parse_mount_opts(struct bch_fs *, struct bch_opts *, char *);
 /* inode opts: */
 
 struct bch_io_opts {
-#define x(_name, _bits)        unsigned _name##_defined:1;
-       BCH_INODE_OPTS()
-#undef x
-
 #define x(_name, _bits)        u##_bits _name;
        BCH_INODE_OPTS()
 #undef x
 };
 
 struct bch_io_opts bch2_opts_to_inode_opts(struct bch_opts);
-void bch2_io_opts_apply(struct bch_io_opts *, struct bch_io_opts);
 bool bch2_opt_is_inode_opt(enum bch_opt_id);
 
 #endif /* _BCACHEFS_OPTS_H */