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