1 From f32e48e925964c4f8ab917850788a87e1cef3bad Mon Sep 17 00:00:00 2001
2 From: Chandan Rajendra <chandan@linux.vnet.ibm.com>
3 Date: Thu, 7 Jan 2016 18:56:59 +0530
4 Subject: Btrfs: Initialize btrfs_root->highest_objectid when loading tree root and subvolume roots
6 From: Chandan Rajendra <chandan@linux.vnet.ibm.com>
8 commit f32e48e925964c4f8ab917850788a87e1cef3bad upstream.
10 The following call trace is seen when btrfs/031 test is executed in a loop,
12 [ 158.661848] ------------[ cut here ]------------
13 [ 158.662634] WARNING: CPU: 2 PID: 890 at /home/chandan/repos/linux/fs/btrfs/ioctl.c:558 create_subvol+0x3d1/0x6ea()
14 [ 158.664102] BTRFS: Transaction aborted (error -2)
15 [ 158.664774] Modules linked in:
16 [ 158.665266] CPU: 2 PID: 890 Comm: btrfs Not tainted 4.4.0-rc6-g511711a #2
17 [ 158.666251] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
18 [ 158.667392] ffffffff81c0a6b0 ffff8806c7c4f8e8 ffffffff81431fc8 ffff8806c7c4f930
19 [ 158.668515] ffff8806c7c4f920 ffffffff81051aa1 ffff880c85aff000 ffff8800bb44d000
20 [ 158.669647] ffff8808863b5c98 0000000000000000 00000000fffffffe ffff8806c7c4f980
21 [ 158.670769] Call Trace:
22 [ 158.671153] [<ffffffff81431fc8>] dump_stack+0x44/0x5c
23 [ 158.671884] [<ffffffff81051aa1>] warn_slowpath_common+0x81/0xc0
24 [ 158.672769] [<ffffffff81051b27>] warn_slowpath_fmt+0x47/0x50
25 [ 158.673620] [<ffffffff813bc98d>] create_subvol+0x3d1/0x6ea
26 [ 158.674440] [<ffffffff813777c9>] btrfs_mksubvol.isra.30+0x369/0x520
27 [ 158.675376] [<ffffffff8108a4aa>] ? percpu_down_read+0x1a/0x50
28 [ 158.676235] [<ffffffff81377a81>] btrfs_ioctl_snap_create_transid+0x101/0x180
29 [ 158.677268] [<ffffffff81377b52>] btrfs_ioctl_snap_create+0x52/0x70
30 [ 158.678183] [<ffffffff8137afb4>] btrfs_ioctl+0x474/0x2f90
31 [ 158.678975] [<ffffffff81144b8e>] ? vma_merge+0xee/0x300
32 [ 158.679751] [<ffffffff8115be31>] ? alloc_pages_vma+0x91/0x170
33 [ 158.680599] [<ffffffff81123f62>] ? lru_cache_add_active_or_unevictable+0x22/0x70
34 [ 158.681686] [<ffffffff813d99cf>] ? selinux_file_ioctl+0xff/0x1d0
35 [ 158.682581] [<ffffffff8117b791>] do_vfs_ioctl+0x2c1/0x490
36 [ 158.683399] [<ffffffff813d3cde>] ? security_file_ioctl+0x3e/0x60
37 [ 158.684297] [<ffffffff8117b9d4>] SyS_ioctl+0x74/0x80
38 [ 158.685051] [<ffffffff819b2bd7>] entry_SYSCALL_64_fastpath+0x12/0x6a
39 [ 158.685958] ---[ end trace 4b63312de5a2cb76 ]---
40 [ 158.686647] BTRFS: error (device loop0) in create_subvol:558: errno=-2 No such entry
41 [ 158.709508] BTRFS info (device loop0): forced readonly
42 [ 158.737113] BTRFS info (device loop0): disk space caching is enabled
43 [ 158.738096] BTRFS error (device loop0): Remounting read-write after error is not allowed
44 [ 158.851303] BTRFS error (device loop0): cleaner transaction attach returned -30
49 Create subvol with ID 257
52 Delete subvol with ID 257
54 Add root corresponding to subvol 257 into
55 btrfs_transaction->dropped_roots list
56 Create new subvol (i.e. create_subvol())
57 257 is returned as the next free objectid
58 btrfs_read_fs_root_no_name()
59 Finds the btrfs_root instance corresponding to the old subvol with ID 257
60 in btrfs_fs_info->fs_roots_radix.
61 Returns error since btrfs_root_item->refs has the value of 0.
63 To fix the issue the commit initializes tree root's and subvolume root's
64 highest_objectid when loading the roots from disk.
66 Signed-off-by: Chandan Rajendra <chandan@linux.vnet.ibm.com>
67 Signed-off-by: David Sterba <dsterba@suse.com>
68 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
71 fs/btrfs/disk-io.c | 27 +++++++++++++++++++++++++++
72 fs/btrfs/inode-map.c | 9 +--------
73 fs/btrfs/inode-map.h | 1 +
74 fs/btrfs/ioctl.c | 4 ++++
75 4 files changed, 33 insertions(+), 8 deletions(-)
77 --- a/fs/btrfs/disk-io.c
78 +++ b/fs/btrfs/disk-io.c
79 @@ -1582,8 +1582,23 @@ int btrfs_init_fs_root(struct btrfs_root
80 ret = get_anon_bdev(&root->anon_dev);
84 + mutex_lock(&root->objectid_mutex);
85 + ret = btrfs_find_highest_objectid(root,
86 + &root->highest_objectid);
88 + mutex_unlock(&root->objectid_mutex);
92 + ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
94 + mutex_unlock(&root->objectid_mutex);
99 + free_anon_bdev(root->anon_dev);
101 btrfs_free_subvolume_writers(root->subv_writers);
103 @@ -2900,6 +2915,18 @@ retry_root_backup:
104 tree_root->commit_root = btrfs_root_node(tree_root);
105 btrfs_set_root_refs(&tree_root->root_item, 1);
107 + mutex_lock(&tree_root->objectid_mutex);
108 + ret = btrfs_find_highest_objectid(tree_root,
109 + &tree_root->highest_objectid);
111 + mutex_unlock(&tree_root->objectid_mutex);
112 + goto recovery_tree_root;
115 + ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
117 + mutex_unlock(&tree_root->objectid_mutex);
119 ret = btrfs_read_roots(fs_info, tree_root);
121 goto recovery_tree_root;
122 --- a/fs/btrfs/inode-map.c
123 +++ b/fs/btrfs/inode-map.c
124 @@ -515,7 +515,7 @@ out:
128 -static int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
129 +int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid)
131 struct btrfs_path *path;
133 @@ -555,13 +555,6 @@ int btrfs_find_free_objectid(struct btrf
135 mutex_lock(&root->objectid_mutex);
137 - if (unlikely(root->highest_objectid < BTRFS_FIRST_FREE_OBJECTID)) {
138 - ret = btrfs_find_highest_objectid(root,
139 - &root->highest_objectid);
144 if (unlikely(root->highest_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
147 --- a/fs/btrfs/inode-map.h
148 +++ b/fs/btrfs/inode-map.h
149 @@ -9,5 +9,6 @@ int btrfs_save_ino_cache(struct btrfs_ro
150 struct btrfs_trans_handle *trans);
152 int btrfs_find_free_objectid(struct btrfs_root *root, u64 *objectid);
153 +int btrfs_find_highest_objectid(struct btrfs_root *root, u64 *objectid);
156 --- a/fs/btrfs/ioctl.c
157 +++ b/fs/btrfs/ioctl.c
158 @@ -568,6 +568,10 @@ static noinline int create_subvol(struct
162 + mutex_lock(&new_root->objectid_mutex);
163 + new_root->highest_objectid = new_dirid;
164 + mutex_unlock(&new_root->objectid_mutex);
167 * insert the directory item