2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley
8 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
9 * Support code is derived from software contributed to Berkeley
10 * by Atsushi Murai (amurai@spec.co.jp).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
36 * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91
37 * @(#)cd9660_lookup.c 8.2 (Berkeley) 1/23/94
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
51 #include <fs/cd9660/iso.h>
52 #include <fs/cd9660/cd9660_node.h>
53 #include <fs/cd9660/iso_rrip.h>
55 struct cd9660_ino_alloc_arg {
58 struct iso_directory_record *ep;
62 cd9660_ino_alloc(struct mount *mp, void *arg, int lkflags,
65 struct cd9660_ino_alloc_arg *dd_arg;
68 return (cd9660_vget_internal(mp, dd_arg->i_ino, lkflags, vpp,
69 dd_arg->i_ino != dd_arg->ino, dd_arg->ep));
73 * Convert a component of a pathname into a pointer to a locked inode.
74 * This is a very central and rather complicated routine.
75 * If the filesystem is not maintained in a strict tree hierarchy,
76 * this can result in a deadlock situation (see comments in code below).
78 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
79 * whether the name is to be looked up, created, renamed, or deleted.
80 * When CREATE, RENAME, or DELETE is specified, information usable in
81 * creating, renaming, or deleting a directory entry may be calculated.
82 * If flag has LOCKPARENT or'ed into it and the target of the pathname
83 * exists, lookup returns both the target and its parent directory locked.
84 * When creating or renaming and LOCKPARENT is specified, the target may
85 * not be ".". When deleting and LOCKPARENT is specified, the target may
86 * be "."., but the caller must check to ensure it does an vrele and iput
87 * instead of two iputs.
89 * Overall outline of ufs_lookup:
91 * search for name in directory, to found or notfound
93 * if creating, return locked directory, leaving info on available slots
96 * if at end of path and deleting, return information to allow delete
97 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
98 * inode and return info to allow rewrite
99 * if not at end, add name to cache; if at end and neither creating
100 * nor deleting, add name to cache
102 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
106 struct vop_cachedlookup_args /* {
108 struct vnode **a_vpp;
109 struct componentname *a_cnp;
112 struct vnode *vdp; /* vnode for directory being searched */
113 struct iso_node *dp; /* inode for directory being searched */
114 struct iso_mnt *imp; /* filesystem that directory is in */
115 struct buf *bp; /* a buffer of directory entries */
116 struct iso_directory_record *ep;/* the current directory entry */
117 struct iso_directory_record *ep2;/* copy of current directory entry */
118 int entryoffsetinblock; /* offset of ep in bp's buffer */
119 int saveoffset = 0; /* offset of last directory entry in dir */
120 doff_t i_diroff; /* cached i_diroff value. */
121 doff_t i_offset; /* cached i_offset value. */
122 int numdirpasses; /* strategy for directory search */
123 doff_t endsearch; /* offset to end directory search */
124 struct vnode *pdp; /* saved dp during symlink work */
125 struct vnode *tdp; /* returned by cd9660_vget_internal */
126 struct cd9660_ino_alloc_arg dd_arg;
127 u_long bmask; /* block offset mask */
133 char altname[NAME_MAX];
137 struct vnode **vpp = ap->a_vpp;
138 struct componentname *cnp = ap->a_cnp;
139 int flags = cnp->cn_flags;
140 int nameiop = cnp->cn_nameiop;
150 * We now have a segment name to search for, and a directory to search.
153 i_diroff = dp->i_diroff;
154 len = cnp->cn_namelen;
155 name = cnp->cn_nameptr;
158 * A leading `=' means, we are looking for an associated file
160 if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)))
167 * If there is cached information on a previous search of
168 * this directory, pick up where we last left off.
169 * We cache only lookups as these are the most common
170 * and have the greatest payoff. Caching CREATE has little
171 * benefit as it usually must search the entire directory
172 * to determine that the entry does not exist. Caching the
173 * location of the last DELETE or RENAME has not reduced
174 * profiling time and hence has been removed in the interest
177 bmask = imp->im_bmask;
178 if (nameiop != LOOKUP || i_diroff == 0 || i_diroff > dp->i_size) {
179 entryoffsetinblock = 0;
184 if ((entryoffsetinblock = i_offset & bmask) &&
185 (error = cd9660_blkatoff(vdp, (off_t)i_offset, NULL, &bp)))
188 nchstats.ncs_2passes++;
190 endsearch = dp->i_size;
193 while (i_offset < endsearch) {
195 * If offset is on a block boundary,
196 * read the next directory block.
197 * Release previous if it exists.
199 if ((i_offset & bmask) == 0) {
203 cd9660_blkatoff(vdp, (off_t)i_offset, NULL, &bp)) != 0)
205 entryoffsetinblock = 0;
208 * Get pointer to next entry.
210 ep = (struct iso_directory_record *)
211 ((char *)bp->b_data + entryoffsetinblock);
213 reclen = isonum_711(ep->length);
215 /* skip to next block, if any */
217 (i_offset & ~bmask) + imp->logical_block_size;
221 if (reclen < ISO_DIRECTORY_RECORD_SIZE)
222 /* illegal entry, stop */
225 if (entryoffsetinblock + reclen > imp->logical_block_size)
226 /* entries are not allowed to cross boundaries */
229 namelen = isonum_711(ep->name_len);
230 isoflags = isonum_711(imp->iso_ftype == ISO_FTYPE_HIGH_SIERRA?
231 &ep->date[6]: ep->flags);
233 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
234 /* illegal entry, stop */
238 * Check for a name match.
240 switch (imp->iso_ftype) {
242 if (!(isoflags & 4) == !assoc) {
245 || (flags & ISDOTDOT)) {
247 && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
249 * Save directory entry's inode number and
250 * release directory buffer.
252 i_ino = isodirino(ep, imp);
258 } else if (!(res = isofncmp(name, len,
265 ino = isodirino(ep, imp);
267 ino = dbtob(bp->b_blkno)
268 + entryoffsetinblock;
269 saveoffset = i_offset;
272 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
275 else if (res > 0 && numdirpasses == 2)
281 if (isonum_711(ep->flags)&2)
282 ino = isodirino(ep, imp);
284 ino = dbtob(bp->b_blkno) + entryoffsetinblock;
286 cd9660_rrip_getname(ep, altname, &namelen, &i_ino, imp);
287 if (namelen == cnp->cn_namelen
288 && !bcmp(name,altname,namelen))
294 entryoffsetinblock += reclen;
299 if (saveoffset != i_offset) {
300 if (lblkno(imp, i_offset) !=
301 lblkno(imp, saveoffset)) {
304 if ((error = cd9660_blkatoff(vdp,
305 (off_t)saveoffset, NULL, &bp)) != 0)
308 entryoffsetinblock = saveoffset & bmask;
309 ep = (struct iso_directory_record *)
310 ((char *)bp->b_data + entryoffsetinblock);
311 reclen = isonum_711(ep->length);
312 i_offset = saveoffset;
318 * If we started in the middle of the directory and failed
319 * to find our target, we must check the beginning as well.
321 if (numdirpasses == 2) {
324 endsearch = i_diroff;
331 * Insert name into cache (as non-existent) if appropriate.
333 if (cnp->cn_flags & MAKEENTRY)
334 cache_enter(vdp, *vpp, cnp);
335 if (nameiop == CREATE || nameiop == RENAME)
340 if (numdirpasses == 2)
341 nchstats.ncs_pass2++;
344 * Found component in pathname.
345 * If the final component of path name, save information
346 * in the cache as to where the entry was found.
348 if ((flags & ISLASTCN) && nameiop == LOOKUP)
349 dp->i_diroff = i_offset;
352 * Step through the translation in the name. We do not `vput' the
353 * directory because we may need it again if a symbolic link
354 * is relative to the current directory. Instead we save it
355 * unlocked as "pdp". We must get the target inode before unlocking
356 * the directory to insure that the inode will not be removed
357 * before we get it. We prevent deadlock by always fetching
358 * inodes from the root, moving down the directory tree. Thus
359 * when following backward pointers ".." we must unlock the
360 * parent directory before getting the requested directory.
361 * There is a potential race condition here if both the current
362 * and parent directories are removed before the `vget' for the
363 * inode associated with ".." returns. We hope that this occurs
364 * infrequently since we cannot avoid this race condition without
365 * implementing a sophisticated deadlock detection algorithm.
366 * Note also that this simple deadlock detection scheme will not
367 * work if the filesystem has any hard links other than ".."
368 * that point backwards in the directory structure.
373 * Make a copy of the directory entry for non "." lookups so
374 * we can drop the buffer before calling vget() to avoid a
375 * lock order reversal between the vnode lock and the buffer
378 if (dp->i_number != i_ino) {
379 ep2 = malloc(reclen, M_TEMP, M_WAITOK);
380 memcpy(ep2, ep, reclen);
386 * If ino is different from i_ino,
387 * it's a relocated directory.
389 if (flags & ISDOTDOT) {
391 dd_arg.i_ino = i_ino;
393 error = vn_vget_ino_gen(pdp, cd9660_ino_alloc, &dd_arg,
394 cnp->cn_lkflags, &tdp);
399 } else if (dp->i_number == i_ino) {
400 VREF(vdp); /* we want ourself, ie "." */
402 * When we lookup "." we still can be asked to lock it
405 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
406 if (ltype != VOP_ISLOCKED(vdp)) {
407 if (ltype == LK_EXCLUSIVE)
408 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
409 else /* if (ltype == LK_SHARED) */
410 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
414 error = cd9660_vget_internal(vdp->v_mount, i_ino,
415 cnp->cn_lkflags, &tdp,
424 * Insert name into cache if appropriate.
426 if (cnp->cn_flags & MAKEENTRY)
427 cache_enter(vdp, *vpp, cnp);
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.
437 cd9660_blkatoff(vp, offset, res, bpp)
447 int bsize, bshift, error;
451 lbn = lblkno(imp, offset);
452 bsize = blksize(imp, ip, lbn);
453 bshift = imp->im_bshift;
455 if ((error = bread(vp, lbn, bsize, NOCRED, &bp)) != 0) {
462 * We must BMAP the buffer because the directory code may use b_blkno
463 * to calculate the inode for certain types of directory entries.
464 * We could get away with not doing it before we VMIO-backed the
465 * directories because the buffers would get freed atomically with
466 * the invalidation of their data. But with VMIO-backed buffers
467 * the buffers may be freed and then later reconstituted - and the
468 * reconstituted buffer will have no knowledge of b_blkno.
470 if (bp->b_blkno == bp->b_lblkno) {
471 bp->b_blkno = (ip->iso_start + bp->b_lblkno) << (bshift - DEV_BSHIFT);
475 *res = (char *)bp->b_data + blkoff(imp, offset);