-
-/*
- * A virgin directory (no blushing please).
- */
-struct dirtemplate mastertemplate = {
- 0, 12, 1, ".",
- 0, DIRBLKSIZ - 12, 2, ".."
-};
-
-/*
- * Mkdir system call
- */
-ufs_mkdir(ndp, vap)
- struct nameidata *ndp;
- struct vattr *vap;
-{
- register struct inode *ip, *dp;
- struct inode *tip;
- struct vnode *dvp;
- struct dirtemplate dirtemplate;
- int error;
- int dmode;
-
- dvp = ndp->ni_dvp;
- dp = VTOI(dvp);
- dmode = vap->va_mode&0777;
- dmode |= IFDIR;
- /*
- * Must simulate part of maknode here
- * in order to acquire the inode, but
- * not have it entered in the parent
- * directory. The entry is made later
- * after writing "." and ".." entries out.
- */
- if (error = ialloc(dp, dirpref(dp->i_fs), dmode, ndp->ni_cred, &tip)) {
- iput(dp);
- return (error);
- }
- ip = tip;
- ip->i_uid = ndp->ni_cred->cr_uid;
- ip->i_gid = dp->i_gid;
-#ifdef QUOTA
- if ((error = getinoquota(ip)) ||
- (error = chkiq(ip, 1, ndp->ni_cred, 0))) {
- ifree(ip, ip->i_number, dmode);
- iput(ip);
- iput(dp);
- return (error);
- }
-#endif
- ip->i_flag |= IACC|IUPD|ICHG;
- ip->i_mode = dmode;
- ITOV(ip)->v_type = VDIR; /* Rest init'd in iget() */
- ip->i_nlink = 2;
- error = iupdat(ip, &time, &time, 1);
-
- /*
- * Bump link count in parent directory
- * to reflect work done below. Should
- * be done before reference is created
- * so reparation is possible if we crash.
- */
- dp->i_nlink++;
- dp->i_flag |= ICHG;
- error = iupdat(dp, &time, &time, 1);
-
- /*
- * Initialize directory with "."
- * and ".." from static template.
- */
- dirtemplate = mastertemplate;
- dirtemplate.dot_ino = ip->i_number;
- dirtemplate.dotdot_ino = dp->i_number;
- error = vn_rdwr(UIO_WRITE, ITOV(ip), (caddr_t)&dirtemplate,
- sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE,
- IO_NODELOCKED|IO_SYNC, ndp->ni_cred, (int *)0);
- if (error) {
- dp->i_nlink--;
- dp->i_flag |= ICHG;
- goto bad;
- }
- if (DIRBLKSIZ > dp->i_fs->fs_fsize) {
- panic("mkdir: blksize"); /* XXX - should grow w/balloc() */
- } else {
- ip->i_size = DIRBLKSIZ;
- ip->i_flag |= ICHG;
- }
- /*
- * Directory all set up, now
- * install the entry for it in
- * the parent directory.
- */
- error = direnter(ip, ndp);
- dp = NULL;
- if (error) {
- ndp->ni_nameiop = LOOKUP | NOCACHE;
- error = namei(ndp);
- if (!error) {
- dp = VTOI(ndp->ni_vp);
- dp->i_nlink--;
- dp->i_flag |= ICHG;
- }
- }
-bad:
- /*
- * No need to do an explicit itrunc here,
- * vrele will do this for us because we set
- * the link count to 0.
- */
- if (error) {
- ip->i_nlink = 0;
- ip->i_flag |= ICHG;
- iput(ip);
- } else
- ndp->ni_vp = ITOV(ip);
- if (dp)
- iput(dp);
- return (error);
-}
-
-/*
- * Rmdir system call.
- */
-ufs_rmdir(ndp)
- register struct nameidata *ndp;
-{
- register struct inode *ip, *dp;
- int error = 0;
-
- ip = VTOI(ndp->ni_vp);
- dp = VTOI(ndp->ni_dvp);
- /*
- * No rmdir "." please.
- */
- if (dp == ip) {
- vrele(ITOV(dp));
- iput(ip);
- return (EINVAL);
- }
- /*
- * Verify the directory is empty (and valid).
- * (Rmdir ".." won't be valid since
- * ".." will contain a reference to
- * the current directory and thus be
- * non-empty.)
- */
- if (ip->i_nlink != 2 || !dirempty(ip, dp->i_number, ndp->ni_cred)) {
- error = ENOTEMPTY;
- goto out;
- }
- /*
- * Delete reference to directory before purging
- * inode. If we crash in between, the directory
- * will be reattached to lost+found,
- */
- if (error = dirremove(ndp))
- goto out;
- dp->i_nlink--;
- dp->i_flag |= ICHG;
- cache_purge(ITOV(dp));
- iput(dp);
- ndp->ni_dvp = NULL;
- /*
- * Truncate inode. The only stuff left
- * in the directory is "." and "..". The
- * "." reference is inconsequential since
- * we're quashing it. The ".." reference
- * has already been adjusted above. We've
- * removed the "." reference and the reference
- * in the parent directory, but there may be
- * other hard links so decrement by 2 and
- * worry about them later.
- */
- ip->i_nlink -= 2;
- error = itrunc(ip, (u_long)0, IO_SYNC);
- cache_purge(ITOV(ip));
-out:
- if (ndp->ni_dvp)
- iput(dp);
- iput(ip);
- return (error);
-}
-
-/*
- * symlink -- make a symbolic link
- */
-ufs_symlink(ndp, vap, target)
- struct nameidata *ndp;
- struct vattr *vap;
- char *target;
-{
- struct inode *ip;
- int error;
-
- error = maknode(IFLNK | vap->va_mode, ndp, &ip);
- if (error)
- return (error);
- error = vn_rdwr(UIO_WRITE, ITOV(ip), target, strlen(target), (off_t)0,
- UIO_SYSSPACE, IO_NODELOCKED, ndp->ni_cred, (int *)0);
- iput(ip);
- return (error);
-}
-
-/*
- * Vnode op for read and write
- */
-ufs_readdir(vp, uio, cred, eofflagp)
- struct vnode *vp;
- register struct uio *uio;
- struct ucred *cred;
- int *eofflagp;
-{
- int count, lost, error;
-
- count = uio->uio_resid;
- count &= ~(DIRBLKSIZ - 1);
- lost = uio->uio_resid - count;
- if (count < DIRBLKSIZ || (uio->uio_offset & (DIRBLKSIZ -1)))
- return (EINVAL);
- uio->uio_resid = count;
- uio->uio_iov->iov_len = count;
- error = ufs_read(vp, uio, 0, cred);
- uio->uio_resid += lost;
- if ((VTOI(vp)->i_size - uio->uio_offset) <= 0)
- *eofflagp = 1;
- else
- *eofflagp = 0;
- return (error);
-}
-
-/*
- * Return target name of a symbolic link
- */
-ufs_readlink(vp, uiop, cred)
- struct vnode *vp;
- struct uio *uiop;
- struct ucred *cred;
-{
-
- return (ufs_read(vp, uiop, 0, cred));
-}
-
-/*
- * Ufs abort op, called after namei() when a CREATE/DELETE isn't actually
- * done. Nothing to do at the moment.
- */
-/* ARGSUSED */
-ufs_abortop(ndp)
- struct nameidata *ndp;
-{
-
- return (0);
-}
-
-/*
- * Lock an inode.
- */
-ufs_lock(vp)
- struct vnode *vp;
-{
- register struct inode *ip = VTOI(vp);
-
- ILOCK(ip);
- return (0);
-}
-
-/*
- * Unlock an inode.
- */
-ufs_unlock(vp)
- struct vnode *vp;
-{
- register struct inode *ip = VTOI(vp);
-
- if (!(ip->i_flag & ILOCKED))
- panic("ufs_unlock NOT LOCKED");
- IUNLOCK(ip);
- return (0);
-}
-
-/*
- * Check for a locked inode.
- */
-ufs_islocked(vp)
- struct vnode *vp;
-{
-
- if (VTOI(vp)->i_flag & ILOCKED)
- return (1);
- return (0);
-}
-
-/*
- * Get access to bmap
- */
-ufs_bmap(vp, bn, vpp, bnp)
- struct vnode *vp;
- daddr_t bn;
- struct vnode **vpp;
- daddr_t *bnp;
-{
- struct inode *ip = VTOI(vp);
-
- if (vpp != NULL)
- *vpp = ip->i_devvp;
- if (bnp == NULL)
- return (0);
- return (bmap(ip, bn, bnp));
-}
-
-/*
- * Calculate the logical to physical mapping if not done already,
- * then call the device strategy routine.
- */
-int checkoverlap = 0;
-
-ufs_strategy(bp)
- register struct buf *bp;
-{
- register struct inode *ip = VTOI(bp->b_vp);
- struct vnode *vp;
- int error;
-
- if (bp->b_vp->v_type == VBLK || bp->b_vp->v_type == VCHR)
- panic("ufs_strategy: spec");
- if (bp->b_blkno == bp->b_lblkno) {
- if (error = bmap(ip, bp->b_lblkno, &bp->b_blkno))
- return (error);
- if ((long)bp->b_blkno == -1)
- clrbuf(bp);
- }
- if ((long)bp->b_blkno == -1) {
- biodone(bp);
- return (0);
- }
-#ifdef DIAGNOSTIC
- if (checkoverlap) {
- register struct buf *ep;
- struct buf *ebp;
- daddr_t start, last;
-
- ebp = &buf[nbuf];
- start = bp->b_blkno;
- last = start + btodb(bp->b_bcount) - 1;
- for (ep = buf; ep < ebp; ep++) {
- if (ep == bp || (ep->b_flags & B_INVAL) ||
- ep->b_vp == NULLVP)
- continue;
- if (VOP_BMAP(ep->b_vp, (daddr_t)0, &vp, (daddr_t)0))
- continue;
- if (vp != ip->i_devvp)
- continue;
- /* look for overlap */
- if (ep->b_bcount == 0 || ep->b_blkno > last ||
- ep->b_blkno + btodb(ep->b_bcount) <= start)
- continue;
- vprint("Disk overlap", vp);
- printf("\tstart %d, end %d overlap start %d, end %d\n",
- start, last, ep->b_blkno,
- ep->b_blkno + btodb(ep->b_bcount) - 1);
- panic("Disk buffer overlap");
- }
- }
-#endif /* DIAGNOSTIC */
- vp = ip->i_devvp;
- bp->b_dev = vp->v_rdev;
- (*(vp->v_op->vn_strategy))(bp);
- return (0);
-}
-
-/*
- * Print out the contents of an inode.
- */
-ufs_print(vp)
- struct vnode *vp;
-{
- register struct inode *ip = VTOI(vp);
-
- printf("tag VT_UFS, ino %d, on dev %d, %d", ip->i_number,
- major(ip->i_dev), minor(ip->i_dev));
-#ifdef FIFO
- if (vp->v_type == VFIFO)
- fifo_printinfo(vp);
-#endif /* FIFO */
- printf("%s\n", (ip->i_flag & ILOCKED) ? " (LOCKED)" : "");
- if (ip->i_spare0 == 0)
- return;
- printf("\towner pid %d", ip->i_spare0);
- if (ip->i_spare1)
- printf(" waiting pid %d", ip->i_spare1);
- printf("\n");
-}
-
-/*
- * Read wrapper for special devices.
- */
-ufsspec_read(vp, uio, ioflag, cred)
- struct vnode *vp;
- struct uio *uio;
- int ioflag;
- struct ucred *cred;
-{
-
- /*
- * Set access flag.
- */
- VTOI(vp)->i_flag |= IACC;
- return (spec_read(vp, uio, ioflag, cred));
-}
-
-/*
- * Write wrapper for special devices.
- */
-ufsspec_write(vp, uio, ioflag, cred)
- struct vnode *vp;
- struct uio *uio;
- int ioflag;
- struct ucred *cred;
-{
-
- /*
- * Set update and change flags.
- */
- VTOI(vp)->i_flag |= IUPD|ICHG;
- return (spec_write(vp, uio, ioflag, cred));
-}
-
-/*
- * Close wrapper for special devices.
- *
- * Update the times on the inode then do device close.
- */
-ufsspec_close(vp, fflag, cred)
- struct vnode *vp;
- int fflag;
- struct ucred *cred;
-{
- register struct inode *ip = VTOI(vp);
-
- if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
- ITIMES(ip, &time, &time);
- return (spec_close(vp, fflag, cred));
-}
-
-#ifdef FIFO
-/*
- * Read wrapper for fifo's
- */
-ufsfifo_read(vp, uio, ioflag, cred)
- struct vnode *vp;
- struct uio *uio;
- int ioflag;
- struct ucred *cred;
-{
-
- /*
- * Set access flag.
- */
- VTOI(vp)->i_flag |= IACC;
- return (fifo_read(vp, uio, ioflag, cred));
-}
-
-/*
- * Write wrapper for fifo's.
- */
-ufsfifo_write(vp, uio, ioflag, cred)
- struct vnode *vp;
- struct uio *uio;
- int ioflag;
- struct ucred *cred;
-{
-
- /*
- * Set update and change flags.
- */
- VTOI(vp)->i_flag |= IUPD|ICHG;
- return (fifo_write(vp, uio, ioflag, cred));
-}
-
-/*
- * Close wrapper for fifo's.
- *
- * Update the times on the inode then do device close.
- */
-ufsfifo_close(vp, fflag, cred)
- struct vnode *vp;
- int fflag;
- struct ucred *cred;
-{
- register struct inode *ip = VTOI(vp);
-
- if (vp->v_usecount > 1 && !(ip->i_flag & ILOCKED))
- ITIMES(ip, &time, &time);
- return (fifo_close(vp, fflag, cred));
-}
-#endif /* FIFO */
-
-/*
- * Make a new file.
- */
-maknode(mode, ndp, ipp)
- int mode;
- register struct nameidata *ndp;
- struct inode **ipp;
-{
- register struct inode *ip;
- struct inode *tip;
- register struct inode *pdir = VTOI(ndp->ni_dvp);
- ino_t ipref;
- int error;
-
- *ipp = 0;
- if ((mode & IFMT) == 0)
- mode |= IFREG;
- if ((mode & IFMT) == IFDIR)
- ipref = dirpref(pdir->i_fs);
- else
- ipref = pdir->i_number;
- if (error = ialloc(pdir, ipref, mode, ndp->ni_cred, &tip)) {
- iput(pdir);
- return (error);
- }
- ip = tip;
- ip->i_uid = ndp->ni_cred->cr_uid;
- ip->i_gid = pdir->i_gid;
-#ifdef QUOTA
- if ((error = getinoquota(ip)) ||
- (error = chkiq(ip, 1, ndp->ni_cred, 0))) {
- ifree(ip, ip->i_number, mode);
- iput(ip);
- iput(pdir);
- return (error);
- }
-#endif
- ip->i_flag |= IACC|IUPD|ICHG;
- ip->i_mode = mode;
- ITOV(ip)->v_type = IFTOVT(mode); /* Rest init'd in iget() */
- ip->i_nlink = 1;
- if ((ip->i_mode & ISGID) && !groupmember(ip->i_gid, ndp->ni_cred) &&
- suser(ndp->ni_cred, NULL))
- ip->i_mode &= ~ISGID;
-
- /*
- * Make sure inode goes to disk before directory entry.
- */
- if (error = iupdat(ip, &time, &time, 1))
- goto bad;
- if (error = direnter(ip, ndp)) {
- pdir = NULL;
- goto bad;
- }
- *ipp = ip;
- return (0);
-
-bad:
- /*
- * Write error occurred trying to update the inode
- * or the directory so must deallocate the inode.
- */
- if (pdir)
- iput(pdir);
- ip->i_nlink = 0;
- ip->i_flag |= ICHG;
- iput(ip);
- return (error);
-}
-
-/*
- * Advisory record locking support
- */
-ufs_advlock(vp, id, op, fl, flags)
- struct vnode *vp;
- caddr_t id;
- int op;
- register struct flock *fl;
- int flags;
-{
- register struct inode *ip = VTOI(vp);
- register struct lockf *lock;
- off_t start, end;
- int error;
-
- /*
- * Avoid the common case of unlocking when inode has no locks.
- */
- if (ip->i_lockf == (struct lockf *)0) {
- if (op != F_SETLK) {
- fl->l_type = F_UNLCK;
- return (0);
- }
- }
- /*
- * Convert the flock structure into a start and end.
- */
- switch (fl->l_whence) {
-
- case SEEK_SET:
- case SEEK_CUR:
- /*
- * Caller is responsible for adding any necessary offset
- * when SEEK_CUR is used.
- */
- start = fl->l_start;
- break;
-
- case SEEK_END:
- start = ip->i_size + fl->l_start;
- break;
-
- default:
- return (EINVAL);
- }
- if (start < 0)
- return (EINVAL);
- if (fl->l_len == 0)
- end = -1;
- else
- end = start + fl->l_len;
- /*
- * Create the lockf structure
- */
- MALLOC(lock, struct lockf *, sizeof *lock, M_LOCKF, M_WAITOK);
- lock->lf_start = start;
- lock->lf_end = end;
- lock->lf_id = id;
- lock->lf_inode = ip;
- lock->lf_type = fl->l_type;
- lock->lf_next = (struct lockf *)0;
- lock->lf_block = (struct lockf *)0;
- lock->lf_flags = flags;
- /*
- * Do the requested operation.
- */
- switch(op) {
- case F_SETLK:
- return (ufs_setlock(lock));
-
- case F_UNLCK:
- return (ufs_advunlock(lock));
-
- case F_GETLK:
- return (ufs_advgetlock(lock, fl));
-
- default:
- free(lock, M_LOCKF);
- return (EINVAL);
- }
- /* NOTREACHED */
-}
-
-/*
- * This variable controls the maximum number of processes that will
- * be checked in doing deadlock detection.
- */
-int maxlockdepth = MAXDEPTH;
-
-/*
- * Set a byte-range lock.
- */
-ufs_setlock(lock)
- register struct lockf *lock;
-{
- register struct inode *ip = lock->lf_inode;
- register struct lockf *block;
- static char lockstr[] = "lockf";
- int priority, error;
-
-#ifdef LOCKF_DEBUG
- if (lockf_debug & 4)
- lf_print("ufs_setlock", lock);
-#endif /* LOCKF_DEBUG */
-
- /*
- * Set the priority
- */
- priority = PLOCK;
- if ((lock->lf_type & F_WRLCK) == 0)
- priority += 4;
- priority |= PCATCH;
- /*
- * Scan lock list for this file looking for locks that would block us.
- */
- while (block = lf_getblock(lock)) {
- /*
- * Free the structure and return if nonblocking.
- */
- if ((lock->lf_flags & F_WAIT) == 0) {
- free(lock, M_LOCKF);
- return (EAGAIN);
- }
- /*
- * We are blocked. Since flock style locks cover
- * the whole file, there is no chance for deadlock.
- * For byte-range locks we must check for deadlock.
- *
- * Deadlock detection is done by looking through the
- * wait channels to see if there are any cycles that
- * involve us. MAXDEPTH is set just to make sure we
- * do not go off into neverland.
- */
- if ((lock->lf_flags & F_POSIX) &&
- (block->lf_flags & F_POSIX)) {
- register struct proc *wproc;
- register struct lockf *waitblock;
- int i = 0;
-
- /* The block is waiting on something */
- wproc = (struct proc *)block->lf_id;
- while (wproc->p_wchan &&
- (wproc->p_wmesg == lockstr) &&
- (i++ < maxlockdepth)) {
- waitblock = (struct lockf *)wproc->p_wchan;
- /* Get the owner of the blocking lock */
- waitblock = waitblock->lf_next;
- if ((waitblock->lf_flags & F_POSIX) == 0)
- break;
- wproc = (struct proc *)waitblock->lf_id;
- if (wproc == (struct proc *)lock->lf_id) {
- free(lock, M_LOCKF);
- return (EDEADLK);
- }
- }
- }
- /*
- * Add our lock to the blocked
- * list and sleep until we're free.
- */
-#ifdef LOCKF_DEBUG
- if (lockf_debug & 4)
- lf_print("ufs_advlock: blocking on", block);
-#endif /* LOCKF_DEBUG */
- /*
- * Remember who blocked us (for deadlock detection)
- */
- lock->lf_next = block;
- lf_addblock(block, lock);
- if (error = tsleep((caddr_t *)lock, priority, lockstr, 0)) {
- free(lock, M_LOCKF);
- return (error);
- }
- }
- /*
- * No blocks!! Add the lock. Note that addlock will
- * downgrade or upgrade any overlapping locks this
- * process already owns.
- */
-#ifdef LOCKF_DEBUG
- if (lockf_debug & 4)
- lf_print("ufs_advlock: got the lock", lock);
-#endif /* LOCKF_DEBUG */
- lf_addlock(lock);
- return (0);
-}
-
-/*
- * Remove a byte-range lock on an inode.
- */
-ufs_advunlock(lock)
- struct lockf *lock;
-{
- struct lockf *blocklist;
-
- if (lock->lf_inode->i_lockf == (struct lockf *)0)
- return (0);
-#ifdef LOCKF_DEBUG
- if (lockf_debug & 4)
- lf_print("ufs_advunlock", lock);
-#endif /* LOCKF_DEBUG */
- /*
- * Generally, find the lock (or an overlap to that lock)
- * and remove it (or shrink it), then wakeup anyone we can.
- */
- blocklist = lf_remove(lock);
- FREE(lock, M_LOCKF);
- lf_wakelock(blocklist);
- return (0);
-}
-
-/*
- * Return the blocking pid
- */
-ufs_advgetlock(lock, fl)
- register struct lockf *lock;
- register struct flock *fl;
-{
- register struct lockf *block;
- off_t start, end;
-
-#ifdef LOCKF_DEBUG
- if (lockf_debug & 4)
- lf_print("ufs_advgetlock", lock);
-#endif /* LOCKF_DEBUG */
-
- if (block = lf_getblock(lock)) {
- fl->l_type = block->lf_type;
- fl->l_whence = SEEK_SET;
- fl->l_start = block->lf_start;
- if (block->lf_end == -1)
- fl->l_len = 0;
- else
- fl->l_len = block->lf_end - block->lf_start;
- if (block->lf_flags & F_POSIX)
- fl->l_pid = ((struct proc *)(block->lf_id))->p_pid;
- else
- fl->l_pid = -1;
- }
- FREE(lock, M_LOCKF);
- return (0);
-}