]>
Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
f46b5a66 CH |
2 | /* |
3 | * Copyright (C) 2007 Oracle. All rights reserved. | |
f46b5a66 CH |
4 | */ |
5 | ||
6 | #include <linux/kernel.h> | |
7 | #include <linux/bio.h> | |
f46b5a66 CH |
8 | #include <linux/file.h> |
9 | #include <linux/fs.h> | |
cb8e7090 | 10 | #include <linux/fsnotify.h> |
f46b5a66 CH |
11 | #include <linux/pagemap.h> |
12 | #include <linux/highmem.h> | |
13 | #include <linux/time.h> | |
f46b5a66 | 14 | #include <linux/string.h> |
f46b5a66 | 15 | #include <linux/backing-dev.h> |
cb8e7090 | 16 | #include <linux/mount.h> |
cb8e7090 | 17 | #include <linux/namei.h> |
f46b5a66 | 18 | #include <linux/writeback.h> |
f46b5a66 | 19 | #include <linux/compat.h> |
cb8e7090 | 20 | #include <linux/security.h> |
f46b5a66 | 21 | #include <linux/xattr.h> |
f54de068 | 22 | #include <linux/mm.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
f7039b1d | 24 | #include <linux/blkdev.h> |
8ea05e3a | 25 | #include <linux/uuid.h> |
55e301fd | 26 | #include <linux/btrfs.h> |
416161db | 27 | #include <linux/uaccess.h> |
ae5e165d | 28 | #include <linux/iversion.h> |
f46b5a66 CH |
29 | #include "ctree.h" |
30 | #include "disk-io.h" | |
31 | #include "transaction.h" | |
32 | #include "btrfs_inode.h" | |
f46b5a66 CH |
33 | #include "print-tree.h" |
34 | #include "volumes.h" | |
925baedd | 35 | #include "locking.h" |
581bb050 | 36 | #include "inode-map.h" |
d7728c96 | 37 | #include "backref.h" |
606686ee | 38 | #include "rcu-string.h" |
31db9f7c | 39 | #include "send.h" |
3f6bcfbd | 40 | #include "dev-replace.h" |
63541927 | 41 | #include "props.h" |
3b02a68a | 42 | #include "sysfs.h" |
fcebe456 | 43 | #include "qgroup.h" |
1ec9a1ae | 44 | #include "tree-log.h" |
ebb8765b | 45 | #include "compression.h" |
f46b5a66 | 46 | |
abccd00f HM |
47 | #ifdef CONFIG_64BIT |
48 | /* If we have a 32-bit userspace and 64-bit kernel, then the UAPI | |
49 | * structures are incorrect, as the timespec structure from userspace | |
50 | * is 4 bytes too small. We define these alternatives here to teach | |
51 | * the kernel about the 32-bit struct packing. | |
52 | */ | |
53 | struct btrfs_ioctl_timespec_32 { | |
54 | __u64 sec; | |
55 | __u32 nsec; | |
56 | } __attribute__ ((__packed__)); | |
57 | ||
58 | struct btrfs_ioctl_received_subvol_args_32 { | |
59 | char uuid[BTRFS_UUID_SIZE]; /* in */ | |
60 | __u64 stransid; /* in */ | |
61 | __u64 rtransid; /* out */ | |
62 | struct btrfs_ioctl_timespec_32 stime; /* in */ | |
63 | struct btrfs_ioctl_timespec_32 rtime; /* out */ | |
64 | __u64 flags; /* in */ | |
65 | __u64 reserved[16]; /* in */ | |
66 | } __attribute__ ((__packed__)); | |
67 | ||
68 | #define BTRFS_IOC_SET_RECEIVED_SUBVOL_32 _IOWR(BTRFS_IOCTL_MAGIC, 37, \ | |
69 | struct btrfs_ioctl_received_subvol_args_32) | |
70 | #endif | |
71 | ||
2351f431 JB |
72 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) |
73 | struct btrfs_ioctl_send_args_32 { | |
74 | __s64 send_fd; /* in */ | |
75 | __u64 clone_sources_count; /* in */ | |
76 | compat_uptr_t clone_sources; /* in */ | |
77 | __u64 parent_root; /* in */ | |
78 | __u64 flags; /* in */ | |
79 | __u64 reserved[4]; /* in */ | |
80 | } __attribute__ ((__packed__)); | |
81 | ||
82 | #define BTRFS_IOC_SEND_32 _IOW(BTRFS_IOCTL_MAGIC, 38, \ | |
83 | struct btrfs_ioctl_send_args_32) | |
84 | #endif | |
abccd00f | 85 | |
416161db | 86 | static int btrfs_clone(struct inode *src, struct inode *inode, |
1c919a5e MF |
87 | u64 off, u64 olen, u64 olen_aligned, u64 destoff, |
88 | int no_time_update); | |
416161db | 89 | |
6cbff00f | 90 | /* Mask out flags that are inappropriate for the given type of inode. */ |
1905a0f7 DS |
91 | static unsigned int btrfs_mask_fsflags_for_type(struct inode *inode, |
92 | unsigned int flags) | |
6cbff00f | 93 | { |
1905a0f7 | 94 | if (S_ISDIR(inode->i_mode)) |
6cbff00f | 95 | return flags; |
1905a0f7 | 96 | else if (S_ISREG(inode->i_mode)) |
6cbff00f CH |
97 | return flags & ~FS_DIRSYNC_FL; |
98 | else | |
99 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); | |
100 | } | |
101 | ||
102 | /* | |
a157d4fd DS |
103 | * Export internal inode flags to the format expected by the FS_IOC_GETFLAGS |
104 | * ioctl. | |
6cbff00f | 105 | */ |
a157d4fd | 106 | static unsigned int btrfs_inode_flags_to_fsflags(unsigned int flags) |
6cbff00f CH |
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 | ||
13f48dc9 | 125 | if (flags & BTRFS_INODE_NOCOMPRESS) |
d0092bdd | 126 | iflags |= FS_NOCOMP_FL; |
13f48dc9 ST |
127 | else if (flags & BTRFS_INODE_COMPRESS) |
128 | iflags |= FS_COMPR_FL; | |
6cbff00f CH |
129 | |
130 | return iflags; | |
131 | } | |
132 | ||
133 | /* | |
134 | * Update inode->i_flags based on the btrfs internal flags. | |
135 | */ | |
7b6a221e | 136 | void btrfs_sync_inode_flags_to_i_flags(struct inode *inode) |
6cbff00f | 137 | { |
5c57b8b6 | 138 | struct btrfs_inode *binode = BTRFS_I(inode); |
3cc79392 | 139 | unsigned int new_fl = 0; |
6cbff00f | 140 | |
5c57b8b6 | 141 | if (binode->flags & BTRFS_INODE_SYNC) |
3cc79392 | 142 | new_fl |= S_SYNC; |
5c57b8b6 | 143 | if (binode->flags & BTRFS_INODE_IMMUTABLE) |
3cc79392 | 144 | new_fl |= S_IMMUTABLE; |
5c57b8b6 | 145 | if (binode->flags & BTRFS_INODE_APPEND) |
3cc79392 | 146 | new_fl |= S_APPEND; |
5c57b8b6 | 147 | if (binode->flags & BTRFS_INODE_NOATIME) |
3cc79392 | 148 | new_fl |= S_NOATIME; |
5c57b8b6 | 149 | if (binode->flags & BTRFS_INODE_DIRSYNC) |
3cc79392 FM |
150 | new_fl |= S_DIRSYNC; |
151 | ||
152 | set_mask_bits(&inode->i_flags, | |
153 | S_SYNC | S_APPEND | S_IMMUTABLE | S_NOATIME | S_DIRSYNC, | |
154 | new_fl); | |
6cbff00f CH |
155 | } |
156 | ||
6cbff00f CH |
157 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) |
158 | { | |
5c57b8b6 DS |
159 | struct btrfs_inode *binode = BTRFS_I(file_inode(file)); |
160 | unsigned int flags = btrfs_inode_flags_to_fsflags(binode->flags); | |
6cbff00f CH |
161 | |
162 | if (copy_to_user(arg, &flags, sizeof(flags))) | |
163 | return -EFAULT; | |
164 | return 0; | |
165 | } | |
166 | ||
5ba76abf DS |
167 | /* Check if @flags are a supported and valid set of FS_*_FL flags */ |
168 | static int check_fsflags(unsigned int flags) | |
75e7cb7f LB |
169 | { |
170 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ | |
171 | FS_NOATIME_FL | FS_NODUMP_FL | \ | |
172 | FS_SYNC_FL | FS_DIRSYNC_FL | \ | |
e1e8fb6a LZ |
173 | FS_NOCOMP_FL | FS_COMPR_FL | |
174 | FS_NOCOW_FL)) | |
75e7cb7f LB |
175 | return -EOPNOTSUPP; |
176 | ||
177 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) | |
178 | return -EINVAL; | |
179 | ||
75e7cb7f LB |
180 | return 0; |
181 | } | |
182 | ||
6cbff00f CH |
183 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) |
184 | { | |
496ad9aa | 185 | struct inode *inode = file_inode(file); |
0b246afa | 186 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
5c57b8b6 DS |
187 | struct btrfs_inode *binode = BTRFS_I(inode); |
188 | struct btrfs_root *root = binode->root; | |
6cbff00f | 189 | struct btrfs_trans_handle *trans; |
5c57b8b6 | 190 | unsigned int fsflags, old_fsflags; |
6cbff00f | 191 | int ret; |
5c57b8b6 DS |
192 | u64 old_flags; |
193 | unsigned int old_i_flags; | |
7e97b8da | 194 | umode_t mode; |
6cbff00f | 195 | |
bd60ea0f DS |
196 | if (!inode_owner_or_capable(inode)) |
197 | return -EPERM; | |
198 | ||
b83cc969 LZ |
199 | if (btrfs_root_readonly(root)) |
200 | return -EROFS; | |
201 | ||
5c57b8b6 | 202 | if (copy_from_user(&fsflags, arg, sizeof(fsflags))) |
6cbff00f CH |
203 | return -EFAULT; |
204 | ||
5c57b8b6 | 205 | ret = check_fsflags(fsflags); |
75e7cb7f LB |
206 | if (ret) |
207 | return ret; | |
f46b5a66 | 208 | |
e7848683 JK |
209 | ret = mnt_want_write_file(file); |
210 | if (ret) | |
211 | return ret; | |
212 | ||
5955102c | 213 | inode_lock(inode); |
6cbff00f | 214 | |
5c57b8b6 DS |
215 | old_flags = binode->flags; |
216 | old_i_flags = inode->i_flags; | |
7e97b8da | 217 | mode = inode->i_mode; |
f062abf0 | 218 | |
5c57b8b6 DS |
219 | fsflags = btrfs_mask_fsflags_for_type(inode, fsflags); |
220 | old_fsflags = btrfs_inode_flags_to_fsflags(binode->flags); | |
221 | if ((fsflags ^ old_fsflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { | |
6cbff00f CH |
222 | if (!capable(CAP_LINUX_IMMUTABLE)) { |
223 | ret = -EPERM; | |
224 | goto out_unlock; | |
225 | } | |
226 | } | |
227 | ||
5c57b8b6 DS |
228 | if (fsflags & FS_SYNC_FL) |
229 | binode->flags |= BTRFS_INODE_SYNC; | |
6cbff00f | 230 | else |
5c57b8b6 DS |
231 | binode->flags &= ~BTRFS_INODE_SYNC; |
232 | if (fsflags & FS_IMMUTABLE_FL) | |
233 | binode->flags |= BTRFS_INODE_IMMUTABLE; | |
6cbff00f | 234 | else |
5c57b8b6 DS |
235 | binode->flags &= ~BTRFS_INODE_IMMUTABLE; |
236 | if (fsflags & FS_APPEND_FL) | |
237 | binode->flags |= BTRFS_INODE_APPEND; | |
6cbff00f | 238 | else |
5c57b8b6 DS |
239 | binode->flags &= ~BTRFS_INODE_APPEND; |
240 | if (fsflags & FS_NODUMP_FL) | |
241 | binode->flags |= BTRFS_INODE_NODUMP; | |
6cbff00f | 242 | else |
5c57b8b6 DS |
243 | binode->flags &= ~BTRFS_INODE_NODUMP; |
244 | if (fsflags & FS_NOATIME_FL) | |
245 | binode->flags |= BTRFS_INODE_NOATIME; | |
6cbff00f | 246 | else |
5c57b8b6 DS |
247 | binode->flags &= ~BTRFS_INODE_NOATIME; |
248 | if (fsflags & FS_DIRSYNC_FL) | |
249 | binode->flags |= BTRFS_INODE_DIRSYNC; | |
6cbff00f | 250 | else |
5c57b8b6 DS |
251 | binode->flags &= ~BTRFS_INODE_DIRSYNC; |
252 | if (fsflags & FS_NOCOW_FL) { | |
7e97b8da DS |
253 | if (S_ISREG(mode)) { |
254 | /* | |
255 | * It's safe to turn csums off here, no extents exist. | |
256 | * Otherwise we want the flag to reflect the real COW | |
257 | * status of the file and will not set it. | |
258 | */ | |
259 | if (inode->i_size == 0) | |
5c57b8b6 DS |
260 | binode->flags |= BTRFS_INODE_NODATACOW |
261 | | BTRFS_INODE_NODATASUM; | |
7e97b8da | 262 | } else { |
5c57b8b6 | 263 | binode->flags |= BTRFS_INODE_NODATACOW; |
7e97b8da DS |
264 | } |
265 | } else { | |
266 | /* | |
01327610 | 267 | * Revert back under same assumptions as above |
7e97b8da DS |
268 | */ |
269 | if (S_ISREG(mode)) { | |
270 | if (inode->i_size == 0) | |
5c57b8b6 | 271 | binode->flags &= ~(BTRFS_INODE_NODATACOW |
7e97b8da DS |
272 | | BTRFS_INODE_NODATASUM); |
273 | } else { | |
5c57b8b6 | 274 | binode->flags &= ~BTRFS_INODE_NODATACOW; |
7e97b8da DS |
275 | } |
276 | } | |
6cbff00f | 277 | |
75e7cb7f LB |
278 | /* |
279 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS | |
280 | * flag may be changed automatically if compression code won't make | |
281 | * things smaller. | |
282 | */ | |
5c57b8b6 DS |
283 | if (fsflags & FS_NOCOMP_FL) { |
284 | binode->flags &= ~BTRFS_INODE_COMPRESS; | |
285 | binode->flags |= BTRFS_INODE_NOCOMPRESS; | |
63541927 FDBM |
286 | |
287 | ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); | |
288 | if (ret && ret != -ENODATA) | |
289 | goto out_drop; | |
5c57b8b6 | 290 | } else if (fsflags & FS_COMPR_FL) { |
63541927 FDBM |
291 | const char *comp; |
292 | ||
5c57b8b6 DS |
293 | binode->flags |= BTRFS_INODE_COMPRESS; |
294 | binode->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
63541927 | 295 | |
93370509 DS |
296 | comp = btrfs_compress_type2str(fs_info->compress_type); |
297 | if (!comp || comp[0] == 0) | |
298 | comp = btrfs_compress_type2str(BTRFS_COMPRESS_ZLIB); | |
299 | ||
63541927 FDBM |
300 | ret = btrfs_set_prop(inode, "btrfs.compression", |
301 | comp, strlen(comp), 0); | |
302 | if (ret) | |
303 | goto out_drop; | |
304 | ||
ebcb904d | 305 | } else { |
78a017a2 FM |
306 | ret = btrfs_set_prop(inode, "btrfs.compression", NULL, 0, 0); |
307 | if (ret && ret != -ENODATA) | |
308 | goto out_drop; | |
5c57b8b6 | 309 | binode->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); |
75e7cb7f | 310 | } |
6cbff00f | 311 | |
4da6f1a3 | 312 | trans = btrfs_start_transaction(root, 1); |
f062abf0 LZ |
313 | if (IS_ERR(trans)) { |
314 | ret = PTR_ERR(trans); | |
315 | goto out_drop; | |
316 | } | |
6cbff00f | 317 | |
7b6a221e | 318 | btrfs_sync_inode_flags_to_i_flags(inode); |
0c4d2d95 | 319 | inode_inc_iversion(inode); |
c2050a45 | 320 | inode->i_ctime = current_time(inode); |
6cbff00f | 321 | ret = btrfs_update_inode(trans, root, inode); |
6cbff00f | 322 | |
3a45bb20 | 323 | btrfs_end_transaction(trans); |
f062abf0 LZ |
324 | out_drop: |
325 | if (ret) { | |
5c57b8b6 DS |
326 | binode->flags = old_flags; |
327 | inode->i_flags = old_i_flags; | |
f062abf0 | 328 | } |
6cbff00f | 329 | |
6cbff00f | 330 | out_unlock: |
5955102c | 331 | inode_unlock(inode); |
e7848683 | 332 | mnt_drop_write_file(file); |
2d4e6f6a | 333 | return ret; |
6cbff00f CH |
334 | } |
335 | ||
19f93b3c DS |
336 | /* |
337 | * Translate btrfs internal inode flags to xflags as expected by the | |
338 | * FS_IOC_FSGETXATT ioctl. Filter only the supported ones, unknown flags are | |
339 | * silently dropped. | |
340 | */ | |
341 | static unsigned int btrfs_inode_flags_to_xflags(unsigned int flags) | |
342 | { | |
343 | unsigned int xflags = 0; | |
344 | ||
345 | if (flags & BTRFS_INODE_APPEND) | |
346 | xflags |= FS_XFLAG_APPEND; | |
347 | if (flags & BTRFS_INODE_IMMUTABLE) | |
348 | xflags |= FS_XFLAG_IMMUTABLE; | |
349 | if (flags & BTRFS_INODE_NOATIME) | |
350 | xflags |= FS_XFLAG_NOATIME; | |
351 | if (flags & BTRFS_INODE_NODUMP) | |
352 | xflags |= FS_XFLAG_NODUMP; | |
353 | if (flags & BTRFS_INODE_SYNC) | |
354 | xflags |= FS_XFLAG_SYNC; | |
355 | ||
356 | return xflags; | |
357 | } | |
358 | ||
359 | /* Check if @flags are a supported and valid set of FS_XFLAGS_* flags */ | |
360 | static int check_xflags(unsigned int flags) | |
361 | { | |
362 | if (flags & ~(FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE | FS_XFLAG_NOATIME | | |
363 | FS_XFLAG_NODUMP | FS_XFLAG_SYNC)) | |
364 | return -EOPNOTSUPP; | |
365 | return 0; | |
366 | } | |
367 | ||
e4202ac9 DS |
368 | /* |
369 | * Set the xflags from the internal inode flags. The remaining items of fsxattr | |
370 | * are zeroed. | |
371 | */ | |
372 | static int btrfs_ioctl_fsgetxattr(struct file *file, void __user *arg) | |
373 | { | |
374 | struct btrfs_inode *binode = BTRFS_I(file_inode(file)); | |
375 | struct fsxattr fa; | |
376 | ||
377 | memset(&fa, 0, sizeof(fa)); | |
378 | fa.fsx_xflags = btrfs_inode_flags_to_xflags(binode->flags); | |
379 | ||
380 | if (copy_to_user(arg, &fa, sizeof(fa))) | |
381 | return -EFAULT; | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
025f2121 DS |
386 | static int btrfs_ioctl_fssetxattr(struct file *file, void __user *arg) |
387 | { | |
388 | struct inode *inode = file_inode(file); | |
389 | struct btrfs_inode *binode = BTRFS_I(inode); | |
390 | struct btrfs_root *root = binode->root; | |
391 | struct btrfs_trans_handle *trans; | |
392 | struct fsxattr fa; | |
393 | unsigned old_flags; | |
394 | unsigned old_i_flags; | |
395 | int ret = 0; | |
396 | ||
397 | if (!inode_owner_or_capable(inode)) | |
398 | return -EPERM; | |
399 | ||
400 | if (btrfs_root_readonly(root)) | |
401 | return -EROFS; | |
402 | ||
403 | memset(&fa, 0, sizeof(fa)); | |
404 | if (copy_from_user(&fa, arg, sizeof(fa))) | |
405 | return -EFAULT; | |
406 | ||
407 | ret = check_xflags(fa.fsx_xflags); | |
408 | if (ret) | |
409 | return ret; | |
410 | ||
411 | if (fa.fsx_extsize != 0 || fa.fsx_projid != 0 || fa.fsx_cowextsize != 0) | |
412 | return -EOPNOTSUPP; | |
413 | ||
414 | ret = mnt_want_write_file(file); | |
415 | if (ret) | |
416 | return ret; | |
417 | ||
418 | inode_lock(inode); | |
419 | ||
420 | old_flags = binode->flags; | |
421 | old_i_flags = inode->i_flags; | |
422 | ||
423 | /* We need the capabilities to change append-only or immutable inode */ | |
424 | if (((old_flags & (BTRFS_INODE_APPEND | BTRFS_INODE_IMMUTABLE)) || | |
425 | (fa.fsx_xflags & (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE))) && | |
426 | !capable(CAP_LINUX_IMMUTABLE)) { | |
427 | ret = -EPERM; | |
428 | goto out_unlock; | |
429 | } | |
430 | ||
431 | if (fa.fsx_xflags & FS_XFLAG_SYNC) | |
432 | binode->flags |= BTRFS_INODE_SYNC; | |
433 | else | |
434 | binode->flags &= ~BTRFS_INODE_SYNC; | |
435 | if (fa.fsx_xflags & FS_XFLAG_IMMUTABLE) | |
436 | binode->flags |= BTRFS_INODE_IMMUTABLE; | |
437 | else | |
438 | binode->flags &= ~BTRFS_INODE_IMMUTABLE; | |
439 | if (fa.fsx_xflags & FS_XFLAG_APPEND) | |
440 | binode->flags |= BTRFS_INODE_APPEND; | |
441 | else | |
442 | binode->flags &= ~BTRFS_INODE_APPEND; | |
443 | if (fa.fsx_xflags & FS_XFLAG_NODUMP) | |
444 | binode->flags |= BTRFS_INODE_NODUMP; | |
445 | else | |
446 | binode->flags &= ~BTRFS_INODE_NODUMP; | |
447 | if (fa.fsx_xflags & FS_XFLAG_NOATIME) | |
448 | binode->flags |= BTRFS_INODE_NOATIME; | |
449 | else | |
450 | binode->flags &= ~BTRFS_INODE_NOATIME; | |
451 | ||
452 | /* 1 item for the inode */ | |
453 | trans = btrfs_start_transaction(root, 1); | |
454 | if (IS_ERR(trans)) { | |
455 | ret = PTR_ERR(trans); | |
456 | goto out_unlock; | |
457 | } | |
458 | ||
459 | btrfs_sync_inode_flags_to_i_flags(inode); | |
460 | inode_inc_iversion(inode); | |
461 | inode->i_ctime = current_time(inode); | |
462 | ret = btrfs_update_inode(trans, root, inode); | |
463 | ||
464 | btrfs_end_transaction(trans); | |
465 | ||
466 | out_unlock: | |
467 | if (ret) { | |
468 | binode->flags = old_flags; | |
469 | inode->i_flags = old_i_flags; | |
470 | } | |
471 | ||
472 | inode_unlock(inode); | |
473 | mnt_drop_write_file(file); | |
474 | ||
475 | return ret; | |
476 | } | |
477 | ||
6cbff00f CH |
478 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) |
479 | { | |
496ad9aa | 480 | struct inode *inode = file_inode(file); |
6cbff00f CH |
481 | |
482 | return put_user(inode->i_generation, arg); | |
483 | } | |
f46b5a66 | 484 | |
f7039b1d LD |
485 | static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) |
486 | { | |
0b246afa JM |
487 | struct inode *inode = file_inode(file); |
488 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
f7039b1d LD |
489 | struct btrfs_device *device; |
490 | struct request_queue *q; | |
491 | struct fstrim_range range; | |
492 | u64 minlen = ULLONG_MAX; | |
493 | u64 num_devices = 0; | |
494 | int ret; | |
495 | ||
496 | if (!capable(CAP_SYS_ADMIN)) | |
497 | return -EPERM; | |
498 | ||
1f78160c XG |
499 | rcu_read_lock(); |
500 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, | |
501 | dev_list) { | |
f7039b1d LD |
502 | if (!device->bdev) |
503 | continue; | |
504 | q = bdev_get_queue(device->bdev); | |
505 | if (blk_queue_discard(q)) { | |
506 | num_devices++; | |
50d0446e | 507 | minlen = min_t(u64, q->limits.discard_granularity, |
f7039b1d LD |
508 | minlen); |
509 | } | |
510 | } | |
1f78160c | 511 | rcu_read_unlock(); |
f4c697e6 | 512 | |
f7039b1d LD |
513 | if (!num_devices) |
514 | return -EOPNOTSUPP; | |
f7039b1d LD |
515 | if (copy_from_user(&range, arg, sizeof(range))) |
516 | return -EFAULT; | |
6ba9fc8e QW |
517 | |
518 | /* | |
519 | * NOTE: Don't truncate the range using super->total_bytes. Bytenr of | |
520 | * block group is in the logical address space, which can be any | |
521 | * sectorsize aligned bytenr in the range [0, U64_MAX]. | |
522 | */ | |
523 | if (range.len < fs_info->sb->s_blocksize) | |
f4c697e6 | 524 | return -EINVAL; |
f7039b1d LD |
525 | |
526 | range.minlen = max(range.minlen, minlen); | |
2ff7e61e | 527 | ret = btrfs_trim_fs(fs_info, &range); |
f7039b1d LD |
528 | if (ret < 0) |
529 | return ret; | |
530 | ||
531 | if (copy_to_user(arg, &range, sizeof(range))) | |
532 | return -EFAULT; | |
533 | ||
534 | return 0; | |
535 | } | |
536 | ||
dd5f9615 SB |
537 | int btrfs_is_empty_uuid(u8 *uuid) |
538 | { | |
46e0f66a CM |
539 | int i; |
540 | ||
541 | for (i = 0; i < BTRFS_UUID_SIZE; i++) { | |
542 | if (uuid[i]) | |
543 | return 0; | |
544 | } | |
545 | return 1; | |
dd5f9615 SB |
546 | } |
547 | ||
d5c12070 | 548 | static noinline int create_subvol(struct inode *dir, |
cb8e7090 | 549 | struct dentry *dentry, |
52f75f4f | 550 | const char *name, int namelen, |
6f72c7e2 | 551 | u64 *async_transid, |
8696c533 | 552 | struct btrfs_qgroup_inherit *inherit) |
f46b5a66 | 553 | { |
0b246afa | 554 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
f46b5a66 CH |
555 | struct btrfs_trans_handle *trans; |
556 | struct btrfs_key key; | |
49a3c4d9 | 557 | struct btrfs_root_item *root_item; |
f46b5a66 CH |
558 | struct btrfs_inode_item *inode_item; |
559 | struct extent_buffer *leaf; | |
d5c12070 | 560 | struct btrfs_root *root = BTRFS_I(dir)->root; |
76dda93c | 561 | struct btrfs_root *new_root; |
d5c12070 | 562 | struct btrfs_block_rsv block_rsv; |
95582b00 | 563 | struct timespec64 cur_time = current_time(dir); |
5662344b | 564 | struct inode *inode; |
f46b5a66 CH |
565 | int ret; |
566 | int err; | |
567 | u64 objectid; | |
568 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | |
3de4586c | 569 | u64 index = 0; |
8ea05e3a | 570 | uuid_le new_uuid; |
f46b5a66 | 571 | |
49a3c4d9 DS |
572 | root_item = kzalloc(sizeof(*root_item), GFP_KERNEL); |
573 | if (!root_item) | |
574 | return -ENOMEM; | |
575 | ||
0b246afa | 576 | ret = btrfs_find_free_objectid(fs_info->tree_root, &objectid); |
2fbe8c8a | 577 | if (ret) |
49a3c4d9 | 578 | goto fail_free; |
6a912213 | 579 | |
e09fe2d2 QW |
580 | /* |
581 | * Don't create subvolume whose level is not zero. Or qgroup will be | |
01327610 | 582 | * screwed up since it assumes subvolume qgroup's level to be 0. |
e09fe2d2 | 583 | */ |
49a3c4d9 DS |
584 | if (btrfs_qgroup_level(objectid)) { |
585 | ret = -ENOSPC; | |
586 | goto fail_free; | |
587 | } | |
e09fe2d2 | 588 | |
d5c12070 | 589 | btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP); |
9ed74f2d | 590 | /* |
d5c12070 MX |
591 | * The same as the snapshot creation, please see the comment |
592 | * of create_snapshot(). | |
9ed74f2d | 593 | */ |
c4c129db | 594 | ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 8, false); |
d5c12070 | 595 | if (ret) |
49a3c4d9 | 596 | goto fail_free; |
d5c12070 MX |
597 | |
598 | trans = btrfs_start_transaction(root, 0); | |
599 | if (IS_ERR(trans)) { | |
600 | ret = PTR_ERR(trans); | |
7775c818 | 601 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); |
49a3c4d9 | 602 | goto fail_free; |
d5c12070 MX |
603 | } |
604 | trans->block_rsv = &block_rsv; | |
605 | trans->bytes_reserved = block_rsv.size; | |
f46b5a66 | 606 | |
a9377422 | 607 | ret = btrfs_qgroup_inherit(trans, 0, objectid, inherit); |
6f72c7e2 AJ |
608 | if (ret) |
609 | goto fail; | |
610 | ||
4d75f8a9 | 611 | leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0); |
8e8a1e31 JB |
612 | if (IS_ERR(leaf)) { |
613 | ret = PTR_ERR(leaf); | |
614 | goto fail; | |
615 | } | |
f46b5a66 | 616 | |
f46b5a66 CH |
617 | btrfs_mark_buffer_dirty(leaf); |
618 | ||
49a3c4d9 | 619 | inode_item = &root_item->inode; |
3cae210f QW |
620 | btrfs_set_stack_inode_generation(inode_item, 1); |
621 | btrfs_set_stack_inode_size(inode_item, 3); | |
622 | btrfs_set_stack_inode_nlink(inode_item, 1); | |
da17066c | 623 | btrfs_set_stack_inode_nbytes(inode_item, |
0b246afa | 624 | fs_info->nodesize); |
3cae210f | 625 | btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755); |
f46b5a66 | 626 | |
49a3c4d9 DS |
627 | btrfs_set_root_flags(root_item, 0); |
628 | btrfs_set_root_limit(root_item, 0); | |
3cae210f | 629 | btrfs_set_stack_inode_flags(inode_item, BTRFS_INODE_ROOT_ITEM_INIT); |
08fe4db1 | 630 | |
49a3c4d9 DS |
631 | btrfs_set_root_bytenr(root_item, leaf->start); |
632 | btrfs_set_root_generation(root_item, trans->transid); | |
633 | btrfs_set_root_level(root_item, 0); | |
634 | btrfs_set_root_refs(root_item, 1); | |
635 | btrfs_set_root_used(root_item, leaf->len); | |
636 | btrfs_set_root_last_snapshot(root_item, 0); | |
f46b5a66 | 637 | |
49a3c4d9 DS |
638 | btrfs_set_root_generation_v2(root_item, |
639 | btrfs_root_generation(root_item)); | |
8ea05e3a | 640 | uuid_le_gen(&new_uuid); |
49a3c4d9 DS |
641 | memcpy(root_item->uuid, new_uuid.b, BTRFS_UUID_SIZE); |
642 | btrfs_set_stack_timespec_sec(&root_item->otime, cur_time.tv_sec); | |
643 | btrfs_set_stack_timespec_nsec(&root_item->otime, cur_time.tv_nsec); | |
644 | root_item->ctime = root_item->otime; | |
645 | btrfs_set_root_ctransid(root_item, trans->transid); | |
646 | btrfs_set_root_otransid(root_item, trans->transid); | |
f46b5a66 | 647 | |
925baedd | 648 | btrfs_tree_unlock(leaf); |
f46b5a66 CH |
649 | free_extent_buffer(leaf); |
650 | leaf = NULL; | |
651 | ||
49a3c4d9 | 652 | btrfs_set_root_dirid(root_item, new_dirid); |
f46b5a66 CH |
653 | |
654 | key.objectid = objectid; | |
5d4f98a2 | 655 | key.offset = 0; |
962a298f | 656 | key.type = BTRFS_ROOT_ITEM_KEY; |
0b246afa | 657 | ret = btrfs_insert_root(trans, fs_info->tree_root, &key, |
49a3c4d9 | 658 | root_item); |
f46b5a66 CH |
659 | if (ret) |
660 | goto fail; | |
661 | ||
76dda93c | 662 | key.offset = (u64)-1; |
0b246afa | 663 | new_root = btrfs_read_fs_root_no_name(fs_info, &key); |
79787eaa | 664 | if (IS_ERR(new_root)) { |
79787eaa | 665 | ret = PTR_ERR(new_root); |
66642832 | 666 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
667 | goto fail; |
668 | } | |
76dda93c YZ |
669 | |
670 | btrfs_record_root_in_trans(trans, new_root); | |
671 | ||
63541927 | 672 | ret = btrfs_create_subvol_root(trans, new_root, root, new_dirid); |
ce598979 MF |
673 | if (ret) { |
674 | /* We potentially lose an unused inode item here */ | |
66642832 | 675 | btrfs_abort_transaction(trans, ret); |
ce598979 MF |
676 | goto fail; |
677 | } | |
678 | ||
f32e48e9 CR |
679 | mutex_lock(&new_root->objectid_mutex); |
680 | new_root->highest_objectid = new_dirid; | |
681 | mutex_unlock(&new_root->objectid_mutex); | |
682 | ||
f46b5a66 CH |
683 | /* |
684 | * insert the directory item | |
685 | */ | |
877574e2 | 686 | ret = btrfs_set_inode_index(BTRFS_I(dir), &index); |
79787eaa | 687 | if (ret) { |
66642832 | 688 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
689 | goto fail; |
690 | } | |
3de4586c | 691 | |
684572df | 692 | ret = btrfs_insert_dir_item(trans, name, namelen, BTRFS_I(dir), &key, |
3de4586c | 693 | BTRFS_FT_DIR, index); |
79787eaa | 694 | if (ret) { |
66642832 | 695 | btrfs_abort_transaction(trans, ret); |
f46b5a66 | 696 | goto fail; |
79787eaa | 697 | } |
0660b5af | 698 | |
6ef06d27 | 699 | btrfs_i_size_write(BTRFS_I(dir), dir->i_size + namelen * 2); |
52c26179 YZ |
700 | ret = btrfs_update_inode(trans, root, dir); |
701 | BUG_ON(ret); | |
702 | ||
6025c19f | 703 | ret = btrfs_add_root_ref(trans, objectid, root->root_key.objectid, |
4a0cc7ca | 704 | btrfs_ino(BTRFS_I(dir)), index, name, namelen); |
76dda93c | 705 | BUG_ON(ret); |
f46b5a66 | 706 | |
cdb345a8 | 707 | ret = btrfs_uuid_tree_add(trans, root_item->uuid, |
6bccf3ab | 708 | BTRFS_UUID_KEY_SUBVOL, objectid); |
dd5f9615 | 709 | if (ret) |
66642832 | 710 | btrfs_abort_transaction(trans, ret); |
dd5f9615 | 711 | |
f46b5a66 | 712 | fail: |
49a3c4d9 | 713 | kfree(root_item); |
d5c12070 MX |
714 | trans->block_rsv = NULL; |
715 | trans->bytes_reserved = 0; | |
7775c818 | 716 | btrfs_subvolume_release_metadata(fs_info, &block_rsv); |
de6e8200 | 717 | |
72fd032e SW |
718 | if (async_transid) { |
719 | *async_transid = trans->transid; | |
3a45bb20 | 720 | err = btrfs_commit_transaction_async(trans, 1); |
00d71c9c | 721 | if (err) |
3a45bb20 | 722 | err = btrfs_commit_transaction(trans); |
72fd032e | 723 | } else { |
3a45bb20 | 724 | err = btrfs_commit_transaction(trans); |
72fd032e | 725 | } |
f46b5a66 CH |
726 | if (err && !ret) |
727 | ret = err; | |
1a65e24b | 728 | |
5662344b TI |
729 | if (!ret) { |
730 | inode = btrfs_lookup_dentry(dir, dentry); | |
de6e8200 LB |
731 | if (IS_ERR(inode)) |
732 | return PTR_ERR(inode); | |
5662344b TI |
733 | d_instantiate(dentry, inode); |
734 | } | |
f46b5a66 | 735 | return ret; |
49a3c4d9 DS |
736 | |
737 | fail_free: | |
738 | kfree(root_item); | |
739 | return ret; | |
f46b5a66 CH |
740 | } |
741 | ||
e9662f70 | 742 | static int create_snapshot(struct btrfs_root *root, struct inode *dir, |
61d7e4cb | 743 | struct dentry *dentry, |
e9662f70 MX |
744 | u64 *async_transid, bool readonly, |
745 | struct btrfs_qgroup_inherit *inherit) | |
f46b5a66 | 746 | { |
0b246afa | 747 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); |
2e4bfab9 | 748 | struct inode *inode; |
f46b5a66 CH |
749 | struct btrfs_pending_snapshot *pending_snapshot; |
750 | struct btrfs_trans_handle *trans; | |
2e4bfab9 | 751 | int ret; |
8ecebf4d | 752 | bool snapshot_force_cow = false; |
f46b5a66 | 753 | |
27cdeb70 | 754 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state)) |
f46b5a66 CH |
755 | return -EINVAL; |
756 | ||
23269bf5 | 757 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_KERNEL); |
a1ee7362 DS |
758 | if (!pending_snapshot) |
759 | return -ENOMEM; | |
760 | ||
b0c0ea63 | 761 | pending_snapshot->root_item = kzalloc(sizeof(struct btrfs_root_item), |
23269bf5 | 762 | GFP_KERNEL); |
8546b570 DS |
763 | pending_snapshot->path = btrfs_alloc_path(); |
764 | if (!pending_snapshot->root_item || !pending_snapshot->path) { | |
b0c0ea63 DS |
765 | ret = -ENOMEM; |
766 | goto free_pending; | |
767 | } | |
768 | ||
8ecebf4d RK |
769 | /* |
770 | * Force new buffered writes to reserve space even when NOCOW is | |
771 | * possible. This is to avoid later writeback (running dealloc) to | |
772 | * fallback to COW mode and unexpectedly fail with ENOSPC. | |
773 | */ | |
ea14b57f | 774 | atomic_inc(&root->will_be_snapshotted); |
4e857c58 | 775 | smp_mb__after_atomic(); |
45bac0f3 LB |
776 | /* wait for no snapshot writes */ |
777 | wait_event(root->subv_writers->wait, | |
778 | percpu_counter_sum(&root->subv_writers->counter) == 0); | |
8257b2dc | 779 | |
76f32e24 | 780 | ret = btrfs_start_delalloc_inodes(root); |
6a03843d | 781 | if (ret) |
a1ee7362 | 782 | goto dec_and_free; |
6a03843d | 783 | |
8ecebf4d RK |
784 | /* |
785 | * All previous writes have started writeback in NOCOW mode, so now | |
786 | * we force future writes to fallback to COW mode during snapshot | |
787 | * creation. | |
788 | */ | |
789 | atomic_inc(&root->snapshot_force_cow); | |
790 | snapshot_force_cow = true; | |
791 | ||
6374e57a | 792 | btrfs_wait_ordered_extents(root, U64_MAX, 0, (u64)-1); |
6a03843d | 793 | |
66d8f3dd MX |
794 | btrfs_init_block_rsv(&pending_snapshot->block_rsv, |
795 | BTRFS_BLOCK_RSV_TEMP); | |
d5c12070 MX |
796 | /* |
797 | * 1 - parent dir inode | |
798 | * 2 - dir entries | |
799 | * 1 - root item | |
800 | * 2 - root ref/backref | |
801 | * 1 - root of snapshot | |
dd5f9615 | 802 | * 1 - UUID item |
d5c12070 MX |
803 | */ |
804 | ret = btrfs_subvolume_reserve_metadata(BTRFS_I(dir)->root, | |
dd5f9615 | 805 | &pending_snapshot->block_rsv, 8, |
ee3441b4 | 806 | false); |
d5c12070 | 807 | if (ret) |
a1ee7362 | 808 | goto dec_and_free; |
d5c12070 | 809 | |
3de4586c | 810 | pending_snapshot->dentry = dentry; |
f46b5a66 | 811 | pending_snapshot->root = root; |
b83cc969 | 812 | pending_snapshot->readonly = readonly; |
e9662f70 | 813 | pending_snapshot->dir = dir; |
8696c533 | 814 | pending_snapshot->inherit = inherit; |
a22285a6 | 815 | |
d5c12070 | 816 | trans = btrfs_start_transaction(root, 0); |
a22285a6 YZ |
817 | if (IS_ERR(trans)) { |
818 | ret = PTR_ERR(trans); | |
819 | goto fail; | |
820 | } | |
821 | ||
0b246afa | 822 | spin_lock(&fs_info->trans_lock); |
f46b5a66 CH |
823 | list_add(&pending_snapshot->list, |
824 | &trans->transaction->pending_snapshots); | |
0b246afa | 825 | spin_unlock(&fs_info->trans_lock); |
72fd032e SW |
826 | if (async_transid) { |
827 | *async_transid = trans->transid; | |
3a45bb20 | 828 | ret = btrfs_commit_transaction_async(trans, 1); |
00d71c9c | 829 | if (ret) |
3a45bb20 | 830 | ret = btrfs_commit_transaction(trans); |
72fd032e | 831 | } else { |
3a45bb20 | 832 | ret = btrfs_commit_transaction(trans); |
72fd032e | 833 | } |
aec8030a | 834 | if (ret) |
c37b2b62 | 835 | goto fail; |
a22285a6 YZ |
836 | |
837 | ret = pending_snapshot->error; | |
838 | if (ret) | |
839 | goto fail; | |
840 | ||
d3797308 CM |
841 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
842 | if (ret) | |
843 | goto fail; | |
844 | ||
2b0143b5 | 845 | inode = btrfs_lookup_dentry(d_inode(dentry->d_parent), dentry); |
2e4bfab9 YZ |
846 | if (IS_ERR(inode)) { |
847 | ret = PTR_ERR(inode); | |
848 | goto fail; | |
849 | } | |
5662344b | 850 | |
2e4bfab9 YZ |
851 | d_instantiate(dentry, inode); |
852 | ret = 0; | |
853 | fail: | |
7775c818 | 854 | btrfs_subvolume_release_metadata(fs_info, &pending_snapshot->block_rsv); |
a1ee7362 | 855 | dec_and_free: |
8ecebf4d RK |
856 | if (snapshot_force_cow) |
857 | atomic_dec(&root->snapshot_force_cow); | |
ea14b57f | 858 | if (atomic_dec_and_test(&root->will_be_snapshotted)) |
4625956a | 859 | wake_up_var(&root->will_be_snapshotted); |
b0c0ea63 DS |
860 | free_pending: |
861 | kfree(pending_snapshot->root_item); | |
8546b570 | 862 | btrfs_free_path(pending_snapshot->path); |
a1ee7362 DS |
863 | kfree(pending_snapshot); |
864 | ||
f46b5a66 CH |
865 | return ret; |
866 | } | |
867 | ||
4260f7c7 SW |
868 | /* copy of may_delete in fs/namei.c() |
869 | * Check whether we can remove a link victim from directory dir, check | |
870 | * whether the type of victim is right. | |
871 | * 1. We can't do it if dir is read-only (done in permission()) | |
872 | * 2. We should have write and exec permissions on dir | |
873 | * 3. We can't remove anything from append-only dir | |
874 | * 4. We can't do anything with immutable dir (done in permission()) | |
875 | * 5. If the sticky bit on dir is set we should either | |
876 | * a. be owner of dir, or | |
877 | * b. be owner of victim, or | |
878 | * c. have CAP_FOWNER capability | |
01327610 | 879 | * 6. If the victim is append-only or immutable we can't do anything with |
4260f7c7 SW |
880 | * links pointing to it. |
881 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
882 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
883 | * 9. We can't remove a root or mountpoint. | |
884 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by | |
885 | * nfs_async_unlink(). | |
886 | */ | |
887 | ||
67871254 | 888 | static int btrfs_may_delete(struct inode *dir, struct dentry *victim, int isdir) |
4260f7c7 SW |
889 | { |
890 | int error; | |
891 | ||
2b0143b5 | 892 | if (d_really_is_negative(victim)) |
4260f7c7 SW |
893 | return -ENOENT; |
894 | ||
2b0143b5 | 895 | BUG_ON(d_inode(victim->d_parent) != dir); |
4fa6b5ec | 896 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
4260f7c7 SW |
897 | |
898 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
899 | if (error) | |
900 | return error; | |
901 | if (IS_APPEND(dir)) | |
902 | return -EPERM; | |
2b0143b5 DH |
903 | if (check_sticky(dir, d_inode(victim)) || IS_APPEND(d_inode(victim)) || |
904 | IS_IMMUTABLE(d_inode(victim)) || IS_SWAPFILE(d_inode(victim))) | |
4260f7c7 SW |
905 | return -EPERM; |
906 | if (isdir) { | |
e36cb0b8 | 907 | if (!d_is_dir(victim)) |
4260f7c7 SW |
908 | return -ENOTDIR; |
909 | if (IS_ROOT(victim)) | |
910 | return -EBUSY; | |
e36cb0b8 | 911 | } else if (d_is_dir(victim)) |
4260f7c7 SW |
912 | return -EISDIR; |
913 | if (IS_DEADDIR(dir)) | |
914 | return -ENOENT; | |
915 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
916 | return -EBUSY; | |
917 | return 0; | |
918 | } | |
919 | ||
cb8e7090 CH |
920 | /* copy of may_create in fs/namei.c() */ |
921 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) | |
922 | { | |
2b0143b5 | 923 | if (d_really_is_positive(child)) |
cb8e7090 CH |
924 | return -EEXIST; |
925 | if (IS_DEADDIR(dir)) | |
926 | return -ENOENT; | |
927 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
928 | } | |
929 | ||
930 | /* | |
931 | * Create a new subvolume below @parent. This is largely modeled after | |
932 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup | |
933 | * inside this filesystem so it's quite a bit simpler. | |
934 | */ | |
92872094 | 935 | static noinline int btrfs_mksubvol(const struct path *parent, |
52f75f4f | 936 | const char *name, int namelen, |
72fd032e | 937 | struct btrfs_root *snap_src, |
6f72c7e2 | 938 | u64 *async_transid, bool readonly, |
8696c533 | 939 | struct btrfs_qgroup_inherit *inherit) |
cb8e7090 | 940 | { |
0b246afa JM |
941 | struct inode *dir = d_inode(parent->dentry); |
942 | struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb); | |
cb8e7090 CH |
943 | struct dentry *dentry; |
944 | int error; | |
945 | ||
00235411 AV |
946 | error = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
947 | if (error == -EINTR) | |
948 | return error; | |
cb8e7090 CH |
949 | |
950 | dentry = lookup_one_len(name, parent->dentry, namelen); | |
951 | error = PTR_ERR(dentry); | |
952 | if (IS_ERR(dentry)) | |
953 | goto out_unlock; | |
954 | ||
76dda93c | 955 | error = btrfs_may_create(dir, dentry); |
cb8e7090 | 956 | if (error) |
a874a63e | 957 | goto out_dput; |
cb8e7090 | 958 | |
9c52057c CM |
959 | /* |
960 | * even if this name doesn't exist, we may get hash collisions. | |
961 | * check for them now when we can safely fail | |
962 | */ | |
963 | error = btrfs_check_dir_item_collision(BTRFS_I(dir)->root, | |
964 | dir->i_ino, name, | |
965 | namelen); | |
966 | if (error) | |
967 | goto out_dput; | |
968 | ||
0b246afa | 969 | down_read(&fs_info->subvol_sem); |
76dda93c YZ |
970 | |
971 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) | |
972 | goto out_up_read; | |
973 | ||
3de4586c | 974 | if (snap_src) { |
61d7e4cb | 975 | error = create_snapshot(snap_src, dir, dentry, |
6f72c7e2 | 976 | async_transid, readonly, inherit); |
3de4586c | 977 | } else { |
d5c12070 MX |
978 | error = create_subvol(dir, dentry, name, namelen, |
979 | async_transid, inherit); | |
3de4586c | 980 | } |
76dda93c YZ |
981 | if (!error) |
982 | fsnotify_mkdir(dir, dentry); | |
983 | out_up_read: | |
0b246afa | 984 | up_read(&fs_info->subvol_sem); |
cb8e7090 CH |
985 | out_dput: |
986 | dput(dentry); | |
987 | out_unlock: | |
5955102c | 988 | inode_unlock(dir); |
cb8e7090 CH |
989 | return error; |
990 | } | |
991 | ||
4cb5300b CM |
992 | /* |
993 | * When we're defragging a range, we don't want to kick it off again | |
994 | * if it is really just waiting for delalloc to send it down. | |
995 | * If we find a nice big extent or delalloc range for the bytes in the | |
996 | * file you want to defrag, we return 0 to let you know to skip this | |
997 | * part of the file | |
998 | */ | |
aab110ab | 999 | static int check_defrag_in_cache(struct inode *inode, u64 offset, u32 thresh) |
4cb5300b CM |
1000 | { |
1001 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
1002 | struct extent_map *em = NULL; | |
1003 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
1004 | u64 end; | |
1005 | ||
1006 | read_lock(&em_tree->lock); | |
09cbfeaf | 1007 | em = lookup_extent_mapping(em_tree, offset, PAGE_SIZE); |
4cb5300b CM |
1008 | read_unlock(&em_tree->lock); |
1009 | ||
1010 | if (em) { | |
1011 | end = extent_map_end(em); | |
1012 | free_extent_map(em); | |
1013 | if (end - offset > thresh) | |
1014 | return 0; | |
1015 | } | |
1016 | /* if we already have a nice delalloc here, just stop */ | |
1017 | thresh /= 2; | |
1018 | end = count_range_bits(io_tree, &offset, offset + thresh, | |
1019 | thresh, EXTENT_DELALLOC, 1); | |
1020 | if (end >= thresh) | |
1021 | return 0; | |
1022 | return 1; | |
1023 | } | |
1024 | ||
1025 | /* | |
1026 | * helper function to walk through a file and find extents | |
1027 | * newer than a specific transid, and smaller than thresh. | |
1028 | * | |
1029 | * This is used by the defragging code to find new and small | |
1030 | * extents | |
1031 | */ | |
1032 | static int find_new_extents(struct btrfs_root *root, | |
1033 | struct inode *inode, u64 newer_than, | |
aab110ab | 1034 | u64 *off, u32 thresh) |
4cb5300b CM |
1035 | { |
1036 | struct btrfs_path *path; | |
1037 | struct btrfs_key min_key; | |
4cb5300b CM |
1038 | struct extent_buffer *leaf; |
1039 | struct btrfs_file_extent_item *extent; | |
1040 | int type; | |
1041 | int ret; | |
4a0cc7ca | 1042 | u64 ino = btrfs_ino(BTRFS_I(inode)); |
4cb5300b CM |
1043 | |
1044 | path = btrfs_alloc_path(); | |
1045 | if (!path) | |
1046 | return -ENOMEM; | |
1047 | ||
a4689d2b | 1048 | min_key.objectid = ino; |
4cb5300b CM |
1049 | min_key.type = BTRFS_EXTENT_DATA_KEY; |
1050 | min_key.offset = *off; | |
1051 | ||
67871254 | 1052 | while (1) { |
6174d3cb | 1053 | ret = btrfs_search_forward(root, &min_key, path, newer_than); |
4cb5300b CM |
1054 | if (ret != 0) |
1055 | goto none; | |
f094c9bd | 1056 | process_slot: |
a4689d2b | 1057 | if (min_key.objectid != ino) |
4cb5300b CM |
1058 | goto none; |
1059 | if (min_key.type != BTRFS_EXTENT_DATA_KEY) | |
1060 | goto none; | |
1061 | ||
1062 | leaf = path->nodes[0]; | |
1063 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
1064 | struct btrfs_file_extent_item); | |
1065 | ||
1066 | type = btrfs_file_extent_type(leaf, extent); | |
1067 | if (type == BTRFS_FILE_EXTENT_REG && | |
1068 | btrfs_file_extent_num_bytes(leaf, extent) < thresh && | |
1069 | check_defrag_in_cache(inode, min_key.offset, thresh)) { | |
1070 | *off = min_key.offset; | |
1071 | btrfs_free_path(path); | |
1072 | return 0; | |
1073 | } | |
1074 | ||
f094c9bd FM |
1075 | path->slots[0]++; |
1076 | if (path->slots[0] < btrfs_header_nritems(leaf)) { | |
1077 | btrfs_item_key_to_cpu(leaf, &min_key, path->slots[0]); | |
1078 | goto process_slot; | |
1079 | } | |
1080 | ||
4cb5300b CM |
1081 | if (min_key.offset == (u64)-1) |
1082 | goto none; | |
1083 | ||
1084 | min_key.offset++; | |
1085 | btrfs_release_path(path); | |
1086 | } | |
1087 | none: | |
1088 | btrfs_free_path(path); | |
1089 | return -ENOENT; | |
1090 | } | |
1091 | ||
6c282eb4 | 1092 | static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start) |
17ce6ef8 LB |
1093 | { |
1094 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
6c282eb4 LZ |
1095 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
1096 | struct extent_map *em; | |
09cbfeaf | 1097 | u64 len = PAGE_SIZE; |
17ce6ef8 | 1098 | |
6c282eb4 LZ |
1099 | /* |
1100 | * hopefully we have this extent in the tree already, try without | |
1101 | * the full extent lock | |
1102 | */ | |
17ce6ef8 | 1103 | read_lock(&em_tree->lock); |
6c282eb4 | 1104 | em = lookup_extent_mapping(em_tree, start, len); |
17ce6ef8 LB |
1105 | read_unlock(&em_tree->lock); |
1106 | ||
6c282eb4 | 1107 | if (!em) { |
308d9800 FM |
1108 | struct extent_state *cached = NULL; |
1109 | u64 end = start + len - 1; | |
1110 | ||
6c282eb4 | 1111 | /* get the big lock and read metadata off disk */ |
ff13db41 | 1112 | lock_extent_bits(io_tree, start, end, &cached); |
fc4f21b1 | 1113 | em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len, 0); |
e43bbe5e | 1114 | unlock_extent_cached(io_tree, start, end, &cached); |
6c282eb4 LZ |
1115 | |
1116 | if (IS_ERR(em)) | |
1117 | return NULL; | |
1118 | } | |
1119 | ||
1120 | return em; | |
1121 | } | |
17ce6ef8 | 1122 | |
6c282eb4 LZ |
1123 | static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em) |
1124 | { | |
1125 | struct extent_map *next; | |
1126 | bool ret = true; | |
1127 | ||
1128 | /* this is the last extent */ | |
1129 | if (em->start + em->len >= i_size_read(inode)) | |
1130 | return false; | |
1131 | ||
1132 | next = defrag_lookup_extent(inode, em->start + em->len); | |
e9512d72 CM |
1133 | if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE) |
1134 | ret = false; | |
1135 | else if ((em->block_start + em->block_len == next->block_start) && | |
ee22184b | 1136 | (em->block_len > SZ_128K && next->block_len > SZ_128K)) |
6c282eb4 LZ |
1137 | ret = false; |
1138 | ||
1139 | free_extent_map(next); | |
17ce6ef8 LB |
1140 | return ret; |
1141 | } | |
1142 | ||
aab110ab | 1143 | static int should_defrag_range(struct inode *inode, u64 start, u32 thresh, |
a43a2111 AM |
1144 | u64 *last_len, u64 *skip, u64 *defrag_end, |
1145 | int compress) | |
940100a4 | 1146 | { |
6c282eb4 | 1147 | struct extent_map *em; |
940100a4 | 1148 | int ret = 1; |
6c282eb4 | 1149 | bool next_mergeable = true; |
4a3560c4 | 1150 | bool prev_mergeable = true; |
940100a4 CM |
1151 | |
1152 | /* | |
008873ea | 1153 | * make sure that once we start defragging an extent, we keep on |
940100a4 CM |
1154 | * defragging it |
1155 | */ | |
1156 | if (start < *defrag_end) | |
1157 | return 1; | |
1158 | ||
1159 | *skip = 0; | |
1160 | ||
6c282eb4 LZ |
1161 | em = defrag_lookup_extent(inode, start); |
1162 | if (!em) | |
1163 | return 0; | |
940100a4 CM |
1164 | |
1165 | /* this will cover holes, and inline extents */ | |
17ce6ef8 | 1166 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
940100a4 | 1167 | ret = 0; |
17ce6ef8 LB |
1168 | goto out; |
1169 | } | |
1170 | ||
4a3560c4 LB |
1171 | if (!*defrag_end) |
1172 | prev_mergeable = false; | |
1173 | ||
6c282eb4 | 1174 | next_mergeable = defrag_check_next_extent(inode, em); |
940100a4 | 1175 | /* |
6c282eb4 LZ |
1176 | * we hit a real extent, if it is big or the next extent is not a |
1177 | * real extent, don't bother defragging it | |
940100a4 | 1178 | */ |
a43a2111 | 1179 | if (!compress && (*last_len == 0 || *last_len >= thresh) && |
4a3560c4 | 1180 | (em->len >= thresh || (!next_mergeable && !prev_mergeable))) |
940100a4 | 1181 | ret = 0; |
17ce6ef8 | 1182 | out: |
940100a4 CM |
1183 | /* |
1184 | * last_len ends up being a counter of how many bytes we've defragged. | |
1185 | * every time we choose not to defrag an extent, we reset *last_len | |
1186 | * so that the next tiny extent will force a defrag. | |
1187 | * | |
1188 | * The end result of this is that tiny extents before a single big | |
1189 | * extent will force at least part of that big extent to be defragged. | |
1190 | */ | |
1191 | if (ret) { | |
940100a4 CM |
1192 | *defrag_end = extent_map_end(em); |
1193 | } else { | |
1194 | *last_len = 0; | |
1195 | *skip = extent_map_end(em); | |
1196 | *defrag_end = 0; | |
1197 | } | |
1198 | ||
1199 | free_extent_map(em); | |
1200 | return ret; | |
1201 | } | |
1202 | ||
4cb5300b CM |
1203 | /* |
1204 | * it doesn't do much good to defrag one or two pages | |
1205 | * at a time. This pulls in a nice chunk of pages | |
1206 | * to COW and defrag. | |
1207 | * | |
1208 | * It also makes sure the delalloc code has enough | |
1209 | * dirty data to avoid making new small extents as part | |
1210 | * of the defrag | |
1211 | * | |
1212 | * It's a good idea to start RA on this range | |
1213 | * before calling this. | |
1214 | */ | |
1215 | static int cluster_pages_for_defrag(struct inode *inode, | |
1216 | struct page **pages, | |
1217 | unsigned long start_index, | |
c41570c9 | 1218 | unsigned long num_pages) |
f46b5a66 | 1219 | { |
4cb5300b CM |
1220 | unsigned long file_end; |
1221 | u64 isize = i_size_read(inode); | |
1222 | u64 page_start; | |
1223 | u64 page_end; | |
1f12bd06 | 1224 | u64 page_cnt; |
4cb5300b CM |
1225 | int ret; |
1226 | int i; | |
1227 | int i_done; | |
3eaa2885 | 1228 | struct btrfs_ordered_extent *ordered; |
4cb5300b | 1229 | struct extent_state *cached_state = NULL; |
600a45e1 | 1230 | struct extent_io_tree *tree; |
364ecf36 | 1231 | struct extent_changeset *data_reserved = NULL; |
3b16a4e3 | 1232 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
4cb5300b | 1233 | |
09cbfeaf | 1234 | file_end = (isize - 1) >> PAGE_SHIFT; |
1f12bd06 LB |
1235 | if (!isize || start_index > file_end) |
1236 | return 0; | |
1237 | ||
1238 | page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); | |
4cb5300b | 1239 | |
364ecf36 | 1240 | ret = btrfs_delalloc_reserve_space(inode, &data_reserved, |
09cbfeaf KS |
1241 | start_index << PAGE_SHIFT, |
1242 | page_cnt << PAGE_SHIFT); | |
4cb5300b CM |
1243 | if (ret) |
1244 | return ret; | |
4cb5300b | 1245 | i_done = 0; |
600a45e1 | 1246 | tree = &BTRFS_I(inode)->io_tree; |
4cb5300b CM |
1247 | |
1248 | /* step one, lock all the pages */ | |
1f12bd06 | 1249 | for (i = 0; i < page_cnt; i++) { |
4cb5300b | 1250 | struct page *page; |
600a45e1 | 1251 | again: |
a94733d0 | 1252 | page = find_or_create_page(inode->i_mapping, |
600a45e1 | 1253 | start_index + i, mask); |
4cb5300b CM |
1254 | if (!page) |
1255 | break; | |
1256 | ||
600a45e1 | 1257 | page_start = page_offset(page); |
09cbfeaf | 1258 | page_end = page_start + PAGE_SIZE - 1; |
600a45e1 | 1259 | while (1) { |
308d9800 | 1260 | lock_extent_bits(tree, page_start, page_end, |
ff13db41 | 1261 | &cached_state); |
600a45e1 MX |
1262 | ordered = btrfs_lookup_ordered_extent(inode, |
1263 | page_start); | |
308d9800 | 1264 | unlock_extent_cached(tree, page_start, page_end, |
e43bbe5e | 1265 | &cached_state); |
600a45e1 MX |
1266 | if (!ordered) |
1267 | break; | |
1268 | ||
1269 | unlock_page(page); | |
1270 | btrfs_start_ordered_extent(inode, ordered, 1); | |
1271 | btrfs_put_ordered_extent(ordered); | |
1272 | lock_page(page); | |
1f12bd06 LB |
1273 | /* |
1274 | * we unlocked the page above, so we need check if | |
1275 | * it was released or not. | |
1276 | */ | |
1277 | if (page->mapping != inode->i_mapping) { | |
1278 | unlock_page(page); | |
09cbfeaf | 1279 | put_page(page); |
1f12bd06 LB |
1280 | goto again; |
1281 | } | |
600a45e1 MX |
1282 | } |
1283 | ||
4cb5300b CM |
1284 | if (!PageUptodate(page)) { |
1285 | btrfs_readpage(NULL, page); | |
1286 | lock_page(page); | |
1287 | if (!PageUptodate(page)) { | |
1288 | unlock_page(page); | |
09cbfeaf | 1289 | put_page(page); |
4cb5300b CM |
1290 | ret = -EIO; |
1291 | break; | |
1292 | } | |
1293 | } | |
600a45e1 | 1294 | |
600a45e1 MX |
1295 | if (page->mapping != inode->i_mapping) { |
1296 | unlock_page(page); | |
09cbfeaf | 1297 | put_page(page); |
600a45e1 MX |
1298 | goto again; |
1299 | } | |
1300 | ||
4cb5300b CM |
1301 | pages[i] = page; |
1302 | i_done++; | |
1303 | } | |
1304 | if (!i_done || ret) | |
1305 | goto out; | |
1306 | ||
1751e8a6 | 1307 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) |
4cb5300b CM |
1308 | goto out; |
1309 | ||
1310 | /* | |
1311 | * so now we have a nice long stream of locked | |
1312 | * and up to date pages, lets wait on them | |
1313 | */ | |
1314 | for (i = 0; i < i_done; i++) | |
1315 | wait_on_page_writeback(pages[i]); | |
1316 | ||
1317 | page_start = page_offset(pages[0]); | |
09cbfeaf | 1318 | page_end = page_offset(pages[i_done - 1]) + PAGE_SIZE; |
4cb5300b CM |
1319 | |
1320 | lock_extent_bits(&BTRFS_I(inode)->io_tree, | |
ff13db41 | 1321 | page_start, page_end - 1, &cached_state); |
4cb5300b CM |
1322 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, |
1323 | page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | | |
9e8a4a8b | 1324 | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 0, 0, |
ae0f1625 | 1325 | &cached_state); |
4cb5300b | 1326 | |
1f12bd06 | 1327 | if (i_done != page_cnt) { |
9e0baf60 | 1328 | spin_lock(&BTRFS_I(inode)->lock); |
28c4a3e2 | 1329 | btrfs_mod_outstanding_extents(BTRFS_I(inode), 1); |
9e0baf60 | 1330 | spin_unlock(&BTRFS_I(inode)->lock); |
bc42bda2 | 1331 | btrfs_delalloc_release_space(inode, data_reserved, |
09cbfeaf | 1332 | start_index << PAGE_SHIFT, |
43b18595 | 1333 | (page_cnt - i_done) << PAGE_SHIFT, true); |
4cb5300b CM |
1334 | } |
1335 | ||
1336 | ||
9e8a4a8b | 1337 | set_extent_defrag(&BTRFS_I(inode)->io_tree, page_start, page_end - 1, |
018ed4f7 | 1338 | &cached_state); |
4cb5300b CM |
1339 | |
1340 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, | |
e43bbe5e | 1341 | page_start, page_end - 1, &cached_state); |
4cb5300b CM |
1342 | |
1343 | for (i = 0; i < i_done; i++) { | |
1344 | clear_page_dirty_for_io(pages[i]); | |
1345 | ClearPageChecked(pages[i]); | |
1346 | set_page_extent_mapped(pages[i]); | |
1347 | set_page_dirty(pages[i]); | |
1348 | unlock_page(pages[i]); | |
09cbfeaf | 1349 | put_page(pages[i]); |
4cb5300b | 1350 | } |
43b18595 QW |
1351 | btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT, |
1352 | false); | |
364ecf36 | 1353 | extent_changeset_free(data_reserved); |
4cb5300b CM |
1354 | return i_done; |
1355 | out: | |
1356 | for (i = 0; i < i_done; i++) { | |
1357 | unlock_page(pages[i]); | |
09cbfeaf | 1358 | put_page(pages[i]); |
4cb5300b | 1359 | } |
bc42bda2 | 1360 | btrfs_delalloc_release_space(inode, data_reserved, |
09cbfeaf | 1361 | start_index << PAGE_SHIFT, |
43b18595 QW |
1362 | page_cnt << PAGE_SHIFT, true); |
1363 | btrfs_delalloc_release_extents(BTRFS_I(inode), page_cnt << PAGE_SHIFT, | |
1364 | true); | |
364ecf36 | 1365 | extent_changeset_free(data_reserved); |
4cb5300b CM |
1366 | return ret; |
1367 | ||
1368 | } | |
1369 | ||
1370 | int btrfs_defrag_file(struct inode *inode, struct file *file, | |
1371 | struct btrfs_ioctl_defrag_range_args *range, | |
1372 | u64 newer_than, unsigned long max_to_defrag) | |
1373 | { | |
0b246afa | 1374 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4cb5300b | 1375 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4cb5300b | 1376 | struct file_ra_state *ra = NULL; |
f46b5a66 | 1377 | unsigned long last_index; |
151a31b2 | 1378 | u64 isize = i_size_read(inode); |
940100a4 CM |
1379 | u64 last_len = 0; |
1380 | u64 skip = 0; | |
1381 | u64 defrag_end = 0; | |
4cb5300b | 1382 | u64 newer_off = range->start; |
f46b5a66 | 1383 | unsigned long i; |
008873ea | 1384 | unsigned long ra_index = 0; |
f46b5a66 | 1385 | int ret; |
4cb5300b | 1386 | int defrag_count = 0; |
1a419d85 | 1387 | int compress_type = BTRFS_COMPRESS_ZLIB; |
aab110ab | 1388 | u32 extent_thresh = range->extent_thresh; |
09cbfeaf | 1389 | unsigned long max_cluster = SZ_256K >> PAGE_SHIFT; |
c41570c9 | 1390 | unsigned long cluster = max_cluster; |
ee22184b | 1391 | u64 new_align = ~((u64)SZ_128K - 1); |
4cb5300b | 1392 | struct page **pages = NULL; |
1e2ef46d | 1393 | bool do_compress = range->flags & BTRFS_DEFRAG_RANGE_COMPRESS; |
4cb5300b | 1394 | |
0abd5b17 LB |
1395 | if (isize == 0) |
1396 | return 0; | |
1397 | ||
1398 | if (range->start >= isize) | |
1399 | return -EINVAL; | |
1a419d85 | 1400 | |
1e2ef46d | 1401 | if (do_compress) { |
1a419d85 LZ |
1402 | if (range->compress_type > BTRFS_COMPRESS_TYPES) |
1403 | return -EINVAL; | |
1404 | if (range->compress_type) | |
1405 | compress_type = range->compress_type; | |
1406 | } | |
f46b5a66 | 1407 | |
0abd5b17 | 1408 | if (extent_thresh == 0) |
ee22184b | 1409 | extent_thresh = SZ_256K; |
940100a4 | 1410 | |
4cb5300b | 1411 | /* |
0a52d108 DS |
1412 | * If we were not given a file, allocate a readahead context. As |
1413 | * readahead is just an optimization, defrag will work without it so | |
1414 | * we don't error out. | |
4cb5300b CM |
1415 | */ |
1416 | if (!file) { | |
63e727ec | 1417 | ra = kzalloc(sizeof(*ra), GFP_KERNEL); |
0a52d108 DS |
1418 | if (ra) |
1419 | file_ra_state_init(ra, inode->i_mapping); | |
4cb5300b CM |
1420 | } else { |
1421 | ra = &file->f_ra; | |
1422 | } | |
1423 | ||
63e727ec | 1424 | pages = kmalloc_array(max_cluster, sizeof(struct page *), GFP_KERNEL); |
4cb5300b CM |
1425 | if (!pages) { |
1426 | ret = -ENOMEM; | |
1427 | goto out_ra; | |
1428 | } | |
1429 | ||
1430 | /* find the last page to defrag */ | |
1e701a32 | 1431 | if (range->start + range->len > range->start) { |
151a31b2 | 1432 | last_index = min_t(u64, isize - 1, |
09cbfeaf | 1433 | range->start + range->len - 1) >> PAGE_SHIFT; |
1e701a32 | 1434 | } else { |
09cbfeaf | 1435 | last_index = (isize - 1) >> PAGE_SHIFT; |
1e701a32 CM |
1436 | } |
1437 | ||
4cb5300b CM |
1438 | if (newer_than) { |
1439 | ret = find_new_extents(root, inode, newer_than, | |
ee22184b | 1440 | &newer_off, SZ_64K); |
4cb5300b CM |
1441 | if (!ret) { |
1442 | range->start = newer_off; | |
1443 | /* | |
1444 | * we always align our defrag to help keep | |
1445 | * the extents in the file evenly spaced | |
1446 | */ | |
09cbfeaf | 1447 | i = (newer_off & new_align) >> PAGE_SHIFT; |
4cb5300b CM |
1448 | } else |
1449 | goto out_ra; | |
1450 | } else { | |
09cbfeaf | 1451 | i = range->start >> PAGE_SHIFT; |
4cb5300b CM |
1452 | } |
1453 | if (!max_to_defrag) | |
070034bd | 1454 | max_to_defrag = last_index - i + 1; |
4cb5300b | 1455 | |
2a0f7f57 LZ |
1456 | /* |
1457 | * make writeback starts from i, so the defrag range can be | |
1458 | * written sequentially. | |
1459 | */ | |
1460 | if (i < inode->i_mapping->writeback_index) | |
1461 | inode->i_mapping->writeback_index = i; | |
1462 | ||
f7f43cc8 | 1463 | while (i <= last_index && defrag_count < max_to_defrag && |
09cbfeaf | 1464 | (i < DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE))) { |
4cb5300b CM |
1465 | /* |
1466 | * make sure we stop running if someone unmounts | |
1467 | * the FS | |
1468 | */ | |
1751e8a6 | 1469 | if (!(inode->i_sb->s_flags & SB_ACTIVE)) |
4cb5300b CM |
1470 | break; |
1471 | ||
0b246afa JM |
1472 | if (btrfs_defrag_cancelled(fs_info)) { |
1473 | btrfs_debug(fs_info, "defrag_file cancelled"); | |
210549eb DS |
1474 | ret = -EAGAIN; |
1475 | break; | |
1476 | } | |
1477 | ||
09cbfeaf | 1478 | if (!should_defrag_range(inode, (u64)i << PAGE_SHIFT, |
6c282eb4 | 1479 | extent_thresh, &last_len, &skip, |
1e2ef46d | 1480 | &defrag_end, do_compress)){ |
940100a4 CM |
1481 | unsigned long next; |
1482 | /* | |
1483 | * the should_defrag function tells us how much to skip | |
1484 | * bump our counter by the suggested amount | |
1485 | */ | |
09cbfeaf | 1486 | next = DIV_ROUND_UP(skip, PAGE_SIZE); |
940100a4 CM |
1487 | i = max(i + 1, next); |
1488 | continue; | |
1489 | } | |
008873ea LZ |
1490 | |
1491 | if (!newer_than) { | |
09cbfeaf KS |
1492 | cluster = (PAGE_ALIGN(defrag_end) >> |
1493 | PAGE_SHIFT) - i; | |
008873ea LZ |
1494 | cluster = min(cluster, max_cluster); |
1495 | } else { | |
1496 | cluster = max_cluster; | |
1497 | } | |
1498 | ||
008873ea LZ |
1499 | if (i + cluster > ra_index) { |
1500 | ra_index = max(i, ra_index); | |
0a52d108 | 1501 | if (ra) |
d3c0bab5 DS |
1502 | page_cache_sync_readahead(inode->i_mapping, ra, |
1503 | file, ra_index, cluster); | |
e4826a5b | 1504 | ra_index += cluster; |
008873ea | 1505 | } |
940100a4 | 1506 | |
5955102c | 1507 | inode_lock(inode); |
1e2ef46d | 1508 | if (do_compress) |
eec63c65 | 1509 | BTRFS_I(inode)->defrag_compress = compress_type; |
008873ea | 1510 | ret = cluster_pages_for_defrag(inode, pages, i, cluster); |
ecb8bea8 | 1511 | if (ret < 0) { |
5955102c | 1512 | inode_unlock(inode); |
4cb5300b | 1513 | goto out_ra; |
ecb8bea8 | 1514 | } |
4cb5300b CM |
1515 | |
1516 | defrag_count += ret; | |
d0e1d66b | 1517 | balance_dirty_pages_ratelimited(inode->i_mapping); |
5955102c | 1518 | inode_unlock(inode); |
4cb5300b CM |
1519 | |
1520 | if (newer_than) { | |
1521 | if (newer_off == (u64)-1) | |
1522 | break; | |
1523 | ||
e1f041e1 LB |
1524 | if (ret > 0) |
1525 | i += ret; | |
1526 | ||
4cb5300b | 1527 | newer_off = max(newer_off + 1, |
09cbfeaf | 1528 | (u64)i << PAGE_SHIFT); |
4cb5300b | 1529 | |
ee22184b BL |
1530 | ret = find_new_extents(root, inode, newer_than, |
1531 | &newer_off, SZ_64K); | |
4cb5300b CM |
1532 | if (!ret) { |
1533 | range->start = newer_off; | |
09cbfeaf | 1534 | i = (newer_off & new_align) >> PAGE_SHIFT; |
4cb5300b CM |
1535 | } else { |
1536 | break; | |
f46b5a66 | 1537 | } |
4cb5300b | 1538 | } else { |
008873ea | 1539 | if (ret > 0) { |
cbcc8326 | 1540 | i += ret; |
09cbfeaf | 1541 | last_len += ret << PAGE_SHIFT; |
008873ea | 1542 | } else { |
cbcc8326 | 1543 | i++; |
008873ea LZ |
1544 | last_len = 0; |
1545 | } | |
f46b5a66 | 1546 | } |
f46b5a66 CH |
1547 | } |
1548 | ||
dec8ef90 | 1549 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) { |
1e701a32 | 1550 | filemap_flush(inode->i_mapping); |
dec8ef90 FM |
1551 | if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, |
1552 | &BTRFS_I(inode)->runtime_flags)) | |
1553 | filemap_flush(inode->i_mapping); | |
1554 | } | |
1e701a32 | 1555 | |
1a419d85 | 1556 | if (range->compress_type == BTRFS_COMPRESS_LZO) { |
0b246afa | 1557 | btrfs_set_fs_incompat(fs_info, COMPRESS_LZO); |
5c1aab1d NT |
1558 | } else if (range->compress_type == BTRFS_COMPRESS_ZSTD) { |
1559 | btrfs_set_fs_incompat(fs_info, COMPRESS_ZSTD); | |
1a419d85 LZ |
1560 | } |
1561 | ||
60ccf82f | 1562 | ret = defrag_count; |
940100a4 | 1563 | |
4cb5300b | 1564 | out_ra: |
1e2ef46d | 1565 | if (do_compress) { |
5955102c | 1566 | inode_lock(inode); |
eec63c65 | 1567 | BTRFS_I(inode)->defrag_compress = BTRFS_COMPRESS_NONE; |
5955102c | 1568 | inode_unlock(inode); |
633085c7 | 1569 | } |
4cb5300b CM |
1570 | if (!file) |
1571 | kfree(ra); | |
1572 | kfree(pages); | |
940100a4 | 1573 | return ret; |
f46b5a66 CH |
1574 | } |
1575 | ||
198605a8 | 1576 | static noinline int btrfs_ioctl_resize(struct file *file, |
76dda93c | 1577 | void __user *arg) |
f46b5a66 | 1578 | { |
0b246afa JM |
1579 | struct inode *inode = file_inode(file); |
1580 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
f46b5a66 CH |
1581 | u64 new_size; |
1582 | u64 old_size; | |
1583 | u64 devid = 1; | |
0b246afa | 1584 | struct btrfs_root *root = BTRFS_I(inode)->root; |
f46b5a66 CH |
1585 | struct btrfs_ioctl_vol_args *vol_args; |
1586 | struct btrfs_trans_handle *trans; | |
1587 | struct btrfs_device *device = NULL; | |
1588 | char *sizestr; | |
9a40f122 | 1589 | char *retptr; |
f46b5a66 CH |
1590 | char *devstr = NULL; |
1591 | int ret = 0; | |
f46b5a66 CH |
1592 | int mod = 0; |
1593 | ||
e441d54d CM |
1594 | if (!capable(CAP_SYS_ADMIN)) |
1595 | return -EPERM; | |
1596 | ||
198605a8 MX |
1597 | ret = mnt_want_write_file(file); |
1598 | if (ret) | |
1599 | return ret; | |
1600 | ||
171938e5 | 1601 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
97547676 | 1602 | mnt_drop_write_file(file); |
e57138b3 | 1603 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
c9e9f97b ID |
1604 | } |
1605 | ||
dae7b665 | 1606 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
1607 | if (IS_ERR(vol_args)) { |
1608 | ret = PTR_ERR(vol_args); | |
1609 | goto out; | |
1610 | } | |
5516e595 MF |
1611 | |
1612 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
f46b5a66 | 1613 | |
f46b5a66 CH |
1614 | sizestr = vol_args->name; |
1615 | devstr = strchr(sizestr, ':'); | |
1616 | if (devstr) { | |
f46b5a66 CH |
1617 | sizestr = devstr + 1; |
1618 | *devstr = '\0'; | |
1619 | devstr = vol_args->name; | |
58dfae63 Z |
1620 | ret = kstrtoull(devstr, 10, &devid); |
1621 | if (ret) | |
1622 | goto out_free; | |
dfd79829 MX |
1623 | if (!devid) { |
1624 | ret = -EINVAL; | |
1625 | goto out_free; | |
1626 | } | |
0b246afa | 1627 | btrfs_info(fs_info, "resizing devid %llu", devid); |
f46b5a66 | 1628 | } |
dba60f3f | 1629 | |
0b246afa | 1630 | device = btrfs_find_device(fs_info, devid, NULL, NULL); |
f46b5a66 | 1631 | if (!device) { |
0b246afa JM |
1632 | btrfs_info(fs_info, "resizer unable to find device %llu", |
1633 | devid); | |
dfd79829 | 1634 | ret = -ENODEV; |
c9e9f97b | 1635 | goto out_free; |
f46b5a66 | 1636 | } |
dba60f3f | 1637 | |
ebbede42 | 1638 | if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state)) { |
0b246afa | 1639 | btrfs_info(fs_info, |
efe120a0 | 1640 | "resizer unable to apply on readonly device %llu", |
c1c9ff7c | 1641 | devid); |
dfd79829 | 1642 | ret = -EPERM; |
4e42ae1b LB |
1643 | goto out_free; |
1644 | } | |
1645 | ||
f46b5a66 CH |
1646 | if (!strcmp(sizestr, "max")) |
1647 | new_size = device->bdev->bd_inode->i_size; | |
1648 | else { | |
1649 | if (sizestr[0] == '-') { | |
1650 | mod = -1; | |
1651 | sizestr++; | |
1652 | } else if (sizestr[0] == '+') { | |
1653 | mod = 1; | |
1654 | sizestr++; | |
1655 | } | |
9a40f122 GH |
1656 | new_size = memparse(sizestr, &retptr); |
1657 | if (*retptr != '\0' || new_size == 0) { | |
f46b5a66 | 1658 | ret = -EINVAL; |
c9e9f97b | 1659 | goto out_free; |
f46b5a66 CH |
1660 | } |
1661 | } | |
1662 | ||
401e29c1 | 1663 | if (test_bit(BTRFS_DEV_STATE_REPLACE_TGT, &device->dev_state)) { |
dfd79829 | 1664 | ret = -EPERM; |
63a212ab SB |
1665 | goto out_free; |
1666 | } | |
1667 | ||
7cc8e58d | 1668 | old_size = btrfs_device_get_total_bytes(device); |
f46b5a66 CH |
1669 | |
1670 | if (mod < 0) { | |
1671 | if (new_size > old_size) { | |
1672 | ret = -EINVAL; | |
c9e9f97b | 1673 | goto out_free; |
f46b5a66 CH |
1674 | } |
1675 | new_size = old_size - new_size; | |
1676 | } else if (mod > 0) { | |
eb8052e0 | 1677 | if (new_size > ULLONG_MAX - old_size) { |
902c68a4 | 1678 | ret = -ERANGE; |
eb8052e0 WF |
1679 | goto out_free; |
1680 | } | |
f46b5a66 CH |
1681 | new_size = old_size + new_size; |
1682 | } | |
1683 | ||
ee22184b | 1684 | if (new_size < SZ_256M) { |
f46b5a66 | 1685 | ret = -EINVAL; |
c9e9f97b | 1686 | goto out_free; |
f46b5a66 CH |
1687 | } |
1688 | if (new_size > device->bdev->bd_inode->i_size) { | |
1689 | ret = -EFBIG; | |
c9e9f97b | 1690 | goto out_free; |
f46b5a66 CH |
1691 | } |
1692 | ||
47f08b96 | 1693 | new_size = round_down(new_size, fs_info->sectorsize); |
f46b5a66 | 1694 | |
0b246afa JM |
1695 | btrfs_info_in_rcu(fs_info, "new size for %s is %llu", |
1696 | rcu_str_deref(device->name), new_size); | |
f46b5a66 CH |
1697 | |
1698 | if (new_size > old_size) { | |
a22285a6 | 1699 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
1700 | if (IS_ERR(trans)) { |
1701 | ret = PTR_ERR(trans); | |
c9e9f97b | 1702 | goto out_free; |
98d5dc13 | 1703 | } |
f46b5a66 | 1704 | ret = btrfs_grow_device(trans, device, new_size); |
3a45bb20 | 1705 | btrfs_commit_transaction(trans); |
ece7d20e | 1706 | } else if (new_size < old_size) { |
f46b5a66 | 1707 | ret = btrfs_shrink_device(device, new_size); |
0253f40e | 1708 | } /* equal, nothing need to do */ |
f46b5a66 | 1709 | |
c9e9f97b | 1710 | out_free: |
f46b5a66 | 1711 | kfree(vol_args); |
c9e9f97b | 1712 | out: |
171938e5 | 1713 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
18f39c41 | 1714 | mnt_drop_write_file(file); |
f46b5a66 CH |
1715 | return ret; |
1716 | } | |
1717 | ||
72fd032e | 1718 | static noinline int btrfs_ioctl_snap_create_transid(struct file *file, |
52f75f4f | 1719 | const char *name, unsigned long fd, int subvol, |
6f72c7e2 | 1720 | u64 *transid, bool readonly, |
8696c533 | 1721 | struct btrfs_qgroup_inherit *inherit) |
f46b5a66 | 1722 | { |
f46b5a66 | 1723 | int namelen; |
3de4586c | 1724 | int ret = 0; |
f46b5a66 | 1725 | |
325c50e3 JM |
1726 | if (!S_ISDIR(file_inode(file)->i_mode)) |
1727 | return -ENOTDIR; | |
1728 | ||
a874a63e LB |
1729 | ret = mnt_want_write_file(file); |
1730 | if (ret) | |
1731 | goto out; | |
1732 | ||
72fd032e SW |
1733 | namelen = strlen(name); |
1734 | if (strchr(name, '/')) { | |
f46b5a66 | 1735 | ret = -EINVAL; |
a874a63e | 1736 | goto out_drop_write; |
f46b5a66 CH |
1737 | } |
1738 | ||
16780cab CM |
1739 | if (name[0] == '.' && |
1740 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { | |
1741 | ret = -EEXIST; | |
a874a63e | 1742 | goto out_drop_write; |
16780cab CM |
1743 | } |
1744 | ||
3de4586c | 1745 | if (subvol) { |
72fd032e | 1746 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
6f72c7e2 | 1747 | NULL, transid, readonly, inherit); |
cb8e7090 | 1748 | } else { |
2903ff01 | 1749 | struct fd src = fdget(fd); |
3de4586c | 1750 | struct inode *src_inode; |
2903ff01 | 1751 | if (!src.file) { |
3de4586c | 1752 | ret = -EINVAL; |
a874a63e | 1753 | goto out_drop_write; |
3de4586c CM |
1754 | } |
1755 | ||
496ad9aa AV |
1756 | src_inode = file_inode(src.file); |
1757 | if (src_inode->i_sb != file_inode(file)->i_sb) { | |
c79b4713 | 1758 | btrfs_info(BTRFS_I(file_inode(file))->root->fs_info, |
efe120a0 | 1759 | "Snapshot src from another FS"); |
23ad5b17 | 1760 | ret = -EXDEV; |
d0242061 DS |
1761 | } else if (!inode_owner_or_capable(src_inode)) { |
1762 | /* | |
1763 | * Subvolume creation is not restricted, but snapshots | |
1764 | * are limited to own subvolumes only | |
1765 | */ | |
1766 | ret = -EPERM; | |
ecd18815 AV |
1767 | } else { |
1768 | ret = btrfs_mksubvol(&file->f_path, name, namelen, | |
1769 | BTRFS_I(src_inode)->root, | |
1770 | transid, readonly, inherit); | |
3de4586c | 1771 | } |
2903ff01 | 1772 | fdput(src); |
cb8e7090 | 1773 | } |
a874a63e LB |
1774 | out_drop_write: |
1775 | mnt_drop_write_file(file); | |
f46b5a66 | 1776 | out: |
72fd032e SW |
1777 | return ret; |
1778 | } | |
1779 | ||
1780 | static noinline int btrfs_ioctl_snap_create(struct file *file, | |
fa0d2b9b | 1781 | void __user *arg, int subvol) |
72fd032e | 1782 | { |
fa0d2b9b | 1783 | struct btrfs_ioctl_vol_args *vol_args; |
72fd032e SW |
1784 | int ret; |
1785 | ||
325c50e3 JM |
1786 | if (!S_ISDIR(file_inode(file)->i_mode)) |
1787 | return -ENOTDIR; | |
1788 | ||
fa0d2b9b LZ |
1789 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1790 | if (IS_ERR(vol_args)) | |
1791 | return PTR_ERR(vol_args); | |
1792 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
72fd032e | 1793 | |
fa0d2b9b | 1794 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
b83cc969 | 1795 | vol_args->fd, subvol, |
6f72c7e2 | 1796 | NULL, false, NULL); |
fdfb1e4f | 1797 | |
fa0d2b9b LZ |
1798 | kfree(vol_args); |
1799 | return ret; | |
1800 | } | |
fdfb1e4f | 1801 | |
fa0d2b9b LZ |
1802 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
1803 | void __user *arg, int subvol) | |
1804 | { | |
1805 | struct btrfs_ioctl_vol_args_v2 *vol_args; | |
1806 | int ret; | |
1807 | u64 transid = 0; | |
1808 | u64 *ptr = NULL; | |
b83cc969 | 1809 | bool readonly = false; |
6f72c7e2 | 1810 | struct btrfs_qgroup_inherit *inherit = NULL; |
75eaa0e2 | 1811 | |
325c50e3 JM |
1812 | if (!S_ISDIR(file_inode(file)->i_mode)) |
1813 | return -ENOTDIR; | |
1814 | ||
fa0d2b9b LZ |
1815 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1816 | if (IS_ERR(vol_args)) | |
1817 | return PTR_ERR(vol_args); | |
1818 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; | |
75eaa0e2 | 1819 | |
b83cc969 | 1820 | if (vol_args->flags & |
6f72c7e2 AJ |
1821 | ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY | |
1822 | BTRFS_SUBVOL_QGROUP_INHERIT)) { | |
b83cc969 | 1823 | ret = -EOPNOTSUPP; |
c47ca32d | 1824 | goto free_args; |
72fd032e | 1825 | } |
fa0d2b9b LZ |
1826 | |
1827 | if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) | |
1828 | ptr = &transid; | |
b83cc969 LZ |
1829 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
1830 | readonly = true; | |
6f72c7e2 | 1831 | if (vol_args->flags & BTRFS_SUBVOL_QGROUP_INHERIT) { |
09cbfeaf | 1832 | if (vol_args->size > PAGE_SIZE) { |
6f72c7e2 | 1833 | ret = -EINVAL; |
c47ca32d | 1834 | goto free_args; |
6f72c7e2 AJ |
1835 | } |
1836 | inherit = memdup_user(vol_args->qgroup_inherit, vol_args->size); | |
1837 | if (IS_ERR(inherit)) { | |
1838 | ret = PTR_ERR(inherit); | |
c47ca32d | 1839 | goto free_args; |
6f72c7e2 AJ |
1840 | } |
1841 | } | |
fa0d2b9b LZ |
1842 | |
1843 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, | |
6f72c7e2 | 1844 | vol_args->fd, subvol, ptr, |
8696c533 | 1845 | readonly, inherit); |
c47ca32d DC |
1846 | if (ret) |
1847 | goto free_inherit; | |
fa0d2b9b | 1848 | |
c47ca32d DC |
1849 | if (ptr && copy_to_user(arg + |
1850 | offsetof(struct btrfs_ioctl_vol_args_v2, | |
1851 | transid), | |
1852 | ptr, sizeof(*ptr))) | |
fa0d2b9b | 1853 | ret = -EFAULT; |
c47ca32d DC |
1854 | |
1855 | free_inherit: | |
6f72c7e2 | 1856 | kfree(inherit); |
c47ca32d DC |
1857 | free_args: |
1858 | kfree(vol_args); | |
f46b5a66 CH |
1859 | return ret; |
1860 | } | |
1861 | ||
0caa102d LZ |
1862 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
1863 | void __user *arg) | |
1864 | { | |
496ad9aa | 1865 | struct inode *inode = file_inode(file); |
0b246afa | 1866 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0caa102d LZ |
1867 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1868 | int ret = 0; | |
1869 | u64 flags = 0; | |
1870 | ||
4a0cc7ca | 1871 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) |
0caa102d LZ |
1872 | return -EINVAL; |
1873 | ||
0b246afa | 1874 | down_read(&fs_info->subvol_sem); |
0caa102d LZ |
1875 | if (btrfs_root_readonly(root)) |
1876 | flags |= BTRFS_SUBVOL_RDONLY; | |
0b246afa | 1877 | up_read(&fs_info->subvol_sem); |
0caa102d LZ |
1878 | |
1879 | if (copy_to_user(arg, &flags, sizeof(flags))) | |
1880 | ret = -EFAULT; | |
1881 | ||
1882 | return ret; | |
1883 | } | |
1884 | ||
1885 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, | |
1886 | void __user *arg) | |
1887 | { | |
496ad9aa | 1888 | struct inode *inode = file_inode(file); |
0b246afa | 1889 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0caa102d LZ |
1890 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1891 | struct btrfs_trans_handle *trans; | |
1892 | u64 root_flags; | |
1893 | u64 flags; | |
1894 | int ret = 0; | |
1895 | ||
bd60ea0f DS |
1896 | if (!inode_owner_or_capable(inode)) |
1897 | return -EPERM; | |
1898 | ||
b9ca0664 LB |
1899 | ret = mnt_want_write_file(file); |
1900 | if (ret) | |
1901 | goto out; | |
0caa102d | 1902 | |
4a0cc7ca | 1903 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
b9ca0664 LB |
1904 | ret = -EINVAL; |
1905 | goto out_drop_write; | |
1906 | } | |
0caa102d | 1907 | |
b9ca0664 LB |
1908 | if (copy_from_user(&flags, arg, sizeof(flags))) { |
1909 | ret = -EFAULT; | |
1910 | goto out_drop_write; | |
1911 | } | |
0caa102d | 1912 | |
b9ca0664 LB |
1913 | if (flags & BTRFS_SUBVOL_CREATE_ASYNC) { |
1914 | ret = -EINVAL; | |
1915 | goto out_drop_write; | |
1916 | } | |
0caa102d | 1917 | |
b9ca0664 LB |
1918 | if (flags & ~BTRFS_SUBVOL_RDONLY) { |
1919 | ret = -EOPNOTSUPP; | |
1920 | goto out_drop_write; | |
1921 | } | |
0caa102d | 1922 | |
0b246afa | 1923 | down_write(&fs_info->subvol_sem); |
0caa102d LZ |
1924 | |
1925 | /* nothing to do */ | |
1926 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) | |
b9ca0664 | 1927 | goto out_drop_sem; |
0caa102d LZ |
1928 | |
1929 | root_flags = btrfs_root_flags(&root->root_item); | |
2c686537 | 1930 | if (flags & BTRFS_SUBVOL_RDONLY) { |
0caa102d LZ |
1931 | btrfs_set_root_flags(&root->root_item, |
1932 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); | |
2c686537 DS |
1933 | } else { |
1934 | /* | |
1935 | * Block RO -> RW transition if this subvolume is involved in | |
1936 | * send | |
1937 | */ | |
1938 | spin_lock(&root->root_item_lock); | |
1939 | if (root->send_in_progress == 0) { | |
1940 | btrfs_set_root_flags(&root->root_item, | |
0caa102d | 1941 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); |
2c686537 DS |
1942 | spin_unlock(&root->root_item_lock); |
1943 | } else { | |
1944 | spin_unlock(&root->root_item_lock); | |
0b246afa JM |
1945 | btrfs_warn(fs_info, |
1946 | "Attempt to set subvolume %llu read-write during send", | |
1947 | root->root_key.objectid); | |
2c686537 DS |
1948 | ret = -EPERM; |
1949 | goto out_drop_sem; | |
1950 | } | |
1951 | } | |
0caa102d LZ |
1952 | |
1953 | trans = btrfs_start_transaction(root, 1); | |
1954 | if (IS_ERR(trans)) { | |
1955 | ret = PTR_ERR(trans); | |
1956 | goto out_reset; | |
1957 | } | |
1958 | ||
0b246afa | 1959 | ret = btrfs_update_root(trans, fs_info->tree_root, |
0caa102d | 1960 | &root->root_key, &root->root_item); |
9417ebc8 NB |
1961 | if (ret < 0) { |
1962 | btrfs_end_transaction(trans); | |
1963 | goto out_reset; | |
1964 | } | |
1965 | ||
1966 | ret = btrfs_commit_transaction(trans); | |
0caa102d | 1967 | |
0caa102d LZ |
1968 | out_reset: |
1969 | if (ret) | |
1970 | btrfs_set_root_flags(&root->root_item, root_flags); | |
b9ca0664 | 1971 | out_drop_sem: |
0b246afa | 1972 | up_write(&fs_info->subvol_sem); |
b9ca0664 LB |
1973 | out_drop_write: |
1974 | mnt_drop_write_file(file); | |
1975 | out: | |
0caa102d LZ |
1976 | return ret; |
1977 | } | |
1978 | ||
ac8e9819 CM |
1979 | static noinline int key_in_sk(struct btrfs_key *key, |
1980 | struct btrfs_ioctl_search_key *sk) | |
1981 | { | |
abc6e134 CM |
1982 | struct btrfs_key test; |
1983 | int ret; | |
1984 | ||
1985 | test.objectid = sk->min_objectid; | |
1986 | test.type = sk->min_type; | |
1987 | test.offset = sk->min_offset; | |
1988 | ||
1989 | ret = btrfs_comp_cpu_keys(key, &test); | |
1990 | if (ret < 0) | |
ac8e9819 | 1991 | return 0; |
abc6e134 CM |
1992 | |
1993 | test.objectid = sk->max_objectid; | |
1994 | test.type = sk->max_type; | |
1995 | test.offset = sk->max_offset; | |
1996 | ||
1997 | ret = btrfs_comp_cpu_keys(key, &test); | |
1998 | if (ret > 0) | |
ac8e9819 CM |
1999 | return 0; |
2000 | return 1; | |
2001 | } | |
2002 | ||
df397565 | 2003 | static noinline int copy_to_sk(struct btrfs_path *path, |
ac8e9819 CM |
2004 | struct btrfs_key *key, |
2005 | struct btrfs_ioctl_search_key *sk, | |
9b6e817d | 2006 | size_t *buf_size, |
ba346b35 | 2007 | char __user *ubuf, |
ac8e9819 CM |
2008 | unsigned long *sk_offset, |
2009 | int *num_found) | |
2010 | { | |
2011 | u64 found_transid; | |
2012 | struct extent_buffer *leaf; | |
2013 | struct btrfs_ioctl_search_header sh; | |
dd81d459 | 2014 | struct btrfs_key test; |
ac8e9819 CM |
2015 | unsigned long item_off; |
2016 | unsigned long item_len; | |
2017 | int nritems; | |
2018 | int i; | |
2019 | int slot; | |
ac8e9819 CM |
2020 | int ret = 0; |
2021 | ||
2022 | leaf = path->nodes[0]; | |
2023 | slot = path->slots[0]; | |
2024 | nritems = btrfs_header_nritems(leaf); | |
2025 | ||
2026 | if (btrfs_header_generation(leaf) > sk->max_transid) { | |
2027 | i = nritems; | |
2028 | goto advance_key; | |
2029 | } | |
2030 | found_transid = btrfs_header_generation(leaf); | |
2031 | ||
2032 | for (i = slot; i < nritems; i++) { | |
2033 | item_off = btrfs_item_ptr_offset(leaf, i); | |
2034 | item_len = btrfs_item_size_nr(leaf, i); | |
2035 | ||
03b71c6c GP |
2036 | btrfs_item_key_to_cpu(leaf, key, i); |
2037 | if (!key_in_sk(key, sk)) | |
2038 | continue; | |
2039 | ||
9b6e817d | 2040 | if (sizeof(sh) + item_len > *buf_size) { |
8f5f6178 GH |
2041 | if (*num_found) { |
2042 | ret = 1; | |
2043 | goto out; | |
2044 | } | |
2045 | ||
2046 | /* | |
2047 | * return one empty item back for v1, which does not | |
2048 | * handle -EOVERFLOW | |
2049 | */ | |
2050 | ||
9b6e817d | 2051 | *buf_size = sizeof(sh) + item_len; |
ac8e9819 | 2052 | item_len = 0; |
8f5f6178 GH |
2053 | ret = -EOVERFLOW; |
2054 | } | |
ac8e9819 | 2055 | |
9b6e817d | 2056 | if (sizeof(sh) + item_len + *sk_offset > *buf_size) { |
ac8e9819 | 2057 | ret = 1; |
25c9bc2e | 2058 | goto out; |
ac8e9819 CM |
2059 | } |
2060 | ||
ac8e9819 CM |
2061 | sh.objectid = key->objectid; |
2062 | sh.offset = key->offset; | |
2063 | sh.type = key->type; | |
2064 | sh.len = item_len; | |
2065 | sh.transid = found_transid; | |
2066 | ||
2067 | /* copy search result header */ | |
ba346b35 GH |
2068 | if (copy_to_user(ubuf + *sk_offset, &sh, sizeof(sh))) { |
2069 | ret = -EFAULT; | |
2070 | goto out; | |
2071 | } | |
2072 | ||
ac8e9819 CM |
2073 | *sk_offset += sizeof(sh); |
2074 | ||
2075 | if (item_len) { | |
ba346b35 | 2076 | char __user *up = ubuf + *sk_offset; |
ac8e9819 | 2077 | /* copy the item */ |
ba346b35 GH |
2078 | if (read_extent_buffer_to_user(leaf, up, |
2079 | item_off, item_len)) { | |
2080 | ret = -EFAULT; | |
2081 | goto out; | |
2082 | } | |
2083 | ||
ac8e9819 | 2084 | *sk_offset += item_len; |
ac8e9819 | 2085 | } |
e2156867 | 2086 | (*num_found)++; |
ac8e9819 | 2087 | |
8f5f6178 GH |
2088 | if (ret) /* -EOVERFLOW from above */ |
2089 | goto out; | |
2090 | ||
25c9bc2e GH |
2091 | if (*num_found >= sk->nr_items) { |
2092 | ret = 1; | |
2093 | goto out; | |
2094 | } | |
ac8e9819 CM |
2095 | } |
2096 | advance_key: | |
abc6e134 | 2097 | ret = 0; |
dd81d459 NA |
2098 | test.objectid = sk->max_objectid; |
2099 | test.type = sk->max_type; | |
2100 | test.offset = sk->max_offset; | |
2101 | if (btrfs_comp_cpu_keys(key, &test) >= 0) | |
2102 | ret = 1; | |
2103 | else if (key->offset < (u64)-1) | |
ac8e9819 | 2104 | key->offset++; |
dd81d459 | 2105 | else if (key->type < (u8)-1) { |
abc6e134 | 2106 | key->offset = 0; |
ac8e9819 | 2107 | key->type++; |
dd81d459 | 2108 | } else if (key->objectid < (u64)-1) { |
abc6e134 CM |
2109 | key->offset = 0; |
2110 | key->type = 0; | |
ac8e9819 | 2111 | key->objectid++; |
abc6e134 CM |
2112 | } else |
2113 | ret = 1; | |
25c9bc2e | 2114 | out: |
ba346b35 GH |
2115 | /* |
2116 | * 0: all items from this leaf copied, continue with next | |
2117 | * 1: * more items can be copied, but unused buffer is too small | |
2118 | * * all items were found | |
2119 | * Either way, it will stops the loop which iterates to the next | |
2120 | * leaf | |
2121 | * -EOVERFLOW: item was to large for buffer | |
2122 | * -EFAULT: could not copy extent buffer back to userspace | |
2123 | */ | |
ac8e9819 CM |
2124 | return ret; |
2125 | } | |
2126 | ||
2127 | static noinline int search_ioctl(struct inode *inode, | |
12544442 | 2128 | struct btrfs_ioctl_search_key *sk, |
9b6e817d | 2129 | size_t *buf_size, |
ba346b35 | 2130 | char __user *ubuf) |
ac8e9819 | 2131 | { |
0b246afa | 2132 | struct btrfs_fs_info *info = btrfs_sb(inode->i_sb); |
ac8e9819 CM |
2133 | struct btrfs_root *root; |
2134 | struct btrfs_key key; | |
ac8e9819 | 2135 | struct btrfs_path *path; |
ac8e9819 CM |
2136 | int ret; |
2137 | int num_found = 0; | |
2138 | unsigned long sk_offset = 0; | |
2139 | ||
9b6e817d GH |
2140 | if (*buf_size < sizeof(struct btrfs_ioctl_search_header)) { |
2141 | *buf_size = sizeof(struct btrfs_ioctl_search_header); | |
12544442 | 2142 | return -EOVERFLOW; |
9b6e817d | 2143 | } |
12544442 | 2144 | |
ac8e9819 CM |
2145 | path = btrfs_alloc_path(); |
2146 | if (!path) | |
2147 | return -ENOMEM; | |
2148 | ||
2149 | if (sk->tree_id == 0) { | |
2150 | /* search the root of the inode that was passed */ | |
2151 | root = BTRFS_I(inode)->root; | |
2152 | } else { | |
2153 | key.objectid = sk->tree_id; | |
2154 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2155 | key.offset = (u64)-1; | |
2156 | root = btrfs_read_fs_root_no_name(info, &key); | |
2157 | if (IS_ERR(root)) { | |
ac8e9819 | 2158 | btrfs_free_path(path); |
ad1e3d56 | 2159 | return PTR_ERR(root); |
ac8e9819 CM |
2160 | } |
2161 | } | |
2162 | ||
2163 | key.objectid = sk->min_objectid; | |
2164 | key.type = sk->min_type; | |
2165 | key.offset = sk->min_offset; | |
2166 | ||
67871254 | 2167 | while (1) { |
6174d3cb | 2168 | ret = btrfs_search_forward(root, &key, path, sk->min_transid); |
ac8e9819 CM |
2169 | if (ret != 0) { |
2170 | if (ret > 0) | |
2171 | ret = 0; | |
2172 | goto err; | |
2173 | } | |
df397565 | 2174 | ret = copy_to_sk(path, &key, sk, buf_size, ubuf, |
ac8e9819 | 2175 | &sk_offset, &num_found); |
b3b4aa74 | 2176 | btrfs_release_path(path); |
25c9bc2e | 2177 | if (ret) |
ac8e9819 CM |
2178 | break; |
2179 | ||
2180 | } | |
8f5f6178 GH |
2181 | if (ret > 0) |
2182 | ret = 0; | |
ac8e9819 CM |
2183 | err: |
2184 | sk->nr_items = num_found; | |
2185 | btrfs_free_path(path); | |
2186 | return ret; | |
2187 | } | |
2188 | ||
2189 | static noinline int btrfs_ioctl_tree_search(struct file *file, | |
2190 | void __user *argp) | |
2191 | { | |
ba346b35 GH |
2192 | struct btrfs_ioctl_search_args __user *uargs; |
2193 | struct btrfs_ioctl_search_key sk; | |
9b6e817d GH |
2194 | struct inode *inode; |
2195 | int ret; | |
2196 | size_t buf_size; | |
ac8e9819 CM |
2197 | |
2198 | if (!capable(CAP_SYS_ADMIN)) | |
2199 | return -EPERM; | |
2200 | ||
ba346b35 GH |
2201 | uargs = (struct btrfs_ioctl_search_args __user *)argp; |
2202 | ||
2203 | if (copy_from_user(&sk, &uargs->key, sizeof(sk))) | |
2204 | return -EFAULT; | |
ac8e9819 | 2205 | |
ba346b35 | 2206 | buf_size = sizeof(uargs->buf); |
ac8e9819 | 2207 | |
496ad9aa | 2208 | inode = file_inode(file); |
ba346b35 | 2209 | ret = search_ioctl(inode, &sk, &buf_size, uargs->buf); |
8f5f6178 GH |
2210 | |
2211 | /* | |
2212 | * In the origin implementation an overflow is handled by returning a | |
2213 | * search header with a len of zero, so reset ret. | |
2214 | */ | |
2215 | if (ret == -EOVERFLOW) | |
2216 | ret = 0; | |
2217 | ||
ba346b35 | 2218 | if (ret == 0 && copy_to_user(&uargs->key, &sk, sizeof(sk))) |
ac8e9819 | 2219 | ret = -EFAULT; |
ac8e9819 CM |
2220 | return ret; |
2221 | } | |
2222 | ||
cc68a8a5 GH |
2223 | static noinline int btrfs_ioctl_tree_search_v2(struct file *file, |
2224 | void __user *argp) | |
2225 | { | |
2226 | struct btrfs_ioctl_search_args_v2 __user *uarg; | |
2227 | struct btrfs_ioctl_search_args_v2 args; | |
2228 | struct inode *inode; | |
2229 | int ret; | |
2230 | size_t buf_size; | |
ee22184b | 2231 | const size_t buf_limit = SZ_16M; |
cc68a8a5 GH |
2232 | |
2233 | if (!capable(CAP_SYS_ADMIN)) | |
2234 | return -EPERM; | |
2235 | ||
2236 | /* copy search header and buffer size */ | |
2237 | uarg = (struct btrfs_ioctl_search_args_v2 __user *)argp; | |
2238 | if (copy_from_user(&args, uarg, sizeof(args))) | |
2239 | return -EFAULT; | |
2240 | ||
2241 | buf_size = args.buf_size; | |
2242 | ||
cc68a8a5 GH |
2243 | /* limit result size to 16MB */ |
2244 | if (buf_size > buf_limit) | |
2245 | buf_size = buf_limit; | |
2246 | ||
2247 | inode = file_inode(file); | |
2248 | ret = search_ioctl(inode, &args.key, &buf_size, | |
718dc5fa | 2249 | (char __user *)(&uarg->buf[0])); |
cc68a8a5 GH |
2250 | if (ret == 0 && copy_to_user(&uarg->key, &args.key, sizeof(args.key))) |
2251 | ret = -EFAULT; | |
2252 | else if (ret == -EOVERFLOW && | |
2253 | copy_to_user(&uarg->buf_size, &buf_size, sizeof(buf_size))) | |
2254 | ret = -EFAULT; | |
2255 | ||
ac8e9819 CM |
2256 | return ret; |
2257 | } | |
2258 | ||
98d377a0 | 2259 | /* |
ac8e9819 CM |
2260 | * Search INODE_REFs to identify path name of 'dirid' directory |
2261 | * in a 'tree_id' tree. and sets path name to 'name'. | |
2262 | */ | |
98d377a0 TH |
2263 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
2264 | u64 tree_id, u64 dirid, char *name) | |
2265 | { | |
2266 | struct btrfs_root *root; | |
2267 | struct btrfs_key key; | |
ac8e9819 | 2268 | char *ptr; |
98d377a0 TH |
2269 | int ret = -1; |
2270 | int slot; | |
2271 | int len; | |
2272 | int total_len = 0; | |
2273 | struct btrfs_inode_ref *iref; | |
2274 | struct extent_buffer *l; | |
2275 | struct btrfs_path *path; | |
2276 | ||
2277 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { | |
2278 | name[0]='\0'; | |
2279 | return 0; | |
2280 | } | |
2281 | ||
2282 | path = btrfs_alloc_path(); | |
2283 | if (!path) | |
2284 | return -ENOMEM; | |
2285 | ||
c8bcbfbd | 2286 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX - 1]; |
98d377a0 TH |
2287 | |
2288 | key.objectid = tree_id; | |
2289 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2290 | key.offset = (u64)-1; | |
2291 | root = btrfs_read_fs_root_no_name(info, &key); | |
2292 | if (IS_ERR(root)) { | |
ad1e3d56 | 2293 | ret = PTR_ERR(root); |
8ad6fcab | 2294 | goto out; |
98d377a0 TH |
2295 | } |
2296 | ||
2297 | key.objectid = dirid; | |
2298 | key.type = BTRFS_INODE_REF_KEY; | |
8ad6fcab | 2299 | key.offset = (u64)-1; |
98d377a0 | 2300 | |
67871254 | 2301 | while (1) { |
98d377a0 TH |
2302 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
2303 | if (ret < 0) | |
2304 | goto out; | |
18674c6c FDBM |
2305 | else if (ret > 0) { |
2306 | ret = btrfs_previous_item(root, path, dirid, | |
2307 | BTRFS_INODE_REF_KEY); | |
2308 | if (ret < 0) | |
2309 | goto out; | |
2310 | else if (ret > 0) { | |
2311 | ret = -ENOENT; | |
2312 | goto out; | |
2313 | } | |
2314 | } | |
98d377a0 TH |
2315 | |
2316 | l = path->nodes[0]; | |
2317 | slot = path->slots[0]; | |
2318 | btrfs_item_key_to_cpu(l, &key, slot); | |
2319 | ||
98d377a0 TH |
2320 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); |
2321 | len = btrfs_inode_ref_name_len(l, iref); | |
2322 | ptr -= len + 1; | |
2323 | total_len += len + 1; | |
a696cf35 FDBM |
2324 | if (ptr < name) { |
2325 | ret = -ENAMETOOLONG; | |
98d377a0 | 2326 | goto out; |
a696cf35 | 2327 | } |
98d377a0 TH |
2328 | |
2329 | *(ptr + len) = '/'; | |
67871254 | 2330 | read_extent_buffer(l, ptr, (unsigned long)(iref + 1), len); |
98d377a0 TH |
2331 | |
2332 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) | |
2333 | break; | |
2334 | ||
b3b4aa74 | 2335 | btrfs_release_path(path); |
98d377a0 | 2336 | key.objectid = key.offset; |
8ad6fcab | 2337 | key.offset = (u64)-1; |
98d377a0 | 2338 | dirid = key.objectid; |
98d377a0 | 2339 | } |
77906a50 | 2340 | memmove(name, ptr, total_len); |
67871254 | 2341 | name[total_len] = '\0'; |
98d377a0 TH |
2342 | ret = 0; |
2343 | out: | |
2344 | btrfs_free_path(path); | |
ac8e9819 CM |
2345 | return ret; |
2346 | } | |
2347 | ||
23d0b79d TM |
2348 | static int btrfs_search_path_in_tree_user(struct inode *inode, |
2349 | struct btrfs_ioctl_ino_lookup_user_args *args) | |
2350 | { | |
2351 | struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info; | |
2352 | struct super_block *sb = inode->i_sb; | |
2353 | struct btrfs_key upper_limit = BTRFS_I(inode)->location; | |
2354 | u64 treeid = BTRFS_I(inode)->root->root_key.objectid; | |
2355 | u64 dirid = args->dirid; | |
2356 | unsigned long item_off; | |
2357 | unsigned long item_len; | |
2358 | struct btrfs_inode_ref *iref; | |
2359 | struct btrfs_root_ref *rref; | |
2360 | struct btrfs_root *root; | |
2361 | struct btrfs_path *path; | |
2362 | struct btrfs_key key, key2; | |
2363 | struct extent_buffer *leaf; | |
2364 | struct inode *temp_inode; | |
2365 | char *ptr; | |
2366 | int slot; | |
2367 | int len; | |
2368 | int total_len = 0; | |
2369 | int ret; | |
2370 | ||
2371 | path = btrfs_alloc_path(); | |
2372 | if (!path) | |
2373 | return -ENOMEM; | |
2374 | ||
2375 | /* | |
2376 | * If the bottom subvolume does not exist directly under upper_limit, | |
2377 | * construct the path in from the bottom up. | |
2378 | */ | |
2379 | if (dirid != upper_limit.objectid) { | |
2380 | ptr = &args->path[BTRFS_INO_LOOKUP_USER_PATH_MAX - 1]; | |
2381 | ||
2382 | key.objectid = treeid; | |
2383 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2384 | key.offset = (u64)-1; | |
2385 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2386 | if (IS_ERR(root)) { | |
2387 | ret = PTR_ERR(root); | |
2388 | goto out; | |
2389 | } | |
2390 | ||
2391 | key.objectid = dirid; | |
2392 | key.type = BTRFS_INODE_REF_KEY; | |
2393 | key.offset = (u64)-1; | |
2394 | while (1) { | |
2395 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2396 | if (ret < 0) { | |
2397 | goto out; | |
2398 | } else if (ret > 0) { | |
2399 | ret = btrfs_previous_item(root, path, dirid, | |
2400 | BTRFS_INODE_REF_KEY); | |
2401 | if (ret < 0) { | |
2402 | goto out; | |
2403 | } else if (ret > 0) { | |
2404 | ret = -ENOENT; | |
2405 | goto out; | |
2406 | } | |
2407 | } | |
2408 | ||
2409 | leaf = path->nodes[0]; | |
2410 | slot = path->slots[0]; | |
2411 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2412 | ||
2413 | iref = btrfs_item_ptr(leaf, slot, struct btrfs_inode_ref); | |
2414 | len = btrfs_inode_ref_name_len(leaf, iref); | |
2415 | ptr -= len + 1; | |
2416 | total_len += len + 1; | |
2417 | if (ptr < args->path) { | |
2418 | ret = -ENAMETOOLONG; | |
2419 | goto out; | |
2420 | } | |
2421 | ||
2422 | *(ptr + len) = '/'; | |
2423 | read_extent_buffer(leaf, ptr, | |
2424 | (unsigned long)(iref + 1), len); | |
2425 | ||
2426 | /* Check the read+exec permission of this directory */ | |
2427 | ret = btrfs_previous_item(root, path, dirid, | |
2428 | BTRFS_INODE_ITEM_KEY); | |
2429 | if (ret < 0) { | |
2430 | goto out; | |
2431 | } else if (ret > 0) { | |
2432 | ret = -ENOENT; | |
2433 | goto out; | |
2434 | } | |
2435 | ||
2436 | leaf = path->nodes[0]; | |
2437 | slot = path->slots[0]; | |
2438 | btrfs_item_key_to_cpu(leaf, &key2, slot); | |
2439 | if (key2.objectid != dirid) { | |
2440 | ret = -ENOENT; | |
2441 | goto out; | |
2442 | } | |
2443 | ||
2444 | temp_inode = btrfs_iget(sb, &key2, root, NULL); | |
3ca57bd6 MT |
2445 | if (IS_ERR(temp_inode)) { |
2446 | ret = PTR_ERR(temp_inode); | |
2447 | goto out; | |
2448 | } | |
23d0b79d TM |
2449 | ret = inode_permission(temp_inode, MAY_READ | MAY_EXEC); |
2450 | iput(temp_inode); | |
2451 | if (ret) { | |
2452 | ret = -EACCES; | |
2453 | goto out; | |
2454 | } | |
2455 | ||
2456 | if (key.offset == upper_limit.objectid) | |
2457 | break; | |
2458 | if (key.objectid == BTRFS_FIRST_FREE_OBJECTID) { | |
2459 | ret = -EACCES; | |
2460 | goto out; | |
2461 | } | |
2462 | ||
2463 | btrfs_release_path(path); | |
2464 | key.objectid = key.offset; | |
2465 | key.offset = (u64)-1; | |
2466 | dirid = key.objectid; | |
2467 | } | |
2468 | ||
2469 | memmove(args->path, ptr, total_len); | |
2470 | args->path[total_len] = '\0'; | |
2471 | btrfs_release_path(path); | |
2472 | } | |
2473 | ||
2474 | /* Get the bottom subvolume's name from ROOT_REF */ | |
2475 | root = fs_info->tree_root; | |
2476 | key.objectid = treeid; | |
2477 | key.type = BTRFS_ROOT_REF_KEY; | |
2478 | key.offset = args->treeid; | |
2479 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2480 | if (ret < 0) { | |
2481 | goto out; | |
2482 | } else if (ret > 0) { | |
2483 | ret = -ENOENT; | |
2484 | goto out; | |
2485 | } | |
2486 | ||
2487 | leaf = path->nodes[0]; | |
2488 | slot = path->slots[0]; | |
2489 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2490 | ||
2491 | item_off = btrfs_item_ptr_offset(leaf, slot); | |
2492 | item_len = btrfs_item_size_nr(leaf, slot); | |
2493 | /* Check if dirid in ROOT_REF corresponds to passed dirid */ | |
2494 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); | |
2495 | if (args->dirid != btrfs_root_ref_dirid(leaf, rref)) { | |
2496 | ret = -EINVAL; | |
2497 | goto out; | |
2498 | } | |
2499 | ||
2500 | /* Copy subvolume's name */ | |
2501 | item_off += sizeof(struct btrfs_root_ref); | |
2502 | item_len -= sizeof(struct btrfs_root_ref); | |
2503 | read_extent_buffer(leaf, args->name, item_off, item_len); | |
2504 | args->name[item_len] = 0; | |
2505 | ||
2506 | out: | |
2507 | btrfs_free_path(path); | |
2508 | return ret; | |
2509 | } | |
2510 | ||
ac8e9819 CM |
2511 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, |
2512 | void __user *argp) | |
2513 | { | |
bece2e82 BVA |
2514 | struct btrfs_ioctl_ino_lookup_args *args; |
2515 | struct inode *inode; | |
01b810b8 | 2516 | int ret = 0; |
ac8e9819 | 2517 | |
2354d08f JL |
2518 | args = memdup_user(argp, sizeof(*args)); |
2519 | if (IS_ERR(args)) | |
2520 | return PTR_ERR(args); | |
c2b96929 | 2521 | |
496ad9aa | 2522 | inode = file_inode(file); |
ac8e9819 | 2523 | |
01b810b8 DS |
2524 | /* |
2525 | * Unprivileged query to obtain the containing subvolume root id. The | |
2526 | * path is reset so it's consistent with btrfs_search_path_in_tree. | |
2527 | */ | |
1b53ac4d CM |
2528 | if (args->treeid == 0) |
2529 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; | |
2530 | ||
01b810b8 DS |
2531 | if (args->objectid == BTRFS_FIRST_FREE_OBJECTID) { |
2532 | args->name[0] = 0; | |
2533 | goto out; | |
2534 | } | |
2535 | ||
2536 | if (!capable(CAP_SYS_ADMIN)) { | |
2537 | ret = -EPERM; | |
2538 | goto out; | |
2539 | } | |
2540 | ||
ac8e9819 CM |
2541 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
2542 | args->treeid, args->objectid, | |
2543 | args->name); | |
2544 | ||
01b810b8 | 2545 | out: |
ac8e9819 CM |
2546 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) |
2547 | ret = -EFAULT; | |
2548 | ||
2549 | kfree(args); | |
98d377a0 TH |
2550 | return ret; |
2551 | } | |
2552 | ||
23d0b79d TM |
2553 | /* |
2554 | * Version of ino_lookup ioctl (unprivileged) | |
2555 | * | |
2556 | * The main differences from ino_lookup ioctl are: | |
2557 | * | |
2558 | * 1. Read + Exec permission will be checked using inode_permission() during | |
2559 | * path construction. -EACCES will be returned in case of failure. | |
2560 | * 2. Path construction will be stopped at the inode number which corresponds | |
2561 | * to the fd with which this ioctl is called. If constructed path does not | |
2562 | * exist under fd's inode, -EACCES will be returned. | |
2563 | * 3. The name of bottom subvolume is also searched and filled. | |
2564 | */ | |
2565 | static int btrfs_ioctl_ino_lookup_user(struct file *file, void __user *argp) | |
2566 | { | |
2567 | struct btrfs_ioctl_ino_lookup_user_args *args; | |
2568 | struct inode *inode; | |
2569 | int ret; | |
2570 | ||
2571 | args = memdup_user(argp, sizeof(*args)); | |
2572 | if (IS_ERR(args)) | |
2573 | return PTR_ERR(args); | |
2574 | ||
2575 | inode = file_inode(file); | |
2576 | ||
2577 | if (args->dirid == BTRFS_FIRST_FREE_OBJECTID && | |
2578 | BTRFS_I(inode)->location.objectid != BTRFS_FIRST_FREE_OBJECTID) { | |
2579 | /* | |
2580 | * The subvolume does not exist under fd with which this is | |
2581 | * called | |
2582 | */ | |
2583 | kfree(args); | |
2584 | return -EACCES; | |
2585 | } | |
2586 | ||
2587 | ret = btrfs_search_path_in_tree_user(inode, args); | |
2588 | ||
2589 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) | |
2590 | ret = -EFAULT; | |
2591 | ||
2592 | kfree(args); | |
2593 | return ret; | |
2594 | } | |
2595 | ||
b64ec075 TM |
2596 | /* Get the subvolume information in BTRFS_ROOT_ITEM and BTRFS_ROOT_BACKREF */ |
2597 | static int btrfs_ioctl_get_subvol_info(struct file *file, void __user *argp) | |
2598 | { | |
2599 | struct btrfs_ioctl_get_subvol_info_args *subvol_info; | |
2600 | struct btrfs_fs_info *fs_info; | |
2601 | struct btrfs_root *root; | |
2602 | struct btrfs_path *path; | |
2603 | struct btrfs_key key; | |
2604 | struct btrfs_root_item *root_item; | |
2605 | struct btrfs_root_ref *rref; | |
2606 | struct extent_buffer *leaf; | |
2607 | unsigned long item_off; | |
2608 | unsigned long item_len; | |
2609 | struct inode *inode; | |
2610 | int slot; | |
2611 | int ret = 0; | |
2612 | ||
2613 | path = btrfs_alloc_path(); | |
2614 | if (!path) | |
2615 | return -ENOMEM; | |
2616 | ||
2617 | subvol_info = kzalloc(sizeof(*subvol_info), GFP_KERNEL); | |
2618 | if (!subvol_info) { | |
2619 | btrfs_free_path(path); | |
2620 | return -ENOMEM; | |
2621 | } | |
2622 | ||
2623 | inode = file_inode(file); | |
2624 | fs_info = BTRFS_I(inode)->root->fs_info; | |
2625 | ||
2626 | /* Get root_item of inode's subvolume */ | |
2627 | key.objectid = BTRFS_I(inode)->root->root_key.objectid; | |
2628 | key.type = BTRFS_ROOT_ITEM_KEY; | |
2629 | key.offset = (u64)-1; | |
2630 | root = btrfs_read_fs_root_no_name(fs_info, &key); | |
2631 | if (IS_ERR(root)) { | |
2632 | ret = PTR_ERR(root); | |
2633 | goto out; | |
2634 | } | |
2635 | root_item = &root->root_item; | |
2636 | ||
2637 | subvol_info->treeid = key.objectid; | |
2638 | ||
2639 | subvol_info->generation = btrfs_root_generation(root_item); | |
2640 | subvol_info->flags = btrfs_root_flags(root_item); | |
2641 | ||
2642 | memcpy(subvol_info->uuid, root_item->uuid, BTRFS_UUID_SIZE); | |
2643 | memcpy(subvol_info->parent_uuid, root_item->parent_uuid, | |
2644 | BTRFS_UUID_SIZE); | |
2645 | memcpy(subvol_info->received_uuid, root_item->received_uuid, | |
2646 | BTRFS_UUID_SIZE); | |
2647 | ||
2648 | subvol_info->ctransid = btrfs_root_ctransid(root_item); | |
2649 | subvol_info->ctime.sec = btrfs_stack_timespec_sec(&root_item->ctime); | |
2650 | subvol_info->ctime.nsec = btrfs_stack_timespec_nsec(&root_item->ctime); | |
2651 | ||
2652 | subvol_info->otransid = btrfs_root_otransid(root_item); | |
2653 | subvol_info->otime.sec = btrfs_stack_timespec_sec(&root_item->otime); | |
2654 | subvol_info->otime.nsec = btrfs_stack_timespec_nsec(&root_item->otime); | |
2655 | ||
2656 | subvol_info->stransid = btrfs_root_stransid(root_item); | |
2657 | subvol_info->stime.sec = btrfs_stack_timespec_sec(&root_item->stime); | |
2658 | subvol_info->stime.nsec = btrfs_stack_timespec_nsec(&root_item->stime); | |
2659 | ||
2660 | subvol_info->rtransid = btrfs_root_rtransid(root_item); | |
2661 | subvol_info->rtime.sec = btrfs_stack_timespec_sec(&root_item->rtime); | |
2662 | subvol_info->rtime.nsec = btrfs_stack_timespec_nsec(&root_item->rtime); | |
2663 | ||
2664 | if (key.objectid != BTRFS_FS_TREE_OBJECTID) { | |
2665 | /* Search root tree for ROOT_BACKREF of this subvolume */ | |
2666 | root = fs_info->tree_root; | |
2667 | ||
2668 | key.type = BTRFS_ROOT_BACKREF_KEY; | |
2669 | key.offset = 0; | |
2670 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2671 | if (ret < 0) { | |
2672 | goto out; | |
2673 | } else if (path->slots[0] >= | |
2674 | btrfs_header_nritems(path->nodes[0])) { | |
2675 | ret = btrfs_next_leaf(root, path); | |
2676 | if (ret < 0) { | |
2677 | goto out; | |
2678 | } else if (ret > 0) { | |
2679 | ret = -EUCLEAN; | |
2680 | goto out; | |
2681 | } | |
2682 | } | |
2683 | ||
2684 | leaf = path->nodes[0]; | |
2685 | slot = path->slots[0]; | |
2686 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2687 | if (key.objectid == subvol_info->treeid && | |
2688 | key.type == BTRFS_ROOT_BACKREF_KEY) { | |
2689 | subvol_info->parent_id = key.offset; | |
2690 | ||
2691 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); | |
2692 | subvol_info->dirid = btrfs_root_ref_dirid(leaf, rref); | |
2693 | ||
2694 | item_off = btrfs_item_ptr_offset(leaf, slot) | |
2695 | + sizeof(struct btrfs_root_ref); | |
2696 | item_len = btrfs_item_size_nr(leaf, slot) | |
2697 | - sizeof(struct btrfs_root_ref); | |
2698 | read_extent_buffer(leaf, subvol_info->name, | |
2699 | item_off, item_len); | |
2700 | } else { | |
2701 | ret = -ENOENT; | |
2702 | goto out; | |
2703 | } | |
2704 | } | |
2705 | ||
2706 | if (copy_to_user(argp, subvol_info, sizeof(*subvol_info))) | |
2707 | ret = -EFAULT; | |
2708 | ||
2709 | out: | |
2710 | btrfs_free_path(path); | |
2711 | kzfree(subvol_info); | |
2712 | return ret; | |
2713 | } | |
2714 | ||
42e4b520 TM |
2715 | /* |
2716 | * Return ROOT_REF information of the subvolume containing this inode | |
2717 | * except the subvolume name. | |
2718 | */ | |
2719 | static int btrfs_ioctl_get_subvol_rootref(struct file *file, void __user *argp) | |
2720 | { | |
2721 | struct btrfs_ioctl_get_subvol_rootref_args *rootrefs; | |
2722 | struct btrfs_root_ref *rref; | |
2723 | struct btrfs_root *root; | |
2724 | struct btrfs_path *path; | |
2725 | struct btrfs_key key; | |
2726 | struct extent_buffer *leaf; | |
2727 | struct inode *inode; | |
2728 | u64 objectid; | |
2729 | int slot; | |
2730 | int ret; | |
2731 | u8 found; | |
2732 | ||
2733 | path = btrfs_alloc_path(); | |
2734 | if (!path) | |
2735 | return -ENOMEM; | |
2736 | ||
2737 | rootrefs = memdup_user(argp, sizeof(*rootrefs)); | |
2738 | if (IS_ERR(rootrefs)) { | |
2739 | btrfs_free_path(path); | |
2740 | return PTR_ERR(rootrefs); | |
2741 | } | |
2742 | ||
2743 | inode = file_inode(file); | |
2744 | root = BTRFS_I(inode)->root->fs_info->tree_root; | |
2745 | objectid = BTRFS_I(inode)->root->root_key.objectid; | |
2746 | ||
2747 | key.objectid = objectid; | |
2748 | key.type = BTRFS_ROOT_REF_KEY; | |
2749 | key.offset = rootrefs->min_treeid; | |
2750 | found = 0; | |
2751 | ||
2752 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2753 | if (ret < 0) { | |
2754 | goto out; | |
2755 | } else if (path->slots[0] >= | |
2756 | btrfs_header_nritems(path->nodes[0])) { | |
2757 | ret = btrfs_next_leaf(root, path); | |
2758 | if (ret < 0) { | |
2759 | goto out; | |
2760 | } else if (ret > 0) { | |
2761 | ret = -EUCLEAN; | |
2762 | goto out; | |
2763 | } | |
2764 | } | |
2765 | while (1) { | |
2766 | leaf = path->nodes[0]; | |
2767 | slot = path->slots[0]; | |
2768 | ||
2769 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
2770 | if (key.objectid != objectid || key.type != BTRFS_ROOT_REF_KEY) { | |
2771 | ret = 0; | |
2772 | goto out; | |
2773 | } | |
2774 | ||
2775 | if (found == BTRFS_MAX_ROOTREF_BUFFER_NUM) { | |
2776 | ret = -EOVERFLOW; | |
2777 | goto out; | |
2778 | } | |
2779 | ||
2780 | rref = btrfs_item_ptr(leaf, slot, struct btrfs_root_ref); | |
2781 | rootrefs->rootref[found].treeid = key.offset; | |
2782 | rootrefs->rootref[found].dirid = | |
2783 | btrfs_root_ref_dirid(leaf, rref); | |
2784 | found++; | |
2785 | ||
2786 | ret = btrfs_next_item(root, path); | |
2787 | if (ret < 0) { | |
2788 | goto out; | |
2789 | } else if (ret > 0) { | |
2790 | ret = -EUCLEAN; | |
2791 | goto out; | |
2792 | } | |
2793 | } | |
2794 | ||
2795 | out: | |
2796 | if (!ret || ret == -EOVERFLOW) { | |
2797 | rootrefs->num_items = found; | |
2798 | /* update min_treeid for next search */ | |
2799 | if (found) | |
2800 | rootrefs->min_treeid = | |
2801 | rootrefs->rootref[found - 1].treeid + 1; | |
2802 | if (copy_to_user(argp, rootrefs, sizeof(*rootrefs))) | |
2803 | ret = -EFAULT; | |
2804 | } | |
2805 | ||
2806 | kfree(rootrefs); | |
2807 | btrfs_free_path(path); | |
2808 | ||
2809 | return ret; | |
2810 | } | |
2811 | ||
76dda93c YZ |
2812 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
2813 | void __user *arg) | |
2814 | { | |
54563d41 | 2815 | struct dentry *parent = file->f_path.dentry; |
0b246afa | 2816 | struct btrfs_fs_info *fs_info = btrfs_sb(parent->d_sb); |
76dda93c | 2817 | struct dentry *dentry; |
2b0143b5 | 2818 | struct inode *dir = d_inode(parent); |
76dda93c YZ |
2819 | struct inode *inode; |
2820 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
2821 | struct btrfs_root *dest = NULL; | |
2822 | struct btrfs_ioctl_vol_args *vol_args; | |
76dda93c | 2823 | int namelen; |
76dda93c YZ |
2824 | int err = 0; |
2825 | ||
325c50e3 JM |
2826 | if (!S_ISDIR(dir->i_mode)) |
2827 | return -ENOTDIR; | |
2828 | ||
76dda93c YZ |
2829 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
2830 | if (IS_ERR(vol_args)) | |
2831 | return PTR_ERR(vol_args); | |
2832 | ||
2833 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
2834 | namelen = strlen(vol_args->name); | |
2835 | if (strchr(vol_args->name, '/') || | |
2836 | strncmp(vol_args->name, "..", namelen) == 0) { | |
2837 | err = -EINVAL; | |
2838 | goto out; | |
2839 | } | |
2840 | ||
a561be71 | 2841 | err = mnt_want_write_file(file); |
76dda93c YZ |
2842 | if (err) |
2843 | goto out; | |
2844 | ||
521e0546 | 2845 | |
00235411 AV |
2846 | err = down_write_killable_nested(&dir->i_rwsem, I_MUTEX_PARENT); |
2847 | if (err == -EINTR) | |
2848 | goto out_drop_write; | |
76dda93c YZ |
2849 | dentry = lookup_one_len(vol_args->name, parent, namelen); |
2850 | if (IS_ERR(dentry)) { | |
2851 | err = PTR_ERR(dentry); | |
2852 | goto out_unlock_dir; | |
2853 | } | |
2854 | ||
2b0143b5 | 2855 | if (d_really_is_negative(dentry)) { |
76dda93c YZ |
2856 | err = -ENOENT; |
2857 | goto out_dput; | |
2858 | } | |
2859 | ||
2b0143b5 | 2860 | inode = d_inode(dentry); |
4260f7c7 | 2861 | dest = BTRFS_I(inode)->root; |
67871254 | 2862 | if (!capable(CAP_SYS_ADMIN)) { |
4260f7c7 SW |
2863 | /* |
2864 | * Regular user. Only allow this with a special mount | |
2865 | * option, when the user has write+exec access to the | |
2866 | * subvol root, and when rmdir(2) would have been | |
2867 | * allowed. | |
2868 | * | |
2869 | * Note that this is _not_ check that the subvol is | |
2870 | * empty or doesn't contain data that we wouldn't | |
2871 | * otherwise be able to delete. | |
2872 | * | |
2873 | * Users who want to delete empty subvols should try | |
2874 | * rmdir(2). | |
2875 | */ | |
2876 | err = -EPERM; | |
0b246afa | 2877 | if (!btrfs_test_opt(fs_info, USER_SUBVOL_RM_ALLOWED)) |
4260f7c7 SW |
2878 | goto out_dput; |
2879 | ||
2880 | /* | |
2881 | * Do not allow deletion if the parent dir is the same | |
2882 | * as the dir to be deleted. That means the ioctl | |
2883 | * must be called on the dentry referencing the root | |
2884 | * of the subvol, not a random directory contained | |
2885 | * within it. | |
2886 | */ | |
2887 | err = -EINVAL; | |
2888 | if (root == dest) | |
2889 | goto out_dput; | |
2890 | ||
2891 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); | |
2892 | if (err) | |
2893 | goto out_dput; | |
4260f7c7 SW |
2894 | } |
2895 | ||
5c39da5b MX |
2896 | /* check if subvolume may be deleted by a user */ |
2897 | err = btrfs_may_delete(dir, dentry, 1); | |
2898 | if (err) | |
2899 | goto out_dput; | |
2900 | ||
4a0cc7ca | 2901 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
76dda93c YZ |
2902 | err = -EINVAL; |
2903 | goto out_dput; | |
2904 | } | |
2905 | ||
5955102c | 2906 | inode_lock(inode); |
f60a2364 | 2907 | err = btrfs_delete_subvolume(dir, dentry); |
5955102c | 2908 | inode_unlock(inode); |
f60a2364 | 2909 | if (!err) |
76dda93c | 2910 | d_delete(dentry); |
fa6ac876 | 2911 | |
76dda93c YZ |
2912 | out_dput: |
2913 | dput(dentry); | |
2914 | out_unlock_dir: | |
5955102c | 2915 | inode_unlock(dir); |
00235411 | 2916 | out_drop_write: |
2a79f17e | 2917 | mnt_drop_write_file(file); |
76dda93c YZ |
2918 | out: |
2919 | kfree(vol_args); | |
2920 | return err; | |
2921 | } | |
2922 | ||
1e701a32 | 2923 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
f46b5a66 | 2924 | { |
496ad9aa | 2925 | struct inode *inode = file_inode(file); |
f46b5a66 | 2926 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1e701a32 | 2927 | struct btrfs_ioctl_defrag_range_args *range; |
c146afad YZ |
2928 | int ret; |
2929 | ||
25122d15 ID |
2930 | ret = mnt_want_write_file(file); |
2931 | if (ret) | |
2932 | return ret; | |
b83cc969 | 2933 | |
25122d15 ID |
2934 | if (btrfs_root_readonly(root)) { |
2935 | ret = -EROFS; | |
2936 | goto out; | |
5ac00add | 2937 | } |
f46b5a66 CH |
2938 | |
2939 | switch (inode->i_mode & S_IFMT) { | |
2940 | case S_IFDIR: | |
e441d54d CM |
2941 | if (!capable(CAP_SYS_ADMIN)) { |
2942 | ret = -EPERM; | |
2943 | goto out; | |
2944 | } | |
de78b51a | 2945 | ret = btrfs_defrag_root(root); |
f46b5a66 CH |
2946 | break; |
2947 | case S_IFREG: | |
616d374e AB |
2948 | /* |
2949 | * Note that this does not check the file descriptor for write | |
2950 | * access. This prevents defragmenting executables that are | |
2951 | * running and allows defrag on files open in read-only mode. | |
2952 | */ | |
2953 | if (!capable(CAP_SYS_ADMIN) && | |
2954 | inode_permission(inode, MAY_WRITE)) { | |
2955 | ret = -EPERM; | |
e441d54d CM |
2956 | goto out; |
2957 | } | |
1e701a32 CM |
2958 | |
2959 | range = kzalloc(sizeof(*range), GFP_KERNEL); | |
2960 | if (!range) { | |
2961 | ret = -ENOMEM; | |
2962 | goto out; | |
2963 | } | |
2964 | ||
2965 | if (argp) { | |
2966 | if (copy_from_user(range, argp, | |
2967 | sizeof(*range))) { | |
2968 | ret = -EFAULT; | |
2969 | kfree(range); | |
683be16e | 2970 | goto out; |
1e701a32 CM |
2971 | } |
2972 | /* compression requires us to start the IO */ | |
2973 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { | |
2974 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; | |
2975 | range->extent_thresh = (u32)-1; | |
2976 | } | |
2977 | } else { | |
2978 | /* the rest are all set to zero by kzalloc */ | |
2979 | range->len = (u64)-1; | |
2980 | } | |
496ad9aa | 2981 | ret = btrfs_defrag_file(file_inode(file), file, |
7c829b72 | 2982 | range, BTRFS_OLDEST_GENERATION, 0); |
4cb5300b CM |
2983 | if (ret > 0) |
2984 | ret = 0; | |
1e701a32 | 2985 | kfree(range); |
f46b5a66 | 2986 | break; |
8929ecfa YZ |
2987 | default: |
2988 | ret = -EINVAL; | |
f46b5a66 | 2989 | } |
e441d54d | 2990 | out: |
25122d15 | 2991 | mnt_drop_write_file(file); |
e441d54d | 2992 | return ret; |
f46b5a66 CH |
2993 | } |
2994 | ||
2ff7e61e | 2995 | static long btrfs_ioctl_add_dev(struct btrfs_fs_info *fs_info, void __user *arg) |
f46b5a66 CH |
2996 | { |
2997 | struct btrfs_ioctl_vol_args *vol_args; | |
2998 | int ret; | |
2999 | ||
e441d54d CM |
3000 | if (!capable(CAP_SYS_ADMIN)) |
3001 | return -EPERM; | |
3002 | ||
171938e5 | 3003 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) |
e57138b3 | 3004 | return BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
c9e9f97b | 3005 | |
dae7b665 | 3006 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
3007 | if (IS_ERR(vol_args)) { |
3008 | ret = PTR_ERR(vol_args); | |
3009 | goto out; | |
3010 | } | |
f46b5a66 | 3011 | |
5516e595 | 3012 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
2ff7e61e | 3013 | ret = btrfs_init_new_device(fs_info, vol_args->name); |
f46b5a66 | 3014 | |
43d20761 | 3015 | if (!ret) |
0b246afa | 3016 | btrfs_info(fs_info, "disk added %s", vol_args->name); |
43d20761 | 3017 | |
f46b5a66 | 3018 | kfree(vol_args); |
c9e9f97b | 3019 | out: |
171938e5 | 3020 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
f46b5a66 CH |
3021 | return ret; |
3022 | } | |
3023 | ||
6b526ed7 | 3024 | static long btrfs_ioctl_rm_dev_v2(struct file *file, void __user *arg) |
f46b5a66 | 3025 | { |
0b246afa JM |
3026 | struct inode *inode = file_inode(file); |
3027 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
6b526ed7 | 3028 | struct btrfs_ioctl_vol_args_v2 *vol_args; |
f46b5a66 CH |
3029 | int ret; |
3030 | ||
e441d54d CM |
3031 | if (!capable(CAP_SYS_ADMIN)) |
3032 | return -EPERM; | |
3033 | ||
da24927b MX |
3034 | ret = mnt_want_write_file(file); |
3035 | if (ret) | |
3036 | return ret; | |
c146afad | 3037 | |
dae7b665 | 3038 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
3039 | if (IS_ERR(vol_args)) { |
3040 | ret = PTR_ERR(vol_args); | |
c47ca32d | 3041 | goto err_drop; |
c9e9f97b | 3042 | } |
f46b5a66 | 3043 | |
6b526ed7 | 3044 | /* Check for compatibility reject unknown flags */ |
fd4e994b OS |
3045 | if (vol_args->flags & ~BTRFS_VOL_ARG_V2_FLAGS_SUPPORTED) { |
3046 | ret = -EOPNOTSUPP; | |
3047 | goto out; | |
3048 | } | |
f46b5a66 | 3049 | |
171938e5 | 3050 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
183860f6 AJ |
3051 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
3052 | goto out; | |
3053 | } | |
3054 | ||
735654ea | 3055 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) { |
2ff7e61e | 3056 | ret = btrfs_rm_device(fs_info, NULL, vol_args->devid); |
6b526ed7 AJ |
3057 | } else { |
3058 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; | |
2ff7e61e | 3059 | ret = btrfs_rm_device(fs_info, vol_args->name, 0); |
6b526ed7 | 3060 | } |
171938e5 | 3061 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
183860f6 | 3062 | |
6b526ed7 | 3063 | if (!ret) { |
735654ea | 3064 | if (vol_args->flags & BTRFS_DEVICE_SPEC_BY_ID) |
0b246afa | 3065 | btrfs_info(fs_info, "device deleted: id %llu", |
6b526ed7 AJ |
3066 | vol_args->devid); |
3067 | else | |
0b246afa | 3068 | btrfs_info(fs_info, "device deleted: %s", |
6b526ed7 AJ |
3069 | vol_args->name); |
3070 | } | |
183860f6 AJ |
3071 | out: |
3072 | kfree(vol_args); | |
c47ca32d | 3073 | err_drop: |
4ac20c70 | 3074 | mnt_drop_write_file(file); |
f46b5a66 CH |
3075 | return ret; |
3076 | } | |
3077 | ||
da24927b | 3078 | static long btrfs_ioctl_rm_dev(struct file *file, void __user *arg) |
f46b5a66 | 3079 | { |
0b246afa JM |
3080 | struct inode *inode = file_inode(file); |
3081 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
f46b5a66 CH |
3082 | struct btrfs_ioctl_vol_args *vol_args; |
3083 | int ret; | |
3084 | ||
e441d54d CM |
3085 | if (!capable(CAP_SYS_ADMIN)) |
3086 | return -EPERM; | |
3087 | ||
da24927b MX |
3088 | ret = mnt_want_write_file(file); |
3089 | if (ret) | |
3090 | return ret; | |
c146afad | 3091 | |
171938e5 | 3092 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
183860f6 | 3093 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
58d7bbf8 DS |
3094 | goto out_drop_write; |
3095 | } | |
3096 | ||
3097 | vol_args = memdup_user(arg, sizeof(*vol_args)); | |
3098 | if (IS_ERR(vol_args)) { | |
3099 | ret = PTR_ERR(vol_args); | |
183860f6 AJ |
3100 | goto out; |
3101 | } | |
3102 | ||
58d7bbf8 | 3103 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
2ff7e61e | 3104 | ret = btrfs_rm_device(fs_info, vol_args->name, 0); |
183860f6 | 3105 | |
ec95d491 | 3106 | if (!ret) |
0b246afa | 3107 | btrfs_info(fs_info, "disk deleted %s", vol_args->name); |
183860f6 | 3108 | kfree(vol_args); |
58d7bbf8 | 3109 | out: |
171938e5 | 3110 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
58d7bbf8 | 3111 | out_drop_write: |
4ac20c70 | 3112 | mnt_drop_write_file(file); |
58d7bbf8 | 3113 | |
f46b5a66 CH |
3114 | return ret; |
3115 | } | |
3116 | ||
2ff7e61e JM |
3117 | static long btrfs_ioctl_fs_info(struct btrfs_fs_info *fs_info, |
3118 | void __user *arg) | |
475f6387 | 3119 | { |
027ed2f0 | 3120 | struct btrfs_ioctl_fs_info_args *fi_args; |
475f6387 | 3121 | struct btrfs_device *device; |
0b246afa | 3122 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
027ed2f0 | 3123 | int ret = 0; |
475f6387 | 3124 | |
027ed2f0 LZ |
3125 | fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); |
3126 | if (!fi_args) | |
3127 | return -ENOMEM; | |
3128 | ||
d03262c7 | 3129 | rcu_read_lock(); |
027ed2f0 | 3130 | fi_args->num_devices = fs_devices->num_devices; |
475f6387 | 3131 | |
d03262c7 | 3132 | list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) { |
027ed2f0 LZ |
3133 | if (device->devid > fi_args->max_id) |
3134 | fi_args->max_id = device->devid; | |
475f6387 | 3135 | } |
d03262c7 | 3136 | rcu_read_unlock(); |
475f6387 | 3137 | |
d03262c7 | 3138 | memcpy(&fi_args->fsid, fs_info->fsid, sizeof(fi_args->fsid)); |
bea7eafd OS |
3139 | fi_args->nodesize = fs_info->nodesize; |
3140 | fi_args->sectorsize = fs_info->sectorsize; | |
3141 | fi_args->clone_alignment = fs_info->sectorsize; | |
80a773fb | 3142 | |
027ed2f0 LZ |
3143 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) |
3144 | ret = -EFAULT; | |
475f6387 | 3145 | |
027ed2f0 LZ |
3146 | kfree(fi_args); |
3147 | return ret; | |
475f6387 JS |
3148 | } |
3149 | ||
2ff7e61e JM |
3150 | static long btrfs_ioctl_dev_info(struct btrfs_fs_info *fs_info, |
3151 | void __user *arg) | |
475f6387 JS |
3152 | { |
3153 | struct btrfs_ioctl_dev_info_args *di_args; | |
3154 | struct btrfs_device *dev; | |
475f6387 JS |
3155 | int ret = 0; |
3156 | char *s_uuid = NULL; | |
475f6387 | 3157 | |
475f6387 JS |
3158 | di_args = memdup_user(arg, sizeof(*di_args)); |
3159 | if (IS_ERR(di_args)) | |
3160 | return PTR_ERR(di_args); | |
3161 | ||
dd5f9615 | 3162 | if (!btrfs_is_empty_uuid(di_args->uuid)) |
475f6387 JS |
3163 | s_uuid = di_args->uuid; |
3164 | ||
c5593ca3 | 3165 | rcu_read_lock(); |
0b246afa | 3166 | dev = btrfs_find_device(fs_info, di_args->devid, s_uuid, NULL); |
475f6387 JS |
3167 | |
3168 | if (!dev) { | |
3169 | ret = -ENODEV; | |
3170 | goto out; | |
3171 | } | |
3172 | ||
3173 | di_args->devid = dev->devid; | |
7cc8e58d MX |
3174 | di_args->bytes_used = btrfs_device_get_bytes_used(dev); |
3175 | di_args->total_bytes = btrfs_device_get_total_bytes(dev); | |
475f6387 | 3176 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); |
a27202fb | 3177 | if (dev->name) { |
672d5990 MT |
3178 | strncpy(di_args->path, rcu_str_deref(dev->name), |
3179 | sizeof(di_args->path) - 1); | |
a27202fb JM |
3180 | di_args->path[sizeof(di_args->path) - 1] = 0; |
3181 | } else { | |
99ba55ad | 3182 | di_args->path[0] = '\0'; |
a27202fb | 3183 | } |
475f6387 JS |
3184 | |
3185 | out: | |
c5593ca3 | 3186 | rcu_read_unlock(); |
475f6387 JS |
3187 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) |
3188 | ret = -EFAULT; | |
3189 | ||
3190 | kfree(di_args); | |
3191 | return ret; | |
3192 | } | |
3193 | ||
f4414602 | 3194 | static struct page *extent_same_get_page(struct inode *inode, pgoff_t index) |
416161db MF |
3195 | { |
3196 | struct page *page; | |
416161db | 3197 | |
416161db MF |
3198 | page = grab_cache_page(inode->i_mapping, index); |
3199 | if (!page) | |
31314002 | 3200 | return ERR_PTR(-ENOMEM); |
416161db MF |
3201 | |
3202 | if (!PageUptodate(page)) { | |
31314002 FM |
3203 | int ret; |
3204 | ||
3205 | ret = btrfs_readpage(NULL, page); | |
3206 | if (ret) | |
3207 | return ERR_PTR(ret); | |
416161db MF |
3208 | lock_page(page); |
3209 | if (!PageUptodate(page)) { | |
3210 | unlock_page(page); | |
09cbfeaf | 3211 | put_page(page); |
31314002 FM |
3212 | return ERR_PTR(-EIO); |
3213 | } | |
3214 | if (page->mapping != inode->i_mapping) { | |
3215 | unlock_page(page); | |
09cbfeaf | 3216 | put_page(page); |
31314002 | 3217 | return ERR_PTR(-EAGAIN); |
416161db MF |
3218 | } |
3219 | } | |
416161db MF |
3220 | |
3221 | return page; | |
3222 | } | |
3223 | ||
f4414602 MF |
3224 | static int gather_extent_pages(struct inode *inode, struct page **pages, |
3225 | int num_pages, u64 off) | |
3226 | { | |
3227 | int i; | |
09cbfeaf | 3228 | pgoff_t index = off >> PAGE_SHIFT; |
f4414602 MF |
3229 | |
3230 | for (i = 0; i < num_pages; i++) { | |
31314002 | 3231 | again: |
f4414602 | 3232 | pages[i] = extent_same_get_page(inode, index + i); |
31314002 FM |
3233 | if (IS_ERR(pages[i])) { |
3234 | int err = PTR_ERR(pages[i]); | |
3235 | ||
3236 | if (err == -EAGAIN) | |
3237 | goto again; | |
3238 | pages[i] = NULL; | |
3239 | return err; | |
3240 | } | |
f4414602 MF |
3241 | } |
3242 | return 0; | |
3243 | } | |
3244 | ||
e0bd70c6 FM |
3245 | static int lock_extent_range(struct inode *inode, u64 off, u64 len, |
3246 | bool retry_range_locking) | |
77fe20dc | 3247 | { |
e0bd70c6 FM |
3248 | /* |
3249 | * Do any pending delalloc/csum calculations on inode, one way or | |
3250 | * another, and lock file content. | |
3251 | * The locking order is: | |
3252 | * | |
3253 | * 1) pages | |
3254 | * 2) range in the inode's io tree | |
3255 | */ | |
77fe20dc MF |
3256 | while (1) { |
3257 | struct btrfs_ordered_extent *ordered; | |
3258 | lock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); | |
3259 | ordered = btrfs_lookup_first_ordered_extent(inode, | |
3260 | off + len - 1); | |
ff5df9b8 FM |
3261 | if ((!ordered || |
3262 | ordered->file_offset + ordered->len <= off || | |
3263 | ordered->file_offset >= off + len) && | |
77fe20dc | 3264 | !test_range_bit(&BTRFS_I(inode)->io_tree, off, |
ff5df9b8 FM |
3265 | off + len - 1, EXTENT_DELALLOC, 0, NULL)) { |
3266 | if (ordered) | |
3267 | btrfs_put_ordered_extent(ordered); | |
77fe20dc | 3268 | break; |
ff5df9b8 | 3269 | } |
77fe20dc MF |
3270 | unlock_extent(&BTRFS_I(inode)->io_tree, off, off + len - 1); |
3271 | if (ordered) | |
3272 | btrfs_put_ordered_extent(ordered); | |
e0bd70c6 FM |
3273 | if (!retry_range_locking) |
3274 | return -EAGAIN; | |
77fe20dc MF |
3275 | btrfs_wait_ordered_range(inode, off, len); |
3276 | } | |
e0bd70c6 | 3277 | return 0; |
77fe20dc MF |
3278 | } |
3279 | ||
f4414602 | 3280 | static void btrfs_double_inode_unlock(struct inode *inode1, struct inode *inode2) |
416161db | 3281 | { |
5955102c AV |
3282 | inode_unlock(inode1); |
3283 | inode_unlock(inode2); | |
416161db MF |
3284 | } |
3285 | ||
f4414602 MF |
3286 | static void btrfs_double_inode_lock(struct inode *inode1, struct inode *inode2) |
3287 | { | |
3288 | if (inode1 < inode2) | |
3289 | swap(inode1, inode2); | |
3290 | ||
5955102c AV |
3291 | inode_lock_nested(inode1, I_MUTEX_PARENT); |
3292 | inode_lock_nested(inode2, I_MUTEX_CHILD); | |
f4414602 MF |
3293 | } |
3294 | ||
3295 | static void btrfs_double_extent_unlock(struct inode *inode1, u64 loff1, | |
3296 | struct inode *inode2, u64 loff2, u64 len) | |
3297 | { | |
3298 | unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, loff1 + len - 1); | |
3299 | unlock_extent(&BTRFS_I(inode2)->io_tree, loff2, loff2 + len - 1); | |
3300 | } | |
3301 | ||
e0bd70c6 FM |
3302 | static int btrfs_double_extent_lock(struct inode *inode1, u64 loff1, |
3303 | struct inode *inode2, u64 loff2, u64 len, | |
3304 | bool retry_range_locking) | |
416161db | 3305 | { |
e0bd70c6 FM |
3306 | int ret; |
3307 | ||
416161db MF |
3308 | if (inode1 < inode2) { |
3309 | swap(inode1, inode2); | |
3310 | swap(loff1, loff2); | |
3311 | } | |
e0bd70c6 FM |
3312 | ret = lock_extent_range(inode1, loff1, len, retry_range_locking); |
3313 | if (ret) | |
3314 | return ret; | |
3315 | ret = lock_extent_range(inode2, loff2, len, retry_range_locking); | |
3316 | if (ret) | |
3317 | unlock_extent(&BTRFS_I(inode1)->io_tree, loff1, | |
3318 | loff1 + len - 1); | |
3319 | return ret; | |
f4414602 MF |
3320 | } |
3321 | ||
3322 | struct cmp_pages { | |
3323 | int num_pages; | |
3324 | struct page **src_pages; | |
3325 | struct page **dst_pages; | |
3326 | }; | |
3327 | ||
3328 | static void btrfs_cmp_data_free(struct cmp_pages *cmp) | |
3329 | { | |
3330 | int i; | |
3331 | struct page *pg; | |
3332 | ||
3333 | for (i = 0; i < cmp->num_pages; i++) { | |
3334 | pg = cmp->src_pages[i]; | |
e0bd70c6 FM |
3335 | if (pg) { |
3336 | unlock_page(pg); | |
09cbfeaf | 3337 | put_page(pg); |
97b19170 | 3338 | cmp->src_pages[i] = NULL; |
e0bd70c6 | 3339 | } |
f4414602 | 3340 | pg = cmp->dst_pages[i]; |
e0bd70c6 FM |
3341 | if (pg) { |
3342 | unlock_page(pg); | |
09cbfeaf | 3343 | put_page(pg); |
97b19170 | 3344 | cmp->dst_pages[i] = NULL; |
e0bd70c6 | 3345 | } |
f4414602 | 3346 | } |
f4414602 MF |
3347 | } |
3348 | ||
3349 | static int btrfs_cmp_data_prepare(struct inode *src, u64 loff, | |
3350 | struct inode *dst, u64 dst_loff, | |
3351 | u64 len, struct cmp_pages *cmp) | |
3352 | { | |
3353 | int ret; | |
09cbfeaf | 3354 | int num_pages = PAGE_ALIGN(len) >> PAGE_SHIFT; |
f4414602 | 3355 | |
f4414602 | 3356 | cmp->num_pages = num_pages; |
b1517622 | 3357 | |
67b07bd4 | 3358 | ret = gather_extent_pages(src, cmp->src_pages, num_pages, loff); |
f4414602 MF |
3359 | if (ret) |
3360 | goto out; | |
3361 | ||
67b07bd4 | 3362 | ret = gather_extent_pages(dst, cmp->dst_pages, num_pages, dst_loff); |
f4414602 MF |
3363 | |
3364 | out: | |
3365 | if (ret) | |
3366 | btrfs_cmp_data_free(cmp); | |
78ad4ce0 | 3367 | return ret; |
416161db MF |
3368 | } |
3369 | ||
1a287cfe | 3370 | static int btrfs_cmp_data(u64 len, struct cmp_pages *cmp) |
416161db MF |
3371 | { |
3372 | int ret = 0; | |
f4414602 | 3373 | int i; |
416161db | 3374 | struct page *src_page, *dst_page; |
09cbfeaf | 3375 | unsigned int cmp_len = PAGE_SIZE; |
416161db MF |
3376 | void *addr, *dst_addr; |
3377 | ||
f4414602 | 3378 | i = 0; |
416161db | 3379 | while (len) { |
09cbfeaf | 3380 | if (len < PAGE_SIZE) |
416161db MF |
3381 | cmp_len = len; |
3382 | ||
f4414602 MF |
3383 | BUG_ON(i >= cmp->num_pages); |
3384 | ||
3385 | src_page = cmp->src_pages[i]; | |
3386 | dst_page = cmp->dst_pages[i]; | |
e0bd70c6 FM |
3387 | ASSERT(PageLocked(src_page)); |
3388 | ASSERT(PageLocked(dst_page)); | |
f4414602 | 3389 | |
416161db MF |
3390 | addr = kmap_atomic(src_page); |
3391 | dst_addr = kmap_atomic(dst_page); | |
3392 | ||
3393 | flush_dcache_page(src_page); | |
3394 | flush_dcache_page(dst_page); | |
3395 | ||
3396 | if (memcmp(addr, dst_addr, cmp_len)) | |
2b3909f8 | 3397 | ret = -EBADE; |
416161db MF |
3398 | |
3399 | kunmap_atomic(addr); | |
3400 | kunmap_atomic(dst_addr); | |
416161db MF |
3401 | |
3402 | if (ret) | |
3403 | break; | |
3404 | ||
416161db | 3405 | len -= cmp_len; |
f4414602 | 3406 | i++; |
416161db MF |
3407 | } |
3408 | ||
3409 | return ret; | |
3410 | } | |
3411 | ||
e1d227a4 MF |
3412 | static int extent_same_check_offsets(struct inode *inode, u64 off, u64 *plen, |
3413 | u64 olen) | |
416161db | 3414 | { |
e1d227a4 | 3415 | u64 len = *plen; |
416161db MF |
3416 | u64 bs = BTRFS_I(inode)->root->fs_info->sb->s_blocksize; |
3417 | ||
e1d227a4 | 3418 | if (off + olen > inode->i_size || off + olen < off) |
416161db | 3419 | return -EINVAL; |
e1d227a4 MF |
3420 | |
3421 | /* if we extend to eof, continue to block boundary */ | |
3422 | if (off + len == inode->i_size) | |
3423 | *plen = len = ALIGN(inode->i_size, bs) - off; | |
3424 | ||
416161db MF |
3425 | /* Check that we are block aligned - btrfs_clone() requires this */ |
3426 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs)) | |
3427 | return -EINVAL; | |
3428 | ||
3429 | return 0; | |
3430 | } | |
3431 | ||
3973909d | 3432 | static int btrfs_extent_same_range(struct inode *src, u64 loff, u64 olen, |
67b07bd4 TT |
3433 | struct inode *dst, u64 dst_loff, |
3434 | struct cmp_pages *cmp) | |
416161db MF |
3435 | { |
3436 | int ret; | |
e1d227a4 | 3437 | u64 len = olen; |
fc4badd9 | 3438 | bool same_inode = (src == dst); |
0efa9f48 MF |
3439 | u64 same_lock_start = 0; |
3440 | u64 same_lock_len = 0; | |
416161db | 3441 | |
fc4badd9 OS |
3442 | ret = extent_same_check_offsets(src, loff, &len, olen); |
3443 | if (ret) | |
3973909d | 3444 | return ret; |
416161db | 3445 | |
fc4badd9 OS |
3446 | ret = extent_same_check_offsets(dst, dst_loff, &len, olen); |
3447 | if (ret) | |
3973909d | 3448 | return ret; |
fc4badd9 OS |
3449 | |
3450 | if (same_inode) { | |
0efa9f48 MF |
3451 | /* |
3452 | * Single inode case wants the same checks, except we | |
3453 | * don't want our length pushed out past i_size as | |
3454 | * comparing that data range makes no sense. | |
3455 | * | |
3456 | * extent_same_check_offsets() will do this for an | |
3457 | * unaligned length at i_size, so catch it here and | |
3458 | * reject the request. | |
3459 | * | |
3460 | * This effectively means we require aligned extents | |
3461 | * for the single-inode case, whereas the other cases | |
3462 | * allow an unaligned length so long as it ends at | |
3463 | * i_size. | |
3464 | */ | |
3973909d TT |
3465 | if (len != olen) |
3466 | return -EINVAL; | |
0efa9f48 MF |
3467 | |
3468 | /* Check for overlapping ranges */ | |
3973909d TT |
3469 | if (dst_loff + len > loff && dst_loff < loff + len) |
3470 | return -EINVAL; | |
0efa9f48 MF |
3471 | |
3472 | same_lock_start = min_t(u64, loff, dst_loff); | |
3473 | same_lock_len = max_t(u64, loff, dst_loff) + len - same_lock_start; | |
de02b9f6 FM |
3474 | } else { |
3475 | /* | |
3476 | * If the source and destination inodes are different, the | |
3477 | * source's range end offset matches the source's i_size, that | |
3478 | * i_size is not a multiple of the sector size, and the | |
3479 | * destination range does not go past the destination's i_size, | |
3480 | * we must round down the length to the nearest sector size | |
3481 | * multiple. If we don't do this adjustment we end replacing | |
3482 | * with zeroes the bytes in the range that starts at the | |
3483 | * deduplication range's end offset and ends at the next sector | |
3484 | * size multiple. | |
3485 | */ | |
3486 | if (loff + olen == i_size_read(src) && | |
3487 | dst_loff + len < i_size_read(dst)) { | |
3488 | const u64 sz = BTRFS_I(src)->root->fs_info->sectorsize; | |
3489 | ||
3490 | len = round_down(i_size_read(src), sz) - loff; | |
3491 | olen = len; | |
3492 | } | |
0efa9f48 | 3493 | } |
416161db | 3494 | |
e0bd70c6 | 3495 | again: |
67b07bd4 | 3496 | ret = btrfs_cmp_data_prepare(src, loff, dst, dst_loff, olen, cmp); |
f4414602 | 3497 | if (ret) |
3973909d | 3498 | return ret; |
f4414602 | 3499 | |
0efa9f48 | 3500 | if (same_inode) |
e0bd70c6 FM |
3501 | ret = lock_extent_range(src, same_lock_start, same_lock_len, |
3502 | false); | |
0efa9f48 | 3503 | else |
e0bd70c6 FM |
3504 | ret = btrfs_double_extent_lock(src, loff, dst, dst_loff, len, |
3505 | false); | |
3506 | /* | |
3507 | * If one of the inodes has dirty pages in the respective range or | |
3508 | * ordered extents, we need to flush dellaloc and wait for all ordered | |
3509 | * extents in the range. We must unlock the pages and the ranges in the | |
3510 | * io trees to avoid deadlocks when flushing delalloc (requires locking | |
3511 | * pages) and when waiting for ordered extents to complete (they require | |
3512 | * range locking). | |
3513 | */ | |
3514 | if (ret == -EAGAIN) { | |
3515 | /* | |
3516 | * Ranges in the io trees already unlocked. Now unlock all | |
3517 | * pages before waiting for all IO to complete. | |
3518 | */ | |
67b07bd4 | 3519 | btrfs_cmp_data_free(cmp); |
e0bd70c6 FM |
3520 | if (same_inode) { |
3521 | btrfs_wait_ordered_range(src, same_lock_start, | |
3522 | same_lock_len); | |
3523 | } else { | |
3524 | btrfs_wait_ordered_range(src, loff, len); | |
3525 | btrfs_wait_ordered_range(dst, dst_loff, len); | |
3526 | } | |
3527 | goto again; | |
3528 | } | |
3529 | ASSERT(ret == 0); | |
3530 | if (WARN_ON(ret)) { | |
3531 | /* ranges in the io trees already unlocked */ | |
67b07bd4 | 3532 | btrfs_cmp_data_free(cmp); |
e0bd70c6 FM |
3533 | return ret; |
3534 | } | |
f4414602 | 3535 | |
207910dd | 3536 | /* pass original length for comparison so we stay within i_size */ |
67b07bd4 | 3537 | ret = btrfs_cmp_data(olen, cmp); |
416161db | 3538 | if (ret == 0) |
1c919a5e | 3539 | ret = btrfs_clone(src, dst, loff, olen, len, dst_loff, 1); |
416161db | 3540 | |
0efa9f48 MF |
3541 | if (same_inode) |
3542 | unlock_extent(&BTRFS_I(src)->io_tree, same_lock_start, | |
3543 | same_lock_start + same_lock_len - 1); | |
3544 | else | |
3545 | btrfs_double_extent_unlock(src, loff, dst, dst_loff, len); | |
f4414602 | 3546 | |
67b07bd4 | 3547 | btrfs_cmp_data_free(cmp); |
3973909d TT |
3548 | |
3549 | return ret; | |
3550 | } | |
3551 | ||
b6728768 TT |
3552 | #define BTRFS_MAX_DEDUPE_LEN SZ_16M |
3553 | ||
3973909d TT |
3554 | static int btrfs_extent_same(struct inode *src, u64 loff, u64 olen, |
3555 | struct inode *dst, u64 dst_loff) | |
3556 | { | |
3557 | int ret; | |
67b07bd4 TT |
3558 | struct cmp_pages cmp; |
3559 | int num_pages = PAGE_ALIGN(BTRFS_MAX_DEDUPE_LEN) >> PAGE_SHIFT; | |
3973909d | 3560 | bool same_inode = (src == dst); |
b6728768 | 3561 | u64 i, tail_len, chunk_count; |
3973909d TT |
3562 | |
3563 | if (olen == 0) | |
3564 | return 0; | |
3565 | ||
3566 | if (same_inode) | |
3567 | inode_lock(src); | |
3568 | else | |
3569 | btrfs_double_inode_lock(src, dst); | |
3570 | ||
3571 | /* don't make the dst file partly checksummed */ | |
3572 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != | |
3573 | (BTRFS_I(dst)->flags & BTRFS_INODE_NODATASUM)) { | |
3574 | ret = -EINVAL; | |
3575 | goto out_unlock; | |
3576 | } | |
3577 | ||
b6728768 TT |
3578 | tail_len = olen % BTRFS_MAX_DEDUPE_LEN; |
3579 | chunk_count = div_u64(olen, BTRFS_MAX_DEDUPE_LEN); | |
67b07bd4 TT |
3580 | if (chunk_count == 0) |
3581 | num_pages = PAGE_ALIGN(tail_len) >> PAGE_SHIFT; | |
3582 | ||
3583 | /* | |
3584 | * If deduping ranges in the same inode, locking rules make it | |
3585 | * mandatory to always lock pages in ascending order to avoid deadlocks | |
3586 | * with concurrent tasks (such as starting writeback/delalloc). | |
3587 | */ | |
3588 | if (same_inode && dst_loff < loff) | |
3589 | swap(loff, dst_loff); | |
3590 | ||
3591 | /* | |
3592 | * We must gather up all the pages before we initiate our extent | |
3593 | * locking. We use an array for the page pointers. Size of the array is | |
3594 | * bounded by len, which is in turn bounded by BTRFS_MAX_DEDUPE_LEN. | |
3595 | */ | |
bf5091c8 DS |
3596 | cmp.src_pages = kvmalloc_array(num_pages, sizeof(struct page *), |
3597 | GFP_KERNEL | __GFP_ZERO); | |
3598 | cmp.dst_pages = kvmalloc_array(num_pages, sizeof(struct page *), | |
3599 | GFP_KERNEL | __GFP_ZERO); | |
67b07bd4 | 3600 | if (!cmp.src_pages || !cmp.dst_pages) { |
bf5091c8 DS |
3601 | ret = -ENOMEM; |
3602 | goto out_free; | |
67b07bd4 | 3603 | } |
b6728768 TT |
3604 | |
3605 | for (i = 0; i < chunk_count; i++) { | |
3606 | ret = btrfs_extent_same_range(src, loff, BTRFS_MAX_DEDUPE_LEN, | |
67b07bd4 | 3607 | dst, dst_loff, &cmp); |
b6728768 | 3608 | if (ret) |
22883ddc | 3609 | goto out_free; |
b6728768 TT |
3610 | |
3611 | loff += BTRFS_MAX_DEDUPE_LEN; | |
3612 | dst_loff += BTRFS_MAX_DEDUPE_LEN; | |
3613 | } | |
3614 | ||
3615 | if (tail_len > 0) | |
67b07bd4 TT |
3616 | ret = btrfs_extent_same_range(src, loff, tail_len, dst, |
3617 | dst_loff, &cmp); | |
3973909d | 3618 | |
22883ddc LF |
3619 | out_free: |
3620 | kvfree(cmp.src_pages); | |
3621 | kvfree(cmp.dst_pages); | |
3622 | ||
416161db | 3623 | out_unlock: |
0efa9f48 | 3624 | if (same_inode) |
5955102c | 3625 | inode_unlock(src); |
0efa9f48 MF |
3626 | else |
3627 | btrfs_double_inode_unlock(src, dst); | |
416161db MF |
3628 | |
3629 | return ret; | |
3630 | } | |
3631 | ||
87eb5eb2 MS |
3632 | int btrfs_dedupe_file_range(struct file *src_file, loff_t src_loff, |
3633 | struct file *dst_file, loff_t dst_loff, | |
3634 | u64 olen) | |
416161db | 3635 | { |
2b3909f8 DW |
3636 | struct inode *src = file_inode(src_file); |
3637 | struct inode *dst = file_inode(dst_file); | |
416161db | 3638 | u64 bs = BTRFS_I(src)->root->fs_info->sb->s_blocksize; |
416161db | 3639 | |
09cbfeaf | 3640 | if (WARN_ON_ONCE(bs < PAGE_SIZE)) { |
416161db MF |
3641 | /* |
3642 | * Btrfs does not support blocksize < page_size. As a | |
3643 | * result, btrfs_cmp_data() won't correctly handle | |
3644 | * this situation without an update. | |
3645 | */ | |
2b3909f8 | 3646 | return -EINVAL; |
416161db MF |
3647 | } |
3648 | ||
87eb5eb2 | 3649 | return btrfs_extent_same(src, src_loff, olen, dst, dst_loff); |
416161db MF |
3650 | } |
3651 | ||
f82a9901 FM |
3652 | static int clone_finish_inode_update(struct btrfs_trans_handle *trans, |
3653 | struct inode *inode, | |
3654 | u64 endoff, | |
3655 | const u64 destoff, | |
1c919a5e MF |
3656 | const u64 olen, |
3657 | int no_time_update) | |
f82a9901 FM |
3658 | { |
3659 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3660 | int ret; | |
3661 | ||
3662 | inode_inc_iversion(inode); | |
1c919a5e | 3663 | if (!no_time_update) |
c2050a45 | 3664 | inode->i_mtime = inode->i_ctime = current_time(inode); |
f82a9901 FM |
3665 | /* |
3666 | * We round up to the block size at eof when determining which | |
3667 | * extents to clone above, but shouldn't round up the file size. | |
3668 | */ | |
3669 | if (endoff > destoff + olen) | |
3670 | endoff = destoff + olen; | |
3671 | if (endoff > inode->i_size) | |
6ef06d27 | 3672 | btrfs_i_size_write(BTRFS_I(inode), endoff); |
f82a9901 FM |
3673 | |
3674 | ret = btrfs_update_inode(trans, root, inode); | |
3675 | if (ret) { | |
66642832 | 3676 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 3677 | btrfs_end_transaction(trans); |
f82a9901 FM |
3678 | goto out; |
3679 | } | |
3a45bb20 | 3680 | ret = btrfs_end_transaction(trans); |
f82a9901 FM |
3681 | out: |
3682 | return ret; | |
3683 | } | |
3684 | ||
a2f392e4 | 3685 | static void clone_update_extent_map(struct btrfs_inode *inode, |
7ffbb598 FM |
3686 | const struct btrfs_trans_handle *trans, |
3687 | const struct btrfs_path *path, | |
7ffbb598 FM |
3688 | const u64 hole_offset, |
3689 | const u64 hole_len) | |
3690 | { | |
a2f392e4 | 3691 | struct extent_map_tree *em_tree = &inode->extent_tree; |
7ffbb598 FM |
3692 | struct extent_map *em; |
3693 | int ret; | |
3694 | ||
3695 | em = alloc_extent_map(); | |
3696 | if (!em) { | |
a2f392e4 | 3697 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
7ffbb598 FM |
3698 | return; |
3699 | } | |
3700 | ||
14f59796 FM |
3701 | if (path) { |
3702 | struct btrfs_file_extent_item *fi; | |
3703 | ||
3704 | fi = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3705 | struct btrfs_file_extent_item); | |
a2f392e4 | 3706 | btrfs_extent_item_to_extent_map(inode, path, fi, false, em); |
7ffbb598 FM |
3707 | em->generation = -1; |
3708 | if (btrfs_file_extent_type(path->nodes[0], fi) == | |
3709 | BTRFS_FILE_EXTENT_INLINE) | |
3710 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, | |
a2f392e4 | 3711 | &inode->runtime_flags); |
7ffbb598 FM |
3712 | } else { |
3713 | em->start = hole_offset; | |
3714 | em->len = hole_len; | |
3715 | em->ram_bytes = em->len; | |
3716 | em->orig_start = hole_offset; | |
3717 | em->block_start = EXTENT_MAP_HOLE; | |
3718 | em->block_len = 0; | |
3719 | em->orig_block_len = 0; | |
3720 | em->compress_type = BTRFS_COMPRESS_NONE; | |
3721 | em->generation = trans->transid; | |
3722 | } | |
3723 | ||
3724 | while (1) { | |
3725 | write_lock(&em_tree->lock); | |
3726 | ret = add_extent_mapping(em_tree, em, 1); | |
3727 | write_unlock(&em_tree->lock); | |
3728 | if (ret != -EEXIST) { | |
3729 | free_extent_map(em); | |
3730 | break; | |
3731 | } | |
a2f392e4 | 3732 | btrfs_drop_extent_cache(inode, em->start, |
7ffbb598 FM |
3733 | em->start + em->len - 1, 0); |
3734 | } | |
3735 | ||
ee39b432 | 3736 | if (ret) |
a2f392e4 | 3737 | set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags); |
7ffbb598 FM |
3738 | } |
3739 | ||
8039d87d FM |
3740 | /* |
3741 | * Make sure we do not end up inserting an inline extent into a file that has | |
3742 | * already other (non-inline) extents. If a file has an inline extent it can | |
3743 | * not have any other extents and the (single) inline extent must start at the | |
3744 | * file offset 0. Failing to respect these rules will lead to file corruption, | |
3745 | * resulting in EIO errors on read/write operations, hitting BUG_ON's in mm, etc | |
3746 | * | |
3747 | * We can have extents that have been already written to disk or we can have | |
3748 | * dirty ranges still in delalloc, in which case the extent maps and items are | |
3749 | * created only when we run delalloc, and the delalloc ranges might fall outside | |
3750 | * the range we are currently locking in the inode's io tree. So we check the | |
3751 | * inode's i_size because of that (i_size updates are done while holding the | |
3752 | * i_mutex, which we are holding here). | |
3753 | * We also check to see if the inode has a size not greater than "datal" but has | |
3754 | * extents beyond it, due to an fallocate with FALLOC_FL_KEEP_SIZE (and we are | |
3755 | * protected against such concurrent fallocate calls by the i_mutex). | |
3756 | * | |
3757 | * If the file has no extents but a size greater than datal, do not allow the | |
3758 | * copy because we would need turn the inline extent into a non-inline one (even | |
3759 | * with NO_HOLES enabled). If we find our destination inode only has one inline | |
3760 | * extent, just overwrite it with the source inline extent if its size is less | |
3761 | * than the source extent's size, or we could copy the source inline extent's | |
3762 | * data into the destination inode's inline extent if the later is greater then | |
3763 | * the former. | |
3764 | */ | |
4a0ab9d7 | 3765 | static int clone_copy_inline_extent(struct inode *dst, |
8039d87d FM |
3766 | struct btrfs_trans_handle *trans, |
3767 | struct btrfs_path *path, | |
3768 | struct btrfs_key *new_key, | |
3769 | const u64 drop_start, | |
3770 | const u64 datal, | |
3771 | const u64 skip, | |
3772 | const u64 size, | |
3773 | char *inline_data) | |
3774 | { | |
0b246afa | 3775 | struct btrfs_fs_info *fs_info = btrfs_sb(dst->i_sb); |
8039d87d FM |
3776 | struct btrfs_root *root = BTRFS_I(dst)->root; |
3777 | const u64 aligned_end = ALIGN(new_key->offset + datal, | |
0b246afa | 3778 | fs_info->sectorsize); |
8039d87d FM |
3779 | int ret; |
3780 | struct btrfs_key key; | |
3781 | ||
3782 | if (new_key->offset > 0) | |
3783 | return -EOPNOTSUPP; | |
3784 | ||
4a0cc7ca | 3785 | key.objectid = btrfs_ino(BTRFS_I(dst)); |
8039d87d FM |
3786 | key.type = BTRFS_EXTENT_DATA_KEY; |
3787 | key.offset = 0; | |
3788 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
3789 | if (ret < 0) { | |
3790 | return ret; | |
3791 | } else if (ret > 0) { | |
3792 | if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) { | |
3793 | ret = btrfs_next_leaf(root, path); | |
3794 | if (ret < 0) | |
3795 | return ret; | |
3796 | else if (ret > 0) | |
3797 | goto copy_inline_extent; | |
3798 | } | |
3799 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
4a0cc7ca | 3800 | if (key.objectid == btrfs_ino(BTRFS_I(dst)) && |
8039d87d FM |
3801 | key.type == BTRFS_EXTENT_DATA_KEY) { |
3802 | ASSERT(key.offset > 0); | |
3803 | return -EOPNOTSUPP; | |
3804 | } | |
3805 | } else if (i_size_read(dst) <= datal) { | |
3806 | struct btrfs_file_extent_item *ei; | |
3807 | u64 ext_len; | |
3808 | ||
3809 | /* | |
3810 | * If the file size is <= datal, make sure there are no other | |
3811 | * extents following (can happen do to an fallocate call with | |
3812 | * the flag FALLOC_FL_KEEP_SIZE). | |
3813 | */ | |
3814 | ei = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
3815 | struct btrfs_file_extent_item); | |
3816 | /* | |
3817 | * If it's an inline extent, it can not have other extents | |
3818 | * following it. | |
3819 | */ | |
3820 | if (btrfs_file_extent_type(path->nodes[0], ei) == | |
3821 | BTRFS_FILE_EXTENT_INLINE) | |
3822 | goto copy_inline_extent; | |
3823 | ||
3824 | ext_len = btrfs_file_extent_num_bytes(path->nodes[0], ei); | |
3825 | if (ext_len > aligned_end) | |
3826 | return -EOPNOTSUPP; | |
3827 | ||
3828 | ret = btrfs_next_item(root, path); | |
3829 | if (ret < 0) { | |
3830 | return ret; | |
3831 | } else if (ret == 0) { | |
3832 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
3833 | path->slots[0]); | |
4a0cc7ca | 3834 | if (key.objectid == btrfs_ino(BTRFS_I(dst)) && |
8039d87d FM |
3835 | key.type == BTRFS_EXTENT_DATA_KEY) |
3836 | return -EOPNOTSUPP; | |
3837 | } | |
3838 | } | |
3839 | ||
3840 | copy_inline_extent: | |
3841 | /* | |
3842 | * We have no extent items, or we have an extent at offset 0 which may | |
3843 | * or may not be inlined. All these cases are dealt the same way. | |
3844 | */ | |
3845 | if (i_size_read(dst) > datal) { | |
3846 | /* | |
3847 | * If the destination inode has an inline extent... | |
3848 | * This would require copying the data from the source inline | |
3849 | * extent into the beginning of the destination's inline extent. | |
3850 | * But this is really complex, both extents can be compressed | |
3851 | * or just one of them, which would require decompressing and | |
3852 | * re-compressing data (which could increase the new compressed | |
3853 | * size, not allowing the compressed data to fit anymore in an | |
3854 | * inline extent). | |
3855 | * So just don't support this case for now (it should be rare, | |
3856 | * we are not really saving space when cloning inline extents). | |
3857 | */ | |
3858 | return -EOPNOTSUPP; | |
3859 | } | |
3860 | ||
3861 | btrfs_release_path(path); | |
3862 | ret = btrfs_drop_extents(trans, root, dst, drop_start, aligned_end, 1); | |
3863 | if (ret) | |
3864 | return ret; | |
3865 | ret = btrfs_insert_empty_item(trans, root, path, new_key, size); | |
3866 | if (ret) | |
3867 | return ret; | |
3868 | ||
3869 | if (skip) { | |
3870 | const u32 start = btrfs_file_extent_calc_inline_size(0); | |
3871 | ||
3872 | memmove(inline_data + start, inline_data + start + skip, datal); | |
3873 | } | |
3874 | ||
3875 | write_extent_buffer(path->nodes[0], inline_data, | |
3876 | btrfs_item_ptr_offset(path->nodes[0], | |
3877 | path->slots[0]), | |
3878 | size); | |
3879 | inode_add_bytes(dst, datal); | |
3880 | ||
3881 | return 0; | |
3882 | } | |
3883 | ||
32b7c687 MF |
3884 | /** |
3885 | * btrfs_clone() - clone a range from inode file to another | |
3886 | * | |
3887 | * @src: Inode to clone from | |
3888 | * @inode: Inode to clone to | |
3889 | * @off: Offset within source to start clone from | |
3890 | * @olen: Original length, passed by user, of range to clone | |
1c919a5e | 3891 | * @olen_aligned: Block-aligned value of olen |
32b7c687 | 3892 | * @destoff: Offset within @inode to start clone |
1c919a5e | 3893 | * @no_time_update: Whether to update mtime/ctime on the target inode |
32b7c687 MF |
3894 | */ |
3895 | static int btrfs_clone(struct inode *src, struct inode *inode, | |
f82a9901 | 3896 | const u64 off, const u64 olen, const u64 olen_aligned, |
1c919a5e | 3897 | const u64 destoff, int no_time_update) |
f46b5a66 | 3898 | { |
0b246afa | 3899 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
f46b5a66 | 3900 | struct btrfs_root *root = BTRFS_I(inode)->root; |
32b7c687 | 3901 | struct btrfs_path *path = NULL; |
f46b5a66 | 3902 | struct extent_buffer *leaf; |
32b7c687 MF |
3903 | struct btrfs_trans_handle *trans; |
3904 | char *buf = NULL; | |
ae01a0ab | 3905 | struct btrfs_key key; |
f46b5a66 CH |
3906 | u32 nritems; |
3907 | int slot; | |
ae01a0ab | 3908 | int ret; |
f82a9901 | 3909 | const u64 len = olen_aligned; |
f82a9901 | 3910 | u64 last_dest_end = destoff; |
ae01a0ab YZ |
3911 | |
3912 | ret = -ENOMEM; | |
752ade68 MH |
3913 | buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); |
3914 | if (!buf) | |
3915 | return ret; | |
ae01a0ab YZ |
3916 | |
3917 | path = btrfs_alloc_path(); | |
3918 | if (!path) { | |
15351955 | 3919 | kvfree(buf); |
32b7c687 | 3920 | return ret; |
d525e8ab LZ |
3921 | } |
3922 | ||
e4058b54 | 3923 | path->reada = READA_FORWARD; |
c5c9cd4d | 3924 | /* clone data */ |
4a0cc7ca | 3925 | key.objectid = btrfs_ino(BTRFS_I(src)); |
ae01a0ab | 3926 | key.type = BTRFS_EXTENT_DATA_KEY; |
2c463823 | 3927 | key.offset = off; |
f46b5a66 CH |
3928 | |
3929 | while (1) { | |
de249e66 | 3930 | u64 next_key_min_offset = key.offset + 1; |
df858e76 | 3931 | |
f46b5a66 CH |
3932 | /* |
3933 | * note the key will change type as we walk through the | |
3934 | * tree. | |
3935 | */ | |
e4355f34 | 3936 | path->leave_spinning = 1; |
362a20c5 DS |
3937 | ret = btrfs_search_slot(NULL, BTRFS_I(src)->root, &key, path, |
3938 | 0, 0); | |
f46b5a66 CH |
3939 | if (ret < 0) |
3940 | goto out; | |
2c463823 FM |
3941 | /* |
3942 | * First search, if no extent item that starts at offset off was | |
3943 | * found but the previous item is an extent item, it's possible | |
3944 | * it might overlap our target range, therefore process it. | |
3945 | */ | |
3946 | if (key.offset == off && ret > 0 && path->slots[0] > 0) { | |
3947 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
3948 | path->slots[0] - 1); | |
3949 | if (key.type == BTRFS_EXTENT_DATA_KEY) | |
3950 | path->slots[0]--; | |
3951 | } | |
f46b5a66 | 3952 | |
ae01a0ab | 3953 | nritems = btrfs_header_nritems(path->nodes[0]); |
e4355f34 | 3954 | process_slot: |
ae01a0ab | 3955 | if (path->slots[0] >= nritems) { |
362a20c5 | 3956 | ret = btrfs_next_leaf(BTRFS_I(src)->root, path); |
f46b5a66 CH |
3957 | if (ret < 0) |
3958 | goto out; | |
3959 | if (ret > 0) | |
3960 | break; | |
ae01a0ab | 3961 | nritems = btrfs_header_nritems(path->nodes[0]); |
f46b5a66 CH |
3962 | } |
3963 | leaf = path->nodes[0]; | |
3964 | slot = path->slots[0]; | |
f46b5a66 | 3965 | |
ae01a0ab | 3966 | btrfs_item_key_to_cpu(leaf, &key, slot); |
962a298f | 3967 | if (key.type > BTRFS_EXTENT_DATA_KEY || |
4a0cc7ca | 3968 | key.objectid != btrfs_ino(BTRFS_I(src))) |
f46b5a66 CH |
3969 | break; |
3970 | ||
962a298f | 3971 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
c5c9cd4d SW |
3972 | struct btrfs_file_extent_item *extent; |
3973 | int type; | |
31840ae1 ZY |
3974 | u32 size; |
3975 | struct btrfs_key new_key; | |
c5c9cd4d SW |
3976 | u64 disko = 0, diskl = 0; |
3977 | u64 datao = 0, datal = 0; | |
3978 | u8 comp; | |
f82a9901 | 3979 | u64 drop_start; |
31840ae1 | 3980 | |
c5c9cd4d SW |
3981 | extent = btrfs_item_ptr(leaf, slot, |
3982 | struct btrfs_file_extent_item); | |
3983 | comp = btrfs_file_extent_compression(leaf, extent); | |
3984 | type = btrfs_file_extent_type(leaf, extent); | |
c8a894d7 CM |
3985 | if (type == BTRFS_FILE_EXTENT_REG || |
3986 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d397712b CM |
3987 | disko = btrfs_file_extent_disk_bytenr(leaf, |
3988 | extent); | |
3989 | diskl = btrfs_file_extent_disk_num_bytes(leaf, | |
3990 | extent); | |
c5c9cd4d | 3991 | datao = btrfs_file_extent_offset(leaf, extent); |
d397712b CM |
3992 | datal = btrfs_file_extent_num_bytes(leaf, |
3993 | extent); | |
c5c9cd4d SW |
3994 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
3995 | /* take upper bound, may be compressed */ | |
3996 | datal = btrfs_file_extent_ram_bytes(leaf, | |
3997 | extent); | |
3998 | } | |
31840ae1 | 3999 | |
2c463823 FM |
4000 | /* |
4001 | * The first search might have left us at an extent | |
4002 | * item that ends before our target range's start, can | |
4003 | * happen if we have holes and NO_HOLES feature enabled. | |
4004 | */ | |
4005 | if (key.offset + datal <= off) { | |
e4355f34 FDBM |
4006 | path->slots[0]++; |
4007 | goto process_slot; | |
2c463823 FM |
4008 | } else if (key.offset >= off + len) { |
4009 | break; | |
e4355f34 | 4010 | } |
df858e76 | 4011 | next_key_min_offset = key.offset + datal; |
e4355f34 FDBM |
4012 | size = btrfs_item_size_nr(leaf, slot); |
4013 | read_extent_buffer(leaf, buf, | |
4014 | btrfs_item_ptr_offset(leaf, slot), | |
4015 | size); | |
4016 | ||
4017 | btrfs_release_path(path); | |
4018 | path->leave_spinning = 0; | |
c5c9cd4d | 4019 | |
31840ae1 | 4020 | memcpy(&new_key, &key, sizeof(new_key)); |
4a0cc7ca | 4021 | new_key.objectid = btrfs_ino(BTRFS_I(inode)); |
4d728ec7 LZ |
4022 | if (off <= key.offset) |
4023 | new_key.offset = key.offset + destoff - off; | |
4024 | else | |
4025 | new_key.offset = destoff; | |
31840ae1 | 4026 | |
f82a9901 FM |
4027 | /* |
4028 | * Deal with a hole that doesn't have an extent item | |
4029 | * that represents it (NO_HOLES feature enabled). | |
4030 | * This hole is either in the middle of the cloning | |
4031 | * range or at the beginning (fully overlaps it or | |
4032 | * partially overlaps it). | |
4033 | */ | |
4034 | if (new_key.offset != last_dest_end) | |
4035 | drop_start = last_dest_end; | |
4036 | else | |
4037 | drop_start = new_key.offset; | |
4038 | ||
b6f3409b SW |
4039 | /* |
4040 | * 1 - adjusting old extent (we may have to split it) | |
4041 | * 1 - add new extent | |
4042 | * 1 - inode update | |
4043 | */ | |
4044 | trans = btrfs_start_transaction(root, 3); | |
a22285a6 YZ |
4045 | if (IS_ERR(trans)) { |
4046 | ret = PTR_ERR(trans); | |
4047 | goto out; | |
4048 | } | |
4049 | ||
c8a894d7 CM |
4050 | if (type == BTRFS_FILE_EXTENT_REG || |
4051 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d72c0842 LZ |
4052 | /* |
4053 | * a | --- range to clone ---| b | |
4054 | * | ------------- extent ------------- | | |
4055 | */ | |
4056 | ||
93915584 | 4057 | /* subtract range b */ |
d72c0842 LZ |
4058 | if (key.offset + datal > off + len) |
4059 | datal = off + len - key.offset; | |
4060 | ||
93915584 | 4061 | /* subtract range a */ |
a22285a6 YZ |
4062 | if (off > key.offset) { |
4063 | datao += off - key.offset; | |
4064 | datal -= off - key.offset; | |
4065 | } | |
4066 | ||
5dc562c5 | 4067 | ret = btrfs_drop_extents(trans, root, inode, |
f82a9901 | 4068 | drop_start, |
a22285a6 | 4069 | new_key.offset + datal, |
2671485d | 4070 | 1); |
79787eaa | 4071 | if (ret) { |
3f9e3df8 | 4072 | if (ret != -EOPNOTSUPP) |
00fdf13a | 4073 | btrfs_abort_transaction(trans, |
66642832 | 4074 | ret); |
3a45bb20 | 4075 | btrfs_end_transaction(trans); |
79787eaa JM |
4076 | goto out; |
4077 | } | |
a22285a6 | 4078 | |
c5c9cd4d SW |
4079 | ret = btrfs_insert_empty_item(trans, root, path, |
4080 | &new_key, size); | |
79787eaa | 4081 | if (ret) { |
66642832 | 4082 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4083 | btrfs_end_transaction(trans); |
79787eaa JM |
4084 | goto out; |
4085 | } | |
c5c9cd4d SW |
4086 | |
4087 | leaf = path->nodes[0]; | |
4088 | slot = path->slots[0]; | |
4089 | write_extent_buffer(leaf, buf, | |
31840ae1 ZY |
4090 | btrfs_item_ptr_offset(leaf, slot), |
4091 | size); | |
ae01a0ab | 4092 | |
c5c9cd4d | 4093 | extent = btrfs_item_ptr(leaf, slot, |
f46b5a66 | 4094 | struct btrfs_file_extent_item); |
c5c9cd4d | 4095 | |
c5c9cd4d SW |
4096 | /* disko == 0 means it's a hole */ |
4097 | if (!disko) | |
4098 | datao = 0; | |
c5c9cd4d SW |
4099 | |
4100 | btrfs_set_file_extent_offset(leaf, extent, | |
4101 | datao); | |
4102 | btrfs_set_file_extent_num_bytes(leaf, extent, | |
4103 | datal); | |
fcebe456 | 4104 | |
c5c9cd4d SW |
4105 | if (disko) { |
4106 | inode_add_bytes(inode, datal); | |
2ff7e61e | 4107 | ret = btrfs_inc_extent_ref(trans, |
84f7d8e6 | 4108 | root, |
5d4f98a2 YZ |
4109 | disko, diskl, 0, |
4110 | root->root_key.objectid, | |
4a0cc7ca | 4111 | btrfs_ino(BTRFS_I(inode)), |
b06c4bf5 | 4112 | new_key.offset - datao); |
79787eaa JM |
4113 | if (ret) { |
4114 | btrfs_abort_transaction(trans, | |
79787eaa | 4115 | ret); |
3a45bb20 | 4116 | btrfs_end_transaction(trans); |
79787eaa JM |
4117 | goto out; |
4118 | ||
4119 | } | |
f46b5a66 | 4120 | } |
c5c9cd4d SW |
4121 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
4122 | u64 skip = 0; | |
4123 | u64 trim = 0; | |
ed958762 | 4124 | |
c5c9cd4d SW |
4125 | if (off > key.offset) { |
4126 | skip = off - key.offset; | |
4127 | new_key.offset += skip; | |
4128 | } | |
d397712b | 4129 | |
aa42ffd9 LB |
4130 | if (key.offset + datal > off + len) |
4131 | trim = key.offset + datal - (off + len); | |
d397712b | 4132 | |
c5c9cd4d | 4133 | if (comp && (skip || trim)) { |
c5c9cd4d | 4134 | ret = -EINVAL; |
3a45bb20 | 4135 | btrfs_end_transaction(trans); |
c5c9cd4d SW |
4136 | goto out; |
4137 | } | |
4138 | size -= skip + trim; | |
4139 | datal -= skip + trim; | |
a22285a6 | 4140 | |
4a0ab9d7 | 4141 | ret = clone_copy_inline_extent(inode, |
8039d87d FM |
4142 | trans, path, |
4143 | &new_key, | |
4144 | drop_start, | |
4145 | datal, | |
4146 | skip, size, buf); | |
79787eaa | 4147 | if (ret) { |
3f9e3df8 | 4148 | if (ret != -EOPNOTSUPP) |
3a29bc09 | 4149 | btrfs_abort_transaction(trans, |
8039d87d | 4150 | ret); |
3a45bb20 | 4151 | btrfs_end_transaction(trans); |
79787eaa JM |
4152 | goto out; |
4153 | } | |
c5c9cd4d SW |
4154 | leaf = path->nodes[0]; |
4155 | slot = path->slots[0]; | |
f46b5a66 | 4156 | } |
c5c9cd4d | 4157 | |
7ffbb598 FM |
4158 | /* If we have an implicit hole (NO_HOLES feature). */ |
4159 | if (drop_start < new_key.offset) | |
a2f392e4 | 4160 | clone_update_extent_map(BTRFS_I(inode), trans, |
14f59796 | 4161 | NULL, drop_start, |
7ffbb598 FM |
4162 | new_key.offset - drop_start); |
4163 | ||
a2f392e4 NB |
4164 | clone_update_extent_map(BTRFS_I(inode), trans, |
4165 | path, 0, 0); | |
7ffbb598 | 4166 | |
c5c9cd4d | 4167 | btrfs_mark_buffer_dirty(leaf); |
b3b4aa74 | 4168 | btrfs_release_path(path); |
c5c9cd4d | 4169 | |
62e2390e | 4170 | last_dest_end = ALIGN(new_key.offset + datal, |
0b246afa | 4171 | fs_info->sectorsize); |
f82a9901 FM |
4172 | ret = clone_finish_inode_update(trans, inode, |
4173 | last_dest_end, | |
1c919a5e MF |
4174 | destoff, olen, |
4175 | no_time_update); | |
f82a9901 | 4176 | if (ret) |
79787eaa | 4177 | goto out; |
2c463823 FM |
4178 | if (new_key.offset + datal >= destoff + len) |
4179 | break; | |
a22285a6 | 4180 | } |
b3b4aa74 | 4181 | btrfs_release_path(path); |
df858e76 | 4182 | key.offset = next_key_min_offset; |
69ae5e44 WX |
4183 | |
4184 | if (fatal_signal_pending(current)) { | |
4185 | ret = -EINTR; | |
4186 | goto out; | |
4187 | } | |
f46b5a66 | 4188 | } |
f46b5a66 | 4189 | ret = 0; |
32b7c687 | 4190 | |
f82a9901 FM |
4191 | if (last_dest_end < destoff + len) { |
4192 | /* | |
4193 | * We have an implicit hole (NO_HOLES feature is enabled) that | |
4194 | * fully or partially overlaps our cloning range at its end. | |
4195 | */ | |
4196 | btrfs_release_path(path); | |
4197 | ||
4198 | /* | |
4199 | * 1 - remove extent(s) | |
4200 | * 1 - inode update | |
4201 | */ | |
4202 | trans = btrfs_start_transaction(root, 2); | |
4203 | if (IS_ERR(trans)) { | |
4204 | ret = PTR_ERR(trans); | |
4205 | goto out; | |
4206 | } | |
4207 | ret = btrfs_drop_extents(trans, root, inode, | |
4208 | last_dest_end, destoff + len, 1); | |
4209 | if (ret) { | |
4210 | if (ret != -EOPNOTSUPP) | |
66642832 | 4211 | btrfs_abort_transaction(trans, ret); |
3a45bb20 | 4212 | btrfs_end_transaction(trans); |
f82a9901 FM |
4213 | goto out; |
4214 | } | |
a2f392e4 NB |
4215 | clone_update_extent_map(BTRFS_I(inode), trans, NULL, |
4216 | last_dest_end, | |
4217 | destoff + len - last_dest_end); | |
f82a9901 | 4218 | ret = clone_finish_inode_update(trans, inode, destoff + len, |
1c919a5e | 4219 | destoff, olen, no_time_update); |
f82a9901 FM |
4220 | } |
4221 | ||
f46b5a66 | 4222 | out: |
32b7c687 | 4223 | btrfs_free_path(path); |
15351955 | 4224 | kvfree(buf); |
32b7c687 MF |
4225 | return ret; |
4226 | } | |
4227 | ||
3db11b2e ZB |
4228 | static noinline int btrfs_clone_files(struct file *file, struct file *file_src, |
4229 | u64 off, u64 olen, u64 destoff) | |
32b7c687 | 4230 | { |
54563d41 | 4231 | struct inode *inode = file_inode(file); |
3db11b2e | 4232 | struct inode *src = file_inode(file_src); |
0b246afa | 4233 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
32b7c687 | 4234 | struct btrfs_root *root = BTRFS_I(inode)->root; |
32b7c687 MF |
4235 | int ret; |
4236 | u64 len = olen; | |
0b246afa | 4237 | u64 bs = fs_info->sb->s_blocksize; |
3db11b2e | 4238 | int same_inode = src == inode; |
32b7c687 MF |
4239 | |
4240 | /* | |
4241 | * TODO: | |
4242 | * - split compressed inline extents. annoying: we need to | |
4243 | * decompress into destination's address_space (the file offset | |
4244 | * may change, so source mapping won't do), then recompress (or | |
4245 | * otherwise reinsert) a subrange. | |
00fdf13a LB |
4246 | * |
4247 | * - split destination inode's inline extents. The inline extents can | |
4248 | * be either compressed or non-compressed. | |
32b7c687 MF |
4249 | */ |
4250 | ||
32b7c687 MF |
4251 | if (btrfs_root_readonly(root)) |
4252 | return -EROFS; | |
4253 | ||
3db11b2e ZB |
4254 | if (file_src->f_path.mnt != file->f_path.mnt || |
4255 | src->i_sb != inode->i_sb) | |
4256 | return -EXDEV; | |
32b7c687 | 4257 | |
32b7c687 | 4258 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) |
3db11b2e | 4259 | return -EISDIR; |
32b7c687 MF |
4260 | |
4261 | if (!same_inode) { | |
293a8489 | 4262 | btrfs_double_inode_lock(src, inode); |
32b7c687 | 4263 | } else { |
5955102c | 4264 | inode_lock(src); |
32b7c687 MF |
4265 | } |
4266 | ||
b5c40d59 OS |
4267 | /* don't make the dst file partly checksummed */ |
4268 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != | |
4269 | (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) { | |
4270 | ret = -EINVAL; | |
4271 | goto out_unlock; | |
4272 | } | |
4273 | ||
32b7c687 MF |
4274 | /* determine range to clone */ |
4275 | ret = -EINVAL; | |
4276 | if (off + len > src->i_size || off + len < off) | |
4277 | goto out_unlock; | |
4278 | if (len == 0) | |
4279 | olen = len = src->i_size - off; | |
4280 | /* if we extend to eof, continue to block boundary */ | |
4281 | if (off + len == src->i_size) | |
4282 | len = ALIGN(src->i_size, bs) - off; | |
4283 | ||
ccccf3d6 FM |
4284 | if (len == 0) { |
4285 | ret = 0; | |
4286 | goto out_unlock; | |
4287 | } | |
4288 | ||
32b7c687 MF |
4289 | /* verify the end result is block aligned */ |
4290 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || | |
4291 | !IS_ALIGNED(destoff, bs)) | |
4292 | goto out_unlock; | |
4293 | ||
4294 | /* verify if ranges are overlapped within the same file */ | |
4295 | if (same_inode) { | |
4296 | if (destoff + len > off && destoff < off + len) | |
4297 | goto out_unlock; | |
4298 | } | |
4299 | ||
4300 | if (destoff > inode->i_size) { | |
4301 | ret = btrfs_cont_expand(inode, inode->i_size, destoff); | |
4302 | if (ret) | |
4303 | goto out_unlock; | |
4304 | } | |
4305 | ||
c125b8bf FM |
4306 | /* |
4307 | * Lock the target range too. Right after we replace the file extent | |
4308 | * items in the fs tree (which now point to the cloned data), we might | |
4309 | * have a worker replace them with extent items relative to a write | |
4310 | * operation that was issued before this clone operation (i.e. confront | |
4311 | * with inode.c:btrfs_finish_ordered_io). | |
4312 | */ | |
4313 | if (same_inode) { | |
4314 | u64 lock_start = min_t(u64, off, destoff); | |
4315 | u64 lock_len = max_t(u64, off, destoff) + len - lock_start; | |
32b7c687 | 4316 | |
e0bd70c6 | 4317 | ret = lock_extent_range(src, lock_start, lock_len, true); |
c125b8bf | 4318 | } else { |
e0bd70c6 FM |
4319 | ret = btrfs_double_extent_lock(src, off, inode, destoff, len, |
4320 | true); | |
4321 | } | |
4322 | ASSERT(ret == 0); | |
4323 | if (WARN_ON(ret)) { | |
4324 | /* ranges in the io trees already unlocked */ | |
4325 | goto out_unlock; | |
c125b8bf | 4326 | } |
32b7c687 | 4327 | |
1c919a5e | 4328 | ret = btrfs_clone(src, inode, off, olen, len, destoff, 0); |
32b7c687 | 4329 | |
c125b8bf FM |
4330 | if (same_inode) { |
4331 | u64 lock_start = min_t(u64, off, destoff); | |
4332 | u64 lock_end = max_t(u64, off, destoff) + len - 1; | |
4333 | ||
4334 | unlock_extent(&BTRFS_I(src)->io_tree, lock_start, lock_end); | |
4335 | } else { | |
293a8489 | 4336 | btrfs_double_extent_unlock(src, off, inode, destoff, len); |
c125b8bf FM |
4337 | } |
4338 | /* | |
4339 | * Truncate page cache pages so that future reads will see the cloned | |
4340 | * data immediately and not the previous data. | |
4341 | */ | |
65bfa658 | 4342 | truncate_inode_pages_range(&inode->i_data, |
09cbfeaf KS |
4343 | round_down(destoff, PAGE_SIZE), |
4344 | round_up(destoff + len, PAGE_SIZE) - 1); | |
f46b5a66 | 4345 | out_unlock: |
293a8489 MF |
4346 | if (!same_inode) |
4347 | btrfs_double_inode_unlock(src, inode); | |
4348 | else | |
5955102c | 4349 | inode_unlock(src); |
3db11b2e ZB |
4350 | return ret; |
4351 | } | |
4352 | ||
04b38d60 CH |
4353 | int btrfs_clone_file_range(struct file *src_file, loff_t off, |
4354 | struct file *dst_file, loff_t destoff, u64 len) | |
3db11b2e | 4355 | { |
04b38d60 | 4356 | return btrfs_clone_files(dst_file, src_file, off, len, destoff); |
c5c9cd4d SW |
4357 | } |
4358 | ||
6ef5ed0d JB |
4359 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
4360 | { | |
496ad9aa | 4361 | struct inode *inode = file_inode(file); |
0b246afa | 4362 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
6ef5ed0d JB |
4363 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4364 | struct btrfs_root *new_root; | |
4365 | struct btrfs_dir_item *di; | |
4366 | struct btrfs_trans_handle *trans; | |
4367 | struct btrfs_path *path; | |
4368 | struct btrfs_key location; | |
4369 | struct btrfs_disk_key disk_key; | |
6ef5ed0d JB |
4370 | u64 objectid = 0; |
4371 | u64 dir_id; | |
3c04ce01 | 4372 | int ret; |
6ef5ed0d JB |
4373 | |
4374 | if (!capable(CAP_SYS_ADMIN)) | |
4375 | return -EPERM; | |
4376 | ||
3c04ce01 MX |
4377 | ret = mnt_want_write_file(file); |
4378 | if (ret) | |
4379 | return ret; | |
4380 | ||
4381 | if (copy_from_user(&objectid, argp, sizeof(objectid))) { | |
4382 | ret = -EFAULT; | |
4383 | goto out; | |
4384 | } | |
6ef5ed0d JB |
4385 | |
4386 | if (!objectid) | |
1cecf579 | 4387 | objectid = BTRFS_FS_TREE_OBJECTID; |
6ef5ed0d JB |
4388 | |
4389 | location.objectid = objectid; | |
4390 | location.type = BTRFS_ROOT_ITEM_KEY; | |
4391 | location.offset = (u64)-1; | |
4392 | ||
0b246afa | 4393 | new_root = btrfs_read_fs_root_no_name(fs_info, &location); |
3c04ce01 MX |
4394 | if (IS_ERR(new_root)) { |
4395 | ret = PTR_ERR(new_root); | |
4396 | goto out; | |
4397 | } | |
4fd786e6 | 4398 | if (!is_fstree(new_root->root_key.objectid)) { |
6d6d2829 | 4399 | ret = -ENOENT; |
4400 | goto out; | |
4401 | } | |
6ef5ed0d | 4402 | |
6ef5ed0d | 4403 | path = btrfs_alloc_path(); |
3c04ce01 MX |
4404 | if (!path) { |
4405 | ret = -ENOMEM; | |
4406 | goto out; | |
4407 | } | |
6ef5ed0d JB |
4408 | path->leave_spinning = 1; |
4409 | ||
4410 | trans = btrfs_start_transaction(root, 1); | |
98d5dc13 | 4411 | if (IS_ERR(trans)) { |
6ef5ed0d | 4412 | btrfs_free_path(path); |
3c04ce01 MX |
4413 | ret = PTR_ERR(trans); |
4414 | goto out; | |
6ef5ed0d JB |
4415 | } |
4416 | ||
0b246afa JM |
4417 | dir_id = btrfs_super_root_dir(fs_info->super_copy); |
4418 | di = btrfs_lookup_dir_item(trans, fs_info->tree_root, path, | |
6ef5ed0d | 4419 | dir_id, "default", 7, 1); |
cf1e99a4 | 4420 | if (IS_ERR_OR_NULL(di)) { |
6ef5ed0d | 4421 | btrfs_free_path(path); |
3a45bb20 | 4422 | btrfs_end_transaction(trans); |
0b246afa | 4423 | btrfs_err(fs_info, |
5d163e0e | 4424 | "Umm, you don't have the default diritem, this isn't going to work"); |
3c04ce01 MX |
4425 | ret = -ENOENT; |
4426 | goto out; | |
6ef5ed0d JB |
4427 | } |
4428 | ||
4429 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); | |
4430 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); | |
4431 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
4432 | btrfs_free_path(path); | |
4433 | ||
0b246afa | 4434 | btrfs_set_fs_incompat(fs_info, DEFAULT_SUBVOL); |
3a45bb20 | 4435 | btrfs_end_transaction(trans); |
3c04ce01 MX |
4436 | out: |
4437 | mnt_drop_write_file(file); | |
4438 | return ret; | |
6ef5ed0d JB |
4439 | } |
4440 | ||
c065f5b1 SY |
4441 | static void get_block_group_info(struct list_head *groups_list, |
4442 | struct btrfs_ioctl_space_info *space) | |
bf5fc093 JB |
4443 | { |
4444 | struct btrfs_block_group_cache *block_group; | |
4445 | ||
4446 | space->total_bytes = 0; | |
4447 | space->used_bytes = 0; | |
4448 | space->flags = 0; | |
4449 | list_for_each_entry(block_group, groups_list, list) { | |
4450 | space->flags = block_group->flags; | |
4451 | space->total_bytes += block_group->key.offset; | |
4452 | space->used_bytes += | |
4453 | btrfs_block_group_used(&block_group->item); | |
4454 | } | |
4455 | } | |
4456 | ||
2ff7e61e JM |
4457 | static long btrfs_ioctl_space_info(struct btrfs_fs_info *fs_info, |
4458 | void __user *arg) | |
1406e432 JB |
4459 | { |
4460 | struct btrfs_ioctl_space_args space_args; | |
4461 | struct btrfs_ioctl_space_info space; | |
4462 | struct btrfs_ioctl_space_info *dest; | |
7fde62bf | 4463 | struct btrfs_ioctl_space_info *dest_orig; |
13f2696f | 4464 | struct btrfs_ioctl_space_info __user *user_dest; |
1406e432 | 4465 | struct btrfs_space_info *info; |
315d8e98 CIK |
4466 | static const u64 types[] = { |
4467 | BTRFS_BLOCK_GROUP_DATA, | |
4468 | BTRFS_BLOCK_GROUP_SYSTEM, | |
4469 | BTRFS_BLOCK_GROUP_METADATA, | |
4470 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA | |
4471 | }; | |
bf5fc093 | 4472 | int num_types = 4; |
7fde62bf | 4473 | int alloc_size; |
1406e432 | 4474 | int ret = 0; |
51788b1b | 4475 | u64 slot_count = 0; |
bf5fc093 | 4476 | int i, c; |
1406e432 JB |
4477 | |
4478 | if (copy_from_user(&space_args, | |
4479 | (struct btrfs_ioctl_space_args __user *)arg, | |
4480 | sizeof(space_args))) | |
4481 | return -EFAULT; | |
4482 | ||
bf5fc093 JB |
4483 | for (i = 0; i < num_types; i++) { |
4484 | struct btrfs_space_info *tmp; | |
4485 | ||
4486 | info = NULL; | |
4487 | rcu_read_lock(); | |
0b246afa | 4488 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
bf5fc093 JB |
4489 | list) { |
4490 | if (tmp->flags == types[i]) { | |
4491 | info = tmp; | |
4492 | break; | |
4493 | } | |
4494 | } | |
4495 | rcu_read_unlock(); | |
4496 | ||
4497 | if (!info) | |
4498 | continue; | |
4499 | ||
4500 | down_read(&info->groups_sem); | |
4501 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
4502 | if (!list_empty(&info->block_groups[c])) | |
4503 | slot_count++; | |
4504 | } | |
4505 | up_read(&info->groups_sem); | |
4506 | } | |
7fde62bf | 4507 | |
36523e95 DS |
4508 | /* |
4509 | * Global block reserve, exported as a space_info | |
4510 | */ | |
4511 | slot_count++; | |
4512 | ||
7fde62bf CM |
4513 | /* space_slots == 0 means they are asking for a count */ |
4514 | if (space_args.space_slots == 0) { | |
4515 | space_args.total_spaces = slot_count; | |
4516 | goto out; | |
4517 | } | |
bf5fc093 | 4518 | |
51788b1b | 4519 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
bf5fc093 | 4520 | |
7fde62bf | 4521 | alloc_size = sizeof(*dest) * slot_count; |
bf5fc093 | 4522 | |
7fde62bf CM |
4523 | /* we generally have at most 6 or so space infos, one for each raid |
4524 | * level. So, a whole page should be more than enough for everyone | |
4525 | */ | |
09cbfeaf | 4526 | if (alloc_size > PAGE_SIZE) |
7fde62bf CM |
4527 | return -ENOMEM; |
4528 | ||
1406e432 | 4529 | space_args.total_spaces = 0; |
8d2db785 | 4530 | dest = kmalloc(alloc_size, GFP_KERNEL); |
7fde62bf CM |
4531 | if (!dest) |
4532 | return -ENOMEM; | |
4533 | dest_orig = dest; | |
1406e432 | 4534 | |
7fde62bf | 4535 | /* now we have a buffer to copy into */ |
bf5fc093 JB |
4536 | for (i = 0; i < num_types; i++) { |
4537 | struct btrfs_space_info *tmp; | |
4538 | ||
51788b1b DR |
4539 | if (!slot_count) |
4540 | break; | |
4541 | ||
bf5fc093 JB |
4542 | info = NULL; |
4543 | rcu_read_lock(); | |
0b246afa | 4544 | list_for_each_entry_rcu(tmp, &fs_info->space_info, |
bf5fc093 JB |
4545 | list) { |
4546 | if (tmp->flags == types[i]) { | |
4547 | info = tmp; | |
4548 | break; | |
4549 | } | |
4550 | } | |
4551 | rcu_read_unlock(); | |
7fde62bf | 4552 | |
bf5fc093 JB |
4553 | if (!info) |
4554 | continue; | |
4555 | down_read(&info->groups_sem); | |
4556 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
4557 | if (!list_empty(&info->block_groups[c])) { | |
c065f5b1 SY |
4558 | get_block_group_info(&info->block_groups[c], |
4559 | &space); | |
bf5fc093 JB |
4560 | memcpy(dest, &space, sizeof(space)); |
4561 | dest++; | |
4562 | space_args.total_spaces++; | |
51788b1b | 4563 | slot_count--; |
bf5fc093 | 4564 | } |
51788b1b DR |
4565 | if (!slot_count) |
4566 | break; | |
bf5fc093 JB |
4567 | } |
4568 | up_read(&info->groups_sem); | |
1406e432 | 4569 | } |
1406e432 | 4570 | |
36523e95 DS |
4571 | /* |
4572 | * Add global block reserve | |
4573 | */ | |
4574 | if (slot_count) { | |
0b246afa | 4575 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; |
36523e95 DS |
4576 | |
4577 | spin_lock(&block_rsv->lock); | |
4578 | space.total_bytes = block_rsv->size; | |
4579 | space.used_bytes = block_rsv->size - block_rsv->reserved; | |
4580 | spin_unlock(&block_rsv->lock); | |
4581 | space.flags = BTRFS_SPACE_INFO_GLOBAL_RSV; | |
4582 | memcpy(dest, &space, sizeof(space)); | |
4583 | space_args.total_spaces++; | |
4584 | } | |
4585 | ||
2eec6c81 | 4586 | user_dest = (struct btrfs_ioctl_space_info __user *) |
7fde62bf CM |
4587 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
4588 | ||
4589 | if (copy_to_user(user_dest, dest_orig, alloc_size)) | |
4590 | ret = -EFAULT; | |
4591 | ||
4592 | kfree(dest_orig); | |
4593 | out: | |
4594 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) | |
1406e432 JB |
4595 | ret = -EFAULT; |
4596 | ||
4597 | return ret; | |
4598 | } | |
4599 | ||
9a8c28be MX |
4600 | static noinline long btrfs_ioctl_start_sync(struct btrfs_root *root, |
4601 | void __user *argp) | |
46204592 | 4602 | { |
46204592 SW |
4603 | struct btrfs_trans_handle *trans; |
4604 | u64 transid; | |
db5b493a | 4605 | int ret; |
46204592 | 4606 | |
d4edf39b | 4607 | trans = btrfs_attach_transaction_barrier(root); |
ff7c1d33 MX |
4608 | if (IS_ERR(trans)) { |
4609 | if (PTR_ERR(trans) != -ENOENT) | |
4610 | return PTR_ERR(trans); | |
4611 | ||
4612 | /* No running transaction, don't bother */ | |
4613 | transid = root->fs_info->last_trans_committed; | |
4614 | goto out; | |
4615 | } | |
46204592 | 4616 | transid = trans->transid; |
3a45bb20 | 4617 | ret = btrfs_commit_transaction_async(trans, 0); |
8b2b2d3c | 4618 | if (ret) { |
3a45bb20 | 4619 | btrfs_end_transaction(trans); |
db5b493a | 4620 | return ret; |
8b2b2d3c | 4621 | } |
ff7c1d33 | 4622 | out: |
46204592 SW |
4623 | if (argp) |
4624 | if (copy_to_user(argp, &transid, sizeof(transid))) | |
4625 | return -EFAULT; | |
4626 | return 0; | |
4627 | } | |
4628 | ||
2ff7e61e | 4629 | static noinline long btrfs_ioctl_wait_sync(struct btrfs_fs_info *fs_info, |
9a8c28be | 4630 | void __user *argp) |
46204592 | 4631 | { |
46204592 SW |
4632 | u64 transid; |
4633 | ||
4634 | if (argp) { | |
4635 | if (copy_from_user(&transid, argp, sizeof(transid))) | |
4636 | return -EFAULT; | |
4637 | } else { | |
4638 | transid = 0; /* current trans */ | |
4639 | } | |
2ff7e61e | 4640 | return btrfs_wait_for_commit(fs_info, transid); |
46204592 SW |
4641 | } |
4642 | ||
b8e95489 | 4643 | static long btrfs_ioctl_scrub(struct file *file, void __user *arg) |
475f6387 | 4644 | { |
0b246afa | 4645 | struct btrfs_fs_info *fs_info = btrfs_sb(file_inode(file)->i_sb); |
475f6387 | 4646 | struct btrfs_ioctl_scrub_args *sa; |
b8e95489 | 4647 | int ret; |
475f6387 JS |
4648 | |
4649 | if (!capable(CAP_SYS_ADMIN)) | |
4650 | return -EPERM; | |
4651 | ||
4652 | sa = memdup_user(arg, sizeof(*sa)); | |
4653 | if (IS_ERR(sa)) | |
4654 | return PTR_ERR(sa); | |
4655 | ||
b8e95489 MX |
4656 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) { |
4657 | ret = mnt_want_write_file(file); | |
4658 | if (ret) | |
4659 | goto out; | |
4660 | } | |
4661 | ||
0b246afa | 4662 | ret = btrfs_scrub_dev(fs_info, sa->devid, sa->start, sa->end, |
63a212ab SB |
4663 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY, |
4664 | 0); | |
475f6387 JS |
4665 | |
4666 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
4667 | ret = -EFAULT; | |
4668 | ||
b8e95489 MX |
4669 | if (!(sa->flags & BTRFS_SCRUB_READONLY)) |
4670 | mnt_drop_write_file(file); | |
4671 | out: | |
475f6387 JS |
4672 | kfree(sa); |
4673 | return ret; | |
4674 | } | |
4675 | ||
2ff7e61e | 4676 | static long btrfs_ioctl_scrub_cancel(struct btrfs_fs_info *fs_info) |
475f6387 JS |
4677 | { |
4678 | if (!capable(CAP_SYS_ADMIN)) | |
4679 | return -EPERM; | |
4680 | ||
2ff7e61e | 4681 | return btrfs_scrub_cancel(fs_info); |
475f6387 JS |
4682 | } |
4683 | ||
2ff7e61e | 4684 | static long btrfs_ioctl_scrub_progress(struct btrfs_fs_info *fs_info, |
475f6387 JS |
4685 | void __user *arg) |
4686 | { | |
4687 | struct btrfs_ioctl_scrub_args *sa; | |
4688 | int ret; | |
4689 | ||
4690 | if (!capable(CAP_SYS_ADMIN)) | |
4691 | return -EPERM; | |
4692 | ||
4693 | sa = memdup_user(arg, sizeof(*sa)); | |
4694 | if (IS_ERR(sa)) | |
4695 | return PTR_ERR(sa); | |
4696 | ||
2ff7e61e | 4697 | ret = btrfs_scrub_progress(fs_info, sa->devid, &sa->progress); |
475f6387 JS |
4698 | |
4699 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
4700 | ret = -EFAULT; | |
4701 | ||
4702 | kfree(sa); | |
4703 | return ret; | |
4704 | } | |
4705 | ||
2ff7e61e | 4706 | static long btrfs_ioctl_get_dev_stats(struct btrfs_fs_info *fs_info, |
b27f7c0c | 4707 | void __user *arg) |
c11d2c23 SB |
4708 | { |
4709 | struct btrfs_ioctl_get_dev_stats *sa; | |
4710 | int ret; | |
4711 | ||
c11d2c23 SB |
4712 | sa = memdup_user(arg, sizeof(*sa)); |
4713 | if (IS_ERR(sa)) | |
4714 | return PTR_ERR(sa); | |
4715 | ||
b27f7c0c DS |
4716 | if ((sa->flags & BTRFS_DEV_STATS_RESET) && !capable(CAP_SYS_ADMIN)) { |
4717 | kfree(sa); | |
4718 | return -EPERM; | |
4719 | } | |
4720 | ||
2ff7e61e | 4721 | ret = btrfs_get_dev_stats(fs_info, sa); |
c11d2c23 SB |
4722 | |
4723 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
4724 | ret = -EFAULT; | |
4725 | ||
4726 | kfree(sa); | |
4727 | return ret; | |
4728 | } | |
4729 | ||
2ff7e61e JM |
4730 | static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info, |
4731 | void __user *arg) | |
3f6bcfbd SB |
4732 | { |
4733 | struct btrfs_ioctl_dev_replace_args *p; | |
4734 | int ret; | |
4735 | ||
4736 | if (!capable(CAP_SYS_ADMIN)) | |
4737 | return -EPERM; | |
4738 | ||
4739 | p = memdup_user(arg, sizeof(*p)); | |
4740 | if (IS_ERR(p)) | |
4741 | return PTR_ERR(p); | |
4742 | ||
4743 | switch (p->cmd) { | |
4744 | case BTRFS_IOCTL_DEV_REPLACE_CMD_START: | |
bc98a42c | 4745 | if (sb_rdonly(fs_info->sb)) { |
adfa97cb ID |
4746 | ret = -EROFS; |
4747 | goto out; | |
4748 | } | |
171938e5 | 4749 | if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
e57138b3 | 4750 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
3f6bcfbd | 4751 | } else { |
2ff7e61e | 4752 | ret = btrfs_dev_replace_by_ioctl(fs_info, p); |
171938e5 | 4753 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
3f6bcfbd SB |
4754 | } |
4755 | break; | |
4756 | case BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS: | |
0b246afa | 4757 | btrfs_dev_replace_status(fs_info, p); |
3f6bcfbd SB |
4758 | ret = 0; |
4759 | break; | |
4760 | case BTRFS_IOCTL_DEV_REPLACE_CMD_CANCEL: | |
17d202b9 | 4761 | p->result = btrfs_dev_replace_cancel(fs_info); |
97282031 | 4762 | ret = 0; |
3f6bcfbd SB |
4763 | break; |
4764 | default: | |
4765 | ret = -EINVAL; | |
4766 | break; | |
4767 | } | |
4768 | ||
4769 | if (copy_to_user(arg, p, sizeof(*p))) | |
4770 | ret = -EFAULT; | |
adfa97cb | 4771 | out: |
3f6bcfbd SB |
4772 | kfree(p); |
4773 | return ret; | |
4774 | } | |
4775 | ||
d7728c96 JS |
4776 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
4777 | { | |
4778 | int ret = 0; | |
4779 | int i; | |
740c3d22 | 4780 | u64 rel_ptr; |
d7728c96 | 4781 | int size; |
806468f8 | 4782 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
d7728c96 JS |
4783 | struct inode_fs_paths *ipath = NULL; |
4784 | struct btrfs_path *path; | |
4785 | ||
82b22ac8 | 4786 | if (!capable(CAP_DAC_READ_SEARCH)) |
d7728c96 JS |
4787 | return -EPERM; |
4788 | ||
4789 | path = btrfs_alloc_path(); | |
4790 | if (!path) { | |
4791 | ret = -ENOMEM; | |
4792 | goto out; | |
4793 | } | |
4794 | ||
4795 | ipa = memdup_user(arg, sizeof(*ipa)); | |
4796 | if (IS_ERR(ipa)) { | |
4797 | ret = PTR_ERR(ipa); | |
4798 | ipa = NULL; | |
4799 | goto out; | |
4800 | } | |
4801 | ||
4802 | size = min_t(u32, ipa->size, 4096); | |
4803 | ipath = init_ipath(size, root, path); | |
4804 | if (IS_ERR(ipath)) { | |
4805 | ret = PTR_ERR(ipath); | |
4806 | ipath = NULL; | |
4807 | goto out; | |
4808 | } | |
4809 | ||
4810 | ret = paths_from_inode(ipa->inum, ipath); | |
4811 | if (ret < 0) | |
4812 | goto out; | |
4813 | ||
4814 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { | |
745c4d8e JM |
4815 | rel_ptr = ipath->fspath->val[i] - |
4816 | (u64)(unsigned long)ipath->fspath->val; | |
740c3d22 | 4817 | ipath->fspath->val[i] = rel_ptr; |
d7728c96 JS |
4818 | } |
4819 | ||
718dc5fa OS |
4820 | ret = copy_to_user((void __user *)(unsigned long)ipa->fspath, |
4821 | ipath->fspath, size); | |
d7728c96 JS |
4822 | if (ret) { |
4823 | ret = -EFAULT; | |
4824 | goto out; | |
4825 | } | |
4826 | ||
4827 | out: | |
4828 | btrfs_free_path(path); | |
4829 | free_ipath(ipath); | |
4830 | kfree(ipa); | |
4831 | ||
4832 | return ret; | |
4833 | } | |
4834 | ||
4835 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) | |
4836 | { | |
4837 | struct btrfs_data_container *inodes = ctx; | |
4838 | const size_t c = 3 * sizeof(u64); | |
4839 | ||
4840 | if (inodes->bytes_left >= c) { | |
4841 | inodes->bytes_left -= c; | |
4842 | inodes->val[inodes->elem_cnt] = inum; | |
4843 | inodes->val[inodes->elem_cnt + 1] = offset; | |
4844 | inodes->val[inodes->elem_cnt + 2] = root; | |
4845 | inodes->elem_cnt += 3; | |
4846 | } else { | |
4847 | inodes->bytes_missing += c - inodes->bytes_left; | |
4848 | inodes->bytes_left = 0; | |
4849 | inodes->elem_missed += 3; | |
4850 | } | |
4851 | ||
4852 | return 0; | |
4853 | } | |
4854 | ||
2ff7e61e | 4855 | static long btrfs_ioctl_logical_to_ino(struct btrfs_fs_info *fs_info, |
d24a67b2 | 4856 | void __user *arg, int version) |
d7728c96 JS |
4857 | { |
4858 | int ret = 0; | |
4859 | int size; | |
d7728c96 JS |
4860 | struct btrfs_ioctl_logical_ino_args *loi; |
4861 | struct btrfs_data_container *inodes = NULL; | |
4862 | struct btrfs_path *path = NULL; | |
d24a67b2 | 4863 | bool ignore_offset; |
d7728c96 JS |
4864 | |
4865 | if (!capable(CAP_SYS_ADMIN)) | |
4866 | return -EPERM; | |
4867 | ||
4868 | loi = memdup_user(arg, sizeof(*loi)); | |
7b9ea627 SV |
4869 | if (IS_ERR(loi)) |
4870 | return PTR_ERR(loi); | |
d7728c96 | 4871 | |
d24a67b2 ZB |
4872 | if (version == 1) { |
4873 | ignore_offset = false; | |
b115e3bc | 4874 | size = min_t(u32, loi->size, SZ_64K); |
d24a67b2 ZB |
4875 | } else { |
4876 | /* All reserved bits must be 0 for now */ | |
4877 | if (memchr_inv(loi->reserved, 0, sizeof(loi->reserved))) { | |
4878 | ret = -EINVAL; | |
4879 | goto out_loi; | |
4880 | } | |
4881 | /* Only accept flags we have defined so far */ | |
4882 | if (loi->flags & ~(BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET)) { | |
4883 | ret = -EINVAL; | |
4884 | goto out_loi; | |
4885 | } | |
4886 | ignore_offset = loi->flags & BTRFS_LOGICAL_INO_ARGS_IGNORE_OFFSET; | |
b115e3bc | 4887 | size = min_t(u32, loi->size, SZ_16M); |
d24a67b2 ZB |
4888 | } |
4889 | ||
d7728c96 JS |
4890 | path = btrfs_alloc_path(); |
4891 | if (!path) { | |
4892 | ret = -ENOMEM; | |
4893 | goto out; | |
4894 | } | |
4895 | ||
d7728c96 JS |
4896 | inodes = init_data_container(size); |
4897 | if (IS_ERR(inodes)) { | |
4898 | ret = PTR_ERR(inodes); | |
4899 | inodes = NULL; | |
4900 | goto out; | |
4901 | } | |
4902 | ||
2ff7e61e | 4903 | ret = iterate_inodes_from_logical(loi->logical, fs_info, path, |
d24a67b2 | 4904 | build_ino_list, inodes, ignore_offset); |
df031f07 | 4905 | if (ret == -EINVAL) |
d7728c96 JS |
4906 | ret = -ENOENT; |
4907 | if (ret < 0) | |
4908 | goto out; | |
4909 | ||
718dc5fa OS |
4910 | ret = copy_to_user((void __user *)(unsigned long)loi->inodes, inodes, |
4911 | size); | |
d7728c96 JS |
4912 | if (ret) |
4913 | ret = -EFAULT; | |
4914 | ||
4915 | out: | |
4916 | btrfs_free_path(path); | |
f54de068 | 4917 | kvfree(inodes); |
d24a67b2 | 4918 | out_loi: |
d7728c96 JS |
4919 | kfree(loi); |
4920 | ||
4921 | return ret; | |
4922 | } | |
4923 | ||
008ef096 | 4924 | void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info, |
c9e9f97b ID |
4925 | struct btrfs_ioctl_balance_args *bargs) |
4926 | { | |
4927 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
4928 | ||
4929 | bargs->flags = bctl->flags; | |
4930 | ||
3009a62f | 4931 | if (test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) |
837d5b6e ID |
4932 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; |
4933 | if (atomic_read(&fs_info->balance_pause_req)) | |
4934 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; | |
a7e99c69 ID |
4935 | if (atomic_read(&fs_info->balance_cancel_req)) |
4936 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; | |
837d5b6e | 4937 | |
c9e9f97b ID |
4938 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
4939 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); | |
4940 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); | |
19a39dce | 4941 | |
008ef096 DS |
4942 | spin_lock(&fs_info->balance_lock); |
4943 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); | |
4944 | spin_unlock(&fs_info->balance_lock); | |
c9e9f97b ID |
4945 | } |
4946 | ||
9ba1f6e4 | 4947 | static long btrfs_ioctl_balance(struct file *file, void __user *arg) |
c9e9f97b | 4948 | { |
496ad9aa | 4949 | struct btrfs_root *root = BTRFS_I(file_inode(file))->root; |
c9e9f97b ID |
4950 | struct btrfs_fs_info *fs_info = root->fs_info; |
4951 | struct btrfs_ioctl_balance_args *bargs; | |
4952 | struct btrfs_balance_control *bctl; | |
ed0fb78f | 4953 | bool need_unlock; /* for mut. excl. ops lock */ |
c9e9f97b ID |
4954 | int ret; |
4955 | ||
4956 | if (!capable(CAP_SYS_ADMIN)) | |
4957 | return -EPERM; | |
4958 | ||
e54bfa31 | 4959 | ret = mnt_want_write_file(file); |
9ba1f6e4 LB |
4960 | if (ret) |
4961 | return ret; | |
4962 | ||
ed0fb78f | 4963 | again: |
171938e5 | 4964 | if (!test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) { |
ed0fb78f ID |
4965 | mutex_lock(&fs_info->balance_mutex); |
4966 | need_unlock = true; | |
4967 | goto locked; | |
4968 | } | |
4969 | ||
4970 | /* | |
01327610 | 4971 | * mut. excl. ops lock is locked. Three possibilities: |
ed0fb78f ID |
4972 | * (1) some other op is running |
4973 | * (2) balance is running | |
4974 | * (3) balance is paused -- special case (think resume) | |
4975 | */ | |
c9e9f97b | 4976 | mutex_lock(&fs_info->balance_mutex); |
ed0fb78f ID |
4977 | if (fs_info->balance_ctl) { |
4978 | /* this is either (2) or (3) */ | |
3009a62f | 4979 | if (!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
ed0fb78f | 4980 | mutex_unlock(&fs_info->balance_mutex); |
dccdb07b DS |
4981 | /* |
4982 | * Lock released to allow other waiters to continue, | |
4983 | * we'll reexamine the status again. | |
4984 | */ | |
ed0fb78f ID |
4985 | mutex_lock(&fs_info->balance_mutex); |
4986 | ||
4987 | if (fs_info->balance_ctl && | |
3009a62f | 4988 | !test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags)) { |
ed0fb78f ID |
4989 | /* this is (3) */ |
4990 | need_unlock = false; | |
4991 | goto locked; | |
4992 | } | |
4993 | ||
4994 | mutex_unlock(&fs_info->balance_mutex); | |
ed0fb78f ID |
4995 | goto again; |
4996 | } else { | |
4997 | /* this is (2) */ | |
4998 | mutex_unlock(&fs_info->balance_mutex); | |
4999 | ret = -EINPROGRESS; | |
5000 | goto out; | |
5001 | } | |
5002 | } else { | |
5003 | /* this is (1) */ | |
5004 | mutex_unlock(&fs_info->balance_mutex); | |
e57138b3 | 5005 | ret = BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS; |
ed0fb78f ID |
5006 | goto out; |
5007 | } | |
5008 | ||
5009 | locked: | |
171938e5 | 5010 | BUG_ON(!test_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)); |
c9e9f97b ID |
5011 | |
5012 | if (arg) { | |
5013 | bargs = memdup_user(arg, sizeof(*bargs)); | |
5014 | if (IS_ERR(bargs)) { | |
5015 | ret = PTR_ERR(bargs); | |
ed0fb78f | 5016 | goto out_unlock; |
c9e9f97b | 5017 | } |
de322263 ID |
5018 | |
5019 | if (bargs->flags & BTRFS_BALANCE_RESUME) { | |
5020 | if (!fs_info->balance_ctl) { | |
5021 | ret = -ENOTCONN; | |
5022 | goto out_bargs; | |
5023 | } | |
5024 | ||
5025 | bctl = fs_info->balance_ctl; | |
5026 | spin_lock(&fs_info->balance_lock); | |
5027 | bctl->flags |= BTRFS_BALANCE_RESUME; | |
5028 | spin_unlock(&fs_info->balance_lock); | |
5029 | ||
5030 | goto do_balance; | |
5031 | } | |
c9e9f97b ID |
5032 | } else { |
5033 | bargs = NULL; | |
5034 | } | |
5035 | ||
ed0fb78f | 5036 | if (fs_info->balance_ctl) { |
837d5b6e ID |
5037 | ret = -EINPROGRESS; |
5038 | goto out_bargs; | |
5039 | } | |
5040 | ||
8d2db785 | 5041 | bctl = kzalloc(sizeof(*bctl), GFP_KERNEL); |
c9e9f97b ID |
5042 | if (!bctl) { |
5043 | ret = -ENOMEM; | |
5044 | goto out_bargs; | |
5045 | } | |
5046 | ||
c9e9f97b ID |
5047 | if (arg) { |
5048 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); | |
5049 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); | |
5050 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); | |
5051 | ||
5052 | bctl->flags = bargs->flags; | |
f43ffb60 ID |
5053 | } else { |
5054 | /* balance everything - no filters */ | |
5055 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; | |
c9e9f97b ID |
5056 | } |
5057 | ||
8eb93459 DS |
5058 | if (bctl->flags & ~(BTRFS_BALANCE_ARGS_MASK | BTRFS_BALANCE_TYPE_MASK)) { |
5059 | ret = -EINVAL; | |
0f89abf5 | 5060 | goto out_bctl; |
8eb93459 DS |
5061 | } |
5062 | ||
de322263 | 5063 | do_balance: |
c9e9f97b | 5064 | /* |
149196a2 DS |
5065 | * Ownership of bctl and filesystem flag BTRFS_FS_EXCL_OP goes to |
5066 | * btrfs_balance. bctl is freed in reset_balance_state, or, if | |
5067 | * restriper was paused all the way until unmount, in free_fs_info. | |
5068 | * The flag should be cleared after reset_balance_state. | |
c9e9f97b | 5069 | */ |
ed0fb78f ID |
5070 | need_unlock = false; |
5071 | ||
6fcf6e2b | 5072 | ret = btrfs_balance(fs_info, bctl, bargs); |
0f89abf5 | 5073 | bctl = NULL; |
ed0fb78f | 5074 | |
c9e9f97b ID |
5075 | if (arg) { |
5076 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | |
5077 | ret = -EFAULT; | |
5078 | } | |
5079 | ||
0f89abf5 CE |
5080 | out_bctl: |
5081 | kfree(bctl); | |
c9e9f97b ID |
5082 | out_bargs: |
5083 | kfree(bargs); | |
ed0fb78f | 5084 | out_unlock: |
c9e9f97b | 5085 | mutex_unlock(&fs_info->balance_mutex); |
ed0fb78f | 5086 | if (need_unlock) |
171938e5 | 5087 | clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags); |
ed0fb78f | 5088 | out: |
e54bfa31 | 5089 | mnt_drop_write_file(file); |
c9e9f97b ID |
5090 | return ret; |
5091 | } | |
5092 | ||
2ff7e61e | 5093 | static long btrfs_ioctl_balance_ctl(struct btrfs_fs_info *fs_info, int cmd) |
837d5b6e ID |
5094 | { |
5095 | if (!capable(CAP_SYS_ADMIN)) | |
5096 | return -EPERM; | |
5097 | ||
5098 | switch (cmd) { | |
5099 | case BTRFS_BALANCE_CTL_PAUSE: | |
0b246afa | 5100 | return btrfs_pause_balance(fs_info); |
a7e99c69 | 5101 | case BTRFS_BALANCE_CTL_CANCEL: |
0b246afa | 5102 | return btrfs_cancel_balance(fs_info); |
837d5b6e ID |
5103 | } |
5104 | ||
5105 | return -EINVAL; | |
5106 | } | |
5107 | ||
2ff7e61e | 5108 | static long btrfs_ioctl_balance_progress(struct btrfs_fs_info *fs_info, |
19a39dce ID |
5109 | void __user *arg) |
5110 | { | |
19a39dce ID |
5111 | struct btrfs_ioctl_balance_args *bargs; |
5112 | int ret = 0; | |
5113 | ||
5114 | if (!capable(CAP_SYS_ADMIN)) | |
5115 | return -EPERM; | |
5116 | ||
5117 | mutex_lock(&fs_info->balance_mutex); | |
5118 | if (!fs_info->balance_ctl) { | |
5119 | ret = -ENOTCONN; | |
5120 | goto out; | |
5121 | } | |
5122 | ||
8d2db785 | 5123 | bargs = kzalloc(sizeof(*bargs), GFP_KERNEL); |
19a39dce ID |
5124 | if (!bargs) { |
5125 | ret = -ENOMEM; | |
5126 | goto out; | |
5127 | } | |
5128 | ||
008ef096 | 5129 | btrfs_update_ioctl_balance_args(fs_info, bargs); |
19a39dce ID |
5130 | |
5131 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | |
5132 | ret = -EFAULT; | |
5133 | ||
5134 | kfree(bargs); | |
5135 | out: | |
5136 | mutex_unlock(&fs_info->balance_mutex); | |
5137 | return ret; | |
5138 | } | |
5139 | ||
905b0dda | 5140 | static long btrfs_ioctl_quota_ctl(struct file *file, void __user *arg) |
5d13a37b | 5141 | { |
0b246afa JM |
5142 | struct inode *inode = file_inode(file); |
5143 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5d13a37b | 5144 | struct btrfs_ioctl_quota_ctl_args *sa; |
5d13a37b | 5145 | int ret; |
5d13a37b AJ |
5146 | |
5147 | if (!capable(CAP_SYS_ADMIN)) | |
5148 | return -EPERM; | |
5149 | ||
905b0dda MX |
5150 | ret = mnt_want_write_file(file); |
5151 | if (ret) | |
5152 | return ret; | |
5d13a37b AJ |
5153 | |
5154 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
5155 | if (IS_ERR(sa)) { |
5156 | ret = PTR_ERR(sa); | |
5157 | goto drop_write; | |
5158 | } | |
5d13a37b | 5159 | |
0b246afa | 5160 | down_write(&fs_info->subvol_sem); |
5d13a37b AJ |
5161 | |
5162 | switch (sa->cmd) { | |
5163 | case BTRFS_QUOTA_CTL_ENABLE: | |
340f1aa2 | 5164 | ret = btrfs_quota_enable(fs_info); |
5d13a37b AJ |
5165 | break; |
5166 | case BTRFS_QUOTA_CTL_DISABLE: | |
340f1aa2 | 5167 | ret = btrfs_quota_disable(fs_info); |
5d13a37b | 5168 | break; |
5d13a37b AJ |
5169 | default: |
5170 | ret = -EINVAL; | |
5171 | break; | |
5172 | } | |
5173 | ||
5d13a37b | 5174 | kfree(sa); |
0b246afa | 5175 | up_write(&fs_info->subvol_sem); |
905b0dda MX |
5176 | drop_write: |
5177 | mnt_drop_write_file(file); | |
5d13a37b AJ |
5178 | return ret; |
5179 | } | |
5180 | ||
905b0dda | 5181 | static long btrfs_ioctl_qgroup_assign(struct file *file, void __user *arg) |
5d13a37b | 5182 | { |
0b246afa JM |
5183 | struct inode *inode = file_inode(file); |
5184 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5185 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5d13a37b AJ |
5186 | struct btrfs_ioctl_qgroup_assign_args *sa; |
5187 | struct btrfs_trans_handle *trans; | |
5188 | int ret; | |
5189 | int err; | |
5190 | ||
5191 | if (!capable(CAP_SYS_ADMIN)) | |
5192 | return -EPERM; | |
5193 | ||
905b0dda MX |
5194 | ret = mnt_want_write_file(file); |
5195 | if (ret) | |
5196 | return ret; | |
5d13a37b AJ |
5197 | |
5198 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
5199 | if (IS_ERR(sa)) { |
5200 | ret = PTR_ERR(sa); | |
5201 | goto drop_write; | |
5202 | } | |
5d13a37b AJ |
5203 | |
5204 | trans = btrfs_join_transaction(root); | |
5205 | if (IS_ERR(trans)) { | |
5206 | ret = PTR_ERR(trans); | |
5207 | goto out; | |
5208 | } | |
5209 | ||
5d13a37b | 5210 | if (sa->assign) { |
9f8a6ce6 | 5211 | ret = btrfs_add_qgroup_relation(trans, sa->src, sa->dst); |
5d13a37b | 5212 | } else { |
39616c27 | 5213 | ret = btrfs_del_qgroup_relation(trans, sa->src, sa->dst); |
5d13a37b AJ |
5214 | } |
5215 | ||
e082f563 | 5216 | /* update qgroup status and info */ |
280f8bd2 | 5217 | err = btrfs_run_qgroups(trans); |
e082f563 | 5218 | if (err < 0) |
0b246afa JM |
5219 | btrfs_handle_fs_error(fs_info, err, |
5220 | "failed to update qgroup status and info"); | |
3a45bb20 | 5221 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
5222 | if (err && !ret) |
5223 | ret = err; | |
5224 | ||
5225 | out: | |
5226 | kfree(sa); | |
905b0dda MX |
5227 | drop_write: |
5228 | mnt_drop_write_file(file); | |
5d13a37b AJ |
5229 | return ret; |
5230 | } | |
5231 | ||
905b0dda | 5232 | static long btrfs_ioctl_qgroup_create(struct file *file, void __user *arg) |
5d13a37b | 5233 | { |
0b246afa | 5234 | struct inode *inode = file_inode(file); |
0b246afa | 5235 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5d13a37b AJ |
5236 | struct btrfs_ioctl_qgroup_create_args *sa; |
5237 | struct btrfs_trans_handle *trans; | |
5238 | int ret; | |
5239 | int err; | |
5240 | ||
5241 | if (!capable(CAP_SYS_ADMIN)) | |
5242 | return -EPERM; | |
5243 | ||
905b0dda MX |
5244 | ret = mnt_want_write_file(file); |
5245 | if (ret) | |
5246 | return ret; | |
5d13a37b AJ |
5247 | |
5248 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
5249 | if (IS_ERR(sa)) { |
5250 | ret = PTR_ERR(sa); | |
5251 | goto drop_write; | |
5252 | } | |
5d13a37b | 5253 | |
d86e56cf MX |
5254 | if (!sa->qgroupid) { |
5255 | ret = -EINVAL; | |
5256 | goto out; | |
5257 | } | |
5258 | ||
5d13a37b AJ |
5259 | trans = btrfs_join_transaction(root); |
5260 | if (IS_ERR(trans)) { | |
5261 | ret = PTR_ERR(trans); | |
5262 | goto out; | |
5263 | } | |
5264 | ||
5d13a37b | 5265 | if (sa->create) { |
49a05ecd | 5266 | ret = btrfs_create_qgroup(trans, sa->qgroupid); |
5d13a37b | 5267 | } else { |
3efbee1d | 5268 | ret = btrfs_remove_qgroup(trans, sa->qgroupid); |
5d13a37b AJ |
5269 | } |
5270 | ||
3a45bb20 | 5271 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
5272 | if (err && !ret) |
5273 | ret = err; | |
5274 | ||
5275 | out: | |
5276 | kfree(sa); | |
905b0dda MX |
5277 | drop_write: |
5278 | mnt_drop_write_file(file); | |
5d13a37b AJ |
5279 | return ret; |
5280 | } | |
5281 | ||
905b0dda | 5282 | static long btrfs_ioctl_qgroup_limit(struct file *file, void __user *arg) |
5d13a37b | 5283 | { |
0b246afa | 5284 | struct inode *inode = file_inode(file); |
0b246afa | 5285 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5d13a37b AJ |
5286 | struct btrfs_ioctl_qgroup_limit_args *sa; |
5287 | struct btrfs_trans_handle *trans; | |
5288 | int ret; | |
5289 | int err; | |
5290 | u64 qgroupid; | |
5291 | ||
5292 | if (!capable(CAP_SYS_ADMIN)) | |
5293 | return -EPERM; | |
5294 | ||
905b0dda MX |
5295 | ret = mnt_want_write_file(file); |
5296 | if (ret) | |
5297 | return ret; | |
5d13a37b AJ |
5298 | |
5299 | sa = memdup_user(arg, sizeof(*sa)); | |
905b0dda MX |
5300 | if (IS_ERR(sa)) { |
5301 | ret = PTR_ERR(sa); | |
5302 | goto drop_write; | |
5303 | } | |
5d13a37b AJ |
5304 | |
5305 | trans = btrfs_join_transaction(root); | |
5306 | if (IS_ERR(trans)) { | |
5307 | ret = PTR_ERR(trans); | |
5308 | goto out; | |
5309 | } | |
5310 | ||
5311 | qgroupid = sa->qgroupid; | |
5312 | if (!qgroupid) { | |
5313 | /* take the current subvol as qgroup */ | |
5314 | qgroupid = root->root_key.objectid; | |
5315 | } | |
5316 | ||
f0042d5e | 5317 | ret = btrfs_limit_qgroup(trans, qgroupid, &sa->lim); |
5d13a37b | 5318 | |
3a45bb20 | 5319 | err = btrfs_end_transaction(trans); |
5d13a37b AJ |
5320 | if (err && !ret) |
5321 | ret = err; | |
5322 | ||
5323 | out: | |
5324 | kfree(sa); | |
905b0dda MX |
5325 | drop_write: |
5326 | mnt_drop_write_file(file); | |
5d13a37b AJ |
5327 | return ret; |
5328 | } | |
5329 | ||
2f232036 JS |
5330 | static long btrfs_ioctl_quota_rescan(struct file *file, void __user *arg) |
5331 | { | |
0b246afa JM |
5332 | struct inode *inode = file_inode(file); |
5333 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2f232036 JS |
5334 | struct btrfs_ioctl_quota_rescan_args *qsa; |
5335 | int ret; | |
5336 | ||
5337 | if (!capable(CAP_SYS_ADMIN)) | |
5338 | return -EPERM; | |
5339 | ||
5340 | ret = mnt_want_write_file(file); | |
5341 | if (ret) | |
5342 | return ret; | |
5343 | ||
5344 | qsa = memdup_user(arg, sizeof(*qsa)); | |
5345 | if (IS_ERR(qsa)) { | |
5346 | ret = PTR_ERR(qsa); | |
5347 | goto drop_write; | |
5348 | } | |
5349 | ||
5350 | if (qsa->flags) { | |
5351 | ret = -EINVAL; | |
5352 | goto out; | |
5353 | } | |
5354 | ||
0b246afa | 5355 | ret = btrfs_qgroup_rescan(fs_info); |
2f232036 JS |
5356 | |
5357 | out: | |
5358 | kfree(qsa); | |
5359 | drop_write: | |
5360 | mnt_drop_write_file(file); | |
5361 | return ret; | |
5362 | } | |
5363 | ||
5364 | static long btrfs_ioctl_quota_rescan_status(struct file *file, void __user *arg) | |
5365 | { | |
0b246afa JM |
5366 | struct inode *inode = file_inode(file); |
5367 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
2f232036 JS |
5368 | struct btrfs_ioctl_quota_rescan_args *qsa; |
5369 | int ret = 0; | |
5370 | ||
5371 | if (!capable(CAP_SYS_ADMIN)) | |
5372 | return -EPERM; | |
5373 | ||
8d2db785 | 5374 | qsa = kzalloc(sizeof(*qsa), GFP_KERNEL); |
2f232036 JS |
5375 | if (!qsa) |
5376 | return -ENOMEM; | |
5377 | ||
0b246afa | 5378 | if (fs_info->qgroup_flags & BTRFS_QGROUP_STATUS_FLAG_RESCAN) { |
2f232036 | 5379 | qsa->flags = 1; |
0b246afa | 5380 | qsa->progress = fs_info->qgroup_rescan_progress.objectid; |
2f232036 JS |
5381 | } |
5382 | ||
5383 | if (copy_to_user(arg, qsa, sizeof(*qsa))) | |
5384 | ret = -EFAULT; | |
5385 | ||
5386 | kfree(qsa); | |
5387 | return ret; | |
5388 | } | |
5389 | ||
57254b6e JS |
5390 | static long btrfs_ioctl_quota_rescan_wait(struct file *file, void __user *arg) |
5391 | { | |
0b246afa JM |
5392 | struct inode *inode = file_inode(file); |
5393 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
57254b6e JS |
5394 | |
5395 | if (!capable(CAP_SYS_ADMIN)) | |
5396 | return -EPERM; | |
5397 | ||
0b246afa | 5398 | return btrfs_qgroup_wait_for_completion(fs_info, true); |
57254b6e JS |
5399 | } |
5400 | ||
abccd00f HM |
5401 | static long _btrfs_ioctl_set_received_subvol(struct file *file, |
5402 | struct btrfs_ioctl_received_subvol_args *sa) | |
8ea05e3a | 5403 | { |
496ad9aa | 5404 | struct inode *inode = file_inode(file); |
0b246afa | 5405 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
8ea05e3a AB |
5406 | struct btrfs_root *root = BTRFS_I(inode)->root; |
5407 | struct btrfs_root_item *root_item = &root->root_item; | |
5408 | struct btrfs_trans_handle *trans; | |
95582b00 | 5409 | struct timespec64 ct = current_time(inode); |
8ea05e3a | 5410 | int ret = 0; |
dd5f9615 | 5411 | int received_uuid_changed; |
8ea05e3a | 5412 | |
bd60ea0f DS |
5413 | if (!inode_owner_or_capable(inode)) |
5414 | return -EPERM; | |
5415 | ||
8ea05e3a AB |
5416 | ret = mnt_want_write_file(file); |
5417 | if (ret < 0) | |
5418 | return ret; | |
5419 | ||
0b246afa | 5420 | down_write(&fs_info->subvol_sem); |
8ea05e3a | 5421 | |
4a0cc7ca | 5422 | if (btrfs_ino(BTRFS_I(inode)) != BTRFS_FIRST_FREE_OBJECTID) { |
8ea05e3a AB |
5423 | ret = -EINVAL; |
5424 | goto out; | |
5425 | } | |
5426 | ||
5427 | if (btrfs_root_readonly(root)) { | |
5428 | ret = -EROFS; | |
5429 | goto out; | |
5430 | } | |
5431 | ||
dd5f9615 SB |
5432 | /* |
5433 | * 1 - root item | |
5434 | * 2 - uuid items (received uuid + subvol uuid) | |
5435 | */ | |
5436 | trans = btrfs_start_transaction(root, 3); | |
8ea05e3a AB |
5437 | if (IS_ERR(trans)) { |
5438 | ret = PTR_ERR(trans); | |
5439 | trans = NULL; | |
5440 | goto out; | |
5441 | } | |
5442 | ||
5443 | sa->rtransid = trans->transid; | |
5444 | sa->rtime.sec = ct.tv_sec; | |
5445 | sa->rtime.nsec = ct.tv_nsec; | |
5446 | ||
dd5f9615 SB |
5447 | received_uuid_changed = memcmp(root_item->received_uuid, sa->uuid, |
5448 | BTRFS_UUID_SIZE); | |
5449 | if (received_uuid_changed && | |
d87ff758 | 5450 | !btrfs_is_empty_uuid(root_item->received_uuid)) { |
d1957791 | 5451 | ret = btrfs_uuid_tree_remove(trans, root_item->received_uuid, |
d87ff758 NB |
5452 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
5453 | root->root_key.objectid); | |
5454 | if (ret && ret != -ENOENT) { | |
5455 | btrfs_abort_transaction(trans, ret); | |
5456 | btrfs_end_transaction(trans); | |
5457 | goto out; | |
5458 | } | |
5459 | } | |
8ea05e3a AB |
5460 | memcpy(root_item->received_uuid, sa->uuid, BTRFS_UUID_SIZE); |
5461 | btrfs_set_root_stransid(root_item, sa->stransid); | |
5462 | btrfs_set_root_rtransid(root_item, sa->rtransid); | |
3cae210f QW |
5463 | btrfs_set_stack_timespec_sec(&root_item->stime, sa->stime.sec); |
5464 | btrfs_set_stack_timespec_nsec(&root_item->stime, sa->stime.nsec); | |
5465 | btrfs_set_stack_timespec_sec(&root_item->rtime, sa->rtime.sec); | |
5466 | btrfs_set_stack_timespec_nsec(&root_item->rtime, sa->rtime.nsec); | |
8ea05e3a | 5467 | |
0b246afa | 5468 | ret = btrfs_update_root(trans, fs_info->tree_root, |
8ea05e3a AB |
5469 | &root->root_key, &root->root_item); |
5470 | if (ret < 0) { | |
3a45bb20 | 5471 | btrfs_end_transaction(trans); |
8ea05e3a | 5472 | goto out; |
dd5f9615 SB |
5473 | } |
5474 | if (received_uuid_changed && !btrfs_is_empty_uuid(sa->uuid)) { | |
cdb345a8 | 5475 | ret = btrfs_uuid_tree_add(trans, sa->uuid, |
dd5f9615 SB |
5476 | BTRFS_UUID_KEY_RECEIVED_SUBVOL, |
5477 | root->root_key.objectid); | |
5478 | if (ret < 0 && ret != -EEXIST) { | |
66642832 | 5479 | btrfs_abort_transaction(trans, ret); |
efd38150 | 5480 | btrfs_end_transaction(trans); |
8ea05e3a | 5481 | goto out; |
dd5f9615 SB |
5482 | } |
5483 | } | |
3a45bb20 | 5484 | ret = btrfs_commit_transaction(trans); |
abccd00f | 5485 | out: |
0b246afa | 5486 | up_write(&fs_info->subvol_sem); |
abccd00f HM |
5487 | mnt_drop_write_file(file); |
5488 | return ret; | |
5489 | } | |
5490 | ||
5491 | #ifdef CONFIG_64BIT | |
5492 | static long btrfs_ioctl_set_received_subvol_32(struct file *file, | |
5493 | void __user *arg) | |
5494 | { | |
5495 | struct btrfs_ioctl_received_subvol_args_32 *args32 = NULL; | |
5496 | struct btrfs_ioctl_received_subvol_args *args64 = NULL; | |
5497 | int ret = 0; | |
5498 | ||
5499 | args32 = memdup_user(arg, sizeof(*args32)); | |
7b9ea627 SV |
5500 | if (IS_ERR(args32)) |
5501 | return PTR_ERR(args32); | |
abccd00f | 5502 | |
8d2db785 | 5503 | args64 = kmalloc(sizeof(*args64), GFP_KERNEL); |
84dbeb87 DC |
5504 | if (!args64) { |
5505 | ret = -ENOMEM; | |
abccd00f HM |
5506 | goto out; |
5507 | } | |
5508 | ||
5509 | memcpy(args64->uuid, args32->uuid, BTRFS_UUID_SIZE); | |
5510 | args64->stransid = args32->stransid; | |
5511 | args64->rtransid = args32->rtransid; | |
5512 | args64->stime.sec = args32->stime.sec; | |
5513 | args64->stime.nsec = args32->stime.nsec; | |
5514 | args64->rtime.sec = args32->rtime.sec; | |
5515 | args64->rtime.nsec = args32->rtime.nsec; | |
5516 | args64->flags = args32->flags; | |
5517 | ||
5518 | ret = _btrfs_ioctl_set_received_subvol(file, args64); | |
5519 | if (ret) | |
5520 | goto out; | |
5521 | ||
5522 | memcpy(args32->uuid, args64->uuid, BTRFS_UUID_SIZE); | |
5523 | args32->stransid = args64->stransid; | |
5524 | args32->rtransid = args64->rtransid; | |
5525 | args32->stime.sec = args64->stime.sec; | |
5526 | args32->stime.nsec = args64->stime.nsec; | |
5527 | args32->rtime.sec = args64->rtime.sec; | |
5528 | args32->rtime.nsec = args64->rtime.nsec; | |
5529 | args32->flags = args64->flags; | |
5530 | ||
5531 | ret = copy_to_user(arg, args32, sizeof(*args32)); | |
5532 | if (ret) | |
5533 | ret = -EFAULT; | |
5534 | ||
5535 | out: | |
5536 | kfree(args32); | |
5537 | kfree(args64); | |
5538 | return ret; | |
5539 | } | |
5540 | #endif | |
5541 | ||
5542 | static long btrfs_ioctl_set_received_subvol(struct file *file, | |
5543 | void __user *arg) | |
5544 | { | |
5545 | struct btrfs_ioctl_received_subvol_args *sa = NULL; | |
5546 | int ret = 0; | |
5547 | ||
5548 | sa = memdup_user(arg, sizeof(*sa)); | |
7b9ea627 SV |
5549 | if (IS_ERR(sa)) |
5550 | return PTR_ERR(sa); | |
abccd00f HM |
5551 | |
5552 | ret = _btrfs_ioctl_set_received_subvol(file, sa); | |
5553 | ||
5554 | if (ret) | |
5555 | goto out; | |
5556 | ||
8ea05e3a AB |
5557 | ret = copy_to_user(arg, sa, sizeof(*sa)); |
5558 | if (ret) | |
5559 | ret = -EFAULT; | |
5560 | ||
5561 | out: | |
5562 | kfree(sa); | |
8ea05e3a AB |
5563 | return ret; |
5564 | } | |
5565 | ||
867ab667 | 5566 | static int btrfs_ioctl_get_fslabel(struct file *file, void __user *arg) |
5567 | { | |
0b246afa JM |
5568 | struct inode *inode = file_inode(file); |
5569 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
a1b83ac5 | 5570 | size_t len; |
867ab667 | 5571 | int ret; |
a1b83ac5 AJ |
5572 | char label[BTRFS_LABEL_SIZE]; |
5573 | ||
0b246afa JM |
5574 | spin_lock(&fs_info->super_lock); |
5575 | memcpy(label, fs_info->super_copy->label, BTRFS_LABEL_SIZE); | |
5576 | spin_unlock(&fs_info->super_lock); | |
a1b83ac5 AJ |
5577 | |
5578 | len = strnlen(label, BTRFS_LABEL_SIZE); | |
867ab667 | 5579 | |
5580 | if (len == BTRFS_LABEL_SIZE) { | |
0b246afa JM |
5581 | btrfs_warn(fs_info, |
5582 | "label is too long, return the first %zu bytes", | |
5583 | --len); | |
867ab667 | 5584 | } |
5585 | ||
867ab667 | 5586 | ret = copy_to_user(arg, label, len); |
867ab667 | 5587 | |
5588 | return ret ? -EFAULT : 0; | |
5589 | } | |
5590 | ||
a8bfd4ab | 5591 | static int btrfs_ioctl_set_fslabel(struct file *file, void __user *arg) |
5592 | { | |
0b246afa JM |
5593 | struct inode *inode = file_inode(file); |
5594 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5595 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5596 | struct btrfs_super_block *super_block = fs_info->super_copy; | |
a8bfd4ab | 5597 | struct btrfs_trans_handle *trans; |
5598 | char label[BTRFS_LABEL_SIZE]; | |
5599 | int ret; | |
5600 | ||
5601 | if (!capable(CAP_SYS_ADMIN)) | |
5602 | return -EPERM; | |
5603 | ||
5604 | if (copy_from_user(label, arg, sizeof(label))) | |
5605 | return -EFAULT; | |
5606 | ||
5607 | if (strnlen(label, BTRFS_LABEL_SIZE) == BTRFS_LABEL_SIZE) { | |
0b246afa | 5608 | btrfs_err(fs_info, |
5d163e0e JM |
5609 | "unable to set label with more than %d bytes", |
5610 | BTRFS_LABEL_SIZE - 1); | |
a8bfd4ab | 5611 | return -EINVAL; |
5612 | } | |
5613 | ||
5614 | ret = mnt_want_write_file(file); | |
5615 | if (ret) | |
5616 | return ret; | |
5617 | ||
a8bfd4ab | 5618 | trans = btrfs_start_transaction(root, 0); |
5619 | if (IS_ERR(trans)) { | |
5620 | ret = PTR_ERR(trans); | |
5621 | goto out_unlock; | |
5622 | } | |
5623 | ||
0b246afa | 5624 | spin_lock(&fs_info->super_lock); |
a8bfd4ab | 5625 | strcpy(super_block->label, label); |
0b246afa | 5626 | spin_unlock(&fs_info->super_lock); |
3a45bb20 | 5627 | ret = btrfs_commit_transaction(trans); |
a8bfd4ab | 5628 | |
5629 | out_unlock: | |
a8bfd4ab | 5630 | mnt_drop_write_file(file); |
5631 | return ret; | |
5632 | } | |
5633 | ||
2eaa055f JM |
5634 | #define INIT_FEATURE_FLAGS(suffix) \ |
5635 | { .compat_flags = BTRFS_FEATURE_COMPAT_##suffix, \ | |
5636 | .compat_ro_flags = BTRFS_FEATURE_COMPAT_RO_##suffix, \ | |
5637 | .incompat_flags = BTRFS_FEATURE_INCOMPAT_##suffix } | |
5638 | ||
d5131b65 | 5639 | int btrfs_ioctl_get_supported_features(void __user *arg) |
2eaa055f | 5640 | { |
4d4ab6d6 | 5641 | static const struct btrfs_ioctl_feature_flags features[3] = { |
2eaa055f JM |
5642 | INIT_FEATURE_FLAGS(SUPP), |
5643 | INIT_FEATURE_FLAGS(SAFE_SET), | |
5644 | INIT_FEATURE_FLAGS(SAFE_CLEAR) | |
5645 | }; | |
5646 | ||
5647 | if (copy_to_user(arg, &features, sizeof(features))) | |
5648 | return -EFAULT; | |
5649 | ||
5650 | return 0; | |
5651 | } | |
5652 | ||
5653 | static int btrfs_ioctl_get_features(struct file *file, void __user *arg) | |
5654 | { | |
0b246afa JM |
5655 | struct inode *inode = file_inode(file); |
5656 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5657 | struct btrfs_super_block *super_block = fs_info->super_copy; | |
2eaa055f JM |
5658 | struct btrfs_ioctl_feature_flags features; |
5659 | ||
5660 | features.compat_flags = btrfs_super_compat_flags(super_block); | |
5661 | features.compat_ro_flags = btrfs_super_compat_ro_flags(super_block); | |
5662 | features.incompat_flags = btrfs_super_incompat_flags(super_block); | |
5663 | ||
5664 | if (copy_to_user(arg, &features, sizeof(features))) | |
5665 | return -EFAULT; | |
5666 | ||
5667 | return 0; | |
5668 | } | |
5669 | ||
2ff7e61e | 5670 | static int check_feature_bits(struct btrfs_fs_info *fs_info, |
3b02a68a | 5671 | enum btrfs_feature_set set, |
2eaa055f JM |
5672 | u64 change_mask, u64 flags, u64 supported_flags, |
5673 | u64 safe_set, u64 safe_clear) | |
5674 | { | |
3b02a68a JM |
5675 | const char *type = btrfs_feature_set_names[set]; |
5676 | char *names; | |
2eaa055f JM |
5677 | u64 disallowed, unsupported; |
5678 | u64 set_mask = flags & change_mask; | |
5679 | u64 clear_mask = ~flags & change_mask; | |
5680 | ||
5681 | unsupported = set_mask & ~supported_flags; | |
5682 | if (unsupported) { | |
3b02a68a JM |
5683 | names = btrfs_printable_features(set, unsupported); |
5684 | if (names) { | |
0b246afa JM |
5685 | btrfs_warn(fs_info, |
5686 | "this kernel does not support the %s feature bit%s", | |
5687 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
5688 | kfree(names); |
5689 | } else | |
0b246afa JM |
5690 | btrfs_warn(fs_info, |
5691 | "this kernel does not support %s bits 0x%llx", | |
5692 | type, unsupported); | |
2eaa055f JM |
5693 | return -EOPNOTSUPP; |
5694 | } | |
5695 | ||
5696 | disallowed = set_mask & ~safe_set; | |
5697 | if (disallowed) { | |
3b02a68a JM |
5698 | names = btrfs_printable_features(set, disallowed); |
5699 | if (names) { | |
0b246afa JM |
5700 | btrfs_warn(fs_info, |
5701 | "can't set the %s feature bit%s while mounted", | |
5702 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
5703 | kfree(names); |
5704 | } else | |
0b246afa JM |
5705 | btrfs_warn(fs_info, |
5706 | "can't set %s bits 0x%llx while mounted", | |
5707 | type, disallowed); | |
2eaa055f JM |
5708 | return -EPERM; |
5709 | } | |
5710 | ||
5711 | disallowed = clear_mask & ~safe_clear; | |
5712 | if (disallowed) { | |
3b02a68a JM |
5713 | names = btrfs_printable_features(set, disallowed); |
5714 | if (names) { | |
0b246afa JM |
5715 | btrfs_warn(fs_info, |
5716 | "can't clear the %s feature bit%s while mounted", | |
5717 | names, strchr(names, ',') ? "s" : ""); | |
3b02a68a JM |
5718 | kfree(names); |
5719 | } else | |
0b246afa JM |
5720 | btrfs_warn(fs_info, |
5721 | "can't clear %s bits 0x%llx while mounted", | |
5722 | type, disallowed); | |
2eaa055f JM |
5723 | return -EPERM; |
5724 | } | |
5725 | ||
5726 | return 0; | |
5727 | } | |
5728 | ||
2ff7e61e JM |
5729 | #define check_feature(fs_info, change_mask, flags, mask_base) \ |
5730 | check_feature_bits(fs_info, FEAT_##mask_base, change_mask, flags, \ | |
2eaa055f JM |
5731 | BTRFS_FEATURE_ ## mask_base ## _SUPP, \ |
5732 | BTRFS_FEATURE_ ## mask_base ## _SAFE_SET, \ | |
5733 | BTRFS_FEATURE_ ## mask_base ## _SAFE_CLEAR) | |
5734 | ||
5735 | static int btrfs_ioctl_set_features(struct file *file, void __user *arg) | |
5736 | { | |
0b246afa JM |
5737 | struct inode *inode = file_inode(file); |
5738 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5739 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
5740 | struct btrfs_super_block *super_block = fs_info->super_copy; | |
2eaa055f JM |
5741 | struct btrfs_ioctl_feature_flags flags[2]; |
5742 | struct btrfs_trans_handle *trans; | |
5743 | u64 newflags; | |
5744 | int ret; | |
5745 | ||
5746 | if (!capable(CAP_SYS_ADMIN)) | |
5747 | return -EPERM; | |
5748 | ||
5749 | if (copy_from_user(flags, arg, sizeof(flags))) | |
5750 | return -EFAULT; | |
5751 | ||
5752 | /* Nothing to do */ | |
5753 | if (!flags[0].compat_flags && !flags[0].compat_ro_flags && | |
5754 | !flags[0].incompat_flags) | |
5755 | return 0; | |
5756 | ||
2ff7e61e | 5757 | ret = check_feature(fs_info, flags[0].compat_flags, |
2eaa055f JM |
5758 | flags[1].compat_flags, COMPAT); |
5759 | if (ret) | |
5760 | return ret; | |
5761 | ||
2ff7e61e | 5762 | ret = check_feature(fs_info, flags[0].compat_ro_flags, |
2eaa055f JM |
5763 | flags[1].compat_ro_flags, COMPAT_RO); |
5764 | if (ret) | |
5765 | return ret; | |
5766 | ||
2ff7e61e | 5767 | ret = check_feature(fs_info, flags[0].incompat_flags, |
2eaa055f JM |
5768 | flags[1].incompat_flags, INCOMPAT); |
5769 | if (ret) | |
5770 | return ret; | |
5771 | ||
7ab19625 DS |
5772 | ret = mnt_want_write_file(file); |
5773 | if (ret) | |
5774 | return ret; | |
5775 | ||
8051aa1a | 5776 | trans = btrfs_start_transaction(root, 0); |
7ab19625 DS |
5777 | if (IS_ERR(trans)) { |
5778 | ret = PTR_ERR(trans); | |
5779 | goto out_drop_write; | |
5780 | } | |
2eaa055f | 5781 | |
0b246afa | 5782 | spin_lock(&fs_info->super_lock); |
2eaa055f JM |
5783 | newflags = btrfs_super_compat_flags(super_block); |
5784 | newflags |= flags[0].compat_flags & flags[1].compat_flags; | |
5785 | newflags &= ~(flags[0].compat_flags & ~flags[1].compat_flags); | |
5786 | btrfs_set_super_compat_flags(super_block, newflags); | |
5787 | ||
5788 | newflags = btrfs_super_compat_ro_flags(super_block); | |
5789 | newflags |= flags[0].compat_ro_flags & flags[1].compat_ro_flags; | |
5790 | newflags &= ~(flags[0].compat_ro_flags & ~flags[1].compat_ro_flags); | |
5791 | btrfs_set_super_compat_ro_flags(super_block, newflags); | |
5792 | ||
5793 | newflags = btrfs_super_incompat_flags(super_block); | |
5794 | newflags |= flags[0].incompat_flags & flags[1].incompat_flags; | |
5795 | newflags &= ~(flags[0].incompat_flags & ~flags[1].incompat_flags); | |
5796 | btrfs_set_super_incompat_flags(super_block, newflags); | |
0b246afa | 5797 | spin_unlock(&fs_info->super_lock); |
2eaa055f | 5798 | |
3a45bb20 | 5799 | ret = btrfs_commit_transaction(trans); |
7ab19625 DS |
5800 | out_drop_write: |
5801 | mnt_drop_write_file(file); | |
5802 | ||
5803 | return ret; | |
2eaa055f JM |
5804 | } |
5805 | ||
2351f431 JB |
5806 | static int _btrfs_ioctl_send(struct file *file, void __user *argp, bool compat) |
5807 | { | |
5808 | struct btrfs_ioctl_send_args *arg; | |
5809 | int ret; | |
5810 | ||
5811 | if (compat) { | |
5812 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) | |
5813 | struct btrfs_ioctl_send_args_32 args32; | |
5814 | ||
5815 | ret = copy_from_user(&args32, argp, sizeof(args32)); | |
5816 | if (ret) | |
5817 | return -EFAULT; | |
5818 | arg = kzalloc(sizeof(*arg), GFP_KERNEL); | |
5819 | if (!arg) | |
5820 | return -ENOMEM; | |
5821 | arg->send_fd = args32.send_fd; | |
5822 | arg->clone_sources_count = args32.clone_sources_count; | |
5823 | arg->clone_sources = compat_ptr(args32.clone_sources); | |
5824 | arg->parent_root = args32.parent_root; | |
5825 | arg->flags = args32.flags; | |
5826 | memcpy(arg->reserved, args32.reserved, | |
5827 | sizeof(args32.reserved)); | |
5828 | #else | |
5829 | return -ENOTTY; | |
5830 | #endif | |
5831 | } else { | |
5832 | arg = memdup_user(argp, sizeof(*arg)); | |
5833 | if (IS_ERR(arg)) | |
5834 | return PTR_ERR(arg); | |
5835 | } | |
5836 | ret = btrfs_ioctl_send(file, arg); | |
5837 | kfree(arg); | |
5838 | return ret; | |
5839 | } | |
5840 | ||
f46b5a66 CH |
5841 | long btrfs_ioctl(struct file *file, unsigned int |
5842 | cmd, unsigned long arg) | |
5843 | { | |
0b246afa JM |
5844 | struct inode *inode = file_inode(file); |
5845 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); | |
5846 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4bcabaa3 | 5847 | void __user *argp = (void __user *)arg; |
f46b5a66 CH |
5848 | |
5849 | switch (cmd) { | |
6cbff00f CH |
5850 | case FS_IOC_GETFLAGS: |
5851 | return btrfs_ioctl_getflags(file, argp); | |
5852 | case FS_IOC_SETFLAGS: | |
5853 | return btrfs_ioctl_setflags(file, argp); | |
5854 | case FS_IOC_GETVERSION: | |
5855 | return btrfs_ioctl_getversion(file, argp); | |
f7039b1d LD |
5856 | case FITRIM: |
5857 | return btrfs_ioctl_fitrim(file, argp); | |
f46b5a66 | 5858 | case BTRFS_IOC_SNAP_CREATE: |
fa0d2b9b | 5859 | return btrfs_ioctl_snap_create(file, argp, 0); |
fdfb1e4f | 5860 | case BTRFS_IOC_SNAP_CREATE_V2: |
fa0d2b9b | 5861 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
3de4586c | 5862 | case BTRFS_IOC_SUBVOL_CREATE: |
fa0d2b9b | 5863 | return btrfs_ioctl_snap_create(file, argp, 1); |
6f72c7e2 AJ |
5864 | case BTRFS_IOC_SUBVOL_CREATE_V2: |
5865 | return btrfs_ioctl_snap_create_v2(file, argp, 1); | |
76dda93c YZ |
5866 | case BTRFS_IOC_SNAP_DESTROY: |
5867 | return btrfs_ioctl_snap_destroy(file, argp); | |
0caa102d LZ |
5868 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
5869 | return btrfs_ioctl_subvol_getflags(file, argp); | |
5870 | case BTRFS_IOC_SUBVOL_SETFLAGS: | |
5871 | return btrfs_ioctl_subvol_setflags(file, argp); | |
6ef5ed0d JB |
5872 | case BTRFS_IOC_DEFAULT_SUBVOL: |
5873 | return btrfs_ioctl_default_subvol(file, argp); | |
f46b5a66 | 5874 | case BTRFS_IOC_DEFRAG: |
1e701a32 CM |
5875 | return btrfs_ioctl_defrag(file, NULL); |
5876 | case BTRFS_IOC_DEFRAG_RANGE: | |
5877 | return btrfs_ioctl_defrag(file, argp); | |
f46b5a66 | 5878 | case BTRFS_IOC_RESIZE: |
198605a8 | 5879 | return btrfs_ioctl_resize(file, argp); |
f46b5a66 | 5880 | case BTRFS_IOC_ADD_DEV: |
2ff7e61e | 5881 | return btrfs_ioctl_add_dev(fs_info, argp); |
f46b5a66 | 5882 | case BTRFS_IOC_RM_DEV: |
da24927b | 5883 | return btrfs_ioctl_rm_dev(file, argp); |
6b526ed7 AJ |
5884 | case BTRFS_IOC_RM_DEV_V2: |
5885 | return btrfs_ioctl_rm_dev_v2(file, argp); | |
475f6387 | 5886 | case BTRFS_IOC_FS_INFO: |
2ff7e61e | 5887 | return btrfs_ioctl_fs_info(fs_info, argp); |
475f6387 | 5888 | case BTRFS_IOC_DEV_INFO: |
2ff7e61e | 5889 | return btrfs_ioctl_dev_info(fs_info, argp); |
f46b5a66 | 5890 | case BTRFS_IOC_BALANCE: |
9ba1f6e4 | 5891 | return btrfs_ioctl_balance(file, NULL); |
ac8e9819 CM |
5892 | case BTRFS_IOC_TREE_SEARCH: |
5893 | return btrfs_ioctl_tree_search(file, argp); | |
cc68a8a5 GH |
5894 | case BTRFS_IOC_TREE_SEARCH_V2: |
5895 | return btrfs_ioctl_tree_search_v2(file, argp); | |
ac8e9819 CM |
5896 | case BTRFS_IOC_INO_LOOKUP: |
5897 | return btrfs_ioctl_ino_lookup(file, argp); | |
d7728c96 JS |
5898 | case BTRFS_IOC_INO_PATHS: |
5899 | return btrfs_ioctl_ino_to_path(root, argp); | |
5900 | case BTRFS_IOC_LOGICAL_INO: | |
d24a67b2 ZB |
5901 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 1); |
5902 | case BTRFS_IOC_LOGICAL_INO_V2: | |
5903 | return btrfs_ioctl_logical_to_ino(fs_info, argp, 2); | |
1406e432 | 5904 | case BTRFS_IOC_SPACE_INFO: |
2ff7e61e | 5905 | return btrfs_ioctl_space_info(fs_info, argp); |
9b199859 FDBM |
5906 | case BTRFS_IOC_SYNC: { |
5907 | int ret; | |
5908 | ||
82b3e53b | 5909 | ret = btrfs_start_delalloc_roots(fs_info, -1); |
9b199859 FDBM |
5910 | if (ret) |
5911 | return ret; | |
0b246afa | 5912 | ret = btrfs_sync_fs(inode->i_sb, 1); |
2fad4e83 DS |
5913 | /* |
5914 | * The transaction thread may want to do more work, | |
01327610 | 5915 | * namely it pokes the cleaner kthread that will start |
2fad4e83 DS |
5916 | * processing uncleaned subvols. |
5917 | */ | |
0b246afa | 5918 | wake_up_process(fs_info->transaction_kthread); |
9b199859 FDBM |
5919 | return ret; |
5920 | } | |
46204592 | 5921 | case BTRFS_IOC_START_SYNC: |
9a8c28be | 5922 | return btrfs_ioctl_start_sync(root, argp); |
46204592 | 5923 | case BTRFS_IOC_WAIT_SYNC: |
2ff7e61e | 5924 | return btrfs_ioctl_wait_sync(fs_info, argp); |
475f6387 | 5925 | case BTRFS_IOC_SCRUB: |
b8e95489 | 5926 | return btrfs_ioctl_scrub(file, argp); |
475f6387 | 5927 | case BTRFS_IOC_SCRUB_CANCEL: |
2ff7e61e | 5928 | return btrfs_ioctl_scrub_cancel(fs_info); |
475f6387 | 5929 | case BTRFS_IOC_SCRUB_PROGRESS: |
2ff7e61e | 5930 | return btrfs_ioctl_scrub_progress(fs_info, argp); |
c9e9f97b | 5931 | case BTRFS_IOC_BALANCE_V2: |
9ba1f6e4 | 5932 | return btrfs_ioctl_balance(file, argp); |
837d5b6e | 5933 | case BTRFS_IOC_BALANCE_CTL: |
2ff7e61e | 5934 | return btrfs_ioctl_balance_ctl(fs_info, arg); |
19a39dce | 5935 | case BTRFS_IOC_BALANCE_PROGRESS: |
2ff7e61e | 5936 | return btrfs_ioctl_balance_progress(fs_info, argp); |
8ea05e3a AB |
5937 | case BTRFS_IOC_SET_RECEIVED_SUBVOL: |
5938 | return btrfs_ioctl_set_received_subvol(file, argp); | |
abccd00f HM |
5939 | #ifdef CONFIG_64BIT |
5940 | case BTRFS_IOC_SET_RECEIVED_SUBVOL_32: | |
5941 | return btrfs_ioctl_set_received_subvol_32(file, argp); | |
5942 | #endif | |
31db9f7c | 5943 | case BTRFS_IOC_SEND: |
2351f431 JB |
5944 | return _btrfs_ioctl_send(file, argp, false); |
5945 | #if defined(CONFIG_64BIT) && defined(CONFIG_COMPAT) | |
5946 | case BTRFS_IOC_SEND_32: | |
5947 | return _btrfs_ioctl_send(file, argp, true); | |
5948 | #endif | |
c11d2c23 | 5949 | case BTRFS_IOC_GET_DEV_STATS: |
2ff7e61e | 5950 | return btrfs_ioctl_get_dev_stats(fs_info, argp); |
5d13a37b | 5951 | case BTRFS_IOC_QUOTA_CTL: |
905b0dda | 5952 | return btrfs_ioctl_quota_ctl(file, argp); |
5d13a37b | 5953 | case BTRFS_IOC_QGROUP_ASSIGN: |
905b0dda | 5954 | return btrfs_ioctl_qgroup_assign(file, argp); |
5d13a37b | 5955 | case BTRFS_IOC_QGROUP_CREATE: |
905b0dda | 5956 | return btrfs_ioctl_qgroup_create(file, argp); |
5d13a37b | 5957 | case BTRFS_IOC_QGROUP_LIMIT: |
905b0dda | 5958 | return btrfs_ioctl_qgroup_limit(file, argp); |
2f232036 JS |
5959 | case BTRFS_IOC_QUOTA_RESCAN: |
5960 | return btrfs_ioctl_quota_rescan(file, argp); | |
5961 | case BTRFS_IOC_QUOTA_RESCAN_STATUS: | |
5962 | return btrfs_ioctl_quota_rescan_status(file, argp); | |
57254b6e JS |
5963 | case BTRFS_IOC_QUOTA_RESCAN_WAIT: |
5964 | return btrfs_ioctl_quota_rescan_wait(file, argp); | |
3f6bcfbd | 5965 | case BTRFS_IOC_DEV_REPLACE: |
2ff7e61e | 5966 | return btrfs_ioctl_dev_replace(fs_info, argp); |
867ab667 | 5967 | case BTRFS_IOC_GET_FSLABEL: |
5968 | return btrfs_ioctl_get_fslabel(file, argp); | |
a8bfd4ab | 5969 | case BTRFS_IOC_SET_FSLABEL: |
5970 | return btrfs_ioctl_set_fslabel(file, argp); | |
2eaa055f | 5971 | case BTRFS_IOC_GET_SUPPORTED_FEATURES: |
d5131b65 | 5972 | return btrfs_ioctl_get_supported_features(argp); |
2eaa055f JM |
5973 | case BTRFS_IOC_GET_FEATURES: |
5974 | return btrfs_ioctl_get_features(file, argp); | |
5975 | case BTRFS_IOC_SET_FEATURES: | |
5976 | return btrfs_ioctl_set_features(file, argp); | |
e4202ac9 DS |
5977 | case FS_IOC_FSGETXATTR: |
5978 | return btrfs_ioctl_fsgetxattr(file, argp); | |
025f2121 DS |
5979 | case FS_IOC_FSSETXATTR: |
5980 | return btrfs_ioctl_fssetxattr(file, argp); | |
b64ec075 TM |
5981 | case BTRFS_IOC_GET_SUBVOL_INFO: |
5982 | return btrfs_ioctl_get_subvol_info(file, argp); | |
42e4b520 TM |
5983 | case BTRFS_IOC_GET_SUBVOL_ROOTREF: |
5984 | return btrfs_ioctl_get_subvol_rootref(file, argp); | |
23d0b79d TM |
5985 | case BTRFS_IOC_INO_LOOKUP_USER: |
5986 | return btrfs_ioctl_ino_lookup_user(file, argp); | |
f46b5a66 CH |
5987 | } |
5988 | ||
5989 | return -ENOTTY; | |
5990 | } | |
4c63c245 LD |
5991 | |
5992 | #ifdef CONFIG_COMPAT | |
5993 | long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
5994 | { | |
2a362249 JM |
5995 | /* |
5996 | * These all access 32-bit values anyway so no further | |
5997 | * handling is necessary. | |
5998 | */ | |
4c63c245 LD |
5999 | switch (cmd) { |
6000 | case FS_IOC32_GETFLAGS: | |
6001 | cmd = FS_IOC_GETFLAGS; | |
6002 | break; | |
6003 | case FS_IOC32_SETFLAGS: | |
6004 | cmd = FS_IOC_SETFLAGS; | |
6005 | break; | |
6006 | case FS_IOC32_GETVERSION: | |
6007 | cmd = FS_IOC_GETVERSION; | |
6008 | break; | |
4c63c245 LD |
6009 | } |
6010 | ||
6011 | return btrfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg)); | |
6012 | } | |
6013 | #endif |