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