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> |
4b4e25f2 | 42 | #include "compat.h" |
f46b5a66 CH |
43 | #include "ctree.h" |
44 | #include "disk-io.h" | |
45 | #include "transaction.h" | |
46 | #include "btrfs_inode.h" | |
47 | #include "ioctl.h" | |
48 | #include "print-tree.h" | |
49 | #include "volumes.h" | |
925baedd | 50 | #include "locking.h" |
f46b5a66 | 51 | |
6cbff00f CH |
52 | /* Mask out flags that are inappropriate for the given type of inode. */ |
53 | static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags) | |
54 | { | |
55 | if (S_ISDIR(mode)) | |
56 | return flags; | |
57 | else if (S_ISREG(mode)) | |
58 | return flags & ~FS_DIRSYNC_FL; | |
59 | else | |
60 | return flags & (FS_NODUMP_FL | FS_NOATIME_FL); | |
61 | } | |
62 | ||
63 | /* | |
64 | * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl. | |
65 | */ | |
66 | static unsigned int btrfs_flags_to_ioctl(unsigned int flags) | |
67 | { | |
68 | unsigned int iflags = 0; | |
69 | ||
70 | if (flags & BTRFS_INODE_SYNC) | |
71 | iflags |= FS_SYNC_FL; | |
72 | if (flags & BTRFS_INODE_IMMUTABLE) | |
73 | iflags |= FS_IMMUTABLE_FL; | |
74 | if (flags & BTRFS_INODE_APPEND) | |
75 | iflags |= FS_APPEND_FL; | |
76 | if (flags & BTRFS_INODE_NODUMP) | |
77 | iflags |= FS_NODUMP_FL; | |
78 | if (flags & BTRFS_INODE_NOATIME) | |
79 | iflags |= FS_NOATIME_FL; | |
80 | if (flags & BTRFS_INODE_DIRSYNC) | |
81 | iflags |= FS_DIRSYNC_FL; | |
82 | ||
83 | return iflags; | |
84 | } | |
85 | ||
86 | /* | |
87 | * Update inode->i_flags based on the btrfs internal flags. | |
88 | */ | |
89 | void btrfs_update_iflags(struct inode *inode) | |
90 | { | |
91 | struct btrfs_inode *ip = BTRFS_I(inode); | |
92 | ||
93 | inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); | |
94 | ||
95 | if (ip->flags & BTRFS_INODE_SYNC) | |
96 | inode->i_flags |= S_SYNC; | |
97 | if (ip->flags & BTRFS_INODE_IMMUTABLE) | |
98 | inode->i_flags |= S_IMMUTABLE; | |
99 | if (ip->flags & BTRFS_INODE_APPEND) | |
100 | inode->i_flags |= S_APPEND; | |
101 | if (ip->flags & BTRFS_INODE_NOATIME) | |
102 | inode->i_flags |= S_NOATIME; | |
103 | if (ip->flags & BTRFS_INODE_DIRSYNC) | |
104 | inode->i_flags |= S_DIRSYNC; | |
105 | } | |
106 | ||
107 | /* | |
108 | * Inherit flags from the parent inode. | |
109 | * | |
110 | * Unlike extN we don't have any flags we don't want to inherit currently. | |
111 | */ | |
112 | void btrfs_inherit_iflags(struct inode *inode, struct inode *dir) | |
113 | { | |
0b4dcea5 CM |
114 | unsigned int flags; |
115 | ||
116 | if (!dir) | |
117 | return; | |
118 | ||
119 | flags = BTRFS_I(dir)->flags; | |
6cbff00f CH |
120 | |
121 | if (S_ISREG(inode->i_mode)) | |
122 | flags &= ~BTRFS_INODE_DIRSYNC; | |
123 | else if (!S_ISDIR(inode->i_mode)) | |
124 | flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME); | |
125 | ||
126 | BTRFS_I(inode)->flags = flags; | |
127 | btrfs_update_iflags(inode); | |
128 | } | |
129 | ||
130 | static int btrfs_ioctl_getflags(struct file *file, void __user *arg) | |
131 | { | |
132 | struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode); | |
133 | unsigned int flags = btrfs_flags_to_ioctl(ip->flags); | |
134 | ||
135 | if (copy_to_user(arg, &flags, sizeof(flags))) | |
136 | return -EFAULT; | |
137 | return 0; | |
138 | } | |
139 | ||
140 | static int btrfs_ioctl_setflags(struct file *file, void __user *arg) | |
141 | { | |
142 | struct inode *inode = file->f_path.dentry->d_inode; | |
143 | struct btrfs_inode *ip = BTRFS_I(inode); | |
144 | struct btrfs_root *root = ip->root; | |
145 | struct btrfs_trans_handle *trans; | |
146 | unsigned int flags, oldflags; | |
147 | int ret; | |
148 | ||
149 | if (copy_from_user(&flags, arg, sizeof(flags))) | |
150 | return -EFAULT; | |
151 | ||
152 | if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \ | |
153 | FS_NOATIME_FL | FS_NODUMP_FL | \ | |
154 | FS_SYNC_FL | FS_DIRSYNC_FL)) | |
155 | return -EOPNOTSUPP; | |
f46b5a66 | 156 | |
6cbff00f CH |
157 | if (!is_owner_or_cap(inode)) |
158 | return -EACCES; | |
159 | ||
160 | mutex_lock(&inode->i_mutex); | |
161 | ||
162 | flags = btrfs_mask_flags(inode->i_mode, flags); | |
163 | oldflags = btrfs_flags_to_ioctl(ip->flags); | |
164 | if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) { | |
165 | if (!capable(CAP_LINUX_IMMUTABLE)) { | |
166 | ret = -EPERM; | |
167 | goto out_unlock; | |
168 | } | |
169 | } | |
170 | ||
171 | ret = mnt_want_write(file->f_path.mnt); | |
172 | if (ret) | |
173 | goto out_unlock; | |
174 | ||
175 | if (flags & FS_SYNC_FL) | |
176 | ip->flags |= BTRFS_INODE_SYNC; | |
177 | else | |
178 | ip->flags &= ~BTRFS_INODE_SYNC; | |
179 | if (flags & FS_IMMUTABLE_FL) | |
180 | ip->flags |= BTRFS_INODE_IMMUTABLE; | |
181 | else | |
182 | ip->flags &= ~BTRFS_INODE_IMMUTABLE; | |
183 | if (flags & FS_APPEND_FL) | |
184 | ip->flags |= BTRFS_INODE_APPEND; | |
185 | else | |
186 | ip->flags &= ~BTRFS_INODE_APPEND; | |
187 | if (flags & FS_NODUMP_FL) | |
188 | ip->flags |= BTRFS_INODE_NODUMP; | |
189 | else | |
190 | ip->flags &= ~BTRFS_INODE_NODUMP; | |
191 | if (flags & FS_NOATIME_FL) | |
192 | ip->flags |= BTRFS_INODE_NOATIME; | |
193 | else | |
194 | ip->flags &= ~BTRFS_INODE_NOATIME; | |
195 | if (flags & FS_DIRSYNC_FL) | |
196 | ip->flags |= BTRFS_INODE_DIRSYNC; | |
197 | else | |
198 | ip->flags &= ~BTRFS_INODE_DIRSYNC; | |
199 | ||
200 | ||
201 | trans = btrfs_join_transaction(root, 1); | |
202 | BUG_ON(!trans); | |
203 | ||
204 | ret = btrfs_update_inode(trans, root, inode); | |
205 | BUG_ON(ret); | |
206 | ||
207 | btrfs_update_iflags(inode); | |
208 | inode->i_ctime = CURRENT_TIME; | |
209 | btrfs_end_transaction(trans, root); | |
210 | ||
211 | mnt_drop_write(file->f_path.mnt); | |
212 | out_unlock: | |
213 | mutex_unlock(&inode->i_mutex); | |
214 | return 0; | |
215 | } | |
216 | ||
217 | static int btrfs_ioctl_getversion(struct file *file, int __user *arg) | |
218 | { | |
219 | struct inode *inode = file->f_path.dentry->d_inode; | |
220 | ||
221 | return put_user(inode->i_generation, arg); | |
222 | } | |
f46b5a66 | 223 | |
cb8e7090 CH |
224 | static noinline int create_subvol(struct btrfs_root *root, |
225 | struct dentry *dentry, | |
226 | char *name, int namelen) | |
f46b5a66 CH |
227 | { |
228 | struct btrfs_trans_handle *trans; | |
229 | struct btrfs_key key; | |
230 | struct btrfs_root_item root_item; | |
231 | struct btrfs_inode_item *inode_item; | |
232 | struct extent_buffer *leaf; | |
76dda93c YZ |
233 | struct btrfs_root *new_root; |
234 | struct inode *dir = dentry->d_parent->d_inode; | |
f46b5a66 CH |
235 | int ret; |
236 | int err; | |
237 | u64 objectid; | |
238 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; | |
3de4586c | 239 | u64 index = 0; |
f46b5a66 | 240 | |
9ed74f2d JB |
241 | /* |
242 | * 1 - inode item | |
243 | * 2 - refs | |
244 | * 1 - root item | |
245 | * 2 - dir items | |
246 | */ | |
247 | ret = btrfs_reserve_metadata_space(root, 6); | |
f46b5a66 | 248 | if (ret) |
76dda93c | 249 | return ret; |
f46b5a66 CH |
250 | |
251 | trans = btrfs_start_transaction(root, 1); | |
252 | BUG_ON(!trans); | |
253 | ||
254 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, | |
255 | 0, &objectid); | |
256 | if (ret) | |
257 | goto fail; | |
258 | ||
5d4f98a2 YZ |
259 | leaf = btrfs_alloc_free_block(trans, root, root->leafsize, |
260 | 0, objectid, NULL, 0, 0, 0); | |
8e8a1e31 JB |
261 | if (IS_ERR(leaf)) { |
262 | ret = PTR_ERR(leaf); | |
263 | goto fail; | |
264 | } | |
f46b5a66 | 265 | |
5d4f98a2 | 266 | memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header)); |
f46b5a66 CH |
267 | btrfs_set_header_bytenr(leaf, leaf->start); |
268 | btrfs_set_header_generation(leaf, trans->transid); | |
5d4f98a2 | 269 | btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV); |
f46b5a66 CH |
270 | btrfs_set_header_owner(leaf, objectid); |
271 | ||
272 | write_extent_buffer(leaf, root->fs_info->fsid, | |
273 | (unsigned long)btrfs_header_fsid(leaf), | |
274 | BTRFS_FSID_SIZE); | |
5d4f98a2 YZ |
275 | write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid, |
276 | (unsigned long)btrfs_header_chunk_tree_uuid(leaf), | |
277 | BTRFS_UUID_SIZE); | |
f46b5a66 CH |
278 | btrfs_mark_buffer_dirty(leaf); |
279 | ||
280 | inode_item = &root_item.inode; | |
281 | memset(inode_item, 0, sizeof(*inode_item)); | |
282 | inode_item->generation = cpu_to_le64(1); | |
283 | inode_item->size = cpu_to_le64(3); | |
284 | inode_item->nlink = cpu_to_le32(1); | |
a76a3cd4 | 285 | inode_item->nbytes = cpu_to_le64(root->leafsize); |
f46b5a66 CH |
286 | inode_item->mode = cpu_to_le32(S_IFDIR | 0755); |
287 | ||
288 | btrfs_set_root_bytenr(&root_item, leaf->start); | |
84234f3a | 289 | btrfs_set_root_generation(&root_item, trans->transid); |
f46b5a66 CH |
290 | btrfs_set_root_level(&root_item, 0); |
291 | btrfs_set_root_refs(&root_item, 1); | |
86b9f2ec | 292 | btrfs_set_root_used(&root_item, leaf->len); |
80ff3856 | 293 | btrfs_set_root_last_snapshot(&root_item, 0); |
f46b5a66 CH |
294 | |
295 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); | |
296 | root_item.drop_level = 0; | |
297 | ||
925baedd | 298 | btrfs_tree_unlock(leaf); |
f46b5a66 CH |
299 | free_extent_buffer(leaf); |
300 | leaf = NULL; | |
301 | ||
302 | btrfs_set_root_dirid(&root_item, new_dirid); | |
303 | ||
304 | key.objectid = objectid; | |
5d4f98a2 | 305 | key.offset = 0; |
f46b5a66 CH |
306 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
307 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, | |
308 | &root_item); | |
309 | if (ret) | |
310 | goto fail; | |
311 | ||
76dda93c YZ |
312 | key.offset = (u64)-1; |
313 | new_root = btrfs_read_fs_root_no_name(root->fs_info, &key); | |
314 | BUG_ON(IS_ERR(new_root)); | |
315 | ||
316 | btrfs_record_root_in_trans(trans, new_root); | |
317 | ||
318 | ret = btrfs_create_subvol_root(trans, new_root, new_dirid, | |
319 | BTRFS_I(dir)->block_group); | |
f46b5a66 CH |
320 | /* |
321 | * insert the directory item | |
322 | */ | |
3de4586c CM |
323 | ret = btrfs_set_inode_index(dir, &index); |
324 | BUG_ON(ret); | |
325 | ||
326 | ret = btrfs_insert_dir_item(trans, root, | |
f46b5a66 | 327 | name, namelen, dir->i_ino, &key, |
3de4586c | 328 | BTRFS_FT_DIR, index); |
f46b5a66 CH |
329 | if (ret) |
330 | goto fail; | |
0660b5af | 331 | |
52c26179 YZ |
332 | btrfs_i_size_write(dir, dir->i_size + namelen * 2); |
333 | ret = btrfs_update_inode(trans, root, dir); | |
334 | BUG_ON(ret); | |
335 | ||
0660b5af | 336 | ret = btrfs_add_root_ref(trans, root->fs_info->tree_root, |
4df27c4d | 337 | objectid, root->root_key.objectid, |
0660b5af | 338 | dir->i_ino, index, name, namelen); |
0660b5af | 339 | |
76dda93c | 340 | BUG_ON(ret); |
f46b5a66 | 341 | |
76dda93c | 342 | d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry)); |
f46b5a66 | 343 | fail: |
76dda93c | 344 | err = btrfs_commit_transaction(trans, root); |
f46b5a66 CH |
345 | if (err && !ret) |
346 | ret = err; | |
9ed74f2d JB |
347 | |
348 | btrfs_unreserve_metadata_space(root, 6); | |
f46b5a66 CH |
349 | return ret; |
350 | } | |
351 | ||
3de4586c CM |
352 | static int create_snapshot(struct btrfs_root *root, struct dentry *dentry, |
353 | char *name, int namelen) | |
f46b5a66 | 354 | { |
2e4bfab9 | 355 | struct inode *inode; |
f46b5a66 CH |
356 | struct btrfs_pending_snapshot *pending_snapshot; |
357 | struct btrfs_trans_handle *trans; | |
2e4bfab9 | 358 | int ret; |
f46b5a66 CH |
359 | |
360 | if (!root->ref_cows) | |
361 | return -EINVAL; | |
362 | ||
9ed74f2d JB |
363 | /* |
364 | * 1 - inode item | |
365 | * 2 - refs | |
366 | * 1 - root item | |
367 | * 2 - dir items | |
368 | */ | |
369 | ret = btrfs_reserve_metadata_space(root, 6); | |
f46b5a66 | 370 | if (ret) |
2e4bfab9 | 371 | goto fail; |
f46b5a66 | 372 | |
3de4586c | 373 | pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS); |
f46b5a66 CH |
374 | if (!pending_snapshot) { |
375 | ret = -ENOMEM; | |
9ed74f2d | 376 | btrfs_unreserve_metadata_space(root, 6); |
2e4bfab9 | 377 | goto fail; |
f46b5a66 CH |
378 | } |
379 | pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS); | |
380 | if (!pending_snapshot->name) { | |
381 | ret = -ENOMEM; | |
382 | kfree(pending_snapshot); | |
9ed74f2d | 383 | btrfs_unreserve_metadata_space(root, 6); |
2e4bfab9 | 384 | goto fail; |
f46b5a66 CH |
385 | } |
386 | memcpy(pending_snapshot->name, name, namelen); | |
387 | pending_snapshot->name[namelen] = '\0'; | |
3de4586c | 388 | pending_snapshot->dentry = dentry; |
f46b5a66 CH |
389 | trans = btrfs_start_transaction(root, 1); |
390 | BUG_ON(!trans); | |
391 | pending_snapshot->root = root; | |
392 | list_add(&pending_snapshot->list, | |
393 | &trans->transaction->pending_snapshots); | |
2e4bfab9 YZ |
394 | ret = btrfs_commit_transaction(trans, root); |
395 | BUG_ON(ret); | |
396 | btrfs_unreserve_metadata_space(root, 6); | |
f46b5a66 | 397 | |
2e4bfab9 YZ |
398 | inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry); |
399 | if (IS_ERR(inode)) { | |
400 | ret = PTR_ERR(inode); | |
401 | goto fail; | |
402 | } | |
403 | BUG_ON(!inode); | |
404 | d_instantiate(dentry, inode); | |
405 | ret = 0; | |
406 | fail: | |
f46b5a66 CH |
407 | return ret; |
408 | } | |
409 | ||
cb8e7090 CH |
410 | /* copy of may_create in fs/namei.c() */ |
411 | static inline int btrfs_may_create(struct inode *dir, struct dentry *child) | |
412 | { | |
413 | if (child->d_inode) | |
414 | return -EEXIST; | |
415 | if (IS_DEADDIR(dir)) | |
416 | return -ENOENT; | |
417 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); | |
418 | } | |
419 | ||
420 | /* | |
421 | * Create a new subvolume below @parent. This is largely modeled after | |
422 | * sys_mkdirat and vfs_mkdir, but we only do a single component lookup | |
423 | * inside this filesystem so it's quite a bit simpler. | |
424 | */ | |
76dda93c YZ |
425 | static noinline int btrfs_mksubvol(struct path *parent, |
426 | char *name, int namelen, | |
3de4586c | 427 | struct btrfs_root *snap_src) |
cb8e7090 | 428 | { |
76dda93c | 429 | struct inode *dir = parent->dentry->d_inode; |
cb8e7090 CH |
430 | struct dentry *dentry; |
431 | int error; | |
432 | ||
76dda93c | 433 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
cb8e7090 CH |
434 | |
435 | dentry = lookup_one_len(name, parent->dentry, namelen); | |
436 | error = PTR_ERR(dentry); | |
437 | if (IS_ERR(dentry)) | |
438 | goto out_unlock; | |
439 | ||
440 | error = -EEXIST; | |
441 | if (dentry->d_inode) | |
442 | goto out_dput; | |
443 | ||
cb8e7090 CH |
444 | error = mnt_want_write(parent->mnt); |
445 | if (error) | |
446 | goto out_dput; | |
447 | ||
76dda93c | 448 | error = btrfs_may_create(dir, dentry); |
cb8e7090 CH |
449 | if (error) |
450 | goto out_drop_write; | |
451 | ||
76dda93c YZ |
452 | down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); |
453 | ||
454 | if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0) | |
455 | goto out_up_read; | |
456 | ||
3de4586c | 457 | if (snap_src) { |
76dda93c YZ |
458 | error = create_snapshot(snap_src, dentry, |
459 | name, namelen); | |
3de4586c | 460 | } else { |
76dda93c YZ |
461 | error = create_subvol(BTRFS_I(dir)->root, dentry, |
462 | name, namelen); | |
3de4586c | 463 | } |
76dda93c YZ |
464 | if (!error) |
465 | fsnotify_mkdir(dir, dentry); | |
466 | out_up_read: | |
467 | up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem); | |
cb8e7090 CH |
468 | out_drop_write: |
469 | mnt_drop_write(parent->mnt); | |
470 | out_dput: | |
471 | dput(dentry); | |
472 | out_unlock: | |
76dda93c | 473 | mutex_unlock(&dir->i_mutex); |
cb8e7090 CH |
474 | return error; |
475 | } | |
476 | ||
b2950863 | 477 | static int btrfs_defrag_file(struct file *file) |
f46b5a66 CH |
478 | { |
479 | struct inode *inode = fdentry(file)->d_inode; | |
480 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
481 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
3eaa2885 | 482 | struct btrfs_ordered_extent *ordered; |
f46b5a66 CH |
483 | struct page *page; |
484 | unsigned long last_index; | |
485 | unsigned long ra_pages = root->fs_info->bdi.ra_pages; | |
486 | unsigned long total_read = 0; | |
487 | u64 page_start; | |
488 | u64 page_end; | |
489 | unsigned long i; | |
490 | int ret; | |
491 | ||
6a63209f | 492 | ret = btrfs_check_data_free_space(root, inode, inode->i_size); |
f46b5a66 CH |
493 | if (ret) |
494 | return -ENOSPC; | |
495 | ||
496 | mutex_lock(&inode->i_mutex); | |
497 | last_index = inode->i_size >> PAGE_CACHE_SHIFT; | |
498 | for (i = 0; i <= last_index; i++) { | |
499 | if (total_read % ra_pages == 0) { | |
500 | btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i, | |
501 | min(last_index, i + ra_pages - 1)); | |
502 | } | |
503 | total_read++; | |
3eaa2885 | 504 | again: |
f46b5a66 CH |
505 | page = grab_cache_page(inode->i_mapping, i); |
506 | if (!page) | |
507 | goto out_unlock; | |
508 | if (!PageUptodate(page)) { | |
509 | btrfs_readpage(NULL, page); | |
510 | lock_page(page); | |
511 | if (!PageUptodate(page)) { | |
512 | unlock_page(page); | |
513 | page_cache_release(page); | |
514 | goto out_unlock; | |
515 | } | |
516 | } | |
517 | ||
f46b5a66 | 518 | wait_on_page_writeback(page); |
f46b5a66 CH |
519 | |
520 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; | |
521 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
f46b5a66 | 522 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
3eaa2885 CM |
523 | |
524 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
525 | if (ordered) { | |
526 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
527 | unlock_page(page); | |
528 | page_cache_release(page); | |
529 | btrfs_start_ordered_extent(inode, ordered, 1); | |
530 | btrfs_put_ordered_extent(ordered); | |
531 | goto again; | |
532 | } | |
533 | set_page_extent_mapped(page); | |
534 | ||
f87f057b CM |
535 | /* |
536 | * this makes sure page_mkwrite is called on the | |
537 | * page if it is dirtied again later | |
538 | */ | |
539 | clear_page_dirty_for_io(page); | |
540 | ||
ea8c2819 | 541 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
f46b5a66 | 542 | set_page_dirty(page); |
a1ed835e | 543 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
f46b5a66 CH |
544 | unlock_page(page); |
545 | page_cache_release(page); | |
546 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1); | |
547 | } | |
548 | ||
549 | out_unlock: | |
550 | mutex_unlock(&inode->i_mutex); | |
551 | return 0; | |
552 | } | |
553 | ||
76dda93c YZ |
554 | static noinline int btrfs_ioctl_resize(struct btrfs_root *root, |
555 | void __user *arg) | |
f46b5a66 CH |
556 | { |
557 | u64 new_size; | |
558 | u64 old_size; | |
559 | u64 devid = 1; | |
560 | struct btrfs_ioctl_vol_args *vol_args; | |
561 | struct btrfs_trans_handle *trans; | |
562 | struct btrfs_device *device = NULL; | |
563 | char *sizestr; | |
564 | char *devstr = NULL; | |
565 | int ret = 0; | |
566 | int namelen; | |
567 | int mod = 0; | |
568 | ||
c146afad YZ |
569 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
570 | return -EROFS; | |
571 | ||
e441d54d CM |
572 | if (!capable(CAP_SYS_ADMIN)) |
573 | return -EPERM; | |
574 | ||
dae7b665 LZ |
575 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
576 | if (IS_ERR(vol_args)) | |
577 | return PTR_ERR(vol_args); | |
5516e595 MF |
578 | |
579 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
f46b5a66 | 580 | namelen = strlen(vol_args->name); |
f46b5a66 | 581 | |
7d9eb12c | 582 | mutex_lock(&root->fs_info->volume_mutex); |
f46b5a66 CH |
583 | sizestr = vol_args->name; |
584 | devstr = strchr(sizestr, ':'); | |
585 | if (devstr) { | |
586 | char *end; | |
587 | sizestr = devstr + 1; | |
588 | *devstr = '\0'; | |
589 | devstr = vol_args->name; | |
590 | devid = simple_strtoull(devstr, &end, 10); | |
21380931 JB |
591 | printk(KERN_INFO "resizing devid %llu\n", |
592 | (unsigned long long)devid); | |
f46b5a66 | 593 | } |
2b82032c | 594 | device = btrfs_find_device(root, devid, NULL, NULL); |
f46b5a66 | 595 | if (!device) { |
21380931 JB |
596 | printk(KERN_INFO "resizer unable to find device %llu\n", |
597 | (unsigned long long)devid); | |
f46b5a66 CH |
598 | ret = -EINVAL; |
599 | goto out_unlock; | |
600 | } | |
601 | if (!strcmp(sizestr, "max")) | |
602 | new_size = device->bdev->bd_inode->i_size; | |
603 | else { | |
604 | if (sizestr[0] == '-') { | |
605 | mod = -1; | |
606 | sizestr++; | |
607 | } else if (sizestr[0] == '+') { | |
608 | mod = 1; | |
609 | sizestr++; | |
610 | } | |
611 | new_size = btrfs_parse_size(sizestr); | |
612 | if (new_size == 0) { | |
613 | ret = -EINVAL; | |
614 | goto out_unlock; | |
615 | } | |
616 | } | |
617 | ||
618 | old_size = device->total_bytes; | |
619 | ||
620 | if (mod < 0) { | |
621 | if (new_size > old_size) { | |
622 | ret = -EINVAL; | |
623 | goto out_unlock; | |
624 | } | |
625 | new_size = old_size - new_size; | |
626 | } else if (mod > 0) { | |
627 | new_size = old_size + new_size; | |
628 | } | |
629 | ||
630 | if (new_size < 256 * 1024 * 1024) { | |
631 | ret = -EINVAL; | |
632 | goto out_unlock; | |
633 | } | |
634 | if (new_size > device->bdev->bd_inode->i_size) { | |
635 | ret = -EFBIG; | |
636 | goto out_unlock; | |
637 | } | |
638 | ||
639 | do_div(new_size, root->sectorsize); | |
640 | new_size *= root->sectorsize; | |
641 | ||
642 | printk(KERN_INFO "new size for %s is %llu\n", | |
643 | device->name, (unsigned long long)new_size); | |
644 | ||
645 | if (new_size > old_size) { | |
646 | trans = btrfs_start_transaction(root, 1); | |
647 | ret = btrfs_grow_device(trans, device, new_size); | |
648 | btrfs_commit_transaction(trans, root); | |
649 | } else { | |
650 | ret = btrfs_shrink_device(device, new_size); | |
651 | } | |
652 | ||
653 | out_unlock: | |
7d9eb12c | 654 | mutex_unlock(&root->fs_info->volume_mutex); |
f46b5a66 CH |
655 | kfree(vol_args); |
656 | return ret; | |
657 | } | |
658 | ||
cb8e7090 | 659 | static noinline int btrfs_ioctl_snap_create(struct file *file, |
3de4586c | 660 | void __user *arg, int subvol) |
f46b5a66 | 661 | { |
cb8e7090 | 662 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; |
f46b5a66 | 663 | struct btrfs_ioctl_vol_args *vol_args; |
3de4586c | 664 | struct file *src_file; |
f46b5a66 | 665 | int namelen; |
3de4586c | 666 | int ret = 0; |
f46b5a66 | 667 | |
c146afad YZ |
668 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
669 | return -EROFS; | |
670 | ||
dae7b665 LZ |
671 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
672 | if (IS_ERR(vol_args)) | |
673 | return PTR_ERR(vol_args); | |
f46b5a66 | 674 | |
5516e595 | 675 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
f46b5a66 | 676 | namelen = strlen(vol_args->name); |
f46b5a66 CH |
677 | if (strchr(vol_args->name, '/')) { |
678 | ret = -EINVAL; | |
679 | goto out; | |
680 | } | |
681 | ||
3de4586c | 682 | if (subvol) { |
76dda93c YZ |
683 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen, |
684 | NULL); | |
cb8e7090 | 685 | } else { |
3de4586c CM |
686 | struct inode *src_inode; |
687 | src_file = fget(vol_args->fd); | |
688 | if (!src_file) { | |
689 | ret = -EINVAL; | |
690 | goto out; | |
691 | } | |
692 | ||
693 | src_inode = src_file->f_path.dentry->d_inode; | |
694 | if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) { | |
d397712b CM |
695 | printk(KERN_INFO "btrfs: Snapshot src from " |
696 | "another FS\n"); | |
3de4586c CM |
697 | ret = -EINVAL; |
698 | fput(src_file); | |
699 | goto out; | |
700 | } | |
76dda93c YZ |
701 | ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen, |
702 | BTRFS_I(src_inode)->root); | |
3de4586c | 703 | fput(src_file); |
cb8e7090 | 704 | } |
f46b5a66 CH |
705 | out: |
706 | kfree(vol_args); | |
707 | return ret; | |
708 | } | |
709 | ||
76dda93c YZ |
710 | /* |
711 | * helper to check if the subvolume references other subvolumes | |
712 | */ | |
713 | static noinline int may_destroy_subvol(struct btrfs_root *root) | |
714 | { | |
715 | struct btrfs_path *path; | |
716 | struct btrfs_key key; | |
717 | int ret; | |
718 | ||
719 | path = btrfs_alloc_path(); | |
720 | if (!path) | |
721 | return -ENOMEM; | |
722 | ||
723 | key.objectid = root->root_key.objectid; | |
724 | key.type = BTRFS_ROOT_REF_KEY; | |
725 | key.offset = (u64)-1; | |
726 | ||
727 | ret = btrfs_search_slot(NULL, root->fs_info->tree_root, | |
728 | &key, path, 0, 0); | |
729 | if (ret < 0) | |
730 | goto out; | |
731 | BUG_ON(ret == 0); | |
732 | ||
733 | ret = 0; | |
734 | if (path->slots[0] > 0) { | |
735 | path->slots[0]--; | |
736 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); | |
737 | if (key.objectid == root->root_key.objectid && | |
738 | key.type == BTRFS_ROOT_REF_KEY) | |
739 | ret = -ENOTEMPTY; | |
740 | } | |
741 | out: | |
742 | btrfs_free_path(path); | |
743 | return ret; | |
744 | } | |
745 | ||
746 | static noinline int btrfs_ioctl_snap_destroy(struct file *file, | |
747 | void __user *arg) | |
748 | { | |
749 | struct dentry *parent = fdentry(file); | |
750 | struct dentry *dentry; | |
751 | struct inode *dir = parent->d_inode; | |
752 | struct inode *inode; | |
753 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
754 | struct btrfs_root *dest = NULL; | |
755 | struct btrfs_ioctl_vol_args *vol_args; | |
756 | struct btrfs_trans_handle *trans; | |
757 | int namelen; | |
758 | int ret; | |
759 | int err = 0; | |
760 | ||
761 | if (!capable(CAP_SYS_ADMIN)) | |
762 | return -EPERM; | |
763 | ||
764 | vol_args = memdup_user(arg, sizeof(*vol_args)); | |
765 | if (IS_ERR(vol_args)) | |
766 | return PTR_ERR(vol_args); | |
767 | ||
768 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; | |
769 | namelen = strlen(vol_args->name); | |
770 | if (strchr(vol_args->name, '/') || | |
771 | strncmp(vol_args->name, "..", namelen) == 0) { | |
772 | err = -EINVAL; | |
773 | goto out; | |
774 | } | |
775 | ||
776 | err = mnt_want_write(file->f_path.mnt); | |
777 | if (err) | |
778 | goto out; | |
779 | ||
780 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); | |
781 | dentry = lookup_one_len(vol_args->name, parent, namelen); | |
782 | if (IS_ERR(dentry)) { | |
783 | err = PTR_ERR(dentry); | |
784 | goto out_unlock_dir; | |
785 | } | |
786 | ||
787 | if (!dentry->d_inode) { | |
788 | err = -ENOENT; | |
789 | goto out_dput; | |
790 | } | |
791 | ||
792 | inode = dentry->d_inode; | |
793 | if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) { | |
794 | err = -EINVAL; | |
795 | goto out_dput; | |
796 | } | |
797 | ||
798 | dest = BTRFS_I(inode)->root; | |
799 | ||
800 | mutex_lock(&inode->i_mutex); | |
801 | err = d_invalidate(dentry); | |
802 | if (err) | |
803 | goto out_unlock; | |
804 | ||
805 | down_write(&root->fs_info->subvol_sem); | |
806 | ||
807 | err = may_destroy_subvol(dest); | |
808 | if (err) | |
809 | goto out_up_write; | |
810 | ||
811 | trans = btrfs_start_transaction(root, 1); | |
812 | ret = btrfs_unlink_subvol(trans, root, dir, | |
813 | dest->root_key.objectid, | |
814 | dentry->d_name.name, | |
815 | dentry->d_name.len); | |
816 | BUG_ON(ret); | |
817 | ||
818 | btrfs_record_root_in_trans(trans, dest); | |
819 | ||
820 | memset(&dest->root_item.drop_progress, 0, | |
821 | sizeof(dest->root_item.drop_progress)); | |
822 | dest->root_item.drop_level = 0; | |
823 | btrfs_set_root_refs(&dest->root_item, 0); | |
824 | ||
825 | ret = btrfs_insert_orphan_item(trans, | |
826 | root->fs_info->tree_root, | |
827 | dest->root_key.objectid); | |
828 | BUG_ON(ret); | |
829 | ||
830 | ret = btrfs_commit_transaction(trans, root); | |
831 | BUG_ON(ret); | |
832 | inode->i_flags |= S_DEAD; | |
833 | out_up_write: | |
834 | up_write(&root->fs_info->subvol_sem); | |
835 | out_unlock: | |
836 | mutex_unlock(&inode->i_mutex); | |
837 | if (!err) { | |
efefb143 | 838 | shrink_dcache_sb(root->fs_info->sb); |
76dda93c YZ |
839 | btrfs_invalidate_inodes(dest); |
840 | d_delete(dentry); | |
841 | } | |
842 | out_dput: | |
843 | dput(dentry); | |
844 | out_unlock_dir: | |
845 | mutex_unlock(&dir->i_mutex); | |
846 | mnt_drop_write(file->f_path.mnt); | |
847 | out: | |
848 | kfree(vol_args); | |
849 | return err; | |
850 | } | |
851 | ||
f46b5a66 CH |
852 | static int btrfs_ioctl_defrag(struct file *file) |
853 | { | |
854 | struct inode *inode = fdentry(file)->d_inode; | |
855 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
c146afad YZ |
856 | int ret; |
857 | ||
858 | ret = mnt_want_write(file->f_path.mnt); | |
859 | if (ret) | |
860 | return ret; | |
f46b5a66 CH |
861 | |
862 | switch (inode->i_mode & S_IFMT) { | |
863 | case S_IFDIR: | |
e441d54d CM |
864 | if (!capable(CAP_SYS_ADMIN)) { |
865 | ret = -EPERM; | |
866 | goto out; | |
867 | } | |
f46b5a66 CH |
868 | btrfs_defrag_root(root, 0); |
869 | btrfs_defrag_root(root->fs_info->extent_root, 0); | |
f46b5a66 CH |
870 | break; |
871 | case S_IFREG: | |
e441d54d CM |
872 | if (!(file->f_mode & FMODE_WRITE)) { |
873 | ret = -EINVAL; | |
874 | goto out; | |
875 | } | |
f46b5a66 CH |
876 | btrfs_defrag_file(file); |
877 | break; | |
878 | } | |
e441d54d | 879 | out: |
ab67b7c1 | 880 | mnt_drop_write(file->f_path.mnt); |
e441d54d | 881 | return ret; |
f46b5a66 CH |
882 | } |
883 | ||
b2950863 | 884 | static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg) |
f46b5a66 CH |
885 | { |
886 | struct btrfs_ioctl_vol_args *vol_args; | |
887 | int ret; | |
888 | ||
e441d54d CM |
889 | if (!capable(CAP_SYS_ADMIN)) |
890 | return -EPERM; | |
891 | ||
dae7b665 LZ |
892 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
893 | if (IS_ERR(vol_args)) | |
894 | return PTR_ERR(vol_args); | |
f46b5a66 | 895 | |
5516e595 | 896 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
f46b5a66 CH |
897 | ret = btrfs_init_new_device(root, vol_args->name); |
898 | ||
f46b5a66 CH |
899 | kfree(vol_args); |
900 | return ret; | |
901 | } | |
902 | ||
b2950863 | 903 | static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg) |
f46b5a66 CH |
904 | { |
905 | struct btrfs_ioctl_vol_args *vol_args; | |
906 | int ret; | |
907 | ||
e441d54d CM |
908 | if (!capable(CAP_SYS_ADMIN)) |
909 | return -EPERM; | |
910 | ||
c146afad YZ |
911 | if (root->fs_info->sb->s_flags & MS_RDONLY) |
912 | return -EROFS; | |
913 | ||
dae7b665 LZ |
914 | vol_args = memdup_user(arg, sizeof(*vol_args)); |
915 | if (IS_ERR(vol_args)) | |
916 | return PTR_ERR(vol_args); | |
f46b5a66 | 917 | |
5516e595 | 918 | vol_args->name[BTRFS_PATH_NAME_MAX] = '\0'; |
f46b5a66 CH |
919 | ret = btrfs_rm_device(root, vol_args->name); |
920 | ||
f46b5a66 CH |
921 | kfree(vol_args); |
922 | return ret; | |
923 | } | |
924 | ||
76dda93c YZ |
925 | static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd, |
926 | u64 off, u64 olen, u64 destoff) | |
f46b5a66 CH |
927 | { |
928 | struct inode *inode = fdentry(file)->d_inode; | |
929 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
930 | struct file *src_file; | |
931 | struct inode *src; | |
932 | struct btrfs_trans_handle *trans; | |
f46b5a66 | 933 | struct btrfs_path *path; |
f46b5a66 | 934 | struct extent_buffer *leaf; |
ae01a0ab YZ |
935 | char *buf; |
936 | struct btrfs_key key; | |
f46b5a66 CH |
937 | u32 nritems; |
938 | int slot; | |
ae01a0ab | 939 | int ret; |
c5c9cd4d SW |
940 | u64 len = olen; |
941 | u64 bs = root->fs_info->sb->s_blocksize; | |
942 | u64 hint_byte; | |
d20f7043 | 943 | |
c5c9cd4d SW |
944 | /* |
945 | * TODO: | |
946 | * - split compressed inline extents. annoying: we need to | |
947 | * decompress into destination's address_space (the file offset | |
948 | * may change, so source mapping won't do), then recompress (or | |
949 | * otherwise reinsert) a subrange. | |
950 | * - allow ranges within the same file to be cloned (provided | |
951 | * they don't overlap)? | |
952 | */ | |
953 | ||
e441d54d CM |
954 | /* the destination must be opened for writing */ |
955 | if (!(file->f_mode & FMODE_WRITE)) | |
956 | return -EINVAL; | |
957 | ||
c146afad YZ |
958 | ret = mnt_want_write(file->f_path.mnt); |
959 | if (ret) | |
960 | return ret; | |
961 | ||
c5c9cd4d | 962 | src_file = fget(srcfd); |
ab67b7c1 YZ |
963 | if (!src_file) { |
964 | ret = -EBADF; | |
965 | goto out_drop_write; | |
966 | } | |
f46b5a66 CH |
967 | src = src_file->f_dentry->d_inode; |
968 | ||
c5c9cd4d SW |
969 | ret = -EINVAL; |
970 | if (src == inode) | |
971 | goto out_fput; | |
972 | ||
ae01a0ab YZ |
973 | ret = -EISDIR; |
974 | if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode)) | |
975 | goto out_fput; | |
976 | ||
f46b5a66 | 977 | ret = -EXDEV; |
ae01a0ab YZ |
978 | if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root) |
979 | goto out_fput; | |
980 | ||
981 | ret = -ENOMEM; | |
982 | buf = vmalloc(btrfs_level_size(root, 0)); | |
983 | if (!buf) | |
984 | goto out_fput; | |
985 | ||
986 | path = btrfs_alloc_path(); | |
987 | if (!path) { | |
988 | vfree(buf); | |
f46b5a66 | 989 | goto out_fput; |
ae01a0ab YZ |
990 | } |
991 | path->reada = 2; | |
f46b5a66 CH |
992 | |
993 | if (inode < src) { | |
994 | mutex_lock(&inode->i_mutex); | |
995 | mutex_lock(&src->i_mutex); | |
996 | } else { | |
997 | mutex_lock(&src->i_mutex); | |
998 | mutex_lock(&inode->i_mutex); | |
999 | } | |
1000 | ||
c5c9cd4d SW |
1001 | /* determine range to clone */ |
1002 | ret = -EINVAL; | |
1003 | if (off >= src->i_size || off + len > src->i_size) | |
f46b5a66 | 1004 | goto out_unlock; |
c5c9cd4d SW |
1005 | if (len == 0) |
1006 | olen = len = src->i_size - off; | |
1007 | /* if we extend to eof, continue to block boundary */ | |
1008 | if (off + len == src->i_size) | |
1009 | len = ((src->i_size + bs-1) & ~(bs-1)) | |
1010 | - off; | |
1011 | ||
1012 | /* verify the end result is block aligned */ | |
1013 | if ((off & (bs-1)) || | |
1014 | ((off + len) & (bs-1))) | |
1015 | goto out_unlock; | |
1016 | ||
f46b5a66 CH |
1017 | /* do any pending delalloc/csum calc on src, one way or |
1018 | another, and lock file content */ | |
1019 | while (1) { | |
31840ae1 | 1020 | struct btrfs_ordered_extent *ordered; |
c5c9cd4d SW |
1021 | lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
1022 | ordered = btrfs_lookup_first_ordered_extent(inode, off+len); | |
ae01a0ab | 1023 | if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered) |
f46b5a66 | 1024 | break; |
c5c9cd4d | 1025 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
ae01a0ab YZ |
1026 | if (ordered) |
1027 | btrfs_put_ordered_extent(ordered); | |
c5c9cd4d | 1028 | btrfs_wait_ordered_range(src, off, off+len); |
f46b5a66 CH |
1029 | } |
1030 | ||
ae01a0ab YZ |
1031 | trans = btrfs_start_transaction(root, 1); |
1032 | BUG_ON(!trans); | |
1033 | ||
c5c9cd4d | 1034 | /* punch hole in destination first */ |
920bbbfb | 1035 | btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1); |
c5c9cd4d SW |
1036 | |
1037 | /* clone data */ | |
f46b5a66 | 1038 | key.objectid = src->i_ino; |
ae01a0ab YZ |
1039 | key.type = BTRFS_EXTENT_DATA_KEY; |
1040 | key.offset = 0; | |
f46b5a66 CH |
1041 | |
1042 | while (1) { | |
1043 | /* | |
1044 | * note the key will change type as we walk through the | |
1045 | * tree. | |
1046 | */ | |
1047 | ret = btrfs_search_slot(trans, root, &key, path, 0, 0); | |
1048 | if (ret < 0) | |
1049 | goto out; | |
1050 | ||
ae01a0ab YZ |
1051 | nritems = btrfs_header_nritems(path->nodes[0]); |
1052 | if (path->slots[0] >= nritems) { | |
f46b5a66 CH |
1053 | ret = btrfs_next_leaf(root, path); |
1054 | if (ret < 0) | |
1055 | goto out; | |
1056 | if (ret > 0) | |
1057 | break; | |
ae01a0ab | 1058 | nritems = btrfs_header_nritems(path->nodes[0]); |
f46b5a66 CH |
1059 | } |
1060 | leaf = path->nodes[0]; | |
1061 | slot = path->slots[0]; | |
f46b5a66 | 1062 | |
ae01a0ab | 1063 | btrfs_item_key_to_cpu(leaf, &key, slot); |
d20f7043 | 1064 | if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY || |
f46b5a66 CH |
1065 | key.objectid != src->i_ino) |
1066 | break; | |
1067 | ||
c5c9cd4d SW |
1068 | if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) { |
1069 | struct btrfs_file_extent_item *extent; | |
1070 | int type; | |
31840ae1 ZY |
1071 | u32 size; |
1072 | struct btrfs_key new_key; | |
c5c9cd4d SW |
1073 | u64 disko = 0, diskl = 0; |
1074 | u64 datao = 0, datal = 0; | |
1075 | u8 comp; | |
31840ae1 ZY |
1076 | |
1077 | size = btrfs_item_size_nr(leaf, slot); | |
1078 | read_extent_buffer(leaf, buf, | |
1079 | btrfs_item_ptr_offset(leaf, slot), | |
1080 | size); | |
c5c9cd4d SW |
1081 | |
1082 | extent = btrfs_item_ptr(leaf, slot, | |
1083 | struct btrfs_file_extent_item); | |
1084 | comp = btrfs_file_extent_compression(leaf, extent); | |
1085 | type = btrfs_file_extent_type(leaf, extent); | |
c8a894d7 CM |
1086 | if (type == BTRFS_FILE_EXTENT_REG || |
1087 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
d397712b CM |
1088 | disko = btrfs_file_extent_disk_bytenr(leaf, |
1089 | extent); | |
1090 | diskl = btrfs_file_extent_disk_num_bytes(leaf, | |
1091 | extent); | |
c5c9cd4d | 1092 | datao = btrfs_file_extent_offset(leaf, extent); |
d397712b CM |
1093 | datal = btrfs_file_extent_num_bytes(leaf, |
1094 | extent); | |
c5c9cd4d SW |
1095 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
1096 | /* take upper bound, may be compressed */ | |
1097 | datal = btrfs_file_extent_ram_bytes(leaf, | |
1098 | extent); | |
1099 | } | |
31840ae1 ZY |
1100 | btrfs_release_path(root, path); |
1101 | ||
c5c9cd4d SW |
1102 | if (key.offset + datal < off || |
1103 | key.offset >= off+len) | |
1104 | goto next; | |
1105 | ||
31840ae1 ZY |
1106 | memcpy(&new_key, &key, sizeof(new_key)); |
1107 | new_key.objectid = inode->i_ino; | |
c5c9cd4d | 1108 | new_key.offset = key.offset + destoff - off; |
31840ae1 | 1109 | |
c8a894d7 CM |
1110 | if (type == BTRFS_FILE_EXTENT_REG || |
1111 | type == BTRFS_FILE_EXTENT_PREALLOC) { | |
c5c9cd4d SW |
1112 | ret = btrfs_insert_empty_item(trans, root, path, |
1113 | &new_key, size); | |
1114 | if (ret) | |
1115 | goto out; | |
1116 | ||
1117 | leaf = path->nodes[0]; | |
1118 | slot = path->slots[0]; | |
1119 | write_extent_buffer(leaf, buf, | |
31840ae1 ZY |
1120 | btrfs_item_ptr_offset(leaf, slot), |
1121 | size); | |
ae01a0ab | 1122 | |
c5c9cd4d | 1123 | extent = btrfs_item_ptr(leaf, slot, |
f46b5a66 | 1124 | struct btrfs_file_extent_item); |
c5c9cd4d SW |
1125 | |
1126 | if (off > key.offset) { | |
1127 | datao += off - key.offset; | |
1128 | datal -= off - key.offset; | |
1129 | } | |
ac6889cb CM |
1130 | |
1131 | if (key.offset + datal > off + len) | |
1132 | datal = off + len - key.offset; | |
1133 | ||
c5c9cd4d SW |
1134 | /* disko == 0 means it's a hole */ |
1135 | if (!disko) | |
1136 | datao = 0; | |
c5c9cd4d SW |
1137 | |
1138 | btrfs_set_file_extent_offset(leaf, extent, | |
1139 | datao); | |
1140 | btrfs_set_file_extent_num_bytes(leaf, extent, | |
1141 | datal); | |
1142 | if (disko) { | |
1143 | inode_add_bytes(inode, datal); | |
ae01a0ab | 1144 | ret = btrfs_inc_extent_ref(trans, root, |
5d4f98a2 YZ |
1145 | disko, diskl, 0, |
1146 | root->root_key.objectid, | |
1147 | inode->i_ino, | |
1148 | new_key.offset - datao); | |
31840ae1 | 1149 | BUG_ON(ret); |
f46b5a66 | 1150 | } |
c5c9cd4d SW |
1151 | } else if (type == BTRFS_FILE_EXTENT_INLINE) { |
1152 | u64 skip = 0; | |
1153 | u64 trim = 0; | |
1154 | if (off > key.offset) { | |
1155 | skip = off - key.offset; | |
1156 | new_key.offset += skip; | |
1157 | } | |
d397712b | 1158 | |
c5c9cd4d SW |
1159 | if (key.offset + datal > off+len) |
1160 | trim = key.offset + datal - (off+len); | |
d397712b | 1161 | |
c5c9cd4d | 1162 | if (comp && (skip || trim)) { |
c5c9cd4d SW |
1163 | ret = -EINVAL; |
1164 | goto out; | |
1165 | } | |
1166 | size -= skip + trim; | |
1167 | datal -= skip + trim; | |
1168 | ret = btrfs_insert_empty_item(trans, root, path, | |
1169 | &new_key, size); | |
1170 | if (ret) | |
1171 | goto out; | |
1172 | ||
1173 | if (skip) { | |
d397712b CM |
1174 | u32 start = |
1175 | btrfs_file_extent_calc_inline_size(0); | |
c5c9cd4d SW |
1176 | memmove(buf+start, buf+start+skip, |
1177 | datal); | |
1178 | } | |
1179 | ||
1180 | leaf = path->nodes[0]; | |
1181 | slot = path->slots[0]; | |
1182 | write_extent_buffer(leaf, buf, | |
1183 | btrfs_item_ptr_offset(leaf, slot), | |
1184 | size); | |
1185 | inode_add_bytes(inode, datal); | |
f46b5a66 | 1186 | } |
c5c9cd4d SW |
1187 | |
1188 | btrfs_mark_buffer_dirty(leaf); | |
ae01a0ab | 1189 | } |
c5c9cd4d | 1190 | |
d397712b | 1191 | next: |
31840ae1 | 1192 | btrfs_release_path(root, path); |
f46b5a66 | 1193 | key.offset++; |
f46b5a66 | 1194 | } |
f46b5a66 CH |
1195 | ret = 0; |
1196 | out: | |
ae01a0ab YZ |
1197 | btrfs_release_path(root, path); |
1198 | if (ret == 0) { | |
1199 | inode->i_mtime = inode->i_ctime = CURRENT_TIME; | |
c5c9cd4d SW |
1200 | if (destoff + olen > inode->i_size) |
1201 | btrfs_i_size_write(inode, destoff + olen); | |
ae01a0ab YZ |
1202 | BTRFS_I(inode)->flags = BTRFS_I(src)->flags; |
1203 | ret = btrfs_update_inode(trans, root, inode); | |
1204 | } | |
f46b5a66 | 1205 | btrfs_end_transaction(trans, root); |
c5c9cd4d | 1206 | unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS); |
ae01a0ab YZ |
1207 | if (ret) |
1208 | vmtruncate(inode, 0); | |
f46b5a66 CH |
1209 | out_unlock: |
1210 | mutex_unlock(&src->i_mutex); | |
1211 | mutex_unlock(&inode->i_mutex); | |
ae01a0ab YZ |
1212 | vfree(buf); |
1213 | btrfs_free_path(path); | |
f46b5a66 CH |
1214 | out_fput: |
1215 | fput(src_file); | |
ab67b7c1 YZ |
1216 | out_drop_write: |
1217 | mnt_drop_write(file->f_path.mnt); | |
f46b5a66 CH |
1218 | return ret; |
1219 | } | |
1220 | ||
7a865e8a | 1221 | static long btrfs_ioctl_clone_range(struct file *file, void __user *argp) |
c5c9cd4d SW |
1222 | { |
1223 | struct btrfs_ioctl_clone_range_args args; | |
1224 | ||
7a865e8a | 1225 | if (copy_from_user(&args, argp, sizeof(args))) |
c5c9cd4d SW |
1226 | return -EFAULT; |
1227 | return btrfs_ioctl_clone(file, args.src_fd, args.src_offset, | |
1228 | args.src_length, args.dest_offset); | |
1229 | } | |
1230 | ||
f46b5a66 CH |
1231 | /* |
1232 | * there are many ways the trans_start and trans_end ioctls can lead | |
1233 | * to deadlocks. They should only be used by applications that | |
1234 | * basically own the machine, and have a very in depth understanding | |
1235 | * of all the possible deadlocks and enospc problems. | |
1236 | */ | |
b2950863 | 1237 | static long btrfs_ioctl_trans_start(struct file *file) |
f46b5a66 CH |
1238 | { |
1239 | struct inode *inode = fdentry(file)->d_inode; | |
1240 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1241 | struct btrfs_trans_handle *trans; | |
1ab86aed | 1242 | int ret; |
f46b5a66 | 1243 | |
1ab86aed | 1244 | ret = -EPERM; |
df5b5520 | 1245 | if (!capable(CAP_SYS_ADMIN)) |
1ab86aed | 1246 | goto out; |
df5b5520 | 1247 | |
1ab86aed SW |
1248 | ret = -EINPROGRESS; |
1249 | if (file->private_data) | |
f46b5a66 | 1250 | goto out; |
9ca9ee09 | 1251 | |
c146afad YZ |
1252 | ret = mnt_want_write(file->f_path.mnt); |
1253 | if (ret) | |
1254 | goto out; | |
1255 | ||
9ca9ee09 SW |
1256 | mutex_lock(&root->fs_info->trans_mutex); |
1257 | root->fs_info->open_ioctl_trans++; | |
1258 | mutex_unlock(&root->fs_info->trans_mutex); | |
1259 | ||
1ab86aed | 1260 | ret = -ENOMEM; |
9ca9ee09 | 1261 | trans = btrfs_start_ioctl_transaction(root, 0); |
1ab86aed SW |
1262 | if (!trans) |
1263 | goto out_drop; | |
1264 | ||
1265 | file->private_data = trans; | |
1266 | return 0; | |
1267 | ||
1268 | out_drop: | |
1269 | mutex_lock(&root->fs_info->trans_mutex); | |
1270 | root->fs_info->open_ioctl_trans--; | |
1271 | mutex_unlock(&root->fs_info->trans_mutex); | |
1272 | mnt_drop_write(file->f_path.mnt); | |
f46b5a66 | 1273 | out: |
f46b5a66 CH |
1274 | return ret; |
1275 | } | |
1276 | ||
1277 | /* | |
1278 | * there are many ways the trans_start and trans_end ioctls can lead | |
1279 | * to deadlocks. They should only be used by applications that | |
1280 | * basically own the machine, and have a very in depth understanding | |
1281 | * of all the possible deadlocks and enospc problems. | |
1282 | */ | |
1283 | long btrfs_ioctl_trans_end(struct file *file) | |
1284 | { | |
1285 | struct inode *inode = fdentry(file)->d_inode; | |
1286 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
1287 | struct btrfs_trans_handle *trans; | |
f46b5a66 | 1288 | |
f46b5a66 | 1289 | trans = file->private_data; |
1ab86aed SW |
1290 | if (!trans) |
1291 | return -EINVAL; | |
b214107e | 1292 | file->private_data = NULL; |
9ca9ee09 | 1293 | |
1ab86aed SW |
1294 | btrfs_end_transaction(trans, root); |
1295 | ||
9ca9ee09 SW |
1296 | mutex_lock(&root->fs_info->trans_mutex); |
1297 | root->fs_info->open_ioctl_trans--; | |
1298 | mutex_unlock(&root->fs_info->trans_mutex); | |
1299 | ||
cfc8ea87 | 1300 | mnt_drop_write(file->f_path.mnt); |
1ab86aed | 1301 | return 0; |
f46b5a66 CH |
1302 | } |
1303 | ||
1304 | long btrfs_ioctl(struct file *file, unsigned int | |
1305 | cmd, unsigned long arg) | |
1306 | { | |
1307 | struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root; | |
4bcabaa3 | 1308 | void __user *argp = (void __user *)arg; |
f46b5a66 CH |
1309 | |
1310 | switch (cmd) { | |
6cbff00f CH |
1311 | case FS_IOC_GETFLAGS: |
1312 | return btrfs_ioctl_getflags(file, argp); | |
1313 | case FS_IOC_SETFLAGS: | |
1314 | return btrfs_ioctl_setflags(file, argp); | |
1315 | case FS_IOC_GETVERSION: | |
1316 | return btrfs_ioctl_getversion(file, argp); | |
f46b5a66 | 1317 | case BTRFS_IOC_SNAP_CREATE: |
4bcabaa3 | 1318 | return btrfs_ioctl_snap_create(file, argp, 0); |
3de4586c | 1319 | case BTRFS_IOC_SUBVOL_CREATE: |
4bcabaa3 | 1320 | return btrfs_ioctl_snap_create(file, argp, 1); |
76dda93c YZ |
1321 | case BTRFS_IOC_SNAP_DESTROY: |
1322 | return btrfs_ioctl_snap_destroy(file, argp); | |
f46b5a66 CH |
1323 | case BTRFS_IOC_DEFRAG: |
1324 | return btrfs_ioctl_defrag(file); | |
1325 | case BTRFS_IOC_RESIZE: | |
4bcabaa3 | 1326 | return btrfs_ioctl_resize(root, argp); |
f46b5a66 | 1327 | case BTRFS_IOC_ADD_DEV: |
4bcabaa3 | 1328 | return btrfs_ioctl_add_dev(root, argp); |
f46b5a66 | 1329 | case BTRFS_IOC_RM_DEV: |
4bcabaa3 | 1330 | return btrfs_ioctl_rm_dev(root, argp); |
f46b5a66 CH |
1331 | case BTRFS_IOC_BALANCE: |
1332 | return btrfs_balance(root->fs_info->dev_root); | |
1333 | case BTRFS_IOC_CLONE: | |
c5c9cd4d SW |
1334 | return btrfs_ioctl_clone(file, arg, 0, 0, 0); |
1335 | case BTRFS_IOC_CLONE_RANGE: | |
7a865e8a | 1336 | return btrfs_ioctl_clone_range(file, argp); |
f46b5a66 CH |
1337 | case BTRFS_IOC_TRANS_START: |
1338 | return btrfs_ioctl_trans_start(file); | |
1339 | case BTRFS_IOC_TRANS_END: | |
1340 | return btrfs_ioctl_trans_end(file); | |
1341 | case BTRFS_IOC_SYNC: | |
1342 | btrfs_sync_fs(file->f_dentry->d_sb, 1); | |
1343 | return 0; | |
1344 | } | |
1345 | ||
1346 | return -ENOTTY; | |
1347 | } |