Debian Codegraph @

patches/autofs4-2.6.20-v5-update-20090903.patch

raw
1 --- linux-2.6.20.orig/fs/autofs4/waitq.c
2 +++ linux-2.6.20/fs/autofs4/waitq.c
3 @@ -28,6 +28,12 @@ void autofs4_catatonic_mode(struct autof
4 {
5 struct autofs_wait_queue *wq, *nwq;
6
7 + mutex_lock(&sbi->wq_mutex);
8 + if (sbi->catatonic) {
9 + mutex_unlock(&sbi->wq_mutex);
10 + return;
11 + }
12 +
13 DPRINTK("entering catatonic mode");
14
15 sbi->catatonic = 1;
16 @@ -36,13 +42,18 @@ void autofs4_catatonic_mode(struct autof
17 while (wq) {
18 nwq = wq->next;
19 wq->status = -ENOENT; /* Magic is gone - report failure */
20 - kfree(wq->name);
21 - wq->name = NULL;
22 + if (wq->name.name) {
23 + kfree(wq->name.name);
24 + wq->name.name = NULL;
25 + }
26 + wq->wait_ctr--;
27 wake_up_interruptible(&wq->queue);
28 wq = nwq;
29 }
30 fput(sbi->pipe); /* Close the pipe */
31 sbi->pipe = NULL;
32 + sbi->pipefd = -1;
33 + mutex_unlock(&sbi->wq_mutex);
34 }
35
36 static int autofs4_write(struct file *file, const void *addr, int bytes)
37 @@ -84,11 +95,16 @@ static void autofs4_notify_daemon(struct
38 struct autofs_wait_queue *wq,
39 int type)
40 {
41 - union autofs_packet_union pkt;
42 + union {
43 + struct autofs_packet_hdr hdr;
44 + union autofs_packet_union v4_pkt;
45 + union autofs_v5_packet_union v5_pkt;
46 + } pkt;
47 + struct file *pipe = NULL;
48 size_t pktsz;
49
50 DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
51 - wq->wait_queue_token, wq->len, wq->name, type);
52 + wq->wait_queue_token, wq->name.len, wq->name.name, type);
53
54 memset(&pkt,0,sizeof pkt); /* For security reasons */
55
56 @@ -98,26 +114,26 @@ static void autofs4_notify_daemon(struct
57 /* Kernel protocol v4 missing and expire packets */
58 case autofs_ptype_missing:
59 {
60 - struct autofs_packet_missing *mp = &pkt.missing;
61 + struct autofs_packet_missing *mp = &pkt.v4_pkt.missing;
62
63 pktsz = sizeof(*mp);
64
65 mp->wait_queue_token = wq->wait_queue_token;
66 - mp->len = wq->len;
67 - memcpy(mp->name, wq->name, wq->len);
68 - mp->name[wq->len] = '\0';
69 + mp->len = wq->name.len;
70 + memcpy(mp->name, wq->name.name, wq->name.len);
71 + mp->name[wq->name.len] = '\0';
72 break;
73 }
74 case autofs_ptype_expire_multi:
75 {
76 - struct autofs_packet_expire_multi *ep = &pkt.expire_multi;
77 + struct autofs_packet_expire_multi *ep = &pkt.v4_pkt.expire_multi;
78
79 pktsz = sizeof(*ep);
80
81 ep->wait_queue_token = wq->wait_queue_token;
82 - ep->len = wq->len;
83 - memcpy(ep->name, wq->name, wq->len);
84 - ep->name[wq->len] = '\0';
85 + ep->len = wq->name.len;
86 + memcpy(ep->name, wq->name.name, wq->name.len);
87 + ep->name[wq->name.len] = '\0';
88 break;
89 }
90 /*
91 @@ -129,14 +145,14 @@ static void autofs4_notify_daemon(struct
92 case autofs_ptype_missing_direct:
93 case autofs_ptype_expire_direct:
94 {
95 - struct autofs_v5_packet *packet = &pkt.v5_packet;
96 + struct autofs_v5_packet *packet = &pkt.v5_pkt.v5_packet;
97
98 pktsz = sizeof(*packet);
99
100 packet->wait_queue_token = wq->wait_queue_token;
101 - packet->len = wq->len;
102 - memcpy(packet->name, wq->name, wq->len);
103 - packet->name[wq->len] = '\0';
104 + packet->len = wq->name.len;
105 + memcpy(packet->name, wq->name.name, wq->name.len);
106 + packet->name[wq->name.len] = '\0';
107 packet->dev = wq->dev;
108 packet->ino = wq->ino;
109 packet->uid = wq->uid;
110 @@ -150,8 +166,19 @@ static void autofs4_notify_daemon(struct
111 return;
112 }
113
114 - if (autofs4_write(sbi->pipe, &pkt, pktsz))
115 - autofs4_catatonic_mode(sbi);
116 + /* Check if we have become catatonic */
117 + mutex_lock(&sbi->wq_mutex);
118 + if (!sbi->catatonic) {
119 + pipe = sbi->pipe;
120 + get_file(pipe);
121 + }
122 + mutex_unlock(&sbi->wq_mutex);
123 +
124 + if (pipe) {
125 + if (autofs4_write(pipe, &pkt, pktsz))
126 + autofs4_catatonic_mode(sbi);
127 + fput(pipe);
128 + }
129 }
130
131 static int autofs4_getpath(struct autofs_sb_info *sbi,
132 @@ -167,7 +194,7 @@ static int autofs4_getpath(struct autofs
133 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
134 len += tmp->d_name.len + 1;
135
136 - if (--len > NAME_MAX) {
137 + if (!len || --len > NAME_MAX) {
138 spin_unlock(&dcache_lock);
139 return 0;
140 }
141 @@ -187,58 +214,55 @@ static int autofs4_getpath(struct autofs
142 }
143
144 static struct autofs_wait_queue *
145 -autofs4_find_wait(struct autofs_sb_info *sbi,
146 - char *name, unsigned int hash, unsigned int len)
147 +autofs4_find_wait(struct autofs_sb_info *sbi, struct qstr *qstr)
148 {
149 struct autofs_wait_queue *wq;
150
151 for (wq = sbi->queues; wq; wq = wq->next) {
152 - if (wq->hash == hash &&
153 - wq->len == len &&
154 - wq->name && !memcmp(wq->name, name, len))
155 + if (wq->name.hash == qstr->hash &&
156 + wq->name.len == qstr->len &&
157 + wq->name.name &&
158 + !memcmp(wq->name.name, qstr->name, qstr->len))
159 break;
160 }
161 return wq;
162 }
163
164 -int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
165 - enum autofs_notify notify)
166 +/*
167 + * Check if we have a valid request.
168 + * Returns
169 + * 1 if the request should continue.
170 + * In this case we can return an autofs_wait_queue entry if one is
171 + * found or NULL to idicate a new wait needs to be created.
172 + * 0 or a negative errno if the request shouldn't continue.
173 + */
174 +static int validate_request(struct autofs_wait_queue **wait,
175 + struct autofs_sb_info *sbi,
176 + struct qstr *qstr,
177 + struct dentry*dentry, enum autofs_notify notify)
178 {
179 - struct autofs_info *ino;
180 struct autofs_wait_queue *wq;
181 - char *name;
182 - unsigned int len = 0;
183 - unsigned int hash = 0;
184 - int status, type;
185 -
186 - /* In catatonic mode, we don't wait for nobody */
187 - if (sbi->catatonic)
188 - return -ENOENT;
189 -
190 - name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
191 - if (!name)
192 - return -ENOMEM;
193 + struct autofs_info *ino;
194
195 - /* If this is a direct mount request create a dummy name */
196 - if (IS_ROOT(dentry) && (sbi->type & AUTOFS_TYPE_DIRECT))
197 - len = sprintf(name, "%p", dentry);
198 - else {
199 - len = autofs4_getpath(sbi, dentry, &name);
200 - if (!len) {
201 - kfree(name);
202 - return -ENOENT;
203 - }
204 + /* Wait in progress, continue; */
205 + wq = autofs4_find_wait(sbi, qstr);
206 + if (wq) {
207 + *wait = wq;
208 + return 1;
209 }
210 - hash = full_name_hash(name, len);
211
212 - if (mutex_lock_interruptible(&sbi->wq_mutex)) {
213 - kfree(name);
214 - return -EINTR;
215 - }
216 + *wait = NULL;
217
218 - wq = autofs4_find_wait(sbi, name, hash, len);
219 + /* If we don't yet have any info this is a new request */
220 ino = autofs4_dentry_ino(dentry);
221 - if (!wq && ino && notify == NFY_NONE) {
222 + if (!ino)
223 + return 1;
224 +
225 + /*
226 + * If we've been asked to wait on an existing expire (NFY_NONE)
227 + * but there is no wait in the queue ...
228 + */
229 + if (notify == NFY_NONE) {
230 /*
231 * Either we've betean the pending expire to post it's
232 * wait or it finished while we waited on the mutex.
233 @@ -249,13 +273,14 @@ int autofs4_wait(struct autofs_sb_info *
234 while (ino->flags & AUTOFS_INF_EXPIRING) {
235 mutex_unlock(&sbi->wq_mutex);
236 schedule_timeout_interruptible(HZ/10);
237 - if (mutex_lock_interruptible(&sbi->wq_mutex)) {
238 - kfree(name);
239 + if (mutex_lock_interruptible(&sbi->wq_mutex))
240 return -EINTR;
241 +
242 + wq = autofs4_find_wait(sbi, qstr);
243 + if (wq) {
244 + *wait = wq;
245 + return 1;
246 }
247 - wq = autofs4_find_wait(sbi, name, hash, len);
248 - if (wq)
249 - break;
250 }
251
252 /*
253 @@ -263,18 +288,90 @@ int autofs4_wait(struct autofs_sb_info *
254 * cases where we wait on NFY_NONE neither depend on the
255 * return status of the wait.
256 */
257 - if (!wq) {
258 - kfree(name);
259 - mutex_unlock(&sbi->wq_mutex);
260 + return 0;
261 + }
262 +
263 + /*
264 + * If we've been asked to trigger a mount and the request
265 + * completed while we waited on the mutex ...
266 + */
267 + if (notify == NFY_MOUNT) {
268 + /*
269 + * If the dentry was successfully mounted while we slept
270 + * on the wait queue mutex we can return success. If it
271 + * isn't mounted (doesn't have submounts for the case of
272 + * a multi-mount with no mount at it's base) we can
273 + * continue on and create a new request.
274 + */
275 + if (have_submounts(dentry))
276 return 0;
277 + }
278 +
279 + return 1;
280 +}
281 +
282 +int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
283 + enum autofs_notify notify)
284 +{
285 + struct autofs_wait_queue *wq;
286 + struct qstr qstr;
287 + char *name;
288 + int status, ret, type;
289 +
290 + /* In catatonic mode, we don't wait for nobody */
291 + if (sbi->catatonic)
292 + return -ENOENT;
293 +
294 + if (!dentry->d_inode) {
295 + /*
296 + * A wait for a negative dentry is invalid for certain
297 + * cases. A direct or offset mount "always" has its mount
298 + * point directory created and so the request dentry must
299 + * be positive or the map key doesn't exist. The situation
300 + * is very similar for indirect mounts except only dentrys
301 + * in the root of the autofs file system may be negative.
302 + */
303 + if (autofs_type_trigger(sbi->type))
304 + return -ENOENT;
305 + else if (!IS_ROOT(dentry->d_parent))
306 + return -ENOENT;
307 + }
308 +
309 + name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
310 + if (!name)
311 + return -ENOMEM;
312 +
313 + /* If this is a direct mount request create a dummy name */
314 + if (IS_ROOT(dentry) && autofs_type_trigger(sbi->type))
315 + qstr.len = sprintf(name, "%p", dentry);
316 + else {
317 + qstr.len = autofs4_getpath(sbi, dentry, &name);
318 + if (!qstr.len) {
319 + kfree(name);
320 + return -ENOENT;
321 }
322 }
323 + qstr.name = name;
324 + qstr.hash = full_name_hash(name, qstr.len);
325 +
326 + if (mutex_lock_interruptible(&sbi->wq_mutex)) {
327 + kfree(qstr.name);
328 + return -EINTR;
329 + }
330 +
331 + ret = validate_request(&wq, sbi, &qstr, dentry, notify);
332 + if (ret <= 0) {
333 + if (ret == 0)
334 + mutex_unlock(&sbi->wq_mutex);
335 + kfree(qstr.name);
336 + return ret;
337 + }
338
339 if (!wq) {
340 /* Create a new wait queue */
341 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
342 if (!wq) {
343 - kfree(name);
344 + kfree(qstr.name);
345 mutex_unlock(&sbi->wq_mutex);
346 return -ENOMEM;
347 }
348 @@ -285,9 +382,7 @@ int autofs4_wait(struct autofs_sb_info *
349 wq->next = sbi->queues;
350 sbi->queues = wq;
351 init_waitqueue_head(&wq->queue);
352 - wq->hash = hash;
353 - wq->name = name;
354 - wq->len = len;
355 + memcpy(&wq->name, &qstr, sizeof(struct qstr));
356 wq->dev = autofs4_get_dev(sbi);
357 wq->ino = autofs4_get_ino(sbi);
358 wq->uid = current->uid;
359 @@ -295,7 +390,7 @@ int autofs4_wait(struct autofs_sb_info *
360 wq->pid = current->pid;
361 wq->tgid = current->tgid;
362 wq->status = -EINTR; /* Status return if interrupted */
363 - atomic_set(&wq->wait_ctr, 2);
364 + wq->wait_ctr = 2;
365 mutex_unlock(&sbi->wq_mutex);
366
367 if (sbi->version < 5) {
368 @@ -305,38 +400,35 @@ int autofs4_wait(struct autofs_sb_info *
369 type = autofs_ptype_expire_multi;
370 } else {
371 if (notify == NFY_MOUNT)
372 - type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
373 + type = autofs_type_trigger(sbi->type) ?
374 autofs_ptype_missing_direct :
375 autofs_ptype_missing_indirect;
376 else
377 - type = (sbi->type & AUTOFS_TYPE_DIRECT) ?
378 + type = autofs_type_trigger(sbi->type) ?
379 autofs_ptype_expire_direct :
380 autofs_ptype_expire_indirect;
381 }
382
383 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d\n",
384 - (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
385 + (unsigned long) wq->wait_queue_token, wq->name.len,
386 + wq->name.name, notify);
387
388 /* autofs4_notify_daemon() may block */
389 autofs4_notify_daemon(sbi, wq, type);
390 } else {
391 - atomic_inc(&wq->wait_ctr);
392 + wq->wait_ctr++;
393 mutex_unlock(&sbi->wq_mutex);
394 - kfree(name);
395 + kfree(qstr.name);
396 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
397 - (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
398 + (unsigned long) wq->wait_queue_token, wq->name.len,
399 + wq->name.name, notify);
400 }
401
402 - /* wq->name is NULL if and only if the lock is already released */
403 -
404 - if (sbi->catatonic) {
405 - /* We might have slept, so check again for catatonic mode */
406 - wq->status = -ENOENT;
407 - kfree(wq->name);
408 - wq->name = NULL;
409 - }
410 -
411 - if (wq->name) {
412 + /*
413 + * wq->name.name is NULL iff the lock is already released
414 + * or the mount has been made catatonic.
415 + */
416 + if (wq->name.name) {
417 /* Block all but "shutdown" signals while waiting */
418 sigset_t oldset;
419 unsigned long irqflags;
420 @@ -347,7 +439,7 @@ int autofs4_wait(struct autofs_sb_info *
421 recalc_sigpending();
422 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
423
424 - wait_event_interruptible(wq->queue, wq->name == NULL);
425 + wait_event_interruptible(wq->queue, wq->name.name == NULL);
426
427 spin_lock_irqsave(&current->sighand->siglock, irqflags);
428 current->blocked = oldset;
429 @@ -359,9 +451,45 @@ int autofs4_wait(struct autofs_sb_info *
430
431 status = wq->status;
432
433 + /*
434 + * For direct and offset mounts we need to track the requester's
435 + * uid and gid in the dentry info struct. This is so it can be
436 + * supplied, on request, by the misc device ioctl interface.
437 + * This is needed during daemon resatart when reconnecting
438 + * to existing, active, autofs mounts. The uid and gid (and
439 + * related string values) may be used for macro substitution
440 + * in autofs mount maps.
441 + */
442 + if (!status) {
443 + struct autofs_info *ino;
444 + struct dentry *de = NULL;
445 +
446 + /* direct mount or browsable map */
447 + ino = autofs4_dentry_ino(dentry);
448 + if (!ino) {
449 + /* If not lookup actual dentry used */
450 + de = d_lookup(dentry->d_parent, &dentry->d_name);
451 + if (de)
452 + ino = autofs4_dentry_ino(de);
453 + }
454 +
455 + /* Set mount requester */
456 + if (ino) {
457 + spin_lock(&sbi->fs_lock);
458 + ino->uid = wq->uid;
459 + ino->gid = wq->gid;
460 + spin_unlock(&sbi->fs_lock);
461 + }
462 +
463 + if (de)
464 + dput(de);
465 + }
466 +
467 /* Are we the last process to need status? */
468 - if (atomic_dec_and_test(&wq->wait_ctr))
469 + mutex_lock(&sbi->wq_mutex);
470 + if (!--wq->wait_ctr)
471 kfree(wq);
472 + mutex_unlock(&sbi->wq_mutex);
473
474 return status;
475 }
476 @@ -383,16 +511,13 @@ int autofs4_wait_release(struct autofs_s
477 }
478
479 *wql = wq->next; /* Unlink from chain */
480 - mutex_unlock(&sbi->wq_mutex);
481 - kfree(wq->name);
482 - wq->name = NULL; /* Do not wait on this queue */
483 -
484 + kfree(wq->name.name);
485 + wq->name.name = NULL; /* Do not wait on this queue */
486 wq->status = status;
487 -
488 - if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
489 + wake_up_interruptible(&wq->queue);
490 + if (!--wq->wait_ctr)
491 kfree(wq);
492 - else
493 - wake_up_interruptible(&wq->queue);
494 + mutex_unlock(&sbi->wq_mutex);
495
496 return 0;
497 }
498 --- linux-2.6.20.orig/include/linux/auto_fs4.h
499 +++ linux-2.6.20/include/linux/auto_fs4.h
500 @@ -23,12 +23,71 @@
501 #define AUTOFS_MIN_PROTO_VERSION 3
502 #define AUTOFS_MAX_PROTO_VERSION 5
503
504 -#define AUTOFS_PROTO_SUBVERSION 0
505 +#define AUTOFS_PROTO_SUBVERSION 1
506
507 /* Mask for expire behaviour */
508 #define AUTOFS_EXP_IMMEDIATE 1
509 #define AUTOFS_EXP_LEAVES 2
510
511 +#define AUTOFS_TYPE_ANY 0U
512 +#define AUTOFS_TYPE_INDIRECT 1U
513 +#define AUTOFS_TYPE_DIRECT 2U
514 +#define AUTOFS_TYPE_OFFSET 4U
515 +
516 +static inline void set_autofs_type_indirect(unsigned int *type)
517 +{
518 + *type = AUTOFS_TYPE_INDIRECT;
519 + return;
520 +}
521 +
522 +static inline unsigned int autofs_type_indirect(unsigned int type)
523 +{
524 + return (type == AUTOFS_TYPE_INDIRECT);
525 +}
526 +
527 +static inline void set_autofs_type_direct(unsigned int *type)
528 +{
529 + *type = AUTOFS_TYPE_DIRECT;
530 + return;
531 +}
532 +
533 +static inline unsigned int autofs_type_direct(unsigned int type)
534 +{
535 + return (type == AUTOFS_TYPE_DIRECT);
536 +}
537 +
538 +static inline void set_autofs_type_offset(unsigned int *type)
539 +{
540 + *type = AUTOFS_TYPE_OFFSET;
541 + return;
542 +}
543 +
544 +static inline unsigned int autofs_type_offset(unsigned int type)
545 +{
546 + return (type == AUTOFS_TYPE_OFFSET);
547 +}
548 +
549 +static inline unsigned int autofs_type_trigger(unsigned int type)
550 +{
551 + return (type == AUTOFS_TYPE_DIRECT || type == AUTOFS_TYPE_OFFSET);
552 +}
553 +
554 +/*
555 + * This isn't really a type as we use it to say "no type set" to
556 + * indicate we want to search for "any" mount in the
557 + * autofs_dev_ioctl_ismountpoint() device ioctl function.
558 + */
559 +static inline void set_autofs_type_any(unsigned int *type)
560 +{
561 + *type = AUTOFS_TYPE_ANY;
562 + return;
563 +}
564 +
565 +static inline unsigned int autofs_type_any(unsigned int type)
566 +{
567 + return (type == AUTOFS_TYPE_ANY);
568 +}
569 +
570 /* Daemon notification packet types */
571 enum autofs_notify {
572 NFY_NONE,
573 @@ -59,6 +118,13 @@ struct autofs_packet_expire_multi {
574 char name[NAME_MAX+1];
575 };
576
577 +union autofs_packet_union {
578 + struct autofs_packet_hdr hdr;
579 + struct autofs_packet_missing missing;
580 + struct autofs_packet_expire expire;
581 + struct autofs_packet_expire_multi expire_multi;
582 +};
583 +
584 /* autofs v5 common packet struct */
585 struct autofs_v5_packet {
586 struct autofs_packet_hdr hdr;
587 @@ -78,20 +144,19 @@ typedef struct autofs_v5_packet autofs_p
588 typedef struct autofs_v5_packet autofs_packet_missing_direct_t;
589 typedef struct autofs_v5_packet autofs_packet_expire_direct_t;
590
591 -union autofs_packet_union {
592 +union autofs_v5_packet_union {
593 struct autofs_packet_hdr hdr;
594 - struct autofs_packet_missing missing;
595 - struct autofs_packet_expire expire;
596 - struct autofs_packet_expire_multi expire_multi;
597 struct autofs_v5_packet v5_packet;
598 + autofs_packet_missing_indirect_t missing_indirect;
599 + autofs_packet_expire_indirect_t expire_indirect;
600 + autofs_packet_missing_direct_t missing_direct;
601 + autofs_packet_expire_direct_t expire_direct;
602 };
603
604 #define AUTOFS_IOC_EXPIRE_MULTI _IOW(0x93,0x66,int)
605 #define AUTOFS_IOC_EXPIRE_INDIRECT AUTOFS_IOC_EXPIRE_MULTI
606 #define AUTOFS_IOC_EXPIRE_DIRECT AUTOFS_IOC_EXPIRE_MULTI
607 #define AUTOFS_IOC_PROTOSUBVER _IOR(0x93,0x67,int)
608 -#define AUTOFS_IOC_ASKREGHOST _IOR(0x93,0x68,int)
609 -#define AUTOFS_IOC_TOGGLEREGHOST _IOR(0x93,0x69,int)
610 #define AUTOFS_IOC_ASKUMOUNT _IOR(0x93,0x70,int)
611
612
613 --- linux-2.6.20.orig/fs/autofs4/autofs_i.h
614 +++ linux-2.6.20/fs/autofs4/autofs_i.h
615 @@ -14,6 +14,7 @@
616 /* Internal header file for autofs */
617
618 #include <linux/auto_fs4.h>
619 +#include <linux/auto_dev-ioctl.h>
620 #include <linux/mutex.h>
621 #include <linux/list.h>
622
623 @@ -21,6 +22,9 @@
624 #define AUTOFS_IOC_FIRST AUTOFS_IOC_READY
625 #define AUTOFS_IOC_COUNT 32
626
627 +#define AUTOFS_DEV_IOCTL_IOC_FIRST (AUTOFS_DEV_IOCTL_VERSION)
628 +#define AUTOFS_DEV_IOCTL_IOC_COUNT (AUTOFS_IOC_COUNT - 11)
629 +
630 #include <linux/kernel.h>
631 #include <linux/slab.h>
632 #include <linux/time.h>
633 @@ -35,11 +39,27 @@
634 /* #define DEBUG */
635
636 #ifdef DEBUG
637 -#define DPRINTK(fmt,args...) do { printk(KERN_DEBUG "pid %d: %s: " fmt "\n" , current->pid , __FUNCTION__ , ##args); } while(0)
638 +#define DPRINTK(fmt, args...) \
639 +do { \
640 + printk(KERN_DEBUG "pid %d: %s: " fmt "\n", \
641 + current->pid, __FUNCTION__, ##args); \
642 +} while (0)
643 #else
644 -#define DPRINTK(fmt,args...) do {} while(0)
645 +#define DPRINTK(fmt, args...) do {} while (0)
646 #endif
647
648 +#define AUTOFS_WARN(fmt, args...) \
649 +do { \
650 + printk(KERN_WARNING "pid %d: %s: " fmt "\n", \
651 + current->pid, __FUNCTION__, ##args); \
652 +} while (0)
653 +
654 +#define AUTOFS_ERROR(fmt, args...) \
655 +do { \
656 + printk(KERN_ERR "pid %d: %s: " fmt "\n", \
657 + current->pid, __FUNCTION__, ##args); \
658 +} while (0)
659 +
660 /* Unified info structure. This is pointed to by both the dentry and
661 inode structures. Each file in the filesystem has an instance of this
662 structure. It holds a reference to the dentry, so dentries are never
663 @@ -52,10 +72,18 @@ struct autofs_info {
664
665 int flags;
666
667 + struct completion expire_complete;
668 +
669 + struct list_head active;
670 + struct list_head expiring;
671 +
672 struct autofs_sb_info *sbi;
673 unsigned long last_used;
674 atomic_t count;
675
676 + uid_t uid;
677 + gid_t gid;
678 +
679 mode_t mode;
680 size_t size;
681
682 @@ -66,15 +94,14 @@ struct autofs_info {
683 };
684
685 #define AUTOFS_INF_EXPIRING (1<<0) /* dentry is in the process of expiring */
686 +#define AUTOFS_INF_MOUNTPOINT (1<<1) /* mountpoint status for direct expire */
687
688 struct autofs_wait_queue {
689 wait_queue_head_t queue;
690 struct autofs_wait_queue *next;
691 autofs_wqt_t wait_queue_token;
692 /* We use the following to see what we are waiting for */
693 - unsigned int hash;
694 - unsigned int len;
695 - char *name;
696 + struct qstr name;
697 u32 dev;
698 u64 ino;
699 uid_t uid;
700 @@ -83,15 +110,11 @@ struct autofs_wait_queue {
701 pid_t tgid;
702 /* This is for status reporting upon return */
703 int status;
704 - atomic_t wait_ctr;
705 + unsigned int wait_ctr;
706 };
707
708 #define AUTOFS_SBI_MAGIC 0x6d4a556d
709
710 -#define AUTOFS_TYPE_INDIRECT 0x0001
711 -#define AUTOFS_TYPE_DIRECT 0x0002
712 -#define AUTOFS_TYPE_OFFSET 0x0004
713 -
714 struct autofs_sb_info {
715 u32 magic;
716 int pipefd;
717 @@ -110,6 +133,9 @@ struct autofs_sb_info {
718 struct mutex wq_mutex;
719 spinlock_t fs_lock;
720 struct autofs_wait_queue *queues; /* Wait queue pointer */
721 + spinlock_t lookup_lock;
722 + struct list_head active_list;
723 + struct list_head expiring_list;
724 };
725
726 static inline struct autofs_sb_info *autofs4_sbi(struct super_block *sb)
727 @@ -134,18 +160,14 @@ static inline int autofs4_oz_mode(struct
728 static inline int autofs4_ispending(struct dentry *dentry)
729 {
730 struct autofs_info *inf = autofs4_dentry_ino(dentry);
731 - int pending = 0;
732
733 if (dentry->d_flags & DCACHE_AUTOFS_PENDING)
734 return 1;
735
736 - if (inf) {
737 - spin_lock(&inf->sbi->fs_lock);
738 - pending = inf->flags & AUTOFS_INF_EXPIRING;
739 - spin_unlock(&inf->sbi->fs_lock);
740 - }
741 + if (inf->flags & AUTOFS_INF_EXPIRING)
742 + return 1;
743
744 - return pending;
745 + return 0;
746 }
747
748 static inline void autofs4_copy_atime(struct file *src, struct file *dst)
749 @@ -160,11 +182,25 @@ void autofs4_free_ino(struct autofs_info
750
751 /* Expiration */
752 int is_autofs4_dentry(struct dentry *);
753 +int autofs4_expire_wait(struct dentry *dentry);
754 int autofs4_expire_run(struct super_block *, struct vfsmount *,
755 struct autofs_sb_info *,
756 struct autofs_packet_expire __user *);
757 +int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
758 + struct autofs_sb_info *sbi, int when);
759 int autofs4_expire_multi(struct super_block *, struct vfsmount *,
760 struct autofs_sb_info *, int __user *);
761 +struct dentry *autofs4_expire_direct(struct super_block *sb,
762 + struct vfsmount *mnt,
763 + struct autofs_sb_info *sbi, int how);
764 +struct dentry *autofs4_expire_indirect(struct super_block *sb,
765 + struct vfsmount *mnt,
766 + struct autofs_sb_info *sbi, int how);
767 +
768 +/* Device node initialization */
769 +
770 +int autofs_dev_ioctl_init(void);
771 +void autofs_dev_ioctl_exit(void);
772
773 /* Operations structures */
774
775 --- linux-2.6.20.orig/fs/autofs4/inode.c
776 +++ linux-2.6.20/fs/autofs4/inode.c
777 @@ -25,8 +25,10 @@
778
779 static void ino_lnkfree(struct autofs_info *ino)
780 {
781 - kfree(ino->u.symlink);
782 - ino->u.symlink = NULL;
783 + if (ino->u.symlink) {
784 + kfree(ino->u.symlink);
785 + ino->u.symlink = NULL;
786 + }
787 }
788
789 struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
790 @@ -42,14 +44,20 @@ struct autofs_info *autofs4_init_ino(str
791 if (ino == NULL)
792 return NULL;
793
794 - ino->flags = 0;
795 - ino->mode = mode;
796 - ino->inode = NULL;
797 - ino->dentry = NULL;
798 - ino->size = 0;
799 + if (!reinit) {
800 + ino->flags = 0;
801 + ino->inode = NULL;
802 + ino->dentry = NULL;
803 + ino->size = 0;
804 + INIT_LIST_HEAD(&ino->active);
805 + INIT_LIST_HEAD(&ino->expiring);
806 + atomic_set(&ino->count, 0);
807 + }
808
809 + ino->uid = 0;
810 + ino->gid = 0;
811 + ino->mode = mode;
812 ino->last_used = jiffies;
813 - atomic_set(&ino->count, 0);
814
815 ino->sbi = sbi;
816
817 @@ -158,14 +166,13 @@ void autofs4_kill_sb(struct super_block
818 if (!sbi)
819 goto out_kill_sb;
820
821 - sb->s_fs_info = NULL;
822 -
823 - if ( !sbi->catatonic )
824 - autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
825 + /* Free wait queues, close pipe */
826 + autofs4_catatonic_mode(sbi);
827
828 /* Clean up and release dangling references */
829 autofs4_force_release(sbi);
830
831 + sb->s_fs_info = NULL;
832 kfree(sbi);
833
834 out_kill_sb:
835 @@ -186,9 +193,9 @@ static int autofs4_show_options(struct s
836 seq_printf(m, ",minproto=%d", sbi->min_proto);
837 seq_printf(m, ",maxproto=%d", sbi->max_proto);
838
839 - if (sbi->type & AUTOFS_TYPE_OFFSET)
840 + if (autofs_type_offset(sbi->type))
841 seq_printf(m, ",offset");
842 - else if (sbi->type & AUTOFS_TYPE_DIRECT)
843 + else if (autofs_type_direct(sbi->type))
844 seq_printf(m, ",direct");
845 else
846 seq_printf(m, ",indirect");
847 @@ -274,13 +281,13 @@ static int parse_options(char *options,
848 *maxproto = option;
849 break;
850 case Opt_indirect:
851 - *type = AUTOFS_TYPE_INDIRECT;
852 + set_autofs_type_indirect(type);
853 break;
854 case Opt_direct:
855 - *type = AUTOFS_TYPE_DIRECT;
856 + set_autofs_type_direct(type);
857 break;
858 case Opt_offset:
859 - *type = AUTOFS_TYPE_DIRECT | AUTOFS_TYPE_OFFSET;
860 + set_autofs_type_offset(type);
861 break;
862 default:
863 return 1;
864 @@ -330,12 +337,15 @@ int autofs4_fill_super(struct super_bloc
865 sbi->sb = s;
866 sbi->version = 0;
867 sbi->sub_version = 0;
868 - sbi->type = 0;
869 + set_autofs_type_indirect(&sbi->type);
870 sbi->min_proto = 0;
871 sbi->max_proto = 0;
872 mutex_init(&sbi->wq_mutex);
873 spin_lock_init(&sbi->fs_lock);
874 sbi->queues = NULL;
875 + spin_lock_init(&sbi->lookup_lock);
876 + INIT_LIST_HEAD(&sbi->active_list);
877 + INIT_LIST_HEAD(&sbi->expiring_list);
878 s->s_blocksize = 1024;
879 s->s_blocksize_bits = 10;
880 s->s_magic = AUTOFS_SUPER_MAGIC;
881 @@ -370,7 +380,7 @@ int autofs4_fill_super(struct super_bloc
882 }
883
884 root_inode->i_fop = &autofs4_root_operations;
885 - root_inode->i_op = sbi->type & AUTOFS_TYPE_DIRECT ?
886 + root_inode->i_op = autofs_type_trigger(sbi->type) ?
887 &autofs4_direct_root_inode_operations :
888 &autofs4_indirect_root_inode_operations;
889
890 --- linux-2.6.20.orig/fs/autofs4/root.c
891 +++ linux-2.6.20/fs/autofs4/root.c
892 @@ -26,25 +26,25 @@ static int autofs4_dir_rmdir(struct inod
893 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
894 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
895 static int autofs4_dir_open(struct inode *inode, struct file *file);
896 -static int autofs4_dir_close(struct inode *inode, struct file *file);
897 -static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
898 -static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
899 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
900 static void *autofs4_follow_link(struct dentry *, struct nameidata *);
901
902 +#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
903 +#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
904 +
905 const struct file_operations autofs4_root_operations = {
906 .open = dcache_dir_open,
907 .release = dcache_dir_close,
908 .read = generic_read_dir,
909 - .readdir = autofs4_root_readdir,
910 + .readdir = dcache_readdir,
911 .ioctl = autofs4_root_ioctl,
912 };
913
914 const struct file_operations autofs4_dir_operations = {
915 .open = autofs4_dir_open,
916 - .release = autofs4_dir_close,
917 + .release = dcache_dir_close,
918 .read = generic_read_dir,
919 - .readdir = autofs4_dir_readdir,
920 + .readdir = dcache_readdir,
921 };
922
923 struct inode_operations autofs4_indirect_root_inode_operations = {
924 @@ -71,42 +71,10 @@ struct inode_operations autofs4_dir_inod
925 .rmdir = autofs4_dir_rmdir,
926 };
927
928 -static int autofs4_root_readdir(struct file *file, void *dirent,
929 - filldir_t filldir)
930 -{
931 - struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb);
932 - int oz_mode = autofs4_oz_mode(sbi);
933 -
934 - DPRINTK("called, filp->f_pos = %lld", file->f_pos);
935 -
936 - /*
937 - * Don't set reghost flag if:
938 - * 1) f_pos is larger than zero -- we've already been here.
939 - * 2) we haven't even enabled reghosting in the 1st place.
940 - * 3) this is the daemon doing a readdir
941 - */
942 - if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
943 - sbi->needs_reghost = 1;
944 -
945 - DPRINTK("needs_reghost = %d", sbi->needs_reghost);
946 -
947 - return dcache_readdir(file, dirent, filldir);
948 -}
949 -
950 static int autofs4_dir_open(struct inode *inode, struct file *file)
951 {
952 struct dentry *dentry = file->f_path.dentry;
953 - struct vfsmount *mnt = file->f_path.mnt;
954 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
955 - struct dentry *cursor;
956 - int status;
957 -
958 - status = dcache_dir_open(inode, file);
959 - if (status)
960 - goto out;
961 -
962 - cursor = file->private_data;
963 - cursor->d_fsdata = NULL;
964
965 DPRINTK("file=%p dentry=%p %.*s",
966 file, dentry, dentry->d_name.len, dentry->d_name.name);
967 @@ -114,157 +82,31 @@ static int autofs4_dir_open(struct inode
968 if (autofs4_oz_mode(sbi))
969 goto out;
970
971 - if (autofs4_ispending(dentry)) {
972 - DPRINTK("dentry busy");
973 - dcache_dir_close(inode, file);
974 - status = -EBUSY;
975 - goto out;
976 - }
977 -
978 - status = -ENOENT;
979 - if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
980 - struct nameidata nd;
981 - int empty, ret;
982 -
983 - /* In case there are stale directory dentrys from a failed mount */
984 - spin_lock(&dcache_lock);
985 - empty = list_empty(&dentry->d_subdirs);
986 + /*
987 + * An empty directory in an autofs file system is always a
988 + * mount point. The daemon must have failed to mount this
989 + * during lookup so it doesn't exist. This can happen, for
990 + * example, if user space returns an incorrect status for a
991 + * mount request. Otherwise we're doing a readdir on the
992 + * autofs file system so just let the libfs routines handle
993 + * it.
994 + */
995 + spin_lock(&dcache_lock);
996 + if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
997 spin_unlock(&dcache_lock);
998 -
999 - if (!empty)
1000 - d_invalidate(dentry);
1001 -
1002 - nd.flags = LOOKUP_DIRECTORY;
1003 - ret = (dentry->d_op->d_revalidate)(dentry, &nd);
1004 -
1005 - if (ret <= 0) {
1006 - if (ret < 0)
1007 - status = ret;
1008 - dcache_dir_close(inode, file);
1009 - goto out;
1010 - }
1011 - }
1012 -
1013 - if (d_mountpoint(dentry)) {
1014 - struct file *fp = NULL;
1015 - struct vfsmount *fp_mnt = mntget(mnt);
1016 - struct dentry *fp_dentry = dget(dentry);
1017 -
1018 - if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
1019 - dput(fp_dentry);
1020 - mntput(fp_mnt);
1021 - dcache_dir_close(inode, file);
1022 - goto out;
1023 - }
1024 -
1025 - fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
1026 - status = PTR_ERR(fp);
1027 - if (IS_ERR(fp)) {
1028 - dcache_dir_close(inode, file);
1029 - goto out;
1030 - }
1031 - cursor->d_fsdata = fp;
1032 - }
1033 - return 0;
1034 -out:
1035 - return status;
1036 -}
1037 -
1038 -static int autofs4_dir_close(struct inode *inode, struct file *file)
1039 -{
1040 - struct dentry *dentry = file->f_path.dentry;
1041 - struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
1042 - struct dentry *cursor = file->private_data;
1043 - int status = 0;
1044 -
1045 - DPRINTK("file=%p dentry=%p %.*s",
1046 - file, dentry, dentry->d_name.len, dentry->d_name.name);
1047 -
1048 - if (autofs4_oz_mode(sbi))
1049 - goto out;
1050 -
1051 - if (autofs4_ispending(dentry)) {
1052 - DPRINTK("dentry busy");
1053 - status = -EBUSY;
1054 - goto out;
1055 - }
1056 -
1057 - if (d_mountpoint(dentry)) {
1058 - struct file *fp = cursor->d_fsdata;
1059 - if (!fp) {
1060 - status = -ENOENT;
1061 - goto out;
1062 - }
1063 - filp_close(fp, current->files);
1064 - }
1065 -out:
1066 - dcache_dir_close(inode, file);
1067 - return status;
1068 -}
1069 -
1070 -static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
1071 -{
1072 - struct dentry *dentry = file->f_path.dentry;
1073 - struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
1074 - struct dentry *cursor = file->private_data;
1075 - int status;
1076 -
1077 - DPRINTK("file=%p dentry=%p %.*s",
1078 - file, dentry, dentry->d_name.len, dentry->d_name.name);
1079 -
1080 - if (autofs4_oz_mode(sbi))
1081 - goto out;
1082 -
1083 - if (autofs4_ispending(dentry)) {
1084 - DPRINTK("dentry busy");
1085 - return -EBUSY;
1086 + return -ENOENT;
1087 }
1088 + spin_unlock(&dcache_lock);
1089
1090 - if (d_mountpoint(dentry)) {
1091 - struct file *fp = cursor->d_fsdata;
1092 -
1093 - if (!fp)
1094 - return -ENOENT;
1095 -
1096 - if (!fp->f_op || !fp->f_op->readdir)
1097 - goto out;
1098 -
1099 - status = vfs_readdir(fp, filldir, dirent);
1100 - file->f_pos = fp->f_pos;
1101 - if (status)
1102 - autofs4_copy_atime(file, fp);
1103 - return status;
1104 - }
1105 out:
1106 - return dcache_readdir(file, dirent, filldir);
1107 + return dcache_dir_open(inode, file);
1108 }
1109
1110 static int try_to_fill_dentry(struct dentry *dentry, int flags)
1111 {
1112 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
1113 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1114 - int status = 0;
1115 -
1116 - /* Block on any pending expiry here; invalidate the dentry
1117 - when expiration is done to trigger mount request with a new
1118 - dentry */
1119 - if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
1120 - DPRINTK("waiting for expire %p name=%.*s",
1121 - dentry, dentry->d_name.len, dentry->d_name.name);
1122 -
1123 - status = autofs4_wait(sbi, dentry, NFY_NONE);
1124 -
1125 - DPRINTK("expire done status=%d", status);
1126 -
1127 - /*
1128 - * If the directory still exists the mount request must
1129 - * continue otherwise it can't be followed at the right
1130 - * time during the walk.
1131 - */
1132 - status = d_invalidate(dentry);
1133 - if (status != -EBUSY)
1134 - return -ENOENT;
1135 - }
1136 + int status;
1137
1138 DPRINTK("dentry=%p %.*s ino=%p",
1139 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
1140 @@ -292,7 +134,8 @@ static int try_to_fill_dentry(struct den
1141 return status;
1142 }
1143 /* Trigger mount for path component or follow link */
1144 - } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
1145 + } else if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
1146 + flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) ||
1147 current->link_count) {
1148 DPRINTK("waiting for mount name=%.*s",
1149 dentry->d_name.len, dentry->d_name.name);
1150 @@ -319,7 +162,8 @@ static int try_to_fill_dentry(struct den
1151 spin_lock(&dentry->d_lock);
1152 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
1153 spin_unlock(&dentry->d_lock);
1154 - return status;
1155 +
1156 + return 0;
1157 }
1158
1159 /* For autofs direct mounts the follow link triggers the mount */
1160 @@ -334,50 +178,62 @@ static void *autofs4_follow_link(struct
1161 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
1162 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
1163 nd->flags);
1164 -
1165 - /* If it's our master or we shouldn't trigger a mount we're done */
1166 - lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
1167 - if (oz_mode || !lookup_type)
1168 + /*
1169 + * For an expire of a covered direct or offset mount we need
1170 + * to beeak out of follow_down() at the autofs mount trigger
1171 + * (d_mounted--), so we can see the expiring flag, and manage
1172 + * the blocking and following here until the expire is completed.
1173 + */
1174 + if (oz_mode) {
1175 + spin_lock(&sbi->fs_lock);
1176 + if (ino->flags & AUTOFS_INF_EXPIRING) {
1177 + spin_unlock(&sbi->fs_lock);
1178 + /* Follow down to our covering mount. */
1179 + if (!follow_down(&nd->mnt, &nd->dentry))
1180 + goto done;
1181 + goto follow;
1182 + }
1183 + spin_unlock(&sbi->fs_lock);
1184 goto done;
1185 + }
1186
1187 - /* If an expire request is pending wait for it. */
1188 - if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
1189 - DPRINTK("waiting for active request %p name=%.*s",
1190 - dentry, dentry->d_name.len, dentry->d_name.name);
1191 + /* If an expire request is pending everyone must wait. */
1192 + autofs4_expire_wait(dentry);
1193
1194 - status = autofs4_wait(sbi, dentry, NFY_NONE);
1195 -
1196 - DPRINTK("request done status=%d", status);
1197 - }
1198 + /* We trigger a mount for almost all flags */
1199 + lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS);
1200 + if (!(lookup_type || dentry->d_flags & DCACHE_AUTOFS_PENDING))
1201 + goto follow;
1202
1203 /*
1204 - * If the dentry contains directories then it is an
1205 - * autofs multi-mount with no root mount offset. So
1206 - * don't try to mount it again.
1207 + * If the dentry contains directories then it is an autofs
1208 + * multi-mount with no root mount offset. So don't try to
1209 + * mount it again.
1210 */
1211 spin_lock(&dcache_lock);
1212 - if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
1213 + if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
1214 + (!d_mountpoint(dentry) && __simple_empty(dentry))) {
1215 spin_unlock(&dcache_lock);
1216
1217 status = try_to_fill_dentry(dentry, 0);
1218 if (status)
1219 goto out_error;
1220
1221 - /*
1222 - * The mount succeeded but if there is no root mount
1223 - * it must be an autofs multi-mount with no root offset
1224 - * so we don't need to follow the mount.
1225 - */
1226 - if (d_mountpoint(dentry)) {
1227 - if (!autofs4_follow_mount(&nd->mnt, &nd->dentry)) {
1228 - status = -ENOENT;
1229 - goto out_error;
1230 - }
1231 - }
1232 -
1233 - goto done;
1234 + goto follow;
1235 }
1236 spin_unlock(&dcache_lock);
1237 +follow:
1238 + /*
1239 + * If there is no root mount it must be an autofs
1240 + * multi-mount with no root offset so we don't need
1241 + * to follow it.
1242 + */
1243 + if (d_mountpoint(dentry)) {
1244 + if (!autofs4_follow_mount(&nd->mnt, &nd->dentry)) {
1245 + status = -ENOENT;
1246 + goto out_error;
1247 + }
1248 + }
1249
1250 done:
1251 return NULL;
1252 @@ -402,21 +258,33 @@ static int autofs4_revalidate(struct den
1253 int status = 1;
1254
1255 /* Pending dentry */
1256 + spin_lock(&sbi->fs_lock);
1257 if (autofs4_ispending(dentry)) {
1258 /* The daemon never causes a mount to trigger */
1259 + spin_unlock(&sbi->fs_lock);
1260 +
1261 if (oz_mode)
1262 return 1;
1263
1264 /*
1265 + * If the directory has gone away due to an expire
1266 + * we have been called as ->d_revalidate() and so
1267 + * we need to return false and proceed to ->lookup().
1268 + */
1269 + if (autofs4_expire_wait(dentry) == -EAGAIN)
1270 + return 0;
1271 +
1272 + /*
1273 * A zero status is success otherwise we have a
1274 * negative error code.
1275 */
1276 status = try_to_fill_dentry(dentry, flags);
1277 if (status == 0)
1278 - return 1;
1279 + return 1;
1280
1281 return status;
1282 }
1283 + spin_unlock(&sbi->fs_lock);
1284
1285 /* Negative dentry.. invalidate if "old" */
1286 if (dentry->d_inode == NULL)
1287 @@ -430,6 +298,7 @@ static int autofs4_revalidate(struct den
1288 DPRINTK("dentry=%p %.*s, emptydir",
1289 dentry, dentry->d_name.len, dentry->d_name.name);
1290 spin_unlock(&dcache_lock);
1291 +
1292 /* The daemon never causes a mount to trigger */
1293 if (oz_mode)
1294 return 1;
1295 @@ -459,6 +328,17 @@ void autofs4_dentry_release(struct dentr
1296 de->d_fsdata = NULL;
1297
1298 if (inf) {
1299 + struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
1300 +
1301 + if (sbi) {
1302 + spin_lock(&sbi->lookup_lock);
1303 + if (!list_empty(&inf->active))
1304 + list_del(&inf->active);
1305 + if (!list_empty(&inf->expiring))
1306 + list_del(&inf->expiring);
1307 + spin_unlock(&sbi->lookup_lock);
1308 + }
1309 +
1310 inf->dentry = NULL;
1311 inf->inode = NULL;
1312
1313 @@ -478,10 +358,116 @@ static struct dentry_operations autofs4_
1314 .d_release = autofs4_dentry_release,
1315 };
1316
1317 +static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
1318 +{
1319 + unsigned int len = name->len;
1320 + unsigned int hash = name->hash;
1321 + const unsigned char *str = name->name;
1322 + struct list_head *p, *head;
1323 +
1324 + spin_lock(&dcache_lock);
1325 + spin_lock(&sbi->lookup_lock);
1326 + head = &sbi->active_list;
1327 + list_for_each(p, head) {
1328 + struct autofs_info *ino;
1329 + struct dentry *dentry;
1330 + struct qstr *qstr;
1331 +
1332 + ino = list_entry(p, struct autofs_info, active);
1333 + dentry = ino->dentry;
1334 +
1335 + spin_lock(&dentry->d_lock);
1336 +
1337 + /* Already gone? */
1338 + if (atomic_read(&dentry->d_count) == 0)
1339 + goto next;
1340 +
1341 + qstr = &dentry->d_name;
1342 +
1343 + if (dentry->d_name.hash != hash)
1344 + goto next;
1345 + if (dentry->d_parent != parent)
1346 + goto next;
1347 +
1348 + if (qstr->len != len)
1349 + goto next;
1350 + if (memcmp(qstr->name, str, len))
1351 + goto next;
1352 +
1353 + if (d_unhashed(dentry)) {
1354 + dget(dentry);
1355 + spin_unlock(&dentry->d_lock);
1356 + spin_unlock(&sbi->lookup_lock);
1357 + spin_unlock(&dcache_lock);
1358 + return dentry;
1359 + }
1360 +next:
1361 + spin_unlock(&dentry->d_lock);
1362 + }
1363 + spin_unlock(&sbi->lookup_lock);
1364 + spin_unlock(&dcache_lock);
1365 +
1366 + return NULL;
1367 +}
1368 +
1369 +static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
1370 +{
1371 + unsigned int len = name->len;
1372 + unsigned int hash = name->hash;
1373 + const unsigned char *str = name->name;
1374 + struct list_head *p, *head;
1375 +
1376 + spin_lock(&dcache_lock);
1377 + spin_lock(&sbi->lookup_lock);
1378 + head = &sbi->expiring_list;
1379 + list_for_each(p, head) {
1380 + struct autofs_info *ino;
1381 + struct dentry *dentry;
1382 + struct qstr *qstr;
1383 +
1384 + ino = list_entry(p, struct autofs_info, expiring);
1385 + dentry = ino->dentry;
1386 +
1387 + spin_lock(&dentry->d_lock);
1388 +
1389 + /* Bad luck, we've already been dentry_iput */
1390 + if (!dentry->d_inode)
1391 + goto next;
1392 +
1393 + qstr = &dentry->d_name;
1394 +
1395 + if (dentry->d_name.hash != hash)
1396 + goto next;
1397 + if (dentry->d_parent != parent)
1398 + goto next;
1399 +
1400 + if (qstr->len != len)
1401 + goto next;
1402 + if (memcmp(qstr->name, str, len))
1403 + goto next;
1404 +
1405 + if (d_unhashed(dentry)) {
1406 + dget(dentry);
1407 + spin_unlock(&dentry->d_lock);
1408 + spin_unlock(&sbi->lookup_lock);
1409 + spin_unlock(&dcache_lock);
1410 + return dentry;
1411 + }
1412 +next:
1413 + spin_unlock(&dentry->d_lock);
1414 + }
1415 + spin_unlock(&sbi->lookup_lock);
1416 + spin_unlock(&dcache_lock);
1417 +
1418 + return NULL;
1419 +}
1420 +
1421 /* Lookups in the root directory */
1422 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1423 {
1424 struct autofs_sb_info *sbi;
1425 + struct autofs_info *ino;
1426 + struct dentry *expiring, *unhashed;
1427 int oz_mode;
1428
1429 DPRINTK("name = %.*s",
1430 @@ -497,29 +483,67 @@ static struct dentry *autofs4_lookup(str
1431 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
1432 current->pid, process_group(current), sbi->catatonic, oz_mode);
1433
1434 - /*
1435 - * Mark the dentry incomplete, but add it. This is needed so
1436 - * that the VFS layer knows about the dentry, and we can count
1437 - * on catching any lookups through the revalidate.
1438 - *
1439 - * Let all the hard work be done by the revalidate function that
1440 - * needs to be able to do this anyway..
1441 - *
1442 - * We need to do this before we release the directory semaphore.
1443 - */
1444 - dentry->d_op = &autofs4_root_dentry_operations;
1445 + unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
1446 + if (unhashed)
1447 + dentry = unhashed;
1448 + else {
1449 + /*
1450 + * Mark the dentry incomplete but don't hash it. We do this
1451 + * to serialize our inode creation operations (symlink and
1452 + * mkdir) which prevents deadlock during the callback to
1453 + * the daemon. Subsequent user space lookups for the same
1454 + * dentry are placed on the wait queue while the daemon
1455 + * itself is allowed passage unresticted so the create
1456 + * operation itself can then hash the dentry. Finally,
1457 + * we check for the hashed dentry and return the newly
1458 + * hashed dentry.
1459 + */
1460 + dentry->d_op = &autofs4_root_dentry_operations;
1461 +
1462 + /*
1463 + * And we need to ensure that the same dentry is used for
1464 + * all following lookup calls until it is hashed so that
1465 + * the dentry flags are persistent throughout the request.
1466 + */
1467 + ino = autofs4_init_ino(NULL, sbi, 0555);
1468 + if (!ino)
1469 + return ERR_PTR(-ENOMEM);
1470 +
1471 + dentry->d_fsdata = ino;
1472 + ino->dentry = dentry;
1473 +
1474 + spin_lock(&sbi->lookup_lock);
1475 + list_add(&ino->active, &sbi->active_list);
1476 + spin_unlock(&sbi->lookup_lock);
1477 +
1478 + d_instantiate(dentry, NULL);
1479 + }
1480
1481 if (!oz_mode) {
1482 + mutex_unlock(&dir->i_mutex);
1483 + expiring = autofs4_lookup_expiring(sbi,
1484 + dentry->d_parent,
1485 + &dentry->d_name);
1486 + if (expiring) {
1487 + /*
1488 + * If we are racing with expire the request might not
1489 + * be quite complete but the directory has been removed
1490 + * so it must have been successful, so just wait for it.
1491 + */
1492 + ino = autofs4_dentry_ino(expiring);
1493 + autofs4_expire_wait(expiring);
1494 + spin_lock(&sbi->lookup_lock);
1495 + if (!list_empty(&ino->expiring))
1496 + list_del_init(&ino->expiring);
1497 + spin_unlock(&sbi->lookup_lock);
1498 + dput(expiring);
1499 + }
1500 +
1501 spin_lock(&dentry->d_lock);
1502 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
1503 spin_unlock(&dentry->d_lock);
1504 - }
1505 - dentry->d_fsdata = NULL;
1506 - d_add(dentry, NULL);
1507 -
1508 - if (dentry->d_op && dentry->d_op->d_revalidate) {
1509 - mutex_unlock(&dir->i_mutex);
1510 - (dentry->d_op->d_revalidate)(dentry, nd);
1511 + if (dentry->d_op && dentry->d_op->d_revalidate)
1512 + (dentry->d_op->d_revalidate)(dentry, nd);
1513 mutex_lock(&dir->i_mutex);
1514 }
1515
1516 @@ -534,22 +558,47 @@ static struct dentry *autofs4_lookup(str
1517 if (sigismember (sigset, SIGKILL) ||
1518 sigismember (sigset, SIGQUIT) ||
1519 sigismember (sigset, SIGINT)) {
1520 + if (unhashed)
1521 + dput(unhashed);
1522 return ERR_PTR(-ERESTARTNOINTR);
1523 }
1524 }
1525 - spin_lock(&dentry->d_lock);
1526 - dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
1527 - spin_unlock(&dentry->d_lock);
1528 + if (!oz_mode) {
1529 + spin_lock(&dentry->d_lock);
1530 + dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
1531 + spin_unlock(&dentry->d_lock);
1532 + }
1533 }
1534
1535 /*
1536 * If this dentry is unhashed, then we shouldn't honour this
1537 - * lookup even if the dentry is positive. Returning ENOENT here
1538 - * doesn't do the right thing for all system calls, but it should
1539 - * be OK for the operations we permit from an autofs.
1540 + * lookup. Returning ENOENT here doesn't do the right thing
1541 + * for all system calls, but it should be OK for the operations
1542 + * we permit from an autofs.
1543 */
1544 - if (dentry->d_inode && d_unhashed(dentry))
1545 - return ERR_PTR(-ENOENT);
1546 + if (!oz_mode && d_unhashed(dentry)) {
1547 + /*
1548 + * A user space application can (and has done in the past)
1549 + * remove and re-create this directory during the callback.
1550 + * This can leave us with an unhashed dentry, but a
1551 + * successful mount! So we need to perform another
1552 + * cached lookup in case the dentry now exists.
1553 + */
1554 + struct dentry *parent = dentry->d_parent;
1555 + struct dentry *new = d_lookup(parent, &dentry->d_name);
1556 + if (new != NULL)
1557 + dentry = new;
1558 + else
1559 + dentry = ERR_PTR(-ENOENT);
1560 +
1561 + if (unhashed)
1562 + dput(unhashed);
1563 +
1564 + return dentry;
1565 + }
1566 +
1567 + if (unhashed)
1568 + return unhashed;
1569
1570 return NULL;
1571 }
1572 @@ -571,21 +620,32 @@ static int autofs4_dir_symlink(struct in
1573 return -EACCES;
1574
1575 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
1576 - if (ino == NULL)
1577 - return -ENOSPC;
1578 + if (!ino)
1579 + return -ENOMEM;
1580
1581 - ino->size = strlen(symname);
1582 - ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
1583 + spin_lock(&sbi->lookup_lock);
1584 + if (!list_empty(&ino->active))
1585 + list_del_init(&ino->active);
1586 + spin_unlock(&sbi->lookup_lock);
1587
1588 - if (cp == NULL) {
1589 - kfree(ino);
1590 - return -ENOSPC;
1591 + ino->size = strlen(symname);
1592 + cp = kmalloc(ino->size + 1, GFP_KERNEL);
1593 + if (!cp) {
1594 + if (!dentry->d_fsdata)
1595 + kfree(ino);
1596 + return -ENOMEM;
1597 }
1598
1599 strcpy(cp, symname);
1600
1601 inode = autofs4_get_inode(dir->i_sb, ino);
1602 - d_instantiate(dentry, inode);
1603 + if (!inode) {
1604 + kfree(cp);
1605 + if (!dentry->d_fsdata)
1606 + kfree(ino);
1607 + return -ENOMEM;
1608 + }
1609 + d_add(dentry, inode);
1610
1611 if (dir == dir->i_sb->s_root->d_inode)
1612 dentry->d_op = &autofs4_root_dentry_operations;
1613 @@ -600,6 +660,7 @@ static int autofs4_dir_symlink(struct in
1614 atomic_inc(&p_ino->count);
1615 ino->inode = inode;
1616
1617 + ino->u.symlink = cp;
1618 dir->i_mtime = CURRENT_TIME;
1619
1620 return 0;
1621 @@ -611,9 +672,9 @@ static int autofs4_dir_symlink(struct in
1622 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
1623 * that the file no longer exists. However, doing that means that the
1624 * VFS layer can turn the dentry into a negative dentry. We don't want
1625 - * this, because since the unlink is probably the result of an expire.
1626 - * We simply d_drop it, which allows the dentry lookup to remount it
1627 - * if necessary.
1628 + * this, because the unlink is probably the result of an expire.
1629 + * We simply d_drop it and add it to a expiring list in the super block,
1630 + * which allows the dentry lookup to check for an incomplete expire.
1631 *
1632 * If a process is blocked on the dentry waiting for the expire to finish,
1633 * it will invalidate the dentry and try to mount with a new one.
1634 @@ -642,7 +703,15 @@ static int autofs4_dir_unlink(struct ino
1635
1636 dir->i_mtime = CURRENT_TIME;
1637
1638 - d_drop(dentry);
1639 + spin_lock(&dcache_lock);
1640 + spin_lock(&sbi->lookup_lock);
1641 + if (list_empty(&ino->expiring))
1642 + list_add(&ino->expiring, &sbi->expiring_list);
1643 + spin_unlock(&sbi->lookup_lock);
1644 + spin_lock(&dentry->d_lock);
1645 + __d_drop(dentry);
1646 + spin_unlock(&dentry->d_lock);
1647 + spin_unlock(&dcache_lock);
1648
1649 return 0;
1650 }
1651 @@ -653,6 +722,9 @@ static int autofs4_dir_rmdir(struct inod
1652 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1653 struct autofs_info *p_ino;
1654
1655 + DPRINTK("dentry %p, removing %.*s",
1656 + dentry, dentry->d_name.len, dentry->d_name.name);
1657 +
1658 if (!autofs4_oz_mode(sbi))
1659 return -EACCES;
1660
1661 @@ -661,6 +733,10 @@ static int autofs4_dir_rmdir(struct inod
1662 spin_unlock(&dcache_lock);
1663 return -ENOTEMPTY;
1664 }
1665 + spin_lock(&sbi->lookup_lock);
1666 + if (list_empty(&ino->expiring))
1667 + list_add(&ino->expiring, &sbi->expiring_list);
1668 + spin_unlock(&sbi->lookup_lock);
1669 spin_lock(&dentry->d_lock);
1670 __d_drop(dentry);
1671 spin_unlock(&dentry->d_lock);
1672 @@ -695,11 +771,21 @@ static int autofs4_dir_mkdir(struct inod
1673 dentry, dentry->d_name.len, dentry->d_name.name);
1674
1675 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
1676 - if (ino == NULL)
1677 - return -ENOSPC;
1678 + if (!ino)
1679 + return -ENOMEM;
1680 +
1681 + spin_lock(&sbi->lookup_lock);
1682 + if (!list_empty(&ino->active))
1683 + list_del_init(&ino->active);
1684 + spin_unlock(&sbi->lookup_lock);
1685
1686 inode = autofs4_get_inode(dir->i_sb, ino);
1687 - d_instantiate(dentry, inode);
1688 + if (!inode) {
1689 + if (!dentry->d_fsdata)
1690 + kfree(ino);
1691 + return -ENOMEM;
1692 + }
1693 + d_add(dentry, inode);
1694
1695 if (dir == dir->i_sb->s_root->d_inode)
1696 dentry->d_op = &autofs4_root_dentry_operations;
1697 @@ -751,44 +837,6 @@ static inline int autofs4_get_protosubve
1698 }
1699
1700 /*
1701 - * Tells the daemon whether we need to reghost or not. Also, clears
1702 - * the reghost_needed flag.
1703 - */
1704 -static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
1705 -{
1706 - int status;
1707 -
1708 - DPRINTK("returning %d", sbi->needs_reghost);
1709 -
1710 - status = put_user(sbi->needs_reghost, p);
1711 - if ( status )
1712 - return status;
1713 -
1714 - sbi->needs_reghost = 0;
1715 - return 0;
1716 -}
1717 -
1718 -/*
1719 - * Enable / Disable reghosting ioctl() operation
1720 - */
1721 -static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
1722 -{
1723 - int status;
1724 - int val;
1725 -
1726 - status = get_user(val, p);
1727 -
1728 - DPRINTK("reghost = %d", val);
1729 -
1730 - if (status)
1731 - return status;
1732 -
1733 - /* turn on/off reghosting, with the val */
1734 - sbi->reghost_enabled = val;
1735 - return 0;
1736 -}
1737 -
1738 -/*
1739 * Tells the daemon whether it can umount the autofs mount.
1740 */
1741 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
1742 @@ -852,11 +900,6 @@ static int autofs4_root_ioctl(struct ino
1743 case AUTOFS_IOC_SETTIMEOUT:
1744 return autofs4_get_set_timeout(sbi, p);
1745
1746 - case AUTOFS_IOC_TOGGLEREGHOST:
1747 - return autofs4_toggle_reghost(sbi, p);
1748 - case AUTOFS_IOC_ASKREGHOST:
1749 - return autofs4_ask_reghost(sbi, p);
1750 -
1751 case AUTOFS_IOC_ASKUMOUNT:
1752 return autofs4_ask_umount(filp->f_path.mnt, p);
1753
1754 --- linux-2.6.20.orig/fs/autofs4/expire.c
1755 +++ linux-2.6.20/fs/autofs4/expire.c
1756 @@ -56,12 +56,25 @@ static int autofs4_mount_busy(struct vfs
1757 mntget(mnt);
1758 dget(dentry);
1759
1760 - if (!autofs4_follow_mount(&mnt, &dentry))
1761 + if (!follow_down(&mnt, &dentry))
1762 goto done;
1763
1764 - /* This is an autofs submount, we can't expire it */
1765 - if (is_autofs4_dentry(dentry))
1766 - goto done;
1767 + if (is_autofs4_dentry(dentry)) {
1768 + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
1769 +
1770 + /* This is an autofs submount, we can't expire it */
1771 + if (autofs_type_indirect(sbi->type))
1772 + goto done;
1773 +
1774 + /*
1775 + * Otherwise it's an offset mount and we need to check
1776 + * if we can umount its mount, if there is one.
1777 + */
1778 + if (!d_mountpoint(dentry)) {
1779 + status = 0;
1780 + goto done;
1781 + }
1782 + }
1783
1784 /* Update the expiry counter if fs is busy */
1785 if (!may_umount_tree(mnt)) {
1786 @@ -73,8 +86,8 @@ static int autofs4_mount_busy(struct vfs
1787 status = 0;
1788 done:
1789 DPRINTK("returning = %d", status);
1790 - mntput(mnt);
1791 dput(dentry);
1792 + mntput(mnt);
1793 return status;
1794 }
1795
1796 @@ -244,10 +257,10 @@ cont:
1797 }
1798
1799 /* Check if we can expire a direct mount (possibly a tree) */
1800 -static struct dentry *autofs4_expire_direct(struct super_block *sb,
1801 - struct vfsmount *mnt,
1802 - struct autofs_sb_info *sbi,
1803 - int how)
1804 +struct dentry *autofs4_expire_direct(struct super_block *sb,
1805 + struct vfsmount *mnt,
1806 + struct autofs_sb_info *sbi,
1807 + int how)
1808 {
1809 unsigned long timeout;
1810 struct dentry *root = dget(sb->s_root);
1811 @@ -259,13 +272,15 @@ static struct dentry *autofs4_expire_dir
1812 now = jiffies;
1813 timeout = sbi->exp_timeout;
1814
1815 - /* Lock the tree as we must expire as a whole */
1816 spin_lock(&sbi->fs_lock);
1817 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
1818 struct autofs_info *ino = autofs4_dentry_ino(root);
1819 -
1820 - /* Set this flag early to catch sys_chdir and the like */
1821 + if (d_mountpoint(root)) {
1822 + ino->flags |= AUTOFS_INF_MOUNTPOINT;
1823 + root->d_mounted--;
1824 + }
1825 ino->flags |= AUTOFS_INF_EXPIRING;
1826 + init_completion(&ino->expire_complete);
1827 spin_unlock(&sbi->fs_lock);
1828 return root;
1829 }
1830 @@ -281,10 +296,10 @@ static struct dentry *autofs4_expire_dir
1831 * - it is unused by any user process
1832 * - it has been unused for exp_timeout time
1833 */
1834 -static struct dentry *autofs4_expire_indirect(struct super_block *sb,
1835 - struct vfsmount *mnt,
1836 - struct autofs_sb_info *sbi,
1837 - int how)
1838 +struct dentry *autofs4_expire_indirect(struct super_block *sb,
1839 + struct vfsmount *mnt,
1840 + struct autofs_sb_info *sbi,
1841 + int how)
1842 {
1843 unsigned long timeout;
1844 struct dentry *root = sb->s_root;
1845 @@ -292,6 +307,8 @@ static struct dentry *autofs4_expire_ind
1846 struct list_head *next;
1847 int do_now = how & AUTOFS_EXP_IMMEDIATE;
1848 int exp_leaves = how & AUTOFS_EXP_LEAVES;
1849 + struct autofs_info *ino;
1850 + unsigned int ino_count;
1851
1852 if (!root)
1853 return NULL;
1854 @@ -316,6 +333,9 @@ static struct dentry *autofs4_expire_ind
1855 dentry = dget(dentry);
1856 spin_unlock(&dcache_lock);
1857
1858 + spin_lock(&sbi->fs_lock);
1859 + ino = autofs4_dentry_ino(dentry);
1860 +
1861 /*
1862 * Case 1: (i) indirect mount or top level pseudo direct mount
1863 * (autofs-4.1).
1864 @@ -326,6 +346,11 @@ static struct dentry *autofs4_expire_ind
1865 DPRINTK("checking mountpoint %p %.*s",
1866 dentry, (int)dentry->d_name.len, dentry->d_name.name);
1867
1868 + /* Path walk currently on this dentry? */
1869 + ino_count = atomic_read(&ino->count) + 2;
1870 + if (atomic_read(&dentry->d_count) > ino_count)
1871 + goto next;
1872 +
1873 /* Can we umount this guy */
1874 if (autofs4_mount_busy(mnt, dentry))
1875 goto next;
1876 @@ -333,7 +358,7 @@ static struct dentry *autofs4_expire_ind
1877 /* Can we expire this guy */
1878 if (autofs4_can_expire(dentry, timeout, do_now)) {
1879 expired = dentry;
1880 - break;
1881 + goto found;
1882 }
1883 goto next;
1884 }
1885 @@ -343,46 +368,80 @@ static struct dentry *autofs4_expire_ind
1886
1887 /* Case 2: tree mount, expire iff entire tree is not busy */
1888 if (!exp_leaves) {
1889 - /* Lock the tree as we must expire as a whole */
1890 - spin_lock(&sbi->fs_lock);
1891 - if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
1892 - struct autofs_info *inf = autofs4_dentry_ino(dentry);
1893 + /* Path walk currently on this dentry? */
1894 + ino_count = atomic_read(&ino->count) + 1;
1895 + if (atomic_read(&dentry->d_count) > ino_count)
1896 + goto next;
1897
1898 - /* Set this flag early to catch sys_chdir and the like */
1899 - inf->flags |= AUTOFS_INF_EXPIRING;
1900 - spin_unlock(&sbi->fs_lock);
1901 + if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
1902 expired = dentry;
1903 - break;
1904 + goto found;
1905 }
1906 - spin_unlock(&sbi->fs_lock);
1907 /*
1908 * Case 3: pseudo direct mount, expire individual leaves
1909 * (autofs-4.1).
1910 */
1911 } else {
1912 + /* Path walk currently on this dentry? */
1913 + ino_count = atomic_read(&ino->count) + 1;
1914 + if (atomic_read(&dentry->d_count) > ino_count)
1915 + goto next;
1916 +
1917 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
1918 if (expired) {
1919 dput(dentry);
1920 - break;
1921 + goto found;
1922 }
1923 }
1924 next:
1925 + spin_unlock(&sbi->fs_lock);
1926 dput(dentry);
1927 spin_lock(&dcache_lock);
1928 next = next->next;
1929 }
1930 + spin_unlock(&dcache_lock);
1931 + return NULL;
1932
1933 - if (expired) {
1934 - DPRINTK("returning %p %.*s",
1935 - expired, (int)expired->d_name.len, expired->d_name.name);
1936 - spin_lock(&dcache_lock);
1937 - list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
1938 - spin_unlock(&dcache_lock);
1939 - return expired;
1940 - }
1941 +found:
1942 + DPRINTK("returning %p %.*s",
1943 + expired, (int)expired->d_name.len, expired->d_name.name);
1944 + ino = autofs4_dentry_ino(expired);
1945 + ino->flags |= AUTOFS_INF_EXPIRING;
1946 + init_completion(&ino->expire_complete);
1947 + spin_unlock(&sbi->fs_lock);
1948 + spin_lock(&dcache_lock);
1949 + list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
1950 spin_unlock(&dcache_lock);
1951 + return expired;
1952 +}
1953
1954 - return NULL;
1955 +int autofs4_expire_wait(struct dentry *dentry)
1956 +{
1957 + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
1958 + struct autofs_info *ino = autofs4_dentry_ino(dentry);
1959 + int status;
1960 +
1961 + /* Block on any pending expire */
1962 + spin_lock(&sbi->fs_lock);
1963 + if (ino->flags & AUTOFS_INF_EXPIRING) {
1964 + spin_unlock(&sbi->fs_lock);
1965 +
1966 + DPRINTK("waiting for expire %p name=%.*s",
1967 + dentry, dentry->d_name.len, dentry->d_name.name);
1968 +
1969 + status = autofs4_wait(sbi, dentry, NFY_NONE);
1970 + wait_for_completion(&ino->expire_complete);
1971 +
1972 + DPRINTK("expire done status=%d", status);
1973 +
1974 + if (d_unhashed(dentry))
1975 + return -EAGAIN;
1976 +
1977 + return status;
1978 + }
1979 + spin_unlock(&sbi->fs_lock);
1980 +
1981 + return 0;
1982 }
1983
1984 /* Perform an expiry operation */
1985 @@ -392,7 +451,9 @@ int autofs4_expire_run(struct super_bloc
1986 struct autofs_packet_expire __user *pkt_p)
1987 {
1988 struct autofs_packet_expire pkt;
1989 + struct autofs_info *ino;
1990 struct dentry *dentry;
1991 + int ret = 0;
1992
1993 memset(&pkt,0,sizeof pkt);
1994
1995 @@ -408,39 +469,59 @@ int autofs4_expire_run(struct super_bloc
1996 dput(dentry);
1997
1998 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
1999 - return -EFAULT;
2000 + ret = -EFAULT;
2001
2002 - return 0;
2003 + spin_lock(&sbi->fs_lock);
2004 + ino = autofs4_dentry_ino(dentry);
2005 + ino->flags &= ~AUTOFS_INF_EXPIRING;
2006 + complete_all(&ino->expire_complete);
2007 + spin_unlock(&sbi->fs_lock);
2008 +
2009 + return ret;
2010 }
2011
2012 -/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
2013 - more to be done */
2014 -int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
2015 - struct autofs_sb_info *sbi, int __user *arg)
2016 +int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
2017 + struct autofs_sb_info *sbi, int when)
2018 {
2019 struct dentry *dentry;
2020 int ret = -EAGAIN;
2021 - int do_now = 0;
2022
2023 - if (arg && get_user(do_now, arg))
2024 - return -EFAULT;
2025 -
2026 - if (sbi->type & AUTOFS_TYPE_DIRECT)
2027 - dentry = autofs4_expire_direct(sb, mnt, sbi, do_now);
2028 + if (autofs_type_trigger(sbi->type))
2029 + dentry = autofs4_expire_direct(sb, mnt, sbi, when);
2030 else
2031 - dentry = autofs4_expire_indirect(sb, mnt, sbi, do_now);
2032 + dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
2033
2034 if (dentry) {
2035 struct autofs_info *ino = autofs4_dentry_ino(dentry);
2036
2037 /* This is synchronous because it makes the daemon a
2038 little easier */
2039 - ino->flags |= AUTOFS_INF_EXPIRING;
2040 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
2041 +
2042 + spin_lock(&sbi->fs_lock);
2043 + if (ino->flags & AUTOFS_INF_MOUNTPOINT) {
2044 + sb->s_root->d_mounted++;
2045 + ino->flags &= ~AUTOFS_INF_MOUNTPOINT;
2046 + }
2047 ino->flags &= ~AUTOFS_INF_EXPIRING;
2048 + complete_all(&ino->expire_complete);
2049 + spin_unlock(&sbi->fs_lock);
2050 dput(dentry);
2051 }
2052
2053 return ret;
2054 }
2055
2056 +/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
2057 + more to be done */
2058 +int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
2059 + struct autofs_sb_info *sbi, int __user *arg)
2060 +{
2061 + int do_now = 0;
2062 +
2063 + if (arg && get_user(do_now, arg))
2064 + return -EFAULT;
2065 +
2066 + return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
2067 +}
2068 +
2069 --- linux-2.6.20.orig/include/linux/compat_ioctl.h
2070 +++ linux-2.6.20/include/linux/compat_ioctl.h
2071 @@ -568,8 +568,6 @@ COMPATIBLE_IOCTL(AUTOFS_IOC_PROTOVER)
2072 COMPATIBLE_IOCTL(AUTOFS_IOC_EXPIRE)
2073 COMPATIBLE_IOCTL(AUTOFS_IOC_EXPIRE_MULTI)
2074 COMPATIBLE_IOCTL(AUTOFS_IOC_PROTOSUBVER)
2075 -COMPATIBLE_IOCTL(AUTOFS_IOC_ASKREGHOST)
2076 -COMPATIBLE_IOCTL(AUTOFS_IOC_TOGGLEREGHOST)
2077 COMPATIBLE_IOCTL(AUTOFS_IOC_ASKUMOUNT)
2078 /* Raw devices */
2079 COMPATIBLE_IOCTL(RAW_SETBIND)
2080 --- /dev/null
2081 +++ linux-2.6.20/Documentation/filesystems/autofs4-mount-control.txt
2082 @@ -0,0 +1,414 @@
2083 +
2084 +Miscellaneous Device control operations for the autofs4 kernel module
2085 +====================================================================
2086 +
2087 +The problem
2088 +===========
2089 +
2090 +There is a problem with active restarts in autofs (that is to say
2091 +restarting autofs when there are busy mounts).
2092 +
2093 +During normal operation autofs uses a file descriptor opened on the
2094 +directory that is being managed in order to be able to issue control
2095 +operations. Using a file descriptor gives ioctl operations access to
2096 +autofs specific information stored in the super block. The operations
2097 +are things such as setting an autofs mount catatonic, setting the
2098 +expire timeout and requesting expire checks. As is explained below,
2099 +certain types of autofs triggered mounts can end up covering an autofs
2100 +mount itself which prevents us being able to use open(2) to obtain a
2101 +file descriptor for these operations if we don't already have one open.
2102 +
2103 +Currently autofs uses "umount -l" (lazy umount) to clear active mounts
2104 +at restart. While using lazy umount works for most cases, anything that
2105 +needs to walk back up the mount tree to construct a path, such as
2106 +getcwd(2) and the proc file system /proc/<pid>/cwd, no longer works
2107 +because the point from which the path is constructed has been detached
2108 +from the mount tree.
2109 +
2110 +The actual problem with autofs is that it can't reconnect to existing
2111 +mounts. Immediately one thinks of just adding the ability to remount
2112 +autofs file systems would solve it, but alas, that can't work. This is
2113 +because autofs direct mounts and the implementation of "on demand mount
2114 +and expire" of nested mount trees have the file system mounted directly
2115 +on top of the mount trigger directory dentry.
2116 +
2117 +For example, there are two types of automount maps, direct (in the kernel
2118 +module source you will see a third type called an offset, which is just
2119 +a direct mount in disguise) and indirect.
2120 +
2121 +Here is a master map with direct and indirect map entries:
2122 +
2123 +/- /etc/auto.direct
2124 +/test /etc/auto.indirect
2125 +
2126 +and the corresponding map files:
2127 +
2128 +/etc/auto.direct:
2129 +
2130 +/automount/dparse/g6 budgie:/autofs/export1
2131 +/automount/dparse/g1 shark:/autofs/export1
2132 +and so on.
2133 +
2134 +/etc/auto.indirect:
2135 +
2136 +g1 shark:/autofs/export1
2137 +g6 budgie:/autofs/export1
2138 +and so on.
2139 +
2140 +For the above indirect map an autofs file system is mounted on /test and
2141 +mounts are triggered for each sub-directory key by the inode lookup
2142 +operation. So we see a mount of shark:/autofs/export1 on /test/g1, for
2143 +example.
2144 +
2145 +The way that direct mounts are handled is by making an autofs mount on
2146 +each full path, such as /automount/dparse/g1, and using it as a mount
2147 +trigger. So when we walk on the path we mount shark:/autofs/export1 "on
2148 +top of this mount point". Since these are always directories we can
2149 +use the follow_link inode operation to trigger the mount.
2150 +
2151 +But, each entry in direct and indirect maps can have offsets (making
2152 +them multi-mount map entries).
2153 +
2154 +For example, an indirect mount map entry could also be:
2155 +
2156 +g1 \
2157 + / shark:/autofs/export5/testing/test \
2158 + /s1 shark:/autofs/export/testing/test/s1 \
2159 + /s2 shark:/autofs/export5/testing/test/s2 \
2160 + /s1/ss1 shark:/autofs/export1 \
2161 + /s2/ss2 shark:/autofs/export2
2162 +
2163 +and a similarly a direct mount map entry could also be:
2164 +
2165 +/automount/dparse/g1 \
2166 + / shark:/autofs/export5/testing/test \
2167 + /s1 shark:/autofs/export/testing/test/s1 \
2168 + /s2 shark:/autofs/export5/testing/test/s2 \
2169 + /s1/ss1 shark:/autofs/export2 \
2170 + /s2/ss2 shark:/autofs/export2
2171 +
2172 +One of the issues with version 4 of autofs was that, when mounting an
2173 +entry with a large number of offsets, possibly with nesting, we needed
2174 +to mount and umount all of the offsets as a single unit. Not really a
2175 +problem, except for people with a large number of offsets in map entries.
2176 +This mechanism is used for the well known "hosts" map and we have seen
2177 +cases (in 2.4) where the available number of mounts are exhausted or
2178 +where the number of privileged ports available is exhausted.
2179 +
2180 +In version 5 we mount only as we go down the tree of offsets and
2181 +similarly for expiring them which resolves the above problem. There is
2182 +somewhat more detail to the implementation but it isn't needed for the
2183 +sake of the problem explanation. The one important detail is that these
2184 +offsets are implemented using the same mechanism as the direct mounts
2185 +above and so the mount points can be covered by a mount.
2186 +
2187 +The current autofs implementation uses an ioctl file descriptor opened
2188 +on the mount point for control operations. The references held by the
2189 +descriptor are accounted for in checks made to determine if a mount is
2190 +in use and is also used to access autofs file system information held
2191 +in the mount super block. So the use of a file handle needs to be
2192 +retained.
2193 +
2194 +
2195 +The Solution
2196 +============
2197 +
2198 +To be able to restart autofs leaving existing direct, indirect and
2199 +offset mounts in place we need to be able to obtain a file handle
2200 +for these potentially covered autofs mount points. Rather than just
2201 +implement an isolated operation it was decided to re-implement the
2202 +existing ioctl interface and add new operations to provide this
2203 +functionality.
2204 +
2205 +In addition, to be able to reconstruct a mount tree that has busy mounts,
2206 +the uid and gid of the last user that triggered the mount needs to be
2207 +available because these can be used as macro substitution variables in
2208 +autofs maps. They are recorded at mount request time and an operation
2209 +has been added to retrieve them.
2210 +
2211 +Since we're re-implementing the control interface, a couple of other
2212 +problems with the existing interface have been addressed. First, when
2213 +a mount or expire operation completes a status is returned to the
2214 +kernel by either a "send ready" or a "send fail" operation. The
2215 +"send fail" operation of the ioctl interface could only ever send
2216 +ENOENT so the re-implementation allows user space to send an actual
2217 +status. Another expensive operation in user space, for those using
2218 +very large maps, is discovering if a mount is present. Usually this
2219 +involves scanning /proc/mounts and since it needs to be done quite
2220 +often it can introduce significant overhead when there are many entries
2221 +in the mount table. An operation to lookup the mount status of a mount
2222 +point dentry (covered or not) has also been added.
2223 +
2224 +Current kernel development policy recommends avoiding the use of the
2225 +ioctl mechanism in favor of systems such as Netlink. An implementation
2226 +using this system was attempted to evaluate its suitability and it was
2227 +found to be inadequate, in this case. The Generic Netlink system was
2228 +used for this as raw Netlink would lead to a significant increase in
2229 +complexity. There's no question that the Generic Netlink system is an
2230 +elegant solution for common case ioctl functions but it's not a complete
2231 +replacement probably because it's primary purpose in life is to be a
2232 +message bus implementation rather than specifically an ioctl replacement.
2233 +While it would be possible to work around this there is one concern
2234 +that lead to the decision to not use it. This is that the autofs
2235 +expire in the daemon has become far to complex because umount
2236 +candidates are enumerated, almost for no other reason than to "count"
2237 +the number of times to call the expire ioctl. This involves scanning
2238 +the mount table which has proved to be a big overhead for users with
2239 +large maps. The best way to improve this is try and get back to the
2240 +way the expire was done long ago. That is, when an expire request is
2241 +issued for a mount (file handle) we should continually call back to
2242 +the daemon until we can't umount any more mounts, then return the
2243 +appropriate status to the daemon. At the moment we just expire one
2244 +mount at a time. A Generic Netlink implementation would exclude this
2245 +possibility for future development due to the requirements of the
2246 +message bus architecture.
2247 +
2248 +
2249 +autofs4 Miscellaneous Device mount control interface
2250 +====================================================
2251 +
2252 +The control interface is opening a device node, typically /dev/autofs.
2253 +
2254 +All the ioctls use a common structure to pass the needed parameter
2255 +information and return operation results:
2256 +
2257 +struct autofs_dev_ioctl {
2258 + __u32 ver_major;
2259 + __u32 ver_minor;
2260 + __u32 size; /* total size of data passed in
2261 + * including this struct */
2262 + __s32 ioctlfd; /* automount command fd */
2263 +
2264 + /* Command parameters */
2265 +
2266 + union {
2267 + struct args_protover protover;
2268 + struct args_protosubver protosubver;
2269 + struct args_openmount openmount;
2270 + struct args_ready ready;
2271 + struct args_fail fail;
2272 + struct args_setpipefd setpipefd;
2273 + struct args_timeout timeout;
2274 + struct args_requester requester;
2275 + struct args_expire expire;
2276 + struct args_askumount askumount;
2277 + struct args_ismountpoint ismountpoint;
2278 + };
2279 +
2280 + char path[0];
2281 +};
2282 +
2283 +The ioctlfd field is a mount point file descriptor of an autofs mount
2284 +point. It is returned by the open call and is used by all calls except
2285 +the check for whether a given path is a mount point, where it may
2286 +optionally be used to check a specific mount corresponding to a given
2287 +mount point file descriptor, and when requesting the uid and gid of the
2288 +last successful mount on a directory within the autofs file system.
2289 +
2290 +The anonymous union is used to communicate parameters and results of calls
2291 +made as described below.
2292 +
2293 +The path field is used to pass a path where it is needed and the size field
2294 +is used account for the increased structure length when translating the
2295 +structure sent from user space.
2296 +
2297 +This structure can be initialized before setting specific fields by using
2298 +the void function call init_autofs_dev_ioctl(struct autofs_dev_ioctl *).
2299 +
2300 +All of the ioctls perform a copy of this structure from user space to
2301 +kernel space and return -EINVAL if the size parameter is smaller than
2302 +the structure size itself, -ENOMEM if the kernel memory allocation fails
2303 +or -EFAULT if the copy itself fails. Other checks include a version check
2304 +of the compiled in user space version against the module version and a
2305 +mismatch results in a -EINVAL return. If the size field is greater than
2306 +the structure size then a path is assumed to be present and is checked to
2307 +ensure it begins with a "/" and is NULL terminated, otherwise -EINVAL is
2308 +returned. Following these checks, for all ioctl commands except
2309 +AUTOFS_DEV_IOCTL_VERSION_CMD, AUTOFS_DEV_IOCTL_OPENMOUNT_CMD and
2310 +AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD the ioctlfd is validated and if it is
2311 +not a valid descriptor or doesn't correspond to an autofs mount point
2312 +an error of -EBADF, -ENOTTY or -EINVAL (not an autofs descriptor) is
2313 +returned.
2314 +
2315 +
2316 +The ioctls
2317 +==========
2318 +
2319 +An example of an implementation which uses this interface can be seen
2320 +in autofs version 5.0.4 and later in file lib/dev-ioctl-lib.c of the
2321 +distribution tar available for download from kernel.org in directory
2322 +/pub/linux/daemons/autofs/v5.
2323 +
2324 +The device node ioctl operations implemented by this interface are:
2325 +
2326 +
2327 +AUTOFS_DEV_IOCTL_VERSION
2328 +------------------------
2329 +
2330 +Get the major and minor version of the autofs4 device ioctl kernel module
2331 +implementation. It requires an initialized struct autofs_dev_ioctl as an
2332 +input parameter and sets the version information in the passed in structure.
2333 +It returns 0 on success or the error -EINVAL if a version mismatch is
2334 +detected.
2335 +
2336 +
2337 +AUTOFS_DEV_IOCTL_PROTOVER_CMD and AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD
2338 +------------------------------------------------------------------
2339 +
2340 +Get the major and minor version of the autofs4 protocol version understood
2341 +by loaded module. This call requires an initialized struct autofs_dev_ioctl
2342 +with the ioctlfd field set to a valid autofs mount point descriptor
2343 +and sets the requested version number in structure field protover.version
2344 +and ptotosubver.sub_version respectively. These commands return 0 on
2345 +success or one of the negative error codes if validation fails.
2346 +
2347 +
2348 +AUTOFS_DEV_IOCTL_OPENMOUNT_CMD and AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD
2349 +------------------------------------------------------------------
2350 +
2351 +Obtain and release a file descriptor for an autofs managed mount point
2352 +path. The open call requires an initialized struct autofs_dev_ioctl with
2353 +the the path field set and the size field adjusted appropriately as well
2354 +as the openmount.devid field set to the device number of the autofs mount.
2355 +The device number of an autofs mounted filesystem can be obtained by using
2356 +the AUTOFS_DEV_IOCTL_ISMOUNTPOINT ioctl function by providing the path
2357 +and autofs mount type, as described below. The close call requires an
2358 +initialized struct autofs_dev_ioct with the ioctlfd field set to the
2359 +descriptor obtained from the open call. The release of the file descriptor
2360 +can also be done with close(2) so any open descriptors will also be
2361 +closed at process exit. The close call is included in the implemented
2362 +operations largely for completeness and to provide for a consistent
2363 +user space implementation.
2364 +
2365 +
2366 +AUTOFS_DEV_IOCTL_READY_CMD and AUTOFS_DEV_IOCTL_FAIL_CMD
2367 +--------------------------------------------------------
2368 +
2369 +Return mount and expire result status from user space to the kernel.
2370 +Both of these calls require an initialized struct autofs_dev_ioctl
2371 +with the ioctlfd field set to the descriptor obtained from the open
2372 +call and the ready.token or fail.token field set to the wait queue
2373 +token number, received by user space in the foregoing mount or expire
2374 +request. The fail.status field is set to the status to be returned when
2375 +sending a failure notification with AUTOFS_DEV_IOCTL_FAIL_CMD.
2376 +
2377 +
2378 +AUTOFS_DEV_IOCTL_SETPIPEFD_CMD
2379 +------------------------------
2380 +
2381 +Set the pipe file descriptor used for kernel communication to the daemon.
2382 +Normally this is set at mount time using an option but when reconnecting
2383 +to a existing mount we need to use this to tell the autofs mount about
2384 +the new kernel pipe descriptor. In order to protect mounts against
2385 +incorrectly setting the pipe descriptor we also require that the autofs
2386 +mount be catatonic (see next call).
2387 +
2388 +The call requires an initialized struct autofs_dev_ioctl with the
2389 +ioctlfd field set to the descriptor obtained from the open call and
2390 +the setpipefd.pipefd field set to descriptor of the pipe. On success
2391 +the call also sets the process group id used to identify the controlling
2392 +process (eg. the owning automount(8) daemon) to the process group of
2393 +the caller.
2394 +
2395 +
2396 +AUTOFS_DEV_IOCTL_CATATONIC_CMD
2397 +------------------------------
2398 +
2399 +Make the autofs mount point catatonic. The autofs mount will no longer
2400 +issue mount requests, the kernel communication pipe descriptor is released
2401 +and any remaining waits in the queue released.
2402 +
2403 +The call requires an initialized struct autofs_dev_ioctl with the
2404 +ioctlfd field set to the descriptor obtained from the open call.
2405 +
2406 +
2407 +AUTOFS_DEV_IOCTL_TIMEOUT_CMD
2408 +----------------------------
2409 +
2410 +Set the expire timeout for mounts withing an autofs mount point.
2411 +
2412 +The call requires an initialized struct autofs_dev_ioctl with the
2413 +ioctlfd field set to the descriptor obtained from the open call.
2414 +The timeout.timeout field is set to the desired timeout and this
2415 +field is set to the value of the value of the current timeout of
2416 +the mount upon successful completion.
2417 +
2418 +
2419 +AUTOFS_DEV_IOCTL_REQUESTER_CMD
2420 +------------------------------
2421 +
2422 +Return the uid and gid of the last process to successfully trigger a the
2423 +mount on the given path dentry.
2424 +
2425 +The call requires an initialized struct autofs_dev_ioctl with the path
2426 +field set to the mount point in question and the size field adjusted
2427 +appropriately as well as the ioctlfd field set to the descriptor obtained
2428 +from the open call. Upon return the struct fields requester.uid and
2429 +requester.gid contain the uid and gid respectively.
2430 +
2431 +When reconstructing an autofs mount tree with active mounts we need to
2432 +re-connect to mounts that may have used the original process uid and
2433 +gid (or string variations of them) for mount lookups within the map entry.
2434 +This call provides the ability to obtain this uid and gid so they may be
2435 +used by user space for the mount map lookups.
2436 +
2437 +
2438 +AUTOFS_DEV_IOCTL_EXPIRE_CMD
2439 +---------------------------
2440 +
2441 +Issue an expire request to the kernel for an autofs mount. Typically
2442 +this ioctl is called until no further expire candidates are found.
2443 +
2444 +The call requires an initialized struct autofs_dev_ioctl with the
2445 +ioctlfd field set to the descriptor obtained from the open call. In
2446 +addition an immediate expire, independent of the mount timeout, can be
2447 +requested by setting the expire.how field to 1. If no expire candidates
2448 +can be found the ioctl returns -1 with errno set to EAGAIN.
2449 +
2450 +This call causes the kernel module to check the mount corresponding
2451 +to the given ioctlfd for mounts that can be expired, issues an expire
2452 +request back to the daemon and waits for completion.
2453 +
2454 +AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD
2455 +------------------------------
2456 +
2457 +Checks if an autofs mount point is in use.
2458 +
2459 +The call requires an initialized struct autofs_dev_ioctl with the
2460 +ioctlfd field set to the descriptor obtained from the open call and
2461 +it returns the result in the askumount.may_umount field, 1 for busy
2462 +and 0 otherwise.
2463 +
2464 +
2465 +AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
2466 +---------------------------------
2467 +
2468 +Check if the given path is a mountpoint.
2469 +
2470 +The call requires an initialized struct autofs_dev_ioctl. There are two
2471 +possible variations. Both use the path field set to the path of the mount
2472 +point to check and the size field must be adjusted appropriately. One uses
2473 +the ioctlfd field to identify a specific mount point to check while the
2474 +other variation uses the path and optionaly the ismountpoint.in.type
2475 +field set to an autofs mount type. The call returns 1 if this is a mount
2476 +point and sets the ismountpoint.out.devid field to the device number of
2477 +the mount and the ismountpoint.out.magic field to the relevant super
2478 +block magic number (described below) or 0 if it isn't a mountpoint. In
2479 +both cases the the device number (as returned by new_encode_dev()) is
2480 +returned in the ismountpoint.out.devid field.
2481 +
2482 +If supplied with a file descriptor we're looking for a specific mount,
2483 +not necessarily at the top of the mounted stack. In this case the path
2484 +the descriptor corresponds to is considered a mountpoint if it is itself
2485 +a mountpoint or contains a mount, such as a multi-mount without a root
2486 +mount. In this case we return 1 if the descriptor corresponds to a mount
2487 +point and and also returns the super magic of the covering mount if there
2488 +is one or 0 if it isn't a mountpoint.
2489 +
2490 +If a path is supplied (and the ioctlfd field is set to -1) then the path
2491 +is looked up and is checked to see if it is the root of a mount. If a
2492 +type is also given we are looking for a particular autofs mount and if
2493 +a match isn't found a fail is returned. If the the located path is the
2494 +root of a mount 1 is returned along with the super magic of the mount
2495 +or 0 otherwise.
2496 +
2497 --- linux-2.6.20.orig/fs/autofs4/Makefile
2498 +++ linux-2.6.20/fs/autofs4/Makefile
2499 @@ -4,4 +4,4 @@
2500
2501 obj-$(CONFIG_AUTOFS4_FS) += autofs4.o
2502
2503 -autofs4-objs := init.o inode.o root.o symlink.o waitq.o expire.o
2504 +autofs4-objs := init.o inode.o root.o symlink.o waitq.o expire.o dev-ioctl.o
2505 --- /dev/null
2506 +++ linux-2.6.20/fs/autofs4/dev-ioctl.c
2507 @@ -0,0 +1,840 @@
2508 +/*
2509 + * Copyright 2008 Red Hat, Inc. All rights reserved.
2510 + * Copyright 2008 Ian Kent <[email protected]>
2511 + *
2512 + * This file is part of the Linux kernel and is made available under
2513 + * the terms of the GNU General Public License, version 2, or at your
2514 + * option, any later version, incorporated herein by reference.
2515 + */
2516 +
2517 +#include <linux/module.h>
2518 +#include <linux/vmalloc.h>
2519 +#include <linux/miscdevice.h>
2520 +#include <linux/init.h>
2521 +#include <linux/wait.h>
2522 +#include <linux/namei.h>
2523 +#include <linux/fcntl.h>
2524 +#include <linux/file.h>
2525 +#include <linux/sched.h>
2526 +#include <linux/compat.h>
2527 +#include <linux/syscalls.h>
2528 +#include <linux/smp_lock.h>
2529 +#include <linux/magic.h>
2530 +#include <linux/dcache.h>
2531 +#include <linux/uaccess.h>
2532 +
2533 +#include "autofs_i.h"
2534 +
2535 +/*
2536 + * This module implements an interface for routing autofs ioctl control
2537 + * commands via a miscellaneous device file.
2538 + *
2539 + * The alternate interface is needed because we need to be able open
2540 + * an ioctl file descriptor on an autofs mount that may be covered by
2541 + * another mount. This situation arises when starting automount(8)
2542 + * or other user space daemon which uses direct mounts or offset
2543 + * mounts (used for autofs lazy mount/umount of nested mount trees),
2544 + * which have been left busy at at service shutdown.
2545 + */
2546 +
2547 +#define AUTOFS_DEV_IOCTL_SIZE sizeof(struct autofs_dev_ioctl)
2548 +
2549 +typedef int (*ioctl_fn)(struct file *,
2550 +struct autofs_sb_info *, struct autofs_dev_ioctl *);
2551 +
2552 +static int check_name(const char *name)
2553 +{
2554 + if (!strchr(name, '/'))
2555 + return -EINVAL;
2556 + return 0;
2557 +}
2558 +
2559 +/*
2560 + * Check a string doesn't overrun the chunk of
2561 + * memory we copied from user land.
2562 + */
2563 +static int invalid_str(char *str, void *end)
2564 +{
2565 + while ((void *) str <= end)
2566 + if (!*str++)
2567 + return 0;
2568 + return -EINVAL;
2569 +}
2570 +
2571 +/*
2572 + * Check that the user compiled against correct version of autofs
2573 + * misc device code.
2574 + *
2575 + * As well as checking the version compatibility this always copies
2576 + * the kernel interface version out.
2577 + */
2578 +static int check_dev_ioctl_version(int cmd, struct autofs_dev_ioctl *param)
2579 +{
2580 + int err = 0;
2581 +
2582 + if ((AUTOFS_DEV_IOCTL_VERSION_MAJOR != param->ver_major) ||
2583 + (AUTOFS_DEV_IOCTL_VERSION_MINOR < param->ver_minor)) {
2584 + AUTOFS_WARN("ioctl control interface version mismatch: "
2585 + "kernel(%u.%u), user(%u.%u), cmd(%d)",
2586 + AUTOFS_DEV_IOCTL_VERSION_MAJOR,
2587 + AUTOFS_DEV_IOCTL_VERSION_MINOR,
2588 + param->ver_major, param->ver_minor, cmd);
2589 + err = -EINVAL;
2590 + }
2591 +
2592 + /* Fill in the kernel version. */
2593 + param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
2594 + param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
2595 +
2596 + return err;
2597 +}
2598 +
2599 +/*
2600 + * Copy parameter control struct, including a possible path allocated
2601 + * at the end of the struct.
2602 + */
2603 +static struct autofs_dev_ioctl *copy_dev_ioctl(struct autofs_dev_ioctl __user *in)
2604 +{
2605 + struct autofs_dev_ioctl tmp, *ads;
2606 +
2607 + if (copy_from_user(&tmp, in, sizeof(tmp)))
2608 + return ERR_PTR(-EFAULT);
2609 +
2610 + if (tmp.size < sizeof(tmp))
2611 + return ERR_PTR(-EINVAL);
2612 +
2613 + ads = kmalloc(tmp.size, GFP_KERNEL);
2614 + if (!ads)
2615 + return ERR_PTR(-ENOMEM);
2616 +
2617 + if (copy_from_user(ads, in, tmp.size)) {
2618 + kfree(ads);
2619 + return ERR_PTR(-EFAULT);
2620 + }
2621 +
2622 + return ads;
2623 +}
2624 +
2625 +static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
2626 +{
2627 + kfree(param);
2628 + return;
2629 +}
2630 +
2631 +/*
2632 + * Check sanity of parameter control fields and if a path is present
2633 + * check that it is terminated and contains at least one "/".
2634 + */
2635 +static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param)
2636 +{
2637 + int err;
2638 +
2639 + if ((err = check_dev_ioctl_version(cmd, param))) {
2640 + AUTOFS_WARN("invalid device control module version "
2641 + "supplied for cmd(0x%08x)", cmd);
2642 + goto out;
2643 + }
2644 +
2645 + if (param->size > sizeof(*param)) {
2646 + err = invalid_str(param->path,
2647 + (void *) ((size_t) param + param->size));
2648 + if (err) {
2649 + AUTOFS_WARN(
2650 + "path string terminator missing for cmd(0x%08x)",
2651 + cmd);
2652 + goto out;
2653 + }
2654 +
2655 + err = check_name(param->path);
2656 + if (err) {
2657 + AUTOFS_WARN("invalid path supplied for cmd(0x%08x)",
2658 + cmd);
2659 + goto out;
2660 + }
2661 + }
2662 +
2663 + err = 0;
2664 +out:
2665 + return err;
2666 +}
2667 +
2668 +/*
2669 + * Get the autofs super block info struct from the file opened on
2670 + * the autofs mount point.
2671 + */
2672 +static struct autofs_sb_info *autofs_dev_ioctl_sbi(struct file *f)
2673 +{
2674 + struct autofs_sb_info *sbi = NULL;
2675 + struct inode *inode;
2676 +
2677 + if (f) {
2678 + inode = f->f_path.dentry->d_inode;
2679 + sbi = autofs4_sbi(inode->i_sb);
2680 + }
2681 + return sbi;
2682 +}
2683 +
2684 +/* Return autofs module protocol version */
2685 +static int autofs_dev_ioctl_protover(struct file *fp,
2686 + struct autofs_sb_info *sbi,
2687 + struct autofs_dev_ioctl *param)
2688 +{
2689 + param->protover.version = sbi->version;
2690 + return 0;
2691 +}
2692 +
2693 +/* Return autofs module protocol sub version */
2694 +static int autofs_dev_ioctl_protosubver(struct file *fp,
2695 + struct autofs_sb_info *sbi,
2696 + struct autofs_dev_ioctl *param)
2697 +{
2698 + param->protosubver.sub_version = sbi->sub_version;
2699 + return 0;
2700 +}
2701 +
2702 +/*
2703 + * Walk down the mount stack looking for an autofs mount that
2704 + * has the requested device number (aka. new_encode_dev(sb->s_dev).
2705 + */
2706 +static int autofs_dev_ioctl_find_super(struct nameidata *nd, dev_t devno)
2707 +{
2708 + struct dentry *dentry;
2709 + struct inode *inode;
2710 + struct super_block *sb;
2711 + dev_t s_dev;
2712 + unsigned int err;
2713 +
2714 + err = -ENOENT;
2715 +
2716 + /* Lookup the dentry name at the base of our mount point */
2717 + dentry = d_lookup(nd->dentry, &nd->last);
2718 + if (!dentry)
2719 + goto out;
2720 +
2721 + dput(nd->dentry);
2722 + nd->dentry = dentry;
2723 +
2724 + /* And follow the mount stack looking for our autofs mount */
2725 + while (follow_down(&nd->mnt, &nd->dentry)) {
2726 + inode = nd->dentry->d_inode;
2727 + if (!inode)
2728 + break;
2729 +
2730 + sb = inode->i_sb;
2731 + s_dev = new_encode_dev(sb->s_dev);
2732 + if (devno == s_dev) {
2733 + if (sb->s_magic == AUTOFS_SUPER_MAGIC) {
2734 + err = 0;
2735 + break;
2736 + }
2737 + }
2738 + }
2739 +out:
2740 + return err;
2741 +}
2742 +
2743 +/*
2744 + * Walk down the mount stack looking for an autofs mount that
2745 + * has the requested mount type (ie. indirect, direct or offset).
2746 + */
2747 +static int autofs_dev_ioctl_find_sbi_type(struct nameidata *nd, unsigned int type)
2748 +{
2749 + struct dentry *dentry;
2750 + struct autofs_info *ino;
2751 + unsigned int err;
2752 +
2753 + err = -ENOENT;
2754 +
2755 + /* Lookup the dentry name at the base of our mount point */
2756 + dentry = d_lookup(nd->dentry, &nd->last);
2757 + if (!dentry)
2758 + goto out;
2759 +
2760 + dput(nd->dentry);
2761 + nd->dentry = dentry;
2762 +
2763 + /* And follow the mount stack looking for our autofs mount */
2764 + while (follow_down(&nd->mnt, &nd->dentry)) {
2765 + ino = autofs4_dentry_ino(nd->dentry);
2766 + if (ino && ino->sbi->type & type) {
2767 + err = 0;
2768 + break;
2769 + }
2770 + }
2771 +out:
2772 + return err;
2773 +}
2774 +
2775 +static void autofs_dev_ioctl_fd_install(unsigned int fd, struct file *file)
2776 +{
2777 + struct files_struct *files = current->files;
2778 + struct fdtable *fdt;
2779 +
2780 + spin_lock(&files->file_lock);
2781 + fdt = files_fdtable(files);
2782 + BUG_ON(fdt->fd[fd] != NULL);
2783 + rcu_assign_pointer(fdt->fd[fd], file);
2784 + FD_SET(fd, fdt->close_on_exec);
2785 + spin_unlock(&files->file_lock);
2786 +}
2787 +
2788 +
2789 +/*
2790 + * Open a file descriptor on the autofs mount point corresponding
2791 + * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
2792 + */
2793 +static int autofs_dev_ioctl_open_mountpoint(const char *path, dev_t devid)
2794 +{
2795 + struct file *filp;
2796 + struct nameidata nd;
2797 + int err, fd;
2798 +
2799 + fd = get_unused_fd();
2800 + if (likely(fd >= 0)) {
2801 + /* Get nameidata of the parent directory */
2802 + err = path_lookup(path, LOOKUP_PARENT, &nd);
2803 + if (err)
2804 + goto out;
2805 +
2806 + /*
2807 + * Search down, within the parent, looking for an
2808 + * autofs super block that has the device number
2809 + * corresponding to the autofs fs we want to open.
2810 + */
2811 + err = autofs_dev_ioctl_find_super(&nd, devid);
2812 + if (err) {
2813 + path_release(&nd);
2814 + goto out;
2815 + }
2816 +
2817 + filp = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
2818 + if (IS_ERR(filp)) {
2819 + err = PTR_ERR(filp);
2820 + goto out;
2821 + }
2822 +
2823 + autofs_dev_ioctl_fd_install(fd, filp);
2824 + }
2825 +
2826 + return fd;
2827 +
2828 +out:
2829 + put_unused_fd(fd);
2830 + return err;
2831 +}
2832 +
2833 +/* Open a file descriptor on an autofs mount point */
2834 +static int autofs_dev_ioctl_openmount(struct file *fp,
2835 + struct autofs_sb_info *sbi,
2836 + struct autofs_dev_ioctl *param)
2837 +{
2838 + const char *path;
2839 + dev_t devid;
2840 + int err, fd;
2841 +
2842 + /* param->path has already been checked */
2843 + if (!param->openmount.devid)
2844 + return -EINVAL;
2845 +
2846 + param->ioctlfd = -1;
2847 +
2848 + path = param->path;
2849 + devid = param->openmount.devid;
2850 +
2851 + err = 0;
2852 + fd = autofs_dev_ioctl_open_mountpoint(path, devid);
2853 + if (unlikely(fd < 0)) {
2854 + err = fd;
2855 + goto out;
2856 + }
2857 +
2858 + param->ioctlfd = fd;
2859 +out:
2860 + return err;
2861 +}
2862 +
2863 +/* Close file descriptor allocated above (user can also use close(2)). */
2864 +static int autofs_dev_ioctl_closemount(struct file *fp,
2865 + struct autofs_sb_info *sbi,
2866 + struct autofs_dev_ioctl *param)
2867 +{
2868 + return sys_close(param->ioctlfd);
2869 +}
2870 +
2871 +/*
2872 + * Send "ready" status for an existing wait (either a mount or an expire
2873 + * request).
2874 + */
2875 +static int autofs_dev_ioctl_ready(struct file *fp,
2876 + struct autofs_sb_info *sbi,
2877 + struct autofs_dev_ioctl *param)
2878 +{
2879 + autofs_wqt_t token;
2880 +
2881 + token = (autofs_wqt_t) param->ready.token;
2882 + return autofs4_wait_release(sbi, token, 0);
2883 +}
2884 +
2885 +/*
2886 + * Send "fail" status for an existing wait (either a mount or an expire
2887 + * request).
2888 + */
2889 +static int autofs_dev_ioctl_fail(struct file *fp,
2890 + struct autofs_sb_info *sbi,
2891 + struct autofs_dev_ioctl *param)
2892 +{
2893 + autofs_wqt_t token;
2894 + int status;
2895 +
2896 + token = (autofs_wqt_t) param->fail.token;
2897 + status = param->fail.status ? param->fail.status : -ENOENT;
2898 + return autofs4_wait_release(sbi, token, status);
2899 +}
2900 +
2901 +/*
2902 + * Set the pipe fd for kernel communication to the daemon.
2903 + *
2904 + * Normally this is set at mount using an option but if we
2905 + * are reconnecting to a busy mount then we need to use this
2906 + * to tell the autofs mount about the new kernel pipe fd. In
2907 + * order to protect mounts against incorrectly setting the
2908 + * pipefd we also require that the autofs mount be catatonic.
2909 + *
2910 + * This also sets the process group id used to identify the
2911 + * controlling process (eg. the owning automount(8) daemon).
2912 + */
2913 +static int autofs_dev_ioctl_setpipefd(struct file *fp,
2914 + struct autofs_sb_info *sbi,
2915 + struct autofs_dev_ioctl *param)
2916 +{
2917 + int pipefd;
2918 + int err = 0;
2919 +
2920 + if (param->setpipefd.pipefd == -1)
2921 + return -EINVAL;
2922 +
2923 + pipefd = param->setpipefd.pipefd;
2924 +
2925 + mutex_lock(&sbi->wq_mutex);
2926 + if (!sbi->catatonic) {
2927 + mutex_unlock(&sbi->wq_mutex);
2928 + return -EBUSY;
2929 + } else {
2930 + struct file *pipe = fget(pipefd);
2931 + if (!pipe->f_op || !pipe->f_op->write) {
2932 + err = -EPIPE;
2933 + fput(pipe);
2934 + goto out;
2935 + }
2936 + sbi->oz_pgrp = process_group(current);
2937 + sbi->pipefd = pipefd;
2938 + sbi->pipe = pipe;
2939 + sbi->catatonic = 0;
2940 + }
2941 +out:
2942 + mutex_unlock(&sbi->wq_mutex);
2943 + return err;
2944 +}
2945 +
2946 +/*
2947 + * Make the autofs mount point catatonic, no longer responsive to
2948 + * mount requests. Also closes the kernel pipe file descriptor.
2949 + */
2950 +static int autofs_dev_ioctl_catatonic(struct file *fp,
2951 + struct autofs_sb_info *sbi,
2952 + struct autofs_dev_ioctl *param)
2953 +{
2954 + autofs4_catatonic_mode(sbi);
2955 + return 0;
2956 +}
2957 +
2958 +/* Set the autofs mount timeout */
2959 +static int autofs_dev_ioctl_timeout(struct file *fp,
2960 + struct autofs_sb_info *sbi,
2961 + struct autofs_dev_ioctl *param)
2962 +{
2963 + unsigned long timeout;
2964 +
2965 + timeout = param->timeout.timeout;
2966 + param->timeout.timeout = sbi->exp_timeout / HZ;
2967 + sbi->exp_timeout = timeout * HZ;
2968 + return 0;
2969 +}
2970 +
2971 +/*
2972 + * Return the uid and gid of the last request for the mount
2973 + *
2974 + * When reconstructing an autofs mount tree with active mounts
2975 + * we need to re-connect to mounts that may have used the original
2976 + * process uid and gid (or string variations of them) for mount
2977 + * lookups within the map entry.
2978 + */
2979 +static int autofs_dev_ioctl_requester(struct file *fp,
2980 + struct autofs_sb_info *sbi,
2981 + struct autofs_dev_ioctl *param)
2982 +{
2983 + struct autofs_info *ino;
2984 + struct nameidata nd;
2985 + const char *path;
2986 + dev_t devid;
2987 + int err = -ENOENT;
2988 +
2989 + if (param->size <= sizeof(*param)) {
2990 + err = -EINVAL;
2991 + goto out;
2992 + }
2993 +
2994 + path = param->path;
2995 + devid = sbi->sb->s_dev;
2996 +
2997 + param->requester.uid = param->requester.gid = -1;
2998 +
2999 + /* Get nameidata of the parent directory */
3000 + err = path_lookup(path, LOOKUP_PARENT, &nd);
3001 + if (err)
3002 + goto out;
3003 +
3004 + err = autofs_dev_ioctl_find_super(&nd, devid);
3005 + if (err)
3006 + goto out_release;
3007 +
3008 + ino = autofs4_dentry_ino(nd.dentry);
3009 + if (ino) {
3010 + err = 0;
3011 + autofs4_expire_wait(nd.dentry);
3012 + spin_lock(&sbi->fs_lock);
3013 + param->requester.uid = ino->uid;
3014 + param->requester.gid = ino->gid;
3015 + spin_unlock(&sbi->fs_lock);
3016 + }
3017 +
3018 +out_release:
3019 + path_release(&nd);
3020 +out:
3021 + return err;
3022 +}
3023 +
3024 +/*
3025 + * Call repeatedly until it returns -EAGAIN, meaning there's nothing
3026 + * more that can be done.
3027 + */
3028 +static int autofs_dev_ioctl_expire(struct file *fp,
3029 + struct autofs_sb_info *sbi,
3030 + struct autofs_dev_ioctl *param)
3031 +{
3032 + struct vfsmount *mnt;
3033 + int how;
3034 +
3035 + how = param->expire.how;
3036 + mnt = fp->f_path.mnt;
3037 +
3038 + return autofs4_do_expire_multi(sbi->sb, mnt, sbi, how);
3039 +}
3040 +
3041 +/* Check if autofs mount point is in use */
3042 +static int autofs_dev_ioctl_askumount(struct file *fp,
3043 + struct autofs_sb_info *sbi,
3044 + struct autofs_dev_ioctl *param)
3045 +{
3046 + param->askumount.may_umount = 0;
3047 + if (may_umount(fp->f_path.mnt))
3048 + param->askumount.may_umount = 1;
3049 + return 0;
3050 +}
3051 +
3052 +/*
3053 + * Check if the given path is a mountpoint.
3054 + *
3055 + * If we are supplied with the file descriptor of an autofs
3056 + * mount we're looking for a specific mount. In this case
3057 + * the path is considered a mountpoint if it is itself a
3058 + * mountpoint or contains a mount, such as a multi-mount
3059 + * without a root mount. In this case we return 1 if the
3060 + * path is a mount point and the super magic of the covering
3061 + * mount if there is one or 0 if it isn't a mountpoint.
3062 + *
3063 + * If we aren't supplied with a file descriptor then we
3064 + * lookup the nameidata of the path and check if it is the
3065 + * root of a mount. If a type is given we are looking for
3066 + * a particular autofs mount and if we don't find a match
3067 + * we return fail. If the located nameidata path is the
3068 + * root of a mount we return 1 along with the super magic
3069 + * of the mount or 0 otherwise.
3070 + *
3071 + * In both cases the the device number (as returned by
3072 + * new_encode_dev()) is also returned.
3073 + */
3074 +static int autofs_dev_ioctl_ismountpoint(struct file *fp,
3075 + struct autofs_sb_info *sbi,
3076 + struct autofs_dev_ioctl *param)
3077 +{
3078 + struct nameidata nd;
3079 + const char *path;
3080 + unsigned int type;
3081 + unsigned int devid, magic;
3082 + int err = -ENOENT;
3083 +
3084 + if (param->size <= sizeof(*param)) {
3085 + err = -EINVAL;
3086 + goto out;
3087 + }
3088 +
3089 + path = param->path;
3090 + type = param->ismountpoint.in.type;
3091 +
3092 + param->ismountpoint.out.devid = devid = 0;
3093 + param->ismountpoint.out.magic = magic = 0;
3094 +
3095 + if (!fp || param->ioctlfd == -1) {
3096 + if (autofs_type_any(type)) {
3097 + struct super_block *sb;
3098 +
3099 + err = path_lookup(path, LOOKUP_FOLLOW, &nd);
3100 + if (err)
3101 + goto out;
3102 +
3103 + sb = nd.dentry->d_sb;
3104 + devid = new_encode_dev(sb->s_dev);
3105 + } else {
3106 + struct autofs_info *ino;
3107 +
3108 + err = path_lookup(path, LOOKUP_PARENT, &nd);
3109 + if (err)
3110 + goto out;
3111 +
3112 + err = autofs_dev_ioctl_find_sbi_type(&nd, type);
3113 + if (err)
3114 + goto out_release;
3115 +
3116 + ino = autofs4_dentry_ino(nd.dentry);
3117 + devid = autofs4_get_dev(ino->sbi);
3118 + }
3119 +
3120 + err = 0;
3121 + if (nd.dentry->d_inode &&
3122 + nd.mnt->mnt_root == nd.dentry) {
3123 + err = 1;
3124 + magic = nd.dentry->d_inode->i_sb->s_magic;
3125 + }
3126 + } else {
3127 + dev_t dev = autofs4_get_dev(sbi);
3128 +
3129 + err = path_lookup(path, LOOKUP_PARENT, &nd);
3130 + if (err)
3131 + goto out;
3132 +
3133 + err = autofs_dev_ioctl_find_super(&nd, dev);
3134 + if (err)
3135 + goto out_release;
3136 +
3137 + devid = dev;
3138 +
3139 + err = have_submounts(nd.dentry);
3140 +
3141 + if (nd.mnt->mnt_mountpoint != nd.mnt->mnt_root) {
3142 + if (follow_down(&nd.mnt, &nd.dentry)) {
3143 + struct inode *inode = nd.dentry->d_inode;
3144 + magic = inode->i_sb->s_magic;
3145 + }
3146 + }
3147 + }
3148 +
3149 + param->ismountpoint.out.devid = devid;
3150 + param->ismountpoint.out.magic = magic;
3151 +
3152 +out_release:
3153 + path_release(&nd);
3154 +out:
3155 + return err;
3156 +}
3157 +
3158 +/*
3159 + * Our range of ioctl numbers isn't 0 based so we need to shift
3160 + * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
3161 + * lookup.
3162 + */
3163 +#define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
3164 +
3165 +static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
3166 +{
3167 + static struct {
3168 + int cmd;
3169 + ioctl_fn fn;
3170 + } _ioctls[] = {
3171 + {cmd_idx(AUTOFS_DEV_IOCTL_VERSION_CMD), NULL},
3172 + {cmd_idx(AUTOFS_DEV_IOCTL_PROTOVER_CMD),
3173 + autofs_dev_ioctl_protover},
3174 + {cmd_idx(AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD),
3175 + autofs_dev_ioctl_protosubver},
3176 + {cmd_idx(AUTOFS_DEV_IOCTL_OPENMOUNT_CMD),
3177 + autofs_dev_ioctl_openmount},
3178 + {cmd_idx(AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD),
3179 + autofs_dev_ioctl_closemount},
3180 + {cmd_idx(AUTOFS_DEV_IOCTL_READY_CMD),
3181 + autofs_dev_ioctl_ready},
3182 + {cmd_idx(AUTOFS_DEV_IOCTL_FAIL_CMD),
3183 + autofs_dev_ioctl_fail},
3184 + {cmd_idx(AUTOFS_DEV_IOCTL_SETPIPEFD_CMD),
3185 + autofs_dev_ioctl_setpipefd},
3186 + {cmd_idx(AUTOFS_DEV_IOCTL_CATATONIC_CMD),
3187 + autofs_dev_ioctl_catatonic},
3188 + {cmd_idx(AUTOFS_DEV_IOCTL_TIMEOUT_CMD),
3189 + autofs_dev_ioctl_timeout},
3190 + {cmd_idx(AUTOFS_DEV_IOCTL_REQUESTER_CMD),
3191 + autofs_dev_ioctl_requester},
3192 + {cmd_idx(AUTOFS_DEV_IOCTL_EXPIRE_CMD),
3193 + autofs_dev_ioctl_expire},
3194 + {cmd_idx(AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD),
3195 + autofs_dev_ioctl_askumount},
3196 + {cmd_idx(AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD),
3197 + autofs_dev_ioctl_ismountpoint}
3198 + };
3199 + unsigned int idx = cmd_idx(cmd);
3200 +
3201 + return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx].fn;
3202 +}
3203 +
3204 +/* ioctl dispatcher */
3205 +static int _autofs_dev_ioctl(unsigned int command, struct autofs_dev_ioctl __user *user)
3206 +{
3207 + struct autofs_dev_ioctl *param;
3208 + struct file *fp;
3209 + struct autofs_sb_info *sbi;
3210 + unsigned int cmd_first, cmd;
3211 + ioctl_fn fn = NULL;
3212 + int err = 0;
3213 +
3214 + /* only root can play with this */
3215 + if (!capable(CAP_SYS_ADMIN))
3216 + return -EPERM;
3217 +
3218 + cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
3219 + cmd = _IOC_NR(command);
3220 +
3221 + if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
3222 + cmd - cmd_first >= AUTOFS_DEV_IOCTL_IOC_COUNT) {
3223 + return -ENOTTY;
3224 + }
3225 +
3226 + /* Copy the parameters into kernel space. */
3227 + param = copy_dev_ioctl(user);
3228 + if (IS_ERR(param))
3229 + return PTR_ERR(param);
3230 +
3231 + err = validate_dev_ioctl(command, param);
3232 + if (err)
3233 + goto out;
3234 +
3235 + /* The validate routine above always sets the version */
3236 + if (cmd == AUTOFS_DEV_IOCTL_VERSION_CMD)
3237 + goto done;
3238 +
3239 + fn = lookup_dev_ioctl(cmd);
3240 + if (!fn) {
3241 + AUTOFS_WARN("unknown command 0x%08x", command);
3242 + return -ENOTTY;
3243 + }
3244 +
3245 + fp = NULL;
3246 + sbi = NULL;
3247 +
3248 + /*
3249 + * For obvious reasons the openmount can't have a file
3250 + * descriptor yet. We don't take a reference to the
3251 + * file during close to allow for immediate release.
3252 + */
3253 + if (cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
3254 + cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
3255 + fp = fget(param->ioctlfd);
3256 + if (!fp) {
3257 + if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
3258 + goto cont;
3259 + err = -EBADF;
3260 + goto out;
3261 + }
3262 +
3263 + if (!fp->f_op) {
3264 + err = -ENOTTY;
3265 + fput(fp);
3266 + goto out;
3267 + }
3268 +
3269 + sbi = autofs_dev_ioctl_sbi(fp);
3270 + if (!sbi || sbi->magic != AUTOFS_SBI_MAGIC) {
3271 + err = -EINVAL;
3272 + fput(fp);
3273 + goto out;
3274 + }
3275 +
3276 + /*
3277 + * Admin needs to be able to set the mount catatonic in
3278 + * order to be able to perform the re-open.
3279 + */
3280 + if (!autofs4_oz_mode(sbi) &&
3281 + cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
3282 + err = -EACCES;
3283 + fput(fp);
3284 + goto out;
3285 + }
3286 + }
3287 +cont:
3288 + err = fn(fp, sbi, param);
3289 +
3290 + if (fp)
3291 + fput(fp);
3292 +done:
3293 + if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
3294 + err = -EFAULT;
3295 +out:
3296 + free_dev_ioctl(param);
3297 + return err;
3298 +}
3299 +
3300 +static long autofs_dev_ioctl(struct file *file, uint command, ulong u)
3301 +{
3302 + int err;
3303 + err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
3304 + return (long) err;
3305 +}
3306 +
3307 +#ifdef CONFIG_COMPAT
3308 +static long autofs_dev_ioctl_compat(struct file *file, uint command, ulong u)
3309 +{
3310 + return (long) autofs_dev_ioctl(file, command, (ulong) compat_ptr(u));
3311 +}
3312 +#else
3313 +#define autofs_dev_ioctl_compat NULL
3314 +#endif
3315 +
3316 +static const struct file_operations _dev_ioctl_fops = {
3317 + .unlocked_ioctl = autofs_dev_ioctl,
3318 + .compat_ioctl = autofs_dev_ioctl_compat,
3319 + .owner = THIS_MODULE,
3320 +};
3321 +
3322 +static struct miscdevice _autofs_dev_ioctl_misc = {
3323 + .minor = MISC_DYNAMIC_MINOR,
3324 + .name = AUTOFS_DEVICE_NAME,
3325 + .fops = &_dev_ioctl_fops
3326 +};
3327 +
3328 +/* Register/deregister misc character device */
3329 +int autofs_dev_ioctl_init(void)
3330 +{
3331 + int r;
3332 +
3333 + r = misc_register(&_autofs_dev_ioctl_misc);
3334 + if (r) {
3335 + AUTOFS_ERROR("misc_register failed for control device");
3336 + return r;
3337 + }
3338 +
3339 + return 0;
3340 +}
3341 +
3342 +void autofs_dev_ioctl_exit(void)
3343 +{
3344 + misc_deregister(&_autofs_dev_ioctl_misc);
3345 + return;
3346 +}
3347 +
3348 --- linux-2.6.20.orig/fs/autofs4/init.c
3349 +++ linux-2.6.20/fs/autofs4/init.c
3350 @@ -29,11 +29,20 @@ static struct file_system_type autofs_fs
3351
3352 static int __init init_autofs4_fs(void)
3353 {
3354 - return register_filesystem(&autofs_fs_type);
3355 + int err;
3356 +
3357 + err = register_filesystem(&autofs_fs_type);
3358 + if (err)
3359 + return err;
3360 +
3361 + autofs_dev_ioctl_init();
3362 +
3363 + return err;
3364 }
3365
3366 static void __exit exit_autofs4_fs(void)
3367 {
3368 + autofs_dev_ioctl_exit();
3369 unregister_filesystem(&autofs_fs_type);
3370 }
3371
3372 --- /dev/null
3373 +++ linux-2.6.20/include/linux/auto_dev-ioctl.h
3374 @@ -0,0 +1,229 @@
3375 +/*
3376 + * Copyright 2008 Red Hat, Inc. All rights reserved.
3377 + * Copyright 2008 Ian Kent <[email protected]>
3378 + *
3379 + * This file is part of the Linux kernel and is made available under
3380 + * the terms of the GNU General Public License, version 2, or at your
3381 + * option, any later version, incorporated herein by reference.
3382 + */
3383 +
3384 +#ifndef _LINUX_AUTO_DEV_IOCTL_H
3385 +#define _LINUX_AUTO_DEV_IOCTL_H
3386 +
3387 +#include <linux/auto_fs.h>
3388 +
3389 +#ifdef __KERNEL__
3390 +#include <linux/string.h>
3391 +#else
3392 +#include <string.h>
3393 +#endif /* __KERNEL__ */
3394 +
3395 +#define AUTOFS_DEVICE_NAME "autofs"
3396 +
3397 +#define AUTOFS_DEV_IOCTL_VERSION_MAJOR 1
3398 +#define AUTOFS_DEV_IOCTL_VERSION_MINOR 0
3399 +
3400 +#define AUTOFS_DEVID_LEN 16
3401 +
3402 +#define AUTOFS_DEV_IOCTL_SIZE sizeof(struct autofs_dev_ioctl)
3403 +
3404 +/*
3405 + * An ioctl interface for autofs mount point control.
3406 + */
3407 +
3408 +struct args_protover {
3409 + __u32 version;
3410 +};
3411 +
3412 +struct args_protosubver {
3413 + __u32 sub_version;
3414 +};
3415 +
3416 +struct args_openmount {
3417 + __u32 devid;
3418 +};
3419 +
3420 +struct args_ready {
3421 + __u32 token;
3422 +};
3423 +
3424 +struct args_fail {
3425 + __u32 token;
3426 + __s32 status;
3427 +};
3428 +
3429 +struct args_setpipefd {
3430 + __s32 pipefd;
3431 +};
3432 +
3433 +struct args_timeout {
3434 + __u64 timeout;
3435 +};
3436 +
3437 +struct args_requester {
3438 + __u32 uid;
3439 + __u32 gid;
3440 +};
3441 +
3442 +struct args_expire {
3443 + __u32 how;
3444 +};
3445 +
3446 +struct args_askumount {
3447 + __u32 may_umount;
3448 +};
3449 +
3450 +struct args_ismountpoint {
3451 + union {
3452 + struct args_in {
3453 + __u32 type;
3454 + } in;
3455 + struct args_out {
3456 + __u32 devid;
3457 + __u32 magic;
3458 + } out;
3459 + };
3460 +};
3461 +
3462 +/*
3463 + * All the ioctls use this structure.
3464 + * When sending a path size must account for the total length
3465 + * of the chunk of memory otherwise is is the size of the
3466 + * structure.
3467 + */
3468 +
3469 +struct autofs_dev_ioctl {
3470 + __u32 ver_major;
3471 + __u32 ver_minor;
3472 + __u32 size; /* total size of data passed in
3473 + * including this struct */
3474 + __s32 ioctlfd; /* automount command fd */
3475 +
3476 + /* Command parameters */
3477 +
3478 + union {
3479 + struct args_protover protover;
3480 + struct args_protosubver protosubver;
3481 + struct args_openmount openmount;
3482 + struct args_ready ready;
3483 + struct args_fail fail;
3484 + struct args_setpipefd setpipefd;
3485 + struct args_timeout timeout;
3486 + struct args_requester requester;
3487 + struct args_expire expire;
3488 + struct args_askumount askumount;
3489 + struct args_ismountpoint ismountpoint;
3490 + };
3491 +
3492 + char path[0];
3493 +};
3494 +
3495 +static inline void init_autofs_dev_ioctl(struct autofs_dev_ioctl *in)
3496 +{
3497 + memset(in, 0, sizeof(struct autofs_dev_ioctl));
3498 + in->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
3499 + in->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
3500 + in->size = sizeof(struct autofs_dev_ioctl);
3501 + in->ioctlfd = -1;
3502 + return;
3503 +}
3504 +
3505 +/*
3506 + * If you change this make sure you make the corresponding change
3507 + * to autofs-dev-ioctl.c:lookup_ioctl()
3508 + */
3509 +enum {
3510 + /* Get various version info */
3511 + AUTOFS_DEV_IOCTL_VERSION_CMD = 0x71,
3512 + AUTOFS_DEV_IOCTL_PROTOVER_CMD,
3513 + AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD,
3514 +
3515 + /* Open mount ioctl fd */
3516 + AUTOFS_DEV_IOCTL_OPENMOUNT_CMD,
3517 +
3518 + /* Close mount ioctl fd */
3519 + AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD,
3520 +
3521 + /* Mount/expire status returns */
3522 + AUTOFS_DEV_IOCTL_READY_CMD,
3523 + AUTOFS_DEV_IOCTL_FAIL_CMD,
3524 +
3525 + /* Activate/deactivate autofs mount */
3526 + AUTOFS_DEV_IOCTL_SETPIPEFD_CMD,
3527 + AUTOFS_DEV_IOCTL_CATATONIC_CMD,
3528 +
3529 + /* Expiry timeout */
3530 + AUTOFS_DEV_IOCTL_TIMEOUT_CMD,
3531 +
3532 + /* Get mount last requesting uid and gid */
3533 + AUTOFS_DEV_IOCTL_REQUESTER_CMD,
3534 +
3535 + /* Check for eligible expire candidates */
3536 + AUTOFS_DEV_IOCTL_EXPIRE_CMD,
3537 +
3538 + /* Request busy status */
3539 + AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD,
3540 +
3541 + /* Check if path is a mountpoint */
3542 + AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD,
3543 +};
3544 +
3545 +#define AUTOFS_IOCTL 0x93
3546 +
3547 +#define AUTOFS_DEV_IOCTL_VERSION \
3548 + _IOWR(AUTOFS_IOCTL, \
3549 + AUTOFS_DEV_IOCTL_VERSION_CMD, struct autofs_dev_ioctl)
3550 +
3551 +#define AUTOFS_DEV_IOCTL_PROTOVER \
3552 + _IOWR(AUTOFS_IOCTL, \
3553 + AUTOFS_DEV_IOCTL_PROTOVER_CMD, struct autofs_dev_ioctl)
3554 +
3555 +#define AUTOFS_DEV_IOCTL_PROTOSUBVER \
3556 + _IOWR(AUTOFS_IOCTL, \
3557 + AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD, struct autofs_dev_ioctl)
3558 +
3559 +#define AUTOFS_DEV_IOCTL_OPENMOUNT \
3560 + _IOWR(AUTOFS_IOCTL, \
3561 + AUTOFS_DEV_IOCTL_OPENMOUNT_CMD, struct autofs_dev_ioctl)
3562 +
3563 +#define AUTOFS_DEV_IOCTL_CLOSEMOUNT \
3564 + _IOWR(AUTOFS_IOCTL, \
3565 + AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD, struct autofs_dev_ioctl)
3566 +
3567 +#define AUTOFS_DEV_IOCTL_READY \
3568 + _IOWR(AUTOFS_IOCTL, \
3569 + AUTOFS_DEV_IOCTL_READY_CMD, struct autofs_dev_ioctl)
3570 +
3571 +#define AUTOFS_DEV_IOCTL_FAIL \
3572 + _IOWR(AUTOFS_IOCTL, \
3573 + AUTOFS_DEV_IOCTL_FAIL_CMD, struct autofs_dev_ioctl)
3574 +
3575 +#define AUTOFS_DEV_IOCTL_SETPIPEFD \
3576 + _IOWR(AUTOFS_IOCTL, \
3577 + AUTOFS_DEV_IOCTL_SETPIPEFD_CMD, struct autofs_dev_ioctl)
3578 +
3579 +#define AUTOFS_DEV_IOCTL_CATATONIC \
3580 + _IOWR(AUTOFS_IOCTL, \
3581 + AUTOFS_DEV_IOCTL_CATATONIC_CMD, struct autofs_dev_ioctl)
3582 +
3583 +#define AUTOFS_DEV_IOCTL_TIMEOUT \
3584 + _IOWR(AUTOFS_IOCTL, \
3585 + AUTOFS_DEV_IOCTL_TIMEOUT_CMD, struct autofs_dev_ioctl)
3586 +
3587 +#define AUTOFS_DEV_IOCTL_REQUESTER \
3588 + _IOWR(AUTOFS_IOCTL, \
3589 + AUTOFS_DEV_IOCTL_REQUESTER_CMD, struct autofs_dev_ioctl)
3590 +
3591 +#define AUTOFS_DEV_IOCTL_EXPIRE \
3592 + _IOWR(AUTOFS_IOCTL, \
3593 + AUTOFS_DEV_IOCTL_EXPIRE_CMD, struct autofs_dev_ioctl)
3594 +
3595 +#define AUTOFS_DEV_IOCTL_ASKUMOUNT \
3596 + _IOWR(AUTOFS_IOCTL, \
3597 + AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD, struct autofs_dev_ioctl)
3598 +
3599 +#define AUTOFS_DEV_IOCTL_ISMOUNTPOINT \
3600 + _IOWR(AUTOFS_IOCTL, \
3601 + AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD, struct autofs_dev_ioctl)
3602 +
3603 +#endif /* _LINUX_AUTO_DEV_IOCTL_H */
3604 --- linux-2.6.20.orig/include/linux/auto_fs.h
3605 +++ linux-2.6.20/include/linux/auto_fs.h
3606 @@ -17,11 +17,13 @@
3607 #ifdef __KERNEL__
3608 #include <linux/fs.h>
3609 #include <linux/limits.h>
3610 +#include <linux/types.h>
3611 +#include <linux/ioctl.h>
3612 +#else
3613 #include <asm/types.h>
3614 +#include <sys/ioctl.h>
3615 #endif /* __KERNEL__ */
3616
3617 -#include <linux/ioctl.h>
3618 -
3619 /* This file describes autofs v3 */
3620 #define AUTOFS_PROTO_VERSION 3
3621