| 1 | /* |
| 2 | * Copyright (c) 1982, 1986, 1989 Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms are permitted |
| 6 | * provided that the above copyright notice and this paragraph are |
| 7 | * duplicated in all such forms and that any documentation, |
| 8 | * advertising materials, and other materials related to such |
| 9 | * distribution and use acknowledge that the software was developed |
| 10 | * by the University of California, Berkeley. The name of the |
| 11 | * University may not be used to endorse or promote products derived |
| 12 | * from this software without specific prior written permission. |
| 13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 16 | * |
| 17 | * @(#)ffs_vnops.c 7.19 (Berkeley) %G% |
| 18 | */ |
| 19 | |
| 20 | #include "param.h" |
| 21 | #include "systm.h" |
| 22 | #include "user.h" |
| 23 | #include "kernel.h" |
| 24 | #include "file.h" |
| 25 | #include "stat.h" |
| 26 | #include "buf.h" |
| 27 | #include "proc.h" |
| 28 | #include "uio.h" |
| 29 | #include "socket.h" |
| 30 | #include "socketvar.h" |
| 31 | #include "conf.h" |
| 32 | #include "mount.h" |
| 33 | #include "vnode.h" |
| 34 | #include "../ufs/inode.h" |
| 35 | #include "../ufs/fs.h" |
| 36 | #include "../ufs/quota.h" |
| 37 | |
| 38 | /* |
| 39 | * Global vfs data structures for ufs |
| 40 | */ |
| 41 | |
| 42 | int ufs_lookup(), |
| 43 | ufs_create(), |
| 44 | ufs_mknod(), |
| 45 | ufs_open(), |
| 46 | ufs_close(), |
| 47 | ufs_access(), |
| 48 | ufs_getattr(), |
| 49 | ufs_setattr(), |
| 50 | ufs_read(), |
| 51 | ufs_write(), |
| 52 | ufs_ioctl(), |
| 53 | ufs_select(), |
| 54 | ufs_mmap(), |
| 55 | ufs_fsync(), |
| 56 | ufs_seek(), |
| 57 | ufs_remove(), |
| 58 | ufs_link(), |
| 59 | ufs_rename(), |
| 60 | ufs_mkdir(), |
| 61 | ufs_rmdir(), |
| 62 | ufs_symlink(), |
| 63 | ufs_readdir(), |
| 64 | ufs_readlink(), |
| 65 | ufs_abortop(), |
| 66 | ufs_inactive(), |
| 67 | ufs_reclaim(), |
| 68 | ufs_lock(), |
| 69 | ufs_unlock(), |
| 70 | ufs_bmap(), |
| 71 | ufs_strategy(); |
| 72 | |
| 73 | struct vnodeops ufs_vnodeops = { |
| 74 | ufs_lookup, |
| 75 | ufs_create, |
| 76 | ufs_mknod, |
| 77 | ufs_open, |
| 78 | ufs_close, |
| 79 | ufs_access, |
| 80 | ufs_getattr, |
| 81 | ufs_setattr, |
| 82 | ufs_read, |
| 83 | ufs_write, |
| 84 | ufs_ioctl, |
| 85 | ufs_select, |
| 86 | ufs_mmap, |
| 87 | ufs_fsync, |
| 88 | ufs_seek, |
| 89 | ufs_remove, |
| 90 | ufs_link, |
| 91 | ufs_rename, |
| 92 | ufs_mkdir, |
| 93 | ufs_rmdir, |
| 94 | ufs_symlink, |
| 95 | ufs_readdir, |
| 96 | ufs_readlink, |
| 97 | ufs_abortop, |
| 98 | ufs_inactive, |
| 99 | ufs_reclaim, |
| 100 | ufs_lock, |
| 101 | ufs_unlock, |
| 102 | ufs_bmap, |
| 103 | ufs_strategy, |
| 104 | }; |
| 105 | |
| 106 | enum vtype iftovt_tab[8] = { |
| 107 | VNON, VCHR, VDIR, VBLK, VREG, VLNK, VSOCK, VBAD, |
| 108 | }; |
| 109 | int vttoif_tab[8] = { |
| 110 | 0, IFREG, IFDIR, IFBLK, IFCHR, IFLNK, IFSOCK, IFMT, |
| 111 | }; |
| 112 | |
| 113 | /* |
| 114 | * Create a regular file |
| 115 | */ |
| 116 | ufs_create(ndp, vap) |
| 117 | struct nameidata *ndp; |
| 118 | struct vattr *vap; |
| 119 | { |
| 120 | struct inode *ip; |
| 121 | int error; |
| 122 | |
| 123 | if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) |
| 124 | return (error); |
| 125 | ndp->ni_vp = ITOV(ip); |
| 126 | return (0); |
| 127 | } |
| 128 | |
| 129 | /* |
| 130 | * Mknod vnode call |
| 131 | */ |
| 132 | /* ARGSUSED */ |
| 133 | ufs_mknod(ndp, vap, cred) |
| 134 | struct nameidata *ndp; |
| 135 | struct ucred *cred; |
| 136 | struct vattr *vap; |
| 137 | { |
| 138 | struct inode *ip; |
| 139 | int error; |
| 140 | |
| 141 | if (error = maknode(MAKEIMODE(vap->va_type, vap->va_mode), ndp, &ip)) |
| 142 | return (error); |
| 143 | if (vap->va_rdev) { |
| 144 | /* |
| 145 | * Want to be able to use this to make badblock |
| 146 | * inodes, so don't truncate the dev number. |
| 147 | */ |
| 148 | ITOV(ip)->v_rdev = ip->i_rdev = vap->va_rdev; |
| 149 | ip->i_flag |= IACC|IUPD|ICHG; |
| 150 | } |
| 151 | /* |
| 152 | * Remove inode so that it will be reloaded by iget and |
| 153 | * checked to see if it is an alias of an existing entry |
| 154 | * in the inode cache. |
| 155 | */ |
| 156 | remque(ip); |
| 157 | ip->i_forw = ip; |
| 158 | ip->i_back = ip; |
| 159 | ITOV(ip)->v_type = VNON; |
| 160 | iput(ip); |
| 161 | return (0); |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Open called. |
| 166 | * |
| 167 | * Nothing to do. |
| 168 | */ |
| 169 | /* ARGSUSED */ |
| 170 | ufs_open(vp, mode, cred) |
| 171 | struct vnode *vp; |
| 172 | int mode; |
| 173 | struct ucred *cred; |
| 174 | { |
| 175 | |
| 176 | return (0); |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Close called |
| 181 | * |
| 182 | * Update the times on the inode. |
| 183 | */ |
| 184 | /* ARGSUSED */ |
| 185 | ufs_close(vp, fflag, cred) |
| 186 | struct vnode *vp; |
| 187 | int fflag; |
| 188 | struct ucred *cred; |
| 189 | { |
| 190 | register struct inode *ip = VTOI(vp); |
| 191 | |
| 192 | if (vp->v_count > 1 && !(ip->i_flag & ILOCKED)) |
| 193 | ITIMES(ip, &time, &time); |
| 194 | return (0); |
| 195 | } |
| 196 | |
| 197 | ufs_access(vp, mode, cred) |
| 198 | struct vnode *vp; |
| 199 | int mode; |
| 200 | struct ucred *cred; |
| 201 | { |
| 202 | |
| 203 | return (iaccess(VTOI(vp), mode, cred)); |
| 204 | } |
| 205 | |
| 206 | /* ARGSUSED */ |
| 207 | ufs_getattr(vp, vap, cred) |
| 208 | struct vnode *vp; |
| 209 | register struct vattr *vap; |
| 210 | struct ucred *cred; |
| 211 | { |
| 212 | register struct inode *ip = VTOI(vp); |
| 213 | |
| 214 | ITIMES(ip, &time, &time); |
| 215 | /* |
| 216 | * Copy from inode table |
| 217 | */ |
| 218 | vap->va_fsid = ip->i_dev; |
| 219 | vap->va_fileid = ip->i_number; |
| 220 | vap->va_mode = ip->i_mode & ~IFMT; |
| 221 | vap->va_nlink = ip->i_nlink; |
| 222 | vap->va_uid = ip->i_uid; |
| 223 | vap->va_gid = ip->i_gid; |
| 224 | vap->va_rdev = (dev_t)ip->i_rdev; |
| 225 | vap->va_size = ip->i_din.di_qsize.val[0]; |
| 226 | vap->va_size1 = ip->i_din.di_qsize.val[1]; |
| 227 | vap->va_atime.tv_sec = ip->i_atime; |
| 228 | vap->va_atime.tv_usec = 0; |
| 229 | vap->va_mtime.tv_sec = ip->i_mtime; |
| 230 | vap->va_mtime.tv_usec = 0; |
| 231 | vap->va_ctime.tv_sec = ip->i_ctime; |
| 232 | vap->va_ctime.tv_usec = 0; |
| 233 | vap->va_flags = ip->i_flags; |
| 234 | vap->va_gen = ip->i_gen; |
| 235 | /* this doesn't belong here */ |
| 236 | if (vp->v_type == VBLK) |
| 237 | vap->va_blocksize = BLKDEV_IOSIZE; |
| 238 | else if (vp->v_type == VCHR) |
| 239 | vap->va_blocksize = MAXBSIZE; |
| 240 | else |
| 241 | vap->va_blocksize = ip->i_fs->fs_bsize; |
| 242 | vap->va_bytes = dbtob(ip->i_blocks); |
| 243 | vap->va_bytes1 = -1; |
| 244 | vap->va_type = vp->v_type; |
| 245 | return (0); |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Set attribute vnode op. called from several syscalls |
| 250 | */ |
| 251 | ufs_setattr(vp, vap, cred) |
| 252 | register struct vnode *vp; |
| 253 | register struct vattr *vap; |
| 254 | register struct ucred *cred; |
| 255 | { |
| 256 | register struct inode *ip = VTOI(vp); |
| 257 | int error = 0; |
| 258 | |
| 259 | /* |
| 260 | * Check for unsetable attributes. |
| 261 | */ |
| 262 | if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) || |
| 263 | (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) || |
| 264 | (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) || |
| 265 | ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) { |
| 266 | return (EINVAL); |
| 267 | } |
| 268 | /* |
| 269 | * Go through the fields and update iff not VNOVAL. |
| 270 | */ |
| 271 | if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL) |
| 272 | if (error = chown1(vp, vap->va_uid, vap->va_gid, cred)) |
| 273 | return (error); |
| 274 | if (vap->va_size != VNOVAL) { |
| 275 | if (vp->v_type == VDIR) |
| 276 | return (EISDIR); |
| 277 | if (error = itrunc(ip, vap->va_size)) |
| 278 | return (error); |
| 279 | } |
| 280 | if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) { |
| 281 | if (cred->cr_uid != ip->i_uid && |
| 282 | (error = suser(cred, &u.u_acflag))) |
| 283 | return (error); |
| 284 | if (vap->va_atime.tv_sec != VNOVAL) |
| 285 | ip->i_flag |= IACC; |
| 286 | if (vap->va_mtime.tv_sec != VNOVAL) |
| 287 | ip->i_flag |= IUPD; |
| 288 | ip->i_flag |= ICHG; |
| 289 | if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1)) |
| 290 | return (error); |
| 291 | } |
| 292 | if (vap->va_mode != (u_short)VNOVAL) |
| 293 | error = chmod1(vp, (int)vap->va_mode, cred); |
| 294 | if (vap->va_flags != VNOVAL) { |
| 295 | if (cred->cr_uid != ip->i_uid && |
| 296 | (error = suser(cred, &u.u_acflag))) |
| 297 | return (error); |
| 298 | if (cred->cr_uid == 0) { |
| 299 | ip->i_flags = vap->va_flags; |
| 300 | } else { |
| 301 | ip->i_flags &= 0xffff0000; |
| 302 | ip->i_flags |= (vap->va_flags & 0xffff); |
| 303 | } |
| 304 | ip->i_flag |= ICHG; |
| 305 | } |
| 306 | return (error); |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Change the mode on a file. |
| 311 | * Inode must be locked before calling. |
| 312 | */ |
| 313 | chmod1(vp, mode, cred) |
| 314 | register struct vnode *vp; |
| 315 | register int mode; |
| 316 | struct ucred *cred; |
| 317 | { |
| 318 | register struct inode *ip = VTOI(vp); |
| 319 | int error; |
| 320 | |
| 321 | if (cred->cr_uid != ip->i_uid && |
| 322 | (error = suser(cred, &u.u_acflag))) |
| 323 | return (error); |
| 324 | ip->i_mode &= ~07777; |
| 325 | if (cred->cr_uid) { |
| 326 | if (vp->v_type != VDIR) |
| 327 | mode &= ~ISVTX; |
| 328 | if (!groupmember(ip->i_gid, cred)) |
| 329 | mode &= ~ISGID; |
| 330 | } |
| 331 | ip->i_mode |= mode & 07777; |
| 332 | ip->i_flag |= ICHG; |
| 333 | if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0) |
| 334 | xrele(vp); |
| 335 | return (0); |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Perform chown operation on inode ip; |
| 340 | * inode must be locked prior to call. |
| 341 | */ |
| 342 | chown1(vp, uid, gid, cred) |
| 343 | register struct vnode *vp; |
| 344 | uid_t uid; |
| 345 | gid_t gid; |
| 346 | struct ucred *cred; |
| 347 | { |
| 348 | register struct inode *ip = VTOI(vp); |
| 349 | #ifdef QUOTA |
| 350 | register long change; |
| 351 | #endif |
| 352 | int error; |
| 353 | |
| 354 | if (uid == (u_short)VNOVAL) |
| 355 | uid = ip->i_uid; |
| 356 | if (gid == (u_short)VNOVAL) |
| 357 | gid = ip->i_gid; |
| 358 | /* |
| 359 | * If we don't own the file, are trying to change the owner |
| 360 | * of the file, or are not a member of the target group, |
| 361 | * the caller must be superuser or the call fails. |
| 362 | */ |
| 363 | if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid || |
| 364 | !groupmember((gid_t)gid, cred)) && |
| 365 | (error = suser(cred, &u.u_acflag))) |
| 366 | return (error); |
| 367 | #ifdef QUOTA |
| 368 | if (ip->i_uid == uid) /* this just speeds things a little */ |
| 369 | change = 0; |
| 370 | else |
| 371 | change = ip->i_blocks; |
| 372 | (void) chkdq(ip, -change, 1); |
| 373 | (void) chkiq(ip->i_dev, ip, ip->i_uid, 1); |
| 374 | dqrele(ip->i_dquot); |
| 375 | #endif |
| 376 | if (ip->i_uid != uid && cred->cr_uid != 0) |
| 377 | ip->i_mode &= ~ISUID; |
| 378 | if (ip->i_gid != gid && cred->cr_uid != 0) |
| 379 | ip->i_mode &= ~ISGID; |
| 380 | ip->i_uid = uid; |
| 381 | ip->i_gid = gid; |
| 382 | ip->i_flag |= ICHG; |
| 383 | #ifdef QUOTA |
| 384 | ip->i_dquot = inoquota(ip); |
| 385 | (void) chkdq(ip, change, 1); |
| 386 | (void) chkiq(ip->i_dev, (struct inode *)NULL, (uid_t)uid, 1); |
| 387 | return (u.u_error); /* should == 0 ALWAYS !! */ |
| 388 | #else |
| 389 | return (0); |
| 390 | #endif |
| 391 | } |
| 392 | |
| 393 | /* ARGSUSED */ |
| 394 | ufs_ioctl(vp, com, data, fflag, cred) |
| 395 | struct vnode *vp; |
| 396 | int com; |
| 397 | caddr_t data; |
| 398 | int fflag; |
| 399 | struct ucred *cred; |
| 400 | { |
| 401 | |
| 402 | printf("ufs_ioctl called with type %d\n", vp->v_type); |
| 403 | return (ENOTTY); |
| 404 | } |
| 405 | |
| 406 | /* ARGSUSED */ |
| 407 | ufs_select(vp, which, cred) |
| 408 | struct vnode *vp; |
| 409 | int which; |
| 410 | struct ucred *cred; |
| 411 | { |
| 412 | |
| 413 | printf("ufs_select called with type %d\n", vp->v_type); |
| 414 | return (1); /* XXX */ |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Mmap a file |
| 419 | * |
| 420 | * NB Currently unsupported. |
| 421 | */ |
| 422 | /* ARGSUSED */ |
| 423 | ufs_mmap(vp, fflags, cred) |
| 424 | struct vnode *vp; |
| 425 | int fflags; |
| 426 | struct ucred *cred; |
| 427 | { |
| 428 | |
| 429 | return (EINVAL); |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * Synch an open file. |
| 434 | */ |
| 435 | /* ARGSUSED */ |
| 436 | ufs_fsync(vp, fflags, cred) |
| 437 | struct vnode *vp; |
| 438 | int fflags; |
| 439 | struct ucred *cred; |
| 440 | { |
| 441 | register struct inode *ip = VTOI(vp); |
| 442 | int error; |
| 443 | |
| 444 | ILOCK(ip); |
| 445 | if (fflags&FWRITE) |
| 446 | ip->i_flag |= ICHG; |
| 447 | error = syncip(ip); |
| 448 | IUNLOCK(ip); |
| 449 | return (error); |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Seek on a file |
| 454 | * |
| 455 | * Nothing to do, so just return. |
| 456 | */ |
| 457 | /* ARGSUSED */ |
| 458 | ufs_seek(vp, oldoff, newoff, cred) |
| 459 | struct vnode *vp; |
| 460 | off_t oldoff, newoff; |
| 461 | struct ucred *cred; |
| 462 | { |
| 463 | |
| 464 | return (0); |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * ufs remove |
| 469 | * Hard to avoid races here, especially |
| 470 | * in unlinking directories. |
| 471 | */ |
| 472 | ufs_remove(ndp) |
| 473 | struct nameidata *ndp; |
| 474 | { |
| 475 | register struct inode *ip, *dp; |
| 476 | int error; |
| 477 | |
| 478 | ip = VTOI(ndp->ni_vp); |
| 479 | dp = VTOI(ndp->ni_dvp); |
| 480 | error = dirremove(ndp); |
| 481 | if (!error) { |
| 482 | ip->i_nlink--; |
| 483 | ip->i_flag |= ICHG; |
| 484 | } |
| 485 | if (dp == ip) |
| 486 | vrele(ITOV(ip)); |
| 487 | else |
| 488 | iput(ip); |
| 489 | iput(dp); |
| 490 | return (error); |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * link vnode call |
| 495 | */ |
| 496 | ufs_link(vp, ndp) |
| 497 | register struct vnode *vp; |
| 498 | register struct nameidata *ndp; |
| 499 | { |
| 500 | register struct inode *ip = VTOI(vp); |
| 501 | int error; |
| 502 | |
| 503 | if (ndp->ni_dvp != vp) |
| 504 | ILOCK(ip); |
| 505 | if (ip->i_nlink == LINK_MAX - 1) { |
| 506 | error = EMLINK; |
| 507 | goto out; |
| 508 | } |
| 509 | ip->i_nlink++; |
| 510 | ip->i_flag |= ICHG; |
| 511 | error = iupdat(ip, &time, &time, 1); |
| 512 | if (!error) |
| 513 | error = direnter(ip, ndp); |
| 514 | out: |
| 515 | if (ndp->ni_dvp != vp) |
| 516 | IUNLOCK(ip); |
| 517 | if (error) { |
| 518 | ip->i_nlink--; |
| 519 | ip->i_flag |= ICHG; |
| 520 | } |
| 521 | return (error); |
| 522 | } |
| 523 | |
| 524 | /* |
| 525 | * Rename system call. |
| 526 | * rename("foo", "bar"); |
| 527 | * is essentially |
| 528 | * unlink("bar"); |
| 529 | * link("foo", "bar"); |
| 530 | * unlink("foo"); |
| 531 | * but ``atomically''. Can't do full commit without saving state in the |
| 532 | * inode on disk which isn't feasible at this time. Best we can do is |
| 533 | * always guarantee the target exists. |
| 534 | * |
| 535 | * Basic algorithm is: |
| 536 | * |
| 537 | * 1) Bump link count on source while we're linking it to the |
| 538 | * target. This also ensure the inode won't be deleted out |
| 539 | * from underneath us while we work (it may be truncated by |
| 540 | * a concurrent `trunc' or `open' for creation). |
| 541 | * 2) Link source to destination. If destination already exists, |
| 542 | * delete it first. |
| 543 | * 3) Unlink source reference to inode if still around. If a |
| 544 | * directory was moved and the parent of the destination |
| 545 | * is different from the source, patch the ".." entry in the |
| 546 | * directory. |
| 547 | */ |
| 548 | ufs_rename(fndp, tndp) |
| 549 | register struct nameidata *fndp, *tndp; |
| 550 | { |
| 551 | register struct inode *ip, *xp, *dp; |
| 552 | struct dirtemplate dirbuf; |
| 553 | int doingdirectory = 0, oldparent = 0, newparent = 0; |
| 554 | int error = 0; |
| 555 | |
| 556 | dp = VTOI(fndp->ni_dvp); |
| 557 | ip = VTOI(fndp->ni_vp); |
| 558 | ILOCK(ip); |
| 559 | if ((ip->i_mode&IFMT) == IFDIR) { |
| 560 | register struct direct *d = &fndp->ni_dent; |
| 561 | |
| 562 | /* |
| 563 | * Avoid ".", "..", and aliases of "." for obvious reasons. |
| 564 | */ |
| 565 | if ((d->d_namlen == 1 && d->d_name[0] == '.') || dp == ip || |
| 566 | fndp->ni_isdotdot || (ip->i_flag & IRENAME)) { |
| 567 | IUNLOCK(ip); |
| 568 | ufs_abortop(fndp); |
| 569 | ufs_abortop(tndp); |
| 570 | return (EINVAL); |
| 571 | } |
| 572 | ip->i_flag |= IRENAME; |
| 573 | oldparent = dp->i_number; |
| 574 | doingdirectory++; |
| 575 | } |
| 576 | vrele(fndp->ni_dvp); |
| 577 | |
| 578 | /* |
| 579 | * 1) Bump link count while we're moving stuff |
| 580 | * around. If we crash somewhere before |
| 581 | * completing our work, the link count |
| 582 | * may be wrong, but correctable. |
| 583 | */ |
| 584 | ip->i_nlink++; |
| 585 | ip->i_flag |= ICHG; |
| 586 | error = iupdat(ip, &time, &time, 1); |
| 587 | IUNLOCK(ip); |
| 588 | |
| 589 | /* |
| 590 | * When the target exists, both the directory |
| 591 | * and target vnodes are returned locked. |
| 592 | */ |
| 593 | dp = VTOI(tndp->ni_dvp); |
| 594 | xp = NULL; |
| 595 | if (tndp->ni_vp) |
| 596 | xp = VTOI(tndp->ni_vp); |
| 597 | /* |
| 598 | * If ".." must be changed (ie the directory gets a new |
| 599 | * parent) then the source directory must not be in the |
| 600 | * directory heirarchy above the target, as this would |
| 601 | * orphan everything below the source directory. Also |
| 602 | * the user must have write permission in the source so |
| 603 | * as to be able to change "..". We must repeat the call |
| 604 | * to namei, as the parent directory is unlocked by the |
| 605 | * call to checkpath(). |
| 606 | */ |
| 607 | if (oldparent != dp->i_number) |
| 608 | newparent = dp->i_number; |
| 609 | if (doingdirectory && newparent) { |
| 610 | if (error = iaccess(ip, IWRITE, tndp->ni_cred)) |
| 611 | goto bad; |
| 612 | tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE; |
| 613 | do { |
| 614 | dp = VTOI(tndp->ni_dvp); |
| 615 | if (xp != NULL) |
| 616 | iput(xp); |
| 617 | if (error = checkpath(ip, dp, tndp->ni_cred)) |
| 618 | goto out; |
| 619 | if (error = namei(tndp)) |
| 620 | goto out; |
| 621 | xp = NULL; |
| 622 | if (tndp->ni_vp) |
| 623 | xp = VTOI(tndp->ni_vp); |
| 624 | } while (dp != VTOI(tndp->ni_dvp)); |
| 625 | } |
| 626 | /* |
| 627 | * 2) If target doesn't exist, link the target |
| 628 | * to the source and unlink the source. |
| 629 | * Otherwise, rewrite the target directory |
| 630 | * entry to reference the source inode and |
| 631 | * expunge the original entry's existence. |
| 632 | */ |
| 633 | if (xp == NULL) { |
| 634 | if (dp->i_dev != ip->i_dev) |
| 635 | panic("rename: EXDEV"); |
| 636 | /* |
| 637 | * Account for ".." in new directory. |
| 638 | * When source and destination have the same |
| 639 | * parent we don't fool with the link count. |
| 640 | */ |
| 641 | if (doingdirectory && newparent) { |
| 642 | dp->i_nlink++; |
| 643 | dp->i_flag |= ICHG; |
| 644 | error = iupdat(dp, &time, &time, 1); |
| 645 | } |
| 646 | if (error = direnter(ip, tndp)) |
| 647 | goto out; |
| 648 | } else { |
| 649 | if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev) |
| 650 | panic("rename: EXDEV"); |
| 651 | /* |
| 652 | * Short circuit rename(foo, foo). |
| 653 | */ |
| 654 | if (xp->i_number == ip->i_number) |
| 655 | panic("rename: same file"); |
| 656 | /* |
| 657 | * If the parent directory is "sticky", then the user must |
| 658 | * own the parent directory, or the destination of the rename, |
| 659 | * otherwise the destination may not be changed (except by |
| 660 | * root). This implements append-only directories. |
| 661 | */ |
| 662 | if ((dp->i_mode & ISVTX) && tndp->ni_cred->cr_uid != 0 && |
| 663 | tndp->ni_cred->cr_uid != dp->i_uid && |
| 664 | xp->i_uid != tndp->ni_cred->cr_uid) { |
| 665 | error = EPERM; |
| 666 | goto bad; |
| 667 | } |
| 668 | /* |
| 669 | * Target must be empty if a directory |
| 670 | * and have no links to it. |
| 671 | * Also, insure source and target are |
| 672 | * compatible (both directories, or both |
| 673 | * not directories). |
| 674 | */ |
| 675 | if ((xp->i_mode&IFMT) == IFDIR) { |
| 676 | if (!dirempty(xp, dp->i_number, tndp->ni_cred) || |
| 677 | xp->i_nlink > 2) { |
| 678 | error = ENOTEMPTY; |
| 679 | goto bad; |
| 680 | } |
| 681 | if (!doingdirectory) { |
| 682 | error = ENOTDIR; |
| 683 | goto bad; |
| 684 | } |
| 685 | cache_purge(ITOV(dp)); |
| 686 | } else if (doingdirectory) { |
| 687 | error = EISDIR; |
| 688 | goto bad; |
| 689 | } |
| 690 | if (error = dirrewrite(dp, ip, tndp)) |
| 691 | goto bad; |
| 692 | vput(ITOV(dp)); |
| 693 | /* |
| 694 | * Adjust the link count of the target to |
| 695 | * reflect the dirrewrite above. If this is |
| 696 | * a directory it is empty and there are |
| 697 | * no links to it, so we can squash the inode and |
| 698 | * any space associated with it. We disallowed |
| 699 | * renaming over top of a directory with links to |
| 700 | * it above, as the remaining link would point to |
| 701 | * a directory without "." or ".." entries. |
| 702 | */ |
| 703 | xp->i_nlink--; |
| 704 | if (doingdirectory) { |
| 705 | if (--xp->i_nlink != 0) |
| 706 | panic("rename: linked directory"); |
| 707 | error = itrunc(xp, (u_long)0); |
| 708 | } |
| 709 | xp->i_flag |= ICHG; |
| 710 | iput(xp); |
| 711 | xp = NULL; |
| 712 | } |
| 713 | |
| 714 | /* |
| 715 | * 3) Unlink the source. |
| 716 | */ |
| 717 | fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF; |
| 718 | (void)namei(fndp); |
| 719 | if (fndp->ni_vp != NULL) { |
| 720 | xp = VTOI(fndp->ni_vp); |
| 721 | dp = VTOI(fndp->ni_dvp); |
| 722 | } else { |
| 723 | if (fndp->ni_dvp != NULL) |
| 724 | vput(fndp->ni_dvp); |
| 725 | xp = NULL; |
| 726 | dp = NULL; |
| 727 | } |
| 728 | /* |
| 729 | * Ensure that the directory entry still exists and has not |
| 730 | * changed while the new name has been entered. If the source is |
| 731 | * a file then the entry may have been unlinked or renamed. In |
| 732 | * either case there is no further work to be done. If the source |
| 733 | * is a directory then it cannot have been rmdir'ed; its link |
| 734 | * count of three would cause a rmdir to fail with ENOTEMPTY. |
| 735 | * The IRENAME flag ensures that it cannot be moved by another |
| 736 | * rename. |
| 737 | */ |
| 738 | if (xp != ip) { |
| 739 | if (doingdirectory) |
| 740 | panic("rename: lost dir entry"); |
| 741 | } else { |
| 742 | /* |
| 743 | * If the source is a directory with a |
| 744 | * new parent, the link count of the old |
| 745 | * parent directory must be decremented |
| 746 | * and ".." set to point to the new parent. |
| 747 | */ |
| 748 | if (doingdirectory && newparent) { |
| 749 | dp->i_nlink--; |
| 750 | dp->i_flag |= ICHG; |
| 751 | error = rdwri(UIO_READ, xp, (caddr_t)&dirbuf, |
| 752 | sizeof (struct dirtemplate), (off_t)0, |
| 753 | UIO_SYSSPACE, tndp->ni_cred, (int *)0); |
| 754 | if (error == 0) { |
| 755 | if (dirbuf.dotdot_namlen != 2 || |
| 756 | dirbuf.dotdot_name[0] != '.' || |
| 757 | dirbuf.dotdot_name[1] != '.') { |
| 758 | printf("rename: mangled dir\n"); |
| 759 | } else { |
| 760 | dirbuf.dotdot_ino = newparent; |
| 761 | (void) rdwri(UIO_WRITE, xp, |
| 762 | (caddr_t)&dirbuf, |
| 763 | sizeof (struct dirtemplate), |
| 764 | (off_t)0, UIO_SYSSPACE, |
| 765 | tndp->ni_cred, (int *)0); |
| 766 | cache_purge(ITOV(dp)); |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | error = dirremove(fndp); |
| 771 | if (!error) { |
| 772 | xp->i_nlink--; |
| 773 | xp->i_flag |= ICHG; |
| 774 | } |
| 775 | xp->i_flag &= ~IRENAME; |
| 776 | } |
| 777 | if (dp) |
| 778 | vput(ITOV(dp)); |
| 779 | if (xp) |
| 780 | vput(ITOV(xp)); |
| 781 | vrele(ITOV(ip)); |
| 782 | return (error); |
| 783 | |
| 784 | bad: |
| 785 | if (xp) |
| 786 | vput(ITOV(xp)); |
| 787 | vput(ITOV(dp)); |
| 788 | out: |
| 789 | ip->i_nlink--; |
| 790 | ip->i_flag |= ICHG; |
| 791 | vrele(ITOV(ip)); |
| 792 | return (error); |
| 793 | } |
| 794 | |
| 795 | /* |
| 796 | * A virgin directory (no blushing please). |
| 797 | */ |
| 798 | struct dirtemplate mastertemplate = { |
| 799 | 0, 12, 1, ".", |
| 800 | 0, DIRBLKSIZ - 12, 2, ".." |
| 801 | }; |
| 802 | |
| 803 | /* |
| 804 | * Mkdir system call |
| 805 | */ |
| 806 | ufs_mkdir(ndp, vap) |
| 807 | struct nameidata *ndp; |
| 808 | struct vattr *vap; |
| 809 | { |
| 810 | register struct inode *ip, *dp; |
| 811 | struct inode *tip; |
| 812 | struct vnode *dvp; |
| 813 | struct dirtemplate dirtemplate; |
| 814 | int error; |
| 815 | int dmode; |
| 816 | |
| 817 | dvp = ndp->ni_dvp; |
| 818 | dp = VTOI(dvp); |
| 819 | dmode = vap->va_mode&0777; |
| 820 | dmode |= IFDIR; |
| 821 | /* |
| 822 | * Must simulate part of maknode here |
| 823 | * in order to acquire the inode, but |
| 824 | * not have it entered in the parent |
| 825 | * directory. The entry is made later |
| 826 | * after writing "." and ".." entries out. |
| 827 | */ |
| 828 | error = ialloc(dp, dirpref(dp->i_fs), dmode, &tip); |
| 829 | if (error) { |
| 830 | iput(dp); |
| 831 | return (error); |
| 832 | } |
| 833 | ip = tip; |
| 834 | #ifdef QUOTA |
| 835 | if (ip->i_dquot != NODQUOT) |
| 836 | panic("mkdir: dquot"); |
| 837 | #endif |
| 838 | ip->i_flag |= IACC|IUPD|ICHG; |
| 839 | ip->i_mode = dmode; |
| 840 | ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */ |
| 841 | ip->i_nlink = 2; |
| 842 | ip->i_uid = ndp->ni_cred->cr_uid; |
| 843 | ip->i_gid = dp->i_gid; |
| 844 | #ifdef QUOTA |
| 845 | ip->i_dquot = inoquota(ip); |
| 846 | #endif |
| 847 | error = iupdat(ip, &time, &time, 1); |
| 848 | |
| 849 | /* |
| 850 | * Bump link count in parent directory |
| 851 | * to reflect work done below. Should |
| 852 | * be done before reference is created |
| 853 | * so reparation is possible if we crash. |
| 854 | */ |
| 855 | dp->i_nlink++; |
| 856 | dp->i_flag |= ICHG; |
| 857 | error = iupdat(dp, &time, &time, 1); |
| 858 | |
| 859 | /* |
| 860 | * Initialize directory with "." |
| 861 | * and ".." from static template. |
| 862 | */ |
| 863 | dirtemplate = mastertemplate; |
| 864 | dirtemplate.dot_ino = ip->i_number; |
| 865 | dirtemplate.dotdot_ino = dp->i_number; |
| 866 | error = rdwri(UIO_WRITE, ip, (caddr_t)&dirtemplate, |
| 867 | sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, |
| 868 | ndp->ni_cred, (int *)0); |
| 869 | if (error) { |
| 870 | dp->i_nlink--; |
| 871 | dp->i_flag |= ICHG; |
| 872 | goto bad; |
| 873 | } |
| 874 | if (DIRBLKSIZ > dp->i_fs->fs_fsize) |
| 875 | panic("mkdir: blksize"); /* XXX - should grow w/balloc() */ |
| 876 | else |
| 877 | ip->i_size = DIRBLKSIZ; |
| 878 | /* |
| 879 | * Directory all set up, now |
| 880 | * install the entry for it in |
| 881 | * the parent directory. |
| 882 | */ |
| 883 | error = direnter(ip, ndp); |
| 884 | dp = NULL; |
| 885 | if (error) { |
| 886 | ndp->ni_nameiop = LOOKUP | NOCACHE; |
| 887 | error = namei(ndp); |
| 888 | if (!error) { |
| 889 | dp = VTOI(ndp->ni_vp); |
| 890 | dp->i_nlink--; |
| 891 | dp->i_flag |= ICHG; |
| 892 | } |
| 893 | } |
| 894 | bad: |
| 895 | /* |
| 896 | * No need to do an explicit itrunc here, |
| 897 | * vrele will do this for us because we set |
| 898 | * the link count to 0. |
| 899 | */ |
| 900 | if (error) { |
| 901 | ip->i_nlink = 0; |
| 902 | ip->i_flag |= ICHG; |
| 903 | iput(ip); |
| 904 | } else |
| 905 | ndp->ni_vp = ITOV(ip); |
| 906 | if (dp) |
| 907 | iput(dp); |
| 908 | return (error); |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | * Rmdir system call. |
| 913 | */ |
| 914 | ufs_rmdir(ndp) |
| 915 | register struct nameidata *ndp; |
| 916 | { |
| 917 | register struct inode *ip, *dp; |
| 918 | int error = 0; |
| 919 | |
| 920 | ip = VTOI(ndp->ni_vp); |
| 921 | dp = VTOI(ndp->ni_dvp); |
| 922 | /* |
| 923 | * No rmdir "." please. |
| 924 | */ |
| 925 | if (dp == ip) { |
| 926 | vrele(ITOV(dp)); |
| 927 | iput(ip); |
| 928 | return (EINVAL); |
| 929 | } |
| 930 | /* |
| 931 | * Verify the directory is empty (and valid). |
| 932 | * (Rmdir ".." won't be valid since |
| 933 | * ".." will contain a reference to |
| 934 | * the current directory and thus be |
| 935 | * non-empty.) |
| 936 | */ |
| 937 | if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) { |
| 938 | error = ENOTEMPTY; |
| 939 | goto out; |
| 940 | } |
| 941 | /* |
| 942 | * Delete reference to directory before purging |
| 943 | * inode. If we crash in between, the directory |
| 944 | * will be reattached to lost+found, |
| 945 | */ |
| 946 | if (error = dirremove(ndp)) |
| 947 | goto out; |
| 948 | dp->i_nlink--; |
| 949 | dp->i_flag |= ICHG; |
| 950 | cache_purge(ITOV(dp)); |
| 951 | iput(dp); |
| 952 | ndp->ni_dvp = NULL; |
| 953 | /* |
| 954 | * Truncate inode. The only stuff left |
| 955 | * in the directory is "." and "..". The |
| 956 | * "." reference is inconsequential since |
| 957 | * we're quashing it. The ".." reference |
| 958 | * has already been adjusted above. We've |
| 959 | * removed the "." reference and the reference |
| 960 | * in the parent directory, but there may be |
| 961 | * other hard links so decrement by 2 and |
| 962 | * worry about them later. |
| 963 | */ |
| 964 | ip->i_nlink -= 2; |
| 965 | error = itrunc(ip, (u_long)0); |
| 966 | cache_purge(ITOV(ip)); |
| 967 | out: |
| 968 | if (ndp->ni_dvp) |
| 969 | iput(dp); |
| 970 | iput(ip); |
| 971 | return (error); |
| 972 | } |
| 973 | |
| 974 | /* |
| 975 | * symlink -- make a symbolic link |
| 976 | */ |
| 977 | ufs_symlink(ndp, vap, target) |
| 978 | struct nameidata *ndp; |
| 979 | struct vattr *vap; |
| 980 | char *target; |
| 981 | { |
| 982 | struct inode *ip; |
| 983 | int error; |
| 984 | |
| 985 | error = maknode(IFLNK | vap->va_mode, ndp, &ip); |
| 986 | if (error) |
| 987 | return (error); |
| 988 | error = rdwri(UIO_WRITE, ip, target, strlen(target), (off_t)0, |
| 989 | UIO_SYSSPACE, ndp->ni_cred, (int *)0); |
| 990 | iput(ip); |
| 991 | return (error); |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * Vnode op for read and write |
| 996 | */ |
| 997 | ufs_readdir(vp, uio, offp, cred) |
| 998 | struct vnode *vp; |
| 999 | register struct uio *uio; |
| 1000 | off_t *offp; |
| 1001 | struct ucred *cred; |
| 1002 | { |
| 1003 | register struct inode *ip = VTOI(vp); |
| 1004 | int count, error; |
| 1005 | |
| 1006 | ILOCK(ip); |
| 1007 | uio->uio_offset = *offp; |
| 1008 | count = uio->uio_resid; |
| 1009 | count &= ~(DIRBLKSIZ - 1); |
| 1010 | if (vp->v_type != VDIR || uio->uio_iovcnt != 1 || |
| 1011 | (count < DIRBLKSIZ) || (uio->uio_offset & (DIRBLKSIZ -1))) { |
| 1012 | IUNLOCK(ip); |
| 1013 | return (EINVAL); |
| 1014 | } |
| 1015 | uio->uio_resid = count; |
| 1016 | uio->uio_iov->iov_len = count; |
| 1017 | error = readip(ip, uio, cred); |
| 1018 | *offp += count - uio->uio_resid; |
| 1019 | IUNLOCK(ip); |
| 1020 | return (error); |
| 1021 | } |
| 1022 | |
| 1023 | /* |
| 1024 | * Return target name of a symbolic link |
| 1025 | */ |
| 1026 | ufs_readlink(vp, uiop, cred) |
| 1027 | struct vnode *vp; |
| 1028 | struct uio *uiop; |
| 1029 | struct ucred *cred; |
| 1030 | { |
| 1031 | |
| 1032 | return (readip(VTOI(vp), uiop, cred)); |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually |
| 1037 | * done. Iff ni_vp/ni_dvp not null and locked, unlock. |
| 1038 | */ |
| 1039 | ufs_abortop(ndp) |
| 1040 | register struct nameidata *ndp; |
| 1041 | { |
| 1042 | register struct inode *ip; |
| 1043 | |
| 1044 | if (ndp->ni_vp) { |
| 1045 | ip = VTOI(ndp->ni_vp); |
| 1046 | if (ip->i_flag & ILOCKED) |
| 1047 | IUNLOCK(ip); |
| 1048 | vrele(ndp->ni_vp); |
| 1049 | } |
| 1050 | if (ndp->ni_dvp) { |
| 1051 | ip = VTOI(ndp->ni_dvp); |
| 1052 | if (ip->i_flag & ILOCKED) |
| 1053 | IUNLOCK(ip); |
| 1054 | vrele(ndp->ni_dvp); |
| 1055 | } |
| 1056 | return; |
| 1057 | } |
| 1058 | |
| 1059 | ufs_lock(vp) |
| 1060 | struct vnode *vp; |
| 1061 | { |
| 1062 | register struct inode *ip = VTOI(vp); |
| 1063 | |
| 1064 | ILOCK(ip); |
| 1065 | return (0); |
| 1066 | } |
| 1067 | |
| 1068 | ufs_unlock(vp) |
| 1069 | struct vnode *vp; |
| 1070 | { |
| 1071 | register struct inode *ip = VTOI(vp); |
| 1072 | |
| 1073 | if (!(ip->i_flag & ILOCKED)) |
| 1074 | panic("ufs_unlock NOT LOCKED"); |
| 1075 | IUNLOCK(ip); |
| 1076 | return (0); |
| 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * Get access to bmap |
| 1081 | */ |
| 1082 | ufs_bmap(vp, bn, vpp, bnp) |
| 1083 | struct vnode *vp; |
| 1084 | daddr_t bn; |
| 1085 | struct vnode **vpp; |
| 1086 | daddr_t *bnp; |
| 1087 | { |
| 1088 | struct inode *ip = VTOI(vp); |
| 1089 | |
| 1090 | if (vpp != NULL) |
| 1091 | *vpp = ip->i_devvp; |
| 1092 | if (bnp == NULL) |
| 1093 | return (0); |
| 1094 | return (bmap(ip, bn, bnp, (daddr_t *)0, (int *)0)); |
| 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * Just call the device strategy routine |
| 1099 | */ |
| 1100 | ufs_strategy(bp) |
| 1101 | register struct buf *bp; |
| 1102 | { |
| 1103 | (*bdevsw[major(bp->b_dev)].d_strategy)(bp); |
| 1104 | return (0); |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | * Make a new file. |
| 1109 | */ |
| 1110 | maknode(mode, ndp, ipp) |
| 1111 | int mode; |
| 1112 | register struct nameidata *ndp; |
| 1113 | struct inode **ipp; |
| 1114 | { |
| 1115 | register struct inode *ip; |
| 1116 | struct inode *tip; |
| 1117 | register struct inode *pdir = VTOI(ndp->ni_dvp); |
| 1118 | ino_t ipref; |
| 1119 | int error; |
| 1120 | |
| 1121 | *ipp = 0; |
| 1122 | if ((mode & IFMT) == IFDIR) |
| 1123 | ipref = dirpref(pdir->i_fs); |
| 1124 | else |
| 1125 | ipref = pdir->i_number; |
| 1126 | error = ialloc(pdir, ipref, mode, &tip); |
| 1127 | if (error) { |
| 1128 | iput(pdir); |
| 1129 | return (error); |
| 1130 | } |
| 1131 | ip = tip; |
| 1132 | #ifdef QUOTA |
| 1133 | if (ip->i_dquot != NODQUOT) |
| 1134 | panic("maknode: dquot"); |
| 1135 | #endif |
| 1136 | ip->i_flag |= IACC|IUPD|ICHG; |
| 1137 | if ((mode & IFMT) == 0) |
| 1138 | mode |= IFREG; |
| 1139 | ip->i_mode = mode; |
| 1140 | ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */ |
| 1141 | ip->i_nlink = 1; |
| 1142 | ip->i_uid = ndp->ni_cred->cr_uid; |
| 1143 | ip->i_gid = pdir->i_gid; |
| 1144 | if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) && |
| 1145 | suser(ndp->ni_cred, NULL)) |
| 1146 | ip->i_mode &= ~ISGID; |
| 1147 | #ifdef QUOTA |
| 1148 | ip->i_dquot = inoquota(ip); |
| 1149 | #endif |
| 1150 | |
| 1151 | /* |
| 1152 | * Make sure inode goes to disk before directory entry. |
| 1153 | */ |
| 1154 | if ((error = iupdat(ip, &time, &time, 1)) || |
| 1155 | (error = direnter(ip, ndp))) { |
| 1156 | /* |
| 1157 | * Write error occurred trying to update the inode |
| 1158 | * or the directory so must deallocate the inode. |
| 1159 | */ |
| 1160 | ip->i_nlink = 0; |
| 1161 | ip->i_flag |= ICHG; |
| 1162 | iput(ip); |
| 1163 | return (error); |
| 1164 | } |
| 1165 | *ipp = ip; |
| 1166 | return (0); |
| 1167 | } |