blob: ce103e7b0bc360f684a2556c55ea9ed8be0e38f2 [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 shrink_dcache_sb(sbi->sb);
49}
50
51static int autofs4_write(struct file *file, const void *addr, int bytes)
52{
53 unsigned long sigpipe, flags;
54 mm_segment_t fs;
55 const char *data = (const char *)addr;
56 ssize_t wr = 0;
57
58 /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
59
60 sigpipe = sigismember(&current->pending.signal, SIGPIPE);
61
62 /* Save pointer to user space and point back to kernel space */
63 fs = get_fs();
64 set_fs(KERNEL_DS);
65
66 while (bytes &&
67 (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) {
68 data += wr;
69 bytes -= wr;
70 }
71
72 set_fs(fs);
73
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(&current->sighand->siglock, flags);
78 sigdelset(&current->pending.signal, SIGPIPE);
79 recalc_sigpending();
80 spin_unlock_irqrestore(&current->sighand->siglock, flags);
81 }
82
83 return (bytes > 0);
84}
85
86static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
87 struct autofs_wait_queue *wq,
88 int type)
89{
90 union autofs_packet_union pkt;
91 size_t pktsz;
92
93 DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
94 wq->wait_queue_token, wq->len, wq->name, type);
95
96 memset(&pkt,0,sizeof pkt); /* For security reasons */
97
98 pkt.hdr.proto_version = sbi->version;
99 pkt.hdr.type = type;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800100 switch (type) {
101 /* Kernel protocol v4 missing and expire packets */
102 case autofs_ptype_missing:
103 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct autofs_packet_missing *mp = &pkt.missing;
105
106 pktsz = sizeof(*mp);
107
108 mp->wait_queue_token = wq->wait_queue_token;
109 mp->len = wq->len;
110 memcpy(mp->name, wq->name, wq->len);
111 mp->name[wq->len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800112 break;
113 }
114 case autofs_ptype_expire_multi:
115 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct autofs_packet_expire_multi *ep = &pkt.expire_multi;
117
118 pktsz = sizeof(*ep);
119
120 ep->wait_queue_token = wq->wait_queue_token;
121 ep->len = wq->len;
122 memcpy(ep->name, wq->name, wq->len);
123 ep->name[wq->len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800124 break;
125 }
126 /*
127 * Kernel protocol v5 packet for handling indirect and direct
128 * mount missing and expire requests
129 */
130 case autofs_ptype_missing_indirect:
131 case autofs_ptype_expire_indirect:
132 case autofs_ptype_missing_direct:
133 case autofs_ptype_expire_direct:
134 {
135 struct autofs_v5_packet *packet = &pkt.v5_packet;
136
137 pktsz = sizeof(*packet);
138
139 packet->wait_queue_token = wq->wait_queue_token;
140 packet->len = wq->len;
141 memcpy(packet->name, wq->name, wq->len);
142 packet->name[wq->len] = '\0';
143 packet->dev = wq->dev;
144 packet->ino = wq->ino;
145 packet->uid = wq->uid;
146 packet->gid = wq->gid;
147 packet->pid = wq->pid;
148 packet->tgid = wq->tgid;
149 break;
150 }
151 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 printk("autofs4_notify_daemon: bad type %d!\n", type);
153 return;
154 }
155
156 if (autofs4_write(sbi->pipe, &pkt, pktsz))
157 autofs4_catatonic_mode(sbi);
158}
159
160static int autofs4_getpath(struct autofs_sb_info *sbi,
161 struct dentry *dentry, char **name)
162{
163 struct dentry *root = sbi->sb->s_root;
164 struct dentry *tmp;
165 char *buf = *name;
166 char *p;
167 int len = 0;
168
169 spin_lock(&dcache_lock);
170 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
171 len += tmp->d_name.len + 1;
172
173 if (--len > NAME_MAX) {
174 spin_unlock(&dcache_lock);
175 return 0;
176 }
177
178 *(buf + len) = '\0';
179 p = buf + len - dentry->d_name.len;
180 strncpy(p, dentry->d_name.name, dentry->d_name.len);
181
182 for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
183 *(--p) = '/';
184 p -= tmp->d_name.len;
185 strncpy(p, tmp->d_name.name, tmp->d_name.len);
186 }
187 spin_unlock(&dcache_lock);
188
189 return len;
190}
191
Ian Kenta5370552006-05-15 09:43:51 -0700192static struct autofs_wait_queue *
193autofs4_find_wait(struct autofs_sb_info *sbi,
194 char *name, unsigned int hash, unsigned int len)
195{
196 struct autofs_wait_queue *wq;
197
198 for (wq = sbi->queues; wq; wq = wq->next) {
199 if (wq->hash == hash &&
200 wq->len == len &&
201 wq->name && !memcmp(wq->name, name, len))
202 break;
203 }
204 return wq;
205}
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
208 enum autofs_notify notify)
209{
Ian Kenta5370552006-05-15 09:43:51 -0700210 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct autofs_wait_queue *wq;
212 char *name;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800213 unsigned int len = 0;
214 unsigned int hash = 0;
Ian Kenta5370552006-05-15 09:43:51 -0700215 int status, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* In catatonic mode, we don't wait for nobody */
Ian Kente77fbdd2006-03-27 01:14:49 -0800218 if (sbi->catatonic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return -ENOENT;
220
221 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
222 if (!name)
223 return -ENOMEM;
224
Ian Kent5c0a32f2006-03-27 01:14:55 -0800225 /* If this is a direct mount request create a dummy name */
Ian Kent44d53eb2006-03-27 01:14:56 -0800226 if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT))
Ian Kent5c0a32f2006-03-27 01:14:55 -0800227 len = sprintf(name, "%p", dentry);
228 else {
229 len = autofs4_getpath(sbi, dentry, &name);
230 if (!len) {
231 kfree(name);
232 return -ENOENT;
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Ian Kent5c0a32f2006-03-27 01:14:55 -0800235 hash = full_name_hash(name, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800237 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 kfree(name);
239 return -EINTR;
240 }
241
Ian Kenta5370552006-05-15 09:43:51 -0700242 wq = autofs4_find_wait(sbi, name, hash, len);
243 ino = autofs4_dentry_ino(dentry);
244 if (!wq && ino && notify == NFY_NONE) {
245 /*
246 * Either we've betean the pending expire to post it's
247 * wait or it finished while we waited on the mutex.
248 * So we need to wait till either, the wait appears
249 * or the expire finishes.
250 */
251
252 while (ino->flags & AUTOFS_INF_EXPIRING) {
253 mutex_unlock(&sbi->wq_mutex);
254 schedule_timeout_interruptible(HZ/10);
255 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
256 kfree(name);
257 return -EINTR;
258 }
259 wq = autofs4_find_wait(sbi, name, hash, len);
260 if (wq)
261 break;
262 }
263
264 /*
265 * Not ideal but the status has already gone. Of the two
266 * cases where we wait on NFY_NONE neither depend on the
267 * return status of the wait.
268 */
269 if (!wq) {
270 kfree(name);
271 mutex_unlock(&sbi->wq_mutex);
272 return 0;
273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
Ian Kente77fbdd2006-03-27 01:14:49 -0800276 if (!wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* Create a new wait queue */
278 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
Ian Kente77fbdd2006-03-27 01:14:49 -0800279 if (!wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 kfree(name);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800281 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -ENOMEM;
283 }
284
285 wq->wait_queue_token = autofs4_next_wait_queue;
286 if (++autofs4_next_wait_queue == 0)
287 autofs4_next_wait_queue = 1;
288 wq->next = sbi->queues;
289 sbi->queues = wq;
290 init_waitqueue_head(&wq->queue);
Ian Kent5c0a32f2006-03-27 01:14:55 -0800291 wq->hash = hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 wq->name = name;
293 wq->len = len;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800294 wq->dev = autofs4_get_dev(sbi);
295 wq->ino = autofs4_get_ino(sbi);
296 wq->uid = current->uid;
297 wq->gid = current->gid;
298 wq->pid = current->pid;
299 wq->tgid = current->tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 wq->status = -EINTR; /* Status return if interrupted */
301 atomic_set(&wq->wait_ctr, 2);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800302 mutex_unlock(&sbi->wq_mutex);
Ian Kent3e7b1912006-03-27 01:14:59 -0800303
Ian Kent5c0a32f2006-03-27 01:14:55 -0800304 if (sbi->version < 5) {
305 if (notify == NFY_MOUNT)
306 type = autofs_ptype_missing;
307 else
308 type = autofs_ptype_expire_multi;
309 } else {
310 if (notify == NFY_MOUNT)
Ian Kent44d53eb2006-03-27 01:14:56 -0800311 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800312 autofs_ptype_missing_direct :
313 autofs_ptype_missing_indirect;
314 else
Ian Kent44d53eb2006-03-27 01:14:56 -0800315 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800316 autofs_ptype_expire_direct :
317 autofs_ptype_expire_indirect;
318 }
Ian Kent4dcd00b2005-05-01 08:59:16 -0700319
Ian Kent682d4fc2005-07-07 17:57:02 -0700320 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
321 (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700322
323 /* autofs4_notify_daemon() may block */
324 autofs4_notify_daemon(sbi, wq, type);
Ian Kenta5370552006-05-15 09:43:51 -0700325 } else {
326 atomic_inc(&wq->wait_ctr);
327 mutex_unlock(&sbi->wq_mutex);
328 kfree(name);
329 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
330 (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700331 }
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* wq->name is NULL if and only if the lock is already released */
334
Ian Kente77fbdd2006-03-27 01:14:49 -0800335 if (sbi->catatonic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* We might have slept, so check again for catatonic mode */
337 wq->status = -ENOENT;
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800338 kfree(wq->name);
339 wq->name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
Ian Kente77fbdd2006-03-27 01:14:49 -0800342 if (wq->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* Block all but "shutdown" signals while waiting */
344 sigset_t oldset;
345 unsigned long irqflags;
346
347 spin_lock_irqsave(&current->sighand->siglock, irqflags);
348 oldset = current->blocked;
349 siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
350 recalc_sigpending();
351 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
352
353 wait_event_interruptible(wq->queue, wq->name == NULL);
354
355 spin_lock_irqsave(&current->sighand->siglock, irqflags);
356 current->blocked = oldset;
357 recalc_sigpending();
358 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
359 } else {
360 DPRINTK("skipped sleeping");
361 }
362
363 status = wq->status;
364
365 /* Are we the last process to need status? */
366 if (atomic_dec_and_test(&wq->wait_ctr))
367 kfree(wq);
368
369 return status;
370}
371
372
373int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
374{
375 struct autofs_wait_queue *wq, **wql;
376
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800377 mutex_lock(&sbi->wq_mutex);
Ian Kente77fbdd2006-03-27 01:14:49 -0800378 for (wql = &sbi->queues ; (wq = *wql) != 0 ; wql = &wq->next) {
379 if (wq->wait_queue_token == wait_queue_token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 break;
381 }
382
Ian Kente77fbdd2006-03-27 01:14:49 -0800383 if (!wq) {
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800384 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return -EINVAL;
386 }
387
388 *wql = wq->next; /* Unlink from chain */
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800389 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 kfree(wq->name);
391 wq->name = NULL; /* Do not wait on this queue */
392
393 wq->status = status;
394
395 if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
396 kfree(wq);
397 else
398 wake_up_interruptible(&wq->queue);
399
400 return 0;
401}
402