]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/fs.c
Update bcachefs sources to 3f3f969859 bcachefs: Fix some compiler warnings
[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/pagemap.h>
31 #include <linux/posix_acl.h>
32 #include <linux/random.h>
33 #include <linux/statfs.h>
34 #include <linux/string.h>
35 #include <linux/xattr.h>
36
37 static struct kmem_cache *bch2_inode_cache;
38
39 static void bch2_vfs_inode_init(struct bch_fs *,
40                                 struct bch_inode_info *,
41                                 struct bch_inode_unpacked *);
42
43 static void journal_seq_copy(struct bch_fs *c,
44                              struct bch_inode_info *dst,
45                              u64 journal_seq)
46 {
47         /*
48          * atomic64_cmpxchg has a fallback for archs that don't support it,
49          * cmpxchg does not:
50          */
51         atomic64_t *dst_seq = (void *) &dst->ei_journal_seq;
52         u64 old, v = READ_ONCE(dst->ei_journal_seq);
53
54         do {
55                 old = v;
56
57                 if (old >= journal_seq)
58                         break;
59         } while ((v = atomic64_cmpxchg(dst_seq, old, journal_seq)) != old);
60
61         bch2_journal_set_has_inum(&c->journal, dst->v.i_ino, journal_seq);
62 }
63
64 static void __pagecache_lock_put(struct pagecache_lock *lock, long i)
65 {
66         BUG_ON(atomic_long_read(&lock->v) == 0);
67
68         if (atomic_long_sub_return_release(i, &lock->v) == 0)
69                 wake_up_all(&lock->wait);
70 }
71
72 static bool __pagecache_lock_tryget(struct pagecache_lock *lock, long i)
73 {
74         long v = atomic_long_read(&lock->v), old;
75
76         do {
77                 old = v;
78
79                 if (i > 0 ? v < 0 : v > 0)
80                         return false;
81         } while ((v = atomic_long_cmpxchg_acquire(&lock->v,
82                                         old, old + i)) != old);
83         return true;
84 }
85
86 static void __pagecache_lock_get(struct pagecache_lock *lock, long i)
87 {
88         wait_event(lock->wait, __pagecache_lock_tryget(lock, i));
89 }
90
91 void bch2_pagecache_add_put(struct pagecache_lock *lock)
92 {
93         __pagecache_lock_put(lock, 1);
94 }
95
96 bool bch2_pagecache_add_tryget(struct pagecache_lock *lock)
97 {
98         return __pagecache_lock_tryget(lock, 1);
99 }
100
101 void bch2_pagecache_add_get(struct pagecache_lock *lock)
102 {
103         __pagecache_lock_get(lock, 1);
104 }
105
106 void bch2_pagecache_block_put(struct pagecache_lock *lock)
107 {
108         __pagecache_lock_put(lock, -1);
109 }
110
111 void bch2_pagecache_block_get(struct pagecache_lock *lock)
112 {
113         __pagecache_lock_get(lock, -1);
114 }
115
116 void bch2_inode_update_after_write(struct bch_fs *c,
117                                    struct bch_inode_info *inode,
118                                    struct bch_inode_unpacked *bi,
119                                    unsigned fields)
120 {
121         set_nlink(&inode->v, bch2_inode_nlink_get(bi));
122         i_uid_write(&inode->v, bi->bi_uid);
123         i_gid_write(&inode->v, bi->bi_gid);
124         inode->v.i_mode = bi->bi_mode;
125
126         if (fields & ATTR_ATIME)
127                 inode->v.i_atime = bch2_time_to_timespec(c, bi->bi_atime);
128         if (fields & ATTR_MTIME)
129                 inode->v.i_mtime = bch2_time_to_timespec(c, bi->bi_mtime);
130         if (fields & ATTR_CTIME)
131                 inode->v.i_ctime = bch2_time_to_timespec(c, bi->bi_ctime);
132
133         inode->ei_inode         = *bi;
134
135         bch2_inode_flags_to_vfs(inode);
136 }
137
138 int __must_check bch2_write_inode(struct bch_fs *c,
139                                   struct bch_inode_info *inode,
140                                   inode_set_fn set,
141                                   void *p, unsigned fields)
142 {
143         struct btree_trans trans;
144         struct btree_iter iter = { NULL };
145         struct bch_inode_unpacked inode_u;
146         int ret;
147
148         bch2_trans_init(&trans, c, 0, 512);
149 retry:
150         bch2_trans_begin(&trans);
151
152         ret   = bch2_inode_peek(&trans, &iter, &inode_u, inode->v.i_ino,
153                                 BTREE_ITER_INTENT) ?:
154                 (set ? set(inode, &inode_u, p) : 0) ?:
155                 bch2_inode_write(&trans, &iter, &inode_u) ?:
156                 bch2_trans_commit(&trans, NULL,
157                                   &inode->ei_journal_seq,
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_exit(&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 user_namespace *mnt_userns,
247               struct bch_inode_info *dir, struct dentry *dentry,
248               umode_t mode, dev_t rdev, bool tmpfile)
249 {
250         struct bch_fs *c = dir->v.i_sb->s_fs_info;
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(mnt_userns, current_fsuid()),
287                                   from_kgid(mnt_userns, 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, 0);
296         if (unlikely(ret)) {
297                 bch2_quota_acct(c, bch_qid(&inode_u), Q_INO, -1,
298                                 KEY_TYPE_QUOTA_WARN);
299 err_before_quota:
300                 if (ret == -EINTR)
301                         goto retry;
302                 goto err_trans;
303         }
304
305         if (!tmpfile) {
306                 bch2_inode_update_after_write(c, dir, &dir_u,
307                                               ATTR_MTIME|ATTR_CTIME);
308                 journal_seq_copy(c, dir, journal_seq);
309                 mutex_unlock(&dir->ei_update_lock);
310         }
311
312         bch2_vfs_inode_init(c, inode, &inode_u);
313         journal_seq_copy(c, inode, journal_seq);
314
315         set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
316         set_cached_acl(&inode->v, ACL_TYPE_DEFAULT, default_acl);
317
318         /*
319          * we must insert the new inode into the inode cache before calling
320          * bch2_trans_exit() and dropping locks, else we could race with another
321          * thread pulling the inode in and modifying it:
322          */
323
324         inode->v.i_state |= I_CREATING;
325         old = to_bch_ei(inode_insert5(&inode->v, inode->v.i_ino,
326                                       inum_test, NULL, &inode->v.i_ino));
327         BUG_ON(!old);
328
329         if (unlikely(old != inode)) {
330                 /*
331                  * We raced, another process pulled the new inode into cache
332                  * before us:
333                  */
334                 journal_seq_copy(c, old, journal_seq);
335                 make_bad_inode(&inode->v);
336                 iput(&inode->v);
337
338                 inode = old;
339         } else {
340                 /*
341                  * we really don't want insert_inode_locked2() to be setting
342                  * I_NEW...
343                  */
344                 unlock_new_inode(&inode->v);
345         }
346
347         bch2_trans_exit(&trans);
348 err:
349         posix_acl_release(default_acl);
350         posix_acl_release(acl);
351         return inode;
352 err_trans:
353         if (!tmpfile)
354                 mutex_unlock(&dir->ei_update_lock);
355
356         bch2_trans_exit(&trans);
357         make_bad_inode(&inode->v);
358         iput(&inode->v);
359         inode = ERR_PTR(ret);
360         goto err;
361 }
362
363 /* methods */
364
365 static struct dentry *bch2_lookup(struct inode *vdir, struct dentry *dentry,
366                                   unsigned int flags)
367 {
368         struct bch_fs *c = vdir->i_sb->s_fs_info;
369         struct bch_inode_info *dir = to_bch_ei(vdir);
370         struct bch_hash_info hash = bch2_hash_info_init(c, &dir->ei_inode);
371         struct inode *vinode = NULL;
372         u64 inum;
373
374         inum = bch2_dirent_lookup(c, dir->v.i_ino, &hash,
375                                   &dentry->d_name);
376
377         if (inum)
378                 vinode = bch2_vfs_inode_get(c, inum);
379
380         return d_splice_alias(vinode, dentry);
381 }
382
383 static int bch2_mknod(struct user_namespace *mnt_userns,
384                       struct inode *vdir, struct dentry *dentry,
385                       umode_t mode, dev_t rdev)
386 {
387         struct bch_inode_info *inode =
388                 __bch2_create(mnt_userns, 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 user_namespace *mnt_userns,
398                        struct inode *vdir, struct dentry *dentry,
399                        umode_t mode, bool excl)
400 {
401         return bch2_mknod(mnt_userns, vdir, dentry, mode|S_IFREG, 0);
402 }
403
404 static int __bch2_link(struct bch_fs *c,
405                        struct bch_inode_info *inode,
406                        struct bch_inode_info *dir,
407                        struct dentry *dentry)
408 {
409         struct btree_trans trans;
410         struct bch_inode_unpacked dir_u, inode_u;
411         int ret;
412
413         mutex_lock(&inode->ei_update_lock);
414         bch2_trans_init(&trans, c, 4, 1024);
415
416         ret = __bch2_trans_do(&trans, NULL, &inode->ei_journal_seq, 0,
417                         bch2_link_trans(&trans,
418                                         dir->v.i_ino,
419                                         inode->v.i_ino, &dir_u, &inode_u,
420                                         &dentry->d_name));
421
422         if (likely(!ret)) {
423                 BUG_ON(inode_u.bi_inum != inode->v.i_ino);
424
425                 journal_seq_copy(c, inode, dir->ei_journal_seq);
426                 bch2_inode_update_after_write(c, dir, &dir_u,
427                                               ATTR_MTIME|ATTR_CTIME);
428                 bch2_inode_update_after_write(c, inode, &inode_u, ATTR_CTIME);
429         }
430
431         bch2_trans_exit(&trans);
432         mutex_unlock(&inode->ei_update_lock);
433         return ret;
434 }
435
436 static int bch2_link(struct dentry *old_dentry, struct inode *vdir,
437                      struct dentry *dentry)
438 {
439         struct bch_fs *c = vdir->i_sb->s_fs_info;
440         struct bch_inode_info *dir = to_bch_ei(vdir);
441         struct bch_inode_info *inode = to_bch_ei(old_dentry->d_inode);
442         int ret;
443
444         lockdep_assert_held(&inode->v.i_rwsem);
445
446         ret = __bch2_link(c, inode, dir, dentry);
447         if (unlikely(ret))
448                 return ret;
449
450         ihold(&inode->v);
451         d_instantiate(dentry, &inode->v);
452         return 0;
453 }
454
455 static int bch2_unlink(struct inode *vdir, struct dentry *dentry)
456 {
457         struct bch_fs *c = vdir->i_sb->s_fs_info;
458         struct bch_inode_info *dir = to_bch_ei(vdir);
459         struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
460         struct bch_inode_unpacked dir_u, inode_u;
461         struct btree_trans trans;
462         int ret;
463
464         bch2_lock_inodes(INODE_UPDATE_LOCK, dir, inode);
465         bch2_trans_init(&trans, c, 4, 1024);
466
467         ret = __bch2_trans_do(&trans, NULL, &dir->ei_journal_seq,
468                               BTREE_INSERT_NOFAIL,
469                         bch2_unlink_trans(&trans,
470                                           dir->v.i_ino, &dir_u,
471                                           &inode_u, &dentry->d_name));
472
473         if (likely(!ret)) {
474                 BUG_ON(inode_u.bi_inum != inode->v.i_ino);
475
476                 journal_seq_copy(c, inode, dir->ei_journal_seq);
477                 bch2_inode_update_after_write(c, dir, &dir_u,
478                                               ATTR_MTIME|ATTR_CTIME);
479                 bch2_inode_update_after_write(c, inode, &inode_u,
480                                               ATTR_MTIME);
481         }
482
483         bch2_trans_exit(&trans);
484         bch2_unlock_inodes(INODE_UPDATE_LOCK, dir, inode);
485
486         return ret;
487 }
488
489 static int bch2_symlink(struct user_namespace *mnt_userns,
490                         struct inode *vdir, struct dentry *dentry,
491                         const char *symname)
492 {
493         struct bch_fs *c = vdir->i_sb->s_fs_info;
494         struct bch_inode_info *dir = to_bch_ei(vdir), *inode;
495         int ret;
496
497         inode = __bch2_create(mnt_userns, dir, dentry, S_IFLNK|S_IRWXUGO, 0, true);
498         if (unlikely(IS_ERR(inode)))
499                 return PTR_ERR(inode);
500
501         inode_lock(&inode->v);
502         ret = page_symlink(&inode->v, symname, strlen(symname) + 1);
503         inode_unlock(&inode->v);
504
505         if (unlikely(ret))
506                 goto err;
507
508         ret = filemap_write_and_wait_range(inode->v.i_mapping, 0, LLONG_MAX);
509         if (unlikely(ret))
510                 goto err;
511
512         journal_seq_copy(c, dir, inode->ei_journal_seq);
513
514         ret = __bch2_link(c, inode, dir, dentry);
515         if (unlikely(ret))
516                 goto err;
517
518         d_instantiate(dentry, &inode->v);
519         return 0;
520 err:
521         iput(&inode->v);
522         return ret;
523 }
524
525 static int bch2_mkdir(struct user_namespace *mnt_userns,
526                       struct inode *vdir, struct dentry *dentry, umode_t mode)
527 {
528         return bch2_mknod(mnt_userns, vdir, dentry, mode|S_IFDIR, 0);
529 }
530
531 static int bch2_rename2(struct user_namespace *mnt_userns,
532                         struct inode *src_vdir, struct dentry *src_dentry,
533                         struct inode *dst_vdir, struct dentry *dst_dentry,
534                         unsigned flags)
535 {
536         struct bch_fs *c = src_vdir->i_sb->s_fs_info;
537         struct bch_inode_info *src_dir = to_bch_ei(src_vdir);
538         struct bch_inode_info *dst_dir = to_bch_ei(dst_vdir);
539         struct bch_inode_info *src_inode = to_bch_ei(src_dentry->d_inode);
540         struct bch_inode_info *dst_inode = to_bch_ei(dst_dentry->d_inode);
541         struct bch_inode_unpacked dst_dir_u, src_dir_u;
542         struct bch_inode_unpacked src_inode_u, dst_inode_u;
543         struct btree_trans trans;
544         enum bch_rename_mode mode = flags & RENAME_EXCHANGE
545                 ? BCH_RENAME_EXCHANGE
546                 : dst_dentry->d_inode
547                 ? BCH_RENAME_OVERWRITE : BCH_RENAME;
548         u64 journal_seq = 0;
549         int ret;
550
551         if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
552                 return -EINVAL;
553
554         if (mode == BCH_RENAME_OVERWRITE) {
555                 ret = filemap_write_and_wait_range(src_inode->v.i_mapping,
556                                                    0, LLONG_MAX);
557                 if (ret)
558                         return ret;
559         }
560
561         bch2_trans_init(&trans, c, 8, 2048);
562
563         bch2_lock_inodes(INODE_UPDATE_LOCK,
564                          src_dir,
565                          dst_dir,
566                          src_inode,
567                          dst_inode);
568
569         if (inode_attr_changing(dst_dir, src_inode, Inode_opt_project)) {
570                 ret = bch2_fs_quota_transfer(c, src_inode,
571                                              dst_dir->ei_qid,
572                                              1 << QTYP_PRJ,
573                                              KEY_TYPE_QUOTA_PREALLOC);
574                 if (ret)
575                         goto err;
576         }
577
578         if (mode == BCH_RENAME_EXCHANGE &&
579             inode_attr_changing(src_dir, dst_inode, Inode_opt_project)) {
580                 ret = bch2_fs_quota_transfer(c, dst_inode,
581                                              src_dir->ei_qid,
582                                              1 << QTYP_PRJ,
583                                              KEY_TYPE_QUOTA_PREALLOC);
584                 if (ret)
585                         goto err;
586         }
587
588         ret = __bch2_trans_do(&trans, NULL, &journal_seq, 0,
589                         bch2_rename_trans(&trans,
590                                           src_dir->v.i_ino, &src_dir_u,
591                                           dst_dir->v.i_ino, &dst_dir_u,
592                                           &src_inode_u,
593                                           &dst_inode_u,
594                                           &src_dentry->d_name,
595                                           &dst_dentry->d_name,
596                                           mode));
597         if (unlikely(ret))
598                 goto err;
599
600         BUG_ON(src_inode->v.i_ino != src_inode_u.bi_inum);
601         BUG_ON(dst_inode &&
602                dst_inode->v.i_ino != dst_inode_u.bi_inum);
603
604         bch2_inode_update_after_write(c, src_dir, &src_dir_u,
605                                       ATTR_MTIME|ATTR_CTIME);
606         journal_seq_copy(c, src_dir, journal_seq);
607
608         if (src_dir != dst_dir) {
609                 bch2_inode_update_after_write(c, dst_dir, &dst_dir_u,
610                                               ATTR_MTIME|ATTR_CTIME);
611                 journal_seq_copy(c, dst_dir, journal_seq);
612         }
613
614         bch2_inode_update_after_write(c, src_inode, &src_inode_u,
615                                       ATTR_CTIME);
616         journal_seq_copy(c, src_inode, journal_seq);
617
618         if (dst_inode) {
619                 bch2_inode_update_after_write(c, dst_inode, &dst_inode_u,
620                                               ATTR_CTIME);
621                 journal_seq_copy(c, dst_inode, journal_seq);
622         }
623 err:
624         bch2_trans_exit(&trans);
625
626         bch2_fs_quota_transfer(c, src_inode,
627                                bch_qid(&src_inode->ei_inode),
628                                1 << QTYP_PRJ,
629                                KEY_TYPE_QUOTA_NOCHECK);
630         if (dst_inode)
631                 bch2_fs_quota_transfer(c, dst_inode,
632                                        bch_qid(&dst_inode->ei_inode),
633                                        1 << QTYP_PRJ,
634                                        KEY_TYPE_QUOTA_NOCHECK);
635
636         bch2_unlock_inodes(INODE_UPDATE_LOCK,
637                            src_dir,
638                            dst_dir,
639                            src_inode,
640                            dst_inode);
641
642         return ret;
643 }
644
645 static void bch2_setattr_copy(struct user_namespace *mnt_userns,
646                               struct bch_inode_info *inode,
647                               struct bch_inode_unpacked *bi,
648                               struct iattr *attr)
649 {
650         struct bch_fs *c = inode->v.i_sb->s_fs_info;
651         unsigned int ia_valid = attr->ia_valid;
652
653         if (ia_valid & ATTR_UID)
654                 bi->bi_uid = from_kuid(mnt_userns, attr->ia_uid);
655         if (ia_valid & ATTR_GID)
656                 bi->bi_gid = from_kgid(mnt_userns, attr->ia_gid);
657
658         if (ia_valid & ATTR_SIZE)
659                 bi->bi_size = attr->ia_size;
660
661         if (ia_valid & ATTR_ATIME)
662                 bi->bi_atime = timespec_to_bch2_time(c, attr->ia_atime);
663         if (ia_valid & ATTR_MTIME)
664                 bi->bi_mtime = timespec_to_bch2_time(c, attr->ia_mtime);
665         if (ia_valid & ATTR_CTIME)
666                 bi->bi_ctime = timespec_to_bch2_time(c, attr->ia_ctime);
667
668         if (ia_valid & ATTR_MODE) {
669                 umode_t mode = attr->ia_mode;
670                 kgid_t gid = ia_valid & ATTR_GID
671                         ? attr->ia_gid
672                         : inode->v.i_gid;
673
674                 if (!in_group_p(gid) &&
675                     !capable_wrt_inode_uidgid(mnt_userns, &inode->v, CAP_FSETID))
676                         mode &= ~S_ISGID;
677                 bi->bi_mode = mode;
678         }
679 }
680
681 int bch2_setattr_nonsize(struct user_namespace *mnt_userns,
682                          struct bch_inode_info *inode,
683                          struct iattr *attr)
684 {
685         struct bch_fs *c = inode->v.i_sb->s_fs_info;
686         struct bch_qid qid;
687         struct btree_trans trans;
688         struct btree_iter inode_iter = { NULL };
689         struct bch_inode_unpacked inode_u;
690         struct posix_acl *acl = NULL;
691         int ret;
692
693         mutex_lock(&inode->ei_update_lock);
694
695         qid = inode->ei_qid;
696
697         if (attr->ia_valid & ATTR_UID)
698                 qid.q[QTYP_USR] = from_kuid(&init_user_ns, attr->ia_uid);
699
700         if (attr->ia_valid & ATTR_GID)
701                 qid.q[QTYP_GRP] = from_kgid(&init_user_ns, attr->ia_gid);
702
703         ret = bch2_fs_quota_transfer(c, inode, qid, ~0,
704                                      KEY_TYPE_QUOTA_PREALLOC);
705         if (ret)
706                 goto err;
707
708         bch2_trans_init(&trans, c, 0, 0);
709 retry:
710         bch2_trans_begin(&trans);
711         kfree(acl);
712         acl = NULL;
713
714         ret = bch2_inode_peek(&trans, &inode_iter, &inode_u, inode->v.i_ino,
715                               BTREE_ITER_INTENT);
716         if (ret)
717                 goto btree_err;
718
719         bch2_setattr_copy(mnt_userns, inode, &inode_u, attr);
720
721         if (attr->ia_valid & ATTR_MODE) {
722                 ret = bch2_acl_chmod(&trans, &inode_u, inode_u.bi_mode, &acl);
723                 if (ret)
724                         goto btree_err;
725         }
726
727         ret =   bch2_inode_write(&trans, &inode_iter, &inode_u) ?:
728                 bch2_trans_commit(&trans, NULL,
729                                   &inode->ei_journal_seq,
730                                   BTREE_INSERT_NOFAIL);
731 btree_err:
732         bch2_trans_iter_exit(&trans, &inode_iter);
733
734         if (ret == -EINTR)
735                 goto retry;
736         if (unlikely(ret))
737                 goto err_trans;
738
739         bch2_inode_update_after_write(c, inode, &inode_u, attr->ia_valid);
740
741         if (acl)
742                 set_cached_acl(&inode->v, ACL_TYPE_ACCESS, acl);
743 err_trans:
744         bch2_trans_exit(&trans);
745 err:
746         mutex_unlock(&inode->ei_update_lock);
747
748         return ret;
749 }
750
751 static int bch2_getattr(struct user_namespace *mnt_userns,
752                         const struct path *path, struct kstat *stat,
753                         u32 request_mask, unsigned query_flags)
754 {
755         struct bch_inode_info *inode = to_bch_ei(d_inode(path->dentry));
756         struct bch_fs *c = inode->v.i_sb->s_fs_info;
757
758         stat->dev       = inode->v.i_sb->s_dev;
759         stat->ino       = inode->v.i_ino;
760         stat->mode      = inode->v.i_mode;
761         stat->nlink     = inode->v.i_nlink;
762         stat->uid       = inode->v.i_uid;
763         stat->gid       = inode->v.i_gid;
764         stat->rdev      = inode->v.i_rdev;
765         stat->size      = i_size_read(&inode->v);
766         stat->atime     = inode->v.i_atime;
767         stat->mtime     = inode->v.i_mtime;
768         stat->ctime     = inode->v.i_ctime;
769         stat->blksize   = block_bytes(c);
770         stat->blocks    = inode->v.i_blocks;
771
772         if (request_mask & STATX_BTIME) {
773                 stat->result_mask |= STATX_BTIME;
774                 stat->btime = bch2_time_to_timespec(c, inode->ei_inode.bi_otime);
775         }
776
777         if (inode->ei_inode.bi_flags & BCH_INODE_IMMUTABLE)
778                 stat->attributes |= STATX_ATTR_IMMUTABLE;
779         stat->attributes_mask    |= STATX_ATTR_IMMUTABLE;
780
781         if (inode->ei_inode.bi_flags & BCH_INODE_APPEND)
782                 stat->attributes |= STATX_ATTR_APPEND;
783         stat->attributes_mask    |= STATX_ATTR_APPEND;
784
785         if (inode->ei_inode.bi_flags & BCH_INODE_NODUMP)
786                 stat->attributes |= STATX_ATTR_NODUMP;
787         stat->attributes_mask    |= STATX_ATTR_NODUMP;
788
789         return 0;
790 }
791
792 static int bch2_setattr(struct user_namespace *mnt_userns,
793                         struct dentry *dentry, struct iattr *iattr)
794 {
795         struct bch_inode_info *inode = to_bch_ei(dentry->d_inode);
796         int ret;
797
798         lockdep_assert_held(&inode->v.i_rwsem);
799
800         ret = setattr_prepare(mnt_userns, dentry, iattr);
801         if (ret)
802                 return ret;
803
804         return iattr->ia_valid & ATTR_SIZE
805                 ? bch2_truncate(mnt_userns, inode, iattr)
806                 : bch2_setattr_nonsize(mnt_userns, inode, iattr);
807 }
808
809 static int bch2_tmpfile(struct user_namespace *mnt_userns,
810                         struct inode *vdir, struct dentry *dentry, umode_t mode)
811 {
812         struct bch_inode_info *inode =
813                 __bch2_create(mnt_userns, to_bch_ei(vdir), dentry, mode, 0, true);
814
815         if (IS_ERR(inode))
816                 return PTR_ERR(inode);
817
818         d_mark_tmpfile(dentry, &inode->v);
819         d_instantiate(dentry, &inode->v);
820         return 0;
821 }
822
823 static int bch2_fill_extent(struct bch_fs *c,
824                             struct fiemap_extent_info *info,
825                             struct bkey_s_c k, unsigned flags)
826 {
827         if (bkey_extent_is_direct_data(k.k)) {
828                 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
829                 const union bch_extent_entry *entry;
830                 struct extent_ptr_decoded p;
831                 int ret;
832
833                 if (k.k->type == KEY_TYPE_reflink_v)
834                         flags |= FIEMAP_EXTENT_SHARED;
835
836                 bkey_for_each_ptr_decode(k.k, ptrs, p, entry) {
837                         int flags2 = 0;
838                         u64 offset = p.ptr.offset;
839
840                         if (p.crc.compression_type)
841                                 flags2 |= FIEMAP_EXTENT_ENCODED;
842                         else
843                                 offset += p.crc.offset;
844
845                         if ((offset & (c->opts.block_size - 1)) ||
846                             (k.k->size & (c->opts.block_size - 1)))
847                                 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
848
849                         ret = fiemap_fill_next_extent(info,
850                                                 bkey_start_offset(k.k) << 9,
851                                                 offset << 9,
852                                                 k.k->size << 9, flags|flags2);
853                         if (ret)
854                                 return ret;
855                 }
856
857                 return 0;
858         } else if (bkey_extent_is_inline_data(k.k)) {
859                 return fiemap_fill_next_extent(info,
860                                                bkey_start_offset(k.k) << 9,
861                                                0, k.k->size << 9,
862                                                flags|
863                                                FIEMAP_EXTENT_DATA_INLINE);
864         } else if (k.k->type == KEY_TYPE_reservation) {
865                 return fiemap_fill_next_extent(info,
866                                                bkey_start_offset(k.k) << 9,
867                                                0, k.k->size << 9,
868                                                flags|
869                                                FIEMAP_EXTENT_DELALLOC|
870                                                FIEMAP_EXTENT_UNWRITTEN);
871         } else {
872                 BUG();
873         }
874 }
875
876 static int bch2_fiemap(struct inode *vinode, struct fiemap_extent_info *info,
877                        u64 start, u64 len)
878 {
879         struct bch_fs *c = vinode->i_sb->s_fs_info;
880         struct bch_inode_info *ei = to_bch_ei(vinode);
881         struct btree_trans trans;
882         struct btree_iter iter;
883         struct bkey_s_c k;
884         struct bkey_buf cur, prev;
885         struct bpos end = POS(ei->v.i_ino, (start + len) >> 9);
886         unsigned offset_into_extent, sectors;
887         bool have_extent = false;
888         int ret = 0;
889
890         ret = fiemap_prep(&ei->v, info, start, &len, FIEMAP_FLAG_SYNC);
891         if (ret)
892                 return ret;
893
894         if (start + len < start)
895                 return -EINVAL;
896
897         bch2_bkey_buf_init(&cur);
898         bch2_bkey_buf_init(&prev);
899         bch2_trans_init(&trans, c, 0, 0);
900
901         bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
902                              POS(ei->v.i_ino, start >> 9), 0);
903 retry:
904         bch2_trans_begin(&trans);
905
906         while ((k = bch2_btree_iter_peek(&iter)).k &&
907                !(ret = bkey_err(k)) &&
908                bkey_cmp(iter.pos, end) < 0) {
909                 enum btree_id data_btree = BTREE_ID_extents;
910
911                 if (!bkey_extent_is_data(k.k) &&
912                     k.k->type != KEY_TYPE_reservation) {
913                         bch2_btree_iter_advance(&iter);
914                         continue;
915                 }
916
917                 offset_into_extent      = iter.pos.offset -
918                         bkey_start_offset(k.k);
919                 sectors                 = k.k->size - offset_into_extent;
920
921                 bch2_bkey_buf_reassemble(&cur, c, k);
922
923                 ret = bch2_read_indirect_extent(&trans, &data_btree,
924                                         &offset_into_extent, &cur);
925                 if (ret)
926                         break;
927
928                 k = bkey_i_to_s_c(cur.k);
929                 bch2_bkey_buf_realloc(&prev, c, k.k->u64s);
930
931                 sectors = min(sectors, k.k->size - offset_into_extent);
932
933                 bch2_cut_front(POS(k.k->p.inode,
934                                    bkey_start_offset(k.k) +
935                                    offset_into_extent),
936                                cur.k);
937                 bch2_key_resize(&cur.k->k, sectors);
938                 cur.k->k.p = iter.pos;
939                 cur.k->k.p.offset += cur.k->k.size;
940
941                 if (have_extent) {
942                         ret = bch2_fill_extent(c, info,
943                                         bkey_i_to_s_c(prev.k), 0);
944                         if (ret)
945                                 break;
946                 }
947
948                 bkey_copy(prev.k, cur.k);
949                 have_extent = true;
950
951                 bch2_btree_iter_set_pos(&iter,
952                         POS(iter.pos.inode, iter.pos.offset + sectors));
953         }
954
955         if (ret == -EINTR)
956                 goto retry;
957
958         if (!ret && have_extent)
959                 ret = bch2_fill_extent(c, info, bkey_i_to_s_c(prev.k),
960                                        FIEMAP_EXTENT_LAST);
961
962         bch2_trans_iter_exit(&trans, &iter);
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_qid           = bch_qid(bi);
1155
1156         inode->v.i_mapping->a_ops = &bch_address_space_operations;
1157
1158         switch (inode->v.i_mode & S_IFMT) {
1159         case S_IFREG:
1160                 inode->v.i_op   = &bch_file_inode_operations;
1161                 inode->v.i_fop  = &bch_file_operations;
1162                 break;
1163         case S_IFDIR:
1164                 inode->v.i_op   = &bch_dir_inode_operations;
1165                 inode->v.i_fop  = &bch_dir_file_operations;
1166                 break;
1167         case S_IFLNK:
1168                 inode_nohighmem(&inode->v);
1169                 inode->v.i_op   = &bch_symlink_inode_operations;
1170                 break;
1171         default:
1172                 init_special_inode(&inode->v, inode->v.i_mode, inode->v.i_rdev);
1173                 inode->v.i_op   = &bch_special_inode_operations;
1174                 break;
1175         }
1176 }
1177
1178 static struct inode *bch2_alloc_inode(struct super_block *sb)
1179 {
1180         struct bch_inode_info *inode;
1181
1182         inode = kmem_cache_alloc(bch2_inode_cache, GFP_NOFS);
1183         if (!inode)
1184                 return NULL;
1185
1186         inode_init_once(&inode->v);
1187         mutex_init(&inode->ei_update_lock);
1188         pagecache_lock_init(&inode->ei_pagecache_lock);
1189         mutex_init(&inode->ei_quota_lock);
1190         inode->ei_journal_seq = 0;
1191
1192         return &inode->v;
1193 }
1194
1195 static void bch2_i_callback(struct rcu_head *head)
1196 {
1197         struct inode *vinode = container_of(head, struct inode, i_rcu);
1198         struct bch_inode_info *inode = to_bch_ei(vinode);
1199
1200         kmem_cache_free(bch2_inode_cache, inode);
1201 }
1202
1203 static void bch2_destroy_inode(struct inode *vinode)
1204 {
1205         call_rcu(&vinode->i_rcu, bch2_i_callback);
1206 }
1207
1208 static int inode_update_times_fn(struct bch_inode_info *inode,
1209                                  struct bch_inode_unpacked *bi,
1210                                  void *p)
1211 {
1212         struct bch_fs *c = inode->v.i_sb->s_fs_info;
1213
1214         bi->bi_atime    = timespec_to_bch2_time(c, inode->v.i_atime);
1215         bi->bi_mtime    = timespec_to_bch2_time(c, inode->v.i_mtime);
1216         bi->bi_ctime    = timespec_to_bch2_time(c, inode->v.i_ctime);
1217
1218         return 0;
1219 }
1220
1221 static int bch2_vfs_write_inode(struct inode *vinode,
1222                                 struct writeback_control *wbc)
1223 {
1224         struct bch_fs *c = vinode->i_sb->s_fs_info;
1225         struct bch_inode_info *inode = to_bch_ei(vinode);
1226         int ret;
1227
1228         mutex_lock(&inode->ei_update_lock);
1229         ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
1230                                ATTR_ATIME|ATTR_MTIME|ATTR_CTIME);
1231         mutex_unlock(&inode->ei_update_lock);
1232
1233         return ret;
1234 }
1235
1236 static void bch2_evict_inode(struct inode *vinode)
1237 {
1238         struct bch_fs *c = vinode->i_sb->s_fs_info;
1239         struct bch_inode_info *inode = to_bch_ei(vinode);
1240
1241         truncate_inode_pages_final(&inode->v.i_data);
1242
1243         clear_inode(&inode->v);
1244
1245         BUG_ON(!is_bad_inode(&inode->v) && inode->ei_quota_reserved);
1246
1247         if (!inode->v.i_nlink && !is_bad_inode(&inode->v)) {
1248                 bch2_quota_acct(c, inode->ei_qid, Q_SPC, -((s64) inode->v.i_blocks),
1249                                 KEY_TYPE_QUOTA_WARN);
1250                 bch2_quota_acct(c, inode->ei_qid, Q_INO, -1,
1251                                 KEY_TYPE_QUOTA_WARN);
1252                 bch2_inode_rm(c, inode->v.i_ino, true);
1253         }
1254 }
1255
1256 static int bch2_statfs(struct dentry *dentry, struct kstatfs *buf)
1257 {
1258         struct super_block *sb = dentry->d_sb;
1259         struct bch_fs *c = sb->s_fs_info;
1260         struct bch_fs_usage_short usage = bch2_fs_usage_read_short(c);
1261         unsigned shift = sb->s_blocksize_bits - 9;
1262         /*
1263          * this assumes inodes take up 64 bytes, which is a decent average
1264          * number:
1265          */
1266         u64 avail_inodes = ((usage.capacity - usage.used) << 3);
1267         u64 fsid;
1268
1269         buf->f_type     = BCACHEFS_STATFS_MAGIC;
1270         buf->f_bsize    = sb->s_blocksize;
1271         buf->f_blocks   = usage.capacity >> shift;
1272         buf->f_bfree    = usage.free >> shift;
1273         buf->f_bavail   = avail_factor(usage.free) >> shift;
1274
1275         buf->f_files    = usage.nr_inodes + avail_inodes;
1276         buf->f_ffree    = avail_inodes;
1277
1278         fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
1279                le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
1280         buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1281         buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1282         buf->f_namelen  = BCH_NAME_MAX;
1283
1284         return 0;
1285 }
1286
1287 static int bch2_sync_fs(struct super_block *sb, int wait)
1288 {
1289         struct bch_fs *c = sb->s_fs_info;
1290
1291         if (c->opts.journal_flush_disabled)
1292                 return 0;
1293
1294         if (!wait) {
1295                 bch2_journal_flush_async(&c->journal, NULL);
1296                 return 0;
1297         }
1298
1299         return bch2_journal_flush(&c->journal);
1300 }
1301
1302 static struct bch_fs *bch2_path_to_fs(const char *path)
1303 {
1304         struct bch_fs *c;
1305         dev_t dev;
1306         int ret;
1307
1308         ret = lookup_bdev(path, &dev);
1309         if (ret)
1310                 return ERR_PTR(ret);
1311
1312         c = bch2_dev_to_fs(dev);
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         if (!dev_name || strlen(dev_name) == 0)
1499                 return ERR_PTR(-EINVAL);
1500
1501         devs = split_devs(dev_name, &nr_devs);
1502         if (!devs)
1503                 return ERR_PTR(-ENOMEM);
1504
1505         devs_to_fs = kcalloc(nr_devs + 1, sizeof(void *), GFP_KERNEL);
1506         if (!devs_to_fs) {
1507                 sb = ERR_PTR(-ENOMEM);
1508                 goto got_sb;
1509         }
1510
1511         for (i = 0; i < nr_devs; i++)
1512                 devs_to_fs[i] = bch2_path_to_fs(devs[i]);
1513
1514         sb = sget(fs_type, bch2_test_super, bch2_noset_super,
1515                   flags|SB_NOSEC, devs_to_fs);
1516         if (!IS_ERR(sb))
1517                 goto got_sb;
1518
1519         c = bch2_fs_open(devs, nr_devs, opts);
1520         if (IS_ERR(c)) {
1521                 sb = ERR_CAST(c);
1522                 goto got_sb;
1523         }
1524
1525         /* Some options can't be parsed until after the fs is started: */
1526         ret = bch2_parse_mount_opts(c, &opts, data);
1527         if (ret) {
1528                 bch2_fs_stop(c);
1529                 sb = ERR_PTR(ret);
1530                 goto got_sb;
1531         }
1532
1533         bch2_opts_apply(&c->opts, opts);
1534
1535         sb = sget(fs_type, NULL, bch2_set_super, flags|SB_NOSEC, c);
1536         if (IS_ERR(sb))
1537                 bch2_fs_stop(c);
1538 got_sb:
1539         kfree(devs_to_fs);
1540         kfree(devs[0]);
1541         kfree(devs);
1542
1543         if (IS_ERR(sb))
1544                 return ERR_CAST(sb);
1545
1546         c = sb->s_fs_info;
1547
1548         if (sb->s_root) {
1549                 if ((flags ^ sb->s_flags) & SB_RDONLY) {
1550                         ret = -EBUSY;
1551                         goto err_put_super;
1552                 }
1553                 goto out;
1554         }
1555
1556         sb->s_blocksize         = block_bytes(c);
1557         sb->s_blocksize_bits    = ilog2(block_bytes(c));
1558         sb->s_maxbytes          = MAX_LFS_FILESIZE;
1559         sb->s_op                = &bch_super_operations;
1560         sb->s_export_op         = &bch_export_ops;
1561 #ifdef CONFIG_BCACHEFS_QUOTA
1562         sb->s_qcop              = &bch2_quotactl_operations;
1563         sb->s_quota_types       = QTYPE_MASK_USR|QTYPE_MASK_GRP|QTYPE_MASK_PRJ;
1564 #endif
1565         sb->s_xattr             = bch2_xattr_handlers;
1566         sb->s_magic             = BCACHEFS_STATFS_MAGIC;
1567         sb->s_time_gran         = c->sb.nsec_per_time_unit;
1568         sb->s_time_min          = div_s64(S64_MIN, c->sb.time_units_per_sec) + 1;
1569         sb->s_time_max          = div_s64(S64_MAX, c->sb.time_units_per_sec);
1570         c->vfs_sb               = sb;
1571         strlcpy(sb->s_id, c->name, sizeof(sb->s_id));
1572
1573         ret = super_setup_bdi(sb);
1574         if (ret)
1575                 goto err_put_super;
1576
1577         sb->s_bdi->ra_pages             = VM_READAHEAD_PAGES;
1578
1579         for_each_online_member(ca, c, i) {
1580                 struct block_device *bdev = ca->disk_sb.bdev;
1581
1582                 /* XXX: create an anonymous device for multi device filesystems */
1583                 sb->s_bdev      = bdev;
1584                 sb->s_dev       = bdev->bd_dev;
1585                 percpu_ref_put(&ca->io_ref);
1586                 break;
1587         }
1588
1589         c->dev = sb->s_dev;
1590
1591 #ifdef CONFIG_BCACHEFS_POSIX_ACL
1592         if (c->opts.acl)
1593                 sb->s_flags     |= SB_POSIXACL;
1594 #endif
1595
1596         vinode = bch2_vfs_inode_get(c, BCACHEFS_ROOT_INO);
1597         if (IS_ERR(vinode)) {
1598                 bch_err(c, "error mounting: error getting root inode %i",
1599                         (int) PTR_ERR(vinode));
1600                 ret = PTR_ERR(vinode);
1601                 goto err_put_super;
1602         }
1603
1604         sb->s_root = d_make_root(vinode);
1605         if (!sb->s_root) {
1606                 bch_err(c, "error mounting: error allocating root dentry");
1607                 ret = -ENOMEM;
1608                 goto err_put_super;
1609         }
1610
1611         sb->s_flags |= SB_ACTIVE;
1612 out:
1613         return dget(sb->s_root);
1614
1615 err_put_super:
1616         deactivate_locked_super(sb);
1617         return ERR_PTR(ret);
1618 }
1619
1620 static void bch2_kill_sb(struct super_block *sb)
1621 {
1622         struct bch_fs *c = sb->s_fs_info;
1623
1624         generic_shutdown_super(sb);
1625         bch2_fs_free(c);
1626 }
1627
1628 static struct file_system_type bcache_fs_type = {
1629         .owner          = THIS_MODULE,
1630         .name           = "bcachefs",
1631         .mount          = bch2_mount,
1632         .kill_sb        = bch2_kill_sb,
1633         .fs_flags       = FS_REQUIRES_DEV,
1634 };
1635
1636 MODULE_ALIAS_FS("bcachefs");
1637
1638 void bch2_vfs_exit(void)
1639 {
1640         unregister_filesystem(&bcache_fs_type);
1641         if (bch2_inode_cache)
1642                 kmem_cache_destroy(bch2_inode_cache);
1643 }
1644
1645 int __init bch2_vfs_init(void)
1646 {
1647         int ret = -ENOMEM;
1648
1649         bch2_inode_cache = KMEM_CACHE(bch_inode_info, 0);
1650         if (!bch2_inode_cache)
1651                 goto err;
1652
1653         ret = register_filesystem(&bcache_fs_type);
1654         if (ret)
1655                 goto err;
1656
1657         return 0;
1658 err:
1659         bch2_vfs_exit();
1660         return ret;
1661 }
1662
1663 #endif /* NO_BCACHEFS_FS */