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