diff options
Diffstat (limited to 'fs/autofs4/waitq.c')
-rw-r--r-- | fs/autofs4/waitq.c | 267 |
1 files changed, 180 insertions, 87 deletions
diff --git a/fs/autofs4/waitq.c b/fs/autofs4/waitq.c index 75e5955c3f6d..35216d18d8b5 100644 --- a/fs/autofs4/waitq.c +++ b/fs/autofs4/waitq.c | |||
@@ -28,6 +28,12 @@ void autofs4_catatonic_mode(struct autofs_sb_info *sbi) | |||
28 | { | 28 | { |
29 | struct autofs_wait_queue *wq, *nwq; | 29 | struct autofs_wait_queue *wq, *nwq; |
30 | 30 | ||
31 | mutex_lock(&sbi->wq_mutex); | ||
32 | if (sbi->catatonic) { | ||
33 | mutex_unlock(&sbi->wq_mutex); | ||
34 | return; | ||
35 | } | ||
36 | |||
31 | DPRINTK("entering catatonic mode"); | 37 | DPRINTK("entering catatonic mode"); |
32 | 38 | ||
33 | sbi->catatonic = 1; | 39 | sbi->catatonic = 1; |
@@ -36,13 +42,18 @@ void autofs4_catatonic_mode(struct autofs_sb_info *sbi) | |||
36 | while (wq) { | 42 | while (wq) { |
37 | nwq = wq->next; | 43 | nwq = wq->next; |
38 | wq->status = -ENOENT; /* Magic is gone - report failure */ | 44 | wq->status = -ENOENT; /* Magic is gone - report failure */ |
39 | kfree(wq->name); | 45 | if (wq->name.name) { |
40 | wq->name = NULL; | 46 | kfree(wq->name.name); |
47 | wq->name.name = NULL; | ||
48 | } | ||
49 | wq->wait_ctr--; | ||
41 | wake_up_interruptible(&wq->queue); | 50 | wake_up_interruptible(&wq->queue); |
42 | wq = nwq; | 51 | wq = nwq; |
43 | } | 52 | } |
44 | fput(sbi->pipe); /* Close the pipe */ | 53 | fput(sbi->pipe); /* Close the pipe */ |
45 | sbi->pipe = NULL; | 54 | sbi->pipe = NULL; |
55 | sbi->pipefd = -1; | ||
56 | mutex_unlock(&sbi->wq_mutex); | ||
46 | } | 57 | } |
47 | 58 | ||
48 | static int autofs4_write(struct file *file, const void *addr, int bytes) | 59 | static int autofs4_write(struct file *file, const void *addr, int bytes) |
@@ -89,10 +100,11 @@ static void autofs4_notify_daemon(struct autofs_sb_info *sbi, | |||
89 | union autofs_packet_union v4_pkt; | 100 | union autofs_packet_union v4_pkt; |
90 | union autofs_v5_packet_union v5_pkt; | 101 | union autofs_v5_packet_union v5_pkt; |
91 | } pkt; | 102 | } pkt; |
103 | struct file *pipe = NULL; | ||
92 | size_t pktsz; | 104 | size_t pktsz; |
93 | 105 | ||
94 | DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d", | 106 | DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d", |
95 | wq->wait_queue_token, wq->len, wq->name, type); | 107 | wq->wait_queue_token, wq->name.len, wq->name.name, type); |
96 | 108 | ||
97 | memset(&pkt,0,sizeof pkt); /* For security reasons */ | 109 | memset(&pkt,0,sizeof pkt); /* For security reasons */ |
98 | 110 | ||
@@ -107,9 +119,9 @@ static void autofs4_notify_daemon(struct autofs_sb_info *sbi, | |||
107 | pktsz = sizeof(*mp); | 119 | pktsz = sizeof(*mp); |
108 | 120 | ||
109 | mp->wait_queue_token = wq->wait_queue_token; | 121 | mp->wait_queue_token = wq->wait_queue_token; |
110 | mp->len = wq->len; | 122 | mp->len = wq->name.len; |
111 | memcpy(mp->name, wq->name, wq->len); | 123 | memcpy(mp->name, wq->name.name, wq->name.len); |
112 | mp->name[wq->len] = '\0'; | 124 | mp->name[wq->name.len] = '\0'; |
113 | break; | 125 | break; |
114 | } | 126 | } |
115 | case autofs_ptype_expire_multi: | 127 | case autofs_ptype_expire_multi: |
@@ -119,9 +131,9 @@ static void autofs4_notify_daemon(struct autofs_sb_info *sbi, | |||
119 | pktsz = sizeof(*ep); | 131 | pktsz = sizeof(*ep); |
120 | 132 | ||
121 | ep->wait_queue_token = wq->wait_queue_token; | 133 | ep->wait_queue_token = wq->wait_queue_token; |
122 | ep->len = wq->len; | 134 | ep->len = wq->name.len; |
123 | memcpy(ep->name, wq->name, wq->len); | 135 | memcpy(ep->name, wq->name.name, wq->name.len); |
124 | ep->name[wq->len] = '\0'; | 136 | ep->name[wq->name.len] = '\0'; |
125 | break; | 137 | break; |
126 | } | 138 | } |
127 | /* | 139 | /* |
@@ -138,9 +150,9 @@ static void autofs4_notify_daemon(struct autofs_sb_info *sbi, | |||
138 | pktsz = sizeof(*packet); | 150 | pktsz = sizeof(*packet); |
139 | 151 | ||
140 | packet->wait_queue_token = wq->wait_queue_token; | 152 | packet->wait_queue_token = wq->wait_queue_token; |
141 | packet->len = wq->len; | 153 | packet->len = wq->name.len; |
142 | memcpy(packet->name, wq->name, wq->len); | 154 | memcpy(packet->name, wq->name.name, wq->name.len); |
143 | packet->name[wq->len] = '\0'; | 155 | packet->name[wq->name.len] = '\0'; |
144 | packet->dev = wq->dev; | 156 | packet->dev = wq->dev; |
145 | packet->ino = wq->ino; | 157 | packet->ino = wq->ino; |
146 | packet->uid = wq->uid; | 158 | packet->uid = wq->uid; |
@@ -154,8 +166,19 @@ static void autofs4_notify_daemon(struct autofs_sb_info *sbi, | |||
154 | return; | 166 | return; |
155 | } | 167 | } |
156 | 168 | ||
157 | if (autofs4_write(sbi->pipe, &pkt, pktsz)) | 169 | /* Check if we have become catatonic */ |
158 | autofs4_catatonic_mode(sbi); | 170 | mutex_lock(&sbi->wq_mutex); |
171 | if (!sbi->catatonic) { | ||
172 | pipe = sbi->pipe; | ||
173 | get_file(pipe); | ||
174 | } | ||
175 | mutex_unlock(&sbi->wq_mutex); | ||
176 | |||
177 | if (pipe) { | ||
178 | if (autofs4_write(pipe, &pkt, pktsz)) | ||
179 | autofs4_catatonic_mode(sbi); | ||
180 | fput(pipe); | ||
181 | } | ||
159 | } | 182 | } |
160 | 183 | ||
161 | static int autofs4_getpath(struct autofs_sb_info *sbi, | 184 | static int autofs4_getpath(struct autofs_sb_info *sbi, |
@@ -191,58 +214,55 @@ static int autofs4_getpath(struct autofs_sb_info *sbi, | |||
191 | } | 214 | } |
192 | 215 | ||
193 | static struct autofs_wait_queue * | 216 | static struct autofs_wait_queue * |
194 | autofs4_find_wait(struct autofs_sb_info *sbi, | 217 | autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr) |
195 | char *name, unsigned int hash, unsigned int len) | ||
196 | { | 218 | { |
197 | struct autofs_wait_queue *wq; | 219 | struct autofs_wait_queue *wq; |
198 | 220 | ||
199 | for (wq = sbi->queues; wq; wq = wq->next) { | 221 | for (wq = sbi->queues; wq; wq = wq->next) { |
200 | if (wq->hash == hash && | 222 | if (wq->name.hash == qstr->hash && |
201 | wq->len == len && | 223 | wq->name.len == qstr->len && |
202 | wq->name && !memcmp(wq->name, name, len)) | 224 | wq->name.name && |
225 | !memcmp(wq->name.name, qstr->name, qstr->len)) | ||
203 | break; | 226 | break; |
204 | } | 227 | } |
205 | return wq; | 228 | return wq; |
206 | } | 229 | } |
207 | 230 | ||
208 | int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry, | 231 | /* |
209 | enum autofs_notify notify) | 232 | * Check if we have a valid request. |
233 | * Returns | ||
234 | * 1 if the request should continue. | ||
235 | * In this case we can return an autofs_wait_queue entry if one is | ||
236 | * found or NULL to idicate a new wait needs to be created. | ||
237 | * 0 or a negative errno if the request shouldn't continue. | ||
238 | */ | ||
239 | static int validate_request(struct autofs_wait_queue **wait, | ||
240 | struct autofs_sb_info *sbi, | ||
241 | struct qstr *qstr, | ||
242 | struct dentry*dentry, enum autofs_notify notify) | ||
210 | { | 243 | { |
211 | struct autofs_info *ino; | ||
212 | struct autofs_wait_queue *wq; | 244 | struct autofs_wait_queue *wq; |
213 | char *name; | 245 | struct autofs_info *ino; |
214 | unsigned int len = 0; | ||
215 | unsigned int hash = 0; | ||
216 | int status, type; | ||
217 | |||
218 | /* In catatonic mode, we don't wait for nobody */ | ||
219 | if (sbi->catatonic) | ||
220 | return -ENOENT; | ||
221 | |||
222 | name = kmalloc(NAME_MAX + 1, GFP_KERNEL); | ||
223 | if (!name) | ||
224 | return -ENOMEM; | ||
225 | 246 | ||
226 | /* If this is a direct mount request create a dummy name */ | 247 | /* Wait in progress, continue; */ |
227 | if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT)) | 248 | wq = autofs4_find_wait(sbi, qstr); |
228 | len = sprintf(name, "%p", dentry); | 249 | if (wq) { |
229 | else { | 250 | *wait = wq; |
230 | len = autofs4_getpath(sbi, dentry, &name); | 251 | return 1; |
231 | if (!len) { | ||
232 | kfree(name); | ||
233 | return -ENOENT; | ||
234 | } | ||
235 | } | 252 | } |
236 | hash = full_name_hash(name, len); | ||
237 | 253 | ||
238 | if (mutex_lock_interruptible(&sbi->wq_mutex)) { | 254 | *wait = NULL; |
239 | kfree(name); | ||
240 | return -EINTR; | ||
241 | } | ||
242 | 255 | ||
243 | wq = autofs4_find_wait(sbi, name, hash, len); | 256 | /* If we don't yet have any info this is a new request */ |
244 | ino = autofs4_dentry_ino(dentry); | 257 | ino = autofs4_dentry_ino(dentry); |
245 | if (!wq && ino && notify == NFY_NONE) { | 258 | if (!ino) |
259 | return 1; | ||
260 | |||
261 | /* | ||
262 | * If we've been asked to wait on an existing expire (NFY_NONE) | ||
263 | * but there is no wait in the queue ... | ||
264 | */ | ||
265 | if (notify == NFY_NONE) { | ||
246 | /* | 266 | /* |
247 | * Either we've betean the pending expire to post it's | 267 | * Either we've betean the pending expire to post it's |
248 | * wait or it finished while we waited on the mutex. | 268 | * wait or it finished while we waited on the mutex. |
@@ -253,13 +273,14 @@ int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry, | |||
253 | while (ino->flags & AUTOFS_INF_EXPIRING) { | 273 | while (ino->flags & AUTOFS_INF_EXPIRING) { |
254 | mutex_unlock(&sbi->wq_mutex); | 274 | mutex_unlock(&sbi->wq_mutex); |
255 | schedule_timeout_interruptible(HZ/10); | 275 | schedule_timeout_interruptible(HZ/10); |
256 | if (mutex_lock_interruptible(&sbi->wq_mutex)) { | 276 | if (mutex_lock_interruptible(&sbi->wq_mutex)) |
257 | kfree(name); | ||
258 | return -EINTR; | 277 | return -EINTR; |
278 | |||
279 | wq = autofs4_find_wait(sbi, qstr); | ||
280 | if (wq) { | ||
281 | *wait = wq; | ||
282 | return 1; | ||
259 | } | 283 | } |
260 | wq = autofs4_find_wait(sbi, name, hash, len); | ||
261 | if (wq) | ||
262 | break; | ||
263 | } | 284 | } |
264 | 285 | ||
265 | /* | 286 | /* |
@@ -267,18 +288,96 @@ int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry, | |||
267 | * cases where we wait on NFY_NONE neither depend on the | 288 | * cases where we wait on NFY_NONE neither depend on the |
268 | * return status of the wait. | 289 | * return status of the wait. |
269 | */ | 290 | */ |
270 | if (!wq) { | 291 | return 0; |
292 | } | ||
293 | |||
294 | /* | ||
295 | * If we've been asked to trigger a mount and the request | ||
296 | * completed while we waited on the mutex ... | ||
297 | */ | ||
298 | if (notify == NFY_MOUNT) { | ||
299 | /* | ||
300 | * If the dentry isn't hashed just go ahead and try the | ||
301 | * mount again with a new wait (not much else we can do). | ||
302 | */ | ||
303 | if (!d_unhashed(dentry)) { | ||
304 | /* | ||
305 | * But if the dentry is hashed, that means that we | ||
306 | * got here through the revalidate path. Thus, we | ||
307 | * need to check if the dentry has been mounted | ||
308 | * while we waited on the wq_mutex. If it has, | ||
309 | * simply return success. | ||
310 | */ | ||
311 | if (d_mountpoint(dentry)) | ||
312 | return 0; | ||
313 | } | ||
314 | } | ||
315 | |||
316 | return 1; | ||
317 | } | ||
318 | |||
319 | int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry, | ||
320 | enum autofs_notify notify) | ||
321 | { | ||
322 | struct autofs_wait_queue *wq; | ||
323 | struct qstr qstr; | ||
324 | char *name; | ||
325 | int status, ret, type; | ||
326 | |||
327 | /* In catatonic mode, we don't wait for nobody */ | ||
328 | if (sbi->catatonic) | ||
329 | return -ENOENT; | ||
330 | |||
331 | if (!dentry->d_inode) { | ||
332 | /* | ||
333 | * A wait for a negative dentry is invalid for certain | ||
334 | * cases. A direct or offset mount "always" has its mount | ||
335 | * point directory created and so the request dentry must | ||
336 | * be positive or the map key doesn't exist. The situation | ||
337 | * is very similar for indirect mounts except only dentrys | ||
338 | * in the root of the autofs file system may be negative. | ||
339 | */ | ||
340 | if (sbi->type & (AUTOFS_TYPE_DIRECT|AUTOFS_TYPE_OFFSET)) | ||
341 | return -ENOENT; | ||
342 | else if (!IS_ROOT(dentry->d_parent)) | ||
343 | return -ENOENT; | ||
344 | } | ||
345 | |||
346 | name = kmalloc(NAME_MAX + 1, GFP_KERNEL); | ||
347 | if (!name) | ||
348 | return -ENOMEM; | ||
349 | |||
350 | /* If this is a direct mount request create a dummy name */ | ||
351 | if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT)) | ||
352 | qstr.len = sprintf(name, "%p", dentry); | ||
353 | else { | ||
354 | qstr.len = autofs4_getpath(sbi, dentry, &name); | ||
355 | if (!qstr.len) { | ||
271 | kfree(name); | 356 | kfree(name); |
272 | mutex_unlock(&sbi->wq_mutex); | 357 | return -ENOENT; |
273 | return 0; | ||
274 | } | 358 | } |
275 | } | 359 | } |
360 | qstr.name = name; | ||
361 | qstr.hash = full_name_hash(name, qstr.len); | ||
362 | |||
363 | if (mutex_lock_interruptible(&sbi->wq_mutex)) { | ||
364 | kfree(qstr.name); | ||
365 | return -EINTR; | ||
366 | } | ||
367 | |||
368 | ret = validate_request(&wq, sbi, &qstr, dentry, notify); | ||
369 | if (ret <= 0) { | ||
370 | if (ret == 0) | ||
371 | mutex_unlock(&sbi->wq_mutex); | ||
372 | kfree(qstr.name); | ||
373 | return ret; | ||
374 | } | ||
276 | 375 | ||
277 | if (!wq) { | 376 | if (!wq) { |
278 | /* Create a new wait queue */ | 377 | /* Create a new wait queue */ |
279 | wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL); | 378 | wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL); |
280 | if (!wq) { | 379 | if (!wq) { |
281 | kfree(name); | 380 | kfree(qstr.name); |
282 | mutex_unlock(&sbi->wq_mutex); | 381 | mutex_unlock(&sbi->wq_mutex); |
283 | return -ENOMEM; | 382 | return -ENOMEM; |
284 | } | 383 | } |
@@ -289,9 +388,7 @@ int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry, | |||
289 | wq->next = sbi->queues; | 388 | wq->next = sbi->queues; |
290 | sbi->queues = wq; | 389 | sbi->queues = wq; |
291 | init_waitqueue_head(&wq->queue); | 390 | init_waitqueue_head(&wq->queue); |
292 | wq->hash = hash; | 391 | memcpy(&wq->name, &qstr, sizeof(struct qstr)); |
293 | wq->name = name; | ||
294 | wq->len = len; | ||
295 | wq->dev = autofs4_get_dev(sbi); | 392 | wq->dev = autofs4_get_dev(sbi); |
296 | wq->ino = autofs4_get_ino(sbi); | 393 | wq->ino = autofs4_get_ino(sbi); |
297 | wq->uid = current->uid; | 394 | wq->uid = current->uid; |
@@ -299,7 +396,7 @@ int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry, | |||
299 | wq->pid = current->pid; | 396 | wq->pid = current->pid; |
300 | wq->tgid = current->tgid; | 397 | wq->tgid = current->tgid; |
301 | wq->status = -EINTR; /* Status return if interrupted */ | 398 | wq->status = -EINTR; /* Status return if interrupted */ |
302 | atomic_set(&wq->wait_ctr, 2); | 399 | wq->wait_ctr = 2; |
303 | mutex_unlock(&sbi->wq_mutex); | 400 | mutex_unlock(&sbi->wq_mutex); |
304 | 401 | ||
305 | if (sbi->version < 5) { | 402 | if (sbi->version < 5) { |
@@ -319,28 +416,25 @@ int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry, | |||
319 | } | 416 | } |
320 | 417 | ||
321 | DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n", | 418 | DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n", |
322 | (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify); | 419 | (unsigned long) wq->wait_queue_token, wq->name.len, |
420 | wq->name.name, notify); | ||
323 | 421 | ||
324 | /* autofs4_notify_daemon() may block */ | 422 | /* autofs4_notify_daemon() may block */ |
325 | autofs4_notify_daemon(sbi, wq, type); | 423 | autofs4_notify_daemon(sbi, wq, type); |
326 | } else { | 424 | } else { |
327 | atomic_inc(&wq->wait_ctr); | 425 | wq->wait_ctr++; |
328 | mutex_unlock(&sbi->wq_mutex); | 426 | mutex_unlock(&sbi->wq_mutex); |
329 | kfree(name); | 427 | kfree(qstr.name); |
330 | DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d", | 428 | DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d", |
331 | (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify); | 429 | (unsigned long) wq->wait_queue_token, wq->name.len, |
332 | } | 430 | wq->name.name, notify); |
333 | |||
334 | /* wq->name is NULL if and only if the lock is already released */ | ||
335 | |||
336 | if (sbi->catatonic) { | ||
337 | /* We might have slept, so check again for catatonic mode */ | ||
338 | wq->status = -ENOENT; | ||
339 | kfree(wq->name); | ||
340 | wq->name = NULL; | ||
341 | } | 431 | } |
342 | 432 | ||
343 | if (wq->name) { | 433 | /* |
434 | * wq->name.name is NULL iff the lock is already released | ||
435 | * or the mount has been made catatonic. | ||
436 | */ | ||
437 | if (wq->name.name) { | ||
344 | /* Block all but "shutdown" signals while waiting */ | 438 | /* Block all but "shutdown" signals while waiting */ |
345 | sigset_t oldset; | 439 | sigset_t oldset; |
346 | unsigned long irqflags; | 440 | unsigned long irqflags; |
@@ -351,7 +445,7 @@ int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry, | |||
351 | recalc_sigpending(); | 445 | recalc_sigpending(); |
352 | spin_unlock_irqrestore(¤t->sighand->siglock, irqflags); | 446 | spin_unlock_irqrestore(¤t->sighand->siglock, irqflags); |
353 | 447 | ||
354 | wait_event_interruptible(wq->queue, wq->name == NULL); | 448 | wait_event_interruptible(wq->queue, wq->name.name == NULL); |
355 | 449 | ||
356 | spin_lock_irqsave(¤t->sighand->siglock, irqflags); | 450 | spin_lock_irqsave(¤t->sighand->siglock, irqflags); |
357 | current->blocked = oldset; | 451 | current->blocked = oldset; |
@@ -364,8 +458,10 @@ int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry, | |||
364 | status = wq->status; | 458 | status = wq->status; |
365 | 459 | ||
366 | /* Are we the last process to need status? */ | 460 | /* Are we the last process to need status? */ |
367 | if (atomic_dec_and_test(&wq->wait_ctr)) | 461 | mutex_lock(&sbi->wq_mutex); |
462 | if (!--wq->wait_ctr) | ||
368 | kfree(wq); | 463 | kfree(wq); |
464 | mutex_unlock(&sbi->wq_mutex); | ||
369 | 465 | ||
370 | return status; | 466 | return status; |
371 | } | 467 | } |
@@ -387,16 +483,13 @@ int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_tok | |||
387 | } | 483 | } |
388 | 484 | ||
389 | *wql = wq->next; /* Unlink from chain */ | 485 | *wql = wq->next; /* Unlink from chain */ |
390 | mutex_unlock(&sbi->wq_mutex); | 486 | kfree(wq->name.name); |
391 | kfree(wq->name); | 487 | wq->name.name = NULL; /* Do not wait on this queue */ |
392 | wq->name = NULL; /* Do not wait on this queue */ | ||
393 | |||
394 | wq->status = status; | 488 | wq->status = status; |
395 | 489 | wake_up_interruptible(&wq->queue); | |
396 | if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */ | 490 | if (!--wq->wait_ctr) |
397 | kfree(wq); | 491 | kfree(wq); |
398 | else | 492 | mutex_unlock(&sbi->wq_mutex); |
399 | wake_up_interruptible(&wq->queue); | ||
400 | 493 | ||
401 | return 0; | 494 | return 0; |
402 | } | 495 | } |