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