Linux 6.13-rc4
[linux.git] / fs / bcachefs / fs-io.h
blobca70346e68dc3d9196c85ce7768b1c9f53e6e792
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _BCACHEFS_FS_IO_H
3 #define _BCACHEFS_FS_IO_H
5 #ifndef NO_BCACHEFS_FS
7 #include "buckets.h"
8 #include "fs.h"
9 #include "io_write_types.h"
10 #include "quota.h"
12 #include <linux/uio.h>
14 struct folio_vec {
15 struct folio *fv_folio;
16 size_t fv_offset;
17 size_t fv_len;
20 static inline struct folio_vec biovec_to_foliovec(struct bio_vec bv)
23 struct folio *folio = page_folio(bv.bv_page);
24 size_t offset = (folio_page_idx(folio, bv.bv_page) << PAGE_SHIFT) +
25 bv.bv_offset;
26 size_t len = min_t(size_t, folio_size(folio) - offset, bv.bv_len);
28 return (struct folio_vec) {
29 .fv_folio = folio,
30 .fv_offset = offset,
31 .fv_len = len,
35 static inline struct folio_vec bio_iter_iovec_folio(struct bio *bio,
36 struct bvec_iter iter)
38 return biovec_to_foliovec(bio_iter_iovec(bio, iter));
41 #define __bio_for_each_folio(bvl, bio, iter, start) \
42 for (iter = (start); \
43 (iter).bi_size && \
44 ((bvl = bio_iter_iovec_folio((bio), (iter))), 1); \
45 bio_advance_iter_single((bio), &(iter), (bvl).fv_len))
47 /**
48 * bio_for_each_folio - iterate over folios within a bio
50 * Like other non-_all versions, this iterates over what bio->bi_iter currently
51 * points to. This version is for drivers, where the bio may have previously
52 * been split or cloned.
54 #define bio_for_each_folio(bvl, bio, iter) \
55 __bio_for_each_folio(bvl, bio, iter, (bio)->bi_iter)
57 struct quota_res {
58 u64 sectors;
61 #ifdef CONFIG_BCACHEFS_QUOTA
63 static inline void __bch2_quota_reservation_put(struct bch_fs *c,
64 struct bch_inode_info *inode,
65 struct quota_res *res)
67 BUG_ON(res->sectors > inode->ei_quota_reserved);
69 bch2_quota_acct(c, inode->ei_qid, Q_SPC,
70 -((s64) res->sectors), KEY_TYPE_QUOTA_PREALLOC);
71 inode->ei_quota_reserved -= res->sectors;
72 res->sectors = 0;
75 static inline void bch2_quota_reservation_put(struct bch_fs *c,
76 struct bch_inode_info *inode,
77 struct quota_res *res)
79 if (res->sectors) {
80 mutex_lock(&inode->ei_quota_lock);
81 __bch2_quota_reservation_put(c, inode, res);
82 mutex_unlock(&inode->ei_quota_lock);
86 static inline int bch2_quota_reservation_add(struct bch_fs *c,
87 struct bch_inode_info *inode,
88 struct quota_res *res,
89 u64 sectors,
90 bool check_enospc)
92 int ret;
94 if (test_bit(EI_INODE_SNAPSHOT, &inode->ei_flags))
95 return 0;
97 mutex_lock(&inode->ei_quota_lock);
98 ret = bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors,
99 check_enospc ? KEY_TYPE_QUOTA_PREALLOC : KEY_TYPE_QUOTA_NOCHECK);
100 if (likely(!ret)) {
101 inode->ei_quota_reserved += sectors;
102 res->sectors += sectors;
104 mutex_unlock(&inode->ei_quota_lock);
106 return ret;
109 #else
111 static inline void __bch2_quota_reservation_put(struct bch_fs *c,
112 struct bch_inode_info *inode,
113 struct quota_res *res) {}
115 static inline void bch2_quota_reservation_put(struct bch_fs *c,
116 struct bch_inode_info *inode,
117 struct quota_res *res) {}
119 static inline int bch2_quota_reservation_add(struct bch_fs *c,
120 struct bch_inode_info *inode,
121 struct quota_res *res,
122 unsigned sectors,
123 bool check_enospc)
125 return 0;
128 #endif
130 void __bch2_i_sectors_acct(struct bch_fs *, struct bch_inode_info *,
131 struct quota_res *, s64);
133 static inline void bch2_i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode,
134 struct quota_res *quota_res, s64 sectors)
136 if (sectors) {
137 mutex_lock(&inode->ei_quota_lock);
138 __bch2_i_sectors_acct(c, inode, quota_res, sectors);
139 mutex_unlock(&inode->ei_quota_lock);
143 static inline struct address_space *faults_disabled_mapping(void)
145 return (void *) (((unsigned long) current->faults_disabled_mapping) & ~1UL);
148 static inline void set_fdm_dropped_locks(void)
150 current->faults_disabled_mapping =
151 (void *) (((unsigned long) current->faults_disabled_mapping)|1);
154 static inline bool fdm_dropped_locks(void)
156 return ((unsigned long) current->faults_disabled_mapping) & 1;
159 void bch2_inode_flush_nocow_writes_async(struct bch_fs *,
160 struct bch_inode_info *, struct closure *);
162 int __must_check bch2_write_inode_size(struct bch_fs *,
163 struct bch_inode_info *,
164 loff_t, unsigned);
166 int bch2_fsync(struct file *, loff_t, loff_t, int);
168 int bchfs_truncate(struct mnt_idmap *,
169 struct bch_inode_info *, struct iattr *);
170 long bch2_fallocate_dispatch(struct file *, int, loff_t, loff_t);
172 loff_t bch2_remap_file_range(struct file *, loff_t, struct file *,
173 loff_t, loff_t, unsigned);
175 loff_t bch2_llseek(struct file *, loff_t, int);
177 void bch2_fs_fsio_exit(struct bch_fs *);
178 int bch2_fs_fsio_init(struct bch_fs *);
179 #else
180 static inline void bch2_fs_fsio_exit(struct bch_fs *c) {}
181 static inline int bch2_fs_fsio_init(struct bch_fs *c) { return 0; }
182 #endif
184 #endif /* _BCACHEFS_FS_IO_H */