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.
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
,
26 if (type
>= MAXQUOTAS
)
28 if (!sb
&& cmd
!= Q_SYNC
)
30 /* Is operation supported? */
31 if (sb
&& !sb
->s_qcop
)
38 if (!sb
->s_qcop
->quota_on
)
42 if (!sb
->s_qcop
->quota_off
)
46 if (!sb
->s_qcop
->set_info
)
50 if (!sb
->s_qcop
->get_info
)
54 if (!sb
->s_qcop
->set_dqblk
)
58 if (!sb
->s_qcop
->get_dqblk
)
62 if (sb
&& !sb
->s_qcop
->quota_sync
)
69 /* Is quota turned on for commands which need it? */
76 /* This is just an informative test so we are satisfied
78 if (!sb_has_quota_active(sb
, type
))
82 /* Check privileges */
83 if (cmd
== Q_GETQUOTA
) {
84 if (((type
== USRQUOTA
&& current_euid() != id
) ||
85 (type
== GRPQUOTA
&& !in_egroup_p(id
))) &&
86 !capable(CAP_SYS_ADMIN
))
89 else if (cmd
!= Q_GETFMT
&& cmd
!= Q_SYNC
&& cmd
!= Q_GETINFO
)
90 if (!capable(CAP_SYS_ADMIN
))
96 /* Check validity of XFS Quota Manager commands */
97 static int xqm_quotactl_valid(struct super_block
*sb
, int type
, int cmd
,
100 if (type
>= XQM_MAXQUOTAS
)
111 if (!sb
->s_qcop
->set_xstate
)
115 if (!sb
->s_qcop
->get_xstate
)
119 if (!sb
->s_qcop
->set_xquota
)
123 if (!sb
->s_qcop
->get_xquota
)
127 if (!sb
->s_qcop
->quota_sync
)
134 /* Check privileges */
135 if (cmd
== Q_XGETQUOTA
) {
136 if (((type
== XQM_USRQUOTA
&& current_euid() != id
) ||
137 (type
== XQM_GRPQUOTA
&& !in_egroup_p(id
))) &&
138 !capable(CAP_SYS_ADMIN
))
140 } else if (cmd
!= Q_XGETQSTAT
&& cmd
!= Q_XQUOTASYNC
) {
141 if (!capable(CAP_SYS_ADMIN
))
148 static int check_quotactl_valid(struct super_block
*sb
, int type
, int cmd
,
153 if (XQM_COMMAND(cmd
))
154 error
= xqm_quotactl_valid(sb
, type
, cmd
, id
);
156 error
= generic_quotactl_valid(sb
, type
, cmd
, id
);
158 error
= security_quotactl(cmd
, type
, id
, sb
);
162 static void quota_sync_sb(struct super_block
*sb
, int type
)
166 sb
->s_qcop
->quota_sync(sb
, type
);
168 if (sb_dqopt(sb
)->flags
& DQUOT_QUOTA_SYS_FILE
)
170 /* This is not very clever (and fast) but currently I don't know about
171 * any other simple way of getting quota data to disk and we must get
172 * them there for userspace to be visible... */
173 if (sb
->s_op
->sync_fs
)
174 sb
->s_op
->sync_fs(sb
, 1);
175 sync_blockdev(sb
->s_bdev
);
178 * Now when everything is written we can discard the pagecache so
179 * that userspace sees the changes.
181 mutex_lock(&sb_dqopt(sb
)->dqonoff_mutex
);
182 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
183 if (type
!= -1 && cnt
!= type
)
185 if (!sb_has_quota_active(sb
, cnt
))
187 mutex_lock_nested(&sb_dqopt(sb
)->files
[cnt
]->i_mutex
,
189 truncate_inode_pages(&sb_dqopt(sb
)->files
[cnt
]->i_data
, 0);
190 mutex_unlock(&sb_dqopt(sb
)->files
[cnt
]->i_mutex
);
192 mutex_unlock(&sb_dqopt(sb
)->dqonoff_mutex
);
195 void sync_dquots(struct super_block
*sb
, int type
)
200 if (sb
->s_qcop
->quota_sync
)
201 quota_sync_sb(sb
, type
);
207 list_for_each_entry(sb
, &super_blocks
, s_list
) {
208 /* This test just improves performance so it needn't be
210 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
211 if (type
!= -1 && type
!= cnt
)
213 if (!sb_has_quota_active(sb
, cnt
))
215 if (!info_dirty(&sb_dqopt(sb
)->info
[cnt
]) &&
216 list_empty(&sb_dqopt(sb
)->info
[cnt
].dqi_dirty_list
))
220 if (cnt
== MAXQUOTAS
)
223 spin_unlock(&sb_lock
);
224 down_read(&sb
->s_umount
);
225 if (sb
->s_root
&& sb
->s_qcop
->quota_sync
)
226 quota_sync_sb(sb
, type
);
227 up_read(&sb
->s_umount
);
229 if (__put_super_and_need_restart(sb
))
232 spin_unlock(&sb_lock
);
235 /* Copy parameters and call proper function */
236 static int do_quotactl(struct super_block
*sb
, int type
, int cmd
, qid_t id
,
245 pathname
= getname(addr
);
246 if (IS_ERR(pathname
))
247 return PTR_ERR(pathname
);
248 ret
= sb
->s_qcop
->quota_on(sb
, type
, id
, pathname
, 0);
253 return sb
->s_qcop
->quota_off(sb
, type
, 0);
258 down_read(&sb_dqopt(sb
)->dqptr_sem
);
259 if (!sb_has_quota_active(sb
, type
)) {
260 up_read(&sb_dqopt(sb
)->dqptr_sem
);
263 fmt
= sb_dqopt(sb
)->info
[type
].dqi_format
->qf_fmt_id
;
264 up_read(&sb_dqopt(sb
)->dqptr_sem
);
265 if (copy_to_user(addr
, &fmt
, sizeof(fmt
)))
270 struct if_dqinfo info
;
272 ret
= sb
->s_qcop
->get_info(sb
, type
, &info
);
275 if (copy_to_user(addr
, &info
, sizeof(info
)))
280 struct if_dqinfo info
;
282 if (copy_from_user(&info
, addr
, sizeof(info
)))
284 return sb
->s_qcop
->set_info(sb
, type
, &info
);
289 ret
= sb
->s_qcop
->get_dqblk(sb
, type
, id
, &idq
);
292 if (copy_to_user(addr
, &idq
, sizeof(idq
)))
299 if (copy_from_user(&idq
, addr
, sizeof(idq
)))
301 return sb
->s_qcop
->set_dqblk(sb
, type
, id
, &idq
);
304 sync_dquots(sb
, type
);
312 if (copy_from_user(&flags
, addr
, sizeof(flags
)))
314 return sb
->s_qcop
->set_xstate(sb
, flags
, cmd
);
317 struct fs_quota_stat fqs
;
319 if ((ret
= sb
->s_qcop
->get_xstate(sb
, &fqs
)))
321 if (copy_to_user(addr
, &fqs
, sizeof(fqs
)))
326 struct fs_disk_quota fdq
;
328 if (copy_from_user(&fdq
, addr
, sizeof(fdq
)))
330 return sb
->s_qcop
->set_xquota(sb
, type
, id
, &fdq
);
333 struct fs_disk_quota fdq
;
335 ret
= sb
->s_qcop
->get_xquota(sb
, type
, id
, &fdq
);
338 if (copy_to_user(addr
, &fdq
, sizeof(fdq
)))
343 return sb
->s_qcop
->quota_sync(sb
, type
);
344 /* We never reach here unless validity check is broken */
352 * look up a superblock on which quota ops will be performed
353 * - use the name of a block device to find the superblock thereon
355 static struct super_block
*quotactl_block(const char __user
*special
)
358 struct block_device
*bdev
;
359 struct super_block
*sb
;
360 char *tmp
= getname(special
);
363 return ERR_CAST(tmp
);
364 bdev
= lookup_bdev(tmp
);
367 return ERR_CAST(bdev
);
368 sb
= get_super(bdev
);
371 return ERR_PTR(-ENODEV
);
375 return ERR_PTR(-ENODEV
);
380 * This is the system call interface. This communicates with
381 * the user-level programs. Currently this only supports diskquota
382 * calls. Maybe we need to add the process quotas etc. in the future,
383 * but we probably should use rlimits for that.
385 SYSCALL_DEFINE4(quotactl
, unsigned int, cmd
, const char __user
*, special
,
386 qid_t
, id
, void __user
*, addr
)
389 struct super_block
*sb
= NULL
;
392 cmds
= cmd
>> SUBCMDSHIFT
;
393 type
= cmd
& SUBCMDMASK
;
395 if (cmds
!= Q_SYNC
|| special
) {
396 sb
= quotactl_block(special
);
401 ret
= check_quotactl_valid(sb
, type
, cmds
, id
);
403 ret
= do_quotactl(sb
, type
, cmds
, id
, addr
);
410 #if defined(CONFIG_COMPAT_FOR_U64_ALIGNMENT)
412 * This code works only for 32 bit quota tools over 64 bit OS (x86_64, ia64)
413 * and is necessary due to alignment problems.
415 struct compat_if_dqblk
{
416 compat_u64 dqb_bhardlimit
;
417 compat_u64 dqb_bsoftlimit
;
418 compat_u64 dqb_curspace
;
419 compat_u64 dqb_ihardlimit
;
420 compat_u64 dqb_isoftlimit
;
421 compat_u64 dqb_curinodes
;
422 compat_u64 dqb_btime
;
423 compat_u64 dqb_itime
;
424 compat_uint_t dqb_valid
;
428 struct compat_fs_qfilestat
{
429 compat_u64 dqb_bhardlimit
;
430 compat_u64 qfs_nblks
;
431 compat_uint_t qfs_nextents
;
434 struct compat_fs_quota_stat
{
438 struct compat_fs_qfilestat qs_uquota
;
439 struct compat_fs_qfilestat qs_gquota
;
440 compat_uint_t qs_incoredqs
;
441 compat_int_t qs_btimelimit
;
442 compat_int_t qs_itimelimit
;
443 compat_int_t qs_rtbtimelimit
;
448 asmlinkage
long sys32_quotactl(unsigned int cmd
, const char __user
*special
,
449 qid_t id
, void __user
*addr
)
452 struct if_dqblk __user
*dqblk
;
453 struct compat_if_dqblk __user
*compat_dqblk
;
454 struct fs_quota_stat __user
*fsqstat
;
455 struct compat_fs_quota_stat __user
*compat_fsqstat
;
460 cmds
= cmd
>> SUBCMDSHIFT
;
464 dqblk
= compat_alloc_user_space(sizeof(struct if_dqblk
));
466 ret
= sys_quotactl(cmd
, special
, id
, dqblk
);
469 if (copy_in_user(compat_dqblk
, dqblk
, sizeof(*compat_dqblk
)) ||
470 get_user(data
, &dqblk
->dqb_valid
) ||
471 put_user(data
, &compat_dqblk
->dqb_valid
))
475 dqblk
= compat_alloc_user_space(sizeof(struct if_dqblk
));
478 if (copy_in_user(dqblk
, compat_dqblk
, sizeof(*compat_dqblk
)) ||
479 get_user(data
, &compat_dqblk
->dqb_valid
) ||
480 put_user(data
, &dqblk
->dqb_valid
))
482 ret
= sys_quotactl(cmd
, special
, id
, dqblk
);
485 fsqstat
= compat_alloc_user_space(sizeof(struct fs_quota_stat
));
486 compat_fsqstat
= addr
;
487 ret
= sys_quotactl(cmd
, special
, id
, fsqstat
);
491 /* Copying qs_version, qs_flags, qs_pad */
492 if (copy_in_user(compat_fsqstat
, fsqstat
,
493 offsetof(struct compat_fs_quota_stat
, qs_uquota
)))
495 /* Copying qs_uquota */
496 if (copy_in_user(&compat_fsqstat
->qs_uquota
,
498 sizeof(compat_fsqstat
->qs_uquota
)) ||
499 get_user(data
, &fsqstat
->qs_uquota
.qfs_nextents
) ||
500 put_user(data
, &compat_fsqstat
->qs_uquota
.qfs_nextents
))
502 /* Copying qs_gquota */
503 if (copy_in_user(&compat_fsqstat
->qs_gquota
,
505 sizeof(compat_fsqstat
->qs_gquota
)) ||
506 get_user(data
, &fsqstat
->qs_gquota
.qfs_nextents
) ||
507 put_user(data
, &compat_fsqstat
->qs_gquota
.qfs_nextents
))
509 /* Copying the rest */
510 if (copy_in_user(&compat_fsqstat
->qs_incoredqs
,
511 &fsqstat
->qs_incoredqs
,
512 sizeof(struct compat_fs_quota_stat
) -
513 offsetof(struct compat_fs_quota_stat
, qs_incoredqs
)) ||
514 get_user(xdata
, &fsqstat
->qs_iwarnlimit
) ||
515 put_user(xdata
, &compat_fsqstat
->qs_iwarnlimit
))
520 ret
= sys_quotactl(cmd
, special
, id
, addr
);