Annotation of prex-old/usr/server/fs/vfs/vfs_lookup.c, Revision 1.1.1.1
1.1 nbrk 1: /*
2: * Copyright (c) 2005-2007, Kohsuke Ohtani
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. Neither the name of the author nor the names of any co-contributors
14: * may be used to endorse or promote products derived from this software
15: * without specific prior written permission.
16: *
17: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27: * SUCH DAMAGE.
28: */
29:
30: /*
31: * lookup.c - vnode lookup function.
32: */
33:
34: #include <sys/vnode.h>
35: #include <sys/mount.h>
36:
37: #include <limits.h>
38: #include <unistd.h>
39: #include <string.h>
40: #include <errno.h>
41:
42: #include <stdlib.h>
43:
44: #include "vfs.h"
45:
46: /*
47: * Convert a pathname into a pointer to a locked vnode.
48: * @path: full path name.
49: * @vpp: vnode to be returned.
50: */
51: int
52: namei(char *path, vnode_t *vpp)
53: {
54: char *p;
55: char node[PATH_MAX];
56: char name[PATH_MAX];
57: mount_t mp;
58: vnode_t dvp, vp;
59: int err, i;
60:
61: vn_printf("namei: path=%s\n", path);
62:
63: /*
64: * Convert a full path name to its mount point and
65: * the local node in the file system.
66: */
67: if (vfs_findroot(path, &mp, &p))
68: return ENOTDIR;
69: strcpy(node, "/");
70: strlcat(node, p, PATH_MAX);
71: vp = vn_lookup(mp, node);
72: if (vp) {
73: /* vnode is already active. */
74: vref(vp);
75: *vpp = vp;
76: return 0;
77: }
78: /*
79: * Find target vnode, started from root directory. This is
80: * done to attach the fs specific data to the target vnode.
81: */
82: if ((dvp = mp->m_root) == NULL)
83: sys_panic("VFS: no root");
84:
85: vref(dvp);
86: vn_lock(dvp);
87: node[0] = '\0';
88:
89: while (*p != '\0') {
90: /*
91: * Get lower directory/file name.
92: */
93: while (*p == '/')
94: p++;
95: for (i = 0; i < PATH_MAX; i++) {
96: if (*p == '\0' || *p == '/')
97: break;
98: name[i] = *p++;
99: }
100: name[i] = '\0';
101:
102: /*
103: * Get a vnode for the target.
104: */
105: strlcat(node, "/", PATH_MAX);
106: strlcat(node, name, PATH_MAX);
107: vp = vn_lookup(mp, node);
108: if (vp) {
109: vref(vp);
110: } else {
111: vp = vget(mp, node);
112: if (vp == NULL) {
113: vput(dvp);
114: return ENOMEM;
115: }
116: /* Find a vnode in this directory. */
117: err = VOP_LOOKUP(dvp, name, vp);
118: if (err || (*p == '/' && vp->v_type != VDIR)) {
119: /* Not found */
120: vput(vp);
121: vput(dvp);
122: return err;
123: }
124: }
125: vput(dvp);
126: dvp = vp;
127: while (*p != '\0' && *p != '/')
128: p++;
129: }
130: *vpp = vp;
131: return 0;
132: }
133:
134: /*
135: * Search a pathname.
136: * @path: full path.
137: * @vpp: pointer to locked vnode for directory.
138: * @name: pointer to file name in path.
139: *
140: * This is a very central but not so complicated routine. ;-P
141: * This routine returns a locked directory vnode and file name.
142: */
143: int
144: lookup(char *path, vnode_t *vpp, char **name)
145: {
146: char buf[PATH_MAX];
147: char root[] = "/";
148: char *file, *dir;
149: vnode_t vp;
150: int err;
151:
152: vn_printf("lookup: path=%s\n", path);
153:
154: /*
155: * Get the path for directory.
156: */
157: strcpy(buf, path);
158: file = strrchr(buf, '/');
159: if (!buf[0])
160: return ENOTDIR;
161: if (file == buf)
162: dir = root;
163: else {
164: *file = '\0';
165: dir = buf;
166: }
167: /*
168: * Get the vnode for directory
169: */
170: if ((err = namei(dir, &vp)) != 0)
171: return err;
172: if (vp->v_type != VDIR) {
173: vput(vp);
174: return ENOTDIR;
175: }
176: *vpp = vp;
177:
178: /*
179: * Get the file name
180: */
181: *name = strrchr(path, '/') + 1;
182: return 0;
183: }
CVSweb