/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* VFS operations relating to pathname translation31*/3233#include <types.h>34#include <kern/errno.h>35#include <limits.h>36#include <lib.h>37#include <synch.h>38#include <vfs.h>39#include <fs.h>40#include <vnode.h>4142static struct vnode *bootfs_vnode = NULL;4344/*45* Helper function for actually changing bootfs_vnode.46*/47static48void49change_bootfs(struct vnode *newvn)50{51struct vnode *oldvn;5253oldvn = bootfs_vnode;54bootfs_vnode = newvn;5556if (oldvn != NULL) {57VOP_DECREF(oldvn);58}59}6061/*62* Set bootfs_vnode.63*64* Bootfs_vnode is the vnode used for beginning path translation of65* pathnames starting with /.66*67* It is also incidentally the system's first current directory.68*/69int70vfs_setbootfs(const char *fsname)71{72char tmp[NAME_MAX+1];73char *s;74int result;75struct vnode *newguy;7677vfs_biglock_acquire();7879snprintf(tmp, sizeof(tmp)-1, "%s", fsname);80s = strchr(tmp, ':');81if (s) {82/* If there's a colon, it must be at the end */83if (strlen(s)>0) {84vfs_biglock_release();85return EINVAL;86}87}88else {89strcat(tmp, ":");90}9192result = vfs_chdir(tmp);93if (result) {94vfs_biglock_release();95return result;96}9798result = vfs_getcurdir(&newguy);99if (result) {100vfs_biglock_release();101return result;102}103104change_bootfs(newguy);105106vfs_biglock_release();107return 0;108}109110/*111* Clear the bootfs vnode (preparatory to system shutdown).112*/113void114vfs_clearbootfs(void)115{116vfs_biglock_acquire();117change_bootfs(NULL);118vfs_biglock_release();119}120121122/*123* Common code to pull the device name, if any, off the front of a124* path and choose the vnode to begin the name lookup relative to.125*/126127static128int129getdevice(char *path, char **subpath, struct vnode **startvn)130{131int slash=-1, colon=-1, i;132struct vnode *vn;133int result;134135KASSERT(vfs_biglock_do_i_hold());136137/*138* Locate the first colon or slash.139*/140141for (i=0; path[i]; i++) {142if (path[i]==':') {143colon = i;144break;145}146if (path[i]=='/') {147slash = i;148break;149}150}151152if (colon < 0 && slash != 0) {153/*154* No colon before a slash, so no device name155* specified, and the slash isn't leading or is also156* absent, so this is a relative path or just a bare157* filename. Start from the current directory, and158* use the whole thing as the subpath.159*/160*subpath = path;161return vfs_getcurdir(startvn);162}163164if (colon>0) {165/* device:path - get root of device's filesystem */166path[colon]=0;167while (path[colon+1]=='/') {168/* device:/path - skip slash, treat as device:path */169colon++;170}171*subpath = &path[colon+1];172173result = vfs_getroot(path, startvn);174if (result) {175return result;176}177178return 0;179}180181/*182* We have either /path or :path.183*184* /path is a path relative to the root of the "boot filesystem".185* :path is a path relative to the root of the current filesystem.186*/187KASSERT(colon==0 || slash==0);188189if (path[0]=='/') {190if (bootfs_vnode==NULL) {191return ENOENT;192}193VOP_INCREF(bootfs_vnode);194*startvn = bootfs_vnode;195}196else {197KASSERT(path[0]==':');198199result = vfs_getcurdir(&vn);200if (result) {201return result;202}203204/*205* The current directory may not be a device, so it206* must have a fs.207*/208KASSERT(vn->vn_fs!=NULL);209210*startvn = FSOP_GETROOT(vn->vn_fs);211212VOP_DECREF(vn);213}214215while (path[1]=='/') {216/* ///... or :/... */217path++;218}219220*subpath = path+1;221222return 0;223}224225/*226* Name-to-vnode translation.227* (In BSD, both of these are subsumed by namei().)228*/229230int231vfs_lookparent(char *path, struct vnode **retval,232char *buf, size_t buflen)233{234struct vnode *startvn;235int result;236237vfs_biglock_acquire();238239result = getdevice(path, &path, &startvn);240if (result) {241vfs_biglock_release();242return result;243}244245if (strlen(path)==0) {246/*247* It does not make sense to use just a device name in248* a context where "lookparent" is the desired249* operation.250*/251result = EINVAL;252}253else {254result = VOP_LOOKPARENT(startvn, path, retval, buf, buflen);255}256257VOP_DECREF(startvn);258259vfs_biglock_release();260return result;261}262263int264vfs_lookup(char *path, struct vnode **retval)265{266struct vnode *startvn;267int result;268269vfs_biglock_acquire();270271result = getdevice(path, &path, &startvn);272if (result) {273vfs_biglock_release();274return result;275}276277if (strlen(path)==0) {278*retval = startvn;279vfs_biglock_release();280return 0;281}282283result = VOP_LOOKUP(startvn, path, retval);284285VOP_DECREF(startvn);286vfs_biglock_release();287return result;288}289290291