Merge branch 'stable-4.13' of git://git.infradead.org/users/pcmoore/audit
[linux-2.6-block.git] / fs / reiserfs / ioctl.c
1 /*
2  * Copyright 2000 by Hans Reiser, licensing governed by reiserfs/README
3  */
4
5 #include <linux/capability.h>
6 #include <linux/fs.h>
7 #include <linux/mount.h>
8 #include "reiserfs.h"
9 #include <linux/time.h>
10 #include <linux/uaccess.h>
11 #include <linux/pagemap.h>
12 #include <linux/compat.h>
13
14 /*
15  * reiserfs_ioctl - handler for ioctl for inode
16  * supported commands:
17  *  1) REISERFS_IOC_UNPACK - try to unpack tail from direct item into indirect
18  *                           and prevent packing file (argument arg has t
19  *                            be non-zero)
20  *  2) REISERFS_IOC_[GS]ETFLAGS, REISERFS_IOC_[GS]ETVERSION
21  *  3) That's all for a while ...
22  */
23 long reiserfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
24 {
25         struct inode *inode = file_inode(filp);
26         unsigned int flags;
27         int err = 0;
28
29         reiserfs_write_lock(inode->i_sb);
30
31         switch (cmd) {
32         case REISERFS_IOC_UNPACK:
33                 if (S_ISREG(inode->i_mode)) {
34                         if (arg)
35                                 err = reiserfs_unpack(inode, filp);
36                 } else
37                         err = -ENOTTY;
38                 break;
39                 /*
40                  * following two cases are taken from fs/ext2/ioctl.c by Remy
41                  * Card (card@masi.ibp.fr)
42                  */
43         case REISERFS_IOC_GETFLAGS:
44                 if (!reiserfs_attrs(inode->i_sb)) {
45                         err = -ENOTTY;
46                         break;
47                 }
48
49                 flags = REISERFS_I(inode)->i_attrs;
50                 err = put_user(flags, (int __user *)arg);
51                 break;
52         case REISERFS_IOC_SETFLAGS:{
53                         if (!reiserfs_attrs(inode->i_sb)) {
54                                 err = -ENOTTY;
55                                 break;
56                         }
57
58                         err = mnt_want_write_file(filp);
59                         if (err)
60                                 break;
61
62                         if (!inode_owner_or_capable(inode)) {
63                                 err = -EPERM;
64                                 goto setflags_out;
65                         }
66                         if (get_user(flags, (int __user *)arg)) {
67                                 err = -EFAULT;
68                                 goto setflags_out;
69                         }
70                         /*
71                          * Is it quota file? Do not allow user to mess with it
72                          */
73                         if (IS_NOQUOTA(inode)) {
74                                 err = -EPERM;
75                                 goto setflags_out;
76                         }
77                         if (((flags ^ REISERFS_I(inode)->
78                               i_attrs) & (REISERFS_IMMUTABLE_FL |
79                                           REISERFS_APPEND_FL))
80                             && !capable(CAP_LINUX_IMMUTABLE)) {
81                                 err = -EPERM;
82                                 goto setflags_out;
83                         }
84                         if ((flags & REISERFS_NOTAIL_FL) &&
85                             S_ISREG(inode->i_mode)) {
86                                 int result;
87
88                                 result = reiserfs_unpack(inode, filp);
89                                 if (result) {
90                                         err = result;
91                                         goto setflags_out;
92                                 }
93                         }
94                         sd_attrs_to_i_attrs(flags, inode);
95                         REISERFS_I(inode)->i_attrs = flags;
96                         inode->i_ctime = current_time(inode);
97                         mark_inode_dirty(inode);
98 setflags_out:
99                         mnt_drop_write_file(filp);
100                         break;
101                 }
102         case REISERFS_IOC_GETVERSION:
103                 err = put_user(inode->i_generation, (int __user *)arg);
104                 break;
105         case REISERFS_IOC_SETVERSION:
106                 if (!inode_owner_or_capable(inode)) {
107                         err = -EPERM;
108                         break;
109                 }
110                 err = mnt_want_write_file(filp);
111                 if (err)
112                         break;
113                 if (get_user(inode->i_generation, (int __user *)arg)) {
114                         err = -EFAULT;
115                         goto setversion_out;
116                 }
117                 inode->i_ctime = current_time(inode);
118                 mark_inode_dirty(inode);
119 setversion_out:
120                 mnt_drop_write_file(filp);
121                 break;
122         default:
123                 err = -ENOTTY;
124         }
125
126         reiserfs_write_unlock(inode->i_sb);
127
128         return err;
129 }
130
131 #ifdef CONFIG_COMPAT
132 long reiserfs_compat_ioctl(struct file *file, unsigned int cmd,
133                                 unsigned long arg)
134 {
135         /*
136          * These are just misnamed, they actually
137          * get/put from/to user an int
138          */
139         switch (cmd) {
140         case REISERFS_IOC32_UNPACK:
141                 cmd = REISERFS_IOC_UNPACK;
142                 break;
143         case REISERFS_IOC32_GETFLAGS:
144                 cmd = REISERFS_IOC_GETFLAGS;
145                 break;
146         case REISERFS_IOC32_SETFLAGS:
147                 cmd = REISERFS_IOC_SETFLAGS;
148                 break;
149         case REISERFS_IOC32_GETVERSION:
150                 cmd = REISERFS_IOC_GETVERSION;
151                 break;
152         case REISERFS_IOC32_SETVERSION:
153                 cmd = REISERFS_IOC_SETVERSION;
154                 break;
155         default:
156                 return -ENOIOCTLCMD;
157         }
158
159         return reiserfs_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
160 }
161 #endif
162
163 int reiserfs_commit_write(struct file *f, struct page *page,
164                           unsigned from, unsigned to);
165 /*
166  * reiserfs_unpack
167  * Function try to convert tail from direct item into indirect.
168  * It set up nopack attribute in the REISERFS_I(inode)->nopack
169  */
170 int reiserfs_unpack(struct inode *inode, struct file *filp)
171 {
172         int retval = 0;
173         int index;
174         struct page *page;
175         struct address_space *mapping;
176         unsigned long write_from;
177         unsigned long blocksize = inode->i_sb->s_blocksize;
178
179         if (inode->i_size == 0) {
180                 REISERFS_I(inode)->i_flags |= i_nopack_mask;
181                 return 0;
182         }
183         /* ioctl already done */
184         if (REISERFS_I(inode)->i_flags & i_nopack_mask) {
185                 return 0;
186         }
187
188         /* we need to make sure nobody is changing the file size beneath us */
189 {
190         int depth = reiserfs_write_unlock_nested(inode->i_sb);
191         inode_lock(inode);
192         reiserfs_write_lock_nested(inode->i_sb, depth);
193 }
194
195         reiserfs_write_lock(inode->i_sb);
196
197         write_from = inode->i_size & (blocksize - 1);
198         /* if we are on a block boundary, we are already unpacked.  */
199         if (write_from == 0) {
200                 REISERFS_I(inode)->i_flags |= i_nopack_mask;
201                 goto out;
202         }
203
204         /*
205          * we unpack by finding the page with the tail, and calling
206          * __reiserfs_write_begin on that page.  This will force a
207          * reiserfs_get_block to unpack the tail for us.
208          */
209         index = inode->i_size >> PAGE_SHIFT;
210         mapping = inode->i_mapping;
211         page = grab_cache_page(mapping, index);
212         retval = -ENOMEM;
213         if (!page) {
214                 goto out;
215         }
216         retval = __reiserfs_write_begin(page, write_from, 0);
217         if (retval)
218                 goto out_unlock;
219
220         /* conversion can change page contents, must flush */
221         flush_dcache_page(page);
222         retval = reiserfs_commit_write(NULL, page, write_from, write_from);
223         REISERFS_I(inode)->i_flags |= i_nopack_mask;
224
225 out_unlock:
226         unlock_page(page);
227         put_page(page);
228
229 out:
230         inode_unlock(inode);
231         reiserfs_write_unlock(inode->i_sb);
232         return retval;
233 }