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