1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/waitq.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 2001-2003 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 DPRINTK("entering catatonic mode");
35 sbi->queues = NULL; /* Erase all wait queues */
38 wq->status = -ENOENT; /* Magic is gone - report failure */
41 wake_up_interruptible(&wq->queue);
45 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("wait id = 0x%08lx, name = %.*s, type=%d",
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("autofs4_notify_daemon: bad type %d!\n", type);
123 if (autofs4_write(sbi->pipe, &pkt, pktsz))
124 autofs4_catatonic_mode(sbi);
127 static int autofs4_getpath(struct autofs_sb_info *sbi,
128 struct dentry *dentry, char **name)
130 struct dentry *root = sbi->sb->s_root;
136 spin_lock(&dcache_lock);
137 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
138 len += tmp->d_name.len + 1;
140 if (--len > NAME_MAX) {
141 spin_unlock(&dcache_lock);
146 p = buf + len - dentry->d_name.len;
147 strncpy(p, dentry->d_name.name, dentry->d_name.len);
149 for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
151 p -= tmp->d_name.len;
152 strncpy(p, tmp->d_name.name, tmp->d_name.len);
154 spin_unlock(&dcache_lock);
159 int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
160 enum autofs_notify notify)
162 struct autofs_wait_queue *wq;
166 /* In catatonic mode, we don't wait for nobody */
170 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
174 len = autofs4_getpath(sbi, dentry, &name);
180 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
185 for (wq = sbi->queues ; wq ; wq = wq->next) {
186 if (wq->hash == dentry->d_name.hash &&
188 wq->name && !memcmp(wq->name, name, len))
193 /* Can't wait for an expire if there's no mount */
194 if (notify == NFY_NONE && !d_mountpoint(dentry)) {
196 mutex_unlock(&sbi->wq_mutex);
200 /* Create a new wait queue */
201 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
204 mutex_unlock(&sbi->wq_mutex);
208 wq->wait_queue_token = autofs4_next_wait_queue;
209 if (++autofs4_next_wait_queue == 0)
210 autofs4_next_wait_queue = 1;
211 wq->next = sbi->queues;
213 init_waitqueue_head(&wq->queue);
214 wq->hash = dentry->d_name.hash;
217 wq->status = -EINTR; /* Status return if interrupted */
218 atomic_set(&wq->wait_ctr, 2);
219 atomic_set(&wq->notified, 1);
220 mutex_unlock(&sbi->wq_mutex);
222 atomic_inc(&wq->wait_ctr);
223 mutex_unlock(&sbi->wq_mutex);
225 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
226 (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
229 if (notify != NFY_NONE && atomic_dec_and_test(&wq->notified)) {
230 int type = (notify == NFY_MOUNT ?
231 autofs_ptype_missing : autofs_ptype_expire_multi);
233 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
234 (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
236 /* autofs4_notify_daemon() may block */
237 autofs4_notify_daemon(sbi, wq, type);
240 /* wq->name is NULL if and only if the lock is already released */
242 if (sbi->catatonic) {
243 /* We might have slept, so check again for catatonic mode */
244 wq->status = -ENOENT;
250 /* Block all but "shutdown" signals while waiting */
252 unsigned long irqflags;
254 spin_lock_irqsave(¤t->sighand->siglock, irqflags);
255 oldset = current->blocked;
256 siginitsetinv(¤t->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
258 spin_unlock_irqrestore(¤t->sighand->siglock, irqflags);
260 wait_event_interruptible(wq->queue, wq->name == NULL);
262 spin_lock_irqsave(¤t->sighand->siglock, irqflags);
263 current->blocked = oldset;
265 spin_unlock_irqrestore(¤t->sighand->siglock, irqflags);
267 DPRINTK("skipped sleeping");
272 /* Are we the last process to need status? */
273 if (atomic_dec_and_test(&wq->wait_ctr))
280 int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
282 struct autofs_wait_queue *wq, **wql;
284 mutex_lock(&sbi->wq_mutex);
285 for (wql = &sbi->queues ; (wq = *wql) != 0 ; wql = &wq->next) {
286 if (wq->wait_queue_token == wait_queue_token)
291 mutex_unlock(&sbi->wq_mutex);
295 *wql = wq->next; /* Unlink from chain */
296 mutex_unlock(&sbi->wq_mutex);
298 wq->name = NULL; /* Do not wait on this queue */
302 if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
305 wake_up_interruptible(&wq->queue);