Commit 99ba55ad696944b37d5557bc5b4816890854fdb9
Committed by
David Sterba
1 parent
b9688bb845
Exists in
smarc-l5.0.0_1.0.0-ga
and in
5 other branches
Btrfs: fix btrfs_ioctl_dev_info() crash on missing device
When a filesystem is mounted with the degraded option, it is possible that some of the devices are not there. btrfs_ioctl_dev_info() crashs in this case because the device name is a NULL pointer. This ioctl was only used for scrub. Signed-off-by: Stefan Behrens <sbehrens@giantdisaster.de>
Showing 1 changed file with 4 additions and 1 deletions Inline Diff
fs/btrfs/ioctl.c
1 | /* | 1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
3 | * | 3 | * |
4 | * This program is free software; you can redistribute it and/or | 4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public | 5 | * modify it under the terms of the GNU General Public |
6 | * License v2 as published by the Free Software Foundation. | 6 | * License v2 as published by the Free Software Foundation. |
7 | * | 7 | * |
8 | * This program is distributed in the hope that it will be useful, | 8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | * General Public License for more details. | 11 | * General Public License for more details. |
12 | * | 12 | * |
13 | * You should have received a copy of the GNU General Public | 13 | * You should have received a copy of the GNU General Public |
14 | * License along with this program; if not, write to the | 14 | * License along with this program; if not, write to the |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
16 | * Boston, MA 021110-1307, USA. | 16 | * Boston, MA 021110-1307, USA. |
17 | */ | 17 | */ |
18 | 18 | ||
19 | #include <linux/kernel.h> | 19 | #include <linux/kernel.h> |
20 | #include <linux/bio.h> | 20 | #include <linux/bio.h> |
21 | #include <linux/buffer_head.h> | 21 | #include <linux/buffer_head.h> |
22 | #include <linux/file.h> | 22 | #include <linux/file.h> |
23 | #include <linux/fs.h> | 23 | #include <linux/fs.h> |
24 | #include <linux/fsnotify.h> | 24 | #include <linux/fsnotify.h> |
25 | #include <linux/pagemap.h> | 25 | #include <linux/pagemap.h> |
26 | #include <linux/highmem.h> | 26 | #include <linux/highmem.h> |
27 | #include <linux/time.h> | 27 | #include <linux/time.h> |
28 | #include <linux/init.h> | 28 | #include <linux/init.h> |
29 | #include <linux/string.h> | 29 | #include <linux/string.h> |
30 | #include <linux/backing-dev.h> | 30 | #include <linux/backing-dev.h> |
31 | #include <linux/mount.h> | 31 | #include <linux/mount.h> |
32 | #include <linux/mpage.h> | 32 | #include <linux/mpage.h> |
33 | #include <linux/namei.h> | 33 | #include <linux/namei.h> |
34 | #include <linux/swap.h> | 34 | #include <linux/swap.h> |
35 | #include <linux/writeback.h> | 35 | #include <linux/writeback.h> |
36 | #include <linux/statfs.h> | 36 | #include <linux/statfs.h> |
37 | #include <linux/compat.h> | 37 | #include <linux/compat.h> |
38 | #include <linux/bit_spinlock.h> | 38 | #include <linux/bit_spinlock.h> |
39 | #include <linux/security.h> | 39 | #include <linux/security.h> |
40 | #include <linux/xattr.h> | 40 | #include <linux/xattr.h> |
41 | #include <linux/vmalloc.h> | 41 | #include <linux/vmalloc.h> |
42 | #include <linux/slab.h> | 42 | #include <linux/slab.h> |
43 | #include <linux/blkdev.h> | 43 | #include <linux/blkdev.h> |
44 | #include "compat.h" | 44 | #include "compat.h" |
45 | #include "ctree.h" | 45 | #include "ctree.h" |
46 | #include "disk-io.h" | 46 | #include "disk-io.h" |
47 | #include "transaction.h" | 47 | #include "transaction.h" |
48 | #include "btrfs_inode.h" | 48 | #include "btrfs_inode.h" |
49 | #include "ioctl.h" | 49 | #include "ioctl.h" |
50 | #include "print-tree.h" | 50 | #include "print-tree.h" |
51 | #include "volumes.h" | 51 | #include "volumes.h" |
52 | #include "locking.h" | 52 | #include "locking.h" |
53 | #include "inode-map.h" | 53 | #include "inode-map.h" |
54 | #include "backref.h" | 54 | #include "backref.h" |
55 | 55 | ||
56 | /* Mask out flags that are inappropriate for the given type of inode. */ | 56 | /* Mask out flags that are inappropriate for the given type of inode. */ |
57 | static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) | 57 | static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) |
58 | { | 58 | { |
59 | if (S_ISDIR(mode)) | 59 | if (S_ISDIR(mode)) |
60 | return flags; | 60 | return flags; |
61 | else if (S_ISREG(mode)) | 61 | else if (S_ISREG(mode)) |
62 | return flags & ~FS_DIRSYNC_FL; | 62 | return flags & ~FS_DIRSYNC_FL; |
63 | else | 63 | else |
64 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); | 64 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); |
65 | } | 65 | } |
66 | 66 | ||
67 | /* | 67 | /* |
68 | * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. | 68 | * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. |
69 | */ | 69 | */ |
70 | static unsigned int btrfs_flags_to_ioctl(unsigned int flags) | 70 | static unsigned int btrfs_flags_to_ioctl(unsigned int flags) |
71 | { | 71 | { |
72 | unsigned int iflags = 0; | 72 | unsigned int iflags = 0; |
73 | 73 | ||
74 | if (flags & BTRFS_INODE_SYNC) | 74 | if (flags & BTRFS_INODE_SYNC) |
75 | iflags |= FS_SYNC_FL; | 75 | iflags |= FS_SYNC_FL; |
76 | if (flags & BTRFS_INODE_IMMUTABLE) | 76 | if (flags & BTRFS_INODE_IMMUTABLE) |
77 | iflags |= FS_IMMUTABLE_FL; | 77 | iflags |= FS_IMMUTABLE_FL; |
78 | if (flags & BTRFS_INODE_APPEND) | 78 | if (flags & BTRFS_INODE_APPEND) |
79 | iflags |= FS_APPEND_FL; | 79 | iflags |= FS_APPEND_FL; |
80 | if (flags & BTRFS_INODE_NODUMP) | 80 | if (flags & BTRFS_INODE_NODUMP) |
81 | iflags |= FS_NODUMP_FL; | 81 | iflags |= FS_NODUMP_FL; |
82 | if (flags & BTRFS_INODE_NOATIME) | 82 | if (flags & BTRFS_INODE_NOATIME) |
83 | iflags |= FS_NOATIME_FL; | 83 | iflags |= FS_NOATIME_FL; |
84 | if (flags & BTRFS_INODE_DIRSYNC) | 84 | if (flags & BTRFS_INODE_DIRSYNC) |
85 | iflags |= FS_DIRSYNC_FL; | 85 | iflags |= FS_DIRSYNC_FL; |
86 | if (flags & BTRFS_INODE_NODATACOW) | 86 | if (flags & BTRFS_INODE_NODATACOW) |
87 | iflags |= FS_NOCOW_FL; | 87 | iflags |= FS_NOCOW_FL; |
88 | 88 | ||
89 | if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) | 89 | if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) |
90 | iflags |= FS_COMPR_FL; | 90 | iflags |= FS_COMPR_FL; |
91 | else if (flags & BTRFS_INODE_NOCOMPRESS) | 91 | else if (flags & BTRFS_INODE_NOCOMPRESS) |
92 | iflags |= FS_NOCOMP_FL; | 92 | iflags |= FS_NOCOMP_FL; |
93 | 93 | ||
94 | return iflags; | 94 | return iflags; |
95 | } | 95 | } |
96 | 96 | ||
97 | /* | 97 | /* |
98 | * Update inode->i_flags based on the btrfs internal flags. | 98 | * Update inode->i_flags based on the btrfs internal flags. |
99 | */ | 99 | */ |
100 | void btrfs_update_iflags(struct inode *inode) | 100 | void btrfs_update_iflags(struct inode *inode) |
101 | { | 101 | { |
102 | struct btrfs_inode *ip = BTRFS_I(inode); | 102 | struct btrfs_inode *ip = BTRFS_I(inode); |
103 | 103 | ||
104 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); | 104 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); |
105 | 105 | ||
106 | if (ip->flags & BTRFS_INODE_SYNC) | 106 | if (ip->flags & BTRFS_INODE_SYNC) |
107 | inode->i_flags |= S_SYNC; | 107 | inode->i_flags |= S_SYNC; |
108 | if (ip->flags & BTRFS_INODE_IMMUTABLE) | 108 | if (ip->flags & BTRFS_INODE_IMMUTABLE) |
109 | inode->i_flags |= S_IMMUTABLE; | 109 | inode->i_flags |= S_IMMUTABLE; |
110 | if (ip->flags & BTRFS_INODE_APPEND) | 110 | if (ip->flags & BTRFS_INODE_APPEND) |
111 | inode->i_flags |= S_APPEND; | 111 | inode->i_flags |= S_APPEND; |
112 | if (ip->flags & BTRFS_INODE_NOATIME) | 112 | if (ip->flags & BTRFS_INODE_NOATIME) |
113 | inode->i_flags |= S_NOATIME; | 113 | inode->i_flags |= S_NOATIME; |
114 | if (ip->flags & BTRFS_INODE_DIRSYNC) | 114 | if (ip->flags & BTRFS_INODE_DIRSYNC) |
115 | inode->i_flags |= S_DIRSYNC; | 115 | inode->i_flags |= S_DIRSYNC; |
116 | } | 116 | } |
117 | 117 | ||
118 | /* | 118 | /* |
119 | * Inherit flags from the parent inode. | 119 | * Inherit flags from the parent inode. |
120 | * | 120 | * |
121 | * Currently only the compression flags and the cow flags are inherited. | 121 | * Currently only the compression flags and the cow flags are inherited. |
122 | */ | 122 | */ |
123 | void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | 123 | void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) |
124 | { | 124 | { |
125 | unsigned int flags; | 125 | unsigned int flags; |
126 | 126 | ||
127 | if (!dir) | 127 | if (!dir) |
128 | return; | 128 | return; |
129 | 129 | ||
130 | flags = BTRFS_I(dir)->flags; | 130 | flags = BTRFS_I(dir)->flags; |
131 | 131 | ||
132 | if (flags & BTRFS_INODE_NOCOMPRESS) { | 132 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
133 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | 133 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; |
134 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | 134 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; |
135 | } else if (flags & BTRFS_INODE_COMPRESS) { | 135 | } else if (flags & BTRFS_INODE_COMPRESS) { |
136 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | 136 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; |
137 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | 137 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; |
138 | } | 138 | } |
139 | 139 | ||
140 | if (flags & BTRFS_INODE_NODATACOW) | 140 | if (flags & BTRFS_INODE_NODATACOW) |
141 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; | 141 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
142 | 142 | ||
143 | btrfs_update_iflags(inode); | 143 | btrfs_update_iflags(inode); |
144 | } | 144 | } |
145 | 145 | ||
146 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) | 146 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) |
147 | { | 147 | { |
148 | struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode); | 148 | struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode); |
149 | unsigned int flags = btrfs_flags_to_ioctl(ip->flags); | 149 | unsigned int flags = btrfs_flags_to_ioctl(ip->flags); |
150 | 150 | ||
151 | if (copy_to_user(arg, &flags, sizeof(flags))) | 151 | if (copy_to_user(arg, &flags, sizeof(flags))) |
152 | return -EFAULT; | 152 | return -EFAULT; |
153 | return 0; | 153 | return 0; |
154 | } | 154 | } |
155 | 155 | ||
156 | static int check_flags(unsigned int flags) | 156 | static int check_flags(unsigned int flags) |
157 | { | 157 | { |
158 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ | 158 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ |
159 | FS_NOATIME_FL | FS_NODUMP_FL | \ | 159 | FS_NOATIME_FL | FS_NODUMP_FL | \ |
160 | FS_SYNC_FL | FS_DIRSYNC_FL | \ | 160 | FS_SYNC_FL | FS_DIRSYNC_FL | \ |
161 | FS_NOCOMP_FL | FS_COMPR_FL | | 161 | FS_NOCOMP_FL | FS_COMPR_FL | |
162 | FS_NOCOW_FL)) | 162 | FS_NOCOW_FL)) |
163 | return -EOPNOTSUPP; | 163 | return -EOPNOTSUPP; |
164 | 164 | ||
165 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) | 165 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) |
166 | return -EINVAL; | 166 | return -EINVAL; |
167 | 167 | ||
168 | return 0; | 168 | return 0; |
169 | } | 169 | } |
170 | 170 | ||
171 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) | 171 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) |
172 | { | 172 | { |
173 | struct inode *inode = file->f_path.dentry->d_inode; | 173 | struct inode *inode = file->f_path.dentry->d_inode; |
174 | struct btrfs_inode *ip = BTRFS_I(inode); | 174 | struct btrfs_inode *ip = BTRFS_I(inode); |
175 | struct btrfs_root *root = ip->root; | 175 | struct btrfs_root *root = ip->root; |
176 | struct btrfs_trans_handle *trans; | 176 | struct btrfs_trans_handle *trans; |
177 | unsigned int flags, oldflags; | 177 | unsigned int flags, oldflags; |
178 | int ret; | 178 | int ret; |
179 | u64 ip_oldflags; | 179 | u64 ip_oldflags; |
180 | unsigned int i_oldflags; | 180 | unsigned int i_oldflags; |
181 | 181 | ||
182 | if (btrfs_root_readonly(root)) | 182 | if (btrfs_root_readonly(root)) |
183 | return -EROFS; | 183 | return -EROFS; |
184 | 184 | ||
185 | if (copy_from_user(&flags, arg, sizeof(flags))) | 185 | if (copy_from_user(&flags, arg, sizeof(flags))) |
186 | return -EFAULT; | 186 | return -EFAULT; |
187 | 187 | ||
188 | ret = check_flags(flags); | 188 | ret = check_flags(flags); |
189 | if (ret) | 189 | if (ret) |
190 | return ret; | 190 | return ret; |
191 | 191 | ||
192 | if (!inode_owner_or_capable(inode)) | 192 | if (!inode_owner_or_capable(inode)) |
193 | return -EACCES; | 193 | return -EACCES; |
194 | 194 | ||
195 | mutex_lock(&inode->i_mutex); | 195 | mutex_lock(&inode->i_mutex); |
196 | 196 | ||
197 | ip_oldflags = ip->flags; | 197 | ip_oldflags = ip->flags; |
198 | i_oldflags = inode->i_flags; | 198 | i_oldflags = inode->i_flags; |
199 | 199 | ||
200 | flags = btrfs_mask_flags(inode->i_mode, flags); | 200 | flags = btrfs_mask_flags(inode->i_mode, flags); |
201 | oldflags = btrfs_flags_to_ioctl(ip->flags); | 201 | oldflags = btrfs_flags_to_ioctl(ip->flags); |
202 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { | 202 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { |
203 | if (!capable(CAP_LINUX_IMMUTABLE)) { | 203 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
204 | ret = -EPERM; | 204 | ret = -EPERM; |
205 | goto out_unlock; | 205 | goto out_unlock; |
206 | } | 206 | } |
207 | } | 207 | } |
208 | 208 | ||
209 | ret = mnt_want_write_file(file); | 209 | ret = mnt_want_write_file(file); |
210 | if (ret) | 210 | if (ret) |
211 | goto out_unlock; | 211 | goto out_unlock; |
212 | 212 | ||
213 | if (flags & FS_SYNC_FL) | 213 | if (flags & FS_SYNC_FL) |
214 | ip->flags |= BTRFS_INODE_SYNC; | 214 | ip->flags |= BTRFS_INODE_SYNC; |
215 | else | 215 | else |
216 | ip->flags &= ~BTRFS_INODE_SYNC; | 216 | ip->flags &= ~BTRFS_INODE_SYNC; |
217 | if (flags & FS_IMMUTABLE_FL) | 217 | if (flags & FS_IMMUTABLE_FL) |
218 | ip->flags |= BTRFS_INODE_IMMUTABLE; | 218 | ip->flags |= BTRFS_INODE_IMMUTABLE; |
219 | else | 219 | else |
220 | ip->flags &= ~BTRFS_INODE_IMMUTABLE; | 220 | ip->flags &= ~BTRFS_INODE_IMMUTABLE; |
221 | if (flags & FS_APPEND_FL) | 221 | if (flags & FS_APPEND_FL) |
222 | ip->flags |= BTRFS_INODE_APPEND; | 222 | ip->flags |= BTRFS_INODE_APPEND; |
223 | else | 223 | else |
224 | ip->flags &= ~BTRFS_INODE_APPEND; | 224 | ip->flags &= ~BTRFS_INODE_APPEND; |
225 | if (flags & FS_NODUMP_FL) | 225 | if (flags & FS_NODUMP_FL) |
226 | ip->flags |= BTRFS_INODE_NODUMP; | 226 | ip->flags |= BTRFS_INODE_NODUMP; |
227 | else | 227 | else |
228 | ip->flags &= ~BTRFS_INODE_NODUMP; | 228 | ip->flags &= ~BTRFS_INODE_NODUMP; |
229 | if (flags & FS_NOATIME_FL) | 229 | if (flags & FS_NOATIME_FL) |
230 | ip->flags |= BTRFS_INODE_NOATIME; | 230 | ip->flags |= BTRFS_INODE_NOATIME; |
231 | else | 231 | else |
232 | ip->flags &= ~BTRFS_INODE_NOATIME; | 232 | ip->flags &= ~BTRFS_INODE_NOATIME; |
233 | if (flags & FS_DIRSYNC_FL) | 233 | if (flags & FS_DIRSYNC_FL) |
234 | ip->flags |= BTRFS_INODE_DIRSYNC; | 234 | ip->flags |= BTRFS_INODE_DIRSYNC; |
235 | else | 235 | else |
236 | ip->flags &= ~BTRFS_INODE_DIRSYNC; | 236 | ip->flags &= ~BTRFS_INODE_DIRSYNC; |
237 | if (flags & FS_NOCOW_FL) | 237 | if (flags & FS_NOCOW_FL) |
238 | ip->flags |= BTRFS_INODE_NODATACOW; | 238 | ip->flags |= BTRFS_INODE_NODATACOW; |
239 | else | 239 | else |
240 | ip->flags &= ~BTRFS_INODE_NODATACOW; | 240 | ip->flags &= ~BTRFS_INODE_NODATACOW; |
241 | 241 | ||
242 | /* | 242 | /* |
243 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS | 243 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS |
244 | * flag may be changed automatically if compression code won't make | 244 | * flag may be changed automatically if compression code won't make |
245 | * things smaller. | 245 | * things smaller. |
246 | */ | 246 | */ |
247 | if (flags & FS_NOCOMP_FL) { | 247 | if (flags & FS_NOCOMP_FL) { |
248 | ip->flags &= ~BTRFS_INODE_COMPRESS; | 248 | ip->flags &= ~BTRFS_INODE_COMPRESS; |
249 | ip->flags |= BTRFS_INODE_NOCOMPRESS; | 249 | ip->flags |= BTRFS_INODE_NOCOMPRESS; |
250 | } else if (flags & FS_COMPR_FL) { | 250 | } else if (flags & FS_COMPR_FL) { |
251 | ip->flags |= BTRFS_INODE_COMPRESS; | 251 | ip->flags |= BTRFS_INODE_COMPRESS; |
252 | ip->flags &= ~BTRFS_INODE_NOCOMPRESS; | 252 | ip->flags &= ~BTRFS_INODE_NOCOMPRESS; |
253 | } else { | 253 | } else { |
254 | ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); | 254 | ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); |
255 | } | 255 | } |
256 | 256 | ||
257 | trans = btrfs_start_transaction(root, 1); | 257 | trans = btrfs_start_transaction(root, 1); |
258 | if (IS_ERR(trans)) { | 258 | if (IS_ERR(trans)) { |
259 | ret = PTR_ERR(trans); | 259 | ret = PTR_ERR(trans); |
260 | goto out_drop; | 260 | goto out_drop; |
261 | } | 261 | } |
262 | 262 | ||
263 | btrfs_update_iflags(inode); | 263 | btrfs_update_iflags(inode); |
264 | inode->i_ctime = CURRENT_TIME; | 264 | inode->i_ctime = CURRENT_TIME; |
265 | ret = btrfs_update_inode(trans, root, inode); | 265 | ret = btrfs_update_inode(trans, root, inode); |
266 | 266 | ||
267 | btrfs_end_transaction(trans, root); | 267 | btrfs_end_transaction(trans, root); |
268 | out_drop: | 268 | out_drop: |
269 | if (ret) { | 269 | if (ret) { |
270 | ip->flags = ip_oldflags; | 270 | ip->flags = ip_oldflags; |
271 | inode->i_flags = i_oldflags; | 271 | inode->i_flags = i_oldflags; |
272 | } | 272 | } |
273 | 273 | ||
274 | mnt_drop_write_file(file); | 274 | mnt_drop_write_file(file); |
275 | out_unlock: | 275 | out_unlock: |
276 | mutex_unlock(&inode->i_mutex); | 276 | mutex_unlock(&inode->i_mutex); |
277 | return ret; | 277 | return ret; |
278 | } | 278 | } |
279 | 279 | ||
280 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) | 280 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) |
281 | { | 281 | { |
282 | struct inode *inode = file->f_path.dentry->d_inode; | 282 | struct inode *inode = file->f_path.dentry->d_inode; |
283 | 283 | ||
284 | return put_user(inode->i_generation, arg); | 284 | return put_user(inode->i_generation, arg); |
285 | } | 285 | } |
286 | 286 | ||
287 | static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) | 287 | static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) |
288 | { | 288 | { |
289 | struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb); | 289 | struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb); |
290 | struct btrfs_device *device; | 290 | struct btrfs_device *device; |
291 | struct request_queue *q; | 291 | struct request_queue *q; |
292 | struct fstrim_range range; | 292 | struct fstrim_range range; |
293 | u64 minlen = ULLONG_MAX; | 293 | u64 minlen = ULLONG_MAX; |
294 | u64 num_devices = 0; | 294 | u64 num_devices = 0; |
295 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); | 295 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
296 | int ret; | 296 | int ret; |
297 | 297 | ||
298 | if (!capable(CAP_SYS_ADMIN)) | 298 | if (!capable(CAP_SYS_ADMIN)) |
299 | return -EPERM; | 299 | return -EPERM; |
300 | 300 | ||
301 | rcu_read_lock(); | 301 | rcu_read_lock(); |
302 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, | 302 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, |
303 | dev_list) { | 303 | dev_list) { |
304 | if (!device->bdev) | 304 | if (!device->bdev) |
305 | continue; | 305 | continue; |
306 | q = bdev_get_queue(device->bdev); | 306 | q = bdev_get_queue(device->bdev); |
307 | if (blk_queue_discard(q)) { | 307 | if (blk_queue_discard(q)) { |
308 | num_devices++; | 308 | num_devices++; |
309 | minlen = min((u64)q->limits.discard_granularity, | 309 | minlen = min((u64)q->limits.discard_granularity, |
310 | minlen); | 310 | minlen); |
311 | } | 311 | } |
312 | } | 312 | } |
313 | rcu_read_unlock(); | 313 | rcu_read_unlock(); |
314 | 314 | ||
315 | if (!num_devices) | 315 | if (!num_devices) |
316 | return -EOPNOTSUPP; | 316 | return -EOPNOTSUPP; |
317 | if (copy_from_user(&range, arg, sizeof(range))) | 317 | if (copy_from_user(&range, arg, sizeof(range))) |
318 | return -EFAULT; | 318 | return -EFAULT; |
319 | if (range.start > total_bytes) | 319 | if (range.start > total_bytes) |
320 | return -EINVAL; | 320 | return -EINVAL; |
321 | 321 | ||
322 | range.len = min(range.len, total_bytes - range.start); | 322 | range.len = min(range.len, total_bytes - range.start); |
323 | range.minlen = max(range.minlen, minlen); | 323 | range.minlen = max(range.minlen, minlen); |
324 | ret = btrfs_trim_fs(fs_info->tree_root, &range); | 324 | ret = btrfs_trim_fs(fs_info->tree_root, &range); |
325 | if (ret < 0) | 325 | if (ret < 0) |
326 | return ret; | 326 | return ret; |
327 | 327 | ||
328 | if (copy_to_user(arg, &range, sizeof(range))) | 328 | if (copy_to_user(arg, &range, sizeof(range))) |
329 | return -EFAULT; | 329 | return -EFAULT; |
330 | 330 | ||
331 | return 0; | 331 | return 0; |
332 | } | 332 | } |
333 | 333 | ||
334 | static noinline int create_subvol(struct btrfs_root *root, | 334 | static noinline int create_subvol(struct btrfs_root *root, |
335 | struct dentry *dentry, | 335 | struct dentry *dentry, |
336 | char *name, int namelen, | 336 | char *name, int namelen, |
337 | u64 *async_transid) | 337 | u64 *async_transid) |
338 | { | 338 | { |
339 | struct btrfs_trans_handle *trans; | 339 | struct btrfs_trans_handle *trans; |
340 | struct btrfs_key key; | 340 | struct btrfs_key key; |
341 | struct btrfs_root_item root_item; | 341 | struct btrfs_root_item root_item; |
342 | struct btrfs_inode_item *inode_item; | 342 | struct btrfs_inode_item *inode_item; |
343 | struct extent_buffer *leaf; | 343 | struct extent_buffer *leaf; |
344 | struct btrfs_root *new_root; | 344 | struct btrfs_root *new_root; |
345 | struct dentry *parent = dentry->d_parent; | 345 | struct dentry *parent = dentry->d_parent; |
346 | struct inode *dir; | 346 | struct inode *dir; |
347 | int ret; | 347 | int ret; |
348 | int err; | 348 | int err; |
349 | u64 objectid; | 349 | u64 objectid; |
350 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | 350 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
351 | u64 index = 0; | 351 | u64 index = 0; |
352 | 352 | ||
353 | ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); | 353 | ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); |
354 | if (ret) | 354 | if (ret) |
355 | return ret; | 355 | return ret; |
356 | 356 | ||
357 | dir = parent->d_inode; | 357 | dir = parent->d_inode; |
358 | 358 | ||
359 | /* | 359 | /* |
360 | * 1 - inode item | 360 | * 1 - inode item |
361 | * 2 - refs | 361 | * 2 - refs |
362 | * 1 - root item | 362 | * 1 - root item |
363 | * 2 - dir items | 363 | * 2 - dir items |
364 | */ | 364 | */ |
365 | trans = btrfs_start_transaction(root, 6); | 365 | trans = btrfs_start_transaction(root, 6); |
366 | if (IS_ERR(trans)) | 366 | if (IS_ERR(trans)) |
367 | return PTR_ERR(trans); | 367 | return PTR_ERR(trans); |
368 | 368 | ||
369 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, | 369 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
370 | 0, objectid, NULL, 0, 0, 0, 0); | 370 | 0, objectid, NULL, 0, 0, 0, 0); |
371 | if (IS_ERR(leaf)) { | 371 | if (IS_ERR(leaf)) { |
372 | ret = PTR_ERR(leaf); | 372 | ret = PTR_ERR(leaf); |
373 | goto fail; | 373 | goto fail; |
374 | } | 374 | } |
375 | 375 | ||
376 | memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); | 376 | memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); |
377 | btrfs_set_header_bytenr(leaf, leaf->start); | 377 | btrfs_set_header_bytenr(leaf, leaf->start); |
378 | btrfs_set_header_generation(leaf, trans->transid); | 378 | btrfs_set_header_generation(leaf, trans->transid); |
379 | btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); | 379 | btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); |
380 | btrfs_set_header_owner(leaf, objectid); | 380 | btrfs_set_header_owner(leaf, objectid); |
381 | 381 | ||
382 | write_extent_buffer(leaf, root->fs_info->fsid, | 382 | write_extent_buffer(leaf, root->fs_info->fsid, |
383 | (unsigned long)btrfs_header_fsid(leaf), | 383 | (unsigned long)btrfs_header_fsid(leaf), |
384 | BTRFS_FSID_SIZE); | 384 | BTRFS_FSID_SIZE); |
385 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, | 385 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
386 | (unsigned long)btrfs_header_chunk_tree_uuid(leaf), | 386 | (unsigned long)btrfs_header_chunk_tree_uuid(leaf), |
387 | BTRFS_UUID_SIZE); | 387 | BTRFS_UUID_SIZE); |
388 | btrfs_mark_buffer_dirty(leaf); | 388 | btrfs_mark_buffer_dirty(leaf); |
389 | 389 | ||
390 | inode_item = &root_item.inode; | 390 | inode_item = &root_item.inode; |
391 | memset(inode_item, 0, sizeof(*inode_item)); | 391 | memset(inode_item, 0, sizeof(*inode_item)); |
392 | inode_item->generation = cpu_to_le64(1); | 392 | inode_item->generation = cpu_to_le64(1); |
393 | inode_item->size = cpu_to_le64(3); | 393 | inode_item->size = cpu_to_le64(3); |
394 | inode_item->nlink = cpu_to_le32(1); | 394 | inode_item->nlink = cpu_to_le32(1); |
395 | inode_item->nbytes = cpu_to_le64(root->leafsize); | 395 | inode_item->nbytes = cpu_to_le64(root->leafsize); |
396 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); | 396 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
397 | 397 | ||
398 | root_item.flags = 0; | 398 | root_item.flags = 0; |
399 | root_item.byte_limit = 0; | 399 | root_item.byte_limit = 0; |
400 | inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT); | 400 | inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT); |
401 | 401 | ||
402 | btrfs_set_root_bytenr(&root_item, leaf->start); | 402 | btrfs_set_root_bytenr(&root_item, leaf->start); |
403 | btrfs_set_root_generation(&root_item, trans->transid); | 403 | btrfs_set_root_generation(&root_item, trans->transid); |
404 | btrfs_set_root_level(&root_item, 0); | 404 | btrfs_set_root_level(&root_item, 0); |
405 | btrfs_set_root_refs(&root_item, 1); | 405 | btrfs_set_root_refs(&root_item, 1); |
406 | btrfs_set_root_used(&root_item, leaf->len); | 406 | btrfs_set_root_used(&root_item, leaf->len); |
407 | btrfs_set_root_last_snapshot(&root_item, 0); | 407 | btrfs_set_root_last_snapshot(&root_item, 0); |
408 | 408 | ||
409 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); | 409 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
410 | root_item.drop_level = 0; | 410 | root_item.drop_level = 0; |
411 | 411 | ||
412 | btrfs_tree_unlock(leaf); | 412 | btrfs_tree_unlock(leaf); |
413 | free_extent_buffer(leaf); | 413 | free_extent_buffer(leaf); |
414 | leaf = NULL; | 414 | leaf = NULL; |
415 | 415 | ||
416 | btrfs_set_root_dirid(&root_item, new_dirid); | 416 | btrfs_set_root_dirid(&root_item, new_dirid); |
417 | 417 | ||
418 | key.objectid = objectid; | 418 | key.objectid = objectid; |
419 | key.offset = 0; | 419 | key.offset = 0; |
420 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); | 420 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
421 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | 421 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
422 | &root_item); | 422 | &root_item); |
423 | if (ret) | 423 | if (ret) |
424 | goto fail; | 424 | goto fail; |
425 | 425 | ||
426 | key.offset = (u64)-1; | 426 | key.offset = (u64)-1; |
427 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); | 427 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); |
428 | if (IS_ERR(new_root)) { | 428 | if (IS_ERR(new_root)) { |
429 | btrfs_abort_transaction(trans, root, PTR_ERR(new_root)); | 429 | btrfs_abort_transaction(trans, root, PTR_ERR(new_root)); |
430 | ret = PTR_ERR(new_root); | 430 | ret = PTR_ERR(new_root); |
431 | goto fail; | 431 | goto fail; |
432 | } | 432 | } |
433 | 433 | ||
434 | btrfs_record_root_in_trans(trans, new_root); | 434 | btrfs_record_root_in_trans(trans, new_root); |
435 | 435 | ||
436 | ret = btrfs_create_subvol_root(trans, new_root, new_dirid); | 436 | ret = btrfs_create_subvol_root(trans, new_root, new_dirid); |
437 | if (ret) { | 437 | if (ret) { |
438 | /* We potentially lose an unused inode item here */ | 438 | /* We potentially lose an unused inode item here */ |
439 | btrfs_abort_transaction(trans, root, ret); | 439 | btrfs_abort_transaction(trans, root, ret); |
440 | goto fail; | 440 | goto fail; |
441 | } | 441 | } |
442 | 442 | ||
443 | /* | 443 | /* |
444 | * insert the directory item | 444 | * insert the directory item |
445 | */ | 445 | */ |
446 | ret = btrfs_set_inode_index(dir, &index); | 446 | ret = btrfs_set_inode_index(dir, &index); |
447 | if (ret) { | 447 | if (ret) { |
448 | btrfs_abort_transaction(trans, root, ret); | 448 | btrfs_abort_transaction(trans, root, ret); |
449 | goto fail; | 449 | goto fail; |
450 | } | 450 | } |
451 | 451 | ||
452 | ret = btrfs_insert_dir_item(trans, root, | 452 | ret = btrfs_insert_dir_item(trans, root, |
453 | name, namelen, dir, &key, | 453 | name, namelen, dir, &key, |
454 | BTRFS_FT_DIR, index); | 454 | BTRFS_FT_DIR, index); |
455 | if (ret) { | 455 | if (ret) { |
456 | btrfs_abort_transaction(trans, root, ret); | 456 | btrfs_abort_transaction(trans, root, ret); |
457 | goto fail; | 457 | goto fail; |
458 | } | 458 | } |
459 | 459 | ||
460 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); | 460 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
461 | ret = btrfs_update_inode(trans, root, dir); | 461 | ret = btrfs_update_inode(trans, root, dir); |
462 | BUG_ON(ret); | 462 | BUG_ON(ret); |
463 | 463 | ||
464 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, | 464 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
465 | objectid, root->root_key.objectid, | 465 | objectid, root->root_key.objectid, |
466 | btrfs_ino(dir), index, name, namelen); | 466 | btrfs_ino(dir), index, name, namelen); |
467 | 467 | ||
468 | BUG_ON(ret); | 468 | BUG_ON(ret); |
469 | 469 | ||
470 | d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); | 470 | d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); |
471 | fail: | 471 | fail: |
472 | if (async_transid) { | 472 | if (async_transid) { |
473 | *async_transid = trans->transid; | 473 | *async_transid = trans->transid; |
474 | err = btrfs_commit_transaction_async(trans, root, 1); | 474 | err = btrfs_commit_transaction_async(trans, root, 1); |
475 | } else { | 475 | } else { |
476 | err = btrfs_commit_transaction(trans, root); | 476 | err = btrfs_commit_transaction(trans, root); |
477 | } | 477 | } |
478 | if (err && !ret) | 478 | if (err && !ret) |
479 | ret = err; | 479 | ret = err; |
480 | return ret; | 480 | return ret; |
481 | } | 481 | } |
482 | 482 | ||
483 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, | 483 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, |
484 | char *name, int namelen, u64 *async_transid, | 484 | char *name, int namelen, u64 *async_transid, |
485 | bool readonly) | 485 | bool readonly) |
486 | { | 486 | { |
487 | struct inode *inode; | 487 | struct inode *inode; |
488 | struct btrfs_pending_snapshot *pending_snapshot; | 488 | struct btrfs_pending_snapshot *pending_snapshot; |
489 | struct btrfs_trans_handle *trans; | 489 | struct btrfs_trans_handle *trans; |
490 | int ret; | 490 | int ret; |
491 | 491 | ||
492 | if (!root->ref_cows) | 492 | if (!root->ref_cows) |
493 | return -EINVAL; | 493 | return -EINVAL; |
494 | 494 | ||
495 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); | 495 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
496 | if (!pending_snapshot) | 496 | if (!pending_snapshot) |
497 | return -ENOMEM; | 497 | return -ENOMEM; |
498 | 498 | ||
499 | btrfs_init_block_rsv(&pending_snapshot->block_rsv); | 499 | btrfs_init_block_rsv(&pending_snapshot->block_rsv); |
500 | pending_snapshot->dentry = dentry; | 500 | pending_snapshot->dentry = dentry; |
501 | pending_snapshot->root = root; | 501 | pending_snapshot->root = root; |
502 | pending_snapshot->readonly = readonly; | 502 | pending_snapshot->readonly = readonly; |
503 | 503 | ||
504 | trans = btrfs_start_transaction(root->fs_info->extent_root, 5); | 504 | trans = btrfs_start_transaction(root->fs_info->extent_root, 5); |
505 | if (IS_ERR(trans)) { | 505 | if (IS_ERR(trans)) { |
506 | ret = PTR_ERR(trans); | 506 | ret = PTR_ERR(trans); |
507 | goto fail; | 507 | goto fail; |
508 | } | 508 | } |
509 | 509 | ||
510 | ret = btrfs_snap_reserve_metadata(trans, pending_snapshot); | 510 | ret = btrfs_snap_reserve_metadata(trans, pending_snapshot); |
511 | BUG_ON(ret); | 511 | BUG_ON(ret); |
512 | 512 | ||
513 | spin_lock(&root->fs_info->trans_lock); | 513 | spin_lock(&root->fs_info->trans_lock); |
514 | list_add(&pending_snapshot->list, | 514 | list_add(&pending_snapshot->list, |
515 | &trans->transaction->pending_snapshots); | 515 | &trans->transaction->pending_snapshots); |
516 | spin_unlock(&root->fs_info->trans_lock); | 516 | spin_unlock(&root->fs_info->trans_lock); |
517 | if (async_transid) { | 517 | if (async_transid) { |
518 | *async_transid = trans->transid; | 518 | *async_transid = trans->transid; |
519 | ret = btrfs_commit_transaction_async(trans, | 519 | ret = btrfs_commit_transaction_async(trans, |
520 | root->fs_info->extent_root, 1); | 520 | root->fs_info->extent_root, 1); |
521 | } else { | 521 | } else { |
522 | ret = btrfs_commit_transaction(trans, | 522 | ret = btrfs_commit_transaction(trans, |
523 | root->fs_info->extent_root); | 523 | root->fs_info->extent_root); |
524 | } | 524 | } |
525 | BUG_ON(ret); | 525 | BUG_ON(ret); |
526 | 526 | ||
527 | ret = pending_snapshot->error; | 527 | ret = pending_snapshot->error; |
528 | if (ret) | 528 | if (ret) |
529 | goto fail; | 529 | goto fail; |
530 | 530 | ||
531 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); | 531 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
532 | if (ret) | 532 | if (ret) |
533 | goto fail; | 533 | goto fail; |
534 | 534 | ||
535 | inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); | 535 | inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); |
536 | if (IS_ERR(inode)) { | 536 | if (IS_ERR(inode)) { |
537 | ret = PTR_ERR(inode); | 537 | ret = PTR_ERR(inode); |
538 | goto fail; | 538 | goto fail; |
539 | } | 539 | } |
540 | BUG_ON(!inode); | 540 | BUG_ON(!inode); |
541 | d_instantiate(dentry, inode); | 541 | d_instantiate(dentry, inode); |
542 | ret = 0; | 542 | ret = 0; |
543 | fail: | 543 | fail: |
544 | kfree(pending_snapshot); | 544 | kfree(pending_snapshot); |
545 | return ret; | 545 | return ret; |
546 | } | 546 | } |
547 | 547 | ||
548 | /* copy of check_sticky in fs/namei.c() | 548 | /* copy of check_sticky in fs/namei.c() |
549 | * It's inline, so penalty for filesystems that don't use sticky bit is | 549 | * It's inline, so penalty for filesystems that don't use sticky bit is |
550 | * minimal. | 550 | * minimal. |
551 | */ | 551 | */ |
552 | static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) | 552 | static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) |
553 | { | 553 | { |
554 | uid_t fsuid = current_fsuid(); | 554 | uid_t fsuid = current_fsuid(); |
555 | 555 | ||
556 | if (!(dir->i_mode & S_ISVTX)) | 556 | if (!(dir->i_mode & S_ISVTX)) |
557 | return 0; | 557 | return 0; |
558 | if (inode->i_uid == fsuid) | 558 | if (inode->i_uid == fsuid) |
559 | return 0; | 559 | return 0; |
560 | if (dir->i_uid == fsuid) | 560 | if (dir->i_uid == fsuid) |
561 | return 0; | 561 | return 0; |
562 | return !capable(CAP_FOWNER); | 562 | return !capable(CAP_FOWNER); |
563 | } | 563 | } |
564 | 564 | ||
565 | /* copy of may_delete in fs/namei.c() | 565 | /* copy of may_delete in fs/namei.c() |
566 | * Check whether we can remove a link victim from directory dir, check | 566 | * Check whether we can remove a link victim from directory dir, check |
567 | * whether the type of victim is right. | 567 | * whether the type of victim is right. |
568 | * 1. We can't do it if dir is read-only (done in permission()) | 568 | * 1. We can't do it if dir is read-only (done in permission()) |
569 | * 2. We should have write and exec permissions on dir | 569 | * 2. We should have write and exec permissions on dir |
570 | * 3. We can't remove anything from append-only dir | 570 | * 3. We can't remove anything from append-only dir |
571 | * 4. We can't do anything with immutable dir (done in permission()) | 571 | * 4. We can't do anything with immutable dir (done in permission()) |
572 | * 5. If the sticky bit on dir is set we should either | 572 | * 5. If the sticky bit on dir is set we should either |
573 | * a. be owner of dir, or | 573 | * a. be owner of dir, or |
574 | * b. be owner of victim, or | 574 | * b. be owner of victim, or |
575 | * c. have CAP_FOWNER capability | 575 | * c. have CAP_FOWNER capability |
576 | * 6. If the victim is append-only or immutable we can't do antyhing with | 576 | * 6. If the victim is append-only or immutable we can't do antyhing with |
577 | * links pointing to it. | 577 | * links pointing to it. |
578 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. | 578 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
579 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. | 579 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
580 | * 9. We can't remove a root or mountpoint. | 580 | * 9. We can't remove a root or mountpoint. |
581 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by | 581 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by |
582 | * nfs_async_unlink(). | 582 | * nfs_async_unlink(). |
583 | */ | 583 | */ |
584 | 584 | ||
585 | static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) | 585 | static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) |
586 | { | 586 | { |
587 | int error; | 587 | int error; |
588 | 588 | ||
589 | if (!victim->d_inode) | 589 | if (!victim->d_inode) |
590 | return -ENOENT; | 590 | return -ENOENT; |
591 | 591 | ||
592 | BUG_ON(victim->d_parent->d_inode != dir); | 592 | BUG_ON(victim->d_parent->d_inode != dir); |
593 | audit_inode_child(victim, dir); | 593 | audit_inode_child(victim, dir); |
594 | 594 | ||
595 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); | 595 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
596 | if (error) | 596 | if (error) |
597 | return error; | 597 | return error; |
598 | if (IS_APPEND(dir)) | 598 | if (IS_APPEND(dir)) |
599 | return -EPERM; | 599 | return -EPERM; |
600 | if (btrfs_check_sticky(dir, victim->d_inode)|| | 600 | if (btrfs_check_sticky(dir, victim->d_inode)|| |
601 | IS_APPEND(victim->d_inode)|| | 601 | IS_APPEND(victim->d_inode)|| |
602 | IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) | 602 | IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) |
603 | return -EPERM; | 603 | return -EPERM; |
604 | if (isdir) { | 604 | if (isdir) { |
605 | if (!S_ISDIR(victim->d_inode->i_mode)) | 605 | if (!S_ISDIR(victim->d_inode->i_mode)) |
606 | return -ENOTDIR; | 606 | return -ENOTDIR; |
607 | if (IS_ROOT(victim)) | 607 | if (IS_ROOT(victim)) |
608 | return -EBUSY; | 608 | return -EBUSY; |
609 | } else if (S_ISDIR(victim->d_inode->i_mode)) | 609 | } else if (S_ISDIR(victim->d_inode->i_mode)) |
610 | return -EISDIR; | 610 | return -EISDIR; |
611 | if (IS_DEADDIR(dir)) | 611 | if (IS_DEADDIR(dir)) |
612 | return -ENOENT; | 612 | return -ENOENT; |
613 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | 613 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
614 | return -EBUSY; | 614 | return -EBUSY; |
615 | return 0; | 615 | return 0; |
616 | } | 616 | } |
617 | 617 | ||
618 | /* copy of may_create in fs/namei.c() */ | 618 | /* copy of may_create in fs/namei.c() */ |
619 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) | 619 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) |
620 | { | 620 | { |
621 | if (child->d_inode) | 621 | if (child->d_inode) |
622 | return -EEXIST; | 622 | return -EEXIST; |
623 | if (IS_DEADDIR(dir)) | 623 | if (IS_DEADDIR(dir)) |
624 | return -ENOENT; | 624 | return -ENOENT; |
625 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); | 625 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
626 | } | 626 | } |
627 | 627 | ||
628 | /* | 628 | /* |
629 | * Create a new subvolume below @parent. This is largely modeled after | 629 | * Create a new subvolume below @parent. This is largely modeled after |
630 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup | 630 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup |
631 | * inside this filesystem so it's quite a bit simpler. | 631 | * inside this filesystem so it's quite a bit simpler. |
632 | */ | 632 | */ |
633 | static noinline int btrfs_mksubvol(struct path *parent, | 633 | static noinline int btrfs_mksubvol(struct path *parent, |
634 | char *name, int namelen, | 634 | char *name, int namelen, |
635 | struct btrfs_root *snap_src, | 635 | struct btrfs_root *snap_src, |
636 | u64 *async_transid, bool readonly) | 636 | u64 *async_transid, bool readonly) |
637 | { | 637 | { |
638 | struct inode *dir = parent->dentry->d_inode; | 638 | struct inode *dir = parent->dentry->d_inode; |
639 | struct dentry *dentry; | 639 | struct dentry *dentry; |
640 | int error; | 640 | int error; |
641 | 641 | ||
642 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); | 642 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
643 | 643 | ||
644 | dentry = lookup_one_len(name, parent->dentry, namelen); | 644 | dentry = lookup_one_len(name, parent->dentry, namelen); |
645 | error = PTR_ERR(dentry); | 645 | error = PTR_ERR(dentry); |
646 | if (IS_ERR(dentry)) | 646 | if (IS_ERR(dentry)) |
647 | goto out_unlock; | 647 | goto out_unlock; |
648 | 648 | ||
649 | error = -EEXIST; | 649 | error = -EEXIST; |
650 | if (dentry->d_inode) | 650 | if (dentry->d_inode) |
651 | goto out_dput; | 651 | goto out_dput; |
652 | 652 | ||
653 | error = mnt_want_write(parent->mnt); | 653 | error = mnt_want_write(parent->mnt); |
654 | if (error) | 654 | if (error) |
655 | goto out_dput; | 655 | goto out_dput; |
656 | 656 | ||
657 | error = btrfs_may_create(dir, dentry); | 657 | error = btrfs_may_create(dir, dentry); |
658 | if (error) | 658 | if (error) |
659 | goto out_drop_write; | 659 | goto out_drop_write; |
660 | 660 | ||
661 | down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); | 661 | down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
662 | 662 | ||
663 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) | 663 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) |
664 | goto out_up_read; | 664 | goto out_up_read; |
665 | 665 | ||
666 | if (snap_src) { | 666 | if (snap_src) { |
667 | error = create_snapshot(snap_src, dentry, | 667 | error = create_snapshot(snap_src, dentry, |
668 | name, namelen, async_transid, readonly); | 668 | name, namelen, async_transid, readonly); |
669 | } else { | 669 | } else { |
670 | error = create_subvol(BTRFS_I(dir)->root, dentry, | 670 | error = create_subvol(BTRFS_I(dir)->root, dentry, |
671 | name, namelen, async_transid); | 671 | name, namelen, async_transid); |
672 | } | 672 | } |
673 | if (!error) | 673 | if (!error) |
674 | fsnotify_mkdir(dir, dentry); | 674 | fsnotify_mkdir(dir, dentry); |
675 | out_up_read: | 675 | out_up_read: |
676 | up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); | 676 | up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
677 | out_drop_write: | 677 | out_drop_write: |
678 | mnt_drop_write(parent->mnt); | 678 | mnt_drop_write(parent->mnt); |
679 | out_dput: | 679 | out_dput: |
680 | dput(dentry); | 680 | dput(dentry); |
681 | out_unlock: | 681 | out_unlock: |
682 | mutex_unlock(&dir->i_mutex); | 682 | mutex_unlock(&dir->i_mutex); |
683 | return error; | 683 | return error; |
684 | } | 684 | } |
685 | 685 | ||
686 | /* | 686 | /* |
687 | * When we're defragging a range, we don't want to kick it off again | 687 | * When we're defragging a range, we don't want to kick it off again |
688 | * if it is really just waiting for delalloc to send it down. | 688 | * if it is really just waiting for delalloc to send it down. |
689 | * If we find a nice big extent or delalloc range for the bytes in the | 689 | * If we find a nice big extent or delalloc range for the bytes in the |
690 | * file you want to defrag, we return 0 to let you know to skip this | 690 | * file you want to defrag, we return 0 to let you know to skip this |
691 | * part of the file | 691 | * part of the file |
692 | */ | 692 | */ |
693 | static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) | 693 | static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) |
694 | { | 694 | { |
695 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | 695 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
696 | struct extent_map *em = NULL; | 696 | struct extent_map *em = NULL; |
697 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | 697 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
698 | u64 end; | 698 | u64 end; |
699 | 699 | ||
700 | read_lock(&em_tree->lock); | 700 | read_lock(&em_tree->lock); |
701 | em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); | 701 | em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); |
702 | read_unlock(&em_tree->lock); | 702 | read_unlock(&em_tree->lock); |
703 | 703 | ||
704 | if (em) { | 704 | if (em) { |
705 | end = extent_map_end(em); | 705 | end = extent_map_end(em); |
706 | free_extent_map(em); | 706 | free_extent_map(em); |
707 | if (end - offset > thresh) | 707 | if (end - offset > thresh) |
708 | return 0; | 708 | return 0; |
709 | } | 709 | } |
710 | /* if we already have a nice delalloc here, just stop */ | 710 | /* if we already have a nice delalloc here, just stop */ |
711 | thresh /= 2; | 711 | thresh /= 2; |
712 | end = count_range_bits(io_tree, &offset, offset + thresh, | 712 | end = count_range_bits(io_tree, &offset, offset + thresh, |
713 | thresh, EXTENT_DELALLOC, 1); | 713 | thresh, EXTENT_DELALLOC, 1); |
714 | if (end >= thresh) | 714 | if (end >= thresh) |
715 | return 0; | 715 | return 0; |
716 | return 1; | 716 | return 1; |
717 | } | 717 | } |
718 | 718 | ||
719 | /* | 719 | /* |
720 | * helper function to walk through a file and find extents | 720 | * helper function to walk through a file and find extents |
721 | * newer than a specific transid, and smaller than thresh. | 721 | * newer than a specific transid, and smaller than thresh. |
722 | * | 722 | * |
723 | * This is used by the defragging code to find new and small | 723 | * This is used by the defragging code to find new and small |
724 | * extents | 724 | * extents |
725 | */ | 725 | */ |
726 | static int find_new_extents(struct btrfs_root *root, | 726 | static int find_new_extents(struct btrfs_root *root, |
727 | struct inode *inode, u64 newer_than, | 727 | struct inode *inode, u64 newer_than, |
728 | u64 *off, int thresh) | 728 | u64 *off, int thresh) |
729 | { | 729 | { |
730 | struct btrfs_path *path; | 730 | struct btrfs_path *path; |
731 | struct btrfs_key min_key; | 731 | struct btrfs_key min_key; |
732 | struct btrfs_key max_key; | 732 | struct btrfs_key max_key; |
733 | struct extent_buffer *leaf; | 733 | struct extent_buffer *leaf; |
734 | struct btrfs_file_extent_item *extent; | 734 | struct btrfs_file_extent_item *extent; |
735 | int type; | 735 | int type; |
736 | int ret; | 736 | int ret; |
737 | u64 ino = btrfs_ino(inode); | 737 | u64 ino = btrfs_ino(inode); |
738 | 738 | ||
739 | path = btrfs_alloc_path(); | 739 | path = btrfs_alloc_path(); |
740 | if (!path) | 740 | if (!path) |
741 | return -ENOMEM; | 741 | return -ENOMEM; |
742 | 742 | ||
743 | min_key.objectid = ino; | 743 | min_key.objectid = ino; |
744 | min_key.type = BTRFS_EXTENT_DATA_KEY; | 744 | min_key.type = BTRFS_EXTENT_DATA_KEY; |
745 | min_key.offset = *off; | 745 | min_key.offset = *off; |
746 | 746 | ||
747 | max_key.objectid = ino; | 747 | max_key.objectid = ino; |
748 | max_key.type = (u8)-1; | 748 | max_key.type = (u8)-1; |
749 | max_key.offset = (u64)-1; | 749 | max_key.offset = (u64)-1; |
750 | 750 | ||
751 | path->keep_locks = 1; | 751 | path->keep_locks = 1; |
752 | 752 | ||
753 | while(1) { | 753 | while(1) { |
754 | ret = btrfs_search_forward(root, &min_key, &max_key, | 754 | ret = btrfs_search_forward(root, &min_key, &max_key, |
755 | path, 0, newer_than); | 755 | path, 0, newer_than); |
756 | if (ret != 0) | 756 | if (ret != 0) |
757 | goto none; | 757 | goto none; |
758 | if (min_key.objectid != ino) | 758 | if (min_key.objectid != ino) |
759 | goto none; | 759 | goto none; |
760 | if (min_key.type != BTRFS_EXTENT_DATA_KEY) | 760 | if (min_key.type != BTRFS_EXTENT_DATA_KEY) |
761 | goto none; | 761 | goto none; |
762 | 762 | ||
763 | leaf = path->nodes[0]; | 763 | leaf = path->nodes[0]; |
764 | extent = btrfs_item_ptr(leaf, path->slots[0], | 764 | extent = btrfs_item_ptr(leaf, path->slots[0], |
765 | struct btrfs_file_extent_item); | 765 | struct btrfs_file_extent_item); |
766 | 766 | ||
767 | type = btrfs_file_extent_type(leaf, extent); | 767 | type = btrfs_file_extent_type(leaf, extent); |
768 | if (type == BTRFS_FILE_EXTENT_REG && | 768 | if (type == BTRFS_FILE_EXTENT_REG && |
769 | btrfs_file_extent_num_bytes(leaf, extent) < thresh && | 769 | btrfs_file_extent_num_bytes(leaf, extent) < thresh && |
770 | check_defrag_in_cache(inode, min_key.offset, thresh)) { | 770 | check_defrag_in_cache(inode, min_key.offset, thresh)) { |
771 | *off = min_key.offset; | 771 | *off = min_key.offset; |
772 | btrfs_free_path(path); | 772 | btrfs_free_path(path); |
773 | return 0; | 773 | return 0; |
774 | } | 774 | } |
775 | 775 | ||
776 | if (min_key.offset == (u64)-1) | 776 | if (min_key.offset == (u64)-1) |
777 | goto none; | 777 | goto none; |
778 | 778 | ||
779 | min_key.offset++; | 779 | min_key.offset++; |
780 | btrfs_release_path(path); | 780 | btrfs_release_path(path); |
781 | } | 781 | } |
782 | none: | 782 | none: |
783 | btrfs_free_path(path); | 783 | btrfs_free_path(path); |
784 | return -ENOENT; | 784 | return -ENOENT; |
785 | } | 785 | } |
786 | 786 | ||
787 | /* | 787 | /* |
788 | * Validaty check of prev em and next em: | 788 | * Validaty check of prev em and next em: |
789 | * 1) no prev/next em | 789 | * 1) no prev/next em |
790 | * 2) prev/next em is an hole/inline extent | 790 | * 2) prev/next em is an hole/inline extent |
791 | */ | 791 | */ |
792 | static int check_adjacent_extents(struct inode *inode, struct extent_map *em) | 792 | static int check_adjacent_extents(struct inode *inode, struct extent_map *em) |
793 | { | 793 | { |
794 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | 794 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
795 | struct extent_map *prev = NULL, *next = NULL; | 795 | struct extent_map *prev = NULL, *next = NULL; |
796 | int ret = 0; | 796 | int ret = 0; |
797 | 797 | ||
798 | read_lock(&em_tree->lock); | 798 | read_lock(&em_tree->lock); |
799 | prev = lookup_extent_mapping(em_tree, em->start - 1, (u64)-1); | 799 | prev = lookup_extent_mapping(em_tree, em->start - 1, (u64)-1); |
800 | next = lookup_extent_mapping(em_tree, em->start + em->len, (u64)-1); | 800 | next = lookup_extent_mapping(em_tree, em->start + em->len, (u64)-1); |
801 | read_unlock(&em_tree->lock); | 801 | read_unlock(&em_tree->lock); |
802 | 802 | ||
803 | if ((!prev || prev->block_start >= EXTENT_MAP_LAST_BYTE) && | 803 | if ((!prev || prev->block_start >= EXTENT_MAP_LAST_BYTE) && |
804 | (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)) | 804 | (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)) |
805 | ret = 1; | 805 | ret = 1; |
806 | free_extent_map(prev); | 806 | free_extent_map(prev); |
807 | free_extent_map(next); | 807 | free_extent_map(next); |
808 | 808 | ||
809 | return ret; | 809 | return ret; |
810 | } | 810 | } |
811 | 811 | ||
812 | static int should_defrag_range(struct inode *inode, u64 start, u64 len, | 812 | static int should_defrag_range(struct inode *inode, u64 start, u64 len, |
813 | int thresh, u64 *last_len, u64 *skip, | 813 | int thresh, u64 *last_len, u64 *skip, |
814 | u64 *defrag_end) | 814 | u64 *defrag_end) |
815 | { | 815 | { |
816 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | 816 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
817 | struct extent_map *em = NULL; | 817 | struct extent_map *em = NULL; |
818 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | 818 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
819 | int ret = 1; | 819 | int ret = 1; |
820 | 820 | ||
821 | /* | 821 | /* |
822 | * make sure that once we start defragging an extent, we keep on | 822 | * make sure that once we start defragging an extent, we keep on |
823 | * defragging it | 823 | * defragging it |
824 | */ | 824 | */ |
825 | if (start < *defrag_end) | 825 | if (start < *defrag_end) |
826 | return 1; | 826 | return 1; |
827 | 827 | ||
828 | *skip = 0; | 828 | *skip = 0; |
829 | 829 | ||
830 | /* | 830 | /* |
831 | * hopefully we have this extent in the tree already, try without | 831 | * hopefully we have this extent in the tree already, try without |
832 | * the full extent lock | 832 | * the full extent lock |
833 | */ | 833 | */ |
834 | read_lock(&em_tree->lock); | 834 | read_lock(&em_tree->lock); |
835 | em = lookup_extent_mapping(em_tree, start, len); | 835 | em = lookup_extent_mapping(em_tree, start, len); |
836 | read_unlock(&em_tree->lock); | 836 | read_unlock(&em_tree->lock); |
837 | 837 | ||
838 | if (!em) { | 838 | if (!em) { |
839 | /* get the big lock and read metadata off disk */ | 839 | /* get the big lock and read metadata off disk */ |
840 | lock_extent(io_tree, start, start + len - 1); | 840 | lock_extent(io_tree, start, start + len - 1); |
841 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); | 841 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
842 | unlock_extent(io_tree, start, start + len - 1); | 842 | unlock_extent(io_tree, start, start + len - 1); |
843 | 843 | ||
844 | if (IS_ERR(em)) | 844 | if (IS_ERR(em)) |
845 | return 0; | 845 | return 0; |
846 | } | 846 | } |
847 | 847 | ||
848 | /* this will cover holes, and inline extents */ | 848 | /* this will cover holes, and inline extents */ |
849 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { | 849 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
850 | ret = 0; | 850 | ret = 0; |
851 | goto out; | 851 | goto out; |
852 | } | 852 | } |
853 | 853 | ||
854 | /* If we have nothing to merge with us, just skip. */ | 854 | /* If we have nothing to merge with us, just skip. */ |
855 | if (check_adjacent_extents(inode, em)) { | 855 | if (check_adjacent_extents(inode, em)) { |
856 | ret = 0; | 856 | ret = 0; |
857 | goto out; | 857 | goto out; |
858 | } | 858 | } |
859 | 859 | ||
860 | /* | 860 | /* |
861 | * we hit a real extent, if it is big don't bother defragging it again | 861 | * we hit a real extent, if it is big don't bother defragging it again |
862 | */ | 862 | */ |
863 | if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh) | 863 | if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh) |
864 | ret = 0; | 864 | ret = 0; |
865 | 865 | ||
866 | out: | 866 | out: |
867 | /* | 867 | /* |
868 | * last_len ends up being a counter of how many bytes we've defragged. | 868 | * last_len ends up being a counter of how many bytes we've defragged. |
869 | * every time we choose not to defrag an extent, we reset *last_len | 869 | * every time we choose not to defrag an extent, we reset *last_len |
870 | * so that the next tiny extent will force a defrag. | 870 | * so that the next tiny extent will force a defrag. |
871 | * | 871 | * |
872 | * The end result of this is that tiny extents before a single big | 872 | * The end result of this is that tiny extents before a single big |
873 | * extent will force at least part of that big extent to be defragged. | 873 | * extent will force at least part of that big extent to be defragged. |
874 | */ | 874 | */ |
875 | if (ret) { | 875 | if (ret) { |
876 | *defrag_end = extent_map_end(em); | 876 | *defrag_end = extent_map_end(em); |
877 | } else { | 877 | } else { |
878 | *last_len = 0; | 878 | *last_len = 0; |
879 | *skip = extent_map_end(em); | 879 | *skip = extent_map_end(em); |
880 | *defrag_end = 0; | 880 | *defrag_end = 0; |
881 | } | 881 | } |
882 | 882 | ||
883 | free_extent_map(em); | 883 | free_extent_map(em); |
884 | return ret; | 884 | return ret; |
885 | } | 885 | } |
886 | 886 | ||
887 | /* | 887 | /* |
888 | * it doesn't do much good to defrag one or two pages | 888 | * it doesn't do much good to defrag one or two pages |
889 | * at a time. This pulls in a nice chunk of pages | 889 | * at a time. This pulls in a nice chunk of pages |
890 | * to COW and defrag. | 890 | * to COW and defrag. |
891 | * | 891 | * |
892 | * It also makes sure the delalloc code has enough | 892 | * It also makes sure the delalloc code has enough |
893 | * dirty data to avoid making new small extents as part | 893 | * dirty data to avoid making new small extents as part |
894 | * of the defrag | 894 | * of the defrag |
895 | * | 895 | * |
896 | * It's a good idea to start RA on this range | 896 | * It's a good idea to start RA on this range |
897 | * before calling this. | 897 | * before calling this. |
898 | */ | 898 | */ |
899 | static int cluster_pages_for_defrag(struct inode *inode, | 899 | static int cluster_pages_for_defrag(struct inode *inode, |
900 | struct page **pages, | 900 | struct page **pages, |
901 | unsigned long start_index, | 901 | unsigned long start_index, |
902 | int num_pages) | 902 | int num_pages) |
903 | { | 903 | { |
904 | unsigned long file_end; | 904 | unsigned long file_end; |
905 | u64 isize = i_size_read(inode); | 905 | u64 isize = i_size_read(inode); |
906 | u64 page_start; | 906 | u64 page_start; |
907 | u64 page_end; | 907 | u64 page_end; |
908 | u64 page_cnt; | 908 | u64 page_cnt; |
909 | int ret; | 909 | int ret; |
910 | int i; | 910 | int i; |
911 | int i_done; | 911 | int i_done; |
912 | struct btrfs_ordered_extent *ordered; | 912 | struct btrfs_ordered_extent *ordered; |
913 | struct extent_state *cached_state = NULL; | 913 | struct extent_state *cached_state = NULL; |
914 | struct extent_io_tree *tree; | 914 | struct extent_io_tree *tree; |
915 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); | 915 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
916 | 916 | ||
917 | file_end = (isize - 1) >> PAGE_CACHE_SHIFT; | 917 | file_end = (isize - 1) >> PAGE_CACHE_SHIFT; |
918 | if (!isize || start_index > file_end) | 918 | if (!isize || start_index > file_end) |
919 | return 0; | 919 | return 0; |
920 | 920 | ||
921 | page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); | 921 | page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); |
922 | 922 | ||
923 | ret = btrfs_delalloc_reserve_space(inode, | 923 | ret = btrfs_delalloc_reserve_space(inode, |
924 | page_cnt << PAGE_CACHE_SHIFT); | 924 | page_cnt << PAGE_CACHE_SHIFT); |
925 | if (ret) | 925 | if (ret) |
926 | return ret; | 926 | return ret; |
927 | i_done = 0; | 927 | i_done = 0; |
928 | tree = &BTRFS_I(inode)->io_tree; | 928 | tree = &BTRFS_I(inode)->io_tree; |
929 | 929 | ||
930 | /* step one, lock all the pages */ | 930 | /* step one, lock all the pages */ |
931 | for (i = 0; i < page_cnt; i++) { | 931 | for (i = 0; i < page_cnt; i++) { |
932 | struct page *page; | 932 | struct page *page; |
933 | again: | 933 | again: |
934 | page = find_or_create_page(inode->i_mapping, | 934 | page = find_or_create_page(inode->i_mapping, |
935 | start_index + i, mask); | 935 | start_index + i, mask); |
936 | if (!page) | 936 | if (!page) |
937 | break; | 937 | break; |
938 | 938 | ||
939 | page_start = page_offset(page); | 939 | page_start = page_offset(page); |
940 | page_end = page_start + PAGE_CACHE_SIZE - 1; | 940 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
941 | while (1) { | 941 | while (1) { |
942 | lock_extent(tree, page_start, page_end); | 942 | lock_extent(tree, page_start, page_end); |
943 | ordered = btrfs_lookup_ordered_extent(inode, | 943 | ordered = btrfs_lookup_ordered_extent(inode, |
944 | page_start); | 944 | page_start); |
945 | unlock_extent(tree, page_start, page_end); | 945 | unlock_extent(tree, page_start, page_end); |
946 | if (!ordered) | 946 | if (!ordered) |
947 | break; | 947 | break; |
948 | 948 | ||
949 | unlock_page(page); | 949 | unlock_page(page); |
950 | btrfs_start_ordered_extent(inode, ordered, 1); | 950 | btrfs_start_ordered_extent(inode, ordered, 1); |
951 | btrfs_put_ordered_extent(ordered); | 951 | btrfs_put_ordered_extent(ordered); |
952 | lock_page(page); | 952 | lock_page(page); |
953 | /* | 953 | /* |
954 | * we unlocked the page above, so we need check if | 954 | * we unlocked the page above, so we need check if |
955 | * it was released or not. | 955 | * it was released or not. |
956 | */ | 956 | */ |
957 | if (page->mapping != inode->i_mapping) { | 957 | if (page->mapping != inode->i_mapping) { |
958 | unlock_page(page); | 958 | unlock_page(page); |
959 | page_cache_release(page); | 959 | page_cache_release(page); |
960 | goto again; | 960 | goto again; |
961 | } | 961 | } |
962 | } | 962 | } |
963 | 963 | ||
964 | if (!PageUptodate(page)) { | 964 | if (!PageUptodate(page)) { |
965 | btrfs_readpage(NULL, page); | 965 | btrfs_readpage(NULL, page); |
966 | lock_page(page); | 966 | lock_page(page); |
967 | if (!PageUptodate(page)) { | 967 | if (!PageUptodate(page)) { |
968 | unlock_page(page); | 968 | unlock_page(page); |
969 | page_cache_release(page); | 969 | page_cache_release(page); |
970 | ret = -EIO; | 970 | ret = -EIO; |
971 | break; | 971 | break; |
972 | } | 972 | } |
973 | } | 973 | } |
974 | 974 | ||
975 | if (page->mapping != inode->i_mapping) { | 975 | if (page->mapping != inode->i_mapping) { |
976 | unlock_page(page); | 976 | unlock_page(page); |
977 | page_cache_release(page); | 977 | page_cache_release(page); |
978 | goto again; | 978 | goto again; |
979 | } | 979 | } |
980 | 980 | ||
981 | pages[i] = page; | 981 | pages[i] = page; |
982 | i_done++; | 982 | i_done++; |
983 | } | 983 | } |
984 | if (!i_done || ret) | 984 | if (!i_done || ret) |
985 | goto out; | 985 | goto out; |
986 | 986 | ||
987 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) | 987 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) |
988 | goto out; | 988 | goto out; |
989 | 989 | ||
990 | /* | 990 | /* |
991 | * so now we have a nice long stream of locked | 991 | * so now we have a nice long stream of locked |
992 | * and up to date pages, lets wait on them | 992 | * and up to date pages, lets wait on them |
993 | */ | 993 | */ |
994 | for (i = 0; i < i_done; i++) | 994 | for (i = 0; i < i_done; i++) |
995 | wait_on_page_writeback(pages[i]); | 995 | wait_on_page_writeback(pages[i]); |
996 | 996 | ||
997 | page_start = page_offset(pages[0]); | 997 | page_start = page_offset(pages[0]); |
998 | page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; | 998 | page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; |
999 | 999 | ||
1000 | lock_extent_bits(&BTRFS_I(inode)->io_tree, | 1000 | lock_extent_bits(&BTRFS_I(inode)->io_tree, |
1001 | page_start, page_end - 1, 0, &cached_state); | 1001 | page_start, page_end - 1, 0, &cached_state); |
1002 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, | 1002 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, |
1003 | page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | | 1003 | page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | |
1004 | EXTENT_DO_ACCOUNTING, 0, 0, &cached_state, | 1004 | EXTENT_DO_ACCOUNTING, 0, 0, &cached_state, |
1005 | GFP_NOFS); | 1005 | GFP_NOFS); |
1006 | 1006 | ||
1007 | if (i_done != page_cnt) { | 1007 | if (i_done != page_cnt) { |
1008 | spin_lock(&BTRFS_I(inode)->lock); | 1008 | spin_lock(&BTRFS_I(inode)->lock); |
1009 | BTRFS_I(inode)->outstanding_extents++; | 1009 | BTRFS_I(inode)->outstanding_extents++; |
1010 | spin_unlock(&BTRFS_I(inode)->lock); | 1010 | spin_unlock(&BTRFS_I(inode)->lock); |
1011 | btrfs_delalloc_release_space(inode, | 1011 | btrfs_delalloc_release_space(inode, |
1012 | (page_cnt - i_done) << PAGE_CACHE_SHIFT); | 1012 | (page_cnt - i_done) << PAGE_CACHE_SHIFT); |
1013 | } | 1013 | } |
1014 | 1014 | ||
1015 | 1015 | ||
1016 | btrfs_set_extent_delalloc(inode, page_start, page_end - 1, | 1016 | btrfs_set_extent_delalloc(inode, page_start, page_end - 1, |
1017 | &cached_state); | 1017 | &cached_state); |
1018 | 1018 | ||
1019 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, | 1019 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, |
1020 | page_start, page_end - 1, &cached_state, | 1020 | page_start, page_end - 1, &cached_state, |
1021 | GFP_NOFS); | 1021 | GFP_NOFS); |
1022 | 1022 | ||
1023 | for (i = 0; i < i_done; i++) { | 1023 | for (i = 0; i < i_done; i++) { |
1024 | clear_page_dirty_for_io(pages[i]); | 1024 | clear_page_dirty_for_io(pages[i]); |
1025 | ClearPageChecked(pages[i]); | 1025 | ClearPageChecked(pages[i]); |
1026 | set_page_extent_mapped(pages[i]); | 1026 | set_page_extent_mapped(pages[i]); |
1027 | set_page_dirty(pages[i]); | 1027 | set_page_dirty(pages[i]); |
1028 | unlock_page(pages[i]); | 1028 | unlock_page(pages[i]); |
1029 | page_cache_release(pages[i]); | 1029 | page_cache_release(pages[i]); |
1030 | } | 1030 | } |
1031 | return i_done; | 1031 | return i_done; |
1032 | out: | 1032 | out: |
1033 | for (i = 0; i < i_done; i++) { | 1033 | for (i = 0; i < i_done; i++) { |
1034 | unlock_page(pages[i]); | 1034 | unlock_page(pages[i]); |
1035 | page_cache_release(pages[i]); | 1035 | page_cache_release(pages[i]); |
1036 | } | 1036 | } |
1037 | btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT); | 1037 | btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT); |
1038 | return ret; | 1038 | return ret; |
1039 | 1039 | ||
1040 | } | 1040 | } |
1041 | 1041 | ||
1042 | int btrfs_defrag_file(struct inode *inode, struct file *file, | 1042 | int btrfs_defrag_file(struct inode *inode, struct file *file, |
1043 | struct btrfs_ioctl_defrag_range_args *range, | 1043 | struct btrfs_ioctl_defrag_range_args *range, |
1044 | u64 newer_than, unsigned long max_to_defrag) | 1044 | u64 newer_than, unsigned long max_to_defrag) |
1045 | { | 1045 | { |
1046 | struct btrfs_root *root = BTRFS_I(inode)->root; | 1046 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1047 | struct btrfs_super_block *disk_super; | 1047 | struct btrfs_super_block *disk_super; |
1048 | struct file_ra_state *ra = NULL; | 1048 | struct file_ra_state *ra = NULL; |
1049 | unsigned long last_index; | 1049 | unsigned long last_index; |
1050 | u64 isize = i_size_read(inode); | 1050 | u64 isize = i_size_read(inode); |
1051 | u64 features; | 1051 | u64 features; |
1052 | u64 last_len = 0; | 1052 | u64 last_len = 0; |
1053 | u64 skip = 0; | 1053 | u64 skip = 0; |
1054 | u64 defrag_end = 0; | 1054 | u64 defrag_end = 0; |
1055 | u64 newer_off = range->start; | 1055 | u64 newer_off = range->start; |
1056 | unsigned long i; | 1056 | unsigned long i; |
1057 | unsigned long ra_index = 0; | 1057 | unsigned long ra_index = 0; |
1058 | int ret; | 1058 | int ret; |
1059 | int defrag_count = 0; | 1059 | int defrag_count = 0; |
1060 | int compress_type = BTRFS_COMPRESS_ZLIB; | 1060 | int compress_type = BTRFS_COMPRESS_ZLIB; |
1061 | int extent_thresh = range->extent_thresh; | 1061 | int extent_thresh = range->extent_thresh; |
1062 | int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; | 1062 | int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; |
1063 | int cluster = max_cluster; | 1063 | int cluster = max_cluster; |
1064 | u64 new_align = ~((u64)128 * 1024 - 1); | 1064 | u64 new_align = ~((u64)128 * 1024 - 1); |
1065 | struct page **pages = NULL; | 1065 | struct page **pages = NULL; |
1066 | 1066 | ||
1067 | if (extent_thresh == 0) | 1067 | if (extent_thresh == 0) |
1068 | extent_thresh = 256 * 1024; | 1068 | extent_thresh = 256 * 1024; |
1069 | 1069 | ||
1070 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { | 1070 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { |
1071 | if (range->compress_type > BTRFS_COMPRESS_TYPES) | 1071 | if (range->compress_type > BTRFS_COMPRESS_TYPES) |
1072 | return -EINVAL; | 1072 | return -EINVAL; |
1073 | if (range->compress_type) | 1073 | if (range->compress_type) |
1074 | compress_type = range->compress_type; | 1074 | compress_type = range->compress_type; |
1075 | } | 1075 | } |
1076 | 1076 | ||
1077 | if (isize == 0) | 1077 | if (isize == 0) |
1078 | return 0; | 1078 | return 0; |
1079 | 1079 | ||
1080 | /* | 1080 | /* |
1081 | * if we were not given a file, allocate a readahead | 1081 | * if we were not given a file, allocate a readahead |
1082 | * context | 1082 | * context |
1083 | */ | 1083 | */ |
1084 | if (!file) { | 1084 | if (!file) { |
1085 | ra = kzalloc(sizeof(*ra), GFP_NOFS); | 1085 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
1086 | if (!ra) | 1086 | if (!ra) |
1087 | return -ENOMEM; | 1087 | return -ENOMEM; |
1088 | file_ra_state_init(ra, inode->i_mapping); | 1088 | file_ra_state_init(ra, inode->i_mapping); |
1089 | } else { | 1089 | } else { |
1090 | ra = &file->f_ra; | 1090 | ra = &file->f_ra; |
1091 | } | 1091 | } |
1092 | 1092 | ||
1093 | pages = kmalloc(sizeof(struct page *) * max_cluster, | 1093 | pages = kmalloc(sizeof(struct page *) * max_cluster, |
1094 | GFP_NOFS); | 1094 | GFP_NOFS); |
1095 | if (!pages) { | 1095 | if (!pages) { |
1096 | ret = -ENOMEM; | 1096 | ret = -ENOMEM; |
1097 | goto out_ra; | 1097 | goto out_ra; |
1098 | } | 1098 | } |
1099 | 1099 | ||
1100 | /* find the last page to defrag */ | 1100 | /* find the last page to defrag */ |
1101 | if (range->start + range->len > range->start) { | 1101 | if (range->start + range->len > range->start) { |
1102 | last_index = min_t(u64, isize - 1, | 1102 | last_index = min_t(u64, isize - 1, |
1103 | range->start + range->len - 1) >> PAGE_CACHE_SHIFT; | 1103 | range->start + range->len - 1) >> PAGE_CACHE_SHIFT; |
1104 | } else { | 1104 | } else { |
1105 | last_index = (isize - 1) >> PAGE_CACHE_SHIFT; | 1105 | last_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
1106 | } | 1106 | } |
1107 | 1107 | ||
1108 | if (newer_than) { | 1108 | if (newer_than) { |
1109 | ret = find_new_extents(root, inode, newer_than, | 1109 | ret = find_new_extents(root, inode, newer_than, |
1110 | &newer_off, 64 * 1024); | 1110 | &newer_off, 64 * 1024); |
1111 | if (!ret) { | 1111 | if (!ret) { |
1112 | range->start = newer_off; | 1112 | range->start = newer_off; |
1113 | /* | 1113 | /* |
1114 | * we always align our defrag to help keep | 1114 | * we always align our defrag to help keep |
1115 | * the extents in the file evenly spaced | 1115 | * the extents in the file evenly spaced |
1116 | */ | 1116 | */ |
1117 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; | 1117 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; |
1118 | } else | 1118 | } else |
1119 | goto out_ra; | 1119 | goto out_ra; |
1120 | } else { | 1120 | } else { |
1121 | i = range->start >> PAGE_CACHE_SHIFT; | 1121 | i = range->start >> PAGE_CACHE_SHIFT; |
1122 | } | 1122 | } |
1123 | if (!max_to_defrag) | 1123 | if (!max_to_defrag) |
1124 | max_to_defrag = last_index + 1; | 1124 | max_to_defrag = last_index + 1; |
1125 | 1125 | ||
1126 | /* | 1126 | /* |
1127 | * make writeback starts from i, so the defrag range can be | 1127 | * make writeback starts from i, so the defrag range can be |
1128 | * written sequentially. | 1128 | * written sequentially. |
1129 | */ | 1129 | */ |
1130 | if (i < inode->i_mapping->writeback_index) | 1130 | if (i < inode->i_mapping->writeback_index) |
1131 | inode->i_mapping->writeback_index = i; | 1131 | inode->i_mapping->writeback_index = i; |
1132 | 1132 | ||
1133 | while (i <= last_index && defrag_count < max_to_defrag && | 1133 | while (i <= last_index && defrag_count < max_to_defrag && |
1134 | (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> | 1134 | (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> |
1135 | PAGE_CACHE_SHIFT)) { | 1135 | PAGE_CACHE_SHIFT)) { |
1136 | /* | 1136 | /* |
1137 | * make sure we stop running if someone unmounts | 1137 | * make sure we stop running if someone unmounts |
1138 | * the FS | 1138 | * the FS |
1139 | */ | 1139 | */ |
1140 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) | 1140 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) |
1141 | break; | 1141 | break; |
1142 | 1142 | ||
1143 | if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, | 1143 | if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, |
1144 | PAGE_CACHE_SIZE, extent_thresh, | 1144 | PAGE_CACHE_SIZE, extent_thresh, |
1145 | &last_len, &skip, &defrag_end)) { | 1145 | &last_len, &skip, &defrag_end)) { |
1146 | unsigned long next; | 1146 | unsigned long next; |
1147 | /* | 1147 | /* |
1148 | * the should_defrag function tells us how much to skip | 1148 | * the should_defrag function tells us how much to skip |
1149 | * bump our counter by the suggested amount | 1149 | * bump our counter by the suggested amount |
1150 | */ | 1150 | */ |
1151 | next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | 1151 | next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
1152 | i = max(i + 1, next); | 1152 | i = max(i + 1, next); |
1153 | continue; | 1153 | continue; |
1154 | } | 1154 | } |
1155 | 1155 | ||
1156 | if (!newer_than) { | 1156 | if (!newer_than) { |
1157 | cluster = (PAGE_CACHE_ALIGN(defrag_end) >> | 1157 | cluster = (PAGE_CACHE_ALIGN(defrag_end) >> |
1158 | PAGE_CACHE_SHIFT) - i; | 1158 | PAGE_CACHE_SHIFT) - i; |
1159 | cluster = min(cluster, max_cluster); | 1159 | cluster = min(cluster, max_cluster); |
1160 | } else { | 1160 | } else { |
1161 | cluster = max_cluster; | 1161 | cluster = max_cluster; |
1162 | } | 1162 | } |
1163 | 1163 | ||
1164 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) | 1164 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) |
1165 | BTRFS_I(inode)->force_compress = compress_type; | 1165 | BTRFS_I(inode)->force_compress = compress_type; |
1166 | 1166 | ||
1167 | if (i + cluster > ra_index) { | 1167 | if (i + cluster > ra_index) { |
1168 | ra_index = max(i, ra_index); | 1168 | ra_index = max(i, ra_index); |
1169 | btrfs_force_ra(inode->i_mapping, ra, file, ra_index, | 1169 | btrfs_force_ra(inode->i_mapping, ra, file, ra_index, |
1170 | cluster); | 1170 | cluster); |
1171 | ra_index += max_cluster; | 1171 | ra_index += max_cluster; |
1172 | } | 1172 | } |
1173 | 1173 | ||
1174 | mutex_lock(&inode->i_mutex); | 1174 | mutex_lock(&inode->i_mutex); |
1175 | ret = cluster_pages_for_defrag(inode, pages, i, cluster); | 1175 | ret = cluster_pages_for_defrag(inode, pages, i, cluster); |
1176 | if (ret < 0) { | 1176 | if (ret < 0) { |
1177 | mutex_unlock(&inode->i_mutex); | 1177 | mutex_unlock(&inode->i_mutex); |
1178 | goto out_ra; | 1178 | goto out_ra; |
1179 | } | 1179 | } |
1180 | 1180 | ||
1181 | defrag_count += ret; | 1181 | defrag_count += ret; |
1182 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret); | 1182 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret); |
1183 | mutex_unlock(&inode->i_mutex); | 1183 | mutex_unlock(&inode->i_mutex); |
1184 | 1184 | ||
1185 | if (newer_than) { | 1185 | if (newer_than) { |
1186 | if (newer_off == (u64)-1) | 1186 | if (newer_off == (u64)-1) |
1187 | break; | 1187 | break; |
1188 | 1188 | ||
1189 | if (ret > 0) | 1189 | if (ret > 0) |
1190 | i += ret; | 1190 | i += ret; |
1191 | 1191 | ||
1192 | newer_off = max(newer_off + 1, | 1192 | newer_off = max(newer_off + 1, |
1193 | (u64)i << PAGE_CACHE_SHIFT); | 1193 | (u64)i << PAGE_CACHE_SHIFT); |
1194 | 1194 | ||
1195 | ret = find_new_extents(root, inode, | 1195 | ret = find_new_extents(root, inode, |
1196 | newer_than, &newer_off, | 1196 | newer_than, &newer_off, |
1197 | 64 * 1024); | 1197 | 64 * 1024); |
1198 | if (!ret) { | 1198 | if (!ret) { |
1199 | range->start = newer_off; | 1199 | range->start = newer_off; |
1200 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; | 1200 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; |
1201 | } else { | 1201 | } else { |
1202 | break; | 1202 | break; |
1203 | } | 1203 | } |
1204 | } else { | 1204 | } else { |
1205 | if (ret > 0) { | 1205 | if (ret > 0) { |
1206 | i += ret; | 1206 | i += ret; |
1207 | last_len += ret << PAGE_CACHE_SHIFT; | 1207 | last_len += ret << PAGE_CACHE_SHIFT; |
1208 | } else { | 1208 | } else { |
1209 | i++; | 1209 | i++; |
1210 | last_len = 0; | 1210 | last_len = 0; |
1211 | } | 1211 | } |
1212 | } | 1212 | } |
1213 | } | 1213 | } |
1214 | 1214 | ||
1215 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) | 1215 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) |
1216 | filemap_flush(inode->i_mapping); | 1216 | filemap_flush(inode->i_mapping); |
1217 | 1217 | ||
1218 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { | 1218 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { |
1219 | /* the filemap_flush will queue IO into the worker threads, but | 1219 | /* the filemap_flush will queue IO into the worker threads, but |
1220 | * we have to make sure the IO is actually started and that | 1220 | * we have to make sure the IO is actually started and that |
1221 | * ordered extents get created before we return | 1221 | * ordered extents get created before we return |
1222 | */ | 1222 | */ |
1223 | atomic_inc(&root->fs_info->async_submit_draining); | 1223 | atomic_inc(&root->fs_info->async_submit_draining); |
1224 | while (atomic_read(&root->fs_info->nr_async_submits) || | 1224 | while (atomic_read(&root->fs_info->nr_async_submits) || |
1225 | atomic_read(&root->fs_info->async_delalloc_pages)) { | 1225 | atomic_read(&root->fs_info->async_delalloc_pages)) { |
1226 | wait_event(root->fs_info->async_submit_wait, | 1226 | wait_event(root->fs_info->async_submit_wait, |
1227 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && | 1227 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && |
1228 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); | 1228 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); |
1229 | } | 1229 | } |
1230 | atomic_dec(&root->fs_info->async_submit_draining); | 1230 | atomic_dec(&root->fs_info->async_submit_draining); |
1231 | 1231 | ||
1232 | mutex_lock(&inode->i_mutex); | 1232 | mutex_lock(&inode->i_mutex); |
1233 | BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; | 1233 | BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; |
1234 | mutex_unlock(&inode->i_mutex); | 1234 | mutex_unlock(&inode->i_mutex); |
1235 | } | 1235 | } |
1236 | 1236 | ||
1237 | disk_super = root->fs_info->super_copy; | 1237 | disk_super = root->fs_info->super_copy; |
1238 | features = btrfs_super_incompat_flags(disk_super); | 1238 | features = btrfs_super_incompat_flags(disk_super); |
1239 | if (range->compress_type == BTRFS_COMPRESS_LZO) { | 1239 | if (range->compress_type == BTRFS_COMPRESS_LZO) { |
1240 | features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; | 1240 | features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; |
1241 | btrfs_set_super_incompat_flags(disk_super, features); | 1241 | btrfs_set_super_incompat_flags(disk_super, features); |
1242 | } | 1242 | } |
1243 | 1243 | ||
1244 | ret = defrag_count; | 1244 | ret = defrag_count; |
1245 | 1245 | ||
1246 | out_ra: | 1246 | out_ra: |
1247 | if (!file) | 1247 | if (!file) |
1248 | kfree(ra); | 1248 | kfree(ra); |
1249 | kfree(pages); | 1249 | kfree(pages); |
1250 | return ret; | 1250 | return ret; |
1251 | } | 1251 | } |
1252 | 1252 | ||
1253 | static noinline int btrfs_ioctl_resize(struct btrfs_root *root, | 1253 | static noinline int btrfs_ioctl_resize(struct btrfs_root *root, |
1254 | void __user *arg) | 1254 | void __user *arg) |
1255 | { | 1255 | { |
1256 | u64 new_size; | 1256 | u64 new_size; |
1257 | u64 old_size; | 1257 | u64 old_size; |
1258 | u64 devid = 1; | 1258 | u64 devid = 1; |
1259 | struct btrfs_ioctl_vol_args *vol_args; | 1259 | struct btrfs_ioctl_vol_args *vol_args; |
1260 | struct btrfs_trans_handle *trans; | 1260 | struct btrfs_trans_handle *trans; |
1261 | struct btrfs_device *device = NULL; | 1261 | struct btrfs_device *device = NULL; |
1262 | char *sizestr; | 1262 | char *sizestr; |
1263 | char *devstr = NULL; | 1263 | char *devstr = NULL; |
1264 | int ret = 0; | 1264 | int ret = 0; |
1265 | int mod = 0; | 1265 | int mod = 0; |
1266 | 1266 | ||
1267 | if (root->fs_info->sb->s_flags & MS_RDONLY) | 1267 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
1268 | return -EROFS; | 1268 | return -EROFS; |
1269 | 1269 | ||
1270 | if (!capable(CAP_SYS_ADMIN)) | 1270 | if (!capable(CAP_SYS_ADMIN)) |
1271 | return -EPERM; | 1271 | return -EPERM; |
1272 | 1272 | ||
1273 | mutex_lock(&root->fs_info->volume_mutex); | 1273 | mutex_lock(&root->fs_info->volume_mutex); |
1274 | if (root->fs_info->balance_ctl) { | 1274 | if (root->fs_info->balance_ctl) { |
1275 | printk(KERN_INFO "btrfs: balance in progress\n"); | 1275 | printk(KERN_INFO "btrfs: balance in progress\n"); |
1276 | ret = -EINVAL; | 1276 | ret = -EINVAL; |
1277 | goto out; | 1277 | goto out; |
1278 | } | 1278 | } |
1279 | 1279 | ||
1280 | vol_args = memdup_user(arg, sizeof(*vol_args)); | 1280 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1281 | if (IS_ERR(vol_args)) { | 1281 | if (IS_ERR(vol_args)) { |
1282 | ret = PTR_ERR(vol_args); | 1282 | ret = PTR_ERR(vol_args); |
1283 | goto out; | 1283 | goto out; |
1284 | } | 1284 | } |
1285 | 1285 | ||
1286 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | 1286 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
1287 | 1287 | ||
1288 | sizestr = vol_args->name; | 1288 | sizestr = vol_args->name; |
1289 | devstr = strchr(sizestr, ':'); | 1289 | devstr = strchr(sizestr, ':'); |
1290 | if (devstr) { | 1290 | if (devstr) { |
1291 | char *end; | 1291 | char *end; |
1292 | sizestr = devstr + 1; | 1292 | sizestr = devstr + 1; |
1293 | *devstr = '\0'; | 1293 | *devstr = '\0'; |
1294 | devstr = vol_args->name; | 1294 | devstr = vol_args->name; |
1295 | devid = simple_strtoull(devstr, &end, 10); | 1295 | devid = simple_strtoull(devstr, &end, 10); |
1296 | printk(KERN_INFO "btrfs: resizing devid %llu\n", | 1296 | printk(KERN_INFO "btrfs: resizing devid %llu\n", |
1297 | (unsigned long long)devid); | 1297 | (unsigned long long)devid); |
1298 | } | 1298 | } |
1299 | device = btrfs_find_device(root, devid, NULL, NULL); | 1299 | device = btrfs_find_device(root, devid, NULL, NULL); |
1300 | if (!device) { | 1300 | if (!device) { |
1301 | printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", | 1301 | printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", |
1302 | (unsigned long long)devid); | 1302 | (unsigned long long)devid); |
1303 | ret = -EINVAL; | 1303 | ret = -EINVAL; |
1304 | goto out_free; | 1304 | goto out_free; |
1305 | } | 1305 | } |
1306 | if (!strcmp(sizestr, "max")) | 1306 | if (!strcmp(sizestr, "max")) |
1307 | new_size = device->bdev->bd_inode->i_size; | 1307 | new_size = device->bdev->bd_inode->i_size; |
1308 | else { | 1308 | else { |
1309 | if (sizestr[0] == '-') { | 1309 | if (sizestr[0] == '-') { |
1310 | mod = -1; | 1310 | mod = -1; |
1311 | sizestr++; | 1311 | sizestr++; |
1312 | } else if (sizestr[0] == '+') { | 1312 | } else if (sizestr[0] == '+') { |
1313 | mod = 1; | 1313 | mod = 1; |
1314 | sizestr++; | 1314 | sizestr++; |
1315 | } | 1315 | } |
1316 | new_size = memparse(sizestr, NULL); | 1316 | new_size = memparse(sizestr, NULL); |
1317 | if (new_size == 0) { | 1317 | if (new_size == 0) { |
1318 | ret = -EINVAL; | 1318 | ret = -EINVAL; |
1319 | goto out_free; | 1319 | goto out_free; |
1320 | } | 1320 | } |
1321 | } | 1321 | } |
1322 | 1322 | ||
1323 | old_size = device->total_bytes; | 1323 | old_size = device->total_bytes; |
1324 | 1324 | ||
1325 | if (mod < 0) { | 1325 | if (mod < 0) { |
1326 | if (new_size > old_size) { | 1326 | if (new_size > old_size) { |
1327 | ret = -EINVAL; | 1327 | ret = -EINVAL; |
1328 | goto out_free; | 1328 | goto out_free; |
1329 | } | 1329 | } |
1330 | new_size = old_size - new_size; | 1330 | new_size = old_size - new_size; |
1331 | } else if (mod > 0) { | 1331 | } else if (mod > 0) { |
1332 | new_size = old_size + new_size; | 1332 | new_size = old_size + new_size; |
1333 | } | 1333 | } |
1334 | 1334 | ||
1335 | if (new_size < 256 * 1024 * 1024) { | 1335 | if (new_size < 256 * 1024 * 1024) { |
1336 | ret = -EINVAL; | 1336 | ret = -EINVAL; |
1337 | goto out_free; | 1337 | goto out_free; |
1338 | } | 1338 | } |
1339 | if (new_size > device->bdev->bd_inode->i_size) { | 1339 | if (new_size > device->bdev->bd_inode->i_size) { |
1340 | ret = -EFBIG; | 1340 | ret = -EFBIG; |
1341 | goto out_free; | 1341 | goto out_free; |
1342 | } | 1342 | } |
1343 | 1343 | ||
1344 | do_div(new_size, root->sectorsize); | 1344 | do_div(new_size, root->sectorsize); |
1345 | new_size *= root->sectorsize; | 1345 | new_size *= root->sectorsize; |
1346 | 1346 | ||
1347 | printk(KERN_INFO "btrfs: new size for %s is %llu\n", | 1347 | printk(KERN_INFO "btrfs: new size for %s is %llu\n", |
1348 | device->name, (unsigned long long)new_size); | 1348 | device->name, (unsigned long long)new_size); |
1349 | 1349 | ||
1350 | if (new_size > old_size) { | 1350 | if (new_size > old_size) { |
1351 | trans = btrfs_start_transaction(root, 0); | 1351 | trans = btrfs_start_transaction(root, 0); |
1352 | if (IS_ERR(trans)) { | 1352 | if (IS_ERR(trans)) { |
1353 | ret = PTR_ERR(trans); | 1353 | ret = PTR_ERR(trans); |
1354 | goto out_free; | 1354 | goto out_free; |
1355 | } | 1355 | } |
1356 | ret = btrfs_grow_device(trans, device, new_size); | 1356 | ret = btrfs_grow_device(trans, device, new_size); |
1357 | btrfs_commit_transaction(trans, root); | 1357 | btrfs_commit_transaction(trans, root); |
1358 | } else if (new_size < old_size) { | 1358 | } else if (new_size < old_size) { |
1359 | ret = btrfs_shrink_device(device, new_size); | 1359 | ret = btrfs_shrink_device(device, new_size); |
1360 | } | 1360 | } |
1361 | 1361 | ||
1362 | out_free: | 1362 | out_free: |
1363 | kfree(vol_args); | 1363 | kfree(vol_args); |
1364 | out: | 1364 | out: |
1365 | mutex_unlock(&root->fs_info->volume_mutex); | 1365 | mutex_unlock(&root->fs_info->volume_mutex); |
1366 | return ret; | 1366 | return ret; |
1367 | } | 1367 | } |
1368 | 1368 | ||
1369 | static noinline int btrfs_ioctl_snap_create_transid(struct file *file, | 1369 | static noinline int btrfs_ioctl_snap_create_transid(struct file *file, |
1370 | char *name, | 1370 | char *name, |
1371 | unsigned long fd, | 1371 | unsigned long fd, |
1372 | int subvol, | 1372 | int subvol, |
1373 | u64 *transid, | 1373 | u64 *transid, |
1374 | bool readonly) | 1374 | bool readonly) |
1375 | { | 1375 | { |
1376 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; | 1376 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
1377 | struct file *src_file; | 1377 | struct file *src_file; |
1378 | int namelen; | 1378 | int namelen; |
1379 | int ret = 0; | 1379 | int ret = 0; |
1380 | 1380 | ||
1381 | if (root->fs_info->sb->s_flags & MS_RDONLY) | 1381 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
1382 | return -EROFS; | 1382 | return -EROFS; |
1383 | 1383 | ||
1384 | namelen = strlen(name); | 1384 | namelen = strlen(name); |
1385 | if (strchr(name, '/')) { | 1385 | if (strchr(name, '/')) { |
1386 | ret = -EINVAL; | 1386 | ret = -EINVAL; |
1387 | goto out; | 1387 | goto out; |
1388 | } | 1388 | } |
1389 | 1389 | ||
1390 | if (name[0] == '.' && | 1390 | if (name[0] == '.' && |
1391 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { | 1391 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { |
1392 | ret = -EEXIST; | 1392 | ret = -EEXIST; |
1393 | goto out; | 1393 | goto out; |
1394 | } | 1394 | } |
1395 | 1395 | ||
1396 | if (subvol) { | 1396 | if (subvol) { |
1397 | ret = btrfs_mksubvol(&file->f_path, name, namelen, | 1397 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
1398 | NULL, transid, readonly); | 1398 | NULL, transid, readonly); |
1399 | } else { | 1399 | } else { |
1400 | struct inode *src_inode; | 1400 | struct inode *src_inode; |
1401 | src_file = fget(fd); | 1401 | src_file = fget(fd); |
1402 | if (!src_file) { | 1402 | if (!src_file) { |
1403 | ret = -EINVAL; | 1403 | ret = -EINVAL; |
1404 | goto out; | 1404 | goto out; |
1405 | } | 1405 | } |
1406 | 1406 | ||
1407 | src_inode = src_file->f_path.dentry->d_inode; | 1407 | src_inode = src_file->f_path.dentry->d_inode; |
1408 | if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) { | 1408 | if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) { |
1409 | printk(KERN_INFO "btrfs: Snapshot src from " | 1409 | printk(KERN_INFO "btrfs: Snapshot src from " |
1410 | "another FS\n"); | 1410 | "another FS\n"); |
1411 | ret = -EINVAL; | 1411 | ret = -EINVAL; |
1412 | fput(src_file); | 1412 | fput(src_file); |
1413 | goto out; | 1413 | goto out; |
1414 | } | 1414 | } |
1415 | ret = btrfs_mksubvol(&file->f_path, name, namelen, | 1415 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
1416 | BTRFS_I(src_inode)->root, | 1416 | BTRFS_I(src_inode)->root, |
1417 | transid, readonly); | 1417 | transid, readonly); |
1418 | fput(src_file); | 1418 | fput(src_file); |
1419 | } | 1419 | } |
1420 | out: | 1420 | out: |
1421 | return ret; | 1421 | return ret; |
1422 | } | 1422 | } |
1423 | 1423 | ||
1424 | static noinline int btrfs_ioctl_snap_create(struct file *file, | 1424 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
1425 | void __user *arg, int subvol) | 1425 | void __user *arg, int subvol) |
1426 | { | 1426 | { |
1427 | struct btrfs_ioctl_vol_args *vol_args; | 1427 | struct btrfs_ioctl_vol_args *vol_args; |
1428 | int ret; | 1428 | int ret; |
1429 | 1429 | ||
1430 | vol_args = memdup_user(arg, sizeof(*vol_args)); | 1430 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1431 | if (IS_ERR(vol_args)) | 1431 | if (IS_ERR(vol_args)) |
1432 | return PTR_ERR(vol_args); | 1432 | return PTR_ERR(vol_args); |
1433 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | 1433 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
1434 | 1434 | ||
1435 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, | 1435 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
1436 | vol_args->fd, subvol, | 1436 | vol_args->fd, subvol, |
1437 | NULL, false); | 1437 | NULL, false); |
1438 | 1438 | ||
1439 | kfree(vol_args); | 1439 | kfree(vol_args); |
1440 | return ret; | 1440 | return ret; |
1441 | } | 1441 | } |
1442 | 1442 | ||
1443 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, | 1443 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
1444 | void __user *arg, int subvol) | 1444 | void __user *arg, int subvol) |
1445 | { | 1445 | { |
1446 | struct btrfs_ioctl_vol_args_v2 *vol_args; | 1446 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
1447 | int ret; | 1447 | int ret; |
1448 | u64 transid = 0; | 1448 | u64 transid = 0; |
1449 | u64 *ptr = NULL; | 1449 | u64 *ptr = NULL; |
1450 | bool readonly = false; | 1450 | bool readonly = false; |
1451 | 1451 | ||
1452 | vol_args = memdup_user(arg, sizeof(*vol_args)); | 1452 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1453 | if (IS_ERR(vol_args)) | 1453 | if (IS_ERR(vol_args)) |
1454 | return PTR_ERR(vol_args); | 1454 | return PTR_ERR(vol_args); |
1455 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; | 1455 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; |
1456 | 1456 | ||
1457 | if (vol_args->flags & | 1457 | if (vol_args->flags & |
1458 | ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) { | 1458 | ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) { |
1459 | ret = -EOPNOTSUPP; | 1459 | ret = -EOPNOTSUPP; |
1460 | goto out; | 1460 | goto out; |
1461 | } | 1461 | } |
1462 | 1462 | ||
1463 | if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) | 1463 | if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) |
1464 | ptr = &transid; | 1464 | ptr = &transid; |
1465 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) | 1465 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
1466 | readonly = true; | 1466 | readonly = true; |
1467 | 1467 | ||
1468 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, | 1468 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
1469 | vol_args->fd, subvol, | 1469 | vol_args->fd, subvol, |
1470 | ptr, readonly); | 1470 | ptr, readonly); |
1471 | 1471 | ||
1472 | if (ret == 0 && ptr && | 1472 | if (ret == 0 && ptr && |
1473 | copy_to_user(arg + | 1473 | copy_to_user(arg + |
1474 | offsetof(struct btrfs_ioctl_vol_args_v2, | 1474 | offsetof(struct btrfs_ioctl_vol_args_v2, |
1475 | transid), ptr, sizeof(*ptr))) | 1475 | transid), ptr, sizeof(*ptr))) |
1476 | ret = -EFAULT; | 1476 | ret = -EFAULT; |
1477 | out: | 1477 | out: |
1478 | kfree(vol_args); | 1478 | kfree(vol_args); |
1479 | return ret; | 1479 | return ret; |
1480 | } | 1480 | } |
1481 | 1481 | ||
1482 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, | 1482 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
1483 | void __user *arg) | 1483 | void __user *arg) |
1484 | { | 1484 | { |
1485 | struct inode *inode = fdentry(file)->d_inode; | 1485 | struct inode *inode = fdentry(file)->d_inode; |
1486 | struct btrfs_root *root = BTRFS_I(inode)->root; | 1486 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1487 | int ret = 0; | 1487 | int ret = 0; |
1488 | u64 flags = 0; | 1488 | u64 flags = 0; |
1489 | 1489 | ||
1490 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) | 1490 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) |
1491 | return -EINVAL; | 1491 | return -EINVAL; |
1492 | 1492 | ||
1493 | down_read(&root->fs_info->subvol_sem); | 1493 | down_read(&root->fs_info->subvol_sem); |
1494 | if (btrfs_root_readonly(root)) | 1494 | if (btrfs_root_readonly(root)) |
1495 | flags |= BTRFS_SUBVOL_RDONLY; | 1495 | flags |= BTRFS_SUBVOL_RDONLY; |
1496 | up_read(&root->fs_info->subvol_sem); | 1496 | up_read(&root->fs_info->subvol_sem); |
1497 | 1497 | ||
1498 | if (copy_to_user(arg, &flags, sizeof(flags))) | 1498 | if (copy_to_user(arg, &flags, sizeof(flags))) |
1499 | ret = -EFAULT; | 1499 | ret = -EFAULT; |
1500 | 1500 | ||
1501 | return ret; | 1501 | return ret; |
1502 | } | 1502 | } |
1503 | 1503 | ||
1504 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, | 1504 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, |
1505 | void __user *arg) | 1505 | void __user *arg) |
1506 | { | 1506 | { |
1507 | struct inode *inode = fdentry(file)->d_inode; | 1507 | struct inode *inode = fdentry(file)->d_inode; |
1508 | struct btrfs_root *root = BTRFS_I(inode)->root; | 1508 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1509 | struct btrfs_trans_handle *trans; | 1509 | struct btrfs_trans_handle *trans; |
1510 | u64 root_flags; | 1510 | u64 root_flags; |
1511 | u64 flags; | 1511 | u64 flags; |
1512 | int ret = 0; | 1512 | int ret = 0; |
1513 | 1513 | ||
1514 | if (root->fs_info->sb->s_flags & MS_RDONLY) | 1514 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
1515 | return -EROFS; | 1515 | return -EROFS; |
1516 | 1516 | ||
1517 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) | 1517 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) |
1518 | return -EINVAL; | 1518 | return -EINVAL; |
1519 | 1519 | ||
1520 | if (copy_from_user(&flags, arg, sizeof(flags))) | 1520 | if (copy_from_user(&flags, arg, sizeof(flags))) |
1521 | return -EFAULT; | 1521 | return -EFAULT; |
1522 | 1522 | ||
1523 | if (flags & BTRFS_SUBVOL_CREATE_ASYNC) | 1523 | if (flags & BTRFS_SUBVOL_CREATE_ASYNC) |
1524 | return -EINVAL; | 1524 | return -EINVAL; |
1525 | 1525 | ||
1526 | if (flags & ~BTRFS_SUBVOL_RDONLY) | 1526 | if (flags & ~BTRFS_SUBVOL_RDONLY) |
1527 | return -EOPNOTSUPP; | 1527 | return -EOPNOTSUPP; |
1528 | 1528 | ||
1529 | if (!inode_owner_or_capable(inode)) | 1529 | if (!inode_owner_or_capable(inode)) |
1530 | return -EACCES; | 1530 | return -EACCES; |
1531 | 1531 | ||
1532 | down_write(&root->fs_info->subvol_sem); | 1532 | down_write(&root->fs_info->subvol_sem); |
1533 | 1533 | ||
1534 | /* nothing to do */ | 1534 | /* nothing to do */ |
1535 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) | 1535 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) |
1536 | goto out; | 1536 | goto out; |
1537 | 1537 | ||
1538 | root_flags = btrfs_root_flags(&root->root_item); | 1538 | root_flags = btrfs_root_flags(&root->root_item); |
1539 | if (flags & BTRFS_SUBVOL_RDONLY) | 1539 | if (flags & BTRFS_SUBVOL_RDONLY) |
1540 | btrfs_set_root_flags(&root->root_item, | 1540 | btrfs_set_root_flags(&root->root_item, |
1541 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); | 1541 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); |
1542 | else | 1542 | else |
1543 | btrfs_set_root_flags(&root->root_item, | 1543 | btrfs_set_root_flags(&root->root_item, |
1544 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); | 1544 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); |
1545 | 1545 | ||
1546 | trans = btrfs_start_transaction(root, 1); | 1546 | trans = btrfs_start_transaction(root, 1); |
1547 | if (IS_ERR(trans)) { | 1547 | if (IS_ERR(trans)) { |
1548 | ret = PTR_ERR(trans); | 1548 | ret = PTR_ERR(trans); |
1549 | goto out_reset; | 1549 | goto out_reset; |
1550 | } | 1550 | } |
1551 | 1551 | ||
1552 | ret = btrfs_update_root(trans, root->fs_info->tree_root, | 1552 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
1553 | &root->root_key, &root->root_item); | 1553 | &root->root_key, &root->root_item); |
1554 | 1554 | ||
1555 | btrfs_commit_transaction(trans, root); | 1555 | btrfs_commit_transaction(trans, root); |
1556 | out_reset: | 1556 | out_reset: |
1557 | if (ret) | 1557 | if (ret) |
1558 | btrfs_set_root_flags(&root->root_item, root_flags); | 1558 | btrfs_set_root_flags(&root->root_item, root_flags); |
1559 | out: | 1559 | out: |
1560 | up_write(&root->fs_info->subvol_sem); | 1560 | up_write(&root->fs_info->subvol_sem); |
1561 | return ret; | 1561 | return ret; |
1562 | } | 1562 | } |
1563 | 1563 | ||
1564 | /* | 1564 | /* |
1565 | * helper to check if the subvolume references other subvolumes | 1565 | * helper to check if the subvolume references other subvolumes |
1566 | */ | 1566 | */ |
1567 | static noinline int may_destroy_subvol(struct btrfs_root *root) | 1567 | static noinline int may_destroy_subvol(struct btrfs_root *root) |
1568 | { | 1568 | { |
1569 | struct btrfs_path *path; | 1569 | struct btrfs_path *path; |
1570 | struct btrfs_key key; | 1570 | struct btrfs_key key; |
1571 | int ret; | 1571 | int ret; |
1572 | 1572 | ||
1573 | path = btrfs_alloc_path(); | 1573 | path = btrfs_alloc_path(); |
1574 | if (!path) | 1574 | if (!path) |
1575 | return -ENOMEM; | 1575 | return -ENOMEM; |
1576 | 1576 | ||
1577 | key.objectid = root->root_key.objectid; | 1577 | key.objectid = root->root_key.objectid; |
1578 | key.type = BTRFS_ROOT_REF_KEY; | 1578 | key.type = BTRFS_ROOT_REF_KEY; |
1579 | key.offset = (u64)-1; | 1579 | key.offset = (u64)-1; |
1580 | 1580 | ||
1581 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, | 1581 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, |
1582 | &key, path, 0, 0); | 1582 | &key, path, 0, 0); |
1583 | if (ret < 0) | 1583 | if (ret < 0) |
1584 | goto out; | 1584 | goto out; |
1585 | BUG_ON(ret == 0); | 1585 | BUG_ON(ret == 0); |
1586 | 1586 | ||
1587 | ret = 0; | 1587 | ret = 0; |
1588 | if (path->slots[0] > 0) { | 1588 | if (path->slots[0] > 0) { |
1589 | path->slots[0]--; | 1589 | path->slots[0]--; |
1590 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | 1590 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
1591 | if (key.objectid == root->root_key.objectid && | 1591 | if (key.objectid == root->root_key.objectid && |
1592 | key.type == BTRFS_ROOT_REF_KEY) | 1592 | key.type == BTRFS_ROOT_REF_KEY) |
1593 | ret = -ENOTEMPTY; | 1593 | ret = -ENOTEMPTY; |
1594 | } | 1594 | } |
1595 | out: | 1595 | out: |
1596 | btrfs_free_path(path); | 1596 | btrfs_free_path(path); |
1597 | return ret; | 1597 | return ret; |
1598 | } | 1598 | } |
1599 | 1599 | ||
1600 | static noinline int key_in_sk(struct btrfs_key *key, | 1600 | static noinline int key_in_sk(struct btrfs_key *key, |
1601 | struct btrfs_ioctl_search_key *sk) | 1601 | struct btrfs_ioctl_search_key *sk) |
1602 | { | 1602 | { |
1603 | struct btrfs_key test; | 1603 | struct btrfs_key test; |
1604 | int ret; | 1604 | int ret; |
1605 | 1605 | ||
1606 | test.objectid = sk->min_objectid; | 1606 | test.objectid = sk->min_objectid; |
1607 | test.type = sk->min_type; | 1607 | test.type = sk->min_type; |
1608 | test.offset = sk->min_offset; | 1608 | test.offset = sk->min_offset; |
1609 | 1609 | ||
1610 | ret = btrfs_comp_cpu_keys(key, &test); | 1610 | ret = btrfs_comp_cpu_keys(key, &test); |
1611 | if (ret < 0) | 1611 | if (ret < 0) |
1612 | return 0; | 1612 | return 0; |
1613 | 1613 | ||
1614 | test.objectid = sk->max_objectid; | 1614 | test.objectid = sk->max_objectid; |
1615 | test.type = sk->max_type; | 1615 | test.type = sk->max_type; |
1616 | test.offset = sk->max_offset; | 1616 | test.offset = sk->max_offset; |
1617 | 1617 | ||
1618 | ret = btrfs_comp_cpu_keys(key, &test); | 1618 | ret = btrfs_comp_cpu_keys(key, &test); |
1619 | if (ret > 0) | 1619 | if (ret > 0) |
1620 | return 0; | 1620 | return 0; |
1621 | return 1; | 1621 | return 1; |
1622 | } | 1622 | } |
1623 | 1623 | ||
1624 | static noinline int copy_to_sk(struct btrfs_root *root, | 1624 | static noinline int copy_to_sk(struct btrfs_root *root, |
1625 | struct btrfs_path *path, | 1625 | struct btrfs_path *path, |
1626 | struct btrfs_key *key, | 1626 | struct btrfs_key *key, |
1627 | struct btrfs_ioctl_search_key *sk, | 1627 | struct btrfs_ioctl_search_key *sk, |
1628 | char *buf, | 1628 | char *buf, |
1629 | unsigned long *sk_offset, | 1629 | unsigned long *sk_offset, |
1630 | int *num_found) | 1630 | int *num_found) |
1631 | { | 1631 | { |
1632 | u64 found_transid; | 1632 | u64 found_transid; |
1633 | struct extent_buffer *leaf; | 1633 | struct extent_buffer *leaf; |
1634 | struct btrfs_ioctl_search_header sh; | 1634 | struct btrfs_ioctl_search_header sh; |
1635 | unsigned long item_off; | 1635 | unsigned long item_off; |
1636 | unsigned long item_len; | 1636 | unsigned long item_len; |
1637 | int nritems; | 1637 | int nritems; |
1638 | int i; | 1638 | int i; |
1639 | int slot; | 1639 | int slot; |
1640 | int ret = 0; | 1640 | int ret = 0; |
1641 | 1641 | ||
1642 | leaf = path->nodes[0]; | 1642 | leaf = path->nodes[0]; |
1643 | slot = path->slots[0]; | 1643 | slot = path->slots[0]; |
1644 | nritems = btrfs_header_nritems(leaf); | 1644 | nritems = btrfs_header_nritems(leaf); |
1645 | 1645 | ||
1646 | if (btrfs_header_generation(leaf) > sk->max_transid) { | 1646 | if (btrfs_header_generation(leaf) > sk->max_transid) { |
1647 | i = nritems; | 1647 | i = nritems; |
1648 | goto advance_key; | 1648 | goto advance_key; |
1649 | } | 1649 | } |
1650 | found_transid = btrfs_header_generation(leaf); | 1650 | found_transid = btrfs_header_generation(leaf); |
1651 | 1651 | ||
1652 | for (i = slot; i < nritems; i++) { | 1652 | for (i = slot; i < nritems; i++) { |
1653 | item_off = btrfs_item_ptr_offset(leaf, i); | 1653 | item_off = btrfs_item_ptr_offset(leaf, i); |
1654 | item_len = btrfs_item_size_nr(leaf, i); | 1654 | item_len = btrfs_item_size_nr(leaf, i); |
1655 | 1655 | ||
1656 | if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) | 1656 | if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) |
1657 | item_len = 0; | 1657 | item_len = 0; |
1658 | 1658 | ||
1659 | if (sizeof(sh) + item_len + *sk_offset > | 1659 | if (sizeof(sh) + item_len + *sk_offset > |
1660 | BTRFS_SEARCH_ARGS_BUFSIZE) { | 1660 | BTRFS_SEARCH_ARGS_BUFSIZE) { |
1661 | ret = 1; | 1661 | ret = 1; |
1662 | goto overflow; | 1662 | goto overflow; |
1663 | } | 1663 | } |
1664 | 1664 | ||
1665 | btrfs_item_key_to_cpu(leaf, key, i); | 1665 | btrfs_item_key_to_cpu(leaf, key, i); |
1666 | if (!key_in_sk(key, sk)) | 1666 | if (!key_in_sk(key, sk)) |
1667 | continue; | 1667 | continue; |
1668 | 1668 | ||
1669 | sh.objectid = key->objectid; | 1669 | sh.objectid = key->objectid; |
1670 | sh.offset = key->offset; | 1670 | sh.offset = key->offset; |
1671 | sh.type = key->type; | 1671 | sh.type = key->type; |
1672 | sh.len = item_len; | 1672 | sh.len = item_len; |
1673 | sh.transid = found_transid; | 1673 | sh.transid = found_transid; |
1674 | 1674 | ||
1675 | /* copy search result header */ | 1675 | /* copy search result header */ |
1676 | memcpy(buf + *sk_offset, &sh, sizeof(sh)); | 1676 | memcpy(buf + *sk_offset, &sh, sizeof(sh)); |
1677 | *sk_offset += sizeof(sh); | 1677 | *sk_offset += sizeof(sh); |
1678 | 1678 | ||
1679 | if (item_len) { | 1679 | if (item_len) { |
1680 | char *p = buf + *sk_offset; | 1680 | char *p = buf + *sk_offset; |
1681 | /* copy the item */ | 1681 | /* copy the item */ |
1682 | read_extent_buffer(leaf, p, | 1682 | read_extent_buffer(leaf, p, |
1683 | item_off, item_len); | 1683 | item_off, item_len); |
1684 | *sk_offset += item_len; | 1684 | *sk_offset += item_len; |
1685 | } | 1685 | } |
1686 | (*num_found)++; | 1686 | (*num_found)++; |
1687 | 1687 | ||
1688 | if (*num_found >= sk->nr_items) | 1688 | if (*num_found >= sk->nr_items) |
1689 | break; | 1689 | break; |
1690 | } | 1690 | } |
1691 | advance_key: | 1691 | advance_key: |
1692 | ret = 0; | 1692 | ret = 0; |
1693 | if (key->offset < (u64)-1 && key->offset < sk->max_offset) | 1693 | if (key->offset < (u64)-1 && key->offset < sk->max_offset) |
1694 | key->offset++; | 1694 | key->offset++; |
1695 | else if (key->type < (u8)-1 && key->type < sk->max_type) { | 1695 | else if (key->type < (u8)-1 && key->type < sk->max_type) { |
1696 | key->offset = 0; | 1696 | key->offset = 0; |
1697 | key->type++; | 1697 | key->type++; |
1698 | } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { | 1698 | } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { |
1699 | key->offset = 0; | 1699 | key->offset = 0; |
1700 | key->type = 0; | 1700 | key->type = 0; |
1701 | key->objectid++; | 1701 | key->objectid++; |
1702 | } else | 1702 | } else |
1703 | ret = 1; | 1703 | ret = 1; |
1704 | overflow: | 1704 | overflow: |
1705 | return ret; | 1705 | return ret; |
1706 | } | 1706 | } |
1707 | 1707 | ||
1708 | static noinline int search_ioctl(struct inode *inode, | 1708 | static noinline int search_ioctl(struct inode *inode, |
1709 | struct btrfs_ioctl_search_args *args) | 1709 | struct btrfs_ioctl_search_args *args) |
1710 | { | 1710 | { |
1711 | struct btrfs_root *root; | 1711 | struct btrfs_root *root; |
1712 | struct btrfs_key key; | 1712 | struct btrfs_key key; |
1713 | struct btrfs_key max_key; | 1713 | struct btrfs_key max_key; |
1714 | struct btrfs_path *path; | 1714 | struct btrfs_path *path; |
1715 | struct btrfs_ioctl_search_key *sk = &args->key; | 1715 | struct btrfs_ioctl_search_key *sk = &args->key; |
1716 | struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; | 1716 | struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; |
1717 | int ret; | 1717 | int ret; |
1718 | int num_found = 0; | 1718 | int num_found = 0; |
1719 | unsigned long sk_offset = 0; | 1719 | unsigned long sk_offset = 0; |
1720 | 1720 | ||
1721 | path = btrfs_alloc_path(); | 1721 | path = btrfs_alloc_path(); |
1722 | if (!path) | 1722 | if (!path) |
1723 | return -ENOMEM; | 1723 | return -ENOMEM; |
1724 | 1724 | ||
1725 | if (sk->tree_id == 0) { | 1725 | if (sk->tree_id == 0) { |
1726 | /* search the root of the inode that was passed */ | 1726 | /* search the root of the inode that was passed */ |
1727 | root = BTRFS_I(inode)->root; | 1727 | root = BTRFS_I(inode)->root; |
1728 | } else { | 1728 | } else { |
1729 | key.objectid = sk->tree_id; | 1729 | key.objectid = sk->tree_id; |
1730 | key.type = BTRFS_ROOT_ITEM_KEY; | 1730 | key.type = BTRFS_ROOT_ITEM_KEY; |
1731 | key.offset = (u64)-1; | 1731 | key.offset = (u64)-1; |
1732 | root = btrfs_read_fs_root_no_name(info, &key); | 1732 | root = btrfs_read_fs_root_no_name(info, &key); |
1733 | if (IS_ERR(root)) { | 1733 | if (IS_ERR(root)) { |
1734 | printk(KERN_ERR "could not find root %llu\n", | 1734 | printk(KERN_ERR "could not find root %llu\n", |
1735 | sk->tree_id); | 1735 | sk->tree_id); |
1736 | btrfs_free_path(path); | 1736 | btrfs_free_path(path); |
1737 | return -ENOENT; | 1737 | return -ENOENT; |
1738 | } | 1738 | } |
1739 | } | 1739 | } |
1740 | 1740 | ||
1741 | key.objectid = sk->min_objectid; | 1741 | key.objectid = sk->min_objectid; |
1742 | key.type = sk->min_type; | 1742 | key.type = sk->min_type; |
1743 | key.offset = sk->min_offset; | 1743 | key.offset = sk->min_offset; |
1744 | 1744 | ||
1745 | max_key.objectid = sk->max_objectid; | 1745 | max_key.objectid = sk->max_objectid; |
1746 | max_key.type = sk->max_type; | 1746 | max_key.type = sk->max_type; |
1747 | max_key.offset = sk->max_offset; | 1747 | max_key.offset = sk->max_offset; |
1748 | 1748 | ||
1749 | path->keep_locks = 1; | 1749 | path->keep_locks = 1; |
1750 | 1750 | ||
1751 | while(1) { | 1751 | while(1) { |
1752 | ret = btrfs_search_forward(root, &key, &max_key, path, 0, | 1752 | ret = btrfs_search_forward(root, &key, &max_key, path, 0, |
1753 | sk->min_transid); | 1753 | sk->min_transid); |
1754 | if (ret != 0) { | 1754 | if (ret != 0) { |
1755 | if (ret > 0) | 1755 | if (ret > 0) |
1756 | ret = 0; | 1756 | ret = 0; |
1757 | goto err; | 1757 | goto err; |
1758 | } | 1758 | } |
1759 | ret = copy_to_sk(root, path, &key, sk, args->buf, | 1759 | ret = copy_to_sk(root, path, &key, sk, args->buf, |
1760 | &sk_offset, &num_found); | 1760 | &sk_offset, &num_found); |
1761 | btrfs_release_path(path); | 1761 | btrfs_release_path(path); |
1762 | if (ret || num_found >= sk->nr_items) | 1762 | if (ret || num_found >= sk->nr_items) |
1763 | break; | 1763 | break; |
1764 | 1764 | ||
1765 | } | 1765 | } |
1766 | ret = 0; | 1766 | ret = 0; |
1767 | err: | 1767 | err: |
1768 | sk->nr_items = num_found; | 1768 | sk->nr_items = num_found; |
1769 | btrfs_free_path(path); | 1769 | btrfs_free_path(path); |
1770 | return ret; | 1770 | return ret; |
1771 | } | 1771 | } |
1772 | 1772 | ||
1773 | static noinline int btrfs_ioctl_tree_search(struct file *file, | 1773 | static noinline int btrfs_ioctl_tree_search(struct file *file, |
1774 | void __user *argp) | 1774 | void __user *argp) |
1775 | { | 1775 | { |
1776 | struct btrfs_ioctl_search_args *args; | 1776 | struct btrfs_ioctl_search_args *args; |
1777 | struct inode *inode; | 1777 | struct inode *inode; |
1778 | int ret; | 1778 | int ret; |
1779 | 1779 | ||
1780 | if (!capable(CAP_SYS_ADMIN)) | 1780 | if (!capable(CAP_SYS_ADMIN)) |
1781 | return -EPERM; | 1781 | return -EPERM; |
1782 | 1782 | ||
1783 | args = memdup_user(argp, sizeof(*args)); | 1783 | args = memdup_user(argp, sizeof(*args)); |
1784 | if (IS_ERR(args)) | 1784 | if (IS_ERR(args)) |
1785 | return PTR_ERR(args); | 1785 | return PTR_ERR(args); |
1786 | 1786 | ||
1787 | inode = fdentry(file)->d_inode; | 1787 | inode = fdentry(file)->d_inode; |
1788 | ret = search_ioctl(inode, args); | 1788 | ret = search_ioctl(inode, args); |
1789 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) | 1789 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
1790 | ret = -EFAULT; | 1790 | ret = -EFAULT; |
1791 | kfree(args); | 1791 | kfree(args); |
1792 | return ret; | 1792 | return ret; |
1793 | } | 1793 | } |
1794 | 1794 | ||
1795 | /* | 1795 | /* |
1796 | * Search INODE_REFs to identify path name of 'dirid' directory | 1796 | * Search INODE_REFs to identify path name of 'dirid' directory |
1797 | * in a 'tree_id' tree. and sets path name to 'name'. | 1797 | * in a 'tree_id' tree. and sets path name to 'name'. |
1798 | */ | 1798 | */ |
1799 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, | 1799 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
1800 | u64 tree_id, u64 dirid, char *name) | 1800 | u64 tree_id, u64 dirid, char *name) |
1801 | { | 1801 | { |
1802 | struct btrfs_root *root; | 1802 | struct btrfs_root *root; |
1803 | struct btrfs_key key; | 1803 | struct btrfs_key key; |
1804 | char *ptr; | 1804 | char *ptr; |
1805 | int ret = -1; | 1805 | int ret = -1; |
1806 | int slot; | 1806 | int slot; |
1807 | int len; | 1807 | int len; |
1808 | int total_len = 0; | 1808 | int total_len = 0; |
1809 | struct btrfs_inode_ref *iref; | 1809 | struct btrfs_inode_ref *iref; |
1810 | struct extent_buffer *l; | 1810 | struct extent_buffer *l; |
1811 | struct btrfs_path *path; | 1811 | struct btrfs_path *path; |
1812 | 1812 | ||
1813 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { | 1813 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { |
1814 | name[0]='\0'; | 1814 | name[0]='\0'; |
1815 | return 0; | 1815 | return 0; |
1816 | } | 1816 | } |
1817 | 1817 | ||
1818 | path = btrfs_alloc_path(); | 1818 | path = btrfs_alloc_path(); |
1819 | if (!path) | 1819 | if (!path) |
1820 | return -ENOMEM; | 1820 | return -ENOMEM; |
1821 | 1821 | ||
1822 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; | 1822 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; |
1823 | 1823 | ||
1824 | key.objectid = tree_id; | 1824 | key.objectid = tree_id; |
1825 | key.type = BTRFS_ROOT_ITEM_KEY; | 1825 | key.type = BTRFS_ROOT_ITEM_KEY; |
1826 | key.offset = (u64)-1; | 1826 | key.offset = (u64)-1; |
1827 | root = btrfs_read_fs_root_no_name(info, &key); | 1827 | root = btrfs_read_fs_root_no_name(info, &key); |
1828 | if (IS_ERR(root)) { | 1828 | if (IS_ERR(root)) { |
1829 | printk(KERN_ERR "could not find root %llu\n", tree_id); | 1829 | printk(KERN_ERR "could not find root %llu\n", tree_id); |
1830 | ret = -ENOENT; | 1830 | ret = -ENOENT; |
1831 | goto out; | 1831 | goto out; |
1832 | } | 1832 | } |
1833 | 1833 | ||
1834 | key.objectid = dirid; | 1834 | key.objectid = dirid; |
1835 | key.type = BTRFS_INODE_REF_KEY; | 1835 | key.type = BTRFS_INODE_REF_KEY; |
1836 | key.offset = (u64)-1; | 1836 | key.offset = (u64)-1; |
1837 | 1837 | ||
1838 | while(1) { | 1838 | while(1) { |
1839 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | 1839 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
1840 | if (ret < 0) | 1840 | if (ret < 0) |
1841 | goto out; | 1841 | goto out; |
1842 | 1842 | ||
1843 | l = path->nodes[0]; | 1843 | l = path->nodes[0]; |
1844 | slot = path->slots[0]; | 1844 | slot = path->slots[0]; |
1845 | if (ret > 0 && slot > 0) | 1845 | if (ret > 0 && slot > 0) |
1846 | slot--; | 1846 | slot--; |
1847 | btrfs_item_key_to_cpu(l, &key, slot); | 1847 | btrfs_item_key_to_cpu(l, &key, slot); |
1848 | 1848 | ||
1849 | if (ret > 0 && (key.objectid != dirid || | 1849 | if (ret > 0 && (key.objectid != dirid || |
1850 | key.type != BTRFS_INODE_REF_KEY)) { | 1850 | key.type != BTRFS_INODE_REF_KEY)) { |
1851 | ret = -ENOENT; | 1851 | ret = -ENOENT; |
1852 | goto out; | 1852 | goto out; |
1853 | } | 1853 | } |
1854 | 1854 | ||
1855 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); | 1855 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); |
1856 | len = btrfs_inode_ref_name_len(l, iref); | 1856 | len = btrfs_inode_ref_name_len(l, iref); |
1857 | ptr -= len + 1; | 1857 | ptr -= len + 1; |
1858 | total_len += len + 1; | 1858 | total_len += len + 1; |
1859 | if (ptr < name) | 1859 | if (ptr < name) |
1860 | goto out; | 1860 | goto out; |
1861 | 1861 | ||
1862 | *(ptr + len) = '/'; | 1862 | *(ptr + len) = '/'; |
1863 | read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); | 1863 | read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); |
1864 | 1864 | ||
1865 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) | 1865 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) |
1866 | break; | 1866 | break; |
1867 | 1867 | ||
1868 | btrfs_release_path(path); | 1868 | btrfs_release_path(path); |
1869 | key.objectid = key.offset; | 1869 | key.objectid = key.offset; |
1870 | key.offset = (u64)-1; | 1870 | key.offset = (u64)-1; |
1871 | dirid = key.objectid; | 1871 | dirid = key.objectid; |
1872 | } | 1872 | } |
1873 | if (ptr < name) | 1873 | if (ptr < name) |
1874 | goto out; | 1874 | goto out; |
1875 | memmove(name, ptr, total_len); | 1875 | memmove(name, ptr, total_len); |
1876 | name[total_len]='\0'; | 1876 | name[total_len]='\0'; |
1877 | ret = 0; | 1877 | ret = 0; |
1878 | out: | 1878 | out: |
1879 | btrfs_free_path(path); | 1879 | btrfs_free_path(path); |
1880 | return ret; | 1880 | return ret; |
1881 | } | 1881 | } |
1882 | 1882 | ||
1883 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, | 1883 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, |
1884 | void __user *argp) | 1884 | void __user *argp) |
1885 | { | 1885 | { |
1886 | struct btrfs_ioctl_ino_lookup_args *args; | 1886 | struct btrfs_ioctl_ino_lookup_args *args; |
1887 | struct inode *inode; | 1887 | struct inode *inode; |
1888 | int ret; | 1888 | int ret; |
1889 | 1889 | ||
1890 | if (!capable(CAP_SYS_ADMIN)) | 1890 | if (!capable(CAP_SYS_ADMIN)) |
1891 | return -EPERM; | 1891 | return -EPERM; |
1892 | 1892 | ||
1893 | args = memdup_user(argp, sizeof(*args)); | 1893 | args = memdup_user(argp, sizeof(*args)); |
1894 | if (IS_ERR(args)) | 1894 | if (IS_ERR(args)) |
1895 | return PTR_ERR(args); | 1895 | return PTR_ERR(args); |
1896 | 1896 | ||
1897 | inode = fdentry(file)->d_inode; | 1897 | inode = fdentry(file)->d_inode; |
1898 | 1898 | ||
1899 | if (args->treeid == 0) | 1899 | if (args->treeid == 0) |
1900 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; | 1900 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; |
1901 | 1901 | ||
1902 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, | 1902 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
1903 | args->treeid, args->objectid, | 1903 | args->treeid, args->objectid, |
1904 | args->name); | 1904 | args->name); |
1905 | 1905 | ||
1906 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) | 1906 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
1907 | ret = -EFAULT; | 1907 | ret = -EFAULT; |
1908 | 1908 | ||
1909 | kfree(args); | 1909 | kfree(args); |
1910 | return ret; | 1910 | return ret; |
1911 | } | 1911 | } |
1912 | 1912 | ||
1913 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, | 1913 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
1914 | void __user *arg) | 1914 | void __user *arg) |
1915 | { | 1915 | { |
1916 | struct dentry *parent = fdentry(file); | 1916 | struct dentry *parent = fdentry(file); |
1917 | struct dentry *dentry; | 1917 | struct dentry *dentry; |
1918 | struct inode *dir = parent->d_inode; | 1918 | struct inode *dir = parent->d_inode; |
1919 | struct inode *inode; | 1919 | struct inode *inode; |
1920 | struct btrfs_root *root = BTRFS_I(dir)->root; | 1920 | struct btrfs_root *root = BTRFS_I(dir)->root; |
1921 | struct btrfs_root *dest = NULL; | 1921 | struct btrfs_root *dest = NULL; |
1922 | struct btrfs_ioctl_vol_args *vol_args; | 1922 | struct btrfs_ioctl_vol_args *vol_args; |
1923 | struct btrfs_trans_handle *trans; | 1923 | struct btrfs_trans_handle *trans; |
1924 | int namelen; | 1924 | int namelen; |
1925 | int ret; | 1925 | int ret; |
1926 | int err = 0; | 1926 | int err = 0; |
1927 | 1927 | ||
1928 | vol_args = memdup_user(arg, sizeof(*vol_args)); | 1928 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1929 | if (IS_ERR(vol_args)) | 1929 | if (IS_ERR(vol_args)) |
1930 | return PTR_ERR(vol_args); | 1930 | return PTR_ERR(vol_args); |
1931 | 1931 | ||
1932 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | 1932 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
1933 | namelen = strlen(vol_args->name); | 1933 | namelen = strlen(vol_args->name); |
1934 | if (strchr(vol_args->name, '/') || | 1934 | if (strchr(vol_args->name, '/') || |
1935 | strncmp(vol_args->name, "..", namelen) == 0) { | 1935 | strncmp(vol_args->name, "..", namelen) == 0) { |
1936 | err = -EINVAL; | 1936 | err = -EINVAL; |
1937 | goto out; | 1937 | goto out; |
1938 | } | 1938 | } |
1939 | 1939 | ||
1940 | err = mnt_want_write_file(file); | 1940 | err = mnt_want_write_file(file); |
1941 | if (err) | 1941 | if (err) |
1942 | goto out; | 1942 | goto out; |
1943 | 1943 | ||
1944 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); | 1944 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
1945 | dentry = lookup_one_len(vol_args->name, parent, namelen); | 1945 | dentry = lookup_one_len(vol_args->name, parent, namelen); |
1946 | if (IS_ERR(dentry)) { | 1946 | if (IS_ERR(dentry)) { |
1947 | err = PTR_ERR(dentry); | 1947 | err = PTR_ERR(dentry); |
1948 | goto out_unlock_dir; | 1948 | goto out_unlock_dir; |
1949 | } | 1949 | } |
1950 | 1950 | ||
1951 | if (!dentry->d_inode) { | 1951 | if (!dentry->d_inode) { |
1952 | err = -ENOENT; | 1952 | err = -ENOENT; |
1953 | goto out_dput; | 1953 | goto out_dput; |
1954 | } | 1954 | } |
1955 | 1955 | ||
1956 | inode = dentry->d_inode; | 1956 | inode = dentry->d_inode; |
1957 | dest = BTRFS_I(inode)->root; | 1957 | dest = BTRFS_I(inode)->root; |
1958 | if (!capable(CAP_SYS_ADMIN)){ | 1958 | if (!capable(CAP_SYS_ADMIN)){ |
1959 | /* | 1959 | /* |
1960 | * Regular user. Only allow this with a special mount | 1960 | * Regular user. Only allow this with a special mount |
1961 | * option, when the user has write+exec access to the | 1961 | * option, when the user has write+exec access to the |
1962 | * subvol root, and when rmdir(2) would have been | 1962 | * subvol root, and when rmdir(2) would have been |
1963 | * allowed. | 1963 | * allowed. |
1964 | * | 1964 | * |
1965 | * Note that this is _not_ check that the subvol is | 1965 | * Note that this is _not_ check that the subvol is |
1966 | * empty or doesn't contain data that we wouldn't | 1966 | * empty or doesn't contain data that we wouldn't |
1967 | * otherwise be able to delete. | 1967 | * otherwise be able to delete. |
1968 | * | 1968 | * |
1969 | * Users who want to delete empty subvols should try | 1969 | * Users who want to delete empty subvols should try |
1970 | * rmdir(2). | 1970 | * rmdir(2). |
1971 | */ | 1971 | */ |
1972 | err = -EPERM; | 1972 | err = -EPERM; |
1973 | if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) | 1973 | if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) |
1974 | goto out_dput; | 1974 | goto out_dput; |
1975 | 1975 | ||
1976 | /* | 1976 | /* |
1977 | * Do not allow deletion if the parent dir is the same | 1977 | * Do not allow deletion if the parent dir is the same |
1978 | * as the dir to be deleted. That means the ioctl | 1978 | * as the dir to be deleted. That means the ioctl |
1979 | * must be called on the dentry referencing the root | 1979 | * must be called on the dentry referencing the root |
1980 | * of the subvol, not a random directory contained | 1980 | * of the subvol, not a random directory contained |
1981 | * within it. | 1981 | * within it. |
1982 | */ | 1982 | */ |
1983 | err = -EINVAL; | 1983 | err = -EINVAL; |
1984 | if (root == dest) | 1984 | if (root == dest) |
1985 | goto out_dput; | 1985 | goto out_dput; |
1986 | 1986 | ||
1987 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); | 1987 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); |
1988 | if (err) | 1988 | if (err) |
1989 | goto out_dput; | 1989 | goto out_dput; |
1990 | 1990 | ||
1991 | /* check if subvolume may be deleted by a non-root user */ | 1991 | /* check if subvolume may be deleted by a non-root user */ |
1992 | err = btrfs_may_delete(dir, dentry, 1); | 1992 | err = btrfs_may_delete(dir, dentry, 1); |
1993 | if (err) | 1993 | if (err) |
1994 | goto out_dput; | 1994 | goto out_dput; |
1995 | } | 1995 | } |
1996 | 1996 | ||
1997 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { | 1997 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { |
1998 | err = -EINVAL; | 1998 | err = -EINVAL; |
1999 | goto out_dput; | 1999 | goto out_dput; |
2000 | } | 2000 | } |
2001 | 2001 | ||
2002 | mutex_lock(&inode->i_mutex); | 2002 | mutex_lock(&inode->i_mutex); |
2003 | err = d_invalidate(dentry); | 2003 | err = d_invalidate(dentry); |
2004 | if (err) | 2004 | if (err) |
2005 | goto out_unlock; | 2005 | goto out_unlock; |
2006 | 2006 | ||
2007 | down_write(&root->fs_info->subvol_sem); | 2007 | down_write(&root->fs_info->subvol_sem); |
2008 | 2008 | ||
2009 | err = may_destroy_subvol(dest); | 2009 | err = may_destroy_subvol(dest); |
2010 | if (err) | 2010 | if (err) |
2011 | goto out_up_write; | 2011 | goto out_up_write; |
2012 | 2012 | ||
2013 | trans = btrfs_start_transaction(root, 0); | 2013 | trans = btrfs_start_transaction(root, 0); |
2014 | if (IS_ERR(trans)) { | 2014 | if (IS_ERR(trans)) { |
2015 | err = PTR_ERR(trans); | 2015 | err = PTR_ERR(trans); |
2016 | goto out_up_write; | 2016 | goto out_up_write; |
2017 | } | 2017 | } |
2018 | trans->block_rsv = &root->fs_info->global_block_rsv; | 2018 | trans->block_rsv = &root->fs_info->global_block_rsv; |
2019 | 2019 | ||
2020 | ret = btrfs_unlink_subvol(trans, root, dir, | 2020 | ret = btrfs_unlink_subvol(trans, root, dir, |
2021 | dest->root_key.objectid, | 2021 | dest->root_key.objectid, |
2022 | dentry->d_name.name, | 2022 | dentry->d_name.name, |
2023 | dentry->d_name.len); | 2023 | dentry->d_name.len); |
2024 | if (ret) { | 2024 | if (ret) { |
2025 | err = ret; | 2025 | err = ret; |
2026 | btrfs_abort_transaction(trans, root, ret); | 2026 | btrfs_abort_transaction(trans, root, ret); |
2027 | goto out_end_trans; | 2027 | goto out_end_trans; |
2028 | } | 2028 | } |
2029 | 2029 | ||
2030 | btrfs_record_root_in_trans(trans, dest); | 2030 | btrfs_record_root_in_trans(trans, dest); |
2031 | 2031 | ||
2032 | memset(&dest->root_item.drop_progress, 0, | 2032 | memset(&dest->root_item.drop_progress, 0, |
2033 | sizeof(dest->root_item.drop_progress)); | 2033 | sizeof(dest->root_item.drop_progress)); |
2034 | dest->root_item.drop_level = 0; | 2034 | dest->root_item.drop_level = 0; |
2035 | btrfs_set_root_refs(&dest->root_item, 0); | 2035 | btrfs_set_root_refs(&dest->root_item, 0); |
2036 | 2036 | ||
2037 | if (!xchg(&dest->orphan_item_inserted, 1)) { | 2037 | if (!xchg(&dest->orphan_item_inserted, 1)) { |
2038 | ret = btrfs_insert_orphan_item(trans, | 2038 | ret = btrfs_insert_orphan_item(trans, |
2039 | root->fs_info->tree_root, | 2039 | root->fs_info->tree_root, |
2040 | dest->root_key.objectid); | 2040 | dest->root_key.objectid); |
2041 | if (ret) { | 2041 | if (ret) { |
2042 | btrfs_abort_transaction(trans, root, ret); | 2042 | btrfs_abort_transaction(trans, root, ret); |
2043 | err = ret; | 2043 | err = ret; |
2044 | goto out_end_trans; | 2044 | goto out_end_trans; |
2045 | } | 2045 | } |
2046 | } | 2046 | } |
2047 | out_end_trans: | 2047 | out_end_trans: |
2048 | ret = btrfs_end_transaction(trans, root); | 2048 | ret = btrfs_end_transaction(trans, root); |
2049 | if (ret && !err) | 2049 | if (ret && !err) |
2050 | err = ret; | 2050 | err = ret; |
2051 | inode->i_flags |= S_DEAD; | 2051 | inode->i_flags |= S_DEAD; |
2052 | out_up_write: | 2052 | out_up_write: |
2053 | up_write(&root->fs_info->subvol_sem); | 2053 | up_write(&root->fs_info->subvol_sem); |
2054 | out_unlock: | 2054 | out_unlock: |
2055 | mutex_unlock(&inode->i_mutex); | 2055 | mutex_unlock(&inode->i_mutex); |
2056 | if (!err) { | 2056 | if (!err) { |
2057 | shrink_dcache_sb(root->fs_info->sb); | 2057 | shrink_dcache_sb(root->fs_info->sb); |
2058 | btrfs_invalidate_inodes(dest); | 2058 | btrfs_invalidate_inodes(dest); |
2059 | d_delete(dentry); | 2059 | d_delete(dentry); |
2060 | } | 2060 | } |
2061 | out_dput: | 2061 | out_dput: |
2062 | dput(dentry); | 2062 | dput(dentry); |
2063 | out_unlock_dir: | 2063 | out_unlock_dir: |
2064 | mutex_unlock(&dir->i_mutex); | 2064 | mutex_unlock(&dir->i_mutex); |
2065 | mnt_drop_write_file(file); | 2065 | mnt_drop_write_file(file); |
2066 | out: | 2066 | out: |
2067 | kfree(vol_args); | 2067 | kfree(vol_args); |
2068 | return err; | 2068 | return err; |
2069 | } | 2069 | } |
2070 | 2070 | ||
2071 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) | 2071 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
2072 | { | 2072 | { |
2073 | struct inode *inode = fdentry(file)->d_inode; | 2073 | struct inode *inode = fdentry(file)->d_inode; |
2074 | struct btrfs_root *root = BTRFS_I(inode)->root; | 2074 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2075 | struct btrfs_ioctl_defrag_range_args *range; | 2075 | struct btrfs_ioctl_defrag_range_args *range; |
2076 | int ret; | 2076 | int ret; |
2077 | 2077 | ||
2078 | if (btrfs_root_readonly(root)) | 2078 | if (btrfs_root_readonly(root)) |
2079 | return -EROFS; | 2079 | return -EROFS; |
2080 | 2080 | ||
2081 | ret = mnt_want_write_file(file); | 2081 | ret = mnt_want_write_file(file); |
2082 | if (ret) | 2082 | if (ret) |
2083 | return ret; | 2083 | return ret; |
2084 | 2084 | ||
2085 | switch (inode->i_mode & S_IFMT) { | 2085 | switch (inode->i_mode & S_IFMT) { |
2086 | case S_IFDIR: | 2086 | case S_IFDIR: |
2087 | if (!capable(CAP_SYS_ADMIN)) { | 2087 | if (!capable(CAP_SYS_ADMIN)) { |
2088 | ret = -EPERM; | 2088 | ret = -EPERM; |
2089 | goto out; | 2089 | goto out; |
2090 | } | 2090 | } |
2091 | ret = btrfs_defrag_root(root, 0); | 2091 | ret = btrfs_defrag_root(root, 0); |
2092 | if (ret) | 2092 | if (ret) |
2093 | goto out; | 2093 | goto out; |
2094 | ret = btrfs_defrag_root(root->fs_info->extent_root, 0); | 2094 | ret = btrfs_defrag_root(root->fs_info->extent_root, 0); |
2095 | break; | 2095 | break; |
2096 | case S_IFREG: | 2096 | case S_IFREG: |
2097 | if (!(file->f_mode & FMODE_WRITE)) { | 2097 | if (!(file->f_mode & FMODE_WRITE)) { |
2098 | ret = -EINVAL; | 2098 | ret = -EINVAL; |
2099 | goto out; | 2099 | goto out; |
2100 | } | 2100 | } |
2101 | 2101 | ||
2102 | range = kzalloc(sizeof(*range), GFP_KERNEL); | 2102 | range = kzalloc(sizeof(*range), GFP_KERNEL); |
2103 | if (!range) { | 2103 | if (!range) { |
2104 | ret = -ENOMEM; | 2104 | ret = -ENOMEM; |
2105 | goto out; | 2105 | goto out; |
2106 | } | 2106 | } |
2107 | 2107 | ||
2108 | if (argp) { | 2108 | if (argp) { |
2109 | if (copy_from_user(range, argp, | 2109 | if (copy_from_user(range, argp, |
2110 | sizeof(*range))) { | 2110 | sizeof(*range))) { |
2111 | ret = -EFAULT; | 2111 | ret = -EFAULT; |
2112 | kfree(range); | 2112 | kfree(range); |
2113 | goto out; | 2113 | goto out; |
2114 | } | 2114 | } |
2115 | /* compression requires us to start the IO */ | 2115 | /* compression requires us to start the IO */ |
2116 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { | 2116 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { |
2117 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; | 2117 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; |
2118 | range->extent_thresh = (u32)-1; | 2118 | range->extent_thresh = (u32)-1; |
2119 | } | 2119 | } |
2120 | } else { | 2120 | } else { |
2121 | /* the rest are all set to zero by kzalloc */ | 2121 | /* the rest are all set to zero by kzalloc */ |
2122 | range->len = (u64)-1; | 2122 | range->len = (u64)-1; |
2123 | } | 2123 | } |
2124 | ret = btrfs_defrag_file(fdentry(file)->d_inode, file, | 2124 | ret = btrfs_defrag_file(fdentry(file)->d_inode, file, |
2125 | range, 0, 0); | 2125 | range, 0, 0); |
2126 | if (ret > 0) | 2126 | if (ret > 0) |
2127 | ret = 0; | 2127 | ret = 0; |
2128 | kfree(range); | 2128 | kfree(range); |
2129 | break; | 2129 | break; |
2130 | default: | 2130 | default: |
2131 | ret = -EINVAL; | 2131 | ret = -EINVAL; |
2132 | } | 2132 | } |
2133 | out: | 2133 | out: |
2134 | mnt_drop_write_file(file); | 2134 | mnt_drop_write_file(file); |
2135 | return ret; | 2135 | return ret; |
2136 | } | 2136 | } |
2137 | 2137 | ||
2138 | static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) | 2138 | static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) |
2139 | { | 2139 | { |
2140 | struct btrfs_ioctl_vol_args *vol_args; | 2140 | struct btrfs_ioctl_vol_args *vol_args; |
2141 | int ret; | 2141 | int ret; |
2142 | 2142 | ||
2143 | if (!capable(CAP_SYS_ADMIN)) | 2143 | if (!capable(CAP_SYS_ADMIN)) |
2144 | return -EPERM; | 2144 | return -EPERM; |
2145 | 2145 | ||
2146 | mutex_lock(&root->fs_info->volume_mutex); | 2146 | mutex_lock(&root->fs_info->volume_mutex); |
2147 | if (root->fs_info->balance_ctl) { | 2147 | if (root->fs_info->balance_ctl) { |
2148 | printk(KERN_INFO "btrfs: balance in progress\n"); | 2148 | printk(KERN_INFO "btrfs: balance in progress\n"); |
2149 | ret = -EINVAL; | 2149 | ret = -EINVAL; |
2150 | goto out; | 2150 | goto out; |
2151 | } | 2151 | } |
2152 | 2152 | ||
2153 | vol_args = memdup_user(arg, sizeof(*vol_args)); | 2153 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
2154 | if (IS_ERR(vol_args)) { | 2154 | if (IS_ERR(vol_args)) { |
2155 | ret = PTR_ERR(vol_args); | 2155 | ret = PTR_ERR(vol_args); |
2156 | goto out; | 2156 | goto out; |
2157 | } | 2157 | } |
2158 | 2158 | ||
2159 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | 2159 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
2160 | ret = btrfs_init_new_device(root, vol_args->name); | 2160 | ret = btrfs_init_new_device(root, vol_args->name); |
2161 | 2161 | ||
2162 | kfree(vol_args); | 2162 | kfree(vol_args); |
2163 | out: | 2163 | out: |
2164 | mutex_unlock(&root->fs_info->volume_mutex); | 2164 | mutex_unlock(&root->fs_info->volume_mutex); |
2165 | return ret; | 2165 | return ret; |
2166 | } | 2166 | } |
2167 | 2167 | ||
2168 | static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) | 2168 | static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) |
2169 | { | 2169 | { |
2170 | struct btrfs_ioctl_vol_args *vol_args; | 2170 | struct btrfs_ioctl_vol_args *vol_args; |
2171 | int ret; | 2171 | int ret; |
2172 | 2172 | ||
2173 | if (!capable(CAP_SYS_ADMIN)) | 2173 | if (!capable(CAP_SYS_ADMIN)) |
2174 | return -EPERM; | 2174 | return -EPERM; |
2175 | 2175 | ||
2176 | if (root->fs_info->sb->s_flags & MS_RDONLY) | 2176 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
2177 | return -EROFS; | 2177 | return -EROFS; |
2178 | 2178 | ||
2179 | mutex_lock(&root->fs_info->volume_mutex); | 2179 | mutex_lock(&root->fs_info->volume_mutex); |
2180 | if (root->fs_info->balance_ctl) { | 2180 | if (root->fs_info->balance_ctl) { |
2181 | printk(KERN_INFO "btrfs: balance in progress\n"); | 2181 | printk(KERN_INFO "btrfs: balance in progress\n"); |
2182 | ret = -EINVAL; | 2182 | ret = -EINVAL; |
2183 | goto out; | 2183 | goto out; |
2184 | } | 2184 | } |
2185 | 2185 | ||
2186 | vol_args = memdup_user(arg, sizeof(*vol_args)); | 2186 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
2187 | if (IS_ERR(vol_args)) { | 2187 | if (IS_ERR(vol_args)) { |
2188 | ret = PTR_ERR(vol_args); | 2188 | ret = PTR_ERR(vol_args); |
2189 | goto out; | 2189 | goto out; |
2190 | } | 2190 | } |
2191 | 2191 | ||
2192 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | 2192 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
2193 | ret = btrfs_rm_device(root, vol_args->name); | 2193 | ret = btrfs_rm_device(root, vol_args->name); |
2194 | 2194 | ||
2195 | kfree(vol_args); | 2195 | kfree(vol_args); |
2196 | out: | 2196 | out: |
2197 | mutex_unlock(&root->fs_info->volume_mutex); | 2197 | mutex_unlock(&root->fs_info->volume_mutex); |
2198 | return ret; | 2198 | return ret; |
2199 | } | 2199 | } |
2200 | 2200 | ||
2201 | static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) | 2201 | static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) |
2202 | { | 2202 | { |
2203 | struct btrfs_ioctl_fs_info_args *fi_args; | 2203 | struct btrfs_ioctl_fs_info_args *fi_args; |
2204 | struct btrfs_device *device; | 2204 | struct btrfs_device *device; |
2205 | struct btrfs_device *next; | 2205 | struct btrfs_device *next; |
2206 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | 2206 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
2207 | int ret = 0; | 2207 | int ret = 0; |
2208 | 2208 | ||
2209 | if (!capable(CAP_SYS_ADMIN)) | 2209 | if (!capable(CAP_SYS_ADMIN)) |
2210 | return -EPERM; | 2210 | return -EPERM; |
2211 | 2211 | ||
2212 | fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); | 2212 | fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); |
2213 | if (!fi_args) | 2213 | if (!fi_args) |
2214 | return -ENOMEM; | 2214 | return -ENOMEM; |
2215 | 2215 | ||
2216 | fi_args->num_devices = fs_devices->num_devices; | 2216 | fi_args->num_devices = fs_devices->num_devices; |
2217 | memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); | 2217 | memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); |
2218 | 2218 | ||
2219 | mutex_lock(&fs_devices->device_list_mutex); | 2219 | mutex_lock(&fs_devices->device_list_mutex); |
2220 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { | 2220 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { |
2221 | if (device->devid > fi_args->max_id) | 2221 | if (device->devid > fi_args->max_id) |
2222 | fi_args->max_id = device->devid; | 2222 | fi_args->max_id = device->devid; |
2223 | } | 2223 | } |
2224 | mutex_unlock(&fs_devices->device_list_mutex); | 2224 | mutex_unlock(&fs_devices->device_list_mutex); |
2225 | 2225 | ||
2226 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) | 2226 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) |
2227 | ret = -EFAULT; | 2227 | ret = -EFAULT; |
2228 | 2228 | ||
2229 | kfree(fi_args); | 2229 | kfree(fi_args); |
2230 | return ret; | 2230 | return ret; |
2231 | } | 2231 | } |
2232 | 2232 | ||
2233 | static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) | 2233 | static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) |
2234 | { | 2234 | { |
2235 | struct btrfs_ioctl_dev_info_args *di_args; | 2235 | struct btrfs_ioctl_dev_info_args *di_args; |
2236 | struct btrfs_device *dev; | 2236 | struct btrfs_device *dev; |
2237 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | 2237 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; |
2238 | int ret = 0; | 2238 | int ret = 0; |
2239 | char *s_uuid = NULL; | 2239 | char *s_uuid = NULL; |
2240 | char empty_uuid[BTRFS_UUID_SIZE] = {0}; | 2240 | char empty_uuid[BTRFS_UUID_SIZE] = {0}; |
2241 | 2241 | ||
2242 | if (!capable(CAP_SYS_ADMIN)) | 2242 | if (!capable(CAP_SYS_ADMIN)) |
2243 | return -EPERM; | 2243 | return -EPERM; |
2244 | 2244 | ||
2245 | di_args = memdup_user(arg, sizeof(*di_args)); | 2245 | di_args = memdup_user(arg, sizeof(*di_args)); |
2246 | if (IS_ERR(di_args)) | 2246 | if (IS_ERR(di_args)) |
2247 | return PTR_ERR(di_args); | 2247 | return PTR_ERR(di_args); |
2248 | 2248 | ||
2249 | if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0) | 2249 | if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0) |
2250 | s_uuid = di_args->uuid; | 2250 | s_uuid = di_args->uuid; |
2251 | 2251 | ||
2252 | mutex_lock(&fs_devices->device_list_mutex); | 2252 | mutex_lock(&fs_devices->device_list_mutex); |
2253 | dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL); | 2253 | dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL); |
2254 | mutex_unlock(&fs_devices->device_list_mutex); | 2254 | mutex_unlock(&fs_devices->device_list_mutex); |
2255 | 2255 | ||
2256 | if (!dev) { | 2256 | if (!dev) { |
2257 | ret = -ENODEV; | 2257 | ret = -ENODEV; |
2258 | goto out; | 2258 | goto out; |
2259 | } | 2259 | } |
2260 | 2260 | ||
2261 | di_args->devid = dev->devid; | 2261 | di_args->devid = dev->devid; |
2262 | di_args->bytes_used = dev->bytes_used; | 2262 | di_args->bytes_used = dev->bytes_used; |
2263 | di_args->total_bytes = dev->total_bytes; | 2263 | di_args->total_bytes = dev->total_bytes; |
2264 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); | 2264 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); |
2265 | strncpy(di_args->path, dev->name, sizeof(di_args->path)); | 2265 | if (dev->name) |
2266 | strncpy(di_args->path, dev->name, sizeof(di_args->path)); | ||
2267 | else | ||
2268 | di_args->path[0] = '\0'; | ||
2266 | 2269 | ||
2267 | out: | 2270 | out: |
2268 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) | 2271 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) |
2269 | ret = -EFAULT; | 2272 | ret = -EFAULT; |
2270 | 2273 | ||
2271 | kfree(di_args); | 2274 | kfree(di_args); |
2272 | return ret; | 2275 | return ret; |
2273 | } | 2276 | } |
2274 | 2277 | ||
2275 | static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | 2278 | static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, |
2276 | u64 off, u64 olen, u64 destoff) | 2279 | u64 off, u64 olen, u64 destoff) |
2277 | { | 2280 | { |
2278 | struct inode *inode = fdentry(file)->d_inode; | 2281 | struct inode *inode = fdentry(file)->d_inode; |
2279 | struct btrfs_root *root = BTRFS_I(inode)->root; | 2282 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2280 | struct file *src_file; | 2283 | struct file *src_file; |
2281 | struct inode *src; | 2284 | struct inode *src; |
2282 | struct btrfs_trans_handle *trans; | 2285 | struct btrfs_trans_handle *trans; |
2283 | struct btrfs_path *path; | 2286 | struct btrfs_path *path; |
2284 | struct extent_buffer *leaf; | 2287 | struct extent_buffer *leaf; |
2285 | char *buf; | 2288 | char *buf; |
2286 | struct btrfs_key key; | 2289 | struct btrfs_key key; |
2287 | u32 nritems; | 2290 | u32 nritems; |
2288 | int slot; | 2291 | int slot; |
2289 | int ret; | 2292 | int ret; |
2290 | u64 len = olen; | 2293 | u64 len = olen; |
2291 | u64 bs = root->fs_info->sb->s_blocksize; | 2294 | u64 bs = root->fs_info->sb->s_blocksize; |
2292 | u64 hint_byte; | 2295 | u64 hint_byte; |
2293 | 2296 | ||
2294 | /* | 2297 | /* |
2295 | * TODO: | 2298 | * TODO: |
2296 | * - split compressed inline extents. annoying: we need to | 2299 | * - split compressed inline extents. annoying: we need to |
2297 | * decompress into destination's address_space (the file offset | 2300 | * decompress into destination's address_space (the file offset |
2298 | * may change, so source mapping won't do), then recompress (or | 2301 | * may change, so source mapping won't do), then recompress (or |
2299 | * otherwise reinsert) a subrange. | 2302 | * otherwise reinsert) a subrange. |
2300 | * - allow ranges within the same file to be cloned (provided | 2303 | * - allow ranges within the same file to be cloned (provided |
2301 | * they don't overlap)? | 2304 | * they don't overlap)? |
2302 | */ | 2305 | */ |
2303 | 2306 | ||
2304 | /* the destination must be opened for writing */ | 2307 | /* the destination must be opened for writing */ |
2305 | if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) | 2308 | if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) |
2306 | return -EINVAL; | 2309 | return -EINVAL; |
2307 | 2310 | ||
2308 | if (btrfs_root_readonly(root)) | 2311 | if (btrfs_root_readonly(root)) |
2309 | return -EROFS; | 2312 | return -EROFS; |
2310 | 2313 | ||
2311 | ret = mnt_want_write_file(file); | 2314 | ret = mnt_want_write_file(file); |
2312 | if (ret) | 2315 | if (ret) |
2313 | return ret; | 2316 | return ret; |
2314 | 2317 | ||
2315 | src_file = fget(srcfd); | 2318 | src_file = fget(srcfd); |
2316 | if (!src_file) { | 2319 | if (!src_file) { |
2317 | ret = -EBADF; | 2320 | ret = -EBADF; |
2318 | goto out_drop_write; | 2321 | goto out_drop_write; |
2319 | } | 2322 | } |
2320 | 2323 | ||
2321 | src = src_file->f_dentry->d_inode; | 2324 | src = src_file->f_dentry->d_inode; |
2322 | 2325 | ||
2323 | ret = -EINVAL; | 2326 | ret = -EINVAL; |
2324 | if (src == inode) | 2327 | if (src == inode) |
2325 | goto out_fput; | 2328 | goto out_fput; |
2326 | 2329 | ||
2327 | /* the src must be open for reading */ | 2330 | /* the src must be open for reading */ |
2328 | if (!(src_file->f_mode & FMODE_READ)) | 2331 | if (!(src_file->f_mode & FMODE_READ)) |
2329 | goto out_fput; | 2332 | goto out_fput; |
2330 | 2333 | ||
2331 | /* don't make the dst file partly checksummed */ | 2334 | /* don't make the dst file partly checksummed */ |
2332 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != | 2335 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != |
2333 | (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) | 2336 | (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) |
2334 | goto out_fput; | 2337 | goto out_fput; |
2335 | 2338 | ||
2336 | ret = -EISDIR; | 2339 | ret = -EISDIR; |
2337 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) | 2340 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
2338 | goto out_fput; | 2341 | goto out_fput; |
2339 | 2342 | ||
2340 | ret = -EXDEV; | 2343 | ret = -EXDEV; |
2341 | if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) | 2344 | if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) |
2342 | goto out_fput; | 2345 | goto out_fput; |
2343 | 2346 | ||
2344 | ret = -ENOMEM; | 2347 | ret = -ENOMEM; |
2345 | buf = vmalloc(btrfs_level_size(root, 0)); | 2348 | buf = vmalloc(btrfs_level_size(root, 0)); |
2346 | if (!buf) | 2349 | if (!buf) |
2347 | goto out_fput; | 2350 | goto out_fput; |
2348 | 2351 | ||
2349 | path = btrfs_alloc_path(); | 2352 | path = btrfs_alloc_path(); |
2350 | if (!path) { | 2353 | if (!path) { |
2351 | vfree(buf); | 2354 | vfree(buf); |
2352 | goto out_fput; | 2355 | goto out_fput; |
2353 | } | 2356 | } |
2354 | path->reada = 2; | 2357 | path->reada = 2; |
2355 | 2358 | ||
2356 | if (inode < src) { | 2359 | if (inode < src) { |
2357 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); | 2360 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); |
2358 | mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); | 2361 | mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); |
2359 | } else { | 2362 | } else { |
2360 | mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); | 2363 | mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); |
2361 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); | 2364 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); |
2362 | } | 2365 | } |
2363 | 2366 | ||
2364 | /* determine range to clone */ | 2367 | /* determine range to clone */ |
2365 | ret = -EINVAL; | 2368 | ret = -EINVAL; |
2366 | if (off + len > src->i_size || off + len < off) | 2369 | if (off + len > src->i_size || off + len < off) |
2367 | goto out_unlock; | 2370 | goto out_unlock; |
2368 | if (len == 0) | 2371 | if (len == 0) |
2369 | olen = len = src->i_size - off; | 2372 | olen = len = src->i_size - off; |
2370 | /* if we extend to eof, continue to block boundary */ | 2373 | /* if we extend to eof, continue to block boundary */ |
2371 | if (off + len == src->i_size) | 2374 | if (off + len == src->i_size) |
2372 | len = ALIGN(src->i_size, bs) - off; | 2375 | len = ALIGN(src->i_size, bs) - off; |
2373 | 2376 | ||
2374 | /* verify the end result is block aligned */ | 2377 | /* verify the end result is block aligned */ |
2375 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || | 2378 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || |
2376 | !IS_ALIGNED(destoff, bs)) | 2379 | !IS_ALIGNED(destoff, bs)) |
2377 | goto out_unlock; | 2380 | goto out_unlock; |
2378 | 2381 | ||
2379 | if (destoff > inode->i_size) { | 2382 | if (destoff > inode->i_size) { |
2380 | ret = btrfs_cont_expand(inode, inode->i_size, destoff); | 2383 | ret = btrfs_cont_expand(inode, inode->i_size, destoff); |
2381 | if (ret) | 2384 | if (ret) |
2382 | goto out_unlock; | 2385 | goto out_unlock; |
2383 | } | 2386 | } |
2384 | 2387 | ||
2385 | /* truncate page cache pages from target inode range */ | 2388 | /* truncate page cache pages from target inode range */ |
2386 | truncate_inode_pages_range(&inode->i_data, destoff, | 2389 | truncate_inode_pages_range(&inode->i_data, destoff, |
2387 | PAGE_CACHE_ALIGN(destoff + len) - 1); | 2390 | PAGE_CACHE_ALIGN(destoff + len) - 1); |
2388 | 2391 | ||
2389 | /* do any pending delalloc/csum calc on src, one way or | 2392 | /* do any pending delalloc/csum calc on src, one way or |
2390 | another, and lock file content */ | 2393 | another, and lock file content */ |
2391 | while (1) { | 2394 | while (1) { |
2392 | struct btrfs_ordered_extent *ordered; | 2395 | struct btrfs_ordered_extent *ordered; |
2393 | lock_extent(&BTRFS_I(src)->io_tree, off, off+len); | 2396 | lock_extent(&BTRFS_I(src)->io_tree, off, off+len); |
2394 | ordered = btrfs_lookup_first_ordered_extent(src, off+len); | 2397 | ordered = btrfs_lookup_first_ordered_extent(src, off+len); |
2395 | if (!ordered && | 2398 | if (!ordered && |
2396 | !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len, | 2399 | !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len, |
2397 | EXTENT_DELALLOC, 0, NULL)) | 2400 | EXTENT_DELALLOC, 0, NULL)) |
2398 | break; | 2401 | break; |
2399 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len); | 2402 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len); |
2400 | if (ordered) | 2403 | if (ordered) |
2401 | btrfs_put_ordered_extent(ordered); | 2404 | btrfs_put_ordered_extent(ordered); |
2402 | btrfs_wait_ordered_range(src, off, len); | 2405 | btrfs_wait_ordered_range(src, off, len); |
2403 | } | 2406 | } |
2404 | 2407 | ||
2405 | /* clone data */ | 2408 | /* clone data */ |
2406 | key.objectid = btrfs_ino(src); | 2409 | key.objectid = btrfs_ino(src); |
2407 | key.type = BTRFS_EXTENT_DATA_KEY; | 2410 | key.type = BTRFS_EXTENT_DATA_KEY; |
2408 | key.offset = 0; | 2411 | key.offset = 0; |
2409 | 2412 | ||
2410 | while (1) { | 2413 | while (1) { |
2411 | /* | 2414 | /* |
2412 | * note the key will change type as we walk through the | 2415 | * note the key will change type as we walk through the |
2413 | * tree. | 2416 | * tree. |
2414 | */ | 2417 | */ |
2415 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | 2418 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
2416 | if (ret < 0) | 2419 | if (ret < 0) |
2417 | goto out; | 2420 | goto out; |
2418 | 2421 | ||
2419 | nritems = btrfs_header_nritems(path->nodes[0]); | 2422 | nritems = btrfs_header_nritems(path->nodes[0]); |
2420 | if (path->slots[0] >= nritems) { | 2423 | if (path->slots[0] >= nritems) { |
2421 | ret = btrfs_next_leaf(root, path); | 2424 | ret = btrfs_next_leaf(root, path); |
2422 | if (ret < 0) | 2425 | if (ret < 0) |
2423 | goto out; | 2426 | goto out; |
2424 | if (ret > 0) | 2427 | if (ret > 0) |
2425 | break; | 2428 | break; |
2426 | nritems = btrfs_header_nritems(path->nodes[0]); | 2429 | nritems = btrfs_header_nritems(path->nodes[0]); |
2427 | } | 2430 | } |
2428 | leaf = path->nodes[0]; | 2431 | leaf = path->nodes[0]; |
2429 | slot = path->slots[0]; | 2432 | slot = path->slots[0]; |
2430 | 2433 | ||
2431 | btrfs_item_key_to_cpu(leaf, &key, slot); | 2434 | btrfs_item_key_to_cpu(leaf, &key, slot); |
2432 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || | 2435 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
2433 | key.objectid != btrfs_ino(src)) | 2436 | key.objectid != btrfs_ino(src)) |
2434 | break; | 2437 | break; |
2435 | 2438 | ||
2436 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { | 2439 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
2437 | struct btrfs_file_extent_item *extent; | 2440 | struct btrfs_file_extent_item *extent; |
2438 | int type; | 2441 | int type; |
2439 | u32 size; | 2442 | u32 size; |
2440 | struct btrfs_key new_key; | 2443 | struct btrfs_key new_key; |
2441 | u64 disko = 0, diskl = 0; | 2444 | u64 disko = 0, diskl = 0; |
2442 | u64 datao = 0, datal = 0; | 2445 | u64 datao = 0, datal = 0; |
2443 | u8 comp; | 2446 | u8 comp; |
2444 | u64 endoff; | 2447 | u64 endoff; |
2445 | 2448 | ||
2446 | size = btrfs_item_size_nr(leaf, slot); | 2449 | size = btrfs_item_size_nr(leaf, slot); |
2447 | read_extent_buffer(leaf, buf, | 2450 | read_extent_buffer(leaf, buf, |
2448 | btrfs_item_ptr_offset(leaf, slot), | 2451 | btrfs_item_ptr_offset(leaf, slot), |
2449 | size); | 2452 | size); |
2450 | 2453 | ||
2451 | extent = btrfs_item_ptr(leaf, slot, | 2454 | extent = btrfs_item_ptr(leaf, slot, |
2452 | struct btrfs_file_extent_item); | 2455 | struct btrfs_file_extent_item); |
2453 | comp = btrfs_file_extent_compression(leaf, extent); | 2456 | comp = btrfs_file_extent_compression(leaf, extent); |
2454 | type = btrfs_file_extent_type(leaf, extent); | 2457 | type = btrfs_file_extent_type(leaf, extent); |
2455 | if (type == BTRFS_FILE_EXTENT_REG || | 2458 | if (type == BTRFS_FILE_EXTENT_REG || |
2456 | type == BTRFS_FILE_EXTENT_PREALLOC) { | 2459 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
2457 | disko = btrfs_file_extent_disk_bytenr(leaf, | 2460 | disko = btrfs_file_extent_disk_bytenr(leaf, |
2458 | extent); | 2461 | extent); |
2459 | diskl = btrfs_file_extent_disk_num_bytes(leaf, | 2462 | diskl = btrfs_file_extent_disk_num_bytes(leaf, |
2460 | extent); | 2463 | extent); |
2461 | datao = btrfs_file_extent_offset(leaf, extent); | 2464 | datao = btrfs_file_extent_offset(leaf, extent); |
2462 | datal = btrfs_file_extent_num_bytes(leaf, | 2465 | datal = btrfs_file_extent_num_bytes(leaf, |
2463 | extent); | 2466 | extent); |
2464 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { | 2467 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
2465 | /* take upper bound, may be compressed */ | 2468 | /* take upper bound, may be compressed */ |
2466 | datal = btrfs_file_extent_ram_bytes(leaf, | 2469 | datal = btrfs_file_extent_ram_bytes(leaf, |
2467 | extent); | 2470 | extent); |
2468 | } | 2471 | } |
2469 | btrfs_release_path(path); | 2472 | btrfs_release_path(path); |
2470 | 2473 | ||
2471 | if (key.offset + datal <= off || | 2474 | if (key.offset + datal <= off || |
2472 | key.offset >= off+len) | 2475 | key.offset >= off+len) |
2473 | goto next; | 2476 | goto next; |
2474 | 2477 | ||
2475 | memcpy(&new_key, &key, sizeof(new_key)); | 2478 | memcpy(&new_key, &key, sizeof(new_key)); |
2476 | new_key.objectid = btrfs_ino(inode); | 2479 | new_key.objectid = btrfs_ino(inode); |
2477 | if (off <= key.offset) | 2480 | if (off <= key.offset) |
2478 | new_key.offset = key.offset + destoff - off; | 2481 | new_key.offset = key.offset + destoff - off; |
2479 | else | 2482 | else |
2480 | new_key.offset = destoff; | 2483 | new_key.offset = destoff; |
2481 | 2484 | ||
2482 | /* | 2485 | /* |
2483 | * 1 - adjusting old extent (we may have to split it) | 2486 | * 1 - adjusting old extent (we may have to split it) |
2484 | * 1 - add new extent | 2487 | * 1 - add new extent |
2485 | * 1 - inode update | 2488 | * 1 - inode update |
2486 | */ | 2489 | */ |
2487 | trans = btrfs_start_transaction(root, 3); | 2490 | trans = btrfs_start_transaction(root, 3); |
2488 | if (IS_ERR(trans)) { | 2491 | if (IS_ERR(trans)) { |
2489 | ret = PTR_ERR(trans); | 2492 | ret = PTR_ERR(trans); |
2490 | goto out; | 2493 | goto out; |
2491 | } | 2494 | } |
2492 | 2495 | ||
2493 | if (type == BTRFS_FILE_EXTENT_REG || | 2496 | if (type == BTRFS_FILE_EXTENT_REG || |
2494 | type == BTRFS_FILE_EXTENT_PREALLOC) { | 2497 | type == BTRFS_FILE_EXTENT_PREALLOC) { |
2495 | /* | 2498 | /* |
2496 | * a | --- range to clone ---| b | 2499 | * a | --- range to clone ---| b |
2497 | * | ------------- extent ------------- | | 2500 | * | ------------- extent ------------- | |
2498 | */ | 2501 | */ |
2499 | 2502 | ||
2500 | /* substract range b */ | 2503 | /* substract range b */ |
2501 | if (key.offset + datal > off + len) | 2504 | if (key.offset + datal > off + len) |
2502 | datal = off + len - key.offset; | 2505 | datal = off + len - key.offset; |
2503 | 2506 | ||
2504 | /* substract range a */ | 2507 | /* substract range a */ |
2505 | if (off > key.offset) { | 2508 | if (off > key.offset) { |
2506 | datao += off - key.offset; | 2509 | datao += off - key.offset; |
2507 | datal -= off - key.offset; | 2510 | datal -= off - key.offset; |
2508 | } | 2511 | } |
2509 | 2512 | ||
2510 | ret = btrfs_drop_extents(trans, inode, | 2513 | ret = btrfs_drop_extents(trans, inode, |
2511 | new_key.offset, | 2514 | new_key.offset, |
2512 | new_key.offset + datal, | 2515 | new_key.offset + datal, |
2513 | &hint_byte, 1); | 2516 | &hint_byte, 1); |
2514 | if (ret) { | 2517 | if (ret) { |
2515 | btrfs_abort_transaction(trans, root, | 2518 | btrfs_abort_transaction(trans, root, |
2516 | ret); | 2519 | ret); |
2517 | btrfs_end_transaction(trans, root); | 2520 | btrfs_end_transaction(trans, root); |
2518 | goto out; | 2521 | goto out; |
2519 | } | 2522 | } |
2520 | 2523 | ||
2521 | ret = btrfs_insert_empty_item(trans, root, path, | 2524 | ret = btrfs_insert_empty_item(trans, root, path, |
2522 | &new_key, size); | 2525 | &new_key, size); |
2523 | if (ret) { | 2526 | if (ret) { |
2524 | btrfs_abort_transaction(trans, root, | 2527 | btrfs_abort_transaction(trans, root, |
2525 | ret); | 2528 | ret); |
2526 | btrfs_end_transaction(trans, root); | 2529 | btrfs_end_transaction(trans, root); |
2527 | goto out; | 2530 | goto out; |
2528 | } | 2531 | } |
2529 | 2532 | ||
2530 | leaf = path->nodes[0]; | 2533 | leaf = path->nodes[0]; |
2531 | slot = path->slots[0]; | 2534 | slot = path->slots[0]; |
2532 | write_extent_buffer(leaf, buf, | 2535 | write_extent_buffer(leaf, buf, |
2533 | btrfs_item_ptr_offset(leaf, slot), | 2536 | btrfs_item_ptr_offset(leaf, slot), |
2534 | size); | 2537 | size); |
2535 | 2538 | ||
2536 | extent = btrfs_item_ptr(leaf, slot, | 2539 | extent = btrfs_item_ptr(leaf, slot, |
2537 | struct btrfs_file_extent_item); | 2540 | struct btrfs_file_extent_item); |
2538 | 2541 | ||
2539 | /* disko == 0 means it's a hole */ | 2542 | /* disko == 0 means it's a hole */ |
2540 | if (!disko) | 2543 | if (!disko) |
2541 | datao = 0; | 2544 | datao = 0; |
2542 | 2545 | ||
2543 | btrfs_set_file_extent_offset(leaf, extent, | 2546 | btrfs_set_file_extent_offset(leaf, extent, |
2544 | datao); | 2547 | datao); |
2545 | btrfs_set_file_extent_num_bytes(leaf, extent, | 2548 | btrfs_set_file_extent_num_bytes(leaf, extent, |
2546 | datal); | 2549 | datal); |
2547 | if (disko) { | 2550 | if (disko) { |
2548 | inode_add_bytes(inode, datal); | 2551 | inode_add_bytes(inode, datal); |
2549 | ret = btrfs_inc_extent_ref(trans, root, | 2552 | ret = btrfs_inc_extent_ref(trans, root, |
2550 | disko, diskl, 0, | 2553 | disko, diskl, 0, |
2551 | root->root_key.objectid, | 2554 | root->root_key.objectid, |
2552 | btrfs_ino(inode), | 2555 | btrfs_ino(inode), |
2553 | new_key.offset - datao, | 2556 | new_key.offset - datao, |
2554 | 0); | 2557 | 0); |
2555 | if (ret) { | 2558 | if (ret) { |
2556 | btrfs_abort_transaction(trans, | 2559 | btrfs_abort_transaction(trans, |
2557 | root, | 2560 | root, |
2558 | ret); | 2561 | ret); |
2559 | btrfs_end_transaction(trans, | 2562 | btrfs_end_transaction(trans, |
2560 | root); | 2563 | root); |
2561 | goto out; | 2564 | goto out; |
2562 | 2565 | ||
2563 | } | 2566 | } |
2564 | } | 2567 | } |
2565 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { | 2568 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
2566 | u64 skip = 0; | 2569 | u64 skip = 0; |
2567 | u64 trim = 0; | 2570 | u64 trim = 0; |
2568 | if (off > key.offset) { | 2571 | if (off > key.offset) { |
2569 | skip = off - key.offset; | 2572 | skip = off - key.offset; |
2570 | new_key.offset += skip; | 2573 | new_key.offset += skip; |
2571 | } | 2574 | } |
2572 | 2575 | ||
2573 | if (key.offset + datal > off+len) | 2576 | if (key.offset + datal > off+len) |
2574 | trim = key.offset + datal - (off+len); | 2577 | trim = key.offset + datal - (off+len); |
2575 | 2578 | ||
2576 | if (comp && (skip || trim)) { | 2579 | if (comp && (skip || trim)) { |
2577 | ret = -EINVAL; | 2580 | ret = -EINVAL; |
2578 | btrfs_end_transaction(trans, root); | 2581 | btrfs_end_transaction(trans, root); |
2579 | goto out; | 2582 | goto out; |
2580 | } | 2583 | } |
2581 | size -= skip + trim; | 2584 | size -= skip + trim; |
2582 | datal -= skip + trim; | 2585 | datal -= skip + trim; |
2583 | 2586 | ||
2584 | ret = btrfs_drop_extents(trans, inode, | 2587 | ret = btrfs_drop_extents(trans, inode, |
2585 | new_key.offset, | 2588 | new_key.offset, |
2586 | new_key.offset + datal, | 2589 | new_key.offset + datal, |
2587 | &hint_byte, 1); | 2590 | &hint_byte, 1); |
2588 | if (ret) { | 2591 | if (ret) { |
2589 | btrfs_abort_transaction(trans, root, | 2592 | btrfs_abort_transaction(trans, root, |
2590 | ret); | 2593 | ret); |
2591 | btrfs_end_transaction(trans, root); | 2594 | btrfs_end_transaction(trans, root); |
2592 | goto out; | 2595 | goto out; |
2593 | } | 2596 | } |
2594 | 2597 | ||
2595 | ret = btrfs_insert_empty_item(trans, root, path, | 2598 | ret = btrfs_insert_empty_item(trans, root, path, |
2596 | &new_key, size); | 2599 | &new_key, size); |
2597 | if (ret) { | 2600 | if (ret) { |
2598 | btrfs_abort_transaction(trans, root, | 2601 | btrfs_abort_transaction(trans, root, |
2599 | ret); | 2602 | ret); |
2600 | btrfs_end_transaction(trans, root); | 2603 | btrfs_end_transaction(trans, root); |
2601 | goto out; | 2604 | goto out; |
2602 | } | 2605 | } |
2603 | 2606 | ||
2604 | if (skip) { | 2607 | if (skip) { |
2605 | u32 start = | 2608 | u32 start = |
2606 | btrfs_file_extent_calc_inline_size(0); | 2609 | btrfs_file_extent_calc_inline_size(0); |
2607 | memmove(buf+start, buf+start+skip, | 2610 | memmove(buf+start, buf+start+skip, |
2608 | datal); | 2611 | datal); |
2609 | } | 2612 | } |
2610 | 2613 | ||
2611 | leaf = path->nodes[0]; | 2614 | leaf = path->nodes[0]; |
2612 | slot = path->slots[0]; | 2615 | slot = path->slots[0]; |
2613 | write_extent_buffer(leaf, buf, | 2616 | write_extent_buffer(leaf, buf, |
2614 | btrfs_item_ptr_offset(leaf, slot), | 2617 | btrfs_item_ptr_offset(leaf, slot), |
2615 | size); | 2618 | size); |
2616 | inode_add_bytes(inode, datal); | 2619 | inode_add_bytes(inode, datal); |
2617 | } | 2620 | } |
2618 | 2621 | ||
2619 | btrfs_mark_buffer_dirty(leaf); | 2622 | btrfs_mark_buffer_dirty(leaf); |
2620 | btrfs_release_path(path); | 2623 | btrfs_release_path(path); |
2621 | 2624 | ||
2622 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | 2625 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
2623 | 2626 | ||
2624 | /* | 2627 | /* |
2625 | * we round up to the block size at eof when | 2628 | * we round up to the block size at eof when |
2626 | * determining which extents to clone above, | 2629 | * determining which extents to clone above, |
2627 | * but shouldn't round up the file size | 2630 | * but shouldn't round up the file size |
2628 | */ | 2631 | */ |
2629 | endoff = new_key.offset + datal; | 2632 | endoff = new_key.offset + datal; |
2630 | if (endoff > destoff+olen) | 2633 | if (endoff > destoff+olen) |
2631 | endoff = destoff+olen; | 2634 | endoff = destoff+olen; |
2632 | if (endoff > inode->i_size) | 2635 | if (endoff > inode->i_size) |
2633 | btrfs_i_size_write(inode, endoff); | 2636 | btrfs_i_size_write(inode, endoff); |
2634 | 2637 | ||
2635 | ret = btrfs_update_inode(trans, root, inode); | 2638 | ret = btrfs_update_inode(trans, root, inode); |
2636 | if (ret) { | 2639 | if (ret) { |
2637 | btrfs_abort_transaction(trans, root, ret); | 2640 | btrfs_abort_transaction(trans, root, ret); |
2638 | btrfs_end_transaction(trans, root); | 2641 | btrfs_end_transaction(trans, root); |
2639 | goto out; | 2642 | goto out; |
2640 | } | 2643 | } |
2641 | ret = btrfs_end_transaction(trans, root); | 2644 | ret = btrfs_end_transaction(trans, root); |
2642 | } | 2645 | } |
2643 | next: | 2646 | next: |
2644 | btrfs_release_path(path); | 2647 | btrfs_release_path(path); |
2645 | key.offset++; | 2648 | key.offset++; |
2646 | } | 2649 | } |
2647 | ret = 0; | 2650 | ret = 0; |
2648 | out: | 2651 | out: |
2649 | btrfs_release_path(path); | 2652 | btrfs_release_path(path); |
2650 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len); | 2653 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len); |
2651 | out_unlock: | 2654 | out_unlock: |
2652 | mutex_unlock(&src->i_mutex); | 2655 | mutex_unlock(&src->i_mutex); |
2653 | mutex_unlock(&inode->i_mutex); | 2656 | mutex_unlock(&inode->i_mutex); |
2654 | vfree(buf); | 2657 | vfree(buf); |
2655 | btrfs_free_path(path); | 2658 | btrfs_free_path(path); |
2656 | out_fput: | 2659 | out_fput: |
2657 | fput(src_file); | 2660 | fput(src_file); |
2658 | out_drop_write: | 2661 | out_drop_write: |
2659 | mnt_drop_write_file(file); | 2662 | mnt_drop_write_file(file); |
2660 | return ret; | 2663 | return ret; |
2661 | } | 2664 | } |
2662 | 2665 | ||
2663 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) | 2666 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
2664 | { | 2667 | { |
2665 | struct btrfs_ioctl_clone_range_args args; | 2668 | struct btrfs_ioctl_clone_range_args args; |
2666 | 2669 | ||
2667 | if (copy_from_user(&args, argp, sizeof(args))) | 2670 | if (copy_from_user(&args, argp, sizeof(args))) |
2668 | return -EFAULT; | 2671 | return -EFAULT; |
2669 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, | 2672 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, |
2670 | args.src_length, args.dest_offset); | 2673 | args.src_length, args.dest_offset); |
2671 | } | 2674 | } |
2672 | 2675 | ||
2673 | /* | 2676 | /* |
2674 | * there are many ways the trans_start and trans_end ioctls can lead | 2677 | * there are many ways the trans_start and trans_end ioctls can lead |
2675 | * to deadlocks. They should only be used by applications that | 2678 | * to deadlocks. They should only be used by applications that |
2676 | * basically own the machine, and have a very in depth understanding | 2679 | * basically own the machine, and have a very in depth understanding |
2677 | * of all the possible deadlocks and enospc problems. | 2680 | * of all the possible deadlocks and enospc problems. |
2678 | */ | 2681 | */ |
2679 | static long btrfs_ioctl_trans_start(struct file *file) | 2682 | static long btrfs_ioctl_trans_start(struct file *file) |
2680 | { | 2683 | { |
2681 | struct inode *inode = fdentry(file)->d_inode; | 2684 | struct inode *inode = fdentry(file)->d_inode; |
2682 | struct btrfs_root *root = BTRFS_I(inode)->root; | 2685 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2683 | struct btrfs_trans_handle *trans; | 2686 | struct btrfs_trans_handle *trans; |
2684 | int ret; | 2687 | int ret; |
2685 | 2688 | ||
2686 | ret = -EPERM; | 2689 | ret = -EPERM; |
2687 | if (!capable(CAP_SYS_ADMIN)) | 2690 | if (!capable(CAP_SYS_ADMIN)) |
2688 | goto out; | 2691 | goto out; |
2689 | 2692 | ||
2690 | ret = -EINPROGRESS; | 2693 | ret = -EINPROGRESS; |
2691 | if (file->private_data) | 2694 | if (file->private_data) |
2692 | goto out; | 2695 | goto out; |
2693 | 2696 | ||
2694 | ret = -EROFS; | 2697 | ret = -EROFS; |
2695 | if (btrfs_root_readonly(root)) | 2698 | if (btrfs_root_readonly(root)) |
2696 | goto out; | 2699 | goto out; |
2697 | 2700 | ||
2698 | ret = mnt_want_write_file(file); | 2701 | ret = mnt_want_write_file(file); |
2699 | if (ret) | 2702 | if (ret) |
2700 | goto out; | 2703 | goto out; |
2701 | 2704 | ||
2702 | atomic_inc(&root->fs_info->open_ioctl_trans); | 2705 | atomic_inc(&root->fs_info->open_ioctl_trans); |
2703 | 2706 | ||
2704 | ret = -ENOMEM; | 2707 | ret = -ENOMEM; |
2705 | trans = btrfs_start_ioctl_transaction(root); | 2708 | trans = btrfs_start_ioctl_transaction(root); |
2706 | if (IS_ERR(trans)) | 2709 | if (IS_ERR(trans)) |
2707 | goto out_drop; | 2710 | goto out_drop; |
2708 | 2711 | ||
2709 | file->private_data = trans; | 2712 | file->private_data = trans; |
2710 | return 0; | 2713 | return 0; |
2711 | 2714 | ||
2712 | out_drop: | 2715 | out_drop: |
2713 | atomic_dec(&root->fs_info->open_ioctl_trans); | 2716 | atomic_dec(&root->fs_info->open_ioctl_trans); |
2714 | mnt_drop_write_file(file); | 2717 | mnt_drop_write_file(file); |
2715 | out: | 2718 | out: |
2716 | return ret; | 2719 | return ret; |
2717 | } | 2720 | } |
2718 | 2721 | ||
2719 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) | 2722 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
2720 | { | 2723 | { |
2721 | struct inode *inode = fdentry(file)->d_inode; | 2724 | struct inode *inode = fdentry(file)->d_inode; |
2722 | struct btrfs_root *root = BTRFS_I(inode)->root; | 2725 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2723 | struct btrfs_root *new_root; | 2726 | struct btrfs_root *new_root; |
2724 | struct btrfs_dir_item *di; | 2727 | struct btrfs_dir_item *di; |
2725 | struct btrfs_trans_handle *trans; | 2728 | struct btrfs_trans_handle *trans; |
2726 | struct btrfs_path *path; | 2729 | struct btrfs_path *path; |
2727 | struct btrfs_key location; | 2730 | struct btrfs_key location; |
2728 | struct btrfs_disk_key disk_key; | 2731 | struct btrfs_disk_key disk_key; |
2729 | struct btrfs_super_block *disk_super; | 2732 | struct btrfs_super_block *disk_super; |
2730 | u64 features; | 2733 | u64 features; |
2731 | u64 objectid = 0; | 2734 | u64 objectid = 0; |
2732 | u64 dir_id; | 2735 | u64 dir_id; |
2733 | 2736 | ||
2734 | if (!capable(CAP_SYS_ADMIN)) | 2737 | if (!capable(CAP_SYS_ADMIN)) |
2735 | return -EPERM; | 2738 | return -EPERM; |
2736 | 2739 | ||
2737 | if (copy_from_user(&objectid, argp, sizeof(objectid))) | 2740 | if (copy_from_user(&objectid, argp, sizeof(objectid))) |
2738 | return -EFAULT; | 2741 | return -EFAULT; |
2739 | 2742 | ||
2740 | if (!objectid) | 2743 | if (!objectid) |
2741 | objectid = root->root_key.objectid; | 2744 | objectid = root->root_key.objectid; |
2742 | 2745 | ||
2743 | location.objectid = objectid; | 2746 | location.objectid = objectid; |
2744 | location.type = BTRFS_ROOT_ITEM_KEY; | 2747 | location.type = BTRFS_ROOT_ITEM_KEY; |
2745 | location.offset = (u64)-1; | 2748 | location.offset = (u64)-1; |
2746 | 2749 | ||
2747 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); | 2750 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); |
2748 | if (IS_ERR(new_root)) | 2751 | if (IS_ERR(new_root)) |
2749 | return PTR_ERR(new_root); | 2752 | return PTR_ERR(new_root); |
2750 | 2753 | ||
2751 | if (btrfs_root_refs(&new_root->root_item) == 0) | 2754 | if (btrfs_root_refs(&new_root->root_item) == 0) |
2752 | return -ENOENT; | 2755 | return -ENOENT; |
2753 | 2756 | ||
2754 | path = btrfs_alloc_path(); | 2757 | path = btrfs_alloc_path(); |
2755 | if (!path) | 2758 | if (!path) |
2756 | return -ENOMEM; | 2759 | return -ENOMEM; |
2757 | path->leave_spinning = 1; | 2760 | path->leave_spinning = 1; |
2758 | 2761 | ||
2759 | trans = btrfs_start_transaction(root, 1); | 2762 | trans = btrfs_start_transaction(root, 1); |
2760 | if (IS_ERR(trans)) { | 2763 | if (IS_ERR(trans)) { |
2761 | btrfs_free_path(path); | 2764 | btrfs_free_path(path); |
2762 | return PTR_ERR(trans); | 2765 | return PTR_ERR(trans); |
2763 | } | 2766 | } |
2764 | 2767 | ||
2765 | dir_id = btrfs_super_root_dir(root->fs_info->super_copy); | 2768 | dir_id = btrfs_super_root_dir(root->fs_info->super_copy); |
2766 | di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, | 2769 | di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, |
2767 | dir_id, "default", 7, 1); | 2770 | dir_id, "default", 7, 1); |
2768 | if (IS_ERR_OR_NULL(di)) { | 2771 | if (IS_ERR_OR_NULL(di)) { |
2769 | btrfs_free_path(path); | 2772 | btrfs_free_path(path); |
2770 | btrfs_end_transaction(trans, root); | 2773 | btrfs_end_transaction(trans, root); |
2771 | printk(KERN_ERR "Umm, you don't have the default dir item, " | 2774 | printk(KERN_ERR "Umm, you don't have the default dir item, " |
2772 | "this isn't going to work\n"); | 2775 | "this isn't going to work\n"); |
2773 | return -ENOENT; | 2776 | return -ENOENT; |
2774 | } | 2777 | } |
2775 | 2778 | ||
2776 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); | 2779 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); |
2777 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); | 2780 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); |
2778 | btrfs_mark_buffer_dirty(path->nodes[0]); | 2781 | btrfs_mark_buffer_dirty(path->nodes[0]); |
2779 | btrfs_free_path(path); | 2782 | btrfs_free_path(path); |
2780 | 2783 | ||
2781 | disk_super = root->fs_info->super_copy; | 2784 | disk_super = root->fs_info->super_copy; |
2782 | features = btrfs_super_incompat_flags(disk_super); | 2785 | features = btrfs_super_incompat_flags(disk_super); |
2783 | if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) { | 2786 | if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) { |
2784 | features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL; | 2787 | features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL; |
2785 | btrfs_set_super_incompat_flags(disk_super, features); | 2788 | btrfs_set_super_incompat_flags(disk_super, features); |
2786 | } | 2789 | } |
2787 | btrfs_end_transaction(trans, root); | 2790 | btrfs_end_transaction(trans, root); |
2788 | 2791 | ||
2789 | return 0; | 2792 | return 0; |
2790 | } | 2793 | } |
2791 | 2794 | ||
2792 | static void get_block_group_info(struct list_head *groups_list, | 2795 | static void get_block_group_info(struct list_head *groups_list, |
2793 | struct btrfs_ioctl_space_info *space) | 2796 | struct btrfs_ioctl_space_info *space) |
2794 | { | 2797 | { |
2795 | struct btrfs_block_group_cache *block_group; | 2798 | struct btrfs_block_group_cache *block_group; |
2796 | 2799 | ||
2797 | space->total_bytes = 0; | 2800 | space->total_bytes = 0; |
2798 | space->used_bytes = 0; | 2801 | space->used_bytes = 0; |
2799 | space->flags = 0; | 2802 | space->flags = 0; |
2800 | list_for_each_entry(block_group, groups_list, list) { | 2803 | list_for_each_entry(block_group, groups_list, list) { |
2801 | space->flags = block_group->flags; | 2804 | space->flags = block_group->flags; |
2802 | space->total_bytes += block_group->key.offset; | 2805 | space->total_bytes += block_group->key.offset; |
2803 | space->used_bytes += | 2806 | space->used_bytes += |
2804 | btrfs_block_group_used(&block_group->item); | 2807 | btrfs_block_group_used(&block_group->item); |
2805 | } | 2808 | } |
2806 | } | 2809 | } |
2807 | 2810 | ||
2808 | long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) | 2811 | long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) |
2809 | { | 2812 | { |
2810 | struct btrfs_ioctl_space_args space_args; | 2813 | struct btrfs_ioctl_space_args space_args; |
2811 | struct btrfs_ioctl_space_info space; | 2814 | struct btrfs_ioctl_space_info space; |
2812 | struct btrfs_ioctl_space_info *dest; | 2815 | struct btrfs_ioctl_space_info *dest; |
2813 | struct btrfs_ioctl_space_info *dest_orig; | 2816 | struct btrfs_ioctl_space_info *dest_orig; |
2814 | struct btrfs_ioctl_space_info __user *user_dest; | 2817 | struct btrfs_ioctl_space_info __user *user_dest; |
2815 | struct btrfs_space_info *info; | 2818 | struct btrfs_space_info *info; |
2816 | u64 types[] = {BTRFS_BLOCK_GROUP_DATA, | 2819 | u64 types[] = {BTRFS_BLOCK_GROUP_DATA, |
2817 | BTRFS_BLOCK_GROUP_SYSTEM, | 2820 | BTRFS_BLOCK_GROUP_SYSTEM, |
2818 | BTRFS_BLOCK_GROUP_METADATA, | 2821 | BTRFS_BLOCK_GROUP_METADATA, |
2819 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; | 2822 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; |
2820 | int num_types = 4; | 2823 | int num_types = 4; |
2821 | int alloc_size; | 2824 | int alloc_size; |
2822 | int ret = 0; | 2825 | int ret = 0; |
2823 | u64 slot_count = 0; | 2826 | u64 slot_count = 0; |
2824 | int i, c; | 2827 | int i, c; |
2825 | 2828 | ||
2826 | if (copy_from_user(&space_args, | 2829 | if (copy_from_user(&space_args, |
2827 | (struct btrfs_ioctl_space_args __user *)arg, | 2830 | (struct btrfs_ioctl_space_args __user *)arg, |
2828 | sizeof(space_args))) | 2831 | sizeof(space_args))) |
2829 | return -EFAULT; | 2832 | return -EFAULT; |
2830 | 2833 | ||
2831 | for (i = 0; i < num_types; i++) { | 2834 | for (i = 0; i < num_types; i++) { |
2832 | struct btrfs_space_info *tmp; | 2835 | struct btrfs_space_info *tmp; |
2833 | 2836 | ||
2834 | info = NULL; | 2837 | info = NULL; |
2835 | rcu_read_lock(); | 2838 | rcu_read_lock(); |
2836 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, | 2839 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, |
2837 | list) { | 2840 | list) { |
2838 | if (tmp->flags == types[i]) { | 2841 | if (tmp->flags == types[i]) { |
2839 | info = tmp; | 2842 | info = tmp; |
2840 | break; | 2843 | break; |
2841 | } | 2844 | } |
2842 | } | 2845 | } |
2843 | rcu_read_unlock(); | 2846 | rcu_read_unlock(); |
2844 | 2847 | ||
2845 | if (!info) | 2848 | if (!info) |
2846 | continue; | 2849 | continue; |
2847 | 2850 | ||
2848 | down_read(&info->groups_sem); | 2851 | down_read(&info->groups_sem); |
2849 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | 2852 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
2850 | if (!list_empty(&info->block_groups[c])) | 2853 | if (!list_empty(&info->block_groups[c])) |
2851 | slot_count++; | 2854 | slot_count++; |
2852 | } | 2855 | } |
2853 | up_read(&info->groups_sem); | 2856 | up_read(&info->groups_sem); |
2854 | } | 2857 | } |
2855 | 2858 | ||
2856 | /* space_slots == 0 means they are asking for a count */ | 2859 | /* space_slots == 0 means they are asking for a count */ |
2857 | if (space_args.space_slots == 0) { | 2860 | if (space_args.space_slots == 0) { |
2858 | space_args.total_spaces = slot_count; | 2861 | space_args.total_spaces = slot_count; |
2859 | goto out; | 2862 | goto out; |
2860 | } | 2863 | } |
2861 | 2864 | ||
2862 | slot_count = min_t(u64, space_args.space_slots, slot_count); | 2865 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
2863 | 2866 | ||
2864 | alloc_size = sizeof(*dest) * slot_count; | 2867 | alloc_size = sizeof(*dest) * slot_count; |
2865 | 2868 | ||
2866 | /* we generally have at most 6 or so space infos, one for each raid | 2869 | /* we generally have at most 6 or so space infos, one for each raid |
2867 | * level. So, a whole page should be more than enough for everyone | 2870 | * level. So, a whole page should be more than enough for everyone |
2868 | */ | 2871 | */ |
2869 | if (alloc_size > PAGE_CACHE_SIZE) | 2872 | if (alloc_size > PAGE_CACHE_SIZE) |
2870 | return -ENOMEM; | 2873 | return -ENOMEM; |
2871 | 2874 | ||
2872 | space_args.total_spaces = 0; | 2875 | space_args.total_spaces = 0; |
2873 | dest = kmalloc(alloc_size, GFP_NOFS); | 2876 | dest = kmalloc(alloc_size, GFP_NOFS); |
2874 | if (!dest) | 2877 | if (!dest) |
2875 | return -ENOMEM; | 2878 | return -ENOMEM; |
2876 | dest_orig = dest; | 2879 | dest_orig = dest; |
2877 | 2880 | ||
2878 | /* now we have a buffer to copy into */ | 2881 | /* now we have a buffer to copy into */ |
2879 | for (i = 0; i < num_types; i++) { | 2882 | for (i = 0; i < num_types; i++) { |
2880 | struct btrfs_space_info *tmp; | 2883 | struct btrfs_space_info *tmp; |
2881 | 2884 | ||
2882 | if (!slot_count) | 2885 | if (!slot_count) |
2883 | break; | 2886 | break; |
2884 | 2887 | ||
2885 | info = NULL; | 2888 | info = NULL; |
2886 | rcu_read_lock(); | 2889 | rcu_read_lock(); |
2887 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, | 2890 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, |
2888 | list) { | 2891 | list) { |
2889 | if (tmp->flags == types[i]) { | 2892 | if (tmp->flags == types[i]) { |
2890 | info = tmp; | 2893 | info = tmp; |
2891 | break; | 2894 | break; |
2892 | } | 2895 | } |
2893 | } | 2896 | } |
2894 | rcu_read_unlock(); | 2897 | rcu_read_unlock(); |
2895 | 2898 | ||
2896 | if (!info) | 2899 | if (!info) |
2897 | continue; | 2900 | continue; |
2898 | down_read(&info->groups_sem); | 2901 | down_read(&info->groups_sem); |
2899 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | 2902 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { |
2900 | if (!list_empty(&info->block_groups[c])) { | 2903 | if (!list_empty(&info->block_groups[c])) { |
2901 | get_block_group_info(&info->block_groups[c], | 2904 | get_block_group_info(&info->block_groups[c], |
2902 | &space); | 2905 | &space); |
2903 | memcpy(dest, &space, sizeof(space)); | 2906 | memcpy(dest, &space, sizeof(space)); |
2904 | dest++; | 2907 | dest++; |
2905 | space_args.total_spaces++; | 2908 | space_args.total_spaces++; |
2906 | slot_count--; | 2909 | slot_count--; |
2907 | } | 2910 | } |
2908 | if (!slot_count) | 2911 | if (!slot_count) |
2909 | break; | 2912 | break; |
2910 | } | 2913 | } |
2911 | up_read(&info->groups_sem); | 2914 | up_read(&info->groups_sem); |
2912 | } | 2915 | } |
2913 | 2916 | ||
2914 | user_dest = (struct btrfs_ioctl_space_info *) | 2917 | user_dest = (struct btrfs_ioctl_space_info *) |
2915 | (arg + sizeof(struct btrfs_ioctl_space_args)); | 2918 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
2916 | 2919 | ||
2917 | if (copy_to_user(user_dest, dest_orig, alloc_size)) | 2920 | if (copy_to_user(user_dest, dest_orig, alloc_size)) |
2918 | ret = -EFAULT; | 2921 | ret = -EFAULT; |
2919 | 2922 | ||
2920 | kfree(dest_orig); | 2923 | kfree(dest_orig); |
2921 | out: | 2924 | out: |
2922 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) | 2925 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) |
2923 | ret = -EFAULT; | 2926 | ret = -EFAULT; |
2924 | 2927 | ||
2925 | return ret; | 2928 | return ret; |
2926 | } | 2929 | } |
2927 | 2930 | ||
2928 | /* | 2931 | /* |
2929 | * there are many ways the trans_start and trans_end ioctls can lead | 2932 | * there are many ways the trans_start and trans_end ioctls can lead |
2930 | * to deadlocks. They should only be used by applications that | 2933 | * to deadlocks. They should only be used by applications that |
2931 | * basically own the machine, and have a very in depth understanding | 2934 | * basically own the machine, and have a very in depth understanding |
2932 | * of all the possible deadlocks and enospc problems. | 2935 | * of all the possible deadlocks and enospc problems. |
2933 | */ | 2936 | */ |
2934 | long btrfs_ioctl_trans_end(struct file *file) | 2937 | long btrfs_ioctl_trans_end(struct file *file) |
2935 | { | 2938 | { |
2936 | struct inode *inode = fdentry(file)->d_inode; | 2939 | struct inode *inode = fdentry(file)->d_inode; |
2937 | struct btrfs_root *root = BTRFS_I(inode)->root; | 2940 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2938 | struct btrfs_trans_handle *trans; | 2941 | struct btrfs_trans_handle *trans; |
2939 | 2942 | ||
2940 | trans = file->private_data; | 2943 | trans = file->private_data; |
2941 | if (!trans) | 2944 | if (!trans) |
2942 | return -EINVAL; | 2945 | return -EINVAL; |
2943 | file->private_data = NULL; | 2946 | file->private_data = NULL; |
2944 | 2947 | ||
2945 | btrfs_end_transaction(trans, root); | 2948 | btrfs_end_transaction(trans, root); |
2946 | 2949 | ||
2947 | atomic_dec(&root->fs_info->open_ioctl_trans); | 2950 | atomic_dec(&root->fs_info->open_ioctl_trans); |
2948 | 2951 | ||
2949 | mnt_drop_write_file(file); | 2952 | mnt_drop_write_file(file); |
2950 | return 0; | 2953 | return 0; |
2951 | } | 2954 | } |
2952 | 2955 | ||
2953 | static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp) | 2956 | static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp) |
2954 | { | 2957 | { |
2955 | struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; | 2958 | struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; |
2956 | struct btrfs_trans_handle *trans; | 2959 | struct btrfs_trans_handle *trans; |
2957 | u64 transid; | 2960 | u64 transid; |
2958 | int ret; | 2961 | int ret; |
2959 | 2962 | ||
2960 | trans = btrfs_start_transaction(root, 0); | 2963 | trans = btrfs_start_transaction(root, 0); |
2961 | if (IS_ERR(trans)) | 2964 | if (IS_ERR(trans)) |
2962 | return PTR_ERR(trans); | 2965 | return PTR_ERR(trans); |
2963 | transid = trans->transid; | 2966 | transid = trans->transid; |
2964 | ret = btrfs_commit_transaction_async(trans, root, 0); | 2967 | ret = btrfs_commit_transaction_async(trans, root, 0); |
2965 | if (ret) { | 2968 | if (ret) { |
2966 | btrfs_end_transaction(trans, root); | 2969 | btrfs_end_transaction(trans, root); |
2967 | return ret; | 2970 | return ret; |
2968 | } | 2971 | } |
2969 | 2972 | ||
2970 | if (argp) | 2973 | if (argp) |
2971 | if (copy_to_user(argp, &transid, sizeof(transid))) | 2974 | if (copy_to_user(argp, &transid, sizeof(transid))) |
2972 | return -EFAULT; | 2975 | return -EFAULT; |
2973 | return 0; | 2976 | return 0; |
2974 | } | 2977 | } |
2975 | 2978 | ||
2976 | static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp) | 2979 | static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp) |
2977 | { | 2980 | { |
2978 | struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; | 2981 | struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; |
2979 | u64 transid; | 2982 | u64 transid; |
2980 | 2983 | ||
2981 | if (argp) { | 2984 | if (argp) { |
2982 | if (copy_from_user(&transid, argp, sizeof(transid))) | 2985 | if (copy_from_user(&transid, argp, sizeof(transid))) |
2983 | return -EFAULT; | 2986 | return -EFAULT; |
2984 | } else { | 2987 | } else { |
2985 | transid = 0; /* current trans */ | 2988 | transid = 0; /* current trans */ |
2986 | } | 2989 | } |
2987 | return btrfs_wait_for_commit(root, transid); | 2990 | return btrfs_wait_for_commit(root, transid); |
2988 | } | 2991 | } |
2989 | 2992 | ||
2990 | static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg) | 2993 | static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg) |
2991 | { | 2994 | { |
2992 | int ret; | 2995 | int ret; |
2993 | struct btrfs_ioctl_scrub_args *sa; | 2996 | struct btrfs_ioctl_scrub_args *sa; |
2994 | 2997 | ||
2995 | if (!capable(CAP_SYS_ADMIN)) | 2998 | if (!capable(CAP_SYS_ADMIN)) |
2996 | return -EPERM; | 2999 | return -EPERM; |
2997 | 3000 | ||
2998 | sa = memdup_user(arg, sizeof(*sa)); | 3001 | sa = memdup_user(arg, sizeof(*sa)); |
2999 | if (IS_ERR(sa)) | 3002 | if (IS_ERR(sa)) |
3000 | return PTR_ERR(sa); | 3003 | return PTR_ERR(sa); |
3001 | 3004 | ||
3002 | ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end, | 3005 | ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end, |
3003 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY); | 3006 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY); |
3004 | 3007 | ||
3005 | if (copy_to_user(arg, sa, sizeof(*sa))) | 3008 | if (copy_to_user(arg, sa, sizeof(*sa))) |
3006 | ret = -EFAULT; | 3009 | ret = -EFAULT; |
3007 | 3010 | ||
3008 | kfree(sa); | 3011 | kfree(sa); |
3009 | return ret; | 3012 | return ret; |
3010 | } | 3013 | } |
3011 | 3014 | ||
3012 | static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) | 3015 | static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) |
3013 | { | 3016 | { |
3014 | if (!capable(CAP_SYS_ADMIN)) | 3017 | if (!capable(CAP_SYS_ADMIN)) |
3015 | return -EPERM; | 3018 | return -EPERM; |
3016 | 3019 | ||
3017 | return btrfs_scrub_cancel(root); | 3020 | return btrfs_scrub_cancel(root); |
3018 | } | 3021 | } |
3019 | 3022 | ||
3020 | static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, | 3023 | static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, |
3021 | void __user *arg) | 3024 | void __user *arg) |
3022 | { | 3025 | { |
3023 | struct btrfs_ioctl_scrub_args *sa; | 3026 | struct btrfs_ioctl_scrub_args *sa; |
3024 | int ret; | 3027 | int ret; |
3025 | 3028 | ||
3026 | if (!capable(CAP_SYS_ADMIN)) | 3029 | if (!capable(CAP_SYS_ADMIN)) |
3027 | return -EPERM; | 3030 | return -EPERM; |
3028 | 3031 | ||
3029 | sa = memdup_user(arg, sizeof(*sa)); | 3032 | sa = memdup_user(arg, sizeof(*sa)); |
3030 | if (IS_ERR(sa)) | 3033 | if (IS_ERR(sa)) |
3031 | return PTR_ERR(sa); | 3034 | return PTR_ERR(sa); |
3032 | 3035 | ||
3033 | ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); | 3036 | ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); |
3034 | 3037 | ||
3035 | if (copy_to_user(arg, sa, sizeof(*sa))) | 3038 | if (copy_to_user(arg, sa, sizeof(*sa))) |
3036 | ret = -EFAULT; | 3039 | ret = -EFAULT; |
3037 | 3040 | ||
3038 | kfree(sa); | 3041 | kfree(sa); |
3039 | return ret; | 3042 | return ret; |
3040 | } | 3043 | } |
3041 | 3044 | ||
3042 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) | 3045 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
3043 | { | 3046 | { |
3044 | int ret = 0; | 3047 | int ret = 0; |
3045 | int i; | 3048 | int i; |
3046 | u64 rel_ptr; | 3049 | u64 rel_ptr; |
3047 | int size; | 3050 | int size; |
3048 | struct btrfs_ioctl_ino_path_args *ipa = NULL; | 3051 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
3049 | struct inode_fs_paths *ipath = NULL; | 3052 | struct inode_fs_paths *ipath = NULL; |
3050 | struct btrfs_path *path; | 3053 | struct btrfs_path *path; |
3051 | 3054 | ||
3052 | if (!capable(CAP_SYS_ADMIN)) | 3055 | if (!capable(CAP_SYS_ADMIN)) |
3053 | return -EPERM; | 3056 | return -EPERM; |
3054 | 3057 | ||
3055 | path = btrfs_alloc_path(); | 3058 | path = btrfs_alloc_path(); |
3056 | if (!path) { | 3059 | if (!path) { |
3057 | ret = -ENOMEM; | 3060 | ret = -ENOMEM; |
3058 | goto out; | 3061 | goto out; |
3059 | } | 3062 | } |
3060 | 3063 | ||
3061 | ipa = memdup_user(arg, sizeof(*ipa)); | 3064 | ipa = memdup_user(arg, sizeof(*ipa)); |
3062 | if (IS_ERR(ipa)) { | 3065 | if (IS_ERR(ipa)) { |
3063 | ret = PTR_ERR(ipa); | 3066 | ret = PTR_ERR(ipa); |
3064 | ipa = NULL; | 3067 | ipa = NULL; |
3065 | goto out; | 3068 | goto out; |
3066 | } | 3069 | } |
3067 | 3070 | ||
3068 | size = min_t(u32, ipa->size, 4096); | 3071 | size = min_t(u32, ipa->size, 4096); |
3069 | ipath = init_ipath(size, root, path); | 3072 | ipath = init_ipath(size, root, path); |
3070 | if (IS_ERR(ipath)) { | 3073 | if (IS_ERR(ipath)) { |
3071 | ret = PTR_ERR(ipath); | 3074 | ret = PTR_ERR(ipath); |
3072 | ipath = NULL; | 3075 | ipath = NULL; |
3073 | goto out; | 3076 | goto out; |
3074 | } | 3077 | } |
3075 | 3078 | ||
3076 | ret = paths_from_inode(ipa->inum, ipath); | 3079 | ret = paths_from_inode(ipa->inum, ipath); |
3077 | if (ret < 0) | 3080 | if (ret < 0) |
3078 | goto out; | 3081 | goto out; |
3079 | 3082 | ||
3080 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { | 3083 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { |
3081 | rel_ptr = ipath->fspath->val[i] - | 3084 | rel_ptr = ipath->fspath->val[i] - |
3082 | (u64)(unsigned long)ipath->fspath->val; | 3085 | (u64)(unsigned long)ipath->fspath->val; |
3083 | ipath->fspath->val[i] = rel_ptr; | 3086 | ipath->fspath->val[i] = rel_ptr; |
3084 | } | 3087 | } |
3085 | 3088 | ||
3086 | ret = copy_to_user((void *)(unsigned long)ipa->fspath, | 3089 | ret = copy_to_user((void *)(unsigned long)ipa->fspath, |
3087 | (void *)(unsigned long)ipath->fspath, size); | 3090 | (void *)(unsigned long)ipath->fspath, size); |
3088 | if (ret) { | 3091 | if (ret) { |
3089 | ret = -EFAULT; | 3092 | ret = -EFAULT; |
3090 | goto out; | 3093 | goto out; |
3091 | } | 3094 | } |
3092 | 3095 | ||
3093 | out: | 3096 | out: |
3094 | btrfs_free_path(path); | 3097 | btrfs_free_path(path); |
3095 | free_ipath(ipath); | 3098 | free_ipath(ipath); |
3096 | kfree(ipa); | 3099 | kfree(ipa); |
3097 | 3100 | ||
3098 | return ret; | 3101 | return ret; |
3099 | } | 3102 | } |
3100 | 3103 | ||
3101 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) | 3104 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) |
3102 | { | 3105 | { |
3103 | struct btrfs_data_container *inodes = ctx; | 3106 | struct btrfs_data_container *inodes = ctx; |
3104 | const size_t c = 3 * sizeof(u64); | 3107 | const size_t c = 3 * sizeof(u64); |
3105 | 3108 | ||
3106 | if (inodes->bytes_left >= c) { | 3109 | if (inodes->bytes_left >= c) { |
3107 | inodes->bytes_left -= c; | 3110 | inodes->bytes_left -= c; |
3108 | inodes->val[inodes->elem_cnt] = inum; | 3111 | inodes->val[inodes->elem_cnt] = inum; |
3109 | inodes->val[inodes->elem_cnt + 1] = offset; | 3112 | inodes->val[inodes->elem_cnt + 1] = offset; |
3110 | inodes->val[inodes->elem_cnt + 2] = root; | 3113 | inodes->val[inodes->elem_cnt + 2] = root; |
3111 | inodes->elem_cnt += 3; | 3114 | inodes->elem_cnt += 3; |
3112 | } else { | 3115 | } else { |
3113 | inodes->bytes_missing += c - inodes->bytes_left; | 3116 | inodes->bytes_missing += c - inodes->bytes_left; |
3114 | inodes->bytes_left = 0; | 3117 | inodes->bytes_left = 0; |
3115 | inodes->elem_missed += 3; | 3118 | inodes->elem_missed += 3; |
3116 | } | 3119 | } |
3117 | 3120 | ||
3118 | return 0; | 3121 | return 0; |
3119 | } | 3122 | } |
3120 | 3123 | ||
3121 | static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, | 3124 | static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, |
3122 | void __user *arg) | 3125 | void __user *arg) |
3123 | { | 3126 | { |
3124 | int ret = 0; | 3127 | int ret = 0; |
3125 | int size; | 3128 | int size; |
3126 | u64 extent_item_pos; | 3129 | u64 extent_item_pos; |
3127 | struct btrfs_ioctl_logical_ino_args *loi; | 3130 | struct btrfs_ioctl_logical_ino_args *loi; |
3128 | struct btrfs_data_container *inodes = NULL; | 3131 | struct btrfs_data_container *inodes = NULL; |
3129 | struct btrfs_path *path = NULL; | 3132 | struct btrfs_path *path = NULL; |
3130 | struct btrfs_key key; | 3133 | struct btrfs_key key; |
3131 | 3134 | ||
3132 | if (!capable(CAP_SYS_ADMIN)) | 3135 | if (!capable(CAP_SYS_ADMIN)) |
3133 | return -EPERM; | 3136 | return -EPERM; |
3134 | 3137 | ||
3135 | loi = memdup_user(arg, sizeof(*loi)); | 3138 | loi = memdup_user(arg, sizeof(*loi)); |
3136 | if (IS_ERR(loi)) { | 3139 | if (IS_ERR(loi)) { |
3137 | ret = PTR_ERR(loi); | 3140 | ret = PTR_ERR(loi); |
3138 | loi = NULL; | 3141 | loi = NULL; |
3139 | goto out; | 3142 | goto out; |
3140 | } | 3143 | } |
3141 | 3144 | ||
3142 | path = btrfs_alloc_path(); | 3145 | path = btrfs_alloc_path(); |
3143 | if (!path) { | 3146 | if (!path) { |
3144 | ret = -ENOMEM; | 3147 | ret = -ENOMEM; |
3145 | goto out; | 3148 | goto out; |
3146 | } | 3149 | } |
3147 | 3150 | ||
3148 | size = min_t(u32, loi->size, 4096); | 3151 | size = min_t(u32, loi->size, 4096); |
3149 | inodes = init_data_container(size); | 3152 | inodes = init_data_container(size); |
3150 | if (IS_ERR(inodes)) { | 3153 | if (IS_ERR(inodes)) { |
3151 | ret = PTR_ERR(inodes); | 3154 | ret = PTR_ERR(inodes); |
3152 | inodes = NULL; | 3155 | inodes = NULL; |
3153 | goto out; | 3156 | goto out; |
3154 | } | 3157 | } |
3155 | 3158 | ||
3156 | ret = extent_from_logical(root->fs_info, loi->logical, path, &key); | 3159 | ret = extent_from_logical(root->fs_info, loi->logical, path, &key); |
3157 | btrfs_release_path(path); | 3160 | btrfs_release_path(path); |
3158 | 3161 | ||
3159 | if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) | 3162 | if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) |
3160 | ret = -ENOENT; | 3163 | ret = -ENOENT; |
3161 | if (ret < 0) | 3164 | if (ret < 0) |
3162 | goto out; | 3165 | goto out; |
3163 | 3166 | ||
3164 | extent_item_pos = loi->logical - key.objectid; | 3167 | extent_item_pos = loi->logical - key.objectid; |
3165 | ret = iterate_extent_inodes(root->fs_info, key.objectid, | 3168 | ret = iterate_extent_inodes(root->fs_info, key.objectid, |
3166 | extent_item_pos, 0, build_ino_list, | 3169 | extent_item_pos, 0, build_ino_list, |
3167 | inodes); | 3170 | inodes); |
3168 | 3171 | ||
3169 | if (ret < 0) | 3172 | if (ret < 0) |
3170 | goto out; | 3173 | goto out; |
3171 | 3174 | ||
3172 | ret = copy_to_user((void *)(unsigned long)loi->inodes, | 3175 | ret = copy_to_user((void *)(unsigned long)loi->inodes, |
3173 | (void *)(unsigned long)inodes, size); | 3176 | (void *)(unsigned long)inodes, size); |
3174 | if (ret) | 3177 | if (ret) |
3175 | ret = -EFAULT; | 3178 | ret = -EFAULT; |
3176 | 3179 | ||
3177 | out: | 3180 | out: |
3178 | btrfs_free_path(path); | 3181 | btrfs_free_path(path); |
3179 | kfree(inodes); | 3182 | kfree(inodes); |
3180 | kfree(loi); | 3183 | kfree(loi); |
3181 | 3184 | ||
3182 | return ret; | 3185 | return ret; |
3183 | } | 3186 | } |
3184 | 3187 | ||
3185 | void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, | 3188 | void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, |
3186 | struct btrfs_ioctl_balance_args *bargs) | 3189 | struct btrfs_ioctl_balance_args *bargs) |
3187 | { | 3190 | { |
3188 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | 3191 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; |
3189 | 3192 | ||
3190 | bargs->flags = bctl->flags; | 3193 | bargs->flags = bctl->flags; |
3191 | 3194 | ||
3192 | if (atomic_read(&fs_info->balance_running)) | 3195 | if (atomic_read(&fs_info->balance_running)) |
3193 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; | 3196 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; |
3194 | if (atomic_read(&fs_info->balance_pause_req)) | 3197 | if (atomic_read(&fs_info->balance_pause_req)) |
3195 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; | 3198 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; |
3196 | if (atomic_read(&fs_info->balance_cancel_req)) | 3199 | if (atomic_read(&fs_info->balance_cancel_req)) |
3197 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; | 3200 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; |
3198 | 3201 | ||
3199 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); | 3202 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
3200 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); | 3203 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); |
3201 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); | 3204 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); |
3202 | 3205 | ||
3203 | if (lock) { | 3206 | if (lock) { |
3204 | spin_lock(&fs_info->balance_lock); | 3207 | spin_lock(&fs_info->balance_lock); |
3205 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); | 3208 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); |
3206 | spin_unlock(&fs_info->balance_lock); | 3209 | spin_unlock(&fs_info->balance_lock); |
3207 | } else { | 3210 | } else { |
3208 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); | 3211 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); |
3209 | } | 3212 | } |
3210 | } | 3213 | } |
3211 | 3214 | ||
3212 | static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg) | 3215 | static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg) |
3213 | { | 3216 | { |
3214 | struct btrfs_fs_info *fs_info = root->fs_info; | 3217 | struct btrfs_fs_info *fs_info = root->fs_info; |
3215 | struct btrfs_ioctl_balance_args *bargs; | 3218 | struct btrfs_ioctl_balance_args *bargs; |
3216 | struct btrfs_balance_control *bctl; | 3219 | struct btrfs_balance_control *bctl; |
3217 | int ret; | 3220 | int ret; |
3218 | 3221 | ||
3219 | if (!capable(CAP_SYS_ADMIN)) | 3222 | if (!capable(CAP_SYS_ADMIN)) |
3220 | return -EPERM; | 3223 | return -EPERM; |
3221 | 3224 | ||
3222 | if (fs_info->sb->s_flags & MS_RDONLY) | 3225 | if (fs_info->sb->s_flags & MS_RDONLY) |
3223 | return -EROFS; | 3226 | return -EROFS; |
3224 | 3227 | ||
3225 | mutex_lock(&fs_info->volume_mutex); | 3228 | mutex_lock(&fs_info->volume_mutex); |
3226 | mutex_lock(&fs_info->balance_mutex); | 3229 | mutex_lock(&fs_info->balance_mutex); |
3227 | 3230 | ||
3228 | if (arg) { | 3231 | if (arg) { |
3229 | bargs = memdup_user(arg, sizeof(*bargs)); | 3232 | bargs = memdup_user(arg, sizeof(*bargs)); |
3230 | if (IS_ERR(bargs)) { | 3233 | if (IS_ERR(bargs)) { |
3231 | ret = PTR_ERR(bargs); | 3234 | ret = PTR_ERR(bargs); |
3232 | goto out; | 3235 | goto out; |
3233 | } | 3236 | } |
3234 | 3237 | ||
3235 | if (bargs->flags & BTRFS_BALANCE_RESUME) { | 3238 | if (bargs->flags & BTRFS_BALANCE_RESUME) { |
3236 | if (!fs_info->balance_ctl) { | 3239 | if (!fs_info->balance_ctl) { |
3237 | ret = -ENOTCONN; | 3240 | ret = -ENOTCONN; |
3238 | goto out_bargs; | 3241 | goto out_bargs; |
3239 | } | 3242 | } |
3240 | 3243 | ||
3241 | bctl = fs_info->balance_ctl; | 3244 | bctl = fs_info->balance_ctl; |
3242 | spin_lock(&fs_info->balance_lock); | 3245 | spin_lock(&fs_info->balance_lock); |
3243 | bctl->flags |= BTRFS_BALANCE_RESUME; | 3246 | bctl->flags |= BTRFS_BALANCE_RESUME; |
3244 | spin_unlock(&fs_info->balance_lock); | 3247 | spin_unlock(&fs_info->balance_lock); |
3245 | 3248 | ||
3246 | goto do_balance; | 3249 | goto do_balance; |
3247 | } | 3250 | } |
3248 | } else { | 3251 | } else { |
3249 | bargs = NULL; | 3252 | bargs = NULL; |
3250 | } | 3253 | } |
3251 | 3254 | ||
3252 | if (fs_info->balance_ctl) { | 3255 | if (fs_info->balance_ctl) { |
3253 | ret = -EINPROGRESS; | 3256 | ret = -EINPROGRESS; |
3254 | goto out_bargs; | 3257 | goto out_bargs; |
3255 | } | 3258 | } |
3256 | 3259 | ||
3257 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); | 3260 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
3258 | if (!bctl) { | 3261 | if (!bctl) { |
3259 | ret = -ENOMEM; | 3262 | ret = -ENOMEM; |
3260 | goto out_bargs; | 3263 | goto out_bargs; |
3261 | } | 3264 | } |
3262 | 3265 | ||
3263 | bctl->fs_info = fs_info; | 3266 | bctl->fs_info = fs_info; |
3264 | if (arg) { | 3267 | if (arg) { |
3265 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); | 3268 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); |
3266 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); | 3269 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); |
3267 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); | 3270 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); |
3268 | 3271 | ||
3269 | bctl->flags = bargs->flags; | 3272 | bctl->flags = bargs->flags; |
3270 | } else { | 3273 | } else { |
3271 | /* balance everything - no filters */ | 3274 | /* balance everything - no filters */ |
3272 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; | 3275 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; |
3273 | } | 3276 | } |
3274 | 3277 | ||
3275 | do_balance: | 3278 | do_balance: |
3276 | ret = btrfs_balance(bctl, bargs); | 3279 | ret = btrfs_balance(bctl, bargs); |
3277 | /* | 3280 | /* |
3278 | * bctl is freed in __cancel_balance or in free_fs_info if | 3281 | * bctl is freed in __cancel_balance or in free_fs_info if |
3279 | * restriper was paused all the way until unmount | 3282 | * restriper was paused all the way until unmount |
3280 | */ | 3283 | */ |
3281 | if (arg) { | 3284 | if (arg) { |
3282 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | 3285 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
3283 | ret = -EFAULT; | 3286 | ret = -EFAULT; |
3284 | } | 3287 | } |
3285 | 3288 | ||
3286 | out_bargs: | 3289 | out_bargs: |
3287 | kfree(bargs); | 3290 | kfree(bargs); |
3288 | out: | 3291 | out: |
3289 | mutex_unlock(&fs_info->balance_mutex); | 3292 | mutex_unlock(&fs_info->balance_mutex); |
3290 | mutex_unlock(&fs_info->volume_mutex); | 3293 | mutex_unlock(&fs_info->volume_mutex); |
3291 | return ret; | 3294 | return ret; |
3292 | } | 3295 | } |
3293 | 3296 | ||
3294 | static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) | 3297 | static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) |
3295 | { | 3298 | { |
3296 | if (!capable(CAP_SYS_ADMIN)) | 3299 | if (!capable(CAP_SYS_ADMIN)) |
3297 | return -EPERM; | 3300 | return -EPERM; |
3298 | 3301 | ||
3299 | switch (cmd) { | 3302 | switch (cmd) { |
3300 | case BTRFS_BALANCE_CTL_PAUSE: | 3303 | case BTRFS_BALANCE_CTL_PAUSE: |
3301 | return btrfs_pause_balance(root->fs_info); | 3304 | return btrfs_pause_balance(root->fs_info); |
3302 | case BTRFS_BALANCE_CTL_CANCEL: | 3305 | case BTRFS_BALANCE_CTL_CANCEL: |
3303 | return btrfs_cancel_balance(root->fs_info); | 3306 | return btrfs_cancel_balance(root->fs_info); |
3304 | } | 3307 | } |
3305 | 3308 | ||
3306 | return -EINVAL; | 3309 | return -EINVAL; |
3307 | } | 3310 | } |
3308 | 3311 | ||
3309 | static long btrfs_ioctl_balance_progress(struct btrfs_root *root, | 3312 | static long btrfs_ioctl_balance_progress(struct btrfs_root *root, |
3310 | void __user *arg) | 3313 | void __user *arg) |
3311 | { | 3314 | { |
3312 | struct btrfs_fs_info *fs_info = root->fs_info; | 3315 | struct btrfs_fs_info *fs_info = root->fs_info; |
3313 | struct btrfs_ioctl_balance_args *bargs; | 3316 | struct btrfs_ioctl_balance_args *bargs; |
3314 | int ret = 0; | 3317 | int ret = 0; |
3315 | 3318 | ||
3316 | if (!capable(CAP_SYS_ADMIN)) | 3319 | if (!capable(CAP_SYS_ADMIN)) |
3317 | return -EPERM; | 3320 | return -EPERM; |
3318 | 3321 | ||
3319 | mutex_lock(&fs_info->balance_mutex); | 3322 | mutex_lock(&fs_info->balance_mutex); |
3320 | if (!fs_info->balance_ctl) { | 3323 | if (!fs_info->balance_ctl) { |
3321 | ret = -ENOTCONN; | 3324 | ret = -ENOTCONN; |
3322 | goto out; | 3325 | goto out; |
3323 | } | 3326 | } |
3324 | 3327 | ||
3325 | bargs = kzalloc(sizeof(*bargs), GFP_NOFS); | 3328 | bargs = kzalloc(sizeof(*bargs), GFP_NOFS); |
3326 | if (!bargs) { | 3329 | if (!bargs) { |
3327 | ret = -ENOMEM; | 3330 | ret = -ENOMEM; |
3328 | goto out; | 3331 | goto out; |
3329 | } | 3332 | } |
3330 | 3333 | ||
3331 | update_ioctl_balance_args(fs_info, 1, bargs); | 3334 | update_ioctl_balance_args(fs_info, 1, bargs); |
3332 | 3335 | ||
3333 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | 3336 | if (copy_to_user(arg, bargs, sizeof(*bargs))) |
3334 | ret = -EFAULT; | 3337 | ret = -EFAULT; |
3335 | 3338 | ||
3336 | kfree(bargs); | 3339 | kfree(bargs); |
3337 | out: | 3340 | out: |
3338 | mutex_unlock(&fs_info->balance_mutex); | 3341 | mutex_unlock(&fs_info->balance_mutex); |
3339 | return ret; | 3342 | return ret; |
3340 | } | 3343 | } |
3341 | 3344 | ||
3342 | long btrfs_ioctl(struct file *file, unsigned int | 3345 | long btrfs_ioctl(struct file *file, unsigned int |
3343 | cmd, unsigned long arg) | 3346 | cmd, unsigned long arg) |
3344 | { | 3347 | { |
3345 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; | 3348 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
3346 | void __user *argp = (void __user *)arg; | 3349 | void __user *argp = (void __user *)arg; |
3347 | 3350 | ||
3348 | switch (cmd) { | 3351 | switch (cmd) { |
3349 | case FS_IOC_GETFLAGS: | 3352 | case FS_IOC_GETFLAGS: |
3350 | return btrfs_ioctl_getflags(file, argp); | 3353 | return btrfs_ioctl_getflags(file, argp); |
3351 | case FS_IOC_SETFLAGS: | 3354 | case FS_IOC_SETFLAGS: |
3352 | return btrfs_ioctl_setflags(file, argp); | 3355 | return btrfs_ioctl_setflags(file, argp); |
3353 | case FS_IOC_GETVERSION: | 3356 | case FS_IOC_GETVERSION: |
3354 | return btrfs_ioctl_getversion(file, argp); | 3357 | return btrfs_ioctl_getversion(file, argp); |
3355 | case FITRIM: | 3358 | case FITRIM: |
3356 | return btrfs_ioctl_fitrim(file, argp); | 3359 | return btrfs_ioctl_fitrim(file, argp); |
3357 | case BTRFS_IOC_SNAP_CREATE: | 3360 | case BTRFS_IOC_SNAP_CREATE: |
3358 | return btrfs_ioctl_snap_create(file, argp, 0); | 3361 | return btrfs_ioctl_snap_create(file, argp, 0); |
3359 | case BTRFS_IOC_SNAP_CREATE_V2: | 3362 | case BTRFS_IOC_SNAP_CREATE_V2: |
3360 | return btrfs_ioctl_snap_create_v2(file, argp, 0); | 3363 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
3361 | case BTRFS_IOC_SUBVOL_CREATE: | 3364 | case BTRFS_IOC_SUBVOL_CREATE: |
3362 | return btrfs_ioctl_snap_create(file, argp, 1); | 3365 | return btrfs_ioctl_snap_create(file, argp, 1); |
3363 | case BTRFS_IOC_SNAP_DESTROY: | 3366 | case BTRFS_IOC_SNAP_DESTROY: |
3364 | return btrfs_ioctl_snap_destroy(file, argp); | 3367 | return btrfs_ioctl_snap_destroy(file, argp); |
3365 | case BTRFS_IOC_SUBVOL_GETFLAGS: | 3368 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
3366 | return btrfs_ioctl_subvol_getflags(file, argp); | 3369 | return btrfs_ioctl_subvol_getflags(file, argp); |
3367 | case BTRFS_IOC_SUBVOL_SETFLAGS: | 3370 | case BTRFS_IOC_SUBVOL_SETFLAGS: |
3368 | return btrfs_ioctl_subvol_setflags(file, argp); | 3371 | return btrfs_ioctl_subvol_setflags(file, argp); |
3369 | case BTRFS_IOC_DEFAULT_SUBVOL: | 3372 | case BTRFS_IOC_DEFAULT_SUBVOL: |
3370 | return btrfs_ioctl_default_subvol(file, argp); | 3373 | return btrfs_ioctl_default_subvol(file, argp); |
3371 | case BTRFS_IOC_DEFRAG: | 3374 | case BTRFS_IOC_DEFRAG: |
3372 | return btrfs_ioctl_defrag(file, NULL); | 3375 | return btrfs_ioctl_defrag(file, NULL); |
3373 | case BTRFS_IOC_DEFRAG_RANGE: | 3376 | case BTRFS_IOC_DEFRAG_RANGE: |
3374 | return btrfs_ioctl_defrag(file, argp); | 3377 | return btrfs_ioctl_defrag(file, argp); |
3375 | case BTRFS_IOC_RESIZE: | 3378 | case BTRFS_IOC_RESIZE: |
3376 | return btrfs_ioctl_resize(root, argp); | 3379 | return btrfs_ioctl_resize(root, argp); |
3377 | case BTRFS_IOC_ADD_DEV: | 3380 | case BTRFS_IOC_ADD_DEV: |
3378 | return btrfs_ioctl_add_dev(root, argp); | 3381 | return btrfs_ioctl_add_dev(root, argp); |
3379 | case BTRFS_IOC_RM_DEV: | 3382 | case BTRFS_IOC_RM_DEV: |
3380 | return btrfs_ioctl_rm_dev(root, argp); | 3383 | return btrfs_ioctl_rm_dev(root, argp); |
3381 | case BTRFS_IOC_FS_INFO: | 3384 | case BTRFS_IOC_FS_INFO: |
3382 | return btrfs_ioctl_fs_info(root, argp); | 3385 | return btrfs_ioctl_fs_info(root, argp); |
3383 | case BTRFS_IOC_DEV_INFO: | 3386 | case BTRFS_IOC_DEV_INFO: |
3384 | return btrfs_ioctl_dev_info(root, argp); | 3387 | return btrfs_ioctl_dev_info(root, argp); |
3385 | case BTRFS_IOC_BALANCE: | 3388 | case BTRFS_IOC_BALANCE: |
3386 | return btrfs_ioctl_balance(root, NULL); | 3389 | return btrfs_ioctl_balance(root, NULL); |
3387 | case BTRFS_IOC_CLONE: | 3390 | case BTRFS_IOC_CLONE: |
3388 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); | 3391 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
3389 | case BTRFS_IOC_CLONE_RANGE: | 3392 | case BTRFS_IOC_CLONE_RANGE: |
3390 | return btrfs_ioctl_clone_range(file, argp); | 3393 | return btrfs_ioctl_clone_range(file, argp); |
3391 | case BTRFS_IOC_TRANS_START: | 3394 | case BTRFS_IOC_TRANS_START: |
3392 | return btrfs_ioctl_trans_start(file); | 3395 | return btrfs_ioctl_trans_start(file); |
3393 | case BTRFS_IOC_TRANS_END: | 3396 | case BTRFS_IOC_TRANS_END: |
3394 | return btrfs_ioctl_trans_end(file); | 3397 | return btrfs_ioctl_trans_end(file); |
3395 | case BTRFS_IOC_TREE_SEARCH: | 3398 | case BTRFS_IOC_TREE_SEARCH: |
3396 | return btrfs_ioctl_tree_search(file, argp); | 3399 | return btrfs_ioctl_tree_search(file, argp); |
3397 | case BTRFS_IOC_INO_LOOKUP: | 3400 | case BTRFS_IOC_INO_LOOKUP: |
3398 | return btrfs_ioctl_ino_lookup(file, argp); | 3401 | return btrfs_ioctl_ino_lookup(file, argp); |
3399 | case BTRFS_IOC_INO_PATHS: | 3402 | case BTRFS_IOC_INO_PATHS: |
3400 | return btrfs_ioctl_ino_to_path(root, argp); | 3403 | return btrfs_ioctl_ino_to_path(root, argp); |
3401 | case BTRFS_IOC_LOGICAL_INO: | 3404 | case BTRFS_IOC_LOGICAL_INO: |
3402 | return btrfs_ioctl_logical_to_ino(root, argp); | 3405 | return btrfs_ioctl_logical_to_ino(root, argp); |
3403 | case BTRFS_IOC_SPACE_INFO: | 3406 | case BTRFS_IOC_SPACE_INFO: |
3404 | return btrfs_ioctl_space_info(root, argp); | 3407 | return btrfs_ioctl_space_info(root, argp); |
3405 | case BTRFS_IOC_SYNC: | 3408 | case BTRFS_IOC_SYNC: |
3406 | btrfs_sync_fs(file->f_dentry->d_sb, 1); | 3409 | btrfs_sync_fs(file->f_dentry->d_sb, 1); |
3407 | return 0; | 3410 | return 0; |
3408 | case BTRFS_IOC_START_SYNC: | 3411 | case BTRFS_IOC_START_SYNC: |
3409 | return btrfs_ioctl_start_sync(file, argp); | 3412 | return btrfs_ioctl_start_sync(file, argp); |
3410 | case BTRFS_IOC_WAIT_SYNC: | 3413 | case BTRFS_IOC_WAIT_SYNC: |
3411 | return btrfs_ioctl_wait_sync(file, argp); | 3414 | return btrfs_ioctl_wait_sync(file, argp); |
3412 | case BTRFS_IOC_SCRUB: | 3415 | case BTRFS_IOC_SCRUB: |
3413 | return btrfs_ioctl_scrub(root, argp); | 3416 | return btrfs_ioctl_scrub(root, argp); |
3414 | case BTRFS_IOC_SCRUB_CANCEL: | 3417 | case BTRFS_IOC_SCRUB_CANCEL: |
3415 | return btrfs_ioctl_scrub_cancel(root, argp); | 3418 | return btrfs_ioctl_scrub_cancel(root, argp); |
3416 | case BTRFS_IOC_SCRUB_PROGRESS: | 3419 | case BTRFS_IOC_SCRUB_PROGRESS: |
3417 | return btrfs_ioctl_scrub_progress(root, argp); | 3420 | return btrfs_ioctl_scrub_progress(root, argp); |
3418 | case BTRFS_IOC_BALANCE_V2: | 3421 | case BTRFS_IOC_BALANCE_V2: |
3419 | return btrfs_ioctl_balance(root, argp); | 3422 | return btrfs_ioctl_balance(root, argp); |
3420 | case BTRFS_IOC_BALANCE_CTL: | 3423 | case BTRFS_IOC_BALANCE_CTL: |
3421 | return btrfs_ioctl_balance_ctl(root, arg); | 3424 | return btrfs_ioctl_balance_ctl(root, arg); |
3422 | case BTRFS_IOC_BALANCE_PROGRESS: | 3425 | case BTRFS_IOC_BALANCE_PROGRESS: |
3423 | return btrfs_ioctl_balance_progress(root, argp); | 3426 | return btrfs_ioctl_balance_progress(root, argp); |
3424 | } | 3427 | } |
3425 | 3428 | ||
3426 | return -ENOTTY; | 3429 | return -ENOTTY; |
3427 | } | 3430 | } |
3428 | 3431 |