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