Remove building with NOCRYPTO option
[minix.git] / sys / ufs / ext2fs / ext2fs_lookup.c
blobfc15c819950568bf7c5f2edd3c180d65e343cf33
1 /* $NetBSD: ext2fs_lookup.c,v 1.78 2015/03/27 17:27:56 riastradh Exp $ */
3 /*
4 * Modified for NetBSD 1.2E
5 * May 1997, Manuel Bouyer
6 * Laboratoire d'informatique de Paris VI
7 */
8 /*
9 * modified for Lites 1.1
11 * Aug 1995, Godmar Back (gback@cs.utah.edu)
12 * University of Utah, Department of Computer Science
15 * Copyright (c) 1989, 1993
16 * The Regents of the University of California. All rights reserved.
17 * (c) UNIX System Laboratories, Inc.
18 * All or some portions of this file are derived from material licensed
19 * to the University of California by American Telephone and Telegraph
20 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
21 * the permission of UNIX System Laboratories, Inc.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. 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
45 * SUCH DAMAGE.
47 * @(#)ufs_lookup.c 8.6 (Berkeley) 4/1/94
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: ext2fs_lookup.c,v 1.78 2015/03/27 17:27:56 riastradh Exp $");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/namei.h>
56 #include <sys/buf.h>
57 #include <sys/file.h>
58 #include <sys/mount.h>
59 #include <sys/vnode.h>
60 #include <sys/kmem.h>
61 #include <sys/malloc.h>
62 #include <sys/dirent.h>
63 #include <sys/kauth.h>
64 #include <sys/proc.h>
66 #include <ufs/ufs/inode.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ufs/ufs_extern.h>
70 #include <ufs/ext2fs/ext2fs_extern.h>
71 #include <ufs/ext2fs/ext2fs_dir.h>
72 #include <ufs/ext2fs/ext2fs.h>
74 #include <miscfs/genfs/genfs.h>
76 extern int dirchk;
78 static void ext2fs_dirconv2ffs(struct ext2fs_direct *e2dir,
79 struct dirent *ffsdir);
80 static int ext2fs_dirbadentry(struct vnode *dp,
81 struct ext2fs_direct *de,
82 int entryoffsetinblock);
85 * the problem that is tackled below is the fact that FFS
86 * includes the terminating zero on disk while EXT2FS doesn't
87 * this implies that we need to introduce some padding.
88 * For instance, a filename "sbin" has normally a reclen 12
89 * in EXT2, but 16 in FFS.
90 * This reminds me of that Pepsi commercial: 'Kid saved a lousy nine cents...'
91 * If it wasn't for that, the complete ufs code for directories would
92 * have worked w/o changes (except for the difference in DIRBLKSIZ)
94 static void
95 ext2fs_dirconv2ffs(struct ext2fs_direct *e2dir, struct dirent *ffsdir)
97 memset(ffsdir, 0, sizeof(struct dirent));
98 ffsdir->d_fileno = fs2h32(e2dir->e2d_ino);
99 ffsdir->d_namlen = e2dir->e2d_namlen;
101 ffsdir->d_type = DT_UNKNOWN; /* don't know more here */
102 #ifdef DIAGNOSTIC
103 #if MAXNAMLEN < E2FS_MAXNAMLEN
105 * we should handle this more gracefully !
107 if (e2dir->e2d_namlen > MAXNAMLEN)
108 panic("ext2fs: e2dir->e2d_namlen");
109 #endif
110 #endif
111 strncpy(ffsdir->d_name, e2dir->e2d_name, ffsdir->d_namlen);
113 /* Godmar thinks: since e2dir->e2d_reclen can be big and means
114 nothing anyway, we compute our own reclen according to what
115 we think is right
117 ffsdir->d_reclen = _DIRENT_SIZE(ffsdir);
121 * Vnode op for reading directories.
123 * Convert the on-disk entries to <sys/dirent.h> entries.
124 * the problem is that the conversion will blow up some entries by four bytes,
125 * so it can't be done in place. This is too bad. Right now the conversion is
126 * done entry by entry, 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 ext2fs_readdir(void *v)
134 struct vop_readdir_args /* {
135 struct vnode *a_vp;
136 struct uio *a_uio;
137 kauth_cred_t a_cred;
138 int **a_eofflag;
139 off_t **a_cookies;
140 int ncookies;
141 } */ *ap = v;
142 struct uio *uio = ap->a_uio;
143 int error;
144 size_t e2fs_count, readcnt;
145 struct vnode *vp = ap->a_vp;
146 struct m_ext2fs *fs = VTOI(vp)->i_e2fs;
148 struct ext2fs_direct *dp;
149 struct dirent *dstd;
150 struct uio auio;
151 struct iovec aiov;
152 void *dirbuf;
153 off_t off = uio->uio_offset;
154 off_t *cookies = NULL;
155 int nc = 0, ncookies = 0;
156 int e2d_reclen;
158 if (vp->v_type != VDIR)
159 return (ENOTDIR);
161 e2fs_count = uio->uio_resid;
162 /* Make sure we don't return partial entries. */
163 e2fs_count -= (uio->uio_offset + e2fs_count) & (fs->e2fs_bsize -1);
164 if (e2fs_count <= 0)
165 return (EINVAL);
167 auio = *uio;
168 auio.uio_iov = &aiov;
169 auio.uio_iovcnt = 1;
170 aiov.iov_len = e2fs_count;
171 auio.uio_resid = e2fs_count;
172 UIO_SETUP_SYSSPACE(&auio);
173 dirbuf = kmem_alloc(e2fs_count, KM_SLEEP);
174 dstd = kmem_zalloc(sizeof(struct dirent), KM_SLEEP);
175 if (ap->a_ncookies) {
176 nc = e2fs_count / _DIRENT_MINSIZE((struct dirent *)0);
177 ncookies = nc;
178 cookies = malloc(sizeof (off_t) * ncookies, M_TEMP, M_WAITOK);
179 *ap->a_cookies = cookies;
181 aiov.iov_base = dirbuf;
183 error = UFS_BUFRD(ap->a_vp, &auio, 0, ap->a_cred);
184 if (error == 0) {
185 readcnt = e2fs_count - auio.uio_resid;
186 for (dp = (struct ext2fs_direct *)dirbuf;
187 (char *)dp < (char *)dirbuf + readcnt; ) {
188 e2d_reclen = fs2h16(dp->e2d_reclen);
189 if (e2d_reclen == 0) {
190 error = EIO;
191 break;
193 ext2fs_dirconv2ffs(dp, dstd);
194 if(dstd->d_reclen > uio->uio_resid) {
195 break;
197 error = uiomove(dstd, dstd->d_reclen, uio);
198 if (error != 0) {
199 break;
201 off = off + e2d_reclen;
202 if (cookies != NULL) {
203 *cookies++ = off;
204 if (--ncookies <= 0){
205 break; /* out of cookies */
208 /* advance dp */
209 dp = (struct ext2fs_direct *) ((char *)dp + e2d_reclen);
211 /* we need to correct uio_offset */
212 uio->uio_offset = off;
214 kmem_free(dirbuf, e2fs_count);
215 kmem_free(dstd, sizeof(*dstd));
216 *ap->a_eofflag = ext2fs_size(VTOI(ap->a_vp)) <= uio->uio_offset;
217 if (ap->a_ncookies) {
218 if (error) {
219 free(*ap->a_cookies, M_TEMP);
220 *ap->a_ncookies = 0;
221 *ap->a_cookies = NULL;
222 } else
223 *ap->a_ncookies = nc - ncookies;
225 return (error);
229 * Convert a component of a pathname into a pointer to a locked inode.
230 * This is a very central and rather complicated routine.
231 * If the file system is not maintained in a strict tree hierarchy,
232 * this can result in a deadlock situation (see comments in code below).
234 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
235 * on whether the name is to be looked up, created, renamed, or deleted.
236 * When CREATE, RENAME, or DELETE is specified, information usable in
237 * creating, renaming, or deleting a directory entry may be calculated.
238 * If flag has LOCKPARENT or'ed into it and the target of the pathname
239 * exists, lookup returns both the target and its parent directory locked.
240 * When creating or renaming and LOCKPARENT is specified, the target may
241 * not be ".". When deleting and LOCKPARENT is specified, the target may
242 * be "."., but the caller must check to ensure it does an vrele and vput
243 * instead of two vputs.
245 * Overall outline of ext2fs_lookup:
247 * check accessibility of directory
248 * look for name in cache, if found, then if at end of path
249 * and deleting or creating, drop it, else return name
250 * search for name in directory, to found or notfound
251 * notfound:
252 * if creating, return locked directory, leaving info on available slots
253 * else return error
254 * found:
255 * if at end of path and deleting, return information to allow delete
256 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
257 * inode and return info to allow rewrite
258 * if not at end, add name to cache; if at end and neither creating
259 * nor deleting, add name to cache
262 ext2fs_lookup(void *v)
264 struct vop_lookup_v2_args /* {
265 struct vnode *a_dvp;
266 struct vnode **a_vpp;
267 struct componentname *a_cnp;
268 } */ *ap = v;
269 struct vnode *vdp = ap->a_dvp; /* vnode for directory being searched */
270 struct inode *dp = VTOI(vdp); /* inode for directory being searched */
271 struct buf *bp; /* a buffer of directory entries */
272 struct ext2fs_direct *ep; /* the current directory entry */
273 int entryoffsetinblock; /* offset of ep in bp's buffer */
274 enum {NONE, COMPACT, FOUND} slotstatus;
275 doff_t slotoffset; /* offset of area with free space */
276 int slotsize; /* size of area at slotoffset */
277 int slotfreespace; /* amount of space free in slot */
278 int slotneeded; /* size of the entry we're seeking */
279 int numdirpasses; /* strategy for directory search */
280 doff_t endsearch; /* offset to end directory search */
281 doff_t prevoff; /* prev entry dp->i_offset */
282 struct vnode *tdp; /* returned by vcache_get */
283 doff_t enduseful; /* pointer past last used dir slot */
284 u_long bmask; /* block offset mask */
285 int namlen, error;
286 struct vnode **vpp = ap->a_vpp;
287 struct componentname *cnp = ap->a_cnp;
288 kauth_cred_t cred = cnp->cn_cred;
289 int flags;
290 int nameiop = cnp->cn_nameiop;
291 struct ufsmount *ump = dp->i_ump;
292 int dirblksiz = ump->um_dirblksiz;
293 ino_t foundino;
294 struct ufs_lookup_results *results;
296 flags = cnp->cn_flags;
298 bp = NULL;
299 slotoffset = -1;
300 *vpp = NULL;
303 * Produce the auxiliary lookup results into i_crap. Increment
304 * its serial number so elsewhere we can tell if we're using
305 * stale results. This should not be done this way. XXX.
307 results = &dp->i_crap;
308 dp->i_crapcounter++;
311 * Check accessiblity of directory.
313 if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0)
314 return (error);
316 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
317 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
318 return (EROFS);
321 * We now have a segment name to search for, and a directory to search.
323 * Before tediously performing a linear scan of the directory,
324 * check the name cache to see if the directory/name pair
325 * we are looking for is known already.
327 if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
328 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
329 return *vpp == NULLVP ? ENOENT : 0;
333 * Suppress search for slots unless creating
334 * file and at end of pathname, in which case
335 * we watch for a place to put the new file in
336 * case it doesn't already exist.
338 slotstatus = FOUND;
339 slotfreespace = slotsize = slotneeded = 0;
340 if ((nameiop == CREATE || nameiop == RENAME) &&
341 (flags & ISLASTCN)) {
342 slotstatus = NONE;
343 slotneeded = EXT2FS_DIRSIZ(cnp->cn_namelen);
347 * If there is cached information on a previous search of
348 * this directory, pick up where we last left off.
349 * We cache only lookups as these are the most common
350 * and have the greatest payoff. Caching CREATE has little
351 * benefit as it usually must search the entire directory
352 * to determine that the entry does not exist. Caching the
353 * location of the last DELETE or RENAME has not reduced
354 * profiling time and hence has been removed in the interest
355 * of simplicity.
357 bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
358 if (nameiop != LOOKUP || results->ulr_diroff == 0 ||
359 results->ulr_diroff >= ext2fs_size(dp)) {
360 entryoffsetinblock = 0;
361 results->ulr_offset = 0;
362 numdirpasses = 1;
363 } else {
364 results->ulr_offset = results->ulr_diroff;
365 if ((entryoffsetinblock = results->ulr_offset & bmask) &&
366 (error = ext2fs_blkatoff(vdp, (off_t)results->ulr_offset, NULL, &bp)))
367 return (error);
368 numdirpasses = 2;
369 namecache_count_2passes();
371 prevoff = results->ulr_offset;
372 endsearch = roundup(ext2fs_size(dp), dirblksiz);
373 enduseful = 0;
375 searchloop:
376 while (results->ulr_offset < endsearch) {
377 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
378 preempt();
380 * If necessary, get the next directory block.
382 if ((results->ulr_offset & bmask) == 0) {
383 if (bp != NULL)
384 brelse(bp, 0);
385 error = ext2fs_blkatoff(vdp, (off_t)results->ulr_offset, NULL,
386 &bp);
387 if (error != 0)
388 return (error);
389 entryoffsetinblock = 0;
392 * If still looking for a slot, and at a dirblksize
393 * boundary, have to start looking for free space again.
395 if (slotstatus == NONE &&
396 (entryoffsetinblock & (dirblksiz - 1)) == 0) {
397 slotoffset = -1;
398 slotfreespace = 0;
401 * Get pointer to next entry.
402 * Full validation checks are slow, so we only check
403 * enough to insure forward progress through the
404 * directory. Complete checks can be run by patching
405 * "dirchk" to be true.
407 KASSERT(bp != NULL);
408 ep = (struct ext2fs_direct *)
409 ((char *)bp->b_data + entryoffsetinblock);
410 if (ep->e2d_reclen == 0 ||
411 (dirchk &&
412 ext2fs_dirbadentry(vdp, ep, entryoffsetinblock))) {
413 int i;
415 ufs_dirbad(dp, results->ulr_offset, "mangled entry");
416 i = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
417 results->ulr_offset += i;
418 entryoffsetinblock += i;
419 continue;
423 * If an appropriate sized slot has not yet been found,
424 * check to see if one is available. Also accumulate space
425 * in the current block so that we can determine if
426 * compaction is viable.
428 if (slotstatus != FOUND) {
429 int size = fs2h16(ep->e2d_reclen);
431 if (ep->e2d_ino != 0)
432 size -= EXT2FS_DIRSIZ(ep->e2d_namlen);
433 if (size > 0) {
434 if (size >= slotneeded) {
435 slotstatus = FOUND;
436 slotoffset = results->ulr_offset;
437 slotsize = fs2h16(ep->e2d_reclen);
438 } else if (slotstatus == NONE) {
439 slotfreespace += size;
440 if (slotoffset == -1)
441 slotoffset = results->ulr_offset;
442 if (slotfreespace >= slotneeded) {
443 slotstatus = COMPACT;
444 slotsize = results->ulr_offset +
445 fs2h16(ep->e2d_reclen) -
446 slotoffset;
453 * Check for a name match.
455 if (ep->e2d_ino) {
456 namlen = ep->e2d_namlen;
457 if (namlen == cnp->cn_namelen &&
458 !memcmp(cnp->cn_nameptr, ep->e2d_name,
459 (unsigned)namlen)) {
461 * Save directory entry's inode number and
462 * reclen in ndp->ni_ufs area, and release
463 * directory buffer.
465 foundino = fs2h32(ep->e2d_ino);
466 results->ulr_reclen = fs2h16(ep->e2d_reclen);
467 goto found;
470 prevoff = results->ulr_offset;
471 results->ulr_offset += fs2h16(ep->e2d_reclen);
472 entryoffsetinblock += fs2h16(ep->e2d_reclen);
473 if (ep->e2d_ino)
474 enduseful = results->ulr_offset;
476 /* notfound: */
478 * If we started in the middle of the directory and failed
479 * to find our target, we must check the beginning as well.
481 if (numdirpasses == 2) {
482 numdirpasses--;
483 results->ulr_offset = 0;
484 endsearch = results->ulr_diroff;
485 goto searchloop;
487 if (bp != NULL)
488 brelse(bp, 0);
490 * If creating, and at end of pathname and current
491 * directory has not been removed, then can consider
492 * allowing file to be created.
494 if ((nameiop == CREATE || nameiop == RENAME) &&
495 (flags & ISLASTCN) && dp->i_e2fs_nlink != 0) {
497 * Access for write is interpreted as allowing
498 * creation of files in the directory.
500 error = VOP_ACCESS(vdp, VWRITE, cred);
501 if (error)
502 return (error);
504 * Return an indication of where the new directory
505 * entry should be put. If we didn't find a slot,
506 * then set results->ulr_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 results->ulr_offset to
510 * results->ulr_offset + results->ulr_count.
512 if (slotstatus == NONE) {
513 results->ulr_offset = roundup(ext2fs_size(dp), dirblksiz);
514 results->ulr_count = 0;
515 enduseful = results->ulr_offset;
516 } else {
517 results->ulr_offset = slotoffset;
518 results->ulr_count = slotsize;
519 if (enduseful < slotoffset + slotsize)
520 enduseful = slotoffset + slotsize;
522 results->ulr_endoff = roundup(enduseful, dirblksiz);
523 #if 0
524 dp->i_flag |= IN_CHANGE | IN_UPDATE;
525 #endif
527 * We return with the directory locked, so that
528 * the parameters we set up above will still be
529 * valid if we actually decide to do a direnter().
530 * We return ni_vp == NULL to indicate that the entry
531 * does not currently exist; we leave a pointer to
532 * the (locked) directory inode in ndp->ni_dvp.
534 * NB - if the directory is unlocked, then this
535 * information cannot be used.
537 return (EJUSTRETURN);
540 * Insert name into cache (as non-existent) if appropriate.
542 if (nameiop != CREATE) {
543 cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
544 cnp->cn_flags);
546 return ENOENT;
548 found:
549 if (numdirpasses == 2)
550 namecache_count_pass2();
552 * Check that directory length properly reflects presence
553 * of this entry.
555 if (results->ulr_offset + EXT2FS_DIRSIZ(ep->e2d_namlen) > ext2fs_size(dp)) {
556 ufs_dirbad(dp, results->ulr_offset, "i_size too small");
557 error = ext2fs_setsize(dp,
558 results->ulr_offset + EXT2FS_DIRSIZ(ep->e2d_namlen));
559 if (error) {
560 brelse(bp, 0);
561 return (error);
563 dp->i_flag |= IN_CHANGE | IN_UPDATE;
564 uvm_vnp_setsize(vdp, ext2fs_size(dp));
566 brelse(bp, 0);
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 & ISLASTCN) && nameiop == LOOKUP)
574 results->ulr_diroff = results->ulr_offset &~ (dirblksiz - 1);
577 * If deleting, and at end of pathname, return
578 * parameters which can be used to remove file.
579 * Lock the inode, being careful with ".".
581 if (nameiop == DELETE && (flags & ISLASTCN)) {
583 * Return pointer to current entry in results->ulr_offset,
584 * and distance past previous entry (if there
585 * is a previous entry in this block) in results->ulr_count.
586 * Save directory inode pointer in ndp->ni_dvp for dirremove().
588 if ((results->ulr_offset & (dirblksiz - 1)) == 0)
589 results->ulr_count = 0;
590 else
591 results->ulr_count = results->ulr_offset - prevoff;
592 if (dp->i_number == foundino) {
593 vref(vdp);
594 tdp = vdp;
595 } else {
596 error = vcache_get(vdp->v_mount,
597 &foundino, sizeof(foundino), &tdp);
598 if (error)
599 return (error);
602 * Write access to directory required to delete files.
604 if ((error = VOP_ACCESS(vdp, VWRITE, cred)) != 0) {
605 vrele(tdp);
606 return (error);
609 * If directory is "sticky", then user must own
610 * the directory, or the file in it, else she
611 * may not delete it (unless she's root). This
612 * implements append-only directories.
614 if (dp->i_e2fs_mode & ISVTX) {
615 error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE,
616 tdp, vdp, genfs_can_sticky(cred, dp->i_uid,
617 VTOI(tdp)->i_uid));
618 if (error) {
619 vrele(tdp);
620 return (EPERM);
623 *vpp = tdp;
624 return (0);
628 * If rewriting (RENAME), return the inode and the
629 * information required to rewrite the present directory
630 * Must get inode of directory entry to verify it's a
631 * regular file, or empty directory.
633 if (nameiop == RENAME && (flags & ISLASTCN)) {
634 error = VOP_ACCESS(vdp, VWRITE, cred);
635 if (error)
636 return (error);
638 * Careful about locking second inode.
639 * This can only occur if the target is ".".
641 if (dp->i_number == foundino)
642 return (EISDIR);
643 error = vcache_get(vdp->v_mount,
644 &foundino, sizeof(foundino), &tdp);
645 if (error)
646 return (error);
647 *vpp = tdp;
648 return (0);
651 if (dp->i_number == foundino) {
652 vref(vdp); /* we want ourself, ie "." */
653 *vpp = vdp;
654 } else {
655 error = vcache_get(vdp->v_mount,
656 &foundino, sizeof(foundino), &tdp);
657 if (error)
658 return (error);
659 *vpp = tdp;
663 * Insert name into cache if appropriate.
665 cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
666 return 0;
670 * Do consistency checking on a directory entry:
671 * record length must be multiple of 4
672 * entry must fit in rest of its dirblksize block
673 * record must be large enough to contain entry
674 * name is not longer than EXT2FS_MAXNAMLEN
675 * name must be as long as advertised, and null terminated
678 * changed so that it confirms to ext2fs_check_dir_entry
680 static int
681 ext2fs_dirbadentry(struct vnode *dp, struct ext2fs_direct *de,
682 int entryoffsetinblock)
684 struct ufsmount *ump = VFSTOUFS(dp->v_mount);
685 int dirblksiz = ump->um_dirblksiz;
687 const char *error_msg = NULL;
688 int reclen = fs2h16(de->e2d_reclen);
689 int namlen = de->e2d_namlen;
691 if (reclen < EXT2FS_DIRSIZ(1)) /* e2d_namlen = 1 */
692 error_msg = "rec_len is smaller than minimal";
693 else if (reclen % 4 != 0)
694 error_msg = "rec_len % 4 != 0";
695 else if (namlen > EXT2FS_MAXNAMLEN)
696 error_msg = "namlen > EXT2FS_MAXNAMLEN";
697 else if (reclen < EXT2FS_DIRSIZ(namlen))
698 error_msg = "reclen is too small for name_len";
699 else if (entryoffsetinblock + reclen > dirblksiz)
700 error_msg = "directory entry across blocks";
701 else if (fs2h32(de->e2d_ino) >
702 VTOI(dp)->i_e2fs->e2fs.e2fs_icount)
703 error_msg = "inode out of bounds";
705 if (error_msg != NULL) {
706 printf( "bad directory entry: %s\n"
707 "offset=%d, inode=%lu, rec_len=%d, name_len=%d \n",
708 error_msg, entryoffsetinblock,
709 (unsigned long) fs2h32(de->e2d_ino),
710 reclen, namlen);
711 panic("ext2fs_dirbadentry");
713 return error_msg == NULL ? 0 : 1;
717 * Write a directory entry after a call to namei, using the parameters
718 * that it left in nameidata. The argument ip is the inode which the new
719 * directory entry will refer to. Dvp is a pointer to the directory to
720 * be written, which was left locked by namei. Remaining parameters
721 * (ulr_offset, ulr_count) indicate how the space for the new
722 * entry is to be obtained.
725 ext2fs_direnter(struct inode *ip, struct vnode *dvp,
726 const struct ufs_lookup_results *ulr,
727 struct componentname *cnp)
729 struct ext2fs_direct *ep, *nep;
730 struct inode *dp;
731 struct buf *bp;
732 struct ext2fs_direct newdir;
733 struct iovec aiov;
734 struct uio auio;
735 u_int dsize;
736 int error, loc, newentrysize, spacefree;
737 char *dirbuf;
738 struct ufsmount *ump = VFSTOUFS(dvp->v_mount);
739 int dirblksiz = ump->um_dirblksiz;
741 dp = VTOI(dvp);
743 newdir.e2d_ino = h2fs32(ip->i_number);
744 newdir.e2d_namlen = cnp->cn_namelen;
745 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
746 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
747 newdir.e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
748 } else {
749 newdir.e2d_type = 0;
751 memcpy(newdir.e2d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1);
752 newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen);
753 if (ulr->ulr_count == 0) {
755 * If ulr_count is 0, then namei could find no
756 * space in the directory. Here, ulr_offset will
757 * be on a directory block boundary and we will write the
758 * new entry into a fresh block.
760 if (ulr->ulr_offset & (dirblksiz - 1))
761 panic("ext2fs_direnter: newblk");
762 auio.uio_offset = ulr->ulr_offset;
763 newdir.e2d_reclen = h2fs16(dirblksiz);
764 auio.uio_resid = newentrysize;
765 aiov.iov_len = newentrysize;
766 aiov.iov_base = (void *)&newdir;
767 auio.uio_iov = &aiov;
768 auio.uio_iovcnt = 1;
769 auio.uio_rw = UIO_WRITE;
770 UIO_SETUP_SYSSPACE(&auio);
771 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
772 if (dirblksiz > dvp->v_mount->mnt_stat.f_bsize)
773 /* XXX should grow with balloc() */
774 panic("ext2fs_direnter: frag size");
775 else if (!error) {
776 error = ext2fs_setsize(dp,
777 roundup(ext2fs_size(dp), dirblksiz));
778 if (error)
779 return (error);
780 dp->i_flag |= IN_CHANGE;
781 uvm_vnp_setsize(dvp, ext2fs_size(dp));
783 return (error);
787 * If ulr_count is non-zero, then namei found space
788 * for the new entry in the range ulr_offset to
789 * ulr_offset + ulr_count in the directory.
790 * To use this space, we may have to compact the entries located
791 * there, by copying them together towards the beginning of the
792 * block, leaving the free space in one usable chunk at the end.
796 * Get the block containing the space for the new directory entry.
798 if ((error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset, &dirbuf, &bp)) != 0)
799 return (error);
801 * Find space for the new entry. In the simple case, the entry at
802 * offset base will have the space. If it does not, then namei
803 * arranged that compacting the region ulr_offset to
804 * ulr_offset + ulr_count would yield the
805 * space.
807 ep = (struct ext2fs_direct *)dirbuf;
808 dsize = EXT2FS_DIRSIZ(ep->e2d_namlen);
809 spacefree = fs2h16(ep->e2d_reclen) - dsize;
810 for (loc = fs2h16(ep->e2d_reclen); loc < ulr->ulr_count; ) {
811 nep = (struct ext2fs_direct *)(dirbuf + loc);
812 if (ep->e2d_ino) {
813 /* trim the existing slot */
814 ep->e2d_reclen = h2fs16(dsize);
815 ep = (struct ext2fs_direct *)((char *)ep + dsize);
816 } else {
817 /* overwrite; nothing there; header is ours */
818 spacefree += dsize;
820 dsize = EXT2FS_DIRSIZ(nep->e2d_namlen);
821 spacefree += fs2h16(nep->e2d_reclen) - dsize;
822 loc += fs2h16(nep->e2d_reclen);
823 memcpy((void *)ep, (void *)nep, dsize);
826 * Update the pointer fields in the previous entry (if any),
827 * copy in the new entry, and write out the block.
829 if (ep->e2d_ino == 0) {
830 #ifdef DIAGNOSTIC
831 if (spacefree + dsize < newentrysize)
832 panic("ext2fs_direnter: compact1");
833 #endif
834 newdir.e2d_reclen = h2fs16(spacefree + dsize);
835 } else {
836 #ifdef DIAGNOSTIC
837 if (spacefree < newentrysize) {
838 printf("ext2fs_direnter: compact2 %u %u",
839 (u_int)spacefree, (u_int)newentrysize);
840 panic("ext2fs_direnter: compact2");
842 #endif
843 newdir.e2d_reclen = h2fs16(spacefree);
844 ep->e2d_reclen = h2fs16(dsize);
845 ep = (struct ext2fs_direct *)((char *)ep + dsize);
847 memcpy((void *)ep, (void *)&newdir, (u_int)newentrysize);
848 error = VOP_BWRITE(bp->b_vp, bp);
849 dp->i_flag |= IN_CHANGE | IN_UPDATE;
850 if (!error && ulr->ulr_endoff && ulr->ulr_endoff < ext2fs_size(dp))
851 error = ext2fs_truncate(dvp, (off_t)ulr->ulr_endoff, IO_SYNC,
852 cnp->cn_cred);
853 return (error);
857 * Remove a directory entry after a call to namei, using
858 * the auxiliary results it provided. The entry
859 * ulr_offset contains the offset into the directory of the
860 * entry to be eliminated. The ulr_count field contains the
861 * size of the previous record in the directory. If this
862 * is 0, the first entry is being deleted, so we need only
863 * zero the inode number to mark the entry as free. If the
864 * entry is not the first in the directory, we must reclaim
865 * the space of the now empty record by adding the record size
866 * to the size of the previous entry.
869 ext2fs_dirremove(struct vnode *dvp, const struct ufs_lookup_results *ulr,
870 struct componentname *cnp)
872 struct inode *dp;
873 struct ext2fs_direct *ep;
874 struct buf *bp;
875 int error;
877 dp = VTOI(dvp);
879 if (ulr->ulr_count == 0) {
881 * First entry in block: set d_ino to zero.
883 error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset,
884 (void *)&ep, &bp);
885 if (error != 0)
886 return (error);
887 ep->e2d_ino = 0;
888 error = VOP_BWRITE(bp->b_vp, bp);
889 dp->i_flag |= IN_CHANGE | IN_UPDATE;
890 return (error);
893 * Collapse new free space into previous entry.
895 error = ext2fs_blkatoff(dvp, (off_t)(ulr->ulr_offset - ulr->ulr_count),
896 (void *)&ep, &bp);
897 if (error != 0)
898 return (error);
899 ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + ulr->ulr_reclen);
900 error = VOP_BWRITE(bp->b_vp, bp);
901 dp->i_flag |= IN_CHANGE | IN_UPDATE;
902 return (error);
906 * Rewrite an existing directory entry to point at the inode
907 * supplied. The parameters describing the directory entry are
908 * set up by a call to namei.
911 ext2fs_dirrewrite(struct inode *dp, const struct ufs_lookup_results *ulr,
912 struct inode *ip, struct componentname *cnp)
914 struct buf *bp;
915 struct ext2fs_direct *ep;
916 struct vnode *vdp = ITOV(dp);
917 int error;
919 error = ext2fs_blkatoff(vdp, (off_t)ulr->ulr_offset, (void *)&ep, &bp);
920 if (error != 0)
921 return (error);
922 ep->e2d_ino = h2fs32(ip->i_number);
923 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
924 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
925 ep->e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
926 } else {
927 ep->e2d_type = 0;
929 error = VOP_BWRITE(bp->b_vp, bp);
930 dp->i_flag |= IN_CHANGE | IN_UPDATE;
931 return (error);
935 * Check if a directory is empty or not.
936 * Inode supplied must be locked.
938 * Using a struct dirtemplate here is not precisely
939 * what we want, but better than using a struct ext2fs_direct.
941 * NB: does not handle corrupted directories.
944 ext2fs_dirempty(struct inode *ip, ino_t parentino, kauth_cred_t cred)
946 off_t off;
947 struct ext2fs_dirtemplate dbuf;
948 struct ext2fs_direct *dp = (struct ext2fs_direct *)&dbuf;
949 int error, namlen;
950 size_t count;
952 #define MINDIRSIZ (sizeof (struct ext2fs_dirtemplate) / 2)
954 for (off = 0; off < ext2fs_size(ip); off += fs2h16(dp->e2d_reclen)) {
955 error = ufs_bufio(UIO_READ, ITOV(ip), (void *)dp, MINDIRSIZ,
956 off, IO_NODELOCKED, cred, &count, NULL);
958 * Since we read MINDIRSIZ, residual must
959 * be 0 unless we're at end of file.
961 if (error || count != 0)
962 return (0);
963 /* avoid infinite loops */
964 if (dp->e2d_reclen == 0)
965 return (0);
966 /* skip empty entries */
967 if (dp->e2d_ino == 0)
968 continue;
969 /* accept only "." and ".." */
970 namlen = dp->e2d_namlen;
971 if (namlen > 2)
972 return (0);
973 if (dp->e2d_name[0] != '.')
974 return (0);
976 * At this point namlen must be 1 or 2.
977 * 1 implies ".", 2 implies ".." if second
978 * char is also "."
980 if (namlen == 1)
981 continue;
982 if (dp->e2d_name[1] == '.' && fs2h32(dp->e2d_ino) == parentino)
983 continue;
984 return (0);
986 return (1);