]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs.c
Merge pull request #24 from brendon-boldt/new-install-distros
[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_on_stack.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/module.h>
29 #include <linux/posix_acl.h>
30 #include <linux/random.h>
31 #include <linux/statfs.h>
32 #include <linux/xattr.h>
33
34 static struct kmem_cache *bch2_inode_cache;
35
36 static void bch2_vfs_inode_init(struct bch_fs *,
37                                 struct bch_inode_info *,
38                                 struct bch_inode_unpacked *);
39
40 static void journal_seq_copy(struct bch_inode_info *dst,
41                              u64 journal_seq)
42 {
43         u64 old, v = READ_ONCE(dst->ei_journal_seq);
44
45         do {
46                 old = v;
47
48                 if (old >= journal_seq)
49                         break;
50         } while ((v = cmpxchg(&dst->ei_journal_seq, old, journal_seq)) != old);
51 }
52
53 static void __pagecache_lock_put(struct pagecache_lock *lock, long i)
54 {
55         BUG_ON(atomic_long_read(&lock->v) == 0);
56
57         if (atomic_long_sub_return_release(i, &lock->v) == 0)
58                 wake_up_all(&lock->wait);
59 }
60
61 static bool __pagecache_lock_tryget(struct pagecache_lock *lock, long i)
62 {
63         long v = atomic_long_read(&lock->v), old;
64
65         do {
66                 old = v;
67
68                 if (i > 0 ? v < 0 : v > 0)
69                         return false;
70         } while ((v = atomic_long_cmpxchg_acquire(&lock->v,
71                                         old, old + i)) != old);
72         return true;
73 }
74
75 static void __pagecache_lock_get(struct pagecache_lock *lock, long i)
76 {
77         wait_event(lock->wait, __pagecache_lock_tryget(lock, i));
78 }
79
80 void bch2_pagecache_add_put(struct pagecache_lock *lock)
81 {
82         __pagecache_lock_put(lock, 1);
83 }
84
85 void bch2_pagecache_add_get(struct pagecache_lock *lock)
86 {
87         __pagecache_lock_get(lock, 1);
88 }
89
90 void bch2_pagecache_block_put(struct pagecache_lock *lock)
91 {
92         __pagecache_lock_put(lock, -1);
93 }
94
95 void bch2_pagecache_block_get(struct pagecache_lock *lock)
96 {
97         __pagecache_lock_get(lock, -1);
98 }
99
100 void bch2_inode_update_after_write(struct bch_fs *c,
101                                    struct bch_inode_info *inode,
102                                    struct bch_inode_unpacked *bi,
103                                    unsigned fields)
104 {
105         set_nlink(&inode->v, bch2_inode_nlink_get(bi));
106         i_uid_write(&inode->v, bi->bi_uid);
107         i_gid_write(&inode->v, bi->bi_gid);
108         inode->v.i_mode = bi->bi_mode;
109
110         if (fields & ATTR_ATIME)
111                 inode->v.i_atime = bch2_time_to_timespec(c, bi->bi_atime);
112         if (fields & ATTR_MTIME)
113                 inode->v.i_mtime = bch2_time_to_timespec(c, bi->bi_mtime);
114         if (fields & ATTR_CTIME)
115                 inode->v.i_ctime = bch2_time_to_timespec(c, bi->bi_ctime);
116
117         inode->ei_inode         = *bi;
118
119         bch2_inode_flags_to_vfs(inode);
120 }
121
122 int __must_check bch2_write_inode(struct bch_fs *c,
123                                   struct bch_inode_info *inode,
124                                   inode_set_fn set,
125                                   void *p, unsigned fields)
126 {
127         struct btree_trans trans;
128         struct btree_iter *iter;
129         struct bch_inode_unpacked inode_u;
130         int ret;
131
132         bch2_trans_init(&trans, c, 0, 0);
133 retry:
134         bch2_trans_begin(&trans);
135
136         iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
137                                BTREE_ITER_INTENT);
138         ret   = PTR_ERR_OR_ZERO(iter) ?:
139                 (set ? set(inode, &inode_u, p) : 0) ?:
140                 bch2_inode_write(&trans, iter, &inode_u) ?:
141                 bch2_trans_commit(&trans, NULL,
142                                   &inode->ei_journal_seq,
143                                   BTREE_INSERT_ATOMIC|
144                                   BTREE_INSERT_NOUNLOCK|
145                                   BTREE_INSERT_NOFAIL);
146         if (ret == -EINTR)
147                 goto retry;
148
149         /*
150          * the btree node lock protects inode->ei_inode, not ei_update_lock;
151          * this is important for inode updates via bchfs_write_index_update
152          */
153         if (!ret)
154                 bch2_inode_update_after_write(c, inode, &inode_u, fields);
155
156         bch2_trans_exit(&trans);
157         return ret < 0 ? ret : 0;
158 }
159
160 int bch2_fs_quota_transfer(struct bch_fs *c,
161                            struct bch_inode_info *inode,
162                            struct bch_qid new_qid,
163                            unsigned qtypes,
164                            enum quota_acct_mode mode)
165 {
166         unsigned i;
167         int ret;
168
169         qtypes &= enabled_qtypes(c);
170
171         for (i = 0; i < QTYP_NR; i++)
172                 if (new_qid.q[i] == inode->ei_qid.q[i])
173                         qtypes &= ~(1U << i);
174
175         if (!qtypes)
176                 return 0;
177
178         mutex_lock(&inode->ei_quota_lock);
179
180         ret = bch2_quota_transfer(c, qtypes, new_qid,
181                                   inode->ei_qid,
182                                   inode->v.i_blocks +
183                                   inode->ei_quota_reserved,
184                                   mode);
185         if (!ret)
186                 for (i = 0; i < QTYP_NR; i++)
187                         if (qtypes & (1 << i))
188                                 inode->ei_qid.q[i] = new_qid.q[i];
189
190         mutex_unlock(&inode->ei_quota_lock);
191
192         return ret;
193 }
194
195 struct inode *bch2_vfs_inode_get(struct bch_fs *c, u64 inum)
196 {
197         struct bch_inode_unpacked inode_u;
198         struct bch_inode_info *inode;
199         int ret;
200
201         inode = to_bch_ei(iget_locked(c->vfs_sb, inum));
202         if (unlikely(!inode))
203                 return ERR_PTR(-ENOMEM);
204         if (!(inode->v.i_state & I_NEW))
205                 return &inode->v;
206
207         ret = bch2_inode_find_by_inum(c, inum, &inode_u);
208         if (ret) {
209                 iget_failed(&inode->v);
210                 return ERR_PTR(ret);
211         }
212
213         bch2_vfs_inode_init(c, inode, &inode_u);
214
215         inode->ei_journal_seq = bch2_inode_journal_seq(&c->journal, inum);
216
217         unlock_new_inode(&inode->v);
218
219         return &inode->v;
220 }
221
222 static struct bch_inode_info *
223 __bch2_create(struct bch_inode_info *dir, struct dentry *dentry,
224               umode_t mode, dev_t rdev, bool tmpfile)
225 {
226         struct bch_fs *c = dir->v.i_sb->s_fs_info;
227         struct user_namespace *ns = dir->v.i_sb->s_user_ns;
228         struct btree_trans trans;
229         struct bch_inode_unpacked dir_u;
230         struct bch_inode_info *inode, *old;
231         struct bch_inode_unpacked inode_u;
232         struct posix_acl *default_acl = NULL, *acl = NULL;
233         u64 journal_seq = 0;
234         int ret;
235
236         /*
237          * preallocate acls + vfs inode before btree transaction, so that
238          * nothing can fail after the transaction succeeds:
239          */
240 #ifdef CONFIG_BCACHEFS_POSIX_ACL
241         ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
242         if (ret)
243                 return ERR_PTR(ret);
244 #endif
245         inode = to_bch_ei(new_inode(c->vfs_sb));
246         if (unlikely(!inode)) {
247                 inode = ERR_PTR(-ENOMEM);
248                 goto err;
249         }
250
251         bch2_inode_init_early(c, &inode_u);
252
253         if (!tmpfile)
254                 mutex_lock(&dir->ei_update_lock);
255
256         bch2_trans_init(&trans, c, 8, 1024);
257 retry:
258         bch2_trans_begin(&trans);
259
260         ret   = bch2_create_trans(&trans, dir->v.i_ino, &dir_u, &inode_u,
261                                   !tmpfile ? &dentry->d_name : NULL,
262                                   from_kuid(ns, current_fsuid()),
263                                   from_kgid(ns, current_fsgid()),
264                                   mode, rdev,
265                                   default_acl, acl) ?:
266                 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
267                                 KEY_TYPE_QUOTA_PREALLOC);
268         if (unlikely(ret))
269                 goto err_before_quota;
270
271         ret   = bch2_trans_commit(&trans, NULL, &journal_seq,
272                                   BTREE_INSERT_ATOMIC|
273                                   BTREE_INSERT_NOUNLOCK);
274         if (unlikely(ret)) {
275                 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
276                                 KEY_TYPE_QUOTA_WARN);
277 err_before_quota:
278                 if (ret == -EINTR)
279                         goto retry;
280                 goto err_trans;
281         }
282
283         if (!tmpfile) {
284                 bch2_inode_update_after_write(c, dir, &dir_u,
285                                               ATTR_MTIME|ATTR_CTIME);
286                 journal_seq_copy(dir, journal_seq);
287                 mutex_unlock(&dir->ei_update_lock);
288         }
289
290         bch2_vfs_inode_init(c, inode, &inode_u);
291         journal_seq_copy(inode, journal_seq);
292
293         set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
294         set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
295
296         /*
297          * we must insert the new inode into the inode cache before calling
298          * bch2_trans_exit() and dropping locks, else we could race with another
299          * thread pulling the inode in and modifying it:
300          */
301
302         old = to_bch_ei(insert_inode_locked2(&inode->v));
303         if (unlikely(old)) {
304                 /*
305                  * We raced, another process pulled the new inode into cache
306                  * before us:
307                  */
308                 journal_seq_copy(old, journal_seq);
309                 make_bad_inode(&inode->v);
310                 iput(&inode->v);
311
312                 inode = old;
313         } else {
314                 /*
315                  * we really don't want insert_inode_locked2() to be setting
316                  * I_NEW...
317                  */
318                 unlock_new_inode(&inode->v);
319         }
320
321         bch2_trans_exit(&trans);
322 err:
323         posix_acl_release(default_acl);
324         posix_acl_release(acl);
325         return inode;
326 err_trans:
327         if (!tmpfile)
328                 mutex_unlock(&dir->ei_update_lock);
329
330         bch2_trans_exit(&trans);
331         make_bad_inode(&inode->v);
332         iput(&inode->v);
333         inode = ERR_PTR(ret);
334         goto err;
335 }
336
337 /* methods */
338
339 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
340                                   unsigned int flags)
341 {
342         struct bch_fs *c = vdir->i_sb->s_fs_info;
343         struct bch_inode_info *dir = to_bch_ei(vdir);
344         struct inode *vinode = NULL;
345         u64 inum;
346
347         inum = bch2_dirent_lookup(c, dir->v.i_ino,
348                                   &dir->ei_str_hash,
349                                   &dentry->d_name);
350
351         if (inum)
352                 vinode = bch2_vfs_inode_get(c, inum);
353
354         return d_splice_alias(vinode, dentry);
355 }
356
357 static int bch2_mknod(struct inode *vdir, struct dentry *dentry,
358                       umode_t mode, dev_t rdev)
359 {
360         struct bch_inode_info *inode =
361                 __bch2_create(to_bch_ei(vdir), dentry, mode, rdev, false);
362
363         if (IS_ERR(inode))
364                 return PTR_ERR(inode);
365
366         d_instantiate(dentry, &inode->v);
367         return 0;
368 }
369
370 static int bch2_create(struct inode *vdir, struct dentry *dentry,
371                        umode_t mode, bool excl)
372 {
373         return bch2_mknod(vdir, dentry, mode|S_IFREG, 0);
374 }
375
376 static int __bch2_link(struct bch_fs *c,
377                        struct bch_inode_info *inode,
378                        struct bch_inode_info *dir,
379                        struct dentry *dentry)
380 {
381         struct btree_trans trans;
382         struct bch_inode_unpacked inode_u;
383         int ret;
384
385         mutex_lock(&inode->ei_update_lock);
386         bch2_trans_init(&trans, c, 4, 1024);
387
388         do {
389                 bch2_trans_begin(&trans);
390                 ret   = bch2_link_trans(&trans,
391                                         dir->v.i_ino,
392                                         inode->v.i_ino, &inode_u,
393                                         &dentry->d_name) ?:
394                         bch2_trans_commit(&trans, NULL,
395                                         &inode->ei_journal_seq,
396                                         BTREE_INSERT_ATOMIC|
397                                         BTREE_INSERT_NOUNLOCK);
398         } while (ret == -EINTR);
399
400         if (likely(!ret))
401                 bch2_inode_update_after_write(c, inode, &inode_u, ATTR_CTIME);
402
403         bch2_trans_exit(&trans);
404         mutex_unlock(&inode->ei_update_lock);
405         return ret;
406 }
407
408 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
409                      struct dentry *dentry)
410 {
411         struct bch_fs *c = vdir->i_sb->s_fs_info;
412         struct bch_inode_info *dir = to_bch_ei(vdir);
413         struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
414         int ret;
415
416         lockdep_assert_held(&inode->v.i_rwsem);
417
418         ret = __bch2_link(c, inode, dir, dentry);
419         if (unlikely(ret))
420                 return ret;
421
422         ihold(&inode->v);
423         d_instantiate(dentry, &inode->v);
424         return 0;
425 }
426
427 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
428 {
429         struct bch_fs *c = vdir->i_sb->s_fs_info;
430         struct bch_inode_info *dir = to_bch_ei(vdir);
431         struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
432         struct bch_inode_unpacked dir_u, inode_u;
433         struct btree_trans trans;
434         int ret;
435
436         bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
437         bch2_trans_init(&trans, c, 4, 1024);
438
439         do {
440                 bch2_trans_begin(&trans);
441
442                 ret   = bch2_unlink_trans(&trans,
443                                           dir->v.i_ino, &dir_u,
444                                           &inode_u, &dentry->d_name) ?:
445                         bch2_trans_commit(&trans, NULL,
446                                           &dir->ei_journal_seq,
447                                           BTREE_INSERT_ATOMIC|
448                                           BTREE_INSERT_NOUNLOCK|
449                                           BTREE_INSERT_NOFAIL);
450         } while (ret == -EINTR);
451
452         if (likely(!ret)) {
453                 BUG_ON(inode_u.bi_inum != inode->v.i_ino);
454
455                 journal_seq_copy(inode, dir->ei_journal_seq);
456                 bch2_inode_update_after_write(c, dir, &dir_u,
457                                               ATTR_MTIME|ATTR_CTIME);
458                 bch2_inode_update_after_write(c, inode, &inode_u,
459                                               ATTR_MTIME);
460         }
461
462         bch2_trans_exit(&trans);
463         bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
464
465         return ret;
466 }
467
468 static int bch2_symlink(struct inode *vdir, struct dentry *dentry,
469                         const char *symname)
470 {
471         struct bch_fs *c = vdir->i_sb->s_fs_info;
472         struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
473         int ret;
474
475         inode = __bch2_create(dir, dentry, S_IFLNK|S_IRWXUGO, 0, true);
476         if (unlikely(IS_ERR(inode)))
477                 return PTR_ERR(inode);
478
479         inode_lock(&inode->v);
480         ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
481         inode_unlock(&inode->v);
482
483         if (unlikely(ret))
484                 goto err;
485
486         ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
487         if (unlikely(ret))
488                 goto err;
489
490         journal_seq_copy(dir, inode->ei_journal_seq);
491
492         ret = __bch2_link(c, inode, dir, dentry);
493         if (unlikely(ret))
494                 goto err;
495
496         d_instantiate(dentry, &inode->v);
497         return 0;
498 err:
499         iput(&inode->v);
500         return ret;
501 }
502
503 static int bch2_mkdir(struct inode *vdir, struct dentry *dentry, umode_t mode)
504 {
505         return bch2_mknod(vdir, dentry, mode|S_IFDIR, 0);
506 }
507
508 static int bch2_rename2(struct inode *src_vdir, struct dentry *src_dentry,
509                         struct inode *dst_vdir, struct dentry *dst_dentry,
510                         unsigned flags)
511 {
512         struct bch_fs *c = src_vdir->i_sb->s_fs_info;
513         struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
514         struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
515         struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
516         struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
517         struct bch_inode_unpacked dst_dir_u, src_dir_u;
518         struct bch_inode_unpacked src_inode_u, dst_inode_u;
519         struct btree_trans trans;
520         enum bch_rename_mode mode = flags & RENAME_EXCHANGE
521                 ? BCH_RENAME_EXCHANGE
522                 : dst_dentry->d_inode
523                 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
524         u64 journal_seq = 0;
525         int ret;
526
527         if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
528                 return -EINVAL;
529
530         if (mode == BCH_RENAME_OVERWRITE) {
531                 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
532                                                    0, LLONG_MAX);
533                 if (ret)
534                         return ret;
535         }
536
537         bch2_trans_init(&trans, c, 8, 2048);
538
539         bch2_lock_inodes(INODE_UPDATE_LOCK,
540                          src_dir,
541                          dst_dir,
542                          src_inode,
543                          dst_inode);
544
545         if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
546                 ret = bch2_fs_quota_transfer(c, src_inode,
547                                              dst_dir->ei_qid,
548                                              1 << QTYP_PRJ,
549                                              KEY_TYPE_QUOTA_PREALLOC);
550                 if (ret)
551                         goto err;
552         }
553
554         if (mode == BCH_RENAME_EXCHANGE &&
555             inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
556                 ret = bch2_fs_quota_transfer(c, dst_inode,
557                                              src_dir->ei_qid,
558                                              1 << QTYP_PRJ,
559                                              KEY_TYPE_QUOTA_PREALLOC);
560                 if (ret)
561                         goto err;
562         }
563
564 retry:
565         bch2_trans_begin(&trans);
566         ret   = bch2_rename_trans(&trans,
567                                   src_dir->v.i_ino, &src_dir_u,
568                                   dst_dir->v.i_ino, &dst_dir_u,
569                                   &src_inode_u,
570                                   &dst_inode_u,
571                                   &src_dentry->d_name,
572                                   &dst_dentry->d_name,
573                                   mode) ?:
574                 bch2_trans_commit(&trans, NULL,
575                                   &journal_seq,
576                                   BTREE_INSERT_ATOMIC|
577                                   BTREE_INSERT_NOUNLOCK);
578         if (ret == -EINTR)
579                 goto retry;
580         if (unlikely(ret))
581                 goto err;
582
583         BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
584         BUG_ON(dst_inode &&
585                dst_inode->v.i_ino != dst_inode_u.bi_inum);
586
587         bch2_inode_update_after_write(c, src_dir, &src_dir_u,
588                                       ATTR_MTIME|ATTR_CTIME);
589         journal_seq_copy(src_dir, journal_seq);
590
591         if (src_dir != dst_dir) {
592                 bch2_inode_update_after_write(c, dst_dir, &dst_dir_u,
593                                               ATTR_MTIME|ATTR_CTIME);
594                 journal_seq_copy(dst_dir, journal_seq);
595         }
596
597         bch2_inode_update_after_write(c, src_inode, &src_inode_u,
598                                       ATTR_CTIME);
599         journal_seq_copy(src_inode, journal_seq);
600
601         if (dst_inode) {
602                 bch2_inode_update_after_write(c, dst_inode, &dst_inode_u,
603                                               ATTR_CTIME);
604                 journal_seq_copy(dst_inode, journal_seq);
605         }
606 err:
607         bch2_trans_exit(&trans);
608
609         bch2_fs_quota_transfer(c, src_inode,
610                                bch_qid(&src_inode->ei_inode),
611                                1 << QTYP_PRJ,
612                                KEY_TYPE_QUOTA_NOCHECK);
613         if (dst_inode)
614                 bch2_fs_quota_transfer(c, dst_inode,
615                                        bch_qid(&dst_inode->ei_inode),
616                                        1 << QTYP_PRJ,
617                                        KEY_TYPE_QUOTA_NOCHECK);
618
619         bch2_unlock_inodes(INODE_UPDATE_LOCK,
620                            src_dir,
621                            dst_dir,
622                            src_inode,
623                            dst_inode);
624
625         return ret;
626 }
627
628 void bch2_setattr_copy(struct bch_inode_info *inode,
629                        struct bch_inode_unpacked *bi,
630                        struct iattr *attr)
631 {
632         struct bch_fs *c = inode->v.i_sb->s_fs_info;
633         unsigned int ia_valid = attr->ia_valid;
634
635         if (ia_valid & ATTR_UID)
636                 bi->bi_uid = from_kuid(c->vfs_sb->s_user_ns, attr->ia_uid);
637         if (ia_valid & ATTR_GID)
638                 bi->bi_gid = from_kgid(c->vfs_sb->s_user_ns, attr->ia_gid);
639
640         if (ia_valid & ATTR_ATIME)
641                 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
642         if (ia_valid & ATTR_MTIME)
643                 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
644         if (ia_valid & ATTR_CTIME)
645                 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
646
647         if (ia_valid & ATTR_MODE) {
648                 umode_t mode = attr->ia_mode;
649                 kgid_t gid = ia_valid & ATTR_GID
650                         ? attr->ia_gid
651                         : inode->v.i_gid;
652
653                 if (!in_group_p(gid) &&
654                     !capable_wrt_inode_uidgid(&inode->v, CAP_FSETID))
655                         mode &= ~S_ISGID;
656                 bi->bi_mode = mode;
657         }
658 }
659
660 static int bch2_setattr_nonsize(struct bch_inode_info *inode,
661                                 struct iattr *attr)
662 {
663         struct bch_fs *c = inode->v.i_sb->s_fs_info;
664         struct bch_qid qid;
665         struct btree_trans trans;
666         struct btree_iter *inode_iter;
667         struct bch_inode_unpacked inode_u;
668         struct posix_acl *acl = NULL;
669         int ret;
670
671         mutex_lock(&inode->ei_update_lock);
672
673         qid = inode->ei_qid;
674
675         if (attr->ia_valid & ATTR_UID)
676                 qid.q[QTYP_USR] = from_kuid(&init_user_ns, attr->ia_uid);
677
678         if (attr->ia_valid & ATTR_GID)
679                 qid.q[QTYP_GRP] = from_kgid(&init_user_ns, attr->ia_gid);
680
681         ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
682                                      KEY_TYPE_QUOTA_PREALLOC);
683         if (ret)
684                 goto err;
685
686         bch2_trans_init(&trans, c, 0, 0);
687 retry:
688         bch2_trans_begin(&trans);
689         kfree(acl);
690         acl = NULL;
691
692         inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
693                                      BTREE_ITER_INTENT);
694         ret = PTR_ERR_OR_ZERO(inode_iter);
695         if (ret)
696                 goto btree_err;
697
698         bch2_setattr_copy(inode, &inode_u, attr);
699
700         if (attr->ia_valid & ATTR_MODE) {
701                 ret = bch2_acl_chmod(&trans, inode, inode_u.bi_mode, &acl);
702                 if (ret)
703                         goto btree_err;
704         }
705
706         ret =   bch2_inode_write(&trans, inode_iter, &inode_u) ?:
707                 bch2_trans_commit(&trans, NULL,
708                                   &inode->ei_journal_seq,
709                                   BTREE_INSERT_ATOMIC|
710                                   BTREE_INSERT_NOUNLOCK|
711                                   BTREE_INSERT_NOFAIL);
712 btree_err:
713         if (ret == -EINTR)
714                 goto retry;
715         if (unlikely(ret))
716                 goto err_trans;
717
718         bch2_inode_update_after_write(c, inode, &inode_u, attr->ia_valid);
719
720         if (acl)
721                 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
722 err_trans:
723         bch2_trans_exit(&trans);
724 err:
725         mutex_unlock(&inode->ei_update_lock);
726
727         return ret;
728 }
729
730 static int bch2_getattr(const struct path *path, struct kstat *stat,
731                         u32 request_mask, unsigned query_flags)
732 {
733         struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
734         struct bch_fs *c = inode->v.i_sb->s_fs_info;
735
736         stat->dev       = inode->v.i_sb->s_dev;
737         stat->ino       = inode->v.i_ino;
738         stat->mode      = inode->v.i_mode;
739         stat->nlink     = inode->v.i_nlink;
740         stat->uid       = inode->v.i_uid;
741         stat->gid       = inode->v.i_gid;
742         stat->rdev      = inode->v.i_rdev;
743         stat->size      = i_size_read(&inode->v);
744         stat->atime     = inode->v.i_atime;
745         stat->mtime     = inode->v.i_mtime;
746         stat->ctime     = inode->v.i_ctime;
747         stat->blksize   = block_bytes(c);
748         stat->blocks    = inode->v.i_blocks;
749
750         if (request_mask & STATX_BTIME) {
751                 stat->result_mask |= STATX_BTIME;
752                 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
753         }
754
755         if (inode->ei_inode.bi_flags & BCH_INODE_IMMUTABLE)
756                 stat->attributes |= STATX_ATTR_IMMUTABLE;
757         stat->attributes_mask    |= STATX_ATTR_IMMUTABLE;
758
759         if (inode->ei_inode.bi_flags & BCH_INODE_APPEND)
760                 stat->attributes |= STATX_ATTR_APPEND;
761         stat->attributes_mask    |= STATX_ATTR_APPEND;
762
763         if (inode->ei_inode.bi_flags & BCH_INODE_NODUMP)
764                 stat->attributes |= STATX_ATTR_NODUMP;
765         stat->attributes_mask    |= STATX_ATTR_NODUMP;
766
767         return 0;
768 }
769
770 static int bch2_setattr(struct dentry *dentry, struct iattr *iattr)
771 {
772         struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
773         int ret;
774
775         lockdep_assert_held(&inode->v.i_rwsem);
776
777         ret = setattr_prepare(dentry, iattr);
778         if (ret)
779                 return ret;
780
781         return iattr->ia_valid & ATTR_SIZE
782                 ? bch2_truncate(inode, iattr)
783                 : bch2_setattr_nonsize(inode, iattr);
784 }
785
786 static int bch2_tmpfile(struct inode *vdir, struct dentry *dentry, umode_t mode)
787 {
788         struct bch_inode_info *inode =
789                 __bch2_create(to_bch_ei(vdir), dentry, mode, 0, true);
790
791         if (IS_ERR(inode))
792                 return PTR_ERR(inode);
793
794         d_mark_tmpfile(dentry, &inode->v);
795         d_instantiate(dentry, &inode->v);
796         return 0;
797 }
798
799 static int bch2_fill_extent(struct bch_fs *c,
800                             struct fiemap_extent_info *info,
801                             struct bkey_s_c k, unsigned flags)
802 {
803         if (bkey_extent_is_data(k.k)) {
804                 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
805                 const union bch_extent_entry *entry;
806                 struct extent_ptr_decoded p;
807                 int ret;
808
809                 if (k.k->type == KEY_TYPE_reflink_v)
810                         flags |= FIEMAP_EXTENT_SHARED;
811
812                 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
813                         int flags2 = 0;
814                         u64 offset = p.ptr.offset;
815
816                         if (p.crc.compression_type)
817                                 flags2 |= FIEMAP_EXTENT_ENCODED;
818                         else
819                                 offset += p.crc.offset;
820
821                         if ((offset & (c->opts.block_size - 1)) ||
822                             (k.k->size & (c->opts.block_size - 1)))
823                                 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
824
825                         ret = fiemap_fill_next_extent(info,
826                                                 bkey_start_offset(k.k) << 9,
827                                                 offset << 9,
828                                                 k.k->size << 9, flags|flags2);
829                         if (ret)
830                                 return ret;
831                 }
832
833                 return 0;
834         } else if (k.k->type == KEY_TYPE_reservation) {
835                 return fiemap_fill_next_extent(info,
836                                                bkey_start_offset(k.k) << 9,
837                                                0, k.k->size << 9,
838                                                flags|
839                                                FIEMAP_EXTENT_DELALLOC|
840                                                FIEMAP_EXTENT_UNWRITTEN);
841         } else {
842                 BUG();
843         }
844 }
845
846 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
847                        u64 start, u64 len)
848 {
849         struct bch_fs *c = vinode->i_sb->s_fs_info;
850         struct bch_inode_info *ei = to_bch_ei(vinode);
851         struct btree_trans trans;
852         struct btree_iter *iter;
853         struct bkey_s_c k;
854         struct bkey_on_stack cur, prev;
855         struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
856         unsigned offset_into_extent, sectors;
857         bool have_extent = false;
858         int ret = 0;
859
860         if (start + len < start)
861                 return -EINVAL;
862
863         bkey_on_stack_init(&cur);
864         bkey_on_stack_init(&prev);
865         bch2_trans_init(&trans, c, 0, 0);
866
867         iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
868                                    POS(ei->v.i_ino, start >> 9), 0);
869 retry:
870         while ((k = bch2_btree_iter_peek(iter)).k &&
871                !(ret = bkey_err(k)) &&
872                bkey_cmp(iter->pos, end) < 0) {
873                 if (!bkey_extent_is_data(k.k) &&
874                     k.k->type != KEY_TYPE_reservation) {
875                         bch2_btree_iter_next(iter);
876                         continue;
877                 }
878
879                 bkey_on_stack_realloc(&cur, c, k.k->u64s);
880                 bkey_on_stack_realloc(&prev, c, k.k->u64s);
881                 bkey_reassemble(cur.k, k);
882                 k = bkey_i_to_s_c(cur.k);
883
884                 offset_into_extent      = iter->pos.offset -
885                         bkey_start_offset(k.k);
886                 sectors                 = k.k->size - offset_into_extent;
887
888                 ret = bch2_read_indirect_extent(&trans,
889                                         &offset_into_extent, cur.k);
890                 if (ret)
891                         break;
892
893                 sectors = min(sectors, k.k->size - offset_into_extent);
894
895                 if (offset_into_extent)
896                         bch2_cut_front(POS(k.k->p.inode,
897                                            bkey_start_offset(k.k) +
898                                            offset_into_extent),
899                                        cur.k);
900                 bch2_key_resize(&cur.k->k, sectors);
901                 cur.k->k.p = iter->pos;
902                 cur.k->k.p.offset += cur.k->k.size;
903
904                 if (have_extent) {
905                         ret = bch2_fill_extent(c, info,
906                                         bkey_i_to_s_c(prev.k), 0);
907                         if (ret)
908                                 break;
909                 }
910
911                 bkey_copy(prev.k, cur.k);
912                 have_extent = true;
913
914                 if (k.k->type == KEY_TYPE_reflink_v)
915                         bch2_btree_iter_set_pos(iter, k.k->p);
916                 else
917                         bch2_btree_iter_next(iter);
918         }
919
920         if (ret == -EINTR)
921                 goto retry;
922
923         if (!ret && have_extent)
924                 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
925                                        FIEMAP_EXTENT_LAST);
926
927         ret = bch2_trans_exit(&trans) ?: ret;
928         bkey_on_stack_exit(&cur, c);
929         bkey_on_stack_exit(&prev, c);
930         return ret < 0 ? ret : 0;
931 }
932
933 static const struct vm_operations_struct bch_vm_ops = {
934         .fault          = bch2_page_fault,
935         .map_pages      = filemap_map_pages,
936         .page_mkwrite   = bch2_page_mkwrite,
937 };
938
939 static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
940 {
941         file_accessed(file);
942
943         vma->vm_ops = &bch_vm_ops;
944         return 0;
945 }
946
947 /* Directories: */
948
949 static loff_t bch2_dir_llseek(struct file *file, loff_t offset, int whence)
950 {
951         return generic_file_llseek_size(file, offset, whence,
952                                         S64_MAX, S64_MAX);
953 }
954
955 static int bch2_vfs_readdir(struct file *file, struct dir_context *ctx)
956 {
957         struct bch_inode_info *inode = file_bch_inode(file);
958         struct bch_fs *c = inode->v.i_sb->s_fs_info;
959
960         if (!dir_emit_dots(file, ctx))
961                 return 0;
962
963         return bch2_readdir(c, inode->v.i_ino, ctx);
964 }
965
966 static const struct file_operations bch_file_operations = {
967         .llseek         = bch2_llseek,
968         .read_iter      = bch2_read_iter,
969         .write_iter     = bch2_write_iter,
970         .mmap           = bch2_mmap,
971         .open           = generic_file_open,
972         .fsync          = bch2_fsync,
973         .splice_read    = generic_file_splice_read,
974         /*
975          * Broken, on v5.3:
976         .splice_write   = iter_file_splice_write,
977         */
978         .fallocate      = bch2_fallocate_dispatch,
979         .unlocked_ioctl = bch2_fs_file_ioctl,
980 #ifdef CONFIG_COMPAT
981         .compat_ioctl   = bch2_compat_fs_ioctl,
982 #endif
983         .remap_file_range = bch2_remap_file_range,
984 };
985
986 static const struct inode_operations bch_file_inode_operations = {
987         .getattr        = bch2_getattr,
988         .setattr        = bch2_setattr,
989         .fiemap         = bch2_fiemap,
990         .listxattr      = bch2_xattr_list,
991 #ifdef CONFIG_BCACHEFS_POSIX_ACL
992         .get_acl        = bch2_get_acl,
993         .set_acl        = bch2_set_acl,
994 #endif
995 };
996
997 static const struct inode_operations bch_dir_inode_operations = {
998         .lookup         = bch2_lookup,
999         .create         = bch2_create,
1000         .link           = bch2_link,
1001         .unlink         = bch2_unlink,
1002         .symlink        = bch2_symlink,
1003         .mkdir          = bch2_mkdir,
1004         .rmdir          = bch2_unlink,
1005         .mknod          = bch2_mknod,
1006         .rename         = bch2_rename2,
1007         .getattr        = bch2_getattr,
1008         .setattr        = bch2_setattr,
1009         .tmpfile        = bch2_tmpfile,
1010         .listxattr      = bch2_xattr_list,
1011 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1012         .get_acl        = bch2_get_acl,
1013         .set_acl        = bch2_set_acl,
1014 #endif
1015 };
1016
1017 static const struct file_operations bch_dir_file_operations = {
1018         .llseek         = bch2_dir_llseek,
1019         .read           = generic_read_dir,
1020         .iterate_shared = bch2_vfs_readdir,
1021         .fsync          = bch2_fsync,
1022         .unlocked_ioctl = bch2_fs_file_ioctl,
1023 #ifdef CONFIG_COMPAT
1024         .compat_ioctl   = bch2_compat_fs_ioctl,
1025 #endif
1026 };
1027
1028 static const struct inode_operations bch_symlink_inode_operations = {
1029         .get_link       = page_get_link,
1030         .getattr        = bch2_getattr,
1031         .setattr        = bch2_setattr,
1032         .listxattr      = bch2_xattr_list,
1033 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1034         .get_acl        = bch2_get_acl,
1035         .set_acl        = bch2_set_acl,
1036 #endif
1037 };
1038
1039 static const struct inode_operations bch_special_inode_operations = {
1040         .getattr        = bch2_getattr,
1041         .setattr        = bch2_setattr,
1042         .listxattr      = bch2_xattr_list,
1043 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1044         .get_acl        = bch2_get_acl,
1045         .set_acl        = bch2_set_acl,
1046 #endif
1047 };
1048
1049 static const struct address_space_operations bch_address_space_operations = {
1050         .writepage      = bch2_writepage,
1051         .readpage       = bch2_readpage,
1052         .writepages     = bch2_writepages,
1053         .readpages      = bch2_readpages,
1054         .set_page_dirty = __set_page_dirty_nobuffers,
1055         .write_begin    = bch2_write_begin,
1056         .write_end      = bch2_write_end,
1057         .invalidatepage = bch2_invalidatepage,
1058         .releasepage    = bch2_releasepage,
1059         .direct_IO      = noop_direct_IO,
1060 #ifdef CONFIG_MIGRATION
1061         .migratepage    = bch2_migrate_page,
1062 #endif
1063         .error_remove_page = generic_error_remove_page,
1064 };
1065
1066 static struct inode *bch2_nfs_get_inode(struct super_block *sb,
1067                 u64 ino, u32 generation)
1068 {
1069         struct bch_fs *c = sb->s_fs_info;
1070         struct inode *vinode;
1071
1072         if (ino < BCACHEFS_ROOT_INO)
1073                 return ERR_PTR(-ESTALE);
1074
1075         vinode = bch2_vfs_inode_get(c, ino);
1076         if (IS_ERR(vinode))
1077                 return ERR_CAST(vinode);
1078         if (generation && vinode->i_generation != generation) {
1079                 /* we didn't find the right inode.. */
1080                 iput(vinode);
1081                 return ERR_PTR(-ESTALE);
1082         }
1083         return vinode;
1084 }
1085
1086 static struct dentry *bch2_fh_to_dentry(struct super_block *sb, struct fid *fid,
1087                 int fh_len, int fh_type)
1088 {
1089         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1090                                     bch2_nfs_get_inode);
1091 }
1092
1093 static struct dentry *bch2_fh_to_parent(struct super_block *sb, struct fid *fid,
1094                 int fh_len, int fh_type)
1095 {
1096         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1097                                     bch2_nfs_get_inode);
1098 }
1099
1100 static const struct export_operations bch_export_ops = {
1101         .fh_to_dentry   = bch2_fh_to_dentry,
1102         .fh_to_parent   = bch2_fh_to_parent,
1103         //.get_parent   = bch2_get_parent,
1104 };
1105
1106 static void bch2_vfs_inode_init(struct bch_fs *c,
1107                                 struct bch_inode_info *inode,
1108                                 struct bch_inode_unpacked *bi)
1109 {
1110         bch2_inode_update_after_write(c, inode, bi, ~0);
1111
1112         inode->v.i_blocks       = bi->bi_sectors;
1113         inode->v.i_ino          = bi->bi_inum;
1114         inode->v.i_rdev         = bi->bi_dev;
1115         inode->v.i_generation   = bi->bi_generation;
1116         inode->v.i_size         = bi->bi_size;
1117
1118         inode->ei_journal_seq   = 0;
1119         inode->ei_quota_reserved = 0;
1120         inode->ei_str_hash      = bch2_hash_info_init(c, bi);
1121         inode->ei_qid           = bch_qid(bi);
1122
1123         inode->v.i_mapping->a_ops = &bch_address_space_operations;
1124
1125         switch (inode->v.i_mode & S_IFMT) {
1126         case S_IFREG:
1127                 inode->v.i_op   = &bch_file_inode_operations;
1128                 inode->v.i_fop  = &bch_file_operations;
1129                 break;
1130         case S_IFDIR:
1131                 inode->v.i_op   = &bch_dir_inode_operations;
1132                 inode->v.i_fop  = &bch_dir_file_operations;
1133                 break;
1134         case S_IFLNK:
1135                 inode_nohighmem(&inode->v);
1136                 inode->v.i_op   = &bch_symlink_inode_operations;
1137                 break;
1138         default:
1139                 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1140                 inode->v.i_op   = &bch_special_inode_operations;
1141                 break;
1142         }
1143 }
1144
1145 static struct inode *bch2_alloc_inode(struct super_block *sb)
1146 {
1147         struct bch_inode_info *inode;
1148
1149         inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1150         if (!inode)
1151                 return NULL;
1152
1153         inode_init_once(&inode->v);
1154         mutex_init(&inode->ei_update_lock);
1155         pagecache_lock_init(&inode->ei_pagecache_lock);
1156         mutex_init(&inode->ei_quota_lock);
1157         inode->ei_journal_seq = 0;
1158
1159         return &inode->v;
1160 }
1161
1162 static void bch2_i_callback(struct rcu_head *head)
1163 {
1164         struct inode *vinode = container_of(head, struct inode, i_rcu);
1165         struct bch_inode_info *inode = to_bch_ei(vinode);
1166
1167         kmem_cache_free(bch2_inode_cache, inode);
1168 }
1169
1170 static void bch2_destroy_inode(struct inode *vinode)
1171 {
1172         call_rcu(&vinode->i_rcu, bch2_i_callback);
1173 }
1174
1175 static int inode_update_times_fn(struct bch_inode_info *inode,
1176                                  struct bch_inode_unpacked *bi,
1177                                  void *p)
1178 {
1179         struct bch_fs *c = inode->v.i_sb->s_fs_info;
1180
1181         bi->bi_atime    = timespec_to_bch2_time(c, inode->v.i_atime);
1182         bi->bi_mtime    = timespec_to_bch2_time(c, inode->v.i_mtime);
1183         bi->bi_ctime    = timespec_to_bch2_time(c, inode->v.i_ctime);
1184
1185         return 0;
1186 }
1187
1188 static int bch2_vfs_write_inode(struct inode *vinode,
1189                                 struct writeback_control *wbc)
1190 {
1191         struct bch_fs *c = vinode->i_sb->s_fs_info;
1192         struct bch_inode_info *inode = to_bch_ei(vinode);
1193         int ret;
1194
1195         mutex_lock(&inode->ei_update_lock);
1196         ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1197                                ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1198         mutex_unlock(&inode->ei_update_lock);
1199
1200         return ret;
1201 }
1202
1203 static void bch2_evict_inode(struct inode *vinode)
1204 {
1205         struct bch_fs *c = vinode->i_sb->s_fs_info;
1206         struct bch_inode_info *inode = to_bch_ei(vinode);
1207
1208         truncate_inode_pages_final(&inode->v.i_data);
1209
1210         clear_inode(&inode->v);
1211
1212         BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1213
1214         if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1215                 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1216                                 KEY_TYPE_QUOTA_WARN);
1217                 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1218                                 KEY_TYPE_QUOTA_WARN);
1219                 bch2_inode_rm(c, inode->v.i_ino);
1220         }
1221 }
1222
1223 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1224 {
1225         struct super_block *sb = dentry->d_sb;
1226         struct bch_fs *c = sb->s_fs_info;
1227         struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1228         unsigned shift = sb->s_blocksize_bits - 9;
1229         u64 fsid;
1230
1231         buf->f_type     = BCACHEFS_STATFS_MAGIC;
1232         buf->f_bsize    = sb->s_blocksize;
1233         buf->f_blocks   = usage.capacity >> shift;
1234         buf->f_bfree    = (usage.capacity - usage.used) >> shift;
1235         buf->f_bavail   = buf->f_bfree;
1236         buf->f_files    = usage.nr_inodes;
1237         buf->f_ffree    = U64_MAX;
1238
1239         fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
1240                le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
1241         buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1242         buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1243         buf->f_namelen  = BCH_NAME_MAX;
1244
1245         return 0;
1246 }
1247
1248 static int bch2_sync_fs(struct super_block *sb, int wait)
1249 {
1250         struct bch_fs *c = sb->s_fs_info;
1251
1252         if (c->opts.journal_flush_disabled)
1253                 return 0;
1254
1255         if (!wait) {
1256                 bch2_journal_flush_async(&c->journal, NULL);
1257                 return 0;
1258         }
1259
1260         return bch2_journal_flush(&c->journal);
1261 }
1262
1263 static struct bch_fs *bch2_path_to_fs(const char *dev)
1264 {
1265         struct bch_fs *c;
1266         struct block_device *bdev = lookup_bdev(dev);
1267
1268         if (IS_ERR(bdev))
1269                 return ERR_CAST(bdev);
1270
1271         c = bch2_bdev_to_fs(bdev);
1272         bdput(bdev);
1273         return c ?: ERR_PTR(-ENOENT);
1274 }
1275
1276 static struct bch_fs *__bch2_open_as_blockdevs(const char *dev_name, char * const *devs,
1277                                                unsigned nr_devs, struct bch_opts opts)
1278 {
1279         struct bch_fs *c, *c1, *c2;
1280         size_t i;
1281
1282         if (!nr_devs)
1283                 return ERR_PTR(-EINVAL);
1284
1285         c = bch2_fs_open(devs, nr_devs, opts);
1286
1287         if (IS_ERR(c) && PTR_ERR(c) == -EBUSY) {
1288                 /*
1289                  * Already open?
1290                  * Look up each block device, make sure they all belong to a
1291                  * filesystem and they all belong to the _same_ filesystem
1292                  */
1293
1294                 c1 = bch2_path_to_fs(devs[0]);
1295                 if (IS_ERR(c1))
1296                         return c;
1297
1298                 for (i = 1; i < nr_devs; i++) {
1299                         c2 = bch2_path_to_fs(devs[i]);
1300                         if (!IS_ERR(c2))
1301                                 closure_put(&c2->cl);
1302
1303                         if (c1 != c2) {
1304                                 closure_put(&c1->cl);
1305                                 return c;
1306                         }
1307                 }
1308
1309                 c = c1;
1310         }
1311
1312         if (IS_ERR(c))
1313                 return c;
1314
1315         mutex_lock(&c->state_lock);
1316
1317         if (!test_bit(BCH_FS_STARTED, &c->flags)) {
1318                 mutex_unlock(&c->state_lock);
1319                 closure_put(&c->cl);
1320                 pr_err("err mounting %s: incomplete filesystem", dev_name);
1321                 return ERR_PTR(-EINVAL);
1322         }
1323
1324         mutex_unlock(&c->state_lock);
1325
1326         set_bit(BCH_FS_BDEV_MOUNTED, &c->flags);
1327         return c;
1328 }
1329
1330 static struct bch_fs *bch2_open_as_blockdevs(const char *_dev_name,
1331                                              struct bch_opts opts)
1332 {
1333         char *dev_name = NULL, **devs = NULL, *s;
1334         struct bch_fs *c = ERR_PTR(-ENOMEM);
1335         size_t i, nr_devs = 0;
1336
1337         dev_name = kstrdup(_dev_name, GFP_KERNEL);
1338         if (!dev_name)
1339                 goto err;
1340
1341         for (s = dev_name; s; s = strchr(s + 1, ':'))
1342                 nr_devs++;
1343
1344         devs = kcalloc(nr_devs, sizeof(const char *), GFP_KERNEL);
1345         if (!devs)
1346                 goto err;
1347
1348         for (i = 0, s = dev_name;
1349              s;
1350              (s = strchr(s, ':')) && (*s++ = '\0'))
1351                 devs[i++] = s;
1352
1353         c = __bch2_open_as_blockdevs(_dev_name, devs, nr_devs, opts);
1354 err:
1355         kfree(devs);
1356         kfree(dev_name);
1357         return c;
1358 }
1359
1360 static int bch2_remount(struct super_block *sb, int *flags, char *data)
1361 {
1362         struct bch_fs *c = sb->s_fs_info;
1363         struct bch_opts opts = bch2_opts_empty();
1364         int ret;
1365
1366         opt_set(opts, read_only, (*flags & SB_RDONLY) != 0);
1367
1368         ret = bch2_parse_mount_opts(&opts, data);
1369         if (ret)
1370                 return ret;
1371
1372         if (opts.read_only != c->opts.read_only) {
1373                 mutex_lock(&c->state_lock);
1374
1375                 if (opts.read_only) {
1376                         bch2_fs_read_only(c);
1377
1378                         sb->s_flags |= SB_RDONLY;
1379                 } else {
1380                         ret = bch2_fs_read_write(c);
1381                         if (ret) {
1382                                 bch_err(c, "error going rw: %i", ret);
1383                                 mutex_unlock(&c->state_lock);
1384                                 return -EINVAL;
1385                         }
1386
1387                         sb->s_flags &= ~SB_RDONLY;
1388                 }
1389
1390                 c->opts.read_only = opts.read_only;
1391
1392                 mutex_unlock(&c->state_lock);
1393         }
1394
1395         if (opts.errors >= 0)
1396                 c->opts.errors = opts.errors;
1397
1398         return ret;
1399 }
1400
1401 static int bch2_show_options(struct seq_file *seq, struct dentry *root)
1402 {
1403         struct bch_fs *c = root->d_sb->s_fs_info;
1404         enum bch_opt_id i;
1405         char buf[512];
1406
1407         for (i = 0; i < bch2_opts_nr; i++) {
1408                 const struct bch_option *opt = &bch2_opt_table[i];
1409                 u64 v = bch2_opt_get_by_id(&c->opts, i);
1410
1411                 if (!(opt->mode & OPT_MOUNT))
1412                         continue;
1413
1414                 if (v == bch2_opt_get_by_id(&bch2_opts_default, i))
1415                         continue;
1416
1417                 bch2_opt_to_text(&PBUF(buf), c, opt, v,
1418                                  OPT_SHOW_MOUNT_STYLE);
1419                 seq_putc(seq, ',');
1420                 seq_puts(seq, buf);
1421         }
1422
1423         return 0;
1424
1425 }
1426
1427 static const struct super_operations bch_super_operations = {
1428         .alloc_inode    = bch2_alloc_inode,
1429         .destroy_inode  = bch2_destroy_inode,
1430         .write_inode    = bch2_vfs_write_inode,
1431         .evict_inode    = bch2_evict_inode,
1432         .sync_fs        = bch2_sync_fs,
1433         .statfs         = bch2_statfs,
1434         .show_options   = bch2_show_options,
1435         .remount_fs     = bch2_remount,
1436 #if 0
1437         .put_super      = bch2_put_super,
1438         .freeze_fs      = bch2_freeze,
1439         .unfreeze_fs    = bch2_unfreeze,
1440 #endif
1441 };
1442
1443 static int bch2_test_super(struct super_block *s, void *data)
1444 {
1445         return s->s_fs_info == data;
1446 }
1447
1448 static int bch2_set_super(struct super_block *s, void *data)
1449 {
1450         s->s_fs_info = data;
1451         return 0;
1452 }
1453
1454 static struct dentry *bch2_mount(struct file_system_type *fs_type,
1455                                  int flags, const char *dev_name, void *data)
1456 {
1457         struct bch_fs *c;
1458         struct bch_dev *ca;
1459         struct super_block *sb;
1460         struct inode *vinode;
1461         struct bch_opts opts = bch2_opts_empty();
1462         unsigned i;
1463         int ret;
1464
1465         opt_set(opts, read_only, (flags & SB_RDONLY) != 0);
1466
1467         ret = bch2_parse_mount_opts(&opts, data);
1468         if (ret)
1469                 return ERR_PTR(ret);
1470
1471         c = bch2_open_as_blockdevs(dev_name, opts);
1472         if (IS_ERR(c))
1473                 return ERR_CAST(c);
1474
1475         sb = sget(fs_type, bch2_test_super, bch2_set_super, flags|SB_NOSEC, c);
1476         if (IS_ERR(sb)) {
1477                 closure_put(&c->cl);
1478                 return ERR_CAST(sb);
1479         }
1480
1481         BUG_ON(sb->s_fs_info != c);
1482
1483         if (sb->s_root) {
1484                 closure_put(&c->cl);
1485
1486                 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1487                         ret = -EBUSY;
1488                         goto err_put_super;
1489                 }
1490                 goto out;
1491         }
1492
1493         sb->s_blocksize         = block_bytes(c);
1494         sb->s_blocksize_bits    = ilog2(block_bytes(c));
1495         sb->s_maxbytes          = MAX_LFS_FILESIZE;
1496         sb->s_op                = &bch_super_operations;
1497         sb->s_export_op         = &bch_export_ops;
1498 #ifdef CONFIG_BCACHEFS_QUOTA
1499         sb->s_qcop              = &bch2_quotactl_operations;
1500         sb->s_quota_types       = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1501 #endif
1502         sb->s_xattr             = bch2_xattr_handlers;
1503         sb->s_magic             = BCACHEFS_STATFS_MAGIC;
1504         sb->s_time_gran         = c->sb.time_precision;
1505         c->vfs_sb               = sb;
1506         strlcpy(sb->s_id, c->name, sizeof(sb->s_id));
1507
1508         ret = super_setup_bdi(sb);
1509         if (ret)
1510                 goto err_put_super;
1511
1512         sb->s_bdi->congested_fn         = bch2_congested;
1513         sb->s_bdi->congested_data       = c;
1514         sb->s_bdi->ra_pages             = VM_READAHEAD_PAGES;
1515
1516         for_each_online_member(ca, c, i) {
1517                 struct block_device *bdev = ca->disk_sb.bdev;
1518
1519                 /* XXX: create an anonymous device for multi device filesystems */
1520                 sb->s_bdev      = bdev;
1521                 sb->s_dev       = bdev->bd_dev;
1522                 percpu_ref_put(&ca->io_ref);
1523                 break;
1524         }
1525
1526 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1527         if (c->opts.acl)
1528                 sb->s_flags     |= SB_POSIXACL;
1529 #endif
1530
1531         vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_INO);
1532         if (IS_ERR(vinode)) {
1533                 bch_err(c, "error mounting: error getting root inode %i",
1534                         (int) PTR_ERR(vinode));
1535                 ret = PTR_ERR(vinode);
1536                 goto err_put_super;
1537         }
1538
1539         sb->s_root = d_make_root(vinode);
1540         if (!sb->s_root) {
1541                 bch_err(c, "error mounting: error allocating root dentry");
1542                 ret = -ENOMEM;
1543                 goto err_put_super;
1544         }
1545
1546         sb->s_flags |= SB_ACTIVE;
1547 out:
1548         return dget(sb->s_root);
1549
1550 err_put_super:
1551         deactivate_locked_super(sb);
1552         return ERR_PTR(ret);
1553 }
1554
1555 static void bch2_kill_sb(struct super_block *sb)
1556 {
1557         struct bch_fs *c = sb->s_fs_info;
1558
1559         generic_shutdown_super(sb);
1560
1561         if (test_bit(BCH_FS_BDEV_MOUNTED, &c->flags))
1562                 bch2_fs_stop(c);
1563         else
1564                 closure_put(&c->cl);
1565 }
1566
1567 static struct file_system_type bcache_fs_type = {
1568         .owner          = THIS_MODULE,
1569         .name           = "bcachefs",
1570         .mount          = bch2_mount,
1571         .kill_sb        = bch2_kill_sb,
1572         .fs_flags       = FS_REQUIRES_DEV,
1573 };
1574
1575 MODULE_ALIAS_FS("bcachefs");
1576
1577 void bch2_vfs_exit(void)
1578 {
1579         unregister_filesystem(&bcache_fs_type);
1580         if (bch2_inode_cache)
1581                 kmem_cache_destroy(bch2_inode_cache);
1582 }
1583
1584 int __init bch2_vfs_init(void)
1585 {
1586         int ret = -ENOMEM;
1587
1588         bch2_inode_cache = KMEM_CACHE(bch_inode_info, 0);
1589         if (!bch2_inode_cache)
1590                 goto err;
1591
1592         ret = register_filesystem(&bcache_fs_type);
1593         if (ret)
1594                 goto err;
1595
1596         return 0;
1597 err:
1598         bch2_vfs_exit();
1599         return ret;
1600 }
1601
1602 #endif /* NO_BCACHEFS_FS */