]>
Commit | Line | Data |
---|---|---|
f46b5a66 CH |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
18 | ||
19 | #include <linux/kernel.h> | |
20 | #include <linux/bio.h> | |
21 | #include <linux/buffer_head.h> | |
22 | #include <linux/file.h> | |
23 | #include <linux/fs.h> | |
cb8e7090 | 24 | #include <linux/fsnotify.h> |
f46b5a66 CH |
25 | #include <linux/pagemap.h> |
26 | #include <linux/highmem.h> | |
27 | #include <linux/time.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/string.h> | |
f46b5a66 | 30 | #include <linux/backing-dev.h> |
cb8e7090 | 31 | #include <linux/mount.h> |
f46b5a66 | 32 | #include <linux/mpage.h> |
cb8e7090 | 33 | #include <linux/namei.h> |
f46b5a66 CH |
34 | #include <linux/swap.h> |
35 | #include <linux/writeback.h> | |
36 | #include <linux/statfs.h> | |
37 | #include <linux/compat.h> | |
38 | #include <linux/bit_spinlock.h> | |
cb8e7090 | 39 | #include <linux/security.h> |
f46b5a66 | 40 | #include <linux/xattr.h> |
7ea394f1 | 41 | #include <linux/vmalloc.h> |
5a0e3ad6 | 42 | #include <linux/slab.h> |
f7039b1d | 43 | #include <linux/blkdev.h> |
4b4e25f2 | 44 | #include "compat.h" |
f46b5a66 CH |
45 | #include "ctree.h" |
46 | #include "disk-io.h" | |
47 | #include "transaction.h" | |
48 | #include "btrfs_inode.h" | |
49 | #include "ioctl.h" | |
50 | #include "print-tree.h" | |
51 | #include "volumes.h" | |
925baedd | 52 | #include "locking.h" |
581bb050 | 53 | #include "inode-map.h" |
d7728c96 | 54 | #include "backref.h" |
f46b5a66 | 55 | |
6cbff00f CH |
56 | /* Mask out flags that are inappropriate for the given type of inode. */ |
57 | static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) | |
58 | { | |
59 | if (S_ISDIR(mode)) | |
60 | return flags; | |
61 | else if (S_ISREG(mode)) | |
62 | return flags & ~FS_DIRSYNC_FL; | |
63 | else | |
64 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); | |
65 | } | |
66 | ||
67 | /* | |
68 | * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. | |
69 | */ | |
70 | static unsigned int btrfs_flags_to_ioctl(unsigned int flags) | |
71 | { | |
72 | unsigned int iflags = 0; | |
73 | ||
74 | if (flags & BTRFS_INODE_SYNC) | |
75 | iflags |= FS_SYNC_FL; | |
76 | if (flags & BTRFS_INODE_IMMUTABLE) | |
77 | iflags |= FS_IMMUTABLE_FL; | |
78 | if (flags & BTRFS_INODE_APPEND) | |
79 | iflags |= FS_APPEND_FL; | |
80 | if (flags & BTRFS_INODE_NODUMP) | |
81 | iflags |= FS_NODUMP_FL; | |
82 | if (flags & BTRFS_INODE_NOATIME) | |
83 | iflags |= FS_NOATIME_FL; | |
84 | if (flags & BTRFS_INODE_DIRSYNC) | |
85 | iflags |= FS_DIRSYNC_FL; | |
d0092bdd LZ |
86 | if (flags & BTRFS_INODE_NODATACOW) |
87 | iflags |= FS_NOCOW_FL; | |
88 | ||
89 | if ((flags & BTRFS_INODE_COMPRESS) && !(flags & BTRFS_INODE_NOCOMPRESS)) | |
90 | iflags |= FS_COMPR_FL; | |
91 | else if (flags & BTRFS_INODE_NOCOMPRESS) | |
92 | iflags |= FS_NOCOMP_FL; | |
6cbff00f CH |
93 | |
94 | return iflags; | |
95 | } | |
96 | ||
97 | /* | |
98 | * Update inode->i_flags based on the btrfs internal flags. | |
99 | */ | |
100 | void btrfs_update_iflags(struct inode *inode) | |
101 | { | |
102 | struct btrfs_inode *ip = BTRFS_I(inode); | |
103 | ||
104 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); | |
105 | ||
106 | if (ip->flags & BTRFS_INODE_SYNC) | |
107 | inode->i_flags |= S_SYNC; | |
108 | if (ip->flags & BTRFS_INODE_IMMUTABLE) | |
109 | inode->i_flags |= S_IMMUTABLE; | |
110 | if (ip->flags & BTRFS_INODE_APPEND) | |
111 | inode->i_flags |= S_APPEND; | |
112 | if (ip->flags & BTRFS_INODE_NOATIME) | |
113 | inode->i_flags |= S_NOATIME; | |
114 | if (ip->flags & BTRFS_INODE_DIRSYNC) | |
115 | inode->i_flags |= S_DIRSYNC; | |
116 | } | |
117 | ||
118 | /* | |
119 | * Inherit flags from the parent inode. | |
120 | * | |
e27425d6 | 121 | * Currently only the compression flags and the cow flags are inherited. |
6cbff00f CH |
122 | */ |
123 | void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
124 | { | |
0b4dcea5 CM |
125 | unsigned int flags; |
126 | ||
127 | if (!dir) | |
128 | return; | |
129 | ||
130 | flags = BTRFS_I(dir)->flags; | |
6cbff00f | 131 | |
e27425d6 JB |
132 | if (flags & BTRFS_INODE_NOCOMPRESS) { |
133 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS; | |
134 | BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS; | |
135 | } else if (flags & BTRFS_INODE_COMPRESS) { | |
136 | BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
137 | BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS; | |
138 | } | |
139 | ||
140 | if (flags & BTRFS_INODE_NODATACOW) | |
141 | BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW; | |
6cbff00f | 142 | |
6cbff00f CH |
143 | btrfs_update_iflags(inode); |
144 | } | |
145 | ||
146 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) | |
147 | { | |
148 | struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode); | |
149 | unsigned int flags = btrfs_flags_to_ioctl(ip->flags); | |
150 | ||
151 | if (copy_to_user(arg, &flags, sizeof(flags))) | |
152 | return -EFAULT; | |
153 | return 0; | |
154 | } | |
155 | ||
75e7cb7f LB |
156 | static int check_flags(unsigned int flags) |
157 | { | |
158 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ | |
159 | FS_NOATIME_FL | FS_NODUMP_FL | \ | |
160 | FS_SYNC_FL | FS_DIRSYNC_FL | \ | |
e1e8fb6a LZ |
161 | FS_NOCOMP_FL | FS_COMPR_FL | |
162 | FS_NOCOW_FL)) | |
75e7cb7f LB |
163 | return -EOPNOTSUPP; |
164 | ||
165 | if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL)) | |
166 | return -EINVAL; | |
167 | ||
75e7cb7f LB |
168 | return 0; |
169 | } | |
170 | ||
6cbff00f CH |
171 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) |
172 | { | |
173 | struct inode *inode = file->f_path.dentry->d_inode; | |
174 | struct btrfs_inode *ip = BTRFS_I(inode); | |
175 | struct btrfs_root *root = ip->root; | |
176 | struct btrfs_trans_handle *trans; | |
177 | unsigned int flags, oldflags; | |
178 | int ret; | |
f062abf0 LZ |
179 | u64 ip_oldflags; |
180 | unsigned int i_oldflags; | |
6cbff00f | 181 | |
b83cc969 LZ |
182 | if (btrfs_root_readonly(root)) |
183 | return -EROFS; | |
184 | ||
6cbff00f CH |
185 | if (copy_from_user(&flags, arg, sizeof(flags))) |
186 | return -EFAULT; | |
187 | ||
75e7cb7f LB |
188 | ret = check_flags(flags); |
189 | if (ret) | |
190 | return ret; | |
f46b5a66 | 191 | |
2e149670 | 192 | if (!inode_owner_or_capable(inode)) |
6cbff00f CH |
193 | return -EACCES; |
194 | ||
195 | mutex_lock(&inode->i_mutex); | |
196 | ||
f062abf0 LZ |
197 | ip_oldflags = ip->flags; |
198 | i_oldflags = inode->i_flags; | |
199 | ||
6cbff00f CH |
200 | flags = btrfs_mask_flags(inode->i_mode, flags); |
201 | oldflags = btrfs_flags_to_ioctl(ip->flags); | |
202 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { | |
203 | if (!capable(CAP_LINUX_IMMUTABLE)) { | |
204 | ret = -EPERM; | |
205 | goto out_unlock; | |
206 | } | |
207 | } | |
208 | ||
a561be71 | 209 | ret = mnt_want_write_file(file); |
6cbff00f CH |
210 | if (ret) |
211 | goto out_unlock; | |
212 | ||
213 | if (flags & FS_SYNC_FL) | |
214 | ip->flags |= BTRFS_INODE_SYNC; | |
215 | else | |
216 | ip->flags &= ~BTRFS_INODE_SYNC; | |
217 | if (flags & FS_IMMUTABLE_FL) | |
218 | ip->flags |= BTRFS_INODE_IMMUTABLE; | |
219 | else | |
220 | ip->flags &= ~BTRFS_INODE_IMMUTABLE; | |
221 | if (flags & FS_APPEND_FL) | |
222 | ip->flags |= BTRFS_INODE_APPEND; | |
223 | else | |
224 | ip->flags &= ~BTRFS_INODE_APPEND; | |
225 | if (flags & FS_NODUMP_FL) | |
226 | ip->flags |= BTRFS_INODE_NODUMP; | |
227 | else | |
228 | ip->flags &= ~BTRFS_INODE_NODUMP; | |
229 | if (flags & FS_NOATIME_FL) | |
230 | ip->flags |= BTRFS_INODE_NOATIME; | |
231 | else | |
232 | ip->flags &= ~BTRFS_INODE_NOATIME; | |
233 | if (flags & FS_DIRSYNC_FL) | |
234 | ip->flags |= BTRFS_INODE_DIRSYNC; | |
235 | else | |
236 | ip->flags &= ~BTRFS_INODE_DIRSYNC; | |
e1e8fb6a LZ |
237 | if (flags & FS_NOCOW_FL) |
238 | ip->flags |= BTRFS_INODE_NODATACOW; | |
239 | else | |
240 | ip->flags &= ~BTRFS_INODE_NODATACOW; | |
6cbff00f | 241 | |
75e7cb7f LB |
242 | /* |
243 | * The COMPRESS flag can only be changed by users, while the NOCOMPRESS | |
244 | * flag may be changed automatically if compression code won't make | |
245 | * things smaller. | |
246 | */ | |
247 | if (flags & FS_NOCOMP_FL) { | |
248 | ip->flags &= ~BTRFS_INODE_COMPRESS; | |
249 | ip->flags |= BTRFS_INODE_NOCOMPRESS; | |
250 | } else if (flags & FS_COMPR_FL) { | |
251 | ip->flags |= BTRFS_INODE_COMPRESS; | |
252 | ip->flags &= ~BTRFS_INODE_NOCOMPRESS; | |
ebcb904d LZ |
253 | } else { |
254 | ip->flags &= ~(BTRFS_INODE_COMPRESS | BTRFS_INODE_NOCOMPRESS); | |
75e7cb7f | 255 | } |
6cbff00f | 256 | |
4da6f1a3 | 257 | trans = btrfs_start_transaction(root, 1); |
f062abf0 LZ |
258 | if (IS_ERR(trans)) { |
259 | ret = PTR_ERR(trans); | |
260 | goto out_drop; | |
261 | } | |
6cbff00f | 262 | |
306424cc | 263 | btrfs_update_iflags(inode); |
0c4d2d95 | 264 | inode_inc_iversion(inode); |
306424cc | 265 | inode->i_ctime = CURRENT_TIME; |
6cbff00f | 266 | ret = btrfs_update_inode(trans, root, inode); |
6cbff00f | 267 | |
6cbff00f | 268 | btrfs_end_transaction(trans, root); |
f062abf0 LZ |
269 | out_drop: |
270 | if (ret) { | |
271 | ip->flags = ip_oldflags; | |
272 | inode->i_flags = i_oldflags; | |
273 | } | |
6cbff00f | 274 | |
2a79f17e | 275 | mnt_drop_write_file(file); |
6cbff00f CH |
276 | out_unlock: |
277 | mutex_unlock(&inode->i_mutex); | |
2d4e6f6a | 278 | return ret; |
6cbff00f CH |
279 | } |
280 | ||
281 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) | |
282 | { | |
283 | struct inode *inode = file->f_path.dentry->d_inode; | |
284 | ||
285 | return put_user(inode->i_generation, arg); | |
286 | } | |
f46b5a66 | 287 | |
f7039b1d LD |
288 | static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg) |
289 | { | |
815745cf | 290 | struct btrfs_fs_info *fs_info = btrfs_sb(fdentry(file)->d_sb); |
f7039b1d LD |
291 | struct btrfs_device *device; |
292 | struct request_queue *q; | |
293 | struct fstrim_range range; | |
294 | u64 minlen = ULLONG_MAX; | |
295 | u64 num_devices = 0; | |
815745cf | 296 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
f7039b1d LD |
297 | int ret; |
298 | ||
299 | if (!capable(CAP_SYS_ADMIN)) | |
300 | return -EPERM; | |
301 | ||
1f78160c XG |
302 | rcu_read_lock(); |
303 | list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, | |
304 | dev_list) { | |
f7039b1d LD |
305 | if (!device->bdev) |
306 | continue; | |
307 | q = bdev_get_queue(device->bdev); | |
308 | if (blk_queue_discard(q)) { | |
309 | num_devices++; | |
310 | minlen = min((u64)q->limits.discard_granularity, | |
311 | minlen); | |
312 | } | |
313 | } | |
1f78160c | 314 | rcu_read_unlock(); |
f4c697e6 | 315 | |
f7039b1d LD |
316 | if (!num_devices) |
317 | return -EOPNOTSUPP; | |
f7039b1d LD |
318 | if (copy_from_user(&range, arg, sizeof(range))) |
319 | return -EFAULT; | |
f4c697e6 LC |
320 | if (range.start > total_bytes) |
321 | return -EINVAL; | |
f7039b1d | 322 | |
f4c697e6 | 323 | range.len = min(range.len, total_bytes - range.start); |
f7039b1d | 324 | range.minlen = max(range.minlen, minlen); |
815745cf | 325 | ret = btrfs_trim_fs(fs_info->tree_root, &range); |
f7039b1d LD |
326 | if (ret < 0) |
327 | return ret; | |
328 | ||
329 | if (copy_to_user(arg, &range, sizeof(range))) | |
330 | return -EFAULT; | |
331 | ||
332 | return 0; | |
333 | } | |
334 | ||
cb8e7090 CH |
335 | static noinline int create_subvol(struct btrfs_root *root, |
336 | struct dentry *dentry, | |
72fd032e SW |
337 | char *name, int namelen, |
338 | u64 *async_transid) | |
f46b5a66 CH |
339 | { |
340 | struct btrfs_trans_handle *trans; | |
341 | struct btrfs_key key; | |
342 | struct btrfs_root_item root_item; | |
343 | struct btrfs_inode_item *inode_item; | |
344 | struct extent_buffer *leaf; | |
76dda93c | 345 | struct btrfs_root *new_root; |
2fbe8c8a | 346 | struct dentry *parent = dentry->d_parent; |
6a912213 | 347 | struct inode *dir; |
f46b5a66 CH |
348 | int ret; |
349 | int err; | |
350 | u64 objectid; | |
351 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | |
3de4586c | 352 | u64 index = 0; |
f46b5a66 | 353 | |
581bb050 | 354 | ret = btrfs_find_free_objectid(root->fs_info->tree_root, &objectid); |
2fbe8c8a | 355 | if (ret) |
a22285a6 | 356 | return ret; |
6a912213 JB |
357 | |
358 | dir = parent->d_inode; | |
359 | ||
9ed74f2d JB |
360 | /* |
361 | * 1 - inode item | |
362 | * 2 - refs | |
363 | * 1 - root item | |
364 | * 2 - dir items | |
365 | */ | |
a22285a6 | 366 | trans = btrfs_start_transaction(root, 6); |
2fbe8c8a | 367 | if (IS_ERR(trans)) |
a22285a6 | 368 | return PTR_ERR(trans); |
f46b5a66 | 369 | |
5d4f98a2 | 370 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
66d7e7f0 | 371 | 0, objectid, NULL, 0, 0, 0, 0); |
8e8a1e31 JB |
372 | if (IS_ERR(leaf)) { |
373 | ret = PTR_ERR(leaf); | |
374 | goto fail; | |
375 | } | |
f46b5a66 | 376 | |
5d4f98a2 | 377 | memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); |
f46b5a66 CH |
378 | btrfs_set_header_bytenr(leaf, leaf->start); |
379 | btrfs_set_header_generation(leaf, trans->transid); | |
5d4f98a2 | 380 | btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); |
f46b5a66 CH |
381 | btrfs_set_header_owner(leaf, objectid); |
382 | ||
383 | write_extent_buffer(leaf, root->fs_info->fsid, | |
384 | (unsigned long)btrfs_header_fsid(leaf), | |
385 | BTRFS_FSID_SIZE); | |
5d4f98a2 YZ |
386 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
387 | (unsigned long)btrfs_header_chunk_tree_uuid(leaf), | |
388 | BTRFS_UUID_SIZE); | |
f46b5a66 CH |
389 | btrfs_mark_buffer_dirty(leaf); |
390 | ||
391 | inode_item = &root_item.inode; | |
392 | memset(inode_item, 0, sizeof(*inode_item)); | |
393 | inode_item->generation = cpu_to_le64(1); | |
394 | inode_item->size = cpu_to_le64(3); | |
395 | inode_item->nlink = cpu_to_le32(1); | |
a76a3cd4 | 396 | inode_item->nbytes = cpu_to_le64(root->leafsize); |
f46b5a66 CH |
397 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
398 | ||
08fe4db1 LZ |
399 | root_item.flags = 0; |
400 | root_item.byte_limit = 0; | |
401 | inode_item->flags = cpu_to_le64(BTRFS_INODE_ROOT_ITEM_INIT); | |
402 | ||
f46b5a66 | 403 | btrfs_set_root_bytenr(&root_item, leaf->start); |
84234f3a | 404 | btrfs_set_root_generation(&root_item, trans->transid); |
f46b5a66 CH |
405 | btrfs_set_root_level(&root_item, 0); |
406 | btrfs_set_root_refs(&root_item, 1); | |
86b9f2ec | 407 | btrfs_set_root_used(&root_item, leaf->len); |
80ff3856 | 408 | btrfs_set_root_last_snapshot(&root_item, 0); |
f46b5a66 CH |
409 | |
410 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); | |
411 | root_item.drop_level = 0; | |
412 | ||
925baedd | 413 | btrfs_tree_unlock(leaf); |
f46b5a66 CH |
414 | free_extent_buffer(leaf); |
415 | leaf = NULL; | |
416 | ||
417 | btrfs_set_root_dirid(&root_item, new_dirid); | |
418 | ||
419 | key.objectid = objectid; | |
5d4f98a2 | 420 | key.offset = 0; |
f46b5a66 CH |
421 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
422 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | |
423 | &root_item); | |
424 | if (ret) | |
425 | goto fail; | |
426 | ||
76dda93c YZ |
427 | key.offset = (u64)-1; |
428 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); | |
79787eaa JM |
429 | if (IS_ERR(new_root)) { |
430 | btrfs_abort_transaction(trans, root, PTR_ERR(new_root)); | |
431 | ret = PTR_ERR(new_root); | |
432 | goto fail; | |
433 | } | |
76dda93c YZ |
434 | |
435 | btrfs_record_root_in_trans(trans, new_root); | |
436 | ||
d82a6f1d | 437 | ret = btrfs_create_subvol_root(trans, new_root, new_dirid); |
ce598979 MF |
438 | if (ret) { |
439 | /* We potentially lose an unused inode item here */ | |
79787eaa | 440 | btrfs_abort_transaction(trans, root, ret); |
ce598979 MF |
441 | goto fail; |
442 | } | |
443 | ||
f46b5a66 CH |
444 | /* |
445 | * insert the directory item | |
446 | */ | |
3de4586c | 447 | ret = btrfs_set_inode_index(dir, &index); |
79787eaa JM |
448 | if (ret) { |
449 | btrfs_abort_transaction(trans, root, ret); | |
450 | goto fail; | |
451 | } | |
3de4586c CM |
452 | |
453 | ret = btrfs_insert_dir_item(trans, root, | |
16cdcec7 | 454 | name, namelen, dir, &key, |
3de4586c | 455 | BTRFS_FT_DIR, index); |
79787eaa JM |
456 | if (ret) { |
457 | btrfs_abort_transaction(trans, root, ret); | |
f46b5a66 | 458 | goto fail; |
79787eaa | 459 | } |
0660b5af | 460 | |
52c26179 YZ |
461 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
462 | ret = btrfs_update_inode(trans, root, dir); | |
463 | BUG_ON(ret); | |
464 | ||
0660b5af | 465 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
4df27c4d | 466 | objectid, root->root_key.objectid, |
33345d01 | 467 | btrfs_ino(dir), index, name, namelen); |
0660b5af | 468 | |
76dda93c | 469 | BUG_ON(ret); |
f46b5a66 | 470 | |
76dda93c | 471 | d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); |
f46b5a66 | 472 | fail: |
72fd032e SW |
473 | if (async_transid) { |
474 | *async_transid = trans->transid; | |
475 | err = btrfs_commit_transaction_async(trans, root, 1); | |
476 | } else { | |
477 | err = btrfs_commit_transaction(trans, root); | |
478 | } | |
f46b5a66 CH |
479 | if (err && !ret) |
480 | ret = err; | |
f46b5a66 CH |
481 | return ret; |
482 | } | |
483 | ||
72fd032e | 484 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, |
b83cc969 LZ |
485 | char *name, int namelen, u64 *async_transid, |
486 | bool readonly) | |
f46b5a66 | 487 | { |
2e4bfab9 | 488 | struct inode *inode; |
f46b5a66 CH |
489 | struct btrfs_pending_snapshot *pending_snapshot; |
490 | struct btrfs_trans_handle *trans; | |
2e4bfab9 | 491 | int ret; |
f46b5a66 CH |
492 | |
493 | if (!root->ref_cows) | |
494 | return -EINVAL; | |
495 | ||
3de4586c | 496 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
a22285a6 YZ |
497 | if (!pending_snapshot) |
498 | return -ENOMEM; | |
499 | ||
500 | btrfs_init_block_rsv(&pending_snapshot->block_rsv); | |
3de4586c | 501 | pending_snapshot->dentry = dentry; |
f46b5a66 | 502 | pending_snapshot->root = root; |
b83cc969 | 503 | pending_snapshot->readonly = readonly; |
a22285a6 YZ |
504 | |
505 | trans = btrfs_start_transaction(root->fs_info->extent_root, 5); | |
506 | if (IS_ERR(trans)) { | |
507 | ret = PTR_ERR(trans); | |
508 | goto fail; | |
509 | } | |
510 | ||
511 | ret = btrfs_snap_reserve_metadata(trans, pending_snapshot); | |
512 | BUG_ON(ret); | |
513 | ||
8351583e | 514 | spin_lock(&root->fs_info->trans_lock); |
f46b5a66 CH |
515 | list_add(&pending_snapshot->list, |
516 | &trans->transaction->pending_snapshots); | |
8351583e | 517 | spin_unlock(&root->fs_info->trans_lock); |
72fd032e SW |
518 | if (async_transid) { |
519 | *async_transid = trans->transid; | |
520 | ret = btrfs_commit_transaction_async(trans, | |
521 | root->fs_info->extent_root, 1); | |
522 | } else { | |
523 | ret = btrfs_commit_transaction(trans, | |
524 | root->fs_info->extent_root); | |
525 | } | |
2e4bfab9 | 526 | BUG_ON(ret); |
a22285a6 YZ |
527 | |
528 | ret = pending_snapshot->error; | |
529 | if (ret) | |
530 | goto fail; | |
531 | ||
66b4ffd1 JB |
532 | ret = btrfs_orphan_cleanup(pending_snapshot->snap); |
533 | if (ret) | |
534 | goto fail; | |
f46b5a66 | 535 | |
2fbe8c8a | 536 | inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); |
2e4bfab9 YZ |
537 | if (IS_ERR(inode)) { |
538 | ret = PTR_ERR(inode); | |
539 | goto fail; | |
540 | } | |
541 | BUG_ON(!inode); | |
542 | d_instantiate(dentry, inode); | |
543 | ret = 0; | |
544 | fail: | |
a22285a6 | 545 | kfree(pending_snapshot); |
f46b5a66 CH |
546 | return ret; |
547 | } | |
548 | ||
4260f7c7 SW |
549 | /* copy of check_sticky in fs/namei.c() |
550 | * It's inline, so penalty for filesystems that don't use sticky bit is | |
551 | * minimal. | |
552 | */ | |
553 | static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode) | |
554 | { | |
555 | uid_t fsuid = current_fsuid(); | |
556 | ||
557 | if (!(dir->i_mode & S_ISVTX)) | |
558 | return 0; | |
559 | if (inode->i_uid == fsuid) | |
560 | return 0; | |
561 | if (dir->i_uid == fsuid) | |
562 | return 0; | |
563 | return !capable(CAP_FOWNER); | |
564 | } | |
565 | ||
566 | /* copy of may_delete in fs/namei.c() | |
567 | * Check whether we can remove a link victim from directory dir, check | |
568 | * whether the type of victim is right. | |
569 | * 1. We can't do it if dir is read-only (done in permission()) | |
570 | * 2. We should have write and exec permissions on dir | |
571 | * 3. We can't remove anything from append-only dir | |
572 | * 4. We can't do anything with immutable dir (done in permission()) | |
573 | * 5. If the sticky bit on dir is set we should either | |
574 | * a. be owner of dir, or | |
575 | * b. be owner of victim, or | |
576 | * c. have CAP_FOWNER capability | |
577 | * 6. If the victim is append-only or immutable we can't do antyhing with | |
578 | * links pointing to it. | |
579 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
580 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
581 | * 9. We can't remove a root or mountpoint. | |
582 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by | |
583 | * nfs_async_unlink(). | |
584 | */ | |
585 | ||
586 | static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir) | |
587 | { | |
588 | int error; | |
589 | ||
590 | if (!victim->d_inode) | |
591 | return -ENOENT; | |
592 | ||
593 | BUG_ON(victim->d_parent->d_inode != dir); | |
594 | audit_inode_child(victim, dir); | |
595 | ||
596 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
597 | if (error) | |
598 | return error; | |
599 | if (IS_APPEND(dir)) | |
600 | return -EPERM; | |
601 | if (btrfs_check_sticky(dir, victim->d_inode)|| | |
602 | IS_APPEND(victim->d_inode)|| | |
603 | IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) | |
604 | return -EPERM; | |
605 | if (isdir) { | |
606 | if (!S_ISDIR(victim->d_inode->i_mode)) | |
607 | return -ENOTDIR; | |
608 | if (IS_ROOT(victim)) | |
609 | return -EBUSY; | |
610 | } else if (S_ISDIR(victim->d_inode->i_mode)) | |
611 | return -EISDIR; | |
612 | if (IS_DEADDIR(dir)) | |
613 | return -ENOENT; | |
614 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
615 | return -EBUSY; | |
616 | return 0; | |
617 | } | |
618 | ||
cb8e7090 CH |
619 | /* copy of may_create in fs/namei.c() */ |
620 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) | |
621 | { | |
622 | if (child->d_inode) | |
623 | return -EEXIST; | |
624 | if (IS_DEADDIR(dir)) | |
625 | return -ENOENT; | |
626 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
627 | } | |
628 | ||
629 | /* | |
630 | * Create a new subvolume below @parent. This is largely modeled after | |
631 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup | |
632 | * inside this filesystem so it's quite a bit simpler. | |
633 | */ | |
76dda93c YZ |
634 | static noinline int btrfs_mksubvol(struct path *parent, |
635 | char *name, int namelen, | |
72fd032e | 636 | struct btrfs_root *snap_src, |
b83cc969 | 637 | u64 *async_transid, bool readonly) |
cb8e7090 | 638 | { |
76dda93c | 639 | struct inode *dir = parent->dentry->d_inode; |
cb8e7090 CH |
640 | struct dentry *dentry; |
641 | int error; | |
642 | ||
76dda93c | 643 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
cb8e7090 CH |
644 | |
645 | dentry = lookup_one_len(name, parent->dentry, namelen); | |
646 | error = PTR_ERR(dentry); | |
647 | if (IS_ERR(dentry)) | |
648 | goto out_unlock; | |
649 | ||
650 | error = -EEXIST; | |
651 | if (dentry->d_inode) | |
652 | goto out_dput; | |
653 | ||
cb8e7090 CH |
654 | error = mnt_want_write(parent->mnt); |
655 | if (error) | |
656 | goto out_dput; | |
657 | ||
76dda93c | 658 | error = btrfs_may_create(dir, dentry); |
cb8e7090 CH |
659 | if (error) |
660 | goto out_drop_write; | |
661 | ||
76dda93c YZ |
662 | down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
663 | ||
664 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) | |
665 | goto out_up_read; | |
666 | ||
3de4586c | 667 | if (snap_src) { |
72fd032e | 668 | error = create_snapshot(snap_src, dentry, |
b83cc969 | 669 | name, namelen, async_transid, readonly); |
3de4586c | 670 | } else { |
76dda93c | 671 | error = create_subvol(BTRFS_I(dir)->root, dentry, |
72fd032e | 672 | name, namelen, async_transid); |
3de4586c | 673 | } |
76dda93c YZ |
674 | if (!error) |
675 | fsnotify_mkdir(dir, dentry); | |
676 | out_up_read: | |
677 | up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); | |
cb8e7090 CH |
678 | out_drop_write: |
679 | mnt_drop_write(parent->mnt); | |
680 | out_dput: | |
681 | dput(dentry); | |
682 | out_unlock: | |
76dda93c | 683 | mutex_unlock(&dir->i_mutex); |
cb8e7090 CH |
684 | return error; |
685 | } | |
686 | ||
4cb5300b CM |
687 | /* |
688 | * When we're defragging a range, we don't want to kick it off again | |
689 | * if it is really just waiting for delalloc to send it down. | |
690 | * If we find a nice big extent or delalloc range for the bytes in the | |
691 | * file you want to defrag, we return 0 to let you know to skip this | |
692 | * part of the file | |
693 | */ | |
694 | static int check_defrag_in_cache(struct inode *inode, u64 offset, int thresh) | |
695 | { | |
696 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
697 | struct extent_map *em = NULL; | |
698 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
699 | u64 end; | |
700 | ||
701 | read_lock(&em_tree->lock); | |
702 | em = lookup_extent_mapping(em_tree, offset, PAGE_CACHE_SIZE); | |
703 | read_unlock(&em_tree->lock); | |
704 | ||
705 | if (em) { | |
706 | end = extent_map_end(em); | |
707 | free_extent_map(em); | |
708 | if (end - offset > thresh) | |
709 | return 0; | |
710 | } | |
711 | /* if we already have a nice delalloc here, just stop */ | |
712 | thresh /= 2; | |
713 | end = count_range_bits(io_tree, &offset, offset + thresh, | |
714 | thresh, EXTENT_DELALLOC, 1); | |
715 | if (end >= thresh) | |
716 | return 0; | |
717 | return 1; | |
718 | } | |
719 | ||
720 | /* | |
721 | * helper function to walk through a file and find extents | |
722 | * newer than a specific transid, and smaller than thresh. | |
723 | * | |
724 | * This is used by the defragging code to find new and small | |
725 | * extents | |
726 | */ | |
727 | static int find_new_extents(struct btrfs_root *root, | |
728 | struct inode *inode, u64 newer_than, | |
729 | u64 *off, int thresh) | |
730 | { | |
731 | struct btrfs_path *path; | |
732 | struct btrfs_key min_key; | |
733 | struct btrfs_key max_key; | |
734 | struct extent_buffer *leaf; | |
735 | struct btrfs_file_extent_item *extent; | |
736 | int type; | |
737 | int ret; | |
a4689d2b | 738 | u64 ino = btrfs_ino(inode); |
4cb5300b CM |
739 | |
740 | path = btrfs_alloc_path(); | |
741 | if (!path) | |
742 | return -ENOMEM; | |
743 | ||
a4689d2b | 744 | min_key.objectid = ino; |
4cb5300b CM |
745 | min_key.type = BTRFS_EXTENT_DATA_KEY; |
746 | min_key.offset = *off; | |
747 | ||
a4689d2b | 748 | max_key.objectid = ino; |
4cb5300b CM |
749 | max_key.type = (u8)-1; |
750 | max_key.offset = (u64)-1; | |
751 | ||
752 | path->keep_locks = 1; | |
753 | ||
754 | while(1) { | |
755 | ret = btrfs_search_forward(root, &min_key, &max_key, | |
756 | path, 0, newer_than); | |
757 | if (ret != 0) | |
758 | goto none; | |
a4689d2b | 759 | if (min_key.objectid != ino) |
4cb5300b CM |
760 | goto none; |
761 | if (min_key.type != BTRFS_EXTENT_DATA_KEY) | |
762 | goto none; | |
763 | ||
764 | leaf = path->nodes[0]; | |
765 | extent = btrfs_item_ptr(leaf, path->slots[0], | |
766 | struct btrfs_file_extent_item); | |
767 | ||
768 | type = btrfs_file_extent_type(leaf, extent); | |
769 | if (type == BTRFS_FILE_EXTENT_REG && | |
770 | btrfs_file_extent_num_bytes(leaf, extent) < thresh && | |
771 | check_defrag_in_cache(inode, min_key.offset, thresh)) { | |
772 | *off = min_key.offset; | |
773 | btrfs_free_path(path); | |
774 | return 0; | |
775 | } | |
776 | ||
777 | if (min_key.offset == (u64)-1) | |
778 | goto none; | |
779 | ||
780 | min_key.offset++; | |
781 | btrfs_release_path(path); | |
782 | } | |
783 | none: | |
784 | btrfs_free_path(path); | |
785 | return -ENOENT; | |
786 | } | |
787 | ||
17ce6ef8 LB |
788 | /* |
789 | * Validaty check of prev em and next em: | |
790 | * 1) no prev/next em | |
791 | * 2) prev/next em is an hole/inline extent | |
792 | */ | |
793 | static int check_adjacent_extents(struct inode *inode, struct extent_map *em) | |
794 | { | |
795 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
796 | struct extent_map *prev = NULL, *next = NULL; | |
797 | int ret = 0; | |
798 | ||
799 | read_lock(&em_tree->lock); | |
800 | prev = lookup_extent_mapping(em_tree, em->start - 1, (u64)-1); | |
801 | next = lookup_extent_mapping(em_tree, em->start + em->len, (u64)-1); | |
802 | read_unlock(&em_tree->lock); | |
803 | ||
804 | if ((!prev || prev->block_start >= EXTENT_MAP_LAST_BYTE) && | |
805 | (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)) | |
806 | ret = 1; | |
807 | free_extent_map(prev); | |
808 | free_extent_map(next); | |
809 | ||
810 | return ret; | |
811 | } | |
812 | ||
940100a4 | 813 | static int should_defrag_range(struct inode *inode, u64 start, u64 len, |
1e701a32 CM |
814 | int thresh, u64 *last_len, u64 *skip, |
815 | u64 *defrag_end) | |
940100a4 CM |
816 | { |
817 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
818 | struct extent_map *em = NULL; | |
819 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
820 | int ret = 1; | |
821 | ||
822 | /* | |
008873ea | 823 | * make sure that once we start defragging an extent, we keep on |
940100a4 CM |
824 | * defragging it |
825 | */ | |
826 | if (start < *defrag_end) | |
827 | return 1; | |
828 | ||
829 | *skip = 0; | |
830 | ||
831 | /* | |
832 | * hopefully we have this extent in the tree already, try without | |
833 | * the full extent lock | |
834 | */ | |
835 | read_lock(&em_tree->lock); | |
836 | em = lookup_extent_mapping(em_tree, start, len); | |
837 | read_unlock(&em_tree->lock); | |
838 | ||
839 | if (!em) { | |
840 | /* get the big lock and read metadata off disk */ | |
d0082371 | 841 | lock_extent(io_tree, start, start + len - 1); |
940100a4 | 842 | em = btrfs_get_extent(inode, NULL, 0, start, len, 0); |
d0082371 | 843 | unlock_extent(io_tree, start, start + len - 1); |
940100a4 | 844 | |
6cf8bfbf | 845 | if (IS_ERR(em)) |
940100a4 CM |
846 | return 0; |
847 | } | |
848 | ||
849 | /* this will cover holes, and inline extents */ | |
17ce6ef8 | 850 | if (em->block_start >= EXTENT_MAP_LAST_BYTE) { |
940100a4 | 851 | ret = 0; |
17ce6ef8 LB |
852 | goto out; |
853 | } | |
854 | ||
855 | /* If we have nothing to merge with us, just skip. */ | |
856 | if (check_adjacent_extents(inode, em)) { | |
857 | ret = 0; | |
858 | goto out; | |
859 | } | |
940100a4 CM |
860 | |
861 | /* | |
862 | * we hit a real extent, if it is big don't bother defragging it again | |
863 | */ | |
1e701a32 | 864 | if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh) |
940100a4 CM |
865 | ret = 0; |
866 | ||
17ce6ef8 | 867 | out: |
940100a4 CM |
868 | /* |
869 | * last_len ends up being a counter of how many bytes we've defragged. | |
870 | * every time we choose not to defrag an extent, we reset *last_len | |
871 | * so that the next tiny extent will force a defrag. | |
872 | * | |
873 | * The end result of this is that tiny extents before a single big | |
874 | * extent will force at least part of that big extent to be defragged. | |
875 | */ | |
876 | if (ret) { | |
940100a4 CM |
877 | *defrag_end = extent_map_end(em); |
878 | } else { | |
879 | *last_len = 0; | |
880 | *skip = extent_map_end(em); | |
881 | *defrag_end = 0; | |
882 | } | |
883 | ||
884 | free_extent_map(em); | |
885 | return ret; | |
886 | } | |
887 | ||
4cb5300b CM |
888 | /* |
889 | * it doesn't do much good to defrag one or two pages | |
890 | * at a time. This pulls in a nice chunk of pages | |
891 | * to COW and defrag. | |
892 | * | |
893 | * It also makes sure the delalloc code has enough | |
894 | * dirty data to avoid making new small extents as part | |
895 | * of the defrag | |
896 | * | |
897 | * It's a good idea to start RA on this range | |
898 | * before calling this. | |
899 | */ | |
900 | static int cluster_pages_for_defrag(struct inode *inode, | |
901 | struct page **pages, | |
902 | unsigned long start_index, | |
903 | int num_pages) | |
f46b5a66 | 904 | { |
4cb5300b CM |
905 | unsigned long file_end; |
906 | u64 isize = i_size_read(inode); | |
907 | u64 page_start; | |
908 | u64 page_end; | |
1f12bd06 | 909 | u64 page_cnt; |
4cb5300b CM |
910 | int ret; |
911 | int i; | |
912 | int i_done; | |
3eaa2885 | 913 | struct btrfs_ordered_extent *ordered; |
4cb5300b | 914 | struct extent_state *cached_state = NULL; |
600a45e1 | 915 | struct extent_io_tree *tree; |
3b16a4e3 | 916 | gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping); |
4cb5300b | 917 | |
4cb5300b | 918 | file_end = (isize - 1) >> PAGE_CACHE_SHIFT; |
1f12bd06 LB |
919 | if (!isize || start_index > file_end) |
920 | return 0; | |
921 | ||
922 | page_cnt = min_t(u64, (u64)num_pages, (u64)file_end - start_index + 1); | |
4cb5300b CM |
923 | |
924 | ret = btrfs_delalloc_reserve_space(inode, | |
1f12bd06 | 925 | page_cnt << PAGE_CACHE_SHIFT); |
4cb5300b CM |
926 | if (ret) |
927 | return ret; | |
4cb5300b | 928 | i_done = 0; |
600a45e1 | 929 | tree = &BTRFS_I(inode)->io_tree; |
4cb5300b CM |
930 | |
931 | /* step one, lock all the pages */ | |
1f12bd06 | 932 | for (i = 0; i < page_cnt; i++) { |
4cb5300b | 933 | struct page *page; |
600a45e1 | 934 | again: |
a94733d0 | 935 | page = find_or_create_page(inode->i_mapping, |
600a45e1 | 936 | start_index + i, mask); |
4cb5300b CM |
937 | if (!page) |
938 | break; | |
939 | ||
600a45e1 MX |
940 | page_start = page_offset(page); |
941 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
942 | while (1) { | |
d0082371 | 943 | lock_extent(tree, page_start, page_end); |
600a45e1 MX |
944 | ordered = btrfs_lookup_ordered_extent(inode, |
945 | page_start); | |
d0082371 | 946 | unlock_extent(tree, page_start, page_end); |
600a45e1 MX |
947 | if (!ordered) |
948 | break; | |
949 | ||
950 | unlock_page(page); | |
951 | btrfs_start_ordered_extent(inode, ordered, 1); | |
952 | btrfs_put_ordered_extent(ordered); | |
953 | lock_page(page); | |
1f12bd06 LB |
954 | /* |
955 | * we unlocked the page above, so we need check if | |
956 | * it was released or not. | |
957 | */ | |
958 | if (page->mapping != inode->i_mapping) { | |
959 | unlock_page(page); | |
960 | page_cache_release(page); | |
961 | goto again; | |
962 | } | |
600a45e1 MX |
963 | } |
964 | ||
4cb5300b CM |
965 | if (!PageUptodate(page)) { |
966 | btrfs_readpage(NULL, page); | |
967 | lock_page(page); | |
968 | if (!PageUptodate(page)) { | |
969 | unlock_page(page); | |
970 | page_cache_release(page); | |
971 | ret = -EIO; | |
972 | break; | |
973 | } | |
974 | } | |
600a45e1 | 975 | |
600a45e1 MX |
976 | if (page->mapping != inode->i_mapping) { |
977 | unlock_page(page); | |
978 | page_cache_release(page); | |
979 | goto again; | |
980 | } | |
981 | ||
4cb5300b CM |
982 | pages[i] = page; |
983 | i_done++; | |
984 | } | |
985 | if (!i_done || ret) | |
986 | goto out; | |
987 | ||
988 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) | |
989 | goto out; | |
990 | ||
991 | /* | |
992 | * so now we have a nice long stream of locked | |
993 | * and up to date pages, lets wait on them | |
994 | */ | |
995 | for (i = 0; i < i_done; i++) | |
996 | wait_on_page_writeback(pages[i]); | |
997 | ||
998 | page_start = page_offset(pages[0]); | |
999 | page_end = page_offset(pages[i_done - 1]) + PAGE_CACHE_SIZE; | |
1000 | ||
1001 | lock_extent_bits(&BTRFS_I(inode)->io_tree, | |
d0082371 | 1002 | page_start, page_end - 1, 0, &cached_state); |
4cb5300b CM |
1003 | clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, |
1004 | page_end - 1, EXTENT_DIRTY | EXTENT_DELALLOC | | |
1005 | EXTENT_DO_ACCOUNTING, 0, 0, &cached_state, | |
1006 | GFP_NOFS); | |
1007 | ||
1f12bd06 | 1008 | if (i_done != page_cnt) { |
9e0baf60 JB |
1009 | spin_lock(&BTRFS_I(inode)->lock); |
1010 | BTRFS_I(inode)->outstanding_extents++; | |
1011 | spin_unlock(&BTRFS_I(inode)->lock); | |
4cb5300b | 1012 | btrfs_delalloc_release_space(inode, |
1f12bd06 | 1013 | (page_cnt - i_done) << PAGE_CACHE_SHIFT); |
4cb5300b CM |
1014 | } |
1015 | ||
1016 | ||
1017 | btrfs_set_extent_delalloc(inode, page_start, page_end - 1, | |
1018 | &cached_state); | |
1019 | ||
1020 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, | |
1021 | page_start, page_end - 1, &cached_state, | |
1022 | GFP_NOFS); | |
1023 | ||
1024 | for (i = 0; i < i_done; i++) { | |
1025 | clear_page_dirty_for_io(pages[i]); | |
1026 | ClearPageChecked(pages[i]); | |
1027 | set_page_extent_mapped(pages[i]); | |
1028 | set_page_dirty(pages[i]); | |
1029 | unlock_page(pages[i]); | |
1030 | page_cache_release(pages[i]); | |
1031 | } | |
1032 | return i_done; | |
1033 | out: | |
1034 | for (i = 0; i < i_done; i++) { | |
1035 | unlock_page(pages[i]); | |
1036 | page_cache_release(pages[i]); | |
1037 | } | |
1f12bd06 | 1038 | btrfs_delalloc_release_space(inode, page_cnt << PAGE_CACHE_SHIFT); |
4cb5300b CM |
1039 | return ret; |
1040 | ||
1041 | } | |
1042 | ||
1043 | int btrfs_defrag_file(struct inode *inode, struct file *file, | |
1044 | struct btrfs_ioctl_defrag_range_args *range, | |
1045 | u64 newer_than, unsigned long max_to_defrag) | |
1046 | { | |
1047 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1a419d85 | 1048 | struct btrfs_super_block *disk_super; |
4cb5300b | 1049 | struct file_ra_state *ra = NULL; |
f46b5a66 | 1050 | unsigned long last_index; |
151a31b2 | 1051 | u64 isize = i_size_read(inode); |
1a419d85 | 1052 | u64 features; |
940100a4 CM |
1053 | u64 last_len = 0; |
1054 | u64 skip = 0; | |
1055 | u64 defrag_end = 0; | |
4cb5300b | 1056 | u64 newer_off = range->start; |
f46b5a66 | 1057 | unsigned long i; |
008873ea | 1058 | unsigned long ra_index = 0; |
f46b5a66 | 1059 | int ret; |
4cb5300b | 1060 | int defrag_count = 0; |
1a419d85 | 1061 | int compress_type = BTRFS_COMPRESS_ZLIB; |
4cb5300b | 1062 | int extent_thresh = range->extent_thresh; |
008873ea LZ |
1063 | int max_cluster = (256 * 1024) >> PAGE_CACHE_SHIFT; |
1064 | int cluster = max_cluster; | |
4cb5300b CM |
1065 | u64 new_align = ~((u64)128 * 1024 - 1); |
1066 | struct page **pages = NULL; | |
1067 | ||
1068 | if (extent_thresh == 0) | |
1069 | extent_thresh = 256 * 1024; | |
1a419d85 LZ |
1070 | |
1071 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) { | |
1072 | if (range->compress_type > BTRFS_COMPRESS_TYPES) | |
1073 | return -EINVAL; | |
1074 | if (range->compress_type) | |
1075 | compress_type = range->compress_type; | |
1076 | } | |
f46b5a66 | 1077 | |
151a31b2 | 1078 | if (isize == 0) |
940100a4 CM |
1079 | return 0; |
1080 | ||
4cb5300b CM |
1081 | /* |
1082 | * if we were not given a file, allocate a readahead | |
1083 | * context | |
1084 | */ | |
1085 | if (!file) { | |
1086 | ra = kzalloc(sizeof(*ra), GFP_NOFS); | |
1087 | if (!ra) | |
1088 | return -ENOMEM; | |
1089 | file_ra_state_init(ra, inode->i_mapping); | |
1090 | } else { | |
1091 | ra = &file->f_ra; | |
1092 | } | |
1093 | ||
008873ea | 1094 | pages = kmalloc(sizeof(struct page *) * max_cluster, |
4cb5300b CM |
1095 | GFP_NOFS); |
1096 | if (!pages) { | |
1097 | ret = -ENOMEM; | |
1098 | goto out_ra; | |
1099 | } | |
1100 | ||
1101 | /* find the last page to defrag */ | |
1e701a32 | 1102 | if (range->start + range->len > range->start) { |
151a31b2 | 1103 | last_index = min_t(u64, isize - 1, |
1e701a32 CM |
1104 | range->start + range->len - 1) >> PAGE_CACHE_SHIFT; |
1105 | } else { | |
151a31b2 | 1106 | last_index = (isize - 1) >> PAGE_CACHE_SHIFT; |
1e701a32 CM |
1107 | } |
1108 | ||
4cb5300b CM |
1109 | if (newer_than) { |
1110 | ret = find_new_extents(root, inode, newer_than, | |
1111 | &newer_off, 64 * 1024); | |
1112 | if (!ret) { | |
1113 | range->start = newer_off; | |
1114 | /* | |
1115 | * we always align our defrag to help keep | |
1116 | * the extents in the file evenly spaced | |
1117 | */ | |
1118 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; | |
4cb5300b CM |
1119 | } else |
1120 | goto out_ra; | |
1121 | } else { | |
1122 | i = range->start >> PAGE_CACHE_SHIFT; | |
1123 | } | |
1124 | if (!max_to_defrag) | |
7ec31b54 | 1125 | max_to_defrag = last_index + 1; |
4cb5300b | 1126 | |
2a0f7f57 LZ |
1127 | /* |
1128 | * make writeback starts from i, so the defrag range can be | |
1129 | * written sequentially. | |
1130 | */ | |
1131 | if (i < inode->i_mapping->writeback_index) | |
1132 | inode->i_mapping->writeback_index = i; | |
1133 | ||
f7f43cc8 CM |
1134 | while (i <= last_index && defrag_count < max_to_defrag && |
1135 | (i < (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> | |
1136 | PAGE_CACHE_SHIFT)) { | |
4cb5300b CM |
1137 | /* |
1138 | * make sure we stop running if someone unmounts | |
1139 | * the FS | |
1140 | */ | |
1141 | if (!(inode->i_sb->s_flags & MS_ACTIVE)) | |
1142 | break; | |
1143 | ||
66c26892 LB |
1144 | if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT, |
1145 | PAGE_CACHE_SIZE, extent_thresh, | |
1146 | &last_len, &skip, &defrag_end)) { | |
940100a4 CM |
1147 | unsigned long next; |
1148 | /* | |
1149 | * the should_defrag function tells us how much to skip | |
1150 | * bump our counter by the suggested amount | |
1151 | */ | |
1152 | next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
1153 | i = max(i + 1, next); | |
1154 | continue; | |
1155 | } | |
008873ea LZ |
1156 | |
1157 | if (!newer_than) { | |
1158 | cluster = (PAGE_CACHE_ALIGN(defrag_end) >> | |
1159 | PAGE_CACHE_SHIFT) - i; | |
1160 | cluster = min(cluster, max_cluster); | |
1161 | } else { | |
1162 | cluster = max_cluster; | |
1163 | } | |
1164 | ||
1e701a32 | 1165 | if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) |
1a419d85 | 1166 | BTRFS_I(inode)->force_compress = compress_type; |
940100a4 | 1167 | |
008873ea LZ |
1168 | if (i + cluster > ra_index) { |
1169 | ra_index = max(i, ra_index); | |
1170 | btrfs_force_ra(inode->i_mapping, ra, file, ra_index, | |
1171 | cluster); | |
1172 | ra_index += max_cluster; | |
1173 | } | |
940100a4 | 1174 | |
ecb8bea8 | 1175 | mutex_lock(&inode->i_mutex); |
008873ea | 1176 | ret = cluster_pages_for_defrag(inode, pages, i, cluster); |
ecb8bea8 LB |
1177 | if (ret < 0) { |
1178 | mutex_unlock(&inode->i_mutex); | |
4cb5300b | 1179 | goto out_ra; |
ecb8bea8 | 1180 | } |
4cb5300b CM |
1181 | |
1182 | defrag_count += ret; | |
1183 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, ret); | |
ecb8bea8 | 1184 | mutex_unlock(&inode->i_mutex); |
4cb5300b CM |
1185 | |
1186 | if (newer_than) { | |
1187 | if (newer_off == (u64)-1) | |
1188 | break; | |
1189 | ||
e1f041e1 LB |
1190 | if (ret > 0) |
1191 | i += ret; | |
1192 | ||
4cb5300b CM |
1193 | newer_off = max(newer_off + 1, |
1194 | (u64)i << PAGE_CACHE_SHIFT); | |
1195 | ||
1196 | ret = find_new_extents(root, inode, | |
1197 | newer_than, &newer_off, | |
1198 | 64 * 1024); | |
1199 | if (!ret) { | |
1200 | range->start = newer_off; | |
1201 | i = (newer_off & new_align) >> PAGE_CACHE_SHIFT; | |
4cb5300b CM |
1202 | } else { |
1203 | break; | |
f46b5a66 | 1204 | } |
4cb5300b | 1205 | } else { |
008873ea | 1206 | if (ret > 0) { |
cbcc8326 | 1207 | i += ret; |
008873ea LZ |
1208 | last_len += ret << PAGE_CACHE_SHIFT; |
1209 | } else { | |
cbcc8326 | 1210 | i++; |
008873ea LZ |
1211 | last_len = 0; |
1212 | } | |
f46b5a66 | 1213 | } |
f46b5a66 CH |
1214 | } |
1215 | ||
1e701a32 CM |
1216 | if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO)) |
1217 | filemap_flush(inode->i_mapping); | |
1218 | ||
1219 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { | |
1220 | /* the filemap_flush will queue IO into the worker threads, but | |
1221 | * we have to make sure the IO is actually started and that | |
1222 | * ordered extents get created before we return | |
1223 | */ | |
1224 | atomic_inc(&root->fs_info->async_submit_draining); | |
1225 | while (atomic_read(&root->fs_info->nr_async_submits) || | |
1226 | atomic_read(&root->fs_info->async_delalloc_pages)) { | |
1227 | wait_event(root->fs_info->async_submit_wait, | |
1228 | (atomic_read(&root->fs_info->nr_async_submits) == 0 && | |
1229 | atomic_read(&root->fs_info->async_delalloc_pages) == 0)); | |
1230 | } | |
1231 | atomic_dec(&root->fs_info->async_submit_draining); | |
1232 | ||
1233 | mutex_lock(&inode->i_mutex); | |
261507a0 | 1234 | BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE; |
1e701a32 CM |
1235 | mutex_unlock(&inode->i_mutex); |
1236 | } | |
1237 | ||
6c41761f | 1238 | disk_super = root->fs_info->super_copy; |
1a419d85 LZ |
1239 | features = btrfs_super_incompat_flags(disk_super); |
1240 | if (range->compress_type == BTRFS_COMPRESS_LZO) { | |
1241 | features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO; | |
1242 | btrfs_set_super_incompat_flags(disk_super, features); | |
1243 | } | |
1244 | ||
60ccf82f | 1245 | ret = defrag_count; |
940100a4 | 1246 | |
4cb5300b CM |
1247 | out_ra: |
1248 | if (!file) | |
1249 | kfree(ra); | |
1250 | kfree(pages); | |
940100a4 | 1251 | return ret; |
f46b5a66 CH |
1252 | } |
1253 | ||
76dda93c YZ |
1254 | static noinline int btrfs_ioctl_resize(struct btrfs_root *root, |
1255 | void __user *arg) | |
f46b5a66 CH |
1256 | { |
1257 | u64 new_size; | |
1258 | u64 old_size; | |
1259 | u64 devid = 1; | |
1260 | struct btrfs_ioctl_vol_args *vol_args; | |
1261 | struct btrfs_trans_handle *trans; | |
1262 | struct btrfs_device *device = NULL; | |
1263 | char *sizestr; | |
1264 | char *devstr = NULL; | |
1265 | int ret = 0; | |
f46b5a66 CH |
1266 | int mod = 0; |
1267 | ||
c146afad YZ |
1268 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
1269 | return -EROFS; | |
1270 | ||
e441d54d CM |
1271 | if (!capable(CAP_SYS_ADMIN)) |
1272 | return -EPERM; | |
1273 | ||
c9e9f97b ID |
1274 | mutex_lock(&root->fs_info->volume_mutex); |
1275 | if (root->fs_info->balance_ctl) { | |
1276 | printk(KERN_INFO "btrfs: balance in progress\n"); | |
1277 | ret = -EINVAL; | |
1278 | goto out; | |
1279 | } | |
1280 | ||
dae7b665 | 1281 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
1282 | if (IS_ERR(vol_args)) { |
1283 | ret = PTR_ERR(vol_args); | |
1284 | goto out; | |
1285 | } | |
5516e595 MF |
1286 | |
1287 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
f46b5a66 | 1288 | |
f46b5a66 CH |
1289 | sizestr = vol_args->name; |
1290 | devstr = strchr(sizestr, ':'); | |
1291 | if (devstr) { | |
1292 | char *end; | |
1293 | sizestr = devstr + 1; | |
1294 | *devstr = '\0'; | |
1295 | devstr = vol_args->name; | |
1296 | devid = simple_strtoull(devstr, &end, 10); | |
5bb14682 | 1297 | printk(KERN_INFO "btrfs: resizing devid %llu\n", |
21380931 | 1298 | (unsigned long long)devid); |
f46b5a66 | 1299 | } |
2b82032c | 1300 | device = btrfs_find_device(root, devid, NULL, NULL); |
f46b5a66 | 1301 | if (!device) { |
5bb14682 | 1302 | printk(KERN_INFO "btrfs: resizer unable to find device %llu\n", |
21380931 | 1303 | (unsigned long long)devid); |
f46b5a66 | 1304 | ret = -EINVAL; |
c9e9f97b | 1305 | goto out_free; |
f46b5a66 CH |
1306 | } |
1307 | if (!strcmp(sizestr, "max")) | |
1308 | new_size = device->bdev->bd_inode->i_size; | |
1309 | else { | |
1310 | if (sizestr[0] == '-') { | |
1311 | mod = -1; | |
1312 | sizestr++; | |
1313 | } else if (sizestr[0] == '+') { | |
1314 | mod = 1; | |
1315 | sizestr++; | |
1316 | } | |
91748467 | 1317 | new_size = memparse(sizestr, NULL); |
f46b5a66 CH |
1318 | if (new_size == 0) { |
1319 | ret = -EINVAL; | |
c9e9f97b | 1320 | goto out_free; |
f46b5a66 CH |
1321 | } |
1322 | } | |
1323 | ||
1324 | old_size = device->total_bytes; | |
1325 | ||
1326 | if (mod < 0) { | |
1327 | if (new_size > old_size) { | |
1328 | ret = -EINVAL; | |
c9e9f97b | 1329 | goto out_free; |
f46b5a66 CH |
1330 | } |
1331 | new_size = old_size - new_size; | |
1332 | } else if (mod > 0) { | |
1333 | new_size = old_size + new_size; | |
1334 | } | |
1335 | ||
1336 | if (new_size < 256 * 1024 * 1024) { | |
1337 | ret = -EINVAL; | |
c9e9f97b | 1338 | goto out_free; |
f46b5a66 CH |
1339 | } |
1340 | if (new_size > device->bdev->bd_inode->i_size) { | |
1341 | ret = -EFBIG; | |
c9e9f97b | 1342 | goto out_free; |
f46b5a66 CH |
1343 | } |
1344 | ||
1345 | do_div(new_size, root->sectorsize); | |
1346 | new_size *= root->sectorsize; | |
1347 | ||
5bb14682 | 1348 | printk(KERN_INFO "btrfs: new size for %s is %llu\n", |
f46b5a66 CH |
1349 | device->name, (unsigned long long)new_size); |
1350 | ||
1351 | if (new_size > old_size) { | |
a22285a6 | 1352 | trans = btrfs_start_transaction(root, 0); |
98d5dc13 TI |
1353 | if (IS_ERR(trans)) { |
1354 | ret = PTR_ERR(trans); | |
c9e9f97b | 1355 | goto out_free; |
98d5dc13 | 1356 | } |
f46b5a66 CH |
1357 | ret = btrfs_grow_device(trans, device, new_size); |
1358 | btrfs_commit_transaction(trans, root); | |
ece7d20e | 1359 | } else if (new_size < old_size) { |
f46b5a66 CH |
1360 | ret = btrfs_shrink_device(device, new_size); |
1361 | } | |
1362 | ||
c9e9f97b | 1363 | out_free: |
f46b5a66 | 1364 | kfree(vol_args); |
c9e9f97b ID |
1365 | out: |
1366 | mutex_unlock(&root->fs_info->volume_mutex); | |
f46b5a66 CH |
1367 | return ret; |
1368 | } | |
1369 | ||
72fd032e SW |
1370 | static noinline int btrfs_ioctl_snap_create_transid(struct file *file, |
1371 | char *name, | |
1372 | unsigned long fd, | |
1373 | int subvol, | |
b83cc969 LZ |
1374 | u64 *transid, |
1375 | bool readonly) | |
f46b5a66 | 1376 | { |
cb8e7090 | 1377 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
3de4586c | 1378 | struct file *src_file; |
f46b5a66 | 1379 | int namelen; |
3de4586c | 1380 | int ret = 0; |
f46b5a66 | 1381 | |
c146afad YZ |
1382 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
1383 | return -EROFS; | |
1384 | ||
72fd032e SW |
1385 | namelen = strlen(name); |
1386 | if (strchr(name, '/')) { | |
f46b5a66 CH |
1387 | ret = -EINVAL; |
1388 | goto out; | |
1389 | } | |
1390 | ||
16780cab CM |
1391 | if (name[0] == '.' && |
1392 | (namelen == 1 || (name[1] == '.' && namelen == 2))) { | |
1393 | ret = -EEXIST; | |
1394 | goto out; | |
1395 | } | |
1396 | ||
3de4586c | 1397 | if (subvol) { |
72fd032e | 1398 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
b83cc969 | 1399 | NULL, transid, readonly); |
cb8e7090 | 1400 | } else { |
3de4586c | 1401 | struct inode *src_inode; |
72fd032e | 1402 | src_file = fget(fd); |
3de4586c CM |
1403 | if (!src_file) { |
1404 | ret = -EINVAL; | |
1405 | goto out; | |
1406 | } | |
1407 | ||
1408 | src_inode = src_file->f_path.dentry->d_inode; | |
1409 | if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) { | |
d397712b CM |
1410 | printk(KERN_INFO "btrfs: Snapshot src from " |
1411 | "another FS\n"); | |
3de4586c CM |
1412 | ret = -EINVAL; |
1413 | fput(src_file); | |
1414 | goto out; | |
1415 | } | |
72fd032e SW |
1416 | ret = btrfs_mksubvol(&file->f_path, name, namelen, |
1417 | BTRFS_I(src_inode)->root, | |
b83cc969 | 1418 | transid, readonly); |
3de4586c | 1419 | fput(src_file); |
cb8e7090 | 1420 | } |
f46b5a66 | 1421 | out: |
72fd032e SW |
1422 | return ret; |
1423 | } | |
1424 | ||
1425 | static noinline int btrfs_ioctl_snap_create(struct file *file, | |
fa0d2b9b | 1426 | void __user *arg, int subvol) |
72fd032e | 1427 | { |
fa0d2b9b | 1428 | struct btrfs_ioctl_vol_args *vol_args; |
72fd032e SW |
1429 | int ret; |
1430 | ||
fa0d2b9b LZ |
1431 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1432 | if (IS_ERR(vol_args)) | |
1433 | return PTR_ERR(vol_args); | |
1434 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
72fd032e | 1435 | |
fa0d2b9b | 1436 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, |
b83cc969 LZ |
1437 | vol_args->fd, subvol, |
1438 | NULL, false); | |
fdfb1e4f | 1439 | |
fa0d2b9b LZ |
1440 | kfree(vol_args); |
1441 | return ret; | |
1442 | } | |
fdfb1e4f | 1443 | |
fa0d2b9b LZ |
1444 | static noinline int btrfs_ioctl_snap_create_v2(struct file *file, |
1445 | void __user *arg, int subvol) | |
1446 | { | |
1447 | struct btrfs_ioctl_vol_args_v2 *vol_args; | |
1448 | int ret; | |
1449 | u64 transid = 0; | |
1450 | u64 *ptr = NULL; | |
b83cc969 | 1451 | bool readonly = false; |
75eaa0e2 | 1452 | |
fa0d2b9b LZ |
1453 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1454 | if (IS_ERR(vol_args)) | |
1455 | return PTR_ERR(vol_args); | |
1456 | vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0'; | |
75eaa0e2 | 1457 | |
b83cc969 LZ |
1458 | if (vol_args->flags & |
1459 | ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) { | |
1460 | ret = -EOPNOTSUPP; | |
fa0d2b9b | 1461 | goto out; |
72fd032e | 1462 | } |
fa0d2b9b LZ |
1463 | |
1464 | if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC) | |
1465 | ptr = &transid; | |
b83cc969 LZ |
1466 | if (vol_args->flags & BTRFS_SUBVOL_RDONLY) |
1467 | readonly = true; | |
fa0d2b9b LZ |
1468 | |
1469 | ret = btrfs_ioctl_snap_create_transid(file, vol_args->name, | |
b83cc969 LZ |
1470 | vol_args->fd, subvol, |
1471 | ptr, readonly); | |
fa0d2b9b LZ |
1472 | |
1473 | if (ret == 0 && ptr && | |
1474 | copy_to_user(arg + | |
1475 | offsetof(struct btrfs_ioctl_vol_args_v2, | |
1476 | transid), ptr, sizeof(*ptr))) | |
1477 | ret = -EFAULT; | |
fdfb1e4f | 1478 | out: |
f46b5a66 CH |
1479 | kfree(vol_args); |
1480 | return ret; | |
1481 | } | |
1482 | ||
0caa102d LZ |
1483 | static noinline int btrfs_ioctl_subvol_getflags(struct file *file, |
1484 | void __user *arg) | |
1485 | { | |
1486 | struct inode *inode = fdentry(file)->d_inode; | |
1487 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1488 | int ret = 0; | |
1489 | u64 flags = 0; | |
1490 | ||
33345d01 | 1491 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) |
0caa102d LZ |
1492 | return -EINVAL; |
1493 | ||
1494 | down_read(&root->fs_info->subvol_sem); | |
1495 | if (btrfs_root_readonly(root)) | |
1496 | flags |= BTRFS_SUBVOL_RDONLY; | |
1497 | up_read(&root->fs_info->subvol_sem); | |
1498 | ||
1499 | if (copy_to_user(arg, &flags, sizeof(flags))) | |
1500 | ret = -EFAULT; | |
1501 | ||
1502 | return ret; | |
1503 | } | |
1504 | ||
1505 | static noinline int btrfs_ioctl_subvol_setflags(struct file *file, | |
1506 | void __user *arg) | |
1507 | { | |
1508 | struct inode *inode = fdentry(file)->d_inode; | |
1509 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1510 | struct btrfs_trans_handle *trans; | |
1511 | u64 root_flags; | |
1512 | u64 flags; | |
1513 | int ret = 0; | |
1514 | ||
1515 | if (root->fs_info->sb->s_flags & MS_RDONLY) | |
1516 | return -EROFS; | |
1517 | ||
33345d01 | 1518 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) |
0caa102d LZ |
1519 | return -EINVAL; |
1520 | ||
1521 | if (copy_from_user(&flags, arg, sizeof(flags))) | |
1522 | return -EFAULT; | |
1523 | ||
b4dc2b8c | 1524 | if (flags & BTRFS_SUBVOL_CREATE_ASYNC) |
0caa102d LZ |
1525 | return -EINVAL; |
1526 | ||
1527 | if (flags & ~BTRFS_SUBVOL_RDONLY) | |
1528 | return -EOPNOTSUPP; | |
1529 | ||
2e149670 | 1530 | if (!inode_owner_or_capable(inode)) |
b4dc2b8c LZ |
1531 | return -EACCES; |
1532 | ||
0caa102d LZ |
1533 | down_write(&root->fs_info->subvol_sem); |
1534 | ||
1535 | /* nothing to do */ | |
1536 | if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root)) | |
1537 | goto out; | |
1538 | ||
1539 | root_flags = btrfs_root_flags(&root->root_item); | |
1540 | if (flags & BTRFS_SUBVOL_RDONLY) | |
1541 | btrfs_set_root_flags(&root->root_item, | |
1542 | root_flags | BTRFS_ROOT_SUBVOL_RDONLY); | |
1543 | else | |
1544 | btrfs_set_root_flags(&root->root_item, | |
1545 | root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY); | |
1546 | ||
1547 | trans = btrfs_start_transaction(root, 1); | |
1548 | if (IS_ERR(trans)) { | |
1549 | ret = PTR_ERR(trans); | |
1550 | goto out_reset; | |
1551 | } | |
1552 | ||
b4dc2b8c | 1553 | ret = btrfs_update_root(trans, root->fs_info->tree_root, |
0caa102d LZ |
1554 | &root->root_key, &root->root_item); |
1555 | ||
1556 | btrfs_commit_transaction(trans, root); | |
1557 | out_reset: | |
1558 | if (ret) | |
1559 | btrfs_set_root_flags(&root->root_item, root_flags); | |
1560 | out: | |
1561 | up_write(&root->fs_info->subvol_sem); | |
1562 | return ret; | |
1563 | } | |
1564 | ||
76dda93c YZ |
1565 | /* |
1566 | * helper to check if the subvolume references other subvolumes | |
1567 | */ | |
1568 | static noinline int may_destroy_subvol(struct btrfs_root *root) | |
1569 | { | |
1570 | struct btrfs_path *path; | |
1571 | struct btrfs_key key; | |
1572 | int ret; | |
1573 | ||
1574 | path = btrfs_alloc_path(); | |
1575 | if (!path) | |
1576 | return -ENOMEM; | |
1577 | ||
1578 | key.objectid = root->root_key.objectid; | |
1579 | key.type = BTRFS_ROOT_REF_KEY; | |
1580 | key.offset = (u64)-1; | |
1581 | ||
1582 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, | |
1583 | &key, path, 0, 0); | |
1584 | if (ret < 0) | |
1585 | goto out; | |
1586 | BUG_ON(ret == 0); | |
1587 | ||
1588 | ret = 0; | |
1589 | if (path->slots[0] > 0) { | |
1590 | path->slots[0]--; | |
1591 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
1592 | if (key.objectid == root->root_key.objectid && | |
1593 | key.type == BTRFS_ROOT_REF_KEY) | |
1594 | ret = -ENOTEMPTY; | |
1595 | } | |
1596 | out: | |
1597 | btrfs_free_path(path); | |
1598 | return ret; | |
1599 | } | |
1600 | ||
ac8e9819 CM |
1601 | static noinline int key_in_sk(struct btrfs_key *key, |
1602 | struct btrfs_ioctl_search_key *sk) | |
1603 | { | |
abc6e134 CM |
1604 | struct btrfs_key test; |
1605 | int ret; | |
1606 | ||
1607 | test.objectid = sk->min_objectid; | |
1608 | test.type = sk->min_type; | |
1609 | test.offset = sk->min_offset; | |
1610 | ||
1611 | ret = btrfs_comp_cpu_keys(key, &test); | |
1612 | if (ret < 0) | |
ac8e9819 | 1613 | return 0; |
abc6e134 CM |
1614 | |
1615 | test.objectid = sk->max_objectid; | |
1616 | test.type = sk->max_type; | |
1617 | test.offset = sk->max_offset; | |
1618 | ||
1619 | ret = btrfs_comp_cpu_keys(key, &test); | |
1620 | if (ret > 0) | |
ac8e9819 CM |
1621 | return 0; |
1622 | return 1; | |
1623 | } | |
1624 | ||
1625 | static noinline int copy_to_sk(struct btrfs_root *root, | |
1626 | struct btrfs_path *path, | |
1627 | struct btrfs_key *key, | |
1628 | struct btrfs_ioctl_search_key *sk, | |
1629 | char *buf, | |
1630 | unsigned long *sk_offset, | |
1631 | int *num_found) | |
1632 | { | |
1633 | u64 found_transid; | |
1634 | struct extent_buffer *leaf; | |
1635 | struct btrfs_ioctl_search_header sh; | |
1636 | unsigned long item_off; | |
1637 | unsigned long item_len; | |
1638 | int nritems; | |
1639 | int i; | |
1640 | int slot; | |
ac8e9819 CM |
1641 | int ret = 0; |
1642 | ||
1643 | leaf = path->nodes[0]; | |
1644 | slot = path->slots[0]; | |
1645 | nritems = btrfs_header_nritems(leaf); | |
1646 | ||
1647 | if (btrfs_header_generation(leaf) > sk->max_transid) { | |
1648 | i = nritems; | |
1649 | goto advance_key; | |
1650 | } | |
1651 | found_transid = btrfs_header_generation(leaf); | |
1652 | ||
1653 | for (i = slot; i < nritems; i++) { | |
1654 | item_off = btrfs_item_ptr_offset(leaf, i); | |
1655 | item_len = btrfs_item_size_nr(leaf, i); | |
1656 | ||
1657 | if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE) | |
1658 | item_len = 0; | |
1659 | ||
1660 | if (sizeof(sh) + item_len + *sk_offset > | |
1661 | BTRFS_SEARCH_ARGS_BUFSIZE) { | |
1662 | ret = 1; | |
1663 | goto overflow; | |
1664 | } | |
1665 | ||
1666 | btrfs_item_key_to_cpu(leaf, key, i); | |
1667 | if (!key_in_sk(key, sk)) | |
1668 | continue; | |
1669 | ||
1670 | sh.objectid = key->objectid; | |
1671 | sh.offset = key->offset; | |
1672 | sh.type = key->type; | |
1673 | sh.len = item_len; | |
1674 | sh.transid = found_transid; | |
1675 | ||
1676 | /* copy search result header */ | |
1677 | memcpy(buf + *sk_offset, &sh, sizeof(sh)); | |
1678 | *sk_offset += sizeof(sh); | |
1679 | ||
1680 | if (item_len) { | |
1681 | char *p = buf + *sk_offset; | |
1682 | /* copy the item */ | |
1683 | read_extent_buffer(leaf, p, | |
1684 | item_off, item_len); | |
1685 | *sk_offset += item_len; | |
ac8e9819 | 1686 | } |
e2156867 | 1687 | (*num_found)++; |
ac8e9819 CM |
1688 | |
1689 | if (*num_found >= sk->nr_items) | |
1690 | break; | |
1691 | } | |
1692 | advance_key: | |
abc6e134 CM |
1693 | ret = 0; |
1694 | if (key->offset < (u64)-1 && key->offset < sk->max_offset) | |
ac8e9819 | 1695 | key->offset++; |
abc6e134 CM |
1696 | else if (key->type < (u8)-1 && key->type < sk->max_type) { |
1697 | key->offset = 0; | |
ac8e9819 | 1698 | key->type++; |
abc6e134 CM |
1699 | } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) { |
1700 | key->offset = 0; | |
1701 | key->type = 0; | |
ac8e9819 | 1702 | key->objectid++; |
abc6e134 CM |
1703 | } else |
1704 | ret = 1; | |
ac8e9819 | 1705 | overflow: |
ac8e9819 CM |
1706 | return ret; |
1707 | } | |
1708 | ||
1709 | static noinline int search_ioctl(struct inode *inode, | |
1710 | struct btrfs_ioctl_search_args *args) | |
1711 | { | |
1712 | struct btrfs_root *root; | |
1713 | struct btrfs_key key; | |
1714 | struct btrfs_key max_key; | |
1715 | struct btrfs_path *path; | |
1716 | struct btrfs_ioctl_search_key *sk = &args->key; | |
1717 | struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info; | |
1718 | int ret; | |
1719 | int num_found = 0; | |
1720 | unsigned long sk_offset = 0; | |
1721 | ||
1722 | path = btrfs_alloc_path(); | |
1723 | if (!path) | |
1724 | return -ENOMEM; | |
1725 | ||
1726 | if (sk->tree_id == 0) { | |
1727 | /* search the root of the inode that was passed */ | |
1728 | root = BTRFS_I(inode)->root; | |
1729 | } else { | |
1730 | key.objectid = sk->tree_id; | |
1731 | key.type = BTRFS_ROOT_ITEM_KEY; | |
1732 | key.offset = (u64)-1; | |
1733 | root = btrfs_read_fs_root_no_name(info, &key); | |
1734 | if (IS_ERR(root)) { | |
1735 | printk(KERN_ERR "could not find root %llu\n", | |
1736 | sk->tree_id); | |
1737 | btrfs_free_path(path); | |
1738 | return -ENOENT; | |
1739 | } | |
1740 | } | |
1741 | ||
1742 | key.objectid = sk->min_objectid; | |
1743 | key.type = sk->min_type; | |
1744 | key.offset = sk->min_offset; | |
1745 | ||
1746 | max_key.objectid = sk->max_objectid; | |
1747 | max_key.type = sk->max_type; | |
1748 | max_key.offset = sk->max_offset; | |
1749 | ||
1750 | path->keep_locks = 1; | |
1751 | ||
1752 | while(1) { | |
1753 | ret = btrfs_search_forward(root, &key, &max_key, path, 0, | |
1754 | sk->min_transid); | |
1755 | if (ret != 0) { | |
1756 | if (ret > 0) | |
1757 | ret = 0; | |
1758 | goto err; | |
1759 | } | |
1760 | ret = copy_to_sk(root, path, &key, sk, args->buf, | |
1761 | &sk_offset, &num_found); | |
b3b4aa74 | 1762 | btrfs_release_path(path); |
ac8e9819 CM |
1763 | if (ret || num_found >= sk->nr_items) |
1764 | break; | |
1765 | ||
1766 | } | |
1767 | ret = 0; | |
1768 | err: | |
1769 | sk->nr_items = num_found; | |
1770 | btrfs_free_path(path); | |
1771 | return ret; | |
1772 | } | |
1773 | ||
1774 | static noinline int btrfs_ioctl_tree_search(struct file *file, | |
1775 | void __user *argp) | |
1776 | { | |
1777 | struct btrfs_ioctl_search_args *args; | |
1778 | struct inode *inode; | |
1779 | int ret; | |
1780 | ||
1781 | if (!capable(CAP_SYS_ADMIN)) | |
1782 | return -EPERM; | |
1783 | ||
2354d08f JL |
1784 | args = memdup_user(argp, sizeof(*args)); |
1785 | if (IS_ERR(args)) | |
1786 | return PTR_ERR(args); | |
ac8e9819 | 1787 | |
ac8e9819 CM |
1788 | inode = fdentry(file)->d_inode; |
1789 | ret = search_ioctl(inode, args); | |
1790 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) | |
1791 | ret = -EFAULT; | |
1792 | kfree(args); | |
1793 | return ret; | |
1794 | } | |
1795 | ||
98d377a0 | 1796 | /* |
ac8e9819 CM |
1797 | * Search INODE_REFs to identify path name of 'dirid' directory |
1798 | * in a 'tree_id' tree. and sets path name to 'name'. | |
1799 | */ | |
98d377a0 TH |
1800 | static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info, |
1801 | u64 tree_id, u64 dirid, char *name) | |
1802 | { | |
1803 | struct btrfs_root *root; | |
1804 | struct btrfs_key key; | |
ac8e9819 | 1805 | char *ptr; |
98d377a0 TH |
1806 | int ret = -1; |
1807 | int slot; | |
1808 | int len; | |
1809 | int total_len = 0; | |
1810 | struct btrfs_inode_ref *iref; | |
1811 | struct extent_buffer *l; | |
1812 | struct btrfs_path *path; | |
1813 | ||
1814 | if (dirid == BTRFS_FIRST_FREE_OBJECTID) { | |
1815 | name[0]='\0'; | |
1816 | return 0; | |
1817 | } | |
1818 | ||
1819 | path = btrfs_alloc_path(); | |
1820 | if (!path) | |
1821 | return -ENOMEM; | |
1822 | ||
ac8e9819 | 1823 | ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX]; |
98d377a0 TH |
1824 | |
1825 | key.objectid = tree_id; | |
1826 | key.type = BTRFS_ROOT_ITEM_KEY; | |
1827 | key.offset = (u64)-1; | |
1828 | root = btrfs_read_fs_root_no_name(info, &key); | |
1829 | if (IS_ERR(root)) { | |
1830 | printk(KERN_ERR "could not find root %llu\n", tree_id); | |
8ad6fcab CM |
1831 | ret = -ENOENT; |
1832 | goto out; | |
98d377a0 TH |
1833 | } |
1834 | ||
1835 | key.objectid = dirid; | |
1836 | key.type = BTRFS_INODE_REF_KEY; | |
8ad6fcab | 1837 | key.offset = (u64)-1; |
98d377a0 TH |
1838 | |
1839 | while(1) { | |
1840 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
1841 | if (ret < 0) | |
1842 | goto out; | |
1843 | ||
1844 | l = path->nodes[0]; | |
1845 | slot = path->slots[0]; | |
8ad6fcab CM |
1846 | if (ret > 0 && slot > 0) |
1847 | slot--; | |
98d377a0 TH |
1848 | btrfs_item_key_to_cpu(l, &key, slot); |
1849 | ||
1850 | if (ret > 0 && (key.objectid != dirid || | |
ac8e9819 CM |
1851 | key.type != BTRFS_INODE_REF_KEY)) { |
1852 | ret = -ENOENT; | |
98d377a0 | 1853 | goto out; |
ac8e9819 | 1854 | } |
98d377a0 TH |
1855 | |
1856 | iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref); | |
1857 | len = btrfs_inode_ref_name_len(l, iref); | |
1858 | ptr -= len + 1; | |
1859 | total_len += len + 1; | |
ac8e9819 | 1860 | if (ptr < name) |
98d377a0 TH |
1861 | goto out; |
1862 | ||
1863 | *(ptr + len) = '/'; | |
1864 | read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len); | |
1865 | ||
1866 | if (key.offset == BTRFS_FIRST_FREE_OBJECTID) | |
1867 | break; | |
1868 | ||
b3b4aa74 | 1869 | btrfs_release_path(path); |
98d377a0 | 1870 | key.objectid = key.offset; |
8ad6fcab | 1871 | key.offset = (u64)-1; |
98d377a0 | 1872 | dirid = key.objectid; |
98d377a0 | 1873 | } |
ac8e9819 | 1874 | if (ptr < name) |
98d377a0 | 1875 | goto out; |
77906a50 | 1876 | memmove(name, ptr, total_len); |
98d377a0 TH |
1877 | name[total_len]='\0'; |
1878 | ret = 0; | |
1879 | out: | |
1880 | btrfs_free_path(path); | |
ac8e9819 CM |
1881 | return ret; |
1882 | } | |
1883 | ||
1884 | static noinline int btrfs_ioctl_ino_lookup(struct file *file, | |
1885 | void __user *argp) | |
1886 | { | |
1887 | struct btrfs_ioctl_ino_lookup_args *args; | |
1888 | struct inode *inode; | |
1889 | int ret; | |
1890 | ||
1891 | if (!capable(CAP_SYS_ADMIN)) | |
1892 | return -EPERM; | |
1893 | ||
2354d08f JL |
1894 | args = memdup_user(argp, sizeof(*args)); |
1895 | if (IS_ERR(args)) | |
1896 | return PTR_ERR(args); | |
c2b96929 | 1897 | |
ac8e9819 CM |
1898 | inode = fdentry(file)->d_inode; |
1899 | ||
1b53ac4d CM |
1900 | if (args->treeid == 0) |
1901 | args->treeid = BTRFS_I(inode)->root->root_key.objectid; | |
1902 | ||
ac8e9819 CM |
1903 | ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info, |
1904 | args->treeid, args->objectid, | |
1905 | args->name); | |
1906 | ||
1907 | if (ret == 0 && copy_to_user(argp, args, sizeof(*args))) | |
1908 | ret = -EFAULT; | |
1909 | ||
1910 | kfree(args); | |
98d377a0 TH |
1911 | return ret; |
1912 | } | |
1913 | ||
76dda93c YZ |
1914 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, |
1915 | void __user *arg) | |
1916 | { | |
1917 | struct dentry *parent = fdentry(file); | |
1918 | struct dentry *dentry; | |
1919 | struct inode *dir = parent->d_inode; | |
1920 | struct inode *inode; | |
1921 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1922 | struct btrfs_root *dest = NULL; | |
1923 | struct btrfs_ioctl_vol_args *vol_args; | |
1924 | struct btrfs_trans_handle *trans; | |
1925 | int namelen; | |
1926 | int ret; | |
1927 | int err = 0; | |
1928 | ||
76dda93c YZ |
1929 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
1930 | if (IS_ERR(vol_args)) | |
1931 | return PTR_ERR(vol_args); | |
1932 | ||
1933 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
1934 | namelen = strlen(vol_args->name); | |
1935 | if (strchr(vol_args->name, '/') || | |
1936 | strncmp(vol_args->name, "..", namelen) == 0) { | |
1937 | err = -EINVAL; | |
1938 | goto out; | |
1939 | } | |
1940 | ||
a561be71 | 1941 | err = mnt_want_write_file(file); |
76dda93c YZ |
1942 | if (err) |
1943 | goto out; | |
1944 | ||
1945 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); | |
1946 | dentry = lookup_one_len(vol_args->name, parent, namelen); | |
1947 | if (IS_ERR(dentry)) { | |
1948 | err = PTR_ERR(dentry); | |
1949 | goto out_unlock_dir; | |
1950 | } | |
1951 | ||
1952 | if (!dentry->d_inode) { | |
1953 | err = -ENOENT; | |
1954 | goto out_dput; | |
1955 | } | |
1956 | ||
1957 | inode = dentry->d_inode; | |
4260f7c7 SW |
1958 | dest = BTRFS_I(inode)->root; |
1959 | if (!capable(CAP_SYS_ADMIN)){ | |
1960 | /* | |
1961 | * Regular user. Only allow this with a special mount | |
1962 | * option, when the user has write+exec access to the | |
1963 | * subvol root, and when rmdir(2) would have been | |
1964 | * allowed. | |
1965 | * | |
1966 | * Note that this is _not_ check that the subvol is | |
1967 | * empty or doesn't contain data that we wouldn't | |
1968 | * otherwise be able to delete. | |
1969 | * | |
1970 | * Users who want to delete empty subvols should try | |
1971 | * rmdir(2). | |
1972 | */ | |
1973 | err = -EPERM; | |
1974 | if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED)) | |
1975 | goto out_dput; | |
1976 | ||
1977 | /* | |
1978 | * Do not allow deletion if the parent dir is the same | |
1979 | * as the dir to be deleted. That means the ioctl | |
1980 | * must be called on the dentry referencing the root | |
1981 | * of the subvol, not a random directory contained | |
1982 | * within it. | |
1983 | */ | |
1984 | err = -EINVAL; | |
1985 | if (root == dest) | |
1986 | goto out_dput; | |
1987 | ||
1988 | err = inode_permission(inode, MAY_WRITE | MAY_EXEC); | |
1989 | if (err) | |
1990 | goto out_dput; | |
1991 | ||
1992 | /* check if subvolume may be deleted by a non-root user */ | |
1993 | err = btrfs_may_delete(dir, dentry, 1); | |
1994 | if (err) | |
1995 | goto out_dput; | |
1996 | } | |
1997 | ||
33345d01 | 1998 | if (btrfs_ino(inode) != BTRFS_FIRST_FREE_OBJECTID) { |
76dda93c YZ |
1999 | err = -EINVAL; |
2000 | goto out_dput; | |
2001 | } | |
2002 | ||
76dda93c YZ |
2003 | mutex_lock(&inode->i_mutex); |
2004 | err = d_invalidate(dentry); | |
2005 | if (err) | |
2006 | goto out_unlock; | |
2007 | ||
2008 | down_write(&root->fs_info->subvol_sem); | |
2009 | ||
2010 | err = may_destroy_subvol(dest); | |
2011 | if (err) | |
2012 | goto out_up_write; | |
2013 | ||
a22285a6 YZ |
2014 | trans = btrfs_start_transaction(root, 0); |
2015 | if (IS_ERR(trans)) { | |
2016 | err = PTR_ERR(trans); | |
d327099a | 2017 | goto out_up_write; |
a22285a6 YZ |
2018 | } |
2019 | trans->block_rsv = &root->fs_info->global_block_rsv; | |
2020 | ||
76dda93c YZ |
2021 | ret = btrfs_unlink_subvol(trans, root, dir, |
2022 | dest->root_key.objectid, | |
2023 | dentry->d_name.name, | |
2024 | dentry->d_name.len); | |
79787eaa JM |
2025 | if (ret) { |
2026 | err = ret; | |
2027 | btrfs_abort_transaction(trans, root, ret); | |
2028 | goto out_end_trans; | |
2029 | } | |
76dda93c YZ |
2030 | |
2031 | btrfs_record_root_in_trans(trans, dest); | |
2032 | ||
2033 | memset(&dest->root_item.drop_progress, 0, | |
2034 | sizeof(dest->root_item.drop_progress)); | |
2035 | dest->root_item.drop_level = 0; | |
2036 | btrfs_set_root_refs(&dest->root_item, 0); | |
2037 | ||
d68fc57b YZ |
2038 | if (!xchg(&dest->orphan_item_inserted, 1)) { |
2039 | ret = btrfs_insert_orphan_item(trans, | |
2040 | root->fs_info->tree_root, | |
2041 | dest->root_key.objectid); | |
79787eaa JM |
2042 | if (ret) { |
2043 | btrfs_abort_transaction(trans, root, ret); | |
2044 | err = ret; | |
2045 | goto out_end_trans; | |
2046 | } | |
d68fc57b | 2047 | } |
79787eaa | 2048 | out_end_trans: |
531cb13f | 2049 | ret = btrfs_end_transaction(trans, root); |
79787eaa JM |
2050 | if (ret && !err) |
2051 | err = ret; | |
76dda93c YZ |
2052 | inode->i_flags |= S_DEAD; |
2053 | out_up_write: | |
2054 | up_write(&root->fs_info->subvol_sem); | |
2055 | out_unlock: | |
2056 | mutex_unlock(&inode->i_mutex); | |
2057 | if (!err) { | |
efefb143 | 2058 | shrink_dcache_sb(root->fs_info->sb); |
76dda93c YZ |
2059 | btrfs_invalidate_inodes(dest); |
2060 | d_delete(dentry); | |
2061 | } | |
2062 | out_dput: | |
2063 | dput(dentry); | |
2064 | out_unlock_dir: | |
2065 | mutex_unlock(&dir->i_mutex); | |
2a79f17e | 2066 | mnt_drop_write_file(file); |
76dda93c YZ |
2067 | out: |
2068 | kfree(vol_args); | |
2069 | return err; | |
2070 | } | |
2071 | ||
1e701a32 | 2072 | static int btrfs_ioctl_defrag(struct file *file, void __user *argp) |
f46b5a66 CH |
2073 | { |
2074 | struct inode *inode = fdentry(file)->d_inode; | |
2075 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1e701a32 | 2076 | struct btrfs_ioctl_defrag_range_args *range; |
c146afad YZ |
2077 | int ret; |
2078 | ||
b83cc969 LZ |
2079 | if (btrfs_root_readonly(root)) |
2080 | return -EROFS; | |
2081 | ||
a561be71 | 2082 | ret = mnt_want_write_file(file); |
c146afad YZ |
2083 | if (ret) |
2084 | return ret; | |
f46b5a66 CH |
2085 | |
2086 | switch (inode->i_mode & S_IFMT) { | |
2087 | case S_IFDIR: | |
e441d54d CM |
2088 | if (!capable(CAP_SYS_ADMIN)) { |
2089 | ret = -EPERM; | |
2090 | goto out; | |
2091 | } | |
8929ecfa YZ |
2092 | ret = btrfs_defrag_root(root, 0); |
2093 | if (ret) | |
2094 | goto out; | |
2095 | ret = btrfs_defrag_root(root->fs_info->extent_root, 0); | |
f46b5a66 CH |
2096 | break; |
2097 | case S_IFREG: | |
e441d54d CM |
2098 | if (!(file->f_mode & FMODE_WRITE)) { |
2099 | ret = -EINVAL; | |
2100 | goto out; | |
2101 | } | |
1e701a32 CM |
2102 | |
2103 | range = kzalloc(sizeof(*range), GFP_KERNEL); | |
2104 | if (!range) { | |
2105 | ret = -ENOMEM; | |
2106 | goto out; | |
2107 | } | |
2108 | ||
2109 | if (argp) { | |
2110 | if (copy_from_user(range, argp, | |
2111 | sizeof(*range))) { | |
2112 | ret = -EFAULT; | |
2113 | kfree(range); | |
683be16e | 2114 | goto out; |
1e701a32 CM |
2115 | } |
2116 | /* compression requires us to start the IO */ | |
2117 | if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) { | |
2118 | range->flags |= BTRFS_DEFRAG_RANGE_START_IO; | |
2119 | range->extent_thresh = (u32)-1; | |
2120 | } | |
2121 | } else { | |
2122 | /* the rest are all set to zero by kzalloc */ | |
2123 | range->len = (u64)-1; | |
2124 | } | |
4cb5300b CM |
2125 | ret = btrfs_defrag_file(fdentry(file)->d_inode, file, |
2126 | range, 0, 0); | |
2127 | if (ret > 0) | |
2128 | ret = 0; | |
1e701a32 | 2129 | kfree(range); |
f46b5a66 | 2130 | break; |
8929ecfa YZ |
2131 | default: |
2132 | ret = -EINVAL; | |
f46b5a66 | 2133 | } |
e441d54d | 2134 | out: |
2a79f17e | 2135 | mnt_drop_write_file(file); |
e441d54d | 2136 | return ret; |
f46b5a66 CH |
2137 | } |
2138 | ||
b2950863 | 2139 | static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) |
f46b5a66 CH |
2140 | { |
2141 | struct btrfs_ioctl_vol_args *vol_args; | |
2142 | int ret; | |
2143 | ||
e441d54d CM |
2144 | if (!capable(CAP_SYS_ADMIN)) |
2145 | return -EPERM; | |
2146 | ||
c9e9f97b ID |
2147 | mutex_lock(&root->fs_info->volume_mutex); |
2148 | if (root->fs_info->balance_ctl) { | |
2149 | printk(KERN_INFO "btrfs: balance in progress\n"); | |
2150 | ret = -EINVAL; | |
2151 | goto out; | |
2152 | } | |
2153 | ||
dae7b665 | 2154 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
2155 | if (IS_ERR(vol_args)) { |
2156 | ret = PTR_ERR(vol_args); | |
2157 | goto out; | |
2158 | } | |
f46b5a66 | 2159 | |
5516e595 | 2160 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
f46b5a66 CH |
2161 | ret = btrfs_init_new_device(root, vol_args->name); |
2162 | ||
f46b5a66 | 2163 | kfree(vol_args); |
c9e9f97b ID |
2164 | out: |
2165 | mutex_unlock(&root->fs_info->volume_mutex); | |
f46b5a66 CH |
2166 | return ret; |
2167 | } | |
2168 | ||
b2950863 | 2169 | static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) |
f46b5a66 CH |
2170 | { |
2171 | struct btrfs_ioctl_vol_args *vol_args; | |
2172 | int ret; | |
2173 | ||
e441d54d CM |
2174 | if (!capable(CAP_SYS_ADMIN)) |
2175 | return -EPERM; | |
2176 | ||
c146afad YZ |
2177 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
2178 | return -EROFS; | |
2179 | ||
c9e9f97b ID |
2180 | mutex_lock(&root->fs_info->volume_mutex); |
2181 | if (root->fs_info->balance_ctl) { | |
2182 | printk(KERN_INFO "btrfs: balance in progress\n"); | |
2183 | ret = -EINVAL; | |
2184 | goto out; | |
2185 | } | |
2186 | ||
dae7b665 | 2187 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
c9e9f97b ID |
2188 | if (IS_ERR(vol_args)) { |
2189 | ret = PTR_ERR(vol_args); | |
2190 | goto out; | |
2191 | } | |
f46b5a66 | 2192 | |
5516e595 | 2193 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
f46b5a66 CH |
2194 | ret = btrfs_rm_device(root, vol_args->name); |
2195 | ||
f46b5a66 | 2196 | kfree(vol_args); |
c9e9f97b ID |
2197 | out: |
2198 | mutex_unlock(&root->fs_info->volume_mutex); | |
f46b5a66 CH |
2199 | return ret; |
2200 | } | |
2201 | ||
475f6387 JS |
2202 | static long btrfs_ioctl_fs_info(struct btrfs_root *root, void __user *arg) |
2203 | { | |
027ed2f0 | 2204 | struct btrfs_ioctl_fs_info_args *fi_args; |
475f6387 JS |
2205 | struct btrfs_device *device; |
2206 | struct btrfs_device *next; | |
2207 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | |
027ed2f0 | 2208 | int ret = 0; |
475f6387 JS |
2209 | |
2210 | if (!capable(CAP_SYS_ADMIN)) | |
2211 | return -EPERM; | |
2212 | ||
027ed2f0 LZ |
2213 | fi_args = kzalloc(sizeof(*fi_args), GFP_KERNEL); |
2214 | if (!fi_args) | |
2215 | return -ENOMEM; | |
2216 | ||
2217 | fi_args->num_devices = fs_devices->num_devices; | |
2218 | memcpy(&fi_args->fsid, root->fs_info->fsid, sizeof(fi_args->fsid)); | |
475f6387 JS |
2219 | |
2220 | mutex_lock(&fs_devices->device_list_mutex); | |
2221 | list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) { | |
027ed2f0 LZ |
2222 | if (device->devid > fi_args->max_id) |
2223 | fi_args->max_id = device->devid; | |
475f6387 JS |
2224 | } |
2225 | mutex_unlock(&fs_devices->device_list_mutex); | |
2226 | ||
027ed2f0 LZ |
2227 | if (copy_to_user(arg, fi_args, sizeof(*fi_args))) |
2228 | ret = -EFAULT; | |
475f6387 | 2229 | |
027ed2f0 LZ |
2230 | kfree(fi_args); |
2231 | return ret; | |
475f6387 JS |
2232 | } |
2233 | ||
2234 | static long btrfs_ioctl_dev_info(struct btrfs_root *root, void __user *arg) | |
2235 | { | |
2236 | struct btrfs_ioctl_dev_info_args *di_args; | |
2237 | struct btrfs_device *dev; | |
2238 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | |
2239 | int ret = 0; | |
2240 | char *s_uuid = NULL; | |
2241 | char empty_uuid[BTRFS_UUID_SIZE] = {0}; | |
2242 | ||
2243 | if (!capable(CAP_SYS_ADMIN)) | |
2244 | return -EPERM; | |
2245 | ||
2246 | di_args = memdup_user(arg, sizeof(*di_args)); | |
2247 | if (IS_ERR(di_args)) | |
2248 | return PTR_ERR(di_args); | |
2249 | ||
2250 | if (memcmp(empty_uuid, di_args->uuid, BTRFS_UUID_SIZE) != 0) | |
2251 | s_uuid = di_args->uuid; | |
2252 | ||
2253 | mutex_lock(&fs_devices->device_list_mutex); | |
2254 | dev = btrfs_find_device(root, di_args->devid, s_uuid, NULL); | |
2255 | mutex_unlock(&fs_devices->device_list_mutex); | |
2256 | ||
2257 | if (!dev) { | |
2258 | ret = -ENODEV; | |
2259 | goto out; | |
2260 | } | |
2261 | ||
2262 | di_args->devid = dev->devid; | |
2263 | di_args->bytes_used = dev->bytes_used; | |
2264 | di_args->total_bytes = dev->total_bytes; | |
2265 | memcpy(di_args->uuid, dev->uuid, sizeof(di_args->uuid)); | |
99ba55ad SB |
2266 | if (dev->name) |
2267 | strncpy(di_args->path, dev->name, sizeof(di_args->path)); | |
2268 | else | |
2269 | di_args->path[0] = '\0'; | |
475f6387 JS |
2270 | |
2271 | out: | |
2272 | if (ret == 0 && copy_to_user(arg, di_args, sizeof(*di_args))) | |
2273 | ret = -EFAULT; | |
2274 | ||
2275 | kfree(di_args); | |
2276 | return ret; | |
2277 | } | |
2278 | ||
76dda93c YZ |
2279 | static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, |
2280 | u64 off, u64 olen, u64 destoff) | |
f46b5a66 CH |
2281 | { |
2282 | struct inode *inode = fdentry(file)->d_inode; | |
2283 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2284 | struct file *src_file; | |
2285 | struct inode *src; | |
2286 | struct btrfs_trans_handle *trans; | |
f46b5a66 | 2287 | struct btrfs_path *path; |
f46b5a66 | 2288 | struct extent_buffer *leaf; |
ae01a0ab YZ |
2289 | char *buf; |
2290 | struct btrfs_key key; | |
f46b5a66 CH |
2291 | u32 nritems; |
2292 | int slot; | |
ae01a0ab | 2293 | int ret; |
c5c9cd4d SW |
2294 | u64 len = olen; |
2295 | u64 bs = root->fs_info->sb->s_blocksize; | |
2296 | u64 hint_byte; | |
d20f7043 | 2297 | |
c5c9cd4d SW |
2298 | /* |
2299 | * TODO: | |
2300 | * - split compressed inline extents. annoying: we need to | |
2301 | * decompress into destination's address_space (the file offset | |
2302 | * may change, so source mapping won't do), then recompress (or | |
2303 | * otherwise reinsert) a subrange. | |
2304 | * - allow ranges within the same file to be cloned (provided | |
2305 | * they don't overlap)? | |
2306 | */ | |
2307 | ||
e441d54d | 2308 | /* the destination must be opened for writing */ |
2ebc3464 | 2309 | if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND)) |
e441d54d CM |
2310 | return -EINVAL; |
2311 | ||
b83cc969 LZ |
2312 | if (btrfs_root_readonly(root)) |
2313 | return -EROFS; | |
2314 | ||
a561be71 | 2315 | ret = mnt_want_write_file(file); |
c146afad YZ |
2316 | if (ret) |
2317 | return ret; | |
2318 | ||
c5c9cd4d | 2319 | src_file = fget(srcfd); |
ab67b7c1 YZ |
2320 | if (!src_file) { |
2321 | ret = -EBADF; | |
2322 | goto out_drop_write; | |
2323 | } | |
5dc64164 | 2324 | |
f46b5a66 CH |
2325 | src = src_file->f_dentry->d_inode; |
2326 | ||
c5c9cd4d SW |
2327 | ret = -EINVAL; |
2328 | if (src == inode) | |
2329 | goto out_fput; | |
2330 | ||
5dc64164 DR |
2331 | /* the src must be open for reading */ |
2332 | if (!(src_file->f_mode & FMODE_READ)) | |
2333 | goto out_fput; | |
2334 | ||
0e7b824c LZ |
2335 | /* don't make the dst file partly checksummed */ |
2336 | if ((BTRFS_I(src)->flags & BTRFS_INODE_NODATASUM) != | |
2337 | (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) | |
2338 | goto out_fput; | |
2339 | ||
ae01a0ab YZ |
2340 | ret = -EISDIR; |
2341 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) | |
2342 | goto out_fput; | |
2343 | ||
f46b5a66 | 2344 | ret = -EXDEV; |
ae01a0ab YZ |
2345 | if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) |
2346 | goto out_fput; | |
2347 | ||
2348 | ret = -ENOMEM; | |
2349 | buf = vmalloc(btrfs_level_size(root, 0)); | |
2350 | if (!buf) | |
2351 | goto out_fput; | |
2352 | ||
2353 | path = btrfs_alloc_path(); | |
2354 | if (!path) { | |
2355 | vfree(buf); | |
f46b5a66 | 2356 | goto out_fput; |
ae01a0ab YZ |
2357 | } |
2358 | path->reada = 2; | |
f46b5a66 CH |
2359 | |
2360 | if (inode < src) { | |
fccdae43 SW |
2361 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT); |
2362 | mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD); | |
f46b5a66 | 2363 | } else { |
fccdae43 SW |
2364 | mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT); |
2365 | mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD); | |
f46b5a66 CH |
2366 | } |
2367 | ||
c5c9cd4d SW |
2368 | /* determine range to clone */ |
2369 | ret = -EINVAL; | |
2ebc3464 | 2370 | if (off + len > src->i_size || off + len < off) |
f46b5a66 | 2371 | goto out_unlock; |
c5c9cd4d SW |
2372 | if (len == 0) |
2373 | olen = len = src->i_size - off; | |
2374 | /* if we extend to eof, continue to block boundary */ | |
2375 | if (off + len == src->i_size) | |
2a6b8dae | 2376 | len = ALIGN(src->i_size, bs) - off; |
c5c9cd4d SW |
2377 | |
2378 | /* verify the end result is block aligned */ | |
2a6b8dae LZ |
2379 | if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) || |
2380 | !IS_ALIGNED(destoff, bs)) | |
c5c9cd4d SW |
2381 | goto out_unlock; |
2382 | ||
d525e8ab LZ |
2383 | if (destoff > inode->i_size) { |
2384 | ret = btrfs_cont_expand(inode, inode->i_size, destoff); | |
2385 | if (ret) | |
2386 | goto out_unlock; | |
2387 | } | |
2388 | ||
71ef0786 LZ |
2389 | /* truncate page cache pages from target inode range */ |
2390 | truncate_inode_pages_range(&inode->i_data, destoff, | |
2391 | PAGE_CACHE_ALIGN(destoff + len) - 1); | |
2392 | ||
f46b5a66 CH |
2393 | /* do any pending delalloc/csum calc on src, one way or |
2394 | another, and lock file content */ | |
2395 | while (1) { | |
31840ae1 | 2396 | struct btrfs_ordered_extent *ordered; |
d0082371 | 2397 | lock_extent(&BTRFS_I(src)->io_tree, off, off+len); |
9a019196 SW |
2398 | ordered = btrfs_lookup_first_ordered_extent(src, off+len); |
2399 | if (!ordered && | |
2400 | !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len, | |
2401 | EXTENT_DELALLOC, 0, NULL)) | |
f46b5a66 | 2402 | break; |
d0082371 | 2403 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len); |
ae01a0ab YZ |
2404 | if (ordered) |
2405 | btrfs_put_ordered_extent(ordered); | |
9a019196 | 2406 | btrfs_wait_ordered_range(src, off, len); |
f46b5a66 CH |
2407 | } |
2408 | ||
c5c9cd4d | 2409 | /* clone data */ |
33345d01 | 2410 | key.objectid = btrfs_ino(src); |
ae01a0ab YZ |
2411 | key.type = BTRFS_EXTENT_DATA_KEY; |
2412 | key.offset = 0; | |
f46b5a66 CH |
2413 | |
2414 | while (1) { | |
2415 | /* | |
2416 | * note the key will change type as we walk through the | |
2417 | * tree. | |
2418 | */ | |
a22285a6 | 2419 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
f46b5a66 CH |
2420 | if (ret < 0) |
2421 | goto out; | |
2422 | ||
ae01a0ab YZ |
2423 | nritems = btrfs_header_nritems(path->nodes[0]); |
2424 | if (path->slots[0] >= nritems) { | |
f46b5a66 CH |
2425 | ret = btrfs_next_leaf(root, path); |
2426 | if (ret < 0) | |
2427 | goto out; | |
2428 | if (ret > 0) | |
2429 | break; | |
ae01a0ab | 2430 | nritems = btrfs_header_nritems(path->nodes[0]); |
f46b5a66 CH |
2431 | } |
2432 | leaf = path->nodes[0]; | |
2433 | slot = path->slots[0]; | |
f46b5a66 | 2434 | |
ae01a0ab | 2435 | btrfs_item_key_to_cpu(leaf, &key, slot); |
d20f7043 | 2436 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
33345d01 | 2437 | key.objectid != btrfs_ino(src)) |
f46b5a66 CH |
2438 | break; |
2439 | ||
c5c9cd4d SW |
2440 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
2441 | struct btrfs_file_extent_item *extent; | |
2442 | int type; | |
31840ae1 ZY |
2443 | u32 size; |
2444 | struct btrfs_key new_key; | |
c5c9cd4d SW |
2445 | u64 disko = 0, diskl = 0; |
2446 | u64 datao = 0, datal = 0; | |
2447 | u8 comp; | |
b5384d48 | 2448 | u64 endoff; |
31840ae1 ZY |
2449 | |
2450 | size = btrfs_item_size_nr(leaf, slot); | |
2451 | read_extent_buffer(leaf, buf, | |
2452 | btrfs_item_ptr_offset(leaf, slot), | |
2453 | size); | |
c5c9cd4d SW |
2454 | |
2455 | extent = btrfs_item_ptr(leaf, slot, | |
2456 | struct btrfs_file_extent_item); | |
2457 | comp = btrfs_file_extent_compression(leaf, extent); | |
2458 | type = btrfs_file_extent_type(leaf, extent); | |
c8a894d7 CM |
2459 | if (type == BTRFS_FILE_EXTENT_REG || |
2460 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d397712b CM |
2461 | disko = btrfs_file_extent_disk_bytenr(leaf, |
2462 | extent); | |
2463 | diskl = btrfs_file_extent_disk_num_bytes(leaf, | |
2464 | extent); | |
c5c9cd4d | 2465 | datao = btrfs_file_extent_offset(leaf, extent); |
d397712b CM |
2466 | datal = btrfs_file_extent_num_bytes(leaf, |
2467 | extent); | |
c5c9cd4d SW |
2468 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
2469 | /* take upper bound, may be compressed */ | |
2470 | datal = btrfs_file_extent_ram_bytes(leaf, | |
2471 | extent); | |
2472 | } | |
b3b4aa74 | 2473 | btrfs_release_path(path); |
31840ae1 | 2474 | |
050006a7 | 2475 | if (key.offset + datal <= off || |
c5c9cd4d SW |
2476 | key.offset >= off+len) |
2477 | goto next; | |
2478 | ||
31840ae1 | 2479 | memcpy(&new_key, &key, sizeof(new_key)); |
33345d01 | 2480 | new_key.objectid = btrfs_ino(inode); |
4d728ec7 LZ |
2481 | if (off <= key.offset) |
2482 | new_key.offset = key.offset + destoff - off; | |
2483 | else | |
2484 | new_key.offset = destoff; | |
31840ae1 | 2485 | |
b6f3409b SW |
2486 | /* |
2487 | * 1 - adjusting old extent (we may have to split it) | |
2488 | * 1 - add new extent | |
2489 | * 1 - inode update | |
2490 | */ | |
2491 | trans = btrfs_start_transaction(root, 3); | |
a22285a6 YZ |
2492 | if (IS_ERR(trans)) { |
2493 | ret = PTR_ERR(trans); | |
2494 | goto out; | |
2495 | } | |
2496 | ||
c8a894d7 CM |
2497 | if (type == BTRFS_FILE_EXTENT_REG || |
2498 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d72c0842 LZ |
2499 | /* |
2500 | * a | --- range to clone ---| b | |
2501 | * | ------------- extent ------------- | | |
2502 | */ | |
2503 | ||
2504 | /* substract range b */ | |
2505 | if (key.offset + datal > off + len) | |
2506 | datal = off + len - key.offset; | |
2507 | ||
2508 | /* substract range a */ | |
a22285a6 YZ |
2509 | if (off > key.offset) { |
2510 | datao += off - key.offset; | |
2511 | datal -= off - key.offset; | |
2512 | } | |
2513 | ||
a22285a6 YZ |
2514 | ret = btrfs_drop_extents(trans, inode, |
2515 | new_key.offset, | |
2516 | new_key.offset + datal, | |
2517 | &hint_byte, 1); | |
79787eaa JM |
2518 | if (ret) { |
2519 | btrfs_abort_transaction(trans, root, | |
2520 | ret); | |
2521 | btrfs_end_transaction(trans, root); | |
2522 | goto out; | |
2523 | } | |
a22285a6 | 2524 | |
c5c9cd4d SW |
2525 | ret = btrfs_insert_empty_item(trans, root, path, |
2526 | &new_key, size); | |
79787eaa JM |
2527 | if (ret) { |
2528 | btrfs_abort_transaction(trans, root, | |
2529 | ret); | |
2530 | btrfs_end_transaction(trans, root); | |
2531 | goto out; | |
2532 | } | |
c5c9cd4d SW |
2533 | |
2534 | leaf = path->nodes[0]; | |
2535 | slot = path->slots[0]; | |
2536 | write_extent_buffer(leaf, buf, | |
31840ae1 ZY |
2537 | btrfs_item_ptr_offset(leaf, slot), |
2538 | size); | |
ae01a0ab | 2539 | |
c5c9cd4d | 2540 | extent = btrfs_item_ptr(leaf, slot, |
f46b5a66 | 2541 | struct btrfs_file_extent_item); |
c5c9cd4d | 2542 | |
c5c9cd4d SW |
2543 | /* disko == 0 means it's a hole */ |
2544 | if (!disko) | |
2545 | datao = 0; | |
c5c9cd4d SW |
2546 | |
2547 | btrfs_set_file_extent_offset(leaf, extent, | |
2548 | datao); | |
2549 | btrfs_set_file_extent_num_bytes(leaf, extent, | |
2550 | datal); | |
2551 | if (disko) { | |
2552 | inode_add_bytes(inode, datal); | |
ae01a0ab | 2553 | ret = btrfs_inc_extent_ref(trans, root, |
5d4f98a2 YZ |
2554 | disko, diskl, 0, |
2555 | root->root_key.objectid, | |
33345d01 | 2556 | btrfs_ino(inode), |
66d7e7f0 AJ |
2557 | new_key.offset - datao, |
2558 | 0); | |
79787eaa JM |
2559 | if (ret) { |
2560 | btrfs_abort_transaction(trans, | |
2561 | root, | |
2562 | ret); | |
2563 | btrfs_end_transaction(trans, | |
2564 | root); | |
2565 | goto out; | |
2566 | ||
2567 | } | |
f46b5a66 | 2568 | } |
c5c9cd4d SW |
2569 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
2570 | u64 skip = 0; | |
2571 | u64 trim = 0; | |
2572 | if (off > key.offset) { | |
2573 | skip = off - key.offset; | |
2574 | new_key.offset += skip; | |
2575 | } | |
d397712b | 2576 | |
c5c9cd4d SW |
2577 | if (key.offset + datal > off+len) |
2578 | trim = key.offset + datal - (off+len); | |
d397712b | 2579 | |
c5c9cd4d | 2580 | if (comp && (skip || trim)) { |
c5c9cd4d | 2581 | ret = -EINVAL; |
a22285a6 | 2582 | btrfs_end_transaction(trans, root); |
c5c9cd4d SW |
2583 | goto out; |
2584 | } | |
2585 | size -= skip + trim; | |
2586 | datal -= skip + trim; | |
a22285a6 YZ |
2587 | |
2588 | ret = btrfs_drop_extents(trans, inode, | |
2589 | new_key.offset, | |
2590 | new_key.offset + datal, | |
2591 | &hint_byte, 1); | |
79787eaa JM |
2592 | if (ret) { |
2593 | btrfs_abort_transaction(trans, root, | |
2594 | ret); | |
2595 | btrfs_end_transaction(trans, root); | |
2596 | goto out; | |
2597 | } | |
a22285a6 | 2598 | |
c5c9cd4d SW |
2599 | ret = btrfs_insert_empty_item(trans, root, path, |
2600 | &new_key, size); | |
79787eaa JM |
2601 | if (ret) { |
2602 | btrfs_abort_transaction(trans, root, | |
2603 | ret); | |
2604 | btrfs_end_transaction(trans, root); | |
2605 | goto out; | |
2606 | } | |
c5c9cd4d SW |
2607 | |
2608 | if (skip) { | |
d397712b CM |
2609 | u32 start = |
2610 | btrfs_file_extent_calc_inline_size(0); | |
c5c9cd4d SW |
2611 | memmove(buf+start, buf+start+skip, |
2612 | datal); | |
2613 | } | |
2614 | ||
2615 | leaf = path->nodes[0]; | |
2616 | slot = path->slots[0]; | |
2617 | write_extent_buffer(leaf, buf, | |
2618 | btrfs_item_ptr_offset(leaf, slot), | |
2619 | size); | |
2620 | inode_add_bytes(inode, datal); | |
f46b5a66 | 2621 | } |
c5c9cd4d SW |
2622 | |
2623 | btrfs_mark_buffer_dirty(leaf); | |
b3b4aa74 | 2624 | btrfs_release_path(path); |
c5c9cd4d | 2625 | |
0c4d2d95 | 2626 | inode_inc_iversion(inode); |
a22285a6 | 2627 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; |
b5384d48 SW |
2628 | |
2629 | /* | |
2630 | * we round up to the block size at eof when | |
2631 | * determining which extents to clone above, | |
2632 | * but shouldn't round up the file size | |
2633 | */ | |
2634 | endoff = new_key.offset + datal; | |
5f3888ff LZ |
2635 | if (endoff > destoff+olen) |
2636 | endoff = destoff+olen; | |
b5384d48 SW |
2637 | if (endoff > inode->i_size) |
2638 | btrfs_i_size_write(inode, endoff); | |
2639 | ||
a22285a6 | 2640 | ret = btrfs_update_inode(trans, root, inode); |
79787eaa JM |
2641 | if (ret) { |
2642 | btrfs_abort_transaction(trans, root, ret); | |
2643 | btrfs_end_transaction(trans, root); | |
2644 | goto out; | |
2645 | } | |
2646 | ret = btrfs_end_transaction(trans, root); | |
a22285a6 | 2647 | } |
d397712b | 2648 | next: |
b3b4aa74 | 2649 | btrfs_release_path(path); |
f46b5a66 | 2650 | key.offset++; |
f46b5a66 | 2651 | } |
f46b5a66 CH |
2652 | ret = 0; |
2653 | out: | |
b3b4aa74 | 2654 | btrfs_release_path(path); |
d0082371 | 2655 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len); |
f46b5a66 CH |
2656 | out_unlock: |
2657 | mutex_unlock(&src->i_mutex); | |
2658 | mutex_unlock(&inode->i_mutex); | |
ae01a0ab YZ |
2659 | vfree(buf); |
2660 | btrfs_free_path(path); | |
f46b5a66 CH |
2661 | out_fput: |
2662 | fput(src_file); | |
ab67b7c1 | 2663 | out_drop_write: |
2a79f17e | 2664 | mnt_drop_write_file(file); |
f46b5a66 CH |
2665 | return ret; |
2666 | } | |
2667 | ||
7a865e8a | 2668 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
c5c9cd4d SW |
2669 | { |
2670 | struct btrfs_ioctl_clone_range_args args; | |
2671 | ||
7a865e8a | 2672 | if (copy_from_user(&args, argp, sizeof(args))) |
c5c9cd4d SW |
2673 | return -EFAULT; |
2674 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, | |
2675 | args.src_length, args.dest_offset); | |
2676 | } | |
2677 | ||
f46b5a66 CH |
2678 | /* |
2679 | * there are many ways the trans_start and trans_end ioctls can lead | |
2680 | * to deadlocks. They should only be used by applications that | |
2681 | * basically own the machine, and have a very in depth understanding | |
2682 | * of all the possible deadlocks and enospc problems. | |
2683 | */ | |
b2950863 | 2684 | static long btrfs_ioctl_trans_start(struct file *file) |
f46b5a66 CH |
2685 | { |
2686 | struct inode *inode = fdentry(file)->d_inode; | |
2687 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2688 | struct btrfs_trans_handle *trans; | |
1ab86aed | 2689 | int ret; |
f46b5a66 | 2690 | |
1ab86aed | 2691 | ret = -EPERM; |
df5b5520 | 2692 | if (!capable(CAP_SYS_ADMIN)) |
1ab86aed | 2693 | goto out; |
df5b5520 | 2694 | |
1ab86aed SW |
2695 | ret = -EINPROGRESS; |
2696 | if (file->private_data) | |
f46b5a66 | 2697 | goto out; |
9ca9ee09 | 2698 | |
b83cc969 LZ |
2699 | ret = -EROFS; |
2700 | if (btrfs_root_readonly(root)) | |
2701 | goto out; | |
2702 | ||
a561be71 | 2703 | ret = mnt_want_write_file(file); |
c146afad YZ |
2704 | if (ret) |
2705 | goto out; | |
2706 | ||
a4abeea4 | 2707 | atomic_inc(&root->fs_info->open_ioctl_trans); |
9ca9ee09 | 2708 | |
1ab86aed | 2709 | ret = -ENOMEM; |
7a7eaa40 | 2710 | trans = btrfs_start_ioctl_transaction(root); |
abd30bb0 | 2711 | if (IS_ERR(trans)) |
1ab86aed SW |
2712 | goto out_drop; |
2713 | ||
2714 | file->private_data = trans; | |
2715 | return 0; | |
2716 | ||
2717 | out_drop: | |
a4abeea4 | 2718 | atomic_dec(&root->fs_info->open_ioctl_trans); |
2a79f17e | 2719 | mnt_drop_write_file(file); |
f46b5a66 | 2720 | out: |
f46b5a66 CH |
2721 | return ret; |
2722 | } | |
2723 | ||
6ef5ed0d JB |
2724 | static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp) |
2725 | { | |
2726 | struct inode *inode = fdentry(file)->d_inode; | |
2727 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2728 | struct btrfs_root *new_root; | |
2729 | struct btrfs_dir_item *di; | |
2730 | struct btrfs_trans_handle *trans; | |
2731 | struct btrfs_path *path; | |
2732 | struct btrfs_key location; | |
2733 | struct btrfs_disk_key disk_key; | |
2734 | struct btrfs_super_block *disk_super; | |
2735 | u64 features; | |
2736 | u64 objectid = 0; | |
2737 | u64 dir_id; | |
2738 | ||
2739 | if (!capable(CAP_SYS_ADMIN)) | |
2740 | return -EPERM; | |
2741 | ||
2742 | if (copy_from_user(&objectid, argp, sizeof(objectid))) | |
2743 | return -EFAULT; | |
2744 | ||
2745 | if (!objectid) | |
2746 | objectid = root->root_key.objectid; | |
2747 | ||
2748 | location.objectid = objectid; | |
2749 | location.type = BTRFS_ROOT_ITEM_KEY; | |
2750 | location.offset = (u64)-1; | |
2751 | ||
2752 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &location); | |
2753 | if (IS_ERR(new_root)) | |
2754 | return PTR_ERR(new_root); | |
2755 | ||
2756 | if (btrfs_root_refs(&new_root->root_item) == 0) | |
2757 | return -ENOENT; | |
2758 | ||
2759 | path = btrfs_alloc_path(); | |
2760 | if (!path) | |
2761 | return -ENOMEM; | |
2762 | path->leave_spinning = 1; | |
2763 | ||
2764 | trans = btrfs_start_transaction(root, 1); | |
98d5dc13 | 2765 | if (IS_ERR(trans)) { |
6ef5ed0d | 2766 | btrfs_free_path(path); |
98d5dc13 | 2767 | return PTR_ERR(trans); |
6ef5ed0d JB |
2768 | } |
2769 | ||
6c41761f | 2770 | dir_id = btrfs_super_root_dir(root->fs_info->super_copy); |
6ef5ed0d JB |
2771 | di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path, |
2772 | dir_id, "default", 7, 1); | |
cf1e99a4 | 2773 | if (IS_ERR_OR_NULL(di)) { |
6ef5ed0d JB |
2774 | btrfs_free_path(path); |
2775 | btrfs_end_transaction(trans, root); | |
2776 | printk(KERN_ERR "Umm, you don't have the default dir item, " | |
2777 | "this isn't going to work\n"); | |
2778 | return -ENOENT; | |
2779 | } | |
2780 | ||
2781 | btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key); | |
2782 | btrfs_set_dir_item_key(path->nodes[0], di, &disk_key); | |
2783 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
2784 | btrfs_free_path(path); | |
2785 | ||
6c41761f | 2786 | disk_super = root->fs_info->super_copy; |
6ef5ed0d JB |
2787 | features = btrfs_super_incompat_flags(disk_super); |
2788 | if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) { | |
2789 | features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL; | |
2790 | btrfs_set_super_incompat_flags(disk_super, features); | |
2791 | } | |
2792 | btrfs_end_transaction(trans, root); | |
2793 | ||
2794 | return 0; | |
2795 | } | |
2796 | ||
bf5fc093 JB |
2797 | static void get_block_group_info(struct list_head *groups_list, |
2798 | struct btrfs_ioctl_space_info *space) | |
2799 | { | |
2800 | struct btrfs_block_group_cache *block_group; | |
2801 | ||
2802 | space->total_bytes = 0; | |
2803 | space->used_bytes = 0; | |
2804 | space->flags = 0; | |
2805 | list_for_each_entry(block_group, groups_list, list) { | |
2806 | space->flags = block_group->flags; | |
2807 | space->total_bytes += block_group->key.offset; | |
2808 | space->used_bytes += | |
2809 | btrfs_block_group_used(&block_group->item); | |
2810 | } | |
2811 | } | |
2812 | ||
1406e432 JB |
2813 | long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg) |
2814 | { | |
2815 | struct btrfs_ioctl_space_args space_args; | |
2816 | struct btrfs_ioctl_space_info space; | |
2817 | struct btrfs_ioctl_space_info *dest; | |
7fde62bf | 2818 | struct btrfs_ioctl_space_info *dest_orig; |
13f2696f | 2819 | struct btrfs_ioctl_space_info __user *user_dest; |
1406e432 | 2820 | struct btrfs_space_info *info; |
bf5fc093 JB |
2821 | u64 types[] = {BTRFS_BLOCK_GROUP_DATA, |
2822 | BTRFS_BLOCK_GROUP_SYSTEM, | |
2823 | BTRFS_BLOCK_GROUP_METADATA, | |
2824 | BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA}; | |
2825 | int num_types = 4; | |
7fde62bf | 2826 | int alloc_size; |
1406e432 | 2827 | int ret = 0; |
51788b1b | 2828 | u64 slot_count = 0; |
bf5fc093 | 2829 | int i, c; |
1406e432 JB |
2830 | |
2831 | if (copy_from_user(&space_args, | |
2832 | (struct btrfs_ioctl_space_args __user *)arg, | |
2833 | sizeof(space_args))) | |
2834 | return -EFAULT; | |
2835 | ||
bf5fc093 JB |
2836 | for (i = 0; i < num_types; i++) { |
2837 | struct btrfs_space_info *tmp; | |
2838 | ||
2839 | info = NULL; | |
2840 | rcu_read_lock(); | |
2841 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, | |
2842 | list) { | |
2843 | if (tmp->flags == types[i]) { | |
2844 | info = tmp; | |
2845 | break; | |
2846 | } | |
2847 | } | |
2848 | rcu_read_unlock(); | |
2849 | ||
2850 | if (!info) | |
2851 | continue; | |
2852 | ||
2853 | down_read(&info->groups_sem); | |
2854 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
2855 | if (!list_empty(&info->block_groups[c])) | |
2856 | slot_count++; | |
2857 | } | |
2858 | up_read(&info->groups_sem); | |
2859 | } | |
7fde62bf CM |
2860 | |
2861 | /* space_slots == 0 means they are asking for a count */ | |
2862 | if (space_args.space_slots == 0) { | |
2863 | space_args.total_spaces = slot_count; | |
2864 | goto out; | |
2865 | } | |
bf5fc093 | 2866 | |
51788b1b | 2867 | slot_count = min_t(u64, space_args.space_slots, slot_count); |
bf5fc093 | 2868 | |
7fde62bf | 2869 | alloc_size = sizeof(*dest) * slot_count; |
bf5fc093 | 2870 | |
7fde62bf CM |
2871 | /* we generally have at most 6 or so space infos, one for each raid |
2872 | * level. So, a whole page should be more than enough for everyone | |
2873 | */ | |
2874 | if (alloc_size > PAGE_CACHE_SIZE) | |
2875 | return -ENOMEM; | |
2876 | ||
1406e432 | 2877 | space_args.total_spaces = 0; |
7fde62bf CM |
2878 | dest = kmalloc(alloc_size, GFP_NOFS); |
2879 | if (!dest) | |
2880 | return -ENOMEM; | |
2881 | dest_orig = dest; | |
1406e432 | 2882 | |
7fde62bf | 2883 | /* now we have a buffer to copy into */ |
bf5fc093 JB |
2884 | for (i = 0; i < num_types; i++) { |
2885 | struct btrfs_space_info *tmp; | |
2886 | ||
51788b1b DR |
2887 | if (!slot_count) |
2888 | break; | |
2889 | ||
bf5fc093 JB |
2890 | info = NULL; |
2891 | rcu_read_lock(); | |
2892 | list_for_each_entry_rcu(tmp, &root->fs_info->space_info, | |
2893 | list) { | |
2894 | if (tmp->flags == types[i]) { | |
2895 | info = tmp; | |
2896 | break; | |
2897 | } | |
2898 | } | |
2899 | rcu_read_unlock(); | |
7fde62bf | 2900 | |
bf5fc093 JB |
2901 | if (!info) |
2902 | continue; | |
2903 | down_read(&info->groups_sem); | |
2904 | for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) { | |
2905 | if (!list_empty(&info->block_groups[c])) { | |
2906 | get_block_group_info(&info->block_groups[c], | |
2907 | &space); | |
2908 | memcpy(dest, &space, sizeof(space)); | |
2909 | dest++; | |
2910 | space_args.total_spaces++; | |
51788b1b | 2911 | slot_count--; |
bf5fc093 | 2912 | } |
51788b1b DR |
2913 | if (!slot_count) |
2914 | break; | |
bf5fc093 JB |
2915 | } |
2916 | up_read(&info->groups_sem); | |
1406e432 | 2917 | } |
1406e432 | 2918 | |
2eec6c81 | 2919 | user_dest = (struct btrfs_ioctl_space_info __user *) |
7fde62bf CM |
2920 | (arg + sizeof(struct btrfs_ioctl_space_args)); |
2921 | ||
2922 | if (copy_to_user(user_dest, dest_orig, alloc_size)) | |
2923 | ret = -EFAULT; | |
2924 | ||
2925 | kfree(dest_orig); | |
2926 | out: | |
2927 | if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args))) | |
1406e432 JB |
2928 | ret = -EFAULT; |
2929 | ||
2930 | return ret; | |
2931 | } | |
2932 | ||
f46b5a66 CH |
2933 | /* |
2934 | * there are many ways the trans_start and trans_end ioctls can lead | |
2935 | * to deadlocks. They should only be used by applications that | |
2936 | * basically own the machine, and have a very in depth understanding | |
2937 | * of all the possible deadlocks and enospc problems. | |
2938 | */ | |
2939 | long btrfs_ioctl_trans_end(struct file *file) | |
2940 | { | |
2941 | struct inode *inode = fdentry(file)->d_inode; | |
2942 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2943 | struct btrfs_trans_handle *trans; | |
f46b5a66 | 2944 | |
f46b5a66 | 2945 | trans = file->private_data; |
1ab86aed SW |
2946 | if (!trans) |
2947 | return -EINVAL; | |
b214107e | 2948 | file->private_data = NULL; |
9ca9ee09 | 2949 | |
1ab86aed SW |
2950 | btrfs_end_transaction(trans, root); |
2951 | ||
a4abeea4 | 2952 | atomic_dec(&root->fs_info->open_ioctl_trans); |
9ca9ee09 | 2953 | |
2a79f17e | 2954 | mnt_drop_write_file(file); |
1ab86aed | 2955 | return 0; |
f46b5a66 CH |
2956 | } |
2957 | ||
46204592 SW |
2958 | static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp) |
2959 | { | |
2960 | struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; | |
2961 | struct btrfs_trans_handle *trans; | |
2962 | u64 transid; | |
db5b493a | 2963 | int ret; |
46204592 SW |
2964 | |
2965 | trans = btrfs_start_transaction(root, 0); | |
98d5dc13 TI |
2966 | if (IS_ERR(trans)) |
2967 | return PTR_ERR(trans); | |
46204592 | 2968 | transid = trans->transid; |
db5b493a | 2969 | ret = btrfs_commit_transaction_async(trans, root, 0); |
8b2b2d3c TI |
2970 | if (ret) { |
2971 | btrfs_end_transaction(trans, root); | |
db5b493a | 2972 | return ret; |
8b2b2d3c | 2973 | } |
46204592 SW |
2974 | |
2975 | if (argp) | |
2976 | if (copy_to_user(argp, &transid, sizeof(transid))) | |
2977 | return -EFAULT; | |
2978 | return 0; | |
2979 | } | |
2980 | ||
2981 | static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp) | |
2982 | { | |
2983 | struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root; | |
2984 | u64 transid; | |
2985 | ||
2986 | if (argp) { | |
2987 | if (copy_from_user(&transid, argp, sizeof(transid))) | |
2988 | return -EFAULT; | |
2989 | } else { | |
2990 | transid = 0; /* current trans */ | |
2991 | } | |
2992 | return btrfs_wait_for_commit(root, transid); | |
2993 | } | |
2994 | ||
475f6387 JS |
2995 | static long btrfs_ioctl_scrub(struct btrfs_root *root, void __user *arg) |
2996 | { | |
2997 | int ret; | |
2998 | struct btrfs_ioctl_scrub_args *sa; | |
2999 | ||
3000 | if (!capable(CAP_SYS_ADMIN)) | |
3001 | return -EPERM; | |
3002 | ||
3003 | sa = memdup_user(arg, sizeof(*sa)); | |
3004 | if (IS_ERR(sa)) | |
3005 | return PTR_ERR(sa); | |
3006 | ||
3007 | ret = btrfs_scrub_dev(root, sa->devid, sa->start, sa->end, | |
8628764e | 3008 | &sa->progress, sa->flags & BTRFS_SCRUB_READONLY); |
475f6387 JS |
3009 | |
3010 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
3011 | ret = -EFAULT; | |
3012 | ||
3013 | kfree(sa); | |
3014 | return ret; | |
3015 | } | |
3016 | ||
3017 | static long btrfs_ioctl_scrub_cancel(struct btrfs_root *root, void __user *arg) | |
3018 | { | |
3019 | if (!capable(CAP_SYS_ADMIN)) | |
3020 | return -EPERM; | |
3021 | ||
3022 | return btrfs_scrub_cancel(root); | |
3023 | } | |
3024 | ||
3025 | static long btrfs_ioctl_scrub_progress(struct btrfs_root *root, | |
3026 | void __user *arg) | |
3027 | { | |
3028 | struct btrfs_ioctl_scrub_args *sa; | |
3029 | int ret; | |
3030 | ||
3031 | if (!capable(CAP_SYS_ADMIN)) | |
3032 | return -EPERM; | |
3033 | ||
3034 | sa = memdup_user(arg, sizeof(*sa)); | |
3035 | if (IS_ERR(sa)) | |
3036 | return PTR_ERR(sa); | |
3037 | ||
3038 | ret = btrfs_scrub_progress(root, sa->devid, &sa->progress); | |
3039 | ||
3040 | if (copy_to_user(arg, sa, sizeof(*sa))) | |
3041 | ret = -EFAULT; | |
3042 | ||
3043 | kfree(sa); | |
3044 | return ret; | |
3045 | } | |
3046 | ||
d7728c96 JS |
3047 | static long btrfs_ioctl_ino_to_path(struct btrfs_root *root, void __user *arg) |
3048 | { | |
3049 | int ret = 0; | |
3050 | int i; | |
740c3d22 | 3051 | u64 rel_ptr; |
d7728c96 | 3052 | int size; |
806468f8 | 3053 | struct btrfs_ioctl_ino_path_args *ipa = NULL; |
d7728c96 JS |
3054 | struct inode_fs_paths *ipath = NULL; |
3055 | struct btrfs_path *path; | |
3056 | ||
3057 | if (!capable(CAP_SYS_ADMIN)) | |
3058 | return -EPERM; | |
3059 | ||
3060 | path = btrfs_alloc_path(); | |
3061 | if (!path) { | |
3062 | ret = -ENOMEM; | |
3063 | goto out; | |
3064 | } | |
3065 | ||
3066 | ipa = memdup_user(arg, sizeof(*ipa)); | |
3067 | if (IS_ERR(ipa)) { | |
3068 | ret = PTR_ERR(ipa); | |
3069 | ipa = NULL; | |
3070 | goto out; | |
3071 | } | |
3072 | ||
3073 | size = min_t(u32, ipa->size, 4096); | |
3074 | ipath = init_ipath(size, root, path); | |
3075 | if (IS_ERR(ipath)) { | |
3076 | ret = PTR_ERR(ipath); | |
3077 | ipath = NULL; | |
3078 | goto out; | |
3079 | } | |
3080 | ||
3081 | ret = paths_from_inode(ipa->inum, ipath); | |
3082 | if (ret < 0) | |
3083 | goto out; | |
3084 | ||
3085 | for (i = 0; i < ipath->fspath->elem_cnt; ++i) { | |
745c4d8e JM |
3086 | rel_ptr = ipath->fspath->val[i] - |
3087 | (u64)(unsigned long)ipath->fspath->val; | |
740c3d22 | 3088 | ipath->fspath->val[i] = rel_ptr; |
d7728c96 JS |
3089 | } |
3090 | ||
745c4d8e JM |
3091 | ret = copy_to_user((void *)(unsigned long)ipa->fspath, |
3092 | (void *)(unsigned long)ipath->fspath, size); | |
d7728c96 JS |
3093 | if (ret) { |
3094 | ret = -EFAULT; | |
3095 | goto out; | |
3096 | } | |
3097 | ||
3098 | out: | |
3099 | btrfs_free_path(path); | |
3100 | free_ipath(ipath); | |
3101 | kfree(ipa); | |
3102 | ||
3103 | return ret; | |
3104 | } | |
3105 | ||
3106 | static int build_ino_list(u64 inum, u64 offset, u64 root, void *ctx) | |
3107 | { | |
3108 | struct btrfs_data_container *inodes = ctx; | |
3109 | const size_t c = 3 * sizeof(u64); | |
3110 | ||
3111 | if (inodes->bytes_left >= c) { | |
3112 | inodes->bytes_left -= c; | |
3113 | inodes->val[inodes->elem_cnt] = inum; | |
3114 | inodes->val[inodes->elem_cnt + 1] = offset; | |
3115 | inodes->val[inodes->elem_cnt + 2] = root; | |
3116 | inodes->elem_cnt += 3; | |
3117 | } else { | |
3118 | inodes->bytes_missing += c - inodes->bytes_left; | |
3119 | inodes->bytes_left = 0; | |
3120 | inodes->elem_missed += 3; | |
3121 | } | |
3122 | ||
3123 | return 0; | |
3124 | } | |
3125 | ||
3126 | static long btrfs_ioctl_logical_to_ino(struct btrfs_root *root, | |
3127 | void __user *arg) | |
3128 | { | |
3129 | int ret = 0; | |
3130 | int size; | |
4692cf58 | 3131 | u64 extent_item_pos; |
d7728c96 JS |
3132 | struct btrfs_ioctl_logical_ino_args *loi; |
3133 | struct btrfs_data_container *inodes = NULL; | |
3134 | struct btrfs_path *path = NULL; | |
3135 | struct btrfs_key key; | |
3136 | ||
3137 | if (!capable(CAP_SYS_ADMIN)) | |
3138 | return -EPERM; | |
3139 | ||
3140 | loi = memdup_user(arg, sizeof(*loi)); | |
3141 | if (IS_ERR(loi)) { | |
3142 | ret = PTR_ERR(loi); | |
3143 | loi = NULL; | |
3144 | goto out; | |
3145 | } | |
3146 | ||
3147 | path = btrfs_alloc_path(); | |
3148 | if (!path) { | |
3149 | ret = -ENOMEM; | |
3150 | goto out; | |
3151 | } | |
3152 | ||
3153 | size = min_t(u32, loi->size, 4096); | |
3154 | inodes = init_data_container(size); | |
3155 | if (IS_ERR(inodes)) { | |
3156 | ret = PTR_ERR(inodes); | |
3157 | inodes = NULL; | |
3158 | goto out; | |
3159 | } | |
3160 | ||
3161 | ret = extent_from_logical(root->fs_info, loi->logical, path, &key); | |
4692cf58 | 3162 | btrfs_release_path(path); |
d7728c96 JS |
3163 | |
3164 | if (ret & BTRFS_EXTENT_FLAG_TREE_BLOCK) | |
3165 | ret = -ENOENT; | |
3166 | if (ret < 0) | |
3167 | goto out; | |
3168 | ||
4692cf58 | 3169 | extent_item_pos = loi->logical - key.objectid; |
7a3ae2f8 JS |
3170 | ret = iterate_extent_inodes(root->fs_info, key.objectid, |
3171 | extent_item_pos, 0, build_ino_list, | |
4692cf58 | 3172 | inodes); |
d7728c96 JS |
3173 | |
3174 | if (ret < 0) | |
3175 | goto out; | |
3176 | ||
745c4d8e JM |
3177 | ret = copy_to_user((void *)(unsigned long)loi->inodes, |
3178 | (void *)(unsigned long)inodes, size); | |
d7728c96 JS |
3179 | if (ret) |
3180 | ret = -EFAULT; | |
3181 | ||
3182 | out: | |
3183 | btrfs_free_path(path); | |
3184 | kfree(inodes); | |
3185 | kfree(loi); | |
3186 | ||
3187 | return ret; | |
3188 | } | |
3189 | ||
19a39dce | 3190 | void update_ioctl_balance_args(struct btrfs_fs_info *fs_info, int lock, |
c9e9f97b ID |
3191 | struct btrfs_ioctl_balance_args *bargs) |
3192 | { | |
3193 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
3194 | ||
3195 | bargs->flags = bctl->flags; | |
3196 | ||
837d5b6e ID |
3197 | if (atomic_read(&fs_info->balance_running)) |
3198 | bargs->state |= BTRFS_BALANCE_STATE_RUNNING; | |
3199 | if (atomic_read(&fs_info->balance_pause_req)) | |
3200 | bargs->state |= BTRFS_BALANCE_STATE_PAUSE_REQ; | |
a7e99c69 ID |
3201 | if (atomic_read(&fs_info->balance_cancel_req)) |
3202 | bargs->state |= BTRFS_BALANCE_STATE_CANCEL_REQ; | |
837d5b6e | 3203 | |
c9e9f97b ID |
3204 | memcpy(&bargs->data, &bctl->data, sizeof(bargs->data)); |
3205 | memcpy(&bargs->meta, &bctl->meta, sizeof(bargs->meta)); | |
3206 | memcpy(&bargs->sys, &bctl->sys, sizeof(bargs->sys)); | |
19a39dce ID |
3207 | |
3208 | if (lock) { | |
3209 | spin_lock(&fs_info->balance_lock); | |
3210 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); | |
3211 | spin_unlock(&fs_info->balance_lock); | |
3212 | } else { | |
3213 | memcpy(&bargs->stat, &bctl->stat, sizeof(bargs->stat)); | |
3214 | } | |
c9e9f97b ID |
3215 | } |
3216 | ||
3217 | static long btrfs_ioctl_balance(struct btrfs_root *root, void __user *arg) | |
3218 | { | |
3219 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3220 | struct btrfs_ioctl_balance_args *bargs; | |
3221 | struct btrfs_balance_control *bctl; | |
3222 | int ret; | |
3223 | ||
3224 | if (!capable(CAP_SYS_ADMIN)) | |
3225 | return -EPERM; | |
3226 | ||
3227 | if (fs_info->sb->s_flags & MS_RDONLY) | |
3228 | return -EROFS; | |
3229 | ||
3230 | mutex_lock(&fs_info->volume_mutex); | |
3231 | mutex_lock(&fs_info->balance_mutex); | |
3232 | ||
3233 | if (arg) { | |
3234 | bargs = memdup_user(arg, sizeof(*bargs)); | |
3235 | if (IS_ERR(bargs)) { | |
3236 | ret = PTR_ERR(bargs); | |
3237 | goto out; | |
3238 | } | |
de322263 ID |
3239 | |
3240 | if (bargs->flags & BTRFS_BALANCE_RESUME) { | |
3241 | if (!fs_info->balance_ctl) { | |
3242 | ret = -ENOTCONN; | |
3243 | goto out_bargs; | |
3244 | } | |
3245 | ||
3246 | bctl = fs_info->balance_ctl; | |
3247 | spin_lock(&fs_info->balance_lock); | |
3248 | bctl->flags |= BTRFS_BALANCE_RESUME; | |
3249 | spin_unlock(&fs_info->balance_lock); | |
3250 | ||
3251 | goto do_balance; | |
3252 | } | |
c9e9f97b ID |
3253 | } else { |
3254 | bargs = NULL; | |
3255 | } | |
3256 | ||
837d5b6e ID |
3257 | if (fs_info->balance_ctl) { |
3258 | ret = -EINPROGRESS; | |
3259 | goto out_bargs; | |
3260 | } | |
3261 | ||
c9e9f97b ID |
3262 | bctl = kzalloc(sizeof(*bctl), GFP_NOFS); |
3263 | if (!bctl) { | |
3264 | ret = -ENOMEM; | |
3265 | goto out_bargs; | |
3266 | } | |
3267 | ||
3268 | bctl->fs_info = fs_info; | |
3269 | if (arg) { | |
3270 | memcpy(&bctl->data, &bargs->data, sizeof(bctl->data)); | |
3271 | memcpy(&bctl->meta, &bargs->meta, sizeof(bctl->meta)); | |
3272 | memcpy(&bctl->sys, &bargs->sys, sizeof(bctl->sys)); | |
3273 | ||
3274 | bctl->flags = bargs->flags; | |
f43ffb60 ID |
3275 | } else { |
3276 | /* balance everything - no filters */ | |
3277 | bctl->flags |= BTRFS_BALANCE_TYPE_MASK; | |
c9e9f97b ID |
3278 | } |
3279 | ||
de322263 | 3280 | do_balance: |
c9e9f97b ID |
3281 | ret = btrfs_balance(bctl, bargs); |
3282 | /* | |
837d5b6e ID |
3283 | * bctl is freed in __cancel_balance or in free_fs_info if |
3284 | * restriper was paused all the way until unmount | |
c9e9f97b ID |
3285 | */ |
3286 | if (arg) { | |
3287 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | |
3288 | ret = -EFAULT; | |
3289 | } | |
3290 | ||
3291 | out_bargs: | |
3292 | kfree(bargs); | |
3293 | out: | |
3294 | mutex_unlock(&fs_info->balance_mutex); | |
3295 | mutex_unlock(&fs_info->volume_mutex); | |
3296 | return ret; | |
3297 | } | |
3298 | ||
837d5b6e ID |
3299 | static long btrfs_ioctl_balance_ctl(struct btrfs_root *root, int cmd) |
3300 | { | |
3301 | if (!capable(CAP_SYS_ADMIN)) | |
3302 | return -EPERM; | |
3303 | ||
3304 | switch (cmd) { | |
3305 | case BTRFS_BALANCE_CTL_PAUSE: | |
3306 | return btrfs_pause_balance(root->fs_info); | |
a7e99c69 ID |
3307 | case BTRFS_BALANCE_CTL_CANCEL: |
3308 | return btrfs_cancel_balance(root->fs_info); | |
837d5b6e ID |
3309 | } |
3310 | ||
3311 | return -EINVAL; | |
3312 | } | |
3313 | ||
19a39dce ID |
3314 | static long btrfs_ioctl_balance_progress(struct btrfs_root *root, |
3315 | void __user *arg) | |
3316 | { | |
3317 | struct btrfs_fs_info *fs_info = root->fs_info; | |
3318 | struct btrfs_ioctl_balance_args *bargs; | |
3319 | int ret = 0; | |
3320 | ||
3321 | if (!capable(CAP_SYS_ADMIN)) | |
3322 | return -EPERM; | |
3323 | ||
3324 | mutex_lock(&fs_info->balance_mutex); | |
3325 | if (!fs_info->balance_ctl) { | |
3326 | ret = -ENOTCONN; | |
3327 | goto out; | |
3328 | } | |
3329 | ||
3330 | bargs = kzalloc(sizeof(*bargs), GFP_NOFS); | |
3331 | if (!bargs) { | |
3332 | ret = -ENOMEM; | |
3333 | goto out; | |
3334 | } | |
3335 | ||
3336 | update_ioctl_balance_args(fs_info, 1, bargs); | |
3337 | ||
3338 | if (copy_to_user(arg, bargs, sizeof(*bargs))) | |
3339 | ret = -EFAULT; | |
3340 | ||
3341 | kfree(bargs); | |
3342 | out: | |
3343 | mutex_unlock(&fs_info->balance_mutex); | |
3344 | return ret; | |
3345 | } | |
3346 | ||
f46b5a66 CH |
3347 | long btrfs_ioctl(struct file *file, unsigned int |
3348 | cmd, unsigned long arg) | |
3349 | { | |
3350 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; | |
4bcabaa3 | 3351 | void __user *argp = (void __user *)arg; |
f46b5a66 CH |
3352 | |
3353 | switch (cmd) { | |
6cbff00f CH |
3354 | case FS_IOC_GETFLAGS: |
3355 | return btrfs_ioctl_getflags(file, argp); | |
3356 | case FS_IOC_SETFLAGS: | |
3357 | return btrfs_ioctl_setflags(file, argp); | |
3358 | case FS_IOC_GETVERSION: | |
3359 | return btrfs_ioctl_getversion(file, argp); | |
f7039b1d LD |
3360 | case FITRIM: |
3361 | return btrfs_ioctl_fitrim(file, argp); | |
f46b5a66 | 3362 | case BTRFS_IOC_SNAP_CREATE: |
fa0d2b9b | 3363 | return btrfs_ioctl_snap_create(file, argp, 0); |
fdfb1e4f | 3364 | case BTRFS_IOC_SNAP_CREATE_V2: |
fa0d2b9b | 3365 | return btrfs_ioctl_snap_create_v2(file, argp, 0); |
3de4586c | 3366 | case BTRFS_IOC_SUBVOL_CREATE: |
fa0d2b9b | 3367 | return btrfs_ioctl_snap_create(file, argp, 1); |
76dda93c YZ |
3368 | case BTRFS_IOC_SNAP_DESTROY: |
3369 | return btrfs_ioctl_snap_destroy(file, argp); | |
0caa102d LZ |
3370 | case BTRFS_IOC_SUBVOL_GETFLAGS: |
3371 | return btrfs_ioctl_subvol_getflags(file, argp); | |
3372 | case BTRFS_IOC_SUBVOL_SETFLAGS: | |
3373 | return btrfs_ioctl_subvol_setflags(file, argp); | |
6ef5ed0d JB |
3374 | case BTRFS_IOC_DEFAULT_SUBVOL: |
3375 | return btrfs_ioctl_default_subvol(file, argp); | |
f46b5a66 | 3376 | case BTRFS_IOC_DEFRAG: |
1e701a32 CM |
3377 | return btrfs_ioctl_defrag(file, NULL); |
3378 | case BTRFS_IOC_DEFRAG_RANGE: | |
3379 | return btrfs_ioctl_defrag(file, argp); | |
f46b5a66 | 3380 | case BTRFS_IOC_RESIZE: |
4bcabaa3 | 3381 | return btrfs_ioctl_resize(root, argp); |
f46b5a66 | 3382 | case BTRFS_IOC_ADD_DEV: |
4bcabaa3 | 3383 | return btrfs_ioctl_add_dev(root, argp); |
f46b5a66 | 3384 | case BTRFS_IOC_RM_DEV: |
4bcabaa3 | 3385 | return btrfs_ioctl_rm_dev(root, argp); |
475f6387 JS |
3386 | case BTRFS_IOC_FS_INFO: |
3387 | return btrfs_ioctl_fs_info(root, argp); | |
3388 | case BTRFS_IOC_DEV_INFO: | |
3389 | return btrfs_ioctl_dev_info(root, argp); | |
f46b5a66 | 3390 | case BTRFS_IOC_BALANCE: |
c9e9f97b | 3391 | return btrfs_ioctl_balance(root, NULL); |
f46b5a66 | 3392 | case BTRFS_IOC_CLONE: |
c5c9cd4d SW |
3393 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
3394 | case BTRFS_IOC_CLONE_RANGE: | |
7a865e8a | 3395 | return btrfs_ioctl_clone_range(file, argp); |
f46b5a66 CH |
3396 | case BTRFS_IOC_TRANS_START: |
3397 | return btrfs_ioctl_trans_start(file); | |
3398 | case BTRFS_IOC_TRANS_END: | |
3399 | return btrfs_ioctl_trans_end(file); | |
ac8e9819 CM |
3400 | case BTRFS_IOC_TREE_SEARCH: |
3401 | return btrfs_ioctl_tree_search(file, argp); | |
3402 | case BTRFS_IOC_INO_LOOKUP: | |
3403 | return btrfs_ioctl_ino_lookup(file, argp); | |
d7728c96 JS |
3404 | case BTRFS_IOC_INO_PATHS: |
3405 | return btrfs_ioctl_ino_to_path(root, argp); | |
3406 | case BTRFS_IOC_LOGICAL_INO: | |
3407 | return btrfs_ioctl_logical_to_ino(root, argp); | |
1406e432 JB |
3408 | case BTRFS_IOC_SPACE_INFO: |
3409 | return btrfs_ioctl_space_info(root, argp); | |
f46b5a66 CH |
3410 | case BTRFS_IOC_SYNC: |
3411 | btrfs_sync_fs(file->f_dentry->d_sb, 1); | |
3412 | return 0; | |
46204592 SW |
3413 | case BTRFS_IOC_START_SYNC: |
3414 | return btrfs_ioctl_start_sync(file, argp); | |
3415 | case BTRFS_IOC_WAIT_SYNC: | |
3416 | return btrfs_ioctl_wait_sync(file, argp); | |
475f6387 JS |
3417 | case BTRFS_IOC_SCRUB: |
3418 | return btrfs_ioctl_scrub(root, argp); | |
3419 | case BTRFS_IOC_SCRUB_CANCEL: | |
3420 | return btrfs_ioctl_scrub_cancel(root, argp); | |
3421 | case BTRFS_IOC_SCRUB_PROGRESS: | |
3422 | return btrfs_ioctl_scrub_progress(root, argp); | |
c9e9f97b ID |
3423 | case BTRFS_IOC_BALANCE_V2: |
3424 | return btrfs_ioctl_balance(root, argp); | |
837d5b6e ID |
3425 | case BTRFS_IOC_BALANCE_CTL: |
3426 | return btrfs_ioctl_balance_ctl(root, arg); | |
19a39dce ID |
3427 | case BTRFS_IOC_BALANCE_PROGRESS: |
3428 | return btrfs_ioctl_balance_progress(root, argp); | |
f46b5a66 CH |
3429 | } |
3430 | ||
3431 | return -ENOTTY; | |
3432 | } |