aboutsummaryrefslogtreecommitdiffstats
path: root/kernel4/inodehash.c
blob: 5bcffacc2d826c712773046076f719f10a04a0a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* -*- linux-c -*- --------------------------------------------------------- *
 *
 * linux/fs/autofs/inodehash.c
 *
 *  Copyright 1997 Transmeta Corporation -- All Rights Reserved
 *
 * This file is part of the Linux kernel and is made available under
 * the terms of the GNU General Public License, version 2, or at your
 * option, any later version, incorporated herein by reference.
 *
 * ------------------------------------------------------------------------- */

#include "autofs_i.h"

void vfree(const void * addr);
void * vmalloc(unsigned long size);


struct inode_hash {
	ino_t ino;
	struct dentry *dent;
};

struct inode_hash_tbl {
	int entries;
	int maxent;		/* Highest entry number = max entries - 1 */
	int shiftcnt;
	struct inode_hash *hash;
	void (*free)(const void *);
};

#define AUTOFS_MIN_INO_HASH_SIZE  6  /*  2^6 entries minimum */
#define AUTOFS_MAX_INO_HASH_SIZE 20  /* 2^20 entries maximum */ 

static int autofs_rehash(struct inode_hash_tbl *tbl, int nshift)
{
	void (*nfree)(const void *);
	struct inode_hash *nhash;
	int nmax = 1 << nshift;
	int i, ep, step;
	ino_t ino;
	
	if ( tbl->entries > nmax )
		return -1;	/* Can't fit all entries in that size table */

	nhash = kmalloc(GFP_KERNEL, nmax*sizeof(struct inode_hash));
	if ( nhash ) {
		nfree = kfree;
	} else {
		if ( nmax > PAGE_SIZE/sizeof(struct inode_hash) ) {
			nhash = vmalloc(nmax*sizeof(struct inode_hash));
			if ( !nhash )
				return -1;
			nfree = vfree;
		} else
			return -1;
	}

	memset(nhash, 0, nmax*sizeof(struct inode_hash));
	nmax--;			/* Make maximum value (= mask), not count */

	for ( i = 0 ; i <= tbl->maxent ; i++ ) {
		if ( (ino = tbl->hash[i].ino) ) {
			ep = ino & nmax;
			step = (ino ^ (ino >> (nshift-1))) | 1;
			while ( nhash[ep].ino )
				ep = (ep+step) & nmax;
			nhash[ep].ino = ino;
			nhash[ep].dent = tbl->hash[i].dent;
		}
	}

	tbl->free(tbl->hash);

	tbl->maxent = nmax;
	tbl->shiftcnt = nshift;
	tbl->hash = nhash;
	tbl->free = nfree;

	return 0;
}

static inline int autofs_grow_hash(struct inode_hash_tbl *tbl)
{
	return autofs_rehash(tbl,tbl->shiftcnt+1);
}

static inline int autofs_shrink_hash(struct inode_hash_tbl *tbl)
{
	return autofs_rehash(tbl,tbl->shiftcnt-1);
}

int autofs_ino_hash_insert(struct inode_hash_tbl *tbl,
			   struct dentry *dent, ino_t ino)
{
	int ep, step;

	if ( tbl->entries > tbl->maxent )
		if ( tbl->shiftcnt >= AUTOFS_MAX_INO_HASH_SIZE ||
		     autofs_grow_hash(tbl) )
			return -1;
	
	ep = ino & tbl->maxent;
	step = (ino ^ (ino >> (tbl->shiftcnt-1))) | 1; /* Always odd */

	/* step is always odd and the size of the table is 2^n, so we will
	   always seek the whole list */
	while ( tbl->hash[ep].ino )
		ep = (ep+step) & tbl->maxent;

	tbl->hash[ep].ino  = ino;
	tbl->hash[ep].dent = dent;
	tbl->entries++;

	return 0;
}

int autofs_ino_hash_remove(struct inode_hash_tbl *tbl, ino_t ino)
{
	int ep, ep1, ep0, step;

	ep = ep0 = ino & tbl->maxent;
	step = (ino ^ (ino >> (tbl->shiftcnt-1))) | 1;

	while ( tbl->hash[ep].ino != ino ) {
		ep = (ep+step) & tbl->maxent;
		if ( ep == ep0 )
			return -1;
	}
	
	ep1 = (ep+step) & tbl->maxent;

	/* Compact the hash chain */
	while ( tbl->hash[ep1].ino && ep1 != ep0 ) {
		if ( (tbl->hash[ep1].ino & tbl->maxent) == ep0 ) {
			tbl->hash[ep] = tbl->hash[ep1];
			ep = ep1;
		}
		ep1 = (ep1+step) & tbl->maxent;
	}

	tbl->hash[ep].ino  = 0;
	tbl->hash[ep].dent = NULL;
	tbl->entries--;

	if ( tbl->shiftcnt > AUTOFS_MIN_INO_HASH_SIZE &&
	     tbl->entries <= (tbl->maxent >> 2) )
		autofs_shrink_hash(tbl);

	return 0;
}

struct dentry *autofs_ino_hash_lookup(struct inode_hash_tbl *tbl, ino_t ino)
{
	int ep, ep0, step;

	ep = ep0 = ino & tbl->maxent;
	step = (ino ^ (ino >> (tbl->shiftcnt-1))) | 1;

	while ( tbl->hash[ep].ino && tbl->hash[ep].ino != ino ) {
		ep = (ep+step) & tbl->maxent;
		if ( ep == ep0 )
			return NULL; /* Not found */
	}
	return tbl->hash[ep].dent;
}