Remove building with NOCRYPTO option
[minix.git] / sys / ufs / lfs / ulfs_dirhash.c
blob39062d7cfce4ae7117fb245a9709be735d0b3dc6
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 */
4 /*
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
9 * are met:
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
26 * SUCH DAMAGE.
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>
41 #include <sys/kmem.h>
42 #include <sys/types.h>
43 #include <sys/hash.h>
44 #include <sys/proc.h>
45 #include <sys/buf.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/pool.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,
72 int dirblksiz);
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;
101 * Locking order:
102 * ulfsdirhash_lock
103 * dh_lock
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;
118 struct dirhash *dh;
119 struct buf *bp = NULL;
120 LFS_DIRHEADER *ep;
121 struct vnode *vp;
122 doff_t bmask, pos;
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))
129 return (-1);
130 } else {
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);
135 return (-1);
137 /* Check if hash exists and is intact (note: unlocked read). */
138 if (ip->i_dirhash->dh_hash != NULL)
139 return (0);
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)
146 return (-1);
148 vp = ip->i_vnode;
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)
166 return (-1);
167 /* Try to free some space. */
168 if (ulfsdirhash_recycle(memreqd) != 0)
169 return (-1);
170 else
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);
179 if (dh == NULL) {
180 atomic_add_int(&ulfs_dirhashmem, -memreqd);
181 return (-1);
183 memset(dh, 0, sizeof(*dh));
184 mutex_init(&dh->dh_lock, MUTEX_DEFAULT, IPL_NONE);
185 DIRHASH_LOCK(dh);
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)
191 goto fail;
192 for (i = 0; i < narrays; i++) {
193 if ((dh->dh_hash[i] = DIRHASH_BLKALLOC()) == NULL)
194 goto fail;
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;
209 dh->dh_seqopt = 0;
210 dh->dh_seqoff = 0;
211 dh->dh_score = DH_SCOREINIT;
212 ip->i_dirhash = dh;
214 bmask = VFSTOULFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
215 pos = 0;
216 while (pos < ip->i_size) {
217 if ((curcpu()->ci_schedstate.spc_flags & SPCF_SHOULDYIELD)
218 != 0) {
219 preempt();
221 /* If necessary, get the next directory block. */
222 if ((pos & bmask) == 0) {
223 if (bp != NULL)
224 brelse(bp, 0);
225 if (ulfs_blkatoff(vp, (off_t)pos, NULL, &bp, false) != 0)
226 goto fail;
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. */
234 brelse(bp, 0);
235 goto fail;
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);
243 dh->dh_hused++;
244 DH_ENTRY(dh, slot) = pos;
245 ulfsdirhash_adjfree(dh, pos, -LFS_DIRSIZ(fs, ep),
246 dirblksiz);
248 pos += lfs_dir_getreclen(fs, ep);
251 if (bp != NULL)
252 brelse(bp, 0);
253 DIRHASHLIST_LOCK();
254 TAILQ_INSERT_TAIL(&ulfsdirhash_list, dh, dh_list);
255 dh->dh_onlist = 1;
256 DIRHASH_UNLOCK(dh);
257 DIRHASHLIST_UNLOCK();
258 return (0);
260 fail:
261 DIRHASH_UNLOCK(dh);
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);
274 return (-1);
278 * Free any hash table associated with inode 'ip'.
280 void
281 ulfsdirhash_free(struct inode *ip)
283 struct dirhash *dh;
284 int i, mem;
286 if ((dh = ip->i_dirhash) == NULL)
287 return;
289 if (dh->dh_onlist) {
290 DIRHASHLIST_LOCK();
291 if (dh->dh_onlist)
292 TAILQ_REMOVE(&ulfsdirhash_list, dh, dh_list);
293 DIRHASHLIST_UNLOCK();
296 /* The dirhash pointed to by 'dh' is exclusively ours now. */
297 mem = sizeof(*dh);
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;
331 LFS_DIRHEADER *dp;
332 struct vnode *vp;
333 struct buf *bp;
334 doff_t blkoff, bmask, offset, prevoff;
335 int i, slot;
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) {
350 DIRHASHLIST_LOCK();
351 DIRHASH_LOCK(dh);
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,
364 dh_list);
366 DIRHASHLIST_UNLOCK();
367 } else {
368 /* Already the last, though that could change as we wait. */
369 DIRHASH_LOCK(dh);
371 if (dh->dh_hash == NULL) {
372 DIRHASH_UNLOCK(dh);
373 ulfsdirhash_free(ip);
374 return (EJUSTRETURN);
377 /* Update the score. */
378 if (dh->dh_score < DH_SCOREMAX)
379 dh->dh_score++;
381 vp = ip->i_vnode;
382 bmask = VFSTOULFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
383 blkoff = -1;
384 bp = NULL;
385 restart:
386 slot = ulfsdirhash_hash(dh, name, namelen);
388 if (dh->dh_seqopt) {
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)
398 break;
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.
405 slot = i;
406 } else
407 dh->dh_seqopt = 0;
410 for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY;
411 slot = WRAPINCR(slot, dh->dh_hlen)) {
412 if (offset == DIRHASH_DEL)
413 continue;
415 if (offset < 0 || offset >= ip->i_size)
416 panic("ulfsdirhash_lookup: bad offset in hash array");
417 if ((offset & ~bmask) != blkoff) {
418 if (bp != NULL)
419 brelse(bp, 0);
420 blkoff = offset & ~bmask;
421 if (ulfs_blkatoff(vp, (off_t)blkoff,
422 NULL, &bp, false) != 0) {
423 DIRHASH_UNLOCK(dh);
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. */
431 DIRHASH_UNLOCK(dh);
432 brelse(bp, 0);
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,
441 offset, dirblksiz);
442 if (prevoff == -1) {
443 brelse(bp, 0);
444 return (EJUSTRETURN);
446 } else
447 prevoff = offset;
448 *prevoffp = prevoff;
451 /* Check for sequential access, and update offset. */
452 if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset)
453 dh->dh_seqopt = 1;
454 dh->dh_seqoff = offset + LFS_DIRSIZ(fs, dp);
455 DIRHASH_UNLOCK(dh);
457 *bpp = bp;
458 *offp = offset;
459 return (0);
462 if (dh->dh_hash == NULL) {
463 DIRHASH_UNLOCK(dh);
464 if (bp != NULL)
465 brelse(bp, 0);
466 ulfsdirhash_free(ip);
467 return (EJUSTRETURN);
470 * When the name doesn't match in the seqopt case, go back
471 * and search normally.
473 if (dh->dh_seqopt) {
474 dh->dh_seqopt = 0;
475 goto restart;
478 DIRHASH_UNLOCK(dh);
479 if (bp != NULL)
480 brelse(bp, 0);
481 return (ENOENT);
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
498 * must be extended.
500 doff_t
501 ulfsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize)
503 struct lfs *fs = ip->i_lfs;
504 LFS_DIRHEADER *dp;
505 struct dirhash *dh;
506 struct buf *bp;
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)
512 return (-1);
514 DIRHASH_LOCK(dh);
515 if (dh->dh_hash == NULL) {
516 DIRHASH_UNLOCK(dh);
517 ulfsdirhash_free(ip);
518 return (-1);
521 /* Find a directory block with the desired free space. */
522 dirblock = -1;
523 for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++)
524 if ((dirblock = dh->dh_firstfree[i]) != -1)
525 break;
526 if (dirblock == -1) {
527 DIRHASH_UNLOCK(dh);
528 return (-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);
535 if (error) {
536 DIRHASH_UNLOCK(dh);
537 return (-1);
539 /* Find the first entry with free space. */
540 for (i = 0; i < dirblksiz; ) {
541 if (lfs_dir_getreclen(fs, dp) == 0) {
542 DIRHASH_UNLOCK(dh);
543 brelse(bp, 0);
544 return (-1);
546 if (lfs_dir_getino(fs, dp) == 0 || lfs_dir_getreclen(fs, dp) > LFS_DIRSIZ(fs, dp))
547 break;
548 i += lfs_dir_getreclen(fs, dp);
549 dp = LFS_NEXTDIR(fs, dp);
551 if (i > dirblksiz) {
552 DIRHASH_UNLOCK(dh);
553 brelse(bp, 0);
554 return (-1);
556 slotstart = pos + i;
558 /* Find the range of entries needed to get enough space */
559 freebytes = 0;
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) {
565 DIRHASH_UNLOCK(dh);
566 brelse(bp, 0);
567 return (-1);
569 i += lfs_dir_getreclen(fs, dp);
570 dp = LFS_NEXTDIR(fs, dp);
572 if (i > dirblksiz) {
573 DIRHASH_UNLOCK(dh);
574 brelse(bp, 0);
575 return (-1);
577 if (freebytes < slotneeded)
578 panic("ulfsdirhash_findfree: free mismatch");
579 DIRHASH_UNLOCK(dh);
580 brelse(bp, 0);
581 *slotsize = pos + i - slotstart;
582 return (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.
589 doff_t
590 ulfsdirhash_enduseful(struct inode *ip)
592 struct dirhash *dh;
593 int i;
594 int dirblksiz = ip->i_lfs->um_dirblksiz;
596 if ((dh = ip->i_dirhash) == NULL)
597 return (-1);
599 DIRHASH_LOCK(dh);
600 if (dh->dh_hash == NULL) {
601 DIRHASH_UNLOCK(dh);
602 ulfsdirhash_free(ip);
603 return (-1);
606 if (dh->dh_blkfree[dh->dh_dirblks - 1] != dirblksiz / DIRALIGN) {
607 DIRHASH_UNLOCK(dh);
608 return (-1);
611 for (i = dh->dh_dirblks - 1; i >= 0; i--)
612 if (dh->dh_blkfree[i] != dirblksiz / DIRALIGN)
613 break;
614 DIRHASH_UNLOCK(dh);
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.
623 void
624 ulfsdirhash_add(struct inode *ip, LFS_DIRHEADER *dirp, doff_t offset)
626 struct lfs *fs = ip->i_lfs;
627 struct dirhash *dh;
628 int slot;
629 int dirblksiz = ip->i_lfs->um_dirblksiz;
631 if ((dh = ip->i_dirhash) == NULL)
632 return;
634 DIRHASH_LOCK(dh);
635 if (dh->dh_hash == NULL) {
636 DIRHASH_UNLOCK(dh);
637 ulfsdirhash_free(ip);
638 return;
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) {
647 DIRHASH_UNLOCK(dh);
648 ulfsdirhash_free(ip);
649 return;
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)
658 dh->dh_hused++;
659 DH_ENTRY(dh, slot) = offset;
661 /* Update the per-block summary info. */
662 ulfsdirhash_adjfree(dh, offset, -LFS_DIRSIZ(fs, dirp), dirblksiz);
663 DIRHASH_UNLOCK(dh);
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.
671 void
672 ulfsdirhash_remove(struct inode *ip, LFS_DIRHEADER *dirp, doff_t offset)
674 struct lfs *fs = ip->i_lfs;
675 struct dirhash *dh;
676 int slot;
677 int dirblksiz = ip->i_lfs->um_dirblksiz;
679 if ((dh = ip->i_dirhash) == NULL)
680 return;
682 DIRHASH_LOCK(dh);
683 if (dh->dh_hash == NULL) {
684 DIRHASH_UNLOCK(dh);
685 ulfsdirhash_free(ip);
686 return;
689 KASSERT(offset < dh->dh_dirblks * dirblksiz);
690 /* Find the entry */
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);
699 DIRHASH_UNLOCK(dh);
703 * Change the offset associated with a directory entry in the hash. Used
704 * when compacting directory blocks.
706 void
707 ulfsdirhash_move(struct inode *ip, LFS_DIRHEADER *dirp, doff_t oldoff,
708 doff_t newoff)
710 struct lfs *fs = ip->i_lfs;
711 struct dirhash *dh;
712 int slot;
714 if ((dh = ip->i_dirhash) == NULL)
715 return;
716 DIRHASH_LOCK(dh);
717 if (dh->dh_hash == NULL) {
718 DIRHASH_UNLOCK(dh);
719 ulfsdirhash_free(ip);
720 return;
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;
729 DIRHASH_UNLOCK(dh);
733 * Inform dirhash that the directory has grown by one block that
734 * begins at offset (i.e. the new length is offset + DIRBLKSIZ).
736 void
737 ulfsdirhash_newblk(struct inode *ip, doff_t offset)
739 struct dirhash *dh;
740 int block;
741 int dirblksiz = ip->i_lfs->um_dirblksiz;
743 if ((dh = ip->i_dirhash) == NULL)
744 return;
745 DIRHASH_LOCK(dh);
746 if (dh->dh_hash == NULL) {
747 DIRHASH_UNLOCK(dh);
748 ulfsdirhash_free(ip);
749 return;
752 KASSERT(offset == dh->dh_dirblks * dirblksiz);
753 block = offset / dirblksiz;
754 if (block >= dh->dh_nblk) {
755 /* Out of space; must rebuild. */
756 DIRHASH_UNLOCK(dh);
757 ulfsdirhash_free(ip);
758 return;
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;
766 DIRHASH_UNLOCK(dh);
770 * Inform dirhash that the directory is being truncated.
772 void
773 ulfsdirhash_dirtrunc(struct inode *ip, doff_t offset)
775 struct dirhash *dh;
776 int block, i;
777 int dirblksiz = ip->i_lfs->um_dirblksiz;
779 if ((dh = ip->i_dirhash) == NULL)
780 return;
782 DIRHASH_LOCK(dh);
783 if (dh->dh_hash == NULL) {
784 DIRHASH_UNLOCK(dh);
785 ulfsdirhash_free(ip);
786 return;
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
795 * if necessary.
797 if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) {
798 DIRHASH_UNLOCK(dh);
799 ulfsdirhash_free(ip);
800 return;
804 * Remove any `first free' information pertaining to the
805 * truncated blocks. All blocks we're removing should be
806 * completely unused.
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;
817 DIRHASH_UNLOCK(dh);
821 * Debugging function to check that the dirhash information about
822 * a directory block matches its actual contents. Panics if a mismatch
823 * is detected.
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.
829 void
830 ulfsdirhash_checkblock(struct inode *ip, char *sbuf, doff_t offset)
832 struct lfs *fs = ip->i_lfs;
833 struct dirhash *dh;
834 LFS_DIRHEADER *dp;
835 int block, ffslot, i, nfree;
836 int dirblksiz = ip->i_lfs->um_dirblksiz;
838 if (!ulfs_dirhashcheck)
839 return;
840 if ((dh = ip->i_dirhash) == NULL)
841 return;
843 DIRHASH_LOCK(dh);
844 if (dh->dh_hash == NULL) {
845 DIRHASH_UNLOCK(dh);
846 ulfsdirhash_free(ip);
847 return;
850 block = offset / dirblksiz;
851 if ((offset & (dirblksiz - 1)) != 0 || block >= dh->dh_dirblks)
852 panic("ulfsdirhash_checkblock: bad offset");
854 nfree = 0;
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) {
861 #if 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.
868 if (i != 0)
869 panic("ulfsdirhash_checkblock: bad dir inode");
870 #endif
871 nfree += lfs_dir_getreclen(fs, dp);
872 continue;
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),
878 offset + i);
880 nfree += lfs_dir_getreclen(fs, dp) - LFS_DIRSIZ(fs, dp);
882 if (i != dirblksiz)
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");
894 DIRHASH_UNLOCK(dh);
898 * Hash the specified filename into a dirhash slot.
900 static int
901 ulfsdirhash_hash(struct dirhash *dh, const char *name, int namelen)
903 u_int32_t hash;
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.
924 static void
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)
944 break;
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.
961 static int
962 ulfsdirhash_findslot(struct dirhash *dh, const char *name, int namelen,
963 doff_t offset)
965 int slot;
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);
978 return (slot);
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.
986 static void
987 ulfsdirhash_delslot(struct dirhash *dh, int slot)
989 int i;
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;
1003 dh->dh_hused--;
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.
1016 static doff_t
1017 ulfsdirhash_getprev(struct lfs *fs, LFS_DIRHEADER *dirp,
1018 doff_t offset, int dirblksiz)
1020 LFS_DIRHEADER *dp;
1021 char *blkbuf;
1022 doff_t blkoff, prevoff;
1023 int entrypos, i;
1024 unsigned reclen;
1026 blkoff = offset & ~(dirblksiz - 1); /* offset of start of block */
1027 entrypos = offset & (dirblksiz - 1); /* entry relative to block */
1028 blkbuf = (char *)dirp - entrypos;
1029 prevoff = blkoff;
1031 /* If `offset' is the start of a block, there is no previous entry. */
1032 if (entrypos == 0)
1033 return (-1);
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;
1043 return (prevoff);
1047 * Try to free up `wanted' bytes by stealing memory from existing
1048 * dirhashes. Returns zero with list locked if successful.
1050 static int
1051 ulfsdirhash_recycle(int wanted)
1053 struct dirhash *dh;
1054 doff_t **hash;
1055 u_int8_t *blkfree;
1056 int i, mem, narrays;
1057 size_t hashsz, blkfreesz;
1059 DIRHASHLIST_LOCK();
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();
1064 return (-1);
1066 DIRHASH_LOCK(dh);
1067 KASSERT(dh->dh_hash != NULL);
1069 /* Decrement the score; only recycle if it becomes zero. */
1070 if (--dh->dh_score > 0) {
1071 DIRHASH_UNLOCK(dh);
1072 DIRHASHLIST_UNLOCK();
1073 return (-1);
1076 /* Remove it from the list and detach its memory. */
1077 TAILQ_REMOVE(&ulfsdirhash_list, dh, dh_list);
1078 dh->dh_onlist = 0;
1079 hash = dh->dh_hash;
1080 hashsz = dh->dh_hashsz;
1081 dh->dh_hash = NULL;
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. */
1091 DIRHASH_UNLOCK(dh);
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. */
1100 DIRHASHLIST_LOCK();
1101 atomic_add_int(&ulfs_dirhashmem, -mem);
1103 /* Success. */
1104 return (0);
1107 static void
1108 ulfsdirhash_sysctl_init(void)
1110 const struct sysctlnode *rnode, *cnode;
1112 sysctl_createv(&ulfsdirhash_sysctl_log, 0, NULL, &rnode,
1113 CTLFLAG_PERMANENT,
1114 CTLTYPE_NODE, "ulfs",
1115 SYSCTL_DESCR("ulfs"),
1116 NULL, 0, NULL, 0,
1117 CTL_VFS, CTL_CREATE, CTL_EOL);
1119 sysctl_createv(&ulfsdirhash_sysctl_log, 0, &rnode, &rnode,
1120 CTLFLAG_PERMANENT,
1121 CTLTYPE_NODE, "dirhash",
1122 SYSCTL_DESCR("dirhash"),
1123 NULL, 0, NULL, 0,
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);
1155 void
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();
1168 void
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);