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