]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
316670eb | 2 | * Copyright (c) 1999-2012 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1989, 1993 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * (c) UNIX System Laboratories, Inc. | |
32 | * All or some portions of this file are derived from material licensed | |
33 | * to the University of California by American Telephone and Telegraph | |
34 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
35 | * the permission of UNIX System Laboratories, Inc. | |
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 | * @(#)hfs_lookup.c 1.0 | |
66 | * derived from @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95 | |
67 | * | |
68 | * (c) 1998-1999 Apple Computer, Inc. All Rights Reserved | |
69 | * (c) 1990, 1992 NeXT Computer, Inc. All Rights Reserved | |
70 | * | |
71 | * | |
72 | * hfs_lookup.c -- code to handle directory traversal on HFS/HFS+ volume | |
1c79356b A |
73 | */ |
74 | ||
75 | #include <sys/param.h> | |
1c79356b A |
76 | #include <sys/file.h> |
77 | #include <sys/mount.h> | |
78 | #include <sys/vnode.h> | |
79 | #include <sys/malloc.h> | |
91447636 A |
80 | #include <sys/kdebug.h> |
81 | #include <sys/kauth.h> | |
2d21ac55 | 82 | #include <sys/namei.h> |
316670eb | 83 | #include <sys/user.h> |
1c79356b | 84 | |
9bccf70c A |
85 | #include "hfs.h" |
86 | #include "hfs_catalog.h" | |
87 | #include "hfs_cnode.h" | |
1c79356b | 88 | |
1c79356b | 89 | |
1c79356b A |
90 | /* |
91 | * FROM FREEBSD 3.1 | |
9bccf70c | 92 | * Convert a component of a pathname into a pointer to a locked cnode. |
1c79356b A |
93 | * This is a very central and rather complicated routine. |
94 | * If the file system is not maintained in a strict tree hierarchy, | |
95 | * this can result in a deadlock situation (see comments in code below). | |
96 | * | |
97 | * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending | |
98 | * on whether the name is to be looked up, created, renamed, or deleted. | |
99 | * When CREATE, RENAME, or DELETE is specified, information usable in | |
100 | * creating, renaming, or deleting a directory entry may be calculated. | |
101 | * Notice that these are the only operations that can affect the directory of the target. | |
102 | * | |
1c79356b A |
103 | * LOCKPARENT and WANTPARENT actually refer to the parent of the last item, |
104 | * so if ISLASTCN is not set, they should be ignored. Also they are mutually exclusive, or | |
105 | * WANTPARENT really implies DONTLOCKPARENT. Either of them set means that the calling | |
106 | * routine wants to access the parent of the target, locked or unlocked. | |
107 | * | |
108 | * Keeping the parent locked as long as possible protects from other processes | |
9bccf70c | 109 | * looking up the same item, so it has to be locked until the cnode is totally finished |
1c79356b | 110 | * |
1c79356b A |
111 | * hfs_cache_lookup() performs the following for us: |
112 | * check that it is a directory | |
113 | * check accessibility of directory | |
114 | * check for modification attempts on read-only mounts | |
115 | * if name found in cache | |
116 | * if at end of path and deleting or creating | |
117 | * drop it | |
118 | * else | |
119 | * return name. | |
91447636 | 120 | * return hfs_lookup() |
1c79356b A |
121 | * |
122 | * Overall outline of hfs_lookup: | |
123 | * | |
124 | * handle simple cases of . and .. | |
125 | * search for name in directory, to found or notfound | |
126 | * notfound: | |
127 | * if creating, return locked directory, leaving info on available slots | |
128 | * else return error | |
129 | * found: | |
130 | * if at end of path and deleting, return information to allow delete | |
131 | * if at end of path and rewriting (RENAME and LOCKPARENT), lock target | |
9bccf70c | 132 | * cnode and return info to allow rewrite |
1c79356b A |
133 | * if not at end, add name to cache; if at end and neither creating |
134 | * nor deleting, add name to cache | |
135 | */ | |
136 | ||
91447636 | 137 | |
1c79356b | 138 | /* |
91447636 A |
139 | * Lookup *cnp in directory *dvp, return it in *vpp. |
140 | * **vpp is held on exit. | |
9bccf70c | 141 | * We create a cnode for the file, but we do NOT open the file here. |
1c79356b A |
142 | |
143 | #% lookup dvp L ? ? | |
144 | #% lookup vpp - L - | |
145 | ||
146 | IN struct vnode *dvp - Parent node of file; | |
9bccf70c A |
147 | INOUT struct vnode **vpp - node of target file, its a new node if |
148 | the target vnode did not exist; | |
1c79356b A |
149 | IN struct componentname *cnp - Name of file; |
150 | ||
151 | * When should we lock parent_hp in here ?? | |
152 | */ | |
91447636 | 153 | static int |
2d21ac55 | 154 | hfs_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, int *cnode_locked) |
1c79356b | 155 | { |
9bccf70c A |
156 | struct cnode *dcp; /* cnode for directory being searched */ |
157 | struct vnode *tvp; /* target vnode */ | |
158 | struct hfsmount *hfsmp; | |
9bccf70c | 159 | int flags; |
9bccf70c A |
160 | int nameiop; |
161 | int retval = 0; | |
162 | int isDot; | |
91447636 | 163 | struct cat_desc desc; |
9bccf70c A |
164 | struct cat_desc cndesc; |
165 | struct cat_attr attr; | |
166 | struct cat_fork fork; | |
91447636 | 167 | int lockflags; |
6d2010ae | 168 | int newvnode_flags; |
9bccf70c | 169 | |
2d21ac55 | 170 | retry: |
6d2010ae | 171 | newvnode_flags = 0; |
2d21ac55 | 172 | dcp = NULL; |
9bccf70c A |
173 | hfsmp = VTOHFS(dvp); |
174 | *vpp = NULL; | |
91447636 | 175 | *cnode_locked = 0; |
9bccf70c A |
176 | isDot = FALSE; |
177 | tvp = NULL; | |
178 | nameiop = cnp->cn_nameiop; | |
9bccf70c | 179 | flags = cnp->cn_flags; |
91447636 A |
180 | bzero(&desc, sizeof(desc)); |
181 | ||
1c79356b A |
182 | /* |
183 | * First check to see if it is a . or .., else look it up. | |
184 | */ | |
9bccf70c | 185 | if (flags & ISDOTDOT) { /* Wanting the parent */ |
91447636 | 186 | cnp->cn_flags &= ~MAKEENTRY; |
9bccf70c A |
187 | goto found; /* .. is always defined */ |
188 | } else if ((cnp->cn_nameptr[0] == '.') && (cnp->cn_namelen == 1)) { | |
1c79356b | 189 | isDot = TRUE; |
91447636 | 190 | cnp->cn_flags &= ~MAKEENTRY; |
9bccf70c A |
191 | goto found; /* We always know who we are */ |
192 | } else { | |
2d21ac55 A |
193 | if (hfs_lock(VTOC(dvp), HFS_EXCLUSIVE_LOCK) != 0) { |
194 | retval = ENOENT; /* The parent no longer exists ? */ | |
195 | goto exit; | |
196 | } | |
197 | dcp = VTOC(dvp); | |
91447636 | 198 | |
2d21ac55 A |
199 | if (dcp->c_flag & C_DIR_MODIFICATION) { |
200 | // XXXdbg - if we could msleep on a lck_rw_t then we would do that | |
201 | // but since we can't we have to unlock, delay for a bit | |
202 | // and then retry... | |
203 | // msleep((caddr_t)&dcp->c_flag, &dcp->c_rwlock, PINOD, "hfs_vnop_lookup", 0); | |
204 | hfs_unlock(dcp); | |
205 | tsleep((caddr_t)dvp, PRIBIO, "hfs_lookup", 1); | |
206 | ||
207 | goto retry; | |
91447636 | 208 | } |
1c79356b | 209 | |
9bccf70c | 210 | /* No need to go to catalog if there are no children */ |
91447636 | 211 | if (dcp->c_entries == 0) { |
9bccf70c | 212 | goto notfound; |
91447636 | 213 | } |
1c79356b | 214 | |
9bccf70c | 215 | bzero(&cndesc, sizeof(cndesc)); |
2d21ac55 | 216 | cndesc.cd_nameptr = (const u_int8_t *)cnp->cn_nameptr; |
9bccf70c | 217 | cndesc.cd_namelen = cnp->cn_namelen; |
2d21ac55 | 218 | cndesc.cd_parentcnid = dcp->c_fileid; |
9bccf70c | 219 | cndesc.cd_hint = dcp->c_childhint; |
1c79356b | 220 | |
91447636 A |
221 | lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK); |
222 | ||
2d21ac55 | 223 | retval = cat_lookup(hfsmp, &cndesc, 0, &desc, &attr, &fork, NULL); |
9bccf70c | 224 | |
91447636 A |
225 | hfs_systemfile_unlock(hfsmp, lockflags); |
226 | ||
9bccf70c A |
227 | if (retval == 0) { |
228 | dcp->c_childhint = desc.cd_hint; | |
2d21ac55 A |
229 | /* |
230 | * Note: We must drop the parent lock here before calling | |
231 | * hfs_getnewvnode (which takes the child lock). | |
232 | */ | |
6d2010ae A |
233 | hfs_unlock(dcp); |
234 | dcp = NULL; | |
235 | ||
236 | /* Verify that the item just looked up isn't one of the hidden directories. */ | |
237 | if (desc.cd_cnid == hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid || | |
238 | desc.cd_cnid == hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid) { | |
239 | retval = ENOENT; | |
240 | goto exit; | |
241 | } | |
242 | ||
9bccf70c A |
243 | goto found; |
244 | } | |
245 | notfound: | |
2d21ac55 A |
246 | /* |
247 | * ENAMETOOLONG supersedes other errors | |
248 | * | |
249 | * For a CREATE or RENAME operation on the last component | |
250 | * the ENAMETOOLONG will be handled in the next VNOP. | |
251 | */ | |
252 | if ((retval != ENAMETOOLONG) && | |
253 | (cnp->cn_namelen > kHFSPlusMaxFileNameChars) && | |
254 | (((flags & ISLASTCN) == 0) || ((nameiop != CREATE) && (nameiop != RENAME)))) { | |
91447636 A |
255 | retval = ENAMETOOLONG; |
256 | } else if (retval == 0) { | |
257 | retval = ENOENT; | |
258 | } | |
2d21ac55 A |
259 | if (retval != ENOENT) |
260 | goto exit; | |
9bccf70c A |
261 | /* |
262 | * This is a non-existing entry | |
263 | * | |
264 | * If creating, and at end of pathname and current | |
265 | * directory has not been removed, then can consider | |
266 | * allowing file to be created. | |
267 | */ | |
268 | if ((nameiop == CREATE || nameiop == RENAME || | |
269 | (nameiop == DELETE && | |
91447636 A |
270 | (cnp->cn_flags & DOWHITEOUT) && |
271 | (cnp->cn_flags & ISWHITEOUT))) && | |
272 | (flags & ISLASTCN) && | |
2d21ac55 | 273 | !(ISSET(dcp->c_flag, C_DELETED | C_NOEXISTS))) { |
9bccf70c A |
274 | retval = EJUSTRETURN; |
275 | goto exit; | |
1c79356b | 276 | } |
1c79356b | 277 | /* |
2d21ac55 | 278 | * Insert name into the name cache (as non-existent). |
1c79356b | 279 | */ |
2d21ac55 A |
280 | if ((hfsmp->hfs_flags & HFS_STANDARD) == 0 && |
281 | (cnp->cn_flags & MAKEENTRY) && | |
282 | (nameiop != CREATE)) { | |
91447636 | 283 | cache_enter(dvp, NULL, cnp); |
2d21ac55 | 284 | dcp->c_flag |= C_NEG_ENTRIES; |
91447636 | 285 | } |
9bccf70c | 286 | goto exit; |
1c79356b | 287 | } |
1c79356b | 288 | |
9bccf70c | 289 | found: |
91447636 A |
290 | if (flags & ISLASTCN) { |
291 | switch(nameiop) { | |
292 | case DELETE: | |
293 | cnp->cn_flags &= ~MAKEENTRY; | |
294 | break; | |
295 | ||
296 | case RENAME: | |
297 | cnp->cn_flags &= ~MAKEENTRY; | |
298 | if (isDot) { | |
299 | retval = EISDIR; | |
9bccf70c | 300 | goto exit; |
91447636 A |
301 | } |
302 | break; | |
1c79356b | 303 | } |
91447636 | 304 | } |
9bccf70c | 305 | |
91447636 A |
306 | if (isDot) { |
307 | if ((retval = vnode_get(dvp))) | |
9bccf70c | 308 | goto exit; |
91447636 A |
309 | *vpp = dvp; |
310 | } else if (flags & ISDOTDOT) { | |
2d21ac55 A |
311 | /* |
312 | * Directory hard links can have multiple parents so | |
313 | * find the appropriate parent for the current thread. | |
314 | */ | |
6d2010ae | 315 | if ((retval = hfs_vget(hfsmp, hfs_currentparent(VTOC(dvp)), &tvp, 0, 0))) { |
9bccf70c | 316 | goto exit; |
2d21ac55 | 317 | } |
91447636 | 318 | *cnode_locked = 1; |
9bccf70c | 319 | *vpp = tvp; |
9bccf70c A |
320 | } else { |
321 | int type = (attr.ca_mode & S_IFMT); | |
1c79356b | 322 | |
91447636 | 323 | if (!(flags & ISLASTCN) && (type != S_IFDIR) && (type != S_IFLNK)) { |
9bccf70c A |
324 | retval = ENOTDIR; |
325 | goto exit; | |
1c79356b | 326 | } |
2d21ac55 A |
327 | /* Don't cache directory hardlink names. */ |
328 | if (attr.ca_recflags & kHFSHasLinkChainMask) { | |
329 | cnp->cn_flags &= ~MAKEENTRY; | |
330 | } | |
91447636 A |
331 | /* Names with composed chars are not cached. */ |
332 | if (cnp->cn_namelen != desc.cd_namelen) | |
333 | cnp->cn_flags &= ~MAKEENTRY; | |
1c79356b | 334 | |
6d2010ae | 335 | retval = hfs_getnewvnode(hfsmp, dvp, cnp, &desc, 0, &attr, &fork, &tvp, &newvnode_flags); |
1c79356b | 336 | |
2d21ac55 A |
337 | if (retval) { |
338 | /* | |
6d2010ae A |
339 | * If this was a create/rename operation lookup, then by this point |
340 | * we expected to see the item returned from hfs_getnewvnode above. | |
341 | * In the create case, it would probably eventually bubble out an EEXIST | |
342 | * because the item existed when we were trying to create it. In the | |
343 | * rename case, it would let us know that we need to go ahead and | |
344 | * delete it as part of the rename. However, if we hit the condition below | |
345 | * then it means that we found the element during cat_lookup above, but | |
346 | * it is now no longer there. We simply behave as though we never found | |
347 | * the element at all and return EJUSTRETURN. | |
348 | */ | |
2d21ac55 | 349 | if ((retval == ENOENT) && |
6d2010ae A |
350 | ((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME)) && |
351 | (flags & ISLASTCN)) { | |
2d21ac55 A |
352 | retval = EJUSTRETURN; |
353 | } | |
6d2010ae A |
354 | |
355 | /* | |
356 | * If this was a straight lookup operation, we may need to redrive the entire | |
357 | * lookup starting from cat_lookup if the element was deleted as the result of | |
358 | * a rename operation. Since rename is supposed to guarantee atomicity, then | |
359 | * lookups cannot fail because the underlying element is deleted as a result of | |
360 | * the rename call -- either they returned the looked up element prior to rename | |
361 | * or return the newer element. If we are in this region, then all we can do is add | |
362 | * workarounds to guarantee the latter case. The element has already been deleted, so | |
363 | * we just re-try the lookup to ensure the caller gets the most recent element. | |
364 | */ | |
365 | if ((retval == ENOENT) && (cnp->cn_nameiop == LOOKUP) && | |
366 | (newvnode_flags & (GNV_CHASH_RENAMED | GNV_CAT_DELETED))) { | |
367 | if (dcp) { | |
368 | hfs_unlock (dcp); | |
369 | } | |
370 | /* get rid of any name buffers that may have lingered from the cat_lookup call */ | |
371 | cat_releasedesc (&desc); | |
372 | goto retry; | |
373 | } | |
374 | ||
375 | /* Also, re-drive the lookup if the item we looked up was a hardlink, and the number | |
376 | * or name of hardlinks has changed in the interim between the cat_lookup above, and | |
377 | * our call to hfs_getnewvnode. hfs_getnewvnode will validate the cattr we passed it | |
378 | * against what is actually in the catalog after the cnode is created. If there were | |
379 | * any issues, it will bubble out ERECYCLE, which we need to swallow and use as the | |
380 | * key to redrive as well. We need to special case this below because in this case, | |
381 | * it needs to occur regardless of the type of lookup we're doing here. | |
382 | */ | |
383 | if ((retval == ERECYCLE) && (newvnode_flags & GNV_CAT_ATTRCHANGED)) { | |
384 | if (dcp) { | |
385 | hfs_unlock (dcp); | |
386 | } | |
387 | /* get rid of any name buffers that may have lingered from the cat_lookup call */ | |
388 | cat_releasedesc (&desc); | |
389 | retval = 0; | |
390 | goto retry; | |
391 | } | |
392 | ||
393 | /* skip to the error-handling code if we can't retry */ | |
91447636 | 394 | goto exit; |
2d21ac55 | 395 | } |
b0d623f7 | 396 | |
935ed37a A |
397 | /* |
398 | * Save the origin info for file and directory hardlinks. Directory hardlinks | |
399 | * need the origin for '..' lookups, and file hardlinks need it to ensure that | |
400 | * competing lookups do not cause us to vend different hardlinks than the ones requested. | |
401 | * We want to restrict saving the cache entries to LOOKUP namei operations, since | |
402 | * we're really doing this to protect getattr. | |
403 | */ | |
b0d623f7 | 404 | if ((nameiop == LOOKUP) && (VTOC(tvp)->c_flag & C_HARDLINK)) { |
2d21ac55 A |
405 | hfs_savelinkorigin(VTOC(tvp), VTOC(dvp)->c_fileid); |
406 | } | |
91447636 A |
407 | *cnode_locked = 1; |
408 | *vpp = tvp; | |
55e303ae | 409 | } |
9bccf70c | 410 | exit: |
2d21ac55 A |
411 | if (dcp) { |
412 | hfs_unlock(dcp); | |
413 | } | |
9bccf70c | 414 | cat_releasedesc(&desc); |
1c79356b A |
415 | return (retval); |
416 | } | |
417 | ||
418 | ||
419 | ||
420 | /* | |
1c79356b A |
421 | * Name caching works as follows: |
422 | * | |
423 | * Names found by directory scans are retained in a cache | |
424 | * for future reference. It is managed LRU, so frequently | |
425 | * used names will hang around. Cache is indexed by hash value | |
426 | * obtained from (vp, name) where vp refers to the directory | |
427 | * containing name. | |
428 | * | |
429 | * If it is a "negative" entry, (i.e. for a name that is known NOT to | |
430 | * exist) the vnode pointer will be NULL. | |
431 | * | |
432 | * Upon reaching the last segment of a path, if the reference | |
433 | * is for DELETE, or NOCACHE is set (rewrite), and the | |
434 | * name is located in the cache, it will be dropped. | |
435 | * | |
1c79356b A |
436 | */ |
437 | ||
55e303ae A |
438 | #define S_IXALL 0000111 |
439 | ||
1c79356b | 440 | int |
91447636 | 441 | hfs_vnop_lookup(struct vnop_lookup_args *ap) |
1c79356b | 442 | { |
91447636 | 443 | struct vnode *dvp = ap->a_dvp; |
9bccf70c A |
444 | struct vnode *vp; |
445 | struct cnode *cp; | |
55e303ae | 446 | struct cnode *dcp; |
6d2010ae | 447 | struct hfsmount *hfsmp; |
1c79356b A |
448 | int error; |
449 | struct vnode **vpp = ap->a_vpp; | |
9bccf70c | 450 | struct componentname *cnp = ap->a_cnp; |
1c79356b | 451 | int flags = cnp->cn_flags; |
91447636 | 452 | int cnode_locked; |
1c79356b | 453 | |
91447636 | 454 | *vpp = NULL; |
55e303ae | 455 | dcp = VTOC(dvp); |
6d2010ae A |
456 | |
457 | hfsmp = VTOHFS(dvp); | |
1c79356b A |
458 | |
459 | /* | |
460 | * Lookup an entry in the cache | |
91447636 A |
461 | * |
462 | * If the lookup succeeds, the vnode is returned in *vpp, | |
463 | * and a status of -1 is returned. | |
464 | * | |
465 | * If the lookup determines that the name does not exist | |
466 | * (negative cacheing), a status of ENOENT is returned. | |
467 | * | |
468 | * If the lookup fails, a status of zero is returned. | |
1c79356b | 469 | */ |
9bccf70c | 470 | error = cache_lookup(dvp, vpp, cnp); |
55e303ae | 471 | if (error != -1) { |
2d21ac55 A |
472 | if ((error == ENOENT) && (cnp->cn_nameiop != CREATE)) |
473 | goto exit; /* found a negative cache entry */ | |
474 | goto lookup; /* did not find it in the cache */ | |
9bccf70c | 475 | } |
91447636 A |
476 | /* |
477 | * We have a name that matched | |
478 | * cache_lookup returns the vp with an iocount reference already taken | |
479 | */ | |
480 | error = 0; | |
9bccf70c | 481 | vp = *vpp; |
6d2010ae A |
482 | cp = VTOC(vp); |
483 | ||
484 | /* We aren't allowed to vend out vp's via lookup to the hidden directory */ | |
485 | if (cp->c_cnid == hfsmp->hfs_private_desc[FILE_HARDLINKS].cd_cnid || | |
486 | cp->c_cnid == hfsmp->hfs_private_desc[DIR_HARDLINKS].cd_cnid) { | |
487 | /* Drop the iocount from cache_lookup */ | |
488 | vnode_put (vp); | |
489 | error = ENOENT; | |
490 | goto exit; | |
491 | } | |
492 | ||
493 | ||
1c79356b A |
494 | /* |
495 | * If this is a hard-link vnode then we need to update | |
9bccf70c A |
496 | * the name (of the link), the parent ID, the cnid, the |
497 | * text encoding and the catalog hint. This enables | |
498 | * getattrlist calls to return the correct link info. | |
1c79356b | 499 | */ |
9bccf70c | 500 | |
91447636 | 501 | if ((flags & ISLASTCN) && (cp->c_flag & C_HARDLINK)) { |
316670eb A |
502 | int stale_link = 0; |
503 | ||
504 | hfs_lock(cp, HFS_FORCE_LOCK); | |
2d21ac55 | 505 | if ((cp->c_parentcnid != dcp->c_cnid) || |
91447636 A |
506 | (bcmp(cnp->cn_nameptr, cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen) != 0)) { |
507 | struct cat_desc desc; | |
316670eb | 508 | struct cat_attr lookup_attr; |
91447636 | 509 | int lockflags; |
91447636 A |
510 | /* |
511 | * Get an updated descriptor | |
512 | */ | |
2d21ac55 | 513 | desc.cd_nameptr = (const u_int8_t *)cnp->cn_nameptr; |
91447636 | 514 | desc.cd_namelen = cnp->cn_namelen; |
2d21ac55 A |
515 | desc.cd_parentcnid = dcp->c_fileid; |
516 | desc.cd_hint = dcp->c_childhint; | |
517 | desc.cd_encoding = 0; | |
518 | desc.cd_cnid = 0; | |
519 | desc.cd_flags = S_ISDIR(cp->c_mode) ? CD_ISDIR : 0; | |
316670eb A |
520 | |
521 | /* | |
522 | * Because lookups call replace_desc to put a new descriptor in | |
523 | * the cnode we are modifying it is possible that this cnode's | |
524 | * descriptor is out of date for the parent ID / name that | |
525 | * we are trying to look up. (It may point to a different hardlink). | |
526 | * | |
527 | * We need to be cautious that when re-supplying the | |
528 | * descriptor below that the results of the catalog lookup | |
529 | * still point to the same raw inode for the hardlink. This would | |
530 | * not be the case if we found something in the cache above but | |
531 | * the vnode it returned no longer has a valid hardlink for the | |
532 | * parent ID/filename combo we are requesting. (This is because | |
533 | * hfs_unlink does not directly trigger namecache removal). | |
534 | * | |
535 | * As a result, before vending out the vnode (and replacing | |
536 | * its descriptor) verify that the fileID is the same by comparing | |
537 | * the in-cnode attributes vs. the one returned from the lookup call | |
538 | * below. If they do not match, treat this lookup as if we never hit | |
539 | * in the cache at all. | |
540 | */ | |
b0d623f7 | 541 | |
91447636 | 542 | lockflags = hfs_systemfile_lock(VTOHFS(dvp), SFL_CATALOG, HFS_SHARED_LOCK); |
316670eb A |
543 | |
544 | error = cat_lookup(VTOHFS(vp), &desc, 0, &desc, &lookup_attr, NULL, NULL); | |
545 | ||
91447636 | 546 | hfs_systemfile_unlock(VTOHFS(dvp), lockflags); |
935ed37a | 547 | |
b0d623f7 | 548 | /* |
316670eb A |
549 | * Note that cat_lookup may fail to find something with the name provided in the |
550 | * stack-based descriptor above. In that case, an ENOENT is a legitimate errno | |
551 | * to be placed in error, which will get returned in the fastpath below. | |
b0d623f7 | 552 | */ |
316670eb A |
553 | if (error == 0) { |
554 | if (lookup_attr.ca_fileid == cp->c_attr.ca_fileid) { | |
555 | /* It still points to the right raw inode. Replacing the descriptor is fine */ | |
556 | replace_desc (cp, &desc); | |
557 | ||
558 | /* | |
559 | * Save the origin info for file and directory hardlinks. Directory hardlinks | |
560 | * need the origin for '..' lookups, and file hardlinks need it to ensure that | |
561 | * competing lookups do not cause us to vend different hardlinks than the ones requested. | |
562 | * We want to restrict saving the cache entries to LOOKUP namei operations, since | |
563 | * we're really doing this to protect getattr. | |
564 | */ | |
565 | if (cnp->cn_nameiop == LOOKUP) { | |
566 | hfs_savelinkorigin(cp, dcp->c_fileid); | |
567 | } | |
568 | } | |
569 | else { | |
570 | /* If the fileID does not match then do NOT replace the descriptor! */ | |
571 | stale_link = 1; | |
572 | } | |
b0d623f7 A |
573 | } |
574 | } | |
316670eb A |
575 | hfs_unlock (cp); |
576 | ||
577 | if (stale_link) { | |
578 | /* | |
579 | * If we had a stale_link, then we need to pretend as though | |
580 | * we never found this vnode and force a lookup through the | |
581 | * traditional path. Drop the iocount acquired through | |
582 | * cache_lookup above and force a cat lookup / getnewvnode | |
583 | */ | |
584 | vnode_put(vp); | |
585 | goto lookup; | |
586 | } | |
587 | ||
588 | if (error) { | |
589 | /* | |
590 | * If the cat_lookup failed then the caller will not expect | |
591 | * a vnode with an iocount on it. | |
592 | */ | |
593 | vnode_put(vp); | |
594 | } | |
2d21ac55 | 595 | |
316670eb A |
596 | } |
597 | goto exit; | |
91447636 A |
598 | |
599 | lookup: | |
1c79356b | 600 | /* |
91447636 A |
601 | * The vnode was not in the name cache or it was stale. |
602 | * | |
603 | * So we need to do a real lookup. | |
1c79356b | 604 | */ |
91447636 | 605 | cnode_locked = 0; |
55e303ae | 606 | |
2d21ac55 | 607 | error = hfs_lookup(dvp, vpp, cnp, &cnode_locked); |
91447636 A |
608 | |
609 | if (cnode_locked) | |
610 | hfs_unlock(VTOC(*vpp)); | |
611 | exit: | |
316670eb A |
612 | { |
613 | uthread_t ut = (struct uthread *)get_bsdthread_info(current_thread()); | |
614 | ||
615 | /* | |
616 | * check to see if we issued any I/O while completing this lookup and | |
617 | * this thread/task is throttleable... if so, throttle now | |
618 | * | |
619 | * this allows us to throttle in between multiple meta data reads that | |
620 | * might result due to looking up a long pathname (since we'll have to | |
621 | * re-enter hfs_vnop_lookup for each component of the pathnam not in | |
622 | * the VFS cache), instead of waiting until the entire path lookup has | |
623 | * completed and throttling at the systemcall return | |
624 | */ | |
625 | if (__improbable(ut->uu_lowpri_window)) { | |
626 | throttle_lowpri_io(TRUE); | |
627 | } | |
628 | } | |
629 | ||
55e303ae | 630 | return (error); |
1c79356b A |
631 | } |
632 | ||
9bccf70c | 633 |