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 * ------------------------------------------------------------------------- */
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 */
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
);
71 if ( !dentry
->d_inode
) {
73 printk("autofs: negative dentry on expiry queue: %s\n",
75 autofs_delete_usage(ent
);
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
));
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
=
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
) )
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
);
128 ent
->dentry
->d_count
++;
130 dhnp
= &dh
->h
[(unsigned) ent
->hash
% AUTOFS_HASH_SIZE
];
135 ent
->next
->back
= &(ent
->next
);
138 void autofs_hash_delete(struct autofs_dir_ent
*ent
)
140 *(ent
->back
) = ent
->next
;
142 ent
->next
->back
= ent
->back
;
144 autofs_delete_usage(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;
173 DPRINTK(("autofs_hash_enum: bucket %d, entry %d\n", bucket
, ecount
));
175 ent
= last
? last
->next
: NULL
;
180 while ( bucket
< AUTOFS_HASH_SIZE
) {
182 for ( i
= ecount
; ent
&& i
; i
-- )
186 ecount
++; /* Point to *next* entry */
190 bucket
++; ecount
= 0;
196 printk("autofs_hash_enum: nothing found\n");
198 printk("autofs_hash_enum: found hash %08x, name", ent
->hash
);
199 autofs_say(ent
->name
,ent
->len
);
203 *ptr
= ((bucket
+1) << 16) + ecount
;
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
)
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
) {
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
)
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
) {