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