+ dirsize = roundup(dp->i_size, DIRBLKSIZ);
+ u.u_offset = 0;
+ while (u.u_offset < dirsize) {
+ /*
+ * If offset is on a block boundary,
+ * read the next directory block.
+ * Release previous if it exists.
+ */
+ if (blkoff(fs, u.u_offset) == 0) {
+ if (bp != NULL)
+ brelse(bp);
+ bp = blkatoff(dp, u.u_offset, (char **)0);
+ if (bp == 0)
+ goto bad;
+ entryoffsetinblock = 0;
+ }
+
+ /*
+ * If still looking for a slot, and at a DIRBLKSIZE
+ * boundary, have to start looking for free space
+ * again.
+ */
+ if (slotstatus == NONE &&
+ (entryoffsetinblock&(DIRBLKSIZ-1)) == 0) {
+ slotoffset = -1;
+ slotfreespace = 0;
+ }
+
+ /*
+ * Get pointer to next entry, and do consistency checking:
+ * record length must be multiple of 4
+ * record length must not be zero
+ * entry must fit in rest of this DIRBLKSIZ block
+ * record must be large enough to contain name
+ * When dirchk is set we also check:
+ * name is not longer than MAXNAMLEN
+ * name must be as long as advertised, and null terminated
+ * Checking last two conditions is done only when dirchk is
+ * set, to save time.
+ */
+ ep = (struct direct *)(bp->b_un.b_addr + entryoffsetinblock);
+ i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
+ if ((ep->d_reclen & 0x3) || ep->d_reclen == 0 ||
+ ep->d_reclen > i || DIRSIZ(ep) > ep->d_reclen ||
+ dirchk && (ep->d_namlen > MAXNAMLEN || dirbadname(ep))) {
+ dirbad(dp, "mangled entry");
+ u.u_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 = u.u_offset;
+ slotsize = ep->d_reclen;
+ } else if (slotstatus == NONE) {
+ slotfreespace += size;
+ if (slotoffset == -1)
+ slotoffset = u.u_offset;
+ if (slotfreespace >= slotneeded) {
+ slotstatus = COMPACT;
+ slotsize =
+ u.u_offset+ep->d_reclen -
+ slotoffset;
+ }
+ }
+ }
+ }
+
+ /*
+ * Check for a name match.
+ */
+ if (ep->d_ino) {
+ if (ep->d_namlen == u.u_dent.d_namlen &&
+ !bcmp(u.u_dent.d_name, ep->d_name, ep->d_namlen))
+ goto found;
+ }
+ prevoff = u.u_offset;
+ u.u_offset += ep->d_reclen;
+ entryoffsetinblock += ep->d_reclen;
+ }
+/* notfound: */
+ /*
+ * 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 && *cp == 0 && dp->i_nlink != 0) {
+ /*
+ * Access for write is interpreted as allowing
+ * creation of files in the directory.
+ */
+ if (access(dp, IWRITE))
+ goto bad;
+ /*
+ * Return an indication of where the new directory
+ * entry should be put. If we didn't find a slot,
+ * then set u.u_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 [u.u_offset..u.u_offset+u.u_count)
+ */
+ if (slotstatus == NONE)
+ u.u_count = 0;
+ else {
+ u.u_offset = slotoffset;
+ u.u_count = slotsize;
+ }
+ dp->i_flag |= IUPD|ICHG;
+ if (bp)
+ brelse(bp);
+ brelse(nbp);
+ /*
+ * 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 NULL to indicate that the entry doesn't
+ * currently exist, leaving a pointer to the (locked)
+ * directory inode in u.u_pdir.
+ */
+ u.u_pdir = dp;
+ return (NULL);
+ }
+ u.u_error = ENOENT;
+ goto bad;
+found:
+ /*
+ * Check that directory length properly reflects presence
+ * of this entry.
+ */
+ if (entryoffsetinblock + DIRSIZ(ep) > dp->i_size) {
+ dirbad(dp, "i_size too small");
+ dp->i_size = entryoffsetinblock + DIRSIZ(ep);
+ dp->i_flag |= IUPD|ICHG;
+ }