]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_lookup.c
2 * Copyright (c) 1999-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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
65 * @(#)hfs_lookup.c 1.0
66 * derived from @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
68 * (c) 1998-1999 Apple Computer, Inc. All Rights Reserved
69 * (c) 1990, 1992 NeXT Computer, Inc. All Rights Reserved
72 * hfs_lookup.c -- code to handle directory traversal on HFS/HFS+ volume
75 #include <sys/param.h>
77 #include <sys/mount.h>
78 #include <sys/vnode.h>
79 #include <sys/malloc.h>
80 #include <sys/paths.h>
81 #include <sys/kdebug.h>
82 #include <sys/kauth.h>
85 #include "hfs_catalog.h"
86 #include "hfs_cnode.h"
88 #define LEGACY_FORK_NAMES 1
90 static int forkcomponent(struct componentname
*cnp
, int *rsrcfork
);
92 #define _PATH_DATAFORKSPEC "/..namedfork/data"
95 #define LEGACY_RSRCFORKSPEC "/rsrc"
100 * Convert a component of a pathname into a pointer to a locked cnode.
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).
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.
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.
116 * Keeping the parent locked as long as possible protects from other processes
117 * looking up the same item, so it has to be locked until the cnode is totally finished
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
128 * return hfs_lookup()
130 * Overall outline of hfs_lookup:
132 * handle simple cases of . and ..
133 * search for name in directory, to found or notfound
135 * if creating, return locked directory, leaving info on available slots
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
140 * cnode and return info to allow rewrite
141 * if not at end, add name to cache; if at end and neither creating
142 * nor deleting, add name to cache
147 * Lookup *cnp in directory *dvp, return it in *vpp.
148 * **vpp is held on exit.
149 * We create a cnode for the file, but we do NOT open the file here.
154 IN struct vnode *dvp - Parent node of file;
155 INOUT struct vnode **vpp - node of target file, its a new node if
156 the target vnode did not exist;
157 IN struct componentname *cnp - Name of file;
159 * When should we lock parent_hp in here ??
162 hfs_lookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
, vfs_context_t context
, int *cnode_locked
)
164 struct cnode
*dcp
; /* cnode for directory being searched */
165 struct vnode
*tvp
; /* target vnode */
166 struct hfsmount
*hfsmp
;
175 struct cat_desc desc
;
176 struct cat_desc cndesc
;
177 struct cat_attr attr
;
178 struct cat_fork fork
;
187 nameiop
= cnp
->cn_nameiop
;
188 flags
= cnp
->cn_flags
;
189 bzero(&desc
, sizeof(desc
));
191 cred
= vfs_context_ucred(context
);
192 p
= vfs_context_proc(context
);
195 * First check to see if it is a . or .., else look it up.
197 if (flags
& ISDOTDOT
) { /* Wanting the parent */
198 cnp
->cn_flags
&= ~MAKEENTRY
;
199 goto found
; /* .. is always defined */
200 } else if ((cnp
->cn_nameptr
[0] == '.') && (cnp
->cn_namelen
== 1)) {
202 cnp
->cn_flags
&= ~MAKEENTRY
;
203 goto found
; /* We always know who we are */
205 /* Check fork suffix to see if we want the resource fork */
206 forknamelen
= forkcomponent(cnp
, &wantrsrc
);
208 /* Resource fork names are not cached. */
210 cnp
->cn_flags
&= ~MAKEENTRY
;
212 if (hfs_lock(dcp
, HFS_EXCLUSIVE_LOCK
) != 0) {
216 /* No need to go to catalog if there are no children */
217 if (dcp
->c_entries
== 0) {
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
;
228 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
230 retval
= cat_lookup(hfsmp
, &cndesc
, wantrsrc
, &desc
, &attr
, &fork
, NULL
);
232 hfs_systemfile_unlock(hfsmp
, lockflags
);
235 dcp
->c_childhint
= desc
.cd_hint
;
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) {
250 * This is a non-existing entry
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.
256 if ((nameiop
== CREATE
|| nameiop
== RENAME
||
257 (nameiop
== DELETE
&&
258 (cnp
->cn_flags
& DOWHITEOUT
) &&
259 (cnp
->cn_flags
& ISWHITEOUT
))) &&
260 (flags
& ISLASTCN
) &&
261 (retval
== ENOENT
)) {
262 retval
= EJUSTRETURN
;
266 * Insert name into cache (as non-existent) if appropriate.
268 * Only done for case-sensitive HFS+ volumes.
270 if ((retval
== ENOENT
) &&
271 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
) &&
272 (cnp
->cn_flags
& MAKEENTRY
) && nameiop
!= CREATE
) {
273 cache_enter(dvp
, NULL
, cnp
);
280 * Process any fork specifiers
282 if (forknamelen
&& S_ISREG(attr
.ca_mode
)) {
283 /* fork names are only for lookups */
284 if ((nameiop
!= LOOKUP
) && (nameiop
!= CREATE
)) {
288 cnp
->cn_consume
= forknamelen
;
294 if (flags
& ISLASTCN
) {
297 cnp
->cn_flags
&= ~MAKEENTRY
;
301 cnp
->cn_flags
&= ~MAKEENTRY
;
311 if ((retval
= vnode_get(dvp
)))
314 } else if (flags
& ISDOTDOT
) {
315 if ((retval
= hfs_vget(hfsmp
, dcp
->c_parentcnid
, &tvp
, 0)))
320 int type
= (attr
.ca_mode
& S_IFMT
);
322 if (!(flags
& ISLASTCN
) && (type
!= S_IFDIR
) && (type
!= S_IFLNK
)) {
327 /* Names with composed chars are not cached. */
328 if (cnp
->cn_namelen
!= desc
.cd_namelen
)
329 cnp
->cn_flags
&= ~MAKEENTRY
;
331 /* Resource fork vnode names include the fork specifier. */
332 if (wantrsrc
&& (flags
& ISLASTCN
))
333 cnp
->cn_namelen
+= forknamelen
;
335 retval
= hfs_getnewvnode(hfsmp
, dvp
, cnp
, &desc
, wantrsrc
, &attr
, &fork
, &tvp
);
337 if (wantrsrc
&& (flags
& ISLASTCN
))
338 cnp
->cn_namelen
-= forknamelen
;
346 cat_releasedesc(&desc
);
353 * Name caching works as follows:
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
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.
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.
370 #define S_IXALL 0000111
374 hfs_vnop_lookup(struct vnop_lookup_args
*ap
)
376 struct vnode
*dvp
= ap
->a_dvp
;
381 struct vnode
**vpp
= ap
->a_vpp
;
382 struct componentname
*cnp
= ap
->a_cnp
;
383 int flags
= cnp
->cn_flags
;
390 * Lookup an entry in the cache
392 * If the lookup succeeds, the vnode is returned in *vpp,
393 * and a status of -1 is returned.
395 * If the lookup determines that the name does not exist
396 * (negative cacheing), a status of ENOENT is returned.
398 * If the lookup fails, a status of zero is returned.
400 error
= cache_lookup(dvp
, vpp
, cnp
);
402 if (error
== ENOENT
) /* found a negative cache entry */
404 goto lookup
; /* did not find it in the cache */
407 * We have a name that matched
408 * cache_lookup returns the vp with an iocount reference already taken
414 * If this is a hard-link vnode then we need to update
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.
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
;
429 * Get an updated descriptor
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
;
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
);
444 if (dvp
!= vp
&& !(flags
& ISDOTDOT
)) {
445 if ((flags
& ISLASTCN
) == 0 && vnode_isreg(vp
)) {
448 cnp
->cn_consume
= forkcomponent(cnp
, &wantrsrc
);
449 if (cnp
->cn_consume
) {
451 /* Fork names are only for lookups */
452 if (cnp
->cn_nameiop
!= LOOKUP
&&
453 cnp
->cn_nameiop
!= CREATE
) {
460 * Use cnode's rsrcfork vnode if possible.
467 if (cp
->c_rsrc_vp
== NULL
) {
471 vid
= vnode_vid(cp
->c_rsrc_vp
);
473 error
= vnode_getwithvid(cp
->c_rsrc_vp
, vid
);
478 *vpp
= cp
->c_rsrc_vp
;
488 * The vnode was not in the name cache or it was stale.
490 * So we need to do a real lookup.
494 error
= hfs_lookup(dvp
, vpp
, cnp
, ap
->a_context
, &cnode_locked
);
497 hfs_unlock(VTOC(*vpp
));
504 * forkcomponent - look for a fork suffix in the component name
508 forkcomponent(struct componentname
*cnp
, int *rsrcfork
)
510 char *suffix
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
517 * There are only 3 valid fork suffixes:
518 * "/..namedfork/rsrc"
519 * "/..namedfork/data"
522 if (bcmp(suffix
, _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
)) == 0) {
523 consume
= sizeof(_PATH_RSRCFORKSPEC
) - 1;
525 } else if (bcmp(suffix
, _PATH_DATAFORKSPEC
, sizeof(_PATH_DATAFORKSPEC
)) == 0) {
526 consume
= sizeof(_PATH_DATAFORKSPEC
) - 1;
529 #if LEGACY_FORK_NAMES
530 else if (bcmp(suffix
, LEGACY_RSRCFORKSPEC
, sizeof(LEGACY_RSRCFORKSPEC
)) == 0) {
531 consume
= sizeof(LEGACY_RSRCFORKSPEC
) - 1;
533 printf("HFS: /rsrc paths are deprecated (%s)\n", cnp
->cn_nameptr
);