tools/llvm: Do not build with symbols
[minix3.git] / sys / ufs / ext2fs / ext2fs_lookup.c
blobcc10f2ec6e7d0c8b11bcfe144dfd56922d73f68a
1 /* $NetBSD: ext2fs_lookup.c,v 1.73 2013/01/22 09:39:15 dholland 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.73 2013/01/22 09:39:15 dholland 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 = VOP_READ(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_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 *pdp; /* saved dp during symlink work */
283 struct vnode *tdp; /* returned by VFS_VGET */
284 doff_t enduseful; /* pointer past last used dir slot */
285 u_long bmask; /* block offset mask */
286 int namlen, error;
287 struct vnode **vpp = ap->a_vpp;
288 struct componentname *cnp = ap->a_cnp;
289 kauth_cred_t cred = cnp->cn_cred;
290 int flags;
291 int nameiop = cnp->cn_nameiop;
292 struct ufsmount *ump = dp->i_ump;
293 int dirblksiz = ump->um_dirblksiz;
294 ino_t foundino;
295 struct ufs_lookup_results *results;
297 flags = cnp->cn_flags;
299 bp = NULL;
300 slotoffset = -1;
301 *vpp = NULL;
304 * Produce the auxiliary lookup results into i_crap. Increment
305 * its serial number so elsewhere we can tell if we're using
306 * stale results. This should not be done this way. XXX.
308 results = &dp->i_crap;
309 dp->i_crapcounter++;
312 * Check accessiblity of directory.
314 if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0)
315 return (error);
317 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
318 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
319 return (EROFS);
322 * We now have a segment name to search for, and a directory to search.
324 * Before tediously performing a linear scan of the directory,
325 * check the name cache to see if the directory/name pair
326 * we are looking for is known already.
328 if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen,
329 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) {
330 return *vpp == NULLVP ? ENOENT : 0;
334 * Suppress search for slots unless creating
335 * file and at end of pathname, in which case
336 * we watch for a place to put the new file in
337 * case it doesn't already exist.
339 slotstatus = FOUND;
340 slotfreespace = slotsize = slotneeded = 0;
341 if ((nameiop == CREATE || nameiop == RENAME) &&
342 (flags & ISLASTCN)) {
343 slotstatus = NONE;
344 slotneeded = EXT2FS_DIRSIZ(cnp->cn_namelen);
348 * If there is cached information on a previous search of
349 * this directory, pick up where we last left off.
350 * We cache only lookups as these are the most common
351 * and have the greatest payoff. Caching CREATE has little
352 * benefit as it usually must search the entire directory
353 * to determine that the entry does not exist. Caching the
354 * location of the last DELETE or RENAME has not reduced
355 * profiling time and hence has been removed in the interest
356 * of simplicity.
358 bmask = vdp->v_mount->mnt_stat.f_iosize - 1;
359 if (nameiop != LOOKUP || results->ulr_diroff == 0 ||
360 results->ulr_diroff >= ext2fs_size(dp)) {
361 entryoffsetinblock = 0;
362 results->ulr_offset = 0;
363 numdirpasses = 1;
364 } else {
365 results->ulr_offset = results->ulr_diroff;
366 if ((entryoffsetinblock = results->ulr_offset & bmask) &&
367 (error = ext2fs_blkatoff(vdp, (off_t)results->ulr_offset, NULL, &bp)))
368 return (error);
369 numdirpasses = 2;
370 nchstats.ncs_2passes++;
372 prevoff = results->ulr_offset;
373 endsearch = roundup(ext2fs_size(dp), dirblksiz);
374 enduseful = 0;
376 searchloop:
377 while (results->ulr_offset < endsearch) {
378 if (curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
379 preempt();
381 * If necessary, get the next directory block.
383 if ((results->ulr_offset & bmask) == 0) {
384 if (bp != NULL)
385 brelse(bp, 0);
386 error = ext2fs_blkatoff(vdp, (off_t)results->ulr_offset, NULL,
387 &bp);
388 if (error != 0)
389 return (error);
390 entryoffsetinblock = 0;
393 * If still looking for a slot, and at a dirblksize
394 * boundary, have to start looking for free space again.
396 if (slotstatus == NONE &&
397 (entryoffsetinblock & (dirblksiz - 1)) == 0) {
398 slotoffset = -1;
399 slotfreespace = 0;
402 * Get pointer to next entry.
403 * Full validation checks are slow, so we only check
404 * enough to insure forward progress through the
405 * directory. Complete checks can be run by patching
406 * "dirchk" to be true.
408 KASSERT(bp != NULL);
409 ep = (struct ext2fs_direct *)
410 ((char *)bp->b_data + entryoffsetinblock);
411 if (ep->e2d_reclen == 0 ||
412 (dirchk &&
413 ext2fs_dirbadentry(vdp, ep, entryoffsetinblock))) {
414 int i;
416 ufs_dirbad(dp, results->ulr_offset, "mangled entry");
417 i = dirblksiz - (entryoffsetinblock & (dirblksiz - 1));
418 results->ulr_offset += i;
419 entryoffsetinblock += i;
420 continue;
424 * If an appropriate sized slot has not yet been found,
425 * check to see if one is available. Also accumulate space
426 * in the current block so that we can determine if
427 * compaction is viable.
429 if (slotstatus != FOUND) {
430 int size = fs2h16(ep->e2d_reclen);
432 if (ep->e2d_ino != 0)
433 size -= EXT2FS_DIRSIZ(ep->e2d_namlen);
434 if (size > 0) {
435 if (size >= slotneeded) {
436 slotstatus = FOUND;
437 slotoffset = results->ulr_offset;
438 slotsize = fs2h16(ep->e2d_reclen);
439 } else if (slotstatus == NONE) {
440 slotfreespace += size;
441 if (slotoffset == -1)
442 slotoffset = results->ulr_offset;
443 if (slotfreespace >= slotneeded) {
444 slotstatus = COMPACT;
445 slotsize = results->ulr_offset +
446 fs2h16(ep->e2d_reclen) -
447 slotoffset;
454 * Check for a name match.
456 if (ep->e2d_ino) {
457 namlen = ep->e2d_namlen;
458 if (namlen == cnp->cn_namelen &&
459 !memcmp(cnp->cn_nameptr, ep->e2d_name,
460 (unsigned)namlen)) {
462 * Save directory entry's inode number and
463 * reclen in ndp->ni_ufs area, and release
464 * directory buffer.
466 foundino = fs2h32(ep->e2d_ino);
467 results->ulr_reclen = fs2h16(ep->e2d_reclen);
468 goto found;
471 prevoff = results->ulr_offset;
472 results->ulr_offset += fs2h16(ep->e2d_reclen);
473 entryoffsetinblock += fs2h16(ep->e2d_reclen);
474 if (ep->e2d_ino)
475 enduseful = results->ulr_offset;
477 /* notfound: */
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) {
483 numdirpasses--;
484 results->ulr_offset = 0;
485 endsearch = results->ulr_diroff;
486 goto searchloop;
488 if (bp != NULL)
489 brelse(bp, 0);
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_e2fs_nlink != 0) {
498 * Access for write is interpreted as allowing
499 * creation of files in the directory.
501 error = VOP_ACCESS(vdp, VWRITE, cred);
502 if (error)
503 return (error);
505 * Return an indication of where the new directory
506 * entry should be put. If we didn't find a slot,
507 * then set results->ulr_count to 0 indicating
508 * that the new slot belongs at the end of the
509 * directory. If we found a slot, then the new entry
510 * can be put in the range from results->ulr_offset to
511 * results->ulr_offset + results->ulr_count.
513 if (slotstatus == NONE) {
514 results->ulr_offset = roundup(ext2fs_size(dp), dirblksiz);
515 results->ulr_count = 0;
516 enduseful = results->ulr_offset;
517 } else {
518 results->ulr_offset = slotoffset;
519 results->ulr_count = slotsize;
520 if (enduseful < slotoffset + slotsize)
521 enduseful = slotoffset + slotsize;
523 results->ulr_endoff = roundup(enduseful, dirblksiz);
524 #if 0
525 dp->i_flag |= IN_CHANGE | IN_UPDATE;
526 #endif
528 * We return with the directory locked, so that
529 * the parameters we set up above will still be
530 * valid if we actually decide to do a direnter().
531 * We return ni_vp == NULL to indicate that the entry
532 * does not currently exist; we leave a pointer to
533 * the (locked) directory inode in ndp->ni_dvp.
535 * NB - if the directory is unlocked, then this
536 * information cannot be used.
538 return (EJUSTRETURN);
541 * Insert name into cache (as non-existent) if appropriate.
543 if (nameiop != CREATE) {
544 cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen,
545 cnp->cn_flags);
547 return ENOENT;
549 found:
550 if (numdirpasses == 2)
551 nchstats.ncs_pass2++;
553 * Check that directory length properly reflects presence
554 * of this entry.
556 if (results->ulr_offset + EXT2FS_DIRSIZ(ep->e2d_namlen) > ext2fs_size(dp)) {
557 ufs_dirbad(dp, results->ulr_offset, "i_size too small");
558 error = ext2fs_setsize(dp,
559 results->ulr_offset + EXT2FS_DIRSIZ(ep->e2d_namlen));
560 if (error) {
561 brelse(bp, 0);
562 return (error);
564 dp->i_flag |= IN_CHANGE | IN_UPDATE;
565 uvm_vnp_setsize(vdp, ext2fs_size(dp));
567 brelse(bp, 0);
570 * Found component in pathname.
571 * If the final component of path name, save information
572 * in the cache as to where the entry was found.
574 if ((flags & ISLASTCN) && nameiop == LOOKUP)
575 results->ulr_diroff = results->ulr_offset &~ (dirblksiz - 1);
578 * If deleting, and at end of pathname, return
579 * parameters which can be used to remove file.
580 * Lock the inode, being careful with ".".
582 if (nameiop == DELETE && (flags & ISLASTCN)) {
584 * Return pointer to current entry in results->ulr_offset,
585 * and distance past previous entry (if there
586 * is a previous entry in this block) in results->ulr_count.
587 * Save directory inode pointer in ndp->ni_dvp for dirremove().
589 if ((results->ulr_offset & (dirblksiz - 1)) == 0)
590 results->ulr_count = 0;
591 else
592 results->ulr_count = results->ulr_offset - prevoff;
593 if (dp->i_number == foundino) {
594 vref(vdp);
595 tdp = vdp;
596 } else {
597 if (flags & ISDOTDOT)
598 VOP_UNLOCK(vdp); /* race to get the inode */
599 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
600 if (flags & ISDOTDOT)
601 vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY);
602 if (error)
603 return (error);
606 * Write access to directory required to delete files.
608 if ((error = VOP_ACCESS(vdp, VWRITE, cred)) != 0) {
609 if (dp->i_number == foundino)
610 vrele(tdp);
611 else
612 vput(tdp);
613 return (error);
616 * If directory is "sticky", then user must own
617 * the directory, or the file in it, else she
618 * may not delete it (unless she's root). This
619 * implements append-only directories.
621 if (dp->i_e2fs_mode & ISVTX) {
622 error = kauth_authorize_vnode(cred, KAUTH_VNODE_DELETE,
623 tdp, vdp, genfs_can_sticky(cred, dp->i_uid,
624 VTOI(tdp)->i_uid));
625 if (error) {
626 if (dp->i_number == foundino)
627 vrele(tdp);
628 else
629 vput(tdp);
630 return (EPERM);
633 *vpp = tdp;
634 return (0);
638 * If rewriting (RENAME), return the inode and the
639 * information required to rewrite the present directory
640 * Must get inode of directory entry to verify it's a
641 * regular file, or empty directory.
643 if (nameiop == RENAME && (flags & ISLASTCN)) {
644 error = VOP_ACCESS(vdp, VWRITE, cred);
645 if (error)
646 return (error);
648 * Careful about locking second inode.
649 * This can only occur if the target is ".".
651 if (dp->i_number == foundino)
652 return (EISDIR);
653 if (flags & ISDOTDOT)
654 VOP_UNLOCK(vdp); /* race to get the inode */
655 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
656 if (flags & ISDOTDOT)
657 vn_lock(vdp, LK_EXCLUSIVE | LK_RETRY);
658 if (error)
659 return (error);
660 *vpp = tdp;
661 return (0);
665 * Step through the translation in the name. We do not `vput' the
666 * directory because we may need it again if a symbolic link
667 * is relative to the current directory. Instead we save it
668 * unlocked as "pdp". We must get the target inode before unlocking
669 * the directory to insure that the inode will not be removed
670 * before we get it. We prevent deadlock by always fetching
671 * inodes from the root, moving down the directory tree. Thus
672 * when following backward pointers ".." we must unlock the
673 * parent directory before getting the requested directory.
674 * There is a potential race condition here if both the current
675 * and parent directories are removed before the VFS_VGET for the
676 * inode associated with ".." returns. We hope that this occurs
677 * infrequently since we cannot avoid this race condition without
678 * implementing a sophisticated deadlock detection algorithm.
679 * Note also that this simple deadlock detection scheme will not
680 * work if the file system has any hard links other than ".."
681 * that point backwards in the directory structure.
683 pdp = vdp;
684 if (flags & ISDOTDOT) {
685 VOP_UNLOCK(pdp); /* race to get the inode */
686 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
687 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY);
688 if (error) {
689 return (error);
691 *vpp = tdp;
692 } else if (dp->i_number == foundino) {
693 vref(vdp); /* we want ourself, ie "." */
694 *vpp = vdp;
695 } else {
696 error = VFS_VGET(vdp->v_mount, foundino, &tdp);
697 if (error)
698 return (error);
699 *vpp = tdp;
703 * Insert name into cache if appropriate.
705 cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_flags);
706 return 0;
710 * Do consistency checking on a directory entry:
711 * record length must be multiple of 4
712 * entry must fit in rest of its dirblksize block
713 * record must be large enough to contain entry
714 * name is not longer than EXT2FS_MAXNAMLEN
715 * name must be as long as advertised, and null terminated
718 * changed so that it confirms to ext2fs_check_dir_entry
720 static int
721 ext2fs_dirbadentry(struct vnode *dp, struct ext2fs_direct *de,
722 int entryoffsetinblock)
724 struct ufsmount *ump = VFSTOUFS(dp->v_mount);
725 int dirblksiz = ump->um_dirblksiz;
727 const char *error_msg = NULL;
728 int reclen = fs2h16(de->e2d_reclen);
729 int namlen = de->e2d_namlen;
731 if (reclen < EXT2FS_DIRSIZ(1)) /* e2d_namlen = 1 */
732 error_msg = "rec_len is smaller than minimal";
733 else if (reclen % 4 != 0)
734 error_msg = "rec_len % 4 != 0";
735 else if (namlen > EXT2FS_MAXNAMLEN)
736 error_msg = "namlen > EXT2FS_MAXNAMLEN";
737 else if (reclen < EXT2FS_DIRSIZ(namlen))
738 error_msg = "reclen is too small for name_len";
739 else if (entryoffsetinblock + reclen > dirblksiz)
740 error_msg = "directory entry across blocks";
741 else if (fs2h32(de->e2d_ino) >
742 VTOI(dp)->i_e2fs->e2fs.e2fs_icount)
743 error_msg = "inode out of bounds";
745 if (error_msg != NULL) {
746 printf( "bad directory entry: %s\n"
747 "offset=%d, inode=%lu, rec_len=%d, name_len=%d \n",
748 error_msg, entryoffsetinblock,
749 (unsigned long) fs2h32(de->e2d_ino),
750 reclen, namlen);
751 panic("ext2fs_dirbadentry");
753 return error_msg == NULL ? 0 : 1;
757 * Write a directory entry after a call to namei, using the parameters
758 * that it left in nameidata. The argument ip is the inode which the new
759 * directory entry will refer to. Dvp is a pointer to the directory to
760 * be written, which was left locked by namei. Remaining parameters
761 * (ulr_offset, ulr_count) indicate how the space for the new
762 * entry is to be obtained.
765 ext2fs_direnter(struct inode *ip, struct vnode *dvp,
766 const struct ufs_lookup_results *ulr,
767 struct componentname *cnp)
769 struct ext2fs_direct *ep, *nep;
770 struct inode *dp;
771 struct buf *bp;
772 struct ext2fs_direct newdir;
773 struct iovec aiov;
774 struct uio auio;
775 u_int dsize;
776 int error, loc, newentrysize, spacefree;
777 char *dirbuf;
778 struct ufsmount *ump = VFSTOUFS(dvp->v_mount);
779 int dirblksiz = ump->um_dirblksiz;
781 dp = VTOI(dvp);
783 newdir.e2d_ino = h2fs32(ip->i_number);
784 newdir.e2d_namlen = cnp->cn_namelen;
785 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
786 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
787 newdir.e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
788 } else {
789 newdir.e2d_type = 0;
791 memcpy(newdir.e2d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1);
792 newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen);
793 if (ulr->ulr_count == 0) {
795 * If ulr_count is 0, then namei could find no
796 * space in the directory. Here, ulr_offset will
797 * be on a directory block boundary and we will write the
798 * new entry into a fresh block.
800 if (ulr->ulr_offset & (dirblksiz - 1))
801 panic("ext2fs_direnter: newblk");
802 auio.uio_offset = ulr->ulr_offset;
803 newdir.e2d_reclen = h2fs16(dirblksiz);
804 auio.uio_resid = newentrysize;
805 aiov.iov_len = newentrysize;
806 aiov.iov_base = (void *)&newdir;
807 auio.uio_iov = &aiov;
808 auio.uio_iovcnt = 1;
809 auio.uio_rw = UIO_WRITE;
810 UIO_SETUP_SYSSPACE(&auio);
811 error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred);
812 if (dirblksiz > dvp->v_mount->mnt_stat.f_bsize)
813 /* XXX should grow with balloc() */
814 panic("ext2fs_direnter: frag size");
815 else if (!error) {
816 error = ext2fs_setsize(dp,
817 roundup(ext2fs_size(dp), dirblksiz));
818 if (error)
819 return (error);
820 dp->i_flag |= IN_CHANGE;
821 uvm_vnp_setsize(dvp, ext2fs_size(dp));
823 return (error);
827 * If ulr_count is non-zero, then namei found space
828 * for the new entry in the range ulr_offset to
829 * ulr_offset + ulr_count in the directory.
830 * To use this space, we may have to compact the entries located
831 * there, by copying them together towards the beginning of the
832 * block, leaving the free space in one usable chunk at the end.
836 * Get the block containing the space for the new directory entry.
838 if ((error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset, &dirbuf, &bp)) != 0)
839 return (error);
841 * Find space for the new entry. In the simple case, the entry at
842 * offset base will have the space. If it does not, then namei
843 * arranged that compacting the region ulr_offset to
844 * ulr_offset + ulr_count would yield the
845 * space.
847 ep = (struct ext2fs_direct *)dirbuf;
848 dsize = EXT2FS_DIRSIZ(ep->e2d_namlen);
849 spacefree = fs2h16(ep->e2d_reclen) - dsize;
850 for (loc = fs2h16(ep->e2d_reclen); loc < ulr->ulr_count; ) {
851 nep = (struct ext2fs_direct *)(dirbuf + loc);
852 if (ep->e2d_ino) {
853 /* trim the existing slot */
854 ep->e2d_reclen = h2fs16(dsize);
855 ep = (struct ext2fs_direct *)((char *)ep + dsize);
856 } else {
857 /* overwrite; nothing there; header is ours */
858 spacefree += dsize;
860 dsize = EXT2FS_DIRSIZ(nep->e2d_namlen);
861 spacefree += fs2h16(nep->e2d_reclen) - dsize;
862 loc += fs2h16(nep->e2d_reclen);
863 memcpy((void *)ep, (void *)nep, dsize);
866 * Update the pointer fields in the previous entry (if any),
867 * copy in the new entry, and write out the block.
869 if (ep->e2d_ino == 0) {
870 #ifdef DIAGNOSTIC
871 if (spacefree + dsize < newentrysize)
872 panic("ext2fs_direnter: compact1");
873 #endif
874 newdir.e2d_reclen = h2fs16(spacefree + dsize);
875 } else {
876 #ifdef DIAGNOSTIC
877 if (spacefree < newentrysize) {
878 printf("ext2fs_direnter: compact2 %u %u",
879 (u_int)spacefree, (u_int)newentrysize);
880 panic("ext2fs_direnter: compact2");
882 #endif
883 newdir.e2d_reclen = h2fs16(spacefree);
884 ep->e2d_reclen = h2fs16(dsize);
885 ep = (struct ext2fs_direct *)((char *)ep + dsize);
887 memcpy((void *)ep, (void *)&newdir, (u_int)newentrysize);
888 error = VOP_BWRITE(bp->b_vp, bp);
889 dp->i_flag |= IN_CHANGE | IN_UPDATE;
890 if (!error && ulr->ulr_endoff && ulr->ulr_endoff < ext2fs_size(dp))
891 error = ext2fs_truncate(dvp, (off_t)ulr->ulr_endoff, IO_SYNC,
892 cnp->cn_cred);
893 return (error);
897 * Remove a directory entry after a call to namei, using
898 * the auxiliary results it provided. The entry
899 * ulr_offset contains the offset into the directory of the
900 * entry to be eliminated. The ulr_count field contains the
901 * size of the previous record in the directory. If this
902 * is 0, the first entry is being deleted, so we need only
903 * zero the inode number to mark the entry as free. If the
904 * entry is not the first in the directory, we must reclaim
905 * the space of the now empty record by adding the record size
906 * to the size of the previous entry.
909 ext2fs_dirremove(struct vnode *dvp, const struct ufs_lookup_results *ulr,
910 struct componentname *cnp)
912 struct inode *dp;
913 struct ext2fs_direct *ep;
914 struct buf *bp;
915 int error;
917 dp = VTOI(dvp);
919 if (ulr->ulr_count == 0) {
921 * First entry in block: set d_ino to zero.
923 error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset,
924 (void *)&ep, &bp);
925 if (error != 0)
926 return (error);
927 ep->e2d_ino = 0;
928 error = VOP_BWRITE(bp->b_vp, bp);
929 dp->i_flag |= IN_CHANGE | IN_UPDATE;
930 return (error);
933 * Collapse new free space into previous entry.
935 error = ext2fs_blkatoff(dvp, (off_t)(ulr->ulr_offset - ulr->ulr_count),
936 (void *)&ep, &bp);
937 if (error != 0)
938 return (error);
939 ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + ulr->ulr_reclen);
940 error = VOP_BWRITE(bp->b_vp, bp);
941 dp->i_flag |= IN_CHANGE | IN_UPDATE;
942 return (error);
946 * Rewrite an existing directory entry to point at the inode
947 * supplied. The parameters describing the directory entry are
948 * set up by a call to namei.
951 ext2fs_dirrewrite(struct inode *dp, const struct ufs_lookup_results *ulr,
952 struct inode *ip, struct componentname *cnp)
954 struct buf *bp;
955 struct ext2fs_direct *ep;
956 struct vnode *vdp = ITOV(dp);
957 int error;
959 error = ext2fs_blkatoff(vdp, (off_t)ulr->ulr_offset, (void *)&ep, &bp);
960 if (error != 0)
961 return (error);
962 ep->e2d_ino = h2fs32(ip->i_number);
963 if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 &&
964 (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) {
965 ep->e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode));
966 } else {
967 ep->e2d_type = 0;
969 error = VOP_BWRITE(bp->b_vp, bp);
970 dp->i_flag |= IN_CHANGE | IN_UPDATE;
971 return (error);
975 * Check if a directory is empty or not.
976 * Inode supplied must be locked.
978 * Using a struct dirtemplate here is not precisely
979 * what we want, but better than using a struct ext2fs_direct.
981 * NB: does not handle corrupted directories.
984 ext2fs_dirempty(struct inode *ip, ino_t parentino, kauth_cred_t cred)
986 off_t off;
987 struct ext2fs_dirtemplate dbuf;
988 struct ext2fs_direct *dp = (struct ext2fs_direct *)&dbuf;
989 int error, namlen;
990 size_t count;
992 #define MINDIRSIZ (sizeof (struct ext2fs_dirtemplate) / 2)
994 for (off = 0; off < ext2fs_size(ip); off += fs2h16(dp->e2d_reclen)) {
995 error = vn_rdwr(UIO_READ, ITOV(ip), (void *)dp, MINDIRSIZ, off,
996 UIO_SYSSPACE, IO_NODELOCKED, cred, &count, NULL);
998 * Since we read MINDIRSIZ, residual must
999 * be 0 unless we're at end of file.
1001 if (error || count != 0)
1002 return (0);
1003 /* avoid infinite loops */
1004 if (dp->e2d_reclen == 0)
1005 return (0);
1006 /* skip empty entries */
1007 if (dp->e2d_ino == 0)
1008 continue;
1009 /* accept only "." and ".." */
1010 namlen = dp->e2d_namlen;
1011 if (namlen > 2)
1012 return (0);
1013 if (dp->e2d_name[0] != '.')
1014 return (0);
1016 * At this point namlen must be 1 or 2.
1017 * 1 implies ".", 2 implies ".." if second
1018 * char is also "."
1020 if (namlen == 1)
1021 continue;
1022 if (dp->e2d_name[1] == '.' && fs2h32(dp->e2d_ino) == parentino)
1023 continue;
1024 return (0);
1026 return (1);
1030 * Check if source directory is in the path of the target directory.
1031 * Target is supplied locked, source is unlocked.
1032 * The target is always vput before returning.
1035 ext2fs_checkpath(struct inode *source, struct inode *target,
1036 kauth_cred_t cred)
1038 struct vnode *vp;
1039 int error, rootino, namlen;
1040 struct ext2fs_dirtemplate dirbuf;
1041 uint32_t ino;
1043 vp = ITOV(target);
1044 if (target->i_number == source->i_number) {
1045 error = EEXIST;
1046 goto out;
1048 rootino = UFS_ROOTINO;
1049 error = 0;
1050 if (target->i_number == rootino)
1051 goto out;
1053 for (;;) {
1054 if (vp->v_type != VDIR) {
1055 error = ENOTDIR;
1056 break;
1058 error = vn_rdwr(UIO_READ, vp, (void *)&dirbuf,
1059 sizeof (struct ext2fs_dirtemplate), (off_t)0,
1060 UIO_SYSSPACE, IO_NODELOCKED, cred, (size_t *)0,
1061 NULL);
1062 if (error != 0)
1063 break;
1064 namlen = dirbuf.dotdot_namlen;
1065 if (namlen != 2 ||
1066 dirbuf.dotdot_name[0] != '.' ||
1067 dirbuf.dotdot_name[1] != '.') {
1068 error = ENOTDIR;
1069 break;
1071 ino = fs2h32(dirbuf.dotdot_ino);
1072 if (ino == source->i_number) {
1073 error = EINVAL;
1074 break;
1076 if (ino == rootino)
1077 break;
1078 vput(vp);
1079 error = VFS_VGET(vp->v_mount, ino, &vp);
1080 if (error != 0) {
1081 vp = NULL;
1082 break;
1086 out:
1087 if (error == ENOTDIR) {
1088 printf("checkpath: .. not a directory\n");
1089 panic("checkpath");
1091 if (vp != NULL)
1092 vput(vp);
1093 return (error);