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