Add a dentry op to allow processes to be held during pathwalk transit
[deliverable/linux.git] / drivers / staging / autofs / dirhash.c
1 /* -*- linux-c -*- --------------------------------------------------------- *
2 *
3 * drivers/staging/autofs/dirhash.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 *
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.
10 *
11 * ------------------------------------------------------------------------- */
12
13 #include "autofs_i.h"
14
15 /* Functions for maintenance of expiry queue */
16
17 static void autofs_init_usage(struct autofs_dirhash *dh,
18 struct autofs_dir_ent *ent)
19 {
20 list_add_tail(&ent->exp, &dh->expiry_head);
21 ent->last_usage = jiffies;
22 }
23
24 static void autofs_delete_usage(struct autofs_dir_ent *ent)
25 {
26 list_del(&ent->exp);
27 }
28
29 void autofs_update_usage(struct autofs_dirhash *dh,
30 struct autofs_dir_ent *ent)
31 {
32 autofs_delete_usage(ent); /* Unlink from current position */
33 autofs_init_usage(dh, ent); /* Relink at queue tail */
34 }
35
36 struct autofs_dir_ent *autofs_expire(struct super_block *sb,
37 struct autofs_sb_info *sbi,
38 struct vfsmount *mnt)
39 {
40 struct autofs_dirhash *dh = &sbi->dirhash;
41 struct autofs_dir_ent *ent;
42 unsigned long timeout = sbi->exp_timeout;
43
44 while (1) {
45 struct path path;
46 int umount_ok;
47
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)
54 break;
55 /* Move to end of list in case expiry isn't desirable */
56 autofs_update_usage(dh, ent);
57
58 /* Check to see that entry is expirable */
59 if (ent->ino < AUTOFS_FIRST_DIR_INO)
60 return ent; /* Symlinks are always expirable */
61
62 /* Get the dentry for the autofs subdirectory */
63 path.dentry = ent->dentry;
64
65 if (!path.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);
70 continue;
71 }
72
73 if (!path.dentry->d_inode) {
74 dput(path.dentry);
75 printk(KERN_DEBUG "autofs: negative dentry on expiry queue: %s\n",
76 ent->name);
77 autofs_delete_usage(ent);
78 continue;
79 }
80
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));
87 continue;
88 }
89 path.mnt = mnt;
90 path_get(&path);
91 if (!follow_down_one(&path)) {
92 path_put(&path);
93 DPRINTK(("autofs: not expirable\
94 (not a mounted directory): %s\n", ent->name));
95 continue;
96 }
97 follow_down(&path, false); // TODO: need to check error
98 umount_ok = may_umount(path.mnt);
99 path_put(&path);
100
101 if (umount_ok) {
102 DPRINTK(("autofs: signaling expire on %s\n",
103 ent->name));
104 return ent; /* Expirable! */
105 }
106
107 DPRINTK(("autofs: didn't expire due to may_umount: %s\n",
108 ent->name));
109 }
110 return NULL; /* No expirable entries */
111 }
112
113 void autofs_initialize_hash(struct autofs_dirhash *dh)
114 {
115 memset(&dh->h, 0, AUTOFS_HASH_SIZE*sizeof(struct autofs_dir_ent *));
116 INIT_LIST_HEAD(&dh->expiry_head);
117 }
118
119 struct autofs_dir_ent *autofs_hash_lookup(const struct autofs_dirhash *dh,
120 struct qstr *name)
121 {
122 struct autofs_dir_ent *dhn;
123
124 DPRINTK(("autofs_hash_lookup: hash = 0x%08x, name = ", name->hash));
125 autofs_say(name->name, name->len);
126
127 for (dhn = dh->h[(unsigned) name->hash % AUTOFS_HASH_SIZE];
128 dhn;
129 dhn = dhn->next) {
130 if (name->hash == dhn->hash &&
131 name->len == dhn->len &&
132 !memcmp(name->name, dhn->name, name->len))
133 break;
134 }
135
136 return dhn;
137 }
138
139 void autofs_hash_insert(struct autofs_dirhash *dh, struct autofs_dir_ent *ent)
140 {
141 struct autofs_dir_ent **dhnp;
142
143 DPRINTK(("autofs_hash_insert: hash = 0x%08x, name = ", ent->hash));
144 autofs_say(ent->name, ent->len);
145
146 autofs_init_usage(dh, ent);
147 if (ent->dentry)
148 dget(ent->dentry);
149
150 dhnp = &dh->h[(unsigned) ent->hash % AUTOFS_HASH_SIZE];
151 ent->next = *dhnp;
152 ent->back = dhnp;
153 *dhnp = ent;
154 if (ent->next)
155 ent->next->back = &(ent->next);
156 }
157
158 void autofs_hash_delete(struct autofs_dir_ent *ent)
159 {
160 *(ent->back) = ent->next;
161 if (ent->next)
162 ent->next->back = ent->back;
163
164 autofs_delete_usage(ent);
165
166 if (ent->dentry)
167 dput(ent->dentry);
168 kfree(ent->name);
169 kfree(ent);
170 }
171
172 /*
173 * Used by readdir(). We must validate "ptr", so we can't simply make it
174 * a pointer. Values below 0xffff are reserved; calling with any value
175 * <= 0x10000 will return the first entry found.
176 *
177 * "last" can be NULL or the value returned by the last search *if* we
178 * want the next sequential entry.
179 */
180 struct autofs_dir_ent *autofs_hash_enum(const struct autofs_dirhash *dh,
181 off_t *ptr, struct autofs_dir_ent *last)
182 {
183 int bucket, ecount, i;
184 struct autofs_dir_ent *ent;
185
186 bucket = (*ptr >> 16) - 1;
187 ecount = *ptr & 0xffff;
188
189 if (bucket < 0)
190 bucket = ecount = 0;
191
192 DPRINTK(("autofs_hash_enum: bucket %d, entry %d\n", bucket, ecount));
193
194 ent = last ? last->next : NULL;
195
196 if (ent) {
197 ecount++;
198 } else {
199 while (bucket < AUTOFS_HASH_SIZE) {
200 ent = dh->h[bucket];
201 for (i = ecount ; ent && i ; i--)
202 ent = ent->next;
203
204 if (ent) {
205 ecount++; /* Point to *next* entry */
206 break;
207 }
208
209 bucket++; ecount = 0;
210 }
211 }
212
213 #ifdef DEBUG
214 if (!ent)
215 printk(KERN_DEBUG "autofs_hash_enum: nothing found\n");
216 else {
217 printk(KERN_DEBUG "autofs_hash_enum: found hash %08x, name",
218 ent->hash);
219 autofs_say(ent->name, ent->len);
220 }
221 #endif
222
223 *ptr = ((bucket+1) << 16) + ecount;
224 return ent;
225 }
226
227 /* Iterate over all the ents, and remove all dentry pointers. Used on
228 entering catatonic mode, in order to make the filesystem unmountable. */
229 void autofs_hash_dputall(struct autofs_dirhash *dh)
230 {
231 int i;
232 struct autofs_dir_ent *ent;
233
234 for (i = 0 ; i < AUTOFS_HASH_SIZE ; i++) {
235 for (ent = dh->h[i] ; ent ; ent = ent->next) {
236 if (ent->dentry) {
237 dput(ent->dentry);
238 ent->dentry = NULL;
239 }
240 }
241 }
242 }
243
244 /* Delete everything. This is used on filesystem destruction, so we
245 make no attempt to keep the pointers valid */
246 void autofs_hash_nuke(struct autofs_sb_info *sbi)
247 {
248 int i;
249 struct autofs_dir_ent *ent, *nent;
250
251 for (i = 0 ; i < AUTOFS_HASH_SIZE ; i++) {
252 for (ent = sbi->dirhash.h[i] ; ent ; ent = nent) {
253 nent = ent->next;
254 if (ent->dentry)
255 dput(ent->dentry);
256 kfree(ent->name);
257 kfree(ent);
258 }
259 }
260 }
This page took 0.034889 seconds and 5 git commands to generate.