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