2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91
35 * @(#)cd9660_lookup.c 8.2 (Berkeley) 1/23/94
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
49 #include <fs/cd9660/iso.h>
50 #include <fs/cd9660/cd9660_node.h>
51 #include <fs/cd9660/iso_rrip.h>
54 * Convert a component of a pathname into a pointer to a locked inode.
55 * This is a very central and rather complicated routine.
56 * If the filesystem is not maintained in a strict tree hierarchy,
57 * this can result in a deadlock situation (see comments in code below).
59 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
60 * whether the name is to be looked up, created, renamed, or deleted.
61 * When CREATE, RENAME, or DELETE is specified, information usable in
62 * creating, renaming, or deleting a directory entry may be calculated.
63 * If flag has LOCKPARENT or'ed into it and the target of the pathname
64 * exists, lookup returns both the target and its parent directory locked.
65 * When creating or renaming and LOCKPARENT is specified, the target may
66 * not be ".". When deleting and LOCKPARENT is specified, the target may
67 * be "."., but the caller must check to ensure it does an vrele and iput
68 * instead of two iputs.
70 * Overall outline of ufs_lookup:
72 * search for name in directory, to found or notfound
74 * if creating, return locked directory, leaving info on available slots
77 * if at end of path and deleting, return information to allow delete
78 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
79 * inode and return info to allow rewrite
80 * if not at end, add name to cache; if at end and neither creating
81 * nor deleting, add name to cache
83 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
87 struct vop_cachedlookup_args /* {
90 struct componentname *a_cnp;
93 struct vnode *vdp; /* vnode for directory being searched */
94 struct iso_node *dp; /* inode for directory being searched */
95 struct iso_mnt *imp; /* filesystem that directory is in */
96 struct buf *bp; /* a buffer of directory entries */
97 struct iso_directory_record *ep;/* the current directory entry */
98 struct iso_directory_record *ep2;/* copy of current directory entry */
99 int entryoffsetinblock; /* offset of ep in bp's buffer */
100 int saveoffset = 0; /* offset of last directory entry in dir */
101 doff_t i_diroff; /* cached i_diroff value. */
102 doff_t i_offset; /* cached i_offset value. */
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 */
113 char altname[NAME_MAX];
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;
131 * We now have a segment name to search for, and a directory to search.
134 i_diroff = dp->i_diroff;
135 len = cnp->cn_namelen;
136 name = cnp->cn_nameptr;
139 * A leading `=' means, we are looking for an associated file
141 if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)))
148 * If there is cached information on a previous search of
149 * this directory, pick up where we last left off.
150 * We cache only lookups as these are the most common
151 * and have the greatest payoff. Caching CREATE has little
152 * benefit as it usually must search the entire directory
153 * to determine that the entry does not exist. Caching the
154 * location of the last DELETE or RENAME has not reduced
155 * profiling time and hence has been removed in the interest
158 bmask = imp->im_bmask;
159 if (nameiop != LOOKUP || i_diroff == 0 || i_diroff > dp->i_size) {
160 entryoffsetinblock = 0;
165 if ((entryoffsetinblock = i_offset & bmask) &&
166 (error = cd9660_blkatoff(vdp, (off_t)i_offset, NULL, &bp)))
169 nchstats.ncs_2passes++;
171 endsearch = dp->i_size;
174 while (i_offset < endsearch) {
176 * If offset is on a block boundary,
177 * read the next directory block.
178 * Release previous if it exists.
180 if ((i_offset & bmask) == 0) {
184 cd9660_blkatoff(vdp, (off_t)i_offset, NULL, &bp)) != 0)
186 entryoffsetinblock = 0;
189 * Get pointer to next entry.
191 ep = (struct iso_directory_record *)
192 ((char *)bp->b_data + entryoffsetinblock);
194 reclen = isonum_711(ep->length);
196 /* skip to next block, if any */
198 (i_offset & ~bmask) + imp->logical_block_size;
202 if (reclen < ISO_DIRECTORY_RECORD_SIZE)
203 /* illegal entry, stop */
206 if (entryoffsetinblock + reclen > imp->logical_block_size)
207 /* entries are not allowed to cross boundaries */
210 namelen = isonum_711(ep->name_len);
211 isoflags = isonum_711(imp->iso_ftype == ISO_FTYPE_HIGH_SIERRA?
212 &ep->date[6]: ep->flags);
214 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
215 /* illegal entry, stop */
219 * Check for a name match.
221 switch (imp->iso_ftype) {
223 if (!(isoflags & 4) == !assoc) {
226 || (flags & ISDOTDOT)) {
228 && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
230 * Save directory entry's inode number and
231 * release directory buffer.
233 i_ino = isodirino(ep, imp);
239 } else if (!(res = isofncmp(name, len,
246 ino = isodirino(ep, imp);
248 ino = dbtob(bp->b_blkno)
249 + entryoffsetinblock;
250 saveoffset = i_offset;
253 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
256 else if (res > 0 && numdirpasses == 2)
262 if (isonum_711(ep->flags)&2)
263 ino = isodirino(ep, imp);
265 ino = dbtob(bp->b_blkno) + entryoffsetinblock;
267 cd9660_rrip_getname(ep, altname, &namelen, &i_ino, imp);
268 if (namelen == cnp->cn_namelen
269 && !bcmp(name,altname,namelen))
275 entryoffsetinblock += reclen;
280 if (saveoffset != i_offset) {
281 if (lblkno(imp, i_offset) !=
282 lblkno(imp, saveoffset)) {
285 if ((error = cd9660_blkatoff(vdp,
286 (off_t)saveoffset, NULL, &bp)) != 0)
289 entryoffsetinblock = saveoffset & bmask;
290 ep = (struct iso_directory_record *)
291 ((char *)bp->b_data + entryoffsetinblock);
292 reclen = isonum_711(ep->length);
293 i_offset = saveoffset;
299 * If we started in the middle of the directory and failed
300 * to find our target, we must check the beginning as well.
302 if (numdirpasses == 2) {
305 endsearch = i_diroff;
312 * Insert name into cache (as non-existent) if appropriate.
314 if (cnp->cn_flags & MAKEENTRY)
315 cache_enter(vdp, *vpp, cnp);
316 if (nameiop == CREATE || nameiop == RENAME)
321 if (numdirpasses == 2)
322 nchstats.ncs_pass2++;
325 * Found component in pathname.
326 * If the final component of path name, save information
327 * in the cache as to where the entry was found.
329 if ((flags & ISLASTCN) && nameiop == LOOKUP)
330 dp->i_diroff = i_offset;
333 * Step through the translation in the name. We do not `vput' the
334 * directory because we may need it again if a symbolic link
335 * is relative to the current directory. Instead we save it
336 * unlocked as "pdp". We must get the target inode before unlocking
337 * the directory to insure that the inode will not be removed
338 * before we get it. We prevent deadlock by always fetching
339 * inodes from the root, moving down the directory tree. Thus
340 * when following backward pointers ".." we must unlock the
341 * parent directory before getting the requested directory.
342 * There is a potential race condition here if both the current
343 * and parent directories are removed before the `vget' for the
344 * inode associated with ".." returns. We hope that this occurs
345 * infrequently since we cannot avoid this race condition without
346 * implementing a sophisticated deadlock detection algorithm.
347 * Note also that this simple deadlock detection scheme will not
348 * work if the filesystem has any hard links other than ".."
349 * that point backwards in the directory structure.
354 * Make a copy of the directory entry for non "." lookups so
355 * we can drop the buffer before calling vget() to avoid a
356 * lock order reversal between the vnode lock and the buffer
359 if (dp->i_number != i_ino) {
360 ep2 = malloc(reclen, M_TEMP, M_WAITOK);
361 bcopy(ep, ep2, reclen);
367 * If ino is different from i_ino,
368 * it's a relocated directory.
370 if (flags & ISDOTDOT) {
372 * Expanded copy of vn_vget_ino() so that we can use
373 * cd9660_vget_internal().
376 ltype = VOP_ISLOCKED(pdp);
377 error = vfs_busy(mp, MBF_NOWAIT);
381 error = vfs_busy(mp, 0);
382 vn_lock(pdp, ltype | LK_RETRY);
386 if (pdp->v_iflag & VI_DOOMED) {
392 error = cd9660_vget_internal(vdp->v_mount, i_ino,
393 cnp->cn_lkflags, &tdp,
397 vn_lock(pdp, ltype | LK_RETRY);
398 if (pdp->v_iflag & VI_DOOMED) {
406 } else if (dp->i_number == i_ino) {
407 VREF(vdp); /* we want ourself, ie "." */
409 * When we lookup "." we still can be asked to lock it
412 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
413 if (ltype != VOP_ISLOCKED(vdp)) {
414 if (ltype == LK_EXCLUSIVE)
415 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
416 else /* if (ltype == LK_SHARED) */
417 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
421 error = cd9660_vget_internal(vdp->v_mount, i_ino,
422 cnp->cn_lkflags, &tdp,
431 * Insert name into cache if appropriate.
433 if (cnp->cn_flags & MAKEENTRY)
434 cache_enter(vdp, *vpp, cnp);
439 * Return buffer with the contents of block "offset" from the beginning of
440 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
441 * remaining space in the directory.
444 cd9660_blkatoff(vp, offset, res, bpp)
454 int bsize, bshift, error;
458 lbn = lblkno(imp, offset);
459 bsize = blksize(imp, ip, lbn);
460 bshift = imp->im_bshift;
462 if ((error = bread(vp, lbn, bsize, NOCRED, &bp)) != 0) {
469 * We must BMAP the buffer because the directory code may use b_blkno
470 * to calculate the inode for certain types of directory entries.
471 * We could get away with not doing it before we VMIO-backed the
472 * directories because the buffers would get freed atomically with
473 * the invalidation of their data. But with VMIO-backed buffers
474 * the buffers may be freed and then later reconstituted - and the
475 * reconstituted buffer will have no knowledge of b_blkno.
477 if (bp->b_blkno == bp->b_lblkno) {
478 bp->b_blkno = (ip->iso_start + bp->b_lblkno) << (bshift - DEV_BSHIFT);
482 *res = (char *)bp->b_data + blkoff(imp, offset);