]> git.saurik.com Git - apple/xnu.git/blame - bsd/hfs/hfs_lookup.c
xnu-517.12.7.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_lookup.c
CommitLineData
1c79356b 1/*
55e303ae 2 * Copyright (c) 1999-2003 Apple Computer, Inc. All rights reserved.
1c79356b
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
e5568f75
A
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
1c79356b 11 *
e5568f75
A
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
1c79356b
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
e5568f75
A
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
1c79356b
A
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/*
23 * Copyright (c) 1989, 1993
24 * The Regents of the University of California. All rights reserved.
25 * (c) UNIX System Laboratories, Inc.
26 * All or some portions of this file are derived from material licensed
27 * to the University of California by American Telephone and Telegraph
28 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
29 * the permission of UNIX System Laboratories, Inc.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)hfs_lookup.c 1.0
60 * derived from @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
61 *
62 * (c) 1998-1999 Apple Computer, Inc. All Rights Reserved
63 * (c) 1990, 1992 NeXT Computer, Inc. All Rights Reserved
64 *
65 *
66 * hfs_lookup.c -- code to handle directory traversal on HFS/HFS+ volume
1c79356b 67 */
9bccf70c 68#define LEGACY_FORK_NAMES 0
1c79356b
A
69
70#include <sys/param.h>
1c79356b
A
71#include <sys/buf.h>
72#include <sys/file.h>
73#include <sys/mount.h>
74#include <sys/vnode.h>
9bccf70c 75#include <sys/namei.h>
1c79356b
A
76#include <sys/malloc.h>
77#include <sys/paths.h>
78
9bccf70c
A
79#include "hfs.h"
80#include "hfs_catalog.h"
81#include "hfs_cnode.h"
1c79356b 82
1c79356b 83
9bccf70c 84static int forkcomponent(struct componentname *cnp, int *rsrcfork);
1c79356b 85
9bccf70c 86#define _PATH_DATAFORKSPEC "/..namedfork/data"
1c79356b 87
9bccf70c
A
88#ifdef LEGACY_FORK_NAMES
89#define LEGACY_RSRCFORKSPEC "/rsrc"
1c79356b
A
90#endif
91
1c79356b
A
92/*
93 * FROM FREEBSD 3.1
9bccf70c 94 * Convert a component of a pathname into a pointer to a locked cnode.
1c79356b
A
95 * This is a very central and rather complicated routine.
96 * If the file system is not maintained in a strict tree hierarchy,
97 * this can result in a deadlock situation (see comments in code below).
98 *
99 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
100 * on whether the name is to be looked up, created, renamed, or deleted.
101 * When CREATE, RENAME, or DELETE is specified, information usable in
102 * creating, renaming, or deleting a directory entry may be calculated.
103 * Notice that these are the only operations that can affect the directory of the target.
104 *
105 * If flag has LOCKPARENT or'ed into it and the target of the pathname
106 * exists, lookup returns both the target and its parent directory locked.
107 * When creating or renaming and LOCKPARENT is specified, the target may
108 * not be ".". When deleting and LOCKPARENT is specified, the target may
109 * be "."., but the caller must check to ensure it does an vrele and vput
110 * instead of two vputs.
111 *
112 * LOCKPARENT and WANTPARENT actually refer to the parent of the last item,
113 * so if ISLASTCN is not set, they should be ignored. Also they are mutually exclusive, or
114 * WANTPARENT really implies DONTLOCKPARENT. Either of them set means that the calling
115 * routine wants to access the parent of the target, locked or unlocked.
116 *
117 * Keeping the parent locked as long as possible protects from other processes
9bccf70c 118 * looking up the same item, so it has to be locked until the cnode is totally finished
1c79356b
A
119 *
120 * This routine is actually used as VOP_CACHEDLOOKUP method, and the
121 * filesystem employs the generic hfs_cache_lookup() as VOP_LOOKUP
122 * method.
123 *
124 * hfs_cache_lookup() performs the following for us:
125 * check that it is a directory
126 * check accessibility of directory
127 * check for modification attempts on read-only mounts
128 * if name found in cache
129 * if at end of path and deleting or creating
130 * drop it
131 * else
132 * return name.
133 * return VOP_CACHEDLOOKUP()
134 *
135 * Overall outline of hfs_lookup:
136 *
137 * handle simple cases of . and ..
138 * search for name in directory, to found or notfound
139 * notfound:
140 * if creating, return locked directory, leaving info on available slots
141 * else return error
142 * found:
143 * if at end of path and deleting, return information to allow delete
144 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
9bccf70c 145 * cnode and return info to allow rewrite
1c79356b
A
146 * if not at end, add name to cache; if at end and neither creating
147 * nor deleting, add name to cache
148 */
149
150/*
151 * Lookup *nm in directory *pvp, return it in *a_vpp.
152 * **a_vpp is held on exit.
9bccf70c 153 * We create a cnode for the file, but we do NOT open the file here.
1c79356b
A
154
155#% lookup dvp L ? ?
156#% lookup vpp - L -
157
158 IN struct vnode *dvp - Parent node of file;
9bccf70c
A
159 INOUT struct vnode **vpp - node of target file, its a new node if
160 the target vnode did not exist;
1c79356b
A
161 IN struct componentname *cnp - Name of file;
162
163 * When should we lock parent_hp in here ??
164 */
165
9bccf70c 166__private_extern__
1c79356b
A
167int
168hfs_lookup(ap)
169 struct vop_cachedlookup_args /* {
170 struct vnode *a_dvp;
171 struct vnode **a_vpp;
172 struct componentname *a_cnp;
173 } */ *ap;
174{
9bccf70c
A
175 struct vnode *dvp; /* vnode for directory being searched */
176 struct cnode *dcp; /* cnode for directory being searched */
177 struct vnode *tvp; /* target vnode */
178 struct hfsmount *hfsmp;
179 struct componentname *cnp;
180 struct ucred *cred;
181 struct proc *p;
182 int wantrsrc = 0;
183 int forknamelen = 0;
184 int flags;
185 int wantparent;
186 int nameiop;
187 int retval = 0;
188 int isDot;
189 struct cat_desc desc = {0};
190 struct cat_desc cndesc;
191 struct cat_attr attr;
192 struct cat_fork fork;
193 struct vnode **vpp;
194
195 vpp = ap->a_vpp;
196 cnp = ap->a_cnp;
197 dvp = ap->a_dvp;
198 dcp = VTOC(dvp);
199 hfsmp = VTOHFS(dvp);
200 *vpp = NULL;
201 isDot = FALSE;
202 tvp = NULL;
203 nameiop = cnp->cn_nameiop;
204 cred = cnp->cn_cred;
205 p = cnp->cn_proc;
206 flags = cnp->cn_flags;
207 wantparent = flags & (LOCKPARENT|WANTPARENT);
1c79356b
A
208
209 /*
210 * First check to see if it is a . or .., else look it up.
211 */
9bccf70c
A
212 if (flags & ISDOTDOT) { /* Wanting the parent */
213 goto found; /* .. is always defined */
214 } else if ((cnp->cn_nameptr[0] == '.') && (cnp->cn_namelen == 1)) {
1c79356b 215 isDot = TRUE;
9bccf70c
A
216 goto found; /* We always know who we are */
217 } else {
218 /* Check fork suffix to see if we want the resource fork */
219 forknamelen = forkcomponent(cnp, &wantrsrc);
1c79356b 220
9bccf70c
A
221 /* No need to go to catalog if there are no children */
222 if (dcp->c_entries == 0)
223 goto notfound;
1c79356b 224
9bccf70c
A
225 bzero(&cndesc, sizeof(cndesc));
226 cndesc.cd_nameptr = cnp->cn_nameptr;
227 cndesc.cd_namelen = cnp->cn_namelen;
228 cndesc.cd_parentcnid = dcp->c_cnid;
229 cndesc.cd_hint = dcp->c_childhint;
1c79356b 230
9bccf70c
A
231 /* Lock catalog b-tree */
232 retval = hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_SHARED, p);
1c79356b 233 if (retval)
9bccf70c
A
234 goto exit;
235 retval = cat_lookup(hfsmp, &cndesc, wantrsrc, &desc, &attr, &fork);
236
237 if (retval == 0 && S_ISREG(attr.ca_mode) && attr.ca_blocks < fork.cf_blocks)
238 panic("hfs_lookup: bad ca_blocks (too small)");
1c79356b 239
9bccf70c
A
240 /* Unlock catalog b-tree */
241 (void) hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_RELEASE, p);
242 if (retval == 0) {
243 dcp->c_childhint = desc.cd_hint;
244 goto found;
245 }
246notfound:
247 /*
248 * This is a non-existing entry
249 *
250 * If creating, and at end of pathname and current
251 * directory has not been removed, then can consider
252 * allowing file to be created.
253 */
254 if ((nameiop == CREATE || nameiop == RENAME ||
255 (nameiop == DELETE &&
256 (ap->a_cnp->cn_flags & DOWHITEOUT) &&
257 (ap->a_cnp->cn_flags & ISWHITEOUT))) &&
258 (flags & ISLASTCN)) {
259 /*
260 * Access for write is interpreted as allowing
261 * creation of files in the directory.
262 */
263 retval = VOP_ACCESS(dvp, VWRITE, cred, cnp->cn_proc);
b4c24cb9 264 if (retval) {
9bccf70c 265 goto exit;
b4c24cb9 266 }
9bccf70c
A
267
268 cnp->cn_flags |= SAVENAME;
269 if (!(flags & LOCKPARENT))
270 VOP_UNLOCK(dvp, 0, p);
271 retval = EJUSTRETURN;
272 goto exit;
1c79356b 273 }
55e303ae 274
1c79356b
A
275 /*
276 * Insert name into cache (as non-existent) if appropriate.
9bccf70c 277 *
55e303ae 278 * Only done for case-sensitive HFS+ volumes.
1c79356b 279 */
55e303ae
A
280 if ((hfsmp->hfs_flags & HFS_CASE_SENSITIVE) &&
281 (cnp->cn_flags & MAKEENTRY) && nameiop != CREATE)
9bccf70c 282 cache_enter(dvp, *vpp, cnp);
1c79356b 283 retval = ENOENT;
9bccf70c 284 goto exit;
1c79356b 285 }
1c79356b 286
9bccf70c
A
287found:
288 /*
289 * Process any fork specifiers
290 */
291 if (forknamelen && S_ISREG(attr.ca_mode)) {
292 /* fork names are only for lookups */
293 if ((nameiop != LOOKUP) && (nameiop != CREATE)) {
294 retval = EPERM;
295 goto exit;
296 }
297 cnp->cn_consume = forknamelen;
298 flags |= ISLASTCN;
299 } else {
300 wantrsrc = 0;
301 forknamelen = 0;
302 }
303
304 /*
305 * If deleting, and at end of pathname, return
306 * parameters which can be used to remove file.
307 */
308 if (nameiop == DELETE && (flags & ISLASTCN)) {
1c79356b 309 /*
9bccf70c
A
310 * Write access to directory required to delete files.
311 */
312 if ((retval = VOP_ACCESS(dvp, VWRITE, cred, cnp->cn_proc)))
313 goto exit;
314
315 if (isDot) { /* Want to return ourselves */
316 VREF(dvp);
317 *vpp = dvp;
318 goto exit;
319 } else if (flags & ISDOTDOT) {
320 retval = hfs_getcnode(hfsmp, dcp->c_parentcnid,
321 NULL, 0, NULL, NULL, &tvp);
1c79356b 322 if (retval)
9bccf70c
A
323 goto exit;
324 } else {
325 retval = hfs_getcnode(hfsmp, attr.ca_fileid,
326 &desc, wantrsrc, &attr, &fork, &tvp);
327 if (retval)
328 goto exit;
329 }
1c79356b 330
1c79356b 331 /*
9bccf70c
A
332 * If directory is "sticky", then user must own
333 * the directory, or the file in it, else she
334 * may not delete it (unless she's root). This
335 * implements append-only directories.
1c79356b 336 */
9bccf70c
A
337 if ((dcp->c_mode & S_ISTXT) &&
338 (cred->cr_uid != 0) &&
339 (cred->cr_uid != dcp->c_uid) &&
340 (tvp->v_type != VLNK) &&
341 (hfs_owner_rights(hfsmp, VTOC(tvp)->c_uid, cred, p, false))) {
342 vput(tvp);
343 retval = EPERM;
344 goto exit;
345 }
1c79356b 346
9bccf70c
A
347 /*
348 * If this is a link node then we need to save the name
349 * (of the link) so we can delete it from the catalog b-tree.
350 * In this case, hfs_remove will then free the component name.
351 *
352 * DJB - IS THIS STILL NEEDED????
353 */
354 if (tvp && (VTOC(tvp)->c_flag & C_HARDLINK))
1c79356b 355 cnp->cn_flags |= SAVENAME;
9bccf70c
A
356
357 if (!(flags & LOCKPARENT))
358 VOP_UNLOCK(dvp, 0, p);
359 *vpp = tvp;
360 goto exit;
361 }
0b4e3aa0 362
9bccf70c
A
363 /*
364 * If renaming, return the cnode and save the current name.
365 */
366 if (nameiop == RENAME && wantparent && (flags & ISLASTCN)) {
367 if ((retval = VOP_ACCESS(dvp, VWRITE, cred, cnp->cn_proc)) != 0)
368 goto exit;
1c79356b 369 /*
9bccf70c 370 * Careful about locking second cnode.
1c79356b 371 */
9bccf70c
A
372 if (isDot) {
373 retval = EISDIR;
374 goto exit;
375 } else if (flags & ISDOTDOT) {
376 retval = hfs_getcnode(hfsmp, dcp->c_parentcnid,
377 NULL, 0, NULL, NULL, &tvp);
378 if (retval)
379 goto exit;
380 } else {
381 retval = hfs_getcnode(hfsmp, attr.ca_fileid,
382 &desc, wantrsrc, &attr, &fork, &tvp);
383 if (retval)
384 goto exit;
1c79356b 385 }
9bccf70c
A
386 cnp->cn_flags |= SAVENAME;
387 if (!(flags & LOCKPARENT))
388 VOP_UNLOCK(dvp, 0, p);
389 *vpp = tvp;
390 goto exit;
391 }
392
393 /*
394 * We must get the target cnode before unlocking
395 * the directory to insure that the cnode will not be removed
396 * before we get it. We prevent deadlock by always fetching
397 * cnodes from the root, moving down the directory tree. Thus
398 * when following backward pointers ".." we must unlock the
399 * parent directory before getting the requested directory.
400 * There is a potential race condition here if both the current
401 * and parent directories are removed before the VFS_VGET for the
402 * cnode associated with ".." returns. We hope that this occurs
403 * infrequently since we cannot avoid this race condition without
404 * implementing a sophisticated deadlock detection algorithm.
405 */
406 if (flags & ISDOTDOT) {
407 VOP_UNLOCK(dvp, 0, p); /* race to get the cnode */
408 retval = hfs_getcnode(hfsmp, dcp->c_parentcnid,
409 NULL, 0, NULL, NULL, &tvp);
410 if (retval) {
411 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
412 goto exit;
1c79356b 413 }
9bccf70c
A
414 if ((flags & LOCKPARENT) && (flags & ISLASTCN) && (dvp != tvp) &&
415 (retval = vn_lock(dvp, LK_EXCLUSIVE, p))) {
416 vput(tvp);
417 goto exit;
1c79356b 418 }
9bccf70c
A
419 *vpp = tvp;
420 } else if (isDot) {
421 VREF(dvp); /* we want ourself, ie "." */
422 *vpp = dvp;
423 } else {
424 int type = (attr.ca_mode & S_IFMT);
1c79356b 425
9bccf70c
A
426 if (!(flags & ISLASTCN) && type != S_IFDIR && type != S_IFLNK) {
427 retval = ENOTDIR;
428 goto exit;
1c79356b 429 }
1c79356b 430
9bccf70c
A
431 retval = hfs_getcnode(hfsmp, attr.ca_fileid,
432 &desc, wantrsrc, &attr, &fork, &tvp);
433 if (retval)
434 goto exit;
1c79356b 435
9bccf70c
A
436 if (!(flags & LOCKPARENT) || !(flags & ISLASTCN))
437 VOP_UNLOCK(dvp, 0, p);
438 *vpp = tvp;
439 }
1c79356b 440
9bccf70c
A
441 /*
442 * Insert name in cache if appropriate.
443 * - "." and ".." are not cached.
444 * - Resource fork names are not cached.
445 * - Names with composed chars are not cached.
446 */
447 if ((cnp->cn_flags & MAKEENTRY)
448 && !isDot
449 && !(flags & ISDOTDOT)
450 && !wantrsrc
451 && (cnp->cn_namelen == VTOC(*vpp)->c_desc.cd_namelen)) {
452 cache_enter(dvp, *vpp, cnp);
453 }
1c79356b 454
55e303ae
A
455
456 //
457 // have to patch up the resource fork name because
458 // it won't happen properly in the layers above us.
459 //
460 if (wantrsrc) {
461 if (VTOC(*vpp)->c_vp == NULL) {
462 if (VNAME(*vpp) == NULL) {
463 VNAME(*vpp) = add_name(cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, 0);
464 }
465 if (VPARENT(*vpp) == NULL) {
466 vget(dvp, 0, p);
467 VPARENT(*vpp) = dvp;
468 }
469 } else {
470 if (VNAME(*vpp) == NULL) {
471 // the +1/-2 thing is to skip the leading "/" on the rsrc fork spec
472 // and to not count the trailing null byte at the end of the string.
473 VNAME(*vpp) = add_name(_PATH_RSRCFORKSPEC+1, sizeof(_PATH_RSRCFORKSPEC)-2, 0, 0);
474 }
475 if (VPARENT(*vpp) == NULL && *vpp != VTOC(*vpp)->c_vp) {
476 VPARENT(*vpp) = VTOC(*vpp)->c_vp;
477 VTOC(*vpp)->c_flag |= C_VPREFHELD;
478 vget(VTOC(*vpp)->c_vp, 0, p);
479 }
480 }
481 }
482
9bccf70c
A
483exit:
484 cat_releasedesc(&desc);
1c79356b
A
485 return (retval);
486}
487
488
489
490/*
491 * Based on vn_cache_lookup (which is vfs_cache_lookup in FreeBSD 3.1)
492 *
493 * Name caching works as follows:
494 *
495 * Names found by directory scans are retained in a cache
496 * for future reference. It is managed LRU, so frequently
497 * used names will hang around. Cache is indexed by hash value
498 * obtained from (vp, name) where vp refers to the directory
499 * containing name.
500 *
501 * If it is a "negative" entry, (i.e. for a name that is known NOT to
502 * exist) the vnode pointer will be NULL.
503 *
504 * Upon reaching the last segment of a path, if the reference
505 * is for DELETE, or NOCACHE is set (rewrite), and the
506 * name is located in the cache, it will be dropped.
507 *
1c79356b
A
508 */
509
55e303ae
A
510#define S_IXALL 0000111
511
9bccf70c 512__private_extern__
1c79356b
A
513int
514hfs_cache_lookup(ap)
515 struct vop_lookup_args /* {
516 struct vnode *a_dvp;
517 struct vnode **a_vpp;
518 struct componentname *a_cnp;
519 } */ *ap;
520{
9bccf70c
A
521 struct vnode *dvp;
522 struct vnode *vp;
523 struct cnode *cp;
55e303ae 524 struct cnode *dcp;
1c79356b
A
525 int lockparent;
526 int error;
527 struct vnode **vpp = ap->a_vpp;
9bccf70c 528 struct componentname *cnp = ap->a_cnp;
1c79356b 529 int flags = cnp->cn_flags;
9bccf70c
A
530 struct proc *p = cnp->cn_proc;
531 u_long vpid; /* capability number of vnode */
1c79356b 532
9bccf70c 533 dvp = ap->a_dvp;
1c79356b
A
534 lockparent = flags & LOCKPARENT;
535
9bccf70c
A
536 /*
537 * Check accessiblity of directory.
538 */
539 if (dvp->v_type != VDIR)
0b4e3aa0 540 return (ENOTDIR);
9bccf70c 541 if ((flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
55e303ae
A
542 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
543 error = EROFS;
544 goto err_exit;
545 }
546 dcp = VTOC(dvp);
1c79356b 547
55e303ae
A
548 if (((dcp->c_mode & S_IXALL) != S_IXALL) && (cnp->cn_cred->cr_uid != 0)) {
549 if ((error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, p))) {
550 goto err_exit;
551 }
552 }
1c79356b
A
553 /*
554 * Lookup an entry in the cache
555 * If the lookup succeeds, the vnode is returned in *vpp, and a status of -1 is
556 * returned. If the lookup determines that the name does not exist
557 * (negative cacheing), a status of ENOENT is returned. If the lookup
558 * fails, a status of zero is returned.
559 */
9bccf70c 560 error = cache_lookup(dvp, vpp, cnp);
55e303ae
A
561 if (error != -1) {
562 if (error == 0) { /* Unsuccessfull */
563 goto lookup;
564 }
565
566 if (error == ENOENT) {
567 goto err_exit;
568 }
9bccf70c 569 }
9bccf70c
A
570 /* We have a name that matched */
571 vp = *vpp;
572 vpid = vp->v_id;
573
1c79356b
A
574 /*
575 * If this is a hard-link vnode then we need to update
9bccf70c
A
576 * the name (of the link), the parent ID, the cnid, the
577 * text encoding and the catalog hint. This enables
578 * getattrlist calls to return the correct link info.
1c79356b 579 */
9bccf70c
A
580 cp = VTOC(vp);
581 if ((flags & ISLASTCN) && (cp->c_flag & C_HARDLINK) &&
582 ((cp->c_parentcnid != VTOC(ap->a_dvp)->c_cnid) ||
583 (bcmp(cnp->cn_nameptr, cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen) != 0))) {
584
585 struct cat_desc desc;
586
587 /*
588 * Get an updated descriptor
589 */
590 bzero(&desc, sizeof(desc));
591 desc.cd_nameptr = cnp->cn_nameptr;
592 desc.cd_namelen = cnp->cn_namelen;
593 desc.cd_parentcnid = VTOC(ap->a_dvp)->c_cnid;
594 desc.cd_hint = VTOC(ap->a_dvp)->c_childhint;
595 if (cat_lookup(VTOHFS(vp), &desc, 0, &desc, NULL, NULL) == 0)
596 replace_desc(cp, &desc);
1c79356b 597 }
1c79356b 598
9bccf70c
A
599 if (dvp == vp) { /* lookup on "." */
600 VREF(vp);
1c79356b
A
601 error = 0;
602 } else if (flags & ISDOTDOT) {
603 /*
604 * Carefull on the locking policy,
605 * remember we always lock from parent to child, so have
606 * to release lock on child before trying to lock parent
607 * then regain lock if needed
608 */
9bccf70c
A
609 VOP_UNLOCK(dvp, 0, p);
610 error = vget(vp, LK_EXCLUSIVE, p);
1c79356b 611 if (!error && lockparent && (flags & ISLASTCN))
9bccf70c 612 error = vn_lock(dvp, LK_EXCLUSIVE, p);
0b4e3aa0 613 } else {
9bccf70c
A
614 if ((flags & ISLASTCN) == 0 && vp->v_type == VREG) {
615 int wantrsrc = 0;
616
617 cnp->cn_consume = forkcomponent(cnp, &wantrsrc);
55e303ae
A
618 if (cnp->cn_consume) {
619 flags |= ISLASTCN;
620 /* Fork names are only for lookups */
621 if (cnp->cn_nameiop != LOOKUP &&
622 cnp->cn_nameiop != CREATE) {
623 error = EPERM;
624
625 goto err_exit;
626 }
627 }
628
629 if (wantrsrc) {
630 /* Use cnode's rsrcfork vnode (if available) */
631 if (cp->c_rsrc_vp != NULL) {
632 *vpp = vp = cp->c_rsrc_vp;
633 if (VNAME(vp) == NULL) {
634 // the +1/-2 thing is to skip the leading "/" on the rsrc fork spec
635 // and to not count the trailing null byte at the end of the string.
636 VNAME(vp) = add_name(_PATH_RSRCFORKSPEC+1, sizeof(_PATH_RSRCFORKSPEC)-2, 0, 0);
637 }
638 if (VPARENT(vp) == NULL) {
639 vget(cp->c_vp, 0, p);
640 VPARENT(vp) = cp->c_vp;
641 }
642 vpid = vp->v_id;
643 } else {
644 goto lookup;
645 }
646 }
647 }
648 error = vget(vp, 0, p);
649 if (error == 0) {
650 if (VTOC(vp) == NULL || vp->v_data != (void *)cp) {
651 panic("hfs: cache lookup: my cnode disappeared/went bad! vp 0x%x 0x%x 0x%x\n",
652 vp, vp->v_data, cp);
653 }
654 if (cnp->cn_nameiop == LOOKUP &&
655 (!(flags & ISLASTCN) || (flags & SHAREDLEAF)))
656 error = lockmgr(&VTOC(vp)->c_lock, LK_SHARED, NULL, p);
657 else
658 error = lockmgr(&VTOC(vp)->c_lock, LK_EXCLUSIVE, NULL, p);
659 }
660 if (!lockparent || error || !(flags & ISLASTCN)) {
661 (void) lockmgr(&dcp->c_lock, LK_RELEASE, NULL, p);
9bccf70c 662 }
0b4e3aa0 663 }
1c79356b
A
664 /*
665 * Check that the capability number did not change
666 * while we were waiting for the lock.
667 */
668 if (!error) {
9bccf70c
A
669 if (vpid == vp->v_id)
670 return (0);
1c79356b
A
671 /*
672 * The above is the NORMAL exit, after this point is an error
673 * condition.
674 */
9bccf70c
A
675 vput(vp);
676 if (lockparent && (dvp != vp) && (flags & ISLASTCN))
677 VOP_UNLOCK(dvp, 0, p);
1c79356b 678 }
0b4e3aa0 679
9bccf70c
A
680 if ((error = vn_lock(dvp, LK_EXCLUSIVE, p)))
681 return (error);
55e303ae 682lookup:
1c79356b 683 return (hfs_lookup(ap));
55e303ae
A
684
685err_exit:
686 *vpp = NULL;
687 return (error);
1c79356b
A
688}
689
9bccf70c 690
1c79356b 691/*
9bccf70c
A
692 * forkcomponent - look for a fork suffix in the component name
693 *
1c79356b 694 */
9bccf70c
A
695static int
696forkcomponent(struct componentname *cnp, int *rsrcfork)
1c79356b 697{
9bccf70c
A
698 char *suffix = cnp->cn_nameptr + cnp->cn_namelen;
699 int consume = 0;
1c79356b 700
9bccf70c
A
701 *rsrcfork = 0;
702 if (*suffix == '\0')
703 return (0);
704 /*
705 * There are only 3 valid fork suffixes:
706 * "/..namedfork/rsrc"
707 * "/..namedfork/data"
708 * "/rsrc" (legacy)
709 */
710 if (bcmp(suffix, _PATH_RSRCFORKSPEC, sizeof(_PATH_RSRCFORKSPEC)) == 0) {
711 consume = sizeof(_PATH_RSRCFORKSPEC) - 1;
712 *rsrcfork = 1;
713 } else if (bcmp(suffix, _PATH_DATAFORKSPEC, sizeof(_PATH_DATAFORKSPEC)) == 0) {
714 consume = sizeof(_PATH_DATAFORKSPEC) - 1;
1c79356b
A
715 }
716
9bccf70c
A
717#ifdef LEGACY_FORK_NAMES
718 else if (bcmp(suffix, LEGACY_RSRCFORKSPEC, sizeof(LEGACY_RSRCFORKSPEC)) == 0) {
719 consume = sizeof(LEGACY_RSRCFORKSPEC) - 1;
720 *rsrcfork = 1;
1c79356b 721 }
9bccf70c
A
722#endif
723 return (consume);
1c79356b
A
724}
725