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