]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/ext2fs/ext2_lookup.c
Update to bmake-201802222
[FreeBSD/FreeBSD.git] / sys / fs / ext2fs / ext2_lookup.c
1 /*-
2  *  modified for Lites 1.1
3  *
4  *  Aug 1995, Godmar Back (gback@cs.utah.edu)
5  *  University of Utah, Department of Computer Science
6  */
7 /*-
8  * SPDX-License-Identifier: BSD-3-Clause
9  *
10  * Copyright (c) 1989, 1993
11  *      The Regents of the University of California.  All rights reserved.
12  * (c) UNIX System Laboratories, Inc.
13  * All or some portions of this file are derived from material licensed
14  * to the University of California by American Telephone and Telegraph
15  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
16  * the permission of UNIX System Laboratories, Inc.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the University nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  *
42  *      @(#)ufs_lookup.c        8.6 (Berkeley) 4/1/94
43  * $FreeBSD$
44  */
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/bio.h>
50 #include <sys/buf.h>
51 #include <sys/endian.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <sys/sysctl.h>
57
58 #include <ufs/ufs/dir.h>
59
60 #include <fs/ext2fs/inode.h>
61 #include <fs/ext2fs/ext2_mount.h>
62 #include <fs/ext2fs/ext2fs.h>
63 #include <fs/ext2fs/ext2_dinode.h>
64 #include <fs/ext2fs/ext2_dir.h>
65 #include <fs/ext2fs/ext2_extern.h>
66 #include <fs/ext2fs/fs.h>
67
68 #ifdef INVARIANTS
69 static int dirchk = 1;
70 #else
71 static int dirchk = 0;
72 #endif
73
74 static SYSCTL_NODE(_vfs, OID_AUTO, e2fs, CTLFLAG_RD, 0, "EXT2FS filesystem");
75 SYSCTL_INT(_vfs_e2fs, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
76
77 /*
78    DIRBLKSIZE in ffs is DEV_BSIZE (in most cases 512)
79    while it is the native blocksize in ext2fs - thus, a #define
80    is no longer appropriate
81 */
82 #undef  DIRBLKSIZ
83
84 static u_char ext2_ft_to_dt[] = {
85         DT_UNKNOWN,             /* EXT2_FT_UNKNOWN */
86         DT_REG,                 /* EXT2_FT_REG_FILE */
87         DT_DIR,                 /* EXT2_FT_DIR */
88         DT_CHR,                 /* EXT2_FT_CHRDEV */
89         DT_BLK,                 /* EXT2_FT_BLKDEV */
90         DT_FIFO,                /* EXT2_FT_FIFO */
91         DT_SOCK,                /* EXT2_FT_SOCK */
92         DT_LNK,                 /* EXT2_FT_SYMLINK */
93 };
94 #define FTTODT(ft) \
95     ((ft) < nitems(ext2_ft_to_dt) ? ext2_ft_to_dt[(ft)] : DT_UNKNOWN)
96
97 static u_char dt_to_ext2_ft[] = {
98         EXT2_FT_UNKNOWN,        /* DT_UNKNOWN */
99         EXT2_FT_FIFO,           /* DT_FIFO */
100         EXT2_FT_CHRDEV,         /* DT_CHR */
101         EXT2_FT_UNKNOWN,        /* unused */
102         EXT2_FT_DIR,            /* DT_DIR */
103         EXT2_FT_UNKNOWN,        /* unused */
104         EXT2_FT_BLKDEV,         /* DT_BLK */
105         EXT2_FT_UNKNOWN,        /* unused */
106         EXT2_FT_REG_FILE,       /* DT_REG */
107         EXT2_FT_UNKNOWN,        /* unused */
108         EXT2_FT_SYMLINK,        /* DT_LNK */
109         EXT2_FT_UNKNOWN,        /* unused */
110         EXT2_FT_SOCK,           /* DT_SOCK */
111         EXT2_FT_UNKNOWN,        /* unused */
112         EXT2_FT_UNKNOWN,        /* DT_WHT */
113 };
114 #define DTTOFT(dt) \
115     ((dt) < nitems(dt_to_ext2_ft) ? dt_to_ext2_ft[(dt)] : EXT2_FT_UNKNOWN)
116
117 static int      ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
118                     int entryoffsetinblock);
119 static int      ext2_is_dot_entry(struct componentname *cnp);
120 static int      ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp,
121                     struct componentname *cnp, ino_t *dd_ino);
122
123 static int
124 ext2_is_dot_entry(struct componentname *cnp)
125 {
126         if (cnp->cn_namelen <= 2 && cnp->cn_nameptr[0] == '.' &&
127             (cnp->cn_nameptr[1] == '.' || cnp->cn_nameptr[1] == '\0'))
128                 return (1);
129         return (0);
130 }
131
132 /*
133  * Vnode op for reading directories.
134  */
135 int
136 ext2_readdir(struct vop_readdir_args *ap)
137 {
138         struct vnode *vp = ap->a_vp;
139         struct uio *uio = ap->a_uio;
140         struct buf *bp;
141         struct inode *ip;
142         struct ext2fs_direct_2 *dp, *edp;
143         u_long *cookies;
144         struct dirent dstdp;
145         off_t offset, startoffset;
146         size_t readcnt, skipcnt;
147         ssize_t startresid;
148         u_int ncookies;
149         int DIRBLKSIZ = VTOI(ap->a_vp)->i_e2fs->e2fs_bsize;
150         int error;
151
152         if (uio->uio_offset < 0)
153                 return (EINVAL);
154         ip = VTOI(vp);
155         if (ap->a_ncookies != NULL) {
156                 if (uio->uio_resid < 0)
157                         ncookies = 0;
158                 else
159                         ncookies = uio->uio_resid;
160                 if (uio->uio_offset >= ip->i_size)
161                         ncookies = 0;
162                 else if (ip->i_size - uio->uio_offset < ncookies)
163                         ncookies = ip->i_size - uio->uio_offset;
164                 ncookies = ncookies / (offsetof(struct ext2fs_direct_2,
165                     e2d_namlen) + 4) + 1;
166                 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
167                 *ap->a_ncookies = ncookies;
168                 *ap->a_cookies = cookies;
169         } else {
170                 ncookies = 0;
171                 cookies = NULL;
172         }
173         offset = startoffset = uio->uio_offset;
174         startresid = uio->uio_resid;
175         error = 0;
176         while (error == 0 && uio->uio_resid > 0 &&
177             uio->uio_offset < ip->i_size) {
178                 error = ext2_blkatoff(vp, uio->uio_offset, NULL, &bp);
179                 if (error)
180                         break;
181                 if (bp->b_offset + bp->b_bcount > ip->i_size)
182                         readcnt = ip->i_size - bp->b_offset;
183                 else
184                         readcnt = bp->b_bcount;
185                 skipcnt = (size_t)(uio->uio_offset - bp->b_offset) &
186                     ~(size_t)(DIRBLKSIZ - 1);
187                 offset = bp->b_offset + skipcnt;
188                 dp = (struct ext2fs_direct_2 *)&bp->b_data[skipcnt];
189                 edp = (struct ext2fs_direct_2 *)&bp->b_data[readcnt];
190                 while (error == 0 && uio->uio_resid > 0 && dp < edp) {
191                         if (dp->e2d_reclen <= offsetof(struct ext2fs_direct_2,
192                             e2d_namlen) || (caddr_t)dp + dp->e2d_reclen >
193                             (caddr_t)edp) {
194                                 error = EIO;
195                                 break;
196                         }
197                         /*-
198                          * "New" ext2fs directory entries differ in 3 ways
199                          * from ufs on-disk ones:
200                          * - the name is not necessarily NUL-terminated.
201                          * - the file type field always exists and always
202                          *   follows the name length field.
203                          * - the file type is encoded in a different way.
204                          *
205                          * "Old" ext2fs directory entries need no special
206                          * conversions, since they are binary compatible
207                          * with "new" entries having a file type of 0 (i.e.,
208                          * EXT2_FT_UNKNOWN).  Splitting the old name length
209                          * field didn't make a mess like it did in ufs,
210                          * because ext2fs uses a machine-independent disk
211                          * layout.
212                          */
213                         dstdp.d_namlen = dp->e2d_namlen;
214                         dstdp.d_type = FTTODT(dp->e2d_type);
215                         if (offsetof(struct ext2fs_direct_2, e2d_namlen) +
216                             dstdp.d_namlen > dp->e2d_reclen) {
217                                 error = EIO;
218                                 break;
219                         }
220                         if (offset < startoffset || dp->e2d_ino == 0)
221                                 goto nextentry;
222                         dstdp.d_fileno = dp->e2d_ino;
223                         dstdp.d_reclen = GENERIC_DIRSIZ(&dstdp);
224                         bcopy(dp->e2d_name, dstdp.d_name, dstdp.d_namlen);
225                         dstdp.d_name[dstdp.d_namlen] = '\0';
226                         if (dstdp.d_reclen > uio->uio_resid) {
227                                 if (uio->uio_resid == startresid)
228                                         error = EINVAL;
229                                 else
230                                         error = EJUSTRETURN;
231                                 break;
232                         }
233                         /* Advance dp. */
234                         error = uiomove((caddr_t)&dstdp, dstdp.d_reclen, uio);
235                         if (error)
236                                 break;
237                         if (cookies != NULL) {
238                                 KASSERT(ncookies > 0,
239                                     ("ext2_readdir: cookies buffer too small"));
240                                 *cookies = offset + dp->e2d_reclen;
241                                 cookies++;
242                                 ncookies--;
243                         }
244 nextentry:
245                         offset += dp->e2d_reclen;
246                         dp = (struct ext2fs_direct_2 *)((caddr_t)dp +
247                             dp->e2d_reclen);
248                 }
249                 bqrelse(bp);
250                 uio->uio_offset = offset;
251         }
252         /* We need to correct uio_offset. */
253         uio->uio_offset = offset;
254         if (error == EJUSTRETURN)
255                 error = 0;
256         if (ap->a_ncookies != NULL) {
257                 if (error == 0) {
258                         ap->a_ncookies -= ncookies;
259                 } else {
260                         free(*ap->a_cookies, M_TEMP);
261                         *ap->a_ncookies = 0;
262                         *ap->a_cookies = NULL;
263                 }
264         }
265         if (error == 0 && ap->a_eofflag)
266                 *ap->a_eofflag = ip->i_size <= uio->uio_offset;
267         return (error);
268 }
269
270 /*
271  * Convert a component of a pathname into a pointer to a locked inode.
272  * This is a very central and rather complicated routine.
273  * If the file system is not maintained in a strict tree hierarchy,
274  * this can result in a deadlock situation (see comments in code below).
275  *
276  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
277  * on whether the name is to be looked up, created, renamed, or deleted.
278  * When CREATE, RENAME, or DELETE is specified, information usable in
279  * creating, renaming, or deleting a directory entry may be calculated.
280  * If flag has LOCKPARENT or'ed into it and the target of the pathname
281  * exists, lookup returns both the target and its parent directory locked.
282  * When creating or renaming and LOCKPARENT is specified, the target may
283  * not be ".".  When deleting and LOCKPARENT is specified, the target may
284  * be "."., but the caller must check to ensure it does an vrele and vput
285  * instead of two vputs.
286  *
287  * Overall outline of ext2_lookup:
288  *
289  *      search for name in directory, to found or notfound
290  * notfound:
291  *      if creating, return locked directory, leaving info on available slots
292  *      else return error
293  * found:
294  *      if at end of path and deleting, return information to allow delete
295  *      if at end of path and rewriting (RENAME and LOCKPARENT), lock target
296  *        inode and return info to allow rewrite
297  *      if not at end, add name to cache; if at end and neither creating
298  *        nor deleting, add name to cache
299  */
300 int
301 ext2_lookup(struct vop_cachedlookup_args *ap)
302 {
303
304         return (ext2_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL));
305 }
306
307 static int
308 ext2_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname *cnp,
309     ino_t *dd_ino)
310 {
311         struct inode *dp;               /* inode for directory being searched */
312         struct buf *bp;                 /* a buffer of directory entries */
313         struct ext2fs_direct_2 *ep;     /* the current directory entry */
314         int entryoffsetinblock;         /* offset of ep in bp's buffer */
315         struct ext2fs_searchslot ss;
316         doff_t i_diroff;                /* cached i_diroff value */
317         doff_t i_offset;                /* cached i_offset value */
318         int numdirpasses;               /* strategy for directory search */
319         doff_t endsearch;               /* offset to end directory search */
320         doff_t prevoff;                 /* prev entry dp->i_offset */
321         struct vnode *pdp;              /* saved dp during symlink work */
322         struct vnode *tdp;              /* returned by VFS_VGET */
323         doff_t enduseful;               /* pointer past last used dir slot */
324         u_long bmask;                   /* block offset mask */
325         int error;
326         struct ucred *cred = cnp->cn_cred;
327         int flags = cnp->cn_flags;
328         int nameiop = cnp->cn_nameiop;
329         ino_t ino, ino1;
330         int ltype;
331         int entry_found = 0;
332
333         int DIRBLKSIZ = VTOI(vdp)->i_e2fs->e2fs_bsize;
334
335         if (vpp != NULL)
336                 *vpp = NULL;
337
338         dp = VTOI(vdp);
339         bmask = VFSTOEXT2(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
340 restart:
341         bp = NULL;
342         ss.slotoffset = -1;
343
344         /*
345          * We now have a segment name to search for, and a directory to search.
346          *
347          * Suppress search for slots unless creating
348          * file and at end of pathname, in which case
349          * we watch for a place to put the new file in
350          * case it doesn't already exist.
351          */
352         i_diroff = dp->i_diroff;
353         ss.slotstatus = FOUND;
354         ss.slotfreespace = ss.slotsize = ss.slotneeded = 0;
355         if ((nameiop == CREATE || nameiop == RENAME) &&
356             (flags & ISLASTCN)) {
357                 ss.slotstatus = NONE;
358                 ss.slotneeded = EXT2_DIR_REC_LEN(cnp->cn_namelen);
359                 /*
360                  * was ss.slotneeded = (sizeof(struct direct) - MAXNAMLEN +
361                  * cnp->cn_namelen + 3) &~ 3;
362                  */
363         }
364         /*
365          * Try to lookup dir entry using htree directory index.
366          *
367          * If we got an error or we want to find '.' or '..' entry,
368          * we will fall back to linear search.
369          */
370         if (!ext2_is_dot_entry(cnp) && ext2_htree_has_idx(dp)) {
371                 numdirpasses = 1;
372                 entryoffsetinblock = 0;
373                 switch (ext2_htree_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
374                     &bp, &entryoffsetinblock, &i_offset, &prevoff,
375                     &enduseful, &ss)) {
376                 case 0:
377                         ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
378                             (i_offset & bmask));
379                         goto foundentry;
380                 case ENOENT:
381                         i_offset = roundup2(dp->i_size, DIRBLKSIZ);
382                         goto notfound;
383                 default:
384                         /*
385                          * Something failed; just fallback to do a linear
386                          * search.
387                          */
388                         break;
389                 }
390         }
391
392         /*
393          * If there is cached information on a previous search of
394          * this directory, pick up where we last left off.
395          * We cache only lookups as these are the most common
396          * and have the greatest payoff. Caching CREATE has little
397          * benefit as it usually must search the entire directory
398          * to determine that the entry does not exist. Caching the
399          * location of the last DELETE or RENAME has not reduced
400          * profiling time and hence has been removed in the interest
401          * of simplicity.
402          */
403         if (nameiop != LOOKUP || i_diroff == 0 ||
404             i_diroff > dp->i_size) {
405                 entryoffsetinblock = 0;
406                 i_offset = 0;
407                 numdirpasses = 1;
408         } else {
409                 i_offset = i_diroff;
410                 if ((entryoffsetinblock = i_offset & bmask) &&
411                     (error = ext2_blkatoff(vdp, (off_t)i_offset, NULL,
412                     &bp)))
413                         return (error);
414                 numdirpasses = 2;
415                 nchstats.ncs_2passes++;
416         }
417         prevoff = i_offset;
418         endsearch = roundup2(dp->i_size, DIRBLKSIZ);
419         enduseful = 0;
420
421 searchloop:
422         while (i_offset < endsearch) {
423                 /*
424                  * If necessary, get the next directory block.
425                  */
426                 if (bp != NULL)
427                         brelse(bp);
428                 error = ext2_blkatoff(vdp, (off_t)i_offset, NULL, &bp);
429                 if (error != 0)
430                         return (error);
431                 entryoffsetinblock = 0;
432                 /*
433                  * If still looking for a slot, and at a DIRBLKSIZE
434                  * boundary, have to start looking for free space again.
435                  */
436                 if (ss.slotstatus == NONE &&
437                     (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
438                         ss.slotoffset = -1;
439                         ss.slotfreespace = 0;
440                 }
441                 error = ext2_search_dirblock(dp, bp->b_data, &entry_found,
442                     cnp->cn_nameptr, cnp->cn_namelen,
443                     &entryoffsetinblock, &i_offset, &prevoff,
444                     &enduseful, &ss);
445                 if (error != 0) {
446                         brelse(bp);
447                         return (error);
448                 }
449                 if (entry_found) {
450                         ep = (struct ext2fs_direct_2 *)((char *)bp->b_data +
451                             (entryoffsetinblock & bmask));
452 foundentry:
453                         ino = ep->e2d_ino;
454                         goto found;
455                 }
456         }
457 notfound:
458         /*
459          * If we started in the middle of the directory and failed
460          * to find our target, we must check the beginning as well.
461          */
462         if (numdirpasses == 2) {
463                 numdirpasses--;
464                 i_offset = 0;
465                 endsearch = i_diroff;
466                 goto searchloop;
467         }
468         if (bp != NULL)
469                 brelse(bp);
470         /*
471          * If creating, and at end of pathname and current
472          * directory has not been removed, then can consider
473          * allowing file to be created.
474          */
475         if ((nameiop == CREATE || nameiop == RENAME) &&
476             (flags & ISLASTCN) && dp->i_nlink != 0) {
477                 /*
478                  * Access for write is interpreted as allowing
479                  * creation of files in the directory.
480                  */
481                 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
482                         return (error);
483                 /*
484                  * Return an indication of where the new directory
485                  * entry should be put.  If we didn't find a slot,
486                  * then set dp->i_count to 0 indicating
487                  * that the new slot belongs at the end of the
488                  * directory. If we found a slot, then the new entry
489                  * can be put in the range from dp->i_offset to
490                  * dp->i_offset + dp->i_count.
491                  */
492                 if (ss.slotstatus == NONE) {
493                         dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
494                         dp->i_count = 0;
495                         enduseful = dp->i_offset;
496                 } else {
497                         dp->i_offset = ss.slotoffset;
498                         dp->i_count = ss.slotsize;
499                         if (enduseful < ss.slotoffset + ss.slotsize)
500                                 enduseful = ss.slotoffset + ss.slotsize;
501                 }
502                 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
503                 /*
504                  * We return with the directory locked, so that
505                  * the parameters we set up above will still be
506                  * valid if we actually decide to do a direnter().
507                  * We return ni_vp == NULL to indicate that the entry
508                  * does not currently exist; we leave a pointer to
509                  * the (locked) directory inode in ndp->ni_dvp.
510                  * The pathname buffer is saved so that the name
511                  * can be obtained later.
512                  *
513                  * NB - if the directory is unlocked, then this
514                  * information cannot be used.
515                  */
516                 cnp->cn_flags |= SAVENAME;
517                 return (EJUSTRETURN);
518         }
519         /*
520          * Insert name into cache (as non-existent) if appropriate.
521          */
522         if ((cnp->cn_flags & MAKEENTRY) != 0)
523                 cache_enter(vdp, NULL, cnp);
524         return (ENOENT);
525
526 found:
527         if (dd_ino != NULL)
528                 *dd_ino = ino;
529         if (numdirpasses == 2)
530                 nchstats.ncs_pass2++;
531         /*
532          * Check that directory length properly reflects presence
533          * of this entry.
534          */
535         if (entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen)
536             > dp->i_size) {
537                 ext2_dirbad(dp, i_offset, "i_size too small");
538                 dp->i_size = entryoffsetinblock + EXT2_DIR_REC_LEN(ep->e2d_namlen);
539                 dp->i_flag |= IN_CHANGE | IN_UPDATE;
540         }
541         brelse(bp);
542
543         /*
544          * Found component in pathname.
545          * If the final component of path name, save information
546          * in the cache as to where the entry was found.
547          */
548         if ((flags & ISLASTCN) && nameiop == LOOKUP)
549                 dp->i_diroff = rounddown2(i_offset, DIRBLKSIZ);
550         /*
551          * If deleting, and at end of pathname, return
552          * parameters which can be used to remove file.
553          */
554         if (nameiop == DELETE && (flags & ISLASTCN)) {
555                 if (flags & LOCKPARENT)
556                         ASSERT_VOP_ELOCKED(vdp, __FUNCTION__);
557                 /*
558                  * Write access to directory required to delete files.
559                  */
560                 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
561                         return (error);
562                 /*
563                  * Return pointer to current entry in dp->i_offset,
564                  * and distance past previous entry (if there
565                  * is a previous entry in this block) in dp->i_count.
566                  * Save directory inode pointer in ndp->ni_dvp for dirremove().
567                  *
568                  * Technically we shouldn't be setting these in the
569                  * WANTPARENT case (first lookup in rename()), but any
570                  * lookups that will result in directory changes will
571                  * overwrite these.
572                  */
573                 dp->i_offset = i_offset;
574                 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
575                         dp->i_count = 0;
576                 else
577                         dp->i_count = dp->i_offset - prevoff;
578                 if (dd_ino != NULL)
579                         return (0);
580                 if (dp->i_number == ino) {
581                         VREF(vdp);
582                         *vpp = vdp;
583                         return (0);
584                 }
585                 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
586                     &tdp)) != 0)
587                         return (error);
588                 /*
589                  * If directory is "sticky", then user must own
590                  * the directory, or the file in it, else she
591                  * may not delete it (unless she's root). This
592                  * implements append-only directories.
593                  */
594                 if ((dp->i_mode & ISVTX) &&
595                     cred->cr_uid != 0 &&
596                     cred->cr_uid != dp->i_uid &&
597                     VTOI(tdp)->i_uid != cred->cr_uid) {
598                         vput(tdp);
599                         return (EPERM);
600                 }
601                 *vpp = tdp;
602                 return (0);
603         }
604
605         /*
606          * If rewriting (RENAME), return the inode and the
607          * information required to rewrite the present directory
608          * Must get inode of directory entry to verify it's a
609          * regular file, or empty directory.
610          */
611         if (nameiop == RENAME && (flags & ISLASTCN)) {
612                 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_thread)) != 0)
613                         return (error);
614                 /*
615                  * Careful about locking second inode.
616                  * This can only occur if the target is ".".
617                  */
618                 dp->i_offset = i_offset;
619                 if (dp->i_number == ino)
620                         return (EISDIR);
621                 if (dd_ino != NULL)
622                         return (0);
623                 if ((error = VFS_VGET(vdp->v_mount, ino, LK_EXCLUSIVE,
624                     &tdp)) != 0)
625                         return (error);
626                 *vpp = tdp;
627                 cnp->cn_flags |= SAVENAME;
628                 return (0);
629         }
630         if (dd_ino != NULL)
631                 return (0);
632
633         /*
634          * Step through the translation in the name.  We do not `vput' the
635          * directory because we may need it again if a symbolic link
636          * is relative to the current directory.  Instead we save it
637          * unlocked as "pdp".  We must get the target inode before unlocking
638          * the directory to insure that the inode will not be removed
639          * before we get it.  We prevent deadlock by always fetching
640          * inodes from the root, moving down the directory tree. Thus
641          * when following backward pointers ".." we must unlock the
642          * parent directory before getting the requested directory.
643          * There is a potential race condition here if both the current
644          * and parent directories are removed before the VFS_VGET for the
645          * inode associated with ".." returns.  We hope that this occurs
646          * infrequently since we cannot avoid this race condition without
647          * implementing a sophisticated deadlock detection algorithm.
648          * Note also that this simple deadlock detection scheme will not
649          * work if the file system has any hard links other than ".."
650          * that point backwards in the directory structure.
651          */
652         pdp = vdp;
653         if (flags & ISDOTDOT) {
654                 error = vn_vget_ino(pdp, ino, cnp->cn_lkflags, &tdp);
655                 if (pdp->v_iflag & VI_DOOMED) {
656                         if (error == 0)
657                                 vput(tdp);
658                         error = ENOENT;
659                 }
660                 if (error)
661                         return (error);
662                 /*
663                  * Recheck that ".." entry in the vdp directory points
664                  * to the inode we looked up before vdp lock was
665                  * dropped.
666                  */
667                 error = ext2_lookup_ino(pdp, NULL, cnp, &ino1);
668                 if (error) {
669                         vput(tdp);
670                         return (error);
671                 }
672                 if (ino1 != ino) {
673                         vput(tdp);
674                         goto restart;
675                 }
676                 *vpp = tdp;
677         } else if (dp->i_number == ino) {
678                 VREF(vdp);      /* we want ourself, ie "." */
679                 /*
680                  * When we lookup "." we still can be asked to lock it
681                  * differently.
682                  */
683                 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
684                 if (ltype != VOP_ISLOCKED(vdp)) {
685                         if (ltype == LK_EXCLUSIVE)
686                                 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
687                         else    /* if (ltype == LK_SHARED) */
688                                 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
689                 }
690                 *vpp = vdp;
691         } else {
692                 if ((error = VFS_VGET(vdp->v_mount, ino, cnp->cn_lkflags,
693                     &tdp)) != 0)
694                         return (error);
695                 *vpp = tdp;
696         }
697
698         /*
699          * Insert name into cache if appropriate.
700          */
701         if (cnp->cn_flags & MAKEENTRY)
702                 cache_enter(vdp, *vpp, cnp);
703         return (0);
704 }
705
706 int
707 ext2_search_dirblock(struct inode *ip, void *data, int *foundp,
708     const char *name, int namelen, int *entryoffsetinblockp,
709     doff_t *offp, doff_t *prevoffp, doff_t *endusefulp,
710     struct ext2fs_searchslot *ssp)
711 {
712         struct vnode *vdp;
713         struct ext2fs_direct_2 *ep, *top;
714         uint32_t bsize = ip->i_e2fs->e2fs_bsize;
715         int offset = *entryoffsetinblockp;
716         int namlen;
717
718         vdp = ITOV(ip);
719
720         ep = (struct ext2fs_direct_2 *)((char *)data + offset);
721         top = (struct ext2fs_direct_2 *)((char *)data +
722             bsize - EXT2_DIR_REC_LEN(0));
723
724         while (ep < top) {
725                 /*
726                  * Full validation checks are slow, so we only check
727                  * enough to insure forward progress through the
728                  * directory. Complete checks can be run by setting
729                  * "vfs.e2fs.dirchk" to be true.
730                  */
731                 if (ep->e2d_reclen == 0 ||
732                     (dirchk && ext2_dirbadentry(vdp, ep, offset))) {
733                         int i;
734
735                         ext2_dirbad(ip, *offp, "mangled entry");
736                         i = bsize - (offset & (bsize - 1));
737                         *offp += i;
738                         offset += i;
739                         continue;
740                 }
741
742                 /*
743                  * If an appropriate sized slot has not yet been found,
744                  * check to see if one is available. Also accumulate space
745                  * in the current block so that we can determine if
746                  * compaction is viable.
747                  */
748                 if (ssp->slotstatus != FOUND) {
749                         int size = ep->e2d_reclen;
750
751                         if (ep->e2d_ino != 0)
752                                 size -= EXT2_DIR_REC_LEN(ep->e2d_namlen);
753                         if (size > 0) {
754                                 if (size >= ssp->slotneeded) {
755                                         ssp->slotstatus = FOUND;
756                                         ssp->slotoffset = *offp;
757                                         ssp->slotsize = ep->e2d_reclen;
758                                 } else if (ssp->slotstatus == NONE) {
759                                         ssp->slotfreespace += size;
760                                         if (ssp->slotoffset == -1)
761                                                 ssp->slotoffset = *offp;
762                                         if (ssp->slotfreespace >= ssp->slotneeded) {
763                                                 ssp->slotstatus = COMPACT;
764                                                 ssp->slotsize = *offp +
765                                                     ep->e2d_reclen -
766                                                     ssp->slotoffset;
767                                         }
768                                 }
769                         }
770                 }
771                 /*
772                  * Check for a name match.
773                  */
774                 if (ep->e2d_ino) {
775                         namlen = ep->e2d_namlen;
776                         if (namlen == namelen &&
777                             !bcmp(name, ep->e2d_name, (unsigned)namlen)) {
778                                 /*
779                                  * Save directory entry's inode number and
780                                  * reclen in ndp->ni_ufs area, and release
781                                  * directory buffer.
782                                  */
783                                 *foundp = 1;
784                                 return (0);
785                         }
786                 }
787                 *prevoffp = *offp;
788                 *offp += ep->e2d_reclen;
789                 offset += ep->e2d_reclen;
790                 *entryoffsetinblockp = offset;
791                 if (ep->e2d_ino)
792                         *endusefulp = *offp;
793                 /*
794                  * Get pointer to the next entry.
795                  */
796                 ep = (struct ext2fs_direct_2 *)((char *)data + offset);
797         }
798
799         return (0);
800 }
801
802 void
803 ext2_dirbad(struct inode *ip, doff_t offset, char *how)
804 {
805         struct mount *mp;
806
807         mp = ITOV(ip)->v_mount;
808         if ((mp->mnt_flag & MNT_RDONLY) == 0)
809                 panic("ext2_dirbad: %s: bad dir ino %ju at offset %ld: %s\n",
810                     mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number,
811                     (long)offset, how);
812         else
813                 (void)printf("%s: bad dir ino %ju at offset %ld: %s\n",
814                     mp->mnt_stat.f_mntonname, (uintmax_t)ip->i_number,
815                     (long)offset, how);
816
817 }
818
819 /*
820  * Do consistency checking on a directory entry:
821  *      record length must be multiple of 4
822  *      entry must fit in rest of its DIRBLKSIZ block
823  *      record must be large enough to contain entry
824  *      name is not longer than MAXNAMLEN
825  *      name must be as long as advertised, and null terminated
826  */
827 /*
828  *      changed so that it confirms to ext2_check_dir_entry
829  */
830 static int
831 ext2_dirbadentry(struct vnode *dp, struct ext2fs_direct_2 *de,
832     int entryoffsetinblock)
833 {
834         int DIRBLKSIZ = VTOI(dp)->i_e2fs->e2fs_bsize;
835
836         char *error_msg = NULL;
837
838         if (de->e2d_reclen < EXT2_DIR_REC_LEN(1))
839                 error_msg = "rec_len is smaller than minimal";
840         else if (de->e2d_reclen % 4 != 0)
841                 error_msg = "rec_len % 4 != 0";
842         else if (de->e2d_reclen < EXT2_DIR_REC_LEN(de->e2d_namlen))
843                 error_msg = "reclen is too small for name_len";
844         else if (entryoffsetinblock + de->e2d_reclen > DIRBLKSIZ)
845                 error_msg = "directory entry across blocks";
846         /* else LATER
847              if (de->inode > dir->i_sb->u.ext2_sb.s_es->s_inodes_count)
848                 error_msg = "inode out of bounds";
849         */
850
851         if (error_msg != NULL) {
852                 printf("bad directory entry: %s\n", error_msg);
853                 printf("offset=%d, inode=%lu, rec_len=%u, name_len=%u\n",
854                         entryoffsetinblock, (unsigned long)de->e2d_ino,
855                         de->e2d_reclen, de->e2d_namlen);
856         }
857         return error_msg == NULL ? 0 : 1;
858 }
859
860 /*
861  * Write a directory entry after a call to namei, using the parameters
862  * that it left in nameidata.  The argument ip is the inode which the new
863  * directory entry will refer to.  Dvp is a pointer to the directory to
864  * be written, which was left locked by namei. Remaining parameters
865  * (dp->i_offset, dp->i_count) indicate how the space for the new
866  * entry is to be obtained.
867  */
868 int
869 ext2_direnter(struct inode *ip, struct vnode *dvp, struct componentname *cnp)
870 {
871         struct inode *dp;
872         struct ext2fs_direct_2 newdir;
873         struct buf *bp;
874         int DIRBLKSIZ = ip->i_e2fs->e2fs_bsize;
875         int error;
876
877
878 #ifdef INVARIANTS
879         if ((cnp->cn_flags & SAVENAME) == 0)
880                 panic("ext2_direnter: missing name");
881 #endif
882         dp = VTOI(dvp);
883         newdir.e2d_ino = ip->i_number;
884         newdir.e2d_namlen = cnp->cn_namelen;
885         if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
886             EXT2F_INCOMPAT_FTYPE))
887                 newdir.e2d_type = DTTOFT(IFTODT(ip->i_mode));
888         else
889                 newdir.e2d_type = EXT2_FT_UNKNOWN;
890         bcopy(cnp->cn_nameptr, newdir.e2d_name, (unsigned)cnp->cn_namelen + 1);
891
892         if (ext2_htree_has_idx(dp)) {
893                 error = ext2_htree_add_entry(dvp, &newdir, cnp);
894                 if (error) {
895                         dp->i_flag &= ~IN_E3INDEX;
896                         dp->i_flag |= IN_CHANGE | IN_UPDATE;
897                 }
898                 return (error);
899         }
900
901         if (EXT2_HAS_COMPAT_FEATURE(ip->i_e2fs, EXT2F_COMPAT_DIRHASHINDEX) &&
902             !ext2_htree_has_idx(dp)) {
903                 if ((dp->i_size / DIRBLKSIZ) == 1 &&
904                     dp->i_offset == DIRBLKSIZ) {
905                         /*
906                          * Making indexed directory when one block is not
907                          * enough to save all entries.
908                          */
909                         return ext2_htree_create_index(dvp, cnp, &newdir);
910                 }
911         }
912
913         if (dp->i_count == 0) {
914                 /*
915                  * If dp->i_count is 0, then namei could find no
916                  * space in the directory. Here, dp->i_offset will
917                  * be on a directory block boundary and we will write the
918                  * new entry into a fresh block.
919                  */
920                 if (dp->i_offset & (DIRBLKSIZ - 1))
921                         panic("ext2_direnter: newblk");
922
923                 newdir.e2d_reclen = DIRBLKSIZ;
924
925                 bp = getblk(ip->i_devvp, lblkno(dp->i_e2fs, dp->i_offset),
926                     DIRBLKSIZ, 0, 0, 0);
927                 if (!bp)
928                         return (EIO);
929
930                 memcpy(bp->b_data, &newdir, sizeof(struct ext2fs_direct_2));
931
932                 ext2_dir_blk_csum_set(dp, bp);
933                 error = bwrite(bp);
934                 if (error)
935                         return (error);
936
937                 dp->i_size = roundup2(dp->i_size, DIRBLKSIZ);
938                 dp->i_flag |= IN_CHANGE;
939
940                 return (0);
941         }
942
943         error = ext2_add_entry(dvp, &newdir);
944         if (!error && dp->i_endoff && dp->i_endoff < dp->i_size)
945                 error = ext2_truncate(dvp, (off_t)dp->i_endoff, IO_SYNC,
946                     cnp->cn_cred, cnp->cn_thread);
947         return (error);
948 }
949
950 /*
951  * Insert an entry into the directory block.
952  * Compact the contents.
953  */
954 int
955 ext2_add_entry(struct vnode *dvp, struct ext2fs_direct_2 *entry)
956 {
957         struct ext2fs_direct_2 *ep, *nep;
958         struct inode *dp;
959         struct buf *bp;
960         u_int dsize;
961         int error, loc, newentrysize, spacefree;
962         char *dirbuf;
963
964         dp = VTOI(dvp);
965
966         /*
967          * If dp->i_count is non-zero, then namei found space
968          * for the new entry in the range dp->i_offset to
969          * dp->i_offset + dp->i_count in the directory.
970          * To use this space, we may have to compact the entries located
971          * there, by copying them together towards the beginning of the
972          * block, leaving the free space in one usable chunk at the end.
973          */
974
975         /*
976          * Increase size of directory if entry eats into new space.
977          * This should never push the size past a new multiple of
978          * DIRBLKSIZE.
979          *
980          * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
981          */
982         if (dp->i_offset + dp->i_count > dp->i_size)
983                 dp->i_size = dp->i_offset + dp->i_count;
984         /*
985          * Get the block containing the space for the new directory entry.
986          */
987         if ((error = ext2_blkatoff(dvp, (off_t)dp->i_offset, &dirbuf,
988             &bp)) != 0)
989                 return (error);
990         /*
991          * Find space for the new entry. In the simple case, the entry at
992          * offset base will have the space. If it does not, then namei
993          * arranged that compacting the region dp->i_offset to
994          * dp->i_offset + dp->i_count would yield the
995          * space.
996          */
997         newentrysize = EXT2_DIR_REC_LEN(entry->e2d_namlen);
998         ep = (struct ext2fs_direct_2 *)dirbuf;
999         dsize = EXT2_DIR_REC_LEN(ep->e2d_namlen);
1000         spacefree = ep->e2d_reclen - dsize;
1001         for (loc = ep->e2d_reclen; loc < dp->i_count; ) {
1002                 nep = (struct ext2fs_direct_2 *)(dirbuf + loc);
1003                 if (ep->e2d_ino) {
1004                         /* trim the existing slot */
1005                         ep->e2d_reclen = dsize;
1006                         ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1007                 } else {
1008                         /* overwrite; nothing there; header is ours */
1009                         spacefree += dsize;
1010                 }
1011                 dsize = EXT2_DIR_REC_LEN(nep->e2d_namlen);
1012                 spacefree += nep->e2d_reclen - dsize;
1013                 loc += nep->e2d_reclen;
1014                 bcopy((caddr_t)nep, (caddr_t)ep, dsize);
1015         }
1016         /*
1017          * Update the pointer fields in the previous entry (if any),
1018          * copy in the new entry, and write out the block.
1019          */
1020         if (ep->e2d_ino == 0) {
1021                 if (spacefree + dsize < newentrysize)
1022                         panic("ext2_direnter: compact1");
1023                 entry->e2d_reclen = spacefree + dsize;
1024         } else {
1025                 if (spacefree < newentrysize)
1026                         panic("ext2_direnter: compact2");
1027                 entry->e2d_reclen = spacefree;
1028                 ep->e2d_reclen = dsize;
1029                 ep = (struct ext2fs_direct_2 *)((char *)ep + dsize);
1030         }
1031         bcopy((caddr_t)entry, (caddr_t)ep, (u_int)newentrysize);
1032         ext2_dir_blk_csum_set(dp, bp);
1033         if (DOINGASYNC(dvp)) {
1034                 bdwrite(bp);
1035                 error = 0;
1036         } else {
1037                 error = bwrite(bp);
1038         }
1039         dp->i_flag |= IN_CHANGE | IN_UPDATE;
1040         return (error);
1041 }
1042
1043 /*
1044  * Remove a directory entry after a call to namei, using
1045  * the parameters which it left in nameidata. The entry
1046  * dp->i_offset contains the offset into the directory of the
1047  * entry to be eliminated.  The dp->i_count field contains the
1048  * size of the previous record in the directory.  If this
1049  * is 0, the first entry is being deleted, so we need only
1050  * zero the inode number to mark the entry as free.  If the
1051  * entry is not the first in the directory, we must reclaim
1052  * the space of the now empty record by adding the record size
1053  * to the size of the previous entry.
1054  */
1055 int
1056 ext2_dirremove(struct vnode *dvp, struct componentname *cnp)
1057 {
1058         struct inode *dp;
1059         struct ext2fs_direct_2 *ep, *rep;
1060         struct buf *bp;
1061         int error;
1062
1063         dp = VTOI(dvp);
1064         if (dp->i_count == 0) {
1065                 /*
1066                  * First entry in block: set d_ino to zero.
1067                  */
1068                 if ((error =
1069                     ext2_blkatoff(dvp, (off_t)dp->i_offset, (char **)&ep,
1070                     &bp)) != 0)
1071                         return (error);
1072                 ep->e2d_ino = 0;
1073                 error = bwrite(bp);
1074                 dp->i_flag |= IN_CHANGE | IN_UPDATE;
1075                 return (error);
1076         }
1077         /*
1078          * Collapse new free space into previous entry.
1079          */
1080         if ((error = ext2_blkatoff(dvp, (off_t)(dp->i_offset - dp->i_count),
1081             (char **)&ep, &bp)) != 0)
1082                 return (error);
1083
1084         /* Set 'rep' to the entry being removed. */
1085         if (dp->i_count == 0)
1086                 rep = ep;
1087         else
1088                 rep = (struct ext2fs_direct_2 *)((char *)ep + ep->e2d_reclen);
1089         ep->e2d_reclen += rep->e2d_reclen;
1090         ext2_dir_blk_csum_set(dp, bp);
1091         if (DOINGASYNC(dvp) && dp->i_count != 0)
1092                 bdwrite(bp);
1093         else
1094                 error = bwrite(bp);
1095         dp->i_flag |= IN_CHANGE | IN_UPDATE;
1096         return (error);
1097 }
1098
1099 /*
1100  * Rewrite an existing directory entry to point at the inode
1101  * supplied.  The parameters describing the directory entry are
1102  * set up by a call to namei.
1103  */
1104 int
1105 ext2_dirrewrite(struct inode *dp, struct inode *ip, struct componentname *cnp)
1106 {
1107         struct buf *bp;
1108         struct ext2fs_direct_2 *ep;
1109         struct vnode *vdp = ITOV(dp);
1110         int error;
1111
1112         if ((error = ext2_blkatoff(vdp, (off_t)dp->i_offset, (char **)&ep,
1113             &bp)) != 0)
1114                 return (error);
1115         ep->e2d_ino = ip->i_number;
1116         if (EXT2_HAS_INCOMPAT_FEATURE(ip->i_e2fs,
1117             EXT2F_INCOMPAT_FTYPE))
1118                 ep->e2d_type = DTTOFT(IFTODT(ip->i_mode));
1119         else
1120                 ep->e2d_type = EXT2_FT_UNKNOWN;
1121         ext2_dir_blk_csum_set(dp, bp);
1122         error = bwrite(bp);
1123         dp->i_flag |= IN_CHANGE | IN_UPDATE;
1124         return (error);
1125 }
1126
1127 /*
1128  * Check if a directory is empty or not.
1129  * Inode supplied must be locked.
1130  *
1131  * Using a struct dirtemplate here is not precisely
1132  * what we want, but better than using a struct direct.
1133  *
1134  * NB: does not handle corrupted directories.
1135  */
1136 int
1137 ext2_dirempty(struct inode *ip, ino_t parentino, struct ucred *cred)
1138 {
1139         off_t off;
1140         struct dirtemplate dbuf;
1141         struct ext2fs_direct_2 *dp = (struct ext2fs_direct_2 *)&dbuf;
1142         int error, namlen;
1143         ssize_t count;
1144 #define MINDIRSIZ (sizeof(struct dirtemplate) / 2)
1145
1146         for (off = 0; off < ip->i_size; off += dp->e2d_reclen) {
1147                 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ,
1148                     off, UIO_SYSSPACE, IO_NODELOCKED | IO_NOMACCHECK, cred,
1149                     NOCRED, &count, (struct thread *)0);
1150                 /*
1151                  * Since we read MINDIRSIZ, residual must
1152                  * be 0 unless we're at end of file.
1153                  */
1154                 if (error || count != 0)
1155                         return (0);
1156                 /* avoid infinite loops */
1157                 if (dp->e2d_reclen == 0)
1158                         return (0);
1159                 /* skip empty entries */
1160                 if (dp->e2d_ino == 0)
1161                         continue;
1162                 /* accept only "." and ".." */
1163                 namlen = dp->e2d_namlen;
1164                 if (namlen > 2)
1165                         return (0);
1166                 if (dp->e2d_name[0] != '.')
1167                         return (0);
1168                 /*
1169                  * At this point namlen must be 1 or 2.
1170                  * 1 implies ".", 2 implies ".." if second
1171                  * char is also "."
1172                  */
1173                 if (namlen == 1)
1174                         continue;
1175                 if (dp->e2d_name[1] == '.' && dp->e2d_ino == parentino)
1176                         continue;
1177                 return (0);
1178         }
1179         return (1);
1180 }
1181
1182 /*
1183  * Check if source directory is in the path of the target directory.
1184  * Target is supplied locked, source is unlocked.
1185  * The target is always vput before returning.
1186  */
1187 int
1188 ext2_checkpath(struct inode *source, struct inode *target, struct ucred *cred)
1189 {
1190         struct vnode *vp;
1191         int error, namlen;
1192         struct dirtemplate dirbuf;
1193
1194         vp = ITOV(target);
1195         if (target->i_number == source->i_number) {
1196                 error = EEXIST;
1197                 goto out;
1198         }
1199         if (target->i_number == EXT2_ROOTINO) {
1200                 error = 0;
1201                 goto out;
1202         }
1203
1204         for (;;) {
1205                 if (vp->v_type != VDIR) {
1206                         error = ENOTDIR;
1207                         break;
1208                 }
1209                 error = vn_rdwr(UIO_READ, vp, (caddr_t)&dirbuf,
1210                     sizeof(struct dirtemplate), (off_t)0, UIO_SYSSPACE,
1211                     IO_NODELOCKED | IO_NOMACCHECK, cred, NOCRED, NULL,
1212                     NULL);
1213                 if (error != 0)
1214                         break;
1215                 namlen = dirbuf.dotdot_type;    /* like ufs little-endian */
1216                 if (namlen != 2 ||
1217                     dirbuf.dotdot_name[0] != '.' ||
1218                     dirbuf.dotdot_name[1] != '.') {
1219                         error = ENOTDIR;
1220                         break;
1221                 }
1222                 if (dirbuf.dotdot_ino == source->i_number) {
1223                         error = EINVAL;
1224                         break;
1225                 }
1226                 if (dirbuf.dotdot_ino == EXT2_ROOTINO)
1227                         break;
1228                 vput(vp);
1229                 if ((error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino,
1230                     LK_EXCLUSIVE, &vp)) != 0) {
1231                         vp = NULL;
1232                         break;
1233                 }
1234         }
1235
1236 out:
1237         if (error == ENOTDIR)
1238                 printf("checkpath: .. not a directory\n");
1239         if (vp != NULL)
1240                 vput(vp);
1241         return (error);
1242 }