2 * modified for Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
8 * Copyright (c) 1989, 1993
9 * The Regents of the University of California. All rights reserved.
10 * (c) UNIX System Laboratories, Inc.
11 * All or some portions of this file are derived from material licensed
12 * to the University of California by American Telephone and Telegraph
13 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
14 * the permission of UNIX System Laboratories, Inc.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#)ufs_lookup.c 8.6 (Berkeley) 4/1/94
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/namei.h>
49 #include <sys/endian.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/malloc.h>
53 #include <sys/dirent.h>
54 #include <sys/sysctl.h>
56 #include <ufs/ufs/dir.h>
58 #include <fs/ext2fs/inode.h>
59 #include <fs/ext2fs/ext2_mount.h>
60 #include <fs/ext2fs/ext2fs.h>
61 #include <fs/ext2fs/ext2_dinode.h>
62 #include <fs/ext2fs/ext2_dir.h>
63 #include <fs/ext2fs/ext2_extern.h>
66 static int dirchk = 1;
68 static int dirchk = 0;
71 static SYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD, 0, "EXT2FS filesystem");
72 SYSCTL_INT(_vfs_e2fs, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
75 DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
76 while it is the native blocksize in ext2fs - thus, a #define
77 is no longer appropriate
81 static u_char ext2_ft_to_dt[] = {
82 DT_UNKNOWN, /* EXT2_FT_UNKNOWN */
83 DT_REG, /* EXT2_FT_REG_FILE */
84 DT_DIR, /* EXT2_FT_DIR */
85 DT_CHR, /* EXT2_FT_CHRDEV */
86 DT_BLK, /* EXT2_FT_BLKDEV */
87 DT_FIFO, /* EXT2_FT_FIFO */
88 DT_SOCK, /* EXT2_FT_SOCK */
89 DT_LNK, /* EXT2_FT_SYMLINK */
92 ((ft) < nitems(ext2_ft_to_dt) ? ext2_ft_to_dt[(ft)] : DT_UNKNOWN)
94 static u_char dt_to_ext2_ft[] = {
95 EXT2_FT_UNKNOWN, /* DT_UNKNOWN */
96 EXT2_FT_FIFO, /* DT_FIFO */
97 EXT2_FT_CHRDEV, /* DT_CHR */
98 EXT2_FT_UNKNOWN, /* unused */
99 EXT2_FT_DIR, /* DT_DIR */
100 EXT2_FT_UNKNOWN, /* unused */
101 EXT2_FT_BLKDEV, /* DT_BLK */
102 EXT2_FT_UNKNOWN, /* unused */
103 EXT2_FT_REG_FILE, /* DT_REG */
104 EXT2_FT_UNKNOWN, /* unused */
105 EXT2_FT_SYMLINK, /* DT_LNK */
106 EXT2_FT_UNKNOWN, /* unused */
107 EXT2_FT_SOCK, /* DT_SOCK */
108 EXT2_FT_UNKNOWN, /* unused */
109 EXT2_FT_UNKNOWN, /* DT_WHT */
112 ((dt) < nitems(dt_to_ext2_ft) ? dt_to_ext2_ft[(dt)] : EXT2_FT_UNKNOWN)
114 static int ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
115 int entryoffsetinblock);
116 static int ext2_is_dot_entry(struct componentname *cnp);
117 static int ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp,
118 struct componentname *cnp, ino_t *dd_ino);
121 ext2_is_dot_entry(struct componentname *cnp)
123 if (cnp->cn_namelen <= 2 && cnp->cn_nameptr[0] == '.' &&
124 (cnp->cn_nameptr[1] == '.' || cnp->cn_nameptr[1] == '0'))
130 * Vnode op for reading directories.
133 ext2_readdir(struct vop_readdir_args *ap)
135 struct vnode *vp = ap->a_vp;
136 struct uio *uio = ap->a_uio;
139 struct ext2fs_direct_2 *dp, *edp;
142 off_t offset, startoffset;
143 size_t readcnt, skipcnt;
146 int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->e2fs_bsize;
149 if (uio->uio_offset < 0)
152 if (ap->a_ncookies != NULL) {
153 ncookies = uio->uio_resid;
154 if (uio->uio_offset >= ip->i_size)
156 else if (ip->i_size - uio->uio_offset < ncookies)
157 ncookies = ip->i_size - uio->uio_offset;
158 ncookies = ncookies / (offsetof(struct ext2fs_direct_2,
159 e2d_namlen) + 4) + 1;
160 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
161 *ap->a_ncookies = ncookies;
162 *ap->a_cookies = cookies;
167 offset = startoffset = uio->uio_offset;
168 startresid = uio->uio_resid;
170 while (error == 0 && uio->uio_resid > 0 &&
171 uio->uio_offset < ip->i_size) {
172 error = ext2_blkatoff(vp, uio->uio_offset, NULL, &bp);
175 if (bp->b_offset + bp->b_bcount > ip->i_size)
176 readcnt = ip->i_size - bp->b_offset;
178 readcnt = bp->b_bcount;
179 skipcnt = (size_t)(uio->uio_offset - bp->b_offset) &
180 ~(size_t)(DIRBLKSIZ - 1);
181 offset = bp->b_offset + skipcnt;
182 dp = (struct ext2fs_direct_2 *)&bp->b_data[skipcnt];
183 edp = (struct ext2fs_direct_2 *)&bp->b_data[readcnt];
184 while (error == 0 && uio->uio_resid > 0 && dp < edp) {
185 if (dp->e2d_reclen <= offsetof(struct ext2fs_direct_2,
186 e2d_namlen) || (caddr_t)dp + dp->e2d_reclen >
192 * "New" ext2fs directory entries differ in 3 ways
193 * from ufs on-disk ones:
194 * - the name is not necessarily NUL-terminated.
195 * - the file type field always exists and always
196 * follows the name length field.
197 * - the file type is encoded in a different way.
199 * "Old" ext2fs directory entries need no special
200 * conversions, since they are binary compatible
201 * with "new" entries having a file type of 0 (i.e.,
202 * EXT2_FT_UNKNOWN). Splitting the old name length
203 * field didn't make a mess like it did in ufs,
204 * because ext2fs uses a machine-independent disk
207 dstdp.d_namlen = dp->e2d_namlen;
208 dstdp.d_type = FTTODT(dp->e2d_type);
209 if (offsetof(struct ext2fs_direct_2, e2d_namlen) +
210 dstdp.d_namlen > dp->e2d_reclen) {
214 if (offset < startoffset || dp->e2d_ino == 0)
216 dstdp.d_fileno = dp->e2d_ino;
217 dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
218 bcopy(dp->e2d_name, dstdp.d_name, dstdp.d_namlen);
219 dstdp.d_name[dstdp.d_namlen] = '\0';
220 if (dstdp.d_reclen > uio->uio_resid) {
221 if (uio->uio_resid == startresid)
228 error = uiomove((caddr_t)&dstdp, dstdp.d_reclen, uio);
231 if (cookies != NULL) {
232 KASSERT(ncookies > 0,
233 ("ext2_readdir: cookies buffer too small"));
234 *cookies = offset + dp->e2d_reclen;
239 offset += dp->e2d_reclen;
240 dp = (struct ext2fs_direct_2 *)((caddr_t)dp +
244 uio->uio_offset = offset;
246 /* We need to correct uio_offset. */
247 uio->uio_offset = offset;
248 if (error == EJUSTRETURN)
250 if (ap->a_ncookies != NULL) {
252 ap->a_ncookies -= ncookies;
254 free(*ap->a_cookies, M_TEMP);
256 *ap->a_cookies = NULL;
259 if (error == 0 && ap->a_eofflag)
260 *ap->a_eofflag = ip->i_size <= uio->uio_offset;
265 * Convert a component of a pathname into a pointer to a locked inode.
266 * This is a very central and rather complicated routine.
267 * If the file system is not maintained in a strict tree hierarchy,
268 * this can result in a deadlock situation (see comments in code below).
270 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
271 * on whether the name is to be looked up, created, renamed, or deleted.
272 * When CREATE, RENAME, or DELETE is specified, information usable in
273 * creating, renaming, or deleting a directory entry may be calculated.
274 * If flag has LOCKPARENT or'ed into it and the target of the pathname
275 * exists, lookup returns both the target and its parent directory locked.
276 * When creating or renaming and LOCKPARENT is specified, the target may
277 * not be ".". When deleting and LOCKPARENT is specified, the target may
278 * be "."., but the caller must check to ensure it does an vrele and vput
279 * instead of two vputs.
281 * Overall outline of ext2_lookup:
283 * search for name in directory, to found or notfound
285 * if creating, return locked directory, leaving info on available slots
288 * if at end of path and deleting, return information to allow delete
289 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
290 * inode and return info to allow rewrite
291 * if not at end, add name to cache; if at end and neither creating
292 * nor deleting, add name to cache
295 ext2_lookup(struct vop_cachedlookup_args *ap)
298 return (ext2_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
302 ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
305 struct inode *dp; /* inode for directory being searched */
306 struct buf *bp; /* a buffer of directory entries */
307 struct ext2fs_direct_2 *ep; /* the current directory entry */
308 int entryoffsetinblock; /* offset of ep in bp's buffer */
309 struct ext2fs_searchslot ss;
310 doff_t i_diroff; /* cached i_diroff value */
311 doff_t i_offset; /* cached i_offset value */
312 int numdirpasses; /* strategy for directory search */
313 doff_t endsearch; /* offset to end directory search */
314 doff_t prevoff; /* prev entry dp->i_offset */
315 struct vnode *pdp; /* saved dp during symlink work */
316 struct vnode *tdp; /* returned by VFS_VGET */
317 doff_t enduseful; /* pointer past last used dir slot */
318 u_long bmask; /* block offset mask */
320 struct ucred *cred = cnp->cn_cred;
321 int flags = cnp->cn_flags;
322 int nameiop = cnp->cn_nameiop;
327 int DIRBLKSIZ = VTOI(vdp)->i_e2fs->e2fs_bsize;
333 bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
339 * We now have a segment name to search for, and a directory to search.
341 * Suppress search for slots unless creating
342 * file and at end of pathname, in which case
343 * we watch for a place to put the new file in
344 * case it doesn't already exist.
347 i_diroff = dp->i_diroff;
348 ss.slotstatus = FOUND;
349 ss.slotfreespace = ss.slotsize = ss.slotneeded = 0;
350 if ((nameiop == CREATE || nameiop == RENAME) &&
351 (flags & ISLASTCN)) {
352 ss.slotstatus = NONE;
353 ss.slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
355 ss.slotneeded = (sizeof(struct direct) - MAXNAMLEN +
356 cnp->cn_namelen + 3) &~ 3; */
360 * Try to lookup dir entry using htree directory index.
362 * If we got an error or we want to find '.' or '..' entry,
363 * we will fall back to linear search.
365 if (!ext2_is_dot_entry(cnp) && ext2_htree_has_idx(dp)) {
367 entryoffsetinblock = 0;
368 switch (ext2_htree_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
369 &bp, &entryoffsetinblock, &i_offset, &prevoff,
372 ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
376 i_offset = roundup2(dp->i_size, DIRBLKSIZ);
380 * Something failed; just fallback to do a linear
388 * If there is cached information on a previous search of
389 * this directory, pick up where we last left off.
390 * We cache only lookups as these are the most common
391 * and have the greatest payoff. Caching CREATE has little
392 * benefit as it usually must search the entire directory
393 * to determine that the entry does not exist. Caching the
394 * location of the last DELETE or RENAME has not reduced
395 * profiling time and hence has been removed in the interest
398 if (nameiop != LOOKUP || i_diroff == 0 ||
399 i_diroff > dp->i_size) {
400 entryoffsetinblock = 0;
405 if ((entryoffsetinblock = i_offset & bmask) &&
406 (error = ext2_blkatoff(vdp, (off_t)i_offset, NULL,
410 nchstats.ncs_2passes++;
413 endsearch = roundup2(dp->i_size, DIRBLKSIZ);
417 while (i_offset < endsearch) {
419 * If necessary, get the next directory block.
423 error = ext2_blkatoff(vdp, (off_t)i_offset, NULL, &bp);
426 entryoffsetinblock = 0;
428 * If still looking for a slot, and at a DIRBLKSIZE
429 * boundary, have to start looking for free space again.
431 if (ss.slotstatus == NONE &&
432 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
434 ss.slotfreespace = 0;
436 error = ext2_search_dirblock(dp, bp->b_data, &entry_found,
437 cnp->cn_nameptr, cnp->cn_namelen,
438 &entryoffsetinblock, &i_offset, &prevoff,
445 ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
446 (entryoffsetinblock & bmask));
454 * If we started in the middle of the directory and failed
455 * to find our target, we must check the beginning as well.
457 if (numdirpasses == 2) {
460 endsearch = i_diroff;
466 * If creating, and at end of pathname and current
467 * directory has not been removed, then can consider
468 * allowing file to be created.
470 if ((nameiop == CREATE || nameiop == RENAME) &&
471 (flags & ISLASTCN) && dp->i_nlink != 0) {
473 * Access for write is interpreted as allowing
474 * creation of files in the directory.
476 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
479 * Return an indication of where the new directory
480 * entry should be put. If we didn't find a slot,
481 * then set dp->i_count to 0 indicating
482 * that the new slot belongs at the end of the
483 * directory. If we found a slot, then the new entry
484 * can be put in the range from dp->i_offset to
485 * dp->i_offset + dp->i_count.
487 if (ss.slotstatus == NONE) {
488 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
490 enduseful = dp->i_offset;
492 dp->i_offset = ss.slotoffset;
493 dp->i_count = ss.slotsize;
494 if (enduseful < ss.slotoffset + ss.slotsize)
495 enduseful = ss.slotoffset + ss.slotsize;
497 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
499 * We return with the directory locked, so that
500 * the parameters we set up above will still be
501 * valid if we actually decide to do a direnter().
502 * We return ni_vp == NULL to indicate that the entry
503 * does not currently exist; we leave a pointer to
504 * the (locked) directory inode in ndp->ni_dvp.
505 * The pathname buffer is saved so that the name
506 * can be obtained later.
508 * NB - if the directory is unlocked, then this
509 * information cannot be used.
511 cnp->cn_flags |= SAVENAME;
512 return (EJUSTRETURN);
515 * Insert name into cache (as non-existent) if appropriate.
517 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
518 cache_enter(vdp, NULL, cnp);
524 if (numdirpasses == 2)
525 nchstats.ncs_pass2++;
527 * Check that directory length properly reflects presence
530 if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen)
532 ext2_dirbad(dp, i_offset, "i_size too small");
533 dp->i_size = entryoffsetinblock+EXT2_DIR_REC_LEN(ep->e2d_namlen);
534 dp->i_flag |= IN_CHANGE | IN_UPDATE;
539 * Found component in pathname.
540 * If the final component of path name, save information
541 * in the cache as to where the entry was found.
543 if ((flags & ISLASTCN) && nameiop == LOOKUP)
544 dp->i_diroff = i_offset &~ (DIRBLKSIZ - 1);
546 * If deleting, and at end of pathname, return
547 * parameters which can be used to remove file.
549 if (nameiop == DELETE && (flags & ISLASTCN)) {
550 if (flags & LOCKPARENT)
551 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
553 * Write access to directory required to delete files.
555 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
558 * Return pointer to current entry in dp->i_offset,
559 * and distance past previous entry (if there
560 * is a previous entry in this block) in dp->i_count.
561 * Save directory inode pointer in ndp->ni_dvp for dirremove().
563 * Technically we shouldn't be setting these in the
564 * WANTPARENT case (first lookup in rename()), but any
565 * lookups that will result in directory changes will
568 dp->i_offset = i_offset;
569 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
572 dp->i_count = dp->i_offset - prevoff;
575 if (dp->i_number == ino) {
580 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
584 * If directory is "sticky", then user must own
585 * the directory, or the file in it, else she
586 * may not delete it (unless she's root). This
587 * implements append-only directories.
589 if ((dp->i_mode & ISVTX) &&
591 cred->cr_uid != dp->i_uid &&
592 VTOI(tdp)->i_uid != cred->cr_uid) {
601 * If rewriting (RENAME), return the inode and the
602 * information required to rewrite the present directory
603 * Must get inode of directory entry to verify it's a
604 * regular file, or empty directory.
606 if (nameiop == RENAME && (flags & ISLASTCN)) {
607 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
610 * Careful about locking second inode.
611 * This can only occur if the target is ".".
613 dp->i_offset = i_offset;
614 if (dp->i_number == ino)
618 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
622 cnp->cn_flags |= SAVENAME;
629 * Step through the translation in the name. We do not `vput' the
630 * directory because we may need it again if a symbolic link
631 * is relative to the current directory. Instead we save it
632 * unlocked as "pdp". We must get the target inode before unlocking
633 * the directory to insure that the inode will not be removed
634 * before we get it. We prevent deadlock by always fetching
635 * inodes from the root, moving down the directory tree. Thus
636 * when following backward pointers ".." we must unlock the
637 * parent directory before getting the requested directory.
638 * There is a potential race condition here if both the current
639 * and parent directories are removed before the VFS_VGET for the
640 * inode associated with ".." returns. We hope that this occurs
641 * infrequently since we cannot avoid this race condition without
642 * implementing a sophisticated deadlock detection algorithm.
643 * Note also that this simple deadlock detection scheme will not
644 * work if the file system has any hard links other than ".."
645 * that point backwards in the directory structure.
648 if (flags & ISDOTDOT) {
649 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
650 if (pdp->v_iflag & VI_DOOMED) {
658 * Recheck that ".." entry in the vdp directory points
659 * to the inode we looked up before vdp lock was
662 error = ext2_lookup_ino(pdp, NULL, cnp, &ino1);
672 } else if (dp->i_number == ino) {
673 VREF(vdp); /* we want ourself, ie "." */
675 * When we lookup "." we still can be asked to lock it
678 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
679 if (ltype != VOP_ISLOCKED(vdp)) {
680 if (ltype == LK_EXCLUSIVE)
681 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
682 else /* if (ltype == LK_SHARED) */
683 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
687 if ((error = VFS_VGET(vdp->v_mount, ino, cnp->cn_lkflags,
694 * Insert name into cache if appropriate.
696 if (cnp->cn_flags & MAKEENTRY)
697 cache_enter(vdp, *vpp, cnp);
702 ext2_search_dirblock(struct inode *ip, void *data, int *foundp,
703 const char *name, int namelen, int *entryoffsetinblockp,
704 doff_t *offp, doff_t *prevoffp, doff_t *endusefulp,
705 struct ext2fs_searchslot *ssp)
708 struct ext2fs_direct_2 *ep, *top;
709 uint32_t bsize = ip->i_e2fs->e2fs_bsize;
710 int offset = *entryoffsetinblockp;
715 ep = (struct ext2fs_direct_2 *)((char *)data + offset);
716 top = (struct ext2fs_direct_2 *)((char *)data +
717 bsize - EXT2_DIR_REC_LEN(0));
721 * Full validation checks are slow, so we only check
722 * enough to insure forward progress through the
723 * directory. Complete checks can be run by setting
724 * "vfs.e2fs.dirchk" to be true.
726 if (ep->e2d_reclen == 0 ||
727 (dirchk && ext2_dirbadentry(vdp, ep, offset))) {
729 ext2_dirbad(ip, *offp, "mangled entry");
730 i = bsize - (offset & (bsize - 1));
737 * If an appropriate sized slot has not yet been found,
738 * check to see if one is available. Also accumulate space
739 * in the current block so that we can determine if
740 * compaction is viable.
742 if (ssp->slotstatus != FOUND) {
743 int size = ep->e2d_reclen;
745 if (ep->e2d_ino != 0)
746 size -= EXT2_DIR_REC_LEN(ep->e2d_namlen);
748 if (size >= ssp->slotneeded) {
749 ssp->slotstatus = FOUND;
750 ssp->slotoffset = *offp;
751 ssp->slotsize = ep->e2d_reclen;
752 } else if (ssp->slotstatus == NONE) {
753 ssp->slotfreespace += size;
754 if (ssp->slotoffset == -1)
755 ssp->slotoffset = *offp;
756 if (ssp->slotfreespace >= ssp->slotneeded) {
757 ssp->slotstatus = COMPACT;
758 ssp->slotsize = *offp +
767 * Check for a name match.
770 namlen = ep->e2d_namlen;
771 if (namlen == namelen &&
772 !bcmp(name, ep->e2d_name, (unsigned)namlen)) {
774 * Save directory entry's inode number and
775 * reclen in ndp->ni_ufs area, and release
783 *offp += ep->e2d_reclen;
784 offset += ep->e2d_reclen;
785 *entryoffsetinblockp = offset;
789 * Get pointer to the next entry.
791 ep = (struct ext2fs_direct_2 *)((char *)data + offset);
798 ext2_dirbad(struct inode *ip, doff_t offset, char *how)
802 mp = ITOV(ip)->v_mount;
803 if ((mp->mnt_flag & MNT_RDONLY) == 0)
804 panic("ext2_dirbad: %s: bad dir ino %lu at offset %ld: %s\n",
805 mp->mnt_stat.f_mntonname, (u_long)ip->i_number,(long)offset, how);
807 (void)printf("%s: bad dir ino %lu at offset %ld: %s\n",
808 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
813 * Do consistency checking on a directory entry:
814 * record length must be multiple of 4
815 * entry must fit in rest of its DIRBLKSIZ block
816 * record must be large enough to contain entry
817 * name is not longer than MAXNAMLEN
818 * name must be as long as advertised, and null terminated
821 * changed so that it confirms to ext2_check_dir_entry
824 ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
825 int entryoffsetinblock)
827 int DIRBLKSIZ = VTOI(dp)->i_e2fs->e2fs_bsize;
829 char * error_msg = NULL;
831 if (de->e2d_reclen < EXT2_DIR_REC_LEN(1))
832 error_msg = "rec_len is smaller than minimal";
833 else if (de->e2d_reclen % 4 != 0)
834 error_msg = "rec_len % 4 != 0";
835 else if (de->e2d_reclen < EXT2_DIR_REC_LEN(de->e2d_namlen))
836 error_msg = "reclen is too small for name_len";
837 else if (entryoffsetinblock + de->e2d_reclen > DIRBLKSIZ)
838 error_msg = "directory entry across blocks";
840 if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count)
841 error_msg = "inode out of bounds";
844 if (error_msg != NULL) {
845 printf("bad directory entry: %s\n", error_msg);
846 printf("offset=%d, inode=%lu, rec_len=%u, name_len=%u\n",
847 entryoffsetinblock, (unsigned long)de->e2d_ino,
848 de->e2d_reclen, de->e2d_namlen);
850 return error_msg == NULL ? 0 : 1;
854 * Write a directory entry after a call to namei, using the parameters
855 * that it left in nameidata. The argument ip is the inode which the new
856 * directory entry will refer to. Dvp is a pointer to the directory to
857 * be written, which was left locked by namei. Remaining parameters
858 * (dp->i_offset, dp->i_count) indicate how the space for the new
859 * entry is to be obtained.
862 ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp)
865 struct ext2fs_direct_2 newdir;
868 int error, newentrysize;
869 int DIRBLKSIZ = ip->i_e2fs->e2fs_bsize;
873 if ((cnp->cn_flags & SAVENAME) == 0)
874 panic("ext2_direnter: missing name");
877 newdir.e2d_ino = ip->i_number;
878 newdir.e2d_namlen = cnp->cn_namelen;
879 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
880 EXT2F_INCOMPAT_FTYPE))
881 newdir.e2d_type = DTTOFT(IFTODT(ip->i_mode));
883 newdir.e2d_type = EXT2_FT_UNKNOWN;
884 bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1);
885 newentrysize = EXT2_DIR_REC_LEN(newdir.e2d_namlen);
888 if (ext2_htree_has_idx(dp)) {
889 error = ext2_htree_add_entry(dvp, &newdir, cnp);
891 dp->i_flags &= ~EXT4_INDEX;
892 dp->i_flags |= IN_CHANGE | IN_UPDATE;
897 if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) &&
898 !ext2_htree_has_idx(dp)) {
899 if ((dp->i_size / DIRBLKSIZ) == 1 &&
900 dp->i_offset == DIRBLKSIZ) {
902 * Making indexed directory when one block is not
903 * enough to save all entries.
905 return ext2_htree_create_index(dvp, cnp, &newdir);
908 #endif /* EXT2FS_HTREE */
910 if (dp->i_count == 0) {
912 * If dp->i_count is 0, then namei could find no
913 * space in the directory. Here, dp->i_offset will
914 * be on a directory block boundary and we will write the
915 * new entry into a fresh block.
917 if (dp->i_offset & (DIRBLKSIZ - 1))
918 panic("ext2_direnter: newblk");
919 auio.uio_offset = dp->i_offset;
920 newdir.e2d_reclen = DIRBLKSIZ;
921 auio.uio_resid = newentrysize;
922 aiov.iov_len = newentrysize;
923 aiov.iov_base = (caddr_t)&newdir;
924 auio.uio_iov = &aiov;
926 auio.uio_rw = UIO_WRITE;
927 auio.uio_segflg = UIO_SYSSPACE;
928 auio.uio_td = (struct thread *)0;
929 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
931 VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
932 /* XXX should grow with balloc() */
933 panic("ext2_direnter: frag size");
935 dp->i_size = roundup2(dp->i_size, DIRBLKSIZ);
936 dp->i_flag |= IN_CHANGE;
941 error = ext2_add_entry(dvp, &newdir);
942 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
943 error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
944 cnp->cn_cred, cnp->cn_thread);
949 * Insert an entry into the directory block.
950 * Compact the contents.
953 ext2_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry)
955 struct ext2fs_direct_2 *ep, *nep;
959 int error, loc, newentrysize, spacefree;
965 * If dp->i_count is non-zero, then namei found space
966 * for the new entry in the range dp->i_offset to
967 * dp->i_offset + dp->i_count in the directory.
968 * To use this space, we may have to compact the entries located
969 * there, by copying them together towards the beginning of the
970 * block, leaving the free space in one usable chunk at the end.
974 * Increase size of directory if entry eats into new space.
975 * This should never push the size past a new multiple of
978 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
980 if (dp->i_offset + dp->i_count > dp->i_size)
981 dp->i_size = dp->i_offset + dp->i_count;
983 * Get the block containing the space for the new directory entry.
985 if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf,
989 * Find space for the new entry. In the simple case, the entry at
990 * offset base will have the space. If it does not, then namei
991 * arranged that compacting the region dp->i_offset to
992 * dp->i_offset + dp->i_count would yield the
995 newentrysize = EXT2_DIR_REC_LEN(entry->e2d_namlen);
996 ep = (struct ext2fs_direct_2 *)dirbuf;
997 dsize = EXT2_DIR_REC_LEN(ep->e2d_namlen);
998 spacefree = ep->e2d_reclen - dsize;
999 for (loc = ep->e2d_reclen; loc < dp->i_count; ) {
1000 nep = (struct ext2fs_direct_2 *)(dirbuf + loc);
1002 /* trim the existing slot */
1003 ep->e2d_reclen = dsize;
1004 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1006 /* overwrite; nothing there; header is ours */
1009 dsize = EXT2_DIR_REC_LEN(nep->e2d_namlen);
1010 spacefree += nep->e2d_reclen - dsize;
1011 loc += nep->e2d_reclen;
1012 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1015 * Update the pointer fields in the previous entry (if any),
1016 * copy in the new entry, and write out the block.
1018 if (ep->e2d_ino == 0) {
1019 if (spacefree + dsize < newentrysize)
1020 panic("ext2_direnter: compact1");
1021 entry->e2d_reclen = spacefree + dsize;
1023 if (spacefree < newentrysize)
1024 panic("ext2_direnter: compact2");
1025 entry->e2d_reclen = spacefree;
1026 ep->e2d_reclen = dsize;
1027 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1029 bcopy((caddr_t)entry, (caddr_t)ep, (u_int)newentrysize);
1030 if (DOINGASYNC(dvp)) {
1036 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1041 * Remove a directory entry after a call to namei, using
1042 * the parameters which it left in nameidata. The entry
1043 * dp->i_offset contains the offset into the directory of the
1044 * entry to be eliminated. The dp->i_count field contains the
1045 * size of the previous record in the directory. If this
1046 * is 0, the first entry is being deleted, so we need only
1047 * zero the inode number to mark the entry as free. If the
1048 * entry is not the first in the directory, we must reclaim
1049 * the space of the now empty record by adding the record size
1050 * to the size of the previous entry.
1053 ext2_dirremove(struct vnode *dvp, struct componentname *cnp)
1056 struct ext2fs_direct_2 *ep, *rep;
1061 if (dp->i_count == 0) {
1063 * First entry in block: set d_ino to zero.
1066 ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep,
1071 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1075 * Collapse new free space into previous entry.
1077 if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
1078 (char **)&ep, &bp)) != 0)
1081 /* Set 'rep' to the entry being removed. */
1082 if (dp->i_count == 0)
1085 rep = (struct ext2fs_direct_2 *)((char *)ep + ep->e2d_reclen);
1086 ep->e2d_reclen += rep->e2d_reclen;
1087 if (DOINGASYNC(dvp) && dp->i_count != 0)
1091 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1096 * Rewrite an existing directory entry to point at the inode
1097 * supplied. The parameters describing the directory entry are
1098 * set up by a call to namei.
1101 ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp)
1104 struct ext2fs_direct_2 *ep;
1105 struct vnode *vdp = ITOV(dp);
1108 if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep,
1111 ep->e2d_ino = ip->i_number;
1112 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1113 EXT2F_INCOMPAT_FTYPE))
1114 ep->e2d_type = DTTOFT(IFTODT(ip->i_mode));
1116 ep->e2d_type = EXT2_FT_UNKNOWN;
1118 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1123 * Check if a directory is empty or not.
1124 * Inode supplied must be locked.
1126 * Using a struct dirtemplate here is not precisely
1127 * what we want, but better than using a struct direct.
1129 * NB: does not handle corrupted directories.
1132 ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
1135 struct dirtemplate dbuf;
1136 struct ext2fs_direct_2 *dp = (struct ext2fs_direct_2 *)&dbuf;
1139 #define MINDIRSIZ (sizeof(struct dirtemplate) / 2)
1141 for (off = 0; off < ip->i_size; off += dp->e2d_reclen) {
1142 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1143 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1144 NOCRED, &count, (struct thread *)0);
1146 * Since we read MINDIRSIZ, residual must
1147 * be 0 unless we're at end of file.
1149 if (error || count != 0)
1151 /* avoid infinite loops */
1152 if (dp->e2d_reclen == 0)
1154 /* skip empty entries */
1155 if (dp->e2d_ino == 0)
1157 /* accept only "." and ".." */
1158 namlen = dp->e2d_namlen;
1161 if (dp->e2d_name[0] != '.')
1164 * At this point namlen must be 1 or 2.
1165 * 1 implies ".", 2 implies ".." if second
1170 if (dp->e2d_name[1] == '.' && dp->e2d_ino == parentino)
1178 * Check if source directory is in the path of the target directory.
1179 * Target is supplied locked, source is unlocked.
1180 * The target is always vput before returning.
1183 ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred)
1187 struct dirtemplate dirbuf;
1190 if (target->i_number == source->i_number) {
1194 if (target->i_number == EXT2_ROOTINO) {
1200 if (vp->v_type != VDIR) {
1204 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1205 sizeof(struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1206 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL,
1210 namlen = dirbuf.dotdot_type; /* like ufs little-endian */
1212 dirbuf.dotdot_name[0] != '.' ||
1213 dirbuf.dotdot_name[1] != '.') {
1217 if (dirbuf.dotdot_ino == source->i_number) {
1221 if (dirbuf.dotdot_ino == EXT2_ROOTINO)
1224 if ((error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino,
1225 LK_EXCLUSIVE, &vp)) != 0) {
1232 if (error == ENOTDIR)
1233 printf("checkpath: .. not a directory\n");