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