Debian Codegraph @

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

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