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