]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcache/fs.c
cmd_migrate
[bcachefs-tools-debian] / libbcache / fs.c
1
2 #include "bcache.h"
3 #include "acl.h"
4 #include "btree_update.h"
5 #include "buckets.h"
6 #include "chardev.h"
7 #include "dirent.h"
8 #include "extents.h"
9 #include "fs.h"
10 #include "fs-gc.h"
11 #include "fs-io.h"
12 #include "inode.h"
13 #include "journal.h"
14 #include "keylist.h"
15 #include "super.h"
16 #include "xattr.h"
17
18 #include <linux/aio.h>
19 #include <linux/backing-dev.h>
20 #include <linux/compat.h>
21 #include <linux/module.h>
22 #include <linux/mount.h>
23 #include <linux/random.h>
24 #include <linux/statfs.h>
25 #include <linux/xattr.h>
26
27 static struct kmem_cache *bch_inode_cache;
28
29 static void bch_vfs_inode_init(struct cache_set *,
30                                struct bch_inode_info *,
31                                struct bch_inode_unpacked *);
32
33 /*
34  * I_SIZE_DIRTY requires special handling:
35  *
36  * To the recovery code, the flag means that there is stale data past i_size
37  * that needs to be deleted; it's used for implementing atomic appends and
38  * truncates.
39  *
40  * On append, we set I_SIZE_DIRTY before doing the write, then after the write
41  * we clear I_SIZE_DIRTY atomically with updating i_size to the new larger size
42  * that exposes the data we just wrote.
43  *
44  * On truncate, it's the reverse: We set I_SIZE_DIRTY atomically with setting
45  * i_size to the new smaller size, then we delete the data that we just made
46  * invisible, and then we clear I_SIZE_DIRTY.
47  *
48  * Because there can be multiple appends in flight at a time, we need a refcount
49  * (i_size_dirty_count) instead of manipulating the flag directly. Nonzero
50  * refcount means I_SIZE_DIRTY is set, zero means it's cleared.
51  *
52  * Because write_inode() can be called at any time, i_size_dirty_count means
53  * something different to the runtime code - it means to write_inode() "don't
54  * update i_size yet".
55  *
56  * We don't clear I_SIZE_DIRTY directly, we let write_inode() clear it when
57  * i_size_dirty_count is zero - but the reverse is not true, I_SIZE_DIRTY must
58  * be set explicitly.
59  */
60
61 int __must_check __bch_write_inode(struct cache_set *c,
62                                    struct bch_inode_info *ei,
63                                    inode_set_fn set,
64                                    void *p)
65 {
66         struct btree_iter iter;
67         struct inode *inode = &ei->vfs_inode;
68         struct bch_inode_unpacked inode_u;
69         struct bkey_inode_buf inode_p;
70         u64 inum = inode->i_ino;
71         unsigned i_nlink = READ_ONCE(inode->i_nlink);
72         int ret;
73
74         /*
75          * We can't write an inode with i_nlink == 0 because it's stored biased;
76          * however, we don't need to because if i_nlink is 0 the inode is
77          * getting deleted when it's evicted.
78          */
79         if (!i_nlink)
80                 return 0;
81
82         lockdep_assert_held(&ei->update_lock);
83
84         bch_btree_iter_init_intent(&iter, c, BTREE_ID_INODES, POS(inum, 0));
85
86         do {
87                 struct bkey_s_c k = bch_btree_iter_peek_with_holes(&iter);
88
89                 if ((ret = btree_iter_err(k)))
90                         goto out;
91
92                 if (WARN_ONCE(k.k->type != BCH_INODE_FS,
93                               "inode %llu not found when updating", inum)) {
94                         bch_btree_iter_unlock(&iter);
95                         return -ENOENT;
96                 }
97
98                 ret = bch_inode_unpack(bkey_s_c_to_inode(k), &inode_u);
99                 if (WARN_ONCE(ret,
100                               "error %i unpacking inode %llu", ret, inum)) {
101                         ret = -ENOENT;
102                         break;
103                 }
104
105                 if (set) {
106                         ret = set(ei, &inode_u, p);
107                         if (ret)
108                                 goto out;
109                 }
110
111                 BUG_ON(i_nlink < nlink_bias(inode->i_mode));
112
113                 inode_u.i_mode  = inode->i_mode;
114                 inode_u.i_uid   = i_uid_read(inode);
115                 inode_u.i_gid   = i_gid_read(inode);
116                 inode_u.i_nlink = i_nlink - nlink_bias(inode->i_mode);
117                 inode_u.i_dev   = inode->i_rdev;
118                 inode_u.i_atime = timespec_to_bch_time(c, inode->i_atime);
119                 inode_u.i_mtime = timespec_to_bch_time(c, inode->i_mtime);
120                 inode_u.i_ctime = timespec_to_bch_time(c, inode->i_ctime);
121
122                 bch_inode_pack(&inode_p, &inode_u);
123
124                 ret = bch_btree_insert_at(c, NULL, NULL, &ei->journal_seq,
125                                 BTREE_INSERT_ATOMIC|
126                                 BTREE_INSERT_NOFAIL,
127                                 BTREE_INSERT_ENTRY(&iter, &inode_p.inode.k_i));
128         } while (ret == -EINTR);
129
130         if (!ret) {
131                 ei->i_size      = inode_u.i_size;
132                 ei->i_flags     = inode_u.i_flags;
133         }
134 out:
135         bch_btree_iter_unlock(&iter);
136
137         return ret < 0 ? ret : 0;
138 }
139
140 int __must_check bch_write_inode(struct cache_set *c,
141                                  struct bch_inode_info *ei)
142 {
143         return __bch_write_inode(c, ei, NULL, NULL);
144 }
145
146 int bch_inc_nlink(struct cache_set *c, struct bch_inode_info *ei)
147 {
148         int ret;
149
150         mutex_lock(&ei->update_lock);
151         inc_nlink(&ei->vfs_inode);
152         ret = bch_write_inode(c, ei);
153         mutex_unlock(&ei->update_lock);
154
155         return ret;
156 }
157
158 int bch_dec_nlink(struct cache_set *c, struct bch_inode_info *ei)
159 {
160         int ret = 0;
161
162         mutex_lock(&ei->update_lock);
163         drop_nlink(&ei->vfs_inode);
164         ret = bch_write_inode(c, ei);
165         mutex_unlock(&ei->update_lock);
166
167         return ret;
168 }
169
170 static struct inode *bch_vfs_inode_get(struct super_block *sb, u64 inum)
171 {
172         struct cache_set *c = sb->s_fs_info;
173         struct inode *inode;
174         struct bch_inode_unpacked inode_u;
175         struct bch_inode_info *ei;
176         int ret;
177
178         pr_debug("inum %llu", inum);
179
180         inode = iget_locked(sb, inum);
181         if (unlikely(!inode))
182                 return ERR_PTR(-ENOMEM);
183         if (!(inode->i_state & I_NEW))
184                 return inode;
185
186         ret = bch_inode_find_by_inum(c, inum, &inode_u);
187         if (ret) {
188                 iget_failed(inode);
189                 return ERR_PTR(ret);
190         }
191
192         ei = to_bch_ei(inode);
193         bch_vfs_inode_init(c, ei, &inode_u);
194
195         ei->journal_seq = bch_inode_journal_seq(&c->journal, inum);
196
197         unlock_new_inode(inode);
198
199         return inode;
200 }
201
202 static struct inode *bch_vfs_inode_create(struct cache_set *c,
203                                           struct inode *parent,
204                                           umode_t mode, dev_t rdev)
205 {
206         struct inode *inode;
207         struct posix_acl *default_acl = NULL, *acl = NULL;
208         struct bch_inode_info *ei;
209         struct bch_inode_unpacked inode_u;
210         struct bkey_inode_buf inode_p;
211         int ret;
212
213         inode = new_inode(parent->i_sb);
214         if (unlikely(!inode))
215                 return ERR_PTR(-ENOMEM);
216
217         inode_init_owner(inode, parent, mode);
218
219         ret = posix_acl_create(parent, &inode->i_mode, &default_acl, &acl);
220         if (ret) {
221                 make_bad_inode(inode);
222                 goto err;
223         }
224
225         ei = to_bch_ei(inode);
226
227         bch_inode_init(c, &inode_u, i_uid_read(inode),
228                        i_gid_read(inode), inode->i_mode, rdev);
229         bch_inode_pack(&inode_p, &inode_u);
230
231         ret = bch_inode_create(c, &inode_p.inode.k_i,
232                                BLOCKDEV_INODE_MAX, 0,
233                                &c->unused_inode_hint);
234         if (unlikely(ret)) {
235                 /*
236                  * indicate to bch_evict_inode that the inode was never actually
237                  * created:
238                  */
239                 make_bad_inode(inode);
240                 goto err;
241         }
242
243         inode_u.inum = inode_p.inode.k.p.inode;
244         bch_vfs_inode_init(c, ei, &inode_u);
245
246         if (default_acl) {
247                 ret = bch_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
248                 if (unlikely(ret))
249                         goto err;
250         }
251
252         if (acl) {
253                 ret = bch_set_acl(inode, acl, ACL_TYPE_ACCESS);
254                 if (unlikely(ret))
255                         goto err;
256         }
257
258         insert_inode_hash(inode);
259         atomic_long_inc(&c->nr_inodes);
260 out:
261         posix_acl_release(default_acl);
262         posix_acl_release(acl);
263         return inode;
264 err:
265         clear_nlink(inode);
266         iput(inode);
267         inode = ERR_PTR(ret);
268         goto out;
269 }
270
271 static int bch_vfs_dirent_create(struct cache_set *c, struct inode *dir,
272                                  u8 type, const struct qstr *name,
273                                  struct inode *dst)
274 {
275         struct bch_inode_info *dir_ei = to_bch_ei(dir);
276         int ret;
277
278         ret = bch_dirent_create(c, dir->i_ino, &dir_ei->str_hash,
279                                 type, name, dst->i_ino,
280                                 &dir_ei->journal_seq,
281                                 BCH_HASH_SET_MUST_CREATE);
282         if (unlikely(ret))
283                 return ret;
284
285         dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb);
286         mark_inode_dirty_sync(dir);
287         return 0;
288 }
289
290 static int __bch_create(struct inode *dir, struct dentry *dentry,
291                         umode_t mode, dev_t rdev)
292 {
293         struct bch_inode_info *dir_ei = to_bch_ei(dir);
294         struct cache_set *c = dir->i_sb->s_fs_info;
295         struct inode *inode;
296         struct bch_inode_info *ei;
297         int ret;
298
299         inode = bch_vfs_inode_create(c, dir, mode, rdev);
300         if (unlikely(IS_ERR(inode)))
301                 return PTR_ERR(inode);
302
303         ei = to_bch_ei(inode);
304
305         ret = bch_vfs_dirent_create(c, dir, mode_to_type(mode),
306                                     &dentry->d_name, inode);
307         if (unlikely(ret)) {
308                 clear_nlink(inode);
309                 iput(inode);
310                 return ret;
311         }
312
313         if (dir_ei->journal_seq > ei->journal_seq)
314                 ei->journal_seq = dir_ei->journal_seq;
315
316         d_instantiate(dentry, inode);
317         return 0;
318 }
319
320 /* methods */
321
322 static struct dentry *bch_lookup(struct inode *dir, struct dentry *dentry,
323                                  unsigned int flags)
324 {
325         struct cache_set *c = dir->i_sb->s_fs_info;
326         struct bch_inode_info *dir_ei = to_bch_ei(dir);
327         struct inode *inode = NULL;
328         u64 inum;
329
330         inum = bch_dirent_lookup(c, dir->i_ino,
331                                  &dir_ei->str_hash,
332                                  &dentry->d_name);
333
334         if (inum)
335                 inode = bch_vfs_inode_get(dir->i_sb, inum);
336
337         return d_splice_alias(inode, dentry);
338 }
339
340 static int bch_create(struct inode *dir, struct dentry *dentry,
341                       umode_t mode, bool excl)
342 {
343         return __bch_create(dir, dentry, mode|S_IFREG, 0);
344 }
345
346 static int bch_link(struct dentry *old_dentry, struct inode *dir,
347                     struct dentry *dentry)
348 {
349         struct cache_set *c = dir->i_sb->s_fs_info;
350         struct inode *inode = old_dentry->d_inode;
351         struct bch_inode_info *ei = to_bch_ei(inode);
352         int ret;
353
354         lockdep_assert_held(&inode->i_rwsem);
355
356         inode->i_ctime = current_fs_time(dir->i_sb);
357
358         ret = bch_inc_nlink(c, ei);
359         if (ret)
360                 return ret;
361
362         ihold(inode);
363
364         ret = bch_vfs_dirent_create(c, dir, mode_to_type(inode->i_mode),
365                                     &dentry->d_name, inode);
366         if (unlikely(ret)) {
367                 bch_dec_nlink(c, ei);
368                 iput(inode);
369                 return ret;
370         }
371
372         d_instantiate(dentry, inode);
373         return 0;
374 }
375
376 static int bch_unlink(struct inode *dir, struct dentry *dentry)
377 {
378         struct cache_set *c = dir->i_sb->s_fs_info;
379         struct bch_inode_info *dir_ei = to_bch_ei(dir);
380         struct inode *inode = dentry->d_inode;
381         struct bch_inode_info *ei = to_bch_ei(inode);
382         int ret;
383
384         lockdep_assert_held(&inode->i_rwsem);
385
386         ret = bch_dirent_delete(c, dir->i_ino, &dir_ei->str_hash,
387                                 &dentry->d_name, &dir_ei->journal_seq);
388         if (ret)
389                 return ret;
390
391         if (dir_ei->journal_seq > ei->journal_seq)
392                 ei->journal_seq = dir_ei->journal_seq;
393
394         inode->i_ctime = dir->i_ctime;
395
396         if (S_ISDIR(inode->i_mode)) {
397                 bch_dec_nlink(c, dir_ei);
398                 drop_nlink(inode);
399         }
400
401         bch_dec_nlink(c, ei);
402
403         return 0;
404 }
405
406 static int bch_symlink(struct inode *dir, struct dentry *dentry,
407                        const char *symname)
408 {
409         struct cache_set *c = dir->i_sb->s_fs_info;
410         struct inode *inode;
411         struct bch_inode_info *ei, *dir_ei = to_bch_ei(dir);
412         int ret;
413
414         inode = bch_vfs_inode_create(c, dir, S_IFLNK|S_IRWXUGO, 0);
415         if (unlikely(IS_ERR(inode)))
416                 return PTR_ERR(inode);
417
418         ei = to_bch_ei(inode);
419
420         inode_lock(inode);
421         ret = page_symlink(inode, symname, strlen(symname) + 1);
422         inode_unlock(inode);
423
424         if (unlikely(ret))
425                 goto err;
426
427         ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
428         if (unlikely(ret))
429                 goto err;
430
431         /* XXX: racy */
432         if (dir_ei->journal_seq < ei->journal_seq)
433                 dir_ei->journal_seq = ei->journal_seq;
434
435         ret = bch_vfs_dirent_create(c, dir, DT_LNK, &dentry->d_name, inode);
436         if (unlikely(ret))
437                 goto err;
438
439         d_instantiate(dentry, inode);
440         return 0;
441 err:
442         clear_nlink(inode);
443         iput(inode);
444         return ret;
445 }
446
447 static int bch_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
448 {
449         struct cache_set *c = dir->i_sb->s_fs_info;
450         int ret;
451
452         lockdep_assert_held(&dir->i_rwsem);
453
454         ret = __bch_create(dir, dentry, mode|S_IFDIR, 0);
455         if (unlikely(ret))
456                 return ret;
457
458         bch_inc_nlink(c, to_bch_ei(dir));
459
460         return 0;
461 }
462
463 static int bch_rmdir(struct inode *dir, struct dentry *dentry)
464 {
465         struct cache_set *c = dir->i_sb->s_fs_info;
466         struct inode *inode = dentry->d_inode;
467
468         if (bch_empty_dir(c, inode->i_ino))
469                 return -ENOTEMPTY;
470
471         return bch_unlink(dir, dentry);
472 }
473
474 static int bch_mknod(struct inode *dir, struct dentry *dentry,
475                      umode_t mode, dev_t rdev)
476 {
477         return __bch_create(dir, dentry, mode, rdev);
478 }
479
480 static int bch_rename(struct inode *old_dir, struct dentry *old_dentry,
481                       struct inode *new_dir, struct dentry *new_dentry)
482 {
483         struct cache_set *c = old_dir->i_sb->s_fs_info;
484         struct inode *old_inode = old_dentry->d_inode;
485         struct bch_inode_info *ei = to_bch_ei(old_inode);
486         struct inode *new_inode = new_dentry->d_inode;
487         struct timespec now = current_fs_time(old_dir->i_sb);
488         int ret;
489
490         lockdep_assert_held(&old_dir->i_rwsem);
491         lockdep_assert_held(&new_dir->i_rwsem);
492
493         if (new_inode)
494                 filemap_write_and_wait_range(old_inode->i_mapping,
495                                              0, LLONG_MAX);
496
497         if (new_inode && S_ISDIR(old_inode->i_mode)) {
498                 lockdep_assert_held(&new_inode->i_rwsem);
499
500                 if (!S_ISDIR(new_inode->i_mode))
501                         return -ENOTDIR;
502
503                 if (bch_empty_dir(c, new_inode->i_ino))
504                         return -ENOTEMPTY;
505
506                 ret = bch_dirent_rename(c,
507                                         old_dir, &old_dentry->d_name,
508                                         new_dir, &new_dentry->d_name,
509                                         &ei->journal_seq, BCH_RENAME_OVERWRITE);
510                 if (unlikely(ret))
511                         return ret;
512
513                 clear_nlink(new_inode);
514                 bch_dec_nlink(c, to_bch_ei(old_dir));
515         } else if (new_inode) {
516                 lockdep_assert_held(&new_inode->i_rwsem);
517
518                 ret = bch_dirent_rename(c,
519                                         old_dir, &old_dentry->d_name,
520                                         new_dir, &new_dentry->d_name,
521                                         &ei->journal_seq, BCH_RENAME_OVERWRITE);
522                 if (unlikely(ret))
523                         return ret;
524
525                 new_inode->i_ctime = now;
526                 bch_dec_nlink(c, to_bch_ei(new_inode));
527         } else if (S_ISDIR(old_inode->i_mode)) {
528                 ret = bch_dirent_rename(c,
529                                         old_dir, &old_dentry->d_name,
530                                         new_dir, &new_dentry->d_name,
531                                         &ei->journal_seq, BCH_RENAME);
532                 if (unlikely(ret))
533                         return ret;
534
535                 bch_inc_nlink(c, to_bch_ei(new_dir));
536                 bch_dec_nlink(c, to_bch_ei(old_dir));
537         } else {
538                 ret = bch_dirent_rename(c,
539                                         old_dir, &old_dentry->d_name,
540                                         new_dir, &new_dentry->d_name,
541                                         &ei->journal_seq, BCH_RENAME);
542                 if (unlikely(ret))
543                         return ret;
544         }
545
546         old_dir->i_ctime = old_dir->i_mtime = now;
547         new_dir->i_ctime = new_dir->i_mtime = now;
548         mark_inode_dirty_sync(old_dir);
549         mark_inode_dirty_sync(new_dir);
550
551         old_inode->i_ctime = now;
552         mark_inode_dirty_sync(old_inode);
553
554         return 0;
555 }
556
557 static int bch_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
558                                struct inode *new_dir, struct dentry *new_dentry)
559 {
560         struct cache_set *c = old_dir->i_sb->s_fs_info;
561         struct inode *old_inode = old_dentry->d_inode;
562         struct inode *new_inode = new_dentry->d_inode;
563         struct bch_inode_info *ei = to_bch_ei(old_inode);
564         struct timespec now = current_fs_time(old_dir->i_sb);
565         int ret;
566
567         ret = bch_dirent_rename(c,
568                                 old_dir, &old_dentry->d_name,
569                                 new_dir, &new_dentry->d_name,
570                                 &ei->journal_seq, BCH_RENAME_EXCHANGE);
571         if (unlikely(ret))
572                 return ret;
573
574         if (S_ISDIR(old_inode->i_mode) !=
575             S_ISDIR(new_inode->i_mode)) {
576                 if (S_ISDIR(old_inode->i_mode)) {
577                         bch_inc_nlink(c, to_bch_ei(new_dir));
578                         bch_dec_nlink(c, to_bch_ei(old_dir));
579                 } else {
580                         bch_dec_nlink(c, to_bch_ei(new_dir));
581                         bch_inc_nlink(c, to_bch_ei(old_dir));
582                 }
583         }
584
585         old_dir->i_ctime = old_dir->i_mtime = now;
586         new_dir->i_ctime = new_dir->i_mtime = now;
587         mark_inode_dirty_sync(old_dir);
588         mark_inode_dirty_sync(new_dir);
589
590         old_inode->i_ctime = now;
591         new_inode->i_ctime = now;
592         mark_inode_dirty_sync(old_inode);
593         mark_inode_dirty_sync(new_inode);
594
595         return 0;
596 }
597
598 static int bch_rename2(struct inode *old_dir, struct dentry *old_dentry,
599                        struct inode *new_dir, struct dentry *new_dentry,
600                        unsigned flags)
601 {
602         if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
603                 return -EINVAL;
604
605         if (flags & RENAME_EXCHANGE)
606                 return bch_rename_exchange(old_dir, old_dentry,
607                                            new_dir, new_dentry);
608
609         return bch_rename(old_dir, old_dentry, new_dir, new_dentry);
610 }
611
612 static int bch_setattr(struct dentry *dentry, struct iattr *iattr)
613 {
614         struct inode *inode = dentry->d_inode;
615         struct bch_inode_info *ei = to_bch_ei(inode);
616         struct cache_set *c = inode->i_sb->s_fs_info;
617         int ret = 0;
618
619         lockdep_assert_held(&inode->i_rwsem);
620
621         pr_debug("i_size was %llu update has %llu",
622                  inode->i_size, iattr->ia_size);
623
624         ret = setattr_prepare(dentry, iattr);
625         if (ret)
626                 return ret;
627
628         if (iattr->ia_valid & ATTR_SIZE) {
629                 ret = bch_truncate(inode, iattr);
630         } else {
631                 mutex_lock(&ei->update_lock);
632                 setattr_copy(inode, iattr);
633                 ret = bch_write_inode(c, ei);
634                 mutex_unlock(&ei->update_lock);
635         }
636
637         if (unlikely(ret))
638                 return ret;
639
640         if (iattr->ia_valid & ATTR_MODE)
641                 ret = posix_acl_chmod(inode, inode->i_mode);
642
643         return ret;
644 }
645
646 static int bch_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
647 {
648         struct cache_set *c = dir->i_sb->s_fs_info;
649         struct inode *inode;
650
651         /* XXX: i_nlink should be 0? */
652         inode = bch_vfs_inode_create(c, dir, mode, 0);
653         if (unlikely(IS_ERR(inode)))
654                 return PTR_ERR(inode);
655
656         d_tmpfile(dentry, inode);
657         return 0;
658 }
659
660 static int bch_fill_extent(struct fiemap_extent_info *info,
661                            const struct bkey_i *k, unsigned flags)
662 {
663         if (bkey_extent_is_data(&k->k)) {
664                 struct bkey_s_c_extent e = bkey_i_to_s_c_extent(k);
665                 const struct bch_extent_ptr *ptr;
666                 const union bch_extent_crc *crc;
667                 int ret;
668
669                 extent_for_each_ptr_crc(e, ptr, crc) {
670                         int flags2 = 0;
671                         u64 offset = ptr->offset;
672
673                         if (crc_compression_type(crc))
674                                 flags2 |= FIEMAP_EXTENT_ENCODED;
675                         else
676                                 offset += crc_offset(crc);
677
678                         if ((offset & (PAGE_SECTORS - 1)) ||
679                             (e.k->size & (PAGE_SECTORS - 1)))
680                                 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
681
682                         ret = fiemap_fill_next_extent(info,
683                                                       bkey_start_offset(e.k) << 9,
684                                                       offset << 9,
685                                                       e.k->size << 9, flags|flags2);
686                         if (ret)
687                                 return ret;
688                 }
689
690                 return 0;
691         } else if (k->k.type == BCH_RESERVATION) {
692                 return fiemap_fill_next_extent(info,
693                                                bkey_start_offset(&k->k) << 9,
694                                                0, k->k.size << 9,
695                                                flags|
696                                                FIEMAP_EXTENT_DELALLOC|
697                                                FIEMAP_EXTENT_UNWRITTEN);
698         } else {
699                 BUG();
700         }
701 }
702
703 static int bch_fiemap(struct inode *inode, struct fiemap_extent_info *info,
704                       u64 start, u64 len)
705 {
706         struct cache_set *c = inode->i_sb->s_fs_info;
707         struct btree_iter iter;
708         struct bkey_s_c k;
709         BKEY_PADDED(k) tmp;
710         bool have_extent = false;
711         int ret = 0;
712
713         if (start + len < start)
714                 return -EINVAL;
715
716         for_each_btree_key(&iter, c, BTREE_ID_EXTENTS,
717                            POS(inode->i_ino, start >> 9), k)
718                 if (bkey_extent_is_data(k.k) ||
719                     k.k->type == BCH_RESERVATION) {
720                         if (bkey_cmp(bkey_start_pos(k.k),
721                                      POS(inode->i_ino, (start + len) >> 9)) >= 0)
722                                 break;
723
724                         if (have_extent) {
725                                 ret = bch_fill_extent(info, &tmp.k, 0);
726                                 if (ret)
727                                         goto out;
728                         }
729
730                         bkey_reassemble(&tmp.k, k);
731                         have_extent = true;
732                 }
733
734         if (have_extent)
735                 ret = bch_fill_extent(info, &tmp.k, FIEMAP_EXTENT_LAST);
736 out:
737         bch_btree_iter_unlock(&iter);
738         return ret < 0 ? ret : 0;
739 }
740
741 static const struct vm_operations_struct bch_vm_ops = {
742         .fault          = filemap_fault,
743         .map_pages      = filemap_map_pages,
744         .page_mkwrite   = bch_page_mkwrite,
745 };
746
747 static int bch_mmap(struct file *file, struct vm_area_struct *vma)
748 {
749         file_accessed(file);
750
751         vma->vm_ops = &bch_vm_ops;
752         return 0;
753 }
754
755 /* Inode flags: */
756
757 static const unsigned bch_inode_flags_to_vfs_flags_map[] = {
758         [__BCH_INODE_SYNC]      = S_SYNC,
759         [__BCH_INODE_IMMUTABLE] = S_IMMUTABLE,
760         [__BCH_INODE_APPEND]    = S_APPEND,
761         [__BCH_INODE_NOATIME]   = S_NOATIME,
762 };
763
764 static const unsigned bch_inode_flags_to_user_flags_map[] = {
765         [__BCH_INODE_SYNC]      = FS_SYNC_FL,
766         [__BCH_INODE_IMMUTABLE] = FS_IMMUTABLE_FL,
767         [__BCH_INODE_APPEND]    = FS_APPEND_FL,
768         [__BCH_INODE_NODUMP]    = FS_NODUMP_FL,
769         [__BCH_INODE_NOATIME]   = FS_NOATIME_FL,
770 };
771
772 /* Set VFS inode flags from bcache inode: */
773 static void bch_inode_flags_to_vfs(struct inode *inode)
774 {
775         unsigned i, flags = to_bch_ei(inode)->i_flags;
776
777         for (i = 0; i < ARRAY_SIZE(bch_inode_flags_to_vfs_flags_map); i++)
778                 if (flags & (1 << i))
779                         inode->i_flags |=  bch_inode_flags_to_vfs_flags_map[i];
780                 else
781                         inode->i_flags &= ~bch_inode_flags_to_vfs_flags_map[i];
782 }
783
784 /* Get FS_IOC_GETFLAGS flags from bcache inode: */
785 static unsigned bch_inode_flags_to_user_flags(unsigned flags)
786 {
787         unsigned i, ret = 0;
788
789         for (i = 0; i < ARRAY_SIZE(bch_inode_flags_to_user_flags_map); i++)
790                 if (flags & (1 << i))
791                         ret |= bch_inode_flags_to_user_flags_map[i];
792
793         return ret;
794 }
795
796 static int bch_inode_user_flags_set(struct bch_inode_info *ei,
797                                     struct bch_inode_unpacked *bi,
798                                     void *p)
799 {
800         /*
801          * We're relying on btree locking here for exclusion with other ioctl
802          * calls - use the flags in the btree (@bi), not ei->i_flags:
803          */
804         unsigned bch_flags = bi->i_flags;
805         unsigned oldflags = bch_inode_flags_to_user_flags(bch_flags);
806         unsigned newflags = *((unsigned *) p);
807         unsigned i;
808
809         if (((newflags ^ oldflags) & (FS_APPEND_FL|FS_IMMUTABLE_FL)) &&
810             !capable(CAP_LINUX_IMMUTABLE))
811                 return -EPERM;
812
813         for (i = 0; i < ARRAY_SIZE(bch_inode_flags_to_user_flags_map); i++) {
814                 if (newflags & bch_inode_flags_to_user_flags_map[i])
815                         bch_flags |=  (1 << i);
816                 else
817                         bch_flags &= ~(1 << i);
818
819                 newflags &= ~bch_inode_flags_to_user_flags_map[i];
820                 oldflags &= ~bch_inode_flags_to_user_flags_map[i];
821         }
822
823         if (oldflags != newflags)
824                 return -EOPNOTSUPP;
825
826         bi->i_flags = bch_flags;
827         ei->vfs_inode.i_ctime = current_fs_time(ei->vfs_inode.i_sb);
828
829         return 0;
830 }
831
832 #define FS_IOC_GOINGDOWN             _IOR ('X', 125, __u32)
833
834 static long bch_fs_file_ioctl(struct file *filp, unsigned int cmd,
835                               unsigned long arg)
836 {
837         struct inode *inode = file_inode(filp);
838         struct super_block *sb = inode->i_sb;
839         struct cache_set *c = sb->s_fs_info;
840         struct bch_inode_info *ei = to_bch_ei(inode);
841         unsigned flags;
842         int ret;
843
844         switch (cmd) {
845         case FS_IOC_GETFLAGS:
846                 return put_user(bch_inode_flags_to_user_flags(ei->i_flags),
847                                 (int __user *) arg);
848
849         case FS_IOC_SETFLAGS: {
850                 ret = mnt_want_write_file(filp);
851                 if (ret)
852                         return ret;
853
854                 if (!inode_owner_or_capable(inode)) {
855                         ret = -EACCES;
856                         goto setflags_out;
857                 }
858
859                 if (get_user(flags, (int __user *) arg)) {
860                         ret = -EFAULT;
861                         goto setflags_out;
862                 }
863
864                 if (!S_ISREG(inode->i_mode) &&
865                     !S_ISDIR(inode->i_mode) &&
866                     (flags & (FS_NODUMP_FL|FS_NOATIME_FL)) != flags) {
867                         ret = -EINVAL;
868                         goto setflags_out;
869                 }
870
871                 inode_lock(inode);
872
873                 mutex_lock(&ei->update_lock);
874                 ret = __bch_write_inode(c, ei, bch_inode_user_flags_set, &flags);
875                 mutex_unlock(&ei->update_lock);
876
877                 if (!ret)
878                         bch_inode_flags_to_vfs(inode);
879
880                 inode_unlock(inode);
881 setflags_out:
882                 mnt_drop_write_file(filp);
883                 return ret;
884         }
885
886         case FS_IOC_GETVERSION:
887                 return -ENOTTY;
888         case FS_IOC_SETVERSION:
889                 return -ENOTTY;
890
891         case FS_IOC_GOINGDOWN:
892                 if (!capable(CAP_SYS_ADMIN))
893                         return -EPERM;
894
895                 down_write(&sb->s_umount);
896                 sb->s_flags |= MS_RDONLY;
897                 bch_fs_emergency_read_only(c);
898                 up_write(&sb->s_umount);
899                 return 0;
900
901         default:
902                 return bch_fs_ioctl(c, cmd, (void __user *) arg);
903         }
904 }
905
906 #ifdef CONFIG_COMPAT
907 static long bch_compat_fs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
908 {
909         /* These are just misnamed, they actually get/put from/to user an int */
910         switch (cmd) {
911         case FS_IOC_GETFLAGS:
912                 cmd = FS_IOC_GETFLAGS;
913                 break;
914         case FS_IOC32_SETFLAGS:
915                 cmd = FS_IOC_SETFLAGS;
916                 break;
917         default:
918                 return -ENOIOCTLCMD;
919         }
920         return bch_fs_file_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
921 }
922 #endif
923
924 /* Directories: */
925
926 static loff_t bch_dir_llseek(struct file *file, loff_t offset, int whence)
927 {
928         return generic_file_llseek_size(file, offset, whence,
929                                         S64_MAX, S64_MAX);
930 }
931
932 static int bch_vfs_readdir(struct file *file, struct dir_context *ctx)
933 {
934         struct inode *inode = file_inode(file);
935         struct cache_set *c = inode->i_sb->s_fs_info;
936
937         return bch_readdir(c, file, ctx);
938 }
939
940 static const struct file_operations bch_file_operations = {
941         .llseek         = bch_llseek,
942         .read_iter      = generic_file_read_iter,
943         .write_iter     = bch_write_iter,
944         .mmap           = bch_mmap,
945         .open           = generic_file_open,
946         .fsync          = bch_fsync,
947         .splice_read    = generic_file_splice_read,
948         .splice_write   = iter_file_splice_write,
949         .fallocate      = bch_fallocate_dispatch,
950         .unlocked_ioctl = bch_fs_file_ioctl,
951 #ifdef CONFIG_COMPAT
952         .compat_ioctl   = bch_compat_fs_ioctl,
953 #endif
954 };
955
956 static const struct inode_operations bch_file_inode_operations = {
957         .setattr        = bch_setattr,
958         .fiemap         = bch_fiemap,
959         .listxattr      = bch_xattr_list,
960         .get_acl        = bch_get_acl,
961         .set_acl        = bch_set_acl,
962 };
963
964 static const struct inode_operations bch_dir_inode_operations = {
965         .lookup         = bch_lookup,
966         .create         = bch_create,
967         .link           = bch_link,
968         .unlink         = bch_unlink,
969         .symlink        = bch_symlink,
970         .mkdir          = bch_mkdir,
971         .rmdir          = bch_rmdir,
972         .mknod          = bch_mknod,
973         .rename         = bch_rename2,
974         .setattr        = bch_setattr,
975         .tmpfile        = bch_tmpfile,
976         .listxattr      = bch_xattr_list,
977         .get_acl        = bch_get_acl,
978         .set_acl        = bch_set_acl,
979 };
980
981 static const struct file_operations bch_dir_file_operations = {
982         .llseek         = bch_dir_llseek,
983         .read           = generic_read_dir,
984         .iterate        = bch_vfs_readdir,
985         .fsync          = bch_fsync,
986         .unlocked_ioctl = bch_fs_file_ioctl,
987 #ifdef CONFIG_COMPAT
988         .compat_ioctl   = bch_compat_fs_ioctl,
989 #endif
990 };
991
992 static const struct inode_operations bch_symlink_inode_operations = {
993         .readlink       = generic_readlink,
994         .get_link       = page_get_link,
995         .setattr        = bch_setattr,
996         .listxattr      = bch_xattr_list,
997         .get_acl        = bch_get_acl,
998         .set_acl        = bch_set_acl,
999 };
1000
1001 static const struct inode_operations bch_special_inode_operations = {
1002         .setattr        = bch_setattr,
1003         .listxattr      = bch_xattr_list,
1004         .get_acl        = bch_get_acl,
1005         .set_acl        = bch_set_acl,
1006 };
1007
1008 static const struct address_space_operations bch_address_space_operations = {
1009         .writepage      = bch_writepage,
1010         .readpage       = bch_readpage,
1011         .writepages     = bch_writepages,
1012         .readpages      = bch_readpages,
1013         .set_page_dirty = bch_set_page_dirty,
1014         .write_begin    = bch_write_begin,
1015         .write_end      = bch_write_end,
1016         .invalidatepage = bch_invalidatepage,
1017         .releasepage    = bch_releasepage,
1018         .direct_IO      = bch_direct_IO,
1019 #ifdef CONFIG_MIGRATION
1020         .migratepage    = bch_migrate_page,
1021 #endif
1022         .error_remove_page = generic_error_remove_page,
1023 };
1024
1025 static void bch_vfs_inode_init(struct cache_set *c,
1026                                struct bch_inode_info *ei,
1027                                struct bch_inode_unpacked *bi)
1028 {
1029         struct inode *inode = &ei->vfs_inode;
1030
1031         pr_debug("init inode %llu with mode %o",
1032                  bi->inum, bi->i_mode);
1033
1034         ei->i_flags     = bi->i_flags;
1035         ei->i_size      = bi->i_size;
1036
1037         inode->i_mode   = bi->i_mode;
1038         i_uid_write(inode, bi->i_uid);
1039         i_gid_write(inode, bi->i_gid);
1040
1041         atomic64_set(&ei->i_sectors, bi->i_sectors);
1042         inode->i_blocks = bi->i_sectors;
1043
1044         inode->i_ino    = bi->inum;
1045         set_nlink(inode, bi->i_nlink + nlink_bias(inode->i_mode));
1046         inode->i_rdev   = bi->i_dev;
1047         inode->i_generation = bi->i_generation;
1048         inode->i_size   = bi->i_size;
1049         inode->i_atime  = bch_time_to_timespec(c, bi->i_atime);
1050         inode->i_mtime  = bch_time_to_timespec(c, bi->i_mtime);
1051         inode->i_ctime  = bch_time_to_timespec(c, bi->i_ctime);
1052         bch_inode_flags_to_vfs(inode);
1053
1054         ei->str_hash = bch_hash_info_init(bi);
1055
1056         inode->i_mapping->a_ops = &bch_address_space_operations;
1057
1058         switch (inode->i_mode & S_IFMT) {
1059         case S_IFREG:
1060                 inode->i_op = &bch_file_inode_operations;
1061                 inode->i_fop = &bch_file_operations;
1062                 break;
1063         case S_IFDIR:
1064                 inode->i_op = &bch_dir_inode_operations;
1065                 inode->i_fop = &bch_dir_file_operations;
1066                 break;
1067         case S_IFLNK:
1068                 inode_nohighmem(inode);
1069                 inode->i_op = &bch_symlink_inode_operations;
1070                 break;
1071         default:
1072                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1073                 inode->i_op = &bch_special_inode_operations;
1074                 break;
1075         }
1076 }
1077
1078 static struct inode *bch_alloc_inode(struct super_block *sb)
1079 {
1080         struct bch_inode_info *ei;
1081
1082         ei = kmem_cache_alloc(bch_inode_cache, GFP_NOFS);
1083         if (!ei)
1084                 return NULL;
1085
1086         pr_debug("allocated %p", &ei->vfs_inode);
1087
1088         inode_init_once(&ei->vfs_inode);
1089         mutex_init(&ei->update_lock);
1090         ei->journal_seq = 0;
1091         atomic_long_set(&ei->i_size_dirty_count, 0);
1092         atomic_long_set(&ei->i_sectors_dirty_count, 0);
1093
1094         return &ei->vfs_inode;
1095 }
1096
1097 static void bch_i_callback(struct rcu_head *head)
1098 {
1099         struct inode *inode = container_of(head, struct inode, i_rcu);
1100
1101         kmem_cache_free(bch_inode_cache, to_bch_ei(inode));
1102 }
1103
1104 static void bch_destroy_inode(struct inode *inode)
1105 {
1106         call_rcu(&inode->i_rcu, bch_i_callback);
1107 }
1108
1109 static int bch_vfs_write_inode(struct inode *inode,
1110                                struct writeback_control *wbc)
1111 {
1112         struct cache_set *c = inode->i_sb->s_fs_info;
1113         struct bch_inode_info *ei = to_bch_ei(inode);
1114         int ret;
1115
1116         mutex_lock(&ei->update_lock);
1117         ret = bch_write_inode(c, ei);
1118         mutex_unlock(&ei->update_lock);
1119
1120         if (c->opts.journal_flush_disabled)
1121                 return ret;
1122
1123         if (!ret && wbc->sync_mode == WB_SYNC_ALL)
1124                 ret = bch_journal_flush_seq(&c->journal, ei->journal_seq);
1125
1126         return ret;
1127 }
1128
1129 static void bch_evict_inode(struct inode *inode)
1130 {
1131         struct cache_set *c = inode->i_sb->s_fs_info;
1132
1133         truncate_inode_pages_final(&inode->i_data);
1134
1135         if (!bch_journal_error(&c->journal) && !is_bad_inode(inode)) {
1136                 struct bch_inode_info *ei = to_bch_ei(inode);
1137
1138                 /* XXX - we want to check this stuff iff there weren't IO errors: */
1139                 BUG_ON(atomic_long_read(&ei->i_sectors_dirty_count));
1140                 BUG_ON(atomic64_read(&ei->i_sectors) != inode->i_blocks);
1141         }
1142
1143         clear_inode(inode);
1144
1145         if (!inode->i_nlink && !is_bad_inode(inode)) {
1146                 bch_inode_rm(c, inode->i_ino);
1147                 atomic_long_dec(&c->nr_inodes);
1148         }
1149 }
1150
1151 static int bch_statfs(struct dentry *dentry, struct kstatfs *buf)
1152 {
1153         struct super_block *sb = dentry->d_sb;
1154         struct cache_set *c = sb->s_fs_info;
1155         u64 fsid;
1156
1157         buf->f_type     = BCACHE_STATFS_MAGIC;
1158         buf->f_bsize    = sb->s_blocksize;
1159         buf->f_blocks   = c->capacity >> PAGE_SECTOR_SHIFT;
1160         buf->f_bfree    = (c->capacity - bch_fs_sectors_used(c)) >> PAGE_SECTOR_SHIFT;
1161         buf->f_bavail   = buf->f_bfree;
1162         buf->f_files    = atomic_long_read(&c->nr_inodes);
1163         buf->f_ffree    = U64_MAX;
1164
1165         fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
1166                le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
1167         buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1168         buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1169         buf->f_namelen  = NAME_MAX;
1170
1171         return 0;
1172 }
1173
1174 static int bch_sync_fs(struct super_block *sb, int wait)
1175 {
1176         struct cache_set *c = sb->s_fs_info;
1177
1178         if (!wait) {
1179                 bch_journal_flush_async(&c->journal, NULL);
1180                 return 0;
1181         }
1182
1183         return bch_journal_flush(&c->journal);
1184 }
1185
1186 static struct cache_set *bdev_to_cache_set(struct block_device *bdev)
1187 {
1188         struct cache_set *c;
1189         struct cache *ca;
1190         unsigned i;
1191
1192         rcu_read_lock();
1193
1194         list_for_each_entry(c, &bch_fs_list, list)
1195                 for_each_cache_rcu(ca, c, i)
1196                         if (ca->disk_sb.bdev == bdev) {
1197                                 rcu_read_unlock();
1198                                 return c;
1199                         }
1200
1201         rcu_read_unlock();
1202
1203         return NULL;
1204 }
1205
1206 static struct cache_set *bch_open_as_blockdevs(const char *_dev_name,
1207                                                struct bch_opts opts)
1208 {
1209         size_t nr_devs = 0, i = 0;
1210         char *dev_name, *s, **devs;
1211         struct cache_set *c = NULL;
1212         const char *err;
1213
1214         dev_name = kstrdup(_dev_name, GFP_KERNEL);
1215         if (!dev_name)
1216                 return NULL;
1217
1218         for (s = dev_name; s; s = strchr(s + 1, ':'))
1219                 nr_devs++;
1220
1221         devs = kcalloc(nr_devs, sizeof(const char *), GFP_KERNEL);
1222         if (!devs)
1223                 goto err;
1224
1225         for (i = 0, s = dev_name;
1226              s;
1227              (s = strchr(s, ':')) && (*s++ = '\0'))
1228                 devs[i++] = s;
1229
1230         err = bch_fs_open(devs, nr_devs, opts, &c);
1231         if (err) {
1232                 /*
1233                  * Already open?
1234                  * Look up each block device, make sure they all belong to a
1235                  * cache set and they all belong to the _same_ cache set
1236                  */
1237
1238                 mutex_lock(&bch_register_lock);
1239
1240                 for (i = 0; i < nr_devs; i++) {
1241                         struct block_device *bdev = lookup_bdev(devs[i]);
1242                         struct cache_set *c2;
1243
1244                         if (IS_ERR(bdev))
1245                                 goto err_unlock;
1246
1247                         c2 = bdev_to_cache_set(bdev);
1248                         bdput(bdev);
1249
1250                         if (!c)
1251                                 c = c2;
1252
1253                         if (c != c2)
1254                                 goto err_unlock;
1255                 }
1256
1257                 if (!c)
1258                         goto err_unlock;
1259
1260                 mutex_lock(&c->state_lock);
1261
1262                 if (!bch_fs_running(c)) {
1263                         mutex_unlock(&c->state_lock);
1264                         err = "incomplete cache set";
1265                         c = NULL;
1266                         goto err_unlock;
1267                 }
1268
1269                 closure_get(&c->cl);
1270                 mutex_unlock(&c->state_lock);
1271                 mutex_unlock(&bch_register_lock);
1272         }
1273
1274         set_bit(BCH_FS_BDEV_MOUNTED, &c->flags);
1275 err:
1276         kfree(devs);
1277         kfree(dev_name);
1278
1279         return c;
1280 err_unlock:
1281         mutex_unlock(&bch_register_lock);
1282         pr_err("register_cache_set err %s", err);
1283         goto err;
1284 }
1285
1286 static int bch_remount(struct super_block *sb, int *flags, char *data)
1287 {
1288         struct cache_set *c = sb->s_fs_info;
1289         struct bch_opts opts = bch_opts_empty();
1290         int ret;
1291
1292         opts.read_only = (*flags & MS_RDONLY) != 0;
1293
1294         ret = bch_parse_mount_opts(&opts, data);
1295         if (ret)
1296                 return ret;
1297
1298         if (opts.read_only >= 0 &&
1299             opts.read_only != c->opts.read_only) {
1300                 const char *err = NULL;
1301
1302                 if (opts.read_only) {
1303                         bch_fs_read_only(c);
1304
1305                         sb->s_flags |= MS_RDONLY;
1306                 } else {
1307                         err = bch_fs_read_write(c);
1308                         if (err) {
1309                                 bch_err(c, "error going rw: %s", err);
1310                                 return -EINVAL;
1311                         }
1312
1313                         sb->s_flags &= ~MS_RDONLY;
1314                 }
1315
1316                 c->opts.read_only = opts.read_only;
1317         }
1318
1319         if (opts.errors >= 0)
1320                 c->opts.errors = opts.errors;
1321
1322         return ret;
1323 }
1324
1325 static const struct super_operations bch_super_operations = {
1326         .alloc_inode    = bch_alloc_inode,
1327         .destroy_inode  = bch_destroy_inode,
1328         .write_inode    = bch_vfs_write_inode,
1329         .evict_inode    = bch_evict_inode,
1330         .sync_fs        = bch_sync_fs,
1331         .statfs         = bch_statfs,
1332         .show_options   = generic_show_options,
1333         .remount_fs     = bch_remount,
1334 #if 0
1335         .put_super      = bch_put_super,
1336         .freeze_fs      = bch_freeze,
1337         .unfreeze_fs    = bch_unfreeze,
1338 #endif
1339 };
1340
1341 static int bch_test_super(struct super_block *s, void *data)
1342 {
1343         return s->s_fs_info == data;
1344 }
1345
1346 static int bch_set_super(struct super_block *s, void *data)
1347 {
1348         s->s_fs_info = data;
1349         return 0;
1350 }
1351
1352 static struct dentry *bch_mount(struct file_system_type *fs_type,
1353                                 int flags, const char *dev_name, void *data)
1354 {
1355         struct cache_set *c;
1356         struct cache *ca;
1357         struct super_block *sb;
1358         struct inode *inode;
1359         struct bch_opts opts = bch_opts_empty();
1360         unsigned i;
1361         int ret;
1362
1363         opts.read_only = (flags & MS_RDONLY) != 0;
1364
1365         ret = bch_parse_mount_opts(&opts, data);
1366         if (ret)
1367                 return ERR_PTR(ret);
1368
1369         c = bch_open_as_blockdevs(dev_name, opts);
1370         if (!c)
1371                 return ERR_PTR(-ENOENT);
1372
1373         sb = sget(fs_type, bch_test_super, bch_set_super, flags|MS_NOSEC, c);
1374         if (IS_ERR(sb)) {
1375                 closure_put(&c->cl);
1376                 return ERR_CAST(sb);
1377         }
1378
1379         BUG_ON(sb->s_fs_info != c);
1380
1381         if (sb->s_root) {
1382                 closure_put(&c->cl);
1383
1384                 if ((flags ^ sb->s_flags) & MS_RDONLY) {
1385                         ret = -EBUSY;
1386                         goto err_put_super;
1387                 }
1388                 goto out;
1389         }
1390
1391         /* XXX: blocksize */
1392         sb->s_blocksize         = PAGE_SIZE;
1393         sb->s_blocksize_bits    = PAGE_SHIFT;
1394         sb->s_maxbytes          = MAX_LFS_FILESIZE;
1395         sb->s_op                = &bch_super_operations;
1396         sb->s_xattr             = bch_xattr_handlers;
1397         sb->s_magic             = BCACHE_STATFS_MAGIC;
1398         sb->s_time_gran         = c->sb.time_precision;
1399         c->vfs_sb               = sb;
1400         sb->s_bdi               = &c->bdi;
1401
1402         rcu_read_lock();
1403         for_each_cache_rcu(ca, c, i) {
1404                 struct block_device *bdev = ca->disk_sb.bdev;
1405
1406                 BUILD_BUG_ON(sizeof(sb->s_id) < BDEVNAME_SIZE);
1407
1408                 bdevname(bdev, sb->s_id);
1409
1410                 /* XXX: do we even need s_bdev? */
1411                 sb->s_bdev      = bdev;
1412                 sb->s_dev       = bdev->bd_dev;
1413                 break;
1414         }
1415         rcu_read_unlock();
1416
1417         if (opts.posix_acl < 0)
1418                 sb->s_flags     |= MS_POSIXACL;
1419         else
1420                 sb->s_flags     |= opts.posix_acl ? MS_POSIXACL : 0;
1421
1422         inode = bch_vfs_inode_get(sb, BCACHE_ROOT_INO);
1423         if (IS_ERR(inode)) {
1424                 ret = PTR_ERR(inode);
1425                 goto err_put_super;
1426         }
1427
1428         sb->s_root = d_make_root(inode);
1429         if (!sb->s_root) {
1430                 ret = -ENOMEM;
1431                 goto err_put_super;
1432         }
1433
1434         sb->s_flags |= MS_ACTIVE;
1435 out:
1436         return dget(sb->s_root);
1437
1438 err_put_super:
1439         deactivate_locked_super(sb);
1440         return ERR_PTR(ret);
1441 }
1442
1443 static void bch_kill_sb(struct super_block *sb)
1444 {
1445         struct cache_set *c = sb->s_fs_info;
1446
1447         generic_shutdown_super(sb);
1448
1449         if (test_bit(BCH_FS_BDEV_MOUNTED, &c->flags))
1450                 bch_fs_stop(c);
1451         else
1452                 closure_put(&c->cl);
1453 }
1454
1455 static struct file_system_type bcache_fs_type = {
1456         .owner          = THIS_MODULE,
1457         .name           = "bcache",
1458         .mount          = bch_mount,
1459         .kill_sb        = bch_kill_sb,
1460         .fs_flags       = FS_REQUIRES_DEV,
1461 };
1462
1463 MODULE_ALIAS_FS("bcache");
1464
1465 void bch_vfs_exit(void)
1466 {
1467         unregister_filesystem(&bcache_fs_type);
1468         if (bch_dio_write_bioset)
1469                 bioset_free(bch_dio_write_bioset);
1470         if (bch_dio_read_bioset)
1471                 bioset_free(bch_dio_read_bioset);
1472         if (bch_writepage_bioset)
1473                 bioset_free(bch_writepage_bioset);
1474         if (bch_inode_cache)
1475                 kmem_cache_destroy(bch_inode_cache);
1476 }
1477
1478 int __init bch_vfs_init(void)
1479 {
1480         int ret = -ENOMEM;
1481
1482         bch_inode_cache = KMEM_CACHE(bch_inode_info, 0);
1483         if (!bch_inode_cache)
1484                 goto err;
1485
1486         bch_writepage_bioset =
1487                 bioset_create(4, offsetof(struct bch_writepage_io, bio.bio));
1488         if (!bch_writepage_bioset)
1489                 goto err;
1490
1491         bch_dio_read_bioset = bioset_create(4, offsetof(struct dio_read, rbio.bio));
1492         if (!bch_dio_read_bioset)
1493                 goto err;
1494
1495         bch_dio_write_bioset = bioset_create(4, offsetof(struct dio_write, bio.bio));
1496         if (!bch_dio_write_bioset)
1497                 goto err;
1498
1499         ret = register_filesystem(&bcache_fs_type);
1500         if (ret)
1501                 goto err;
1502
1503         return 0;
1504 err:
1505         bch_vfs_exit();
1506         return ret;
1507 }