ufs_quota.c revision 1.26 1 1.26 thorpej /* $NetBSD: ufs_quota.c,v 1.26 2003/06/29 18:43:48 thorpej Exp $ */
2 1.2 cgd
3 1.1 mycroft /*
4 1.11 fvdl * Copyright (c) 1982, 1986, 1990, 1993, 1995
5 1.1 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * This code is derived from software contributed to Berkeley by
8 1.1 mycroft * Robert Elz at The University of Melbourne.
9 1.1 mycroft *
10 1.1 mycroft * Redistribution and use in source and binary forms, with or without
11 1.1 mycroft * modification, are permitted provided that the following conditions
12 1.1 mycroft * are met:
13 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.1 mycroft * notice, this list of conditions and the following disclaimer.
15 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.1 mycroft * documentation and/or other materials provided with the distribution.
18 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
19 1.1 mycroft * must display the following acknowledgement:
20 1.1 mycroft * This product includes software developed by the University of
21 1.1 mycroft * California, Berkeley and its contributors.
22 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
23 1.1 mycroft * may be used to endorse or promote products derived from this software
24 1.1 mycroft * without specific prior written permission.
25 1.1 mycroft *
26 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 mycroft * SUCH DAMAGE.
37 1.1 mycroft *
38 1.11 fvdl * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
39 1.1 mycroft */
40 1.22 lukem
41 1.22 lukem #include <sys/cdefs.h>
42 1.26 thorpej __KERNEL_RCSID(0, "$NetBSD: ufs_quota.c,v 1.26 2003/06/29 18:43:48 thorpej Exp $");
43 1.22 lukem
44 1.1 mycroft #include <sys/param.h>
45 1.1 mycroft #include <sys/kernel.h>
46 1.1 mycroft #include <sys/systm.h>
47 1.1 mycroft #include <sys/namei.h>
48 1.1 mycroft #include <sys/malloc.h>
49 1.1 mycroft #include <sys/file.h>
50 1.1 mycroft #include <sys/proc.h>
51 1.1 mycroft #include <sys/vnode.h>
52 1.1 mycroft #include <sys/mount.h>
53 1.1 mycroft
54 1.1 mycroft #include <ufs/ufs/quota.h>
55 1.1 mycroft #include <ufs/ufs/inode.h>
56 1.1 mycroft #include <ufs/ufs/ufsmount.h>
57 1.1 mycroft #include <ufs/ufs/ufs_extern.h>
58 1.1 mycroft
59 1.1 mycroft /*
60 1.1 mycroft * Quota name to error message mapping.
61 1.1 mycroft */
62 1.1 mycroft static char *quotatypes[] = INITQFNAMES;
63 1.1 mycroft
64 1.1 mycroft /*
65 1.1 mycroft * Set up the quotas for an inode.
66 1.1 mycroft *
67 1.1 mycroft * This routine completely defines the semantics of quotas.
68 1.1 mycroft * If other criterion want to be used to establish quotas, the
69 1.1 mycroft * MAXQUOTAS value in quotas.h should be increased, and the
70 1.1 mycroft * additional dquots set up here.
71 1.1 mycroft */
72 1.1 mycroft int
73 1.1 mycroft getinoquota(ip)
74 1.15 augustss struct inode *ip;
75 1.1 mycroft {
76 1.1 mycroft struct ufsmount *ump;
77 1.1 mycroft struct vnode *vp = ITOV(ip);
78 1.1 mycroft int error;
79 1.1 mycroft
80 1.1 mycroft ump = VFSTOUFS(vp->v_mount);
81 1.1 mycroft /*
82 1.1 mycroft * Set up the user quota based on file uid.
83 1.1 mycroft * EINVAL means that quotas are not enabled.
84 1.1 mycroft */
85 1.1 mycroft if (ip->i_dquot[USRQUOTA] == NODQUOT &&
86 1.1 mycroft (error =
87 1.24 fvdl dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
88 1.1 mycroft error != EINVAL)
89 1.1 mycroft return (error);
90 1.1 mycroft /*
91 1.1 mycroft * Set up the group quota based on file gid.
92 1.1 mycroft * EINVAL means that quotas are not enabled.
93 1.1 mycroft */
94 1.1 mycroft if (ip->i_dquot[GRPQUOTA] == NODQUOT &&
95 1.1 mycroft (error =
96 1.24 fvdl dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
97 1.1 mycroft error != EINVAL)
98 1.1 mycroft return (error);
99 1.1 mycroft return (0);
100 1.1 mycroft }
101 1.1 mycroft
102 1.1 mycroft /*
103 1.1 mycroft * Update disk usage, and take corrective action.
104 1.1 mycroft */
105 1.1 mycroft int
106 1.1 mycroft chkdq(ip, change, cred, flags)
107 1.15 augustss struct inode *ip;
108 1.24 fvdl int64_t change;
109 1.1 mycroft struct ucred *cred;
110 1.1 mycroft int flags;
111 1.1 mycroft {
112 1.15 augustss struct dquot *dq;
113 1.15 augustss int i;
114 1.1 mycroft int ncurblocks, error;
115 1.1 mycroft
116 1.1 mycroft #ifdef DIAGNOSTIC
117 1.1 mycroft if ((flags & CHOWN) == 0)
118 1.1 mycroft chkdquot(ip);
119 1.1 mycroft #endif
120 1.1 mycroft if (change == 0)
121 1.1 mycroft return (0);
122 1.1 mycroft if (change < 0) {
123 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
124 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
125 1.1 mycroft continue;
126 1.1 mycroft while (dq->dq_flags & DQ_LOCK) {
127 1.1 mycroft dq->dq_flags |= DQ_WANT;
128 1.17 thorpej (void) tsleep(dq, PINOD+1, "chkdq", 0);
129 1.1 mycroft }
130 1.1 mycroft ncurblocks = dq->dq_curblocks + change;
131 1.1 mycroft if (ncurblocks >= 0)
132 1.1 mycroft dq->dq_curblocks = ncurblocks;
133 1.1 mycroft else
134 1.1 mycroft dq->dq_curblocks = 0;
135 1.1 mycroft dq->dq_flags &= ~DQ_BLKS;
136 1.1 mycroft dq->dq_flags |= DQ_MOD;
137 1.1 mycroft }
138 1.1 mycroft return (0);
139 1.1 mycroft }
140 1.16 thorpej if ((flags & FORCE) == 0 &&
141 1.16 thorpej (cred != NOCRED && cred->cr_uid != 0)) {
142 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
143 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
144 1.1 mycroft continue;
145 1.8 christos if ((error = chkdqchg(ip, change, cred, i)) != 0)
146 1.1 mycroft return (error);
147 1.1 mycroft }
148 1.1 mycroft }
149 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
150 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
151 1.1 mycroft continue;
152 1.1 mycroft while (dq->dq_flags & DQ_LOCK) {
153 1.1 mycroft dq->dq_flags |= DQ_WANT;
154 1.17 thorpej (void) tsleep(dq, PINOD+1, "chkdq", 0);
155 1.1 mycroft }
156 1.1 mycroft dq->dq_curblocks += change;
157 1.1 mycroft dq->dq_flags |= DQ_MOD;
158 1.1 mycroft }
159 1.1 mycroft return (0);
160 1.1 mycroft }
161 1.1 mycroft
162 1.1 mycroft /*
163 1.1 mycroft * Check for a valid change to a users allocation.
164 1.1 mycroft * Issue an error message if appropriate.
165 1.1 mycroft */
166 1.1 mycroft int
167 1.1 mycroft chkdqchg(ip, change, cred, type)
168 1.1 mycroft struct inode *ip;
169 1.24 fvdl int64_t change;
170 1.1 mycroft struct ucred *cred;
171 1.1 mycroft int type;
172 1.1 mycroft {
173 1.15 augustss struct dquot *dq = ip->i_dquot[type];
174 1.1 mycroft long ncurblocks = dq->dq_curblocks + change;
175 1.1 mycroft
176 1.1 mycroft /*
177 1.1 mycroft * If user would exceed their hard limit, disallow space allocation.
178 1.1 mycroft */
179 1.1 mycroft if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
180 1.1 mycroft if ((dq->dq_flags & DQ_BLKS) == 0 &&
181 1.24 fvdl ip->i_uid == cred->cr_uid) {
182 1.1 mycroft uprintf("\n%s: write failed, %s disk limit reached\n",
183 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
184 1.1 mycroft quotatypes[type]);
185 1.1 mycroft dq->dq_flags |= DQ_BLKS;
186 1.1 mycroft }
187 1.1 mycroft return (EDQUOT);
188 1.1 mycroft }
189 1.1 mycroft /*
190 1.1 mycroft * If user is over their soft limit for too long, disallow space
191 1.1 mycroft * allocation. Reset time limit as they cross their soft limit.
192 1.1 mycroft */
193 1.1 mycroft if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
194 1.1 mycroft if (dq->dq_curblocks < dq->dq_bsoftlimit) {
195 1.1 mycroft dq->dq_btime = time.tv_sec +
196 1.1 mycroft VFSTOUFS(ITOV(ip)->v_mount)->um_btime[type];
197 1.24 fvdl if (ip->i_uid == cred->cr_uid)
198 1.1 mycroft uprintf("\n%s: warning, %s %s\n",
199 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
200 1.1 mycroft quotatypes[type], "disk quota exceeded");
201 1.1 mycroft return (0);
202 1.1 mycroft }
203 1.1 mycroft if (time.tv_sec > dq->dq_btime) {
204 1.1 mycroft if ((dq->dq_flags & DQ_BLKS) == 0 &&
205 1.24 fvdl ip->i_uid == cred->cr_uid) {
206 1.1 mycroft uprintf("\n%s: write failed, %s %s\n",
207 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
208 1.1 mycroft quotatypes[type],
209 1.1 mycroft "disk quota exceeded for too long");
210 1.1 mycroft dq->dq_flags |= DQ_BLKS;
211 1.1 mycroft }
212 1.1 mycroft return (EDQUOT);
213 1.1 mycroft }
214 1.1 mycroft }
215 1.1 mycroft return (0);
216 1.1 mycroft }
217 1.1 mycroft
218 1.1 mycroft /*
219 1.1 mycroft * Check the inode limit, applying corrective action.
220 1.1 mycroft */
221 1.1 mycroft int
222 1.1 mycroft chkiq(ip, change, cred, flags)
223 1.15 augustss struct inode *ip;
224 1.24 fvdl int32_t change;
225 1.1 mycroft struct ucred *cred;
226 1.1 mycroft int flags;
227 1.1 mycroft {
228 1.15 augustss struct dquot *dq;
229 1.15 augustss int i;
230 1.1 mycroft int ncurinodes, error;
231 1.1 mycroft
232 1.1 mycroft #ifdef DIAGNOSTIC
233 1.1 mycroft if ((flags & CHOWN) == 0)
234 1.1 mycroft chkdquot(ip);
235 1.1 mycroft #endif
236 1.1 mycroft if (change == 0)
237 1.1 mycroft return (0);
238 1.1 mycroft if (change < 0) {
239 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
240 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
241 1.1 mycroft continue;
242 1.1 mycroft while (dq->dq_flags & DQ_LOCK) {
243 1.1 mycroft dq->dq_flags |= DQ_WANT;
244 1.17 thorpej (void) tsleep(dq, PINOD+1, "chkiq", 0);
245 1.1 mycroft }
246 1.1 mycroft ncurinodes = dq->dq_curinodes + change;
247 1.1 mycroft if (ncurinodes >= 0)
248 1.1 mycroft dq->dq_curinodes = ncurinodes;
249 1.1 mycroft else
250 1.1 mycroft dq->dq_curinodes = 0;
251 1.1 mycroft dq->dq_flags &= ~DQ_INODS;
252 1.1 mycroft dq->dq_flags |= DQ_MOD;
253 1.1 mycroft }
254 1.1 mycroft return (0);
255 1.1 mycroft }
256 1.1 mycroft if ((flags & FORCE) == 0 && cred->cr_uid != 0) {
257 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
258 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
259 1.1 mycroft continue;
260 1.8 christos if ((error = chkiqchg(ip, change, cred, i)) != 0)
261 1.1 mycroft return (error);
262 1.1 mycroft }
263 1.1 mycroft }
264 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
265 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
266 1.1 mycroft continue;
267 1.1 mycroft while (dq->dq_flags & DQ_LOCK) {
268 1.1 mycroft dq->dq_flags |= DQ_WANT;
269 1.17 thorpej (void) tsleep(dq, PINOD+1, "chkiq", 0);
270 1.1 mycroft }
271 1.1 mycroft dq->dq_curinodes += change;
272 1.1 mycroft dq->dq_flags |= DQ_MOD;
273 1.1 mycroft }
274 1.1 mycroft return (0);
275 1.1 mycroft }
276 1.1 mycroft
277 1.1 mycroft /*
278 1.1 mycroft * Check for a valid change to a users allocation.
279 1.1 mycroft * Issue an error message if appropriate.
280 1.1 mycroft */
281 1.1 mycroft int
282 1.1 mycroft chkiqchg(ip, change, cred, type)
283 1.1 mycroft struct inode *ip;
284 1.24 fvdl int32_t change;
285 1.1 mycroft struct ucred *cred;
286 1.1 mycroft int type;
287 1.1 mycroft {
288 1.15 augustss struct dquot *dq = ip->i_dquot[type];
289 1.1 mycroft long ncurinodes = dq->dq_curinodes + change;
290 1.1 mycroft
291 1.1 mycroft /*
292 1.1 mycroft * If user would exceed their hard limit, disallow inode allocation.
293 1.1 mycroft */
294 1.1 mycroft if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
295 1.1 mycroft if ((dq->dq_flags & DQ_INODS) == 0 &&
296 1.24 fvdl ip->i_uid == cred->cr_uid) {
297 1.1 mycroft uprintf("\n%s: write failed, %s inode limit reached\n",
298 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
299 1.1 mycroft quotatypes[type]);
300 1.1 mycroft dq->dq_flags |= DQ_INODS;
301 1.1 mycroft }
302 1.1 mycroft return (EDQUOT);
303 1.1 mycroft }
304 1.1 mycroft /*
305 1.1 mycroft * If user is over their soft limit for too long, disallow inode
306 1.1 mycroft * allocation. Reset time limit as they cross their soft limit.
307 1.1 mycroft */
308 1.1 mycroft if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
309 1.1 mycroft if (dq->dq_curinodes < dq->dq_isoftlimit) {
310 1.1 mycroft dq->dq_itime = time.tv_sec +
311 1.1 mycroft VFSTOUFS(ITOV(ip)->v_mount)->um_itime[type];
312 1.24 fvdl if (ip->i_uid == cred->cr_uid)
313 1.1 mycroft uprintf("\n%s: warning, %s %s\n",
314 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
315 1.1 mycroft quotatypes[type], "inode quota exceeded");
316 1.1 mycroft return (0);
317 1.1 mycroft }
318 1.1 mycroft if (time.tv_sec > dq->dq_itime) {
319 1.1 mycroft if ((dq->dq_flags & DQ_INODS) == 0 &&
320 1.24 fvdl ip->i_uid == cred->cr_uid) {
321 1.1 mycroft uprintf("\n%s: write failed, %s %s\n",
322 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
323 1.1 mycroft quotatypes[type],
324 1.1 mycroft "inode quota exceeded for too long");
325 1.1 mycroft dq->dq_flags |= DQ_INODS;
326 1.1 mycroft }
327 1.1 mycroft return (EDQUOT);
328 1.1 mycroft }
329 1.1 mycroft }
330 1.1 mycroft return (0);
331 1.1 mycroft }
332 1.1 mycroft
333 1.1 mycroft #ifdef DIAGNOSTIC
334 1.1 mycroft /*
335 1.1 mycroft * On filesystems with quotas enabled, it is an error for a file to change
336 1.1 mycroft * size and not to have a dquot structure associated with it.
337 1.1 mycroft */
338 1.1 mycroft void
339 1.1 mycroft chkdquot(ip)
340 1.15 augustss struct inode *ip;
341 1.1 mycroft {
342 1.1 mycroft struct ufsmount *ump = VFSTOUFS(ITOV(ip)->v_mount);
343 1.15 augustss int i;
344 1.1 mycroft
345 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
346 1.1 mycroft if (ump->um_quotas[i] == NULLVP ||
347 1.1 mycroft (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
348 1.1 mycroft continue;
349 1.1 mycroft if (ip->i_dquot[i] == NODQUOT) {
350 1.1 mycroft vprint("chkdquot: missing dquot", ITOV(ip));
351 1.1 mycroft panic("missing dquot");
352 1.1 mycroft }
353 1.1 mycroft }
354 1.1 mycroft }
355 1.1 mycroft #endif
356 1.1 mycroft
357 1.1 mycroft /*
358 1.1 mycroft * Code to process quotactl commands.
359 1.1 mycroft */
360 1.1 mycroft
361 1.1 mycroft /*
362 1.1 mycroft * Q_QUOTAON - set up a quota file for a particular file system.
363 1.1 mycroft */
364 1.1 mycroft int
365 1.25 darrenr quotaon(l, mp, type, fname)
366 1.25 darrenr struct lwp *l;
367 1.1 mycroft struct mount *mp;
368 1.15 augustss int type;
369 1.1 mycroft caddr_t fname;
370 1.1 mycroft {
371 1.11 fvdl struct ufsmount *ump = VFSTOUFS(mp);
372 1.11 fvdl struct vnode *vp, **vpp;
373 1.1 mycroft struct vnode *nextvp;
374 1.1 mycroft struct dquot *dq;
375 1.25 darrenr struct proc *p;
376 1.1 mycroft int error;
377 1.1 mycroft struct nameidata nd;
378 1.1 mycroft
379 1.25 darrenr p = l->l_proc;
380 1.1 mycroft vpp = &ump->um_quotas[type];
381 1.25 darrenr NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, l);
382 1.8 christos if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0)
383 1.1 mycroft return (error);
384 1.1 mycroft vp = nd.ni_vp;
385 1.11 fvdl VOP_UNLOCK(vp, 0);
386 1.1 mycroft if (vp->v_type != VREG) {
387 1.25 darrenr (void) vn_close(vp, FREAD|FWRITE, p->p_ucred, l);
388 1.1 mycroft return (EACCES);
389 1.1 mycroft }
390 1.1 mycroft if (*vpp != vp)
391 1.25 darrenr quotaoff(l, mp, type);
392 1.1 mycroft ump->um_qflags[type] |= QTF_OPENING;
393 1.1 mycroft mp->mnt_flag |= MNT_QUOTA;
394 1.1 mycroft vp->v_flag |= VSYSTEM;
395 1.1 mycroft *vpp = vp;
396 1.1 mycroft /*
397 1.1 mycroft * Save the credential of the process that turned on quotas.
398 1.1 mycroft * Set up the time limits for this quota.
399 1.1 mycroft */
400 1.1 mycroft crhold(p->p_ucred);
401 1.1 mycroft ump->um_cred[type] = p->p_ucred;
402 1.1 mycroft ump->um_btime[type] = MAX_DQ_TIME;
403 1.1 mycroft ump->um_itime[type] = MAX_IQ_TIME;
404 1.1 mycroft if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
405 1.1 mycroft if (dq->dq_btime > 0)
406 1.1 mycroft ump->um_btime[type] = dq->dq_btime;
407 1.1 mycroft if (dq->dq_itime > 0)
408 1.1 mycroft ump->um_itime[type] = dq->dq_itime;
409 1.1 mycroft dqrele(NULLVP, dq);
410 1.1 mycroft }
411 1.1 mycroft /*
412 1.1 mycroft * Search vnodes associated with this mount point,
413 1.1 mycroft * adding references to quota file being opened.
414 1.1 mycroft * NB: only need to add dquot's for inodes being modified.
415 1.1 mycroft */
416 1.1 mycroft again:
417 1.21 chs for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nextvp) {
418 1.21 chs nextvp = LIST_NEXT(vp, v_mntvnodes);
419 1.13 fvdl if (vp->v_type == VNON ||vp->v_writecount == 0)
420 1.1 mycroft continue;
421 1.26 thorpej if (vget(vp, LK_EXCLUSIVE))
422 1.1 mycroft goto again;
423 1.8 christos if ((error = getinoquota(VTOI(vp))) != 0) {
424 1.1 mycroft vput(vp);
425 1.1 mycroft break;
426 1.1 mycroft }
427 1.1 mycroft vput(vp);
428 1.21 chs if (LIST_NEXT(vp, v_mntvnodes) != nextvp || vp->v_mount != mp)
429 1.1 mycroft goto again;
430 1.1 mycroft }
431 1.1 mycroft ump->um_qflags[type] &= ~QTF_OPENING;
432 1.1 mycroft if (error)
433 1.25 darrenr quotaoff(l, mp, type);
434 1.1 mycroft return (error);
435 1.1 mycroft }
436 1.1 mycroft
437 1.1 mycroft /*
438 1.1 mycroft * Q_QUOTAOFF - turn off disk quotas for a filesystem.
439 1.1 mycroft */
440 1.1 mycroft int
441 1.25 darrenr quotaoff(l, mp, type)
442 1.25 darrenr struct lwp *l;
443 1.1 mycroft struct mount *mp;
444 1.15 augustss int type;
445 1.1 mycroft {
446 1.11 fvdl struct vnode *vp;
447 1.1 mycroft struct vnode *qvp, *nextvp;
448 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
449 1.11 fvdl struct dquot *dq;
450 1.11 fvdl struct inode *ip;
451 1.1 mycroft int error;
452 1.1 mycroft
453 1.1 mycroft if ((qvp = ump->um_quotas[type]) == NULLVP)
454 1.1 mycroft return (0);
455 1.1 mycroft ump->um_qflags[type] |= QTF_CLOSING;
456 1.1 mycroft /*
457 1.1 mycroft * Search vnodes associated with this mount point,
458 1.1 mycroft * deleting any references to quota file being closed.
459 1.1 mycroft */
460 1.1 mycroft again:
461 1.21 chs for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nextvp) {
462 1.21 chs nextvp = LIST_NEXT(vp, v_mntvnodes);
463 1.13 fvdl if (vp->v_type == VNON)
464 1.13 fvdl continue;
465 1.26 thorpej if (vget(vp, LK_EXCLUSIVE))
466 1.1 mycroft goto again;
467 1.1 mycroft ip = VTOI(vp);
468 1.1 mycroft dq = ip->i_dquot[type];
469 1.1 mycroft ip->i_dquot[type] = NODQUOT;
470 1.1 mycroft dqrele(vp, dq);
471 1.1 mycroft vput(vp);
472 1.21 chs if (LIST_NEXT(vp, v_mntvnodes) != nextvp || vp->v_mount != mp)
473 1.1 mycroft goto again;
474 1.1 mycroft }
475 1.1 mycroft dqflush(qvp);
476 1.1 mycroft qvp->v_flag &= ~VSYSTEM;
477 1.25 darrenr error = vn_close(qvp, FREAD|FWRITE, l->l_proc->p_ucred, l);
478 1.1 mycroft ump->um_quotas[type] = NULLVP;
479 1.1 mycroft crfree(ump->um_cred[type]);
480 1.1 mycroft ump->um_cred[type] = NOCRED;
481 1.1 mycroft ump->um_qflags[type] &= ~QTF_CLOSING;
482 1.1 mycroft for (type = 0; type < MAXQUOTAS; type++)
483 1.1 mycroft if (ump->um_quotas[type] != NULLVP)
484 1.1 mycroft break;
485 1.1 mycroft if (type == MAXQUOTAS)
486 1.1 mycroft mp->mnt_flag &= ~MNT_QUOTA;
487 1.1 mycroft return (error);
488 1.1 mycroft }
489 1.1 mycroft
490 1.1 mycroft /*
491 1.1 mycroft * Q_GETQUOTA - return current values in a dqblk structure.
492 1.1 mycroft */
493 1.1 mycroft int
494 1.1 mycroft getquota(mp, id, type, addr)
495 1.1 mycroft struct mount *mp;
496 1.1 mycroft u_long id;
497 1.1 mycroft int type;
498 1.1 mycroft caddr_t addr;
499 1.1 mycroft {
500 1.1 mycroft struct dquot *dq;
501 1.1 mycroft int error;
502 1.1 mycroft
503 1.8 christos if ((error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq)) != 0)
504 1.1 mycroft return (error);
505 1.1 mycroft error = copyout((caddr_t)&dq->dq_dqb, addr, sizeof (struct dqblk));
506 1.1 mycroft dqrele(NULLVP, dq);
507 1.1 mycroft return (error);
508 1.1 mycroft }
509 1.1 mycroft
510 1.1 mycroft /*
511 1.1 mycroft * Q_SETQUOTA - assign an entire dqblk structure.
512 1.1 mycroft */
513 1.1 mycroft int
514 1.1 mycroft setquota(mp, id, type, addr)
515 1.1 mycroft struct mount *mp;
516 1.1 mycroft u_long id;
517 1.1 mycroft int type;
518 1.1 mycroft caddr_t addr;
519 1.1 mycroft {
520 1.15 augustss struct dquot *dq;
521 1.1 mycroft struct dquot *ndq;
522 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
523 1.1 mycroft struct dqblk newlim;
524 1.1 mycroft int error;
525 1.1 mycroft
526 1.8 christos error = copyin(addr, (caddr_t)&newlim, sizeof (struct dqblk));
527 1.8 christos if (error)
528 1.1 mycroft return (error);
529 1.8 christos if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
530 1.1 mycroft return (error);
531 1.1 mycroft dq = ndq;
532 1.1 mycroft while (dq->dq_flags & DQ_LOCK) {
533 1.1 mycroft dq->dq_flags |= DQ_WANT;
534 1.17 thorpej (void) tsleep(dq, PINOD+1, "setquota", 0);
535 1.1 mycroft }
536 1.1 mycroft /*
537 1.1 mycroft * Copy all but the current values.
538 1.1 mycroft * Reset time limit if previously had no soft limit or were
539 1.1 mycroft * under it, but now have a soft limit and are over it.
540 1.1 mycroft */
541 1.1 mycroft newlim.dqb_curblocks = dq->dq_curblocks;
542 1.1 mycroft newlim.dqb_curinodes = dq->dq_curinodes;
543 1.1 mycroft if (dq->dq_id != 0) {
544 1.1 mycroft newlim.dqb_btime = dq->dq_btime;
545 1.1 mycroft newlim.dqb_itime = dq->dq_itime;
546 1.1 mycroft }
547 1.1 mycroft if (newlim.dqb_bsoftlimit &&
548 1.1 mycroft dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
549 1.1 mycroft (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
550 1.1 mycroft newlim.dqb_btime = time.tv_sec + ump->um_btime[type];
551 1.1 mycroft if (newlim.dqb_isoftlimit &&
552 1.1 mycroft dq->dq_curinodes >= newlim.dqb_isoftlimit &&
553 1.1 mycroft (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
554 1.1 mycroft newlim.dqb_itime = time.tv_sec + ump->um_itime[type];
555 1.1 mycroft dq->dq_dqb = newlim;
556 1.1 mycroft if (dq->dq_curblocks < dq->dq_bsoftlimit)
557 1.1 mycroft dq->dq_flags &= ~DQ_BLKS;
558 1.1 mycroft if (dq->dq_curinodes < dq->dq_isoftlimit)
559 1.1 mycroft dq->dq_flags &= ~DQ_INODS;
560 1.1 mycroft if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
561 1.1 mycroft dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
562 1.1 mycroft dq->dq_flags |= DQ_FAKE;
563 1.1 mycroft else
564 1.1 mycroft dq->dq_flags &= ~DQ_FAKE;
565 1.1 mycroft dq->dq_flags |= DQ_MOD;
566 1.1 mycroft dqrele(NULLVP, dq);
567 1.1 mycroft return (0);
568 1.1 mycroft }
569 1.1 mycroft
570 1.1 mycroft /*
571 1.1 mycroft * Q_SETUSE - set current inode and block usage.
572 1.1 mycroft */
573 1.1 mycroft int
574 1.1 mycroft setuse(mp, id, type, addr)
575 1.1 mycroft struct mount *mp;
576 1.1 mycroft u_long id;
577 1.1 mycroft int type;
578 1.1 mycroft caddr_t addr;
579 1.1 mycroft {
580 1.15 augustss struct dquot *dq;
581 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
582 1.1 mycroft struct dquot *ndq;
583 1.1 mycroft struct dqblk usage;
584 1.1 mycroft int error;
585 1.1 mycroft
586 1.8 christos error = copyin(addr, (caddr_t)&usage, sizeof (struct dqblk));
587 1.8 christos if (error)
588 1.1 mycroft return (error);
589 1.8 christos if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
590 1.1 mycroft return (error);
591 1.1 mycroft dq = ndq;
592 1.1 mycroft while (dq->dq_flags & DQ_LOCK) {
593 1.1 mycroft dq->dq_flags |= DQ_WANT;
594 1.17 thorpej (void) tsleep(dq, PINOD+1, "setuse", 0);
595 1.1 mycroft }
596 1.1 mycroft /*
597 1.1 mycroft * Reset time limit if have a soft limit and were
598 1.1 mycroft * previously under it, but are now over it.
599 1.1 mycroft */
600 1.1 mycroft if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
601 1.1 mycroft usage.dqb_curblocks >= dq->dq_bsoftlimit)
602 1.1 mycroft dq->dq_btime = time.tv_sec + ump->um_btime[type];
603 1.1 mycroft if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
604 1.1 mycroft usage.dqb_curinodes >= dq->dq_isoftlimit)
605 1.1 mycroft dq->dq_itime = time.tv_sec + ump->um_itime[type];
606 1.1 mycroft dq->dq_curblocks = usage.dqb_curblocks;
607 1.1 mycroft dq->dq_curinodes = usage.dqb_curinodes;
608 1.1 mycroft if (dq->dq_curblocks < dq->dq_bsoftlimit)
609 1.1 mycroft dq->dq_flags &= ~DQ_BLKS;
610 1.1 mycroft if (dq->dq_curinodes < dq->dq_isoftlimit)
611 1.1 mycroft dq->dq_flags &= ~DQ_INODS;
612 1.1 mycroft dq->dq_flags |= DQ_MOD;
613 1.1 mycroft dqrele(NULLVP, dq);
614 1.1 mycroft return (0);
615 1.1 mycroft }
616 1.1 mycroft
617 1.1 mycroft /*
618 1.1 mycroft * Q_SYNC - sync quota files to disk.
619 1.1 mycroft */
620 1.1 mycroft int
621 1.25 darrenr qsync(l, mp)
622 1.25 darrenr struct lwp *l;
623 1.1 mycroft struct mount *mp;
624 1.1 mycroft {
625 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
626 1.11 fvdl struct vnode *vp, *nextvp;
627 1.11 fvdl struct dquot *dq;
628 1.11 fvdl int i, error;
629 1.1 mycroft
630 1.1 mycroft /*
631 1.1 mycroft * Check if the mount point has any quotas.
632 1.1 mycroft * If not, simply return.
633 1.1 mycroft */
634 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++)
635 1.1 mycroft if (ump->um_quotas[i] != NULLVP)
636 1.1 mycroft break;
637 1.1 mycroft if (i == MAXQUOTAS)
638 1.1 mycroft return (0);
639 1.1 mycroft /*
640 1.1 mycroft * Search vnodes associated with this mount point,
641 1.1 mycroft * synchronizing any modified dquot structures.
642 1.1 mycroft */
643 1.11 fvdl simple_lock(&mntvnode_slock);
644 1.1 mycroft again:
645 1.21 chs for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nextvp) {
646 1.11 fvdl if (vp->v_mount != mp)
647 1.11 fvdl goto again;
648 1.21 chs nextvp = LIST_NEXT(vp, v_mntvnodes);
649 1.13 fvdl if (vp->v_type == VNON)
650 1.13 fvdl continue;
651 1.11 fvdl simple_lock(&vp->v_interlock);
652 1.11 fvdl simple_unlock(&mntvnode_slock);
653 1.26 thorpej error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
654 1.11 fvdl if (error) {
655 1.11 fvdl simple_lock(&mntvnode_slock);
656 1.11 fvdl if (error == ENOENT)
657 1.11 fvdl goto again;
658 1.1 mycroft continue;
659 1.11 fvdl }
660 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
661 1.1 mycroft dq = VTOI(vp)->i_dquot[i];
662 1.1 mycroft if (dq != NODQUOT && (dq->dq_flags & DQ_MOD))
663 1.1 mycroft dqsync(vp, dq);
664 1.1 mycroft }
665 1.1 mycroft vput(vp);
666 1.11 fvdl simple_lock(&mntvnode_slock);
667 1.21 chs if (LIST_NEXT(vp, v_mntvnodes) != nextvp)
668 1.1 mycroft goto again;
669 1.1 mycroft }
670 1.11 fvdl simple_unlock(&mntvnode_slock);
671 1.1 mycroft return (0);
672 1.1 mycroft }
673 1.1 mycroft
674 1.1 mycroft /*
675 1.1 mycroft * Code pertaining to management of the in-core dquot data structures.
676 1.1 mycroft */
677 1.5 mycroft #define DQHASH(dqvp, id) \
678 1.21 chs (((((long)(dqvp)) >> 8) + id) & dqhash)
679 1.21 chs LIST_HEAD(dqhashhead, dquot) *dqhashtbl;
680 1.1 mycroft u_long dqhash;
681 1.1 mycroft
682 1.1 mycroft /*
683 1.1 mycroft * Dquot free list.
684 1.1 mycroft */
685 1.1 mycroft #define DQUOTINC 5 /* minimum free dquots desired */
686 1.5 mycroft TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
687 1.1 mycroft long numdquot, desireddquot = DQUOTINC;
688 1.23 thorpej
689 1.23 thorpej MALLOC_DEFINE(M_DQUOT, "UFS quota", "UFS quota entries");
690 1.1 mycroft
691 1.1 mycroft /*
692 1.1 mycroft * Initialize the quota system.
693 1.1 mycroft */
694 1.1 mycroft void
695 1.1 mycroft dqinit()
696 1.1 mycroft {
697 1.20 ad dqhashtbl =
698 1.20 ad hashinit(desiredvnodes, HASH_LIST, M_DQUOT, M_WAITOK, &dqhash);
699 1.5 mycroft TAILQ_INIT(&dqfreelist);
700 1.14 jdolecek }
701 1.14 jdolecek
702 1.21 chs void
703 1.21 chs dqreinit()
704 1.21 chs {
705 1.21 chs struct dquot *dq;
706 1.21 chs struct dqhashhead *oldhash, *hash;
707 1.21 chs struct vnode *dqvp;
708 1.21 chs u_long oldmask, mask, hashval;
709 1.21 chs int i;
710 1.21 chs
711 1.21 chs hash = hashinit(desiredvnodes, HASH_LIST, M_DQUOT, M_WAITOK, &mask);
712 1.21 chs oldhash = dqhashtbl;
713 1.21 chs oldmask = dqhash;
714 1.21 chs dqhashtbl = hash;
715 1.21 chs dqhash = mask;
716 1.21 chs for (i = 0; i <= oldmask; i++) {
717 1.21 chs while ((dq = LIST_FIRST(&oldhash[i])) != NULL) {
718 1.21 chs dqvp = dq->dq_ump->um_quotas[dq->dq_type];
719 1.21 chs LIST_REMOVE(dq, dq_hash);
720 1.21 chs hashval = DQHASH(dqvp, dq->dq_id);
721 1.21 chs LIST_INSERT_HEAD(&dqhashtbl[hashval], dq, dq_hash);
722 1.21 chs }
723 1.21 chs }
724 1.21 chs hashdone(oldhash, M_DQUOT);
725 1.21 chs }
726 1.21 chs
727 1.14 jdolecek /*
728 1.14 jdolecek * Free resources held by quota system.
729 1.14 jdolecek */
730 1.14 jdolecek void
731 1.14 jdolecek dqdone()
732 1.14 jdolecek {
733 1.14 jdolecek hashdone(dqhashtbl, M_DQUOT);
734 1.1 mycroft }
735 1.1 mycroft
736 1.1 mycroft /*
737 1.1 mycroft * Obtain a dquot structure for the specified identifier and quota file
738 1.1 mycroft * reading the information from the file if necessary.
739 1.1 mycroft */
740 1.1 mycroft int
741 1.1 mycroft dqget(vp, id, ump, type, dqp)
742 1.1 mycroft struct vnode *vp;
743 1.1 mycroft u_long id;
744 1.15 augustss struct ufsmount *ump;
745 1.15 augustss int type;
746 1.1 mycroft struct dquot **dqp;
747 1.1 mycroft {
748 1.11 fvdl struct dquot *dq;
749 1.21 chs struct dqhashhead *dqh;
750 1.11 fvdl struct vnode *dqvp;
751 1.1 mycroft struct iovec aiov;
752 1.1 mycroft struct uio auio;
753 1.1 mycroft int error;
754 1.1 mycroft
755 1.1 mycroft dqvp = ump->um_quotas[type];
756 1.1 mycroft if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
757 1.1 mycroft *dqp = NODQUOT;
758 1.1 mycroft return (EINVAL);
759 1.1 mycroft }
760 1.1 mycroft /*
761 1.1 mycroft * Check the cache first.
762 1.1 mycroft */
763 1.21 chs dqh = &dqhashtbl[DQHASH(dqvp, id)];
764 1.21 chs LIST_FOREACH(dq, dqh, dq_hash) {
765 1.1 mycroft if (dq->dq_id != id ||
766 1.1 mycroft dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
767 1.1 mycroft continue;
768 1.1 mycroft /*
769 1.1 mycroft * Cache hit with no references. Take
770 1.1 mycroft * the structure off the free list.
771 1.1 mycroft */
772 1.5 mycroft if (dq->dq_cnt == 0)
773 1.5 mycroft TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
774 1.1 mycroft DQREF(dq);
775 1.1 mycroft *dqp = dq;
776 1.1 mycroft return (0);
777 1.1 mycroft }
778 1.1 mycroft /*
779 1.1 mycroft * Not in cache, allocate a new one.
780 1.1 mycroft */
781 1.5 mycroft if (dqfreelist.tqh_first == NODQUOT &&
782 1.5 mycroft numdquot < MAXQUOTAS * desiredvnodes)
783 1.1 mycroft desireddquot += DQUOTINC;
784 1.1 mycroft if (numdquot < desireddquot) {
785 1.1 mycroft dq = (struct dquot *)malloc(sizeof *dq, M_DQUOT, M_WAITOK);
786 1.12 perry memset((char *)dq, 0, sizeof *dq);
787 1.1 mycroft numdquot++;
788 1.1 mycroft } else {
789 1.5 mycroft if ((dq = dqfreelist.tqh_first) == NULL) {
790 1.18 mjacob tablefull("dquot",
791 1.19 jdolecek "increase kern.maxvnodes or NVNODE");
792 1.1 mycroft *dqp = NODQUOT;
793 1.1 mycroft return (EUSERS);
794 1.1 mycroft }
795 1.1 mycroft if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
796 1.1 mycroft panic("free dquot isn't");
797 1.5 mycroft TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
798 1.5 mycroft LIST_REMOVE(dq, dq_hash);
799 1.1 mycroft }
800 1.1 mycroft /*
801 1.1 mycroft * Initialize the contents of the dquot structure.
802 1.1 mycroft */
803 1.1 mycroft if (vp != dqvp)
804 1.11 fvdl vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
805 1.5 mycroft LIST_INSERT_HEAD(dqh, dq, dq_hash);
806 1.1 mycroft DQREF(dq);
807 1.1 mycroft dq->dq_flags = DQ_LOCK;
808 1.1 mycroft dq->dq_id = id;
809 1.1 mycroft dq->dq_ump = ump;
810 1.1 mycroft dq->dq_type = type;
811 1.1 mycroft auio.uio_iov = &aiov;
812 1.1 mycroft auio.uio_iovcnt = 1;
813 1.1 mycroft aiov.iov_base = (caddr_t)&dq->dq_dqb;
814 1.1 mycroft aiov.iov_len = sizeof (struct dqblk);
815 1.1 mycroft auio.uio_resid = sizeof (struct dqblk);
816 1.1 mycroft auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
817 1.1 mycroft auio.uio_segflg = UIO_SYSSPACE;
818 1.1 mycroft auio.uio_rw = UIO_READ;
819 1.25 darrenr auio.uio_lwp = (struct lwp *)0;
820 1.1 mycroft error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
821 1.1 mycroft if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
822 1.12 perry memset((caddr_t)&dq->dq_dqb, 0, sizeof(struct dqblk));
823 1.1 mycroft if (vp != dqvp)
824 1.11 fvdl VOP_UNLOCK(dqvp, 0);
825 1.1 mycroft if (dq->dq_flags & DQ_WANT)
826 1.1 mycroft wakeup((caddr_t)dq);
827 1.1 mycroft dq->dq_flags = 0;
828 1.1 mycroft /*
829 1.1 mycroft * I/O error in reading quota file, release
830 1.1 mycroft * quota structure and reflect problem to caller.
831 1.1 mycroft */
832 1.1 mycroft if (error) {
833 1.5 mycroft LIST_REMOVE(dq, dq_hash);
834 1.1 mycroft dqrele(vp, dq);
835 1.1 mycroft *dqp = NODQUOT;
836 1.1 mycroft return (error);
837 1.1 mycroft }
838 1.1 mycroft /*
839 1.1 mycroft * Check for no limit to enforce.
840 1.1 mycroft * Initialize time values if necessary.
841 1.1 mycroft */
842 1.1 mycroft if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
843 1.1 mycroft dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
844 1.1 mycroft dq->dq_flags |= DQ_FAKE;
845 1.1 mycroft if (dq->dq_id != 0) {
846 1.1 mycroft if (dq->dq_btime == 0)
847 1.1 mycroft dq->dq_btime = time.tv_sec + ump->um_btime[type];
848 1.1 mycroft if (dq->dq_itime == 0)
849 1.1 mycroft dq->dq_itime = time.tv_sec + ump->um_itime[type];
850 1.1 mycroft }
851 1.1 mycroft *dqp = dq;
852 1.1 mycroft return (0);
853 1.1 mycroft }
854 1.1 mycroft
855 1.1 mycroft /*
856 1.1 mycroft * Obtain a reference to a dquot.
857 1.1 mycroft */
858 1.1 mycroft void
859 1.1 mycroft dqref(dq)
860 1.1 mycroft struct dquot *dq;
861 1.1 mycroft {
862 1.1 mycroft
863 1.1 mycroft dq->dq_cnt++;
864 1.1 mycroft }
865 1.1 mycroft
866 1.1 mycroft /*
867 1.1 mycroft * Release a reference to a dquot.
868 1.1 mycroft */
869 1.1 mycroft void
870 1.1 mycroft dqrele(vp, dq)
871 1.1 mycroft struct vnode *vp;
872 1.15 augustss struct dquot *dq;
873 1.1 mycroft {
874 1.1 mycroft
875 1.1 mycroft if (dq == NODQUOT)
876 1.1 mycroft return;
877 1.1 mycroft if (dq->dq_cnt > 1) {
878 1.1 mycroft dq->dq_cnt--;
879 1.1 mycroft return;
880 1.1 mycroft }
881 1.1 mycroft if (dq->dq_flags & DQ_MOD)
882 1.1 mycroft (void) dqsync(vp, dq);
883 1.1 mycroft if (--dq->dq_cnt > 0)
884 1.1 mycroft return;
885 1.5 mycroft TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
886 1.1 mycroft }
887 1.1 mycroft
888 1.1 mycroft /*
889 1.1 mycroft * Update the disk quota in the quota file.
890 1.1 mycroft */
891 1.1 mycroft int
892 1.1 mycroft dqsync(vp, dq)
893 1.1 mycroft struct vnode *vp;
894 1.15 augustss struct dquot *dq;
895 1.1 mycroft {
896 1.1 mycroft struct vnode *dqvp;
897 1.1 mycroft struct iovec aiov;
898 1.1 mycroft struct uio auio;
899 1.1 mycroft int error;
900 1.1 mycroft
901 1.1 mycroft if (dq == NODQUOT)
902 1.1 mycroft panic("dqsync: dquot");
903 1.1 mycroft if ((dq->dq_flags & DQ_MOD) == 0)
904 1.1 mycroft return (0);
905 1.1 mycroft if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
906 1.1 mycroft panic("dqsync: file");
907 1.1 mycroft if (vp != dqvp)
908 1.11 fvdl vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
909 1.1 mycroft while (dq->dq_flags & DQ_LOCK) {
910 1.1 mycroft dq->dq_flags |= DQ_WANT;
911 1.17 thorpej (void) tsleep(dq, PINOD+2, "dqsync", 0);
912 1.1 mycroft if ((dq->dq_flags & DQ_MOD) == 0) {
913 1.1 mycroft if (vp != dqvp)
914 1.11 fvdl VOP_UNLOCK(dqvp, 0);
915 1.1 mycroft return (0);
916 1.1 mycroft }
917 1.1 mycroft }
918 1.1 mycroft dq->dq_flags |= DQ_LOCK;
919 1.1 mycroft auio.uio_iov = &aiov;
920 1.1 mycroft auio.uio_iovcnt = 1;
921 1.1 mycroft aiov.iov_base = (caddr_t)&dq->dq_dqb;
922 1.1 mycroft aiov.iov_len = sizeof (struct dqblk);
923 1.1 mycroft auio.uio_resid = sizeof (struct dqblk);
924 1.1 mycroft auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
925 1.1 mycroft auio.uio_segflg = UIO_SYSSPACE;
926 1.1 mycroft auio.uio_rw = UIO_WRITE;
927 1.25 darrenr auio.uio_lwp = (struct lwp *)0;
928 1.1 mycroft error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
929 1.1 mycroft if (auio.uio_resid && error == 0)
930 1.1 mycroft error = EIO;
931 1.1 mycroft if (dq->dq_flags & DQ_WANT)
932 1.1 mycroft wakeup((caddr_t)dq);
933 1.1 mycroft dq->dq_flags &= ~(DQ_MOD|DQ_LOCK|DQ_WANT);
934 1.1 mycroft if (vp != dqvp)
935 1.11 fvdl VOP_UNLOCK(dqvp, 0);
936 1.1 mycroft return (error);
937 1.1 mycroft }
938 1.1 mycroft
939 1.1 mycroft /*
940 1.1 mycroft * Flush all entries from the cache for a particular vnode.
941 1.1 mycroft */
942 1.1 mycroft void
943 1.1 mycroft dqflush(vp)
944 1.15 augustss struct vnode *vp;
945 1.1 mycroft {
946 1.15 augustss struct dquot *dq, *nextdq;
947 1.21 chs struct dqhashhead *dqh;
948 1.1 mycroft
949 1.1 mycroft /*
950 1.1 mycroft * Move all dquot's that used to refer to this quota
951 1.1 mycroft * file off their hash chains (they will eventually
952 1.1 mycroft * fall off the head of the free list and be re-used).
953 1.1 mycroft */
954 1.5 mycroft for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
955 1.21 chs for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
956 1.21 chs nextdq = LIST_NEXT(dq, dq_hash);
957 1.1 mycroft if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
958 1.1 mycroft continue;
959 1.1 mycroft if (dq->dq_cnt)
960 1.1 mycroft panic("dqflush: stray dquot");
961 1.5 mycroft LIST_REMOVE(dq, dq_hash);
962 1.21 chs dq->dq_ump = NULL;
963 1.1 mycroft }
964 1.1 mycroft }
965 1.1 mycroft }
966