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 *,
84 ufs_delete_denied(struct vnode *vdp, struct vnode *tdp, struct ucred *cred,
91 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
93 * 3.16.2.1. ACE4_DELETE vs. ACE4_DELETE_CHILD
97 * XXX: Is this check required?
99 error = VOP_ACCESS(vdp, VEXEC, cred, td);
103 error = VOP_ACCESSX(tdp, VDELETE, cred, td);
107 error = VOP_ACCESSX(vdp, VDELETE_CHILD, cred, td);
111 error = VOP_ACCESSX(vdp, VEXPLICIT_DENY | VDELETE_CHILD, cred, td);
115 #endif /* !UFS_ACL */
118 * Standard Unix access control - delete access requires VWRITE.
120 error = VOP_ACCESS(vdp, VWRITE, cred, td);
125 * If directory is "sticky", then user must own
126 * the directory, or the file in it, else she
127 * may not delete it (unless she's root). This
128 * implements append-only directories.
130 if ((VTOI(vdp)->i_mode & ISVTX) &&
131 VOP_ACCESS(vdp, VADMIN, cred, td) &&
132 VOP_ACCESS(tdp, VADMIN, cred, td))
139 * Convert a component of a pathname into a pointer to a locked inode.
140 * This is a very central and rather complicated routine.
141 * If the filesystem is not maintained in a strict tree hierarchy,
142 * this can result in a deadlock situation (see comments in code below).
144 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
145 * on whether the name is to be looked up, created, renamed, or deleted.
146 * When CREATE, RENAME, or DELETE is specified, information usable in
147 * creating, renaming, or deleting a directory entry may be calculated.
148 * If flag has LOCKPARENT or'ed into it and the target of the pathname
149 * exists, lookup returns both the target and its parent directory locked.
150 * When creating or renaming and LOCKPARENT is specified, the target may
151 * not be ".". When deleting and LOCKPARENT is specified, the target may
152 * be "."., but the caller must check to ensure it does an vrele and vput
153 * instead of two vputs.
155 * This routine is actually used as VOP_CACHEDLOOKUP method, and the
156 * filesystem employs the generic vfs_cache_lookup() as VOP_LOOKUP
159 * vfs_cache_lookup() performs the following for us:
160 * check that it is a directory
161 * check accessibility of directory
162 * check for modification attempts on read-only mounts
163 * if name found in cache
164 * if at end of path and deleting or creating
168 * return VOP_CACHEDLOOKUP()
170 * Overall outline of ufs_lookup:
172 * search for name in directory, to found or notfound
174 * if creating, return locked directory, leaving info on available slots
177 * if at end of path and deleting, return information to allow delete
178 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
179 * inode and return info to allow rewrite
180 * if not at end, add name to cache; if at end and neither creating
181 * nor deleting, add name to cache
185 struct vop_cachedlookup_args /* {
187 struct vnode **a_vpp;
188 struct componentname *a_cnp;
192 return (ufs_lookup_(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
196 ufs_lookup_(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
199 struct inode *dp; /* inode for directory being searched */
200 struct buf *bp; /* a buffer of directory entries */
201 struct direct *ep; /* the current directory entry */
202 int entryoffsetinblock; /* offset of ep in bp's buffer */
203 enum {NONE, COMPACT, FOUND} slotstatus;
204 doff_t slotoffset; /* offset of area with free space */
205 doff_t i_diroff; /* cached i_diroff value. */
206 doff_t i_offset; /* cached i_offset value. */
207 int slotsize; /* size of area at slotoffset */
208 int slotfreespace; /* amount of space free in slot */
209 int slotneeded; /* size of the entry we're seeking */
210 int numdirpasses; /* strategy for directory search */
211 doff_t endsearch; /* offset to end directory search */
212 doff_t prevoff; /* prev entry dp->i_offset */
213 struct vnode *pdp; /* saved dp during symlink work */
214 struct vnode *tdp; /* returned by VFS_VGET */
215 doff_t enduseful; /* pointer past last used dir slot */
216 u_long bmask; /* block offset mask */
218 struct ucred *cred = cnp->cn_cred;
219 int flags = cnp->cn_flags;
220 int nameiop = cnp->cn_nameiop;
230 * Create a vm object if vmiodirenable is enabled.
231 * Alternatively we could call vnode_create_vobject
232 * in VFS_VGET but we could end up creating objects
233 * that are never used.
235 vnode_create_vobject(vdp, DIP(dp, i_size), cnp->cn_thread);
237 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
244 * We now have a segment name to search for, and a directory to search.
246 * Suppress search for slots unless creating
247 * file and at end of pathname, in which case
248 * we watch for a place to put the new file in
249 * case it doesn't already exist.
252 i_diroff = dp->i_diroff;
254 slotfreespace = slotsize = slotneeded = 0;
255 if ((nameiop == CREATE || nameiop == RENAME) &&
256 (flags & ISLASTCN)) {
258 slotneeded = DIRECTSIZ(cnp->cn_namelen);
263 * Use dirhash for fast operations on large directories. The logic
264 * to determine whether to hash the directory is contained within
265 * ufsdirhash_build(); a zero return means that it decided to hash
266 * this directory and it successfully built up the hash table.
268 if (ufsdirhash_build(dp) == 0) {
269 /* Look for a free slot if needed. */
270 enduseful = dp->i_size;
271 if (slotstatus != FOUND) {
272 slotoffset = ufsdirhash_findfree(dp, slotneeded,
274 if (slotoffset >= 0) {
275 slotstatus = COMPACT;
276 enduseful = ufsdirhash_enduseful(dp);
278 enduseful = dp->i_size;
281 /* Look up the component. */
283 entryoffsetinblock = 0; /* silence compiler warning */
284 switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
285 &i_offset, &bp, nameiop == DELETE ? &prevoff : NULL)) {
287 ep = (struct direct *)((char *)bp->b_data +
291 i_offset = roundup2(dp->i_size, DIRBLKSIZ);
294 /* Something failed; just do a linear search. */
298 #endif /* UFS_DIRHASH */
300 * If there is cached information on a previous search of
301 * this directory, pick up where we last left off.
302 * We cache only lookups as these are the most common
303 * and have the greatest payoff. Caching CREATE has little
304 * benefit as it usually must search the entire directory
305 * to determine that the entry does not exist. Caching the
306 * location of the last DELETE or RENAME has not reduced
307 * profiling time and hence has been removed in the interest
310 if (nameiop != LOOKUP || i_diroff == 0 || i_diroff >= dp->i_size) {
311 entryoffsetinblock = 0;
316 if ((entryoffsetinblock = i_offset & bmask) &&
317 (error = UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp)))
320 nchstats.ncs_2passes++;
323 endsearch = roundup2(dp->i_size, DIRBLKSIZ);
327 while (i_offset < endsearch) {
329 * If necessary, get the next directory block.
331 if ((i_offset & bmask) == 0) {
335 UFS_BLKATOFF(vdp, (off_t)i_offset, NULL, &bp);
338 entryoffsetinblock = 0;
341 * If still looking for a slot, and at a DIRBLKSIZE
342 * boundary, have to start looking for free space again.
344 if (slotstatus == NONE &&
345 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
350 * Get pointer to next entry.
351 * Full validation checks are slow, so we only check
352 * enough to insure forward progress through the
353 * directory. Complete checks can be run by patching
354 * "dirchk" to be true.
356 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
357 if (ep->d_reclen == 0 || ep->d_reclen >
358 DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
359 (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) {
362 ufs_dirbad(dp, i_offset, "mangled entry");
363 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
365 entryoffsetinblock += i;
370 * If an appropriate sized slot has not yet been found,
371 * check to see if one is available. Also accumulate space
372 * in the current block so that we can determine if
373 * compaction is viable.
375 if (slotstatus != FOUND) {
376 int size = ep->d_reclen;
379 size -= DIRSIZ(OFSFMT(vdp), ep);
381 if (size >= slotneeded) {
383 slotoffset = i_offset;
384 slotsize = ep->d_reclen;
385 } else if (slotstatus == NONE) {
386 slotfreespace += size;
387 if (slotoffset == -1)
388 slotoffset = i_offset;
389 if (slotfreespace >= slotneeded) {
390 slotstatus = COMPACT;
391 slotsize = i_offset +
392 ep->d_reclen - slotoffset;
399 * Check for a name match.
402 # if (BYTE_ORDER == LITTLE_ENDIAN)
406 namlen = ep->d_namlen;
408 namlen = ep->d_namlen;
410 if (namlen == cnp->cn_namelen &&
411 (cnp->cn_nameptr[0] == ep->d_name[0]) &&
412 !bcmp(cnp->cn_nameptr, ep->d_name,
418 * Save directory entry's inode number and
419 * reclen in ndp->ni_ufs area, and release
422 if (vdp->v_mount->mnt_maxsymlinklen > 0 &&
423 ep->d_type == DT_WHT) {
425 slotoffset = i_offset;
426 slotsize = ep->d_reclen;
427 enduseful = dp->i_size;
428 cnp->cn_flags |= ISWHITEOUT;
437 i_offset += ep->d_reclen;
438 entryoffsetinblock += ep->d_reclen;
440 enduseful = i_offset;
444 * If we started in the middle of the directory and failed
445 * to find our target, we must check the beginning as well.
447 if (numdirpasses == 2) {
450 endsearch = i_diroff;
456 * If creating, and at end of pathname and current
457 * directory has not been removed, then can consider
458 * allowing file to be created.
460 if ((nameiop == CREATE || nameiop == RENAME ||
461 (nameiop == DELETE &&
462 (cnp->cn_flags & DOWHITEOUT) &&
463 (cnp->cn_flags & ISWHITEOUT))) &&
464 (flags & ISLASTCN) && dp->i_effnlink != 0) {
466 * Access for write is interpreted as allowing
467 * creation of files in the directory.
469 * XXX: Fix the comment above.
471 if (flags & WILLBEDIR)
472 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread);
474 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread);
478 * Return an indication of where the new directory
479 * entry should be put. If we didn't find a slot,
480 * then set dp->i_count to 0 indicating
481 * that the new slot belongs at the end of the
482 * directory. If we found a slot, then the new entry
483 * can be put in the range from dp->i_offset to
484 * dp->i_offset + dp->i_count.
486 if (slotstatus == NONE) {
487 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
489 enduseful = dp->i_offset;
490 } else if (nameiop == DELETE) {
491 dp->i_offset = slotoffset;
492 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
495 dp->i_count = dp->i_offset - prevoff;
497 dp->i_offset = slotoffset;
498 dp->i_count = slotsize;
499 if (enduseful < slotoffset + slotsize)
500 enduseful = slotoffset + slotsize;
502 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
504 * We return with the directory locked, so that
505 * the parameters we set up above will still be
506 * valid if we actually decide to do a direnter().
507 * We return ni_vp == NULL to indicate that the entry
508 * does not currently exist; we leave a pointer to
509 * the (locked) directory inode in ndp->ni_dvp.
510 * The pathname buffer is saved so that the name
511 * can be obtained later.
513 * NB - if the directory is unlocked, then this
514 * information cannot be used.
516 cnp->cn_flags |= SAVENAME;
517 return (EJUSTRETURN);
520 * Insert name into cache (as non-existent) if appropriate.
522 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
523 cache_enter(vdp, NULL, cnp);
527 if (numdirpasses == 2)
528 nchstats.ncs_pass2++;
530 * Check that directory length properly reflects presence
533 if (i_offset + DIRSIZ(OFSFMT(vdp), ep) > dp->i_size) {
534 ufs_dirbad(dp, i_offset, "i_size too small");
535 dp->i_size = i_offset + DIRSIZ(OFSFMT(vdp), ep);
536 DIP_SET(dp, i_size, dp->i_size);
537 dp->i_flag |= IN_CHANGE | IN_UPDATE;
542 * Found component in pathname.
543 * If the final component of path name, save information
544 * in the cache as to where the entry was found.
546 if ((flags & ISLASTCN) && nameiop == LOOKUP)
547 dp->i_diroff = i_offset &~ (DIRBLKSIZ - 1);
549 if (dd_ino != NULL) {
555 * If deleting, and at end of pathname, return
556 * parameters which can be used to remove file.
558 if (nameiop == DELETE && (flags & ISLASTCN)) {
559 if (flags & LOCKPARENT)
560 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
561 if ((error = VFS_VGET(vdp->v_mount, ino,
562 LK_EXCLUSIVE, &tdp)) != 0)
565 error = ufs_delete_denied(vdp, tdp, cred, cnp->cn_thread);
573 * Return pointer to current entry in dp->i_offset,
574 * and distance past previous entry (if there
575 * is a previous entry in this block) in dp->i_count.
576 * Save directory inode pointer in ndp->ni_dvp for dirremove().
578 * Technically we shouldn't be setting these in the
579 * WANTPARENT case (first lookup in rename()), but any
580 * lookups that will result in directory changes will
583 dp->i_offset = i_offset;
584 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
587 dp->i_count = dp->i_offset - prevoff;
588 if (dp->i_number == ino) {
600 * If rewriting (RENAME), return the inode and the
601 * information required to rewrite the present directory
602 * Must get inode of directory entry to verify it's a
603 * regular file, or empty directory.
605 if (nameiop == RENAME && (flags & ISLASTCN)) {
606 if (flags & WILLBEDIR)
607 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread);
609 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread);
613 * Careful about locking second inode.
614 * This can only occur if the target is ".".
616 dp->i_offset = i_offset;
617 if (dp->i_number == ino)
619 if ((error = VFS_VGET(vdp->v_mount, ino,
620 LK_EXCLUSIVE, &tdp)) != 0)
623 error = ufs_delete_denied(vdp, tdp, cred, cnp->cn_thread);
629 #ifdef SunOS_doesnt_do_that
631 * The only purpose of this check is to return the correct
632 * error. Assume that we want to rename directory "a"
633 * to a file "b", and that we have no ACL_WRITE_DATA on
634 * a containing directory, but we _do_ have ACL_APPEND_DATA.
635 * In that case, the VOP_ACCESS check above will return 0,
636 * and the operation will fail with ENOTDIR instead
639 if (tdp->v_type == VDIR)
640 error = VOP_ACCESSX(vdp, VWRITE | VAPPEND, cred, cnp->cn_thread);
642 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread);
650 cnp->cn_flags |= SAVENAME;
655 * Step through the translation in the name. We do not `vput' the
656 * directory because we may need it again if a symbolic link
657 * is relative to the current directory. Instead we save it
658 * unlocked as "pdp". We must get the target inode before unlocking
659 * the directory to insure that the inode will not be removed
660 * before we get it. We prevent deadlock by always fetching
661 * inodes from the root, moving down the directory tree. Thus
662 * when following backward pointers ".." we must unlock the
663 * parent directory before getting the requested directory.
664 * There is a potential race condition here if both the current
665 * and parent directories are removed before the VFS_VGET for the
666 * inode associated with ".." returns. We hope that this occurs
667 * infrequently since we cannot avoid this race condition without
668 * implementing a sophisticated deadlock detection algorithm.
669 * Note also that this simple deadlock detection scheme will not
670 * work if the filesystem has any hard links other than ".."
671 * that point backwards in the directory structure.
674 if (flags & ISDOTDOT) {
675 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
680 * Recheck that ".." entry in the vdp directory points
681 * to the inode we looked up before vdp lock was
684 error = ufs_lookup_(pdp, NULL, cnp, &ino1);
695 } else if (dp->i_number == ino) {
696 VREF(vdp); /* we want ourself, ie "." */
698 * When we lookup "." we still can be asked to lock it
701 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
702 if (ltype != VOP_ISLOCKED(vdp)) {
703 if (ltype == LK_EXCLUSIVE)
704 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
705 else /* if (ltype == LK_SHARED) */
706 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
708 * Relock for the "." case may left us with
711 if (vdp->v_iflag & VI_DOOMED) {
718 error = VFS_VGET(pdp->v_mount, ino, cnp->cn_lkflags, &tdp);
725 * Insert name into cache if appropriate.
727 if (cnp->cn_flags & MAKEENTRY)
728 cache_enter(vdp, *vpp, cnp);
733 ufs_dirbad(ip, offset, how)
740 mp = ITOV(ip)->v_mount;
741 if ((mp->mnt_flag & MNT_RDONLY) == 0)
742 panic("ufs_dirbad: %s: bad dir ino %lu at offset %ld: %s",
743 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
745 (void)printf("%s: bad dir ino %lu at offset %ld: %s\n",
746 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
750 * Do consistency checking on a directory entry:
751 * record length must be multiple of 4
752 * entry must fit in rest of its DIRBLKSIZ block
753 * record must be large enough to contain entry
754 * name is not longer than MAXNAMLEN
755 * name must be as long as advertised, and null terminated
758 ufs_dirbadentry(dp, ep, entryoffsetinblock)
761 int entryoffsetinblock;
765 # if (BYTE_ORDER == LITTLE_ENDIAN)
769 namlen = ep->d_namlen;
771 namlen = ep->d_namlen;
773 if ((ep->d_reclen & 0x3) != 0 ||
774 ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
775 ep->d_reclen < DIRSIZ(OFSFMT(dp), ep) || namlen > MAXNAMLEN) {
777 printf("First bad\n");
782 for (i = 0; i < namlen; i++)
783 if (ep->d_name[i] == '\0') {
785 printf("Second bad\n");
796 * Construct a new directory entry after a call to namei, using the
797 * parameters that it left in the componentname argument cnp. The
798 * argument ip is the inode to which the new directory entry will refer.
801 ufs_makedirentry(ip, cnp, newdirp)
803 struct componentname *cnp;
804 struct direct *newdirp;
808 if ((cnp->cn_flags & SAVENAME) == 0)
809 panic("ufs_makedirentry: missing name");
811 newdirp->d_ino = ip->i_number;
812 newdirp->d_namlen = cnp->cn_namelen;
813 bcopy(cnp->cn_nameptr, newdirp->d_name, (unsigned)cnp->cn_namelen + 1);
814 if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
815 newdirp->d_type = IFTODT(ip->i_mode);
818 # if (BYTE_ORDER == LITTLE_ENDIAN)
819 { u_char tmp = newdirp->d_namlen;
820 newdirp->d_namlen = newdirp->d_type;
821 newdirp->d_type = tmp; }
827 * Write a directory entry after a call to namei, using the parameters
828 * that it left in nameidata. The argument dirp is the new directory
829 * entry contents. Dvp is a pointer to the directory to be written,
830 * which was left locked by namei. Remaining parameters (dp->i_offset,
831 * dp->i_count) indicate how the space for the new entry is to be obtained.
832 * Non-null bp indicates that a directory is being created (for the
833 * soft dependency code).
836 ufs_direnter(dvp, tvp, dirp, cnp, newdirbp)
840 struct componentname *cnp;
841 struct buf *newdirbp;
849 struct direct *ep, *nep;
850 int error, ret, blkoff, loc, spacefree, flags, namlen;
853 td = curthread; /* XXX */
857 newentrysize = DIRSIZ(OFSFMT(dvp), dirp);
859 if (dp->i_count == 0) {
861 * If dp->i_count is 0, then namei could find no
862 * space in the directory. Here, dp->i_offset will
863 * be on a directory block boundary and we will write the
864 * new entry into a fresh block.
866 if (dp->i_offset & (DIRBLKSIZ - 1))
867 panic("ufs_direnter: newblk");
869 if (!DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp))
872 if ((error = getinoquota(dp)) != 0) {
873 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
878 if ((error = UFS_BALLOC(dvp, (off_t)dp->i_offset, DIRBLKSIZ,
879 cr, flags, &bp)) != 0) {
880 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
884 dp->i_size = dp->i_offset + DIRBLKSIZ;
885 DIP_SET(dp, i_size, dp->i_size);
886 dp->i_flag |= IN_CHANGE | IN_UPDATE;
887 vnode_pager_setsize(dvp, (u_long)dp->i_size);
888 dirp->d_reclen = DIRBLKSIZ;
889 blkoff = dp->i_offset &
890 (VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_iosize - 1);
891 bcopy((caddr_t)dirp, (caddr_t)bp->b_data + blkoff,newentrysize);
893 if (dp->i_dirhash != NULL) {
894 ufsdirhash_newblk(dp, dp->i_offset);
895 ufsdirhash_add(dp, dirp, dp->i_offset);
896 ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff,
900 if (DOINGSOFTDEP(dvp)) {
902 * Ensure that the entire newly allocated block is a
903 * valid directory so that future growth within the
904 * block does not have to ensure that the block is
905 * written before the inode.
908 while (blkoff < bp->b_bcount) {
910 (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
913 if (softdep_setup_directory_add(bp, dp, dp->i_offset,
914 dirp->d_ino, newdirbp, 1) == 0) {
916 return (UFS_UPDATE(dvp, 0));
918 /* We have just allocated a directory block in an
919 * indirect block. Rather than tracking when it gets
920 * claimed by the inode, we simply do a VOP_FSYNC
921 * now to ensure that it is there (in case the user
922 * does a future fsync). Note that we have to unlock
923 * the inode for the entry that we just entered, as
924 * the VOP_FSYNC may need to lock other inodes which
925 * can lead to deadlock if we also hold a lock on
926 * the newly entered node.
928 if ((error = bwrite(bp)))
932 error = VOP_FSYNC(dvp, MNT_WAIT, td);
934 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
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 (dp->i_offset + dp->i_count > dp->i_size) {
965 dp->i_size = dp->i_offset + dp->i_count;
966 DIP_SET(dp, i_size, dp->i_size);
969 * Get the block containing the space for the new directory entry.
971 error = UFS_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp);
973 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
978 * Find space for the new entry. In the simple case, the entry at
979 * offset base will have the space. If it does not, then namei
980 * arranged that compacting the region dp->i_offset to
981 * dp->i_offset + dp->i_count would yield the space.
983 ep = (struct direct *)dirbuf;
984 dsize = ep->d_ino ? DIRSIZ(OFSFMT(dvp), ep) : 0;
985 spacefree = ep->d_reclen - dsize;
986 for (loc = ep->d_reclen; loc < dp->i_count; ) {
987 nep = (struct direct *)(dirbuf + loc);
989 /* Trim the existing slot (NB: dsize may be zero). */
990 ep->d_reclen = dsize;
991 ep = (struct direct *)((char *)ep + dsize);
993 /* Read nep->d_reclen now as the bcopy() may clobber it. */
994 loc += nep->d_reclen;
995 if (nep->d_ino == 0) {
997 * A mid-block unused entry. Such entries are
998 * never created by the kernel, but fsck_ffs
999 * can create them (and it doesn't fix them).
1001 * Add up the free space, and initialise the
1002 * relocated entry since we don't bcopy it.
1004 spacefree += nep->d_reclen;
1009 dsize = DIRSIZ(OFSFMT(dvp), nep);
1010 spacefree += nep->d_reclen - dsize;
1012 if (dp->i_dirhash != NULL)
1013 ufsdirhash_move(dp, nep,
1014 dp->i_offset + ((char *)nep - dirbuf),
1015 dp->i_offset + ((char *)ep - dirbuf));
1017 if (DOINGSOFTDEP(dvp))
1018 softdep_change_directoryentry_offset(dp, dirbuf,
1019 (caddr_t)nep, (caddr_t)ep, dsize);
1021 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1024 * Here, `ep' points to a directory entry containing `dsize' in-use
1025 * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
1026 * then the entry is completely unused (dsize == 0). The value
1027 * of ep->d_reclen is always indeterminate.
1029 * Update the pointer fields in the previous entry (if any),
1030 * copy in the new entry, and write out the block.
1032 # if (BYTE_ORDER == LITTLE_ENDIAN)
1034 namlen = ep->d_type;
1036 namlen = ep->d_namlen;
1038 namlen = ep->d_namlen;
1040 if (ep->d_ino == 0 ||
1041 (ep->d_ino == WINO && namlen == dirp->d_namlen &&
1042 bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
1043 if (spacefree + dsize < newentrysize)
1044 panic("ufs_direnter: compact1");
1045 dirp->d_reclen = spacefree + dsize;
1047 if (spacefree < newentrysize)
1048 panic("ufs_direnter: compact2");
1049 dirp->d_reclen = spacefree;
1050 ep->d_reclen = dsize;
1051 ep = (struct direct *)((char *)ep + dsize);
1054 if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
1055 dirp->d_reclen == spacefree))
1056 ufsdirhash_add(dp, dirp, dp->i_offset + ((char *)ep - dirbuf));
1058 bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize);
1060 if (dp->i_dirhash != NULL)
1061 ufsdirhash_checkblock(dp, dirbuf -
1062 (dp->i_offset & (DIRBLKSIZ - 1)),
1063 dp->i_offset & ~(DIRBLKSIZ - 1));
1066 if (DOINGSOFTDEP(dvp)) {
1067 (void) softdep_setup_directory_add(bp, dp,
1068 dp->i_offset + (caddr_t)ep - dirbuf,
1069 dirp->d_ino, newdirbp, 0);
1072 if (DOINGASYNC(dvp)) {
1079 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1081 * If all went well, and the directory can be shortened, proceed
1082 * with the truncation. Note that we have to unlock the inode for
1083 * the entry that we just entered, as the truncation may need to
1084 * lock other inodes which can lead to deadlock if we also hold a
1085 * lock on the newly entered node.
1087 if (error == 0 && dp->i_endoff && dp->i_endoff < dp->i_size) {
1091 if (dp->i_dirhash != NULL)
1092 ufsdirhash_dirtrunc(dp, dp->i_endoff);
1094 (void) UFS_TRUNCATE(dvp, (off_t)dp->i_endoff,
1095 IO_NORMAL | IO_SYNC, cr, td);
1097 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
1103 * Remove a directory entry after a call to namei, using
1104 * the parameters which it left in nameidata. The entry
1105 * dp->i_offset contains the offset into the directory of the
1106 * entry to be eliminated. The dp->i_count field contains the
1107 * size of the previous record in the directory. If this
1108 * is 0, the first entry is being deleted, so we need only
1109 * zero the inode number to mark the entry as free. If the
1110 * entry is not the first in the directory, we must reclaim
1111 * the space of the now empty record by adding the record size
1112 * to the size of the previous entry.
1115 ufs_dirremove(dvp, ip, flags, isrmdir)
1122 struct direct *ep, *rep;
1128 if (flags & DOWHITEOUT) {
1130 * Whiteout entry: set d_ino to WINO.
1133 UFS_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) != 0)
1136 ep->d_type = DT_WHT;
1140 if ((error = UFS_BLKATOFF(dvp,
1141 (off_t)(dp->i_offset - dp->i_count), (char **)&ep, &bp)) != 0)
1144 /* Set 'rep' to the entry being removed. */
1145 if (dp->i_count == 0)
1148 rep = (struct direct *)((char *)ep + ep->d_reclen);
1151 * Remove the dirhash entry. This is complicated by the fact
1152 * that `ep' is the previous entry when dp->i_count != 0.
1154 if (dp->i_dirhash != NULL)
1155 ufsdirhash_remove(dp, rep, dp->i_offset);
1157 if (dp->i_count == 0) {
1159 * First entry in block: set d_ino to zero.
1164 * Collapse new free space into previous entry.
1166 ep->d_reclen += rep->d_reclen;
1169 if (dp->i_dirhash != NULL)
1170 ufsdirhash_checkblock(dp, (char *)ep -
1171 ((dp->i_offset - dp->i_count) & (DIRBLKSIZ - 1)),
1172 dp->i_offset & ~(DIRBLKSIZ - 1));
1175 if (DOINGSOFTDEP(dvp)) {
1178 softdep_change_linkcnt(ip);
1179 softdep_setup_remove(bp, dp, ip, isrmdir);
1181 if (softdep_slowdown(dvp)) {
1191 DIP_SET(ip, i_nlink, ip->i_nlink);
1192 ip->i_flag |= IN_CHANGE;
1194 if (flags & DOWHITEOUT)
1196 else if (DOINGASYNC(dvp) && dp->i_count != 0) {
1202 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1204 * If the last named reference to a snapshot goes away,
1205 * drop its snapshot reference so that it will be reclaimed
1206 * when last open reference goes away.
1208 #if defined(FFS) || defined(IFS)
1209 if (ip != 0 && (ip->i_flags & SF_SNAPSHOT) != 0 && ip->i_effnlink == 0)
1216 * Rewrite an existing directory entry to point at the inode
1217 * supplied. The parameters describing the directory entry are
1218 * set up by a call to namei.
1221 ufs_dirrewrite(dp, oip, newinum, newtype, isrmdir)
1222 struct inode *dp, *oip;
1229 struct vnode *vdp = ITOV(dp);
1232 error = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp);
1235 ep->d_ino = newinum;
1237 ep->d_type = newtype;
1239 if (DOINGSOFTDEP(vdp)) {
1240 softdep_change_linkcnt(oip);
1241 softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir);
1245 DIP_SET(oip, i_nlink, oip->i_nlink);
1246 oip->i_flag |= IN_CHANGE;
1247 if (DOINGASYNC(vdp)) {
1254 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1256 * If the last named reference to a snapshot goes away,
1257 * drop its snapshot reference so that it will be reclaimed
1258 * when last open reference goes away.
1260 #if defined(FFS) || defined(IFS)
1261 if ((oip->i_flags & SF_SNAPSHOT) != 0 && oip->i_effnlink == 0)
1268 * Check if a directory is empty or not.
1269 * Inode supplied must be locked.
1271 * Using a struct dirtemplate here is not precisely
1272 * what we want, but better than using a struct direct.
1274 * NB: does not handle corrupted directories.
1277 ufs_dirempty(ip, parentino, cred)
1283 struct dirtemplate dbuf;
1284 struct direct *dp = (struct direct *)&dbuf;
1285 int error, count, namlen;
1286 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1288 for (off = 0; off < ip->i_size; off += dp->d_reclen) {
1289 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1290 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1291 NOCRED, &count, (struct thread *)0);
1293 * Since we read MINDIRSIZ, residual must
1294 * be 0 unless we're at end of file.
1296 if (error || count != 0)
1298 /* avoid infinite loops */
1299 if (dp->d_reclen == 0)
1301 /* skip empty entries */
1302 if (dp->d_ino == 0 || dp->d_ino == WINO)
1304 /* accept only "." and ".." */
1305 # if (BYTE_ORDER == LITTLE_ENDIAN)
1306 if (OFSFMT(ITOV(ip)))
1307 namlen = dp->d_type;
1309 namlen = dp->d_namlen;
1311 namlen = dp->d_namlen;
1315 if (dp->d_name[0] != '.')
1318 * At this point namlen must be 1 or 2.
1319 * 1 implies ".", 2 implies ".." if second
1322 if (namlen == 1 && dp->d_ino == ip->i_number)
1324 if (dp->d_name[1] == '.' && dp->d_ino == parentino)
1332 ufs_dir_dd_ino(struct vnode *vp, struct ucred *cred, ino_t *dd_ino)
1334 struct dirtemplate dirbuf;
1337 if (vp->v_type != VDIR)
1339 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1340 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1341 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL, NULL);
1344 #if (BYTE_ORDER == LITTLE_ENDIAN)
1346 namlen = dirbuf.dotdot_type;
1348 namlen = dirbuf.dotdot_namlen;
1350 namlen = dirbuf.dotdot_namlen;
1352 if (namlen != 2 || dirbuf.dotdot_name[0] != '.' ||
1353 dirbuf.dotdot_name[1] != '.')
1355 *dd_ino = dirbuf.dotdot_ino;
1360 * Check if source directory is in the path of the target directory.
1361 * Target is supplied locked, source is unlocked.
1362 * The target is always vput before returning.
1365 ufs_checkpath(ino_t source_ino, struct inode *target, struct ucred *cred)
1367 struct vnode *vp, *vp1;
1372 if (target->i_number == source_ino) {
1377 if (target->i_number == ROOTINO)
1381 error = ufs_dir_dd_ino(vp, cred, &dd_ino);
1384 if (dd_ino == source_ino) {
1388 if (dd_ino == ROOTINO)
1390 error = vn_vget_ino(vp, dd_ino, LK_EXCLUSIVE, &vp1);
1393 /* Recheck that ".." still points to vp1 after relock of vp */
1394 error = ufs_dir_dd_ino(vp, cred, &dd_ino);
1399 /* Redo the check of ".." if directory was reparented */
1400 if (dd_ino != VTOI(vp1)->i_number) {
1409 if (error == ENOTDIR)
1410 printf("checkpath: .. not a directory\n");