165789Smckusick /*-
265789Smckusick * Copyright (c) 1989, 1993, 1994
365789Smckusick * The Regents of the University of California. All rights reserved.
465789Smckusick *
565789Smckusick * This code is derived from software contributed to Berkeley
665789Smckusick * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
765789Smckusick * Support code is derived from software contributed to Berkeley
865789Smckusick * by Atsushi Murai (amurai@spec.co.jp).
965789Smckusick *
1065789Smckusick * %sccs.include.redist.c%
1165789Smckusick *
1265789Smckusick * from: @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91
1365789Smckusick *
14*69741Smckusick * @(#)cd9660_lookup.c 8.7 (Berkeley) 05/27/95
1565789Smckusick */
1665789Smckusick
1765789Smckusick #include <sys/param.h>
1865789Smckusick #include <sys/namei.h>
1965789Smckusick #include <sys/buf.h>
2065789Smckusick #include <sys/file.h>
2165789Smckusick #include <sys/vnode.h>
2265789Smckusick #include <sys/mount.h>
2365789Smckusick
2465789Smckusick #include <isofs/cd9660/iso.h>
2565855Smckusick #include <isofs/cd9660/cd9660_node.h>
2665789Smckusick #include <isofs/cd9660/iso_rrip.h>
2765855Smckusick #include <isofs/cd9660/cd9660_rrip.h>
2865789Smckusick
2965789Smckusick struct nchstats iso_nchstats;
3065789Smckusick
3165789Smckusick /*
3265789Smckusick * Convert a component of a pathname into a pointer to a locked inode.
3365789Smckusick * This is a very central and rather complicated routine.
3465789Smckusick * If the file system is not maintained in a strict tree hierarchy,
3565789Smckusick * this can result in a deadlock situation (see comments in code below).
3665789Smckusick *
3765789Smckusick * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
3865789Smckusick * whether the name is to be looked up, created, renamed, or deleted.
3965789Smckusick * When CREATE, RENAME, or DELETE is specified, information usable in
4065789Smckusick * creating, renaming, or deleting a directory entry may be calculated.
4165789Smckusick * If flag has LOCKPARENT or'ed into it and the target of the pathname
4265789Smckusick * exists, lookup returns both the target and its parent directory locked.
4365789Smckusick * When creating or renaming and LOCKPARENT is specified, the target may
4465789Smckusick * not be ".". When deleting and LOCKPARENT is specified, the target may
4565789Smckusick * be "."., but the caller must check to ensure it does an vrele and iput
4665789Smckusick * instead of two iputs.
4765789Smckusick *
4865789Smckusick * Overall outline of ufs_lookup:
4965789Smckusick *
5065789Smckusick * check accessibility of directory
5165789Smckusick * look for name in cache, if found, then if at end of path
5265789Smckusick * and deleting or creating, drop it, else return name
5365789Smckusick * search for name in directory, to found or notfound
5465789Smckusick * notfound:
5565789Smckusick * if creating, return locked directory, leaving info on available slots
5665789Smckusick * else return error
5765789Smckusick * found:
5865789Smckusick * if at end of path and deleting, return information to allow delete
5965789Smckusick * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
6065789Smckusick * inode and return info to allow rewrite
6165789Smckusick * if not at end, add name to cache; if at end and neither creating
6265789Smckusick * nor deleting, add name to cache
6365789Smckusick *
6465789Smckusick * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
6565789Smckusick */
6665855Smckusick cd9660_lookup(ap)
6765789Smckusick struct vop_lookup_args /* {
6865789Smckusick struct vnode *a_dvp;
6965789Smckusick struct vnode **a_vpp;
7065789Smckusick struct componentname *a_cnp;
7165789Smckusick } */ *ap;
7265789Smckusick {
7365789Smckusick register struct vnode *vdp; /* vnode for directory being searched */
7465789Smckusick register struct iso_node *dp; /* inode for directory being searched */
7565789Smckusick register struct iso_mnt *imp; /* file system that directory is in */
7665789Smckusick struct buf *bp; /* a buffer of directory entries */
7765789Smckusick struct iso_directory_record *ep;/* the current directory entry */
7865789Smckusick int entryoffsetinblock; /* offset of ep in bp's buffer */
7965789Smckusick int saveoffset; /* offset of last directory entry in dir */
8065789Smckusick int numdirpasses; /* strategy for directory search */
8165789Smckusick doff_t endsearch; /* offset to end directory search */
8267522Smckusick struct vnode *pdp; /* saved dp during symlink work */
8367522Smckusick struct vnode *tdp; /* returned by cd9660_vget_internal */
8468047Smckusick u_long bmask; /* block offset mask */
8565789Smckusick int lockparent; /* 1 => lockparent flag is set */
8665789Smckusick int wantparent; /* 1 => wantparent or lockparent flag */
8765789Smckusick int error;
8865789Smckusick ino_t ino = 0;
8965789Smckusick int reclen;
9065789Smckusick u_short namelen;
9165789Smckusick char altname[NAME_MAX];
9265789Smckusick int res;
9365789Smckusick int assoc, len;
9465789Smckusick char *name;
9565789Smckusick struct vnode **vpp = ap->a_vpp;
9665789Smckusick struct componentname *cnp = ap->a_cnp;
9765789Smckusick struct ucred *cred = cnp->cn_cred;
9865789Smckusick int flags = cnp->cn_flags;
9965789Smckusick int nameiop = cnp->cn_nameiop;
10069428Smckusick struct proc *p = cnp->cn_proc;
10165789Smckusick
10265789Smckusick bp = NULL;
10365789Smckusick *vpp = NULL;
10465789Smckusick vdp = ap->a_dvp;
10565789Smckusick dp = VTOI(vdp);
10665789Smckusick imp = dp->i_mnt;
10765789Smckusick lockparent = flags & LOCKPARENT;
10865789Smckusick wantparent = flags & (LOCKPARENT|WANTPARENT);
10965789Smckusick
11065789Smckusick /*
11165789Smckusick * Check accessiblity of directory.
11265789Smckusick */
11365789Smckusick if (vdp->v_type != VDIR)
11467375Smkm return (ENOTDIR);
11565789Smckusick if (error = VOP_ACCESS(vdp, VEXEC, cred, cnp->cn_proc))
11665789Smckusick return (error);
117*69741Smckusick if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) &&
118*69741Smckusick (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
119*69741Smckusick return (EROFS);
12065789Smckusick
12165789Smckusick /*
12265789Smckusick * We now have a segment name to search for, and a directory to search.
12365789Smckusick *
12465789Smckusick * Before tediously performing a linear scan of the directory,
12565789Smckusick * check the name cache to see if the directory/name pair
12665789Smckusick * we are looking for is known already.
12765789Smckusick */
12865789Smckusick if (error = cache_lookup(vdp, vpp, cnp)) {
12965789Smckusick int vpid; /* capability number of vnode */
13065789Smckusick
13165789Smckusick if (error == ENOENT)
13265789Smckusick return (error);
13365789Smckusick #ifdef PARANOID
13465789Smckusick if ((vdp->v_flag & VROOT) && (flags & ISDOTDOT))
13568047Smckusick panic("cd9660_lookup: .. through root");
13665789Smckusick #endif
13765789Smckusick /*
13865789Smckusick * Get the next vnode in the path.
13965789Smckusick * See comment below starting `Step through' for
14065789Smckusick * an explaination of the locking protocol.
14165789Smckusick */
14267522Smckusick pdp = vdp;
14365789Smckusick dp = VTOI(*vpp);
14465789Smckusick vdp = *vpp;
14565789Smckusick vpid = vdp->v_id;
14667522Smckusick if (pdp == vdp) {
14765789Smckusick VREF(vdp);
14865789Smckusick error = 0;
14965789Smckusick } else if (flags & ISDOTDOT) {
15069428Smckusick VOP_UNLOCK(pdp, 0, p);
15169428Smckusick error = vget(vdp, LK_EXCLUSIVE, p);
15265789Smckusick if (!error && lockparent && (flags & ISLASTCN))
15369428Smckusick error = vn_lock(pdp, LK_EXCLUSIVE, p);
15465789Smckusick } else {
15569428Smckusick error = vget(vdp, LK_EXCLUSIVE, p);
15665789Smckusick if (!lockparent || error || !(flags & ISLASTCN))
15769428Smckusick VOP_UNLOCK(pdp, 0, p);
15865789Smckusick }
15965789Smckusick /*
16065789Smckusick * Check that the capability number did not change
16165789Smckusick * while we were waiting for the lock.
16265789Smckusick */
16365789Smckusick if (!error) {
16465789Smckusick if (vpid == vdp->v_id)
16565789Smckusick return (0);
16667522Smckusick vput(vdp);
16767522Smckusick if (lockparent && pdp != vdp && (flags & ISLASTCN))
16869428Smckusick VOP_UNLOCK(pdp, 0, p);
16965789Smckusick }
17069428Smckusick if (error = vn_lock(pdp, LK_EXCLUSIVE, p))
17167522Smckusick return (error);
17267522Smckusick vdp = pdp;
17367522Smckusick dp = VTOI(pdp);
17465789Smckusick *vpp = NULL;
17565789Smckusick }
17665789Smckusick
17765789Smckusick len = cnp->cn_namelen;
17865789Smckusick name = cnp->cn_nameptr;
17965789Smckusick /*
18065789Smckusick * A leading `=' means, we are looking for an associated file
18165789Smckusick */
18265789Smckusick if (assoc = (imp->iso_ftype != ISO_FTYPE_RRIP && *name == ASSOCCHAR)) {
18365789Smckusick len--;
18465789Smckusick name++;
18565789Smckusick }
18665789Smckusick
18765789Smckusick /*
18865789Smckusick * If there is cached information on a previous search of
18965789Smckusick * this directory, pick up where we last left off.
19065789Smckusick * We cache only lookups as these are the most common
19165789Smckusick * and have the greatest payoff. Caching CREATE has little
19265789Smckusick * benefit as it usually must search the entire directory
19365789Smckusick * to determine that the entry does not exist. Caching the
19465789Smckusick * location of the last DELETE or RENAME has not reduced
19565789Smckusick * profiling time and hence has been removed in the interest
19665789Smckusick * of simplicity.
19765789Smckusick */
19868047Smckusick bmask = imp->im_bmask;
19965789Smckusick if (nameiop != LOOKUP || dp->i_diroff == 0 ||
20065789Smckusick dp->i_diroff > dp->i_size) {
20165789Smckusick entryoffsetinblock = 0;
20265789Smckusick dp->i_offset = 0;
20365789Smckusick numdirpasses = 1;
20465789Smckusick } else {
20565789Smckusick dp->i_offset = dp->i_diroff;
20668047Smckusick if ((entryoffsetinblock = dp->i_offset & bmask) &&
20768047Smckusick (error = VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp)))
20865789Smckusick return (error);
20965789Smckusick numdirpasses = 2;
21065789Smckusick iso_nchstats.ncs_2passes++;
21165789Smckusick }
21268047Smckusick endsearch = dp->i_size;
21365789Smckusick
21465789Smckusick searchloop:
21565789Smckusick while (dp->i_offset < endsearch) {
21665789Smckusick /*
21765789Smckusick * If offset is on a block boundary,
21865789Smckusick * read the next directory block.
21965789Smckusick * Release previous if it exists.
22065789Smckusick */
22168047Smckusick if ((dp->i_offset & bmask) == 0) {
22265789Smckusick if (bp != NULL)
22365789Smckusick brelse(bp);
22468047Smckusick if (error =
22568047Smckusick VOP_BLKATOFF(vdp, (off_t)dp->i_offset, NULL, &bp))
22665789Smckusick return (error);
22765789Smckusick entryoffsetinblock = 0;
22865789Smckusick }
22965789Smckusick /*
23065789Smckusick * Get pointer to next entry.
23165789Smckusick */
23265789Smckusick ep = (struct iso_directory_record *)
23368047Smckusick ((char *)bp->b_data + entryoffsetinblock);
23465789Smckusick
23568047Smckusick reclen = isonum_711(ep->length);
23665789Smckusick if (reclen == 0) {
23765789Smckusick /* skip to next block, if any */
23865789Smckusick dp->i_offset =
23968047Smckusick (dp->i_offset & ~bmask) + imp->logical_block_size;
24065789Smckusick continue;
24165789Smckusick }
24265789Smckusick
24365789Smckusick if (reclen < ISO_DIRECTORY_RECORD_SIZE)
24465789Smckusick /* illegal entry, stop */
24565789Smckusick break;
24665789Smckusick
24765789Smckusick if (entryoffsetinblock + reclen > imp->logical_block_size)
24865789Smckusick /* entries are not allowed to cross boundaries */
24965789Smckusick break;
25065789Smckusick
25165789Smckusick namelen = isonum_711(ep->name_len);
25265789Smckusick
25365789Smckusick if (reclen < ISO_DIRECTORY_RECORD_SIZE + namelen)
25465789Smckusick /* illegal entry, stop */
25565789Smckusick break;
25665789Smckusick
25768047Smckusick /*
25868047Smckusick * Check for a name match.
25968047Smckusick */
26065789Smckusick switch (imp->iso_ftype) {
26165789Smckusick default:
26265789Smckusick if ((!(isonum_711(ep->flags)&4)) == !assoc) {
26365789Smckusick if ((len == 1
26465789Smckusick && *name == '.')
26565789Smckusick || (flags & ISDOTDOT)) {
26665789Smckusick if (namelen == 1
26765789Smckusick && ep->name[0] == ((flags & ISDOTDOT) ? 1 : 0)) {
26865789Smckusick /*
26965789Smckusick * Save directory entry's inode number and
27068047Smckusick * release directory buffer.
27165789Smckusick */
27268047Smckusick dp->i_ino = isodirino(ep, imp);
27365789Smckusick goto found;
27465789Smckusick }
27565789Smckusick if (namelen != 1
27665789Smckusick || ep->name[0] != 0)
27765789Smckusick goto notfound;
27865789Smckusick } else if (!(res = isofncmp(name,len,
27965789Smckusick ep->name,namelen))) {
28065789Smckusick if (isonum_711(ep->flags)&2)
28168047Smckusick ino = isodirino(ep, imp);
28265789Smckusick else
28365789Smckusick ino = dbtob(bp->b_blkno)
28465789Smckusick + entryoffsetinblock;
28565789Smckusick saveoffset = dp->i_offset;
28665789Smckusick } else if (ino)
28765789Smckusick goto foundino;
28865789Smckusick #ifdef NOSORTBUG /* On some CDs directory entries are not sorted correctly */
28965789Smckusick else if (res < 0)
29065789Smckusick goto notfound;
29165789Smckusick else if (res > 0 && numdirpasses == 2)
29265789Smckusick numdirpasses++;
29365789Smckusick #endif
29465789Smckusick }
29565789Smckusick break;
29665789Smckusick case ISO_FTYPE_RRIP:
29765789Smckusick if (isonum_711(ep->flags)&2)
29868047Smckusick ino = isodirino(ep, imp);
29965789Smckusick else
30065789Smckusick ino = dbtob(bp->b_blkno) + entryoffsetinblock;
30165789Smckusick dp->i_ino = ino;
30265855Smckusick cd9660_rrip_getname(ep,altname,&namelen,&dp->i_ino,imp);
30365789Smckusick if (namelen == cnp->cn_namelen
30465789Smckusick && !bcmp(name,altname,namelen))
30565789Smckusick goto found;
30665789Smckusick ino = 0;
30765789Smckusick break;
30865789Smckusick }
30965789Smckusick dp->i_offset += reclen;
31065789Smckusick entryoffsetinblock += reclen;
31165789Smckusick }
31265789Smckusick if (ino) {
31365789Smckusick foundino:
31465789Smckusick dp->i_ino = ino;
31565789Smckusick if (saveoffset != dp->i_offset) {
31668047Smckusick if (lblkno(imp, dp->i_offset) !=
31768047Smckusick lblkno(imp, saveoffset)) {
31865789Smckusick if (bp != NULL)
31965789Smckusick brelse(bp);
32068047Smckusick if (error = VOP_BLKATOFF(vdp,
32168047Smckusick (off_t)saveoffset, NULL, &bp))
32265789Smckusick return (error);
32365789Smckusick }
32468047Smckusick entryoffsetinblock = saveoffset & bmask;
32568047Smckusick ep = (struct iso_directory_record *)
32668047Smckusick ((char *)bp->b_data + entryoffsetinblock);
32765789Smckusick dp->i_offset = saveoffset;
32865789Smckusick }
32965789Smckusick goto found;
33065789Smckusick }
33165789Smckusick notfound:
33265789Smckusick /*
33365789Smckusick * If we started in the middle of the directory and failed
33465789Smckusick * to find our target, we must check the beginning as well.
33565789Smckusick */
33665789Smckusick if (numdirpasses == 2) {
33765789Smckusick numdirpasses--;
33865789Smckusick dp->i_offset = 0;
33965789Smckusick endsearch = dp->i_diroff;
34065789Smckusick goto searchloop;
34165789Smckusick }
34265789Smckusick if (bp != NULL)
34365789Smckusick brelse(bp);
34468047Smckusick
34565789Smckusick /*
34665789Smckusick * Insert name into cache (as non-existent) if appropriate.
34765789Smckusick */
34865789Smckusick if (cnp->cn_flags & MAKEENTRY)
34965789Smckusick cache_enter(vdp, *vpp, cnp);
35065789Smckusick if (nameiop == CREATE || nameiop == RENAME)
351*69741Smckusick return (EROFS);
35265789Smckusick return (ENOENT);
35365789Smckusick
35465789Smckusick found:
35565789Smckusick if (numdirpasses == 2)
35665789Smckusick iso_nchstats.ncs_pass2++;
35765789Smckusick
35865789Smckusick /*
35965789Smckusick * Found component in pathname.
36065789Smckusick * If the final component of path name, save information
36165789Smckusick * in the cache as to where the entry was found.
36265789Smckusick */
36365789Smckusick if ((flags & ISLASTCN) && nameiop == LOOKUP)
36465789Smckusick dp->i_diroff = dp->i_offset;
36565789Smckusick
36665789Smckusick /*
36765789Smckusick * Step through the translation in the name. We do not `iput' the
36865789Smckusick * directory because we may need it again if a symbolic link
36965789Smckusick * is relative to the current directory. Instead we save it
37065789Smckusick * unlocked as "pdp". We must get the target inode before unlocking
37165789Smckusick * the directory to insure that the inode will not be removed
37265789Smckusick * before we get it. We prevent deadlock by always fetching
37365789Smckusick * inodes from the root, moving down the directory tree. Thus
37465789Smckusick * when following backward pointers ".." we must unlock the
37565789Smckusick * parent directory before getting the requested directory.
37665789Smckusick * There is a potential race condition here if both the current
37765789Smckusick * and parent directories are removed before the `iget' for the
37865789Smckusick * inode associated with ".." returns. We hope that this occurs
37965789Smckusick * infrequently since we cannot avoid this race condition without
38065789Smckusick * implementing a sophisticated deadlock detection algorithm.
38165789Smckusick * Note also that this simple deadlock detection scheme will not
38265789Smckusick * work if the file system has any hard links other than ".."
38365789Smckusick * that point backwards in the directory structure.
38465789Smckusick */
38567522Smckusick pdp = vdp;
38665789Smckusick /*
38765789Smckusick * If ino is different from dp->i_ino,
38865789Smckusick * it's a relocated directory.
38965789Smckusick */
39065789Smckusick if (flags & ISDOTDOT) {
39169428Smckusick VOP_UNLOCK(pdp, 0, p); /* race to get the inode */
39268047Smckusick error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
39368047Smckusick dp->i_ino != ino, ep);
39468047Smckusick brelse(bp);
39568047Smckusick if (error) {
39669428Smckusick vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY, p);
39765789Smckusick return (error);
39865789Smckusick }
39967522Smckusick if (lockparent && (flags & ISLASTCN) &&
40069428Smckusick (error = vn_lock(pdp, LK_EXCLUSIVE, p))) {
40167522Smckusick vput(tdp);
40267522Smckusick return (error);
40367522Smckusick }
40467522Smckusick *vpp = tdp;
40565789Smckusick } else if (dp->i_number == dp->i_ino) {
40668047Smckusick brelse(bp);
40765789Smckusick VREF(vdp); /* we want ourself, ie "." */
40865789Smckusick *vpp = vdp;
40965789Smckusick } else {
41068047Smckusick error = cd9660_vget_internal(vdp->v_mount, dp->i_ino, &tdp,
41168047Smckusick dp->i_ino != ino, ep);
41268047Smckusick brelse(bp);
41368047Smckusick if (error)
41465789Smckusick return (error);
41565789Smckusick if (!lockparent || !(flags & ISLASTCN))
41669428Smckusick VOP_UNLOCK(pdp, 0, p);
41767522Smckusick *vpp = tdp;
41865789Smckusick }
41965789Smckusick
42065789Smckusick /*
42165789Smckusick * Insert name into cache if appropriate.
42265789Smckusick */
42365789Smckusick if (cnp->cn_flags & MAKEENTRY)
42465789Smckusick cache_enter(vdp, *vpp, cnp);
42565789Smckusick return (0);
42665789Smckusick }
42765789Smckusick
42865789Smckusick /*
42968047Smckusick * Return buffer with the contents of block "offset" from the beginning of
43068047Smckusick * directory "ip". If "res" is non-zero, fill it in with a pointer to the
43165789Smckusick * remaining space in the directory.
43265789Smckusick */
43368047Smckusick int
cd9660_blkatoff(ap)43468047Smckusick cd9660_blkatoff(ap)
43568047Smckusick struct vop_blkatoff_args /* {
43668047Smckusick struct vnode *a_vp;
43768047Smckusick off_t a_offset;
43868047Smckusick char **a_res;
43968047Smckusick struct buf **a_bpp;
44068047Smckusick } */ *ap;
44168047Smckusick {
44265789Smckusick struct iso_node *ip;
44368047Smckusick register struct iso_mnt *imp;
44465789Smckusick struct buf *bp;
44568047Smckusick daddr_t lbn;
44668047Smckusick int bsize, error;
44768047Smckusick
44868047Smckusick ip = VTOI(ap->a_vp);
44968047Smckusick imp = ip->i_mnt;
45068047Smckusick lbn = lblkno(imp, ap->a_offset);
45168047Smckusick bsize = blksize(imp, ip, lbn);
45265789Smckusick
45368047Smckusick if (error = bread(ap->a_vp, lbn, bsize, NOCRED, &bp)) {
45465789Smckusick brelse(bp);
45568047Smckusick *ap->a_bpp = NULL;
45665789Smckusick return (error);
45765789Smckusick }
45868047Smckusick if (ap->a_res)
45968047Smckusick *ap->a_res = (char *)bp->b_data + blkoff(imp, ap->a_offset);
46068047Smckusick *ap->a_bpp = bp;
46165789Smckusick return (0);
46265789Smckusick }
463