Debian Codegraph @

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

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