]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/autofs4/waitq.c
dd2914d7ad7f858f73fd44cf34d2d32354c38556
[linux-2.6-omap-h63xx.git] / fs / autofs4 / waitq.c
1 /* -*- c -*- --------------------------------------------------------------- *
2  *
3  * linux/fs/autofs/waitq.c
4  *
5  *  Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6  *  Copyright 2001-2006 Ian Kent <raven@themaw.net>
7  *
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 */
22 static 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
27 void autofs4_catatonic_mode(struct autofs_sb_info *sbi)
28 {
29         struct autofs_wait_queue *wq, *nwq;
30
31         mutex_lock(&sbi->wq_mutex);
32         if (sbi->catatonic) {
33                 mutex_unlock(&sbi->wq_mutex);
34                 return;
35         }
36
37         DPRINTK("entering catatonic mode");
38
39         sbi->catatonic = 1;
40         wq = sbi->queues;
41         sbi->queues = NULL;     /* Erase all wait queues */
42         while (wq) {
43                 nwq = wq->next;
44                 wq->status = -ENOENT; /* Magic is gone - report failure */
45                 if (wq->name.name) {
46                         kfree(wq->name.name);
47                         wq->name.name = NULL;
48                 }
49                 wake_up_interruptible(&wq->queue);
50                 wq = nwq;
51         }
52         fput(sbi->pipe);        /* Close the pipe */
53         sbi->pipe = NULL;
54         sbi->pipefd = -1;
55         mutex_unlock(&sbi->wq_mutex);
56 }
57
58 static 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         
93 static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
94                                  struct autofs_wait_queue *wq,
95                                  int type)
96 {
97         union {
98                 struct autofs_packet_hdr hdr;
99                 union autofs_packet_union v4_pkt;
100                 union autofs_v5_packet_union v5_pkt;
101         } pkt;
102         struct file *pipe = NULL;
103         size_t pktsz;
104
105         DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
106                 wq->wait_queue_token, wq->name.len, wq->name.name, type);
107
108         memset(&pkt,0,sizeof pkt); /* For security reasons */
109
110         pkt.hdr.proto_version = sbi->version;
111         pkt.hdr.type = type;
112         switch (type) {
113         /* Kernel protocol v4 missing and expire packets */
114         case autofs_ptype_missing:
115         {
116                 struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
117
118                 pktsz = sizeof(*mp);
119
120                 mp->wait_queue_token = wq->wait_queue_token;
121                 mp->len = wq->name.len;
122                 memcpy(mp->name, wq->name.name, wq->name.len);
123                 mp->name[wq->name.len] = '\0';
124                 break;
125         }
126         case autofs_ptype_expire_multi:
127         {
128                 struct autofs_packet_expire_multi *ep = &pkt.v4_pkt.expire_multi;
129
130                 pktsz = sizeof(*ep);
131
132                 ep->wait_queue_token = wq->wait_queue_token;
133                 ep->len = wq->name.len;
134                 memcpy(ep->name, wq->name.name, wq->name.len);
135                 ep->name[wq->name.len] = '\0';
136                 break;
137         }
138         /*
139          * Kernel protocol v5 packet for handling indirect and direct
140          * mount missing and expire requests
141          */
142         case autofs_ptype_missing_indirect:
143         case autofs_ptype_expire_indirect:
144         case autofs_ptype_missing_direct:
145         case autofs_ptype_expire_direct:
146         {
147                 struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
148
149                 pktsz = sizeof(*packet);
150
151                 packet->wait_queue_token = wq->wait_queue_token;
152                 packet->len = wq->name.len;
153                 memcpy(packet->name, wq->name.name, wq->name.len);
154                 packet->name[wq->name.len] = '\0';
155                 packet->dev = wq->dev;
156                 packet->ino = wq->ino;
157                 packet->uid = wq->uid;
158                 packet->gid = wq->gid;
159                 packet->pid = wq->pid;
160                 packet->tgid = wq->tgid;
161                 break;
162         }
163         default:
164                 printk("autofs4_notify_daemon: bad type %d!\n", type);
165                 return;
166         }
167
168         /* Check if we have become catatonic */
169         mutex_lock(&sbi->wq_mutex);
170         if (!sbi->catatonic) {
171                 pipe = sbi->pipe;
172                 get_file(pipe);
173         }
174         mutex_unlock(&sbi->wq_mutex);
175
176         if (pipe) {
177                 if (autofs4_write(pipe, &pkt, pktsz))
178                         autofs4_catatonic_mode(sbi);
179                 fput(pipe);
180         }
181 }
182
183 static int autofs4_getpath(struct autofs_sb_info *sbi,
184                            struct dentry *dentry, char **name)
185 {
186         struct dentry *root = sbi->sb->s_root;
187         struct dentry *tmp;
188         char *buf = *name;
189         char *p;
190         int len = 0;
191
192         spin_lock(&dcache_lock);
193         for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
194                 len += tmp->d_name.len + 1;
195
196         if (!len || --len > NAME_MAX) {
197                 spin_unlock(&dcache_lock);
198                 return 0;
199         }
200
201         *(buf + len) = '\0';
202         p = buf + len - dentry->d_name.len;
203         strncpy(p, dentry->d_name.name, dentry->d_name.len);
204
205         for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
206                 *(--p) = '/';
207                 p -= tmp->d_name.len;
208                 strncpy(p, tmp->d_name.name, tmp->d_name.len);
209         }
210         spin_unlock(&dcache_lock);
211
212         return len;
213 }
214
215 static struct autofs_wait_queue *
216 autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr)
217 {
218         struct autofs_wait_queue *wq;
219
220         for (wq = sbi->queues; wq; wq = wq->next) {
221                 if (wq->name.hash == qstr->hash &&
222                     wq->name.len == qstr->len &&
223                     wq->name.name &&
224                          !memcmp(wq->name.name, qstr->name, qstr->len))
225                         break;
226         }
227         return wq;
228 }
229
230 /*
231  * Check if we have a valid request.
232  * Returns
233  * 1 if the request should continue.
234  *   In this case we can return an autofs_wait_queue entry if one is
235  *   found or NULL to idicate a new wait needs to be created.
236  * 0 or a negative errno if the request shouldn't continue.
237  */
238 static int validate_request(struct autofs_wait_queue **wait,
239                             struct autofs_sb_info *sbi,
240                             struct qstr *qstr,
241                             struct dentry*dentry, enum autofs_notify notify)
242 {
243         struct autofs_wait_queue *wq;
244         struct autofs_info *ino;
245
246         /* Wait in progress, continue; */
247         wq = autofs4_find_wait(sbi, qstr);
248         if (wq) {
249                 *wait = wq;
250                 return 1;
251         }
252
253         *wait = NULL;
254
255         /* If we don't yet have any info this is a new request */
256         ino = autofs4_dentry_ino(dentry);
257         if (!ino)
258                 return 1;
259
260         /*
261          * If we've been asked to wait on an existing expire (NFY_NONE)
262          * but there is no wait in the queue ...
263          */
264         if (notify == NFY_NONE) {
265                 /*
266                  * Either we've betean the pending expire to post it's
267                  * wait or it finished while we waited on the mutex.
268                  * So we need to wait till either, the wait appears
269                  * or the expire finishes.
270                  */
271
272                 while (ino->flags & AUTOFS_INF_EXPIRING) {
273                         mutex_unlock(&sbi->wq_mutex);
274                         schedule_timeout_interruptible(HZ/10);
275                         if (mutex_lock_interruptible(&sbi->wq_mutex))
276                                 return -EINTR;
277
278                         wq = autofs4_find_wait(sbi, qstr);
279                         if (wq) {
280                                 *wait = wq;
281                                 return 1;
282                         }
283                 }
284
285                 /*
286                  * Not ideal but the status has already gone. Of the two
287                  * cases where we wait on NFY_NONE neither depend on the
288                  * return status of the wait.
289                  */
290                 return 0;
291         }
292
293         /*
294          * If we've been asked to trigger a mount and the request
295          * completed while we waited on the mutex ...
296          */
297         if (notify == NFY_MOUNT) {
298                 /*
299                  * If the dentry isn't hashed just go ahead and try the
300                  * mount again with a new wait (not much else we can do).
301                 */
302                 if (!d_unhashed(dentry)) {
303                         /*
304                          * But if the dentry is hashed, that means that we
305                          * got here through the revalidate path.  Thus, we
306                          * need to check if the dentry has been mounted
307                          * while we waited on the wq_mutex. If it has,
308                          * simply return success.
309                          */
310                         if (d_mountpoint(dentry))
311                                 return 0;
312                 }
313         }
314
315         return 1;
316 }
317
318 int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
319                 enum autofs_notify notify)
320 {
321         struct autofs_wait_queue *wq;
322         struct qstr qstr;
323         char *name;
324         int status, ret, type;
325
326         /* In catatonic mode, we don't wait for nobody */
327         if (sbi->catatonic)
328                 return -ENOENT;
329
330         name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
331         if (!name)
332                 return -ENOMEM;
333
334         /* If this is a direct mount request create a dummy name */
335         if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT))
336                 qstr.len = sprintf(name, "%p", dentry);
337         else {
338                 qstr.len = autofs4_getpath(sbi, dentry, &name);
339                 if (!qstr.len) {
340                         kfree(name);
341                         return -ENOENT;
342                 }
343         }
344         qstr.name = name;
345         qstr.hash = full_name_hash(name, qstr.len);
346
347         if (mutex_lock_interruptible(&sbi->wq_mutex)) {
348                 kfree(qstr.name);
349                 return -EINTR;
350         }
351
352         ret = validate_request(&wq, sbi, &qstr, dentry, notify);
353         if (ret <= 0) {
354                 if (ret == 0)
355                         mutex_unlock(&sbi->wq_mutex);
356                 kfree(qstr.name);
357                 return ret;
358         }
359
360         if (!wq) {
361                 /* Create a new wait queue */
362                 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
363                 if (!wq) {
364                         kfree(qstr.name);
365                         mutex_unlock(&sbi->wq_mutex);
366                         return -ENOMEM;
367                 }
368
369                 wq->wait_queue_token = autofs4_next_wait_queue;
370                 if (++autofs4_next_wait_queue == 0)
371                         autofs4_next_wait_queue = 1;
372                 wq->next = sbi->queues;
373                 sbi->queues = wq;
374                 init_waitqueue_head(&wq->queue);
375                 memcpy(&wq->name, &qstr, sizeof(struct qstr));
376                 wq->dev = autofs4_get_dev(sbi);
377                 wq->ino = autofs4_get_ino(sbi);
378                 wq->uid = current->uid;
379                 wq->gid = current->gid;
380                 wq->pid = current->pid;
381                 wq->tgid = current->tgid;
382                 wq->status = -EINTR; /* Status return if interrupted */
383                 atomic_set(&wq->wait_ctr, 2);
384                 mutex_unlock(&sbi->wq_mutex);
385
386                 if (sbi->version < 5) {
387                         if (notify == NFY_MOUNT)
388                                 type = autofs_ptype_missing;
389                         else
390                                 type = autofs_ptype_expire_multi;
391                 } else {
392                         if (notify == NFY_MOUNT)
393                                 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
394                                         autofs_ptype_missing_direct :
395                                          autofs_ptype_missing_indirect;
396                         else
397                                 type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
398                                         autofs_ptype_expire_direct :
399                                         autofs_ptype_expire_indirect;
400                 }
401
402                 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
403                         (unsigned long) wq->wait_queue_token, wq->name.len,
404                         wq->name.name, notify);
405
406                 /* autofs4_notify_daemon() may block */
407                 autofs4_notify_daemon(sbi, wq, type);
408         } else {
409                 atomic_inc(&wq->wait_ctr);
410                 mutex_unlock(&sbi->wq_mutex);
411                 kfree(qstr.name);
412                 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
413                         (unsigned long) wq->wait_queue_token, wq->name.len,
414                         wq->name.name, notify);
415         }
416
417         /*
418          * wq->name.name is NULL iff the lock is already released
419          * or the mount has been made catatonic.
420          */
421         if (wq->name.name) {
422                 /* Block all but "shutdown" signals while waiting */
423                 sigset_t oldset;
424                 unsigned long irqflags;
425
426                 spin_lock_irqsave(&current->sighand->siglock, irqflags);
427                 oldset = current->blocked;
428                 siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
429                 recalc_sigpending();
430                 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
431
432                 wait_event_interruptible(wq->queue, wq->name.name == NULL);
433
434                 spin_lock_irqsave(&current->sighand->siglock, irqflags);
435                 current->blocked = oldset;
436                 recalc_sigpending();
437                 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
438         } else {
439                 DPRINTK("skipped sleeping");
440         }
441
442         status = wq->status;
443
444         /* Are we the last process to need status? */
445         if (atomic_dec_and_test(&wq->wait_ctr))
446                 kfree(wq);
447
448         return status;
449 }
450
451
452 int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
453 {
454         struct autofs_wait_queue *wq, **wql;
455
456         mutex_lock(&sbi->wq_mutex);
457         for (wql = &sbi->queues; (wq = *wql) != NULL; wql = &wq->next) {
458                 if (wq->wait_queue_token == wait_queue_token)
459                         break;
460         }
461
462         if (!wq) {
463                 mutex_unlock(&sbi->wq_mutex);
464                 return -EINVAL;
465         }
466
467         *wql = wq->next;        /* Unlink from chain */
468         kfree(wq->name.name);
469         wq->name.name = NULL;   /* Do not wait on this queue */
470         mutex_unlock(&sbi->wq_mutex);
471
472         wq->status = status;
473
474         if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
475                 kfree(wq);
476         else
477                 wake_up_interruptible(&wq->queue);
478
479         return 0;
480 }
481