| 1 | /* $NetBSD: ext2fs_vnops.c,v 1.125 2016/08/15 18:38:10 jdolecek Exp $ */ |
| 2 | |
| 3 | /* |
| 4 | * Copyright (c) 1982, 1986, 1989, 1993 |
| 5 | * The Regents of the University of California. All rights reserved. |
| 6 | * (c) UNIX System Laboratories, Inc. |
| 7 | * All or some portions of this file are derived from material licensed |
| 8 | * to the University of California by American Telephone and Telegraph |
| 9 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with |
| 10 | * the permission of UNIX System Laboratories, Inc. |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * 3. Neither the name of the University nor the names of its contributors |
| 21 | * may be used to endorse or promote products derived from this software |
| 22 | * without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 34 | * SUCH DAMAGE. |
| 35 | * |
| 36 | * @(#)ufs_vnops.c 8.14 (Berkeley) 10/26/94 |
| 37 | * Modified for ext2fs by Manuel Bouyer. |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | * Copyright (c) 1997 Manuel Bouyer. |
| 42 | * |
| 43 | * Redistribution and use in source and binary forms, with or without |
| 44 | * modification, are permitted provided that the following conditions |
| 45 | * are met: |
| 46 | * 1. Redistributions of source code must retain the above copyright |
| 47 | * notice, this list of conditions and the following disclaimer. |
| 48 | * 2. Redistributions in binary form must reproduce the above copyright |
| 49 | * notice, this list of conditions and the following disclaimer in the |
| 50 | * documentation and/or other materials provided with the distribution. |
| 51 | * |
| 52 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 53 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 54 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 55 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 56 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 57 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 58 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 59 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 60 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 61 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 62 | * |
| 63 | * @(#)ufs_vnops.c 8.14 (Berkeley) 10/26/94 |
| 64 | * Modified for ext2fs by Manuel Bouyer. |
| 65 | */ |
| 66 | |
| 67 | #include <sys/cdefs.h> |
| 68 | __KERNEL_RCSID(0, "$NetBSD: ext2fs_vnops.c,v 1.125 2016/08/15 18:38:10 jdolecek Exp $" ); |
| 69 | |
| 70 | #include <sys/param.h> |
| 71 | #include <sys/systm.h> |
| 72 | #include <sys/resourcevar.h> |
| 73 | #include <sys/kernel.h> |
| 74 | #include <sys/file.h> |
| 75 | #include <sys/stat.h> |
| 76 | #include <sys/buf.h> |
| 77 | #include <sys/proc.h> |
| 78 | #include <sys/mount.h> |
| 79 | #include <sys/namei.h> |
| 80 | #include <sys/vnode.h> |
| 81 | #include <sys/lockf.h> |
| 82 | #include <sys/pool.h> |
| 83 | #include <sys/signalvar.h> |
| 84 | #include <sys/kauth.h> |
| 85 | |
| 86 | #include <miscfs/fifofs/fifo.h> |
| 87 | #include <miscfs/genfs/genfs.h> |
| 88 | #include <miscfs/specfs/specdev.h> |
| 89 | |
| 90 | #include <ufs/ufs/inode.h> |
| 91 | #include <ufs/ufs/ufs_extern.h> |
| 92 | #include <ufs/ufs/ufsmount.h> |
| 93 | |
| 94 | #include <ufs/ext2fs/ext2fs.h> |
| 95 | #include <ufs/ext2fs/ext2fs_extern.h> |
| 96 | #include <ufs/ext2fs/ext2fs_dir.h> |
| 97 | #include <ufs/ext2fs/ext2fs_xattr.h> |
| 98 | |
| 99 | extern int prtactive; |
| 100 | |
| 101 | static int ext2fs_chmod(struct vnode *, int, kauth_cred_t, struct lwp *); |
| 102 | static int ext2fs_chown(struct vnode *, uid_t, gid_t, kauth_cred_t, |
| 103 | struct lwp *); |
| 104 | |
| 105 | union _qcvt { |
| 106 | int64_t qcvt; |
| 107 | int32_t val[2]; |
| 108 | }; |
| 109 | |
| 110 | #define SETHIGH(q, h) { \ |
| 111 | union _qcvt tmp; \ |
| 112 | tmp.qcvt = (q); \ |
| 113 | tmp.val[_QUAD_HIGHWORD] = (h); \ |
| 114 | (q) = tmp.qcvt; \ |
| 115 | } |
| 116 | #define SETLOW(q, l) { \ |
| 117 | union _qcvt tmp; \ |
| 118 | tmp.qcvt = (q); \ |
| 119 | tmp.val[_QUAD_LOWWORD] = (l); \ |
| 120 | (q) = tmp.qcvt; \ |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Create a regular file |
| 125 | */ |
| 126 | int |
| 127 | ext2fs_create(void *v) |
| 128 | { |
| 129 | struct vop_create_v3_args /* { |
| 130 | struct vnode *a_dvp; |
| 131 | struct vnode **a_vpp; |
| 132 | struct componentname *a_cnp; |
| 133 | struct vattr *a_vap; |
| 134 | } */ *ap = v; |
| 135 | int error; |
| 136 | |
| 137 | error = |
| 138 | ext2fs_makeinode(MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode), |
| 139 | ap->a_dvp, ap->a_vpp, ap->a_cnp, 1); |
| 140 | |
| 141 | if (error) |
| 142 | return error; |
| 143 | VN_KNOTE(ap->a_dvp, NOTE_WRITE); |
| 144 | VOP_UNLOCK(*ap->a_vpp); |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Mknod vnode call |
| 150 | */ |
| 151 | /* ARGSUSED */ |
| 152 | int |
| 153 | ext2fs_mknod(void *v) |
| 154 | { |
| 155 | struct vop_mknod_v3_args /* { |
| 156 | struct vnode *a_dvp; |
| 157 | struct vnode **a_vpp; |
| 158 | struct componentname *a_cnp; |
| 159 | struct vattr *a_vap; |
| 160 | } */ *ap = v; |
| 161 | struct vattr *vap = ap->a_vap; |
| 162 | struct vnode **vpp = ap->a_vpp; |
| 163 | struct inode *ip; |
| 164 | int error; |
| 165 | struct mount *mp; |
| 166 | ino_t ino; |
| 167 | |
| 168 | if ((error = ext2fs_makeinode(MAKEIMODE(vap->va_type, vap->va_mode), |
| 169 | ap->a_dvp, vpp, ap->a_cnp, 1)) != 0) |
| 170 | return error; |
| 171 | VN_KNOTE(ap->a_dvp, NOTE_WRITE); |
| 172 | ip = VTOI(*vpp); |
| 173 | mp = (*vpp)->v_mount; |
| 174 | ino = ip->i_number; |
| 175 | ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; |
| 176 | if (vap->va_rdev != VNOVAL) { |
| 177 | /* |
| 178 | * Want to be able to use this to make badblock |
| 179 | * inodes, so don't truncate the dev number. |
| 180 | */ |
| 181 | ip->i_din.e2fs_din->e2di_rdev = h2fs32(vap->va_rdev); |
| 182 | } |
| 183 | /* |
| 184 | * Remove inode so that it will be reloaded by vcache_get and |
| 185 | * checked to see if it is an alias of an existing entry in |
| 186 | * the inode cache. |
| 187 | */ |
| 188 | (*vpp)->v_type = VNON; |
| 189 | VOP_UNLOCK(*vpp); |
| 190 | vgone(*vpp); |
| 191 | error = vcache_get(mp, &ino, sizeof(ino), vpp); |
| 192 | if (error != 0) { |
| 193 | *vpp = NULL; |
| 194 | return error; |
| 195 | } |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * Open called. |
| 201 | * |
| 202 | * Just check the APPEND flag. |
| 203 | */ |
| 204 | /* ARGSUSED */ |
| 205 | int |
| 206 | ext2fs_open(void *v) |
| 207 | { |
| 208 | struct vop_open_args /* { |
| 209 | struct vnode *a_vp; |
| 210 | int a_mode; |
| 211 | kauth_cred_t a_cred; |
| 212 | } */ *ap = v; |
| 213 | |
| 214 | /* |
| 215 | * Files marked append-only must be opened for appending. |
| 216 | */ |
| 217 | if ((VTOI(ap->a_vp)->i_e2fs_flags & EXT2_APPEND) && |
| 218 | (ap->a_mode & (FWRITE | O_APPEND)) == FWRITE) |
| 219 | return EPERM; |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | static int |
| 224 | ext2fs_check_possible(struct vnode *vp, struct inode *ip, mode_t mode) |
| 225 | { |
| 226 | |
| 227 | /* |
| 228 | * Disallow write attempts on read-only file systems; |
| 229 | * unless the file is a socket, fifo, or a block or |
| 230 | * character device resident on the file system. |
| 231 | */ |
| 232 | if (mode & VWRITE) { |
| 233 | switch (vp->v_type) { |
| 234 | case VDIR: |
| 235 | case VLNK: |
| 236 | case VREG: |
| 237 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 238 | return EROFS; |
| 239 | break; |
| 240 | default: |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* If immutable bit set, nobody gets to write it. */ |
| 246 | if ((mode & VWRITE) && (ip->i_e2fs_flags & EXT2_IMMUTABLE)) |
| 247 | return EPERM; |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static int |
| 253 | ext2fs_check_permitted(struct vnode *vp, struct inode *ip, mode_t mode, |
| 254 | kauth_cred_t cred) |
| 255 | { |
| 256 | |
| 257 | return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(mode, vp->v_type, |
| 258 | ip->i_e2fs_mode & ALLPERMS), vp, NULL, genfs_can_access(vp->v_type, |
| 259 | ip->i_e2fs_mode & ALLPERMS, ip->i_uid, ip->i_gid, mode, cred)); |
| 260 | } |
| 261 | |
| 262 | int |
| 263 | ext2fs_access(void *v) |
| 264 | { |
| 265 | struct vop_access_args /* { |
| 266 | struct vnode *a_vp; |
| 267 | int a_mode; |
| 268 | kauth_cred_t a_cred; |
| 269 | } */ *ap = v; |
| 270 | struct vnode *vp = ap->a_vp; |
| 271 | struct inode *ip = VTOI(vp); |
| 272 | mode_t mode = ap->a_mode; |
| 273 | int error; |
| 274 | |
| 275 | error = ext2fs_check_possible(vp, ip, mode); |
| 276 | if (error) |
| 277 | return error; |
| 278 | |
| 279 | error = ext2fs_check_permitted(vp, ip, mode, ap->a_cred); |
| 280 | |
| 281 | return error; |
| 282 | } |
| 283 | |
| 284 | /* ARGSUSED */ |
| 285 | int |
| 286 | ext2fs_getattr(void *v) |
| 287 | { |
| 288 | struct vop_getattr_args /* { |
| 289 | struct vnode *a_vp; |
| 290 | struct vattr *a_vap; |
| 291 | kauth_cred_t a_cred; |
| 292 | } */ *ap = v; |
| 293 | struct vnode *vp = ap->a_vp; |
| 294 | struct inode *ip = VTOI(vp); |
| 295 | struct vattr *vap = ap->a_vap; |
| 296 | |
| 297 | EXT2FS_ITIMES(ip, NULL, NULL, NULL); |
| 298 | /* |
| 299 | * Copy from inode table |
| 300 | */ |
| 301 | vap->va_fsid = ip->i_dev; |
| 302 | vap->va_fileid = ip->i_number; |
| 303 | vap->va_mode = ip->i_e2fs_mode & ALLPERMS; |
| 304 | vap->va_nlink = ip->i_e2fs_nlink; |
| 305 | vap->va_uid = ip->i_uid; |
| 306 | vap->va_gid = ip->i_gid; |
| 307 | vap->va_rdev = (dev_t)fs2h32(ip->i_din.e2fs_din->e2di_rdev); |
| 308 | vap->va_size = vp->v_size; |
| 309 | EXT2_DINODE_TIME_GET(&vap->va_atime, ip->i_din.e2fs_din, e2di_atime, EXT2_DINODE_SIZE(ip->i_e2fs)); |
| 310 | EXT2_DINODE_TIME_GET(&vap->va_mtime, ip->i_din.e2fs_din, e2di_mtime, EXT2_DINODE_SIZE(ip->i_e2fs)); |
| 311 | EXT2_DINODE_TIME_GET(&vap->va_ctime, ip->i_din.e2fs_din, e2di_ctime, EXT2_DINODE_SIZE(ip->i_e2fs)); |
| 312 | if (EXT2_DINODE_FITS(ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs))) { |
| 313 | EXT2_DINODE_TIME_GET(&vap->va_birthtime, ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs)); |
| 314 | } |
| 315 | |
| 316 | vap->va_flags = 0; |
| 317 | vap->va_flags |= (ip->i_e2fs_flags & EXT2_NODUMP) ? UF_NODUMP : 0; |
| 318 | #ifdef EXT2FS_SYSTEM_FLAGS |
| 319 | vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? SF_IMMUTABLE : 0; |
| 320 | vap->va_flags |= (ip->i_e2fs_flags & EXT2_APPEND) ? SF_APPEND : 0; |
| 321 | #else |
| 322 | vap->va_flags |= (ip->i_e2fs_flags & EXT2_IMMUTABLE) ? UF_IMMUTABLE : 0; |
| 323 | vap->va_flags |= (ip->i_e2fs_flags & EXT2_APPEND) ? UF_APPEND : 0; |
| 324 | #endif |
| 325 | |
| 326 | vap->va_gen = ip->i_e2fs_gen; |
| 327 | /* this doesn't belong here */ |
| 328 | if (vp->v_type == VBLK) |
| 329 | vap->va_blocksize = BLKDEV_IOSIZE; |
| 330 | else if (vp->v_type == VCHR) |
| 331 | vap->va_blocksize = MAXBSIZE; |
| 332 | else |
| 333 | vap->va_blocksize = vp->v_mount->mnt_stat.f_iosize; |
| 334 | vap->va_bytes = dbtob(ext2fs_nblock(ip)); |
| 335 | vap->va_type = vp->v_type; |
| 336 | vap->va_filerev = ip->i_modrev; |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Set attribute vnode op. called from several syscalls |
| 342 | */ |
| 343 | int |
| 344 | ext2fs_setattr(void *v) |
| 345 | { |
| 346 | struct vop_setattr_args /* { |
| 347 | struct vnode *a_vp; |
| 348 | struct vattr *a_vap; |
| 349 | kauth_cred_t a_cred; |
| 350 | } */ *ap = v; |
| 351 | struct vattr *vap = ap->a_vap; |
| 352 | struct vnode *vp = ap->a_vp; |
| 353 | struct inode *ip = VTOI(vp); |
| 354 | kauth_cred_t cred = ap->a_cred; |
| 355 | struct lwp *l = curlwp; |
| 356 | int error; |
| 357 | kauth_action_t action = KAUTH_VNODE_WRITE_FLAGS; |
| 358 | bool changing_sysflags = false; |
| 359 | |
| 360 | /* |
| 361 | * Check for unsettable attributes. |
| 362 | */ |
| 363 | if ((vap->va_type != VNON) || (vap->va_nlink != (nlink_t)VNOVAL) || |
| 364 | (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || |
| 365 | (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || |
| 366 | ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { |
| 367 | return EINVAL; |
| 368 | } |
| 369 | if (vap->va_flags != VNOVAL) { |
| 370 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 371 | return EROFS; |
| 372 | |
| 373 | /* |
| 374 | * Check if we're allowed to change the flags. |
| 375 | * If EXT2FS_SYSTEM_FLAGS is set, then the flags are treated |
| 376 | * as system flags, otherwise they're considered to be user |
| 377 | * flags. |
| 378 | */ |
| 379 | #ifdef EXT2FS_SYSTEM_FLAGS |
| 380 | /* Indicate we're changing system flags if we are. */ |
| 381 | if ((vap->va_flags & SF_APPEND) || |
| 382 | (vap->va_flags & SF_IMMUTABLE)) { |
| 383 | action |= KAUTH_VNODE_WRITE_SYSFLAGS; |
| 384 | changing_sysflags = true; |
| 385 | } |
| 386 | |
| 387 | /* Indicate the node has system flags if it does. */ |
| 388 | if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE)) { |
| 389 | action |= KAUTH_VNODE_HAS_SYSFLAGS; |
| 390 | } |
| 391 | #endif /* EXT2FS_SYSTEM_FLAGS */ |
| 392 | |
| 393 | error = kauth_authorize_vnode(cred, action, vp, NULL, |
| 394 | genfs_can_chflags(cred, vp->v_type, ip->i_uid, |
| 395 | changing_sysflags)); |
| 396 | if (error) |
| 397 | return error; |
| 398 | |
| 399 | ip->i_e2fs_flags &= ~(EXT2_APPEND | EXT2_IMMUTABLE | EXT2_NODUMP); |
| 400 | #ifdef EXT2FS_SYSTEM_FLAGS |
| 401 | ip->i_e2fs_flags |= |
| 402 | (vap->va_flags & SF_APPEND) ? EXT2_APPEND : 0 | |
| 403 | (vap->va_flags & SF_IMMUTABLE) ? EXT2_IMMUTABLE : 0; |
| 404 | #else |
| 405 | ip->i_e2fs_flags |= |
| 406 | (vap->va_flags & UF_APPEND) ? EXT2_APPEND : 0 | |
| 407 | (vap->va_flags & UF_IMMUTABLE) ? EXT2_IMMUTABLE : 0; |
| 408 | #endif |
| 409 | ip->i_e2fs_flags |= |
| 410 | (vap->va_flags & UF_NODUMP) ? EXT2_NODUMP : 0; |
| 411 | ip->i_flag |= IN_CHANGE; |
| 412 | if (vap->va_flags & (IMMUTABLE | APPEND)) |
| 413 | return 0; |
| 414 | } |
| 415 | if (ip->i_e2fs_flags & (EXT2_APPEND | EXT2_IMMUTABLE)) |
| 416 | return EPERM; |
| 417 | /* |
| 418 | * Go through the fields and update iff not VNOVAL. |
| 419 | */ |
| 420 | if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) { |
| 421 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 422 | return EROFS; |
| 423 | error = ext2fs_chown(vp, vap->va_uid, vap->va_gid, cred, l); |
| 424 | if (error) |
| 425 | return error; |
| 426 | } |
| 427 | if (vap->va_size != VNOVAL) { |
| 428 | /* |
| 429 | * Disallow write attempts on read-only file systems; |
| 430 | * unless the file is a socket, fifo, or a block or |
| 431 | * character device resident on the file system. |
| 432 | */ |
| 433 | switch (vp->v_type) { |
| 434 | case VDIR: |
| 435 | return EISDIR; |
| 436 | case VLNK: |
| 437 | case VREG: |
| 438 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 439 | return EROFS; |
| 440 | default: |
| 441 | break; |
| 442 | } |
| 443 | error = ext2fs_truncate(vp, vap->va_size, 0, cred); |
| 444 | if (error) |
| 445 | return error; |
| 446 | } |
| 447 | ip = VTOI(vp); |
| 448 | if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL || vap->va_birthtime.tv_sec != VNOVAL) { |
| 449 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 450 | return EROFS; |
| 451 | error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_TIMES, vp, |
| 452 | NULL, genfs_can_chtimes(vp, vap->va_vaflags, ip->i_uid, |
| 453 | cred)); |
| 454 | if (error) |
| 455 | return error; |
| 456 | if (vap->va_atime.tv_sec != VNOVAL) |
| 457 | if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) |
| 458 | ip->i_flag |= IN_ACCESS; |
| 459 | if (vap->va_mtime.tv_sec != VNOVAL) { |
| 460 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 461 | if (vp->v_mount->mnt_flag & MNT_RELATIME) |
| 462 | ip->i_flag |= IN_ACCESS; |
| 463 | } |
| 464 | if (vap->va_birthtime.tv_sec != VNOVAL && |
| 465 | EXT2_DINODE_FITS(ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs))) { |
| 466 | |
| 467 | EXT2_DINODE_TIME_SET(&vap->va_birthtime, ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs)); |
| 468 | } |
| 469 | error = ext2fs_update(vp, &vap->va_atime, &vap->va_mtime, |
| 470 | UPDATE_WAIT); |
| 471 | if (error) |
| 472 | return error; |
| 473 | } |
| 474 | error = 0; |
| 475 | if (vap->va_mode != (mode_t)VNOVAL) { |
| 476 | if (vp->v_mount->mnt_flag & MNT_RDONLY) |
| 477 | return EROFS; |
| 478 | error = ext2fs_chmod(vp, (int)vap->va_mode, cred, l); |
| 479 | } |
| 480 | VN_KNOTE(vp, NOTE_ATTRIB); |
| 481 | return error; |
| 482 | } |
| 483 | |
| 484 | /* |
| 485 | * Change the mode on a file. |
| 486 | * Inode must be locked before calling. |
| 487 | */ |
| 488 | static int |
| 489 | ext2fs_chmod(struct vnode *vp, int mode, kauth_cred_t cred, struct lwp *l) |
| 490 | { |
| 491 | struct inode *ip = VTOI(vp); |
| 492 | int error; |
| 493 | |
| 494 | error = kauth_authorize_vnode(cred, KAUTH_VNODE_WRITE_SECURITY, vp, |
| 495 | NULL, genfs_can_chmod(vp->v_type, cred, ip->i_uid, ip->i_gid, |
| 496 | mode)); |
| 497 | if (error) |
| 498 | return error; |
| 499 | |
| 500 | ip->i_e2fs_mode &= ~ALLPERMS; |
| 501 | ip->i_e2fs_mode |= (mode & ALLPERMS); |
| 502 | ip->i_flag |= IN_CHANGE; |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Perform chown operation on inode ip; |
| 508 | * inode must be locked prior to call. |
| 509 | */ |
| 510 | static int |
| 511 | ext2fs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred, |
| 512 | struct lwp *l) |
| 513 | { |
| 514 | struct inode *ip = VTOI(vp); |
| 515 | uid_t ouid; |
| 516 | gid_t ogid; |
| 517 | int error; |
| 518 | |
| 519 | if (uid == (uid_t)VNOVAL) |
| 520 | uid = ip->i_uid; |
| 521 | if (gid == (gid_t)VNOVAL) |
| 522 | gid = ip->i_gid; |
| 523 | |
| 524 | error = kauth_authorize_vnode(cred, KAUTH_VNODE_CHANGE_OWNERSHIP, vp, |
| 525 | NULL, genfs_can_chown(cred, ip->i_uid, ip->i_gid, uid, gid)); |
| 526 | if (error) |
| 527 | return error; |
| 528 | |
| 529 | ogid = ip->i_gid; |
| 530 | ouid = ip->i_uid; |
| 531 | |
| 532 | ip->i_e2fs_gid = gid & 0xffff; |
| 533 | ip->i_e2fs_uid = uid & 0xffff; |
| 534 | if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) { |
| 535 | ip->i_e2fs_gid_high = (gid >> 16) & 0xffff; |
| 536 | ip->i_e2fs_uid_high = (uid >> 16) & 0xffff; |
| 537 | } else { |
| 538 | ip->i_e2fs_gid_high = 0; |
| 539 | ip->i_e2fs_uid_high = 0; |
| 540 | } |
| 541 | if (ouid != uid || ogid != gid) { |
| 542 | ext2fs_set_inode_guid(ip); |
| 543 | ip->i_flag |= IN_CHANGE; |
| 544 | } |
| 545 | if (ouid != uid && (ip->i_e2fs_mode & ISUID) && |
| 546 | kauth_authorize_vnode(cred, KAUTH_VNODE_RETAIN_SUID, |
| 547 | vp, NULL, EPERM) != 0) |
| 548 | ip->i_e2fs_mode &= ~ISUID; |
| 549 | if (ogid != gid && (ip->i_e2fs_mode & ISGID) && |
| 550 | kauth_authorize_vnode(cred, KAUTH_VNODE_RETAIN_SGID, |
| 551 | vp, NULL, EPERM) != 0) |
| 552 | ip->i_e2fs_mode &= ~ISGID; |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | int |
| 557 | ext2fs_remove(void *v) |
| 558 | { |
| 559 | struct vop_remove_args /* { |
| 560 | struct vnode *a_dvp; |
| 561 | struct vnode *a_vp; |
| 562 | struct componentname *a_cnp; |
| 563 | } */ *ap = v; |
| 564 | struct inode *ip; |
| 565 | struct vnode *vp = ap->a_vp; |
| 566 | struct vnode *dvp = ap->a_dvp; |
| 567 | struct ufs_lookup_results *ulr; |
| 568 | int error; |
| 569 | |
| 570 | /* XXX should handle this material another way */ |
| 571 | ulr = &VTOI(dvp)->i_crap; |
| 572 | UFS_CHECK_CRAPCOUNTER(VTOI(dvp)); |
| 573 | |
| 574 | ip = VTOI(vp); |
| 575 | if (vp->v_type == VDIR || |
| 576 | (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) || |
| 577 | (VTOI(dvp)->i_e2fs_flags & EXT2_APPEND)) { |
| 578 | error = EPERM; |
| 579 | } else { |
| 580 | error = ext2fs_dirremove(dvp, ulr, ap->a_cnp); |
| 581 | if (error == 0) { |
| 582 | ip->i_e2fs_nlink--; |
| 583 | ip->i_flag |= IN_CHANGE; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | VN_KNOTE(vp, NOTE_DELETE); |
| 588 | VN_KNOTE(dvp, NOTE_WRITE); |
| 589 | if (dvp == vp) |
| 590 | vrele(vp); |
| 591 | else |
| 592 | vput(vp); |
| 593 | vput(dvp); |
| 594 | return error; |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * ext2fs_link: create hard link. |
| 599 | */ |
| 600 | int |
| 601 | ext2fs_link(void *v) |
| 602 | { |
| 603 | struct vop_link_v2_args /* { |
| 604 | struct vnode *a_dvp; |
| 605 | struct vnode *a_vp; |
| 606 | struct componentname *a_cnp; |
| 607 | } */ *ap = v; |
| 608 | struct vnode *dvp = ap->a_dvp; |
| 609 | struct vnode *vp = ap->a_vp; |
| 610 | struct componentname *cnp = ap->a_cnp; |
| 611 | struct inode *ip; |
| 612 | int error; |
| 613 | struct ufs_lookup_results *ulr; |
| 614 | |
| 615 | KASSERT(dvp != vp); |
| 616 | KASSERT(vp->v_type != VDIR); |
| 617 | KASSERT(dvp->v_mount == vp->v_mount); |
| 618 | |
| 619 | /* XXX should handle this material another way */ |
| 620 | ulr = &VTOI(dvp)->i_crap; |
| 621 | UFS_CHECK_CRAPCOUNTER(VTOI(dvp)); |
| 622 | |
| 623 | error = vn_lock(vp, LK_EXCLUSIVE); |
| 624 | if (error) { |
| 625 | VOP_ABORTOP(dvp, cnp); |
| 626 | goto out2; |
| 627 | } |
| 628 | ip = VTOI(vp); |
| 629 | if ((nlink_t)ip->i_e2fs_nlink >= EXT2FS_LINK_MAX) { |
| 630 | VOP_ABORTOP(dvp, cnp); |
| 631 | error = EMLINK; |
| 632 | goto out1; |
| 633 | } |
| 634 | if (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND)) { |
| 635 | VOP_ABORTOP(dvp, cnp); |
| 636 | error = EPERM; |
| 637 | goto out1; |
| 638 | } |
| 639 | ip->i_e2fs_nlink++; |
| 640 | ip->i_flag |= IN_CHANGE; |
| 641 | error = ext2fs_update(vp, NULL, NULL, UPDATE_WAIT); |
| 642 | if (!error) |
| 643 | error = ext2fs_direnter(ip, dvp, ulr, cnp); |
| 644 | if (error) { |
| 645 | ip->i_e2fs_nlink--; |
| 646 | ip->i_flag |= IN_CHANGE; |
| 647 | } |
| 648 | out1: |
| 649 | VOP_UNLOCK(vp); |
| 650 | out2: |
| 651 | VN_KNOTE(vp, NOTE_LINK); |
| 652 | VN_KNOTE(dvp, NOTE_WRITE); |
| 653 | return error; |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Mkdir system call |
| 658 | */ |
| 659 | int |
| 660 | ext2fs_mkdir(void *v) |
| 661 | { |
| 662 | struct vop_mkdir_v3_args /* { |
| 663 | struct vnode *a_dvp; |
| 664 | struct vnode **a_vpp; |
| 665 | struct componentname *a_cnp; |
| 666 | struct vattr *a_vap; |
| 667 | } */ *ap = v; |
| 668 | struct vnode *dvp = ap->a_dvp; |
| 669 | struct componentname *cnp = ap->a_cnp; |
| 670 | struct inode *ip, *dp = VTOI(dvp); |
| 671 | struct vnode *tvp; |
| 672 | struct ext2fs_dirtemplate dirtemplate; |
| 673 | int error; |
| 674 | struct ufs_lookup_results *ulr; |
| 675 | |
| 676 | /* XXX should handle this material another way */ |
| 677 | ulr = &VTOI(dvp)->i_crap; |
| 678 | UFS_CHECK_CRAPCOUNTER(VTOI(dvp)); |
| 679 | |
| 680 | /* |
| 681 | * Acquire the inode, but don't sync/direnter it just yet |
| 682 | */ |
| 683 | error = ext2fs_makeinode(IFDIR | ap->a_vap->va_mode, ap->a_dvp, |
| 684 | &tvp, ap->a_cnp, 0); |
| 685 | if (error) |
| 686 | goto out; |
| 687 | |
| 688 | /* the link count is going to be 2 when all is done */ |
| 689 | ip = VTOI(tvp); |
| 690 | ip->i_e2fs_nlink = 2; |
| 691 | |
| 692 | /* |
| 693 | * Bump link count in parent directory |
| 694 | * to reflect work done below. Should |
| 695 | * be done before reference is created |
| 696 | * so reparation is possible if we crash. |
| 697 | */ |
| 698 | if (dp->i_e2fs_nlink != EXT2FS_LINK_INF) |
| 699 | dp->i_e2fs_nlink++; |
| 700 | |
| 701 | /* |
| 702 | * If we hit the link limit, for directories just set the nlink |
| 703 | * to special value 1, which means the link count is bigger |
| 704 | * than EXT2FS_LINK_MAX. |
| 705 | */ |
| 706 | if ((nlink_t)dp->i_e2fs_nlink >= EXT2FS_LINK_MAX) { |
| 707 | dp->i_e2fs_nlink = EXT2FS_LINK_INF; |
| 708 | |
| 709 | /* set the feature flag DIR_NLINK if not set already */ |
| 710 | if (!EXT2F_HAS_ROCOMPAT_FEATURE(dp->i_e2fs, EXT2F_ROCOMPAT_DIR_NLINK)) { |
| 711 | dp->i_e2fs->e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_DIR_NLINK; |
| 712 | dp->i_e2fs->e2fs_fmod = 1; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | dp->i_flag |= IN_CHANGE; |
| 717 | if ((error = ext2fs_update(dvp, NULL, NULL, UPDATE_DIROP)) != 0) |
| 718 | goto bad; |
| 719 | |
| 720 | /* Initialize directory with "." and ".." from static template. */ |
| 721 | memset(&dirtemplate, 0, sizeof(dirtemplate)); |
| 722 | dirtemplate.dot_ino = h2fs32(ip->i_number); |
| 723 | dirtemplate.dot_reclen = h2fs16(12); |
| 724 | dirtemplate.dot_namlen = 1; |
| 725 | if (EXT2F_HAS_INCOMPAT_FEATURE(dp->i_e2fs, EXT2F_INCOMPAT_FTYPE)) { |
| 726 | dirtemplate.dot_type = EXT2_FT_DIR; |
| 727 | } |
| 728 | dirtemplate.dot_name[0] = '.'; |
| 729 | dirtemplate.dotdot_ino = h2fs32(dp->i_number); |
| 730 | dirtemplate.dotdot_reclen = h2fs16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12); |
| 731 | dirtemplate.dotdot_namlen = 2; |
| 732 | if (EXT2F_HAS_INCOMPAT_FEATURE(dp->i_e2fs, EXT2F_INCOMPAT_FTYPE)) { |
| 733 | dirtemplate.dotdot_type = EXT2_FT_DIR; |
| 734 | } |
| 735 | dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.'; |
| 736 | error = ufs_bufio(UIO_WRITE, tvp, (void *)&dirtemplate, |
| 737 | sizeof (dirtemplate), (off_t)0, IO_NODELOCKED|IO_SYNC, |
| 738 | cnp->cn_cred, (size_t *)0, NULL); |
| 739 | if (error) { |
| 740 | if (dp->i_e2fs_nlink != EXT2FS_LINK_INF) |
| 741 | dp->i_e2fs_nlink--; |
| 742 | dp->i_flag |= IN_CHANGE; |
| 743 | goto bad; |
| 744 | } |
| 745 | if (VTOI(dvp)->i_e2fs->e2fs_bsize > dvp->v_mount->mnt_stat.f_bsize) |
| 746 | panic("ext2fs_mkdir: blksize" ); /* XXX should grow with balloc() */ |
| 747 | else { |
| 748 | error = ext2fs_setsize(ip, VTOI(dvp)->i_e2fs->e2fs_bsize); |
| 749 | if (error) { |
| 750 | if (dp->i_e2fs_nlink != EXT2FS_LINK_INF) |
| 751 | dp->i_e2fs_nlink--; |
| 752 | dp->i_flag |= IN_CHANGE; |
| 753 | goto bad; |
| 754 | } |
| 755 | ip->i_flag |= IN_CHANGE; |
| 756 | uvm_vnp_setsize(tvp, ext2fs_size(ip)); |
| 757 | } |
| 758 | |
| 759 | /* Directory set up, now install its entry in the parent directory. */ |
| 760 | error = ext2fs_direnter(ip, dvp, ulr, cnp); |
| 761 | if (error != 0) { |
| 762 | if (dp->i_e2fs_nlink != EXT2FS_LINK_INF) |
| 763 | dp->i_e2fs_nlink--; |
| 764 | dp->i_flag |= IN_CHANGE; |
| 765 | } |
| 766 | bad: |
| 767 | /* |
| 768 | * No need to do an explicit ext2fs_truncate here, vrele will do this |
| 769 | * for us because we set the link count to 0. |
| 770 | */ |
| 771 | if (error) { |
| 772 | ip->i_e2fs_nlink = 0; |
| 773 | ip->i_flag |= IN_CHANGE; |
| 774 | vput(tvp); |
| 775 | } else { |
| 776 | VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK); |
| 777 | VOP_UNLOCK(tvp); |
| 778 | *ap->a_vpp = tvp; |
| 779 | } |
| 780 | out: |
| 781 | return error; |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * Rmdir system call. |
| 786 | */ |
| 787 | int |
| 788 | ext2fs_rmdir(void *v) |
| 789 | { |
| 790 | struct vop_rmdir_args /* { |
| 791 | struct vnode *a_dvp; |
| 792 | struct vnode *a_vp; |
| 793 | struct componentname *a_cnp; |
| 794 | } */ *ap = v; |
| 795 | struct vnode *vp = ap->a_vp; |
| 796 | struct vnode *dvp = ap->a_dvp; |
| 797 | struct componentname *cnp = ap->a_cnp; |
| 798 | struct inode *ip, *dp; |
| 799 | int error; |
| 800 | struct ufs_lookup_results *ulr; |
| 801 | |
| 802 | ip = VTOI(vp); |
| 803 | dp = VTOI(dvp); |
| 804 | |
| 805 | /* XXX should handle this material another way */ |
| 806 | ulr = &dp->i_crap; |
| 807 | UFS_CHECK_CRAPCOUNTER(dp); |
| 808 | |
| 809 | /* |
| 810 | * No rmdir "." please. |
| 811 | */ |
| 812 | if (dp == ip) { |
| 813 | vrele(dvp); |
| 814 | vput(vp); |
| 815 | return EINVAL; |
| 816 | } |
| 817 | /* |
| 818 | * Verify the directory is empty (and valid). |
| 819 | * (Rmdir ".." won't be valid since |
| 820 | * ".." will contain a reference to |
| 821 | * the current directory and thus be |
| 822 | * non-empty.) |
| 823 | */ |
| 824 | error = 0; |
| 825 | if ((ip->i_e2fs_nlink != 2 && ip->i_e2fs_nlink != EXT2FS_LINK_INF) || |
| 826 | !ext2fs_dirempty(ip, dp->i_number, cnp->cn_cred)) { |
| 827 | error = ENOTEMPTY; |
| 828 | goto out; |
| 829 | } |
| 830 | if ((dp->i_e2fs_flags & EXT2_APPEND) || |
| 831 | (ip->i_e2fs_flags & (EXT2_IMMUTABLE | EXT2_APPEND))) { |
| 832 | error = EPERM; |
| 833 | goto out; |
| 834 | } |
| 835 | /* |
| 836 | * Delete reference to directory before purging |
| 837 | * inode. If we crash in between, the directory |
| 838 | * will be reattached to lost+found, |
| 839 | */ |
| 840 | error = ext2fs_dirremove(dvp, ulr, cnp); |
| 841 | if (error != 0) |
| 842 | goto out; |
| 843 | if (dp->i_e2fs_nlink != EXT2FS_LINK_INF) |
| 844 | dp->i_e2fs_nlink--; |
| 845 | dp->i_flag |= IN_CHANGE; |
| 846 | VN_KNOTE(dvp, NOTE_WRITE | NOTE_LINK); |
| 847 | cache_purge(dvp); |
| 848 | vput(dvp); |
| 849 | dvp = NULL; |
| 850 | /* |
| 851 | * Truncate inode. The only stuff left |
| 852 | * in the directory is "." and "..". The |
| 853 | * "." reference is inconsequential since |
| 854 | * we're quashing it. The ".." reference |
| 855 | * has already been adjusted above. We've |
| 856 | * removed the "." reference and the reference |
| 857 | * in the parent directory, but there may be |
| 858 | * other hard links so decrement by 2 and |
| 859 | * worry about them later. |
| 860 | */ |
| 861 | ip->i_e2fs_nlink -= 2; |
| 862 | error = ext2fs_truncate(vp, (off_t)0, IO_SYNC, cnp->cn_cred); |
| 863 | cache_purge(ITOV(ip)); |
| 864 | out: |
| 865 | VN_KNOTE(vp, NOTE_DELETE); |
| 866 | if (dvp) |
| 867 | vput(dvp); |
| 868 | vput(vp); |
| 869 | return error; |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * symlink -- make a symbolic link |
| 874 | */ |
| 875 | int |
| 876 | ext2fs_symlink(void *v) |
| 877 | { |
| 878 | struct vop_symlink_v3_args /* { |
| 879 | struct vnode *a_dvp; |
| 880 | struct vnode **a_vpp; |
| 881 | struct componentname *a_cnp; |
| 882 | struct vattr *a_vap; |
| 883 | char *a_target; |
| 884 | } */ *ap = v; |
| 885 | struct vnode *vp, **vpp; |
| 886 | struct inode *ip; |
| 887 | int len, error; |
| 888 | |
| 889 | vpp = ap->a_vpp; |
| 890 | error = ext2fs_makeinode(IFLNK | ap->a_vap->va_mode, ap->a_dvp, |
| 891 | vpp, ap->a_cnp, 1); |
| 892 | if (error) |
| 893 | return error; |
| 894 | VN_KNOTE(ap->a_dvp, NOTE_WRITE); |
| 895 | vp = *vpp; |
| 896 | len = strlen(ap->a_target); |
| 897 | ip = VTOI(vp); |
| 898 | if (len < ip->i_ump->um_maxsymlinklen) { |
| 899 | memcpy(ip->i_din.e2fs_din->e2di_shortlink, ap->a_target, len); |
| 900 | error = ext2fs_setsize(ip, len); |
| 901 | if (error) |
| 902 | goto bad; |
| 903 | ip->i_flag |= IN_CHANGE | IN_UPDATE; |
| 904 | if (vp->v_mount->mnt_flag & MNT_RELATIME) |
| 905 | ip->i_flag |= IN_ACCESS; |
| 906 | uvm_vnp_setsize(vp, len); |
| 907 | } else |
| 908 | error = ufs_bufio(UIO_WRITE, vp, ap->a_target, len, (off_t)0, |
| 909 | IO_NODELOCKED, ap->a_cnp->cn_cred, (size_t *)0, NULL); |
| 910 | bad: |
| 911 | VOP_UNLOCK(vp); |
| 912 | if (error) |
| 913 | vrele(vp); |
| 914 | return error; |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * Return target name of a symbolic link |
| 919 | */ |
| 920 | int |
| 921 | ext2fs_readlink(void *v) |
| 922 | { |
| 923 | struct vop_readlink_args /* { |
| 924 | struct vnode *a_vp; |
| 925 | struct uio *a_uio; |
| 926 | kauth_cred_t a_cred; |
| 927 | } */ *ap = v; |
| 928 | struct vnode *vp = ap->a_vp; |
| 929 | struct inode *ip = VTOI(vp); |
| 930 | struct ufsmount *ump = ip->i_ump; |
| 931 | int isize; |
| 932 | |
| 933 | isize = ext2fs_size(ip); |
| 934 | if (isize < ump->um_maxsymlinklen || |
| 935 | (ump->um_maxsymlinklen == 0 && ext2fs_nblock(ip) == 0)) { |
| 936 | uiomove(ip->i_din.e2fs_din->e2di_shortlink, isize, ap->a_uio); |
| 937 | return 0; |
| 938 | } |
| 939 | return UFS_BUFRD(vp, ap->a_uio, 0, ap->a_cred); |
| 940 | } |
| 941 | |
| 942 | /* |
| 943 | * Advisory record locking support |
| 944 | */ |
| 945 | int |
| 946 | ext2fs_advlock(void *v) |
| 947 | { |
| 948 | struct vop_advlock_args /* { |
| 949 | struct vnode *a_vp; |
| 950 | void * a_id; |
| 951 | int a_op; |
| 952 | struct flock *a_fl; |
| 953 | int a_flags; |
| 954 | } */ *ap = v; |
| 955 | struct inode *ip = VTOI(ap->a_vp); |
| 956 | |
| 957 | return lf_advlock(ap, &ip->i_lockf, ext2fs_size(ip)); |
| 958 | } |
| 959 | |
| 960 | int |
| 961 | ext2fs_fsync(void *v) |
| 962 | { |
| 963 | struct vop_fsync_args /* { |
| 964 | struct vnode *a_vp; |
| 965 | kauth_cred_t a_cred; |
| 966 | int a_flags; |
| 967 | off_t offlo; |
| 968 | off_t offhi; |
| 969 | struct proc *a_p; |
| 970 | } */ *ap = v; |
| 971 | struct vnode *vp = ap->a_vp; |
| 972 | int wait; |
| 973 | int error; |
| 974 | |
| 975 | wait = (ap->a_flags & FSYNC_WAIT) != 0; |
| 976 | |
| 977 | if (vp->v_type == VBLK) |
| 978 | error = spec_fsync(v); |
| 979 | else |
| 980 | error = vflushbuf(vp, ap->a_flags); |
| 981 | if (error == 0 && (ap->a_flags & FSYNC_DATAONLY) == 0) |
| 982 | error = ext2fs_update(vp, NULL, NULL, wait ? UPDATE_WAIT : 0); |
| 983 | |
| 984 | if (error == 0 && ap->a_flags & FSYNC_CACHE) { |
| 985 | int l = 0; |
| 986 | error = VOP_IOCTL(VTOI(vp)->i_devvp, DIOCCACHESYNC, &l, FWRITE, |
| 987 | curlwp->l_cred); |
| 988 | } |
| 989 | |
| 990 | return error; |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * Initialize the vnode associated with a new inode, handle aliased |
| 995 | * vnodes. |
| 996 | */ |
| 997 | int |
| 998 | ext2fs_vinit(struct mount *mntp, int (**specops)(void *), |
| 999 | int (**fifoops)(void *), struct vnode **vpp) |
| 1000 | { |
| 1001 | struct timeval tv; |
| 1002 | struct inode *ip; |
| 1003 | struct vnode *vp; |
| 1004 | |
| 1005 | vp = *vpp; |
| 1006 | ip = VTOI(vp); |
| 1007 | switch(vp->v_type = IFTOVT(ip->i_e2fs_mode)) { |
| 1008 | case VCHR: |
| 1009 | case VBLK: |
| 1010 | vp->v_op = specops; |
| 1011 | spec_node_init(vp, fs2h32(ip->i_din.e2fs_din->e2di_rdev)); |
| 1012 | break; |
| 1013 | case VFIFO: |
| 1014 | vp->v_op = fifoops; |
| 1015 | break; |
| 1016 | case VNON: |
| 1017 | case VBAD: |
| 1018 | case VSOCK: |
| 1019 | case VLNK: |
| 1020 | case VDIR: |
| 1021 | case VREG: |
| 1022 | break; |
| 1023 | } |
| 1024 | if (ip->i_number == UFS_ROOTINO) |
| 1025 | vp->v_vflag |= VV_ROOT; |
| 1026 | /* |
| 1027 | * Initialize modrev times |
| 1028 | */ |
| 1029 | getmicrouptime(&tv); |
| 1030 | SETHIGH(ip->i_modrev, tv.tv_sec); |
| 1031 | SETLOW(ip->i_modrev, tv.tv_usec * 4294); |
| 1032 | *vpp = vp; |
| 1033 | return 0; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
| 1037 | * Allocate a new inode. |
| 1038 | */ |
| 1039 | int |
| 1040 | ext2fs_makeinode(int mode, struct vnode *dvp, struct vnode **vpp, |
| 1041 | struct componentname *cnp, int do_direnter) |
| 1042 | { |
| 1043 | struct inode *ip, *pdir; |
| 1044 | struct vnode *tvp; |
| 1045 | int error; |
| 1046 | struct ufs_lookup_results *ulr; |
| 1047 | |
| 1048 | pdir = VTOI(dvp); |
| 1049 | |
| 1050 | /* XXX should handle this material another way */ |
| 1051 | ulr = &pdir->i_crap; |
| 1052 | UFS_CHECK_CRAPCOUNTER(pdir); |
| 1053 | |
| 1054 | *vpp = NULL; |
| 1055 | if ((mode & IFMT) == 0) |
| 1056 | mode |= IFREG; |
| 1057 | |
| 1058 | if ((error = ext2fs_valloc(dvp, mode, cnp->cn_cred, &tvp)) != 0) { |
| 1059 | return error; |
| 1060 | } |
| 1061 | ip = VTOI(tvp); |
| 1062 | ip->i_uid = kauth_cred_geteuid(cnp->cn_cred); |
| 1063 | ip->i_e2fs_uid = ip->i_uid & 0xffff; |
| 1064 | ip->i_e2fs_gid = pdir->i_e2fs_gid; |
| 1065 | if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0) { |
| 1066 | ip->i_e2fs_uid_high = (ip->i_uid >> 16) & 0xffff; |
| 1067 | ip->i_e2fs_gid_high = pdir->i_e2fs_gid_high; |
| 1068 | } else { |
| 1069 | ip->i_e2fs_uid_high = 0; |
| 1070 | ip->i_e2fs_gid_high = 0; |
| 1071 | } |
| 1072 | ip->i_gid = ip->i_e2fs_gid | (ip->i_e2fs_gid_high << 16); |
| 1073 | ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; |
| 1074 | ip->i_e2fs_mode = mode; |
| 1075 | tvp->v_type = IFTOVT(mode); /* Rest init'd in getnewvnode(). */ |
| 1076 | ip->i_e2fs_nlink = 1; |
| 1077 | |
| 1078 | /* Authorize setting SGID if needed. */ |
| 1079 | if (ip->i_e2fs_mode & ISGID) { |
| 1080 | error = kauth_authorize_vnode(cnp->cn_cred, KAUTH_VNODE_WRITE_SECURITY, |
| 1081 | tvp, NULL, genfs_can_chmod(tvp->v_type, cnp->cn_cred, ip->i_uid, |
| 1082 | ip->i_gid, mode)); |
| 1083 | if (error) |
| 1084 | ip->i_e2fs_mode &= ~ISGID; |
| 1085 | } |
| 1086 | |
| 1087 | /* Initialize extra_isize according to what is set in superblock */ |
| 1088 | if (EXT2F_HAS_ROCOMPAT_FEATURE(ip->i_e2fs, EXT2F_ROCOMPAT_EXTRA_ISIZE) |
| 1089 | && EXT2_DINODE_SIZE(ip->i_e2fs) > EXT2_REV0_DINODE_SIZE) { |
| 1090 | ip->i_din.e2fs_din->e2di_extra_isize = ip->i_e2fs->e2fs.e4fs_want_extra_isize; |
| 1091 | } |
| 1092 | |
| 1093 | /* Set create time if possible */ |
| 1094 | if (EXT2_DINODE_FITS(ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs))) { |
| 1095 | struct timespec now; |
| 1096 | vfs_timestamp(&now); |
| 1097 | EXT2_DINODE_TIME_SET(&now, ip->i_din.e2fs_din, e2di_crtime, EXT2_DINODE_SIZE(ip->i_e2fs)); |
| 1098 | } |
| 1099 | |
| 1100 | if (do_direnter) { |
| 1101 | /* |
| 1102 | * Make sure inode goes to disk before directory entry. |
| 1103 | */ |
| 1104 | if ((error = ext2fs_update(tvp, NULL, NULL, UPDATE_WAIT)) != 0) |
| 1105 | goto bad; |
| 1106 | error = ext2fs_direnter(ip, dvp, ulr, cnp); |
| 1107 | if (error != 0) |
| 1108 | goto bad; |
| 1109 | } |
| 1110 | |
| 1111 | *vpp = tvp; |
| 1112 | return 0; |
| 1113 | |
| 1114 | bad: |
| 1115 | /* |
| 1116 | * Write error occurred trying to update the inode |
| 1117 | * or the directory so must deallocate the inode. |
| 1118 | */ |
| 1119 | tvp->v_type = VNON; /* Stop explosion if VBLK */ |
| 1120 | ip->i_e2fs_nlink = 0; |
| 1121 | ip->i_flag |= IN_CHANGE; |
| 1122 | vput(tvp); |
| 1123 | return error; |
| 1124 | } |
| 1125 | |
| 1126 | /* |
| 1127 | * Reclaim an inode so that it can be used for other purposes. |
| 1128 | */ |
| 1129 | int |
| 1130 | ext2fs_reclaim(void *v) |
| 1131 | { |
| 1132 | struct vop_reclaim_args /* { |
| 1133 | struct vnode *a_vp; |
| 1134 | } */ *ap = v; |
| 1135 | struct vnode *vp = ap->a_vp; |
| 1136 | struct inode *ip = VTOI(vp); |
| 1137 | int error; |
| 1138 | |
| 1139 | /* |
| 1140 | * The inode must be freed and updated before being removed |
| 1141 | * from its hash chain. Other threads trying to gain a hold |
| 1142 | * or lock on the inode will be stalled. |
| 1143 | */ |
| 1144 | if (ip->i_omode == 1 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) |
| 1145 | ext2fs_vfree(vp, ip->i_number, ip->i_e2fs_mode); |
| 1146 | if ((error = ufs_reclaim(vp)) != 0) |
| 1147 | return error; |
| 1148 | if (ip->i_din.e2fs_din != NULL) |
| 1149 | kmem_free(ip->i_din.e2fs_din, EXT2_DINODE_SIZE(ip->i_e2fs)); |
| 1150 | genfs_node_destroy(vp); |
| 1151 | pool_put(&ext2fs_inode_pool, vp->v_data); |
| 1152 | vp->v_data = NULL; |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
| 1156 | /* Global vfs data structures for ext2fs. */ |
| 1157 | int (**ext2fs_vnodeop_p)(void *); |
| 1158 | const struct vnodeopv_entry_desc ext2fs_vnodeop_entries[] = { |
| 1159 | { &vop_default_desc, vn_default_error }, |
| 1160 | { &vop_lookup_desc, ext2fs_lookup }, /* lookup */ |
| 1161 | { &vop_create_desc, ext2fs_create }, /* create */ |
| 1162 | { &vop_mknod_desc, ext2fs_mknod }, /* mknod */ |
| 1163 | { &vop_open_desc, ext2fs_open }, /* open */ |
| 1164 | { &vop_close_desc, ufs_close }, /* close */ |
| 1165 | { &vop_access_desc, ext2fs_access }, /* access */ |
| 1166 | { &vop_getattr_desc, ext2fs_getattr }, /* getattr */ |
| 1167 | { &vop_setattr_desc, ext2fs_setattr }, /* setattr */ |
| 1168 | { &vop_read_desc, ext2fs_read }, /* read */ |
| 1169 | { &vop_write_desc, ext2fs_write }, /* write */ |
| 1170 | { &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */ |
| 1171 | { &vop_fdiscard_desc, genfs_eopnotsupp }, /* fdiscard */ |
| 1172 | { &vop_ioctl_desc, ufs_ioctl }, /* ioctl */ |
| 1173 | { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */ |
| 1174 | { &vop_poll_desc, ufs_poll }, /* poll */ |
| 1175 | { &vop_kqfilter_desc, genfs_kqfilter }, /* kqfilter */ |
| 1176 | { &vop_revoke_desc, ufs_revoke }, /* revoke */ |
| 1177 | { &vop_mmap_desc, ufs_mmap }, /* mmap */ |
| 1178 | { &vop_fsync_desc, ext2fs_fsync }, /* fsync */ |
| 1179 | { &vop_seek_desc, ufs_seek }, /* seek */ |
| 1180 | { &vop_remove_desc, ext2fs_remove }, /* remove */ |
| 1181 | { &vop_link_desc, ext2fs_link }, /* link */ |
| 1182 | { &vop_rename_desc, ext2fs_rename }, /* rename */ |
| 1183 | { &vop_mkdir_desc, ext2fs_mkdir }, /* mkdir */ |
| 1184 | { &vop_rmdir_desc, ext2fs_rmdir }, /* rmdir */ |
| 1185 | { &vop_symlink_desc, ext2fs_symlink }, /* symlink */ |
| 1186 | { &vop_readdir_desc, ext2fs_readdir }, /* readdir */ |
| 1187 | { &vop_readlink_desc, ext2fs_readlink }, /* readlink */ |
| 1188 | { &vop_abortop_desc, ufs_abortop }, /* abortop */ |
| 1189 | { &vop_inactive_desc, ext2fs_inactive }, /* inactive */ |
| 1190 | { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */ |
| 1191 | { &vop_lock_desc, ufs_lock }, /* lock */ |
| 1192 | { &vop_unlock_desc, ufs_unlock }, /* unlock */ |
| 1193 | { &vop_bmap_desc, ext2fs_bmap }, /* bmap */ |
| 1194 | { &vop_strategy_desc, ufs_strategy }, /* strategy */ |
| 1195 | { &vop_print_desc, ufs_print }, /* print */ |
| 1196 | { &vop_islocked_desc, ufs_islocked }, /* islocked */ |
| 1197 | { &vop_pathconf_desc, ufs_pathconf }, /* pathconf */ |
| 1198 | { &vop_advlock_desc, ext2fs_advlock }, /* advlock */ |
| 1199 | { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ |
| 1200 | { &vop_getpages_desc, genfs_getpages }, /* getpages */ |
| 1201 | { &vop_putpages_desc, genfs_putpages }, /* putpages */ |
| 1202 | { &vop_getextattr_desc, ext2fs_getextattr }, /* getextattr */ |
| 1203 | { &vop_setextattr_desc, ext2fs_setextattr }, /* setextattr */ |
| 1204 | { &vop_listextattr_desc, ext2fs_listextattr }, /* listextattr */ |
| 1205 | { &vop_deleteextattr_desc, ext2fs_deleteextattr },/* deleteextattr */ |
| 1206 | { NULL, NULL } |
| 1207 | }; |
| 1208 | const struct vnodeopv_desc ext2fs_vnodeop_opv_desc = |
| 1209 | { &ext2fs_vnodeop_p, ext2fs_vnodeop_entries }; |
| 1210 | |
| 1211 | int (**ext2fs_specop_p)(void *); |
| 1212 | const struct vnodeopv_entry_desc ext2fs_specop_entries[] = { |
| 1213 | { &vop_default_desc, vn_default_error }, |
| 1214 | { &vop_lookup_desc, spec_lookup }, /* lookup */ |
| 1215 | { &vop_create_desc, spec_create }, /* create */ |
| 1216 | { &vop_mknod_desc, spec_mknod }, /* mknod */ |
| 1217 | { &vop_open_desc, spec_open }, /* open */ |
| 1218 | { &vop_close_desc, ufsspec_close }, /* close */ |
| 1219 | { &vop_access_desc, ext2fs_access }, /* access */ |
| 1220 | { &vop_getattr_desc, ext2fs_getattr }, /* getattr */ |
| 1221 | { &vop_setattr_desc, ext2fs_setattr }, /* setattr */ |
| 1222 | { &vop_read_desc, ufsspec_read }, /* read */ |
| 1223 | { &vop_write_desc, ufsspec_write }, /* write */ |
| 1224 | { &vop_fallocate_desc, spec_fallocate }, /* fallocate */ |
| 1225 | { &vop_fdiscard_desc, spec_fdiscard }, /* fdiscard */ |
| 1226 | { &vop_ioctl_desc, spec_ioctl }, /* ioctl */ |
| 1227 | { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */ |
| 1228 | { &vop_poll_desc, spec_poll }, /* poll */ |
| 1229 | { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */ |
| 1230 | { &vop_revoke_desc, spec_revoke }, /* revoke */ |
| 1231 | { &vop_mmap_desc, spec_mmap }, /* mmap */ |
| 1232 | { &vop_fsync_desc, ext2fs_fsync }, /* fsync */ |
| 1233 | { &vop_seek_desc, spec_seek }, /* seek */ |
| 1234 | { &vop_remove_desc, spec_remove }, /* remove */ |
| 1235 | { &vop_link_desc, spec_link }, /* link */ |
| 1236 | { &vop_rename_desc, spec_rename }, /* rename */ |
| 1237 | { &vop_mkdir_desc, spec_mkdir }, /* mkdir */ |
| 1238 | { &vop_rmdir_desc, spec_rmdir }, /* rmdir */ |
| 1239 | { &vop_symlink_desc, spec_symlink }, /* symlink */ |
| 1240 | { &vop_readdir_desc, spec_readdir }, /* readdir */ |
| 1241 | { &vop_readlink_desc, spec_readlink }, /* readlink */ |
| 1242 | { &vop_abortop_desc, spec_abortop }, /* abortop */ |
| 1243 | { &vop_inactive_desc, ext2fs_inactive }, /* inactive */ |
| 1244 | { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */ |
| 1245 | { &vop_lock_desc, ufs_lock }, /* lock */ |
| 1246 | { &vop_unlock_desc, ufs_unlock }, /* unlock */ |
| 1247 | { &vop_bmap_desc, spec_bmap }, /* bmap */ |
| 1248 | { &vop_strategy_desc, spec_strategy }, /* strategy */ |
| 1249 | { &vop_print_desc, ufs_print }, /* print */ |
| 1250 | { &vop_islocked_desc, ufs_islocked }, /* islocked */ |
| 1251 | { &vop_pathconf_desc, spec_pathconf }, /* pathconf */ |
| 1252 | { &vop_advlock_desc, spec_advlock }, /* advlock */ |
| 1253 | { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ |
| 1254 | { &vop_getpages_desc, spec_getpages }, /* getpages */ |
| 1255 | { &vop_putpages_desc, spec_putpages }, /* putpages */ |
| 1256 | { &vop_getextattr_desc, ext2fs_getextattr }, /* getextattr */ |
| 1257 | { &vop_setextattr_desc, ext2fs_setextattr }, /* setextattr */ |
| 1258 | { &vop_listextattr_desc, ext2fs_listextattr }, /* listextattr */ |
| 1259 | { &vop_deleteextattr_desc, ext2fs_deleteextattr },/* deleteextattr */ |
| 1260 | { NULL, NULL } |
| 1261 | }; |
| 1262 | const struct vnodeopv_desc ext2fs_specop_opv_desc = |
| 1263 | { &ext2fs_specop_p, ext2fs_specop_entries }; |
| 1264 | |
| 1265 | int (**ext2fs_fifoop_p)(void *); |
| 1266 | const struct vnodeopv_entry_desc ext2fs_fifoop_entries[] = { |
| 1267 | { &vop_default_desc, vn_default_error }, |
| 1268 | { &vop_lookup_desc, vn_fifo_bypass }, /* lookup */ |
| 1269 | { &vop_create_desc, vn_fifo_bypass }, /* create */ |
| 1270 | { &vop_mknod_desc, vn_fifo_bypass }, /* mknod */ |
| 1271 | { &vop_open_desc, vn_fifo_bypass }, /* open */ |
| 1272 | { &vop_close_desc, ufsfifo_close }, /* close */ |
| 1273 | { &vop_access_desc, ext2fs_access }, /* access */ |
| 1274 | { &vop_getattr_desc, ext2fs_getattr }, /* getattr */ |
| 1275 | { &vop_setattr_desc, ext2fs_setattr }, /* setattr */ |
| 1276 | { &vop_read_desc, ufsfifo_read }, /* read */ |
| 1277 | { &vop_write_desc, ufsfifo_write }, /* write */ |
| 1278 | { &vop_fallocate_desc, vn_fifo_bypass }, /* fallocate */ |
| 1279 | { &vop_fdiscard_desc, vn_fifo_bypass }, /* fdiscard */ |
| 1280 | { &vop_ioctl_desc, vn_fifo_bypass }, /* ioctl */ |
| 1281 | { &vop_fcntl_desc, ufs_fcntl }, /* fcntl */ |
| 1282 | { &vop_poll_desc, vn_fifo_bypass }, /* poll */ |
| 1283 | { &vop_kqfilter_desc, vn_fifo_bypass }, /* kqfilter */ |
| 1284 | { &vop_revoke_desc, vn_fifo_bypass }, /* revoke */ |
| 1285 | { &vop_mmap_desc, vn_fifo_bypass }, /* mmap */ |
| 1286 | { &vop_fsync_desc, ext2fs_fsync }, /* fsync */ |
| 1287 | { &vop_seek_desc, vn_fifo_bypass }, /* seek */ |
| 1288 | { &vop_remove_desc, vn_fifo_bypass }, /* remove */ |
| 1289 | { &vop_link_desc, vn_fifo_bypass }, /* link */ |
| 1290 | { &vop_rename_desc, vn_fifo_bypass }, /* rename */ |
| 1291 | { &vop_mkdir_desc, vn_fifo_bypass }, /* mkdir */ |
| 1292 | { &vop_rmdir_desc, vn_fifo_bypass }, /* rmdir */ |
| 1293 | { &vop_symlink_desc, vn_fifo_bypass }, /* symlink */ |
| 1294 | { &vop_readdir_desc, vn_fifo_bypass }, /* readdir */ |
| 1295 | { &vop_readlink_desc, vn_fifo_bypass }, /* readlink */ |
| 1296 | { &vop_abortop_desc, vn_fifo_bypass }, /* abortop */ |
| 1297 | { &vop_inactive_desc, ext2fs_inactive }, /* inactive */ |
| 1298 | { &vop_reclaim_desc, ext2fs_reclaim }, /* reclaim */ |
| 1299 | { &vop_lock_desc, ufs_lock }, /* lock */ |
| 1300 | { &vop_unlock_desc, ufs_unlock }, /* unlock */ |
| 1301 | { &vop_bmap_desc, vn_fifo_bypass }, /* bmap */ |
| 1302 | { &vop_strategy_desc, vn_fifo_bypass }, /* strategy */ |
| 1303 | { &vop_print_desc, ufs_print }, /* print */ |
| 1304 | { &vop_islocked_desc, ufs_islocked }, /* islocked */ |
| 1305 | { &vop_pathconf_desc, vn_fifo_bypass }, /* pathconf */ |
| 1306 | { &vop_advlock_desc, vn_fifo_bypass }, /* advlock */ |
| 1307 | { &vop_bwrite_desc, vn_bwrite }, /* bwrite */ |
| 1308 | { &vop_putpages_desc, vn_fifo_bypass }, /* putpages */ |
| 1309 | { &vop_getextattr_desc, ext2fs_getextattr }, /* getextattr */ |
| 1310 | { &vop_setextattr_desc, ext2fs_setextattr }, /* setextattr */ |
| 1311 | { &vop_listextattr_desc, ext2fs_listextattr }, /* listextattr */ |
| 1312 | { &vop_deleteextattr_desc, ext2fs_deleteextattr },/* deleteextattr */ |
| 1313 | { NULL, NULL } |
| 1314 | }; |
| 1315 | const struct vnodeopv_desc ext2fs_fifoop_opv_desc = |
| 1316 | { &ext2fs_fifoop_p, ext2fs_fifoop_entries }; |
| 1317 | |