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