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