* better
[mascara-docs.git] / i386 / linux-2.3.21 / fs / autofs / dirhash.c
blobd861dc59d6e22f67c32cf1a2fb3f8ff564750c96
1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * linux/fs/autofs/dirhash.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
7 * This file is part of the Linux kernel and is made available under
8 * the terms of the GNU General Public License, version 2, or at your
9 * option, any later version, incorporated herein by reference.
11 * ------------------------------------------------------------------------- */
13 #include "autofs_i.h"
15 /* Functions for maintenance of expiry queue */
17 static void autofs_init_usage(struct autofs_dirhash *dh,
18 struct autofs_dir_ent *ent)
20 ent->exp_next = &dh->expiry_head;
21 ent->exp_prev = dh->expiry_head.exp_prev;
22 dh->expiry_head.exp_prev->exp_next = ent;
23 dh->expiry_head.exp_prev = ent;
24 ent->last_usage = jiffies;
27 static void autofs_delete_usage(struct autofs_dir_ent *ent)
29 ent->exp_prev->exp_next = ent->exp_next;
30 ent->exp_next->exp_prev = ent->exp_prev;
33 void autofs_update_usage(struct autofs_dirhash *dh,
34 struct autofs_dir_ent *ent)
36 autofs_delete_usage(ent); /* Unlink from current position */
37 autofs_init_usage(dh,ent); /* Relink at queue tail */
40 struct autofs_dir_ent *autofs_expire(struct super_block *sb,
41 struct autofs_sb_info *sbi)
43 struct autofs_dirhash *dh = &sbi->dirhash;
44 struct autofs_dir_ent *ent;
45 struct dentry *dentry;
46 unsigned long timeout = sbi->exp_timeout;
48 ent = dh->expiry_head.exp_next;
50 if ( ent == &(dh->expiry_head) || sbi->catatonic )
51 return NULL; /* No entries */
53 while ( jiffies - ent->last_usage >= timeout ) {
54 /* Move to end of list in case expiry isn't desirable */
55 autofs_update_usage(dh, ent);
57 /* Check to see that entry is expirable */
58 if ( ent->ino < AUTOFS_FIRST_DIR_INO )
59 return ent; /* Symlinks are always expirable */
61 /* Get the dentry for the autofs subdirectory */
62 dentry = ent->dentry;
64 if ( !dentry ) {
65 /* Should only happen in catatonic mode */
66 printk("autofs: dentry == NULL but inode range is directory, entry %s\n", ent->name);
67 autofs_delete_usage(ent);
68 continue;
71 if ( !dentry->d_inode ) {
72 dput(dentry);
73 printk("autofs: negative dentry on expiry queue: %s\n",
74 ent->name);
75 autofs_delete_usage(ent);
76 continue;
79 /* Make sure entry is mounted and unused; note that dentry will
80 point to the mounted-on-top root. */
81 if ( !S_ISDIR(dentry->d_inode->i_mode)
82 || dentry->d_mounts == dentry ) {
83 DPRINTK(("autofs: not expirable (not a mounted directory): %s\n", ent->name));
84 continue;
87 if ( !is_root_busy(dentry->d_mounts) ) {
88 DPRINTK(("autofs: signaling expire on %s\n", ent->name));
89 return ent; /* Expirable! */
91 DPRINTK(("autofs: didn't expire due to is_root_busy: %s\n", ent->name));
93 return NULL; /* No expirable entries */
96 void autofs_initialize_hash(struct autofs_dirhash *dh) {
97 memset(&dh->h, 0, AUTOFS_HASH_SIZE*sizeof(struct autofs_dir_ent *));
98 dh->expiry_head.exp_next = dh->expiry_head.exp_prev =
99 &dh->expiry_head;
102 struct autofs_dir_ent *autofs_hash_lookup(const struct autofs_dirhash *dh, struct qstr *name)
104 struct autofs_dir_ent *dhn;
106 DPRINTK(("autofs_hash_lookup: hash = 0x%08x, name = ", name->hash));
107 autofs_say(name->name,name->len);
109 for ( dhn = dh->h[(unsigned) name->hash % AUTOFS_HASH_SIZE] ; dhn ; dhn = dhn->next ) {
110 if ( name->hash == dhn->hash &&
111 name->len == dhn->len &&
112 !memcmp(name->name, dhn->name, name->len) )
113 break;
116 return dhn;
119 void autofs_hash_insert(struct autofs_dirhash *dh, struct autofs_dir_ent *ent)
121 struct autofs_dir_ent **dhnp;
123 DPRINTK(("autofs_hash_insert: hash = 0x%08x, name = ", ent->hash));
124 autofs_say(ent->name,ent->len);
126 autofs_init_usage(dh,ent);
127 if ( ent->dentry )
128 ent->dentry->d_count++;
130 dhnp = &dh->h[(unsigned) ent->hash % AUTOFS_HASH_SIZE];
131 ent->next = *dhnp;
132 ent->back = dhnp;
133 *dhnp = ent;
134 if ( ent->next )
135 ent->next->back = &(ent->next);
138 void autofs_hash_delete(struct autofs_dir_ent *ent)
140 *(ent->back) = ent->next;
141 if ( ent->next )
142 ent->next->back = ent->back;
144 autofs_delete_usage(ent);
146 if ( ent->dentry )
147 dput(ent->dentry);
148 kfree(ent->name);
149 kfree(ent);
153 * Used by readdir(). We must validate "ptr", so we can't simply make it
154 * a pointer. Values below 0xffff are reserved; calling with any value
155 * <= 0x10000 will return the first entry found.
157 * "last" can be NULL or the value returned by the last search *if* we
158 * want the next sequential entry.
160 struct autofs_dir_ent *autofs_hash_enum(const struct autofs_dirhash *dh,
161 off_t *ptr, struct autofs_dir_ent *last)
163 int bucket, ecount, i;
164 struct autofs_dir_ent *ent;
166 bucket = (*ptr >> 16) - 1;
167 ecount = *ptr & 0xffff;
169 if ( bucket < 0 ) {
170 bucket = ecount = 0;
173 DPRINTK(("autofs_hash_enum: bucket %d, entry %d\n", bucket, ecount));
175 ent = last ? last->next : NULL;
177 if ( ent ) {
178 ecount++;
179 } else {
180 while ( bucket < AUTOFS_HASH_SIZE ) {
181 ent = dh->h[bucket];
182 for ( i = ecount ; ent && i ; i-- )
183 ent = ent->next;
185 if (ent) {
186 ecount++; /* Point to *next* entry */
187 break;
190 bucket++; ecount = 0;
194 #ifdef DEBUG
195 if ( !ent )
196 printk("autofs_hash_enum: nothing found\n");
197 else {
198 printk("autofs_hash_enum: found hash %08x, name", ent->hash);
199 autofs_say(ent->name,ent->len);
201 #endif
203 *ptr = ((bucket+1) << 16) + ecount;
204 return ent;
207 /* Iterate over all the ents, and remove all dentry pointers. Used on
208 entering catatonic mode, in order to make the filesystem unmountable. */
209 void autofs_hash_dputall(struct autofs_dirhash *dh)
211 int i;
212 struct autofs_dir_ent *ent;
214 for ( i = 0 ; i < AUTOFS_HASH_SIZE ; i++ ) {
215 for ( ent = dh->h[i] ; ent ; ent = ent->next ) {
216 if ( ent->dentry ) {
217 dput(ent->dentry);
218 ent->dentry = NULL;
224 /* Delete everything. This is used on filesystem destruction, so we
225 make no attempt to keep the pointers valid */
226 void autofs_hash_nuke(struct autofs_dirhash *dh)
228 int i;
229 struct autofs_dir_ent *ent, *nent;
231 for ( i = 0 ; i < AUTOFS_HASH_SIZE ; i++ ) {
232 for ( ent = dh->h[i] ; ent ; ent = nent ) {
233 nent = ent->next;
234 if ( ent->dentry )
235 dput(ent->dentry);
236 kfree(ent->name);
237 kfree(ent);