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