]> git.saurik.com Git - apple/xnu.git/blob - bsd/isofs/cd9660/cd9660_lookup.c
f1576ac0e3074ccd18a721769e377786d4dd1fb2
[apple/xnu.git] / bsd / isofs / cd9660 / cd9660_lookup.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /* $NetBSD: cd9660_lookup.c,v 1.13 1994/12/24 15:30:03 cgd Exp $ */
24
25 /*-
26 * Copyright (c) 1989, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
28 *
29 * This code is derived from software contributed to Berkeley
30 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
31 * Support code is derived from software contributed to Berkeley
32 * by Atsushi Murai (amurai@spec.co.jp).
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91
63 *
64 * @(#)cd9660_lookup.c 8.5 (Berkeley) 12/5/94
65 *
66 */
67
68 #include <sys/param.h>
69 #include <sys/vnode.h>
70 #include <sys/mount.h>
71 #include <sys/namei.h>
72 #include <sys/buf.h>
73 #include <sys/file.h>
74 #include <sys/utfconv.h>
75
76 #include <isofs/cd9660/iso.h>
77 #include <isofs/cd9660/cd9660_node.h>
78 #include <isofs/cd9660/iso_rrip.h>
79 #include <isofs/cd9660/cd9660_rrip.h>
80
81 struct nchstats iso_nchstats;
82
83 /*
84 * Convert a component of a pathname into a pointer to a locked inode.
85 * This is a very central and rather complicated routine.
86 * If the file system is not maintained in a strict tree hierarchy,
87 * this can result in a deadlock situation (see comments in code below).
88 *
89 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
90 * whether the name is to be looked up, created, renamed, or deleted.
91 * When CREATE, RENAME, or DELETE is specified, information usable in
92 * creating, renaming, or deleting a directory entry may be calculated.
93 * If flag has LOCKPARENT or'ed into it and the target of the pathname
94 * exists, lookup returns both the target and its parent directory locked.
95 * When creating or renaming and LOCKPARENT is specified, the target may
96 * not be ".". When deleting and LOCKPARENT is specified, the target may
97 * be "."., but the caller must check to ensure it does an vrele and iput
98 * instead of two iputs.
99 *
100 * Overall outline of ufs_lookup:
101 *
102 * check accessibility of directory
103 * look for name in cache, if found, then if at end of path
104 * and deleting or creating, drop it, else return name
105 * search for name in directory, to found or notfound
106 * notfound:
107 * if creating, return locked directory, leaving info on available slots
108 * else return error
109 * found:
110 * if at end of path and deleting, return information to allow delete
111 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
112 * inode and return info to allow rewrite
113 * if not at end, add name to cache; if at end and neither creating
114 * nor deleting, add name to cache
115 *
116 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
117 */
118 int
119 cd9660_lookup(struct vnop_lookup_args *ap)
120 {
121 register struct vnode *vdp; /* vnode for directory being searched */
122 register struct iso_node *dp; /* inode for directory being searched */
123 register struct iso_mnt *imp; /* file system that directory is in */
124 struct buf *bp; /* a buffer of directory entries */
125 struct iso_directory_record *ep = NULL;/* the current directory entry */
126 int entryoffsetinblock; /* offset of ep in bp's buffer */
127 int saveoffset = 0; /* offset of last directory entry in dir */
128 int numdirpasses; /* strategy for directory search */
129 doff_t endsearch; /* offset to end directory search */
130 struct vnode *pdp; /* saved dp during symlink work */
131 struct vnode *tdp; /* returned by cd9660_vget_internal */
132 u_long bmask; /* block offset mask */
133 int lockparent; /* 1 => lockparent flag is set */
134 int wantparent; /* 1 => wantparent or lockparent flag */
135 int wantassoc;
136 int error;
137 ino_t ino = 0;
138 int reclen;
139 u_short namelen;
140 int isoflags;
141 char altname[ISO_RRIP_NAMEMAX];
142 int res;
143 int len;
144 char *name;
145 struct vnode **vpp = ap->a_vpp;
146 struct componentname *cnp = ap->a_cnp;
147 int flags = cnp->cn_flags;
148 int nameiop = cnp->cn_nameiop;
149 vfs_context_t ctx = cnp->cn_context;
150 struct proc *p = vfs_context_proc(ctx);
151 size_t altlen;
152
153 bp = NULL;
154 *vpp = NULL;
155 vdp = ap->a_dvp;
156 dp = VTOI(vdp);
157 imp = dp->i_mnt;
158 lockparent = flags & LOCKPARENT;
159 wantparent = flags & (LOCKPARENT|WANTPARENT);
160 wantassoc = 0;
161
162
163 /*
164 * We now have a segment name to search for, and a directory to search.
165 *
166 * Before tediously performing a linear scan of the directory,
167 * check the name cache to see if the directory/name pair
168 * we are looking for is known already.
169 */
170 if ((error = cache_lookup(vdp, vpp, cnp))) {
171 if (error == ENOENT)
172 return (error);
173 return (0);
174 }
175
176 len = cnp->cn_namelen;
177 name = cnp->cn_nameptr;
178 altname[0] = '\0';
179 /*
180 * A "._" prefix means, we are looking for an associated file
181 */
182 if (imp->iso_ftype != ISO_FTYPE_RRIP &&
183 *name == ASSOCCHAR1 && *(name+1) == ASSOCCHAR2) {
184 wantassoc = 1;
185 len -= 2;
186 name += 2;
187 }
188 /*
189 * Decode search name into UCS-2 (Unicode)
190 */
191 if ((imp->iso_ftype == ISO_FTYPE_JOLIET) &&
192 !((len == 1 && *name == '.') || (flags & ISDOTDOT))) {
193 int flags1 = UTF_PRECOMPOSED;
194
195 if (BYTE_ORDER != BIG_ENDIAN)
196 flags1 |= UTF_REVERSE_ENDIAN;
197
198 (void) utf8_decodestr(name, len, (u_int16_t*) altname, &altlen,
199 sizeof(altname), 0, flags1);
200 name = altname;
201 len = altlen;
202 }
203 /*
204 * If there is cached information on a previous search of
205 * this directory, pick up where we last left off.
206 * We cache only lookups as these are the most common
207 * and have the greatest payoff. Caching CREATE has little
208 * benefit as it usually must search the entire directory
209 * to determine that the entry does not exist. Caching the
210 * location of the last DELETE or RENAME has not reduced
211 * profiling time and hence has been removed in the interest
212 * of simplicity.
213 */
214 bmask = imp->im_sector_size - 1;
215 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
216 dp->i_diroff > dp->i_size) {
217 entryoffsetinblock = 0;
218 dp->i_offset = 0;
219 numdirpasses = 1;
220 } else {
221 dp->i_offset = dp->i_diroff;
222
223 if ((entryoffsetinblock = dp->i_offset & bmask) &&
224 (error = cd9660_blkatoff(vdp, SECTOFF(imp, dp->i_offset), NULL, &bp)))
225 return (error);
226 numdirpasses = 2;
227 iso_nchstats.ncs_2passes++;
228 }
229 endsearch = dp->i_size;
230
231 searchloop:
232 while (dp->i_offset < endsearch) {
233 /*
234 * If offset is on a block boundary,
235 * read the next directory block.
236 * Release previous if it exists.
237 */
238 if ((dp->i_offset & bmask) == 0) {
239 if (bp != NULL)
240 buf_brelse(bp);
241 if ( (error = cd9660_blkatoff(vdp, SECTOFF(imp,dp->i_offset), NULL, &bp)) )
242 return (error);
243 entryoffsetinblock = 0;
244 }
245 /*
246 * Get pointer to next entry.
247 */
248 ep = (struct iso_directory_record *)
249 ((char *)buf_dataptr(bp) + entryoffsetinblock);
250
251 reclen = isonum_711(ep->length);
252 if (reclen == 0) {
253 /* skip to next block, if any */
254 dp->i_offset =
255 (dp->i_offset & ~bmask) + imp->im_sector_size;
256 continue;
257 }
258
259 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
260 /* illegal entry, stop */
261 break;
262 }
263 if (entryoffsetinblock + reclen > imp->im_sector_size) {
264 /* entries are not allowed to cross sector boundaries */
265 break;
266 }
267 namelen = isonum_711(ep->name_len);
268 isoflags = isonum_711(ep->flags);
269
270 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
271 /* illegal entry, stop */
272 break;
273 /*
274 * Check for a name match.
275 */
276 if (imp->iso_ftype == ISO_FTYPE_RRIP) {
277 if (isoflags & directoryBit)
278 ino = isodirino(ep, imp);
279 else
280 ino = ((daddr_t)buf_blkno(bp) << imp->im_bshift) + entryoffsetinblock;
281 dp->i_ino = ino;
282 cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
283 if (namelen == cnp->cn_namelen
284 && !bcmp(name,altname,namelen))
285 goto found;
286 ino = 0;
287 } else {
288 if ((!(isoflags & associatedBit)) == !wantassoc) {
289 if ((len == 1
290 && *name == '.')
291 || (flags & ISDOTDOT)) {
292 if (namelen == 1
293 && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
294 /*
295 * Save directory entry's inode number and
296 * release directory buffer.
297 */
298 dp->i_ino = isodirino(ep, imp);
299 goto found;
300 }
301 if (namelen != 1
302 || ep->name[0] != 0)
303 goto notfound;
304 } else if (imp->iso_ftype != ISO_FTYPE_JOLIET && !(res = isofncmp(name,len,
305 ep->name,namelen))) {
306 if ( isoflags & directoryBit )
307 ino = isodirino(ep, imp);
308 else
309 ino = ((daddr_t)buf_blkno(bp) << imp->im_bshift) + entryoffsetinblock;
310 saveoffset = dp->i_offset;
311 } else if (imp->iso_ftype == ISO_FTYPE_JOLIET && !(res = ucsfncmp((u_int16_t*)name, len,
312 (u_int16_t*) ep->name, namelen))) {
313 if ( isoflags & directoryBit )
314 ino = isodirino(ep, imp);
315 else
316 ino = ((daddr_t)buf_blkno(bp) << imp->im_bshift) + entryoffsetinblock;
317 saveoffset = dp->i_offset;
318 } else if (ino)
319 goto foundino;
320 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
321 else if (res < 0)
322 goto notfound;
323 else if (res > 0 && numdirpasses == 2)
324 numdirpasses++;
325 #endif
326 }
327 }
328 dp->i_offset += reclen;
329 entryoffsetinblock += reclen;
330 } /* endwhile */
331
332 if (ino) {
333 foundino:
334 dp->i_ino = ino;
335 if (saveoffset != dp->i_offset) {
336 if (lblkno(imp, dp->i_offset) !=
337 lblkno(imp, saveoffset)) {
338 if (bp != NULL)
339 buf_brelse(bp);
340 if ( (error = cd9660_blkatoff(vdp, SECTOFF(imp, saveoffset), NULL, &bp)) )
341 return (error);
342 }
343 entryoffsetinblock = saveoffset & bmask;
344 ep = (struct iso_directory_record *)
345 ((char *)buf_dataptr(bp) + entryoffsetinblock);
346 dp->i_offset = saveoffset;
347 }
348 goto found;
349 }
350 notfound:
351 /*
352 * If we started in the middle of the directory and failed
353 * to find our target, we must check the beginning as well.
354 */
355 if (numdirpasses == 2) {
356 numdirpasses--;
357 dp->i_offset = 0;
358 endsearch = dp->i_diroff;
359 goto searchloop;
360 }
361 if (bp != NULL)
362 buf_brelse(bp);
363
364 /*
365 * Insert name into cache (as non-existent) if appropriate.
366 */
367 if (cnp->cn_flags & MAKEENTRY)
368 cache_enter(vdp, *vpp, cnp);
369 return (ENOENT);
370
371 found:
372 if (numdirpasses == 2)
373 iso_nchstats.ncs_pass2++;
374
375 /*
376 * Found component in pathname.
377 * If the final component of path name, save information
378 * in the cache as to where the entry was found.
379 */
380 if ((flags & ISLASTCN) && nameiop == LOOKUP)
381 dp->i_diroff = dp->i_offset;
382
383 /*
384 * Step through the translation in the name. We do not `iput' the
385 * directory because we may need it again if a symbolic link
386 * is relative to the current directory. Instead we save it
387 * unlocked as "pdp". We must get the target inode before unlocking
388 * the directory to insure that the inode will not be removed
389 * before we get it. We prevent deadlock by always fetching
390 * inodes from the root, moving down the directory tree. Thus
391 * when following backward pointers ".." we must unlock the
392 * parent directory before getting the requested directory.
393 * There is a potential race condition here if both the current
394 * and parent directories are removed before the `iget' for the
395 * inode associated with ".." returns. We hope that this occurs
396 * infrequently since we cannot avoid this race condition without
397 * implementing a sophisticated deadlock detection algorithm.
398 * Note also that this simple deadlock detection scheme will not
399 * work if the file system has any hard links other than ".."
400 * that point backwards in the directory structure.
401 */
402 pdp = vdp;
403 /*
404 * If ino is different from dp->i_ino,
405 * it's a relocated directory.
406 */
407 if (flags & ISDOTDOT) {
408 error = cd9660_vget_internal(vnode_mount(vdp), dp->i_ino, &tdp, NULL, NULL,
409 dp->i_ino != ino, ep, p);
410 VTOI(tdp)->i_parent = VTOI(pdp)->i_number;
411 buf_brelse(bp);
412
413 *vpp = tdp;
414 } else if (dp->i_number == dp->i_ino) {
415 buf_brelse(bp);
416 vnode_get(vdp); /* we want ourself, ie "." */
417 *vpp = vdp;
418 } else {
419 error = cd9660_vget_internal(vnode_mount(vdp), dp->i_ino, &tdp, vdp, cnp,
420 dp->i_ino != ino, ep, p);
421 /* save parent inode number */
422 VTOI(tdp)->i_parent = VTOI(pdp)->i_number;
423 buf_brelse(bp);
424 if (error)
425 return (error);
426 *vpp = tdp;
427 }
428 return (0);
429 }
430
431
432 /*
433 * Return buffer with the contents of block "offset" from the beginning of
434 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
435 * remaining space in the directory.
436 */
437 int
438 cd9660_blkatoff(vnode_t vp, off_t offset, char **res, buf_t *bpp)
439 {
440 struct iso_node *ip;
441 register struct iso_mnt *imp;
442 buf_t bp;
443 daddr_t lbn;
444 int bsize, error;
445
446 ip = VTOI(vp);
447 imp = ip->i_mnt;
448 lbn = lblkno(imp, offset);
449 bsize = blksize(imp, ip, lbn);
450
451 if ((bsize != imp->im_sector_size) &&
452 (offset & (imp->im_sector_size - 1)) == 0) {
453 bsize = imp->im_sector_size;
454 }
455
456 if ( (error = (int)buf_bread(vp, (daddr64_t)((unsigned)lbn), bsize, NOCRED, &bp)) ) {
457 buf_brelse(bp);
458 *bpp = NULL;
459 return (error);
460 }
461 if (res)
462 *res = (char *)buf_dataptr(bp) + blkoff(imp, offset);
463 *bpp = bp;
464
465 return (0);
466 }