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)
80 static int ufs_lookup_(struct vnode *, struct vnode **, struct componentname *,
85 ufs_lookup_upgrade_lock(struct vnode *vp)
89 ASSERT_VOP_LOCKED(vp, __FUNCTION__);
90 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
96 * Upgrade vnode lock, since getinoquota()
97 * requires exclusive lock to modify inode.
100 vn_lock(vp, LK_UPGRADE | LK_RETRY);
102 if (vp->v_iflag & VI_DOOMED)
110 ufs_delete_denied(struct vnode *vdp, struct vnode *tdp, struct ucred *cred,
117 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
119 * 3.16.2.1. ACE4_DELETE vs. ACE4_DELETE_CHILD
123 * XXX: Is this check required?
125 error = VOP_ACCESS(vdp, VEXEC, cred, td);
129 error = VOP_ACCESSX(tdp, VDELETE, cred, td);
133 error = VOP_ACCESSX(vdp, VDELETE_CHILD, cred, td);
137 error = VOP_ACCESSX(vdp, VEXPLICIT_DENY | VDELETE_CHILD, cred, td);
141 #endif /* !UFS_ACL */
144 * Standard Unix access control - delete access requires VWRITE.
146 error = VOP_ACCESS(vdp, VWRITE, cred, td);
151 * If directory is "sticky", then user must own
152 * the directory, or the file in it, else she
153 * may not delete it (unless she's root). This
154 * implements append-only directories.
156 if ((VTOI(vdp)->i_mode & ISVTX) &&
157 VOP_ACCESS(vdp, VADMIN, cred, td) &&
158 VOP_ACCESS(tdp, VADMIN, cred, td))
165 * Convert a component of a pathname into a pointer to a locked inode.
166 * This is a very central and rather complicated routine.
167 * If the filesystem is not maintained in a strict tree hierarchy,
168 * this can result in a deadlock situation (see comments in code below).
170 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
171 * on whether the name is to be looked up, created, renamed, or deleted.
172 * When CREATE, RENAME, or DELETE is specified, information usable in
173 * creating, renaming, or deleting a directory entry may be calculated.
174 * If flag has LOCKPARENT or'ed into it and the target of the pathname
175 * exists, lookup returns both the target and its parent directory locked.
176 * When creating or renaming and LOCKPARENT is specified, the target may
177 * not be ".". When deleting and LOCKPARENT is specified, the target may
178 * be "."., but the caller must check to ensure it does an vrele and vput
179 * instead of two vputs.
181 * This routine is actually used as VOP_CACHEDLOOKUP method, and the
182 * filesystem employs the generic vfs_cache_lookup() as VOP_LOOKUP
185 * vfs_cache_lookup() performs the following for us:
186 * check that it is a directory
187 * check accessibility of directory
188 * check for modification attempts on read-only mounts
189 * if name found in cache
190 * if at end of path and deleting or creating
194 * return VOP_CACHEDLOOKUP()
196 * Overall outline of ufs_lookup:
198 * search for name in directory, to found or notfound
200 * if creating, return locked directory, leaving info on available slots
203 * if at end of path and deleting, return information to allow delete
204 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
205 * inode and return info to allow rewrite
206 * if not at end, add name to cache; if at end and neither creating
207 * nor deleting, add name to cache
211 struct vop_cachedlookup_args /* {
213 struct vnode **a_vpp;
214 struct componentname *a_cnp;
218 return (ufs_lookup_(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
222 ufs_lookup_(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
225 struct inode *dp; /* inode for directory being searched */
226 struct buf *bp; /* a buffer of directory entries */
227 struct direct *ep; /* the current directory entry */
228 int entryoffsetinblock; /* offset of ep in bp's buffer */
229 enum {NONE, COMPACT, FOUND} slotstatus;
230 doff_t slotoffset; /* offset of area with free space */
231 doff_t i_diroff; /* cached i_diroff value. */
232 doff_t i_offset; /* cached i_offset value. */
233 int slotsize; /* size of area at slotoffset */
234 int slotfreespace; /* amount of space free in slot */
235 int slotneeded; /* size of the entry we're seeking */
236 int numdirpasses; /* strategy for directory search */
237 doff_t endsearch; /* offset to end directory search */
238 doff_t prevoff; /* prev entry dp->i_offset */
239 struct vnode *pdp; /* saved dp during symlink work */
240 struct vnode *tdp; /* returned by VFS_VGET */
241 doff_t enduseful; /* pointer past last used dir slot */
242 u_long bmask; /* block offset mask */
244 struct ucred *cred = cnp->cn_cred;
245 int flags = cnp->cn_flags;
246 int nameiop = cnp->cn_nameiop;
256 * Create a vm object if vmiodirenable is enabled.
257 * Alternatively we could call vnode_create_vobject
258 * in VFS_VGET but we could end up creating objects
259 * that are never used.
261 vnode_create_vobject(vdp, DIP(dp, i_size), cnp->cn_thread);
263 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
265 if ((nameiop == DELETE || nameiop == RENAME) && (flags & ISLASTCN)) {
266 error = ufs_lookup_upgrade_lock(vdp);
277 * We now have a segment name to search for, and a directory to search.
279 * Suppress search for slots unless creating
280 * file and at end of pathname, in which case
281 * we watch for a place to put the new file in
282 * case it doesn't already exist.
285 i_diroff = dp->i_diroff;
287 slotfreespace = slotsize = slotneeded = 0;
288 if ((nameiop == CREATE || nameiop == RENAME) &&
289 (flags & ISLASTCN)) {
291 slotneeded = DIRECTSIZ(cnp->cn_namelen);
296 * Use dirhash for fast operations on large directories. The logic
297 * to determine whether to hash the directory is contained within
298 * ufsdirhash_build(); a zero return means that it decided to hash
299 * this directory and it successfully built up the hash table.
301 if (ufsdirhash_build(dp) == 0) {
302 /* Look for a free slot if needed. */
303 enduseful = dp->i_size;
304 if (slotstatus != FOUND) {
305 slotoffset = ufsdirhash_findfree(dp, slotneeded,
307 if (slotoffset >= 0) {
308 slotstatus = COMPACT;
309 enduseful = ufsdirhash_enduseful(dp);
311 enduseful = dp->i_size;
314 /* Look up the component. */
316 entryoffsetinblock = 0; /* silence compiler warning */
317 switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
318 &i_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) {
320 ep = (struct direct *)((char *)bp->b_data +
324 i_offset = roundup2(dp->i_size, DIRBLKSIZ);
327 /* Something failed; just do a linear search. */
331 #endif /* UFS_DIRHASH */
333 * If there is cached information on a previous search of
334 * this directory, pick up where we last left off.
335 * We cache only lookups as these are the most common
336 * and have the greatest payoff. Caching CREATE has little
337 * benefit as it usually must search the entire directory
338 * to determine that the entry does not exist. Caching the
339 * location of the last DELETE or RENAME has not reduced
340 * profiling time and hence has been removed in the interest
343 if (nameiop != LOOKUP || i_diroff == 0 || i_diroff >= dp->i_size) {
344 entryoffsetinblock = 0;
349 if ((entryoffsetinblock = i_offset & bmask) &&
350 (error = UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp)))
353 nchstats.ncs_2passes++;
356 endsearch = roundup2(dp->i_size, DIRBLKSIZ);
360 while (i_offset < endsearch) {
362 * If necessary, get the next directory block.
364 if ((i_offset & bmask) == 0) {
368 UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp);
371 entryoffsetinblock = 0;
374 * If still looking for a slot, and at a DIRBLKSIZE
375 * boundary, have to start looking for free space again.
377 if (slotstatus == NONE &&
378 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
383 * Get pointer to next entry.
384 * Full validation checks are slow, so we only check
385 * enough to insure forward progress through the
386 * directory. Complete checks can be run by patching
387 * "dirchk" to be true.
389 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
390 if (ep->d_reclen == 0 || ep->d_reclen >
391 DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
392 (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) {
395 ufs_dirbad(dp, i_offset, "mangled entry");
396 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
398 entryoffsetinblock += i;
403 * If an appropriate sized slot has not yet been found,
404 * check to see if one is available. Also accumulate space
405 * in the current block so that we can determine if
406 * compaction is viable.
408 if (slotstatus != FOUND) {
409 int size = ep->d_reclen;
412 size -= DIRSIZ(OFSFMT(vdp), ep);
414 if (size >= slotneeded) {
416 slotoffset = i_offset;
417 slotsize = ep->d_reclen;
418 } else if (slotstatus == NONE) {
419 slotfreespace += size;
420 if (slotoffset == -1)
421 slotoffset = i_offset;
422 if (slotfreespace >= slotneeded) {
423 slotstatus = COMPACT;
424 slotsize = i_offset +
425 ep->d_reclen - slotoffset;
432 * Check for a name match.
435 # if (BYTE_ORDER == LITTLE_ENDIAN)
439 namlen = ep->d_namlen;
441 namlen = ep->d_namlen;
443 if (namlen == cnp->cn_namelen &&
444 (cnp->cn_nameptr[0] == ep->d_name[0]) &&
445 !bcmp(cnp->cn_nameptr, ep->d_name,
451 * Save directory entry's inode number and
452 * reclen in ndp->ni_ufs area, and release
455 if (vdp->v_mount->mnt_maxsymlinklen > 0 &&
456 ep->d_type == DT_WHT) {
458 slotoffset = i_offset;
459 slotsize = ep->d_reclen;
460 enduseful = dp->i_size;
461 cnp->cn_flags |= ISWHITEOUT;
470 i_offset += ep->d_reclen;
471 entryoffsetinblock += ep->d_reclen;
473 enduseful = i_offset;
477 * If we started in the middle of the directory and failed
478 * to find our target, we must check the beginning as well.
480 if (numdirpasses == 2) {
483 endsearch = i_diroff;
489 * If creating, and at end of pathname and current
490 * directory has not been removed, then can consider
491 * allowing file to be created.
493 if ((nameiop == CREATE || nameiop == RENAME ||
494 (nameiop == DELETE &&
495 (cnp->cn_flags & DOWHITEOUT) &&
496 (cnp->cn_flags & ISWHITEOUT))) &&
497 (flags & ISLASTCN) && dp->i_effnlink != 0) {
499 * Access for write is interpreted as allowing
500 * creation of files in the directory.
502 * XXX: Fix the comment above.
504 if (flags & WILLBEDIR)
505 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread);
507 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread);
511 * Return an indication of where the new directory
512 * entry should be put. If we didn't find a slot,
513 * then set dp->i_count to 0 indicating
514 * that the new slot belongs at the end of the
515 * directory. If we found a slot, then the new entry
516 * can be put in the range from dp->i_offset to
517 * dp->i_offset + dp->i_count.
519 if (slotstatus == NONE) {
520 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
522 enduseful = dp->i_offset;
523 } else if (nameiop == DELETE) {
524 dp->i_offset = slotoffset;
525 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
528 dp->i_count = dp->i_offset - prevoff;
530 dp->i_offset = slotoffset;
531 dp->i_count = slotsize;
532 if (enduseful < slotoffset + slotsize)
533 enduseful = slotoffset + slotsize;
535 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
537 * We return with the directory locked, so that
538 * the parameters we set up above will still be
539 * valid if we actually decide to do a direnter().
540 * We return ni_vp == NULL to indicate that the entry
541 * does not currently exist; we leave a pointer to
542 * the (locked) directory inode in ndp->ni_dvp.
543 * The pathname buffer is saved so that the name
544 * can be obtained later.
546 * NB - if the directory is unlocked, then this
547 * information cannot be used.
549 cnp->cn_flags |= SAVENAME;
550 return (EJUSTRETURN);
553 * Insert name into cache (as non-existent) if appropriate.
555 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
556 cache_enter(vdp, NULL, cnp);
560 if (numdirpasses == 2)
561 nchstats.ncs_pass2++;
563 * Check that directory length properly reflects presence
566 if (i_offset + DIRSIZ(OFSFMT(vdp), ep) > dp->i_size) {
567 ufs_dirbad(dp, i_offset, "i_size too small");
568 dp->i_size = i_offset + DIRSIZ(OFSFMT(vdp), ep);
569 DIP_SET(dp, i_size, dp->i_size);
570 dp->i_flag |= IN_CHANGE | IN_UPDATE;
575 * Found component in pathname.
576 * If the final component of path name, save information
577 * in the cache as to where the entry was found.
579 if ((flags & ISLASTCN) && nameiop == LOOKUP)
580 dp->i_diroff = i_offset &~ (DIRBLKSIZ - 1);
582 if (dd_ino != NULL) {
588 * If deleting, and at end of pathname, return
589 * parameters which can be used to remove file.
591 if (nameiop == DELETE && (flags & ISLASTCN)) {
592 if (flags & LOCKPARENT)
593 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
594 if ((error = VFS_VGET(vdp->v_mount, ino,
595 LK_EXCLUSIVE, &tdp)) != 0)
598 error = ufs_delete_denied(vdp, tdp, cred, cnp->cn_thread);
606 * Return pointer to current entry in dp->i_offset,
607 * and distance past previous entry (if there
608 * is a previous entry in this block) in dp->i_count.
609 * Save directory inode pointer in ndp->ni_dvp for dirremove().
611 * Technically we shouldn't be setting these in the
612 * WANTPARENT case (first lookup in rename()), but any
613 * lookups that will result in directory changes will
616 dp->i_offset = i_offset;
617 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
620 dp->i_count = dp->i_offset - prevoff;
621 if (dp->i_number == ino) {
633 * If rewriting (RENAME), return the inode and the
634 * information required to rewrite the present directory
635 * Must get inode of directory entry to verify it's a
636 * regular file, or empty directory.
638 if (nameiop == RENAME && (flags & ISLASTCN)) {
639 if (flags & WILLBEDIR)
640 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread);
642 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread);
646 * Careful about locking second inode.
647 * This can only occur if the target is ".".
649 dp->i_offset = i_offset;
650 if (dp->i_number == ino)
652 if ((error = VFS_VGET(vdp->v_mount, ino,
653 LK_EXCLUSIVE, &tdp)) != 0)
656 error = ufs_delete_denied(vdp, tdp, cred, cnp->cn_thread);
662 #ifdef SunOS_doesnt_do_that
664 * The only purpose of this check is to return the correct
665 * error. Assume that we want to rename directory "a"
666 * to a file "b", and that we have no ACL_WRITE_DATA on
667 * a containing directory, but we _do_ have ACL_APPEND_DATA.
668 * In that case, the VOP_ACCESS check above will return 0,
669 * and the operation will fail with ENOTDIR instead
672 if (tdp->v_type == VDIR)
673 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread);
675 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread);
683 cnp->cn_flags |= SAVENAME;
688 * Step through the translation in the name. We do not `vput' the
689 * directory because we may need it again if a symbolic link
690 * is relative to the current directory. Instead we save it
691 * unlocked as "pdp". We must get the target inode before unlocking
692 * the directory to insure that the inode will not be removed
693 * before we get it. We prevent deadlock by always fetching
694 * inodes from the root, moving down the directory tree. Thus
695 * when following backward pointers ".." we must unlock the
696 * parent directory before getting the requested directory.
697 * There is a potential race condition here if both the current
698 * and parent directories are removed before the VFS_VGET for the
699 * inode associated with ".." returns. We hope that this occurs
700 * infrequently since we cannot avoid this race condition without
701 * implementing a sophisticated deadlock detection algorithm.
702 * Note also that this simple deadlock detection scheme will not
703 * work if the filesystem has any hard links other than ".."
704 * that point backwards in the directory structure.
707 if (flags & ISDOTDOT) {
708 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
713 * Recheck that ".." entry in the vdp directory points
714 * to the inode we looked up before vdp lock was
717 error = ufs_lookup_(pdp, NULL, cnp, &ino1);
728 } else if (dp->i_number == ino) {
729 VREF(vdp); /* we want ourself, ie "." */
731 * When we lookup "." we still can be asked to lock it
734 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
735 if (ltype != VOP_ISLOCKED(vdp)) {
736 if (ltype == LK_EXCLUSIVE)
737 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
738 else /* if (ltype == LK_SHARED) */
739 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
741 * Relock for the "." case may left us with
744 if (vdp->v_iflag & VI_DOOMED) {
751 error = VFS_VGET(pdp->v_mount, ino, cnp->cn_lkflags, &tdp);
758 * Insert name into cache if appropriate.
760 if (cnp->cn_flags & MAKEENTRY)
761 cache_enter(vdp, *vpp, cnp);
766 ufs_dirbad(ip, offset, how)
773 mp = ITOV(ip)->v_mount;
774 if ((mp->mnt_flag & MNT_RDONLY) == 0)
775 panic("ufs_dirbad: %s: bad dir ino %lu at offset %ld: %s",
776 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
778 (void)printf("%s: bad dir ino %lu at offset %ld: %s\n",
779 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
783 * Do consistency checking on a directory entry:
784 * record length must be multiple of 4
785 * entry must fit in rest of its DIRBLKSIZ block
786 * record must be large enough to contain entry
787 * name is not longer than MAXNAMLEN
788 * name must be as long as advertised, and null terminated
791 ufs_dirbadentry(dp, ep, entryoffsetinblock)
794 int entryoffsetinblock;
798 # if (BYTE_ORDER == LITTLE_ENDIAN)
802 namlen = ep->d_namlen;
804 namlen = ep->d_namlen;
806 if ((ep->d_reclen & 0x3) != 0 ||
807 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
808 ep->d_reclen < DIRSIZ(OFSFMT(dp), ep) || namlen > MAXNAMLEN) {
810 printf("First bad\n");
815 for (i = 0; i < namlen; i++)
816 if (ep->d_name[i] == '\0') {
818 printf("Second bad\n");
829 * Construct a new directory entry after a call to namei, using the
830 * parameters that it left in the componentname argument cnp. The
831 * argument ip is the inode to which the new directory entry will refer.
834 ufs_makedirentry(ip, cnp, newdirp)
836 struct componentname *cnp;
837 struct direct *newdirp;
841 if ((cnp->cn_flags & SAVENAME) == 0)
842 panic("ufs_makedirentry: missing name");
844 newdirp->d_ino = ip->i_number;
845 newdirp->d_namlen = cnp->cn_namelen;
846 bcopy(cnp->cn_nameptr, newdirp->d_name, (unsigned)cnp->cn_namelen + 1);
847 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
848 newdirp->d_type = IFTODT(ip->i_mode);
851 # if (BYTE_ORDER == LITTLE_ENDIAN)
852 { u_char tmp = newdirp->d_namlen;
853 newdirp->d_namlen = newdirp->d_type;
854 newdirp->d_type = tmp; }
860 * Write a directory entry after a call to namei, using the parameters
861 * that it left in nameidata. The argument dirp is the new directory
862 * entry contents. Dvp is a pointer to the directory to be written,
863 * which was left locked by namei. Remaining parameters (dp->i_offset,
864 * dp->i_count) indicate how the space for the new entry is to be obtained.
865 * Non-null bp indicates that a directory is being created (for the
866 * soft dependency code).
869 ufs_direnter(dvp, tvp, dirp, cnp, newdirbp)
873 struct componentname *cnp;
874 struct buf *newdirbp;
882 struct direct *ep, *nep;
883 int error, ret, blkoff, loc, spacefree, flags, namlen;
886 td = curthread; /* XXX */
890 newentrysize = DIRSIZ(OFSFMT(dvp), dirp);
892 if (dp->i_count == 0) {
894 * If dp->i_count is 0, then namei could find no
895 * space in the directory. Here, dp->i_offset will
896 * be on a directory block boundary and we will write the
897 * new entry into a fresh block.
899 if (dp->i_offset & (DIRBLKSIZ - 1))
900 panic("ufs_direnter: newblk");
902 if (!DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp))
905 if ((error = getinoquota(dp)) != 0) {
906 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
911 if ((error = UFS_BALLOC(dvp, (off_t)dp->i_offset, DIRBLKSIZ,
912 cr, flags, &bp)) != 0) {
913 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
917 dp->i_size = dp->i_offset + DIRBLKSIZ;
918 DIP_SET(dp, i_size, dp->i_size);
919 dp->i_flag |= IN_CHANGE | IN_UPDATE;
920 vnode_pager_setsize(dvp, (u_long)dp->i_size);
921 dirp->d_reclen = DIRBLKSIZ;
922 blkoff = dp->i_offset &
923 (VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_iosize - 1);
924 bcopy((caddr_t)dirp, (caddr_t)bp->b_data + blkoff,newentrysize);
926 if (dp->i_dirhash != NULL) {
927 ufsdirhash_newblk(dp, dp->i_offset);
928 ufsdirhash_add(dp, dirp, dp->i_offset);
929 ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff,
933 if (DOINGSOFTDEP(dvp)) {
935 * Ensure that the entire newly allocated block is a
936 * valid directory so that future growth within the
937 * block does not have to ensure that the block is
938 * written before the inode.
941 while (blkoff < bp->b_bcount) {
943 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
946 if (softdep_setup_directory_add(bp, dp, dp->i_offset,
947 dirp->d_ino, newdirbp, 1) == 0) {
949 return (UFS_UPDATE(dvp, 0));
951 /* We have just allocated a directory block in an
952 * indirect block. Rather than tracking when it gets
953 * claimed by the inode, we simply do a VOP_FSYNC
954 * now to ensure that it is there (in case the user
955 * does a future fsync). Note that we have to unlock
956 * the inode for the entry that we just entered, as
957 * the VOP_FSYNC may need to lock other inodes which
958 * can lead to deadlock if we also hold a lock on
959 * the newly entered node.
961 if ((error = bwrite(bp)))
965 error = VOP_FSYNC(dvp, MNT_WAIT, td);
967 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
970 if (DOINGASYNC(dvp)) {
972 return (UFS_UPDATE(dvp, 0));
975 ret = UFS_UPDATE(dvp, 1);
982 * If dp->i_count is non-zero, then namei found space for the new
983 * entry in the range dp->i_offset to dp->i_offset + dp->i_count
984 * in the directory. To use this space, we may have to compact
985 * the entries located there, by copying them together towards the
986 * beginning of the block, leaving the free space in one usable
991 * Increase size of directory if entry eats into new space.
992 * This should never push the size past a new multiple of
995 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
997 if (dp->i_offset + dp->i_count > dp->i_size) {
998 dp->i_size = dp->i_offset + dp->i_count;
999 DIP_SET(dp, i_size, dp->i_size);
1002 * Get the block containing the space for the new directory entry.
1004 error = UFS_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp);
1006 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
1011 * Find space for the new entry. In the simple case, the entry at
1012 * offset base will have the space. If it does not, then namei
1013 * arranged that compacting the region dp->i_offset to
1014 * dp->i_offset + dp->i_count would yield the space.
1016 ep = (struct direct *)dirbuf;
1017 dsize = ep->d_ino ? DIRSIZ(OFSFMT(dvp), ep) : 0;
1018 spacefree = ep->d_reclen - dsize;
1019 for (loc = ep->d_reclen; loc < dp->i_count; ) {
1020 nep = (struct direct *)(dirbuf + loc);
1022 /* Trim the existing slot (NB: dsize may be zero). */
1023 ep->d_reclen = dsize;
1024 ep = (struct direct *)((char *)ep + dsize);
1026 /* Read nep->d_reclen now as the bcopy() may clobber it. */
1027 loc += nep->d_reclen;
1028 if (nep->d_ino == 0) {
1030 * A mid-block unused entry. Such entries are
1031 * never created by the kernel, but fsck_ffs
1032 * can create them (and it doesn't fix them).
1034 * Add up the free space, and initialise the
1035 * relocated entry since we don't bcopy it.
1037 spacefree += nep->d_reclen;
1042 dsize = DIRSIZ(OFSFMT(dvp), nep);
1043 spacefree += nep->d_reclen - dsize;
1045 if (dp->i_dirhash != NULL)
1046 ufsdirhash_move(dp, nep,
1047 dp->i_offset + ((char *)nep - dirbuf),
1048 dp->i_offset + ((char *)ep - dirbuf));
1050 if (DOINGSOFTDEP(dvp))
1051 softdep_change_directoryentry_offset(dp, dirbuf,
1052 (caddr_t)nep, (caddr_t)ep, dsize);
1054 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1057 * Here, `ep' points to a directory entry containing `dsize' in-use
1058 * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
1059 * then the entry is completely unused (dsize == 0). The value
1060 * of ep->d_reclen is always indeterminate.
1062 * Update the pointer fields in the previous entry (if any),
1063 * copy in the new entry, and write out the block.
1065 # if (BYTE_ORDER == LITTLE_ENDIAN)
1067 namlen = ep->d_type;
1069 namlen = ep->d_namlen;
1071 namlen = ep->d_namlen;
1073 if (ep->d_ino == 0 ||
1074 (ep->d_ino == WINO && namlen == dirp->d_namlen &&
1075 bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
1076 if (spacefree + dsize < newentrysize)
1077 panic("ufs_direnter: compact1");
1078 dirp->d_reclen = spacefree + dsize;
1080 if (spacefree < newentrysize)
1081 panic("ufs_direnter: compact2");
1082 dirp->d_reclen = spacefree;
1083 ep->d_reclen = dsize;
1084 ep = (struct direct *)((char *)ep + dsize);
1087 if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
1088 dirp->d_reclen == spacefree))
1089 ufsdirhash_add(dp, dirp, dp->i_offset + ((char *)ep - dirbuf));
1091 bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize);
1093 if (dp->i_dirhash != NULL)
1094 ufsdirhash_checkblock(dp, dirbuf -
1095 (dp->i_offset & (DIRBLKSIZ - 1)),
1096 dp->i_offset & ~(DIRBLKSIZ - 1));
1099 if (DOINGSOFTDEP(dvp)) {
1100 (void) softdep_setup_directory_add(bp, dp,
1101 dp->i_offset + (caddr_t)ep - dirbuf,
1102 dirp->d_ino, newdirbp, 0);
1105 if (DOINGASYNC(dvp)) {
1112 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1114 * If all went well, and the directory can be shortened, proceed
1115 * with the truncation. Note that we have to unlock the inode for
1116 * the entry that we just entered, as the truncation may need to
1117 * lock other inodes which can lead to deadlock if we also hold a
1118 * lock on the newly entered node.
1120 if (error == 0 && dp->i_endoff && dp->i_endoff < dp->i_size) {
1124 if (dp->i_dirhash != NULL)
1125 ufsdirhash_dirtrunc(dp, dp->i_endoff);
1127 (void) UFS_TRUNCATE(dvp, (off_t)dp->i_endoff,
1128 IO_NORMAL | IO_SYNC, cr, td);
1130 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
1136 * Remove a directory entry after a call to namei, using
1137 * the parameters which it left in nameidata. The entry
1138 * dp->i_offset contains the offset into the directory of the
1139 * entry to be eliminated. The dp->i_count field contains the
1140 * size of the previous record in the directory. If this
1141 * is 0, the first entry is being deleted, so we need only
1142 * zero the inode number to mark the entry as free. If the
1143 * entry is not the first in the directory, we must reclaim
1144 * the space of the now empty record by adding the record size
1145 * to the size of the previous entry.
1148 ufs_dirremove(dvp, ip, flags, isrmdir)
1155 struct direct *ep, *rep;
1161 if (flags & DOWHITEOUT) {
1163 * Whiteout entry: set d_ino to WINO.
1166 UFS_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) != 0)
1169 ep->d_type = DT_WHT;
1173 if ((error = UFS_BLKATOFF(dvp,
1174 (off_t)(dp->i_offset - dp->i_count), (char **)&ep, &bp)) != 0)
1177 /* Set 'rep' to the entry being removed. */
1178 if (dp->i_count == 0)
1181 rep = (struct direct *)((char *)ep + ep->d_reclen);
1184 * Remove the dirhash entry. This is complicated by the fact
1185 * that `ep' is the previous entry when dp->i_count != 0.
1187 if (dp->i_dirhash != NULL)
1188 ufsdirhash_remove(dp, rep, dp->i_offset);
1190 if (dp->i_count == 0) {
1192 * First entry in block: set d_ino to zero.
1197 * Collapse new free space into previous entry.
1199 ep->d_reclen += rep->d_reclen;
1202 if (dp->i_dirhash != NULL)
1203 ufsdirhash_checkblock(dp, (char *)ep -
1204 ((dp->i_offset - dp->i_count) & (DIRBLKSIZ - 1)),
1205 dp->i_offset & ~(DIRBLKSIZ - 1));
1208 if (DOINGSOFTDEP(dvp)) {
1211 softdep_change_linkcnt(ip);
1212 softdep_setup_remove(bp, dp, ip, isrmdir);
1214 if (softdep_slowdown(dvp)) {
1224 DIP_SET(ip, i_nlink, ip->i_nlink);
1225 ip->i_flag |= IN_CHANGE;
1227 if (flags & DOWHITEOUT)
1229 else if (DOINGASYNC(dvp) && dp->i_count != 0) {
1235 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1237 * If the last named reference to a snapshot goes away,
1238 * drop its snapshot reference so that it will be reclaimed
1239 * when last open reference goes away.
1241 #if defined(FFS) || defined(IFS)
1242 if (ip != 0 && (ip->i_flags & SF_SNAPSHOT) != 0 && ip->i_effnlink == 0)
1249 * Rewrite an existing directory entry to point at the inode
1250 * supplied. The parameters describing the directory entry are
1251 * set up by a call to namei.
1254 ufs_dirrewrite(dp, oip, newinum, newtype, isrmdir)
1255 struct inode *dp, *oip;
1262 struct vnode *vdp = ITOV(dp);
1265 error = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp);
1268 ep->d_ino = newinum;
1270 ep->d_type = newtype;
1272 if (DOINGSOFTDEP(vdp)) {
1273 softdep_change_linkcnt(oip);
1274 softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir);
1278 DIP_SET(oip, i_nlink, oip->i_nlink);
1279 oip->i_flag |= IN_CHANGE;
1280 if (DOINGASYNC(vdp)) {
1287 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1289 * If the last named reference to a snapshot goes away,
1290 * drop its snapshot reference so that it will be reclaimed
1291 * when last open reference goes away.
1293 #if defined(FFS) || defined(IFS)
1294 if ((oip->i_flags & SF_SNAPSHOT) != 0 && oip->i_effnlink == 0)
1301 * Check if a directory is empty or not.
1302 * Inode supplied must be locked.
1304 * Using a struct dirtemplate here is not precisely
1305 * what we want, but better than using a struct direct.
1307 * NB: does not handle corrupted directories.
1310 ufs_dirempty(ip, parentino, cred)
1316 struct dirtemplate dbuf;
1317 struct direct *dp = (struct direct *)&dbuf;
1318 int error, count, namlen;
1319 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1321 for (off = 0; off < ip->i_size; off += dp->d_reclen) {
1322 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1323 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1324 NOCRED, &count, (struct thread *)0);
1326 * Since we read MINDIRSIZ, residual must
1327 * be 0 unless we're at end of file.
1329 if (error || count != 0)
1331 /* avoid infinite loops */
1332 if (dp->d_reclen == 0)
1334 /* skip empty entries */
1335 if (dp->d_ino == 0 || dp->d_ino == WINO)
1337 /* accept only "." and ".." */
1338 # if (BYTE_ORDER == LITTLE_ENDIAN)
1339 if (OFSFMT(ITOV(ip)))
1340 namlen = dp->d_type;
1342 namlen = dp->d_namlen;
1344 namlen = dp->d_namlen;
1348 if (dp->d_name[0] != '.')
1351 * At this point namlen must be 1 or 2.
1352 * 1 implies ".", 2 implies ".." if second
1355 if (namlen == 1 && dp->d_ino == ip->i_number)
1357 if (dp->d_name[1] == '.' && dp->d_ino == parentino)
1365 ufs_dir_dd_ino(struct vnode *vp, struct ucred *cred, ino_t *dd_ino)
1367 struct dirtemplate dirbuf;
1370 if (vp->v_type != VDIR)
1372 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1373 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1374 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL, NULL);
1377 #if (BYTE_ORDER == LITTLE_ENDIAN)
1379 namlen = dirbuf.dotdot_type;
1381 namlen = dirbuf.dotdot_namlen;
1383 namlen = dirbuf.dotdot_namlen;
1385 if (namlen != 2 || dirbuf.dotdot_name[0] != '.' ||
1386 dirbuf.dotdot_name[1] != '.')
1388 *dd_ino = dirbuf.dotdot_ino;
1393 * Check if source directory is in the path of the target directory.
1394 * Target is supplied locked, source is unlocked.
1395 * The target is always vput before returning.
1398 ufs_checkpath(ino_t source_ino, struct inode *target, struct ucred *cred)
1400 struct vnode *vp, *vp1;
1405 if (target->i_number == source_ino) {
1410 if (target->i_number == ROOTINO)
1414 error = ufs_dir_dd_ino(vp, cred, &dd_ino);
1417 if (dd_ino == source_ino) {
1421 if (dd_ino == ROOTINO)
1423 error = vn_vget_ino(vp, dd_ino, LK_EXCLUSIVE, &vp1);
1426 /* Recheck that ".." still points to vp1 after relock of vp */
1427 error = ufs_dir_dd_ino(vp, cred, &dd_ino);
1432 /* Redo the check of ".." if directory was reparented */
1433 if (dd_ino != VTOI(vp1)->i_number) {
1442 if (error == ENOTDIR)
1443 printf("checkpath: .. not a directory\n");