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