[CIFS] Fix warnings from checkpatch
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / quota.c
blobdb1cc9f3c7aa3a24be8439c632f77228e5e266ab
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 */
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/compat.h>
14 #include <linux/kernel.h>
15 #include <linux/security.h>
16 #include <linux/syscalls.h>
17 #include <linux/buffer_head.h>
18 #include <linux/capability.h>
19 #include <linux/quotaops.h>
20 #include <linux/types.h>
22 /* Check validity of generic quotactl commands */
23 static int generic_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
25 if (type >= MAXQUOTAS)
26 return -EINVAL;
27 if (!sb && cmd != Q_SYNC)
28 return -ENODEV;
29 /* Is operation supported? */
30 if (sb && !sb->s_qcop)
31 return -ENOSYS;
33 switch (cmd) {
34 case Q_GETFMT:
35 break;
36 case Q_QUOTAON:
37 if (!sb->s_qcop->quota_on)
38 return -ENOSYS;
39 break;
40 case Q_QUOTAOFF:
41 if (!sb->s_qcop->quota_off)
42 return -ENOSYS;
43 break;
44 case Q_SETINFO:
45 if (!sb->s_qcop->set_info)
46 return -ENOSYS;
47 break;
48 case Q_GETINFO:
49 if (!sb->s_qcop->get_info)
50 return -ENOSYS;
51 break;
52 case Q_SETQUOTA:
53 if (!sb->s_qcop->set_dqblk)
54 return -ENOSYS;
55 break;
56 case Q_GETQUOTA:
57 if (!sb->s_qcop->get_dqblk)
58 return -ENOSYS;
59 break;
60 case Q_SYNC:
61 if (sb && !sb->s_qcop->quota_sync)
62 return -ENOSYS;
63 break;
64 default:
65 return -EINVAL;
68 /* Is quota turned on for commands which need it? */
69 switch (cmd) {
70 case Q_GETFMT:
71 case Q_GETINFO:
72 case Q_SETINFO:
73 case Q_SETQUOTA:
74 case Q_GETQUOTA:
75 /* This is just informative test so we are satisfied without a lock */
76 if (!sb_has_quota_enabled(sb, type))
77 return -ESRCH;
80 /* Check privileges */
81 if (cmd == Q_GETQUOTA) {
82 if (((type == USRQUOTA && current->euid != id) ||
83 (type == GRPQUOTA && !in_egroup_p(id))) &&
84 !capable(CAP_SYS_ADMIN))
85 return -EPERM;
87 else if (cmd != Q_GETFMT && cmd != Q_SYNC && cmd != Q_GETINFO)
88 if (!capable(CAP_SYS_ADMIN))
89 return -EPERM;
91 return 0;
94 /* Check validity of XFS Quota Manager commands */
95 static int xqm_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
97 if (type >= XQM_MAXQUOTAS)
98 return -EINVAL;
99 if (!sb)
100 return -ENODEV;
101 if (!sb->s_qcop)
102 return -ENOSYS;
104 switch (cmd) {
105 case Q_XQUOTAON:
106 case Q_XQUOTAOFF:
107 case Q_XQUOTARM:
108 if (!sb->s_qcop->set_xstate)
109 return -ENOSYS;
110 break;
111 case Q_XGETQSTAT:
112 if (!sb->s_qcop->get_xstate)
113 return -ENOSYS;
114 break;
115 case Q_XSETQLIM:
116 if (!sb->s_qcop->set_xquota)
117 return -ENOSYS;
118 break;
119 case Q_XGETQUOTA:
120 if (!sb->s_qcop->get_xquota)
121 return -ENOSYS;
122 break;
123 case Q_XQUOTASYNC:
124 if (!sb->s_qcop->quota_sync)
125 return -ENOSYS;
126 break;
127 default:
128 return -EINVAL;
131 /* Check privileges */
132 if (cmd == Q_XGETQUOTA) {
133 if (((type == XQM_USRQUOTA && current->euid != id) ||
134 (type == XQM_GRPQUOTA && !in_egroup_p(id))) &&
135 !capable(CAP_SYS_ADMIN))
136 return -EPERM;
137 } else if (cmd != Q_XGETQSTAT && cmd != Q_XQUOTASYNC) {
138 if (!capable(CAP_SYS_ADMIN))
139 return -EPERM;
142 return 0;
145 static int check_quotactl_valid(struct super_block *sb, int type, int cmd, qid_t id)
147 int error;
149 if (XQM_COMMAND(cmd))
150 error = xqm_quotactl_valid(sb, type, cmd, id);
151 else
152 error = generic_quotactl_valid(sb, type, cmd, id);
153 if (!error)
154 error = security_quotactl(cmd, type, id, sb);
155 return error;
158 static void quota_sync_sb(struct super_block *sb, int type)
160 int cnt;
162 sb->s_qcop->quota_sync(sb, type);
163 /* This is not very clever (and fast) but currently I don't know about
164 * any other simple way of getting quota data to disk and we must get
165 * them there for userspace to be visible... */
166 if (sb->s_op->sync_fs)
167 sb->s_op->sync_fs(sb, 1);
168 sync_blockdev(sb->s_bdev);
171 * Now when everything is written we can discard the pagecache so
172 * that userspace sees the changes.
174 mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
175 for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
176 if (type != -1 && cnt != type)
177 continue;
178 if (!sb_has_quota_enabled(sb, cnt))
179 continue;
180 mutex_lock_nested(&sb_dqopt(sb)->files[cnt]->i_mutex, I_MUTEX_QUOTA);
181 truncate_inode_pages(&sb_dqopt(sb)->files[cnt]->i_data, 0);
182 mutex_unlock(&sb_dqopt(sb)->files[cnt]->i_mutex);
184 mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
187 void sync_dquots(struct super_block *sb, int type)
189 int cnt, dirty;
191 if (sb) {
192 if (sb->s_qcop->quota_sync)
193 quota_sync_sb(sb, type);
194 return;
197 spin_lock(&sb_lock);
198 restart:
199 list_for_each_entry(sb, &super_blocks, s_list) {
200 /* This test just improves performance so it needn't be reliable... */
201 for (cnt = 0, dirty = 0; cnt < MAXQUOTAS; cnt++)
202 if ((type == cnt || type == -1) && sb_has_quota_enabled(sb, cnt)
203 && info_any_dirty(&sb_dqopt(sb)->info[cnt]))
204 dirty = 1;
205 if (!dirty)
206 continue;
207 sb->s_count++;
208 spin_unlock(&sb_lock);
209 down_read(&sb->s_umount);
210 if (sb->s_root && sb->s_qcop->quota_sync)
211 quota_sync_sb(sb, type);
212 up_read(&sb->s_umount);
213 spin_lock(&sb_lock);
214 if (__put_super_and_need_restart(sb))
215 goto restart;
217 spin_unlock(&sb_lock);
220 /* Copy parameters and call proper function */
221 static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, void __user *addr)
223 int ret;
225 switch (cmd) {
226 case Q_QUOTAON: {
227 char *pathname;
229 if (IS_ERR(pathname = getname(addr)))
230 return PTR_ERR(pathname);
231 ret = sb->s_qcop->quota_on(sb, type, id, pathname, 0);
232 putname(pathname);
233 return ret;
235 case Q_QUOTAOFF:
236 return sb->s_qcop->quota_off(sb, type, 0);
238 case Q_GETFMT: {
239 __u32 fmt;
241 down_read(&sb_dqopt(sb)->dqptr_sem);
242 if (!sb_has_quota_enabled(sb, type)) {
243 up_read(&sb_dqopt(sb)->dqptr_sem);
244 return -ESRCH;
246 fmt = sb_dqopt(sb)->info[type].dqi_format->qf_fmt_id;
247 up_read(&sb_dqopt(sb)->dqptr_sem);
248 if (copy_to_user(addr, &fmt, sizeof(fmt)))
249 return -EFAULT;
250 return 0;
252 case Q_GETINFO: {
253 struct if_dqinfo info;
255 if ((ret = sb->s_qcop->get_info(sb, type, &info)))
256 return ret;
257 if (copy_to_user(addr, &info, sizeof(info)))
258 return -EFAULT;
259 return 0;
261 case Q_SETINFO: {
262 struct if_dqinfo info;
264 if (copy_from_user(&info, addr, sizeof(info)))
265 return -EFAULT;
266 return sb->s_qcop->set_info(sb, type, &info);
268 case Q_GETQUOTA: {
269 struct if_dqblk idq;
271 if ((ret = sb->s_qcop->get_dqblk(sb, type, id, &idq)))
272 return ret;
273 if (copy_to_user(addr, &idq, sizeof(idq)))
274 return -EFAULT;
275 return 0;
277 case Q_SETQUOTA: {
278 struct if_dqblk idq;
280 if (copy_from_user(&idq, addr, sizeof(idq)))
281 return -EFAULT;
282 return sb->s_qcop->set_dqblk(sb, type, id, &idq);
284 case Q_SYNC:
285 sync_dquots(sb, type);
286 return 0;
288 case Q_XQUOTAON:
289 case Q_XQUOTAOFF:
290 case Q_XQUOTARM: {
291 __u32 flags;
293 if (copy_from_user(&flags, addr, sizeof(flags)))
294 return -EFAULT;
295 return sb->s_qcop->set_xstate(sb, flags, cmd);
297 case Q_XGETQSTAT: {
298 struct fs_quota_stat fqs;
300 if ((ret = sb->s_qcop->get_xstate(sb, &fqs)))
301 return ret;
302 if (copy_to_user(addr, &fqs, sizeof(fqs)))
303 return -EFAULT;
304 return 0;
306 case Q_XSETQLIM: {
307 struct fs_disk_quota fdq;
309 if (copy_from_user(&fdq, addr, sizeof(fdq)))
310 return -EFAULT;
311 return sb->s_qcop->set_xquota(sb, type, id, &fdq);
313 case Q_XGETQUOTA: {
314 struct fs_disk_quota fdq;
316 if ((ret = sb->s_qcop->get_xquota(sb, type, id, &fdq)))
317 return ret;
318 if (copy_to_user(addr, &fdq, sizeof(fdq)))
319 return -EFAULT;
320 return 0;
322 case Q_XQUOTASYNC:
323 return sb->s_qcop->quota_sync(sb, type);
324 /* We never reach here unless validity check is broken */
325 default:
326 BUG();
328 return 0;
332 * look up a superblock on which quota ops will be performed
333 * - use the name of a block device to find the superblock thereon
335 static inline struct super_block *quotactl_block(const char __user *special)
337 #ifdef CONFIG_BLOCK
338 struct block_device *bdev;
339 struct super_block *sb;
340 char *tmp = getname(special);
342 if (IS_ERR(tmp))
343 return ERR_CAST(tmp);
344 bdev = lookup_bdev(tmp);
345 putname(tmp);
346 if (IS_ERR(bdev))
347 return ERR_CAST(bdev);
348 sb = get_super(bdev);
349 bdput(bdev);
350 if (!sb)
351 return ERR_PTR(-ENODEV);
353 return sb;
354 #else
355 return ERR_PTR(-ENODEV);
356 #endif
360 * This is the system call interface. This communicates with
361 * the user-level programs. Currently this only supports diskquota
362 * calls. Maybe we need to add the process quotas etc. in the future,
363 * but we probably should use rlimits for that.
365 asmlinkage long sys_quotactl(unsigned int cmd, const char __user *special, qid_t id, void __user *addr)
367 uint cmds, type;
368 struct super_block *sb = NULL;
369 int ret;
371 cmds = cmd >> SUBCMDSHIFT;
372 type = cmd & SUBCMDMASK;
374 if (cmds != Q_SYNC || special) {
375 sb = quotactl_block(special);
376 if (IS_ERR(sb))
377 return PTR_ERR(sb);
380 ret = check_quotactl_valid(sb, type, cmds, id);
381 if (ret >= 0)
382 ret = do_quotactl(sb, type, cmds, id, addr);
383 if (sb)
384 drop_super(sb);
386 return ret;
389 #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
391 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
392 * and is necessary due to alignment problems.
394 struct compat_if_dqblk {
395 compat_u64 dqb_bhardlimit;
396 compat_u64 dqb_bsoftlimit;
397 compat_u64 dqb_curspace;
398 compat_u64 dqb_ihardlimit;
399 compat_u64 dqb_isoftlimit;
400 compat_u64 dqb_curinodes;
401 compat_u64 dqb_btime;
402 compat_u64 dqb_itime;
403 compat_uint_t dqb_valid;
406 /* XFS structures */
407 struct compat_fs_qfilestat {
408 compat_u64 dqb_bhardlimit;
409 compat_u64 qfs_nblks;
410 compat_uint_t qfs_nextents;
413 struct compat_fs_quota_stat {
414 __s8 qs_version;
415 __u16 qs_flags;
416 __s8 qs_pad;
417 struct compat_fs_qfilestat qs_uquota;
418 struct compat_fs_qfilestat qs_gquota;
419 compat_uint_t qs_incoredqs;
420 compat_int_t qs_btimelimit;
421 compat_int_t qs_itimelimit;
422 compat_int_t qs_rtbtimelimit;
423 __u16 qs_bwarnlimit;
424 __u16 qs_iwarnlimit;
427 asmlinkage long sys32_quotactl(unsigned int cmd, const char __user *special,
428 qid_t id, void __user *addr)
430 unsigned int cmds;
431 struct if_dqblk __user *dqblk;
432 struct compat_if_dqblk __user *compat_dqblk;
433 struct fs_quota_stat __user *fsqstat;
434 struct compat_fs_quota_stat __user *compat_fsqstat;
435 compat_uint_t data;
436 u16 xdata;
437 long ret;
439 cmds = cmd >> SUBCMDSHIFT;
441 switch (cmds) {
442 case Q_GETQUOTA:
443 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
444 compat_dqblk = addr;
445 ret = sys_quotactl(cmd, special, id, dqblk);
446 if (ret)
447 break;
448 if (copy_in_user(compat_dqblk, dqblk, sizeof(*compat_dqblk)) ||
449 get_user(data, &dqblk->dqb_valid) ||
450 put_user(data, &compat_dqblk->dqb_valid))
451 ret = -EFAULT;
452 break;
453 case Q_SETQUOTA:
454 dqblk = compat_alloc_user_space(sizeof(struct if_dqblk));
455 compat_dqblk = addr;
456 ret = -EFAULT;
457 if (copy_in_user(dqblk, compat_dqblk, sizeof(*compat_dqblk)) ||
458 get_user(data, &compat_dqblk->dqb_valid) ||
459 put_user(data, &dqblk->dqb_valid))
460 break;
461 ret = sys_quotactl(cmd, special, id, dqblk);
462 break;
463 case Q_XGETQSTAT:
464 fsqstat = compat_alloc_user_space(sizeof(struct fs_quota_stat));
465 compat_fsqstat = addr;
466 ret = sys_quotactl(cmd, special, id, fsqstat);
467 if (ret)
468 break;
469 ret = -EFAULT;
470 /* Copying qs_version, qs_flags, qs_pad */
471 if (copy_in_user(compat_fsqstat, fsqstat,
472 offsetof(struct compat_fs_quota_stat, qs_uquota)))
473 break;
474 /* Copying qs_uquota */
475 if (copy_in_user(&compat_fsqstat->qs_uquota,
476 &fsqstat->qs_uquota,
477 sizeof(compat_fsqstat->qs_uquota)) ||
478 get_user(data, &fsqstat->qs_uquota.qfs_nextents) ||
479 put_user(data, &compat_fsqstat->qs_uquota.qfs_nextents))
480 break;
481 /* Copying qs_gquota */
482 if (copy_in_user(&compat_fsqstat->qs_gquota,
483 &fsqstat->qs_gquota,
484 sizeof(compat_fsqstat->qs_gquota)) ||
485 get_user(data, &fsqstat->qs_gquota.qfs_nextents) ||
486 put_user(data, &compat_fsqstat->qs_gquota.qfs_nextents))
487 break;
488 /* Copying the rest */
489 if (copy_in_user(&compat_fsqstat->qs_incoredqs,
490 &fsqstat->qs_incoredqs,
491 sizeof(struct compat_fs_quota_stat) -
492 offsetof(struct compat_fs_quota_stat, qs_incoredqs)) ||
493 get_user(xdata, &fsqstat->qs_iwarnlimit) ||
494 put_user(xdata, &compat_fsqstat->qs_iwarnlimit))
495 break;
496 ret = 0;
497 break;
498 default:
499 ret = sys_quotactl(cmd, special, id, addr);
501 return ret;
503 #endif