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