2 * modified for Lites 1.1
4 * Aug 1995, Godmar Back (gback@cs.utah.edu)
5 * University of Utah, Department of Computer Science
7 * $FreeBSD: src/sys/gnu/ext2fs/ext2_lookup.c,v 1.21.2.3 2002/11/17 02:02:42 bde Exp $
8 * $DragonFly: src/sys/vfs/gnu/ext2fs/ext2_lookup.c,v 1.10 2004/04/02 05:46:03 hmp Exp $
11 * Copyright (c) 1989, 1993
12 * The Regents of the University of California. All rights reserved.
13 * (c) UNIX System Laboratories, Inc.
14 * All or some portions of this file are derived from material licensed
15 * to the University of California by American Telephone and Telegraph
16 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
17 * the permission of UNIX System Laboratories, Inc.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. All advertising materials mentioning features or use of this software
28 * must display the following acknowledgement:
29 * This product includes software developed by the University of
30 * California, Berkeley and its contributors.
31 * 4. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * @(#)ufs_lookup.c 8.6 (Berkeley) 4/1/94
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/namei.h>
54 #include <sys/mount.h>
55 #include <sys/vnode.h>
56 #include <sys/malloc.h>
57 #include <sys/dirent.h>
59 #include <vfs/ufs/quota.h>
60 #include <vfs/ufs/inode.h>
61 #include <vfs/ufs/dir.h>
62 #include <vfs/ufs/ufsmount.h>
63 #include <vfs/ufs/ufs_extern.h>
65 #include "ext2_extern.h"
67 #include "ext2_fs_sb.h"
70 DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
71 while it is the native blocksize in ext2fs - thus, a #define
72 is no longer appropriate
78 static u_char ext2_ft_to_dt[] = {
79 DT_UNKNOWN, /* EXT2_FT_UNKNOWN */
80 DT_REG, /* EXT2_FT_REG_FILE */
81 DT_DIR, /* EXT2_FT_DIR */
82 DT_CHR, /* EXT2_FT_CHRDEV */
83 DT_BLK, /* EXT2_FT_BLKDEV */
84 DT_FIFO, /* EXT2_FT_FIFO */
85 DT_SOCK, /* EXT2_FT_SOCK */
86 DT_LNK, /* EXT2_FT_SYMLINK */
89 ((ft) > sizeof(ext2_ft_to_dt) / sizeof(ext2_ft_to_dt[0]) ? \
90 DT_UNKNOWN : ext2_ft_to_dt[(ft)])
92 static u_char dt_to_ext2_ft[] = {
93 EXT2_FT_UNKNOWN, /* DT_UNKNOWN */
94 EXT2_FT_FIFO, /* DT_FIFO */
95 EXT2_FT_CHRDEV, /* DT_CHR */
96 EXT2_FT_UNKNOWN, /* unused */
97 EXT2_FT_DIR, /* DT_DIR */
98 EXT2_FT_UNKNOWN, /* unused */
99 EXT2_FT_BLKDEV, /* DT_BLK */
100 EXT2_FT_UNKNOWN, /* unused */
101 EXT2_FT_REG_FILE, /* DT_REG */
102 EXT2_FT_UNKNOWN, /* unused */
103 EXT2_FT_SYMLINK, /* DT_LNK */
104 EXT2_FT_UNKNOWN, /* unused */
105 EXT2_FT_SOCK, /* DT_SOCK */
106 EXT2_FT_UNKNOWN, /* unused */
107 EXT2_FT_UNKNOWN, /* DT_WHT */
110 ((dt) > sizeof(dt_to_ext2_ft) / sizeof(dt_to_ext2_ft[0]) ? \
111 EXT2_FT_UNKNOWN : dt_to_ext2_ft[(dt)])
113 static int ext2_dirbadentry (struct vnode *dp,
114 struct ext2_dir_entry_2 *de,
115 int entryoffsetinblock);
118 * Vnode op for reading directories.
120 * The routine below assumes that the on-disk format of a directory
121 * is the same as that defined by <sys/dirent.h>. If the on-disk
122 * format changes, then it will be necessary to do a conversion
123 * from the on-disk format that read returns to the format defined
127 * this is exactly what we do here - the problem is that the conversion
128 * will blow up some entries by four bytes, so it can't be done in place.
129 * This is too bad. Right now the conversion is done entry by entry, the
130 * converted entry is sent via uiomove.
132 * XXX allocate a buffer, convert as many entries as possible, then send
133 * the whole buffer to uiomove
137 struct vop_readdir_args /* {
140 struct ucred *a_cred;
143 struct uio *uio = ap->a_uio;
146 struct ext2_dir_entry_2 *edp, *dp;
152 int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->s_blocksize;
154 off_t startoffset = uio->uio_offset;
156 count = uio->uio_resid;
158 * Avoid complications for partial directory entries by adjusting
159 * the i/o to end at a block boundary. Don't give up (like ufs
160 * does) if the initial adjustment gives a negative count, since
161 * many callers don't supply a large enough buffer. The correct
162 * size is a little larger than DIRBLKSIZ to allow for expansion
163 * of directory entries, but some callers just use 512.
165 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
170 printf("ext2_readdir: uio_offset = %lld, uio_resid = %d, count = %d\n",
171 uio->uio_offset, uio->uio_resid, count);
175 auio.uio_iov = &aiov;
177 auio.uio_resid = count;
178 auio.uio_segflg = UIO_SYSSPACE;
179 aiov.iov_len = count;
180 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
181 aiov.iov_base = dirbuf;
182 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
184 readcnt = count - auio.uio_resid;
185 edp = (struct ext2_dir_entry_2 *)&dirbuf[readcnt];
187 bzero(&dstdp, offsetof(struct dirent, d_name));
188 for (dp = (struct ext2_dir_entry_2 *)dirbuf;
189 !error && uio->uio_resid > 0 && dp < edp; ) {
191 * "New" ext2fs directory entries differ in 3 ways
192 * from ufs on-disk ones:
193 * - the name is not necessarily NUL-terminated.
194 * - the file type field always exists and always
195 * follows the name length field.
196 * - the file type is encoded in a different way.
198 * "Old" ext2fs directory entries need no special
199 * conversions, since they binary compatible with
200 * "new" entries having a file type of 0 (i.e.,
201 * EXT2_FT_UNKNOWN). Splitting the old name length
202 * field didn't make a mess like it did in ufs,
203 * because ext2fs uses a machine-dependent disk
206 dstdp.d_fileno = dp->inode;
207 dstdp.d_type = FTTODT(dp->file_type);
208 dstdp.d_namlen = dp->name_len;
209 dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
210 bcopy(dp->name, dstdp.d_name, dstdp.d_namlen);
211 bzero(dstdp.d_name + dstdp.d_namlen,
212 dstdp.d_reclen - offsetof(struct dirent, d_name) -
215 if (dp->rec_len > 0) {
216 if(dstdp.d_reclen <= uio->uio_resid) {
218 dp = (struct ext2_dir_entry_2 *)
219 ((char *)dp + dp->rec_len);
221 uiomove((caddr_t)&dstdp,
222 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("ext2fs_readdir: unexpected uio from NFS server");
241 MALLOC(cookies, u_long *, 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 ufs_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 */
316 int lockparent; /* 1 => lockparent flag is set */
317 int wantparent; /* 1 => wantparent or lockparent flag */
319 struct vnode **vpp = ap->a_vpp;
320 struct componentname *cnp = ap->a_cnp;
321 struct ucred *cred = cnp->cn_cred;
322 int flags = cnp->cn_flags;
323 int nameiop = cnp->cn_nameiop;
324 struct thread *td = cnp->cn_td;
325 globaldata_t gd = mycpu;
327 int DIRBLKSIZ = VTOI(ap->a_dvp)->i_e2fs->s_blocksize;
334 lockparent = flags & CNP_LOCKPARENT;
335 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
338 * We now have a segment name to search for, and a directory to search.
342 * Suppress search for slots unless creating
343 * file and at end of pathname, in which case
344 * we watch for a place to put the new file in
345 * case it doesn't already exist.
348 slotfreespace = slotsize = slotneeded = 0;
349 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) &&
350 (flags & CNP_ISLASTCN)) {
352 slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
354 slotneeded = (sizeof(struct direct) - MAXNAMLEN +
355 cnp->cn_namelen + 3) &~ 3; */
359 * If there is cached information on a previous search of
360 * this directory, pick up where we last left off.
361 * We cache only lookups as these are the most common
362 * and have the greatest payoff. Caching CREATE has little
363 * benefit as it usually must search the entire directory
364 * to determine that the entry does not exist. Caching the
365 * location of the last DELETE or RENAME has not reduced
366 * profiling time and hence has been removed in the interest
369 bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
370 if (nameiop != NAMEI_LOOKUP || dp->i_diroff == 0 ||
371 dp->i_diroff > dp->i_size) {
372 entryoffsetinblock = 0;
376 dp->i_offset = dp->i_diroff;
377 if ((entryoffsetinblock = dp->i_offset & bmask) &&
378 (error = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
381 gd->gd_nchstats.ncs_2passes++;
383 prevoff = dp->i_offset;
384 endsearch = roundup(dp->i_size, DIRBLKSIZ);
388 while (dp->i_offset < endsearch) {
390 * If necessary, get the next directory block.
392 if ((dp->i_offset & bmask) == 0) {
396 UFS_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)) != 0)
398 entryoffsetinblock = 0;
401 * If still looking for a slot, and at a DIRBLKSIZE
402 * boundary, have to start looking for free space again.
404 if (slotstatus == NONE &&
405 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
410 * Get pointer to next entry.
411 * Full validation checks are slow, so we only check
412 * enough to insure forward progress through the
413 * directory. Complete checks can be run by patching
414 * "dirchk" to be true.
416 ep = (struct ext2_dir_entry_2 *)
417 ((char *)bp->b_data + entryoffsetinblock);
418 if (ep->rec_len == 0 ||
419 (dirchk && ext2_dirbadentry(vdp, ep, entryoffsetinblock))) {
421 ufs_dirbad(dp, dp->i_offset, "mangled entry");
422 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
424 entryoffsetinblock += i;
429 * If an appropriate sized slot has not yet been found,
430 * check to see if one is available. Also accumulate space
431 * in the current block so that we can determine if
432 * compaction is viable.
434 if (slotstatus != FOUND) {
435 int size = ep->rec_len;
438 size -= EXT2_DIR_REC_LEN(ep->name_len);
440 if (size >= slotneeded) {
442 slotoffset = dp->i_offset;
443 slotsize = ep->rec_len;
444 } else if (slotstatus == NONE) {
445 slotfreespace += size;
446 if (slotoffset == -1)
447 slotoffset = dp->i_offset;
448 if (slotfreespace >= slotneeded) {
449 slotstatus = COMPACT;
450 slotsize = dp->i_offset +
451 ep->rec_len - slotoffset;
458 * Check for a name match.
461 namlen = ep->name_len;
462 if (namlen == cnp->cn_namelen &&
463 !bcmp(cnp->cn_nameptr, ep->name,
466 * Save directory entry's inode number and
467 * reclen in ndp->ni_ufs area, and release
470 dp->i_ino = ep->inode;
471 dp->i_reclen = ep->rec_len;
475 prevoff = dp->i_offset;
476 dp->i_offset += ep->rec_len;
477 entryoffsetinblock += ep->rec_len;
479 enduseful = dp->i_offset;
483 * If we started in the middle of the directory and failed
484 * to find our target, we must check the beginning as well.
486 if (numdirpasses == 2) {
489 endsearch = dp->i_diroff;
495 * If creating, and at end of pathname and current
496 * directory has not been removed, then can consider
497 * allowing file to be created.
499 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) &&
500 (flags & CNP_ISLASTCN) && dp->i_nlink != 0) {
502 * Access for write is interpreted as allowing
503 * creation of files in the directory.
505 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_td)) != 0)
508 * Return an indication of where the new directory
509 * entry should be put. If we didn't find a slot,
510 * then set dp->i_count to 0 indicating
511 * that the new slot belongs at the end of the
512 * directory. If we found a slot, then the new entry
513 * can be put in the range from dp->i_offset to
514 * dp->i_offset + dp->i_count.
516 if (slotstatus == NONE) {
517 dp->i_offset = roundup(dp->i_size, DIRBLKSIZ);
519 enduseful = dp->i_offset;
521 dp->i_offset = slotoffset;
522 dp->i_count = slotsize;
523 if (enduseful < slotoffset + slotsize)
524 enduseful = slotoffset + slotsize;
526 dp->i_endoff = roundup(enduseful, DIRBLKSIZ);
527 dp->i_flag |= IN_CHANGE | IN_UPDATE;
529 * We return with the directory locked, so that
530 * the parameters we set up above will still be
531 * valid if we actually decide to do a direnter().
532 * We return ni_vp == NULL to indicate that the entry
533 * does not currently exist; we leave a pointer to
534 * the (locked) directory inode in ndp->ni_dvp.
535 * The pathname buffer is saved so that the name
536 * can be obtained later.
538 * NB - if the directory is unlocked, then this
539 * information cannot be used.
541 cnp->cn_flags |= CNP_SAVENAME;
543 VOP_UNLOCK(vdp, NULL, 0, td);
544 return (EJUSTRETURN);
547 * Insert name into cache (as non-existent) if appropriate.
549 if ((cnp->cn_flags & CNP_MAKEENTRY) && nameiop != NAMEI_CREATE)
550 cache_enter(vdp, NCPNULL, *vpp, cnp);
554 if (numdirpasses == 2)
555 gd->gd_nchstats.ncs_pass2++;
557 * Check that directory length properly reflects presence
560 if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->name_len)
562 ufs_dirbad(dp, dp->i_offset, "i_size too small");
563 dp->i_size = entryoffsetinblock+EXT2_DIR_REC_LEN(ep->name_len);
564 dp->i_flag |= IN_CHANGE | IN_UPDATE;
569 * Found component in pathname.
570 * If the final component of path name, save information
571 * in the cache as to where the entry was found.
573 if ((flags & CNP_ISLASTCN) && nameiop == NAMEI_LOOKUP)
574 dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
577 * If deleting, and at end of pathname, return
578 * parameters which can be used to remove file.
579 * If the wantparent flag isn't set, we return only
580 * the directory (in ndp->ni_dvp), otherwise we go
581 * on and lock the inode, being careful with ".".
583 if (nameiop == NAMEI_DELETE && (flags & CNP_ISLASTCN)) {
585 * Write access to directory required to delete files.
587 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_td)) != 0)
590 * Return pointer to current entry in dp->i_offset,
591 * and distance past previous entry (if there
592 * is a previous entry in this block) in dp->i_count.
593 * Save directory inode pointer in ndp->ni_dvp for dirremove().
595 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
598 dp->i_count = dp->i_offset - prevoff;
599 if (dp->i_number == dp->i_ino) {
604 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
607 * If directory is "sticky", then user must own
608 * the directory, or the file in it, else she
609 * may not delete it (unless she's root). This
610 * implements append-only directories.
612 if ((dp->i_mode & ISVTX) &&
614 cred->cr_uid != dp->i_uid &&
615 VTOI(tdp)->i_uid != cred->cr_uid) {
621 VOP_UNLOCK(vdp, NULL, 0, td);
626 * If rewriting (RENAME), return the inode and the
627 * information required to rewrite the present directory
628 * Must get inode of directory entry to verify it's a
629 * regular file, or empty directory.
631 if (nameiop == NAMEI_RENAME && wantparent &&
632 (flags & CNP_ISLASTCN)) {
633 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_td)) != 0)
636 * Careful about locking second inode.
637 * This can only occur if the target is ".".
639 if (dp->i_number == dp->i_ino)
641 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
644 cnp->cn_flags |= CNP_SAVENAME;
646 VOP_UNLOCK(vdp, NULL, 0, td);
651 * Step through the translation in the name. We do not `vput' the
652 * directory because we may need it again if a symbolic link
653 * is relative to the current directory. Instead we save it
654 * unlocked as "pdp". We must get the target inode before unlocking
655 * the directory to insure that the inode will not be removed
656 * before we get it. We prevent deadlock by always fetching
657 * inodes from the root, moving down the directory tree. Thus
658 * when following backward pointers ".." we must unlock the
659 * parent directory before getting the requested directory.
660 * There is a potential race condition here if both the current
661 * and parent directories are removed before the VFS_VGET for the
662 * inode associated with ".." returns. We hope that this occurs
663 * infrequently since we cannot avoid this race condition without
664 * implementing a sophisticated deadlock detection algorithm.
665 * Note also that this simple deadlock detection scheme will not
666 * work if the file system has any hard links other than ".."
667 * that point backwards in the directory structure.
670 if (flags & CNP_ISDOTDOT) {
671 VOP_UNLOCK(pdp, NULL, 0, td); /* race to get the inode */
672 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0) {
673 vn_lock(pdp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
676 if (lockparent && (flags & CNP_ISLASTCN) &&
677 (error = vn_lock(pdp, NULL, LK_EXCLUSIVE, td))) {
682 } else if (dp->i_number == dp->i_ino) {
683 VREF(vdp); /* we want ourself, ie "." */
686 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
688 if (!lockparent || !(flags & CNP_ISLASTCN))
689 VOP_UNLOCK(pdp, NULL, 0, td);
694 * Insert name into cache if appropriate.
696 if (cnp->cn_flags & CNP_MAKEENTRY)
697 cache_enter(vdp, NCPNULL, *vpp, cnp);
702 * Do consistency checking on a directory entry:
703 * record length must be multiple of 4
704 * entry must fit in rest of its DIRBLKSIZ block
705 * record must be large enough to contain entry
706 * name is not longer than MAXNAMLEN
707 * name must be as long as advertised, and null terminated
710 * changed so that it confirms to ext2_check_dir_entry
713 ext2_dirbadentry(dp, de, entryoffsetinblock)
715 struct ext2_dir_entry_2 *de;
716 int entryoffsetinblock;
718 int DIRBLKSIZ = VTOI(dp)->i_e2fs->s_blocksize;
720 char * error_msg = NULL;
722 if (de->rec_len < EXT2_DIR_REC_LEN(1))
723 error_msg = "rec_len is smaller than minimal";
724 else if (de->rec_len % 4 != 0)
725 error_msg = "rec_len % 4 != 0";
726 else if (de->rec_len < EXT2_DIR_REC_LEN(de->name_len))
727 error_msg = "reclen is too small for name_len";
728 else if (entryoffsetinblock + de->rec_len > DIRBLKSIZ)
729 error_msg = "directory entry across blocks";
731 if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count)
732 error_msg = "inode out of bounds";
735 if (error_msg != NULL) {
736 printf("bad directory entry: %s\n", error_msg);
737 printf("offset=%d, inode=%lu, rec_len=%u, name_len=%u\n",
738 entryoffsetinblock, (unsigned long)de->inode,
739 de->rec_len, de->name_len);
741 return error_msg == NULL ? 0 : 1;
745 * Write a directory entry after a call to namei, using the parameters
746 * that it left in nameidata. The argument ip is the inode which the new
747 * directory entry will refer to. Dvp is a pointer to the directory to
748 * be written, which was left locked by namei. Remaining parameters
749 * (dp->i_offset, dp->i_count) indicate how the space for the new
750 * entry is to be obtained.
753 ext2_direnter(ip, dvp, cnp)
756 struct componentname *cnp;
758 struct ext2_dir_entry_2 *ep, *nep;
761 struct ext2_dir_entry_2 newdir;
765 int error, loc, newentrysize, spacefree;
767 int DIRBLKSIZ = ip->i_e2fs->s_blocksize;
771 if ((cnp->cn_flags & CNP_SAVENAME) == 0)
772 panic("direnter: missing name");
775 newdir.inode = ip->i_number;
776 newdir.name_len = cnp->cn_namelen;
777 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
778 EXT2_FEATURE_INCOMPAT_FILETYPE))
779 newdir.file_type = DTTOFT(IFTODT(ip->i_mode));
781 newdir.file_type = EXT2_FT_UNKNOWN;
782 bcopy(cnp->cn_nameptr, newdir.name, (unsigned)cnp->cn_namelen + 1);
783 newentrysize = EXT2_DIR_REC_LEN(newdir.name_len);
784 if (dp->i_count == 0) {
786 * If dp->i_count is 0, then namei could find no
787 * space in the directory. Here, dp->i_offset will
788 * be on a directory block boundary and we will write the
789 * new entry into a fresh block.
791 if (dp->i_offset & (DIRBLKSIZ - 1))
792 panic("ext2_direnter: newblk");
793 auio.uio_offset = dp->i_offset;
794 newdir.rec_len = DIRBLKSIZ;
795 auio.uio_resid = newentrysize;
796 aiov.iov_len = newentrysize;
797 aiov.iov_base = (caddr_t)&newdir;
798 auio.uio_iov = &aiov;
800 auio.uio_rw = UIO_WRITE;
801 auio.uio_segflg = UIO_SYSSPACE;
803 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
805 VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
806 /* XXX should grow with balloc() */
807 panic("ext2_direnter: frag size");
809 dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
810 dp->i_flag |= IN_CHANGE;
816 * If dp->i_count is non-zero, then namei found space
817 * for the new entry in the range dp->i_offset to
818 * dp->i_offset + dp->i_count in the directory.
819 * To use this space, we may have to compact the entries located
820 * there, by copying them together towards the beginning of the
821 * block, leaving the free space in one usable chunk at the end.
825 * Increase size of directory if entry eats into new space.
826 * This should never push the size past a new multiple of
829 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
831 if (dp->i_offset + dp->i_count > dp->i_size)
832 dp->i_size = dp->i_offset + dp->i_count;
834 * Get the block containing the space for the new directory entry.
836 if ((error = UFS_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp)) != 0)
839 * Find space for the new entry. In the simple case, the entry at
840 * offset base will have the space. If it does not, then namei
841 * arranged that compacting the region dp->i_offset to
842 * dp->i_offset + dp->i_count would yield the
845 ep = (struct ext2_dir_entry_2 *)dirbuf;
846 dsize = EXT2_DIR_REC_LEN(ep->name_len);
847 spacefree = ep->rec_len - dsize;
848 for (loc = ep->rec_len; loc < dp->i_count; ) {
849 nep = (struct ext2_dir_entry_2 *)(dirbuf + loc);
851 /* trim the existing slot */
853 ep = (struct ext2_dir_entry_2 *)((char *)ep + dsize);
855 /* overwrite; nothing there; header is ours */
858 dsize = EXT2_DIR_REC_LEN(nep->name_len);
859 spacefree += nep->rec_len - dsize;
861 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
864 * Update the pointer fields in the previous entry (if any),
865 * copy in the new entry, and write out the block.
867 if (ep->inode == 0) {
868 if (spacefree + dsize < newentrysize)
869 panic("ext2_direnter: compact1");
870 newdir.rec_len = spacefree + dsize;
872 if (spacefree < newentrysize)
873 panic("ext2_direnter: compact2");
874 newdir.rec_len = spacefree;
876 ep = (struct ext2_dir_entry_2 *)((char *)ep + dsize);
878 bcopy((caddr_t)&newdir, (caddr_t)ep, (u_int)newentrysize);
879 error = VOP_BWRITE(bp->b_vp, bp);
880 dp->i_flag |= IN_CHANGE | IN_UPDATE;
881 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
882 error = UFS_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC,
883 cnp->cn_cred, cnp->cn_td);
888 * Remove a directory entry after a call to namei, using
889 * the parameters which it left in nameidata. The entry
890 * dp->i_offset contains the offset into the directory of the
891 * entry to be eliminated. The dp->i_count field contains the
892 * size of the previous record in the directory. If this
893 * is 0, the first entry is being deleted, so we need only
894 * zero the inode number to mark the entry as free. If the
895 * entry is not the first in the directory, we must reclaim
896 * the space of the now empty record by adding the record size
897 * to the size of the previous entry.
900 ext2_dirremove(dvp, cnp)
902 struct componentname *cnp;
905 struct ext2_dir_entry_2 *ep;
910 if (dp->i_count == 0) {
912 * First entry in block: set d_ino to zero.
915 UFS_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) != 0)
918 error = VOP_BWRITE(bp->b_vp, bp);
919 dp->i_flag |= IN_CHANGE | IN_UPDATE;
923 * Collapse new free space into previous entry.
925 if ((error = UFS_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count),
926 (char **)&ep, &bp)) != 0)
928 ep->rec_len += dp->i_reclen;
929 error = VOP_BWRITE(bp->b_vp, bp);
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 = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp)) != 0)
951 ep->inode = ip->i_number;
952 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
953 EXT2_FEATURE_INCOMPAT_FILETYPE))
954 ep->file_type = DTTOFT(IFTODT(ip->i_mode));
956 ep->file_type = EXT2_FT_UNKNOWN;
957 error = VOP_BWRITE(bp->b_vp, bp);
958 dp->i_flag |= IN_CHANGE | IN_UPDATE;
963 * Check if a directory is empty or not.
964 * Inode supplied must be locked.
966 * Using a struct dirtemplate here is not precisely
967 * what we want, but better than using a struct direct.
969 * NB: does not handle corrupted directories.
972 ext2_dirempty(ip, parentino, cred)
978 struct dirtemplate dbuf;
979 struct ext2_dir_entry_2 *dp = (struct ext2_dir_entry_2 *)&dbuf;
980 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, off,
986 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, NULL);
988 * Since we read MINDIRSIZ, residual must
989 * be 0 unless we're at end of file.
991 if (error || count != 0)
993 /* avoid infinite loops */
994 if (dp->rec_len == 0)
996 /* skip empty entries */
999 /* accept only "." and ".." */
1000 namlen = dp->name_len;
1003 if (dp->name[0] != '.')
1006 * At this point namlen must be 1 or 2.
1007 * 1 implies ".", 2 implies ".." if second
1012 if (dp->name[1] == '.' && dp->inode == parentino)
1020 * Check if source directory is in the path of the target directory.
1021 * Target is supplied locked, source is unlocked.
1022 * The target is always vput before returning.
1025 ext2_checkpath(source, target, cred)
1026 struct inode *source, *target;
1030 int error, rootino, namlen;
1031 struct dirtemplate dirbuf;
1034 if (target->i_number == source->i_number) {
1040 if (target->i_number == rootino)
1044 if (vp->v_type != VDIR) {
1048 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1049 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1050 IO_NODELOCKED, cred, (int *)0, NULL);
1053 namlen = dirbuf.dotdot_type; /* like ufs little-endian */
1055 dirbuf.dotdot_name[0] != '.' ||
1056 dirbuf.dotdot_name[1] != '.') {
1060 if (dirbuf.dotdot_ino == source->i_number) {
1064 if (dirbuf.dotdot_ino == rootino)
1067 if ((error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino, &vp)) != 0) {
1074 if (error == ENOTDIR)
1075 printf("checkpath: .. not a directory\n");