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