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 * 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.
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
38 * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91
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 $
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/namei.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
51 #include <isofs/cd9660/iso.h>
52 #include <isofs/cd9660/cd9660_node.h>
53 #include <isofs/cd9660/iso_rrip.h>
56 * Convert a component of a pathname into a pointer to a locked inode.
57 * This is a very central and rather complicated routine.
58 * If the file system is not maintained in a strict tree hierarchy,
59 * this can result in a deadlock situation (see comments in code below).
61 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
62 * whether the name is to be looked up, created, renamed, or deleted.
63 * When CREATE, RENAME, or DELETE is specified, information usable in
64 * creating, renaming, or deleting a directory entry may be calculated.
65 * If flag has LOCKPARENT or'ed into it and the target of the pathname
66 * exists, lookup returns both the target and its parent directory locked.
67 * When creating or renaming and LOCKPARENT is specified, the target may
68 * not be ".". When deleting and LOCKPARENT is specified, the target may
69 * be "."., but the caller must check to ensure it does an vrele and iput
70 * instead of two iputs.
72 * Overall outline of ufs_lookup:
74 * search for name in directory, to found or notfound
76 * if creating, return locked directory, leaving info on available slots
79 * if at end of path and deleting, return information to allow delete
80 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
81 * inode and return info to allow rewrite
82 * if not at end, add name to cache; if at end and neither creating
83 * nor deleting, add name to cache
85 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
89 struct vop_cachedlookup_args /* {
92 struct componentname *a_cnp;
95 register struct vnode *vdp; /* vnode for directory being searched */
96 register struct iso_node *dp; /* inode for directory being searched */
97 register struct iso_mnt *imp; /* file system that directory is in */
98 struct buf *bp; /* a buffer of directory entries */
99 struct iso_directory_record *ep = 0;/* the current directory entry */
100 int entryoffsetinblock; /* offset of ep in bp's buffer */
101 int saveoffset = 0; /* offset of last directory entry in dir */
102 int numdirpasses; /* strategy for directory search */
103 doff_t endsearch; /* offset to end directory search */
104 struct vnode *pdp; /* saved dp during symlink work */
105 struct vnode *tdp; /* returned by cd9660_vget_internal */
106 u_long bmask; /* block offset mask */
107 int lockparent; /* 1 => lockparent flag is set */
108 int wantparent; /* 1 => wantparent or lockparent flag */
114 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;
122 struct proc *p = cnp->cn_proc;
129 lockparent = flags & LOCKPARENT;
130 wantparent = flags & (LOCKPARENT|WANTPARENT);
131 cnp->cn_flags &= ~PDIRUNLOCK;
134 * We now have a segment name to search for, and a directory to search.
137 len = cnp->cn_namelen;
138 name = cnp->cn_nameptr;
140 * A leading `=' means, we are looking for an associated file
142 if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)))
149 * If there is cached information on a previous search of
150 * this directory, pick up where we last left off.
151 * We cache only lookups as these are the most common
152 * and have the greatest payoff. Caching CREATE has little
153 * benefit as it usually must search the entire directory
154 * to determine that the entry does not exist. Caching the
155 * location of the last DELETE or RENAME has not reduced
156 * profiling time and hence has been removed in the interest
159 bmask = imp->im_bmask;
160 if (nameiop != LOOKUP || dp->i_diroff == 0 ||
161 dp->i_diroff > dp->i_size) {
162 entryoffsetinblock = 0;
166 dp->i_offset = dp->i_diroff;
167 if ((entryoffsetinblock = dp->i_offset & bmask) &&
168 (error = cd9660_blkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)))
171 nchstats.ncs_2passes++;
173 endsearch = dp->i_size;
176 while (dp->i_offset < endsearch) {
178 * If offset is on a block boundary,
179 * read the next directory block.
180 * Release previous if it exists.
182 if ((dp->i_offset & bmask) == 0) {
186 cd9660_blkatoff(vdp, (off_t)dp->i_offset, NULL, &bp)) != 0)
188 entryoffsetinblock = 0;
191 * Get pointer to next entry.
193 ep = (struct iso_directory_record *)
194 ((char *)bp->b_data + entryoffsetinblock);
196 reclen = isonum_711(ep->length);
198 /* skip to next block, if any */
200 (dp->i_offset & ~bmask) + imp->logical_block_size;
204 if (reclen < ISO_DIRECTORY_RECORD_SIZE)
205 /* illegal entry, stop */
208 if (entryoffsetinblock + reclen > imp->logical_block_size)
209 /* entries are not allowed to cross boundaries */
212 namelen = isonum_711(ep->name_len);
213 isoflags = isonum_711(imp->iso_ftype == ISO_FTYPE_HIGH_SIERRA?
214 &ep->date[6]: ep->flags);
216 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
217 /* illegal entry, stop */
221 * Check for a name match.
223 switch (imp->iso_ftype) {
225 if (!(isoflags & 4) == !assoc) {
228 || (flags & ISDOTDOT)) {
230 && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
232 * Save directory entry's inode number and
233 * release directory buffer.
235 dp->i_ino = isodirino(ep, imp);
241 } else if (!(res = isofncmp(name, len, ep->name, namelen, imp->joliet_level))) {
243 ino = isodirino(ep, imp);
245 ino = dbtob(bp->b_blkno)
246 + entryoffsetinblock;
247 saveoffset = dp->i_offset;
250 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
253 else if (res > 0 && numdirpasses == 2)
259 if (isonum_711(ep->flags)&2)
260 ino = isodirino(ep, imp);
262 ino = dbtob(bp->b_blkno) + entryoffsetinblock;
264 cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
265 if (namelen == cnp->cn_namelen
266 && !bcmp(name,altname,namelen))
271 dp->i_offset += reclen;
272 entryoffsetinblock += reclen;
277 if (saveoffset != dp->i_offset) {
278 if (lblkno(imp, dp->i_offset) !=
279 lblkno(imp, saveoffset)) {
282 if ((error = cd9660_blkatoff(vdp,
283 (off_t)saveoffset, NULL, &bp)) != 0)
286 entryoffsetinblock = saveoffset & bmask;
287 ep = (struct iso_directory_record *)
288 ((char *)bp->b_data + entryoffsetinblock);
289 dp->i_offset = saveoffset;
295 * If we started in the middle of the directory and failed
296 * to find our target, we must check the beginning as well.
298 if (numdirpasses == 2) {
301 endsearch = dp->i_diroff;
308 * Insert name into cache (as non-existent) if appropriate.
310 if (cnp->cn_flags & MAKEENTRY)
311 cache_enter(vdp, *vpp, cnp);
312 if (nameiop == CREATE || nameiop == RENAME)
317 if (numdirpasses == 2)
318 nchstats.ncs_pass2++;
321 * Found component in pathname.
322 * If the final component of path name, save information
323 * in the cache as to where the entry was found.
325 if ((flags & ISLASTCN) && nameiop == LOOKUP)
326 dp->i_diroff = dp->i_offset;
329 * Step through the translation in the name. We do not `iput' the
330 * directory because we may need it again if a symbolic link
331 * is relative to the current directory. Instead we save it
332 * unlocked as "pdp". We must get the target inode before unlocking
333 * the directory to insure that the inode will not be removed
334 * before we get it. We prevent deadlock by always fetching
335 * inodes from the root, moving down the directory tree. Thus
336 * when following backward pointers ".." we must unlock the
337 * parent directory before getting the requested directory.
338 * There is a potential race condition here if both the current
339 * and parent directories are removed before the `iget' for the
340 * inode associated with ".." returns. We hope that this occurs
341 * infrequently since we cannot avoid this race condition without
342 * implementing a sophisticated deadlock detection algorithm.
343 * Note also that this simple deadlock detection scheme will not
344 * work if the file system has any hard links other than ".."
345 * that point backwards in the directory structure.
349 * If ino is different from dp->i_ino,
350 * it's a relocated directory.
352 if (flags & ISDOTDOT) {
353 VOP_UNLOCK(pdp, 0, p); /* race to get the inode */
354 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
355 dp->i_ino != ino, ep);
358 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, p);
361 if (lockparent && (flags & ISLASTCN)) {
362 if ((error = vn_lock(pdp, LK_EXCLUSIVE, p)) != 0) {
363 cnp->cn_flags |= PDIRUNLOCK;
368 cnp->cn_flags |= PDIRUNLOCK;
370 } else if (dp->i_number == dp->i_ino) {
372 VREF(vdp); /* we want ourself, ie "." */
375 error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
376 dp->i_ino != ino, ep);
380 if (!lockparent || !(flags & ISLASTCN)) {
381 cnp->cn_flags |= PDIRUNLOCK;
382 VOP_UNLOCK(pdp, 0, p);
388 * Insert name into cache if appropriate.
390 if (cnp->cn_flags & MAKEENTRY)
391 cache_enter(vdp, *vpp, cnp);
396 * Return buffer with the contents of block "offset" from the beginning of
397 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
398 * remaining space in the directory.
401 cd9660_blkatoff(vp, offset, res, bpp)
408 register struct iso_mnt *imp;
415 lbn = lblkno(imp, offset);
416 bsize = blksize(imp, ip, lbn);
418 if ((error = bread(vp, lbn, bsize, NOCRED, &bp)) != 0) {
425 * We must BMAP the buffer because the directory code may use b_blkno
426 * to calculate the inode for certain types of directory entries.
427 * We could get away with not doing it before we VMIO-backed the
428 * directories because the buffers would get freed atomically with
429 * the invalidation of their data. But with VMIO-backed buffers
430 * the buffers may be freed and then later reconstituted - and the
431 * reconstituted buffer will have no knowledge of b_blkno.
433 if (bp->b_blkno == bp->b_lblkno) {
434 error = VOP_BMAP(vp, bp->b_lblkno, NULL,
435 &bp->b_blkno, NULL, NULL);
438 bp->b_flags |= B_ERROR;
446 *res = (char *)bp->b_data + blkoff(imp, offset);