]>
git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_lookup.c
2 * Copyright (c) 1999-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1989, 1993
27 * The Regents of the University of California. All rights reserved.
28 * (c) UNIX System Laboratories, Inc.
29 * All or some portions of this file are derived from material licensed
30 * to the University of California by American Telephone and Telegraph
31 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
32 * the permission of UNIX System Laboratories, Inc.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)hfs_lookup.c 1.0
63 * derived from @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
65 * (c) 1998-1999 Apple Computer, Inc. All Rights Reserved
66 * (c) 1990, 1992 NeXT Computer, Inc. All Rights Reserved
69 * hfs_lookup.c -- code to handle directory traversal on HFS/HFS+ volume
71 #define LEGACY_FORK_NAMES 0
73 #include <sys/param.h>
76 #include <sys/mount.h>
77 #include <sys/vnode.h>
78 #include <sys/namei.h>
79 #include <sys/malloc.h>
80 #include <sys/paths.h>
83 #include "hfs_catalog.h"
84 #include "hfs_cnode.h"
87 static int forkcomponent(struct componentname
*cnp
, int *rsrcfork
);
89 #define _PATH_DATAFORKSPEC "/..namedfork/data"
91 #ifdef LEGACY_FORK_NAMES
92 #define LEGACY_RSRCFORKSPEC "/rsrc"
97 * Convert a component of a pathname into a pointer to a locked cnode.
98 * This is a very central and rather complicated routine.
99 * If the file system is not maintained in a strict tree hierarchy,
100 * this can result in a deadlock situation (see comments in code below).
102 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
103 * on whether the name is to be looked up, created, renamed, or deleted.
104 * When CREATE, RENAME, or DELETE is specified, information usable in
105 * creating, renaming, or deleting a directory entry may be calculated.
106 * Notice that these are the only operations that can affect the directory of the target.
108 * If flag has LOCKPARENT or'ed into it and the target of the pathname
109 * exists, lookup returns both the target and its parent directory locked.
110 * When creating or renaming and LOCKPARENT is specified, the target may
111 * not be ".". When deleting and LOCKPARENT is specified, the target may
112 * be "."., but the caller must check to ensure it does an vrele and vput
113 * instead of two vputs.
115 * LOCKPARENT and WANTPARENT actually refer to the parent of the last item,
116 * so if ISLASTCN is not set, they should be ignored. Also they are mutually exclusive, or
117 * WANTPARENT really implies DONTLOCKPARENT. Either of them set means that the calling
118 * routine wants to access the parent of the target, locked or unlocked.
120 * Keeping the parent locked as long as possible protects from other processes
121 * looking up the same item, so it has to be locked until the cnode is totally finished
123 * This routine is actually used as VOP_CACHEDLOOKUP method, and the
124 * filesystem employs the generic hfs_cache_lookup() as VOP_LOOKUP
127 * hfs_cache_lookup() performs the following for us:
128 * check that it is a directory
129 * check accessibility of directory
130 * check for modification attempts on read-only mounts
131 * if name found in cache
132 * if at end of path and deleting or creating
136 * return VOP_CACHEDLOOKUP()
138 * Overall outline of hfs_lookup:
140 * handle simple cases of . and ..
141 * search for name in directory, to found or notfound
143 * if creating, return locked directory, leaving info on available slots
146 * if at end of path and deleting, return information to allow delete
147 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
148 * cnode and return info to allow rewrite
149 * if not at end, add name to cache; if at end and neither creating
150 * nor deleting, add name to cache
154 * Lookup *nm in directory *pvp, return it in *a_vpp.
155 * **a_vpp is held on exit.
156 * We create a cnode for the file, but we do NOT open the file here.
161 IN struct vnode *dvp - Parent node of file;
162 INOUT struct vnode **vpp - node of target file, its a new node if
163 the target vnode did not exist;
164 IN struct componentname *cnp - Name of file;
166 * When should we lock parent_hp in here ??
172 struct vop_cachedlookup_args
/* {
174 struct vnode **a_vpp;
175 struct componentname *a_cnp;
178 struct vnode
*dvp
; /* vnode for directory being searched */
179 struct cnode
*dcp
; /* cnode for directory being searched */
180 struct vnode
*tvp
; /* target vnode */
181 struct hfsmount
*hfsmp
;
182 struct componentname
*cnp
;
192 struct cat_desc desc
= {0};
193 struct cat_desc cndesc
;
194 struct cat_attr attr
;
195 struct cat_fork fork
;
206 nameiop
= cnp
->cn_nameiop
;
209 flags
= cnp
->cn_flags
;
210 wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
213 * First check to see if it is a . or .., else look it up.
215 if (flags
& ISDOTDOT
) { /* Wanting the parent */
216 goto found
; /* .. is always defined */
217 } else if ((cnp
->cn_nameptr
[0] == '.') && (cnp
->cn_namelen
== 1)) {
219 goto found
; /* We always know who we are */
221 /* Check fork suffix to see if we want the resource fork */
222 forknamelen
= forkcomponent(cnp
, &wantrsrc
);
224 /* No need to go to catalog if there are no children */
225 if (dcp
->c_entries
== 0)
228 bzero(&cndesc
, sizeof(cndesc
));
229 cndesc
.cd_nameptr
= cnp
->cn_nameptr
;
230 cndesc
.cd_namelen
= cnp
->cn_namelen
;
231 cndesc
.cd_parentcnid
= dcp
->c_cnid
;
232 cndesc
.cd_hint
= dcp
->c_childhint
;
234 /* Lock catalog b-tree */
235 retval
= hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_SHARED
, p
);
238 retval
= cat_lookup(hfsmp
, &cndesc
, wantrsrc
, &desc
, &attr
, &fork
);
240 if (retval
== 0 && S_ISREG(attr
.ca_mode
) && attr
.ca_blocks
< fork
.cf_blocks
)
241 panic("hfs_lookup: bad ca_blocks (too small)");
243 /* Unlock catalog b-tree */
244 (void) hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_RELEASE
, p
);
246 dcp
->c_childhint
= desc
.cd_hint
;
251 * This is a non-existing entry
253 * If creating, and at end of pathname and current
254 * directory has not been removed, then can consider
255 * allowing file to be created.
257 if ((nameiop
== CREATE
|| nameiop
== RENAME
||
258 (nameiop
== DELETE
&&
259 (ap
->a_cnp
->cn_flags
& DOWHITEOUT
) &&
260 (ap
->a_cnp
->cn_flags
& ISWHITEOUT
))) &&
261 (flags
& ISLASTCN
)) {
263 * Access for write is interpreted as allowing
264 * creation of files in the directory.
266 retval
= VOP_ACCESS(dvp
, VWRITE
, cred
, cnp
->cn_proc
);
271 cnp
->cn_flags
|= SAVENAME
;
272 if (!(flags
& LOCKPARENT
))
273 VOP_UNLOCK(dvp
, 0, p
);
274 retval
= EJUSTRETURN
;
279 * Insert name into cache (as non-existent) if appropriate.
281 * Disable negative caching since HFS is case-insensitive.
284 if ((cnp
->cn_flags
& MAKEENTRY
) && nameiop
!= CREATE
)
285 cache_enter(dvp
, *vpp
, cnp
);
293 * Process any fork specifiers
295 if (forknamelen
&& S_ISREG(attr
.ca_mode
)) {
296 /* fork names are only for lookups */
297 if ((nameiop
!= LOOKUP
) && (nameiop
!= CREATE
)) {
301 cnp
->cn_consume
= forknamelen
;
309 * If deleting, and at end of pathname, return
310 * parameters which can be used to remove file.
312 if (nameiop
== DELETE
&& (flags
& ISLASTCN
)) {
314 * Write access to directory required to delete files.
316 if ((retval
= VOP_ACCESS(dvp
, VWRITE
, cred
, cnp
->cn_proc
)))
319 if (isDot
) { /* Want to return ourselves */
323 } else if (flags
& ISDOTDOT
) {
324 retval
= hfs_getcnode(hfsmp
, dcp
->c_parentcnid
,
325 NULL
, 0, NULL
, NULL
, &tvp
);
329 retval
= hfs_getcnode(hfsmp
, attr
.ca_fileid
,
330 &desc
, wantrsrc
, &attr
, &fork
, &tvp
);
336 * If directory is "sticky", then user must own
337 * the directory, or the file in it, else she
338 * may not delete it (unless she's root). This
339 * implements append-only directories.
341 if ((dcp
->c_mode
& S_ISTXT
) &&
342 (cred
->cr_uid
!= 0) &&
343 (cred
->cr_uid
!= dcp
->c_uid
) &&
344 (tvp
->v_type
!= VLNK
) &&
345 (hfs_owner_rights(hfsmp
, VTOC(tvp
)->c_uid
, cred
, p
, false))) {
352 * If this is a link node then we need to save the name
353 * (of the link) so we can delete it from the catalog b-tree.
354 * In this case, hfs_remove will then free the component name.
356 * DJB - IS THIS STILL NEEDED????
358 if (tvp
&& (VTOC(tvp
)->c_flag
& C_HARDLINK
))
359 cnp
->cn_flags
|= SAVENAME
;
361 if (!(flags
& LOCKPARENT
))
362 VOP_UNLOCK(dvp
, 0, p
);
368 * If renaming, return the cnode and save the current name.
370 if (nameiop
== RENAME
&& wantparent
&& (flags
& ISLASTCN
)) {
371 if ((retval
= VOP_ACCESS(dvp
, VWRITE
, cred
, cnp
->cn_proc
)) != 0)
374 * Careful about locking second cnode.
379 } else if (flags
& ISDOTDOT
) {
380 retval
= hfs_getcnode(hfsmp
, dcp
->c_parentcnid
,
381 NULL
, 0, NULL
, NULL
, &tvp
);
385 retval
= hfs_getcnode(hfsmp
, attr
.ca_fileid
,
386 &desc
, wantrsrc
, &attr
, &fork
, &tvp
);
390 cnp
->cn_flags
|= SAVENAME
;
391 if (!(flags
& LOCKPARENT
))
392 VOP_UNLOCK(dvp
, 0, p
);
398 * We must get the target cnode before unlocking
399 * the directory to insure that the cnode will not be removed
400 * before we get it. We prevent deadlock by always fetching
401 * cnodes from the root, moving down the directory tree. Thus
402 * when following backward pointers ".." we must unlock the
403 * parent directory before getting the requested directory.
404 * There is a potential race condition here if both the current
405 * and parent directories are removed before the VFS_VGET for the
406 * cnode associated with ".." returns. We hope that this occurs
407 * infrequently since we cannot avoid this race condition without
408 * implementing a sophisticated deadlock detection algorithm.
410 if (flags
& ISDOTDOT
) {
411 VOP_UNLOCK(dvp
, 0, p
); /* race to get the cnode */
412 retval
= hfs_getcnode(hfsmp
, dcp
->c_parentcnid
,
413 NULL
, 0, NULL
, NULL
, &tvp
);
415 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
418 if ((flags
& LOCKPARENT
) && (flags
& ISLASTCN
) && (dvp
!= tvp
) &&
419 (retval
= vn_lock(dvp
, LK_EXCLUSIVE
, p
))) {
425 VREF(dvp
); /* we want ourself, ie "." */
428 int type
= (attr
.ca_mode
& S_IFMT
);
430 if (!(flags
& ISLASTCN
) && type
!= S_IFDIR
&& type
!= S_IFLNK
) {
435 retval
= hfs_getcnode(hfsmp
, attr
.ca_fileid
,
436 &desc
, wantrsrc
, &attr
, &fork
, &tvp
);
440 if (!(flags
& LOCKPARENT
) || !(flags
& ISLASTCN
))
441 VOP_UNLOCK(dvp
, 0, p
);
446 * Insert name in cache if appropriate.
447 * - "." and ".." are not cached.
448 * - Resource fork names are not cached.
449 * - Names with composed chars are not cached.
451 if ((cnp
->cn_flags
& MAKEENTRY
)
453 && !(flags
& ISDOTDOT
)
455 && (cnp
->cn_namelen
== VTOC(*vpp
)->c_desc
.cd_namelen
)) {
456 cache_enter(dvp
, *vpp
, cnp
);
460 cat_releasedesc(&desc
);
467 * Based on vn_cache_lookup (which is vfs_cache_lookup in FreeBSD 3.1)
469 * Name caching works as follows:
471 * Names found by directory scans are retained in a cache
472 * for future reference. It is managed LRU, so frequently
473 * used names will hang around. Cache is indexed by hash value
474 * obtained from (vp, name) where vp refers to the directory
477 * If it is a "negative" entry, (i.e. for a name that is known NOT to
478 * exist) the vnode pointer will be NULL.
480 * Upon reaching the last segment of a path, if the reference
481 * is for DELETE, or NOCACHE is set (rewrite), and the
482 * name is located in the cache, it will be dropped.
489 struct vop_lookup_args
/* {
491 struct vnode **a_vpp;
492 struct componentname *a_cnp;
500 struct vnode
**vpp
= ap
->a_vpp
;
501 struct componentname
*cnp
= ap
->a_cnp
;
502 struct ucred
*cred
= cnp
->cn_cred
;
503 int flags
= cnp
->cn_flags
;
504 struct proc
*p
= cnp
->cn_proc
;
505 u_long vpid
; /* capability number of vnode */
509 lockparent
= flags
& LOCKPARENT
;
512 * Check accessiblity of directory.
514 if (dvp
->v_type
!= VDIR
)
516 if ((flags
& ISLASTCN
) && (dvp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
517 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
))
519 if ((error
= VOP_ACCESS(dvp
, VEXEC
, cred
, cnp
->cn_proc
)))
523 * Lookup an entry in the cache
524 * If the lookup succeeds, the vnode is returned in *vpp, and a status of -1 is
525 * returned. If the lookup determines that the name does not exist
526 * (negative cacheing), a status of ENOENT is returned. If the lookup
527 * fails, a status of zero is returned.
529 error
= cache_lookup(dvp
, vpp
, cnp
);
530 if (error
== 0) { /* Unsuccessfull */
531 error
= hfs_lookup(ap
);
538 /* We have a name that matched */
543 * If this is a hard-link vnode then we need to update
544 * the name (of the link), the parent ID, the cnid, the
545 * text encoding and the catalog hint. This enables
546 * getattrlist calls to return the correct link info.
549 if ((flags
& ISLASTCN
) && (cp
->c_flag
& C_HARDLINK
) &&
550 ((cp
->c_parentcnid
!= VTOC(ap
->a_dvp
)->c_cnid
) ||
551 (bcmp(cnp
->cn_nameptr
, cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
) != 0))) {
553 struct cat_desc desc
;
556 * Get an updated descriptor
558 bzero(&desc
, sizeof(desc
));
559 desc
.cd_nameptr
= cnp
->cn_nameptr
;
560 desc
.cd_namelen
= cnp
->cn_namelen
;
561 desc
.cd_parentcnid
= VTOC(ap
->a_dvp
)->c_cnid
;
562 desc
.cd_hint
= VTOC(ap
->a_dvp
)->c_childhint
;
563 if (cat_lookup(VTOHFS(vp
), &desc
, 0, &desc
, NULL
, NULL
) == 0)
564 replace_desc(cp
, &desc
);
567 if (dvp
== vp
) { /* lookup on "." */
570 } else if (flags
& ISDOTDOT
) {
572 * Carefull on the locking policy,
573 * remember we always lock from parent to child, so have
574 * to release lock on child before trying to lock parent
575 * then regain lock if needed
577 VOP_UNLOCK(dvp
, 0, p
);
578 error
= vget(vp
, LK_EXCLUSIVE
, p
);
579 if (!error
&& lockparent
&& (flags
& ISLASTCN
))
580 error
= vn_lock(dvp
, LK_EXCLUSIVE
, p
);
582 if ((flags
& ISLASTCN
) == 0 && vp
->v_type
== VREG
) {
585 cnp
->cn_consume
= forkcomponent(cnp
, &wantrsrc
);
587 /* Fork names are only for lookups */
588 if (cnp
->cn_consume
&&
589 (cnp
->cn_nameiop
!= LOOKUP
&& cnp
->cn_nameiop
!= CREATE
))
592 * We only store data forks in the name cache.
595 return (hfs_lookup(ap
));
597 error
= vget(vp
, LK_EXCLUSIVE
, p
);
598 if (!lockparent
|| error
|| !(flags
& ISLASTCN
))
599 VOP_UNLOCK(dvp
, 0, p
);
602 * Check that the capability number did not change
603 * while we were waiting for the lock.
606 if (vpid
== vp
->v_id
)
609 * The above is the NORMAL exit, after this point is an error
613 if (lockparent
&& (dvp
!= vp
) && (flags
& ISLASTCN
))
614 VOP_UNLOCK(dvp
, 0, p
);
617 if ((error
= vn_lock(dvp
, LK_EXCLUSIVE
, p
)))
620 return (hfs_lookup(ap
));
625 * forkcomponent - look for a fork suffix in the component name
629 forkcomponent(struct componentname
*cnp
, int *rsrcfork
)
631 char *suffix
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
638 * There are only 3 valid fork suffixes:
639 * "/..namedfork/rsrc"
640 * "/..namedfork/data"
643 if (bcmp(suffix
, _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
)) == 0) {
644 consume
= sizeof(_PATH_RSRCFORKSPEC
) - 1;
646 } else if (bcmp(suffix
, _PATH_DATAFORKSPEC
, sizeof(_PATH_DATAFORKSPEC
)) == 0) {
647 consume
= sizeof(_PATH_DATAFORKSPEC
) - 1;
650 #ifdef LEGACY_FORK_NAMES
651 else if (bcmp(suffix
, LEGACY_RSRCFORKSPEC
, sizeof(LEGACY_RSRCFORKSPEC
)) == 0) {
652 consume
= sizeof(LEGACY_RSRCFORKSPEC
) - 1;