]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/autofs4/waitq.c
[PATCH] autofs4: white space cleanup for waitq.c
[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-2003 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         DPRINTK("entering catatonic mode");
32
33         sbi->catatonic = 1;
34         wq = sbi->queues;
35         sbi->queues = NULL;     /* Erase all wait queues */
36         while (wq) {
37                 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         }
48         shrink_dcache_sb(sbi->sb);
49 }
50
51 static 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         
86 static 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;
100         if (type == autofs_ptype_missing) {
101                 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';
109         } else if (type == autofs_ptype_expire_multi) {
110                 struct autofs_packet_expire_multi *ep = &pkt.expire_multi;
111
112                 pktsz = sizeof(*ep);
113
114                 ep->wait_queue_token = wq->wait_queue_token;
115                 ep->len = wq->len;
116                 memcpy(ep->name, wq->name, wq->len);
117                 ep->name[wq->len] = '\0';
118         } else {
119                 printk("autofs4_notify_daemon: bad type %d!\n", type);
120                 return;
121         }
122
123         if (autofs4_write(sbi->pipe, &pkt, pktsz))
124                 autofs4_catatonic_mode(sbi);
125 }
126
127 static int autofs4_getpath(struct autofs_sb_info *sbi,
128                            struct dentry *dentry, char **name)
129 {
130         struct dentry *root = sbi->sb->s_root;
131         struct dentry *tmp;
132         char *buf = *name;
133         char *p;
134         int len = 0;
135
136         spin_lock(&dcache_lock);
137         for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
138                 len += tmp->d_name.len + 1;
139
140         if (--len > NAME_MAX) {
141                 spin_unlock(&dcache_lock);
142                 return 0;
143         }
144
145         *(buf + len) = '\0';
146         p = buf + len - dentry->d_name.len;
147         strncpy(p, dentry->d_name.name, dentry->d_name.len);
148
149         for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
150                 *(--p) = '/';
151                 p -= tmp->d_name.len;
152                 strncpy(p, tmp->d_name.name, tmp->d_name.len);
153         }
154         spin_unlock(&dcache_lock);
155
156         return len;
157 }
158
159 int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
160                 enum autofs_notify notify)
161 {
162         struct autofs_wait_queue *wq;
163         char *name;
164         int len, status;
165
166         /* In catatonic mode, we don't wait for nobody */
167         if (sbi->catatonic)
168                 return -ENOENT;
169         
170         name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
171         if (!name)
172                 return -ENOMEM;
173
174         len = autofs4_getpath(sbi, dentry, &name);
175         if (!len) {
176                 kfree(name);
177                 return -ENOENT;
178         }
179
180         if (mutex_lock_interruptible(&sbi->wq_mutex)) {
181                 kfree(name);
182                 return -EINTR;
183         }
184
185         for (wq = sbi->queues ; wq ; wq = wq->next) {
186                 if (wq->hash == dentry->d_name.hash &&
187                     wq->len == len &&
188                     wq->name && !memcmp(wq->name, name, len))
189                         break;
190         }
191
192         if (!wq) {
193                 /* Can't wait for an expire if there's no mount */
194                 if (notify == NFY_NONE && !d_mountpoint(dentry)) {
195                         kfree(name);
196                         mutex_unlock(&sbi->wq_mutex);
197                         return -ENOENT;
198                 }
199
200                 /* Create a new wait queue */
201                 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
202                 if (!wq) {
203                         kfree(name);
204                         mutex_unlock(&sbi->wq_mutex);
205                         return -ENOMEM;
206                 }
207
208                 wq->wait_queue_token = autofs4_next_wait_queue;
209                 if (++autofs4_next_wait_queue == 0)
210                         autofs4_next_wait_queue = 1;
211                 wq->next = sbi->queues;
212                 sbi->queues = wq;
213                 init_waitqueue_head(&wq->queue);
214                 wq->hash = dentry->d_name.hash;
215                 wq->name = name;
216                 wq->len = len;
217                 wq->status = -EINTR; /* Status return if interrupted */
218                 atomic_set(&wq->wait_ctr, 2);
219                 atomic_set(&wq->notified, 1);
220                 mutex_unlock(&sbi->wq_mutex);
221         } else {
222                 atomic_inc(&wq->wait_ctr);
223                 mutex_unlock(&sbi->wq_mutex);
224                 kfree(name);
225                 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
226                         (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
227         }
228
229         if (notify != NFY_NONE && atomic_dec_and_test(&wq->notified)) {
230                 int type = (notify == NFY_MOUNT ?
231                         autofs_ptype_missing : autofs_ptype_expire_multi);
232
233                 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
234                         (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
235
236                 /* autofs4_notify_daemon() may block */
237                 autofs4_notify_daemon(sbi, wq, type);
238         }
239
240         /* wq->name is NULL if and only if the lock is already released */
241
242         if (sbi->catatonic) {
243                 /* We might have slept, so check again for catatonic mode */
244                 wq->status = -ENOENT;
245                 kfree(wq->name);
246                 wq->name = NULL;
247         }
248
249         if (wq->name) {
250                 /* Block all but "shutdown" signals while waiting */
251                 sigset_t oldset;
252                 unsigned long irqflags;
253
254                 spin_lock_irqsave(&current->sighand->siglock, irqflags);
255                 oldset = current->blocked;
256                 siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
257                 recalc_sigpending();
258                 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
259
260                 wait_event_interruptible(wq->queue, wq->name == NULL);
261
262                 spin_lock_irqsave(&current->sighand->siglock, irqflags);
263                 current->blocked = oldset;
264                 recalc_sigpending();
265                 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
266         } else {
267                 DPRINTK("skipped sleeping");
268         }
269
270         status = wq->status;
271
272         /* Are we the last process to need status? */
273         if (atomic_dec_and_test(&wq->wait_ctr))
274                 kfree(wq);
275
276         return status;
277 }
278
279
280 int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
281 {
282         struct autofs_wait_queue *wq, **wql;
283
284         mutex_lock(&sbi->wq_mutex);
285         for (wql = &sbi->queues ; (wq = *wql) != 0 ; wql = &wq->next) {
286                 if (wq->wait_queue_token == wait_queue_token)
287                         break;
288         }
289
290         if (!wq) {
291                 mutex_unlock(&sbi->wq_mutex);
292                 return -EINVAL;
293         }
294
295         *wql = wq->next;        /* Unlink from chain */
296         mutex_unlock(&sbi->wq_mutex);
297         kfree(wq->name);
298         wq->name = NULL;        /* Do not wait on this queue */
299
300         wq->status = status;
301
302         if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
303                 kfree(wq);
304         else
305                 wake_up_interruptible(&wq->queue);
306
307         return 0;
308 }
309