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 struct super_block
*get_super_to_sync(int type
)
154 struct list_head
*head
;
159 list_for_each(head
, &super_blocks
) {
160 struct super_block
*sb
= list_entry(head
, struct super_block
, s_list
);
162 /* This test just improves performance so it needn't be reliable... */
163 for (cnt
= 0, dirty
= 0; cnt
< MAXQUOTAS
; cnt
++)
164 if ((type
== cnt
|| type
== -1) && sb_has_quota_enabled(sb
, cnt
)
165 && info_any_dirty(&sb_dqopt(sb
)->info
[cnt
]))
170 spin_unlock(&sb_lock
);
171 down_read(&sb
->s_umount
);
178 spin_unlock(&sb_lock
);
182 static void quota_sync_sb(struct super_block
*sb
, int type
)
185 struct inode
*discard
[MAXQUOTAS
];
187 sb
->s_qcop
->quota_sync(sb
, type
);
188 /* This is not very clever (and fast) but currently I don't know about
189 * any other simple way of getting quota data to disk and we must get
190 * them there for userspace to be visible... */
191 if (sb
->s_op
->sync_fs
)
192 sb
->s_op
->sync_fs(sb
, 1);
193 sync_blockdev(sb
->s_bdev
);
195 /* Now when everything is written we can discard the pagecache so
196 * that userspace sees the changes. We need i_sem and so we could
197 * not do it inside dqonoff_sem. Moreover we need to be carefull
198 * about races with quotaoff() (that is the reason why we have own
199 * reference to inode). */
200 down(&sb_dqopt(sb
)->dqonoff_sem
);
201 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
203 if (type
!= -1 && cnt
!= type
)
205 if (!sb_has_quota_enabled(sb
, cnt
))
207 discard
[cnt
] = igrab(sb_dqopt(sb
)->files
[cnt
]);
209 up(&sb_dqopt(sb
)->dqonoff_sem
);
210 for (cnt
= 0; cnt
< MAXQUOTAS
; cnt
++) {
212 down(&discard
[cnt
]->i_sem
);
213 truncate_inode_pages(&discard
[cnt
]->i_data
, 0);
214 up(&discard
[cnt
]->i_sem
);
220 void sync_dquots(struct super_block
*sb
, int type
)
223 if (sb
->s_qcop
->quota_sync
)
224 quota_sync_sb(sb
, type
);
227 while ((sb
= get_super_to_sync(type
)) != NULL
) {
228 if (sb
->s_qcop
->quota_sync
)
229 quota_sync_sb(sb
, type
);
235 /* Copy parameters and call proper function */
236 static int do_quotactl(struct super_block
*sb
, int type
, int cmd
, qid_t id
, void __user
*addr
)
244 if (IS_ERR(pathname
= getname(addr
)))
245 return PTR_ERR(pathname
);
246 ret
= sb
->s_qcop
->quota_on(sb
, type
, id
, pathname
);
251 return sb
->s_qcop
->quota_off(sb
, type
);
256 down_read(&sb_dqopt(sb
)->dqptr_sem
);
257 if (!sb_has_quota_enabled(sb
, type
)) {
258 up_read(&sb_dqopt(sb
)->dqptr_sem
);
261 fmt
= sb_dqopt(sb
)->info
[type
].dqi_format
->qf_fmt_id
;
262 up_read(&sb_dqopt(sb
)->dqptr_sem
);
263 if (copy_to_user(addr
, &fmt
, sizeof(fmt
)))
268 struct if_dqinfo info
;
270 if ((ret
= sb
->s_qcop
->get_info(sb
, type
, &info
)))
272 if (copy_to_user(addr
, &info
, sizeof(info
)))
277 struct if_dqinfo info
;
279 if (copy_from_user(&info
, addr
, sizeof(info
)))
281 return sb
->s_qcop
->set_info(sb
, type
, &info
);
286 if ((ret
= sb
->s_qcop
->get_dqblk(sb
, type
, id
, &idq
)))
288 if (copy_to_user(addr
, &idq
, sizeof(idq
)))
295 if (copy_from_user(&idq
, addr
, sizeof(idq
)))
297 return sb
->s_qcop
->set_dqblk(sb
, type
, id
, &idq
);
300 sync_dquots(sb
, type
);
308 if (copy_from_user(&flags
, addr
, sizeof(flags
)))
310 return sb
->s_qcop
->set_xstate(sb
, flags
, cmd
);
313 struct fs_quota_stat fqs
;
315 if ((ret
= sb
->s_qcop
->get_xstate(sb
, &fqs
)))
317 if (copy_to_user(addr
, &fqs
, sizeof(fqs
)))
322 struct fs_disk_quota fdq
;
324 if (copy_from_user(&fdq
, addr
, sizeof(fdq
)))
326 return sb
->s_qcop
->set_xquota(sb
, type
, id
, &fdq
);
329 struct fs_disk_quota fdq
;
331 if ((ret
= sb
->s_qcop
->get_xquota(sb
, type
, id
, &fdq
)))
333 if (copy_to_user(addr
, &fdq
, sizeof(fdq
)))
337 /* We never reach here unless validity check is broken */
345 * This is the system call interface. This communicates with
346 * the user-level programs. Currently this only supports diskquota
347 * calls. Maybe we need to add the process quotas etc. in the future,
348 * but we probably should use rlimits for that.
350 asmlinkage
long sys_quotactl(unsigned int cmd
, const char __user
*special
, qid_t id
, void __user
*addr
)
353 struct super_block
*sb
= NULL
;
354 struct block_device
*bdev
;
358 cmds
= cmd
>> SUBCMDSHIFT
;
359 type
= cmd
& SUBCMDMASK
;
361 if (cmds
!= Q_SYNC
|| special
) {
362 tmp
= getname(special
);
365 bdev
= lookup_bdev(tmp
);
368 return PTR_ERR(bdev
);
369 sb
= get_super(bdev
);
375 ret
= check_quotactl_valid(sb
, type
, cmds
, id
);
377 ret
= do_quotactl(sb
, type
, cmds
, id
, addr
);