blob: 5208cfb1df4e5f9c134a1bfad60aaf404befe38f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- c -*- --------------------------------------------------------------- *
2 *
3 * linux/fs/autofs/waitq.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
Ian Kent5c0a32f2006-03-27 01:14:55 -08006 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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.
11 *
12 * ------------------------------------------------------------------------- */
13
14#include <linux/slab.h>
15#include <linux/time.h>
16#include <linux/signal.h>
17#include <linux/file.h>
18#include "autofs_i.h"
19
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 */
22static autofs_wqt_t autofs4_next_wait_queue = 1;
23
24/* These are the signals we allow interrupting a pending mount */
25#define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT))
26
27void autofs4_catatonic_mode(struct autofs_sb_info *sbi)
28{
29 struct autofs_wait_queue *wq, *nwq;
30
31 DPRINTK("entering catatonic mode");
32
33 sbi->catatonic = 1;
34 wq = sbi->queues;
35 sbi->queues = NULL; /* Erase all wait queues */
Ian Kente77fbdd2006-03-27 01:14:49 -080036 while (wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 nwq = wq->next;
38 wq->status = -ENOENT; /* Magic is gone - report failure */
Jeff Moyer70b52a02008-07-23 21:30:16 -070039 if (wq->name.name) {
40 kfree(wq->name.name);
41 wq->name.name = NULL;
42 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 wake_up_interruptible(&wq->queue);
44 wq = nwq;
45 }
Ian Kentba8df432006-11-14 02:03:29 -080046 fput(sbi->pipe); /* Close the pipe */
47 sbi->pipe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50static int autofs4_write(struct file *file, const void *addr, int bytes)
51{
52 unsigned long sigpipe, flags;
53 mm_segment_t fs;
54 const char *data = (const char *)addr;
55 ssize_t wr = 0;
56
57 /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
58
59 sigpipe = sigismember(&current->pending.signal, SIGPIPE);
60
61 /* Save pointer to user space and point back to kernel space */
62 fs = get_fs();
63 set_fs(KERNEL_DS);
64
65 while (bytes &&
66 (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) {
67 data += wr;
68 bytes -= wr;
69 }
70
71 set_fs(fs);
72
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(&current->sighand->siglock, flags);
77 sigdelset(&current->pending.signal, SIGPIPE);
78 recalc_sigpending();
79 spin_unlock_irqrestore(&current->sighand->siglock, flags);
80 }
81
82 return (bytes > 0);
83}
84
85static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
86 struct autofs_wait_queue *wq,
87 int type)
88{
Ian Kente8514472007-02-20 13:58:09 -080089 union {
90 struct autofs_packet_hdr hdr;
91 union autofs_packet_union v4_pkt;
92 union autofs_v5_packet_union v5_pkt;
93 } pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 size_t pktsz;
95
96 DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
Jeff Moyer70b52a02008-07-23 21:30:16 -070097 wq->wait_queue_token, wq->name.len, wq->name.name, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 memset(&pkt,0,sizeof pkt); /* For security reasons */
100
101 pkt.hdr.proto_version = sbi->version;
102 pkt.hdr.type = type;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800103 switch (type) {
104 /* Kernel protocol v4 missing and expire packets */
105 case autofs_ptype_missing:
106 {
Ian Kente8514472007-02-20 13:58:09 -0800107 struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 pktsz = sizeof(*mp);
110
111 mp->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700112 mp->len = wq->name.len;
113 memcpy(mp->name, wq->name.name, wq->name.len);
114 mp->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800115 break;
116 }
117 case autofs_ptype_expire_multi:
118 {
Ian Kente8514472007-02-20 13:58:09 -0800119 struct autofs_packet_expire_multi *ep = &pkt.v4_pkt.expire_multi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 pktsz = sizeof(*ep);
122
123 ep->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700124 ep->len = wq->name.len;
125 memcpy(ep->name, wq->name.name, wq->name.len);
126 ep->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800127 break;
128 }
129 /*
130 * Kernel protocol v5 packet for handling indirect and direct
131 * mount missing and expire requests
132 */
133 case autofs_ptype_missing_indirect:
134 case autofs_ptype_expire_indirect:
135 case autofs_ptype_missing_direct:
136 case autofs_ptype_expire_direct:
137 {
Ian Kente8514472007-02-20 13:58:09 -0800138 struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800139
140 pktsz = sizeof(*packet);
141
142 packet->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700143 packet->len = wq->name.len;
144 memcpy(packet->name, wq->name.name, wq->name.len);
145 packet->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800146 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;
152 break;
153 }
154 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 printk("autofs4_notify_daemon: bad type %d!\n", type);
156 return;
157 }
158
159 if (autofs4_write(sbi->pipe, &pkt, pktsz))
160 autofs4_catatonic_mode(sbi);
161}
162
163static int autofs4_getpath(struct autofs_sb_info *sbi,
164 struct dentry *dentry, char **name)
165{
166 struct dentry *root = sbi->sb->s_root;
167 struct dentry *tmp;
168 char *buf = *name;
169 char *p;
170 int len = 0;
171
172 spin_lock(&dcache_lock);
173 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
174 len += tmp->d_name.len + 1;
175
Ian Kentcab09362008-05-01 04:35:07 -0700176 if (!len || --len > NAME_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 spin_unlock(&dcache_lock);
178 return 0;
179 }
180
181 *(buf + len) = '\0';
182 p = buf + len - dentry->d_name.len;
183 strncpy(p, dentry->d_name.name, dentry->d_name.len);
184
185 for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
186 *(--p) = '/';
187 p -= tmp->d_name.len;
188 strncpy(p, tmp->d_name.name, tmp->d_name.len);
189 }
190 spin_unlock(&dcache_lock);
191
192 return len;
193}
194
Ian Kenta5370552006-05-15 09:43:51 -0700195static struct autofs_wait_queue *
Jeff Moyer70b52a02008-07-23 21:30:16 -0700196autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr)
Ian Kenta5370552006-05-15 09:43:51 -0700197{
198 struct autofs_wait_queue *wq;
199
200 for (wq = sbi->queues; wq; wq = wq->next) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700201 if (wq->name.hash == qstr->hash &&
202 wq->name.len == qstr->len &&
203 wq->name.name &&
204 !memcmp(wq->name.name, qstr->name, qstr->len))
Ian Kenta5370552006-05-15 09:43:51 -0700205 break;
206 }
207 return wq;
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
211 enum autofs_notify notify)
212{
Ian Kenta5370552006-05-15 09:43:51 -0700213 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct autofs_wait_queue *wq;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700215 struct qstr qstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 char *name;
Ian Kenta5370552006-05-15 09:43:51 -0700217 int status, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219 /* In catatonic mode, we don't wait for nobody */
Ian Kente77fbdd2006-03-27 01:14:49 -0800220 if (sbi->catatonic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return -ENOENT;
222
223 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
224 if (!name)
225 return -ENOMEM;
226
Ian Kent5c0a32f2006-03-27 01:14:55 -0800227 /* If this is a direct mount request create a dummy name */
Ian Kent44d53eb2006-03-27 01:14:56 -0800228 if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT))
Jeff Moyer70b52a02008-07-23 21:30:16 -0700229 qstr.len = sprintf(name, "%p", dentry);
Ian Kent5c0a32f2006-03-27 01:14:55 -0800230 else {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700231 qstr.len = autofs4_getpath(sbi, dentry, &name);
232 if (!qstr.len) {
Ian Kent5c0a32f2006-03-27 01:14:55 -0800233 kfree(name);
234 return -ENOENT;
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Jeff Moyer70b52a02008-07-23 21:30:16 -0700237 qstr.name = name;
238 qstr.hash = full_name_hash(name, qstr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800240 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700241 kfree(qstr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return -EINTR;
243 }
244
Jeff Moyer70b52a02008-07-23 21:30:16 -0700245 wq = autofs4_find_wait(sbi, &qstr);
Ian Kenta5370552006-05-15 09:43:51 -0700246 ino = autofs4_dentry_ino(dentry);
247 if (!wq && ino && notify == NFY_NONE) {
248 /*
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.
253 */
254
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)) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700259 kfree(qstr.name);
Ian Kenta5370552006-05-15 09:43:51 -0700260 return -EINTR;
261 }
Jeff Moyer70b52a02008-07-23 21:30:16 -0700262 wq = autofs4_find_wait(sbi, &qstr);
Ian Kenta5370552006-05-15 09:43:51 -0700263 if (wq)
264 break;
265 }
266
267 /*
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.
271 */
272 if (!wq) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700273 kfree(qstr.name);
Ian Kenta5370552006-05-15 09:43:51 -0700274 mutex_unlock(&sbi->wq_mutex);
275 return 0;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
Ian Kente77fbdd2006-03-27 01:14:49 -0800279 if (!wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 /* Create a new wait queue */
281 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
Ian Kente77fbdd2006-03-27 01:14:49 -0800282 if (!wq) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700283 kfree(qstr.name);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800284 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return -ENOMEM;
286 }
287
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;
292 sbi->queues = wq;
293 init_waitqueue_head(&wq->queue);
Jeff Moyer70b52a02008-07-23 21:30:16 -0700294 memcpy(&wq->name, &qstr, sizeof(struct qstr));
Ian Kent5c0a32f2006-03-27 01:14:55 -0800295 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 wq->status = -EINTR; /* Status return if interrupted */
302 atomic_set(&wq->wait_ctr, 2);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800303 mutex_unlock(&sbi->wq_mutex);
Ian Kent3e7b1912006-03-27 01:14:59 -0800304
Ian Kent5c0a32f2006-03-27 01:14:55 -0800305 if (sbi->version < 5) {
306 if (notify == NFY_MOUNT)
307 type = autofs_ptype_missing;
308 else
309 type = autofs_ptype_expire_multi;
310 } else {
311 if (notify == NFY_MOUNT)
Ian Kent44d53eb2006-03-27 01:14:56 -0800312 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800313 autofs_ptype_missing_direct :
314 autofs_ptype_missing_indirect;
315 else
Ian Kent44d53eb2006-03-27 01:14:56 -0800316 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800317 autofs_ptype_expire_direct :
318 autofs_ptype_expire_indirect;
319 }
Ian Kent4dcd00b2005-05-01 08:59:16 -0700320
Ian Kent682d4fc2005-07-07 17:57:02 -0700321 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
Jeff Moyer70b52a02008-07-23 21:30:16 -0700322 (unsigned long) wq->wait_queue_token, wq->name.len,
323 wq->name.name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700324
325 /* autofs4_notify_daemon() may block */
326 autofs4_notify_daemon(sbi, wq, type);
Ian Kenta5370552006-05-15 09:43:51 -0700327 } else {
328 atomic_inc(&wq->wait_ctr);
329 mutex_unlock(&sbi->wq_mutex);
Jeff Moyer70b52a02008-07-23 21:30:16 -0700330 kfree(qstr.name);
Ian Kenta5370552006-05-15 09:43:51 -0700331 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
Jeff Moyer70b52a02008-07-23 21:30:16 -0700332 (unsigned long) wq->wait_queue_token, wq->name.len,
333 wq->name.name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700334 }
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* wq->name is NULL if and only if the lock is already released */
337
Ian Kente77fbdd2006-03-27 01:14:49 -0800338 if (sbi->catatonic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 /* We might have slept, so check again for catatonic mode */
340 wq->status = -ENOENT;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700341 if (wq->name.name) {
342 kfree(wq->name.name);
343 wq->name.name = NULL;
344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
Jeff Moyer70b52a02008-07-23 21:30:16 -0700347 if (wq->name.name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /* Block all but "shutdown" signals while waiting */
349 sigset_t oldset;
350 unsigned long irqflags;
351
352 spin_lock_irqsave(&current->sighand->siglock, irqflags);
353 oldset = current->blocked;
354 siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
355 recalc_sigpending();
356 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
357
Jeff Moyer70b52a02008-07-23 21:30:16 -0700358 wait_event_interruptible(wq->queue, wq->name.name == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 spin_lock_irqsave(&current->sighand->siglock, irqflags);
361 current->blocked = oldset;
362 recalc_sigpending();
363 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
364 } else {
365 DPRINTK("skipped sleeping");
366 }
367
368 status = wq->status;
369
370 /* Are we the last process to need status? */
371 if (atomic_dec_and_test(&wq->wait_ctr))
372 kfree(wq);
373
374 return status;
375}
376
377
378int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
379{
380 struct autofs_wait_queue *wq, **wql;
381
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800382 mutex_lock(&sbi->wq_mutex);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700383 for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
Ian Kente77fbdd2006-03-27 01:14:49 -0800384 if (wq->wait_queue_token == wait_queue_token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
386 }
387
Ian Kente77fbdd2006-03-27 01:14:49 -0800388 if (!wq) {
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800389 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return -EINVAL;
391 }
392
393 *wql = wq->next; /* Unlink from chain */
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800394 mutex_unlock(&sbi->wq_mutex);
Jeff Moyer70b52a02008-07-23 21:30:16 -0700395 kfree(wq->name.name);
396 wq->name.name = NULL; /* Do not wait on this queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 wq->status = status;
399
400 if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
401 kfree(wq);
402 else
403 wake_up_interruptible(&wq->queue);
404
405 return 0;
406}
407