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