+ dirbad(dp, ndp->ni_offset, "mangled entry");
+ i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
+ ndp->ni_offset += i;
+ entryoffsetinblock += i;
+ continue;
+ }
+
+ /*
+ * If an appropriate sized slot has not yet been found,
+ * check to see if one is available. Also accumulate space
+ * in the current block so that we can determine if
+ * compaction is viable.
+ */
+ if (slotstatus != FOUND) {
+ int size = ep->d_reclen;
+
+ if (ep->d_ino != 0)
+ size -= DIRSIZ(ep);
+ if (size > 0) {
+ if (size >= slotneeded) {
+ slotstatus = FOUND;
+ slotoffset = ndp->ni_offset;
+ slotsize = ep->d_reclen;
+ } else if (slotstatus == NONE) {
+ slotfreespace += size;
+ if (slotoffset == -1)
+ slotoffset = ndp->ni_offset;
+ if (slotfreespace >= slotneeded) {
+ slotstatus = COMPACT;
+ slotsize = ndp->ni_offset +
+ ep->d_reclen - slotoffset;
+ }
+ }
+ }
+ }
+
+ /*
+ * Check for a name match.
+ */
+ if (ep->d_ino) {
+ if (ep->d_namlen == ndp->ni_dent.d_namlen &&
+ !bcmp(ndp->ni_ptr, ep->d_name,
+ (unsigned)ep->d_namlen)) {
+ /*
+ * Save directory entry's inode number and
+ * reclen in ndp->ni_dent, and release
+ * directory buffer.
+ */
+ ndp->ni_dent.d_ino = ep->d_ino;
+ ndp->ni_dent.d_reclen = ep->d_reclen;
+ brelse(bp);
+ goto found;
+ }
+ }
+ prevoff = ndp->ni_offset;
+ ndp->ni_offset += ep->d_reclen;
+ entryoffsetinblock += ep->d_reclen;
+ if (ep->d_ino)
+ enduseful = ndp->ni_offset;
+ }
+/* notfound: */
+ /*
+ * If we started in the middle of the directory and failed
+ * to find our target, we must check the beginning as well.
+ */
+ if (numdirpasses == 2) {
+ numdirpasses--;
+ ndp->ni_offset = 0;
+ endsearch = dp->i_diroff;
+ goto searchloop;
+ }
+ if (bp != NULL)
+ brelse(bp);
+ /*
+ * If creating, and at end of pathname and current
+ * directory has not been removed, then can consider
+ * allowing file to be created.
+ */
+ if ((flag == CREATE || flag == RENAME) &&
+ *ndp->ni_next == 0 && dp->i_nlink != 0) {
+ /*
+ * Access for write is interpreted as allowing
+ * creation of files in the directory.
+ */
+ if (error = ufs_access(vdp, VWRITE, ndp->ni_cred, p))
+ return (error);
+ /*
+ * Return an indication of where the new directory
+ * entry should be put. If we didn't find a slot,
+ * then set ndp->ni_count to 0 indicating that the new
+ * slot belongs at the end of the directory. If we found
+ * a slot, then the new entry can be put in the range
+ * [ndp->ni_offset .. ndp->ni_offset + ndp->ni_count)
+ */
+ if (slotstatus == NONE) {
+ ndp->ni_offset = roundup(dp->i_size, DIRBLKSIZ);
+ ndp->ni_count = 0;
+ enduseful = ndp->ni_offset;
+ } else {
+ ndp->ni_offset = slotoffset;
+ ndp->ni_count = slotsize;
+ if (enduseful < slotoffset + slotsize)
+ enduseful = slotoffset + slotsize;
+ }
+ ndp->ni_endoff = roundup(enduseful, DIRBLKSIZ);
+ dp->i_flag |= IUPD|ICHG;
+ /*
+ * We return with the directory locked, so that
+ * the parameters we set up above will still be
+ * valid if we actually decide to do a direnter().
+ * We return ni_vp == NULL to indicate that the entry
+ * does not currently exist; we leave a pointer to
+ * the (locked) directory inode in ndp->ni_dvp.
+ *
+ * NB - if the directory is unlocked, then this
+ * information cannot be used.
+ */
+ if (!lockparent)
+ IUNLOCK(dp);
+ }