1 /*-
2 * Copyright (c) 1982, 1986, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)ufs_quota.c 8.5 (Berkeley) 5/20/95
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include "opt_ffs.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/endian.h>
43 #include <sys/fcntl.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/priv.h>
51 #include <sys/proc.h>
52 #include <sys/socket.h>
53 #include <sys/stat.h>
54 #include <sys/sysctl.h>
55 #include <sys/vnode.h>
56
57 #include <ufs/ufs/extattr.h>
58 #include <ufs/ufs/quota.h>
59 #include <ufs/ufs/inode.h>
60 #include <ufs/ufs/ufsmount.h>
61 #include <ufs/ufs/ufs_extern.h>
62
63 CTASSERT(sizeof(struct dqblk64) == sizeof(struct dqhdr64));
64
65 static int unprivileged_get_quota = 0;
66 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_get_quota, CTLFLAG_RW,
67 &unprivileged_get_quota, 0,
68 "Unprivileged processes may retrieve quotas for other uids and gids");
69
70 static MALLOC_DEFINE(M_DQUOT, "ufs_quota", "UFS quota entries");
71
72 /*
73 * Quota name to error message mapping.
74 */
75 static char *quotatypes[] = INITQFNAMES;
76
77 static int chkdqchg(struct inode *, ufs2_daddr_t, struct ucred *, int, int *);
78 static int chkiqchg(struct inode *, int, struct ucred *, int, int *);
79 static int dqopen(struct vnode *, struct ufsmount *, int);
80 static int dqget(struct vnode *,
81 u_long, struct ufsmount *, int, struct dquot **);
82 static int dqsync(struct vnode *, struct dquot *);
83 static int dqflush(struct vnode *);
84 static int quotaoff1(struct thread *td, struct mount *mp, int type);
85 static int quotaoff_inchange(struct thread *td, struct mount *mp, int type);
86
87 /* conversion functions - from_to() */
88 static void dqb32_dq(const struct dqblk32 *, struct dquot *);
89 static void dqb64_dq(const struct dqblk64 *, struct dquot *);
90 static void dq_dqb32(const struct dquot *, struct dqblk32 *);
91 static void dq_dqb64(const struct dquot *, struct dqblk64 *);
92 static void dqb32_dqb64(const struct dqblk32 *, struct dqblk64 *);
93 static void dqb64_dqb32(const struct dqblk64 *, struct dqblk32 *);
94
95 #ifdef DIAGNOSTIC
96 static void dqref(struct dquot *);
97 static void chkdquot(struct inode *);
98 #endif
99
100 /*
101 * Set up the quotas for an inode.
102 *
103 * This routine completely defines the semantics of quotas.
104 * If other criterion want to be used to establish quotas, the
105 * MAXQUOTAS value in quota.h should be increased, and the
106 * additional dquots set up here.
107 */
108 int
getinoquota(struct inode * ip)109 getinoquota(struct inode *ip)
110 {
111 struct ufsmount *ump;
112 struct vnode *vp;
113 int error;
114
115 vp = ITOV(ip);
116
117 /*
118 * Disk quotas must be turned off for system files. Currently
119 * snapshot and quota files.
120 */
121 if ((vp->v_vflag & VV_SYSTEM) != 0)
122 return (0);
123 /*
124 * XXX: Turn off quotas for files with a negative UID or GID.
125 * This prevents the creation of 100GB+ quota files.
126 */
127 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
128 return (0);
129 ump = VFSTOUFS(vp->v_mount);
130 /*
131 * Set up the user quota based on file uid.
132 * EINVAL means that quotas are not enabled.
133 */
134 if ((error =
135 dqget(vp, ip->i_uid, ump, USRQUOTA, &ip->i_dquot[USRQUOTA])) &&
136 error != EINVAL)
137 return (error);
138 /*
139 * Set up the group quota based on file gid.
140 * EINVAL means that quotas are not enabled.
141 */
142 if ((error =
143 dqget(vp, ip->i_gid, ump, GRPQUOTA, &ip->i_dquot[GRPQUOTA])) &&
144 error != EINVAL)
145 return (error);
146 return (0);
147 }
148
149 /*
150 * Update disk usage, and take corrective action.
151 */
152 int
chkdq(struct inode * ip,ufs2_daddr_t change,struct ucred * cred,int flags)153 chkdq(struct inode *ip, ufs2_daddr_t change, struct ucred *cred, int flags)
154 {
155 struct dquot *dq;
156 ufs2_daddr_t ncurblocks;
157 struct vnode *vp = ITOV(ip);
158 int i, error, warn, do_check;
159
160 /*
161 * Disk quotas must be turned off for system files. Currently
162 * snapshot and quota files.
163 */
164 if ((vp->v_vflag & VV_SYSTEM) != 0)
165 return (0);
166 /*
167 * XXX: Turn off quotas for files with a negative UID or GID.
168 * This prevents the creation of 100GB+ quota files.
169 */
170 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
171 return (0);
172 #ifdef DIAGNOSTIC
173 if ((flags & CHOWN) == 0)
174 chkdquot(ip);
175 #endif
176 if (change == 0)
177 return (0);
178 if (change < 0) {
179 for (i = 0; i < MAXQUOTAS; i++) {
180 if ((dq = ip->i_dquot[i]) == NODQUOT)
181 continue;
182 DQI_LOCK(dq);
183 DQI_WAIT(dq, PINOD+1, "chkdq1");
184 ncurblocks = dq->dq_curblocks + change;
185 if (ncurblocks >= 0)
186 dq->dq_curblocks = ncurblocks;
187 else
188 dq->dq_curblocks = 0;
189 dq->dq_flags &= ~DQ_BLKS;
190 dq->dq_flags |= DQ_MOD;
191 DQI_UNLOCK(dq);
192 }
193 return (0);
194 }
195 if ((flags & FORCE) == 0 &&
196 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
197 do_check = 1;
198 else
199 do_check = 0;
200 for (i = 0; i < MAXQUOTAS; i++) {
201 if ((dq = ip->i_dquot[i]) == NODQUOT)
202 continue;
203 warn = 0;
204 DQI_LOCK(dq);
205 DQI_WAIT(dq, PINOD+1, "chkdq2");
206 if (do_check) {
207 error = chkdqchg(ip, change, cred, i, &warn);
208 if (error) {
209 /*
210 * Roll back user quota changes when
211 * group quota failed.
212 */
213 while (i > 0) {
214 --i;
215 dq = ip->i_dquot[i];
216 if (dq == NODQUOT)
217 continue;
218 DQI_LOCK(dq);
219 DQI_WAIT(dq, PINOD+1, "chkdq3");
220 ncurblocks = dq->dq_curblocks - change;
221 if (ncurblocks >= 0)
222 dq->dq_curblocks = ncurblocks;
223 else
224 dq->dq_curblocks = 0;
225 dq->dq_flags &= ~DQ_BLKS;
226 dq->dq_flags |= DQ_MOD;
227 DQI_UNLOCK(dq);
228 }
229 return (error);
230 }
231 }
232 /* Reset timer when crossing soft limit */
233 if (dq->dq_curblocks + change >= dq->dq_bsoftlimit &&
234 dq->dq_curblocks < dq->dq_bsoftlimit)
235 dq->dq_btime = time_second + ITOUMP(ip)->um_btime[i];
236 dq->dq_curblocks += change;
237 dq->dq_flags |= DQ_MOD;
238 DQI_UNLOCK(dq);
239 if (warn)
240 uprintf("\n%s: warning, %s disk quota exceeded\n",
241 ITOVFS(ip)->mnt_stat.f_mntonname,
242 quotatypes[i]);
243 }
244 return (0);
245 }
246
247 /*
248 * Check for a valid change to a users allocation.
249 * Issue an error message if appropriate.
250 */
251 static int
chkdqchg(struct inode * ip,ufs2_daddr_t change,struct ucred * cred,int type,int * warn)252 chkdqchg(struct inode *ip, ufs2_daddr_t change, struct ucred *cred,
253 int type, int *warn)
254 {
255 struct dquot *dq = ip->i_dquot[type];
256 ufs2_daddr_t ncurblocks = dq->dq_curblocks + change;
257
258 /*
259 * If user would exceed their hard limit, disallow space allocation.
260 */
261 if (ncurblocks >= dq->dq_bhardlimit && dq->dq_bhardlimit) {
262 if ((dq->dq_flags & DQ_BLKS) == 0 &&
263 ip->i_uid == cred->cr_uid) {
264 dq->dq_flags |= DQ_BLKS;
265 DQI_UNLOCK(dq);
266 uprintf("\n%s: write failed, %s disk limit reached\n",
267 ITOVFS(ip)->mnt_stat.f_mntonname,
268 quotatypes[type]);
269 return (EDQUOT);
270 }
271 DQI_UNLOCK(dq);
272 return (EDQUOT);
273 }
274 /*
275 * If user is over their soft limit for too long, disallow space
276 * allocation. Reset time limit as they cross their soft limit.
277 */
278 if (ncurblocks >= dq->dq_bsoftlimit && dq->dq_bsoftlimit) {
279 if (dq->dq_curblocks < dq->dq_bsoftlimit) {
280 dq->dq_btime = time_second + ITOUMP(ip)->um_btime[type];
281 if (ip->i_uid == cred->cr_uid)
282 *warn = 1;
283 return (0);
284 }
285 if (time_second > dq->dq_btime) {
286 if ((dq->dq_flags & DQ_BLKS) == 0 &&
287 ip->i_uid == cred->cr_uid) {
288 dq->dq_flags |= DQ_BLKS;
289 DQI_UNLOCK(dq);
290 uprintf("\n%s: write failed, %s "
291 "disk quota exceeded for too long\n",
292 ITOVFS(ip)->mnt_stat.f_mntonname,
293 quotatypes[type]);
294 return (EDQUOT);
295 }
296 DQI_UNLOCK(dq);
297 return (EDQUOT);
298 }
299 }
300 return (0);
301 }
302
303 /*
304 * Check the inode limit, applying corrective action.
305 */
306 int
chkiq(struct inode * ip,int change,struct ucred * cred,int flags)307 chkiq(struct inode *ip, int change, struct ucred *cred, int flags)
308 {
309 struct dquot *dq;
310 int i, error, warn, do_check;
311
312 #ifdef DIAGNOSTIC
313 if ((flags & CHOWN) == 0)
314 chkdquot(ip);
315 #endif
316 if (change == 0)
317 return (0);
318 if (change < 0) {
319 for (i = 0; i < MAXQUOTAS; i++) {
320 if ((dq = ip->i_dquot[i]) == NODQUOT)
321 continue;
322 DQI_LOCK(dq);
323 DQI_WAIT(dq, PINOD+1, "chkiq1");
324 if (dq->dq_curinodes >= -change)
325 dq->dq_curinodes += change;
326 else
327 dq->dq_curinodes = 0;
328 dq->dq_flags &= ~DQ_INODS;
329 dq->dq_flags |= DQ_MOD;
330 DQI_UNLOCK(dq);
331 }
332 return (0);
333 }
334 if ((flags & FORCE) == 0 &&
335 priv_check_cred(cred, PRIV_VFS_EXCEEDQUOTA, 0))
336 do_check = 1;
337 else
338 do_check = 0;
339 for (i = 0; i < MAXQUOTAS; i++) {
340 if ((dq = ip->i_dquot[i]) == NODQUOT)
341 continue;
342 warn = 0;
343 DQI_LOCK(dq);
344 DQI_WAIT(dq, PINOD+1, "chkiq2");
345 if (do_check) {
346 error = chkiqchg(ip, change, cred, i, &warn);
347 if (error) {
348 /*
349 * Roll back user quota changes when
350 * group quota failed.
351 */
352 while (i > 0) {
353 --i;
354 dq = ip->i_dquot[i];
355 if (dq == NODQUOT)
356 continue;
357 DQI_LOCK(dq);
358 DQI_WAIT(dq, PINOD+1, "chkiq3");
359 if (dq->dq_curinodes >= change)
360 dq->dq_curinodes -= change;
361 else
362 dq->dq_curinodes = 0;
363 dq->dq_flags &= ~DQ_INODS;
364 dq->dq_flags |= DQ_MOD;
365 DQI_UNLOCK(dq);
366 }
367 return (error);
368 }
369 }
370 /* Reset timer when crossing soft limit */
371 if (dq->dq_curinodes + change >= dq->dq_isoftlimit &&
372 dq->dq_curinodes < dq->dq_isoftlimit)
373 dq->dq_itime = time_second + ITOUMP(ip)->um_itime[i];
374 dq->dq_curinodes += change;
375 dq->dq_flags |= DQ_MOD;
376 DQI_UNLOCK(dq);
377 if (warn)
378 uprintf("\n%s: warning, %s inode quota exceeded\n",
379 ITOVFS(ip)->mnt_stat.f_mntonname,
380 quotatypes[i]);
381 }
382 return (0);
383 }
384
385 /*
386 * Check for a valid change to a users allocation.
387 * Issue an error message if appropriate.
388 */
389 static int
chkiqchg(struct inode * ip,int change,struct ucred * cred,int type,int * warn)390 chkiqchg(struct inode *ip, int change, struct ucred *cred, int type, int *warn)
391 {
392 struct dquot *dq = ip->i_dquot[type];
393 ino_t ncurinodes = dq->dq_curinodes + change;
394
395 /*
396 * If user would exceed their hard limit, disallow inode allocation.
397 */
398 if (ncurinodes >= dq->dq_ihardlimit && dq->dq_ihardlimit) {
399 if ((dq->dq_flags & DQ_INODS) == 0 &&
400 ip->i_uid == cred->cr_uid) {
401 dq->dq_flags |= DQ_INODS;
402 DQI_UNLOCK(dq);
403 uprintf("\n%s: write failed, %s inode limit reached\n",
404 ITOVFS(ip)->mnt_stat.f_mntonname,
405 quotatypes[type]);
406 return (EDQUOT);
407 }
408 DQI_UNLOCK(dq);
409 return (EDQUOT);
410 }
411 /*
412 * If user is over their soft limit for too long, disallow inode
413 * allocation. Reset time limit as they cross their soft limit.
414 */
415 if (ncurinodes >= dq->dq_isoftlimit && dq->dq_isoftlimit) {
416 if (dq->dq_curinodes < dq->dq_isoftlimit) {
417 dq->dq_itime = time_second + ITOUMP(ip)->um_itime[type];
418 if (ip->i_uid == cred->cr_uid)
419 *warn = 1;
420 return (0);
421 }
422 if (time_second > dq->dq_itime) {
423 if ((dq->dq_flags & DQ_INODS) == 0 &&
424 ip->i_uid == cred->cr_uid) {
425 dq->dq_flags |= DQ_INODS;
426 DQI_UNLOCK(dq);
427 uprintf("\n%s: write failed, %s "
428 "inode quota exceeded for too long\n",
429 ITOVFS(ip)->mnt_stat.f_mntonname,
430 quotatypes[type]);
431 return (EDQUOT);
432 }
433 DQI_UNLOCK(dq);
434 return (EDQUOT);
435 }
436 }
437 return (0);
438 }
439
440 #ifdef DIAGNOSTIC
441 /*
442 * On filesystems with quotas enabled, it is an error for a file to change
443 * size and not to have a dquot structure associated with it.
444 */
445 static void
chkdquot(struct inode * ip)446 chkdquot(struct inode *ip)
447 {
448 struct ufsmount *ump;
449 struct vnode *vp;
450 int i;
451
452 ump = ITOUMP(ip);
453 vp = ITOV(ip);
454
455 /*
456 * Disk quotas must be turned off for system files. Currently
457 * these are snapshots and quota files.
458 */
459 if ((vp->v_vflag & VV_SYSTEM) != 0)
460 return;
461 /*
462 * XXX: Turn off quotas for files with a negative UID or GID.
463 * This prevents the creation of 100GB+ quota files.
464 */
465 if ((int)ip->i_uid < 0 || (int)ip->i_gid < 0)
466 return;
467
468 UFS_LOCK(ump);
469 for (i = 0; i < MAXQUOTAS; i++) {
470 if (ump->um_quotas[i] == NULLVP ||
471 (ump->um_qflags[i] & (QTF_OPENING|QTF_CLOSING)))
472 continue;
473 if (ip->i_dquot[i] == NODQUOT) {
474 UFS_UNLOCK(ump);
475 vn_printf(ITOV(ip), "chkdquot: missing dquot ");
476 panic("chkdquot: missing dquot");
477 }
478 }
479 UFS_UNLOCK(ump);
480 }
481 #endif
482
483 /*
484 * Code to process quotactl commands.
485 */
486
487 /*
488 * Q_QUOTAON - set up a quota file for a particular filesystem.
489 */
490 int
quotaon(struct thread * td,struct mount * mp,int type,void * fname)491 quotaon(struct thread *td, struct mount *mp, int type, void *fname)
492 {
493 struct ufsmount *ump;
494 struct vnode *vp, **vpp;
495 struct vnode *mvp;
496 struct dquot *dq;
497 int error, flags;
498 struct nameidata nd;
499
500 error = priv_check(td, PRIV_UFS_QUOTAON);
501 if (error != 0) {
502 vfs_unbusy(mp);
503 return (error);
504 }
505
506 if ((mp->mnt_flag & MNT_RDONLY) != 0) {
507 vfs_unbusy(mp);
508 return (EROFS);
509 }
510
511 ump = VFSTOUFS(mp);
512 dq = NODQUOT;
513
514 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fname, td);
515 flags = FREAD | FWRITE;
516 vfs_ref(mp);
517 vfs_unbusy(mp);
518 error = vn_open(&nd, &flags, 0, NULL);
519 if (error != 0) {
520 vfs_rel(mp);
521 return (error);
522 }
523 NDFREE(&nd, NDF_ONLY_PNBUF);
524 vp = nd.ni_vp;
525 error = vfs_busy(mp, MBF_NOWAIT);
526 vfs_rel(mp);
527 if (error == 0) {
528 if (vp->v_type != VREG) {
529 error = EACCES;
530 vfs_unbusy(mp);
531 }
532 }
533 if (error != 0) {
534 VOP_UNLOCK(vp, 0);
535 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
536 return (error);
537 }
538
539 UFS_LOCK(ump);
540 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
541 UFS_UNLOCK(ump);
542 VOP_UNLOCK(vp, 0);
543 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
544 vfs_unbusy(mp);
545 return (EALREADY);
546 }
547 ump->um_qflags[type] |= QTF_OPENING|QTF_CLOSING;
548 UFS_UNLOCK(ump);
549 if ((error = dqopen(vp, ump, type)) != 0) {
550 VOP_UNLOCK(vp, 0);
551 UFS_LOCK(ump);
552 ump->um_qflags[type] &= ~(QTF_OPENING|QTF_CLOSING);
553 UFS_UNLOCK(ump);
554 (void) vn_close(vp, FREAD|FWRITE, td->td_ucred, td);
555 vfs_unbusy(mp);
556 return (error);
557 }
558 VOP_UNLOCK(vp, 0);
559 MNT_ILOCK(mp);
560 mp->mnt_flag |= MNT_QUOTA;
561 MNT_IUNLOCK(mp);
562
563 vpp = &ump->um_quotas[type];
564 if (*vpp != vp)
565 quotaoff1(td, mp, type);
566
567 /*
568 * When the directory vnode containing the quota file is
569 * inactivated, due to the shared lookup of the quota file
570 * vput()ing the dvp, the qsyncvp() call for the containing
571 * directory would try to acquire the quota lock exclusive.
572 * At the same time, lookup already locked the quota vnode
573 * shared. Mark the quota vnode lock as allowing recursion
574 * and automatically converting shared locks to exclusive.
575 *
576 * Also mark quota vnode as system.
577 */
578 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
579 vp->v_vflag |= VV_SYSTEM;
580 VN_LOCK_AREC(vp);
581 VN_LOCK_DSHARE(vp);
582 VOP_UNLOCK(vp, 0);
583 *vpp = vp;
584 /*
585 * Save the credential of the process that turned on quotas.
586 * Set up the time limits for this quota.
587 */
588 ump->um_cred[type] = crhold(td->td_ucred);
589 ump->um_btime[type] = MAX_DQ_TIME;
590 ump->um_itime[type] = MAX_IQ_TIME;
591 if (dqget(NULLVP, 0, ump, type, &dq) == 0) {
592 if (dq->dq_btime > 0)
593 ump->um_btime[type] = dq->dq_btime;
594 if (dq->dq_itime > 0)
595 ump->um_itime[type] = dq->dq_itime;
596 dqrele(NULLVP, dq);
597 }
598 /*
599 * Allow the getdq from getinoquota below to read the quota
600 * from file.
601 */
602 UFS_LOCK(ump);
603 ump->um_qflags[type] &= ~QTF_CLOSING;
604 UFS_UNLOCK(ump);
605 /*
606 * Search vnodes associated with this mount point,
607 * adding references to quota file being opened.
608 * NB: only need to add dquot's for inodes being modified.
609 */
610 again:
611 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
612 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
613 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
614 goto again;
615 }
616 if (vp->v_type == VNON || vp->v_writecount == 0) {
617 VOP_UNLOCK(vp, 0);
618 vrele(vp);
619 continue;
620 }
621 error = getinoquota(VTOI(vp));
622 VOP_UNLOCK(vp, 0);
623 vrele(vp);
624 if (error) {
625 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
626 break;
627 }
628 }
629
630 if (error)
631 quotaoff_inchange(td, mp, type);
632 UFS_LOCK(ump);
633 ump->um_qflags[type] &= ~QTF_OPENING;
634 KASSERT((ump->um_qflags[type] & QTF_CLOSING) == 0,
635 ("quotaon: leaking flags"));
636 UFS_UNLOCK(ump);
637
638 vfs_unbusy(mp);
639 return (error);
640 }
641
642 /*
643 * Main code to turn off disk quotas for a filesystem. Does not change
644 * flags.
645 */
646 static int
quotaoff1(struct thread * td,struct mount * mp,int type)647 quotaoff1(struct thread *td, struct mount *mp, int type)
648 {
649 struct vnode *vp;
650 struct vnode *qvp, *mvp;
651 struct ufsmount *ump;
652 struct dquot *dq;
653 struct inode *ip;
654 struct ucred *cr;
655 int error;
656
657 ump = VFSTOUFS(mp);
658
659 UFS_LOCK(ump);
660 KASSERT((ump->um_qflags[type] & QTF_CLOSING) != 0,
661 ("quotaoff1: flags are invalid"));
662 if ((qvp = ump->um_quotas[type]) == NULLVP) {
663 UFS_UNLOCK(ump);
664 return (0);
665 }
666 cr = ump->um_cred[type];
667 UFS_UNLOCK(ump);
668
669 /*
670 * Search vnodes associated with this mount point,
671 * deleting any references to quota file being closed.
672 */
673 again:
674 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
675 if (vp->v_type == VNON) {
676 VI_UNLOCK(vp);
677 continue;
678 }
679 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td)) {
680 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
681 goto again;
682 }
683 ip = VTOI(vp);
684 dq = ip->i_dquot[type];
685 ip->i_dquot[type] = NODQUOT;
686 dqrele(vp, dq);
687 VOP_UNLOCK(vp, 0);
688 vrele(vp);
689 }
690
691 error = dqflush(qvp);
692 if (error != 0)
693 return (error);
694
695 /*
696 * Clear um_quotas before closing the quota vnode to prevent
697 * access to the closed vnode from dqget/dqsync
698 */
699 UFS_LOCK(ump);
700 ump->um_quotas[type] = NULLVP;
701 ump->um_cred[type] = NOCRED;
702 UFS_UNLOCK(ump);
703
704 vn_lock(qvp, LK_EXCLUSIVE | LK_RETRY);
705 qvp->v_vflag &= ~VV_SYSTEM;
706 VOP_UNLOCK(qvp, 0);
707 error = vn_close(qvp, FREAD|FWRITE, td->td_ucred, td);
708 crfree(cr);
709
710 return (error);
711 }
712
713 static int
quotaoff_inchange1(struct thread * td,struct mount * mp,int type)714 quotaoff_inchange1(struct thread *td, struct mount *mp, int type)
715 {
716 int error;
717 bool need_resume;
718
719 /*
720 * mp is already suspended on unmount. If not, suspend it, to
721 * avoid the situation where quotaoff operation eventually
722 * failing due to SU structures still keeping references on
723 * dquots, but vnode's references are already clean. This
724 * would cause quota accounting leak and asserts otherwise.
725 * Note that the thread has already called vn_start_write().
726 */
727 if (mp->mnt_susp_owner == td) {
728 need_resume = false;
729 } else {
730 error = vfs_write_suspend_umnt(mp);
731 if (error != 0)
732 return (error);
733 need_resume = true;
734 }
735 error = quotaoff1(td, mp, type);
736 if (need_resume)
737 vfs_write_resume(mp, VR_START_WRITE);
738 return (error);
739 }
740
741 /*
742 * Turns off quotas, assumes that ump->um_qflags are already checked
743 * and QTF_CLOSING is set to indicate operation in progress. Fixes
744 * ump->um_qflags and mp->mnt_flag after.
745 */
746 int
quotaoff_inchange(struct thread * td,struct mount * mp,int type)747 quotaoff_inchange(struct thread *td, struct mount *mp, int type)
748 {
749 struct ufsmount *ump;
750 int error, i;
751
752 error = quotaoff_inchange1(td, mp, type);
753
754 ump = VFSTOUFS(mp);
755 UFS_LOCK(ump);
756 ump->um_qflags[type] &= ~QTF_CLOSING;
757 for (i = 0; i < MAXQUOTAS; i++)
758 if (ump->um_quotas[i] != NULLVP)
759 break;
760 if (i == MAXQUOTAS) {
761 MNT_ILOCK(mp);
762 mp->mnt_flag &= ~MNT_QUOTA;
763 MNT_IUNLOCK(mp);
764 }
765 UFS_UNLOCK(ump);
766 return (error);
767 }
768
769 /*
770 * Q_QUOTAOFF - turn off disk quotas for a filesystem.
771 */
772 int
quotaoff(struct thread * td,struct mount * mp,int type)773 quotaoff(struct thread *td, struct mount *mp, int type)
774 {
775 struct ufsmount *ump;
776 int error;
777
778 error = priv_check(td, PRIV_UFS_QUOTAOFF);
779 if (error)
780 return (error);
781
782 ump = VFSTOUFS(mp);
783 UFS_LOCK(ump);
784 if ((ump->um_qflags[type] & (QTF_OPENING|QTF_CLOSING)) != 0) {
785 UFS_UNLOCK(ump);
786 return (EALREADY);
787 }
788 ump->um_qflags[type] |= QTF_CLOSING;
789 UFS_UNLOCK(ump);
790
791 return (quotaoff_inchange(td, mp, type));
792 }
793
794 /*
795 * Q_GETQUOTA - return current values in a dqblk structure.
796 */
797 static int
_getquota(struct thread * td,struct mount * mp,u_long id,int type,struct dqblk64 * dqb)798 _getquota(struct thread *td, struct mount *mp, u_long id, int type,
799 struct dqblk64 *dqb)
800 {
801 struct dquot *dq;
802 int error;
803
804 switch (type) {
805 case USRQUOTA:
806 if ((td->td_ucred->cr_uid != id) && !unprivileged_get_quota) {
807 error = priv_check(td, PRIV_VFS_GETQUOTA);
808 if (error)
809 return (error);
810 }
811 break;
812
813 case GRPQUOTA:
814 if (!groupmember(id, td->td_ucred) &&
815 !unprivileged_get_quota) {
816 error = priv_check(td, PRIV_VFS_GETQUOTA);
817 if (error)
818 return (error);
819 }
820 break;
821
822 default:
823 return (EINVAL);
824 }
825
826 dq = NODQUOT;
827 error = dqget(NULLVP, id, VFSTOUFS(mp), type, &dq);
828 if (error)
829 return (error);
830 *dqb = dq->dq_dqb;
831 dqrele(NULLVP, dq);
832 return (error);
833 }
834
835 /*
836 * Q_SETQUOTA - assign an entire dqblk structure.
837 */
838 static int
_setquota(struct thread * td,struct mount * mp,u_long id,int type,struct dqblk64 * dqb)839 _setquota(struct thread *td, struct mount *mp, u_long id, int type,
840 struct dqblk64 *dqb)
841 {
842 struct dquot *dq;
843 struct dquot *ndq;
844 struct ufsmount *ump;
845 struct dqblk64 newlim;
846 int error;
847
848 error = priv_check(td, PRIV_VFS_SETQUOTA);
849 if (error)
850 return (error);
851
852 newlim = *dqb;
853
854 ndq = NODQUOT;
855 ump = VFSTOUFS(mp);
856
857 error = dqget(NULLVP, id, ump, type, &ndq);
858 if (error)
859 return (error);
860 dq = ndq;
861 DQI_LOCK(dq);
862 DQI_WAIT(dq, PINOD+1, "setqta");
863 /*
864 * Copy all but the current values.
865 * Reset time limit if previously had no soft limit or were
866 * under it, but now have a soft limit and are over it.
867 */
868 newlim.dqb_curblocks = dq->dq_curblocks;
869 newlim.dqb_curinodes = dq->dq_curinodes;
870 if (dq->dq_id != 0) {
871 newlim.dqb_btime = dq->dq_btime;
872 newlim.dqb_itime = dq->dq_itime;
873 }
874 if (newlim.dqb_bsoftlimit &&
875 dq->dq_curblocks >= newlim.dqb_bsoftlimit &&
876 (dq->dq_bsoftlimit == 0 || dq->dq_curblocks < dq->dq_bsoftlimit))
877 newlim.dqb_btime = time_second + ump->um_btime[type];
878 if (newlim.dqb_isoftlimit &&
879 dq->dq_curinodes >= newlim.dqb_isoftlimit &&
880 (dq->dq_isoftlimit == 0 || dq->dq_curinodes < dq->dq_isoftlimit))
881 newlim.dqb_itime = time_second + ump->um_itime[type];
882 dq->dq_dqb = newlim;
883 if (dq->dq_curblocks < dq->dq_bsoftlimit)
884 dq->dq_flags &= ~DQ_BLKS;
885 if (dq->dq_curinodes < dq->dq_isoftlimit)
886 dq->dq_flags &= ~DQ_INODS;
887 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
888 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
889 dq->dq_flags |= DQ_FAKE;
890 else
891 dq->dq_flags &= ~DQ_FAKE;
892 dq->dq_flags |= DQ_MOD;
893 DQI_UNLOCK(dq);
894 dqrele(NULLVP, dq);
895 return (0);
896 }
897
898 /*
899 * Q_SETUSE - set current inode and block usage.
900 */
901 static int
_setuse(struct thread * td,struct mount * mp,u_long id,int type,struct dqblk64 * dqb)902 _setuse(struct thread *td, struct mount *mp, u_long id, int type,
903 struct dqblk64 *dqb)
904 {
905 struct dquot *dq;
906 struct ufsmount *ump;
907 struct dquot *ndq;
908 struct dqblk64 usage;
909 int error;
910
911 error = priv_check(td, PRIV_UFS_SETUSE);
912 if (error)
913 return (error);
914
915 usage = *dqb;
916
917 ump = VFSTOUFS(mp);
918 ndq = NODQUOT;
919
920 error = dqget(NULLVP, id, ump, type, &ndq);
921 if (error)
922 return (error);
923 dq = ndq;
924 DQI_LOCK(dq);
925 DQI_WAIT(dq, PINOD+1, "setuse");
926 /*
927 * Reset time limit if have a soft limit and were
928 * previously under it, but are now over it.
929 */
930 if (dq->dq_bsoftlimit && dq->dq_curblocks < dq->dq_bsoftlimit &&
931 usage.dqb_curblocks >= dq->dq_bsoftlimit)
932 dq->dq_btime = time_second + ump->um_btime[type];
933 if (dq->dq_isoftlimit && dq->dq_curinodes < dq->dq_isoftlimit &&
934 usage.dqb_curinodes >= dq->dq_isoftlimit)
935 dq->dq_itime = time_second + ump->um_itime[type];
936 dq->dq_curblocks = usage.dqb_curblocks;
937 dq->dq_curinodes = usage.dqb_curinodes;
938 if (dq->dq_curblocks < dq->dq_bsoftlimit)
939 dq->dq_flags &= ~DQ_BLKS;
940 if (dq->dq_curinodes < dq->dq_isoftlimit)
941 dq->dq_flags &= ~DQ_INODS;
942 dq->dq_flags |= DQ_MOD;
943 DQI_UNLOCK(dq);
944 dqrele(NULLVP, dq);
945 return (0);
946 }
947
948 int
getquota32(struct thread * td,struct mount * mp,u_long id,int type,void * addr)949 getquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
950 {
951 struct dqblk32 dqb32;
952 struct dqblk64 dqb64;
953 int error;
954
955 error = _getquota(td, mp, id, type, &dqb64);
956 if (error)
957 return (error);
958 dqb64_dqb32(&dqb64, &dqb32);
959 error = copyout(&dqb32, addr, sizeof(dqb32));
960 return (error);
961 }
962
963 int
setquota32(struct thread * td,struct mount * mp,u_long id,int type,void * addr)964 setquota32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
965 {
966 struct dqblk32 dqb32;
967 struct dqblk64 dqb64;
968 int error;
969
970 error = copyin(addr, &dqb32, sizeof(dqb32));
971 if (error)
972 return (error);
973 dqb32_dqb64(&dqb32, &dqb64);
974 error = _setquota(td, mp, id, type, &dqb64);
975 return (error);
976 }
977
978 int
setuse32(struct thread * td,struct mount * mp,u_long id,int type,void * addr)979 setuse32(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
980 {
981 struct dqblk32 dqb32;
982 struct dqblk64 dqb64;
983 int error;
984
985 error = copyin(addr, &dqb32, sizeof(dqb32));
986 if (error)
987 return (error);
988 dqb32_dqb64(&dqb32, &dqb64);
989 error = _setuse(td, mp, id, type, &dqb64);
990 return (error);
991 }
992
993 int
getquota(struct thread * td,struct mount * mp,u_long id,int type,void * addr)994 getquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
995 {
996 struct dqblk64 dqb64;
997 int error;
998
999 error = _getquota(td, mp, id, type, &dqb64);
1000 if (error)
1001 return (error);
1002 error = copyout(&dqb64, addr, sizeof(dqb64));
1003 return (error);
1004 }
1005
1006 int
setquota(struct thread * td,struct mount * mp,u_long id,int type,void * addr)1007 setquota(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
1008 {
1009 struct dqblk64 dqb64;
1010 int error;
1011
1012 error = copyin(addr, &dqb64, sizeof(dqb64));
1013 if (error)
1014 return (error);
1015 error = _setquota(td, mp, id, type, &dqb64);
1016 return (error);
1017 }
1018
1019 int
setuse(struct thread * td,struct mount * mp,u_long id,int type,void * addr)1020 setuse(struct thread *td, struct mount *mp, u_long id, int type, void *addr)
1021 {
1022 struct dqblk64 dqb64;
1023 int error;
1024
1025 error = copyin(addr, &dqb64, sizeof(dqb64));
1026 if (error)
1027 return (error);
1028 error = _setuse(td, mp, id, type, &dqb64);
1029 return (error);
1030 }
1031
1032 /*
1033 * Q_GETQUOTASIZE - get bit-size of quota file fields
1034 */
1035 int
getquotasize(struct thread * td,struct mount * mp,u_long id,int type,void * sizep)1036 getquotasize(struct thread *td, struct mount *mp, u_long id, int type,
1037 void *sizep)
1038 {
1039 struct ufsmount *ump = VFSTOUFS(mp);
1040 int bitsize;
1041
1042 UFS_LOCK(ump);
1043 if (ump->um_quotas[type] == NULLVP ||
1044 (ump->um_qflags[type] & QTF_CLOSING)) {
1045 UFS_UNLOCK(ump);
1046 return (EINVAL);
1047 }
1048 if ((ump->um_qflags[type] & QTF_64BIT) != 0)
1049 bitsize = 64;
1050 else
1051 bitsize = 32;
1052 UFS_UNLOCK(ump);
1053 return (copyout(&bitsize, sizep, sizeof(int)));
1054 }
1055
1056 /*
1057 * Q_SYNC - sync quota files to disk.
1058 */
1059 int
qsync(struct mount * mp)1060 qsync(struct mount *mp)
1061 {
1062 struct ufsmount *ump = VFSTOUFS(mp);
1063 struct thread *td = curthread; /* XXX */
1064 struct vnode *vp, *mvp;
1065 struct dquot *dq;
1066 int i, error;
1067
1068 /*
1069 * Check if the mount point has any quotas.
1070 * If not, simply return.
1071 */
1072 for (i = 0; i < MAXQUOTAS; i++)
1073 if (ump->um_quotas[i] != NULLVP)
1074 break;
1075 if (i == MAXQUOTAS)
1076 return (0);
1077 /*
1078 * Search vnodes associated with this mount point,
1079 * synchronizing any modified dquot structures.
1080 */
1081 again:
1082 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
1083 if (vp->v_type == VNON) {
1084 VI_UNLOCK(vp);
1085 continue;
1086 }
1087 error = vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
1088 if (error) {
1089 if (error == ENOENT) {
1090 MNT_VNODE_FOREACH_ACTIVE_ABORT(mp, mvp);
1091 goto again;
1092 }
1093 continue;
1094 }
1095 for (i = 0; i < MAXQUOTAS; i++) {
1096 dq = VTOI(vp)->i_dquot[i];
1097 if (dq != NODQUOT)
1098 dqsync(vp, dq);
1099 }
1100 vput(vp);
1101 }
1102 return (0);
1103 }
1104
1105 /*
1106 * Sync quota file for given vnode to disk.
1107 */
1108 int
qsyncvp(struct vnode * vp)1109 qsyncvp(struct vnode *vp)
1110 {
1111 struct ufsmount *ump = VFSTOUFS(vp->v_mount);
1112 struct dquot *dq;
1113 int i;
1114
1115 /*
1116 * Check if the mount point has any quotas.
1117 * If not, simply return.
1118 */
1119 for (i = 0; i < MAXQUOTAS; i++)
1120 if (ump->um_quotas[i] != NULLVP)
1121 break;
1122 if (i == MAXQUOTAS)
1123 return (0);
1124 /*
1125 * Search quotas associated with this vnode
1126 * synchronizing any modified dquot structures.
1127 */
1128 for (i = 0; i < MAXQUOTAS; i++) {
1129 dq = VTOI(vp)->i_dquot[i];
1130 if (dq != NODQUOT)
1131 dqsync(vp, dq);
1132 }
1133 return (0);
1134 }
1135
1136 /*
1137 * Code pertaining to management of the in-core dquot data structures.
1138 */
1139 #define DQHASH(dqvp, id) \
1140 (&dqhashtbl[((((intptr_t)(dqvp)) >> 8) + id) & dqhash])
1141 static LIST_HEAD(dqhash, dquot) *dqhashtbl;
1142 static u_long dqhash;
1143
1144 /*
1145 * Dquot free list.
1146 */
1147 #define DQUOTINC 5 /* minimum free dquots desired */
1148 static TAILQ_HEAD(dqfreelist, dquot) dqfreelist;
1149 static long numdquot, desireddquot = DQUOTINC;
1150
1151 /*
1152 * Lock to protect quota hash, dq free list and dq_cnt ref counters of
1153 * _all_ dqs.
1154 */
1155 struct mtx dqhlock;
1156
1157 #define DQH_LOCK() mtx_lock(&dqhlock)
1158 #define DQH_UNLOCK() mtx_unlock(&dqhlock)
1159
1160 static struct dquot *dqhashfind(struct dqhash *dqh, u_long id,
1161 struct vnode *dqvp);
1162
1163 /*
1164 * Initialize the quota system.
1165 */
1166 void
dqinit(void)1167 dqinit(void)
1168 {
1169
1170 mtx_init(&dqhlock, "dqhlock", NULL, MTX_DEF);
1171 dqhashtbl = hashinit(desiredvnodes, M_DQUOT, &dqhash);
1172 TAILQ_INIT(&dqfreelist);
1173 }
1174
1175 /*
1176 * Shut down the quota system.
1177 */
1178 void
dquninit(void)1179 dquninit(void)
1180 {
1181 struct dquot *dq;
1182
1183 hashdestroy(dqhashtbl, M_DQUOT, dqhash);
1184 while ((dq = TAILQ_FIRST(&dqfreelist)) != NULL) {
1185 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1186 mtx_destroy(&dq->dq_lock);
1187 free(dq, M_DQUOT);
1188 }
1189 mtx_destroy(&dqhlock);
1190 }
1191
1192 static struct dquot *
dqhashfind(struct dqhash * dqh,u_long id,struct vnode * dqvp)1193 dqhashfind(struct dqhash *dqh, u_long id, struct vnode *dqvp)
1194 {
1195 struct dquot *dq;
1196
1197 mtx_assert(&dqhlock, MA_OWNED);
1198 LIST_FOREACH(dq, dqh, dq_hash) {
1199 if (dq->dq_id != id ||
1200 dq->dq_ump->um_quotas[dq->dq_type] != dqvp)
1201 continue;
1202 /*
1203 * Cache hit with no references. Take
1204 * the structure off the free list.
1205 */
1206 if (dq->dq_cnt == 0)
1207 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1208 DQREF(dq);
1209 return (dq);
1210 }
1211 return (NODQUOT);
1212 }
1213
1214 /*
1215 * Determine the quota file type.
1216 *
1217 * A 32-bit quota file is simply an array of struct dqblk32.
1218 *
1219 * A 64-bit quota file is a struct dqhdr64 followed by an array of struct
1220 * dqblk64. The header contains various magic bits which allow us to be
1221 * reasonably confident that it is indeeda 64-bit quota file and not just
1222 * a 32-bit quota file that just happens to "look right".
1223 *
1224 */
1225 static int
dqopen(struct vnode * vp,struct ufsmount * ump,int type)1226 dqopen(struct vnode *vp, struct ufsmount *ump, int type)
1227 {
1228 struct dqhdr64 dqh;
1229 struct iovec aiov;
1230 struct uio auio;
1231 int error;
1232
1233 ASSERT_VOP_LOCKED(vp, "dqopen");
1234 auio.uio_iov = &aiov;
1235 auio.uio_iovcnt = 1;
1236 aiov.iov_base = &dqh;
1237 aiov.iov_len = sizeof(dqh);
1238 auio.uio_resid = sizeof(dqh);
1239 auio.uio_offset = 0;
1240 auio.uio_segflg = UIO_SYSSPACE;
1241 auio.uio_rw = UIO_READ;
1242 auio.uio_td = (struct thread *)0;
1243 error = VOP_READ(vp, &auio, 0, ump->um_cred[type]);
1244
1245 if (error != 0)
1246 return (error);
1247 if (auio.uio_resid > 0) {
1248 /* assume 32 bits */
1249 return (0);
1250 }
1251
1252 UFS_LOCK(ump);
1253 if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) == 0 &&
1254 be32toh(dqh.dqh_version) == Q_DQHDR64_VERSION &&
1255 be32toh(dqh.dqh_hdrlen) == (uint32_t)sizeof(struct dqhdr64) &&
1256 be32toh(dqh.dqh_reclen) == (uint32_t)sizeof(struct dqblk64)) {
1257 /* XXX: what if the magic matches, but the sizes are wrong? */
1258 ump->um_qflags[type] |= QTF_64BIT;
1259 } else {
1260 ump->um_qflags[type] &= ~QTF_64BIT;
1261 }
1262 UFS_UNLOCK(ump);
1263
1264 return (0);
1265 }
1266
1267 /*
1268 * Obtain a dquot structure for the specified identifier and quota file
1269 * reading the information from the file if necessary.
1270 */
1271 static int
dqget(struct vnode * vp,u_long id,struct ufsmount * ump,int type,struct dquot ** dqp)1272 dqget(struct vnode *vp, u_long id, struct ufsmount *ump, int type,
1273 struct dquot **dqp)
1274 {
1275 uint8_t buf[sizeof(struct dqblk64)];
1276 off_t base, recsize;
1277 struct dquot *dq, *dq1;
1278 struct dqhash *dqh;
1279 struct vnode *dqvp;
1280 struct iovec aiov;
1281 struct uio auio;
1282 int dqvplocked, error;
1283
1284 #ifdef DEBUG_VFS_LOCKS
1285 if (vp != NULLVP)
1286 ASSERT_VOP_ELOCKED(vp, "dqget");
1287 #endif
1288
1289 if (vp != NULLVP && *dqp != NODQUOT) {
1290 return (0);
1291 }
1292
1293 /* XXX: Disallow negative id values to prevent the
1294 * creation of 100GB+ quota data files.
1295 */
1296 if ((int)id < 0)
1297 return (EINVAL);
1298
1299 UFS_LOCK(ump);
1300 dqvp = ump->um_quotas[type];
1301 if (dqvp == NULLVP || (ump->um_qflags[type] & QTF_CLOSING)) {
1302 *dqp = NODQUOT;
1303 UFS_UNLOCK(ump);
1304 return (EINVAL);
1305 }
1306 vref(dqvp);
1307 UFS_UNLOCK(ump);
1308 error = 0;
1309 dqvplocked = 0;
1310
1311 /*
1312 * Check the cache first.
1313 */
1314 dqh = DQHASH(dqvp, id);
1315 DQH_LOCK();
1316 dq = dqhashfind(dqh, id, dqvp);
1317 if (dq != NULL) {
1318 DQH_UNLOCK();
1319 hfound: DQI_LOCK(dq);
1320 DQI_WAIT(dq, PINOD+1, "dqget");
1321 DQI_UNLOCK(dq);
1322 if (dq->dq_ump == NULL) {
1323 dqrele(vp, dq);
1324 dq = NODQUOT;
1325 error = EIO;
1326 }
1327 *dqp = dq;
1328 if (dqvplocked)
1329 vput(dqvp);
1330 else
1331 vrele(dqvp);
1332 return (error);
1333 }
1334
1335 /*
1336 * Quota vnode lock is before DQ_LOCK. Acquire dqvp lock there
1337 * since new dq will appear on the hash chain DQ_LOCKed.
1338 */
1339 if (vp != dqvp) {
1340 DQH_UNLOCK();
1341 vn_lock(dqvp, LK_SHARED | LK_RETRY);
1342 dqvplocked = 1;
1343 DQH_LOCK();
1344 /*
1345 * Recheck the cache after sleep for quota vnode lock.
1346 */
1347 dq = dqhashfind(dqh, id, dqvp);
1348 if (dq != NULL) {
1349 DQH_UNLOCK();
1350 goto hfound;
1351 }
1352 }
1353
1354 /*
1355 * Not in cache, allocate a new one or take it from the
1356 * free list.
1357 */
1358 if (TAILQ_FIRST(&dqfreelist) == NODQUOT &&
1359 numdquot < MAXQUOTAS * desiredvnodes)
1360 desireddquot += DQUOTINC;
1361 if (numdquot < desireddquot) {
1362 numdquot++;
1363 DQH_UNLOCK();
1364 dq1 = malloc(sizeof *dq1, M_DQUOT, M_WAITOK | M_ZERO);
1365 mtx_init(&dq1->dq_lock, "dqlock", NULL, MTX_DEF);
1366 DQH_LOCK();
1367 /*
1368 * Recheck the cache after sleep for memory.
1369 */
1370 dq = dqhashfind(dqh, id, dqvp);
1371 if (dq != NULL) {
1372 numdquot--;
1373 DQH_UNLOCK();
1374 mtx_destroy(&dq1->dq_lock);
1375 free(dq1, M_DQUOT);
1376 goto hfound;
1377 }
1378 dq = dq1;
1379 } else {
1380 if ((dq = TAILQ_FIRST(&dqfreelist)) == NULL) {
1381 DQH_UNLOCK();
1382 tablefull("dquot");
1383 *dqp = NODQUOT;
1384 if (dqvplocked)
1385 vput(dqvp);
1386 else
1387 vrele(dqvp);
1388 return (EUSERS);
1389 }
1390 if (dq->dq_cnt || (dq->dq_flags & DQ_MOD))
1391 panic("dqget: free dquot isn't %p", dq);
1392 TAILQ_REMOVE(&dqfreelist, dq, dq_freelist);
1393 if (dq->dq_ump != NULL)
1394 LIST_REMOVE(dq, dq_hash);
1395 }
1396
1397 /*
1398 * Dq is put into hash already locked to prevent parallel
1399 * usage while it is being read from file.
1400 */
1401 dq->dq_flags = DQ_LOCK;
1402 dq->dq_id = id;
1403 dq->dq_type = type;
1404 dq->dq_ump = ump;
1405 LIST_INSERT_HEAD(dqh, dq, dq_hash);
1406 DQREF(dq);
1407 DQH_UNLOCK();
1408
1409 /*
1410 * Read the requested quota record from the quota file, performing
1411 * any necessary conversions.
1412 */
1413 if (ump->um_qflags[type] & QTF_64BIT) {
1414 recsize = sizeof(struct dqblk64);
1415 base = sizeof(struct dqhdr64);
1416 } else {
1417 recsize = sizeof(struct dqblk32);
1418 base = 0;
1419 }
1420 auio.uio_iov = &aiov;
1421 auio.uio_iovcnt = 1;
1422 aiov.iov_base = buf;
1423 aiov.iov_len = recsize;
1424 auio.uio_resid = recsize;
1425 auio.uio_offset = base + id * recsize;
1426 auio.uio_segflg = UIO_SYSSPACE;
1427 auio.uio_rw = UIO_READ;
1428 auio.uio_td = (struct thread *)0;
1429
1430 error = VOP_READ(dqvp, &auio, 0, ump->um_cred[type]);
1431 if (auio.uio_resid == recsize && error == 0) {
1432 bzero(&dq->dq_dqb, sizeof(dq->dq_dqb));
1433 } else {
1434 if (ump->um_qflags[type] & QTF_64BIT)
1435 dqb64_dq((struct dqblk64 *)buf, dq);
1436 else
1437 dqb32_dq((struct dqblk32 *)buf, dq);
1438 }
1439 if (dqvplocked)
1440 vput(dqvp);
1441 else
1442 vrele(dqvp);
1443 /*
1444 * I/O error in reading quota file, release
1445 * quota structure and reflect problem to caller.
1446 */
1447 if (error) {
1448 DQH_LOCK();
1449 dq->dq_ump = NULL;
1450 LIST_REMOVE(dq, dq_hash);
1451 DQH_UNLOCK();
1452 DQI_LOCK(dq);
1453 if (dq->dq_flags & DQ_WANT)
1454 wakeup(dq);
1455 dq->dq_flags = 0;
1456 DQI_UNLOCK(dq);
1457 dqrele(vp, dq);
1458 *dqp = NODQUOT;
1459 return (error);
1460 }
1461 DQI_LOCK(dq);
1462 /*
1463 * Check for no limit to enforce.
1464 * Initialize time values if necessary.
1465 */
1466 if (dq->dq_isoftlimit == 0 && dq->dq_bsoftlimit == 0 &&
1467 dq->dq_ihardlimit == 0 && dq->dq_bhardlimit == 0)
1468 dq->dq_flags |= DQ_FAKE;
1469 if (dq->dq_id != 0) {
1470 if (dq->dq_btime == 0) {
1471 dq->dq_btime = time_second + ump->um_btime[type];
1472 if (dq->dq_bsoftlimit &&
1473 dq->dq_curblocks >= dq->dq_bsoftlimit)
1474 dq->dq_flags |= DQ_MOD;
1475 }
1476 if (dq->dq_itime == 0) {
1477 dq->dq_itime = time_second + ump->um_itime[type];
1478 if (dq->dq_isoftlimit &&
1479 dq->dq_curinodes >= dq->dq_isoftlimit)
1480 dq->dq_flags |= DQ_MOD;
1481 }
1482 }
1483 DQI_WAKEUP(dq);
1484 DQI_UNLOCK(dq);
1485 *dqp = dq;
1486 return (0);
1487 }
1488
1489 #ifdef DIAGNOSTIC
1490 /*
1491 * Obtain a reference to a dquot.
1492 */
1493 static void
dqref(struct dquot * dq)1494 dqref(struct dquot *dq)
1495 {
1496
1497 dq->dq_cnt++;
1498 }
1499 #endif
1500
1501 /*
1502 * Release a reference to a dquot.
1503 */
1504 void
dqrele(struct vnode * vp,struct dquot * dq)1505 dqrele(struct vnode *vp, struct dquot *dq)
1506 {
1507
1508 if (dq == NODQUOT)
1509 return;
1510 DQH_LOCK();
1511 KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 1", dq));
1512 if (dq->dq_cnt > 1) {
1513 dq->dq_cnt--;
1514 DQH_UNLOCK();
1515 return;
1516 }
1517 DQH_UNLOCK();
1518 sync:
1519 (void) dqsync(vp, dq);
1520
1521 DQH_LOCK();
1522 KASSERT(dq->dq_cnt > 0, ("Lost dq %p reference 2", dq));
1523 if (--dq->dq_cnt > 0)
1524 {
1525 DQH_UNLOCK();
1526 return;
1527 }
1528
1529 /*
1530 * The dq may become dirty after it is synced but before it is
1531 * put to the free list. Checking the DQ_MOD there without
1532 * locking dq should be safe since no other references to the
1533 * dq exist.
1534 */
1535 if ((dq->dq_flags & DQ_MOD) != 0) {
1536 dq->dq_cnt++;
1537 DQH_UNLOCK();
1538 goto sync;
1539 }
1540 TAILQ_INSERT_TAIL(&dqfreelist, dq, dq_freelist);
1541 DQH_UNLOCK();
1542 }
1543
1544 /*
1545 * Update the disk quota in the quota file.
1546 */
1547 static int
dqsync(struct vnode * vp,struct dquot * dq)1548 dqsync(struct vnode *vp, struct dquot *dq)
1549 {
1550 uint8_t buf[sizeof(struct dqblk64)];
1551 off_t base, recsize;
1552 struct vnode *dqvp;
1553 struct iovec aiov;
1554 struct uio auio;
1555 int error;
1556 struct mount *mp;
1557 struct ufsmount *ump;
1558
1559 #ifdef DEBUG_VFS_LOCKS
1560 if (vp != NULL)
1561 ASSERT_VOP_ELOCKED(vp, "dqsync");
1562 #endif
1563
1564 mp = NULL;
1565 error = 0;
1566 if (dq == NODQUOT)
1567 panic("dqsync: dquot");
1568 if ((ump = dq->dq_ump) == NULL)
1569 return (0);
1570 UFS_LOCK(ump);
1571 if ((dqvp = ump->um_quotas[dq->dq_type]) == NULLVP) {
1572 if (vp == NULL) {
1573 UFS_UNLOCK(ump);
1574 return (0);
1575 } else
1576 panic("dqsync: file");
1577 }
1578 vref(dqvp);
1579 UFS_UNLOCK(ump);
1580
1581 DQI_LOCK(dq);
1582 if ((dq->dq_flags & DQ_MOD) == 0) {
1583 DQI_UNLOCK(dq);
1584 vrele(dqvp);
1585 return (0);
1586 }
1587 DQI_UNLOCK(dq);
1588
1589 (void) vn_start_secondary_write(dqvp, &mp, V_WAIT);
1590 if (vp != dqvp)
1591 vn_lock(dqvp, LK_EXCLUSIVE | LK_RETRY);
1592
1593 DQI_LOCK(dq);
1594 DQI_WAIT(dq, PINOD+2, "dqsync");
1595 if ((dq->dq_flags & DQ_MOD) == 0)
1596 goto out;
1597 dq->dq_flags |= DQ_LOCK;
1598 DQI_UNLOCK(dq);
1599
1600 /*
1601 * Write the quota record to the quota file, performing any
1602 * necessary conversions. See dqget() for additional details.
1603 */
1604 if (ump->um_qflags[dq->dq_type] & QTF_64BIT) {
1605 dq_dqb64(dq, (struct dqblk64 *)buf);
1606 recsize = sizeof(struct dqblk64);
1607 base = sizeof(struct dqhdr64);
1608 } else {
1609 dq_dqb32(dq, (struct dqblk32 *)buf);
1610 recsize = sizeof(struct dqblk32);
1611 base = 0;
1612 }
1613
1614 auio.uio_iov = &aiov;
1615 auio.uio_iovcnt = 1;
1616 aiov.iov_base = buf;
1617 aiov.iov_len = recsize;
1618 auio.uio_resid = recsize;
1619 auio.uio_offset = base + dq->dq_id * recsize;
1620 auio.uio_segflg = UIO_SYSSPACE;
1621 auio.uio_rw = UIO_WRITE;
1622 auio.uio_td = (struct thread *)0;
1623 error = VOP_WRITE(dqvp, &auio, 0, dq->dq_ump->um_cred[dq->dq_type]);
1624 if (auio.uio_resid && error == 0)
1625 error = EIO;
1626
1627 DQI_LOCK(dq);
1628 DQI_WAKEUP(dq);
1629 dq->dq_flags &= ~DQ_MOD;
1630 out:
1631 DQI_UNLOCK(dq);
1632 if (vp != dqvp)
1633 vput(dqvp);
1634 else
1635 vrele(dqvp);
1636 vn_finished_secondary_write(mp);
1637 return (error);
1638 }
1639
1640 /*
1641 * Flush all entries from the cache for a particular vnode.
1642 */
1643 static int
dqflush(struct vnode * vp)1644 dqflush(struct vnode *vp)
1645 {
1646 struct dquot *dq, *nextdq;
1647 struct dqhash *dqh;
1648 int error;
1649
1650 /*
1651 * Move all dquot's that used to refer to this quota
1652 * file off their hash chains (they will eventually
1653 * fall off the head of the free list and be re-used).
1654 */
1655 error = 0;
1656 DQH_LOCK();
1657 for (dqh = &dqhashtbl[dqhash]; dqh >= dqhashtbl; dqh--) {
1658 for (dq = LIST_FIRST(dqh); dq; dq = nextdq) {
1659 nextdq = LIST_NEXT(dq, dq_hash);
1660 if (dq->dq_ump->um_quotas[dq->dq_type] != vp)
1661 continue;
1662 if (dq->dq_cnt)
1663 error = EBUSY;
1664 else {
1665 LIST_REMOVE(dq, dq_hash);
1666 dq->dq_ump = NULL;
1667 }
1668 }
1669 }
1670 DQH_UNLOCK();
1671 return (error);
1672 }
1673
1674 /*
1675 * The following three functions are provided for the adjustment of
1676 * quotas by the soft updates code.
1677 */
1678 #ifdef SOFTUPDATES
1679 /*
1680 * Acquire a reference to the quota structures associated with a vnode.
1681 * Return count of number of quota structures found.
1682 */
1683 int
quotaref(vp,qrp)1684 quotaref(vp, qrp)
1685 struct vnode *vp;
1686 struct dquot **qrp;
1687 {
1688 struct inode *ip;
1689 struct dquot *dq;
1690 int i, found;
1691
1692 for (i = 0; i < MAXQUOTAS; i++)
1693 qrp[i] = NODQUOT;
1694 /*
1695 * Disk quotas must be turned off for system files. Currently
1696 * snapshot and quota files.
1697 */
1698 if ((vp->v_vflag & VV_SYSTEM) != 0)
1699 return (0);
1700 /*
1701 * Iterate through and copy active quotas.
1702 */
1703 found = 0;
1704 ip = VTOI(vp);
1705 mtx_lock(&dqhlock);
1706 for (i = 0; i < MAXQUOTAS; i++) {
1707 if ((dq = ip->i_dquot[i]) == NODQUOT)
1708 continue;
1709 DQREF(dq);
1710 qrp[i] = dq;
1711 found++;
1712 }
1713 mtx_unlock(&dqhlock);
1714 return (found);
1715 }
1716
1717 /*
1718 * Release a set of quota structures obtained from a vnode.
1719 */
1720 void
quotarele(qrp)1721 quotarele(qrp)
1722 struct dquot **qrp;
1723 {
1724 struct dquot *dq;
1725 int i;
1726
1727 for (i = 0; i < MAXQUOTAS; i++) {
1728 if ((dq = qrp[i]) == NODQUOT)
1729 continue;
1730 dqrele(NULL, dq);
1731 }
1732 }
1733
1734 /*
1735 * Adjust the number of blocks associated with a quota.
1736 * Positive numbers when adding blocks; negative numbers when freeing blocks.
1737 */
1738 void
quotaadj(qrp,ump,blkcount)1739 quotaadj(qrp, ump, blkcount)
1740 struct dquot **qrp;
1741 struct ufsmount *ump;
1742 int64_t blkcount;
1743 {
1744 struct dquot *dq;
1745 ufs2_daddr_t ncurblocks;
1746 int i;
1747
1748 if (blkcount == 0)
1749 return;
1750 for (i = 0; i < MAXQUOTAS; i++) {
1751 if ((dq = qrp[i]) == NODQUOT)
1752 continue;
1753 DQI_LOCK(dq);
1754 DQI_WAIT(dq, PINOD+1, "adjqta");
1755 ncurblocks = dq->dq_curblocks + blkcount;
1756 if (ncurblocks >= 0)
1757 dq->dq_curblocks = ncurblocks;
1758 else
1759 dq->dq_curblocks = 0;
1760 if (blkcount < 0)
1761 dq->dq_flags &= ~DQ_BLKS;
1762 else if (dq->dq_curblocks + blkcount >= dq->dq_bsoftlimit &&
1763 dq->dq_curblocks < dq->dq_bsoftlimit)
1764 dq->dq_btime = time_second + ump->um_btime[i];
1765 dq->dq_flags |= DQ_MOD;
1766 DQI_UNLOCK(dq);
1767 }
1768 }
1769 #endif /* SOFTUPDATES */
1770
1771 /*
1772 * 32-bit / 64-bit conversion functions.
1773 *
1774 * 32-bit quota records are stored in native byte order. Attention must
1775 * be paid to overflow issues.
1776 *
1777 * 64-bit quota records are stored in network byte order.
1778 */
1779
1780 #define CLIP32(u64) (u64 > UINT32_MAX ? UINT32_MAX : (uint32_t)u64)
1781
1782 /*
1783 * Convert 32-bit host-order structure to dquot.
1784 */
1785 static void
dqb32_dq(const struct dqblk32 * dqb32,struct dquot * dq)1786 dqb32_dq(const struct dqblk32 *dqb32, struct dquot *dq)
1787 {
1788
1789 dq->dq_bhardlimit = dqb32->dqb_bhardlimit;
1790 dq->dq_bsoftlimit = dqb32->dqb_bsoftlimit;
1791 dq->dq_curblocks = dqb32->dqb_curblocks;
1792 dq->dq_ihardlimit = dqb32->dqb_ihardlimit;
1793 dq->dq_isoftlimit = dqb32->dqb_isoftlimit;
1794 dq->dq_curinodes = dqb32->dqb_curinodes;
1795 dq->dq_btime = dqb32->dqb_btime;
1796 dq->dq_itime = dqb32->dqb_itime;
1797 }
1798
1799 /*
1800 * Convert 64-bit network-order structure to dquot.
1801 */
1802 static void
dqb64_dq(const struct dqblk64 * dqb64,struct dquot * dq)1803 dqb64_dq(const struct dqblk64 *dqb64, struct dquot *dq)
1804 {
1805
1806 dq->dq_bhardlimit = be64toh(dqb64->dqb_bhardlimit);
1807 dq->dq_bsoftlimit = be64toh(dqb64->dqb_bsoftlimit);
1808 dq->dq_curblocks = be64toh(dqb64->dqb_curblocks);
1809 dq->dq_ihardlimit = be64toh(dqb64->dqb_ihardlimit);
1810 dq->dq_isoftlimit = be64toh(dqb64->dqb_isoftlimit);
1811 dq->dq_curinodes = be64toh(dqb64->dqb_curinodes);
1812 dq->dq_btime = be64toh(dqb64->dqb_btime);
1813 dq->dq_itime = be64toh(dqb64->dqb_itime);
1814 }
1815
1816 /*
1817 * Convert dquot to 32-bit host-order structure.
1818 */
1819 static void
dq_dqb32(const struct dquot * dq,struct dqblk32 * dqb32)1820 dq_dqb32(const struct dquot *dq, struct dqblk32 *dqb32)
1821 {
1822
1823 dqb32->dqb_bhardlimit = CLIP32(dq->dq_bhardlimit);
1824 dqb32->dqb_bsoftlimit = CLIP32(dq->dq_bsoftlimit);
1825 dqb32->dqb_curblocks = CLIP32(dq->dq_curblocks);
1826 dqb32->dqb_ihardlimit = CLIP32(dq->dq_ihardlimit);
1827 dqb32->dqb_isoftlimit = CLIP32(dq->dq_isoftlimit);
1828 dqb32->dqb_curinodes = CLIP32(dq->dq_curinodes);
1829 dqb32->dqb_btime = CLIP32(dq->dq_btime);
1830 dqb32->dqb_itime = CLIP32(dq->dq_itime);
1831 }
1832
1833 /*
1834 * Convert dquot to 64-bit network-order structure.
1835 */
1836 static void
dq_dqb64(const struct dquot * dq,struct dqblk64 * dqb64)1837 dq_dqb64(const struct dquot *dq, struct dqblk64 *dqb64)
1838 {
1839
1840 dqb64->dqb_bhardlimit = htobe64(dq->dq_bhardlimit);
1841 dqb64->dqb_bsoftlimit = htobe64(dq->dq_bsoftlimit);
1842 dqb64->dqb_curblocks = htobe64(dq->dq_curblocks);
1843 dqb64->dqb_ihardlimit = htobe64(dq->dq_ihardlimit);
1844 dqb64->dqb_isoftlimit = htobe64(dq->dq_isoftlimit);
1845 dqb64->dqb_curinodes = htobe64(dq->dq_curinodes);
1846 dqb64->dqb_btime = htobe64(dq->dq_btime);
1847 dqb64->dqb_itime = htobe64(dq->dq_itime);
1848 }
1849
1850 /*
1851 * Convert 64-bit host-order structure to 32-bit host-order structure.
1852 */
1853 static void
dqb64_dqb32(const struct dqblk64 * dqb64,struct dqblk32 * dqb32)1854 dqb64_dqb32(const struct dqblk64 *dqb64, struct dqblk32 *dqb32)
1855 {
1856
1857 dqb32->dqb_bhardlimit = CLIP32(dqb64->dqb_bhardlimit);
1858 dqb32->dqb_bsoftlimit = CLIP32(dqb64->dqb_bsoftlimit);
1859 dqb32->dqb_curblocks = CLIP32(dqb64->dqb_curblocks);
1860 dqb32->dqb_ihardlimit = CLIP32(dqb64->dqb_ihardlimit);
1861 dqb32->dqb_isoftlimit = CLIP32(dqb64->dqb_isoftlimit);
1862 dqb32->dqb_curinodes = CLIP32(dqb64->dqb_curinodes);
1863 dqb32->dqb_btime = CLIP32(dqb64->dqb_btime);
1864 dqb32->dqb_itime = CLIP32(dqb64->dqb_itime);
1865 }
1866
1867 /*
1868 * Convert 32-bit host-order structure to 64-bit host-order structure.
1869 */
1870 static void
dqb32_dqb64(const struct dqblk32 * dqb32,struct dqblk64 * dqb64)1871 dqb32_dqb64(const struct dqblk32 *dqb32, struct dqblk64 *dqb64)
1872 {
1873
1874 dqb64->dqb_bhardlimit = dqb32->dqb_bhardlimit;
1875 dqb64->dqb_bsoftlimit = dqb32->dqb_bsoftlimit;
1876 dqb64->dqb_curblocks = dqb32->dqb_curblocks;
1877 dqb64->dqb_ihardlimit = dqb32->dqb_ihardlimit;
1878 dqb64->dqb_isoftlimit = dqb32->dqb_isoftlimit;
1879 dqb64->dqb_curinodes = dqb32->dqb_curinodes;
1880 dqb64->dqb_btime = dqb32->dqb_btime;
1881 dqb64->dqb_itime = dqb32->dqb_itime;
1882 }
1883