Debian Codegraph @

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

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