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