1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * linux/fs/autofs/waitq.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 * ------------------------------------------------------------------------- */
13 #include <linux/malloc.h>
14 #include <linux/sched.h>
15 #include <linux/signal.h>
16 #include <linux/file.h>
19 /* We make this a static variable rather than a part of the superblock; it
20 is better if we don't reassign numbers easily even across filesystems */
21 static autofs_wqt_t autofs_next_wait_queue
= 1;
23 /* These are the signals we allow interrupting a pending mount */
24 #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT))
26 void autofs_catatonic_mode(struct autofs_sb_info
*sbi
)
28 struct autofs_wait_queue
*wq
, *nwq
;
30 DPRINTK(("autofs: entering catatonic mode\n"));
34 sbi
->queues
= NULL
; /* Erase all wait queues */
37 wq
->status
= -ENOENT
; /* Magic is gone - report failure */
43 fput(sbi
->pipe
); /* Close the pipe */
44 autofs_hash_dputall(&sbi
->dirhash
); /* Remove all dentry pointers */
47 static int autofs_write(struct file
*file
, const void *addr
, int bytes
)
49 unsigned long sigpipe
, flags
;
51 const char *data
= (const char *)addr
;
54 /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
56 sigpipe
= sigismember(¤t
->signal
, SIGPIPE
);
58 /* Save pointer to user space and point back to kernel space */
63 (wr
= file
->f_op
->write(file
,data
,bytes
,&file
->f_pos
)) > 0) {
70 /* Keep the currently executing process from receiving a
71 SIGPIPE unless it was already supposed to get one */
72 if (wr
== -EPIPE
&& !sigpipe
) {
73 spin_lock_irqsave(¤t
->sigmask_lock
, flags
);
74 sigdelset(¤t
->signal
, SIGPIPE
);
75 recalc_sigpending(current
);
76 spin_unlock_irqrestore(¤t
->sigmask_lock
, flags
);
82 static void autofs_notify_daemon(struct autofs_sb_info
*sbi
, struct autofs_wait_queue
*wq
)
84 struct autofs_packet_missing pkt
;
86 DPRINTK(("autofs_wait: wait id = 0x%08lx, name = ", wq
->wait_queue_token
));
87 autofs_say(wq
->name
,wq
->len
);
89 memset(&pkt
,0,sizeof pkt
); /* For security reasons */
91 pkt
.hdr
.proto_version
= AUTOFS_PROTO_VERSION
;
92 pkt
.hdr
.type
= autofs_ptype_missing
;
93 pkt
.wait_queue_token
= wq
->wait_queue_token
;
95 memcpy(pkt
.name
, wq
->name
, pkt
.len
);
96 pkt
.name
[pkt
.len
] = '\0';
98 if ( autofs_write(sbi
->pipe
,&pkt
,sizeof(struct autofs_packet_missing
)) )
99 autofs_catatonic_mode(sbi
);
102 int autofs_wait(struct autofs_sb_info
*sbi
, struct qstr
* name
)
104 struct autofs_wait_queue
*wq
;
107 /* In catatonic mode, we don't wait for nobody */
108 if ( sbi
->catatonic
)
111 for ( wq
= sbi
->queues
; wq
; wq
= wq
->next
) {
112 if ( wq
->hash
== name
->hash
&&
113 wq
->len
== name
->len
&&
114 wq
->name
&& !memcmp(wq
->name
,name
->name
,name
->len
) )
119 /* Create a new wait queue */
120 wq
= kmalloc(sizeof(struct autofs_wait_queue
),GFP_KERNEL
);
124 wq
->name
= kmalloc(name
->len
,GFP_KERNEL
);
129 wq
->wait_queue_token
= autofs_next_wait_queue
++;
130 init_waitqueue(&wq
->queue
);
131 wq
->hash
= name
->hash
;
133 wq
->status
= -EINTR
; /* Status return if interrupted */
134 memcpy(wq
->name
, name
->name
, name
->len
);
135 wq
->next
= sbi
->queues
;
138 /* autofs_notify_daemon() may block */
140 autofs_notify_daemon(sbi
,wq
);
144 /* wq->name is NULL if and only if the lock is already released */
146 if ( sbi
->catatonic
) {
147 /* We might have slept, so check again for catatonic mode */
148 wq
->status
= -ENOENT
;
156 /* Block all but "shutdown" signals while waiting */
158 unsigned long irqflags
;
160 spin_lock_irqsave(¤t
->sigmask_lock
, irqflags
);
161 oldset
= current
->blocked
;
162 siginitsetinv(¤t
->blocked
, SHUTDOWN_SIGS
& ~oldset
.sig
[0]);
163 recalc_sigpending(current
);
164 spin_unlock_irqrestore(¤t
->sigmask_lock
, irqflags
);
166 interruptible_sleep_on(&wq
->queue
);
168 spin_lock_irqsave(¤t
->sigmask_lock
, irqflags
);
169 current
->blocked
= oldset
;
170 recalc_sigpending(current
);
171 spin_unlock_irqrestore(¤t
->sigmask_lock
, irqflags
);
173 DPRINTK(("autofs_wait: skipped sleeping\n"));
178 if ( ! --wq
->wait_ctr
) /* Are we the last process to need status? */
185 int autofs_wait_release(struct autofs_sb_info
*sbi
, autofs_wqt_t wait_queue_token
, int status
)
187 struct autofs_wait_queue
*wq
, **wql
;
189 for ( wql
= &sbi
->queues
; (wq
= *wql
) ; wql
= &wq
->next
) {
190 if ( wq
->wait_queue_token
== wait_queue_token
)
196 *wql
= wq
->next
; /* Unlink from chain */
198 wq
->name
= NULL
; /* Do not wait on this queue */
202 if ( ! --wq
->wait_ctr
) /* Is anyone still waiting for this guy? */