blob: ac2a28f4fa1a7f00e8bc3d5890565eccf44a077e [file] [log] [blame]
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001/*
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>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040024#include <linux/fsnotify.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040025#include <linux/pagemap.h>
26#include <linux/highmem.h>
27#include <linux/time.h>
28#include <linux/init.h>
29#include <linux/string.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040030#include <linux/backing-dev.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040031#include <linux/mount.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040032#include <linux/mpage.h>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040033#include <linux/namei.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040034#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>
Christoph Hellwigcb8e7092008-10-09 13:39:39 -040039#include <linux/security.h>
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040040#include <linux/xattr.h>
Yan Zheng7ea394f2008-08-05 13:05:02 -040041#include <linux/vmalloc.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050042#include "compat.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040043#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"
Chris Mason925baed2008-06-25 16:01:30 -040050#include "locking.h"
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +000051#include "ctree.h"
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040052
Christoph Hellwig6cbff002009-04-17 10:37:41 +020053/* Mask out flags that are inappropriate for the given type of inode. */
54static inline __u32 btrfs_mask_flags(umode_t mode, __u32 flags)
55{
56 if (S_ISDIR(mode))
57 return flags;
58 else if (S_ISREG(mode))
59 return flags & ~FS_DIRSYNC_FL;
60 else
61 return flags & (FS_NODUMP_FL | FS_NOATIME_FL);
62}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -040063
Christoph Hellwig6cbff002009-04-17 10:37:41 +020064/*
65 * Export inode flags to the format expected by the FS_IOC_GETFLAGS ioctl.
66 */
67static unsigned int btrfs_flags_to_ioctl(unsigned int flags)
68{
69 unsigned int iflags = 0;
70
71 if (flags & BTRFS_INODE_SYNC)
72 iflags |= FS_SYNC_FL;
73 if (flags & BTRFS_INODE_IMMUTABLE)
74 iflags |= FS_IMMUTABLE_FL;
75 if (flags & BTRFS_INODE_APPEND)
76 iflags |= FS_APPEND_FL;
77 if (flags & BTRFS_INODE_NODUMP)
78 iflags |= FS_NODUMP_FL;
79 if (flags & BTRFS_INODE_NOATIME)
80 iflags |= FS_NOATIME_FL;
81 if (flags & BTRFS_INODE_DIRSYNC)
82 iflags |= FS_DIRSYNC_FL;
83
84 return iflags;
85}
86
87/*
88 * Update inode->i_flags based on the btrfs internal flags.
89 */
90void btrfs_update_iflags(struct inode *inode)
91{
92 struct btrfs_inode *ip = BTRFS_I(inode);
93
94 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
95
96 if (ip->flags & BTRFS_INODE_SYNC)
97 inode->i_flags |= S_SYNC;
98 if (ip->flags & BTRFS_INODE_IMMUTABLE)
99 inode->i_flags |= S_IMMUTABLE;
100 if (ip->flags & BTRFS_INODE_APPEND)
101 inode->i_flags |= S_APPEND;
102 if (ip->flags & BTRFS_INODE_NOATIME)
103 inode->i_flags |= S_NOATIME;
104 if (ip->flags & BTRFS_INODE_DIRSYNC)
105 inode->i_flags |= S_DIRSYNC;
106}
107
108/*
109 * Inherit flags from the parent inode.
110 *
111 * Unlike extN we don't have any flags we don't want to inherit currently.
112 */
113void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
114{
Chris Mason0b4dcea2009-06-11 11:13:35 -0400115 unsigned int flags;
116
117 if (!dir)
118 return;
119
120 flags = BTRFS_I(dir)->flags;
Christoph Hellwig6cbff002009-04-17 10:37:41 +0200121
122 if (S_ISREG(inode->i_mode))
123 flags &= ~BTRFS_INODE_DIRSYNC;
124 else if (!S_ISDIR(inode->i_mode))
125 flags &= (BTRFS_INODE_NODUMP | BTRFS_INODE_NOATIME);
126
127 BTRFS_I(inode)->flags = flags;
128 btrfs_update_iflags(inode);
129}
130
131static int btrfs_ioctl_getflags(struct file *file, void __user *arg)
132{
133 struct btrfs_inode *ip = BTRFS_I(file->f_path.dentry->d_inode);
134 unsigned int flags = btrfs_flags_to_ioctl(ip->flags);
135
136 if (copy_to_user(arg, &flags, sizeof(flags)))
137 return -EFAULT;
138 return 0;
139}
140
141static int btrfs_ioctl_setflags(struct file *file, void __user *arg)
142{
143 struct inode *inode = file->f_path.dentry->d_inode;
144 struct btrfs_inode *ip = BTRFS_I(inode);
145 struct btrfs_root *root = ip->root;
146 struct btrfs_trans_handle *trans;
147 unsigned int flags, oldflags;
148 int ret;
149
150 if (copy_from_user(&flags, arg, sizeof(flags)))
151 return -EFAULT;
152
153 if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
154 FS_NOATIME_FL | FS_NODUMP_FL | \
155 FS_SYNC_FL | FS_DIRSYNC_FL))
156 return -EOPNOTSUPP;
157
158 if (!is_owner_or_cap(inode))
159 return -EACCES;
160
161 mutex_lock(&inode->i_mutex);
162
163 flags = btrfs_mask_flags(inode->i_mode, flags);
164 oldflags = btrfs_flags_to_ioctl(ip->flags);
165 if ((flags ^ oldflags) & (FS_APPEND_FL | FS_IMMUTABLE_FL)) {
166 if (!capable(CAP_LINUX_IMMUTABLE)) {
167 ret = -EPERM;
168 goto out_unlock;
169 }
170 }
171
172 ret = mnt_want_write(file->f_path.mnt);
173 if (ret)
174 goto out_unlock;
175
176 if (flags & FS_SYNC_FL)
177 ip->flags |= BTRFS_INODE_SYNC;
178 else
179 ip->flags &= ~BTRFS_INODE_SYNC;
180 if (flags & FS_IMMUTABLE_FL)
181 ip->flags |= BTRFS_INODE_IMMUTABLE;
182 else
183 ip->flags &= ~BTRFS_INODE_IMMUTABLE;
184 if (flags & FS_APPEND_FL)
185 ip->flags |= BTRFS_INODE_APPEND;
186 else
187 ip->flags &= ~BTRFS_INODE_APPEND;
188 if (flags & FS_NODUMP_FL)
189 ip->flags |= BTRFS_INODE_NODUMP;
190 else
191 ip->flags &= ~BTRFS_INODE_NODUMP;
192 if (flags & FS_NOATIME_FL)
193 ip->flags |= BTRFS_INODE_NOATIME;
194 else
195 ip->flags &= ~BTRFS_INODE_NOATIME;
196 if (flags & FS_DIRSYNC_FL)
197 ip->flags |= BTRFS_INODE_DIRSYNC;
198 else
199 ip->flags &= ~BTRFS_INODE_DIRSYNC;
200
201
202 trans = btrfs_join_transaction(root, 1);
203 BUG_ON(!trans);
204
205 ret = btrfs_update_inode(trans, root, inode);
206 BUG_ON(ret);
207
208 btrfs_update_iflags(inode);
209 inode->i_ctime = CURRENT_TIME;
210 btrfs_end_transaction(trans, root);
211
212 mnt_drop_write(file->f_path.mnt);
213 out_unlock:
214 mutex_unlock(&inode->i_mutex);
215 return 0;
216}
217
218static int btrfs_ioctl_getversion(struct file *file, int __user *arg)
219{
220 struct inode *inode = file->f_path.dentry->d_inode;
221
222 return put_user(inode->i_generation, arg);
223}
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400224
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400225static noinline int create_subvol(struct btrfs_root *root,
226 struct dentry *dentry,
227 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400228{
229 struct btrfs_trans_handle *trans;
230 struct btrfs_key key;
231 struct btrfs_root_item root_item;
232 struct btrfs_inode_item *inode_item;
233 struct extent_buffer *leaf;
Yan, Zheng76dda932009-09-21 16:00:26 -0400234 struct btrfs_root *new_root;
235 struct inode *dir = dentry->d_parent->d_inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400236 int ret;
237 int err;
238 u64 objectid;
239 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Mason3de45862008-11-17 21:02:50 -0500240 u64 index = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400241
Josef Bacik9ed74f22009-09-11 16:12:44 -0400242 /*
243 * 1 - inode item
244 * 2 - refs
245 * 1 - root item
246 * 2 - dir items
247 */
248 ret = btrfs_reserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400249 if (ret)
Yan, Zheng76dda932009-09-21 16:00:26 -0400250 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400251
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
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400260 leaf = btrfs_alloc_free_block(trans, root, root->leafsize,
261 0, objectid, NULL, 0, 0, 0);
Josef Bacik8e8a1e32008-07-24 12:17:14 -0400262 if (IS_ERR(leaf)) {
263 ret = PTR_ERR(leaf);
264 goto fail;
265 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400266
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400267 memset_extent_buffer(leaf, 0, 0, sizeof(struct btrfs_header));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400268 btrfs_set_header_bytenr(leaf, leaf->start);
269 btrfs_set_header_generation(leaf, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400270 btrfs_set_header_backref_rev(leaf, BTRFS_MIXED_BACKREF_REV);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400271 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);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400276 write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
277 (unsigned long)btrfs_header_chunk_tree_uuid(leaf),
278 BTRFS_UUID_SIZE);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400279 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);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400286 inode_item->nbytes = cpu_to_le64(root->leafsize);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400287 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
288
289 btrfs_set_root_bytenr(&root_item, leaf->start);
Yan Zheng84234f32008-10-29 14:49:05 -0400290 btrfs_set_root_generation(&root_item, trans->transid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400291 btrfs_set_root_level(&root_item, 0);
292 btrfs_set_root_refs(&root_item, 1);
Yan, Zheng86b9f2e2009-11-12 09:36:50 +0000293 btrfs_set_root_used(&root_item, leaf->len);
Yan Zheng80ff3852008-10-30 14:20:02 -0400294 btrfs_set_root_last_snapshot(&root_item, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400295
296 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
297 root_item.drop_level = 0;
298
Chris Mason925baed2008-06-25 16:01:30 -0400299 btrfs_tree_unlock(leaf);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400300 free_extent_buffer(leaf);
301 leaf = NULL;
302
303 btrfs_set_root_dirid(&root_item, new_dirid);
304
305 key.objectid = objectid;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400306 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400307 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
Yan, Zheng76dda932009-09-21 16:00:26 -0400313 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);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400321 /*
322 * insert the directory item
323 */
Chris Mason3de45862008-11-17 21:02:50 -0500324 ret = btrfs_set_inode_index(dir, &index);
325 BUG_ON(ret);
326
327 ret = btrfs_insert_dir_item(trans, root,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400328 name, namelen, dir->i_ino, &key,
Chris Mason3de45862008-11-17 21:02:50 -0500329 BTRFS_FT_DIR, index);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400330 if (ret)
331 goto fail;
Chris Mason0660b5a2008-11-17 20:37:39 -0500332
Yan Zheng52c26172009-01-05 15:43:43 -0500333 btrfs_i_size_write(dir, dir->i_size + namelen * 2);
334 ret = btrfs_update_inode(trans, root, dir);
335 BUG_ON(ret);
336
Chris Mason0660b5a2008-11-17 20:37:39 -0500337 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -0400338 objectid, root->root_key.objectid,
Chris Mason0660b5a2008-11-17 20:37:39 -0500339 dir->i_ino, index, name, namelen);
Yan, Zheng76dda932009-09-21 16:00:26 -0400340
Chris Mason0660b5a2008-11-17 20:37:39 -0500341 BUG_ON(ret);
342
Yan, Zheng76dda932009-09-21 16:00:26 -0400343 d_instantiate(dentry, btrfs_lookup_dentry(dir, dentry));
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400344fail:
Yan, Zheng76dda932009-09-21 16:00:26 -0400345 err = btrfs_commit_transaction(trans, root);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400346 if (err && !ret)
347 ret = err;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400348
349 btrfs_unreserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400350 return ret;
351}
352
Chris Mason3de45862008-11-17 21:02:50 -0500353static int create_snapshot(struct btrfs_root *root, struct dentry *dentry,
354 char *name, int namelen)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400355{
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000356 struct inode *inode;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400357 struct btrfs_pending_snapshot *pending_snapshot;
358 struct btrfs_trans_handle *trans;
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000359 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400360
361 if (!root->ref_cows)
362 return -EINVAL;
363
Josef Bacik9ed74f22009-09-11 16:12:44 -0400364 /*
365 * 1 - inode item
366 * 2 - refs
367 * 1 - root item
368 * 2 - dir items
369 */
370 ret = btrfs_reserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400371 if (ret)
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000372 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400373
Chris Mason3de45862008-11-17 21:02:50 -0500374 pending_snapshot = kzalloc(sizeof(*pending_snapshot), GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400375 if (!pending_snapshot) {
376 ret = -ENOMEM;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400377 btrfs_unreserve_metadata_space(root, 6);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000378 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400379 }
380 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
381 if (!pending_snapshot->name) {
382 ret = -ENOMEM;
383 kfree(pending_snapshot);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400384 btrfs_unreserve_metadata_space(root, 6);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000385 goto fail;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400386 }
387 memcpy(pending_snapshot->name, name, namelen);
388 pending_snapshot->name[namelen] = '\0';
Chris Mason3de45862008-11-17 21:02:50 -0500389 pending_snapshot->dentry = dentry;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400390 trans = btrfs_start_transaction(root, 1);
391 BUG_ON(!trans);
392 pending_snapshot->root = root;
393 list_add(&pending_snapshot->list,
394 &trans->transaction->pending_snapshots);
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000395 ret = btrfs_commit_transaction(trans, root);
396 BUG_ON(ret);
397 btrfs_unreserve_metadata_space(root, 6);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400398
Yan, Zheng2e4bfab2009-11-12 09:37:02 +0000399 inode = btrfs_lookup_dentry(dentry->d_parent->d_inode, dentry);
400 if (IS_ERR(inode)) {
401 ret = PTR_ERR(inode);
402 goto fail;
403 }
404 BUG_ON(!inode);
405 d_instantiate(dentry, inode);
406 ret = 0;
407fail:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400408 return ret;
409}
410
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400411/* copy of may_create in fs/namei.c() */
412static inline int btrfs_may_create(struct inode *dir, struct dentry *child)
413{
414 if (child->d_inode)
415 return -EEXIST;
416 if (IS_DEADDIR(dir))
417 return -ENOENT;
418 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
419}
420
421/*
422 * Create a new subvolume below @parent. This is largely modeled after
423 * sys_mkdirat and vfs_mkdir, but we only do a single component lookup
424 * inside this filesystem so it's quite a bit simpler.
425 */
Yan, Zheng76dda932009-09-21 16:00:26 -0400426static noinline int btrfs_mksubvol(struct path *parent,
427 char *name, int namelen,
Chris Mason3de45862008-11-17 21:02:50 -0500428 struct btrfs_root *snap_src)
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400429{
Yan, Zheng76dda932009-09-21 16:00:26 -0400430 struct inode *dir = parent->dentry->d_inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400431 struct dentry *dentry;
432 int error;
433
Yan, Zheng76dda932009-09-21 16:00:26 -0400434 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400435
436 dentry = lookup_one_len(name, parent->dentry, namelen);
437 error = PTR_ERR(dentry);
438 if (IS_ERR(dentry))
439 goto out_unlock;
440
441 error = -EEXIST;
442 if (dentry->d_inode)
443 goto out_dput;
444
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400445 error = mnt_want_write(parent->mnt);
446 if (error)
447 goto out_dput;
448
Yan, Zheng76dda932009-09-21 16:00:26 -0400449 error = btrfs_may_create(dir, dentry);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400450 if (error)
451 goto out_drop_write;
452
Yan, Zheng76dda932009-09-21 16:00:26 -0400453 down_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
454
455 if (btrfs_root_refs(&BTRFS_I(dir)->root->root_item) == 0)
456 goto out_up_read;
457
Chris Mason3de45862008-11-17 21:02:50 -0500458 if (snap_src) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400459 error = create_snapshot(snap_src, dentry,
460 name, namelen);
Chris Mason3de45862008-11-17 21:02:50 -0500461 } else {
Yan, Zheng76dda932009-09-21 16:00:26 -0400462 error = create_subvol(BTRFS_I(dir)->root, dentry,
463 name, namelen);
Chris Mason3de45862008-11-17 21:02:50 -0500464 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400465 if (!error)
466 fsnotify_mkdir(dir, dentry);
467out_up_read:
468 up_read(&BTRFS_I(dir)->root->fs_info->subvol_sem);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400469out_drop_write:
470 mnt_drop_write(parent->mnt);
471out_dput:
472 dput(dentry);
473out_unlock:
Yan, Zheng76dda932009-09-21 16:00:26 -0400474 mutex_unlock(&dir->i_mutex);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400475 return error;
476}
477
Christoph Hellwigb2950862008-12-02 09:54:17 -0500478static int btrfs_defrag_file(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400479{
480 struct inode *inode = fdentry(file)->d_inode;
481 struct btrfs_root *root = BTRFS_I(inode)->root;
482 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason3eaa2882008-07-24 11:57:52 -0400483 struct btrfs_ordered_extent *ordered;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400484 struct page *page;
485 unsigned long last_index;
486 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
487 unsigned long total_read = 0;
488 u64 page_start;
489 u64 page_end;
490 unsigned long i;
491 int ret;
492
Josef Bacik6a632092009-02-20 11:00:09 -0500493 ret = btrfs_check_data_free_space(root, inode, inode->i_size);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400494 if (ret)
495 return -ENOSPC;
496
497 mutex_lock(&inode->i_mutex);
498 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
499 for (i = 0; i <= last_index; i++) {
500 if (total_read % ra_pages == 0) {
501 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
502 min(last_index, i + ra_pages - 1));
503 }
504 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -0400505again:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400506 page = grab_cache_page(inode->i_mapping, i);
507 if (!page)
508 goto out_unlock;
509 if (!PageUptodate(page)) {
510 btrfs_readpage(NULL, page);
511 lock_page(page);
512 if (!PageUptodate(page)) {
513 unlock_page(page);
514 page_cache_release(page);
515 goto out_unlock;
516 }
517 }
518
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400519 wait_on_page_writeback(page);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400520
521 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
522 page_end = page_start + PAGE_CACHE_SIZE - 1;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400523 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason3eaa2882008-07-24 11:57:52 -0400524
525 ordered = btrfs_lookup_ordered_extent(inode, page_start);
526 if (ordered) {
527 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
528 unlock_page(page);
529 page_cache_release(page);
530 btrfs_start_ordered_extent(inode, ordered, 1);
531 btrfs_put_ordered_extent(ordered);
532 goto again;
533 }
534 set_page_extent_mapped(page);
535
Chris Masonf87f0572008-08-01 11:27:23 -0400536 /*
537 * this makes sure page_mkwrite is called on the
538 * page if it is dirtied again later
539 */
540 clear_page_dirty_for_io(page);
541
Chris Masonea8c2812008-08-04 23:17:27 -0400542 btrfs_set_extent_delalloc(inode, page_start, page_end);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400543 set_page_dirty(page);
Chris Masona1ed8352009-09-11 12:27:37 -0400544 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400545 unlock_page(page);
546 page_cache_release(page);
547 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
548 }
549
550out_unlock:
551 mutex_unlock(&inode->i_mutex);
552 return 0;
553}
554
Yan, Zheng76dda932009-09-21 16:00:26 -0400555static noinline int btrfs_ioctl_resize(struct btrfs_root *root,
556 void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400557{
558 u64 new_size;
559 u64 old_size;
560 u64 devid = 1;
561 struct btrfs_ioctl_vol_args *vol_args;
562 struct btrfs_trans_handle *trans;
563 struct btrfs_device *device = NULL;
564 char *sizestr;
565 char *devstr = NULL;
566 int ret = 0;
567 int namelen;
568 int mod = 0;
569
Yan Zhengc146afa2008-11-12 14:34:12 -0500570 if (root->fs_info->sb->s_flags & MS_RDONLY)
571 return -EROFS;
572
Chris Masone441d542009-01-05 16:57:23 -0500573 if (!capable(CAP_SYS_ADMIN))
574 return -EPERM;
575
Li Zefandae7b662009-04-08 15:06:54 +0800576 vol_args = memdup_user(arg, sizeof(*vol_args));
577 if (IS_ERR(vol_args))
578 return PTR_ERR(vol_args);
Mark Fasheh5516e592008-07-24 12:20:14 -0400579
580 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400581 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400582
Chris Mason7d9eb122008-07-08 14:19:17 -0400583 mutex_lock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400584 sizestr = vol_args->name;
585 devstr = strchr(sizestr, ':');
586 if (devstr) {
587 char *end;
588 sizestr = devstr + 1;
589 *devstr = '\0';
590 devstr = vol_args->name;
591 devid = simple_strtoull(devstr, &end, 10);
Joel Becker21380932009-04-21 12:38:29 -0700592 printk(KERN_INFO "resizing devid %llu\n",
593 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400594 }
Yan Zheng2b820322008-11-17 21:11:30 -0500595 device = btrfs_find_device(root, devid, NULL, NULL);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400596 if (!device) {
Joel Becker21380932009-04-21 12:38:29 -0700597 printk(KERN_INFO "resizer unable to find device %llu\n",
598 (unsigned long long)devid);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400599 ret = -EINVAL;
600 goto out_unlock;
601 }
602 if (!strcmp(sizestr, "max"))
603 new_size = device->bdev->bd_inode->i_size;
604 else {
605 if (sizestr[0] == '-') {
606 mod = -1;
607 sizestr++;
608 } else if (sizestr[0] == '+') {
609 mod = 1;
610 sizestr++;
611 }
612 new_size = btrfs_parse_size(sizestr);
613 if (new_size == 0) {
614 ret = -EINVAL;
615 goto out_unlock;
616 }
617 }
618
619 old_size = device->total_bytes;
620
621 if (mod < 0) {
622 if (new_size > old_size) {
623 ret = -EINVAL;
624 goto out_unlock;
625 }
626 new_size = old_size - new_size;
627 } else if (mod > 0) {
628 new_size = old_size + new_size;
629 }
630
631 if (new_size < 256 * 1024 * 1024) {
632 ret = -EINVAL;
633 goto out_unlock;
634 }
635 if (new_size > device->bdev->bd_inode->i_size) {
636 ret = -EFBIG;
637 goto out_unlock;
638 }
639
640 do_div(new_size, root->sectorsize);
641 new_size *= root->sectorsize;
642
643 printk(KERN_INFO "new size for %s is %llu\n",
644 device->name, (unsigned long long)new_size);
645
646 if (new_size > old_size) {
647 trans = btrfs_start_transaction(root, 1);
648 ret = btrfs_grow_device(trans, device, new_size);
649 btrfs_commit_transaction(trans, root);
650 } else {
651 ret = btrfs_shrink_device(device, new_size);
652 }
653
654out_unlock:
Chris Mason7d9eb122008-07-08 14:19:17 -0400655 mutex_unlock(&root->fs_info->volume_mutex);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400656 kfree(vol_args);
657 return ret;
658}
659
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400660static noinline int btrfs_ioctl_snap_create(struct file *file,
Chris Mason3de45862008-11-17 21:02:50 -0500661 void __user *arg, int subvol)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400662{
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400663 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400664 struct btrfs_ioctl_vol_args *vol_args;
Chris Mason3de45862008-11-17 21:02:50 -0500665 struct file *src_file;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400666 int namelen;
Chris Mason3de45862008-11-17 21:02:50 -0500667 int ret = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400668
Yan Zhengc146afa2008-11-12 14:34:12 -0500669 if (root->fs_info->sb->s_flags & MS_RDONLY)
670 return -EROFS;
671
Li Zefandae7b662009-04-08 15:06:54 +0800672 vol_args = memdup_user(arg, sizeof(*vol_args));
673 if (IS_ERR(vol_args))
674 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400675
Mark Fasheh5516e592008-07-24 12:20:14 -0400676 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400677 namelen = strlen(vol_args->name);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400678 if (strchr(vol_args->name, '/')) {
679 ret = -EINVAL;
680 goto out;
681 }
682
Chris Mason3de45862008-11-17 21:02:50 -0500683 if (subvol) {
Yan, Zheng76dda932009-09-21 16:00:26 -0400684 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
685 NULL);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400686 } else {
Chris Mason3de45862008-11-17 21:02:50 -0500687 struct inode *src_inode;
688 src_file = fget(vol_args->fd);
689 if (!src_file) {
690 ret = -EINVAL;
691 goto out;
692 }
693
694 src_inode = src_file->f_path.dentry->d_inode;
695 if (src_inode->i_sb != file->f_path.dentry->d_inode->i_sb) {
Chris Masond3977122009-01-05 21:25:51 -0500696 printk(KERN_INFO "btrfs: Snapshot src from "
697 "another FS\n");
Chris Mason3de45862008-11-17 21:02:50 -0500698 ret = -EINVAL;
699 fput(src_file);
700 goto out;
701 }
Yan, Zheng76dda932009-09-21 16:00:26 -0400702 ret = btrfs_mksubvol(&file->f_path, vol_args->name, namelen,
703 BTRFS_I(src_inode)->root);
Chris Mason3de45862008-11-17 21:02:50 -0500704 fput(src_file);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -0400705 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400706out:
707 kfree(vol_args);
708 return ret;
709}
710
Yan, Zheng76dda932009-09-21 16:00:26 -0400711/*
712 * helper to check if the subvolume references other subvolumes
713 */
714static noinline int may_destroy_subvol(struct btrfs_root *root)
715{
716 struct btrfs_path *path;
717 struct btrfs_key key;
718 int ret;
719
720 path = btrfs_alloc_path();
721 if (!path)
722 return -ENOMEM;
723
724 key.objectid = root->root_key.objectid;
725 key.type = BTRFS_ROOT_REF_KEY;
726 key.offset = (u64)-1;
727
728 ret = btrfs_search_slot(NULL, root->fs_info->tree_root,
729 &key, path, 0, 0);
730 if (ret < 0)
731 goto out;
732 BUG_ON(ret == 0);
733
734 ret = 0;
735 if (path->slots[0] > 0) {
736 path->slots[0]--;
737 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
738 if (key.objectid == root->root_key.objectid &&
739 key.type == BTRFS_ROOT_REF_KEY)
740 ret = -ENOTEMPTY;
741 }
742out:
743 btrfs_free_path(path);
744 return ret;
745}
746
TARUISI Hiroaki98d377a2009-11-18 05:42:14 +0000747/*
748 Search INODE_REFs to identify path name of 'dirid' directory
749 in a 'tree_id' tree. and sets path name to 'name'.
750*/
751static noinline int btrfs_search_path_in_tree(struct btrfs_fs_info *info,
752 u64 tree_id, u64 dirid, char *name)
753{
754 struct btrfs_root *root;
755 struct btrfs_key key;
756 char *name_stack, *ptr;
757 int ret = -1;
758 int slot;
759 int len;
760 int total_len = 0;
761 struct btrfs_inode_ref *iref;
762 struct extent_buffer *l;
763 struct btrfs_path *path;
764
765 if (dirid == BTRFS_FIRST_FREE_OBJECTID) {
766 name[0]='\0';
767 return 0;
768 }
769
770 path = btrfs_alloc_path();
771 if (!path)
772 return -ENOMEM;
773
774 name_stack = kzalloc(BTRFS_PATH_NAME_MAX+1, GFP_NOFS);
775 if (!name_stack) {
776 btrfs_free_path(path);
777 return -ENOMEM;
778 }
779
780 ptr = &name_stack[BTRFS_PATH_NAME_MAX];
781
782 key.objectid = tree_id;
783 key.type = BTRFS_ROOT_ITEM_KEY;
784 key.offset = (u64)-1;
785 root = btrfs_read_fs_root_no_name(info, &key);
786 if (IS_ERR(root)) {
787 printk(KERN_ERR "could not find root %llu\n", tree_id);
788 return -ENOENT;
789 }
790
791 key.objectid = dirid;
792 key.type = BTRFS_INODE_REF_KEY;
793 key.offset = 0;
794
795 while(1) {
796 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
797 if (ret < 0)
798 goto out;
799
800 l = path->nodes[0];
801 slot = path->slots[0];
802 btrfs_item_key_to_cpu(l, &key, slot);
803
804 if (ret > 0 && (key.objectid != dirid ||
805 key.type != BTRFS_INODE_REF_KEY))
806 goto out;
807
808 iref = btrfs_item_ptr(l, slot, struct btrfs_inode_ref);
809 len = btrfs_inode_ref_name_len(l, iref);
810 ptr -= len + 1;
811 total_len += len + 1;
812 if (ptr < name_stack)
813 goto out;
814
815 *(ptr + len) = '/';
816 read_extent_buffer(l, ptr,(unsigned long)(iref + 1), len);
817
818 if (key.offset == BTRFS_FIRST_FREE_OBJECTID)
819 break;
820
821 btrfs_release_path(root, path);
822 key.objectid = key.offset;
823 key.offset = 0;
824 dirid = key.objectid;
825
826 }
827 if (ptr < name_stack)
828 goto out;
829 strncpy(name, ptr, total_len);
830 name[total_len]='\0';
831 ret = 0;
832out:
833 btrfs_free_path(path);
834 kfree(name_stack);
835 return ret;
836}
837
Yan, Zheng76dda932009-09-21 16:00:26 -0400838static noinline int btrfs_ioctl_snap_destroy(struct file *file,
839 void __user *arg)
840{
841 struct dentry *parent = fdentry(file);
842 struct dentry *dentry;
843 struct inode *dir = parent->d_inode;
844 struct inode *inode;
845 struct btrfs_root *root = BTRFS_I(dir)->root;
846 struct btrfs_root *dest = NULL;
847 struct btrfs_ioctl_vol_args *vol_args;
848 struct btrfs_trans_handle *trans;
849 int namelen;
850 int ret;
851 int err = 0;
852
853 if (!capable(CAP_SYS_ADMIN))
854 return -EPERM;
855
856 vol_args = memdup_user(arg, sizeof(*vol_args));
857 if (IS_ERR(vol_args))
858 return PTR_ERR(vol_args);
859
860 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
861 namelen = strlen(vol_args->name);
862 if (strchr(vol_args->name, '/') ||
863 strncmp(vol_args->name, "..", namelen) == 0) {
864 err = -EINVAL;
865 goto out;
866 }
867
868 err = mnt_want_write(file->f_path.mnt);
869 if (err)
870 goto out;
871
872 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
873 dentry = lookup_one_len(vol_args->name, parent, namelen);
874 if (IS_ERR(dentry)) {
875 err = PTR_ERR(dentry);
876 goto out_unlock_dir;
877 }
878
879 if (!dentry->d_inode) {
880 err = -ENOENT;
881 goto out_dput;
882 }
883
884 inode = dentry->d_inode;
885 if (inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
886 err = -EINVAL;
887 goto out_dput;
888 }
889
890 dest = BTRFS_I(inode)->root;
891
892 mutex_lock(&inode->i_mutex);
893 err = d_invalidate(dentry);
894 if (err)
895 goto out_unlock;
896
897 down_write(&root->fs_info->subvol_sem);
898
899 err = may_destroy_subvol(dest);
900 if (err)
901 goto out_up_write;
902
903 trans = btrfs_start_transaction(root, 1);
904 ret = btrfs_unlink_subvol(trans, root, dir,
905 dest->root_key.objectid,
906 dentry->d_name.name,
907 dentry->d_name.len);
908 BUG_ON(ret);
909
910 btrfs_record_root_in_trans(trans, dest);
911
912 memset(&dest->root_item.drop_progress, 0,
913 sizeof(dest->root_item.drop_progress));
914 dest->root_item.drop_level = 0;
915 btrfs_set_root_refs(&dest->root_item, 0);
916
917 ret = btrfs_insert_orphan_item(trans,
918 root->fs_info->tree_root,
919 dest->root_key.objectid);
920 BUG_ON(ret);
921
922 ret = btrfs_commit_transaction(trans, root);
923 BUG_ON(ret);
924 inode->i_flags |= S_DEAD;
925out_up_write:
926 up_write(&root->fs_info->subvol_sem);
927out_unlock:
928 mutex_unlock(&inode->i_mutex);
929 if (!err) {
Yan, Zhengefefb142009-10-09 09:25:16 -0400930 shrink_dcache_sb(root->fs_info->sb);
Yan, Zheng76dda932009-09-21 16:00:26 -0400931 btrfs_invalidate_inodes(dest);
932 d_delete(dentry);
933 }
934out_dput:
935 dput(dentry);
936out_unlock_dir:
937 mutex_unlock(&dir->i_mutex);
938 mnt_drop_write(file->f_path.mnt);
939out:
940 kfree(vol_args);
941 return err;
942}
943
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400944static int btrfs_ioctl_defrag(struct file *file)
945{
946 struct inode *inode = fdentry(file)->d_inode;
947 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengc146afa2008-11-12 14:34:12 -0500948 int ret;
949
950 ret = mnt_want_write(file->f_path.mnt);
951 if (ret)
952 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400953
954 switch (inode->i_mode & S_IFMT) {
955 case S_IFDIR:
Chris Masone441d542009-01-05 16:57:23 -0500956 if (!capable(CAP_SYS_ADMIN)) {
957 ret = -EPERM;
958 goto out;
959 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400960 btrfs_defrag_root(root, 0);
961 btrfs_defrag_root(root->fs_info->extent_root, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400962 break;
963 case S_IFREG:
Chris Masone441d542009-01-05 16:57:23 -0500964 if (!(file->f_mode & FMODE_WRITE)) {
965 ret = -EINVAL;
966 goto out;
967 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400968 btrfs_defrag_file(file);
969 break;
970 }
Chris Masone441d542009-01-05 16:57:23 -0500971out:
Yan Zhengab67b7c2008-12-19 10:58:39 -0500972 mnt_drop_write(file->f_path.mnt);
Chris Masone441d542009-01-05 16:57:23 -0500973 return ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400974}
975
Christoph Hellwigb2950862008-12-02 09:54:17 -0500976static long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400977{
978 struct btrfs_ioctl_vol_args *vol_args;
979 int ret;
980
Chris Masone441d542009-01-05 16:57:23 -0500981 if (!capable(CAP_SYS_ADMIN))
982 return -EPERM;
983
Li Zefandae7b662009-04-08 15:06:54 +0800984 vol_args = memdup_user(arg, sizeof(*vol_args));
985 if (IS_ERR(vol_args))
986 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400987
Mark Fasheh5516e592008-07-24 12:20:14 -0400988 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400989 ret = btrfs_init_new_device(root, vol_args->name);
990
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400991 kfree(vol_args);
992 return ret;
993}
994
Christoph Hellwigb2950862008-12-02 09:54:17 -0500995static long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -0400996{
997 struct btrfs_ioctl_vol_args *vol_args;
998 int ret;
999
Chris Masone441d542009-01-05 16:57:23 -05001000 if (!capable(CAP_SYS_ADMIN))
1001 return -EPERM;
1002
Yan Zhengc146afa2008-11-12 14:34:12 -05001003 if (root->fs_info->sb->s_flags & MS_RDONLY)
1004 return -EROFS;
1005
Li Zefandae7b662009-04-08 15:06:54 +08001006 vol_args = memdup_user(arg, sizeof(*vol_args));
1007 if (IS_ERR(vol_args))
1008 return PTR_ERR(vol_args);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001009
Mark Fasheh5516e592008-07-24 12:20:14 -04001010 vol_args->name[BTRFS_PATH_NAME_MAX] = '\0';
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001011 ret = btrfs_rm_device(root, vol_args->name);
1012
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001013 kfree(vol_args);
1014 return ret;
1015}
1016
Yan, Zheng76dda932009-09-21 16:00:26 -04001017static noinline long btrfs_ioctl_clone(struct file *file, unsigned long srcfd,
1018 u64 off, u64 olen, u64 destoff)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001019{
1020 struct inode *inode = fdentry(file)->d_inode;
1021 struct btrfs_root *root = BTRFS_I(inode)->root;
1022 struct file *src_file;
1023 struct inode *src;
1024 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001025 struct btrfs_path *path;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001026 struct extent_buffer *leaf;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001027 char *buf;
1028 struct btrfs_key key;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001029 u32 nritems;
1030 int slot;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001031 int ret;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001032 u64 len = olen;
1033 u64 bs = root->fs_info->sb->s_blocksize;
1034 u64 hint_byte;
Chris Masond20f7042008-12-08 16:58:54 -05001035
Sage Weilc5c9cd42008-11-12 14:32:25 -05001036 /*
1037 * TODO:
1038 * - split compressed inline extents. annoying: we need to
1039 * decompress into destination's address_space (the file offset
1040 * may change, so source mapping won't do), then recompress (or
1041 * otherwise reinsert) a subrange.
1042 * - allow ranges within the same file to be cloned (provided
1043 * they don't overlap)?
1044 */
1045
Chris Masone441d542009-01-05 16:57:23 -05001046 /* the destination must be opened for writing */
1047 if (!(file->f_mode & FMODE_WRITE))
1048 return -EINVAL;
1049
Yan Zhengc146afa2008-11-12 14:34:12 -05001050 ret = mnt_want_write(file->f_path.mnt);
1051 if (ret)
1052 return ret;
1053
Sage Weilc5c9cd42008-11-12 14:32:25 -05001054 src_file = fget(srcfd);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001055 if (!src_file) {
1056 ret = -EBADF;
1057 goto out_drop_write;
1058 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001059 src = src_file->f_dentry->d_inode;
1060
Sage Weilc5c9cd42008-11-12 14:32:25 -05001061 ret = -EINVAL;
1062 if (src == inode)
1063 goto out_fput;
1064
Yan Zhengae01a0a2008-08-04 23:23:47 -04001065 ret = -EISDIR;
1066 if (S_ISDIR(src->i_mode) || S_ISDIR(inode->i_mode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001067 goto out_fput;
1068
Yan Zhengae01a0a2008-08-04 23:23:47 -04001069 ret = -EXDEV;
1070 if (src->i_sb != inode->i_sb || BTRFS_I(src)->root != root)
1071 goto out_fput;
1072
1073 ret = -ENOMEM;
1074 buf = vmalloc(btrfs_level_size(root, 0));
1075 if (!buf)
1076 goto out_fput;
1077
1078 path = btrfs_alloc_path();
1079 if (!path) {
1080 vfree(buf);
1081 goto out_fput;
1082 }
1083 path->reada = 2;
1084
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001085 if (inode < src) {
1086 mutex_lock(&inode->i_mutex);
1087 mutex_lock(&src->i_mutex);
1088 } else {
1089 mutex_lock(&src->i_mutex);
1090 mutex_lock(&inode->i_mutex);
1091 }
1092
Sage Weilc5c9cd42008-11-12 14:32:25 -05001093 /* determine range to clone */
1094 ret = -EINVAL;
1095 if (off >= src->i_size || off + len > src->i_size)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001096 goto out_unlock;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001097 if (len == 0)
1098 olen = len = src->i_size - off;
1099 /* if we extend to eof, continue to block boundary */
1100 if (off + len == src->i_size)
1101 len = ((src->i_size + bs-1) & ~(bs-1))
1102 - off;
1103
1104 /* verify the end result is block aligned */
1105 if ((off & (bs-1)) ||
1106 ((off + len) & (bs-1)))
1107 goto out_unlock;
1108
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001109 /* do any pending delalloc/csum calc on src, one way or
1110 another, and lock file content */
1111 while (1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001112 struct btrfs_ordered_extent *ordered;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001113 lock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
1114 ordered = btrfs_lookup_first_ordered_extent(inode, off+len);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001115 if (BTRFS_I(src)->delalloc_bytes == 0 && !ordered)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001116 break;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001117 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001118 if (ordered)
1119 btrfs_put_ordered_extent(ordered);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001120 btrfs_wait_ordered_range(src, off, off+len);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001121 }
1122
Yan Zhengae01a0a2008-08-04 23:23:47 -04001123 trans = btrfs_start_transaction(root, 1);
1124 BUG_ON(!trans);
1125
Sage Weilc5c9cd42008-11-12 14:32:25 -05001126 /* punch hole in destination first */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001127 btrfs_drop_extents(trans, inode, off, off + len, &hint_byte, 1);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001128
1129 /* clone data */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001130 key.objectid = src->i_ino;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001131 key.type = BTRFS_EXTENT_DATA_KEY;
1132 key.offset = 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001133
1134 while (1) {
1135 /*
1136 * note the key will change type as we walk through the
1137 * tree.
1138 */
1139 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
1140 if (ret < 0)
1141 goto out;
1142
Yan Zhengae01a0a2008-08-04 23:23:47 -04001143 nritems = btrfs_header_nritems(path->nodes[0]);
1144 if (path->slots[0] >= nritems) {
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001145 ret = btrfs_next_leaf(root, path);
1146 if (ret < 0)
1147 goto out;
1148 if (ret > 0)
1149 break;
Yan Zhengae01a0a2008-08-04 23:23:47 -04001150 nritems = btrfs_header_nritems(path->nodes[0]);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001151 }
1152 leaf = path->nodes[0];
1153 slot = path->slots[0];
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001154
Yan Zhengae01a0a2008-08-04 23:23:47 -04001155 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Masond20f7042008-12-08 16:58:54 -05001156 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001157 key.objectid != src->i_ino)
1158 break;
1159
Sage Weilc5c9cd42008-11-12 14:32:25 -05001160 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
1161 struct btrfs_file_extent_item *extent;
1162 int type;
Zheng Yan31840ae2008-09-23 13:14:14 -04001163 u32 size;
1164 struct btrfs_key new_key;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001165 u64 disko = 0, diskl = 0;
1166 u64 datao = 0, datal = 0;
1167 u8 comp;
Zheng Yan31840ae2008-09-23 13:14:14 -04001168
1169 size = btrfs_item_size_nr(leaf, slot);
1170 read_extent_buffer(leaf, buf,
1171 btrfs_item_ptr_offset(leaf, slot),
1172 size);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001173
1174 extent = btrfs_item_ptr(leaf, slot,
1175 struct btrfs_file_extent_item);
1176 comp = btrfs_file_extent_compression(leaf, extent);
1177 type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8a894d2009-06-27 21:07:03 -04001178 if (type == BTRFS_FILE_EXTENT_REG ||
1179 type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masond3977122009-01-05 21:25:51 -05001180 disko = btrfs_file_extent_disk_bytenr(leaf,
1181 extent);
1182 diskl = btrfs_file_extent_disk_num_bytes(leaf,
1183 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001184 datao = btrfs_file_extent_offset(leaf, extent);
Chris Masond3977122009-01-05 21:25:51 -05001185 datal = btrfs_file_extent_num_bytes(leaf,
1186 extent);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001187 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1188 /* take upper bound, may be compressed */
1189 datal = btrfs_file_extent_ram_bytes(leaf,
1190 extent);
1191 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001192 btrfs_release_path(root, path);
1193
Sage Weilc5c9cd42008-11-12 14:32:25 -05001194 if (key.offset + datal < off ||
1195 key.offset >= off+len)
1196 goto next;
1197
Zheng Yan31840ae2008-09-23 13:14:14 -04001198 memcpy(&new_key, &key, sizeof(new_key));
1199 new_key.objectid = inode->i_ino;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001200 new_key.offset = key.offset + destoff - off;
1201
Chris Masonc8a894d2009-06-27 21:07:03 -04001202 if (type == BTRFS_FILE_EXTENT_REG ||
1203 type == BTRFS_FILE_EXTENT_PREALLOC) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001204 ret = btrfs_insert_empty_item(trans, root, path,
1205 &new_key, size);
1206 if (ret)
1207 goto out;
1208
1209 leaf = path->nodes[0];
1210 slot = path->slots[0];
1211 write_extent_buffer(leaf, buf,
1212 btrfs_item_ptr_offset(leaf, slot),
1213 size);
1214
1215 extent = btrfs_item_ptr(leaf, slot,
1216 struct btrfs_file_extent_item);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001217
1218 if (off > key.offset) {
1219 datao += off - key.offset;
1220 datal -= off - key.offset;
1221 }
Chris Masonac6889c2009-10-09 11:29:53 -04001222
1223 if (key.offset + datal > off + len)
1224 datal = off + len - key.offset;
1225
Sage Weilc5c9cd42008-11-12 14:32:25 -05001226 /* disko == 0 means it's a hole */
1227 if (!disko)
1228 datao = 0;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001229
1230 btrfs_set_file_extent_offset(leaf, extent,
1231 datao);
1232 btrfs_set_file_extent_num_bytes(leaf, extent,
1233 datal);
1234 if (disko) {
1235 inode_add_bytes(inode, datal);
1236 ret = btrfs_inc_extent_ref(trans, root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001237 disko, diskl, 0,
1238 root->root_key.objectid,
1239 inode->i_ino,
1240 new_key.offset - datao);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001241 BUG_ON(ret);
1242 }
1243 } else if (type == BTRFS_FILE_EXTENT_INLINE) {
1244 u64 skip = 0;
1245 u64 trim = 0;
1246 if (off > key.offset) {
1247 skip = off - key.offset;
1248 new_key.offset += skip;
1249 }
Chris Masond3977122009-01-05 21:25:51 -05001250
Sage Weilc5c9cd42008-11-12 14:32:25 -05001251 if (key.offset + datal > off+len)
1252 trim = key.offset + datal - (off+len);
Chris Masond3977122009-01-05 21:25:51 -05001253
Sage Weilc5c9cd42008-11-12 14:32:25 -05001254 if (comp && (skip || trim)) {
Sage Weilc5c9cd42008-11-12 14:32:25 -05001255 ret = -EINVAL;
1256 goto out;
1257 }
1258 size -= skip + trim;
1259 datal -= skip + trim;
1260 ret = btrfs_insert_empty_item(trans, root, path,
1261 &new_key, size);
1262 if (ret)
1263 goto out;
1264
1265 if (skip) {
Chris Masond3977122009-01-05 21:25:51 -05001266 u32 start =
1267 btrfs_file_extent_calc_inline_size(0);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001268 memmove(buf+start, buf+start+skip,
1269 datal);
1270 }
1271
1272 leaf = path->nodes[0];
1273 slot = path->slots[0];
1274 write_extent_buffer(leaf, buf,
1275 btrfs_item_ptr_offset(leaf, slot),
1276 size);
1277 inode_add_bytes(inode, datal);
1278 }
1279
1280 btrfs_mark_buffer_dirty(leaf);
1281 }
1282
Chris Masond3977122009-01-05 21:25:51 -05001283next:
Zheng Yan31840ae2008-09-23 13:14:14 -04001284 btrfs_release_path(root, path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001285 key.offset++;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001286 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001287 ret = 0;
1288out:
Yan Zhengae01a0a2008-08-04 23:23:47 -04001289 btrfs_release_path(root, path);
1290 if (ret == 0) {
1291 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Sage Weilc5c9cd42008-11-12 14:32:25 -05001292 if (destoff + olen > inode->i_size)
1293 btrfs_i_size_write(inode, destoff + olen);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001294 BTRFS_I(inode)->flags = BTRFS_I(src)->flags;
1295 ret = btrfs_update_inode(trans, root, inode);
1296 }
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001297 btrfs_end_transaction(trans, root);
Sage Weilc5c9cd42008-11-12 14:32:25 -05001298 unlock_extent(&BTRFS_I(src)->io_tree, off, off+len, GFP_NOFS);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001299 if (ret)
1300 vmtruncate(inode, 0);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001301out_unlock:
1302 mutex_unlock(&src->i_mutex);
1303 mutex_unlock(&inode->i_mutex);
Yan Zhengae01a0a2008-08-04 23:23:47 -04001304 vfree(buf);
1305 btrfs_free_path(path);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001306out_fput:
1307 fput(src_file);
Yan Zhengab67b7c2008-12-19 10:58:39 -05001308out_drop_write:
1309 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001310 return ret;
1311}
1312
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001313static long btrfs_ioctl_clone_range(struct file *file, void __user *argp)
Sage Weilc5c9cd42008-11-12 14:32:25 -05001314{
1315 struct btrfs_ioctl_clone_range_args args;
1316
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001317 if (copy_from_user(&args, argp, sizeof(args)))
Sage Weilc5c9cd42008-11-12 14:32:25 -05001318 return -EFAULT;
1319 return btrfs_ioctl_clone(file, args.src_fd, args.src_offset,
1320 args.src_length, args.dest_offset);
1321}
1322
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001323/*
1324 * there are many ways the trans_start and trans_end ioctls can lead
1325 * to deadlocks. They should only be used by applications that
1326 * basically own the machine, and have a very in depth understanding
1327 * of all the possible deadlocks and enospc problems.
1328 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001329static long btrfs_ioctl_trans_start(struct file *file)
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001330{
1331 struct inode *inode = fdentry(file)->d_inode;
1332 struct btrfs_root *root = BTRFS_I(inode)->root;
1333 struct btrfs_trans_handle *trans;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001334 int ret;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001335
Sage Weil1ab86ae2009-09-29 18:38:44 -04001336 ret = -EPERM;
Christoph Hellwigdf5b5522008-06-11 21:53:58 -04001337 if (!capable(CAP_SYS_ADMIN))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001338 goto out;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001339
1340 ret = -EINPROGRESS;
1341 if (file->private_data)
1342 goto out;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001343
Yan Zhengc146afa2008-11-12 14:34:12 -05001344 ret = mnt_want_write(file->f_path.mnt);
1345 if (ret)
1346 goto out;
1347
Sage Weil9ca9ee02008-08-04 10:41:27 -04001348 mutex_lock(&root->fs_info->trans_mutex);
1349 root->fs_info->open_ioctl_trans++;
1350 mutex_unlock(&root->fs_info->trans_mutex);
1351
Sage Weil1ab86ae2009-09-29 18:38:44 -04001352 ret = -ENOMEM;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001353 trans = btrfs_start_ioctl_transaction(root, 0);
Sage Weil1ab86ae2009-09-29 18:38:44 -04001354 if (!trans)
1355 goto out_drop;
1356
1357 file->private_data = trans;
1358 return 0;
1359
1360out_drop:
1361 mutex_lock(&root->fs_info->trans_mutex);
1362 root->fs_info->open_ioctl_trans--;
1363 mutex_unlock(&root->fs_info->trans_mutex);
1364 mnt_drop_write(file->f_path.mnt);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001365out:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001366 return ret;
1367}
1368
1369/*
1370 * there are many ways the trans_start and trans_end ioctls can lead
1371 * to deadlocks. They should only be used by applications that
1372 * basically own the machine, and have a very in depth understanding
1373 * of all the possible deadlocks and enospc problems.
1374 */
1375long btrfs_ioctl_trans_end(struct file *file)
1376{
1377 struct inode *inode = fdentry(file)->d_inode;
1378 struct btrfs_root *root = BTRFS_I(inode)->root;
1379 struct btrfs_trans_handle *trans;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001380
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001381 trans = file->private_data;
Sage Weil1ab86ae2009-09-29 18:38:44 -04001382 if (!trans)
1383 return -EINVAL;
Christoph Hellwigb2141072008-09-05 16:43:31 -04001384 file->private_data = NULL;
Sage Weil9ca9ee02008-08-04 10:41:27 -04001385
Sage Weil1ab86ae2009-09-29 18:38:44 -04001386 btrfs_end_transaction(trans, root);
1387
Sage Weil9ca9ee02008-08-04 10:41:27 -04001388 mutex_lock(&root->fs_info->trans_mutex);
1389 root->fs_info->open_ioctl_trans--;
1390 mutex_unlock(&root->fs_info->trans_mutex);
1391
Sage Weilcfc8ea82008-12-11 16:30:06 -05001392 mnt_drop_write(file->f_path.mnt);
Sage Weil1ab86ae2009-09-29 18:38:44 -04001393 return 0;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001394}
1395
1396long btrfs_ioctl(struct file *file, unsigned int
1397 cmd, unsigned long arg)
1398{
1399 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001400 void __user *argp = (void __user *)arg;
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001401
1402 switch (cmd) {
Christoph Hellwig6cbff002009-04-17 10:37:41 +02001403 case FS_IOC_GETFLAGS:
1404 return btrfs_ioctl_getflags(file, argp);
1405 case FS_IOC_SETFLAGS:
1406 return btrfs_ioctl_setflags(file, argp);
1407 case FS_IOC_GETVERSION:
1408 return btrfs_ioctl_getversion(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001409 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001410 return btrfs_ioctl_snap_create(file, argp, 0);
Chris Mason3de45862008-11-17 21:02:50 -05001411 case BTRFS_IOC_SUBVOL_CREATE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001412 return btrfs_ioctl_snap_create(file, argp, 1);
Yan, Zheng76dda932009-09-21 16:00:26 -04001413 case BTRFS_IOC_SNAP_DESTROY:
1414 return btrfs_ioctl_snap_destroy(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001415 case BTRFS_IOC_DEFRAG:
1416 return btrfs_ioctl_defrag(file);
1417 case BTRFS_IOC_RESIZE:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001418 return btrfs_ioctl_resize(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001419 case BTRFS_IOC_ADD_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001420 return btrfs_ioctl_add_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001421 case BTRFS_IOC_RM_DEV:
Christoph Hellwig4bcabaa2008-12-02 06:36:08 -05001422 return btrfs_ioctl_rm_dev(root, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001423 case BTRFS_IOC_BALANCE:
1424 return btrfs_balance(root->fs_info->dev_root);
1425 case BTRFS_IOC_CLONE:
Sage Weilc5c9cd42008-11-12 14:32:25 -05001426 return btrfs_ioctl_clone(file, arg, 0, 0, 0);
1427 case BTRFS_IOC_CLONE_RANGE:
Christoph Hellwig7a865e82008-12-02 09:52:24 -05001428 return btrfs_ioctl_clone_range(file, argp);
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001429 case BTRFS_IOC_TRANS_START:
1430 return btrfs_ioctl_trans_start(file);
1431 case BTRFS_IOC_TRANS_END:
1432 return btrfs_ioctl_trans_end(file);
1433 case BTRFS_IOC_SYNC:
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04001434 btrfs_sync_fs(file->f_dentry->d_sb, 1);
1435 return 0;
1436 }
1437
1438 return -ENOTTY;
1439}