Per-CPU VFS Namecache Effectiveness Statistics:
[dragonfly.git] / sys / vfs / isofs / cd9660 / cd9660_lookup.c
1 /*-
2  * Copyright (c) 1989, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley
6  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
7  * Support code is derived from software contributed to Berkeley
8  * by Atsushi Murai (amurai@spec.co.jp).
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  *      from: @(#)ufs_lookup.c  7.33 (Berkeley) 5/19/91
39  *
40  *      @(#)cd9660_lookup.c     8.2 (Berkeley) 1/23/94
41  * $FreeBSD: src/sys/isofs/cd9660/cd9660_lookup.c,v 1.23.2.2 2001/11/04 06:19:47 dillon Exp $
42  * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_lookup.c,v 1.10 2004/04/02 05:46:03 hmp Exp $
43  */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/namei.h>
49 #include <sys/buf.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52
53 #include "iso.h"
54 #include "cd9660_node.h"
55 #include "iso_rrip.h"
56
57 /*
58  * Convert a component of a pathname into a pointer to a locked inode.
59  * This is a very central and rather complicated routine.
60  * If the file system is not maintained in a strict tree hierarchy,
61  * this can result in a deadlock situation (see comments in code below).
62  *
63  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
64  * whether the name is to be looked up, created, renamed, or deleted.
65  * When CREATE, RENAME, or DELETE is specified, information usable in
66  * creating, renaming, or deleting a directory entry may be calculated.
67  * If flag has LOCKPARENT or'ed into it and the target of the pathname
68  * exists, lookup returns both the target and its parent directory locked.
69  * When creating or renaming and LOCKPARENT is specified, the target may
70  * not be ".".  When deleting and LOCKPARENT is specified, the target may
71  * be "."., but the caller must check to ensure it does an vrele and iput
72  * instead of two iputs.
73  *
74  * Overall outline of ufs_lookup:
75  *
76  *      search for name in directory, to found or notfound
77  * notfound:
78  *      if creating, return locked directory, leaving info on available slots
79  *      else return error
80  * found:
81  *      if at end of path and deleting, return information to allow delete
82  *      if at end of path and rewriting (RENAME and LOCKPARENT), lock target
83  *        inode and return info to allow rewrite
84  *      if not at end, add name to cache; if at end and neither creating
85  *        nor deleting, add name to cache
86  *
87  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
88  */
89 int
90 cd9660_lookup(ap)
91         struct vop_cachedlookup_args /* {
92                 struct vnode *a_dvp;
93                 struct vnode **a_vpp;
94                 struct componentname *a_cnp;
95         } */ *ap;
96 {
97         struct vnode *vdp;      /* vnode for directory being searched */
98         globaldata_t gd = mycpu;
99         struct iso_node *dp;    /* inode for directory being searched */
100         struct iso_mnt *imp;    /* file system that directory is in */
101         struct buf *bp;                 /* a buffer of directory entries */
102         struct iso_directory_record *ep = 0;/* the current directory entry */
103         int entryoffsetinblock;         /* offset of ep in bp's buffer */
104         int saveoffset = 0;             /* offset of last directory entry in dir */
105         int numdirpasses;               /* strategy for directory search */
106         doff_t endsearch;               /* offset to end directory search */
107         struct vnode *pdp;              /* saved dp during symlink work */
108         struct vnode *tdp;              /* returned by cd9660_vget_internal */
109         u_long bmask;                   /* block offset mask */
110         int lockparent;                 /* 1 => lockparent flag is set */
111         int wantparent;                 /* 1 => wantparent or lockparent flag */
112         int error;
113         ino_t ino = 0;
114         int reclen;
115         u_short namelen;
116         int isoflags;
117         char altname[NAME_MAX];
118         int res;
119         int assoc, len;
120         char *name;
121         struct vnode **vpp = ap->a_vpp;
122         struct componentname *cnp = ap->a_cnp;
123         int flags = cnp->cn_flags;
124         int nameiop = cnp->cn_nameiop;
125         struct thread *td = cnp->cn_td;
126
127         bp = NULL;
128         *vpp = NULL;
129         vdp = ap->a_dvp;
130         dp = VTOI(vdp);
131         imp = dp->i_mnt;
132         lockparent = flags & CNP_LOCKPARENT;
133         wantparent = flags & (CNP_LOCKPARENT | CNP_WANTPARENT);
134         cnp->cn_flags &= ~CNP_PDIRUNLOCK;
135
136         /*
137          * We now have a segment name to search for, and a directory to search.
138          */
139
140         len = cnp->cn_namelen;
141         name = cnp->cn_nameptr;
142         /*
143          * A leading `=' means, we are looking for an associated file
144          */
145         if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)))
146         {
147                 len--;
148                 name++;
149         }
150
151         /*
152          * If there is cached information on a previous search of
153          * this directory, pick up where we last left off.
154          * We cache only lookups as these are the most common
155          * and have the greatest payoff. Caching CREATE has little
156          * benefit as it usually must search the entire directory
157          * to determine that the entry does not exist. Caching the
158          * location of the last DELETE or RENAME has not reduced
159          * profiling time and hence has been removed in the interest
160          * of simplicity.
161          */
162         bmask = imp->im_bmask;
163         if (nameiop != NAMEI_LOOKUP || dp->i_diroff == 0 ||
164             dp->i_diroff > dp->i_size) {
165                 entryoffsetinblock = 0;
166                 dp->i_offset = 0;
167                 numdirpasses = 1;
168         } else {
169                 dp->i_offset = dp->i_diroff;
170                 if ((entryoffsetinblock = dp->i_offset & bmask) &&
171                     (error = cd9660_blkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)))
172                                 return (error);
173                 numdirpasses = 2;
174                 gd->gd_nchstats->ncs_2passes++;
175         }
176         endsearch = dp->i_size;
177         
178 searchloop:
179         while (dp->i_offset < endsearch) {
180                 /*
181                  * If offset is on a block boundary,
182                  * read the next directory block.
183                  * Release previous if it exists.
184                  */
185                 if ((dp->i_offset & bmask) == 0) {
186                         if (bp != NULL)
187                                 brelse(bp);
188                         if ((error =
189                             cd9660_blkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)) != 0)
190                                 return (error);
191                         entryoffsetinblock = 0;
192                 }
193                 /*
194                  * Get pointer to next entry.
195                  */
196                 ep = (struct iso_directory_record *)
197                         ((char *)bp->b_data + entryoffsetinblock);
198                 
199                 reclen = isonum_711(ep->length);
200                 if (reclen == 0) {
201                         /* skip to next block, if any */
202                         dp->i_offset =
203                             (dp->i_offset & ~bmask) + imp->logical_block_size;
204                         continue;
205                 }
206
207                 if (reclen < ISO_DIRECTORY_RECORD_SIZE)
208                         /* illegal entry, stop */
209                         break;
210
211                 if (entryoffsetinblock + reclen > imp->logical_block_size)
212                         /* entries are not allowed to cross boundaries */
213                         break;
214                 
215                 namelen = isonum_711(ep->name_len);
216                 isoflags = isonum_711(imp->iso_ftype == ISO_FTYPE_HIGH_SIERRA?
217                                       &ep->date[6]: ep->flags);
218
219                 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
220                         /* illegal entry, stop */
221                         break;
222                 
223                 /*
224                  * Check for a name match.
225                  */
226                 switch (imp->iso_ftype) {
227                 default:
228                         if (!(isoflags & 4) == !assoc) {
229                                 if ((len == 1
230                                      && *name == '.')
231                                     || (flags & CNP_ISDOTDOT)) {
232                                         if (namelen == 1
233                                             && ep->name[0] == ((flags & CNP_ISDOTDOT) ? 1 : 0)) {
234                                                 /*
235                                                  * Save directory entry's inode number and
236                                                  * release directory buffer.
237                                                  */
238                                                 dp->i_ino = isodirino(ep, imp);
239                                                 goto found;
240                                         }
241                                         if (namelen != 1
242                                             || ep->name[0] != 0)
243                                                 goto notfound;
244                                 } else if (!(res = isofncmp(name, len, ep->name, namelen, imp->joliet_level))) {
245                                         if (isoflags & 2)
246                                                 ino = isodirino(ep, imp);
247                                         else
248                                                 ino = dbtob(bp->b_blkno)
249                                                         + entryoffsetinblock;
250                                         saveoffset = dp->i_offset;
251                                 } else if (ino)
252                                         goto foundino;
253 #ifdef  NOSORTBUG       /* On some CDs directory entries are not sorted correctly */
254                                 else if (res < 0)
255                                         goto notfound;
256                                 else if (res > 0 && numdirpasses == 2)
257                                         numdirpasses++;
258 #endif
259                         }
260                         break;
261                 case ISO_FTYPE_RRIP:
262                         if (isonum_711(ep->flags)&2)
263                                 ino = isodirino(ep, imp);
264                         else
265                                 ino = dbtob(bp->b_blkno) + entryoffsetinblock;
266                         dp->i_ino = ino;
267                         cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
268                         if (namelen == cnp->cn_namelen
269                             && !bcmp(name,altname,namelen))
270                                 goto found;
271                         ino = 0;
272                         break;
273                 }
274                 dp->i_offset += reclen;
275                 entryoffsetinblock += reclen;
276         }
277         if (ino) {
278 foundino:
279                 dp->i_ino = ino;
280                 if (saveoffset != dp->i_offset) {
281                         if (lblkno(imp, dp->i_offset) !=
282                             lblkno(imp, saveoffset)) {
283                                 if (bp != NULL)
284                                         brelse(bp);
285                                 if ((error = cd9660_blkatoff(vdp,
286                                     (off_t)saveoffset, NULL, &bp)) != 0)
287                                         return (error);
288                         }
289                         entryoffsetinblock = saveoffset & bmask;
290                         ep = (struct iso_directory_record *)
291                                 ((char *)bp->b_data + entryoffsetinblock);
292                         dp->i_offset = saveoffset;
293                 }
294                 goto found;
295         }
296 notfound:
297         /*
298          * If we started in the middle of the directory and failed
299          * to find our target, we must check the beginning as well.
300          */
301         if (numdirpasses == 2) {
302                 numdirpasses--;
303                 dp->i_offset = 0;
304                 endsearch = dp->i_diroff;
305                 goto searchloop;
306         }
307         if (bp != NULL)
308                 brelse(bp);
309
310         /*
311          * Insert name into cache (as non-existent) if appropriate.
312          */
313         if (cnp->cn_flags & CNP_MAKEENTRY)
314                 cache_enter(vdp, NCPNULL, *vpp, cnp);
315         if (nameiop == NAMEI_CREATE || nameiop == NAMEI_RENAME)
316                 return (EROFS);
317         return (ENOENT);
318
319 found:
320         if (numdirpasses == 2)
321                 gd->gd_nchstats->ncs_pass2++;
322         
323         /*
324          * Found component in pathname.
325          * If the final component of path name, save information
326          * in the cache as to where the entry was found.
327          */
328         if ((flags & CNP_ISLASTCN) && nameiop == NAMEI_LOOKUP)
329                 dp->i_diroff = dp->i_offset;
330
331         /*
332          * Step through the translation in the name.  We do not `iput' the
333          * directory because we may need it again if a symbolic link
334          * is relative to the current directory.  Instead we save it
335          * unlocked as "pdp".  We must get the target inode before unlocking
336          * the directory to insure that the inode will not be removed
337          * before we get it.  We prevent deadlock by always fetching
338          * inodes from the root, moving down the directory tree. Thus
339          * when following backward pointers ".." we must unlock the
340          * parent directory before getting the requested directory.
341          * There is a potential race condition here if both the current
342          * and parent directories are removed before the `iget' for the
343          * inode associated with ".." returns.  We hope that this occurs
344          * infrequently since we cannot avoid this race condition without
345          * implementing a sophisticated deadlock detection algorithm.
346          * Note also that this simple deadlock detection scheme will not
347          * work if the file system has any hard links other than ".."
348          * that point backwards in the directory structure.
349          */
350         pdp = vdp;
351         /*
352          * If ino is different from dp->i_ino,
353          * it's a relocated directory.
354          */
355         if (flags & CNP_ISDOTDOT) {
356                 VOP_UNLOCK(pdp, NULL, 0, td);   /* race to get the inode */
357                 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
358                                              dp->i_ino != ino, ep);
359                 brelse(bp);
360                 if (error) {
361                         vn_lock(pdp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
362                         return (error);
363                 }
364                 if (lockparent && (flags & CNP_ISLASTCN)) {
365                         if ((error = vn_lock(pdp, NULL, LK_EXCLUSIVE, td)) != 0) {
366                                 cnp->cn_flags |= CNP_PDIRUNLOCK;
367                                 vput(tdp);
368                                 return (error);
369                         }
370                 } else
371                         cnp->cn_flags |= CNP_PDIRUNLOCK;
372                 *vpp = tdp;
373         } else if (dp->i_number == dp->i_ino) {
374                 brelse(bp);
375                 VREF(vdp);      /* we want ourself, ie "." */
376                 *vpp = vdp;
377         } else {
378                 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
379                                              dp->i_ino != ino, ep);
380                 brelse(bp);
381                 if (error)
382                         return (error);
383                 if (!lockparent || !(flags & CNP_ISLASTCN)) {
384                         cnp->cn_flags |= CNP_PDIRUNLOCK;
385                         VOP_UNLOCK(pdp, NULL, 0, td);
386                 }
387                 *vpp = tdp;
388         }
389
390         /*
391          * Insert name into cache if appropriate.
392          */
393         if (cnp->cn_flags & CNP_MAKEENTRY)
394                 cache_enter(vdp, NCPNULL, *vpp, cnp);
395         return (0);
396 }
397
398 /*
399  * Return buffer with the contents of block "offset" from the beginning of
400  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
401  * remaining space in the directory.
402  */
403 int
404 cd9660_blkatoff(vp, offset, res, bpp)
405         struct vnode *vp;
406         off_t offset;
407         char **res;
408         struct buf **bpp;
409 {
410         struct iso_node *ip;
411         struct iso_mnt *imp;
412         struct buf *bp;
413         daddr_t lbn;
414         int bsize, error;
415
416         ip = VTOI(vp);
417         imp = ip->i_mnt;
418         lbn = lblkno(imp, offset);
419         bsize = blksize(imp, ip, lbn);
420
421         if ((error = bread(vp, lbn, bsize, &bp)) != 0) {
422                 brelse(bp);
423                 *bpp = NULL;
424                 return (error);
425         }
426
427         /*
428          * We must BMAP the buffer because the directory code may use b_blkno
429          * to calculate the inode for certain types of directory entries.
430          * We could get away with not doing it before we VMIO-backed the
431          * directories because the buffers would get freed atomically with
432          * the invalidation of their data.  But with VMIO-backed buffers
433          * the buffers may be freed and then later reconstituted - and the
434          * reconstituted buffer will have no knowledge of b_blkno.
435          */
436         if (bp->b_blkno == bp->b_lblkno) {
437                 error = VOP_BMAP(vp, bp->b_lblkno, NULL, 
438                             &bp->b_blkno, NULL, NULL);
439                 if (error) {
440                         bp->b_error = error;
441                         bp->b_flags |= B_ERROR;
442                         brelse(bp);
443                         *bpp = NULL;
444                         return (error);
445                 }
446         }
447
448         if (res)
449                 *res = (char *)bp->b_data + blkoff(imp, offset);
450         *bpp = bp;
451         return (0);
452 }