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