4 #include "btree_update.h"
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>
27 static struct kmem_cache *bch_inode_cache;
29 static void bch_vfs_inode_init(struct cache_set *,
30 struct bch_inode_info *,
31 struct bch_inode_unpacked *);
34 * I_SIZE_DIRTY requires special handling:
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
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.
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.
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.
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
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
61 int __must_check __bch_write_inode(struct cache_set *c,
62 struct bch_inode_info *ei,
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);
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.
82 lockdep_assert_held(&ei->update_lock);
84 bch_btree_iter_init_intent(&iter, c, BTREE_ID_INODES, POS(inum, 0));
87 struct bkey_s_c k = bch_btree_iter_peek_with_holes(&iter);
89 if ((ret = btree_iter_err(k)))
92 if (WARN_ONCE(k.k->type != BCH_INODE_FS,
93 "inode %llu not found when updating", inum)) {
94 bch_btree_iter_unlock(&iter);
98 ret = bch_inode_unpack(bkey_s_c_to_inode(k), &inode_u);
100 "error %i unpacking inode %llu", ret, inum)) {
106 ret = set(ei, &inode_u, p);
111 BUG_ON(i_nlink < nlink_bias(inode->i_mode));
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);
122 bch_inode_pack(&inode_p, &inode_u);
124 ret = bch_btree_insert_at(c, NULL, NULL, &ei->journal_seq,
127 BTREE_INSERT_ENTRY(&iter, &inode_p.inode.k_i));
128 } while (ret == -EINTR);
131 ei->i_size = inode_u.i_size;
132 ei->i_flags = inode_u.i_flags;
135 bch_btree_iter_unlock(&iter);
137 return ret < 0 ? ret : 0;
140 int __must_check bch_write_inode(struct cache_set *c,
141 struct bch_inode_info *ei)
143 return __bch_write_inode(c, ei, NULL, NULL);
146 int bch_inc_nlink(struct cache_set *c, struct bch_inode_info *ei)
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);
158 int bch_dec_nlink(struct cache_set *c, struct bch_inode_info *ei)
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);
170 static struct inode *bch_vfs_inode_get(struct super_block *sb, u64 inum)
172 struct cache_set *c = sb->s_fs_info;
174 struct bch_inode_unpacked inode_u;
175 struct bch_inode_info *ei;
178 pr_debug("inum %llu", inum);
180 inode = iget_locked(sb, inum);
181 if (unlikely(!inode))
182 return ERR_PTR(-ENOMEM);
183 if (!(inode->i_state & I_NEW))
186 ret = bch_inode_find_by_inum(c, inum, &inode_u);
192 ei = to_bch_ei(inode);
193 bch_vfs_inode_init(c, ei, &inode_u);
195 ei->journal_seq = bch_inode_journal_seq(&c->journal, inum);
197 unlock_new_inode(inode);
202 static struct inode *bch_vfs_inode_create(struct cache_set *c,
203 struct inode *parent,
204 umode_t mode, dev_t rdev)
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;
213 inode = new_inode(parent->i_sb);
214 if (unlikely(!inode))
215 return ERR_PTR(-ENOMEM);
217 inode_init_owner(inode, parent, mode);
219 ret = posix_acl_create(parent, &inode->i_mode, &default_acl, &acl);
221 make_bad_inode(inode);
225 ei = to_bch_ei(inode);
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);
231 ret = bch_inode_create(c, &inode_p.inode.k_i,
232 BLOCKDEV_INODE_MAX, 0,
233 &c->unused_inode_hint);
236 * indicate to bch_evict_inode that the inode was never actually
239 make_bad_inode(inode);
243 inode_u.inum = inode_p.inode.k.p.inode;
244 bch_vfs_inode_init(c, ei, &inode_u);
247 ret = bch_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
253 ret = bch_set_acl(inode, acl, ACL_TYPE_ACCESS);
258 insert_inode_hash(inode);
259 atomic_long_inc(&c->nr_inodes);
261 posix_acl_release(default_acl);
262 posix_acl_release(acl);
267 inode = ERR_PTR(ret);
271 static int bch_vfs_dirent_create(struct cache_set *c, struct inode *dir,
272 u8 type, const struct qstr *name,
275 struct bch_inode_info *dir_ei = to_bch_ei(dir);
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);
285 dir->i_mtime = dir->i_ctime = current_fs_time(dir->i_sb);
286 mark_inode_dirty_sync(dir);
290 static int __bch_create(struct inode *dir, struct dentry *dentry,
291 umode_t mode, dev_t rdev)
293 struct bch_inode_info *dir_ei = to_bch_ei(dir);
294 struct cache_set *c = dir->i_sb->s_fs_info;
296 struct bch_inode_info *ei;
299 inode = bch_vfs_inode_create(c, dir, mode, rdev);
300 if (unlikely(IS_ERR(inode)))
301 return PTR_ERR(inode);
303 ei = to_bch_ei(inode);
305 ret = bch_vfs_dirent_create(c, dir, mode_to_type(mode),
306 &dentry->d_name, inode);
313 if (dir_ei->journal_seq > ei->journal_seq)
314 ei->journal_seq = dir_ei->journal_seq;
316 d_instantiate(dentry, inode);
322 static struct dentry *bch_lookup(struct inode *dir, struct dentry *dentry,
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;
330 inum = bch_dirent_lookup(c, dir->i_ino,
335 inode = bch_vfs_inode_get(dir->i_sb, inum);
337 return d_splice_alias(inode, dentry);
340 static int bch_create(struct inode *dir, struct dentry *dentry,
341 umode_t mode, bool excl)
343 return __bch_create(dir, dentry, mode|S_IFREG, 0);
346 static int bch_link(struct dentry *old_dentry, struct inode *dir,
347 struct dentry *dentry)
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);
354 lockdep_assert_held(&inode->i_rwsem);
356 inode->i_ctime = current_fs_time(dir->i_sb);
358 ret = bch_inc_nlink(c, ei);
364 ret = bch_vfs_dirent_create(c, dir, mode_to_type(inode->i_mode),
365 &dentry->d_name, inode);
367 bch_dec_nlink(c, ei);
372 d_instantiate(dentry, inode);
376 static int bch_unlink(struct inode *dir, struct dentry *dentry)
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);
384 lockdep_assert_held(&inode->i_rwsem);
386 ret = bch_dirent_delete(c, dir->i_ino, &dir_ei->str_hash,
387 &dentry->d_name, &dir_ei->journal_seq);
391 if (dir_ei->journal_seq > ei->journal_seq)
392 ei->journal_seq = dir_ei->journal_seq;
394 inode->i_ctime = dir->i_ctime;
396 if (S_ISDIR(inode->i_mode)) {
397 bch_dec_nlink(c, dir_ei);
401 bch_dec_nlink(c, ei);
406 static int bch_symlink(struct inode *dir, struct dentry *dentry,
409 struct cache_set *c = dir->i_sb->s_fs_info;
411 struct bch_inode_info *ei, *dir_ei = to_bch_ei(dir);
414 inode = bch_vfs_inode_create(c, dir, S_IFLNK|S_IRWXUGO, 0);
415 if (unlikely(IS_ERR(inode)))
416 return PTR_ERR(inode);
418 ei = to_bch_ei(inode);
421 ret = page_symlink(inode, symname, strlen(symname) + 1);
427 ret = filemap_write_and_wait_range(inode->i_mapping, 0, LLONG_MAX);
432 if (dir_ei->journal_seq < ei->journal_seq)
433 dir_ei->journal_seq = ei->journal_seq;
435 ret = bch_vfs_dirent_create(c, dir, DT_LNK, &dentry->d_name, inode);
439 d_instantiate(dentry, inode);
447 static int bch_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
449 struct cache_set *c = dir->i_sb->s_fs_info;
452 lockdep_assert_held(&dir->i_rwsem);
454 ret = __bch_create(dir, dentry, mode|S_IFDIR, 0);
458 bch_inc_nlink(c, to_bch_ei(dir));
463 static int bch_rmdir(struct inode *dir, struct dentry *dentry)
465 struct cache_set *c = dir->i_sb->s_fs_info;
466 struct inode *inode = dentry->d_inode;
468 if (bch_empty_dir(c, inode->i_ino))
471 return bch_unlink(dir, dentry);
474 static int bch_mknod(struct inode *dir, struct dentry *dentry,
475 umode_t mode, dev_t rdev)
477 return __bch_create(dir, dentry, mode, rdev);
480 static int bch_rename(struct inode *old_dir, struct dentry *old_dentry,
481 struct inode *new_dir, struct dentry *new_dentry)
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);
490 lockdep_assert_held(&old_dir->i_rwsem);
491 lockdep_assert_held(&new_dir->i_rwsem);
494 filemap_write_and_wait_range(old_inode->i_mapping,
497 if (new_inode && S_ISDIR(old_inode->i_mode)) {
498 lockdep_assert_held(&new_inode->i_rwsem);
500 if (!S_ISDIR(new_inode->i_mode))
503 if (bch_empty_dir(c, new_inode->i_ino))
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);
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);
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);
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);
535 bch_inc_nlink(c, to_bch_ei(new_dir));
536 bch_dec_nlink(c, to_bch_ei(old_dir));
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);
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);
551 old_inode->i_ctime = now;
552 mark_inode_dirty_sync(old_inode);
557 static int bch_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
558 struct inode *new_dir, struct dentry *new_dentry)
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);
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);
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));
580 bch_dec_nlink(c, to_bch_ei(new_dir));
581 bch_inc_nlink(c, to_bch_ei(old_dir));
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);
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);
598 static int bch_rename2(struct inode *old_dir, struct dentry *old_dentry,
599 struct inode *new_dir, struct dentry *new_dentry,
602 if (flags & ~(RENAME_NOREPLACE|RENAME_EXCHANGE))
605 if (flags & RENAME_EXCHANGE)
606 return bch_rename_exchange(old_dir, old_dentry,
607 new_dir, new_dentry);
609 return bch_rename(old_dir, old_dentry, new_dir, new_dentry);
612 static int bch_setattr(struct dentry *dentry, struct iattr *iattr)
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;
619 lockdep_assert_held(&inode->i_rwsem);
621 pr_debug("i_size was %llu update has %llu",
622 inode->i_size, iattr->ia_size);
624 ret = setattr_prepare(dentry, iattr);
628 if (iattr->ia_valid & ATTR_SIZE) {
629 ret = bch_truncate(inode, iattr);
631 mutex_lock(&ei->update_lock);
632 setattr_copy(inode, iattr);
633 ret = bch_write_inode(c, ei);
634 mutex_unlock(&ei->update_lock);
640 if (iattr->ia_valid & ATTR_MODE)
641 ret = posix_acl_chmod(inode, inode->i_mode);
646 static int bch_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
648 struct cache_set *c = dir->i_sb->s_fs_info;
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);
656 d_tmpfile(dentry, inode);
660 static int bch_fill_extent(struct fiemap_extent_info *info,
661 const struct bkey_i *k, unsigned flags)
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;
669 extent_for_each_ptr_crc(e, ptr, crc) {
671 u64 offset = ptr->offset;
673 if (crc_compression_type(crc))
674 flags2 |= FIEMAP_EXTENT_ENCODED;
676 offset += crc_offset(crc);
678 if ((offset & (PAGE_SECTORS - 1)) ||
679 (e.k->size & (PAGE_SECTORS - 1)))
680 flags2 |= FIEMAP_EXTENT_NOT_ALIGNED;
682 ret = fiemap_fill_next_extent(info,
683 bkey_start_offset(e.k) << 9,
685 e.k->size << 9, flags|flags2);
691 } else if (k->k.type == BCH_RESERVATION) {
692 return fiemap_fill_next_extent(info,
693 bkey_start_offset(&k->k) << 9,
696 FIEMAP_EXTENT_DELALLOC|
697 FIEMAP_EXTENT_UNWRITTEN);
703 static int bch_fiemap(struct inode *inode, struct fiemap_extent_info *info,
706 struct cache_set *c = inode->i_sb->s_fs_info;
707 struct btree_iter iter;
710 bool have_extent = false;
713 if (start + len < start)
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)
725 ret = bch_fill_extent(info, &tmp.k, 0);
730 bkey_reassemble(&tmp.k, k);
735 ret = bch_fill_extent(info, &tmp.k, FIEMAP_EXTENT_LAST);
737 bch_btree_iter_unlock(&iter);
738 return ret < 0 ? ret : 0;
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,
747 static int bch_mmap(struct file *file, struct vm_area_struct *vma)
751 vma->vm_ops = &bch_vm_ops;
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,
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,
772 /* Set VFS inode flags from bcache inode: */
773 static void bch_inode_flags_to_vfs(struct inode *inode)
775 unsigned i, flags = to_bch_ei(inode)->i_flags;
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];
781 inode->i_flags &= ~bch_inode_flags_to_vfs_flags_map[i];
784 /* Get FS_IOC_GETFLAGS flags from bcache inode: */
785 static unsigned bch_inode_flags_to_user_flags(unsigned flags)
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];
796 static int bch_inode_user_flags_set(struct bch_inode_info *ei,
797 struct bch_inode_unpacked *bi,
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:
804 unsigned bch_flags = bi->i_flags;
805 unsigned oldflags = bch_inode_flags_to_user_flags(bch_flags);
806 unsigned newflags = *((unsigned *) p);
809 if (((newflags ^ oldflags) & (FS_APPEND_FL|FS_IMMUTABLE_FL)) &&
810 !capable(CAP_LINUX_IMMUTABLE))
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);
817 bch_flags &= ~(1 << i);
819 newflags &= ~bch_inode_flags_to_user_flags_map[i];
820 oldflags &= ~bch_inode_flags_to_user_flags_map[i];
823 if (oldflags != newflags)
826 bi->i_flags = bch_flags;
827 ei->vfs_inode.i_ctime = current_fs_time(ei->vfs_inode.i_sb);
832 #define FS_IOC_GOINGDOWN _IOR ('X', 125, __u32)
834 static long bch_fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
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);
844 case FS_IOC_GETFLAGS:
845 return put_user(bch_inode_flags_to_user_flags(ei->i_flags),
848 case FS_IOC_SETFLAGS: {
849 ret = mnt_want_write_file(filp);
853 if (!inode_owner_or_capable(inode)) {
858 if (get_user(flags, (int __user *) arg)) {
863 if (!S_ISREG(inode->i_mode) &&
864 !S_ISDIR(inode->i_mode) &&
865 (flags & (FS_NODUMP_FL|FS_NOATIME_FL)) != flags) {
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);
877 bch_inode_flags_to_vfs(inode);
881 mnt_drop_write_file(filp);
885 case FS_IOC_GETVERSION:
887 case FS_IOC_SETVERSION:
890 case FS_IOC_GOINGDOWN:
891 if (!capable(CAP_SYS_ADMIN))
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);
901 return bch_cache_set_ioctl(c, cmd, (void __user *) arg);
906 static long bch_compat_fs_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
908 /* These are just misnamed, they actually get/put from/to user an int */
910 case FS_IOC_GETFLAGS:
911 cmd = FS_IOC_GETFLAGS;
913 case FS_IOC32_SETFLAGS:
914 cmd = FS_IOC_SETFLAGS;
919 return bch_fs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
925 static loff_t bch_dir_llseek(struct file *file, loff_t offset, int whence)
927 return generic_file_llseek_size(file, offset, whence,
931 static int bch_vfs_readdir(struct file *file, struct dir_context *ctx)
933 struct inode *inode = file_inode(file);
934 struct cache_set *c = inode->i_sb->s_fs_info;
936 return bch_readdir(c, file, ctx);
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,
944 .open = generic_file_open,
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,
951 .compat_ioctl = bch_compat_fs_ioctl,
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,
963 static const struct inode_operations bch_dir_inode_operations = {
964 .lookup = bch_lookup,
965 .create = bch_create,
967 .unlink = bch_unlink,
968 .symlink = bch_symlink,
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,
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,
985 .unlocked_ioctl = bch_fs_ioctl,
987 .compat_ioctl = bch_compat_fs_ioctl,
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,
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,
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,
1021 .error_remove_page = generic_error_remove_page,
1024 static void bch_vfs_inode_init(struct cache_set *c,
1025 struct bch_inode_info *ei,
1026 struct bch_inode_unpacked *bi)
1028 struct inode *inode = &ei->vfs_inode;
1030 pr_debug("init inode %llu with mode %o",
1031 bi->inum, bi->i_mode);
1033 ei->i_flags = bi->i_flags;
1034 ei->i_size = bi->i_size;
1036 inode->i_mode = bi->i_mode;
1037 i_uid_write(inode, bi->i_uid);
1038 i_gid_write(inode, bi->i_gid);
1040 atomic64_set(&ei->i_sectors, bi->i_sectors);
1041 inode->i_blocks = bi->i_sectors;
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);
1053 ei->str_hash = bch_hash_info_init(bi);
1055 inode->i_mapping->a_ops = &bch_address_space_operations;
1057 switch (inode->i_mode & S_IFMT) {
1059 inode->i_op = &bch_file_inode_operations;
1060 inode->i_fop = &bch_file_operations;
1063 inode->i_op = &bch_dir_inode_operations;
1064 inode->i_fop = &bch_dir_file_operations;
1067 inode_nohighmem(inode);
1068 inode->i_op = &bch_symlink_inode_operations;
1071 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1072 inode->i_op = &bch_special_inode_operations;
1077 static struct inode *bch_alloc_inode(struct super_block *sb)
1079 struct bch_inode_info *ei;
1081 ei = kmem_cache_alloc(bch_inode_cache, GFP_NOFS);
1085 pr_debug("allocated %p", &ei->vfs_inode);
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);
1093 return &ei->vfs_inode;
1096 static void bch_i_callback(struct rcu_head *head)
1098 struct inode *inode = container_of(head, struct inode, i_rcu);
1100 kmem_cache_free(bch_inode_cache, to_bch_ei(inode));
1103 static void bch_destroy_inode(struct inode *inode)
1105 call_rcu(&inode->i_rcu, bch_i_callback);
1108 static int bch_vfs_write_inode(struct inode *inode,
1109 struct writeback_control *wbc)
1111 struct cache_set *c = inode->i_sb->s_fs_info;
1112 struct bch_inode_info *ei = to_bch_ei(inode);
1115 mutex_lock(&ei->update_lock);
1116 ret = bch_write_inode(c, ei);
1117 mutex_unlock(&ei->update_lock);
1119 if (c->opts.journal_flush_disabled)
1122 if (!ret && wbc->sync_mode == WB_SYNC_ALL)
1123 ret = bch_journal_flush_seq(&c->journal, ei->journal_seq);
1128 static void bch_evict_inode(struct inode *inode)
1130 struct cache_set *c = inode->i_sb->s_fs_info;
1132 truncate_inode_pages_final(&inode->i_data);
1134 if (!bch_journal_error(&c->journal) && !is_bad_inode(inode)) {
1135 struct bch_inode_info *ei = to_bch_ei(inode);
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);
1144 if (!inode->i_nlink && !is_bad_inode(inode)) {
1145 bch_inode_rm(c, inode->i_ino);
1146 atomic_long_dec(&c->nr_inodes);
1150 static int bch_statfs(struct dentry *dentry, struct kstatfs *buf)
1152 struct super_block *sb = dentry->d_sb;
1153 struct cache_set *c = sb->s_fs_info;
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;
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;
1173 static int bch_sync_fs(struct super_block *sb, int wait)
1175 struct cache_set *c = sb->s_fs_info;
1178 bch_journal_flush_async(&c->journal, NULL);
1182 return bch_journal_flush(&c->journal);
1185 static struct cache_set *bdev_to_cache_set(struct block_device *bdev)
1187 struct cache_set *c;
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) {
1205 static struct cache_set *bch_open_as_blockdevs(const char *_dev_name,
1206 struct cache_set_opts opts)
1208 size_t nr_devs = 0, i = 0;
1209 char *dev_name, *s, **devs;
1210 struct cache_set *c = NULL;
1213 dev_name = kstrdup(_dev_name, GFP_KERNEL);
1217 for (s = dev_name; s; s = strchr(s + 1, ':'))
1220 devs = kcalloc(nr_devs, sizeof(const char *), GFP_KERNEL);
1224 for (i = 0, s = dev_name;
1226 (s = strchr(s, ':')) && (*s++ = '\0'))
1229 err = bch_register_cache_set(devs, nr_devs, opts, &c);
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
1237 mutex_lock(&bch_register_lock);
1239 for (i = 0; i < nr_devs; i++) {
1240 struct block_device *bdev = lookup_bdev(devs[i]);
1241 struct cache_set *c2;
1246 c2 = bdev_to_cache_set(bdev);
1259 if (!test_bit(CACHE_SET_RUNNING, &c->flags)) {
1260 err = "incomplete cache set";
1265 closure_get(&c->cl);
1266 mutex_unlock(&bch_register_lock);
1269 set_bit(CACHE_SET_BDEV_MOUNTED, &c->flags);
1276 mutex_unlock(&bch_register_lock);
1277 pr_err("register_cache_set err %s", err);
1281 static int bch_remount(struct super_block *sb, int *flags, char *data)
1283 struct cache_set *c = sb->s_fs_info;
1284 struct cache_set_opts opts;
1287 ret = bch_parse_options(&opts, *flags, data);
1291 mutex_lock(&bch_register_lock);
1293 if (opts.read_only >= 0 &&
1294 opts.read_only != c->opts.read_only) {
1295 const char *err = NULL;
1297 if (opts.read_only) {
1298 bch_cache_set_read_only_sync(c);
1300 sb->s_flags |= MS_RDONLY;
1302 err = bch_cache_set_read_write(c);
1304 bch_err(c, "error going rw: %s", err);
1309 sb->s_flags &= ~MS_RDONLY;
1312 c->opts.read_only = opts.read_only;
1315 if (opts.errors >= 0)
1316 c->opts.errors = opts.errors;
1319 mutex_unlock(&bch_register_lock);
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,
1334 .put_super = bch_put_super,
1335 .freeze_fs = bch_freeze,
1336 .unfreeze_fs = bch_unfreeze,
1340 static int bch_test_super(struct super_block *s, void *data)
1342 return s->s_fs_info == data;
1345 static int bch_set_super(struct super_block *s, void *data)
1347 s->s_fs_info = data;
1351 static struct dentry *bch_mount(struct file_system_type *fs_type,
1352 int flags, const char *dev_name, void *data)
1354 struct cache_set *c;
1356 struct super_block *sb;
1357 struct inode *inode;
1358 struct cache_set_opts opts;
1362 ret = bch_parse_options(&opts, flags, data);
1364 return ERR_PTR(ret);
1366 c = bch_open_as_blockdevs(dev_name, opts);
1368 return ERR_PTR(-ENOENT);
1370 sb = sget(fs_type, bch_test_super, bch_set_super, flags|MS_NOSEC, c);
1372 closure_put(&c->cl);
1373 return ERR_CAST(sb);
1376 BUG_ON(sb->s_fs_info != c);
1379 closure_put(&c->cl);
1381 if ((flags ^ sb->s_flags) & MS_RDONLY) {
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;
1397 sb->s_bdi = &c->bdi;
1400 for_each_cache_rcu(ca, c, i) {
1401 struct block_device *bdev = ca->disk_sb.bdev;
1403 BUILD_BUG_ON(sizeof(sb->s_id) < BDEVNAME_SIZE);
1405 bdevname(bdev, sb->s_id);
1407 /* XXX: do we even need s_bdev? */
1409 sb->s_dev = bdev->bd_dev;
1414 if (opts.posix_acl < 0)
1415 sb->s_flags |= MS_POSIXACL;
1417 sb->s_flags |= opts.posix_acl ? MS_POSIXACL : 0;
1419 inode = bch_vfs_inode_get(sb, BCACHE_ROOT_INO);
1420 if (IS_ERR(inode)) {
1421 ret = PTR_ERR(inode);
1425 sb->s_root = d_make_root(inode);
1431 sb->s_flags |= MS_ACTIVE;
1433 return dget(sb->s_root);
1436 deactivate_locked_super(sb);
1437 return ERR_PTR(ret);
1440 static void bch_kill_sb(struct super_block *sb)
1442 struct cache_set *c = sb->s_fs_info;
1444 generic_shutdown_super(sb);
1446 if (test_bit(CACHE_SET_BDEV_MOUNTED, &c->flags)) {
1447 DECLARE_COMPLETION_ONSTACK(complete);
1449 c->stop_completion = &complete;
1450 bch_cache_set_stop(c);
1451 closure_put(&c->cl);
1454 wait_for_completion(&complete);
1456 closure_put(&c->cl);
1459 static struct file_system_type bcache_fs_type = {
1460 .owner = THIS_MODULE,
1463 .kill_sb = bch_kill_sb,
1464 .fs_flags = FS_REQUIRES_DEV,
1467 MODULE_ALIAS_FS("bcache");
1469 void bch_fs_exit(void)
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);
1482 int __init bch_fs_init(void)
1486 bch_inode_cache = KMEM_CACHE(bch_inode_info, 0);
1487 if (!bch_inode_cache)
1490 bch_writepage_bioset =
1491 bioset_create(4, offsetof(struct bch_writepage_io, bio.bio));
1492 if (!bch_writepage_bioset)
1495 bch_dio_read_bioset = bioset_create(4, offsetof(struct dio_read, rbio.bio));
1496 if (!bch_dio_read_bioset)
1499 bch_dio_write_bioset = bioset_create(4, offsetof(struct dio_write, bio.bio));
1500 if (!bch_dio_write_bioset)
1503 ret = register_filesystem(&bcache_fs_type);