blob: c0a6c8d445c7b7cffc8a7c5163bd32d7ba6bdbd7 [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 */
39 kfree(wq->name);
40 wq->name = NULL;
41 wake_up_interruptible(&wq->queue);
42 wq = nwq;
43 }
44 if (sbi->pipe) {
45 fput(sbi->pipe); /* Close the pipe */
46 sbi->pipe = NULL;
47 }
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{
89 union autofs_packet_union pkt;
90 size_t pktsz;
91
92 DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
93 wq->wait_queue_token, wq->len, wq->name, type);
94
95 memset(&pkt,0,sizeof pkt); /* For security reasons */
96
97 pkt.hdr.proto_version = sbi->version;
98 pkt.hdr.type = type;
Ian Kent5c0a32f2006-03-27 01:14:55 -080099 switch (type) {
100 /* Kernel protocol v4 missing and expire packets */
101 case autofs_ptype_missing:
102 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct autofs_packet_missing *mp = &pkt.missing;
104
105 pktsz = sizeof(*mp);
106
107 mp->wait_queue_token = wq->wait_queue_token;
108 mp->len = wq->len;
109 memcpy(mp->name, wq->name, wq->len);
110 mp->name[wq->len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800111 break;
112 }
113 case autofs_ptype_expire_multi:
114 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 struct autofs_packet_expire_multi *ep = &pkt.expire_multi;
116
117 pktsz = sizeof(*ep);
118
119 ep->wait_queue_token = wq->wait_queue_token;
120 ep->len = wq->len;
121 memcpy(ep->name, wq->name, wq->len);
122 ep->name[wq->len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800123 break;
124 }
125 /*
126 * Kernel protocol v5 packet for handling indirect and direct
127 * mount missing and expire requests
128 */
129 case autofs_ptype_missing_indirect:
130 case autofs_ptype_expire_indirect:
131 case autofs_ptype_missing_direct:
132 case autofs_ptype_expire_direct:
133 {
134 struct autofs_v5_packet *packet = &pkt.v5_packet;
135
136 pktsz = sizeof(*packet);
137
138 packet->wait_queue_token = wq->wait_queue_token;
139 packet->len = wq->len;
140 memcpy(packet->name, wq->name, wq->len);
141 packet->name[wq->len] = '\0';
142 packet->dev = wq->dev;
143 packet->ino = wq->ino;
144 packet->uid = wq->uid;
145 packet->gid = wq->gid;
146 packet->pid = wq->pid;
147 packet->tgid = wq->tgid;
148 break;
149 }
150 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 printk("autofs4_notify_daemon: bad type %d!\n", type);
152 return;
153 }
154
155 if (autofs4_write(sbi->pipe, &pkt, pktsz))
156 autofs4_catatonic_mode(sbi);
157}
158
159static int autofs4_getpath(struct autofs_sb_info *sbi,
160 struct dentry *dentry, char **name)
161{
162 struct dentry *root = sbi->sb->s_root;
163 struct dentry *tmp;
164 char *buf = *name;
165 char *p;
166 int len = 0;
167
168 spin_lock(&dcache_lock);
169 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
170 len += tmp->d_name.len + 1;
171
172 if (--len > NAME_MAX) {
173 spin_unlock(&dcache_lock);
174 return 0;
175 }
176
177 *(buf + len) = '\0';
178 p = buf + len - dentry->d_name.len;
179 strncpy(p, dentry->d_name.name, dentry->d_name.len);
180
181 for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
182 *(--p) = '/';
183 p -= tmp->d_name.len;
184 strncpy(p, tmp->d_name.name, tmp->d_name.len);
185 }
186 spin_unlock(&dcache_lock);
187
188 return len;
189}
190
Ian Kenta5370552006-05-15 09:43:51 -0700191static struct autofs_wait_queue *
192autofs4_find_wait(struct autofs_sb_info *sbi,
193 char *name, unsigned int hash, unsigned int len)
194{
195 struct autofs_wait_queue *wq;
196
197 for (wq = sbi->queues; wq; wq = wq->next) {
198 if (wq->hash == hash &&
199 wq->len == len &&
200 wq->name && !memcmp(wq->name, name, len))
201 break;
202 }
203 return wq;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
207 enum autofs_notify notify)
208{
Ian Kenta5370552006-05-15 09:43:51 -0700209 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct autofs_wait_queue *wq;
211 char *name;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800212 unsigned int len = 0;
213 unsigned int hash = 0;
Ian Kenta5370552006-05-15 09:43:51 -0700214 int status, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 /* In catatonic mode, we don't wait for nobody */
Ian Kente77fbdd2006-03-27 01:14:49 -0800217 if (sbi->catatonic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return -ENOENT;
219
220 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
221 if (!name)
222 return -ENOMEM;
223
Ian Kent5c0a32f2006-03-27 01:14:55 -0800224 /* If this is a direct mount request create a dummy name */
Ian Kent44d53eb2006-03-27 01:14:56 -0800225 if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT))
Ian Kent5c0a32f2006-03-27 01:14:55 -0800226 len = sprintf(name, "%p", dentry);
227 else {
228 len = autofs4_getpath(sbi, dentry, &name);
229 if (!len) {
230 kfree(name);
231 return -ENOENT;
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Ian Kent5c0a32f2006-03-27 01:14:55 -0800234 hash = full_name_hash(name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800236 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 kfree(name);
238 return -EINTR;
239 }
240
Ian Kenta5370552006-05-15 09:43:51 -0700241 wq = autofs4_find_wait(sbi, name, hash, len);
242 ino = autofs4_dentry_ino(dentry);
243 if (!wq && ino && notify == NFY_NONE) {
244 /*
245 * Either we've betean the pending expire to post it's
246 * wait or it finished while we waited on the mutex.
247 * So we need to wait till either, the wait appears
248 * or the expire finishes.
249 */
250
251 while (ino->flags & AUTOFS_INF_EXPIRING) {
252 mutex_unlock(&sbi->wq_mutex);
253 schedule_timeout_interruptible(HZ/10);
254 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
255 kfree(name);
256 return -EINTR;
257 }
258 wq = autofs4_find_wait(sbi, name, hash, len);
259 if (wq)
260 break;
261 }
262
263 /*
264 * Not ideal but the status has already gone. Of the two
265 * cases where we wait on NFY_NONE neither depend on the
266 * return status of the wait.
267 */
268 if (!wq) {
269 kfree(name);
270 mutex_unlock(&sbi->wq_mutex);
271 return 0;
272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
Ian Kente77fbdd2006-03-27 01:14:49 -0800275 if (!wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 /* Create a new wait queue */
277 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
Ian Kente77fbdd2006-03-27 01:14:49 -0800278 if (!wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 kfree(name);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800280 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return -ENOMEM;
282 }
283
284 wq->wait_queue_token = autofs4_next_wait_queue;
285 if (++autofs4_next_wait_queue == 0)
286 autofs4_next_wait_queue = 1;
287 wq->next = sbi->queues;
288 sbi->queues = wq;
289 init_waitqueue_head(&wq->queue);
Ian Kent5c0a32f2006-03-27 01:14:55 -0800290 wq->hash = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 wq->name = name;
292 wq->len = len;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800293 wq->dev = autofs4_get_dev(sbi);
294 wq->ino = autofs4_get_ino(sbi);
295 wq->uid = current->uid;
296 wq->gid = current->gid;
297 wq->pid = current->pid;
298 wq->tgid = current->tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 wq->status = -EINTR; /* Status return if interrupted */
300 atomic_set(&wq->wait_ctr, 2);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800301 mutex_unlock(&sbi->wq_mutex);
Ian Kent3e7b1912006-03-27 01:14:59 -0800302
Ian Kent5c0a32f2006-03-27 01:14:55 -0800303 if (sbi->version < 5) {
304 if (notify == NFY_MOUNT)
305 type = autofs_ptype_missing;
306 else
307 type = autofs_ptype_expire_multi;
308 } else {
309 if (notify == NFY_MOUNT)
Ian Kent44d53eb2006-03-27 01:14:56 -0800310 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800311 autofs_ptype_missing_direct :
312 autofs_ptype_missing_indirect;
313 else
Ian Kent44d53eb2006-03-27 01:14:56 -0800314 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800315 autofs_ptype_expire_direct :
316 autofs_ptype_expire_indirect;
317 }
Ian Kent4dcd00b2005-05-01 08:59:16 -0700318
Ian Kent682d4fc2005-07-07 17:57:02 -0700319 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
320 (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700321
322 /* autofs4_notify_daemon() may block */
323 autofs4_notify_daemon(sbi, wq, type);
Ian Kenta5370552006-05-15 09:43:51 -0700324 } else {
325 atomic_inc(&wq->wait_ctr);
326 mutex_unlock(&sbi->wq_mutex);
327 kfree(name);
328 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
329 (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700330 }
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* wq->name is NULL if and only if the lock is already released */
333
Ian Kente77fbdd2006-03-27 01:14:49 -0800334 if (sbi->catatonic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* We might have slept, so check again for catatonic mode */
336 wq->status = -ENOENT;
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800337 kfree(wq->name);
338 wq->name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
Ian Kente77fbdd2006-03-27 01:14:49 -0800341 if (wq->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* Block all but "shutdown" signals while waiting */
343 sigset_t oldset;
344 unsigned long irqflags;
345
346 spin_lock_irqsave(&current->sighand->siglock, irqflags);
347 oldset = current->blocked;
348 siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
349 recalc_sigpending();
350 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
351
352 wait_event_interruptible(wq->queue, wq->name == NULL);
353
354 spin_lock_irqsave(&current->sighand->siglock, irqflags);
355 current->blocked = oldset;
356 recalc_sigpending();
357 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
358 } else {
359 DPRINTK("skipped sleeping");
360 }
361
362 status = wq->status;
363
364 /* Are we the last process to need status? */
365 if (atomic_dec_and_test(&wq->wait_ctr))
366 kfree(wq);
367
368 return status;
369}
370
371
372int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
373{
374 struct autofs_wait_queue *wq, **wql;
375
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800376 mutex_lock(&sbi->wq_mutex);
Ian Kente77fbdd2006-03-27 01:14:49 -0800377 for (wql = &sbi->queues ; (wq = *wql) != 0 ; wql = &wq->next) {
378 if (wq->wait_queue_token == wait_queue_token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380 }
381
Ian Kente77fbdd2006-03-27 01:14:49 -0800382 if (!wq) {
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800383 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return -EINVAL;
385 }
386
387 *wql = wq->next; /* Unlink from chain */
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800388 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 kfree(wq->name);
390 wq->name = NULL; /* Do not wait on this queue */
391
392 wq->status = status;
393
394 if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
395 kfree(wq);
396 else
397 wake_up_interruptible(&wq->queue);
398
399 return 0;
400}
401