2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
39 #include <sys/cdefs.h>
41 #include "opt_quota.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/namei.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/sysctl.h>
56 #include <vm/vm_extern.h>
58 #include <ufs/ufs/extattr.h>
59 #include <ufs/ufs/quota.h>
60 #include <ufs/ufs/inode.h>
61 #include <ufs/ufs/dir.h>
63 #include <ufs/ufs/dirhash.h>
65 #include <ufs/ufs/ufsmount.h>
66 #include <ufs/ufs/ufs_extern.h>
67 #include <ufs/ffs/ffs_extern.h>
70 static int dirchk = 1;
72 static int dirchk = 0;
75 SYSCTL_INT(_debug, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
78 ufs_delete_denied(struct vnode *vdp, struct vnode *tdp, struct ucred *cred,
85 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
87 * 3.16.2.1. ACE4_DELETE vs. ACE4_DELETE_CHILD
91 * XXX: Is this check required?
93 error = VOP_ACCESS(vdp, VEXEC, cred, td);
97 error = VOP_ACCESSX(tdp, VDELETE, cred, td);
101 error = VOP_ACCESSX(vdp, VDELETE_CHILD, cred, td);
105 error = VOP_ACCESSX(vdp, VEXPLICIT_DENY | VDELETE_CHILD, cred, td);
109 #endif /* !UFS_ACL */
112 * Standard Unix access control - delete access requires VWRITE.
114 error = VOP_ACCESS(vdp, VWRITE, cred, td);
119 * If directory is "sticky", then user must own
120 * the directory, or the file in it, else she
121 * may not delete it (unless she's root). This
122 * implements append-only directories.
124 if ((VTOI(vdp)->i_mode & ISVTX) &&
125 VOP_ACCESS(vdp, VADMIN, cred, td) &&
126 VOP_ACCESS(tdp, VADMIN, cred, td))
133 * Convert a component of a pathname into a pointer to a locked inode.
134 * This is a very central and rather complicated routine.
135 * If the filesystem is not maintained in a strict tree hierarchy,
136 * this can result in a deadlock situation (see comments in code below).
138 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
139 * on whether the name is to be looked up, created, renamed, or deleted.
140 * When CREATE, RENAME, or DELETE is specified, information usable in
141 * creating, renaming, or deleting a directory entry may be calculated.
142 * If flag has LOCKPARENT or'ed into it and the target of the pathname
143 * exists, lookup returns both the target and its parent directory locked.
144 * When creating or renaming and LOCKPARENT is specified, the target may
145 * not be ".". When deleting and LOCKPARENT is specified, the target may
146 * be "."., but the caller must check to ensure it does an vrele and vput
147 * instead of two vputs.
149 * This routine is actually used as VOP_CACHEDLOOKUP method, and the
150 * filesystem employs the generic vfs_cache_lookup() as VOP_LOOKUP
153 * vfs_cache_lookup() performs the following for us:
154 * check that it is a directory
155 * check accessibility of directory
156 * check for modification attempts on read-only mounts
157 * if name found in cache
158 * if at end of path and deleting or creating
162 * return VOP_CACHEDLOOKUP()
164 * Overall outline of ufs_lookup:
166 * search for name in directory, to found or notfound
168 * if creating, return locked directory, leaving info on available slots
171 * if at end of path and deleting, return information to allow delete
172 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
173 * inode and return info to allow rewrite
174 * if not at end, add name to cache; if at end and neither creating
175 * nor deleting, add name to cache
179 struct vop_cachedlookup_args /* {
181 struct vnode **a_vpp;
182 struct componentname *a_cnp;
186 return (ufs_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
190 ufs_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
193 struct inode *dp; /* inode for directory being searched */
194 struct buf *bp; /* a buffer of directory entries */
195 struct direct *ep; /* the current directory entry */
196 int entryoffsetinblock; /* offset of ep in bp's buffer */
197 enum {NONE, COMPACT, FOUND} slotstatus;
198 doff_t slotoffset; /* offset of area with free space */
199 doff_t i_diroff; /* cached i_diroff value. */
200 doff_t i_offset; /* cached i_offset value. */
201 int slotsize; /* size of area at slotoffset */
202 int slotfreespace; /* amount of space free in slot */
203 int slotneeded; /* size of the entry we're seeking */
204 int numdirpasses; /* strategy for directory search */
205 doff_t endsearch; /* offset to end directory search */
206 doff_t prevoff; /* prev entry dp->i_offset */
207 struct vnode *pdp; /* saved dp during symlink work */
208 struct vnode *tdp; /* returned by VFS_VGET */
209 doff_t enduseful; /* pointer past last used dir slot */
210 uint64_t bmask; /* block offset mask */
212 struct ucred *cred = cnp->cn_cred;
213 int flags = cnp->cn_flags;
214 int nameiop = cnp->cn_nameiop;
222 if (dp->i_effnlink == 0)
226 * Create a vm object if vmiodirenable is enabled.
227 * Alternatively we could call vnode_create_vobject
228 * in VFS_VGET but we could end up creating objects
229 * that are never used.
231 vnode_create_vobject(vdp, DIP(dp, i_size), curthread);
233 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
235 #ifdef DEBUG_VFS_LOCKS
237 * Assert that the directory vnode is locked, and locked
238 * exclusively for the last component lookup for modifying
241 * The directory-modifying operations need to save
242 * intermediate state in the inode between namei() call and
243 * actual directory manipulations. See fields in the struct
244 * inode marked as 'used during directory lookup'. We must
245 * ensure that upgrade in namei() does not happen, since
246 * upgrade might need to unlock vdp. If quotas are enabled,
247 * getinoquota() also requires exclusive lock to modify inode.
249 ASSERT_VOP_LOCKED(vdp, "ufs_lookup1");
250 if ((nameiop == CREATE || nameiop == DELETE || nameiop == RENAME) &&
251 (flags & (LOCKPARENT | ISLASTCN)) == (LOCKPARENT | ISLASTCN))
252 ASSERT_VOP_ELOCKED(vdp, "ufs_lookup2");
260 * We now have a segment name to search for, and a directory to search.
262 * Suppress search for slots unless creating
263 * file and at end of pathname, in which case
264 * we watch for a place to put the new file in
265 * case it doesn't already exist.
268 i_diroff = dp->i_diroff;
270 slotfreespace = slotsize = slotneeded = 0;
271 if ((nameiop == CREATE || nameiop == RENAME) &&
272 (flags & ISLASTCN)) {
274 slotneeded = DIRECTSIZ(cnp->cn_namelen);
279 * Use dirhash for fast operations on large directories. The logic
280 * to determine whether to hash the directory is contained within
281 * ufsdirhash_build(); a zero return means that it decided to hash
282 * this directory and it successfully built up the hash table.
284 if (ufsdirhash_build(dp) == 0) {
285 /* Look for a free slot if needed. */
286 enduseful = dp->i_size;
287 if (slotstatus != FOUND) {
288 slotoffset = ufsdirhash_findfree(dp, slotneeded,
290 if (slotoffset >= 0) {
291 slotstatus = COMPACT;
292 enduseful = ufsdirhash_enduseful(dp);
294 enduseful = dp->i_size;
297 /* Look up the component. */
299 entryoffsetinblock = 0; /* silence compiler warning */
300 switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
301 &i_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) {
303 ep = (struct direct *)((char *)bp->b_data +
307 i_offset = roundup2(dp->i_size, DIRBLKSIZ);
310 /* Something failed; just do a linear search. */
314 #endif /* UFS_DIRHASH */
316 * If there is cached information on a previous search of
317 * this directory, pick up where we last left off.
318 * We cache only lookups as these are the most common
319 * and have the greatest payoff. Caching CREATE has little
320 * benefit as it usually must search the entire directory
321 * to determine that the entry does not exist. Caching the
322 * location of the last DELETE or RENAME has not reduced
323 * profiling time and hence has been removed in the interest
326 if (nameiop != LOOKUP || i_diroff == 0 || i_diroff >= dp->i_size) {
327 entryoffsetinblock = 0;
332 if ((entryoffsetinblock = i_offset & bmask) &&
333 (error = UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp)))
336 nchstats.ncs_2passes++;
339 endsearch = roundup2(dp->i_size, DIRBLKSIZ);
343 while (i_offset < endsearch) {
345 * If necessary, get the next directory block.
347 if ((i_offset & bmask) == 0) {
351 UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp);
354 entryoffsetinblock = 0;
357 * If still looking for a slot, and at a DIRBLKSIZE
358 * boundary, have to start looking for free space again.
360 if (slotstatus == NONE &&
361 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
366 * Get pointer to next entry.
367 * Full validation checks are slow, so we only check
368 * enough to insure forward progress through the
369 * directory. Complete checks can be run by patching
370 * "dirchk" to be true.
372 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
373 if (ep->d_reclen == 0 || ep->d_reclen >
374 DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
375 (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) {
378 ufs_dirbad(dp, i_offset, "mangled entry");
379 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
381 entryoffsetinblock += i;
386 * If an appropriate sized slot has not yet been found,
387 * check to see if one is available. Also accumulate space
388 * in the current block so that we can determine if
389 * compaction is viable.
391 if (slotstatus != FOUND) {
392 int size = ep->d_reclen;
395 size -= DIRSIZ(OFSFMT(vdp), ep);
397 if (size >= slotneeded) {
399 slotoffset = i_offset;
400 slotsize = ep->d_reclen;
401 } else if (slotstatus == NONE) {
402 slotfreespace += size;
403 if (slotoffset == -1)
404 slotoffset = i_offset;
405 if (slotfreespace >= slotneeded) {
406 slotstatus = COMPACT;
407 slotsize = i_offset +
408 ep->d_reclen - slotoffset;
415 * Check for a name match.
418 # if (BYTE_ORDER == LITTLE_ENDIAN)
422 namlen = ep->d_namlen;
424 namlen = ep->d_namlen;
426 if (namlen == cnp->cn_namelen &&
427 (cnp->cn_nameptr[0] == ep->d_name[0]) &&
428 !bcmp(cnp->cn_nameptr, ep->d_name,
434 * Save directory entry's inode number and
435 * reclen in ndp->ni_ufs area, and release
438 if (!OFSFMT(vdp) && ep->d_type == DT_WHT) {
440 slotoffset = i_offset;
441 slotsize = ep->d_reclen;
442 enduseful = dp->i_size;
443 cnp->cn_flags |= ISWHITEOUT;
452 i_offset += ep->d_reclen;
453 entryoffsetinblock += ep->d_reclen;
455 enduseful = i_offset;
459 * If we started in the middle of the directory and failed
460 * to find our target, we must check the beginning as well.
462 if (numdirpasses == 2) {
465 endsearch = i_diroff;
471 * If creating, and at end of pathname and current
472 * directory has not been removed, then can consider
473 * allowing file to be created.
475 if ((nameiop == CREATE || nameiop == RENAME ||
476 (nameiop == DELETE &&
477 (cnp->cn_flags & DOWHITEOUT) &&
478 (cnp->cn_flags & ISWHITEOUT))) &&
479 (flags & ISLASTCN) && dp->i_effnlink != 0) {
481 * Access for write is interpreted as allowing
482 * creation of files in the directory.
484 * XXX: Fix the comment above.
486 if (flags & WILLBEDIR)
487 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, curthread);
489 error = VOP_ACCESS(vdp, VWRITE, cred, curthread);
493 * Return an indication of where the new directory
494 * entry should be put. If we didn't find a slot,
495 * then set dp->i_count to 0 indicating
496 * that the new slot belongs at the end of the
497 * directory. If we found a slot, then the new entry
498 * can be put in the range from dp->i_offset to
499 * dp->i_offset + dp->i_count.
501 if (slotstatus == NONE) {
502 SET_I_OFFSET(dp, roundup2(dp->i_size, DIRBLKSIZ));
504 enduseful = I_OFFSET(dp);
505 } else if (nameiop == DELETE) {
506 SET_I_OFFSET(dp, slotoffset);
507 if ((I_OFFSET(dp) & (DIRBLKSIZ - 1)) == 0)
510 SET_I_COUNT(dp, I_OFFSET(dp) - prevoff);
512 SET_I_OFFSET(dp, slotoffset);
513 SET_I_COUNT(dp, slotsize);
514 if (enduseful < slotoffset + slotsize)
515 enduseful = slotoffset + slotsize;
517 SET_I_ENDOFF(dp, roundup2(enduseful, DIRBLKSIZ));
519 * We return with the directory locked, so that
520 * the parameters we set up above will still be
521 * valid if we actually decide to do a direnter().
522 * We return ni_vp == NULL to indicate that the entry
523 * does not currently exist; we leave a pointer to
524 * the (locked) directory inode in ndp->ni_dvp.
526 * NB - if the directory is unlocked, then this
527 * information cannot be used.
529 return (EJUSTRETURN);
532 * Insert name into cache (as non-existent) if appropriate.
534 if ((cnp->cn_flags & MAKEENTRY) != 0)
535 cache_enter(vdp, NULL, cnp);
541 if (numdirpasses == 2)
542 nchstats.ncs_pass2++;
544 * Check that directory length properly reflects presence
547 if (i_offset + DIRSIZ(OFSFMT(vdp), ep) > dp->i_size) {
548 ufs_dirbad(dp, i_offset, "i_size too small");
555 * Found component in pathname.
556 * If the final component of path name, save information
557 * in the cache as to where the entry was found.
559 if ((flags & ISLASTCN) && nameiop == LOOKUP)
560 dp->i_diroff = rounddown2(i_offset, DIRBLKSIZ);
563 * If deleting, and at end of pathname, return
564 * parameters which can be used to remove file.
566 if (nameiop == DELETE && (flags & ISLASTCN)) {
567 if (flags & LOCKPARENT)
568 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
570 if (VOP_ISLOCKED(vdp) == LK_EXCLUSIVE) {
572 * Return pointer to current entry in
573 * dp->i_offset, and distance past previous
574 * entry (if there is a previous entry in this
575 * block) in dp->i_count.
577 * We shouldn't be setting these in the
578 * WANTPARENT case (first lookup in rename()), but any
579 * lookups that will result in directory changes will
582 SET_I_OFFSET(dp, i_offset);
583 if ((I_OFFSET(dp) & (DIRBLKSIZ - 1)) == 0)
586 SET_I_COUNT(dp, I_OFFSET(dp) - prevoff);
592 * Save directory inode pointer in ndp->ni_dvp for
595 if ((error = VFS_VGET(vdp->v_mount, ino,
596 LK_EXCLUSIVE, &tdp)) != 0)
598 error = ufs_delete_denied(vdp, tdp, cred, curthread);
603 if (dp->i_number == ino) {
615 * If rewriting (RENAME), return the inode and the
616 * information required to rewrite the present directory
617 * Must get inode of directory entry to verify it's a
618 * regular file, or empty directory.
620 if (nameiop == RENAME && (flags & ISLASTCN)) {
621 if (flags & WILLBEDIR)
622 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, curthread);
624 error = VOP_ACCESS(vdp, VWRITE, cred, curthread);
628 * Careful about locking second inode.
629 * This can only occur if the target is ".".
631 SET_I_OFFSET(dp, i_offset);
632 if (dp->i_number == ino)
636 if ((error = VFS_VGET(vdp->v_mount, ino,
637 LK_EXCLUSIVE, &tdp)) != 0)
640 error = ufs_delete_denied(vdp, tdp, cred, curthread);
646 #ifdef SunOS_doesnt_do_that
648 * The only purpose of this check is to return the correct
649 * error. Assume that we want to rename directory "a"
650 * to a file "b", and that we have no ACL_WRITE_DATA on
651 * a containing directory, but we _do_ have ACL_APPEND_DATA.
652 * In that case, the VOP_ACCESS check above will return 0,
653 * and the operation will fail with ENOTDIR instead
656 if (tdp->v_type == VDIR)
657 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, curthread);
659 error = VOP_ACCESS(vdp, VWRITE, cred, curthread);
673 * Step through the translation in the name. We do not `vput' the
674 * directory because we may need it again if a symbolic link
675 * is relative to the current directory. Instead we save it
676 * unlocked as "pdp". We must get the target inode before unlocking
677 * the directory to insure that the inode will not be removed
678 * before we get it. We prevent deadlock by always fetching
679 * inodes from the root, moving down the directory tree. Thus
680 * when following backward pointers ".." we must unlock the
681 * parent directory before getting the requested directory.
682 * There is a potential race condition here if both the current
683 * and parent directories are removed before the VFS_VGET for the
684 * inode associated with ".." returns. We hope that this occurs
685 * infrequently since we cannot avoid this race condition without
686 * implementing a sophisticated deadlock detection algorithm.
687 * Note also that this simple deadlock detection scheme will not
688 * work if the filesystem has any hard links other than ".."
689 * that point backwards in the directory structure.
692 if (flags & ISDOTDOT) {
693 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
698 * Recheck that ".." entry in the vdp directory points
699 * to the inode we looked up before vdp lock was
702 error = ufs_lookup_ino(pdp, NULL, cnp, &ino1);
713 } else if (dp->i_number == ino) {
714 VREF(vdp); /* we want ourself, ie "." */
716 * When we lookup "." we still can be asked to lock it
719 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
720 if (ltype != VOP_ISLOCKED(vdp)) {
721 if (ltype == LK_EXCLUSIVE)
722 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
723 else /* if (ltype == LK_SHARED) */
724 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
726 * Relock for the "." case may left us with
729 if (VN_IS_DOOMED(vdp)) {
736 error = VFS_VGET(pdp->v_mount, ino, cnp->cn_lkflags, &tdp);
737 if (error == 0 && VTOI(tdp)->i_mode == 0) {
748 * Insert name into cache if appropriate.
750 if (cnp->cn_flags & MAKEENTRY)
751 cache_enter(vdp, *vpp, cnp);
756 ufs_dirbad(struct inode *ip, doff_t offset, char *how)
759 (void)printf("%s: bad dir ino %ju at offset %ld: %s\n",
760 ITOV(ip)->v_mount->mnt_stat.f_mntonname, (uintmax_t)ip->i_number,
765 * Do consistency checking on a directory entry:
766 * record length must be multiple of 4
767 * entry must fit in rest of its DIRBLKSIZ block
768 * record must be large enough to contain entry
769 * name is not longer than UFS_MAXNAMLEN
770 * name must be as long as advertised, and null terminated
773 ufs_dirbadentry(struct vnode *dp, struct direct *ep, int entryoffsetinblock)
777 # if (BYTE_ORDER == LITTLE_ENDIAN)
781 namlen = ep->d_namlen;
783 namlen = ep->d_namlen;
785 if ((ep->d_reclen & 0x3) != 0 ||
786 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
787 ep->d_reclen < DIRSIZ(OFSFMT(dp), ep) || namlen > UFS_MAXNAMLEN) {
789 printf("First bad\n");
794 for (i = 0; i < namlen; i++)
795 if (ep->d_name[i] == '\0') {
797 printf("Second bad\n");
808 * Construct a new directory entry after a call to namei, using the
809 * parameters that it left in the componentname argument cnp. The
810 * argument ip is the inode to which the new directory entry will refer.
813 ufs_makedirentry(struct inode *ip, struct componentname *cnp,
814 struct direct *newdirp)
818 namelen = (unsigned)cnp->cn_namelen;
819 KASSERT(namelen <= UFS_MAXNAMLEN,
820 ("ufs_makedirentry: name too long"));
821 newdirp->d_ino = ip->i_number;
822 newdirp->d_namlen = namelen;
824 /* Zero out after-name padding */
825 *(uint32_t *)(&newdirp->d_name[namelen & ~(DIR_ROUNDUP - 1)]) = 0;
827 bcopy(cnp->cn_nameptr, newdirp->d_name, namelen);
829 if (!OFSFMT(ITOV(ip)))
830 newdirp->d_type = IFTODT(ip->i_mode);
833 # if (BYTE_ORDER == LITTLE_ENDIAN)
834 { uint8_t tmp = newdirp->d_namlen;
835 newdirp->d_namlen = newdirp->d_type;
836 newdirp->d_type = tmp; }
842 * Write a directory entry after a call to namei, using the parameters
843 * that it left in nameidata. The argument dirp is the new directory
844 * entry contents. Dvp is a pointer to the directory to be written,
845 * which was left locked by namei. Remaining parameters (dp->i_offset,
846 * dp->i_count) indicate how the space for the new entry is to be obtained.
847 * Non-null bp indicates that a directory is being created (for the
848 * soft dependency code).
851 ufs_direnter(struct vnode *dvp, struct vnode *tvp, struct direct *dirp,
852 struct componentname *cnp, struct buf *newdirbp)
860 struct direct *ep, *nep;
862 int error, ret, blkoff, loc, spacefree, flags, namlen;
865 td = curthread; /* XXX */
869 newentrysize = DIRSIZ(OFSFMT(dvp), dirp);
871 if (I_COUNT(dp) == 0) {
873 * If dp->i_count is 0, then namei could find no
874 * space in the directory. Here, dp->i_offset will
875 * be on a directory block boundary and we will write the
876 * new entry into a fresh block.
878 if (I_OFFSET(dp) & (DIRBLKSIZ - 1))
879 panic("ufs_direnter: newblk");
881 if (!DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp))
884 if ((error = getinoquota(dp)) != 0) {
885 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
890 old_isize = dp->i_size;
891 vnode_pager_setsize(dvp,
892 (vm_ooffset_t)I_OFFSET(dp) + DIRBLKSIZ);
893 if ((error = UFS_BALLOC(dvp, (off_t)I_OFFSET(dp), DIRBLKSIZ,
894 cr, flags, &bp)) != 0) {
895 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
897 vnode_pager_setsize(dvp, (vm_ooffset_t)old_isize);
900 dp->i_size = I_OFFSET(dp) + DIRBLKSIZ;
901 DIP_SET(dp, i_size, dp->i_size);
902 SET_I_ENDOFF(dp, dp->i_size);
903 UFS_INODE_SET_FLAG(dp, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
904 dirp->d_reclen = DIRBLKSIZ;
905 blkoff = I_OFFSET(dp) &
906 (VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_iosize - 1);
907 bcopy((caddr_t)dirp, (caddr_t)bp->b_data + blkoff,newentrysize);
909 if (dp->i_dirhash != NULL) {
910 ufsdirhash_newblk(dp, I_OFFSET(dp));
911 ufsdirhash_add(dp, dirp, I_OFFSET(dp));
912 ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff,
916 if (DOINGSOFTDEP(dvp)) {
918 * Ensure that the entire newly allocated block is a
919 * valid directory so that future growth within the
920 * block does not have to ensure that the block is
921 * written before the inode.
924 while (blkoff < bp->b_bcount) {
926 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
929 if (softdep_setup_directory_add(bp, dp, I_OFFSET(dp),
930 dirp->d_ino, newdirbp, 1))
931 UFS_INODE_SET_FLAG(dp, IN_NEEDSYNC);
935 return (UFS_UPDATE(dvp, 0));
937 if (DOINGASYNC(dvp)) {
939 return (UFS_UPDATE(dvp, 0));
942 ret = UFS_UPDATE(dvp, 1);
949 * If dp->i_count is non-zero, then namei found space for the new
950 * entry in the range dp->i_offset to dp->i_offset + dp->i_count
951 * in the directory. To use this space, we may have to compact
952 * the entries located there, by copying them together towards the
953 * beginning of the block, leaving the free space in one usable
958 * Increase size of directory if entry eats into new space.
959 * This should never push the size past a new multiple of
962 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
964 if (I_OFFSET(dp) + I_COUNT(dp) > dp->i_size) {
965 dp->i_size = I_OFFSET(dp) + I_COUNT(dp);
966 DIP_SET(dp, i_size, dp->i_size);
967 UFS_INODE_SET_FLAG(dp, IN_SIZEMOD | IN_MODIFIED);
970 * Get the block containing the space for the new directory entry.
972 error = UFS_BLKATOFF(dvp, (off_t)I_OFFSET(dp), &dirbuf, &bp);
974 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
979 * Find space for the new entry. In the simple case, the entry at
980 * offset base will have the space. If it does not, then namei
981 * arranged that compacting the region dp->i_offset to
982 * dp->i_offset + dp->i_count would yield the space.
984 ep = (struct direct *)dirbuf;
985 dsize = ep->d_ino ? DIRSIZ(OFSFMT(dvp), ep) : 0;
986 spacefree = ep->d_reclen - dsize;
987 for (loc = ep->d_reclen; loc < I_COUNT(dp); ) {
988 nep = (struct direct *)(dirbuf + loc);
990 /* Trim the existing slot (NB: dsize may be zero). */
991 ep->d_reclen = dsize;
992 ep = (struct direct *)((char *)ep + dsize);
994 /* Read nep->d_reclen now as the bcopy() may clobber it. */
995 loc += nep->d_reclen;
996 if (nep->d_ino == 0) {
998 * A mid-block unused entry. Such entries are
999 * never created by the kernel, but fsck_ffs
1000 * can create them (and it doesn't fix them).
1002 * Add up the free space, and initialise the
1003 * relocated entry since we don't bcopy it.
1005 spacefree += nep->d_reclen;
1010 dsize = DIRSIZ(OFSFMT(dvp), nep);
1011 spacefree += nep->d_reclen - dsize;
1013 if (dp->i_dirhash != NULL)
1014 ufsdirhash_move(dp, nep,
1015 I_OFFSET(dp) + ((char *)nep - dirbuf),
1016 I_OFFSET(dp) + ((char *)ep - dirbuf));
1018 if (DOINGSOFTDEP(dvp))
1019 softdep_change_directoryentry_offset(bp, dp, dirbuf,
1020 (caddr_t)nep, (caddr_t)ep, dsize);
1022 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1025 * Here, `ep' points to a directory entry containing `dsize' in-use
1026 * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
1027 * then the entry is completely unused (dsize == 0). The value
1028 * of ep->d_reclen is always indeterminate.
1030 * Update the pointer fields in the previous entry (if any),
1031 * copy in the new entry, and write out the block.
1033 # if (BYTE_ORDER == LITTLE_ENDIAN)
1035 namlen = ep->d_type;
1037 namlen = ep->d_namlen;
1039 namlen = ep->d_namlen;
1041 if (ep->d_ino == 0 ||
1042 (ep->d_ino == UFS_WINO && namlen == dirp->d_namlen &&
1043 bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
1044 if (spacefree + dsize < newentrysize)
1045 panic("ufs_direnter: compact1");
1046 dirp->d_reclen = spacefree + dsize;
1048 if (spacefree < newentrysize)
1049 panic("ufs_direnter: compact2");
1050 dirp->d_reclen = spacefree;
1051 ep->d_reclen = dsize;
1052 ep = (struct direct *)((char *)ep + dsize);
1055 if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
1056 dirp->d_reclen == spacefree))
1057 ufsdirhash_add(dp, dirp, I_OFFSET(dp) + ((char *)ep - dirbuf));
1059 bcopy((caddr_t)dirp, (caddr_t)ep, (uint64_t)newentrysize);
1061 if (dp->i_dirhash != NULL)
1062 ufsdirhash_checkblock(dp, dirbuf -
1063 (I_OFFSET(dp) & (DIRBLKSIZ - 1)),
1064 rounddown2(I_OFFSET(dp), DIRBLKSIZ));
1067 if (DOINGSOFTDEP(dvp)) {
1068 (void) softdep_setup_directory_add(bp, dp,
1069 I_OFFSET(dp) + (caddr_t)ep - dirbuf,
1070 dirp->d_ino, newdirbp, 0);
1071 if (newdirbp != NULL)
1075 if (DOINGASYNC(dvp)) {
1084 * If all went well, and the directory can be shortened,
1085 * mark directory inode with the truncation request.
1087 UFS_INODE_SET_FLAG(dp, IN_CHANGE | IN_UPDATE | (error == 0 &&
1088 I_ENDOFF(dp) != 0 && I_ENDOFF(dp) < dp->i_size ? IN_ENDOFF : 0));
1094 * Remove a directory entry after a call to namei, using
1095 * the parameters which it left in nameidata. The entry
1096 * dp->i_offset contains the offset into the directory of the
1097 * entry to be eliminated. The dp->i_count field contains the
1098 * size of the previous record in the directory. If this
1099 * is 0, the first entry is being deleted, so we need only
1100 * zero the inode number to mark the entry as free. If the
1101 * entry is not the first in the directory, we must reclaim
1102 * the space of the now empty record by adding the record size
1103 * to the size of the previous entry.
1106 ufs_dirremove(struct vnode *dvp, struct inode *ip, int flags, int isrmdir)
1109 struct direct *ep, *rep;
1117 * Adjust the link count early so softdep can block if necessary.
1121 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1122 if (DOINGSOFTDEP(dvp)) {
1123 softdep_setup_unlink(dp, ip);
1126 DIP_SET(ip, i_nlink, ip->i_nlink);
1127 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1130 if (flags & DOWHITEOUT)
1131 offset = I_OFFSET(dp);
1133 offset = I_OFFSET(dp) - I_COUNT(dp);
1134 if ((error = UFS_BLKATOFF(dvp, offset, (char **)&ep, &bp)) != 0) {
1137 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1138 if (DOINGSOFTDEP(dvp)) {
1139 softdep_change_linkcnt(ip);
1142 DIP_SET(ip, i_nlink, ip->i_nlink);
1143 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
1148 if (flags & DOWHITEOUT) {
1150 * Whiteout entry: set d_ino to UFS_WINO.
1152 ep->d_ino = UFS_WINO;
1153 ep->d_type = DT_WHT;
1156 /* Set 'rep' to the entry being removed. */
1157 if (I_COUNT(dp) == 0)
1160 rep = (struct direct *)((char *)ep + ep->d_reclen);
1163 * Remove the dirhash entry. This is complicated by the fact
1164 * that `ep' is the previous entry when dp->i_count != 0.
1166 if (dp->i_dirhash != NULL)
1167 ufsdirhash_remove(dp, rep, I_OFFSET(dp));
1169 if (ip && rep->d_ino != ip->i_number)
1170 panic("ufs_dirremove: ip %ju does not match dirent ino %ju\n",
1171 (uintmax_t)ip->i_number, (uintmax_t)rep->d_ino);
1173 * Zero out the file directory entry metadata to reduce disk
1174 * scavenging disclosure.
1176 bzero(&rep->d_name[0], rep->d_namlen);
1181 if (I_COUNT(dp) != 0) {
1183 * Collapse new free space into previous entry.
1185 ep->d_reclen += rep->d_reclen;
1189 if (dp->i_dirhash != NULL)
1190 ufsdirhash_checkblock(dp, (char *)ep -
1191 ((I_OFFSET(dp) - I_COUNT(dp)) & (DIRBLKSIZ - 1)),
1192 rounddown2(I_OFFSET(dp), DIRBLKSIZ));
1196 if (DOINGSOFTDEP(dvp)) {
1198 softdep_setup_remove(bp, dp, ip, isrmdir);
1199 if (softdep_slowdown(dvp))
1204 if (flags & DOWHITEOUT)
1206 else if (DOINGASYNC(dvp))
1211 UFS_INODE_SET_FLAG(dp, IN_CHANGE | IN_UPDATE);
1213 * If the last named reference to a snapshot goes away,
1214 * drop its snapshot reference so that it will be reclaimed
1215 * when last open reference goes away.
1217 if (ip != NULL && IS_SNAPSHOT(ip) && ip->i_effnlink == 0)
1223 * Rewrite an existing directory entry to point at the inode
1224 * supplied. The parameters describing the directory entry are
1225 * set up by a call to namei.
1228 ufs_dirrewrite(struct inode *dp, struct inode *oip, ino_t newinum, int newtype,
1233 struct vnode *vdp = ITOV(dp);
1237 * Drop the link before we lock the buf so softdep can block if
1241 UFS_INODE_SET_FLAG(oip, IN_CHANGE);
1242 if (DOINGSOFTDEP(vdp)) {
1243 softdep_setup_unlink(dp, oip);
1246 DIP_SET(oip, i_nlink, oip->i_nlink);
1247 UFS_INODE_SET_FLAG(oip, IN_CHANGE);
1250 error = UFS_BLKATOFF(vdp, (off_t)I_OFFSET(dp), (char **)&ep, &bp);
1251 if (error == 0 && ep->d_namlen == 2 && ep->d_name[1] == '.' &&
1252 ep->d_name[0] == '.' && ep->d_ino != oip->i_number) {
1258 UFS_INODE_SET_FLAG(oip, IN_CHANGE);
1259 if (DOINGSOFTDEP(vdp)) {
1260 softdep_change_linkcnt(oip);
1263 DIP_SET(oip, i_nlink, oip->i_nlink);
1264 UFS_INODE_SET_FLAG(oip, IN_CHANGE);
1268 ep->d_ino = newinum;
1270 ep->d_type = newtype;
1271 if (DOINGSOFTDEP(vdp)) {
1272 softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir);
1275 if (DOINGASYNC(vdp)) {
1282 UFS_INODE_SET_FLAG(dp, IN_CHANGE | IN_UPDATE);
1284 * If the last named reference to a snapshot goes away,
1285 * drop its snapshot reference so that it will be reclaimed
1286 * when last open reference goes away.
1288 if (IS_SNAPSHOT(oip) && oip->i_effnlink == 0)
1294 * Check if a directory is empty or not.
1295 * Inode supplied must be locked.
1297 * Using a struct dirtemplate here is not precisely
1298 * what we want, but better than using a struct direct.
1300 * NB: does not handle corrupted directories.
1303 ufs_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
1306 struct dirtemplate dbuf;
1307 struct direct *dp = (struct direct *)&dbuf;
1310 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1312 for (off = 0; off < ip->i_size; off += dp->d_reclen) {
1313 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1314 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1315 NOCRED, &count, (struct thread *)0);
1317 * Since we read MINDIRSIZ, residual must
1318 * be 0 unless we're at end of file.
1320 if (error || count != 0)
1322 /* avoid infinite loops */
1323 if (dp->d_reclen == 0)
1325 /* skip empty entries */
1326 if (dp->d_ino == 0 || dp->d_ino == UFS_WINO)
1328 /* accept only "." and ".." */
1329 # if (BYTE_ORDER == LITTLE_ENDIAN)
1330 if (OFSFMT(ITOV(ip)))
1331 namlen = dp->d_type;
1333 namlen = dp->d_namlen;
1335 namlen = dp->d_namlen;
1339 if (dp->d_name[0] != '.')
1342 * At this point namlen must be 1 or 2.
1343 * 1 implies ".", 2 implies ".." if second
1346 if (namlen == 1 && dp->d_ino == ip->i_number)
1348 if (dp->d_name[1] == '.' && dp->d_ino == parentino)
1356 ufs_dir_dd_ino(struct vnode *vp, struct ucred *cred, ino_t *dd_ino,
1357 struct vnode **dd_vp)
1359 struct dirtemplate dirbuf;
1363 ASSERT_VOP_LOCKED(vp, "ufs_dir_dd_ino");
1365 if (vp->v_type != VDIR)
1368 * First check to see if we have it in the name cache.
1370 if ((ddvp = vn_dir_dd_ino(vp)) != NULL) {
1371 KASSERT(ddvp->v_mount == vp->v_mount,
1372 ("ufs_dir_dd_ino: Unexpected mount point crossing"));
1373 *dd_ino = VTOI(ddvp)->i_number;
1378 * Have to read the directory.
1380 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1381 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1382 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL, NULL);
1385 #if (BYTE_ORDER == LITTLE_ENDIAN)
1387 namlen = dirbuf.dotdot_type;
1389 namlen = dirbuf.dotdot_namlen;
1391 namlen = dirbuf.dotdot_namlen;
1393 if (namlen != 2 || dirbuf.dotdot_name[0] != '.' ||
1394 dirbuf.dotdot_name[1] != '.')
1396 *dd_ino = dirbuf.dotdot_ino;
1401 * Check if source directory is in the path of the target directory.
1404 ufs_checkpath(ino_t source_ino, ino_t parent_ino, struct inode *target,
1405 struct ucred *cred, ino_t *wait_ino)
1408 struct vnode *tvp, *vp, *vp1;
1412 vp = tvp = ITOV(target);
1415 sx_assert(&VFSTOUFS(mp)->um_checkpath_lock, SA_XLOCKED);
1417 if (target->i_number == source_ino)
1419 if (target->i_number == parent_ino)
1421 if (target->i_number == UFS_ROOTINO)
1424 error = ufs_dir_dd_ino(vp, cred, &dd_ino, &vp1);
1427 if (dd_ino == source_ino) {
1431 if (dd_ino == UFS_ROOTINO)
1433 if (dd_ino == parent_ino)
1436 error = VFS_VGET(mp, dd_ino, LK_SHARED | LK_NOWAIT,
1443 KASSERT(dd_ino == VTOI(vp1)->i_number,
1444 ("directory %ju reparented\n",
1445 (uintmax_t)VTOI(vp1)->i_number));
1451 if (error == ENOTDIR)
1452 panic("checkpath: .. not a directory\n");
1462 ufs_assert_inode_offset_owner(struct inode *ip, struct iown_tracker *tr,
1463 const char *name, const char *file, int line)
1467 snprintf(msg, sizeof(msg), "at %s@%d", file, line);
1468 ASSERT_VOP_ELOCKED(ITOV(ip), msg);
1469 MPASS((ip->i_mode & IFMT) == IFDIR);
1470 if (curthread == tr->tr_owner && ip->i_lock_gen == tr->tr_gen)
1472 printf("locked at\n");
1473 stack_print(&tr->tr_st);
1474 printf("unlocked at\n");
1475 stack_print(&tr->tr_unlock);
1476 panic("%s ip %p %jd offset owner %p %d gen %d "
1477 "curthread %p %d gen %d at %s@%d\n",
1478 name, ip, (uintmax_t)ip->i_number, tr->tr_owner,
1479 tr->tr_owner->td_tid, tr->tr_gen,
1480 curthread, curthread->td_tid, ip->i_lock_gen,
1485 ufs_set_inode_offset_owner(struct inode *ip, struct iown_tracker *tr,
1486 const char *file, int line)
1490 snprintf(msg, sizeof(msg), "at %s@%d", file, line);
1491 ASSERT_VOP_ELOCKED(ITOV(ip), msg);
1492 MPASS((ip->i_mode & IFMT) == IFDIR);
1493 tr->tr_owner = curthread;
1494 tr->tr_gen = ip->i_lock_gen;
1495 stack_save(&tr->tr_st);
1499 ufs_init_one_tracker(struct iown_tracker *tr)
1501 tr->tr_owner = NULL;
1502 stack_zero(&tr->tr_st);
1506 ufs_init_trackers(struct inode *ip)
1508 ufs_init_one_tracker(&ip->i_offset_tracker);
1509 ufs_init_one_tracker(&ip->i_count_tracker);
1510 ufs_init_one_tracker(&ip->i_endoff_tracker);
1514 ufs_unlock_tracker(struct inode *ip)
1516 if (ip->i_count_tracker.tr_gen == ip->i_lock_gen)
1517 stack_save(&ip->i_count_tracker.tr_unlock);
1518 if (ip->i_offset_tracker.tr_gen == ip->i_lock_gen)
1519 stack_save(&ip->i_offset_tracker.tr_unlock);
1520 if (ip->i_endoff_tracker.tr_gen == ip->i_lock_gen)
1521 stack_save(&ip->i_endoff_tracker.tr_unlock);
1526 ufs_get_i_offset(struct inode *ip, const char *file, int line)
1528 ufs_assert_inode_offset_owner(ip, &ip->i_offset_tracker, "i_offset",
1530 return (ip->i_offset);
1534 ufs_set_i_offset(struct inode *ip, doff_t off, const char *file, int line)
1536 ufs_set_inode_offset_owner(ip, &ip->i_offset_tracker, file, line);
1541 ufs_get_i_count(struct inode *ip, const char *file, int line)
1543 ufs_assert_inode_offset_owner(ip, &ip->i_count_tracker, "i_count",
1545 return (ip->i_count);
1549 ufs_set_i_count(struct inode *ip, int32_t cnt, const char *file, int line)
1551 ufs_set_inode_offset_owner(ip, &ip->i_count_tracker, file, line);
1556 ufs_get_i_endoff(struct inode *ip, const char *file, int line)
1558 ufs_assert_inode_offset_owner(ip, &ip->i_endoff_tracker, "i_endoff",
1560 return (ip->i_endoff);
1564 ufs_set_i_endoff(struct inode *ip, doff_t off, const char *file, int line)
1566 ufs_set_inode_offset_owner(ip, &ip->i_endoff_tracker, file, line);