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