2 * This Cplant(TM) source code is the property of Sandia National
5 * This Cplant(TM) source code is copyrighted by Sandia National
8 * The redistribution of this Cplant(TM) source code is subject to the
9 * terms of the GNU Lesser General Public License
10 * (see cit/LGPL or http://www.gnu.org/licenses/lgpl.html)
12 * Cplant(TM) Copyright 1998-2003 Sandia Corporation.
13 * Under the terms of Contract DE-AC04-94AL85000, there is a non-exclusive
14 * license for use of this work by or on behalf of the US Government.
15 * Export of this program may require a license from the United States
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 * Questions or comments about this library should be sent to:
37 * Sandia National Laboratories, New Mexico
39 * Albuquerque, NM 87185-1110
44 #if defined(AUTOMOUNT_FILE_NAME) && defined(__linux__)
53 #include <sys/param.h>
54 #include <sys/types.h>
56 #include <sys/queue.h>
63 * Parse next component in path.
65 #ifndef AUTOMOUNT_FILE_NAME
69 _sysio_next_component(const char *path, struct qstr *name)
71 while (*path == PATH_SEPARATOR)
76 while (*path && *path != PATH_SEPARATOR) {
78 37 * name->hashval + *path++;
84 * Given parent, look up component.
87 lookup(struct pnode *parent,
96 if (!parent->p_base->pb_ino)
99 err = _sysio_permitted(parent->p_base->pb_ino, X_OK);
104 * Short-circuit `.' and `..'; We don't cache those.
107 if (name->len == 1 && name->name[0] == '.')
109 else if (name->len == 2 && name->name[0] == '.' && name->name[1] == '.')
110 pno = parent->p_parent;
115 * Get cache entry then.
117 err = _sysio_p_find_alias(parent, name, &pno);
123 * While covered, move to the covering node.
125 while (pno->p_cover && pno->p_cover != pno) {
128 cover = pno->p_cover;
137 * (Re)validate the pnode.
139 err = _sysio_p_validate(pno, intnt, path);
147 * The meat. Walk an absolute or relative path, looking up each
148 * component. Various flags in the nameidata argument govern actions
149 * and return values/state. They are:
151 * ND_NOFOLLOW symbolic links are not followed
152 * ND_NEGOK if terminal/leaf does not exist, return
153 * path node (alias) anyway.
156 _sysio_path_walk(struct pnode *parent, struct nameidata *nd)
160 struct qstr this, next;
178 if (*nd->nd_path == PATH_SEPARATOR) {
180 * Make parent the root of the name space.
182 parent = nd->nd_root;
189 if (!_sysio_init_cwd)
193 * Finally have to set the curretn working directory. We can
194 * not tolerate errors here or else risk leaving the process
195 * in a very unexpected location. We abort then unless all goes
198 icwd = _sysio_init_cwd;
199 _sysio_init_cwd = NULL;
200 if (_sysio_namei(NULL, icwd, 0, NULL, &parent) != 0 ||
201 _sysio_p_chdir(parent) != 0)
207 * (Re)Validate the parent.
209 err = _sysio_p_validate(parent, NULL, NULL);
214 * Prime everything for the loop. Will need another reference to the
215 * initial directory. It'll be dropped later.
219 _sysio_next_component(nd->nd_path, &next);
225 * Derecurse the path tree-walk.
228 ino = nd->nd_pno->p_base->pb_ino;
229 if (S_ISLNK(ino->i_stbuf.st_mode) &&
230 (next.len || !(nd->nd_flags & ND_NOFOLLOW))) {
233 struct nameidata nameidata;
235 if (nd->nd_slicnt >= MAX_SYMLINK) {
241 * Follow symbolic link.
243 lpath = malloc(MAXPATHLEN + 1);
249 ino->i_ops.inop_readlink(nd->nd_pno,
257 lpath[cc] = '\0'; /* NUL term */
259 * Handle symbolic links with recursion. Yuck!
260 * Pass the NULL intent for recursive symlink
261 * except the last component.
264 (nd->nd_flags | ND_NEGOK),
267 !next.len ? nd->nd_intent : NULL);
268 nameidata.nd_slicnt = nd->nd_slicnt + 1;
270 _sysio_path_walk(nd->nd_pno->p_parent, &nameidata);
275 nd->nd_pno = nameidata.nd_pno;
276 ino = nd->nd_pno->p_base->pb_ino;
278 #ifdef AUTOMOUNT_FILE_NAME
280 S_ISDIR(ino->i_stbuf.st_mode) &&
281 (nd->nd_pno->p_mount->mnt_flags & MOUNT_F_AUTO) &&
282 nd->nd_amcnt < MAX_MOUNT_DEPTH &&
283 ino->i_stbuf.st_mode & S_ISUID) {
287 * We're committed to a lookup. It's time to see if
288 * we're going to do it in an automount-point and
289 * arrange the mount if so.
291 assert(!nd->nd_pno->p_cover);
294 &_sysio_mount_file_name,
300 if (!err && _sysio_automount(pno) == 0) {
304 * All went well. Need to switch
305 * parent pno and ino to the
306 * root of the newly mounted sub-tree.
309 * We don't recurseively retry these
310 * things. It's OK to have the new root
311 * be an automount-point but it's going
312 * to take another lookup to accomplish it.
313 * The alternative could get us into an
316 root = nd->nd_pno->p_cover;
323 ino = nd->nd_pno->p_base->pb_ino;
327 * Must send the intent-path again.
333 * Must go back top and retry with this
334 * new pnode as parent.
338 err = 0; /* it never happened */
343 * Set up for next component.
352 * Should only be here if final component was
353 * target of a symlink.
355 nd->nd_path = this.name + this.len;
359 nd->nd_path = this.name + this.len;
360 _sysio_next_component(nd->nd_path, &next);
365 * Parent must be a directory.
367 if (ino && !S_ISDIR(ino->i_stbuf.st_mode)) {
373 * The extra path arg is passed only on the first lookup in the
374 * walk as we cross into each file system, anew. The intent is
375 * passed both on the first lookup and when trying to look up
376 * the final component -- Of the original path, not on the
379 * Confused? Me too and I came up with this weirdness. It's
380 * hints to the file system drivers. Read on.
382 * The first lookup will give everything one needs to ready
383 * everything for the entire operation before the path is
384 * walked. The file system driver knows it's the first lookup
385 * in the walk because it has both the path and the intent.
387 * Alternatively, one could split the duties; The first lookup
388 * can be used to prime the file system inode cache with the
389 * interior nodes we'll want in the path-walk. Then, when
390 * looking up the last component, ready everything for the
391 * operations(s) to come. The file system driver knows it's
392 * the last lookup in the walk because it has the intent,
393 * again, but without the path.
395 * One special case; If we were asked to look up a single
396 * component, we treat it as the last component. The file
397 * system driver never sees the extra path argument. It should
398 * be noted that the driver always has the fully qualified
399 * path, on the target file system, available to it for any
400 * node it is looking up, including the last, via the base
401 * path node and it's ancestor chain.
410 (path && next.len) ? path : NULL);
412 if (err == -ENOENT &&
414 (nd->nd_flags & ND_NEGOK))
418 path = NULL; /* Stop that! */
419 if ((parent->p_mount->mnt_fs !=
420 nd->nd_pno->p_mount->mnt_fs)) {
422 * Crossed into a new fs. We'll want the next lookup
423 * to include the path again.
429 * Release the parent.
436 * Trailing separators cause us to break from the loop with
437 * a parent set but no pnode. Check for that.
443 * Make sure the last processed component was a directory. The
444 * trailing slashes are illegal behind anything else.
447 S_ISDIR(nd->nd_pno->p_base->pb_ino->i_stbuf.st_mode)))
452 * Drop reference to parent if set. Either we have a dup of the original
453 * parent or an intermediate reference.
459 * On error, we will want to drop our reference to the current
460 * path node if at end.
462 if (err && nd->nd_pno) {
472 * for backward compatibility w/protocol switch
473 * remove everything up to the first ':'
474 * fortran libs prepend cwd to path, so not much choice
476 #define STRIP_PREFIX(p) strchr(p,':') ? strchr(p,':')+1 : p
478 #define STRIP_PREFIX(p) p
482 * Expanded form of the path-walk routine, with the common arguments, builds
483 * the nameidata bundle and calls path-walk.
486 _sysio_namei(struct pnode *parent,
489 struct intent *intnt,
492 struct nameidata nameidata;
495 ND_INIT(&nameidata, flags, STRIP_PREFIX(path), _sysio_root, intnt);
496 err = _sysio_path_walk(parent, &nameidata);
498 *pnop = nameidata.nd_pno;