1 /*- 2 * modified for EXT2FS support in Lites 1.1 3 * 4 * Aug 1995, Godmar Back (gback@cs.utah.edu) 5 * University of Utah, Department of Computer Science 6 */ 7 /*- 8 * Copyright (c) 1982, 1986, 1989, 1993 9 * The Regents of the University of California. All rights reserved. 10 * (c) UNIX System Laboratories, Inc. 11 * All or some portions of this file are derived from material licensed 12 * to the University of California by American Telephone and Telegraph 13 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 14 * the permission of UNIX System Laboratories, Inc. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)ufs_vnops.c 8.7 (Berkeley) 2/3/94 41 * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95 42 * $FreeBSD$ 43 */ 44 45 #include "opt_suiddir.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/fcntl.h> 51 #include <sys/filio.h> 52 #include <sys/stat.h> 53 #include <sys/bio.h> 54 #include <sys/buf.h> 55 #include <sys/endian.h> 56 #include <sys/priv.h> 57 #include <sys/mount.h> 58 #include <sys/unistd.h> 59 #include <sys/time.h> 60 #include <sys/vnode.h> 61 #include <sys/namei.h> 62 #include <sys/lockf.h> 63 #include <sys/event.h> 64 #include <sys/conf.h> 65 #include <sys/file.h> 66 67 #include <vm/vm.h> 68 #include <vm/vm_page.h> 69 #include <vm/vm_object.h> 70 #include <vm/vm_extern.h> 71 #include <vm/vnode_pager.h> 72 73 #include "opt_directio.h" 74 75 #include <ufs/ufs/dir.h> 76 77 #include <fs/ext2fs/fs.h> 78 #include <fs/ext2fs/inode.h> 79 #include <fs/ext2fs/ext2_extern.h> 80 #include <fs/ext2fs/ext2fs.h> 81 #include <fs/ext2fs/ext2_dinode.h> 82 #include <fs/ext2fs/ext2_dir.h> 83 #include <fs/ext2fs/ext2_mount.h> 84 85 static int ext2_makeinode(int mode, struct vnode *, struct vnode **, struct componentname *); 86 static void ext2_itimes_locked(struct vnode *); 87 88 static vop_access_t ext2_access; 89 static int ext2_chmod(struct vnode *, int, struct ucred *, struct thread *); 90 static int ext2_chown(struct vnode *, uid_t, gid_t, struct ucred *, 91 struct thread *); 92 static vop_close_t ext2_close; 93 static vop_create_t ext2_create; 94 static vop_fsync_t ext2_fsync; 95 static vop_getattr_t ext2_getattr; 96 static vop_ioctl_t ext2_ioctl; 97 static vop_link_t ext2_link; 98 static vop_mkdir_t ext2_mkdir; 99 static vop_mknod_t ext2_mknod; 100 static vop_open_t ext2_open; 101 static vop_pathconf_t ext2_pathconf; 102 static vop_print_t ext2_print; 103 static vop_read_t ext2_read; 104 static vop_readlink_t ext2_readlink; 105 static vop_remove_t ext2_remove; 106 static vop_rename_t ext2_rename; 107 static vop_rmdir_t ext2_rmdir; 108 static vop_setattr_t ext2_setattr; 109 static vop_strategy_t ext2_strategy; 110 static vop_symlink_t ext2_symlink; 111 static vop_write_t ext2_write; 112 static vop_vptofh_t ext2_vptofh; 113 static vop_close_t ext2fifo_close; 114 static vop_kqfilter_t ext2fifo_kqfilter; 115 116 /* Global vfs data structures for ext2. */ 117 struct vop_vector ext2_vnodeops = { 118 .vop_default = &default_vnodeops, 119 .vop_access = ext2_access, 120 .vop_bmap = ext2_bmap, 121 .vop_cachedlookup = ext2_lookup, 122 .vop_close = ext2_close, 123 .vop_create = ext2_create, 124 .vop_fsync = ext2_fsync, 125 .vop_getattr = ext2_getattr, 126 .vop_inactive = ext2_inactive, 127 .vop_ioctl = ext2_ioctl, 128 .vop_link = ext2_link, 129 .vop_lookup = vfs_cache_lookup, 130 .vop_mkdir = ext2_mkdir, 131 .vop_mknod = ext2_mknod, 132 .vop_open = ext2_open, 133 .vop_pathconf = ext2_pathconf, 134 .vop_poll = vop_stdpoll, 135 .vop_print = ext2_print, 136 .vop_read = ext2_read, 137 .vop_readdir = ext2_readdir, 138 .vop_readlink = ext2_readlink, 139 .vop_reallocblks = ext2_reallocblks, 140 .vop_reclaim = ext2_reclaim, 141 .vop_remove = ext2_remove, 142 .vop_rename = ext2_rename, 143 .vop_rmdir = ext2_rmdir, 144 .vop_setattr = ext2_setattr, 145 .vop_strategy = ext2_strategy, 146 .vop_symlink = ext2_symlink, 147 .vop_write = ext2_write, 148 .vop_vptofh = ext2_vptofh, 149 }; 150 151 struct vop_vector ext2_fifoops = { 152 .vop_default = &fifo_specops, 153 .vop_access = ext2_access, 154 .vop_close = ext2fifo_close, 155 .vop_fsync = ext2_fsync, 156 .vop_getattr = ext2_getattr, 157 .vop_inactive = ext2_inactive, 158 .vop_kqfilter = ext2fifo_kqfilter, 159 .vop_print = ext2_print, 160 .vop_read = VOP_PANIC, 161 .vop_reclaim = ext2_reclaim, 162 .vop_setattr = ext2_setattr, 163 .vop_write = VOP_PANIC, 164 .vop_vptofh = ext2_vptofh, 165 }; 166 167 /* 168 * A virgin directory (no blushing please). 169 * Note that the type and namlen fields are reversed relative to ext2. 170 * Also, we don't use `struct odirtemplate', since it would just cause 171 * endianness problems. 172 */ 173 static struct dirtemplate mastertemplate = { 174 0, 12, 1, EXT2_FT_DIR, ".", 175 0, DIRBLKSIZ - 12, 2, EXT2_FT_DIR, ".." 176 }; 177 static struct dirtemplate omastertemplate = { 178 0, 12, 1, EXT2_FT_UNKNOWN, ".", 179 0, DIRBLKSIZ - 12, 2, EXT2_FT_UNKNOWN, ".." 180 }; 181 182 static void 183 ext2_itimes_locked(struct vnode *vp) 184 { 185 struct inode *ip; 186 struct timespec ts; 187 188 ASSERT_VI_LOCKED(vp, __func__); 189 190 ip = VTOI(vp); 191 if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0) 192 return; 193 if ((vp->v_type == VBLK || vp->v_type == VCHR)) 194 ip->i_flag |= IN_LAZYMOD; 195 else 196 ip->i_flag |= IN_MODIFIED; 197 if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 198 vfs_timestamp(&ts); 199 if (ip->i_flag & IN_ACCESS) { 200 ip->i_atime = ts.tv_sec; 201 ip->i_atimensec = ts.tv_nsec; 202 } 203 if (ip->i_flag & IN_UPDATE) { 204 ip->i_mtime = ts.tv_sec; 205 ip->i_mtimensec = ts.tv_nsec; 206 ip->i_modrev++; 207 } 208 if (ip->i_flag & IN_CHANGE) { 209 ip->i_ctime = ts.tv_sec; 210 ip->i_ctimensec = ts.tv_nsec; 211 } 212 } 213 ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE); 214 } 215 216 void 217 ext2_itimes(struct vnode *vp) 218 { 219 220 VI_LOCK(vp); 221 ext2_itimes_locked(vp); 222 VI_UNLOCK(vp); 223 } 224 225 /* 226 * Create a regular file 227 */ 228 static int 229 ext2_create(struct vop_create_args *ap) 230 { 231 int error; 232 233 error = 234 ext2_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode), 235 ap->a_dvp, ap->a_vpp, ap->a_cnp); 236 if (error) 237 return (error); 238 return (0); 239 } 240 241 static int 242 ext2_open(struct vop_open_args *ap) 243 { 244 245 if (ap->a_vp->v_type == VBLK || ap->a_vp->v_type == VCHR) 246 return (EOPNOTSUPP); 247 248 /* 249 * Files marked append-only must be opened for appending. 250 */ 251 if ((VTOI(ap->a_vp)->i_flags & APPEND) && 252 (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) 253 return (EPERM); 254 255 vnode_create_vobject(ap->a_vp, VTOI(ap->a_vp)->i_size, ap->a_td); 256 257 return (0); 258 } 259 260 /* 261 * Close called. 262 * 263 * Update the times on the inode. 264 */ 265 static int 266 ext2_close(struct vop_close_args *ap) 267 { 268 struct vnode *vp = ap->a_vp; 269 270 VI_LOCK(vp); 271 if (vp->v_usecount > 1) 272 ext2_itimes_locked(vp); 273 VI_UNLOCK(vp); 274 return (0); 275 } 276 277 static int 278 ext2_access(struct vop_access_args *ap) 279 { 280 struct vnode *vp = ap->a_vp; 281 struct inode *ip = VTOI(vp); 282 accmode_t accmode = ap->a_accmode; 283 int error; 284 285 if (vp->v_type == VBLK || vp->v_type == VCHR) 286 return (EOPNOTSUPP); 287 288 /* 289 * Disallow write attempts on read-only file systems; 290 * unless the file is a socket, fifo, or a block or 291 * character device resident on the file system. 292 */ 293 if (accmode & VWRITE) { 294 switch (vp->v_type) { 295 case VDIR: 296 case VLNK: 297 case VREG: 298 if (vp->v_mount->mnt_flag & MNT_RDONLY) 299 return (EROFS); 300 break; 301 default: 302 break; 303 } 304 } 305 306 /* If immutable bit set, nobody gets to write it. */ 307 if ((accmode & VWRITE) && (ip->i_flags & (SF_IMMUTABLE | SF_SNAPSHOT))) 308 return (EPERM); 309 310 error = vaccess(vp->v_type, ip->i_mode, ip->i_uid, ip->i_gid, 311 ap->a_accmode, ap->a_cred, NULL); 312 return (error); 313 } 314 315 static int 316 ext2_getattr(struct vop_getattr_args *ap) 317 { 318 struct vnode *vp = ap->a_vp; 319 struct inode *ip = VTOI(vp); 320 struct vattr *vap = ap->a_vap; 321 322 ext2_itimes(vp); 323 /* 324 * Copy from inode table 325 */ 326 vap->va_fsid = dev2udev(ip->i_devvp->v_rdev); 327 vap->va_fileid = ip->i_number; 328 vap->va_mode = ip->i_mode & ~IFMT; 329 vap->va_nlink = ip->i_nlink; 330 vap->va_uid = ip->i_uid; 331 vap->va_gid = ip->i_gid; 332 vap->va_rdev = ip->i_rdev; 333 vap->va_size = ip->i_size; 334 vap->va_atime.tv_sec = ip->i_atime; 335 vap->va_atime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_atimensec : 0; 336 vap->va_mtime.tv_sec = ip->i_mtime; 337 vap->va_mtime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_mtimensec : 0; 338 vap->va_ctime.tv_sec = ip->i_ctime; 339 vap->va_ctime.tv_nsec = E2DI_HAS_XTIME(ip) ? ip->i_ctimensec : 0; 340 if E2DI_HAS_XTIME(ip) { 341 vap->va_birthtime.tv_sec = ip->i_birthtime; 342 vap->va_birthtime.tv_nsec = ip->i_birthnsec; 343 } 344 vap->va_flags = ip->i_flags; 345 vap->va_gen = ip->i_gen; 346 vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize; 347 vap->va_bytes = dbtob((u_quad_t)ip->i_blocks); 348 vap->va_type = IFTOVT(ip->i_mode); 349 vap->va_filerev = ip->i_modrev; 350 return (0); 351 } 352 353 /* 354 * Set attribute vnode op. called from several syscalls 355 */ 356 static int 357 ext2_setattr(struct vop_setattr_args *ap) 358 { 359 struct vattr *vap = ap->a_vap; 360 struct vnode *vp = ap->a_vp; 361 struct inode *ip = VTOI(vp); 362 struct ucred *cred = ap->a_cred; 363 struct thread *td = curthread; 364 int error; 365 366 /* 367 * Check for unsettable attributes. 368 */ 369 if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || 370 (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || 371 (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || 372 ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { 373 return (EINVAL); 374 } 375 if (vap->va_flags != VNOVAL) { 376 /* Disallow flags not supported by ext2fs. */ 377 if(vap->va_flags & ~(SF_APPEND | SF_IMMUTABLE | UF_NODUMP)) 378 return (EOPNOTSUPP); 379 380 if (vp->v_mount->mnt_flag & MNT_RDONLY) 381 return (EROFS); 382 /* 383 * Callers may only modify the file flags on objects they 384 * have VADMIN rights for. 385 */ 386 if ((error = VOP_ACCESS(vp, VADMIN, cred, td))) 387 return (error); 388 /* 389 * Unprivileged processes and privileged processes in 390 * jail() are not permitted to unset system flags, or 391 * modify flags if any system flags are set. 392 * Privileged non-jail processes may not modify system flags 393 * if securelevel > 0 and any existing system flags are set. 394 */ 395 if (!priv_check_cred(cred, PRIV_VFS_SYSFLAGS, 0)) { 396 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND)) { 397 error = securelevel_gt(cred, 0); 398 if (error) 399 return (error); 400 } 401 } else { 402 if (ip->i_flags & (SF_IMMUTABLE | SF_APPEND) || 403 ((vap->va_flags ^ ip->i_flags) & SF_SETTABLE)) 404 return (EPERM); 405 } 406 ip->i_flags = vap->va_flags; 407 ip->i_flag |= IN_CHANGE; 408 if (ip->i_flags & (IMMUTABLE | APPEND)) 409 return (0); 410 } 411 if (ip->i_flags & (IMMUTABLE | APPEND)) 412 return (EPERM); 413 /* 414 * Go through the fields and update iff not VNOVAL. 415 */ 416 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { 417 if (vp->v_mount->mnt_flag & MNT_RDONLY) 418 return (EROFS); 419 if ((error = ext2_chown(vp, vap->va_uid, vap->va_gid, cred, 420 td)) != 0) 421 return (error); 422 } 423 if (vap->va_size != VNOVAL) { 424 /* 425 * Disallow write attempts on read-only file systems; 426 * unless the file is a socket, fifo, or a block or 427 * character device resident on the file system. 428 */ 429 switch (vp->v_type) { 430 case VDIR: 431 return (EISDIR); 432 case VLNK: 433 case VREG: 434 if (vp->v_mount->mnt_flag & MNT_RDONLY) 435 return (EROFS); 436 break; 437 default: 438 break; 439 } 440 if ((error = ext2_truncate(vp, vap->va_size, 0, cred, td)) != 0) 441 return (error); 442 } 443 if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { 444 if (vp->v_mount->mnt_flag & MNT_RDONLY) 445 return (EROFS); 446 /* 447 * From utimes(2): 448 * If times is NULL, ... The caller must be the owner of 449 * the file, have permission to write the file, or be the 450 * super-user. 451 * If times is non-NULL, ... The caller must be the owner of 452 * the file or be the super-user. 453 */ 454 if ((error = VOP_ACCESS(vp, VADMIN, cred, td)) && 455 ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || 456 (error = VOP_ACCESS(vp, VWRITE, cred, td)))) 457 return (error); 458 if (vap->va_atime.tv_sec != VNOVAL) 459 ip->i_flag |= IN_ACCESS; 460 if (vap->va_mtime.tv_sec != VNOVAL) 461 ip->i_flag |= IN_CHANGE | IN_UPDATE; 462 ext2_itimes(vp); 463 if (vap->va_atime.tv_sec != VNOVAL) { 464 ip->i_atime = vap->va_atime.tv_sec; 465 ip->i_atimensec = vap->va_atime.tv_nsec; 466 } 467 if (vap->va_mtime.tv_sec != VNOVAL) { 468 ip->i_mtime = vap->va_mtime.tv_sec; 469 ip->i_mtimensec = vap->va_mtime.tv_nsec; 470 } 471 ip->i_birthtime = vap->va_birthtime.tv_sec; 472 ip->i_birthnsec = vap->va_birthtime.tv_nsec; 473 error = ext2_update(vp, 0); 474 if (error) 475 return (error); 476 } 477 error = 0; 478 if (vap->va_mode != (mode_t)VNOVAL) { 479 if (vp->v_mount->mnt_flag & MNT_RDONLY) 480 return (EROFS); 481 error = ext2_chmod(vp, (int)vap->va_mode, cred, td); 482 } 483 return (error); 484 } 485 486 /* 487 * Change the mode on a file. 488 * Inode must be locked before calling. 489 */ 490 static int 491 ext2_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td) 492 { 493 struct inode *ip = VTOI(vp); 494 int error; 495 496 /* 497 * To modify the permissions on a file, must possess VADMIN 498 * for that file. 499 */ 500 if ((error = VOP_ACCESS(vp, VADMIN, cred, td))) 501 return (error); 502 /* 503 * Privileged processes may set the sticky bit on non-directories, 504 * as well as set the setgid bit on a file with a group that the 505 * process is not a member of. 506 */ 507 if (vp->v_type != VDIR && (mode & S_ISTXT)) { 508 error = priv_check_cred(cred, PRIV_VFS_STICKYFILE, 0); 509 if (error) 510 return (EFTYPE); 511 } 512 if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) { 513 error = priv_check_cred(cred, PRIV_VFS_SETGID, 0); 514 if (error) 515 return (error); 516 } 517 ip->i_mode &= ~ALLPERMS; 518 ip->i_mode |= (mode & ALLPERMS); 519 ip->i_flag |= IN_CHANGE; 520 return (0); 521 } 522 523 /* 524 * Perform chown operation on inode ip; 525 * inode must be locked prior to call. 526 */ 527 static int 528 ext2_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred, 529 struct thread *td) 530 { 531 struct inode *ip = VTOI(vp); 532 uid_t ouid; 533 gid_t ogid; 534 int error = 0; 535 536 if (uid == (uid_t)VNOVAL) 537 uid = ip->i_uid; 538 if (gid == (gid_t)VNOVAL) 539 gid = ip->i_gid; 540 /* 541 * To modify the ownership of a file, must possess VADMIN 542 * for that file. 543 */ 544 if ((error = VOP_ACCESS(vp, VADMIN, cred, td))) 545 return (error); 546 /* 547 * To change the owner of a file, or change the group of a file 548 * to a group of which we are not a member, the caller must 549 * have privilege. 550 */ 551 if (uid != ip->i_uid || (gid != ip->i_gid && 552 !groupmember(gid, cred))) { 553 error = priv_check_cred(cred, PRIV_VFS_CHOWN, 0); 554 if (error) 555 return (error); 556 } 557 ogid = ip->i_gid; 558 ouid = ip->i_uid; 559 ip->i_gid = gid; 560 ip->i_uid = uid; 561 ip->i_flag |= IN_CHANGE; 562 if ((ip->i_mode & (ISUID | ISGID)) && (ouid != uid || ogid != gid)) { 563 if (priv_check_cred(cred, PRIV_VFS_RETAINSUGID, 0) != 0) 564 ip->i_mode &= ~(ISUID | ISGID); 565 } 566 return (0); 567 } 568 569 /* 570 * Synch an open file. 571 */ 572 /* ARGSUSED */ 573 static int 574 ext2_fsync(struct vop_fsync_args *ap) 575 { 576 /* 577 * Flush all dirty buffers associated with a vnode. 578 */ 579 580 vop_stdfsync(ap); 581 582 return (ext2_update(ap->a_vp, ap->a_waitfor == MNT_WAIT)); 583 } 584 585 /* 586 * Mknod vnode call 587 */ 588 /* ARGSUSED */ 589 static int 590 ext2_mknod(struct vop_mknod_args *ap) 591 { 592 struct vattr *vap = ap->a_vap; 593 struct vnode **vpp = ap->a_vpp; 594 struct inode *ip; 595 ino_t ino; 596 int error; 597 598 error = ext2_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), 599 ap->a_dvp, vpp, ap->a_cnp); 600 if (error) 601 return (error); 602 ip = VTOI(*vpp); 603 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 604 if (vap->va_rdev != VNOVAL) { 605 /* 606 * Want to be able to use this to make badblock 607 * inodes, so don't truncate the dev number. 608 */ 609 ip->i_rdev = vap->va_rdev; 610 } 611 /* 612 * Remove inode, then reload it through VFS_VGET so it is 613 * checked to see if it is an alias of an existing entry in 614 * the inode cache. XXX I don't believe this is necessary now. 615 */ 616 (*vpp)->v_type = VNON; 617 ino = ip->i_number; /* Save this before vgone() invalidates ip. */ 618 vgone(*vpp); 619 vput(*vpp); 620 error = VFS_VGET(ap->a_dvp->v_mount, ino, LK_EXCLUSIVE, vpp); 621 if (error) { 622 *vpp = NULL; 623 return (error); 624 } 625 return (0); 626 } 627 628 static int 629 ext2_remove(struct vop_remove_args *ap) 630 { 631 struct inode *ip; 632 struct vnode *vp = ap->a_vp; 633 struct vnode *dvp = ap->a_dvp; 634 int error; 635 636 ip = VTOI(vp); 637 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 638 (VTOI(dvp)->i_flags & APPEND)) { 639 error = EPERM; 640 goto out; 641 } 642 error = ext2_dirremove(dvp, ap->a_cnp); 643 if (error == 0) { 644 ip->i_nlink--; 645 ip->i_flag |= IN_CHANGE; 646 } 647 out: 648 return (error); 649 } 650 651 /* 652 * link vnode call 653 */ 654 static int 655 ext2_link(struct vop_link_args *ap) 656 { 657 struct vnode *vp = ap->a_vp; 658 struct vnode *tdvp = ap->a_tdvp; 659 struct componentname *cnp = ap->a_cnp; 660 struct inode *ip; 661 int error; 662 663 #ifdef INVARIANTS 664 if ((cnp->cn_flags & HASBUF) == 0) 665 panic("ext2_link: no name"); 666 #endif 667 if (tdvp->v_mount != vp->v_mount) { 668 error = EXDEV; 669 goto out; 670 } 671 ip = VTOI(vp); 672 if ((nlink_t)ip->i_nlink >= EXT2_LINK_MAX) { 673 error = EMLINK; 674 goto out; 675 } 676 if (ip->i_flags & (IMMUTABLE | APPEND)) { 677 error = EPERM; 678 goto out; 679 } 680 ip->i_nlink++; 681 ip->i_flag |= IN_CHANGE; 682 error = ext2_update(vp, !DOINGASYNC(vp)); 683 if (!error) 684 error = ext2_direnter(ip, tdvp, cnp); 685 if (error) { 686 ip->i_nlink--; 687 ip->i_flag |= IN_CHANGE; 688 } 689 out: 690 return (error); 691 } 692 693 /* 694 * Rename system call. 695 * rename("foo", "bar"); 696 * is essentially 697 * unlink("bar"); 698 * link("foo", "bar"); 699 * unlink("foo"); 700 * but ``atomically''. Can't do full commit without saving state in the 701 * inode on disk which isn't feasible at this time. Best we can do is 702 * always guarantee the target exists. 703 * 704 * Basic algorithm is: 705 * 706 * 1) Bump link count on source while we're linking it to the 707 * target. This also ensure the inode won't be deleted out 708 * from underneath us while we work (it may be truncated by 709 * a concurrent `trunc' or `open' for creation). 710 * 2) Link source to destination. If destination already exists, 711 * delete it first. 712 * 3) Unlink source reference to inode if still around. If a 713 * directory was moved and the parent of the destination 714 * is different from the source, patch the ".." entry in the 715 * directory. 716 */ 717 static int 718 ext2_rename(struct vop_rename_args *ap) 719 { 720 struct vnode *tvp = ap->a_tvp; 721 struct vnode *tdvp = ap->a_tdvp; 722 struct vnode *fvp = ap->a_fvp; 723 struct vnode *fdvp = ap->a_fdvp; 724 struct componentname *tcnp = ap->a_tcnp; 725 struct componentname *fcnp = ap->a_fcnp; 726 struct inode *ip, *xp, *dp; 727 struct dirtemplate dirbuf; 728 int doingdirectory = 0, oldparent = 0, newparent = 0; 729 int error = 0; 730 u_char namlen; 731 732 #ifdef INVARIANTS 733 if ((tcnp->cn_flags & HASBUF) == 0 || 734 (fcnp->cn_flags & HASBUF) == 0) 735 panic("ext2_rename: no name"); 736 #endif 737 /* 738 * Check for cross-device rename. 739 */ 740 if ((fvp->v_mount != tdvp->v_mount) || 741 (tvp && (fvp->v_mount != tvp->v_mount))) { 742 error = EXDEV; 743 abortit: 744 if (tdvp == tvp) 745 vrele(tdvp); 746 else 747 vput(tdvp); 748 if (tvp) 749 vput(tvp); 750 vrele(fdvp); 751 vrele(fvp); 752 return (error); 753 } 754 755 if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) || 756 (VTOI(tdvp)->i_flags & APPEND))) { 757 error = EPERM; 758 goto abortit; 759 } 760 761 /* 762 * Renaming a file to itself has no effect. The upper layers should 763 * not call us in that case. Temporarily just warn if they do. 764 */ 765 if (fvp == tvp) { 766 printf("ext2_rename: fvp == tvp (can't happen)\n"); 767 error = 0; 768 goto abortit; 769 } 770 771 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0) 772 goto abortit; 773 dp = VTOI(fdvp); 774 ip = VTOI(fvp); 775 if (ip->i_nlink >= EXT2_LINK_MAX) { 776 VOP_UNLOCK(fvp, 0); 777 error = EMLINK; 778 goto abortit; 779 } 780 if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) 781 || (dp->i_flags & APPEND)) { 782 VOP_UNLOCK(fvp, 0); 783 error = EPERM; 784 goto abortit; 785 } 786 if ((ip->i_mode & IFMT) == IFDIR) { 787 /* 788 * Avoid ".", "..", and aliases of "." for obvious reasons. 789 */ 790 if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') || 791 dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & ISDOTDOT || 792 (ip->i_flag & IN_RENAME)) { 793 VOP_UNLOCK(fvp, 0); 794 error = EINVAL; 795 goto abortit; 796 } 797 ip->i_flag |= IN_RENAME; 798 oldparent = dp->i_number; 799 doingdirectory++; 800 } 801 vrele(fdvp); 802 803 /* 804 * When the target exists, both the directory 805 * and target vnodes are returned locked. 806 */ 807 dp = VTOI(tdvp); 808 xp = NULL; 809 if (tvp) 810 xp = VTOI(tvp); 811 812 /* 813 * 1) Bump link count while we're moving stuff 814 * around. If we crash somewhere before 815 * completing our work, the link count 816 * may be wrong, but correctable. 817 */ 818 ip->i_nlink++; 819 ip->i_flag |= IN_CHANGE; 820 if ((error = ext2_update(fvp, !DOINGASYNC(fvp))) != 0) { 821 VOP_UNLOCK(fvp, 0); 822 goto bad; 823 } 824 825 /* 826 * If ".." must be changed (ie the directory gets a new 827 * parent) then the source directory must not be in the 828 * directory hierarchy above the target, as this would 829 * orphan everything below the source directory. Also 830 * the user must have write permission in the source so 831 * as to be able to change "..". We must repeat the call 832 * to namei, as the parent directory is unlocked by the 833 * call to checkpath(). 834 */ 835 error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_thread); 836 VOP_UNLOCK(fvp, 0); 837 if (oldparent != dp->i_number) 838 newparent = dp->i_number; 839 if (doingdirectory && newparent) { 840 if (error) /* write access check above */ 841 goto bad; 842 if (xp != NULL) 843 vput(tvp); 844 error = ext2_checkpath(ip, dp, tcnp->cn_cred); 845 if (error) 846 goto out; 847 VREF(tdvp); 848 error = relookup(tdvp, &tvp, tcnp); 849 if (error) 850 goto out; 851 vrele(tdvp); 852 dp = VTOI(tdvp); 853 xp = NULL; 854 if (tvp) 855 xp = VTOI(tvp); 856 } 857 /* 858 * 2) If target doesn't exist, link the target 859 * to the source and unlink the source. 860 * Otherwise, rewrite the target directory 861 * entry to reference the source inode and 862 * expunge the original entry's existence. 863 */ 864 if (xp == NULL) { 865 if (dp->i_devvp != ip->i_devvp) 866 panic("ext2_rename: EXDEV"); 867 /* 868 * Account for ".." in new directory. 869 * When source and destination have the same 870 * parent we don't fool with the link count. 871 */ 872 if (doingdirectory && newparent) { 873 if ((nlink_t)dp->i_nlink >= EXT2_LINK_MAX) { 874 error = EMLINK; 875 goto bad; 876 } 877 dp->i_nlink++; 878 dp->i_flag |= IN_CHANGE; 879 error = ext2_update(tdvp, !DOINGASYNC(tdvp)); 880 if (error) 881 goto bad; 882 } 883 error = ext2_direnter(ip, tdvp, tcnp); 884 if (error) { 885 if (doingdirectory && newparent) { 886 dp->i_nlink--; 887 dp->i_flag |= IN_CHANGE; 888 (void)ext2_update(tdvp, 1); 889 } 890 goto bad; 891 } 892 vput(tdvp); 893 } else { 894 if (xp->i_devvp != dp->i_devvp || xp->i_devvp != ip->i_devvp) 895 panic("ext2_rename: EXDEV"); 896 /* 897 * Short circuit rename(foo, foo). 898 */ 899 if (xp->i_number == ip->i_number) 900 panic("ext2_rename: same file"); 901 /* 902 * If the parent directory is "sticky", then the user must 903 * own the parent directory, or the destination of the rename, 904 * otherwise the destination may not be changed (except by 905 * root). This implements append-only directories. 906 */ 907 if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 && 908 tcnp->cn_cred->cr_uid != dp->i_uid && 909 xp->i_uid != tcnp->cn_cred->cr_uid) { 910 error = EPERM; 911 goto bad; 912 } 913 /* 914 * Target must be empty if a directory and have no links 915 * to it. Also, ensure source and target are compatible 916 * (both directories, or both not directories). 917 */ 918 if ((xp->i_mode&IFMT) == IFDIR) { 919 if (! ext2_dirempty(xp, dp->i_number, tcnp->cn_cred) || 920 xp->i_nlink > 2) { 921 error = ENOTEMPTY; 922 goto bad; 923 } 924 if (!doingdirectory) { 925 error = ENOTDIR; 926 goto bad; 927 } 928 cache_purge(tdvp); 929 } else if (doingdirectory) { 930 error = EISDIR; 931 goto bad; 932 } 933 error = ext2_dirrewrite(dp, ip, tcnp); 934 if (error) 935 goto bad; 936 /* 937 * If the target directory is in the same 938 * directory as the source directory, 939 * decrement the link count on the parent 940 * of the target directory. 941 */ 942 if (doingdirectory && !newparent) { 943 dp->i_nlink--; 944 dp->i_flag |= IN_CHANGE; 945 } 946 vput(tdvp); 947 /* 948 * Adjust the link count of the target to 949 * reflect the dirrewrite above. If this is 950 * a directory it is empty and there are 951 * no links to it, so we can squash the inode and 952 * any space associated with it. We disallowed 953 * renaming over top of a directory with links to 954 * it above, as the remaining link would point to 955 * a directory without "." or ".." entries. 956 */ 957 xp->i_nlink--; 958 if (doingdirectory) { 959 if (--xp->i_nlink != 0) 960 panic("ext2_rename: linked directory"); 961 error = ext2_truncate(tvp, (off_t)0, IO_SYNC, 962 tcnp->cn_cred, tcnp->cn_thread); 963 } 964 xp->i_flag |= IN_CHANGE; 965 vput(tvp); 966 xp = NULL; 967 } 968 969 /* 970 * 3) Unlink the source. 971 */ 972 fcnp->cn_flags &= ~MODMASK; 973 fcnp->cn_flags |= LOCKPARENT | LOCKLEAF; 974 VREF(fdvp); 975 error = relookup(fdvp, &fvp, fcnp); 976 if (error == 0) 977 vrele(fdvp); 978 if (fvp != NULL) { 979 xp = VTOI(fvp); 980 dp = VTOI(fdvp); 981 } else { 982 /* 983 * From name has disappeared. 984 */ 985 if (doingdirectory) 986 panic("ext2_rename: lost dir entry"); 987 vrele(ap->a_fvp); 988 return (0); 989 } 990 /* 991 * Ensure that the directory entry still exists and has not 992 * changed while the new name has been entered. If the source is 993 * a file then the entry may have been unlinked or renamed. In 994 * either case there is no further work to be done. If the source 995 * is a directory then it cannot have been rmdir'ed; its link 996 * count of three would cause a rmdir to fail with ENOTEMPTY. 997 * The IN_RENAME flag ensures that it cannot be moved by another 998 * rename. 999 */ 1000 if (xp != ip) { 1001 if (doingdirectory) 1002 panic("ext2_rename: lost dir entry"); 1003 } else { 1004 /* 1005 * If the source is a directory with a 1006 * new parent, the link count of the old 1007 * parent directory must be decremented 1008 * and ".." set to point to the new parent. 1009 */ 1010 if (doingdirectory && newparent) { 1011 dp->i_nlink--; 1012 dp->i_flag |= IN_CHANGE; 1013 error = vn_rdwr(UIO_READ, fvp, (caddr_t)&dirbuf, 1014 sizeof(struct dirtemplate), (off_t)0, 1015 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, 1016 tcnp->cn_cred, NOCRED, NULL, NULL); 1017 if (error == 0) { 1018 /* Like ufs little-endian: */ 1019 namlen = dirbuf.dotdot_type; 1020 if (namlen != 2 || 1021 dirbuf.dotdot_name[0] != '.' || 1022 dirbuf.dotdot_name[1] != '.') { 1023 ext2_dirbad(xp, (doff_t)12, 1024 "rename: mangled dir"); 1025 } else { 1026 dirbuf.dotdot_ino = newparent; 1027 (void) vn_rdwr(UIO_WRITE, fvp, 1028 (caddr_t)&dirbuf, 1029 sizeof(struct dirtemplate), 1030 (off_t)0, UIO_SYSSPACE, 1031 IO_NODELOCKED | IO_SYNC | 1032 IO_NOMACCHECK, tcnp->cn_cred, 1033 NOCRED, NULL, NULL); 1034 cache_purge(fdvp); 1035 } 1036 } 1037 } 1038 error = ext2_dirremove(fdvp, fcnp); 1039 if (!error) { 1040 xp->i_nlink--; 1041 xp->i_flag |= IN_CHANGE; 1042 } 1043 xp->i_flag &= ~IN_RENAME; 1044 } 1045 if (dp) 1046 vput(fdvp); 1047 if (xp) 1048 vput(fvp); 1049 vrele(ap->a_fvp); 1050 return (error); 1051 1052 bad: 1053 if (xp) 1054 vput(ITOV(xp)); 1055 vput(ITOV(dp)); 1056 out: 1057 if (doingdirectory) 1058 ip->i_flag &= ~IN_RENAME; 1059 if (vn_lock(fvp, LK_EXCLUSIVE) == 0) { 1060 ip->i_nlink--; 1061 ip->i_flag |= IN_CHANGE; 1062 ip->i_flag &= ~IN_RENAME; 1063 vput(fvp); 1064 } else 1065 vrele(fvp); 1066 return (error); 1067 } 1068 1069 /* 1070 * Mkdir system call 1071 */ 1072 static int 1073 ext2_mkdir(struct vop_mkdir_args *ap) 1074 { 1075 struct vnode *dvp = ap->a_dvp; 1076 struct vattr *vap = ap->a_vap; 1077 struct componentname *cnp = ap->a_cnp; 1078 struct inode *ip, *dp; 1079 struct vnode *tvp; 1080 struct dirtemplate dirtemplate, *dtp; 1081 int error, dmode; 1082 1083 #ifdef INVARIANTS 1084 if ((cnp->cn_flags & HASBUF) == 0) 1085 panic("ext2_mkdir: no name"); 1086 #endif 1087 dp = VTOI(dvp); 1088 if ((nlink_t)dp->i_nlink >= EXT2_LINK_MAX) { 1089 error = EMLINK; 1090 goto out; 1091 } 1092 dmode = vap->va_mode & 0777; 1093 dmode |= IFDIR; 1094 /* 1095 * Must simulate part of ext2_makeinode here to acquire the inode, 1096 * but not have it entered in the parent directory. The entry is 1097 * made later after writing "." and ".." entries. 1098 */ 1099 error = ext2_valloc(dvp, dmode, cnp->cn_cred, &tvp); 1100 if (error) 1101 goto out; 1102 ip = VTOI(tvp); 1103 ip->i_gid = dp->i_gid; 1104 #ifdef SUIDDIR 1105 { 1106 /* 1107 * if we are hacking owners here, (only do this where told to) 1108 * and we are not giving it TOO root, (would subvert quotas) 1109 * then go ahead and give it to the other user. 1110 * The new directory also inherits the SUID bit. 1111 * If user's UID and dir UID are the same, 1112 * 'give it away' so that the SUID is still forced on. 1113 */ 1114 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) && 1115 (dp->i_mode & ISUID) && dp->i_uid) { 1116 dmode |= ISUID; 1117 ip->i_uid = dp->i_uid; 1118 } else { 1119 ip->i_uid = cnp->cn_cred->cr_uid; 1120 } 1121 } 1122 #else 1123 ip->i_uid = cnp->cn_cred->cr_uid; 1124 #endif 1125 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 1126 ip->i_mode = dmode; 1127 tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */ 1128 ip->i_nlink = 2; 1129 if (cnp->cn_flags & ISWHITEOUT) 1130 ip->i_flags |= UF_OPAQUE; 1131 error = ext2_update(tvp, 1); 1132 1133 /* 1134 * Bump link count in parent directory 1135 * to reflect work done below. Should 1136 * be done before reference is created 1137 * so reparation is possible if we crash. 1138 */ 1139 dp->i_nlink++; 1140 dp->i_flag |= IN_CHANGE; 1141 error = ext2_update(dvp, !DOINGASYNC(dvp)); 1142 if (error) 1143 goto bad; 1144 1145 /* Initialize directory with "." and ".." from static template. */ 1146 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs, 1147 EXT2F_INCOMPAT_FTYPE)) 1148 dtp = &mastertemplate; 1149 else 1150 dtp = &omastertemplate; 1151 dirtemplate = *dtp; 1152 dirtemplate.dot_ino = ip->i_number; 1153 dirtemplate.dotdot_ino = dp->i_number; 1154 /* note that in ext2 DIRBLKSIZ == blocksize, not DEV_BSIZE 1155 * so let's just redefine it - for this function only 1156 */ 1157 #undef DIRBLKSIZ 1158 #define DIRBLKSIZ VTOI(dvp)->i_e2fs->e2fs_bsize 1159 dirtemplate.dotdot_reclen = DIRBLKSIZ - 12; 1160 error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate, 1161 sizeof(dirtemplate), (off_t)0, UIO_SYSSPACE, 1162 IO_NODELOCKED | IO_SYNC | IO_NOMACCHECK, cnp->cn_cred, NOCRED, 1163 NULL, NULL); 1164 if (error) { 1165 dp->i_nlink--; 1166 dp->i_flag |= IN_CHANGE; 1167 goto bad; 1168 } 1169 if (DIRBLKSIZ > VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize) 1170 /* XXX should grow with balloc() */ 1171 panic("ext2_mkdir: blksize"); 1172 else { 1173 ip->i_size = DIRBLKSIZ; 1174 ip->i_flag |= IN_CHANGE; 1175 } 1176 1177 /* Directory set up, now install its entry in the parent directory. */ 1178 error = ext2_direnter(ip, dvp, cnp); 1179 if (error) { 1180 dp->i_nlink--; 1181 dp->i_flag |= IN_CHANGE; 1182 } 1183 bad: 1184 /* 1185 * No need to do an explicit VOP_TRUNCATE here, vrele will do this 1186 * for us because we set the link count to 0. 1187 */ 1188 if (error) { 1189 ip->i_nlink = 0; 1190 ip->i_flag |= IN_CHANGE; 1191 vput(tvp); 1192 } else 1193 *ap->a_vpp = tvp; 1194 out: 1195 return (error); 1196 #undef DIRBLKSIZ 1197 #define DIRBLKSIZ DEV_BSIZE 1198 } 1199 1200 /* 1201 * Rmdir system call. 1202 */ 1203 static int 1204 ext2_rmdir(struct vop_rmdir_args *ap) 1205 { 1206 struct vnode *vp = ap->a_vp; 1207 struct vnode *dvp = ap->a_dvp; 1208 struct componentname *cnp = ap->a_cnp; 1209 struct inode *ip, *dp; 1210 int error; 1211 1212 ip = VTOI(vp); 1213 dp = VTOI(dvp); 1214 1215 /* 1216 * Verify the directory is empty (and valid). 1217 * (Rmdir ".." won't be valid since 1218 * ".." will contain a reference to 1219 * the current directory and thus be 1220 * non-empty.) 1221 */ 1222 error = 0; 1223 if (ip->i_nlink != 2 || !ext2_dirempty(ip, dp->i_number, cnp->cn_cred)) { 1224 error = ENOTEMPTY; 1225 goto out; 1226 } 1227 if ((dp->i_flags & APPEND) 1228 || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) { 1229 error = EPERM; 1230 goto out; 1231 } 1232 /* 1233 * Delete reference to directory before purging 1234 * inode. If we crash in between, the directory 1235 * will be reattached to lost+found, 1236 */ 1237 error = ext2_dirremove(dvp, cnp); 1238 if (error) 1239 goto out; 1240 dp->i_nlink--; 1241 dp->i_flag |= IN_CHANGE; 1242 cache_purge(dvp); 1243 VOP_UNLOCK(dvp, 0); 1244 /* 1245 * Truncate inode. The only stuff left 1246 * in the directory is "." and "..". The 1247 * "." reference is inconsequential since 1248 * we're quashing it. The ".." reference 1249 * has already been adjusted above. We've 1250 * removed the "." reference and the reference 1251 * in the parent directory, but there may be 1252 * other hard links so decrement by 2 and 1253 * worry about them later. 1254 */ 1255 ip->i_nlink -= 2; 1256 error = ext2_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred, 1257 cnp->cn_thread); 1258 cache_purge(ITOV(ip)); 1259 if (vn_lock(dvp, LK_EXCLUSIVE | LK_NOWAIT) != 0) { 1260 VOP_UNLOCK(vp, 0); 1261 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 1262 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 1263 } 1264 out: 1265 return (error); 1266 } 1267 1268 /* 1269 * symlink -- make a symbolic link 1270 */ 1271 static int 1272 ext2_symlink(struct vop_symlink_args *ap) 1273 { 1274 struct vnode *vp, **vpp = ap->a_vpp; 1275 struct inode *ip; 1276 int len, error; 1277 1278 error = ext2_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp, 1279 vpp, ap->a_cnp); 1280 if (error) 1281 return (error); 1282 vp = *vpp; 1283 len = strlen(ap->a_target); 1284 if (len < vp->v_mount->mnt_maxsymlinklen) { 1285 ip = VTOI(vp); 1286 bcopy(ap->a_target, (char *)ip->i_shortlink, len); 1287 ip->i_size = len; 1288 ip->i_flag |= IN_CHANGE | IN_UPDATE; 1289 } else 1290 error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0, 1291 UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, 1292 ap->a_cnp->cn_cred, NOCRED, NULL, NULL); 1293 if (error) 1294 vput(vp); 1295 return (error); 1296 } 1297 1298 /* 1299 * Return target name of a symbolic link 1300 */ 1301 static int 1302 ext2_readlink(struct vop_readlink_args *ap) 1303 { 1304 struct vnode *vp = ap->a_vp; 1305 struct inode *ip = VTOI(vp); 1306 int isize; 1307 1308 isize = ip->i_size; 1309 if (isize < vp->v_mount->mnt_maxsymlinklen) { 1310 uiomove((char *)ip->i_shortlink, isize, ap->a_uio); 1311 return (0); 1312 } 1313 return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred)); 1314 } 1315 1316 /* 1317 * Calculate the logical to physical mapping if not done already, 1318 * then call the device strategy routine. 1319 * 1320 * In order to be able to swap to a file, the ext2_bmaparray() operation may not 1321 * deadlock on memory. See ext2_bmap() for details. 1322 */ 1323 static int 1324 ext2_strategy(struct vop_strategy_args *ap) 1325 { 1326 struct buf *bp = ap->a_bp; 1327 struct vnode *vp = ap->a_vp; 1328 struct inode *ip; 1329 struct bufobj *bo; 1330 int32_t blkno; 1331 int error; 1332 1333 ip = VTOI(vp); 1334 if (vp->v_type == VBLK || vp->v_type == VCHR) 1335 panic("ext2_strategy: spec"); 1336 if (bp->b_blkno == bp->b_lblkno) { 1337 error = ext2_bmaparray(vp, bp->b_lblkno, &blkno, NULL, NULL); 1338 bp->b_blkno = blkno; 1339 if (error) { 1340 bp->b_error = error; 1341 bp->b_ioflags |= BIO_ERROR; 1342 bufdone(bp); 1343 return (0); 1344 } 1345 if ((long)bp->b_blkno == -1) 1346 vfs_bio_clrbuf(bp); 1347 } 1348 if ((long)bp->b_blkno == -1) { 1349 bufdone(bp); 1350 return (0); 1351 } 1352 bp->b_iooffset = dbtob(bp->b_blkno); 1353 bo = VFSTOEXT2(vp->v_mount)->um_bo; 1354 BO_STRATEGY(bo, bp); 1355 return (0); 1356 } 1357 1358 /* 1359 * Print out the contents of an inode. 1360 */ 1361 static int 1362 ext2_print(struct vop_print_args *ap) 1363 { 1364 struct vnode *vp = ap->a_vp; 1365 struct inode *ip = VTOI(vp); 1366 1367 vn_printf(ip->i_devvp, "\tino %lu", (u_long)ip->i_number); 1368 if (vp->v_type == VFIFO) 1369 fifo_printinfo(vp); 1370 printf("\n"); 1371 return (0); 1372 } 1373 1374 /* 1375 * Close wrapper for fifos. 1376 * 1377 * Update the times on the inode then do device close. 1378 */ 1379 static int 1380 ext2fifo_close(struct vop_close_args *ap) 1381 { 1382 struct vnode *vp = ap->a_vp; 1383 1384 VI_LOCK(vp); 1385 if (vp->v_usecount > 1) 1386 ext2_itimes_locked(vp); 1387 VI_UNLOCK(vp); 1388 return (fifo_specops.vop_close(ap)); 1389 } 1390 1391 /* 1392 * Kqfilter wrapper for fifos. 1393 * 1394 * Fall through to ext2 kqfilter routines if needed 1395 */ 1396 static int 1397 ext2fifo_kqfilter(struct vop_kqfilter_args *ap) 1398 { 1399 int error; 1400 1401 error = fifo_specops.vop_kqfilter(ap); 1402 if (error) 1403 error = vfs_kqfilter(ap); 1404 return (error); 1405 } 1406 1407 /* 1408 * Return POSIX pathconf information applicable to ext2 filesystems. 1409 */ 1410 static int 1411 ext2_pathconf(struct vop_pathconf_args *ap) 1412 { 1413 int error = 0; 1414 1415 switch (ap->a_name) { 1416 case _PC_LINK_MAX: 1417 *ap->a_retval = EXT2_LINK_MAX; 1418 break; 1419 case _PC_NAME_MAX: 1420 *ap->a_retval = NAME_MAX; 1421 break; 1422 case _PC_PATH_MAX: 1423 *ap->a_retval = PATH_MAX; 1424 break; 1425 case _PC_PIPE_BUF: 1426 *ap->a_retval = PIPE_BUF; 1427 break; 1428 case _PC_CHOWN_RESTRICTED: 1429 *ap->a_retval = 1; 1430 break; 1431 case _PC_NO_TRUNC: 1432 *ap->a_retval = 1; 1433 break; 1434 case _PC_MIN_HOLE_SIZE: 1435 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize; 1436 break; 1437 case _PC_ASYNC_IO: 1438 /* _PC_ASYNC_IO should have been handled by upper layers. */ 1439 KASSERT(0, ("_PC_ASYNC_IO should not get here")); 1440 error = EINVAL; 1441 break; 1442 case _PC_PRIO_IO: 1443 *ap->a_retval = 0; 1444 break; 1445 case _PC_SYNC_IO: 1446 *ap->a_retval = 0; 1447 break; 1448 case _PC_ALLOC_SIZE_MIN: 1449 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_bsize; 1450 break; 1451 case _PC_FILESIZEBITS: 1452 *ap->a_retval = 64; 1453 break; 1454 case _PC_REC_INCR_XFER_SIZE: 1455 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize; 1456 break; 1457 case _PC_REC_MAX_XFER_SIZE: 1458 *ap->a_retval = -1; /* means ``unlimited'' */ 1459 break; 1460 case _PC_REC_MIN_XFER_SIZE: 1461 *ap->a_retval = ap->a_vp->v_mount->mnt_stat.f_iosize; 1462 break; 1463 case _PC_REC_XFER_ALIGN: 1464 *ap->a_retval = PAGE_SIZE; 1465 break; 1466 case _PC_SYMLINK_MAX: 1467 *ap->a_retval = MAXPATHLEN; 1468 break; 1469 1470 default: 1471 error = EINVAL; 1472 break; 1473 } 1474 return (error); 1475 } 1476 1477 /* 1478 * Vnode pointer to File handle 1479 */ 1480 /* ARGSUSED */ 1481 static int 1482 ext2_vptofh(struct vop_vptofh_args *ap) 1483 { 1484 struct inode *ip; 1485 struct ufid *ufhp; 1486 1487 ip = VTOI(ap->a_vp); 1488 ufhp = (struct ufid *)ap->a_fhp; 1489 ufhp->ufid_len = sizeof(struct ufid); 1490 ufhp->ufid_ino = ip->i_number; 1491 ufhp->ufid_gen = ip->i_gen; 1492 return (0); 1493 } 1494 1495 /* 1496 * Initialize the vnode associated with a new inode, handle aliased 1497 * vnodes. 1498 */ 1499 int 1500 ext2_vinit(struct mount *mntp, struct vop_vector *fifoops, struct vnode **vpp) 1501 { 1502 struct inode *ip; 1503 struct vnode *vp; 1504 1505 vp = *vpp; 1506 ip = VTOI(vp); 1507 vp->v_type = IFTOVT(ip->i_mode); 1508 if (vp->v_type == VFIFO) 1509 vp->v_op = fifoops; 1510 1511 if (ip->i_number == EXT2_ROOTINO) 1512 vp->v_vflag |= VV_ROOT; 1513 ip->i_modrev = init_va_filerev(); 1514 *vpp = vp; 1515 return (0); 1516 } 1517 1518 /* 1519 * Allocate a new inode. 1520 */ 1521 static int 1522 ext2_makeinode(int mode, struct vnode *dvp, struct vnode **vpp, 1523 struct componentname *cnp) 1524 { 1525 struct inode *ip, *pdir; 1526 struct vnode *tvp; 1527 int error; 1528 1529 pdir = VTOI(dvp); 1530 #ifdef INVARIANTS 1531 if ((cnp->cn_flags & HASBUF) == 0) 1532 panic("ext2_makeinode: no name"); 1533 #endif 1534 *vpp = NULL; 1535 if ((mode & IFMT) == 0) 1536 mode |= IFREG; 1537 1538 error = ext2_valloc(dvp, mode, cnp->cn_cred, &tvp); 1539 if (error) { 1540 return (error); 1541 } 1542 ip = VTOI(tvp); 1543 ip->i_gid = pdir->i_gid; 1544 #ifdef SUIDDIR 1545 { 1546 /* 1547 * if we are 1548 * not the owner of the directory, 1549 * and we are hacking owners here, (only do this where told to) 1550 * and we are not giving it TOO root, (would subvert quotas) 1551 * then go ahead and give it to the other user. 1552 * Note that this drops off the execute bits for security. 1553 */ 1554 if ( (dvp->v_mount->mnt_flag & MNT_SUIDDIR) && 1555 (pdir->i_mode & ISUID) && 1556 (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) { 1557 ip->i_uid = pdir->i_uid; 1558 mode &= ~07111; 1559 } else { 1560 ip->i_uid = cnp->cn_cred->cr_uid; 1561 } 1562 } 1563 #else 1564 ip->i_uid = cnp->cn_cred->cr_uid; 1565 #endif 1566 ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; 1567 ip->i_mode = mode; 1568 tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */ 1569 ip->i_nlink = 1; 1570 if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred)) { 1571 if (priv_check_cred(cnp->cn_cred, PRIV_VFS_RETAINSUGID, 0)) 1572 ip->i_mode &= ~ISGID; 1573 } 1574 1575 if (cnp->cn_flags & ISWHITEOUT) 1576 ip->i_flags |= UF_OPAQUE; 1577 1578 /* 1579 * Make sure inode goes to disk before directory entry. 1580 */ 1581 error = ext2_update(tvp, !DOINGASYNC(tvp)); 1582 if (error) 1583 goto bad; 1584 error = ext2_direnter(ip, dvp, cnp); 1585 if (error) 1586 goto bad; 1587 1588 *vpp = tvp; 1589 return (0); 1590 1591 bad: 1592 /* 1593 * Write error occurred trying to update the inode 1594 * or the directory so must deallocate the inode. 1595 */ 1596 ip->i_nlink = 0; 1597 ip->i_flag |= IN_CHANGE; 1598 vput(tvp); 1599 return (error); 1600 } 1601 1602 /* 1603 * Vnode op for reading. 1604 */ 1605 static int 1606 ext2_read(struct vop_read_args *ap) 1607 { 1608 struct vnode *vp; 1609 struct inode *ip; 1610 struct uio *uio; 1611 struct m_ext2fs *fs; 1612 struct buf *bp; 1613 daddr_t lbn, nextlbn; 1614 off_t bytesinfile; 1615 long size, xfersize, blkoffset; 1616 int error, orig_resid, seqcount; 1617 int ioflag; 1618 1619 vp = ap->a_vp; 1620 uio = ap->a_uio; 1621 ioflag = ap->a_ioflag; 1622 1623 seqcount = ap->a_ioflag >> IO_SEQSHIFT; 1624 ip = VTOI(vp); 1625 1626 #ifdef INVARIANTS 1627 if (uio->uio_rw != UIO_READ) 1628 panic("%s: mode", "ext2_read"); 1629 1630 if (vp->v_type == VLNK) { 1631 if ((int)ip->i_size < vp->v_mount->mnt_maxsymlinklen) 1632 panic("%s: short symlink", "ext2_read"); 1633 } else if (vp->v_type != VREG && vp->v_type != VDIR) 1634 panic("%s: type %d", "ext2_read", vp->v_type); 1635 #endif 1636 orig_resid = uio->uio_resid; 1637 KASSERT(orig_resid >= 0, ("ext2_read: uio->uio_resid < 0")); 1638 if (orig_resid == 0) 1639 return (0); 1640 KASSERT(uio->uio_offset >= 0, ("ext2_read: uio->uio_offset < 0")); 1641 fs = ip->i_e2fs; 1642 if (uio->uio_offset < ip->i_size && 1643 uio->uio_offset >= fs->e2fs_maxfilesize) 1644 return (EOVERFLOW); 1645 1646 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) { 1647 if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0) 1648 break; 1649 lbn = lblkno(fs, uio->uio_offset); 1650 nextlbn = lbn + 1; 1651 size = blksize(fs, ip, lbn); 1652 blkoffset = blkoff(fs, uio->uio_offset); 1653 1654 xfersize = fs->e2fs_fsize - blkoffset; 1655 if (uio->uio_resid < xfersize) 1656 xfersize = uio->uio_resid; 1657 if (bytesinfile < xfersize) 1658 xfersize = bytesinfile; 1659 1660 if (lblktosize(fs, nextlbn) >= ip->i_size) 1661 error = bread(vp, lbn, size, NOCRED, &bp); 1662 else if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) { 1663 error = cluster_read(vp, ip->i_size, lbn, size, 1664 NOCRED, blkoffset + uio->uio_resid, seqcount, 1665 0, &bp); 1666 } else if (seqcount > 1) { 1667 int nextsize = blksize(fs, ip, nextlbn); 1668 error = breadn(vp, lbn, 1669 size, &nextlbn, &nextsize, 1, NOCRED, &bp); 1670 } else 1671 error = bread(vp, lbn, size, NOCRED, &bp); 1672 if (error) { 1673 brelse(bp); 1674 bp = NULL; 1675 break; 1676 } 1677 1678 /* 1679 * If IO_DIRECT then set B_DIRECT for the buffer. This 1680 * will cause us to attempt to release the buffer later on 1681 * and will cause the buffer cache to attempt to free the 1682 * underlying pages. 1683 */ 1684 if (ioflag & IO_DIRECT) 1685 bp->b_flags |= B_DIRECT; 1686 1687 /* 1688 * We should only get non-zero b_resid when an I/O error 1689 * has occurred, which should cause us to break above. 1690 * However, if the short read did not cause an error, 1691 * then we want to ensure that we do not uiomove bad 1692 * or uninitialized data. 1693 */ 1694 size -= bp->b_resid; 1695 if (size < xfersize) { 1696 if (size == 0) 1697 break; 1698 xfersize = size; 1699 } 1700 error = uiomove((char *)bp->b_data + blkoffset, 1701 (int)xfersize, uio); 1702 if (error) 1703 break; 1704 1705 if (ioflag & (IO_VMIO|IO_DIRECT)) { 1706 /* 1707 * If it's VMIO or direct I/O, then we don't 1708 * need the buf, mark it available for 1709 * freeing. If it's non-direct VMIO, the VM has 1710 * the data. 1711 */ 1712 bp->b_flags |= B_RELBUF; 1713 brelse(bp); 1714 } else { 1715 /* 1716 * Otherwise let whoever 1717 * made the request take care of 1718 * freeing it. We just queue 1719 * it onto another list. 1720 */ 1721 bqrelse(bp); 1722 } 1723 } 1724 1725 /* 1726 * This can only happen in the case of an error 1727 * because the loop above resets bp to NULL on each iteration 1728 * and on normal completion has not set a new value into it. 1729 * so it must have come from a 'break' statement 1730 */ 1731 if (bp != NULL) { 1732 if (ioflag & (IO_VMIO|IO_DIRECT)) { 1733 bp->b_flags |= B_RELBUF; 1734 brelse(bp); 1735 } else { 1736 bqrelse(bp); 1737 } 1738 } 1739 1740 if ((error == 0 || uio->uio_resid != orig_resid) && 1741 (vp->v_mount->mnt_flag & MNT_NOATIME) == 0) 1742 ip->i_flag |= IN_ACCESS; 1743 return (error); 1744 } 1745 1746 static int 1747 ext2_ioctl(struct vop_ioctl_args *ap) 1748 { 1749 1750 switch (ap->a_command) { 1751 case FIOSEEKDATA: 1752 case FIOSEEKHOLE: 1753 return (vn_bmap_seekhole(ap->a_vp, ap->a_command, 1754 (off_t *)ap->a_data, ap->a_cred)); 1755 default: 1756 return (ENOTTY); 1757 } 1758 } 1759 1760 /* 1761 * Vnode op for writing. 1762 */ 1763 static int 1764 ext2_write(struct vop_write_args *ap) 1765 { 1766 struct vnode *vp; 1767 struct uio *uio; 1768 struct inode *ip; 1769 struct m_ext2fs *fs; 1770 struct buf *bp; 1771 daddr_t lbn; 1772 off_t osize; 1773 int blkoffset, error, flags, ioflag, resid, size, seqcount, xfersize; 1774 1775 ioflag = ap->a_ioflag; 1776 uio = ap->a_uio; 1777 vp = ap->a_vp; 1778 1779 seqcount = ioflag >> IO_SEQSHIFT; 1780 ip = VTOI(vp); 1781 1782 #ifdef INVARIANTS 1783 if (uio->uio_rw != UIO_WRITE) 1784 panic("%s: mode", "ext2_write"); 1785 #endif 1786 1787 switch (vp->v_type) { 1788 case VREG: 1789 if (ioflag & IO_APPEND) 1790 uio->uio_offset = ip->i_size; 1791 if ((ip->i_flags & APPEND) && uio->uio_offset != ip->i_size) 1792 return (EPERM); 1793 /* FALLTHROUGH */ 1794 case VLNK: 1795 break; 1796 case VDIR: 1797 /* XXX differs from ffs -- this is called from ext2_mkdir(). */ 1798 if ((ioflag & IO_SYNC) == 0) 1799 panic("ext2_write: nonsync dir write"); 1800 break; 1801 default: 1802 panic("ext2_write: type %p %d (%jd,%jd)", (void *)vp, 1803 vp->v_type, (intmax_t)uio->uio_offset, 1804 (intmax_t)uio->uio_resid); 1805 } 1806 1807 KASSERT(uio->uio_resid >= 0, ("ext2_write: uio->uio_resid < 0")); 1808 KASSERT(uio->uio_offset >= 0, ("ext2_write: uio->uio_offset < 0")); 1809 fs = ip->i_e2fs; 1810 if ((uoff_t)uio->uio_offset + uio->uio_resid > fs->e2fs_maxfilesize) 1811 return (EFBIG); 1812 /* 1813 * Maybe this should be above the vnode op call, but so long as 1814 * file servers have no limits, I don't think it matters. 1815 */ 1816 if (vn_rlimit_fsize(vp, uio, uio->uio_td)) 1817 return (EFBIG); 1818 1819 resid = uio->uio_resid; 1820 osize = ip->i_size; 1821 if (seqcount > BA_SEQMAX) 1822 flags = BA_SEQMAX << BA_SEQSHIFT; 1823 else 1824 flags = seqcount << BA_SEQSHIFT; 1825 if ((ioflag & IO_SYNC) && !DOINGASYNC(vp)) 1826 flags |= IO_SYNC; 1827 1828 for (error = 0; uio->uio_resid > 0;) { 1829 lbn = lblkno(fs, uio->uio_offset); 1830 blkoffset = blkoff(fs, uio->uio_offset); 1831 xfersize = fs->e2fs_fsize - blkoffset; 1832 if (uio->uio_resid < xfersize) 1833 xfersize = uio->uio_resid; 1834 if (uio->uio_offset + xfersize > ip->i_size) 1835 vnode_pager_setsize(vp, uio->uio_offset + xfersize); 1836 1837 /* 1838 * We must perform a read-before-write if the transfer size 1839 * does not cover the entire buffer. 1840 */ 1841 if (fs->e2fs_bsize > xfersize) 1842 flags |= BA_CLRBUF; 1843 else 1844 flags &= ~BA_CLRBUF; 1845 error = ext2_balloc(ip, lbn, blkoffset + xfersize, 1846 ap->a_cred, &bp, flags); 1847 if (error != 0) 1848 break; 1849 1850 if ((ioflag & (IO_SYNC|IO_INVAL)) == (IO_SYNC|IO_INVAL)) 1851 bp->b_flags |= B_NOCACHE; 1852 if (uio->uio_offset + xfersize > ip->i_size) 1853 ip->i_size = uio->uio_offset + xfersize; 1854 size = blksize(fs, ip, lbn) - bp->b_resid; 1855 if (size < xfersize) 1856 xfersize = size; 1857 1858 error = 1859 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio); 1860 /* 1861 * If the buffer is not already filled and we encounter an 1862 * error while trying to fill it, we have to clear out any 1863 * garbage data from the pages instantiated for the buffer. 1864 * If we do not, a failed uiomove() during a write can leave 1865 * the prior contents of the pages exposed to a userland mmap. 1866 * 1867 * Note that we need only clear buffers with a transfer size 1868 * equal to the block size because buffers with a shorter 1869 * transfer size were cleared above by the call to ext2_balloc() 1870 * with the BA_CLRBUF flag set. 1871 * 1872 * If the source region for uiomove identically mmaps the 1873 * buffer, uiomove() performed the NOP copy, and the buffer 1874 * content remains valid because the page fault handler 1875 * validated the pages. 1876 */ 1877 if (error != 0 && (bp->b_flags & B_CACHE) == 0 && 1878 fs->e2fs_bsize == xfersize) 1879 vfs_bio_clrbuf(bp); 1880 if (ioflag & (IO_VMIO|IO_DIRECT)) { 1881 bp->b_flags |= B_RELBUF; 1882 } 1883 1884 /* 1885 * If IO_SYNC each buffer is written synchronously. Otherwise 1886 * if we have a severe page deficiency write the buffer 1887 * asynchronously. Otherwise try to cluster, and if that 1888 * doesn't do it then either do an async write (if O_DIRECT), 1889 * or a delayed write (if not). 1890 */ 1891 if (ioflag & IO_SYNC) { 1892 (void)bwrite(bp); 1893 } else if (vm_page_count_severe() || 1894 buf_dirty_count_severe() || 1895 (ioflag & IO_ASYNC)) { 1896 bp->b_flags |= B_CLUSTEROK; 1897 bawrite(bp); 1898 } else if (xfersize + blkoffset == fs->e2fs_fsize) { 1899 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERW) == 0) { 1900 bp->b_flags |= B_CLUSTEROK; 1901 cluster_write(vp, bp, ip->i_size, seqcount, 0); 1902 } else { 1903 bawrite(bp); 1904 } 1905 } else if (ioflag & IO_DIRECT) { 1906 bp->b_flags |= B_CLUSTEROK; 1907 bawrite(bp); 1908 } else { 1909 bp->b_flags |= B_CLUSTEROK; 1910 bdwrite(bp); 1911 } 1912 if (error || xfersize == 0) 1913 break; 1914 } 1915 /* 1916 * If we successfully wrote any data, and we are not the superuser 1917 * we clear the setuid and setgid bits as a precaution against 1918 * tampering. 1919 */ 1920 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && 1921 ap->a_cred) { 1922 if (priv_check_cred(ap->a_cred, PRIV_VFS_RETAINSUGID, 0)) 1923 ip->i_mode &= ~(ISUID | ISGID); 1924 } 1925 if (error) { 1926 if (ioflag & IO_UNIT) { 1927 (void)ext2_truncate(vp, osize, 1928 ioflag & IO_SYNC, ap->a_cred, uio->uio_td); 1929 uio->uio_offset -= resid - uio->uio_resid; 1930 uio->uio_resid = resid; 1931 } 1932 } 1933 if (uio->uio_resid != resid) { 1934 ip->i_flag |= IN_CHANGE | IN_UPDATE; 1935 if (ioflag & IO_SYNC) 1936 error = ext2_update(vp, 1); 1937 } 1938 return (error); 1939 } 1940