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