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