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