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 <gnu/fs/ext2fs/inode.h>
59 #include <gnu/fs/ext2fs/ext2_mount.h>
60 #include <gnu/fs/ext2fs/ext2_extern.h>
61 #include <gnu/fs/ext2fs/ext2_fs.h>
62 #include <gnu/fs/ext2fs/ext2_fs_sb.h>
65 static int dirchk = 1;
67 static int dirchk = 0;
70 static SYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD, 0, "EXT2FS filesystem");
71 SYSCTL_INT(_vfs_e2fs, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
74 DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
75 while it is the native blocksize in ext2fs - thus, a #define
76 is no longer appropriate
80 static u_char ext2_ft_to_dt[] = {
81 DT_UNKNOWN, /* EXT2_FT_UNKNOWN */
82 DT_REG, /* EXT2_FT_REG_FILE */
83 DT_DIR, /* EXT2_FT_DIR */
84 DT_CHR, /* EXT2_FT_CHRDEV */
85 DT_BLK, /* EXT2_FT_BLKDEV */
86 DT_FIFO, /* EXT2_FT_FIFO */
87 DT_SOCK, /* EXT2_FT_SOCK */
88 DT_LNK, /* EXT2_FT_SYMLINK */
91 ((ft) > sizeof(ext2_ft_to_dt) / sizeof(ext2_ft_to_dt[0]) ? \
92 DT_UNKNOWN : ext2_ft_to_dt[(ft)])
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) > sizeof(dt_to_ext2_ft) / sizeof(dt_to_ext2_ft[0]) ? \
113 EXT2_FT_UNKNOWN : dt_to_ext2_ft[(dt)])
115 static int ext2_dirbadentry(struct vnode *dp, struct ext2_dir_entry_2 *de,
116 int entryoffsetinblock);
119 * Vnode op for reading directories.
121 * The routine below assumes that the on-disk format of a directory
122 * is the same as that defined by <sys/dirent.h>. If the on-disk
123 * format changes, then it will be necessary to do a conversion
124 * from the on-disk format that read returns to the format defined
128 * this is exactly what we do here - the problem is that the conversion
129 * will blow up some entries by four bytes, so it can't be done in place.
130 * This is too bad. Right now the conversion is done entry by entry, the
131 * converted entry is sent via uiomove.
133 * XXX allocate a buffer, convert as many entries as possible, then send
134 * the whole buffer to uiomove
138 struct vop_readdir_args /* {
141 struct ucred *a_cred;
144 struct uio *uio = ap->a_uio;
147 struct ext2_dir_entry_2 *edp, *dp;
153 int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->s_blocksize;
155 off_t startoffset = uio->uio_offset;
157 count = uio->uio_resid;
159 * Avoid complications for partial directory entries by adjusting
160 * the i/o to end at a block boundary. Don't give up (like ufs
161 * does) if the initial adjustment gives a negative count, since
162 * many callers don't supply a large enough buffer. The correct
163 * size is a little larger than DIRBLKSIZ to allow for expansion
164 * of directory entries, but some callers just use 512.
166 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
171 printf("ext2_readdir: uio_offset = %lld, uio_resid = %d, count = %d\n",
172 uio->uio_offset, uio->uio_resid, count);
176 auio.uio_iov = &aiov;
178 auio.uio_resid = count;
179 auio.uio_segflg = UIO_SYSSPACE;
180 aiov.iov_len = count;
181 dirbuf = malloc(count, M_TEMP, M_WAITOK);
182 aiov.iov_base = dirbuf;
183 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
185 readcnt = count - auio.uio_resid;
186 edp = (struct ext2_dir_entry_2 *)&dirbuf[readcnt];
188 bzero(&dstdp, offsetof(struct dirent, d_name));
189 for (dp = (struct ext2_dir_entry_2 *)dirbuf;
190 !error && uio->uio_resid > 0 && dp < edp; ) {
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_fileno = dp->inode;
208 dstdp.d_type = FTTODT(dp->file_type);
209 dstdp.d_namlen = dp->name_len;
210 dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
211 bcopy(dp->name, dstdp.d_name, dstdp.d_namlen);
212 bzero(dstdp.d_name + dstdp.d_namlen,
213 dstdp.d_reclen - offsetof(struct dirent, d_name) -
216 if (dp->rec_len > 0) {
217 if(dstdp.d_reclen <= uio->uio_resid) {
219 dp = (struct ext2_dir_entry_2 *)
220 ((char *)dp + dp->rec_len);
222 uiomove(&dstdp, dstdp.d_reclen, uio);
232 /* we need to correct uio_offset */
233 uio->uio_offset = startoffset + (caddr_t)dp - dirbuf;
235 if (!error && ap->a_ncookies != NULL) {
236 u_long *cookiep, *cookies, *ecookies;
239 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
240 panic("ext2_readdir: unexpected uio from NFS server");
241 cookies = malloc(ncookies * sizeof(u_long), M_TEMP,
244 for (dp = (struct ext2_dir_entry_2 *)dirbuf,
245 cookiep = cookies, ecookies = cookies + ncookies;
247 dp = (struct ext2_dir_entry_2 *)((caddr_t) dp + dp->rec_len)) {
249 *cookiep++ = (u_long) off;
251 *ap->a_ncookies = ncookies;
252 *ap->a_cookies = cookies;
255 free(dirbuf, M_TEMP);
257 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
262 * Convert a component of a pathname into a pointer to a locked inode.
263 * This is a very central and rather complicated routine.
264 * If the file system is not maintained in a strict tree hierarchy,
265 * this can result in a deadlock situation (see comments in code below).
267 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
268 * on whether the name is to be looked up, created, renamed, or deleted.
269 * When CREATE, RENAME, or DELETE is specified, information usable in
270 * creating, renaming, or deleting a directory entry may be calculated.
271 * If flag has LOCKPARENT or'ed into it and the target of the pathname
272 * exists, lookup returns both the target and its parent directory locked.
273 * When creating or renaming and LOCKPARENT is specified, the target may
274 * not be ".". When deleting and LOCKPARENT is specified, the target may
275 * be "."., but the caller must check to ensure it does an vrele and vput
276 * instead of two vputs.
278 * Overall outline of ext2_lookup:
280 * search for name in directory, to found or notfound
282 * if creating, return locked directory, leaving info on available slots
285 * if at end of path and deleting, return information to allow delete
286 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
287 * inode and return info to allow rewrite
288 * if not at end, add name to cache; if at end and neither creating
289 * nor deleting, add name to cache
293 struct vop_cachedlookup_args /* {
295 struct vnode **a_vpp;
296 struct componentname *a_cnp;
299 struct vnode *vdp; /* vnode for directory being searched */
300 struct inode *dp; /* inode for directory being searched */
301 struct buf *bp; /* a buffer of directory entries */
302 struct ext2_dir_entry_2 *ep; /* the current directory entry */
303 int entryoffsetinblock; /* offset of ep in bp's buffer */
304 enum {NONE, COMPACT, FOUND} slotstatus;
305 doff_t slotoffset; /* offset of area with free space */
306 int slotsize; /* size of area at slotoffset */
307 int slotfreespace; /* amount of space free in slot */
308 int slotneeded; /* size of the entry we're seeking */
309 int numdirpasses; /* strategy for directory search */
310 doff_t endsearch; /* offset to end directory search */
311 doff_t prevoff; /* prev entry dp->i_offset */
312 struct vnode *pdp; /* saved dp during symlink work */
313 struct vnode *tdp; /* returned by VFS_VGET */
314 doff_t enduseful; /* pointer past last used dir slot */
315 u_long bmask; /* block offset mask */
317 struct vnode **vpp = ap->a_vpp;
318 struct componentname *cnp = ap->a_cnp;
319 struct ucred *cred = cnp->cn_cred;
320 int flags = cnp->cn_flags;
321 int nameiop = cnp->cn_nameiop;
324 int DIRBLKSIZ = VTOI(ap->a_dvp)->i_e2fs->s_blocksize;
332 * We now have a segment name to search for, and a directory to search.
336 * Suppress search for slots unless creating
337 * file and at end of pathname, in which case
338 * we watch for a place to put the new file in
339 * case it doesn't already exist.
342 slotfreespace = slotsize = slotneeded = 0;
343 if ((nameiop == CREATE || nameiop == RENAME) &&
344 (flags & ISLASTCN)) {
346 slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
348 slotneeded = (sizeof(struct direct) - MAXNAMLEN +
349 cnp->cn_namelen + 3) &~ 3; */
353 * If there is cached information on a previous search of
354 * this directory, pick up where we last left off.
355 * We cache only lookups as these are the most common
356 * and have the greatest payoff. Caching CREATE has little
357 * benefit as it usually must search the entire directory
358 * to determine that the entry does not exist. Caching the
359 * location of the last DELETE or RENAME has not reduced
360 * profiling time and hence has been removed in the interest
363 bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
364 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
365 dp->i_diroff > dp->i_size) {
366 entryoffsetinblock = 0;
370 dp->i_offset = dp->i_diroff;
371 if ((entryoffsetinblock = dp->i_offset & bmask) &&
372 (error = ext2_blkatoff(vdp, (off_t)dp->i_offset, NULL,
376 nchstats.ncs_2passes++;
378 prevoff = dp->i_offset;
379 endsearch = roundup(dp->i_size, DIRBLKSIZ);
383 while (dp->i_offset < endsearch) {
385 * If necessary, get the next directory block.
387 if ((dp->i_offset & bmask) == 0) {
391 ext2_blkatoff(vdp, (off_t)dp->i_offset, NULL,
394 entryoffsetinblock = 0;
397 * If still looking for a slot, and at a DIRBLKSIZE
398 * boundary, have to start looking for free space again.
400 if (slotstatus == NONE &&
401 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
406 * Get pointer to next entry.
407 * Full validation checks are slow, so we only check
408 * enough to insure forward progress through the
409 * directory. Complete checks can be run by setting
410 * "vfs.e2fs.dirchk" to be true.
412 ep = (struct ext2_dir_entry_2 *)
413 ((char *)bp->b_data + entryoffsetinblock);
414 if (ep->rec_len == 0 ||
415 (dirchk && ext2_dirbadentry(vdp, ep, entryoffsetinblock))) {
417 ext2_dirbad(dp, dp->i_offset, "mangled entry");
418 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
420 entryoffsetinblock += i;
425 * If an appropriate sized slot has not yet been found,
426 * check to see if one is available. Also accumulate space
427 * in the current block so that we can determine if
428 * compaction is viable.
430 if (slotstatus != FOUND) {
431 int size = ep->rec_len;
434 size -= EXT2_DIR_REC_LEN(ep->name_len);
436 if (size >= slotneeded) {
438 slotoffset = dp->i_offset;
439 slotsize = ep->rec_len;
440 } else if (slotstatus == NONE) {
441 slotfreespace += size;
442 if (slotoffset == -1)
443 slotoffset = dp->i_offset;
444 if (slotfreespace >= slotneeded) {
445 slotstatus = COMPACT;
446 slotsize = dp->i_offset +
447 ep->rec_len - slotoffset;
454 * Check for a name match.
457 namlen = ep->name_len;
458 if (namlen == cnp->cn_namelen &&
459 !bcmp(cnp->cn_nameptr, ep->name,
462 * Save directory entry's inode number and
463 * reclen in ndp->ni_ufs area, and release
466 dp->i_ino = ep->inode;
467 dp->i_reclen = ep->rec_len;
471 prevoff = dp->i_offset;
472 dp->i_offset += ep->rec_len;
473 entryoffsetinblock += ep->rec_len;
475 enduseful = dp->i_offset;
479 * If we started in the middle of the directory and failed
480 * to find our target, we must check the beginning as well.
482 if (numdirpasses == 2) {
485 endsearch = dp->i_diroff;
491 * If creating, and at end of pathname and current
492 * directory has not been removed, then can consider
493 * allowing file to be created.
495 if ((nameiop == CREATE || nameiop == RENAME) &&
496 (flags & ISLASTCN) && dp->i_nlink != 0) {
498 * Access for write is interpreted as allowing
499 * creation of files in the directory.
501 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
504 * Return an indication of where the new directory
505 * entry should be put. If we didn't find a slot,
506 * then set dp->i_count to 0 indicating
507 * that the new slot belongs at the end of the
508 * directory. If we found a slot, then the new entry
509 * can be put in the range from dp->i_offset to
510 * dp->i_offset + dp->i_count.
512 if (slotstatus == NONE) {
513 dp->i_offset = roundup(dp->i_size, DIRBLKSIZ);
515 enduseful = dp->i_offset;
517 dp->i_offset = slotoffset;
518 dp->i_count = slotsize;
519 if (enduseful < slotoffset + slotsize)
520 enduseful = slotoffset + slotsize;
522 dp->i_endoff = roundup(enduseful, DIRBLKSIZ);
523 dp->i_flag |= IN_CHANGE | IN_UPDATE;
525 * We return with the directory locked, so that
526 * the parameters we set up above will still be
527 * valid if we actually decide to do a direnter().
528 * We return ni_vp == NULL to indicate that the entry
529 * does not currently exist; we leave a pointer to
530 * the (locked) directory inode in ndp->ni_dvp.
531 * The pathname buffer is saved so that the name
532 * can be obtained later.
534 * NB - if the directory is unlocked, then this
535 * information cannot be used.
537 cnp->cn_flags |= SAVENAME;
538 return (EJUSTRETURN);
541 * Insert name into cache (as non-existent) if appropriate.
543 if ((cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
544 cache_enter(vdp, *vpp, cnp);
548 if (numdirpasses == 2)
549 nchstats.ncs_pass2++;
551 * Check that directory length properly reflects presence
554 if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->name_len)
556 ext2_dirbad(dp, dp->i_offset, "i_size too small");
557 dp->i_size = entryoffsetinblock+EXT2_DIR_REC_LEN(ep->name_len);
558 dp->i_flag |= IN_CHANGE | IN_UPDATE;
563 * Found component in pathname.
564 * If the final component of path name, save information
565 * in the cache as to where the entry was found.
567 if ((flags & ISLASTCN) && nameiop == LOOKUP)
568 dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
571 * If deleting, and at end of pathname, return
572 * parameters which can be used to remove file.
574 if (nameiop == DELETE && (flags & ISLASTCN)) {
576 * Write access to directory required to delete files.
578 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
581 * Return pointer to current entry in dp->i_offset,
582 * and distance past previous entry (if there
583 * is a previous entry in this block) in dp->i_count.
584 * Save directory inode pointer in ndp->ni_dvp for dirremove().
586 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
589 dp->i_count = dp->i_offset - prevoff;
590 if (dp->i_number == dp->i_ino) {
595 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, LK_EXCLUSIVE,
599 * If directory is "sticky", then user must own
600 * the directory, or the file in it, else she
601 * may not delete it (unless she's root). This
602 * implements append-only directories.
604 if ((dp->i_mode & ISVTX) &&
606 cred->cr_uid != dp->i_uid &&
607 VTOI(tdp)->i_uid != cred->cr_uid) {
616 * If rewriting (RENAME), return the inode and the
617 * information required to rewrite the present directory
618 * Must get inode of directory entry to verify it's a
619 * regular file, or empty directory.
621 if (nameiop == RENAME && (flags & ISLASTCN)) {
622 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
625 * Careful about locking second inode.
626 * This can only occur if the target is ".".
628 if (dp->i_number == dp->i_ino)
630 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, LK_EXCLUSIVE,
634 cnp->cn_flags |= SAVENAME;
639 * Step through the translation in the name. We do not `vput' the
640 * directory because we may need it again if a symbolic link
641 * is relative to the current directory. Instead we save it
642 * unlocked as "pdp". We must get the target inode before unlocking
643 * the directory to insure that the inode will not be removed
644 * before we get it. We prevent deadlock by always fetching
645 * inodes from the root, moving down the directory tree. Thus
646 * when following backward pointers ".." we must unlock the
647 * parent directory before getting the requested directory.
648 * There is a potential race condition here if both the current
649 * and parent directories are removed before the VFS_VGET for the
650 * inode associated with ".." returns. We hope that this occurs
651 * infrequently since we cannot avoid this race condition without
652 * implementing a sophisticated deadlock detection algorithm.
653 * Note also that this simple deadlock detection scheme will not
654 * work if the file system has any hard links other than ".."
655 * that point backwards in the directory structure.
658 if (flags & ISDOTDOT) {
659 saved_ino = dp->i_ino;
660 VOP_UNLOCK(pdp, 0); /* race to get the inode */
661 error = VFS_VGET(vdp->v_mount, saved_ino, LK_EXCLUSIVE, &tdp);
662 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY);
666 } else if (dp->i_number == dp->i_ino) {
667 VREF(vdp); /* we want ourself, ie "." */
670 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, LK_EXCLUSIVE,
677 * Insert name into cache if appropriate.
679 if (cnp->cn_flags & MAKEENTRY)
680 cache_enter(vdp, *vpp, cnp);
685 ext2_dirbad(ip, offset, how)
692 mp = ITOV(ip)->v_mount;
693 (void)printf("%s: bad dir ino %lu at offset %ld: %s\n",
694 mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
695 if ((mp->mnt_flag & MNT_RDONLY) == 0)
696 panic("ext2_dirbad: bad dir");
700 * Do consistency checking on a directory entry:
701 * record length must be multiple of 4
702 * entry must fit in rest of its DIRBLKSIZ block
703 * record must be large enough to contain entry
704 * name is not longer than MAXNAMLEN
705 * name must be as long as advertised, and null terminated
708 * changed so that it confirms to ext2_check_dir_entry
711 ext2_dirbadentry(dp, de, entryoffsetinblock)
713 struct ext2_dir_entry_2 *de;
714 int entryoffsetinblock;
716 int DIRBLKSIZ = VTOI(dp)->i_e2fs->s_blocksize;
718 char * error_msg = NULL;
720 if (de->rec_len < EXT2_DIR_REC_LEN(1))
721 error_msg = "rec_len is smaller than minimal";
722 else if (de->rec_len % 4 != 0)
723 error_msg = "rec_len % 4 != 0";
724 else if (de->rec_len < EXT2_DIR_REC_LEN(de->name_len))
725 error_msg = "reclen is too small for name_len";
726 else if (entryoffsetinblock + de->rec_len > DIRBLKSIZ)
727 error_msg = "directory entry across blocks";
729 if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count)
730 error_msg = "inode out of bounds";
733 if (error_msg != NULL) {
734 printf("bad directory entry: %s\n", error_msg);
735 printf("offset=%d, inode=%lu, rec_len=%u, name_len=%u\n",
736 entryoffsetinblock, (unsigned long)de->inode,
737 de->rec_len, de->name_len);
739 return error_msg == NULL ? 0 : 1;
743 * Write a directory entry after a call to namei, using the parameters
744 * that it left in nameidata. The argument ip is the inode which the new
745 * directory entry will refer to. Dvp is a pointer to the directory to
746 * be written, which was left locked by namei. Remaining parameters
747 * (dp->i_offset, dp->i_count) indicate how the space for the new
748 * entry is to be obtained.
751 ext2_direnter(ip, dvp, cnp)
754 struct componentname *cnp;
756 struct ext2_dir_entry_2 *ep, *nep;
759 struct ext2_dir_entry_2 newdir;
763 int error, loc, newentrysize, spacefree;
765 int DIRBLKSIZ = ip->i_e2fs->s_blocksize;
769 if ((cnp->cn_flags & SAVENAME) == 0)
770 panic("direnter: missing name");
773 newdir.inode = ip->i_number;
774 newdir.name_len = cnp->cn_namelen;
775 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
776 EXT2_FEATURE_INCOMPAT_FILETYPE))
777 newdir.file_type = DTTOFT(IFTODT(ip->i_mode));
779 newdir.file_type = EXT2_FT_UNKNOWN;
780 bcopy(cnp->cn_nameptr, newdir.name, (unsigned)cnp->cn_namelen + 1);
781 newentrysize = EXT2_DIR_REC_LEN(newdir.name_len);
782 if (dp->i_count == 0) {
784 * If dp->i_count is 0, then namei could find no
785 * space in the directory. Here, dp->i_offset will
786 * be on a directory block boundary and we will write the
787 * new entry into a fresh block.
789 if (dp->i_offset & (DIRBLKSIZ - 1))
790 panic("ext2_direnter: newblk");
791 auio.uio_offset = dp->i_offset;
792 newdir.rec_len = DIRBLKSIZ;
793 auio.uio_resid = newentrysize;
794 aiov.iov_len = newentrysize;
795 aiov.iov_base = (caddr_t)&newdir;
796 auio.uio_iov = &aiov;
798 auio.uio_rw = UIO_WRITE;
799 auio.uio_segflg = UIO_SYSSPACE;
800 auio.uio_td = (struct thread *)0;
801 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
803 VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
804 /* XXX should grow with balloc() */
805 panic("ext2_direnter: frag size");
807 dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
808 dp->i_flag |= IN_CHANGE;
814 * If dp->i_count is non-zero, then namei found space
815 * for the new entry in the range dp->i_offset to
816 * dp->i_offset + dp->i_count in the directory.
817 * To use this space, we may have to compact the entries located
818 * there, by copying them together towards the beginning of the
819 * block, leaving the free space in one usable chunk at the end.
823 * Increase size of directory if entry eats into new space.
824 * This should never push the size past a new multiple of
827 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
829 if (dp->i_offset + dp->i_count > dp->i_size)
830 dp->i_size = dp->i_offset + dp->i_count;
832 * Get the block containing the space for the new directory entry.
834 if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf,
838 * Find space for the new entry. In the simple case, the entry at
839 * offset base will have the space. If it does not, then namei
840 * arranged that compacting the region dp->i_offset to
841 * dp->i_offset + dp->i_count would yield the
844 ep = (struct ext2_dir_entry_2 *)dirbuf;
845 dsize = EXT2_DIR_REC_LEN(ep->name_len);
846 spacefree = ep->rec_len - dsize;
847 for (loc = ep->rec_len; loc < dp->i_count; ) {
848 nep = (struct ext2_dir_entry_2 *)(dirbuf + loc);
850 /* trim the existing slot */
852 ep = (struct ext2_dir_entry_2 *)((char *)ep + dsize);
854 /* overwrite; nothing there; header is ours */
857 dsize = EXT2_DIR_REC_LEN(nep->name_len);
858 spacefree += nep->rec_len - dsize;
860 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
863 * Update the pointer fields in the previous entry (if any),
864 * copy in the new entry, and write out the block.
866 if (ep->inode == 0) {
867 if (spacefree + dsize < newentrysize)
868 panic("ext2_direnter: compact1");
869 newdir.rec_len = spacefree + dsize;
871 if (spacefree < newentrysize)
872 panic("ext2_direnter: compact2");
873 newdir.rec_len = spacefree;
875 ep = (struct ext2_dir_entry_2 *)((char *)ep + dsize);
877 bcopy((caddr_t)&newdir, (caddr_t)ep, (u_int)newentrysize);
879 dp->i_flag |= IN_CHANGE | IN_UPDATE;
880 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
881 error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
882 cnp->cn_cred, cnp->cn_thread);
887 * Remove a directory entry after a call to namei, using
888 * the parameters which it left in nameidata. The entry
889 * dp->i_offset contains the offset into the directory of the
890 * entry to be eliminated. The dp->i_count field contains the
891 * size of the previous record in the directory. If this
892 * is 0, the first entry is being deleted, so we need only
893 * zero the inode number to mark the entry as free. If the
894 * entry is not the first in the directory, we must reclaim
895 * the space of the now empty record by adding the record size
896 * to the size of the previous entry.
899 ext2_dirremove(dvp, cnp)
901 struct componentname *cnp;
904 struct ext2_dir_entry_2 *ep;
909 if (dp->i_count == 0) {
911 * First entry in block: set d_ino to zero.
914 ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep,
919 dp->i_flag |= IN_CHANGE | IN_UPDATE;
923 * Collapse new free space into previous entry.
925 if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
926 (char **)&ep, &bp)) != 0)
928 ep->rec_len += dp->i_reclen;
930 dp->i_flag |= IN_CHANGE | IN_UPDATE;
935 * Rewrite an existing directory entry to point at the inode
936 * supplied. The parameters describing the directory entry are
937 * set up by a call to namei.
940 ext2_dirrewrite(dp, ip, cnp)
941 struct inode *dp, *ip;
942 struct componentname *cnp;
945 struct ext2_dir_entry_2 *ep;
946 struct vnode *vdp = ITOV(dp);
949 if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep,
952 ep->inode = ip->i_number;
953 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
954 EXT2_FEATURE_INCOMPAT_FILETYPE))
955 ep->file_type = DTTOFT(IFTODT(ip->i_mode));
957 ep->file_type = EXT2_FT_UNKNOWN;
959 dp->i_flag |= IN_CHANGE | IN_UPDATE;
964 * Check if a directory is empty or not.
965 * Inode supplied must be locked.
967 * Using a struct dirtemplate here is not precisely
968 * what we want, but better than using a struct direct.
970 * NB: does not handle corrupted directories.
973 ext2_dirempty(ip, parentino, cred)
979 struct dirtemplate dbuf;
980 struct ext2_dir_entry_2 *dp = (struct ext2_dir_entry_2 *)&dbuf;
981 int error, count, namlen;
982 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
984 for (off = 0; off < ip->i_size; off += dp->rec_len) {
985 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
986 off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
987 NOCRED, &count, (struct thread *)0);
989 * Since we read MINDIRSIZ, residual must
990 * be 0 unless we're at end of file.
992 if (error || count != 0)
994 /* avoid infinite loops */
995 if (dp->rec_len == 0)
997 /* skip empty entries */
1000 /* accept only "." and ".." */
1001 namlen = dp->name_len;
1004 if (dp->name[0] != '.')
1007 * At this point namlen must be 1 or 2.
1008 * 1 implies ".", 2 implies ".." if second
1013 if (dp->name[1] == '.' && dp->inode == parentino)
1021 * Check if source directory is in the path of the target directory.
1022 * Target is supplied locked, source is unlocked.
1023 * The target is always vput before returning.
1026 ext2_checkpath(source, target, cred)
1027 struct inode *source, *target;
1031 int error, rootino, namlen;
1032 struct dirtemplate dirbuf;
1035 if (target->i_number == source->i_number) {
1041 if (target->i_number == rootino)
1045 if (vp->v_type != VDIR) {
1049 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1050 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1051 IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL,
1055 namlen = dirbuf.dotdot_type; /* like ufs little-endian */
1057 dirbuf.dotdot_name[0] != '.' ||
1058 dirbuf.dotdot_name[1] != '.') {
1062 if (dirbuf.dotdot_ino == source->i_number) {
1066 if (dirbuf.dotdot_ino == rootino)
1069 if ((error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino,
1070 LK_EXCLUSIVE, &vp)) != 0) {
1077 if (error == ENOTDIR)
1078 printf("checkpath: .. not a directory\n");