2 * modified for Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * SPDX-License-Identifier: BSD-3-Clause
10 * Copyright (c) 1989, 1993
11 * The Regents of the University of California. All rights reserved.
12 * (c) UNIX System Laboratories, Inc.
13 * All or some portions of this file are derived from material licensed
14 * to the University of California by American Telephone and Telegraph
15 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16 * the permission of UNIX System Laboratories, Inc.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * @(#)ufs_lookup.c 8.6 (Berkeley) 4/1/94
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
51 #include <sys/endian.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <sys/sysctl.h>
58 #include <ufs/ufs/dir.h>
60 #include <fs/ext2fs/inode.h>
61 #include <fs/ext2fs/ext2_mount.h>
62 #include <fs/ext2fs/ext2fs.h>
63 #include <fs/ext2fs/ext2_dinode.h>
64 #include <fs/ext2fs/ext2_dir.h>
65 #include <fs/ext2fs/ext2_extern.h>
68 static int dirchk = 1;
70 static int dirchk = 0;
73 static SYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD, 0, "EXT2FS filesystem");
74 SYSCTL_INT(_vfs_e2fs, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
77 DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
78 while it is the native blocksize in ext2fs - thus, a #define
79 is no longer appropriate
83 static u_char ext2_ft_to_dt[] = {
84 DT_UNKNOWN, /* EXT2_FT_UNKNOWN */
85 DT_REG, /* EXT2_FT_REG_FILE */
86 DT_DIR, /* EXT2_FT_DIR */
87 DT_CHR, /* EXT2_FT_CHRDEV */
88 DT_BLK, /* EXT2_FT_BLKDEV */
89 DT_FIFO, /* EXT2_FT_FIFO */
90 DT_SOCK, /* EXT2_FT_SOCK */
91 DT_LNK, /* EXT2_FT_SYMLINK */
94 ((ft) < nitems(ext2_ft_to_dt) ? ext2_ft_to_dt[(ft)] : DT_UNKNOWN)
96 static u_char dt_to_ext2_ft[] = {
97 EXT2_FT_UNKNOWN, /* DT_UNKNOWN */
98 EXT2_FT_FIFO, /* DT_FIFO */
99 EXT2_FT_CHRDEV, /* DT_CHR */
100 EXT2_FT_UNKNOWN, /* unused */
101 EXT2_FT_DIR, /* DT_DIR */
102 EXT2_FT_UNKNOWN, /* unused */
103 EXT2_FT_BLKDEV, /* DT_BLK */
104 EXT2_FT_UNKNOWN, /* unused */
105 EXT2_FT_REG_FILE, /* DT_REG */
106 EXT2_FT_UNKNOWN, /* unused */
107 EXT2_FT_SYMLINK, /* DT_LNK */
108 EXT2_FT_UNKNOWN, /* unused */
109 EXT2_FT_SOCK, /* DT_SOCK */
110 EXT2_FT_UNKNOWN, /* unused */
111 EXT2_FT_UNKNOWN, /* DT_WHT */
114 ((dt) < nitems(dt_to_ext2_ft) ? dt_to_ext2_ft[(dt)] : EXT2_FT_UNKNOWN)
116 static int ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
117 int entryoffsetinblock);
118 static int ext2_is_dot_entry(struct componentname *cnp);
119 static int ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp,
120 struct componentname *cnp, ino_t *dd_ino);
123 ext2_is_dot_entry(struct componentname *cnp)
125 if (cnp->cn_namelen <= 2 && cnp->cn_nameptr[0] == '.' &&
126 (cnp->cn_nameptr[1] == '.' || cnp->cn_nameptr[1] == '\0'))
132 * Vnode op for reading directories.
135 ext2_readdir(struct vop_readdir_args *ap)
137 struct vnode *vp = ap->a_vp;
138 struct uio *uio = ap->a_uio;
141 struct ext2fs_direct_2 *dp, *edp;
144 off_t offset, startoffset;
145 size_t readcnt, skipcnt;
148 int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->e2fs_bsize;
151 if (uio->uio_offset < 0)
154 if (ap->a_ncookies != NULL) {
155 ncookies = uio->uio_resid;
156 if (uio->uio_offset >= ip->i_size)
158 else if (ip->i_size - uio->uio_offset < ncookies)
159 ncookies = ip->i_size - uio->uio_offset;
160 ncookies = ncookies / (offsetof(struct ext2fs_direct_2,
161 e2d_namlen) + 4) + 1;
162 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
163 *ap->a_ncookies = ncookies;
164 *ap->a_cookies = cookies;
169 offset = startoffset = uio->uio_offset;
170 startresid = uio->uio_resid;
172 while (error == 0 && uio->uio_resid > 0 &&
173 uio->uio_offset < ip->i_size) {
174 error = ext2_blkatoff(vp, uio->uio_offset, NULL, &bp);
177 if (bp->b_offset + bp->b_bcount > ip->i_size)
178 readcnt = ip->i_size - bp->b_offset;
180 readcnt = bp->b_bcount;
181 skipcnt = (size_t)(uio->uio_offset - bp->b_offset) &
182 ~(size_t)(DIRBLKSIZ - 1);
183 offset = bp->b_offset + skipcnt;
184 dp = (struct ext2fs_direct_2 *)&bp->b_data[skipcnt];
185 edp = (struct ext2fs_direct_2 *)&bp->b_data[readcnt];
186 while (error == 0 && uio->uio_resid > 0 && dp < edp) {
187 if (dp->e2d_reclen <= offsetof(struct ext2fs_direct_2,
188 e2d_namlen) || (caddr_t)dp + dp->e2d_reclen >
194 * "New" ext2fs directory entries differ in 3 ways
195 * from ufs on-disk ones:
196 * - the name is not necessarily NUL-terminated.
197 * - the file type field always exists and always
198 * follows the name length field.
199 * - the file type is encoded in a different way.
201 * "Old" ext2fs directory entries need no special
202 * conversions, since they are binary compatible
203 * with "new" entries having a file type of 0 (i.e.,
204 * EXT2_FT_UNKNOWN). Splitting the old name length
205 * field didn't make a mess like it did in ufs,
206 * because ext2fs uses a machine-independent disk
209 dstdp.d_namlen = dp->e2d_namlen;
210 dstdp.d_type = FTTODT(dp->e2d_type);
211 if (offsetof(struct ext2fs_direct_2, e2d_namlen) +
212 dstdp.d_namlen > dp->e2d_reclen) {
216 if (offset < startoffset || dp->e2d_ino == 0)
218 dstdp.d_fileno = dp->e2d_ino;
219 dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
220 bcopy(dp->e2d_name, dstdp.d_name, dstdp.d_namlen);
221 dstdp.d_name[dstdp.d_namlen] = '\0';
222 if (dstdp.d_reclen > uio->uio_resid) {
223 if (uio->uio_resid == startresid)
230 error = uiomove((caddr_t)&dstdp, dstdp.d_reclen, uio);
233 if (cookies != NULL) {
234 KASSERT(ncookies > 0,
235 ("ext2_readdir: cookies buffer too small"));
236 *cookies = offset + dp->e2d_reclen;
241 offset += dp->e2d_reclen;
242 dp = (struct ext2fs_direct_2 *)((caddr_t)dp +
246 uio->uio_offset = offset;
248 /* We need to correct uio_offset. */
249 uio->uio_offset = offset;
250 if (error == EJUSTRETURN)
252 if (ap->a_ncookies != NULL) {
254 ap->a_ncookies -= ncookies;
256 free(*ap->a_cookies, M_TEMP);
258 *ap->a_cookies = NULL;
261 if (error == 0 && ap->a_eofflag)
262 *ap->a_eofflag = ip->i_size <= uio->uio_offset;
267 * Convert a component of a pathname into a pointer to a locked inode.
268 * This is a very central and rather complicated routine.
269 * If the file system is not maintained in a strict tree hierarchy,
270 * this can result in a deadlock situation (see comments in code below).
272 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
273 * on whether the name is to be looked up, created, renamed, or deleted.
274 * When CREATE, RENAME, or DELETE is specified, information usable in
275 * creating, renaming, or deleting a directory entry may be calculated.
276 * If flag has LOCKPARENT or'ed into it and the target of the pathname
277 * exists, lookup returns both the target and its parent directory locked.
278 * When creating or renaming and LOCKPARENT is specified, the target may
279 * not be ".". When deleting and LOCKPARENT is specified, the target may
280 * be "."., but the caller must check to ensure it does an vrele and vput
281 * instead of two vputs.
283 * Overall outline of ext2_lookup:
285 * search for name in directory, to found or notfound
287 * if creating, return locked directory, leaving info on available slots
290 * if at end of path and deleting, return information to allow delete
291 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
292 * inode and return info to allow rewrite
293 * if not at end, add name to cache; if at end and neither creating
294 * nor deleting, add name to cache
297 ext2_lookup(struct vop_cachedlookup_args *ap)
300 return (ext2_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
304 ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
307 struct inode *dp; /* inode for directory being searched */
308 struct buf *bp; /* a buffer of directory entries */
309 struct ext2fs_direct_2 *ep; /* the current directory entry */
310 int entryoffsetinblock; /* offset of ep in bp's buffer */
311 struct ext2fs_searchslot ss;
312 doff_t i_diroff; /* cached i_diroff value */
313 doff_t i_offset; /* cached i_offset value */
314 int numdirpasses; /* strategy for directory search */
315 doff_t endsearch; /* offset to end directory search */
316 doff_t prevoff; /* prev entry dp->i_offset */
317 struct vnode *pdp; /* saved dp during symlink work */
318 struct vnode *tdp; /* returned by VFS_VGET */
319 doff_t enduseful; /* pointer past last used dir slot */
320 u_long bmask; /* block offset mask */
322 struct ucred *cred = cnp->cn_cred;
323 int flags = cnp->cn_flags;
324 int nameiop = cnp->cn_nameiop;
329 int DIRBLKSIZ = VTOI(vdp)->i_e2fs->e2fs_bsize;
335 bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
341 * We now have a segment name to search for, and a directory to search.
343 * Suppress search for slots unless creating
344 * file and at end of pathname, in which case
345 * we watch for a place to put the new file in
346 * case it doesn't already exist.
348 i_diroff = dp->i_diroff;
349 ss.slotstatus = FOUND;
350 ss.slotfreespace = ss.slotsize = ss.slotneeded = 0;
351 if ((nameiop == CREATE || nameiop == RENAME) &&
352 (flags & ISLASTCN)) {
353 ss.slotstatus = NONE;
354 ss.slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
356 * was ss.slotneeded = (sizeof(struct direct) - MAXNAMLEN +
357 * cnp->cn_namelen + 3) &~ 3;
361 * Try to lookup dir entry using htree directory index.
363 * If we got an error or we want to find '.' or '..' entry,
364 * we will fall back to linear search.
366 if (!ext2_is_dot_entry(cnp) && ext2_htree_has_idx(dp)) {
368 entryoffsetinblock = 0;
369 switch (ext2_htree_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
370 &bp, &entryoffsetinblock, &i_offset, &prevoff,
373 ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
377 i_offset = roundup2(dp->i_size, DIRBLKSIZ);
381 * Something failed; just fallback to do a linear
389 * If there is cached information on a previous search of
390 * this directory, pick up where we last left off.
391 * We cache only lookups as these are the most common
392 * and have the greatest payoff. Caching CREATE has little
393 * benefit as it usually must search the entire directory
394 * to determine that the entry does not exist. Caching the
395 * location of the last DELETE or RENAME has not reduced
396 * profiling time and hence has been removed in the interest
399 if (nameiop != LOOKUP || i_diroff == 0 ||
400 i_diroff > dp->i_size) {
401 entryoffsetinblock = 0;
406 if ((entryoffsetinblock = i_offset & bmask) &&
407 (error = ext2_blkatoff(vdp, (off_t)i_offset, NULL,
411 nchstats.ncs_2passes++;
414 endsearch = roundup2(dp->i_size, DIRBLKSIZ);
418 while (i_offset < endsearch) {
420 * If necessary, get the next directory block.
424 error = ext2_blkatoff(vdp, (off_t)i_offset, NULL, &bp);
427 entryoffsetinblock = 0;
429 * If still looking for a slot, and at a DIRBLKSIZE
430 * boundary, have to start looking for free space again.
432 if (ss.slotstatus == NONE &&
433 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
435 ss.slotfreespace = 0;
437 error = ext2_search_dirblock(dp, bp->b_data, &entry_found,
438 cnp->cn_nameptr, cnp->cn_namelen,
439 &entryoffsetinblock, &i_offset, &prevoff,
446 ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
447 (entryoffsetinblock & bmask));
455 * If we started in the middle of the directory and failed
456 * to find our target, we must check the beginning as well.
458 if (numdirpasses == 2) {
461 endsearch = i_diroff;
467 * If creating, and at end of pathname and current
468 * directory has not been removed, then can consider
469 * allowing file to be created.
471 if ((nameiop == CREATE || nameiop == RENAME) &&
472 (flags & ISLASTCN) && dp->i_nlink != 0) {
474 * Access for write is interpreted as allowing
475 * creation of files in the directory.
477 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
480 * Return an indication of where the new directory
481 * entry should be put. If we didn't find a slot,
482 * then set dp->i_count to 0 indicating
483 * that the new slot belongs at the end of the
484 * directory. If we found a slot, then the new entry
485 * can be put in the range from dp->i_offset to
486 * dp->i_offset + dp->i_count.
488 if (ss.slotstatus == NONE) {
489 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
491 enduseful = dp->i_offset;
493 dp->i_offset = ss.slotoffset;
494 dp->i_count = ss.slotsize;
495 if (enduseful < ss.slotoffset + ss.slotsize)
496 enduseful = ss.slotoffset + ss.slotsize;
498 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
500 * We return with the directory locked, so that
501 * the parameters we set up above will still be
502 * valid if we actually decide to do a direnter().
503 * We return ni_vp == NULL to indicate that the entry
504 * does not currently exist; we leave a pointer to
505 * the (locked) directory inode in ndp->ni_dvp.
506 * The pathname buffer is saved so that the name
507 * can be obtained later.
509 * NB - if the directory is unlocked, then this
510 * information cannot be used.
512 cnp->cn_flags |= SAVENAME;
513 return (EJUSTRETURN);
516 * Insert name into cache (as non-existent) if appropriate.
518 if ((cnp->cn_flags & MAKEENTRY) != 0)
519 cache_enter(vdp, NULL, cnp);
525 if (numdirpasses == 2)
526 nchstats.ncs_pass2++;
528 * Check that directory length properly reflects presence
531 if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen)
533 ext2_dirbad(dp, i_offset, "i_size too small");
534 dp->i_size = entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen);
535 dp->i_flag |= IN_CHANGE | IN_UPDATE;
540 * Found component in pathname.
541 * If the final component of path name, save information
542 * in the cache as to where the entry was found.
544 if ((flags & ISLASTCN) && nameiop == LOOKUP)
545 dp->i_diroff = rounddown2(i_offset, DIRBLKSIZ);
547 * If deleting, and at end of pathname, return
548 * parameters which can be used to remove file.
550 if (nameiop == DELETE && (flags & ISLASTCN)) {
551 if (flags & LOCKPARENT)
552 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
554 * Write access to directory required to delete files.
556 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
559 * Return pointer to current entry in dp->i_offset,
560 * and distance past previous entry (if there
561 * is a previous entry in this block) in dp->i_count.
562 * Save directory inode pointer in ndp->ni_dvp for dirremove().
564 * Technically we shouldn't be setting these in the
565 * WANTPARENT case (first lookup in rename()), but any
566 * lookups that will result in directory changes will
569 dp->i_offset = i_offset;
570 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
573 dp->i_count = dp->i_offset - prevoff;
576 if (dp->i_number == ino) {
581 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
585 * If directory is "sticky", then user must own
586 * the directory, or the file in it, else she
587 * may not delete it (unless she's root). This
588 * implements append-only directories.
590 if ((dp->i_mode & ISVTX) &&
592 cred->cr_uid != dp->i_uid &&
593 VTOI(tdp)->i_uid != cred->cr_uid) {
602 * If rewriting (RENAME), return the inode and the
603 * information required to rewrite the present directory
604 * Must get inode of directory entry to verify it's a
605 * regular file, or empty directory.
607 if (nameiop == RENAME && (flags & ISLASTCN)) {
608 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
611 * Careful about locking second inode.
612 * This can only occur if the target is ".".
614 dp->i_offset = i_offset;
615 if (dp->i_number == ino)
619 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
623 cnp->cn_flags |= SAVENAME;
630 * Step through the translation in the name. We do not `vput' the
631 * directory because we may need it again if a symbolic link
632 * is relative to the current directory. Instead we save it
633 * unlocked as "pdp". We must get the target inode before unlocking
634 * the directory to insure that the inode will not be removed
635 * before we get it. We prevent deadlock by always fetching
636 * inodes from the root, moving down the directory tree. Thus
637 * when following backward pointers ".." we must unlock the
638 * parent directory before getting the requested directory.
639 * There is a potential race condition here if both the current
640 * and parent directories are removed before the VFS_VGET for the
641 * inode associated with ".." returns. We hope that this occurs
642 * infrequently since we cannot avoid this race condition without
643 * implementing a sophisticated deadlock detection algorithm.
644 * Note also that this simple deadlock detection scheme will not
645 * work if the file system has any hard links other than ".."
646 * that point backwards in the directory structure.
649 if (flags & ISDOTDOT) {
650 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
651 if (pdp->v_iflag & VI_DOOMED) {
659 * Recheck that ".." entry in the vdp directory points
660 * to the inode we looked up before vdp lock was
663 error = ext2_lookup_ino(pdp, NULL, cnp, &ino1);
673 } else if (dp->i_number == ino) {
674 VREF(vdp); /* we want ourself, ie "." */
676 * When we lookup "." we still can be asked to lock it
679 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
680 if (ltype != VOP_ISLOCKED(vdp)) {
681 if (ltype == LK_EXCLUSIVE)
682 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
683 else /* if (ltype == LK_SHARED) */
684 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
688 if ((error = VFS_VGET(vdp->v_mount, ino, cnp->cn_lkflags,
695 * Insert name into cache if appropriate.
697 if (cnp->cn_flags & MAKEENTRY)
698 cache_enter(vdp, *vpp, cnp);
703 ext2_search_dirblock(struct inode *ip, void *data, int *foundp,
704 const char *name, int namelen, int *entryoffsetinblockp,
705 doff_t *offp, doff_t *prevoffp, doff_t *endusefulp,
706 struct ext2fs_searchslot *ssp)
709 struct ext2fs_direct_2 *ep, *top;
710 uint32_t bsize = ip->i_e2fs->e2fs_bsize;
711 int offset = *entryoffsetinblockp;
716 ep = (struct ext2fs_direct_2 *)((char *)data + offset);
717 top = (struct ext2fs_direct_2 *)((char *)data +
718 bsize - EXT2_DIR_REC_LEN(0));
722 * Full validation checks are slow, so we only check
723 * enough to insure forward progress through the
724 * directory. Complete checks can be run by setting
725 * "vfs.e2fs.dirchk" to be true.
727 if (ep->e2d_reclen == 0 ||
728 (dirchk && ext2_dirbadentry(vdp, ep, offset))) {
731 ext2_dirbad(ip, *offp, "mangled entry");
732 i = bsize - (offset & (bsize - 1));
739 * If an appropriate sized slot has not yet been found,
740 * check to see if one is available. Also accumulate space
741 * in the current block so that we can determine if
742 * compaction is viable.
744 if (ssp->slotstatus != FOUND) {
745 int size = ep->e2d_reclen;
747 if (ep->e2d_ino != 0)
748 size -= EXT2_DIR_REC_LEN(ep->e2d_namlen);
750 if (size >= ssp->slotneeded) {
751 ssp->slotstatus = FOUND;
752 ssp->slotoffset = *offp;
753 ssp->slotsize = ep->e2d_reclen;
754 } else if (ssp->slotstatus == NONE) {
755 ssp->slotfreespace += size;
756 if (ssp->slotoffset == -1)
757 ssp->slotoffset = *offp;
758 if (ssp->slotfreespace >= ssp->slotneeded) {
759 ssp->slotstatus = COMPACT;
760 ssp->slotsize = *offp +
768 * Check for a name match.
771 namlen = ep->e2d_namlen;
772 if (namlen == namelen &&
773 !bcmp(name, ep->e2d_name, (unsigned)namlen)) {
775 * Save directory entry's inode number and
776 * reclen in ndp->ni_ufs area, and release
784 *offp += ep->e2d_reclen;
785 offset += ep->e2d_reclen;
786 *entryoffsetinblockp = offset;
790 * Get pointer to the next entry.
792 ep = (struct ext2fs_direct_2 *)((char *)data + offset);
799 ext2_dirbad(struct inode *ip, doff_t offset, char *how)
803 mp = ITOV(ip)->v_mount;
804 if ((mp->mnt_flag & MNT_RDONLY) == 0)
805 panic("ext2_dirbad: %s: bad dir ino %ju at offset %ld: %s\n",
806 mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number,
809 (void)printf("%s: bad dir ino %ju at offset %ld: %s\n",
810 mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number,
816 * Do consistency checking on a directory entry:
817 * record length must be multiple of 4
818 * entry must fit in rest of its DIRBLKSIZ block
819 * record must be large enough to contain entry
820 * name is not longer than MAXNAMLEN
821 * name must be as long as advertised, and null terminated
824 * changed so that it confirms to ext2_check_dir_entry
827 ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
828 int entryoffsetinblock)
830 int DIRBLKSIZ = VTOI(dp)->i_e2fs->e2fs_bsize;
832 char *error_msg = NULL;
834 if (de->e2d_reclen < EXT2_DIR_REC_LEN(1))
835 error_msg = "rec_len is smaller than minimal";
836 else if (de->e2d_reclen % 4 != 0)
837 error_msg = "rec_len % 4 != 0";
838 else if (de->e2d_reclen < EXT2_DIR_REC_LEN(de->e2d_namlen))
839 error_msg = "reclen is too small for name_len";
840 else if (entryoffsetinblock + de->e2d_reclen > DIRBLKSIZ)
841 error_msg = "directory entry across blocks";
843 if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count)
844 error_msg = "inode out of bounds";
847 if (error_msg != NULL) {
848 printf("bad directory entry: %s\n", error_msg);
849 printf("offset=%d, inode=%lu, rec_len=%u, name_len=%u\n",
850 entryoffsetinblock, (unsigned long)de->e2d_ino,
851 de->e2d_reclen, de->e2d_namlen);
853 return error_msg == NULL ? 0 : 1;
857 * Write a directory entry after a call to namei, using the parameters
858 * that it left in nameidata. The argument ip is the inode which the new
859 * directory entry will refer to. Dvp is a pointer to the directory to
860 * be written, which was left locked by namei. Remaining parameters
861 * (dp->i_offset, dp->i_count) indicate how the space for the new
862 * entry is to be obtained.
865 ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp)
868 struct ext2fs_direct_2 newdir;
871 int error, newentrysize;
872 int DIRBLKSIZ = ip->i_e2fs->e2fs_bsize;
876 if ((cnp->cn_flags & SAVENAME) == 0)
877 panic("ext2_direnter: missing name");
880 newdir.e2d_ino = ip->i_number;
881 newdir.e2d_namlen = cnp->cn_namelen;
882 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
883 EXT2F_INCOMPAT_FTYPE))
884 newdir.e2d_type = DTTOFT(IFTODT(ip->i_mode));
886 newdir.e2d_type = EXT2_FT_UNKNOWN;
887 bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1);
888 newentrysize = EXT2_DIR_REC_LEN(newdir.e2d_namlen);
890 if (ext2_htree_has_idx(dp)) {
891 error = ext2_htree_add_entry(dvp, &newdir, cnp);
893 dp->i_flag &= ~IN_E3INDEX;
894 dp->i_flag |= IN_CHANGE | IN_UPDATE;
899 if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) &&
900 !ext2_htree_has_idx(dp)) {
901 if ((dp->i_size / DIRBLKSIZ) == 1 &&
902 dp->i_offset == DIRBLKSIZ) {
904 * Making indexed directory when one block is not
905 * enough to save all entries.
907 return ext2_htree_create_index(dvp, cnp, &newdir);
911 if (dp->i_count == 0) {
913 * If dp->i_count is 0, then namei could find no
914 * space in the directory. Here, dp->i_offset will
915 * be on a directory block boundary and we will write the
916 * new entry into a fresh block.
918 if (dp->i_offset & (DIRBLKSIZ - 1))
919 panic("ext2_direnter: newblk");
920 auio.uio_offset = dp->i_offset;
921 newdir.e2d_reclen = DIRBLKSIZ;
922 auio.uio_resid = newentrysize;
923 aiov.iov_len = newentrysize;
924 aiov.iov_base = (caddr_t)&newdir;
925 auio.uio_iov = &aiov;
927 auio.uio_rw = UIO_WRITE;
928 auio.uio_segflg = UIO_SYSSPACE;
929 auio.uio_td = (struct thread *)0;
930 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
932 VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
933 /* XXX should grow with balloc() */
934 panic("ext2_direnter: frag size");
936 dp->i_size = roundup2(dp->i_size, DIRBLKSIZ);
937 dp->i_flag |= IN_CHANGE;
942 error = ext2_add_entry(dvp, &newdir);
943 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
944 error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
945 cnp->cn_cred, cnp->cn_thread);
950 * Insert an entry into the directory block.
951 * Compact the contents.
954 ext2_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry)
956 struct ext2fs_direct_2 *ep, *nep;
960 int error, loc, newentrysize, spacefree;
966 * If dp->i_count is non-zero, then namei found space
967 * for the new entry in the range dp->i_offset to
968 * dp->i_offset + dp->i_count in the directory.
969 * To use this space, we may have to compact the entries located
970 * there, by copying them together towards the beginning of the
971 * block, leaving the free space in one usable chunk at the end.
975 * Increase size of directory if entry eats into new space.
976 * This should never push the size past a new multiple of
979 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
981 if (dp->i_offset + dp->i_count > dp->i_size)
982 dp->i_size = dp->i_offset + dp->i_count;
984 * Get the block containing the space for the new directory entry.
986 if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf,
990 * Find space for the new entry. In the simple case, the entry at
991 * offset base will have the space. If it does not, then namei
992 * arranged that compacting the region dp->i_offset to
993 * dp->i_offset + dp->i_count would yield the
996 newentrysize = EXT2_DIR_REC_LEN(entry->e2d_namlen);
997 ep = (struct ext2fs_direct_2 *)dirbuf;
998 dsize = EXT2_DIR_REC_LEN(ep->e2d_namlen);
999 spacefree = ep->e2d_reclen - dsize;
1000 for (loc = ep->e2d_reclen; loc < dp->i_count; ) {
1001 nep = (struct ext2fs_direct_2 *)(dirbuf + loc);
1003 /* trim the existing slot */
1004 ep->e2d_reclen = dsize;
1005 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1007 /* overwrite; nothing there; header is ours */
1010 dsize = EXT2_DIR_REC_LEN(nep->e2d_namlen);
1011 spacefree += nep->e2d_reclen - dsize;
1012 loc += nep->e2d_reclen;
1013 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1016 * Update the pointer fields in the previous entry (if any),
1017 * copy in the new entry, and write out the block.
1019 if (ep->e2d_ino == 0) {
1020 if (spacefree + dsize < newentrysize)
1021 panic("ext2_direnter: compact1");
1022 entry->e2d_reclen = spacefree + dsize;
1024 if (spacefree < newentrysize)
1025 panic("ext2_direnter: compact2");
1026 entry->e2d_reclen = spacefree;
1027 ep->e2d_reclen = dsize;
1028 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1030 bcopy((caddr_t)entry, (caddr_t)ep, (u_int)newentrysize);
1031 if (DOINGASYNC(dvp)) {
1037 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1042 * Remove a directory entry after a call to namei, using
1043 * the parameters which it left in nameidata. The entry
1044 * dp->i_offset contains the offset into the directory of the
1045 * entry to be eliminated. The dp->i_count field contains the
1046 * size of the previous record in the directory. If this
1047 * is 0, the first entry is being deleted, so we need only
1048 * zero the inode number to mark the entry as free. If the
1049 * entry is not the first in the directory, we must reclaim
1050 * the space of the now empty record by adding the record size
1051 * to the size of the previous entry.
1054 ext2_dirremove(struct vnode *dvp, struct componentname *cnp)
1057 struct ext2fs_direct_2 *ep, *rep;
1062 if (dp->i_count == 0) {
1064 * First entry in block: set d_ino to zero.
1067 ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep,
1072 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1076 * Collapse new free space into previous entry.
1078 if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
1079 (char **)&ep, &bp)) != 0)
1082 /* Set 'rep' to the entry being removed. */
1083 if (dp->i_count == 0)
1086 rep = (struct ext2fs_direct_2 *)((char *)ep + ep->e2d_reclen);
1087 ep->e2d_reclen += rep->e2d_reclen;
1088 if (DOINGASYNC(dvp) && dp->i_count != 0)
1092 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1097 * Rewrite an existing directory entry to point at the inode
1098 * supplied. The parameters describing the directory entry are
1099 * set up by a call to namei.
1102 ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp)
1105 struct ext2fs_direct_2 *ep;
1106 struct vnode *vdp = ITOV(dp);
1109 if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep,
1112 ep->e2d_ino = ip->i_number;
1113 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1114 EXT2F_INCOMPAT_FTYPE))
1115 ep->e2d_type = DTTOFT(IFTODT(ip->i_mode));
1117 ep->e2d_type = EXT2_FT_UNKNOWN;
1119 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1124 * Check if a directory is empty or not.
1125 * Inode supplied must be locked.
1127 * Using a struct dirtemplate here is not precisely
1128 * what we want, but better than using a struct direct.
1130 * NB: does not handle corrupted directories.
1133 ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
1136 struct dirtemplate dbuf;
1137 struct ext2fs_direct_2 *dp = (struct ext2fs_direct_2 *)&dbuf;
1140 #define MINDIRSIZ (sizeof(struct dirtemplate) / 2)
1142 for (off = 0; off < ip->i_size; off += dp->e2d_reclen) {
1143 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1144 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1145 NOCRED, &count, (struct thread *)0);
1147 * Since we read MINDIRSIZ, residual must
1148 * be 0 unless we're at end of file.
1150 if (error || count != 0)
1152 /* avoid infinite loops */
1153 if (dp->e2d_reclen == 0)
1155 /* skip empty entries */
1156 if (dp->e2d_ino == 0)
1158 /* accept only "." and ".." */
1159 namlen = dp->e2d_namlen;
1162 if (dp->e2d_name[0] != '.')
1165 * At this point namlen must be 1 or 2.
1166 * 1 implies ".", 2 implies ".." if second
1171 if (dp->e2d_name[1] == '.' && dp->e2d_ino == parentino)
1179 * Check if source directory is in the path of the target directory.
1180 * Target is supplied locked, source is unlocked.
1181 * The target is always vput before returning.
1184 ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred)
1188 struct dirtemplate dirbuf;
1191 if (target->i_number == source->i_number) {
1195 if (target->i_number == EXT2_ROOTINO) {
1201 if (vp->v_type != VDIR) {
1205 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1206 sizeof(struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1207 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL,
1211 namlen = dirbuf.dotdot_type; /* like ufs little-endian */
1213 dirbuf.dotdot_name[0] != '.' ||
1214 dirbuf.dotdot_name[1] != '.') {
1218 if (dirbuf.dotdot_ino == source->i_number) {
1222 if (dirbuf.dotdot_ino == EXT2_ROOTINO)
1225 if ((error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino,
1226 LK_EXCLUSIVE, &vp)) != 0) {
1233 if (error == ENOTDIR)
1234 printf("checkpath: .. not a directory\n");