1 /* $NetBSD: ulfs_dirhash.c,v 1.14 2015/09/21 01:24:23 dholland Exp $ */
2 /* from NetBSD: ufs_dirhash.c,v 1.34 2009/10/05 23:48:08 rmind Exp */
5 * Copyright (c) 2001, 2002 Ian Dowse. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/ufs/ufs/ufs_dirhash.c,v 1.3.2.8 2004/12/08 11:54:13 dwmalone Exp $
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: ulfs_dirhash.c,v 1.14 2015/09/21 01:24:23 dholland Exp $");
35 * This implements a hash-based lookup scheme for ULFS directories.
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
42 #include <sys/types.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
49 #include <sys/sysctl.h>
50 #include <sys/atomic.h>
52 #include <ufs/lfs/lfs.h>
53 #include <ufs/lfs/lfs_accessors.h>
54 #include <ufs/lfs/ulfs_inode.h>
55 #include <ufs/lfs/ulfs_dirhash.h>
56 #include <ufs/lfs/ulfsmount.h>
57 #include <ufs/lfs/ulfs_bswap.h>
58 #include <ufs/lfs/ulfs_extern.h>
60 #define WRAPINCR(val, limit) (((val) + 1 == (limit)) ? 0 : ((val) + 1))
61 #define WRAPDECR(val, limit) (((val) == 0) ? ((limit) - 1) : ((val) - 1))
62 #define OFSFMT(ip) ((ip)->i_lfs->um_maxsymlinklen <= 0)
63 #define BLKFREE2IDX(n) ((n) > DH_NFSTATS ? DH_NFSTATS : (n))
65 static u_int ulfs_dirhashminblks
= 5;
66 static u_int ulfs_dirhashmaxmem
= 2 * 1024 * 1024;
67 static u_int ulfs_dirhashmem
;
68 static u_int ulfs_dirhashcheck
= 0;
70 static int ulfsdirhash_hash(struct dirhash
*dh
, const char *name
, int namelen
);
71 static void ulfsdirhash_adjfree(struct dirhash
*dh
, doff_t offset
, int diff
,
73 static void ulfsdirhash_delslot(struct dirhash
*dh
, int slot
);
74 static int ulfsdirhash_findslot(struct dirhash
*dh
, const char *name
,
75 int namelen
, doff_t offset
);
76 static doff_t
ulfsdirhash_getprev(struct lfs
*fs
, LFS_DIRHEADER
*dp
,
77 doff_t offset
, int dirblksiz
);
78 static int ulfsdirhash_recycle(int wanted
);
80 static pool_cache_t ulfsdirhashblk_cache
;
81 static pool_cache_t ulfsdirhash_cache
;
83 #define DIRHASHLIST_LOCK() mutex_enter(&ulfsdirhash_lock)
84 #define DIRHASHLIST_UNLOCK() mutex_exit(&ulfsdirhash_lock)
85 #define DIRHASH_LOCK(dh) mutex_enter(&(dh)->dh_lock)
86 #define DIRHASH_UNLOCK(dh) mutex_exit(&(dh)->dh_lock)
87 #define DIRHASH_BLKALLOC() \
88 pool_cache_get(ulfsdirhashblk_cache, PR_NOWAIT)
89 #define DIRHASH_BLKFREE(ptr) \
90 pool_cache_put(ulfsdirhashblk_cache, ptr)
92 /* Dirhash list; recently-used entries are near the tail. */
93 static TAILQ_HEAD(, dirhash
) ulfsdirhash_list
;
95 /* Protects: ulfsdirhash_list, `dh_list' field, ulfs_dirhashmem. */
96 static kmutex_t ulfsdirhash_lock
;
98 static struct sysctllog
*ulfsdirhash_sysctl_log
;
105 * The dh_lock mutex should be acquired either via the inode lock, or via
106 * ulfsdirhash_lock. Only the owner of the inode may free the associated
107 * dirhash, but anything can steal its memory and set dh_hash to NULL.
111 * Attempt to build up a hash table for the directory contents in
112 * inode 'ip'. Returns 0 on success, or -1 of the operation failed.
115 ulfsdirhash_build(struct inode
*ip
)
117 struct lfs
*fs
= ip
->i_lfs
;
119 struct buf
*bp
= NULL
;
123 int dirblocks
, i
, j
, memreqd
, nblocks
, narrays
, nslots
, slot
;
124 int dirblksiz
= ip
->i_lfs
->um_dirblksiz
;
126 /* Check if we can/should use dirhash. */
127 if (ip
->i_dirhash
== NULL
) {
128 if (ip
->i_size
< (ulfs_dirhashminblks
* dirblksiz
) || OFSFMT(ip
))
131 /* Hash exists, but sysctls could have changed. */
132 if (ip
->i_size
< (ulfs_dirhashminblks
* dirblksiz
) ||
133 ulfs_dirhashmem
> ulfs_dirhashmaxmem
) {
134 ulfsdirhash_free(ip
);
137 /* Check if hash exists and is intact (note: unlocked read). */
138 if (ip
->i_dirhash
->dh_hash
!= NULL
)
140 /* Free the old, recycled hash and build a new one. */
141 ulfsdirhash_free(ip
);
144 /* Don't hash removed directories. */
145 if (ip
->i_nlink
== 0)
149 /* Allocate 50% more entries than this dir size could ever need. */
150 KASSERT(ip
->i_size
>= dirblksiz
);
151 nslots
= ip
->i_size
/ LFS_DIRECTSIZ(fs
, 1);
152 nslots
= (nslots
* 3 + 1) / 2;
153 narrays
= howmany(nslots
, DH_NBLKOFF
);
154 nslots
= narrays
* DH_NBLKOFF
;
155 dirblocks
= howmany(ip
->i_size
, dirblksiz
);
156 nblocks
= (dirblocks
* 3 + 1) / 2;
158 memreqd
= sizeof(*dh
) + narrays
* sizeof(*dh
->dh_hash
) +
159 narrays
* DH_NBLKOFF
* sizeof(**dh
->dh_hash
) +
160 nblocks
* sizeof(*dh
->dh_blkfree
);
162 while (atomic_add_int_nv(&ulfs_dirhashmem
, memreqd
) >
163 ulfs_dirhashmaxmem
) {
164 atomic_add_int(&ulfs_dirhashmem
, -memreqd
);
165 if (memreqd
> ulfs_dirhashmaxmem
/ 2)
167 /* Try to free some space. */
168 if (ulfsdirhash_recycle(memreqd
) != 0)
171 DIRHASHLIST_UNLOCK();
175 * Use non-blocking mallocs so that we will revert to a linear
176 * lookup on failure rather than potentially blocking forever.
178 dh
= pool_cache_get(ulfsdirhash_cache
, PR_NOWAIT
);
180 atomic_add_int(&ulfs_dirhashmem
, -memreqd
);
183 memset(dh
, 0, sizeof(*dh
));
184 mutex_init(&dh
->dh_lock
, MUTEX_DEFAULT
, IPL_NONE
);
186 dh
->dh_hashsz
= narrays
* sizeof(dh
->dh_hash
[0]);
187 dh
->dh_hash
= kmem_zalloc(dh
->dh_hashsz
, KM_NOSLEEP
);
188 dh
->dh_blkfreesz
= nblocks
* sizeof(dh
->dh_blkfree
[0]);
189 dh
->dh_blkfree
= kmem_zalloc(dh
->dh_blkfreesz
, KM_NOSLEEP
);
190 if (dh
->dh_hash
== NULL
|| dh
->dh_blkfree
== NULL
)
192 for (i
= 0; i
< narrays
; i
++) {
193 if ((dh
->dh_hash
[i
] = DIRHASH_BLKALLOC()) == NULL
)
195 for (j
= 0; j
< DH_NBLKOFF
; j
++)
196 dh
->dh_hash
[i
][j
] = DIRHASH_EMPTY
;
199 /* Initialise the hash table and block statistics. */
200 dh
->dh_narrays
= narrays
;
201 dh
->dh_hlen
= nslots
;
202 dh
->dh_nblk
= nblocks
;
203 dh
->dh_dirblks
= dirblocks
;
204 for (i
= 0; i
< dirblocks
; i
++)
205 dh
->dh_blkfree
[i
] = dirblksiz
/ DIRALIGN
;
206 for (i
= 0; i
< DH_NFSTATS
; i
++)
207 dh
->dh_firstfree
[i
] = -1;
208 dh
->dh_firstfree
[DH_NFSTATS
] = 0;
211 dh
->dh_score
= DH_SCOREINIT
;
214 bmask
= VFSTOULFS(vp
->v_mount
)->um_mountp
->mnt_stat
.f_iosize
- 1;
216 while (pos
< ip
->i_size
) {
217 if ((curcpu()->ci_schedstate
.spc_flags
& SPCF_SHOULDYIELD
)
221 /* If necessary, get the next directory block. */
222 if ((pos
& bmask
) == 0) {
225 if (ulfs_blkatoff(vp
, (off_t
)pos
, NULL
, &bp
, false) != 0)
229 /* Add this entry to the hash. */
230 ep
= (LFS_DIRHEADER
*)((char *)bp
->b_data
+ (pos
& bmask
));
231 if (lfs_dir_getreclen(fs
, ep
) == 0 || lfs_dir_getreclen(fs
, ep
) >
232 dirblksiz
- (pos
& (dirblksiz
- 1))) {
233 /* Corrupted directory. */
237 if (lfs_dir_getino(fs
, ep
) != 0) {
238 /* Add the entry (simplified ulfsdirhash_add). */
239 slot
= ulfsdirhash_hash(dh
, lfs_dir_nameptr(fs
, ep
),
240 lfs_dir_getnamlen(fs
, ep
));
241 while (DH_ENTRY(dh
, slot
) != DIRHASH_EMPTY
)
242 slot
= WRAPINCR(slot
, dh
->dh_hlen
);
244 DH_ENTRY(dh
, slot
) = pos
;
245 ulfsdirhash_adjfree(dh
, pos
, -LFS_DIRSIZ(fs
, ep
),
248 pos
+= lfs_dir_getreclen(fs
, ep
);
254 TAILQ_INSERT_TAIL(&ulfsdirhash_list
, dh
, dh_list
);
257 DIRHASHLIST_UNLOCK();
262 if (dh
->dh_hash
!= NULL
) {
263 for (i
= 0; i
< narrays
; i
++)
264 if (dh
->dh_hash
[i
] != NULL
)
265 DIRHASH_BLKFREE(dh
->dh_hash
[i
]);
266 kmem_free(dh
->dh_hash
, dh
->dh_hashsz
);
268 if (dh
->dh_blkfree
!= NULL
)
269 kmem_free(dh
->dh_blkfree
, dh
->dh_blkfreesz
);
270 mutex_destroy(&dh
->dh_lock
);
271 pool_cache_put(ulfsdirhash_cache
, dh
);
272 ip
->i_dirhash
= NULL
;
273 atomic_add_int(&ulfs_dirhashmem
, -memreqd
);
278 * Free any hash table associated with inode 'ip'.
281 ulfsdirhash_free(struct inode
*ip
)
286 if ((dh
= ip
->i_dirhash
) == NULL
)
292 TAILQ_REMOVE(&ulfsdirhash_list
, dh
, dh_list
);
293 DIRHASHLIST_UNLOCK();
296 /* The dirhash pointed to by 'dh' is exclusively ours now. */
298 if (dh
->dh_hash
!= NULL
) {
299 for (i
= 0; i
< dh
->dh_narrays
; i
++)
300 DIRHASH_BLKFREE(dh
->dh_hash
[i
]);
301 kmem_free(dh
->dh_hash
, dh
->dh_hashsz
);
302 kmem_free(dh
->dh_blkfree
, dh
->dh_blkfreesz
);
303 mem
+= dh
->dh_hashsz
;
304 mem
+= dh
->dh_narrays
* DH_NBLKOFF
* sizeof(**dh
->dh_hash
);
305 mem
+= dh
->dh_nblk
* sizeof(*dh
->dh_blkfree
);
307 mutex_destroy(&dh
->dh_lock
);
308 pool_cache_put(ulfsdirhash_cache
, dh
);
309 ip
->i_dirhash
= NULL
;
311 atomic_add_int(&ulfs_dirhashmem
, -mem
);
315 * Find the offset of the specified name within the given inode.
316 * Returns 0 on success, ENOENT if the entry does not exist, or
317 * EJUSTRETURN if the caller should revert to a linear search.
319 * If successful, the directory offset is stored in *offp, and a
320 * pointer to a struct buf containing the entry is stored in *bpp. If
321 * prevoffp is non-NULL, the offset of the previous entry within
322 * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry
323 * is the first in a block, the start of the block is used).
326 ulfsdirhash_lookup(struct inode
*ip
, const char *name
, int namelen
, doff_t
*offp
,
327 struct buf
**bpp
, doff_t
*prevoffp
)
329 struct lfs
*fs
= ip
->i_lfs
;
330 struct dirhash
*dh
, *dh_next
;
334 doff_t blkoff
, bmask
, offset
, prevoff
;
336 int dirblksiz
= ip
->i_lfs
->um_dirblksiz
;
338 if ((dh
= ip
->i_dirhash
) == NULL
)
339 return (EJUSTRETURN
);
342 * Move this dirhash towards the end of the list if it has a
343 * score higher than the next entry, and acquire the dh_lock.
344 * Optimise the case where it's already the last by performing
345 * an unlocked read of the TAILQ_NEXT pointer.
347 * In both cases, end up holding just dh_lock.
349 if (TAILQ_NEXT(dh
, dh_list
) != NULL
) {
353 * If the new score will be greater than that of the next
354 * entry, then move this entry past it. With both mutexes
355 * held, dh_next won't go away, but its dh_score could
356 * change; that's not important since it is just a hint.
358 if (dh
->dh_hash
!= NULL
&&
359 (dh_next
= TAILQ_NEXT(dh
, dh_list
)) != NULL
&&
360 dh
->dh_score
>= dh_next
->dh_score
) {
361 KASSERT(dh
->dh_onlist
);
362 TAILQ_REMOVE(&ulfsdirhash_list
, dh
, dh_list
);
363 TAILQ_INSERT_AFTER(&ulfsdirhash_list
, dh_next
, dh
,
366 DIRHASHLIST_UNLOCK();
368 /* Already the last, though that could change as we wait. */
371 if (dh
->dh_hash
== NULL
) {
373 ulfsdirhash_free(ip
);
374 return (EJUSTRETURN
);
377 /* Update the score. */
378 if (dh
->dh_score
< DH_SCOREMAX
)
382 bmask
= VFSTOULFS(vp
->v_mount
)->um_mountp
->mnt_stat
.f_iosize
- 1;
386 slot
= ulfsdirhash_hash(dh
, name
, namelen
);
390 * Sequential access optimisation. dh_seqoff contains the
391 * offset of the directory entry immediately following
392 * the last entry that was looked up. Check if this offset
393 * appears in the hash chain for the name we are looking for.
395 for (i
= slot
; (offset
= DH_ENTRY(dh
, i
)) != DIRHASH_EMPTY
;
396 i
= WRAPINCR(i
, dh
->dh_hlen
))
397 if (offset
== dh
->dh_seqoff
)
399 if (offset
== dh
->dh_seqoff
) {
401 * We found an entry with the expected offset. This
402 * is probably the entry we want, but if not, the
403 * code below will turn off seqoff and retry.
410 for (; (offset
= DH_ENTRY(dh
, slot
)) != DIRHASH_EMPTY
;
411 slot
= WRAPINCR(slot
, dh
->dh_hlen
)) {
412 if (offset
== DIRHASH_DEL
)
415 if (offset
< 0 || offset
>= ip
->i_size
)
416 panic("ulfsdirhash_lookup: bad offset in hash array");
417 if ((offset
& ~bmask
) != blkoff
) {
420 blkoff
= offset
& ~bmask
;
421 if (ulfs_blkatoff(vp
, (off_t
)blkoff
,
422 NULL
, &bp
, false) != 0) {
424 return (EJUSTRETURN
);
427 dp
= (LFS_DIRHEADER
*)((char *)bp
->b_data
+ (offset
& bmask
));
428 if (lfs_dir_getreclen(fs
, dp
) == 0 || lfs_dir_getreclen(fs
, dp
) >
429 dirblksiz
- (offset
& (dirblksiz
- 1))) {
430 /* Corrupted directory. */
433 return (EJUSTRETURN
);
435 if (lfs_dir_getnamlen(fs
, dp
) == namelen
&&
436 memcmp(lfs_dir_nameptr(fs
, dp
), name
, namelen
) == 0) {
437 /* Found. Get the prev offset if needed. */
438 if (prevoffp
!= NULL
) {
439 if (offset
& (dirblksiz
- 1)) {
440 prevoff
= ulfsdirhash_getprev(fs
, dp
,
444 return (EJUSTRETURN
);
451 /* Check for sequential access, and update offset. */
452 if (dh
->dh_seqopt
== 0 && dh
->dh_seqoff
== offset
)
454 dh
->dh_seqoff
= offset
+ LFS_DIRSIZ(fs
, dp
);
462 if (dh
->dh_hash
== NULL
) {
466 ulfsdirhash_free(ip
);
467 return (EJUSTRETURN
);
470 * When the name doesn't match in the seqopt case, go back
471 * and search normally.
485 * Find a directory block with room for 'slotneeded' bytes. Returns
486 * the offset of the directory entry that begins the free space.
487 * This will either be the offset of an existing entry that has free
488 * space at the end, or the offset of an entry with d_ino == 0 at
489 * the start of a DIRBLKSIZ block.
491 * To use the space, the caller may need to compact existing entries in
492 * the directory. The total number of bytes in all of the entries involved
493 * in the compaction is stored in *slotsize. In other words, all of
494 * the entries that must be compacted are exactly contained in the
495 * region beginning at the returned offset and spanning *slotsize bytes.
497 * Returns -1 if no space was found, indicating that the directory
501 ulfsdirhash_findfree(struct inode
*ip
, int slotneeded
, int *slotsize
)
503 struct lfs
*fs
= ip
->i_lfs
;
507 doff_t pos
, slotstart
;
508 int dirblock
, error
, freebytes
, i
;
509 int dirblksiz
= ip
->i_lfs
->um_dirblksiz
;
511 if ((dh
= ip
->i_dirhash
) == NULL
)
515 if (dh
->dh_hash
== NULL
) {
517 ulfsdirhash_free(ip
);
521 /* Find a directory block with the desired free space. */
523 for (i
= howmany(slotneeded
, DIRALIGN
); i
<= DH_NFSTATS
; i
++)
524 if ((dirblock
= dh
->dh_firstfree
[i
]) != -1)
526 if (dirblock
== -1) {
531 KASSERT(dirblock
< dh
->dh_nblk
&&
532 dh
->dh_blkfree
[dirblock
] >= howmany(slotneeded
, DIRALIGN
));
533 pos
= dirblock
* dirblksiz
;
534 error
= ulfs_blkatoff(ip
->i_vnode
, (off_t
)pos
, (void *)&dp
, &bp
, false);
539 /* Find the first entry with free space. */
540 for (i
= 0; i
< dirblksiz
; ) {
541 if (lfs_dir_getreclen(fs
, dp
) == 0) {
546 if (lfs_dir_getino(fs
, dp
) == 0 || lfs_dir_getreclen(fs
, dp
) > LFS_DIRSIZ(fs
, dp
))
548 i
+= lfs_dir_getreclen(fs
, dp
);
549 dp
= LFS_NEXTDIR(fs
, dp
);
558 /* Find the range of entries needed to get enough space */
560 while (i
< dirblksiz
&& freebytes
< slotneeded
) {
561 freebytes
+= lfs_dir_getreclen(fs
, dp
);
562 if (lfs_dir_getino(fs
, dp
) != 0)
563 freebytes
-= LFS_DIRSIZ(fs
, dp
);
564 if (lfs_dir_getreclen(fs
, dp
) == 0) {
569 i
+= lfs_dir_getreclen(fs
, dp
);
570 dp
= LFS_NEXTDIR(fs
, dp
);
577 if (freebytes
< slotneeded
)
578 panic("ulfsdirhash_findfree: free mismatch");
581 *slotsize
= pos
+ i
- slotstart
;
586 * Return the start of the unused space at the end of a directory, or
587 * -1 if there are no trailing unused blocks.
590 ulfsdirhash_enduseful(struct inode
*ip
)
594 int dirblksiz
= ip
->i_lfs
->um_dirblksiz
;
596 if ((dh
= ip
->i_dirhash
) == NULL
)
600 if (dh
->dh_hash
== NULL
) {
602 ulfsdirhash_free(ip
);
606 if (dh
->dh_blkfree
[dh
->dh_dirblks
- 1] != dirblksiz
/ DIRALIGN
) {
611 for (i
= dh
->dh_dirblks
- 1; i
>= 0; i
--)
612 if (dh
->dh_blkfree
[i
] != dirblksiz
/ DIRALIGN
)
615 return ((doff_t
)(i
+ 1) * dirblksiz
);
619 * Insert information into the hash about a new directory entry. dirp
620 * points to a struct lfs_direct containing the entry, and offset specifies
621 * the offset of this entry.
624 ulfsdirhash_add(struct inode
*ip
, LFS_DIRHEADER
*dirp
, doff_t offset
)
626 struct lfs
*fs
= ip
->i_lfs
;
629 int dirblksiz
= ip
->i_lfs
->um_dirblksiz
;
631 if ((dh
= ip
->i_dirhash
) == NULL
)
635 if (dh
->dh_hash
== NULL
) {
637 ulfsdirhash_free(ip
);
641 KASSERT(offset
< dh
->dh_dirblks
* dirblksiz
);
643 * Normal hash usage is < 66%. If the usage gets too high then
644 * remove the hash entirely and let it be rebuilt later.
646 if (dh
->dh_hused
>= (dh
->dh_hlen
* 3) / 4) {
648 ulfsdirhash_free(ip
);
652 /* Find a free hash slot (empty or deleted), and add the entry. */
653 slot
= ulfsdirhash_hash(dh
, lfs_dir_nameptr(fs
, dirp
),
654 lfs_dir_getnamlen(fs
, dirp
));
655 while (DH_ENTRY(dh
, slot
) >= 0)
656 slot
= WRAPINCR(slot
, dh
->dh_hlen
);
657 if (DH_ENTRY(dh
, slot
) == DIRHASH_EMPTY
)
659 DH_ENTRY(dh
, slot
) = offset
;
661 /* Update the per-block summary info. */
662 ulfsdirhash_adjfree(dh
, offset
, -LFS_DIRSIZ(fs
, dirp
), dirblksiz
);
667 * Remove the specified directory entry from the hash. The entry to remove
668 * is defined by the name in `dirp', which must exist at the specified
669 * `offset' within the directory.
672 ulfsdirhash_remove(struct inode
*ip
, LFS_DIRHEADER
*dirp
, doff_t offset
)
674 struct lfs
*fs
= ip
->i_lfs
;
677 int dirblksiz
= ip
->i_lfs
->um_dirblksiz
;
679 if ((dh
= ip
->i_dirhash
) == NULL
)
683 if (dh
->dh_hash
== NULL
) {
685 ulfsdirhash_free(ip
);
689 KASSERT(offset
< dh
->dh_dirblks
* dirblksiz
);
691 slot
= ulfsdirhash_findslot(dh
, lfs_dir_nameptr(fs
, dirp
),
692 lfs_dir_getnamlen(fs
, dirp
), offset
);
694 /* Remove the hash entry. */
695 ulfsdirhash_delslot(dh
, slot
);
697 /* Update the per-block summary info. */
698 ulfsdirhash_adjfree(dh
, offset
, LFS_DIRSIZ(fs
, dirp
), dirblksiz
);
703 * Change the offset associated with a directory entry in the hash. Used
704 * when compacting directory blocks.
707 ulfsdirhash_move(struct inode
*ip
, LFS_DIRHEADER
*dirp
, doff_t oldoff
,
710 struct lfs
*fs
= ip
->i_lfs
;
714 if ((dh
= ip
->i_dirhash
) == NULL
)
717 if (dh
->dh_hash
== NULL
) {
719 ulfsdirhash_free(ip
);
723 KASSERT(oldoff
< dh
->dh_dirblks
* ip
->i_lfs
->um_dirblksiz
&&
724 newoff
< dh
->dh_dirblks
* ip
->i_lfs
->um_dirblksiz
);
725 /* Find the entry, and update the offset. */
726 slot
= ulfsdirhash_findslot(dh
, lfs_dir_nameptr(fs
, dirp
),
727 lfs_dir_getnamlen(fs
, dirp
), oldoff
);
728 DH_ENTRY(dh
, slot
) = newoff
;
733 * Inform dirhash that the directory has grown by one block that
734 * begins at offset (i.e. the new length is offset + DIRBLKSIZ).
737 ulfsdirhash_newblk(struct inode
*ip
, doff_t offset
)
741 int dirblksiz
= ip
->i_lfs
->um_dirblksiz
;
743 if ((dh
= ip
->i_dirhash
) == NULL
)
746 if (dh
->dh_hash
== NULL
) {
748 ulfsdirhash_free(ip
);
752 KASSERT(offset
== dh
->dh_dirblks
* dirblksiz
);
753 block
= offset
/ dirblksiz
;
754 if (block
>= dh
->dh_nblk
) {
755 /* Out of space; must rebuild. */
757 ulfsdirhash_free(ip
);
760 dh
->dh_dirblks
= block
+ 1;
762 /* Account for the new free block. */
763 dh
->dh_blkfree
[block
] = dirblksiz
/ DIRALIGN
;
764 if (dh
->dh_firstfree
[DH_NFSTATS
] == -1)
765 dh
->dh_firstfree
[DH_NFSTATS
] = block
;
770 * Inform dirhash that the directory is being truncated.
773 ulfsdirhash_dirtrunc(struct inode
*ip
, doff_t offset
)
777 int dirblksiz
= ip
->i_lfs
->um_dirblksiz
;
779 if ((dh
= ip
->i_dirhash
) == NULL
)
783 if (dh
->dh_hash
== NULL
) {
785 ulfsdirhash_free(ip
);
789 KASSERT(offset
<= dh
->dh_dirblks
* dirblksiz
);
790 block
= howmany(offset
, dirblksiz
);
792 * If the directory shrinks to less than 1/8 of dh_nblk blocks
793 * (about 20% of its original size due to the 50% extra added in
794 * ulfsdirhash_build) then free it, and let the caller rebuild
797 if (block
< dh
->dh_nblk
/ 8 && dh
->dh_narrays
> 1) {
799 ulfsdirhash_free(ip
);
804 * Remove any `first free' information pertaining to the
805 * truncated blocks. All blocks we're removing should be
808 if (dh
->dh_firstfree
[DH_NFSTATS
] >= block
)
809 dh
->dh_firstfree
[DH_NFSTATS
] = -1;
810 for (i
= block
; i
< dh
->dh_dirblks
; i
++)
811 if (dh
->dh_blkfree
[i
] != dirblksiz
/ DIRALIGN
)
812 panic("ulfsdirhash_dirtrunc: blocks in use");
813 for (i
= 0; i
< DH_NFSTATS
; i
++)
814 if (dh
->dh_firstfree
[i
] >= block
)
815 panic("ulfsdirhash_dirtrunc: first free corrupt");
816 dh
->dh_dirblks
= block
;
821 * Debugging function to check that the dirhash information about
822 * a directory block matches its actual contents. Panics if a mismatch
825 * On entry, `sbuf' should point to the start of an in-core
826 * DIRBLKSIZ-sized directory block, and `offset' should contain the
827 * offset from the start of the directory of that block.
830 ulfsdirhash_checkblock(struct inode
*ip
, char *sbuf
, doff_t offset
)
832 struct lfs
*fs
= ip
->i_lfs
;
835 int block
, ffslot
, i
, nfree
;
836 int dirblksiz
= ip
->i_lfs
->um_dirblksiz
;
838 if (!ulfs_dirhashcheck
)
840 if ((dh
= ip
->i_dirhash
) == NULL
)
844 if (dh
->dh_hash
== NULL
) {
846 ulfsdirhash_free(ip
);
850 block
= offset
/ dirblksiz
;
851 if ((offset
& (dirblksiz
- 1)) != 0 || block
>= dh
->dh_dirblks
)
852 panic("ulfsdirhash_checkblock: bad offset");
855 for (i
= 0; i
< dirblksiz
; i
+= lfs_dir_getreclen(fs
, dp
)) {
856 dp
= (LFS_DIRHEADER
*)(sbuf
+ i
);
857 if (lfs_dir_getreclen(fs
, dp
) == 0 || i
+ lfs_dir_getreclen(fs
, dp
) > dirblksiz
)
858 panic("ulfsdirhash_checkblock: bad dir");
860 if (lfs_dir_getino(fs
, dp
) == 0) {
863 * XXX entries with d_ino == 0 should only occur
864 * at the start of a DIRBLKSIZ block. However the
865 * ulfs code is tolerant of such entries at other
866 * offsets, and fsck does not fix them.
869 panic("ulfsdirhash_checkblock: bad dir inode");
871 nfree
+= lfs_dir_getreclen(fs
, dp
);
875 /* Check that the entry exists (will panic if it doesn't). */
876 ulfsdirhash_findslot(dh
, lfs_dir_nameptr(fs
, dp
),
877 lfs_dir_getnamlen(fs
, dp
),
880 nfree
+= lfs_dir_getreclen(fs
, dp
) - LFS_DIRSIZ(fs
, dp
);
883 panic("ulfsdirhash_checkblock: bad dir end");
885 if (dh
->dh_blkfree
[block
] * DIRALIGN
!= nfree
)
886 panic("ulfsdirhash_checkblock: bad free count");
888 ffslot
= BLKFREE2IDX(nfree
/ DIRALIGN
);
889 for (i
= 0; i
<= DH_NFSTATS
; i
++)
890 if (dh
->dh_firstfree
[i
] == block
&& i
!= ffslot
)
891 panic("ulfsdirhash_checkblock: bad first-free");
892 if (dh
->dh_firstfree
[ffslot
] == -1)
893 panic("ulfsdirhash_checkblock: missing first-free entry");
898 * Hash the specified filename into a dirhash slot.
901 ulfsdirhash_hash(struct dirhash
*dh
, const char *name
, int namelen
)
906 * We hash the name and then some other bit of data that is
907 * invariant over the dirhash's lifetime. Otherwise names
908 * differing only in the last byte are placed close to one
909 * another in the table, which is bad for linear probing.
911 hash
= hash32_buf(name
, namelen
, HASH32_BUF_INIT
);
912 hash
= hash32_buf(&dh
, sizeof(dh
), hash
);
913 return (hash
% dh
->dh_hlen
);
917 * Adjust the number of free bytes in the block containing `offset'
918 * by the value specified by `diff'.
920 * The caller must ensure we have exclusive access to `dh'; normally
921 * that means that dh_lock should be held, but this is also called
922 * from ulfsdirhash_build() where exclusive access can be assumed.
925 ulfsdirhash_adjfree(struct dirhash
*dh
, doff_t offset
, int diff
, int dirblksiz
)
927 int block
, i
, nfidx
, ofidx
;
929 KASSERT(mutex_owned(&dh
->dh_lock
));
931 /* Update the per-block summary info. */
932 block
= offset
/ dirblksiz
;
933 KASSERT(block
< dh
->dh_nblk
&& block
< dh
->dh_dirblks
);
934 ofidx
= BLKFREE2IDX(dh
->dh_blkfree
[block
]);
935 dh
->dh_blkfree
[block
] = (int)dh
->dh_blkfree
[block
] + (diff
/ DIRALIGN
);
936 nfidx
= BLKFREE2IDX(dh
->dh_blkfree
[block
]);
938 /* Update the `first free' list if necessary. */
939 if (ofidx
!= nfidx
) {
940 /* If removing, scan forward for the next block. */
941 if (dh
->dh_firstfree
[ofidx
] == block
) {
942 for (i
= block
+ 1; i
< dh
->dh_dirblks
; i
++)
943 if (BLKFREE2IDX(dh
->dh_blkfree
[i
]) == ofidx
)
945 dh
->dh_firstfree
[ofidx
] = (i
< dh
->dh_dirblks
) ? i
: -1;
948 /* Make this the new `first free' if necessary */
949 if (dh
->dh_firstfree
[nfidx
] > block
||
950 dh
->dh_firstfree
[nfidx
] == -1)
951 dh
->dh_firstfree
[nfidx
] = block
;
956 * Find the specified name which should have the specified offset.
957 * Returns a slot number, and panics on failure.
959 * `dh' must be locked on entry and remains so on return.
962 ulfsdirhash_findslot(struct dirhash
*dh
, const char *name
, int namelen
,
967 KASSERT(mutex_owned(&dh
->dh_lock
));
969 /* Find the entry. */
970 KASSERT(dh
->dh_hused
< dh
->dh_hlen
);
971 slot
= ulfsdirhash_hash(dh
, name
, namelen
);
972 while (DH_ENTRY(dh
, slot
) != offset
&&
973 DH_ENTRY(dh
, slot
) != DIRHASH_EMPTY
)
974 slot
= WRAPINCR(slot
, dh
->dh_hlen
);
975 if (DH_ENTRY(dh
, slot
) != offset
)
976 panic("ulfsdirhash_findslot: '%.*s' not found", namelen
, name
);
982 * Remove the entry corresponding to the specified slot from the hash array.
984 * `dh' must be locked on entry and remains so on return.
987 ulfsdirhash_delslot(struct dirhash
*dh
, int slot
)
991 KASSERT(mutex_owned(&dh
->dh_lock
));
993 /* Mark the entry as deleted. */
994 DH_ENTRY(dh
, slot
) = DIRHASH_DEL
;
996 /* If this is the end of a chain of DIRHASH_DEL slots, remove them. */
997 for (i
= slot
; DH_ENTRY(dh
, i
) == DIRHASH_DEL
; )
998 i
= WRAPINCR(i
, dh
->dh_hlen
);
999 if (DH_ENTRY(dh
, i
) == DIRHASH_EMPTY
) {
1000 i
= WRAPDECR(i
, dh
->dh_hlen
);
1001 while (DH_ENTRY(dh
, i
) == DIRHASH_DEL
) {
1002 DH_ENTRY(dh
, i
) = DIRHASH_EMPTY
;
1004 i
= WRAPDECR(i
, dh
->dh_hlen
);
1006 KASSERT(dh
->dh_hused
>= 0);
1011 * Given a directory entry and its offset, find the offset of the
1012 * previous entry in the same DIRBLKSIZ-sized block. Returns an
1013 * offset, or -1 if there is no previous entry in the block or some
1014 * other problem occurred.
1017 ulfsdirhash_getprev(struct lfs
*fs
, LFS_DIRHEADER
*dirp
,
1018 doff_t offset
, int dirblksiz
)
1022 doff_t blkoff
, prevoff
;
1026 blkoff
= offset
& ~(dirblksiz
- 1); /* offset of start of block */
1027 entrypos
= offset
& (dirblksiz
- 1); /* entry relative to block */
1028 blkbuf
= (char *)dirp
- entrypos
;
1031 /* If `offset' is the start of a block, there is no previous entry. */
1035 /* Scan from the start of the block until we get to the entry. */
1036 for (i
= 0; i
< entrypos
; i
+= reclen
) {
1037 dp
= (LFS_DIRHEADER
*)(blkbuf
+ i
);
1038 reclen
= lfs_dir_getreclen(fs
, dp
);
1039 if (reclen
== 0 || i
+ reclen
> entrypos
)
1040 return (-1); /* Corrupted directory. */
1041 prevoff
= blkoff
+ i
;
1047 * Try to free up `wanted' bytes by stealing memory from existing
1048 * dirhashes. Returns zero with list locked if successful.
1051 ulfsdirhash_recycle(int wanted
)
1056 int i
, mem
, narrays
;
1057 size_t hashsz
, blkfreesz
;
1060 while (wanted
+ ulfs_dirhashmem
> ulfs_dirhashmaxmem
) {
1061 /* Find a dirhash, and lock it. */
1062 if ((dh
= TAILQ_FIRST(&ulfsdirhash_list
)) == NULL
) {
1063 DIRHASHLIST_UNLOCK();
1067 KASSERT(dh
->dh_hash
!= NULL
);
1069 /* Decrement the score; only recycle if it becomes zero. */
1070 if (--dh
->dh_score
> 0) {
1072 DIRHASHLIST_UNLOCK();
1076 /* Remove it from the list and detach its memory. */
1077 TAILQ_REMOVE(&ulfsdirhash_list
, dh
, dh_list
);
1080 hashsz
= dh
->dh_hashsz
;
1082 blkfree
= dh
->dh_blkfree
;
1083 blkfreesz
= dh
->dh_blkfreesz
;
1084 dh
->dh_blkfree
= NULL
;
1085 narrays
= dh
->dh_narrays
;
1086 mem
= narrays
* sizeof(*dh
->dh_hash
) +
1087 narrays
* DH_NBLKOFF
* sizeof(**dh
->dh_hash
) +
1088 dh
->dh_nblk
* sizeof(*dh
->dh_blkfree
);
1090 /* Unlock everything, free the detached memory. */
1092 DIRHASHLIST_UNLOCK();
1094 for (i
= 0; i
< narrays
; i
++)
1095 DIRHASH_BLKFREE(hash
[i
]);
1096 kmem_free(hash
, hashsz
);
1097 kmem_free(blkfree
, blkfreesz
);
1099 /* Account for the returned memory, and repeat if necessary. */
1101 atomic_add_int(&ulfs_dirhashmem
, -mem
);
1108 ulfsdirhash_sysctl_init(void)
1110 const struct sysctlnode
*rnode
, *cnode
;
1112 sysctl_createv(&ulfsdirhash_sysctl_log
, 0, NULL
, &rnode
,
1114 CTLTYPE_NODE
, "ulfs",
1115 SYSCTL_DESCR("ulfs"),
1117 CTL_VFS
, CTL_CREATE
, CTL_EOL
);
1119 sysctl_createv(&ulfsdirhash_sysctl_log
, 0, &rnode
, &rnode
,
1121 CTLTYPE_NODE
, "dirhash",
1122 SYSCTL_DESCR("dirhash"),
1124 CTL_CREATE
, CTL_EOL
);
1126 sysctl_createv(&ulfsdirhash_sysctl_log
, 0, &rnode
, &cnode
,
1127 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
1128 CTLTYPE_INT
, "minblocks",
1129 SYSCTL_DESCR("minimum hashed directory size in blocks"),
1130 NULL
, 0, &ulfs_dirhashminblks
, 0,
1131 CTL_CREATE
, CTL_EOL
);
1133 sysctl_createv(&ulfsdirhash_sysctl_log
, 0, &rnode
, &cnode
,
1134 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
1135 CTLTYPE_INT
, "maxmem",
1136 SYSCTL_DESCR("maximum dirhash memory usage"),
1137 NULL
, 0, &ulfs_dirhashmaxmem
, 0,
1138 CTL_CREATE
, CTL_EOL
);
1140 sysctl_createv(&ulfsdirhash_sysctl_log
, 0, &rnode
, &cnode
,
1141 CTLFLAG_PERMANENT
|CTLFLAG_READONLY
,
1142 CTLTYPE_INT
, "memused",
1143 SYSCTL_DESCR("current dirhash memory usage"),
1144 NULL
, 0, &ulfs_dirhashmem
, 0,
1145 CTL_CREATE
, CTL_EOL
);
1147 sysctl_createv(&ulfsdirhash_sysctl_log
, 0, &rnode
, &cnode
,
1148 CTLFLAG_PERMANENT
|CTLFLAG_READWRITE
,
1149 CTLTYPE_INT
, "docheck",
1150 SYSCTL_DESCR("enable extra sanity checks"),
1151 NULL
, 0, &ulfs_dirhashcheck
, 0,
1152 CTL_CREATE
, CTL_EOL
);
1156 ulfsdirhash_init(void)
1159 mutex_init(&ulfsdirhash_lock
, MUTEX_DEFAULT
, IPL_NONE
);
1160 ulfsdirhashblk_cache
= pool_cache_init(DH_NBLKOFF
* sizeof(daddr_t
), 0,
1161 0, 0, "dirhashblk", NULL
, IPL_NONE
, NULL
, NULL
, NULL
);
1162 ulfsdirhash_cache
= pool_cache_init(sizeof(struct dirhash
), 0,
1163 0, 0, "dirhash", NULL
, IPL_NONE
, NULL
, NULL
, NULL
);
1164 TAILQ_INIT(&ulfsdirhash_list
);
1165 ulfsdirhash_sysctl_init();
1169 ulfsdirhash_done(void)
1172 KASSERT(TAILQ_EMPTY(&ulfsdirhash_list
));
1173 pool_cache_destroy(ulfsdirhashblk_cache
);
1174 pool_cache_destroy(ulfsdirhash_cache
);
1175 mutex_destroy(&ulfsdirhash_lock
);
1176 sysctl_teardown(&ulfsdirhash_sysctl_log
);