bcc1436c1720680632193d35c912d45271b8747e
[linux-2.6-microblaze.git] / fs / bcachefs / fs.c
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef NO_BCACHEFS_FS
3
4 #include "bcachefs.h"
5 #include "acl.h"
6 #include "bkey_buf.h"
7 #include "btree_update.h"
8 #include "buckets.h"
9 #include "chardev.h"
10 #include "dirent.h"
11 #include "errcode.h"
12 #include "extents.h"
13 #include "fs.h"
14 #include "fs-common.h"
15 #include "fs-io.h"
16 #include "fs-ioctl.h"
17 #include "fs-io-buffered.h"
18 #include "fs-io-direct.h"
19 #include "fs-io-pagecache.h"
20 #include "fsck.h"
21 #include "inode.h"
22 #include "io_read.h"
23 #include "journal.h"
24 #include "keylist.h"
25 #include "quota.h"
26 #include "snapshot.h"
27 #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>
40 #include <linux/string.h>
41 #include <linux/xattr.h>
42
43 static struct kmem_cache *bch2_inode_cache;
44
45 static void bch2_vfs_inode_init(struct btree_trans *, subvol_inum,
46                                 struct bch_inode_info *,
47                                 struct bch_inode_unpacked *,
48                                 struct bch_subvolume *);
49
50 void bch2_inode_update_after_write(struct btree_trans *trans,
51                                    struct bch_inode_info *inode,
52                                    struct bch_inode_unpacked *bi,
53                                    unsigned fields)
54 {
55         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),
61                                c->opts.inodes_use_key_cache);
62
63         set_nlink(&inode->v, bch2_inode_nlink_get(bi));
64         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)
69                 inode_set_atime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_atime));
70         if (fields & ATTR_MTIME)
71                 inode_set_mtime_to_ts(&inode->v, bch2_time_to_timespec(c, bi->bi_mtime));
72         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;
76
77         bch2_inode_flags_to_vfs(inode);
78 }
79
80 int __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)
84 {
85         struct btree_trans *trans = bch2_trans_get(c);
86         struct btree_iter iter = { NULL };
87         struct bch_inode_unpacked inode_u;
88         int ret;
89 retry:
90         bch2_trans_begin(trans);
91
92         ret   = bch2_inode_peek(trans, &iter, &inode_u, inode_inum(inode),
93                                 BTREE_ITER_INTENT) ?:
94                 (set ? set(trans, inode, &inode_u, p) : 0) ?:
95                 bch2_inode_write(trans, &iter, &inode_u) ?:
96                 bch2_trans_commit(trans, NULL, NULL, BCH_TRANS_COMMIT_no_enospc);
97
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)
103                 bch2_inode_update_after_write(trans, inode, &inode_u, fields);
104
105         bch2_trans_iter_exit(trans, &iter);
106
107         if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
108                 goto retry;
109
110         bch2_fs_fatal_err_on(bch2_err_matches(ret, ENOENT), c,
111                              "inode %u:%llu not found when updating",
112                              inode_inum(inode).subvol,
113                              inode_inum(inode).inum);
114
115         bch2_trans_put(trans);
116         return ret < 0 ? ret : 0;
117 }
118
119 int bch2_fs_quota_transfer(struct bch_fs *c,
120                            struct bch_inode_info *inode,
121                            struct bch_qid new_qid,
122                            unsigned qtypes,
123                            enum quota_acct_mode mode)
124 {
125         unsigned i;
126         int ret;
127
128         qtypes &= enabled_qtypes(c);
129
130         for (i = 0; i < QTYP_NR; i++)
131                 if (new_qid.q[i] == inode->ei_qid.q[i])
132                         qtypes &= ~(1U << i);
133
134         if (!qtypes)
135                 return 0;
136
137         mutex_lock(&inode->ei_quota_lock);
138
139         ret = bch2_quota_transfer(c, qtypes, new_qid,
140                                   inode->ei_qid,
141                                   inode->v.i_blocks +
142                                   inode->ei_quota_reserved,
143                                   mode);
144         if (!ret)
145                 for (i = 0; i < QTYP_NR; i++)
146                         if (qtypes & (1 << i))
147                                 inode->ei_qid.q[i] = new_qid.q[i];
148
149         mutex_unlock(&inode->ei_quota_lock);
150
151         return ret;
152 }
153
154 static int bch2_iget5_test(struct inode *vinode, void *p)
155 {
156         struct bch_inode_info *inode = to_bch_ei(vinode);
157         subvol_inum *inum = p;
158
159         return inode->ei_subvol == inum->subvol &&
160                 inode->ei_inode.bi_inum == inum->inum;
161 }
162
163 static int bch2_iget5_set(struct inode *vinode, void *p)
164 {
165         struct bch_inode_info *inode = to_bch_ei(vinode);
166         subvol_inum *inum = p;
167
168         inode->v.i_ino          = inum->inum;
169         inode->ei_subvol        = inum->subvol;
170         inode->ei_inode.bi_inum = inum->inum;
171         return 0;
172 }
173
174 static unsigned bch2_inode_hash(subvol_inum inum)
175 {
176         return jhash_3words(inum.subvol, inum.inum >> 32, inum.inum, JHASH_INITVAL);
177 }
178
179 static struct bch_inode_info *bch2_inode_insert(struct bch_fs *c, struct bch_inode_info *inode)
180 {
181         subvol_inum inum = inode_inum(inode);
182         struct bch_inode_info *old = to_bch_ei(inode_insert5(&inode->v,
183                                       bch2_inode_hash(inum),
184                                       bch2_iget5_test,
185                                       bch2_iget5_set,
186                                       &inum));
187         BUG_ON(!old);
188
189         if (unlikely(old != inode)) {
190                 discard_new_inode(&inode->v);
191                 inode = old;
192         } else {
193                 mutex_lock(&c->vfs_inodes_lock);
194                 list_add(&inode->ei_vfs_inode_list, &c->vfs_inodes_list);
195                 mutex_unlock(&c->vfs_inodes_lock);
196                 /*
197                  * we really don't want insert_inode_locked2() to be setting
198                  * I_NEW...
199                  */
200                 unlock_new_inode(&inode->v);
201         }
202
203         return inode;
204 }
205
206 #define memalloc_flags_do(_flags, _do)                                          \
207 ({                                                                              \
208         unsigned _saved_flags = memalloc_flags_save(_flags);                    \
209         typeof(_do) _ret = _do;                                                 \
210         memalloc_noreclaim_restore(_saved_flags);                               \
211         _ret;                                                                   \
212 })
213
214 /*
215  * Allocate a new inode, dropping/retaking btree locks if necessary:
216  */
217 static struct bch_inode_info *bch2_new_inode(struct btree_trans *trans)
218 {
219         struct bch_fs *c = trans->c;
220
221         struct bch_inode_info *inode =
222                 memalloc_flags_do(PF_MEMALLOC_NORECLAIM|PF_MEMALLOC_NOWARN,
223                                   to_bch_ei(new_inode(c->vfs_sb)));
224
225         if (unlikely(!inode)) {
226                 int ret = drop_locks_do(trans, (inode = to_bch_ei(new_inode(c->vfs_sb))) ? 0 : -ENOMEM);
227                 if (ret && inode)
228                         discard_new_inode(&inode->v);
229                 if (ret)
230                         return ERR_PTR(ret);
231         }
232
233         return inode;
234 }
235
236 struct inode *bch2_vfs_inode_get(struct bch_fs *c, subvol_inum inum)
237 {
238         struct bch_inode_info *inode =
239                 to_bch_ei(ilookup5_nowait(c->vfs_sb,
240                                           bch2_inode_hash(inum),
241                                           bch2_iget5_test,
242                                           &inum));
243         if (inode)
244                 return &inode->v;
245
246         struct btree_trans *trans = bch2_trans_get(c);
247
248         struct bch_inode_unpacked inode_u;
249         struct bch_subvolume subvol;
250         int ret = lockrestart_do(trans,
251                 bch2_subvolume_get(trans, inum.subvol, true, 0, &subvol) ?:
252                 bch2_inode_find_by_inum_trans(trans, inum, &inode_u)) ?:
253                 PTR_ERR_OR_ZERO(inode = bch2_new_inode(trans));
254         if (!ret) {
255                 bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
256                 inode = bch2_inode_insert(c, inode);
257         }
258         bch2_trans_put(trans);
259
260         return ret ? ERR_PTR(ret) : &inode->v;
261 }
262
263 struct bch_inode_info *
264 __bch2_create(struct mnt_idmap *idmap,
265               struct bch_inode_info *dir, struct dentry *dentry,
266               umode_t mode, dev_t rdev, subvol_inum snapshot_src,
267               unsigned flags)
268 {
269         struct bch_fs *c = dir->v.i_sb->s_fs_info;
270         struct btree_trans *trans;
271         struct bch_inode_unpacked dir_u;
272         struct bch_inode_info *inode;
273         struct bch_inode_unpacked inode_u;
274         struct posix_acl *default_acl = NULL, *acl = NULL;
275         subvol_inum inum;
276         struct bch_subvolume subvol;
277         u64 journal_seq = 0;
278         int ret;
279
280         /*
281          * preallocate acls + vfs inode before btree transaction, so that
282          * nothing can fail after the transaction succeeds:
283          */
284 #ifdef CONFIG_BCACHEFS_POSIX_ACL
285         ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
286         if (ret)
287                 return ERR_PTR(ret);
288 #endif
289         inode = to_bch_ei(new_inode(c->vfs_sb));
290         if (unlikely(!inode)) {
291                 inode = ERR_PTR(-ENOMEM);
292                 goto err;
293         }
294
295         bch2_inode_init_early(c, &inode_u);
296
297         if (!(flags & BCH_CREATE_TMPFILE))
298                 mutex_lock(&dir->ei_update_lock);
299
300         trans = bch2_trans_get(c);
301 retry:
302         bch2_trans_begin(trans);
303
304         ret   = bch2_subvol_is_ro_trans(trans, dir->ei_subvol) ?:
305                 bch2_create_trans(trans,
306                                   inode_inum(dir), &dir_u, &inode_u,
307                                   !(flags & BCH_CREATE_TMPFILE)
308                                   ? &dentry->d_name : NULL,
309                                   from_kuid(i_user_ns(&dir->v), current_fsuid()),
310                                   from_kgid(i_user_ns(&dir->v), current_fsgid()),
311                                   mode, rdev,
312                                   default_acl, acl, snapshot_src, flags) ?:
313                 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
314                                 KEY_TYPE_QUOTA_PREALLOC);
315         if (unlikely(ret))
316                 goto err_before_quota;
317
318         inum.subvol = inode_u.bi_subvol ?: dir->ei_subvol;
319         inum.inum = inode_u.bi_inum;
320
321         ret   = bch2_subvolume_get(trans, inum.subvol, true,
322                                    BTREE_ITER_WITH_UPDATES, &subvol) ?:
323                 bch2_trans_commit(trans, NULL, &journal_seq, 0);
324         if (unlikely(ret)) {
325                 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
326                                 KEY_TYPE_QUOTA_WARN);
327 err_before_quota:
328                 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
329                         goto retry;
330                 goto err_trans;
331         }
332
333         if (!(flags & BCH_CREATE_TMPFILE)) {
334                 bch2_inode_update_after_write(trans, dir, &dir_u,
335                                               ATTR_MTIME|ATTR_CTIME);
336                 mutex_unlock(&dir->ei_update_lock);
337         }
338
339         bch2_vfs_inode_init(trans, inum, inode, &inode_u, &subvol);
340
341         set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
342         set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
343
344         /*
345          * we must insert the new inode into the inode cache before calling
346          * bch2_trans_exit() and dropping locks, else we could race with another
347          * thread pulling the inode in and modifying it:
348          */
349         inode = bch2_inode_insert(c, inode);
350         bch2_trans_put(trans);
351 err:
352         posix_acl_release(default_acl);
353         posix_acl_release(acl);
354         return inode;
355 err_trans:
356         if (!(flags & BCH_CREATE_TMPFILE))
357                 mutex_unlock(&dir->ei_update_lock);
358
359         bch2_trans_put(trans);
360         make_bad_inode(&inode->v);
361         iput(&inode->v);
362         inode = ERR_PTR(ret);
363         goto err;
364 }
365
366 /* methods */
367
368 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
369                                   unsigned int flags)
370 {
371         struct bch_fs *c = vdir->i_sb->s_fs_info;
372         struct bch_inode_info *dir = to_bch_ei(vdir);
373         struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
374         struct inode *vinode = NULL;
375         subvol_inum inum = { .subvol = 1 };
376         int ret;
377
378         ret = bch2_dirent_lookup(c, inode_inum(dir), &hash,
379                                  &dentry->d_name, &inum);
380
381         if (!ret)
382                 vinode = bch2_vfs_inode_get(c, inum);
383
384         return d_splice_alias(vinode, dentry);
385 }
386
387 static int bch2_mknod(struct mnt_idmap *idmap,
388                       struct inode *vdir, struct dentry *dentry,
389                       umode_t mode, dev_t rdev)
390 {
391         struct bch_inode_info *inode =
392                 __bch2_create(idmap, to_bch_ei(vdir), dentry, mode, rdev,
393                               (subvol_inum) { 0 }, 0);
394
395         if (IS_ERR(inode))
396                 return bch2_err_class(PTR_ERR(inode));
397
398         d_instantiate(dentry, &inode->v);
399         return 0;
400 }
401
402 static int bch2_create(struct mnt_idmap *idmap,
403                        struct inode *vdir, struct dentry *dentry,
404                        umode_t mode, bool excl)
405 {
406         return bch2_mknod(idmap, vdir, dentry, mode|S_IFREG, 0);
407 }
408
409 static int __bch2_link(struct bch_fs *c,
410                        struct bch_inode_info *inode,
411                        struct bch_inode_info *dir,
412                        struct dentry *dentry)
413 {
414         struct btree_trans *trans = bch2_trans_get(c);
415         struct bch_inode_unpacked dir_u, inode_u;
416         int ret;
417
418         mutex_lock(&inode->ei_update_lock);
419
420         ret = commit_do(trans, NULL, NULL, 0,
421                         bch2_link_trans(trans,
422                                         inode_inum(dir),   &dir_u,
423                                         inode_inum(inode), &inode_u,
424                                         &dentry->d_name));
425
426         if (likely(!ret)) {
427                 bch2_inode_update_after_write(trans, dir, &dir_u,
428                                               ATTR_MTIME|ATTR_CTIME);
429                 bch2_inode_update_after_write(trans, inode, &inode_u, ATTR_CTIME);
430         }
431
432         bch2_trans_put(trans);
433         mutex_unlock(&inode->ei_update_lock);
434         return ret;
435 }
436
437 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
438                      struct dentry *dentry)
439 {
440         struct bch_fs *c = vdir->i_sb->s_fs_info;
441         struct bch_inode_info *dir = to_bch_ei(vdir);
442         struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
443         int ret;
444
445         lockdep_assert_held(&inode->v.i_rwsem);
446
447         ret   = bch2_subvol_is_ro(c, dir->ei_subvol) ?:
448                 bch2_subvol_is_ro(c, inode->ei_subvol) ?:
449                 __bch2_link(c, inode, dir, dentry);
450         if (unlikely(ret))
451                 return bch2_err_class(ret);
452
453         ihold(&inode->v);
454         d_instantiate(dentry, &inode->v);
455         return 0;
456 }
457
458 int __bch2_unlink(struct inode *vdir, struct dentry *dentry,
459                   bool deleting_snapshot)
460 {
461         struct bch_fs *c = vdir->i_sb->s_fs_info;
462         struct bch_inode_info *dir = to_bch_ei(vdir);
463         struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
464         struct bch_inode_unpacked dir_u, inode_u;
465         struct btree_trans *trans = bch2_trans_get(c);
466         int ret;
467
468         bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
469
470         ret = commit_do(trans, NULL, NULL,
471                         BCH_TRANS_COMMIT_no_enospc,
472                 bch2_unlink_trans(trans,
473                                   inode_inum(dir), &dir_u,
474                                   &inode_u, &dentry->d_name,
475                                   deleting_snapshot));
476         if (unlikely(ret))
477                 goto err;
478
479         bch2_inode_update_after_write(trans, dir, &dir_u,
480                                       ATTR_MTIME|ATTR_CTIME);
481         bch2_inode_update_after_write(trans, inode, &inode_u,
482                                       ATTR_MTIME);
483
484         if (inode_u.bi_subvol) {
485                 /*
486                  * Subvolume deletion is asynchronous, but we still want to tell
487                  * the VFS that it's been deleted here:
488                  */
489                 set_nlink(&inode->v, 0);
490         }
491 err:
492         bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
493         bch2_trans_put(trans);
494
495         return ret;
496 }
497
498 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
499 {
500         struct bch_inode_info *dir= to_bch_ei(vdir);
501         struct bch_fs *c = dir->v.i_sb->s_fs_info;
502
503         int ret = bch2_subvol_is_ro(c, dir->ei_subvol) ?:
504                 __bch2_unlink(vdir, dentry, false);
505         return bch2_err_class(ret);
506 }
507
508 static int bch2_symlink(struct mnt_idmap *idmap,
509                         struct inode *vdir, struct dentry *dentry,
510                         const char *symname)
511 {
512         struct bch_fs *c = vdir->i_sb->s_fs_info;
513         struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
514         int ret;
515
516         inode = __bch2_create(idmap, dir, dentry, S_IFLNK|S_IRWXUGO, 0,
517                               (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
518         if (IS_ERR(inode))
519                 return bch2_err_class(PTR_ERR(inode));
520
521         inode_lock(&inode->v);
522         ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
523         inode_unlock(&inode->v);
524
525         if (unlikely(ret))
526                 goto err;
527
528         ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
529         if (unlikely(ret))
530                 goto err;
531
532         ret = __bch2_link(c, inode, dir, dentry);
533         if (unlikely(ret))
534                 goto err;
535
536         d_instantiate(dentry, &inode->v);
537         return 0;
538 err:
539         iput(&inode->v);
540         return bch2_err_class(ret);
541 }
542
543 static int bch2_mkdir(struct mnt_idmap *idmap,
544                       struct inode *vdir, struct dentry *dentry, umode_t mode)
545 {
546         return bch2_mknod(idmap, vdir, dentry, mode|S_IFDIR, 0);
547 }
548
549 static int bch2_rename2(struct mnt_idmap *idmap,
550                         struct inode *src_vdir, struct dentry *src_dentry,
551                         struct inode *dst_vdir, struct dentry *dst_dentry,
552                         unsigned flags)
553 {
554         struct bch_fs *c = src_vdir->i_sb->s_fs_info;
555         struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
556         struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
557         struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
558         struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
559         struct bch_inode_unpacked dst_dir_u, src_dir_u;
560         struct bch_inode_unpacked src_inode_u, dst_inode_u;
561         struct btree_trans *trans;
562         enum bch_rename_mode mode = flags & RENAME_EXCHANGE
563                 ? BCH_RENAME_EXCHANGE
564                 : dst_dentry->d_inode
565                 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
566         int ret;
567
568         if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
569                 return -EINVAL;
570
571         if (mode == BCH_RENAME_OVERWRITE) {
572                 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
573                                                    0, LLONG_MAX);
574                 if (ret)
575                         return ret;
576         }
577
578         trans = bch2_trans_get(c);
579
580         bch2_lock_inodes(INODE_UPDATE_LOCK,
581                          src_dir,
582                          dst_dir,
583                          src_inode,
584                          dst_inode);
585
586         ret   = bch2_subvol_is_ro_trans(trans, src_dir->ei_subvol) ?:
587                 bch2_subvol_is_ro_trans(trans, dst_dir->ei_subvol);
588         if (ret)
589                 goto err;
590
591         if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
592                 ret = bch2_fs_quota_transfer(c, src_inode,
593                                              dst_dir->ei_qid,
594                                              1 << QTYP_PRJ,
595                                              KEY_TYPE_QUOTA_PREALLOC);
596                 if (ret)
597                         goto err;
598         }
599
600         if (mode == BCH_RENAME_EXCHANGE &&
601             inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
602                 ret = bch2_fs_quota_transfer(c, dst_inode,
603                                              src_dir->ei_qid,
604                                              1 << QTYP_PRJ,
605                                              KEY_TYPE_QUOTA_PREALLOC);
606                 if (ret)
607                         goto err;
608         }
609
610         ret = commit_do(trans, NULL, NULL, 0,
611                         bch2_rename_trans(trans,
612                                           inode_inum(src_dir), &src_dir_u,
613                                           inode_inum(dst_dir), &dst_dir_u,
614                                           &src_inode_u,
615                                           &dst_inode_u,
616                                           &src_dentry->d_name,
617                                           &dst_dentry->d_name,
618                                           mode));
619         if (unlikely(ret))
620                 goto err;
621
622         BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
623         BUG_ON(dst_inode &&
624                dst_inode->v.i_ino != dst_inode_u.bi_inum);
625
626         bch2_inode_update_after_write(trans, src_dir, &src_dir_u,
627                                       ATTR_MTIME|ATTR_CTIME);
628
629         if (src_dir != dst_dir)
630                 bch2_inode_update_after_write(trans, dst_dir, &dst_dir_u,
631                                               ATTR_MTIME|ATTR_CTIME);
632
633         bch2_inode_update_after_write(trans, src_inode, &src_inode_u,
634                                       ATTR_CTIME);
635
636         if (dst_inode)
637                 bch2_inode_update_after_write(trans, dst_inode, &dst_inode_u,
638                                               ATTR_CTIME);
639 err:
640         bch2_trans_put(trans);
641
642         bch2_fs_quota_transfer(c, src_inode,
643                                bch_qid(&src_inode->ei_inode),
644                                1 << QTYP_PRJ,
645                                KEY_TYPE_QUOTA_NOCHECK);
646         if (dst_inode)
647                 bch2_fs_quota_transfer(c, dst_inode,
648                                        bch_qid(&dst_inode->ei_inode),
649                                        1 << QTYP_PRJ,
650                                        KEY_TYPE_QUOTA_NOCHECK);
651
652         bch2_unlock_inodes(INODE_UPDATE_LOCK,
653                            src_dir,
654                            dst_dir,
655                            src_inode,
656                            dst_inode);
657
658         return bch2_err_class(ret);
659 }
660
661 static void bch2_setattr_copy(struct mnt_idmap *idmap,
662                               struct bch_inode_info *inode,
663                               struct bch_inode_unpacked *bi,
664                               struct iattr *attr)
665 {
666         struct bch_fs *c = inode->v.i_sb->s_fs_info;
667         unsigned int ia_valid = attr->ia_valid;
668
669         if (ia_valid & ATTR_UID)
670                 bi->bi_uid = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
671         if (ia_valid & ATTR_GID)
672                 bi->bi_gid = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
673
674         if (ia_valid & ATTR_SIZE)
675                 bi->bi_size = attr->ia_size;
676
677         if (ia_valid & ATTR_ATIME)
678                 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
679         if (ia_valid & ATTR_MTIME)
680                 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
681         if (ia_valid & ATTR_CTIME)
682                 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
683
684         if (ia_valid & ATTR_MODE) {
685                 umode_t mode = attr->ia_mode;
686                 kgid_t gid = ia_valid & ATTR_GID
687                         ? attr->ia_gid
688                         : inode->v.i_gid;
689
690                 if (!in_group_p(gid) &&
691                     !capable_wrt_inode_uidgid(idmap, &inode->v, CAP_FSETID))
692                         mode &= ~S_ISGID;
693                 bi->bi_mode = mode;
694         }
695 }
696
697 int bch2_setattr_nonsize(struct mnt_idmap *idmap,
698                          struct bch_inode_info *inode,
699                          struct iattr *attr)
700 {
701         struct bch_fs *c = inode->v.i_sb->s_fs_info;
702         struct bch_qid qid;
703         struct btree_trans *trans;
704         struct btree_iter inode_iter = { NULL };
705         struct bch_inode_unpacked inode_u;
706         struct posix_acl *acl = NULL;
707         int ret;
708
709         mutex_lock(&inode->ei_update_lock);
710
711         qid = inode->ei_qid;
712
713         if (attr->ia_valid & ATTR_UID)
714                 qid.q[QTYP_USR] = from_kuid(i_user_ns(&inode->v), attr->ia_uid);
715
716         if (attr->ia_valid & ATTR_GID)
717                 qid.q[QTYP_GRP] = from_kgid(i_user_ns(&inode->v), attr->ia_gid);
718
719         ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
720                                      KEY_TYPE_QUOTA_PREALLOC);
721         if (ret)
722                 goto err;
723
724         trans = bch2_trans_get(c);
725 retry:
726         bch2_trans_begin(trans);
727         kfree(acl);
728         acl = NULL;
729
730         ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inode_inum(inode),
731                               BTREE_ITER_INTENT);
732         if (ret)
733                 goto btree_err;
734
735         bch2_setattr_copy(idmap, inode, &inode_u, attr);
736
737         if (attr->ia_valid & ATTR_MODE) {
738                 ret = bch2_acl_chmod(trans, inode_inum(inode), &inode_u,
739                                      inode_u.bi_mode, &acl);
740                 if (ret)
741                         goto btree_err;
742         }
743
744         ret =   bch2_inode_write(trans, &inode_iter, &inode_u) ?:
745                 bch2_trans_commit(trans, NULL, NULL,
746                                   BCH_TRANS_COMMIT_no_enospc);
747 btree_err:
748         bch2_trans_iter_exit(trans, &inode_iter);
749
750         if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
751                 goto retry;
752         if (unlikely(ret))
753                 goto err_trans;
754
755         bch2_inode_update_after_write(trans, inode, &inode_u, attr->ia_valid);
756
757         if (acl)
758                 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
759 err_trans:
760         bch2_trans_put(trans);
761 err:
762         mutex_unlock(&inode->ei_update_lock);
763
764         return bch2_err_class(ret);
765 }
766
767 static int bch2_getattr(struct mnt_idmap *idmap,
768                         const struct path *path, struct kstat *stat,
769                         u32 request_mask, unsigned query_flags)
770 {
771         struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
772         struct bch_fs *c = inode->v.i_sb->s_fs_info;
773
774         stat->dev       = inode->v.i_sb->s_dev;
775         stat->ino       = inode->v.i_ino;
776         stat->mode      = inode->v.i_mode;
777         stat->nlink     = inode->v.i_nlink;
778         stat->uid       = inode->v.i_uid;
779         stat->gid       = inode->v.i_gid;
780         stat->rdev      = inode->v.i_rdev;
781         stat->size      = i_size_read(&inode->v);
782         stat->atime     = inode_get_atime(&inode->v);
783         stat->mtime     = inode_get_mtime(&inode->v);
784         stat->ctime     = inode_get_ctime(&inode->v);
785         stat->blksize   = block_bytes(c);
786         stat->blocks    = inode->v.i_blocks;
787
788         if (request_mask & STATX_BTIME) {
789                 stat->result_mask |= STATX_BTIME;
790                 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
791         }
792
793         if (inode->ei_inode.bi_flags & BCH_INODE_immutable)
794                 stat->attributes |= STATX_ATTR_IMMUTABLE;
795         stat->attributes_mask    |= STATX_ATTR_IMMUTABLE;
796
797         if (inode->ei_inode.bi_flags & BCH_INODE_append)
798                 stat->attributes |= STATX_ATTR_APPEND;
799         stat->attributes_mask    |= STATX_ATTR_APPEND;
800
801         if (inode->ei_inode.bi_flags & BCH_INODE_nodump)
802                 stat->attributes |= STATX_ATTR_NODUMP;
803         stat->attributes_mask    |= STATX_ATTR_NODUMP;
804
805         return 0;
806 }
807
808 static int bch2_setattr(struct mnt_idmap *idmap,
809                         struct dentry *dentry, struct iattr *iattr)
810 {
811         struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
812         struct bch_fs *c = inode->v.i_sb->s_fs_info;
813         int ret;
814
815         lockdep_assert_held(&inode->v.i_rwsem);
816
817         ret   = bch2_subvol_is_ro(c, inode->ei_subvol) ?:
818                 setattr_prepare(idmap, dentry, iattr);
819         if (ret)
820                 return ret;
821
822         return iattr->ia_valid & ATTR_SIZE
823                 ? bchfs_truncate(idmap, inode, iattr)
824                 : bch2_setattr_nonsize(idmap, inode, iattr);
825 }
826
827 static int bch2_tmpfile(struct mnt_idmap *idmap,
828                         struct inode *vdir, struct file *file, umode_t mode)
829 {
830         struct bch_inode_info *inode =
831                 __bch2_create(idmap, to_bch_ei(vdir),
832                               file->f_path.dentry, mode, 0,
833                               (subvol_inum) { 0 }, BCH_CREATE_TMPFILE);
834
835         if (IS_ERR(inode))
836                 return bch2_err_class(PTR_ERR(inode));
837
838         d_mark_tmpfile(file, &inode->v);
839         d_instantiate(file->f_path.dentry, &inode->v);
840         return finish_open_simple(file, 0);
841 }
842
843 static int bch2_fill_extent(struct bch_fs *c,
844                             struct fiemap_extent_info *info,
845                             struct bkey_s_c k, unsigned flags)
846 {
847         if (bkey_extent_is_direct_data(k.k)) {
848                 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
849                 const union bch_extent_entry *entry;
850                 struct extent_ptr_decoded p;
851                 int ret;
852
853                 if (k.k->type == KEY_TYPE_reflink_v)
854                         flags |= FIEMAP_EXTENT_SHARED;
855
856                 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
857                         int flags2 = 0;
858                         u64 offset = p.ptr.offset;
859
860                         if (p.ptr.unwritten)
861                                 flags2 |= FIEMAP_EXTENT_UNWRITTEN;
862
863                         if (p.crc.compression_type)
864                                 flags2 |= FIEMAP_EXTENT_ENCODED;
865                         else
866                                 offset += p.crc.offset;
867
868                         if ((offset & (block_sectors(c) - 1)) ||
869                             (k.k->size & (block_sectors(c) - 1)))
870                                 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
871
872                         ret = fiemap_fill_next_extent(info,
873                                                 bkey_start_offset(k.k) << 9,
874                                                 offset << 9,
875                                                 k.k->size << 9, flags|flags2);
876                         if (ret)
877                                 return ret;
878                 }
879
880                 return 0;
881         } else if (bkey_extent_is_inline_data(k.k)) {
882                 return fiemap_fill_next_extent(info,
883                                                bkey_start_offset(k.k) << 9,
884                                                0, k.k->size << 9,
885                                                flags|
886                                                FIEMAP_EXTENT_DATA_INLINE);
887         } else if (k.k->type == KEY_TYPE_reservation) {
888                 return fiemap_fill_next_extent(info,
889                                                bkey_start_offset(k.k) << 9,
890                                                0, k.k->size << 9,
891                                                flags|
892                                                FIEMAP_EXTENT_DELALLOC|
893                                                FIEMAP_EXTENT_UNWRITTEN);
894         } else {
895                 BUG();
896         }
897 }
898
899 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
900                        u64 start, u64 len)
901 {
902         struct bch_fs *c = vinode->i_sb->s_fs_info;
903         struct bch_inode_info *ei = to_bch_ei(vinode);
904         struct btree_trans *trans;
905         struct btree_iter iter;
906         struct bkey_s_c k;
907         struct bkey_buf cur, prev;
908         struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
909         unsigned offset_into_extent, sectors;
910         bool have_extent = false;
911         u32 snapshot;
912         int ret = 0;
913
914         ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
915         if (ret)
916                 return ret;
917
918         if (start + len < start)
919                 return -EINVAL;
920
921         start >>= 9;
922
923         bch2_bkey_buf_init(&cur);
924         bch2_bkey_buf_init(&prev);
925         trans = bch2_trans_get(c);
926 retry:
927         bch2_trans_begin(trans);
928
929         ret = bch2_subvolume_get_snapshot(trans, ei->ei_subvol, &snapshot);
930         if (ret)
931                 goto err;
932
933         bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
934                              SPOS(ei->v.i_ino, start, snapshot), 0);
935
936         while (!(ret = btree_trans_too_many_iters(trans)) &&
937                (k = bch2_btree_iter_peek_upto(&iter, end)).k &&
938                !(ret = bkey_err(k))) {
939                 enum btree_id data_btree = BTREE_ID_extents;
940
941                 if (!bkey_extent_is_data(k.k) &&
942                     k.k->type != KEY_TYPE_reservation) {
943                         bch2_btree_iter_advance(&iter);
944                         continue;
945                 }
946
947                 offset_into_extent      = iter.pos.offset -
948                         bkey_start_offset(k.k);
949                 sectors                 = k.k->size - offset_into_extent;
950
951                 bch2_bkey_buf_reassemble(&cur, c, k);
952
953                 ret = bch2_read_indirect_extent(trans, &data_btree,
954                                         &offset_into_extent, &cur);
955                 if (ret)
956                         break;
957
958                 k = bkey_i_to_s_c(cur.k);
959                 bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
960
961                 sectors = min(sectors, k.k->size - offset_into_extent);
962
963                 bch2_cut_front(POS(k.k->p.inode,
964                                    bkey_start_offset(k.k) +
965                                    offset_into_extent),
966                                cur.k);
967                 bch2_key_resize(&cur.k->k, sectors);
968                 cur.k->k.p = iter.pos;
969                 cur.k->k.p.offset += cur.k->k.size;
970
971                 if (have_extent) {
972                         bch2_trans_unlock(trans);
973                         ret = bch2_fill_extent(c, info,
974                                         bkey_i_to_s_c(prev.k), 0);
975                         if (ret)
976                                 break;
977                 }
978
979                 bkey_copy(prev.k, cur.k);
980                 have_extent = true;
981
982                 bch2_btree_iter_set_pos(&iter,
983                         POS(iter.pos.inode, iter.pos.offset + sectors));
984         }
985         start = iter.pos.offset;
986         bch2_trans_iter_exit(trans, &iter);
987 err:
988         if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
989                 goto retry;
990
991         if (!ret && have_extent) {
992                 bch2_trans_unlock(trans);
993                 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
994                                        FIEMAP_EXTENT_LAST);
995         }
996
997         bch2_trans_put(trans);
998         bch2_bkey_buf_exit(&cur, c);
999         bch2_bkey_buf_exit(&prev, c);
1000         return ret < 0 ? ret : 0;
1001 }
1002
1003 static const struct vm_operations_struct bch_vm_ops = {
1004         .fault          = bch2_page_fault,
1005         .map_pages      = filemap_map_pages,
1006         .page_mkwrite   = bch2_page_mkwrite,
1007 };
1008
1009 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
1010 {
1011         file_accessed(file);
1012
1013         vma->vm_ops = &bch_vm_ops;
1014         return 0;
1015 }
1016
1017 /* Directories: */
1018
1019 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
1020 {
1021         return generic_file_llseek_size(file, offset, whence,
1022                                         S64_MAX, S64_MAX);
1023 }
1024
1025 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
1026 {
1027         struct bch_inode_info *inode = file_bch_inode(file);
1028         struct bch_fs *c = inode->v.i_sb->s_fs_info;
1029
1030         if (!dir_emit_dots(file, ctx))
1031                 return 0;
1032
1033         int ret = bch2_readdir(c, inode_inum(inode), ctx);
1034
1035         bch_err_fn(c, ret);
1036         return bch2_err_class(ret);
1037 }
1038
1039 static int bch2_open(struct inode *vinode, struct file *file)
1040 {
1041         if (file->f_flags & (O_WRONLY|O_RDWR)) {
1042                 struct bch_inode_info *inode = to_bch_ei(vinode);
1043                 struct bch_fs *c = inode->v.i_sb->s_fs_info;
1044
1045                 int ret = bch2_subvol_is_ro(c, inode->ei_subvol);
1046                 if (ret)
1047                         return ret;
1048         }
1049
1050         return generic_file_open(vinode, file);
1051 }
1052
1053 static const struct file_operations bch_file_operations = {
1054         .open           = bch2_open,
1055         .llseek         = bch2_llseek,
1056         .read_iter      = bch2_read_iter,
1057         .write_iter     = bch2_write_iter,
1058         .mmap           = bch2_mmap,
1059         .fsync          = bch2_fsync,
1060         .splice_read    = filemap_splice_read,
1061         .splice_write   = iter_file_splice_write,
1062         .fallocate      = bch2_fallocate_dispatch,
1063         .unlocked_ioctl = bch2_fs_file_ioctl,
1064 #ifdef CONFIG_COMPAT
1065         .compat_ioctl   = bch2_compat_fs_ioctl,
1066 #endif
1067         .remap_file_range = bch2_remap_file_range,
1068 };
1069
1070 static const struct inode_operations bch_file_inode_operations = {
1071         .getattr        = bch2_getattr,
1072         .setattr        = bch2_setattr,
1073         .fiemap         = bch2_fiemap,
1074         .listxattr      = bch2_xattr_list,
1075 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1076         .get_acl        = bch2_get_acl,
1077         .set_acl        = bch2_set_acl,
1078 #endif
1079 };
1080
1081 static const struct inode_operations bch_dir_inode_operations = {
1082         .lookup         = bch2_lookup,
1083         .create         = bch2_create,
1084         .link           = bch2_link,
1085         .unlink         = bch2_unlink,
1086         .symlink        = bch2_symlink,
1087         .mkdir          = bch2_mkdir,
1088         .rmdir          = bch2_unlink,
1089         .mknod          = bch2_mknod,
1090         .rename         = bch2_rename2,
1091         .getattr        = bch2_getattr,
1092         .setattr        = bch2_setattr,
1093         .tmpfile        = bch2_tmpfile,
1094         .listxattr      = bch2_xattr_list,
1095 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1096         .get_acl        = bch2_get_acl,
1097         .set_acl        = bch2_set_acl,
1098 #endif
1099 };
1100
1101 static const struct file_operations bch_dir_file_operations = {
1102         .llseek         = bch2_dir_llseek,
1103         .read           = generic_read_dir,
1104         .iterate_shared = bch2_vfs_readdir,
1105         .fsync          = bch2_fsync,
1106         .unlocked_ioctl = bch2_fs_file_ioctl,
1107 #ifdef CONFIG_COMPAT
1108         .compat_ioctl   = bch2_compat_fs_ioctl,
1109 #endif
1110 };
1111
1112 static const struct inode_operations bch_symlink_inode_operations = {
1113         .get_link       = page_get_link,
1114         .getattr        = bch2_getattr,
1115         .setattr        = bch2_setattr,
1116         .listxattr      = bch2_xattr_list,
1117 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1118         .get_acl        = bch2_get_acl,
1119         .set_acl        = bch2_set_acl,
1120 #endif
1121 };
1122
1123 static const struct inode_operations bch_special_inode_operations = {
1124         .getattr        = bch2_getattr,
1125         .setattr        = bch2_setattr,
1126         .listxattr      = bch2_xattr_list,
1127 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1128         .get_acl        = bch2_get_acl,
1129         .set_acl        = bch2_set_acl,
1130 #endif
1131 };
1132
1133 static const struct address_space_operations bch_address_space_operations = {
1134         .read_folio     = bch2_read_folio,
1135         .writepages     = bch2_writepages,
1136         .readahead      = bch2_readahead,
1137         .dirty_folio    = filemap_dirty_folio,
1138         .write_begin    = bch2_write_begin,
1139         .write_end      = bch2_write_end,
1140         .invalidate_folio = bch2_invalidate_folio,
1141         .release_folio  = bch2_release_folio,
1142         .direct_IO      = noop_direct_IO,
1143 #ifdef CONFIG_MIGRATION
1144         .migrate_folio  = filemap_migrate_folio,
1145 #endif
1146         .error_remove_folio = generic_error_remove_folio,
1147 };
1148
1149 struct bcachefs_fid {
1150         u64             inum;
1151         u32             subvol;
1152         u32             gen;
1153 } __packed;
1154
1155 struct bcachefs_fid_with_parent {
1156         struct bcachefs_fid     fid;
1157         struct bcachefs_fid     dir;
1158 } __packed;
1159
1160 static int bcachefs_fid_valid(int fh_len, int fh_type)
1161 {
1162         switch (fh_type) {
1163         case FILEID_BCACHEFS_WITHOUT_PARENT:
1164                 return fh_len == sizeof(struct bcachefs_fid) / sizeof(u32);
1165         case FILEID_BCACHEFS_WITH_PARENT:
1166                 return fh_len == sizeof(struct bcachefs_fid_with_parent) / sizeof(u32);
1167         default:
1168                 return false;
1169         }
1170 }
1171
1172 static struct bcachefs_fid bch2_inode_to_fid(struct bch_inode_info *inode)
1173 {
1174         return (struct bcachefs_fid) {
1175                 .inum   = inode->ei_inode.bi_inum,
1176                 .subvol = inode->ei_subvol,
1177                 .gen    = inode->ei_inode.bi_generation,
1178         };
1179 }
1180
1181 static int bch2_encode_fh(struct inode *vinode, u32 *fh, int *len,
1182                           struct inode *vdir)
1183 {
1184         struct bch_inode_info *inode    = to_bch_ei(vinode);
1185         struct bch_inode_info *dir      = to_bch_ei(vdir);
1186         int min_len;
1187
1188         if (!S_ISDIR(inode->v.i_mode) && dir) {
1189                 struct bcachefs_fid_with_parent *fid = (void *) fh;
1190
1191                 min_len = sizeof(*fid) / sizeof(u32);
1192                 if (*len < min_len) {
1193                         *len = min_len;
1194                         return FILEID_INVALID;
1195                 }
1196
1197                 fid->fid = bch2_inode_to_fid(inode);
1198                 fid->dir = bch2_inode_to_fid(dir);
1199
1200                 *len = min_len;
1201                 return FILEID_BCACHEFS_WITH_PARENT;
1202         } else {
1203                 struct bcachefs_fid *fid = (void *) fh;
1204
1205                 min_len = sizeof(*fid) / sizeof(u32);
1206                 if (*len < min_len) {
1207                         *len = min_len;
1208                         return FILEID_INVALID;
1209                 }
1210                 *fid = bch2_inode_to_fid(inode);
1211
1212                 *len = min_len;
1213                 return FILEID_BCACHEFS_WITHOUT_PARENT;
1214         }
1215 }
1216
1217 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1218                                         struct bcachefs_fid fid)
1219 {
1220         struct bch_fs *c = sb->s_fs_info;
1221         struct inode *vinode = bch2_vfs_inode_get(c, (subvol_inum) {
1222                                     .subvol = fid.subvol,
1223                                     .inum = fid.inum,
1224         });
1225         if (!IS_ERR(vinode) && vinode->i_generation != fid.gen) {
1226                 iput(vinode);
1227                 vinode = ERR_PTR(-ESTALE);
1228         }
1229         return vinode;
1230 }
1231
1232 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *_fid,
1233                 int fh_len, int fh_type)
1234 {
1235         struct bcachefs_fid *fid = (void *) _fid;
1236
1237         if (!bcachefs_fid_valid(fh_len, fh_type))
1238                 return NULL;
1239
1240         return d_obtain_alias(bch2_nfs_get_inode(sb, *fid));
1241 }
1242
1243 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *_fid,
1244                 int fh_len, int fh_type)
1245 {
1246         struct bcachefs_fid_with_parent *fid = (void *) _fid;
1247
1248         if (!bcachefs_fid_valid(fh_len, fh_type) ||
1249             fh_type != FILEID_BCACHEFS_WITH_PARENT)
1250                 return NULL;
1251
1252         return d_obtain_alias(bch2_nfs_get_inode(sb, fid->dir));
1253 }
1254
1255 static struct dentry *bch2_get_parent(struct dentry *child)
1256 {
1257         struct bch_inode_info *inode = to_bch_ei(child->d_inode);
1258         struct bch_fs *c = inode->v.i_sb->s_fs_info;
1259         subvol_inum parent_inum = {
1260                 .subvol = inode->ei_inode.bi_parent_subvol ?:
1261                         inode->ei_subvol,
1262                 .inum = inode->ei_inode.bi_dir,
1263         };
1264
1265         return d_obtain_alias(bch2_vfs_inode_get(c, parent_inum));
1266 }
1267
1268 static int bch2_get_name(struct dentry *parent, char *name, struct dentry *child)
1269 {
1270         struct bch_inode_info *inode    = to_bch_ei(child->d_inode);
1271         struct bch_inode_info *dir      = to_bch_ei(parent->d_inode);
1272         struct bch_fs *c = inode->v.i_sb->s_fs_info;
1273         struct btree_trans *trans;
1274         struct btree_iter iter1;
1275         struct btree_iter iter2;
1276         struct bkey_s_c k;
1277         struct bkey_s_c_dirent d;
1278         struct bch_inode_unpacked inode_u;
1279         subvol_inum target;
1280         u32 snapshot;
1281         struct qstr dirent_name;
1282         unsigned name_len = 0;
1283         int ret;
1284
1285         if (!S_ISDIR(dir->v.i_mode))
1286                 return -EINVAL;
1287
1288         trans = bch2_trans_get(c);
1289
1290         bch2_trans_iter_init(trans, &iter1, BTREE_ID_dirents,
1291                              POS(dir->ei_inode.bi_inum, 0), 0);
1292         bch2_trans_iter_init(trans, &iter2, BTREE_ID_dirents,
1293                              POS(dir->ei_inode.bi_inum, 0), 0);
1294 retry:
1295         bch2_trans_begin(trans);
1296
1297         ret = bch2_subvolume_get_snapshot(trans, dir->ei_subvol, &snapshot);
1298         if (ret)
1299                 goto err;
1300
1301         bch2_btree_iter_set_snapshot(&iter1, snapshot);
1302         bch2_btree_iter_set_snapshot(&iter2, snapshot);
1303
1304         ret = bch2_inode_find_by_inum_trans(trans, inode_inum(inode), &inode_u);
1305         if (ret)
1306                 goto err;
1307
1308         if (inode_u.bi_dir == dir->ei_inode.bi_inum) {
1309                 bch2_btree_iter_set_pos(&iter1, POS(inode_u.bi_dir, inode_u.bi_dir_offset));
1310
1311                 k = bch2_btree_iter_peek_slot(&iter1);
1312                 ret = bkey_err(k);
1313                 if (ret)
1314                         goto err;
1315
1316                 if (k.k->type != KEY_TYPE_dirent) {
1317                         ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
1318                         goto err;
1319                 }
1320
1321                 d = bkey_s_c_to_dirent(k);
1322                 ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
1323                 if (ret > 0)
1324                         ret = -BCH_ERR_ENOENT_dirent_doesnt_match_inode;
1325                 if (ret)
1326                         goto err;
1327
1328                 if (target.subvol       == inode->ei_subvol &&
1329                     target.inum         == inode->ei_inode.bi_inum)
1330                         goto found;
1331         } else {
1332                 /*
1333                  * File with multiple hardlinks and our backref is to the wrong
1334                  * directory - linear search:
1335                  */
1336                 for_each_btree_key_continue_norestart(iter2, 0, k, ret) {
1337                         if (k.k->p.inode > dir->ei_inode.bi_inum)
1338                                 break;
1339
1340                         if (k.k->type != KEY_TYPE_dirent)
1341                                 continue;
1342
1343                         d = bkey_s_c_to_dirent(k);
1344                         ret = bch2_dirent_read_target(trans, inode_inum(dir), d, &target);
1345                         if (ret < 0)
1346                                 break;
1347                         if (ret)
1348                                 continue;
1349
1350                         if (target.subvol       == inode->ei_subvol &&
1351                             target.inum         == inode->ei_inode.bi_inum)
1352                                 goto found;
1353                 }
1354         }
1355
1356         ret = -ENOENT;
1357         goto err;
1358 found:
1359         dirent_name = bch2_dirent_get_name(d);
1360
1361         name_len = min_t(unsigned, dirent_name.len, NAME_MAX);
1362         memcpy(name, dirent_name.name, name_len);
1363         name[name_len] = '\0';
1364 err:
1365         if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
1366                 goto retry;
1367
1368         bch2_trans_iter_exit(trans, &iter1);
1369         bch2_trans_iter_exit(trans, &iter2);
1370         bch2_trans_put(trans);
1371
1372         return ret;
1373 }
1374
1375 static const struct export_operations bch_export_ops = {
1376         .encode_fh      = bch2_encode_fh,
1377         .fh_to_dentry   = bch2_fh_to_dentry,
1378         .fh_to_parent   = bch2_fh_to_parent,
1379         .get_parent     = bch2_get_parent,
1380         .get_name       = bch2_get_name,
1381 };
1382
1383 static void bch2_vfs_inode_init(struct btree_trans *trans, subvol_inum inum,
1384                                 struct bch_inode_info *inode,
1385                                 struct bch_inode_unpacked *bi,
1386                                 struct bch_subvolume *subvol)
1387 {
1388         bch2_iget5_set(&inode->v, &inum);
1389         bch2_inode_update_after_write(trans, inode, bi, ~0);
1390
1391         if (BCH_SUBVOLUME_SNAP(subvol))
1392                 set_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1393         else
1394                 clear_bit(EI_INODE_SNAPSHOT, &inode->ei_flags);
1395
1396         inode->v.i_blocks       = bi->bi_sectors;
1397         inode->v.i_ino          = bi->bi_inum;
1398         inode->v.i_rdev         = bi->bi_dev;
1399         inode->v.i_generation   = bi->bi_generation;
1400         inode->v.i_size         = bi->bi_size;
1401
1402         inode->ei_flags         = 0;
1403         inode->ei_quota_reserved = 0;
1404         inode->ei_qid           = bch_qid(bi);
1405         inode->ei_subvol        = inum.subvol;
1406
1407         inode->v.i_mapping->a_ops = &bch_address_space_operations;
1408
1409         switch (inode->v.i_mode & S_IFMT) {
1410         case S_IFREG:
1411                 inode->v.i_op   = &bch_file_inode_operations;
1412                 inode->v.i_fop  = &bch_file_operations;
1413                 break;
1414         case S_IFDIR:
1415                 inode->v.i_op   = &bch_dir_inode_operations;
1416                 inode->v.i_fop  = &bch_dir_file_operations;
1417                 break;
1418         case S_IFLNK:
1419                 inode_nohighmem(&inode->v);
1420                 inode->v.i_op   = &bch_symlink_inode_operations;
1421                 break;
1422         default:
1423                 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1424                 inode->v.i_op   = &bch_special_inode_operations;
1425                 break;
1426         }
1427
1428         mapping_set_large_folios(inode->v.i_mapping);
1429 }
1430
1431 static struct inode *bch2_alloc_inode(struct super_block *sb)
1432 {
1433         struct bch_inode_info *inode;
1434
1435         inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1436         if (!inode)
1437                 return NULL;
1438
1439         inode_init_once(&inode->v);
1440         mutex_init(&inode->ei_update_lock);
1441         two_state_lock_init(&inode->ei_pagecache_lock);
1442         INIT_LIST_HEAD(&inode->ei_vfs_inode_list);
1443         mutex_init(&inode->ei_quota_lock);
1444
1445         return &inode->v;
1446 }
1447
1448 static void bch2_i_callback(struct rcu_head *head)
1449 {
1450         struct inode *vinode = container_of(head, struct inode, i_rcu);
1451         struct bch_inode_info *inode = to_bch_ei(vinode);
1452
1453         kmem_cache_free(bch2_inode_cache, inode);
1454 }
1455
1456 static void bch2_destroy_inode(struct inode *vinode)
1457 {
1458         call_rcu(&vinode->i_rcu, bch2_i_callback);
1459 }
1460
1461 static int inode_update_times_fn(struct btree_trans *trans,
1462                                  struct bch_inode_info *inode,
1463                                  struct bch_inode_unpacked *bi,
1464                                  void *p)
1465 {
1466         struct bch_fs *c = inode->v.i_sb->s_fs_info;
1467
1468         bi->bi_atime    = timespec_to_bch2_time(c, inode_get_atime(&inode->v));
1469         bi->bi_mtime    = timespec_to_bch2_time(c, inode_get_mtime(&inode->v));
1470         bi->bi_ctime    = timespec_to_bch2_time(c, inode_get_ctime(&inode->v));
1471
1472         return 0;
1473 }
1474
1475 static int bch2_vfs_write_inode(struct inode *vinode,
1476                                 struct writeback_control *wbc)
1477 {
1478         struct bch_fs *c = vinode->i_sb->s_fs_info;
1479         struct bch_inode_info *inode = to_bch_ei(vinode);
1480         int ret;
1481
1482         mutex_lock(&inode->ei_update_lock);
1483         ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1484                                ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1485         mutex_unlock(&inode->ei_update_lock);
1486
1487         return bch2_err_class(ret);
1488 }
1489
1490 static void bch2_evict_inode(struct inode *vinode)
1491 {
1492         struct bch_fs *c = vinode->i_sb->s_fs_info;
1493         struct bch_inode_info *inode = to_bch_ei(vinode);
1494
1495         truncate_inode_pages_final(&inode->v.i_data);
1496
1497         clear_inode(&inode->v);
1498
1499         BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1500
1501         if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1502                 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1503                                 KEY_TYPE_QUOTA_WARN);
1504                 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1505                                 KEY_TYPE_QUOTA_WARN);
1506                 bch2_inode_rm(c, inode_inum(inode));
1507         }
1508
1509         mutex_lock(&c->vfs_inodes_lock);
1510         list_del_init(&inode->ei_vfs_inode_list);
1511         mutex_unlock(&c->vfs_inodes_lock);
1512 }
1513
1514 void bch2_evict_subvolume_inodes(struct bch_fs *c, snapshot_id_list *s)
1515 {
1516         struct bch_inode_info *inode;
1517         DARRAY(struct bch_inode_info *) grabbed;
1518         bool clean_pass = false, this_pass_clean;
1519
1520         /*
1521          * Initially, we scan for inodes without I_DONTCACHE, then mark them to
1522          * be pruned with d_mark_dontcache().
1523          *
1524          * Once we've had a clean pass where we didn't find any inodes without
1525          * I_DONTCACHE, we wait for them to be freed:
1526          */
1527
1528         darray_init(&grabbed);
1529         darray_make_room(&grabbed, 1024);
1530 again:
1531         cond_resched();
1532         this_pass_clean = true;
1533
1534         mutex_lock(&c->vfs_inodes_lock);
1535         list_for_each_entry(inode, &c->vfs_inodes_list, ei_vfs_inode_list) {
1536                 if (!snapshot_list_has_id(s, inode->ei_subvol))
1537                         continue;
1538
1539                 if (!(inode->v.i_state & I_DONTCACHE) &&
1540                     !(inode->v.i_state & I_FREEING) &&
1541                     igrab(&inode->v)) {
1542                         this_pass_clean = false;
1543
1544                         if (darray_push_gfp(&grabbed, inode, GFP_ATOMIC|__GFP_NOWARN)) {
1545                                 iput(&inode->v);
1546                                 break;
1547                         }
1548                 } else if (clean_pass && this_pass_clean) {
1549                         wait_queue_head_t *wq = bit_waitqueue(&inode->v.i_state, __I_NEW);
1550                         DEFINE_WAIT_BIT(wait, &inode->v.i_state, __I_NEW);
1551
1552                         prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
1553                         mutex_unlock(&c->vfs_inodes_lock);
1554
1555                         schedule();
1556                         finish_wait(wq, &wait.wq_entry);
1557                         goto again;
1558                 }
1559         }
1560         mutex_unlock(&c->vfs_inodes_lock);
1561
1562         darray_for_each(grabbed, i) {
1563                 inode = *i;
1564                 d_mark_dontcache(&inode->v);
1565                 d_prune_aliases(&inode->v);
1566                 iput(&inode->v);
1567         }
1568         grabbed.nr = 0;
1569
1570         if (!clean_pass || !this_pass_clean) {
1571                 clean_pass = this_pass_clean;
1572                 goto again;
1573         }
1574
1575         darray_exit(&grabbed);
1576 }
1577
1578 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1579 {
1580         struct super_block *sb = dentry->d_sb;
1581         struct bch_fs *c = sb->s_fs_info;
1582         struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1583         unsigned shift = sb->s_blocksize_bits - 9;
1584         /*
1585          * this assumes inodes take up 64 bytes, which is a decent average
1586          * number:
1587          */
1588         u64 avail_inodes = ((usage.capacity - usage.used) << 3);
1589
1590         buf->f_type     = BCACHEFS_STATFS_MAGIC;
1591         buf->f_bsize    = sb->s_blocksize;
1592         buf->f_blocks   = usage.capacity >> shift;
1593         buf->f_bfree    = usage.free >> shift;
1594         buf->f_bavail   = avail_factor(usage.free) >> shift;
1595
1596         buf->f_files    = usage.nr_inodes + avail_inodes;
1597         buf->f_ffree    = avail_inodes;
1598
1599         buf->f_fsid     = uuid_to_fsid(c->sb.user_uuid.b);
1600         buf->f_namelen  = BCH_NAME_MAX;
1601
1602         return 0;
1603 }
1604
1605 static int bch2_sync_fs(struct super_block *sb, int wait)
1606 {
1607         struct bch_fs *c = sb->s_fs_info;
1608         int ret;
1609
1610         if (c->opts.journal_flush_disabled)
1611                 return 0;
1612
1613         if (!wait) {
1614                 bch2_journal_flush_async(&c->journal, NULL);
1615                 return 0;
1616         }
1617
1618         ret = bch2_journal_flush(&c->journal);
1619         return bch2_err_class(ret);
1620 }
1621
1622 static struct bch_fs *bch2_path_to_fs(const char *path)
1623 {
1624         struct bch_fs *c;
1625         dev_t dev;
1626         int ret;
1627
1628         ret = lookup_bdev(path, &dev);
1629         if (ret)
1630                 return ERR_PTR(ret);
1631
1632         c = bch2_dev_to_fs(dev);
1633         if (c)
1634                 closure_put(&c->cl);
1635         return c ?: ERR_PTR(-ENOENT);
1636 }
1637
1638 static int bch2_remount(struct super_block *sb, int *flags, char *data)
1639 {
1640         struct bch_fs *c = sb->s_fs_info;
1641         struct bch_opts opts = bch2_opts_empty();
1642         int ret;
1643
1644         ret = bch2_parse_mount_opts(c, &opts, data);
1645         if (ret)
1646                 goto err;
1647
1648         opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1649
1650         if (opts.read_only != c->opts.read_only) {
1651                 down_write(&c->state_lock);
1652
1653                 if (opts.read_only) {
1654                         bch2_fs_read_only(c);
1655
1656                         sb->s_flags |= SB_RDONLY;
1657                 } else {
1658                         ret = bch2_fs_read_write(c);
1659                         if (ret) {
1660                                 bch_err(c, "error going rw: %i", ret);
1661                                 up_write(&c->state_lock);
1662                                 ret = -EINVAL;
1663                                 goto err;
1664                         }
1665
1666                         sb->s_flags &= ~SB_RDONLY;
1667                 }
1668
1669                 c->opts.read_only = opts.read_only;
1670
1671                 up_write(&c->state_lock);
1672         }
1673
1674         if (opt_defined(opts, errors))
1675                 c->opts.errors = opts.errors;
1676 err:
1677         return bch2_err_class(ret);
1678 }
1679
1680 static int bch2_show_devname(struct seq_file *seq, struct dentry *root)
1681 {
1682         struct bch_fs *c = root->d_sb->s_fs_info;
1683         bool first = true;
1684
1685         for_each_online_member(c, ca) {
1686                 if (!first)
1687                         seq_putc(seq, ':');
1688                 first = false;
1689                 seq_puts(seq, ca->disk_sb.sb_name);
1690         }
1691
1692         return 0;
1693 }
1694
1695 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1696 {
1697         struct bch_fs *c = root->d_sb->s_fs_info;
1698         enum bch_opt_id i;
1699         struct printbuf buf = PRINTBUF;
1700         int ret = 0;
1701
1702         for (i = 0; i < bch2_opts_nr; i++) {
1703                 const struct bch_option *opt = &bch2_opt_table[i];
1704                 u64 v = bch2_opt_get_by_id(&c->opts, i);
1705
1706                 if (!(opt->flags & OPT_MOUNT))
1707                         continue;
1708
1709                 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1710                         continue;
1711
1712                 printbuf_reset(&buf);
1713                 bch2_opt_to_text(&buf, c, c->disk_sb.sb, opt, v,
1714                                  OPT_SHOW_MOUNT_STYLE);
1715                 seq_putc(seq, ',');
1716                 seq_puts(seq, buf.buf);
1717         }
1718
1719         if (buf.allocation_failure)
1720                 ret = -ENOMEM;
1721         printbuf_exit(&buf);
1722         return ret;
1723 }
1724
1725 static void bch2_put_super(struct super_block *sb)
1726 {
1727         struct bch_fs *c = sb->s_fs_info;
1728
1729         __bch2_fs_stop(c);
1730 }
1731
1732 /*
1733  * bcachefs doesn't currently integrate intwrite freeze protection but the
1734  * internal write references serve the same purpose. Therefore reuse the
1735  * read-only transition code to perform the quiesce. The caveat is that we don't
1736  * currently have the ability to block tasks that want a write reference while
1737  * the superblock is frozen. This is fine for now, but we should either add
1738  * blocking support or find a way to integrate sb_start_intwrite() and friends.
1739  */
1740 static int bch2_freeze(struct super_block *sb)
1741 {
1742         struct bch_fs *c = sb->s_fs_info;
1743
1744         down_write(&c->state_lock);
1745         bch2_fs_read_only(c);
1746         up_write(&c->state_lock);
1747         return 0;
1748 }
1749
1750 static int bch2_unfreeze(struct super_block *sb)
1751 {
1752         struct bch_fs *c = sb->s_fs_info;
1753         int ret;
1754
1755         if (test_bit(BCH_FS_emergency_ro, &c->flags))
1756                 return 0;
1757
1758         down_write(&c->state_lock);
1759         ret = bch2_fs_read_write(c);
1760         up_write(&c->state_lock);
1761         return ret;
1762 }
1763
1764 static const struct super_operations bch_super_operations = {
1765         .alloc_inode    = bch2_alloc_inode,
1766         .destroy_inode  = bch2_destroy_inode,
1767         .write_inode    = bch2_vfs_write_inode,
1768         .evict_inode    = bch2_evict_inode,
1769         .sync_fs        = bch2_sync_fs,
1770         .statfs         = bch2_statfs,
1771         .show_devname   = bch2_show_devname,
1772         .show_options   = bch2_show_options,
1773         .remount_fs     = bch2_remount,
1774         .put_super      = bch2_put_super,
1775         .freeze_fs      = bch2_freeze,
1776         .unfreeze_fs    = bch2_unfreeze,
1777 };
1778
1779 static int bch2_set_super(struct super_block *s, void *data)
1780 {
1781         s->s_fs_info = data;
1782         return 0;
1783 }
1784
1785 static int bch2_noset_super(struct super_block *s, void *data)
1786 {
1787         return -EBUSY;
1788 }
1789
1790 typedef DARRAY(struct bch_fs *) darray_fs;
1791
1792 static int bch2_test_super(struct super_block *s, void *data)
1793 {
1794         struct bch_fs *c = s->s_fs_info;
1795         darray_fs *d = data;
1796
1797         if (!c)
1798                 return false;
1799
1800         darray_for_each(*d, i)
1801                 if (c != *i)
1802                         return false;
1803         return true;
1804 }
1805
1806 static struct dentry *bch2_mount(struct file_system_type *fs_type,
1807                                  int flags, const char *dev_name, void *data)
1808 {
1809         struct bch_fs *c;
1810         struct super_block *sb;
1811         struct inode *vinode;
1812         struct bch_opts opts = bch2_opts_empty();
1813         int ret;
1814
1815         opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1816
1817         ret = bch2_parse_mount_opts(NULL, &opts, data);
1818         if (ret)
1819                 return ERR_PTR(ret);
1820
1821         if (!dev_name || strlen(dev_name) == 0)
1822                 return ERR_PTR(-EINVAL);
1823
1824         darray_str devs;
1825         ret = bch2_split_devs(dev_name, &devs);
1826         if (ret)
1827                 return ERR_PTR(ret);
1828
1829         darray_fs devs_to_fs = {};
1830         darray_for_each(devs, i) {
1831                 ret = darray_push(&devs_to_fs, bch2_path_to_fs(*i));
1832                 if (ret) {
1833                         sb = ERR_PTR(ret);
1834                         goto got_sb;
1835                 }
1836         }
1837
1838         sb = sget(fs_type, bch2_test_super, bch2_noset_super, flags|SB_NOSEC, &devs_to_fs);
1839         if (!IS_ERR(sb))
1840                 goto got_sb;
1841
1842         c = bch2_fs_open(devs.data, devs.nr, opts);
1843         if (IS_ERR(c)) {
1844                 sb = ERR_CAST(c);
1845                 goto got_sb;
1846         }
1847
1848         /* Some options can't be parsed until after the fs is started: */
1849         ret = bch2_parse_mount_opts(c, &opts, data);
1850         if (ret) {
1851                 bch2_fs_stop(c);
1852                 sb = ERR_PTR(ret);
1853                 goto got_sb;
1854         }
1855
1856         bch2_opts_apply(&c->opts, opts);
1857
1858         sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
1859         if (IS_ERR(sb))
1860                 bch2_fs_stop(c);
1861 got_sb:
1862         darray_exit(&devs_to_fs);
1863         bch2_darray_str_exit(&devs);
1864
1865         if (IS_ERR(sb)) {
1866                 ret = PTR_ERR(sb);
1867                 ret = bch2_err_class(ret);
1868                 return ERR_PTR(ret);
1869         }
1870
1871         c = sb->s_fs_info;
1872
1873         if (sb->s_root) {
1874                 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1875                         ret = -EBUSY;
1876                         goto err_put_super;
1877                 }
1878                 goto out;
1879         }
1880
1881         sb->s_blocksize         = block_bytes(c);
1882         sb->s_blocksize_bits    = ilog2(block_bytes(c));
1883         sb->s_maxbytes          = MAX_LFS_FILESIZE;
1884         sb->s_op                = &bch_super_operations;
1885         sb->s_export_op         = &bch_export_ops;
1886 #ifdef CONFIG_BCACHEFS_QUOTA
1887         sb->s_qcop              = &bch2_quotactl_operations;
1888         sb->s_quota_types       = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1889 #endif
1890         sb->s_xattr             = bch2_xattr_handlers;
1891         sb->s_magic             = BCACHEFS_STATFS_MAGIC;
1892         sb->s_time_gran         = c->sb.nsec_per_time_unit;
1893         sb->s_time_min          = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
1894         sb->s_time_max          = div_s64(S64_MAX, c->sb.time_units_per_sec);
1895         sb->s_uuid              = c->sb.user_uuid;
1896         c->vfs_sb               = sb;
1897         strscpy(sb->s_id, c->name, sizeof(sb->s_id));
1898
1899         ret = super_setup_bdi(sb);
1900         if (ret)
1901                 goto err_put_super;
1902
1903         sb->s_bdi->ra_pages             = VM_READAHEAD_PAGES;
1904
1905         for_each_online_member(c, ca) {
1906                 struct block_device *bdev = ca->disk_sb.bdev;
1907
1908                 /* XXX: create an anonymous device for multi device filesystems */
1909                 sb->s_bdev      = bdev;
1910                 sb->s_dev       = bdev->bd_dev;
1911                 percpu_ref_put(&ca->io_ref);
1912                 break;
1913         }
1914
1915         c->dev = sb->s_dev;
1916
1917 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1918         if (c->opts.acl)
1919                 sb->s_flags     |= SB_POSIXACL;
1920 #endif
1921
1922         sb->s_shrink->seeks = 0;
1923
1924         vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_SUBVOL_INUM);
1925         ret = PTR_ERR_OR_ZERO(vinode);
1926         bch_err_msg(c, ret, "mounting: error getting root inode");
1927         if (ret)
1928                 goto err_put_super;
1929
1930         sb->s_root = d_make_root(vinode);
1931         if (!sb->s_root) {
1932                 bch_err(c, "error mounting: error allocating root dentry");
1933                 ret = -ENOMEM;
1934                 goto err_put_super;
1935         }
1936
1937         sb->s_flags |= SB_ACTIVE;
1938 out:
1939         return dget(sb->s_root);
1940
1941 err_put_super:
1942         deactivate_locked_super(sb);
1943         return ERR_PTR(bch2_err_class(ret));
1944 }
1945
1946 static void bch2_kill_sb(struct super_block *sb)
1947 {
1948         struct bch_fs *c = sb->s_fs_info;
1949
1950         generic_shutdown_super(sb);
1951         bch2_fs_free(c);
1952 }
1953
1954 static struct file_system_type bcache_fs_type = {
1955         .owner          = THIS_MODULE,
1956         .name           = "bcachefs",
1957         .mount          = bch2_mount,
1958         .kill_sb        = bch2_kill_sb,
1959         .fs_flags       = FS_REQUIRES_DEV,
1960 };
1961
1962 MODULE_ALIAS_FS("bcachefs");
1963
1964 void bch2_vfs_exit(void)
1965 {
1966         unregister_filesystem(&bcache_fs_type);
1967         kmem_cache_destroy(bch2_inode_cache);
1968 }
1969
1970 int __init bch2_vfs_init(void)
1971 {
1972         int ret = -ENOMEM;
1973
1974         bch2_inode_cache = KMEM_CACHE(bch_inode_info, SLAB_RECLAIM_ACCOUNT);
1975         if (!bch2_inode_cache)
1976                 goto err;
1977
1978         ret = register_filesystem(&bcache_fs_type);
1979         if (ret)
1980                 goto err;
1981
1982         return 0;
1983 err:
1984         bch2_vfs_exit();
1985         return ret;
1986 }
1987
1988 #endif /* NO_BCACHEFS_FS */