1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * drivers/staging/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 * ------------------------------------------------------------------------- */
15 /* Functions for maintenance of expiry queue */
17 static void autofs_init_usage(struct autofs_dirhash *dh,
18 struct autofs_dir_ent *ent)
20 list_add_tail(&ent->exp, &dh->expiry_head);
21 ent->last_usage = jiffies;
24 static void autofs_delete_usage(struct autofs_dir_ent *ent)
29 void autofs_update_usage(struct autofs_dirhash *dh,
30 struct autofs_dir_ent *ent)
32 autofs_delete_usage(ent); /* Unlink from current position */
33 autofs_init_usage(dh, ent); /* Relink at queue tail */
36 struct autofs_dir_ent *autofs_expire(struct super_block *sb,
37 struct autofs_sb_info *sbi,
40 struct autofs_dirhash *dh = &sbi->dirhash;
41 struct autofs_dir_ent *ent;
42 unsigned long timeout = sbi->exp_timeout;
48 if (list_empty(&dh->expiry_head) || sbi->catatonic)
49 return NULL; /* No entries */
50 /* We keep the list sorted by last_usage and want old stuff */
51 ent = list_entry(dh->expiry_head.next,
52 struct autofs_dir_ent, exp);
53 if (jiffies - ent->last_usage < timeout)
55 /* Move to end of list in case expiry isn't desirable */
56 autofs_update_usage(dh, ent);
58 /* Check to see that entry is expirable */
59 if (ent->ino < AUTOFS_FIRST_DIR_INO)
60 return ent; /* Symlinks are always expirable */
62 /* Get the dentry for the autofs subdirectory */
63 path.dentry = ent->dentry;
66 /* Should only happen in catatonic mode */
67 printk(KERN_DEBUG "autofs: dentry == NULL but inode \
68 range is directory, entry %s\n", ent->name);
69 autofs_delete_usage(ent);
73 if (!path.dentry->d_inode) {
75 printk(KERN_DEBUG "autofs: negative dentry on expiry queue: %s\n",
77 autofs_delete_usage(ent);
81 /* Make sure entry is mounted and unused; note that dentry will
82 point to the mounted-on-top root. */
83 if (!S_ISDIR(path.dentry->d_inode->i_mode) ||
84 !d_mountpoint(path.dentry)) {
85 DPRINTK(("autofs: not expirable \
86 (not a mounted directory): %s\n", ent->name));
91 if (!follow_down(&path)) {
93 DPRINTK(("autofs: not expirable\
94 (not a mounted directory): %s\n", ent->name));
97 while (d_mountpoint(path.dentry) && follow_down(&path))
99 umount_ok = may_umount(path.mnt);
103 DPRINTK(("autofs: signaling expire on %s\n",
105 return ent; /* Expirable! */
108 DPRINTK(("autofs: didn't expire due to may_umount: %s\n",
111 return NULL; /* No expirable entries */
114 void autofs_initialize_hash(struct autofs_dirhash *dh)
116 memset(&dh->h, 0, AUTOFS_HASH_SIZE*sizeof(struct autofs_dir_ent *));
117 INIT_LIST_HEAD(&dh->expiry_head);
120 struct autofs_dir_ent *autofs_hash_lookup(const struct autofs_dirhash *dh,
123 struct autofs_dir_ent *dhn;
125 DPRINTK(("autofs_hash_lookup: hash = 0x%08x, name = ", name->hash));
126 autofs_say(name->name, name->len);
128 for (dhn = dh->h[(unsigned) name->hash % AUTOFS_HASH_SIZE];
131 if (name->hash == dhn->hash &&
132 name->len == dhn->len &&
133 !memcmp(name->name, dhn->name, name->len))
140 void autofs_hash_insert(struct autofs_dirhash *dh, struct autofs_dir_ent *ent)
142 struct autofs_dir_ent **dhnp;
144 DPRINTK(("autofs_hash_insert: hash = 0x%08x, name = ", ent->hash));
145 autofs_say(ent->name, ent->len);
147 autofs_init_usage(dh, ent);
151 dhnp = &dh->h[(unsigned) ent->hash % AUTOFS_HASH_SIZE];
156 ent->next->back = &(ent->next);
159 void autofs_hash_delete(struct autofs_dir_ent *ent)
161 *(ent->back) = ent->next;
163 ent->next->back = ent->back;
165 autofs_delete_usage(ent);
174 * Used by readdir(). We must validate "ptr", so we can't simply make it
175 * a pointer. Values below 0xffff are reserved; calling with any value
176 * <= 0x10000 will return the first entry found.
178 * "last" can be NULL or the value returned by the last search *if* we
179 * want the next sequential entry.
181 struct autofs_dir_ent *autofs_hash_enum(const struct autofs_dirhash *dh,
182 off_t *ptr, struct autofs_dir_ent *last)
184 int bucket, ecount, i;
185 struct autofs_dir_ent *ent;
187 bucket = (*ptr >> 16) - 1;
188 ecount = *ptr & 0xffff;
193 DPRINTK(("autofs_hash_enum: bucket %d, entry %d\n", bucket, ecount));
195 ent = last ? last->next : NULL;
200 while (bucket < AUTOFS_HASH_SIZE) {
202 for (i = ecount ; ent && i ; i--)
206 ecount++; /* Point to *next* entry */
210 bucket++; ecount = 0;
216 printk(KERN_DEBUG "autofs_hash_enum: nothing found\n");
218 printk(KERN_DEBUG "autofs_hash_enum: found hash %08x, name",
220 autofs_say(ent->name, ent->len);
224 *ptr = ((bucket+1) << 16) + ecount;
228 /* Iterate over all the ents, and remove all dentry pointers. Used on
229 entering catatonic mode, in order to make the filesystem unmountable. */
230 void autofs_hash_dputall(struct autofs_dirhash *dh)
233 struct autofs_dir_ent *ent;
235 for (i = 0 ; i < AUTOFS_HASH_SIZE ; i++) {
236 for (ent = dh->h[i] ; ent ; ent = ent->next) {
245 /* Delete everything. This is used on filesystem destruction, so we
246 make no attempt to keep the pointers valid */
247 void autofs_hash_nuke(struct autofs_sb_info *sbi)
250 struct autofs_dir_ent *ent, *nent;
252 for (i = 0 ; i < AUTOFS_HASH_SIZE ; i++) {
253 for (ent = sbi->dirhash.h[i] ; ent ; ent = nent) {