|
|
1.1 root 1: /*
2: * Copyright (c) 1989 The Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. All advertising materials mentioning features or use of this software
14: * must display the following acknowledgement:
15: * This product includes software developed by the University of
16: * California, Berkeley and its contributors.
17: * 4. Neither the name of the University nor the names of its contributors
18: * may be used to endorse or promote products derived from this software
19: * without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31: * SUCH DAMAGE.
32: *
33: * @(#)ufs_lookup.c 7.33 (Berkeley) 5/19/91
1.1.1.2 ! root 34: *
! 35: * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE
! 36: * -------------------- ----- ----------------------
! 37: * CURRENT PATCH LEVEL: 1 00040
! 38: * -------------------- ----- ----------------------
! 39: *
! 40: * 10 Aug 92 Scott Burris Fixed "delete from CD-ROM" bug
1.1 root 41: */
42:
43: #include "param.h"
44: #include "namei.h"
45: #include "buf.h"
46: #include "file.h"
47: #include "vnode.h"
48: #include "mount.h"
49:
50: #include "iso.h"
51: #include "isofs_node.h"
52:
53: struct nchstats nchstats;
54:
55: /*
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).
60: *
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.
71: *
72: * Overall outline of ufs_lookup:
73: *
74: * check accessibility of directory
75: * look for name in cache, if found, then if at end of path
76: * and deleting or creating, drop it, else return name
77: * search for name in directory, to found or notfound
78: * notfound:
79: * if creating, return locked directory, leaving info on available slots
80: * else return error
81: * found:
82: * if at end of path and deleting, return information to allow delete
83: * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
84: * inode and return info to allow rewrite
85: * if not at end, add name to cache; if at end and neither creating
86: * nor deleting, add name to cache
87: *
88: * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked.
89: */
90: isofs_lookup(vdp, ndp, p)
91: register struct vnode *vdp;
92: register struct nameidata *ndp;
93: struct proc *p;
94: {
95: register struct iso_node *dp; /* the directory we are searching */
96: register struct iso_mnt *imp; /* file system that directory is in */
97: struct buf *bp = 0; /* a buffer of directory entries */
98: register struct iso_directory_record *ep;
99: /* the current directory entry */
100: int entryoffsetinblock; /* offset of ep in bp's buffer */
101: enum {NONE, COMPACT, FOUND} slotstatus;
102: int slotoffset = -1; /* offset of area with free space */
103: int slotsize; /* size of area at slotoffset */
104: int slotfreespace; /* amount of space free in slot */
105: int slotneeded; /* size of the entry we're seeking */
106: int numdirpasses; /* strategy for directory search */
107: int endsearch; /* offset to end directory search */
108: struct iso_node *pdp; /* saved dp during symlink work */
109: struct iso_node *tdp; /* returned by iget */
110: int flag; /* LOOKUP, CREATE, RENAME, or DELETE */
111: int lockparent; /* 1 => lockparent flag is set */
112: int wantparent; /* 1 => wantparent or lockparent flag */
113: int error;
114:
115: int reclen;
116: int namelen;
117:
118: ndp->ni_dvp = vdp;
119: ndp->ni_vp = NULL;
120: dp = VTOI(vdp);
121: imp = dp->i_mnt;
122: lockparent = ndp->ni_nameiop & LOCKPARENT;
123: flag = ndp->ni_nameiop & OPMASK;
124: wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
125:
126: /*
127: * Check accessiblity of directory.
128: */
129: if ((dp->iso_flags & 2) == 0)
130: return (ENOTDIR);
131:
132: /*
133: * We now have a segment name to search for, and a directory to search.
134: *
135: * Before tediously performing a linear scan of the directory,
136: * check the name cache to see if the directory/name pair
137: * we are looking for is known already.
138: */
139: if (error = cache_lookup(ndp)) {
140: int vpid; /* capability number of vnode */
141:
142: if (error == ENOENT)
143: return (error);
144: #ifdef PARANOID
145: if (vdp == ndp->ni_rdir && ndp->ni_isdotdot)
146: panic("ufs_lookup: .. through root");
147: #endif
148: /*
149: * Get the next vnode in the path.
150: * See comment below starting `Step through' for
151: * an explaination of the locking protocol.
152: */
153: pdp = dp;
154: dp = VTOI(ndp->ni_vp);
155: vdp = ndp->ni_vp;
156: vpid = vdp->v_id;
157: if (pdp == dp) {
158: VREF(vdp);
159: error = 0;
160: } else if (ndp->ni_isdotdot) {
161: ISO_IUNLOCK(pdp);
162: error = vget(vdp);
163: if (!error && lockparent && *ndp->ni_next == '\0')
164: ISO_ILOCK(pdp);
165: } else {
166: error = vget(vdp);
167: if (!lockparent || error || *ndp->ni_next != '\0')
168: ISO_IUNLOCK(pdp);
169: }
170: /*
171: * Check that the capability number did not change
172: * while we were waiting for the lock.
173: */
174: if (!error) {
175: if (vpid == vdp->v_id)
176: return (0);
177: iso_iput(dp);
178: if (lockparent && pdp != dp && *ndp->ni_next == '\0')
179: ISO_IUNLOCK(pdp);
180: }
181: ISO_ILOCK(pdp);
182: dp = pdp;
183: vdp = ITOV(dp);
184: ndp->ni_vp = NULL;
185: }
186:
187: /*
188: * If there is cached information on a previous search of
189: * this directory, pick up where we last left off.
190: * We cache only lookups as these are the most common
191: * and have the greatest payoff. Caching CREATE has little
192: * benefit as it usually must search the entire directory
193: * to determine that the entry does not exist. Caching the
194: * location of the last DELETE or RENAME has not reduced
195: * profiling time and hence has been removed in the interest
196: * of simplicity.
197: */
198: if (flag != LOOKUP || dp->i_diroff == 0 || dp->i_diroff > dp->i_size) {
199: ndp->ni_ufs.ufs_offset = 0;
200: numdirpasses = 1;
201: } else {
202: ndp->ni_ufs.ufs_offset = dp->i_diroff;
203: entryoffsetinblock = iso_blkoff(imp, ndp->ni_ufs.ufs_offset);
204: if (entryoffsetinblock != 0) {
205: if (error = iso_blkatoff(dp, ndp->ni_ufs.ufs_offset,
206: (char **)0, &bp))
207: return (error);
208: }
209: numdirpasses = 2;
210: nchstats.ncs_2passes++;
211: }
212: endsearch = roundup(dp->i_size, imp->logical_block_size);
213:
214: searchloop:
215: while (ndp->ni_ufs.ufs_offset < endsearch) {
216: /*
217: * If offset is on a block boundary,
218: * read the next directory block.
219: * Release previous if it exists.
220: */
221: if (iso_blkoff(imp, ndp->ni_ufs.ufs_offset) == 0) {
222: if (bp != NULL)
223: brelse(bp);
224: if (error = iso_blkatoff(dp, ndp->ni_ufs.ufs_offset,
225: (char **)0, &bp))
226: return (error);
227: entryoffsetinblock = 0;
228: }
229: /*
230: * Get pointer to next entry.
231: */
232:
233: ep = (struct iso_directory_record *)
234: (bp->b_un.b_addr + entryoffsetinblock);
235:
236: reclen = isonum_711 (ep->length);
237: if (reclen == 0) {
238: /* skip to next block, if any */
239: ndp->ni_ufs.ufs_offset =
240: roundup (ndp->ni_ufs.ufs_offset,
241: imp->logical_block_size);
242: continue;
243: }
244:
245: if (reclen < sizeof (struct iso_directory_record))
246: /* illegal entry, stop */
247: break;
248:
1.1.1.2 ! root 249: /* 10 Aug 92*/ if (entryoffsetinblock + reclen -1 >= imp->logical_block_size)
1.1 root 250: /* entries are not allowed to cross boundaries */
251: break;
252:
253: /*
254: * Check for a name match.
255: */
256: namelen = isonum_711 (ep->name_len);
257:
258: if (reclen < sizeof (struct iso_directory_record) + namelen)
259: /* illegal entry, stop */
260: break;
261:
262: if ((namelen == 1
263: && ((ndp->ni_namelen == 1
264: && ndp->ni_ptr[0] == '.'
265: && ep->name[0] == 0)
266: || (ndp->ni_isdotdot && ep->name[0] == 1)))
267: || (namelen >= ndp->ni_namelen
268: && isofncmp(ndp->ni_ptr, ndp->ni_namelen, ep->name,
269: namelen))) {
270: /*
271: * Save directory entry's inode number and
272: * reclen in ndp->ni_ufs area, and release
273: * directory buffer.
274: */
275: ndp->ni_ufs.ufs_ino = isonum_733 (ep->extent);
276: brelse(bp);
277: goto found;
278: }
279: ndp->ni_ufs.ufs_offset += reclen;
280: entryoffsetinblock += reclen;
281: }
282: /* notfound: */
283: /*
284: * If we started in the middle of the directory and failed
285: * to find our target, we must check the beginning as well.
286: */
287: if (numdirpasses == 2) {
288: numdirpasses--;
289: ndp->ni_ufs.ufs_offset = 0;
290: endsearch = dp->i_diroff;
291: goto searchloop;
292: }
293: if (bp != NULL)
294: brelse(bp);
295: /*
296: * Insert name into cache (as non-existent) if appropriate.
297: */
298: if (ndp->ni_makeentry)
299: cache_enter(ndp);
300: return (ENOENT);
301:
302: found:
303: if (numdirpasses == 2)
304: nchstats.ncs_pass2++;
305:
306: /*
307: * Found component in pathname.
308: * If the final component of path name, save information
309: * in the cache as to where the entry was found.
310: */
311: if (*ndp->ni_next == '\0' && flag == LOOKUP)
312: dp->i_diroff = ndp->ni_ufs.ufs_offset
313: &~ (imp->logical_block_size - 1);
314:
315: /*
316: * Step through the translation in the name. We do not `iput' the
317: * directory because we may need it again if a symbolic link
318: * is relative to the current directory. Instead we save it
319: * unlocked as "pdp". We must get the target inode before unlocking
320: * the directory to insure that the inode will not be removed
321: * before we get it. We prevent deadlock by always fetching
322: * inodes from the root, moving down the directory tree. Thus
323: * when following backward pointers ".." we must unlock the
324: * parent directory before getting the requested directory.
325: * There is a potential race condition here if both the current
326: * and parent directories are removed before the `iget' for the
327: * inode associated with ".." returns. We hope that this occurs
328: * infrequently since we cannot avoid this race condition without
329: * implementing a sophisticated deadlock detection algorithm.
330: * Note also that this simple deadlock detection scheme will not
331: * work if the file system has any hard links other than ".."
332: * that point backwards in the directory structure.
333: */
334: pdp = dp;
335: if (ndp->ni_isdotdot) {
336: ISO_IUNLOCK(pdp); /* race to get the inode */
337: if (error = iso_iget(dp, ndp->ni_ufs.ufs_ino, &tdp, ep)) {
338: ISO_ILOCK(pdp);
339: return (error);
340: }
341: if (lockparent && *ndp->ni_next == '\0')
342: ISO_ILOCK(pdp);
343: ndp->ni_vp = ITOV(tdp);
344: } else if (dp->i_number == ndp->ni_ufs.ufs_ino) {
345: VREF(vdp); /* we want ourself, ie "." */
346: ndp->ni_vp = vdp;
347: } else {
348: if (error = iso_iget(dp, ndp->ni_ufs.ufs_ino, &tdp, ep))
349: return (error);
350: if (!lockparent || *ndp->ni_next != '\0')
351: ISO_IUNLOCK(pdp);
352: ndp->ni_vp = ITOV(tdp);
353: }
354:
355: /*
356: * Insert name into cache if appropriate.
357: */
358: if (ndp->ni_makeentry)
359: cache_enter(ndp);
360: return (0);
361: }
362:
363:
364: /*
365: * Return buffer with contents of block "offset"
366: * from the beginning of directory "ip". If "res"
367: * is non-zero, fill it in with a pointer to the
368: * remaining space in the directory.
369: */
370: iso_blkatoff(ip, offset, res, bpp)
371: struct iso_node *ip;
372: off_t offset;
373: char **res;
374: struct buf **bpp;
375: {
376: register struct iso_mnt *imp = ip->i_mnt;
377: daddr_t lbn = iso_lblkno (imp, offset);
378: int bsize = iso_blksize (imp, ip, lbn);
379: struct buf *bp;
380: int error;
381:
382: *bpp = 0;
383: if (error = bread(ITOV(ip), lbn, bsize, NOCRED, &bp)) {
384: brelse(bp);
385: return (error);
386: }
387: if (res)
388: *res = bp->b_un.b_addr + iso_blkoff(imp, offset);
389: *bpp = bp;
390:
391: return (0);
392: }
393:
394:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.