]> git.saurik.com Git - apple/xnu.git/blame - bsd/hfs/hfs_lookup.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_lookup.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 1999-2005 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
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.
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
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>
80#include <sys/paths.h>
91447636
A
81#include <sys/kdebug.h>
82#include <sys/kauth.h>
1c79356b 83
9bccf70c
A
84#include "hfs.h"
85#include "hfs_catalog.h"
86#include "hfs_cnode.h"
1c79356b 87
91447636 88#define LEGACY_FORK_NAMES 1
1c79356b 89
9bccf70c 90static int forkcomponent(struct componentname *cnp, int *rsrcfork);
1c79356b 91
9bccf70c 92#define _PATH_DATAFORKSPEC "/..namedfork/data"
1c79356b 93
91447636 94#if LEGACY_FORK_NAMES
9bccf70c 95#define LEGACY_RSRCFORKSPEC "/rsrc"
1c79356b
A
96#endif
97
1c79356b
A
98/*
99 * FROM FREEBSD 3.1
9bccf70c 100 * Convert a component of a pathname into a pointer to a locked cnode.
1c79356b
A
101 * This is a very central and rather complicated routine.
102 * If the file system is not maintained in a strict tree hierarchy,
103 * this can result in a deadlock situation (see comments in code below).
104 *
105 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
106 * on whether the name is to be looked up, created, renamed, or deleted.
107 * When CREATE, RENAME, or DELETE is specified, information usable in
108 * creating, renaming, or deleting a directory entry may be calculated.
109 * Notice that these are the only operations that can affect the directory of the target.
110 *
1c79356b
A
111 * LOCKPARENT and WANTPARENT actually refer to the parent of the last item,
112 * so if ISLASTCN is not set, they should be ignored. Also they are mutually exclusive, or
113 * WANTPARENT really implies DONTLOCKPARENT. Either of them set means that the calling
114 * routine wants to access the parent of the target, locked or unlocked.
115 *
116 * Keeping the parent locked as long as possible protects from other processes
9bccf70c 117 * looking up the same item, so it has to be locked until the cnode is totally finished
1c79356b 118 *
1c79356b
A
119 * hfs_cache_lookup() performs the following for us:
120 * check that it is a directory
121 * check accessibility of directory
122 * check for modification attempts on read-only mounts
123 * if name found in cache
124 * if at end of path and deleting or creating
125 * drop it
126 * else
127 * return name.
91447636 128 * return hfs_lookup()
1c79356b
A
129 *
130 * Overall outline of hfs_lookup:
131 *
132 * handle simple cases of . and ..
133 * search for name in directory, to found or notfound
134 * notfound:
135 * if creating, return locked directory, leaving info on available slots
136 * else return error
137 * found:
138 * if at end of path and deleting, return information to allow delete
139 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
9bccf70c 140 * cnode and return info to allow rewrite
1c79356b
A
141 * if not at end, add name to cache; if at end and neither creating
142 * nor deleting, add name to cache
143 */
144
91447636 145
1c79356b 146/*
91447636
A
147 * Lookup *cnp in directory *dvp, return it in *vpp.
148 * **vpp is held on exit.
9bccf70c 149 * We create a cnode for the file, but we do NOT open the file here.
1c79356b
A
150
151#% lookup dvp L ? ?
152#% lookup vpp - L -
153
154 IN struct vnode *dvp - Parent node of file;
9bccf70c
A
155 INOUT struct vnode **vpp - node of target file, its a new node if
156 the target vnode did not exist;
1c79356b
A
157 IN struct componentname *cnp - Name of file;
158
159 * When should we lock parent_hp in here ??
160 */
91447636
A
161static int
162hfs_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, vfs_context_t context, int *cnode_locked)
1c79356b 163{
9bccf70c
A
164 struct cnode *dcp; /* cnode for directory being searched */
165 struct vnode *tvp; /* target vnode */
166 struct hfsmount *hfsmp;
91447636 167 kauth_cred_t cred;
9bccf70c
A
168 struct proc *p;
169 int wantrsrc = 0;
170 int forknamelen = 0;
171 int flags;
9bccf70c
A
172 int nameiop;
173 int retval = 0;
174 int isDot;
91447636 175 struct cat_desc desc;
9bccf70c
A
176 struct cat_desc cndesc;
177 struct cat_attr attr;
178 struct cat_fork fork;
91447636 179 int lockflags;
9bccf70c 180
9bccf70c
A
181 dcp = VTOC(dvp);
182 hfsmp = VTOHFS(dvp);
183 *vpp = NULL;
91447636 184 *cnode_locked = 0;
9bccf70c
A
185 isDot = FALSE;
186 tvp = NULL;
187 nameiop = cnp->cn_nameiop;
9bccf70c 188 flags = cnp->cn_flags;
91447636
A
189 bzero(&desc, sizeof(desc));
190
191 cred = vfs_context_ucred(context);
192 p = vfs_context_proc(context);
1c79356b
A
193
194 /*
195 * First check to see if it is a . or .., else look it up.
196 */
9bccf70c 197 if (flags & ISDOTDOT) { /* Wanting the parent */
91447636 198 cnp->cn_flags &= ~MAKEENTRY;
9bccf70c
A
199 goto found; /* .. is always defined */
200 } else if ((cnp->cn_nameptr[0] == '.') && (cnp->cn_namelen == 1)) {
1c79356b 201 isDot = TRUE;
91447636 202 cnp->cn_flags &= ~MAKEENTRY;
9bccf70c
A
203 goto found; /* We always know who we are */
204 } else {
205 /* Check fork suffix to see if we want the resource fork */
206 forknamelen = forkcomponent(cnp, &wantrsrc);
91447636
A
207
208 /* Resource fork names are not cached. */
209 if (wantrsrc)
210 cnp->cn_flags &= ~MAKEENTRY;
211
212 if (hfs_lock(dcp, HFS_EXCLUSIVE_LOCK) != 0) {
213 goto notfound;
214 }
1c79356b 215
9bccf70c 216 /* No need to go to catalog if there are no children */
91447636
A
217 if (dcp->c_entries == 0) {
218 hfs_unlock(dcp);
9bccf70c 219 goto notfound;
91447636 220 }
1c79356b 221
9bccf70c
A
222 bzero(&cndesc, sizeof(cndesc));
223 cndesc.cd_nameptr = cnp->cn_nameptr;
224 cndesc.cd_namelen = cnp->cn_namelen;
225 cndesc.cd_parentcnid = dcp->c_cnid;
226 cndesc.cd_hint = dcp->c_childhint;
1c79356b 227
91447636
A
228 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
229
230 retval = cat_lookup(hfsmp, &cndesc, wantrsrc, &desc, &attr, &fork, NULL);
9bccf70c 231
91447636
A
232 hfs_systemfile_unlock(hfsmp, lockflags);
233
9bccf70c
A
234 if (retval == 0) {
235 dcp->c_childhint = desc.cd_hint;
91447636 236 hfs_unlock(dcp);
9bccf70c
A
237 goto found;
238 }
91447636 239 hfs_unlock(dcp);
9bccf70c 240notfound:
91447636
A
241 /* ENAMETOOLONG supersedes other errors */
242 if (((nameiop != CREATE) && (nameiop != RENAME)) &&
243 (retval != ENAMETOOLONG) &&
244 (cnp->cn_namelen > kHFSPlusMaxFileNameChars)) {
245 retval = ENAMETOOLONG;
246 } else if (retval == 0) {
247 retval = ENOENT;
248 }
9bccf70c
A
249 /*
250 * This is a non-existing entry
251 *
252 * If creating, and at end of pathname and current
253 * directory has not been removed, then can consider
254 * allowing file to be created.
255 */
256 if ((nameiop == CREATE || nameiop == RENAME ||
257 (nameiop == DELETE &&
91447636
A
258 (cnp->cn_flags & DOWHITEOUT) &&
259 (cnp->cn_flags & ISWHITEOUT))) &&
260 (flags & ISLASTCN) &&
261 (retval == ENOENT)) {
9bccf70c
A
262 retval = EJUSTRETURN;
263 goto exit;
1c79356b 264 }
1c79356b
A
265 /*
266 * Insert name into cache (as non-existent) if appropriate.
9bccf70c 267 *
55e303ae 268 * Only done for case-sensitive HFS+ volumes.
1c79356b 269 */
91447636
A
270 if ((retval == ENOENT) &&
271 (hfsmp->hfs_flags & HFS_CASE_SENSITIVE) &&
272 (cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) {
273 cache_enter(dvp, NULL, cnp);
274 }
9bccf70c 275 goto exit;
1c79356b 276 }
1c79356b 277
9bccf70c
A
278found:
279 /*
280 * Process any fork specifiers
281 */
282 if (forknamelen && S_ISREG(attr.ca_mode)) {
283 /* fork names are only for lookups */
284 if ((nameiop != LOOKUP) && (nameiop != CREATE)) {
285 retval = EPERM;
286 goto exit;
287 }
288 cnp->cn_consume = forknamelen;
289 flags |= ISLASTCN;
290 } else {
291 wantrsrc = 0;
292 forknamelen = 0;
293 }
91447636
A
294 if (flags & ISLASTCN) {
295 switch(nameiop) {
296 case DELETE:
297 cnp->cn_flags &= ~MAKEENTRY;
298 break;
299
300 case RENAME:
301 cnp->cn_flags &= ~MAKEENTRY;
302 if (isDot) {
303 retval = EISDIR;
9bccf70c 304 goto exit;
91447636
A
305 }
306 break;
1c79356b 307 }
91447636 308 }
9bccf70c 309
91447636
A
310 if (isDot) {
311 if ((retval = vnode_get(dvp)))
9bccf70c 312 goto exit;
91447636
A
313 *vpp = dvp;
314 } else if (flags & ISDOTDOT) {
315 if ((retval = hfs_vget(hfsmp, dcp->c_parentcnid, &tvp, 0)))
9bccf70c 316 goto exit;
91447636 317 *cnode_locked = 1;
9bccf70c 318 *vpp = tvp;
9bccf70c
A
319 } else {
320 int type = (attr.ca_mode & S_IFMT);
1c79356b 321
91447636 322 if (!(flags & ISLASTCN) && (type != S_IFDIR) && (type != S_IFLNK)) {
9bccf70c
A
323 retval = ENOTDIR;
324 goto exit;
1c79356b 325 }
1c79356b 326
91447636
A
327 /* Names with composed chars are not cached. */
328 if (cnp->cn_namelen != desc.cd_namelen)
329 cnp->cn_flags &= ~MAKEENTRY;
1c79356b 330
91447636
A
331 /* Resource fork vnode names include the fork specifier. */
332 if (wantrsrc && (flags & ISLASTCN))
333 cnp->cn_namelen += forknamelen;
1c79356b 334
91447636 335 retval = hfs_getnewvnode(hfsmp, dvp, cnp, &desc, wantrsrc, &attr, &fork, &tvp);
1c79356b 336
91447636
A
337 if (wantrsrc && (flags & ISLASTCN))
338 cnp->cn_namelen -= forknamelen;
55e303ae 339
91447636
A
340 if (retval)
341 goto exit;
342 *cnode_locked = 1;
343 *vpp = tvp;
55e303ae 344 }
9bccf70c
A
345exit:
346 cat_releasedesc(&desc);
1c79356b
A
347 return (retval);
348}
349
350
351
352/*
1c79356b
A
353 * Name caching works as follows:
354 *
355 * Names found by directory scans are retained in a cache
356 * for future reference. It is managed LRU, so frequently
357 * used names will hang around. Cache is indexed by hash value
358 * obtained from (vp, name) where vp refers to the directory
359 * containing name.
360 *
361 * If it is a "negative" entry, (i.e. for a name that is known NOT to
362 * exist) the vnode pointer will be NULL.
363 *
364 * Upon reaching the last segment of a path, if the reference
365 * is for DELETE, or NOCACHE is set (rewrite), and the
366 * name is located in the cache, it will be dropped.
367 *
1c79356b
A
368 */
369
55e303ae
A
370#define S_IXALL 0000111
371
9bccf70c 372__private_extern__
1c79356b 373int
91447636 374hfs_vnop_lookup(struct vnop_lookup_args *ap)
1c79356b 375{
91447636 376 struct vnode *dvp = ap->a_dvp;
9bccf70c
A
377 struct vnode *vp;
378 struct cnode *cp;
55e303ae 379 struct cnode *dcp;
1c79356b
A
380 int error;
381 struct vnode **vpp = ap->a_vpp;
9bccf70c 382 struct componentname *cnp = ap->a_cnp;
1c79356b 383 int flags = cnp->cn_flags;
91447636 384 int cnode_locked;
1c79356b 385
91447636 386 *vpp = NULL;
55e303ae 387 dcp = VTOC(dvp);
1c79356b
A
388
389 /*
390 * Lookup an entry in the cache
91447636
A
391 *
392 * If the lookup succeeds, the vnode is returned in *vpp,
393 * and a status of -1 is returned.
394 *
395 * If the lookup determines that the name does not exist
396 * (negative cacheing), a status of ENOENT is returned.
397 *
398 * If the lookup fails, a status of zero is returned.
1c79356b 399 */
9bccf70c 400 error = cache_lookup(dvp, vpp, cnp);
55e303ae 401 if (error != -1) {
91447636
A
402 if (error == ENOENT) /* found a negative cache entry */
403 goto exit;
404 goto lookup; /* did not find it in the cache */
9bccf70c 405 }
91447636
A
406 /*
407 * We have a name that matched
408 * cache_lookup returns the vp with an iocount reference already taken
409 */
410 error = 0;
9bccf70c 411 vp = *vpp;
9bccf70c 412
1c79356b
A
413 /*
414 * If this is a hard-link vnode then we need to update
9bccf70c
A
415 * the name (of the link), the parent ID, the cnid, the
416 * text encoding and the catalog hint. This enables
417 * getattrlist calls to return the correct link info.
1c79356b 418 */
9bccf70c 419 cp = VTOC(vp);
9bccf70c 420
91447636
A
421 if ((flags & ISLASTCN) && (cp->c_flag & C_HARDLINK)) {
422 hfs_lock(cp, HFS_FORCE_LOCK);
423 if ((cp->c_parentcnid != VTOC(dvp)->c_cnid) ||
424 (bcmp(cnp->cn_nameptr, cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen) != 0)) {
425 struct cat_desc desc;
426 int lockflags;
1c79356b 427
91447636
A
428 /*
429 * Get an updated descriptor
430 */
431 bzero(&desc, sizeof(desc));
432 desc.cd_nameptr = cnp->cn_nameptr;
433 desc.cd_namelen = cnp->cn_namelen;
434 desc.cd_parentcnid = VTOC(dvp)->c_cnid;
435 desc.cd_hint = VTOC(dvp)->c_childhint;
436
437 lockflags = hfs_systemfile_lock(VTOHFS(dvp), SFL_CATALOG, HFS_SHARED_LOCK);
438 if (cat_lookup(VTOHFS(vp), &desc, 0, &desc, NULL, NULL, NULL) == 0)
439 replace_desc(cp, &desc);
440 hfs_systemfile_unlock(VTOHFS(dvp), lockflags);
441 }
442 hfs_unlock(cp);
443 }
444 if (dvp != vp && !(flags & ISDOTDOT)) {
445 if ((flags & ISLASTCN) == 0 && vnode_isreg(vp)) {
9bccf70c
A
446 int wantrsrc = 0;
447
448 cnp->cn_consume = forkcomponent(cnp, &wantrsrc);
55e303ae
A
449 if (cnp->cn_consume) {
450 flags |= ISLASTCN;
451 /* Fork names are only for lookups */
452 if (cnp->cn_nameiop != LOOKUP &&
453 cnp->cn_nameiop != CREATE) {
91447636 454 vnode_put(vp);
55e303ae 455 error = EPERM;
91447636 456 goto exit;
55e303ae
A
457 }
458 }
91447636
A
459 /*
460 * Use cnode's rsrcfork vnode if possible.
461 */
55e303ae 462 if (wantrsrc) {
91447636
A
463 int vid;
464
465 *vpp = NULL;
466
467 if (cp->c_rsrc_vp == NULL) {
468 vnode_put(vp);
469 goto lookup;
55e303ae 470 }
91447636
A
471 vid = vnode_vid(cp->c_rsrc_vp);
472
473 error = vnode_getwithvid(cp->c_rsrc_vp, vid);
474 if (error) {
475 vnode_put(vp);
476 goto lookup;
477 }
478 *vpp = cp->c_rsrc_vp;
479 vnode_put(vp);
480 vp = *vpp;
55e303ae
A
481 }
482 }
0b4e3aa0 483 }
91447636
A
484 return (error);
485
486lookup:
1c79356b 487 /*
91447636
A
488 * The vnode was not in the name cache or it was stale.
489 *
490 * So we need to do a real lookup.
1c79356b 491 */
91447636 492 cnode_locked = 0;
55e303ae 493
91447636
A
494 error = hfs_lookup(dvp, vpp, cnp, ap->a_context, &cnode_locked);
495
496 if (cnode_locked)
497 hfs_unlock(VTOC(*vpp));
498exit:
55e303ae 499 return (error);
1c79356b
A
500}
501
9bccf70c 502
1c79356b 503/*
9bccf70c
A
504 * forkcomponent - look for a fork suffix in the component name
505 *
1c79356b 506 */
9bccf70c
A
507static int
508forkcomponent(struct componentname *cnp, int *rsrcfork)
1c79356b 509{
9bccf70c
A
510 char *suffix = cnp->cn_nameptr + cnp->cn_namelen;
511 int consume = 0;
1c79356b 512
9bccf70c
A
513 *rsrcfork = 0;
514 if (*suffix == '\0')
515 return (0);
516 /*
517 * There are only 3 valid fork suffixes:
518 * "/..namedfork/rsrc"
519 * "/..namedfork/data"
520 * "/rsrc" (legacy)
521 */
522 if (bcmp(suffix, _PATH_RSRCFORKSPEC, sizeof(_PATH_RSRCFORKSPEC)) == 0) {
523 consume = sizeof(_PATH_RSRCFORKSPEC) - 1;
524 *rsrcfork = 1;
525 } else if (bcmp(suffix, _PATH_DATAFORKSPEC, sizeof(_PATH_DATAFORKSPEC)) == 0) {
526 consume = sizeof(_PATH_DATAFORKSPEC) - 1;
1c79356b
A
527 }
528
91447636 529#if LEGACY_FORK_NAMES
9bccf70c
A
530 else if (bcmp(suffix, LEGACY_RSRCFORKSPEC, sizeof(LEGACY_RSRCFORKSPEC)) == 0) {
531 consume = sizeof(LEGACY_RSRCFORKSPEC) - 1;
532 *rsrcfork = 1;
91447636 533 printf("HFS: /rsrc paths are deprecated (%s)\n", cnp->cn_nameptr);
1c79356b 534 }
9bccf70c
A
535#endif
536 return (consume);
1c79356b
A
537}
538