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> |
8ea05e3a | 44 | #include <linux/uuid.h> |
55e301fd | 45 | #include <linux/btrfs.h> |
416161db | 46 | #include <linux/uaccess.h> |
4b4e25f2 | 47 | #include "compat.h" |
f46b5a66 CH |
48 | #include "ctree.h" |
49 | #include "disk-io.h" | |
50 | #include "transaction.h" | |
51 | #include "btrfs_inode.h" | |
f46b5a66 CH |
52 | #include "print-tree.h" |
53 | #include "volumes.h" | |
925baedd | 54 | #include "locking.h" |
581bb050 | 55 | #include "inode-map.h" |
d7728c96 | 56 | #include "backref.h" |
606686ee | 57 | #include "rcu-string.h" |
31db9f7c | 58 | #include "send.h" |
3f6bcfbd | 59 | #include "dev-replace.h" |
f46b5a66 | 60 | |
416161db MF |
61 | static int btrfs_clone(struct inode *src, struct inode *inode, |
62 | u64 off, u64 olen, u64 olen_aligned, u64 destoff); | |
63 | ||
6cbff00f CH |
64 | /* Mask out flags that are inappropriate for the given type of inode. */ |
65 | static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) | |
66 | { | |
67 | if (S_ISDIR(mode)) | |
68 | return flags; | |
69 | else if (S_ISREG(mode)) | |
70 | return flags & ~FS_DIRSYNC_FL; | |
71 | else | |
72 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); | |
73 | } | |
74 | ||
75 | /* | |
76 | * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. | |
77 | */ | |
78 | static unsigned int btrfs_flags_to_ioctl(unsigned int flags) | |
79 | { | |
80 | unsigned int iflags = 0; | |
81 | ||
82 | if (flags & BTRFS_INODE_SYNC) | |
83 | iflags |= FS_SYNC_FL; | |
84 | if (flags & BTRFS_INODE_IMMUTABLE) | |
85 | iflags |= FS_IMMUTABLE_FL; | |
86 | if (flags & BTRFS_INODE_APPEND) | |
87 | iflags |= FS_APPEND_FL; | |
88 | if (flags & BTRFS_INODE_NODUMP) | |
89 | iflags |= FS_NODUMP_FL; | |
90 | if (flags & BTRFS_INODE_NOATIME) | |
91 | iflags |= FS_NOATIME_FL; | |
92 | if (flags & BTRFS_INODE_DIRSYNC) | |
93 | iflags |= FS_DIRSYNC_FL; | |
d0092bdd LZ |
94 | if (flags & BTRFS_INODE_NODATACOW) |
95 | iflags |= FS_NOCOW_FL; | |
96 | ||
97 | if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) | |
98 | iflags |= FS_COMPR_FL; | |
99 | else if (flags & BTRFS_INODE_NOCOMPRESS) | |
100 | iflags |= FS_NOCOMP_FL; | |
6cbff00f CH |
101 | |
102 | return iflags; | |
103 | } | |
104 | ||
105 | /* | |
106 | * Update inode->i_flags based on the btrfs internal flags. | |
107 | */ | |
108 | void btrfs_update_iflags(struct inode *inode) | |
109 | { | |
110 | struct btrfs_inode *ip = BTRFS_I(inode); | |
111 | ||
112 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); | |
113 | ||
114 | if (ip->flags & BTRFS_INODE_SYNC) | |
115 | inode->i_flags |= S_SYNC; | |
116 | if (ip->flags & BTRFS_INODE_IMMUTABLE) | |
117 | inode->i_flags |= S_IMMUTABLE; | |
118 | if (ip->flags & BTRFS_INODE_APPEND) | |
119 | inode->i_flags |= S_APPEND; | |
120 | if (ip->flags & BTRFS_INODE_NOATIME) | |
121 | inode->i_flags |= S_NOATIME; | |
122 | if (ip->flags & BTRFS_INODE_DIRSYNC) | |
123 | inode->i_flags |= S_DIRSYNC; | |
124 | } | |
125 | ||
126 | /* | |
127 | * Inherit flags from the parent inode. | |
128 | * | |
e27425d6 | 129 | * Currently only the compression flags and the cow flags are inherited. |
6cbff00f CH |
130 | */ |
131 | void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
132 | { | |
0b4dcea5 CM |
133 | unsigned int flags; |
134 | ||
135 | if (!dir) | |
136 | return; | |
137 | ||
138 | flags = BTRFS_I(dir)->flags; | |
6cbff00f | 139 | |
e27425d6 JB |
140 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
141 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | |
142 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
143 | } else if (flags & BTRFS_INODE_COMPRESS) { | |
144 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
145 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | |
146 | } | |
147 | ||
213490b3 | 148 | if (flags & BTRFS_INODE_NODATACOW) { |
e27425d6 | 149 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; |
213490b3 LB |
150 | if (S_ISREG(inode->i_mode)) |
151 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM; | |
152 | } | |
6cbff00f | 153 | |
6cbff00f CH |
154 | btrfs_update_iflags(inode); |
155 | } | |
156 | ||
157 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) | |
158 | { | |
496ad9aa | 159 | struct btrfs_inode *ip = BTRFS_I(file_inode(file)); |
6cbff00f CH |
160 | unsigned int flags = btrfs_flags_to_ioctl(ip->flags); |
161 | ||
162 | if (copy_to_user(arg, &flags, sizeof(flags))) | |
163 | return -EFAULT; | |
164 | return 0; | |
165 | } | |
166 | ||
75e7cb7f LB |
167 | static int check_flags(unsigned int flags) |
168 | { | |
169 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ | |
170 | FS_NOATIME_FL | FS_NODUMP_FL | \ | |
171 | FS_SYNC_FL | FS_DIRSYNC_FL | \ | |
e1e8fb6a LZ |
172 | FS_NOCOMP_FL | FS_COMPR_FL | |
173 | FS_NOCOW_FL)) | |
75e7cb7f LB |
174 | return -EOPNOTSUPP; |
175 | ||
176 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) | |
177 | return -EINVAL; | |
178 | ||
75e7cb7f LB |
179 | return 0; |
180 | } | |
181 | ||
6cbff00f CH |
182 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) |
183 | { | |
496ad9aa | 184 | struct inode *inode = file_inode(file); |
6cbff00f CH |
185 | struct btrfs_inode *ip = BTRFS_I(inode); |
186 | struct btrfs_root *root = ip->root; | |
187 | struct btrfs_trans_handle *trans; | |
188 | unsigned int flags, oldflags; | |
189 | int ret; | |
f062abf0 LZ |
190 | u64 ip_oldflags; |
191 | unsigned int i_oldflags; | |
7e97b8da | 192 | umode_t mode; |
6cbff00f | 193 | |
b83cc969 LZ |
194 | if (btrfs_root_readonly(root)) |
195 | return -EROFS; | |
196 | ||
6cbff00f CH |
197 | if (copy_from_user(&flags, arg, sizeof(flags))) |
198 | return -EFAULT; | |
199 | ||
75e7cb7f LB |
200 | ret = check_flags(flags); |
201 | if (ret) | |
202 | return ret; | |
f46b5a66 | 203 | |
2e149670 | 204 | if (!inode_owner_or_capable(inode)) |
6cbff00f CH |
205 | return -EACCES; |
206 | ||
e7848683 JK |
207 | ret = mnt_want_write_file(file); |
208 | if (ret) | |
209 | return ret; | |
210 | ||
6cbff00f CH |
211 | mutex_lock(&inode->i_mutex); |
212 | ||
f062abf0 LZ |
213 | ip_oldflags = ip->flags; |
214 | i_oldflags = inode->i_flags; | |
7e97b8da | 215 | mode = inode->i_mode; |
f062abf0 | 216 | |
6cbff00f CH |
217 | flags = btrfs_mask_flags(inode->i_mode, flags); |
218 | oldflags = btrfs_flags_to_ioctl(ip->flags); | |
219 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { | |
220 | if (!capable(CAP_LINUX_IMMUTABLE)) { | |
221 | ret = -EPERM; | |
222 | goto out_unlock; | |
223 | } | |
224 | } | |
225 | ||
6cbff00f CH |
226 | if (flags & FS_SYNC_FL) |
227 | ip->flags |= BTRFS_INODE_SYNC; | |
228 | else | |
229 | ip->flags &= ~BTRFS_INODE_SYNC; | |
230 | if (flags & FS_IMMUTABLE_FL) | |
231 | ip->flags |= BTRFS_INODE_IMMUTABLE; | |
232 | else | |
233 | ip->flags &= ~BTRFS_INODE_IMMUTABLE; | |
234 | if (flags & FS_APPEND_FL) | |
235 | ip->flags |= BTRFS_INODE_APPEND; | |
236 | else | |
237 | ip->flags &= ~BTRFS_INODE_APPEND; | |
238 | if (flags & FS_NODUMP_FL) | |
239 | ip->flags |= BTRFS_INODE_NODUMP; | |
240 | else | |
241 | ip->flags &= ~BTRFS_INODE_NODUMP; | |
242 | if (flags & FS_NOATIME_FL) | |
243 | ip->flags |= BTRFS_INODE_NOATIME; | |
244 | else | |
245 | ip->flags &= ~BTRFS_INODE_NOATIME; | |
246 | if (flags & FS_DIRSYNC_FL) | |
247 | ip->flags |= BTRFS_INODE_DIRSYNC; | |
248 | else | |
249 | ip->flags &= ~BTRFS_INODE_DIRSYNC; | |
7e97b8da DS |
250 | if (flags & FS_NOCOW_FL) { |
251 | if (S_ISREG(mode)) { | |
252 | /* | |
253 | * It's safe to turn csums off here, no extents exist. | |
254 | * Otherwise we want the flag to reflect the real COW | |
255 | * status of the file and will not set it. | |
256 | */ | |
257 | if (inode->i_size == 0) | |
258 | ip->flags |= BTRFS_INODE_NODATACOW | |
259 | | BTRFS_INODE_NODATASUM; | |
260 | } else { | |
261 | ip->flags |= BTRFS_INODE_NODATACOW; | |
262 | } | |
263 | } else { | |
264 | /* | |
265 | * Revert back under same assuptions as above | |
266 | */ | |
267 | if (S_ISREG(mode)) { | |
268 | if (inode->i_size == 0) | |
269 | ip->flags &= ~(BTRFS_INODE_NODATACOW | |
270 | | BTRFS_INODE_NODATASUM); | |
271 | } else { | |
272 | ip->flags &= ~BTRFS_INODE_NODATACOW; | |
273 | } | |
274 | } | |
6cbff00f | 275 | |
75e7cb7f LB |
276 | /* |
277 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS | |
278 | * flag may be changed automatically if compression code won't make | |
279 | * things smaller. | |
280 | */ | |
281 | if (flags & FS_NOCOMP_FL) { | |
282 | ip->flags &= ~BTRFS_INODE_COMPRESS; | |
283 | ip->flags |= BTRFS_INODE_NOCOMPRESS; | |
284 | } else if (flags & FS_COMPR_FL) { | |
285 | ip->flags |= BTRFS_INODE_COMPRESS; | |
286 | ip->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
ebcb904d LZ |
287 | } else { |
288 | ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); | |
75e7cb7f | 289 | } |
6cbff00f | 290 | |
4da6f1a3 | 291 | trans = btrfs_start_transaction(root, 1); |
f062abf0 LZ |
292 | if (IS_ERR(trans)) { |
293 | ret = PTR_ERR(trans); | |
294 | goto out_drop; | |
295 | } | |
6cbff00f | 296 | |
306424cc | 297 | btrfs_update_iflags(inode); |
0c4d2d95 | 298 | inode_inc_iversion(inode); |
306424cc | 299 | inode->i_ctime = CURRENT_TIME; |
6cbff00f | 300 | ret = btrfs_update_inode(trans, root, inode); |
6cbff00f | 301 | |
6cbff00f | 302 | btrfs_end_transaction(trans, root); |
f062abf0 LZ |
303 | out_drop: |
304 | if (ret) { | |
305 | ip->flags = ip_oldflags; | |
306 | inode->i_flags = i_oldflags; | |
307 | } | |
6cbff00f | 308 | |
6cbff00f CH |
309 | out_unlock: |
310 | mutex_unlock(&inode->i_mutex); | |
e7848683 | 311 | mnt_drop_write_file(file); |
2d4e6f6a | 312 | return ret; |
6cbff00f CH |
313 | } |
314 | ||
315 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) | |
316 | { | |
496ad9aa | 317 | struct inode *inode = file_inode(file); |
6cbff00f CH |
318 | |
319 | return put_user(inode->i_generation, arg); | |
320 | } | |
f46b5a66 | 321 | |
f7039b1d LD |
322 | static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) |
323 | { | |
815745cf | 324 | struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb); |
f7039b1d LD |
325 | struct btrfs_device *device; |
326 | struct request_queue *q; | |
327 | struct fstrim_range range; | |
328 | u64 minlen = ULLONG_MAX; | |
329 | u64 num_devices = 0; | |
815745cf | 330 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
f7039b1d LD |
331 | int ret; |
332 | ||
333 | if (!capable(CAP_SYS_ADMIN)) | |
334 | return -EPERM; | |
335 | ||
1f78160c XG |
336 | rcu_read_lock(); |
337 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, | |
338 | dev_list) { | |
f7039b1d LD |
339 | if (!device->bdev) |
340 | continue; | |
341 | q = bdev_get_queue(device->bdev); | |
342 | if (blk_queue_discard(q)) { | |
343 | num_devices++; | |
344 | minlen = min((u64)q->limits.discard_granularity, | |
345 | minlen); | |
346 | } | |
347 | } | |
1f78160c | 348 | rcu_read_unlock(); |
f4c697e6 | 349 | |
f7039b1d LD |
350 | if (!num_devices) |
351 | return -EOPNOTSUPP; | |
f7039b1d LD |
352 | if (copy_from_user(&range, arg, sizeof(range))) |
353 | return -EFAULT; | |
e515c18b LC |
354 | if (range.start > total_bytes || |
355 | range.len < fs_info->sb->s_blocksize) | |
f4c697e6 | 356 | return -EINVAL; |
f7039b1d | 357 | |
f4c697e6 | 358 | range.len = min(range.len, total_bytes - range.start); |
f7039b1d | 359 | range.minlen = max(range.minlen, minlen); |
815745cf | 360 | ret = btrfs_trim_fs(fs_info->tree_root, &range); |
f7039b1d LD |
361 | if (ret < 0) |
362 | return ret; | |
363 | ||
364 | if (copy_to_user(arg, &range, sizeof(range))) | |
365 | return -EFAULT; | |
366 | ||
367 | return 0; | |
368 | } | |
369 | ||
d5c12070 | 370 | static noinline int create_subvol(struct inode *dir, |
cb8e7090 | 371 | struct dentry *dentry, |
72fd032e | 372 | char *name, int namelen, |
6f72c7e2 | 373 | u64 *async_transid, |
8696c533 | 374 | struct btrfs_qgroup_inherit *inherit) |
f46b5a66 CH |
375 | { |
376 | struct btrfs_trans_handle *trans; | |
377 | struct btrfs_key key; | |
378 | struct btrfs_root_item root_item; | |
379 | struct btrfs_inode_item *inode_item; | |
380 | struct extent_buffer *leaf; | |
d5c12070 | 381 | struct btrfs_root *root = BTRFS_I(dir)->root; |
76dda93c | 382 | struct btrfs_root *new_root; |
d5c12070 | 383 | struct btrfs_block_rsv block_rsv; |
8ea05e3a | 384 | struct timespec cur_time = CURRENT_TIME; |
f46b5a66 CH |
385 | int ret; |
386 | int err; | |
387 | u64 objectid; | |
388 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | |
3de4586c | 389 | u64 index = 0; |
d5c12070 | 390 | u64 qgroup_reserved; |
8ea05e3a | 391 | uuid_le new_uuid; |
f46b5a66 | 392 | |
581bb050 | 393 | ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); |
2fbe8c8a | 394 | if (ret) |
a22285a6 | 395 | return ret; |
6a912213 | 396 | |
d5c12070 | 397 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
9ed74f2d | 398 | /* |
d5c12070 MX |
399 | * The same as the snapshot creation, please see the comment |
400 | * of create_snapshot(). | |
9ed74f2d | 401 | */ |
d5c12070 | 402 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, |
ee3441b4 | 403 | 7, &qgroup_reserved, false); |
d5c12070 MX |
404 | if (ret) |
405 | return ret; | |
406 | ||
407 | trans = btrfs_start_transaction(root, 0); | |
408 | if (IS_ERR(trans)) { | |
409 | ret = PTR_ERR(trans); | |
410 | goto out; | |
411 | } | |
412 | trans->block_rsv = &block_rsv; | |
413 | trans->bytes_reserved = block_rsv.size; | |
f46b5a66 | 414 | |
8696c533 | 415 | ret = btrfs_qgroup_inherit(trans, root->fs_info, 0, objectid, inherit); |
6f72c7e2 AJ |
416 | if (ret) |
417 | goto fail; | |
418 | ||
5d4f98a2 | 419 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
5581a51a | 420 | 0, objectid, NULL, 0, 0, 0); |
8e8a1e31 JB |
421 | if (IS_ERR(leaf)) { |
422 | ret = PTR_ERR(leaf); | |
423 | goto fail; | |
424 | } | |
f46b5a66 | 425 | |
5d4f98a2 | 426 | memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); |
f46b5a66 CH |
427 | btrfs_set_header_bytenr(leaf, leaf->start); |
428 | btrfs_set_header_generation(leaf, trans->transid); | |
5d4f98a2 | 429 | btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); |
f46b5a66 CH |
430 | btrfs_set_header_owner(leaf, objectid); |
431 | ||
432 | write_extent_buffer(leaf, root->fs_info->fsid, | |
433 | (unsigned long)btrfs_header_fsid(leaf), | |
434 | BTRFS_FSID_SIZE); | |
5d4f98a2 YZ |
435 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
436 | (unsigned long)btrfs_header_chunk_tree_uuid(leaf), | |
437 | BTRFS_UUID_SIZE); | |
f46b5a66 CH |
438 | btrfs_mark_buffer_dirty(leaf); |
439 | ||
8ea05e3a AB |
440 | memset(&root_item, 0, sizeof(root_item)); |
441 | ||
f46b5a66 | 442 | inode_item = &root_item.inode; |
3cae210f QW |
443 | btrfs_set_stack_inode_generation(inode_item, 1); |
444 | btrfs_set_stack_inode_size(inode_item, 3); | |
445 | btrfs_set_stack_inode_nlink(inode_item, 1); | |
446 | btrfs_set_stack_inode_nbytes(inode_item, root->leafsize); | |
447 | btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); | |
f46b5a66 | 448 | |
3cae210f QW |
449 | btrfs_set_root_flags(&root_item, 0); |
450 | btrfs_set_root_limit(&root_item, 0); | |
451 | btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); | |
08fe4db1 | 452 | |
f46b5a66 | 453 | btrfs_set_root_bytenr(&root_item, leaf->start); |
84234f3a | 454 | btrfs_set_root_generation(&root_item, trans->transid); |
f46b5a66 CH |
455 | btrfs_set_root_level(&root_item, 0); |
456 | btrfs_set_root_refs(&root_item, 1); | |
86b9f2ec | 457 | btrfs_set_root_used(&root_item, leaf->len); |
80ff3856 | 458 | btrfs_set_root_last_snapshot(&root_item, 0); |
f46b5a66 | 459 | |
8ea05e3a AB |
460 | btrfs_set_root_generation_v2(&root_item, |
461 | btrfs_root_generation(&root_item)); | |
462 | uuid_le_gen(&new_uuid); | |
463 | memcpy(root_item.uuid, new_uuid.b, BTRFS_UUID_SIZE); | |
3cae210f QW |
464 | btrfs_set_stack_timespec_sec(&root_item.otime, cur_time.tv_sec); |
465 | btrfs_set_stack_timespec_nsec(&root_item.otime, cur_time.tv_nsec); | |
8ea05e3a AB |
466 | root_item.ctime = root_item.otime; |
467 | btrfs_set_root_ctransid(&root_item, trans->transid); | |
468 | btrfs_set_root_otransid(&root_item, trans->transid); | |
f46b5a66 | 469 | |
925baedd | 470 | btrfs_tree_unlock(leaf); |
f46b5a66 CH |
471 | free_extent_buffer(leaf); |
472 | leaf = NULL; | |
473 | ||
474 | btrfs_set_root_dirid(&root_item, new_dirid); | |
475 | ||
476 | key.objectid = objectid; | |
5d4f98a2 | 477 | key.offset = 0; |
f46b5a66 CH |
478 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
479 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | |
480 | &root_item); | |
481 | if (ret) | |
482 | goto fail; | |
483 | ||
76dda93c YZ |
484 | key.offset = (u64)-1; |
485 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); | |
79787eaa JM |
486 | if (IS_ERR(new_root)) { |
487 | btrfs_abort_transaction(trans, root, PTR_ERR(new_root)); | |
488 | ret = PTR_ERR(new_root); | |
489 | goto fail; | |
490 | } | |
76dda93c YZ |
491 | |
492 | btrfs_record_root_in_trans(trans, new_root); | |
493 | ||
d82a6f1d | 494 | ret = btrfs_create_subvol_root(trans, new_root, new_dirid); |
ce598979 MF |
495 | if (ret) { |
496 | /* We potentially lose an unused inode item here */ | |
79787eaa | 497 | btrfs_abort_transaction(trans, root, ret); |
ce598979 MF |
498 | goto fail; |
499 | } | |
500 | ||
f46b5a66 CH |
501 | /* |
502 | * insert the directory item | |
503 | */ | |
3de4586c | 504 | ret = btrfs_set_inode_index(dir, &index); |
79787eaa JM |
505 | if (ret) { |
506 | btrfs_abort_transaction(trans, root, ret); | |
507 | goto fail; | |
508 | } | |
3de4586c CM |
509 | |
510 | ret = btrfs_insert_dir_item(trans, root, | |
16cdcec7 | 511 | name, namelen, dir, &key, |
3de4586c | 512 | BTRFS_FT_DIR, index); |
79787eaa JM |
513 | if (ret) { |
514 | btrfs_abort_transaction(trans, root, ret); | |
f46b5a66 | 515 | goto fail; |
79787eaa | 516 | } |
0660b5af | 517 | |
52c26179 YZ |
518 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
519 | ret = btrfs_update_inode(trans, root, dir); | |
520 | BUG_ON(ret); | |
521 | ||
0660b5af | 522 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
4df27c4d | 523 | objectid, root->root_key.objectid, |
33345d01 | 524 | btrfs_ino(dir), index, name, namelen); |
0660b5af | 525 | |
76dda93c | 526 | BUG_ON(ret); |
f46b5a66 | 527 | |
f46b5a66 | 528 | fail: |
d5c12070 MX |
529 | trans->block_rsv = NULL; |
530 | trans->bytes_reserved = 0; | |
72fd032e SW |
531 | if (async_transid) { |
532 | *async_transid = trans->transid; | |
533 | err = btrfs_commit_transaction_async(trans, root, 1); | |
00d71c9c MX |
534 | if (err) |
535 | err = btrfs_commit_transaction(trans, root); | |
72fd032e SW |
536 | } else { |
537 | err = btrfs_commit_transaction(trans, root); | |
538 | } | |
f46b5a66 CH |
539 | if (err && !ret) |
540 | ret = err; | |
1a65e24b CM |
541 | |
542 | if (!ret) | |
543 | d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); | |
d5c12070 MX |
544 | out: |
545 | btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); | |
f46b5a66 CH |
546 | return ret; |
547 | } | |
548 | ||
e9662f70 MX |
549 | static int create_snapshot(struct btrfs_root *root, struct inode *dir, |
550 | struct dentry *dentry, char *name, int namelen, | |
551 | u64 *async_transid, bool readonly, | |
552 | struct btrfs_qgroup_inherit *inherit) | |
f46b5a66 | 553 | { |
2e4bfab9 | 554 | struct inode *inode; |
f46b5a66 CH |
555 | struct btrfs_pending_snapshot *pending_snapshot; |
556 | struct btrfs_trans_handle *trans; | |
2e4bfab9 | 557 | int ret; |
f46b5a66 CH |
558 | |
559 | if (!root->ref_cows) | |
560 | return -EINVAL; | |
561 | ||
6a03843d MX |
562 | ret = btrfs_start_delalloc_inodes(root, 0); |
563 | if (ret) | |
564 | return ret; | |
565 | ||
566 | btrfs_wait_ordered_extents(root, 0); | |
567 | ||
3de4586c | 568 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
a22285a6 YZ |
569 | if (!pending_snapshot) |
570 | return -ENOMEM; | |
571 | ||
66d8f3dd MX |
572 | btrfs_init_block_rsv(&pending_snapshot->block_rsv, |
573 | BTRFS_BLOCK_RSV_TEMP); | |
d5c12070 MX |
574 | /* |
575 | * 1 - parent dir inode | |
576 | * 2 - dir entries | |
577 | * 1 - root item | |
578 | * 2 - root ref/backref | |
579 | * 1 - root of snapshot | |
580 | */ | |
581 | ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, | |
582 | &pending_snapshot->block_rsv, 7, | |
ee3441b4 JM |
583 | &pending_snapshot->qgroup_reserved, |
584 | false); | |
d5c12070 MX |
585 | if (ret) |
586 | goto out; | |
587 | ||
3de4586c | 588 | pending_snapshot->dentry = dentry; |
f46b5a66 | 589 | pending_snapshot->root = root; |
b83cc969 | 590 | pending_snapshot->readonly = readonly; |
e9662f70 | 591 | pending_snapshot->dir = dir; |
8696c533 | 592 | pending_snapshot->inherit = inherit; |
a22285a6 | 593 | |
d5c12070 | 594 | trans = btrfs_start_transaction(root, 0); |
a22285a6 YZ |
595 | if (IS_ERR(trans)) { |
596 | ret = PTR_ERR(trans); | |
597 | goto fail; | |
598 | } | |
599 | ||
8351583e | 600 | spin_lock(&root->fs_info->trans_lock); |
f46b5a66 CH |
601 | list_add(&pending_snapshot->list, |
602 | &trans->transaction->pending_snapshots); | |
8351583e | 603 | spin_unlock(&root->fs_info->trans_lock); |
72fd032e SW |
604 | if (async_transid) { |
605 | *async_transid = trans->transid; | |
606 | ret = btrfs_commit_transaction_async(trans, | |
607 | root->fs_info->extent_root, 1); | |
00d71c9c MX |
608 | if (ret) |
609 | ret = btrfs_commit_transaction(trans, root); | |
72fd032e SW |
610 | } else { |
611 | ret = btrfs_commit_transaction(trans, | |
612 | root->fs_info->extent_root); | |
613 | } | |
aec8030a | 614 | if (ret) |
c37b2b62 | 615 | goto fail; |
a22285a6 YZ |
616 | |
617 | ret = pending_snapshot->error; | |
618 | if (ret) | |
619 | goto fail; | |
620 | ||
66b4ffd1 JB |
621 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
622 | if (ret) | |
623 | goto fail; | |
f46b5a66 | 624 | |
2fbe8c8a | 625 | inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); |
2e4bfab9 YZ |
626 | if (IS_ERR(inode)) { |
627 | ret = PTR_ERR(inode); | |
628 | goto fail; | |
629 | } | |
630 | BUG_ON(!inode); | |
631 | d_instantiate(dentry, inode); | |
632 | ret = 0; | |
633 | fail: | |
d5c12070 MX |
634 | btrfs_subvolume_release_metadata(BTRFS_I(dir)->root, |
635 | &pending_snapshot->block_rsv, | |
636 | pending_snapshot->qgroup_reserved); | |
637 | out: | |
a22285a6 | 638 | kfree(pending_snapshot); |
f46b5a66 CH |
639 | return ret; |
640 | } | |
641 | ||
4260f7c7 SW |
642 | /* copy of check_sticky in fs/namei.c() |
643 | * It's inline, so penalty for filesystems that don't use sticky bit is | |
644 | * minimal. | |
645 | */ | |
646 | static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) | |
647 | { | |
2f2f43d3 | 648 | kuid_t fsuid = current_fsuid(); |
4260f7c7 SW |
649 | |
650 | if (!(dir->i_mode & S_ISVTX)) | |
651 | return 0; | |
2f2f43d3 | 652 | if (uid_eq(inode->i_uid, fsuid)) |
4260f7c7 | 653 | return 0; |
2f2f43d3 | 654 | if (uid_eq(dir->i_uid, fsuid)) |
4260f7c7 SW |
655 | return 0; |
656 | return !capable(CAP_FOWNER); | |
657 | } | |
658 | ||
659 | /* copy of may_delete in fs/namei.c() | |
660 | * Check whether we can remove a link victim from directory dir, check | |
661 | * whether the type of victim is right. | |
662 | * 1. We can't do it if dir is read-only (done in permission()) | |
663 | * 2. We should have write and exec permissions on dir | |
664 | * 3. We can't remove anything from append-only dir | |
665 | * 4. We can't do anything with immutable dir (done in permission()) | |
666 | * 5. If the sticky bit on dir is set we should either | |
667 | * a. be owner of dir, or | |
668 | * b. be owner of victim, or | |
669 | * c. have CAP_FOWNER capability | |
670 | * 6. If the victim is append-only or immutable we can't do antyhing with | |
671 | * links pointing to it. | |
672 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
673 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
674 | * 9. We can't remove a root or mountpoint. | |
675 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by | |
676 | * nfs_async_unlink(). | |
677 | */ | |
678 | ||
679 | static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) | |
680 | { | |
681 | int error; | |
682 | ||
683 | if (!victim->d_inode) | |
684 | return -ENOENT; | |
685 | ||
686 | BUG_ON(victim->d_parent->d_inode != dir); | |
4fa6b5ec | 687 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
4260f7c7 SW |
688 | |
689 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
690 | if (error) | |
691 | return error; | |
692 | if (IS_APPEND(dir)) | |
693 | return -EPERM; | |
694 | if (btrfs_check_sticky(dir, victim->d_inode)|| | |
695 | IS_APPEND(victim->d_inode)|| | |
696 | IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) | |
697 | return -EPERM; | |
698 | if (isdir) { | |
699 | if (!S_ISDIR(victim->d_inode->i_mode)) | |
700 | return -ENOTDIR; | |
701 | if (IS_ROOT(victim)) | |
702 | return -EBUSY; | |
703 | } else if (S_ISDIR(victim->d_inode->i_mode)) | |
704 | return -EISDIR; | |
705 | if (IS_DEADDIR(dir)) | |
706 | return -ENOENT; | |
707 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
708 | return -EBUSY; | |
709 | return 0; | |
710 | } | |
711 | ||
cb8e7090 CH |
712 | /* copy of may_create in fs/namei.c() */ |
713 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) | |
714 | { | |
715 | if (child->d_inode) | |
716 | return -EEXIST; | |
717 | if (IS_DEADDIR(dir)) | |
718 | return -ENOENT; | |
719 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
720 | } | |
721 | ||
722 | /* | |
723 | * Create a new subvolume below @parent. This is largely modeled after | |
724 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup | |
725 | * inside this filesystem so it's quite a bit simpler. | |
726 | */ | |
76dda93c YZ |
727 | static noinline int btrfs_mksubvol(struct path *parent, |
728 | char *name, int namelen, | |
72fd032e | 729 | struct btrfs_root *snap_src, |
6f72c7e2 | 730 | u64 *async_transid, bool readonly, |
8696c533 | 731 | struct btrfs_qgroup_inherit *inherit) |
cb8e7090 | 732 | { |
76dda93c | 733 | struct inode *dir = parent->dentry->d_inode; |
cb8e7090 CH |
734 | struct dentry *dentry; |
735 | int error; | |
736 | ||
5c50c9b8 DS |
737 | error = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); |
738 | if (error == -EINTR) | |
739 | return error; | |
cb8e7090 CH |
740 | |
741 | dentry = lookup_one_len(name, parent->dentry, namelen); | |
742 | error = PTR_ERR(dentry); | |
743 | if (IS_ERR(dentry)) | |
744 | goto out_unlock; | |
745 | ||
746 | error = -EEXIST; | |
747 | if (dentry->d_inode) | |
748 | goto out_dput; | |
749 | ||
76dda93c | 750 | error = btrfs_may_create(dir, dentry); |
cb8e7090 | 751 | if (error) |
a874a63e | 752 | goto out_dput; |
cb8e7090 | 753 | |
9c52057c CM |
754 | /* |
755 | * even if this name doesn't exist, we may get hash collisions. | |
756 | * check for them now when we can safely fail | |
757 | */ | |
758 | error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, | |
759 | dir->i_ino, name, | |
760 | namelen); | |
761 | if (error) | |
762 | goto out_dput; | |
763 | ||
76dda93c YZ |
764 | down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
765 | ||
766 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) | |
767 | goto out_up_read; | |
768 | ||
3de4586c | 769 | if (snap_src) { |
e9662f70 | 770 | error = create_snapshot(snap_src, dir, dentry, name, namelen, |
6f72c7e2 | 771 | async_transid, readonly, inherit); |
3de4586c | 772 | } else { |
d5c12070 MX |
773 | error = create_subvol(dir, dentry, name, namelen, |
774 | async_transid, inherit); | |
3de4586c | 775 | } |
76dda93c YZ |
776 | if (!error) |
777 | fsnotify_mkdir(dir, dentry); | |
778 | out_up_read: | |
779 | up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); | |
cb8e7090 CH |
780 | out_dput: |
781 | dput(dentry); | |
782 | out_unlock: | |
76dda93c | 783 | mutex_unlock(&dir->i_mutex); |
cb8e7090 CH |
784 | return error; |
785 | } | |
786 | ||
4cb5300b CM |
787 | /* |
788 | * When we're defragging a range, we don't want to kick it off again | |
789 | * if it is really just waiting for delalloc to send it down. | |
790 | * If we find a nice big extent or delalloc range for the bytes in the | |
791 | * file you want to defrag, we return 0 to let you know to skip this | |
792 | * part of the file | |
793 | */ | |
794 | static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) | |
795 | { | |
796 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
797 | struct extent_map *em = NULL; | |
798 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
799 | u64 end; | |
800 | ||
801 | read_lock(&em_tree->lock); | |
802 | em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); | |
803 | read_unlock(&em_tree->lock); | |
804 | ||
805 | if (em) { | |
806 | end = extent_map_end(em); | |
807 | free_extent_map(em); | |
808 | if (end - offset > thresh) | |
809 | return 0; | |
810 | } | |
811 | /* if we already have a nice delalloc here, just stop */ | |
812 | thresh /= 2; | |
813 | end = count_range_bits(io_tree, &offset, offset + thresh, | |
814 | thresh, EXTENT_DELALLOC, 1); | |
815 | if (end >= thresh) | |
816 | return 0; | |
817 | return 1; | |
818 | } | |
819 | ||
820 | /* | |
821 | * helper function to walk through a file and find extents | |
822 | * newer than a specific transid, and smaller than thresh. | |
823 | * | |
824 | * This is used by the defragging code to find new and small | |
825 | * extents | |
826 | */ | |
827 | static int find_new_extents(struct btrfs_root *root, | |
828 | struct inode *inode, u64 newer_than, | |
829 | u64 *off, int thresh) | |
830 | { | |
831 | struct btrfs_path *path; | |
832 | struct btrfs_key min_key; | |
833 | struct btrfs_key max_key; | |
834 | struct extent_buffer *leaf; | |
835 | struct btrfs_file_extent_item *extent; | |
836 | int type; | |
837 | int ret; | |
a4689d2b | 838 | u64 ino = btrfs_ino(inode); |
4cb5300b CM |
839 | |
840 | path = btrfs_alloc_path(); | |
841 | if (!path) | |
842 | return -ENOMEM; | |
843 | ||
a4689d2b | 844 | min_key.objectid = ino; |
4cb5300b CM |
845 | min_key.type = BTRFS_EXTENT_DATA_KEY; |
846 | min_key.offset = *off; | |
847 | ||
a4689d2b | 848 | max_key.objectid = ino; |
4cb5300b CM |
849 | max_key.type = (u8)-1; |
850 | max_key.offset = (u64)-1; | |
851 | ||
852 | path->keep_locks = 1; | |
853 | ||
854 | while(1) { | |
855 | ret = btrfs_search_forward(root, &min_key, &max_key, | |
de78b51a | 856 | path, newer_than); |
4cb5300b CM |
857 | if (ret != 0) |
858 | goto none; | |
a4689d2b | 859 | if (min_key.objectid != ino) |
4cb5300b CM |
860 | goto none; |
861 | if (min_key.type != BTRFS_EXTENT_DATA_KEY) | |
862 | goto none; | |
863 | ||
864 | leaf = path->nodes[0]; | |
865 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
866 | struct btrfs_file_extent_item); | |
867 | ||
868 | type = btrfs_file_extent_type(leaf, extent); | |
869 | if (type == BTRFS_FILE_EXTENT_REG && | |
870 | btrfs_file_extent_num_bytes(leaf, extent) < thresh && | |
871 | check_defrag_in_cache(inode, min_key.offset, thresh)) { | |
872 | *off = min_key.offset; | |
873 | btrfs_free_path(path); | |
874 | return 0; | |
875 | } | |
876 | ||
877 | if (min_key.offset == (u64)-1) | |
878 | goto none; | |
879 | ||
880 | min_key.offset++; | |
881 | btrfs_release_path(path); | |
882 | } | |
883 | none: | |
884 | btrfs_free_path(path); | |
885 | return -ENOENT; | |
886 | } | |
887 | ||
6c282eb4 | 888 | static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) |
17ce6ef8 LB |
889 | { |
890 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
6c282eb4 LZ |
891 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
892 | struct extent_map *em; | |
893 | u64 len = PAGE_CACHE_SIZE; | |
17ce6ef8 | 894 | |
6c282eb4 LZ |
895 | /* |
896 | * hopefully we have this extent in the tree already, try without | |
897 | * the full extent lock | |
898 | */ | |
17ce6ef8 | 899 | read_lock(&em_tree->lock); |
6c282eb4 | 900 | em = lookup_extent_mapping(em_tree, start, len); |
17ce6ef8 LB |
901 | read_unlock(&em_tree->lock); |
902 | ||
6c282eb4 LZ |
903 | if (!em) { |
904 | /* get the big lock and read metadata off disk */ | |
905 | lock_extent(io_tree, start, start + len - 1); | |
906 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); | |
907 | unlock_extent(io_tree, start, start + len - 1); | |
908 | ||
909 | if (IS_ERR(em)) | |
910 | return NULL; | |
911 | } | |
912 | ||
913 | return em; | |
914 | } | |
17ce6ef8 | 915 | |
6c282eb4 LZ |
916 | static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) |
917 | { | |
918 | struct extent_map *next; | |
919 | bool ret = true; | |
920 | ||
921 | /* this is the last extent */ | |
922 | if (em->start + em->len >= i_size_read(inode)) | |
923 | return false; | |
924 | ||
925 | next = defrag_lookup_extent(inode, em->start + em->len); | |
926 | if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) | |
927 | ret = false; | |
928 | ||
929 | free_extent_map(next); | |
17ce6ef8 LB |
930 | return ret; |
931 | } | |
932 | ||
6c282eb4 | 933 | static int should_defrag_range(struct inode *inode, u64 start, int thresh, |
a43a2111 AM |
934 | u64 *last_len, u64 *skip, u64 *defrag_end, |
935 | int compress) | |
940100a4 | 936 | { |
6c282eb4 | 937 | struct extent_map *em; |
940100a4 | 938 | int ret = 1; |
6c282eb4 | 939 | bool next_mergeable = true; |
940100a4 CM |
940 | |
941 | /* | |
008873ea | 942 | * make sure that once we start defragging an extent, we keep on |
940100a4 CM |
943 | * defragging it |
944 | */ | |
945 | if (start < *defrag_end) | |
946 | return 1; | |
947 | ||
948 | *skip = 0; | |
949 | ||
6c282eb4 LZ |
950 | em = defrag_lookup_extent(inode, start); |
951 | if (!em) | |
952 | return 0; | |
940100a4 CM |
953 | |
954 | /* this will cover holes, and inline extents */ | |
17ce6ef8 | 955 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
940100a4 | 956 | ret = 0; |
17ce6ef8 LB |
957 | goto out; |
958 | } | |
959 | ||
6c282eb4 | 960 | next_mergeable = defrag_check_next_extent(inode, em); |
940100a4 CM |
961 | |
962 | /* | |
6c282eb4 LZ |
963 | * we hit a real extent, if it is big or the next extent is not a |
964 | * real extent, don't bother defragging it | |
940100a4 | 965 | */ |
a43a2111 | 966 | if (!compress && (*last_len == 0 || *last_len >= thresh) && |
6c282eb4 | 967 | (em->len >= thresh || !next_mergeable)) |
940100a4 | 968 | ret = 0; |
17ce6ef8 | 969 | out: |
940100a4 CM |
970 | /* |
971 | * last_len ends up being a counter of how many bytes we've defragged. | |
972 | * every time we choose not to defrag an extent, we reset *last_len | |
973 | * so that the next tiny extent will force a defrag. | |
974 | * | |
975 | * The end result of this is that tiny extents before a single big | |
976 | * extent will force at least part of that big extent to be defragged. | |
977 | */ | |
978 | if (ret) { | |
940100a4 CM |
979 | *defrag_end = extent_map_end(em); |
980 | } else { | |
981 | *last_len = 0; | |
982 | *skip = extent_map_end(em); | |
983 | *defrag_end = 0; | |
984 | } | |
985 | ||
986 | free_extent_map(em); | |
987 | return ret; | |
988 | } | |
989 | ||
4cb5300b CM |
990 | /* |
991 | * it doesn't do much good to defrag one or two pages | |
992 | * at a time. This pulls in a nice chunk of pages | |
993 | * to COW and defrag. | |
994 | * | |
995 | * It also makes sure the delalloc code has enough | |
996 | * dirty data to avoid making new small extents as part | |
997 | * of the defrag | |
998 | * | |
999 | * It's a good idea to start RA on this range | |
1000 | * before calling this. | |
1001 | */ | |
1002 | static int cluster_pages_for_defrag(struct inode *inode, | |
1003 | struct page **pages, | |
1004 | unsigned long start_index, | |
1005 | int num_pages) | |
f46b5a66 | 1006 | { |
4cb5300b CM |
1007 | unsigned long file_end; |
1008 | u64 isize = i_size_read(inode); | |
1009 | u64 page_start; | |
1010 | u64 page_end; | |
1f12bd06 | 1011 | u64 page_cnt; |
4cb5300b CM |
1012 | int ret; |
1013 | int i; | |
1014 | int i_done; | |
3eaa2885 | 1015 | struct btrfs_ordered_extent *ordered; |
4cb5300b | 1016 | struct extent_state *cached_state = NULL; |
600a45e1 | 1017 | struct extent_io_tree *tree; |
3b16a4e3 | 1018 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
4cb5300b | 1019 | |
4cb5300b | 1020 | file_end = (isize - 1) >> PAGE_CACHE_SHIFT; |
1f12bd06 LB |
1021 | if (!isize || start_index > file_end) |
1022 | return 0; | |
1023 | ||
1024 | page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); | |
4cb5300b CM |
1025 | |
1026 | ret = btrfs_delalloc_reserve_space(inode, | |
1f12bd06 | 1027 | page_cnt << PAGE_CACHE_SHIFT); |
4cb5300b CM |
1028 | if (ret) |
1029 | return ret; | |
4cb5300b | 1030 | i_done = 0; |
600a45e1 | 1031 | tree = &BTRFS_I(inode)->io_tree; |
4cb5300b CM |
1032 | |
1033 | /* step one, lock all the pages */ | |
1f12bd06 | 1034 | for (i = 0; i < page_cnt; i++) { |
4cb5300b | 1035 | struct page *page; |
600a45e1 | 1036 | again: |
a94733d0 | 1037 | page = find_or_create_page(inode->i_mapping, |
600a45e1 | 1038 | start_index + i, mask); |
4cb5300b CM |
1039 | if (!page) |
1040 | break; | |
1041 | ||
600a45e1 MX |
1042 | page_start = page_offset(page); |
1043 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
1044 | while (1) { | |
d0082371 | 1045 | lock_extent(tree, page_start, page_end); |
600a45e1 MX |
1046 | ordered = btrfs_lookup_ordered_extent(inode, |
1047 | page_start); | |
d0082371 | 1048 | unlock_extent(tree, page_start, page_end); |
600a45e1 MX |
1049 | if (!ordered) |
1050 | break; | |
1051 | ||
1052 | unlock_page(page); | |
1053 | btrfs_start_ordered_extent(inode, ordered, 1); | |
1054 | btrfs_put_ordered_extent(ordered); | |
1055 | lock_page(page); | |
1f12bd06 LB |
1056 | /* |
1057 | * we unlocked the page above, so we need check if | |
1058 | * it was released or not. | |
1059 | */ | |
1060 | if (page->mapping != inode->i_mapping) { | |
1061 | unlock_page(page); | |
1062 | page_cache_release(page); | |
1063 | goto again; | |
1064 | } | |
600a45e1 MX |
1065 | } |
1066 | ||
4cb5300b CM |
1067 | if (!PageUptodate(page)) { |
1068 | btrfs_readpage(NULL, page); | |
1069 | lock_page(page); | |
1070 | if (!PageUptodate(page)) { | |
1071 | unlock_page(page); | |
1072 | page_cache_release(page); | |
1073 | ret = -EIO; | |
1074 | break; | |
1075 | } | |
1076 | } | |
600a45e1 | 1077 | |
600a45e1 MX |
1078 | if (page->mapping != inode->i_mapping) { |
1079 | unlock_page(page); | |
1080 | page_cache_release(page); | |
1081 | goto again; | |
1082 | } | |
1083 | ||
4cb5300b CM |
1084 | pages[i] = page; |
1085 | i_done++; | |
1086 | } | |
1087 | if (!i_done || ret) | |
1088 | goto out; | |
1089 | ||
1090 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) | |
1091 | goto out; | |
1092 | ||
1093 | /* | |
1094 | * so now we have a nice long stream of locked | |
1095 | * and up to date pages, lets wait on them | |
1096 | */ | |
1097 | for (i = 0; i < i_done; i++) | |
1098 | wait_on_page_writeback(pages[i]); | |
1099 | ||
1100 | page_start = page_offset(pages[0]); | |
1101 | page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; | |
1102 | ||
1103 | lock_extent_bits(&BTRFS_I(inode)->io_tree, | |
d0082371 | 1104 | page_start, page_end - 1, 0, &cached_state); |
4cb5300b CM |
1105 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, |
1106 | page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | | |
9e8a4a8b LB |
1107 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, |
1108 | &cached_state, GFP_NOFS); | |
4cb5300b | 1109 | |
1f12bd06 | 1110 | if (i_done != page_cnt) { |
9e0baf60 JB |
1111 | spin_lock(&BTRFS_I(inode)->lock); |
1112 | BTRFS_I(inode)->outstanding_extents++; | |
1113 | spin_unlock(&BTRFS_I(inode)->lock); | |
4cb5300b | 1114 | btrfs_delalloc_release_space(inode, |
1f12bd06 | 1115 | (page_cnt - i_done) << PAGE_CACHE_SHIFT); |
4cb5300b CM |
1116 | } |
1117 | ||
1118 | ||
9e8a4a8b LB |
1119 | set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, |
1120 | &cached_state, GFP_NOFS); | |
4cb5300b CM |
1121 | |
1122 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, | |
1123 | page_start, page_end - 1, &cached_state, | |
1124 | GFP_NOFS); | |
1125 | ||
1126 | for (i = 0; i < i_done; i++) { | |
1127 | clear_page_dirty_for_io(pages[i]); | |
1128 | ClearPageChecked(pages[i]); | |
1129 | set_page_extent_mapped(pages[i]); | |
1130 | set_page_dirty(pages[i]); | |
1131 | unlock_page(pages[i]); | |
1132 | page_cache_release(pages[i]); | |
1133 | } | |
1134 | return i_done; | |
1135 | out: | |
1136 | for (i = 0; i < i_done; i++) { | |
1137 | unlock_page(pages[i]); | |
1138 | page_cache_release(pages[i]); | |
1139 | } | |
1f12bd06 | 1140 | btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT); |
4cb5300b CM |
1141 | return ret; |
1142 | ||
1143 | } | |
1144 | ||
1145 | int btrfs_defrag_file(struct inode *inode, struct file *file, | |
1146 | struct btrfs_ioctl_defrag_range_args *range, | |
1147 | u64 newer_than, unsigned long max_to_defrag) | |
1148 | { | |
1149 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4cb5300b | 1150 | struct file_ra_state *ra = NULL; |
f46b5a66 | 1151 | unsigned long last_index; |
151a31b2 | 1152 | u64 isize = i_size_read(inode); |
940100a4 CM |
1153 | u64 last_len = 0; |
1154 | u64 skip = 0; | |
1155 | u64 defrag_end = 0; | |
4cb5300b | 1156 | u64 newer_off = range->start; |
f46b5a66 | 1157 | unsigned long i; |
008873ea | 1158 | unsigned long ra_index = 0; |
f46b5a66 | 1159 | int ret; |
4cb5300b | 1160 | int defrag_count = 0; |
1a419d85 | 1161 | int compress_type = BTRFS_COMPRESS_ZLIB; |
4cb5300b | 1162 | int extent_thresh = range->extent_thresh; |
008873ea LZ |
1163 | int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; |
1164 | int cluster = max_cluster; | |
4cb5300b CM |
1165 | u64 new_align = ~((u64)128 * 1024 - 1); |
1166 | struct page **pages = NULL; | |
1167 | ||
0abd5b17 LB |
1168 | if (isize == 0) |
1169 | return 0; | |
1170 | ||
1171 | if (range->start >= isize) | |
1172 | return -EINVAL; | |
1a419d85 LZ |
1173 | |
1174 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { | |
1175 | if (range->compress_type > BTRFS_COMPRESS_TYPES) | |
1176 | return -EINVAL; | |
1177 | if (range->compress_type) | |
1178 | compress_type = range->compress_type; | |
1179 | } | |
f46b5a66 | 1180 | |
0abd5b17 LB |
1181 | if (extent_thresh == 0) |
1182 | extent_thresh = 256 * 1024; | |
940100a4 | 1183 | |
4cb5300b CM |
1184 | /* |
1185 | * if we were not given a file, allocate a readahead | |
1186 | * context | |
1187 | */ | |
1188 | if (!file) { | |
1189 | ra = kzalloc(sizeof(*ra), GFP_NOFS); | |
1190 | if (!ra) | |
1191 | return -ENOMEM; | |
1192 | file_ra_state_init(ra, inode->i_mapping); | |
1193 | } else { | |
1194 | ra = &file->f_ra; | |
1195 | } | |
1196 | ||
008873ea | 1197 | pages = kmalloc(sizeof(struct page *) * max_cluster, |
4cb5300b CM |
1198 | GFP_NOFS); |
1199 | if (!pages) { | |
1200 | ret = -ENOMEM; | |
1201 | goto out_ra; | |
1202 | } | |
1203 | ||
1204 | /* find the last page to defrag */ | |
1e701a32 | 1205 | if (range->start + range->len > range->start) { |
151a31b2 | 1206 | last_index = min_t(u64, isize - 1, |
1e701a32 CM |
1207 | range->start + range->len - 1) >> PAGE_CACHE_SHIFT; |
1208 | } else { | |
151a31b2 | 1209 | last_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
1e701a32 CM |
1210 | } |
1211 | ||
4cb5300b CM |
1212 | if (newer_than) { |
1213 | ret = find_new_extents(root, inode, newer_than, | |
1214 | &newer_off, 64 * 1024); | |
1215 | if (!ret) { | |
1216 | range->start = newer_off; | |
1217 | /* | |
1218 | * we always align our defrag to help keep | |
1219 | * the extents in the file evenly spaced | |
1220 | */ | |
1221 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; | |
4cb5300b CM |
1222 | } else |
1223 | goto out_ra; | |
1224 | } else { | |
1225 | i = range->start >> PAGE_CACHE_SHIFT; | |
1226 | } | |
1227 | if (!max_to_defrag) | |
7ec31b54 | 1228 | max_to_defrag = last_index + 1; |
4cb5300b | 1229 | |
2a0f7f57 LZ |
1230 | /* |
1231 | * make writeback starts from i, so the defrag range can be | |
1232 | * written sequentially. | |
1233 | */ | |
1234 | if (i < inode->i_mapping->writeback_index) | |
1235 | inode->i_mapping->writeback_index = i; | |
1236 | ||
f7f43cc8 CM |
1237 | while (i <= last_index && defrag_count < max_to_defrag && |
1238 | (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> | |
1239 | PAGE_CACHE_SHIFT)) { | |
4cb5300b CM |
1240 | /* |
1241 | * make sure we stop running if someone unmounts | |
1242 | * the FS | |
1243 | */ | |
1244 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) | |
1245 | break; | |
1246 | ||
210549eb DS |
1247 | if (btrfs_defrag_cancelled(root->fs_info)) { |
1248 | printk(KERN_DEBUG "btrfs: defrag_file cancelled\n"); | |
1249 | ret = -EAGAIN; | |
1250 | break; | |
1251 | } | |
1252 | ||
66c26892 | 1253 | if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, |
6c282eb4 | 1254 | extent_thresh, &last_len, &skip, |
a43a2111 AM |
1255 | &defrag_end, range->flags & |
1256 | BTRFS_DEFRAG_RANGE_COMPRESS)) { | |
940100a4 CM |
1257 | unsigned long next; |
1258 | /* | |
1259 | * the should_defrag function tells us how much to skip | |
1260 | * bump our counter by the suggested amount | |
1261 | */ | |
1262 | next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
1263 | i = max(i + 1, next); | |
1264 | continue; | |
1265 | } | |
008873ea LZ |
1266 | |
1267 | if (!newer_than) { | |
1268 | cluster = (PAGE_CACHE_ALIGN(defrag_end) >> | |
1269 | PAGE_CACHE_SHIFT) - i; | |
1270 | cluster = min(cluster, max_cluster); | |
1271 | } else { | |
1272 | cluster = max_cluster; | |
1273 | } | |
1274 | ||
1e701a32 | 1275 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) |
1a419d85 | 1276 | BTRFS_I(inode)->force_compress = compress_type; |
940100a4 | 1277 | |
008873ea LZ |
1278 | if (i + cluster > ra_index) { |
1279 | ra_index = max(i, ra_index); | |
1280 | btrfs_force_ra(inode->i_mapping, ra, file, ra_index, | |
1281 | cluster); | |
1282 | ra_index += max_cluster; | |
1283 | } | |
940100a4 | 1284 | |
ecb8bea8 | 1285 | mutex_lock(&inode->i_mutex); |
008873ea | 1286 | ret = cluster_pages_for_defrag(inode, pages, i, cluster); |
ecb8bea8 LB |
1287 | if (ret < 0) { |
1288 | mutex_unlock(&inode->i_mutex); | |
4cb5300b | 1289 | goto out_ra; |
ecb8bea8 | 1290 | } |
4cb5300b CM |
1291 | |
1292 | defrag_count += ret; | |
d0e1d66b | 1293 | balance_dirty_pages_ratelimited(inode->i_mapping); |
ecb8bea8 | 1294 | mutex_unlock(&inode->i_mutex); |
4cb5300b CM |
1295 | |
1296 | if (newer_than) { | |
1297 | if (newer_off == (u64)-1) | |
1298 | break; | |
1299 | ||
e1f041e1 LB |
1300 | if (ret > 0) |
1301 | i += ret; | |
1302 | ||
4cb5300b CM |
1303 | newer_off = max(newer_off + 1, |
1304 | (u64)i << PAGE_CACHE_SHIFT); | |
1305 | ||
1306 | ret = find_new_extents(root, inode, | |
1307 | newer_than, &newer_off, | |
1308 | 64 * 1024); | |
1309 | if (!ret) { | |
1310 | range->start = newer_off; | |
1311 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; | |
4cb5300b CM |
1312 | } else { |
1313 | break; | |
f46b5a66 | 1314 | } |
4cb5300b | 1315 | } else { |
008873ea | 1316 | if (ret > 0) { |
cbcc8326 | 1317 | i += ret; |
008873ea LZ |
1318 | last_len += ret << PAGE_CACHE_SHIFT; |
1319 | } else { | |
cbcc8326 | 1320 | i++; |
008873ea LZ |
1321 | last_len = 0; |
1322 | } | |
f46b5a66 | 1323 | } |
f46b5a66 CH |
1324 | } |
1325 | ||
1e701a32 CM |
1326 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) |
1327 | filemap_flush(inode->i_mapping); | |
1328 | ||
1329 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { | |
1330 | /* the filemap_flush will queue IO into the worker threads, but | |
1331 | * we have to make sure the IO is actually started and that | |
1332 | * ordered extents get created before we return | |
1333 | */ | |
1334 | atomic_inc(&root->fs_info->async_submit_draining); | |
1335 | while (atomic_read(&root->fs_info->nr_async_submits) || | |
1336 | atomic_read(&root->fs_info->async_delalloc_pages)) { | |
1337 | wait_event(root->fs_info->async_submit_wait, | |
1338 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && | |
1339 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); | |
1340 | } | |
1341 | atomic_dec(&root->fs_info->async_submit_draining); | |
1342 | ||
1343 | mutex_lock(&inode->i_mutex); | |
261507a0 | 1344 | BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; |
1e701a32 CM |
1345 | mutex_unlock(&inode->i_mutex); |
1346 | } | |
1347 | ||
1a419d85 | 1348 | if (range->compress_type == BTRFS_COMPRESS_LZO) { |
2b0ce2c2 | 1349 | btrfs_set_fs_incompat(root->fs_info, COMPRESS_LZO); |
1a419d85 LZ |
1350 | } |
1351 | ||
60ccf82f | 1352 | ret = defrag_count; |
940100a4 | 1353 | |
4cb5300b CM |
1354 | out_ra: |
1355 | if (!file) | |
1356 | kfree(ra); | |
1357 | kfree(pages); | |
940100a4 | 1358 | return ret; |
f46b5a66 CH |
1359 | } |
1360 | ||
198605a8 | 1361 | static noinline int btrfs_ioctl_resize(struct file *file, |
76dda93c | 1362 | void __user *arg) |
f46b5a66 CH |
1363 | { |
1364 | u64 new_size; | |
1365 | u64 old_size; | |
1366 | u64 devid = 1; | |
496ad9aa | 1367 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
f46b5a66 CH |
1368 | struct btrfs_ioctl_vol_args *vol_args; |
1369 | struct btrfs_trans_handle *trans; | |
1370 | struct btrfs_device *device = NULL; | |
1371 | char *sizestr; | |
1372 | char *devstr = NULL; | |
1373 | int ret = 0; | |
f46b5a66 CH |
1374 | int mod = 0; |
1375 | ||
e441d54d CM |
1376 | if (!capable(CAP_SYS_ADMIN)) |
1377 | return -EPERM; | |
1378 | ||
198605a8 MX |
1379 | ret = mnt_want_write_file(file); |
1380 | if (ret) | |
1381 | return ret; | |
1382 | ||
5ac00add SB |
1383 | if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, |
1384 | 1)) { | |
1385 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); | |
97547676 | 1386 | mnt_drop_write_file(file); |
2c0c9da0 | 1387 | return -EINVAL; |
c9e9f97b ID |
1388 | } |
1389 | ||
5ac00add | 1390 | mutex_lock(&root->fs_info->volume_mutex); |
dae7b665 | 1391 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
1392 | if (IS_ERR(vol_args)) { |
1393 | ret = PTR_ERR(vol_args); | |
1394 | goto out; | |
1395 | } | |
5516e595 MF |
1396 | |
1397 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
f46b5a66 | 1398 | |
f46b5a66 CH |
1399 | sizestr = vol_args->name; |
1400 | devstr = strchr(sizestr, ':'); | |
1401 | if (devstr) { | |
1402 | char *end; | |
1403 | sizestr = devstr + 1; | |
1404 | *devstr = '\0'; | |
1405 | devstr = vol_args->name; | |
1406 | devid = simple_strtoull(devstr, &end, 10); | |
dfd79829 MX |
1407 | if (!devid) { |
1408 | ret = -EINVAL; | |
1409 | goto out_free; | |
1410 | } | |
5bb14682 | 1411 | printk(KERN_INFO "btrfs: resizing devid %llu\n", |
21380931 | 1412 | (unsigned long long)devid); |
f46b5a66 | 1413 | } |
dba60f3f | 1414 | |
aa1b8cd4 | 1415 | device = btrfs_find_device(root->fs_info, devid, NULL, NULL); |
f46b5a66 | 1416 | if (!device) { |
5bb14682 | 1417 | printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", |
21380931 | 1418 | (unsigned long long)devid); |
dfd79829 | 1419 | ret = -ENODEV; |
c9e9f97b | 1420 | goto out_free; |
f46b5a66 | 1421 | } |
dba60f3f MX |
1422 | |
1423 | if (!device->writeable) { | |
4e42ae1b | 1424 | printk(KERN_INFO "btrfs: resizer unable to apply on " |
dba60f3f | 1425 | "readonly device %llu\n", |
a8c4a33b | 1426 | (unsigned long long)devid); |
dfd79829 | 1427 | ret = -EPERM; |
4e42ae1b LB |
1428 | goto out_free; |
1429 | } | |
1430 | ||
f46b5a66 CH |
1431 | if (!strcmp(sizestr, "max")) |
1432 | new_size = device->bdev->bd_inode->i_size; | |
1433 | else { | |
1434 | if (sizestr[0] == '-') { | |
1435 | mod = -1; | |
1436 | sizestr++; | |
1437 | } else if (sizestr[0] == '+') { | |
1438 | mod = 1; | |
1439 | sizestr++; | |
1440 | } | |
91748467 | 1441 | new_size = memparse(sizestr, NULL); |
f46b5a66 CH |
1442 | if (new_size == 0) { |
1443 | ret = -EINVAL; | |
c9e9f97b | 1444 | goto out_free; |
f46b5a66 CH |
1445 | } |
1446 | } | |
1447 | ||
63a212ab | 1448 | if (device->is_tgtdev_for_dev_replace) { |
dfd79829 | 1449 | ret = -EPERM; |
63a212ab SB |
1450 | goto out_free; |
1451 | } | |
1452 | ||
f46b5a66 CH |
1453 | old_size = device->total_bytes; |
1454 | ||
1455 | if (mod < 0) { | |
1456 | if (new_size > old_size) { | |
1457 | ret = -EINVAL; | |
c9e9f97b | 1458 | goto out_free; |
f46b5a66 CH |
1459 | } |
1460 | new_size = old_size - new_size; | |
1461 | } else if (mod > 0) { | |
1462 | new_size = old_size + new_size; | |
1463 | } | |
1464 | ||
1465 | if (new_size < 256 * 1024 * 1024) { | |
1466 | ret = -EINVAL; | |
c9e9f97b | 1467 | goto out_free; |
f46b5a66 CH |
1468 | } |
1469 | if (new_size > device->bdev->bd_inode->i_size) { | |
1470 | ret = -EFBIG; | |
c9e9f97b | 1471 | goto out_free; |
f46b5a66 CH |
1472 | } |
1473 | ||
1474 | do_div(new_size, root->sectorsize); | |
1475 | new_size *= root->sectorsize; | |
1476 | ||
606686ee JB |
1477 | printk_in_rcu(KERN_INFO "btrfs: new size for %s is %llu\n", |
1478 | rcu_str_deref(device->name), | |
1479 | (unsigned long long)new_size); | |
f46b5a66 CH |
1480 | |
1481 | if (new_size > old_size) { | |
a22285a6 | 1482 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
1483 | if (IS_ERR(trans)) { |
1484 | ret = PTR_ERR(trans); | |
c9e9f97b | 1485 | goto out_free; |
98d5dc13 | 1486 | } |
f46b5a66 CH |
1487 | ret = btrfs_grow_device(trans, device, new_size); |
1488 | btrfs_commit_transaction(trans, root); | |
ece7d20e | 1489 | } else if (new_size < old_size) { |
f46b5a66 | 1490 | ret = btrfs_shrink_device(device, new_size); |
0253f40e | 1491 | } /* equal, nothing need to do */ |
f46b5a66 | 1492 | |
c9e9f97b | 1493 | out_free: |
f46b5a66 | 1494 | kfree(vol_args); |
c9e9f97b ID |
1495 | out: |
1496 | mutex_unlock(&root->fs_info->volume_mutex); | |
5ac00add | 1497 | atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); |
18f39c41 | 1498 | mnt_drop_write_file(file); |
f46b5a66 CH |
1499 | return ret; |
1500 | } | |
1501 | ||
72fd032e | 1502 | static noinline int btrfs_ioctl_snap_create_transid(struct file *file, |
6f72c7e2 AJ |
1503 | char *name, unsigned long fd, int subvol, |
1504 | u64 *transid, bool readonly, | |
8696c533 | 1505 | struct btrfs_qgroup_inherit *inherit) |
f46b5a66 | 1506 | { |
f46b5a66 | 1507 | int namelen; |
3de4586c | 1508 | int ret = 0; |
f46b5a66 | 1509 | |
a874a63e LB |
1510 | ret = mnt_want_write_file(file); |
1511 | if (ret) | |
1512 | goto out; | |
1513 | ||
72fd032e SW |
1514 | namelen = strlen(name); |
1515 | if (strchr(name, '/')) { | |
f46b5a66 | 1516 | ret = -EINVAL; |
a874a63e | 1517 | goto out_drop_write; |
f46b5a66 CH |
1518 | } |
1519 | ||
16780cab CM |
1520 | if (name[0] == '.' && |
1521 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { | |
1522 | ret = -EEXIST; | |
a874a63e | 1523 | goto out_drop_write; |
16780cab CM |
1524 | } |
1525 | ||
3de4586c | 1526 | if (subvol) { |
72fd032e | 1527 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
6f72c7e2 | 1528 | NULL, transid, readonly, inherit); |
cb8e7090 | 1529 | } else { |
2903ff01 | 1530 | struct fd src = fdget(fd); |
3de4586c | 1531 | struct inode *src_inode; |
2903ff01 | 1532 | if (!src.file) { |
3de4586c | 1533 | ret = -EINVAL; |
a874a63e | 1534 | goto out_drop_write; |
3de4586c CM |
1535 | } |
1536 | ||
496ad9aa AV |
1537 | src_inode = file_inode(src.file); |
1538 | if (src_inode->i_sb != file_inode(file)->i_sb) { | |
d397712b CM |
1539 | printk(KERN_INFO "btrfs: Snapshot src from " |
1540 | "another FS\n"); | |
3de4586c | 1541 | ret = -EINVAL; |
ecd18815 AV |
1542 | } else { |
1543 | ret = btrfs_mksubvol(&file->f_path, name, namelen, | |
1544 | BTRFS_I(src_inode)->root, | |
1545 | transid, readonly, inherit); | |
3de4586c | 1546 | } |
2903ff01 | 1547 | fdput(src); |
cb8e7090 | 1548 | } |
a874a63e LB |
1549 | out_drop_write: |
1550 | mnt_drop_write_file(file); | |
f46b5a66 | 1551 | out: |
72fd032e SW |
1552 | return ret; |
1553 | } | |
1554 | ||
1555 | static noinline int btrfs_ioctl_snap_create(struct file *file, | |
fa0d2b9b | 1556 | void __user *arg, int subvol) |
72fd032e | 1557 | { |
fa0d2b9b | 1558 | struct btrfs_ioctl_vol_args *vol_args; |
72fd032e SW |
1559 | int ret; |
1560 | ||
fa0d2b9b LZ |
1561 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1562 | if (IS_ERR(vol_args)) | |
1563 | return PTR_ERR(vol_args); | |
1564 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
72fd032e | 1565 | |
fa0d2b9b | 1566 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
b83cc969 | 1567 | vol_args->fd, subvol, |
6f72c7e2 | 1568 | NULL, false, NULL); |
fdfb1e4f | 1569 | |
fa0d2b9b LZ |
1570 | kfree(vol_args); |
1571 | return ret; | |
1572 | } | |
fdfb1e4f | 1573 | |
fa0d2b9b LZ |
1574 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
1575 | void __user *arg, int subvol) | |
1576 | { | |
1577 | struct btrfs_ioctl_vol_args_v2 *vol_args; | |
1578 | int ret; | |
1579 | u64 transid = 0; | |
1580 | u64 *ptr = NULL; | |
b83cc969 | 1581 | bool readonly = false; |
6f72c7e2 | 1582 | struct btrfs_qgroup_inherit *inherit = NULL; |
75eaa0e2 | 1583 | |
fa0d2b9b LZ |
1584 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1585 | if (IS_ERR(vol_args)) | |
1586 | return PTR_ERR(vol_args); | |
1587 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; | |
75eaa0e2 | 1588 | |
b83cc969 | 1589 | if (vol_args->flags & |
6f72c7e2 AJ |
1590 | ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | |
1591 | BTRFS_SUBVOL_QGROUP_INHERIT)) { | |
b83cc969 | 1592 | ret = -EOPNOTSUPP; |
fa0d2b9b | 1593 | goto out; |
72fd032e | 1594 | } |
fa0d2b9b LZ |
1595 | |
1596 | if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) | |
1597 | ptr = &transid; | |
b83cc969 LZ |
1598 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
1599 | readonly = true; | |
6f72c7e2 AJ |
1600 | if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { |
1601 | if (vol_args->size > PAGE_CACHE_SIZE) { | |
1602 | ret = -EINVAL; | |
1603 | goto out; | |
1604 | } | |
1605 | inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); | |
1606 | if (IS_ERR(inherit)) { | |
1607 | ret = PTR_ERR(inherit); | |
1608 | goto out; | |
1609 | } | |
1610 | } | |
fa0d2b9b LZ |
1611 | |
1612 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, | |
6f72c7e2 | 1613 | vol_args->fd, subvol, ptr, |
8696c533 | 1614 | readonly, inherit); |
fa0d2b9b LZ |
1615 | |
1616 | if (ret == 0 && ptr && | |
1617 | copy_to_user(arg + | |
1618 | offsetof(struct btrfs_ioctl_vol_args_v2, | |
1619 | transid), ptr, sizeof(*ptr))) | |
1620 | ret = -EFAULT; | |
fdfb1e4f | 1621 | out: |
f46b5a66 | 1622 | kfree(vol_args); |
6f72c7e2 | 1623 | kfree(inherit); |
f46b5a66 CH |
1624 | return ret; |
1625 | } | |
1626 | ||
0caa102d LZ |
1627 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
1628 | void __user *arg) | |
1629 | { | |
496ad9aa | 1630 | struct inode *inode = file_inode(file); |
0caa102d LZ |
1631 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1632 | int ret = 0; | |
1633 | u64 flags = 0; | |
1634 | ||
33345d01 | 1635 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) |
0caa102d LZ |
1636 | return -EINVAL; |
1637 | ||
1638 | down_read(&root->fs_info->subvol_sem); | |
1639 | if (btrfs_root_readonly(root)) | |
1640 | flags |= BTRFS_SUBVOL_RDONLY; | |
1641 | up_read(&root->fs_info->subvol_sem); | |
1642 | ||
1643 | if (copy_to_user(arg, &flags, sizeof(flags))) | |
1644 | ret = -EFAULT; | |
1645 | ||
1646 | return ret; | |
1647 | } | |
1648 | ||
1649 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, | |
1650 | void __user *arg) | |
1651 | { | |
496ad9aa | 1652 | struct inode *inode = file_inode(file); |
0caa102d LZ |
1653 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1654 | struct btrfs_trans_handle *trans; | |
1655 | u64 root_flags; | |
1656 | u64 flags; | |
1657 | int ret = 0; | |
1658 | ||
b9ca0664 LB |
1659 | ret = mnt_want_write_file(file); |
1660 | if (ret) | |
1661 | goto out; | |
0caa102d | 1662 | |
b9ca0664 LB |
1663 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { |
1664 | ret = -EINVAL; | |
1665 | goto out_drop_write; | |
1666 | } | |
0caa102d | 1667 | |
b9ca0664 LB |
1668 | if (copy_from_user(&flags, arg, sizeof(flags))) { |
1669 | ret = -EFAULT; | |
1670 | goto out_drop_write; | |
1671 | } | |
0caa102d | 1672 | |
b9ca0664 LB |
1673 | if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { |
1674 | ret = -EINVAL; | |
1675 | goto out_drop_write; | |
1676 | } | |
0caa102d | 1677 | |
b9ca0664 LB |
1678 | if (flags & ~BTRFS_SUBVOL_RDONLY) { |
1679 | ret = -EOPNOTSUPP; | |
1680 | goto out_drop_write; | |
1681 | } | |
0caa102d | 1682 | |
b9ca0664 LB |
1683 | if (!inode_owner_or_capable(inode)) { |
1684 | ret = -EACCES; | |
1685 | goto out_drop_write; | |
1686 | } | |
b4dc2b8c | 1687 | |
0caa102d LZ |
1688 | down_write(&root->fs_info->subvol_sem); |
1689 | ||
1690 | /* nothing to do */ | |
1691 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) | |
b9ca0664 | 1692 | goto out_drop_sem; |
0caa102d LZ |
1693 | |
1694 | root_flags = btrfs_root_flags(&root->root_item); | |
1695 | if (flags & BTRFS_SUBVOL_RDONLY) | |
1696 | btrfs_set_root_flags(&root->root_item, | |
1697 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); | |
1698 | else | |
1699 | btrfs_set_root_flags(&root->root_item, | |
1700 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); | |
1701 | ||
1702 | trans = btrfs_start_transaction(root, 1); | |
1703 | if (IS_ERR(trans)) { | |
1704 | ret = PTR_ERR(trans); | |
1705 | goto out_reset; | |
1706 | } | |
1707 | ||
b4dc2b8c | 1708 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
0caa102d LZ |
1709 | &root->root_key, &root->root_item); |
1710 | ||
1711 | btrfs_commit_transaction(trans, root); | |
1712 | out_reset: | |
1713 | if (ret) | |
1714 | btrfs_set_root_flags(&root->root_item, root_flags); | |
b9ca0664 | 1715 | out_drop_sem: |
0caa102d | 1716 | up_write(&root->fs_info->subvol_sem); |
b9ca0664 LB |
1717 | out_drop_write: |
1718 | mnt_drop_write_file(file); | |
1719 | out: | |
0caa102d LZ |
1720 | return ret; |
1721 | } | |
1722 | ||
76dda93c YZ |
1723 | /* |
1724 | * helper to check if the subvolume references other subvolumes | |
1725 | */ | |
1726 | static noinline int may_destroy_subvol(struct btrfs_root *root) | |
1727 | { | |
1728 | struct btrfs_path *path; | |
175a2b87 | 1729 | struct btrfs_dir_item *di; |
76dda93c | 1730 | struct btrfs_key key; |
175a2b87 | 1731 | u64 dir_id; |
76dda93c YZ |
1732 | int ret; |
1733 | ||
1734 | path = btrfs_alloc_path(); | |
1735 | if (!path) | |
1736 | return -ENOMEM; | |
1737 | ||
175a2b87 JB |
1738 | /* Make sure this root isn't set as the default subvol */ |
1739 | dir_id = btrfs_super_root_dir(root->fs_info->super_copy); | |
1740 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, path, | |
1741 | dir_id, "default", 7, 0); | |
1742 | if (di && !IS_ERR(di)) { | |
1743 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key); | |
1744 | if (key.objectid == root->root_key.objectid) { | |
1745 | ret = -ENOTEMPTY; | |
1746 | goto out; | |
1747 | } | |
1748 | btrfs_release_path(path); | |
1749 | } | |
1750 | ||
76dda93c YZ |
1751 | key.objectid = root->root_key.objectid; |
1752 | key.type = BTRFS_ROOT_REF_KEY; | |
1753 | key.offset = (u64)-1; | |
1754 | ||
1755 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, | |
1756 | &key, path, 0, 0); | |
1757 | if (ret < 0) | |
1758 | goto out; | |
1759 | BUG_ON(ret == 0); | |
1760 | ||
1761 | ret = 0; | |
1762 | if (path->slots[0] > 0) { | |
1763 | path->slots[0]--; | |
1764 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1765 | if (key.objectid == root->root_key.objectid && | |
1766 | key.type == BTRFS_ROOT_REF_KEY) | |
1767 | ret = -ENOTEMPTY; | |
1768 | } | |
1769 | out: | |
1770 | btrfs_free_path(path); | |
1771 | return ret; | |
1772 | } | |
1773 | ||
ac8e9819 CM |
1774 | static noinline int key_in_sk(struct btrfs_key *key, |
1775 | struct btrfs_ioctl_search_key *sk) | |
1776 | { | |
abc6e134 CM |
1777 | struct btrfs_key test; |
1778 | int ret; | |
1779 | ||
1780 | test.objectid = sk->min_objectid; | |
1781 | test.type = sk->min_type; | |
1782 | test.offset = sk->min_offset; | |
1783 | ||
1784 | ret = btrfs_comp_cpu_keys(key, &test); | |
1785 | if (ret < 0) | |
ac8e9819 | 1786 | return 0; |
abc6e134 CM |
1787 | |
1788 | test.objectid = sk->max_objectid; | |
1789 | test.type = sk->max_type; | |
1790 | test.offset = sk->max_offset; | |
1791 | ||
1792 | ret = btrfs_comp_cpu_keys(key, &test); | |
1793 | if (ret > 0) | |
ac8e9819 CM |
1794 | return 0; |
1795 | return 1; | |
1796 | } | |
1797 | ||
1798 | static noinline int copy_to_sk(struct btrfs_root *root, | |
1799 | struct btrfs_path *path, | |
1800 | struct btrfs_key *key, | |
1801 | struct btrfs_ioctl_search_key *sk, | |
1802 | char *buf, | |
1803 | unsigned long *sk_offset, | |
1804 | int *num_found) | |
1805 | { | |
1806 | u64 found_transid; | |
1807 | struct extent_buffer *leaf; | |
1808 | struct btrfs_ioctl_search_header sh; | |
1809 | unsigned long item_off; | |
1810 | unsigned long item_len; | |
1811 | int nritems; | |
1812 | int i; | |
1813 | int slot; | |
ac8e9819 CM |
1814 | int ret = 0; |
1815 | ||
1816 | leaf = path->nodes[0]; | |
1817 | slot = path->slots[0]; | |
1818 | nritems = btrfs_header_nritems(leaf); | |
1819 | ||
1820 | if (btrfs_header_generation(leaf) > sk->max_transid) { | |
1821 | i = nritems; | |
1822 | goto advance_key; | |
1823 | } | |
1824 | found_transid = btrfs_header_generation(leaf); | |
1825 | ||
1826 | for (i = slot; i < nritems; i++) { | |
1827 | item_off = btrfs_item_ptr_offset(leaf, i); | |
1828 | item_len = btrfs_item_size_nr(leaf, i); | |
1829 | ||
03b71c6c GP |
1830 | btrfs_item_key_to_cpu(leaf, key, i); |
1831 | if (!key_in_sk(key, sk)) | |
1832 | continue; | |
1833 | ||
1834 | if (sizeof(sh) + item_len > BTRFS_SEARCH_ARGS_BUFSIZE) | |
ac8e9819 CM |
1835 | item_len = 0; |
1836 | ||
1837 | if (sizeof(sh) + item_len + *sk_offset > | |
1838 | BTRFS_SEARCH_ARGS_BUFSIZE) { | |
1839 | ret = 1; | |
1840 | goto overflow; | |
1841 | } | |
1842 | ||
ac8e9819 CM |
1843 | sh.objectid = key->objectid; |
1844 | sh.offset = key->offset; | |
1845 | sh.type = key->type; | |
1846 | sh.len = item_len; | |
1847 | sh.transid = found_transid; | |
1848 | ||
1849 | /* copy search result header */ | |
1850 | memcpy(buf + *sk_offset, &sh, sizeof(sh)); | |
1851 | *sk_offset += sizeof(sh); | |
1852 | ||
1853 | if (item_len) { | |
1854 | char *p = buf + *sk_offset; | |
1855 | /* copy the item */ | |
1856 | read_extent_buffer(leaf, p, | |
1857 | item_off, item_len); | |
1858 | *sk_offset += item_len; | |
ac8e9819 | 1859 | } |
e2156867 | 1860 | (*num_found)++; |
ac8e9819 CM |
1861 | |
1862 | if (*num_found >= sk->nr_items) | |
1863 | break; | |
1864 | } | |
1865 | advance_key: | |
abc6e134 CM |
1866 | ret = 0; |
1867 | if (key->offset < (u64)-1 && key->offset < sk->max_offset) | |
ac8e9819 | 1868 | key->offset++; |
abc6e134 CM |
1869 | else if (key->type < (u8)-1 && key->type < sk->max_type) { |
1870 | key->offset = 0; | |
ac8e9819 | 1871 | key->type++; |
abc6e134 CM |
1872 | } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { |
1873 | key->offset = 0; | |
1874 | key->type = 0; | |
ac8e9819 | 1875 | key->objectid++; |
abc6e134 CM |
1876 | } else |
1877 | ret = 1; | |
ac8e9819 | 1878 | overflow: |
ac8e9819 CM |
1879 | return ret; |
1880 | } | |
1881 | ||
1882 | static noinline int search_ioctl(struct inode *inode, | |
1883 | struct btrfs_ioctl_search_args *args) | |
1884 | { | |
1885 | struct btrfs_root *root; | |
1886 | struct btrfs_key key; | |
1887 | struct btrfs_key max_key; | |
1888 | struct btrfs_path *path; | |
1889 | struct btrfs_ioctl_search_key *sk = &args->key; | |
1890 | struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; | |
1891 | int ret; | |
1892 | int num_found = 0; | |
1893 | unsigned long sk_offset = 0; | |
1894 | ||
1895 | path = btrfs_alloc_path(); | |
1896 | if (!path) | |
1897 | return -ENOMEM; | |
1898 | ||
1899 | if (sk->tree_id == 0) { | |
1900 | /* search the root of the inode that was passed */ | |
1901 | root = BTRFS_I(inode)->root; | |
1902 | } else { | |
1903 | key.objectid = sk->tree_id; | |
1904 | key.type = BTRFS_ROOT_ITEM_KEY; | |
1905 | key.offset = (u64)-1; | |
1906 | root = btrfs_read_fs_root_no_name(info, &key); | |
1907 | if (IS_ERR(root)) { | |
1908 | printk(KERN_ERR "could not find root %llu\n", | |
1909 | sk->tree_id); | |
1910 | btrfs_free_path(path); | |
1911 | return -ENOENT; | |
1912 | } | |
1913 | } | |
1914 | ||
1915 | key.objectid = sk->min_objectid; | |
1916 | key.type = sk->min_type; | |
1917 | key.offset = sk->min_offset; | |
1918 | ||
1919 | max_key.objectid = sk->max_objectid; | |
1920 | max_key.type = sk->max_type; | |
1921 | max_key.offset = sk->max_offset; | |
1922 | ||
1923 | path->keep_locks = 1; | |
1924 | ||
1925 | while(1) { | |
de78b51a | 1926 | ret = btrfs_search_forward(root, &key, &max_key, path, |
ac8e9819 CM |
1927 | sk->min_transid); |
1928 | if (ret != 0) { | |
1929 | if (ret > 0) | |
1930 | ret = 0; | |
1931 | goto err; | |
1932 | } | |
1933 | ret = copy_to_sk(root, path, &key, sk, args->buf, | |
1934 | &sk_offset, &num_found); | |
b3b4aa74 | 1935 | btrfs_release_path(path); |
ac8e9819 CM |
1936 | if (ret || num_found >= sk->nr_items) |
1937 | break; | |
1938 | ||
1939 | } | |
1940 | ret = 0; | |
1941 | err: | |
1942 | sk->nr_items = num_found; | |
1943 | btrfs_free_path(path); | |
1944 | return ret; | |
1945 | } | |
1946 | ||
1947 | static noinline int btrfs_ioctl_tree_search(struct file *file, | |
1948 | void __user *argp) | |
1949 | { | |
1950 | struct btrfs_ioctl_search_args *args; | |
1951 | struct inode *inode; | |
1952 | int ret; | |
1953 | ||
1954 | if (!capable(CAP_SYS_ADMIN)) | |
1955 | return -EPERM; | |
1956 | ||
2354d08f JL |
1957 | args = memdup_user(argp, sizeof(*args)); |
1958 | if (IS_ERR(args)) | |
1959 | return PTR_ERR(args); | |
ac8e9819 | 1960 | |
496ad9aa | 1961 | inode = file_inode(file); |
ac8e9819 CM |
1962 | ret = search_ioctl(inode, args); |
1963 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) | |
1964 | ret = -EFAULT; | |
1965 | kfree(args); | |
1966 | return ret; | |
1967 | } | |
1968 | ||
98d377a0 | 1969 | /* |
ac8e9819 CM |
1970 | * Search INODE_REFs to identify path name of 'dirid' directory |
1971 | * in a 'tree_id' tree. and sets path name to 'name'. | |
1972 | */ | |
98d377a0 TH |
1973 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
1974 | u64 tree_id, u64 dirid, char *name) | |
1975 | { | |
1976 | struct btrfs_root *root; | |
1977 | struct btrfs_key key; | |
ac8e9819 | 1978 | char *ptr; |
98d377a0 TH |
1979 | int ret = -1; |
1980 | int slot; | |
1981 | int len; | |
1982 | int total_len = 0; | |
1983 | struct btrfs_inode_ref *iref; | |
1984 | struct extent_buffer *l; | |
1985 | struct btrfs_path *path; | |
1986 | ||
1987 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { | |
1988 | name[0]='\0'; | |
1989 | return 0; | |
1990 | } | |
1991 | ||
1992 | path = btrfs_alloc_path(); | |
1993 | if (!path) | |
1994 | return -ENOMEM; | |
1995 | ||
ac8e9819 | 1996 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; |
98d377a0 TH |
1997 | |
1998 | key.objectid = tree_id; | |
1999 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2000 | key.offset = (u64)-1; | |
2001 | root = btrfs_read_fs_root_no_name(info, &key); | |
2002 | if (IS_ERR(root)) { | |
2003 | printk(KERN_ERR "could not find root %llu\n", tree_id); | |
8ad6fcab CM |
2004 | ret = -ENOENT; |
2005 | goto out; | |
98d377a0 TH |
2006 | } |
2007 | ||
2008 | key.objectid = dirid; | |
2009 | key.type = BTRFS_INODE_REF_KEY; | |
8ad6fcab | 2010 | key.offset = (u64)-1; |
98d377a0 TH |
2011 | |
2012 | while(1) { | |
2013 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2014 | if (ret < 0) | |
2015 | goto out; | |
2016 | ||
2017 | l = path->nodes[0]; | |
2018 | slot = path->slots[0]; | |
8ad6fcab CM |
2019 | if (ret > 0 && slot > 0) |
2020 | slot--; | |
98d377a0 TH |
2021 | btrfs_item_key_to_cpu(l, &key, slot); |
2022 | ||
2023 | if (ret > 0 && (key.objectid != dirid || | |
ac8e9819 CM |
2024 | key.type != BTRFS_INODE_REF_KEY)) { |
2025 | ret = -ENOENT; | |
98d377a0 | 2026 | goto out; |
ac8e9819 | 2027 | } |
98d377a0 TH |
2028 | |
2029 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); | |
2030 | len = btrfs_inode_ref_name_len(l, iref); | |
2031 | ptr -= len + 1; | |
2032 | total_len += len + 1; | |
a696cf35 FDBM |
2033 | if (ptr < name) { |
2034 | ret = -ENAMETOOLONG; | |
98d377a0 | 2035 | goto out; |
a696cf35 | 2036 | } |
98d377a0 TH |
2037 | |
2038 | *(ptr + len) = '/'; | |
2039 | read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); | |
2040 | ||
2041 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) | |
2042 | break; | |
2043 | ||
b3b4aa74 | 2044 | btrfs_release_path(path); |
98d377a0 | 2045 | key.objectid = key.offset; |
8ad6fcab | 2046 | key.offset = (u64)-1; |
98d377a0 | 2047 | dirid = key.objectid; |
98d377a0 | 2048 | } |
77906a50 | 2049 | memmove(name, ptr, total_len); |
98d377a0 TH |
2050 | name[total_len]='\0'; |
2051 | ret = 0; | |
2052 | out: | |
2053 | btrfs_free_path(path); | |
ac8e9819 CM |
2054 | return ret; |
2055 | } | |
2056 | ||
2057 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, | |
2058 | void __user *argp) | |
2059 | { | |
2060 | struct btrfs_ioctl_ino_lookup_args *args; | |
2061 | struct inode *inode; | |
2062 | int ret; | |
2063 | ||
2064 | if (!capable(CAP_SYS_ADMIN)) | |
2065 | return -EPERM; | |
2066 | ||
2354d08f JL |
2067 | args = memdup_user(argp, sizeof(*args)); |
2068 | if (IS_ERR(args)) | |
2069 | return PTR_ERR(args); | |
c2b96929 | 2070 | |
496ad9aa | 2071 | inode = file_inode(file); |
ac8e9819 | 2072 | |
1b53ac4d CM |
2073 | if (args->treeid == 0) |
2074 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; | |
2075 | ||
ac8e9819 CM |
2076 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
2077 | args->treeid, args->objectid, | |
2078 | args->name); | |
2079 | ||
2080 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) | |
2081 | ret = -EFAULT; | |
2082 | ||
2083 | kfree(args); | |
98d377a0 TH |
2084 | return ret; |
2085 | } | |
2086 | ||
76dda93c YZ |
2087 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
2088 | void __user *arg) | |
2089 | { | |
2090 | struct dentry *parent = fdentry(file); | |
2091 | struct dentry *dentry; | |
2092 | struct inode *dir = parent->d_inode; | |
2093 | struct inode *inode; | |
2094 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
2095 | struct btrfs_root *dest = NULL; | |
2096 | struct btrfs_ioctl_vol_args *vol_args; | |
2097 | struct btrfs_trans_handle *trans; | |
c58aaad2 MX |
2098 | struct btrfs_block_rsv block_rsv; |
2099 | u64 qgroup_reserved; | |
76dda93c YZ |
2100 | int namelen; |
2101 | int ret; | |
2102 | int err = 0; | |
2103 | ||
76dda93c YZ |
2104 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
2105 | if (IS_ERR(vol_args)) | |
2106 | return PTR_ERR(vol_args); | |
2107 | ||
2108 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
2109 | namelen = strlen(vol_args->name); | |
2110 | if (strchr(vol_args->name, '/') || | |
2111 | strncmp(vol_args->name, "..", namelen) == 0) { | |
2112 | err = -EINVAL; | |
2113 | goto out; | |
2114 | } | |
2115 | ||
a561be71 | 2116 | err = mnt_want_write_file(file); |
76dda93c YZ |
2117 | if (err) |
2118 | goto out; | |
2119 | ||
5c50c9b8 DS |
2120 | err = mutex_lock_killable_nested(&dir->i_mutex, I_MUTEX_PARENT); |
2121 | if (err == -EINTR) | |
2122 | goto out; | |
76dda93c YZ |
2123 | dentry = lookup_one_len(vol_args->name, parent, namelen); |
2124 | if (IS_ERR(dentry)) { | |
2125 | err = PTR_ERR(dentry); | |
2126 | goto out_unlock_dir; | |
2127 | } | |
2128 | ||
2129 | if (!dentry->d_inode) { | |
2130 | err = -ENOENT; | |
2131 | goto out_dput; | |
2132 | } | |
2133 | ||
2134 | inode = dentry->d_inode; | |
4260f7c7 SW |
2135 | dest = BTRFS_I(inode)->root; |
2136 | if (!capable(CAP_SYS_ADMIN)){ | |
2137 | /* | |
2138 | * Regular user. Only allow this with a special mount | |
2139 | * option, when the user has write+exec access to the | |
2140 | * subvol root, and when rmdir(2) would have been | |
2141 | * allowed. | |
2142 | * | |
2143 | * Note that this is _not_ check that the subvol is | |
2144 | * empty or doesn't contain data that we wouldn't | |
2145 | * otherwise be able to delete. | |
2146 | * | |
2147 | * Users who want to delete empty subvols should try | |
2148 | * rmdir(2). | |
2149 | */ | |
2150 | err = -EPERM; | |
2151 | if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) | |
2152 | goto out_dput; | |
2153 | ||
2154 | /* | |
2155 | * Do not allow deletion if the parent dir is the same | |
2156 | * as the dir to be deleted. That means the ioctl | |
2157 | * must be called on the dentry referencing the root | |
2158 | * of the subvol, not a random directory contained | |
2159 | * within it. | |
2160 | */ | |
2161 | err = -EINVAL; | |
2162 | if (root == dest) | |
2163 | goto out_dput; | |
2164 | ||
2165 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); | |
2166 | if (err) | |
2167 | goto out_dput; | |
4260f7c7 SW |
2168 | } |
2169 | ||
5c39da5b MX |
2170 | /* check if subvolume may be deleted by a user */ |
2171 | err = btrfs_may_delete(dir, dentry, 1); | |
2172 | if (err) | |
2173 | goto out_dput; | |
2174 | ||
33345d01 | 2175 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { |
76dda93c YZ |
2176 | err = -EINVAL; |
2177 | goto out_dput; | |
2178 | } | |
2179 | ||
76dda93c YZ |
2180 | mutex_lock(&inode->i_mutex); |
2181 | err = d_invalidate(dentry); | |
2182 | if (err) | |
2183 | goto out_unlock; | |
2184 | ||
2185 | down_write(&root->fs_info->subvol_sem); | |
2186 | ||
2187 | err = may_destroy_subvol(dest); | |
2188 | if (err) | |
2189 | goto out_up_write; | |
2190 | ||
c58aaad2 MX |
2191 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
2192 | /* | |
2193 | * One for dir inode, two for dir entries, two for root | |
2194 | * ref/backref. | |
2195 | */ | |
2196 | err = btrfs_subvolume_reserve_metadata(root, &block_rsv, | |
ee3441b4 | 2197 | 5, &qgroup_reserved, true); |
c58aaad2 MX |
2198 | if (err) |
2199 | goto out_up_write; | |
2200 | ||
a22285a6 YZ |
2201 | trans = btrfs_start_transaction(root, 0); |
2202 | if (IS_ERR(trans)) { | |
2203 | err = PTR_ERR(trans); | |
c58aaad2 | 2204 | goto out_release; |
a22285a6 | 2205 | } |
c58aaad2 MX |
2206 | trans->block_rsv = &block_rsv; |
2207 | trans->bytes_reserved = block_rsv.size; | |
a22285a6 | 2208 | |
76dda93c YZ |
2209 | ret = btrfs_unlink_subvol(trans, root, dir, |
2210 | dest->root_key.objectid, | |
2211 | dentry->d_name.name, | |
2212 | dentry->d_name.len); | |
79787eaa JM |
2213 | if (ret) { |
2214 | err = ret; | |
2215 | btrfs_abort_transaction(trans, root, ret); | |
2216 | goto out_end_trans; | |
2217 | } | |
76dda93c YZ |
2218 | |
2219 | btrfs_record_root_in_trans(trans, dest); | |
2220 | ||
2221 | memset(&dest->root_item.drop_progress, 0, | |
2222 | sizeof(dest->root_item.drop_progress)); | |
2223 | dest->root_item.drop_level = 0; | |
2224 | btrfs_set_root_refs(&dest->root_item, 0); | |
2225 | ||
d68fc57b YZ |
2226 | if (!xchg(&dest->orphan_item_inserted, 1)) { |
2227 | ret = btrfs_insert_orphan_item(trans, | |
2228 | root->fs_info->tree_root, | |
2229 | dest->root_key.objectid); | |
79787eaa JM |
2230 | if (ret) { |
2231 | btrfs_abort_transaction(trans, root, ret); | |
2232 | err = ret; | |
2233 | goto out_end_trans; | |
2234 | } | |
d68fc57b | 2235 | } |
79787eaa | 2236 | out_end_trans: |
c58aaad2 MX |
2237 | trans->block_rsv = NULL; |
2238 | trans->bytes_reserved = 0; | |
531cb13f | 2239 | ret = btrfs_end_transaction(trans, root); |
79787eaa JM |
2240 | if (ret && !err) |
2241 | err = ret; | |
76dda93c | 2242 | inode->i_flags |= S_DEAD; |
c58aaad2 MX |
2243 | out_release: |
2244 | btrfs_subvolume_release_metadata(root, &block_rsv, qgroup_reserved); | |
76dda93c YZ |
2245 | out_up_write: |
2246 | up_write(&root->fs_info->subvol_sem); | |
2247 | out_unlock: | |
2248 | mutex_unlock(&inode->i_mutex); | |
2249 | if (!err) { | |
efefb143 | 2250 | shrink_dcache_sb(root->fs_info->sb); |
76dda93c YZ |
2251 | btrfs_invalidate_inodes(dest); |
2252 | d_delete(dentry); | |
fa6ac876 LB |
2253 | |
2254 | /* the last ref */ | |
2255 | if (dest->cache_inode) { | |
2256 | iput(dest->cache_inode); | |
2257 | dest->cache_inode = NULL; | |
2258 | } | |
76dda93c YZ |
2259 | } |
2260 | out_dput: | |
2261 | dput(dentry); | |
2262 | out_unlock_dir: | |
2263 | mutex_unlock(&dir->i_mutex); | |
2a79f17e | 2264 | mnt_drop_write_file(file); |
76dda93c YZ |
2265 | out: |
2266 | kfree(vol_args); | |
2267 | return err; | |
2268 | } | |
2269 | ||
1e701a32 | 2270 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
f46b5a66 | 2271 | { |
496ad9aa | 2272 | struct inode *inode = file_inode(file); |
f46b5a66 | 2273 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1e701a32 | 2274 | struct btrfs_ioctl_defrag_range_args *range; |
c146afad YZ |
2275 | int ret; |
2276 | ||
25122d15 ID |
2277 | ret = mnt_want_write_file(file); |
2278 | if (ret) | |
2279 | return ret; | |
b83cc969 | 2280 | |
25122d15 ID |
2281 | if (btrfs_root_readonly(root)) { |
2282 | ret = -EROFS; | |
2283 | goto out; | |
5ac00add | 2284 | } |
f46b5a66 CH |
2285 | |
2286 | switch (inode->i_mode & S_IFMT) { | |
2287 | case S_IFDIR: | |
e441d54d CM |
2288 | if (!capable(CAP_SYS_ADMIN)) { |
2289 | ret = -EPERM; | |
2290 | goto out; | |
2291 | } | |
de78b51a | 2292 | ret = btrfs_defrag_root(root); |
8929ecfa YZ |
2293 | if (ret) |
2294 | goto out; | |
de78b51a | 2295 | ret = btrfs_defrag_root(root->fs_info->extent_root); |
f46b5a66 CH |
2296 | break; |
2297 | case S_IFREG: | |
e441d54d CM |
2298 | if (!(file->f_mode & FMODE_WRITE)) { |
2299 | ret = -EINVAL; | |
2300 | goto out; | |
2301 | } | |
1e701a32 CM |
2302 | |
2303 | range = kzalloc(sizeof(*range), GFP_KERNEL); | |
2304 | if (!range) { | |
2305 | ret = -ENOMEM; | |
2306 | goto out; | |
2307 | } | |
2308 | ||
2309 | if (argp) { | |
2310 | if (copy_from_user(range, argp, | |
2311 | sizeof(*range))) { | |
2312 | ret = -EFAULT; | |
2313 | kfree(range); | |
683be16e | 2314 | goto out; |
1e701a32 CM |
2315 | } |
2316 | /* compression requires us to start the IO */ | |
2317 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { | |
2318 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; | |
2319 | range->extent_thresh = (u32)-1; | |
2320 | } | |
2321 | } else { | |
2322 | /* the rest are all set to zero by kzalloc */ | |
2323 | range->len = (u64)-1; | |
2324 | } | |
496ad9aa | 2325 | ret = btrfs_defrag_file(file_inode(file), file, |
4cb5300b CM |
2326 | range, 0, 0); |
2327 | if (ret > 0) | |
2328 | ret = 0; | |
1e701a32 | 2329 | kfree(range); |
f46b5a66 | 2330 | break; |
8929ecfa YZ |
2331 | default: |
2332 | ret = -EINVAL; | |
f46b5a66 | 2333 | } |
e441d54d | 2334 | out: |
25122d15 | 2335 | mnt_drop_write_file(file); |
e441d54d | 2336 | return ret; |
f46b5a66 CH |
2337 | } |
2338 | ||
b2950863 | 2339 | static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) |
f46b5a66 CH |
2340 | { |
2341 | struct btrfs_ioctl_vol_args *vol_args; | |
2342 | int ret; | |
2343 | ||
e441d54d CM |
2344 | if (!capable(CAP_SYS_ADMIN)) |
2345 | return -EPERM; | |
2346 | ||
5ac00add SB |
2347 | if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, |
2348 | 1)) { | |
2349 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); | |
2c0c9da0 | 2350 | return -EINVAL; |
c9e9f97b ID |
2351 | } |
2352 | ||
5ac00add | 2353 | mutex_lock(&root->fs_info->volume_mutex); |
dae7b665 | 2354 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
2355 | if (IS_ERR(vol_args)) { |
2356 | ret = PTR_ERR(vol_args); | |
2357 | goto out; | |
2358 | } | |
f46b5a66 | 2359 | |
5516e595 | 2360 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
f46b5a66 CH |
2361 | ret = btrfs_init_new_device(root, vol_args->name); |
2362 | ||
f46b5a66 | 2363 | kfree(vol_args); |
c9e9f97b ID |
2364 | out: |
2365 | mutex_unlock(&root->fs_info->volume_mutex); | |
5ac00add | 2366 | atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); |
f46b5a66 CH |
2367 | return ret; |
2368 | } | |
2369 | ||
da24927b | 2370 | static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) |
f46b5a66 | 2371 | { |
496ad9aa | 2372 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
f46b5a66 CH |
2373 | struct btrfs_ioctl_vol_args *vol_args; |
2374 | int ret; | |
2375 | ||
e441d54d CM |
2376 | if (!capable(CAP_SYS_ADMIN)) |
2377 | return -EPERM; | |
2378 | ||
da24927b MX |
2379 | ret = mnt_want_write_file(file); |
2380 | if (ret) | |
2381 | return ret; | |
c146afad | 2382 | |
dae7b665 | 2383 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
2384 | if (IS_ERR(vol_args)) { |
2385 | ret = PTR_ERR(vol_args); | |
2386 | goto out; | |
2387 | } | |
f46b5a66 | 2388 | |
5516e595 | 2389 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
f46b5a66 | 2390 | |
183860f6 AJ |
2391 | if (atomic_xchg(&root->fs_info->mutually_exclusive_operation_running, |
2392 | 1)) { | |
2393 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; | |
2394 | goto out; | |
2395 | } | |
2396 | ||
2397 | mutex_lock(&root->fs_info->volume_mutex); | |
2398 | ret = btrfs_rm_device(root, vol_args->name); | |
c9e9f97b | 2399 | mutex_unlock(&root->fs_info->volume_mutex); |
5ac00add | 2400 | atomic_set(&root->fs_info->mutually_exclusive_operation_running, 0); |
183860f6 AJ |
2401 | |
2402 | out: | |
2403 | kfree(vol_args); | |
4ac20c70 | 2404 | mnt_drop_write_file(file); |
f46b5a66 CH |
2405 | return ret; |
2406 | } | |
2407 | ||
475f6387 JS |
2408 | static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) |
2409 | { | |
027ed2f0 | 2410 | struct btrfs_ioctl_fs_info_args *fi_args; |
475f6387 JS |
2411 | struct btrfs_device *device; |
2412 | struct btrfs_device *next; | |
2413 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | |
027ed2f0 | 2414 | int ret = 0; |
475f6387 JS |
2415 | |
2416 | if (!capable(CAP_SYS_ADMIN)) | |
2417 | return -EPERM; | |
2418 | ||
027ed2f0 LZ |
2419 | fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); |
2420 | if (!fi_args) | |
2421 | return -ENOMEM; | |
2422 | ||
2423 | fi_args->num_devices = fs_devices->num_devices; | |
2424 | memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); | |
475f6387 JS |
2425 | |
2426 | mutex_lock(&fs_devices->device_list_mutex); | |
2427 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { | |
027ed2f0 LZ |
2428 | if (device->devid > fi_args->max_id) |
2429 | fi_args->max_id = device->devid; | |
475f6387 JS |
2430 | } |
2431 | mutex_unlock(&fs_devices->device_list_mutex); | |
2432 | ||
027ed2f0 LZ |
2433 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) |
2434 | ret = -EFAULT; | |
475f6387 | 2435 | |
027ed2f0 LZ |
2436 | kfree(fi_args); |
2437 | return ret; | |
475f6387 JS |
2438 | } |
2439 | ||
2440 | static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) | |
2441 | { | |
2442 | struct btrfs_ioctl_dev_info_args *di_args; | |
2443 | struct btrfs_device *dev; | |
2444 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | |
2445 | int ret = 0; | |
2446 | char *s_uuid = NULL; | |
2447 | char empty_uuid[BTRFS_UUID_SIZE] = {0}; | |
2448 | ||
2449 | if (!capable(CAP_SYS_ADMIN)) | |
2450 | return -EPERM; | |
2451 | ||
2452 | di_args = memdup_user(arg, sizeof(*di_args)); | |
2453 | if (IS_ERR(di_args)) | |
2454 | return PTR_ERR(di_args); | |
2455 | ||
2456 | if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0) | |
2457 | s_uuid = di_args->uuid; | |
2458 | ||
2459 | mutex_lock(&fs_devices->device_list_mutex); | |
aa1b8cd4 | 2460 | dev = btrfs_find_device(root->fs_info, di_args->devid, s_uuid, NULL); |
475f6387 JS |
2461 | |
2462 | if (!dev) { | |
2463 | ret = -ENODEV; | |
2464 | goto out; | |
2465 | } | |
2466 | ||
2467 | di_args->devid = dev->devid; | |
2468 | di_args->bytes_used = dev->bytes_used; | |
2469 | di_args->total_bytes = dev->total_bytes; | |
2470 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); | |
a27202fb | 2471 | if (dev->name) { |
606686ee JB |
2472 | struct rcu_string *name; |
2473 | ||
2474 | rcu_read_lock(); | |
2475 | name = rcu_dereference(dev->name); | |
2476 | strncpy(di_args->path, name->str, sizeof(di_args->path)); | |
2477 | rcu_read_unlock(); | |
a27202fb JM |
2478 | di_args->path[sizeof(di_args->path) - 1] = 0; |
2479 | } else { | |
99ba55ad | 2480 | di_args->path[0] = '\0'; |
a27202fb | 2481 | } |
475f6387 JS |
2482 | |
2483 | out: | |
55793c0d | 2484 | mutex_unlock(&fs_devices->device_list_mutex); |
475f6387 JS |
2485 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) |
2486 | ret = -EFAULT; | |
2487 | ||
2488 | kfree(di_args); | |
2489 | return ret; | |
2490 | } | |
2491 | ||
416161db MF |
2492 | static struct page *extent_same_get_page(struct inode *inode, u64 off) |
2493 | { | |
2494 | struct page *page; | |
2495 | pgoff_t index; | |
2496 | struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree; | |
2497 | ||
2498 | index = off >> PAGE_CACHE_SHIFT; | |
2499 | ||
2500 | page = grab_cache_page(inode->i_mapping, index); | |
2501 | if (!page) | |
2502 | return NULL; | |
2503 | ||
2504 | if (!PageUptodate(page)) { | |
2505 | if (extent_read_full_page_nolock(tree, page, btrfs_get_extent, | |
2506 | 0)) | |
2507 | return NULL; | |
2508 | lock_page(page); | |
2509 | if (!PageUptodate(page)) { | |
2510 | unlock_page(page); | |
2511 | page_cache_release(page); | |
2512 | return NULL; | |
2513 | } | |
2514 | } | |
2515 | unlock_page(page); | |
2516 | ||
2517 | return page; | |
2518 | } | |
2519 | ||
77fe20dc MF |
2520 | static inline void lock_extent_range(struct inode *inode, u64 off, u64 len) |
2521 | { | |
2522 | /* do any pending delalloc/csum calc on src, one way or | |
2523 | another, and lock file content */ | |
2524 | while (1) { | |
2525 | struct btrfs_ordered_extent *ordered; | |
2526 | lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); | |
2527 | ordered = btrfs_lookup_first_ordered_extent(inode, | |
2528 | off + len - 1); | |
2529 | if (!ordered && | |
2530 | !test_range_bit(&BTRFS_I(inode)->io_tree, off, | |
2531 | off + len - 1, EXTENT_DELALLOC, 0, NULL)) | |
2532 | break; | |
2533 | unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); | |
2534 | if (ordered) | |
2535 | btrfs_put_ordered_extent(ordered); | |
2536 | btrfs_wait_ordered_range(inode, off, len); | |
2537 | } | |
2538 | } | |
2539 | ||
416161db MF |
2540 | static void btrfs_double_unlock(struct inode *inode1, u64 loff1, |
2541 | struct inode *inode2, u64 loff2, u64 len) | |
2542 | { | |
2543 | unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); | |
2544 | unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); | |
2545 | ||
2546 | mutex_unlock(&inode1->i_mutex); | |
2547 | mutex_unlock(&inode2->i_mutex); | |
2548 | } | |
2549 | ||
2550 | static void btrfs_double_lock(struct inode *inode1, u64 loff1, | |
2551 | struct inode *inode2, u64 loff2, u64 len) | |
2552 | { | |
2553 | if (inode1 < inode2) { | |
2554 | swap(inode1, inode2); | |
2555 | swap(loff1, loff2); | |
2556 | } | |
2557 | ||
2558 | mutex_lock_nested(&inode1->i_mutex, I_MUTEX_PARENT); | |
2559 | lock_extent_range(inode1, loff1, len); | |
2560 | if (inode1 != inode2) { | |
2561 | mutex_lock_nested(&inode2->i_mutex, I_MUTEX_CHILD); | |
2562 | lock_extent_range(inode2, loff2, len); | |
2563 | } | |
2564 | } | |
2565 | ||
2566 | static int btrfs_cmp_data(struct inode *src, u64 loff, struct inode *dst, | |
2567 | u64 dst_loff, u64 len) | |
2568 | { | |
2569 | int ret = 0; | |
2570 | struct page *src_page, *dst_page; | |
2571 | unsigned int cmp_len = PAGE_CACHE_SIZE; | |
2572 | void *addr, *dst_addr; | |
2573 | ||
2574 | while (len) { | |
2575 | if (len < PAGE_CACHE_SIZE) | |
2576 | cmp_len = len; | |
2577 | ||
2578 | src_page = extent_same_get_page(src, loff); | |
2579 | if (!src_page) | |
2580 | return -EINVAL; | |
2581 | dst_page = extent_same_get_page(dst, dst_loff); | |
2582 | if (!dst_page) { | |
2583 | page_cache_release(src_page); | |
2584 | return -EINVAL; | |
2585 | } | |
2586 | addr = kmap_atomic(src_page); | |
2587 | dst_addr = kmap_atomic(dst_page); | |
2588 | ||
2589 | flush_dcache_page(src_page); | |
2590 | flush_dcache_page(dst_page); | |
2591 | ||
2592 | if (memcmp(addr, dst_addr, cmp_len)) | |
2593 | ret = BTRFS_SAME_DATA_DIFFERS; | |
2594 | ||
2595 | kunmap_atomic(addr); | |
2596 | kunmap_atomic(dst_addr); | |
2597 | page_cache_release(src_page); | |
2598 | page_cache_release(dst_page); | |
2599 | ||
2600 | if (ret) | |
2601 | break; | |
2602 | ||
2603 | loff += cmp_len; | |
2604 | dst_loff += cmp_len; | |
2605 | len -= cmp_len; | |
2606 | } | |
2607 | ||
2608 | return ret; | |
2609 | } | |
2610 | ||
2611 | static int extent_same_check_offsets(struct inode *inode, u64 off, u64 len) | |
2612 | { | |
2613 | u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; | |
2614 | ||
2615 | if (off + len > inode->i_size || off + len < off) | |
2616 | return -EINVAL; | |
2617 | /* Check that we are block aligned - btrfs_clone() requires this */ | |
2618 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) | |
2619 | return -EINVAL; | |
2620 | ||
2621 | return 0; | |
2622 | } | |
2623 | ||
2624 | static int btrfs_extent_same(struct inode *src, u64 loff, u64 len, | |
2625 | struct inode *dst, u64 dst_loff) | |
2626 | { | |
2627 | int ret; | |
2628 | ||
2629 | /* | |
2630 | * btrfs_clone() can't handle extents in the same file | |
2631 | * yet. Once that works, we can drop this check and replace it | |
2632 | * with a check for the same inode, but overlapping extents. | |
2633 | */ | |
2634 | if (src == dst) | |
2635 | return -EINVAL; | |
2636 | ||
2637 | btrfs_double_lock(src, loff, dst, dst_loff, len); | |
2638 | ||
2639 | ret = extent_same_check_offsets(src, loff, len); | |
2640 | if (ret) | |
2641 | goto out_unlock; | |
2642 | ||
2643 | ret = extent_same_check_offsets(dst, dst_loff, len); | |
2644 | if (ret) | |
2645 | goto out_unlock; | |
2646 | ||
2647 | /* don't make the dst file partly checksummed */ | |
2648 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != | |
2649 | (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { | |
2650 | ret = -EINVAL; | |
2651 | goto out_unlock; | |
2652 | } | |
2653 | ||
2654 | ret = btrfs_cmp_data(src, loff, dst, dst_loff, len); | |
2655 | if (ret == 0) | |
2656 | ret = btrfs_clone(src, dst, loff, len, len, dst_loff); | |
2657 | ||
2658 | out_unlock: | |
2659 | btrfs_double_unlock(src, loff, dst, dst_loff, len); | |
2660 | ||
2661 | return ret; | |
2662 | } | |
2663 | ||
2664 | #define BTRFS_MAX_DEDUPE_LEN (16 * 1024 * 1024) | |
2665 | ||
2666 | static long btrfs_ioctl_file_extent_same(struct file *file, | |
2667 | void __user *argp) | |
2668 | { | |
2669 | struct btrfs_ioctl_same_args *args = argp; | |
2670 | struct btrfs_ioctl_same_args same; | |
2671 | struct btrfs_ioctl_same_extent_info info; | |
2672 | struct inode *src = file->f_dentry->d_inode; | |
2673 | struct file *dst_file = NULL; | |
2674 | struct inode *dst; | |
2675 | u64 off; | |
2676 | u64 len; | |
2677 | int i; | |
2678 | int ret; | |
2679 | u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; | |
2680 | bool is_admin = capable(CAP_SYS_ADMIN); | |
2681 | ||
2682 | if (!(file->f_mode & FMODE_READ)) | |
2683 | return -EINVAL; | |
2684 | ||
2685 | ret = mnt_want_write_file(file); | |
2686 | if (ret) | |
2687 | return ret; | |
2688 | ||
2689 | if (copy_from_user(&same, | |
2690 | (struct btrfs_ioctl_same_args __user *)argp, | |
2691 | sizeof(same))) { | |
2692 | ret = -EFAULT; | |
2693 | goto out; | |
2694 | } | |
2695 | ||
2696 | off = same.logical_offset; | |
2697 | len = same.length; | |
2698 | ||
2699 | /* | |
2700 | * Limit the total length we will dedupe for each operation. | |
2701 | * This is intended to bound the total time spent in this | |
2702 | * ioctl to something sane. | |
2703 | */ | |
2704 | if (len > BTRFS_MAX_DEDUPE_LEN) | |
2705 | len = BTRFS_MAX_DEDUPE_LEN; | |
2706 | ||
2707 | if (WARN_ON_ONCE(bs < PAGE_CACHE_SIZE)) { | |
2708 | /* | |
2709 | * Btrfs does not support blocksize < page_size. As a | |
2710 | * result, btrfs_cmp_data() won't correctly handle | |
2711 | * this situation without an update. | |
2712 | */ | |
2713 | ret = -EINVAL; | |
2714 | goto out; | |
2715 | } | |
2716 | ||
2717 | ret = -EISDIR; | |
2718 | if (S_ISDIR(src->i_mode)) | |
2719 | goto out; | |
2720 | ||
2721 | ret = -EACCES; | |
2722 | if (!S_ISREG(src->i_mode)) | |
2723 | goto out; | |
2724 | ||
2725 | ret = 0; | |
2726 | for (i = 0; i < same.dest_count; i++) { | |
2727 | if (copy_from_user(&info, &args->info[i], sizeof(info))) { | |
2728 | ret = -EFAULT; | |
2729 | goto out; | |
2730 | } | |
2731 | ||
2732 | info.bytes_deduped = 0; | |
2733 | ||
2734 | dst_file = fget(info.fd); | |
2735 | if (!dst_file) { | |
2736 | info.status = -EBADF; | |
2737 | goto next; | |
2738 | } | |
2739 | ||
2740 | if (!(is_admin || (dst_file->f_mode & FMODE_WRITE))) { | |
2741 | info.status = -EINVAL; | |
2742 | goto next; | |
2743 | } | |
2744 | ||
2745 | info.status = -EXDEV; | |
2746 | if (file->f_path.mnt != dst_file->f_path.mnt) | |
2747 | goto next; | |
2748 | ||
2749 | dst = dst_file->f_dentry->d_inode; | |
2750 | if (src->i_sb != dst->i_sb) | |
2751 | goto next; | |
2752 | ||
2753 | if (S_ISDIR(dst->i_mode)) { | |
2754 | info.status = -EISDIR; | |
2755 | goto next; | |
2756 | } | |
2757 | ||
2758 | if (!S_ISREG(dst->i_mode)) { | |
2759 | info.status = -EACCES; | |
2760 | goto next; | |
2761 | } | |
2762 | ||
2763 | info.status = btrfs_extent_same(src, off, len, dst, | |
2764 | info.logical_offset); | |
2765 | if (info.status == 0) | |
2766 | info.bytes_deduped += len; | |
2767 | ||
2768 | next: | |
2769 | if (dst_file) | |
2770 | fput(dst_file); | |
2771 | ||
2772 | if (__put_user_unaligned(info.status, &args->info[i].status) || | |
2773 | __put_user_unaligned(info.bytes_deduped, | |
2774 | &args->info[i].bytes_deduped)) { | |
2775 | ret = -EFAULT; | |
2776 | goto out; | |
2777 | } | |
2778 | } | |
2779 | ||
2780 | out: | |
2781 | mnt_drop_write_file(file); | |
2782 | return ret; | |
2783 | } | |
2784 | ||
32b7c687 MF |
2785 | /** |
2786 | * btrfs_clone() - clone a range from inode file to another | |
2787 | * | |
2788 | * @src: Inode to clone from | |
2789 | * @inode: Inode to clone to | |
2790 | * @off: Offset within source to start clone from | |
2791 | * @olen: Original length, passed by user, of range to clone | |
2792 | * @olen_aligned: Block-aligned value of olen, extent_same uses | |
2793 | * identical values here | |
2794 | * @destoff: Offset within @inode to start clone | |
2795 | */ | |
2796 | static int btrfs_clone(struct inode *src, struct inode *inode, | |
2797 | u64 off, u64 olen, u64 olen_aligned, u64 destoff) | |
f46b5a66 | 2798 | { |
f46b5a66 | 2799 | struct btrfs_root *root = BTRFS_I(inode)->root; |
32b7c687 | 2800 | struct btrfs_path *path = NULL; |
f46b5a66 | 2801 | struct extent_buffer *leaf; |
32b7c687 MF |
2802 | struct btrfs_trans_handle *trans; |
2803 | char *buf = NULL; | |
ae01a0ab | 2804 | struct btrfs_key key; |
f46b5a66 CH |
2805 | u32 nritems; |
2806 | int slot; | |
ae01a0ab | 2807 | int ret; |
32b7c687 | 2808 | u64 len = olen_aligned; |
ae01a0ab YZ |
2809 | |
2810 | ret = -ENOMEM; | |
2811 | buf = vmalloc(btrfs_level_size(root, 0)); | |
2812 | if (!buf) | |
32b7c687 | 2813 | return ret; |
ae01a0ab YZ |
2814 | |
2815 | path = btrfs_alloc_path(); | |
2816 | if (!path) { | |
2817 | vfree(buf); | |
32b7c687 | 2818 | return ret; |
d525e8ab LZ |
2819 | } |
2820 | ||
32b7c687 | 2821 | path->reada = 2; |
c5c9cd4d | 2822 | /* clone data */ |
33345d01 | 2823 | key.objectid = btrfs_ino(src); |
ae01a0ab YZ |
2824 | key.type = BTRFS_EXTENT_DATA_KEY; |
2825 | key.offset = 0; | |
f46b5a66 CH |
2826 | |
2827 | while (1) { | |
2828 | /* | |
2829 | * note the key will change type as we walk through the | |
2830 | * tree. | |
2831 | */ | |
362a20c5 DS |
2832 | ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, |
2833 | 0, 0); | |
f46b5a66 CH |
2834 | if (ret < 0) |
2835 | goto out; | |
2836 | ||
ae01a0ab YZ |
2837 | nritems = btrfs_header_nritems(path->nodes[0]); |
2838 | if (path->slots[0] >= nritems) { | |
362a20c5 | 2839 | ret = btrfs_next_leaf(BTRFS_I(src)->root, path); |
f46b5a66 CH |
2840 | if (ret < 0) |
2841 | goto out; | |
2842 | if (ret > 0) | |
2843 | break; | |
ae01a0ab | 2844 | nritems = btrfs_header_nritems(path->nodes[0]); |
f46b5a66 CH |
2845 | } |
2846 | leaf = path->nodes[0]; | |
2847 | slot = path->slots[0]; | |
f46b5a66 | 2848 | |
ae01a0ab | 2849 | btrfs_item_key_to_cpu(leaf, &key, slot); |
d20f7043 | 2850 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
33345d01 | 2851 | key.objectid != btrfs_ino(src)) |
f46b5a66 CH |
2852 | break; |
2853 | ||
c5c9cd4d SW |
2854 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
2855 | struct btrfs_file_extent_item *extent; | |
2856 | int type; | |
31840ae1 ZY |
2857 | u32 size; |
2858 | struct btrfs_key new_key; | |
c5c9cd4d SW |
2859 | u64 disko = 0, diskl = 0; |
2860 | u64 datao = 0, datal = 0; | |
2861 | u8 comp; | |
b5384d48 | 2862 | u64 endoff; |
31840ae1 ZY |
2863 | |
2864 | size = btrfs_item_size_nr(leaf, slot); | |
2865 | read_extent_buffer(leaf, buf, | |
2866 | btrfs_item_ptr_offset(leaf, slot), | |
2867 | size); | |
c5c9cd4d SW |
2868 | |
2869 | extent = btrfs_item_ptr(leaf, slot, | |
2870 | struct btrfs_file_extent_item); | |
2871 | comp = btrfs_file_extent_compression(leaf, extent); | |
2872 | type = btrfs_file_extent_type(leaf, extent); | |
c8a894d7 CM |
2873 | if (type == BTRFS_FILE_EXTENT_REG || |
2874 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d397712b CM |
2875 | disko = btrfs_file_extent_disk_bytenr(leaf, |
2876 | extent); | |
2877 | diskl = btrfs_file_extent_disk_num_bytes(leaf, | |
2878 | extent); | |
c5c9cd4d | 2879 | datao = btrfs_file_extent_offset(leaf, extent); |
d397712b CM |
2880 | datal = btrfs_file_extent_num_bytes(leaf, |
2881 | extent); | |
c5c9cd4d SW |
2882 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
2883 | /* take upper bound, may be compressed */ | |
2884 | datal = btrfs_file_extent_ram_bytes(leaf, | |
2885 | extent); | |
2886 | } | |
b3b4aa74 | 2887 | btrfs_release_path(path); |
31840ae1 | 2888 | |
050006a7 | 2889 | if (key.offset + datal <= off || |
aa42ffd9 | 2890 | key.offset >= off + len - 1) |
c5c9cd4d SW |
2891 | goto next; |
2892 | ||
31840ae1 | 2893 | memcpy(&new_key, &key, sizeof(new_key)); |
33345d01 | 2894 | new_key.objectid = btrfs_ino(inode); |
4d728ec7 LZ |
2895 | if (off <= key.offset) |
2896 | new_key.offset = key.offset + destoff - off; | |
2897 | else | |
2898 | new_key.offset = destoff; | |
31840ae1 | 2899 | |
b6f3409b SW |
2900 | /* |
2901 | * 1 - adjusting old extent (we may have to split it) | |
2902 | * 1 - add new extent | |
2903 | * 1 - inode update | |
2904 | */ | |
2905 | trans = btrfs_start_transaction(root, 3); | |
a22285a6 YZ |
2906 | if (IS_ERR(trans)) { |
2907 | ret = PTR_ERR(trans); | |
2908 | goto out; | |
2909 | } | |
2910 | ||
c8a894d7 CM |
2911 | if (type == BTRFS_FILE_EXTENT_REG || |
2912 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d72c0842 LZ |
2913 | /* |
2914 | * a | --- range to clone ---| b | |
2915 | * | ------------- extent ------------- | | |
2916 | */ | |
2917 | ||
2918 | /* substract range b */ | |
2919 | if (key.offset + datal > off + len) | |
2920 | datal = off + len - key.offset; | |
2921 | ||
2922 | /* substract range a */ | |
a22285a6 YZ |
2923 | if (off > key.offset) { |
2924 | datao += off - key.offset; | |
2925 | datal -= off - key.offset; | |
2926 | } | |
2927 | ||
5dc562c5 | 2928 | ret = btrfs_drop_extents(trans, root, inode, |
a22285a6 YZ |
2929 | new_key.offset, |
2930 | new_key.offset + datal, | |
2671485d | 2931 | 1); |
79787eaa JM |
2932 | if (ret) { |
2933 | btrfs_abort_transaction(trans, root, | |
2934 | ret); | |
2935 | btrfs_end_transaction(trans, root); | |
2936 | goto out; | |
2937 | } | |
a22285a6 | 2938 | |
c5c9cd4d SW |
2939 | ret = btrfs_insert_empty_item(trans, root, path, |
2940 | &new_key, size); | |
79787eaa JM |
2941 | if (ret) { |
2942 | btrfs_abort_transaction(trans, root, | |
2943 | ret); | |
2944 | btrfs_end_transaction(trans, root); | |
2945 | goto out; | |
2946 | } | |
c5c9cd4d SW |
2947 | |
2948 | leaf = path->nodes[0]; | |
2949 | slot = path->slots[0]; | |
2950 | write_extent_buffer(leaf, buf, | |
31840ae1 ZY |
2951 | btrfs_item_ptr_offset(leaf, slot), |
2952 | size); | |
ae01a0ab | 2953 | |
c5c9cd4d | 2954 | extent = btrfs_item_ptr(leaf, slot, |
f46b5a66 | 2955 | struct btrfs_file_extent_item); |
c5c9cd4d | 2956 | |
c5c9cd4d SW |
2957 | /* disko == 0 means it's a hole */ |
2958 | if (!disko) | |
2959 | datao = 0; | |
c5c9cd4d SW |
2960 | |
2961 | btrfs_set_file_extent_offset(leaf, extent, | |
2962 | datao); | |
2963 | btrfs_set_file_extent_num_bytes(leaf, extent, | |
2964 | datal); | |
2965 | if (disko) { | |
2966 | inode_add_bytes(inode, datal); | |
ae01a0ab | 2967 | ret = btrfs_inc_extent_ref(trans, root, |
5d4f98a2 YZ |
2968 | disko, diskl, 0, |
2969 | root->root_key.objectid, | |
33345d01 | 2970 | btrfs_ino(inode), |
66d7e7f0 AJ |
2971 | new_key.offset - datao, |
2972 | 0); | |
79787eaa JM |
2973 | if (ret) { |
2974 | btrfs_abort_transaction(trans, | |
2975 | root, | |
2976 | ret); | |
2977 | btrfs_end_transaction(trans, | |
2978 | root); | |
2979 | goto out; | |
2980 | ||
2981 | } | |
f46b5a66 | 2982 | } |
c5c9cd4d SW |
2983 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
2984 | u64 skip = 0; | |
2985 | u64 trim = 0; | |
2986 | if (off > key.offset) { | |
2987 | skip = off - key.offset; | |
2988 | new_key.offset += skip; | |
2989 | } | |
d397712b | 2990 | |
aa42ffd9 LB |
2991 | if (key.offset + datal > off + len) |
2992 | trim = key.offset + datal - (off + len); | |
d397712b | 2993 | |
c5c9cd4d | 2994 | if (comp && (skip || trim)) { |
c5c9cd4d | 2995 | ret = -EINVAL; |
a22285a6 | 2996 | btrfs_end_transaction(trans, root); |
c5c9cd4d SW |
2997 | goto out; |
2998 | } | |
2999 | size -= skip + trim; | |
3000 | datal -= skip + trim; | |
a22285a6 | 3001 | |
5dc562c5 | 3002 | ret = btrfs_drop_extents(trans, root, inode, |
a22285a6 YZ |
3003 | new_key.offset, |
3004 | new_key.offset + datal, | |
2671485d | 3005 | 1); |
79787eaa JM |
3006 | if (ret) { |
3007 | btrfs_abort_transaction(trans, root, | |
3008 | ret); | |
3009 | btrfs_end_transaction(trans, root); | |
3010 | goto out; | |
3011 | } | |
a22285a6 | 3012 | |
c5c9cd4d SW |
3013 | ret = btrfs_insert_empty_item(trans, root, path, |
3014 | &new_key, size); | |
79787eaa JM |
3015 | if (ret) { |
3016 | btrfs_abort_transaction(trans, root, | |
3017 | ret); | |
3018 | btrfs_end_transaction(trans, root); | |
3019 | goto out; | |
3020 | } | |
c5c9cd4d SW |
3021 | |
3022 | if (skip) { | |
d397712b CM |
3023 | u32 start = |
3024 | btrfs_file_extent_calc_inline_size(0); | |
c5c9cd4d SW |
3025 | memmove(buf+start, buf+start+skip, |
3026 | datal); | |
3027 | } | |
3028 | ||
3029 | leaf = path->nodes[0]; | |
3030 | slot = path->slots[0]; | |
3031 | write_extent_buffer(leaf, buf, | |
3032 | btrfs_item_ptr_offset(leaf, slot), | |
3033 | size); | |
3034 | inode_add_bytes(inode, datal); | |
f46b5a66 | 3035 | } |
c5c9cd4d SW |
3036 | |
3037 | btrfs_mark_buffer_dirty(leaf); | |
b3b4aa74 | 3038 | btrfs_release_path(path); |
c5c9cd4d | 3039 | |
0c4d2d95 | 3040 | inode_inc_iversion(inode); |
a22285a6 | 3041 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
b5384d48 SW |
3042 | |
3043 | /* | |
3044 | * we round up to the block size at eof when | |
3045 | * determining which extents to clone above, | |
3046 | * but shouldn't round up the file size | |
3047 | */ | |
3048 | endoff = new_key.offset + datal; | |
5f3888ff LZ |
3049 | if (endoff > destoff+olen) |
3050 | endoff = destoff+olen; | |
b5384d48 SW |
3051 | if (endoff > inode->i_size) |
3052 | btrfs_i_size_write(inode, endoff); | |
3053 | ||
a22285a6 | 3054 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
3055 | if (ret) { |
3056 | btrfs_abort_transaction(trans, root, ret); | |
3057 | btrfs_end_transaction(trans, root); | |
3058 | goto out; | |
3059 | } | |
3060 | ret = btrfs_end_transaction(trans, root); | |
a22285a6 | 3061 | } |
d397712b | 3062 | next: |
b3b4aa74 | 3063 | btrfs_release_path(path); |
f46b5a66 | 3064 | key.offset++; |
f46b5a66 | 3065 | } |
f46b5a66 | 3066 | ret = 0; |
32b7c687 | 3067 | |
f46b5a66 | 3068 | out: |
b3b4aa74 | 3069 | btrfs_release_path(path); |
32b7c687 MF |
3070 | btrfs_free_path(path); |
3071 | vfree(buf); | |
3072 | return ret; | |
3073 | } | |
3074 | ||
3075 | static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, | |
3076 | u64 off, u64 olen, u64 destoff) | |
3077 | { | |
3078 | struct inode *inode = fdentry(file)->d_inode; | |
3079 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3080 | struct fd src_file; | |
3081 | struct inode *src; | |
3082 | int ret; | |
3083 | u64 len = olen; | |
3084 | u64 bs = root->fs_info->sb->s_blocksize; | |
3085 | int same_inode = 0; | |
3086 | ||
3087 | /* | |
3088 | * TODO: | |
3089 | * - split compressed inline extents. annoying: we need to | |
3090 | * decompress into destination's address_space (the file offset | |
3091 | * may change, so source mapping won't do), then recompress (or | |
3092 | * otherwise reinsert) a subrange. | |
3093 | * - allow ranges within the same file to be cloned (provided | |
3094 | * they don't overlap)? | |
3095 | */ | |
3096 | ||
3097 | /* the destination must be opened for writing */ | |
3098 | if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) | |
3099 | return -EINVAL; | |
3100 | ||
3101 | if (btrfs_root_readonly(root)) | |
3102 | return -EROFS; | |
3103 | ||
3104 | ret = mnt_want_write_file(file); | |
3105 | if (ret) | |
3106 | return ret; | |
3107 | ||
3108 | src_file = fdget(srcfd); | |
3109 | if (!src_file.file) { | |
3110 | ret = -EBADF; | |
3111 | goto out_drop_write; | |
3112 | } | |
3113 | ||
3114 | ret = -EXDEV; | |
3115 | if (src_file.file->f_path.mnt != file->f_path.mnt) | |
3116 | goto out_fput; | |
3117 | ||
3118 | src = file_inode(src_file.file); | |
3119 | ||
3120 | ret = -EINVAL; | |
3121 | if (src == inode) | |
3122 | same_inode = 1; | |
3123 | ||
3124 | /* the src must be open for reading */ | |
3125 | if (!(src_file.file->f_mode & FMODE_READ)) | |
3126 | goto out_fput; | |
3127 | ||
3128 | /* don't make the dst file partly checksummed */ | |
3129 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != | |
3130 | (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) | |
3131 | goto out_fput; | |
3132 | ||
3133 | ret = -EISDIR; | |
3134 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) | |
3135 | goto out_fput; | |
3136 | ||
3137 | ret = -EXDEV; | |
3138 | if (src->i_sb != inode->i_sb) | |
3139 | goto out_fput; | |
3140 | ||
3141 | if (!same_inode) { | |
3142 | if (inode < src) { | |
3143 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); | |
3144 | mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); | |
3145 | } else { | |
3146 | mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); | |
3147 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); | |
3148 | } | |
3149 | } else { | |
3150 | mutex_lock(&src->i_mutex); | |
3151 | } | |
3152 | ||
3153 | /* determine range to clone */ | |
3154 | ret = -EINVAL; | |
3155 | if (off + len > src->i_size || off + len < off) | |
3156 | goto out_unlock; | |
3157 | if (len == 0) | |
3158 | olen = len = src->i_size - off; | |
3159 | /* if we extend to eof, continue to block boundary */ | |
3160 | if (off + len == src->i_size) | |
3161 | len = ALIGN(src->i_size, bs) - off; | |
3162 | ||
3163 | /* verify the end result is block aligned */ | |
3164 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || | |
3165 | !IS_ALIGNED(destoff, bs)) | |
3166 | goto out_unlock; | |
3167 | ||
3168 | /* verify if ranges are overlapped within the same file */ | |
3169 | if (same_inode) { | |
3170 | if (destoff + len > off && destoff < off + len) | |
3171 | goto out_unlock; | |
3172 | } | |
3173 | ||
3174 | if (destoff > inode->i_size) { | |
3175 | ret = btrfs_cont_expand(inode, inode->i_size, destoff); | |
3176 | if (ret) | |
3177 | goto out_unlock; | |
3178 | } | |
3179 | ||
3180 | /* truncate page cache pages from target inode range */ | |
3181 | truncate_inode_pages_range(&inode->i_data, destoff, | |
3182 | PAGE_CACHE_ALIGN(destoff + len) - 1); | |
3183 | ||
3184 | lock_extent_range(src, off, len); | |
3185 | ||
3186 | ret = btrfs_clone(src, inode, off, olen, len, destoff); | |
3187 | ||
aa42ffd9 | 3188 | unlock_extent(&BTRFS_I(src)->io_tree, off, off + len - 1); |
f46b5a66 CH |
3189 | out_unlock: |
3190 | mutex_unlock(&src->i_mutex); | |
a96fbc72 LB |
3191 | if (!same_inode) |
3192 | mutex_unlock(&inode->i_mutex); | |
f46b5a66 | 3193 | out_fput: |
2903ff01 | 3194 | fdput(src_file); |
ab67b7c1 | 3195 | out_drop_write: |
2a79f17e | 3196 | mnt_drop_write_file(file); |
f46b5a66 CH |
3197 | return ret; |
3198 | } | |
3199 | ||
7a865e8a | 3200 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
c5c9cd4d SW |
3201 | { |
3202 | struct btrfs_ioctl_clone_range_args args; | |
3203 | ||
7a865e8a | 3204 | if (copy_from_user(&args, argp, sizeof(args))) |
c5c9cd4d SW |
3205 | return -EFAULT; |
3206 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, | |
3207 | args.src_length, args.dest_offset); | |
3208 | } | |
3209 | ||
f46b5a66 CH |
3210 | /* |
3211 | * there are many ways the trans_start and trans_end ioctls can lead | |
3212 | * to deadlocks. They should only be used by applications that | |
3213 | * basically own the machine, and have a very in depth understanding | |
3214 | * of all the possible deadlocks and enospc problems. | |
3215 | */ | |
b2950863 | 3216 | static long btrfs_ioctl_trans_start(struct file *file) |
f46b5a66 | 3217 | { |
496ad9aa | 3218 | struct inode *inode = file_inode(file); |
f46b5a66 CH |
3219 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3220 | struct btrfs_trans_handle *trans; | |
1ab86aed | 3221 | int ret; |
f46b5a66 | 3222 | |
1ab86aed | 3223 | ret = -EPERM; |
df5b5520 | 3224 | if (!capable(CAP_SYS_ADMIN)) |
1ab86aed | 3225 | goto out; |
df5b5520 | 3226 | |
1ab86aed SW |
3227 | ret = -EINPROGRESS; |
3228 | if (file->private_data) | |
f46b5a66 | 3229 | goto out; |
9ca9ee09 | 3230 | |
b83cc969 LZ |
3231 | ret = -EROFS; |
3232 | if (btrfs_root_readonly(root)) | |
3233 | goto out; | |
3234 | ||
a561be71 | 3235 | ret = mnt_want_write_file(file); |
c146afad YZ |
3236 | if (ret) |
3237 | goto out; | |
3238 | ||
a4abeea4 | 3239 | atomic_inc(&root->fs_info->open_ioctl_trans); |
9ca9ee09 | 3240 | |
1ab86aed | 3241 | ret = -ENOMEM; |
7a7eaa40 | 3242 | trans = btrfs_start_ioctl_transaction(root); |
abd30bb0 | 3243 | if (IS_ERR(trans)) |
1ab86aed SW |
3244 | goto out_drop; |
3245 | ||
3246 | file->private_data = trans; | |
3247 | return 0; | |
3248 | ||
3249 | out_drop: | |
a4abeea4 | 3250 | atomic_dec(&root->fs_info->open_ioctl_trans); |
2a79f17e | 3251 | mnt_drop_write_file(file); |
f46b5a66 | 3252 | out: |
f46b5a66 CH |
3253 | return ret; |
3254 | } | |
3255 | ||
6ef5ed0d JB |
3256 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
3257 | { | |
496ad9aa | 3258 | struct inode *inode = file_inode(file); |
6ef5ed0d JB |
3259 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3260 | struct btrfs_root *new_root; | |
3261 | struct btrfs_dir_item *di; | |
3262 | struct btrfs_trans_handle *trans; | |
3263 | struct btrfs_path *path; | |
3264 | struct btrfs_key location; | |
3265 | struct btrfs_disk_key disk_key; | |
6ef5ed0d JB |
3266 | u64 objectid = 0; |
3267 | u64 dir_id; | |
3c04ce01 | 3268 | int ret; |
6ef5ed0d JB |
3269 | |
3270 | if (!capable(CAP_SYS_ADMIN)) | |
3271 | return -EPERM; | |
3272 | ||
3c04ce01 MX |
3273 | ret = mnt_want_write_file(file); |
3274 | if (ret) | |
3275 | return ret; | |
3276 | ||
3277 | if (copy_from_user(&objectid, argp, sizeof(objectid))) { | |
3278 | ret = -EFAULT; | |
3279 | goto out; | |
3280 | } | |
6ef5ed0d JB |
3281 | |
3282 | if (!objectid) | |
3283 | objectid = root->root_key.objectid; | |
3284 | ||
3285 | location.objectid = objectid; | |
3286 | location.type = BTRFS_ROOT_ITEM_KEY; | |
3287 | location.offset = (u64)-1; | |
3288 | ||
3289 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); | |
3c04ce01 MX |
3290 | if (IS_ERR(new_root)) { |
3291 | ret = PTR_ERR(new_root); | |
3292 | goto out; | |
3293 | } | |
6ef5ed0d | 3294 | |
6ef5ed0d | 3295 | path = btrfs_alloc_path(); |
3c04ce01 MX |
3296 | if (!path) { |
3297 | ret = -ENOMEM; | |
3298 | goto out; | |
3299 | } | |
6ef5ed0d JB |
3300 | path->leave_spinning = 1; |
3301 | ||
3302 | trans = btrfs_start_transaction(root, 1); | |
98d5dc13 | 3303 | if (IS_ERR(trans)) { |
6ef5ed0d | 3304 | btrfs_free_path(path); |
3c04ce01 MX |
3305 | ret = PTR_ERR(trans); |
3306 | goto out; | |
6ef5ed0d JB |
3307 | } |
3308 | ||
6c41761f | 3309 | dir_id = btrfs_super_root_dir(root->fs_info->super_copy); |
6ef5ed0d JB |
3310 | di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, |
3311 | dir_id, "default", 7, 1); | |
cf1e99a4 | 3312 | if (IS_ERR_OR_NULL(di)) { |
6ef5ed0d JB |
3313 | btrfs_free_path(path); |
3314 | btrfs_end_transaction(trans, root); | |
3315 | printk(KERN_ERR "Umm, you don't have the default dir item, " | |
3316 | "this isn't going to work\n"); | |
3c04ce01 MX |
3317 | ret = -ENOENT; |
3318 | goto out; | |
6ef5ed0d JB |
3319 | } |
3320 | ||
3321 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); | |
3322 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); | |
3323 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
3324 | btrfs_free_path(path); | |
3325 | ||
2b0ce2c2 | 3326 | btrfs_set_fs_incompat(root->fs_info, DEFAULT_SUBVOL); |
6ef5ed0d | 3327 | btrfs_end_transaction(trans, root); |
3c04ce01 MX |
3328 | out: |
3329 | mnt_drop_write_file(file); | |
3330 | return ret; | |
6ef5ed0d JB |
3331 | } |
3332 | ||
5af3e8cc SB |
3333 | void btrfs_get_block_group_info(struct list_head *groups_list, |
3334 | struct btrfs_ioctl_space_info *space) | |
bf5fc093 JB |
3335 | { |
3336 | struct btrfs_block_group_cache *block_group; | |
3337 | ||
3338 | space->total_bytes = 0; | |
3339 | space->used_bytes = 0; | |
3340 | space->flags = 0; | |
3341 | list_for_each_entry(block_group, groups_list, list) { | |
3342 | space->flags = block_group->flags; | |
3343 | space->total_bytes += block_group->key.offset; | |
3344 | space->used_bytes += | |
3345 | btrfs_block_group_used(&block_group->item); | |
3346 | } | |
3347 | } | |
3348 | ||
48a3b636 | 3349 | static long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) |
1406e432 JB |
3350 | { |
3351 | struct btrfs_ioctl_space_args space_args; | |
3352 | struct btrfs_ioctl_space_info space; | |
3353 | struct btrfs_ioctl_space_info *dest; | |
7fde62bf | 3354 | struct btrfs_ioctl_space_info *dest_orig; |
13f2696f | 3355 | struct btrfs_ioctl_space_info __user *user_dest; |
1406e432 | 3356 | struct btrfs_space_info *info; |
bf5fc093 JB |
3357 | u64 types[] = {BTRFS_BLOCK_GROUP_DATA, |
3358 | BTRFS_BLOCK_GROUP_SYSTEM, | |
3359 | BTRFS_BLOCK_GROUP_METADATA, | |
3360 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; | |
3361 | int num_types = 4; | |
7fde62bf | 3362 | int alloc_size; |
1406e432 | 3363 | int ret = 0; |
51788b1b | 3364 | u64 slot_count = 0; |
bf5fc093 | 3365 | int i, c; |
1406e432 JB |
3366 | |
3367 | if (copy_from_user(&space_args, | |
3368 | (struct btrfs_ioctl_space_args __user *)arg, | |
3369 | sizeof(space_args))) | |
3370 | return -EFAULT; | |
3371 | ||
bf5fc093 JB |
3372 | for (i = 0; i < num_types; i++) { |
3373 | struct btrfs_space_info *tmp; | |
3374 | ||
3375 | info = NULL; | |
3376 | rcu_read_lock(); | |
3377 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, | |
3378 | list) { | |
3379 | if (tmp->flags == types[i]) { | |
3380 | info = tmp; | |
3381 | break; | |
3382 | } | |
3383 | } | |
3384 | rcu_read_unlock(); | |
3385 | ||
3386 | if (!info) | |
3387 | continue; | |
3388 | ||
3389 | down_read(&info->groups_sem); | |
3390 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
3391 | if (!list_empty(&info->block_groups[c])) | |
3392 | slot_count++; | |
3393 | } | |
3394 | up_read(&info->groups_sem); | |
3395 | } | |
7fde62bf CM |
3396 | |
3397 | /* space_slots == 0 means they are asking for a count */ | |
3398 | if (space_args.space_slots == 0) { | |
3399 | space_args.total_spaces = slot_count; | |
3400 | goto out; | |
3401 | } | |
bf5fc093 | 3402 | |
51788b1b | 3403 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
bf5fc093 | 3404 | |
7fde62bf | 3405 | alloc_size = sizeof(*dest) * slot_count; |
bf5fc093 | 3406 | |
7fde62bf CM |
3407 | /* we generally have at most 6 or so space infos, one for each raid |
3408 | * level. So, a whole page should be more than enough for everyone | |
3409 | */ | |
3410 | if (alloc_size > PAGE_CACHE_SIZE) | |
3411 | return -ENOMEM; | |
3412 | ||
1406e432 | 3413 | space_args.total_spaces = 0; |
7fde62bf CM |
3414 | dest = kmalloc(alloc_size, GFP_NOFS); |
3415 | if (!dest) | |
3416 | return -ENOMEM; | |
3417 | dest_orig = dest; | |
1406e432 | 3418 | |
7fde62bf | 3419 | /* now we have a buffer to copy into */ |
bf5fc093 JB |
3420 | for (i = 0; i < num_types; i++) { |
3421 | struct btrfs_space_info *tmp; | |
3422 | ||
51788b1b DR |
3423 | if (!slot_count) |
3424 | break; | |
3425 | ||
bf5fc093 JB |
3426 | info = NULL; |
3427 | rcu_read_lock(); | |
3428 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, | |
3429 | list) { | |
3430 | if (tmp->flags == types[i]) { | |
3431 | info = tmp; | |
3432 | break; | |
3433 | } | |
3434 | } | |
3435 | rcu_read_unlock(); | |
7fde62bf | 3436 | |
bf5fc093 JB |
3437 | if (!info) |
3438 | continue; | |
3439 | down_read(&info->groups_sem); | |
3440 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
3441 | if (!list_empty(&info->block_groups[c])) { | |
5af3e8cc SB |
3442 | btrfs_get_block_group_info( |
3443 | &info->block_groups[c], &space); | |
bf5fc093 JB |
3444 | memcpy(dest, &space, sizeof(space)); |
3445 | dest++; | |
3446 | space_args.total_spaces++; | |
51788b1b | 3447 | slot_count--; |
bf5fc093 | 3448 | } |
51788b1b DR |
3449 | if (!slot_count) |
3450 | break; | |
bf5fc093 JB |
3451 | } |
3452 | up_read(&info->groups_sem); | |
1406e432 | 3453 | } |
1406e432 | 3454 | |
2eec6c81 | 3455 | user_dest = (struct btrfs_ioctl_space_info __user *) |
7fde62bf CM |
3456 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
3457 | ||
3458 | if (copy_to_user(user_dest, dest_orig, alloc_size)) | |
3459 | ret = -EFAULT; | |
3460 | ||
3461 | kfree(dest_orig); | |
3462 | out: | |
3463 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) | |
1406e432 JB |
3464 | ret = -EFAULT; |
3465 | ||
3466 | return ret; | |
3467 | } | |
3468 | ||
f46b5a66 CH |
3469 | /* |
3470 | * there are many ways the trans_start and trans_end ioctls can lead | |
3471 | * to deadlocks. They should only be used by applications that | |
3472 | * basically own the machine, and have a very in depth understanding | |
3473 | * of all the possible deadlocks and enospc problems. | |
3474 | */ | |
3475 | long btrfs_ioctl_trans_end(struct file *file) | |
3476 | { | |
496ad9aa | 3477 | struct inode *inode = file_inode(file); |
f46b5a66 CH |
3478 | struct btrfs_root *root = BTRFS_I(inode)->root; |
3479 | struct btrfs_trans_handle *trans; | |
f46b5a66 | 3480 | |
f46b5a66 | 3481 | trans = file->private_data; |
1ab86aed SW |
3482 | if (!trans) |
3483 | return -EINVAL; | |
b214107e | 3484 | file->private_data = NULL; |
9ca9ee09 | 3485 | |
1ab86aed SW |
3486 | btrfs_end_transaction(trans, root); |
3487 | ||
a4abeea4 | 3488 | atomic_dec(&root->fs_info->open_ioctl_trans); |
9ca9ee09 | 3489 | |
2a79f17e | 3490 | mnt_drop_write_file(file); |
1ab86aed | 3491 | return 0; |
f46b5a66 CH |
3492 | } |
3493 | ||
9a8c28be MX |
3494 | static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, |
3495 | void __user *argp) | |
46204592 | 3496 | { |
46204592 SW |
3497 | struct btrfs_trans_handle *trans; |
3498 | u64 transid; | |
db5b493a | 3499 | int ret; |
46204592 | 3500 | |
d4edf39b | 3501 | trans = btrfs_attach_transaction_barrier(root); |
ff7c1d33 MX |
3502 | if (IS_ERR(trans)) { |
3503 | if (PTR_ERR(trans) != -ENOENT) | |
3504 | return PTR_ERR(trans); | |
3505 | ||
3506 | /* No running transaction, don't bother */ | |
3507 | transid = root->fs_info->last_trans_committed; | |
3508 | goto out; | |
3509 | } | |
46204592 | 3510 | transid = trans->transid; |
db5b493a | 3511 | ret = btrfs_commit_transaction_async(trans, root, 0); |
8b2b2d3c TI |
3512 | if (ret) { |
3513 | btrfs_end_transaction(trans, root); | |
db5b493a | 3514 | return ret; |
8b2b2d3c | 3515 | } |
ff7c1d33 | 3516 | out: |
46204592 SW |
3517 | if (argp) |
3518 | if (copy_to_user(argp, &transid, sizeof(transid))) | |
3519 | return -EFAULT; | |
3520 | return 0; | |
3521 | } | |
3522 | ||
9a8c28be MX |
3523 | static noinline long btrfs_ioctl_wait_sync(struct btrfs_root *root, |
3524 | void __user *argp) | |
46204592 | 3525 | { |
46204592 SW |
3526 | u64 transid; |
3527 | ||
3528 | if (argp) { | |
3529 | if (copy_from_user(&transid, argp, sizeof(transid))) | |
3530 | return -EFAULT; | |
3531 | } else { | |
3532 | transid = 0; /* current trans */ | |
3533 | } | |
3534 | return btrfs_wait_for_commit(root, transid); | |
3535 | } | |
3536 | ||
b8e95489 | 3537 | static long btrfs_ioctl_scrub(struct file *file, void __user *arg) |
475f6387 | 3538 | { |
496ad9aa | 3539 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
475f6387 | 3540 | struct btrfs_ioctl_scrub_args *sa; |
b8e95489 | 3541 | int ret; |
475f6387 JS |
3542 | |
3543 | if (!capable(CAP_SYS_ADMIN)) | |
3544 | return -EPERM; | |
3545 | ||
3546 | sa = memdup_user(arg, sizeof(*sa)); | |
3547 | if (IS_ERR(sa)) | |
3548 | return PTR_ERR(sa); | |
3549 | ||
b8e95489 MX |
3550 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) { |
3551 | ret = mnt_want_write_file(file); | |
3552 | if (ret) | |
3553 | goto out; | |
3554 | } | |
3555 | ||
aa1b8cd4 | 3556 | ret = btrfs_scrub_dev(root->fs_info, sa->devid, sa->start, sa->end, |
63a212ab SB |
3557 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, |
3558 | 0); | |
475f6387 JS |
3559 | |
3560 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
3561 | ret = -EFAULT; | |
3562 | ||
b8e95489 MX |
3563 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) |
3564 | mnt_drop_write_file(file); | |
3565 | out: | |
475f6387 JS |
3566 | kfree(sa); |
3567 | return ret; | |
3568 | } | |
3569 | ||
3570 | static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) | |
3571 | { | |
3572 | if (!capable(CAP_SYS_ADMIN)) | |
3573 | return -EPERM; | |
3574 | ||
aa1b8cd4 | 3575 | return btrfs_scrub_cancel(root->fs_info); |
475f6387 JS |
3576 | } |
3577 | ||
3578 | static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, | |
3579 | void __user *arg) | |
3580 | { | |
3581 | struct btrfs_ioctl_scrub_args *sa; | |
3582 | int ret; | |
3583 | ||
3584 | if (!capable(CAP_SYS_ADMIN)) | |
3585 | return -EPERM; | |
3586 | ||
3587 | sa = memdup_user(arg, sizeof(*sa)); | |
3588 | if (IS_ERR(sa)) | |
3589 | return PTR_ERR(sa); | |
3590 | ||
3591 | ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); | |
3592 | ||
3593 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
3594 | ret = -EFAULT; | |
3595 | ||
3596 | kfree(sa); | |
3597 | return ret; | |
3598 | } | |
3599 | ||
c11d2c23 | 3600 | static long btrfs_ioctl_get_dev_stats(struct btrfs_root *root, |
b27f7c0c | 3601 | void __user *arg) |
c11d2c23 SB |
3602 | { |
3603 | struct btrfs_ioctl_get_dev_stats *sa; | |
3604 | int ret; | |
3605 | ||
c11d2c23 SB |
3606 | sa = memdup_user(arg, sizeof(*sa)); |
3607 | if (IS_ERR(sa)) | |
3608 | return PTR_ERR(sa); | |
3609 | ||
b27f7c0c DS |
3610 | if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { |
3611 | kfree(sa); | |
3612 | return -EPERM; | |
3613 | } | |
3614 | ||
3615 | ret = btrfs_get_dev_stats(root, sa); | |
c11d2c23 SB |
3616 | |
3617 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
3618 | ret = -EFAULT; | |
3619 | ||
3620 | kfree(sa); | |
3621 | return ret; | |
3622 | } | |
3623 | ||
3f6bcfbd SB |
3624 | static long btrfs_ioctl_dev_replace(struct btrfs_root *root, void __user *arg) |
3625 | { | |
3626 | struct btrfs_ioctl_dev_replace_args *p; | |
3627 | int ret; | |
3628 | ||
3629 | if (!capable(CAP_SYS_ADMIN)) | |
3630 | return -EPERM; | |
3631 | ||
3632 | p = memdup_user(arg, sizeof(*p)); | |
3633 | if (IS_ERR(p)) | |
3634 | return PTR_ERR(p); | |
3635 | ||
3636 | switch (p->cmd) { | |
3637 | case BTRFS_IOCTL_DEV_REPLACE_CMD_START: | |
3638 | if (atomic_xchg( | |
3639 | &root->fs_info->mutually_exclusive_operation_running, | |
3640 | 1)) { | |
3641 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); | |
3642 | ret = -EINPROGRESS; | |
3643 | } else { | |
3644 | ret = btrfs_dev_replace_start(root, p); | |
3645 | atomic_set( | |
3646 | &root->fs_info->mutually_exclusive_operation_running, | |
3647 | 0); | |
3648 | } | |
3649 | break; | |
3650 | case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: | |
3651 | btrfs_dev_replace_status(root->fs_info, p); | |
3652 | ret = 0; | |
3653 | break; | |
3654 | case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: | |
3655 | ret = btrfs_dev_replace_cancel(root->fs_info, p); | |
3656 | break; | |
3657 | default: | |
3658 | ret = -EINVAL; | |
3659 | break; | |
3660 | } | |
3661 | ||
3662 | if (copy_to_user(arg, p, sizeof(*p))) | |
3663 | ret = -EFAULT; | |
3664 | ||
3665 | kfree(p); | |
3666 | return ret; | |
3667 | } | |
3668 | ||
d7728c96 JS |
3669 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
3670 | { | |
3671 | int ret = 0; | |
3672 | int i; | |
740c3d22 | 3673 | u64 rel_ptr; |
d7728c96 | 3674 | int size; |
806468f8 | 3675 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
d7728c96 JS |
3676 | struct inode_fs_paths *ipath = NULL; |
3677 | struct btrfs_path *path; | |
3678 | ||
82b22ac8 | 3679 | if (!capable(CAP_DAC_READ_SEARCH)) |
d7728c96 JS |
3680 | return -EPERM; |
3681 | ||
3682 | path = btrfs_alloc_path(); | |
3683 | if (!path) { | |
3684 | ret = -ENOMEM; | |
3685 | goto out; | |
3686 | } | |
3687 | ||
3688 | ipa = memdup_user(arg, sizeof(*ipa)); | |
3689 | if (IS_ERR(ipa)) { | |
3690 | ret = PTR_ERR(ipa); | |
3691 | ipa = NULL; | |
3692 | goto out; | |
3693 | } | |
3694 | ||
3695 | size = min_t(u32, ipa->size, 4096); | |
3696 | ipath = init_ipath(size, root, path); | |
3697 | if (IS_ERR(ipath)) { | |
3698 | ret = PTR_ERR(ipath); | |
3699 | ipath = NULL; | |
3700 | goto out; | |
3701 | } | |
3702 | ||
3703 | ret = paths_from_inode(ipa->inum, ipath); | |
3704 | if (ret < 0) | |
3705 | goto out; | |
3706 | ||
3707 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { | |
745c4d8e JM |
3708 | rel_ptr = ipath->fspath->val[i] - |
3709 | (u64)(unsigned long)ipath->fspath->val; | |
740c3d22 | 3710 | ipath->fspath->val[i] = rel_ptr; |
d7728c96 JS |
3711 | } |
3712 | ||
745c4d8e JM |
3713 | ret = copy_to_user((void *)(unsigned long)ipa->fspath, |
3714 | (void *)(unsigned long)ipath->fspath, size); | |
d7728c96 JS |
3715 | if (ret) { |
3716 | ret = -EFAULT; | |
3717 | goto out; | |
3718 | } | |
3719 | ||
3720 | out: | |
3721 | btrfs_free_path(path); | |
3722 | free_ipath(ipath); | |
3723 | kfree(ipa); | |
3724 | ||
3725 | return ret; | |
3726 | } | |
3727 | ||
3728 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) | |
3729 | { | |
3730 | struct btrfs_data_container *inodes = ctx; | |
3731 | const size_t c = 3 * sizeof(u64); | |
3732 | ||
3733 | if (inodes->bytes_left >= c) { | |
3734 | inodes->bytes_left -= c; | |
3735 | inodes->val[inodes->elem_cnt] = inum; | |
3736 | inodes->val[inodes->elem_cnt + 1] = offset; | |
3737 | inodes->val[inodes->elem_cnt + 2] = root; | |
3738 | inodes->elem_cnt += 3; | |
3739 | } else { | |
3740 | inodes->bytes_missing += c - inodes->bytes_left; | |
3741 | inodes->bytes_left = 0; | |
3742 | inodes->elem_missed += 3; | |
3743 | } | |
3744 | ||
3745 | return 0; | |
3746 | } | |
3747 | ||
3748 | static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, | |
3749 | void __user *arg) | |
3750 | { | |
3751 | int ret = 0; | |
3752 | int size; | |
d7728c96 JS |
3753 | struct btrfs_ioctl_logical_ino_args *loi; |
3754 | struct btrfs_data_container *inodes = NULL; | |
3755 | struct btrfs_path *path = NULL; | |
d7728c96 JS |
3756 | |
3757 | if (!capable(CAP_SYS_ADMIN)) | |
3758 | return -EPERM; | |
3759 | ||
3760 | loi = memdup_user(arg, sizeof(*loi)); | |
3761 | if (IS_ERR(loi)) { | |
3762 | ret = PTR_ERR(loi); | |
3763 | loi = NULL; | |
3764 | goto out; | |
3765 | } | |
3766 | ||
3767 | path = btrfs_alloc_path(); | |
3768 | if (!path) { | |
3769 | ret = -ENOMEM; | |
3770 | goto out; | |
3771 | } | |
3772 | ||
425d17a2 | 3773 | size = min_t(u32, loi->size, 64 * 1024); |
d7728c96 JS |
3774 | inodes = init_data_container(size); |
3775 | if (IS_ERR(inodes)) { | |
3776 | ret = PTR_ERR(inodes); | |
3777 | inodes = NULL; | |
3778 | goto out; | |
3779 | } | |
3780 | ||
df031f07 LB |
3781 | ret = iterate_inodes_from_logical(loi->logical, root->fs_info, path, |
3782 | build_ino_list, inodes); | |
3783 | if (ret == -EINVAL) | |
d7728c96 JS |
3784 | ret = -ENOENT; |
3785 | if (ret < 0) | |
3786 | goto out; | |
3787 | ||
745c4d8e JM |
3788 | ret = copy_to_user((void *)(unsigned long)loi->inodes, |
3789 | (void *)(unsigned long)inodes, size); | |
d7728c96 JS |
3790 | if (ret) |
3791 | ret = -EFAULT; | |
3792 | ||
3793 | out: | |
3794 | btrfs_free_path(path); | |
425d17a2 | 3795 | vfree(inodes); |
d7728c96 JS |
3796 | kfree(loi); |
3797 | ||
3798 | return ret; | |
3799 | } | |
3800 | ||
19a39dce | 3801 | void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, |
c9e9f97b ID |
3802 | struct btrfs_ioctl_balance_args *bargs) |
3803 | { | |
3804 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
3805 | ||
3806 | bargs->flags = bctl->flags; | |
3807 | ||
837d5b6e ID |
3808 | if (atomic_read(&fs_info->balance_running)) |
3809 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; | |
3810 | if (atomic_read(&fs_info->balance_pause_req)) | |
3811 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; | |
a7e99c69 ID |
3812 | if (atomic_read(&fs_info->balance_cancel_req)) |
3813 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; | |
837d5b6e | 3814 | |
c9e9f97b ID |
3815 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
3816 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); | |
3817 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); | |
19a39dce ID |
3818 | |
3819 | if (lock) { | |
3820 | spin_lock(&fs_info->balance_lock); | |
3821 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); | |
3822 | spin_unlock(&fs_info->balance_lock); | |
3823 | } else { | |
3824 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); | |
3825 | } | |
c9e9f97b ID |
3826 | } |
3827 | ||
9ba1f6e4 | 3828 | static long btrfs_ioctl_balance(struct file *file, void __user *arg) |
c9e9f97b | 3829 | { |
496ad9aa | 3830 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
c9e9f97b ID |
3831 | struct btrfs_fs_info *fs_info = root->fs_info; |
3832 | struct btrfs_ioctl_balance_args *bargs; | |
3833 | struct btrfs_balance_control *bctl; | |
ed0fb78f | 3834 | bool need_unlock; /* for mut. excl. ops lock */ |
c9e9f97b ID |
3835 | int ret; |
3836 | ||
3837 | if (!capable(CAP_SYS_ADMIN)) | |
3838 | return -EPERM; | |
3839 | ||
e54bfa31 | 3840 | ret = mnt_want_write_file(file); |
9ba1f6e4 LB |
3841 | if (ret) |
3842 | return ret; | |
3843 | ||
ed0fb78f ID |
3844 | again: |
3845 | if (!atomic_xchg(&fs_info->mutually_exclusive_operation_running, 1)) { | |
3846 | mutex_lock(&fs_info->volume_mutex); | |
3847 | mutex_lock(&fs_info->balance_mutex); | |
3848 | need_unlock = true; | |
3849 | goto locked; | |
3850 | } | |
3851 | ||
3852 | /* | |
3853 | * mut. excl. ops lock is locked. Three possibilites: | |
3854 | * (1) some other op is running | |
3855 | * (2) balance is running | |
3856 | * (3) balance is paused -- special case (think resume) | |
3857 | */ | |
c9e9f97b | 3858 | mutex_lock(&fs_info->balance_mutex); |
ed0fb78f ID |
3859 | if (fs_info->balance_ctl) { |
3860 | /* this is either (2) or (3) */ | |
3861 | if (!atomic_read(&fs_info->balance_running)) { | |
3862 | mutex_unlock(&fs_info->balance_mutex); | |
3863 | if (!mutex_trylock(&fs_info->volume_mutex)) | |
3864 | goto again; | |
3865 | mutex_lock(&fs_info->balance_mutex); | |
3866 | ||
3867 | if (fs_info->balance_ctl && | |
3868 | !atomic_read(&fs_info->balance_running)) { | |
3869 | /* this is (3) */ | |
3870 | need_unlock = false; | |
3871 | goto locked; | |
3872 | } | |
3873 | ||
3874 | mutex_unlock(&fs_info->balance_mutex); | |
3875 | mutex_unlock(&fs_info->volume_mutex); | |
3876 | goto again; | |
3877 | } else { | |
3878 | /* this is (2) */ | |
3879 | mutex_unlock(&fs_info->balance_mutex); | |
3880 | ret = -EINPROGRESS; | |
3881 | goto out; | |
3882 | } | |
3883 | } else { | |
3884 | /* this is (1) */ | |
3885 | mutex_unlock(&fs_info->balance_mutex); | |
3886 | pr_info("btrfs: dev add/delete/balance/replace/resize operation in progress\n"); | |
3887 | ret = -EINVAL; | |
3888 | goto out; | |
3889 | } | |
3890 | ||
3891 | locked: | |
3892 | BUG_ON(!atomic_read(&fs_info->mutually_exclusive_operation_running)); | |
c9e9f97b ID |
3893 | |
3894 | if (arg) { | |
3895 | bargs = memdup_user(arg, sizeof(*bargs)); | |
3896 | if (IS_ERR(bargs)) { | |
3897 | ret = PTR_ERR(bargs); | |
ed0fb78f | 3898 | goto out_unlock; |
c9e9f97b | 3899 | } |
de322263 ID |
3900 | |
3901 | if (bargs->flags & BTRFS_BALANCE_RESUME) { | |
3902 | if (!fs_info->balance_ctl) { | |
3903 | ret = -ENOTCONN; | |
3904 | goto out_bargs; | |
3905 | } | |
3906 | ||
3907 | bctl = fs_info->balance_ctl; | |
3908 | spin_lock(&fs_info->balance_lock); | |
3909 | bctl->flags |= BTRFS_BALANCE_RESUME; | |
3910 | spin_unlock(&fs_info->balance_lock); | |
3911 | ||
3912 | goto do_balance; | |
3913 | } | |
c9e9f97b ID |
3914 | } else { |
3915 | bargs = NULL; | |
3916 | } | |
3917 | ||
ed0fb78f | 3918 | if (fs_info->balance_ctl) { |
837d5b6e ID |
3919 | ret = -EINPROGRESS; |
3920 | goto out_bargs; | |
3921 | } | |
3922 | ||
c9e9f97b ID |
3923 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
3924 | if (!bctl) { | |
3925 | ret = -ENOMEM; | |
3926 | goto out_bargs; | |
3927 | } | |
3928 | ||
3929 | bctl->fs_info = fs_info; | |
3930 | if (arg) { | |
3931 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); | |
3932 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); | |
3933 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); | |
3934 | ||
3935 | bctl->flags = bargs->flags; | |
f43ffb60 ID |
3936 | } else { |
3937 | /* balance everything - no filters */ | |
3938 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; | |
c9e9f97b ID |
3939 | } |
3940 | ||
de322263 | 3941 | do_balance: |
c9e9f97b | 3942 | /* |
ed0fb78f ID |
3943 | * Ownership of bctl and mutually_exclusive_operation_running |
3944 | * goes to to btrfs_balance. bctl is freed in __cancel_balance, | |
3945 | * or, if restriper was paused all the way until unmount, in | |
3946 | * free_fs_info. mutually_exclusive_operation_running is | |
3947 | * cleared in __cancel_balance. | |
c9e9f97b | 3948 | */ |
ed0fb78f ID |
3949 | need_unlock = false; |
3950 | ||
3951 | ret = btrfs_balance(bctl, bargs); | |
3952 | ||
c9e9f97b ID |
3953 | if (arg) { |
3954 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | |
3955 | ret = -EFAULT; | |
3956 | } | |
3957 | ||
3958 | out_bargs: | |
3959 | kfree(bargs); | |
ed0fb78f | 3960 | out_unlock: |
c9e9f97b ID |
3961 | mutex_unlock(&fs_info->balance_mutex); |
3962 | mutex_unlock(&fs_info->volume_mutex); | |
ed0fb78f ID |
3963 | if (need_unlock) |
3964 | atomic_set(&fs_info->mutually_exclusive_operation_running, 0); | |
3965 | out: | |
e54bfa31 | 3966 | mnt_drop_write_file(file); |
c9e9f97b ID |
3967 | return ret; |
3968 | } | |
3969 | ||
837d5b6e ID |
3970 | static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) |
3971 | { | |
3972 | if (!capable(CAP_SYS_ADMIN)) | |
3973 | return -EPERM; | |
3974 | ||
3975 | switch (cmd) { | |
3976 | case BTRFS_BALANCE_CTL_PAUSE: | |
3977 | return btrfs_pause_balance(root->fs_info); | |
a7e99c69 ID |
3978 | case BTRFS_BALANCE_CTL_CANCEL: |
3979 | return btrfs_cancel_balance(root->fs_info); | |
837d5b6e ID |
3980 | } |
3981 | ||
3982 | return -EINVAL; | |
3983 | } | |
3984 | ||
19a39dce ID |
3985 | static long btrfs_ioctl_balance_progress(struct btrfs_root *root, |
3986 | void __user *arg) | |
3987 | { | |
3988 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3989 | struct btrfs_ioctl_balance_args *bargs; | |
3990 | int ret = 0; | |
3991 | ||
3992 | if (!capable(CAP_SYS_ADMIN)) | |
3993 | return -EPERM; | |
3994 | ||
3995 | mutex_lock(&fs_info->balance_mutex); | |
3996 | if (!fs_info->balance_ctl) { | |
3997 | ret = -ENOTCONN; | |
3998 | goto out; | |
3999 | } | |
4000 | ||
4001 | bargs = kzalloc(sizeof(*bargs), GFP_NOFS); | |
4002 | if (!bargs) { | |
4003 | ret = -ENOMEM; | |
4004 | goto out; | |
4005 | } | |
4006 | ||
4007 | update_ioctl_balance_args(fs_info, 1, bargs); | |
4008 | ||
4009 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | |
4010 | ret = -EFAULT; | |
4011 | ||
4012 | kfree(bargs); | |
4013 | out: | |
4014 | mutex_unlock(&fs_info->balance_mutex); | |
4015 | return ret; | |
4016 | } | |
4017 | ||
905b0dda | 4018 | static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) |
5d13a37b | 4019 | { |
496ad9aa | 4020 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
5d13a37b AJ |
4021 | struct btrfs_ioctl_quota_ctl_args *sa; |
4022 | struct btrfs_trans_handle *trans = NULL; | |
4023 | int ret; | |
4024 | int err; | |
4025 | ||
4026 | if (!capable(CAP_SYS_ADMIN)) | |
4027 | return -EPERM; | |
4028 | ||
905b0dda MX |
4029 | ret = mnt_want_write_file(file); |
4030 | if (ret) | |
4031 | return ret; | |
5d13a37b AJ |
4032 | |
4033 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4034 | if (IS_ERR(sa)) { |
4035 | ret = PTR_ERR(sa); | |
4036 | goto drop_write; | |
4037 | } | |
5d13a37b | 4038 | |
7708f029 | 4039 | down_write(&root->fs_info->subvol_sem); |
2f232036 JS |
4040 | trans = btrfs_start_transaction(root->fs_info->tree_root, 2); |
4041 | if (IS_ERR(trans)) { | |
4042 | ret = PTR_ERR(trans); | |
4043 | goto out; | |
5d13a37b AJ |
4044 | } |
4045 | ||
4046 | switch (sa->cmd) { | |
4047 | case BTRFS_QUOTA_CTL_ENABLE: | |
4048 | ret = btrfs_quota_enable(trans, root->fs_info); | |
4049 | break; | |
4050 | case BTRFS_QUOTA_CTL_DISABLE: | |
4051 | ret = btrfs_quota_disable(trans, root->fs_info); | |
4052 | break; | |
5d13a37b AJ |
4053 | default: |
4054 | ret = -EINVAL; | |
4055 | break; | |
4056 | } | |
4057 | ||
2f232036 JS |
4058 | err = btrfs_commit_transaction(trans, root->fs_info->tree_root); |
4059 | if (err && !ret) | |
4060 | ret = err; | |
5d13a37b AJ |
4061 | out: |
4062 | kfree(sa); | |
7708f029 | 4063 | up_write(&root->fs_info->subvol_sem); |
905b0dda MX |
4064 | drop_write: |
4065 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4066 | return ret; |
4067 | } | |
4068 | ||
905b0dda | 4069 | static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) |
5d13a37b | 4070 | { |
496ad9aa | 4071 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
5d13a37b AJ |
4072 | struct btrfs_ioctl_qgroup_assign_args *sa; |
4073 | struct btrfs_trans_handle *trans; | |
4074 | int ret; | |
4075 | int err; | |
4076 | ||
4077 | if (!capable(CAP_SYS_ADMIN)) | |
4078 | return -EPERM; | |
4079 | ||
905b0dda MX |
4080 | ret = mnt_want_write_file(file); |
4081 | if (ret) | |
4082 | return ret; | |
5d13a37b AJ |
4083 | |
4084 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4085 | if (IS_ERR(sa)) { |
4086 | ret = PTR_ERR(sa); | |
4087 | goto drop_write; | |
4088 | } | |
5d13a37b AJ |
4089 | |
4090 | trans = btrfs_join_transaction(root); | |
4091 | if (IS_ERR(trans)) { | |
4092 | ret = PTR_ERR(trans); | |
4093 | goto out; | |
4094 | } | |
4095 | ||
4096 | /* FIXME: check if the IDs really exist */ | |
4097 | if (sa->assign) { | |
4098 | ret = btrfs_add_qgroup_relation(trans, root->fs_info, | |
4099 | sa->src, sa->dst); | |
4100 | } else { | |
4101 | ret = btrfs_del_qgroup_relation(trans, root->fs_info, | |
4102 | sa->src, sa->dst); | |
4103 | } | |
4104 | ||
4105 | err = btrfs_end_transaction(trans, root); | |
4106 | if (err && !ret) | |
4107 | ret = err; | |
4108 | ||
4109 | out: | |
4110 | kfree(sa); | |
905b0dda MX |
4111 | drop_write: |
4112 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4113 | return ret; |
4114 | } | |
4115 | ||
905b0dda | 4116 | static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) |
5d13a37b | 4117 | { |
496ad9aa | 4118 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
5d13a37b AJ |
4119 | struct btrfs_ioctl_qgroup_create_args *sa; |
4120 | struct btrfs_trans_handle *trans; | |
4121 | int ret; | |
4122 | int err; | |
4123 | ||
4124 | if (!capable(CAP_SYS_ADMIN)) | |
4125 | return -EPERM; | |
4126 | ||
905b0dda MX |
4127 | ret = mnt_want_write_file(file); |
4128 | if (ret) | |
4129 | return ret; | |
5d13a37b AJ |
4130 | |
4131 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4132 | if (IS_ERR(sa)) { |
4133 | ret = PTR_ERR(sa); | |
4134 | goto drop_write; | |
4135 | } | |
5d13a37b | 4136 | |
d86e56cf MX |
4137 | if (!sa->qgroupid) { |
4138 | ret = -EINVAL; | |
4139 | goto out; | |
4140 | } | |
4141 | ||
5d13a37b AJ |
4142 | trans = btrfs_join_transaction(root); |
4143 | if (IS_ERR(trans)) { | |
4144 | ret = PTR_ERR(trans); | |
4145 | goto out; | |
4146 | } | |
4147 | ||
4148 | /* FIXME: check if the IDs really exist */ | |
4149 | if (sa->create) { | |
4150 | ret = btrfs_create_qgroup(trans, root->fs_info, sa->qgroupid, | |
4151 | NULL); | |
4152 | } else { | |
4153 | ret = btrfs_remove_qgroup(trans, root->fs_info, sa->qgroupid); | |
4154 | } | |
4155 | ||
4156 | err = btrfs_end_transaction(trans, root); | |
4157 | if (err && !ret) | |
4158 | ret = err; | |
4159 | ||
4160 | out: | |
4161 | kfree(sa); | |
905b0dda MX |
4162 | drop_write: |
4163 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4164 | return ret; |
4165 | } | |
4166 | ||
905b0dda | 4167 | static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) |
5d13a37b | 4168 | { |
496ad9aa | 4169 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
5d13a37b AJ |
4170 | struct btrfs_ioctl_qgroup_limit_args *sa; |
4171 | struct btrfs_trans_handle *trans; | |
4172 | int ret; | |
4173 | int err; | |
4174 | u64 qgroupid; | |
4175 | ||
4176 | if (!capable(CAP_SYS_ADMIN)) | |
4177 | return -EPERM; | |
4178 | ||
905b0dda MX |
4179 | ret = mnt_want_write_file(file); |
4180 | if (ret) | |
4181 | return ret; | |
5d13a37b AJ |
4182 | |
4183 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
4184 | if (IS_ERR(sa)) { |
4185 | ret = PTR_ERR(sa); | |
4186 | goto drop_write; | |
4187 | } | |
5d13a37b AJ |
4188 | |
4189 | trans = btrfs_join_transaction(root); | |
4190 | if (IS_ERR(trans)) { | |
4191 | ret = PTR_ERR(trans); | |
4192 | goto out; | |
4193 | } | |
4194 | ||
4195 | qgroupid = sa->qgroupid; | |
4196 | if (!qgroupid) { | |
4197 | /* take the current subvol as qgroup */ | |
4198 | qgroupid = root->root_key.objectid; | |
4199 | } | |
4200 | ||
4201 | /* FIXME: check if the IDs really exist */ | |
4202 | ret = btrfs_limit_qgroup(trans, root->fs_info, qgroupid, &sa->lim); | |
4203 | ||
4204 | err = btrfs_end_transaction(trans, root); | |
4205 | if (err && !ret) | |
4206 | ret = err; | |
4207 | ||
4208 | out: | |
4209 | kfree(sa); | |
905b0dda MX |
4210 | drop_write: |
4211 | mnt_drop_write_file(file); | |
5d13a37b AJ |
4212 | return ret; |
4213 | } | |
4214 | ||
2f232036 JS |
4215 | static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) |
4216 | { | |
6d0379ec | 4217 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
2f232036 JS |
4218 | struct btrfs_ioctl_quota_rescan_args *qsa; |
4219 | int ret; | |
4220 | ||
4221 | if (!capable(CAP_SYS_ADMIN)) | |
4222 | return -EPERM; | |
4223 | ||
4224 | ret = mnt_want_write_file(file); | |
4225 | if (ret) | |
4226 | return ret; | |
4227 | ||
4228 | qsa = memdup_user(arg, sizeof(*qsa)); | |
4229 | if (IS_ERR(qsa)) { | |
4230 | ret = PTR_ERR(qsa); | |
4231 | goto drop_write; | |
4232 | } | |
4233 | ||
4234 | if (qsa->flags) { | |
4235 | ret = -EINVAL; | |
4236 | goto out; | |
4237 | } | |
4238 | ||
4239 | ret = btrfs_qgroup_rescan(root->fs_info); | |
4240 | ||
4241 | out: | |
4242 | kfree(qsa); | |
4243 | drop_write: | |
4244 | mnt_drop_write_file(file); | |
4245 | return ret; | |
4246 | } | |
4247 | ||
4248 | static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) | |
4249 | { | |
6d0379ec | 4250 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
2f232036 JS |
4251 | struct btrfs_ioctl_quota_rescan_args *qsa; |
4252 | int ret = 0; | |
4253 | ||
4254 | if (!capable(CAP_SYS_ADMIN)) | |
4255 | return -EPERM; | |
4256 | ||
4257 | qsa = kzalloc(sizeof(*qsa), GFP_NOFS); | |
4258 | if (!qsa) | |
4259 | return -ENOMEM; | |
4260 | ||
4261 | if (root->fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { | |
4262 | qsa->flags = 1; | |
4263 | qsa->progress = root->fs_info->qgroup_rescan_progress.objectid; | |
4264 | } | |
4265 | ||
4266 | if (copy_to_user(arg, qsa, sizeof(*qsa))) | |
4267 | ret = -EFAULT; | |
4268 | ||
4269 | kfree(qsa); | |
4270 | return ret; | |
4271 | } | |
4272 | ||
57254b6e JS |
4273 | static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) |
4274 | { | |
4275 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; | |
4276 | ||
4277 | if (!capable(CAP_SYS_ADMIN)) | |
4278 | return -EPERM; | |
4279 | ||
4280 | return btrfs_qgroup_wait_for_completion(root->fs_info); | |
4281 | } | |
4282 | ||
8ea05e3a AB |
4283 | static long btrfs_ioctl_set_received_subvol(struct file *file, |
4284 | void __user *arg) | |
4285 | { | |
4286 | struct btrfs_ioctl_received_subvol_args *sa = NULL; | |
496ad9aa | 4287 | struct inode *inode = file_inode(file); |
8ea05e3a AB |
4288 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4289 | struct btrfs_root_item *root_item = &root->root_item; | |
4290 | struct btrfs_trans_handle *trans; | |
4291 | struct timespec ct = CURRENT_TIME; | |
4292 | int ret = 0; | |
4293 | ||
4294 | ret = mnt_want_write_file(file); | |
4295 | if (ret < 0) | |
4296 | return ret; | |
4297 | ||
4298 | down_write(&root->fs_info->subvol_sem); | |
4299 | ||
4300 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { | |
4301 | ret = -EINVAL; | |
4302 | goto out; | |
4303 | } | |
4304 | ||
4305 | if (btrfs_root_readonly(root)) { | |
4306 | ret = -EROFS; | |
4307 | goto out; | |
4308 | } | |
4309 | ||
4310 | if (!inode_owner_or_capable(inode)) { | |
4311 | ret = -EACCES; | |
4312 | goto out; | |
4313 | } | |
4314 | ||
4315 | sa = memdup_user(arg, sizeof(*sa)); | |
4316 | if (IS_ERR(sa)) { | |
4317 | ret = PTR_ERR(sa); | |
4318 | sa = NULL; | |
4319 | goto out; | |
4320 | } | |
4321 | ||
4322 | trans = btrfs_start_transaction(root, 1); | |
4323 | if (IS_ERR(trans)) { | |
4324 | ret = PTR_ERR(trans); | |
4325 | trans = NULL; | |
4326 | goto out; | |
4327 | } | |
4328 | ||
4329 | sa->rtransid = trans->transid; | |
4330 | sa->rtime.sec = ct.tv_sec; | |
4331 | sa->rtime.nsec = ct.tv_nsec; | |
4332 | ||
4333 | memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); | |
4334 | btrfs_set_root_stransid(root_item, sa->stransid); | |
4335 | btrfs_set_root_rtransid(root_item, sa->rtransid); | |
3cae210f QW |
4336 | btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); |
4337 | btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); | |
4338 | btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); | |
4339 | btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); | |
8ea05e3a AB |
4340 | |
4341 | ret = btrfs_update_root(trans, root->fs_info->tree_root, | |
4342 | &root->root_key, &root->root_item); | |
4343 | if (ret < 0) { | |
4344 | btrfs_end_transaction(trans, root); | |
4345 | trans = NULL; | |
4346 | goto out; | |
4347 | } else { | |
4348 | ret = btrfs_commit_transaction(trans, root); | |
4349 | if (ret < 0) | |
4350 | goto out; | |
4351 | } | |
4352 | ||
4353 | ret = copy_to_user(arg, sa, sizeof(*sa)); | |
4354 | if (ret) | |
4355 | ret = -EFAULT; | |
4356 | ||
4357 | out: | |
4358 | kfree(sa); | |
4359 | up_write(&root->fs_info->subvol_sem); | |
4360 | mnt_drop_write_file(file); | |
4361 | return ret; | |
4362 | } | |
4363 | ||
867ab667 | 4364 | static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) |
4365 | { | |
6d0379ec | 4366 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
a1b83ac5 | 4367 | size_t len; |
867ab667 | 4368 | int ret; |
a1b83ac5 AJ |
4369 | char label[BTRFS_LABEL_SIZE]; |
4370 | ||
4371 | spin_lock(&root->fs_info->super_lock); | |
4372 | memcpy(label, root->fs_info->super_copy->label, BTRFS_LABEL_SIZE); | |
4373 | spin_unlock(&root->fs_info->super_lock); | |
4374 | ||
4375 | len = strnlen(label, BTRFS_LABEL_SIZE); | |
867ab667 | 4376 | |
4377 | if (len == BTRFS_LABEL_SIZE) { | |
4378 | pr_warn("btrfs: label is too long, return the first %zu bytes\n", | |
4379 | --len); | |
4380 | } | |
4381 | ||
867ab667 | 4382 | ret = copy_to_user(arg, label, len); |
867ab667 | 4383 | |
4384 | return ret ? -EFAULT : 0; | |
4385 | } | |
4386 | ||
a8bfd4ab | 4387 | static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) |
4388 | { | |
6d0379ec | 4389 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
a8bfd4ab | 4390 | struct btrfs_super_block *super_block = root->fs_info->super_copy; |
4391 | struct btrfs_trans_handle *trans; | |
4392 | char label[BTRFS_LABEL_SIZE]; | |
4393 | int ret; | |
4394 | ||
4395 | if (!capable(CAP_SYS_ADMIN)) | |
4396 | return -EPERM; | |
4397 | ||
4398 | if (copy_from_user(label, arg, sizeof(label))) | |
4399 | return -EFAULT; | |
4400 | ||
4401 | if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { | |
4402 | pr_err("btrfs: unable to set label with more than %d bytes\n", | |
4403 | BTRFS_LABEL_SIZE - 1); | |
4404 | return -EINVAL; | |
4405 | } | |
4406 | ||
4407 | ret = mnt_want_write_file(file); | |
4408 | if (ret) | |
4409 | return ret; | |
4410 | ||
a8bfd4ab | 4411 | trans = btrfs_start_transaction(root, 0); |
4412 | if (IS_ERR(trans)) { | |
4413 | ret = PTR_ERR(trans); | |
4414 | goto out_unlock; | |
4415 | } | |
4416 | ||
a1b83ac5 | 4417 | spin_lock(&root->fs_info->super_lock); |
a8bfd4ab | 4418 | strcpy(super_block->label, label); |
a1b83ac5 | 4419 | spin_unlock(&root->fs_info->super_lock); |
a8bfd4ab | 4420 | ret = btrfs_end_transaction(trans, root); |
4421 | ||
4422 | out_unlock: | |
a8bfd4ab | 4423 | mnt_drop_write_file(file); |
4424 | return ret; | |
4425 | } | |
4426 | ||
f46b5a66 CH |
4427 | long btrfs_ioctl(struct file *file, unsigned int |
4428 | cmd, unsigned long arg) | |
4429 | { | |
496ad9aa | 4430 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
4bcabaa3 | 4431 | void __user *argp = (void __user *)arg; |
f46b5a66 CH |
4432 | |
4433 | switch (cmd) { | |
6cbff00f CH |
4434 | case FS_IOC_GETFLAGS: |
4435 | return btrfs_ioctl_getflags(file, argp); | |
4436 | case FS_IOC_SETFLAGS: | |
4437 | return btrfs_ioctl_setflags(file, argp); | |
4438 | case FS_IOC_GETVERSION: | |
4439 | return btrfs_ioctl_getversion(file, argp); | |
f7039b1d LD |
4440 | case FITRIM: |
4441 | return btrfs_ioctl_fitrim(file, argp); | |
f46b5a66 | 4442 | case BTRFS_IOC_SNAP_CREATE: |
fa0d2b9b | 4443 | return btrfs_ioctl_snap_create(file, argp, 0); |
fdfb1e4f | 4444 | case BTRFS_IOC_SNAP_CREATE_V2: |
fa0d2b9b | 4445 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
3de4586c | 4446 | case BTRFS_IOC_SUBVOL_CREATE: |
fa0d2b9b | 4447 | return btrfs_ioctl_snap_create(file, argp, 1); |
6f72c7e2 AJ |
4448 | case BTRFS_IOC_SUBVOL_CREATE_V2: |
4449 | return btrfs_ioctl_snap_create_v2(file, argp, 1); | |
76dda93c YZ |
4450 | case BTRFS_IOC_SNAP_DESTROY: |
4451 | return btrfs_ioctl_snap_destroy(file, argp); | |
0caa102d LZ |
4452 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
4453 | return btrfs_ioctl_subvol_getflags(file, argp); | |
4454 | case BTRFS_IOC_SUBVOL_SETFLAGS: | |
4455 | return btrfs_ioctl_subvol_setflags(file, argp); | |
6ef5ed0d JB |
4456 | case BTRFS_IOC_DEFAULT_SUBVOL: |
4457 | return btrfs_ioctl_default_subvol(file, argp); | |
f46b5a66 | 4458 | case BTRFS_IOC_DEFRAG: |
1e701a32 CM |
4459 | return btrfs_ioctl_defrag(file, NULL); |
4460 | case BTRFS_IOC_DEFRAG_RANGE: | |
4461 | return btrfs_ioctl_defrag(file, argp); | |
f46b5a66 | 4462 | case BTRFS_IOC_RESIZE: |
198605a8 | 4463 | return btrfs_ioctl_resize(file, argp); |
f46b5a66 | 4464 | case BTRFS_IOC_ADD_DEV: |
4bcabaa3 | 4465 | return btrfs_ioctl_add_dev(root, argp); |
f46b5a66 | 4466 | case BTRFS_IOC_RM_DEV: |
da24927b | 4467 | return btrfs_ioctl_rm_dev(file, argp); |
475f6387 JS |
4468 | case BTRFS_IOC_FS_INFO: |
4469 | return btrfs_ioctl_fs_info(root, argp); | |
4470 | case BTRFS_IOC_DEV_INFO: | |
4471 | return btrfs_ioctl_dev_info(root, argp); | |
f46b5a66 | 4472 | case BTRFS_IOC_BALANCE: |
9ba1f6e4 | 4473 | return btrfs_ioctl_balance(file, NULL); |
f46b5a66 | 4474 | case BTRFS_IOC_CLONE: |
c5c9cd4d SW |
4475 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
4476 | case BTRFS_IOC_CLONE_RANGE: | |
7a865e8a | 4477 | return btrfs_ioctl_clone_range(file, argp); |
f46b5a66 CH |
4478 | case BTRFS_IOC_TRANS_START: |
4479 | return btrfs_ioctl_trans_start(file); | |
4480 | case BTRFS_IOC_TRANS_END: | |
4481 | return btrfs_ioctl_trans_end(file); | |
ac8e9819 CM |
4482 | case BTRFS_IOC_TREE_SEARCH: |
4483 | return btrfs_ioctl_tree_search(file, argp); | |
4484 | case BTRFS_IOC_INO_LOOKUP: | |
4485 | return btrfs_ioctl_ino_lookup(file, argp); | |
d7728c96 JS |
4486 | case BTRFS_IOC_INO_PATHS: |
4487 | return btrfs_ioctl_ino_to_path(root, argp); | |
4488 | case BTRFS_IOC_LOGICAL_INO: | |
4489 | return btrfs_ioctl_logical_to_ino(root, argp); | |
1406e432 JB |
4490 | case BTRFS_IOC_SPACE_INFO: |
4491 | return btrfs_ioctl_space_info(root, argp); | |
f46b5a66 CH |
4492 | case BTRFS_IOC_SYNC: |
4493 | btrfs_sync_fs(file->f_dentry->d_sb, 1); | |
4494 | return 0; | |
46204592 | 4495 | case BTRFS_IOC_START_SYNC: |
9a8c28be | 4496 | return btrfs_ioctl_start_sync(root, argp); |
46204592 | 4497 | case BTRFS_IOC_WAIT_SYNC: |
9a8c28be | 4498 | return btrfs_ioctl_wait_sync(root, argp); |
475f6387 | 4499 | case BTRFS_IOC_SCRUB: |
b8e95489 | 4500 | return btrfs_ioctl_scrub(file, argp); |
475f6387 JS |
4501 | case BTRFS_IOC_SCRUB_CANCEL: |
4502 | return btrfs_ioctl_scrub_cancel(root, argp); | |
4503 | case BTRFS_IOC_SCRUB_PROGRESS: | |
4504 | return btrfs_ioctl_scrub_progress(root, argp); | |
c9e9f97b | 4505 | case BTRFS_IOC_BALANCE_V2: |
9ba1f6e4 | 4506 | return btrfs_ioctl_balance(file, argp); |
837d5b6e ID |
4507 | case BTRFS_IOC_BALANCE_CTL: |
4508 | return btrfs_ioctl_balance_ctl(root, arg); | |
19a39dce ID |
4509 | case BTRFS_IOC_BALANCE_PROGRESS: |
4510 | return btrfs_ioctl_balance_progress(root, argp); | |
8ea05e3a AB |
4511 | case BTRFS_IOC_SET_RECEIVED_SUBVOL: |
4512 | return btrfs_ioctl_set_received_subvol(file, argp); | |
31db9f7c AB |
4513 | case BTRFS_IOC_SEND: |
4514 | return btrfs_ioctl_send(file, argp); | |
c11d2c23 | 4515 | case BTRFS_IOC_GET_DEV_STATS: |
b27f7c0c | 4516 | return btrfs_ioctl_get_dev_stats(root, argp); |
5d13a37b | 4517 | case BTRFS_IOC_QUOTA_CTL: |
905b0dda | 4518 | return btrfs_ioctl_quota_ctl(file, argp); |
5d13a37b | 4519 | case BTRFS_IOC_QGROUP_ASSIGN: |
905b0dda | 4520 | return btrfs_ioctl_qgroup_assign(file, argp); |
5d13a37b | 4521 | case BTRFS_IOC_QGROUP_CREATE: |
905b0dda | 4522 | return btrfs_ioctl_qgroup_create(file, argp); |
5d13a37b | 4523 | case BTRFS_IOC_QGROUP_LIMIT: |
905b0dda | 4524 | return btrfs_ioctl_qgroup_limit(file, argp); |
2f232036 JS |
4525 | case BTRFS_IOC_QUOTA_RESCAN: |
4526 | return btrfs_ioctl_quota_rescan(file, argp); | |
4527 | case BTRFS_IOC_QUOTA_RESCAN_STATUS: | |
4528 | return btrfs_ioctl_quota_rescan_status(file, argp); | |
57254b6e JS |
4529 | case BTRFS_IOC_QUOTA_RESCAN_WAIT: |
4530 | return btrfs_ioctl_quota_rescan_wait(file, argp); | |
3f6bcfbd SB |
4531 | case BTRFS_IOC_DEV_REPLACE: |
4532 | return btrfs_ioctl_dev_replace(root, argp); | |
867ab667 | 4533 | case BTRFS_IOC_GET_FSLABEL: |
4534 | return btrfs_ioctl_get_fslabel(file, argp); | |
a8bfd4ab | 4535 | case BTRFS_IOC_SET_FSLABEL: |
4536 | return btrfs_ioctl_set_fslabel(file, argp); | |
416161db MF |
4537 | case BTRFS_IOC_FILE_EXTENT_SAME: |
4538 | return btrfs_ioctl_file_extent_same(file, argp); | |
f46b5a66 CH |
4539 | } |
4540 | ||
4541 | return -ENOTTY; | |
4542 | } |