1 /* $NetBSD: ext2fs_vnops.c,v 1.8 1998/03/01 02:23:46 fvdl Exp $ */ 2 3 /* 4 * Copyright (c) 1997 Manuel Bouyer. 5 * Copyright (c) 1982, 1986, 1989, 1993 6 * The Regents of the University of California. All rights reserved. 7 * (c) UNIX System Laboratories, Inc. 8 * All or some portions of this file are derived from material licensed 9 * to the University of California by American Telephone and Telegraph 10 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11 * the permission of UNIX System Laboratories, Inc. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)ufs_vnops.c 8.14 (Berkeley) 10/26/94 42 * Modified for ext2fs by Manuel Bouyer. 43 */ 44 45 #include "opt_uvm.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/resourcevar.h> 50 #include <sys/kernel.h> 51 #include <sys/file.h> 52 #include <sys/stat.h> 53 #include <sys/buf.h> 54 #include <sys/proc.h> 55 #include <sys/conf.h> 56 #include <sys/mount.h> 57 #include <sys/namei.h> 58 #include <sys/vnode.h> 59 #include <sys/lockf.h> 60 #include <sys/malloc.h> 61 #include <sys/signalvar.h> 62 63 #include <vm/vm.h> 64 #if defined(UVM) 65 #include <uvm/uvm_extern.h> 66 #endif 67 68 #include <miscfs/fifofs/fifo.h> 69 #include <miscfs/genfs/genfs.h> 70 #include <miscfs/specfs/specdev.h> 71 72 #include <ufs/ufs/quota.h> 73 #include <ufs/ufs/inode.h> 74 #include <ufs/ufs/ufs_extern.h> 75 #include <ufs/ufs/ufsmount.h> 76 77 #include <ufs/ext2fs/ext2fs.h> 78 #include <ufs/ext2fs/ext2fs_extern.h> 79 #include <ufs/ext2fs/ext2fs_dir.h> 80 81 82 static int ext2fs_chmod 83 __P((struct vnode *, int, struct ucred *, struct proc *)); 84 static int ext2fs_chown 85 __P((struct vnode *, uid_t, gid_t, struct ucred *, struct proc *)); 86 87 union _qcvt { 88 int64_t qcvt; 89 int32_t val[2]; 90 }; 91 #define SETHIGH(q, h) { \ 92 union _qcvt tmp; \ 93 tmp.qcvt = (q); \ 94 tmp.val[_QUAD_HIGHWORD] = (h); \ 95 (q) = tmp.qcvt; \ 96 } 97 #define SETLOW(q, l) { \ 98 union _qcvt tmp; \ 99 tmp.qcvt = (q); \ 100 tmp.val[_QUAD_LOWWORD] = (l); \ 101 (q) = tmp.qcvt; \ 102 } 103 104 /* 105 * Create a regular file 106 */ 107 int 108 ext2fs_create(v) 109 void *v; 110 { 111 struct vop_create_args /* { 112 struct vnode *a_dvp; 113 struct vnode **a_vpp; 114 struct componentname *a_cnp; 115 struct vattr *a_vap; 116 } */ *ap = v; 117 return ext2fs_makeinode(MAKEIMODE(ap->a_vap->va_type, 118 ap->a_vap->va_mode), 119 ap->a_dvp, ap->a_vpp, ap->a_cnp); 120 } 121 122 /* 123 * Mknod vnode call 124 */ 125 /* ARGSUSED */ 126 int 127 ext2fs_mknod(v) 128 void *v; 129 { 130 struct vop_mknod_args /* { 131 struct vnode *a_dvp; 132 struct vnode **a_vpp; 133 struct componentname *a_cnp; 134 struct vattr *a_vap; 135 } */ *ap = v; 136 register struct vattr *vap = ap->a_vap; 137 register struct vnode **vpp = ap->a_vpp; 138 register struct inode *ip; 139 int error; 140 141 if ((error = ext2fs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), 142 ap->a_dvp, vpp, ap->a_cnp)) != 0) 143 return (error); 144 ip = VTOI(*vpp); 145 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 146 if (vap->va_rdev != VNOVAL) { 147 /* 148 * Want to be able to use this to make badblock 149 * inodes, so don't truncate the dev number. 150 */ 151 ip->i_din.e2fs_din.e2di_rdev = h2fs32(vap->va_rdev); 152 } 153 /* 154 * Remove inode so that it will be reloaded by VFS_VGET and 155 * checked to see if it is an alias of an existing entry in 156 * the inode cache. 157 */ 158 vput(*vpp); 159 (*vpp)->v_type = VNON; 160 vgone(*vpp); 161 *vpp = 0; 162 return (0); 163 } 164 165 /* 166 * Open called. 167 * 168 * Just check the APPEND flag. 169 */ 170 /* ARGSUSED */ 171 int 172 ext2fs_open(v) 173 void *v; 174 { 175 struct vop_open_args /* { 176 struct vnode *a_vp; 177 int a_mode; 178 struct ucred *a_cred; 179 struct proc *a_p; 180 } */ *ap = v; 181 182 /* 183 * Files marked append-only must be opened for appending. 184 */ 185 if ((VTOI(ap->a_vp)->i_e2fs_flags & EXT2_APPEND) && 186 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) 187 return (EPERM); 188 return (0); 189 } 190 191 int 192 ext2fs_access(v) 193 void *v; 194 { 195 struct vop_access_args /* { 196 struct vnode *a_vp; 197 int a_mode; 198 struct ucred *a_cred; 199 struct proc *a_p; 200 } */ *ap = v; 201 struct vnode *vp = ap->a_vp; 202 struct inode *ip = VTOI(vp); 203 mode_t mode = ap->a_mode; 204 205 206 /* 207 * Disallow write attempts on read-only file systems; 208 * unless the file is a socket, fifo, or a block or 209 * character device resident on the file system. 210 */ 211 if (mode & VWRITE) { 212 switch (vp->v_type) { 213 case VDIR: 214 case VLNK: 215 case VREG: 216 if (vp->v_mount->mnt_flag & MNT_RDONLY) 217 return (EROFS); 218 break; 219 default: 220 break; 221 } 222 } 223 224 /* If immutable bit set, nobody gets to write it. */ 225 if ((mode & VWRITE) && (ip->i_e2fs_flags & EXT2_IMMUTABLE)) 226 return (EPERM); 227 228 return (vaccess(vp->v_type, ip->i_e2fs_mode & ALLPERMS, 229 ip->i_e2fs_uid, ip->i_e2fs_gid, mode, ap->a_cred)); 230 } 231 232 /* ARGSUSED */ 233 int 234 ext2fs_getattr(v) 235 void *v; 236 { 237 struct vop_getattr_args /* { 238 struct vnode *a_vp; 239 struct vattr *a_vap; 240 struct ucred *a_cred; 241 struct proc *a_p; 242 } */ *ap = v; 243 register struct vnode *vp = ap->a_vp; 244 register struct inode *ip = VTOI(vp); 245 register struct vattr *vap = ap->a_vap; 246 struct timespec ts; 247 248 TIMEVAL_TO_TIMESPEC(&time, &ts); 249 EXT2FS_ITIMES(ip, &ts, &ts, &ts); 250 /* 251 * Copy from inode table 252 */ 253 vap->va_fsid = ip->i_dev; 254 vap->va_fileid = ip->i_number; 255 vap->va_mode = ip->i_e2fs_mode & ALLPERMS; 256 vap->va_nlink = ip->i_e2fs_nlink; 257 vap->va_uid = ip->i_e2fs_uid; 258 vap->va_gid = ip->i_e2fs_gid; 259 vap->va_rdev = (dev_t)fs2h32(ip->i_din.e2fs_din.e2di_rdev); 260 vap->va_size = ip->i_e2fs_size; 261 vap->va_atime.tv_sec = ip->i_e2fs_atime; 262 vap->va_atime.tv_nsec = 0; 263 vap->va_mtime.tv_sec = ip->i_e2fs_mtime; 264 vap->va_mtime.tv_nsec = 0; 265 vap->va_ctime.tv_sec = ip->i_e2fs_ctime; 266 vap->va_ctime.tv_nsec = 0; 267 #ifdef EXT2FS_SYSTEM_FLAGS 268 vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? SF_APPEND : 0; 269 vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0; 270 #else 271 vap->va_flags = (ip->i_e2fs_flags & EXT2_APPEND) ? UF_APPEND : 0; 272 vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? UF_IMMUTABLE : 0; 273 #endif 274 vap->va_gen = ip->i_e2fs_gen; 275 /* this doesn't belong here */ 276 if (vp->v_type == VBLK) 277 vap->va_blocksize = BLKDEV_IOSIZE; 278 else if (vp->v_type == VCHR) 279 vap->va_blocksize = MAXBSIZE; 280 else 281 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize; 282 vap->va_bytes = dbtob((u_quad_t)ip->i_e2fs_nblock); 283 vap->va_type = vp->v_type; 284 vap->va_filerev = ip->i_modrev; 285 return (0); 286 } 287 288 /* 289 * Set attribute vnode op. called from several syscalls 290 */ 291 int 292 ext2fs_setattr(v) 293 void *v; 294 { 295 struct vop_setattr_args /* { 296 struct vnode *a_vp; 297 struct vattr *a_vap; 298 struct ucred *a_cred; 299 struct proc *a_p; 300 } */ *ap = v; 301 struct vattr *vap = ap->a_vap; 302 struct vnode *vp = ap->a_vp; 303 struct inode *ip = VTOI(vp); 304 struct ucred *cred = ap->a_cred; 305 struct proc *p = ap->a_p; 306 int error; 307 308 /* 309 * Check for unsettable attributes. 310 */ 311 if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) || 312 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 313 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 314 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 315 return (EINVAL); 316 } 317 if (vap->va_flags != VNOVAL) { 318 if (vp->v_mount->mnt_flag & MNT_RDONLY) 319 return (EROFS); 320 if (cred->cr_uid != ip->i_e2fs_uid && 321 (error = suser(cred, &p->p_acflag))) 322 return (error); 323 #ifdef EXT2FS_SYSTEM_FLAGS 324 if (cred->cr_uid == 0) { 325 if ((ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE)) && 326 securelevel > 0) 327 return (EPERM); 328 ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE); 329 ip->i_e2fs_flags |= (vap->va_flags & SF_APPEND) ? EXT2_APPEND : 0 | 330 (vap->va_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE: 0; 331 } else { 332 return (EPERM); 333 } 334 #else 335 ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE); 336 ip->i_e2fs_flags |= (vap->va_flags & UF_APPEND) ? EXT2_APPEND : 0 | 337 (vap->va_flags & UF_IMMUTABLE) ? EXT2_IMMUTABLE: 0; 338 #endif 339 ip->i_flag |= IN_CHANGE; 340 if (vap->va_flags & (IMMUTABLE | APPEND)) 341 return (0); 342 } 343 if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE)) 344 return (EPERM); 345 /* 346 * Go through the fields and update iff not VNOVAL. 347 */ 348 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { 349 if (vp->v_mount->mnt_flag & MNT_RDONLY) 350 return (EROFS); 351 error = ext2fs_chown(vp, vap->va_uid, vap->va_gid, cred, p); 352 if (error) 353 return (error); 354 } 355 if (vap->va_size != VNOVAL) { 356 /* 357 * Disallow write attempts on read-only file systems; 358 * unless the file is a socket, fifo, or a block or 359 * character device resident on the file system. 360 */ 361 switch (vp->v_type) { 362 case VDIR: 363 return (EISDIR); 364 case VLNK: 365 case VREG: 366 if (vp->v_mount->mnt_flag & MNT_RDONLY) 367 return (EROFS); 368 default: 369 break; 370 } 371 error = VOP_TRUNCATE(vp, vap->va_size, 0, cred, p); 372 if (error) 373 return (error); 374 } 375 ip = VTOI(vp); 376 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 377 if (vp->v_mount->mnt_flag & MNT_RDONLY) 378 return (EROFS); 379 if (cred->cr_uid != ip->i_e2fs_uid && 380 (error = suser(cred, &p->p_acflag)) && 381 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || 382 (error = VOP_ACCESS(vp, VWRITE, cred, p)))) 383 return (error); 384 if (vap->va_atime.tv_sec != VNOVAL) 385 if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) 386 ip->i_flag |= IN_ACCESS; 387 if (vap->va_mtime.tv_sec != VNOVAL) 388 ip->i_flag |= IN_CHANGE | IN_UPDATE; 389 error = VOP_UPDATE(vp, &vap->va_atime, &vap->va_mtime, 1); 390 if (error) 391 return (error); 392 } 393 error = 0; 394 if (vap->va_mode != (mode_t)VNOVAL) { 395 if (vp->v_mount->mnt_flag & MNT_RDONLY) 396 return (EROFS); 397 error = ext2fs_chmod(vp, (int)vap->va_mode, cred, p); 398 } 399 return (error); 400 } 401 402 /* 403 * Change the mode on a file. 404 * Inode must be locked before calling. 405 */ 406 static int 407 ext2fs_chmod(vp, mode, cred, p) 408 register struct vnode *vp; 409 register int mode; 410 register struct ucred *cred; 411 struct proc *p; 412 { 413 register struct inode *ip = VTOI(vp); 414 int error; 415 416 if (cred->cr_uid != ip->i_e2fs_uid && 417 (error = suser(cred, &p->p_acflag))) 418 return (error); 419 if (cred->cr_uid) { 420 if (vp->v_type != VDIR && (mode & S_ISTXT)) 421 return (EFTYPE); 422 if (!groupmember(ip->i_e2fs_gid, cred) && (mode & ISGID)) 423 return (EPERM); 424 } 425 ip->i_e2fs_mode &= ~ALLPERMS; 426 ip->i_e2fs_mode |= (mode & ALLPERMS); 427 ip->i_flag |= IN_CHANGE; 428 #if defined(UVM) 429 if ((vp->v_flag & VTEXT) && (ip->i_e2fs_mode & S_ISTXT) == 0) 430 (void) uvm_vnp_uncache(vp); 431 #else 432 if ((vp->v_flag & VTEXT) && (ip->i_e2fs_mode & S_ISTXT) == 0) 433 (void) vnode_pager_uncache(vp); 434 #endif 435 return (0); 436 } 437 438 /* 439 * Perform chown operation on inode ip; 440 * inode must be locked prior to call. 441 */ 442 static int 443 ext2fs_chown(vp, uid, gid, cred, p) 444 register struct vnode *vp; 445 uid_t uid; 446 gid_t gid; 447 struct ucred *cred; 448 struct proc *p; 449 { 450 register struct inode *ip = VTOI(vp); 451 uid_t ouid; 452 gid_t ogid; 453 int error = 0; 454 455 if (uid == (uid_t)VNOVAL) 456 uid = ip->i_e2fs_uid; 457 if (gid == (gid_t)VNOVAL) 458 gid = ip->i_e2fs_gid; 459 /* 460 * If we don't own the file, are trying to change the owner 461 * of the file, or are not a member of the target group, 462 * the caller must be superuser or the call fails. 463 */ 464 if ((cred->cr_uid != ip->i_e2fs_uid || uid != ip->i_e2fs_uid || 465 (gid != ip->i_e2fs_gid && !groupmember((gid_t)gid, cred))) && 466 (error = suser(cred, &p->p_acflag))) 467 return (error); 468 ogid = ip->i_e2fs_gid; 469 ouid = ip->i_e2fs_uid; 470 471 ip->i_e2fs_gid = gid; 472 ip->i_e2fs_uid = uid; 473 if (ouid != uid || ogid != gid) 474 ip->i_flag |= IN_CHANGE; 475 if (ouid != uid && cred->cr_uid != 0) 476 ip->i_e2fs_mode &= ~ISUID; 477 if (ogid != gid && cred->cr_uid != 0) 478 ip->i_e2fs_mode &= ~ISGID; 479 return (0); 480 } 481 482 int 483 ext2fs_remove(v) 484 void *v; 485 { 486 struct vop_remove_args /* { 487 struct vnode *a_dvp; 488 struct vnode *a_vp; 489 struct componentname *a_cnp; 490 } */ *ap = v; 491 struct inode *ip; 492 struct vnode *vp = ap->a_vp; 493 struct vnode *dvp = ap->a_dvp; 494 int error; 495 496 ip = VTOI(vp); 497 if (vp->v_type == VDIR || 498 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) || 499 (VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) { 500 error = EPERM; 501 goto out; 502 } 503 error = ext2fs_dirremove(dvp, ap->a_cnp); 504 if (error == 0) { 505 ip->i_e2fs_nlink--; 506 ip->i_flag |= IN_CHANGE; 507 } 508 out: 509 if (dvp == vp) 510 vrele(vp); 511 else 512 vput(vp); 513 vput(dvp); 514 return (error); 515 } 516 517 /* 518 * link vnode call 519 */ 520 int 521 ext2fs_link(v) 522 void *v; 523 { 524 struct vop_link_args /* { 525 struct vnode *a_dvp; 526 struct vnode *a_vp; 527 struct componentname *a_cnp; 528 } */ *ap = v; 529 struct vnode *dvp = ap->a_dvp; 530 struct vnode *vp = ap->a_vp; 531 struct componentname *cnp = ap->a_cnp; 532 struct inode *ip; 533 struct timespec ts; 534 int error; 535 536 #ifdef DIAGNOSTIC 537 if ((cnp->cn_flags & HASBUF) == 0) 538 panic("ext2fs_link: no name"); 539 #endif 540 if (vp->v_type == VDIR) { 541 VOP_ABORTOP(dvp, cnp); 542 error = EISDIR; 543 goto out2; 544 } 545 if (dvp->v_mount != vp->v_mount) { 546 VOP_ABORTOP(dvp, cnp); 547 error = EXDEV; 548 goto out2; 549 } 550 if (dvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) { 551 VOP_ABORTOP(dvp, cnp); 552 goto out2; 553 } 554 ip = VTOI(vp); 555 if ((nlink_t)ip->i_e2fs_nlink >= LINK_MAX) { 556 VOP_ABORTOP(dvp, cnp); 557 error = EMLINK; 558 goto out1; 559 } 560 if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) { 561 VOP_ABORTOP(dvp, cnp); 562 error = EPERM; 563 goto out1; 564 } 565 ip->i_e2fs_nlink++; 566 ip->i_flag |= IN_CHANGE; 567 TIMEVAL_TO_TIMESPEC(&time, &ts); 568 error = VOP_UPDATE(vp, &ts, &ts, 1); 569 if (!error) 570 error = ext2fs_direnter(ip, dvp, cnp); 571 if (error) { 572 ip->i_e2fs_nlink--; 573 ip->i_flag |= IN_CHANGE; 574 } 575 FREE(cnp->cn_pnbuf, M_NAMEI); 576 out1: 577 if (dvp != vp) 578 VOP_UNLOCK(vp, 0); 579 out2: 580 vput(dvp); 581 return (error); 582 } 583 584 /* 585 * Rename system call. 586 * rename("foo", "bar"); 587 * is essentially 588 * unlink("bar"); 589 * link("foo", "bar"); 590 * unlink("foo"); 591 * but ``atomically''. Can't do full commit without saving state in the 592 * inode on disk which isn't feasible at this time. Best we can do is 593 * always guarantee the target exists. 594 * 595 * Basic algorithm is: 596 * 597 * 1) Bump link count on source while we're linking it to the 598 * target. This also ensure the inode won't be deleted out 599 * from underneath us while we work (it may be truncated by 600 * a concurrent `trunc' or `open' for creation). 601 * 2) Link source to destination. If destination already exists, 602 * delete it first. 603 * 3) Unlink source reference to inode if still around. If a 604 * directory was moved and the parent of the destination 605 * is different from the source, patch the ".." entry in the 606 * directory. 607 */ 608 int 609 ext2fs_rename(v) 610 void *v; 611 { 612 struct vop_rename_args /* { 613 struct vnode *a_fdvp; 614 struct vnode *a_fvp; 615 struct componentname *a_fcnp; 616 struct vnode *a_tdvp; 617 struct vnode *a_tvp; 618 struct componentname *a_tcnp; 619 } */ *ap = v; 620 struct vnode *tvp = ap->a_tvp; 621 register struct vnode *tdvp = ap->a_tdvp; 622 struct vnode *fvp = ap->a_fvp; 623 struct vnode *fdvp = ap->a_fdvp; 624 struct componentname *tcnp = ap->a_tcnp; 625 struct componentname *fcnp = ap->a_fcnp; 626 struct inode *ip, *xp, *dp; 627 struct ext2fs_dirtemplate dirbuf; 628 struct timespec ts; 629 int doingdirectory = 0, oldparent = 0, newparent = 0; 630 int error = 0; 631 u_char namlen; 632 633 #ifdef DIAGNOSTIC 634 if ((tcnp->cn_flags & HASBUF) == 0 || 635 (fcnp->cn_flags & HASBUF) == 0) 636 panic("ext2fs_rename: no name"); 637 #endif 638 /* 639 * Check for cross-device rename. 640 */ 641 if ((fvp->v_mount != tdvp->v_mount) || 642 (tvp && (fvp->v_mount != tvp->v_mount))) { 643 error = EXDEV; 644 abortit: 645 VOP_ABORTOP(tdvp, tcnp); /* XXX, why not in NFS? */ 646 if (tdvp == tvp) 647 vrele(tdvp); 648 else 649 vput(tdvp); 650 if (tvp) 651 vput(tvp); 652 VOP_ABORTOP(fdvp, fcnp); /* XXX, why not in NFS? */ 653 vrele(fdvp); 654 vrele(fvp); 655 return (error); 656 } 657 658 /* 659 * Check if just deleting a link name. 660 */ 661 if (tvp && ((VTOI(tvp)->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) || 662 (VTOI(tdvp)->i_e2fs_flags & EXT2_APPEND))) { 663 error = EPERM; 664 goto abortit; 665 } 666 if (fvp == tvp) { 667 if (fvp->v_type == VDIR) { 668 error = EINVAL; 669 goto abortit; 670 } 671 672 /* Release destination completely. */ 673 VOP_ABORTOP(tdvp, tcnp); 674 vput(tdvp); 675 vput(tvp); 676 677 /* Delete source. */ 678 vrele(fdvp); 679 vrele(fvp); 680 fcnp->cn_flags &= ~MODMASK; 681 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF; 682 if ((fcnp->cn_flags & SAVESTART) == 0) 683 panic("ext2fs_rename: lost from startdir"); 684 fcnp->cn_nameiop = DELETE; 685 (void) relookup(fdvp, &fvp, fcnp); 686 return (VOP_REMOVE(fdvp, fvp, fcnp)); 687 } 688 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0) 689 goto abortit; 690 dp = VTOI(fdvp); 691 ip = VTOI(fvp); 692 if ((ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) || 693 (dp->i_e2fs_flags & EXT2_APPEND)) { 694 VOP_UNLOCK(fvp, 0); 695 error = EPERM; 696 goto abortit; 697 } 698 if ((ip->i_e2fs_mode & IFMT) == IFDIR) { 699 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc); 700 if (!error && tvp) 701 error = VOP_ACCESS(tvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc); 702 if (error) { 703 VOP_UNLOCK(fvp, 0); 704 error = EACCES; 705 goto abortit; 706 } 707 /* 708 * Avoid ".", "..", and aliases of "." for obvious reasons. 709 */ 710 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') || 711 dp == ip || 712 (fcnp->cn_flags&ISDOTDOT) || 713 (tcnp->cn_flags & ISDOTDOT) || 714 (ip->i_flag & IN_RENAME)) { 715 VOP_UNLOCK(fvp, 0); 716 error = EINVAL; 717 goto abortit; 718 } 719 ip->i_flag |= IN_RENAME; 720 oldparent = dp->i_number; 721 doingdirectory++; 722 } 723 vrele(fdvp); 724 725 /* 726 * When the target exists, both the directory 727 * and target vnodes are returned locked. 728 */ 729 dp = VTOI(tdvp); 730 xp = NULL; 731 if (tvp) 732 xp = VTOI(tvp); 733 734 /* 735 * 1) Bump link count while we're moving stuff 736 * around. If we crash somewhere before 737 * completing our work, the link count 738 * may be wrong, but correctable. 739 */ 740 ip->i_e2fs_nlink++; 741 ip->i_flag |= IN_CHANGE; 742 TIMEVAL_TO_TIMESPEC(&time, &ts); 743 if ((error = VOP_UPDATE(fvp, &ts, &ts, 1)) != 0) { 744 VOP_UNLOCK(fvp, 0); 745 goto bad; 746 } 747 748 /* 749 * If ".." must be changed (ie the directory gets a new 750 * parent) then the source directory must not be in the 751 * directory heirarchy above the target, as this would 752 * orphan everything below the source directory. Also 753 * the user must have write permission in the source so 754 * as to be able to change "..". We must repeat the call 755 * to namei, as the parent directory is unlocked by the 756 * call to checkpath(). 757 */ 758 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_proc); 759 VOP_UNLOCK(fvp, 0); 760 if (oldparent != dp->i_number) 761 newparent = dp->i_number; 762 if (doingdirectory && newparent) { 763 if (error) /* write access check above */ 764 goto bad; 765 if (xp != NULL) 766 vput(tvp); 767 error = ext2fs_checkpath(ip, dp, tcnp->cn_cred); 768 if (error != 0) 769 goto out; 770 if ((tcnp->cn_flags & SAVESTART) == 0) 771 panic("ext2fs_rename: lost to startdir"); 772 if ((error = relookup(tdvp, &tvp, tcnp)) != 0) 773 goto out; 774 dp = VTOI(tdvp); 775 xp = NULL; 776 if (tvp) 777 xp = VTOI(tvp); 778 } 779 /* 780 * 2) If target doesn't exist, link the target 781 * to the source and unlink the source. 782 * Otherwise, rewrite the target directory 783 * entry to reference the source inode and 784 * expunge the original entry's existence. 785 */ 786 if (xp == NULL) { 787 if (dp->i_dev != ip->i_dev) 788 panic("rename: EXDEV"); 789 /* 790 * Account for ".." in new directory. 791 * When source and destination have the same 792 * parent we don't fool with the link count. 793 */ 794 if (doingdirectory && newparent) { 795 if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) { 796 error = EMLINK; 797 goto bad; 798 } 799 dp->i_e2fs_nlink++; 800 dp->i_flag |= IN_CHANGE; 801 if ((error = VOP_UPDATE(tdvp, &ts, &ts, 1)) != 0) 802 goto bad; 803 } 804 error = ext2fs_direnter(ip, tdvp, tcnp); 805 if (error != 0) { 806 if (doingdirectory && newparent) { 807 dp->i_e2fs_nlink--; 808 dp->i_flag |= IN_CHANGE; 809 (void)VOP_UPDATE(tdvp, &ts, &ts, 1); 810 } 811 goto bad; 812 } 813 vput(tdvp); 814 } else { 815 if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) 816 panic("rename: EXDEV"); 817 /* 818 * Short circuit rename(foo, foo). 819 */ 820 if (xp->i_number == ip->i_number) 821 panic("rename: same file"); 822 /* 823 * If the parent directory is "sticky", then the user must 824 * own the parent directory, or the destination of the rename, 825 * otherwise the destination may not be changed (except by 826 * root). This implements append-only directories. 827 */ 828 if ((dp->i_e2fs_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 && 829 tcnp->cn_cred->cr_uid != dp->i_e2fs_uid && 830 xp->i_e2fs_uid != tcnp->cn_cred->cr_uid) { 831 error = EPERM; 832 goto bad; 833 } 834 /* 835 * Target must be empty if a directory and have no links 836 * to it. Also, ensure source and target are compatible 837 * (both directories, or both not directories). 838 */ 839 if ((xp->i_e2fs_mode & IFMT) == IFDIR) { 840 if (!ext2fs_dirempty(xp, dp->i_number, tcnp->cn_cred) || 841 xp->i_e2fs_nlink > 2) { 842 error = ENOTEMPTY; 843 goto bad; 844 } 845 if (!doingdirectory) { 846 error = ENOTDIR; 847 goto bad; 848 } 849 cache_purge(tdvp); 850 } else if (doingdirectory) { 851 error = EISDIR; 852 goto bad; 853 } 854 error = ext2fs_dirrewrite(dp, ip, tcnp); 855 if (error != 0) 856 goto bad; 857 /* 858 * If the target directory is in the same 859 * directory as the source directory, 860 * decrement the link count on the parent 861 * of the target directory. 862 */ 863 if (doingdirectory && !newparent) { 864 dp->i_e2fs_nlink--; 865 dp->i_flag |= IN_CHANGE; 866 } 867 vput(tdvp); 868 /* 869 * Adjust the link count of the target to 870 * reflect the dirrewrite above. If this is 871 * a directory it is empty and there are 872 * no links to it, so we can squash the inode and 873 * any space associated with it. We disallowed 874 * renaming over top of a directory with links to 875 * it above, as the remaining link would point to 876 * a directory without "." or ".." entries. 877 */ 878 xp->i_e2fs_nlink--; 879 if (doingdirectory) { 880 if (--xp->i_e2fs_nlink != 0) 881 panic("rename: linked directory"); 882 error = VOP_TRUNCATE(tvp, (off_t)0, IO_SYNC, 883 tcnp->cn_cred, tcnp->cn_proc); 884 } 885 xp->i_flag |= IN_CHANGE; 886 vput(tvp); 887 xp = NULL; 888 } 889 890 /* 891 * 3) Unlink the source. 892 */ 893 fcnp->cn_flags &= ~MODMASK; 894 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF; 895 if ((fcnp->cn_flags & SAVESTART) == 0) 896 panic("ext2fs_rename: lost from startdir"); 897 (void) relookup(fdvp, &fvp, fcnp); 898 if (fvp != NULL) { 899 xp = VTOI(fvp); 900 dp = VTOI(fdvp); 901 } else { 902 /* 903 * From name has disappeared. 904 */ 905 if (doingdirectory) 906 panic("ext2fs_rename: lost dir entry"); 907 vrele(ap->a_fvp); 908 return (0); 909 } 910 /* 911 * Ensure that the directory entry still exists and has not 912 * changed while the new name has been entered. If the source is 913 * a file then the entry may have been unlinked or renamed. In 914 * either case there is no further work to be done. If the source 915 * is a directory then it cannot have been rmdir'ed; its link 916 * count of three would cause a rmdir to fail with ENOTEMPTY. 917 * The IRENAME flag ensures that it cannot be moved by another 918 * rename. 919 */ 920 if (xp != ip) { 921 if (doingdirectory) 922 panic("ext2fs_rename: lost dir entry"); 923 } else { 924 /* 925 * If the source is a directory with a 926 * new parent, the link count of the old 927 * parent directory must be decremented 928 * and ".." set to point to the new parent. 929 */ 930 if (doingdirectory && newparent) { 931 dp->i_e2fs_nlink--; 932 dp->i_flag |= IN_CHANGE; 933 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf, 934 sizeof (struct ext2fs_dirtemplate), (off_t)0, 935 UIO_SYSSPACE, IO_NODELOCKED, 936 tcnp->cn_cred, (int *)0, (struct proc *)0); 937 if (error == 0) { 938 namlen = fs2h16(dirbuf.dotdot_namlen); 939 if (namlen != 2 || 940 dirbuf.dotdot_name[0] != '.' || 941 dirbuf.dotdot_name[1] != '.') { 942 ufs_dirbad(xp, (doff_t)12, 943 "ext2fs_rename: mangled dir"); 944 } else { 945 dirbuf.dotdot_ino = h2fs32(newparent); 946 (void) vn_rdwr(UIO_WRITE, fvp, 947 (caddr_t)&dirbuf, 948 sizeof (struct dirtemplate), 949 (off_t)0, UIO_SYSSPACE, 950 IO_NODELOCKED|IO_SYNC, 951 tcnp->cn_cred, (int *)0, 952 (struct proc *)0); 953 cache_purge(fdvp); 954 } 955 } 956 } 957 error = ext2fs_dirremove(fdvp, fcnp); 958 if (!error) { 959 xp->i_e2fs_nlink--; 960 xp->i_flag |= IN_CHANGE; 961 } 962 xp->i_flag &= ~IN_RENAME; 963 } 964 if (dp) 965 vput(fdvp); 966 if (xp) 967 vput(fvp); 968 vrele(ap->a_fvp); 969 return (error); 970 971 bad: 972 if (xp) 973 vput(ITOV(xp)); 974 vput(ITOV(dp)); 975 out: 976 if (doingdirectory) 977 ip->i_flag &= ~IN_RENAME; 978 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) { 979 ip->i_e2fs_nlink--; 980 ip->i_flag |= IN_CHANGE; 981 vput(fvp); 982 } else 983 vrele(fvp); 984 return (error); 985 } 986 987 /* 988 * Mkdir system call 989 */ 990 int 991 ext2fs_mkdir(v) 992 void *v; 993 { 994 struct vop_mkdir_args /* { 995 struct vnode *a_dvp; 996 struct vnode **a_vpp; 997 struct componentname *a_cnp; 998 struct vattr *a_vap; 999 } */ *ap = v; 1000 register struct vnode *dvp = ap->a_dvp; 1001 register struct vattr *vap = ap->a_vap; 1002 register struct componentname *cnp = ap->a_cnp; 1003 register struct inode *ip, *dp; 1004 struct vnode *tvp; 1005 struct ext2fs_dirtemplate dirtemplate; 1006 struct timespec ts; 1007 int error, dmode; 1008 1009 #ifdef DIAGNOSTIC 1010 if ((cnp->cn_flags & HASBUF) == 0) 1011 panic("ext2fs_mkdir: no name"); 1012 #endif 1013 dp = VTOI(dvp); 1014 if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) { 1015 error = EMLINK; 1016 goto out; 1017 } 1018 dmode = vap->va_mode & ACCESSPERMS; 1019 dmode |= IFDIR; 1020 /* 1021 * Must simulate part of ext2fs_makeinode here to acquire the inode, 1022 * but not have it entered in the parent directory. The entry is 1023 * made later after writing "." and ".." entries. 1024 */ 1025 if ((error = VOP_VALLOC(dvp, dmode, cnp->cn_cred, &tvp)) != 0) 1026 goto out; 1027 ip = VTOI(tvp); 1028 ip->i_e2fs_uid = cnp->cn_cred->cr_uid; 1029 ip->i_e2fs_gid = dp->i_e2fs_gid; 1030 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 1031 ip->i_e2fs_mode = dmode; 1032 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */ 1033 ip->i_e2fs_nlink = 2; 1034 TIMEVAL_TO_TIMESPEC(&time, &ts); 1035 error = VOP_UPDATE(tvp, &ts, &ts, 1); 1036 1037 /* 1038 * Bump link count in parent directory 1039 * to reflect work done below. Should 1040 * be done before reference is created 1041 * so reparation is possible if we crash. 1042 */ 1043 dp->i_e2fs_nlink++; 1044 dp->i_flag |= IN_CHANGE; 1045 if ((error = VOP_UPDATE(dvp, &ts, &ts, 1)) != 0) 1046 goto bad; 1047 1048 /* Initialize directory with "." and ".." from static template. */ 1049 bzero(&dirtemplate, sizeof(dirtemplate)); 1050 dirtemplate.dot_ino = h2fs32(ip->i_number); 1051 dirtemplate.dot_reclen = h2fs16(12); 1052 dirtemplate.dot_namlen = h2fs16(1); 1053 dirtemplate.dot_name[0] = '.'; 1054 dirtemplate.dotdot_ino = h2fs32(dp->i_number); 1055 dirtemplate.dotdot_reclen = h2fs16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12); 1056 dirtemplate.dotdot_namlen = h2fs16(2); 1057 dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.'; 1058 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate, 1059 sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, 1060 IO_NODELOCKED|IO_SYNC, cnp->cn_cred, (int *)0, (struct proc *)0); 1061 if (error) { 1062 dp->i_e2fs_nlink--; 1063 dp->i_flag |= IN_CHANGE; 1064 goto bad; 1065 } 1066 if (VTOI(dvp)->i_e2fs->e2fs_bsize > 1067 VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize) 1068 panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */ 1069 else { 1070 ip->i_e2fs_size = VTOI(dvp)->i_e2fs->e2fs_bsize; 1071 ip->i_flag |= IN_CHANGE; 1072 } 1073 1074 /* Directory set up, now install it's entry in the parent directory. */ 1075 error = ext2fs_direnter(ip, dvp, cnp); 1076 if (error != 0) { 1077 dp->i_e2fs_nlink--; 1078 dp->i_flag |= IN_CHANGE; 1079 } 1080 bad: 1081 /* 1082 * No need to do an explicit VOP_TRUNCATE here, vrele will do this 1083 * for us because we set the link count to 0. 1084 */ 1085 if (error) { 1086 ip->i_e2fs_nlink = 0; 1087 ip->i_flag |= IN_CHANGE; 1088 vput(tvp); 1089 } else 1090 *ap->a_vpp = tvp; 1091 out: 1092 FREE(cnp->cn_pnbuf, M_NAMEI); 1093 vput(dvp); 1094 return (error); 1095 } 1096 1097 /* 1098 * Rmdir system call. 1099 */ 1100 int 1101 ext2fs_rmdir(v) 1102 void *v; 1103 { 1104 struct vop_rmdir_args /* { 1105 struct vnode *a_dvp; 1106 struct vnode *a_vp; 1107 struct componentname *a_cnp; 1108 } */ *ap = v; 1109 register struct vnode *vp = ap->a_vp; 1110 register struct vnode *dvp = ap->a_dvp; 1111 register struct componentname *cnp = ap->a_cnp; 1112 register struct inode *ip, *dp; 1113 int error; 1114 1115 ip = VTOI(vp); 1116 dp = VTOI(dvp); 1117 /* 1118 * No rmdir "." please. 1119 */ 1120 if (dp == ip) { 1121 vrele(dvp); 1122 vput(vp); 1123 return (EINVAL); 1124 } 1125 /* 1126 * Verify the directory is empty (and valid). 1127 * (Rmdir ".." won't be valid since 1128 * ".." will contain a reference to 1129 * the current directory and thus be 1130 * non-empty.) 1131 */ 1132 error = 0; 1133 if (ip->i_e2fs_nlink != 2 || 1134 !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) { 1135 error = ENOTEMPTY; 1136 goto out; 1137 } 1138 if ((dp->i_e2fs_flags & EXT2_APPEND) || 1139 (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) { 1140 error = EPERM; 1141 goto out; 1142 } 1143 /* 1144 * Delete reference to directory before purging 1145 * inode. If we crash in between, the directory 1146 * will be reattached to lost+found, 1147 */ 1148 error = ext2fs_dirremove(dvp, cnp); 1149 if (error != 0) 1150 goto out; 1151 dp->i_e2fs_nlink--; 1152 dp->i_flag |= IN_CHANGE; 1153 cache_purge(dvp); 1154 vput(dvp); 1155 dvp = NULL; 1156 /* 1157 * Truncate inode. The only stuff left 1158 * in the directory is "." and "..". The 1159 * "." reference is inconsequential since 1160 * we're quashing it. The ".." reference 1161 * has already been adjusted above. We've 1162 * removed the "." reference and the reference 1163 * in the parent directory, but there may be 1164 * other hard links so decrement by 2 and 1165 * worry about them later. 1166 */ 1167 ip->i_e2fs_nlink -= 2; 1168 error = VOP_TRUNCATE(vp, (off_t)0, IO_SYNC, cnp->cn_cred, 1169 cnp->cn_proc); 1170 cache_purge(ITOV(ip)); 1171 out: 1172 if (dvp) 1173 vput(dvp); 1174 vput(vp); 1175 return (error); 1176 } 1177 1178 /* 1179 * symlink -- make a symbolic link 1180 */ 1181 int 1182 ext2fs_symlink(v) 1183 void *v; 1184 { 1185 struct vop_symlink_args /* { 1186 struct vnode *a_dvp; 1187 struct vnode **a_vpp; 1188 struct componentname *a_cnp; 1189 struct vattr *a_vap; 1190 char *a_target; 1191 } */ *ap = v; 1192 register struct vnode *vp, **vpp = ap->a_vpp; 1193 register struct inode *ip; 1194 int len, error; 1195 1196 error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp, 1197 vpp, ap->a_cnp); 1198 if (error) 1199 return (error); 1200 vp = *vpp; 1201 len = strlen(ap->a_target); 1202 if (len < vp->v_mount->mnt_maxsymlinklen) { 1203 ip = VTOI(vp); 1204 bcopy(ap->a_target, (char *)ip->i_din.e2fs_din.e2di_shortlink, len); 1205 ip->i_e2fs_size = len; 1206 ip->i_flag |= IN_CHANGE | IN_UPDATE; 1207 } else 1208 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0, 1209 UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, (int *)0, 1210 (struct proc *)0); 1211 vput(vp); 1212 return (error); 1213 } 1214 1215 /* 1216 * Return target name of a symbolic link 1217 */ 1218 int 1219 ext2fs_readlink(v) 1220 void *v; 1221 { 1222 struct vop_readlink_args /* { 1223 struct vnode *a_vp; 1224 struct uio *a_uio; 1225 struct ucred *a_cred; 1226 } */ *ap = v; 1227 register struct vnode *vp = ap->a_vp; 1228 register struct inode *ip = VTOI(vp); 1229 int isize; 1230 1231 isize = ip->i_e2fs_size; 1232 if (isize < vp->v_mount->mnt_maxsymlinklen || 1233 (vp->v_mount->mnt_maxsymlinklen == 0 && ip->i_e2fs_nblock == 0)) { 1234 uiomove((char *)ip->i_din.e2fs_din.e2di_shortlink, isize, ap->a_uio); 1235 return (0); 1236 } 1237 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred)); 1238 } 1239 1240 /* 1241 * Advisory record locking support 1242 */ 1243 int 1244 ext2fs_advlock(v) 1245 void *v; 1246 { 1247 struct vop_advlock_args /* { 1248 struct vnode *a_vp; 1249 caddr_t a_id; 1250 int a_op; 1251 struct flock *a_fl; 1252 int a_flags; 1253 } */ *ap = v; 1254 register struct inode *ip = VTOI(ap->a_vp); 1255 1256 return (lf_advlock(&ip->i_lockf, ip->i_e2fs_size, ap->a_id, ap->a_op, 1257 ap->a_fl, ap->a_flags)); 1258 } 1259 1260 /* 1261 * Initialize the vnode associated with a new inode, handle aliased 1262 * vnodes. 1263 */ 1264 int 1265 ext2fs_vinit(mntp, specops, fifoops, vpp) 1266 struct mount *mntp; 1267 int (**specops) __P((void *)); 1268 int (**fifoops) __P((void *)); 1269 struct vnode **vpp; 1270 { 1271 struct inode *ip; 1272 struct vnode *vp, *nvp; 1273 1274 vp = *vpp; 1275 ip = VTOI(vp); 1276 switch(vp->v_type = IFTOVT(ip->i_e2fs_mode)) { 1277 case VCHR: 1278 case VBLK: 1279 vp->v_op = specops; 1280 if ((nvp = checkalias(vp, fs2h32(ip->i_din.e2fs_din.e2di_rdev), mntp)) 1281 != NULL) { 1282 /* 1283 * Discard unneeded vnode, but save its inode. 1284 * Note that the lock is carried over in the inode 1285 * to the replacement vnode. 1286 */ 1287 nvp->v_data = vp->v_data; 1288 vp->v_data = NULL; 1289 vp->v_op = spec_vnodeop_p; 1290 vrele(vp); 1291 vgone(vp); 1292 /* 1293 * Reinitialize aliased inode. 1294 */ 1295 vp = nvp; 1296 ip->i_vnode = vp; 1297 } 1298 break; 1299 case VFIFO: 1300 #ifdef FIFO 1301 vp->v_op = fifoops; 1302 break; 1303 #else 1304 return (EOPNOTSUPP); 1305 #endif 1306 case VNON: 1307 case VBAD: 1308 case VSOCK: 1309 case VLNK: 1310 case VDIR: 1311 case VREG: 1312 break; 1313 } 1314 if (ip->i_number == ROOTINO) 1315 vp->v_flag |= VROOT; 1316 /* 1317 * Initialize modrev times 1318 */ 1319 SETHIGH(ip->i_modrev, mono_time.tv_sec); 1320 SETLOW(ip->i_modrev, mono_time.tv_usec * 4294); 1321 *vpp = vp; 1322 return (0); 1323 } 1324 1325 /* 1326 * Allocate a new inode. 1327 */ 1328 int 1329 ext2fs_makeinode(mode, dvp, vpp, cnp) 1330 int mode; 1331 struct vnode *dvp; 1332 struct vnode **vpp; 1333 struct componentname *cnp; 1334 { 1335 register struct inode *ip, *pdir; 1336 struct timespec ts; 1337 struct vnode *tvp; 1338 int error; 1339 1340 pdir = VTOI(dvp); 1341 #ifdef DIAGNOSTIC 1342 if ((cnp->cn_flags & HASBUF) == 0) 1343 panic("ext2fs_makeinode: no name"); 1344 #endif 1345 *vpp = NULL; 1346 if ((mode & IFMT) == 0) 1347 mode |= IFREG; 1348 1349 if ((error = VOP_VALLOC(dvp, mode, cnp->cn_cred, &tvp)) != 0) { 1350 free(cnp->cn_pnbuf, M_NAMEI); 1351 vput(dvp); 1352 return (error); 1353 } 1354 ip = VTOI(tvp); 1355 ip->i_e2fs_gid = pdir->i_e2fs_gid; 1356 ip->i_e2fs_uid = cnp->cn_cred->cr_uid; 1357 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 1358 ip->i_e2fs_mode = mode; 1359 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */ 1360 ip->i_e2fs_nlink = 1; 1361 if ((ip->i_e2fs_mode & ISGID) && 1362 !groupmember(ip->i_e2fs_gid, cnp->cn_cred) && 1363 suser(cnp->cn_cred, NULL)) 1364 ip->i_e2fs_mode &= ~ISGID; 1365 1366 /* 1367 * Make sure inode goes to disk before directory entry. 1368 */ 1369 TIMEVAL_TO_TIMESPEC(&time, &ts); 1370 if ((error = VOP_UPDATE(tvp, &ts, &ts, 1)) != 0) 1371 goto bad; 1372 error = ext2fs_direnter(ip, dvp, cnp); 1373 if (error != 0) 1374 goto bad; 1375 if ((cnp->cn_flags & SAVESTART) == 0) 1376 FREE(cnp->cn_pnbuf, M_NAMEI); 1377 vput(dvp); 1378 *vpp = tvp; 1379 return (0); 1380 1381 bad: 1382 /* 1383 * Write error occurred trying to update the inode 1384 * or the directory so must deallocate the inode. 1385 */ 1386 free(cnp->cn_pnbuf, M_NAMEI); 1387 vput(dvp); 1388 ip->i_e2fs_nlink = 0; 1389 ip->i_flag |= IN_CHANGE; 1390 vput(tvp); 1391 return (error); 1392 } 1393 1394 /* 1395 * Reclaim an inode so that it can be used for other purposes. 1396 */ 1397 int 1398 ext2fs_reclaim(v) 1399 void *v; 1400 { 1401 struct vop_reclaim_args /* { 1402 struct vnode *a_vp; 1403 } */ *ap = v; 1404 register struct vnode *vp = ap->a_vp; 1405 struct inode *ip; 1406 extern int prtactive; 1407 1408 if (prtactive && vp->v_usecount != 0) 1409 vprint("ext2fs_reclaim: pushing active", vp); 1410 /* 1411 * Remove the inode from its hash chain. 1412 */ 1413 ip = VTOI(vp); 1414 ufs_ihashrem(ip); 1415 /* 1416 * Purge old data structures associated with the inode. 1417 */ 1418 cache_purge(vp); 1419 if (ip->i_devvp) { 1420 vrele(ip->i_devvp); 1421 ip->i_devvp = 0; 1422 } 1423 1424 FREE(vp->v_data, M_EXT2FSNODE); 1425 vp->v_data = NULL; 1426 return (0); 1427 } 1428 1429 /* Global vfs data structures for ext2fs. */ 1430 int (**ext2fs_vnodeop_p) __P((void *)); 1431 struct vnodeopv_entry_desc ext2fs_vnodeop_entries[] = { 1432 { &vop_default_desc, vn_default_error }, 1433 { &vop_lookup_desc, ext2fs_lookup }, /* lookup */ 1434 { &vop_create_desc, ext2fs_create }, /* create */ 1435 { &vop_mknod_desc, ext2fs_mknod }, /* mknod */ 1436 { &vop_open_desc, ext2fs_open }, /* open */ 1437 { &vop_close_desc, ufs_close }, /* close */ 1438 { &vop_access_desc, ext2fs_access }, /* access */ 1439 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */ 1440 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */ 1441 { &vop_read_desc, ext2fs_read }, /* read */ 1442 { &vop_write_desc, ext2fs_write }, /* write */ 1443 { &vop_lease_desc, ufs_lease_check }, /* lease */ 1444 { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */ 1445 { &vop_poll_desc, ufs_poll }, /* poll */ 1446 { &vop_revoke_desc, ufs_revoke }, /* revoke */ 1447 { &vop_mmap_desc, ufs_mmap }, /* mmap */ 1448 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */ 1449 { &vop_seek_desc, ufs_seek }, /* seek */ 1450 { &vop_remove_desc, ext2fs_remove }, /* remove */ 1451 { &vop_link_desc, ext2fs_link }, /* link */ 1452 { &vop_rename_desc, ext2fs_rename }, /* rename */ 1453 { &vop_mkdir_desc, ext2fs_mkdir }, /* mkdir */ 1454 { &vop_rmdir_desc, ext2fs_rmdir }, /* rmdir */ 1455 { &vop_symlink_desc, ext2fs_symlink }, /* symlink */ 1456 { &vop_readdir_desc, ext2fs_readdir }, /* readdir */ 1457 { &vop_readlink_desc, ext2fs_readlink }, /* readlink */ 1458 { &vop_abortop_desc, ufs_abortop }, /* abortop */ 1459 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */ 1460 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */ 1461 { &vop_lock_desc, ufs_lock }, /* lock */ 1462 { &vop_unlock_desc, ufs_unlock }, /* unlock */ 1463 { &vop_bmap_desc, ext2fs_bmap }, /* bmap */ 1464 { &vop_strategy_desc, ufs_strategy }, /* strategy */ 1465 { &vop_print_desc, ufs_print }, /* print */ 1466 { &vop_islocked_desc, ufs_islocked }, /* islocked */ 1467 { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */ 1468 { &vop_advlock_desc, ext2fs_advlock }, /* advlock */ 1469 { &vop_blkatoff_desc, ext2fs_blkatoff }, /* blkatoff */ 1470 { &vop_valloc_desc, ext2fs_valloc }, /* valloc */ 1471 { &vop_vfree_desc, ext2fs_vfree }, /* vfree */ 1472 { &vop_truncate_desc, ext2fs_truncate }, /* truncate */ 1473 { &vop_update_desc, ext2fs_update }, /* update */ 1474 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ 1475 { (struct vnodeop_desc*)NULL, (int(*) __P((void*)))NULL } 1476 }; 1477 struct vnodeopv_desc ext2fs_vnodeop_opv_desc = 1478 { &ext2fs_vnodeop_p, ext2fs_vnodeop_entries }; 1479 1480 int (**ext2fs_specop_p) __P((void *)); 1481 struct vnodeopv_entry_desc ext2fs_specop_entries[] = { 1482 { &vop_default_desc, vn_default_error }, 1483 { &vop_lookup_desc, spec_lookup }, /* lookup */ 1484 { &vop_create_desc, spec_create }, /* create */ 1485 { &vop_mknod_desc, spec_mknod }, /* mknod */ 1486 { &vop_open_desc, spec_open }, /* open */ 1487 { &vop_close_desc, ufsspec_close }, /* close */ 1488 { &vop_access_desc, ext2fs_access }, /* access */ 1489 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */ 1490 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */ 1491 { &vop_read_desc, ufsspec_read }, /* read */ 1492 { &vop_write_desc, ufsspec_write }, /* write */ 1493 { &vop_lease_desc, spec_lease_check }, /* lease */ 1494 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */ 1495 { &vop_poll_desc, spec_poll }, /* poll */ 1496 { &vop_revoke_desc, spec_revoke }, /* revoke */ 1497 { &vop_mmap_desc, spec_mmap }, /* mmap */ 1498 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */ 1499 { &vop_seek_desc, spec_seek }, /* seek */ 1500 { &vop_remove_desc, spec_remove }, /* remove */ 1501 { &vop_link_desc, spec_link }, /* link */ 1502 { &vop_rename_desc, spec_rename }, /* rename */ 1503 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */ 1504 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */ 1505 { &vop_symlink_desc, spec_symlink }, /* symlink */ 1506 { &vop_readdir_desc, spec_readdir }, /* readdir */ 1507 { &vop_readlink_desc, spec_readlink }, /* readlink */ 1508 { &vop_abortop_desc, spec_abortop }, /* abortop */ 1509 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */ 1510 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */ 1511 { &vop_lock_desc, ufs_lock }, /* lock */ 1512 { &vop_unlock_desc, ufs_unlock }, /* unlock */ 1513 { &vop_bmap_desc, spec_bmap }, /* bmap */ 1514 { &vop_strategy_desc, spec_strategy }, /* strategy */ 1515 { &vop_print_desc, ufs_print }, /* print */ 1516 { &vop_islocked_desc, ufs_islocked }, /* islocked */ 1517 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */ 1518 { &vop_advlock_desc, spec_advlock }, /* advlock */ 1519 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */ 1520 { &vop_valloc_desc, spec_valloc }, /* valloc */ 1521 { &vop_vfree_desc, ext2fs_vfree }, /* vfree */ 1522 { &vop_truncate_desc, spec_truncate }, /* truncate */ 1523 { &vop_update_desc, ext2fs_update }, /* update */ 1524 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ 1525 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL } 1526 }; 1527 struct vnodeopv_desc ext2fs_specop_opv_desc = 1528 { &ext2fs_specop_p, ext2fs_specop_entries }; 1529 1530 #ifdef FIFO 1531 int (**ext2fs_fifoop_p) __P((void *)); 1532 struct vnodeopv_entry_desc ext2fs_fifoop_entries[] = { 1533 { &vop_default_desc, vn_default_error }, 1534 { &vop_lookup_desc, fifo_lookup }, /* lookup */ 1535 { &vop_create_desc, fifo_create }, /* create */ 1536 { &vop_mknod_desc, fifo_mknod }, /* mknod */ 1537 { &vop_open_desc, fifo_open }, /* open */ 1538 { &vop_close_desc, ufsfifo_close }, /* close */ 1539 { &vop_access_desc, ext2fs_access }, /* access */ 1540 { &vop_getattr_desc, ext2fs_getattr }, /* getattr */ 1541 { &vop_setattr_desc, ext2fs_setattr }, /* setattr */ 1542 { &vop_read_desc, ufsfifo_read }, /* read */ 1543 { &vop_write_desc, ufsfifo_write }, /* write */ 1544 { &vop_lease_desc, fifo_lease_check }, /* lease */ 1545 { &vop_ioctl_desc, fifo_ioctl }, /* ioctl */ 1546 { &vop_poll_desc, fifo_poll }, /* poll */ 1547 { &vop_revoke_desc, fifo_revoke }, /* revoke */ 1548 { &vop_mmap_desc, fifo_mmap }, /* mmap */ 1549 { &vop_fsync_desc, ext2fs_fsync }, /* fsync */ 1550 { &vop_seek_desc, fifo_seek }, /* seek */ 1551 { &vop_remove_desc, fifo_remove }, /* remove */ 1552 { &vop_link_desc, fifo_link }, /* link */ 1553 { &vop_rename_desc, fifo_rename }, /* rename */ 1554 { &vop_mkdir_desc, fifo_mkdir }, /* mkdir */ 1555 { &vop_rmdir_desc, fifo_rmdir }, /* rmdir */ 1556 { &vop_symlink_desc, fifo_symlink }, /* symlink */ 1557 { &vop_readdir_desc, fifo_readdir }, /* readdir */ 1558 { &vop_readlink_desc, fifo_readlink }, /* readlink */ 1559 { &vop_abortop_desc, fifo_abortop }, /* abortop */ 1560 { &vop_inactive_desc, ext2fs_inactive }, /* inactive */ 1561 { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */ 1562 { &vop_lock_desc, ufs_lock }, /* lock */ 1563 { &vop_unlock_desc, ufs_unlock }, /* unlock */ 1564 { &vop_bmap_desc, fifo_bmap }, /* bmap */ 1565 { &vop_strategy_desc, fifo_strategy }, /* strategy */ 1566 { &vop_print_desc, ufs_print }, /* print */ 1567 { &vop_islocked_desc, ufs_islocked }, /* islocked */ 1568 { &vop_pathconf_desc, fifo_pathconf }, /* pathconf */ 1569 { &vop_advlock_desc, fifo_advlock }, /* advlock */ 1570 { &vop_blkatoff_desc, fifo_blkatoff }, /* blkatoff */ 1571 { &vop_valloc_desc, fifo_valloc }, /* valloc */ 1572 { &vop_vfree_desc, ext2fs_vfree }, /* vfree */ 1573 { &vop_truncate_desc, fifo_truncate }, /* truncate */ 1574 { &vop_update_desc, ext2fs_update }, /* update */ 1575 { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ 1576 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL } 1577 }; 1578 struct vnodeopv_desc ext2fs_fifoop_opv_desc = 1579 { &ext2fs_fifoop_p, ext2fs_fifoop_entries }; 1580 #endif /* FIFO */ 1581