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 DPRINTK("entering catatonic mode");
35 sbi->queues = NULL; /* Erase all wait queues */
38 wq->status = -ENOENT; /* Magic is gone - report failure */
43 wake_up_interruptible(&wq->queue);
46 fput(sbi->pipe); /* Close the pipe */
50 static int autofs4_write(struct file *file, const void *addr, int bytes)
52 unsigned long sigpipe, flags;
54 const char *data = (const char *)addr;
57 /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
59 sigpipe = sigismember(¤t->pending.signal, SIGPIPE);
61 /* Save pointer to user space and point back to kernel space */
66 (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) {
73 /* Keep the currently executing process from receiving a
74 SIGPIPE unless it was already supposed to get one */
75 if (wr == -EPIPE && !sigpipe) {
76 spin_lock_irqsave(¤t->sighand->siglock, flags);
77 sigdelset(¤t->pending.signal, SIGPIPE);
79 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
85 static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
86 struct autofs_wait_queue *wq,
90 struct autofs_packet_hdr hdr;
91 union autofs_packet_union v4_pkt;
92 union autofs_v5_packet_union v5_pkt;
96 DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
97 wq->wait_queue_token, wq->name.len, wq->name.name, type);
99 memset(&pkt,0,sizeof pkt); /* For security reasons */
101 pkt.hdr.proto_version = sbi->version;
104 /* Kernel protocol v4 missing and expire packets */
105 case autofs_ptype_missing:
107 struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
111 mp->wait_queue_token = wq->wait_queue_token;
112 mp->len = wq->name.len;
113 memcpy(mp->name, wq->name.name, wq->name.len);
114 mp->name[wq->name.len] = '\0';
117 case autofs_ptype_expire_multi:
119 struct autofs_packet_expire_multi *ep = &pkt.v4_pkt.expire_multi;
123 ep->wait_queue_token = wq->wait_queue_token;
124 ep->len = wq->name.len;
125 memcpy(ep->name, wq->name.name, wq->name.len);
126 ep->name[wq->name.len] = '\0';
130 * Kernel protocol v5 packet for handling indirect and direct
131 * mount missing and expire requests
133 case autofs_ptype_missing_indirect:
134 case autofs_ptype_expire_indirect:
135 case autofs_ptype_missing_direct:
136 case autofs_ptype_expire_direct:
138 struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
140 pktsz = sizeof(*packet);
142 packet->wait_queue_token = wq->wait_queue_token;
143 packet->len = wq->name.len;
144 memcpy(packet->name, wq->name.name, wq->name.len);
145 packet->name[wq->name.len] = '\0';
146 packet->dev = wq->dev;
147 packet->ino = wq->ino;
148 packet->uid = wq->uid;
149 packet->gid = wq->gid;
150 packet->pid = wq->pid;
151 packet->tgid = wq->tgid;
155 printk("autofs4_notify_daemon: bad type %d!\n", type);
159 if (autofs4_write(sbi->pipe, &pkt, pktsz))
160 autofs4_catatonic_mode(sbi);
163 static int autofs4_getpath(struct autofs_sb_info *sbi,
164 struct dentry *dentry, char **name)
166 struct dentry *root = sbi->sb->s_root;
172 spin_lock(&dcache_lock);
173 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
174 len += tmp->d_name.len + 1;
176 if (!len || --len > NAME_MAX) {
177 spin_unlock(&dcache_lock);
182 p = buf + len - dentry->d_name.len;
183 strncpy(p, dentry->d_name.name, dentry->d_name.len);
185 for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
187 p -= tmp->d_name.len;
188 strncpy(p, tmp->d_name.name, tmp->d_name.len);
190 spin_unlock(&dcache_lock);
195 static struct autofs_wait_queue *
196 autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr)
198 struct autofs_wait_queue *wq;
200 for (wq = sbi->queues; wq; wq = wq->next) {
201 if (wq->name.hash == qstr->hash &&
202 wq->name.len == qstr->len &&
204 !memcmp(wq->name.name, qstr->name, qstr->len))
210 int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
211 enum autofs_notify notify)
213 struct autofs_info *ino;
214 struct autofs_wait_queue *wq;
219 /* In catatonic mode, we don't wait for nobody */
223 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
227 /* If this is a direct mount request create a dummy name */
228 if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT))
229 qstr.len = sprintf(name, "%p", dentry);
231 qstr.len = autofs4_getpath(sbi, dentry, &name);
238 qstr.hash = full_name_hash(name, qstr.len);
240 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
245 wq = autofs4_find_wait(sbi, &qstr);
246 ino = autofs4_dentry_ino(dentry);
247 if (!wq && ino && notify == NFY_NONE) {
249 * Either we've betean the pending expire to post it's
250 * wait or it finished while we waited on the mutex.
251 * So we need to wait till either, the wait appears
252 * or the expire finishes.
255 while (ino->flags & AUTOFS_INF_EXPIRING) {
256 mutex_unlock(&sbi->wq_mutex);
257 schedule_timeout_interruptible(HZ/10);
258 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
262 wq = autofs4_find_wait(sbi, &qstr);
268 * Not ideal but the status has already gone. Of the two
269 * cases where we wait on NFY_NONE neither depend on the
270 * return status of the wait.
274 mutex_unlock(&sbi->wq_mutex);
280 /* Create a new wait queue */
281 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
284 mutex_unlock(&sbi->wq_mutex);
288 wq->wait_queue_token = autofs4_next_wait_queue;
289 if (++autofs4_next_wait_queue == 0)
290 autofs4_next_wait_queue = 1;
291 wq->next = sbi->queues;
293 init_waitqueue_head(&wq->queue);
294 memcpy(&wq->name, &qstr, sizeof(struct qstr));
295 wq->dev = autofs4_get_dev(sbi);
296 wq->ino = autofs4_get_ino(sbi);
297 wq->uid = current->uid;
298 wq->gid = current->gid;
299 wq->pid = current->pid;
300 wq->tgid = current->tgid;
301 wq->status = -EINTR; /* Status return if interrupted */
302 atomic_set(&wq->wait_ctr, 2);
303 mutex_unlock(&sbi->wq_mutex);
305 if (sbi->version < 5) {
306 if (notify == NFY_MOUNT)
307 type = autofs_ptype_missing;
309 type = autofs_ptype_expire_multi;
311 if (notify == NFY_MOUNT)
312 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
313 autofs_ptype_missing_direct :
314 autofs_ptype_missing_indirect;
316 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
317 autofs_ptype_expire_direct :
318 autofs_ptype_expire_indirect;
321 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
322 (unsigned long) wq->wait_queue_token, wq->name.len,
323 wq->name.name, notify);
325 /* autofs4_notify_daemon() may block */
326 autofs4_notify_daemon(sbi, wq, type);
328 atomic_inc(&wq->wait_ctr);
329 mutex_unlock(&sbi->wq_mutex);
331 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
332 (unsigned long) wq->wait_queue_token, wq->name.len,
333 wq->name.name, notify);
336 /* wq->name is NULL if and only if the lock is already released */
338 if (sbi->catatonic) {
339 /* We might have slept, so check again for catatonic mode */
340 wq->status = -ENOENT;
342 kfree(wq->name.name);
343 wq->name.name = NULL;
348 /* Block all but "shutdown" signals while waiting */
350 unsigned long irqflags;
352 spin_lock_irqsave(¤t->sighand->siglock, irqflags);
353 oldset = current->blocked;
354 siginitsetinv(¤t->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
356 spin_unlock_irqrestore(¤t->sighand->siglock, irqflags);
358 wait_event_interruptible(wq->queue, wq->name.name == NULL);
360 spin_lock_irqsave(¤t->sighand->siglock, irqflags);
361 current->blocked = oldset;
363 spin_unlock_irqrestore(¤t->sighand->siglock, irqflags);
365 DPRINTK("skipped sleeping");
370 /* Are we the last process to need status? */
371 if (atomic_dec_and_test(&wq->wait_ctr))
378 int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
380 struct autofs_wait_queue *wq, **wql;
382 mutex_lock(&sbi->wq_mutex);
383 for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
384 if (wq->wait_queue_token == wait_queue_token)
389 mutex_unlock(&sbi->wq_mutex);
393 *wql = wq->next; /* Unlink from chain */
394 mutex_unlock(&sbi->wq_mutex);
395 kfree(wq->name.name);
396 wq->name.name = NULL; /* Do not wait on this queue */
400 if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
403 wake_up_interruptible(&wq->queue);