[PATCH] autofs4: lookup white space cleanup
[linux-2.6-block.git] / fs / autofs4 / root.c
CommitLineData
1da177e4
LT
1/* -*- c -*- --------------------------------------------------------------- *
2 *
3 * linux/fs/autofs/root.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7 * Copyright 2001-2003 Ian Kent <raven@themaw.net>
8 *
9 * This file is part of the Linux kernel and is made available under
10 * the terms of the GNU General Public License, version 2, or at your
11 * option, any later version, incorporated herein by reference.
12 *
13 * ------------------------------------------------------------------------- */
14
16f7e0fe 15#include <linux/capability.h>
1da177e4
LT
16#include <linux/errno.h>
17#include <linux/stat.h>
18#include <linux/param.h>
19#include <linux/time.h>
20#include <linux/smp_lock.h>
21#include "autofs_i.h"
22
23static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
24static int autofs4_dir_unlink(struct inode *,struct dentry *);
25static int autofs4_dir_rmdir(struct inode *,struct dentry *);
26static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
27static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
28static int autofs4_dir_open(struct inode *inode, struct file *file);
29static int autofs4_dir_close(struct inode *inode, struct file *file);
30static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
31static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
32static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
33static int autofs4_dcache_readdir(struct file *, void *, filldir_t);
34
35struct file_operations autofs4_root_operations = {
36 .open = dcache_dir_open,
37 .release = dcache_dir_close,
38 .read = generic_read_dir,
39 .readdir = autofs4_root_readdir,
40 .ioctl = autofs4_root_ioctl,
41};
42
43struct file_operations autofs4_dir_operations = {
44 .open = autofs4_dir_open,
45 .release = autofs4_dir_close,
46 .read = generic_read_dir,
47 .readdir = autofs4_dir_readdir,
48};
49
50struct inode_operations autofs4_root_inode_operations = {
51 .lookup = autofs4_lookup,
52 .unlink = autofs4_dir_unlink,
53 .symlink = autofs4_dir_symlink,
54 .mkdir = autofs4_dir_mkdir,
55 .rmdir = autofs4_dir_rmdir,
56};
57
58struct inode_operations autofs4_dir_inode_operations = {
59 .lookup = autofs4_lookup,
60 .unlink = autofs4_dir_unlink,
61 .symlink = autofs4_dir_symlink,
62 .mkdir = autofs4_dir_mkdir,
63 .rmdir = autofs4_dir_rmdir,
64};
65
66static int autofs4_root_readdir(struct file *file, void *dirent,
67 filldir_t filldir)
68{
69 struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb);
70 int oz_mode = autofs4_oz_mode(sbi);
71
72 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
73
74 /*
75 * Don't set reghost flag if:
76 * 1) f_pos is larger than zero -- we've already been here.
77 * 2) we haven't even enabled reghosting in the 1st place.
78 * 3) this is the daemon doing a readdir
79 */
80 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
81 sbi->needs_reghost = 1;
82
83 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
84
85 return autofs4_dcache_readdir(file, dirent, filldir);
86}
87
88/* Update usage from here to top of tree, so that scan of
89 top-level directories will give a useful result */
9cf6f4b3 90static void autofs4_update_usage(struct vfsmount *mnt, struct dentry *dentry)
1da177e4
LT
91{
92 struct dentry *top = dentry->d_sb->s_root;
93
94 spin_lock(&dcache_lock);
95 for(; dentry != top; dentry = dentry->d_parent) {
96 struct autofs_info *ino = autofs4_dentry_ino(dentry);
97
98 if (ino) {
9cf6f4b3 99 touch_atime(mnt, dentry);
1da177e4
LT
100 ino->last_used = jiffies;
101 }
102 }
103 spin_unlock(&dcache_lock);
104}
105
106/*
107 * From 2.4 kernel readdir.c
108 */
109static int autofs4_dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
110{
111 int i;
112 struct dentry *dentry = filp->f_dentry;
113
114 i = filp->f_pos;
115 switch (i) {
116 case 0:
117 if (filldir(dirent, ".", 1, i, dentry->d_inode->i_ino, DT_DIR) < 0)
118 break;
119 i++;
120 filp->f_pos++;
121 /* fallthrough */
122 case 1:
123 if (filldir(dirent, "..", 2, i, dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
124 break;
125 i++;
126 filp->f_pos++;
127 /* fallthrough */
128 default: {
129 struct list_head *list;
130 int j = i-2;
131
132 spin_lock(&dcache_lock);
133 list = dentry->d_subdirs.next;
134
135 for (;;) {
136 if (list == &dentry->d_subdirs) {
137 spin_unlock(&dcache_lock);
138 return 0;
139 }
140 if (!j)
141 break;
142 j--;
143 list = list->next;
144 }
145
146 while(1) {
5160ee6f
ED
147 struct dentry *de = list_entry(list,
148 struct dentry, d_u.d_child);
1da177e4
LT
149
150 if (!d_unhashed(de) && de->d_inode) {
151 spin_unlock(&dcache_lock);
152 if (filldir(dirent, de->d_name.name, de->d_name.len, filp->f_pos, de->d_inode->i_ino, DT_UNKNOWN) < 0)
153 break;
154 spin_lock(&dcache_lock);
155 }
156 filp->f_pos++;
157 list = list->next;
158 if (list != &dentry->d_subdirs)
159 continue;
160 spin_unlock(&dcache_lock);
161 break;
162 }
163 }
164 }
165 return 0;
166}
167
168static int autofs4_dir_open(struct inode *inode, struct file *file)
169{
170 struct dentry *dentry = file->f_dentry;
171 struct vfsmount *mnt = file->f_vfsmnt;
172 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
173 int status;
174
175 DPRINTK("file=%p dentry=%p %.*s",
176 file, dentry, dentry->d_name.len, dentry->d_name.name);
177
178 if (autofs4_oz_mode(sbi))
179 goto out;
180
181 if (autofs4_ispending(dentry)) {
182 DPRINTK("dentry busy");
183 return -EBUSY;
184 }
185
186 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
187 struct nameidata nd;
188 int empty;
189
190 /* In case there are stale directory dentrys from a failed mount */
191 spin_lock(&dcache_lock);
192 empty = list_empty(&dentry->d_subdirs);
193 spin_unlock(&dcache_lock);
194
195 if (!empty)
196 d_invalidate(dentry);
197
faf3a989
IK
198 nd.dentry = dentry;
199 nd.mnt = mnt;
1da177e4
LT
200 nd.flags = LOOKUP_DIRECTORY;
201 status = (dentry->d_op->d_revalidate)(dentry, &nd);
202
203 if (!status)
204 return -ENOENT;
205 }
206
207 if (d_mountpoint(dentry)) {
208 struct file *fp = NULL;
209 struct vfsmount *fp_mnt = mntget(mnt);
210 struct dentry *fp_dentry = dget(dentry);
211
9b1e3afd
IK
212 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
213 dput(fp_dentry);
214 mntput(fp_mnt);
215 return -ENOENT;
216 }
1da177e4
LT
217
218 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
219 status = PTR_ERR(fp);
220 if (IS_ERR(fp)) {
221 file->private_data = NULL;
222 return status;
223 }
224 file->private_data = fp;
225 }
226out:
227 return 0;
228}
229
230static int autofs4_dir_close(struct inode *inode, struct file *file)
231{
232 struct dentry *dentry = file->f_dentry;
233 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
234
235 DPRINTK("file=%p dentry=%p %.*s",
236 file, dentry, dentry->d_name.len, dentry->d_name.name);
237
238 if (autofs4_oz_mode(sbi))
239 goto out;
240
241 if (autofs4_ispending(dentry)) {
242 DPRINTK("dentry busy");
243 return -EBUSY;
244 }
245
246 if (d_mountpoint(dentry)) {
247 struct file *fp = file->private_data;
248
249 if (!fp)
250 return -ENOENT;
251
252 filp_close(fp, current->files);
253 file->private_data = NULL;
254 }
255out:
256 return 0;
257}
258
259static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
260{
261 struct dentry *dentry = file->f_dentry;
262 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
263 int status;
264
265 DPRINTK("file=%p dentry=%p %.*s",
266 file, dentry, dentry->d_name.len, dentry->d_name.name);
267
268 if (autofs4_oz_mode(sbi))
269 goto out;
270
271 if (autofs4_ispending(dentry)) {
272 DPRINTK("dentry busy");
273 return -EBUSY;
274 }
275
276 if (d_mountpoint(dentry)) {
277 struct file *fp = file->private_data;
278
279 if (!fp)
280 return -ENOENT;
281
282 if (!fp->f_op || !fp->f_op->readdir)
283 goto out;
284
285 status = vfs_readdir(fp, filldir, dirent);
286 file->f_pos = fp->f_pos;
287 if (status)
288 autofs4_copy_atime(file, fp);
289 return status;
290 }
291out:
292 return autofs4_dcache_readdir(file, dirent, filldir);
293}
294
9cf6f4b3 295static int try_to_fill_dentry(struct vfsmount *mnt, struct dentry *dentry, int flags)
1da177e4 296{
9cf6f4b3
CH
297 struct super_block *sb = mnt->mnt_sb;
298 struct autofs_sb_info *sbi = autofs4_sbi(sb);
718c604a 299 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1da177e4
LT
300 int status = 0;
301
302 /* Block on any pending expiry here; invalidate the dentry
303 when expiration is done to trigger mount request with a new
304 dentry */
718c604a 305 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
1da177e4
LT
306 DPRINTK("waiting for expire %p name=%.*s",
307 dentry, dentry->d_name.len, dentry->d_name.name);
308
309 status = autofs4_wait(sbi, dentry, NFY_NONE);
718c604a 310
1da177e4 311 DPRINTK("expire done status=%d", status);
718c604a 312
1684b2bb
IK
313 /*
314 * If the directory still exists the mount request must
315 * continue otherwise it can't be followed at the right
316 * time during the walk.
317 */
318 status = d_invalidate(dentry);
319 if (status != -EBUSY)
320 return 0;
1da177e4
LT
321 }
322
323 DPRINTK("dentry=%p %.*s ino=%p",
324 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
325
718c604a
IK
326 /*
327 * Wait for a pending mount, triggering one if there
328 * isn't one already
329 */
1da177e4
LT
330 if (dentry->d_inode == NULL) {
331 DPRINTK("waiting for mount name=%.*s",
332 dentry->d_name.len, dentry->d_name.name);
333
334 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
718c604a 335
1da177e4
LT
336 DPRINTK("mount done status=%d", status);
337
338 if (status && dentry->d_inode)
339 return 0; /* Try to get the kernel to invalidate this dentry */
718c604a 340
1da177e4
LT
341 /* Turn this into a real negative dentry? */
342 if (status == -ENOENT) {
343 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
344 spin_lock(&dentry->d_lock);
345 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
346 spin_unlock(&dentry->d_lock);
347 return 1;
348 } else if (status) {
349 /* Return a negative dentry, but leave it "pending" */
350 return 1;
351 }
352 /* Trigger mount for path component or follow link */
353 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
354 current->link_count) {
355 DPRINTK("waiting for mount name=%.*s",
356 dentry->d_name.len, dentry->d_name.name);
357
358 spin_lock(&dentry->d_lock);
359 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
360 spin_unlock(&dentry->d_lock);
361 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
362
363 DPRINTK("mount done status=%d", status);
364
365 if (status) {
366 spin_lock(&dentry->d_lock);
367 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
368 spin_unlock(&dentry->d_lock);
369 return 0;
370 }
371 }
372
718c604a
IK
373 /*
374 * We don't update the usages for the autofs daemon itself, this
375 * is necessary for recursive autofs mounts
376 */
1da177e4 377 if (!autofs4_oz_mode(sbi))
9cf6f4b3 378 autofs4_update_usage(mnt, dentry);
1da177e4
LT
379
380 spin_lock(&dentry->d_lock);
381 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
382 spin_unlock(&dentry->d_lock);
383 return 1;
384}
385
386/*
387 * Revalidate is called on every cache lookup. Some of those
388 * cache lookups may actually happen while the dentry is not
389 * yet completely filled in, and revalidate has to delay such
390 * lookups..
391 */
718c604a 392static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
1da177e4 393{
718c604a 394 struct inode *dir = dentry->d_parent->d_inode;
1da177e4
LT
395 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
396 int oz_mode = autofs4_oz_mode(sbi);
397 int flags = nd ? nd->flags : 0;
398 int status = 1;
399
400 /* Pending dentry */
401 if (autofs4_ispending(dentry)) {
402 if (!oz_mode)
9cf6f4b3 403 status = try_to_fill_dentry(nd->mnt, dentry, flags);
1da177e4
LT
404 return status;
405 }
406
407 /* Negative dentry.. invalidate if "old" */
408 if (dentry->d_inode == NULL)
409 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
410
411 /* Check for a non-mountpoint directory with no contents */
412 spin_lock(&dcache_lock);
413 if (S_ISDIR(dentry->d_inode->i_mode) &&
414 !d_mountpoint(dentry) &&
415 list_empty(&dentry->d_subdirs)) {
416 DPRINTK("dentry=%p %.*s, emptydir",
417 dentry, dentry->d_name.len, dentry->d_name.name);
418 spin_unlock(&dcache_lock);
419 if (!oz_mode)
9cf6f4b3 420 status = try_to_fill_dentry(nd->mnt, dentry, flags);
1da177e4
LT
421 return status;
422 }
423 spin_unlock(&dcache_lock);
424
425 /* Update the usage list */
426 if (!oz_mode)
9cf6f4b3 427 autofs4_update_usage(nd->mnt, dentry);
1da177e4
LT
428
429 return 1;
430}
431
432static void autofs4_dentry_release(struct dentry *de)
433{
434 struct autofs_info *inf;
435
436 DPRINTK("releasing %p", de);
437
438 inf = autofs4_dentry_ino(de);
439 de->d_fsdata = NULL;
440
441 if (inf) {
442 inf->dentry = NULL;
443 inf->inode = NULL;
444
445 autofs4_free_ino(inf);
446 }
447}
448
449/* For dentries of directories in the root dir */
450static struct dentry_operations autofs4_root_dentry_operations = {
451 .d_revalidate = autofs4_revalidate,
452 .d_release = autofs4_dentry_release,
453};
454
455/* For other dentries */
456static struct dentry_operations autofs4_dentry_operations = {
457 .d_revalidate = autofs4_revalidate,
458 .d_release = autofs4_dentry_release,
459};
460
461/* Lookups in the root directory */
462static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
463{
464 struct autofs_sb_info *sbi;
465 int oz_mode;
466
467 DPRINTK("name = %.*s",
468 dentry->d_name.len, dentry->d_name.name);
469
718c604a 470 /* File name too long to exist */
1da177e4 471 if (dentry->d_name.len > NAME_MAX)
718c604a 472 return ERR_PTR(-ENAMETOOLONG);
1da177e4
LT
473
474 sbi = autofs4_sbi(dir->i_sb);
1da177e4 475 oz_mode = autofs4_oz_mode(sbi);
718c604a 476
1da177e4
LT
477 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
478 current->pid, process_group(current), sbi->catatonic, oz_mode);
479
480 /*
481 * Mark the dentry incomplete, but add it. This is needed so
482 * that the VFS layer knows about the dentry, and we can count
483 * on catching any lookups through the revalidate.
484 *
485 * Let all the hard work be done by the revalidate function that
486 * needs to be able to do this anyway..
487 *
488 * We need to do this before we release the directory semaphore.
489 */
490 dentry->d_op = &autofs4_root_dentry_operations;
491
492 if (!oz_mode) {
493 spin_lock(&dentry->d_lock);
494 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
495 spin_unlock(&dentry->d_lock);
496 }
497 dentry->d_fsdata = NULL;
498 d_add(dentry, NULL);
499
500 if (dentry->d_op && dentry->d_op->d_revalidate) {
1b1dcc1b 501 mutex_unlock(&dir->i_mutex);
1da177e4 502 (dentry->d_op->d_revalidate)(dentry, nd);
1b1dcc1b 503 mutex_lock(&dir->i_mutex);
1da177e4
LT
504 }
505
506 /*
507 * If we are still pending, check if we had to handle
508 * a signal. If so we can force a restart..
509 */
510 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
511 /* See if we were interrupted */
512 if (signal_pending(current)) {
513 sigset_t *sigset = &current->pending.signal;
514 if (sigismember (sigset, SIGKILL) ||
515 sigismember (sigset, SIGQUIT) ||
516 sigismember (sigset, SIGINT)) {
517 return ERR_PTR(-ERESTARTNOINTR);
518 }
519 }
520 }
521
522 /*
523 * If this dentry is unhashed, then we shouldn't honour this
524 * lookup even if the dentry is positive. Returning ENOENT here
525 * doesn't do the right thing for all system calls, but it should
526 * be OK for the operations we permit from an autofs.
527 */
718c604a 528 if (dentry->d_inode && d_unhashed(dentry))
1da177e4
LT
529 return ERR_PTR(-ENOENT);
530
531 return NULL;
532}
533
534static int autofs4_dir_symlink(struct inode *dir,
535 struct dentry *dentry,
536 const char *symname)
537{
538 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
539 struct autofs_info *ino = autofs4_dentry_ino(dentry);
540 struct inode *inode;
541 char *cp;
542
543 DPRINTK("%s <- %.*s", symname,
544 dentry->d_name.len, dentry->d_name.name);
545
546 if (!autofs4_oz_mode(sbi))
547 return -EACCES;
548
549 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
550 if (ino == NULL)
551 return -ENOSPC;
552
553 ino->size = strlen(symname);
554 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
555
556 if (cp == NULL) {
557 kfree(ino);
558 return -ENOSPC;
559 }
560
561 strcpy(cp, symname);
562
563 inode = autofs4_get_inode(dir->i_sb, ino);
564 d_instantiate(dentry, inode);
565
566 if (dir == dir->i_sb->s_root->d_inode)
567 dentry->d_op = &autofs4_root_dentry_operations;
568 else
569 dentry->d_op = &autofs4_dentry_operations;
570
571 dentry->d_fsdata = ino;
572 ino->dentry = dget(dentry);
573 ino->inode = inode;
574
575 dir->i_mtime = CURRENT_TIME;
576
577 return 0;
578}
579
580/*
581 * NOTE!
582 *
583 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
584 * that the file no longer exists. However, doing that means that the
585 * VFS layer can turn the dentry into a negative dentry. We don't want
586 * this, because since the unlink is probably the result of an expire.
587 * We simply d_drop it, which allows the dentry lookup to remount it
588 * if necessary.
589 *
590 * If a process is blocked on the dentry waiting for the expire to finish,
591 * it will invalidate the dentry and try to mount with a new one.
592 *
593 * Also see autofs4_dir_rmdir()..
594 */
595static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
596{
597 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
598 struct autofs_info *ino = autofs4_dentry_ino(dentry);
599
600 /* This allows root to remove symlinks */
601 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
602 return -EACCES;
603
604 dput(ino->dentry);
605
606 dentry->d_inode->i_size = 0;
607 dentry->d_inode->i_nlink = 0;
608
609 dir->i_mtime = CURRENT_TIME;
610
611 d_drop(dentry);
612
613 return 0;
614}
615
616static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
617{
618 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
619 struct autofs_info *ino = autofs4_dentry_ino(dentry);
620
621 if (!autofs4_oz_mode(sbi))
622 return -EACCES;
623
624 spin_lock(&dcache_lock);
625 if (!list_empty(&dentry->d_subdirs)) {
626 spin_unlock(&dcache_lock);
627 return -ENOTEMPTY;
628 }
629 spin_lock(&dentry->d_lock);
630 __d_drop(dentry);
631 spin_unlock(&dentry->d_lock);
632 spin_unlock(&dcache_lock);
633
634 dput(ino->dentry);
635
636 dentry->d_inode->i_size = 0;
637 dentry->d_inode->i_nlink = 0;
638
639 if (dir->i_nlink)
640 dir->i_nlink--;
641
642 return 0;
643}
644
645static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
646{
647 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
648 struct autofs_info *ino = autofs4_dentry_ino(dentry);
649 struct inode *inode;
650
651 if ( !autofs4_oz_mode(sbi) )
652 return -EACCES;
653
654 DPRINTK("dentry %p, creating %.*s",
655 dentry, dentry->d_name.len, dentry->d_name.name);
656
657 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
658 if (ino == NULL)
659 return -ENOSPC;
660
661 inode = autofs4_get_inode(dir->i_sb, ino);
662 d_instantiate(dentry, inode);
663
664 if (dir == dir->i_sb->s_root->d_inode)
665 dentry->d_op = &autofs4_root_dentry_operations;
666 else
667 dentry->d_op = &autofs4_dentry_operations;
668
669 dentry->d_fsdata = ino;
670 ino->dentry = dget(dentry);
671 ino->inode = inode;
672 dir->i_nlink++;
673 dir->i_mtime = CURRENT_TIME;
674
675 return 0;
676}
677
678/* Get/set timeout ioctl() operation */
679static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
680 unsigned long __user *p)
681{
682 int rv;
683 unsigned long ntimeout;
684
685 if ( (rv = get_user(ntimeout, p)) ||
686 (rv = put_user(sbi->exp_timeout/HZ, p)) )
687 return rv;
688
689 if ( ntimeout > ULONG_MAX/HZ )
690 sbi->exp_timeout = 0;
691 else
692 sbi->exp_timeout = ntimeout * HZ;
693
694 return 0;
695}
696
697/* Return protocol version */
698static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
699{
700 return put_user(sbi->version, p);
701}
702
703/* Return protocol sub version */
704static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
705{
706 return put_user(sbi->sub_version, p);
707}
708
709/*
710 * Tells the daemon whether we need to reghost or not. Also, clears
711 * the reghost_needed flag.
712 */
713static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
714{
715 int status;
716
717 DPRINTK("returning %d", sbi->needs_reghost);
718
719 status = put_user(sbi->needs_reghost, p);
720 if ( status )
721 return status;
722
723 sbi->needs_reghost = 0;
724 return 0;
725}
726
727/*
728 * Enable / Disable reghosting ioctl() operation
729 */
730static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
731{
732 int status;
733 int val;
734
735 status = get_user(val, p);
736
737 DPRINTK("reghost = %d", val);
738
739 if (status)
740 return status;
741
742 /* turn on/off reghosting, with the val */
743 sbi->reghost_enabled = val;
744 return 0;
745}
746
747/*
748* Tells the daemon whether it can umount the autofs mount.
749*/
750static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
751{
752 int status = 0;
753
754 if (may_umount(mnt) == 0)
755 status = 1;
756
757 DPRINTK("returning %d", status);
758
759 status = put_user(status, p);
760
761 return status;
762}
763
764/* Identify autofs4_dentries - this is so we can tell if there's
765 an extra dentry refcount or not. We only hold a refcount on the
766 dentry if its non-negative (ie, d_inode != NULL)
767*/
768int is_autofs4_dentry(struct dentry *dentry)
769{
770 return dentry && dentry->d_inode &&
771 (dentry->d_op == &autofs4_root_dentry_operations ||
772 dentry->d_op == &autofs4_dentry_operations) &&
773 dentry->d_fsdata != NULL;
774}
775
776/*
777 * ioctl()'s on the root directory is the chief method for the daemon to
778 * generate kernel reactions
779 */
780static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
781 unsigned int cmd, unsigned long arg)
782{
783 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
784 void __user *p = (void __user *)arg;
785
786 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
787 cmd,arg,sbi,process_group(current));
788
789 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
790 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
791 return -ENOTTY;
792
793 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
794 return -EPERM;
795
796 switch(cmd) {
797 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
798 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
799 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
800 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
801 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
802 autofs4_catatonic_mode(sbi);
803 return 0;
804 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
805 return autofs4_get_protover(sbi, p);
806 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
807 return autofs4_get_protosubver(sbi, p);
808 case AUTOFS_IOC_SETTIMEOUT:
809 return autofs4_get_set_timeout(sbi, p);
810
811 case AUTOFS_IOC_TOGGLEREGHOST:
812 return autofs4_toggle_reghost(sbi, p);
813 case AUTOFS_IOC_ASKREGHOST:
814 return autofs4_ask_reghost(sbi, p);
815
816 case AUTOFS_IOC_ASKUMOUNT:
817 return autofs4_ask_umount(filp->f_vfsmnt, p);
818
819 /* return a single thing to expire */
820 case AUTOFS_IOC_EXPIRE:
821 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
822 /* same as above, but can send multiple expires through pipe */
823 case AUTOFS_IOC_EXPIRE_MULTI:
824 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);
825
826 default:
827 return -ENOSYS;
828 }
829}