2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
39 * $FreeBSD: src/sys/ufs/ufs/ufs_lookup.c,v 1.33.2.7 2001/09/22 19:22:13 iedowse Exp $
40 * $DragonFly: src/sys/vfs/ufs/ufs_lookup.c,v 1.25 2006/05/06 02:43:14 dillon Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
50 #include <sys/namei.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/sysctl.h>
57 #include <vm/vm_extern.h>
66 #include "ufs_extern.h"
74 SYSCTL_INT(_debug, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
76 /* true if old FS format...*/
77 #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
80 * Convert a component of a pathname into a pointer to a locked inode.
81 * This is a very central and rather complicated routine.
82 * If the filesystem is not maintained in a strict tree hierarchy,
83 * this can result in a deadlock situation (see comments in code below).
85 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
86 * on whether the name is to be looked up, created, renamed, or deleted.
87 * When CREATE, RENAME, or DELETE is specified, information usable in
88 * creating, renaming, or deleting a directory entry may be calculated.
89 * If flag has LOCKPARENT or'ed into it and the target of the pathname
90 * exists, lookup returns both the target and its parent directory locked.
91 * When creating or renaming and LOCKPARENT is specified, the target may
92 * not be ".". When deleting and LOCKPARENT is specified, the target may
93 * be "."., but the caller must check to ensure it does an vrele and vput
94 * instead of two vputs.
96 * Overall outline of ufs_lookup:
98 * search for name in directory, to found or notfound
100 * if creating, return locked directory, leaving info on available slots
103 * if at end of path and deleting, return information to allow delete
104 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
105 * inode and return info to allow rewrite
106 * if not at end, add name to cache; if at end and neither creating
107 * nor deleting, add name to cache
109 * ufs_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
110 * struct componentname *a_cnp)
113 ufs_lookup(struct vop_old_lookup_args *ap)
115 struct vnode *vdp; /* vnode for directory being searched */
116 struct inode *dp; /* inode for directory being searched */
117 struct buf *bp; /* a buffer of directory entries */
118 struct direct *ep; /* the current directory entry */
119 int entryoffsetinblock; /* offset of ep in bp's buffer */
120 enum {NONE, COMPACT, FOUND} slotstatus;
121 doff_t slotoffset; /* offset of area with free space */
122 int slotsize; /* size of area at slotoffset */
123 int slotfreespace; /* amount of space free in slot */
124 int slotneeded; /* size of the entry we're seeking */
125 int numdirpasses; /* strategy for directory search */
126 doff_t endsearch; /* offset to end directory search */
127 doff_t prevoff; /* prev entry dp->i_offset */
128 struct vnode *pdp; /* saved dp during symlink work */
129 struct vnode *tdp; /* returned by VFS_VGET */
130 doff_t enduseful; /* pointer past last used dir slot */
131 u_long bmask; /* block offset mask */
132 int lockparent; /* 1 => lockparent flag is set */
133 int wantparent; /* 1 => wantparent or lockparent flag */
135 struct vnode **vpp = ap->a_vpp;
136 struct componentname *cnp = ap->a_cnp;
137 struct ucred *cred = cnp->cn_cred;
138 int flags = cnp->cn_flags;
139 int nameiop = cnp->cn_nameiop;
140 globaldata_t gd = mycpu;
144 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
146 * XXX there was a soft-update diff about this I couldn't merge.
147 * I think this was the equiv.
153 lockparent = flags & CNP_LOCKPARENT;
154 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
157 * We now have a segment name to search for, and a directory to search.
159 * Suppress search for slots unless creating
160 * file and at end of pathname, in which case
161 * we watch for a place to put the new file in
162 * case it doesn't already exist.
165 slotfreespace = slotsize = slotneeded = 0;
166 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) {
168 slotneeded = DIRECTSIZ(cnp->cn_namelen);
170 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
174 * Use dirhash for fast operations on large directories. The logic
175 * to determine whether to hash the directory is contained within
176 * ufsdirhash_build(); a zero return means that it decided to hash
177 * this directory and it successfully built up the hash table.
179 if (ufsdirhash_build(dp) == 0) {
180 /* Look for a free slot if needed. */
181 enduseful = dp->i_size;
182 if (slotstatus != FOUND) {
183 slotoffset = ufsdirhash_findfree(dp, slotneeded,
185 if (slotoffset >= 0) {
186 slotstatus = COMPACT;
187 enduseful = ufsdirhash_enduseful(dp);
189 enduseful = dp->i_size;
192 /* Look up the component. */
194 entryoffsetinblock = 0; /* silence compiler warning */
195 switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
196 &dp->i_offset, &bp, nameiop == NAMEI_DELETE ? &prevoff : NULL)) {
198 ep = (struct direct *)((char *)bp->b_data +
199 (dp->i_offset & bmask));
202 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
205 /* Something failed; just do a linear search. */
209 #endif /* UFS_DIRHASH */
211 * If there is cached information on a previous search of
212 * this directory, pick up where we last left off.
213 * We cache only lookups as these are the most common
214 * and have the greatest payoff. Caching CREATE has little
215 * benefit as it usually must search the entire directory
216 * to determine that the entry does not exist. Caching the
217 * location of the last DELETE or RENAME has not reduced
218 * profiling time and hence has been removed in the interest
221 if (nameiop != NAMEI_LOOKUP || dp->i_diroff == 0 ||
222 dp->i_diroff >= dp->i_size) {
223 entryoffsetinblock = 0;
227 dp->i_offset = dp->i_diroff;
228 if ((entryoffsetinblock = dp->i_offset & bmask) &&
229 (error = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
232 gd->gd_nchstats->ncs_2passes++;
234 prevoff = dp->i_offset;
235 endsearch = roundup2(dp->i_size, DIRBLKSIZ);
239 while (dp->i_offset < endsearch) {
241 * If necessary, get the next directory block.
243 if ((dp->i_offset & bmask) == 0) {
247 UFS_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp);
250 entryoffsetinblock = 0;
253 * If still looking for a slot, and at a DIRBLKSIZE
254 * boundary, have to start looking for free space again.
256 if (slotstatus == NONE &&
257 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
262 * Get pointer to next entry.
263 * Full validation checks are slow, so we only check
264 * enough to insure forward progress through the
265 * directory. Complete checks can be run by patching
266 * "dirchk" to be true.
268 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
269 if (ep->d_reclen == 0 || ep->d_reclen >
270 DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
271 (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) {
274 ufs_dirbad(dp, dp->i_offset, "mangled entry");
275 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
277 entryoffsetinblock += i;
282 * If an appropriate sized slot has not yet been found,
283 * check to see if one is available. Also accumulate space
284 * in the current block so that we can determine if
285 * compaction is viable.
287 if (slotstatus != FOUND) {
288 int size = ep->d_reclen;
291 size -= DIRSIZ(OFSFMT(vdp), ep);
293 if (size >= slotneeded) {
295 slotoffset = dp->i_offset;
296 slotsize = ep->d_reclen;
297 } else if (slotstatus == NONE) {
298 slotfreespace += size;
299 if (slotoffset == -1)
300 slotoffset = dp->i_offset;
301 if (slotfreespace >= slotneeded) {
302 slotstatus = COMPACT;
303 slotsize = dp->i_offset +
304 ep->d_reclen - slotoffset;
311 * Check for a name match.
314 # if (BYTE_ORDER == LITTLE_ENDIAN)
318 namlen = ep->d_namlen;
320 namlen = ep->d_namlen;
322 if (namlen == cnp->cn_namelen &&
323 (cnp->cn_nameptr[0] == ep->d_name[0]) &&
324 !bcmp(cnp->cn_nameptr, ep->d_name,
330 * Save directory entry's inode number and
331 * reclen in ndp->ni_ufs area, and release
334 if (vdp->v_mount->mnt_maxsymlinklen > 0 &&
335 ep->d_type == DT_WHT) {
337 slotoffset = dp->i_offset;
338 slotsize = ep->d_reclen;
339 dp->i_reclen = slotsize;
340 enduseful = dp->i_size;
341 ap->a_cnp->cn_flags |= CNP_ISWHITEOUT;
345 dp->i_ino = ep->d_ino;
346 dp->i_reclen = ep->d_reclen;
350 prevoff = dp->i_offset;
351 dp->i_offset += ep->d_reclen;
352 entryoffsetinblock += ep->d_reclen;
354 enduseful = dp->i_offset;
358 * If we started in the middle of the directory and failed
359 * to find our target, we must check the beginning as well.
361 if (numdirpasses == 2) {
364 endsearch = dp->i_diroff;
370 * If creating, and at end of pathname and current
371 * directory has not been removed, then can consider
372 * allowing file to be created.
374 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME ||
375 (nameiop == NAMEI_DELETE &&
376 (ap->a_cnp->cn_flags & CNP_DOWHITEOUT) &&
377 (ap->a_cnp->cn_flags & CNP_ISWHITEOUT))) &&
378 dp->i_effnlink != 0) {
380 * Access for write is interpreted as allowing
381 * creation of files in the directory.
383 error = VOP_ACCESS(vdp, VWRITE, cred);
387 * Return an indication of where the new directory
388 * entry should be put. If we didn't find a slot,
389 * then set dp->i_count to 0 indicating
390 * that the new slot belongs at the end of the
391 * directory. If we found a slot, then the new entry
392 * can be put in the range from dp->i_offset to
393 * dp->i_offset + dp->i_count.
395 if (slotstatus == NONE) {
396 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
398 enduseful = dp->i_offset;
399 } else if (nameiop == NAMEI_DELETE) {
400 dp->i_offset = slotoffset;
401 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
404 dp->i_count = dp->i_offset - prevoff;
406 dp->i_offset = slotoffset;
407 dp->i_count = slotsize;
408 if (enduseful < slotoffset + slotsize)
409 enduseful = slotoffset + slotsize;
411 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
412 dp->i_flag |= IN_CHANGE | IN_UPDATE;
414 * We return with the directory locked, so that
415 * the parameters we set up above will still be
416 * valid if we actually decide to do a direnter().
417 * We return ni_vp == NULL to indicate that the entry
418 * does not currently exist; we leave a pointer to
419 * the (locked) directory inode in ndp->ni_dvp.
420 * The pathname buffer is saved so that the name
421 * can be obtained later.
423 * NB - if the directory is unlocked, then this
424 * information cannot be used.
428 cnp->cn_flags |= CNP_PDIRUNLOCK;
430 return (EJUSTRETURN);
435 if (numdirpasses == 2)
436 gd->gd_nchstats->ncs_pass2++;
438 * Check that directory length properly reflects presence
441 if (dp->i_offset + DIRSIZ(OFSFMT(vdp), ep) > dp->i_size) {
442 ufs_dirbad(dp, dp->i_offset, "i_size too small");
443 dp->i_size = dp->i_offset + DIRSIZ(OFSFMT(vdp), ep);
444 dp->i_flag |= IN_CHANGE | IN_UPDATE;
449 * Found component in pathname.
450 * If the final component of path name, save information
451 * in the cache as to where the entry was found.
453 if (nameiop == NAMEI_LOOKUP)
454 dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
457 * If deleting, and at end of pathname, return
458 * parameters which can be used to remove file.
459 * If the wantparent flag isn't set, we return only
460 * the directory (in ndp->ni_dvp), otherwise we go
461 * on and lock the inode, being careful with ".".
463 if (nameiop == NAMEI_DELETE) {
465 * Write access to directory required to delete files.
467 error = VOP_ACCESS(vdp, VWRITE, cred);
471 * Return pointer to current entry in dp->i_offset,
472 * and distance past previous entry (if there
473 * is a previous entry in this block) in dp->i_count.
474 * Save directory inode pointer in ndp->ni_dvp for dirremove().
476 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
479 dp->i_count = dp->i_offset - prevoff;
480 if (dp->i_number == dp->i_ino) {
485 if (flags & CNP_ISDOTDOT)
486 VOP_UNLOCK(vdp, 0); /* race to get the inode */
487 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
488 if (flags & CNP_ISDOTDOT) {
489 if (vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY) != 0)
490 cnp->cn_flags |= CNP_PDIRUNLOCK;
495 * If directory is "sticky", then user must own
496 * the directory, or the file in it, else she
497 * may not delete it (unless she's root). This
498 * implements append-only directories.
500 if ((dp->i_mode & ISVTX) &&
502 cred->cr_uid != dp->i_uid &&
503 VTOI(tdp)->i_uid != cred->cr_uid) {
510 cnp->cn_flags |= CNP_PDIRUNLOCK;
516 * If rewriting (RENAME), return the inode and the
517 * information required to rewrite the present directory
518 * Must get inode of directory entry to verify it's a
519 * regular file, or empty directory.
521 if (nameiop == NAMEI_RENAME && wantparent) {
522 if ((error = VOP_ACCESS(vdp, VWRITE, cred)) != 0)
525 * Careful about locking second inode.
526 * This can only occur if the target is ".".
528 if (dp->i_number == dp->i_ino)
530 if (flags & CNP_ISDOTDOT)
531 VOP_UNLOCK(vdp, 0); /* race to get the inode */
532 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
533 if (flags & CNP_ISDOTDOT) {
534 if (vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY) != 0)
535 cnp->cn_flags |= CNP_PDIRUNLOCK;
542 cnp->cn_flags |= CNP_PDIRUNLOCK;
548 * Step through the translation in the name. We do not `vput' the
549 * directory because we may need it again if a symbolic link
550 * is relative to the current directory. Instead we save it
551 * unlocked as "pdp". We must get the target inode before unlocking
552 * the directory to insure that the inode will not be removed
553 * before we get it. We prevent deadlock by always fetching
554 * inodes from the root, moving down the directory tree. Thus
555 * when following backward pointers ".." we must unlock the
556 * parent directory before getting the requested directory.
557 * There is a potential race condition here if both the current
558 * and parent directories are removed before the VFS_VGET for the
559 * inode associated with ".." returns. We hope that this occurs
560 * infrequently since we cannot avoid this race condition without
561 * implementing a sophisticated deadlock detection algorithm.
562 * Note also that this simple deadlock detection scheme will not
563 * work if the filesystem has any hard links other than ".."
564 * that point backwards in the directory structure.
567 if (flags & CNP_ISDOTDOT) {
568 VOP_UNLOCK(pdp, 0); /* race to get the inode */
569 cnp->cn_flags |= CNP_PDIRUNLOCK;
570 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0) {
571 if (vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY) == 0)
572 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
576 if ((error = vn_lock(pdp, LK_EXCLUSIVE)) != 0) {
580 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
583 } else if (dp->i_number == dp->i_ino) {
584 vref(vdp); /* we want ourself, ie "." */
587 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
592 cnp->cn_flags |= CNP_PDIRUNLOCK;
600 ufs_dirbad(struct inode *ip, doff_t offset, char *how)
604 mp = ITOV(ip)->v_mount;
605 (void)printf("%s: bad dir ino %lu at offset %ld: %s\n",
606 mp->mnt_stat.f_mntfromname, (u_long)ip->i_number, (long)offset, how);
607 if ((mp->mnt_flag & MNT_RDONLY) == 0)
608 panic("ufs_dirbad: bad dir");
612 * Do consistency checking on a directory entry:
613 * record length must be multiple of 4
614 * entry must fit in rest of its DIRBLKSIZ block
615 * record must be large enough to contain entry
616 * name is not longer than MAXNAMLEN
617 * name must be as long as advertised, and null terminated
620 ufs_dirbadentry(struct vnode *dp, struct direct *ep, int entryoffsetinblock)
625 # if (BYTE_ORDER == LITTLE_ENDIAN)
629 namlen = ep->d_namlen;
631 namlen = ep->d_namlen;
633 if ((ep->d_reclen & 0x3) != 0 ||
634 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
635 ep->d_reclen < DIRSIZ(OFSFMT(dp), ep) || namlen > MAXNAMLEN) {
637 printf("First bad\n");
642 for (i = 0; i < namlen; i++)
643 if (ep->d_name[i] == '\0') {
645 printf("Second bad\n");
656 * Construct a new directory entry after a call to namei, using the
657 * parameters that it left in the componentname argument cnp. The
658 * argument ip is the inode to which the new directory entry will refer.
661 ufs_makedirentry(struct inode *ip, struct componentname *cnp,
662 struct direct *newdirp)
665 newdirp->d_ino = ip->i_number;
666 newdirp->d_namlen = cnp->cn_namelen;
667 bcopy(cnp->cn_nameptr, newdirp->d_name, (unsigned)cnp->cn_namelen + 1);
668 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
669 newdirp->d_type = IFTODT(ip->i_mode);
672 # if (BYTE_ORDER == LITTLE_ENDIAN)
673 { u_char tmp = newdirp->d_namlen;
674 newdirp->d_namlen = newdirp->d_type;
675 newdirp->d_type = tmp; }
681 * Write a directory entry after a call to namei, using the parameters
682 * that it left in the directory inode. The argument dirp is the new directory
683 * entry contents. Dvp is a pointer to the directory to be written,
684 * which was left locked by namei. Remaining parameters (dp->i_offset,
685 * dp->i_count) indicate how the space for the new entry is to be obtained.
686 * Non-null bp indicates that a directory is being created (for the
687 * soft dependency code).
690 ufs_direnter(struct vnode *dvp, struct vnode *tvp, struct direct *dirp,
691 struct componentname *cnp, struct buf *newdirbp)
694 struct thread *td = curthread; /* XXX */
699 struct direct *ep, *nep;
700 int error, ret, blkoff, loc, spacefree, flags;
703 KKASSERT(td->td_proc); /* YYY use/require cred passed in cnp? */
704 cred = td->td_proc->p_ucred;
707 newentrysize = DIRSIZ(OFSFMT(dvp), dirp);
709 if (dp->i_count == 0) {
711 * If dp->i_count is 0, then namei could find no
712 * space in the directory. Here, dp->i_offset will
713 * be on a directory block boundary and we will write the
714 * new entry into a fresh block.
716 if (dp->i_offset & (DIRBLKSIZ - 1))
717 panic("ufs_direnter: newblk");
718 vnode_pager_setsize(dvp, dp->i_offset + DIRBLKSIZ);
720 if (!DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp))
722 if ((error = VOP_BALLOC(dvp, (off_t)dp->i_offset, DIRBLKSIZ,
723 cred, flags, &bp)) != 0) {
724 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
728 dp->i_size = dp->i_offset + DIRBLKSIZ;
729 dp->i_flag |= IN_CHANGE | IN_UPDATE;
730 dirp->d_reclen = DIRBLKSIZ;
731 blkoff = dp->i_offset &
732 (VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_iosize - 1);
733 bcopy((caddr_t)dirp, (caddr_t)bp->b_data + blkoff,newentrysize);
735 if (dp->i_dirhash != NULL) {
736 ufsdirhash_newblk(dp, dp->i_offset);
737 ufsdirhash_add(dp, dirp, dp->i_offset);
738 ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff,
742 if (DOINGSOFTDEP(dvp)) {
744 * Ensure that the entire newly allocated block is a
745 * valid directory so that future growth within the
746 * block does not have to ensure that the block is
747 * written before the inode.
750 while (blkoff < bp->b_bcount) {
752 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
755 softdep_setup_directory_add(bp, dp, dp->i_offset,
756 dirp->d_ino, newdirbp);
758 return (UFS_UPDATE(dvp, 0));
760 if (DOINGASYNC(dvp)) {
762 return (UFS_UPDATE(dvp, 0));
765 ret = UFS_UPDATE(dvp, 1);
772 * If dp->i_count is non-zero, then namei found space for the new
773 * entry in the range dp->i_offset to dp->i_offset + dp->i_count
774 * in the directory. To use this space, we may have to compact
775 * the entries located there, by copying them together towards the
776 * beginning of the block, leaving the free space in one usable
781 * Increase size of directory if entry eats into new space.
782 * This should never push the size past a new multiple of
785 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
787 if (dp->i_offset + dp->i_count > dp->i_size)
788 dp->i_size = dp->i_offset + dp->i_count;
790 * Get the block containing the space for the new directory entry.
792 error = UFS_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp);
794 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
799 * Find space for the new entry. In the simple case, the entry at
800 * offset base will have the space. If it does not, then namei
801 * arranged that compacting the region dp->i_offset to
802 * dp->i_offset + dp->i_count would yield the space.
804 ep = (struct direct *)dirbuf;
805 dsize = ep->d_ino ? DIRSIZ(OFSFMT(dvp), ep) : 0;
806 spacefree = ep->d_reclen - dsize;
807 for (loc = ep->d_reclen; loc < dp->i_count; ) {
808 nep = (struct direct *)(dirbuf + loc);
810 /* Trim the existing slot (NB: dsize may be zero). */
811 ep->d_reclen = dsize;
812 ep = (struct direct *)((char *)ep + dsize);
814 /* Read nep->d_reclen now as the bcopy() may clobber it. */
815 loc += nep->d_reclen;
816 if (nep->d_ino == 0) {
818 * A mid-block unused entry. Such entries are
819 * never created by the kernel, but fsck_ffs
820 * can create them (and it doesn't fix them).
822 * Add up the free space, and initialise the
823 * relocated entry since we don't bcopy it.
825 spacefree += nep->d_reclen;
830 dsize = DIRSIZ(OFSFMT(dvp), nep);
831 spacefree += nep->d_reclen - dsize;
833 if (dp->i_dirhash != NULL)
834 ufsdirhash_move(dp, nep,
835 dp->i_offset + ((char *)nep - dirbuf),
836 dp->i_offset + ((char *)ep - dirbuf));
838 if (DOINGSOFTDEP(dvp))
839 softdep_change_directoryentry_offset(dp, dirbuf,
840 (caddr_t)nep, (caddr_t)ep, dsize);
842 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
845 * Here, `ep' points to a directory entry containing `dsize' in-use
846 * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
847 * then the entry is completely unused (dsize == 0). The value
848 * of ep->d_reclen is always indeterminate.
850 * Update the pointer fields in the previous entry (if any),
851 * copy in the new entry, and write out the block.
853 if (ep->d_ino == 0 ||
854 (ep->d_ino == WINO &&
855 bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
856 if (spacefree + dsize < newentrysize)
857 panic("ufs_direnter: compact1");
858 dirp->d_reclen = spacefree + dsize;
860 if (spacefree < newentrysize)
861 panic("ufs_direnter: compact2");
862 dirp->d_reclen = spacefree;
863 ep->d_reclen = dsize;
864 ep = (struct direct *)((char *)ep + dsize);
867 if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
868 dirp->d_reclen == spacefree))
869 ufsdirhash_add(dp, dirp, dp->i_offset + ((char *)ep - dirbuf));
871 bcopy((caddr_t)dirp, (caddr_t)ep, (uint)newentrysize);
873 if (dp->i_dirhash != NULL)
874 ufsdirhash_checkblock(dp, dirbuf -
875 (dp->i_offset & (DIRBLKSIZ - 1)),
876 dp->i_offset & ~(DIRBLKSIZ - 1));
879 if (DOINGSOFTDEP(dvp)) {
880 softdep_setup_directory_add(bp, dp,
881 dp->i_offset + (caddr_t)ep - dirbuf, dirp->d_ino, newdirbp);
884 if (DOINGASYNC(dvp)) {
891 dp->i_flag |= IN_CHANGE | IN_UPDATE;
893 * If all went well, and the directory can be shortened, proceed
894 * with the truncation. Note that we have to unlock the inode for
895 * the entry that we just entered, as the truncation may need to
896 * lock other inodes which can lead to deadlock if we also hold a
897 * lock on the newly entered node.
899 if (error == 0 && dp->i_endoff && dp->i_endoff < dp->i_size) {
903 if (dp->i_dirhash != NULL)
904 ufsdirhash_dirtrunc(dp, dp->i_endoff);
906 (void)UFS_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC, cred);
908 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
914 * Remove a directory entry after a call to namei, using
915 * the parameters which it left in the directory inode. The entry
916 * dp->i_offset contains the offset into the directory of the
917 * entry to be eliminated. The dp->i_count field contains the
918 * size of the previous record in the directory. If this
919 * is 0, the first entry is being deleted, so we need only
920 * zero the inode number to mark the entry as free. If the
921 * entry is not the first in the directory, we must reclaim
922 * the space of the now empty record by adding the record size
923 * to the size of the previous entry.
926 ufs_dirremove(struct vnode *dvp, struct inode *ip, int flags, int isrmdir)
935 if (flags & CNP_DOWHITEOUT) {
937 * Whiteout entry: set d_ino to WINO.
940 UFS_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) != 0)
947 if ((error = UFS_BLKATOFF(dvp,
948 (off_t)(dp->i_offset - dp->i_count), (char **)&ep, &bp)) != 0)
952 * Remove the dirhash entry. This is complicated by the fact
953 * that `ep' is the previous entry when dp->i_count != 0.
955 if (dp->i_dirhash != NULL)
956 ufsdirhash_remove(dp, (dp->i_count == 0) ? ep :
957 (struct direct *)((char *)ep + ep->d_reclen), dp->i_offset);
959 if (dp->i_count == 0) {
961 * First entry in block: set d_ino to zero.
966 * Collapse new free space into previous entry.
968 ep->d_reclen += dp->i_reclen;
971 if (dp->i_dirhash != NULL)
972 ufsdirhash_checkblock(dp, (char *)ep -
973 ((dp->i_offset - dp->i_count) & (DIRBLKSIZ - 1)),
974 dp->i_offset & ~(DIRBLKSIZ - 1));
977 if (DOINGSOFTDEP(dvp)) {
980 softdep_change_linkcnt(ip);
981 softdep_setup_remove(bp, dp, ip, isrmdir);
983 if (softdep_slowdown(dvp)) {
993 ip->i_flag |= IN_CHANGE;
995 if (flags & CNP_DOWHITEOUT)
997 else if (DOINGASYNC(dvp) && dp->i_count != 0) {
1001 error = bowrite(bp);
1003 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1008 * Rewrite an existing directory entry to point at the inode
1009 * supplied. The parameters describing the directory entry are
1010 * set up by a call to namei.
1013 ufs_dirrewrite(struct inode *dp, struct inode *oip, ino_t newinum, int newtype,
1018 struct vnode *vdp = ITOV(dp);
1021 error = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp);
1024 ep->d_ino = newinum;
1026 ep->d_type = newtype;
1028 if (DOINGSOFTDEP(vdp)) {
1029 softdep_change_linkcnt(oip);
1030 softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir);
1034 oip->i_flag |= IN_CHANGE;
1035 if (DOINGASYNC(vdp)) {
1039 error = bowrite(bp);
1042 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1047 * Check if a directory is empty or not.
1048 * Inode supplied must be locked.
1050 * Using a struct dirtemplate here is not precisely
1051 * what we want, but better than using a struct direct.
1053 * NB: does not handle corrupted directories.
1056 ufs_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
1059 struct dirtemplate dbuf;
1060 struct direct *dp = (struct direct *)&dbuf;
1061 int error, count, namlen;
1062 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1064 for (off = 0; off < ip->i_size; off += dp->d_reclen) {
1065 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
1066 UIO_SYSSPACE, IO_NODELOCKED, cred, &count);
1068 * Since we read MINDIRSIZ, residual must
1069 * be 0 unless we're at end of file.
1071 if (error || count != 0)
1073 /* avoid infinite loops */
1074 if (dp->d_reclen == 0)
1076 /* skip empty entries */
1077 if (dp->d_ino == 0 || dp->d_ino == WINO)
1079 /* accept only "." and ".." */
1080 # if (BYTE_ORDER == LITTLE_ENDIAN)
1081 if (OFSFMT(ITOV(ip)))
1082 namlen = dp->d_type;
1084 namlen = dp->d_namlen;
1086 namlen = dp->d_namlen;
1090 if (dp->d_name[0] != '.')
1093 * At this point namlen must be 1 or 2.
1094 * 1 implies ".", 2 implies ".." if second
1097 if (namlen == 1 && dp->d_ino == ip->i_number)
1099 if (dp->d_name[1] == '.' && dp->d_ino == parentino)
1107 * Check if source directory is in the path of the target directory.
1108 * Target is supplied locked, source is unlocked.
1109 * The target is always vput before returning.
1112 ufs_checkpath(struct inode *source, struct inode *target, struct ucred *cred)
1115 int error, rootino, namlen;
1116 struct dirtemplate dirbuf;
1119 if (target->i_number == source->i_number) {
1125 if (target->i_number == rootino)
1129 if (vp->v_type != VDIR) {
1133 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1134 sizeof (struct dirtemplate), (off_t)0,
1135 UIO_SYSSPACE, IO_NODELOCKED, cred, (int *)0);
1138 # if (BYTE_ORDER == LITTLE_ENDIAN)
1140 namlen = dirbuf.dotdot_type;
1142 namlen = dirbuf.dotdot_namlen;
1144 namlen = dirbuf.dotdot_namlen;
1147 dirbuf.dotdot_name[0] != '.' ||
1148 dirbuf.dotdot_name[1] != '.') {
1152 if (dirbuf.dotdot_ino == source->i_number) {
1156 if (dirbuf.dotdot_ino == rootino)
1159 error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino, &vp);
1167 if (error == ENOTDIR)
1168 printf("checkpath: .. not a directory\n");