os161-1.99
 All Data Structures
vfscwd.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * VFS operations involving the current directory.
00032  */
00033 
00034 #include <types.h>
00035 #include <kern/errno.h>
00036 #include <stat.h>
00037 #include <lib.h>
00038 #include <uio.h>
00039 #include <proc.h>
00040 #include <current.h>
00041 #include <vfs.h>
00042 #include <fs.h>
00043 #include <vnode.h>
00044 
00045 /*
00046  * Get current directory as a vnode.
00047  */
00048 int
00049 vfs_getcurdir(struct vnode **ret)
00050 {
00051         int rv = 0;
00052 
00053         spinlock_acquire(&curproc->p_lock);
00054         if (curproc->p_cwd!=NULL) {
00055                 VOP_INCREF(curproc->p_cwd);
00056                 *ret = curproc->p_cwd;
00057         }
00058         else {
00059                 rv = ENOENT;
00060         }
00061         spinlock_release(&curproc->p_lock);
00062 
00063         return rv;
00064 }
00065 
00066 /*
00067  * Set current directory as a vnode.
00068  * The passed vnode must in fact be a directory.
00069  */
00070 int
00071 vfs_setcurdir(struct vnode *dir)
00072 {
00073         struct vnode *old;
00074         mode_t vtype;
00075         int result;
00076 
00077         result = VOP_GETTYPE(dir, &vtype);
00078         if (result) {
00079                 return result;
00080         }
00081         if (vtype != S_IFDIR) {
00082                 return ENOTDIR;
00083         }
00084 
00085         VOP_INCREF(dir);
00086 
00087         spinlock_acquire(&curproc->p_lock);
00088         old = curproc->p_cwd;
00089         curproc->p_cwd = dir;
00090         spinlock_release(&curproc->p_lock);
00091 
00092         if (old!=NULL) { 
00093                 VOP_DECREF(old);
00094         }
00095 
00096         return 0;
00097 }
00098 
00099 /*
00100  * Set current directory to "none".
00101  */
00102 int
00103 vfs_clearcurdir(void)
00104 {
00105         struct vnode *old;
00106 
00107         spinlock_acquire(&curproc->p_lock);
00108         old = curproc->p_cwd;
00109         curproc->p_cwd = NULL;
00110         spinlock_release(&curproc->p_lock);
00111 
00112         if (old!=NULL) { 
00113                 VOP_DECREF(old);
00114         }
00115 
00116         return 0;
00117 }
00118 
00119 /*
00120  * Set current directory, as a pathname. Use vfs_lookup to translate
00121  * it to a vnode.
00122  */
00123 int
00124 vfs_chdir(char *path)
00125 {
00126         struct vnode *vn;
00127         int result;
00128 
00129         result = vfs_lookup(path, &vn);
00130         if (result) {
00131                 return result;
00132         }
00133         result = vfs_setcurdir(vn);
00134         VOP_DECREF(vn);
00135         return result;
00136 }
00137 
00138 /*
00139  * Get current directory, as a pathname.
00140  * Use VOP_NAMEFILE to get the pathname and FSOP_GETVOLNAME to get the
00141  * volume name.
00142  */
00143 int
00144 vfs_getcwd(struct uio *uio)
00145 {
00146         struct vnode *cwd;
00147         int result;
00148         const char *name;
00149         char colon=':';
00150 
00151         KASSERT(uio->uio_rw==UIO_READ);
00152 
00153         result = vfs_getcurdir(&cwd);
00154         if (result) {
00155                 return result;
00156         }
00157 
00158         /* The current dir must be a directory, and thus it is not a device. */
00159         KASSERT(cwd->vn_fs != NULL);
00160 
00161         name = FSOP_GETVOLNAME(cwd->vn_fs);
00162         if (name==NULL) {
00163                 vfs_biglock_acquire();
00164                 name = vfs_getdevname(cwd->vn_fs);
00165                 vfs_biglock_release();
00166         }
00167         KASSERT(name != NULL);
00168 
00169         result = uiomove((char *)name, strlen(name), uio);
00170         if (result) {
00171                 goto out;
00172         }
00173         result = uiomove(&colon, 1, uio);
00174         if (result) {
00175                 goto out;
00176         }
00177 
00178         result = VOP_NAMEFILE(cwd, uio);
00179 
00180  out:
00181 
00182         VOP_DECREF(cwd);
00183         return result;
00184 }
 All Data Structures