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/kernel.h>
14 #include <linux/smp_lock.h>
15 #include <linux/security.h>
16 #include <linux/syscalls.h>
17 #include <linux/buffer_head.h>
19 /* Check validity of generic quotactl commands */
20 static int generic_quotactl_valid(struct super_block
*sb
, int type
, int cmd
, qid_t id
)
22 if (type
>= MAXQUOTAS
)
24 if (!sb
&& cmd
!= Q_SYNC
)
26 /* Is operation supported? */
27 if (sb
&& !sb
->s_qcop
)
34 if (!sb
->s_qcop
->quota_on
)
38 if (!sb
->s_qcop
->quota_off
)
42 if (!sb
->s_qcop
->set_info
)
46 if (!sb
->s_qcop
->get_info
)
50 if (!sb
->s_qcop
->set_dqblk
)
54 if (!sb
->s_qcop
->get_dqblk
)
58 if (sb
&& !sb
->s_qcop
->quota_sync
)
65 /* Is quota turned on for commands which need it? */
73 /* This is just informative test so we are satisfied without a lock */
74 if (!sb_has_quota_enabled(sb
, type
))
78 /* Check privileges */
79 if (cmd
== Q_GETQUOTA
) {
80 if (((type
== USRQUOTA
&& current
->euid
!= id
) ||
81 (type
== GRPQUOTA
&& !in_egroup_p(id
))) &&
82 !capable(CAP_SYS_ADMIN
))
85 else if (cmd
!= Q_GETFMT
&& cmd
!= Q_SYNC
&& cmd
!= Q_GETINFO
)
86 if (!capable(CAP_SYS_ADMIN
))
92 /* Check validity of XFS Quota Manager commands */
93 static int xqm_quotactl_valid(struct super_block
*sb
, int type
, int cmd
, qid_t id
)
95 if (type
>= XQM_MAXQUOTAS
)
106 if (!sb
->s_qcop
->set_xstate
)
110 if (!sb
->s_qcop
->get_xstate
)
114 if (!sb
->s_qcop
->set_xquota
)
118 if (!sb
->s_qcop
->get_xquota
)
125 /* Check privileges */
126 if (cmd
== Q_XGETQUOTA
) {
127 if (((type
== XQM_USRQUOTA
&& current
->euid
!= id
) ||
128 (type
== XQM_GRPQUOTA
&& !in_egroup_p(id
))) &&
129 !capable(CAP_SYS_ADMIN
))
131 } else if (cmd
!= Q_XGETQSTAT
) {
132 if (!capable(CAP_SYS_ADMIN
))
139 static int check_quotactl_valid(struct super_block
*sb
, int type
, int cmd
, qid_t id
)
143 if (XQM_COMMAND(cmd
))
144 error
= xqm_quotactl_valid(sb
, type
, cmd
, id
);
146 error
= generic_quotactl_valid(sb
, type
, cmd
, id
);
148 error
= security_quotactl(cmd
, type
, id
, sb
);
152 static void quota_sync_sb(struct super_block
*sb
, int type
)
155 struct inode
*discard
[MAXQUOTAS
];
157 sb
->s_qcop
->quota_sync(sb
, type
);
158 /* This is not very clever (and fast) but currently I don't know about
159 * any other simple way of getting quota data to disk and we must get
160 * them there for userspace to be visible... */
161 if (sb
->s_op
->sync_fs
)
162 sb
->s_op
->sync_fs(sb
, 1);
163 sync_blockdev(sb
->s_bdev
);
165 /* Now when everything is written we can discard the pagecache so
166 * that userspace sees the changes. We need i_sem and so we could
167 * not do it inside dqonoff_sem. Moreover we need to be carefull
168 * about races with quotaoff() (that is the reason why we have own
169 * reference to inode). */
170 down(&sb_dqopt(sb
)->dqonoff_sem
);
171 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
173 if (type
!= -1 && cnt
!= type
)
175 if (!sb_has_quota_enabled(sb
, cnt
))
177 discard
[cnt
] = igrab(sb_dqopt(sb
)->files
[cnt
]);
179 up(&sb_dqopt(sb
)->dqonoff_sem
);
180 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
182 down(&discard
[cnt
]->i_sem
);
183 truncate_inode_pages(&discard
[cnt
]->i_data
, 0);
184 up(&discard
[cnt
]->i_sem
);
190 void sync_dquots(struct super_block
*sb
, int type
)
195 if (sb
->s_qcop
->quota_sync
)
196 quota_sync_sb(sb
, type
);
202 list_for_each_entry(sb
, &super_blocks
, s_list
) {
203 /* This test just improves performance so it needn't be reliable... */
204 for (cnt
= 0, dirty
= 0; cnt
< MAXQUOTAS
; cnt
++)
205 if ((type
== cnt
|| type
== -1) && sb_has_quota_enabled(sb
, cnt
)
206 && info_any_dirty(&sb_dqopt(sb
)->info
[cnt
]))
211 spin_unlock(&sb_lock
);
212 down_read(&sb
->s_umount
);
213 if (sb
->s_root
&& sb
->s_qcop
->quota_sync
)
214 quota_sync_sb(sb
, type
);
215 up_read(&sb
->s_umount
);
217 if (__put_super_and_need_restart(sb
))
220 spin_unlock(&sb_lock
);
223 /* Copy parameters and call proper function */
224 static int do_quotactl(struct super_block
*sb
, int type
, int cmd
, qid_t id
, void __user
*addr
)
232 if (IS_ERR(pathname
= getname(addr
)))
233 return PTR_ERR(pathname
);
234 ret
= sb
->s_qcop
->quota_on(sb
, type
, id
, pathname
);
239 return sb
->s_qcop
->quota_off(sb
, type
);
244 down_read(&sb_dqopt(sb
)->dqptr_sem
);
245 if (!sb_has_quota_enabled(sb
, type
)) {
246 up_read(&sb_dqopt(sb
)->dqptr_sem
);
249 fmt
= sb_dqopt(sb
)->info
[type
].dqi_format
->qf_fmt_id
;
250 up_read(&sb_dqopt(sb
)->dqptr_sem
);
251 if (copy_to_user(addr
, &fmt
, sizeof(fmt
)))
256 struct if_dqinfo info
;
258 if ((ret
= sb
->s_qcop
->get_info(sb
, type
, &info
)))
260 if (copy_to_user(addr
, &info
, sizeof(info
)))
265 struct if_dqinfo info
;
267 if (copy_from_user(&info
, addr
, sizeof(info
)))
269 return sb
->s_qcop
->set_info(sb
, type
, &info
);
274 if ((ret
= sb
->s_qcop
->get_dqblk(sb
, type
, id
, &idq
)))
276 if (copy_to_user(addr
, &idq
, sizeof(idq
)))
283 if (copy_from_user(&idq
, addr
, sizeof(idq
)))
285 return sb
->s_qcop
->set_dqblk(sb
, type
, id
, &idq
);
288 sync_dquots(sb
, type
);
296 if (copy_from_user(&flags
, addr
, sizeof(flags
)))
298 return sb
->s_qcop
->set_xstate(sb
, flags
, cmd
);
301 struct fs_quota_stat fqs
;
303 if ((ret
= sb
->s_qcop
->get_xstate(sb
, &fqs
)))
305 if (copy_to_user(addr
, &fqs
, sizeof(fqs
)))
310 struct fs_disk_quota fdq
;
312 if (copy_from_user(&fdq
, addr
, sizeof(fdq
)))
314 return sb
->s_qcop
->set_xquota(sb
, type
, id
, &fdq
);
317 struct fs_disk_quota fdq
;
319 if ((ret
= sb
->s_qcop
->get_xquota(sb
, type
, id
, &fdq
)))
321 if (copy_to_user(addr
, &fdq
, sizeof(fdq
)))
325 /* We never reach here unless validity check is broken */
333 * This is the system call interface. This communicates with
334 * the user-level programs. Currently this only supports diskquota
335 * calls. Maybe we need to add the process quotas etc. in the future,
336 * but we probably should use rlimits for that.
338 asmlinkage
long sys_quotactl(unsigned int cmd
, const char __user
*special
, qid_t id
, void __user
*addr
)
341 struct super_block
*sb
= NULL
;
342 struct block_device
*bdev
;
346 cmds
= cmd
>> SUBCMDSHIFT
;
347 type
= cmd
& SUBCMDMASK
;
349 if (cmds
!= Q_SYNC
|| special
) {
350 tmp
= getname(special
);
353 bdev
= lookup_bdev(tmp
);
356 return PTR_ERR(bdev
);
357 sb
= get_super(bdev
);
363 ret
= check_quotactl_valid(sb
, type
, cmds
, id
);
365 ret
= do_quotactl(sb
, type
, cmds
, id
, addr
);