| 1 | /* |
| 2 | * Copyright (c) 1982, 1986, 1989, 1993, 1995 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * (c) UNIX System Laboratories, Inc. |
| 5 | * All or some portions of this file are derived from material licensed |
| 6 | * to the University of California by American Telephone and Telegraph |
| 7 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
| 8 | * the permission of UNIX System Laboratories, Inc. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * 3. All advertising materials mentioning features or use of this software |
| 19 | * must display the following acknowledgement: |
| 20 | * This product includes software developed by the University of |
| 21 | * California, Berkeley and its contributors. |
| 22 | * 4. Neither the name of the University nor the names of its contributors |
| 23 | * may be used to endorse or promote products derived from this software |
| 24 | * without specific prior written permission. |
| 25 | * |
| 26 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 27 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 28 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 29 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 30 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 31 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 32 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 33 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 34 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 35 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 36 | * SUCH DAMAGE. |
| 37 | * |
| 38 | * @(#)ufs_vnops.c 8.27 (Berkeley) 5/27/95 |
| 39 | * $FreeBSD: src/sys/ufs/ufs/ufs_vnops.c,v 1.131.2.8 2003/01/02 17:26:19 bde Exp $ |
| 40 | * $DragonFly: src/sys/vfs/ufs/ufs_vnops.c,v 1.23 2004/10/12 19:21:12 dillon Exp $ |
| 41 | */ |
| 42 | |
| 43 | #include "opt_quota.h" |
| 44 | #include "opt_suiddir.h" |
| 45 | #include "opt_ufs.h" |
| 46 | |
| 47 | #include <sys/param.h> |
| 48 | #include <sys/systm.h> |
| 49 | #include <sys/kernel.h> |
| 50 | #include <sys/fcntl.h> |
| 51 | #include <sys/stat.h> |
| 52 | #include <sys/buf.h> |
| 53 | #include <sys/proc.h> |
| 54 | #include <sys/namei.h> |
| 55 | #include <sys/mount.h> |
| 56 | #include <sys/unistd.h> |
| 57 | #include <sys/vnode.h> |
| 58 | #include <sys/malloc.h> |
| 59 | #include <sys/dirent.h> |
| 60 | #include <sys/lockf.h> |
| 61 | #include <sys/event.h> |
| 62 | #include <sys/conf.h> |
| 63 | |
| 64 | #include <sys/file.h> /* XXX */ |
| 65 | |
| 66 | #include <vm/vm.h> |
| 67 | #include <vm/vm_extern.h> |
| 68 | |
| 69 | #include <vfs/fifofs/fifo.h> |
| 70 | |
| 71 | #include "quota.h" |
| 72 | #include "inode.h" |
| 73 | #include "dir.h" |
| 74 | #include "ufsmount.h" |
| 75 | #include "ufs_extern.h" |
| 76 | #ifdef UFS_DIRHASH |
| 77 | #include "dirhash.h" |
| 78 | #endif |
| 79 | |
| 80 | static int ufs_access (struct vop_access_args *); |
| 81 | static int ufs_advlock (struct vop_advlock_args *); |
| 82 | static int ufs_chmod (struct vnode *, int, struct ucred *, struct thread *); |
| 83 | static int ufs_chown (struct vnode *, uid_t, gid_t, struct ucred *, struct thread *); |
| 84 | static int ufs_close (struct vop_close_args *); |
| 85 | static int ufs_create (struct vop_create_args *); |
| 86 | static int ufs_getattr (struct vop_getattr_args *); |
| 87 | static int ufs_link (struct vop_link_args *); |
| 88 | static int ufs_makeinode (int mode, struct vnode *, struct vnode **, struct componentname *); |
| 89 | static int ufs_missingop (struct vop_generic_args *ap); |
| 90 | static int ufs_mkdir (struct vop_mkdir_args *); |
| 91 | static int ufs_mknod (struct vop_mknod_args *); |
| 92 | static int ufs_mmap (struct vop_mmap_args *); |
| 93 | static int ufs_open (struct vop_open_args *); |
| 94 | static int ufs_pathconf (struct vop_pathconf_args *); |
| 95 | static int ufs_print (struct vop_print_args *); |
| 96 | static int ufs_readdir (struct vop_readdir_args *); |
| 97 | static int ufs_readlink (struct vop_readlink_args *); |
| 98 | static int ufs_remove (struct vop_remove_args *); |
| 99 | static int ufs_rename (struct vop_rename_args *); |
| 100 | static int ufs_rmdir (struct vop_rmdir_args *); |
| 101 | static int ufs_setattr (struct vop_setattr_args *); |
| 102 | static int ufs_strategy (struct vop_strategy_args *); |
| 103 | static int ufs_symlink (struct vop_symlink_args *); |
| 104 | static int ufs_whiteout (struct vop_whiteout_args *); |
| 105 | static int ufsfifo_close (struct vop_close_args *); |
| 106 | static int ufsfifo_kqfilter (struct vop_kqfilter_args *); |
| 107 | static int ufsfifo_read (struct vop_read_args *); |
| 108 | static int ufsfifo_write (struct vop_write_args *); |
| 109 | static int ufsspec_close (struct vop_close_args *); |
| 110 | static int ufsspec_read (struct vop_read_args *); |
| 111 | static int ufsspec_write (struct vop_write_args *); |
| 112 | static int filt_ufsread (struct knote *kn, long hint); |
| 113 | static int filt_ufswrite (struct knote *kn, long hint); |
| 114 | static int filt_ufsvnode (struct knote *kn, long hint); |
| 115 | static void filt_ufsdetach (struct knote *kn); |
| 116 | static int ufs_kqfilter (struct vop_kqfilter_args *ap); |
| 117 | |
| 118 | union _qcvt { |
| 119 | int64_t qcvt; |
| 120 | int32_t val[2]; |
| 121 | }; |
| 122 | #define SETHIGH(q, h) { \ |
| 123 | union _qcvt tmp; \ |
| 124 | tmp.qcvt = (q); \ |
| 125 | tmp.val[_QUAD_HIGHWORD] = (h); \ |
| 126 | (q) = tmp.qcvt; \ |
| 127 | } |
| 128 | #define SETLOW(q, l) { \ |
| 129 | union _qcvt tmp; \ |
| 130 | tmp.qcvt = (q); \ |
| 131 | tmp.val[_QUAD_LOWWORD] = (l); \ |
| 132 | (q) = tmp.qcvt; \ |
| 133 | } |
| 134 | #define VN_KNOTE(vp, b) \ |
| 135 | KNOTE(&vp->v_pollinfo.vpi_selinfo.si_note, (b)) |
| 136 | |
| 137 | /* |
| 138 | * A virgin directory (no blushing please). |
| 139 | */ |
| 140 | static struct dirtemplate mastertemplate = { |
| 141 | 0, 12, DT_DIR, 1, ".", |
| 142 | 0, DIRBLKSIZ - 12, DT_DIR, 2, ".." |
| 143 | }; |
| 144 | static struct odirtemplate omastertemplate = { |
| 145 | 0, 12, 1, ".", |
| 146 | 0, DIRBLKSIZ - 12, 2, ".." |
| 147 | }; |
| 148 | |
| 149 | void |
| 150 | ufs_itimes(struct vnode *vp) |
| 151 | { |
| 152 | struct inode *ip; |
| 153 | struct timespec ts; |
| 154 | |
| 155 | ip = VTOI(vp); |
| 156 | if ((ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_UPDATE)) == 0) |
| 157 | return; |
| 158 | if ((vp->v_type == VBLK || vp->v_type == VCHR) && !DOINGSOFTDEP(vp)) |
| 159 | ip->i_flag |= IN_LAZYMOD; |
| 160 | else |
| 161 | ip->i_flag |= IN_MODIFIED; |
| 162 | if ((vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { |
| 163 | vfs_timestamp(&ts); |
| 164 | if (ip->i_flag & IN_ACCESS) { |
| 165 | ip->i_atime = ts.tv_sec; |
| 166 | ip->i_atimensec = ts.tv_nsec; |
| 167 | } |
| 168 | if (ip->i_flag & IN_UPDATE) { |
| 169 | ip->i_mtime = ts.tv_sec; |
| 170 | ip->i_mtimensec = ts.tv_nsec; |
| 171 | ip->i_modrev++; |
| 172 | } |
| 173 | if (ip->i_flag & IN_CHANGE) { |
| 174 | ip->i_ctime = ts.tv_sec; |
| 175 | ip->i_ctimensec = ts.tv_nsec; |
| 176 | } |
| 177 | } |
| 178 | ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_UPDATE); |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * Create a regular file |
| 183 | * |
| 184 | * ufs_create(struct vnode *a_dvp, struct vnode **a_vpp, |
| 185 | * struct componentname *a_cnp, struct vattr *a_vap) |
| 186 | */ |
| 187 | static |
| 188 | int |
| 189 | ufs_create(struct vop_create_args *ap) |
| 190 | { |
| 191 | int error; |
| 192 | |
| 193 | error = |
| 194 | ufs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode), |
| 195 | ap->a_dvp, ap->a_vpp, ap->a_cnp); |
| 196 | if (error) |
| 197 | return (error); |
| 198 | VN_KNOTE(ap->a_dvp, NOTE_WRITE); |
| 199 | return (0); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * Mknod vnode call |
| 204 | * |
| 205 | * ufs_mknod(struct vnode *a_dvp, struct vnode **a_vpp, |
| 206 | * struct componentname *a_cnp, struct vattr *a_vap) |
| 207 | */ |
| 208 | /* ARGSUSED */ |
| 209 | static |
| 210 | int |
| 211 | ufs_mknod(struct vop_mknod_args *ap) |
| 212 | { |
| 213 | struct vattr *vap = ap->a_vap; |
| 214 | struct vnode **vpp = ap->a_vpp; |
| 215 | struct inode *ip; |
| 216 | ino_t ino; |
| 217 | int error; |
| 218 | |
| 219 | error = ufs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), |
| 220 | ap->a_dvp, vpp, ap->a_cnp); |
| 221 | if (error) |
| 222 | return (error); |
| 223 | VN_KNOTE(ap->a_dvp, NOTE_WRITE); |
| 224 | ip = VTOI(*vpp); |
| 225 | ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; |
| 226 | if (vap->va_rdev != VNOVAL) { |
| 227 | /* |
| 228 | * Want to be able to use this to make badblock |
| 229 | * inodes, so don't truncate the dev number. |
| 230 | */ |
| 231 | ip->i_rdev = vap->va_rdev; |
| 232 | } |
| 233 | /* |
| 234 | * Remove inode, then reload it through VFS_VGET so it is |
| 235 | * checked to see if it is an alias of an existing entry in |
| 236 | * the inode cache. |
| 237 | */ |
| 238 | (*vpp)->v_type = VNON; |
| 239 | ino = ip->i_number; /* Save this before vgone() invalidates ip. */ |
| 240 | vgone(*vpp); |
| 241 | vput(*vpp); |
| 242 | error = VFS_VGET(ap->a_dvp->v_mount, ino, vpp); |
| 243 | if (error) { |
| 244 | *vpp = NULL; |
| 245 | return (error); |
| 246 | } |
| 247 | return (0); |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Open called. |
| 252 | * |
| 253 | * Nothing to do. |
| 254 | * |
| 255 | * ufs_open(struct vnode *a_vp, int a_mode, struct ucred *a_cred, |
| 256 | * struct thread *a_td) |
| 257 | */ |
| 258 | /* ARGSUSED */ |
| 259 | static |
| 260 | int |
| 261 | ufs_open(struct vop_open_args *ap) |
| 262 | { |
| 263 | /* |
| 264 | * Files marked append-only must be opened for appending. |
| 265 | */ |
| 266 | if ((VTOI(ap->a_vp)->i_flags & APPEND) && |
| 267 | (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) |
| 268 | return (EPERM); |
| 269 | return (0); |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Close called. |
| 274 | * |
| 275 | * Update the times on the inode. |
| 276 | * |
| 277 | * ufs_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred, |
| 278 | * struct thread *a_td) |
| 279 | */ |
| 280 | /* ARGSUSED */ |
| 281 | static |
| 282 | int |
| 283 | ufs_close(struct vop_close_args *ap) |
| 284 | { |
| 285 | struct vnode *vp = ap->a_vp; |
| 286 | |
| 287 | if (vp->v_usecount > 1) |
| 288 | ufs_itimes(vp); |
| 289 | return (0); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * ufs_access(struct vnode *a_vp, int a_mode, struct ucred *a_cred, |
| 294 | * struct thread *a_td) |
| 295 | */ |
| 296 | static |
| 297 | int |
| 298 | ufs_access(struct vop_access_args *ap) |
| 299 | { |
| 300 | struct vnode *vp = ap->a_vp; |
| 301 | struct inode *ip = VTOI(vp); |
| 302 | struct ucred *cred = ap->a_cred; |
| 303 | mode_t mask, mode = ap->a_mode; |
| 304 | gid_t *gp; |
| 305 | int i; |
| 306 | #ifdef QUOTA |
| 307 | int error; |
| 308 | #endif |
| 309 | |
| 310 | /* |
| 311 | * Disallow write attempts on read-only filesystems; |
| 312 | * unless the file is a socket, fifo, or a block or |
| 313 | * character device resident on the filesystem. |
| 314 | */ |
| 315 | if (mode & VWRITE) { |
| 316 | switch (vp->v_type) { |
| 317 | case VDIR: |
| 318 | case VLNK: |
| 319 | case VREG: |
| 320 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 321 | return (EROFS); |
| 322 | #ifdef QUOTA |
| 323 | if ((error = getinoquota(ip)) != 0) |
| 324 | return (error); |
| 325 | #endif |
| 326 | break; |
| 327 | default: |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | /* If immutable bit set, nobody gets to write it. */ |
| 333 | if ((mode & VWRITE) && (ip->i_flags & IMMUTABLE)) |
| 334 | return (EPERM); |
| 335 | |
| 336 | /* Otherwise, user id 0 always gets access. */ |
| 337 | if (cred->cr_uid == 0) |
| 338 | return (0); |
| 339 | |
| 340 | mask = 0; |
| 341 | |
| 342 | /* Otherwise, check the owner. */ |
| 343 | if (cred->cr_uid == ip->i_uid) { |
| 344 | if (mode & VEXEC) |
| 345 | mask |= S_IXUSR; |
| 346 | if (mode & VREAD) |
| 347 | mask |= S_IRUSR; |
| 348 | if (mode & VWRITE) |
| 349 | mask |= S_IWUSR; |
| 350 | return ((ip->i_mode & mask) == mask ? 0 : EACCES); |
| 351 | } |
| 352 | |
| 353 | /* Otherwise, check the groups. */ |
| 354 | for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++) |
| 355 | if (ip->i_gid == *gp) { |
| 356 | if (mode & VEXEC) |
| 357 | mask |= S_IXGRP; |
| 358 | if (mode & VREAD) |
| 359 | mask |= S_IRGRP; |
| 360 | if (mode & VWRITE) |
| 361 | mask |= S_IWGRP; |
| 362 | return ((ip->i_mode & mask) == mask ? 0 : EACCES); |
| 363 | } |
| 364 | |
| 365 | /* Otherwise, check everyone else. */ |
| 366 | if (mode & VEXEC) |
| 367 | mask |= S_IXOTH; |
| 368 | if (mode & VREAD) |
| 369 | mask |= S_IROTH; |
| 370 | if (mode & VWRITE) |
| 371 | mask |= S_IWOTH; |
| 372 | return ((ip->i_mode & mask) == mask ? 0 : EACCES); |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * ufs_getattr(struct vnode *a_vp, struct vattr *a_vap, |
| 377 | * struct thread *a_td) |
| 378 | */ |
| 379 | /* ARGSUSED */ |
| 380 | static |
| 381 | int |
| 382 | ufs_getattr(struct vop_getattr_args *ap) |
| 383 | { |
| 384 | struct vnode *vp = ap->a_vp; |
| 385 | struct inode *ip = VTOI(vp); |
| 386 | struct vattr *vap = ap->a_vap; |
| 387 | |
| 388 | ufs_itimes(vp); |
| 389 | /* |
| 390 | * Copy from inode table |
| 391 | */ |
| 392 | vap->va_fsid = dev2udev(ip->i_dev); |
| 393 | vap->va_fileid = ip->i_number; |
| 394 | vap->va_mode = ip->i_mode & ~IFMT; |
| 395 | vap->va_nlink = VFSTOUFS(vp->v_mount)->um_i_effnlink_valid ? |
| 396 | ip->i_effnlink : ip->i_nlink; |
| 397 | vap->va_uid = ip->i_uid; |
| 398 | vap->va_gid = ip->i_gid; |
| 399 | vap->va_rdev = ip->i_rdev; |
| 400 | vap->va_size = ip->i_din.di_size; |
| 401 | vap->va_atime.tv_sec = ip->i_atime; |
| 402 | vap->va_atime.tv_nsec = ip->i_atimensec; |
| 403 | vap->va_mtime.tv_sec = ip->i_mtime; |
| 404 | vap->va_mtime.tv_nsec = ip->i_mtimensec; |
| 405 | vap->va_ctime.tv_sec = ip->i_ctime; |
| 406 | vap->va_ctime.tv_nsec = ip->i_ctimensec; |
| 407 | vap->va_flags = ip->i_flags; |
| 408 | vap->va_gen = ip->i_gen; |
| 409 | vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize; |
| 410 | vap->va_bytes = dbtob((u_quad_t)ip->i_blocks); |
| 411 | vap->va_type = IFTOVT(ip->i_mode); |
| 412 | vap->va_filerev = ip->i_modrev; |
| 413 | return (0); |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Set attribute vnode op. called from several syscalls |
| 418 | * |
| 419 | * ufs_setattr(struct vnode *a_vp, struct vattr *a_vap, |
| 420 | * struct ucred *a_cred, struct thread *a_td) |
| 421 | */ |
| 422 | static |
| 423 | int |
| 424 | ufs_setattr(struct vop_setattr_args *ap) |
| 425 | { |
| 426 | struct vattr *vap = ap->a_vap; |
| 427 | struct vnode *vp = ap->a_vp; |
| 428 | struct inode *ip = VTOI(vp); |
| 429 | struct ucred *cred = ap->a_cred; |
| 430 | int error; |
| 431 | |
| 432 | /* |
| 433 | * Check for unsettable attributes. |
| 434 | */ |
| 435 | if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || |
| 436 | (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || |
| 437 | (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || |
| 438 | ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { |
| 439 | return (EINVAL); |
| 440 | } |
| 441 | if (vap->va_flags != VNOVAL) { |
| 442 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 443 | return (EROFS); |
| 444 | if (cred->cr_uid != ip->i_uid && |
| 445 | (error = suser_cred(cred, PRISON_ROOT))) |
| 446 | return (error); |
| 447 | if ((cred->cr_uid == 0) && (cred->cr_prison == NULL)) { |
| 448 | if ((ip->i_flags |
| 449 | & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND)) && |
| 450 | securelevel > 0) |
| 451 | return (EPERM); |
| 452 | ip->i_flags = vap->va_flags; |
| 453 | } else { |
| 454 | if (ip->i_flags |
| 455 | & (SF_NOUNLINK | SF_IMMUTABLE | SF_APPEND) || |
| 456 | (vap->va_flags & UF_SETTABLE) != vap->va_flags) |
| 457 | return (EPERM); |
| 458 | ip->i_flags &= SF_SETTABLE; |
| 459 | ip->i_flags |= (vap->va_flags & UF_SETTABLE); |
| 460 | } |
| 461 | ip->i_flag |= IN_CHANGE; |
| 462 | if (vap->va_flags & (IMMUTABLE | APPEND)) |
| 463 | return (0); |
| 464 | } |
| 465 | if (ip->i_flags & (IMMUTABLE | APPEND)) |
| 466 | return (EPERM); |
| 467 | /* |
| 468 | * Go through the fields and update iff not VNOVAL. |
| 469 | */ |
| 470 | if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { |
| 471 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 472 | return (EROFS); |
| 473 | if ((error = ufs_chown(vp, vap->va_uid, vap->va_gid, cred, ap->a_td)) != 0) |
| 474 | return (error); |
| 475 | } |
| 476 | if (vap->va_size != VNOVAL) { |
| 477 | /* |
| 478 | * Disallow write attempts on read-only filesystems; |
| 479 | * unless the file is a socket, fifo, or a block or |
| 480 | * character device resident on the filesystem. |
| 481 | */ |
| 482 | switch (vp->v_type) { |
| 483 | case VDIR: |
| 484 | return (EISDIR); |
| 485 | case VLNK: |
| 486 | case VREG: |
| 487 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 488 | return (EROFS); |
| 489 | break; |
| 490 | default: |
| 491 | break; |
| 492 | } |
| 493 | if ((error = UFS_TRUNCATE(vp, vap->va_size, 0, cred, ap->a_td)) != 0) |
| 494 | return (error); |
| 495 | } |
| 496 | ip = VTOI(vp); |
| 497 | if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { |
| 498 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 499 | return (EROFS); |
| 500 | if (cred->cr_uid != ip->i_uid && |
| 501 | (error = suser_cred(cred, PRISON_ROOT)) && |
| 502 | ((vap->va_vaflags & VA_UTIMES_NULL) == 0 || |
| 503 | (error = VOP_ACCESS(vp, VWRITE, cred, ap->a_td)))) |
| 504 | return (error); |
| 505 | if (vap->va_atime.tv_sec != VNOVAL) |
| 506 | ip->i_flag |= IN_ACCESS; |
| 507 | if (vap->va_mtime.tv_sec != VNOVAL) |
| 508 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 509 | ufs_itimes(vp); |
| 510 | if (vap->va_atime.tv_sec != VNOVAL) { |
| 511 | ip->i_atime = vap->va_atime.tv_sec; |
| 512 | ip->i_atimensec = vap->va_atime.tv_nsec; |
| 513 | } |
| 514 | if (vap->va_mtime.tv_sec != VNOVAL) { |
| 515 | ip->i_mtime = vap->va_mtime.tv_sec; |
| 516 | ip->i_mtimensec = vap->va_mtime.tv_nsec; |
| 517 | } |
| 518 | error = UFS_UPDATE(vp, 0); |
| 519 | if (error) |
| 520 | return (error); |
| 521 | } |
| 522 | error = 0; |
| 523 | if (vap->va_mode != (mode_t)VNOVAL) { |
| 524 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 525 | return (EROFS); |
| 526 | error = ufs_chmod(vp, (int)vap->va_mode, cred, ap->a_td); |
| 527 | } |
| 528 | VN_KNOTE(vp, NOTE_ATTRIB); |
| 529 | return (error); |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Change the mode on a file. |
| 534 | * Inode must be locked before calling. |
| 535 | */ |
| 536 | static int |
| 537 | ufs_chmod(struct vnode *vp, int mode, struct ucred *cred, struct thread *td) |
| 538 | { |
| 539 | struct inode *ip = VTOI(vp); |
| 540 | int error; |
| 541 | |
| 542 | if (cred->cr_uid != ip->i_uid) { |
| 543 | error = suser_cred(cred, PRISON_ROOT); |
| 544 | if (error) |
| 545 | return (error); |
| 546 | } |
| 547 | if (cred->cr_uid) { |
| 548 | if (vp->v_type != VDIR && (mode & S_ISTXT)) |
| 549 | return (EFTYPE); |
| 550 | if (!groupmember(ip->i_gid, cred) && (mode & ISGID)) |
| 551 | return (EPERM); |
| 552 | } |
| 553 | ip->i_mode &= ~ALLPERMS; |
| 554 | ip->i_mode |= (mode & ALLPERMS); |
| 555 | ip->i_flag |= IN_CHANGE; |
| 556 | return (0); |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Perform chown operation on inode ip; |
| 561 | * inode must be locked prior to call. |
| 562 | */ |
| 563 | static int |
| 564 | ufs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred, |
| 565 | struct thread *td) |
| 566 | { |
| 567 | struct inode *ip = VTOI(vp); |
| 568 | uid_t ouid; |
| 569 | gid_t ogid; |
| 570 | int error = 0; |
| 571 | #ifdef QUOTA |
| 572 | int i; |
| 573 | long change; |
| 574 | #endif |
| 575 | |
| 576 | if (uid == (uid_t)VNOVAL) |
| 577 | uid = ip->i_uid; |
| 578 | if (gid == (gid_t)VNOVAL) |
| 579 | gid = ip->i_gid; |
| 580 | /* |
| 581 | * If we don't own the file, are trying to change the owner |
| 582 | * of the file, or are not a member of the target group, |
| 583 | * the caller must be superuser or the call fails. |
| 584 | */ |
| 585 | if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || |
| 586 | (gid != ip->i_gid && !(cred->cr_gid == gid || |
| 587 | groupmember((gid_t)gid, cred)))) && |
| 588 | (error = suser_cred(cred, PRISON_ROOT))) |
| 589 | return (error); |
| 590 | ogid = ip->i_gid; |
| 591 | ouid = ip->i_uid; |
| 592 | #ifdef QUOTA |
| 593 | if ((error = getinoquota(ip)) != 0) |
| 594 | return (error); |
| 595 | if (ouid == uid) { |
| 596 | dqrele(vp, ip->i_dquot[USRQUOTA]); |
| 597 | ip->i_dquot[USRQUOTA] = NODQUOT; |
| 598 | } |
| 599 | if (ogid == gid) { |
| 600 | dqrele(vp, ip->i_dquot[GRPQUOTA]); |
| 601 | ip->i_dquot[GRPQUOTA] = NODQUOT; |
| 602 | } |
| 603 | change = ip->i_blocks; |
| 604 | (void) chkdq(ip, -change, cred, CHOWN); |
| 605 | (void) chkiq(ip, -1, cred, CHOWN); |
| 606 | for (i = 0; i < MAXQUOTAS; i++) { |
| 607 | dqrele(vp, ip->i_dquot[i]); |
| 608 | ip->i_dquot[i] = NODQUOT; |
| 609 | } |
| 610 | #endif |
| 611 | ip->i_gid = gid; |
| 612 | ip->i_uid = uid; |
| 613 | #ifdef QUOTA |
| 614 | if ((error = getinoquota(ip)) == 0) { |
| 615 | if (ouid == uid) { |
| 616 | dqrele(vp, ip->i_dquot[USRQUOTA]); |
| 617 | ip->i_dquot[USRQUOTA] = NODQUOT; |
| 618 | } |
| 619 | if (ogid == gid) { |
| 620 | dqrele(vp, ip->i_dquot[GRPQUOTA]); |
| 621 | ip->i_dquot[GRPQUOTA] = NODQUOT; |
| 622 | } |
| 623 | if ((error = chkdq(ip, change, cred, CHOWN)) == 0) { |
| 624 | if ((error = chkiq(ip, 1, cred, CHOWN)) == 0) |
| 625 | goto good; |
| 626 | else |
| 627 | (void) chkdq(ip, -change, cred, CHOWN|FORCE); |
| 628 | } |
| 629 | for (i = 0; i < MAXQUOTAS; i++) { |
| 630 | dqrele(vp, ip->i_dquot[i]); |
| 631 | ip->i_dquot[i] = NODQUOT; |
| 632 | } |
| 633 | } |
| 634 | ip->i_gid = ogid; |
| 635 | ip->i_uid = ouid; |
| 636 | if (getinoquota(ip) == 0) { |
| 637 | if (ouid == uid) { |
| 638 | dqrele(vp, ip->i_dquot[USRQUOTA]); |
| 639 | ip->i_dquot[USRQUOTA] = NODQUOT; |
| 640 | } |
| 641 | if (ogid == gid) { |
| 642 | dqrele(vp, ip->i_dquot[GRPQUOTA]); |
| 643 | ip->i_dquot[GRPQUOTA] = NODQUOT; |
| 644 | } |
| 645 | (void) chkdq(ip, change, cred, FORCE|CHOWN); |
| 646 | (void) chkiq(ip, 1, cred, FORCE|CHOWN); |
| 647 | (void) getinoquota(ip); |
| 648 | } |
| 649 | return (error); |
| 650 | good: |
| 651 | if (getinoquota(ip)) |
| 652 | panic("ufs_chown: lost quota"); |
| 653 | #endif /* QUOTA */ |
| 654 | ip->i_flag |= IN_CHANGE; |
| 655 | if (cred->cr_uid != 0 && (ouid != uid || ogid != gid)) |
| 656 | ip->i_mode &= ~(ISUID | ISGID); |
| 657 | return (0); |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * Mmap a file |
| 662 | * |
| 663 | * NB Currently unsupported. |
| 664 | * |
| 665 | * ufs_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred, |
| 666 | * struct thread *a_td) |
| 667 | */ |
| 668 | /* ARGSUSED */ |
| 669 | static |
| 670 | int |
| 671 | ufs_mmap(struct vop_mmap_args *ap) |
| 672 | { |
| 673 | return (EINVAL); |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * ufs_remove(struct vnode *a_dvp, struct vnode *a_vp, |
| 678 | * struct componentname *a_cnp) |
| 679 | */ |
| 680 | static |
| 681 | int |
| 682 | ufs_remove(struct vop_remove_args *ap) |
| 683 | { |
| 684 | struct inode *ip; |
| 685 | struct vnode *vp = ap->a_vp; |
| 686 | struct vnode *dvp = ap->a_dvp; |
| 687 | int error; |
| 688 | |
| 689 | ip = VTOI(vp); |
| 690 | if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) || |
| 691 | (VTOI(dvp)->i_flags & APPEND)) { |
| 692 | error = EPERM; |
| 693 | goto out; |
| 694 | } |
| 695 | error = ufs_dirremove(dvp, ip, ap->a_cnp->cn_flags, 0); |
| 696 | VN_KNOTE(vp, NOTE_DELETE); |
| 697 | VN_KNOTE(dvp, NOTE_WRITE); |
| 698 | out: |
| 699 | return (error); |
| 700 | } |
| 701 | |
| 702 | /* |
| 703 | * link vnode call |
| 704 | * |
| 705 | * ufs_link(struct vnode *a_tdvp, struct vnode *a_vp, |
| 706 | * struct componentname *a_cnp) |
| 707 | */ |
| 708 | static |
| 709 | int |
| 710 | ufs_link(struct vop_link_args *ap) |
| 711 | { |
| 712 | struct vnode *vp = ap->a_vp; |
| 713 | struct vnode *tdvp = ap->a_tdvp; |
| 714 | struct componentname *cnp = ap->a_cnp; |
| 715 | struct thread *td = cnp->cn_td; |
| 716 | struct inode *ip; |
| 717 | struct direct newdir; |
| 718 | int error; |
| 719 | |
| 720 | #ifdef DIAGNOSTIC |
| 721 | if ((cnp->cn_flags & CNP_HASBUF) == 0) |
| 722 | panic("ufs_link: no name"); |
| 723 | #endif |
| 724 | if (tdvp->v_mount != vp->v_mount) { |
| 725 | error = EXDEV; |
| 726 | goto out2; |
| 727 | } |
| 728 | if (tdvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE, td))) { |
| 729 | goto out2; |
| 730 | } |
| 731 | ip = VTOI(vp); |
| 732 | if ((nlink_t)ip->i_nlink >= LINK_MAX) { |
| 733 | error = EMLINK; |
| 734 | goto out1; |
| 735 | } |
| 736 | if (ip->i_flags & (IMMUTABLE | APPEND)) { |
| 737 | error = EPERM; |
| 738 | goto out1; |
| 739 | } |
| 740 | ip->i_effnlink++; |
| 741 | ip->i_nlink++; |
| 742 | ip->i_flag |= IN_CHANGE; |
| 743 | if (DOINGSOFTDEP(vp)) |
| 744 | softdep_change_linkcnt(ip); |
| 745 | error = UFS_UPDATE(vp, !(DOINGSOFTDEP(vp) | DOINGASYNC(vp))); |
| 746 | if (!error) { |
| 747 | ufs_makedirentry(ip, cnp, &newdir); |
| 748 | error = ufs_direnter(tdvp, vp, &newdir, cnp, NULL); |
| 749 | } |
| 750 | |
| 751 | if (error) { |
| 752 | ip->i_effnlink--; |
| 753 | ip->i_nlink--; |
| 754 | ip->i_flag |= IN_CHANGE; |
| 755 | if (DOINGSOFTDEP(vp)) |
| 756 | softdep_change_linkcnt(ip); |
| 757 | } |
| 758 | out1: |
| 759 | if (tdvp != vp) |
| 760 | VOP_UNLOCK(vp, 0, td); |
| 761 | out2: |
| 762 | VN_KNOTE(vp, NOTE_LINK); |
| 763 | VN_KNOTE(tdvp, NOTE_WRITE); |
| 764 | return (error); |
| 765 | } |
| 766 | |
| 767 | /* |
| 768 | * whiteout vnode call |
| 769 | * |
| 770 | * ufs_whiteout(struct vnode *a_dvp, struct componentname *a_cnp, int a_flags) |
| 771 | */ |
| 772 | static |
| 773 | int |
| 774 | ufs_whiteout(struct vop_whiteout_args *ap) |
| 775 | { |
| 776 | struct vnode *dvp = ap->a_dvp; |
| 777 | struct componentname *cnp = ap->a_cnp; |
| 778 | struct direct newdir; |
| 779 | int error = 0; |
| 780 | |
| 781 | switch (ap->a_flags) { |
| 782 | case NAMEI_LOOKUP: |
| 783 | /* 4.4 format directories support whiteout operations */ |
| 784 | if (dvp->v_mount->mnt_maxsymlinklen > 0) |
| 785 | return (0); |
| 786 | return (EOPNOTSUPP); |
| 787 | |
| 788 | case NAMEI_CREATE: |
| 789 | /* create a new directory whiteout */ |
| 790 | #ifdef DIAGNOSTIC |
| 791 | if ((cnp->cn_flags & CNP_SAVENAME) == 0) |
| 792 | panic("ufs_whiteout: missing name"); |
| 793 | if (dvp->v_mount->mnt_maxsymlinklen <= 0) |
| 794 | panic("ufs_whiteout: old format filesystem"); |
| 795 | #endif |
| 796 | |
| 797 | newdir.d_ino = WINO; |
| 798 | newdir.d_namlen = cnp->cn_namelen; |
| 799 | bcopy(cnp->cn_nameptr, newdir.d_name, (unsigned)cnp->cn_namelen + 1); |
| 800 | newdir.d_type = DT_WHT; |
| 801 | error = ufs_direnter(dvp, NULL, &newdir, cnp, NULL); |
| 802 | break; |
| 803 | |
| 804 | case NAMEI_DELETE: |
| 805 | /* remove an existing directory whiteout */ |
| 806 | #ifdef DIAGNOSTIC |
| 807 | if (dvp->v_mount->mnt_maxsymlinklen <= 0) |
| 808 | panic("ufs_whiteout: old format filesystem"); |
| 809 | #endif |
| 810 | |
| 811 | cnp->cn_flags &= ~CNP_DOWHITEOUT; |
| 812 | error = ufs_dirremove(dvp, NULL, cnp->cn_flags, 0); |
| 813 | break; |
| 814 | default: |
| 815 | panic("ufs_whiteout: unknown op"); |
| 816 | } |
| 817 | return (error); |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | * Rename system call. |
| 822 | * rename("foo", "bar"); |
| 823 | * is essentially |
| 824 | * unlink("bar"); |
| 825 | * link("foo", "bar"); |
| 826 | * unlink("foo"); |
| 827 | * but ``atomically''. Can't do full commit without saving state in the |
| 828 | * inode on disk which isn't feasible at this time. Best we can do is |
| 829 | * always guarantee the target exists. |
| 830 | * |
| 831 | * Basic algorithm is: |
| 832 | * |
| 833 | * 1) Bump link count on source while we're linking it to the |
| 834 | * target. This also ensure the inode won't be deleted out |
| 835 | * from underneath us while we work (it may be truncated by |
| 836 | * a concurrent `trunc' or `open' for creation). |
| 837 | * 2) Link source to destination. If destination already exists, |
| 838 | * delete it first. |
| 839 | * 3) Unlink source reference to inode if still around. If a |
| 840 | * directory was moved and the parent of the destination |
| 841 | * is different from the source, patch the ".." entry in the |
| 842 | * directory. |
| 843 | * |
| 844 | * ufs_rename(struct vnode *a_fdvp, struct vnode *a_fvp, |
| 845 | * struct componentname *a_fcnp, struct vnode *a_tdvp, |
| 846 | * struct vnode *a_tvp, struct componentname *a_tcnp) |
| 847 | */ |
| 848 | static |
| 849 | int |
| 850 | ufs_rename(struct vop_rename_args *ap) |
| 851 | { |
| 852 | struct vnode *tvp = ap->a_tvp; |
| 853 | struct vnode *tdvp = ap->a_tdvp; |
| 854 | struct vnode *fvp = ap->a_fvp; |
| 855 | struct vnode *fdvp = ap->a_fdvp; |
| 856 | struct componentname *tcnp = ap->a_tcnp; |
| 857 | struct componentname *fcnp = ap->a_fcnp; |
| 858 | struct thread *td = fcnp->cn_td; |
| 859 | struct inode *ip, *xp, *dp; |
| 860 | struct direct newdir; |
| 861 | ino_t oldparent = 0, newparent = 0; |
| 862 | int doingdirectory = 0; |
| 863 | int error = 0, ioflag; |
| 864 | |
| 865 | #ifdef DIAGNOSTIC |
| 866 | if ((tcnp->cn_flags & CNP_HASBUF) == 0 || |
| 867 | (fcnp->cn_flags & CNP_HASBUF) == 0) |
| 868 | panic("ufs_rename: no name"); |
| 869 | #endif /* DIAGNOSTIC */ |
| 870 | /* |
| 871 | * Check for cross-device rename. |
| 872 | */ |
| 873 | if ((fvp->v_mount != tdvp->v_mount) || |
| 874 | (tvp && (fvp->v_mount != tvp->v_mount))) { |
| 875 | error = EXDEV; |
| 876 | abortit: |
| 877 | if (tdvp == tvp) |
| 878 | vrele(tdvp); |
| 879 | else |
| 880 | vput(tdvp); |
| 881 | if (tvp) |
| 882 | vput(tvp); |
| 883 | vrele(fdvp); |
| 884 | vrele(fvp); |
| 885 | return (error); |
| 886 | } |
| 887 | |
| 888 | if (tvp && ((VTOI(tvp)->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) || |
| 889 | (VTOI(tdvp)->i_flags & APPEND))) { |
| 890 | error = EPERM; |
| 891 | goto abortit; |
| 892 | } |
| 893 | |
| 894 | /* |
| 895 | * Renaming a file to itself has no effect. The upper layers should |
| 896 | * not call us in that case. Temporarily just warn if they do. |
| 897 | */ |
| 898 | if (fvp == tvp) { |
| 899 | printf("ufs_rename: fvp == tvp (can't happen)\n"); |
| 900 | error = 0; |
| 901 | goto abortit; |
| 902 | } |
| 903 | |
| 904 | if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0) |
| 905 | goto abortit; |
| 906 | dp = VTOI(fdvp); |
| 907 | ip = VTOI(fvp); |
| 908 | if (ip->i_nlink >= LINK_MAX) { |
| 909 | VOP_UNLOCK(fvp, 0, td); |
| 910 | error = EMLINK; |
| 911 | goto abortit; |
| 912 | } |
| 913 | if ((ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND)) |
| 914 | || (dp->i_flags & APPEND)) { |
| 915 | VOP_UNLOCK(fvp, 0, td); |
| 916 | error = EPERM; |
| 917 | goto abortit; |
| 918 | } |
| 919 | if ((ip->i_mode & IFMT) == IFDIR) { |
| 920 | /* |
| 921 | * Avoid ".", "..", and aliases of "." for obvious reasons. |
| 922 | */ |
| 923 | if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.') || |
| 924 | dp == ip || (fcnp->cn_flags | tcnp->cn_flags) & CNP_ISDOTDOT || |
| 925 | (ip->i_flag & IN_RENAME)) { |
| 926 | VOP_UNLOCK(fvp, 0, td); |
| 927 | error = EINVAL; |
| 928 | goto abortit; |
| 929 | } |
| 930 | ip->i_flag |= IN_RENAME; |
| 931 | oldparent = dp->i_number; |
| 932 | doingdirectory = 1; |
| 933 | } |
| 934 | VN_KNOTE(fdvp, NOTE_WRITE); /* XXX right place? */ |
| 935 | vrele(fdvp); |
| 936 | |
| 937 | /* |
| 938 | * When the target exists, both the directory |
| 939 | * and target vnodes are returned locked. |
| 940 | */ |
| 941 | dp = VTOI(tdvp); |
| 942 | xp = NULL; |
| 943 | if (tvp) |
| 944 | xp = VTOI(tvp); |
| 945 | |
| 946 | /* |
| 947 | * 1) Bump link count while we're moving stuff |
| 948 | * around. If we crash somewhere before |
| 949 | * completing our work, the link count |
| 950 | * may be wrong, but correctable. |
| 951 | */ |
| 952 | ip->i_effnlink++; |
| 953 | ip->i_nlink++; |
| 954 | ip->i_flag |= IN_CHANGE; |
| 955 | if (DOINGSOFTDEP(fvp)) |
| 956 | softdep_change_linkcnt(ip); |
| 957 | if ((error = UFS_UPDATE(fvp, !(DOINGSOFTDEP(fvp) | |
| 958 | DOINGASYNC(fvp)))) != 0) { |
| 959 | VOP_UNLOCK(fvp, 0, td); |
| 960 | goto bad; |
| 961 | } |
| 962 | |
| 963 | /* |
| 964 | * If ".." must be changed (ie the directory gets a new |
| 965 | * parent) then the source directory must not be in the |
| 966 | * directory heirarchy above the target, as this would |
| 967 | * orphan everything below the source directory. Also |
| 968 | * the user must have write permission in the source so |
| 969 | * as to be able to change "..". We must repeat the call |
| 970 | * to namei, as the parent directory is unlocked by the |
| 971 | * call to checkpath(). |
| 972 | */ |
| 973 | error = VOP_ACCESS(fvp, VWRITE, tcnp->cn_cred, tcnp->cn_td); |
| 974 | VOP_UNLOCK(fvp, 0, td); |
| 975 | if (oldparent != dp->i_number) |
| 976 | newparent = dp->i_number; |
| 977 | if (doingdirectory && newparent) { |
| 978 | if (error) /* write access check above */ |
| 979 | goto bad; |
| 980 | if (xp != NULL) |
| 981 | vput(tvp); |
| 982 | error = ufs_checkpath(ip, dp, tcnp->cn_cred); |
| 983 | if (error) |
| 984 | goto out; |
| 985 | if ((tcnp->cn_flags & CNP_SAVESTART) == 0) |
| 986 | panic("ufs_rename: lost to startdir"); |
| 987 | vref(tdvp); |
| 988 | error = relookup(tdvp, &tvp, tcnp); |
| 989 | if (error) |
| 990 | goto out; |
| 991 | vrele(tdvp); |
| 992 | dp = VTOI(tdvp); |
| 993 | xp = NULL; |
| 994 | if (tvp) |
| 995 | xp = VTOI(tvp); |
| 996 | } |
| 997 | /* |
| 998 | * 2) If target doesn't exist, link the target |
| 999 | * to the source and unlink the source. |
| 1000 | * Otherwise, rewrite the target directory |
| 1001 | * entry to reference the source inode and |
| 1002 | * expunge the original entry's existence. |
| 1003 | */ |
| 1004 | if (xp == NULL) { |
| 1005 | if (dp->i_dev != ip->i_dev) |
| 1006 | panic("ufs_rename: EXDEV"); |
| 1007 | /* |
| 1008 | * Account for ".." in new directory. |
| 1009 | * When source and destination have the same |
| 1010 | * parent we don't fool with the link count. |
| 1011 | */ |
| 1012 | if (doingdirectory && newparent) { |
| 1013 | if ((nlink_t)dp->i_nlink >= LINK_MAX) { |
| 1014 | error = EMLINK; |
| 1015 | goto bad; |
| 1016 | } |
| 1017 | dp->i_effnlink++; |
| 1018 | dp->i_nlink++; |
| 1019 | dp->i_flag |= IN_CHANGE; |
| 1020 | if (DOINGSOFTDEP(tdvp)) |
| 1021 | softdep_change_linkcnt(dp); |
| 1022 | error = UFS_UPDATE(tdvp, !(DOINGSOFTDEP(tdvp) | |
| 1023 | DOINGASYNC(tdvp))); |
| 1024 | if (error) |
| 1025 | goto bad; |
| 1026 | } |
| 1027 | ufs_makedirentry(ip, tcnp, &newdir); |
| 1028 | error = ufs_direnter(tdvp, NULL, &newdir, tcnp, NULL); |
| 1029 | if (error) { |
| 1030 | if (doingdirectory && newparent) { |
| 1031 | dp->i_effnlink--; |
| 1032 | dp->i_nlink--; |
| 1033 | dp->i_flag |= IN_CHANGE; |
| 1034 | if (DOINGSOFTDEP(tdvp)) |
| 1035 | softdep_change_linkcnt(dp); |
| 1036 | (void)UFS_UPDATE(tdvp, 1); |
| 1037 | } |
| 1038 | goto bad; |
| 1039 | } |
| 1040 | VN_KNOTE(tdvp, NOTE_WRITE); |
| 1041 | vput(tdvp); |
| 1042 | } else { |
| 1043 | if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) |
| 1044 | panic("ufs_rename: EXDEV"); |
| 1045 | /* |
| 1046 | * Short circuit rename(foo, foo). |
| 1047 | */ |
| 1048 | if (xp->i_number == ip->i_number) |
| 1049 | panic("ufs_rename: same file"); |
| 1050 | /* |
| 1051 | * If the parent directory is "sticky", then the user must |
| 1052 | * own the parent directory, or the destination of the rename, |
| 1053 | * otherwise the destination may not be changed (except by |
| 1054 | * root). This implements append-only directories. |
| 1055 | */ |
| 1056 | if ((dp->i_mode & S_ISTXT) && tcnp->cn_cred->cr_uid != 0 && |
| 1057 | tcnp->cn_cred->cr_uid != dp->i_uid && |
| 1058 | xp->i_uid != tcnp->cn_cred->cr_uid) { |
| 1059 | error = EPERM; |
| 1060 | goto bad; |
| 1061 | } |
| 1062 | /* |
| 1063 | * Target must be empty if a directory and have no links |
| 1064 | * to it. Also, ensure source and target are compatible |
| 1065 | * (both directories, or both not directories). |
| 1066 | * |
| 1067 | * Purge the file or directory being replaced from the |
| 1068 | * nameccache. |
| 1069 | */ |
| 1070 | if ((xp->i_mode&IFMT) == IFDIR) { |
| 1071 | if ((xp->i_effnlink > 2) || |
| 1072 | !ufs_dirempty(xp, dp->i_number, tcnp->cn_cred)) { |
| 1073 | error = ENOTEMPTY; |
| 1074 | goto bad; |
| 1075 | } |
| 1076 | if (!doingdirectory) { |
| 1077 | error = ENOTDIR; |
| 1078 | goto bad; |
| 1079 | } |
| 1080 | cache_purge(tvp); |
| 1081 | } else if (doingdirectory == 0) { |
| 1082 | cache_purge(tvp); |
| 1083 | } else { |
| 1084 | error = EISDIR; |
| 1085 | goto bad; |
| 1086 | } |
| 1087 | /* |
| 1088 | * note: inode passed to ufs_dirrewrite() is 0 for a |
| 1089 | * non-directory file rename, 1 for a directory rename |
| 1090 | * in the same directory, and > 1 for an inode representing |
| 1091 | * the new directory. |
| 1092 | */ |
| 1093 | error = ufs_dirrewrite(dp, xp, ip->i_number, |
| 1094 | IFTODT(ip->i_mode), |
| 1095 | (doingdirectory && newparent) ? |
| 1096 | newparent : (ino_t)doingdirectory); |
| 1097 | if (error) |
| 1098 | goto bad; |
| 1099 | if (doingdirectory) { |
| 1100 | if (!newparent) { |
| 1101 | dp->i_effnlink--; |
| 1102 | if (DOINGSOFTDEP(tdvp)) |
| 1103 | softdep_change_linkcnt(dp); |
| 1104 | } |
| 1105 | xp->i_effnlink--; |
| 1106 | if (DOINGSOFTDEP(tvp)) |
| 1107 | softdep_change_linkcnt(xp); |
| 1108 | } |
| 1109 | if (doingdirectory && !DOINGSOFTDEP(tvp)) { |
| 1110 | /* |
| 1111 | * Truncate inode. The only stuff left in the directory |
| 1112 | * is "." and "..". The "." reference is inconsequential |
| 1113 | * since we are quashing it. We have removed the "." |
| 1114 | * reference and the reference in the parent directory, |
| 1115 | * but there may be other hard links. The soft |
| 1116 | * dependency code will arrange to do these operations |
| 1117 | * after the parent directory entry has been deleted on |
| 1118 | * disk, so when running with that code we avoid doing |
| 1119 | * them now. |
| 1120 | */ |
| 1121 | if (!newparent) { |
| 1122 | dp->i_nlink--; |
| 1123 | dp->i_flag |= IN_CHANGE; |
| 1124 | } |
| 1125 | xp->i_nlink--; |
| 1126 | xp->i_flag |= IN_CHANGE; |
| 1127 | ioflag = DOINGASYNC(tvp) ? 0 : IO_SYNC; |
| 1128 | if ((error = UFS_TRUNCATE(tvp, (off_t)0, ioflag, |
| 1129 | tcnp->cn_cred, tcnp->cn_td)) != 0) |
| 1130 | goto bad; |
| 1131 | } |
| 1132 | VN_KNOTE(tdvp, NOTE_WRITE); |
| 1133 | vput(tdvp); |
| 1134 | VN_KNOTE(tvp, NOTE_DELETE); |
| 1135 | vput(tvp); |
| 1136 | xp = NULL; |
| 1137 | } |
| 1138 | |
| 1139 | /* |
| 1140 | * 3) Unlink the source. |
| 1141 | */ |
| 1142 | fcnp->cn_flags &= ~CNP_MODMASK; |
| 1143 | fcnp->cn_flags |= CNP_LOCKPARENT | CNP_LOCKLEAF; |
| 1144 | if ((fcnp->cn_flags & CNP_SAVESTART) == 0) |
| 1145 | panic("ufs_rename: lost from startdir"); |
| 1146 | vref(fdvp); |
| 1147 | error = relookup(fdvp, &fvp, fcnp); |
| 1148 | if (error == 0) |
| 1149 | vrele(fdvp); |
| 1150 | if (fvp != NULL) { |
| 1151 | xp = VTOI(fvp); |
| 1152 | dp = VTOI(fdvp); |
| 1153 | } else { |
| 1154 | /* |
| 1155 | * From name has disappeared. |
| 1156 | */ |
| 1157 | if (doingdirectory) |
| 1158 | panic("ufs_rename: lost dir entry"); |
| 1159 | vrele(ap->a_fvp); |
| 1160 | return (0); |
| 1161 | } |
| 1162 | /* |
| 1163 | * Ensure that the directory entry still exists and has not |
| 1164 | * changed while the new name has been entered. If the source is |
| 1165 | * a file then the entry may have been unlinked or renamed. In |
| 1166 | * either case there is no further work to be done. If the source |
| 1167 | * is a directory then it cannot have been rmdir'ed; the IN_RENAME |
| 1168 | * flag ensures that it cannot be moved by another rename or removed |
| 1169 | * by a rmdir. |
| 1170 | */ |
| 1171 | if (xp != ip) { |
| 1172 | if (doingdirectory) |
| 1173 | panic("ufs_rename: lost dir entry"); |
| 1174 | } else { |
| 1175 | /* |
| 1176 | * If the source is a directory with a |
| 1177 | * new parent, the link count of the old |
| 1178 | * parent directory must be decremented |
| 1179 | * and ".." set to point to the new parent. |
| 1180 | */ |
| 1181 | if (doingdirectory && newparent) { |
| 1182 | xp->i_offset = mastertemplate.dot_reclen; |
| 1183 | ufs_dirrewrite(xp, dp, newparent, DT_DIR, 0); |
| 1184 | /*cache_purge(fdvp);*/ |
| 1185 | } |
| 1186 | error = ufs_dirremove(fdvp, xp, fcnp->cn_flags, 0); |
| 1187 | xp->i_flag &= ~IN_RENAME; |
| 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * The old API can only do a kitchen sink invalidation. It will |
| 1192 | * not be possible to '..' through the disconnected fvp (if a |
| 1193 | * directory) until it is reconnected by a forward lookup. |
| 1194 | */ |
| 1195 | cache_purge(fvp); |
| 1196 | VN_KNOTE(fvp, NOTE_RENAME); |
| 1197 | if (dp) |
| 1198 | vput(fdvp); |
| 1199 | if (xp) |
| 1200 | vput(fvp); |
| 1201 | vrele(ap->a_fvp); |
| 1202 | return (error); |
| 1203 | |
| 1204 | bad: |
| 1205 | if (xp) |
| 1206 | vput(ITOV(xp)); |
| 1207 | vput(ITOV(dp)); |
| 1208 | out: |
| 1209 | if (doingdirectory) |
| 1210 | ip->i_flag &= ~IN_RENAME; |
| 1211 | if (vn_lock(fvp, LK_EXCLUSIVE, td) == 0) { |
| 1212 | ip->i_effnlink--; |
| 1213 | ip->i_nlink--; |
| 1214 | ip->i_flag |= IN_CHANGE; |
| 1215 | ip->i_flag &= ~IN_RENAME; |
| 1216 | if (DOINGSOFTDEP(fvp)) |
| 1217 | softdep_change_linkcnt(ip); |
| 1218 | vput(fvp); |
| 1219 | } else |
| 1220 | vrele(fvp); |
| 1221 | return (error); |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * Mkdir system call |
| 1226 | * |
| 1227 | * ufs_mkdir(struct vnode *a_dvp, struct vnode **a_vpp, |
| 1228 | * struct componentname *a_cnp, struct vattr *a_vap) |
| 1229 | */ |
| 1230 | static |
| 1231 | int |
| 1232 | ufs_mkdir(struct vop_mkdir_args *ap) |
| 1233 | { |
| 1234 | struct vnode *dvp = ap->a_dvp; |
| 1235 | struct vattr *vap = ap->a_vap; |
| 1236 | struct componentname *cnp = ap->a_cnp; |
| 1237 | struct inode *ip, *dp; |
| 1238 | struct vnode *tvp; |
| 1239 | struct buf *bp; |
| 1240 | struct dirtemplate dirtemplate, *dtp; |
| 1241 | struct direct newdir; |
| 1242 | int error, dmode; |
| 1243 | long blkoff; |
| 1244 | |
| 1245 | #ifdef DIAGNOSTIC |
| 1246 | if ((cnp->cn_flags & CNP_HASBUF) == 0) |
| 1247 | panic("ufs_mkdir: no name"); |
| 1248 | #endif |
| 1249 | dp = VTOI(dvp); |
| 1250 | if ((nlink_t)dp->i_nlink >= LINK_MAX) { |
| 1251 | error = EMLINK; |
| 1252 | goto out; |
| 1253 | } |
| 1254 | dmode = vap->va_mode & 0777; |
| 1255 | dmode |= IFDIR; |
| 1256 | /* |
| 1257 | * Must simulate part of ufs_makeinode here to acquire the inode, |
| 1258 | * but not have it entered in the parent directory. The entry is |
| 1259 | * made later after writing "." and ".." entries. |
| 1260 | */ |
| 1261 | error = UFS_VALLOC(dvp, dmode, cnp->cn_cred, &tvp); |
| 1262 | if (error) |
| 1263 | goto out; |
| 1264 | ip = VTOI(tvp); |
| 1265 | ip->i_gid = dp->i_gid; |
| 1266 | #ifdef SUIDDIR |
| 1267 | { |
| 1268 | #ifdef QUOTA |
| 1269 | struct ucred ucred, *ucp; |
| 1270 | ucp = cnp->cn_cred; |
| 1271 | #endif |
| 1272 | /* |
| 1273 | * If we are hacking owners here, (only do this where told to) |
| 1274 | * and we are not giving it TO root, (would subvert quotas) |
| 1275 | * then go ahead and give it to the other user. |
| 1276 | * The new directory also inherits the SUID bit. |
| 1277 | * If user's UID and dir UID are the same, |
| 1278 | * 'give it away' so that the SUID is still forced on. |
| 1279 | */ |
| 1280 | if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) && |
| 1281 | (dp->i_mode & ISUID) && dp->i_uid) { |
| 1282 | dmode |= ISUID; |
| 1283 | ip->i_uid = dp->i_uid; |
| 1284 | #ifdef QUOTA |
| 1285 | if (dp->i_uid != cnp->cn_cred->cr_uid) { |
| 1286 | /* |
| 1287 | * Make sure the correct user gets charged |
| 1288 | * for the space. |
| 1289 | * Make a dummy credential for the victim. |
| 1290 | * XXX This seems to never be accessed out of |
| 1291 | * our context so a stack variable is ok. |
| 1292 | */ |
| 1293 | ucred.cr_ref = 1; |
| 1294 | ucred.cr_uid = ip->i_uid; |
| 1295 | ucred.cr_ngroups = 1; |
| 1296 | ucred.cr_groups[0] = dp->i_gid; |
| 1297 | ucp = &ucred; |
| 1298 | } |
| 1299 | #endif |
| 1300 | } else |
| 1301 | ip->i_uid = cnp->cn_cred->cr_uid; |
| 1302 | #ifdef QUOTA |
| 1303 | if ((error = getinoquota(ip)) || |
| 1304 | (error = chkiq(ip, 1, ucp, 0))) { |
| 1305 | UFS_VFREE(tvp, ip->i_number, dmode); |
| 1306 | vput(tvp); |
| 1307 | return (error); |
| 1308 | } |
| 1309 | #endif |
| 1310 | } |
| 1311 | #else /* !SUIDDIR */ |
| 1312 | ip->i_uid = cnp->cn_cred->cr_uid; |
| 1313 | #ifdef QUOTA |
| 1314 | if ((error = getinoquota(ip)) || |
| 1315 | (error = chkiq(ip, 1, cnp->cn_cred, 0))) { |
| 1316 | UFS_VFREE(tvp, ip->i_number, dmode); |
| 1317 | vput(tvp); |
| 1318 | return (error); |
| 1319 | } |
| 1320 | #endif |
| 1321 | #endif /* !SUIDDIR */ |
| 1322 | ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; |
| 1323 | ip->i_mode = dmode; |
| 1324 | tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */ |
| 1325 | ip->i_effnlink = 2; |
| 1326 | ip->i_nlink = 2; |
| 1327 | if (DOINGSOFTDEP(tvp)) |
| 1328 | softdep_change_linkcnt(ip); |
| 1329 | if (cnp->cn_flags & CNP_ISWHITEOUT) |
| 1330 | ip->i_flags |= UF_OPAQUE; |
| 1331 | |
| 1332 | /* |
| 1333 | * Bump link count in parent directory to reflect work done below. |
| 1334 | * Should be done before reference is created so cleanup is |
| 1335 | * possible if we crash. |
| 1336 | */ |
| 1337 | dp->i_effnlink++; |
| 1338 | dp->i_nlink++; |
| 1339 | dp->i_flag |= IN_CHANGE; |
| 1340 | if (DOINGSOFTDEP(dvp)) |
| 1341 | softdep_change_linkcnt(dp); |
| 1342 | error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(dvp) | DOINGASYNC(dvp))); |
| 1343 | if (error) |
| 1344 | goto bad; |
| 1345 | |
| 1346 | /* |
| 1347 | * Initialize directory with "." and ".." from static template. |
| 1348 | */ |
| 1349 | if (dvp->v_mount->mnt_maxsymlinklen > 0 |
| 1350 | ) |
| 1351 | dtp = &mastertemplate; |
| 1352 | else |
| 1353 | dtp = (struct dirtemplate *)&omastertemplate; |
| 1354 | dirtemplate = *dtp; |
| 1355 | dirtemplate.dot_ino = ip->i_number; |
| 1356 | dirtemplate.dotdot_ino = dp->i_number; |
| 1357 | if ((error = VOP_BALLOC(tvp, (off_t)0, DIRBLKSIZ, cnp->cn_cred, |
| 1358 | B_CLRBUF, &bp)) != 0) |
| 1359 | goto bad; |
| 1360 | ip->i_size = DIRBLKSIZ; |
| 1361 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 1362 | vnode_pager_setsize(tvp, (u_long)ip->i_size); |
| 1363 | bcopy((caddr_t)&dirtemplate, (caddr_t)bp->b_data, sizeof dirtemplate); |
| 1364 | if (DOINGSOFTDEP(tvp)) { |
| 1365 | /* |
| 1366 | * Ensure that the entire newly allocated block is a |
| 1367 | * valid directory so that future growth within the |
| 1368 | * block does not have to ensure that the block is |
| 1369 | * written before the inode. |
| 1370 | */ |
| 1371 | blkoff = DIRBLKSIZ; |
| 1372 | while (blkoff < bp->b_bcount) { |
| 1373 | ((struct direct *) |
| 1374 | (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ; |
| 1375 | blkoff += DIRBLKSIZ; |
| 1376 | } |
| 1377 | } |
| 1378 | if ((error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | |
| 1379 | DOINGASYNC(tvp)))) != 0) { |
| 1380 | (void)VOP_BWRITE(bp->b_vp, bp); |
| 1381 | goto bad; |
| 1382 | } |
| 1383 | /* |
| 1384 | * Directory set up, now install its entry in the parent directory. |
| 1385 | * |
| 1386 | * If we are not doing soft dependencies, then we must write out the |
| 1387 | * buffer containing the new directory body before entering the new |
| 1388 | * name in the parent. If we are doing soft dependencies, then the |
| 1389 | * buffer containing the new directory body will be passed to and |
| 1390 | * released in the soft dependency code after the code has attached |
| 1391 | * an appropriate ordering dependency to the buffer which ensures that |
| 1392 | * the buffer is written before the new name is written in the parent. |
| 1393 | */ |
| 1394 | if (DOINGASYNC(dvp)) |
| 1395 | bdwrite(bp); |
| 1396 | else if (!DOINGSOFTDEP(dvp) && ((error = VOP_BWRITE(bp->b_vp, bp)))) |
| 1397 | goto bad; |
| 1398 | ufs_makedirentry(ip, cnp, &newdir); |
| 1399 | error = ufs_direnter(dvp, tvp, &newdir, cnp, bp); |
| 1400 | |
| 1401 | bad: |
| 1402 | if (error == 0) { |
| 1403 | VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK); |
| 1404 | *ap->a_vpp = tvp; |
| 1405 | } else { |
| 1406 | dp->i_effnlink--; |
| 1407 | dp->i_nlink--; |
| 1408 | dp->i_flag |= IN_CHANGE; |
| 1409 | if (DOINGSOFTDEP(dvp)) |
| 1410 | softdep_change_linkcnt(dp); |
| 1411 | /* |
| 1412 | * No need to do an explicit VOP_TRUNCATE here, vrele will |
| 1413 | * do this for us because we set the link count to 0. |
| 1414 | */ |
| 1415 | ip->i_effnlink = 0; |
| 1416 | ip->i_nlink = 0; |
| 1417 | ip->i_flag |= IN_CHANGE; |
| 1418 | if (DOINGSOFTDEP(tvp)) |
| 1419 | softdep_change_linkcnt(ip); |
| 1420 | vput(tvp); |
| 1421 | } |
| 1422 | out: |
| 1423 | return (error); |
| 1424 | } |
| 1425 | |
| 1426 | /* |
| 1427 | * Rmdir system call. |
| 1428 | * |
| 1429 | * ufs_rmdir(struct vnode *a_dvp, struct vnode *a_vp, |
| 1430 | * struct componentname *a_cnp) |
| 1431 | */ |
| 1432 | static |
| 1433 | int |
| 1434 | ufs_rmdir(struct vop_rmdir_args *ap) |
| 1435 | { |
| 1436 | struct vnode *vp = ap->a_vp; |
| 1437 | struct vnode *dvp = ap->a_dvp; |
| 1438 | struct componentname *cnp = ap->a_cnp; |
| 1439 | struct inode *ip, *dp; |
| 1440 | int error, ioflag; |
| 1441 | |
| 1442 | ip = VTOI(vp); |
| 1443 | dp = VTOI(dvp); |
| 1444 | |
| 1445 | /* |
| 1446 | * Do not remove a directory that is in the process of being renamed. |
| 1447 | * Verify the directory is empty (and valid). Rmdir ".." will not be |
| 1448 | * valid since ".." will contain a reference to the current directory |
| 1449 | * and thus be non-empty. Do not allow the removal of mounted on |
| 1450 | * directories (this can happen when an NFS exported filesystem |
| 1451 | * tries to remove a locally mounted on directory). |
| 1452 | */ |
| 1453 | error = 0; |
| 1454 | if (ip->i_flag & IN_RENAME) { |
| 1455 | error = EINVAL; |
| 1456 | goto out; |
| 1457 | } |
| 1458 | if (ip->i_effnlink != 2 || |
| 1459 | !ufs_dirempty(ip, dp->i_number, cnp->cn_cred)) { |
| 1460 | error = ENOTEMPTY; |
| 1461 | goto out; |
| 1462 | } |
| 1463 | if ((dp->i_flags & APPEND) |
| 1464 | || (ip->i_flags & (NOUNLINK | IMMUTABLE | APPEND))) { |
| 1465 | error = EPERM; |
| 1466 | goto out; |
| 1467 | } |
| 1468 | if (vp->v_mountedhere != 0) { |
| 1469 | error = EINVAL; |
| 1470 | goto out; |
| 1471 | } |
| 1472 | /* |
| 1473 | * Delete reference to directory before purging |
| 1474 | * inode. If we crash in between, the directory |
| 1475 | * will be reattached to lost+found, |
| 1476 | */ |
| 1477 | dp->i_effnlink--; |
| 1478 | ip->i_effnlink--; |
| 1479 | if (DOINGSOFTDEP(vp)) { |
| 1480 | softdep_change_linkcnt(dp); |
| 1481 | softdep_change_linkcnt(ip); |
| 1482 | } |
| 1483 | error = ufs_dirremove(dvp, ip, cnp->cn_flags, 1); |
| 1484 | if (error) { |
| 1485 | dp->i_effnlink++; |
| 1486 | ip->i_effnlink++; |
| 1487 | if (DOINGSOFTDEP(vp)) { |
| 1488 | softdep_change_linkcnt(dp); |
| 1489 | softdep_change_linkcnt(ip); |
| 1490 | } |
| 1491 | goto out; |
| 1492 | } |
| 1493 | VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK); |
| 1494 | /* |
| 1495 | * Truncate inode. The only stuff left in the directory is "." and |
| 1496 | * "..". The "." reference is inconsequential since we are quashing |
| 1497 | * it. The soft dependency code will arrange to do these operations |
| 1498 | * after the parent directory entry has been deleted on disk, so |
| 1499 | * when running with that code we avoid doing them now. |
| 1500 | */ |
| 1501 | if (!DOINGSOFTDEP(vp)) { |
| 1502 | dp->i_nlink--; |
| 1503 | dp->i_flag |= IN_CHANGE; |
| 1504 | ip->i_nlink--; |
| 1505 | ip->i_flag |= IN_CHANGE; |
| 1506 | ioflag = DOINGASYNC(vp) ? 0 : IO_SYNC; |
| 1507 | error = UFS_TRUNCATE(vp, (off_t)0, ioflag, cnp->cn_cred, |
| 1508 | cnp->cn_td); |
| 1509 | } |
| 1510 | cache_purge(vp); |
| 1511 | #ifdef UFS_DIRHASH |
| 1512 | /* Kill any active hash; i_effnlink == 0, so it will not come back. */ |
| 1513 | if (ip->i_dirhash != NULL) |
| 1514 | ufsdirhash_free(ip); |
| 1515 | #endif |
| 1516 | out: |
| 1517 | VN_KNOTE(vp, NOTE_DELETE); |
| 1518 | return (error); |
| 1519 | } |
| 1520 | |
| 1521 | /* |
| 1522 | * symlink -- make a symbolic link |
| 1523 | * |
| 1524 | * ufs_symlink(struct vnode *a_dvp, struct vnode **a_vpp, |
| 1525 | * struct componentname *a_cnp, struct vattr *a_vap, |
| 1526 | * char *a_target) |
| 1527 | */ |
| 1528 | static |
| 1529 | int |
| 1530 | ufs_symlink(struct vop_symlink_args *ap) |
| 1531 | { |
| 1532 | struct vnode *vp, **vpp = ap->a_vpp; |
| 1533 | struct inode *ip; |
| 1534 | int len, error; |
| 1535 | |
| 1536 | error = ufs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp, |
| 1537 | vpp, ap->a_cnp); |
| 1538 | if (error) |
| 1539 | return (error); |
| 1540 | VN_KNOTE(ap->a_dvp, NOTE_WRITE); |
| 1541 | vp = *vpp; |
| 1542 | len = strlen(ap->a_target); |
| 1543 | if (len < vp->v_mount->mnt_maxsymlinklen) { |
| 1544 | ip = VTOI(vp); |
| 1545 | bcopy(ap->a_target, (char *)ip->i_shortlink, len); |
| 1546 | ip->i_size = len; |
| 1547 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 1548 | } else |
| 1549 | error = vn_rdwr(UIO_WRITE, vp, ap->a_target, len, (off_t)0, |
| 1550 | UIO_SYSSPACE, IO_NODELOCKED, ap->a_cnp->cn_cred, |
| 1551 | (int *)0, NULL); |
| 1552 | if (error) |
| 1553 | vput(vp); |
| 1554 | return (error); |
| 1555 | } |
| 1556 | |
| 1557 | /* |
| 1558 | * Vnode op for reading directories. |
| 1559 | * |
| 1560 | * The routine below assumes that the on-disk format of a directory |
| 1561 | * is the same as that defined by <sys/dirent.h>. If the on-disk |
| 1562 | * format changes, then it will be necessary to do a conversion |
| 1563 | * from the on-disk format that read returns to the format defined |
| 1564 | * by <sys/dirent.h>. |
| 1565 | * |
| 1566 | * ufs_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred, |
| 1567 | * int *a_eofflag, int *ncookies, u_long **a_cookies) |
| 1568 | */ |
| 1569 | static |
| 1570 | int |
| 1571 | ufs_readdir(struct vop_readdir_args *ap) |
| 1572 | { |
| 1573 | struct uio *uio = ap->a_uio; |
| 1574 | int error; |
| 1575 | size_t count, lost; |
| 1576 | off_t off; |
| 1577 | |
| 1578 | if (ap->a_ncookies != NULL) |
| 1579 | /* |
| 1580 | * Ensure that the block is aligned. The caller can use |
| 1581 | * the cookies to determine where in the block to start. |
| 1582 | */ |
| 1583 | uio->uio_offset &= ~(DIRBLKSIZ - 1); |
| 1584 | off = uio->uio_offset; |
| 1585 | count = uio->uio_resid; |
| 1586 | /* Make sure we don't return partial entries. */ |
| 1587 | if (count <= ((uio->uio_offset + count) & (DIRBLKSIZ -1))) |
| 1588 | return (EINVAL); |
| 1589 | count -= (uio->uio_offset + count) & (DIRBLKSIZ -1); |
| 1590 | lost = uio->uio_resid - count; |
| 1591 | uio->uio_resid = count; |
| 1592 | uio->uio_iov->iov_len = count; |
| 1593 | # if (BYTE_ORDER == LITTLE_ENDIAN) |
| 1594 | if (ap->a_vp->v_mount->mnt_maxsymlinklen > 0) { |
| 1595 | error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred); |
| 1596 | } else { |
| 1597 | struct dirent *dp, *edp; |
| 1598 | struct uio auio; |
| 1599 | struct iovec aiov; |
| 1600 | caddr_t dirbuf; |
| 1601 | int readcnt; |
| 1602 | u_char tmp; |
| 1603 | |
| 1604 | auio = *uio; |
| 1605 | auio.uio_iov = &aiov; |
| 1606 | auio.uio_iovcnt = 1; |
| 1607 | auio.uio_segflg = UIO_SYSSPACE; |
| 1608 | aiov.iov_len = count; |
| 1609 | MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK); |
| 1610 | aiov.iov_base = dirbuf; |
| 1611 | error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred); |
| 1612 | if (error == 0) { |
| 1613 | readcnt = count - auio.uio_resid; |
| 1614 | edp = (struct dirent *)&dirbuf[readcnt]; |
| 1615 | for (dp = (struct dirent *)dirbuf; dp < edp; ) { |
| 1616 | tmp = dp->d_namlen; |
| 1617 | dp->d_namlen = dp->d_type; |
| 1618 | dp->d_type = tmp; |
| 1619 | if (dp->d_reclen > 0) { |
| 1620 | dp = (struct dirent *) |
| 1621 | ((char *)dp + dp->d_reclen); |
| 1622 | } else { |
| 1623 | error = EIO; |
| 1624 | break; |
| 1625 | } |
| 1626 | } |
| 1627 | if (dp >= edp) |
| 1628 | error = uiomove(dirbuf, readcnt, uio); |
| 1629 | } |
| 1630 | FREE(dirbuf, M_TEMP); |
| 1631 | } |
| 1632 | # else |
| 1633 | error = VOP_READ(ap->a_vp, uio, 0, ap->a_cred); |
| 1634 | # endif |
| 1635 | if (!error && ap->a_ncookies != NULL) { |
| 1636 | struct dirent* dpStart; |
| 1637 | struct dirent* dpEnd; |
| 1638 | struct dirent* dp; |
| 1639 | int ncookies; |
| 1640 | u_long *cookies; |
| 1641 | u_long *cookiep; |
| 1642 | |
| 1643 | if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) |
| 1644 | panic("ufs_readdir: unexpected uio from NFS server"); |
| 1645 | dpStart = (struct dirent *) |
| 1646 | (uio->uio_iov->iov_base - (uio->uio_offset - off)); |
| 1647 | dpEnd = (struct dirent *) uio->uio_iov->iov_base; |
| 1648 | for (dp = dpStart, ncookies = 0; |
| 1649 | dp < dpEnd; |
| 1650 | dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) |
| 1651 | ncookies++; |
| 1652 | MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP, |
| 1653 | M_WAITOK); |
| 1654 | for (dp = dpStart, cookiep = cookies; |
| 1655 | dp < dpEnd; |
| 1656 | dp = (struct dirent *)((caddr_t) dp + dp->d_reclen)) { |
| 1657 | off += dp->d_reclen; |
| 1658 | *cookiep++ = (u_long) off; |
| 1659 | } |
| 1660 | *ap->a_ncookies = ncookies; |
| 1661 | *ap->a_cookies = cookies; |
| 1662 | } |
| 1663 | uio->uio_resid += lost; |
| 1664 | if (ap->a_eofflag) |
| 1665 | *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset; |
| 1666 | return (error); |
| 1667 | } |
| 1668 | |
| 1669 | /* |
| 1670 | * Return target name of a symbolic link |
| 1671 | * |
| 1672 | * ufs_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred) |
| 1673 | */ |
| 1674 | static |
| 1675 | int |
| 1676 | ufs_readlink(struct vop_readlink_args *ap) |
| 1677 | { |
| 1678 | struct vnode *vp = ap->a_vp; |
| 1679 | struct inode *ip = VTOI(vp); |
| 1680 | int isize; |
| 1681 | |
| 1682 | isize = ip->i_size; |
| 1683 | if ((isize < vp->v_mount->mnt_maxsymlinklen) || |
| 1684 | (ip->i_din.di_blocks == 0)) { /* XXX - for old fastlink support */ |
| 1685 | uiomove((char *)ip->i_shortlink, isize, ap->a_uio); |
| 1686 | return (0); |
| 1687 | } |
| 1688 | return (VOP_READ(vp, ap->a_uio, 0, ap->a_cred)); |
| 1689 | } |
| 1690 | |
| 1691 | /* |
| 1692 | * Calculate the logical to physical mapping if not done already, |
| 1693 | * then call the device strategy routine. |
| 1694 | * |
| 1695 | * In order to be able to swap to a file, the VOP_BMAP operation may not |
| 1696 | * deadlock on memory. See ufs_bmap() for details. |
| 1697 | * |
| 1698 | * ufs_strategy(struct vnode *a_vp, struct buf *a_bp) |
| 1699 | */ |
| 1700 | static |
| 1701 | int |
| 1702 | ufs_strategy(struct vop_strategy_args *ap) |
| 1703 | { |
| 1704 | struct buf *bp = ap->a_bp; |
| 1705 | struct vnode *vp = ap->a_vp; |
| 1706 | struct inode *ip; |
| 1707 | int error; |
| 1708 | |
| 1709 | ip = VTOI(vp); |
| 1710 | if (vp->v_type == VBLK || vp->v_type == VCHR) |
| 1711 | panic("ufs_strategy: spec"); |
| 1712 | if (bp->b_blkno == bp->b_lblkno) { |
| 1713 | error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL); |
| 1714 | if (error) { |
| 1715 | bp->b_error = error; |
| 1716 | bp->b_flags |= B_ERROR; |
| 1717 | biodone(bp); |
| 1718 | return (error); |
| 1719 | } |
| 1720 | if (bp->b_blkno == (daddr_t)-1) |
| 1721 | vfs_bio_clrbuf(bp); |
| 1722 | } |
| 1723 | if (bp->b_blkno == (daddr_t)-1) { |
| 1724 | biodone(bp); |
| 1725 | return (0); |
| 1726 | } |
| 1727 | vp = ip->i_devvp; |
| 1728 | bp->b_dev = vp->v_rdev; |
| 1729 | VOP_STRATEGY(vp, bp); |
| 1730 | return (0); |
| 1731 | } |
| 1732 | |
| 1733 | /* |
| 1734 | * Print out the contents of an inode. |
| 1735 | * |
| 1736 | * ufs_print(struct vnode *a_vp) |
| 1737 | */ |
| 1738 | static |
| 1739 | int |
| 1740 | ufs_print(struct vop_print_args *ap) |
| 1741 | { |
| 1742 | struct vnode *vp = ap->a_vp; |
| 1743 | struct inode *ip = VTOI(vp); |
| 1744 | |
| 1745 | printf("tag VT_UFS, ino %lu, on dev %s (%d, %d)", |
| 1746 | (u_long)ip->i_number, devtoname(ip->i_dev), major(ip->i_dev), |
| 1747 | minor(ip->i_dev)); |
| 1748 | if (vp->v_type == VFIFO) |
| 1749 | fifo_printinfo(vp); |
| 1750 | lockmgr_printinfo(&vp->v_lock); |
| 1751 | printf("\n"); |
| 1752 | return (0); |
| 1753 | } |
| 1754 | |
| 1755 | /* |
| 1756 | * Read wrapper for special devices. |
| 1757 | * |
| 1758 | * ufsspec_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, |
| 1759 | * struct ucred *a_cred) |
| 1760 | */ |
| 1761 | static |
| 1762 | int |
| 1763 | ufsspec_read(struct vop_read_args *ap) |
| 1764 | { |
| 1765 | int error, resid; |
| 1766 | struct inode *ip; |
| 1767 | struct uio *uio; |
| 1768 | |
| 1769 | uio = ap->a_uio; |
| 1770 | resid = uio->uio_resid; |
| 1771 | error = VOCALL(spec_vnode_vops, &ap->a_head); |
| 1772 | /* |
| 1773 | * The inode may have been revoked during the call, so it must not |
| 1774 | * be accessed blindly here or in the other wrapper functions. |
| 1775 | */ |
| 1776 | ip = VTOI(ap->a_vp); |
| 1777 | if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0))) |
| 1778 | ip->i_flag |= IN_ACCESS; |
| 1779 | return (error); |
| 1780 | } |
| 1781 | |
| 1782 | /* |
| 1783 | * Write wrapper for special devices. |
| 1784 | * |
| 1785 | * ufsspec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, |
| 1786 | * struct ucred *a_cred) |
| 1787 | */ |
| 1788 | static |
| 1789 | int |
| 1790 | ufsspec_write(struct vop_write_args *ap) |
| 1791 | { |
| 1792 | int error, resid; |
| 1793 | struct inode *ip; |
| 1794 | struct uio *uio; |
| 1795 | |
| 1796 | uio = ap->a_uio; |
| 1797 | resid = uio->uio_resid; |
| 1798 | error = VOCALL(spec_vnode_vops, &ap->a_head); |
| 1799 | ip = VTOI(ap->a_vp); |
| 1800 | if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0))) |
| 1801 | VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE; |
| 1802 | return (error); |
| 1803 | } |
| 1804 | |
| 1805 | /* |
| 1806 | * Close wrapper for special devices. |
| 1807 | * |
| 1808 | * Update the times on the inode then do device close. |
| 1809 | * |
| 1810 | * ufsspec_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred, |
| 1811 | * struct thread *a_td) |
| 1812 | */ |
| 1813 | static |
| 1814 | int |
| 1815 | ufsspec_close(struct vop_close_args *ap) |
| 1816 | { |
| 1817 | struct vnode *vp = ap->a_vp; |
| 1818 | |
| 1819 | if (vp->v_usecount > 1) |
| 1820 | ufs_itimes(vp); |
| 1821 | return (VOCALL(spec_vnode_vops, &ap->a_head)); |
| 1822 | } |
| 1823 | |
| 1824 | /* |
| 1825 | * Read wrapper for fifos. |
| 1826 | * |
| 1827 | * ufsfifo_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, |
| 1828 | * struct ucred *a_cred) |
| 1829 | */ |
| 1830 | static |
| 1831 | int |
| 1832 | ufsfifo_read(struct vop_read_args *ap) |
| 1833 | { |
| 1834 | int error, resid; |
| 1835 | struct inode *ip; |
| 1836 | struct uio *uio; |
| 1837 | |
| 1838 | uio = ap->a_uio; |
| 1839 | resid = uio->uio_resid; |
| 1840 | error = VOCALL(fifo_vnode_vops, &ap->a_head); |
| 1841 | ip = VTOI(ap->a_vp); |
| 1842 | if ((ap->a_vp->v_mount->mnt_flag & MNT_NOATIME) == 0 && ip != NULL && |
| 1843 | (uio->uio_resid != resid || (error == 0 && resid != 0))) |
| 1844 | VTOI(ap->a_vp)->i_flag |= IN_ACCESS; |
| 1845 | return (error); |
| 1846 | } |
| 1847 | |
| 1848 | /* |
| 1849 | * Write wrapper for fifos. |
| 1850 | * |
| 1851 | * ufsfifo_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag, |
| 1852 | * struct ucred *a_cred) |
| 1853 | */ |
| 1854 | static |
| 1855 | int |
| 1856 | ufsfifo_write(struct vop_write_args *ap) |
| 1857 | { |
| 1858 | int error, resid; |
| 1859 | struct inode *ip; |
| 1860 | struct uio *uio; |
| 1861 | |
| 1862 | uio = ap->a_uio; |
| 1863 | resid = uio->uio_resid; |
| 1864 | error = VOCALL(fifo_vnode_vops, &ap->a_head); |
| 1865 | ip = VTOI(ap->a_vp); |
| 1866 | if (ip != NULL && (uio->uio_resid != resid || (error == 0 && resid != 0))) |
| 1867 | VTOI(ap->a_vp)->i_flag |= IN_CHANGE | IN_UPDATE; |
| 1868 | return (error); |
| 1869 | } |
| 1870 | |
| 1871 | /* |
| 1872 | * Close wrapper for fifos. |
| 1873 | * |
| 1874 | * Update the times on the inode then do device close. |
| 1875 | * |
| 1876 | * ufsfifo_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred, |
| 1877 | * struct thread *a_td) |
| 1878 | */ |
| 1879 | static |
| 1880 | int |
| 1881 | ufsfifo_close(struct vop_close_args *ap) |
| 1882 | { |
| 1883 | struct vnode *vp = ap->a_vp; |
| 1884 | |
| 1885 | if (vp->v_usecount > 1) |
| 1886 | ufs_itimes(vp); |
| 1887 | return (VOCALL(fifo_vnode_vops, &ap->a_head)); |
| 1888 | } |
| 1889 | |
| 1890 | /* |
| 1891 | * Kqfilter wrapper for fifos. |
| 1892 | * |
| 1893 | * Fall through to ufs kqfilter routines if needed |
| 1894 | */ |
| 1895 | static |
| 1896 | int |
| 1897 | ufsfifo_kqfilter(struct vop_kqfilter_args *ap) |
| 1898 | { |
| 1899 | int error; |
| 1900 | |
| 1901 | error = VOCALL(fifo_vnode_vops, &ap->a_head); |
| 1902 | if (error) |
| 1903 | error = ufs_kqfilter(ap); |
| 1904 | return (error); |
| 1905 | } |
| 1906 | |
| 1907 | /* |
| 1908 | * Return POSIX pathconf information applicable to ufs filesystems. |
| 1909 | * |
| 1910 | * ufs_pathconf(struct vnode *a_vp, int a_name, int *a_retval) |
| 1911 | */ |
| 1912 | static |
| 1913 | int |
| 1914 | ufs_pathconf(struct vop_pathconf_args *ap) |
| 1915 | { |
| 1916 | switch (ap->a_name) { |
| 1917 | case _PC_LINK_MAX: |
| 1918 | *ap->a_retval = LINK_MAX; |
| 1919 | return (0); |
| 1920 | case _PC_NAME_MAX: |
| 1921 | *ap->a_retval = NAME_MAX; |
| 1922 | return (0); |
| 1923 | case _PC_PATH_MAX: |
| 1924 | *ap->a_retval = PATH_MAX; |
| 1925 | return (0); |
| 1926 | case _PC_PIPE_BUF: |
| 1927 | *ap->a_retval = PIPE_BUF; |
| 1928 | return (0); |
| 1929 | case _PC_CHOWN_RESTRICTED: |
| 1930 | *ap->a_retval = 1; |
| 1931 | return (0); |
| 1932 | case _PC_NO_TRUNC: |
| 1933 | *ap->a_retval = 1; |
| 1934 | return (0); |
| 1935 | default: |
| 1936 | return (EINVAL); |
| 1937 | } |
| 1938 | /* NOTREACHED */ |
| 1939 | } |
| 1940 | |
| 1941 | /* |
| 1942 | * Advisory record locking support |
| 1943 | * |
| 1944 | * ufs_advlock(struct vnode *a_vp, caddr_t a_id, int a_op, struct flock *a_fl, |
| 1945 | * int a_flags) |
| 1946 | */ |
| 1947 | static |
| 1948 | int |
| 1949 | ufs_advlock(struct vop_advlock_args *ap) |
| 1950 | { |
| 1951 | struct inode *ip = VTOI(ap->a_vp); |
| 1952 | |
| 1953 | return (lf_advlock(ap, &(ip->i_lockf), ip->i_size)); |
| 1954 | } |
| 1955 | |
| 1956 | /* |
| 1957 | * Initialize the vnode associated with a new inode, handle aliased |
| 1958 | * vnodes. |
| 1959 | */ |
| 1960 | int |
| 1961 | ufs_vinit(struct mount *mntp, struct vnode **vpp) |
| 1962 | { |
| 1963 | struct inode *ip; |
| 1964 | struct vnode *vp; |
| 1965 | struct timeval tv; |
| 1966 | |
| 1967 | vp = *vpp; |
| 1968 | ip = VTOI(vp); |
| 1969 | switch(vp->v_type = IFTOVT(ip->i_mode)) { |
| 1970 | case VCHR: |
| 1971 | case VBLK: |
| 1972 | vp->v_ops = mntp->mnt_vn_spec_ops; |
| 1973 | addaliasu(vp, ip->i_rdev); |
| 1974 | break; |
| 1975 | case VFIFO: |
| 1976 | vp->v_ops = mntp->mnt_vn_fifo_ops; |
| 1977 | break; |
| 1978 | default: |
| 1979 | break; |
| 1980 | |
| 1981 | } |
| 1982 | if (ip->i_number == ROOTINO) |
| 1983 | vp->v_flag |= VROOT; |
| 1984 | /* |
| 1985 | * Initialize modrev times |
| 1986 | */ |
| 1987 | getmicrouptime(&tv); |
| 1988 | SETHIGH(ip->i_modrev, tv.tv_sec); |
| 1989 | SETLOW(ip->i_modrev, tv.tv_usec * 4294); |
| 1990 | *vpp = vp; |
| 1991 | return (0); |
| 1992 | } |
| 1993 | |
| 1994 | /* |
| 1995 | * Allocate a new inode. |
| 1996 | */ |
| 1997 | static |
| 1998 | int |
| 1999 | ufs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp, |
| 2000 | struct componentname *cnp) |
| 2001 | { |
| 2002 | struct inode *ip, *pdir; |
| 2003 | struct direct newdir; |
| 2004 | struct vnode *tvp; |
| 2005 | int error; |
| 2006 | |
| 2007 | pdir = VTOI(dvp); |
| 2008 | #ifdef DIAGNOSTIC |
| 2009 | if ((cnp->cn_flags & CNP_HASBUF) == 0) |
| 2010 | panic("ufs_makeinode: no name"); |
| 2011 | #endif |
| 2012 | *vpp = NULL; |
| 2013 | if ((mode & IFMT) == 0) |
| 2014 | mode |= IFREG; |
| 2015 | |
| 2016 | error = UFS_VALLOC(dvp, mode, cnp->cn_cred, &tvp); |
| 2017 | if (error) |
| 2018 | return (error); |
| 2019 | ip = VTOI(tvp); |
| 2020 | ip->i_gid = pdir->i_gid; |
| 2021 | #ifdef SUIDDIR |
| 2022 | { |
| 2023 | #ifdef QUOTA |
| 2024 | struct ucred ucred, *ucp; |
| 2025 | ucp = cnp->cn_cred; |
| 2026 | #endif |
| 2027 | /* |
| 2028 | * If we are not the owner of the directory, |
| 2029 | * and we are hacking owners here, (only do this where told to) |
| 2030 | * and we are not giving it TO root, (would subvert quotas) |
| 2031 | * then go ahead and give it to the other user. |
| 2032 | * Note that this drops off the execute bits for security. |
| 2033 | */ |
| 2034 | if ((dvp->v_mount->mnt_flag & MNT_SUIDDIR) && |
| 2035 | (pdir->i_mode & ISUID) && |
| 2036 | (pdir->i_uid != cnp->cn_cred->cr_uid) && pdir->i_uid) { |
| 2037 | ip->i_uid = pdir->i_uid; |
| 2038 | mode &= ~07111; |
| 2039 | #ifdef QUOTA |
| 2040 | /* |
| 2041 | * Make sure the correct user gets charged |
| 2042 | * for the space. |
| 2043 | * Quickly knock up a dummy credential for the victim. |
| 2044 | * XXX This seems to never be accessed out of our |
| 2045 | * context so a stack variable is ok. |
| 2046 | */ |
| 2047 | ucred.cr_ref = 1; |
| 2048 | ucred.cr_uid = ip->i_uid; |
| 2049 | ucred.cr_ngroups = 1; |
| 2050 | ucred.cr_groups[0] = pdir->i_gid; |
| 2051 | ucp = &ucred; |
| 2052 | #endif |
| 2053 | } else |
| 2054 | ip->i_uid = cnp->cn_cred->cr_uid; |
| 2055 | |
| 2056 | #ifdef QUOTA |
| 2057 | if ((error = getinoquota(ip)) || |
| 2058 | (error = chkiq(ip, 1, ucp, 0))) { |
| 2059 | UFS_VFREE(tvp, ip->i_number, mode); |
| 2060 | vput(tvp); |
| 2061 | return (error); |
| 2062 | } |
| 2063 | #endif |
| 2064 | } |
| 2065 | #else /* !SUIDDIR */ |
| 2066 | ip->i_uid = cnp->cn_cred->cr_uid; |
| 2067 | #ifdef QUOTA |
| 2068 | if ((error = getinoquota(ip)) || |
| 2069 | (error = chkiq(ip, 1, cnp->cn_cred, 0))) { |
| 2070 | UFS_VFREE(tvp, ip->i_number, mode); |
| 2071 | vput(tvp); |
| 2072 | return (error); |
| 2073 | } |
| 2074 | #endif |
| 2075 | #endif /* !SUIDDIR */ |
| 2076 | ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; |
| 2077 | ip->i_mode = mode; |
| 2078 | tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */ |
| 2079 | ip->i_effnlink = 1; |
| 2080 | ip->i_nlink = 1; |
| 2081 | if (DOINGSOFTDEP(tvp)) |
| 2082 | softdep_change_linkcnt(ip); |
| 2083 | if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, cnp->cn_cred) && |
| 2084 | suser_cred(cnp->cn_cred, 0)) { |
| 2085 | ip->i_mode &= ~ISGID; |
| 2086 | } |
| 2087 | |
| 2088 | if (cnp->cn_flags & CNP_ISWHITEOUT) |
| 2089 | ip->i_flags |= UF_OPAQUE; |
| 2090 | |
| 2091 | /* |
| 2092 | * Make sure inode goes to disk before directory entry. |
| 2093 | */ |
| 2094 | error = UFS_UPDATE(tvp, !(DOINGSOFTDEP(tvp) | DOINGASYNC(tvp))); |
| 2095 | if (error) |
| 2096 | goto bad; |
| 2097 | ufs_makedirentry(ip, cnp, &newdir); |
| 2098 | error = ufs_direnter(dvp, tvp, &newdir, cnp, NULL); |
| 2099 | if (error) |
| 2100 | goto bad; |
| 2101 | *vpp = tvp; |
| 2102 | return (0); |
| 2103 | |
| 2104 | bad: |
| 2105 | /* |
| 2106 | * Write error occurred trying to update the inode |
| 2107 | * or the directory so must deallocate the inode. |
| 2108 | */ |
| 2109 | ip->i_effnlink = 0; |
| 2110 | ip->i_nlink = 0; |
| 2111 | ip->i_flag |= IN_CHANGE; |
| 2112 | if (DOINGSOFTDEP(tvp)) |
| 2113 | softdep_change_linkcnt(ip); |
| 2114 | vput(tvp); |
| 2115 | return (error); |
| 2116 | } |
| 2117 | |
| 2118 | static int |
| 2119 | ufs_missingop(struct vop_generic_args *ap) |
| 2120 | { |
| 2121 | panic("no vop function for %s in ufs child", ap->a_desc->vdesc_name); |
| 2122 | return (EOPNOTSUPP); |
| 2123 | } |
| 2124 | |
| 2125 | static struct filterops ufsread_filtops = |
| 2126 | { 1, NULL, filt_ufsdetach, filt_ufsread }; |
| 2127 | static struct filterops ufswrite_filtops = |
| 2128 | { 1, NULL, filt_ufsdetach, filt_ufswrite }; |
| 2129 | static struct filterops ufsvnode_filtops = |
| 2130 | { 1, NULL, filt_ufsdetach, filt_ufsvnode }; |
| 2131 | |
| 2132 | /* |
| 2133 | * ufs_kqfilter(struct vnode *a_vp, struct knote *a_kn) |
| 2134 | */ |
| 2135 | static int |
| 2136 | ufs_kqfilter(struct vop_kqfilter_args *ap) |
| 2137 | { |
| 2138 | struct vnode *vp = ap->a_vp; |
| 2139 | struct knote *kn = ap->a_kn; |
| 2140 | lwkt_tokref ilock; |
| 2141 | |
| 2142 | switch (kn->kn_filter) { |
| 2143 | case EVFILT_READ: |
| 2144 | kn->kn_fop = &ufsread_filtops; |
| 2145 | break; |
| 2146 | case EVFILT_WRITE: |
| 2147 | kn->kn_fop = &ufswrite_filtops; |
| 2148 | break; |
| 2149 | case EVFILT_VNODE: |
| 2150 | kn->kn_fop = &ufsvnode_filtops; |
| 2151 | break; |
| 2152 | default: |
| 2153 | return (1); |
| 2154 | } |
| 2155 | |
| 2156 | kn->kn_hook = (caddr_t)vp; |
| 2157 | |
| 2158 | lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token); |
| 2159 | SLIST_INSERT_HEAD(&vp->v_pollinfo.vpi_selinfo.si_note, kn, kn_selnext); |
| 2160 | lwkt_reltoken(&ilock); |
| 2161 | |
| 2162 | return (0); |
| 2163 | } |
| 2164 | |
| 2165 | static void |
| 2166 | filt_ufsdetach(struct knote *kn) |
| 2167 | { |
| 2168 | struct vnode *vp = (struct vnode *)kn->kn_hook; |
| 2169 | lwkt_tokref ilock; |
| 2170 | |
| 2171 | lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token); |
| 2172 | SLIST_REMOVE(&vp->v_pollinfo.vpi_selinfo.si_note, |
| 2173 | kn, knote, kn_selnext); |
| 2174 | lwkt_reltoken(&ilock); |
| 2175 | } |
| 2176 | |
| 2177 | /*ARGSUSED*/ |
| 2178 | static int |
| 2179 | filt_ufsread(struct knote *kn, long hint) |
| 2180 | { |
| 2181 | struct vnode *vp = (struct vnode *)kn->kn_hook; |
| 2182 | struct inode *ip = VTOI(vp); |
| 2183 | |
| 2184 | /* |
| 2185 | * filesystem is gone, so set the EOF flag and schedule |
| 2186 | * the knote for deletion. |
| 2187 | */ |
| 2188 | if (hint == NOTE_REVOKE) { |
| 2189 | kn->kn_flags |= (EV_EOF | EV_ONESHOT); |
| 2190 | return (1); |
| 2191 | } |
| 2192 | |
| 2193 | kn->kn_data = ip->i_size - kn->kn_fp->f_offset; |
| 2194 | return (kn->kn_data != 0); |
| 2195 | } |
| 2196 | |
| 2197 | /*ARGSUSED*/ |
| 2198 | static int |
| 2199 | filt_ufswrite(struct knote *kn, long hint) |
| 2200 | { |
| 2201 | /* |
| 2202 | * filesystem is gone, so set the EOF flag and schedule |
| 2203 | * the knote for deletion. |
| 2204 | */ |
| 2205 | if (hint == NOTE_REVOKE) |
| 2206 | kn->kn_flags |= (EV_EOF | EV_ONESHOT); |
| 2207 | |
| 2208 | kn->kn_data = 0; |
| 2209 | return (1); |
| 2210 | } |
| 2211 | |
| 2212 | static int |
| 2213 | filt_ufsvnode(struct knote *kn, long hint) |
| 2214 | { |
| 2215 | if (kn->kn_sfflags & hint) |
| 2216 | kn->kn_fflags |= hint; |
| 2217 | if (hint == NOTE_REVOKE) { |
| 2218 | kn->kn_flags |= EV_EOF; |
| 2219 | return (1); |
| 2220 | } |
| 2221 | return (kn->kn_fflags != 0); |
| 2222 | } |
| 2223 | |
| 2224 | /* Global vfs data structures for ufs. */ |
| 2225 | static struct vop_ops *ufs_vnode_vops; |
| 2226 | static struct vnodeopv_entry_desc ufs_vnodeop_entries[] = { |
| 2227 | { &vop_default_desc, vop_defaultop }, |
| 2228 | { &vop_fsync_desc, (void *) ufs_missingop }, |
| 2229 | { &vop_read_desc, (void *) ufs_missingop }, |
| 2230 | { &vop_reallocblks_desc, (void *) ufs_missingop }, |
| 2231 | { &vop_write_desc, (void *) ufs_missingop }, |
| 2232 | { &vop_access_desc, (void *) ufs_access }, |
| 2233 | { &vop_advlock_desc, (void *) ufs_advlock }, |
| 2234 | { &vop_bmap_desc, (void *) ufs_bmap }, |
| 2235 | { &vop_cachedlookup_desc, (void *) ufs_lookup }, |
| 2236 | { &vop_close_desc, (void *) ufs_close }, |
| 2237 | { &vop_create_desc, (void *) ufs_create }, |
| 2238 | { &vop_getattr_desc, (void *) ufs_getattr }, |
| 2239 | { &vop_inactive_desc, (void *) ufs_inactive }, |
| 2240 | { &vop_islocked_desc, (void *) vop_stdislocked }, |
| 2241 | { &vop_link_desc, (void *) ufs_link }, |
| 2242 | { &vop_lock_desc, (void *) vop_stdlock }, |
| 2243 | { &vop_lookup_desc, (void *) vfs_cache_lookup }, |
| 2244 | { &vop_mkdir_desc, (void *) ufs_mkdir }, |
| 2245 | { &vop_mknod_desc, (void *) ufs_mknod }, |
| 2246 | { &vop_mmap_desc, (void *) ufs_mmap }, |
| 2247 | { &vop_open_desc, (void *) ufs_open }, |
| 2248 | { &vop_pathconf_desc, (void *) ufs_pathconf }, |
| 2249 | { &vop_poll_desc, (void *) vop_stdpoll }, |
| 2250 | { &vop_kqfilter_desc, (void *) ufs_kqfilter }, |
| 2251 | { &vop_print_desc, (void *) ufs_print }, |
| 2252 | { &vop_readdir_desc, (void *) ufs_readdir }, |
| 2253 | { &vop_readlink_desc, (void *) ufs_readlink }, |
| 2254 | { &vop_reclaim_desc, (void *) ufs_reclaim }, |
| 2255 | { &vop_remove_desc, (void *) ufs_remove }, |
| 2256 | { &vop_rename_desc, (void *) ufs_rename }, |
| 2257 | { &vop_rmdir_desc, (void *) ufs_rmdir }, |
| 2258 | { &vop_setattr_desc, (void *) ufs_setattr }, |
| 2259 | { &vop_strategy_desc, (void *) ufs_strategy }, |
| 2260 | { &vop_symlink_desc, (void *) ufs_symlink }, |
| 2261 | { &vop_unlock_desc, (void *) vop_stdunlock }, |
| 2262 | { &vop_whiteout_desc, (void *) ufs_whiteout }, |
| 2263 | { NULL, NULL } |
| 2264 | }; |
| 2265 | static struct vnodeopv_desc ufs_vnodeop_opv_desc = |
| 2266 | { &ufs_vnode_vops, ufs_vnodeop_entries }; |
| 2267 | |
| 2268 | static struct vop_ops *ufs_spec_vops; |
| 2269 | static struct vnodeopv_entry_desc ufs_specop_entries[] = { |
| 2270 | { &vop_default_desc, (void *) spec_vnoperate }, |
| 2271 | { &vop_fsync_desc, (void *) ufs_missingop }, |
| 2272 | { &vop_access_desc, (void *) ufs_access }, |
| 2273 | { &vop_close_desc, (void *) ufsspec_close }, |
| 2274 | { &vop_getattr_desc, (void *) ufs_getattr }, |
| 2275 | { &vop_inactive_desc, (void *) ufs_inactive }, |
| 2276 | { &vop_islocked_desc, (void *) vop_stdislocked }, |
| 2277 | { &vop_lock_desc, (void *) vop_stdlock }, |
| 2278 | { &vop_print_desc, (void *) ufs_print }, |
| 2279 | { &vop_read_desc, (void *) ufsspec_read }, |
| 2280 | { &vop_reclaim_desc, (void *) ufs_reclaim }, |
| 2281 | { &vop_setattr_desc, (void *) ufs_setattr }, |
| 2282 | { &vop_unlock_desc, (void *) vop_stdunlock }, |
| 2283 | { &vop_write_desc, (void *) ufsspec_write }, |
| 2284 | { NULL, NULL } |
| 2285 | }; |
| 2286 | static struct vnodeopv_desc ufs_specop_opv_desc = |
| 2287 | { &ufs_spec_vops, ufs_specop_entries }; |
| 2288 | |
| 2289 | static struct vop_ops *ufs_fifo_vops; |
| 2290 | static struct vnodeopv_entry_desc ufs_fifoop_entries[] = { |
| 2291 | { &vop_default_desc, (void *) fifo_vnoperate }, |
| 2292 | { &vop_fsync_desc, (void *) ufs_missingop }, |
| 2293 | { &vop_access_desc, (void *) ufs_access }, |
| 2294 | { &vop_close_desc, (void *) ufsfifo_close }, |
| 2295 | { &vop_getattr_desc, (void *) ufs_getattr }, |
| 2296 | { &vop_inactive_desc, (void *) ufs_inactive }, |
| 2297 | { &vop_islocked_desc, (void *) vop_stdislocked }, |
| 2298 | { &vop_kqfilter_desc, (void *) ufsfifo_kqfilter }, |
| 2299 | { &vop_lock_desc, (void *) vop_stdlock }, |
| 2300 | { &vop_print_desc, (void *) ufs_print }, |
| 2301 | { &vop_read_desc, (void *) ufsfifo_read }, |
| 2302 | { &vop_reclaim_desc, (void *) ufs_reclaim }, |
| 2303 | { &vop_setattr_desc, (void *) ufs_setattr }, |
| 2304 | { &vop_unlock_desc, (void *) vop_stdunlock }, |
| 2305 | { &vop_write_desc, (void *) ufsfifo_write }, |
| 2306 | { NULL, NULL } |
| 2307 | }; |
| 2308 | static struct vnodeopv_desc ufs_fifoop_opv_desc = |
| 2309 | { &ufs_fifo_vops, ufs_fifoop_entries }; |
| 2310 | |
| 2311 | VNODEOP_SET(ufs_vnodeop_opv_desc); |
| 2312 | VNODEOP_SET(ufs_specop_opv_desc); |
| 2313 | VNODEOP_SET(ufs_fifoop_opv_desc); |
| 2314 | |
| 2315 | /* |
| 2316 | * ufs_vnoperate(struct vnodeop_desc *a_desc) |
| 2317 | */ |
| 2318 | int |
| 2319 | ufs_vnoperate(struct vop_generic_args *ap) |
| 2320 | { |
| 2321 | return (VOCALL(ufs_vnode_vops, ap)); |
| 2322 | } |
| 2323 | |
| 2324 | /* |
| 2325 | * ufs_vnoperatefifo(struct vnodeop_desc *a_desc) |
| 2326 | */ |
| 2327 | int |
| 2328 | ufs_vnoperatefifo(struct vop_generic_args *ap) |
| 2329 | { |
| 2330 | return (VOCALL(ufs_fifo_vops, ap)); |
| 2331 | } |
| 2332 | |
| 2333 | /* |
| 2334 | * ufs_vnoperatespec(struct vnodeop_desc *a_desc) |
| 2335 | */ |
| 2336 | int |
| 2337 | ufs_vnoperatespec(struct vop_generic_args *ap) |
| 2338 | { |
| 2339 | return (VOCALL(ufs_spec_vops, ap)); |
| 2340 | } |