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