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