1 /* 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94 35 * $FreeBSD: src/sys/kern/vfs_lookup.c,v 1.38.2.3 2001/08/31 19:36:49 dillon Exp $ 36 */ 37 38 #include "opt_ktrace.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/kernel.h> 43 #include <sys/vnode.h> 44 #include <sys/mount.h> 45 #include <sys/filedesc.h> 46 #include <sys/proc.h> 47 #include <sys/namei.h> 48 #include <sys/sysctl.h> 49 50 #ifdef KTRACE 51 #include <sys/ktrace.h> 52 #endif 53 54 #include <vm/vm_zone.h> 55 56 int varsym_enable = 0; 57 SYSCTL_INT(_vfs, OID_AUTO, varsym_enable, CTLFLAG_RW, &varsym_enable, 0, 58 "Enable Variant Symlinks"); 59 60 /* 61 * OLD API FUNCTION 62 * 63 * Relookup a path name component. This function is only used by the 64 * old API *_rename() code under very specific conditions. CNP_LOCKPARENT 65 * must be set in the cnp. dvp must be unlocked on entry and will be left 66 * unlocked if an error occurs. 67 * 68 * The returned *vpp will be NULL if an error occurs. Note that no error 69 * will occur (error == 0) for CREATE requests on a non-existant target, but 70 * *vpp will be NULL in that case. Both dvp and *vpp (if not NULL) will be 71 * locked in the no-error case. No additional references are made to dvp, 72 * only the locking state changes. 73 */ 74 int 75 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp) 76 { 77 int rdonly; /* lookup read-only flag bit */ 78 int error = 0; 79 80 /* 81 * Setup: break out flag bits into variables. 82 */ 83 KKASSERT(cnp->cn_flags & CNP_LOCKPARENT); 84 KKASSERT(cnp->cn_flags & CNP_PDIRUNLOCK); 85 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY); 86 cnp->cn_flags &= ~CNP_PDIRUNLOCK; 87 88 *vpp = NULL; 89 rdonly = cnp->cn_flags & CNP_RDONLY; 90 91 /* 92 * Search a new directory. 93 */ 94 95 /* 96 * Degenerate lookups (e.g. "/", "..", derived from "/.", etc) 97 * are not allowed. 98 */ 99 if (cnp->cn_nameptr[0] == '\0') { 100 error = EISDIR; 101 goto bad; 102 } 103 if (cnp->cn_flags & CNP_ISDOTDOT) 104 panic ("relookup: lookup on dot-dot"); 105 106 /* 107 * We now have a segment name to search for, and a directory to search. 108 */ 109 if ((error = VOP_OLD_LOOKUP(dvp, vpp, cnp)) != 0) { 110 KASSERT(*vpp == NULL, ("leaf should be empty")); 111 if (error != EJUSTRETURN) 112 goto bad; 113 /* 114 * If creating and at end of pathname, then can consider 115 * allowing file to be created. 116 */ 117 if (rdonly) { 118 error = EROFS; 119 goto bad; 120 } 121 /* 122 * We return with ni_vp NULL to indicate that the entry 123 * doesn't currently exist, leaving a pointer to the 124 * (possibly locked) directory inode in ndp->ni_dvp. 125 */ 126 return (0); 127 } 128 129 /* 130 * Check for symbolic link 131 */ 132 KASSERT((*vpp)->v_type != VLNK || !(cnp->cn_flags & CNP_FOLLOW), 133 ("relookup: symlink found.")); 134 135 /* 136 * Disallow directory write attempts on read-only file systems. 137 */ 138 if (rdonly && (cnp->cn_nameiop == NAMEI_DELETE || 139 cnp->cn_nameiop == NAMEI_RENAME)) { 140 error = EROFS; 141 goto bad; 142 } 143 KKASSERT((cnp->cn_flags & CNP_PDIRUNLOCK) == 0); 144 return (0); 145 146 bad: 147 if ((cnp->cn_flags & CNP_PDIRUNLOCK) == 0) { 148 cnp->cn_flags |= CNP_PDIRUNLOCK; 149 vn_unlock(dvp); 150 } 151 if (*vpp) { 152 vput(*vpp); 153 *vpp = NULL; 154 } 155 return (error); 156 } 157 158