Debian Codegraph @

patches/autofs4-2.6.19-v5-update-20090903.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,30 @@ 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 + if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
1045 spin_unlock(&dcache_lock);
1046 -
1047 - if (!empty)
1048 - d_invalidate(dentry);
1049 -
1050 - nd.flags = LOOKUP_DIRECTORY;
1051 - ret = (dentry->d_op->d_revalidate)(dentry, &nd);
1052 -
1053 - if (ret <= 0) {
1054 - if (ret < 0)
1055 - status = ret;
1056 - dcache_dir_close(inode, file);
1057 - goto out;
1058 - }
1059 - }
1060 -
1061 - if (d_mountpoint(dentry)) {
1062 - struct file *fp = NULL;
1063 - struct vfsmount *fp_mnt = mntget(mnt);
1064 - struct dentry *fp_dentry = dget(dentry);
1065 -
1066 - if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
1067 - dput(fp_dentry);
1068 - mntput(fp_mnt);
1069 - dcache_dir_close(inode, file);
1070 - goto out;
1071 - }
1072 -
1073 - fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
1074 - status = PTR_ERR(fp);
1075 - if (IS_ERR(fp)) {
1076 - dcache_dir_close(inode, file);
1077 - goto out;
1078 - }
1079 - cursor->d_fsdata = fp;
1080 - }
1081 - return 0;
1082 -out:
1083 - return status;
1084 -}
1085 -
1086 -static int autofs4_dir_close(struct inode *inode, struct file *file)
1087 -{
1088 - struct dentry *dentry = file->f_dentry;
1089 - struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
1090 - struct dentry *cursor = file->private_data;
1091 - int status = 0;
1092 -
1093 - DPRINTK("file=%p dentry=%p %.*s",
1094 - file, dentry, dentry->d_name.len, dentry->d_name.name);
1095 -
1096 - if (autofs4_oz_mode(sbi))
1097 - goto out;
1098 -
1099 - if (autofs4_ispending(dentry)) {
1100 - DPRINTK("dentry busy");
1101 - status = -EBUSY;
1102 - goto out;
1103 - }
1104 -
1105 - if (d_mountpoint(dentry)) {
1106 - struct file *fp = cursor->d_fsdata;
1107 - if (!fp) {
1108 - status = -ENOENT;
1109 - goto out;
1110 - }
1111 - filp_close(fp, current->files);
1112 - }
1113 -out:
1114 - dcache_dir_close(inode, file);
1115 - return status;
1116 -}
1117 -
1118 -static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
1119 -{
1120 - struct dentry *dentry = file->f_dentry;
1121 - struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
1122 - struct dentry *cursor = file->private_data;
1123 - int status;
1124 -
1125 - DPRINTK("file=%p dentry=%p %.*s",
1126 - file, dentry, dentry->d_name.len, dentry->d_name.name);
1127 -
1128 - if (autofs4_oz_mode(sbi))
1129 - goto out;
1130 -
1131 - if (autofs4_ispending(dentry)) {
1132 - DPRINTK("dentry busy");
1133 - return -EBUSY;
1134 + return -ENOENT;
1135 }
1136 + spin_unlock(&dcache_lock);
1137
1138 - if (d_mountpoint(dentry)) {
1139 - struct file *fp = cursor->d_fsdata;
1140 -
1141 - if (!fp)
1142 - return -ENOENT;
1143 -
1144 - if (!fp->f_op || !fp->f_op->readdir)
1145 - goto out;
1146 -
1147 - status = vfs_readdir(fp, filldir, dirent);
1148 - file->f_pos = fp->f_pos;
1149 - if (status)
1150 - autofs4_copy_atime(file, fp);
1151 - return status;
1152 - }
1153 out:
1154 - return dcache_readdir(file, dirent, filldir);
1155 + return dcache_dir_open(inode, file);
1156 }
1157
1158 static int try_to_fill_dentry(struct dentry *dentry, int flags)
1159 {
1160 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
1161 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1162 - int status = 0;
1163 -
1164 - /* Block on any pending expiry here; invalidate the dentry
1165 - when expiration is done to trigger mount request with a new
1166 - dentry */
1167 - if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
1168 - DPRINTK("waiting for expire %p name=%.*s",
1169 - dentry, dentry->d_name.len, dentry->d_name.name);
1170 -
1171 - status = autofs4_wait(sbi, dentry, NFY_NONE);
1172 -
1173 - DPRINTK("expire done status=%d", status);
1174 -
1175 - /*
1176 - * If the directory still exists the mount request must
1177 - * continue otherwise it can't be followed at the right
1178 - * time during the walk.
1179 - */
1180 - status = d_invalidate(dentry);
1181 - if (status != -EBUSY)
1182 - return -ENOENT;
1183 - }
1184 + int status;
1185
1186 DPRINTK("dentry=%p %.*s ino=%p",
1187 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
1188 @@ -292,7 +133,8 @@ static int try_to_fill_dentry(struct den
1189 return status;
1190 }
1191 /* Trigger mount for path component or follow link */
1192 - } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
1193 + } else if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
1194 + flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) ||
1195 current->link_count) {
1196 DPRINTK("waiting for mount name=%.*s",
1197 dentry->d_name.len, dentry->d_name.name);
1198 @@ -319,7 +161,8 @@ static int try_to_fill_dentry(struct den
1199 spin_lock(&dentry->d_lock);
1200 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
1201 spin_unlock(&dentry->d_lock);
1202 - return status;
1203 +
1204 + return 0;
1205 }
1206
1207 /* For autofs direct mounts the follow link triggers the mount */
1208 @@ -334,50 +177,62 @@ static void *autofs4_follow_link(struct
1209 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
1210 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
1211 nd->flags);
1212 -
1213 - /* If it's our master or we shouldn't trigger a mount we're done */
1214 - lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
1215 - if (oz_mode || !lookup_type)
1216 + /*
1217 + * For an expire of a covered direct or offset mount we need
1218 + * to beeak out of follow_down() at the autofs mount trigger
1219 + * (d_mounted--), so we can see the expiring flag, and manage
1220 + * the blocking and following here until the expire is completed.
1221 + */
1222 + if (oz_mode) {
1223 + spin_lock(&sbi->fs_lock);
1224 + if (ino->flags & AUTOFS_INF_EXPIRING) {
1225 + spin_unlock(&sbi->fs_lock);
1226 + /* Follow down to our covering mount. */
1227 + if (!follow_down(&nd->mnt, &nd->dentry))
1228 + goto done;
1229 + goto follow;
1230 + }
1231 + spin_unlock(&sbi->fs_lock);
1232 goto done;
1233 + }
1234
1235 - /* If an expire request is pending wait for it. */
1236 - if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
1237 - DPRINTK("waiting for active request %p name=%.*s",
1238 - dentry, dentry->d_name.len, dentry->d_name.name);
1239 -
1240 - status = autofs4_wait(sbi, dentry, NFY_NONE);
1241 + /* If an expire request is pending everyone must wait. */
1242 + autofs4_expire_wait(dentry);
1243
1244 - DPRINTK("request done status=%d", status);
1245 - }
1246 + /* We trigger a mount for almost all flags */
1247 + lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS);
1248 + if (!(lookup_type || dentry->d_flags & DCACHE_AUTOFS_PENDING))
1249 + goto follow;
1250
1251 /*
1252 - * If the dentry contains directories then it is an
1253 - * autofs multi-mount with no root mount offset. So
1254 - * don't try to mount it again.
1255 + * If the dentry contains directories then it is an autofs
1256 + * multi-mount with no root mount offset. So don't try to
1257 + * mount it again.
1258 */
1259 spin_lock(&dcache_lock);
1260 - if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
1261 + if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
1262 + (!d_mountpoint(dentry) && __simple_empty(dentry))) {
1263 spin_unlock(&dcache_lock);
1264
1265 status = try_to_fill_dentry(dentry, 0);
1266 if (status)
1267 goto out_error;
1268
1269 - /*
1270 - * The mount succeeded but if there is no root mount
1271 - * it must be an autofs multi-mount with no root offset
1272 - * so we don't need to follow the mount.
1273 - */
1274 - if (d_mountpoint(dentry)) {
1275 - if (!autofs4_follow_mount(&nd->mnt, &nd->dentry)) {
1276 - status = -ENOENT;
1277 - goto out_error;
1278 - }
1279 - }
1280 -
1281 - goto done;
1282 + goto follow;
1283 }
1284 spin_unlock(&dcache_lock);
1285 +follow:
1286 + /*
1287 + * If there is no root mount it must be an autofs
1288 + * multi-mount with no root offset so we don't need
1289 + * to follow it.
1290 + */
1291 + if (d_mountpoint(dentry)) {
1292 + if (!autofs4_follow_mount(&nd->mnt, &nd->dentry)) {
1293 + status = -ENOENT;
1294 + goto out_error;
1295 + }
1296 + }
1297
1298 done:
1299 return NULL;
1300 @@ -402,21 +257,33 @@ static int autofs4_revalidate(struct den
1301 int status = 1;
1302
1303 /* Pending dentry */
1304 + spin_lock(&sbi->fs_lock);
1305 if (autofs4_ispending(dentry)) {
1306 /* The daemon never causes a mount to trigger */
1307 + spin_unlock(&sbi->fs_lock);
1308 +
1309 if (oz_mode)
1310 return 1;
1311
1312 /*
1313 + * If the directory has gone away due to an expire
1314 + * we have been called as ->d_revalidate() and so
1315 + * we need to return false and proceed to ->lookup().
1316 + */
1317 + if (autofs4_expire_wait(dentry) == -EAGAIN)
1318 + return 0;
1319 +
1320 + /*
1321 * A zero status is success otherwise we have a
1322 * negative error code.
1323 */
1324 status = try_to_fill_dentry(dentry, flags);
1325 if (status == 0)
1326 - return 1;
1327 + return 1;
1328
1329 return status;
1330 }
1331 + spin_unlock(&sbi->fs_lock);
1332
1333 /* Negative dentry.. invalidate if "old" */
1334 if (dentry->d_inode == NULL)
1335 @@ -430,6 +297,7 @@ static int autofs4_revalidate(struct den
1336 DPRINTK("dentry=%p %.*s, emptydir",
1337 dentry, dentry->d_name.len, dentry->d_name.name);
1338 spin_unlock(&dcache_lock);
1339 +
1340 /* The daemon never causes a mount to trigger */
1341 if (oz_mode)
1342 return 1;
1343 @@ -459,6 +327,17 @@ void autofs4_dentry_release(struct dentr
1344 de->d_fsdata = NULL;
1345
1346 if (inf) {
1347 + struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
1348 +
1349 + if (sbi) {
1350 + spin_lock(&sbi->lookup_lock);
1351 + if (!list_empty(&inf->active))
1352 + list_del(&inf->active);
1353 + if (!list_empty(&inf->expiring))
1354 + list_del(&inf->expiring);
1355 + spin_unlock(&sbi->lookup_lock);
1356 + }
1357 +
1358 inf->dentry = NULL;
1359 inf->inode = NULL;
1360
1361 @@ -478,10 +357,116 @@ static struct dentry_operations autofs4_
1362 .d_release = autofs4_dentry_release,
1363 };
1364
1365 +static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
1366 +{
1367 + unsigned int len = name->len;
1368 + unsigned int hash = name->hash;
1369 + const unsigned char *str = name->name;
1370 + struct list_head *p, *head;
1371 +
1372 + spin_lock(&dcache_lock);
1373 + spin_lock(&sbi->lookup_lock);
1374 + head = &sbi->active_list;
1375 + list_for_each(p, head) {
1376 + struct autofs_info *ino;
1377 + struct dentry *dentry;
1378 + struct qstr *qstr;
1379 +
1380 + ino = list_entry(p, struct autofs_info, active);
1381 + dentry = ino->dentry;
1382 +
1383 + spin_lock(&dentry->d_lock);
1384 +
1385 + /* Already gone? */
1386 + if (atomic_read(&dentry->d_count) == 0)
1387 + goto next;
1388 +
1389 + qstr = &dentry->d_name;
1390 +
1391 + if (dentry->d_name.hash != hash)
1392 + goto next;
1393 + if (dentry->d_parent != parent)
1394 + goto next;
1395 +
1396 + if (qstr->len != len)
1397 + goto next;
1398 + if (memcmp(qstr->name, str, len))
1399 + goto next;
1400 +
1401 + if (d_unhashed(dentry)) {
1402 + dget(dentry);
1403 + spin_unlock(&dentry->d_lock);
1404 + spin_unlock(&sbi->lookup_lock);
1405 + spin_unlock(&dcache_lock);
1406 + return dentry;
1407 + }
1408 +next:
1409 + spin_unlock(&dentry->d_lock);
1410 + }
1411 + spin_unlock(&sbi->lookup_lock);
1412 + spin_unlock(&dcache_lock);
1413 +
1414 + return NULL;
1415 +}
1416 +
1417 +static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
1418 +{
1419 + unsigned int len = name->len;
1420 + unsigned int hash = name->hash;
1421 + const unsigned char *str = name->name;
1422 + struct list_head *p, *head;
1423 +
1424 + spin_lock(&dcache_lock);
1425 + spin_lock(&sbi->lookup_lock);
1426 + head = &sbi->expiring_list;
1427 + list_for_each(p, head) {
1428 + struct autofs_info *ino;
1429 + struct dentry *dentry;
1430 + struct qstr *qstr;
1431 +
1432 + ino = list_entry(p, struct autofs_info, expiring);
1433 + dentry = ino->dentry;
1434 +
1435 + spin_lock(&dentry->d_lock);
1436 +
1437 + /* Bad luck, we've already been dentry_iput */
1438 + if (!dentry->d_inode)
1439 + goto next;
1440 +
1441 + qstr = &dentry->d_name;
1442 +
1443 + if (dentry->d_name.hash != hash)
1444 + goto next;
1445 + if (dentry->d_parent != parent)
1446 + goto next;
1447 +
1448 + if (qstr->len != len)
1449 + goto next;
1450 + if (memcmp(qstr->name, str, len))
1451 + goto next;
1452 +
1453 + if (d_unhashed(dentry)) {
1454 + dget(dentry);
1455 + spin_unlock(&dentry->d_lock);
1456 + spin_unlock(&sbi->lookup_lock);
1457 + spin_unlock(&dcache_lock);
1458 + return dentry;
1459 + }
1460 +next:
1461 + spin_unlock(&dentry->d_lock);
1462 + }
1463 + spin_unlock(&sbi->lookup_lock);
1464 + spin_unlock(&dcache_lock);
1465 +
1466 + return NULL;
1467 +}
1468 +
1469 /* Lookups in the root directory */
1470 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1471 {
1472 struct autofs_sb_info *sbi;
1473 + struct autofs_info *ino;
1474 + struct dentry *expiring, *unhashed;
1475 int oz_mode;
1476
1477 DPRINTK("name = %.*s",
1478 @@ -497,29 +482,67 @@ static struct dentry *autofs4_lookup(str
1479 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
1480 current->pid, process_group(current), sbi->catatonic, oz_mode);
1481
1482 - /*
1483 - * Mark the dentry incomplete, but add it. This is needed so
1484 - * that the VFS layer knows about the dentry, and we can count
1485 - * on catching any lookups through the revalidate.
1486 - *
1487 - * Let all the hard work be done by the revalidate function that
1488 - * needs to be able to do this anyway..
1489 - *
1490 - * We need to do this before we release the directory semaphore.
1491 - */
1492 - dentry->d_op = &autofs4_root_dentry_operations;
1493 + unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
1494 + if (unhashed)
1495 + dentry = unhashed;
1496 + else {
1497 + /*
1498 + * Mark the dentry incomplete but don't hash it. We do this
1499 + * to serialize our inode creation operations (symlink and
1500 + * mkdir) which prevents deadlock during the callback to
1501 + * the daemon. Subsequent user space lookups for the same
1502 + * dentry are placed on the wait queue while the daemon
1503 + * itself is allowed passage unresticted so the create
1504 + * operation itself can then hash the dentry. Finally,
1505 + * we check for the hashed dentry and return the newly
1506 + * hashed dentry.
1507 + */
1508 + dentry->d_op = &autofs4_root_dentry_operations;
1509 +
1510 + /*
1511 + * And we need to ensure that the same dentry is used for
1512 + * all following lookup calls until it is hashed so that
1513 + * the dentry flags are persistent throughout the request.
1514 + */
1515 + ino = autofs4_init_ino(NULL, sbi, 0555);
1516 + if (!ino)
1517 + return ERR_PTR(-ENOMEM);
1518 +
1519 + dentry->d_fsdata = ino;
1520 + ino->dentry = dentry;
1521 +
1522 + spin_lock(&sbi->lookup_lock);
1523 + list_add(&ino->active, &sbi->active_list);
1524 + spin_unlock(&sbi->lookup_lock);
1525 +
1526 + d_instantiate(dentry, NULL);
1527 + }
1528
1529 if (!oz_mode) {
1530 + mutex_unlock(&dir->i_mutex);
1531 + expiring = autofs4_lookup_expiring(sbi,
1532 + dentry->d_parent,
1533 + &dentry->d_name);
1534 + if (expiring) {
1535 + /*
1536 + * If we are racing with expire the request might not
1537 + * be quite complete but the directory has been removed
1538 + * so it must have been successful, so just wait for it.
1539 + */
1540 + ino = autofs4_dentry_ino(expiring);
1541 + autofs4_expire_wait(expiring);
1542 + spin_lock(&sbi->lookup_lock);
1543 + if (!list_empty(&ino->expiring))
1544 + list_del_init(&ino->expiring);
1545 + spin_unlock(&sbi->lookup_lock);
1546 + dput(expiring);
1547 + }
1548 +
1549 spin_lock(&dentry->d_lock);
1550 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
1551 spin_unlock(&dentry->d_lock);
1552 - }
1553 - dentry->d_fsdata = NULL;
1554 - d_add(dentry, NULL);
1555 -
1556 - if (dentry->d_op && dentry->d_op->d_revalidate) {
1557 - mutex_unlock(&dir->i_mutex);
1558 - (dentry->d_op->d_revalidate)(dentry, nd);
1559 + if (dentry->d_op && dentry->d_op->d_revalidate)
1560 + (dentry->d_op->d_revalidate)(dentry, nd);
1561 mutex_lock(&dir->i_mutex);
1562 }
1563
1564 @@ -534,22 +557,47 @@ static struct dentry *autofs4_lookup(str
1565 if (sigismember (sigset, SIGKILL) ||
1566 sigismember (sigset, SIGQUIT) ||
1567 sigismember (sigset, SIGINT)) {
1568 + if (unhashed)
1569 + dput(unhashed);
1570 return ERR_PTR(-ERESTARTNOINTR);
1571 }
1572 }
1573 - spin_lock(&dentry->d_lock);
1574 - dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
1575 - spin_unlock(&dentry->d_lock);
1576 + if (!oz_mode) {
1577 + spin_lock(&dentry->d_lock);
1578 + dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
1579 + spin_unlock(&dentry->d_lock);
1580 + }
1581 }
1582
1583 /*
1584 * If this dentry is unhashed, then we shouldn't honour this
1585 - * lookup even if the dentry is positive. Returning ENOENT here
1586 - * doesn't do the right thing for all system calls, but it should
1587 - * be OK for the operations we permit from an autofs.
1588 + * lookup. Returning ENOENT here doesn't do the right thing
1589 + * for all system calls, but it should be OK for the operations
1590 + * we permit from an autofs.
1591 */
1592 - if (dentry->d_inode && d_unhashed(dentry))
1593 - return ERR_PTR(-ENOENT);
1594 + if (!oz_mode && d_unhashed(dentry)) {
1595 + /*
1596 + * A user space application can (and has done in the past)
1597 + * remove and re-create this directory during the callback.
1598 + * This can leave us with an unhashed dentry, but a
1599 + * successful mount! So we need to perform another
1600 + * cached lookup in case the dentry now exists.
1601 + */
1602 + struct dentry *parent = dentry->d_parent;
1603 + struct dentry *new = d_lookup(parent, &dentry->d_name);
1604 + if (new != NULL)
1605 + dentry = new;
1606 + else
1607 + dentry = ERR_PTR(-ENOENT);
1608 +
1609 + if (unhashed)
1610 + dput(unhashed);
1611 +
1612 + return dentry;
1613 + }
1614 +
1615 + if (unhashed)
1616 + return unhashed;
1617
1618 return NULL;
1619 }
1620 @@ -571,21 +619,32 @@ static int autofs4_dir_symlink(struct in
1621 return -EACCES;
1622
1623 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
1624 - if (ino == NULL)
1625 - return -ENOSPC;
1626 + if (!ino)
1627 + return -ENOMEM;
1628
1629 - ino->size = strlen(symname);
1630 - ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
1631 + spin_lock(&sbi->lookup_lock);
1632 + if (!list_empty(&ino->active))
1633 + list_del_init(&ino->active);
1634 + spin_unlock(&sbi->lookup_lock);
1635
1636 - if (cp == NULL) {
1637 - kfree(ino);
1638 - return -ENOSPC;
1639 + ino->size = strlen(symname);
1640 + cp = kmalloc(ino->size + 1, GFP_KERNEL);
1641 + if (!cp) {
1642 + if (!dentry->d_fsdata)
1643 + kfree(ino);
1644 + return -ENOMEM;
1645 }
1646
1647 strcpy(cp, symname);
1648
1649 inode = autofs4_get_inode(dir->i_sb, ino);
1650 - d_instantiate(dentry, inode);
1651 + if (!inode) {
1652 + kfree(cp);
1653 + if (!dentry->d_fsdata)
1654 + kfree(ino);
1655 + return -ENOMEM;
1656 + }
1657 + d_add(dentry, inode);
1658
1659 if (dir == dir->i_sb->s_root->d_inode)
1660 dentry->d_op = &autofs4_root_dentry_operations;
1661 @@ -600,6 +659,7 @@ static int autofs4_dir_symlink(struct in
1662 atomic_inc(&p_ino->count);
1663 ino->inode = inode;
1664
1665 + ino->u.symlink = cp;
1666 dir->i_mtime = CURRENT_TIME;
1667
1668 return 0;
1669 @@ -611,9 +671,9 @@ static int autofs4_dir_symlink(struct in
1670 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
1671 * that the file no longer exists. However, doing that means that the
1672 * VFS layer can turn the dentry into a negative dentry. We don't want
1673 - * this, because since the unlink is probably the result of an expire.
1674 - * We simply d_drop it, which allows the dentry lookup to remount it
1675 - * if necessary.
1676 + * this, because the unlink is probably the result of an expire.
1677 + * We simply d_drop it and add it to a expiring list in the super block,
1678 + * which allows the dentry lookup to check for an incomplete expire.
1679 *
1680 * If a process is blocked on the dentry waiting for the expire to finish,
1681 * it will invalidate the dentry and try to mount with a new one.
1682 @@ -642,7 +702,15 @@ static int autofs4_dir_unlink(struct ino
1683
1684 dir->i_mtime = CURRENT_TIME;
1685
1686 - d_drop(dentry);
1687 + spin_lock(&dcache_lock);
1688 + spin_lock(&sbi->lookup_lock);
1689 + if (list_empty(&ino->expiring))
1690 + list_add(&ino->expiring, &sbi->expiring_list);
1691 + spin_unlock(&sbi->lookup_lock);
1692 + spin_lock(&dentry->d_lock);
1693 + __d_drop(dentry);
1694 + spin_unlock(&dentry->d_lock);
1695 + spin_unlock(&dcache_lock);
1696
1697 return 0;
1698 }
1699 @@ -653,6 +721,9 @@ static int autofs4_dir_rmdir(struct inod
1700 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1701 struct autofs_info *p_ino;
1702
1703 + DPRINTK("dentry %p, removing %.*s",
1704 + dentry, dentry->d_name.len, dentry->d_name.name);
1705 +
1706 if (!autofs4_oz_mode(sbi))
1707 return -EACCES;
1708
1709 @@ -661,6 +732,10 @@ static int autofs4_dir_rmdir(struct inod
1710 spin_unlock(&dcache_lock);
1711 return -ENOTEMPTY;
1712 }
1713 + spin_lock(&sbi->lookup_lock);
1714 + if (list_empty(&ino->expiring))
1715 + list_add(&ino->expiring, &sbi->expiring_list);
1716 + spin_unlock(&sbi->lookup_lock);
1717 spin_lock(&dentry->d_lock);
1718 __d_drop(dentry);
1719 spin_unlock(&dentry->d_lock);
1720 @@ -695,11 +770,21 @@ static int autofs4_dir_mkdir(struct inod
1721 dentry, dentry->d_name.len, dentry->d_name.name);
1722
1723 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
1724 - if (ino == NULL)
1725 - return -ENOSPC;
1726 + if (!ino)
1727 + return -ENOMEM;
1728 +
1729 + spin_lock(&sbi->lookup_lock);
1730 + if (!list_empty(&ino->active))
1731 + list_del_init(&ino->active);
1732 + spin_unlock(&sbi->lookup_lock);
1733
1734 inode = autofs4_get_inode(dir->i_sb, ino);
1735 - d_instantiate(dentry, inode);
1736 + if (!inode) {
1737 + if (!dentry->d_fsdata)
1738 + kfree(ino);
1739 + return -ENOMEM;
1740 + }
1741 + d_add(dentry, inode);
1742
1743 if (dir == dir->i_sb->s_root->d_inode)
1744 dentry->d_op = &autofs4_root_dentry_operations;
1745 @@ -751,44 +836,6 @@ static inline int autofs4_get_protosubve
1746 }
1747
1748 /*
1749 - * Tells the daemon whether we need to reghost or not. Also, clears
1750 - * the reghost_needed flag.
1751 - */
1752 -static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
1753 -{
1754 - int status;
1755 -
1756 - DPRINTK("returning %d", sbi->needs_reghost);
1757 -
1758 - status = put_user(sbi->needs_reghost, p);
1759 - if ( status )
1760 - return status;
1761 -
1762 - sbi->needs_reghost = 0;
1763 - return 0;
1764 -}
1765 -
1766 -/*
1767 - * Enable / Disable reghosting ioctl() operation
1768 - */
1769 -static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
1770 -{
1771 - int status;
1772 - int val;
1773 -
1774 - status = get_user(val, p);
1775 -
1776 - DPRINTK("reghost = %d", val);
1777 -
1778 - if (status)
1779 - return status;
1780 -
1781 - /* turn on/off reghosting, with the val */
1782 - sbi->reghost_enabled = val;
1783 - return 0;
1784 -}
1785 -
1786 -/*
1787 * Tells the daemon whether it can umount the autofs mount.
1788 */
1789 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
1790 @@ -852,11 +899,6 @@ static int autofs4_root_ioctl(struct ino
1791 case AUTOFS_IOC_SETTIMEOUT:
1792 return autofs4_get_set_timeout(sbi, p);
1793
1794 - case AUTOFS_IOC_TOGGLEREGHOST:
1795 - return autofs4_toggle_reghost(sbi, p);
1796 - case AUTOFS_IOC_ASKREGHOST:
1797 - return autofs4_ask_reghost(sbi, p);
1798 -
1799 case AUTOFS_IOC_ASKUMOUNT:
1800 return autofs4_ask_umount(filp->f_vfsmnt, p);
1801
1802 --- linux-2.6.19.orig/fs/autofs4/expire.c
1803 +++ linux-2.6.19/fs/autofs4/expire.c
1804 @@ -56,12 +56,25 @@ static int autofs4_mount_busy(struct vfs
1805 mntget(mnt);
1806 dget(dentry);
1807
1808 - if (!autofs4_follow_mount(&mnt, &dentry))
1809 + if (!follow_down(&mnt, &dentry))
1810 goto done;
1811
1812 - /* This is an autofs submount, we can't expire it */
1813 - if (is_autofs4_dentry(dentry))
1814 - goto done;
1815 + if (is_autofs4_dentry(dentry)) {
1816 + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
1817 +
1818 + /* This is an autofs submount, we can't expire it */
1819 + if (autofs_type_indirect(sbi->type))
1820 + goto done;
1821 +
1822 + /*
1823 + * Otherwise it's an offset mount and we need to check
1824 + * if we can umount its mount, if there is one.
1825 + */
1826 + if (!d_mountpoint(dentry)) {
1827 + status = 0;
1828 + goto done;
1829 + }
1830 + }
1831
1832 /* Update the expiry counter if fs is busy */
1833 if (!may_umount_tree(mnt)) {
1834 @@ -73,8 +86,8 @@ static int autofs4_mount_busy(struct vfs
1835 status = 0;
1836 done:
1837 DPRINTK("returning = %d", status);
1838 - mntput(mnt);
1839 dput(dentry);
1840 + mntput(mnt);
1841 return status;
1842 }
1843
1844 @@ -244,10 +257,10 @@ cont:
1845 }
1846
1847 /* Check if we can expire a direct mount (possibly a tree) */
1848 -static struct dentry *autofs4_expire_direct(struct super_block *sb,
1849 - struct vfsmount *mnt,
1850 - struct autofs_sb_info *sbi,
1851 - int how)
1852 +struct dentry *autofs4_expire_direct(struct super_block *sb,
1853 + struct vfsmount *mnt,
1854 + struct autofs_sb_info *sbi,
1855 + int how)
1856 {
1857 unsigned long timeout;
1858 struct dentry *root = dget(sb->s_root);
1859 @@ -259,13 +272,15 @@ static struct dentry *autofs4_expire_dir
1860 now = jiffies;
1861 timeout = sbi->exp_timeout;
1862
1863 - /* Lock the tree as we must expire as a whole */
1864 spin_lock(&sbi->fs_lock);
1865 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
1866 struct autofs_info *ino = autofs4_dentry_ino(root);
1867 -
1868 - /* Set this flag early to catch sys_chdir and the like */
1869 + if (d_mountpoint(root)) {
1870 + ino->flags |= AUTOFS_INF_MOUNTPOINT;
1871 + root->d_mounted--;
1872 + }
1873 ino->flags |= AUTOFS_INF_EXPIRING;
1874 + init_completion(&ino->expire_complete);
1875 spin_unlock(&sbi->fs_lock);
1876 return root;
1877 }
1878 @@ -281,10 +296,10 @@ static struct dentry *autofs4_expire_dir
1879 * - it is unused by any user process
1880 * - it has been unused for exp_timeout time
1881 */
1882 -static struct dentry *autofs4_expire_indirect(struct super_block *sb,
1883 - struct vfsmount *mnt,
1884 - struct autofs_sb_info *sbi,
1885 - int how)
1886 +struct dentry *autofs4_expire_indirect(struct super_block *sb,
1887 + struct vfsmount *mnt,
1888 + struct autofs_sb_info *sbi,
1889 + int how)
1890 {
1891 unsigned long timeout;
1892 struct dentry *root = sb->s_root;
1893 @@ -292,6 +307,8 @@ static struct dentry *autofs4_expire_ind
1894 struct list_head *next;
1895 int do_now = how & AUTOFS_EXP_IMMEDIATE;
1896 int exp_leaves = how & AUTOFS_EXP_LEAVES;
1897 + struct autofs_info *ino;
1898 + unsigned int ino_count;
1899
1900 if (!root)
1901 return NULL;
1902 @@ -316,6 +333,9 @@ static struct dentry *autofs4_expire_ind
1903 dentry = dget(dentry);
1904 spin_unlock(&dcache_lock);
1905
1906 + spin_lock(&sbi->fs_lock);
1907 + ino = autofs4_dentry_ino(dentry);
1908 +
1909 /*
1910 * Case 1: (i) indirect mount or top level pseudo direct mount
1911 * (autofs-4.1).
1912 @@ -326,6 +346,11 @@ static struct dentry *autofs4_expire_ind
1913 DPRINTK("checking mountpoint %p %.*s",
1914 dentry, (int)dentry->d_name.len, dentry->d_name.name);
1915
1916 + /* Path walk currently on this dentry? */
1917 + ino_count = atomic_read(&ino->count) + 2;
1918 + if (atomic_read(&dentry->d_count) > ino_count)
1919 + goto next;
1920 +
1921 /* Can we umount this guy */
1922 if (autofs4_mount_busy(mnt, dentry))
1923 goto next;
1924 @@ -333,7 +358,7 @@ static struct dentry *autofs4_expire_ind
1925 /* Can we expire this guy */
1926 if (autofs4_can_expire(dentry, timeout, do_now)) {
1927 expired = dentry;
1928 - break;
1929 + goto found;
1930 }
1931 goto next;
1932 }
1933 @@ -343,46 +368,80 @@ static struct dentry *autofs4_expire_ind
1934
1935 /* Case 2: tree mount, expire iff entire tree is not busy */
1936 if (!exp_leaves) {
1937 - /* Lock the tree as we must expire as a whole */
1938 - spin_lock(&sbi->fs_lock);
1939 - if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
1940 - struct autofs_info *inf = autofs4_dentry_ino(dentry);
1941 + /* Path walk currently on this dentry? */
1942 + ino_count = atomic_read(&ino->count) + 1;
1943 + if (atomic_read(&dentry->d_count) > ino_count)
1944 + goto next;
1945
1946 - /* Set this flag early to catch sys_chdir and the like */
1947 - inf->flags |= AUTOFS_INF_EXPIRING;
1948 - spin_unlock(&sbi->fs_lock);
1949 + if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
1950 expired = dentry;
1951 - break;
1952 + goto found;
1953 }
1954 - spin_unlock(&sbi->fs_lock);
1955 /*
1956 * Case 3: pseudo direct mount, expire individual leaves
1957 * (autofs-4.1).
1958 */
1959 } else {
1960 + /* Path walk currently on this dentry? */
1961 + ino_count = atomic_read(&ino->count) + 1;
1962 + if (atomic_read(&dentry->d_count) > ino_count)
1963 + goto next;
1964 +
1965 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
1966 if (expired) {
1967 dput(dentry);
1968 - break;
1969 + goto found;
1970 }
1971 }
1972 next:
1973 + spin_unlock(&sbi->fs_lock);
1974 dput(dentry);
1975 spin_lock(&dcache_lock);
1976 next = next->next;
1977 }
1978 + spin_unlock(&dcache_lock);
1979 + return NULL;
1980
1981 - if (expired) {
1982 - DPRINTK("returning %p %.*s",
1983 - expired, (int)expired->d_name.len, expired->d_name.name);
1984 - spin_lock(&dcache_lock);
1985 - list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
1986 - spin_unlock(&dcache_lock);
1987 - return expired;
1988 - }
1989 +found:
1990 + DPRINTK("returning %p %.*s",
1991 + expired, (int)expired->d_name.len, expired->d_name.name);
1992 + ino = autofs4_dentry_ino(expired);
1993 + ino->flags |= AUTOFS_INF_EXPIRING;
1994 + init_completion(&ino->expire_complete);
1995 + spin_unlock(&sbi->fs_lock);
1996 + spin_lock(&dcache_lock);
1997 + list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
1998 spin_unlock(&dcache_lock);
1999 + return expired;
2000 +}
2001
2002 - return NULL;
2003 +int autofs4_expire_wait(struct dentry *dentry)
2004 +{
2005 + struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
2006 + struct autofs_info *ino = autofs4_dentry_ino(dentry);
2007 + int status;
2008 +
2009 + /* Block on any pending expire */
2010 + spin_lock(&sbi->fs_lock);
2011 + if (ino->flags & AUTOFS_INF_EXPIRING) {
2012 + spin_unlock(&sbi->fs_lock);
2013 +
2014 + DPRINTK("waiting for expire %p name=%.*s",
2015 + dentry, dentry->d_name.len, dentry->d_name.name);
2016 +
2017 + status = autofs4_wait(sbi, dentry, NFY_NONE);
2018 + wait_for_completion(&ino->expire_complete);
2019 +
2020 + DPRINTK("expire done status=%d", status);
2021 +
2022 + if (d_unhashed(dentry))
2023 + return -EAGAIN;
2024 +
2025 + return status;
2026 + }
2027 + spin_unlock(&sbi->fs_lock);
2028 +
2029 + return 0;
2030 }
2031
2032 /* Perform an expiry operation */
2033 @@ -392,7 +451,9 @@ int autofs4_expire_run(struct super_bloc
2034 struct autofs_packet_expire __user *pkt_p)
2035 {
2036 struct autofs_packet_expire pkt;
2037 + struct autofs_info *ino;
2038 struct dentry *dentry;
2039 + int ret = 0;
2040
2041 memset(&pkt,0,sizeof pkt);
2042
2043 @@ -408,9 +469,15 @@ int autofs4_expire_run(struct super_bloc
2044 dput(dentry);
2045
2046 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
2047 - return -EFAULT;
2048 + ret = -EFAULT;
2049
2050 - return 0;
2051 + spin_lock(&sbi->fs_lock);
2052 + ino = autofs4_dentry_ino(dentry);
2053 + ino->flags &= ~AUTOFS_INF_EXPIRING;
2054 + complete_all(&ino->expire_complete);
2055 + spin_unlock(&sbi->fs_lock);
2056 +
2057 + return ret;
2058 }
2059
2060 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
2061 @@ -425,7 +492,7 @@ int autofs4_expire_multi(struct super_bl
2062 if (arg && get_user(do_now, arg))
2063 return -EFAULT;
2064
2065 - if (sbi->type & AUTOFS_TYPE_DIRECT)
2066 + if (autofs_type_trigger(sbi->type))
2067 dentry = autofs4_expire_direct(sb, mnt, sbi, do_now);
2068 else
2069 dentry = autofs4_expire_indirect(sb, mnt, sbi, do_now);
2070 @@ -435,9 +502,16 @@ int autofs4_expire_multi(struct super_bl
2071
2072 /* This is synchronous because it makes the daemon a
2073 little easier */
2074 - ino->flags |= AUTOFS_INF_EXPIRING;
2075 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
2076 +
2077 + spin_lock(&sbi->fs_lock);
2078 + if (ino->flags & AUTOFS_INF_MOUNTPOINT) {
2079 + sb->s_root->d_mounted++;
2080 + ino->flags &= ~AUTOFS_INF_MOUNTPOINT;
2081 + }
2082 ino->flags &= ~AUTOFS_INF_EXPIRING;
2083 + complete_all(&ino->expire_complete);
2084 + spin_unlock(&sbi->fs_lock);
2085 dput(dentry);
2086 }
2087
2088 --- linux-2.6.19.orig/include/linux/compat_ioctl.h
2089 +++ linux-2.6.19/include/linux/compat_ioctl.h
2090 @@ -568,8 +568,6 @@ COMPATIBLE_IOCTL(AUTOFS_IOC_PROTOVER)
2091 COMPATIBLE_IOCTL(AUTOFS_IOC_EXPIRE)
2092 COMPATIBLE_IOCTL(AUTOFS_IOC_EXPIRE_MULTI)
2093 COMPATIBLE_IOCTL(AUTOFS_IOC_PROTOSUBVER)
2094 -COMPATIBLE_IOCTL(AUTOFS_IOC_ASKREGHOST)
2095 -COMPATIBLE_IOCTL(AUTOFS_IOC_TOGGLEREGHOST)
2096 COMPATIBLE_IOCTL(AUTOFS_IOC_ASKUMOUNT)
2097 /* Raw devices */
2098 COMPATIBLE_IOCTL(RAW_SETBIND)
2099 --- /dev/null
2100 +++ linux-2.6.19/Documentation/filesystems/autofs4-mount-control.txt
2101 @@ -0,0 +1,414 @@
2102 +
2103 +Miscellaneous Device control operations for the autofs4 kernel module
2104 +====================================================================
2105 +
2106 +The problem
2107 +===========
2108 +
2109 +There is a problem with active restarts in autofs (that is to say
2110 +restarting autofs when there are busy mounts).
2111 +
2112 +During normal operation autofs uses a file descriptor opened on the
2113 +directory that is being managed in order to be able to issue control
2114 +operations. Using a file descriptor gives ioctl operations access to
2115 +autofs specific information stored in the super block. The operations
2116 +are things such as setting an autofs mount catatonic, setting the
2117 +expire timeout and requesting expire checks. As is explained below,
2118 +certain types of autofs triggered mounts can end up covering an autofs
2119 +mount itself which prevents us being able to use open(2) to obtain a
2120 +file descriptor for these operations if we don't already have one open.
2121 +
2122 +Currently autofs uses "umount -l" (lazy umount) to clear active mounts
2123 +at restart. While using lazy umount works for most cases, anything that
2124 +needs to walk back up the mount tree to construct a path, such as
2125 +getcwd(2) and the proc file system /proc/<pid>/cwd, no longer works
2126 +because the point from which the path is constructed has been detached
2127 +from the mount tree.
2128 +
2129 +The actual problem with autofs is that it can't reconnect to existing
2130 +mounts. Immediately one thinks of just adding the ability to remount
2131 +autofs file systems would solve it, but alas, that can't work. This is
2132 +because autofs direct mounts and the implementation of "on demand mount
2133 +and expire" of nested mount trees have the file system mounted directly
2134 +on top of the mount trigger directory dentry.
2135 +
2136 +For example, there are two types of automount maps, direct (in the kernel
2137 +module source you will see a third type called an offset, which is just
2138 +a direct mount in disguise) and indirect.
2139 +
2140 +Here is a master map with direct and indirect map entries:
2141 +
2142 +/- /etc/auto.direct
2143 +/test /etc/auto.indirect
2144 +
2145 +and the corresponding map files:
2146 +
2147 +/etc/auto.direct:
2148 +
2149 +/automount/dparse/g6 budgie:/autofs/export1
2150 +/automount/dparse/g1 shark:/autofs/export1
2151 +and so on.
2152 +
2153 +/etc/auto.indirect:
2154 +
2155 +g1 shark:/autofs/export1
2156 +g6 budgie:/autofs/export1
2157 +and so on.
2158 +
2159 +For the above indirect map an autofs file system is mounted on /test and
2160 +mounts are triggered for each sub-directory key by the inode lookup
2161 +operation. So we see a mount of shark:/autofs/export1 on /test/g1, for
2162 +example.
2163 +
2164 +The way that direct mounts are handled is by making an autofs mount on
2165 +each full path, such as /automount/dparse/g1, and using it as a mount
2166 +trigger. So when we walk on the path we mount shark:/autofs/export1 "on
2167 +top of this mount point". Since these are always directories we can
2168 +use the follow_link inode operation to trigger the mount.
2169 +
2170 +But, each entry in direct and indirect maps can have offsets (making
2171 +them multi-mount map entries).
2172 +
2173 +For example, an indirect mount map entry could also be:
2174 +
2175 +g1 \
2176 + / shark:/autofs/export5/testing/test \
2177 + /s1 shark:/autofs/export/testing/test/s1 \
2178 + /s2 shark:/autofs/export5/testing/test/s2 \
2179 + /s1/ss1 shark:/autofs/export1 \
2180 + /s2/ss2 shark:/autofs/export2
2181 +
2182 +and a similarly a direct mount map entry could also be:
2183 +
2184 +/automount/dparse/g1 \
2185 + / shark:/autofs/export5/testing/test \
2186 + /s1 shark:/autofs/export/testing/test/s1 \
2187 + /s2 shark:/autofs/export5/testing/test/s2 \
2188 + /s1/ss1 shark:/autofs/export2 \
2189 + /s2/ss2 shark:/autofs/export2
2190 +
2191 +One of the issues with version 4 of autofs was that, when mounting an
2192 +entry with a large number of offsets, possibly with nesting, we needed
2193 +to mount and umount all of the offsets as a single unit. Not really a
2194 +problem, except for people with a large number of offsets in map entries.
2195 +This mechanism is used for the well known "hosts" map and we have seen
2196 +cases (in 2.4) where the available number of mounts are exhausted or
2197 +where the number of privileged ports available is exhausted.
2198 +
2199 +In version 5 we mount only as we go down the tree of offsets and
2200 +similarly for expiring them which resolves the above problem. There is
2201 +somewhat more detail to the implementation but it isn't needed for the
2202 +sake of the problem explanation. The one important detail is that these
2203 +offsets are implemented using the same mechanism as the direct mounts
2204 +above and so the mount points can be covered by a mount.
2205 +
2206 +The current autofs implementation uses an ioctl file descriptor opened
2207 +on the mount point for control operations. The references held by the
2208 +descriptor are accounted for in checks made to determine if a mount is
2209 +in use and is also used to access autofs file system information held
2210 +in the mount super block. So the use of a file handle needs to be
2211 +retained.
2212 +
2213 +
2214 +The Solution
2215 +============
2216 +
2217 +To be able to restart autofs leaving existing direct, indirect and
2218 +offset mounts in place we need to be able to obtain a file handle
2219 +for these potentially covered autofs mount points. Rather than just
2220 +implement an isolated operation it was decided to re-implement the
2221 +existing ioctl interface and add new operations to provide this
2222 +functionality.
2223 +
2224 +In addition, to be able to reconstruct a mount tree that has busy mounts,
2225 +the uid and gid of the last user that triggered the mount needs to be
2226 +available because these can be used as macro substitution variables in
2227 +autofs maps. They are recorded at mount request time and an operation
2228 +has been added to retrieve them.
2229 +
2230 +Since we're re-implementing the control interface, a couple of other
2231 +problems with the existing interface have been addressed. First, when
2232 +a mount or expire operation completes a status is returned to the
2233 +kernel by either a "send ready" or a "send fail" operation. The
2234 +"send fail" operation of the ioctl interface could only ever send
2235 +ENOENT so the re-implementation allows user space to send an actual
2236 +status. Another expensive operation in user space, for those using
2237 +very large maps, is discovering if a mount is present. Usually this
2238 +involves scanning /proc/mounts and since it needs to be done quite
2239 +often it can introduce significant overhead when there are many entries
2240 +in the mount table. An operation to lookup the mount status of a mount
2241 +point dentry (covered or not) has also been added.
2242 +
2243 +Current kernel development policy recommends avoiding the use of the
2244 +ioctl mechanism in favor of systems such as Netlink. An implementation
2245 +using this system was attempted to evaluate its suitability and it was
2246 +found to be inadequate, in this case. The Generic Netlink system was
2247 +used for this as raw Netlink would lead to a significant increase in
2248 +complexity. There's no question that the Generic Netlink system is an
2249 +elegant solution for common case ioctl functions but it's not a complete
2250 +replacement probably because it's primary purpose in life is to be a
2251 +message bus implementation rather than specifically an ioctl replacement.
2252 +While it would be possible to work around this there is one concern
2253 +that lead to the decision to not use it. This is that the autofs
2254 +expire in the daemon has become far to complex because umount
2255 +candidates are enumerated, almost for no other reason than to "count"
2256 +the number of times to call the expire ioctl. This involves scanning
2257 +the mount table which has proved to be a big overhead for users with
2258 +large maps. The best way to improve this is try and get back to the
2259 +way the expire was done long ago. That is, when an expire request is
2260 +issued for a mount (file handle) we should continually call back to
2261 +the daemon until we can't umount any more mounts, then return the
2262 +appropriate status to the daemon. At the moment we just expire one
2263 +mount at a time. A Generic Netlink implementation would exclude this
2264 +possibility for future development due to the requirements of the
2265 +message bus architecture.
2266 +
2267 +
2268 +autofs4 Miscellaneous Device mount control interface
2269 +====================================================
2270 +
2271 +The control interface is opening a device node, typically /dev/autofs.
2272 +
2273 +All the ioctls use a common structure to pass the needed parameter
2274 +information and return operation results:
2275 +
2276 +struct autofs_dev_ioctl {
2277 + __u32 ver_major;
2278 + __u32 ver_minor;
2279 + __u32 size; /* total size of data passed in
2280 + * including this struct */
2281 + __s32 ioctlfd; /* automount command fd */
2282 +
2283 + /* Command parameters */
2284 +
2285 + union {
2286 + struct args_protover protover;
2287 + struct args_protosubver protosubver;
2288 + struct args_openmount openmount;
2289 + struct args_ready ready;
2290 + struct args_fail fail;
2291 + struct args_setpipefd setpipefd;
2292 + struct args_timeout timeout;
2293 + struct args_requester requester;
2294 + struct args_expire expire;
2295 + struct args_askumount askumount;
2296 + struct args_ismountpoint ismountpoint;
2297 + };
2298 +
2299 + char path[0];
2300 +};
2301 +
2302 +The ioctlfd field is a mount point file descriptor of an autofs mount
2303 +point. It is returned by the open call and is used by all calls except
2304 +the check for whether a given path is a mount point, where it may
2305 +optionally be used to check a specific mount corresponding to a given
2306 +mount point file descriptor, and when requesting the uid and gid of the
2307 +last successful mount on a directory within the autofs file system.
2308 +
2309 +The anonymous union is used to communicate parameters and results of calls
2310 +made as described below.
2311 +
2312 +The path field is used to pass a path where it is needed and the size field
2313 +is used account for the increased structure length when translating the
2314 +structure sent from user space.
2315 +
2316 +This structure can be initialized before setting specific fields by using
2317 +the void function call init_autofs_dev_ioctl(struct autofs_dev_ioctl *).
2318 +
2319 +All of the ioctls perform a copy of this structure from user space to
2320 +kernel space and return -EINVAL if the size parameter is smaller than
2321 +the structure size itself, -ENOMEM if the kernel memory allocation fails
2322 +or -EFAULT if the copy itself fails. Other checks include a version check
2323 +of the compiled in user space version against the module version and a
2324 +mismatch results in a -EINVAL return. If the size field is greater than
2325 +the structure size then a path is assumed to be present and is checked to
2326 +ensure it begins with a "/" and is NULL terminated, otherwise -EINVAL is
2327 +returned. Following these checks, for all ioctl commands except
2328 +AUTOFS_DEV_IOCTL_VERSION_CMD, AUTOFS_DEV_IOCTL_OPENMOUNT_CMD and
2329 +AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD the ioctlfd is validated and if it is
2330 +not a valid descriptor or doesn't correspond to an autofs mount point
2331 +an error of -EBADF, -ENOTTY or -EINVAL (not an autofs descriptor) is
2332 +returned.
2333 +
2334 +
2335 +The ioctls
2336 +==========
2337 +
2338 +An example of an implementation which uses this interface can be seen
2339 +in autofs version 5.0.4 and later in file lib/dev-ioctl-lib.c of the
2340 +distribution tar available for download from kernel.org in directory
2341 +/pub/linux/daemons/autofs/v5.
2342 +
2343 +The device node ioctl operations implemented by this interface are:
2344 +
2345 +
2346 +AUTOFS_DEV_IOCTL_VERSION
2347 +------------------------
2348 +
2349 +Get the major and minor version of the autofs4 device ioctl kernel module
2350 +implementation. It requires an initialized struct autofs_dev_ioctl as an
2351 +input parameter and sets the version information in the passed in structure.
2352 +It returns 0 on success or the error -EINVAL if a version mismatch is
2353 +detected.
2354 +
2355 +
2356 +AUTOFS_DEV_IOCTL_PROTOVER_CMD and AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD
2357 +------------------------------------------------------------------
2358 +
2359 +Get the major and minor version of the autofs4 protocol version understood
2360 +by loaded module. This call requires an initialized struct autofs_dev_ioctl
2361 +with the ioctlfd field set to a valid autofs mount point descriptor
2362 +and sets the requested version number in structure field protover.version
2363 +and ptotosubver.sub_version respectively. These commands return 0 on
2364 +success or one of the negative error codes if validation fails.
2365 +
2366 +
2367 +AUTOFS_DEV_IOCTL_OPENMOUNT_CMD and AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD
2368 +------------------------------------------------------------------
2369 +
2370 +Obtain and release a file descriptor for an autofs managed mount point
2371 +path. The open call requires an initialized struct autofs_dev_ioctl with
2372 +the the path field set and the size field adjusted appropriately as well
2373 +as the openmount.devid field set to the device number of the autofs mount.
2374 +The device number of an autofs mounted filesystem can be obtained by using
2375 +the AUTOFS_DEV_IOCTL_ISMOUNTPOINT ioctl function by providing the path
2376 +and autofs mount type, as described below. The close call requires an
2377 +initialized struct autofs_dev_ioct with the ioctlfd field set to the
2378 +descriptor obtained from the open call. The release of the file descriptor
2379 +can also be done with close(2) so any open descriptors will also be
2380 +closed at process exit. The close call is included in the implemented
2381 +operations largely for completeness and to provide for a consistent
2382 +user space implementation.
2383 +
2384 +
2385 +AUTOFS_DEV_IOCTL_READY_CMD and AUTOFS_DEV_IOCTL_FAIL_CMD
2386 +--------------------------------------------------------
2387 +
2388 +Return mount and expire result status from user space to the kernel.
2389 +Both of these calls require an initialized struct autofs_dev_ioctl
2390 +with the ioctlfd field set to the descriptor obtained from the open
2391 +call and the ready.token or fail.token field set to the wait queue
2392 +token number, received by user space in the foregoing mount or expire
2393 +request. The fail.status field is set to the status to be returned when
2394 +sending a failure notification with AUTOFS_DEV_IOCTL_FAIL_CMD.
2395 +
2396 +
2397 +AUTOFS_DEV_IOCTL_SETPIPEFD_CMD
2398 +------------------------------
2399 +
2400 +Set the pipe file descriptor used for kernel communication to the daemon.
2401 +Normally this is set at mount time using an option but when reconnecting
2402 +to a existing mount we need to use this to tell the autofs mount about
2403 +the new kernel pipe descriptor. In order to protect mounts against
2404 +incorrectly setting the pipe descriptor we also require that the autofs
2405 +mount be catatonic (see next call).
2406 +
2407 +The call requires an initialized struct autofs_dev_ioctl with the
2408 +ioctlfd field set to the descriptor obtained from the open call and
2409 +the setpipefd.pipefd field set to descriptor of the pipe. On success
2410 +the call also sets the process group id used to identify the controlling
2411 +process (eg. the owning automount(8) daemon) to the process group of
2412 +the caller.
2413 +
2414 +
2415 +AUTOFS_DEV_IOCTL_CATATONIC_CMD
2416 +------------------------------
2417 +
2418 +Make the autofs mount point catatonic. The autofs mount will no longer
2419 +issue mount requests, the kernel communication pipe descriptor is released
2420 +and any remaining waits in the queue released.
2421 +
2422 +The call requires an initialized struct autofs_dev_ioctl with the
2423 +ioctlfd field set to the descriptor obtained from the open call.
2424 +
2425 +
2426 +AUTOFS_DEV_IOCTL_TIMEOUT_CMD
2427 +----------------------------
2428 +
2429 +Set the expire timeout for mounts withing an autofs mount point.
2430 +
2431 +The call requires an initialized struct autofs_dev_ioctl with the
2432 +ioctlfd field set to the descriptor obtained from the open call.
2433 +The timeout.timeout field is set to the desired timeout and this
2434 +field is set to the value of the value of the current timeout of
2435 +the mount upon successful completion.
2436 +
2437 +
2438 +AUTOFS_DEV_IOCTL_REQUESTER_CMD
2439 +------------------------------
2440 +
2441 +Return the uid and gid of the last process to successfully trigger a the
2442 +mount on the given path dentry.
2443 +
2444 +The call requires an initialized struct autofs_dev_ioctl with the path
2445 +field set to the mount point in question and the size field adjusted
2446 +appropriately as well as the ioctlfd field set to the descriptor obtained
2447 +from the open call. Upon return the struct fields requester.uid and
2448 +requester.gid contain the uid and gid respectively.
2449 +
2450 +When reconstructing an autofs mount tree with active mounts we need to
2451 +re-connect to mounts that may have used the original process uid and
2452 +gid (or string variations of them) for mount lookups within the map entry.
2453 +This call provides the ability to obtain this uid and gid so they may be
2454 +used by user space for the mount map lookups.
2455 +
2456 +
2457 +AUTOFS_DEV_IOCTL_EXPIRE_CMD
2458 +---------------------------
2459 +
2460 +Issue an expire request to the kernel for an autofs mount. Typically
2461 +this ioctl is called until no further expire candidates are found.
2462 +
2463 +The call requires an initialized struct autofs_dev_ioctl with the
2464 +ioctlfd field set to the descriptor obtained from the open call. In
2465 +addition an immediate expire, independent of the mount timeout, can be
2466 +requested by setting the expire.how field to 1. If no expire candidates
2467 +can be found the ioctl returns -1 with errno set to EAGAIN.
2468 +
2469 +This call causes the kernel module to check the mount corresponding
2470 +to the given ioctlfd for mounts that can be expired, issues an expire
2471 +request back to the daemon and waits for completion.
2472 +
2473 +AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD
2474 +------------------------------
2475 +
2476 +Checks if an autofs mount point is in use.
2477 +
2478 +The call requires an initialized struct autofs_dev_ioctl with the
2479 +ioctlfd field set to the descriptor obtained from the open call and
2480 +it returns the result in the askumount.may_umount field, 1 for busy
2481 +and 0 otherwise.
2482 +
2483 +
2484 +AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD
2485 +---------------------------------
2486 +
2487 +Check if the given path is a mountpoint.
2488 +
2489 +The call requires an initialized struct autofs_dev_ioctl. There are two
2490 +possible variations. Both use the path field set to the path of the mount
2491 +point to check and the size field must be adjusted appropriately. One uses
2492 +the ioctlfd field to identify a specific mount point to check while the
2493 +other variation uses the path and optionaly the ismountpoint.in.type
2494 +field set to an autofs mount type. The call returns 1 if this is a mount
2495 +point and sets the ismountpoint.out.devid field to the device number of
2496 +the mount and the ismountpoint.out.magic field to the relevant super
2497 +block magic number (described below) or 0 if it isn't a mountpoint. In
2498 +both cases the the device number (as returned by new_encode_dev()) is
2499 +returned in the ismountpoint.out.devid field.
2500 +
2501 +If supplied with a file descriptor we're looking for a specific mount,
2502 +not necessarily at the top of the mounted stack. In this case the path
2503 +the descriptor corresponds to is considered a mountpoint if it is itself
2504 +a mountpoint or contains a mount, such as a multi-mount without a root
2505 +mount. In this case we return 1 if the descriptor corresponds to a mount
2506 +point and and also returns the super magic of the covering mount if there
2507 +is one or 0 if it isn't a mountpoint.
2508 +
2509 +If a path is supplied (and the ioctlfd field is set to -1) then the path
2510 +is looked up and is checked to see if it is the root of a mount. If a
2511 +type is also given we are looking for a particular autofs mount and if
2512 +a match isn't found a fail is returned. If the the located path is the
2513 +root of a mount 1 is returned along with the super magic of the mount
2514 +or 0 otherwise.
2515 +
2516 --- linux-2.6.19.orig/fs/autofs4/Makefile
2517 +++ linux-2.6.19/fs/autofs4/Makefile
2518 @@ -4,4 +4,4 @@
2519
2520 obj-$(CONFIG_AUTOFS4_FS) += autofs4.o
2521
2522 -autofs4-objs := init.o inode.o root.o symlink.o waitq.o expire.o
2523 +autofs4-objs := init.o inode.o root.o symlink.o waitq.o expire.o dev-ioctl.o
2524 --- /dev/null
2525 +++ linux-2.6.19/fs/autofs4/dev-ioctl.c
2526 @@ -0,0 +1,867 @@
2527 +/*
2528 + * Copyright 2008 Red Hat, Inc. All rights reserved.
2529 + * Copyright 2008 Ian Kent <[email protected]>
2530 + *
2531 + * This file is part of the Linux kernel and is made available under
2532 + * the terms of the GNU General Public License, version 2, or at your
2533 + * option, any later version, incorporated herein by reference.
2534 + */
2535 +
2536 +#include <linux/module.h>
2537 +#include <linux/vmalloc.h>
2538 +#include <linux/miscdevice.h>
2539 +#include <linux/init.h>
2540 +#include <linux/wait.h>
2541 +#include <linux/namei.h>
2542 +#include <linux/fcntl.h>
2543 +#include <linux/file.h>
2544 +#include <linux/sched.h>
2545 +#include <linux/compat.h>
2546 +#include <linux/syscalls.h>
2547 +#include <linux/smp_lock.h>
2548 +#include <linux/magic.h>
2549 +#include <linux/dcache.h>
2550 +#include <linux/uaccess.h>
2551 +
2552 +#include "autofs_i.h"
2553 +
2554 +/*
2555 + * This module implements an interface for routing autofs ioctl control
2556 + * commands via a miscellaneous device file.
2557 + *
2558 + * The alternate interface is needed because we need to be able open
2559 + * an ioctl file descriptor on an autofs mount that may be covered by
2560 + * another mount. This situation arises when starting automount(8)
2561 + * or other user space daemon which uses direct mounts or offset
2562 + * mounts (used for autofs lazy mount/umount of nested mount trees),
2563 + * which have been left busy at at service shutdown.
2564 + */
2565 +
2566 +#define AUTOFS_DEV_IOCTL_SIZE sizeof(struct autofs_dev_ioctl)
2567 +
2568 +typedef int (*ioctl_fn)(struct file *,
2569 +struct autofs_sb_info *, struct autofs_dev_ioctl *);
2570 +
2571 +static int check_name(const char *name)
2572 +{
2573 + if (!strchr(name, '/'))
2574 + return -EINVAL;
2575 + return 0;
2576 +}
2577 +
2578 +/*
2579 + * Check a string doesn't overrun the chunk of
2580 + * memory we copied from user land.
2581 + */
2582 +static int invalid_str(char *str, void *end)
2583 +{
2584 + while ((void *) str <= end)
2585 + if (!*str++)
2586 + return 0;
2587 + return -EINVAL;
2588 +}
2589 +
2590 +/*
2591 + * Check that the user compiled against correct version of autofs
2592 + * misc device code.
2593 + *
2594 + * As well as checking the version compatibility this always copies
2595 + * the kernel interface version out.
2596 + */
2597 +static int check_dev_ioctl_version(int cmd, struct autofs_dev_ioctl *param)
2598 +{
2599 + int err = 0;
2600 +
2601 + if ((AUTOFS_DEV_IOCTL_VERSION_MAJOR != param->ver_major) ||
2602 + (AUTOFS_DEV_IOCTL_VERSION_MINOR < param->ver_minor)) {
2603 + AUTOFS_WARN("ioctl control interface version mismatch: "
2604 + "kernel(%u.%u), user(%u.%u), cmd(%d)",
2605 + AUTOFS_DEV_IOCTL_VERSION_MAJOR,
2606 + AUTOFS_DEV_IOCTL_VERSION_MINOR,
2607 + param->ver_major, param->ver_minor, cmd);
2608 + err = -EINVAL;
2609 + }
2610 +
2611 + /* Fill in the kernel version. */
2612 + param->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
2613 + param->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
2614 +
2615 + return err;
2616 +}
2617 +
2618 +/*
2619 + * Copy parameter control struct, including a possible path allocated
2620 + * at the end of the struct.
2621 + */
2622 +static struct autofs_dev_ioctl *copy_dev_ioctl(struct autofs_dev_ioctl __user *in)
2623 +{
2624 + struct autofs_dev_ioctl tmp, *ads;
2625 +
2626 + if (copy_from_user(&tmp, in, sizeof(tmp)))
2627 + return ERR_PTR(-EFAULT);
2628 +
2629 + if (tmp.size < sizeof(tmp))
2630 + return ERR_PTR(-EINVAL);
2631 +
2632 + ads = kmalloc(tmp.size, GFP_KERNEL);
2633 + if (!ads)
2634 + return ERR_PTR(-ENOMEM);
2635 +
2636 + if (copy_from_user(ads, in, tmp.size)) {
2637 + kfree(ads);
2638 + return ERR_PTR(-EFAULT);
2639 + }
2640 +
2641 + return ads;
2642 +}
2643 +
2644 +static inline void free_dev_ioctl(struct autofs_dev_ioctl *param)
2645 +{
2646 + kfree(param);
2647 + return;
2648 +}
2649 +
2650 +/*
2651 + * Check sanity of parameter control fields and if a path is present
2652 + * check that it is terminated and contains at least one "/".
2653 + */
2654 +static int validate_dev_ioctl(int cmd, struct autofs_dev_ioctl *param)
2655 +{
2656 + int err;
2657 +
2658 + if ((err = check_dev_ioctl_version(cmd, param))) {
2659 + AUTOFS_WARN("invalid device control module version "
2660 + "supplied for cmd(0x%08x)", cmd);
2661 + goto out;
2662 + }
2663 +
2664 + if (param->size > sizeof(*param)) {
2665 + err = invalid_str(param->path,
2666 + (void *) ((size_t) param + param->size));
2667 + if (err) {
2668 + AUTOFS_WARN(
2669 + "path string terminator missing for cmd(0x%08x)",
2670 + cmd);
2671 + goto out;
2672 + }
2673 +
2674 + err = check_name(param->path);
2675 + if (err) {
2676 + AUTOFS_WARN("invalid path supplied for cmd(0x%08x)",
2677 + cmd);
2678 + goto out;
2679 + }
2680 + }
2681 +
2682 + err = 0;
2683 +out:
2684 + return err;
2685 +}
2686 +
2687 +/*
2688 + * Get the autofs super block info struct from the file opened on
2689 + * the autofs mount point.
2690 + */
2691 +static struct autofs_sb_info *autofs_dev_ioctl_sbi(struct file *f)
2692 +{
2693 + struct autofs_sb_info *sbi = NULL;
2694 + struct inode *inode;
2695 +
2696 + if (f) {
2697 + inode = f->f_dentry->d_inode;
2698 + sbi = autofs4_sbi(inode->i_sb);
2699 + }
2700 + return sbi;
2701 +}
2702 +
2703 +/* Return autofs module protocol version */
2704 +static int autofs_dev_ioctl_protover(struct file *fp,
2705 + struct autofs_sb_info *sbi,
2706 + struct autofs_dev_ioctl *param)
2707 +{
2708 + param->protover.version = sbi->version;
2709 + return 0;
2710 +}
2711 +
2712 +/* Return autofs module protocol sub version */
2713 +static int autofs_dev_ioctl_protosubver(struct file *fp,
2714 + struct autofs_sb_info *sbi,
2715 + struct autofs_dev_ioctl *param)
2716 +{
2717 + param->protosubver.sub_version = sbi->sub_version;
2718 + return 0;
2719 +}
2720 +
2721 +/*
2722 + * Walk down the mount stack looking for an autofs mount that
2723 + * has the requested device number (aka. new_encode_dev(sb->s_dev).
2724 + */
2725 +static int autofs_dev_ioctl_find_super(struct nameidata *nd, dev_t devno)
2726 +{
2727 + struct dentry *dentry;
2728 + struct inode *inode;
2729 + struct super_block *sb;
2730 + dev_t s_dev;
2731 + unsigned int err;
2732 +
2733 + err = -ENOENT;
2734 +
2735 + /* Lookup the dentry name at the base of our mount point */
2736 + dentry = d_lookup(nd->dentry, &nd->last);
2737 + if (!dentry)
2738 + goto out;
2739 +
2740 + dput(nd->dentry);
2741 + nd->dentry = dentry;
2742 +
2743 + /* And follow the mount stack looking for our autofs mount */
2744 + while (follow_down(&nd->mnt, &nd->dentry)) {
2745 + inode = nd->dentry->d_inode;
2746 + if (!inode)
2747 + break;
2748 +
2749 + sb = inode->i_sb;
2750 + s_dev = new_encode_dev(sb->s_dev);
2751 + if (devno == s_dev) {
2752 + if (sb->s_magic == AUTOFS_SUPER_MAGIC) {
2753 + err = 0;
2754 + break;
2755 + }
2756 + }
2757 + }
2758 +out:
2759 + return err;
2760 +}
2761 +
2762 +/*
2763 + * Walk down the mount stack looking for an autofs mount that
2764 + * has the requested mount type (ie. indirect, direct or offset).
2765 + */
2766 +static int autofs_dev_ioctl_find_sbi_type(struct nameidata *nd, unsigned int type)
2767 +{
2768 + struct dentry *dentry;
2769 + struct autofs_info *ino;
2770 + unsigned int err;
2771 +
2772 + err = -ENOENT;
2773 +
2774 + /* Lookup the dentry name at the base of our mount point */
2775 + dentry = d_lookup(nd->dentry, &nd->last);
2776 + if (!dentry)
2777 + goto out;
2778 +
2779 + dput(nd->dentry);
2780 + nd->dentry = dentry;
2781 +
2782 + /* And follow the mount stack looking for our autofs mount */
2783 + while (follow_down(&nd->mnt, &nd->dentry)) {
2784 + ino = autofs4_dentry_ino(nd->dentry);
2785 + if (ino && ino->sbi->type & type) {
2786 + err = 0;
2787 + break;
2788 + }
2789 + }
2790 +out:
2791 + return err;
2792 +}
2793 +
2794 +static void autofs_dev_ioctl_fd_install(unsigned int fd, struct file *file)
2795 +{
2796 + struct files_struct *files = current->files;
2797 + struct fdtable *fdt;
2798 +
2799 + spin_lock(&files->file_lock);
2800 + fdt = files_fdtable(files);
2801 + BUG_ON(fdt->fd[fd] != NULL);
2802 + rcu_assign_pointer(fdt->fd[fd], file);
2803 + FD_SET(fd, fdt->close_on_exec);
2804 + spin_unlock(&files->file_lock);
2805 +}
2806 +
2807 +
2808 +/*
2809 + * Open a file descriptor on the autofs mount point corresponding
2810 + * to the given path and device number (aka. new_encode_dev(sb->s_dev)).
2811 + */
2812 +static int autofs_dev_ioctl_open_mountpoint(const char *path, dev_t devid)
2813 +{
2814 + struct file *filp;
2815 + struct nameidata nd;
2816 + int err, fd;
2817 +
2818 + fd = get_unused_fd();
2819 + if (likely(fd >= 0)) {
2820 + /* Get nameidata of the parent directory */
2821 + err = path_lookup(path, LOOKUP_PARENT, &nd);
2822 + if (err)
2823 + goto out;
2824 +
2825 + /*
2826 + * Search down, within the parent, looking for an
2827 + * autofs super block that has the device number
2828 + * corresponding to the autofs fs we want to open.
2829 + */
2830 + err = autofs_dev_ioctl_find_super(&nd, devid);
2831 + if (err) {
2832 + path_release(&nd);
2833 + goto out;
2834 + }
2835 +
2836 + filp = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
2837 + if (IS_ERR(filp)) {
2838 + err = PTR_ERR(filp);
2839 + goto out;
2840 + }
2841 +
2842 + autofs_dev_ioctl_fd_install(fd, filp);
2843 + }
2844 +
2845 + return fd;
2846 +
2847 +out:
2848 + put_unused_fd(fd);
2849 + return err;
2850 +}
2851 +
2852 +/* Open a file descriptor on an autofs mount point */
2853 +static int autofs_dev_ioctl_openmount(struct file *fp,
2854 + struct autofs_sb_info *sbi,
2855 + struct autofs_dev_ioctl *param)
2856 +{
2857 + const char *path;
2858 + dev_t devid;
2859 + int err, fd;
2860 +
2861 + /* param->path has already been checked */
2862 + if (!param->openmount.devid)
2863 + return -EINVAL;
2864 +
2865 + param->ioctlfd = -1;
2866 +
2867 + path = param->path;
2868 + devid = param->openmount.devid;
2869 +
2870 + err = 0;
2871 + fd = autofs_dev_ioctl_open_mountpoint(path, devid);
2872 + if (unlikely(fd < 0)) {
2873 + err = fd;
2874 + goto out;
2875 + }
2876 +
2877 + param->ioctlfd = fd;
2878 +out:
2879 + return err;
2880 +}
2881 +
2882 +/* Close file descriptor allocated above (user can also use close(2)). */
2883 +static int autofs_dev_ioctl_closemount(struct file *fp,
2884 + struct autofs_sb_info *sbi,
2885 + struct autofs_dev_ioctl *param)
2886 +{
2887 + return sys_close(param->ioctlfd);
2888 +}
2889 +
2890 +/*
2891 + * Send "ready" status for an existing wait (either a mount or an expire
2892 + * request).
2893 + */
2894 +static int autofs_dev_ioctl_ready(struct file *fp,
2895 + struct autofs_sb_info *sbi,
2896 + struct autofs_dev_ioctl *param)
2897 +{
2898 + autofs_wqt_t token;
2899 +
2900 + token = (autofs_wqt_t) param->ready.token;
2901 + return autofs4_wait_release(sbi, token, 0);
2902 +}
2903 +
2904 +/*
2905 + * Send "fail" status for an existing wait (either a mount or an expire
2906 + * request).
2907 + */
2908 +static int autofs_dev_ioctl_fail(struct file *fp,
2909 + struct autofs_sb_info *sbi,
2910 + struct autofs_dev_ioctl *param)
2911 +{
2912 + autofs_wqt_t token;
2913 + int status;
2914 +
2915 + token = (autofs_wqt_t) param->fail.token;
2916 + status = param->fail.status ? param->fail.status : -ENOENT;
2917 + return autofs4_wait_release(sbi, token, status);
2918 +}
2919 +
2920 +/*
2921 + * Set the pipe fd for kernel communication to the daemon.
2922 + *
2923 + * Normally this is set at mount using an option but if we
2924 + * are reconnecting to a busy mount then we need to use this
2925 + * to tell the autofs mount about the new kernel pipe fd. In
2926 + * order to protect mounts against incorrectly setting the
2927 + * pipefd we also require that the autofs mount be catatonic.
2928 + *
2929 + * This also sets the process group id used to identify the
2930 + * controlling process (eg. the owning automount(8) daemon).
2931 + */
2932 +static int autofs_dev_ioctl_setpipefd(struct file *fp,
2933 + struct autofs_sb_info *sbi,
2934 + struct autofs_dev_ioctl *param)
2935 +{
2936 + int pipefd;
2937 + int err = 0;
2938 +
2939 + if (param->setpipefd.pipefd == -1)
2940 + return -EINVAL;
2941 +
2942 + pipefd = param->setpipefd.pipefd;
2943 +
2944 + mutex_lock(&sbi->wq_mutex);
2945 + if (!sbi->catatonic) {
2946 + mutex_unlock(&sbi->wq_mutex);
2947 + return -EBUSY;
2948 + } else {
2949 + struct file *pipe = fget(pipefd);
2950 + if (!pipe->f_op || !pipe->f_op->write) {
2951 + err = -EPIPE;
2952 + fput(pipe);
2953 + goto out;
2954 + }
2955 + sbi->oz_pgrp = process_group(current);
2956 + sbi->pipefd = pipefd;
2957 + sbi->pipe = pipe;
2958 + sbi->catatonic = 0;
2959 + }
2960 +out:
2961 + mutex_unlock(&sbi->wq_mutex);
2962 + return err;
2963 +}
2964 +
2965 +/*
2966 + * Make the autofs mount point catatonic, no longer responsive to
2967 + * mount requests. Also closes the kernel pipe file descriptor.
2968 + */
2969 +static int autofs_dev_ioctl_catatonic(struct file *fp,
2970 + struct autofs_sb_info *sbi,
2971 + struct autofs_dev_ioctl *param)
2972 +{
2973 + autofs4_catatonic_mode(sbi);
2974 + return 0;
2975 +}
2976 +
2977 +/* Set the autofs mount timeout */
2978 +static int autofs_dev_ioctl_timeout(struct file *fp,
2979 + struct autofs_sb_info *sbi,
2980 + struct autofs_dev_ioctl *param)
2981 +{
2982 + unsigned long timeout;
2983 +
2984 + timeout = param->timeout.timeout;
2985 + param->timeout.timeout = sbi->exp_timeout / HZ;
2986 + sbi->exp_timeout = timeout * HZ;
2987 + return 0;
2988 +}
2989 +
2990 +/*
2991 + * Return the uid and gid of the last request for the mount
2992 + *
2993 + * When reconstructing an autofs mount tree with active mounts
2994 + * we need to re-connect to mounts that may have used the original
2995 + * process uid and gid (or string variations of them) for mount
2996 + * lookups within the map entry.
2997 + */
2998 +static int autofs_dev_ioctl_requester(struct file *fp,
2999 + struct autofs_sb_info *sbi,
3000 + struct autofs_dev_ioctl *param)
3001 +{
3002 + struct autofs_info *ino;
3003 + struct nameidata nd;
3004 + const char *path;
3005 + dev_t devid;
3006 + int err = -ENOENT;
3007 +
3008 + if (param->size <= sizeof(*param)) {
3009 + err = -EINVAL;
3010 + goto out;
3011 + }
3012 +
3013 + path = param->path;
3014 + devid = sbi->sb->s_dev;
3015 +
3016 + param->requester.uid = param->requester.gid = -1;
3017 +
3018 + /* Get nameidata of the parent directory */
3019 + err = path_lookup(path, LOOKUP_PARENT, &nd);
3020 + if (err)
3021 + goto out;
3022 +
3023 + err = autofs_dev_ioctl_find_super(&nd, devid);
3024 + if (err)
3025 + goto out_release;
3026 +
3027 + ino = autofs4_dentry_ino(nd.dentry);
3028 + if (ino) {
3029 + err = 0;
3030 + autofs4_expire_wait(nd.dentry);
3031 + spin_lock(&sbi->fs_lock);
3032 + param->requester.uid = ino->uid;
3033 + param->requester.gid = ino->gid;
3034 + spin_unlock(&sbi->fs_lock);
3035 + }
3036 +
3037 +out_release:
3038 + path_release(&nd);
3039 +out:
3040 + return err;
3041 +}
3042 +
3043 +/*
3044 + * Call repeatedly until it returns -EAGAIN, meaning there's nothing
3045 + * more that can be done.
3046 + */
3047 +static int autofs_dev_ioctl_expire(struct file *fp,
3048 + struct autofs_sb_info *sbi,
3049 + struct autofs_dev_ioctl *param)
3050 +{
3051 + struct dentry *dentry;
3052 + struct vfsmount *mnt;
3053 + int err = -EAGAIN;
3054 + int how;
3055 +
3056 + how = param->expire.how;
3057 + mnt = fp->f_vfsmnt;
3058 +
3059 + if (autofs_type_trigger(sbi->type))
3060 + dentry = autofs4_expire_direct(sbi->sb, mnt, sbi, how);
3061 + else
3062 + dentry = autofs4_expire_indirect(sbi->sb, mnt, sbi, how);
3063 +
3064 + if (dentry) {
3065 + struct autofs_info *ino = autofs4_dentry_ino(dentry);
3066 +
3067 + /*
3068 + * This is synchronous because it makes the daemon a
3069 + * little easier
3070 + */
3071 + err = autofs4_wait(sbi, dentry, NFY_EXPIRE);
3072 +
3073 + spin_lock(&sbi->fs_lock);
3074 + if (ino->flags & AUTOFS_INF_MOUNTPOINT) {
3075 + ino->flags &= ~AUTOFS_INF_MOUNTPOINT;
3076 + sbi->sb->s_root->d_mounted++;
3077 + }
3078 + ino->flags &= ~AUTOFS_INF_EXPIRING;
3079 + complete_all(&ino->expire_complete);
3080 + spin_unlock(&sbi->fs_lock);
3081 + dput(dentry);
3082 + }
3083 +
3084 + return err;
3085 +}
3086 +
3087 +/* Check if autofs mount point is in use */
3088 +static int autofs_dev_ioctl_askumount(struct file *fp,
3089 + struct autofs_sb_info *sbi,
3090 + struct autofs_dev_ioctl *param)
3091 +{
3092 + param->askumount.may_umount = 0;
3093 + if (may_umount(fp->f_vfsmnt))
3094 + param->askumount.may_umount = 1;
3095 + return 0;
3096 +}
3097 +
3098 +/*
3099 + * Check if the given path is a mountpoint.
3100 + *
3101 + * If we are supplied with the file descriptor of an autofs
3102 + * mount we're looking for a specific mount. In this case
3103 + * the path is considered a mountpoint if it is itself a
3104 + * mountpoint or contains a mount, such as a multi-mount
3105 + * without a root mount. In this case we return 1 if the
3106 + * path is a mount point and the super magic of the covering
3107 + * mount if there is one or 0 if it isn't a mountpoint.
3108 + *
3109 + * If we aren't supplied with a file descriptor then we
3110 + * lookup the nameidata of the path and check if it is the
3111 + * root of a mount. If a type is given we are looking for
3112 + * a particular autofs mount and if we don't find a match
3113 + * we return fail. If the located nameidata path is the
3114 + * root of a mount we return 1 along with the super magic
3115 + * of the mount or 0 otherwise.
3116 + *
3117 + * In both cases the the device number (as returned by
3118 + * new_encode_dev()) is also returned.
3119 + */
3120 +static int autofs_dev_ioctl_ismountpoint(struct file *fp,
3121 + struct autofs_sb_info *sbi,
3122 + struct autofs_dev_ioctl *param)
3123 +{
3124 + struct nameidata nd;
3125 + const char *path;
3126 + unsigned int type;
3127 + unsigned int devid, magic;
3128 + int err = -ENOENT;
3129 +
3130 + if (param->size <= sizeof(*param)) {
3131 + err = -EINVAL;
3132 + goto out;
3133 + }
3134 +
3135 + path = param->path;
3136 + type = param->ismountpoint.in.type;
3137 +
3138 + param->ismountpoint.out.devid = devid = 0;
3139 + param->ismountpoint.out.magic = magic = 0;
3140 +
3141 + if (!fp || param->ioctlfd == -1) {
3142 + if (autofs_type_any(type)) {
3143 + struct super_block *sb;
3144 +
3145 + err = path_lookup(path, LOOKUP_FOLLOW, &nd);
3146 + if (err)
3147 + goto out;
3148 +
3149 + sb = nd.dentry->d_sb;
3150 + devid = new_encode_dev(sb->s_dev);
3151 + } else {
3152 + struct autofs_info *ino;
3153 +
3154 + err = path_lookup(path, LOOKUP_PARENT, &nd);
3155 + if (err)
3156 + goto out;
3157 +
3158 + err = autofs_dev_ioctl_find_sbi_type(&nd, type);
3159 + if (err)
3160 + goto out_release;
3161 +
3162 + ino = autofs4_dentry_ino(nd.dentry);
3163 + devid = autofs4_get_dev(ino->sbi);
3164 + }
3165 +
3166 + err = 0;
3167 + if (nd.dentry->d_inode &&
3168 + nd.mnt->mnt_root == nd.dentry) {
3169 + err = 1;
3170 + magic = nd.dentry->d_inode->i_sb->s_magic;
3171 + }
3172 + } else {
3173 + dev_t devid = new_encode_dev(sbi->sb->s_dev);
3174 +
3175 + err = path_lookup(path, LOOKUP_PARENT, &nd);
3176 + if (err)
3177 + goto out;
3178 +
3179 + err = autofs_dev_ioctl_find_super(&nd, devid);
3180 + if (err)
3181 + goto out_release;
3182 +
3183 + devid = autofs4_get_dev(sbi);
3184 +
3185 + err = have_submounts(nd.dentry);
3186 +
3187 + if (nd.mnt->mnt_mountpoint != nd.mnt->mnt_root) {
3188 + if (follow_down(&nd.mnt, &nd.dentry)) {
3189 + struct inode *inode = nd.dentry->d_inode;
3190 + magic = inode->i_sb->s_magic;
3191 + }
3192 + }
3193 + }
3194 +
3195 + param->ismountpoint.out.devid = devid;
3196 + param->ismountpoint.out.magic = magic;
3197 +
3198 +out_release:
3199 + path_release(&nd);
3200 +out:
3201 + return err;
3202 +}
3203 +
3204 +/*
3205 + * Our range of ioctl numbers isn't 0 based so we need to shift
3206 + * the array index by _IOC_NR(AUTOFS_CTL_IOC_FIRST) for the table
3207 + * lookup.
3208 + */
3209 +#define cmd_idx(cmd) (cmd - _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST))
3210 +
3211 +static ioctl_fn lookup_dev_ioctl(unsigned int cmd)
3212 +{
3213 + static struct {
3214 + int cmd;
3215 + ioctl_fn fn;
3216 + } _ioctls[] = {
3217 + {cmd_idx(AUTOFS_DEV_IOCTL_VERSION_CMD), NULL},
3218 + {cmd_idx(AUTOFS_DEV_IOCTL_PROTOVER_CMD),
3219 + autofs_dev_ioctl_protover},
3220 + {cmd_idx(AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD),
3221 + autofs_dev_ioctl_protosubver},
3222 + {cmd_idx(AUTOFS_DEV_IOCTL_OPENMOUNT_CMD),
3223 + autofs_dev_ioctl_openmount},
3224 + {cmd_idx(AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD),
3225 + autofs_dev_ioctl_closemount},
3226 + {cmd_idx(AUTOFS_DEV_IOCTL_READY_CMD),
3227 + autofs_dev_ioctl_ready},
3228 + {cmd_idx(AUTOFS_DEV_IOCTL_FAIL_CMD),
3229 + autofs_dev_ioctl_fail},
3230 + {cmd_idx(AUTOFS_DEV_IOCTL_SETPIPEFD_CMD),
3231 + autofs_dev_ioctl_setpipefd},
3232 + {cmd_idx(AUTOFS_DEV_IOCTL_CATATONIC_CMD),
3233 + autofs_dev_ioctl_catatonic},
3234 + {cmd_idx(AUTOFS_DEV_IOCTL_TIMEOUT_CMD),
3235 + autofs_dev_ioctl_timeout},
3236 + {cmd_idx(AUTOFS_DEV_IOCTL_REQUESTER_CMD),
3237 + autofs_dev_ioctl_requester},
3238 + {cmd_idx(AUTOFS_DEV_IOCTL_EXPIRE_CMD),
3239 + autofs_dev_ioctl_expire},
3240 + {cmd_idx(AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD),
3241 + autofs_dev_ioctl_askumount},
3242 + {cmd_idx(AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD),
3243 + autofs_dev_ioctl_ismountpoint}
3244 + };
3245 + unsigned int idx = cmd_idx(cmd);
3246 +
3247 + return (idx >= ARRAY_SIZE(_ioctls)) ? NULL : _ioctls[idx].fn;
3248 +}
3249 +
3250 +/* ioctl dispatcher */
3251 +static int _autofs_dev_ioctl(unsigned int command, struct autofs_dev_ioctl __user *user)
3252 +{
3253 + struct autofs_dev_ioctl *param;
3254 + struct file *fp;
3255 + struct autofs_sb_info *sbi;
3256 + unsigned int cmd_first, cmd;
3257 + ioctl_fn fn = NULL;
3258 + int err = 0;
3259 +
3260 + /* only root can play with this */
3261 + if (!capable(CAP_SYS_ADMIN))
3262 + return -EPERM;
3263 +
3264 + cmd_first = _IOC_NR(AUTOFS_DEV_IOCTL_IOC_FIRST);
3265 + cmd = _IOC_NR(command);
3266 +
3267 + if (_IOC_TYPE(command) != _IOC_TYPE(AUTOFS_DEV_IOCTL_IOC_FIRST) ||
3268 + cmd - cmd_first >= AUTOFS_DEV_IOCTL_IOC_COUNT) {
3269 + return -ENOTTY;
3270 + }
3271 +
3272 + /* Copy the parameters into kernel space. */
3273 + param = copy_dev_ioctl(user);
3274 + if (IS_ERR(param))
3275 + return PTR_ERR(param);
3276 +
3277 + err = validate_dev_ioctl(command, param);
3278 + if (err)
3279 + goto out;
3280 +
3281 + /* The validate routine above always sets the version */
3282 + if (cmd == AUTOFS_DEV_IOCTL_VERSION_CMD)
3283 + goto done;
3284 +
3285 + fn = lookup_dev_ioctl(cmd);
3286 + if (!fn) {
3287 + AUTOFS_WARN("unknown command 0x%08x", command);
3288 + return -ENOTTY;
3289 + }
3290 +
3291 + fp = NULL;
3292 + sbi = NULL;
3293 +
3294 + /*
3295 + * For obvious reasons the openmount can't have a file
3296 + * descriptor yet. We don't take a reference to the
3297 + * file during close to allow for immediate release.
3298 + */
3299 + if (cmd != AUTOFS_DEV_IOCTL_OPENMOUNT_CMD &&
3300 + cmd != AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD) {
3301 + fp = fget(param->ioctlfd);
3302 + if (!fp) {
3303 + if (cmd == AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD)
3304 + goto cont;
3305 + err = -EBADF;
3306 + goto out;
3307 + }
3308 +
3309 + if (!fp->f_op) {
3310 + err = -ENOTTY;
3311 + fput(fp);
3312 + goto out;
3313 + }
3314 +
3315 + sbi = autofs_dev_ioctl_sbi(fp);
3316 + if (!sbi || sbi->magic != AUTOFS_SBI_MAGIC) {
3317 + err = -EINVAL;
3318 + fput(fp);
3319 + goto out;
3320 + }
3321 +
3322 + /*
3323 + * Admin needs to be able to set the mount catatonic in
3324 + * order to be able to perform the re-open.
3325 + */
3326 + if (!autofs4_oz_mode(sbi) &&
3327 + cmd != AUTOFS_DEV_IOCTL_CATATONIC_CMD) {
3328 + err = -EACCES;
3329 + fput(fp);
3330 + goto out;
3331 + }
3332 + }
3333 +cont:
3334 + err = fn(fp, sbi, param);
3335 +
3336 + if (fp)
3337 + fput(fp);
3338 +done:
3339 + if (err >= 0 && copy_to_user(user, param, AUTOFS_DEV_IOCTL_SIZE))
3340 + err = -EFAULT;
3341 +out:
3342 + free_dev_ioctl(param);
3343 + return err;
3344 +}
3345 +
3346 +static long autofs_dev_ioctl(struct file *file, uint command, ulong u)
3347 +{
3348 + int err;
3349 + err = _autofs_dev_ioctl(command, (struct autofs_dev_ioctl __user *) u);
3350 + return (long) err;
3351 +}
3352 +
3353 +#ifdef CONFIG_COMPAT
3354 +static long autofs_dev_ioctl_compat(struct file *file, uint command, ulong u)
3355 +{
3356 + return (long) autofs_dev_ioctl(file, command, (ulong) compat_ptr(u));
3357 +}
3358 +#else
3359 +#define autofs_dev_ioctl_compat NULL
3360 +#endif
3361 +
3362 +static const struct file_operations _dev_ioctl_fops = {
3363 + .unlocked_ioctl = autofs_dev_ioctl,
3364 + .compat_ioctl = autofs_dev_ioctl_compat,
3365 + .owner = THIS_MODULE,
3366 +};
3367 +
3368 +static struct miscdevice _autofs_dev_ioctl_misc = {
3369 + .minor = MISC_DYNAMIC_MINOR,
3370 + .name = AUTOFS_DEVICE_NAME,
3371 + .fops = &_dev_ioctl_fops
3372 +};
3373 +
3374 +/* Register/deregister misc character device */
3375 +int autofs_dev_ioctl_init(void)
3376 +{
3377 + int r;
3378 +
3379 + r = misc_register(&_autofs_dev_ioctl_misc);
3380 + if (r) {
3381 + AUTOFS_ERROR("misc_register failed for control device");
3382 + return r;
3383 + }
3384 +
3385 + return 0;
3386 +}
3387 +
3388 +void autofs_dev_ioctl_exit(void)
3389 +{
3390 + misc_deregister(&_autofs_dev_ioctl_misc);
3391 + return;
3392 +}
3393 +
3394 --- linux-2.6.19.orig/fs/autofs4/init.c
3395 +++ linux-2.6.19/fs/autofs4/init.c
3396 @@ -29,11 +29,20 @@ static struct file_system_type autofs_fs
3397
3398 static int __init init_autofs4_fs(void)
3399 {
3400 - return register_filesystem(&autofs_fs_type);
3401 + int err;
3402 +
3403 + err = register_filesystem(&autofs_fs_type);
3404 + if (err)
3405 + return err;
3406 +
3407 + autofs_dev_ioctl_init();
3408 +
3409 + return err;
3410 }
3411
3412 static void __exit exit_autofs4_fs(void)
3413 {
3414 + autofs_dev_ioctl_exit();
3415 unregister_filesystem(&autofs_fs_type);
3416 }
3417
3418 --- /dev/null
3419 +++ linux-2.6.19/include/linux/auto_dev-ioctl.h
3420 @@ -0,0 +1,229 @@
3421 +/*
3422 + * Copyright 2008 Red Hat, Inc. All rights reserved.
3423 + * Copyright 2008 Ian Kent <[email protected]>
3424 + *
3425 + * This file is part of the Linux kernel and is made available under
3426 + * the terms of the GNU General Public License, version 2, or at your
3427 + * option, any later version, incorporated herein by reference.
3428 + */
3429 +
3430 +#ifndef _LINUX_AUTO_DEV_IOCTL_H
3431 +#define _LINUX_AUTO_DEV_IOCTL_H
3432 +
3433 +#include <linux/auto_fs.h>
3434 +
3435 +#ifdef __KERNEL__
3436 +#include <linux/string.h>
3437 +#else
3438 +#include <string.h>
3439 +#endif /* __KERNEL__ */
3440 +
3441 +#define AUTOFS_DEVICE_NAME "autofs"
3442 +
3443 +#define AUTOFS_DEV_IOCTL_VERSION_MAJOR 1
3444 +#define AUTOFS_DEV_IOCTL_VERSION_MINOR 0
3445 +
3446 +#define AUTOFS_DEVID_LEN 16
3447 +
3448 +#define AUTOFS_DEV_IOCTL_SIZE sizeof(struct autofs_dev_ioctl)
3449 +
3450 +/*
3451 + * An ioctl interface for autofs mount point control.
3452 + */
3453 +
3454 +struct args_protover {
3455 + __u32 version;
3456 +};
3457 +
3458 +struct args_protosubver {
3459 + __u32 sub_version;
3460 +};
3461 +
3462 +struct args_openmount {
3463 + __u32 devid;
3464 +};
3465 +
3466 +struct args_ready {
3467 + __u32 token;
3468 +};
3469 +
3470 +struct args_fail {
3471 + __u32 token;
3472 + __s32 status;
3473 +};
3474 +
3475 +struct args_setpipefd {
3476 + __s32 pipefd;
3477 +};
3478 +
3479 +struct args_timeout {
3480 + __u64 timeout;
3481 +};
3482 +
3483 +struct args_requester {
3484 + __u32 uid;
3485 + __u32 gid;
3486 +};
3487 +
3488 +struct args_expire {
3489 + __u32 how;
3490 +};
3491 +
3492 +struct args_askumount {
3493 + __u32 may_umount;
3494 +};
3495 +
3496 +struct args_ismountpoint {
3497 + union {
3498 + struct args_in {
3499 + __u32 type;
3500 + } in;
3501 + struct args_out {
3502 + __u32 devid;
3503 + __u32 magic;
3504 + } out;
3505 + };
3506 +};
3507 +
3508 +/*
3509 + * All the ioctls use this structure.
3510 + * When sending a path size must account for the total length
3511 + * of the chunk of memory otherwise is is the size of the
3512 + * structure.
3513 + */
3514 +
3515 +struct autofs_dev_ioctl {
3516 + __u32 ver_major;
3517 + __u32 ver_minor;
3518 + __u32 size; /* total size of data passed in
3519 + * including this struct */
3520 + __s32 ioctlfd; /* automount command fd */
3521 +
3522 + /* Command parameters */
3523 +
3524 + union {
3525 + struct args_protover protover;
3526 + struct args_protosubver protosubver;
3527 + struct args_openmount openmount;
3528 + struct args_ready ready;
3529 + struct args_fail fail;
3530 + struct args_setpipefd setpipefd;
3531 + struct args_timeout timeout;
3532 + struct args_requester requester;
3533 + struct args_expire expire;
3534 + struct args_askumount askumount;
3535 + struct args_ismountpoint ismountpoint;
3536 + };
3537 +
3538 + char path[0];
3539 +};
3540 +
3541 +static inline void init_autofs_dev_ioctl(struct autofs_dev_ioctl *in)
3542 +{
3543 + memset(in, 0, sizeof(struct autofs_dev_ioctl));
3544 + in->ver_major = AUTOFS_DEV_IOCTL_VERSION_MAJOR;
3545 + in->ver_minor = AUTOFS_DEV_IOCTL_VERSION_MINOR;
3546 + in->size = sizeof(struct autofs_dev_ioctl);
3547 + in->ioctlfd = -1;
3548 + return;
3549 +}
3550 +
3551 +/*
3552 + * If you change this make sure you make the corresponding change
3553 + * to autofs-dev-ioctl.c:lookup_ioctl()
3554 + */
3555 +enum {
3556 + /* Get various version info */
3557 + AUTOFS_DEV_IOCTL_VERSION_CMD = 0x71,
3558 + AUTOFS_DEV_IOCTL_PROTOVER_CMD,
3559 + AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD,
3560 +
3561 + /* Open mount ioctl fd */
3562 + AUTOFS_DEV_IOCTL_OPENMOUNT_CMD,
3563 +
3564 + /* Close mount ioctl fd */
3565 + AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD,
3566 +
3567 + /* Mount/expire status returns */
3568 + AUTOFS_DEV_IOCTL_READY_CMD,
3569 + AUTOFS_DEV_IOCTL_FAIL_CMD,
3570 +
3571 + /* Activate/deactivate autofs mount */
3572 + AUTOFS_DEV_IOCTL_SETPIPEFD_CMD,
3573 + AUTOFS_DEV_IOCTL_CATATONIC_CMD,
3574 +
3575 + /* Expiry timeout */
3576 + AUTOFS_DEV_IOCTL_TIMEOUT_CMD,
3577 +
3578 + /* Get mount last requesting uid and gid */
3579 + AUTOFS_DEV_IOCTL_REQUESTER_CMD,
3580 +
3581 + /* Check for eligible expire candidates */
3582 + AUTOFS_DEV_IOCTL_EXPIRE_CMD,
3583 +
3584 + /* Request busy status */
3585 + AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD,
3586 +
3587 + /* Check if path is a mountpoint */
3588 + AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD,
3589 +};
3590 +
3591 +#define AUTOFS_IOCTL 0x93
3592 +
3593 +#define AUTOFS_DEV_IOCTL_VERSION \
3594 + _IOWR(AUTOFS_IOCTL, \
3595 + AUTOFS_DEV_IOCTL_VERSION_CMD, struct autofs_dev_ioctl)
3596 +
3597 +#define AUTOFS_DEV_IOCTL_PROTOVER \
3598 + _IOWR(AUTOFS_IOCTL, \
3599 + AUTOFS_DEV_IOCTL_PROTOVER_CMD, struct autofs_dev_ioctl)
3600 +
3601 +#define AUTOFS_DEV_IOCTL_PROTOSUBVER \
3602 + _IOWR(AUTOFS_IOCTL, \
3603 + AUTOFS_DEV_IOCTL_PROTOSUBVER_CMD, struct autofs_dev_ioctl)
3604 +
3605 +#define AUTOFS_DEV_IOCTL_OPENMOUNT \
3606 + _IOWR(AUTOFS_IOCTL, \
3607 + AUTOFS_DEV_IOCTL_OPENMOUNT_CMD, struct autofs_dev_ioctl)
3608 +
3609 +#define AUTOFS_DEV_IOCTL_CLOSEMOUNT \
3610 + _IOWR(AUTOFS_IOCTL, \
3611 + AUTOFS_DEV_IOCTL_CLOSEMOUNT_CMD, struct autofs_dev_ioctl)
3612 +
3613 +#define AUTOFS_DEV_IOCTL_READY \
3614 + _IOWR(AUTOFS_IOCTL, \
3615 + AUTOFS_DEV_IOCTL_READY_CMD, struct autofs_dev_ioctl)
3616 +
3617 +#define AUTOFS_DEV_IOCTL_FAIL \
3618 + _IOWR(AUTOFS_IOCTL, \
3619 + AUTOFS_DEV_IOCTL_FAIL_CMD, struct autofs_dev_ioctl)
3620 +
3621 +#define AUTOFS_DEV_IOCTL_SETPIPEFD \
3622 + _IOWR(AUTOFS_IOCTL, \
3623 + AUTOFS_DEV_IOCTL_SETPIPEFD_CMD, struct autofs_dev_ioctl)
3624 +
3625 +#define AUTOFS_DEV_IOCTL_CATATONIC \
3626 + _IOWR(AUTOFS_IOCTL, \
3627 + AUTOFS_DEV_IOCTL_CATATONIC_CMD, struct autofs_dev_ioctl)
3628 +
3629 +#define AUTOFS_DEV_IOCTL_TIMEOUT \
3630 + _IOWR(AUTOFS_IOCTL, \
3631 + AUTOFS_DEV_IOCTL_TIMEOUT_CMD, struct autofs_dev_ioctl)
3632 +
3633 +#define AUTOFS_DEV_IOCTL_REQUESTER \
3634 + _IOWR(AUTOFS_IOCTL, \
3635 + AUTOFS_DEV_IOCTL_REQUESTER_CMD, struct autofs_dev_ioctl)
3636 +
3637 +#define AUTOFS_DEV_IOCTL_EXPIRE \
3638 + _IOWR(AUTOFS_IOCTL, \
3639 + AUTOFS_DEV_IOCTL_EXPIRE_CMD, struct autofs_dev_ioctl)
3640 +
3641 +#define AUTOFS_DEV_IOCTL_ASKUMOUNT \
3642 + _IOWR(AUTOFS_IOCTL, \
3643 + AUTOFS_DEV_IOCTL_ASKUMOUNT_CMD, struct autofs_dev_ioctl)
3644 +
3645 +#define AUTOFS_DEV_IOCTL_ISMOUNTPOINT \
3646 + _IOWR(AUTOFS_IOCTL, \
3647 + AUTOFS_DEV_IOCTL_ISMOUNTPOINT_CMD, struct autofs_dev_ioctl)
3648 +
3649 +#endif /* _LINUX_AUTO_DEV_IOCTL_H */
3650 --- linux-2.6.19.orig/include/linux/auto_fs.h
3651 +++ linux-2.6.19/include/linux/auto_fs.h
3652 @@ -17,11 +17,13 @@
3653 #ifdef __KERNEL__
3654 #include <linux/fs.h>
3655 #include <linux/limits.h>
3656 +#include <linux/types.h>
3657 +#include <linux/ioctl.h>
3658 +#else
3659 #include <asm/types.h>
3660 +#include <sys/ioctl.h>
3661 #endif /* __KERNEL__ */
3662
3663 -#include <linux/ioctl.h>
3664 -
3665 /* This file describes autofs v3 */
3666 #define AUTOFS_PROTO_VERSION 3
3667