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