blob: 62d8d4acb8bbbe75ba0965214808c259c4b1b292 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080015#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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 */
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -080090static void autofs4_update_usage(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
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) {
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -080099 touch_atime(mnt, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 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) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800147 struct dentry *de = list_entry(list,
148 struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
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
Ian Kentfaf3a982006-01-14 13:20:37 -0800198 nd.dentry = dentry;
199 nd.mnt = mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 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
Ian Kent9b1e3af2005-06-21 17:16:38 -0700212 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
213 dput(fp_dentry);
214 mntput(fp_mnt);
215 return -ENOENT;
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
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
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800295static int try_to_fill_dentry(struct vfsmount *mnt, struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800297 struct super_block *sb = mnt->mnt_sb;
298 struct autofs_sb_info *sbi = autofs4_sbi(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct autofs_info *de_info = autofs4_dentry_ino(dentry);
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 */
305 if (de_info && (de_info->flags & AUTOFS_INF_EXPIRING)) {
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);
310
311 DPRINTK("expire done status=%d", status);
312
Ian Kent1684b2b2005-06-21 17:16:41 -0700313 /*
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
323 DPRINTK("dentry=%p %.*s ino=%p",
324 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
325
326 /* Wait for a pending mount, triggering one if there isn't one already */
327 if (dentry->d_inode == NULL) {
328 DPRINTK("waiting for mount name=%.*s",
329 dentry->d_name.len, dentry->d_name.name);
330
331 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
332
333 DPRINTK("mount done status=%d", status);
334
335 if (status && dentry->d_inode)
336 return 0; /* Try to get the kernel to invalidate this dentry */
337
338 /* Turn this into a real negative dentry? */
339 if (status == -ENOENT) {
340 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
341 spin_lock(&dentry->d_lock);
342 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
343 spin_unlock(&dentry->d_lock);
344 return 1;
345 } else if (status) {
346 /* Return a negative dentry, but leave it "pending" */
347 return 1;
348 }
349 /* Trigger mount for path component or follow link */
350 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
351 current->link_count) {
352 DPRINTK("waiting for mount name=%.*s",
353 dentry->d_name.len, dentry->d_name.name);
354
355 spin_lock(&dentry->d_lock);
356 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
357 spin_unlock(&dentry->d_lock);
358 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
359
360 DPRINTK("mount done status=%d", status);
361
362 if (status) {
363 spin_lock(&dentry->d_lock);
364 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
365 spin_unlock(&dentry->d_lock);
366 return 0;
367 }
368 }
369
370 /* We don't update the usages for the autofs daemon itself, this
371 is necessary for recursive autofs mounts */
372 if (!autofs4_oz_mode(sbi))
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800373 autofs4_update_usage(mnt, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 spin_lock(&dentry->d_lock);
376 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
377 spin_unlock(&dentry->d_lock);
378 return 1;
379}
380
381/*
382 * Revalidate is called on every cache lookup. Some of those
383 * cache lookups may actually happen while the dentry is not
384 * yet completely filled in, and revalidate has to delay such
385 * lookups..
386 */
387static int autofs4_revalidate(struct dentry * dentry, struct nameidata *nd)
388{
389 struct inode * dir = dentry->d_parent->d_inode;
390 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
391 int oz_mode = autofs4_oz_mode(sbi);
392 int flags = nd ? nd->flags : 0;
393 int status = 1;
394
395 /* Pending dentry */
396 if (autofs4_ispending(dentry)) {
397 if (!oz_mode)
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800398 status = try_to_fill_dentry(nd->mnt, dentry, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return status;
400 }
401
402 /* Negative dentry.. invalidate if "old" */
403 if (dentry->d_inode == NULL)
404 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
405
406 /* Check for a non-mountpoint directory with no contents */
407 spin_lock(&dcache_lock);
408 if (S_ISDIR(dentry->d_inode->i_mode) &&
409 !d_mountpoint(dentry) &&
410 list_empty(&dentry->d_subdirs)) {
411 DPRINTK("dentry=%p %.*s, emptydir",
412 dentry, dentry->d_name.len, dentry->d_name.name);
413 spin_unlock(&dcache_lock);
414 if (!oz_mode)
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800415 status = try_to_fill_dentry(nd->mnt, dentry, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return status;
417 }
418 spin_unlock(&dcache_lock);
419
420 /* Update the usage list */
421 if (!oz_mode)
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800422 autofs4_update_usage(nd->mnt, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 return 1;
425}
426
427static void autofs4_dentry_release(struct dentry *de)
428{
429 struct autofs_info *inf;
430
431 DPRINTK("releasing %p", de);
432
433 inf = autofs4_dentry_ino(de);
434 de->d_fsdata = NULL;
435
436 if (inf) {
437 inf->dentry = NULL;
438 inf->inode = NULL;
439
440 autofs4_free_ino(inf);
441 }
442}
443
444/* For dentries of directories in the root dir */
445static struct dentry_operations autofs4_root_dentry_operations = {
446 .d_revalidate = autofs4_revalidate,
447 .d_release = autofs4_dentry_release,
448};
449
450/* For other dentries */
451static struct dentry_operations autofs4_dentry_operations = {
452 .d_revalidate = autofs4_revalidate,
453 .d_release = autofs4_dentry_release,
454};
455
456/* Lookups in the root directory */
457static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
458{
459 struct autofs_sb_info *sbi;
460 int oz_mode;
461
462 DPRINTK("name = %.*s",
463 dentry->d_name.len, dentry->d_name.name);
464
465 if (dentry->d_name.len > NAME_MAX)
466 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
467
468 sbi = autofs4_sbi(dir->i_sb);
469
470 oz_mode = autofs4_oz_mode(sbi);
471 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
472 current->pid, process_group(current), sbi->catatonic, oz_mode);
473
474 /*
475 * Mark the dentry incomplete, but add it. This is needed so
476 * that the VFS layer knows about the dentry, and we can count
477 * on catching any lookups through the revalidate.
478 *
479 * Let all the hard work be done by the revalidate function that
480 * needs to be able to do this anyway..
481 *
482 * We need to do this before we release the directory semaphore.
483 */
484 dentry->d_op = &autofs4_root_dentry_operations;
485
486 if (!oz_mode) {
487 spin_lock(&dentry->d_lock);
488 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
489 spin_unlock(&dentry->d_lock);
490 }
491 dentry->d_fsdata = NULL;
492 d_add(dentry, NULL);
493
494 if (dentry->d_op && dentry->d_op->d_revalidate) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800495 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 (dentry->d_op->d_revalidate)(dentry, nd);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800497 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
500 /*
501 * If we are still pending, check if we had to handle
502 * a signal. If so we can force a restart..
503 */
504 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
505 /* See if we were interrupted */
506 if (signal_pending(current)) {
507 sigset_t *sigset = &current->pending.signal;
508 if (sigismember (sigset, SIGKILL) ||
509 sigismember (sigset, SIGQUIT) ||
510 sigismember (sigset, SIGINT)) {
511 return ERR_PTR(-ERESTARTNOINTR);
512 }
513 }
514 }
515
516 /*
517 * If this dentry is unhashed, then we shouldn't honour this
518 * lookup even if the dentry is positive. Returning ENOENT here
519 * doesn't do the right thing for all system calls, but it should
520 * be OK for the operations we permit from an autofs.
521 */
522 if ( dentry->d_inode && d_unhashed(dentry) )
523 return ERR_PTR(-ENOENT);
524
525 return NULL;
526}
527
528static int autofs4_dir_symlink(struct inode *dir,
529 struct dentry *dentry,
530 const char *symname)
531{
532 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
533 struct autofs_info *ino = autofs4_dentry_ino(dentry);
534 struct inode *inode;
535 char *cp;
536
537 DPRINTK("%s <- %.*s", symname,
538 dentry->d_name.len, dentry->d_name.name);
539
540 if (!autofs4_oz_mode(sbi))
541 return -EACCES;
542
543 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
544 if (ino == NULL)
545 return -ENOSPC;
546
547 ino->size = strlen(symname);
548 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
549
550 if (cp == NULL) {
551 kfree(ino);
552 return -ENOSPC;
553 }
554
555 strcpy(cp, symname);
556
557 inode = autofs4_get_inode(dir->i_sb, ino);
558 d_instantiate(dentry, inode);
559
560 if (dir == dir->i_sb->s_root->d_inode)
561 dentry->d_op = &autofs4_root_dentry_operations;
562 else
563 dentry->d_op = &autofs4_dentry_operations;
564
565 dentry->d_fsdata = ino;
566 ino->dentry = dget(dentry);
567 ino->inode = inode;
568
569 dir->i_mtime = CURRENT_TIME;
570
571 return 0;
572}
573
574/*
575 * NOTE!
576 *
577 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
578 * that the file no longer exists. However, doing that means that the
579 * VFS layer can turn the dentry into a negative dentry. We don't want
580 * this, because since the unlink is probably the result of an expire.
581 * We simply d_drop it, which allows the dentry lookup to remount it
582 * if necessary.
583 *
584 * If a process is blocked on the dentry waiting for the expire to finish,
585 * it will invalidate the dentry and try to mount with a new one.
586 *
587 * Also see autofs4_dir_rmdir()..
588 */
589static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
590{
591 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
592 struct autofs_info *ino = autofs4_dentry_ino(dentry);
593
594 /* This allows root to remove symlinks */
595 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
596 return -EACCES;
597
598 dput(ino->dentry);
599
600 dentry->d_inode->i_size = 0;
601 dentry->d_inode->i_nlink = 0;
602
603 dir->i_mtime = CURRENT_TIME;
604
605 d_drop(dentry);
606
607 return 0;
608}
609
610static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
611{
612 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
613 struct autofs_info *ino = autofs4_dentry_ino(dentry);
614
615 if (!autofs4_oz_mode(sbi))
616 return -EACCES;
617
618 spin_lock(&dcache_lock);
619 if (!list_empty(&dentry->d_subdirs)) {
620 spin_unlock(&dcache_lock);
621 return -ENOTEMPTY;
622 }
623 spin_lock(&dentry->d_lock);
624 __d_drop(dentry);
625 spin_unlock(&dentry->d_lock);
626 spin_unlock(&dcache_lock);
627
628 dput(ino->dentry);
629
630 dentry->d_inode->i_size = 0;
631 dentry->d_inode->i_nlink = 0;
632
633 if (dir->i_nlink)
634 dir->i_nlink--;
635
636 return 0;
637}
638
639static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
640{
641 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
642 struct autofs_info *ino = autofs4_dentry_ino(dentry);
643 struct inode *inode;
644
645 if ( !autofs4_oz_mode(sbi) )
646 return -EACCES;
647
648 DPRINTK("dentry %p, creating %.*s",
649 dentry, dentry->d_name.len, dentry->d_name.name);
650
651 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
652 if (ino == NULL)
653 return -ENOSPC;
654
655 inode = autofs4_get_inode(dir->i_sb, ino);
656 d_instantiate(dentry, inode);
657
658 if (dir == dir->i_sb->s_root->d_inode)
659 dentry->d_op = &autofs4_root_dentry_operations;
660 else
661 dentry->d_op = &autofs4_dentry_operations;
662
663 dentry->d_fsdata = ino;
664 ino->dentry = dget(dentry);
665 ino->inode = inode;
666 dir->i_nlink++;
667 dir->i_mtime = CURRENT_TIME;
668
669 return 0;
670}
671
672/* Get/set timeout ioctl() operation */
673static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
674 unsigned long __user *p)
675{
676 int rv;
677 unsigned long ntimeout;
678
679 if ( (rv = get_user(ntimeout, p)) ||
680 (rv = put_user(sbi->exp_timeout/HZ, p)) )
681 return rv;
682
683 if ( ntimeout > ULONG_MAX/HZ )
684 sbi->exp_timeout = 0;
685 else
686 sbi->exp_timeout = ntimeout * HZ;
687
688 return 0;
689}
690
691/* Return protocol version */
692static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
693{
694 return put_user(sbi->version, p);
695}
696
697/* Return protocol sub version */
698static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
699{
700 return put_user(sbi->sub_version, p);
701}
702
703/*
704 * Tells the daemon whether we need to reghost or not. Also, clears
705 * the reghost_needed flag.
706 */
707static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
708{
709 int status;
710
711 DPRINTK("returning %d", sbi->needs_reghost);
712
713 status = put_user(sbi->needs_reghost, p);
714 if ( status )
715 return status;
716
717 sbi->needs_reghost = 0;
718 return 0;
719}
720
721/*
722 * Enable / Disable reghosting ioctl() operation
723 */
724static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
725{
726 int status;
727 int val;
728
729 status = get_user(val, p);
730
731 DPRINTK("reghost = %d", val);
732
733 if (status)
734 return status;
735
736 /* turn on/off reghosting, with the val */
737 sbi->reghost_enabled = val;
738 return 0;
739}
740
741/*
742* Tells the daemon whether it can umount the autofs mount.
743*/
744static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
745{
746 int status = 0;
747
748 if (may_umount(mnt) == 0)
749 status = 1;
750
751 DPRINTK("returning %d", status);
752
753 status = put_user(status, p);
754
755 return status;
756}
757
758/* Identify autofs4_dentries - this is so we can tell if there's
759 an extra dentry refcount or not. We only hold a refcount on the
760 dentry if its non-negative (ie, d_inode != NULL)
761*/
762int is_autofs4_dentry(struct dentry *dentry)
763{
764 return dentry && dentry->d_inode &&
765 (dentry->d_op == &autofs4_root_dentry_operations ||
766 dentry->d_op == &autofs4_dentry_operations) &&
767 dentry->d_fsdata != NULL;
768}
769
770/*
771 * ioctl()'s on the root directory is the chief method for the daemon to
772 * generate kernel reactions
773 */
774static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
775 unsigned int cmd, unsigned long arg)
776{
777 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
778 void __user *p = (void __user *)arg;
779
780 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
781 cmd,arg,sbi,process_group(current));
782
783 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
784 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
785 return -ENOTTY;
786
787 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
788 return -EPERM;
789
790 switch(cmd) {
791 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
792 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
793 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
794 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
795 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
796 autofs4_catatonic_mode(sbi);
797 return 0;
798 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
799 return autofs4_get_protover(sbi, p);
800 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
801 return autofs4_get_protosubver(sbi, p);
802 case AUTOFS_IOC_SETTIMEOUT:
803 return autofs4_get_set_timeout(sbi, p);
804
805 case AUTOFS_IOC_TOGGLEREGHOST:
806 return autofs4_toggle_reghost(sbi, p);
807 case AUTOFS_IOC_ASKREGHOST:
808 return autofs4_ask_reghost(sbi, p);
809
810 case AUTOFS_IOC_ASKUMOUNT:
811 return autofs4_ask_umount(filp->f_vfsmnt, p);
812
813 /* return a single thing to expire */
814 case AUTOFS_IOC_EXPIRE:
815 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
816 /* same as above, but can send multiple expires through pipe */
817 case AUTOFS_IOC_EXPIRE_MULTI:
818 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);
819
820 default:
821 return -ENOSYS;
822 }
823}