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