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