]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/fs/cd9660/cd9660_lookup.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / fs / 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  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *      from: @(#)ufs_lookup.c  7.33 (Berkeley) 5/19/91
35  *      @(#)cd9660_lookup.c     8.2 (Berkeley) 1/23/94
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/namei.h>
44 #include <sys/bio.h>
45 #include <sys/buf.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48
49 #include <fs/cd9660/iso.h>
50 #include <fs/cd9660/cd9660_node.h>
51 #include <fs/cd9660/iso_rrip.h>
52
53 /*
54  * Convert a component of a pathname into a pointer to a locked inode.
55  * This is a very central and rather complicated routine.
56  * If the filesystem is not maintained in a strict tree hierarchy,
57  * this can result in a deadlock situation (see comments in code below).
58  *
59  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
60  * whether the name is to be looked up, created, renamed, or deleted.
61  * When CREATE, RENAME, or DELETE is specified, information usable in
62  * creating, renaming, or deleting a directory entry may be calculated.
63  * If flag has LOCKPARENT or'ed into it and the target of the pathname
64  * exists, lookup returns both the target and its parent directory locked.
65  * When creating or renaming and LOCKPARENT is specified, the target may
66  * not be ".".  When deleting and LOCKPARENT is specified, the target may
67  * be "."., but the caller must check to ensure it does an vrele and iput
68  * instead of two iputs.
69  *
70  * Overall outline of ufs_lookup:
71  *
72  *      search for name in directory, to found or notfound
73  * notfound:
74  *      if creating, return locked directory, leaving info on available slots
75  *      else return error
76  * found:
77  *      if at end of path and deleting, return information to allow delete
78  *      if at end of path and rewriting (RENAME and LOCKPARENT), lock target
79  *        inode and return info to allow rewrite
80  *      if not at end, add name to cache; if at end and neither creating
81  *        nor deleting, add name to cache
82  *
83  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
84  */
85 int
86 cd9660_lookup(ap)
87         struct vop_cachedlookup_args /* {
88                 struct vnode *a_dvp;
89                 struct vnode **a_vpp;
90                 struct componentname *a_cnp;
91         } */ *ap;
92 {
93         struct vnode *vdp;              /* vnode for directory being searched */
94         struct iso_node *dp;            /* inode for directory being searched */
95         struct iso_mnt *imp;            /* filesystem that directory is in */
96         struct buf *bp;                 /* a buffer of directory entries */
97         struct iso_directory_record *ep;/* the current directory entry */
98         struct iso_directory_record *ep2;/* copy of current directory entry */
99         int entryoffsetinblock;         /* offset of ep in bp's buffer */
100         int saveoffset = 0;             /* offset of last directory entry in dir */
101         doff_t i_diroff;                /* cached i_diroff value. */
102         doff_t i_offset;                /* cached i_offset value. */
103         int numdirpasses;               /* strategy for directory search */
104         doff_t endsearch;               /* offset to end directory search */
105         struct vnode *pdp;              /* saved dp during symlink work */
106         struct vnode *tdp;              /* returned by cd9660_vget_internal */
107         u_long bmask;                   /* block offset mask */
108         int error;
109         ino_t ino, i_ino;
110         int ltype, reclen;
111         u_short namelen;
112         int isoflags;
113         char altname[NAME_MAX];
114         int res;
115         int assoc, len;
116         char *name;
117         struct mount *mp;
118         struct vnode **vpp = ap->a_vpp;
119         struct componentname *cnp = ap->a_cnp;
120         int flags = cnp->cn_flags;
121         int nameiop = cnp->cn_nameiop;
122         struct thread *td = cnp->cn_thread;
123
124         ep2 = ep = NULL;
125         bp = NULL;
126         *vpp = NULL;
127         vdp = ap->a_dvp;
128         dp = VTOI(vdp);
129         imp = dp->i_mnt;
130
131         /*
132          * We now have a segment name to search for, and a directory to search.
133          */
134         ino = reclen = 0;
135         i_diroff = dp->i_diroff;
136         len = cnp->cn_namelen;
137         name = cnp->cn_nameptr;
138
139         /*
140          * A leading `=' means, we are looking for an associated file
141          */
142         if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)))
143         {
144                 len--;
145                 name++;
146         }
147
148         /*
149          * If there is cached information on a previous search of
150          * this directory, pick up where we last left off.
151          * We cache only lookups as these are the most common
152          * and have the greatest payoff. Caching CREATE has little
153          * benefit as it usually must search the entire directory
154          * to determine that the entry does not exist. Caching the
155          * location of the last DELETE or RENAME has not reduced
156          * profiling time and hence has been removed in the interest
157          * of simplicity.
158          */
159         bmask = imp->im_bmask;
160         if (nameiop != LOOKUP || i_diroff == 0 || i_diroff > dp->i_size) {
161                 entryoffsetinblock = 0;
162                 i_offset = 0;
163                 numdirpasses = 1;
164         } else {
165                 i_offset = i_diroff;
166                 if ((entryoffsetinblock = i_offset & bmask) &&
167                     (error = cd9660_blkatoff(vdp, (off_t)i_offset, NULL, &bp)))
168                                 return (error);
169                 numdirpasses = 2;
170                 nchstats.ncs_2passes++;
171         }
172         endsearch = dp->i_size;
173
174 searchloop:
175         while (i_offset < endsearch) {
176                 /*
177                  * If offset is on a block boundary,
178                  * read the next directory block.
179                  * Release previous if it exists.
180                  */
181                 if ((i_offset & bmask) == 0) {
182                         if (bp != NULL)
183                                 brelse(bp);
184                         if ((error =
185                             cd9660_blkatoff(vdp, (off_t)i_offset, NULL, &bp)) != 0)
186                                 return (error);
187                         entryoffsetinblock = 0;
188                 }
189                 /*
190                  * Get pointer to next entry.
191                  */
192                 ep = (struct iso_directory_record *)
193                         ((char *)bp->b_data + entryoffsetinblock);
194
195                 reclen = isonum_711(ep->length);
196                 if (reclen == 0) {
197                         /* skip to next block, if any */
198                         i_offset =
199                             (i_offset & ~bmask) + imp->logical_block_size;
200                         continue;
201                 }
202
203                 if (reclen < ISO_DIRECTORY_RECORD_SIZE)
204                         /* illegal entry, stop */
205                         break;
206
207                 if (entryoffsetinblock + reclen > imp->logical_block_size)
208                         /* entries are not allowed to cross boundaries */
209                         break;
210
211                 namelen = isonum_711(ep->name_len);
212                 isoflags = isonum_711(imp->iso_ftype == ISO_FTYPE_HIGH_SIERRA?
213                                       &ep->date[6]: ep->flags);
214
215                 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
216                         /* illegal entry, stop */
217                         break;
218
219                 /*
220                  * Check for a name match.
221                  */
222                 switch (imp->iso_ftype) {
223                 default:
224                         if (!(isoflags & 4) == !assoc) {
225                                 if ((len == 1
226                                      && *name == '.')
227                                     || (flags & ISDOTDOT)) {
228                                         if (namelen == 1
229                                             && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
230                                                 /*
231                                                  * Save directory entry's inode number and
232                                                  * release directory buffer.
233                                                  */
234                                                 i_ino = isodirino(ep, imp);
235                                                 goto found;
236                                         }
237                                         if (namelen != 1
238                                             || ep->name[0] != 0)
239                                                 goto notfound;
240                                 } else if (!(res = isofncmp(name, len,
241                                                             ep->name, namelen,
242                                                             imp->joliet_level,
243                                                             imp->im_flags,
244                                                             imp->im_d2l,
245                                                             imp->im_l2d))) {
246                                         if (isoflags & 2)
247                                                 ino = isodirino(ep, imp);
248                                         else
249                                                 ino = dbtob(bp->b_blkno)
250                                                         + entryoffsetinblock;
251                                         saveoffset = i_offset;
252                                 } else if (ino)
253                                         goto foundino;
254 #ifdef  NOSORTBUG       /* On some CDs directory entries are not sorted correctly */
255                                 else if (res < 0)
256                                         goto notfound;
257                                 else if (res > 0 && numdirpasses == 2)
258                                         numdirpasses++;
259 #endif
260                         }
261                         break;
262                 case ISO_FTYPE_RRIP:
263                         if (isonum_711(ep->flags)&2)
264                                 ino = isodirino(ep, imp);
265                         else
266                                 ino = dbtob(bp->b_blkno) + entryoffsetinblock;
267                         i_ino = ino;
268                         cd9660_rrip_getname(ep, altname, &namelen, &i_ino, imp);
269                         if (namelen == cnp->cn_namelen
270                             && !bcmp(name,altname,namelen))
271                                 goto found;
272                         ino = 0;
273                         break;
274                 }
275                 i_offset += reclen;
276                 entryoffsetinblock += reclen;
277         }
278         if (ino) {
279 foundino:
280                 i_ino = ino;
281                 if (saveoffset != i_offset) {
282                         if (lblkno(imp, i_offset) !=
283                             lblkno(imp, saveoffset)) {
284                                 if (bp != NULL)
285                                         brelse(bp);
286                                 if ((error = cd9660_blkatoff(vdp,
287                                     (off_t)saveoffset, NULL, &bp)) != 0)
288                                         return (error);
289                         }
290                         entryoffsetinblock = saveoffset & bmask;
291                         ep = (struct iso_directory_record *)
292                                 ((char *)bp->b_data + entryoffsetinblock);
293                         reclen = isonum_711(ep->length);
294                         i_offset = saveoffset;
295                 }
296                 goto found;
297         }
298 notfound:
299         /*
300          * If we started in the middle of the directory and failed
301          * to find our target, we must check the beginning as well.
302          */
303         if (numdirpasses == 2) {
304                 numdirpasses--;
305                 i_offset = 0;
306                 endsearch = i_diroff;
307                 goto searchloop;
308         }
309         if (bp != NULL)
310                 brelse(bp);
311
312         /*
313          * Insert name into cache (as non-existent) if appropriate.
314          */
315         if (cnp->cn_flags & MAKEENTRY)
316                 cache_enter(vdp, *vpp, cnp);
317         if (nameiop == CREATE || nameiop == RENAME)
318                 return (EROFS);
319         return (ENOENT);
320
321 found:
322         if (numdirpasses == 2)
323                 nchstats.ncs_pass2++;
324
325         /*
326          * Found component in pathname.
327          * If the final component of path name, save information
328          * in the cache as to where the entry was found.
329          */
330         if ((flags & ISLASTCN) && nameiop == LOOKUP)
331                 dp->i_diroff = i_offset;
332
333         /*
334          * Step through the translation in the name.  We do not `vput' the
335          * directory because we may need it again if a symbolic link
336          * is relative to the current directory.  Instead we save it
337          * unlocked as "pdp".  We must get the target inode before unlocking
338          * the directory to insure that the inode will not be removed
339          * before we get it.  We prevent deadlock by always fetching
340          * inodes from the root, moving down the directory tree. Thus
341          * when following backward pointers ".." we must unlock the
342          * parent directory before getting the requested directory.
343          * There is a potential race condition here if both the current
344          * and parent directories are removed before the `vget' for the
345          * inode associated with ".." returns.  We hope that this occurs
346          * infrequently since we cannot avoid this race condition without
347          * implementing a sophisticated deadlock detection algorithm.
348          * Note also that this simple deadlock detection scheme will not
349          * work if the filesystem has any hard links other than ".."
350          * that point backwards in the directory structure.
351          */
352         pdp = vdp;
353
354         /*
355          * Make a copy of the directory entry for non "." lookups so
356          * we can drop the buffer before calling vget() to avoid a
357          * lock order reversal between the vnode lock and the buffer
358          * lock.
359          */
360         if (dp->i_number != i_ino) {
361                 ep2 = malloc(reclen, M_TEMP, M_WAITOK);
362                 bcopy(ep, ep2, reclen);
363                 ep = ep2;
364         }
365         brelse(bp);
366
367         /*
368          * If ino is different from i_ino,
369          * it's a relocated directory.
370          */
371         if (flags & ISDOTDOT) {
372                 /*
373                  * Expanded copy of vn_vget_ino() so that we can use
374                  * cd9660_vget_internal().
375                  */
376                 mp = pdp->v_mount;
377                 ltype = VOP_ISLOCKED(pdp, td);
378                 for (;;) {
379                         error = vfs_busy(mp, LK_NOWAIT, NULL, curthread);
380                         if (error == 0)
381                                 break;
382                         VOP_UNLOCK(pdp, 0, td);
383                         pause("vn_vget", 1);
384                         vn_lock(pdp, ltype | LK_RETRY, td);
385                         if (pdp->v_iflag & VI_DOOMED)
386                                 return (ENOENT);
387                 }
388                 VOP_UNLOCK(pdp, 0, td);
389                 error = cd9660_vget_internal(vdp->v_mount, i_ino,
390                                              cnp->cn_lkflags, &tdp,
391                                              i_ino != ino, ep);
392                 free(ep2, M_TEMP);
393                 vfs_unbusy(mp, td);
394                 vn_lock(pdp, ltype | LK_RETRY, td);
395                 if (pdp->v_iflag & VI_DOOMED) {
396                         if (error == 0)
397                                 vput(tdp);
398                         error = ENOENT;
399                 }
400                 if (error)
401                         return (error);
402                 *vpp = tdp;
403         } else if (dp->i_number == i_ino) {
404                 VREF(vdp);      /* we want ourself, ie "." */
405                 /*
406                  * When we lookup "." we still can be asked to lock it
407                  * differently.
408                  */
409                 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
410                 if (ltype != VOP_ISLOCKED(vdp, td)) {
411                         if (ltype == LK_EXCLUSIVE)
412                                 vn_lock(vdp, LK_UPGRADE | LK_RETRY, td);
413                         else /* if (ltype == LK_SHARED) */
414                                 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY, td);
415                 }
416                 *vpp = vdp;
417         } else {
418                 error = cd9660_vget_internal(vdp->v_mount, i_ino,
419                                              cnp->cn_lkflags, &tdp,
420                                              i_ino != ino, ep);
421                 free(ep2, M_TEMP);
422                 if (error)
423                         return (error);
424                 *vpp = tdp;
425         }
426
427         /*
428          * Insert name into cache if appropriate.
429          */
430         if (cnp->cn_flags & MAKEENTRY)
431                 cache_enter(vdp, *vpp, cnp);
432         return (0);
433 }
434
435 /*
436  * Return buffer with the contents of block "offset" from the beginning of
437  * directory "ip".  If "res" is non-zero, fill it in with a pointer to the
438  * remaining space in the directory.
439  */
440 int
441 cd9660_blkatoff(vp, offset, res, bpp)
442         struct vnode *vp;
443         off_t offset;
444         char **res;
445         struct buf **bpp;
446 {
447         struct iso_node *ip;
448         struct iso_mnt *imp;
449         struct buf *bp;
450         daddr_t lbn;
451         int bsize, bshift, error;
452
453         ip = VTOI(vp);
454         imp = ip->i_mnt;
455         lbn = lblkno(imp, offset);
456         bsize = blksize(imp, ip, lbn);
457         bshift = imp->im_bshift;
458
459         if ((error = bread(vp, lbn, bsize, NOCRED, &bp)) != 0) {
460                 brelse(bp);
461                 *bpp = NULL;
462                 return (error);
463         }
464
465         /*
466          * We must BMAP the buffer because the directory code may use b_blkno
467          * to calculate the inode for certain types of directory entries.
468          * We could get away with not doing it before we VMIO-backed the
469          * directories because the buffers would get freed atomically with
470          * the invalidation of their data.  But with VMIO-backed buffers
471          * the buffers may be freed and then later reconstituted - and the
472          * reconstituted buffer will have no knowledge of b_blkno.
473          */
474         if (bp->b_blkno == bp->b_lblkno) {
475                 bp->b_blkno = (ip->iso_start + bp->b_lblkno) << (bshift - DEV_BSHIFT);
476         }
477
478         if (res)
479                 *res = (char *)bp->b_data + blkoff(imp, offset);
480         *bpp = bp;
481         return (0);
482 }