4669e7e639bd9ca2c745d5d92d80d9c458c384b8
[linux-3.10.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 (list_empty(&sb->s_instances))
63                         continue;
64                 if (!sb->s_qcop || !sb->s_qcop->quota_sync)
65                         continue;
66
67                 sb->s_count++;
68                 spin_unlock(&sb_lock);
69                 down_read(&sb->s_umount);
70                 if (sb->s_root)
71                         sb->s_qcop->quota_sync(sb, type, 1);
72                 up_read(&sb->s_umount);
73                 spin_lock(&sb_lock);
74                 if (__put_super_and_need_restart(sb))
75                         goto restart;
76         }
77         spin_unlock(&sb_lock);
78
79         return 0;
80 }
81
82 static int quota_quotaon(struct super_block *sb, int type, int cmd, qid_t id,
83                          void __user *addr)
84 {
85         char *pathname;
86         int ret = -ENOSYS;
87
88         pathname = getname(addr);
89         if (IS_ERR(pathname))
90                 return PTR_ERR(pathname);
91         if (sb->s_qcop->quota_on)
92                 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
93         putname(pathname);
94         return ret;
95 }
96
97 static int quota_getfmt(struct super_block *sb, int type, void __user *addr)
98 {
99         __u32 fmt;
100
101         down_read(&sb_dqopt(sb)->dqptr_sem);
102         if (!sb_has_quota_active(sb, type)) {
103                 up_read(&sb_dqopt(sb)->dqptr_sem);
104                 return -ESRCH;
105         }
106         fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
107         up_read(&sb_dqopt(sb)->dqptr_sem);
108         if (copy_to_user(addr, &fmt, sizeof(fmt)))
109                 return -EFAULT;
110         return 0;
111 }
112
113 static int quota_getinfo(struct super_block *sb, int type, void __user *addr)
114 {
115         struct if_dqinfo info;
116         int ret;
117
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->s_qcop->set_info)
133                 return -ENOSYS;
134         return sb->s_qcop->set_info(sb, type, &info);
135 }
136
137 static void copy_to_if_dqblk(struct if_dqblk *dst, struct fs_disk_quota *src)
138 {
139         dst->dqb_bhardlimit = src->d_blk_hardlimit;
140         dst->dqb_bsoftlimit = src->d_blk_softlimit;
141         dst->dqb_curspace = src->d_bcount;
142         dst->dqb_ihardlimit = src->d_ino_hardlimit;
143         dst->dqb_isoftlimit = src->d_ino_softlimit;
144         dst->dqb_curinodes = src->d_icount;
145         dst->dqb_btime = src->d_btimer;
146         dst->dqb_itime = src->d_itimer;
147         dst->dqb_valid = QIF_ALL;
148 }
149
150 static int quota_getquota(struct super_block *sb, int type, qid_t id,
151                           void __user *addr)
152 {
153         struct fs_disk_quota fdq;
154         struct if_dqblk idq;
155         int ret;
156
157         if (!sb->s_qcop->get_dqblk)
158                 return -ENOSYS;
159         ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq);
160         if (ret)
161                 return ret;
162         copy_to_if_dqblk(&idq, &fdq);
163         if (copy_to_user(addr, &idq, sizeof(idq)))
164                 return -EFAULT;
165         return 0;
166 }
167
168 static void copy_from_if_dqblk(struct fs_disk_quota *dst, struct if_dqblk *src)
169 {
170         dst->d_blk_hardlimit = src->dqb_bhardlimit;
171         dst->d_blk_softlimit  = src->dqb_bsoftlimit;
172         dst->d_bcount = src->dqb_curspace;
173         dst->d_ino_hardlimit = src->dqb_ihardlimit;
174         dst->d_ino_softlimit = src->dqb_isoftlimit;
175         dst->d_icount = src->dqb_curinodes;
176         dst->d_btimer = src->dqb_btime;
177         dst->d_itimer = src->dqb_itime;
178
179         dst->d_fieldmask = 0;
180         if (src->dqb_valid & QIF_BLIMITS)
181                 dst->d_fieldmask |= FS_DQ_BSOFT | FS_DQ_BHARD;
182         if (src->dqb_valid & QIF_SPACE)
183                 dst->d_fieldmask |= FS_DQ_BCOUNT;
184         if (src->dqb_valid & QIF_ILIMITS)
185                 dst->d_fieldmask |= FS_DQ_ISOFT | FS_DQ_IHARD;
186         if (src->dqb_valid & QIF_INODES)
187                 dst->d_fieldmask |= FS_DQ_ICOUNT;
188         if (src->dqb_valid & QIF_BTIME)
189                 dst->d_fieldmask |= FS_DQ_BTIMER;
190         if (src->dqb_valid & QIF_ITIME)
191                 dst->d_fieldmask |= FS_DQ_ITIMER;
192 }
193
194 static int quota_setquota(struct super_block *sb, int type, qid_t id,
195                           void __user *addr)
196 {
197         struct fs_disk_quota fdq;
198         struct if_dqblk idq;
199
200         if (copy_from_user(&idq, addr, sizeof(idq)))
201                 return -EFAULT;
202         if (!sb->s_qcop->set_dqblk)
203                 return -ENOSYS;
204         copy_from_if_dqblk(&fdq, &idq);
205         return sb->s_qcop->set_dqblk(sb, type, id, &fdq);
206 }
207
208 static int quota_setxstate(struct super_block *sb, int cmd, void __user *addr)
209 {
210         __u32 flags;
211
212         if (copy_from_user(&flags, addr, sizeof(flags)))
213                 return -EFAULT;
214         if (!sb->s_qcop->set_xstate)
215                 return -ENOSYS;
216         return sb->s_qcop->set_xstate(sb, flags, cmd);
217 }
218
219 static int quota_getxstate(struct super_block *sb, void __user *addr)
220 {
221         struct fs_quota_stat fqs;
222         int ret;
223
224         if (!sb->s_qcop->get_xstate)
225                 return -ENOSYS;
226         ret = sb->s_qcop->get_xstate(sb, &fqs);
227         if (!ret && copy_to_user(addr, &fqs, sizeof(fqs)))
228                 return -EFAULT;
229         return ret;
230 }
231
232 static int quota_setxquota(struct super_block *sb, int type, qid_t id,
233                            void __user *addr)
234 {
235         struct fs_disk_quota fdq;
236
237         if (copy_from_user(&fdq, addr, sizeof(fdq)))
238                 return -EFAULT;
239         if (!sb->s_qcop->set_dqblk)
240                 return -ENOSYS;
241         return sb->s_qcop->set_dqblk(sb, type, id, &fdq);
242 }
243
244 static int quota_getxquota(struct super_block *sb, int type, qid_t id,
245                            void __user *addr)
246 {
247         struct fs_disk_quota fdq;
248         int ret;
249
250         if (!sb->s_qcop->get_dqblk)
251                 return -ENOSYS;
252         ret = sb->s_qcop->get_dqblk(sb, type, id, &fdq);
253         if (!ret && copy_to_user(addr, &fdq, sizeof(fdq)))
254                 return -EFAULT;
255         return ret;
256 }
257
258 /* Copy parameters and call proper function */
259 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,
260                        void __user *addr)
261 {
262         int ret;
263
264         if (type >= (XQM_COMMAND(cmd) ? XQM_MAXQUOTAS : MAXQUOTAS))
265                 return -EINVAL;
266         if (!sb->s_qcop)
267                 return -ENOSYS;
268
269         ret = check_quotactl_permission(sb, type, cmd, id);
270         if (ret < 0)
271                 return ret;
272
273         switch (cmd) {
274         case Q_QUOTAON:
275                 return quota_quotaon(sb, type, cmd, id, addr);
276         case Q_QUOTAOFF:
277                 if (!sb->s_qcop->quota_off)
278                         return -ENOSYS;
279                 return sb->s_qcop->quota_off(sb, type, 0);
280         case Q_GETFMT:
281                 return quota_getfmt(sb, type, addr);
282         case Q_GETINFO:
283                 return quota_getinfo(sb, type, addr);
284         case Q_SETINFO:
285                 return quota_setinfo(sb, type, addr);
286         case Q_GETQUOTA:
287                 return quota_getquota(sb, type, id, addr);
288         case Q_SETQUOTA:
289                 return quota_setquota(sb, type, id, addr);
290         case Q_SYNC:
291                 if (!sb->s_qcop->quota_sync)
292                         return -ENOSYS;
293                 return sb->s_qcop->quota_sync(sb, type, 1);
294         case Q_XQUOTAON:
295         case Q_XQUOTAOFF:
296         case Q_XQUOTARM:
297                 return quota_setxstate(sb, cmd, addr);
298         case Q_XGETQSTAT:
299                 return quota_getxstate(sb, addr);
300         case Q_XSETQLIM:
301                 return quota_setxquota(sb, type, id, addr);
302         case Q_XGETQUOTA:
303                 return quota_getxquota(sb, type, id, addr);
304         case Q_XQUOTASYNC:
305                 /* caller already holds s_umount */
306                 if (sb->s_flags & MS_RDONLY)
307                         return -EROFS;
308                 writeback_inodes_sb(sb);
309                 return 0;
310         default:
311                 return -EINVAL;
312         }
313 }
314
315 /*
316  * look up a superblock on which quota ops will be performed
317  * - use the name of a block device to find the superblock thereon
318  */
319 static struct super_block *quotactl_block(const char __user *special)
320 {
321 #ifdef CONFIG_BLOCK
322         struct block_device *bdev;
323         struct super_block *sb;
324         char *tmp = getname(special);
325
326         if (IS_ERR(tmp))
327                 return ERR_CAST(tmp);
328         bdev = lookup_bdev(tmp);
329         putname(tmp);
330         if (IS_ERR(bdev))
331                 return ERR_CAST(bdev);
332         sb = get_super(bdev);
333         bdput(bdev);
334         if (!sb)
335                 return ERR_PTR(-ENODEV);
336
337         return sb;
338 #else
339         return ERR_PTR(-ENODEV);
340 #endif
341 }
342
343 /*
344  * This is the system call interface. This communicates with
345  * the user-level programs. Currently this only supports diskquota
346  * calls. Maybe we need to add the process quotas etc. in the future,
347  * but we probably should use rlimits for that.
348  */
349 SYSCALL_DEFINE4(quotactl, unsigned int, cmd, const char __user *, special,
350                 qid_t, id, void __user *, addr)
351 {
352         uint cmds, type;
353         struct super_block *sb = NULL;
354         int ret;
355
356         cmds = cmd >> SUBCMDSHIFT;
357         type = cmd & SUBCMDMASK;
358
359         /*
360          * As a special case Q_SYNC can be called without a specific device.
361          * It will iterate all superblocks that have quota enabled and call
362          * the sync action on each of them.
363          */
364         if (!special) {
365                 if (cmds == Q_SYNC)
366                         return quota_sync_all(type);
367                 return -ENODEV;
368         }
369
370         sb = quotactl_block(special);
371         if (IS_ERR(sb))
372                 return PTR_ERR(sb);
373
374         ret = do_quotactl(sb, type, cmds, id, addr);
375
376         drop_super(sb);
377         return ret;
378 }