get rid of unneeded namei
[unix-history] / usr / src / sys / ufs / lfs / lfs_vnops.c
index 706e6e6..fc45c08 100644 (file)
@@ -2,38 +2,31 @@
  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  *
  *
- *     @(#)lfs_vnops.c 7.42 (Berkeley) %G%
+ *     @(#)lfs_vnops.c 7.59 (Berkeley) %G%
  */
 
 #include "param.h"
 #include "systm.h"
  */
 
 #include "param.h"
 #include "systm.h"
-#include "user.h"
+#include "namei.h"
+#include "resourcevar.h"
 #include "kernel.h"
 #include "file.h"
 #include "stat.h"
 #include "buf.h"
 #include "proc.h"
 #include "kernel.h"
 #include "file.h"
 #include "stat.h"
 #include "buf.h"
 #include "proc.h"
-#include "socket.h"
-#include "socketvar.h"
 #include "conf.h"
 #include "mount.h"
 #include "vnode.h"
 #include "specdev.h"
 #include "conf.h"
 #include "mount.h"
 #include "vnode.h"
 #include "specdev.h"
-#include "../ufs/quota.h"
-#include "../ufs/inode.h"
-#include "../ufs/fs.h"
+#include "fcntl.h"
+#include "malloc.h"
+
+#include "lockf.h"
+#include "quota.h"
+#include "inode.h"
+#include "fs.h"
 
 /*
  * Global vfs data structures for ufs
 
 /*
  * Global vfs data structures for ufs
@@ -70,7 +63,8 @@ int   ufs_lookup(),
        ufs_bmap(),
        ufs_strategy(),
        ufs_print(),
        ufs_bmap(),
        ufs_strategy(),
        ufs_print(),
-       ufs_islocked();
+       ufs_islocked(),
+       ufs_advlock();
 
 struct vnodeops ufs_vnodeops = {
        ufs_lookup,             /* lookup */
 
 struct vnodeops ufs_vnodeops = {
        ufs_lookup,             /* lookup */
@@ -105,6 +99,7 @@ struct vnodeops ufs_vnodeops = {
        ufs_strategy,           /* strategy */
        ufs_print,              /* print */
        ufs_islocked,           /* islocked */
        ufs_strategy,           /* strategy */
        ufs_print,              /* print */
        ufs_islocked,           /* islocked */
+       ufs_advlock,            /* advlock */
 };
 
 int    spec_lookup(),
 };
 
 int    spec_lookup(),
@@ -116,8 +111,8 @@ int spec_lookup(),
        spec_ioctl(),
        spec_select(),
        ufsspec_close(),
        spec_ioctl(),
        spec_select(),
        ufsspec_close(),
-       spec_badop(),
-       spec_nullop();
+       spec_advlock(),
+       spec_badop();
 
 struct vnodeops spec_inodeops = {
        spec_lookup,            /* lookup */
 
 struct vnodeops spec_inodeops = {
        spec_lookup,            /* lookup */
@@ -133,7 +128,7 @@ struct vnodeops spec_inodeops = {
        spec_ioctl,             /* ioctl */
        spec_select,            /* select */
        spec_badop,             /* mmap */
        spec_ioctl,             /* ioctl */
        spec_select,            /* select */
        spec_badop,             /* mmap */
-       spec_nullop,            /* fsync */
+       nullop,                 /* fsync */
        spec_badop,             /* seek */
        spec_badop,             /* remove */
        spec_badop,             /* link */
        spec_badop,             /* seek */
        spec_badop,             /* remove */
        spec_badop,             /* link */
@@ -152,6 +147,7 @@ struct vnodeops spec_inodeops = {
        spec_strategy,          /* strategy */
        ufs_print,              /* print */
        ufs_islocked,           /* islocked */
        spec_strategy,          /* strategy */
        ufs_print,              /* print */
        ufs_islocked,           /* islocked */
+       spec_advlock,           /* advlock */
 };
 
 #ifdef FIFO
 };
 
 #ifdef FIFO
@@ -164,8 +160,8 @@ int fifo_lookup(),
        fifo_select(),
        ufsfifo_close(),
        fifo_print(),
        fifo_select(),
        ufsfifo_close(),
        fifo_print(),
-       fifo_badop(),
-       fifo_nullop();
+       fifo_advlock(),
+       fifo_badop();
 
 struct vnodeops fifo_inodeops = {
        fifo_lookup,            /* lookup */
 
 struct vnodeops fifo_inodeops = {
        fifo_lookup,            /* lookup */
@@ -181,7 +177,7 @@ struct vnodeops fifo_inodeops = {
        fifo_ioctl,             /* ioctl */
        fifo_select,            /* select */
        fifo_badop,             /* mmap */
        fifo_ioctl,             /* ioctl */
        fifo_select,            /* select */
        fifo_badop,             /* mmap */
-       fifo_nullop,            /* fsync */
+       nullop,                 /* fsync */
        fifo_badop,             /* seek */
        fifo_badop,             /* remove */
        fifo_badop,             /* link */
        fifo_badop,             /* seek */
        fifo_badop,             /* remove */
        fifo_badop,             /* link */
@@ -200,6 +196,7 @@ struct vnodeops fifo_inodeops = {
        fifo_badop,             /* strategy */
        ufs_print,              /* print */
        ufs_islocked,           /* islocked */
        fifo_badop,             /* strategy */
        ufs_print,              /* print */
        ufs_islocked,           /* islocked */
+       fifo_advlock,           /* advlock */
 };
 #endif /* FIFO */
 
 };
 #endif /* FIFO */
 
@@ -403,6 +400,7 @@ ufs_setattr(vp, vap, cred)
        register struct vattr *vap;
        register struct ucred *cred;
 {
        register struct vattr *vap;
        register struct ucred *cred;
 {
+       struct proc *p = curproc;               /* XXX */
        register struct inode *ip = VTOI(vp);
        int error = 0;
 
        register struct inode *ip = VTOI(vp);
        int error = 0;
 
@@ -419,7 +417,7 @@ ufs_setattr(vp, vap, cred)
         * Go through the fields and update iff not VNOVAL.
         */
        if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
         * Go through the fields and update iff not VNOVAL.
         */
        if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)
-               if (error = chown1(vp, vap->va_uid, vap->va_gid, cred))
+               if (error = chown1(vp, vap->va_uid, vap->va_gid, p))
                        return (error);
        if (vap->va_size != VNOVAL) {
                if (vp->v_type == VDIR)
                        return (error);
        if (vap->va_size != VNOVAL) {
                if (vp->v_type == VDIR)
@@ -429,7 +427,7 @@ ufs_setattr(vp, vap, cred)
        }
        if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
                if (cred->cr_uid != ip->i_uid &&
        }
        if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
                if (cred->cr_uid != ip->i_uid &&
-                   (error = suser(cred, &u.u_acflag)))
+                   (error = suser(cred, &p->p_acflag)))
                        return (error);
                if (vap->va_atime.tv_sec != VNOVAL)
                        ip->i_flag |= IACC;
                        return (error);
                if (vap->va_atime.tv_sec != VNOVAL)
                        ip->i_flag |= IACC;
@@ -440,10 +438,10 @@ ufs_setattr(vp, vap, cred)
                        return (error);
        }
        if (vap->va_mode != (u_short)VNOVAL)
                        return (error);
        }
        if (vap->va_mode != (u_short)VNOVAL)
-               error = chmod1(vp, (int)vap->va_mode, cred);
+               error = chmod1(vp, (int)vap->va_mode, p);
        if (vap->va_flags != VNOVAL) {
                if (cred->cr_uid != ip->i_uid &&
        if (vap->va_flags != VNOVAL) {
                if (cred->cr_uid != ip->i_uid &&
-                   (error = suser(cred, &u.u_acflag)))
+                   (error = suser(cred, &p->p_acflag)))
                        return (error);
                if (cred->cr_uid == 0) {
                        ip->i_flags = vap->va_flags;
                        return (error);
                if (cred->cr_uid == 0) {
                        ip->i_flags = vap->va_flags;
@@ -460,28 +458,29 @@ ufs_setattr(vp, vap, cred)
  * Change the mode on a file.
  * Inode must be locked before calling.
  */
  * Change the mode on a file.
  * Inode must be locked before calling.
  */
-chmod1(vp, mode, cred)
+chmod1(vp, mode, p)
        register struct vnode *vp;
        register int mode;
        register struct vnode *vp;
        register int mode;
-       struct ucred *cred;
+       struct proc *p;
 {
 {
+       register struct ucred *cred = p->p_ucred;
        register struct inode *ip = VTOI(vp);
        int error;
 
        if (cred->cr_uid != ip->i_uid &&
        register struct inode *ip = VTOI(vp);
        int error;
 
        if (cred->cr_uid != ip->i_uid &&
-           (error = suser(cred, &u.u_acflag)))
+           (error = suser(cred, &p->p_acflag)))
                return (error);
                return (error);
-       ip->i_mode &= ~07777;
        if (cred->cr_uid) {
        if (cred->cr_uid) {
-               if (vp->v_type != VDIR)
-                       mode &= ~ISVTX;
-               if (!groupmember(ip->i_gid, cred))
-                       mode &= ~ISGID;
+               if (vp->v_type != VDIR && (mode & ISVTX))
+                       return (EFTYPE);
+               if (!groupmember(ip->i_gid, cred) && (mode & ISGID))
+                       return (EPERM);
        }
        }
+       ip->i_mode &= ~07777;
        ip->i_mode |= mode & 07777;
        ip->i_flag |= ICHG;
        if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
        ip->i_mode |= mode & 07777;
        ip->i_flag |= ICHG;
        if ((vp->v_flag & VTEXT) && (ip->i_mode & ISVTX) == 0)
-               xrele(vp);
+               (void) vnode_pager_uncache(vp);
        return (0);
 }
 
        return (0);
 }
 
@@ -489,13 +488,14 @@ chmod1(vp, mode, cred)
  * Perform chown operation on inode ip;
  * inode must be locked prior to call.
  */
  * Perform chown operation on inode ip;
  * inode must be locked prior to call.
  */
-chown1(vp, uid, gid, cred)
+chown1(vp, uid, gid, p)
        register struct vnode *vp;
        uid_t uid;
        gid_t gid;
        register struct vnode *vp;
        uid_t uid;
        gid_t gid;
-       struct ucred *cred;
+       struct proc *p;
 {
        register struct inode *ip = VTOI(vp);
 {
        register struct inode *ip = VTOI(vp);
+       register struct ucred *cred = p->p_ucred;
        uid_t ouid;
        gid_t ogid;
        int error = 0;
        uid_t ouid;
        gid_t ogid;
        int error = 0;
@@ -515,7 +515,7 @@ chown1(vp, uid, gid, cred)
         */
        if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
            !groupmember((gid_t)gid, cred)) &&
         */
        if ((cred->cr_uid != ip->i_uid || uid != ip->i_uid ||
            !groupmember((gid_t)gid, cred)) &&
-           (error = suser(cred, &u.u_acflag)))
+           (error = suser(cred, &p->p_acflag)))
                return (error);
        ouid = ip->i_uid;
        ogid = ip->i_gid;
                return (error);
        ouid = ip->i_uid;
        ogid = ip->i_gid;
@@ -658,13 +658,14 @@ ufs_write(vp, uio, ioflag, cred)
        int ioflag;
        struct ucred *cred;
 {
        int ioflag;
        struct ucred *cred;
 {
+       struct proc *p = curproc;               /* XXX */
        register struct inode *ip = VTOI(vp);
        register struct fs *fs;
        struct buf *bp;
        daddr_t lbn, bn;
        u_long osize;
        register struct inode *ip = VTOI(vp);
        register struct fs *fs;
        struct buf *bp;
        daddr_t lbn, bn;
        u_long osize;
-       int i, n, on, flags;
-       int count, size, resid, error = 0;
+       int n, on, flags;
+       int size, resid, error = 0;
 
        if (uio->uio_rw != UIO_WRITE)
                panic("ufs_write mode");
 
        if (uio->uio_rw != UIO_WRITE)
                panic("ufs_write mode");
@@ -694,8 +695,8 @@ ufs_write(vp, uio, ioflag, cred)
         */
        if (vp->v_type == VREG &&
            uio->uio_offset + uio->uio_resid >
         */
        if (vp->v_type == VREG &&
            uio->uio_offset + uio->uio_resid >
-             u.u_rlimit[RLIMIT_FSIZE].rlim_cur) {
-               psignal(u.u_procp, SIGXFSZ);
+             p->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
+               psignal(p, SIGXFSZ);
                return (EFBIG);
        }
        resid = uio->uio_resid;
                return (EFBIG);
        }
        resid = uio->uio_resid;
@@ -715,12 +716,12 @@ ufs_write(vp, uio, ioflag, cred)
                if (error = balloc(ip, lbn, (int)(on + n), &bp, flags))
                        break;
                bn = bp->b_blkno;
                if (error = balloc(ip, lbn, (int)(on + n), &bp, flags))
                        break;
                bn = bp->b_blkno;
-               if (uio->uio_offset + n > ip->i_size)
+               if (uio->uio_offset + n > ip->i_size) {
                        ip->i_size = uio->uio_offset + n;
                        ip->i_size = uio->uio_offset + n;
+                       vnode_pager_setsize(vp, ip->i_size);
+               }
                size = blksize(fs, ip, lbn);
                size = blksize(fs, ip, lbn);
-               count = howmany(size, CLBYTES);
-               for (i = 0; i < count; i++)
-                       munhash(ip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
+               (void) vnode_pager_uncache(vp);
                n = MIN(n, size - bp->b_resid);
                error = uiomove(bp->b_un.b_addr + on, n, uio);
                if (ioflag & IO_SYNC)
                n = MIN(n, size - bp->b_resid);
                error = uiomove(bp->b_un.b_addr + on, n, uio);
                if (ioflag & IO_SYNC)
@@ -739,6 +740,8 @@ ufs_write(vp, uio, ioflag, cred)
                uio->uio_offset -= resid - uio->uio_resid;
                uio->uio_resid = resid;
        }
                uio->uio_offset -= resid - uio->uio_resid;
                uio->uio_resid = resid;
        }
+       if (!error && (ioflag & IO_SYNC))
+               error = iupdat(ip, &time, &time, 1);
        return (error);
 }
 
        return (error);
 }
 
@@ -848,20 +851,18 @@ ufs_link(vp, ndp)
        register struct inode *ip = VTOI(vp);
        int error;
 
        register struct inode *ip = VTOI(vp);
        int error;
 
+       if ((unsigned short)ip->i_nlink >= LINK_MAX)
+               return (EMLINK);
        if (ndp->ni_dvp != vp)
                ILOCK(ip);
        if (ndp->ni_dvp != vp)
                ILOCK(ip);
-       if (ip->i_nlink == LINK_MAX - 1) {
-               error = EMLINK;
-               goto out;
-       }
        ip->i_nlink++;
        ip->i_flag |= ICHG;
        error = iupdat(ip, &time, &time, 1);
        if (!error)
                error = direnter(ip, ndp);
        ip->i_nlink++;
        ip->i_flag |= ICHG;
        error = iupdat(ip, &time, &time, 1);
        if (!error)
                error = direnter(ip, ndp);
-out:
        if (ndp->ni_dvp != vp)
                IUNLOCK(ip);
        if (ndp->ni_dvp != vp)
                IUNLOCK(ip);
+       vput(ndp->ni_dvp);
        if (error) {
                ip->i_nlink--;
                ip->i_flag |= ICHG;
        if (error) {
                ip->i_nlink--;
                ip->i_flag |= ICHG;
@@ -896,6 +897,7 @@ out:
 ufs_rename(fndp, tndp)
        register struct nameidata *fndp, *tndp;
 {
 ufs_rename(fndp, tndp)
        register struct nameidata *fndp, *tndp;
 {
+       struct proc *p = curproc;               /* XXX */
        register struct inode *ip, *xp, *dp;
        struct dirtemplate dirbuf;
        int doingdirectory = 0, oldparent = 0, newparent = 0;
        register struct inode *ip, *xp, *dp;
        struct dirtemplate dirbuf;
        int doingdirectory = 0, oldparent = 0, newparent = 0;
@@ -964,14 +966,15 @@ ufs_rename(fndp, tndp)
                VOP_UNLOCK(fndp->ni_vp);
                if (error)
                        goto bad;
                VOP_UNLOCK(fndp->ni_vp);
                if (error)
                        goto bad;
-               tndp->ni_nameiop = RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
+               tndp->ni_nameiop &= ~(MODMASK | OPMASK);
+               tndp->ni_nameiop |= RENAME | LOCKPARENT | LOCKLEAF | NOCACHE;
                do {
                        dp = VTOI(tndp->ni_dvp);
                        if (xp != NULL)
                                iput(xp);
                        if (error = checkpath(ip, dp, tndp->ni_cred))
                                goto out;
                do {
                        dp = VTOI(tndp->ni_dvp);
                        if (xp != NULL)
                                iput(xp);
                        if (error = checkpath(ip, dp, tndp->ni_cred))
                                goto out;
-                       if (error = namei(tndp))
+                       if (error = namei(tndp, p))
                                goto out;
                        xp = NULL;
                        if (tndp->ni_vp)
                                goto out;
                        xp = NULL;
                        if (tndp->ni_vp)
@@ -994,12 +997,24 @@ ufs_rename(fndp, tndp)
                 * parent we don't fool with the link count.
                 */
                if (doingdirectory && newparent) {
                 * parent we don't fool with the link count.
                 */
                if (doingdirectory && newparent) {
+                       if ((unsigned short)dp->i_nlink >= LINK_MAX) {
+                               error = EMLINK;
+                               goto bad;
+                       }
                        dp->i_nlink++;
                        dp->i_flag |= ICHG;
                        dp->i_nlink++;
                        dp->i_flag |= ICHG;
-                       error = iupdat(dp, &time, &time, 1);
+                       if (error = iupdat(dp, &time, &time, 1))
+                               goto bad;
+               }
+               if (error = direnter(ip, tndp)) {
+                       if (doingdirectory && newparent) {
+                               dp->i_nlink--;
+                               dp->i_flag |= ICHG;
+                               (void) iupdat(dp, &time, &time, 1);
+                       }
+                       goto bad;
                }
                }
-               if (error = direnter(ip, tndp))
-                       goto out;
+               iput(dp);
        } else {
                if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
                        panic("rename: EXDEV");
        } else {
                if (xp->i_dev != dp->i_dev || xp->i_dev != ip->i_dev)
                        panic("rename: EXDEV");
@@ -1044,6 +1059,16 @@ ufs_rename(fndp, tndp)
                }
                if (error = dirrewrite(dp, ip, tndp))
                        goto bad;
                }
                if (error = dirrewrite(dp, ip, tndp))
                        goto bad;
+               /*
+                * If the target directory is in the same
+                * directory as the source directory,
+                * decrement the link count on the parent
+                * of the target directory.
+                */
+                if (doingdirectory && !newparent) {
+                       dp->i_nlink--;
+                       dp->i_flag |= ICHG;
+               }
                vput(ITOV(dp));
                /*
                 * Adjust the link count of the target to
                vput(ITOV(dp));
                /*
                 * Adjust the link count of the target to
@@ -1069,16 +1094,20 @@ ufs_rename(fndp, tndp)
        /*
         * 3) Unlink the source.
         */
        /*
         * 3) Unlink the source.
         */
-       fndp->ni_nameiop = DELETE | LOCKPARENT | LOCKLEAF;
-       (void)namei(fndp);
+       fndp->ni_nameiop &= ~(MODMASK | OPMASK);
+       fndp->ni_nameiop |= DELETE | LOCKPARENT | LOCKLEAF;
+       (void)namei(fndp, p);
        if (fndp->ni_vp != NULL) {
                xp = VTOI(fndp->ni_vp);
                dp = VTOI(fndp->ni_dvp);
        } else {
        if (fndp->ni_vp != NULL) {
                xp = VTOI(fndp->ni_vp);
                dp = VTOI(fndp->ni_dvp);
        } else {
-               if (fndp->ni_dvp != NULL)
-                       vput(fndp->ni_dvp);
-               xp = NULL;
-               dp = NULL;
+               /*
+                * From name has disappeared.
+                */
+               if (doingdirectory)
+                       panic("rename: lost dir entry");
+               vrele(ITOV(ip));
+               return (0);
        }
        /*
         * Ensure that the directory entry still exists and has not
        }
        /*
         * Ensure that the directory entry still exists and has not
@@ -1173,6 +1202,10 @@ ufs_mkdir(ndp, vap)
 
        dvp = ndp->ni_dvp;
        dp = VTOI(dvp);
 
        dvp = ndp->ni_dvp;
        dp = VTOI(dvp);
+       if ((unsigned short)dp->i_nlink >= LINK_MAX) {
+               iput(dp);
+               return (EMLINK);
+       }
        dmode = vap->va_mode&0777;
        dmode |= IFDIR;
        /*
        dmode = vap->va_mode&0777;
        dmode |= IFDIR;
        /*
@@ -1212,7 +1245,8 @@ ufs_mkdir(ndp, vap)
         */
        dp->i_nlink++;
        dp->i_flag |= ICHG;
         */
        dp->i_nlink++;
        dp->i_flag |= ICHG;
-       error = iupdat(dp, &time, &time, 1);
+       if (error = iupdat(dp, &time, &time, 1))
+               goto bad;
 
        /*
         * Initialize directory with "."
 
        /*
         * Initialize directory with "."
@@ -1229,25 +1263,20 @@ ufs_mkdir(ndp, vap)
                dp->i_flag |= ICHG;
                goto bad;
        }
                dp->i_flag |= ICHG;
                goto bad;
        }
-       if (DIRBLKSIZ > dp->i_fs->fs_fsize)
+       if (DIRBLKSIZ > dp->i_fs->fs_fsize) {
                panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
                panic("mkdir: blksize");     /* XXX - should grow w/balloc() */
-       else
+       } else {
                ip->i_size = DIRBLKSIZ;
                ip->i_size = DIRBLKSIZ;
+               ip->i_flag |= ICHG;
+       }
        /*
         * Directory all set up, now
         * install the entry for it in
         * the parent directory.
         */
        /*
         * 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;
-               }
+       if (error = direnter(ip, ndp)) {
+               dp->i_nlink--;
+               dp->i_flag |= ICHG;
        }
 bad:
        /*
        }
 bad:
        /*
@@ -1261,8 +1290,7 @@ bad:
                iput(ip);
        } else
                ndp->ni_vp = ITOV(ip);
                iput(ip);
        } else
                ndp->ni_vp = ITOV(ip);
-       if (dp)
-               iput(dp);
+       iput(dp);
        return (error);
 }
 
        return (error);
 }
 
@@ -1693,10 +1721,9 @@ maknode(mode, ndp, ipp)
         */
        if (error = iupdat(ip, &time, &time, 1))
                goto bad;
         */
        if (error = iupdat(ip, &time, &time, 1))
                goto bad;
-       if (error = direnter(ip, ndp)) {
-               pdir = NULL;
+       if (error = direnter(ip, ndp))
                goto bad;
                goto bad;
-       }
+       iput(pdir);
        *ipp = ip;
        return (0);
 
        *ipp = ip;
        return (0);
 
@@ -1705,10 +1732,96 @@ bad:
         * Write error occurred trying to update the inode
         * or the directory so must deallocate the inode.
         */
         * Write error occurred trying to update the inode
         * or the directory so must deallocate the inode.
         */
-       if (pdir)
-               iput(pdir);
+       iput(pdir);
        ip->i_nlink = 0;
        ip->i_flag |= ICHG;
        iput(ip);
        return (error);
 }
        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 - 1;
+       /*
+        * 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 (lf_setlock(lock));
+
+       case F_UNLCK:
+               error = lf_clearlock(lock);
+               FREE(lock, M_LOCKF);
+               return (error);
+
+       case F_GETLK:
+               error = lf_getlock(lock, fl);
+               FREE(lock, M_LOCKF);
+               return (error);
+       
+       default:
+               free(lock, M_LOCKF);
+               return (EINVAL);
+       }
+       /* NOTREACHED */
+}