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