]>
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_LICENSE_HEADER_START@
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.
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
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
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.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
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.
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
59 * @(#)hfs_lookup.c 1.0
60 * derived from @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
62 * (c) 1998-1999 Apple Computer, Inc. All Rights Reserved
63 * (c) 1990, 1992 NeXT Computer, Inc. All Rights Reserved
66 * hfs_lookup.c -- code to handle directory traversal on HFS/HFS+ volume
69 #include <sys/param.h>
71 #include <sys/mount.h>
72 #include <sys/vnode.h>
73 #include <sys/malloc.h>
74 #include <sys/paths.h>
75 #include <sys/kdebug.h>
76 #include <sys/kauth.h>
79 #include "hfs_catalog.h"
80 #include "hfs_cnode.h"
82 #define LEGACY_FORK_NAMES 1
84 static int forkcomponent(struct componentname
*cnp
, int *rsrcfork
);
86 #define _PATH_DATAFORKSPEC "/..namedfork/data"
89 #define LEGACY_RSRCFORKSPEC "/rsrc"
94 * Convert a component of a pathname into a pointer to a locked cnode.
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).
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.
105 * LOCKPARENT and WANTPARENT actually refer to the parent of the last item,
106 * so if ISLASTCN is not set, they should be ignored. Also they are mutually exclusive, or
107 * WANTPARENT really implies DONTLOCKPARENT. Either of them set means that the calling
108 * routine wants to access the parent of the target, locked or unlocked.
110 * Keeping the parent locked as long as possible protects from other processes
111 * looking up the same item, so it has to be locked until the cnode is totally finished
113 * hfs_cache_lookup() performs the following for us:
114 * check that it is a directory
115 * check accessibility of directory
116 * check for modification attempts on read-only mounts
117 * if name found in cache
118 * if at end of path and deleting or creating
122 * return hfs_lookup()
124 * Overall outline of hfs_lookup:
126 * handle simple cases of . and ..
127 * search for name in directory, to found or notfound
129 * if creating, return locked directory, leaving info on available slots
132 * if at end of path and deleting, return information to allow delete
133 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
134 * cnode and return info to allow rewrite
135 * if not at end, add name to cache; if at end and neither creating
136 * nor deleting, add name to cache
141 * Lookup *cnp in directory *dvp, return it in *vpp.
142 * **vpp is held on exit.
143 * We create a cnode for the file, but we do NOT open the file here.
148 IN struct vnode *dvp - Parent node of file;
149 INOUT struct vnode **vpp - node of target file, its a new node if
150 the target vnode did not exist;
151 IN struct componentname *cnp - Name of file;
153 * When should we lock parent_hp in here ??
156 hfs_lookup(struct vnode
*dvp
, struct vnode
**vpp
, struct componentname
*cnp
, vfs_context_t context
, int *cnode_locked
)
158 struct cnode
*dcp
; /* cnode for directory being searched */
159 struct vnode
*tvp
; /* target vnode */
160 struct hfsmount
*hfsmp
;
169 struct cat_desc desc
;
170 struct cat_desc cndesc
;
171 struct cat_attr attr
;
172 struct cat_fork fork
;
181 nameiop
= cnp
->cn_nameiop
;
182 flags
= cnp
->cn_flags
;
183 bzero(&desc
, sizeof(desc
));
185 cred
= vfs_context_ucred(context
);
186 p
= vfs_context_proc(context
);
189 * First check to see if it is a . or .., else look it up.
191 if (flags
& ISDOTDOT
) { /* Wanting the parent */
192 cnp
->cn_flags
&= ~MAKEENTRY
;
193 goto found
; /* .. is always defined */
194 } else if ((cnp
->cn_nameptr
[0] == '.') && (cnp
->cn_namelen
== 1)) {
196 cnp
->cn_flags
&= ~MAKEENTRY
;
197 goto found
; /* We always know who we are */
199 /* Check fork suffix to see if we want the resource fork */
200 forknamelen
= forkcomponent(cnp
, &wantrsrc
);
202 /* Resource fork names are not cached. */
204 cnp
->cn_flags
&= ~MAKEENTRY
;
206 if (hfs_lock(dcp
, HFS_EXCLUSIVE_LOCK
) != 0) {
210 /* No need to go to catalog if there are no children */
211 if (dcp
->c_entries
== 0) {
216 bzero(&cndesc
, sizeof(cndesc
));
217 cndesc
.cd_nameptr
= cnp
->cn_nameptr
;
218 cndesc
.cd_namelen
= cnp
->cn_namelen
;
219 cndesc
.cd_parentcnid
= dcp
->c_cnid
;
220 cndesc
.cd_hint
= dcp
->c_childhint
;
222 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
224 retval
= cat_lookup(hfsmp
, &cndesc
, wantrsrc
, &desc
, &attr
, &fork
, NULL
);
226 hfs_systemfile_unlock(hfsmp
, lockflags
);
229 dcp
->c_childhint
= desc
.cd_hint
;
235 /* ENAMETOOLONG supersedes other errors */
236 if (((nameiop
!= CREATE
) && (nameiop
!= RENAME
)) &&
237 (retval
!= ENAMETOOLONG
) &&
238 (cnp
->cn_namelen
> kHFSPlusMaxFileNameChars
)) {
239 retval
= ENAMETOOLONG
;
240 } else if (retval
== 0) {
244 * This is a non-existing entry
246 * If creating, and at end of pathname and current
247 * directory has not been removed, then can consider
248 * allowing file to be created.
250 if ((nameiop
== CREATE
|| nameiop
== RENAME
||
251 (nameiop
== DELETE
&&
252 (cnp
->cn_flags
& DOWHITEOUT
) &&
253 (cnp
->cn_flags
& ISWHITEOUT
))) &&
254 (flags
& ISLASTCN
) &&
255 (retval
== ENOENT
)) {
256 retval
= EJUSTRETURN
;
260 * Insert name into cache (as non-existent) if appropriate.
262 * Only done for case-sensitive HFS+ volumes.
264 if ((retval
== ENOENT
) &&
265 (hfsmp
->hfs_flags
& HFS_CASE_SENSITIVE
) &&
266 (cnp
->cn_flags
& MAKEENTRY
) && nameiop
!= CREATE
) {
267 cache_enter(dvp
, NULL
, cnp
);
274 * Process any fork specifiers
276 if (forknamelen
&& S_ISREG(attr
.ca_mode
)) {
277 /* fork names are only for lookups */
278 if ((nameiop
!= LOOKUP
) && (nameiop
!= CREATE
)) {
282 cnp
->cn_consume
= forknamelen
;
288 if (flags
& ISLASTCN
) {
291 cnp
->cn_flags
&= ~MAKEENTRY
;
295 cnp
->cn_flags
&= ~MAKEENTRY
;
305 if ((retval
= vnode_get(dvp
)))
308 } else if (flags
& ISDOTDOT
) {
309 if ((retval
= hfs_vget(hfsmp
, dcp
->c_parentcnid
, &tvp
, 0)))
314 int type
= (attr
.ca_mode
& S_IFMT
);
316 if (!(flags
& ISLASTCN
) && (type
!= S_IFDIR
) && (type
!= S_IFLNK
)) {
321 /* Names with composed chars are not cached. */
322 if (cnp
->cn_namelen
!= desc
.cd_namelen
)
323 cnp
->cn_flags
&= ~MAKEENTRY
;
325 /* Resource fork vnode names include the fork specifier. */
326 if (wantrsrc
&& (flags
& ISLASTCN
))
327 cnp
->cn_namelen
+= forknamelen
;
329 retval
= hfs_getnewvnode(hfsmp
, dvp
, cnp
, &desc
, wantrsrc
, &attr
, &fork
, &tvp
);
331 if (wantrsrc
&& (flags
& ISLASTCN
))
332 cnp
->cn_namelen
-= forknamelen
;
340 cat_releasedesc(&desc
);
347 * Name caching works as follows:
349 * Names found by directory scans are retained in a cache
350 * for future reference. It is managed LRU, so frequently
351 * used names will hang around. Cache is indexed by hash value
352 * obtained from (vp, name) where vp refers to the directory
355 * If it is a "negative" entry, (i.e. for a name that is known NOT to
356 * exist) the vnode pointer will be NULL.
358 * Upon reaching the last segment of a path, if the reference
359 * is for DELETE, or NOCACHE is set (rewrite), and the
360 * name is located in the cache, it will be dropped.
364 #define S_IXALL 0000111
368 hfs_vnop_lookup(struct vnop_lookup_args
*ap
)
370 struct vnode
*dvp
= ap
->a_dvp
;
375 struct vnode
**vpp
= ap
->a_vpp
;
376 struct componentname
*cnp
= ap
->a_cnp
;
377 int flags
= cnp
->cn_flags
;
384 * Lookup an entry in the cache
386 * If the lookup succeeds, the vnode is returned in *vpp,
387 * and a status of -1 is returned.
389 * If the lookup determines that the name does not exist
390 * (negative cacheing), a status of ENOENT is returned.
392 * If the lookup fails, a status of zero is returned.
394 error
= cache_lookup(dvp
, vpp
, cnp
);
396 if (error
== ENOENT
) /* found a negative cache entry */
398 goto lookup
; /* did not find it in the cache */
401 * We have a name that matched
402 * cache_lookup returns the vp with an iocount reference already taken
408 * If this is a hard-link vnode then we need to update
409 * the name (of the link), the parent ID, the cnid, the
410 * text encoding and the catalog hint. This enables
411 * getattrlist calls to return the correct link info.
415 if ((flags
& ISLASTCN
) && (cp
->c_flag
& C_HARDLINK
)) {
416 hfs_lock(cp
, HFS_FORCE_LOCK
);
417 if ((cp
->c_parentcnid
!= VTOC(dvp
)->c_cnid
) ||
418 (bcmp(cnp
->cn_nameptr
, cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
) != 0)) {
419 struct cat_desc desc
;
423 * Get an updated descriptor
425 bzero(&desc
, sizeof(desc
));
426 desc
.cd_nameptr
= cnp
->cn_nameptr
;
427 desc
.cd_namelen
= cnp
->cn_namelen
;
428 desc
.cd_parentcnid
= VTOC(dvp
)->c_cnid
;
429 desc
.cd_hint
= VTOC(dvp
)->c_childhint
;
431 lockflags
= hfs_systemfile_lock(VTOHFS(dvp
), SFL_CATALOG
, HFS_SHARED_LOCK
);
432 if (cat_lookup(VTOHFS(vp
), &desc
, 0, &desc
, NULL
, NULL
, NULL
) == 0)
433 replace_desc(cp
, &desc
);
434 hfs_systemfile_unlock(VTOHFS(dvp
), lockflags
);
438 if (dvp
!= vp
&& !(flags
& ISDOTDOT
)) {
439 if ((flags
& ISLASTCN
) == 0 && vnode_isreg(vp
)) {
442 cnp
->cn_consume
= forkcomponent(cnp
, &wantrsrc
);
443 if (cnp
->cn_consume
) {
445 /* Fork names are only for lookups */
446 if (cnp
->cn_nameiop
!= LOOKUP
&&
447 cnp
->cn_nameiop
!= CREATE
) {
454 * Use cnode's rsrcfork vnode if possible.
461 if (cp
->c_rsrc_vp
== NULL
) {
465 vid
= vnode_vid(cp
->c_rsrc_vp
);
467 error
= vnode_getwithvid(cp
->c_rsrc_vp
, vid
);
472 *vpp
= cp
->c_rsrc_vp
;
482 * The vnode was not in the name cache or it was stale.
484 * So we need to do a real lookup.
488 error
= hfs_lookup(dvp
, vpp
, cnp
, ap
->a_context
, &cnode_locked
);
491 hfs_unlock(VTOC(*vpp
));
498 * forkcomponent - look for a fork suffix in the component name
502 forkcomponent(struct componentname
*cnp
, int *rsrcfork
)
504 char *suffix
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
511 * There are only 3 valid fork suffixes:
512 * "/..namedfork/rsrc"
513 * "/..namedfork/data"
516 if (bcmp(suffix
, _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
)) == 0) {
517 consume
= sizeof(_PATH_RSRCFORKSPEC
) - 1;
519 } else if (bcmp(suffix
, _PATH_DATAFORKSPEC
, sizeof(_PATH_DATAFORKSPEC
)) == 0) {
520 consume
= sizeof(_PATH_DATAFORKSPEC
) - 1;
523 #if LEGACY_FORK_NAMES
524 else if (bcmp(suffix
, LEGACY_RSRCFORKSPEC
, sizeof(LEGACY_RSRCFORKSPEC
)) == 0) {
525 consume
= sizeof(LEGACY_RSRCFORKSPEC
) - 1;
527 printf("HFS: /rsrc paths are deprecated (%s)\n", cnp
->cn_nameptr
);