Btrfs: add btrfs_trim_fs() to handle FITRIM
[deliverable/linux.git] / fs / btrfs / ioctl.c
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
23 #include <linux/fs.h>
24 #include <linux/fsnotify.h>
25 #include <linux/pagemap.h>
26 #include <linux/highmem.h>
27 #include <linux/time.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mount.h>
32 #include <linux/mpage.h>
33 #include <linux/namei.h>
34 #include <linux/swap.h>
35 #include <linux/writeback.h>
36 #include <linux/statfs.h>
37 #include <linux/compat.h>
38 #include <linux/bit_spinlock.h>
39 #include <linux/security.h>
40 #include <linux/xattr.h>
41 #include <linux/vmalloc.h>
42 #include <linux/slab.h>
43 #include <linux/blkdev.h>
44 #include "compat.h"
45 #include "ctree.h"
46 #include "disk-io.h"
47 #include "transaction.h"
48 #include "btrfs_inode.h"
49 #include "ioctl.h"
50 #include "print-tree.h"
51 #include "volumes.h"
52 #include "locking.h"
53
54 /* Mask out flags that are inappropriate for the given type of inode. */
55 static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
56 {
57 if (S_ISDIR(mode))
58 return flags;
59 else if (S_ISREG(mode))
60 return flags & ~FS_DIRSYNC_FL;
61 else
62 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
63 }
64
65 /*
66 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
67 */
68 static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
69 {
70 unsigned int iflags = 0;
71
72 if (flags & BTRFS_INODE_SYNC)
73 iflags |= FS_SYNC_FL;
74 if (flags & BTRFS_INODE_IMMUTABLE)
75 iflags |= FS_IMMUTABLE_FL;
76 if (flags & BTRFS_INODE_APPEND)
77 iflags |= FS_APPEND_FL;
78 if (flags & BTRFS_INODE_NODUMP)
79 iflags |= FS_NODUMP_FL;
80 if (flags & BTRFS_INODE_NOATIME)
81 iflags |= FS_NOATIME_FL;
82 if (flags & BTRFS_INODE_DIRSYNC)
83 iflags |= FS_DIRSYNC_FL;
84
85 return iflags;
86 }
87
88 /*
89 * Update inode->i_flags based on the btrfs internal flags.
90 */
91 void btrfs_update_iflags(struct inode *inode)
92 {
93 struct btrfs_inode *ip = BTRFS_I(inode);
94
95 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
96
97 if (ip->flags & BTRFS_INODE_SYNC)
98 inode->i_flags |= S_SYNC;
99 if (ip->flags & BTRFS_INODE_IMMUTABLE)
100 inode->i_flags |= S_IMMUTABLE;
101 if (ip->flags & BTRFS_INODE_APPEND)
102 inode->i_flags |= S_APPEND;
103 if (ip->flags & BTRFS_INODE_NOATIME)
104 inode->i_flags |= S_NOATIME;
105 if (ip->flags & BTRFS_INODE_DIRSYNC)
106 inode->i_flags |= S_DIRSYNC;
107 }
108
109 /*
110 * Inherit flags from the parent inode.
111 *
112 * Unlike extN we don't have any flags we don't want to inherit currently.
113 */
114 void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
115 {
116 unsigned int flags;
117
118 if (!dir)
119 return;
120
121 flags = BTRFS_I(dir)->flags;
122
123 if (S_ISREG(inode->i_mode))
124 flags &= ~BTRFS_INODE_DIRSYNC;
125 else if (!S_ISDIR(inode->i_mode))
126 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
127
128 BTRFS_I(inode)->flags = flags;
129 btrfs_update_iflags(inode);
130 }
131
132 static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
133 {
134 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
135 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
136
137 if (copy_to_user(arg, &flags, sizeof(flags)))
138 return -EFAULT;
139 return 0;
140 }
141
142 static int check_flags(unsigned int flags)
143 {
144 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
145 FS_NOATIME_FL | FS_NODUMP_FL | \
146 FS_SYNC_FL | FS_DIRSYNC_FL | \
147 FS_NOCOMP_FL | FS_COMPR_FL | \
148 FS_NOCOW_FL | FS_COW_FL))
149 return -EOPNOTSUPP;
150
151 if ((flags & FS_NOCOMP_FL) && (flags & FS_COMPR_FL))
152 return -EINVAL;
153
154 if ((flags & FS_NOCOW_FL) && (flags & FS_COW_FL))
155 return -EINVAL;
156
157 return 0;
158 }
159
160 static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
161 {
162 struct inode *inode = file->f_path.dentry->d_inode;
163 struct btrfs_inode *ip = BTRFS_I(inode);
164 struct btrfs_root *root = ip->root;
165 struct btrfs_trans_handle *trans;
166 unsigned int flags, oldflags;
167 int ret;
168
169 if (btrfs_root_readonly(root))
170 return -EROFS;
171
172 if (copy_from_user(&flags, arg, sizeof(flags)))
173 return -EFAULT;
174
175 ret = check_flags(flags);
176 if (ret)
177 return ret;
178
179 if (!is_owner_or_cap(inode))
180 return -EACCES;
181
182 mutex_lock(&inode->i_mutex);
183
184 flags = btrfs_mask_flags(inode->i_mode, flags);
185 oldflags = btrfs_flags_to_ioctl(ip->flags);
186 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
187 if (!capable(CAP_LINUX_IMMUTABLE)) {
188 ret = -EPERM;
189 goto out_unlock;
190 }
191 }
192
193 ret = mnt_want_write(file->f_path.mnt);
194 if (ret)
195 goto out_unlock;
196
197 if (flags & FS_SYNC_FL)
198 ip->flags |= BTRFS_INODE_SYNC;
199 else
200 ip->flags &= ~BTRFS_INODE_SYNC;
201 if (flags & FS_IMMUTABLE_FL)
202 ip->flags |= BTRFS_INODE_IMMUTABLE;
203 else
204 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
205 if (flags & FS_APPEND_FL)
206 ip->flags |= BTRFS_INODE_APPEND;
207 else
208 ip->flags &= ~BTRFS_INODE_APPEND;
209 if (flags & FS_NODUMP_FL)
210 ip->flags |= BTRFS_INODE_NODUMP;
211 else
212 ip->flags &= ~BTRFS_INODE_NODUMP;
213 if (flags & FS_NOATIME_FL)
214 ip->flags |= BTRFS_INODE_NOATIME;
215 else
216 ip->flags &= ~BTRFS_INODE_NOATIME;
217 if (flags & FS_DIRSYNC_FL)
218 ip->flags |= BTRFS_INODE_DIRSYNC;
219 else
220 ip->flags &= ~BTRFS_INODE_DIRSYNC;
221
222 /*
223 * The COMPRESS flag can only be changed by users, while the NOCOMPRESS
224 * flag may be changed automatically if compression code won't make
225 * things smaller.
226 */
227 if (flags & FS_NOCOMP_FL) {
228 ip->flags &= ~BTRFS_INODE_COMPRESS;
229 ip->flags |= BTRFS_INODE_NOCOMPRESS;
230 } else if (flags & FS_COMPR_FL) {
231 ip->flags |= BTRFS_INODE_COMPRESS;
232 ip->flags &= ~BTRFS_INODE_NOCOMPRESS;
233 }
234 if (flags & FS_NOCOW_FL)
235 ip->flags |= BTRFS_INODE_NODATACOW;
236 else if (flags & FS_COW_FL)
237 ip->flags &= ~BTRFS_INODE_NODATACOW;
238
239 trans = btrfs_join_transaction(root, 1);
240 BUG_ON(IS_ERR(trans));
241
242 ret = btrfs_update_inode(trans, root, inode);
243 BUG_ON(ret);
244
245 btrfs_update_iflags(inode);
246 inode->i_ctime = CURRENT_TIME;
247 btrfs_end_transaction(trans, root);
248
249 mnt_drop_write(file->f_path.mnt);
250 out_unlock:
251 mutex_unlock(&inode->i_mutex);
252 return 0;
253 }
254
255 static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
256 {
257 struct inode *inode = file->f_path.dentry->d_inode;
258
259 return put_user(inode->i_generation, arg);
260 }
261
262 static noinline int btrfs_ioctl_fitrim(struct file *file, void __user *arg)
263 {
264 struct btrfs_root *root = fdentry(file)->d_sb->s_fs_info;
265 struct btrfs_fs_info *fs_info = root->fs_info;
266 struct btrfs_device *device;
267 struct request_queue *q;
268 struct fstrim_range range;
269 u64 minlen = ULLONG_MAX;
270 u64 num_devices = 0;
271 int ret;
272
273 if (!capable(CAP_SYS_ADMIN))
274 return -EPERM;
275
276 mutex_lock(&fs_info->fs_devices->device_list_mutex);
277 list_for_each_entry(device, &fs_info->fs_devices->devices, dev_list) {
278 if (!device->bdev)
279 continue;
280 q = bdev_get_queue(device->bdev);
281 if (blk_queue_discard(q)) {
282 num_devices++;
283 minlen = min((u64)q->limits.discard_granularity,
284 minlen);
285 }
286 }
287 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
288 if (!num_devices)
289 return -EOPNOTSUPP;
290
291 if (copy_from_user(&range, arg, sizeof(range)))
292 return -EFAULT;
293
294 range.minlen = max(range.minlen, minlen);
295 ret = btrfs_trim_fs(root, &range);
296 if (ret < 0)
297 return ret;
298
299 if (copy_to_user(arg, &range, sizeof(range)))
300 return -EFAULT;
301
302 return 0;
303 }
304
305 static noinline int create_subvol(struct btrfs_root *root,
306 struct dentry *dentry,
307 char *name, int namelen,
308 u64 *async_transid)
309 {
310 struct btrfs_trans_handle *trans;
311 struct btrfs_key key;
312 struct btrfs_root_item root_item;
313 struct btrfs_inode_item *inode_item;
314 struct extent_buffer *leaf;
315 struct btrfs_root *new_root;
316 struct dentry *parent = dget_parent(dentry);
317 struct inode *dir;
318 int ret;
319 int err;
320 u64 objectid;
321 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
322 u64 index = 0;
323
324 ret = btrfs_find_free_objectid(NULL, root->fs_info->tree_root,
325 0, &objectid);
326 if (ret) {
327 dput(parent);
328 return ret;
329 }
330
331 dir = parent->d_inode;
332
333 /*
334 * 1 - inode item
335 * 2 - refs
336 * 1 - root item
337 * 2 - dir items
338 */
339 trans = btrfs_start_transaction(root, 6);
340 if (IS_ERR(trans)) {
341 dput(parent);
342 return PTR_ERR(trans);
343 }
344
345 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
346 0, objectid, NULL, 0, 0, 0);
347 if (IS_ERR(leaf)) {
348 ret = PTR_ERR(leaf);
349 goto fail;
350 }
351
352 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
353 btrfs_set_header_bytenr(leaf, leaf->start);
354 btrfs_set_header_generation(leaf, trans->transid);
355 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
356 btrfs_set_header_owner(leaf, objectid);
357
358 write_extent_buffer(leaf, root->fs_info->fsid,
359 (unsigned long)btrfs_header_fsid(leaf),
360 BTRFS_FSID_SIZE);
361 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
362 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
363 BTRFS_UUID_SIZE);
364 btrfs_mark_buffer_dirty(leaf);
365
366 inode_item = &root_item.inode;
367 memset(inode_item, 0, sizeof(*inode_item));
368 inode_item->generation = cpu_to_le64(1);
369 inode_item->size = cpu_to_le64(3);
370 inode_item->nlink = cpu_to_le32(1);
371 inode_item->nbytes = cpu_to_le64(root->leafsize);
372 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
373
374 btrfs_set_root_bytenr(&root_item, leaf->start);
375 btrfs_set_root_generation(&root_item, trans->transid);
376 btrfs_set_root_level(&root_item, 0);
377 btrfs_set_root_refs(&root_item, 1);
378 btrfs_set_root_used(&root_item, leaf->len);
379 btrfs_set_root_last_snapshot(&root_item, 0);
380
381 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
382 root_item.drop_level = 0;
383
384 btrfs_tree_unlock(leaf);
385 free_extent_buffer(leaf);
386 leaf = NULL;
387
388 btrfs_set_root_dirid(&root_item, new_dirid);
389
390 key.objectid = objectid;
391 key.offset = 0;
392 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
393 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
394 &root_item);
395 if (ret)
396 goto fail;
397
398 key.offset = (u64)-1;
399 new_root = btrfs_read_fs_root_no_name(root->fs_info, &key);
400 BUG_ON(IS_ERR(new_root));
401
402 btrfs_record_root_in_trans(trans, new_root);
403
404 ret = btrfs_create_subvol_root(trans, new_root, new_dirid,
405 BTRFS_I(dir)->block_group);
406 /*
407 * insert the directory item
408 */
409 ret = btrfs_set_inode_index(dir, &index);
410 BUG_ON(ret);
411
412 ret = btrfs_insert_dir_item(trans, root,
413 name, namelen, dir->i_ino, &key,
414 BTRFS_FT_DIR, index);
415 if (ret)
416 goto fail;
417
418 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
419 ret = btrfs_update_inode(trans, root, dir);
420 BUG_ON(ret);
421
422 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
423 objectid, root->root_key.objectid,
424 dir->i_ino, index, name, namelen);
425
426 BUG_ON(ret);
427
428 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
429 fail:
430 dput(parent);
431 if (async_transid) {
432 *async_transid = trans->transid;
433 err = btrfs_commit_transaction_async(trans, root, 1);
434 } else {
435 err = btrfs_commit_transaction(trans, root);
436 }
437 if (err && !ret)
438 ret = err;
439 return ret;
440 }
441
442 static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
443 char *name, int namelen, u64 *async_transid,
444 bool readonly)
445 {
446 struct inode *inode;
447 struct dentry *parent;
448 struct btrfs_pending_snapshot *pending_snapshot;
449 struct btrfs_trans_handle *trans;
450 int ret;
451
452 if (!root->ref_cows)
453 return -EINVAL;
454
455 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
456 if (!pending_snapshot)
457 return -ENOMEM;
458
459 btrfs_init_block_rsv(&pending_snapshot->block_rsv);
460 pending_snapshot->dentry = dentry;
461 pending_snapshot->root = root;
462 pending_snapshot->readonly = readonly;
463
464 trans = btrfs_start_transaction(root->fs_info->extent_root, 5);
465 if (IS_ERR(trans)) {
466 ret = PTR_ERR(trans);
467 goto fail;
468 }
469
470 ret = btrfs_snap_reserve_metadata(trans, pending_snapshot);
471 BUG_ON(ret);
472
473 list_add(&pending_snapshot->list,
474 &trans->transaction->pending_snapshots);
475 if (async_transid) {
476 *async_transid = trans->transid;
477 ret = btrfs_commit_transaction_async(trans,
478 root->fs_info->extent_root, 1);
479 } else {
480 ret = btrfs_commit_transaction(trans,
481 root->fs_info->extent_root);
482 }
483 BUG_ON(ret);
484
485 ret = pending_snapshot->error;
486 if (ret)
487 goto fail;
488
489 ret = btrfs_orphan_cleanup(pending_snapshot->snap);
490 if (ret)
491 goto fail;
492
493 parent = dget_parent(dentry);
494 inode = btrfs_lookup_dentry(parent->d_inode, dentry);
495 dput(parent);
496 if (IS_ERR(inode)) {
497 ret = PTR_ERR(inode);
498 goto fail;
499 }
500 BUG_ON(!inode);
501 d_instantiate(dentry, inode);
502 ret = 0;
503 fail:
504 kfree(pending_snapshot);
505 return ret;
506 }
507
508 /* copy of check_sticky in fs/namei.c()
509 * It's inline, so penalty for filesystems that don't use sticky bit is
510 * minimal.
511 */
512 static inline int btrfs_check_sticky(struct inode *dir, struct inode *inode)
513 {
514 uid_t fsuid = current_fsuid();
515
516 if (!(dir->i_mode & S_ISVTX))
517 return 0;
518 if (inode->i_uid == fsuid)
519 return 0;
520 if (dir->i_uid == fsuid)
521 return 0;
522 return !capable(CAP_FOWNER);
523 }
524
525 /* copy of may_delete in fs/namei.c()
526 * Check whether we can remove a link victim from directory dir, check
527 * whether the type of victim is right.
528 * 1. We can't do it if dir is read-only (done in permission())
529 * 2. We should have write and exec permissions on dir
530 * 3. We can't remove anything from append-only dir
531 * 4. We can't do anything with immutable dir (done in permission())
532 * 5. If the sticky bit on dir is set we should either
533 * a. be owner of dir, or
534 * b. be owner of victim, or
535 * c. have CAP_FOWNER capability
536 * 6. If the victim is append-only or immutable we can't do antyhing with
537 * links pointing to it.
538 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
539 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
540 * 9. We can't remove a root or mountpoint.
541 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
542 * nfs_async_unlink().
543 */
544
545 static int btrfs_may_delete(struct inode *dir,struct dentry *victim,int isdir)
546 {
547 int error;
548
549 if (!victim->d_inode)
550 return -ENOENT;
551
552 BUG_ON(victim->d_parent->d_inode != dir);
553 audit_inode_child(victim, dir);
554
555 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
556 if (error)
557 return error;
558 if (IS_APPEND(dir))
559 return -EPERM;
560 if (btrfs_check_sticky(dir, victim->d_inode)||
561 IS_APPEND(victim->d_inode)||
562 IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode))
563 return -EPERM;
564 if (isdir) {
565 if (!S_ISDIR(victim->d_inode->i_mode))
566 return -ENOTDIR;
567 if (IS_ROOT(victim))
568 return -EBUSY;
569 } else if (S_ISDIR(victim->d_inode->i_mode))
570 return -EISDIR;
571 if (IS_DEADDIR(dir))
572 return -ENOENT;
573 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
574 return -EBUSY;
575 return 0;
576 }
577
578 /* copy of may_create in fs/namei.c() */
579 static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
580 {
581 if (child->d_inode)
582 return -EEXIST;
583 if (IS_DEADDIR(dir))
584 return -ENOENT;
585 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
586 }
587
588 /*
589 * Create a new subvolume below @parent. This is largely modeled after
590 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
591 * inside this filesystem so it's quite a bit simpler.
592 */
593 static noinline int btrfs_mksubvol(struct path *parent,
594 char *name, int namelen,
595 struct btrfs_root *snap_src,
596 u64 *async_transid, bool readonly)
597 {
598 struct inode *dir = parent->dentry->d_inode;
599 struct dentry *dentry;
600 int error;
601
602 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
603
604 dentry = lookup_one_len(name, parent->dentry, namelen);
605 error = PTR_ERR(dentry);
606 if (IS_ERR(dentry))
607 goto out_unlock;
608
609 error = -EEXIST;
610 if (dentry->d_inode)
611 goto out_dput;
612
613 error = mnt_want_write(parent->mnt);
614 if (error)
615 goto out_dput;
616
617 error = btrfs_may_create(dir, dentry);
618 if (error)
619 goto out_drop_write;
620
621 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
622
623 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
624 goto out_up_read;
625
626 if (snap_src) {
627 error = create_snapshot(snap_src, dentry,
628 name, namelen, async_transid, readonly);
629 } else {
630 error = create_subvol(BTRFS_I(dir)->root, dentry,
631 name, namelen, async_transid);
632 }
633 if (!error)
634 fsnotify_mkdir(dir, dentry);
635 out_up_read:
636 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
637 out_drop_write:
638 mnt_drop_write(parent->mnt);
639 out_dput:
640 dput(dentry);
641 out_unlock:
642 mutex_unlock(&dir->i_mutex);
643 return error;
644 }
645
646 static int should_defrag_range(struct inode *inode, u64 start, u64 len,
647 int thresh, u64 *last_len, u64 *skip,
648 u64 *defrag_end)
649 {
650 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
651 struct extent_map *em = NULL;
652 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
653 int ret = 1;
654
655
656 if (thresh == 0)
657 thresh = 256 * 1024;
658
659 /*
660 * make sure that once we start defragging and extent, we keep on
661 * defragging it
662 */
663 if (start < *defrag_end)
664 return 1;
665
666 *skip = 0;
667
668 /*
669 * hopefully we have this extent in the tree already, try without
670 * the full extent lock
671 */
672 read_lock(&em_tree->lock);
673 em = lookup_extent_mapping(em_tree, start, len);
674 read_unlock(&em_tree->lock);
675
676 if (!em) {
677 /* get the big lock and read metadata off disk */
678 lock_extent(io_tree, start, start + len - 1, GFP_NOFS);
679 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
680 unlock_extent(io_tree, start, start + len - 1, GFP_NOFS);
681
682 if (IS_ERR(em))
683 return 0;
684 }
685
686 /* this will cover holes, and inline extents */
687 if (em->block_start >= EXTENT_MAP_LAST_BYTE)
688 ret = 0;
689
690 /*
691 * we hit a real extent, if it is big don't bother defragging it again
692 */
693 if ((*last_len == 0 || *last_len >= thresh) && em->len >= thresh)
694 ret = 0;
695
696 /*
697 * last_len ends up being a counter of how many bytes we've defragged.
698 * every time we choose not to defrag an extent, we reset *last_len
699 * so that the next tiny extent will force a defrag.
700 *
701 * The end result of this is that tiny extents before a single big
702 * extent will force at least part of that big extent to be defragged.
703 */
704 if (ret) {
705 *last_len += len;
706 *defrag_end = extent_map_end(em);
707 } else {
708 *last_len = 0;
709 *skip = extent_map_end(em);
710 *defrag_end = 0;
711 }
712
713 free_extent_map(em);
714 return ret;
715 }
716
717 static int btrfs_defrag_file(struct file *file,
718 struct btrfs_ioctl_defrag_range_args *range)
719 {
720 struct inode *inode = fdentry(file)->d_inode;
721 struct btrfs_root *root = BTRFS_I(inode)->root;
722 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
723 struct btrfs_ordered_extent *ordered;
724 struct page *page;
725 struct btrfs_super_block *disk_super;
726 unsigned long last_index;
727 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
728 unsigned long total_read = 0;
729 u64 features;
730 u64 page_start;
731 u64 page_end;
732 u64 last_len = 0;
733 u64 skip = 0;
734 u64 defrag_end = 0;
735 unsigned long i;
736 int ret;
737 int compress_type = BTRFS_COMPRESS_ZLIB;
738
739 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS) {
740 if (range->compress_type > BTRFS_COMPRESS_TYPES)
741 return -EINVAL;
742 if (range->compress_type)
743 compress_type = range->compress_type;
744 }
745
746 if (inode->i_size == 0)
747 return 0;
748
749 if (range->start + range->len > range->start) {
750 last_index = min_t(u64, inode->i_size - 1,
751 range->start + range->len - 1) >> PAGE_CACHE_SHIFT;
752 } else {
753 last_index = (inode->i_size - 1) >> PAGE_CACHE_SHIFT;
754 }
755
756 i = range->start >> PAGE_CACHE_SHIFT;
757 while (i <= last_index) {
758 if (!should_defrag_range(inode, (u64)i << PAGE_CACHE_SHIFT,
759 PAGE_CACHE_SIZE,
760 range->extent_thresh,
761 &last_len, &skip,
762 &defrag_end)) {
763 unsigned long next;
764 /*
765 * the should_defrag function tells us how much to skip
766 * bump our counter by the suggested amount
767 */
768 next = (skip + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
769 i = max(i + 1, next);
770 continue;
771 }
772
773 if (total_read % ra_pages == 0) {
774 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
775 min(last_index, i + ra_pages - 1));
776 }
777 total_read++;
778 mutex_lock(&inode->i_mutex);
779 if (range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)
780 BTRFS_I(inode)->force_compress = compress_type;
781
782 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
783 if (ret)
784 goto err_unlock;
785 again:
786 if (inode->i_size == 0 ||
787 i > ((inode->i_size - 1) >> PAGE_CACHE_SHIFT)) {
788 ret = 0;
789 goto err_reservations;
790 }
791
792 page = grab_cache_page(inode->i_mapping, i);
793 if (!page) {
794 ret = -ENOMEM;
795 goto err_reservations;
796 }
797
798 if (!PageUptodate(page)) {
799 btrfs_readpage(NULL, page);
800 lock_page(page);
801 if (!PageUptodate(page)) {
802 unlock_page(page);
803 page_cache_release(page);
804 ret = -EIO;
805 goto err_reservations;
806 }
807 }
808
809 if (page->mapping != inode->i_mapping) {
810 unlock_page(page);
811 page_cache_release(page);
812 goto again;
813 }
814
815 wait_on_page_writeback(page);
816
817 if (PageDirty(page)) {
818 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
819 goto loop_unlock;
820 }
821
822 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
823 page_end = page_start + PAGE_CACHE_SIZE - 1;
824 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
825
826 ordered = btrfs_lookup_ordered_extent(inode, page_start);
827 if (ordered) {
828 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
829 unlock_page(page);
830 page_cache_release(page);
831 btrfs_start_ordered_extent(inode, ordered, 1);
832 btrfs_put_ordered_extent(ordered);
833 goto again;
834 }
835 set_page_extent_mapped(page);
836
837 /*
838 * this makes sure page_mkwrite is called on the
839 * page if it is dirtied again later
840 */
841 clear_page_dirty_for_io(page);
842 clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start,
843 page_end, EXTENT_DIRTY | EXTENT_DELALLOC |
844 EXTENT_DO_ACCOUNTING, GFP_NOFS);
845
846 btrfs_set_extent_delalloc(inode, page_start, page_end, NULL);
847 ClearPageChecked(page);
848 set_page_dirty(page);
849 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
850
851 loop_unlock:
852 unlock_page(page);
853 page_cache_release(page);
854 mutex_unlock(&inode->i_mutex);
855
856 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
857 i++;
858 }
859
860 if ((range->flags & BTRFS_DEFRAG_RANGE_START_IO))
861 filemap_flush(inode->i_mapping);
862
863 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
864 /* the filemap_flush will queue IO into the worker threads, but
865 * we have to make sure the IO is actually started and that
866 * ordered extents get created before we return
867 */
868 atomic_inc(&root->fs_info->async_submit_draining);
869 while (atomic_read(&root->fs_info->nr_async_submits) ||
870 atomic_read(&root->fs_info->async_delalloc_pages)) {
871 wait_event(root->fs_info->async_submit_wait,
872 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
873 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
874 }
875 atomic_dec(&root->fs_info->async_submit_draining);
876
877 mutex_lock(&inode->i_mutex);
878 BTRFS_I(inode)->force_compress = BTRFS_COMPRESS_NONE;
879 mutex_unlock(&inode->i_mutex);
880 }
881
882 disk_super = &root->fs_info->super_copy;
883 features = btrfs_super_incompat_flags(disk_super);
884 if (range->compress_type == BTRFS_COMPRESS_LZO) {
885 features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
886 btrfs_set_super_incompat_flags(disk_super, features);
887 }
888
889 return 0;
890
891 err_reservations:
892 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
893 err_unlock:
894 mutex_unlock(&inode->i_mutex);
895 return ret;
896 }
897
898 static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
899 void __user *arg)
900 {
901 u64 new_size;
902 u64 old_size;
903 u64 devid = 1;
904 struct btrfs_ioctl_vol_args *vol_args;
905 struct btrfs_trans_handle *trans;
906 struct btrfs_device *device = NULL;
907 char *sizestr;
908 char *devstr = NULL;
909 int ret = 0;
910 int mod = 0;
911
912 if (root->fs_info->sb->s_flags & MS_RDONLY)
913 return -EROFS;
914
915 if (!capable(CAP_SYS_ADMIN))
916 return -EPERM;
917
918 vol_args = memdup_user(arg, sizeof(*vol_args));
919 if (IS_ERR(vol_args))
920 return PTR_ERR(vol_args);
921
922 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
923
924 mutex_lock(&root->fs_info->volume_mutex);
925 sizestr = vol_args->name;
926 devstr = strchr(sizestr, ':');
927 if (devstr) {
928 char *end;
929 sizestr = devstr + 1;
930 *devstr = '\0';
931 devstr = vol_args->name;
932 devid = simple_strtoull(devstr, &end, 10);
933 printk(KERN_INFO "resizing devid %llu\n",
934 (unsigned long long)devid);
935 }
936 device = btrfs_find_device(root, devid, NULL, NULL);
937 if (!device) {
938 printk(KERN_INFO "resizer unable to find device %llu\n",
939 (unsigned long long)devid);
940 ret = -EINVAL;
941 goto out_unlock;
942 }
943 if (!strcmp(sizestr, "max"))
944 new_size = device->bdev->bd_inode->i_size;
945 else {
946 if (sizestr[0] == '-') {
947 mod = -1;
948 sizestr++;
949 } else if (sizestr[0] == '+') {
950 mod = 1;
951 sizestr++;
952 }
953 new_size = memparse(sizestr, NULL);
954 if (new_size == 0) {
955 ret = -EINVAL;
956 goto out_unlock;
957 }
958 }
959
960 old_size = device->total_bytes;
961
962 if (mod < 0) {
963 if (new_size > old_size) {
964 ret = -EINVAL;
965 goto out_unlock;
966 }
967 new_size = old_size - new_size;
968 } else if (mod > 0) {
969 new_size = old_size + new_size;
970 }
971
972 if (new_size < 256 * 1024 * 1024) {
973 ret = -EINVAL;
974 goto out_unlock;
975 }
976 if (new_size > device->bdev->bd_inode->i_size) {
977 ret = -EFBIG;
978 goto out_unlock;
979 }
980
981 do_div(new_size, root->sectorsize);
982 new_size *= root->sectorsize;
983
984 printk(KERN_INFO "new size for %s is %llu\n",
985 device->name, (unsigned long long)new_size);
986
987 if (new_size > old_size) {
988 trans = btrfs_start_transaction(root, 0);
989 if (IS_ERR(trans)) {
990 ret = PTR_ERR(trans);
991 goto out_unlock;
992 }
993 ret = btrfs_grow_device(trans, device, new_size);
994 btrfs_commit_transaction(trans, root);
995 } else {
996 ret = btrfs_shrink_device(device, new_size);
997 }
998
999 out_unlock:
1000 mutex_unlock(&root->fs_info->volume_mutex);
1001 kfree(vol_args);
1002 return ret;
1003 }
1004
1005 static noinline int btrfs_ioctl_snap_create_transid(struct file *file,
1006 char *name,
1007 unsigned long fd,
1008 int subvol,
1009 u64 *transid,
1010 bool readonly)
1011 {
1012 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
1013 struct file *src_file;
1014 int namelen;
1015 int ret = 0;
1016
1017 if (root->fs_info->sb->s_flags & MS_RDONLY)
1018 return -EROFS;
1019
1020 namelen = strlen(name);
1021 if (strchr(name, '/')) {
1022 ret = -EINVAL;
1023 goto out;
1024 }
1025
1026 if (subvol) {
1027 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1028 NULL, transid, readonly);
1029 } else {
1030 struct inode *src_inode;
1031 src_file = fget(fd);
1032 if (!src_file) {
1033 ret = -EINVAL;
1034 goto out;
1035 }
1036
1037 src_inode = src_file->f_path.dentry->d_inode;
1038 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
1039 printk(KERN_INFO "btrfs: Snapshot src from "
1040 "another FS\n");
1041 ret = -EINVAL;
1042 fput(src_file);
1043 goto out;
1044 }
1045 ret = btrfs_mksubvol(&file->f_path, name, namelen,
1046 BTRFS_I(src_inode)->root,
1047 transid, readonly);
1048 fput(src_file);
1049 }
1050 out:
1051 return ret;
1052 }
1053
1054 static noinline int btrfs_ioctl_snap_create(struct file *file,
1055 void __user *arg, int subvol)
1056 {
1057 struct btrfs_ioctl_vol_args *vol_args;
1058 int ret;
1059
1060 vol_args = memdup_user(arg, sizeof(*vol_args));
1061 if (IS_ERR(vol_args))
1062 return PTR_ERR(vol_args);
1063 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1064
1065 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1066 vol_args->fd, subvol,
1067 NULL, false);
1068
1069 kfree(vol_args);
1070 return ret;
1071 }
1072
1073 static noinline int btrfs_ioctl_snap_create_v2(struct file *file,
1074 void __user *arg, int subvol)
1075 {
1076 struct btrfs_ioctl_vol_args_v2 *vol_args;
1077 int ret;
1078 u64 transid = 0;
1079 u64 *ptr = NULL;
1080 bool readonly = false;
1081
1082 vol_args = memdup_user(arg, sizeof(*vol_args));
1083 if (IS_ERR(vol_args))
1084 return PTR_ERR(vol_args);
1085 vol_args->name[BTRFS_SUBVOL_NAME_MAX] = '\0';
1086
1087 if (vol_args->flags &
1088 ~(BTRFS_SUBVOL_CREATE_ASYNC | BTRFS_SUBVOL_RDONLY)) {
1089 ret = -EOPNOTSUPP;
1090 goto out;
1091 }
1092
1093 if (vol_args->flags & BTRFS_SUBVOL_CREATE_ASYNC)
1094 ptr = &transid;
1095 if (vol_args->flags & BTRFS_SUBVOL_RDONLY)
1096 readonly = true;
1097
1098 ret = btrfs_ioctl_snap_create_transid(file, vol_args->name,
1099 vol_args->fd, subvol,
1100 ptr, readonly);
1101
1102 if (ret == 0 && ptr &&
1103 copy_to_user(arg +
1104 offsetof(struct btrfs_ioctl_vol_args_v2,
1105 transid), ptr, sizeof(*ptr)))
1106 ret = -EFAULT;
1107 out:
1108 kfree(vol_args);
1109 return ret;
1110 }
1111
1112 static noinline int btrfs_ioctl_subvol_getflags(struct file *file,
1113 void __user *arg)
1114 {
1115 struct inode *inode = fdentry(file)->d_inode;
1116 struct btrfs_root *root = BTRFS_I(inode)->root;
1117 int ret = 0;
1118 u64 flags = 0;
1119
1120 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1121 return -EINVAL;
1122
1123 down_read(&root->fs_info->subvol_sem);
1124 if (btrfs_root_readonly(root))
1125 flags |= BTRFS_SUBVOL_RDONLY;
1126 up_read(&root->fs_info->subvol_sem);
1127
1128 if (copy_to_user(arg, &flags, sizeof(flags)))
1129 ret = -EFAULT;
1130
1131 return ret;
1132 }
1133
1134 static noinline int btrfs_ioctl_subvol_setflags(struct file *file,
1135 void __user *arg)
1136 {
1137 struct inode *inode = fdentry(file)->d_inode;
1138 struct btrfs_root *root = BTRFS_I(inode)->root;
1139 struct btrfs_trans_handle *trans;
1140 u64 root_flags;
1141 u64 flags;
1142 int ret = 0;
1143
1144 if (root->fs_info->sb->s_flags & MS_RDONLY)
1145 return -EROFS;
1146
1147 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID)
1148 return -EINVAL;
1149
1150 if (copy_from_user(&flags, arg, sizeof(flags)))
1151 return -EFAULT;
1152
1153 if (flags & BTRFS_SUBVOL_CREATE_ASYNC)
1154 return -EINVAL;
1155
1156 if (flags & ~BTRFS_SUBVOL_RDONLY)
1157 return -EOPNOTSUPP;
1158
1159 if (!is_owner_or_cap(inode))
1160 return -EACCES;
1161
1162 down_write(&root->fs_info->subvol_sem);
1163
1164 /* nothing to do */
1165 if (!!(flags & BTRFS_SUBVOL_RDONLY) == btrfs_root_readonly(root))
1166 goto out;
1167
1168 root_flags = btrfs_root_flags(&root->root_item);
1169 if (flags & BTRFS_SUBVOL_RDONLY)
1170 btrfs_set_root_flags(&root->root_item,
1171 root_flags | BTRFS_ROOT_SUBVOL_RDONLY);
1172 else
1173 btrfs_set_root_flags(&root->root_item,
1174 root_flags & ~BTRFS_ROOT_SUBVOL_RDONLY);
1175
1176 trans = btrfs_start_transaction(root, 1);
1177 if (IS_ERR(trans)) {
1178 ret = PTR_ERR(trans);
1179 goto out_reset;
1180 }
1181
1182 ret = btrfs_update_root(trans, root->fs_info->tree_root,
1183 &root->root_key, &root->root_item);
1184
1185 btrfs_commit_transaction(trans, root);
1186 out_reset:
1187 if (ret)
1188 btrfs_set_root_flags(&root->root_item, root_flags);
1189 out:
1190 up_write(&root->fs_info->subvol_sem);
1191 return ret;
1192 }
1193
1194 /*
1195 * helper to check if the subvolume references other subvolumes
1196 */
1197 static noinline int may_destroy_subvol(struct btrfs_root *root)
1198 {
1199 struct btrfs_path *path;
1200 struct btrfs_key key;
1201 int ret;
1202
1203 path = btrfs_alloc_path();
1204 if (!path)
1205 return -ENOMEM;
1206
1207 key.objectid = root->root_key.objectid;
1208 key.type = BTRFS_ROOT_REF_KEY;
1209 key.offset = (u64)-1;
1210
1211 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
1212 &key, path, 0, 0);
1213 if (ret < 0)
1214 goto out;
1215 BUG_ON(ret == 0);
1216
1217 ret = 0;
1218 if (path->slots[0] > 0) {
1219 path->slots[0]--;
1220 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1221 if (key.objectid == root->root_key.objectid &&
1222 key.type == BTRFS_ROOT_REF_KEY)
1223 ret = -ENOTEMPTY;
1224 }
1225 out:
1226 btrfs_free_path(path);
1227 return ret;
1228 }
1229
1230 static noinline int key_in_sk(struct btrfs_key *key,
1231 struct btrfs_ioctl_search_key *sk)
1232 {
1233 struct btrfs_key test;
1234 int ret;
1235
1236 test.objectid = sk->min_objectid;
1237 test.type = sk->min_type;
1238 test.offset = sk->min_offset;
1239
1240 ret = btrfs_comp_cpu_keys(key, &test);
1241 if (ret < 0)
1242 return 0;
1243
1244 test.objectid = sk->max_objectid;
1245 test.type = sk->max_type;
1246 test.offset = sk->max_offset;
1247
1248 ret = btrfs_comp_cpu_keys(key, &test);
1249 if (ret > 0)
1250 return 0;
1251 return 1;
1252 }
1253
1254 static noinline int copy_to_sk(struct btrfs_root *root,
1255 struct btrfs_path *path,
1256 struct btrfs_key *key,
1257 struct btrfs_ioctl_search_key *sk,
1258 char *buf,
1259 unsigned long *sk_offset,
1260 int *num_found)
1261 {
1262 u64 found_transid;
1263 struct extent_buffer *leaf;
1264 struct btrfs_ioctl_search_header sh;
1265 unsigned long item_off;
1266 unsigned long item_len;
1267 int nritems;
1268 int i;
1269 int slot;
1270 int found = 0;
1271 int ret = 0;
1272
1273 leaf = path->nodes[0];
1274 slot = path->slots[0];
1275 nritems = btrfs_header_nritems(leaf);
1276
1277 if (btrfs_header_generation(leaf) > sk->max_transid) {
1278 i = nritems;
1279 goto advance_key;
1280 }
1281 found_transid = btrfs_header_generation(leaf);
1282
1283 for (i = slot; i < nritems; i++) {
1284 item_off = btrfs_item_ptr_offset(leaf, i);
1285 item_len = btrfs_item_size_nr(leaf, i);
1286
1287 if (item_len > BTRFS_SEARCH_ARGS_BUFSIZE)
1288 item_len = 0;
1289
1290 if (sizeof(sh) + item_len + *sk_offset >
1291 BTRFS_SEARCH_ARGS_BUFSIZE) {
1292 ret = 1;
1293 goto overflow;
1294 }
1295
1296 btrfs_item_key_to_cpu(leaf, key, i);
1297 if (!key_in_sk(key, sk))
1298 continue;
1299
1300 sh.objectid = key->objectid;
1301 sh.offset = key->offset;
1302 sh.type = key->type;
1303 sh.len = item_len;
1304 sh.transid = found_transid;
1305
1306 /* copy search result header */
1307 memcpy(buf + *sk_offset, &sh, sizeof(sh));
1308 *sk_offset += sizeof(sh);
1309
1310 if (item_len) {
1311 char *p = buf + *sk_offset;
1312 /* copy the item */
1313 read_extent_buffer(leaf, p,
1314 item_off, item_len);
1315 *sk_offset += item_len;
1316 }
1317 found++;
1318
1319 if (*num_found >= sk->nr_items)
1320 break;
1321 }
1322 advance_key:
1323 ret = 0;
1324 if (key->offset < (u64)-1 && key->offset < sk->max_offset)
1325 key->offset++;
1326 else if (key->type < (u8)-1 && key->type < sk->max_type) {
1327 key->offset = 0;
1328 key->type++;
1329 } else if (key->objectid < (u64)-1 && key->objectid < sk->max_objectid) {
1330 key->offset = 0;
1331 key->type = 0;
1332 key->objectid++;
1333 } else
1334 ret = 1;
1335 overflow:
1336 *num_found += found;
1337 return ret;
1338 }
1339
1340 static noinline int search_ioctl(struct inode *inode,
1341 struct btrfs_ioctl_search_args *args)
1342 {
1343 struct btrfs_root *root;
1344 struct btrfs_key key;
1345 struct btrfs_key max_key;
1346 struct btrfs_path *path;
1347 struct btrfs_ioctl_search_key *sk = &args->key;
1348 struct btrfs_fs_info *info = BTRFS_I(inode)->root->fs_info;
1349 int ret;
1350 int num_found = 0;
1351 unsigned long sk_offset = 0;
1352
1353 path = btrfs_alloc_path();
1354 if (!path)
1355 return -ENOMEM;
1356
1357 if (sk->tree_id == 0) {
1358 /* search the root of the inode that was passed */
1359 root = BTRFS_I(inode)->root;
1360 } else {
1361 key.objectid = sk->tree_id;
1362 key.type = BTRFS_ROOT_ITEM_KEY;
1363 key.offset = (u64)-1;
1364 root = btrfs_read_fs_root_no_name(info, &key);
1365 if (IS_ERR(root)) {
1366 printk(KERN_ERR "could not find root %llu\n",
1367 sk->tree_id);
1368 btrfs_free_path(path);
1369 return -ENOENT;
1370 }
1371 }
1372
1373 key.objectid = sk->min_objectid;
1374 key.type = sk->min_type;
1375 key.offset = sk->min_offset;
1376
1377 max_key.objectid = sk->max_objectid;
1378 max_key.type = sk->max_type;
1379 max_key.offset = sk->max_offset;
1380
1381 path->keep_locks = 1;
1382
1383 while(1) {
1384 ret = btrfs_search_forward(root, &key, &max_key, path, 0,
1385 sk->min_transid);
1386 if (ret != 0) {
1387 if (ret > 0)
1388 ret = 0;
1389 goto err;
1390 }
1391 ret = copy_to_sk(root, path, &key, sk, args->buf,
1392 &sk_offset, &num_found);
1393 btrfs_release_path(root, path);
1394 if (ret || num_found >= sk->nr_items)
1395 break;
1396
1397 }
1398 ret = 0;
1399 err:
1400 sk->nr_items = num_found;
1401 btrfs_free_path(path);
1402 return ret;
1403 }
1404
1405 static noinline int btrfs_ioctl_tree_search(struct file *file,
1406 void __user *argp)
1407 {
1408 struct btrfs_ioctl_search_args *args;
1409 struct inode *inode;
1410 int ret;
1411
1412 if (!capable(CAP_SYS_ADMIN))
1413 return -EPERM;
1414
1415 args = memdup_user(argp, sizeof(*args));
1416 if (IS_ERR(args))
1417 return PTR_ERR(args);
1418
1419 inode = fdentry(file)->d_inode;
1420 ret = search_ioctl(inode, args);
1421 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1422 ret = -EFAULT;
1423 kfree(args);
1424 return ret;
1425 }
1426
1427 /*
1428 * Search INODE_REFs to identify path name of 'dirid' directory
1429 * in a 'tree_id' tree. and sets path name to 'name'.
1430 */
1431 static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
1432 u64 tree_id, u64 dirid, char *name)
1433 {
1434 struct btrfs_root *root;
1435 struct btrfs_key key;
1436 char *ptr;
1437 int ret = -1;
1438 int slot;
1439 int len;
1440 int total_len = 0;
1441 struct btrfs_inode_ref *iref;
1442 struct extent_buffer *l;
1443 struct btrfs_path *path;
1444
1445 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
1446 name[0]='\0';
1447 return 0;
1448 }
1449
1450 path = btrfs_alloc_path();
1451 if (!path)
1452 return -ENOMEM;
1453
1454 ptr = &name[BTRFS_INO_LOOKUP_PATH_MAX];
1455
1456 key.objectid = tree_id;
1457 key.type = BTRFS_ROOT_ITEM_KEY;
1458 key.offset = (u64)-1;
1459 root = btrfs_read_fs_root_no_name(info, &key);
1460 if (IS_ERR(root)) {
1461 printk(KERN_ERR "could not find root %llu\n", tree_id);
1462 ret = -ENOENT;
1463 goto out;
1464 }
1465
1466 key.objectid = dirid;
1467 key.type = BTRFS_INODE_REF_KEY;
1468 key.offset = (u64)-1;
1469
1470 while(1) {
1471 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1472 if (ret < 0)
1473 goto out;
1474
1475 l = path->nodes[0];
1476 slot = path->slots[0];
1477 if (ret > 0 && slot > 0)
1478 slot--;
1479 btrfs_item_key_to_cpu(l, &key, slot);
1480
1481 if (ret > 0 && (key.objectid != dirid ||
1482 key.type != BTRFS_INODE_REF_KEY)) {
1483 ret = -ENOENT;
1484 goto out;
1485 }
1486
1487 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
1488 len = btrfs_inode_ref_name_len(l, iref);
1489 ptr -= len + 1;
1490 total_len += len + 1;
1491 if (ptr < name)
1492 goto out;
1493
1494 *(ptr + len) = '/';
1495 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
1496
1497 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
1498 break;
1499
1500 btrfs_release_path(root, path);
1501 key.objectid = key.offset;
1502 key.offset = (u64)-1;
1503 dirid = key.objectid;
1504
1505 }
1506 if (ptr < name)
1507 goto out;
1508 memcpy(name, ptr, total_len);
1509 name[total_len]='\0';
1510 ret = 0;
1511 out:
1512 btrfs_free_path(path);
1513 return ret;
1514 }
1515
1516 static noinline int btrfs_ioctl_ino_lookup(struct file *file,
1517 void __user *argp)
1518 {
1519 struct btrfs_ioctl_ino_lookup_args *args;
1520 struct inode *inode;
1521 int ret;
1522
1523 if (!capable(CAP_SYS_ADMIN))
1524 return -EPERM;
1525
1526 args = memdup_user(argp, sizeof(*args));
1527 if (IS_ERR(args))
1528 return PTR_ERR(args);
1529
1530 inode = fdentry(file)->d_inode;
1531
1532 if (args->treeid == 0)
1533 args->treeid = BTRFS_I(inode)->root->root_key.objectid;
1534
1535 ret = btrfs_search_path_in_tree(BTRFS_I(inode)->root->fs_info,
1536 args->treeid, args->objectid,
1537 args->name);
1538
1539 if (ret == 0 && copy_to_user(argp, args, sizeof(*args)))
1540 ret = -EFAULT;
1541
1542 kfree(args);
1543 return ret;
1544 }
1545
1546 static noinline int btrfs_ioctl_snap_destroy(struct file *file,
1547 void __user *arg)
1548 {
1549 struct dentry *parent = fdentry(file);
1550 struct dentry *dentry;
1551 struct inode *dir = parent->d_inode;
1552 struct inode *inode;
1553 struct btrfs_root *root = BTRFS_I(dir)->root;
1554 struct btrfs_root *dest = NULL;
1555 struct btrfs_ioctl_vol_args *vol_args;
1556 struct btrfs_trans_handle *trans;
1557 int namelen;
1558 int ret;
1559 int err = 0;
1560
1561 vol_args = memdup_user(arg, sizeof(*vol_args));
1562 if (IS_ERR(vol_args))
1563 return PTR_ERR(vol_args);
1564
1565 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1566 namelen = strlen(vol_args->name);
1567 if (strchr(vol_args->name, '/') ||
1568 strncmp(vol_args->name, "..", namelen) == 0) {
1569 err = -EINVAL;
1570 goto out;
1571 }
1572
1573 err = mnt_want_write(file->f_path.mnt);
1574 if (err)
1575 goto out;
1576
1577 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
1578 dentry = lookup_one_len(vol_args->name, parent, namelen);
1579 if (IS_ERR(dentry)) {
1580 err = PTR_ERR(dentry);
1581 goto out_unlock_dir;
1582 }
1583
1584 if (!dentry->d_inode) {
1585 err = -ENOENT;
1586 goto out_dput;
1587 }
1588
1589 inode = dentry->d_inode;
1590 dest = BTRFS_I(inode)->root;
1591 if (!capable(CAP_SYS_ADMIN)){
1592 /*
1593 * Regular user. Only allow this with a special mount
1594 * option, when the user has write+exec access to the
1595 * subvol root, and when rmdir(2) would have been
1596 * allowed.
1597 *
1598 * Note that this is _not_ check that the subvol is
1599 * empty or doesn't contain data that we wouldn't
1600 * otherwise be able to delete.
1601 *
1602 * Users who want to delete empty subvols should try
1603 * rmdir(2).
1604 */
1605 err = -EPERM;
1606 if (!btrfs_test_opt(root, USER_SUBVOL_RM_ALLOWED))
1607 goto out_dput;
1608
1609 /*
1610 * Do not allow deletion if the parent dir is the same
1611 * as the dir to be deleted. That means the ioctl
1612 * must be called on the dentry referencing the root
1613 * of the subvol, not a random directory contained
1614 * within it.
1615 */
1616 err = -EINVAL;
1617 if (root == dest)
1618 goto out_dput;
1619
1620 err = inode_permission(inode, MAY_WRITE | MAY_EXEC);
1621 if (err)
1622 goto out_dput;
1623
1624 /* check if subvolume may be deleted by a non-root user */
1625 err = btrfs_may_delete(dir, dentry, 1);
1626 if (err)
1627 goto out_dput;
1628 }
1629
1630 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
1631 err = -EINVAL;
1632 goto out_dput;
1633 }
1634
1635 mutex_lock(&inode->i_mutex);
1636 err = d_invalidate(dentry);
1637 if (err)
1638 goto out_unlock;
1639
1640 down_write(&root->fs_info->subvol_sem);
1641
1642 err = may_destroy_subvol(dest);
1643 if (err)
1644 goto out_up_write;
1645
1646 trans = btrfs_start_transaction(root, 0);
1647 if (IS_ERR(trans)) {
1648 err = PTR_ERR(trans);
1649 goto out_up_write;
1650 }
1651 trans->block_rsv = &root->fs_info->global_block_rsv;
1652
1653 ret = btrfs_unlink_subvol(trans, root, dir,
1654 dest->root_key.objectid,
1655 dentry->d_name.name,
1656 dentry->d_name.len);
1657 BUG_ON(ret);
1658
1659 btrfs_record_root_in_trans(trans, dest);
1660
1661 memset(&dest->root_item.drop_progress, 0,
1662 sizeof(dest->root_item.drop_progress));
1663 dest->root_item.drop_level = 0;
1664 btrfs_set_root_refs(&dest->root_item, 0);
1665
1666 if (!xchg(&dest->orphan_item_inserted, 1)) {
1667 ret = btrfs_insert_orphan_item(trans,
1668 root->fs_info->tree_root,
1669 dest->root_key.objectid);
1670 BUG_ON(ret);
1671 }
1672
1673 ret = btrfs_end_transaction(trans, root);
1674 BUG_ON(ret);
1675 inode->i_flags |= S_DEAD;
1676 out_up_write:
1677 up_write(&root->fs_info->subvol_sem);
1678 out_unlock:
1679 mutex_unlock(&inode->i_mutex);
1680 if (!err) {
1681 shrink_dcache_sb(root->fs_info->sb);
1682 btrfs_invalidate_inodes(dest);
1683 d_delete(dentry);
1684 }
1685 out_dput:
1686 dput(dentry);
1687 out_unlock_dir:
1688 mutex_unlock(&dir->i_mutex);
1689 mnt_drop_write(file->f_path.mnt);
1690 out:
1691 kfree(vol_args);
1692 return err;
1693 }
1694
1695 static int btrfs_ioctl_defrag(struct file *file, void __user *argp)
1696 {
1697 struct inode *inode = fdentry(file)->d_inode;
1698 struct btrfs_root *root = BTRFS_I(inode)->root;
1699 struct btrfs_ioctl_defrag_range_args *range;
1700 int ret;
1701
1702 if (btrfs_root_readonly(root))
1703 return -EROFS;
1704
1705 ret = mnt_want_write(file->f_path.mnt);
1706 if (ret)
1707 return ret;
1708
1709 switch (inode->i_mode & S_IFMT) {
1710 case S_IFDIR:
1711 if (!capable(CAP_SYS_ADMIN)) {
1712 ret = -EPERM;
1713 goto out;
1714 }
1715 ret = btrfs_defrag_root(root, 0);
1716 if (ret)
1717 goto out;
1718 ret = btrfs_defrag_root(root->fs_info->extent_root, 0);
1719 break;
1720 case S_IFREG:
1721 if (!(file->f_mode & FMODE_WRITE)) {
1722 ret = -EINVAL;
1723 goto out;
1724 }
1725
1726 range = kzalloc(sizeof(*range), GFP_KERNEL);
1727 if (!range) {
1728 ret = -ENOMEM;
1729 goto out;
1730 }
1731
1732 if (argp) {
1733 if (copy_from_user(range, argp,
1734 sizeof(*range))) {
1735 ret = -EFAULT;
1736 kfree(range);
1737 goto out;
1738 }
1739 /* compression requires us to start the IO */
1740 if ((range->flags & BTRFS_DEFRAG_RANGE_COMPRESS)) {
1741 range->flags |= BTRFS_DEFRAG_RANGE_START_IO;
1742 range->extent_thresh = (u32)-1;
1743 }
1744 } else {
1745 /* the rest are all set to zero by kzalloc */
1746 range->len = (u64)-1;
1747 }
1748 ret = btrfs_defrag_file(file, range);
1749 kfree(range);
1750 break;
1751 default:
1752 ret = -EINVAL;
1753 }
1754 out:
1755 mnt_drop_write(file->f_path.mnt);
1756 return ret;
1757 }
1758
1759 static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
1760 {
1761 struct btrfs_ioctl_vol_args *vol_args;
1762 int ret;
1763
1764 if (!capable(CAP_SYS_ADMIN))
1765 return -EPERM;
1766
1767 vol_args = memdup_user(arg, sizeof(*vol_args));
1768 if (IS_ERR(vol_args))
1769 return PTR_ERR(vol_args);
1770
1771 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1772 ret = btrfs_init_new_device(root, vol_args->name);
1773
1774 kfree(vol_args);
1775 return ret;
1776 }
1777
1778 static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
1779 {
1780 struct btrfs_ioctl_vol_args *vol_args;
1781 int ret;
1782
1783 if (!capable(CAP_SYS_ADMIN))
1784 return -EPERM;
1785
1786 if (root->fs_info->sb->s_flags & MS_RDONLY)
1787 return -EROFS;
1788
1789 vol_args = memdup_user(arg, sizeof(*vol_args));
1790 if (IS_ERR(vol_args))
1791 return PTR_ERR(vol_args);
1792
1793 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
1794 ret = btrfs_rm_device(root, vol_args->name);
1795
1796 kfree(vol_args);
1797 return ret;
1798 }
1799
1800 static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1801 u64 off, u64 olen, u64 destoff)
1802 {
1803 struct inode *inode = fdentry(file)->d_inode;
1804 struct btrfs_root *root = BTRFS_I(inode)->root;
1805 struct file *src_file;
1806 struct inode *src;
1807 struct btrfs_trans_handle *trans;
1808 struct btrfs_path *path;
1809 struct extent_buffer *leaf;
1810 char *buf;
1811 struct btrfs_key key;
1812 u32 nritems;
1813 int slot;
1814 int ret;
1815 u64 len = olen;
1816 u64 bs = root->fs_info->sb->s_blocksize;
1817 u64 hint_byte;
1818
1819 /*
1820 * TODO:
1821 * - split compressed inline extents. annoying: we need to
1822 * decompress into destination's address_space (the file offset
1823 * may change, so source mapping won't do), then recompress (or
1824 * otherwise reinsert) a subrange.
1825 * - allow ranges within the same file to be cloned (provided
1826 * they don't overlap)?
1827 */
1828
1829 /* the destination must be opened for writing */
1830 if (!(file->f_mode & FMODE_WRITE) || (file->f_flags & O_APPEND))
1831 return -EINVAL;
1832
1833 if (btrfs_root_readonly(root))
1834 return -EROFS;
1835
1836 ret = mnt_want_write(file->f_path.mnt);
1837 if (ret)
1838 return ret;
1839
1840 src_file = fget(srcfd);
1841 if (!src_file) {
1842 ret = -EBADF;
1843 goto out_drop_write;
1844 }
1845
1846 src = src_file->f_dentry->d_inode;
1847
1848 ret = -EINVAL;
1849 if (src == inode)
1850 goto out_fput;
1851
1852 /* the src must be open for reading */
1853 if (!(src_file->f_mode & FMODE_READ))
1854 goto out_fput;
1855
1856 ret = -EISDIR;
1857 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
1858 goto out_fput;
1859
1860 ret = -EXDEV;
1861 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1862 goto out_fput;
1863
1864 ret = -ENOMEM;
1865 buf = vmalloc(btrfs_level_size(root, 0));
1866 if (!buf)
1867 goto out_fput;
1868
1869 path = btrfs_alloc_path();
1870 if (!path) {
1871 vfree(buf);
1872 goto out_fput;
1873 }
1874 path->reada = 2;
1875
1876 if (inode < src) {
1877 mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
1878 mutex_lock_nested(&src->i_mutex, I_MUTEX_CHILD);
1879 } else {
1880 mutex_lock_nested(&src->i_mutex, I_MUTEX_PARENT);
1881 mutex_lock_nested(&inode->i_mutex, I_MUTEX_CHILD);
1882 }
1883
1884 /* determine range to clone */
1885 ret = -EINVAL;
1886 if (off + len > src->i_size || off + len < off)
1887 goto out_unlock;
1888 if (len == 0)
1889 olen = len = src->i_size - off;
1890 /* if we extend to eof, continue to block boundary */
1891 if (off + len == src->i_size)
1892 len = ALIGN(src->i_size, bs) - off;
1893
1894 /* verify the end result is block aligned */
1895 if (!IS_ALIGNED(off, bs) || !IS_ALIGNED(off + len, bs) ||
1896 !IS_ALIGNED(destoff, bs))
1897 goto out_unlock;
1898
1899 /* do any pending delalloc/csum calc on src, one way or
1900 another, and lock file content */
1901 while (1) {
1902 struct btrfs_ordered_extent *ordered;
1903 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1904 ordered = btrfs_lookup_first_ordered_extent(src, off+len);
1905 if (!ordered &&
1906 !test_range_bit(&BTRFS_I(src)->io_tree, off, off+len,
1907 EXTENT_DELALLOC, 0, NULL))
1908 break;
1909 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1910 if (ordered)
1911 btrfs_put_ordered_extent(ordered);
1912 btrfs_wait_ordered_range(src, off, len);
1913 }
1914
1915 /* clone data */
1916 key.objectid = src->i_ino;
1917 key.type = BTRFS_EXTENT_DATA_KEY;
1918 key.offset = 0;
1919
1920 while (1) {
1921 /*
1922 * note the key will change type as we walk through the
1923 * tree.
1924 */
1925 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1926 if (ret < 0)
1927 goto out;
1928
1929 nritems = btrfs_header_nritems(path->nodes[0]);
1930 if (path->slots[0] >= nritems) {
1931 ret = btrfs_next_leaf(root, path);
1932 if (ret < 0)
1933 goto out;
1934 if (ret > 0)
1935 break;
1936 nritems = btrfs_header_nritems(path->nodes[0]);
1937 }
1938 leaf = path->nodes[0];
1939 slot = path->slots[0];
1940
1941 btrfs_item_key_to_cpu(leaf, &key, slot);
1942 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
1943 key.objectid != src->i_ino)
1944 break;
1945
1946 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1947 struct btrfs_file_extent_item *extent;
1948 int type;
1949 u32 size;
1950 struct btrfs_key new_key;
1951 u64 disko = 0, diskl = 0;
1952 u64 datao = 0, datal = 0;
1953 u8 comp;
1954 u64 endoff;
1955
1956 size = btrfs_item_size_nr(leaf, slot);
1957 read_extent_buffer(leaf, buf,
1958 btrfs_item_ptr_offset(leaf, slot),
1959 size);
1960
1961 extent = btrfs_item_ptr(leaf, slot,
1962 struct btrfs_file_extent_item);
1963 comp = btrfs_file_extent_compression(leaf, extent);
1964 type = btrfs_file_extent_type(leaf, extent);
1965 if (type == BTRFS_FILE_EXTENT_REG ||
1966 type == BTRFS_FILE_EXTENT_PREALLOC) {
1967 disko = btrfs_file_extent_disk_bytenr(leaf,
1968 extent);
1969 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1970 extent);
1971 datao = btrfs_file_extent_offset(leaf, extent);
1972 datal = btrfs_file_extent_num_bytes(leaf,
1973 extent);
1974 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1975 /* take upper bound, may be compressed */
1976 datal = btrfs_file_extent_ram_bytes(leaf,
1977 extent);
1978 }
1979 btrfs_release_path(root, path);
1980
1981 if (key.offset + datal <= off ||
1982 key.offset >= off+len)
1983 goto next;
1984
1985 memcpy(&new_key, &key, sizeof(new_key));
1986 new_key.objectid = inode->i_ino;
1987 if (off <= key.offset)
1988 new_key.offset = key.offset + destoff - off;
1989 else
1990 new_key.offset = destoff;
1991
1992 trans = btrfs_start_transaction(root, 1);
1993 if (IS_ERR(trans)) {
1994 ret = PTR_ERR(trans);
1995 goto out;
1996 }
1997
1998 if (type == BTRFS_FILE_EXTENT_REG ||
1999 type == BTRFS_FILE_EXTENT_PREALLOC) {
2000 if (off > key.offset) {
2001 datao += off - key.offset;
2002 datal -= off - key.offset;
2003 }
2004
2005 if (key.offset + datal > off + len)
2006 datal = off + len - key.offset;
2007
2008 ret = btrfs_drop_extents(trans, inode,
2009 new_key.offset,
2010 new_key.offset + datal,
2011 &hint_byte, 1);
2012 BUG_ON(ret);
2013
2014 ret = btrfs_insert_empty_item(trans, root, path,
2015 &new_key, size);
2016 BUG_ON(ret);
2017
2018 leaf = path->nodes[0];
2019 slot = path->slots[0];
2020 write_extent_buffer(leaf, buf,
2021 btrfs_item_ptr_offset(leaf, slot),
2022 size);
2023
2024 extent = btrfs_item_ptr(leaf, slot,
2025 struct btrfs_file_extent_item);
2026
2027 /* disko == 0 means it's a hole */
2028 if (!disko)
2029 datao = 0;
2030
2031 btrfs_set_file_extent_offset(leaf, extent,
2032 datao);
2033 btrfs_set_file_extent_num_bytes(leaf, extent,
2034 datal);
2035 if (disko) {
2036 inode_add_bytes(inode, datal);
2037 ret = btrfs_inc_extent_ref(trans, root,
2038 disko, diskl, 0,
2039 root->root_key.objectid,
2040 inode->i_ino,
2041 new_key.offset - datao);
2042 BUG_ON(ret);
2043 }
2044 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
2045 u64 skip = 0;
2046 u64 trim = 0;
2047 if (off > key.offset) {
2048 skip = off - key.offset;
2049 new_key.offset += skip;
2050 }
2051
2052 if (key.offset + datal > off+len)
2053 trim = key.offset + datal - (off+len);
2054
2055 if (comp && (skip || trim)) {
2056 ret = -EINVAL;
2057 btrfs_end_transaction(trans, root);
2058 goto out;
2059 }
2060 size -= skip + trim;
2061 datal -= skip + trim;
2062
2063 ret = btrfs_drop_extents(trans, inode,
2064 new_key.offset,
2065 new_key.offset + datal,
2066 &hint_byte, 1);
2067 BUG_ON(ret);
2068
2069 ret = btrfs_insert_empty_item(trans, root, path,
2070 &new_key, size);
2071 BUG_ON(ret);
2072
2073 if (skip) {
2074 u32 start =
2075 btrfs_file_extent_calc_inline_size(0);
2076 memmove(buf+start, buf+start+skip,
2077 datal);
2078 }
2079
2080 leaf = path->nodes[0];
2081 slot = path->slots[0];
2082 write_extent_buffer(leaf, buf,
2083 btrfs_item_ptr_offset(leaf, slot),
2084 size);
2085 inode_add_bytes(inode, datal);
2086 }
2087
2088 btrfs_mark_buffer_dirty(leaf);
2089 btrfs_release_path(root, path);
2090
2091 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
2092
2093 /*
2094 * we round up to the block size at eof when
2095 * determining which extents to clone above,
2096 * but shouldn't round up the file size
2097 */
2098 endoff = new_key.offset + datal;
2099 if (endoff > destoff+olen)
2100 endoff = destoff+olen;
2101 if (endoff > inode->i_size)
2102 btrfs_i_size_write(inode, endoff);
2103
2104 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
2105 ret = btrfs_update_inode(trans, root, inode);
2106 BUG_ON(ret);
2107 btrfs_end_transaction(trans, root);
2108 }
2109 next:
2110 btrfs_release_path(root, path);
2111 key.offset++;
2112 }
2113 ret = 0;
2114 out:
2115 btrfs_release_path(root, path);
2116 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
2117 out_unlock:
2118 mutex_unlock(&src->i_mutex);
2119 mutex_unlock(&inode->i_mutex);
2120 vfree(buf);
2121 btrfs_free_path(path);
2122 out_fput:
2123 fput(src_file);
2124 out_drop_write:
2125 mnt_drop_write(file->f_path.mnt);
2126 return ret;
2127 }
2128
2129 static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
2130 {
2131 struct btrfs_ioctl_clone_range_args args;
2132
2133 if (copy_from_user(&args, argp, sizeof(args)))
2134 return -EFAULT;
2135 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
2136 args.src_length, args.dest_offset);
2137 }
2138
2139 /*
2140 * there are many ways the trans_start and trans_end ioctls can lead
2141 * to deadlocks. They should only be used by applications that
2142 * basically own the machine, and have a very in depth understanding
2143 * of all the possible deadlocks and enospc problems.
2144 */
2145 static long btrfs_ioctl_trans_start(struct file *file)
2146 {
2147 struct inode *inode = fdentry(file)->d_inode;
2148 struct btrfs_root *root = BTRFS_I(inode)->root;
2149 struct btrfs_trans_handle *trans;
2150 int ret;
2151
2152 ret = -EPERM;
2153 if (!capable(CAP_SYS_ADMIN))
2154 goto out;
2155
2156 ret = -EINPROGRESS;
2157 if (file->private_data)
2158 goto out;
2159
2160 ret = -EROFS;
2161 if (btrfs_root_readonly(root))
2162 goto out;
2163
2164 ret = mnt_want_write(file->f_path.mnt);
2165 if (ret)
2166 goto out;
2167
2168 mutex_lock(&root->fs_info->trans_mutex);
2169 root->fs_info->open_ioctl_trans++;
2170 mutex_unlock(&root->fs_info->trans_mutex);
2171
2172 ret = -ENOMEM;
2173 trans = btrfs_start_ioctl_transaction(root, 0);
2174 if (IS_ERR(trans))
2175 goto out_drop;
2176
2177 file->private_data = trans;
2178 return 0;
2179
2180 out_drop:
2181 mutex_lock(&root->fs_info->trans_mutex);
2182 root->fs_info->open_ioctl_trans--;
2183 mutex_unlock(&root->fs_info->trans_mutex);
2184 mnt_drop_write(file->f_path.mnt);
2185 out:
2186 return ret;
2187 }
2188
2189 static long btrfs_ioctl_default_subvol(struct file *file, void __user *argp)
2190 {
2191 struct inode *inode = fdentry(file)->d_inode;
2192 struct btrfs_root *root = BTRFS_I(inode)->root;
2193 struct btrfs_root *new_root;
2194 struct btrfs_dir_item *di;
2195 struct btrfs_trans_handle *trans;
2196 struct btrfs_path *path;
2197 struct btrfs_key location;
2198 struct btrfs_disk_key disk_key;
2199 struct btrfs_super_block *disk_super;
2200 u64 features;
2201 u64 objectid = 0;
2202 u64 dir_id;
2203
2204 if (!capable(CAP_SYS_ADMIN))
2205 return -EPERM;
2206
2207 if (copy_from_user(&objectid, argp, sizeof(objectid)))
2208 return -EFAULT;
2209
2210 if (!objectid)
2211 objectid = root->root_key.objectid;
2212
2213 location.objectid = objectid;
2214 location.type = BTRFS_ROOT_ITEM_KEY;
2215 location.offset = (u64)-1;
2216
2217 new_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
2218 if (IS_ERR(new_root))
2219 return PTR_ERR(new_root);
2220
2221 if (btrfs_root_refs(&new_root->root_item) == 0)
2222 return -ENOENT;
2223
2224 path = btrfs_alloc_path();
2225 if (!path)
2226 return -ENOMEM;
2227 path->leave_spinning = 1;
2228
2229 trans = btrfs_start_transaction(root, 1);
2230 if (IS_ERR(trans)) {
2231 btrfs_free_path(path);
2232 return PTR_ERR(trans);
2233 }
2234
2235 dir_id = btrfs_super_root_dir(&root->fs_info->super_copy);
2236 di = btrfs_lookup_dir_item(trans, root->fs_info->tree_root, path,
2237 dir_id, "default", 7, 1);
2238 if (IS_ERR_OR_NULL(di)) {
2239 btrfs_free_path(path);
2240 btrfs_end_transaction(trans, root);
2241 printk(KERN_ERR "Umm, you don't have the default dir item, "
2242 "this isn't going to work\n");
2243 return -ENOENT;
2244 }
2245
2246 btrfs_cpu_key_to_disk(&disk_key, &new_root->root_key);
2247 btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
2248 btrfs_mark_buffer_dirty(path->nodes[0]);
2249 btrfs_free_path(path);
2250
2251 disk_super = &root->fs_info->super_copy;
2252 features = btrfs_super_incompat_flags(disk_super);
2253 if (!(features & BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL)) {
2254 features |= BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL;
2255 btrfs_set_super_incompat_flags(disk_super, features);
2256 }
2257 btrfs_end_transaction(trans, root);
2258
2259 return 0;
2260 }
2261
2262 static void get_block_group_info(struct list_head *groups_list,
2263 struct btrfs_ioctl_space_info *space)
2264 {
2265 struct btrfs_block_group_cache *block_group;
2266
2267 space->total_bytes = 0;
2268 space->used_bytes = 0;
2269 space->flags = 0;
2270 list_for_each_entry(block_group, groups_list, list) {
2271 space->flags = block_group->flags;
2272 space->total_bytes += block_group->key.offset;
2273 space->used_bytes +=
2274 btrfs_block_group_used(&block_group->item);
2275 }
2276 }
2277
2278 long btrfs_ioctl_space_info(struct btrfs_root *root, void __user *arg)
2279 {
2280 struct btrfs_ioctl_space_args space_args;
2281 struct btrfs_ioctl_space_info space;
2282 struct btrfs_ioctl_space_info *dest;
2283 struct btrfs_ioctl_space_info *dest_orig;
2284 struct btrfs_ioctl_space_info *user_dest;
2285 struct btrfs_space_info *info;
2286 u64 types[] = {BTRFS_BLOCK_GROUP_DATA,
2287 BTRFS_BLOCK_GROUP_SYSTEM,
2288 BTRFS_BLOCK_GROUP_METADATA,
2289 BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA};
2290 int num_types = 4;
2291 int alloc_size;
2292 int ret = 0;
2293 u64 slot_count = 0;
2294 int i, c;
2295
2296 if (copy_from_user(&space_args,
2297 (struct btrfs_ioctl_space_args __user *)arg,
2298 sizeof(space_args)))
2299 return -EFAULT;
2300
2301 for (i = 0; i < num_types; i++) {
2302 struct btrfs_space_info *tmp;
2303
2304 info = NULL;
2305 rcu_read_lock();
2306 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2307 list) {
2308 if (tmp->flags == types[i]) {
2309 info = tmp;
2310 break;
2311 }
2312 }
2313 rcu_read_unlock();
2314
2315 if (!info)
2316 continue;
2317
2318 down_read(&info->groups_sem);
2319 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2320 if (!list_empty(&info->block_groups[c]))
2321 slot_count++;
2322 }
2323 up_read(&info->groups_sem);
2324 }
2325
2326 /* space_slots == 0 means they are asking for a count */
2327 if (space_args.space_slots == 0) {
2328 space_args.total_spaces = slot_count;
2329 goto out;
2330 }
2331
2332 slot_count = min_t(u64, space_args.space_slots, slot_count);
2333
2334 alloc_size = sizeof(*dest) * slot_count;
2335
2336 /* we generally have at most 6 or so space infos, one for each raid
2337 * level. So, a whole page should be more than enough for everyone
2338 */
2339 if (alloc_size > PAGE_CACHE_SIZE)
2340 return -ENOMEM;
2341
2342 space_args.total_spaces = 0;
2343 dest = kmalloc(alloc_size, GFP_NOFS);
2344 if (!dest)
2345 return -ENOMEM;
2346 dest_orig = dest;
2347
2348 /* now we have a buffer to copy into */
2349 for (i = 0; i < num_types; i++) {
2350 struct btrfs_space_info *tmp;
2351
2352 if (!slot_count)
2353 break;
2354
2355 info = NULL;
2356 rcu_read_lock();
2357 list_for_each_entry_rcu(tmp, &root->fs_info->space_info,
2358 list) {
2359 if (tmp->flags == types[i]) {
2360 info = tmp;
2361 break;
2362 }
2363 }
2364 rcu_read_unlock();
2365
2366 if (!info)
2367 continue;
2368 down_read(&info->groups_sem);
2369 for (c = 0; c < BTRFS_NR_RAID_TYPES; c++) {
2370 if (!list_empty(&info->block_groups[c])) {
2371 get_block_group_info(&info->block_groups[c],
2372 &space);
2373 memcpy(dest, &space, sizeof(space));
2374 dest++;
2375 space_args.total_spaces++;
2376 slot_count--;
2377 }
2378 if (!slot_count)
2379 break;
2380 }
2381 up_read(&info->groups_sem);
2382 }
2383
2384 user_dest = (struct btrfs_ioctl_space_info *)
2385 (arg + sizeof(struct btrfs_ioctl_space_args));
2386
2387 if (copy_to_user(user_dest, dest_orig, alloc_size))
2388 ret = -EFAULT;
2389
2390 kfree(dest_orig);
2391 out:
2392 if (ret == 0 && copy_to_user(arg, &space_args, sizeof(space_args)))
2393 ret = -EFAULT;
2394
2395 return ret;
2396 }
2397
2398 /*
2399 * there are many ways the trans_start and trans_end ioctls can lead
2400 * to deadlocks. They should only be used by applications that
2401 * basically own the machine, and have a very in depth understanding
2402 * of all the possible deadlocks and enospc problems.
2403 */
2404 long btrfs_ioctl_trans_end(struct file *file)
2405 {
2406 struct inode *inode = fdentry(file)->d_inode;
2407 struct btrfs_root *root = BTRFS_I(inode)->root;
2408 struct btrfs_trans_handle *trans;
2409
2410 trans = file->private_data;
2411 if (!trans)
2412 return -EINVAL;
2413 file->private_data = NULL;
2414
2415 btrfs_end_transaction(trans, root);
2416
2417 mutex_lock(&root->fs_info->trans_mutex);
2418 root->fs_info->open_ioctl_trans--;
2419 mutex_unlock(&root->fs_info->trans_mutex);
2420
2421 mnt_drop_write(file->f_path.mnt);
2422 return 0;
2423 }
2424
2425 static noinline long btrfs_ioctl_start_sync(struct file *file, void __user *argp)
2426 {
2427 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2428 struct btrfs_trans_handle *trans;
2429 u64 transid;
2430 int ret;
2431
2432 trans = btrfs_start_transaction(root, 0);
2433 if (IS_ERR(trans))
2434 return PTR_ERR(trans);
2435 transid = trans->transid;
2436 ret = btrfs_commit_transaction_async(trans, root, 0);
2437 if (ret)
2438 return ret;
2439
2440 if (argp)
2441 if (copy_to_user(argp, &transid, sizeof(transid)))
2442 return -EFAULT;
2443 return 0;
2444 }
2445
2446 static noinline long btrfs_ioctl_wait_sync(struct file *file, void __user *argp)
2447 {
2448 struct btrfs_root *root = BTRFS_I(file->f_dentry->d_inode)->root;
2449 u64 transid;
2450
2451 if (argp) {
2452 if (copy_from_user(&transid, argp, sizeof(transid)))
2453 return -EFAULT;
2454 } else {
2455 transid = 0; /* current trans */
2456 }
2457 return btrfs_wait_for_commit(root, transid);
2458 }
2459
2460 long btrfs_ioctl(struct file *file, unsigned int
2461 cmd, unsigned long arg)
2462 {
2463 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2464 void __user *argp = (void __user *)arg;
2465
2466 switch (cmd) {
2467 case FS_IOC_GETFLAGS:
2468 return btrfs_ioctl_getflags(file, argp);
2469 case FS_IOC_SETFLAGS:
2470 return btrfs_ioctl_setflags(file, argp);
2471 case FS_IOC_GETVERSION:
2472 return btrfs_ioctl_getversion(file, argp);
2473 case FITRIM:
2474 return btrfs_ioctl_fitrim(file, argp);
2475 case BTRFS_IOC_SNAP_CREATE:
2476 return btrfs_ioctl_snap_create(file, argp, 0);
2477 case BTRFS_IOC_SNAP_CREATE_V2:
2478 return btrfs_ioctl_snap_create_v2(file, argp, 0);
2479 case BTRFS_IOC_SUBVOL_CREATE:
2480 return btrfs_ioctl_snap_create(file, argp, 1);
2481 case BTRFS_IOC_SNAP_DESTROY:
2482 return btrfs_ioctl_snap_destroy(file, argp);
2483 case BTRFS_IOC_SUBVOL_GETFLAGS:
2484 return btrfs_ioctl_subvol_getflags(file, argp);
2485 case BTRFS_IOC_SUBVOL_SETFLAGS:
2486 return btrfs_ioctl_subvol_setflags(file, argp);
2487 case BTRFS_IOC_DEFAULT_SUBVOL:
2488 return btrfs_ioctl_default_subvol(file, argp);
2489 case BTRFS_IOC_DEFRAG:
2490 return btrfs_ioctl_defrag(file, NULL);
2491 case BTRFS_IOC_DEFRAG_RANGE:
2492 return btrfs_ioctl_defrag(file, argp);
2493 case BTRFS_IOC_RESIZE:
2494 return btrfs_ioctl_resize(root, argp);
2495 case BTRFS_IOC_ADD_DEV:
2496 return btrfs_ioctl_add_dev(root, argp);
2497 case BTRFS_IOC_RM_DEV:
2498 return btrfs_ioctl_rm_dev(root, argp);
2499 case BTRFS_IOC_BALANCE:
2500 return btrfs_balance(root->fs_info->dev_root);
2501 case BTRFS_IOC_CLONE:
2502 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
2503 case BTRFS_IOC_CLONE_RANGE:
2504 return btrfs_ioctl_clone_range(file, argp);
2505 case BTRFS_IOC_TRANS_START:
2506 return btrfs_ioctl_trans_start(file);
2507 case BTRFS_IOC_TRANS_END:
2508 return btrfs_ioctl_trans_end(file);
2509 case BTRFS_IOC_TREE_SEARCH:
2510 return btrfs_ioctl_tree_search(file, argp);
2511 case BTRFS_IOC_INO_LOOKUP:
2512 return btrfs_ioctl_ino_lookup(file, argp);
2513 case BTRFS_IOC_SPACE_INFO:
2514 return btrfs_ioctl_space_info(root, argp);
2515 case BTRFS_IOC_SYNC:
2516 btrfs_sync_fs(file->f_dentry->d_sb, 1);
2517 return 0;
2518 case BTRFS_IOC_START_SYNC:
2519 return btrfs_ioctl_start_sync(file, argp);
2520 case BTRFS_IOC_WAIT_SYNC:
2521 return btrfs_ioctl_wait_sync(file, argp);
2522 }
2523
2524 return -ENOTTY;
2525 }
This page took 0.127416 seconds and 5 git commands to generate.