1 /* $NetBSD: filecore_lookup.c,v 1.20 2014/06/03 19:30:30 joerg Exp $ */ 2 3 /*- 4 * Copyright (c) 1989, 1993, 1994 The Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * filecore_lookup.c 1.1 1998/6/26 32 */ 33 34 /*- 35 * Copyright (c) 1998 Andrew McMurry 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * filecore_lookup.c 1.1 1998/6/26 66 */ 67 68 #include <sys/cdefs.h> 69 __KERNEL_RCSID(0, "$NetBSD: filecore_lookup.c,v 1.20 2014/06/03 19:30:30 joerg Exp $"); 70 71 #include <sys/param.h> 72 #include <sys/namei.h> 73 #include <sys/buf.h> 74 #include <sys/file.h> 75 #include <sys/vnode.h> 76 #include <sys/mount.h> 77 #include <sys/systm.h> 78 79 #include <fs/filecorefs/filecore.h> 80 #include <fs/filecorefs/filecore_extern.h> 81 #include <fs/filecorefs/filecore_node.h> 82 83 /* 84 * Convert a component of a pathname into a pointer to a locked inode. 85 * This is a very central and rather complicated routine. 86 * If the file system is not maintained in a strict tree hierarchy, 87 * this can result in a deadlock situation (see comments in code below). 88 * 89 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 90 * whether the name is to be looked up, created, renamed, or deleted. 91 * When CREATE, RENAME, or DELETE is specified, information usable in 92 * creating, renaming, or deleting a directory entry may be calculated. 93 * If flag has LOCKPARENT or'ed into it and the target of the pathname 94 * exists, lookup returns both the target and its parent directory locked. 95 * When creating or renaming and LOCKPARENT is specified, the target may 96 * not be ".". When deleting and LOCKPARENT is specified, the target may 97 * be "."., but the caller must check to ensure it does an vrele and iput 98 * instead of two iputs. 99 * 100 * Overall outline of ufs_lookup: 101 * 102 * check accessibility of directory 103 * look for name in cache, if found, then if at end of path 104 * and deleting or creating, drop it, else return name 105 * search for name in directory, to found or notfound 106 * notfound: 107 * if creating, return locked directory, leaving info on available slots 108 * else return error 109 * found: 110 * if at end of path and deleting, return information to allow delete 111 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target 112 * inode and return info to allow rewrite 113 * if not at end, add name to cache; if at end and neither creating 114 * nor deleting, add name to cache 115 * 116 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent inode unlocked. 117 */ 118 int 119 filecore_lookup(void *v) 120 { 121 struct vop_lookup_v2_args /* { 122 struct vnode *a_dvp; 123 struct vnode **a_vpp; 124 struct componentname *a_cnp; 125 } */ *ap = v; 126 struct vnode *vdp; /* vnode for directory being searched */ 127 struct filecore_node *dp; /* inode for directory being searched */ 128 struct buf *bp; /* a buffer of directory entries */ 129 struct filecore_direntry *de; 130 int numdirpasses; /* strategy for directory search */ 131 struct vnode *pdp; /* saved dp during symlink work */ 132 struct vnode *tdp; /* returned by filecore_vget_internal */ 133 int error; 134 u_short namelen; 135 int res; 136 const char *name; 137 struct vnode **vpp = ap->a_vpp; 138 struct componentname *cnp = ap->a_cnp; 139 kauth_cred_t cred = cnp->cn_cred; 140 int flags; 141 int nameiop = cnp->cn_nameiop; 142 int i, endsearch; 143 144 flags = cnp->cn_flags; 145 146 bp = NULL; 147 *vpp = NULL; 148 vdp = ap->a_dvp; 149 dp = VTOI(vdp); 150 151 /* 152 * Check accessiblity of directory. 153 */ 154 if ((error = VOP_ACCESS(vdp, VEXEC, cred)) != 0) 155 return (error); 156 157 if ((flags & ISLASTCN) && (vdp->v_mount->mnt_flag & MNT_RDONLY) && 158 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 159 return (EROFS); 160 161 /* 162 * We now have a segment name to search for, and a directory to search. 163 * 164 * Before tediously performing a linear scan of the directory, 165 * check the name cache to see if the directory/name pair 166 * we are looking for is known already. 167 */ 168 if (cache_lookup(vdp, cnp->cn_nameptr, cnp->cn_namelen, 169 cnp->cn_nameiop, cnp->cn_flags, NULL, vpp)) { 170 return *vpp == NULLVP ? ENOENT : 0; 171 } 172 173 name = cnp->cn_nameptr; 174 namelen = cnp->cn_namelen; 175 176 /* 177 * If there is cached information on a previous search of 178 * this directory, pick up where we last left off. 179 * We cache only lookups as these are the most common 180 * and have the greatest payoff. Caching CREATE has little 181 * benefit as it usually must search the entire directory 182 * to determine that the entry does not exist. Caching the 183 * location of the last DELETE or RENAME has not reduced 184 * profiling time and hence has been removed in the interest 185 * of simplicity. 186 */ 187 if (nameiop != LOOKUP || dp->i_diroff == 0 || 188 dp->i_diroff >= FILECORE_MAXDIRENTS) { 189 i = 0; 190 numdirpasses = 1; 191 } else { 192 i = dp->i_diroff; 193 numdirpasses = 2; 194 namecache_count_2passes(); 195 } 196 endsearch = FILECORE_MAXDIRENTS; 197 198 if ((flags & ISDOTDOT) || (name[0] == '.' && namelen == 1)) 199 goto found; 200 201 error = filecore_dbread(dp, &bp); 202 if (error) { 203 return error; 204 } 205 206 de = fcdirentry(bp->b_data, i); 207 208 searchloop: 209 while (de->name[0] != 0 && i < endsearch) { 210 /* 211 * Check for a name match. 212 */ 213 res = filecore_fncmp(de->name, name, namelen); 214 215 if (res == 0) 216 goto found; 217 if (res < 0) 218 goto notfound; 219 220 i++; 221 de++; 222 } 223 224 notfound: 225 /* 226 * If we started in the middle of the directory and failed 227 * to find our target, we must check the beginning as well. 228 */ 229 if (numdirpasses == 2) { 230 numdirpasses--; 231 i = 0; 232 de = fcdirentry(bp->b_data, i); 233 endsearch = dp->i_diroff; 234 goto searchloop; 235 } 236 if (bp != NULL) { 237 #ifdef FILECORE_DEBUG_BR 238 printf("brelse(%p) lo1\n", bp); 239 #endif 240 brelse(bp, 0); 241 } 242 243 /* 244 * Insert name into cache (as non-existent) if appropriate. 245 */ 246 cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, 247 cnp->cn_flags); 248 return (nameiop == CREATE || nameiop == RENAME) ? EROFS : ENOENT; 249 250 found: 251 if (numdirpasses == 2) 252 namecache_count_pass2(); 253 254 /* 255 * Found component in pathname. 256 * If the final component of path name, save information 257 * in the cache as to where the entry was found. 258 */ 259 if ((flags & ISLASTCN) && nameiop == LOOKUP) 260 dp->i_diroff = i; 261 262 /* 263 * Step through the translation in the name. We do not `iput' the 264 * directory because we may need it again if a symbolic link 265 * is relative to the current directory. Instead we save it 266 * unlocked as "pdp". We must get the target inode before unlocking 267 * the directory to insure that the inode will not be removed 268 * before we get it. We prevent deadlock by always fetching 269 * inodes from the root, moving down the directory tree. Thus 270 * when following backward pointers ".." we must unlock the 271 * parent directory before getting the requested directory. 272 * There is a potential race condition here if both the current 273 * and parent directories are removed before the `iget' for the 274 * inode associated with ".." returns. We hope that this occurs 275 * infrequently since we cannot avoid this race condition without 276 * implementing a sophisticated deadlock detection algorithm. 277 * Note also that this simple deadlock detection scheme will not 278 * work if the file system has any hard links other than ".." 279 * that point backwards in the directory structure. 280 */ 281 pdp = vdp; 282 283 /* 284 * If ino is different from dp->i_ino, 285 * it's a relocated directory. 286 */ 287 if (flags & ISDOTDOT) { 288 ino_t pin = filecore_getparent(dp); 289 290 VOP_UNLOCK(pdp); /* race to get the inode */ 291 error = VFS_VGET(vdp->v_mount, pin, &tdp); 292 vn_lock(pdp, LK_EXCLUSIVE | LK_RETRY); 293 if (error) { 294 return error; 295 } 296 *vpp = tdp; 297 } else if (name[0] == '.' && namelen == 1) { 298 vref(vdp); /* we want ourself, ie "." */ 299 *vpp = vdp; 300 } else { 301 #ifdef FILECORE_DEBUG_BR 302 printf("brelse(%p) lo4\n", bp); 303 #endif 304 brelse(bp, 0); 305 error = VFS_VGET(vdp->v_mount, dp->i_dirent.addr | 306 (i << FILECORE_INO_INDEX), &tdp); 307 if (error) 308 return (error); 309 *vpp = tdp; 310 } 311 312 /* 313 * Insert name into cache if appropriate. 314 */ 315 cache_enter(vdp, *vpp, cnp->cn_nameptr, cnp->cn_namelen, 316 cnp->cn_flags); 317 if (*vpp != vdp) 318 VOP_UNLOCK(*vpp); 319 return 0; 320 } 321