ufs_quota.c revision 1.67 1 1.67 hannken /* $NetBSD: ufs_quota.c,v 1.67 2010/07/21 17:52:14 hannken 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.67 hannken __KERNEL_RCSID(0, "$NetBSD: ufs_quota.c,v 1.67 2010/07/21 17:52:14 hannken 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/file.h>
45 1.1 mycroft #include <sys/proc.h>
46 1.1 mycroft #include <sys/vnode.h>
47 1.1 mycroft #include <sys/mount.h>
48 1.39 elad #include <sys/kauth.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.48 hannken * The following structure records disk usage for a user or group on a
57 1.48 hannken * filesystem. There is one allocated for each quota that exists on any
58 1.48 hannken * filesystem for the current user or group. A cache is kept of recently
59 1.48 hannken * used entries.
60 1.49 hannken * Field markings and the corresponding locks:
61 1.50 hannken * h: dqlock
62 1.49 hannken * d: dq_interlock
63 1.49 hannken *
64 1.50 hannken * Lock order is: dq_interlock -> dqlock
65 1.50 hannken * dq_interlock -> dqvp
66 1.48 hannken */
67 1.48 hannken struct dquot {
68 1.49 hannken LIST_ENTRY(dquot) dq_hash; /* h: hash list */
69 1.49 hannken u_int16_t dq_flags; /* d: flags, see below */
70 1.49 hannken u_int16_t dq_type; /* d: quota type of this dquot */
71 1.49 hannken u_int32_t dq_cnt; /* h: count of active references */
72 1.49 hannken u_int32_t dq_id; /* d: identifier this applies to */
73 1.49 hannken struct ufsmount *dq_ump; /* d: filesystem this is taken from */
74 1.49 hannken kmutex_t dq_interlock; /* d: lock this dquot */
75 1.49 hannken struct dqblk dq_dqb; /* d: actual usage & quotas */
76 1.48 hannken };
77 1.48 hannken /*
78 1.48 hannken * Flag values.
79 1.48 hannken */
80 1.48 hannken #define DQ_MOD 0x04 /* this quota modified since read */
81 1.48 hannken #define DQ_FAKE 0x08 /* no limits here, just usage */
82 1.48 hannken #define DQ_BLKS 0x10 /* has been warned about blk limit */
83 1.48 hannken #define DQ_INODS 0x20 /* has been warned about inode limit */
84 1.48 hannken /*
85 1.48 hannken * Shorthand notation.
86 1.48 hannken */
87 1.48 hannken #define dq_bhardlimit dq_dqb.dqb_bhardlimit
88 1.48 hannken #define dq_bsoftlimit dq_dqb.dqb_bsoftlimit
89 1.48 hannken #define dq_curblocks dq_dqb.dqb_curblocks
90 1.48 hannken #define dq_ihardlimit dq_dqb.dqb_ihardlimit
91 1.48 hannken #define dq_isoftlimit dq_dqb.dqb_isoftlimit
92 1.48 hannken #define dq_curinodes dq_dqb.dqb_curinodes
93 1.48 hannken #define dq_btime dq_dqb.dqb_btime
94 1.48 hannken #define dq_itime dq_dqb.dqb_itime
95 1.48 hannken /*
96 1.48 hannken * If the system has never checked for a quota for this file, then it is
97 1.48 hannken * set to NODQUOT. Once a write attempt is made the inode pointer is set
98 1.48 hannken * to reference a dquot structure.
99 1.48 hannken */
100 1.48 hannken #define NODQUOT NULL
101 1.48 hannken
102 1.48 hannken static int chkdqchg(struct inode *, int64_t, kauth_cred_t, int);
103 1.48 hannken static int chkiqchg(struct inode *, int32_t, kauth_cred_t, int);
104 1.50 hannken #ifdef DIAGNOSTIC
105 1.48 hannken static void dqflush(struct vnode *);
106 1.50 hannken #endif
107 1.48 hannken static int dqget(struct vnode *, u_long, struct ufsmount *, int,
108 1.48 hannken struct dquot **);
109 1.48 hannken static void dqref(struct dquot *);
110 1.48 hannken static void dqrele(struct vnode *, struct dquot *);
111 1.48 hannken static int dqsync(struct vnode *, struct dquot *);
112 1.48 hannken
113 1.50 hannken static kmutex_t dqlock;
114 1.50 hannken static kcondvar_t dqcv;
115 1.48 hannken /*
116 1.1 mycroft * Quota name to error message mapping.
117 1.1 mycroft */
118 1.34 christos static const char *quotatypes[] = INITQFNAMES;
119 1.1 mycroft
120 1.1 mycroft /*
121 1.1 mycroft * Set up the quotas for an inode.
122 1.1 mycroft *
123 1.1 mycroft * This routine completely defines the semantics of quotas.
124 1.1 mycroft * If other criterion want to be used to establish quotas, the
125 1.1 mycroft * MAXQUOTAS value in quotas.h should be increased, and the
126 1.1 mycroft * additional dquots set up here.
127 1.1 mycroft */
128 1.1 mycroft int
129 1.35 thorpej getinoquota(struct inode *ip)
130 1.1 mycroft {
131 1.31 mycroft struct ufsmount *ump = ip->i_ump;
132 1.1 mycroft struct vnode *vp = ITOV(ip);
133 1.49 hannken int i, error;
134 1.49 hannken u_int32_t ino_ids[MAXQUOTAS];
135 1.1 mycroft
136 1.1 mycroft /*
137 1.49 hannken * To avoid deadlocks never update quotas for quota files
138 1.49 hannken * on the same file system
139 1.48 hannken */
140 1.49 hannken for (i = 0; i < MAXQUOTAS; i++)
141 1.49 hannken if (ITOV(ip) == ump->um_quotas[i])
142 1.49 hannken return 0;
143 1.49 hannken
144 1.49 hannken ino_ids[USRQUOTA] = ip->i_uid;
145 1.49 hannken ino_ids[GRPQUOTA] = ip->i_gid;
146 1.49 hannken for (i = 0; i < MAXQUOTAS; i++) {
147 1.49 hannken /*
148 1.49 hannken * If the file id changed the quota needs update.
149 1.49 hannken */
150 1.49 hannken if (ip->i_dquot[i] != NODQUOT &&
151 1.49 hannken ip->i_dquot[i]->dq_id != ino_ids[i]) {
152 1.49 hannken dqrele(ITOV(ip), ip->i_dquot[i]);
153 1.49 hannken ip->i_dquot[i] = NODQUOT;
154 1.49 hannken }
155 1.49 hannken /*
156 1.49 hannken * Set up the quota based on file id.
157 1.49 hannken * EINVAL means that quotas are not enabled.
158 1.49 hannken */
159 1.49 hannken if (ip->i_dquot[i] == NODQUOT &&
160 1.49 hannken (error = dqget(vp, ino_ids[i], ump, i, &ip->i_dquot[i])) &&
161 1.49 hannken error != EINVAL)
162 1.49 hannken return (error);
163 1.48 hannken }
164 1.49 hannken return 0;
165 1.1 mycroft }
166 1.1 mycroft
167 1.1 mycroft /*
168 1.48 hannken * Initialize the quota fields of an inode.
169 1.48 hannken */
170 1.48 hannken void
171 1.48 hannken ufsquota_init(struct inode *ip)
172 1.48 hannken {
173 1.48 hannken int i;
174 1.48 hannken
175 1.48 hannken for (i = 0; i < MAXQUOTAS; i++)
176 1.48 hannken ip->i_dquot[i] = NODQUOT;
177 1.48 hannken }
178 1.48 hannken
179 1.48 hannken /*
180 1.48 hannken * Release the quota fields from an inode.
181 1.48 hannken */
182 1.48 hannken void
183 1.48 hannken ufsquota_free(struct inode *ip)
184 1.48 hannken {
185 1.48 hannken int i;
186 1.48 hannken
187 1.48 hannken for (i = 0; i < MAXQUOTAS; i++) {
188 1.48 hannken dqrele(ITOV(ip), ip->i_dquot[i]);
189 1.48 hannken ip->i_dquot[i] = NODQUOT;
190 1.48 hannken }
191 1.48 hannken }
192 1.48 hannken
193 1.48 hannken /*
194 1.1 mycroft * Update disk usage, and take corrective action.
195 1.1 mycroft */
196 1.1 mycroft int
197 1.39 elad chkdq(struct inode *ip, int64_t change, kauth_cred_t cred, int flags)
198 1.1 mycroft {
199 1.15 augustss struct dquot *dq;
200 1.15 augustss int i;
201 1.1 mycroft int ncurblocks, error;
202 1.1 mycroft
203 1.48 hannken if ((error = getinoquota(ip)) != 0)
204 1.48 hannken return error;
205 1.1 mycroft if (change == 0)
206 1.1 mycroft return (0);
207 1.1 mycroft if (change < 0) {
208 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
209 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
210 1.1 mycroft continue;
211 1.49 hannken mutex_enter(&dq->dq_interlock);
212 1.1 mycroft ncurblocks = dq->dq_curblocks + change;
213 1.1 mycroft if (ncurblocks >= 0)
214 1.1 mycroft dq->dq_curblocks = ncurblocks;
215 1.1 mycroft else
216 1.1 mycroft dq->dq_curblocks = 0;
217 1.1 mycroft dq->dq_flags &= ~DQ_BLKS;
218 1.1 mycroft dq->dq_flags |= DQ_MOD;
219 1.49 hannken mutex_exit(&dq->dq_interlock);
220 1.1 mycroft }
221 1.1 mycroft return (0);
222 1.1 mycroft }
223 1.16 thorpej if ((flags & FORCE) == 0 &&
224 1.62 elad kauth_authorize_system(cred, KAUTH_SYSTEM_FS_QUOTA,
225 1.62 elad KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT, NULL, NULL, NULL) != 0) {
226 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
227 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
228 1.1 mycroft continue;
229 1.49 hannken mutex_enter(&dq->dq_interlock);
230 1.49 hannken error = chkdqchg(ip, change, cred, i);
231 1.49 hannken mutex_exit(&dq->dq_interlock);
232 1.49 hannken if (error != 0)
233 1.1 mycroft return (error);
234 1.1 mycroft }
235 1.1 mycroft }
236 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
237 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
238 1.1 mycroft continue;
239 1.49 hannken mutex_enter(&dq->dq_interlock);
240 1.1 mycroft dq->dq_curblocks += change;
241 1.1 mycroft dq->dq_flags |= DQ_MOD;
242 1.49 hannken mutex_exit(&dq->dq_interlock);
243 1.1 mycroft }
244 1.1 mycroft return (0);
245 1.1 mycroft }
246 1.1 mycroft
247 1.1 mycroft /*
248 1.1 mycroft * Check for a valid change to a users allocation.
249 1.1 mycroft * Issue an error message if appropriate.
250 1.1 mycroft */
251 1.48 hannken static int
252 1.39 elad chkdqchg(struct inode *ip, int64_t change, kauth_cred_t cred, int type)
253 1.1 mycroft {
254 1.15 augustss struct dquot *dq = ip->i_dquot[type];
255 1.1 mycroft long ncurblocks = dq->dq_curblocks + change;
256 1.1 mycroft
257 1.50 hannken KASSERT(mutex_owned(&dq->dq_interlock));
258 1.1 mycroft /*
259 1.1 mycroft * If user would exceed their hard limit, disallow space allocation.
260 1.1 mycroft */
261 1.1 mycroft if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
262 1.1 mycroft if ((dq->dq_flags & DQ_BLKS) == 0 &&
263 1.39 elad ip->i_uid == kauth_cred_geteuid(cred)) {
264 1.1 mycroft uprintf("\n%s: write failed, %s disk limit reached\n",
265 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
266 1.1 mycroft quotatypes[type]);
267 1.1 mycroft dq->dq_flags |= DQ_BLKS;
268 1.1 mycroft }
269 1.1 mycroft return (EDQUOT);
270 1.1 mycroft }
271 1.1 mycroft /*
272 1.1 mycroft * If user is over their soft limit for too long, disallow space
273 1.1 mycroft * allocation. Reset time limit as they cross their soft limit.
274 1.1 mycroft */
275 1.1 mycroft if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
276 1.1 mycroft if (dq->dq_curblocks < dq->dq_bsoftlimit) {
277 1.40 kardel dq->dq_btime = time_second + ip->i_ump->um_btime[type];
278 1.39 elad if (ip->i_uid == kauth_cred_geteuid(cred))
279 1.1 mycroft uprintf("\n%s: warning, %s %s\n",
280 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
281 1.1 mycroft quotatypes[type], "disk quota exceeded");
282 1.1 mycroft return (0);
283 1.1 mycroft }
284 1.40 kardel if (time_second > dq->dq_btime) {
285 1.1 mycroft if ((dq->dq_flags & DQ_BLKS) == 0 &&
286 1.39 elad ip->i_uid == kauth_cred_geteuid(cred)) {
287 1.1 mycroft uprintf("\n%s: write failed, %s %s\n",
288 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
289 1.1 mycroft quotatypes[type],
290 1.1 mycroft "disk quota exceeded for too long");
291 1.1 mycroft dq->dq_flags |= DQ_BLKS;
292 1.1 mycroft }
293 1.1 mycroft return (EDQUOT);
294 1.1 mycroft }
295 1.1 mycroft }
296 1.1 mycroft return (0);
297 1.1 mycroft }
298 1.1 mycroft
299 1.1 mycroft /*
300 1.1 mycroft * Check the inode limit, applying corrective action.
301 1.1 mycroft */
302 1.1 mycroft int
303 1.39 elad chkiq(struct inode *ip, int32_t change, kauth_cred_t cred, int flags)
304 1.1 mycroft {
305 1.15 augustss struct dquot *dq;
306 1.15 augustss int i;
307 1.1 mycroft int ncurinodes, error;
308 1.1 mycroft
309 1.48 hannken if ((error = getinoquota(ip)) != 0)
310 1.48 hannken return error;
311 1.1 mycroft if (change == 0)
312 1.1 mycroft return (0);
313 1.1 mycroft if (change < 0) {
314 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
315 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
316 1.1 mycroft continue;
317 1.49 hannken mutex_enter(&dq->dq_interlock);
318 1.1 mycroft ncurinodes = dq->dq_curinodes + change;
319 1.1 mycroft if (ncurinodes >= 0)
320 1.1 mycroft dq->dq_curinodes = ncurinodes;
321 1.1 mycroft else
322 1.1 mycroft dq->dq_curinodes = 0;
323 1.1 mycroft dq->dq_flags &= ~DQ_INODS;
324 1.1 mycroft dq->dq_flags |= DQ_MOD;
325 1.49 hannken mutex_exit(&dq->dq_interlock);
326 1.1 mycroft }
327 1.1 mycroft return (0);
328 1.1 mycroft }
329 1.62 elad if ((flags & FORCE) == 0 && kauth_authorize_system(cred,
330 1.62 elad KAUTH_SYSTEM_FS_QUOTA, KAUTH_REQ_SYSTEM_FS_QUOTA_NOLIMIT, NULL,
331 1.62 elad NULL, NULL) != 0) {
332 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
333 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
334 1.1 mycroft continue;
335 1.49 hannken mutex_enter(&dq->dq_interlock);
336 1.49 hannken error = chkiqchg(ip, change, cred, i);
337 1.49 hannken mutex_exit(&dq->dq_interlock);
338 1.49 hannken if (error != 0)
339 1.1 mycroft return (error);
340 1.1 mycroft }
341 1.1 mycroft }
342 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
343 1.1 mycroft if ((dq = ip->i_dquot[i]) == NODQUOT)
344 1.1 mycroft continue;
345 1.49 hannken mutex_enter(&dq->dq_interlock);
346 1.1 mycroft dq->dq_curinodes += change;
347 1.1 mycroft dq->dq_flags |= DQ_MOD;
348 1.49 hannken mutex_exit(&dq->dq_interlock);
349 1.1 mycroft }
350 1.1 mycroft return (0);
351 1.1 mycroft }
352 1.1 mycroft
353 1.1 mycroft /*
354 1.1 mycroft * Check for a valid change to a users allocation.
355 1.1 mycroft * Issue an error message if appropriate.
356 1.1 mycroft */
357 1.48 hannken static int
358 1.39 elad chkiqchg(struct inode *ip, int32_t change, kauth_cred_t cred, int type)
359 1.1 mycroft {
360 1.15 augustss struct dquot *dq = ip->i_dquot[type];
361 1.1 mycroft long ncurinodes = dq->dq_curinodes + change;
362 1.1 mycroft
363 1.50 hannken KASSERT(mutex_owned(&dq->dq_interlock));
364 1.1 mycroft /*
365 1.1 mycroft * If user would exceed their hard limit, disallow inode allocation.
366 1.1 mycroft */
367 1.1 mycroft if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
368 1.1 mycroft if ((dq->dq_flags & DQ_INODS) == 0 &&
369 1.39 elad ip->i_uid == kauth_cred_geteuid(cred)) {
370 1.1 mycroft uprintf("\n%s: write failed, %s inode limit reached\n",
371 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
372 1.1 mycroft quotatypes[type]);
373 1.1 mycroft dq->dq_flags |= DQ_INODS;
374 1.1 mycroft }
375 1.1 mycroft return (EDQUOT);
376 1.1 mycroft }
377 1.1 mycroft /*
378 1.1 mycroft * If user is over their soft limit for too long, disallow inode
379 1.1 mycroft * allocation. Reset time limit as they cross their soft limit.
380 1.1 mycroft */
381 1.1 mycroft if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
382 1.1 mycroft if (dq->dq_curinodes < dq->dq_isoftlimit) {
383 1.40 kardel dq->dq_itime = time_second + ip->i_ump->um_itime[type];
384 1.39 elad if (ip->i_uid == kauth_cred_geteuid(cred))
385 1.1 mycroft uprintf("\n%s: warning, %s %s\n",
386 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
387 1.1 mycroft quotatypes[type], "inode quota exceeded");
388 1.1 mycroft return (0);
389 1.1 mycroft }
390 1.40 kardel if (time_second > dq->dq_itime) {
391 1.1 mycroft if ((dq->dq_flags & DQ_INODS) == 0 &&
392 1.39 elad ip->i_uid == kauth_cred_geteuid(cred)) {
393 1.1 mycroft uprintf("\n%s: write failed, %s %s\n",
394 1.1 mycroft ITOV(ip)->v_mount->mnt_stat.f_mntonname,
395 1.1 mycroft quotatypes[type],
396 1.1 mycroft "inode quota exceeded for too long");
397 1.1 mycroft dq->dq_flags |= DQ_INODS;
398 1.1 mycroft }
399 1.1 mycroft return (EDQUOT);
400 1.1 mycroft }
401 1.1 mycroft }
402 1.1 mycroft return (0);
403 1.1 mycroft }
404 1.1 mycroft
405 1.1 mycroft /*
406 1.1 mycroft * Code to process quotactl commands.
407 1.1 mycroft */
408 1.1 mycroft
409 1.1 mycroft /*
410 1.1 mycroft * Q_QUOTAON - set up a quota file for a particular file system.
411 1.1 mycroft */
412 1.1 mycroft int
413 1.44 christos quotaon(struct lwp *l, struct mount *mp, int type, void *fname)
414 1.1 mycroft {
415 1.11 fvdl struct ufsmount *ump = VFSTOUFS(mp);
416 1.53 ad struct vnode *vp, **vpp, *mvp;
417 1.1 mycroft struct dquot *dq;
418 1.1 mycroft int error;
419 1.1 mycroft struct nameidata nd;
420 1.1 mycroft
421 1.61 ad /* XXX XXX XXX */
422 1.61 ad if (mp->mnt_wapbl != NULL) {
423 1.61 ad printf("%s: quotas cannot yet be used with -o log\n",
424 1.61 ad mp->mnt_stat.f_mntonname);
425 1.61 ad return (EOPNOTSUPP);
426 1.61 ad }
427 1.61 ad
428 1.1 mycroft vpp = &ump->um_quotas[type];
429 1.52 pooka NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname);
430 1.8 christos if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0)
431 1.1 mycroft return (error);
432 1.1 mycroft vp = nd.ni_vp;
433 1.66 hannken VOP_UNLOCK(vp);
434 1.1 mycroft if (vp->v_type != VREG) {
435 1.59 ad (void) vn_close(vp, FREAD|FWRITE, l->l_cred);
436 1.1 mycroft return (EACCES);
437 1.1 mycroft }
438 1.1 mycroft if (*vpp != vp)
439 1.36 christos quotaoff(l, mp, type);
440 1.50 hannken mutex_enter(&dqlock);
441 1.50 hannken while ((ump->um_qflags[type] & (QTF_CLOSING | QTF_OPENING)) != 0)
442 1.50 hannken cv_wait(&dqcv, &dqlock);
443 1.1 mycroft ump->um_qflags[type] |= QTF_OPENING;
444 1.50 hannken mutex_exit(&dqlock);
445 1.1 mycroft mp->mnt_flag |= MNT_QUOTA;
446 1.51 ad vp->v_vflag |= VV_SYSTEM; /* XXXSMP */
447 1.1 mycroft *vpp = vp;
448 1.1 mycroft /*
449 1.1 mycroft * Save the credential of the process that turned on quotas.
450 1.1 mycroft * Set up the time limits for this quota.
451 1.1 mycroft */
452 1.41 ad kauth_cred_hold(l->l_cred);
453 1.41 ad ump->um_cred[type] = l->l_cred;
454 1.1 mycroft ump->um_btime[type] = MAX_DQ_TIME;
455 1.1 mycroft ump->um_itime[type] = MAX_IQ_TIME;
456 1.1 mycroft if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
457 1.1 mycroft if (dq->dq_btime > 0)
458 1.1 mycroft ump->um_btime[type] = dq->dq_btime;
459 1.1 mycroft if (dq->dq_itime > 0)
460 1.1 mycroft ump->um_itime[type] = dq->dq_itime;
461 1.1 mycroft dqrele(NULLVP, dq);
462 1.1 mycroft }
463 1.53 ad /* Allocate a marker vnode. */
464 1.54 pooka if ((mvp = vnalloc(mp)) == NULL) {
465 1.53 ad error = ENOMEM;
466 1.53 ad goto out;
467 1.53 ad }
468 1.1 mycroft /*
469 1.1 mycroft * Search vnodes associated with this mount point,
470 1.1 mycroft * adding references to quota file being opened.
471 1.1 mycroft * NB: only need to add dquot's for inodes being modified.
472 1.1 mycroft */
473 1.53 ad mutex_enter(&mntvnode_lock);
474 1.1 mycroft again:
475 1.53 ad for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
476 1.53 ad vmark(mvp, vp);
477 1.53 ad mutex_enter(&vp->v_interlock);
478 1.65 bouyer if (VTOI(vp) == NULL || vp->v_mount != mp || vismarker(vp) ||
479 1.58 ad vp->v_type == VNON || vp->v_writecount == 0 ||
480 1.65 bouyer (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0) {
481 1.53 ad mutex_exit(&vp->v_interlock);
482 1.1 mycroft continue;
483 1.53 ad }
484 1.53 ad mutex_exit(&mntvnode_lock);
485 1.67 hannken if (vget(vp, LK_EXCLUSIVE)) {
486 1.53 ad mutex_enter(&mntvnode_lock);
487 1.53 ad (void)vunmark(mvp);
488 1.1 mycroft goto again;
489 1.53 ad }
490 1.8 christos if ((error = getinoquota(VTOI(vp))) != 0) {
491 1.1 mycroft vput(vp);
492 1.53 ad mutex_enter(&mntvnode_lock);
493 1.53 ad (void)vunmark(mvp);
494 1.1 mycroft break;
495 1.1 mycroft }
496 1.1 mycroft vput(vp);
497 1.56 christos mutex_enter(&mntvnode_lock);
498 1.1 mycroft }
499 1.53 ad mutex_exit(&mntvnode_lock);
500 1.54 pooka vnfree(mvp);
501 1.53 ad out:
502 1.50 hannken mutex_enter(&dqlock);
503 1.1 mycroft ump->um_qflags[type] &= ~QTF_OPENING;
504 1.50 hannken cv_broadcast(&dqcv);
505 1.50 hannken mutex_exit(&dqlock);
506 1.1 mycroft if (error)
507 1.36 christos quotaoff(l, mp, type);
508 1.1 mycroft return (error);
509 1.1 mycroft }
510 1.1 mycroft
511 1.1 mycroft /*
512 1.1 mycroft * Q_QUOTAOFF - turn off disk quotas for a filesystem.
513 1.1 mycroft */
514 1.1 mycroft int
515 1.36 christos quotaoff(struct lwp *l, struct mount *mp, int type)
516 1.1 mycroft {
517 1.11 fvdl struct vnode *vp;
518 1.53 ad struct vnode *qvp, *mvp;
519 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
520 1.11 fvdl struct dquot *dq;
521 1.11 fvdl struct inode *ip;
522 1.50 hannken kauth_cred_t cred;
523 1.50 hannken int i, error;
524 1.33 perry
525 1.53 ad /* Allocate a marker vnode. */
526 1.54 pooka if ((mvp = vnalloc(mp)) == NULL)
527 1.53 ad return ENOMEM;
528 1.53 ad
529 1.50 hannken mutex_enter(&dqlock);
530 1.50 hannken while ((ump->um_qflags[type] & (QTF_CLOSING | QTF_OPENING)) != 0)
531 1.50 hannken cv_wait(&dqcv, &dqlock);
532 1.50 hannken if ((qvp = ump->um_quotas[type]) == NULLVP) {
533 1.50 hannken mutex_exit(&dqlock);
534 1.54 pooka vnfree(mvp);
535 1.1 mycroft return (0);
536 1.50 hannken }
537 1.1 mycroft ump->um_qflags[type] |= QTF_CLOSING;
538 1.50 hannken mutex_exit(&dqlock);
539 1.1 mycroft /*
540 1.1 mycroft * Search vnodes associated with this mount point,
541 1.1 mycroft * deleting any references to quota file being closed.
542 1.1 mycroft */
543 1.53 ad mutex_enter(&mntvnode_lock);
544 1.1 mycroft again:
545 1.53 ad for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
546 1.53 ad vmark(mvp, vp);
547 1.53 ad mutex_enter(&vp->v_interlock);
548 1.65 bouyer if (VTOI(vp) == NULL || vp->v_mount != mp || vismarker(vp) ||
549 1.65 bouyer vp->v_type == VNON ||
550 1.65 bouyer (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0) {
551 1.53 ad mutex_exit(&vp->v_interlock);
552 1.13 fvdl continue;
553 1.53 ad }
554 1.53 ad mutex_exit(&mntvnode_lock);
555 1.67 hannken if (vget(vp, LK_EXCLUSIVE)) {
556 1.53 ad mutex_enter(&mntvnode_lock);
557 1.53 ad (void)vunmark(mvp);
558 1.1 mycroft goto again;
559 1.53 ad }
560 1.1 mycroft ip = VTOI(vp);
561 1.1 mycroft dq = ip->i_dquot[type];
562 1.1 mycroft ip->i_dquot[type] = NODQUOT;
563 1.1 mycroft dqrele(vp, dq);
564 1.1 mycroft vput(vp);
565 1.53 ad mutex_enter(&mntvnode_lock);
566 1.1 mycroft }
567 1.53 ad mutex_exit(&mntvnode_lock);
568 1.50 hannken #ifdef DIAGNOSTIC
569 1.1 mycroft dqflush(qvp);
570 1.50 hannken #endif
571 1.51 ad qvp->v_vflag &= ~VV_SYSTEM;
572 1.59 ad error = vn_close(qvp, FREAD|FWRITE, l->l_cred);
573 1.50 hannken mutex_enter(&dqlock);
574 1.1 mycroft ump->um_quotas[type] = NULLVP;
575 1.50 hannken cred = ump->um_cred[type];
576 1.1 mycroft ump->um_cred[type] = NOCRED;
577 1.50 hannken for (i = 0; i < MAXQUOTAS; i++)
578 1.50 hannken if (ump->um_quotas[i] != NULLVP)
579 1.50 hannken break;
580 1.1 mycroft ump->um_qflags[type] &= ~QTF_CLOSING;
581 1.50 hannken cv_broadcast(&dqcv);
582 1.50 hannken mutex_exit(&dqlock);
583 1.50 hannken kauth_cred_free(cred);
584 1.50 hannken if (i == MAXQUOTAS)
585 1.1 mycroft mp->mnt_flag &= ~MNT_QUOTA;
586 1.1 mycroft return (error);
587 1.1 mycroft }
588 1.1 mycroft
589 1.1 mycroft /*
590 1.1 mycroft * Q_GETQUOTA - return current values in a dqblk structure.
591 1.1 mycroft */
592 1.1 mycroft int
593 1.44 christos getquota(struct mount *mp, u_long id, int type, void *addr)
594 1.1 mycroft {
595 1.1 mycroft struct dquot *dq;
596 1.1 mycroft int error;
597 1.1 mycroft
598 1.8 christos if ((error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq)) != 0)
599 1.1 mycroft return (error);
600 1.44 christos error = copyout((void *)&dq->dq_dqb, addr, sizeof (struct dqblk));
601 1.1 mycroft dqrele(NULLVP, dq);
602 1.1 mycroft return (error);
603 1.1 mycroft }
604 1.1 mycroft
605 1.1 mycroft /*
606 1.1 mycroft * Q_SETQUOTA - assign an entire dqblk structure.
607 1.1 mycroft */
608 1.1 mycroft int
609 1.44 christos setquota(struct mount *mp, u_long id, int type, void *addr)
610 1.1 mycroft {
611 1.15 augustss struct dquot *dq;
612 1.1 mycroft struct dquot *ndq;
613 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
614 1.1 mycroft struct dqblk newlim;
615 1.1 mycroft int error;
616 1.1 mycroft
617 1.44 christos error = copyin(addr, (void *)&newlim, sizeof (struct dqblk));
618 1.8 christos if (error)
619 1.1 mycroft return (error);
620 1.8 christos if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
621 1.1 mycroft return (error);
622 1.1 mycroft dq = ndq;
623 1.49 hannken mutex_enter(&dq->dq_interlock);
624 1.1 mycroft /*
625 1.1 mycroft * Copy all but the current values.
626 1.1 mycroft * Reset time limit if previously had no soft limit or were
627 1.1 mycroft * under it, but now have a soft limit and are over it.
628 1.1 mycroft */
629 1.1 mycroft newlim.dqb_curblocks = dq->dq_curblocks;
630 1.1 mycroft newlim.dqb_curinodes = dq->dq_curinodes;
631 1.1 mycroft if (dq->dq_id != 0) {
632 1.1 mycroft newlim.dqb_btime = dq->dq_btime;
633 1.1 mycroft newlim.dqb_itime = dq->dq_itime;
634 1.1 mycroft }
635 1.1 mycroft if (newlim.dqb_bsoftlimit &&
636 1.1 mycroft dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
637 1.1 mycroft (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
638 1.40 kardel newlim.dqb_btime = time_second + ump->um_btime[type];
639 1.1 mycroft if (newlim.dqb_isoftlimit &&
640 1.1 mycroft dq->dq_curinodes >= newlim.dqb_isoftlimit &&
641 1.1 mycroft (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
642 1.40 kardel newlim.dqb_itime = time_second + ump->um_itime[type];
643 1.1 mycroft dq->dq_dqb = newlim;
644 1.1 mycroft if (dq->dq_curblocks < dq->dq_bsoftlimit)
645 1.1 mycroft dq->dq_flags &= ~DQ_BLKS;
646 1.1 mycroft if (dq->dq_curinodes < dq->dq_isoftlimit)
647 1.1 mycroft dq->dq_flags &= ~DQ_INODS;
648 1.1 mycroft if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
649 1.1 mycroft dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
650 1.1 mycroft dq->dq_flags |= DQ_FAKE;
651 1.1 mycroft else
652 1.1 mycroft dq->dq_flags &= ~DQ_FAKE;
653 1.1 mycroft dq->dq_flags |= DQ_MOD;
654 1.49 hannken mutex_exit(&dq->dq_interlock);
655 1.1 mycroft dqrele(NULLVP, dq);
656 1.1 mycroft return (0);
657 1.1 mycroft }
658 1.1 mycroft
659 1.1 mycroft /*
660 1.1 mycroft * Q_SETUSE - set current inode and block usage.
661 1.1 mycroft */
662 1.1 mycroft int
663 1.44 christos setuse(struct mount *mp, u_long id, int type, void *addr)
664 1.1 mycroft {
665 1.15 augustss struct dquot *dq;
666 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
667 1.1 mycroft struct dquot *ndq;
668 1.1 mycroft struct dqblk usage;
669 1.1 mycroft int error;
670 1.1 mycroft
671 1.44 christos error = copyin(addr, (void *)&usage, sizeof (struct dqblk));
672 1.8 christos if (error)
673 1.1 mycroft return (error);
674 1.8 christos if ((error = dqget(NULLVP, id, ump, type, &ndq)) != 0)
675 1.1 mycroft return (error);
676 1.1 mycroft dq = ndq;
677 1.49 hannken mutex_enter(&dq->dq_interlock);
678 1.1 mycroft /*
679 1.1 mycroft * Reset time limit if have a soft limit and were
680 1.1 mycroft * previously under it, but are now over it.
681 1.1 mycroft */
682 1.1 mycroft if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
683 1.1 mycroft usage.dqb_curblocks >= dq->dq_bsoftlimit)
684 1.40 kardel dq->dq_btime = time_second + ump->um_btime[type];
685 1.1 mycroft if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
686 1.1 mycroft usage.dqb_curinodes >= dq->dq_isoftlimit)
687 1.40 kardel dq->dq_itime = time_second + ump->um_itime[type];
688 1.1 mycroft dq->dq_curblocks = usage.dqb_curblocks;
689 1.1 mycroft dq->dq_curinodes = usage.dqb_curinodes;
690 1.1 mycroft if (dq->dq_curblocks < dq->dq_bsoftlimit)
691 1.1 mycroft dq->dq_flags &= ~DQ_BLKS;
692 1.1 mycroft if (dq->dq_curinodes < dq->dq_isoftlimit)
693 1.1 mycroft dq->dq_flags &= ~DQ_INODS;
694 1.1 mycroft dq->dq_flags |= DQ_MOD;
695 1.49 hannken mutex_exit(&dq->dq_interlock);
696 1.1 mycroft dqrele(NULLVP, dq);
697 1.1 mycroft return (0);
698 1.1 mycroft }
699 1.1 mycroft
700 1.1 mycroft /*
701 1.1 mycroft * Q_SYNC - sync quota files to disk.
702 1.1 mycroft */
703 1.1 mycroft int
704 1.35 thorpej qsync(struct mount *mp)
705 1.1 mycroft {
706 1.1 mycroft struct ufsmount *ump = VFSTOUFS(mp);
707 1.53 ad struct vnode *vp, *mvp;
708 1.11 fvdl struct dquot *dq;
709 1.11 fvdl int i, error;
710 1.1 mycroft
711 1.1 mycroft /*
712 1.1 mycroft * Check if the mount point has any quotas.
713 1.1 mycroft * If not, simply return.
714 1.1 mycroft */
715 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++)
716 1.1 mycroft if (ump->um_quotas[i] != NULLVP)
717 1.1 mycroft break;
718 1.1 mycroft if (i == MAXQUOTAS)
719 1.1 mycroft return (0);
720 1.53 ad
721 1.53 ad /* Allocate a marker vnode. */
722 1.54 pooka if ((mvp = vnalloc(mp)) == NULL)
723 1.53 ad return (ENOMEM);
724 1.53 ad
725 1.1 mycroft /*
726 1.1 mycroft * Search vnodes associated with this mount point,
727 1.1 mycroft * synchronizing any modified dquot structures.
728 1.1 mycroft */
729 1.53 ad mutex_enter(&mntvnode_lock);
730 1.53 ad again:
731 1.53 ad for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) {
732 1.53 ad vmark(mvp, vp);
733 1.53 ad mutex_enter(&vp->v_interlock);
734 1.65 bouyer if (VTOI(vp) == NULL || vp->v_mount != mp || vismarker(vp) ||
735 1.65 bouyer vp->v_type == VNON ||
736 1.65 bouyer (vp->v_iflag & (VI_XLOCK | VI_CLEAN)) != 0) {
737 1.53 ad mutex_exit(&vp->v_interlock);
738 1.13 fvdl continue;
739 1.53 ad }
740 1.53 ad mutex_exit(&mntvnode_lock);
741 1.67 hannken error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT);
742 1.11 fvdl if (error) {
743 1.53 ad mutex_enter(&mntvnode_lock);
744 1.53 ad if (error == ENOENT) {
745 1.53 ad (void)vunmark(mvp);
746 1.11 fvdl goto again;
747 1.53 ad }
748 1.1 mycroft continue;
749 1.11 fvdl }
750 1.1 mycroft for (i = 0; i < MAXQUOTAS; i++) {
751 1.1 mycroft dq = VTOI(vp)->i_dquot[i];
752 1.50 hannken if (dq == NODQUOT)
753 1.50 hannken continue;
754 1.50 hannken mutex_enter(&dq->dq_interlock);
755 1.50 hannken if (dq->dq_flags & DQ_MOD)
756 1.1 mycroft dqsync(vp, dq);
757 1.50 hannken mutex_exit(&dq->dq_interlock);
758 1.1 mycroft }
759 1.1 mycroft vput(vp);
760 1.53 ad mutex_enter(&mntvnode_lock);
761 1.1 mycroft }
762 1.53 ad mutex_exit(&mntvnode_lock);
763 1.54 pooka vnfree(mvp);
764 1.1 mycroft return (0);
765 1.1 mycroft }
766 1.1 mycroft
767 1.1 mycroft /*
768 1.1 mycroft * Code pertaining to management of the in-core dquot data structures.
769 1.1 mycroft */
770 1.5 mycroft #define DQHASH(dqvp, id) \
771 1.21 chs (((((long)(dqvp)) >> 8) + id) & dqhash)
772 1.35 thorpej static LIST_HEAD(dqhashhead, dquot) *dqhashtbl;
773 1.35 thorpej static u_long dqhash;
774 1.55 ad static pool_cache_t dquot_cache;
775 1.23 thorpej
776 1.1 mycroft /*
777 1.1 mycroft * Initialize the quota system.
778 1.1 mycroft */
779 1.1 mycroft void
780 1.35 thorpej dqinit(void)
781 1.1 mycroft {
782 1.47 pooka
783 1.50 hannken mutex_init(&dqlock, MUTEX_DEFAULT, IPL_NONE);
784 1.50 hannken cv_init(&dqcv, "quota");
785 1.60 ad dqhashtbl = hashinit(desiredvnodes, HASH_LIST, true, &dqhash);
786 1.55 ad dquot_cache = pool_cache_init(sizeof(struct dquot), 0, 0, 0, "ufsdq",
787 1.55 ad NULL, IPL_NONE, NULL, NULL, NULL);
788 1.14 jdolecek }
789 1.14 jdolecek
790 1.21 chs void
791 1.35 thorpej dqreinit(void)
792 1.21 chs {
793 1.21 chs struct dquot *dq;
794 1.21 chs struct dqhashhead *oldhash, *hash;
795 1.21 chs struct vnode *dqvp;
796 1.21 chs u_long oldmask, mask, hashval;
797 1.21 chs int i;
798 1.21 chs
799 1.60 ad hash = hashinit(desiredvnodes, HASH_LIST, true, &mask);
800 1.50 hannken mutex_enter(&dqlock);
801 1.21 chs oldhash = dqhashtbl;
802 1.21 chs oldmask = dqhash;
803 1.21 chs dqhashtbl = hash;
804 1.21 chs dqhash = mask;
805 1.21 chs for (i = 0; i <= oldmask; i++) {
806 1.21 chs while ((dq = LIST_FIRST(&oldhash[i])) != NULL) {
807 1.21 chs dqvp = dq->dq_ump->um_quotas[dq->dq_type];
808 1.21 chs LIST_REMOVE(dq, dq_hash);
809 1.21 chs hashval = DQHASH(dqvp, dq->dq_id);
810 1.21 chs LIST_INSERT_HEAD(&dqhashtbl[hashval], dq, dq_hash);
811 1.21 chs }
812 1.21 chs }
813 1.50 hannken mutex_exit(&dqlock);
814 1.60 ad hashdone(oldhash, HASH_LIST, oldmask);
815 1.21 chs }
816 1.21 chs
817 1.14 jdolecek /*
818 1.14 jdolecek * Free resources held by quota system.
819 1.14 jdolecek */
820 1.14 jdolecek void
821 1.35 thorpej dqdone(void)
822 1.14 jdolecek {
823 1.47 pooka
824 1.55 ad pool_cache_destroy(dquot_cache);
825 1.60 ad hashdone(dqhashtbl, HASH_LIST, dqhash);
826 1.50 hannken cv_destroy(&dqcv);
827 1.50 hannken mutex_destroy(&dqlock);
828 1.1 mycroft }
829 1.1 mycroft
830 1.1 mycroft /*
831 1.1 mycroft * Obtain a dquot structure for the specified identifier and quota file
832 1.1 mycroft * reading the information from the file if necessary.
833 1.1 mycroft */
834 1.48 hannken static int
835 1.35 thorpej dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
836 1.35 thorpej struct dquot **dqp)
837 1.1 mycroft {
838 1.50 hannken struct dquot *dq, *ndq;
839 1.21 chs struct dqhashhead *dqh;
840 1.11 fvdl struct vnode *dqvp;
841 1.1 mycroft struct iovec aiov;
842 1.1 mycroft struct uio auio;
843 1.1 mycroft int error;
844 1.1 mycroft
845 1.50 hannken /* Lock to see an up to date value for QTF_CLOSING. */
846 1.50 hannken mutex_enter(&dqlock);
847 1.1 mycroft dqvp = ump->um_quotas[type];
848 1.1 mycroft if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
849 1.50 hannken mutex_exit(&dqlock);
850 1.1 mycroft *dqp = NODQUOT;
851 1.1 mycroft return (EINVAL);
852 1.1 mycroft }
853 1.50 hannken KASSERT(dqvp != vp);
854 1.1 mycroft /*
855 1.1 mycroft * Check the cache first.
856 1.1 mycroft */
857 1.21 chs dqh = &dqhashtbl[DQHASH(dqvp, id)];
858 1.21 chs LIST_FOREACH(dq, dqh, dq_hash) {
859 1.1 mycroft if (dq->dq_id != id ||
860 1.1 mycroft dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
861 1.1 mycroft continue;
862 1.50 hannken KASSERT(dq->dq_cnt > 0);
863 1.37 chs dqref(dq);
864 1.50 hannken mutex_exit(&dqlock);
865 1.1 mycroft *dqp = dq;
866 1.1 mycroft return (0);
867 1.1 mycroft }
868 1.1 mycroft /*
869 1.1 mycroft * Not in cache, allocate a new one.
870 1.1 mycroft */
871 1.50 hannken mutex_exit(&dqlock);
872 1.55 ad ndq = pool_cache_get(dquot_cache, PR_WAITOK);
873 1.1 mycroft /*
874 1.1 mycroft * Initialize the contents of the dquot structure.
875 1.1 mycroft */
876 1.50 hannken memset((char *)ndq, 0, sizeof *ndq);
877 1.50 hannken ndq->dq_flags = 0;
878 1.50 hannken ndq->dq_id = id;
879 1.50 hannken ndq->dq_ump = ump;
880 1.50 hannken ndq->dq_type = type;
881 1.50 hannken mutex_init(&ndq->dq_interlock, MUTEX_DEFAULT, IPL_NONE);
882 1.50 hannken mutex_enter(&dqlock);
883 1.49 hannken dqh = &dqhashtbl[DQHASH(dqvp, id)];
884 1.50 hannken LIST_FOREACH(dq, dqh, dq_hash) {
885 1.50 hannken if (dq->dq_id != id ||
886 1.50 hannken dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
887 1.50 hannken continue;
888 1.50 hannken /*
889 1.50 hannken * Another thread beat us allocating this dquot.
890 1.50 hannken */
891 1.50 hannken KASSERT(dq->dq_cnt > 0);
892 1.50 hannken dqref(dq);
893 1.50 hannken mutex_exit(&dqlock);
894 1.64 bouyer mutex_destroy(&ndq->dq_interlock);
895 1.55 ad pool_cache_put(dquot_cache, ndq);
896 1.50 hannken *dqp = dq;
897 1.50 hannken return 0;
898 1.50 hannken }
899 1.50 hannken dq = ndq;
900 1.5 mycroft LIST_INSERT_HEAD(dqh, dq, dq_hash);
901 1.37 chs dqref(dq);
902 1.50 hannken mutex_enter(&dq->dq_interlock);
903 1.50 hannken mutex_exit(&dqlock);
904 1.50 hannken vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
905 1.1 mycroft auio.uio_iov = &aiov;
906 1.1 mycroft auio.uio_iovcnt = 1;
907 1.44 christos aiov.iov_base = (void *)&dq->dq_dqb;
908 1.1 mycroft aiov.iov_len = sizeof (struct dqblk);
909 1.1 mycroft auio.uio_resid = sizeof (struct dqblk);
910 1.1 mycroft auio.uio_offset = (off_t)(id * sizeof (struct dqblk));
911 1.1 mycroft auio.uio_rw = UIO_READ;
912 1.38 yamt UIO_SETUP_SYSSPACE(&auio);
913 1.1 mycroft error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
914 1.1 mycroft if (auio.uio_resid == sizeof(struct dqblk) && error == 0)
915 1.44 christos memset((void *)&dq->dq_dqb, 0, sizeof(struct dqblk));
916 1.66 hannken VOP_UNLOCK(dqvp);
917 1.1 mycroft /*
918 1.1 mycroft * I/O error in reading quota file, release
919 1.1 mycroft * quota structure and reflect problem to caller.
920 1.1 mycroft */
921 1.1 mycroft if (error) {
922 1.50 hannken mutex_enter(&dqlock);
923 1.5 mycroft LIST_REMOVE(dq, dq_hash);
924 1.50 hannken mutex_exit(&dqlock);
925 1.49 hannken mutex_exit(&dq->dq_interlock);
926 1.1 mycroft dqrele(vp, dq);
927 1.1 mycroft *dqp = NODQUOT;
928 1.1 mycroft return (error);
929 1.1 mycroft }
930 1.1 mycroft /*
931 1.1 mycroft * Check for no limit to enforce.
932 1.1 mycroft * Initialize time values if necessary.
933 1.1 mycroft */
934 1.1 mycroft if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
935 1.1 mycroft dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
936 1.1 mycroft dq->dq_flags |= DQ_FAKE;
937 1.1 mycroft if (dq->dq_id != 0) {
938 1.1 mycroft if (dq->dq_btime == 0)
939 1.40 kardel dq->dq_btime = time_second + ump->um_btime[type];
940 1.1 mycroft if (dq->dq_itime == 0)
941 1.40 kardel dq->dq_itime = time_second + ump->um_itime[type];
942 1.1 mycroft }
943 1.49 hannken mutex_exit(&dq->dq_interlock);
944 1.1 mycroft *dqp = dq;
945 1.1 mycroft return (0);
946 1.1 mycroft }
947 1.1 mycroft
948 1.1 mycroft /*
949 1.1 mycroft * Obtain a reference to a dquot.
950 1.1 mycroft */
951 1.48 hannken static void
952 1.35 thorpej dqref(struct dquot *dq)
953 1.1 mycroft {
954 1.1 mycroft
955 1.50 hannken KASSERT(mutex_owned(&dqlock));
956 1.1 mycroft dq->dq_cnt++;
957 1.46 hannken KASSERT(dq->dq_cnt > 0);
958 1.1 mycroft }
959 1.1 mycroft
960 1.1 mycroft /*
961 1.1 mycroft * Release a reference to a dquot.
962 1.1 mycroft */
963 1.48 hannken static void
964 1.35 thorpej dqrele(struct vnode *vp, struct dquot *dq)
965 1.1 mycroft {
966 1.1 mycroft
967 1.1 mycroft if (dq == NODQUOT)
968 1.1 mycroft return;
969 1.50 hannken mutex_enter(&dq->dq_interlock);
970 1.50 hannken for (;;) {
971 1.50 hannken mutex_enter(&dqlock);
972 1.50 hannken if (dq->dq_cnt > 1) {
973 1.50 hannken dq->dq_cnt--;
974 1.50 hannken mutex_exit(&dqlock);
975 1.50 hannken mutex_exit(&dq->dq_interlock);
976 1.50 hannken return;
977 1.50 hannken }
978 1.50 hannken if ((dq->dq_flags & DQ_MOD) == 0)
979 1.50 hannken break;
980 1.50 hannken mutex_exit(&dqlock);
981 1.50 hannken (void) dqsync(vp, dq);
982 1.1 mycroft }
983 1.50 hannken KASSERT(dq->dq_cnt == 1 && (dq->dq_flags & DQ_MOD) == 0);
984 1.50 hannken LIST_REMOVE(dq, dq_hash);
985 1.50 hannken mutex_exit(&dqlock);
986 1.50 hannken mutex_exit(&dq->dq_interlock);
987 1.50 hannken mutex_destroy(&dq->dq_interlock);
988 1.55 ad pool_cache_put(dquot_cache, dq);
989 1.1 mycroft }
990 1.1 mycroft
991 1.1 mycroft /*
992 1.1 mycroft * Update the disk quota in the quota file.
993 1.1 mycroft */
994 1.48 hannken static int
995 1.35 thorpej dqsync(struct vnode *vp, struct dquot *dq)
996 1.1 mycroft {
997 1.1 mycroft struct vnode *dqvp;
998 1.1 mycroft struct iovec aiov;
999 1.1 mycroft struct uio auio;
1000 1.1 mycroft int error;
1001 1.1 mycroft
1002 1.1 mycroft if (dq == NODQUOT)
1003 1.1 mycroft panic("dqsync: dquot");
1004 1.50 hannken KASSERT(mutex_owned(&dq->dq_interlock));
1005 1.1 mycroft if ((dq->dq_flags & DQ_MOD) == 0)
1006 1.1 mycroft return (0);
1007 1.1 mycroft if ((dqvp = dq->dq_ump->um_quotas[dq->dq_type]) == NULLVP)
1008 1.1 mycroft panic("dqsync: file");
1009 1.50 hannken KASSERT(dqvp != vp);
1010 1.50 hannken vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
1011 1.1 mycroft auio.uio_iov = &aiov;
1012 1.1 mycroft auio.uio_iovcnt = 1;
1013 1.44 christos aiov.iov_base = (void *)&dq->dq_dqb;
1014 1.1 mycroft aiov.iov_len = sizeof (struct dqblk);
1015 1.1 mycroft auio.uio_resid = sizeof (struct dqblk);
1016 1.1 mycroft auio.uio_offset = (off_t)(dq->dq_id * sizeof (struct dqblk));
1017 1.1 mycroft auio.uio_rw = UIO_WRITE;
1018 1.38 yamt UIO_SETUP_SYSSPACE(&auio);
1019 1.1 mycroft error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1020 1.1 mycroft if (auio.uio_resid && error == 0)
1021 1.1 mycroft error = EIO;
1022 1.49 hannken dq->dq_flags &= ~DQ_MOD;
1023 1.66 hannken VOP_UNLOCK(dqvp);
1024 1.1 mycroft return (error);
1025 1.1 mycroft }
1026 1.1 mycroft
1027 1.50 hannken #ifdef DIAGNOSTIC
1028 1.1 mycroft /*
1029 1.50 hannken * Check the hash chains for stray dquot's.
1030 1.1 mycroft */
1031 1.48 hannken static void
1032 1.35 thorpej dqflush(struct vnode *vp)
1033 1.1 mycroft {
1034 1.50 hannken struct dquot *dq;
1035 1.50 hannken int i;
1036 1.1 mycroft
1037 1.50 hannken mutex_enter(&dqlock);
1038 1.50 hannken for (i = 0; i <= dqhash; i++)
1039 1.50 hannken LIST_FOREACH(dq, &dqhashtbl[i], dq_hash)
1040 1.50 hannken KASSERT(dq->dq_ump->um_quotas[dq->dq_type] != vp);
1041 1.50 hannken mutex_exit(&dqlock);
1042 1.1 mycroft }
1043 1.50 hannken #endif
1044