]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs.c
Update bcachefs sources to cfb41d25c7 bcachefs: Add an assertion to track down a...
[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_NOUNLOCK|
144                                   BTREE_INSERT_NOFAIL);
145         if (ret == -EINTR)
146                 goto retry;
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(c, inode, &inode_u, fields);
154
155         bch2_trans_exit(&trans);
156         return ret < 0 ? ret : 0;
157 }
158
159 int bch2_fs_quota_transfer(struct bch_fs *c,
160                            struct bch_inode_info *inode,
161                            struct bch_qid new_qid,
162                            unsigned qtypes,
163                            enum quota_acct_mode mode)
164 {
165         unsigned i;
166         int ret;
167
168         qtypes &= enabled_qtypes(c);
169
170         for (i = 0; i < QTYP_NR; i++)
171                 if (new_qid.q[i] == inode->ei_qid.q[i])
172                         qtypes &= ~(1U << i);
173
174         if (!qtypes)
175                 return 0;
176
177         mutex_lock(&inode->ei_quota_lock);
178
179         ret = bch2_quota_transfer(c, qtypes, new_qid,
180                                   inode->ei_qid,
181                                   inode->v.i_blocks +
182                                   inode->ei_quota_reserved,
183                                   mode);
184         if (!ret)
185                 for (i = 0; i < QTYP_NR; i++)
186                         if (qtypes & (1 << i))
187                                 inode->ei_qid.q[i] = new_qid.q[i];
188
189         mutex_unlock(&inode->ei_quota_lock);
190
191         return ret;
192 }
193
194 struct inode *bch2_vfs_inode_get(struct bch_fs *c, u64 inum)
195 {
196         struct bch_inode_unpacked inode_u;
197         struct bch_inode_info *inode;
198         int ret;
199
200         inode = to_bch_ei(iget_locked(c->vfs_sb, inum));
201         if (unlikely(!inode))
202                 return ERR_PTR(-ENOMEM);
203         if (!(inode->v.i_state & I_NEW))
204                 return &inode->v;
205
206         ret = bch2_inode_find_by_inum(c, inum, &inode_u);
207         if (ret) {
208                 iget_failed(&inode->v);
209                 return ERR_PTR(ret);
210         }
211
212         bch2_vfs_inode_init(c, inode, &inode_u);
213
214         inode->ei_journal_seq = bch2_inode_journal_seq(&c->journal, inum);
215
216         unlock_new_inode(&inode->v);
217
218         return &inode->v;
219 }
220
221 static struct bch_inode_info *
222 __bch2_create(struct bch_inode_info *dir, struct dentry *dentry,
223               umode_t mode, dev_t rdev, bool tmpfile)
224 {
225         struct bch_fs *c = dir->v.i_sb->s_fs_info;
226         struct user_namespace *ns = dir->v.i_sb->s_user_ns;
227         struct btree_trans trans;
228         struct bch_inode_unpacked dir_u;
229         struct bch_inode_info *inode, *old;
230         struct bch_inode_unpacked inode_u;
231         struct posix_acl *default_acl = NULL, *acl = NULL;
232         u64 journal_seq = 0;
233         int ret;
234
235         /*
236          * preallocate acls + vfs inode before btree transaction, so that
237          * nothing can fail after the transaction succeeds:
238          */
239 #ifdef CONFIG_BCACHEFS_POSIX_ACL
240         ret = posix_acl_create(&dir->v, &mode, &default_acl, &acl);
241         if (ret)
242                 return ERR_PTR(ret);
243 #endif
244         inode = to_bch_ei(new_inode(c->vfs_sb));
245         if (unlikely(!inode)) {
246                 inode = ERR_PTR(-ENOMEM);
247                 goto err;
248         }
249
250         bch2_inode_init_early(c, &inode_u);
251
252         if (!tmpfile)
253                 mutex_lock(&dir->ei_update_lock);
254
255         bch2_trans_init(&trans, c, 8, 1024);
256 retry:
257         bch2_trans_begin(&trans);
258
259         ret   = bch2_create_trans(&trans, dir->v.i_ino, &dir_u, &inode_u,
260                                   !tmpfile ? &dentry->d_name : NULL,
261                                   from_kuid(ns, current_fsuid()),
262                                   from_kgid(ns, current_fsgid()),
263                                   mode, rdev,
264                                   default_acl, acl) ?:
265                 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, 1,
266                                 KEY_TYPE_QUOTA_PREALLOC);
267         if (unlikely(ret))
268                 goto err_before_quota;
269
270         ret   = bch2_trans_commit(&trans, NULL, &journal_seq,
271                                   BTREE_INSERT_NOUNLOCK);
272         if (unlikely(ret)) {
273                 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
274                                 KEY_TYPE_QUOTA_WARN);
275 err_before_quota:
276                 if (ret == -EINTR)
277                         goto retry;
278                 goto err_trans;
279         }
280
281         if (!tmpfile) {
282                 bch2_inode_update_after_write(c, dir, &dir_u,
283                                               ATTR_MTIME|ATTR_CTIME);
284                 journal_seq_copy(dir, journal_seq);
285                 mutex_unlock(&dir->ei_update_lock);
286         }
287
288         bch2_vfs_inode_init(c, inode, &inode_u);
289         journal_seq_copy(inode, journal_seq);
290
291         set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
292         set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
293
294         /*
295          * we must insert the new inode into the inode cache before calling
296          * bch2_trans_exit() and dropping locks, else we could race with another
297          * thread pulling the inode in and modifying it:
298          */
299
300         old = to_bch_ei(insert_inode_locked2(&inode->v));
301         if (unlikely(old)) {
302                 /*
303                  * We raced, another process pulled the new inode into cache
304                  * before us:
305                  */
306                 journal_seq_copy(old, journal_seq);
307                 make_bad_inode(&inode->v);
308                 iput(&inode->v);
309
310                 inode = old;
311         } else {
312                 /*
313                  * we really don't want insert_inode_locked2() to be setting
314                  * I_NEW...
315                  */
316                 unlock_new_inode(&inode->v);
317         }
318
319         bch2_trans_exit(&trans);
320 err:
321         posix_acl_release(default_acl);
322         posix_acl_release(acl);
323         return inode;
324 err_trans:
325         if (!tmpfile)
326                 mutex_unlock(&dir->ei_update_lock);
327
328         bch2_trans_exit(&trans);
329         make_bad_inode(&inode->v);
330         iput(&inode->v);
331         inode = ERR_PTR(ret);
332         goto err;
333 }
334
335 /* methods */
336
337 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
338                                   unsigned int flags)
339 {
340         struct bch_fs *c = vdir->i_sb->s_fs_info;
341         struct bch_inode_info *dir = to_bch_ei(vdir);
342         struct inode *vinode = NULL;
343         u64 inum;
344
345         inum = bch2_dirent_lookup(c, dir->v.i_ino,
346                                   &dir->ei_str_hash,
347                                   &dentry->d_name);
348
349         if (inum)
350                 vinode = bch2_vfs_inode_get(c, inum);
351
352         return d_splice_alias(vinode, dentry);
353 }
354
355 static int bch2_mknod(struct inode *vdir, struct dentry *dentry,
356                       umode_t mode, dev_t rdev)
357 {
358         struct bch_inode_info *inode =
359                 __bch2_create(to_bch_ei(vdir), dentry, mode, rdev, false);
360
361         if (IS_ERR(inode))
362                 return PTR_ERR(inode);
363
364         d_instantiate(dentry, &inode->v);
365         return 0;
366 }
367
368 static int bch2_create(struct inode *vdir, struct dentry *dentry,
369                        umode_t mode, bool excl)
370 {
371         return bch2_mknod(vdir, dentry, mode|S_IFREG, 0);
372 }
373
374 static int __bch2_link(struct bch_fs *c,
375                        struct bch_inode_info *inode,
376                        struct bch_inode_info *dir,
377                        struct dentry *dentry)
378 {
379         struct btree_trans trans;
380         struct bch_inode_unpacked dir_u, inode_u;
381         int ret;
382
383         mutex_lock(&inode->ei_update_lock);
384         bch2_trans_init(&trans, c, 4, 1024);
385
386         do {
387                 bch2_trans_begin(&trans);
388                 ret   = bch2_link_trans(&trans,
389                                         dir->v.i_ino,
390                                         inode->v.i_ino, &dir_u, &inode_u,
391                                         &dentry->d_name) ?:
392                         bch2_trans_commit(&trans, NULL,
393                                         &inode->ei_journal_seq,
394                                         BTREE_INSERT_NOUNLOCK);
395         } while (ret == -EINTR);
396
397         if (likely(!ret)) {
398                 BUG_ON(inode_u.bi_inum != inode->v.i_ino);
399
400                 journal_seq_copy(inode, dir->ei_journal_seq);
401                 bch2_inode_update_after_write(c, dir, &dir_u,
402                                               ATTR_MTIME|ATTR_CTIME);
403                 bch2_inode_update_after_write(c, inode, &inode_u, ATTR_CTIME);
404         }
405
406         bch2_trans_exit(&trans);
407         mutex_unlock(&inode->ei_update_lock);
408         return ret;
409 }
410
411 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
412                      struct dentry *dentry)
413 {
414         struct bch_fs *c = vdir->i_sb->s_fs_info;
415         struct bch_inode_info *dir = to_bch_ei(vdir);
416         struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
417         int ret;
418
419         lockdep_assert_held(&inode->v.i_rwsem);
420
421         ret = __bch2_link(c, inode, dir, dentry);
422         if (unlikely(ret))
423                 return ret;
424
425         ihold(&inode->v);
426         d_instantiate(dentry, &inode->v);
427         return 0;
428 }
429
430 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
431 {
432         struct bch_fs *c = vdir->i_sb->s_fs_info;
433         struct bch_inode_info *dir = to_bch_ei(vdir);
434         struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
435         struct bch_inode_unpacked dir_u, inode_u;
436         struct btree_trans trans;
437         int ret;
438
439         bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
440         bch2_trans_init(&trans, c, 4, 1024);
441
442         do {
443                 bch2_trans_begin(&trans);
444
445                 ret   = bch2_unlink_trans(&trans,
446                                           dir->v.i_ino, &dir_u,
447                                           &inode_u, &dentry->d_name) ?:
448                         bch2_trans_commit(&trans, NULL,
449                                           &dir->ei_journal_seq,
450                                           BTREE_INSERT_NOUNLOCK|
451                                           BTREE_INSERT_NOFAIL);
452         } while (ret == -EINTR);
453
454         if (likely(!ret)) {
455                 BUG_ON(inode_u.bi_inum != inode->v.i_ino);
456
457                 journal_seq_copy(inode, dir->ei_journal_seq);
458                 bch2_inode_update_after_write(c, dir, &dir_u,
459                                               ATTR_MTIME|ATTR_CTIME);
460                 bch2_inode_update_after_write(c, inode, &inode_u,
461                                               ATTR_MTIME);
462         }
463
464         bch2_trans_exit(&trans);
465         bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
466
467         return ret;
468 }
469
470 static int bch2_symlink(struct inode *vdir, struct dentry *dentry,
471                         const char *symname)
472 {
473         struct bch_fs *c = vdir->i_sb->s_fs_info;
474         struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
475         int ret;
476
477         inode = __bch2_create(dir, dentry, S_IFLNK|S_IRWXUGO, 0, true);
478         if (unlikely(IS_ERR(inode)))
479                 return PTR_ERR(inode);
480
481         inode_lock(&inode->v);
482         ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
483         inode_unlock(&inode->v);
484
485         if (unlikely(ret))
486                 goto err;
487
488         ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
489         if (unlikely(ret))
490                 goto err;
491
492         journal_seq_copy(dir, inode->ei_journal_seq);
493
494         ret = __bch2_link(c, inode, dir, dentry);
495         if (unlikely(ret))
496                 goto err;
497
498         d_instantiate(dentry, &inode->v);
499         return 0;
500 err:
501         iput(&inode->v);
502         return ret;
503 }
504
505 static int bch2_mkdir(struct inode *vdir, struct dentry *dentry, umode_t mode)
506 {
507         return bch2_mknod(vdir, dentry, mode|S_IFDIR, 0);
508 }
509
510 static int bch2_rename2(struct inode *src_vdir, struct dentry *src_dentry,
511                         struct inode *dst_vdir, struct dentry *dst_dentry,
512                         unsigned flags)
513 {
514         struct bch_fs *c = src_vdir->i_sb->s_fs_info;
515         struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
516         struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
517         struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
518         struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
519         struct bch_inode_unpacked dst_dir_u, src_dir_u;
520         struct bch_inode_unpacked src_inode_u, dst_inode_u;
521         struct btree_trans trans;
522         enum bch_rename_mode mode = flags & RENAME_EXCHANGE
523                 ? BCH_RENAME_EXCHANGE
524                 : dst_dentry->d_inode
525                 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
526         u64 journal_seq = 0;
527         int ret;
528
529         if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
530                 return -EINVAL;
531
532         if (mode == BCH_RENAME_OVERWRITE) {
533                 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
534                                                    0, LLONG_MAX);
535                 if (ret)
536                         return ret;
537         }
538
539         bch2_trans_init(&trans, c, 8, 2048);
540
541         bch2_lock_inodes(INODE_UPDATE_LOCK,
542                          src_dir,
543                          dst_dir,
544                          src_inode,
545                          dst_inode);
546
547         if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
548                 ret = bch2_fs_quota_transfer(c, src_inode,
549                                              dst_dir->ei_qid,
550                                              1 << QTYP_PRJ,
551                                              KEY_TYPE_QUOTA_PREALLOC);
552                 if (ret)
553                         goto err;
554         }
555
556         if (mode == BCH_RENAME_EXCHANGE &&
557             inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
558                 ret = bch2_fs_quota_transfer(c, dst_inode,
559                                              src_dir->ei_qid,
560                                              1 << QTYP_PRJ,
561                                              KEY_TYPE_QUOTA_PREALLOC);
562                 if (ret)
563                         goto err;
564         }
565
566 retry:
567         bch2_trans_begin(&trans);
568         ret   = bch2_rename_trans(&trans,
569                                   src_dir->v.i_ino, &src_dir_u,
570                                   dst_dir->v.i_ino, &dst_dir_u,
571                                   &src_inode_u,
572                                   &dst_inode_u,
573                                   &src_dentry->d_name,
574                                   &dst_dentry->d_name,
575                                   mode) ?:
576                 bch2_trans_commit(&trans, NULL,
577                                   &journal_seq,
578                                   BTREE_INSERT_NOUNLOCK);
579         if (ret == -EINTR)
580                 goto retry;
581         if (unlikely(ret))
582                 goto err;
583
584         BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
585         BUG_ON(dst_inode &&
586                dst_inode->v.i_ino != dst_inode_u.bi_inum);
587
588         bch2_inode_update_after_write(c, src_dir, &src_dir_u,
589                                       ATTR_MTIME|ATTR_CTIME);
590         journal_seq_copy(src_dir, journal_seq);
591
592         if (src_dir != dst_dir) {
593                 bch2_inode_update_after_write(c, dst_dir, &dst_dir_u,
594                                               ATTR_MTIME|ATTR_CTIME);
595                 journal_seq_copy(dst_dir, journal_seq);
596         }
597
598         bch2_inode_update_after_write(c, src_inode, &src_inode_u,
599                                       ATTR_CTIME);
600         journal_seq_copy(src_inode, journal_seq);
601
602         if (dst_inode) {
603                 bch2_inode_update_after_write(c, dst_inode, &dst_inode_u,
604                                               ATTR_CTIME);
605                 journal_seq_copy(dst_inode, journal_seq);
606         }
607 err:
608         bch2_trans_exit(&trans);
609
610         bch2_fs_quota_transfer(c, src_inode,
611                                bch_qid(&src_inode->ei_inode),
612                                1 << QTYP_PRJ,
613                                KEY_TYPE_QUOTA_NOCHECK);
614         if (dst_inode)
615                 bch2_fs_quota_transfer(c, dst_inode,
616                                        bch_qid(&dst_inode->ei_inode),
617                                        1 << QTYP_PRJ,
618                                        KEY_TYPE_QUOTA_NOCHECK);
619
620         bch2_unlock_inodes(INODE_UPDATE_LOCK,
621                            src_dir,
622                            dst_dir,
623                            src_inode,
624                            dst_inode);
625
626         return ret;
627 }
628
629 void bch2_setattr_copy(struct bch_inode_info *inode,
630                        struct bch_inode_unpacked *bi,
631                        struct iattr *attr)
632 {
633         struct bch_fs *c = inode->v.i_sb->s_fs_info;
634         unsigned int ia_valid = attr->ia_valid;
635
636         if (ia_valid & ATTR_UID)
637                 bi->bi_uid = from_kuid(c->vfs_sb->s_user_ns, attr->ia_uid);
638         if (ia_valid & ATTR_GID)
639                 bi->bi_gid = from_kgid(c->vfs_sb->s_user_ns, attr->ia_gid);
640
641         if (ia_valid & ATTR_ATIME)
642                 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
643         if (ia_valid & ATTR_MTIME)
644                 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
645         if (ia_valid & ATTR_CTIME)
646                 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
647
648         if (ia_valid & ATTR_MODE) {
649                 umode_t mode = attr->ia_mode;
650                 kgid_t gid = ia_valid & ATTR_GID
651                         ? attr->ia_gid
652                         : inode->v.i_gid;
653
654                 if (!in_group_p(gid) &&
655                     !capable_wrt_inode_uidgid(&inode->v, CAP_FSETID))
656                         mode &= ~S_ISGID;
657                 bi->bi_mode = mode;
658         }
659 }
660
661 static int bch2_setattr_nonsize(struct bch_inode_info *inode,
662                                 struct iattr *attr)
663 {
664         struct bch_fs *c = inode->v.i_sb->s_fs_info;
665         struct bch_qid qid;
666         struct btree_trans trans;
667         struct btree_iter *inode_iter;
668         struct bch_inode_unpacked inode_u;
669         struct posix_acl *acl = NULL;
670         int ret;
671
672         mutex_lock(&inode->ei_update_lock);
673
674         qid = inode->ei_qid;
675
676         if (attr->ia_valid & ATTR_UID)
677                 qid.q[QTYP_USR] = from_kuid(&init_user_ns, attr->ia_uid);
678
679         if (attr->ia_valid & ATTR_GID)
680                 qid.q[QTYP_GRP] = from_kgid(&init_user_ns, attr->ia_gid);
681
682         ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
683                                      KEY_TYPE_QUOTA_PREALLOC);
684         if (ret)
685                 goto err;
686
687         bch2_trans_init(&trans, c, 0, 0);
688 retry:
689         bch2_trans_begin(&trans);
690         kfree(acl);
691         acl = NULL;
692
693         inode_iter = bch2_inode_peek(&trans, &inode_u, inode->v.i_ino,
694                                      BTREE_ITER_INTENT);
695         ret = PTR_ERR_OR_ZERO(inode_iter);
696         if (ret)
697                 goto btree_err;
698
699         bch2_setattr_copy(inode, &inode_u, attr);
700
701         if (attr->ia_valid & ATTR_MODE) {
702                 ret = bch2_acl_chmod(&trans, inode, inode_u.bi_mode, &acl);
703                 if (ret)
704                         goto btree_err;
705         }
706
707         ret =   bch2_inode_write(&trans, inode_iter, &inode_u) ?:
708                 bch2_trans_commit(&trans, NULL,
709                                   &inode->ei_journal_seq,
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 */