blob: 1e4a539f4417c3c94a38c844c6a419dea606d709 [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 }
Ian Kentba8df432006-11-14 02:03:29 -080044 fput(sbi->pipe); /* Close the pipe */
45 sbi->pipe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48static int autofs4_write(struct file *file, const void *addr, int bytes)
49{
50 unsigned long sigpipe, flags;
51 mm_segment_t fs;
52 const char *data = (const char *)addr;
53 ssize_t wr = 0;
54
55 /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
56
57 sigpipe = sigismember(&current->pending.signal, SIGPIPE);
58
59 /* Save pointer to user space and point back to kernel space */
60 fs = get_fs();
61 set_fs(KERNEL_DS);
62
63 while (bytes &&
64 (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) {
65 data += wr;
66 bytes -= wr;
67 }
68
69 set_fs(fs);
70
71 /* Keep the currently executing process from receiving a
72 SIGPIPE unless it was already supposed to get one */
73 if (wr == -EPIPE && !sigpipe) {
74 spin_lock_irqsave(&current->sighand->siglock, flags);
75 sigdelset(&current->pending.signal, SIGPIPE);
76 recalc_sigpending();
77 spin_unlock_irqrestore(&current->sighand->siglock, flags);
78 }
79
80 return (bytes > 0);
81}
82
83static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
84 struct autofs_wait_queue *wq,
85 int type)
86{
87 union autofs_packet_union pkt;
88 size_t pktsz;
89
90 DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
91 wq->wait_queue_token, wq->len, wq->name, type);
92
93 memset(&pkt,0,sizeof pkt); /* For security reasons */
94
95 pkt.hdr.proto_version = sbi->version;
96 pkt.hdr.type = type;
Ian Kent5c0a32f2006-03-27 01:14:55 -080097 switch (type) {
98 /* Kernel protocol v4 missing and expire packets */
99 case autofs_ptype_missing:
100 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct autofs_packet_missing *mp = &pkt.missing;
102
103 pktsz = sizeof(*mp);
104
105 mp->wait_queue_token = wq->wait_queue_token;
106 mp->len = wq->len;
107 memcpy(mp->name, wq->name, wq->len);
108 mp->name[wq->len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800109 break;
110 }
111 case autofs_ptype_expire_multi:
112 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct autofs_packet_expire_multi *ep = &pkt.expire_multi;
114
115 pktsz = sizeof(*ep);
116
117 ep->wait_queue_token = wq->wait_queue_token;
118 ep->len = wq->len;
119 memcpy(ep->name, wq->name, wq->len);
120 ep->name[wq->len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800121 break;
122 }
123 /*
124 * Kernel protocol v5 packet for handling indirect and direct
125 * mount missing and expire requests
126 */
127 case autofs_ptype_missing_indirect:
128 case autofs_ptype_expire_indirect:
129 case autofs_ptype_missing_direct:
130 case autofs_ptype_expire_direct:
131 {
132 struct autofs_v5_packet *packet = &pkt.v5_packet;
133
134 pktsz = sizeof(*packet);
135
136 packet->wait_queue_token = wq->wait_queue_token;
137 packet->len = wq->len;
138 memcpy(packet->name, wq->name, wq->len);
139 packet->name[wq->len] = '\0';
140 packet->dev = wq->dev;
141 packet->ino = wq->ino;
142 packet->uid = wq->uid;
143 packet->gid = wq->gid;
144 packet->pid = wq->pid;
145 packet->tgid = wq->tgid;
146 break;
147 }
148 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 printk("autofs4_notify_daemon: bad type %d!\n", type);
150 return;
151 }
152
153 if (autofs4_write(sbi->pipe, &pkt, pktsz))
154 autofs4_catatonic_mode(sbi);
155}
156
157static int autofs4_getpath(struct autofs_sb_info *sbi,
158 struct dentry *dentry, char **name)
159{
160 struct dentry *root = sbi->sb->s_root;
161 struct dentry *tmp;
162 char *buf = *name;
163 char *p;
164 int len = 0;
165
166 spin_lock(&dcache_lock);
167 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
168 len += tmp->d_name.len + 1;
169
170 if (--len > NAME_MAX) {
171 spin_unlock(&dcache_lock);
172 return 0;
173 }
174
175 *(buf + len) = '\0';
176 p = buf + len - dentry->d_name.len;
177 strncpy(p, dentry->d_name.name, dentry->d_name.len);
178
179 for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
180 *(--p) = '/';
181 p -= tmp->d_name.len;
182 strncpy(p, tmp->d_name.name, tmp->d_name.len);
183 }
184 spin_unlock(&dcache_lock);
185
186 return len;
187}
188
Ian Kenta5370552006-05-15 09:43:51 -0700189static struct autofs_wait_queue *
190autofs4_find_wait(struct autofs_sb_info *sbi,
191 char *name, unsigned int hash, unsigned int len)
192{
193 struct autofs_wait_queue *wq;
194
195 for (wq = sbi->queues; wq; wq = wq->next) {
196 if (wq->hash == hash &&
197 wq->len == len &&
198 wq->name && !memcmp(wq->name, name, len))
199 break;
200 }
201 return wq;
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
205 enum autofs_notify notify)
206{
Ian Kenta5370552006-05-15 09:43:51 -0700207 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 struct autofs_wait_queue *wq;
209 char *name;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800210 unsigned int len = 0;
211 unsigned int hash = 0;
Ian Kenta5370552006-05-15 09:43:51 -0700212 int status, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* In catatonic mode, we don't wait for nobody */
Ian Kente77fbdd2006-03-27 01:14:49 -0800215 if (sbi->catatonic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -ENOENT;
217
218 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
219 if (!name)
220 return -ENOMEM;
221
Ian Kent5c0a32f2006-03-27 01:14:55 -0800222 /* If this is a direct mount request create a dummy name */
Ian Kent44d53eb2006-03-27 01:14:56 -0800223 if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT))
Ian Kent5c0a32f2006-03-27 01:14:55 -0800224 len = sprintf(name, "%p", dentry);
225 else {
226 len = autofs4_getpath(sbi, dentry, &name);
227 if (!len) {
228 kfree(name);
229 return -ENOENT;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Ian Kent5c0a32f2006-03-27 01:14:55 -0800232 hash = full_name_hash(name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800234 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 kfree(name);
236 return -EINTR;
237 }
238
Ian Kenta5370552006-05-15 09:43:51 -0700239 wq = autofs4_find_wait(sbi, name, hash, len);
240 ino = autofs4_dentry_ino(dentry);
241 if (!wq && ino && notify == NFY_NONE) {
242 /*
243 * Either we've betean the pending expire to post it's
244 * wait or it finished while we waited on the mutex.
245 * So we need to wait till either, the wait appears
246 * or the expire finishes.
247 */
248
249 while (ino->flags & AUTOFS_INF_EXPIRING) {
250 mutex_unlock(&sbi->wq_mutex);
251 schedule_timeout_interruptible(HZ/10);
252 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
253 kfree(name);
254 return -EINTR;
255 }
256 wq = autofs4_find_wait(sbi, name, hash, len);
257 if (wq)
258 break;
259 }
260
261 /*
262 * Not ideal but the status has already gone. Of the two
263 * cases where we wait on NFY_NONE neither depend on the
264 * return status of the wait.
265 */
266 if (!wq) {
267 kfree(name);
268 mutex_unlock(&sbi->wq_mutex);
269 return 0;
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
Ian Kente77fbdd2006-03-27 01:14:49 -0800273 if (!wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /* Create a new wait queue */
275 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
Ian Kente77fbdd2006-03-27 01:14:49 -0800276 if (!wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 kfree(name);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800278 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -ENOMEM;
280 }
281
282 wq->wait_queue_token = autofs4_next_wait_queue;
283 if (++autofs4_next_wait_queue == 0)
284 autofs4_next_wait_queue = 1;
285 wq->next = sbi->queues;
286 sbi->queues = wq;
287 init_waitqueue_head(&wq->queue);
Ian Kent5c0a32f2006-03-27 01:14:55 -0800288 wq->hash = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 wq->name = name;
290 wq->len = len;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800291 wq->dev = autofs4_get_dev(sbi);
292 wq->ino = autofs4_get_ino(sbi);
293 wq->uid = current->uid;
294 wq->gid = current->gid;
295 wq->pid = current->pid;
296 wq->tgid = current->tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 wq->status = -EINTR; /* Status return if interrupted */
298 atomic_set(&wq->wait_ctr, 2);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800299 mutex_unlock(&sbi->wq_mutex);
Ian Kent3e7b1912006-03-27 01:14:59 -0800300
Ian Kent5c0a32f2006-03-27 01:14:55 -0800301 if (sbi->version < 5) {
302 if (notify == NFY_MOUNT)
303 type = autofs_ptype_missing;
304 else
305 type = autofs_ptype_expire_multi;
306 } else {
307 if (notify == NFY_MOUNT)
Ian Kent44d53eb2006-03-27 01:14:56 -0800308 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800309 autofs_ptype_missing_direct :
310 autofs_ptype_missing_indirect;
311 else
Ian Kent44d53eb2006-03-27 01:14:56 -0800312 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800313 autofs_ptype_expire_direct :
314 autofs_ptype_expire_indirect;
315 }
Ian Kent4dcd00b2005-05-01 08:59:16 -0700316
Ian Kent682d4fc2005-07-07 17:57:02 -0700317 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
318 (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700319
320 /* autofs4_notify_daemon() may block */
321 autofs4_notify_daemon(sbi, wq, type);
Ian Kenta5370552006-05-15 09:43:51 -0700322 } else {
323 atomic_inc(&wq->wait_ctr);
324 mutex_unlock(&sbi->wq_mutex);
325 kfree(name);
326 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
327 (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700328 }
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /* wq->name is NULL if and only if the lock is already released */
331
Ian Kente77fbdd2006-03-27 01:14:49 -0800332 if (sbi->catatonic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* We might have slept, so check again for catatonic mode */
334 wq->status = -ENOENT;
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800335 kfree(wq->name);
336 wq->name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
Ian Kente77fbdd2006-03-27 01:14:49 -0800339 if (wq->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* Block all but "shutdown" signals while waiting */
341 sigset_t oldset;
342 unsigned long irqflags;
343
344 spin_lock_irqsave(&current->sighand->siglock, irqflags);
345 oldset = current->blocked;
346 siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
347 recalc_sigpending();
348 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
349
350 wait_event_interruptible(wq->queue, wq->name == NULL);
351
352 spin_lock_irqsave(&current->sighand->siglock, irqflags);
353 current->blocked = oldset;
354 recalc_sigpending();
355 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
356 } else {
357 DPRINTK("skipped sleeping");
358 }
359
360 status = wq->status;
361
362 /* Are we the last process to need status? */
363 if (atomic_dec_and_test(&wq->wait_ctr))
364 kfree(wq);
365
366 return status;
367}
368
369
370int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
371{
372 struct autofs_wait_queue *wq, **wql;
373
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800374 mutex_lock(&sbi->wq_mutex);
Ian Kente77fbdd2006-03-27 01:14:49 -0800375 for (wql = &sbi->queues ; (wq = *wql) != 0 ; wql = &wq->next) {
376 if (wq->wait_queue_token == wait_queue_token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
378 }
379
Ian Kente77fbdd2006-03-27 01:14:49 -0800380 if (!wq) {
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800381 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -EINVAL;
383 }
384
385 *wql = wq->next; /* Unlink from chain */
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800386 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 kfree(wq->name);
388 wq->name = NULL; /* Do not wait on this queue */
389
390 wq->status = status;
391
392 if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
393 kfree(wq);
394 else
395 wake_up_interruptible(&wq->queue);
396
397 return 0;
398}
399