1 /* -*- 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/slab.h>
14 #include <linux/time.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 autofs4_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 autofs4_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 */
44 fput(sbi->pipe); /* Close the pipe */
48 shrink_dcache_sb(sbi->sb);
51 static int autofs4_write(struct file *file, const void *addr, int bytes)
53 unsigned long sigpipe, flags;
55 const char *data = (const char *)addr;
58 /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
60 sigpipe = sigismember(¤t->pending.signal, SIGPIPE);
62 /* Save pointer to user space and point back to kernel space */
67 (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) {
74 /* Keep the currently executing process from receiving a
75 SIGPIPE unless it was already supposed to get one */
76 if (wr == -EPIPE && !sigpipe) {
77 spin_lock_irqsave(¤t->sighand->siglock, flags);
78 sigdelset(¤t->pending.signal, SIGPIPE);
80 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
86 static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
87 struct autofs_wait_queue *wq,
90 union autofs_packet_union pkt;
93 DPRINTK(("autofs_notify: wait id = 0x%08lx, name = %.*s, type=%d\n",
94 wq->wait_queue_token, wq->len, wq->name, type));
96 memset(&pkt,0,sizeof pkt); /* For security reasons */
98 pkt.hdr.proto_version = sbi->version;
100 if (type == autofs_ptype_missing) {
101 struct autofs_packet_missing *mp = &pkt.missing;
105 mp->wait_queue_token = wq->wait_queue_token;
107 memcpy(mp->name, wq->name, wq->len);
108 mp->name[wq->len] = '\0';
109 } else if (type == autofs_ptype_expire_multi) {
110 struct autofs_packet_expire_multi *ep = &pkt.expire_multi;
114 ep->wait_queue_token = wq->wait_queue_token;
116 memcpy(ep->name, wq->name, wq->len);
117 ep->name[wq->len] = '\0';
119 printk("autofs_notify_daemon: bad type %d!\n", type);
123 if (autofs4_write(sbi->pipe, &pkt, pktsz))
124 autofs4_catatonic_mode(sbi);
127 int autofs4_wait(struct autofs_sb_info *sbi, struct qstr *name,
128 enum autofs_notify notify)
130 struct autofs_wait_queue *wq;
133 /* In catatonic mode, we don't wait for nobody */
134 if ( sbi->catatonic )
137 /* We shouldn't be able to get here, but just in case */
138 if ( name->len > NAME_MAX )
141 for ( wq = sbi->queues ; wq ; wq = wq->next ) {
142 if ( wq->hash == name->hash &&
143 wq->len == name->len &&
144 wq->name && !memcmp(wq->name,name->name,name->len) )
149 /* Create a new wait queue */
150 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
154 wq->name = kmalloc(name->len,GFP_KERNEL);
159 wq->wait_queue_token = autofs4_next_wait_queue;
160 if (++autofs4_next_wait_queue == 0)
161 autofs4_next_wait_queue = 1;
162 init_waitqueue_head(&wq->queue);
163 wq->hash = name->hash;
165 wq->status = -EINTR; /* Status return if interrupted */
166 memcpy(wq->name, name->name, name->len);
167 wq->next = sbi->queues;
170 DPRINTK(("autofs_wait: new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
171 wq->wait_queue_token, wq->len, wq->name, notify));
172 /* autofs4_notify_daemon() may block */
174 if (notify != NFY_NONE) {
175 autofs4_notify_daemon(sbi,wq,
176 notify == NFY_MOUNT ? autofs_ptype_missing :
177 autofs_ptype_expire_multi);
181 DPRINTK(("autofs_wait: existing wait id = 0x%08lx, name = %.*s, nfy=%d\n",
182 wq->wait_queue_token, wq->len, wq->name, notify));
185 /* wq->name is NULL if and only if the lock is already released */
187 if ( sbi->catatonic ) {
188 /* We might have slept, so check again for catatonic mode */
189 wq->status = -ENOENT;
197 /* Block all but "shutdown" signals while waiting */
199 unsigned long irqflags;
201 spin_lock_irqsave(¤t->sighand->siglock, irqflags);
202 oldset = current->blocked;
203 siginitsetinv(¤t->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
205 spin_unlock_irqrestore(¤t->sighand->siglock, irqflags);
207 interruptible_sleep_on(&wq->queue);
209 spin_lock_irqsave(¤t->sighand->siglock, irqflags);
210 current->blocked = oldset;
212 spin_unlock_irqrestore(¤t->sighand->siglock, irqflags);
214 DPRINTK(("autofs_wait: skipped sleeping\n"));
219 if (--wq->wait_ctr == 0) /* Are we the last process to need status? */
226 int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
228 struct autofs_wait_queue *wq, **wql;
230 for ( wql = &sbi->queues ; (wq = *wql) ; wql = &wq->next ) {
231 if ( wq->wait_queue_token == wait_queue_token )
237 *wql = wq->next; /* Unlink from chain */
239 wq->name = NULL; /* Do not wait on this queue */
243 if (--wq->wait_ctr == 0) /* Is anyone still waiting for this guy? */