blob: 55aac10cf328e43790dd560a44a0b554ab76d192 [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
Ian Kent5a11d4d2008-07-23 21:30:17 -070031 mutex_lock(&sbi->wq_mutex);
32 if (sbi->catatonic) {
33 mutex_unlock(&sbi->wq_mutex);
34 return;
35 }
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 DPRINTK("entering catatonic mode");
38
39 sbi->catatonic = 1;
40 wq = sbi->queues;
41 sbi->queues = NULL; /* Erase all wait queues */
Ian Kente77fbdd2006-03-27 01:14:49 -080042 while (wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 nwq = wq->next;
44 wq->status = -ENOENT; /* Magic is gone - report failure */
Jeff Moyer70b52a02008-07-23 21:30:16 -070045 if (wq->name.name) {
46 kfree(wq->name.name);
47 wq->name.name = NULL;
48 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 wake_up_interruptible(&wq->queue);
50 wq = nwq;
51 }
Ian Kentba8df432006-11-14 02:03:29 -080052 fput(sbi->pipe); /* Close the pipe */
53 sbi->pipe = NULL;
Ian Kent5a11d4d2008-07-23 21:30:17 -070054 sbi->pipefd = -1;
55 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58static int autofs4_write(struct file *file, const void *addr, int bytes)
59{
60 unsigned long sigpipe, flags;
61 mm_segment_t fs;
62 const char *data = (const char *)addr;
63 ssize_t wr = 0;
64
65 /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
66
67 sigpipe = sigismember(&current->pending.signal, SIGPIPE);
68
69 /* Save pointer to user space and point back to kernel space */
70 fs = get_fs();
71 set_fs(KERNEL_DS);
72
73 while (bytes &&
74 (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) {
75 data += wr;
76 bytes -= wr;
77 }
78
79 set_fs(fs);
80
81 /* Keep the currently executing process from receiving a
82 SIGPIPE unless it was already supposed to get one */
83 if (wr == -EPIPE && !sigpipe) {
84 spin_lock_irqsave(&current->sighand->siglock, flags);
85 sigdelset(&current->pending.signal, SIGPIPE);
86 recalc_sigpending();
87 spin_unlock_irqrestore(&current->sighand->siglock, flags);
88 }
89
90 return (bytes > 0);
91}
92
93static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
94 struct autofs_wait_queue *wq,
95 int type)
96{
Ian Kente8514472007-02-20 13:58:09 -080097 union {
98 struct autofs_packet_hdr hdr;
99 union autofs_packet_union v4_pkt;
100 union autofs_v5_packet_union v5_pkt;
101 } pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 size_t pktsz;
103
104 DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
Jeff Moyer70b52a02008-07-23 21:30:16 -0700105 wq->wait_queue_token, wq->name.len, wq->name.name, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 memset(&pkt,0,sizeof pkt); /* For security reasons */
108
109 pkt.hdr.proto_version = sbi->version;
110 pkt.hdr.type = type;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800111 switch (type) {
112 /* Kernel protocol v4 missing and expire packets */
113 case autofs_ptype_missing:
114 {
Ian Kente8514472007-02-20 13:58:09 -0800115 struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 pktsz = sizeof(*mp);
118
119 mp->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700120 mp->len = wq->name.len;
121 memcpy(mp->name, wq->name.name, wq->name.len);
122 mp->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800123 break;
124 }
125 case autofs_ptype_expire_multi:
126 {
Ian Kente8514472007-02-20 13:58:09 -0800127 struct autofs_packet_expire_multi *ep = &pkt.v4_pkt.expire_multi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 pktsz = sizeof(*ep);
130
131 ep->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700132 ep->len = wq->name.len;
133 memcpy(ep->name, wq->name.name, wq->name.len);
134 ep->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800135 break;
136 }
137 /*
138 * Kernel protocol v5 packet for handling indirect and direct
139 * mount missing and expire requests
140 */
141 case autofs_ptype_missing_indirect:
142 case autofs_ptype_expire_indirect:
143 case autofs_ptype_missing_direct:
144 case autofs_ptype_expire_direct:
145 {
Ian Kente8514472007-02-20 13:58:09 -0800146 struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
Ian Kent5c0a32f2006-03-27 01:14:55 -0800147
148 pktsz = sizeof(*packet);
149
150 packet->wait_queue_token = wq->wait_queue_token;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700151 packet->len = wq->name.len;
152 memcpy(packet->name, wq->name.name, wq->name.len);
153 packet->name[wq->name.len] = '\0';
Ian Kent5c0a32f2006-03-27 01:14:55 -0800154 packet->dev = wq->dev;
155 packet->ino = wq->ino;
156 packet->uid = wq->uid;
157 packet->gid = wq->gid;
158 packet->pid = wq->pid;
159 packet->tgid = wq->tgid;
160 break;
161 }
162 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 printk("autofs4_notify_daemon: bad type %d!\n", type);
164 return;
165 }
166
167 if (autofs4_write(sbi->pipe, &pkt, pktsz))
168 autofs4_catatonic_mode(sbi);
169}
170
171static int autofs4_getpath(struct autofs_sb_info *sbi,
172 struct dentry *dentry, char **name)
173{
174 struct dentry *root = sbi->sb->s_root;
175 struct dentry *tmp;
176 char *buf = *name;
177 char *p;
178 int len = 0;
179
180 spin_lock(&dcache_lock);
181 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
182 len += tmp->d_name.len + 1;
183
Ian Kentcab09362008-05-01 04:35:07 -0700184 if (!len || --len > NAME_MAX) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 spin_unlock(&dcache_lock);
186 return 0;
187 }
188
189 *(buf + len) = '\0';
190 p = buf + len - dentry->d_name.len;
191 strncpy(p, dentry->d_name.name, dentry->d_name.len);
192
193 for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
194 *(--p) = '/';
195 p -= tmp->d_name.len;
196 strncpy(p, tmp->d_name.name, tmp->d_name.len);
197 }
198 spin_unlock(&dcache_lock);
199
200 return len;
201}
202
Ian Kenta5370552006-05-15 09:43:51 -0700203static struct autofs_wait_queue *
Jeff Moyer70b52a02008-07-23 21:30:16 -0700204autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr)
Ian Kenta5370552006-05-15 09:43:51 -0700205{
206 struct autofs_wait_queue *wq;
207
208 for (wq = sbi->queues; wq; wq = wq->next) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700209 if (wq->name.hash == qstr->hash &&
210 wq->name.len == qstr->len &&
211 wq->name.name &&
212 !memcmp(wq->name.name, qstr->name, qstr->len))
Ian Kenta5370552006-05-15 09:43:51 -0700213 break;
214 }
215 return wq;
216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
219 enum autofs_notify notify)
220{
Ian Kenta5370552006-05-15 09:43:51 -0700221 struct autofs_info *ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct autofs_wait_queue *wq;
Jeff Moyer70b52a02008-07-23 21:30:16 -0700223 struct qstr qstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 char *name;
Ian Kenta5370552006-05-15 09:43:51 -0700225 int status, type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* In catatonic mode, we don't wait for nobody */
Ian Kente77fbdd2006-03-27 01:14:49 -0800228 if (sbi->catatonic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return -ENOENT;
230
231 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
232 if (!name)
233 return -ENOMEM;
234
Ian Kent5c0a32f2006-03-27 01:14:55 -0800235 /* If this is a direct mount request create a dummy name */
Ian Kent44d53eb2006-03-27 01:14:56 -0800236 if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT))
Jeff Moyer70b52a02008-07-23 21:30:16 -0700237 qstr.len = sprintf(name, "%p", dentry);
Ian Kent5c0a32f2006-03-27 01:14:55 -0800238 else {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700239 qstr.len = autofs4_getpath(sbi, dentry, &name);
240 if (!qstr.len) {
Ian Kent5c0a32f2006-03-27 01:14:55 -0800241 kfree(name);
242 return -ENOENT;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
Jeff Moyer70b52a02008-07-23 21:30:16 -0700245 qstr.name = name;
246 qstr.hash = full_name_hash(name, qstr.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800248 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700249 kfree(qstr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return -EINTR;
251 }
252
Jeff Moyer70b52a02008-07-23 21:30:16 -0700253 wq = autofs4_find_wait(sbi, &qstr);
Ian Kenta5370552006-05-15 09:43:51 -0700254 ino = autofs4_dentry_ino(dentry);
255 if (!wq && ino && notify == NFY_NONE) {
256 /*
257 * Either we've betean the pending expire to post it's
258 * wait or it finished while we waited on the mutex.
259 * So we need to wait till either, the wait appears
260 * or the expire finishes.
261 */
262
263 while (ino->flags & AUTOFS_INF_EXPIRING) {
264 mutex_unlock(&sbi->wq_mutex);
265 schedule_timeout_interruptible(HZ/10);
266 if (mutex_lock_interruptible(&sbi->wq_mutex)) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700267 kfree(qstr.name);
Ian Kenta5370552006-05-15 09:43:51 -0700268 return -EINTR;
269 }
Jeff Moyer70b52a02008-07-23 21:30:16 -0700270 wq = autofs4_find_wait(sbi, &qstr);
Ian Kenta5370552006-05-15 09:43:51 -0700271 if (wq)
272 break;
273 }
274
275 /*
276 * Not ideal but the status has already gone. Of the two
277 * cases where we wait on NFY_NONE neither depend on the
278 * return status of the wait.
279 */
280 if (!wq) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700281 kfree(qstr.name);
Ian Kenta5370552006-05-15 09:43:51 -0700282 mutex_unlock(&sbi->wq_mutex);
283 return 0;
284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Ian Kente77fbdd2006-03-27 01:14:49 -0800287 if (!wq) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* Create a new wait queue */
289 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
Ian Kente77fbdd2006-03-27 01:14:49 -0800290 if (!wq) {
Jeff Moyer70b52a02008-07-23 21:30:16 -0700291 kfree(qstr.name);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800292 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return -ENOMEM;
294 }
295
296 wq->wait_queue_token = autofs4_next_wait_queue;
297 if (++autofs4_next_wait_queue == 0)
298 autofs4_next_wait_queue = 1;
299 wq->next = sbi->queues;
300 sbi->queues = wq;
301 init_waitqueue_head(&wq->queue);
Jeff Moyer70b52a02008-07-23 21:30:16 -0700302 memcpy(&wq->name, &qstr, sizeof(struct qstr));
Ian Kent5c0a32f2006-03-27 01:14:55 -0800303 wq->dev = autofs4_get_dev(sbi);
304 wq->ino = autofs4_get_ino(sbi);
305 wq->uid = current->uid;
306 wq->gid = current->gid;
307 wq->pid = current->pid;
308 wq->tgid = current->tgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 wq->status = -EINTR; /* Status return if interrupted */
310 atomic_set(&wq->wait_ctr, 2);
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800311 mutex_unlock(&sbi->wq_mutex);
Ian Kent3e7b1912006-03-27 01:14:59 -0800312
Ian Kent5c0a32f2006-03-27 01:14:55 -0800313 if (sbi->version < 5) {
314 if (notify == NFY_MOUNT)
315 type = autofs_ptype_missing;
316 else
317 type = autofs_ptype_expire_multi;
318 } else {
319 if (notify == NFY_MOUNT)
Ian Kent44d53eb2006-03-27 01:14:56 -0800320 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800321 autofs_ptype_missing_direct :
322 autofs_ptype_missing_indirect;
323 else
Ian Kent44d53eb2006-03-27 01:14:56 -0800324 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
Ian Kent5c0a32f2006-03-27 01:14:55 -0800325 autofs_ptype_expire_direct :
326 autofs_ptype_expire_indirect;
327 }
Ian Kent4dcd00b2005-05-01 08:59:16 -0700328
Ian Kent682d4fc2005-07-07 17:57:02 -0700329 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
Jeff Moyer70b52a02008-07-23 21:30:16 -0700330 (unsigned long) wq->wait_queue_token, wq->name.len,
331 wq->name.name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700332
333 /* autofs4_notify_daemon() may block */
334 autofs4_notify_daemon(sbi, wq, type);
Ian Kenta5370552006-05-15 09:43:51 -0700335 } else {
336 atomic_inc(&wq->wait_ctr);
337 mutex_unlock(&sbi->wq_mutex);
Jeff Moyer70b52a02008-07-23 21:30:16 -0700338 kfree(qstr.name);
Ian Kenta5370552006-05-15 09:43:51 -0700339 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
Jeff Moyer70b52a02008-07-23 21:30:16 -0700340 (unsigned long) wq->wait_queue_token, wq->name.len,
341 wq->name.name, notify);
Ian Kent4dcd00b2005-05-01 08:59:16 -0700342 }
343
Ian Kent5a11d4d2008-07-23 21:30:17 -0700344 /*
345 * wq->name.name is NULL iff the lock is already released
346 * or the mount has been made catatonic.
347 */
Jeff Moyer70b52a02008-07-23 21:30:16 -0700348 if (wq->name.name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Block all but "shutdown" signals while waiting */
350 sigset_t oldset;
351 unsigned long irqflags;
352
353 spin_lock_irqsave(&current->sighand->siglock, irqflags);
354 oldset = current->blocked;
355 siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
356 recalc_sigpending();
357 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
358
Jeff Moyer70b52a02008-07-23 21:30:16 -0700359 wait_event_interruptible(wq->queue, wq->name.name == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 spin_lock_irqsave(&current->sighand->siglock, irqflags);
362 current->blocked = oldset;
363 recalc_sigpending();
364 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
365 } else {
366 DPRINTK("skipped sleeping");
367 }
368
369 status = wq->status;
370
371 /* Are we the last process to need status? */
372 if (atomic_dec_and_test(&wq->wait_ctr))
373 kfree(wq);
374
375 return status;
376}
377
378
379int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
380{
381 struct autofs_wait_queue *wq, **wql;
382
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800383 mutex_lock(&sbi->wq_mutex);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -0700384 for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
Ian Kente77fbdd2006-03-27 01:14:49 -0800385 if (wq->wait_queue_token == wait_queue_token)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 break;
387 }
388
Ian Kente77fbdd2006-03-27 01:14:49 -0800389 if (!wq) {
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800390 mutex_unlock(&sbi->wq_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -EINVAL;
392 }
393
394 *wql = wq->next; /* Unlink from chain */
Ingo Molnar1d5599e2006-03-23 03:00:41 -0800395 mutex_unlock(&sbi->wq_mutex);
Jeff Moyer70b52a02008-07-23 21:30:16 -0700396 kfree(wq->name.name);
397 wq->name.name = NULL; /* Do not wait on this queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 wq->status = status;
400
401 if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
402 kfree(wq);
403 else
404 wake_up_interruptible(&wq->queue);
405
406 return 0;
407}
408