]>
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 * 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
68 #define LEGACY_FORK_NAMES 0
70 #include <sys/param.h>
73 #include <sys/mount.h>
74 #include <sys/vnode.h>
75 #include <sys/namei.h>
76 #include <sys/malloc.h>
77 #include <sys/paths.h>
80 #include "hfs_catalog.h"
81 #include "hfs_cnode.h"
84 static int forkcomponent(struct componentname
*cnp
, int *rsrcfork
);
86 #define _PATH_DATAFORKSPEC "/..namedfork/data"
88 #ifdef LEGACY_FORK_NAMES
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 * 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.
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.
117 * Keeping the parent locked as long as possible protects from other processes
118 * looking up the same item, so it has to be locked until the cnode is totally finished
120 * This routine is actually used as VOP_CACHEDLOOKUP method, and the
121 * filesystem employs the generic hfs_cache_lookup() as VOP_LOOKUP
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
133 * return VOP_CACHEDLOOKUP()
135 * Overall outline of hfs_lookup:
137 * handle simple cases of . and ..
138 * search for name in directory, to found or notfound
140 * if creating, return locked directory, leaving info on available slots
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
145 * cnode and return info to allow rewrite
146 * if not at end, add name to cache; if at end and neither creating
147 * nor deleting, add name to cache
151 * Lookup *nm in directory *pvp, return it in *a_vpp.
152 * **a_vpp is held on exit.
153 * We create a cnode for the file, but we do NOT open the file here.
158 IN struct vnode *dvp - Parent node of file;
159 INOUT struct vnode **vpp - node of target file, its a new node if
160 the target vnode did not exist;
161 IN struct componentname *cnp - Name of file;
163 * When should we lock parent_hp in here ??
169 struct vop_cachedlookup_args
/* {
171 struct vnode **a_vpp;
172 struct componentname *a_cnp;
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
;
189 struct cat_desc desc
= {0};
190 struct cat_desc cndesc
;
191 struct cat_attr attr
;
192 struct cat_fork fork
;
203 nameiop
= cnp
->cn_nameiop
;
206 flags
= cnp
->cn_flags
;
207 wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
210 * First check to see if it is a . or .., else look it up.
212 if (flags
& ISDOTDOT
) { /* Wanting the parent */
213 goto found
; /* .. is always defined */
214 } else if ((cnp
->cn_nameptr
[0] == '.') && (cnp
->cn_namelen
== 1)) {
216 goto found
; /* We always know who we are */
218 /* Check fork suffix to see if we want the resource fork */
219 forknamelen
= forkcomponent(cnp
, &wantrsrc
);
221 /* No need to go to catalog if there are no children */
222 if (dcp
->c_entries
== 0)
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
;
231 /* Lock catalog b-tree */
232 retval
= hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_SHARED
, p
);
235 retval
= cat_lookup(hfsmp
, &cndesc
, wantrsrc
, &desc
, &attr
, &fork
);
237 if (retval
== 0 && S_ISREG(attr
.ca_mode
) && attr
.ca_blocks
< fork
.cf_blocks
)
238 panic("hfs_lookup: bad ca_blocks (too small)");
240 /* Unlock catalog b-tree */
241 (void) hfs_metafilelocking(hfsmp
, kHFSCatalogFileID
, LK_RELEASE
, p
);
243 dcp
->c_childhint
= desc
.cd_hint
;
248 * This is a non-existing entry
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.
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
)) {
260 * Access for write is interpreted as allowing
261 * creation of files in the directory.
263 retval
= VOP_ACCESS(dvp
, VWRITE
, cred
, cnp
->cn_proc
);
268 cnp
->cn_flags
|= SAVENAME
;
269 if (!(flags
& LOCKPARENT
))
270 VOP_UNLOCK(dvp
, 0, p
);
271 retval
= EJUSTRETURN
;
276 * Insert name into cache (as non-existent) if appropriate.
278 * Disable negative caching since HFS is case-insensitive.
281 if ((cnp
->cn_flags
& MAKEENTRY
) && nameiop
!= CREATE
)
282 cache_enter(dvp
, *vpp
, cnp
);
290 * Process any fork specifiers
292 if (forknamelen
&& S_ISREG(attr
.ca_mode
)) {
293 /* fork names are only for lookups */
294 if ((nameiop
!= LOOKUP
) && (nameiop
!= CREATE
)) {
298 cnp
->cn_consume
= forknamelen
;
306 * If deleting, and at end of pathname, return
307 * parameters which can be used to remove file.
309 if (nameiop
== DELETE
&& (flags
& ISLASTCN
)) {
311 * Write access to directory required to delete files.
313 if ((retval
= VOP_ACCESS(dvp
, VWRITE
, cred
, cnp
->cn_proc
)))
316 if (isDot
) { /* Want to return ourselves */
320 } else if (flags
& ISDOTDOT
) {
321 retval
= hfs_getcnode(hfsmp
, dcp
->c_parentcnid
,
322 NULL
, 0, NULL
, NULL
, &tvp
);
326 retval
= hfs_getcnode(hfsmp
, attr
.ca_fileid
,
327 &desc
, wantrsrc
, &attr
, &fork
, &tvp
);
333 * If directory is "sticky", then user must own
334 * the directory, or the file in it, else she
335 * may not delete it (unless she's root). This
336 * implements append-only directories.
338 if ((dcp
->c_mode
& S_ISTXT
) &&
339 (cred
->cr_uid
!= 0) &&
340 (cred
->cr_uid
!= dcp
->c_uid
) &&
341 (tvp
->v_type
!= VLNK
) &&
342 (hfs_owner_rights(hfsmp
, VTOC(tvp
)->c_uid
, cred
, p
, false))) {
349 * If this is a link node then we need to save the name
350 * (of the link) so we can delete it from the catalog b-tree.
351 * In this case, hfs_remove will then free the component name.
353 * DJB - IS THIS STILL NEEDED????
355 if (tvp
&& (VTOC(tvp
)->c_flag
& C_HARDLINK
))
356 cnp
->cn_flags
|= SAVENAME
;
358 if (!(flags
& LOCKPARENT
))
359 VOP_UNLOCK(dvp
, 0, p
);
365 * If renaming, return the cnode and save the current name.
367 if (nameiop
== RENAME
&& wantparent
&& (flags
& ISLASTCN
)) {
368 if ((retval
= VOP_ACCESS(dvp
, VWRITE
, cred
, cnp
->cn_proc
)) != 0)
371 * Careful about locking second cnode.
376 } else if (flags
& ISDOTDOT
) {
377 retval
= hfs_getcnode(hfsmp
, dcp
->c_parentcnid
,
378 NULL
, 0, NULL
, NULL
, &tvp
);
382 retval
= hfs_getcnode(hfsmp
, attr
.ca_fileid
,
383 &desc
, wantrsrc
, &attr
, &fork
, &tvp
);
387 cnp
->cn_flags
|= SAVENAME
;
388 if (!(flags
& LOCKPARENT
))
389 VOP_UNLOCK(dvp
, 0, p
);
395 * We must get the target cnode before unlocking
396 * the directory to insure that the cnode will not be removed
397 * before we get it. We prevent deadlock by always fetching
398 * cnodes from the root, moving down the directory tree. Thus
399 * when following backward pointers ".." we must unlock the
400 * parent directory before getting the requested directory.
401 * There is a potential race condition here if both the current
402 * and parent directories are removed before the VFS_VGET for the
403 * cnode associated with ".." returns. We hope that this occurs
404 * infrequently since we cannot avoid this race condition without
405 * implementing a sophisticated deadlock detection algorithm.
407 if (flags
& ISDOTDOT
) {
408 VOP_UNLOCK(dvp
, 0, p
); /* race to get the cnode */
409 retval
= hfs_getcnode(hfsmp
, dcp
->c_parentcnid
,
410 NULL
, 0, NULL
, NULL
, &tvp
);
412 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
415 if ((flags
& LOCKPARENT
) && (flags
& ISLASTCN
) && (dvp
!= tvp
) &&
416 (retval
= vn_lock(dvp
, LK_EXCLUSIVE
, p
))) {
422 VREF(dvp
); /* we want ourself, ie "." */
425 int type
= (attr
.ca_mode
& S_IFMT
);
427 if (!(flags
& ISLASTCN
) && type
!= S_IFDIR
&& type
!= S_IFLNK
) {
432 retval
= hfs_getcnode(hfsmp
, attr
.ca_fileid
,
433 &desc
, wantrsrc
, &attr
, &fork
, &tvp
);
437 if (!(flags
& LOCKPARENT
) || !(flags
& ISLASTCN
))
438 VOP_UNLOCK(dvp
, 0, p
);
443 * Insert name in cache if appropriate.
444 * - "." and ".." are not cached.
445 * - Resource fork names are not cached.
446 * - Names with composed chars are not cached.
448 if ((cnp
->cn_flags
& MAKEENTRY
)
450 && !(flags
& ISDOTDOT
)
452 && (cnp
->cn_namelen
== VTOC(*vpp
)->c_desc
.cd_namelen
)) {
453 cache_enter(dvp
, *vpp
, cnp
);
457 cat_releasedesc(&desc
);
464 * Based on vn_cache_lookup (which is vfs_cache_lookup in FreeBSD 3.1)
466 * Name caching works as follows:
468 * Names found by directory scans are retained in a cache
469 * for future reference. It is managed LRU, so frequently
470 * used names will hang around. Cache is indexed by hash value
471 * obtained from (vp, name) where vp refers to the directory
474 * If it is a "negative" entry, (i.e. for a name that is known NOT to
475 * exist) the vnode pointer will be NULL.
477 * Upon reaching the last segment of a path, if the reference
478 * is for DELETE, or NOCACHE is set (rewrite), and the
479 * name is located in the cache, it will be dropped.
486 struct vop_lookup_args
/* {
488 struct vnode **a_vpp;
489 struct componentname *a_cnp;
497 struct vnode
**vpp
= ap
->a_vpp
;
498 struct componentname
*cnp
= ap
->a_cnp
;
499 struct ucred
*cred
= cnp
->cn_cred
;
500 int flags
= cnp
->cn_flags
;
501 struct proc
*p
= cnp
->cn_proc
;
502 u_long vpid
; /* capability number of vnode */
506 lockparent
= flags
& LOCKPARENT
;
509 * Check accessiblity of directory.
511 if (dvp
->v_type
!= VDIR
)
513 if ((flags
& ISLASTCN
) && (dvp
->v_mount
->mnt_flag
& MNT_RDONLY
) &&
514 (cnp
->cn_nameiop
== DELETE
|| cnp
->cn_nameiop
== RENAME
))
516 if ((error
= VOP_ACCESS(dvp
, VEXEC
, cred
, cnp
->cn_proc
)))
520 * Lookup an entry in the cache
521 * If the lookup succeeds, the vnode is returned in *vpp, and a status of -1 is
522 * returned. If the lookup determines that the name does not exist
523 * (negative cacheing), a status of ENOENT is returned. If the lookup
524 * fails, a status of zero is returned.
526 error
= cache_lookup(dvp
, vpp
, cnp
);
527 if (error
== 0) { /* Unsuccessfull */
528 error
= hfs_lookup(ap
);
535 /* We have a name that matched */
540 * If this is a hard-link vnode then we need to update
541 * the name (of the link), the parent ID, the cnid, the
542 * text encoding and the catalog hint. This enables
543 * getattrlist calls to return the correct link info.
546 if ((flags
& ISLASTCN
) && (cp
->c_flag
& C_HARDLINK
) &&
547 ((cp
->c_parentcnid
!= VTOC(ap
->a_dvp
)->c_cnid
) ||
548 (bcmp(cnp
->cn_nameptr
, cp
->c_desc
.cd_nameptr
, cp
->c_desc
.cd_namelen
) != 0))) {
550 struct cat_desc desc
;
553 * Get an updated descriptor
555 bzero(&desc
, sizeof(desc
));
556 desc
.cd_nameptr
= cnp
->cn_nameptr
;
557 desc
.cd_namelen
= cnp
->cn_namelen
;
558 desc
.cd_parentcnid
= VTOC(ap
->a_dvp
)->c_cnid
;
559 desc
.cd_hint
= VTOC(ap
->a_dvp
)->c_childhint
;
560 if (cat_lookup(VTOHFS(vp
), &desc
, 0, &desc
, NULL
, NULL
) == 0)
561 replace_desc(cp
, &desc
);
564 if (dvp
== vp
) { /* lookup on "." */
567 } else if (flags
& ISDOTDOT
) {
569 * Carefull on the locking policy,
570 * remember we always lock from parent to child, so have
571 * to release lock on child before trying to lock parent
572 * then regain lock if needed
574 VOP_UNLOCK(dvp
, 0, p
);
575 error
= vget(vp
, LK_EXCLUSIVE
, p
);
576 if (!error
&& lockparent
&& (flags
& ISLASTCN
))
577 error
= vn_lock(dvp
, LK_EXCLUSIVE
, p
);
579 if ((flags
& ISLASTCN
) == 0 && vp
->v_type
== VREG
) {
582 cnp
->cn_consume
= forkcomponent(cnp
, &wantrsrc
);
584 /* Fork names are only for lookups */
585 if (cnp
->cn_consume
&&
586 (cnp
->cn_nameiop
!= LOOKUP
&& cnp
->cn_nameiop
!= CREATE
))
589 * We only store data forks in the name cache.
592 return (hfs_lookup(ap
));
594 error
= vget(vp
, LK_EXCLUSIVE
, p
);
595 if (!lockparent
|| error
|| !(flags
& ISLASTCN
))
596 VOP_UNLOCK(dvp
, 0, p
);
599 * Check that the capability number did not change
600 * while we were waiting for the lock.
603 if (vpid
== vp
->v_id
)
606 * The above is the NORMAL exit, after this point is an error
610 if (lockparent
&& (dvp
!= vp
) && (flags
& ISLASTCN
))
611 VOP_UNLOCK(dvp
, 0, p
);
614 if ((error
= vn_lock(dvp
, LK_EXCLUSIVE
, p
)))
617 return (hfs_lookup(ap
));
622 * forkcomponent - look for a fork suffix in the component name
626 forkcomponent(struct componentname
*cnp
, int *rsrcfork
)
628 char *suffix
= cnp
->cn_nameptr
+ cnp
->cn_namelen
;
635 * There are only 3 valid fork suffixes:
636 * "/..namedfork/rsrc"
637 * "/..namedfork/data"
640 if (bcmp(suffix
, _PATH_RSRCFORKSPEC
, sizeof(_PATH_RSRCFORKSPEC
)) == 0) {
641 consume
= sizeof(_PATH_RSRCFORKSPEC
) - 1;
643 } else if (bcmp(suffix
, _PATH_DATAFORKSPEC
, sizeof(_PATH_DATAFORKSPEC
)) == 0) {
644 consume
= sizeof(_PATH_DATAFORKSPEC
) - 1;
647 #ifdef LEGACY_FORK_NAMES
648 else if (bcmp(suffix
, LEGACY_RSRCFORKSPEC
, sizeof(LEGACY_RSRCFORKSPEC
)) == 0) {
649 consume
= sizeof(LEGACY_RSRCFORKSPEC
) - 1;