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