Debian Codegraph @

patches/autofs4-2.6.19-v5-update-20100114.patch

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