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