]> git.saurik.com Git - apple/xnu.git/blob - bsd/hfs/hfs_chash.c
997d247ae1acc47a727293729d958b3393dc1ef7
[apple/xnu.git] / bsd / hfs / hfs_chash.c
1 /*
2 * Copyright (c) 2002-2008 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 /*
30 * Copyright (c) 1982, 1986, 1989, 1991, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)hfs_chash.c
62 * derived from @(#)ufs_ihash.c 8.7 (Berkeley) 5/17/95
63 */
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/vnode.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
70 #include <sys/proc.h>
71 #include <sys/queue.h>
72
73
74 #include "hfs.h" /* XXX bringup */
75 #include "hfs_cnode.h"
76
77 extern lck_attr_t * hfs_lock_attr;
78 extern lck_grp_t * hfs_mutex_group;
79 extern lck_grp_t * hfs_rwlock_group;
80
81 lck_grp_t * chash_lck_grp;
82 lck_grp_attr_t * chash_lck_grp_attr;
83 lck_attr_t * chash_lck_attr;
84
85
86 #define CNODEHASH(hfsmp, inum) (&hfsmp->hfs_cnodehashtbl[(inum) & hfsmp->hfs_cnodehash])
87
88
89 /*
90 * Initialize cnode hash table.
91 */
92 __private_extern__
93 void
94 hfs_chashinit()
95 {
96 chash_lck_grp_attr= lck_grp_attr_alloc_init();
97 chash_lck_grp = lck_grp_alloc_init("cnode_hash", chash_lck_grp_attr);
98 chash_lck_attr = lck_attr_alloc_init();
99 }
100
101 static void hfs_chash_lock(struct hfsmount *hfsmp)
102 {
103 lck_mtx_lock(&hfsmp->hfs_chash_mutex);
104 }
105
106 static void hfs_chash_lock_spin(struct hfsmount *hfsmp)
107 {
108 lck_mtx_lock_spin(&hfsmp->hfs_chash_mutex);
109 }
110
111 #ifdef i386
112 static void hfs_chash_lock_convert (struct hfsmount *hfsmp)
113 #else
114 static void hfs_chash_lock_convert (__unused struct hfsmount *hfsmp)
115 #endif
116 {
117 lck_mtx_convert_spin(&hfsmp->hfs_chash_mutex);
118 }
119
120 static void hfs_chash_unlock(struct hfsmount *hfsmp)
121 {
122 lck_mtx_unlock(&hfsmp->hfs_chash_mutex);
123 }
124
125 __private_extern__
126 void
127 hfs_chashinit_finish(struct hfsmount *hfsmp)
128 {
129 lck_mtx_init(&hfsmp->hfs_chash_mutex, chash_lck_grp, chash_lck_attr);
130
131 hfsmp->hfs_cnodehashtbl = hashinit(desiredvnodes / 4, M_HFSMNT, &hfsmp->hfs_cnodehash);
132 }
133
134 __private_extern__
135 void
136 hfs_delete_chash(struct hfsmount *hfsmp)
137 {
138 lck_mtx_destroy(&hfsmp->hfs_chash_mutex, chash_lck_grp);
139
140 FREE(hfsmp->hfs_cnodehashtbl, M_HFSMNT);
141 }
142
143
144 /*
145 * Use the device, inum pair to find the incore cnode.
146 *
147 * If it is in core, but locked, wait for it.
148 */
149 struct vnode *
150 hfs_chash_getvnode(struct hfsmount *hfsmp, ino_t inum, int wantrsrc, int skiplock, int allow_deleted)
151 {
152 struct cnode *cp;
153 struct vnode *vp;
154 int error;
155 u_int32_t vid;
156
157 /*
158 * Go through the hash list
159 * If a cnode is in the process of being cleaned out or being
160 * allocated, wait for it to be finished and then try again.
161 */
162 loop:
163 hfs_chash_lock_spin(hfsmp);
164
165 for (cp = CNODEHASH(hfsmp, inum)->lh_first; cp; cp = cp->c_hash.le_next) {
166 if (cp->c_fileid != inum)
167 continue;
168 /* Wait if cnode is being created or reclaimed. */
169 if (ISSET(cp->c_hflag, H_ALLOC | H_TRANSIT | H_ATTACH)) {
170 SET(cp->c_hflag, H_WAITING);
171
172 (void) msleep(cp, &hfsmp->hfs_chash_mutex, PDROP | PINOD,
173 "hfs_chash_getvnode", 0);
174 goto loop;
175 }
176 /* Obtain the desired vnode. */
177 vp = wantrsrc ? cp->c_rsrc_vp : cp->c_vp;
178 if (vp == NULLVP)
179 goto exit;
180
181 vid = vnode_vid(vp);
182 hfs_chash_unlock(hfsmp);
183
184 if ((error = vnode_getwithvid(vp, vid))) {
185 /*
186 * If vnode is being reclaimed, or has
187 * already changed identity, no need to wait
188 */
189 return (NULL);
190 }
191 if (!skiplock && hfs_lock(cp, HFS_EXCLUSIVE_LOCK) != 0) {
192 vnode_put(vp);
193 return (NULL);
194 }
195
196 /*
197 * Skip cnodes that are not in the name space anymore
198 * we need to check with the cnode lock held because
199 * we may have blocked acquiring the vnode ref or the
200 * lock on the cnode which would allow the node to be
201 * unlinked
202 */
203 if (!allow_deleted) {
204 if (cp->c_flag & (C_NOEXISTS | C_DELETED)) {
205 if (!skiplock)
206 hfs_unlock(cp);
207 vnode_put(vp);
208
209 return (NULL);
210 }
211 }
212 return (vp);
213 }
214 exit:
215 hfs_chash_unlock(hfsmp);
216 return (NULL);
217 }
218
219
220 /*
221 * Use the device, fileid pair to snoop an incore cnode.
222 *
223 * A cnode can exists in chash even after it has been
224 * deleted from the catalog, so this function returns
225 * ENOENT if C_NOEXIST is set in the cnode's flag.
226 *
227 */
228 int
229 hfs_chash_snoop(struct hfsmount *hfsmp, ino_t inum, int (*callout)(const struct cat_desc *,
230 const struct cat_attr *, void *), void * arg)
231 {
232 struct cnode *cp;
233 int result = ENOENT;
234
235 /*
236 * Go through the hash list
237 * If a cnode is in the process of being cleaned out or being
238 * allocated, wait for it to be finished and then try again.
239 */
240 hfs_chash_lock(hfsmp);
241
242 for (cp = CNODEHASH(hfsmp, inum)->lh_first; cp; cp = cp->c_hash.le_next) {
243 if (cp->c_fileid != inum)
244 continue;
245 /* Skip cnodes that have been removed from the catalog */
246 if (cp->c_flag & (C_NOEXISTS | C_DELETED)) {
247 break;
248 }
249 /* Skip cnodes being created or reclaimed. */
250 if (!ISSET(cp->c_hflag, H_ALLOC | H_TRANSIT | H_ATTACH)) {
251 result = callout(&cp->c_desc, &cp->c_attr, arg);
252 }
253 break;
254 }
255 hfs_chash_unlock(hfsmp);
256
257 return (result);
258 }
259
260
261 /*
262 * Use the device, fileid pair to find the incore cnode.
263 * If no cnode if found one is created
264 *
265 * If it is in core, but locked, wait for it.
266 *
267 * If the cnode is C_DELETED, then return NULL since that
268 * inum is no longer valid for lookups (open-unlinked file).
269 *
270 * If the cnode is C_DELETED but also marked C_RENAMED, then that means
271 * the cnode was renamed over and a new entry exists in its place. The caller
272 * should re-drive the lookup to get the newer entry. In that case, we'll still
273 * return NULL for the cnode, but also return GNV_CHASH_RENAMED in the output flags
274 * of this function to indicate the caller that they should re-drive.
275 */
276 struct cnode *
277 hfs_chash_getcnode(struct hfsmount *hfsmp, ino_t inum, struct vnode **vpp,
278 int wantrsrc, int skiplock, int *out_flags, int *hflags)
279 {
280 struct cnode *cp;
281 struct cnode *ncp = NULL;
282 vnode_t vp;
283 u_int32_t vid;
284
285 /*
286 * Go through the hash list
287 * If a cnode is in the process of being cleaned out or being
288 * allocated, wait for it to be finished and then try again.
289 */
290 loop:
291 hfs_chash_lock_spin(hfsmp);
292
293 loop_with_lock:
294 for (cp = CNODEHASH(hfsmp, inum)->lh_first; cp; cp = cp->c_hash.le_next) {
295 if (cp->c_fileid != inum)
296 continue;
297 /*
298 * Wait if cnode is being created, attached to or reclaimed.
299 */
300 if (ISSET(cp->c_hflag, H_ALLOC | H_ATTACH | H_TRANSIT)) {
301 SET(cp->c_hflag, H_WAITING);
302
303 (void) msleep(cp, &hfsmp->hfs_chash_mutex, PINOD,
304 "hfs_chash_getcnode", 0);
305 goto loop_with_lock;
306 }
307 vp = wantrsrc ? cp->c_rsrc_vp : cp->c_vp;
308 if (vp == NULL) {
309 /*
310 * The desired vnode isn't there so tag the cnode.
311 */
312 SET(cp->c_hflag, H_ATTACH);
313 *hflags |= H_ATTACH;
314
315 hfs_chash_unlock(hfsmp);
316 } else {
317 vid = vnode_vid(vp);
318
319 hfs_chash_unlock(hfsmp);
320
321 if (vnode_getwithvid(vp, vid))
322 goto loop;
323 }
324 if (ncp) {
325 /*
326 * someone else won the race to create
327 * this cnode and add it to the hash
328 * just dump our allocation
329 */
330 FREE_ZONE(ncp, sizeof(struct cnode), M_HFSNODE);
331 ncp = NULL;
332 }
333
334 if (!skiplock) {
335 hfs_lock(cp, HFS_FORCE_LOCK);
336 }
337
338 /*
339 * Skip cnodes that are not in the name space anymore
340 * we need to check with the cnode lock held because
341 * we may have blocked acquiring the vnode ref or the
342 * lock on the cnode which would allow the node to be
343 * unlinked.
344 *
345 * Don't return a cnode in this case since the inum
346 * is no longer valid for lookups.
347 */
348 if ((cp->c_flag & (C_NOEXISTS | C_DELETED)) && !wantrsrc) {
349 int renamed = 0;
350 if (cp->c_flag & C_RENAMED) {
351 renamed = 1;
352 }
353 if (!skiplock)
354 hfs_unlock(cp);
355 if (vp != NULLVP) {
356 vnode_put(vp);
357 } else {
358 hfs_chash_lock_spin(hfsmp);
359 CLR(cp->c_hflag, H_ATTACH);
360 *hflags &= ~H_ATTACH;
361
362 if (ISSET(cp->c_hflag, H_WAITING)) {
363 CLR(cp->c_hflag, H_WAITING);
364 wakeup((caddr_t)cp);
365 }
366 hfs_chash_unlock(hfsmp);
367 }
368 vp = NULL;
369 cp = NULL;
370 if (renamed) {
371 *out_flags = GNV_CHASH_RENAMED;
372 }
373 }
374 *vpp = vp;
375 return (cp);
376 }
377
378 /*
379 * Allocate a new cnode
380 */
381 if (skiplock && !wantrsrc)
382 panic("%s - should never get here when skiplock is set \n", __FUNCTION__);
383
384 if (ncp == NULL) {
385 hfs_chash_unlock(hfsmp);
386 MALLOC_ZONE(ncp, struct cnode *, sizeof(struct cnode), M_HFSNODE, M_WAITOK);
387 /*
388 * since we dropped the chash lock,
389 * we need to go back and re-verify
390 * that this node hasn't come into
391 * existence...
392 */
393 goto loop;
394 }
395 hfs_chash_lock_convert(hfsmp);
396
397 bzero(ncp, sizeof(struct cnode));
398 SET(ncp->c_hflag, H_ALLOC);
399 *hflags |= H_ALLOC;
400 ncp->c_fileid = inum;
401 TAILQ_INIT(&ncp->c_hintlist); /* make the list empty */
402 TAILQ_INIT(&ncp->c_originlist);
403
404 lck_rw_init(&ncp->c_rwlock, hfs_rwlock_group, hfs_lock_attr);
405 if (!skiplock)
406 (void) hfs_lock(ncp, HFS_EXCLUSIVE_LOCK);
407
408 /* Insert the new cnode with it's H_ALLOC flag set */
409 LIST_INSERT_HEAD(CNODEHASH(hfsmp, inum), ncp, c_hash);
410 hfs_chash_unlock(hfsmp);
411
412 *vpp = NULL;
413 return (ncp);
414 }
415
416
417 __private_extern__
418 void
419 hfs_chashwakeup(struct hfsmount *hfsmp, struct cnode *cp, int hflags)
420 {
421 hfs_chash_lock_spin(hfsmp);
422
423 CLR(cp->c_hflag, hflags);
424
425 if (ISSET(cp->c_hflag, H_WAITING)) {
426 CLR(cp->c_hflag, H_WAITING);
427 wakeup((caddr_t)cp);
428 }
429 hfs_chash_unlock(hfsmp);
430 }
431
432
433 /*
434 * Re-hash two cnodes in the hash table.
435 */
436 __private_extern__
437 void
438 hfs_chash_rehash(struct hfsmount *hfsmp, struct cnode *cp1, struct cnode *cp2)
439 {
440 hfs_chash_lock_spin(hfsmp);
441
442 LIST_REMOVE(cp1, c_hash);
443 LIST_REMOVE(cp2, c_hash);
444 LIST_INSERT_HEAD(CNODEHASH(hfsmp, cp1->c_fileid), cp1, c_hash);
445 LIST_INSERT_HEAD(CNODEHASH(hfsmp, cp2->c_fileid), cp2, c_hash);
446
447 hfs_chash_unlock(hfsmp);
448 }
449
450
451 /*
452 * Remove a cnode from the hash table.
453 */
454 __private_extern__
455 int
456 hfs_chashremove(struct hfsmount *hfsmp, struct cnode *cp)
457 {
458 hfs_chash_lock_spin(hfsmp);
459
460 /* Check if a vnode is getting attached */
461 if (ISSET(cp->c_hflag, H_ATTACH)) {
462 hfs_chash_unlock(hfsmp);
463 return (EBUSY);
464 }
465 if (cp->c_hash.le_next || cp->c_hash.le_prev) {
466 LIST_REMOVE(cp, c_hash);
467 cp->c_hash.le_next = NULL;
468 cp->c_hash.le_prev = NULL;
469 }
470 hfs_chash_unlock(hfsmp);
471
472 return (0);
473 }
474
475 /*
476 * Remove a cnode from the hash table and wakeup any waiters.
477 */
478 __private_extern__
479 void
480 hfs_chash_abort(struct hfsmount *hfsmp, struct cnode *cp)
481 {
482 hfs_chash_lock_spin(hfsmp);
483
484 LIST_REMOVE(cp, c_hash);
485 cp->c_hash.le_next = NULL;
486 cp->c_hash.le_prev = NULL;
487
488 CLR(cp->c_hflag, H_ATTACH | H_ALLOC);
489 if (ISSET(cp->c_hflag, H_WAITING)) {
490 CLR(cp->c_hflag, H_WAITING);
491 wakeup((caddr_t)cp);
492 }
493 hfs_chash_unlock(hfsmp);
494 }
495
496
497 /*
498 * mark a cnode as in transition
499 */
500 __private_extern__
501 void
502 hfs_chash_mark_in_transit(struct hfsmount *hfsmp, struct cnode *cp)
503 {
504 hfs_chash_lock_spin(hfsmp);
505
506 SET(cp->c_hflag, H_TRANSIT);
507
508 hfs_chash_unlock(hfsmp);
509 }
510
511 /* Search a cnode in the hash. This function does not return cnode which
512 * are getting created, destroyed or in transition. Note that this function
513 * does not acquire the cnode hash mutex, and expects the caller to acquire it.
514 * On success, returns pointer to the cnode found. On failure, returns NULL.
515 */
516 static
517 struct cnode *
518 hfs_chash_search_cnid(struct hfsmount *hfsmp, cnid_t cnid)
519 {
520 struct cnode *cp;
521
522 for (cp = CNODEHASH(hfsmp, cnid)->lh_first; cp; cp = cp->c_hash.le_next) {
523 if (cp->c_fileid == cnid) {
524 break;
525 }
526 }
527
528 /* If cnode is being created or reclaimed, return error. */
529 if (cp && ISSET(cp->c_hflag, H_ALLOC | H_TRANSIT | H_ATTACH)) {
530 cp = NULL;
531 }
532
533 return cp;
534 }
535
536 /* Search a cnode corresponding to given device and ID in the hash. If the
537 * found cnode has kHFSHasChildLinkBit cleared, set it. If the cnode is not
538 * found, no new cnode is created and error is returned.
539 *
540 * Return values -
541 * -1 : The cnode was not found.
542 * 0 : The cnode was found, and the kHFSHasChildLinkBit was already set.
543 * 1 : The cnode was found, the kHFSHasChildLinkBit was not set, and the
544 * function had to set that bit.
545 */
546 __private_extern__
547 int
548 hfs_chash_set_childlinkbit(struct hfsmount *hfsmp, cnid_t cnid)
549 {
550 int retval = -1;
551 struct cnode *cp;
552
553 hfs_chash_lock_spin(hfsmp);
554
555 cp = hfs_chash_search_cnid(hfsmp, cnid);
556 if (cp) {
557 if (cp->c_attr.ca_recflags & kHFSHasChildLinkMask) {
558 retval = 0;
559 } else {
560 cp->c_attr.ca_recflags |= kHFSHasChildLinkMask;
561 retval = 1;
562 }
563 }
564 hfs_chash_unlock(hfsmp);
565
566 return retval;
567 }