4508e3e0165f2d6285163fc3d7175a4d93802e0d
[dragonfly.git] / sys / vfs / ufs / ufs_lookup.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)ufs_lookup.c        8.15 (Berkeley) 6/16/95
39  * $FreeBSD: src/sys/ufs/ufs/ufs_lookup.c,v 1.33.2.7 2001/09/22 19:22:13 iedowse Exp $
40  * $DragonFly: src/sys/vfs/ufs/ufs_lookup.c,v 1.11 2004/04/24 04:32:05 drhodus Exp $
41  */
42
43 #include "opt_ufs.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/buf.h>
49 #include <sys/proc.h>
50 #include <sys/namei.h>
51 #include <sys/stat.h>
52 #include <sys/mount.h>
53 #include <sys/vnode.h>
54 #include <sys/sysctl.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_extern.h>
58
59 #include "quota.h"
60 #include "inode.h"
61 #include "dir.h"
62 #ifdef UFS_DIRHASH
63 #include "dirhash.h"
64 #endif
65 #include "ufsmount.h"
66 #include "ufs_extern.h"
67
68 #ifdef DIAGNOSTIC
69 int     dirchk = 1;
70 #else
71 int     dirchk = 0;
72 #endif
73
74 SYSCTL_INT(_debug, OID_AUTO, dircheck, CTLFLAG_RW, &dirchk, 0, "");
75
76 /* true if old FS format...*/
77 #define OFSFMT(vp)      ((vp)->v_mount->mnt_maxsymlinklen <= 0)
78
79 /*
80  * Convert a component of a pathname into a pointer to a locked inode.
81  * This is a very central and rather complicated routine.
82  * If the file system is not maintained in a strict tree hierarchy,
83  * this can result in a deadlock situation (see comments in code below).
84  *
85  * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
86  * on whether the name is to be looked up, created, renamed, or deleted.
87  * When CREATE, RENAME, or DELETE is specified, information usable in
88  * creating, renaming, or deleting a directory entry may be calculated.
89  * If flag has LOCKPARENT or'ed into it and the target of the pathname
90  * exists, lookup returns both the target and its parent directory locked.
91  * When creating or renaming and LOCKPARENT is specified, the target may
92  * not be ".".  When deleting and LOCKPARENT is specified, the target may
93  * be "."., but the caller must check to ensure it does an vrele and vput
94  * instead of two vputs.
95  *
96  * This routine is actually used as VOP_CACHEDLOOKUP method, and the
97  * filesystem employs the generic vfs_cache_lookup() as VOP_LOOKUP
98  * method.
99  *
100  * vfs_cache_lookup() performs the following for us:
101  *      check that it is a directory
102  *      check accessibility of directory
103  *      check for modification attempts on read-only mounts
104  *      if name found in cache
105  *          if at end of path and deleting or creating
106  *              drop it
107  *           else
108  *              return name.
109  *      return VOP_CACHEDLOOKUP()
110  *
111  * Overall outline of ufs_lookup:
112  *
113  *      search for name in directory, to found or notfound
114  * notfound:
115  *      if creating, return locked directory, leaving info on available slots
116  *      else return error
117  * found:
118  *      if at end of path and deleting, return information to allow delete
119  *      if at end of path and rewriting (RENAME and LOCKPARENT), lock target
120  *        inode and return info to allow rewrite
121  *      if not at end, add name to cache; if at end and neither creating
122  *        nor deleting, add name to cache
123  */
124 int
125 ufs_lookup(ap)
126         struct vop_cachedlookup_args /* {
127                 struct vnode *a_dvp;
128                 struct vnode **a_vpp;
129                 struct componentname *a_cnp;
130         } */ *ap;
131 {
132         struct vnode *vdp;      /* vnode for directory being searched */
133         struct inode *dp;       /* inode for directory being searched */
134         struct buf *bp;                 /* a buffer of directory entries */
135         struct direct *ep;              /* the current directory entry */
136         int entryoffsetinblock;         /* offset of ep in bp's buffer */
137         enum {NONE, COMPACT, FOUND} slotstatus;
138         doff_t slotoffset;              /* offset of area with free space */
139         int slotsize;                   /* size of area at slotoffset */
140         int slotfreespace;              /* amount of space free in slot */
141         int slotneeded;                 /* size of the entry we're seeking */
142         int numdirpasses;               /* strategy for directory search */
143         doff_t endsearch;               /* offset to end directory search */
144         doff_t prevoff;                 /* prev entry dp->i_offset */
145         struct vnode *pdp;              /* saved dp during symlink work */
146         struct vnode *tdp;              /* returned by VFS_VGET */
147         doff_t enduseful;               /* pointer past last used dir slot */
148         u_long bmask;                   /* block offset mask */
149         int lockparent;                 /* 1 => lockparent flag is set */
150         int wantparent;                 /* 1 => wantparent or lockparent flag */
151         int namlen, error;
152         struct vnode **vpp = ap->a_vpp;
153         struct componentname *cnp = ap->a_cnp;
154         struct ucred *cred = cnp->cn_cred;
155         int flags = cnp->cn_flags;
156         int nameiop = cnp->cn_nameiop;
157         struct thread *td = cnp->cn_td;
158         globaldata_t gd = mycpu;
159
160         bp = NULL;
161         slotoffset = -1;
162         cnp->cn_flags &= ~CNP_PDIRUNLOCK;
163 /*
164  *  XXX there was a soft-update diff about this I couldn't merge.
165  * I think this was the equiv.
166  */
167         *vpp = NULL;
168
169         vdp = ap->a_dvp;
170         dp = VTOI(vdp);
171         lockparent = flags & CNP_LOCKPARENT;
172         wantparent = flags & (CNP_LOCKPARENT|CNP_WANTPARENT);
173
174         /*
175          * We now have a segment name to search for, and a directory to search.
176          *
177          * Suppress search for slots unless creating
178          * file and at end of pathname, in which case
179          * we watch for a place to put the new file in
180          * case it doesn't already exist.
181          */
182         slotstatus = FOUND;
183         slotfreespace = slotsize = slotneeded = 0;
184         if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME) &&
185             (flags & CNP_ISLASTCN)) {
186                 slotstatus = NONE;
187                 slotneeded = DIRECTSIZ(cnp->cn_namelen);
188         }
189         bmask = VFSTOUFS(vdp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
190
191 #ifdef UFS_DIRHASH
192         /*
193          * Use dirhash for fast operations on large directories. The logic
194          * to determine whether to hash the directory is contained within
195          * ufsdirhash_build(); a zero return means that it decided to hash
196          * this directory and it successfully built up the hash table.
197          */
198         if (ufsdirhash_build(dp) == 0) {
199                 /* Look for a free slot if needed. */
200                 enduseful = dp->i_size;
201                 if (slotstatus != FOUND) {
202                         slotoffset = ufsdirhash_findfree(dp, slotneeded,
203                             &slotsize);
204                         if (slotoffset >= 0) {
205                                 slotstatus = COMPACT;
206                                 enduseful = ufsdirhash_enduseful(dp);
207                                 if (enduseful < 0)
208                                         enduseful = dp->i_size;
209                         }
210                 }
211                 /* Look up the component. */
212                 numdirpasses = 1;
213                 entryoffsetinblock = 0; /* silence compiler warning */
214                 switch (ufsdirhash_lookup(dp, cnp->cn_nameptr, cnp->cn_namelen,
215                     &dp->i_offset, &bp, nameiop == NAMEI_DELETE ? &prevoff : NULL)) {
216                 case 0:
217                         ep = (struct direct *)((char *)bp->b_data +
218                             (dp->i_offset & bmask));
219                         goto foundentry;
220                 case ENOENT:
221                         dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
222                         goto notfound;
223                 default:
224                         /* Something failed; just do a linear search. */
225                         break;
226                 }
227         }
228 #endif /* UFS_DIRHASH */
229         /*
230          * If there is cached information on a previous search of
231          * this directory, pick up where we last left off.
232          * We cache only lookups as these are the most common
233          * and have the greatest payoff. Caching CREATE has little
234          * benefit as it usually must search the entire directory
235          * to determine that the entry does not exist. Caching the
236          * location of the last DELETE or RENAME has not reduced
237          * profiling time and hence has been removed in the interest
238          * of simplicity.
239          */
240         if (nameiop != NAMEI_LOOKUP || dp->i_diroff == 0 ||
241             dp->i_diroff >= dp->i_size) {
242                 entryoffsetinblock = 0;
243                 dp->i_offset = 0;
244                 numdirpasses = 1;
245         } else {
246                 dp->i_offset = dp->i_diroff;
247                 if ((entryoffsetinblock = dp->i_offset & bmask) &&
248                     (error = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
249                         return (error);
250                 numdirpasses = 2;
251                 gd->gd_nchstats->ncs_2passes++;
252         }
253         prevoff = dp->i_offset;
254         endsearch = roundup2(dp->i_size, DIRBLKSIZ);
255         enduseful = 0;
256
257 searchloop:
258         while (dp->i_offset < endsearch) {
259                 /*
260                  * If necessary, get the next directory block.
261                  */
262                 if ((dp->i_offset & bmask) == 0) {
263                         if (bp != NULL)
264                                 brelse(bp);
265                         error =
266                             UFS_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp);
267                         if (error)
268                                 return (error);
269                         entryoffsetinblock = 0;
270                 }
271                 /*
272                  * If still looking for a slot, and at a DIRBLKSIZE
273                  * boundary, have to start looking for free space again.
274                  */
275                 if (slotstatus == NONE &&
276                     (entryoffsetinblock & (DIRBLKSIZ - 1)) == 0) {
277                         slotoffset = -1;
278                         slotfreespace = 0;
279                 }
280                 /*
281                  * Get pointer to next entry.
282                  * Full validation checks are slow, so we only check
283                  * enough to insure forward progress through the
284                  * directory. Complete checks can be run by patching
285                  * "dirchk" to be true.
286                  */
287                 ep = (struct direct *)((char *)bp->b_data + entryoffsetinblock);
288                 if (ep->d_reclen == 0 || ep->d_reclen >
289                     DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
290                     (dirchk && ufs_dirbadentry(vdp, ep, entryoffsetinblock))) {
291                         int i;
292
293                         ufs_dirbad(dp, dp->i_offset, "mangled entry");
294                         i = DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1));
295                         dp->i_offset += i;
296                         entryoffsetinblock += i;
297                         continue;
298                 }
299
300                 /*
301                  * If an appropriate sized slot has not yet been found,
302                  * check to see if one is available. Also accumulate space
303                  * in the current block so that we can determine if
304                  * compaction is viable.
305                  */
306                 if (slotstatus != FOUND) {
307                         int size = ep->d_reclen;
308
309                         if (ep->d_ino != 0)
310                                 size -= DIRSIZ(OFSFMT(vdp), ep);
311                         if (size > 0) {
312                                 if (size >= slotneeded) {
313                                         slotstatus = FOUND;
314                                         slotoffset = dp->i_offset;
315                                         slotsize = ep->d_reclen;
316                                 } else if (slotstatus == NONE) {
317                                         slotfreespace += size;
318                                         if (slotoffset == -1)
319                                                 slotoffset = dp->i_offset;
320                                         if (slotfreespace >= slotneeded) {
321                                                 slotstatus = COMPACT;
322                                                 slotsize = dp->i_offset +
323                                                       ep->d_reclen - slotoffset;
324                                         }
325                                 }
326                         }
327                 }
328
329                 /*
330                  * Check for a name match.
331                  */
332                 if (ep->d_ino) {
333 #                       if (BYTE_ORDER == LITTLE_ENDIAN)
334                                 if (OFSFMT(vdp))
335                                         namlen = ep->d_type;
336                                 else
337                                         namlen = ep->d_namlen;
338 #                       else
339                                 namlen = ep->d_namlen;
340 #                       endif
341                         if (namlen == cnp->cn_namelen &&
342                                 (cnp->cn_nameptr[0] == ep->d_name[0]) &&
343                             !bcmp(cnp->cn_nameptr, ep->d_name,
344                                 (unsigned)namlen)) {
345 #ifdef UFS_DIRHASH
346 foundentry:
347 #endif
348                                 /*
349                                  * Save directory entry's inode number and
350                                  * reclen in ndp->ni_ufs area, and release
351                                  * directory buffer.
352                                  */
353                                 if (vdp->v_mount->mnt_maxsymlinklen > 0 &&
354                                     ep->d_type == DT_WHT) {
355                                         slotstatus = FOUND;
356                                         slotoffset = dp->i_offset;
357                                         slotsize = ep->d_reclen;
358                                         dp->i_reclen = slotsize;
359                                         enduseful = dp->i_size;
360                                         ap->a_cnp->cn_flags |= CNP_ISWHITEOUT;
361                                         numdirpasses--;
362                                         goto notfound;
363                                 }
364                                 dp->i_ino = ep->d_ino;
365                                 dp->i_reclen = ep->d_reclen;
366                                 goto found;
367                         }
368                 }
369                 prevoff = dp->i_offset;
370                 dp->i_offset += ep->d_reclen;
371                 entryoffsetinblock += ep->d_reclen;
372                 if (ep->d_ino)
373                         enduseful = dp->i_offset;
374         }
375 notfound:
376         /*
377          * If we started in the middle of the directory and failed
378          * to find our target, we must check the beginning as well.
379          */
380         if (numdirpasses == 2) {
381                 numdirpasses--;
382                 dp->i_offset = 0;
383                 endsearch = dp->i_diroff;
384                 goto searchloop;
385         }
386         if (bp != NULL)
387                 brelse(bp);
388         /*
389          * If creating, and at end of pathname and current
390          * directory has not been removed, then can consider
391          * allowing file to be created.
392          */
393         if ((nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME ||
394              (nameiop == NAMEI_DELETE &&
395               (ap->a_cnp->cn_flags & CNP_DOWHITEOUT) &&
396               (ap->a_cnp->cn_flags & CNP_ISWHITEOUT))) &&
397             (flags & CNP_ISLASTCN) && dp->i_effnlink != 0) {
398                 /*
399                  * Access for write is interpreted as allowing
400                  * creation of files in the directory.
401                  */
402                 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_td);
403                 if (error)
404                         return (error);
405                 /*
406                  * Return an indication of where the new directory
407                  * entry should be put.  If we didn't find a slot,
408                  * then set dp->i_count to 0 indicating
409                  * that the new slot belongs at the end of the
410                  * directory. If we found a slot, then the new entry
411                  * can be put in the range from dp->i_offset to
412                  * dp->i_offset + dp->i_count.
413                  */
414                 if (slotstatus == NONE) {
415                         dp->i_offset = roundup2(dp->i_size, DIRBLKSIZ);
416                         dp->i_count = 0;
417                         enduseful = dp->i_offset;
418                 } else if (nameiop == NAMEI_DELETE) {
419                         dp->i_offset = slotoffset;
420                         if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
421                                 dp->i_count = 0;
422                         else
423                                 dp->i_count = dp->i_offset - prevoff;
424                 } else {
425                         dp->i_offset = slotoffset;
426                         dp->i_count = slotsize;
427                         if (enduseful < slotoffset + slotsize)
428                                 enduseful = slotoffset + slotsize;
429                 }
430                 dp->i_endoff = roundup2(enduseful, DIRBLKSIZ);
431                 dp->i_flag |= IN_CHANGE | IN_UPDATE;
432                 /*
433                  * We return with the directory locked, so that
434                  * the parameters we set up above will still be
435                  * valid if we actually decide to do a direnter().
436                  * We return ni_vp == NULL to indicate that the entry
437                  * does not currently exist; we leave a pointer to
438                  * the (locked) directory inode in ndp->ni_dvp.
439                  * The pathname buffer is saved so that the name
440                  * can be obtained later.
441                  *
442                  * NB - if the directory is unlocked, then this
443                  * information cannot be used.
444                  */
445                 cnp->cn_flags |= CNP_SAVENAME;
446                 if (!lockparent) {
447                         VOP_UNLOCK(vdp, NULL, 0, td);
448                         cnp->cn_flags |= CNP_PDIRUNLOCK;
449                 }
450                 return (EJUSTRETURN);
451         }
452         /*
453          * Insert name into cache (as non-existent) if appropriate.
454          */
455         if ((cnp->cn_flags & CNP_MAKEENTRY) && nameiop != NAMEI_CREATE)
456                 cache_enter(vdp, NCPNULL, *vpp, cnp);
457         return (ENOENT);
458
459 found:
460         if (numdirpasses == 2)
461                 gd->gd_nchstats->ncs_pass2++;
462         /*
463          * Check that directory length properly reflects presence
464          * of this entry.
465          */
466         if (dp->i_offset + DIRSIZ(OFSFMT(vdp), ep) > dp->i_size) {
467                 ufs_dirbad(dp, dp->i_offset, "i_size too small");
468                 dp->i_size = dp->i_offset + DIRSIZ(OFSFMT(vdp), ep);
469                 dp->i_flag |= IN_CHANGE | IN_UPDATE;
470         }
471         brelse(bp);
472
473         /*
474          * Found component in pathname.
475          * If the final component of path name, save information
476          * in the cache as to where the entry was found.
477          */
478         if ((flags & CNP_ISLASTCN) && nameiop == NAMEI_LOOKUP)
479                 dp->i_diroff = dp->i_offset &~ (DIRBLKSIZ - 1);
480
481         /*
482          * If deleting, and at end of pathname, return
483          * parameters which can be used to remove file.
484          * If the wantparent flag isn't set, we return only
485          * the directory (in ndp->ni_dvp), otherwise we go
486          * on and lock the inode, being careful with ".".
487          */
488         if (nameiop == NAMEI_DELETE && (flags & CNP_ISLASTCN)) {
489                 /*
490                  * Write access to directory required to delete files.
491                  */
492                 error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_td);
493                 if (error)
494                         return (error);
495                 /*
496                  * Return pointer to current entry in dp->i_offset,
497                  * and distance past previous entry (if there
498                  * is a previous entry in this block) in dp->i_count.
499                  * Save directory inode pointer in ndp->ni_dvp for dirremove().
500                  */
501                 if ((dp->i_offset & (DIRBLKSIZ - 1)) == 0)
502                         dp->i_count = 0;
503                 else
504                         dp->i_count = dp->i_offset - prevoff;
505                 if (dp->i_number == dp->i_ino) {
506                         vref(vdp);
507                         *vpp = vdp;
508                         return (0);
509                 }
510                 if (flags & CNP_ISDOTDOT)
511                         VOP_UNLOCK(vdp, NULL, 0, td);/* race to get the inode */
512                 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
513                 if (flags & CNP_ISDOTDOT) {
514                         if (vn_lock(vdp, NULL, LK_EXCLUSIVE | LK_RETRY, td) != 0)
515                                 cnp->cn_flags |= CNP_PDIRUNLOCK;
516                 }
517                 if (error)
518                         return (error);
519                 /*
520                  * If directory is "sticky", then user must own
521                  * the directory, or the file in it, else she
522                  * may not delete it (unless she's root). This
523                  * implements append-only directories.
524                  */
525                 if ((dp->i_mode & ISVTX) &&
526                     cred->cr_uid != 0 &&
527                     cred->cr_uid != dp->i_uid &&
528                     VTOI(tdp)->i_uid != cred->cr_uid) {
529                         vput(tdp);
530                         return (EPERM);
531                 }
532                 *vpp = tdp;
533                 if (!lockparent) {
534                         VOP_UNLOCK(vdp, NULL, 0, td);
535                         cnp->cn_flags |= CNP_PDIRUNLOCK;
536                 }
537                 return (0);
538         }
539
540         /*
541          * If rewriting (RENAME), return the inode and the
542          * information required to rewrite the present directory
543          * Must get inode of directory entry to verify it's a
544          * regular file, or empty directory.
545          */
546         if (nameiop == NAMEI_RENAME && wantparent && (flags & CNP_ISLASTCN)) {
547                 if ((error = VOP_ACCESS(vdp, VWRITE, cred, cnp->cn_td)) != 0)
548                         return (error);
549                 /*
550                  * Careful about locking second inode.
551                  * This can only occur if the target is ".".
552                  */
553                 if (dp->i_number == dp->i_ino)
554                         return (EISDIR);
555                 if (flags & CNP_ISDOTDOT)
556                         VOP_UNLOCK(vdp, NULL, 0, td);   /* race to get the inode */
557                 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
558                 if (flags & CNP_ISDOTDOT) {
559                         if (vn_lock(vdp, NULL, LK_EXCLUSIVE | LK_RETRY, td) != 0)
560                                 cnp->cn_flags |= CNP_PDIRUNLOCK;
561                 }
562                 if (error)
563                         return (error);
564                 *vpp = tdp;
565                 cnp->cn_flags |= CNP_SAVENAME;
566                 if (!lockparent) {
567                         VOP_UNLOCK(vdp, NULL, 0, td);
568                         cnp->cn_flags |= CNP_PDIRUNLOCK;
569                 }
570                 return (0);
571         }
572
573         /*
574          * Step through the translation in the name.  We do not `vput' the
575          * directory because we may need it again if a symbolic link
576          * is relative to the current directory.  Instead we save it
577          * unlocked as "pdp".  We must get the target inode before unlocking
578          * the directory to insure that the inode will not be removed
579          * before we get it.  We prevent deadlock by always fetching
580          * inodes from the root, moving down the directory tree. Thus
581          * when following backward pointers ".." we must unlock the
582          * parent directory before getting the requested directory.
583          * There is a potential race condition here if both the current
584          * and parent directories are removed before the VFS_VGET for the
585          * inode associated with ".." returns.  We hope that this occurs
586          * infrequently since we cannot avoid this race condition without
587          * implementing a sophisticated deadlock detection algorithm.
588          * Note also that this simple deadlock detection scheme will not
589          * work if the file system has any hard links other than ".."
590          * that point backwards in the directory structure.
591          */
592         pdp = vdp;
593         if (flags & CNP_ISDOTDOT) {
594                 VOP_UNLOCK(pdp, NULL, 0, td);   /* race to get the inode */
595                 cnp->cn_flags |= CNP_PDIRUNLOCK;
596                 if ((error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp)) != 0) {
597                         if (vn_lock(pdp, NULL, LK_EXCLUSIVE | LK_RETRY, td) == 0)
598                                 cnp->cn_flags &= ~CNP_PDIRUNLOCK;
599                         return (error);
600                 }
601                 if (lockparent && (flags & CNP_ISLASTCN)) {
602                         if ((error = vn_lock(pdp, NULL, LK_EXCLUSIVE, td)) != 0) {
603                                 vput(tdp);
604                                 return (error);
605                         }
606                         cnp->cn_flags &= ~CNP_PDIRUNLOCK;
607                 }
608                 *vpp = tdp;
609         } else if (dp->i_number == dp->i_ino) {
610                 vref(vdp);      /* we want ourself, ie "." */
611                 *vpp = vdp;
612         } else {
613                 error = VFS_VGET(vdp->v_mount, dp->i_ino, &tdp);
614                 if (error)
615                         return (error);
616                 if (!lockparent || !(flags & CNP_ISLASTCN)) {
617                         VOP_UNLOCK(pdp, NULL, 0, td);
618                         cnp->cn_flags |= CNP_PDIRUNLOCK;
619                 }
620                 *vpp = tdp;
621         }
622
623         /*
624          * Insert name into cache if appropriate.
625          */
626         if (cnp->cn_flags & CNP_MAKEENTRY)
627                 cache_enter(vdp, NCPNULL, *vpp, cnp);
628         return (0);
629 }
630
631 void
632 ufs_dirbad(ip, offset, how)
633         struct inode *ip;
634         doff_t offset;
635         char *how;
636 {
637         struct mount *mp;
638
639         mp = ITOV(ip)->v_mount;
640         (void)printf("%s: bad dir ino %lu at offset %ld: %s\n",
641             mp->mnt_stat.f_mntonname, (u_long)ip->i_number, (long)offset, how);
642         if ((mp->mnt_flag & MNT_RDONLY) == 0)
643                 panic("ufs_dirbad: bad dir");
644 }
645
646 /*
647  * Do consistency checking on a directory entry:
648  *      record length must be multiple of 4
649  *      entry must fit in rest of its DIRBLKSIZ block
650  *      record must be large enough to contain entry
651  *      name is not longer than MAXNAMLEN
652  *      name must be as long as advertised, and null terminated
653  */
654 int
655 ufs_dirbadentry(dp, ep, entryoffsetinblock)
656         struct vnode *dp;
657         struct direct *ep;
658         int entryoffsetinblock;
659 {
660         int i;
661         int namlen;
662
663 #       if (BYTE_ORDER == LITTLE_ENDIAN)
664                 if (OFSFMT(dp))
665                         namlen = ep->d_type;
666                 else
667                         namlen = ep->d_namlen;
668 #       else
669                 namlen = ep->d_namlen;
670 #       endif
671         if ((ep->d_reclen & 0x3) != 0 ||
672             ep->d_reclen > DIRBLKSIZ - (entryoffsetinblock & (DIRBLKSIZ - 1)) ||
673             ep->d_reclen < DIRSIZ(OFSFMT(dp), ep) || namlen > MAXNAMLEN) {
674                 /*return (1); */
675                 printf("First bad\n");
676                 goto bad;
677         }
678         if (ep->d_ino == 0)
679                 return (0);
680         for (i = 0; i < namlen; i++)
681                 if (ep->d_name[i] == '\0') {
682                         /*return (1); */
683                         printf("Second bad\n");
684                         goto bad;
685         }
686         if (ep->d_name[i])
687                 goto bad;
688         return (0);
689 bad:
690         return (1);
691 }
692
693 /*
694  * Construct a new directory entry after a call to namei, using the
695  * parameters that it left in the componentname argument cnp. The
696  * argument ip is the inode to which the new directory entry will refer.
697  */
698 void
699 ufs_makedirentry(ip, cnp, newdirp)
700         struct inode *ip;
701         struct componentname *cnp;
702         struct direct *newdirp;
703 {
704
705 #ifdef DIAGNOSTIC
706         if ((cnp->cn_flags & CNP_SAVENAME) == 0)
707                 panic("ufs_makedirentry: missing name");
708 #endif
709         newdirp->d_ino = ip->i_number;
710         newdirp->d_namlen = cnp->cn_namelen;
711         bcopy(cnp->cn_nameptr, newdirp->d_name, (unsigned)cnp->cn_namelen + 1);
712         if (ITOV(ip)->v_mount->mnt_maxsymlinklen > 0)
713                 newdirp->d_type = IFTODT(ip->i_mode);
714         else {
715                 newdirp->d_type = 0;
716 #               if (BYTE_ORDER == LITTLE_ENDIAN)
717                         { u_char tmp = newdirp->d_namlen;
718                         newdirp->d_namlen = newdirp->d_type;
719                         newdirp->d_type = tmp; }
720 #               endif
721         }
722 }
723
724 /*
725  * Write a directory entry after a call to namei, using the parameters
726  * that it left in nameidata. The argument dirp is the new directory
727  * entry contents. Dvp is a pointer to the directory to be written,
728  * which was left locked by namei. Remaining parameters (dp->i_offset, 
729  * dp->i_count) indicate how the space for the new entry is to be obtained.
730  * Non-null bp indicates that a directory is being created (for the
731  * soft dependency code).
732  */
733 int
734 ufs_direnter(dvp, tvp, dirp, cnp, newdirbp)
735         struct vnode *dvp;
736         struct vnode *tvp;
737         struct direct *dirp;
738         struct componentname *cnp;
739         struct buf *newdirbp;
740 {
741         struct ucred *cred;
742         struct thread *td = curthread;  /* XXX */
743         int newentrysize;
744         struct inode *dp;
745         struct buf *bp;
746         u_int dsize;
747         struct direct *ep, *nep;
748         int error, ret, blkoff, loc, spacefree, flags;
749         char *dirbuf;
750
751         KKASSERT(td->td_proc);  /* YYY use/require cred passed in cnp? */
752         cred = td->td_proc->p_ucred;
753
754         dp = VTOI(dvp);
755         newentrysize = DIRSIZ(OFSFMT(dvp), dirp);
756
757         if (dp->i_count == 0) {
758                 /*
759                  * If dp->i_count is 0, then namei could find no
760                  * space in the directory. Here, dp->i_offset will
761                  * be on a directory block boundary and we will write the
762                  * new entry into a fresh block.
763                  */
764                 if (dp->i_offset & (DIRBLKSIZ - 1))
765                         panic("ufs_direnter: newblk");
766                 flags = B_CLRBUF;
767                 if (!DOINGSOFTDEP(dvp) && !DOINGASYNC(dvp))
768                         flags |= B_SYNC;
769                 if ((error = VOP_BALLOC(dvp, (off_t)dp->i_offset, DIRBLKSIZ,
770                     cred, flags, &bp)) != 0) {
771                         if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
772                                 bdwrite(newdirbp);
773                         return (error);
774                 }
775                 dp->i_size = dp->i_offset + DIRBLKSIZ;
776                 dp->i_flag |= IN_CHANGE | IN_UPDATE;
777                 vnode_pager_setsize(dvp, (u_long)dp->i_size);
778                 dirp->d_reclen = DIRBLKSIZ;
779                 blkoff = dp->i_offset &
780                     (VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_iosize - 1);
781                 bcopy((caddr_t)dirp, (caddr_t)bp->b_data + blkoff,newentrysize);
782 #ifdef UFS_DIRHASH
783                 if (dp->i_dirhash != NULL) {
784                         ufsdirhash_newblk(dp, dp->i_offset);
785                         ufsdirhash_add(dp, dirp, dp->i_offset);
786                         ufsdirhash_checkblock(dp, (char *)bp->b_data + blkoff,
787                             dp->i_offset);
788                 }
789 #endif
790                 if (DOINGSOFTDEP(dvp)) {
791                         /*
792                          * Ensure that the entire newly allocated block is a
793                          * valid directory so that future growth within the
794                          * block does not have to ensure that the block is
795                          * written before the inode.
796                          */
797                         blkoff += DIRBLKSIZ;
798                         while (blkoff < bp->b_bcount) {
799                                 ((struct direct *)
800                                    (bp->b_data + blkoff))->d_reclen = DIRBLKSIZ;
801                                 blkoff += DIRBLKSIZ;
802                         }
803                         softdep_setup_directory_add(bp, dp, dp->i_offset,
804                             dirp->d_ino, newdirbp);
805                         bdwrite(bp);
806                         return (UFS_UPDATE(dvp, 0));
807                 }
808                 if (DOINGASYNC(dvp)) {
809                         bdwrite(bp);
810                         return (UFS_UPDATE(dvp, 0));
811                 }
812                 error = VOP_BWRITE(bp->b_vp, bp);
813                 ret = UFS_UPDATE(dvp, 1);
814                 if (error == 0)
815                         return (ret);
816                 return (error);
817         }
818
819         /*
820          * If dp->i_count is non-zero, then namei found space for the new
821          * entry in the range dp->i_offset to dp->i_offset + dp->i_count
822          * in the directory. To use this space, we may have to compact
823          * the entries located there, by copying them together towards the
824          * beginning of the block, leaving the free space in one usable
825          * chunk at the end.
826          */
827
828         /*
829          * Increase size of directory if entry eats into new space.
830          * This should never push the size past a new multiple of
831          * DIRBLKSIZE.
832          *
833          * N.B. - THIS IS AN ARTIFACT OF 4.2 AND SHOULD NEVER HAPPEN.
834          */
835         if (dp->i_offset + dp->i_count > dp->i_size)
836                 dp->i_size = dp->i_offset + dp->i_count;
837         /*
838          * Get the block containing the space for the new directory entry.
839          */
840         error = UFS_BLKATOFF(dvp, (off_t)dp->i_offset, &dirbuf, &bp);
841         if (error) {
842                 if (DOINGSOFTDEP(dvp) && newdirbp != NULL)
843                         bdwrite(newdirbp);
844                 return (error);
845         }
846         /*
847          * Find space for the new entry. In the simple case, the entry at
848          * offset base will have the space. If it does not, then namei
849          * arranged that compacting the region dp->i_offset to
850          * dp->i_offset + dp->i_count would yield the space.
851          */
852         ep = (struct direct *)dirbuf;
853         dsize = ep->d_ino ? DIRSIZ(OFSFMT(dvp), ep) : 0;
854         spacefree = ep->d_reclen - dsize;
855         for (loc = ep->d_reclen; loc < dp->i_count; ) {
856                 nep = (struct direct *)(dirbuf + loc);
857
858                 /* Trim the existing slot (NB: dsize may be zero). */
859                 ep->d_reclen = dsize;
860                 ep = (struct direct *)((char *)ep + dsize);
861
862                 /* Read nep->d_reclen now as the bcopy() may clobber it. */
863                 loc += nep->d_reclen;
864                 if (nep->d_ino == 0) {
865                         /*
866                          * A mid-block unused entry. Such entries are
867                          * never created by the kernel, but fsck_ffs
868                          * can create them (and it doesn't fix them).
869                          *
870                          * Add up the free space, and initialise the
871                          * relocated entry since we don't bcopy it.
872                          */
873                         spacefree += nep->d_reclen;
874                         ep->d_ino = 0;
875                         dsize = 0;
876                         continue;
877                 }
878                 dsize = DIRSIZ(OFSFMT(dvp), nep);
879                 spacefree += nep->d_reclen - dsize;
880 #ifdef UFS_DIRHASH
881                 if (dp->i_dirhash != NULL)
882                         ufsdirhash_move(dp, nep,
883                             dp->i_offset + ((char *)nep - dirbuf),
884                             dp->i_offset + ((char *)ep - dirbuf));
885 #endif
886                 if (DOINGSOFTDEP(dvp))
887                         softdep_change_directoryentry_offset(dp, dirbuf,
888                             (caddr_t)nep, (caddr_t)ep, dsize); 
889                 else
890                         bcopy((caddr_t)nep, (caddr_t)ep, dsize);
891         }
892         /*
893          * Here, `ep' points to a directory entry containing `dsize' in-use
894          * bytes followed by `spacefree' unused bytes. If ep->d_ino == 0,
895          * then the entry is completely unused (dsize == 0). The value
896          * of ep->d_reclen is always indeterminate.
897          *
898          * Update the pointer fields in the previous entry (if any),
899          * copy in the new entry, and write out the block.
900          */
901         if (ep->d_ino == 0 ||
902             (ep->d_ino == WINO &&
903              bcmp(ep->d_name, dirp->d_name, dirp->d_namlen) == 0)) {
904                 if (spacefree + dsize < newentrysize)
905                         panic("ufs_direnter: compact1");
906                 dirp->d_reclen = spacefree + dsize;
907         } else {
908                 if (spacefree < newentrysize)
909                         panic("ufs_direnter: compact2");
910                 dirp->d_reclen = spacefree;
911                 ep->d_reclen = dsize;
912                 ep = (struct direct *)((char *)ep + dsize);
913         }
914 #ifdef UFS_DIRHASH
915         if (dp->i_dirhash != NULL && (ep->d_ino == 0 ||
916             dirp->d_reclen == spacefree))
917                 ufsdirhash_add(dp, dirp, dp->i_offset + ((char *)ep - dirbuf));
918 #endif
919         bcopy((caddr_t)dirp, (caddr_t)ep, (u_int)newentrysize);
920 #ifdef UFS_DIRHASH
921         if (dp->i_dirhash != NULL)
922                 ufsdirhash_checkblock(dp, dirbuf -
923                     (dp->i_offset & (DIRBLKSIZ - 1)),
924                     dp->i_offset & ~(DIRBLKSIZ - 1));
925 #endif
926
927         if (DOINGSOFTDEP(dvp)) {
928                 softdep_setup_directory_add(bp, dp,
929                     dp->i_offset + (caddr_t)ep - dirbuf, dirp->d_ino, newdirbp);
930                 bdwrite(bp);
931         } else {
932                 if (DOINGASYNC(dvp)) {
933                         bdwrite(bp);
934                         error = 0;
935                 } else {
936                         error = bowrite(bp);
937                 }
938         }
939         dp->i_flag |= IN_CHANGE | IN_UPDATE;
940         /*
941          * If all went well, and the directory can be shortened, proceed
942          * with the truncation. Note that we have to unlock the inode for
943          * the entry that we just entered, as the truncation may need to
944          * lock other inodes which can lead to deadlock if we also hold a
945          * lock on the newly entered node.
946          */
947         if (error == 0 && dp->i_endoff && dp->i_endoff < dp->i_size) {
948                 if (tvp != NULL)
949                         VOP_UNLOCK(tvp, NULL, 0, td);
950 #ifdef UFS_DIRHASH
951                 if (dp->i_dirhash != NULL)
952                         ufsdirhash_dirtrunc(dp, dp->i_endoff);
953 #endif
954                 (void) UFS_TRUNCATE(dvp, (off_t)dp->i_endoff, IO_SYNC, cred, td);
955                 if (tvp != NULL)
956                         vn_lock(tvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
957         }
958         return (error);
959 }
960
961 /*
962  * Remove a directory entry after a call to namei, using
963  * the parameters which it left in nameidata. The entry
964  * dp->i_offset contains the offset into the directory of the
965  * entry to be eliminated.  The dp->i_count field contains the
966  * size of the previous record in the directory.  If this
967  * is 0, the first entry is being deleted, so we need only
968  * zero the inode number to mark the entry as free.  If the
969  * entry is not the first in the directory, we must reclaim
970  * the space of the now empty record by adding the record size
971  * to the size of the previous entry.
972  */
973 int
974 ufs_dirremove(dvp, ip, flags, isrmdir)
975         struct vnode *dvp;
976         struct inode *ip;
977         int flags;
978         int isrmdir;
979 {
980         struct inode *dp;
981         struct direct *ep;
982         struct buf *bp;
983         int error;
984
985         dp = VTOI(dvp);
986
987         if (flags & CNP_DOWHITEOUT) {
988                 /*
989                  * Whiteout entry: set d_ino to WINO.
990                  */
991                 if ((error =
992                     UFS_BLKATOFF(dvp, (off_t)dp->i_offset, (char **)&ep, &bp)) != 0)
993                         return (error);
994                 ep->d_ino = WINO;
995                 ep->d_type = DT_WHT;
996                 goto out;
997         }
998
999         if ((error = UFS_BLKATOFF(dvp,
1000             (off_t)(dp->i_offset - dp->i_count), (char **)&ep, &bp)) != 0)
1001                 return (error);
1002 #ifdef UFS_DIRHASH
1003         /*
1004          * Remove the dirhash entry. This is complicated by the fact
1005          * that `ep' is the previous entry when dp->i_count != 0.
1006          */
1007         if (dp->i_dirhash != NULL)
1008                 ufsdirhash_remove(dp, (dp->i_count == 0) ? ep :
1009                    (struct direct *)((char *)ep + ep->d_reclen), dp->i_offset);
1010 #endif
1011         if (dp->i_count == 0) {
1012                 /*
1013                  * First entry in block: set d_ino to zero.
1014                  */
1015                 ep->d_ino = 0;
1016         } else {
1017                 /*
1018                  * Collapse new free space into previous entry.
1019                  */
1020                 ep->d_reclen += dp->i_reclen;
1021         }
1022 #ifdef UFS_DIRHASH
1023         if (dp->i_dirhash != NULL)
1024                 ufsdirhash_checkblock(dp, (char *)ep -
1025                     ((dp->i_offset - dp->i_count) & (DIRBLKSIZ - 1)),
1026                     dp->i_offset & ~(DIRBLKSIZ - 1));
1027 #endif
1028 out:
1029         if (DOINGSOFTDEP(dvp)) {
1030                 if (ip) {
1031                         ip->i_effnlink--;
1032                         softdep_change_linkcnt(ip);
1033                         softdep_setup_remove(bp, dp, ip, isrmdir);
1034                 }
1035                 if (softdep_slowdown(dvp)) {
1036                         error = VOP_BWRITE(bp->b_vp, bp);
1037                 } else {
1038                         bdwrite(bp);
1039                         error = 0;
1040                 }
1041         } else {
1042                 if (ip) {
1043                         ip->i_effnlink--;
1044                         ip->i_nlink--;
1045                         ip->i_flag |= IN_CHANGE;
1046                 }
1047                 if (flags & CNP_DOWHITEOUT)
1048                         error = VOP_BWRITE(bp->b_vp, bp);
1049                 else if (DOINGASYNC(dvp) && dp->i_count != 0) {
1050                         bdwrite(bp);
1051                         error = 0;
1052                 } else
1053                         error = bowrite(bp);
1054         }
1055         dp->i_flag |= IN_CHANGE | IN_UPDATE;
1056         return (error);
1057 }
1058
1059 /*
1060  * Rewrite an existing directory entry to point at the inode
1061  * supplied.  The parameters describing the directory entry are
1062  * set up by a call to namei.
1063  */
1064 int
1065 ufs_dirrewrite(dp, oip, newinum, newtype, isrmdir)
1066         struct inode *dp, *oip;
1067         ino_t newinum;
1068         int newtype;
1069         int isrmdir;
1070 {
1071         struct buf *bp;
1072         struct direct *ep;
1073         struct vnode *vdp = ITOV(dp);
1074         int error;
1075
1076         error = UFS_BLKATOFF(vdp, (off_t)dp->i_offset, (char **)&ep, &bp);
1077         if (error)
1078                 return (error);
1079         ep->d_ino = newinum;
1080         if (!OFSFMT(vdp))
1081                 ep->d_type = newtype;
1082         oip->i_effnlink--;
1083         if (DOINGSOFTDEP(vdp)) {
1084                 softdep_change_linkcnt(oip);
1085                 softdep_setup_directory_change(bp, dp, oip, newinum, isrmdir);
1086                 bdwrite(bp);
1087         } else {
1088                 oip->i_nlink--;
1089                 oip->i_flag |= IN_CHANGE;
1090                 if (DOINGASYNC(vdp)) {
1091                         bdwrite(bp);
1092                         error = 0;
1093                 } else {
1094                         error = bowrite(bp);
1095                 }
1096         }
1097         dp->i_flag |= IN_CHANGE | IN_UPDATE;
1098         return (error);
1099 }
1100
1101 /*
1102  * Check if a directory is empty or not.
1103  * Inode supplied must be locked.
1104  *
1105  * Using a struct dirtemplate here is not precisely
1106  * what we want, but better than using a struct direct.
1107  *
1108  * NB: does not handle corrupted directories.
1109  */
1110 int
1111 ufs_dirempty(ip, parentino, cred)
1112         struct inode *ip;
1113         ino_t parentino;
1114         struct ucred *cred;
1115 {
1116         off_t off;
1117         struct dirtemplate dbuf;
1118         struct direct *dp = (struct direct *)&dbuf;
1119         int error, count, namlen;
1120 #define MINDIRSIZ (sizeof (struct dirtemplate) / 2)
1121
1122         for (off = 0; off < ip->i_size; off += dp->d_reclen) {
1123                 error = vn_rdwr(UIO_READ, ITOV(ip), (caddr_t)dp, MINDIRSIZ, off,
1124                    UIO_SYSSPACE, IO_NODELOCKED, cred, &count, NULL);
1125                 /*
1126                  * Since we read MINDIRSIZ, residual must
1127                  * be 0 unless we're at end of file.
1128                  */
1129                 if (error || count != 0)
1130                         return (0);
1131                 /* avoid infinite loops */
1132                 if (dp->d_reclen == 0)
1133                         return (0);
1134                 /* skip empty entries */
1135                 if (dp->d_ino == 0 || dp->d_ino == WINO)
1136                         continue;
1137                 /* accept only "." and ".." */
1138 #               if (BYTE_ORDER == LITTLE_ENDIAN)
1139                         if (OFSFMT(ITOV(ip)))
1140                                 namlen = dp->d_type;
1141                         else
1142                                 namlen = dp->d_namlen;
1143 #               else
1144                         namlen = dp->d_namlen;
1145 #               endif
1146                 if (namlen > 2)
1147                         return (0);
1148                 if (dp->d_name[0] != '.')
1149                         return (0);
1150                 /*
1151                  * At this point namlen must be 1 or 2.
1152                  * 1 implies ".", 2 implies ".." if second
1153                  * char is also "."
1154                  */
1155                 if (namlen == 1 && dp->d_ino == ip->i_number)
1156                         continue;
1157                 if (dp->d_name[1] == '.' && dp->d_ino == parentino)
1158                         continue;
1159                 return (0);
1160         }
1161         return (1);
1162 }
1163
1164 /*
1165  * Check if source directory is in the path of the target directory.
1166  * Target is supplied locked, source is unlocked.
1167  * The target is always vput before returning.
1168  */
1169 int
1170 ufs_checkpath(source, target, cred)
1171         struct inode *source, *target;
1172         struct ucred *cred;
1173 {
1174         struct vnode *vp;
1175         int error, rootino, 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         rootino = ROOTINO;
1184         error = 0;
1185         if (target->i_number == rootino)
1186                 goto out;
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, cred, (int *)0, NULL);
1196                 if (error != 0)
1197                         break;
1198 #               if (BYTE_ORDER == LITTLE_ENDIAN)
1199                         if (OFSFMT(vp))
1200                                 namlen = dirbuf.dotdot_type;
1201                         else
1202                                 namlen = dirbuf.dotdot_namlen;
1203 #               else
1204                         namlen = dirbuf.dotdot_namlen;
1205 #               endif
1206                 if (namlen != 2 ||
1207                     dirbuf.dotdot_name[0] != '.' ||
1208                     dirbuf.dotdot_name[1] != '.') {
1209                         error = ENOTDIR;
1210                         break;
1211                 }
1212                 if (dirbuf.dotdot_ino == source->i_number) {
1213                         error = EINVAL;
1214                         break;
1215                 }
1216                 if (dirbuf.dotdot_ino == rootino)
1217                         break;
1218                 vput(vp);
1219                 error = VFS_VGET(vp->v_mount, dirbuf.dotdot_ino, &vp);
1220                 if (error) {
1221                         vp = NULL;
1222                         break;
1223                 }
1224         }
1225
1226 out:
1227         if (error == ENOTDIR)
1228                 printf("checkpath: .. not a directory\n");
1229         if (vp != NULL)
1230                 vput(vp);
1231         return (error);
1232 }