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