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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_ffs_broken_fixme.h"
42 #include "opt_quota.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #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>
59 #include <ufs/ufs/extattr.h>
60 #include <ufs/ufs/quota.h>
61 #include <ufs/ufs/inode.h>
62 #include <ufs/ufs/dir.h>
64 #include <ufs/ufs/dirhash.h>
66 #include <ufs/ufs/ufsmount.h>
67 #include <ufs/ufs/ufs_extern.h>
70 static int dirchk = 1;
72 static int dirchk = 0;
75 SYSCTL_INT(_debug, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
77 /* true if old FS format...*/
78 #define OFSFMT(vp) ((vp)->v_mount->mnt_maxsymlinklen <= 0)
81 * Convert a component of a pathname into a pointer to a locked inode.
82 * This is a very central and rather complicated routine.
83 * If the filesystem is not maintained in a strict tree hierarchy,
84 * this can result in a deadlock situation (see comments in code below).
86 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
87 * on whether the name is to be looked up, created, renamed, or deleted.
88 * When CREATE, RENAME, or DELETE is specified, information usable in
89 * creating, renaming, or deleting a directory entry may be calculated.
90 * If flag has LOCKPARENT or'ed into it and the target of the pathname
91 * exists, lookup returns both the target and its parent directory locked.
92 * When creating or renaming and LOCKPARENT is specified, the target may
93 * not be ".". When deleting and LOCKPARENT is specified, the target may
94 * be "."., but the caller must check to ensure it does an vrele and vput
95 * instead of two vputs.
97 * This routine is actually used as VOP_CACHEDLOOKUP method, and the
98 * filesystem employs the generic vfs_cache_lookup() as VOP_LOOKUP
101 * vfs_cache_lookup() performs the following for us:
102 * check that it is a directory
103 * check accessibility of directory
104 * check for modification attempts on read-only mounts
105 * if name found in cache
106 * if at end of path and deleting or creating
110 * return VOP_CACHEDLOOKUP()
112 * Overall outline of ufs_lookup:
114 * search for name in directory, to found or notfound
116 * if creating, return locked directory, leaving info on available slots
119 * if at end of path and deleting, return information to allow delete
120 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
121 * inode and return info to allow rewrite
122 * if not at end, add name to cache; if at end and neither creating
123 * nor deleting, add name to cache
127 struct vop_cachedlookup_args /* {
129 struct vnode **a_vpp;
130 struct componentname *a_cnp;
133 struct vnode *vdp; /* vnode for directory being searched */
134 struct inode *dp; /* inode for directory being searched */
135 struct buf *bp; /* a buffer of directory entries */
136 struct direct *ep; /* the current directory entry */
137 int entryoffsetinblock; /* offset of ep in bp's buffer */
138 enum {NONE, COMPACT, FOUND} slotstatus;
139 doff_t slotoffset; /* offset of area with free space */
140 int slotsize; /* size of area at slotoffset */
141 int slotfreespace; /* amount of space free in slot */
142 int slotneeded; /* size of the entry we're seeking */
143 int numdirpasses; /* strategy for directory search */
144 doff_t endsearch; /* offset to end directory search */
145 doff_t prevoff; /* prev entry dp->i_offset */
146 struct vnode *pdp; /* saved dp during symlink work */
147 struct vnode *tdp; /* returned by VFS_VGET */
148 doff_t enduseful; /* pointer past last used dir slot */
149 u_long bmask; /* block offset mask */
151 struct vnode **vpp = ap->a_vpp;
152 struct componentname *cnp = ap->a_cnp;
153 struct ucred *cred = cnp->cn_cred;
154 int flags = cnp->cn_flags;
155 int nameiop = cnp->cn_nameiop;
156 struct thread *td = cnp->cn_thread;
162 * XXX there was a soft-update diff about this I couldn't merge.
163 * I think this was the equiv.
171 * Create a vm object if vmiodirenable is enabled.
172 * Alternatively we could call vnode_create_vobject
173 * in VFS_VGET but we could end up creating objects
174 * that are never used.
176 vnode_create_vobject_off(vdp, DIP(dp, i_size), cnp->cn_thread);
179 * We now have a segment name to search for, and a directory to search.
181 * Suppress search for slots unless creating
182 * file and at end of pathname, in which case
183 * we watch for a place to put the new file in
184 * case it doesn't already exist.
187 slotfreespace = slotsize = slotneeded = 0;
188 if ((nameiop == CREATE || nameiop == RENAME) &&
189 (flags & ISLASTCN)) {
191 slotneeded = DIRECTSIZ(cnp->cn_namelen);
193 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
197 * Use dirhash for fast operations on large directories. The logic
198 * to determine whether to hash the directory is contained within
199 * ufsdirhash_build(); a zero return means that it decided to hash
200 * this directory and it successfully built up the hash table.
202 if (ufsdirhash_build(dp) == 0) {
203 /* Look for a free slot if needed. */
204 enduseful = dp->i_size;
205 if (slotstatus != FOUND) {
206 slotoffset = ufsdirhash_findfree(dp, slotneeded,
208 if (slotoffset >= 0) {
209 slotstatus = COMPACT;
210 enduseful = ufsdirhash_enduseful(dp);
212 enduseful = dp->i_size;
215 /* Look up the component. */
217 entryoffsetinblock = 0; /* silence compiler warning */
218 switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
219 &dp->i_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) {
221 ep = (struct direct *)((char *)bp->b_data +
222 (dp->i_offset & bmask));
225 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
228 /* Something failed; just do a linear search. */
232 #endif /* UFS_DIRHASH */
234 * If there is cached information on a previous search of
235 * this directory, pick up where we last left off.
236 * We cache only lookups as these are the most common
237 * and have the greatest payoff. Caching CREATE has little
238 * benefit as it usually must search the entire directory
239 * to determine that the entry does not exist. Caching the
240 * location of the last DELETE or RENAME has not reduced
241 * profiling time and hence has been removed in the interest
244 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
245 dp->i_diroff >= dp->i_size) {
246 entryoffsetinblock = 0;
250 dp->i_offset = dp->i_diroff;
251 if ((entryoffsetinblock = dp->i_offset & bmask) &&
252 (error = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
255 nchstats.ncs_2passes++;
257 prevoff = dp->i_offset;
258 endsearch = roundup2(dp->i_size, DIRBLKSIZ);
262 while (dp->i_offset < endsearch) {
264 * If necessary, get the next directory block.
266 if ((dp->i_offset & bmask) == 0) {
270 UFS_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp);
273 entryoffsetinblock = 0;
276 * If still looking for a slot, and at a DIRBLKSIZE
277 * boundary, have to start looking for free space again.
279 if (slotstatus == NONE &&
280 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
285 * Get pointer to next entry.
286 * Full validation checks are slow, so we only check
287 * enough to insure forward progress through the
288 * directory. Complete checks can be run by patching
289 * "dirchk" to be true.
291 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
292 if (ep->d_reclen == 0 || ep->d_reclen >
293 DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
294 (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) {
297 ufs_dirbad(dp, dp->i_offset, "mangled entry");
298 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
300 entryoffsetinblock += i;
305 * If an appropriate sized slot has not yet been found,
306 * check to see if one is available. Also accumulate space
307 * in the current block so that we can determine if
308 * compaction is viable.
310 if (slotstatus != FOUND) {
311 int size = ep->d_reclen;
314 size -= DIRSIZ(OFSFMT(vdp), ep);
316 if (size >= slotneeded) {
318 slotoffset = dp->i_offset;
319 slotsize = ep->d_reclen;
320 } else if (slotstatus == NONE) {
321 slotfreespace += size;
322 if (slotoffset == -1)
323 slotoffset = dp->i_offset;
324 if (slotfreespace >= slotneeded) {
325 slotstatus = COMPACT;
326 slotsize = dp->i_offset +
327 ep->d_reclen - slotoffset;
334 * Check for a name match.
337 # if (BYTE_ORDER == LITTLE_ENDIAN)
341 namlen = ep->d_namlen;
343 namlen = ep->d_namlen;
345 if (namlen == cnp->cn_namelen &&
346 (cnp->cn_nameptr[0] == ep->d_name[0]) &&
347 !bcmp(cnp->cn_nameptr, ep->d_name,
353 * Save directory entry's inode number and
354 * reclen in ndp->ni_ufs area, and release
357 if (vdp->v_mount->mnt_maxsymlinklen > 0 &&
358 ep->d_type == DT_WHT) {
360 slotoffset = dp->i_offset;
361 slotsize = ep->d_reclen;
362 dp->i_reclen = slotsize;
363 enduseful = dp->i_size;
364 ap->a_cnp->cn_flags |= ISWHITEOUT;
368 dp->i_ino = ep->d_ino;
369 dp->i_reclen = ep->d_reclen;
373 prevoff = dp->i_offset;
374 dp->i_offset += ep->d_reclen;
375 entryoffsetinblock += ep->d_reclen;
377 enduseful = dp->i_offset;
381 * If we started in the middle of the directory and failed
382 * to find our target, we must check the beginning as well.
384 if (numdirpasses == 2) {
387 endsearch = dp->i_diroff;
393 * If creating, and at end of pathname and current
394 * directory has not been removed, then can consider
395 * allowing file to be created.
397 if ((nameiop == CREATE || nameiop == RENAME ||
398 (nameiop == DELETE &&
399 (ap->a_cnp->cn_flags & DOWHITEOUT) &&
400 (ap->a_cnp->cn_flags & ISWHITEOUT))) &&
401 (flags & ISLASTCN) && dp->i_effnlink != 0) {
403 * Access for write is interpreted as allowing
404 * creation of files in the directory.
406 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread);
410 * Return an indication of where the new directory
411 * entry should be put. If we didn't find a slot,
412 * then set dp->i_count to 0 indicating
413 * that the new slot belongs at the end of the
414 * directory. If we found a slot, then the new entry
415 * can be put in the range from dp->i_offset to
416 * dp->i_offset + dp->i_count.
418 if (slotstatus == NONE) {
419 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
421 enduseful = dp->i_offset;
422 } else if (nameiop == DELETE) {
423 dp->i_offset = slotoffset;
424 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
427 dp->i_count = dp->i_offset - prevoff;
429 dp->i_offset = slotoffset;
430 dp->i_count = slotsize;
431 if (enduseful < slotoffset + slotsize)
432 enduseful = slotoffset + slotsize;
434 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
436 * We return with the directory locked, so that
437 * the parameters we set up above will still be
438 * valid if we actually decide to do a direnter().
439 * We return ni_vp == NULL to indicate that the entry
440 * does not currently exist; we leave a pointer to
441 * the (locked) directory inode in ndp->ni_dvp.
442 * The pathname buffer is saved so that the name
443 * can be obtained later.
445 * NB - if the directory is unlocked, then this
446 * information cannot be used.
448 cnp->cn_flags |= SAVENAME;
449 return (EJUSTRETURN);
452 * Insert name into cache (as non-existent) if appropriate.
454 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
455 cache_enter(vdp, *vpp, cnp);
459 if (numdirpasses == 2)
460 nchstats.ncs_pass2++;
462 * Check that directory length properly reflects presence
465 if (dp->i_offset + DIRSIZ(OFSFMT(vdp), ep) > dp->i_size) {
466 ufs_dirbad(dp, dp->i_offset, "i_size too small");
467 dp->i_size = dp->i_offset + DIRSIZ(OFSFMT(vdp), ep);
468 DIP_SET(dp, i_size, dp->i_size);
469 dp->i_flag |= IN_CHANGE | IN_UPDATE;
474 * Found component in pathname.
475 * If the final component of path name, save information
476 * in the cache as to where the entry was found.
478 if ((flags & ISLASTCN) && nameiop == LOOKUP)
479 dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
482 * If deleting, and at end of pathname, return
483 * parameters which can be used to remove file.
485 if (nameiop == DELETE && (flags & ISLASTCN)) {
487 * Write access to directory required to delete files.
489 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread);
493 * Return pointer to current entry in dp->i_offset,
494 * and distance past previous entry (if there
495 * is a previous entry in this block) in dp->i_count.
496 * Save directory inode pointer in ndp->ni_dvp for dirremove().
498 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
501 dp->i_count = dp->i_offset - prevoff;
502 if (dp->i_number == dp->i_ino) {
507 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino,
508 LK_EXCLUSIVE, &tdp)) != 0)
511 * If directory is "sticky", then user must own
512 * the directory, or the file in it, else she
513 * may not delete it (unless she's root). This
514 * implements append-only directories.
516 if ((dp->i_mode & ISVTX) &&
517 VOP_ACCESS(vdp, VADMIN, cred, cnp->cn_thread) &&
518 VOP_ACCESS(tdp, VADMIN, cred, cnp->cn_thread)) {
527 * If rewriting (RENAME), return the inode and the
528 * information required to rewrite the present directory
529 * Must get inode of directory entry to verify it's a
530 * regular file, or empty directory.
532 if (nameiop == RENAME && (flags & ISLASTCN)) {
533 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)))
536 * Careful about locking second inode.
537 * This can only occur if the target is ".".
539 if (dp->i_number == dp->i_ino)
541 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino,
542 LK_EXCLUSIVE, &tdp)) != 0)
545 cnp->cn_flags |= SAVENAME;
550 * Step through the translation in the name. We do not `vput' the
551 * directory because we may need it again if a symbolic link
552 * is relative to the current directory. Instead we save it
553 * unlocked as "pdp". We must get the target inode before unlocking
554 * the directory to insure that the inode will not be removed
555 * before we get it. We prevent deadlock by always fetching
556 * inodes from the root, moving down the directory tree. Thus
557 * when following backward pointers ".." we must unlock the
558 * parent directory before getting the requested directory.
559 * There is a potential race condition here if both the current
560 * and parent directories are removed before the VFS_VGET for the
561 * inode associated with ".." returns. We hope that this occurs
562 * infrequently since we cannot avoid this race condition without
563 * implementing a sophisticated deadlock detection algorithm.
564 * Note also that this simple deadlock detection scheme will not
565 * work if the filesystem has any hard links other than ".."
566 * that point backwards in the directory structure.
569 if (flags & ISDOTDOT) {
570 saved_ino = dp->i_ino;
571 VOP_UNLOCK(pdp, 0, td); /* race to get the inode */
572 error = VFS_VGET(pdp->v_mount, saved_ino,
573 cnp->cn_lkflags, &tdp);
574 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, td);
578 } else if (dp->i_number == dp->i_ino) {
579 VREF(vdp); /* we want ourself, ie "." */
582 error = VFS_VGET(pdp->v_mount, dp->i_ino,
583 cnp->cn_lkflags, &tdp);
590 * Insert name into cache if appropriate.
592 if (cnp->cn_flags & MAKEENTRY)
593 cache_enter(vdp, *vpp, cnp);
598 ufs_dirbad(ip, offset, how)
605 mp = ITOV(ip)->v_mount;
606 if ((mp->mnt_flag & MNT_RDONLY) == 0)
607 panic("ufs_dirbad: %s: bad dir ino %lu at offset %ld: %s",
608 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
610 (void)printf("%s: bad dir ino %lu at offset %ld: %s\n",
611 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
615 * Do consistency checking on a directory entry:
616 * record length must be multiple of 4
617 * entry must fit in rest of its DIRBLKSIZ block
618 * record must be large enough to contain entry
619 * name is not longer than MAXNAMLEN
620 * name must be as long as advertised, and null terminated
623 ufs_dirbadentry(dp, ep, entryoffsetinblock)
626 int entryoffsetinblock;
630 # if (BYTE_ORDER == LITTLE_ENDIAN)
634 namlen = ep->d_namlen;
636 namlen = ep->d_namlen;
638 if ((ep->d_reclen & 0x3) != 0 ||
639 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
640 ep->d_reclen < DIRSIZ(OFSFMT(dp), ep) || namlen > MAXNAMLEN) {
642 printf("First bad\n");
647 for (i = 0; i < namlen; i++)
648 if (ep->d_name[i] == '\0') {
650 printf("Second bad\n");
661 * Construct a new directory entry after a call to namei, using the
662 * parameters that it left in the componentname argument cnp. The
663 * argument ip is the inode to which the new directory entry will refer.
666 ufs_makedirentry(ip, cnp, newdirp)
668 struct componentname *cnp;
669 struct direct *newdirp;
673 if ((cnp->cn_flags & SAVENAME) == 0)
674 panic("ufs_makedirentry: missing name");
676 newdirp->d_ino = ip->i_number;
677 newdirp->d_namlen = cnp->cn_namelen;
678 bcopy(cnp->cn_nameptr, newdirp->d_name, (unsigned)cnp->cn_namelen + 1);
679 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
680 newdirp->d_type = IFTODT(ip->i_mode);
683 # if (BYTE_ORDER == LITTLE_ENDIAN)
684 { u_char tmp = newdirp->d_namlen;
685 newdirp->d_namlen = newdirp->d_type;
686 newdirp->d_type = tmp; }
692 * Write a directory entry after a call to namei, using the parameters
693 * that it left in nameidata. The argument dirp is the new directory
694 * entry contents. Dvp is a pointer to the directory to be written,
695 * which was left locked by namei. Remaining parameters (dp->i_offset,
696 * dp->i_count) indicate how the space for the new entry is to be obtained.
697 * Non-null bp indicates that a directory is being created (for the
698 * soft dependency code).
701 ufs_direnter(dvp, tvp, dirp, cnp, newdirbp)
705 struct componentname *cnp;
706 struct buf *newdirbp;
714 struct direct *ep, *nep;
715 int error, ret, blkoff, loc, spacefree, flags, namlen;
718 td = curthread; /* XXX */
722 newentrysize = DIRSIZ(OFSFMT(dvp), dirp);
724 if (dp->i_count == 0) {
726 * If dp->i_count is 0, then namei could find no
727 * space in the directory. Here, dp->i_offset will
728 * be on a directory block boundary and we will write the
729 * new entry into a fresh block.
731 if (dp->i_offset & (DIRBLKSIZ - 1))
732 panic("ufs_direnter: newblk");
734 if (!DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp))
737 if ((error = getinoquota(dp)) != 0) {
738 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
743 if ((error = UFS_BALLOC(dvp, (off_t)dp->i_offset, DIRBLKSIZ,
744 cr, flags, &bp)) != 0) {
745 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
749 dp->i_size = dp->i_offset + DIRBLKSIZ;
750 DIP_SET(dp, i_size, dp->i_size);
751 dp->i_flag |= IN_CHANGE | IN_UPDATE;
752 vnode_pager_setsize(dvp, (u_long)dp->i_size);
753 dirp->d_reclen = DIRBLKSIZ;
754 blkoff = dp->i_offset &
755 (VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_iosize - 1);
756 bcopy((caddr_t)dirp, (caddr_t)bp->b_data + blkoff,newentrysize);
758 if (dp->i_dirhash != NULL) {
759 ufsdirhash_newblk(dp, dp->i_offset);
760 ufsdirhash_add(dp, dirp, dp->i_offset);
761 ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff,
765 if (DOINGSOFTDEP(dvp)) {
767 * Ensure that the entire newly allocated block is a
768 * valid directory so that future growth within the
769 * block does not have to ensure that the block is
770 * written before the inode.
773 while (blkoff < bp->b_bcount) {
775 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
778 if (softdep_setup_directory_add(bp, dp, dp->i_offset,
779 dirp->d_ino, newdirbp, 1) == 0) {
781 return (UFS_UPDATE(dvp, 0));
783 /* We have just allocated a directory block in an
784 * indirect block. Rather than tracking when it gets
785 * claimed by the inode, we simply do a VOP_FSYNC
786 * now to ensure that it is there (in case the user
787 * does a future fsync). Note that we have to unlock
788 * the inode for the entry that we just entered, as
789 * the VOP_FSYNC may need to lock other inodes which
790 * can lead to deadlock if we also hold a lock on
791 * the newly entered node.
793 if ((error = bwrite(bp)))
796 VOP_UNLOCK(tvp, 0, td);
797 error = VOP_FSYNC(dvp, MNT_WAIT, td);
799 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, td);
802 if (DOINGASYNC(dvp)) {
804 return (UFS_UPDATE(dvp, 0));
807 ret = UFS_UPDATE(dvp, 1);
814 * If dp->i_count is non-zero, then namei found space for the new
815 * entry in the range dp->i_offset to dp->i_offset + dp->i_count
816 * in the directory. To use this space, we may have to compact
817 * the entries located there, by copying them together towards the
818 * beginning of the block, leaving the free space in one usable
823 * Increase size of directory if entry eats into new space.
824 * This should never push the size past a new multiple of
827 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
829 if (dp->i_offset + dp->i_count > dp->i_size) {
830 dp->i_size = dp->i_offset + dp->i_count;
831 DIP_SET(dp, i_size, dp->i_size);
834 * Get the block containing the space for the new directory entry.
836 error = UFS_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp);
838 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
843 * Find space for the new entry. In the simple case, the entry at
844 * offset base will have the space. If it does not, then namei
845 * arranged that compacting the region dp->i_offset to
846 * dp->i_offset + dp->i_count would yield the space.
848 ep = (struct direct *)dirbuf;
849 dsize = ep->d_ino ? DIRSIZ(OFSFMT(dvp), ep) : 0;
850 spacefree = ep->d_reclen - dsize;
851 for (loc = ep->d_reclen; loc < dp->i_count; ) {
852 nep = (struct direct *)(dirbuf + loc);
854 /* Trim the existing slot (NB: dsize may be zero). */
855 ep->d_reclen = dsize;
856 ep = (struct direct *)((char *)ep + dsize);
858 /* Read nep->d_reclen now as the bcopy() may clobber it. */
859 loc += nep->d_reclen;
860 if (nep->d_ino == 0) {
862 * A mid-block unused entry. Such entries are
863 * never created by the kernel, but fsck_ffs
864 * can create them (and it doesn't fix them).
866 * Add up the free space, and initialise the
867 * relocated entry since we don't bcopy it.
869 spacefree += nep->d_reclen;
874 dsize = DIRSIZ(OFSFMT(dvp), nep);
875 spacefree += nep->d_reclen - dsize;
877 if (dp->i_dirhash != NULL)
878 ufsdirhash_move(dp, nep,
879 dp->i_offset + ((char *)nep - dirbuf),
880 dp->i_offset + ((char *)ep - dirbuf));
882 if (DOINGSOFTDEP(dvp))
883 softdep_change_directoryentry_offset(dp, dirbuf,
884 (caddr_t)nep, (caddr_t)ep, dsize);
886 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
889 * Here, `ep' points to a directory entry containing `dsize' in-use
890 * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
891 * then the entry is completely unused (dsize == 0). The value
892 * of ep->d_reclen is always indeterminate.
894 * Update the pointer fields in the previous entry (if any),
895 * copy in the new entry, and write out the block.
897 # if (BYTE_ORDER == LITTLE_ENDIAN)
901 namlen = ep->d_namlen;
903 namlen = ep->d_namlen;
905 if (ep->d_ino == 0 ||
906 (ep->d_ino == WINO && namlen == dirp->d_namlen &&
907 bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
908 if (spacefree + dsize < newentrysize)
909 panic("ufs_direnter: compact1");
910 dirp->d_reclen = spacefree + dsize;
912 if (spacefree < newentrysize)
913 panic("ufs_direnter: compact2");
914 dirp->d_reclen = spacefree;
915 ep->d_reclen = dsize;
916 ep = (struct direct *)((char *)ep + dsize);
919 if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
920 dirp->d_reclen == spacefree))
921 ufsdirhash_add(dp, dirp, dp->i_offset + ((char *)ep - dirbuf));
923 bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize);
925 if (dp->i_dirhash != NULL)
926 ufsdirhash_checkblock(dp, dirbuf -
927 (dp->i_offset & (DIRBLKSIZ - 1)),
928 dp->i_offset & ~(DIRBLKSIZ - 1));
931 if (DOINGSOFTDEP(dvp)) {
932 (void) softdep_setup_directory_add(bp, dp,
933 dp->i_offset + (caddr_t)ep - dirbuf,
934 dirp->d_ino, newdirbp, 0);
937 if (DOINGASYNC(dvp)) {
944 dp->i_flag |= IN_CHANGE | IN_UPDATE;
946 * If all went well, and the directory can be shortened, proceed
947 * with the truncation. Note that we have to unlock the inode for
948 * the entry that we just entered, as the truncation may need to
949 * lock other inodes which can lead to deadlock if we also hold a
950 * lock on the newly entered node.
952 if (error == 0 && dp->i_endoff && dp->i_endoff < dp->i_size) {
954 VOP_UNLOCK(tvp, 0, td);
956 if (dp->i_dirhash != NULL)
957 ufsdirhash_dirtrunc(dp, dp->i_endoff);
959 (void) UFS_TRUNCATE(dvp, (off_t)dp->i_endoff,
960 IO_NORMAL | IO_SYNC, cr, td);
962 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, td);
968 * Remove a directory entry after a call to namei, using
969 * the parameters which it left in nameidata. The entry
970 * dp->i_offset contains the offset into the directory of the
971 * entry to be eliminated. The dp->i_count field contains the
972 * size of the previous record in the directory. If this
973 * is 0, the first entry is being deleted, so we need only
974 * zero the inode number to mark the entry as free. If the
975 * entry is not the first in the directory, we must reclaim
976 * the space of the now empty record by adding the record size
977 * to the size of the previous entry.
980 ufs_dirremove(dvp, ip, flags, isrmdir)
993 if (flags & DOWHITEOUT) {
995 * Whiteout entry: set d_ino to WINO.
998 UFS_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) != 0)
1001 ep->d_type = DT_WHT;
1005 if ((error = UFS_BLKATOFF(dvp,
1006 (off_t)(dp->i_offset - dp->i_count), (char **)&ep, &bp)) != 0)
1010 * Remove the dirhash entry. This is complicated by the fact
1011 * that `ep' is the previous entry when dp->i_count != 0.
1013 if (dp->i_dirhash != NULL)
1014 ufsdirhash_remove(dp, (dp->i_count == 0) ? ep :
1015 (struct direct *)((char *)ep + ep->d_reclen), dp->i_offset);
1017 if (dp->i_count == 0) {
1019 * First entry in block: set d_ino to zero.
1024 * Collapse new free space into previous entry.
1026 ep->d_reclen += dp->i_reclen;
1029 if (dp->i_dirhash != NULL)
1030 ufsdirhash_checkblock(dp, (char *)ep -
1031 ((dp->i_offset - dp->i_count) & (DIRBLKSIZ - 1)),
1032 dp->i_offset & ~(DIRBLKSIZ - 1));
1035 if (DOINGSOFTDEP(dvp)) {
1038 softdep_change_linkcnt(ip);
1039 softdep_setup_remove(bp, dp, ip, isrmdir);
1041 if (softdep_slowdown(dvp)) {
1051 DIP_SET(ip, i_nlink, ip->i_nlink);
1052 ip->i_flag |= IN_CHANGE;
1054 if (flags & DOWHITEOUT)
1056 else if (DOINGASYNC(dvp) && dp->i_count != 0) {
1062 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1064 * If the last named reference to a snapshot goes away,
1065 * drop its snapshot reference so that it will be reclaimed
1066 * when last open reference goes away.
1068 #if defined(FFS) || defined(IFS)
1069 if (ip != 0 && (ip->i_flags & SF_SNAPSHOT) != 0 && ip->i_effnlink == 0)
1076 * Rewrite an existing directory entry to point at the inode
1077 * supplied. The parameters describing the directory entry are
1078 * set up by a call to namei.
1081 ufs_dirrewrite(dp, oip, newinum, newtype, isrmdir)
1082 struct inode *dp, *oip;
1089 struct vnode *vdp = ITOV(dp);
1092 error = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp);
1095 ep->d_ino = newinum;
1097 ep->d_type = newtype;
1099 if (DOINGSOFTDEP(vdp)) {
1100 softdep_change_linkcnt(oip);
1101 softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir);
1105 DIP_SET(oip, i_nlink, oip->i_nlink);
1106 oip->i_flag |= IN_CHANGE;
1107 if (DOINGASYNC(vdp)) {
1114 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1116 * If the last named reference to a snapshot goes away,
1117 * drop its snapshot reference so that it will be reclaimed
1118 * when last open reference goes away.
1120 #if defined(FFS) || defined(IFS)
1121 if ((oip->i_flags & SF_SNAPSHOT) != 0 && oip->i_effnlink == 0)
1128 * Check if a directory is empty or not.
1129 * Inode supplied must be locked.
1131 * Using a struct dirtemplate here is not precisely
1132 * what we want, but better than using a struct direct.
1134 * NB: does not handle corrupted directories.
1137 ufs_dirempty(ip, parentino, cred)
1143 struct dirtemplate dbuf;
1144 struct direct *dp = (struct direct *)&dbuf;
1145 int error, count, namlen;
1146 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1148 for (off = 0; off < ip->i_size; off += dp->d_reclen) {
1149 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1150 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1151 NOCRED, &count, (struct thread *)0);
1153 * Since we read MINDIRSIZ, residual must
1154 * be 0 unless we're at end of file.
1156 if (error || count != 0)
1158 /* avoid infinite loops */
1159 if (dp->d_reclen == 0)
1161 /* skip empty entries */
1162 if (dp->d_ino == 0 || dp->d_ino == WINO)
1164 /* accept only "." and ".." */
1165 # if (BYTE_ORDER == LITTLE_ENDIAN)
1166 if (OFSFMT(ITOV(ip)))
1167 namlen = dp->d_type;
1169 namlen = dp->d_namlen;
1171 namlen = dp->d_namlen;
1175 if (dp->d_name[0] != '.')
1178 * At this point namlen must be 1 or 2.
1179 * 1 implies ".", 2 implies ".." if second
1182 if (namlen == 1 && dp->d_ino == ip->i_number)
1184 if (dp->d_name[1] == '.' && dp->d_ino == parentino)
1192 * Check if source directory is in the path of the target directory.
1193 * Target is supplied locked, source is unlocked.
1194 * The target is always vput before returning.
1197 ufs_checkpath(source, target, cred)
1198 struct inode *source, *target;
1204 struct dirtemplate dirbuf;
1207 if (target->i_number == source->i_number) {
1213 if (target->i_number == rootino)
1217 if (vp->v_type != VDIR) {
1221 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1222 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1223 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, (int *)0,
1224 (struct thread *)0);
1227 # if (BYTE_ORDER == LITTLE_ENDIAN)
1229 namlen = dirbuf.dotdot_type;
1231 namlen = dirbuf.dotdot_namlen;
1233 namlen = dirbuf.dotdot_namlen;
1236 dirbuf.dotdot_name[0] != '.' ||
1237 dirbuf.dotdot_name[1] != '.') {
1241 if (dirbuf.dotdot_ino == source->i_number) {
1245 if (dirbuf.dotdot_ino == rootino)
1248 error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino,
1257 if (error == ENOTDIR)
1258 printf("checkpath: .. not a directory\n");