blob: b5ea9fa1259d1462e9033318466fc914911672ce [file] [log] [blame]
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001// SPDX-License-Identifier: GPL-2.0
2#ifndef NO_BCACHEFS_FS
3
4#include "bcachefs.h"
5#include "acl.h"
Kent Overstreet07a10062020-12-17 15:08:58 -05006#include "bkey_buf.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08007#include "btree_update.h"
8#include "buckets.h"
9#include "chardev.h"
10#include "dirent.h"
Kent Overstreetd4bf5ee2022-07-18 19:42:58 -040011#include "errcode.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080012#include "extents.h"
13#include "fs.h"
Kent Overstreet96385742019-10-02 18:35:36 -040014#include "fs-common.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080015#include "fs-io.h"
16#include "fs-ioctl.h"
Kent Overstreetdbbfca92023-08-03 18:18:21 -040017#include "fs-io-buffered.h"
18#include "fs-io-direct.h"
19#include "fs-io-pagecache.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080020#include "fsck.h"
21#include "inode.h"
Kent Overstreet1809b8c2023-09-10 18:05:17 -040022#include "io_read.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080023#include "journal.h"
24#include "keylist.h"
25#include "quota.h"
Kent Overstreet8e877ca2023-08-16 16:54:33 -040026#include "snapshot.h"
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080027#include "super.h"
28#include "xattr.h"
29
30#include <linux/aio.h>
31#include <linux/backing-dev.h>
32#include <linux/exportfs.h>
33#include <linux/fiemap.h>
34#include <linux/module.h>
35#include <linux/pagemap.h>
36#include <linux/posix_acl.h>
37#include <linux/random.h>
38#include <linux/seq_file.h>
39#include <linux/statfs.h>
Stijn Tintelffcf9ec2021-05-13 23:08:47 +030040#include <linux/string.h>
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080041#include <linux/xattr.h>
42
43static struct kmem_cache *bch2_inode_cache;
44
Kent Overstreet32b26e82021-11-06 00:03:40 -040045static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080046 struct bch_inode_info *,
Kent Overstreet9ca48532021-10-27 13:05:56 -040047 struct bch_inode_unpacked *,
48 struct bch_subvolume *);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080049
Kent Overstreet32b26e82021-11-06 00:03:40 -040050void bch2_inode_update_after_write(struct btree_trans *trans,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080051 struct bch_inode_info *inode,
52 struct bch_inode_unpacked *bi,
53 unsigned fields)
54{
Kent Overstreet32b26e82021-11-06 00:03:40 -040055 struct bch_fs *c = trans->c;
56
57 BUG_ON(bi->bi_inum != inode->v.i_ino);
58
59 bch2_assert_pos_locked(trans, BTREE_ID_inodes,
60 POS(0, bi->bi_inum),
Kent Overstreet7c8f6f92022-01-12 02:13:21 -050061 c->opts.inodes_use_key_cache);
Kent Overstreet32b26e82021-11-06 00:03:40 -040062
Kent Overstreetb43a0f62019-09-25 16:19:52 -040063 set_nlink(&inode->v, bch2_inode_nlink_get(bi));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080064 i_uid_write(&inode->v, bi->bi_uid);
65 i_gid_write(&inode->v, bi->bi_gid);
66 inode->v.i_mode = bi->bi_mode;
67
68 if (fields & ATTR_ATIME)
Linus Torvalds9e877052023-10-30 11:09:38 -100069 inode_set_atime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_atime));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080070 if (fields & ATTR_MTIME)
Linus Torvalds9e877052023-10-30 11:09:38 -100071 inode_set_mtime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_mtime));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080072 if (fields & ATTR_CTIME)
73 inode_set_ctime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_ctime));
74
75 inode->ei_inode = *bi;
Kent Overstreet2ea90042018-07-17 14:12:42 -040076
77 bch2_inode_flags_to_vfs(inode);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080078}
79
Kent Overstreet2ea90042018-07-17 14:12:42 -040080int __must_check bch2_write_inode(struct bch_fs *c,
81 struct bch_inode_info *inode,
82 inode_set_fn set,
83 void *p, unsigned fields)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080084{
Kent Overstreet6bd68ec2023-09-12 17:16:02 -040085 struct btree_trans *trans = bch2_trans_get(c);
Kent Overstreet67e0dd82021-08-30 15:18:31 -040086 struct btree_iter iter = { NULL };
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080087 struct bch_inode_unpacked inode_u;
88 int ret;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080089retry:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -040090 bch2_trans_begin(trans);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080091
Kent Overstreet6bd68ec2023-09-12 17:16:02 -040092 ret = bch2_inode_peek(trans, &iter, &inode_u, inode_inum(inode),
Kent Overstreet67e0dd82021-08-30 15:18:31 -040093 BTREE_ITER_INTENT) ?:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -040094 (set ? set(trans, inode, &inode_u, p) : 0) ?:
95 bch2_inode_write(trans, &iter, &inode_u) ?:
Kent Overstreetcb52d23e2023-11-11 16:31:50 -050096 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -080097
98 /*
99 * the btree node lock protects inode->ei_inode, not ei_update_lock;
100 * this is important for inode updates via bchfs_write_index_update
101 */
102 if (!ret)
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400103 bch2_inode_update_after_write(trans, inode, &inode_u, fields);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800104
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400105 bch2_trans_iter_exit(trans, &iter);
Kent Overstreet47c46c92020-04-01 17:28:39 -0400106
Kent Overstreet549d1732022-07-17 23:06:38 -0400107 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
Kent Overstreet47c46c92020-04-01 17:28:39 -0400108 goto retry;
109
Kent Overstreete47a3902023-05-27 19:59:59 -0400110 bch2_fs_fatal_err_on(bch2_err_matches(ret, ENOENT), c,
Kent Overstreet3ed94062024-03-17 21:51:19 -0400111 "%s: inode %u:%llu not found when updating",
112 bch2_err_str(ret),
Kent Overstreet711bf942023-03-15 19:04:05 -0400113 inode_inum(inode).subvol,
114 inode_inum(inode).inum);
115
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400116 bch2_trans_put(trans);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800117 return ret < 0 ? ret : 0;
118}
119
Kent Overstreet0f5254a2018-12-17 05:43:00 -0500120int bch2_fs_quota_transfer(struct bch_fs *c,
121 struct bch_inode_info *inode,
122 struct bch_qid new_qid,
123 unsigned qtypes,
124 enum quota_acct_mode mode)
125{
126 unsigned i;
127 int ret;
128
129 qtypes &= enabled_qtypes(c);
130
131 for (i = 0; i < QTYP_NR; i++)
132 if (new_qid.q[i] == inode->ei_qid.q[i])
133 qtypes &= ~(1U << i);
134
135 if (!qtypes)
136 return 0;
137
138 mutex_lock(&inode->ei_quota_lock);
139
140 ret = bch2_quota_transfer(c, qtypes, new_qid,
141 inode->ei_qid,
142 inode->v.i_blocks +
143 inode->ei_quota_reserved,
144 mode);
145 if (!ret)
146 for (i = 0; i < QTYP_NR; i++)
147 if (qtypes & (1 << i))
148 inode->ei_qid.q[i] = new_qid.q[i];
149
150 mutex_unlock(&inode->ei_quota_lock);
151
152 return ret;
153}
154
Kent Overstreet284ae182021-03-16 01:33:39 -0400155static int bch2_iget5_test(struct inode *vinode, void *p)
156{
157 struct bch_inode_info *inode = to_bch_ei(vinode);
158 subvol_inum *inum = p;
159
160 return inode->ei_subvol == inum->subvol &&
161 inode->ei_inode.bi_inum == inum->inum;
162}
163
164static int bch2_iget5_set(struct inode *vinode, void *p)
165{
166 struct bch_inode_info *inode = to_bch_ei(vinode);
167 subvol_inum *inum = p;
168
169 inode->v.i_ino = inum->inum;
170 inode->ei_subvol = inum->subvol;
171 inode->ei_inode.bi_inum = inum->inum;
172 return 0;
173}
174
175static unsigned bch2_inode_hash(subvol_inum inum)
176{
177 return jhash_3words(inum.subvol, inum.inum >> 32, inum.inum, JHASH_INITVAL);
178}
179
Kent Overstreeta91bc5e2024-01-25 20:25:49 -0500180static struct bch_inode_info *bch2_inode_insert(struct bch_fs *c, struct bch_inode_info *inode)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800181{
Kent Overstreeta91bc5e2024-01-25 20:25:49 -0500182 subvol_inum inum = inode_inum(inode);
183 struct bch_inode_info *old = to_bch_ei(inode_insert5(&inode->v,
184 bch2_inode_hash(inum),
185 bch2_iget5_test,
186 bch2_iget5_set,
187 &inum));
188 BUG_ON(!old);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800189
Kent Overstreeta91bc5e2024-01-25 20:25:49 -0500190 if (unlikely(old != inode)) {
191 discard_new_inode(&inode->v);
192 inode = old;
193 } else {
194 mutex_lock(&c->vfs_inodes_lock);
195 list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list);
196 mutex_unlock(&c->vfs_inodes_lock);
197 /*
198 * we really don't want insert_inode_locked2() to be setting
199 * I_NEW...
200 */
201 unlock_new_inode(&inode->v);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800202 }
203
Kent Overstreeta91bc5e2024-01-25 20:25:49 -0500204 return inode;
205}
Kent Overstreet9edbcc72023-03-15 11:53:51 -0400206
Kent Overstreeta91bc5e2024-01-25 20:25:49 -0500207#define memalloc_flags_do(_flags, _do) \
208({ \
209 unsigned _saved_flags = memalloc_flags_save(_flags); \
210 typeof(_do) _ret = _do; \
211 memalloc_noreclaim_restore(_saved_flags); \
212 _ret; \
213})
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800214
Kent Overstreeta91bc5e2024-01-25 20:25:49 -0500215/*
216 * Allocate a new inode, dropping/retaking btree locks if necessary:
217 */
218static struct bch_inode_info *bch2_new_inode(struct btree_trans *trans)
219{
220 struct bch_fs *c = trans->c;
221
222 struct bch_inode_info *inode =
223 memalloc_flags_do(PF_MEMALLOC_NORECLAIM|PF_MEMALLOC_NOWARN,
224 to_bch_ei(new_inode(c->vfs_sb)));
225
226 if (unlikely(!inode)) {
227 int ret = drop_locks_do(trans, (inode = to_bch_ei(new_inode(c->vfs_sb))) ? 0 : -ENOMEM);
228 if (ret && inode)
229 discard_new_inode(&inode->v);
230 if (ret)
231 return ERR_PTR(ret);
232 }
233
234 return inode;
235}
236
237struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
238{
239 struct bch_inode_info *inode =
240 to_bch_ei(ilookup5_nowait(c->vfs_sb,
241 bch2_inode_hash(inum),
242 bch2_iget5_test,
243 &inum));
244 if (inode)
245 return &inode->v;
246
247 struct btree_trans *trans = bch2_trans_get(c);
248
249 struct bch_inode_unpacked inode_u;
250 struct bch_subvolume subvol;
251 int ret = lockrestart_do(trans,
252 bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?:
253 bch2_inode_find_by_inum_trans(trans, inum, &inode_u)) ?:
254 PTR_ERR_OR_ZERO(inode = bch2_new_inode(trans));
255 if (!ret) {
256 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
257 inode = bch2_inode_insert(c, inode);
258 }
259 bch2_trans_put(trans);
260
261 return ret ? ERR_PTR(ret) : &inode->v;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800262}
263
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400264struct bch_inode_info *
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800265__bch2_create(struct mnt_idmap *idmap,
266 struct bch_inode_info *dir, struct dentry *dentry,
Kent Overstreet42d23732021-03-16 23:28:43 -0400267 umode_t mode, dev_t rdev, subvol_inum snapshot_src,
268 unsigned flags)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800269{
270 struct bch_fs *c = dir->v.i_sb->s_fs_info;
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400271 struct btree_trans *trans;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800272 struct bch_inode_unpacked dir_u;
Kent Overstreeta91bc5e2024-01-25 20:25:49 -0500273 struct bch_inode_info *inode;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800274 struct bch_inode_unpacked inode_u;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800275 struct posix_acl *default_acl = NULL, *acl = NULL;
Kent Overstreet284ae182021-03-16 01:33:39 -0400276 subvol_inum inum;
Kent Overstreet9ca48532021-10-27 13:05:56 -0400277 struct bch_subvolume subvol;
Kent Overstreet73ab6f32018-07-23 05:48:35 -0400278 u64 journal_seq = 0;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800279 int ret;
280
Kent Overstreet96385742019-10-02 18:35:36 -0400281 /*
282 * preallocate acls + vfs inode before btree transaction, so that
283 * nothing can fail after the transaction succeeds:
284 */
285#ifdef CONFIG_BCACHEFS_POSIX_ACL
286 ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800287 if (ret)
288 return ERR_PTR(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800289#endif
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800290 inode = to_bch_ei(new_inode(c->vfs_sb));
291 if (unlikely(!inode)) {
Kent Overstreet96385742019-10-02 18:35:36 -0400292 inode = ERR_PTR(-ENOMEM);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800293 goto err;
294 }
295
Kent Overstreet96385742019-10-02 18:35:36 -0400296 bch2_inode_init_early(c, &inode_u);
297
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400298 if (!(flags & BCH_CREATE_TMPFILE))
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800299 mutex_lock(&dir->ei_update_lock);
300
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400301 trans = bch2_trans_get(c);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800302retry:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400303 bch2_trans_begin(trans);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800304
Kent Overstreet0d72ab32023-12-29 21:16:32 -0500305 ret = bch2_subvol_is_ro_trans(trans, dir->ei_subvol) ?:
306 bch2_create_trans(trans,
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400307 inode_inum(dir), &dir_u, &inode_u,
308 !(flags & BCH_CREATE_TMPFILE)
309 ? &dentry->d_name : NULL,
Kent Overstreet96385742019-10-02 18:35:36 -0400310 from_kuid(i_user_ns(&dir->v), current_fsuid()),
311 from_kgid(i_user_ns(&dir->v), current_fsgid()),
312 mode, rdev,
Kent Overstreet42d23732021-03-16 23:28:43 -0400313 default_acl, acl, snapshot_src, flags) ?:
Kent Overstreet96385742019-10-02 18:35:36 -0400314 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
315 KEY_TYPE_QUOTA_PREALLOC);
316 if (unlikely(ret))
317 goto err_before_quota;
318
Kent Overstreet9ca48532021-10-27 13:05:56 -0400319 inum.subvol = inode_u.bi_subvol ?: dir->ei_subvol;
320 inum.inum = inode_u.bi_inum;
321
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400322 ret = bch2_subvolume_get(trans, inum.subvol, true,
Kent Overstreet9ca48532021-10-27 13:05:56 -0400323 BTREE_ITER_WITH_UPDATES, &subvol) ?:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400324 bch2_trans_commit(trans, NULL, &journal_seq, 0);
Kent Overstreet96385742019-10-02 18:35:36 -0400325 if (unlikely(ret)) {
326 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
327 KEY_TYPE_QUOTA_WARN);
328err_before_quota:
Kent Overstreet549d1732022-07-17 23:06:38 -0400329 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
Kent Overstreet96385742019-10-02 18:35:36 -0400330 goto retry;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800331 goto err_trans;
Kent Overstreet96385742019-10-02 18:35:36 -0400332 }
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800333
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400334 if (!(flags & BCH_CREATE_TMPFILE)) {
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400335 bch2_inode_update_after_write(trans, dir, &dir_u,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800336 ATTR_MTIME|ATTR_CTIME);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800337 mutex_unlock(&dir->ei_update_lock);
338 }
339
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400340 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800341
342 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
343 set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
344
345 /*
346 * we must insert the new inode into the inode cache before calling
347 * bch2_trans_exit() and dropping locks, else we could race with another
348 * thread pulling the inode in and modifying it:
349 */
Kent Overstreeta91bc5e2024-01-25 20:25:49 -0500350 inode = bch2_inode_insert(c, inode);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400351 bch2_trans_put(trans);
Kent Overstreet96385742019-10-02 18:35:36 -0400352err:
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800353 posix_acl_release(default_acl);
354 posix_acl_release(acl);
355 return inode;
356err_trans:
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400357 if (!(flags & BCH_CREATE_TMPFILE))
Kent Overstreet19ee5f22018-07-20 22:23:42 -0400358 mutex_unlock(&dir->ei_update_lock);
359
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400360 bch2_trans_put(trans);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800361 make_bad_inode(&inode->v);
362 iput(&inode->v);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800363 inode = ERR_PTR(ret);
Kent Overstreet96385742019-10-02 18:35:36 -0400364 goto err;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800365}
366
367/* methods */
368
Kent Overstreet737cd172024-01-25 12:36:37 -0500369static struct bch_inode_info *bch2_lookup_trans(struct btree_trans *trans,
370 subvol_inum dir, struct bch_hash_info *dir_hash_info,
371 const struct qstr *name)
372{
373 struct bch_fs *c = trans->c;
374 struct btree_iter dirent_iter = {};
375 subvol_inum inum = {};
376
377 int ret = bch2_hash_lookup(trans, &dirent_iter, bch2_dirent_hash_desc,
378 dir_hash_info, dir, name, 0);
379 if (ret)
380 return ERR_PTR(ret);
381
382 struct bkey_s_c k = bch2_btree_iter_peek_slot(&dirent_iter);
383 ret = bkey_err(k);
384 if (ret)
385 goto err;
386
387 ret = bch2_dirent_read_target(trans, dir, bkey_s_c_to_dirent(k), &inum);
388 if (ret > 0)
389 ret = -ENOENT;
390 if (ret)
391 goto err;
392
393 struct bch_inode_info *inode =
394 to_bch_ei(ilookup5_nowait(c->vfs_sb,
395 bch2_inode_hash(inum),
396 bch2_iget5_test,
397 &inum));
398 if (inode)
399 goto out;
400
401 struct bch_subvolume subvol;
402 struct bch_inode_unpacked inode_u;
403 ret = bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?:
404 bch2_inode_find_by_inum_nowarn_trans(trans, inum, &inode_u) ?:
405 PTR_ERR_OR_ZERO(inode = bch2_new_inode(trans));
406 if (bch2_err_matches(ret, ENOENT)) {
407 struct printbuf buf = PRINTBUF;
408
409 bch2_bkey_val_to_text(&buf, c, k);
410 bch_err(c, "%s points to missing inode", buf.buf);
411 printbuf_exit(&buf);
412 }
413 if (ret)
414 goto err;
415
416 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
417 inode = bch2_inode_insert(c, inode);
418out:
419 bch2_trans_iter_exit(trans, &dirent_iter);
420 return inode;
421err:
422 inode = ERR_PTR(ret);
423 goto out;
424}
425
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800426static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
427 unsigned int flags)
428{
429 struct bch_fs *c = vdir->i_sb->s_fs_info;
430 struct bch_inode_info *dir = to_bch_ei(vdir);
Kent Overstreet07bca3b2021-03-02 18:35:30 -0500431 struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800432
Kent Overstreet737cd172024-01-25 12:36:37 -0500433 struct bch_inode_info *inode;
434 bch2_trans_do(c, NULL, NULL, 0,
435 PTR_ERR_OR_ZERO(inode = bch2_lookup_trans(trans, inode_inum(dir),
436 &hash, &dentry->d_name)));
437 if (IS_ERR(inode))
438 inode = NULL;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800439
Kent Overstreet737cd172024-01-25 12:36:37 -0500440 return d_splice_alias(&inode->v, dentry);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800441}
442
Kent Overstreet821a99b2019-10-11 15:14:36 -0400443static int bch2_mknod(struct mnt_idmap *idmap,
444 struct inode *vdir, struct dentry *dentry,
445 umode_t mode, dev_t rdev)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800446{
447 struct bch_inode_info *inode =
Kent Overstreet42d23732021-03-16 23:28:43 -0400448 __bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev,
449 (subvol_inum) { 0 }, 0);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800450
451 if (IS_ERR(inode))
Kent Overstreet5c1ef832022-09-18 15:43:50 -0400452 return bch2_err_class(PTR_ERR(inode));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800453
454 d_instantiate(dentry, &inode->v);
455 return 0;
456}
457
Kent Overstreet821a99b2019-10-11 15:14:36 -0400458static int bch2_create(struct mnt_idmap *idmap,
459 struct inode *vdir, struct dentry *dentry,
460 umode_t mode, bool excl)
461{
462 return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0);
463}
464
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800465static int __bch2_link(struct bch_fs *c,
466 struct bch_inode_info *inode,
467 struct bch_inode_info *dir,
468 struct dentry *dentry)
469{
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400470 struct btree_trans *trans = bch2_trans_get(c);
Justin Husted184b1dc2019-11-11 20:14:30 -0800471 struct bch_inode_unpacked dir_u, inode_u;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800472 int ret;
473
Kent Overstreet19ee5f22018-07-20 22:23:42 -0400474 mutex_lock(&inode->ei_update_lock);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800475
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400476 ret = commit_do(trans, NULL, NULL, 0,
477 bch2_link_trans(trans,
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400478 inode_inum(dir), &dir_u,
479 inode_inum(inode), &inode_u,
Kent Overstreet5f0e4ae12021-03-12 17:52:42 -0500480 &dentry->d_name));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800481
Justin Husted184b1dc2019-11-11 20:14:30 -0800482 if (likely(!ret)) {
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400483 bch2_inode_update_after_write(trans, dir, &dir_u,
Justin Husted184b1dc2019-11-11 20:14:30 -0800484 ATTR_MTIME|ATTR_CTIME);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400485 bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME);
Justin Husted184b1dc2019-11-11 20:14:30 -0800486 }
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800487
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400488 bch2_trans_put(trans);
Kent Overstreet19ee5f22018-07-20 22:23:42 -0400489 mutex_unlock(&inode->ei_update_lock);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800490 return ret;
491}
492
493static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
494 struct dentry *dentry)
495{
496 struct bch_fs *c = vdir->i_sb->s_fs_info;
497 struct bch_inode_info *dir = to_bch_ei(vdir);
498 struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
499 int ret;
500
Kent Overstreet4e1ec2c2018-07-17 14:03:47 -0400501 lockdep_assert_held(&inode->v.i_rwsem);
502
Kent Overstreet0d72ab32023-12-29 21:16:32 -0500503 ret = bch2_subvol_is_ro(c, dir->ei_subvol) ?:
504 bch2_subvol_is_ro(c, inode->ei_subvol) ?:
505 __bch2_link(c, inode, dir, dentry);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800506 if (unlikely(ret))
Kent Overstreet1a1c93e2024-02-09 18:34:05 -0500507 return bch2_err_class(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800508
509 ihold(&inode->v);
510 d_instantiate(dentry, &inode->v);
511 return 0;
512}
513
Kent Overstreet42d23732021-03-16 23:28:43 -0400514int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
Kent Overstreet20278752021-10-11 12:03:19 -0400515 bool deleting_snapshot)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800516{
517 struct bch_fs *c = vdir->i_sb->s_fs_info;
518 struct bch_inode_info *dir = to_bch_ei(vdir);
519 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
520 struct bch_inode_unpacked dir_u, inode_u;
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400521 struct btree_trans *trans = bch2_trans_get(c);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800522 int ret;
523
Kent Overstreet168f4c52019-06-24 18:24:38 -0400524 bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800525
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400526 ret = commit_do(trans, NULL, NULL,
Kent Overstreetcb52d23e2023-11-11 16:31:50 -0500527 BCH_TRANS_COMMIT_no_enospc,
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400528 bch2_unlink_trans(trans,
Kent Overstreet2d330362023-03-16 11:04:28 -0400529 inode_inum(dir), &dir_u,
530 &inode_u, &dentry->d_name,
531 deleting_snapshot));
532 if (unlikely(ret))
533 goto err;
Kent Overstreet58677a12019-10-01 16:51:57 -0400534
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400535 bch2_inode_update_after_write(trans, dir, &dir_u,
Kent Overstreet2d330362023-03-16 11:04:28 -0400536 ATTR_MTIME|ATTR_CTIME);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400537 bch2_inode_update_after_write(trans, inode, &inode_u,
Kent Overstreet2d330362023-03-16 11:04:28 -0400538 ATTR_MTIME);
539
540 if (inode_u.bi_subvol) {
541 /*
542 * Subvolume deletion is asynchronous, but we still want to tell
543 * the VFS that it's been deleted here:
544 */
545 set_nlink(&inode->v, 0);
Kent Overstreet96385742019-10-02 18:35:36 -0400546 }
Kent Overstreet2d330362023-03-16 11:04:28 -0400547err:
Kent Overstreet168f4c52019-06-24 18:24:38 -0400548 bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400549 bch2_trans_put(trans);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800550
551 return ret;
552}
553
Kent Overstreet42d23732021-03-16 23:28:43 -0400554static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
555{
Kent Overstreet0d72ab32023-12-29 21:16:32 -0500556 struct bch_inode_info *dir= to_bch_ei(vdir);
557 struct bch_fs *c = dir->v.i_sb->s_fs_info;
558
Kent Overstreet1a1c93e2024-02-09 18:34:05 -0500559 int ret = bch2_subvol_is_ro(c, dir->ei_subvol) ?:
Kent Overstreet0d72ab32023-12-29 21:16:32 -0500560 __bch2_unlink(vdir, dentry, false);
Kent Overstreet1a1c93e2024-02-09 18:34:05 -0500561 return bch2_err_class(ret);
Kent Overstreet42d23732021-03-16 23:28:43 -0400562}
563
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800564static int bch2_symlink(struct mnt_idmap *idmap,
565 struct inode *vdir, struct dentry *dentry,
566 const char *symname)
567{
568 struct bch_fs *c = vdir->i_sb->s_fs_info;
569 struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
570 int ret;
571
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400572 inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
Kent Overstreet42d23732021-03-16 23:28:43 -0400573 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
Kent Overstreet3e3e02e2022-10-19 18:31:33 -0400574 if (IS_ERR(inode))
Kent Overstreet5c1ef832022-09-18 15:43:50 -0400575 return bch2_err_class(PTR_ERR(inode));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800576
577 inode_lock(&inode->v);
578 ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
579 inode_unlock(&inode->v);
580
581 if (unlikely(ret))
582 goto err;
583
584 ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
585 if (unlikely(ret))
586 goto err;
587
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800588 ret = __bch2_link(c, inode, dir, dentry);
589 if (unlikely(ret))
590 goto err;
591
592 d_instantiate(dentry, &inode->v);
593 return 0;
594err:
595 iput(&inode->v);
Kent Overstreet1a1c93e2024-02-09 18:34:05 -0500596 return bch2_err_class(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800597}
598
599static int bch2_mkdir(struct mnt_idmap *idmap,
600 struct inode *vdir, struct dentry *dentry, umode_t mode)
601{
Kent Overstreet821a99b2019-10-11 15:14:36 -0400602 return bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800603}
604
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800605static int bch2_rename2(struct mnt_idmap *idmap,
606 struct inode *src_vdir, struct dentry *src_dentry,
607 struct inode *dst_vdir, struct dentry *dst_dentry,
608 unsigned flags)
609{
610 struct bch_fs *c = src_vdir->i_sb->s_fs_info;
Kent Overstreet96385742019-10-02 18:35:36 -0400611 struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
612 struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
613 struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
614 struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800615 struct bch_inode_unpacked dst_dir_u, src_dir_u;
616 struct bch_inode_unpacked src_inode_u, dst_inode_u;
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400617 struct btree_trans *trans;
Kent Overstreet96385742019-10-02 18:35:36 -0400618 enum bch_rename_mode mode = flags & RENAME_EXCHANGE
619 ? BCH_RENAME_EXCHANGE
620 : dst_dentry->d_inode
621 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800622 int ret;
623
624 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
625 return -EINVAL;
626
Kent Overstreet96385742019-10-02 18:35:36 -0400627 if (mode == BCH_RENAME_OVERWRITE) {
628 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800629 0, LLONG_MAX);
630 if (ret)
631 return ret;
632 }
633
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400634 trans = bch2_trans_get(c);
Kent Overstreet20bceec2019-05-15 10:54:43 -0400635
Kent Overstreet168f4c52019-06-24 18:24:38 -0400636 bch2_lock_inodes(INODE_UPDATE_LOCK,
Kent Overstreet96385742019-10-02 18:35:36 -0400637 src_dir,
638 dst_dir,
639 src_inode,
640 dst_inode);
Kent Overstreet19ee5f22018-07-20 22:23:42 -0400641
Kent Overstreet0d72ab32023-12-29 21:16:32 -0500642 ret = bch2_subvol_is_ro_trans(trans, src_dir->ei_subvol) ?:
643 bch2_subvol_is_ro_trans(trans, dst_dir->ei_subvol);
644 if (ret)
645 goto err;
646
Kent Overstreet96385742019-10-02 18:35:36 -0400647 if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
648 ret = bch2_fs_quota_transfer(c, src_inode,
649 dst_dir->ei_qid,
Kent Overstreet96012e12018-12-17 05:31:49 -0500650 1 << QTYP_PRJ,
651 KEY_TYPE_QUOTA_PREALLOC);
652 if (ret)
653 goto err;
654 }
655
Kent Overstreet96385742019-10-02 18:35:36 -0400656 if (mode == BCH_RENAME_EXCHANGE &&
657 inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
658 ret = bch2_fs_quota_transfer(c, dst_inode,
659 src_dir->ei_qid,
Kent Overstreet96012e12018-12-17 05:31:49 -0500660 1 << QTYP_PRJ,
661 KEY_TYPE_QUOTA_PREALLOC);
662 if (ret)
663 goto err;
664 }
665
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400666 ret = commit_do(trans, NULL, NULL, 0,
667 bch2_rename_trans(trans,
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400668 inode_inum(src_dir), &src_dir_u,
669 inode_inum(dst_dir), &dst_dir_u,
Kent Overstreet5f0e4ae12021-03-12 17:52:42 -0500670 &src_inode_u,
671 &dst_inode_u,
672 &src_dentry->d_name,
673 &dst_dentry->d_name,
674 mode));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800675 if (unlikely(ret))
676 goto err;
677
Kent Overstreet96385742019-10-02 18:35:36 -0400678 BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
679 BUG_ON(dst_inode &&
680 dst_inode->v.i_ino != dst_inode_u.bi_inum);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800681
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400682 bch2_inode_update_after_write(trans, src_dir, &src_dir_u,
Kent Overstreet96385742019-10-02 18:35:36 -0400683 ATTR_MTIME|ATTR_CTIME);
Kent Overstreet96385742019-10-02 18:35:36 -0400684
Kent Overstreet68a20542021-11-05 15:17:13 -0400685 if (src_dir != dst_dir)
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400686 bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800687 ATTR_MTIME|ATTR_CTIME);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800688
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400689 bch2_inode_update_after_write(trans, src_inode, &src_inode_u,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800690 ATTR_CTIME);
Kent Overstreet96385742019-10-02 18:35:36 -0400691
Kent Overstreet68a20542021-11-05 15:17:13 -0400692 if (dst_inode)
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400693 bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800694 ATTR_CTIME);
695err:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400696 bch2_trans_put(trans);
Kent Overstreet96012e12018-12-17 05:31:49 -0500697
Kent Overstreet96385742019-10-02 18:35:36 -0400698 bch2_fs_quota_transfer(c, src_inode,
699 bch_qid(&src_inode->ei_inode),
Kent Overstreet96012e12018-12-17 05:31:49 -0500700 1 << QTYP_PRJ,
701 KEY_TYPE_QUOTA_NOCHECK);
Kent Overstreet96385742019-10-02 18:35:36 -0400702 if (dst_inode)
703 bch2_fs_quota_transfer(c, dst_inode,
704 bch_qid(&dst_inode->ei_inode),
Kent Overstreet96012e12018-12-17 05:31:49 -0500705 1 << QTYP_PRJ,
706 KEY_TYPE_QUOTA_NOCHECK);
707
Kent Overstreet168f4c52019-06-24 18:24:38 -0400708 bch2_unlock_inodes(INODE_UPDATE_LOCK,
Kent Overstreet96385742019-10-02 18:35:36 -0400709 src_dir,
710 dst_dir,
711 src_inode,
712 dst_inode);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800713
Kent Overstreet1a1c93e2024-02-09 18:34:05 -0500714 return bch2_err_class(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800715}
716
Kent Overstreet58677a12019-10-01 16:51:57 -0400717static void bch2_setattr_copy(struct mnt_idmap *idmap,
718 struct bch_inode_info *inode,
719 struct bch_inode_unpacked *bi,
720 struct iattr *attr)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800721{
722 struct bch_fs *c = inode->v.i_sb->s_fs_info;
Kent Overstreet58677a12019-10-01 16:51:57 -0400723 unsigned int ia_valid = attr->ia_valid;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800724
725 if (ia_valid & ATTR_UID)
Kent Overstreet58677a12019-10-01 16:51:57 -0400726 bi->bi_uid = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800727 if (ia_valid & ATTR_GID)
Kent Overstreet58677a12019-10-01 16:51:57 -0400728 bi->bi_gid = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800729
Kent Overstreet68a507a2021-06-14 22:29:54 -0400730 if (ia_valid & ATTR_SIZE)
731 bi->bi_size = attr->ia_size;
732
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800733 if (ia_valid & ATTR_ATIME)
Kent Overstreet58677a12019-10-01 16:51:57 -0400734 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800735 if (ia_valid & ATTR_MTIME)
Kent Overstreet58677a12019-10-01 16:51:57 -0400736 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800737 if (ia_valid & ATTR_CTIME)
Kent Overstreet58677a12019-10-01 16:51:57 -0400738 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800739
740 if (ia_valid & ATTR_MODE) {
Kent Overstreet58677a12019-10-01 16:51:57 -0400741 umode_t mode = attr->ia_mode;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800742 kgid_t gid = ia_valid & ATTR_GID
Kent Overstreet58677a12019-10-01 16:51:57 -0400743 ? attr->ia_gid
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800744 : inode->v.i_gid;
745
746 if (!in_group_p(gid) &&
Kent Overstreet58677a12019-10-01 16:51:57 -0400747 !capable_wrt_inode_uidgid(idmap, &inode->v, CAP_FSETID))
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800748 mode &= ~S_ISGID;
749 bi->bi_mode = mode;
750 }
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800751}
752
Kent Overstreet68a507a2021-06-14 22:29:54 -0400753int bch2_setattr_nonsize(struct mnt_idmap *idmap,
754 struct bch_inode_info *inode,
755 struct iattr *attr)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800756{
757 struct bch_fs *c = inode->v.i_sb->s_fs_info;
Kent Overstreet0f5254a2018-12-17 05:43:00 -0500758 struct bch_qid qid;
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400759 struct btree_trans *trans;
Kent Overstreet67e0dd82021-08-30 15:18:31 -0400760 struct btree_iter inode_iter = { NULL };
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800761 struct bch_inode_unpacked inode_u;
762 struct posix_acl *acl = NULL;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800763 int ret;
764
765 mutex_lock(&inode->ei_update_lock);
766
Kent Overstreet0f5254a2018-12-17 05:43:00 -0500767 qid = inode->ei_qid;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800768
Kent Overstreet58677a12019-10-01 16:51:57 -0400769 if (attr->ia_valid & ATTR_UID)
770 qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
Kent Overstreet0f5254a2018-12-17 05:43:00 -0500771
Kent Overstreet58677a12019-10-01 16:51:57 -0400772 if (attr->ia_valid & ATTR_GID)
773 qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800774
Kent Overstreet0f5254a2018-12-17 05:43:00 -0500775 ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
776 KEY_TYPE_QUOTA_PREALLOC);
777 if (ret)
778 goto err;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800779
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400780 trans = bch2_trans_get(c);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800781retry:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400782 bch2_trans_begin(trans);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800783 kfree(acl);
784 acl = NULL;
785
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400786 ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode),
Kent Overstreet67e0dd82021-08-30 15:18:31 -0400787 BTREE_ITER_INTENT);
Kent Overstreet58677a12019-10-01 16:51:57 -0400788 if (ret)
789 goto btree_err;
790
791 bch2_setattr_copy(idmap, inode, &inode_u, attr);
792
793 if (attr->ia_valid & ATTR_MODE) {
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400794 ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u,
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400795 inode_u.bi_mode, &acl);
Kent Overstreet58677a12019-10-01 16:51:57 -0400796 if (ret)
797 goto btree_err;
798 }
799
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400800 ret = bch2_inode_write(trans, &inode_iter, &inode_u) ?:
801 bch2_trans_commit(trans, NULL, NULL,
Kent Overstreetcb52d23e2023-11-11 16:31:50 -0500802 BCH_TRANS_COMMIT_no_enospc);
Kent Overstreet58677a12019-10-01 16:51:57 -0400803btree_err:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400804 bch2_trans_iter_exit(trans, &inode_iter);
Kent Overstreet50dc0f62021-03-19 20:29:11 -0400805
Kent Overstreet549d1732022-07-17 23:06:38 -0400806 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800807 goto retry;
808 if (unlikely(ret))
809 goto err_trans;
810
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400811 bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800812
813 if (acl)
814 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
815err_trans:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400816 bch2_trans_put(trans);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800817err:
818 mutex_unlock(&inode->ei_update_lock);
819
Kent Overstreet5c1ef832022-09-18 15:43:50 -0400820 return bch2_err_class(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800821}
822
823static int bch2_getattr(struct mnt_idmap *idmap,
824 const struct path *path, struct kstat *stat,
825 u32 request_mask, unsigned query_flags)
826{
827 struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
828 struct bch_fs *c = inode->v.i_sb->s_fs_info;
829
830 stat->dev = inode->v.i_sb->s_dev;
831 stat->ino = inode->v.i_ino;
832 stat->mode = inode->v.i_mode;
833 stat->nlink = inode->v.i_nlink;
834 stat->uid = inode->v.i_uid;
835 stat->gid = inode->v.i_gid;
836 stat->rdev = inode->v.i_rdev;
837 stat->size = i_size_read(&inode->v);
Linus Torvalds9e877052023-10-30 11:09:38 -1000838 stat->atime = inode_get_atime(&inode->v);
839 stat->mtime = inode_get_mtime(&inode->v);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800840 stat->ctime = inode_get_ctime(&inode->v);
841 stat->blksize = block_bytes(c);
842 stat->blocks = inode->v.i_blocks;
843
844 if (request_mask & STATX_BTIME) {
845 stat->result_mask |= STATX_BTIME;
846 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
847 }
848
Kent Overstreet103ffe92023-11-02 11:42:48 -0400849 if (inode->ei_inode.bi_flags & BCH_INODE_immutable)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800850 stat->attributes |= STATX_ATTR_IMMUTABLE;
Kent Overstreet4a1d8d32019-11-06 14:29:30 -0500851 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
852
Kent Overstreet103ffe92023-11-02 11:42:48 -0400853 if (inode->ei_inode.bi_flags & BCH_INODE_append)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800854 stat->attributes |= STATX_ATTR_APPEND;
Kent Overstreet4a1d8d32019-11-06 14:29:30 -0500855 stat->attributes_mask |= STATX_ATTR_APPEND;
856
Kent Overstreet103ffe92023-11-02 11:42:48 -0400857 if (inode->ei_inode.bi_flags & BCH_INODE_nodump)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800858 stat->attributes |= STATX_ATTR_NODUMP;
Kent Overstreet4a1d8d32019-11-06 14:29:30 -0500859 stat->attributes_mask |= STATX_ATTR_NODUMP;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800860
861 return 0;
862}
863
864static int bch2_setattr(struct mnt_idmap *idmap,
865 struct dentry *dentry, struct iattr *iattr)
866{
867 struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
Kent Overstreet0d72ab32023-12-29 21:16:32 -0500868 struct bch_fs *c = inode->v.i_sb->s_fs_info;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800869 int ret;
870
871 lockdep_assert_held(&inode->v.i_rwsem);
872
Kent Overstreet0d72ab32023-12-29 21:16:32 -0500873 ret = bch2_subvol_is_ro(c, inode->ei_subvol) ?:
874 setattr_prepare(idmap, dentry, iattr);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800875 if (ret)
876 return ret;
877
878 return iattr->ia_valid & ATTR_SIZE
Kent Overstreet5902cc22023-09-04 05:38:30 -0400879 ? bchfs_truncate(idmap, inode, iattr)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800880 : bch2_setattr_nonsize(idmap, inode, iattr);
881}
882
883static int bch2_tmpfile(struct mnt_idmap *idmap,
884 struct inode *vdir, struct file *file, umode_t mode)
885{
886 struct bch_inode_info *inode =
887 __bch2_create(idmap, to_bch_ei(vdir),
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400888 file->f_path.dentry, mode, 0,
Kent Overstreet42d23732021-03-16 23:28:43 -0400889 (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800890
891 if (IS_ERR(inode))
Kent Overstreet5c1ef832022-09-18 15:43:50 -0400892 return bch2_err_class(PTR_ERR(inode));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800893
894 d_mark_tmpfile(file, &inode->v);
895 d_instantiate(file->f_path.dentry, &inode->v);
896 return finish_open_simple(file, 0);
897}
898
Kent Overstreet5b6d40e2019-06-29 17:59:21 -0400899static int bch2_fill_extent(struct bch_fs *c,
900 struct fiemap_extent_info *info,
Kent Overstreet99aaf572019-07-25 13:52:14 -0400901 struct bkey_s_c k, unsigned flags)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800902{
Kent Overstreete7b854b2020-10-26 17:03:28 -0400903 if (bkey_extent_is_direct_data(k.k)) {
Kent Overstreet99aaf572019-07-25 13:52:14 -0400904 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
Kent Overstreet17422372018-09-27 21:08:39 -0400905 const union bch_extent_entry *entry;
906 struct extent_ptr_decoded p;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800907 int ret;
908
Kent Overstreet76426092019-08-16 09:59:56 -0400909 if (k.k->type == KEY_TYPE_reflink_v)
910 flags |= FIEMAP_EXTENT_SHARED;
911
Kent Overstreet99aaf572019-07-25 13:52:14 -0400912 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800913 int flags2 = 0;
Kent Overstreet17422372018-09-27 21:08:39 -0400914 u64 offset = p.ptr.offset;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800915
Kent Overstreet79203112022-11-13 18:59:01 -0500916 if (p.ptr.unwritten)
917 flags2 |= FIEMAP_EXTENT_UNWRITTEN;
918
Kent Overstreet17422372018-09-27 21:08:39 -0400919 if (p.crc.compression_type)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800920 flags2 |= FIEMAP_EXTENT_ENCODED;
921 else
Kent Overstreet17422372018-09-27 21:08:39 -0400922 offset += p.crc.offset;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800923
Kent Overstreet8244f322021-12-14 14:24:41 -0500924 if ((offset & (block_sectors(c) - 1)) ||
925 (k.k->size & (block_sectors(c) - 1)))
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800926 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
927
928 ret = fiemap_fill_next_extent(info,
Kent Overstreet99aaf572019-07-25 13:52:14 -0400929 bkey_start_offset(k.k) << 9,
Kent Overstreet17422372018-09-27 21:08:39 -0400930 offset << 9,
Kent Overstreet99aaf572019-07-25 13:52:14 -0400931 k.k->size << 9, flags|flags2);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800932 if (ret)
933 return ret;
934 }
935
936 return 0;
Kent Overstreete7b854b2020-10-26 17:03:28 -0400937 } else if (bkey_extent_is_inline_data(k.k)) {
938 return fiemap_fill_next_extent(info,
939 bkey_start_offset(k.k) << 9,
940 0, k.k->size << 9,
941 flags|
942 FIEMAP_EXTENT_DATA_INLINE);
Kent Overstreet99aaf572019-07-25 13:52:14 -0400943 } else if (k.k->type == KEY_TYPE_reservation) {
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800944 return fiemap_fill_next_extent(info,
Kent Overstreet99aaf572019-07-25 13:52:14 -0400945 bkey_start_offset(k.k) << 9,
946 0, k.k->size << 9,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800947 flags|
948 FIEMAP_EXTENT_DELALLOC|
949 FIEMAP_EXTENT_UNWRITTEN);
950 } else {
951 BUG();
952 }
953}
954
955static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
956 u64 start, u64 len)
957{
958 struct bch_fs *c = vinode->i_sb->s_fs_info;
959 struct bch_inode_info *ei = to_bch_ei(vinode);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400960 struct btree_trans *trans;
Kent Overstreet67e0dd82021-08-30 15:18:31 -0400961 struct btree_iter iter;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800962 struct bkey_s_c k;
Kent Overstreet07a10062020-12-17 15:08:58 -0500963 struct bkey_buf cur, prev;
Kent Overstreet7d5224f2019-08-22 16:12:28 -0400964 struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
Kent Overstreet76426092019-08-16 09:59:56 -0400965 unsigned offset_into_extent, sectors;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800966 bool have_extent = false;
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400967 u32 snapshot;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -0800968 int ret = 0;
969
970 ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
971 if (ret)
972 return ret;
973
974 if (start + len < start)
975 return -EINVAL;
976
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400977 start >>= 9;
978
Kent Overstreet07a10062020-12-17 15:08:58 -0500979 bch2_bkey_buf_init(&cur);
980 bch2_bkey_buf_init(&prev);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400981 trans = bch2_trans_get(c);
Kent Overstreet7d5224f2019-08-22 16:12:28 -0400982retry:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400983 bch2_trans_begin(trans);
Kent Overstreet700c25b2021-07-24 20:24:10 -0400984
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400985 ret = bch2_subvolume_get_snapshot(trans, ei->ei_subvol, &snapshot);
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400986 if (ret)
987 goto err;
988
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400989 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
Kent Overstreet6fed42b2021-03-16 00:28:17 -0400990 SPOS(ei->v.i_ino, start, snapshot), 0);
991
Kent Overstreet6bd68ec2023-09-12 17:16:02 -0400992 while (!(ret = btree_trans_too_many_iters(trans)) &&
Kent Overstreet85d8cf12022-03-11 12:31:52 -0500993 (k = bch2_btree_iter_peek_upto(&iter, end)).k &&
994 !(ret = bkey_err(k))) {
Kent Overstreet5ff75cc2021-03-14 21:30:08 -0400995 enum btree_id data_btree = BTREE_ID_extents;
996
Kent Overstreet7d5224f2019-08-22 16:12:28 -0400997 if (!bkey_extent_is_data(k.k) &&
998 k.k->type != KEY_TYPE_reservation) {
Kent Overstreet67e0dd82021-08-30 15:18:31 -0400999 bch2_btree_iter_advance(&iter);
Kent Overstreet7d5224f2019-08-22 16:12:28 -04001000 continue;
1001 }
Kent Overstreet99aaf572019-07-25 13:52:14 -04001002
Kent Overstreet67e0dd82021-08-30 15:18:31 -04001003 offset_into_extent = iter.pos.offset -
Kent Overstreet76426092019-08-16 09:59:56 -04001004 bkey_start_offset(k.k);
1005 sectors = k.k->size - offset_into_extent;
1006
Kent Overstreet07a10062020-12-17 15:08:58 -05001007 bch2_bkey_buf_reassemble(&cur, c, k);
Kent Overstreet13dcd4a2020-10-24 20:56:47 -04001008
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001009 ret = bch2_read_indirect_extent(trans, &data_btree,
Yuxuan Shui22d8a332020-05-22 15:50:05 +01001010 &offset_into_extent, &cur);
Kent Overstreet76426092019-08-16 09:59:56 -04001011 if (ret)
1012 break;
1013
Kent Overstreet13dcd4a2020-10-24 20:56:47 -04001014 k = bkey_i_to_s_c(cur.k);
Kent Overstreet07a10062020-12-17 15:08:58 -05001015 bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
Kent Overstreet13dcd4a2020-10-24 20:56:47 -04001016
Kent Overstreet76426092019-08-16 09:59:56 -04001017 sectors = min(sectors, k.k->size - offset_into_extent);
1018
Kent Overstreete7b854b2020-10-26 17:03:28 -04001019 bch2_cut_front(POS(k.k->p.inode,
1020 bkey_start_offset(k.k) +
1021 offset_into_extent),
1022 cur.k);
Kent Overstreet35189e02019-11-09 16:01:15 -05001023 bch2_key_resize(&cur.k->k, sectors);
Kent Overstreet67e0dd82021-08-30 15:18:31 -04001024 cur.k->k.p = iter.pos;
Kent Overstreet35189e02019-11-09 16:01:15 -05001025 cur.k->k.p.offset += cur.k->k.size;
Kent Overstreet76426092019-08-16 09:59:56 -04001026
Kent Overstreet7d5224f2019-08-22 16:12:28 -04001027 if (have_extent) {
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001028 bch2_trans_unlock(trans);
Kent Overstreet7d5224f2019-08-22 16:12:28 -04001029 ret = bch2_fill_extent(c, info,
Kent Overstreet35189e02019-11-09 16:01:15 -05001030 bkey_i_to_s_c(prev.k), 0);
Kent Overstreet7d5224f2019-08-22 16:12:28 -04001031 if (ret)
1032 break;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001033 }
Kent Overstreet76426092019-08-16 09:59:56 -04001034
Kent Overstreet35189e02019-11-09 16:01:15 -05001035 bkey_copy(prev.k, cur.k);
Kent Overstreet7d5224f2019-08-22 16:12:28 -04001036 have_extent = true;
1037
Kent Overstreet67e0dd82021-08-30 15:18:31 -04001038 bch2_btree_iter_set_pos(&iter,
1039 POS(iter.pos.inode, iter.pos.offset + sectors));
Kent Overstreet99aaf572019-07-25 13:52:14 -04001040 }
Kent Overstreet6fed42b2021-03-16 00:28:17 -04001041 start = iter.pos.offset;
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001042 bch2_trans_iter_exit(trans, &iter);
Kent Overstreet6fed42b2021-03-16 00:28:17 -04001043err:
Kent Overstreet549d1732022-07-17 23:06:38 -04001044 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
Kent Overstreet7d5224f2019-08-22 16:12:28 -04001045 goto retry;
1046
Kent Overstreeta83e1082023-06-19 21:12:05 -04001047 if (!ret && have_extent) {
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001048 bch2_trans_unlock(trans);
Kent Overstreet35189e02019-11-09 16:01:15 -05001049 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
Kent Overstreet99aaf572019-07-25 13:52:14 -04001050 FIEMAP_EXTENT_LAST);
Kent Overstreeta83e1082023-06-19 21:12:05 -04001051 }
Kent Overstreet7d5224f2019-08-22 16:12:28 -04001052
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001053 bch2_trans_put(trans);
Kent Overstreet07a10062020-12-17 15:08:58 -05001054 bch2_bkey_buf_exit(&cur, c);
1055 bch2_bkey_buf_exit(&prev, c);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001056 return ret < 0 ? ret : 0;
1057}
1058
1059static const struct vm_operations_struct bch_vm_ops = {
1060 .fault = bch2_page_fault,
1061 .map_pages = filemap_map_pages,
1062 .page_mkwrite = bch2_page_mkwrite,
1063};
1064
1065static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
1066{
1067 file_accessed(file);
1068
1069 vma->vm_ops = &bch_vm_ops;
1070 return 0;
1071}
1072
1073/* Directories: */
1074
1075static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
1076{
1077 return generic_file_llseek_size(file, offset, whence,
1078 S64_MAX, S64_MAX);
1079}
1080
1081static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
1082{
Kent Overstreet96385742019-10-02 18:35:36 -04001083 struct bch_inode_info *inode = file_bch_inode(file);
1084 struct bch_fs *c = inode->v.i_sb->s_fs_info;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001085
Kent Overstreet96385742019-10-02 18:35:36 -04001086 if (!dir_emit_dots(file, ctx))
1087 return 0;
1088
Kent Overstreetcf904c82023-12-16 22:43:41 -05001089 int ret = bch2_readdir(c, inode_inum(inode), ctx);
Kent Overstreete691b392023-08-06 10:04:05 -04001090
Kent Overstreetcf904c82023-12-16 22:43:41 -05001091 bch_err_fn(c, ret);
Kent Overstreete691b392023-08-06 10:04:05 -04001092 return bch2_err_class(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001093}
1094
Kent Overstreet0d72ab32023-12-29 21:16:32 -05001095static int bch2_open(struct inode *vinode, struct file *file)
1096{
1097 if (file->f_flags & (O_WRONLY|O_RDWR)) {
1098 struct bch_inode_info *inode = to_bch_ei(vinode);
1099 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1100
1101 int ret = bch2_subvol_is_ro(c, inode->ei_subvol);
1102 if (ret)
1103 return ret;
1104 }
1105
1106 return generic_file_open(vinode, file);
1107}
1108
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001109static const struct file_operations bch_file_operations = {
Kent Overstreet0d72ab32023-12-29 21:16:32 -05001110 .open = bch2_open,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001111 .llseek = bch2_llseek,
1112 .read_iter = bch2_read_iter,
1113 .write_iter = bch2_write_iter,
1114 .mmap = bch2_mmap,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001115 .fsync = bch2_fsync,
1116 .splice_read = filemap_splice_read,
1117 .splice_write = iter_file_splice_write,
1118 .fallocate = bch2_fallocate_dispatch,
1119 .unlocked_ioctl = bch2_fs_file_ioctl,
1120#ifdef CONFIG_COMPAT
1121 .compat_ioctl = bch2_compat_fs_ioctl,
1122#endif
Kent Overstreet76426092019-08-16 09:59:56 -04001123 .remap_file_range = bch2_remap_file_range,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001124};
1125
1126static const struct inode_operations bch_file_inode_operations = {
1127 .getattr = bch2_getattr,
1128 .setattr = bch2_setattr,
1129 .fiemap = bch2_fiemap,
1130 .listxattr = bch2_xattr_list,
1131#ifdef CONFIG_BCACHEFS_POSIX_ACL
1132 .get_acl = bch2_get_acl,
1133 .set_acl = bch2_set_acl,
1134#endif
1135};
1136
1137static const struct inode_operations bch_dir_inode_operations = {
1138 .lookup = bch2_lookup,
1139 .create = bch2_create,
1140 .link = bch2_link,
1141 .unlink = bch2_unlink,
1142 .symlink = bch2_symlink,
1143 .mkdir = bch2_mkdir,
Kent Overstreet821a99b2019-10-11 15:14:36 -04001144 .rmdir = bch2_unlink,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001145 .mknod = bch2_mknod,
1146 .rename = bch2_rename2,
1147 .getattr = bch2_getattr,
1148 .setattr = bch2_setattr,
1149 .tmpfile = bch2_tmpfile,
1150 .listxattr = bch2_xattr_list,
1151#ifdef CONFIG_BCACHEFS_POSIX_ACL
1152 .get_acl = bch2_get_acl,
1153 .set_acl = bch2_set_acl,
1154#endif
1155};
1156
1157static const struct file_operations bch_dir_file_operations = {
1158 .llseek = bch2_dir_llseek,
1159 .read = generic_read_dir,
1160 .iterate_shared = bch2_vfs_readdir,
1161 .fsync = bch2_fsync,
1162 .unlocked_ioctl = bch2_fs_file_ioctl,
1163#ifdef CONFIG_COMPAT
1164 .compat_ioctl = bch2_compat_fs_ioctl,
1165#endif
1166};
1167
1168static const struct inode_operations bch_symlink_inode_operations = {
1169 .get_link = page_get_link,
1170 .getattr = bch2_getattr,
1171 .setattr = bch2_setattr,
1172 .listxattr = bch2_xattr_list,
1173#ifdef CONFIG_BCACHEFS_POSIX_ACL
1174 .get_acl = bch2_get_acl,
1175 .set_acl = bch2_set_acl,
1176#endif
1177};
1178
1179static const struct inode_operations bch_special_inode_operations = {
1180 .getattr = bch2_getattr,
1181 .setattr = bch2_setattr,
1182 .listxattr = bch2_xattr_list,
1183#ifdef CONFIG_BCACHEFS_POSIX_ACL
1184 .get_acl = bch2_get_acl,
1185 .set_acl = bch2_set_acl,
1186#endif
1187};
1188
1189static const struct address_space_operations bch_address_space_operations = {
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001190 .read_folio = bch2_read_folio,
1191 .writepages = bch2_writepages,
1192 .readahead = bch2_readahead,
Kent Overstreete1036a22019-07-02 14:59:15 -04001193 .dirty_folio = filemap_dirty_folio,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001194 .write_begin = bch2_write_begin,
1195 .write_end = bch2_write_end,
1196 .invalidate_folio = bch2_invalidate_folio,
1197 .release_folio = bch2_release_folio,
1198 .direct_IO = noop_direct_IO,
1199#ifdef CONFIG_MIGRATION
1200 .migrate_folio = filemap_migrate_folio,
1201#endif
Matthew Wilcox (Oracle)af7628d2023-11-17 16:14:47 +00001202 .error_remove_folio = generic_error_remove_folio,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001203};
1204
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001205struct bcachefs_fid {
1206 u64 inum;
1207 u32 subvol;
1208 u32 gen;
1209} __packed;
1210
1211struct bcachefs_fid_with_parent {
1212 struct bcachefs_fid fid;
1213 struct bcachefs_fid dir;
1214} __packed;
1215
1216static int bcachefs_fid_valid(int fh_len, int fh_type)
1217{
1218 switch (fh_type) {
1219 case FILEID_BCACHEFS_WITHOUT_PARENT:
1220 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
1221 case FILEID_BCACHEFS_WITH_PARENT:
1222 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
1223 default:
1224 return false;
1225 }
1226}
1227
1228static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
1229{
1230 return (struct bcachefs_fid) {
1231 .inum = inode->ei_inode.bi_inum,
1232 .subvol = inode->ei_subvol,
1233 .gen = inode->ei_inode.bi_generation,
1234 };
1235}
1236
1237static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
1238 struct inode *vdir)
1239{
1240 struct bch_inode_info *inode = to_bch_ei(vinode);
1241 struct bch_inode_info *dir = to_bch_ei(vdir);
Jan Kara8bf77192023-12-13 17:51:04 +01001242 int min_len;
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001243
1244 if (!S_ISDIR(inode->v.i_mode) && dir) {
1245 struct bcachefs_fid_with_parent *fid = (void *) fh;
1246
Jan Kara8bf77192023-12-13 17:51:04 +01001247 min_len = sizeof(*fid) / sizeof(u32);
1248 if (*len < min_len) {
1249 *len = min_len;
1250 return FILEID_INVALID;
1251 }
1252
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001253 fid->fid = bch2_inode_to_fid(inode);
1254 fid->dir = bch2_inode_to_fid(dir);
1255
Jan Kara8bf77192023-12-13 17:51:04 +01001256 *len = min_len;
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001257 return FILEID_BCACHEFS_WITH_PARENT;
1258 } else {
1259 struct bcachefs_fid *fid = (void *) fh;
1260
Jan Kara8bf77192023-12-13 17:51:04 +01001261 min_len = sizeof(*fid) / sizeof(u32);
1262 if (*len < min_len) {
1263 *len = min_len;
1264 return FILEID_INVALID;
1265 }
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001266 *fid = bch2_inode_to_fid(inode);
1267
Jan Kara8bf77192023-12-13 17:51:04 +01001268 *len = min_len;
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001269 return FILEID_BCACHEFS_WITHOUT_PARENT;
1270 }
1271}
1272
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001273static struct inode *bch2_nfs_get_inode(struct super_block *sb,
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001274 struct bcachefs_fid fid)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001275{
1276 struct bch_fs *c = sb->s_fs_info;
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001277 struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
1278 .subvol = fid.subvol,
1279 .inum = fid.inum,
1280 });
1281 if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001282 iput(vinode);
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001283 vinode = ERR_PTR(-ESTALE);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001284 }
1285 return vinode;
1286}
1287
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001288static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001289 int fh_len, int fh_type)
1290{
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001291 struct bcachefs_fid *fid = (void *) _fid;
1292
1293 if (!bcachefs_fid_valid(fh_len, fh_type))
1294 return NULL;
1295
1296 return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001297}
1298
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001299static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001300 int fh_len, int fh_type)
1301{
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001302 struct bcachefs_fid_with_parent *fid = (void *) _fid;
1303
1304 if (!bcachefs_fid_valid(fh_len, fh_type) ||
1305 fh_type != FILEID_BCACHEFS_WITH_PARENT)
1306 return NULL;
1307
1308 return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001309}
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001310
1311static struct dentry *bch2_get_parent(struct dentry *child)
1312{
1313 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1314 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1315 subvol_inum parent_inum = {
1316 .subvol = inode->ei_inode.bi_parent_subvol ?:
1317 inode->ei_subvol,
1318 .inum = inode->ei_inode.bi_dir,
1319 };
1320
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001321 return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
1322}
1323
1324static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
1325{
1326 struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1327 struct bch_inode_info *dir = to_bch_ei(parent->d_inode);
1328 struct bch_fs *c = inode->v.i_sb->s_fs_info;
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001329 struct btree_trans *trans;
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001330 struct btree_iter iter1;
1331 struct btree_iter iter2;
1332 struct bkey_s_c k;
1333 struct bkey_s_c_dirent d;
1334 struct bch_inode_unpacked inode_u;
1335 subvol_inum target;
1336 u32 snapshot;
Joshua Ashton01a7e742023-08-12 22:26:29 +01001337 struct qstr dirent_name;
1338 unsigned name_len = 0;
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001339 int ret;
1340
1341 if (!S_ISDIR(dir->v.i_mode))
1342 return -EINVAL;
1343
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001344 trans = bch2_trans_get(c);
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001345
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001346 bch2_trans_iter_init(trans, &iter1, BTREE_ID_dirents,
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001347 POS(dir->ei_inode.bi_inum, 0), 0);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001348 bch2_trans_iter_init(trans, &iter2, BTREE_ID_dirents,
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001349 POS(dir->ei_inode.bi_inum, 0), 0);
1350retry:
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001351 bch2_trans_begin(trans);
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001352
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001353 ret = bch2_subvolume_get_snapshot(trans, dir->ei_subvol, &snapshot);
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001354 if (ret)
1355 goto err;
1356
1357 bch2_btree_iter_set_snapshot(&iter1, snapshot);
1358 bch2_btree_iter_set_snapshot(&iter2, snapshot);
1359
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001360 ret = bch2_inode_find_by_inum_trans(trans, inode_inum(inode), &inode_u);
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001361 if (ret)
1362 goto err;
1363
1364 if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
1365 bch2_btree_iter_set_pos(&iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
1366
1367 k = bch2_btree_iter_peek_slot(&iter1);
1368 ret = bkey_err(k);
1369 if (ret)
1370 goto err;
1371
1372 if (k.k->type != KEY_TYPE_dirent) {
Kent Overstreete47a3902023-05-27 19:59:59 -04001373 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001374 goto err;
1375 }
1376
1377 d = bkey_s_c_to_dirent(k);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001378 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001379 if (ret > 0)
Kent Overstreete47a3902023-05-27 19:59:59 -04001380 ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001381 if (ret)
1382 goto err;
1383
1384 if (target.subvol == inode->ei_subvol &&
1385 target.inum == inode->ei_inode.bi_inum)
1386 goto found;
1387 } else {
1388 /*
1389 * File with multiple hardlinks and our backref is to the wrong
1390 * directory - linear search:
1391 */
1392 for_each_btree_key_continue_norestart(iter2, 0, k, ret) {
1393 if (k.k->p.inode > dir->ei_inode.bi_inum)
1394 break;
1395
1396 if (k.k->type != KEY_TYPE_dirent)
1397 continue;
1398
1399 d = bkey_s_c_to_dirent(k);
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001400 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001401 if (ret < 0)
1402 break;
1403 if (ret)
1404 continue;
1405
1406 if (target.subvol == inode->ei_subvol &&
1407 target.inum == inode->ei_inode.bi_inum)
1408 goto found;
1409 }
1410 }
1411
1412 ret = -ENOENT;
1413 goto err;
1414found:
Joshua Ashton01a7e742023-08-12 22:26:29 +01001415 dirent_name = bch2_dirent_get_name(d);
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001416
Joshua Ashton01a7e742023-08-12 22:26:29 +01001417 name_len = min_t(unsigned, dirent_name.len, NAME_MAX);
1418 memcpy(name, dirent_name.name, name_len);
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001419 name[name_len] = '\0';
1420err:
Kent Overstreet549d1732022-07-17 23:06:38 -04001421 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001422 goto retry;
1423
Kent Overstreet6bd68ec2023-09-12 17:16:02 -04001424 bch2_trans_iter_exit(trans, &iter1);
1425 bch2_trans_iter_exit(trans, &iter2);
1426 bch2_trans_put(trans);
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001427
1428 return ret;
1429}
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001430
1431static const struct export_operations bch_export_ops = {
Kent Overstreet85e95ca2021-11-13 19:49:14 -05001432 .encode_fh = bch2_encode_fh,
1433 .fh_to_dentry = bch2_fh_to_dentry,
1434 .fh_to_parent = bch2_fh_to_parent,
1435 .get_parent = bch2_get_parent,
1436 .get_name = bch2_get_name,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001437};
1438
Kent Overstreet32b26e82021-11-06 00:03:40 -04001439static void bch2_vfs_inode_init(struct btree_trans *trans, subvol_inum inum,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001440 struct bch_inode_info *inode,
Kent Overstreet9ca48532021-10-27 13:05:56 -04001441 struct bch_inode_unpacked *bi,
1442 struct bch_subvolume *subvol)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001443{
Kent Overstreeta91bc5e2024-01-25 20:25:49 -05001444 bch2_iget5_set(&inode->v, &inum);
Kent Overstreet32b26e82021-11-06 00:03:40 -04001445 bch2_inode_update_after_write(trans, inode, bi, ~0);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001446
Kent Overstreet9ca48532021-10-27 13:05:56 -04001447 if (BCH_SUBVOLUME_SNAP(subvol))
1448 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1449 else
1450 clear_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1451
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001452 inode->v.i_blocks = bi->bi_sectors;
1453 inode->v.i_ino = bi->bi_inum;
1454 inode->v.i_rdev = bi->bi_dev;
1455 inode->v.i_generation = bi->bi_generation;
1456 inode->v.i_size = bi->bi_size;
1457
Kent Overstreet33c74e42020-12-03 14:27:20 -05001458 inode->ei_flags = 0;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001459 inode->ei_quota_reserved = 0;
Kent Overstreet0f5254a2018-12-17 05:43:00 -05001460 inode->ei_qid = bch_qid(bi);
Kent Overstreet284ae182021-03-16 01:33:39 -04001461 inode->ei_subvol = inum.subvol;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001462
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001463 inode->v.i_mapping->a_ops = &bch_address_space_operations;
1464
1465 switch (inode->v.i_mode & S_IFMT) {
1466 case S_IFREG:
1467 inode->v.i_op = &bch_file_inode_operations;
1468 inode->v.i_fop = &bch_file_operations;
1469 break;
1470 case S_IFDIR:
1471 inode->v.i_op = &bch_dir_inode_operations;
1472 inode->v.i_fop = &bch_dir_file_operations;
1473 break;
1474 case S_IFLNK:
1475 inode_nohighmem(&inode->v);
1476 inode->v.i_op = &bch_symlink_inode_operations;
1477 break;
1478 default:
1479 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1480 inode->v.i_op = &bch_special_inode_operations;
1481 break;
1482 }
Kent Overstreet550a6a42023-03-19 16:47:30 -04001483
1484 mapping_set_large_folios(inode->v.i_mapping);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001485}
1486
1487static struct inode *bch2_alloc_inode(struct super_block *sb)
1488{
1489 struct bch_inode_info *inode;
1490
1491 inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1492 if (!inode)
1493 return NULL;
1494
1495 inode_init_once(&inode->v);
1496 mutex_init(&inode->ei_update_lock);
Kent Overstreeta7ecd302022-11-04 13:25:57 -04001497 two_state_lock_init(&inode->ei_pagecache_lock);
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001498 INIT_LIST_HEAD(&inode->ei_vfs_inode_list);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001499 mutex_init(&inode->ei_quota_lock);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001500
1501 return &inode->v;
1502}
1503
1504static void bch2_i_callback(struct rcu_head *head)
1505{
1506 struct inode *vinode = container_of(head, struct inode, i_rcu);
1507 struct bch_inode_info *inode = to_bch_ei(vinode);
1508
1509 kmem_cache_free(bch2_inode_cache, inode);
1510}
1511
1512static void bch2_destroy_inode(struct inode *vinode)
1513{
1514 call_rcu(&vinode->i_rcu, bch2_i_callback);
1515}
1516
Joshua Ashton791236b2023-08-12 15:47:45 +01001517static int inode_update_times_fn(struct btree_trans *trans,
1518 struct bch_inode_info *inode,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001519 struct bch_inode_unpacked *bi,
1520 void *p)
1521{
1522 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1523
Linus Torvalds9e877052023-10-30 11:09:38 -10001524 bi->bi_atime = timespec_to_bch2_time(c, inode_get_atime(&inode->v));
1525 bi->bi_mtime = timespec_to_bch2_time(c, inode_get_mtime(&inode->v));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001526 bi->bi_ctime = timespec_to_bch2_time(c, inode_get_ctime(&inode->v));
1527
1528 return 0;
1529}
1530
1531static int bch2_vfs_write_inode(struct inode *vinode,
1532 struct writeback_control *wbc)
1533{
1534 struct bch_fs *c = vinode->i_sb->s_fs_info;
1535 struct bch_inode_info *inode = to_bch_ei(vinode);
1536 int ret;
1537
1538 mutex_lock(&inode->ei_update_lock);
Kent Overstreet2ea90042018-07-17 14:12:42 -04001539 ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1540 ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001541 mutex_unlock(&inode->ei_update_lock);
1542
Kent Overstreet5c1ef832022-09-18 15:43:50 -04001543 return bch2_err_class(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001544}
1545
1546static void bch2_evict_inode(struct inode *vinode)
1547{
1548 struct bch_fs *c = vinode->i_sb->s_fs_info;
1549 struct bch_inode_info *inode = to_bch_ei(vinode);
1550
1551 truncate_inode_pages_final(&inode->v.i_data);
1552
1553 clear_inode(&inode->v);
1554
1555 BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1556
1557 if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1558 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
Kent Overstreet26609b62018-11-01 15:10:01 -04001559 KEY_TYPE_QUOTA_WARN);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001560 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
Kent Overstreet26609b62018-11-01 15:10:01 -04001561 KEY_TYPE_QUOTA_WARN);
Kent Overstreet7c8f6f92022-01-12 02:13:21 -05001562 bch2_inode_rm(c, inode_inum(inode));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001563 }
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001564
1565 mutex_lock(&c->vfs_inodes_lock);
1566 list_del_init(&inode->ei_vfs_inode_list);
1567 mutex_unlock(&c->vfs_inodes_lock);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001568}
1569
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001570void bch2_evict_subvolume_inodes(struct bch_fs *c, snapshot_id_list *s)
Kent Overstreet41f9b7d2021-10-28 16:24:39 -04001571{
Kent Overstreetdefd9e32023-12-16 21:40:26 -05001572 struct bch_inode_info *inode;
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001573 DARRAY(struct bch_inode_info *) grabbed;
1574 bool clean_pass = false, this_pass_clean;
Kent Overstreet41f9b7d2021-10-28 16:24:39 -04001575
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001576 /*
1577 * Initially, we scan for inodes without I_DONTCACHE, then mark them to
1578 * be pruned with d_mark_dontcache().
1579 *
1580 * Once we've had a clean pass where we didn't find any inodes without
1581 * I_DONTCACHE, we wait for them to be freed:
1582 */
Kent Overstreet41f9b7d2021-10-28 16:24:39 -04001583
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001584 darray_init(&grabbed);
1585 darray_make_room(&grabbed, 1024);
Kent Overstreet41f9b7d2021-10-28 16:24:39 -04001586again:
1587 cond_resched();
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001588 this_pass_clean = true;
1589
1590 mutex_lock(&c->vfs_inodes_lock);
1591 list_for_each_entry(inode, &c->vfs_inodes_list, ei_vfs_inode_list) {
1592 if (!snapshot_list_has_id(s, inode->ei_subvol))
Kent Overstreet41f9b7d2021-10-28 16:24:39 -04001593 continue;
1594
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001595 if (!(inode->v.i_state & I_DONTCACHE) &&
Kent Overstreetb0e8c752023-06-09 15:41:41 -04001596 !(inode->v.i_state & I_FREEING) &&
1597 igrab(&inode->v)) {
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001598 this_pass_clean = false;
Kent Overstreet41f9b7d2021-10-28 16:24:39 -04001599
Kent Overstreetb0e8c752023-06-09 15:41:41 -04001600 if (darray_push_gfp(&grabbed, inode, GFP_ATOMIC|__GFP_NOWARN)) {
1601 iput(&inode->v);
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001602 break;
Kent Overstreetb0e8c752023-06-09 15:41:41 -04001603 }
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001604 } else if (clean_pass && this_pass_clean) {
1605 wait_queue_head_t *wq = bit_waitqueue(&inode->v.i_state, __I_NEW);
1606 DEFINE_WAIT_BIT(wait, &inode->v.i_state, __I_NEW);
1607
Kent Overstreet41f9b7d2021-10-28 16:24:39 -04001608 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001609 mutex_unlock(&c->vfs_inodes_lock);
1610
Kent Overstreet41f9b7d2021-10-28 16:24:39 -04001611 schedule();
1612 finish_wait(wq, &wait.wq_entry);
1613 goto again;
1614 }
Kent Overstreet41f9b7d2021-10-28 16:24:39 -04001615 }
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001616 mutex_unlock(&c->vfs_inodes_lock);
1617
Kent Overstreetb0e8c752023-06-09 15:41:41 -04001618 darray_for_each(grabbed, i) {
1619 inode = *i;
1620 d_mark_dontcache(&inode->v);
1621 d_prune_aliases(&inode->v);
1622 iput(&inode->v);
1623 }
Kent Overstreet9edbcc72023-03-15 11:53:51 -04001624 grabbed.nr = 0;
1625
1626 if (!clean_pass || !this_pass_clean) {
1627 clean_pass = this_pass_clean;
1628 goto again;
1629 }
1630
1631 darray_exit(&grabbed);
Kent Overstreet41f9b7d2021-10-28 16:24:39 -04001632}
1633
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001634static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1635{
1636 struct super_block *sb = dentry->d_sb;
1637 struct bch_fs *c = sb->s_fs_info;
Kent Overstreet5663a412018-11-27 08:23:22 -05001638 struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
Kent Overstreet5b650fd12018-07-24 14:54:39 -04001639 unsigned shift = sb->s_blocksize_bits - 9;
Kent Overstreetdf082b32020-11-02 19:49:23 -05001640 /*
1641 * this assumes inodes take up 64 bytes, which is a decent average
1642 * number:
1643 */
1644 u64 avail_inodes = ((usage.capacity - usage.used) << 3);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001645
1646 buf->f_type = BCACHEFS_STATFS_MAGIC;
1647 buf->f_bsize = sb->s_blocksize;
Kent Overstreet5663a412018-11-27 08:23:22 -05001648 buf->f_blocks = usage.capacity >> shift;
Kent Overstreet47924522023-09-10 23:35:02 -04001649 buf->f_bfree = usage.free >> shift;
1650 buf->f_bavail = avail_factor(usage.free) >> shift;
Kent Overstreetdf082b32020-11-02 19:49:23 -05001651
1652 buf->f_files = usage.nr_inodes + avail_inodes;
1653 buf->f_ffree = avail_inodes;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001654
Kent Overstreetf8f8fb42024-02-03 15:05:17 -05001655 buf->f_fsid = uuid_to_fsid(c->sb.user_uuid.b);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001656 buf->f_namelen = BCH_NAME_MAX;
1657
1658 return 0;
1659}
1660
1661static int bch2_sync_fs(struct super_block *sb, int wait)
1662{
1663 struct bch_fs *c = sb->s_fs_info;
Kent Overstreet5c1ef832022-09-18 15:43:50 -04001664 int ret;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001665
Kent Overstreet416f6852019-08-22 16:30:55 -04001666 if (c->opts.journal_flush_disabled)
1667 return 0;
1668
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001669 if (!wait) {
1670 bch2_journal_flush_async(&c->journal, NULL);
1671 return 0;
1672 }
1673
Kent Overstreet5c1ef832022-09-18 15:43:50 -04001674 ret = bch2_journal_flush(&c->journal);
1675 return bch2_err_class(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001676}
1677
1678static struct bch_fs *bch2_path_to_fs(const char *path)
1679{
1680 struct bch_fs *c;
1681 dev_t dev;
1682 int ret;
1683
1684 ret = lookup_bdev(path, &dev);
1685 if (ret)
1686 return ERR_PTR(ret);
1687
1688 c = bch2_dev_to_fs(dev);
Kent Overstreetd5e4dcc2020-09-08 18:30:32 -04001689 if (c)
1690 closure_put(&c->cl);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001691 return c ?: ERR_PTR(-ENOENT);
1692}
1693
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001694static int bch2_remount(struct super_block *sb, int *flags, char *data)
1695{
1696 struct bch_fs *c = sb->s_fs_info;
1697 struct bch_opts opts = bch2_opts_empty();
1698 int ret;
1699
Kent Overstreeta10e6772020-10-23 21:07:17 -04001700 ret = bch2_parse_mount_opts(c, &opts, data);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001701 if (ret)
Kent Overstreet5c1ef832022-09-18 15:43:50 -04001702 goto err;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001703
Kent Overstreet62719cf2023-12-23 17:50:29 -05001704 opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1705
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001706 if (opts.read_only != c->opts.read_only) {
Kent Overstreet1ada1602020-06-15 14:58:47 -04001707 down_write(&c->state_lock);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001708
1709 if (opts.read_only) {
1710 bch2_fs_read_only(c);
1711
1712 sb->s_flags |= SB_RDONLY;
1713 } else {
Kent Overstreet134915f2019-03-21 22:19:57 -04001714 ret = bch2_fs_read_write(c);
1715 if (ret) {
1716 bch_err(c, "error going rw: %i", ret);
Kent Overstreet1ada1602020-06-15 14:58:47 -04001717 up_write(&c->state_lock);
Kent Overstreet5c1ef832022-09-18 15:43:50 -04001718 ret = -EINVAL;
1719 goto err;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001720 }
1721
1722 sb->s_flags &= ~SB_RDONLY;
1723 }
1724
1725 c->opts.read_only = opts.read_only;
1726
Kent Overstreet1ada1602020-06-15 14:58:47 -04001727 up_write(&c->state_lock);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001728 }
1729
Kent Overstreet96dea3d2023-09-12 18:41:22 -04001730 if (opt_defined(opts, errors))
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001731 c->opts.errors = opts.errors;
Kent Overstreet5c1ef832022-09-18 15:43:50 -04001732err:
1733 return bch2_err_class(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001734}
1735
Kent Overstreet625104e2020-09-06 22:58:28 -04001736static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
1737{
1738 struct bch_fs *c = root->d_sb->s_fs_info;
Kent Overstreet625104e2020-09-06 22:58:28 -04001739 bool first = true;
1740
Kent Overstreet9fea2272023-12-16 23:47:29 -05001741 for_each_online_member(c, ca) {
Kent Overstreet625104e2020-09-06 22:58:28 -04001742 if (!first)
1743 seq_putc(seq, ':');
1744 first = false;
Brian Foster63807d92023-11-16 12:13:43 -05001745 seq_puts(seq, ca->disk_sb.sb_name);
Kent Overstreet625104e2020-09-06 22:58:28 -04001746 }
1747
1748 return 0;
1749}
1750
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001751static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1752{
1753 struct bch_fs *c = root->d_sb->s_fs_info;
1754 enum bch_opt_id i;
Kent Overstreetfa8e94f2022-02-25 13:18:19 -05001755 struct printbuf buf = PRINTBUF;
1756 int ret = 0;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001757
1758 for (i = 0; i < bch2_opts_nr; i++) {
1759 const struct bch_option *opt = &bch2_opt_table[i];
1760 u64 v = bch2_opt_get_by_id(&c->opts, i);
1761
Kent Overstreet8244f322021-12-14 14:24:41 -05001762 if (!(opt->flags & OPT_MOUNT))
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001763 continue;
1764
1765 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1766 continue;
1767
Kent Overstreetfa8e94f2022-02-25 13:18:19 -05001768 printbuf_reset(&buf);
Kent Overstreet5521b1d2022-03-05 12:01:16 -05001769 bch2_opt_to_text(&buf, c, c->disk_sb.sb, opt, v,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001770 OPT_SHOW_MOUNT_STYLE);
1771 seq_putc(seq, ',');
Kent Overstreetfa8e94f2022-02-25 13:18:19 -05001772 seq_puts(seq, buf.buf);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001773 }
1774
Kent Overstreetfa8e94f2022-02-25 13:18:19 -05001775 if (buf.allocation_failure)
1776 ret = -ENOMEM;
1777 printbuf_exit(&buf);
1778 return ret;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001779}
1780
Kent Overstreetd5e4dcc2020-09-08 18:30:32 -04001781static void bch2_put_super(struct super_block *sb)
1782{
1783 struct bch_fs *c = sb->s_fs_info;
1784
1785 __bch2_fs_stop(c);
1786}
1787
Brian Foster7239f8e2023-09-15 08:51:54 -04001788/*
1789 * bcachefs doesn't currently integrate intwrite freeze protection but the
1790 * internal write references serve the same purpose. Therefore reuse the
1791 * read-only transition code to perform the quiesce. The caveat is that we don't
1792 * currently have the ability to block tasks that want a write reference while
1793 * the superblock is frozen. This is fine for now, but we should either add
1794 * blocking support or find a way to integrate sb_start_intwrite() and friends.
1795 */
1796static int bch2_freeze(struct super_block *sb)
1797{
1798 struct bch_fs *c = sb->s_fs_info;
1799
1800 down_write(&c->state_lock);
1801 bch2_fs_read_only(c);
1802 up_write(&c->state_lock);
1803 return 0;
1804}
1805
1806static int bch2_unfreeze(struct super_block *sb)
1807{
1808 struct bch_fs *c = sb->s_fs_info;
1809 int ret;
1810
Kent Overstreet3c471b62023-11-26 17:05:02 -05001811 if (test_bit(BCH_FS_emergency_ro, &c->flags))
Brian Foster57962302023-12-05 08:24:38 -05001812 return 0;
1813
Brian Foster7239f8e2023-09-15 08:51:54 -04001814 down_write(&c->state_lock);
1815 ret = bch2_fs_read_write(c);
1816 up_write(&c->state_lock);
1817 return ret;
1818}
1819
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001820static const struct super_operations bch_super_operations = {
1821 .alloc_inode = bch2_alloc_inode,
1822 .destroy_inode = bch2_destroy_inode,
1823 .write_inode = bch2_vfs_write_inode,
1824 .evict_inode = bch2_evict_inode,
1825 .sync_fs = bch2_sync_fs,
1826 .statfs = bch2_statfs,
Kent Overstreet625104e2020-09-06 22:58:28 -04001827 .show_devname = bch2_show_devname,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001828 .show_options = bch2_show_options,
1829 .remount_fs = bch2_remount,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001830 .put_super = bch2_put_super,
1831 .freeze_fs = bch2_freeze,
1832 .unfreeze_fs = bch2_unfreeze,
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001833};
1834
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001835static int bch2_set_super(struct super_block *s, void *data)
1836{
1837 s->s_fs_info = data;
1838 return 0;
1839}
1840
Kent Overstreetd5e4dcc2020-09-08 18:30:32 -04001841static int bch2_noset_super(struct super_block *s, void *data)
1842{
1843 return -EBUSY;
1844}
1845
Kent Overstreet806ebf22023-12-22 21:10:32 -05001846typedef DARRAY(struct bch_fs *) darray_fs;
1847
Kent Overstreetd5e4dcc2020-09-08 18:30:32 -04001848static int bch2_test_super(struct super_block *s, void *data)
1849{
1850 struct bch_fs *c = s->s_fs_info;
Kent Overstreet806ebf22023-12-22 21:10:32 -05001851 darray_fs *d = data;
Kent Overstreetd5e4dcc2020-09-08 18:30:32 -04001852
1853 if (!c)
1854 return false;
1855
Kent Overstreet806ebf22023-12-22 21:10:32 -05001856 darray_for_each(*d, i)
1857 if (c != *i)
Kent Overstreetd5e4dcc2020-09-08 18:30:32 -04001858 return false;
1859 return true;
1860}
1861
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001862static struct dentry *bch2_mount(struct file_system_type *fs_type,
1863 int flags, const char *dev_name, void *data)
1864{
1865 struct bch_fs *c;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001866 struct super_block *sb;
1867 struct inode *vinode;
1868 struct bch_opts opts = bch2_opts_empty();
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001869 int ret;
1870
1871 opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1872
Kent Overstreeta10e6772020-10-23 21:07:17 -04001873 ret = bch2_parse_mount_opts(NULL, &opts, data);
Hongbo Li79162e82024-02-19 20:24:32 +08001874 if (ret) {
1875 ret = bch2_err_class(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001876 return ERR_PTR(ret);
Hongbo Li79162e82024-02-19 20:24:32 +08001877 }
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001878
Dan Robertson044c8c92021-06-10 07:52:42 -04001879 if (!dev_name || strlen(dev_name) == 0)
1880 return ERR_PTR(-EINVAL);
1881
Kent Overstreet806ebf22023-12-22 21:10:32 -05001882 darray_str devs;
1883 ret = bch2_split_devs(dev_name, &devs);
1884 if (ret)
1885 return ERR_PTR(ret);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001886
Kent Overstreet806ebf22023-12-22 21:10:32 -05001887 darray_fs devs_to_fs = {};
1888 darray_for_each(devs, i) {
1889 ret = darray_push(&devs_to_fs, bch2_path_to_fs(*i));
1890 if (ret) {
1891 sb = ERR_PTR(ret);
1892 goto got_sb;
1893 }
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001894 }
1895
Kent Overstreet806ebf22023-12-22 21:10:32 -05001896 sb = sget(fs_type, bch2_test_super, bch2_noset_super, flags|SB_NOSEC, &devs_to_fs);
Kent Overstreetd5e4dcc2020-09-08 18:30:32 -04001897 if (!IS_ERR(sb))
1898 goto got_sb;
1899
Kent Overstreet806ebf22023-12-22 21:10:32 -05001900 c = bch2_fs_open(devs.data, devs.nr, opts);
Kent Overstreeta10e6772020-10-23 21:07:17 -04001901 if (IS_ERR(c)) {
Kent Overstreetd5e4dcc2020-09-08 18:30:32 -04001902 sb = ERR_CAST(c);
Kent Overstreeta10e6772020-10-23 21:07:17 -04001903 goto got_sb;
1904 }
1905
1906 /* Some options can't be parsed until after the fs is started: */
1907 ret = bch2_parse_mount_opts(c, &opts, data);
1908 if (ret) {
1909 bch2_fs_stop(c);
1910 sb = ERR_PTR(ret);
1911 goto got_sb;
1912 }
1913
1914 bch2_opts_apply(&c->opts, opts);
1915
1916 sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
1917 if (IS_ERR(sb))
1918 bch2_fs_stop(c);
Kent Overstreetd5e4dcc2020-09-08 18:30:32 -04001919got_sb:
Kent Overstreet806ebf22023-12-22 21:10:32 -05001920 darray_exit(&devs_to_fs);
1921 bch2_darray_str_exit(&devs);
Kent Overstreetd5e4dcc2020-09-08 18:30:32 -04001922
Kent Overstreet78c0b752022-11-19 22:39:08 -05001923 if (IS_ERR(sb)) {
1924 ret = PTR_ERR(sb);
1925 ret = bch2_err_class(ret);
1926 return ERR_PTR(ret);
1927 }
Kent Overstreetd5e4dcc2020-09-08 18:30:32 -04001928
1929 c = sb->s_fs_info;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001930
1931 if (sb->s_root) {
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001932 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1933 ret = -EBUSY;
1934 goto err_put_super;
1935 }
1936 goto out;
1937 }
1938
Kent Overstreet5b6d40e2019-06-29 17:59:21 -04001939 sb->s_blocksize = block_bytes(c);
1940 sb->s_blocksize_bits = ilog2(block_bytes(c));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001941 sb->s_maxbytes = MAX_LFS_FILESIZE;
1942 sb->s_op = &bch_super_operations;
1943 sb->s_export_op = &bch_export_ops;
1944#ifdef CONFIG_BCACHEFS_QUOTA
1945 sb->s_qcop = &bch2_quotactl_operations;
1946 sb->s_quota_types = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1947#endif
1948 sb->s_xattr = bch2_xattr_handlers;
1949 sb->s_magic = BCACHEFS_STATFS_MAGIC;
Kent Overstreet595c1e92021-04-28 22:51:42 -04001950 sb->s_time_gran = c->sb.nsec_per_time_unit;
1951 sb->s_time_min = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
1952 sb->s_time_max = div_s64(S64_MAX, c->sb.time_units_per_sec);
Kent Overstreet29223b52024-02-03 15:23:07 -05001953 sb->s_uuid = c->sb.user_uuid;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001954 c->vfs_sb = sb;
Kent Overstreet3e3e02e2022-10-19 18:31:33 -04001955 strscpy(sb->s_id, c->name, sizeof(sb->s_id));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001956
1957 ret = super_setup_bdi(sb);
1958 if (ret)
1959 goto err_put_super;
1960
1961 sb->s_bdi->ra_pages = VM_READAHEAD_PAGES;
1962
Kent Overstreet9fea2272023-12-16 23:47:29 -05001963 for_each_online_member(c, ca) {
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001964 struct block_device *bdev = ca->disk_sb.bdev;
1965
1966 /* XXX: create an anonymous device for multi device filesystems */
1967 sb->s_bdev = bdev;
1968 sb->s_dev = bdev->bd_dev;
1969 percpu_ref_put(&ca->io_ref);
1970 break;
1971 }
1972
Kent Overstreetddc7dd62021-05-27 19:15:44 -04001973 c->dev = sb->s_dev;
1974
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001975#ifdef CONFIG_BCACHEFS_POSIX_ACL
1976 if (c->opts.acl)
1977 sb->s_flags |= SB_POSIXACL;
1978#endif
1979
Linus Torvaldsecae0bd2023-11-02 19:38:47 -10001980 sb->s_shrink->seeks = 0;
Kent Overstreete3f2db32021-11-13 13:36:26 -05001981
Kent Overstreet284ae182021-03-16 01:33:39 -04001982 vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
Kent Overstreetd4bf5ee2022-07-18 19:42:58 -04001983 ret = PTR_ERR_OR_ZERO(vinode);
Kent Overstreetcf904c82023-12-16 22:43:41 -05001984 bch_err_msg(c, ret, "mounting: error getting root inode");
1985 if (ret)
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001986 goto err_put_super;
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001987
1988 sb->s_root = d_make_root(vinode);
1989 if (!sb->s_root) {
Kent Overstreet619f5be2019-04-17 18:21:19 -04001990 bch_err(c, "error mounting: error allocating root dentry");
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08001991 ret = -ENOMEM;
1992 goto err_put_super;
1993 }
1994
1995 sb->s_flags |= SB_ACTIVE;
1996out:
1997 return dget(sb->s_root);
1998
1999err_put_super:
Kent Overstreet4bd02d32024-03-28 01:20:36 -04002000 __bch2_fs_stop(c);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002001 deactivate_locked_super(sb);
Kent Overstreetdde72e12023-03-30 20:16:06 -04002002 return ERR_PTR(bch2_err_class(ret));
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002003}
2004
2005static void bch2_kill_sb(struct super_block *sb)
2006{
2007 struct bch_fs *c = sb->s_fs_info;
2008
2009 generic_shutdown_super(sb);
Kent Overstreet178c4872023-11-12 14:15:35 -05002010 bch2_fs_free(c);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002011}
2012
2013static struct file_system_type bcache_fs_type = {
2014 .owner = THIS_MODULE,
2015 .name = "bcachefs",
2016 .mount = bch2_mount,
2017 .kill_sb = bch2_kill_sb,
2018 .fs_flags = FS_REQUIRES_DEV,
2019};
2020
2021MODULE_ALIAS_FS("bcachefs");
2022
2023void bch2_vfs_exit(void)
2024{
2025 unregister_filesystem(&bcache_fs_type);
Kent Overstreet3e3e02e2022-10-19 18:31:33 -04002026 kmem_cache_destroy(bch2_inode_cache);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002027}
2028
2029int __init bch2_vfs_init(void)
2030{
2031 int ret = -ENOMEM;
2032
Mikulas Patocka5eaa76d2023-07-13 18:00:28 +02002033 bch2_inode_cache = KMEM_CACHE(bch_inode_info, SLAB_RECLAIM_ACCOUNT);
Kent Overstreet1c6fdbd2017-03-16 22:18:50 -08002034 if (!bch2_inode_cache)
2035 goto err;
2036
2037 ret = register_filesystem(&bcache_fs_type);
2038 if (ret)
2039 goto err;
2040
2041 return 0;
2042err:
2043 bch2_vfs_exit();
2044 return ret;
2045}
2046
2047#endif /* NO_BCACHEFS_FS */