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