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.19 2006/04/04 17:34:32 dillon 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>
62 #include "ext2mount.h"
63 #include "ext2_extern.h"
65 #include "ext2_fs_sb.h"
68 DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
69 while it is the native blocksize in ext2fs - thus, a #define
70 is no longer appropriate
76 static u_char ext2_ft_to_dt[] = {
77 DT_UNKNOWN, /* EXT2_FT_UNKNOWN */
78 DT_REG, /* EXT2_FT_REG_FILE */
79 DT_DIR, /* EXT2_FT_DIR */
80 DT_CHR, /* EXT2_FT_CHRDEV */
81 DT_BLK, /* EXT2_FT_BLKDEV */
82 DT_FIFO, /* EXT2_FT_FIFO */
83 DT_SOCK, /* EXT2_FT_SOCK */
84 DT_LNK, /* EXT2_FT_SYMLINK */
87 ((ft) > sizeof(ext2_ft_to_dt) / sizeof(ext2_ft_to_dt[0]) ? \
88 DT_UNKNOWN : ext2_ft_to_dt[(ft)])
90 static u_char dt_to_ext2_ft[] = {
91 EXT2_FT_UNKNOWN, /* DT_UNKNOWN */
92 EXT2_FT_FIFO, /* DT_FIFO */
93 EXT2_FT_CHRDEV, /* DT_CHR */
94 EXT2_FT_UNKNOWN, /* unused */
95 EXT2_FT_DIR, /* DT_DIR */
96 EXT2_FT_UNKNOWN, /* unused */
97 EXT2_FT_BLKDEV, /* DT_BLK */
98 EXT2_FT_UNKNOWN, /* unused */
99 EXT2_FT_REG_FILE, /* DT_REG */
100 EXT2_FT_UNKNOWN, /* unused */
101 EXT2_FT_SYMLINK, /* DT_LNK */
102 EXT2_FT_UNKNOWN, /* unused */
103 EXT2_FT_SOCK, /* DT_SOCK */
104 EXT2_FT_UNKNOWN, /* unused */
105 EXT2_FT_UNKNOWN, /* DT_WHT */
108 ((dt) > sizeof(dt_to_ext2_ft) / sizeof(dt_to_ext2_ft[0]) ? \
109 EXT2_FT_UNKNOWN : dt_to_ext2_ft[(dt)])
111 static int ext2_dirbadentry (struct vnode *dp,
112 struct ext2_dir_entry_2 *de,
113 int entryoffsetinblock);
116 * Vnode op for reading directories.
118 * The routine below assumes that the on-disk format of a directory
119 * is the same as that defined by <sys/dirent.h>. If the on-disk
120 * format changes, then it will be necessary to do a conversion
121 * from the on-disk format that read returns to the format defined
125 * this is exactly what we do here - the problem is that the conversion
126 * will blow up some entries by four bytes, so it can't be done in place.
127 * This is too bad. Right now the conversion is done entry by entry, the
128 * converted entry is sent via uiomove.
130 * XXX allocate a buffer, convert as many entries as possible, then send
131 * the whole buffer to uiomove
133 * ext2_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
136 ext2_readdir(struct vop_readdir_args *ap)
138 struct uio *uio = ap->a_uio;
141 struct ext2_dir_entry_2 *edp, *dp;
146 int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->s_blocksize;
148 off_t startoffset = uio->uio_offset;
150 count = uio->uio_resid;
152 * Avoid complications for partial directory entries by adjusting
153 * the i/o to end at a block boundary. Don't give up (like ufs
154 * does) if the initial adjustment gives a negative count, since
155 * many callers don't supply a large enough buffer. The correct
156 * size is a little larger than DIRBLKSIZ to allow for expansion
157 * of directory entries, but some callers just use 512.
159 count -= (uio->uio_offset + count) & (DIRBLKSIZ -1);
164 printf("ext2_readdir: uio_offset = %lld, uio_resid = %d, count = %d\n",
165 uio->uio_offset, uio->uio_resid, count);
169 auio.uio_iov = &aiov;
171 auio.uio_resid = count;
172 auio.uio_segflg = UIO_SYSSPACE;
173 aiov.iov_len = count;
174 MALLOC(dirbuf, caddr_t, count, M_TEMP, M_WAITOK);
175 aiov.iov_base = dirbuf;
176 error = VOP_READ(ap->a_vp, &auio, 0, ap->a_cred);
178 readcnt = count - auio.uio_resid;
179 edp = (struct ext2_dir_entry_2 *)&dirbuf[readcnt];
181 for (dp = (struct ext2_dir_entry_2 *)dirbuf;
182 !error && uio->uio_resid > 0 && dp < edp; ) {
184 * "New" ext2fs directory entries differ in 3 ways
185 * from ufs on-disk ones:
186 * - the name is not necessarily NUL-terminated.
187 * - the file type field always exists and always
188 * follows the name length field.
189 * - the file type is encoded in a different way.
191 * "Old" ext2fs directory entries need no special
192 * conversions, since they binary compatible with
193 * "new" entries having a file type of 0 (i.e.,
194 * EXT2_FT_UNKNOWN). Splitting the old name length
195 * field didn't make a mess like it did in ufs,
196 * because ext2fs uses a machine-dependent disk
199 if (dp->rec_len <= 0) {
203 retval = vop_write_dirent(&error, uio, dp->inode,
204 FTTODT(dp->file_type), dp->name_len, dp->name);
209 dp = (struct ext2_dir_entry_2 *)((char *)dp + dp->rec_len);
213 /* we need to correct uio_offset */
214 uio->uio_offset = startoffset + (caddr_t)dp - dirbuf;
216 if (!error && ap->a_ncookies != NULL) {
217 u_long *cookiep, *cookies, *ecookies;
220 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
221 panic("ext2fs_readdir: unexpected uio from NFS server");
222 MALLOC(cookies, u_long *, ncookies * sizeof(u_long), M_TEMP,
225 for (dp = (struct ext2_dir_entry_2 *)dirbuf,
226 cookiep = cookies, ecookies = cookies + ncookies;
228 dp = (struct ext2_dir_entry_2 *)((caddr_t) dp + dp->rec_len)) {
230 *cookiep++ = (u_long) off;
232 *ap->a_ncookies = ncookies;
233 *ap->a_cookies = cookies;
236 FREE(dirbuf, M_TEMP);
238 *ap->a_eofflag = VTOI(ap->a_vp)->i_size <= uio->uio_offset;
243 * Convert a component of a pathname into a pointer to a locked inode.
244 * This is a very central and rather complicated routine.
245 * If the file system is not maintained in a strict tree hierarchy,
246 * this can result in a deadlock situation (see comments in code below).
248 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
249 * on whether the name is to be looked up, created, renamed, or deleted.
250 * When CREATE, RENAME, or DELETE is specified, information usable in
251 * creating, renaming, or deleting a directory entry may be calculated.
252 * If flag has LOCKPARENT or'ed into it and the target of the pathname
253 * exists, lookup returns both the target and its parent directory locked.
254 * When creating or renaming and LOCKPARENT is specified, the target may
255 * not be ".". When deleting and LOCKPARENT is specified, the target may
256 * be "."., but the caller must check to ensure it does an vrele and vput
257 * instead of two vputs.
259 * Overall outline of ufs_lookup:
261 * search for name in directory, to found or notfound
263 * if creating, return locked directory, leaving info on available slots
266 * if at end of path and deleting, return information to allow delete
267 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
268 * inode and return info to allow rewrite
269 * if not at end, add name to cache; if at end and neither creating
270 * nor deleting, add name to cache
272 * ext2_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
273 * struct componentname *a_cnp)
276 ext2_lookup(struct vop_old_lookup_args *ap)
278 struct vnode *vdp; /* vnode for directory being searched */
279 struct inode *dp; /* inode for directory being searched */
280 struct buf *bp; /* a buffer of directory entries */
281 struct ext2_dir_entry_2 *ep; /* the current directory entry */
282 int entryoffsetinblock; /* offset of ep in bp's buffer */
283 enum {NONE, COMPACT, FOUND} slotstatus;
284 doff_t slotoffset; /* offset of area with free space */
285 int slotsize; /* size of area at slotoffset */
286 int slotfreespace; /* amount of space free in slot */
287 int slotneeded; /* size of the entry we're seeking */
288 int numdirpasses; /* strategy for directory search */
289 doff_t endsearch; /* offset to end directory search */
290 doff_t prevoff; /* prev entry dp->i_offset */
291 struct vnode *pdp; /* saved dp during symlink work */
292 struct vnode *tdp; /* returned by VFS_VGET */
293 doff_t enduseful; /* pointer past last used dir slot */
294 u_long bmask; /* block offset mask */
295 int lockparent; /* 1 => lockparent flag is set */
296 int wantparent; /* 1 => wantparent or lockparent flag */
298 struct vnode **vpp = ap->a_vpp;
299 struct componentname *cnp = ap->a_cnp;
300 struct ucred *cred = cnp->cn_cred;
301 int flags = cnp->cn_flags;
302 int nameiop = cnp->cn_nameiop;
303 struct thread *td = cnp->cn_td;
304 globaldata_t gd = mycpu;
306 int DIRBLKSIZ = VTOI(ap->a_dvp)->i_e2fs->s_blocksize;
313 lockparent = flags & CNP_LOCKPARENT;
314 wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
317 * We now have a segment name to search for, and a directory to search.
321 * Suppress search for slots unless creating
322 * file and at end of pathname, in which case
323 * we watch for a place to put the new file in
324 * case it doesn't already exist.
327 slotfreespace = slotsize = slotneeded = 0;
328 if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) {
330 slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
332 slotneeded = (sizeof(struct direct) - MAXNAMLEN +
333 cnp->cn_namelen + 3) &~ 3; */
337 * If there is cached information on a previous search of
338 * this directory, pick up where we last left off.
339 * We cache only lookups as these are the most common
340 * and have the greatest payoff. Caching CREATE has little
341 * benefit as it usually must search the entire directory
342 * to determine that the entry does not exist. Caching the
343 * location of the last DELETE or RENAME has not reduced
344 * profiling time and hence has been removed in the interest
347 bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
348 if (nameiop != NAMEI_LOOKUP || dp->i_diroff == 0 ||
349 dp->i_diroff > dp->i_size) {
350 entryoffsetinblock = 0;
354 dp->i_offset = dp->i_diroff;
355 if ((entryoffsetinblock = dp->i_offset & bmask) &&
356 (error = EXT2_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
359 gd->gd_nchstats->ncs_2passes++;
361 prevoff = dp->i_offset;
362 endsearch = roundup(dp->i_size, DIRBLKSIZ);
366 while (dp->i_offset < endsearch) {
368 * If necessary, get the next directory block.
370 if ((dp->i_offset & bmask) == 0) {
374 EXT2_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)) != 0)
376 entryoffsetinblock = 0;
379 * If still looking for a slot, and at a DIRBLKSIZE
380 * boundary, have to start looking for free space again.
382 if (slotstatus == NONE &&
383 (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
388 * Get pointer to next entry.
389 * Full validation checks are slow, so we only check
390 * enough to insure forward progress through the
391 * directory. Complete checks can be run by patching
392 * "dirchk" to be true.
394 ep = (struct ext2_dir_entry_2 *)
395 ((char *)bp->b_data + entryoffsetinblock);
396 if (ep->rec_len == 0 ||
397 (dirchk && ext2_dirbadentry(vdp, ep, entryoffsetinblock))) {
399 ext2_dirbad(dp, dp->i_offset, "mangled entry");
400 i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
402 entryoffsetinblock += i;
407 * If an appropriate sized slot has not yet been found,
408 * check to see if one is available. Also accumulate space
409 * in the current block so that we can determine if
410 * compaction is viable.
412 if (slotstatus != FOUND) {
413 int size = ep->rec_len;
416 size -= EXT2_DIR_REC_LEN(ep->name_len);
418 if (size >= slotneeded) {
420 slotoffset = dp->i_offset;
421 slotsize = ep->rec_len;
422 } else if (slotstatus == NONE) {
423 slotfreespace += size;
424 if (slotoffset == -1)
425 slotoffset = dp->i_offset;
426 if (slotfreespace >= slotneeded) {
427 slotstatus = COMPACT;
428 slotsize = dp->i_offset +
429 ep->rec_len - slotoffset;
436 * Check for a name match.
439 namlen = ep->name_len;
440 if (namlen == cnp->cn_namelen &&
441 !bcmp(cnp->cn_nameptr, ep->name,
444 * Save directory entry's inode number and
445 * reclen in ndp->ni_ufs area, and release
448 dp->i_ino = ep->inode;
449 dp->i_reclen = ep->rec_len;
453 prevoff = dp->i_offset;
454 dp->i_offset += ep->rec_len;
455 entryoffsetinblock += ep->rec_len;
457 enduseful = dp->i_offset;
461 * If we started in the middle of the directory and failed
462 * to find our target, we must check the beginning as well.
464 if (numdirpasses == 2) {
467 endsearch = dp->i_diroff;
473 * If creating, and at end of pathname and current
474 * directory has not been removed, then can consider
475 * allowing file to be created.
477 if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) &&
480 * Access for write is interpreted as allowing
481 * creation of files in the directory.
483 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_td)) != 0)
486 * Return an indication of where the new directory
487 * entry should be put. If we didn't find a slot,
488 * then set dp->i_count to 0 indicating
489 * that the new slot belongs at the end of the
490 * directory. If we found a slot, then the new entry
491 * can be put in the range from dp->i_offset to
492 * dp->i_offset + dp->i_count.
494 if (slotstatus == NONE) {
495 dp->i_offset = roundup(dp->i_size, DIRBLKSIZ);
497 enduseful = dp->i_offset;
499 dp->i_offset = slotoffset;
500 dp->i_count = slotsize;
501 if (enduseful < slotoffset + slotsize)
502 enduseful = slotoffset + slotsize;
504 dp->i_endoff = roundup(enduseful, DIRBLKSIZ);
505 dp->i_flag |= IN_CHANGE | IN_UPDATE;
507 * We return with the directory locked, so that
508 * the parameters we set up above will still be
509 * valid if we actually decide to do a direnter().
510 * We return ni_vp == NULL to indicate that the entry
511 * does not currently exist; we leave a pointer to
512 * the (locked) directory inode in ndp->ni_dvp.
513 * The pathname buffer is saved so that the name
514 * can be obtained later.
516 * NB - if the directory is unlocked, then this
517 * information cannot be used.
520 VOP_UNLOCK(vdp, 0, td);
521 return (EJUSTRETURN);
526 if (numdirpasses == 2)
527 gd->gd_nchstats->ncs_pass2++;
529 * Check that directory length properly reflects presence
532 if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->name_len)
534 ext2_dirbad(dp, dp->i_offset, "i_size too small");
535 dp->i_size = entryoffsetinblock+EXT2_DIR_REC_LEN(ep->name_len);
536 dp->i_flag |= IN_CHANGE | IN_UPDATE;
541 * Found component in pathname.
542 * If the final component of path name, save information
543 * in the cache as to where the entry was found.
545 if (nameiop == NAMEI_LOOKUP)
546 dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
549 * If deleting, and at end of pathname, return
550 * parameters which can be used to remove file.
551 * If the wantparent flag isn't set, we return only
552 * the directory (in ndp->ni_dvp), otherwise we go
553 * on and lock the inode, being careful with ".".
555 if (nameiop == NAMEI_DELETE) {
557 * Write access to directory required to delete files.
559 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_td)) != 0)
562 * Return pointer to current entry in dp->i_offset,
563 * and distance past previous entry (if there
564 * is a previous entry in this block) in dp->i_count.
565 * Save directory inode pointer in ndp->ni_dvp for dirremove().
567 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
570 dp->i_count = dp->i_offset - prevoff;
571 if (dp->i_number == dp->i_ino) {
576 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
579 * If directory is "sticky", then user must own
580 * the directory, or the file in it, else she
581 * may not delete it (unless she's root). This
582 * implements append-only directories.
584 if ((dp->i_mode & ISVTX) &&
586 cred->cr_uid != dp->i_uid &&
587 VTOI(tdp)->i_uid != cred->cr_uid) {
593 VOP_UNLOCK(vdp, 0, td);
598 * If rewriting (RENAME), return the inode and the
599 * information required to rewrite the present directory
600 * Must get inode of directory entry to verify it's a
601 * regular file, or empty directory.
603 if (nameiop == NAMEI_RENAME && wantparent) {
604 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_td)) != 0)
607 * Careful about locking second inode.
608 * This can only occur if the target is ".".
610 if (dp->i_number == dp->i_ino)
612 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
616 VOP_UNLOCK(vdp, 0, td);
621 * Step through the translation in the name. We do not `vput' the
622 * directory because we may need it again if a symbolic link
623 * is relative to the current directory. Instead we save it
624 * unlocked as "pdp". We must get the target inode before unlocking
625 * the directory to insure that the inode will not be removed
626 * before we get it. We prevent deadlock by always fetching
627 * inodes from the root, moving down the directory tree. Thus
628 * when following backward pointers ".." we must unlock the
629 * parent directory before getting the requested directory.
630 * There is a potential race condition here if both the current
631 * and parent directories are removed before the VFS_VGET for the
632 * inode associated with ".." returns. We hope that this occurs
633 * infrequently since we cannot avoid this race condition without
634 * implementing a sophisticated deadlock detection algorithm.
635 * Note also that this simple deadlock detection scheme will not
636 * work if the file system has any hard links other than ".."
637 * that point backwards in the directory structure.
640 if (flags & CNP_ISDOTDOT) {
641 VOP_UNLOCK(pdp, 0, td); /* race to get the inode */
642 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0) {
643 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, td);
646 if (lockparent && (error = vn_lock(pdp, LK_EXCLUSIVE, td))) {
651 } else if (dp->i_number == dp->i_ino) {
652 vref(vdp); /* we want ourself, ie "." */
655 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0)
658 VOP_UNLOCK(pdp, 0, td);
665 ext2_dirbad(struct inode *ip, doff_t offset, char *how)
669 mp = ITOV(ip)->v_mount;
670 printf("%s: bad dir ino %lu at offset %ld: %s\n",
671 mp->mnt_stat.f_mntfromname, (u_long)ip->i_number,
673 if ((mp->mnt_flag & MNT_RDONLY) == 0)
674 panic("ufs_dirbad: bad dir");
678 * Do consistency checking on a directory entry:
679 * record length must be multiple of 4
680 * entry must fit in rest of its DIRBLKSIZ block
681 * record must be large enough to contain entry
682 * name is not longer than MAXNAMLEN
683 * name must be as long as advertised, and null terminated
686 * changed so that it confirms to ext2_check_dir_entry
689 ext2_dirbadentry(struct vnode *dp, struct ext2_dir_entry_2 *de,
690 int entryoffsetinblock)
692 int DIRBLKSIZ = VTOI(dp)->i_e2fs->s_blocksize;
694 char * error_msg = NULL;
696 if (de->rec_len < EXT2_DIR_REC_LEN(1))
697 error_msg = "rec_len is smaller than minimal";
698 else if (de->rec_len % 4 != 0)
699 error_msg = "rec_len % 4 != 0";
700 else if (de->rec_len < EXT2_DIR_REC_LEN(de->name_len))
701 error_msg = "reclen is too small for name_len";
702 else if (entryoffsetinblock + de->rec_len > DIRBLKSIZ)
703 error_msg = "directory entry across blocks";
705 if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count)
706 error_msg = "inode out of bounds";
709 if (error_msg != NULL) {
710 printf("bad directory entry: %s\n", error_msg);
711 printf("offset=%d, inode=%lu, rec_len=%u, name_len=%u\n",
712 entryoffsetinblock, (unsigned long)de->inode,
713 de->rec_len, de->name_len);
715 return error_msg == NULL ? 0 : 1;
719 * Write a directory entry after a call to namei, using the parameters
720 * that it left in the directory inode. The argument ip is the inode which
721 * the new directory entry will refer to. Dvp is a pointer to the directory
722 * to be written, which was left locked by namei. Remaining parameters
723 * (dp->i_offset, dp->i_count) indicate how the space for the new
724 * entry is to be obtained.
727 ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp)
729 struct ext2_dir_entry_2 *ep, *nep;
732 struct ext2_dir_entry_2 newdir;
736 int error, loc, newentrysize, spacefree;
738 int DIRBLKSIZ = ip->i_e2fs->s_blocksize;
742 newdir.inode = ip->i_number;
743 newdir.name_len = cnp->cn_namelen;
744 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
745 EXT2_FEATURE_INCOMPAT_FILETYPE))
746 newdir.file_type = DTTOFT(IFTODT(ip->i_mode));
748 newdir.file_type = EXT2_FT_UNKNOWN;
749 bcopy(cnp->cn_nameptr, newdir.name, (unsigned)cnp->cn_namelen + 1);
750 newentrysize = EXT2_DIR_REC_LEN(newdir.name_len);
751 if (dp->i_count == 0) {
753 * If dp->i_count is 0, then namei could find no
754 * space in the directory. Here, dp->i_offset will
755 * be on a directory block boundary and we will write the
756 * new entry into a fresh block.
758 if (dp->i_offset & (DIRBLKSIZ - 1))
759 panic("ext2_direnter: newblk");
760 auio.uio_offset = dp->i_offset;
761 newdir.rec_len = DIRBLKSIZ;
762 auio.uio_resid = newentrysize;
763 aiov.iov_len = newentrysize;
764 aiov.iov_base = (caddr_t)&newdir;
765 auio.uio_iov = &aiov;
767 auio.uio_rw = UIO_WRITE;
768 auio.uio_segflg = UIO_SYSSPACE;
770 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
772 VFSTOEXT2(dvp->v_mount)->um_mountp->mnt_stat.f_bsize)
773 /* XXX should grow with balloc() */
774 panic("ext2_direnter: frag size");
776 dp->i_size = roundup(dp->i_size, DIRBLKSIZ);
777 dp->i_flag |= IN_CHANGE;
783 * If dp->i_count is non-zero, then namei found space
784 * for the new entry in the range dp->i_offset to
785 * dp->i_offset + dp->i_count in the directory.
786 * To use this space, we may have to compact the entries located
787 * there, by copying them together towards the beginning of the
788 * block, leaving the free space in one usable chunk at the end.
792 * Increase size of directory if entry eats into new space.
793 * This should never push the size past a new multiple of
796 * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
798 if (dp->i_offset + dp->i_count > dp->i_size)
799 dp->i_size = dp->i_offset + dp->i_count;
801 * Get the block containing the space for the new directory entry.
803 if ((error = EXT2_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp)) != 0)
806 * Find space for the new entry. In the simple case, the entry at
807 * offset base will have the space. If it does not, then namei
808 * arranged that compacting the region dp->i_offset to
809 * dp->i_offset + dp->i_count would yield the
812 ep = (struct ext2_dir_entry_2 *)dirbuf;
813 dsize = EXT2_DIR_REC_LEN(ep->name_len);
814 spacefree = ep->rec_len - dsize;
815 for (loc = ep->rec_len; loc < dp->i_count; ) {
816 nep = (struct ext2_dir_entry_2 *)(dirbuf + loc);
818 /* trim the existing slot */
820 ep = (struct ext2_dir_entry_2 *)((char *)ep + dsize);
822 /* overwrite; nothing there; header is ours */
825 dsize = EXT2_DIR_REC_LEN(nep->name_len);
826 spacefree += nep->rec_len - dsize;
828 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
831 * Update the pointer fields in the previous entry (if any),
832 * copy in the new entry, and write out the block.
834 if (ep->inode == 0) {
835 if (spacefree + dsize < newentrysize)
836 panic("ext2_direnter: compact1");
837 newdir.rec_len = spacefree + dsize;
839 if (spacefree < newentrysize)
840 panic("ext2_direnter: compact2");
841 newdir.rec_len = spacefree;
843 ep = (struct ext2_dir_entry_2 *)((char *)ep + dsize);
845 bcopy((caddr_t)&newdir, (caddr_t)ep, (u_int)newentrysize);
846 error = VOP_BWRITE(bp->b_vp, bp);
847 dp->i_flag |= IN_CHANGE | IN_UPDATE;
848 if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
849 error = EXT2_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC,
850 cnp->cn_cred, cnp->cn_td);
855 * Remove a directory entry after a call to namei, using
856 * the parameters which it left in the directory inode. The entry
857 * dp->i_offset contains the offset into the directory of the
858 * entry to be eliminated. The dp->i_count field contains the
859 * size of the previous record in the directory. If this
860 * is 0, the first entry is being deleted, so we need only
861 * zero the inode number to mark the entry as free. If the
862 * entry is not the first in the directory, we must reclaim
863 * the space of the now empty record by adding the record size
864 * to the size of the previous entry.
867 ext2_dirremove(struct vnode *dvp, struct componentname *cnp)
870 struct ext2_dir_entry_2 *ep;
875 if (dp->i_count == 0) {
877 * First entry in block: set d_ino to zero.
880 EXT2_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) != 0)
883 error = VOP_BWRITE(bp->b_vp, bp);
884 dp->i_flag |= IN_CHANGE | IN_UPDATE;
888 * Collapse new free space into previous entry.
890 if ((error = EXT2_BLKATOFF(dvp, (off_t)(dp->i_offset - dp->i_count),
891 (char **)&ep, &bp)) != 0)
893 ep->rec_len += dp->i_reclen;
894 error = VOP_BWRITE(bp->b_vp, bp);
895 dp->i_flag |= IN_CHANGE | IN_UPDATE;
900 * Rewrite an existing directory entry to point at the inode
901 * supplied. The parameters describing the directory entry are
902 * set up by a call to namei.
905 ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp)
908 struct ext2_dir_entry_2 *ep;
909 struct vnode *vdp = ITOV(dp);
912 if ((error = EXT2_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp)) != 0)
914 ep->inode = ip->i_number;
915 if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs->s_es,
916 EXT2_FEATURE_INCOMPAT_FILETYPE))
917 ep->file_type = DTTOFT(IFTODT(ip->i_mode));
919 ep->file_type = EXT2_FT_UNKNOWN;
920 error = VOP_BWRITE(bp->b_vp, bp);
921 dp->i_flag |= IN_CHANGE | IN_UPDATE;
926 * Check if a directory is empty or not.
927 * Inode supplied must be locked.
929 * Using a struct dirtemplate here is not precisely
930 * what we want, but better than using a struct direct.
932 * NB: does not handle corrupted directories.
935 ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
938 struct dirtemplate dbuf;
939 struct ext2_dir_entry_2 *dp = (struct ext2_dir_entry_2 *)&dbuf;
940 int error, count, namlen;
942 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
944 for (off = 0; off < ip->i_size; off += dp->rec_len) {
945 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
946 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, NULL);
948 * Since we read MINDIRSIZ, residual must
949 * be 0 unless we're at end of file.
951 if (error || count != 0)
953 /* avoid infinite loops */
954 if (dp->rec_len == 0)
956 /* skip empty entries */
959 /* accept only "." and ".." */
960 namlen = dp->name_len;
963 if (dp->name[0] != '.')
966 * At this point namlen must be 1 or 2.
967 * 1 implies ".", 2 implies ".." if second
972 if (dp->name[1] == '.' && dp->inode == parentino)
980 * Check if source directory is in the path of the target directory.
981 * Target is supplied locked, source is unlocked.
982 * The target is always vput before returning.
985 ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred)
988 int error, rootino, namlen;
989 struct dirtemplate dirbuf;
992 if (target->i_number == source->i_number) {
998 if (target->i_number == rootino)
1002 if (vp->v_type != VDIR) {
1006 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1007 sizeof (struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1008 IO_NODELOCKED, cred, (int *)0, NULL);
1011 namlen = dirbuf.dotdot_type; /* like ufs little-endian */
1013 dirbuf.dotdot_name[0] != '.' ||
1014 dirbuf.dotdot_name[1] != '.') {
1018 if (dirbuf.dotdot_ino == source->i_number) {
1022 if (dirbuf.dotdot_ino == rootino)
1025 if ((error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino, &vp)) != 0) {
1032 if (error == ENOTDIR)
1033 printf("checkpath: .. not a directory\n");