]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_lookup.c
xnu-792.tar.gz
[apple/xnu.git] / bsd / hfs / hfs_lookup.c
1 /*
2 * Copyright (c) 1999-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
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.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
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.
46 *
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
57 * SUCH DAMAGE.
58 *
59 * @(#)hfs_lookup.c 1.0
60 * derived from @(#)ufs_lookup.c 8.15 (Berkeley) 6/16/95
61 *
62 * (c) 1998-1999 Apple Computer, Inc. All Rights Reserved
63 * (c) 1990, 1992 NeXT Computer, Inc. All Rights Reserved
64 *
65 *
66 * hfs_lookup.c -- code to handle directory traversal on HFS/HFS+ volume
67 */
68
69 #include <sys/param.h>
70 #include <sys/file.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>
77
78 #include "hfs.h"
79 #include "hfs_catalog.h"
80 #include "hfs_cnode.h"
81
82 #define LEGACY_FORK_NAMES 1
83
84 static int forkcomponent(struct componentname *cnp, int *rsrcfork);
85
86 #define _PATH_DATAFORKSPEC "/..namedfork/data"
87
88 #if LEGACY_FORK_NAMES
89 #define LEGACY_RSRCFORKSPEC "/rsrc"
90 #endif
91
92 /*
93 * FROM FREEBSD 3.1
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).
98 *
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.
104 *
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.
109 *
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
112 *
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
119 * drop it
120 * else
121 * return name.
122 * return hfs_lookup()
123 *
124 * Overall outline of hfs_lookup:
125 *
126 * handle simple cases of . and ..
127 * search for name in directory, to found or notfound
128 * notfound:
129 * if creating, return locked directory, leaving info on available slots
130 * else return error
131 * found:
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
137 */
138
139
140 /*
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.
144
145 #% lookup dvp L ? ?
146 #% lookup vpp - L -
147
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;
152
153 * When should we lock parent_hp in here ??
154 */
155 static int
156 hfs_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, vfs_context_t context, int *cnode_locked)
157 {
158 struct cnode *dcp; /* cnode for directory being searched */
159 struct vnode *tvp; /* target vnode */
160 struct hfsmount *hfsmp;
161 kauth_cred_t cred;
162 struct proc *p;
163 int wantrsrc = 0;
164 int forknamelen = 0;
165 int flags;
166 int nameiop;
167 int retval = 0;
168 int isDot;
169 struct cat_desc desc;
170 struct cat_desc cndesc;
171 struct cat_attr attr;
172 struct cat_fork fork;
173 int lockflags;
174
175 dcp = VTOC(dvp);
176 hfsmp = VTOHFS(dvp);
177 *vpp = NULL;
178 *cnode_locked = 0;
179 isDot = FALSE;
180 tvp = NULL;
181 nameiop = cnp->cn_nameiop;
182 flags = cnp->cn_flags;
183 bzero(&desc, sizeof(desc));
184
185 cred = vfs_context_ucred(context);
186 p = vfs_context_proc(context);
187
188 /*
189 * First check to see if it is a . or .., else look it up.
190 */
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)) {
195 isDot = TRUE;
196 cnp->cn_flags &= ~MAKEENTRY;
197 goto found; /* We always know who we are */
198 } else {
199 /* Check fork suffix to see if we want the resource fork */
200 forknamelen = forkcomponent(cnp, &wantrsrc);
201
202 /* Resource fork names are not cached. */
203 if (wantrsrc)
204 cnp->cn_flags &= ~MAKEENTRY;
205
206 if (hfs_lock(dcp, HFS_EXCLUSIVE_LOCK) != 0) {
207 goto notfound;
208 }
209
210 /* No need to go to catalog if there are no children */
211 if (dcp->c_entries == 0) {
212 hfs_unlock(dcp);
213 goto notfound;
214 }
215
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;
221
222 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
223
224 retval = cat_lookup(hfsmp, &cndesc, wantrsrc, &desc, &attr, &fork, NULL);
225
226 hfs_systemfile_unlock(hfsmp, lockflags);
227
228 if (retval == 0) {
229 dcp->c_childhint = desc.cd_hint;
230 hfs_unlock(dcp);
231 goto found;
232 }
233 hfs_unlock(dcp);
234 notfound:
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) {
241 retval = ENOENT;
242 }
243 /*
244 * This is a non-existing entry
245 *
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.
249 */
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;
257 goto exit;
258 }
259 /*
260 * Insert name into cache (as non-existent) if appropriate.
261 *
262 * Only done for case-sensitive HFS+ volumes.
263 */
264 if ((retval == ENOENT) &&
265 (hfsmp->hfs_flags & HFS_CASE_SENSITIVE) &&
266 (cnp->cn_flags & MAKEENTRY) && nameiop != CREATE) {
267 cache_enter(dvp, NULL, cnp);
268 }
269 goto exit;
270 }
271
272 found:
273 /*
274 * Process any fork specifiers
275 */
276 if (forknamelen && S_ISREG(attr.ca_mode)) {
277 /* fork names are only for lookups */
278 if ((nameiop != LOOKUP) && (nameiop != CREATE)) {
279 retval = EPERM;
280 goto exit;
281 }
282 cnp->cn_consume = forknamelen;
283 flags |= ISLASTCN;
284 } else {
285 wantrsrc = 0;
286 forknamelen = 0;
287 }
288 if (flags & ISLASTCN) {
289 switch(nameiop) {
290 case DELETE:
291 cnp->cn_flags &= ~MAKEENTRY;
292 break;
293
294 case RENAME:
295 cnp->cn_flags &= ~MAKEENTRY;
296 if (isDot) {
297 retval = EISDIR;
298 goto exit;
299 }
300 break;
301 }
302 }
303
304 if (isDot) {
305 if ((retval = vnode_get(dvp)))
306 goto exit;
307 *vpp = dvp;
308 } else if (flags & ISDOTDOT) {
309 if ((retval = hfs_vget(hfsmp, dcp->c_parentcnid, &tvp, 0)))
310 goto exit;
311 *cnode_locked = 1;
312 *vpp = tvp;
313 } else {
314 int type = (attr.ca_mode & S_IFMT);
315
316 if (!(flags & ISLASTCN) && (type != S_IFDIR) && (type != S_IFLNK)) {
317 retval = ENOTDIR;
318 goto exit;
319 }
320
321 /* Names with composed chars are not cached. */
322 if (cnp->cn_namelen != desc.cd_namelen)
323 cnp->cn_flags &= ~MAKEENTRY;
324
325 /* Resource fork vnode names include the fork specifier. */
326 if (wantrsrc && (flags & ISLASTCN))
327 cnp->cn_namelen += forknamelen;
328
329 retval = hfs_getnewvnode(hfsmp, dvp, cnp, &desc, wantrsrc, &attr, &fork, &tvp);
330
331 if (wantrsrc && (flags & ISLASTCN))
332 cnp->cn_namelen -= forknamelen;
333
334 if (retval)
335 goto exit;
336 *cnode_locked = 1;
337 *vpp = tvp;
338 }
339 exit:
340 cat_releasedesc(&desc);
341 return (retval);
342 }
343
344
345
346 /*
347 * Name caching works as follows:
348 *
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
353 * containing name.
354 *
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.
357 *
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.
361 *
362 */
363
364 #define S_IXALL 0000111
365
366 __private_extern__
367 int
368 hfs_vnop_lookup(struct vnop_lookup_args *ap)
369 {
370 struct vnode *dvp = ap->a_dvp;
371 struct vnode *vp;
372 struct cnode *cp;
373 struct cnode *dcp;
374 int error;
375 struct vnode **vpp = ap->a_vpp;
376 struct componentname *cnp = ap->a_cnp;
377 int flags = cnp->cn_flags;
378 int cnode_locked;
379
380 *vpp = NULL;
381 dcp = VTOC(dvp);
382
383 /*
384 * Lookup an entry in the cache
385 *
386 * If the lookup succeeds, the vnode is returned in *vpp,
387 * and a status of -1 is returned.
388 *
389 * If the lookup determines that the name does not exist
390 * (negative cacheing), a status of ENOENT is returned.
391 *
392 * If the lookup fails, a status of zero is returned.
393 */
394 error = cache_lookup(dvp, vpp, cnp);
395 if (error != -1) {
396 if (error == ENOENT) /* found a negative cache entry */
397 goto exit;
398 goto lookup; /* did not find it in the cache */
399 }
400 /*
401 * We have a name that matched
402 * cache_lookup returns the vp with an iocount reference already taken
403 */
404 error = 0;
405 vp = *vpp;
406
407 /*
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.
412 */
413 cp = VTOC(vp);
414
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;
420 int lockflags;
421
422 /*
423 * Get an updated descriptor
424 */
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;
430
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);
435 }
436 hfs_unlock(cp);
437 }
438 if (dvp != vp && !(flags & ISDOTDOT)) {
439 if ((flags & ISLASTCN) == 0 && vnode_isreg(vp)) {
440 int wantrsrc = 0;
441
442 cnp->cn_consume = forkcomponent(cnp, &wantrsrc);
443 if (cnp->cn_consume) {
444 flags |= ISLASTCN;
445 /* Fork names are only for lookups */
446 if (cnp->cn_nameiop != LOOKUP &&
447 cnp->cn_nameiop != CREATE) {
448 vnode_put(vp);
449 error = EPERM;
450 goto exit;
451 }
452 }
453 /*
454 * Use cnode's rsrcfork vnode if possible.
455 */
456 if (wantrsrc) {
457 int vid;
458
459 *vpp = NULL;
460
461 if (cp->c_rsrc_vp == NULL) {
462 vnode_put(vp);
463 goto lookup;
464 }
465 vid = vnode_vid(cp->c_rsrc_vp);
466
467 error = vnode_getwithvid(cp->c_rsrc_vp, vid);
468 if (error) {
469 vnode_put(vp);
470 goto lookup;
471 }
472 *vpp = cp->c_rsrc_vp;
473 vnode_put(vp);
474 vp = *vpp;
475 }
476 }
477 }
478 return (error);
479
480 lookup:
481 /*
482 * The vnode was not in the name cache or it was stale.
483 *
484 * So we need to do a real lookup.
485 */
486 cnode_locked = 0;
487
488 error = hfs_lookup(dvp, vpp, cnp, ap->a_context, &cnode_locked);
489
490 if (cnode_locked)
491 hfs_unlock(VTOC(*vpp));
492 exit:
493 return (error);
494 }
495
496
497 /*
498 * forkcomponent - look for a fork suffix in the component name
499 *
500 */
501 static int
502 forkcomponent(struct componentname *cnp, int *rsrcfork)
503 {
504 char *suffix = cnp->cn_nameptr + cnp->cn_namelen;
505 int consume = 0;
506
507 *rsrcfork = 0;
508 if (*suffix == '\0')
509 return (0);
510 /*
511 * There are only 3 valid fork suffixes:
512 * "/..namedfork/rsrc"
513 * "/..namedfork/data"
514 * "/rsrc" (legacy)
515 */
516 if (bcmp(suffix, _PATH_RSRCFORKSPEC, sizeof(_PATH_RSRCFORKSPEC)) == 0) {
517 consume = sizeof(_PATH_RSRCFORKSPEC) - 1;
518 *rsrcfork = 1;
519 } else if (bcmp(suffix, _PATH_DATAFORKSPEC, sizeof(_PATH_DATAFORKSPEC)) == 0) {
520 consume = sizeof(_PATH_DATAFORKSPEC) - 1;
521 }
522
523 #if LEGACY_FORK_NAMES
524 else if (bcmp(suffix, LEGACY_RSRCFORKSPEC, sizeof(LEGACY_RSRCFORKSPEC)) == 0) {
525 consume = sizeof(LEGACY_RSRCFORKSPEC) - 1;
526 *rsrcfork = 1;
527 printf("HFS: /rsrc paths are deprecated (%s)\n", cnp->cn_nameptr);
528 }
529 #endif
530 return (consume);
531 }
532