]> git.saurik.com Git - apple/xnu.git/blame_incremental - bsd/hfs/hfs_lookup.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_lookup.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1999-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
73 */
74
75#include <sys/param.h>
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>
81#include <sys/kdebug.h>
82#include <sys/kauth.h>
83
84#include "hfs.h"
85#include "hfs_catalog.h"
86#include "hfs_cnode.h"
87
88#define LEGACY_FORK_NAMES 1
89
90static int forkcomponent(struct componentname *cnp, int *rsrcfork);
91
92#define _PATH_DATAFORKSPEC "/..namedfork/data"
93
94#if LEGACY_FORK_NAMES
95#define LEGACY_RSRCFORKSPEC "/rsrc"
96#endif
97
98/*
99 * FROM FREEBSD 3.1
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).
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 *
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
117 * looking up the same item, so it has to be locked until the cnode is totally finished
118 *
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.
128 * return hfs_lookup()
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
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
143 */
144
145
146/*
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.
150
151#% lookup dvp L ? ?
152#% lookup vpp - L -
153
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;
158
159 * When should we lock parent_hp in here ??
160 */
161static int
162hfs_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, vfs_context_t context, int *cnode_locked)
163{
164 struct cnode *dcp; /* cnode for directory being searched */
165 struct vnode *tvp; /* target vnode */
166 struct hfsmount *hfsmp;
167 kauth_cred_t cred;
168 struct proc *p;
169 int wantrsrc = 0;
170 int forknamelen = 0;
171 int flags;
172 int nameiop;
173 int retval = 0;
174 int isDot;
175 struct cat_desc desc;
176 struct cat_desc cndesc;
177 struct cat_attr attr;
178 struct cat_fork fork;
179 int lockflags;
180
181 dcp = VTOC(dvp);
182 hfsmp = VTOHFS(dvp);
183 *vpp = NULL;
184 *cnode_locked = 0;
185 isDot = FALSE;
186 tvp = NULL;
187 nameiop = cnp->cn_nameiop;
188 flags = cnp->cn_flags;
189 bzero(&desc, sizeof(desc));
190
191 cred = vfs_context_ucred(context);
192 p = vfs_context_proc(context);
193
194 /*
195 * First check to see if it is a . or .., else look it up.
196 */
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)) {
201 isDot = TRUE;
202 cnp->cn_flags &= ~MAKEENTRY;
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);
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 }
215
216 /* No need to go to catalog if there are no children */
217 if (dcp->c_entries == 0) {
218 hfs_unlock(dcp);
219 goto notfound;
220 }
221
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;
227
228 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
229
230 retval = cat_lookup(hfsmp, &cndesc, wantrsrc, &desc, &attr, &fork, NULL);
231
232 hfs_systemfile_unlock(hfsmp, lockflags);
233
234 if (retval == 0) {
235 dcp->c_childhint = desc.cd_hint;
236 hfs_unlock(dcp);
237 goto found;
238 }
239 hfs_unlock(dcp);
240notfound:
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 }
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 &&
258 (cnp->cn_flags & DOWHITEOUT) &&
259 (cnp->cn_flags & ISWHITEOUT))) &&
260 (flags & ISLASTCN) &&
261 (retval == ENOENT)) {
262 retval = EJUSTRETURN;
263 goto exit;
264 }
265 /*
266 * Insert name into cache (as non-existent) if appropriate.
267 *
268 * Only done for case-sensitive HFS+ volumes.
269 */
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 }
275 goto exit;
276 }
277
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 }
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;
304 goto exit;
305 }
306 break;
307 }
308 }
309
310 if (isDot) {
311 if ((retval = vnode_get(dvp)))
312 goto exit;
313 *vpp = dvp;
314 } else if (flags & ISDOTDOT) {
315 if ((retval = hfs_vget(hfsmp, dcp->c_parentcnid, &tvp, 0)))
316 goto exit;
317 *cnode_locked = 1;
318 *vpp = tvp;
319 } else {
320 int type = (attr.ca_mode & S_IFMT);
321
322 if (!(flags & ISLASTCN) && (type != S_IFDIR) && (type != S_IFLNK)) {
323 retval = ENOTDIR;
324 goto exit;
325 }
326
327 /* Names with composed chars are not cached. */
328 if (cnp->cn_namelen != desc.cd_namelen)
329 cnp->cn_flags &= ~MAKEENTRY;
330
331 /* Resource fork vnode names include the fork specifier. */
332 if (wantrsrc && (flags & ISLASTCN))
333 cnp->cn_namelen += forknamelen;
334
335 retval = hfs_getnewvnode(hfsmp, dvp, cnp, &desc, wantrsrc, &attr, &fork, &tvp);
336
337 if (wantrsrc && (flags & ISLASTCN))
338 cnp->cn_namelen -= forknamelen;
339
340 if (retval)
341 goto exit;
342 *cnode_locked = 1;
343 *vpp = tvp;
344 }
345exit:
346 cat_releasedesc(&desc);
347 return (retval);
348}
349
350
351
352/*
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 *
368 */
369
370#define S_IXALL 0000111
371
372__private_extern__
373int
374hfs_vnop_lookup(struct vnop_lookup_args *ap)
375{
376 struct vnode *dvp = ap->a_dvp;
377 struct vnode *vp;
378 struct cnode *cp;
379 struct cnode *dcp;
380 int error;
381 struct vnode **vpp = ap->a_vpp;
382 struct componentname *cnp = ap->a_cnp;
383 int flags = cnp->cn_flags;
384 int cnode_locked;
385
386 *vpp = NULL;
387 dcp = VTOC(dvp);
388
389 /*
390 * Lookup an entry in the cache
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.
399 */
400 error = cache_lookup(dvp, vpp, cnp);
401 if (error != -1) {
402 if (error == ENOENT) /* found a negative cache entry */
403 goto exit;
404 goto lookup; /* did not find it in the cache */
405 }
406 /*
407 * We have a name that matched
408 * cache_lookup returns the vp with an iocount reference already taken
409 */
410 error = 0;
411 vp = *vpp;
412
413 /*
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.
418 */
419 cp = VTOC(vp);
420
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;
427
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)) {
446 int wantrsrc = 0;
447
448 cnp->cn_consume = forkcomponent(cnp, &wantrsrc);
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) {
454 vnode_put(vp);
455 error = EPERM;
456 goto exit;
457 }
458 }
459 /*
460 * Use cnode's rsrcfork vnode if possible.
461 */
462 if (wantrsrc) {
463 int vid;
464
465 *vpp = NULL;
466
467 if (cp->c_rsrc_vp == NULL) {
468 vnode_put(vp);
469 goto lookup;
470 }
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;
481 }
482 }
483 }
484 return (error);
485
486lookup:
487 /*
488 * The vnode was not in the name cache or it was stale.
489 *
490 * So we need to do a real lookup.
491 */
492 cnode_locked = 0;
493
494 error = hfs_lookup(dvp, vpp, cnp, ap->a_context, &cnode_locked);
495
496 if (cnode_locked)
497 hfs_unlock(VTOC(*vpp));
498exit:
499 return (error);
500}
501
502
503/*
504 * forkcomponent - look for a fork suffix in the component name
505 *
506 */
507static int
508forkcomponent(struct componentname *cnp, int *rsrcfork)
509{
510 char *suffix = cnp->cn_nameptr + cnp->cn_namelen;
511 int consume = 0;
512
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;
527 }
528
529#if LEGACY_FORK_NAMES
530 else if (bcmp(suffix, LEGACY_RSRCFORKSPEC, sizeof(LEGACY_RSRCFORKSPEC)) == 0) {
531 consume = sizeof(LEGACY_RSRCFORKSPEC) - 1;
532 *rsrcfork = 1;
533 printf("HFS: /rsrc paths are deprecated (%s)\n", cnp->cn_nameptr);
534 }
535#endif
536 return (consume);
537}
538