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_lookup_ino(struct vnode *vdp, struct vnode **vpp,
117 struct componentname *cnp, ino_t *dd_ino);
120 * Vnode op for reading directories.
122 * This function has to convert directory entries from the on-disk
123 * format to the format defined by <sys/dirent.h>. Unfortunately, the
124 * conversion will blow up some entries by four bytes, so it can't be
125 * done in place. Instead, the conversion is done entry by entry and
126 * the converted entry is sent via uiomove.
128 * XXX allocate a buffer, convert as many entries as possible, then send
129 * the whole buffer to uiomove
132 ext2_readdir(struct vop_readdir_args *ap)
134 struct uio *uio = ap->a_uio;
137 struct ext2fs_direct_2 *edp, *dp;
143 int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->e2fs_bsize;
145 off_t startoffset = uio->uio_offset;
147 count = uio->uio_resid;
149 * Avoid complications for partial directory entries by adjusting
150 * the i/o to end at a block boundary. Don't give up (like ufs
151 * does) if the initial adjustment gives a negative count, since
152 * many callers don't supply a large enough buffer. The correct
153 * size is a little larger than DIRBLKSIZ to allow for expansion
154 * of directory entries, but some callers just use 512.
156 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
160 auio.uio_iov = &aiov;
162 auio.uio_resid = count;
163 auio.uio_segflg = UIO_SYSSPACE;
164 aiov.iov_len = count;
165 dirbuf = malloc(count, M_TEMP, M_WAITOK);
166 aiov.iov_base = dirbuf;
167 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
169 readcnt = count - auio.uio_resid;
170 edp = (struct ext2fs_direct_2 *)&dirbuf[readcnt];
172 bzero(&dstdp, offsetof(struct dirent, d_name));
173 for (dp = (struct ext2fs_direct_2 *)dirbuf;
174 !error && uio->uio_resid > 0 && dp < edp; ) {
176 * "New" ext2fs directory entries differ in 3 ways
177 * from ufs on-disk ones:
178 * - the name is not necessarily NUL-terminated.
179 * - the file type field always exists and always
180 * follows the name length field.
181 * - the file type is encoded in a different way.
183 * "Old" ext2fs directory entries need no special
184 * conversions, since they are binary compatible
185 * with "new" entries having a file type of 0 (i.e.,
186 * EXT2_FT_UNKNOWN). Splitting the old name length
187 * field didn't make a mess like it did in ufs,
188 * because ext2fs uses a machine-independent disk
191 dstdp.d_fileno = dp->e2d_ino;
192 dstdp.d_type = FTTODT(dp->e2d_type);
193 dstdp.d_namlen = dp->e2d_namlen;
194 dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
195 bcopy(dp->e2d_name, dstdp.d_name, dstdp.d_namlen);
196 bzero(dstdp.d_name + dstdp.d_namlen,
197 dstdp.d_reclen - offsetof(struct dirent, d_name) -
200 if (dp->e2d_reclen > 0) {
201 if(dstdp.d_reclen <= uio->uio_resid) {
203 dp = (struct ext2fs_direct_2 *)
204 ((char *)dp + dp->e2d_reclen);
206 uiomove(&dstdp, dstdp.d_reclen, uio);
216 /* we need to correct uio_offset */
217 uio->uio_offset = startoffset + (caddr_t)dp - dirbuf;
219 if (!error && ap->a_ncookies != NULL) {
220 u_long *cookiep, *cookies, *ecookies;
223 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
224 panic("ext2_readdir: unexpected uio from NFS server");
225 cookies = malloc(ncookies * sizeof(u_long), M_TEMP,
228 for (dp = (struct ext2fs_direct_2 *)dirbuf,
229 cookiep = cookies, ecookies = cookies + ncookies;
231 dp = (struct ext2fs_direct_2 *)((caddr_t) dp + dp->e2d_reclen)) {
232 off += dp->e2d_reclen;
233 *cookiep++ = (u_long) off;
235 *ap->a_ncookies = ncookies;
236 *ap->a_cookies = cookies;
239 free(dirbuf, M_TEMP);
241 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
246 * Convert a component of a pathname into a pointer to a locked inode.
247 * This is a very central and rather complicated routine.
248 * If the file system is not maintained in a strict tree hierarchy,
249 * this can result in a deadlock situation (see comments in code below).
251 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
252 * on whether the name is to be looked up, created, renamed, or deleted.
253 * When CREATE, RENAME, or DELETE is specified, information usable in
254 * creating, renaming, or deleting a directory entry may be calculated.
255 * If flag has LOCKPARENT or'ed into it and the target of the pathname
256 * exists, lookup returns both the target and its parent directory locked.
257 * When creating or renaming and LOCKPARENT is specified, the target may
258 * not be ".". When deleting and LOCKPARENT is specified, the target may
259 * be "."., but the caller must check to ensure it does an vrele and vput
260 * instead of two vputs.
262 * Overall outline of ext2_lookup:
264 * search for name in directory, to found or notfound
266 * if creating, return locked directory, leaving info on available slots
269 * if at end of path and deleting, return information to allow delete
270 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
271 * inode and return info to allow rewrite
272 * if not at end, add name to cache; if at end and neither creating
273 * nor deleting, add name to cache
276 ext2_lookup(struct vop_cachedlookup_args *ap)
279 return (ext2_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
283 ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
286 struct inode *dp; /* inode for directory being searched */
287 struct buf *bp; /* a buffer of directory entries */
288 struct ext2fs_direct_2 *ep; /* the current directory entry */
289 int entryoffsetinblock; /* offset of ep in bp's buffer */
290 enum {NONE, COMPACT, FOUND} slotstatus;
291 doff_t slotoffset; /* offset of area with free space */
292 doff_t i_diroff; /* cached i_diroff value */
293 doff_t i_offset; /* cached i_offset value */
294 int slotsize; /* size of area at slotoffset */
295 int slotfreespace; /* amount of space free in slot */
296 int slotneeded; /* size of the entry we're seeking */
297 int numdirpasses; /* strategy for directory search */
298 doff_t endsearch; /* offset to end directory search */
299 doff_t prevoff; /* prev entry dp->i_offset */
300 struct vnode *pdp; /* saved dp during symlink work */
301 struct vnode *tdp; /* returned by VFS_VGET */
302 doff_t enduseful; /* pointer past last used dir slot */
303 u_long bmask; /* block offset mask */
305 struct ucred *cred = cnp->cn_cred;
306 int flags = cnp->cn_flags;
307 int nameiop = cnp->cn_nameiop;
311 int DIRBLKSIZ = VTOI(vdp)->i_e2fs->e2fs_bsize;
317 bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
323 * We now have a segment name to search for, and a directory to search.
327 * Suppress search for slots unless creating
328 * file and at end of pathname, in which case
329 * we watch for a place to put the new file in
330 * case it doesn't already exist.
333 i_diroff = dp->i_diroff;
335 slotfreespace = slotsize = slotneeded = 0;
336 if ((nameiop == CREATE || nameiop == RENAME) &&
337 (flags & ISLASTCN)) {
339 slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
341 slotneeded = (sizeof(struct direct) - MAXNAMLEN +
342 cnp->cn_namelen + 3) &~ 3; */
346 * If there is cached information on a previous search of
347 * this directory, pick up where we last left off.
348 * We cache only lookups as these are the most common
349 * and have the greatest payoff. Caching CREATE has little
350 * benefit as it usually must search the entire directory
351 * to determine that the entry does not exist. Caching the
352 * location of the last DELETE or RENAME has not reduced
353 * profiling time and hence has been removed in the interest
356 if (nameiop != LOOKUP || i_diroff == 0 ||
357 i_diroff > dp->i_size) {
358 entryoffsetinblock = 0;
363 if ((entryoffsetinblock = i_offset & bmask) &&
364 (error = ext2_blkatoff(vdp, (off_t)i_offset, NULL,
368 nchstats.ncs_2passes++;
371 endsearch = roundup2(dp->i_size, DIRBLKSIZ);
375 while (i_offset < endsearch) {
377 * If necessary, get the next directory block.
379 if ((i_offset & bmask) == 0) {
383 ext2_blkatoff(vdp, (off_t)i_offset, NULL,
386 entryoffsetinblock = 0;
389 * If still looking for a slot, and at a DIRBLKSIZE
390 * boundary, have to start looking for free space again.
392 if (slotstatus == NONE &&
393 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
398 * Get pointer to next entry.
399 * Full validation checks are slow, so we only check
400 * enough to insure forward progress through the
401 * directory. Complete checks can be run by setting
402 * "vfs.e2fs.dirchk" to be true.
404 ep = (struct ext2fs_direct_2 *)
405 ((char *)bp->b_data + entryoffsetinblock);
406 if (ep->e2d_reclen == 0 ||
407 (dirchk && ext2_dirbadentry(vdp, ep, entryoffsetinblock))) {
409 ext2_dirbad(dp, i_offset, "mangled entry");
410 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
412 entryoffsetinblock += i;
417 * If an appropriate sized slot has not yet been found,
418 * check to see if one is available. Also accumulate space
419 * in the current block so that we can determine if
420 * compaction is viable.
422 if (slotstatus != FOUND) {
423 int size = ep->e2d_reclen;
425 if (ep->e2d_ino != 0)
426 size -= EXT2_DIR_REC_LEN(ep->e2d_namlen);
428 if (size >= slotneeded) {
430 slotoffset = i_offset;
431 slotsize = ep->e2d_reclen;
432 } else if (slotstatus == NONE) {
433 slotfreespace += size;
434 if (slotoffset == -1)
435 slotoffset = i_offset;
436 if (slotfreespace >= slotneeded) {
437 slotstatus = COMPACT;
438 slotsize = i_offset +
439 ep->e2d_reclen - slotoffset;
446 * Check for a name match.
449 namlen = ep->e2d_namlen;
450 if (namlen == cnp->cn_namelen &&
451 !bcmp(cnp->cn_nameptr, ep->e2d_name,
454 * Save directory entry's inode number and
455 * reclen in ndp->ni_ufs area, and release
463 i_offset += ep->e2d_reclen;
464 entryoffsetinblock += ep->e2d_reclen;
466 enduseful = i_offset;
470 * If we started in the middle of the directory and failed
471 * to find our target, we must check the beginning as well.
473 if (numdirpasses == 2) {
476 endsearch = i_diroff;
482 * If creating, and at end of pathname and current
483 * directory has not been removed, then can consider
484 * allowing file to be created.
486 if ((nameiop == CREATE || nameiop == RENAME) &&
487 (flags & ISLASTCN) && dp->i_nlink != 0) {
489 * Access for write is interpreted as allowing
490 * creation of files in the directory.
492 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
495 * Return an indication of where the new directory
496 * entry should be put. If we didn't find a slot,
497 * then set dp->i_count to 0 indicating
498 * that the new slot belongs at the end of the
499 * directory. If we found a slot, then the new entry
500 * can be put in the range from dp->i_offset to
501 * dp->i_offset + dp->i_count.
503 if (slotstatus == NONE) {
504 dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
506 enduseful = dp->i_offset;
508 dp->i_offset = slotoffset;
509 dp->i_count = slotsize;
510 if (enduseful < slotoffset + slotsize)
511 enduseful = slotoffset + slotsize;
513 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
515 * We return with the directory locked, so that
516 * the parameters we set up above will still be
517 * valid if we actually decide to do a direnter().
518 * We return ni_vp == NULL to indicate that the entry
519 * does not currently exist; we leave a pointer to
520 * the (locked) directory inode in ndp->ni_dvp.
521 * The pathname buffer is saved so that the name
522 * can be obtained later.
524 * NB - if the directory is unlocked, then this
525 * information cannot be used.
527 cnp->cn_flags |= SAVENAME;
528 return (EJUSTRETURN);
531 * Insert name into cache (as non-existent) if appropriate.
533 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
534 cache_enter(vdp, NULL, cnp);
540 if (numdirpasses == 2)
541 nchstats.ncs_pass2++;
543 * Check that directory length properly reflects presence
546 if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen)
548 ext2_dirbad(dp, i_offset, "i_size too small");
549 dp->i_size = entryoffsetinblock+EXT2_DIR_REC_LEN(ep->e2d_namlen);
550 dp->i_flag |= IN_CHANGE | IN_UPDATE;
555 * Found component in pathname.
556 * If the final component of path name, save information
557 * in the cache as to where the entry was found.
559 if ((flags & ISLASTCN) && nameiop == LOOKUP)
560 dp->i_diroff = i_offset &~ (DIRBLKSIZ - 1);
562 * If deleting, and at end of pathname, return
563 * parameters which can be used to remove file.
565 if (nameiop == DELETE && (flags & ISLASTCN)) {
566 if (flags & LOCKPARENT)
567 ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
569 * Write access to directory required to delete files.
571 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
574 * Return pointer to current entry in dp->i_offset,
575 * and distance past previous entry (if there
576 * is a previous entry in this block) in dp->i_count.
577 * Save directory inode pointer in ndp->ni_dvp for dirremove().
579 * Technically we shouldn't be setting these in the
580 * WANTPARENT case (first lookup in rename()), but any
581 * lookups that will result in directory changes will
584 dp->i_offset = i_offset;
585 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
588 dp->i_count = dp->i_offset - prevoff;
591 if (dp->i_number == ino) {
596 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
600 * If directory is "sticky", then user must own
601 * the directory, or the file in it, else she
602 * may not delete it (unless she's root). This
603 * implements append-only directories.
605 if ((dp->i_mode & ISVTX) &&
607 cred->cr_uid != dp->i_uid &&
608 VTOI(tdp)->i_uid != cred->cr_uid) {
617 * If rewriting (RENAME), return the inode and the
618 * information required to rewrite the present directory
619 * Must get inode of directory entry to verify it's a
620 * regular file, or empty directory.
622 if (nameiop == RENAME && (flags & ISLASTCN)) {
623 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
626 * Careful about locking second inode.
627 * This can only occur if the target is ".".
629 dp->i_offset = i_offset;
630 if (dp->i_number == ino)
634 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
638 cnp->cn_flags |= SAVENAME;
645 * Step through the translation in the name. We do not `vput' the
646 * directory because we may need it again if a symbolic link
647 * is relative to the current directory. Instead we save it
648 * unlocked as "pdp". We must get the target inode before unlocking
649 * the directory to insure that the inode will not be removed
650 * before we get it. We prevent deadlock by always fetching
651 * inodes from the root, moving down the directory tree. Thus
652 * when following backward pointers ".." we must unlock the
653 * parent directory before getting the requested directory.
654 * There is a potential race condition here if both the current
655 * and parent directories are removed before the VFS_VGET for the
656 * inode associated with ".." returns. We hope that this occurs
657 * infrequently since we cannot avoid this race condition without
658 * implementing a sophisticated deadlock detection algorithm.
659 * Note also that this simple deadlock detection scheme will not
660 * work if the file system has any hard links other than ".."
661 * that point backwards in the directory structure.
664 if (flags & ISDOTDOT) {
665 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
666 if (pdp->v_iflag & VI_DOOMED) {
674 * Recheck that ".." entry in the vdp directory points
675 * to the inode we looked up before vdp lock was
678 error = ext2_lookup_ino(pdp, NULL, cnp, &ino1);
688 } else if (dp->i_number == ino) {
689 VREF(vdp); /* we want ourself, ie "." */
691 * When we lookup "." we still can be asked to lock it
694 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
695 if (ltype != VOP_ISLOCKED(vdp)) {
696 if (ltype == LK_EXCLUSIVE)
697 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
698 else /* if (ltype == LK_SHARED) */
699 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
703 if ((error = VFS_VGET(vdp->v_mount, ino, cnp->cn_lkflags,
710 * Insert name into cache if appropriate.
712 if (cnp->cn_flags & MAKEENTRY)
713 cache_enter(vdp, *vpp, cnp);
718 ext2_dirbad(struct inode *ip, doff_t offset, char *how)
722 mp = ITOV(ip)->v_mount;
723 if ((mp->mnt_flag & MNT_RDONLY) == 0)
724 panic("ext2_dirbad: %s: bad dir ino %lu at offset %ld: %s\n",
725 mp->mnt_stat.f_mntonname, (u_long)ip->i_number,(long)offset, how);
727 (void)printf("%s: bad dir ino %lu at offset %ld: %s\n",
728 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
733 * Do consistency checking on a directory entry:
734 * record length must be multiple of 4
735 * entry must fit in rest of its DIRBLKSIZ block
736 * record must be large enough to contain entry
737 * name is not longer than MAXNAMLEN
738 * name must be as long as advertised, and null terminated
741 * changed so that it confirms to ext2_check_dir_entry
744 ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
745 int entryoffsetinblock)
747 int DIRBLKSIZ = VTOI(dp)->i_e2fs->e2fs_bsize;
749 char * error_msg = NULL;
751 if (de->e2d_reclen < EXT2_DIR_REC_LEN(1))
752 error_msg = "rec_len is smaller than minimal";
753 else if (de->e2d_reclen % 4 != 0)
754 error_msg = "rec_len % 4 != 0";
755 else if (de->e2d_reclen < EXT2_DIR_REC_LEN(de->e2d_namlen))
756 error_msg = "reclen is too small for name_len";
757 else if (entryoffsetinblock + de->e2d_reclen > DIRBLKSIZ)
758 error_msg = "directory entry across blocks";
760 if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count)
761 error_msg = "inode out of bounds";
764 if (error_msg != NULL) {
765 printf("bad directory entry: %s\n", error_msg);
766 printf("offset=%d, inode=%lu, rec_len=%u, name_len=%u\n",
767 entryoffsetinblock, (unsigned long)de->e2d_ino,
768 de->e2d_reclen, de->e2d_namlen);
770 return error_msg == NULL ? 0 : 1;
774 * Write a directory entry after a call to namei, using the parameters
775 * that it left in nameidata. The argument ip is the inode which the new
776 * directory entry will refer to. Dvp is a pointer to the directory to
777 * be written, which was left locked by namei. Remaining parameters
778 * (dp->i_offset, dp->i_count) indicate how the space for the new
779 * entry is to be obtained.
782 ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp)
784 struct ext2fs_direct_2 *ep, *nep;
787 struct ext2fs_direct_2 newdir;
791 int error, loc, newentrysize, spacefree;
793 int DIRBLKSIZ = ip->i_e2fs->e2fs_bsize;
797 if ((cnp->cn_flags & SAVENAME) == 0)
798 panic("ext2_direnter: missing name");
801 newdir.e2d_ino = ip->i_number;
802 newdir.e2d_namlen = cnp->cn_namelen;
803 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
804 EXT2F_INCOMPAT_FTYPE))
805 newdir.e2d_type = DTTOFT(IFTODT(ip->i_mode));
807 newdir.e2d_type = EXT2_FT_UNKNOWN;
808 bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1);
809 newentrysize = EXT2_DIR_REC_LEN(newdir.e2d_namlen);
810 if (dp->i_count == 0) {
812 * If dp->i_count is 0, then namei could find no
813 * space in the directory. Here, dp->i_offset will
814 * be on a directory block boundary and we will write the
815 * new entry into a fresh block.
817 if (dp->i_offset & (DIRBLKSIZ - 1))
818 panic("ext2_direnter: newblk");
819 auio.uio_offset = dp->i_offset;
820 newdir.e2d_reclen = DIRBLKSIZ;
821 auio.uio_resid = newentrysize;
822 aiov.iov_len = newentrysize;
823 aiov.iov_base = (caddr_t)&newdir;
824 auio.uio_iov = &aiov;
826 auio.uio_rw = UIO_WRITE;
827 auio.uio_segflg = UIO_SYSSPACE;
828 auio.uio_td = (struct thread *)0;
829 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
831 VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
832 /* XXX should grow with balloc() */
833 panic("ext2_direnter: frag size");
835 dp->i_size = roundup2(dp->i_size, DIRBLKSIZ);
836 dp->i_flag |= IN_CHANGE;
842 * If dp->i_count is non-zero, then namei found space
843 * for the new entry in the range dp->i_offset to
844 * dp->i_offset + dp->i_count in the directory.
845 * To use this space, we may have to compact the entries located
846 * there, by copying them together towards the beginning of the
847 * block, leaving the free space in one usable chunk at the end.
851 * Increase size of directory if entry eats into new space.
852 * This should never push the size past a new multiple of
855 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
857 if (dp->i_offset + dp->i_count > dp->i_size)
858 dp->i_size = dp->i_offset + dp->i_count;
860 * Get the block containing the space for the new directory entry.
862 if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf,
866 * Find space for the new entry. In the simple case, the entry at
867 * offset base will have the space. If it does not, then namei
868 * arranged that compacting the region dp->i_offset to
869 * dp->i_offset + dp->i_count would yield the
872 ep = (struct ext2fs_direct_2 *)dirbuf;
873 dsize = EXT2_DIR_REC_LEN(ep->e2d_namlen);
874 spacefree = ep->e2d_reclen - dsize;
875 for (loc = ep->e2d_reclen; loc < dp->i_count; ) {
876 nep = (struct ext2fs_direct_2 *)(dirbuf + loc);
878 /* trim the existing slot */
879 ep->e2d_reclen = dsize;
880 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
882 /* overwrite; nothing there; header is ours */
885 dsize = EXT2_DIR_REC_LEN(nep->e2d_namlen);
886 spacefree += nep->e2d_reclen - dsize;
887 loc += nep->e2d_reclen;
888 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
891 * Update the pointer fields in the previous entry (if any),
892 * copy in the new entry, and write out the block.
894 if (ep->e2d_ino == 0) {
895 if (spacefree + dsize < newentrysize)
896 panic("ext2_direnter: compact1");
897 newdir.e2d_reclen = spacefree + dsize;
899 if (spacefree < newentrysize)
900 panic("ext2_direnter: compact2");
901 newdir.e2d_reclen = spacefree;
902 ep->e2d_reclen = dsize;
903 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
905 bcopy((caddr_t)&newdir, (caddr_t)ep, (u_int)newentrysize);
906 if (DOINGASYNC(dvp)) {
912 dp->i_flag |= IN_CHANGE | IN_UPDATE;
913 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
914 error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
915 cnp->cn_cred, cnp->cn_thread);
920 * Remove a directory entry after a call to namei, using
921 * the parameters which it left in nameidata. The entry
922 * dp->i_offset contains the offset into the directory of the
923 * entry to be eliminated. The dp->i_count field contains the
924 * size of the previous record in the directory. If this
925 * is 0, the first entry is being deleted, so we need only
926 * zero the inode number to mark the entry as free. If the
927 * entry is not the first in the directory, we must reclaim
928 * the space of the now empty record by adding the record size
929 * to the size of the previous entry.
932 ext2_dirremove(struct vnode *dvp, struct componentname *cnp)
935 struct ext2fs_direct_2 *ep, *rep;
940 if (dp->i_count == 0) {
942 * First entry in block: set d_ino to zero.
945 ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep,
950 dp->i_flag |= IN_CHANGE | IN_UPDATE;
954 * Collapse new free space into previous entry.
956 if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
957 (char **)&ep, &bp)) != 0)
960 /* Set 'rep' to the entry being removed. */
961 if (dp->i_count == 0)
964 rep = (struct ext2fs_direct_2 *)((char *)ep + ep->e2d_reclen);
965 ep->e2d_reclen += rep->e2d_reclen;
966 if (DOINGASYNC(dvp) && dp->i_count != 0)
970 dp->i_flag |= IN_CHANGE | IN_UPDATE;
975 * Rewrite an existing directory entry to point at the inode
976 * supplied. The parameters describing the directory entry are
977 * set up by a call to namei.
980 ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp)
983 struct ext2fs_direct_2 *ep;
984 struct vnode *vdp = ITOV(dp);
987 if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep,
990 ep->e2d_ino = ip->i_number;
991 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
992 EXT2F_INCOMPAT_FTYPE))
993 ep->e2d_type = DTTOFT(IFTODT(ip->i_mode));
995 ep->e2d_type = EXT2_FT_UNKNOWN;
997 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1002 * Check if a directory is empty or not.
1003 * Inode supplied must be locked.
1005 * Using a struct dirtemplate here is not precisely
1006 * what we want, but better than using a struct direct.
1008 * NB: does not handle corrupted directories.
1011 ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
1014 struct dirtemplate dbuf;
1015 struct ext2fs_direct_2 *dp = (struct ext2fs_direct_2 *)&dbuf;
1018 #define MINDIRSIZ (sizeof(struct dirtemplate) / 2)
1020 for (off = 0; off < ip->i_size; off += dp->e2d_reclen) {
1021 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1022 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1023 NOCRED, &count, (struct thread *)0);
1025 * Since we read MINDIRSIZ, residual must
1026 * be 0 unless we're at end of file.
1028 if (error || count != 0)
1030 /* avoid infinite loops */
1031 if (dp->e2d_reclen == 0)
1033 /* skip empty entries */
1034 if (dp->e2d_ino == 0)
1036 /* accept only "." and ".." */
1037 namlen = dp->e2d_namlen;
1040 if (dp->e2d_name[0] != '.')
1043 * At this point namlen must be 1 or 2.
1044 * 1 implies ".", 2 implies ".." if second
1049 if (dp->e2d_name[1] == '.' && dp->e2d_ino == parentino)
1057 * Check if source directory is in the path of the target directory.
1058 * Target is supplied locked, source is unlocked.
1059 * The target is always vput before returning.
1062 ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred)
1066 struct dirtemplate dirbuf;
1069 if (target->i_number == source->i_number) {
1073 if (target->i_number == EXT2_ROOTINO) {
1079 if (vp->v_type != VDIR) {
1083 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1084 sizeof(struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1085 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL,
1089 namlen = dirbuf.dotdot_type; /* like ufs little-endian */
1091 dirbuf.dotdot_name[0] != '.' ||
1092 dirbuf.dotdot_name[1] != '.') {
1096 if (dirbuf.dotdot_ino == source->i_number) {
1100 if (dirbuf.dotdot_ino == EXT2_ROOTINO)
1103 if ((error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino,
1104 LK_EXCLUSIVE, &vp)) != 0) {
1111 if (error == ENOTDIR)
1112 printf("checkpath: .. not a directory\n");