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