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