1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/waitq.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
12 * ------------------------------------------------------------------------- */
14 #include <linux/slab.h>
15 #include <linux/time.h>
16 #include <linux/signal.h>
17 #include <linux/file.h>
20 /* We make this a static variable rather than a part of the superblock; it
21 is better if we don't reassign numbers easily even across filesystems */
22 static autofs_wqt_t autofs4_next_wait_queue
= 1;
24 /* These are the signals we allow interrupting a pending mount */
25 #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT))
27 void autofs4_catatonic_mode(struct autofs_sb_info
*sbi
)
29 struct autofs_wait_queue
*wq
, *nwq
;
31 mutex_lock(&sbi
->wq_mutex
);
33 mutex_unlock(&sbi
->wq_mutex
);
37 DPRINTK("entering catatonic mode");
41 sbi
->queues
= NULL
; /* Erase all wait queues */
44 wq
->status
= -ENOENT
; /* Magic is gone - report failure */
49 wake_up_interruptible(&wq
->queue
);
52 fput(sbi
->pipe
); /* Close the pipe */
55 mutex_unlock(&sbi
->wq_mutex
);
58 static int autofs4_write(struct file
*file
, const void *addr
, int bytes
)
60 unsigned long sigpipe
, flags
;
62 const char *data
= (const char *)addr
;
65 /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
67 sigpipe
= sigismember(¤t
->pending
.signal
, SIGPIPE
);
69 /* Save pointer to user space and point back to kernel space */
74 (wr
= file
->f_op
->write(file
,data
,bytes
,&file
->f_pos
)) > 0) {
81 /* Keep the currently executing process from receiving a
82 SIGPIPE unless it was already supposed to get one */
83 if (wr
== -EPIPE
&& !sigpipe
) {
84 spin_lock_irqsave(¤t
->sighand
->siglock
, flags
);
85 sigdelset(¤t
->pending
.signal
, SIGPIPE
);
87 spin_unlock_irqrestore(¤t
->sighand
->siglock
, flags
);
93 static void autofs4_notify_daemon(struct autofs_sb_info
*sbi
,
94 struct autofs_wait_queue
*wq
,
98 struct autofs_packet_hdr hdr
;
99 union autofs_packet_union v4_pkt
;
100 union autofs_v5_packet_union v5_pkt
;
104 DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
105 wq
->wait_queue_token
, wq
->name
.len
, wq
->name
.name
, type
);
107 memset(&pkt
,0,sizeof pkt
); /* For security reasons */
109 pkt
.hdr
.proto_version
= sbi
->version
;
112 /* Kernel protocol v4 missing and expire packets */
113 case autofs_ptype_missing
:
115 struct autofs_packet_missing
*mp
= &pkt
.v4_pkt
.missing
;
119 mp
->wait_queue_token
= wq
->wait_queue_token
;
120 mp
->len
= wq
->name
.len
;
121 memcpy(mp
->name
, wq
->name
.name
, wq
->name
.len
);
122 mp
->name
[wq
->name
.len
] = '\0';
125 case autofs_ptype_expire_multi
:
127 struct autofs_packet_expire_multi
*ep
= &pkt
.v4_pkt
.expire_multi
;
131 ep
->wait_queue_token
= wq
->wait_queue_token
;
132 ep
->len
= wq
->name
.len
;
133 memcpy(ep
->name
, wq
->name
.name
, wq
->name
.len
);
134 ep
->name
[wq
->name
.len
] = '\0';
138 * Kernel protocol v5 packet for handling indirect and direct
139 * mount missing and expire requests
141 case autofs_ptype_missing_indirect
:
142 case autofs_ptype_expire_indirect
:
143 case autofs_ptype_missing_direct
:
144 case autofs_ptype_expire_direct
:
146 struct autofs_v5_packet
*packet
= &pkt
.v5_pkt
.v5_packet
;
148 pktsz
= sizeof(*packet
);
150 packet
->wait_queue_token
= wq
->wait_queue_token
;
151 packet
->len
= wq
->name
.len
;
152 memcpy(packet
->name
, wq
->name
.name
, wq
->name
.len
);
153 packet
->name
[wq
->name
.len
] = '\0';
154 packet
->dev
= wq
->dev
;
155 packet
->ino
= wq
->ino
;
156 packet
->uid
= wq
->uid
;
157 packet
->gid
= wq
->gid
;
158 packet
->pid
= wq
->pid
;
159 packet
->tgid
= wq
->tgid
;
163 printk("autofs4_notify_daemon: bad type %d!\n", type
);
167 if (autofs4_write(sbi
->pipe
, &pkt
, pktsz
))
168 autofs4_catatonic_mode(sbi
);
171 static int autofs4_getpath(struct autofs_sb_info
*sbi
,
172 struct dentry
*dentry
, char **name
)
174 struct dentry
*root
= sbi
->sb
->s_root
;
180 spin_lock(&dcache_lock
);
181 for (tmp
= dentry
; tmp
!= root
; tmp
= tmp
->d_parent
)
182 len
+= tmp
->d_name
.len
+ 1;
184 if (!len
|| --len
> NAME_MAX
) {
185 spin_unlock(&dcache_lock
);
190 p
= buf
+ len
- dentry
->d_name
.len
;
191 strncpy(p
, dentry
->d_name
.name
, dentry
->d_name
.len
);
193 for (tmp
= dentry
->d_parent
; tmp
!= root
; tmp
= tmp
->d_parent
) {
195 p
-= tmp
->d_name
.len
;
196 strncpy(p
, tmp
->d_name
.name
, tmp
->d_name
.len
);
198 spin_unlock(&dcache_lock
);
203 static struct autofs_wait_queue
*
204 autofs4_find_wait(struct autofs_sb_info
*sbi
, struct qstr
*qstr
)
206 struct autofs_wait_queue
*wq
;
208 for (wq
= sbi
->queues
; wq
; wq
= wq
->next
) {
209 if (wq
->name
.hash
== qstr
->hash
&&
210 wq
->name
.len
== qstr
->len
&&
212 !memcmp(wq
->name
.name
, qstr
->name
, qstr
->len
))
219 * Check if we have a valid request.
221 * 1 if the request should continue.
222 * In this case we can return an autofs_wait_queue entry if one is
223 * found or NULL to idicate a new wait needs to be created.
224 * 0 or a negative errno if the request shouldn't continue.
226 static int validate_request(struct autofs_wait_queue
**wait
,
227 struct autofs_sb_info
*sbi
,
229 struct dentry
*dentry
, enum autofs_notify notify
)
231 struct autofs_wait_queue
*wq
;
232 struct autofs_info
*ino
;
234 /* Wait in progress, continue; */
235 wq
= autofs4_find_wait(sbi
, qstr
);
243 /* If we don't yet have any info this is a new request */
244 ino
= autofs4_dentry_ino(dentry
);
249 * If we've been asked to wait on an existing expire (NFY_NONE)
250 * but there is no wait in the queue ...
252 if (notify
== NFY_NONE
) {
254 * Either we've betean the pending expire to post it's
255 * wait or it finished while we waited on the mutex.
256 * So we need to wait till either, the wait appears
257 * or the expire finishes.
260 while (ino
->flags
& AUTOFS_INF_EXPIRING
) {
261 mutex_unlock(&sbi
->wq_mutex
);
262 schedule_timeout_interruptible(HZ
/10);
263 if (mutex_lock_interruptible(&sbi
->wq_mutex
))
266 wq
= autofs4_find_wait(sbi
, qstr
);
274 * Not ideal but the status has already gone. Of the two
275 * cases where we wait on NFY_NONE neither depend on the
276 * return status of the wait.
282 * If we've been asked to trigger a mount and the request
283 * completed while we waited on the mutex ...
285 if (notify
== NFY_MOUNT
) {
287 * If the dentry isn't hashed just go ahead and try the
288 * mount again with a new wait (not much else we can do).
290 if (!d_unhashed(dentry
)) {
292 * But if the dentry is hashed, that means that we
293 * got here through the revalidate path. Thus, we
294 * need to check if the dentry has been mounted
295 * while we waited on the wq_mutex. If it has,
296 * simply return success.
298 if (d_mountpoint(dentry
))
306 int autofs4_wait(struct autofs_sb_info
*sbi
, struct dentry
*dentry
,
307 enum autofs_notify notify
)
309 struct autofs_wait_queue
*wq
;
312 int status
, ret
, type
;
314 /* In catatonic mode, we don't wait for nobody */
318 name
= kmalloc(NAME_MAX
+ 1, GFP_KERNEL
);
322 /* If this is a direct mount request create a dummy name */
323 if (IS_ROOT(dentry
) && (sbi
->type
& AUTOFS_TYPE_DIRECT
))
324 qstr
.len
= sprintf(name
, "%p", dentry
);
326 qstr
.len
= autofs4_getpath(sbi
, dentry
, &name
);
333 qstr
.hash
= full_name_hash(name
, qstr
.len
);
335 if (mutex_lock_interruptible(&sbi
->wq_mutex
)) {
340 ret
= validate_request(&wq
, sbi
, &qstr
, dentry
, notify
);
343 mutex_unlock(&sbi
->wq_mutex
);
349 /* Create a new wait queue */
350 wq
= kmalloc(sizeof(struct autofs_wait_queue
),GFP_KERNEL
);
353 mutex_unlock(&sbi
->wq_mutex
);
357 wq
->wait_queue_token
= autofs4_next_wait_queue
;
358 if (++autofs4_next_wait_queue
== 0)
359 autofs4_next_wait_queue
= 1;
360 wq
->next
= sbi
->queues
;
362 init_waitqueue_head(&wq
->queue
);
363 memcpy(&wq
->name
, &qstr
, sizeof(struct qstr
));
364 wq
->dev
= autofs4_get_dev(sbi
);
365 wq
->ino
= autofs4_get_ino(sbi
);
366 wq
->uid
= current
->uid
;
367 wq
->gid
= current
->gid
;
368 wq
->pid
= current
->pid
;
369 wq
->tgid
= current
->tgid
;
370 wq
->status
= -EINTR
; /* Status return if interrupted */
371 atomic_set(&wq
->wait_ctr
, 2);
372 mutex_unlock(&sbi
->wq_mutex
);
374 if (sbi
->version
< 5) {
375 if (notify
== NFY_MOUNT
)
376 type
= autofs_ptype_missing
;
378 type
= autofs_ptype_expire_multi
;
380 if (notify
== NFY_MOUNT
)
381 type
= (sbi
->type
& AUTOFS_TYPE_DIRECT
) ?
382 autofs_ptype_missing_direct
:
383 autofs_ptype_missing_indirect
;
385 type
= (sbi
->type
& AUTOFS_TYPE_DIRECT
) ?
386 autofs_ptype_expire_direct
:
387 autofs_ptype_expire_indirect
;
390 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
391 (unsigned long) wq
->wait_queue_token
, wq
->name
.len
,
392 wq
->name
.name
, notify
);
394 /* autofs4_notify_daemon() may block */
395 autofs4_notify_daemon(sbi
, wq
, type
);
397 atomic_inc(&wq
->wait_ctr
);
398 mutex_unlock(&sbi
->wq_mutex
);
400 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
401 (unsigned long) wq
->wait_queue_token
, wq
->name
.len
,
402 wq
->name
.name
, notify
);
406 * wq->name.name is NULL iff the lock is already released
407 * or the mount has been made catatonic.
410 /* Block all but "shutdown" signals while waiting */
412 unsigned long irqflags
;
414 spin_lock_irqsave(¤t
->sighand
->siglock
, irqflags
);
415 oldset
= current
->blocked
;
416 siginitsetinv(¤t
->blocked
, SHUTDOWN_SIGS
& ~oldset
.sig
[0]);
418 spin_unlock_irqrestore(¤t
->sighand
->siglock
, irqflags
);
420 wait_event_interruptible(wq
->queue
, wq
->name
.name
== NULL
);
422 spin_lock_irqsave(¤t
->sighand
->siglock
, irqflags
);
423 current
->blocked
= oldset
;
425 spin_unlock_irqrestore(¤t
->sighand
->siglock
, irqflags
);
427 DPRINTK("skipped sleeping");
432 /* Are we the last process to need status? */
433 if (atomic_dec_and_test(&wq
->wait_ctr
))
440 int autofs4_wait_release(struct autofs_sb_info
*sbi
, autofs_wqt_t wait_queue_token
, int status
)
442 struct autofs_wait_queue
*wq
, **wql
;
444 mutex_lock(&sbi
->wq_mutex
);
445 for (wql
= &sbi
->queues
; (wq
= *wql
) != NULL
; wql
= &wq
->next
) {
446 if (wq
->wait_queue_token
== wait_queue_token
)
451 mutex_unlock(&sbi
->wq_mutex
);
455 *wql
= wq
->next
; /* Unlink from chain */
456 kfree(wq
->name
.name
);
457 wq
->name
.name
= NULL
; /* Do not wait on this queue */
458 mutex_unlock(&sbi
->wq_mutex
);
462 if (atomic_dec_and_test(&wq
->wait_ctr
)) /* Is anyone still waiting for this guy? */
465 wake_up_interruptible(&wq
->queue
);