]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcache/fs.c
76948e79d33598d326924726a2aa1ab734a63ce7
[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_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
835 {
836         struct inode *inode = file_inode(filp);
837         struct super_block *sb = inode->i_sb;
838         struct cache_set *c = sb->s_fs_info;
839         struct bch_inode_info *ei = to_bch_ei(inode);
840         unsigned flags;
841         int ret;
842
843         switch (cmd) {
844         case FS_IOC_GETFLAGS:
845                 return put_user(bch_inode_flags_to_user_flags(ei->i_flags),
846                                 (int __user *) arg);
847
848         case FS_IOC_SETFLAGS: {
849                 ret = mnt_want_write_file(filp);
850                 if (ret)
851                         return ret;
852
853                 if (!inode_owner_or_capable(inode)) {
854                         ret = -EACCES;
855                         goto setflags_out;
856                 }
857
858                 if (get_user(flags, (int __user *) arg)) {
859                         ret = -EFAULT;
860                         goto setflags_out;
861                 }
862
863                 if (!S_ISREG(inode->i_mode) &&
864                     !S_ISDIR(inode->i_mode) &&
865                     (flags & (FS_NODUMP_FL|FS_NOATIME_FL)) != flags) {
866                         ret = -EINVAL;
867                         goto setflags_out;
868                 }
869
870                 inode_lock(inode);
871
872                 mutex_lock(&ei->update_lock);
873                 ret = __bch_write_inode(c, ei, bch_inode_user_flags_set, &flags);
874                 mutex_unlock(&ei->update_lock);
875
876                 if (!ret)
877                         bch_inode_flags_to_vfs(inode);
878
879                 inode_unlock(inode);
880 setflags_out:
881                 mnt_drop_write_file(filp);
882                 return ret;
883         }
884
885         case FS_IOC_GETVERSION:
886                 return -ENOTTY;
887         case FS_IOC_SETVERSION:
888                 return -ENOTTY;
889
890         case FS_IOC_GOINGDOWN:
891                 if (!capable(CAP_SYS_ADMIN))
892                         return -EPERM;
893
894                 down_write(&sb->s_umount);
895                 sb->s_flags |= MS_RDONLY;
896                 bch_cache_set_emergency_read_only(c);
897                 up_write(&sb->s_umount);
898                 return 0;
899
900         default:
901                 return bch_cache_set_ioctl(c, cmd, (void __user *) arg);
902         }
903 }
904
905 #ifdef CONFIG_COMPAT
906 static long bch_compat_fs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
907 {
908         /* These are just misnamed, they actually get/put from/to user an int */
909         switch (cmd) {
910         case FS_IOC_GETFLAGS:
911                 cmd = FS_IOC_GETFLAGS;
912                 break;
913         case FS_IOC32_SETFLAGS:
914                 cmd = FS_IOC_SETFLAGS;
915                 break;
916         default:
917                 return -ENOIOCTLCMD;
918         }
919         return bch_fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
920 }
921 #endif
922
923 /* Directories: */
924
925 static loff_t bch_dir_llseek(struct file *file, loff_t offset, int whence)
926 {
927         return generic_file_llseek_size(file, offset, whence,
928                                         S64_MAX, S64_MAX);
929 }
930
931 static int bch_vfs_readdir(struct file *file, struct dir_context *ctx)
932 {
933         struct inode *inode = file_inode(file);
934         struct cache_set *c = inode->i_sb->s_fs_info;
935
936         return bch_readdir(c, file, ctx);
937 }
938
939 static const struct file_operations bch_file_operations = {
940         .llseek         = bch_llseek,
941         .read_iter      = generic_file_read_iter,
942         .write_iter     = bch_write_iter,
943         .mmap           = bch_mmap,
944         .open           = generic_file_open,
945         .fsync          = bch_fsync,
946         .splice_read    = generic_file_splice_read,
947         .splice_write   = iter_file_splice_write,
948         .fallocate      = bch_fallocate_dispatch,
949         .unlocked_ioctl = bch_fs_ioctl,
950 #ifdef CONFIG_COMPAT
951         .compat_ioctl   = bch_compat_fs_ioctl,
952 #endif
953 };
954
955 static const struct inode_operations bch_file_inode_operations = {
956         .setattr        = bch_setattr,
957         .fiemap         = bch_fiemap,
958         .listxattr      = bch_xattr_list,
959         .get_acl        = bch_get_acl,
960         .set_acl        = bch_set_acl,
961 };
962
963 static const struct inode_operations bch_dir_inode_operations = {
964         .lookup         = bch_lookup,
965         .create         = bch_create,
966         .link           = bch_link,
967         .unlink         = bch_unlink,
968         .symlink        = bch_symlink,
969         .mkdir          = bch_mkdir,
970         .rmdir          = bch_rmdir,
971         .mknod          = bch_mknod,
972         .rename         = bch_rename2,
973         .setattr        = bch_setattr,
974         .tmpfile        = bch_tmpfile,
975         .listxattr      = bch_xattr_list,
976         .get_acl        = bch_get_acl,
977         .set_acl        = bch_set_acl,
978 };
979
980 static const struct file_operations bch_dir_file_operations = {
981         .llseek         = bch_dir_llseek,
982         .read           = generic_read_dir,
983         .iterate        = bch_vfs_readdir,
984         .fsync          = bch_fsync,
985         .unlocked_ioctl = bch_fs_ioctl,
986 #ifdef CONFIG_COMPAT
987         .compat_ioctl   = bch_compat_fs_ioctl,
988 #endif
989 };
990
991 static const struct inode_operations bch_symlink_inode_operations = {
992         .readlink       = generic_readlink,
993         .get_link       = page_get_link,
994         .setattr        = bch_setattr,
995         .listxattr      = bch_xattr_list,
996         .get_acl        = bch_get_acl,
997         .set_acl        = bch_set_acl,
998 };
999
1000 static const struct inode_operations bch_special_inode_operations = {
1001         .setattr        = bch_setattr,
1002         .listxattr      = bch_xattr_list,
1003         .get_acl        = bch_get_acl,
1004         .set_acl        = bch_set_acl,
1005 };
1006
1007 static const struct address_space_operations bch_address_space_operations = {
1008         .writepage      = bch_writepage,
1009         .readpage       = bch_readpage,
1010         .writepages     = bch_writepages,
1011         .readpages      = bch_readpages,
1012         .set_page_dirty = bch_set_page_dirty,
1013         .write_begin    = bch_write_begin,
1014         .write_end      = bch_write_end,
1015         .invalidatepage = bch_invalidatepage,
1016         .releasepage    = bch_releasepage,
1017         .direct_IO      = bch_direct_IO,
1018 #ifdef CONFIG_MIGRATION
1019         .migratepage    = bch_migrate_page,
1020 #endif
1021         .error_remove_page = generic_error_remove_page,
1022 };
1023
1024 static void bch_vfs_inode_init(struct cache_set *c,
1025                                struct bch_inode_info *ei,
1026                                struct bch_inode_unpacked *bi)
1027 {
1028         struct inode *inode = &ei->vfs_inode;
1029
1030         pr_debug("init inode %llu with mode %o",
1031                  bi->inum, bi->i_mode);
1032
1033         ei->i_flags     = bi->i_flags;
1034         ei->i_size      = bi->i_size;
1035
1036         inode->i_mode   = bi->i_mode;
1037         i_uid_write(inode, bi->i_uid);
1038         i_gid_write(inode, bi->i_gid);
1039
1040         atomic64_set(&ei->i_sectors, bi->i_sectors);
1041         inode->i_blocks = bi->i_sectors;
1042
1043         inode->i_ino    = bi->inum;
1044         set_nlink(inode, bi->i_nlink + nlink_bias(inode->i_mode));
1045         inode->i_rdev   = bi->i_dev;
1046         inode->i_generation = bi->i_generation;
1047         inode->i_size   = bi->i_size;
1048         inode->i_atime  = bch_time_to_timespec(c, bi->i_atime);
1049         inode->i_mtime  = bch_time_to_timespec(c, bi->i_mtime);
1050         inode->i_ctime  = bch_time_to_timespec(c, bi->i_ctime);
1051         bch_inode_flags_to_vfs(inode);
1052
1053         ei->str_hash = bch_hash_info_init(bi);
1054
1055         inode->i_mapping->a_ops = &bch_address_space_operations;
1056
1057         switch (inode->i_mode & S_IFMT) {
1058         case S_IFREG:
1059                 inode->i_op = &bch_file_inode_operations;
1060                 inode->i_fop = &bch_file_operations;
1061                 break;
1062         case S_IFDIR:
1063                 inode->i_op = &bch_dir_inode_operations;
1064                 inode->i_fop = &bch_dir_file_operations;
1065                 break;
1066         case S_IFLNK:
1067                 inode_nohighmem(inode);
1068                 inode->i_op = &bch_symlink_inode_operations;
1069                 break;
1070         default:
1071                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1072                 inode->i_op = &bch_special_inode_operations;
1073                 break;
1074         }
1075 }
1076
1077 static struct inode *bch_alloc_inode(struct super_block *sb)
1078 {
1079         struct bch_inode_info *ei;
1080
1081         ei = kmem_cache_alloc(bch_inode_cache, GFP_NOFS);
1082         if (!ei)
1083                 return NULL;
1084
1085         pr_debug("allocated %p", &ei->vfs_inode);
1086
1087         inode_init_once(&ei->vfs_inode);
1088         mutex_init(&ei->update_lock);
1089         ei->journal_seq = 0;
1090         atomic_long_set(&ei->i_size_dirty_count, 0);
1091         atomic_long_set(&ei->i_sectors_dirty_count, 0);
1092
1093         return &ei->vfs_inode;
1094 }
1095
1096 static void bch_i_callback(struct rcu_head *head)
1097 {
1098         struct inode *inode = container_of(head, struct inode, i_rcu);
1099
1100         kmem_cache_free(bch_inode_cache, to_bch_ei(inode));
1101 }
1102
1103 static void bch_destroy_inode(struct inode *inode)
1104 {
1105         call_rcu(&inode->i_rcu, bch_i_callback);
1106 }
1107
1108 static int bch_vfs_write_inode(struct inode *inode,
1109                                struct writeback_control *wbc)
1110 {
1111         struct cache_set *c = inode->i_sb->s_fs_info;
1112         struct bch_inode_info *ei = to_bch_ei(inode);
1113         int ret;
1114
1115         mutex_lock(&ei->update_lock);
1116         ret = bch_write_inode(c, ei);
1117         mutex_unlock(&ei->update_lock);
1118
1119         if (c->opts.journal_flush_disabled)
1120                 return ret;
1121
1122         if (!ret && wbc->sync_mode == WB_SYNC_ALL)
1123                 ret = bch_journal_flush_seq(&c->journal, ei->journal_seq);
1124
1125         return ret;
1126 }
1127
1128 static void bch_evict_inode(struct inode *inode)
1129 {
1130         struct cache_set *c = inode->i_sb->s_fs_info;
1131
1132         truncate_inode_pages_final(&inode->i_data);
1133
1134         if (!bch_journal_error(&c->journal) && !is_bad_inode(inode)) {
1135                 struct bch_inode_info *ei = to_bch_ei(inode);
1136
1137                 /* XXX - we want to check this stuff iff there weren't IO errors: */
1138                 BUG_ON(atomic_long_read(&ei->i_sectors_dirty_count));
1139                 BUG_ON(atomic64_read(&ei->i_sectors) != inode->i_blocks);
1140         }
1141
1142         clear_inode(inode);
1143
1144         if (!inode->i_nlink && !is_bad_inode(inode)) {
1145                 bch_inode_rm(c, inode->i_ino);
1146                 atomic_long_dec(&c->nr_inodes);
1147         }
1148 }
1149
1150 static int bch_statfs(struct dentry *dentry, struct kstatfs *buf)
1151 {
1152         struct super_block *sb = dentry->d_sb;
1153         struct cache_set *c = sb->s_fs_info;
1154         u64 fsid;
1155
1156         buf->f_type     = BCACHE_STATFS_MAGIC;
1157         buf->f_bsize    = sb->s_blocksize;
1158         buf->f_blocks   = c->capacity >> PAGE_SECTOR_SHIFT;
1159         buf->f_bfree    = (c->capacity - cache_set_sectors_used(c)) >> PAGE_SECTOR_SHIFT;
1160         buf->f_bavail   = buf->f_bfree;
1161         buf->f_files    = atomic_long_read(&c->nr_inodes);
1162         buf->f_ffree    = U64_MAX;
1163
1164         fsid = le64_to_cpup((void *) c->sb.user_uuid.b) ^
1165                le64_to_cpup((void *) c->sb.user_uuid.b + sizeof(u64));
1166         buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
1167         buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
1168         buf->f_namelen  = NAME_MAX;
1169
1170         return 0;
1171 }
1172
1173 static int bch_sync_fs(struct super_block *sb, int wait)
1174 {
1175         struct cache_set *c = sb->s_fs_info;
1176
1177         if (!wait) {
1178                 bch_journal_flush_async(&c->journal, NULL);
1179                 return 0;
1180         }
1181
1182         return bch_journal_flush(&c->journal);
1183 }
1184
1185 static struct cache_set *bdev_to_cache_set(struct block_device *bdev)
1186 {
1187         struct cache_set *c;
1188         struct cache *ca;
1189         unsigned i;
1190
1191         rcu_read_lock();
1192
1193         list_for_each_entry(c, &bch_cache_sets, list)
1194                 for_each_cache_rcu(ca, c, i)
1195                         if (ca->disk_sb.bdev == bdev) {
1196                                 rcu_read_unlock();
1197                                 return c;
1198                         }
1199
1200         rcu_read_unlock();
1201
1202         return NULL;
1203 }
1204
1205 static struct cache_set *bch_open_as_blockdevs(const char *_dev_name,
1206                                                struct cache_set_opts opts)
1207 {
1208         size_t nr_devs = 0, i = 0;
1209         char *dev_name, *s, **devs;
1210         struct cache_set *c = NULL;
1211         const char *err;
1212
1213         dev_name = kstrdup(_dev_name, GFP_KERNEL);
1214         if (!dev_name)
1215                 return NULL;
1216
1217         for (s = dev_name; s; s = strchr(s + 1, ':'))
1218                 nr_devs++;
1219
1220         devs = kcalloc(nr_devs, sizeof(const char *), GFP_KERNEL);
1221         if (!devs)
1222                 goto err;
1223
1224         for (i = 0, s = dev_name;
1225              s;
1226              (s = strchr(s, ':')) && (*s++ = '\0'))
1227                 devs[i++] = s;
1228
1229         err = bch_register_cache_set(devs, nr_devs, opts, &c);
1230         if (err) {
1231                 /*
1232                  * Already open?
1233                  * Look up each block device, make sure they all belong to a
1234                  * cache set and they all belong to the _same_ cache set
1235                  */
1236
1237                 mutex_lock(&bch_register_lock);
1238
1239                 for (i = 0; i < nr_devs; i++) {
1240                         struct block_device *bdev = lookup_bdev(devs[i]);
1241                         struct cache_set *c2;
1242
1243                         if (IS_ERR(bdev))
1244                                 goto err_unlock;
1245
1246                         c2 = bdev_to_cache_set(bdev);
1247                         bdput(bdev);
1248
1249                         if (!c)
1250                                 c = c2;
1251
1252                         if (c != c2)
1253                                 goto err_unlock;
1254                 }
1255
1256                 if (!c)
1257                         goto err_unlock;
1258
1259                 if (!test_bit(CACHE_SET_RUNNING, &c->flags)) {
1260                         err = "incomplete cache set";
1261                         c = NULL;
1262                         goto err_unlock;
1263                 }
1264
1265                 closure_get(&c->cl);
1266                 mutex_unlock(&bch_register_lock);
1267         }
1268
1269         set_bit(CACHE_SET_BDEV_MOUNTED, &c->flags);
1270 err:
1271         kfree(devs);
1272         kfree(dev_name);
1273
1274         return c;
1275 err_unlock:
1276         mutex_unlock(&bch_register_lock);
1277         pr_err("register_cache_set err %s", err);
1278         goto err;
1279 }
1280
1281 static int bch_remount(struct super_block *sb, int *flags, char *data)
1282 {
1283         struct cache_set *c = sb->s_fs_info;
1284         struct cache_set_opts opts;
1285         int ret;
1286
1287         ret = bch_parse_options(&opts, *flags, data);
1288         if (ret)
1289                 return ret;
1290
1291         mutex_lock(&bch_register_lock);
1292
1293         if (opts.read_only >= 0 &&
1294             opts.read_only != c->opts.read_only) {
1295                 const char *err = NULL;
1296
1297                 if (opts.read_only) {
1298                         bch_cache_set_read_only_sync(c);
1299
1300                         sb->s_flags |= MS_RDONLY;
1301                 } else {
1302                         err = bch_cache_set_read_write(c);
1303                         if (err) {
1304                                 bch_err(c, "error going rw: %s", err);
1305                                 ret = -EINVAL;
1306                                 goto unlock;
1307                         }
1308
1309                         sb->s_flags &= ~MS_RDONLY;
1310                 }
1311
1312                 c->opts.read_only = opts.read_only;
1313         }
1314
1315         if (opts.errors >= 0)
1316                 c->opts.errors = opts.errors;
1317
1318 unlock:
1319         mutex_unlock(&bch_register_lock);
1320
1321         return ret;
1322 }
1323
1324 static const struct super_operations bch_super_operations = {
1325         .alloc_inode    = bch_alloc_inode,
1326         .destroy_inode  = bch_destroy_inode,
1327         .write_inode    = bch_vfs_write_inode,
1328         .evict_inode    = bch_evict_inode,
1329         .sync_fs        = bch_sync_fs,
1330         .statfs         = bch_statfs,
1331         .show_options   = generic_show_options,
1332         .remount_fs     = bch_remount,
1333 #if 0
1334         .put_super      = bch_put_super,
1335         .freeze_fs      = bch_freeze,
1336         .unfreeze_fs    = bch_unfreeze,
1337 #endif
1338 };
1339
1340 static int bch_test_super(struct super_block *s, void *data)
1341 {
1342         return s->s_fs_info == data;
1343 }
1344
1345 static int bch_set_super(struct super_block *s, void *data)
1346 {
1347         s->s_fs_info = data;
1348         return 0;
1349 }
1350
1351 static struct dentry *bch_mount(struct file_system_type *fs_type,
1352                                 int flags, const char *dev_name, void *data)
1353 {
1354         struct cache_set *c;
1355         struct cache *ca;
1356         struct super_block *sb;
1357         struct inode *inode;
1358         struct cache_set_opts opts;
1359         unsigned i;
1360         int ret;
1361
1362         ret = bch_parse_options(&opts, flags, data);
1363         if (ret)
1364                 return ERR_PTR(ret);
1365
1366         c = bch_open_as_blockdevs(dev_name, opts);
1367         if (!c)
1368                 return ERR_PTR(-ENOENT);
1369
1370         sb = sget(fs_type, bch_test_super, bch_set_super, flags|MS_NOSEC, c);
1371         if (IS_ERR(sb)) {
1372                 closure_put(&c->cl);
1373                 return ERR_CAST(sb);
1374         }
1375
1376         BUG_ON(sb->s_fs_info != c);
1377
1378         if (sb->s_root) {
1379                 closure_put(&c->cl);
1380
1381                 if ((flags ^ sb->s_flags) & MS_RDONLY) {
1382                         ret = -EBUSY;
1383                         goto err_put_super;
1384                 }
1385                 goto out;
1386         }
1387
1388         /* XXX: blocksize */
1389         sb->s_blocksize         = PAGE_SIZE;
1390         sb->s_blocksize_bits    = PAGE_SHIFT;
1391         sb->s_maxbytes          = MAX_LFS_FILESIZE;
1392         sb->s_op                = &bch_super_operations;
1393         sb->s_xattr             = bch_xattr_handlers;
1394         sb->s_magic             = BCACHE_STATFS_MAGIC;
1395         sb->s_time_gran         = c->sb.time_precision;
1396         c->vfs_sb               = sb;
1397         sb->s_bdi               = &c->bdi;
1398
1399         rcu_read_lock();
1400         for_each_cache_rcu(ca, c, i) {
1401                 struct block_device *bdev = ca->disk_sb.bdev;
1402
1403                 BUILD_BUG_ON(sizeof(sb->s_id) < BDEVNAME_SIZE);
1404
1405                 bdevname(bdev, sb->s_id);
1406
1407                 /* XXX: do we even need s_bdev? */
1408                 sb->s_bdev      = bdev;
1409                 sb->s_dev       = bdev->bd_dev;
1410                 break;
1411         }
1412         rcu_read_unlock();
1413
1414         if (opts.posix_acl < 0)
1415                 sb->s_flags     |= MS_POSIXACL;
1416         else
1417                 sb->s_flags     |= opts.posix_acl ? MS_POSIXACL : 0;
1418
1419         inode = bch_vfs_inode_get(sb, BCACHE_ROOT_INO);
1420         if (IS_ERR(inode)) {
1421                 ret = PTR_ERR(inode);
1422                 goto err_put_super;
1423         }
1424
1425         sb->s_root = d_make_root(inode);
1426         if (!sb->s_root) {
1427                 ret = -ENOMEM;
1428                 goto err_put_super;
1429         }
1430
1431         sb->s_flags |= MS_ACTIVE;
1432 out:
1433         return dget(sb->s_root);
1434
1435 err_put_super:
1436         deactivate_locked_super(sb);
1437         return ERR_PTR(ret);
1438 }
1439
1440 static void bch_kill_sb(struct super_block *sb)
1441 {
1442         struct cache_set *c = sb->s_fs_info;
1443
1444         generic_shutdown_super(sb);
1445
1446         if (test_bit(CACHE_SET_BDEV_MOUNTED, &c->flags)) {
1447                 DECLARE_COMPLETION_ONSTACK(complete);
1448
1449                 c->stop_completion = &complete;
1450                 bch_cache_set_stop(c);
1451                 closure_put(&c->cl);
1452
1453                 /* Killable? */
1454                 wait_for_completion(&complete);
1455         } else
1456                 closure_put(&c->cl);
1457 }
1458
1459 static struct file_system_type bcache_fs_type = {
1460         .owner          = THIS_MODULE,
1461         .name           = "bcache",
1462         .mount          = bch_mount,
1463         .kill_sb        = bch_kill_sb,
1464         .fs_flags       = FS_REQUIRES_DEV,
1465 };
1466
1467 MODULE_ALIAS_FS("bcache");
1468
1469 void bch_fs_exit(void)
1470 {
1471         unregister_filesystem(&bcache_fs_type);
1472         if (bch_dio_write_bioset)
1473                 bioset_free(bch_dio_write_bioset);
1474         if (bch_dio_read_bioset)
1475                 bioset_free(bch_dio_read_bioset);
1476         if (bch_writepage_bioset)
1477                 bioset_free(bch_writepage_bioset);
1478         if (bch_inode_cache)
1479                 kmem_cache_destroy(bch_inode_cache);
1480 }
1481
1482 int __init bch_fs_init(void)
1483 {
1484         int ret = -ENOMEM;
1485
1486         bch_inode_cache = KMEM_CACHE(bch_inode_info, 0);
1487         if (!bch_inode_cache)
1488                 goto err;
1489
1490         bch_writepage_bioset =
1491                 bioset_create(4, offsetof(struct bch_writepage_io, bio.bio));
1492         if (!bch_writepage_bioset)
1493                 goto err;
1494
1495         bch_dio_read_bioset = bioset_create(4, offsetof(struct dio_read, rbio.bio));
1496         if (!bch_dio_read_bioset)
1497                 goto err;
1498
1499         bch_dio_write_bioset = bioset_create(4, offsetof(struct dio_write, bio.bio));
1500         if (!bch_dio_write_bioset)
1501                 goto err;
1502
1503         ret = register_filesystem(&bcache_fs_type);
1504         if (ret)
1505                 goto err;
1506
1507         return 0;
1508 err:
1509         bch_fs_exit();
1510         return ret;
1511 }