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