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.
105 cd9660_lookup(struct vop_cachedlookup_args *ap)
107 struct vnode *vdp; /* vnode for directory being searched */
108 struct iso_node *dp; /* inode for directory being searched */
109 struct iso_mnt *imp; /* filesystem that directory is in */
110 struct buf *bp; /* a buffer of directory entries */
111 struct iso_directory_record *ep;/* the current directory entry */
112 struct iso_directory_record *ep2;/* copy of current directory entry */
113 int entryoffsetinblock; /* offset of ep in bp's buffer */
114 int saveoffset = 0; /* offset of last directory entry in dir */
115 doff_t i_diroff; /* cached i_diroff value. */
116 doff_t i_offset; /* cached i_offset value. */
117 int numdirpasses; /* strategy for directory search */
118 doff_t endsearch; /* offset to end directory search */
119 struct vnode *pdp; /* saved dp during symlink work */
120 struct vnode *tdp; /* returned by cd9660_vget_internal */
121 struct cd9660_ino_alloc_arg dd_arg;
122 u_long bmask; /* block offset mask */
128 char altname[NAME_MAX];
132 struct vnode **vpp = ap->a_vpp;
133 struct componentname *cnp = ap->a_cnp;
134 int flags = cnp->cn_flags;
135 int nameiop = cnp->cn_nameiop;
145 * We now have a segment name to search for, and a directory to search.
148 i_diroff = dp->i_diroff;
149 len = cnp->cn_namelen;
150 name = cnp->cn_nameptr;
153 * A leading `=' means, we are looking for an associated file
155 if ((assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)))
162 * If there is cached information on a previous search of
163 * this directory, pick up where we last left off.
164 * We cache only lookups as these are the most common
165 * and have the greatest payoff. Caching CREATE has little
166 * benefit as it usually must search the entire directory
167 * to determine that the entry does not exist. Caching the
168 * location of the last DELETE or RENAME has not reduced
169 * profiling time and hence has been removed in the interest
172 bmask = imp->im_bmask;
173 if (nameiop != LOOKUP || i_diroff == 0 || i_diroff > dp->i_size) {
174 entryoffsetinblock = 0;
179 if ((entryoffsetinblock = i_offset & bmask) &&
180 (error = cd9660_blkatoff(vdp, (off_t)i_offset, NULL, &bp)))
183 nchstats.ncs_2passes++;
185 endsearch = dp->i_size;
188 while (i_offset < endsearch) {
190 * If offset is on a block boundary,
191 * read the next directory block.
192 * Release previous if it exists.
194 if ((i_offset & bmask) == 0) {
198 cd9660_blkatoff(vdp, (off_t)i_offset, NULL, &bp)) != 0)
200 entryoffsetinblock = 0;
203 * Get pointer to next entry.
205 ep = (struct iso_directory_record *)
206 ((char *)bp->b_data + entryoffsetinblock);
208 reclen = isonum_711(ep->length);
210 /* skip to next block, if any */
212 (i_offset & ~bmask) + imp->logical_block_size;
216 if (reclen < ISO_DIRECTORY_RECORD_SIZE)
217 /* illegal entry, stop */
220 if (entryoffsetinblock + reclen > imp->logical_block_size)
221 /* entries are not allowed to cross boundaries */
224 namelen = isonum_711(ep->name_len);
225 isoflags = isonum_711(imp->iso_ftype == ISO_FTYPE_HIGH_SIERRA?
226 &ep->date[6]: ep->flags);
228 if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
229 /* illegal entry, stop */
233 * Check for a name match.
235 switch (imp->iso_ftype) {
237 if (!(isoflags & 4) == !assoc) {
240 || (flags & ISDOTDOT)) {
242 && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
244 * Save directory entry's inode number and
245 * release directory buffer.
247 i_ino = isodirino(ep, imp);
253 } else if (!(res = isofncmp(name, len,
260 ino = isodirino(ep, imp);
262 ino = dbtob(bp->b_blkno)
263 + entryoffsetinblock;
264 saveoffset = i_offset;
267 #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
270 else if (res > 0 && numdirpasses == 2)
276 if (isonum_711(ep->flags)&2)
277 ino = isodirino(ep, imp);
279 ino = dbtob(bp->b_blkno) + entryoffsetinblock;
281 cd9660_rrip_getname(ep, altname, &namelen, &i_ino, imp);
282 if (namelen == cnp->cn_namelen
283 && !bcmp(name,altname,namelen))
289 entryoffsetinblock += reclen;
294 if (saveoffset != i_offset) {
295 if (lblkno(imp, i_offset) !=
296 lblkno(imp, saveoffset)) {
299 if ((error = cd9660_blkatoff(vdp,
300 (off_t)saveoffset, NULL, &bp)) != 0)
303 entryoffsetinblock = saveoffset & bmask;
304 ep = (struct iso_directory_record *)
305 ((char *)bp->b_data + entryoffsetinblock);
306 reclen = isonum_711(ep->length);
307 i_offset = saveoffset;
313 * If we started in the middle of the directory and failed
314 * to find our target, we must check the beginning as well.
316 if (numdirpasses == 2) {
319 endsearch = i_diroff;
326 * Insert name into cache (as non-existent) if appropriate.
328 if (cnp->cn_flags & MAKEENTRY)
329 cache_enter(vdp, *vpp, cnp);
330 if (nameiop == CREATE || nameiop == RENAME)
335 if (numdirpasses == 2)
336 nchstats.ncs_pass2++;
339 * Found component in pathname.
340 * If the final component of path name, save information
341 * in the cache as to where the entry was found.
343 if ((flags & ISLASTCN) && nameiop == LOOKUP)
344 dp->i_diroff = i_offset;
347 * Step through the translation in the name. We do not `vput' the
348 * directory because we may need it again if a symbolic link
349 * is relative to the current directory. Instead we save it
350 * unlocked as "pdp". We must get the target inode before unlocking
351 * the directory to insure that the inode will not be removed
352 * before we get it. We prevent deadlock by always fetching
353 * inodes from the root, moving down the directory tree. Thus
354 * when following backward pointers ".." we must unlock the
355 * parent directory before getting the requested directory.
356 * There is a potential race condition here if both the current
357 * and parent directories are removed before the `vget' for the
358 * inode associated with ".." returns. We hope that this occurs
359 * infrequently since we cannot avoid this race condition without
360 * implementing a sophisticated deadlock detection algorithm.
361 * Note also that this simple deadlock detection scheme will not
362 * work if the filesystem has any hard links other than ".."
363 * that point backwards in the directory structure.
368 * Make a copy of the directory entry for non "." lookups so
369 * we can drop the buffer before calling vget() to avoid a
370 * lock order reversal between the vnode lock and the buffer
373 if (dp->i_number != i_ino) {
374 ep2 = malloc(reclen, M_TEMP, M_WAITOK);
375 memcpy(ep2, ep, reclen);
381 * If ino is different from i_ino,
382 * it's a relocated directory.
384 if (flags & ISDOTDOT) {
386 dd_arg.i_ino = i_ino;
388 error = vn_vget_ino_gen(pdp, cd9660_ino_alloc, &dd_arg,
389 cnp->cn_lkflags, &tdp);
394 } else if (dp->i_number == i_ino) {
395 VREF(vdp); /* we want ourself, ie "." */
397 * When we lookup "." we still can be asked to lock it
400 ltype = cnp->cn_lkflags & LK_TYPE_MASK;
401 if (ltype != VOP_ISLOCKED(vdp)) {
402 if (ltype == LK_EXCLUSIVE)
403 vn_lock(vdp, LK_UPGRADE | LK_RETRY);
404 else /* if (ltype == LK_SHARED) */
405 vn_lock(vdp, LK_DOWNGRADE | LK_RETRY);
409 error = cd9660_vget_internal(vdp->v_mount, i_ino,
410 cnp->cn_lkflags, &tdp,
419 * Insert name into cache if appropriate.
421 if (cnp->cn_flags & MAKEENTRY)
422 cache_enter(vdp, *vpp, cnp);
427 * Return buffer with the contents of block "offset" from the beginning of
428 * directory "ip". If "res" is non-zero, fill it in with a pointer to the
429 * remaining space in the directory.
432 cd9660_blkatoff(struct vnode *vp, off_t offset, char **res, struct buf **bpp)
438 int bsize, bshift, error;
442 lbn = lblkno(imp, offset);
443 bsize = blksize(imp, ip, lbn);
444 bshift = imp->im_bshift;
446 if ((error = bread(vp, lbn, bsize, NOCRED, &bp)) != 0) {
453 * We must BMAP the buffer because the directory code may use b_blkno
454 * to calculate the inode for certain types of directory entries.
455 * We could get away with not doing it before we VMIO-backed the
456 * directories because the buffers would get freed atomically with
457 * the invalidation of their data. But with VMIO-backed buffers
458 * the buffers may be freed and then later reconstituted - and the
459 * reconstituted buffer will have no knowledge of b_blkno.
461 if (bp->b_blkno == bp->b_lblkno) {
462 bp->b_blkno = (ip->iso_start + bp->b_lblkno) << (bshift - DEV_BSHIFT);
466 *res = (char *)bp->b_data + blkoff(imp, offset);