ufs_dirhash.c revision 187474
1310490Scy/*-
238494Sobrien * Copyright (c) 2001, 2002 Ian Dowse.  All rights reserved.
338494Sobrien *
438494Sobrien * Redistribution and use in source and binary forms, with or without
538494Sobrien * modification, are permitted provided that the following conditions
638494Sobrien * are met:
738494Sobrien * 1. Redistributions of source code must retain the above copyright
838494Sobrien *    notice, this list of conditions and the following disclaimer.
938494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1038494Sobrien *    notice, this list of conditions and the following disclaimer in the
1138494Sobrien *    documentation and/or other materials provided with the distribution.
1238494Sobrien *
1338494Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1438494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1538494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1638494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1738494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18310490Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1938494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2038494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2138494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2238494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2338494Sobrien * SUCH DAMAGE.
2438494Sobrien */
2538494Sobrien
2638494Sobrien/*
2738494Sobrien * This implements a hash-based lookup scheme for UFS directories.
2838494Sobrien */
2938494Sobrien
3038494Sobrien#include <sys/cdefs.h>
3138494Sobrien__FBSDID("$FreeBSD: head/sys/ufs/ufs/ufs_dirhash.c 187474 2009-01-20 16:35:34Z jhb $");
3238494Sobrien
33#include "opt_ufs.h"
34
35#ifdef UFS_DIRHASH
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/lock.h>
41#include <sys/mutex.h>
42#include <sys/malloc.h>
43#include <sys/fnv_hash.h>
44#include <sys/proc.h>
45#include <sys/bio.h>
46#include <sys/buf.h>
47#include <sys/vnode.h>
48#include <sys/mount.h>
49#include <sys/refcount.h>
50#include <sys/sysctl.h>
51#include <sys/sx.h>
52#include <vm/uma.h>
53
54#include <ufs/ufs/quota.h>
55#include <ufs/ufs/inode.h>
56#include <ufs/ufs/dir.h>
57#include <ufs/ufs/dirhash.h>
58#include <ufs/ufs/extattr.h>
59#include <ufs/ufs/ufsmount.h>
60#include <ufs/ufs/ufs_extern.h>
61
62#define WRAPINCR(val, limit)	(((val) + 1 == (limit)) ? 0 : ((val) + 1))
63#define WRAPDECR(val, limit)	(((val) == 0) ? ((limit) - 1) : ((val) - 1))
64#define OFSFMT(vp)		((vp)->v_mount->mnt_maxsymlinklen <= 0)
65#define BLKFREE2IDX(n)		((n) > DH_NFSTATS ? DH_NFSTATS : (n))
66
67static MALLOC_DEFINE(M_DIRHASH, "ufs_dirhash", "UFS directory hash tables");
68
69static SYSCTL_NODE(_vfs, OID_AUTO, ufs, CTLFLAG_RD, 0, "UFS filesystem");
70
71static int ufs_mindirhashsize = DIRBLKSIZ * 5;
72SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_minsize, CTLFLAG_RW,
73    &ufs_mindirhashsize,
74    0, "minimum directory size in bytes for which to use hashed lookup");
75static int ufs_dirhashmaxmem = 2 * 1024 * 1024;
76SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_maxmem, CTLFLAG_RW, &ufs_dirhashmaxmem,
77    0, "maximum allowed dirhash memory usage");
78static int ufs_dirhashmem;
79SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_mem, CTLFLAG_RD, &ufs_dirhashmem,
80    0, "current dirhash memory usage");
81static int ufs_dirhashcheck = 0;
82SYSCTL_INT(_vfs_ufs, OID_AUTO, dirhash_docheck, CTLFLAG_RW, &ufs_dirhashcheck,
83    0, "enable extra sanity tests");
84
85
86static int ufsdirhash_hash(struct dirhash *dh, char *name, int namelen);
87static void ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff);
88static void ufsdirhash_delslot(struct dirhash *dh, int slot);
89static int ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen,
90	   doff_t offset);
91static doff_t ufsdirhash_getprev(struct direct *dp, doff_t offset);
92static int ufsdirhash_recycle(int wanted);
93static void ufsdirhash_free_locked(struct inode *ip);
94
95static uma_zone_t	ufsdirhash_zone;
96
97#define DIRHASHLIST_LOCK() 		mtx_lock(&ufsdirhash_mtx)
98#define DIRHASHLIST_UNLOCK() 		mtx_unlock(&ufsdirhash_mtx)
99#define DIRHASH_BLKALLOC_WAITOK() 	uma_zalloc(ufsdirhash_zone, M_WAITOK)
100#define DIRHASH_BLKFREE(ptr) 		uma_zfree(ufsdirhash_zone, (ptr))
101#define	DIRHASH_ASSERT_LOCKED(dh)					\
102    sx_assert(&(dh)->dh_lock, SA_LOCKED)
103
104/* Dirhash list; recently-used entries are near the tail. */
105static TAILQ_HEAD(, dirhash) ufsdirhash_list;
106
107/* Protects: ufsdirhash_list, `dh_list' field, ufs_dirhashmem. */
108static struct mtx	ufsdirhash_mtx;
109
110/*
111 * Locking:
112 *
113 * The relationship between inode and dirhash is protected either by an
114 * exclusive vnode lock or the vnode interlock where a shared vnode lock
115 * may be used.  The dirhash_mtx is acquired after the dirhash lock.  To
116 * handle teardown races, code wishing to lock the dirhash for an inode
117 * when using a shared vnode lock must obtain a private reference on the
118 * dirhash while holding the vnode interlock.  They can drop it once they
119 * have obtained the dirhash lock and verified that the dirhash wasn't
120 * recycled while they waited for the dirhash lock.
121 *
122 * ufsdirhash_build() acquires a shared lock on the dirhash when it is
123 * successful.  This lock is released after a call to ufsdirhash_lookup().
124 *
125 * Functions requiring exclusive access use ufsdirhash_acquire() which may
126 * free a dirhash structure that was recycled by ufsdirhash_recycle().
127 *
128 * The dirhash lock may be held across io operations.
129 *
130 * WITNESS reports a lock order reversal between the "bufwait" lock
131 * and the "dirhash" lock.  However, this specific reversal will not
132 * cause a deadlock.  To get a deadlock, one would have to lock a
133 * buffer followed by the dirhash while a second thread locked a
134 * buffer while holding the dirhash lock.  The second order can happen
135 * under a shared or exclusive vnode lock for the associated directory
136 * in lookup().  The first order, however, can only happen under an
137 * exclusive vnode lock (e.g. unlink(), rename(), etc.).  Thus, for
138 * a thread to be doing a "bufwait" -> "dirhash" order, it has to hold
139 * an exclusive vnode lock.  That exclusive vnode lock will prevent
140 * any other threads from doing a "dirhash" -> "bufwait" order.
141 */
142
143static void
144ufsdirhash_hold(struct dirhash *dh)
145{
146
147	refcount_acquire(&dh->dh_refcount);
148}
149
150static void
151ufsdirhash_drop(struct dirhash *dh)
152{
153
154	if (refcount_release(&dh->dh_refcount)) {
155		sx_destroy(&dh->dh_lock);
156		free(dh, M_DIRHASH);
157	}
158}
159
160/*
161 * Release the lock on a dirhash.
162 */
163static void
164ufsdirhash_release(struct dirhash *dh)
165{
166
167	sx_unlock(&dh->dh_lock);
168}
169
170/*
171 * Either acquire an existing hash locked shared or create a new hash and
172 * return it exclusively locked.  May return NULL if the allocation fails.
173 *
174 * The vnode interlock is used to protect the i_dirhash pointer from
175 * simultaneous access while only a shared vnode lock is held.
176 */
177static struct dirhash *
178ufsdirhash_create(struct inode *ip)
179{
180	struct dirhash *ndh;
181	struct dirhash *dh;
182	struct vnode *vp;
183	int error;
184
185	error = 0;
186	ndh = dh = NULL;
187	vp = ip->i_vnode;
188	for (;;) {
189		/* Racy check for i_dirhash to prefetch a dirhash structure. */
190		if (ip->i_dirhash == NULL && ndh == NULL) {
191			ndh = malloc(sizeof *dh, M_DIRHASH,
192			    M_NOWAIT | M_ZERO);
193			if (ndh == NULL)
194				return (NULL);
195			refcount_init(&ndh->dh_refcount, 1);
196
197			/*
198			 * The DUPOK is to prevent warnings from the
199			 * sx_slock() a few lines down which is safe
200			 * since the duplicate lock in that case is
201			 * the one for this dirhash we are creating
202			 * now which has no external references until
203			 * after this function returns.
204			 */
205			sx_init_flags(&ndh->dh_lock, "dirhash", SX_DUPOK);
206			sx_xlock(&ndh->dh_lock);
207		}
208		/*
209		 * Check i_dirhash.  If it's NULL just try to use a
210		 * preallocated structure.  If none exists loop and try again.
211		 */
212		VI_LOCK(vp);
213		dh = ip->i_dirhash;
214		if (dh == NULL) {
215			ip->i_dirhash = ndh;
216			VI_UNLOCK(vp);
217			if (ndh == NULL)
218				continue;
219			return (ndh);
220		}
221		ufsdirhash_hold(dh);
222		VI_UNLOCK(vp);
223
224		/* Acquire a shared lock on existing hashes. */
225		sx_slock(&dh->dh_lock);
226
227		/* The hash could've been recycled while we were waiting. */
228		VI_LOCK(vp);
229		if (ip->i_dirhash != dh) {
230			VI_UNLOCK(vp);
231			ufsdirhash_release(dh);
232			ufsdirhash_drop(dh);
233			continue;
234		}
235		VI_UNLOCK(vp);
236		ufsdirhash_drop(dh);
237
238		/* If the hash is still valid we've succeeded. */
239		if (dh->dh_hash != NULL)
240			break;
241		/*
242		 * If the hash is NULL it has been recycled.  Try to upgrade
243		 * so we can recreate it.  If we fail the upgrade, drop our
244		 * lock and try again.
245		 */
246		if (sx_try_upgrade(&dh->dh_lock))
247			break;
248		sx_sunlock(&dh->dh_lock);
249	}
250	/* Free the preallocated structure if it was not necessary. */
251	if (ndh) {
252		ufsdirhash_release(ndh);
253		ufsdirhash_drop(ndh);
254	}
255	return (dh);
256}
257
258/*
259 * Acquire an exclusive lock on an existing hash.  Requires an exclusive
260 * vnode lock to protect the i_dirhash pointer.  hashes that have been
261 * recycled are reclaimed here and NULL is returned.
262 */
263static struct dirhash *
264ufsdirhash_acquire(struct inode *ip)
265{
266	struct dirhash *dh;
267	struct vnode *vp;
268
269	ASSERT_VOP_ELOCKED(ip->i_vnode, __FUNCTION__);
270
271	vp = ip->i_vnode;
272	dh = ip->i_dirhash;
273	if (dh == NULL)
274		return (NULL);
275	sx_xlock(&dh->dh_lock);
276	if (dh->dh_hash != NULL)
277		return (dh);
278	ufsdirhash_free_locked(ip);
279	return (NULL);
280}
281
282/*
283 * Acquire exclusively and free the hash pointed to by ip.  Works with a
284 * shared or exclusive vnode lock.
285 */
286void
287ufsdirhash_free(struct inode *ip)
288{
289	struct dirhash *dh;
290	struct vnode *vp;
291
292	vp = ip->i_vnode;
293	for (;;) {
294		/* Grab a reference on this inode's dirhash if it has one. */
295		VI_LOCK(vp);
296		dh = ip->i_dirhash;
297		if (dh == NULL) {
298			VI_UNLOCK(vp);
299			return;
300		}
301		ufsdirhash_hold(dh);
302		VI_UNLOCK(vp);
303
304		/* Exclusively lock the dirhash. */
305		sx_xlock(&dh->dh_lock);
306
307		/* If this dirhash still belongs to this inode, then free it. */
308		VI_LOCK(vp);
309		if (ip->i_dirhash == dh) {
310			VI_UNLOCK(vp);
311			ufsdirhash_drop(dh);
312			break;
313		}
314		VI_UNLOCK(vp);
315
316		/*
317		 * This inode's dirhash has changed while we were
318		 * waiting for the dirhash lock, so try again.
319		 */
320		ufsdirhash_release(dh);
321		ufsdirhash_drop(dh);
322	}
323	ufsdirhash_free_locked(ip);
324}
325
326/*
327 * Attempt to build up a hash table for the directory contents in
328 * inode 'ip'. Returns 0 on success, or -1 of the operation failed.
329 */
330int
331ufsdirhash_build(struct inode *ip)
332{
333	struct dirhash *dh;
334	struct buf *bp = NULL;
335	struct direct *ep;
336	struct vnode *vp;
337	doff_t bmask, pos;
338	int dirblocks, i, j, memreqd, nblocks, narrays, nslots, slot;
339
340	/* Take care of a decreased sysctl value. */
341	while (ufs_dirhashmem > ufs_dirhashmaxmem)
342		if (ufsdirhash_recycle(0) != 0)
343			return (-1);
344
345	/* Check if we can/should use dirhash. */
346	if (ip->i_size < ufs_mindirhashsize || OFSFMT(ip->i_vnode) ||
347	    ip->i_effnlink == 0) {
348		if (ip->i_dirhash)
349			ufsdirhash_free(ip);
350		return (-1);
351	}
352	dh = ufsdirhash_create(ip);
353	if (dh == NULL)
354		return (-1);
355	if (dh->dh_hash != NULL)
356		return (0);
357
358	vp = ip->i_vnode;
359	/* Allocate 50% more entries than this dir size could ever need. */
360	KASSERT(ip->i_size >= DIRBLKSIZ, ("ufsdirhash_build size"));
361	nslots = ip->i_size / DIRECTSIZ(1);
362	nslots = (nslots * 3 + 1) / 2;
363	narrays = howmany(nslots, DH_NBLKOFF);
364	nslots = narrays * DH_NBLKOFF;
365	dirblocks = howmany(ip->i_size, DIRBLKSIZ);
366	nblocks = (dirblocks * 3 + 1) / 2;
367	memreqd = sizeof(*dh) + narrays * sizeof(*dh->dh_hash) +
368	    narrays * DH_NBLKOFF * sizeof(**dh->dh_hash) +
369	    nblocks * sizeof(*dh->dh_blkfree);
370	DIRHASHLIST_LOCK();
371	if (memreqd + ufs_dirhashmem > ufs_dirhashmaxmem) {
372		DIRHASHLIST_UNLOCK();
373		if (memreqd > ufs_dirhashmaxmem / 2)
374			goto fail;
375		/* Try to free some space. */
376		if (ufsdirhash_recycle(memreqd) != 0)
377			goto fail;
378		/* Enough was freed, and list has been locked. */
379	}
380	ufs_dirhashmem += memreqd;
381	DIRHASHLIST_UNLOCK();
382
383	/* Initialise the hash table and block statistics. */
384	dh->dh_memreq = memreqd;
385	dh->dh_narrays = narrays;
386	dh->dh_hlen = nslots;
387	dh->dh_nblk = nblocks;
388	dh->dh_dirblks = dirblocks;
389	for (i = 0; i < DH_NFSTATS; i++)
390		dh->dh_firstfree[i] = -1;
391	dh->dh_firstfree[DH_NFSTATS] = 0;
392	dh->dh_hused = 0;
393	dh->dh_seqopt = 0;
394	dh->dh_seqoff = 0;
395	dh->dh_score = DH_SCOREINIT;
396
397	/*
398	 * Use non-blocking mallocs so that we will revert to a linear
399	 * lookup on failure rather than potentially blocking forever.
400	 */
401	dh->dh_hash = malloc(narrays * sizeof(dh->dh_hash[0]),
402	    M_DIRHASH, M_NOWAIT | M_ZERO);
403	if (dh->dh_hash == NULL)
404		goto fail;
405	dh->dh_blkfree = malloc(nblocks * sizeof(dh->dh_blkfree[0]),
406	    M_DIRHASH, M_NOWAIT);
407	if (dh->dh_blkfree == NULL)
408		goto fail;
409	for (i = 0; i < narrays; i++) {
410		if ((dh->dh_hash[i] = DIRHASH_BLKALLOC_WAITOK()) == NULL)
411			goto fail;
412		for (j = 0; j < DH_NBLKOFF; j++)
413			dh->dh_hash[i][j] = DIRHASH_EMPTY;
414	}
415	for (i = 0; i < dirblocks; i++)
416		dh->dh_blkfree[i] = DIRBLKSIZ / DIRALIGN;
417	bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
418	pos = 0;
419	while (pos < ip->i_size) {
420		/* If necessary, get the next directory block. */
421		if ((pos & bmask) == 0) {
422			if (bp != NULL)
423				brelse(bp);
424			if (UFS_BLKATOFF(vp, (off_t)pos, NULL, &bp) != 0)
425				goto fail;
426		}
427
428		/* Add this entry to the hash. */
429		ep = (struct direct *)((char *)bp->b_data + (pos & bmask));
430		if (ep->d_reclen == 0 || ep->d_reclen >
431		    DIRBLKSIZ - (pos & (DIRBLKSIZ - 1))) {
432			/* Corrupted directory. */
433			brelse(bp);
434			goto fail;
435		}
436		if (ep->d_ino != 0) {
437			/* Add the entry (simplified ufsdirhash_add). */
438			slot = ufsdirhash_hash(dh, ep->d_name, ep->d_namlen);
439			while (DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
440				slot = WRAPINCR(slot, dh->dh_hlen);
441			dh->dh_hused++;
442			DH_ENTRY(dh, slot) = pos;
443			ufsdirhash_adjfree(dh, pos, -DIRSIZ(0, ep));
444		}
445		pos += ep->d_reclen;
446	}
447
448	if (bp != NULL)
449		brelse(bp);
450	DIRHASHLIST_LOCK();
451	TAILQ_INSERT_TAIL(&ufsdirhash_list, dh, dh_list);
452	dh->dh_onlist = 1;
453	DIRHASHLIST_UNLOCK();
454	sx_downgrade(&dh->dh_lock);
455	return (0);
456
457fail:
458	ufsdirhash_free_locked(ip);
459	return (-1);
460}
461
462/*
463 * Free any hash table associated with inode 'ip'.
464 */
465static void
466ufsdirhash_free_locked(struct inode *ip)
467{
468	struct dirhash *dh;
469	struct vnode *vp;
470	int i;
471
472	DIRHASH_ASSERT_LOCKED(ip->i_dirhash);
473
474	/*
475	 * Clear the pointer in the inode to prevent new threads from
476	 * finding the dead structure.
477	 */
478	vp = ip->i_vnode;
479	VI_LOCK(vp);
480	dh = ip->i_dirhash;
481	ip->i_dirhash = NULL;
482	VI_UNLOCK(vp);
483
484	/*
485	 * Remove the hash from the list since we are going to free its
486	 * memory.
487	 */
488	DIRHASHLIST_LOCK();
489	if (dh->dh_onlist)
490		TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
491	ufs_dirhashmem -= dh->dh_memreq;
492	DIRHASHLIST_UNLOCK();
493
494	/*
495	 * At this point, any waiters for the lock should hold their
496	 * own reference on the dirhash structure.  They will drop
497	 * that reference once they grab the vnode interlock and see
498	 * that ip->i_dirhash is NULL.
499	 */
500	sx_xunlock(&dh->dh_lock);
501
502	/*
503	 * Handle partially recycled as well as fully constructed hashes.
504	 */
505	if (dh->dh_hash != NULL) {
506		for (i = 0; i < dh->dh_narrays; i++)
507			if (dh->dh_hash[i] != NULL)
508				DIRHASH_BLKFREE(dh->dh_hash[i]);
509		free(dh->dh_hash, M_DIRHASH);
510		if (dh->dh_blkfree != NULL)
511			free(dh->dh_blkfree, M_DIRHASH);
512	}
513
514	/*
515	 * Drop the inode's reference to the data structure.
516	 */
517	ufsdirhash_drop(dh);
518}
519
520/*
521 * Find the offset of the specified name within the given inode.
522 * Returns 0 on success, ENOENT if the entry does not exist, or
523 * EJUSTRETURN if the caller should revert to a linear search.
524 *
525 * If successful, the directory offset is stored in *offp, and a
526 * pointer to a struct buf containing the entry is stored in *bpp. If
527 * prevoffp is non-NULL, the offset of the previous entry within
528 * the DIRBLKSIZ-sized block is stored in *prevoffp (if the entry
529 * is the first in a block, the start of the block is used).
530 *
531 * Must be called with the hash locked.  Returns with the hash unlocked.
532 */
533int
534ufsdirhash_lookup(struct inode *ip, char *name, int namelen, doff_t *offp,
535    struct buf **bpp, doff_t *prevoffp)
536{
537	struct dirhash *dh, *dh_next;
538	struct direct *dp;
539	struct vnode *vp;
540	struct buf *bp;
541	doff_t blkoff, bmask, offset, prevoff;
542	int i, slot;
543	int error;
544
545	dh = ip->i_dirhash;
546	KASSERT(dh != NULL && dh->dh_hash != NULL,
547	    ("ufsdirhash_lookup: Invalid dirhash %p\n", dh));
548	DIRHASH_ASSERT_LOCKED(dh);
549	/*
550	 * Move this dirhash towards the end of the list if it has a
551	 * score higher than the next entry, and acquire the dh_lock.
552	 */
553	DIRHASHLIST_LOCK();
554	if (TAILQ_NEXT(dh, dh_list) != NULL) {
555		/*
556		 * If the new score will be greater than that of the next
557		 * entry, then move this entry past it. With both mutexes
558		 * held, dh_next won't go away, but its dh_score could
559		 * change; that's not important since it is just a hint.
560		 */
561		if ((dh_next = TAILQ_NEXT(dh, dh_list)) != NULL &&
562		    dh->dh_score >= dh_next->dh_score) {
563			KASSERT(dh->dh_onlist, ("dirhash: not on list"));
564			TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
565			TAILQ_INSERT_AFTER(&ufsdirhash_list, dh_next, dh,
566			    dh_list);
567		}
568	}
569	/* Update the score. */
570	if (dh->dh_score < DH_SCOREMAX)
571		dh->dh_score++;
572	DIRHASHLIST_UNLOCK();
573
574	vp = ip->i_vnode;
575	bmask = VFSTOUFS(vp->v_mount)->um_mountp->mnt_stat.f_iosize - 1;
576	blkoff = -1;
577	bp = NULL;
578restart:
579	slot = ufsdirhash_hash(dh, name, namelen);
580
581	if (dh->dh_seqopt) {
582		/*
583		 * Sequential access optimisation. dh_seqoff contains the
584		 * offset of the directory entry immediately following
585		 * the last entry that was looked up. Check if this offset
586		 * appears in the hash chain for the name we are looking for.
587		 */
588		for (i = slot; (offset = DH_ENTRY(dh, i)) != DIRHASH_EMPTY;
589		    i = WRAPINCR(i, dh->dh_hlen))
590			if (offset == dh->dh_seqoff)
591				break;
592		if (offset == dh->dh_seqoff) {
593			/*
594			 * We found an entry with the expected offset. This
595			 * is probably the entry we want, but if not, the
596			 * code below will turn off seqopt and retry.
597			 */
598			slot = i;
599		} else
600			dh->dh_seqopt = 0;
601	}
602
603	for (; (offset = DH_ENTRY(dh, slot)) != DIRHASH_EMPTY;
604	    slot = WRAPINCR(slot, dh->dh_hlen)) {
605		if (offset == DIRHASH_DEL)
606			continue;
607		if (offset < 0 || offset >= ip->i_size)
608			panic("ufsdirhash_lookup: bad offset in hash array");
609		if ((offset & ~bmask) != blkoff) {
610			if (bp != NULL)
611				brelse(bp);
612			blkoff = offset & ~bmask;
613			if (UFS_BLKATOFF(vp, (off_t)blkoff, NULL, &bp) != 0) {
614				error = EJUSTRETURN;
615				goto fail;
616			}
617		}
618		dp = (struct direct *)(bp->b_data + (offset & bmask));
619		if (dp->d_reclen == 0 || dp->d_reclen >
620		    DIRBLKSIZ - (offset & (DIRBLKSIZ - 1))) {
621			/* Corrupted directory. */
622			error = EJUSTRETURN;
623			goto fail;
624		}
625		if (dp->d_namlen == namelen &&
626		    bcmp(dp->d_name, name, namelen) == 0) {
627			/* Found. Get the prev offset if needed. */
628			if (prevoffp != NULL) {
629				if (offset & (DIRBLKSIZ - 1)) {
630					prevoff = ufsdirhash_getprev(dp,
631					    offset);
632					if (prevoff == -1) {
633						error = EJUSTRETURN;
634						goto fail;
635					}
636				} else
637					prevoff = offset;
638				*prevoffp = prevoff;
639			}
640
641			/* Check for sequential access, and update offset. */
642			if (dh->dh_seqopt == 0 && dh->dh_seqoff == offset)
643				dh->dh_seqopt = 1;
644			dh->dh_seqoff = offset + DIRSIZ(0, dp);
645			*bpp = bp;
646			*offp = offset;
647			ufsdirhash_release(dh);
648			return (0);
649		}
650
651		/*
652		 * When the name doesn't match in the seqopt case, go back
653		 * and search normally.
654		 */
655		if (dh->dh_seqopt) {
656			dh->dh_seqopt = 0;
657			goto restart;
658		}
659	}
660	error = ENOENT;
661fail:
662	ufsdirhash_release(dh);
663	if (bp != NULL)
664		brelse(bp);
665	return (error);
666}
667
668/*
669 * Find a directory block with room for 'slotneeded' bytes. Returns
670 * the offset of the directory entry that begins the free space.
671 * This will either be the offset of an existing entry that has free
672 * space at the end, or the offset of an entry with d_ino == 0 at
673 * the start of a DIRBLKSIZ block.
674 *
675 * To use the space, the caller may need to compact existing entries in
676 * the directory. The total number of bytes in all of the entries involved
677 * in the compaction is stored in *slotsize. In other words, all of
678 * the entries that must be compacted are exactly contained in the
679 * region beginning at the returned offset and spanning *slotsize bytes.
680 *
681 * Returns -1 if no space was found, indicating that the directory
682 * must be extended.
683 */
684doff_t
685ufsdirhash_findfree(struct inode *ip, int slotneeded, int *slotsize)
686{
687	struct direct *dp;
688	struct dirhash *dh;
689	struct buf *bp;
690	doff_t pos, slotstart;
691	int dirblock, error, freebytes, i;
692
693	dh = ip->i_dirhash;
694	KASSERT(dh != NULL && dh->dh_hash != NULL,
695	    ("ufsdirhash_findfree: Invalid dirhash %p\n", dh));
696	DIRHASH_ASSERT_LOCKED(dh);
697
698	/* Find a directory block with the desired free space. */
699	dirblock = -1;
700	for (i = howmany(slotneeded, DIRALIGN); i <= DH_NFSTATS; i++)
701		if ((dirblock = dh->dh_firstfree[i]) != -1)
702			break;
703	if (dirblock == -1)
704		return (-1);
705
706	KASSERT(dirblock < dh->dh_nblk &&
707	    dh->dh_blkfree[dirblock] >= howmany(slotneeded, DIRALIGN),
708	    ("ufsdirhash_findfree: bad stats"));
709	pos = dirblock * DIRBLKSIZ;
710	error = UFS_BLKATOFF(ip->i_vnode, (off_t)pos, (char **)&dp, &bp);
711	if (error)
712		return (-1);
713
714	/* Find the first entry with free space. */
715	for (i = 0; i < DIRBLKSIZ; ) {
716		if (dp->d_reclen == 0) {
717			brelse(bp);
718			return (-1);
719		}
720		if (dp->d_ino == 0 || dp->d_reclen > DIRSIZ(0, dp))
721			break;
722		i += dp->d_reclen;
723		dp = (struct direct *)((char *)dp + dp->d_reclen);
724	}
725	if (i > DIRBLKSIZ) {
726		brelse(bp);
727		return (-1);
728	}
729	slotstart = pos + i;
730
731	/* Find the range of entries needed to get enough space */
732	freebytes = 0;
733	while (i < DIRBLKSIZ && freebytes < slotneeded) {
734		freebytes += dp->d_reclen;
735		if (dp->d_ino != 0)
736			freebytes -= DIRSIZ(0, dp);
737		if (dp->d_reclen == 0) {
738			brelse(bp);
739			return (-1);
740		}
741		i += dp->d_reclen;
742		dp = (struct direct *)((char *)dp + dp->d_reclen);
743	}
744	if (i > DIRBLKSIZ) {
745		brelse(bp);
746		return (-1);
747	}
748	if (freebytes < slotneeded)
749		panic("ufsdirhash_findfree: free mismatch");
750	brelse(bp);
751	*slotsize = pos + i - slotstart;
752	return (slotstart);
753}
754
755/*
756 * Return the start of the unused space at the end of a directory, or
757 * -1 if there are no trailing unused blocks.
758 */
759doff_t
760ufsdirhash_enduseful(struct inode *ip)
761{
762
763	struct dirhash *dh;
764	int i;
765
766	dh = ip->i_dirhash;
767	DIRHASH_ASSERT_LOCKED(dh);
768	KASSERT(dh != NULL && dh->dh_hash != NULL,
769	    ("ufsdirhash_enduseful: Invalid dirhash %p\n", dh));
770
771	if (dh->dh_blkfree[dh->dh_dirblks - 1] != DIRBLKSIZ / DIRALIGN)
772		return (-1);
773
774	for (i = dh->dh_dirblks - 1; i >= 0; i--)
775		if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN)
776			break;
777
778	return ((doff_t)(i + 1) * DIRBLKSIZ);
779}
780
781/*
782 * Insert information into the hash about a new directory entry. dirp
783 * points to a struct direct containing the entry, and offset specifies
784 * the offset of this entry.
785 */
786void
787ufsdirhash_add(struct inode *ip, struct direct *dirp, doff_t offset)
788{
789	struct dirhash *dh;
790	int slot;
791
792	if ((dh = ufsdirhash_acquire(ip)) == NULL)
793		return;
794
795	KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ,
796	    ("ufsdirhash_add: bad offset"));
797	/*
798	 * Normal hash usage is < 66%. If the usage gets too high then
799	 * remove the hash entirely and let it be rebuilt later.
800	 */
801	if (dh->dh_hused >= (dh->dh_hlen * 3) / 4) {
802		ufsdirhash_free_locked(ip);
803		return;
804	}
805
806	/* Find a free hash slot (empty or deleted), and add the entry. */
807	slot = ufsdirhash_hash(dh, dirp->d_name, dirp->d_namlen);
808	while (DH_ENTRY(dh, slot) >= 0)
809		slot = WRAPINCR(slot, dh->dh_hlen);
810	if (DH_ENTRY(dh, slot) == DIRHASH_EMPTY)
811		dh->dh_hused++;
812	DH_ENTRY(dh, slot) = offset;
813
814	/* Update the per-block summary info. */
815	ufsdirhash_adjfree(dh, offset, -DIRSIZ(0, dirp));
816	ufsdirhash_release(dh);
817}
818
819/*
820 * Remove the specified directory entry from the hash. The entry to remove
821 * is defined by the name in `dirp', which must exist at the specified
822 * `offset' within the directory.
823 */
824void
825ufsdirhash_remove(struct inode *ip, struct direct *dirp, doff_t offset)
826{
827	struct dirhash *dh;
828	int slot;
829
830	if ((dh = ufsdirhash_acquire(ip)) == NULL)
831		return;
832
833	KASSERT(offset < dh->dh_dirblks * DIRBLKSIZ,
834	    ("ufsdirhash_remove: bad offset"));
835	/* Find the entry */
836	slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, offset);
837
838	/* Remove the hash entry. */
839	ufsdirhash_delslot(dh, slot);
840
841	/* Update the per-block summary info. */
842	ufsdirhash_adjfree(dh, offset, DIRSIZ(0, dirp));
843	ufsdirhash_release(dh);
844}
845
846/*
847 * Change the offset associated with a directory entry in the hash. Used
848 * when compacting directory blocks.
849 */
850void
851ufsdirhash_move(struct inode *ip, struct direct *dirp, doff_t oldoff,
852    doff_t newoff)
853{
854	struct dirhash *dh;
855	int slot;
856
857	if ((dh = ufsdirhash_acquire(ip)) == NULL)
858		return;
859
860	KASSERT(oldoff < dh->dh_dirblks * DIRBLKSIZ &&
861	    newoff < dh->dh_dirblks * DIRBLKSIZ,
862	    ("ufsdirhash_move: bad offset"));
863	/* Find the entry, and update the offset. */
864	slot = ufsdirhash_findslot(dh, dirp->d_name, dirp->d_namlen, oldoff);
865	DH_ENTRY(dh, slot) = newoff;
866	ufsdirhash_release(dh);
867}
868
869/*
870 * Inform dirhash that the directory has grown by one block that
871 * begins at offset (i.e. the new length is offset + DIRBLKSIZ).
872 */
873void
874ufsdirhash_newblk(struct inode *ip, doff_t offset)
875{
876	struct dirhash *dh;
877	int block;
878
879	if ((dh = ufsdirhash_acquire(ip)) == NULL)
880		return;
881
882	KASSERT(offset == dh->dh_dirblks * DIRBLKSIZ,
883	    ("ufsdirhash_newblk: bad offset"));
884	block = offset / DIRBLKSIZ;
885	if (block >= dh->dh_nblk) {
886		/* Out of space; must rebuild. */
887		ufsdirhash_free_locked(ip);
888		return;
889	}
890	dh->dh_dirblks = block + 1;
891
892	/* Account for the new free block. */
893	dh->dh_blkfree[block] = DIRBLKSIZ / DIRALIGN;
894	if (dh->dh_firstfree[DH_NFSTATS] == -1)
895		dh->dh_firstfree[DH_NFSTATS] = block;
896	ufsdirhash_release(dh);
897}
898
899/*
900 * Inform dirhash that the directory is being truncated.
901 */
902void
903ufsdirhash_dirtrunc(struct inode *ip, doff_t offset)
904{
905	struct dirhash *dh;
906	int block, i;
907
908	if ((dh = ufsdirhash_acquire(ip)) == NULL)
909		return;
910
911	KASSERT(offset <= dh->dh_dirblks * DIRBLKSIZ,
912	    ("ufsdirhash_dirtrunc: bad offset"));
913	block = howmany(offset, DIRBLKSIZ);
914	/*
915	 * If the directory shrinks to less than 1/8 of dh_nblk blocks
916	 * (about 20% of its original size due to the 50% extra added in
917	 * ufsdirhash_build) then free it, and let the caller rebuild
918	 * if necessary.
919	 */
920	if (block < dh->dh_nblk / 8 && dh->dh_narrays > 1) {
921		ufsdirhash_free_locked(ip);
922		return;
923	}
924
925	/*
926	 * Remove any `first free' information pertaining to the
927	 * truncated blocks. All blocks we're removing should be
928	 * completely unused.
929	 */
930	if (dh->dh_firstfree[DH_NFSTATS] >= block)
931		dh->dh_firstfree[DH_NFSTATS] = -1;
932	for (i = block; i < dh->dh_dirblks; i++)
933		if (dh->dh_blkfree[i] != DIRBLKSIZ / DIRALIGN)
934			panic("ufsdirhash_dirtrunc: blocks in use");
935	for (i = 0; i < DH_NFSTATS; i++)
936		if (dh->dh_firstfree[i] >= block)
937			panic("ufsdirhash_dirtrunc: first free corrupt");
938	dh->dh_dirblks = block;
939	ufsdirhash_release(dh);
940}
941
942/*
943 * Debugging function to check that the dirhash information about
944 * a directory block matches its actual contents. Panics if a mismatch
945 * is detected.
946 *
947 * On entry, `buf' should point to the start of an in-core
948 * DIRBLKSIZ-sized directory block, and `offset' should contain the
949 * offset from the start of the directory of that block.
950 */
951void
952ufsdirhash_checkblock(struct inode *ip, char *buf, doff_t offset)
953{
954	struct dirhash *dh;
955	struct direct *dp;
956	int block, ffslot, i, nfree;
957
958	if (!ufs_dirhashcheck)
959		return;
960	if ((dh = ufsdirhash_acquire(ip)) == NULL)
961		return;
962
963	block = offset / DIRBLKSIZ;
964	if ((offset & (DIRBLKSIZ - 1)) != 0 || block >= dh->dh_dirblks)
965		panic("ufsdirhash_checkblock: bad offset");
966
967	nfree = 0;
968	for (i = 0; i < DIRBLKSIZ; i += dp->d_reclen) {
969		dp = (struct direct *)(buf + i);
970		if (dp->d_reclen == 0 || i + dp->d_reclen > DIRBLKSIZ)
971			panic("ufsdirhash_checkblock: bad dir");
972
973		if (dp->d_ino == 0) {
974#if 0
975			/*
976			 * XXX entries with d_ino == 0 should only occur
977			 * at the start of a DIRBLKSIZ block. However the
978			 * ufs code is tolerant of such entries at other
979			 * offsets, and fsck does not fix them.
980			 */
981			if (i != 0)
982				panic("ufsdirhash_checkblock: bad dir inode");
983#endif
984			nfree += dp->d_reclen;
985			continue;
986		}
987
988		/* Check that the entry	exists (will panic if it doesn't). */
989		ufsdirhash_findslot(dh, dp->d_name, dp->d_namlen, offset + i);
990
991		nfree += dp->d_reclen - DIRSIZ(0, dp);
992	}
993	if (i != DIRBLKSIZ)
994		panic("ufsdirhash_checkblock: bad dir end");
995
996	if (dh->dh_blkfree[block] * DIRALIGN != nfree)
997		panic("ufsdirhash_checkblock: bad free count");
998
999	ffslot = BLKFREE2IDX(nfree / DIRALIGN);
1000	for (i = 0; i <= DH_NFSTATS; i++)
1001		if (dh->dh_firstfree[i] == block && i != ffslot)
1002			panic("ufsdirhash_checkblock: bad first-free");
1003	if (dh->dh_firstfree[ffslot] == -1)
1004		panic("ufsdirhash_checkblock: missing first-free entry");
1005	ufsdirhash_release(dh);
1006}
1007
1008/*
1009 * Hash the specified filename into a dirhash slot.
1010 */
1011static int
1012ufsdirhash_hash(struct dirhash *dh, char *name, int namelen)
1013{
1014	u_int32_t hash;
1015
1016	/*
1017	 * We hash the name and then some other bit of data that is
1018	 * invariant over the dirhash's lifetime. Otherwise names
1019	 * differing only in the last byte are placed close to one
1020	 * another in the table, which is bad for linear probing.
1021	 */
1022	hash = fnv_32_buf(name, namelen, FNV1_32_INIT);
1023	hash = fnv_32_buf(&dh, sizeof(dh), hash);
1024	return (hash % dh->dh_hlen);
1025}
1026
1027/*
1028 * Adjust the number of free bytes in the block containing `offset'
1029 * by the value specified by `diff'.
1030 *
1031 * The caller must ensure we have exclusive access to `dh'; normally
1032 * that means that dh_lock should be held, but this is also called
1033 * from ufsdirhash_build() where exclusive access can be assumed.
1034 */
1035static void
1036ufsdirhash_adjfree(struct dirhash *dh, doff_t offset, int diff)
1037{
1038	int block, i, nfidx, ofidx;
1039
1040	/* Update the per-block summary info. */
1041	block = offset / DIRBLKSIZ;
1042	KASSERT(block < dh->dh_nblk && block < dh->dh_dirblks,
1043	     ("dirhash bad offset"));
1044	ofidx = BLKFREE2IDX(dh->dh_blkfree[block]);
1045	dh->dh_blkfree[block] = (int)dh->dh_blkfree[block] + (diff / DIRALIGN);
1046	nfidx = BLKFREE2IDX(dh->dh_blkfree[block]);
1047
1048	/* Update the `first free' list if necessary. */
1049	if (ofidx != nfidx) {
1050		/* If removing, scan forward for the next block. */
1051		if (dh->dh_firstfree[ofidx] == block) {
1052			for (i = block + 1; i < dh->dh_dirblks; i++)
1053				if (BLKFREE2IDX(dh->dh_blkfree[i]) == ofidx)
1054					break;
1055			dh->dh_firstfree[ofidx] = (i < dh->dh_dirblks) ? i : -1;
1056		}
1057
1058		/* Make this the new `first free' if necessary */
1059		if (dh->dh_firstfree[nfidx] > block ||
1060		    dh->dh_firstfree[nfidx] == -1)
1061			dh->dh_firstfree[nfidx] = block;
1062	}
1063}
1064
1065/*
1066 * Find the specified name which should have the specified offset.
1067 * Returns a slot number, and panics on failure.
1068 *
1069 * `dh' must be locked on entry and remains so on return.
1070 */
1071static int
1072ufsdirhash_findslot(struct dirhash *dh, char *name, int namelen, doff_t offset)
1073{
1074	int slot;
1075
1076	DIRHASH_ASSERT_LOCKED(dh);
1077
1078	/* Find the entry. */
1079	KASSERT(dh->dh_hused < dh->dh_hlen, ("dirhash find full"));
1080	slot = ufsdirhash_hash(dh, name, namelen);
1081	while (DH_ENTRY(dh, slot) != offset &&
1082	    DH_ENTRY(dh, slot) != DIRHASH_EMPTY)
1083		slot = WRAPINCR(slot, dh->dh_hlen);
1084	if (DH_ENTRY(dh, slot) != offset)
1085		panic("ufsdirhash_findslot: '%.*s' not found", namelen, name);
1086
1087	return (slot);
1088}
1089
1090/*
1091 * Remove the entry corresponding to the specified slot from the hash array.
1092 *
1093 * `dh' must be locked on entry and remains so on return.
1094 */
1095static void
1096ufsdirhash_delslot(struct dirhash *dh, int slot)
1097{
1098	int i;
1099
1100	DIRHASH_ASSERT_LOCKED(dh);
1101
1102	/* Mark the entry as deleted. */
1103	DH_ENTRY(dh, slot) = DIRHASH_DEL;
1104
1105	/* If this is the end of a chain of DIRHASH_DEL slots, remove them. */
1106	for (i = slot; DH_ENTRY(dh, i) == DIRHASH_DEL; )
1107		i = WRAPINCR(i, dh->dh_hlen);
1108	if (DH_ENTRY(dh, i) == DIRHASH_EMPTY) {
1109		i = WRAPDECR(i, dh->dh_hlen);
1110		while (DH_ENTRY(dh, i) == DIRHASH_DEL) {
1111			DH_ENTRY(dh, i) = DIRHASH_EMPTY;
1112			dh->dh_hused--;
1113			i = WRAPDECR(i, dh->dh_hlen);
1114		}
1115		KASSERT(dh->dh_hused >= 0, ("ufsdirhash_delslot neg hlen"));
1116	}
1117}
1118
1119/*
1120 * Given a directory entry and its offset, find the offset of the
1121 * previous entry in the same DIRBLKSIZ-sized block. Returns an
1122 * offset, or -1 if there is no previous entry in the block or some
1123 * other problem occurred.
1124 */
1125static doff_t
1126ufsdirhash_getprev(struct direct *dirp, doff_t offset)
1127{
1128	struct direct *dp;
1129	char *blkbuf;
1130	doff_t blkoff, prevoff;
1131	int entrypos, i;
1132
1133	blkoff = offset & ~(DIRBLKSIZ - 1);	/* offset of start of block */
1134	entrypos = offset & (DIRBLKSIZ - 1);	/* entry relative to block */
1135	blkbuf = (char *)dirp - entrypos;
1136	prevoff = blkoff;
1137
1138	/* If `offset' is the start of a block, there is no previous entry. */
1139	if (entrypos == 0)
1140		return (-1);
1141
1142	/* Scan from the start of the block until we get to the entry. */
1143	for (i = 0; i < entrypos; i += dp->d_reclen) {
1144		dp = (struct direct *)(blkbuf + i);
1145		if (dp->d_reclen == 0 || i + dp->d_reclen > entrypos)
1146			return (-1);	/* Corrupted directory. */
1147		prevoff = blkoff + i;
1148	}
1149	return (prevoff);
1150}
1151
1152/*
1153 * Try to free up `wanted' bytes by stealing memory from existing
1154 * dirhashes. Returns zero with list locked if successful.
1155 */
1156static int
1157ufsdirhash_recycle(int wanted)
1158{
1159	struct dirhash *dh;
1160	doff_t **hash;
1161	u_int8_t *blkfree;
1162	int i, mem, narrays;
1163
1164	DIRHASHLIST_LOCK();
1165	dh = TAILQ_FIRST(&ufsdirhash_list);
1166	while (wanted + ufs_dirhashmem > ufs_dirhashmaxmem) {
1167		/* Decrement the score; only recycle if it becomes zero. */
1168		if (dh == NULL || --dh->dh_score > 0) {
1169			DIRHASHLIST_UNLOCK();
1170			return (-1);
1171		}
1172		/*
1173		 * If we can't lock it it's in use and we don't want to
1174		 * recycle it anyway.
1175		 */
1176		if (!sx_try_xlock(&dh->dh_lock)) {
1177			dh = TAILQ_NEXT(dh, dh_list);
1178			continue;
1179		}
1180		KASSERT(dh->dh_hash != NULL, ("dirhash: NULL hash on list"));
1181
1182		/* Remove it from the list and detach its memory. */
1183		TAILQ_REMOVE(&ufsdirhash_list, dh, dh_list);
1184		dh->dh_onlist = 0;
1185		hash = dh->dh_hash;
1186		dh->dh_hash = NULL;
1187		blkfree = dh->dh_blkfree;
1188		dh->dh_blkfree = NULL;
1189		narrays = dh->dh_narrays;
1190		mem = dh->dh_memreq;
1191		dh->dh_memreq = 0;
1192
1193		/* Unlock everything, free the detached memory. */
1194		ufsdirhash_release(dh);
1195		DIRHASHLIST_UNLOCK();
1196		for (i = 0; i < narrays; i++)
1197			DIRHASH_BLKFREE(hash[i]);
1198		free(hash, M_DIRHASH);
1199		free(blkfree, M_DIRHASH);
1200
1201		/* Account for the returned memory, and repeat if necessary. */
1202		DIRHASHLIST_LOCK();
1203		ufs_dirhashmem -= mem;
1204		dh = TAILQ_FIRST(&ufsdirhash_list);
1205	}
1206	/* Success; return with list locked. */
1207	return (0);
1208}
1209
1210
1211void
1212ufsdirhash_init()
1213{
1214	ufsdirhash_zone = uma_zcreate("DIRHASH", DH_NBLKOFF * sizeof(doff_t),
1215	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1216	mtx_init(&ufsdirhash_mtx, "dirhash list", NULL, MTX_DEF);
1217	TAILQ_INIT(&ufsdirhash_list);
1218}
1219
1220void
1221ufsdirhash_uninit()
1222{
1223	KASSERT(TAILQ_EMPTY(&ufsdirhash_list), ("ufsdirhash_uninit"));
1224	uma_zdestroy(ufsdirhash_zone);
1225	mtx_destroy(&ufsdirhash_mtx);
1226}
1227
1228#endif /* UFS_DIRHASH */
1229