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