quota: split out compat_sys_quotactl support from quota.c
[pandora-kernel.git] / fs / quota / quota.c
1 /*
2  * Quota code necessary even when VFS quota support is not compiled
3  * into the kernel.  The interesting stuff is over in dquot.c, here
4  * we have symbols for initial quotactl(2) handling, the sysctl(2)
5  * variables, etc - things needed even when quota support disabled.
6  */
7
8 #include <linux/fs.h>
9 #include <linux/namei.h>
10 #include <linux/slab.h>
11 #include <asm/current.h>
12 #include <asm/uaccess.h>
13 #include <linux/kernel.h>
14 #include <linux/security.h>
15 #include <linux/syscalls.h>
16 #include <linux/buffer_head.h>
17 #include <linux/capability.h>
18 #include <linux/quotaops.h>
19 #include <linux/types.h>
20 #include <linux/writeback.h>
21
22 static int check_quotactl_permission(struct super_block *sb, int type, int cmd,
23                                      qid_t id)
24 {
25         switch (cmd) {
26         /* these commands do not require any special privilegues */
27         case Q_GETFMT:
28         case Q_SYNC:
29         case Q_GETINFO:
30         case Q_XGETQSTAT:
31         case Q_XQUOTASYNC:
32                 break;
33         /* allow to query information for dquots we "own" */
34         case Q_GETQUOTA:
35         case Q_XGETQUOTA:
36                 if ((type == USRQUOTA && current_euid() == id) ||
37                     (type == GRPQUOTA && in_egroup_p(id)))
38                         break;
39                 /*FALLTHROUGH*/
40         default:
41                 if (!capable(CAP_SYS_ADMIN))
42                         return -EPERM;
43         }
44
45         return security_quotactl(cmd, type, id, sb);
46 }
47
48 static int quota_sync_all(int type)
49 {
50         struct super_block *sb;
51         int ret;
52
53         if (type >= MAXQUOTAS)
54                 return -EINVAL;
55         ret = security_quotactl(Q_SYNC, type, 0, NULL);
56         if (ret)
57                 return ret;
58
59         spin_lock(&sb_lock);
60 restart:
61         list_for_each_entry(sb, &super_blocks, s_list) {
62                 if (!sb->s_qcop || !sb->s_qcop->quota_sync)
63                         continue;
64
65                 sb->s_count++;
66                 spin_unlock(&sb_lock);
67                 down_read(&sb->s_umount);
68                 if (sb->s_root)
69                         sb->s_qcop->quota_sync(sb, type, 1);
70                 up_read(&sb->s_umount);
71                 spin_lock(&sb_lock);
72                 if (__put_super_and_need_restart(sb))
73                         goto restart;
74         }
75         spin_unlock(&sb_lock);
76
77         return 0;
78 }
79
80 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
81                          void __user *addr)
82 {
83         char *pathname;
84         int ret = -ENOSYS;
85
86         pathname = getname(addr);
87         if (IS_ERR(pathname))
88                 return PTR_ERR(pathname);
89         if (sb->s_qcop->quota_on)
90                 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
91         putname(pathname);
92         return ret;
93 }
94
95 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
96 {
97         __u32 fmt;
98
99         down_read(&sb_dqopt(sb)->dqptr_sem);
100         if (!sb_has_quota_active(sb, type)) {
101                 up_read(&sb_dqopt(sb)->dqptr_sem);
102                 return -ESRCH;
103         }
104         fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
105         up_read(&sb_dqopt(sb)->dqptr_sem);
106         if (copy_to_user(addr, &fmt, sizeof(fmt)))
107                 return -EFAULT;
108         return 0;
109 }
110
111 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
112 {
113         struct if_dqinfo info;
114         int ret;
115
116         if (!sb_has_quota_active(sb, type))
117                 return -ESRCH;
118         if (!sb->s_qcop->get_info)
119                 return -ENOSYS;
120         ret = sb->s_qcop->get_info(sb, type, &info);
121         if (!ret && copy_to_user(addr, &info, sizeof(info)))
122                 return -EFAULT;
123         return ret;
124 }
125
126 static int quota_setinfo(struct super_block *sb, int type, void __user *addr)
127 {
128         struct if_dqinfo info;
129
130         if (copy_from_user(&info, addr, sizeof(info)))
131                 return -EFAULT;
132         if (!sb_has_quota_active(sb, type))
133                 return -ESRCH;
134         if (!sb->s_qcop->set_info)
135                 return -ENOSYS;
136         return sb->s_qcop->set_info(sb, type, &info);
137 }
138
139 static int quota_getquota(struct super_block *sb, int type, qid_t id,
140                           void __user *addr)
141 {
142         struct if_dqblk idq;
143         int ret;
144
145         if (!sb_has_quota_active(sb, type))
146                 return -ESRCH;
147         if (!sb->s_qcop->get_dqblk)
148                 return -ENOSYS;
149         ret = sb->s_qcop->get_dqblk(sb, type, id, &idq);
150         if (ret)
151                 return ret;
152         if (copy_to_user(addr, &idq, sizeof(idq)))
153                 return -EFAULT;
154         return 0;
155 }
156
157 static int quota_setquota(struct super_block *sb, int type, qid_t id,
158                           void __user *addr)
159 {
160         struct if_dqblk idq;
161
162         if (copy_from_user(&idq, addr, sizeof(idq)))
163                 return -EFAULT;
164         if (!sb_has_quota_active(sb, type))
165                 return -ESRCH;
166         if (!sb->s_qcop->set_dqblk)
167                 return -ENOSYS;
168         return sb->s_qcop->set_dqblk(sb, type, id, &idq);
169 }
170
171 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
172 {
173         __u32 flags;
174
175         if (copy_from_user(&flags, addr, sizeof(flags)))
176                 return -EFAULT;
177         if (!sb->s_qcop->set_xstate)
178                 return -ENOSYS;
179         return sb->s_qcop->set_xstate(sb, flags, cmd);
180 }
181
182 static int quota_getxstate(struct super_block *sb, void __user *addr)
183 {
184         struct fs_quota_stat fqs;
185         int ret;
186
187         if (!sb->s_qcop->get_xstate)
188                 return -ENOSYS;
189         ret = sb->s_qcop->get_xstate(sb, &fqs);
190         if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
191                 return -EFAULT;
192         return ret;
193 }
194
195 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
196                            void __user *addr)
197 {
198         struct fs_disk_quota fdq;
199
200         if (copy_from_user(&fdq, addr, sizeof(fdq)))
201                 return -EFAULT;
202         if (!sb->s_qcop->set_xquota)
203                 return -ENOSYS;
204         return sb->s_qcop->set_xquota(sb, type, id, &fdq);
205 }
206
207 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
208                            void __user *addr)
209 {
210         struct fs_disk_quota fdq;
211         int ret;
212
213         if (!sb->s_qcop->get_xquota)
214                 return -ENOSYS;
215         ret = sb->s_qcop->get_xquota(sb, type, id, &fdq);
216         if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
217                 return -EFAULT;
218         return ret;
219 }
220
221 /* Copy parameters and call proper function */
222 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
223                        void __user *addr)
224 {
225         int ret;
226
227         if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
228                 return -EINVAL;
229         if (!sb->s_qcop)
230                 return -ENOSYS;
231
232         ret = check_quotactl_permission(sb, type, cmd, id);
233         if (ret < 0)
234                 return ret;
235
236         switch (cmd) {
237         case Q_QUOTAON:
238                 return quota_quotaon(sb, type, cmd, id, addr);
239         case Q_QUOTAOFF:
240                 if (!sb->s_qcop->quota_off)
241                         return -ENOSYS;
242                 return sb->s_qcop->quota_off(sb, type, 0);
243         case Q_GETFMT:
244                 return quota_getfmt(sb, type, addr);
245         case Q_GETINFO:
246                 return quota_getinfo(sb, type, addr);
247         case Q_SETINFO:
248                 return quota_setinfo(sb, type, addr);
249         case Q_GETQUOTA:
250                 return quota_getquota(sb, type, id, addr);
251         case Q_SETQUOTA:
252                 return quota_setquota(sb, type, id, addr);
253         case Q_SYNC:
254                 if (!sb->s_qcop->quota_sync)
255                         return -ENOSYS;
256                 return sb->s_qcop->quota_sync(sb, type, 1);
257         case Q_XQUOTAON:
258         case Q_XQUOTAOFF:
259         case Q_XQUOTARM:
260                 return quota_setxstate(sb, cmd, addr);
261         case Q_XGETQSTAT:
262                 return quota_getxstate(sb, addr);
263         case Q_XSETQLIM:
264                 return quota_setxquota(sb, type, id, addr);
265         case Q_XGETQUOTA:
266                 return quota_getxquota(sb, type, id, addr);
267         case Q_XQUOTASYNC:
268                 /* caller already holds s_umount */
269                 if (sb->s_flags & MS_RDONLY)
270                         return -EROFS;
271                 writeback_inodes_sb(sb);
272                 return 0;
273         default:
274                 return -EINVAL;
275         }
276 }
277
278 /*
279  * look up a superblock on which quota ops will be performed
280  * - use the name of a block device to find the superblock thereon
281  */
282 static struct super_block *quotactl_block(const char __user *special)
283 {
284 #ifdef CONFIG_BLOCK
285         struct block_device *bdev;
286         struct super_block *sb;
287         char *tmp = getname(special);
288
289         if (IS_ERR(tmp))
290                 return ERR_CAST(tmp);
291         bdev = lookup_bdev(tmp);
292         putname(tmp);
293         if (IS_ERR(bdev))
294                 return ERR_CAST(bdev);
295         sb = get_super(bdev);
296         bdput(bdev);
297         if (!sb)
298                 return ERR_PTR(-ENODEV);
299
300         return sb;
301 #else
302         return ERR_PTR(-ENODEV);
303 #endif
304 }
305
306 /*
307  * This is the system call interface. This communicates with
308  * the user-level programs. Currently this only supports diskquota
309  * calls. Maybe we need to add the process quotas etc. in the future,
310  * but we probably should use rlimits for that.
311  */
312 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
313                 qid_t, id, void __user *, addr)
314 {
315         uint cmds, type;
316         struct super_block *sb = NULL;
317         int ret;
318
319         cmds = cmd >> SUBCMDSHIFT;
320         type = cmd & SUBCMDMASK;
321
322         /*
323          * As a special case Q_SYNC can be called without a specific device.
324          * It will iterate all superblocks that have quota enabled and call
325          * the sync action on each of them.
326          */
327         if (!special) {
328                 if (cmds == Q_SYNC)
329                         return quota_sync_all(type);
330                 return -ENODEV;
331         }
332
333         sb = quotactl_block(special);
334         if (IS_ERR(sb))
335                 return PTR_ERR(sb);
336
337         ret = do_quotactl(sb, type, cmds, id, addr);
338
339         drop_super(sb);
340         return ret;
341 }