]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_lookup.c
6009fb787c705d88504dde14a346eae197debbc9
[apple/xnu.git] / bsd / hfs / hfs_lookup.c
1 /*
2 * Copyright (c) 1999-2007 Apple 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/kdebug.h>
81 #include <sys/kauth.h>
82 #include <sys/namei.h>
83
84 #include "hfs.h"
85 #include "hfs_catalog.h"
86 #include "hfs_cnode.h"
87
88
89 /*
90 * FROM FREEBSD 3.1
91 * Convert a component of a pathname into a pointer to a locked cnode.
92 * This is a very central and rather complicated routine.
93 * If the file system is not maintained in a strict tree hierarchy,
94 * this can result in a deadlock situation (see comments in code below).
95 *
96 * The cnp->cn_nameiop argument is LOOKUP, CREATE, RENAME, or DELETE depending
97 * on whether the name is to be looked up, created, renamed, or deleted.
98 * When CREATE, RENAME, or DELETE is specified, information usable in
99 * creating, renaming, or deleting a directory entry may be calculated.
100 * Notice that these are the only operations that can affect the directory of the target.
101 *
102 * LOCKPARENT and WANTPARENT actually refer to the parent of the last item,
103 * so if ISLASTCN is not set, they should be ignored. Also they are mutually exclusive, or
104 * WANTPARENT really implies DONTLOCKPARENT. Either of them set means that the calling
105 * routine wants to access the parent of the target, locked or unlocked.
106 *
107 * Keeping the parent locked as long as possible protects from other processes
108 * looking up the same item, so it has to be locked until the cnode is totally finished
109 *
110 * hfs_cache_lookup() performs the following for us:
111 * check that it is a directory
112 * check accessibility of directory
113 * check for modification attempts on read-only mounts
114 * if name found in cache
115 * if at end of path and deleting or creating
116 * drop it
117 * else
118 * return name.
119 * return hfs_lookup()
120 *
121 * Overall outline of hfs_lookup:
122 *
123 * handle simple cases of . and ..
124 * search for name in directory, to found or notfound
125 * notfound:
126 * if creating, return locked directory, leaving info on available slots
127 * else return error
128 * found:
129 * if at end of path and deleting, return information to allow delete
130 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
131 * cnode and return info to allow rewrite
132 * if not at end, add name to cache; if at end and neither creating
133 * nor deleting, add name to cache
134 */
135
136
137 /*
138 * Lookup *cnp in directory *dvp, return it in *vpp.
139 * **vpp is held on exit.
140 * We create a cnode for the file, but we do NOT open the file here.
141
142 #% lookup dvp L ? ?
143 #% lookup vpp - L -
144
145 IN struct vnode *dvp - Parent node of file;
146 INOUT struct vnode **vpp - node of target file, its a new node if
147 the target vnode did not exist;
148 IN struct componentname *cnp - Name of file;
149
150 * When should we lock parent_hp in here ??
151 */
152 static int
153 hfs_lookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, int *cnode_locked)
154 {
155 struct cnode *dcp; /* cnode for directory being searched */
156 struct vnode *tvp; /* target vnode */
157 struct hfsmount *hfsmp;
158 int flags;
159 int nameiop;
160 int retval = 0;
161 int isDot;
162 struct cat_desc desc;
163 struct cat_desc cndesc;
164 struct cat_attr attr;
165 struct cat_fork fork;
166 int lockflags;
167
168 retry:
169 dcp = NULL;
170 hfsmp = VTOHFS(dvp);
171 *vpp = NULL;
172 *cnode_locked = 0;
173 isDot = FALSE;
174 tvp = NULL;
175 nameiop = cnp->cn_nameiop;
176 flags = cnp->cn_flags;
177 bzero(&desc, sizeof(desc));
178
179 /*
180 * First check to see if it is a . or .., else look it up.
181 */
182 if (flags & ISDOTDOT) { /* Wanting the parent */
183 cnp->cn_flags &= ~MAKEENTRY;
184 goto found; /* .. is always defined */
185 } else if ((cnp->cn_nameptr[0] == '.') && (cnp->cn_namelen == 1)) {
186 isDot = TRUE;
187 cnp->cn_flags &= ~MAKEENTRY;
188 goto found; /* We always know who we are */
189 } else {
190 if (hfs_lock(VTOC(dvp), HFS_EXCLUSIVE_LOCK) != 0) {
191 retval = ENOENT; /* The parent no longer exists ? */
192 goto exit;
193 }
194 dcp = VTOC(dvp);
195
196 if (dcp->c_flag & C_DIR_MODIFICATION) {
197 // XXXdbg - if we could msleep on a lck_rw_t then we would do that
198 // but since we can't we have to unlock, delay for a bit
199 // and then retry...
200 // msleep((caddr_t)&dcp->c_flag, &dcp->c_rwlock, PINOD, "hfs_vnop_lookup", 0);
201 hfs_unlock(dcp);
202 tsleep((caddr_t)dvp, PRIBIO, "hfs_lookup", 1);
203
204 goto retry;
205 }
206
207 /* No need to go to catalog if there are no children */
208 if (dcp->c_entries == 0) {
209 goto notfound;
210 }
211
212 bzero(&cndesc, sizeof(cndesc));
213 cndesc.cd_nameptr = (const u_int8_t *)cnp->cn_nameptr;
214 cndesc.cd_namelen = cnp->cn_namelen;
215 cndesc.cd_parentcnid = dcp->c_fileid;
216 cndesc.cd_hint = dcp->c_childhint;
217
218 lockflags = hfs_systemfile_lock(hfsmp, SFL_CATALOG, HFS_SHARED_LOCK);
219
220 retval = cat_lookup(hfsmp, &cndesc, 0, &desc, &attr, &fork, NULL);
221
222 hfs_systemfile_unlock(hfsmp, lockflags);
223
224 if (retval == 0) {
225 dcp->c_childhint = desc.cd_hint;
226 /*
227 * Note: We must drop the parent lock here before calling
228 * hfs_getnewvnode (which takes the child lock).
229 */
230 hfs_unlock(dcp);
231 dcp = NULL;
232 goto found;
233 }
234 notfound:
235 /*
236 * ENAMETOOLONG supersedes other errors
237 *
238 * For a CREATE or RENAME operation on the last component
239 * the ENAMETOOLONG will be handled in the next VNOP.
240 */
241 if ((retval != ENAMETOOLONG) &&
242 (cnp->cn_namelen > kHFSPlusMaxFileNameChars) &&
243 (((flags & ISLASTCN) == 0) || ((nameiop != CREATE) && (nameiop != RENAME)))) {
244 retval = ENAMETOOLONG;
245 } else if (retval == 0) {
246 retval = ENOENT;
247 }
248 if (retval != ENOENT)
249 goto exit;
250 /*
251 * This is a non-existing entry
252 *
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.
256 */
257 if ((nameiop == CREATE || nameiop == RENAME ||
258 (nameiop == DELETE &&
259 (cnp->cn_flags & DOWHITEOUT) &&
260 (cnp->cn_flags & ISWHITEOUT))) &&
261 (flags & ISLASTCN) &&
262 !(ISSET(dcp->c_flag, C_DELETED | C_NOEXISTS))) {
263 retval = EJUSTRETURN;
264 goto exit;
265 }
266 /*
267 * Insert name into the name cache (as non-existent).
268 */
269 if ((hfsmp->hfs_flags & HFS_STANDARD) == 0 &&
270 (cnp->cn_flags & MAKEENTRY) &&
271 (nameiop != CREATE)) {
272 cache_enter(dvp, NULL, cnp);
273 dcp->c_flag |= C_NEG_ENTRIES;
274 }
275 goto exit;
276 }
277
278 found:
279 if (flags & ISLASTCN) {
280 switch(nameiop) {
281 case DELETE:
282 cnp->cn_flags &= ~MAKEENTRY;
283 break;
284
285 case RENAME:
286 cnp->cn_flags &= ~MAKEENTRY;
287 if (isDot) {
288 retval = EISDIR;
289 goto exit;
290 }
291 break;
292 }
293 }
294
295 if (isDot) {
296 if ((retval = vnode_get(dvp)))
297 goto exit;
298 *vpp = dvp;
299 } else if (flags & ISDOTDOT) {
300 /*
301 * Directory hard links can have multiple parents so
302 * find the appropriate parent for the current thread.
303 */
304 if ((retval = hfs_vget(hfsmp, hfs_currentparent(VTOC(dvp)), &tvp, 0))) {
305 goto exit;
306 }
307 *cnode_locked = 1;
308 *vpp = tvp;
309 } else {
310 int type = (attr.ca_mode & S_IFMT);
311 #if NAMEDRSRCFORK
312 int rsrc_warn = 0;
313
314 /*
315 * Check if caller wants the resource fork but utilized
316 * the legacy "file/rsrc" access path.
317 *
318 * This is deprecated behavior and support for it will not
319 * be allowed beyond case insensitive HFS+ and even that
320 * support will be removed in the next major OS release.
321 */
322 if ((type == S_IFREG) &&
323 ((flags & ISLASTCN) == 0) &&
324 (cnp->cn_nameptr[cnp->cn_namelen] == '/') &&
325 (bcmp(&cnp->cn_nameptr[cnp->cn_namelen+1], "rsrc", 5) == 0) &&
326 ((hfsmp->hfs_flags & (HFS_STANDARD | HFS_CASE_SENSITIVE)) == 0)) {
327
328 cnp->cn_consume = 5;
329 cnp->cn_flags |= CN_WANTSRSRCFORK | ISLASTCN | NOCACHE;
330 cnp->cn_flags &= ~MAKEENTRY;
331 flags |= ISLASTCN;
332 rsrc_warn = 1;
333 }
334 #endif
335 if (!(flags & ISLASTCN) && (type != S_IFDIR) && (type != S_IFLNK)) {
336 retval = ENOTDIR;
337 goto exit;
338 }
339 /* Don't cache directory hardlink names. */
340 if (attr.ca_recflags & kHFSHasLinkChainMask) {
341 cnp->cn_flags &= ~MAKEENTRY;
342 }
343 /* Names with composed chars are not cached. */
344 if (cnp->cn_namelen != desc.cd_namelen)
345 cnp->cn_flags &= ~MAKEENTRY;
346
347 retval = hfs_getnewvnode(hfsmp, dvp, cnp, &desc, 0, &attr, &fork, &tvp);
348
349 if (retval) {
350 /*
351 * If this was a create operation lookup and another
352 * process removed the object before we had a chance
353 * to create the vnode, then just treat it as the not
354 * found case above and return EJUSTRETURN.
355 */
356 if ((retval == ENOENT) &&
357 (cnp->cn_nameiop == CREATE) &&
358 (flags & ISLASTCN)) {
359 retval = EJUSTRETURN;
360 }
361 goto exit;
362 }
363
364 /*
365 * Save the origin info for file and directory hardlinks. Directory hardlinks
366 * need the origin for '..' lookups, and file hardlinks need it to ensure that
367 * competing lookups do not cause us to vend different hardlinks than the ones requested.
368 * We want to restrict saving the cache entries to LOOKUP namei operations, since
369 * we're really doing this to protect getattr.
370 */
371 if ((cnp->cn_nameiop == LOOKUP) && (VTOC(tvp)->c_flag & C_HARDLINK)) {
372 hfs_savelinkorigin(VTOC(tvp), VTOC(dvp)->c_fileid);
373 }
374 *cnode_locked = 1;
375 *vpp = tvp;
376 #if NAMEDRSRCFORK
377 if (rsrc_warn) {
378 if ((VTOC(tvp)->c_flag & C_WARNED_RSRC) == 0) {
379 VTOC(tvp)->c_flag |= C_WARNED_RSRC;
380 printf("%.200s: file access by '/rsrc' was deprecated in 10.4\n",
381 cnp->cn_nameptr);
382 }
383 }
384 #endif
385 }
386 exit:
387 if (dcp) {
388 hfs_unlock(dcp);
389 }
390 cat_releasedesc(&desc);
391 return (retval);
392 }
393
394
395
396 /*
397 * Name caching works as follows:
398 *
399 * Names found by directory scans are retained in a cache
400 * for future reference. It is managed LRU, so frequently
401 * used names will hang around. Cache is indexed by hash value
402 * obtained from (vp, name) where vp refers to the directory
403 * containing name.
404 *
405 * If it is a "negative" entry, (i.e. for a name that is known NOT to
406 * exist) the vnode pointer will be NULL.
407 *
408 * Upon reaching the last segment of a path, if the reference
409 * is for DELETE, or NOCACHE is set (rewrite), and the
410 * name is located in the cache, it will be dropped.
411 *
412 */
413
414 #define S_IXALL 0000111
415
416 __private_extern__
417 int
418 hfs_vnop_lookup(struct vnop_lookup_args *ap)
419 {
420 struct vnode *dvp = ap->a_dvp;
421 struct vnode *vp;
422 struct cnode *cp;
423 struct cnode *dcp;
424 int error;
425 struct vnode **vpp = ap->a_vpp;
426 struct componentname *cnp = ap->a_cnp;
427 int flags = cnp->cn_flags;
428 int cnode_locked;
429
430 *vpp = NULL;
431 dcp = VTOC(dvp);
432
433 /*
434 * Lookup an entry in the cache
435 *
436 * If the lookup succeeds, the vnode is returned in *vpp,
437 * and a status of -1 is returned.
438 *
439 * If the lookup determines that the name does not exist
440 * (negative cacheing), a status of ENOENT is returned.
441 *
442 * If the lookup fails, a status of zero is returned.
443 */
444 error = cache_lookup(dvp, vpp, cnp);
445 if (error != -1) {
446 if ((error == ENOENT) && (cnp->cn_nameiop != CREATE))
447 goto exit; /* found a negative cache entry */
448 goto lookup; /* did not find it in the cache */
449 }
450 /*
451 * We have a name that matched
452 * cache_lookup returns the vp with an iocount reference already taken
453 */
454 error = 0;
455 vp = *vpp;
456
457 /*
458 * If this is a hard-link vnode then we need to update
459 * the name (of the link), the parent ID, the cnid, the
460 * text encoding and the catalog hint. This enables
461 * getattrlist calls to return the correct link info.
462 */
463 cp = VTOC(vp);
464
465 if ((flags & ISLASTCN) && (cp->c_flag & C_HARDLINK)) {
466 hfs_lock(cp, HFS_FORCE_LOCK);
467 if ((cp->c_parentcnid != dcp->c_cnid) ||
468 (bcmp(cnp->cn_nameptr, cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen) != 0)) {
469 struct cat_desc desc;
470 int lockflags;
471
472 /*
473 * Get an updated descriptor
474 */
475 desc.cd_nameptr = (const u_int8_t *)cnp->cn_nameptr;
476 desc.cd_namelen = cnp->cn_namelen;
477 desc.cd_parentcnid = dcp->c_fileid;
478 desc.cd_hint = dcp->c_childhint;
479 desc.cd_encoding = 0;
480 desc.cd_cnid = 0;
481 desc.cd_flags = S_ISDIR(cp->c_mode) ? CD_ISDIR : 0;
482
483 lockflags = hfs_systemfile_lock(VTOHFS(dvp), SFL_CATALOG, HFS_SHARED_LOCK);
484 if (cat_lookup(VTOHFS(vp), &desc, 0, &desc, NULL, NULL, NULL) == 0)
485 replace_desc(cp, &desc);
486 hfs_systemfile_unlock(VTOHFS(dvp), lockflags);
487 }
488
489 /* Save the lookup result in the origin list for future lookups, but
490 * only if it was through a LOOKUP nameiop
491 */
492 if (cnp->cn_nameiop == LOOKUP) {
493 hfs_savelinkorigin(cp, dcp->c_fileid);
494 }
495
496 hfs_unlock(cp);
497 }
498 #if NAMEDRSRCFORK
499 /*
500 * Check if caller wants the resource fork but utilized
501 * the legacy "file/rsrc" access path.
502 *
503 * This is deprecated behavior and support for it will not
504 * be allowed beyond case insensitive HFS+ and even that
505 * support will be removed in the next major OS release.
506 */
507 if ((dvp != vp) &&
508 ((flags & ISLASTCN) == 0) &&
509 vnode_isreg(vp) &&
510 (cnp->cn_nameptr[cnp->cn_namelen] == '/') &&
511 (bcmp(&cnp->cn_nameptr[cnp->cn_namelen+1], "rsrc", 5) == 0) &&
512 ((VTOHFS(vp)->hfs_flags & (HFS_STANDARD | HFS_CASE_SENSITIVE)) == 0)) {
513 cnp->cn_consume = 5;
514 cnp->cn_flags |= CN_WANTSRSRCFORK | ISLASTCN | NOCACHE;
515 cnp->cn_flags &= ~MAKEENTRY;
516
517 hfs_lock(cp, HFS_FORCE_LOCK);
518 if ((cp->c_flag & C_WARNED_RSRC) == 0) {
519 cp->c_flag |= C_WARNED_RSRC;
520 printf("%.200s: file access by '/rsrc' was deprecated in 10.4\n", cnp->cn_nameptr);
521 }
522 hfs_unlock(cp);
523 }
524 #endif
525 return (error);
526
527 lookup:
528 /*
529 * The vnode was not in the name cache or it was stale.
530 *
531 * So we need to do a real lookup.
532 */
533 cnode_locked = 0;
534
535 error = hfs_lookup(dvp, vpp, cnp, &cnode_locked);
536
537 if (cnode_locked)
538 hfs_unlock(VTOC(*vpp));
539 exit:
540 return (error);
541 }
542
543