blob: af9a4c6bbadfdece89b194b5f2801ff099613eda [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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34struct file_operations autofs4_root_operations = {
35 .open = dcache_dir_open,
36 .release = dcache_dir_close,
37 .read = generic_read_dir,
38 .readdir = autofs4_root_readdir,
39 .ioctl = autofs4_root_ioctl,
40};
41
42struct file_operations autofs4_dir_operations = {
43 .open = autofs4_dir_open,
44 .release = autofs4_dir_close,
45 .read = generic_read_dir,
46 .readdir = autofs4_dir_readdir,
47};
48
49struct inode_operations autofs4_root_inode_operations = {
50 .lookup = autofs4_lookup,
51 .unlink = autofs4_dir_unlink,
52 .symlink = autofs4_dir_symlink,
53 .mkdir = autofs4_dir_mkdir,
54 .rmdir = autofs4_dir_rmdir,
55};
56
57struct inode_operations autofs4_dir_inode_operations = {
58 .lookup = autofs4_lookup,
59 .unlink = autofs4_dir_unlink,
60 .symlink = autofs4_dir_symlink,
61 .mkdir = autofs4_dir_mkdir,
62 .rmdir = autofs4_dir_rmdir,
63};
64
65static int autofs4_root_readdir(struct file *file, void *dirent,
66 filldir_t filldir)
67{
68 struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb);
69 int oz_mode = autofs4_oz_mode(sbi);
70
71 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
72
73 /*
74 * Don't set reghost flag if:
75 * 1) f_pos is larger than zero -- we've already been here.
76 * 2) we haven't even enabled reghosting in the 1st place.
77 * 3) this is the daemon doing a readdir
78 */
79 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
80 sbi->needs_reghost = 1;
81
82 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
83
Ian Kentf360ce32006-03-27 01:14:43 -080084 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87/* Update usage from here to top of tree, so that scan of
88 top-level directories will give a useful result */
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -080089static void autofs4_update_usage(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 struct dentry *top = dentry->d_sb->s_root;
92
93 spin_lock(&dcache_lock);
94 for(; dentry != top; dentry = dentry->d_parent) {
95 struct autofs_info *ino = autofs4_dentry_ino(dentry);
96
97 if (ino) {
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -080098 touch_atime(mnt, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 ino->last_used = jiffies;
100 }
101 }
102 spin_unlock(&dcache_lock);
103}
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static int autofs4_dir_open(struct inode *inode, struct file *file)
106{
107 struct dentry *dentry = file->f_dentry;
108 struct vfsmount *mnt = file->f_vfsmnt;
109 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800110 struct dentry *cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 int status;
112
Ian Kentf360ce32006-03-27 01:14:43 -0800113 status = dcache_dir_open(inode, file);
114 if (status)
115 goto out;
116
117 cursor = file->private_data;
118 cursor->d_fsdata = NULL;
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 DPRINTK("file=%p dentry=%p %.*s",
121 file, dentry, dentry->d_name.len, dentry->d_name.name);
122
123 if (autofs4_oz_mode(sbi))
124 goto out;
125
126 if (autofs4_ispending(dentry)) {
127 DPRINTK("dentry busy");
Ian Kentf360ce32006-03-27 01:14:43 -0800128 dcache_dir_close(inode, file);
129 status = -EBUSY;
130 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132
Ian Kentf360ce32006-03-27 01:14:43 -0800133 status = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
135 struct nameidata nd;
Ian Kentf360ce32006-03-27 01:14:43 -0800136 int empty, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /* In case there are stale directory dentrys from a failed mount */
139 spin_lock(&dcache_lock);
140 empty = list_empty(&dentry->d_subdirs);
141 spin_unlock(&dcache_lock);
142
143 if (!empty)
144 d_invalidate(dentry);
145
146 nd.flags = LOOKUP_DIRECTORY;
Ian Kentf360ce32006-03-27 01:14:43 -0800147 ret = (dentry->d_op->d_revalidate)(dentry, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Ian Kentf360ce32006-03-27 01:14:43 -0800149 if (!ret) {
150 dcache_dir_close(inode, file);
151 goto out;
152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
155 if (d_mountpoint(dentry)) {
156 struct file *fp = NULL;
157 struct vfsmount *fp_mnt = mntget(mnt);
158 struct dentry *fp_dentry = dget(dentry);
159
Ian Kent9b1e3af2005-06-21 17:16:38 -0700160 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
161 dput(fp_dentry);
162 mntput(fp_mnt);
Ian Kentf360ce32006-03-27 01:14:43 -0800163 dcache_dir_close(inode, file);
164 goto out;
Ian Kent9b1e3af2005-06-21 17:16:38 -0700165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
168 status = PTR_ERR(fp);
169 if (IS_ERR(fp)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800170 dcache_dir_close(inode, file);
171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Ian Kentf360ce32006-03-27 01:14:43 -0800173 cursor->d_fsdata = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return 0;
Ian Kentf360ce32006-03-27 01:14:43 -0800176out:
177 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180static int autofs4_dir_close(struct inode *inode, struct file *file)
181{
182 struct dentry *dentry = file->f_dentry;
183 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800184 struct dentry *cursor = file->private_data;
185 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 DPRINTK("file=%p dentry=%p %.*s",
188 file, dentry, dentry->d_name.len, dentry->d_name.name);
189
190 if (autofs4_oz_mode(sbi))
191 goto out;
192
193 if (autofs4_ispending(dentry)) {
194 DPRINTK("dentry busy");
Ian Kentf360ce32006-03-27 01:14:43 -0800195 status = -EBUSY;
196 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198
199 if (d_mountpoint(dentry)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800200 struct file *fp = cursor->d_fsdata;
201 if (!fp) {
202 status = -ENOENT;
203 goto out;
204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 filp_close(fp, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207out:
Ian Kentf360ce32006-03-27 01:14:43 -0800208 dcache_dir_close(inode, file);
209 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
213{
214 struct dentry *dentry = file->f_dentry;
215 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800216 struct dentry *cursor = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 int status;
218
219 DPRINTK("file=%p dentry=%p %.*s",
220 file, dentry, dentry->d_name.len, dentry->d_name.name);
221
222 if (autofs4_oz_mode(sbi))
223 goto out;
224
225 if (autofs4_ispending(dentry)) {
226 DPRINTK("dentry busy");
227 return -EBUSY;
228 }
229
230 if (d_mountpoint(dentry)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800231 struct file *fp = cursor->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (!fp)
234 return -ENOENT;
235
236 if (!fp->f_op || !fp->f_op->readdir)
237 goto out;
238
239 status = vfs_readdir(fp, filldir, dirent);
240 file->f_pos = fp->f_pos;
241 if (status)
242 autofs4_copy_atime(file, fp);
243 return status;
244 }
245out:
Ian Kentf360ce32006-03-27 01:14:43 -0800246 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800249static int try_to_fill_dentry(struct vfsmount *mnt, struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800251 struct super_block *sb = mnt->mnt_sb;
252 struct autofs_sb_info *sbi = autofs4_sbi(sb);
Ian Kent718c6042006-03-27 01:14:42 -0800253 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int status = 0;
255
256 /* Block on any pending expiry here; invalidate the dentry
257 when expiration is done to trigger mount request with a new
258 dentry */
Ian Kent718c6042006-03-27 01:14:42 -0800259 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 DPRINTK("waiting for expire %p name=%.*s",
261 dentry, dentry->d_name.len, dentry->d_name.name);
262
263 status = autofs4_wait(sbi, dentry, NFY_NONE);
Ian Kent718c6042006-03-27 01:14:42 -0800264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 DPRINTK("expire done status=%d", status);
Ian Kent718c6042006-03-27 01:14:42 -0800266
Ian Kent1684b2b2005-06-21 17:16:41 -0700267 /*
268 * If the directory still exists the mount request must
269 * continue otherwise it can't be followed at the right
270 * time during the walk.
271 */
272 status = d_invalidate(dentry);
273 if (status != -EBUSY)
274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
277 DPRINTK("dentry=%p %.*s ino=%p",
278 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
279
Ian Kent718c6042006-03-27 01:14:42 -0800280 /*
281 * Wait for a pending mount, triggering one if there
282 * isn't one already
283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (dentry->d_inode == NULL) {
285 DPRINTK("waiting for mount name=%.*s",
286 dentry->d_name.len, dentry->d_name.name);
287
288 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 DPRINTK("mount done status=%d", status);
291
292 if (status && dentry->d_inode)
293 return 0; /* Try to get the kernel to invalidate this dentry */
Ian Kent718c6042006-03-27 01:14:42 -0800294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* Turn this into a real negative dentry? */
296 if (status == -ENOENT) {
297 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
298 spin_lock(&dentry->d_lock);
299 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
300 spin_unlock(&dentry->d_lock);
301 return 1;
302 } else if (status) {
303 /* Return a negative dentry, but leave it "pending" */
304 return 1;
305 }
306 /* Trigger mount for path component or follow link */
307 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
308 current->link_count) {
309 DPRINTK("waiting for mount name=%.*s",
310 dentry->d_name.len, dentry->d_name.name);
311
312 spin_lock(&dentry->d_lock);
313 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
314 spin_unlock(&dentry->d_lock);
315 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
316
317 DPRINTK("mount done status=%d", status);
318
319 if (status) {
320 spin_lock(&dentry->d_lock);
321 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
322 spin_unlock(&dentry->d_lock);
323 return 0;
324 }
325 }
326
Ian Kent718c6042006-03-27 01:14:42 -0800327 /*
328 * We don't update the usages for the autofs daemon itself, this
329 * is necessary for recursive autofs mounts
330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (!autofs4_oz_mode(sbi))
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800332 autofs4_update_usage(mnt, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 spin_lock(&dentry->d_lock);
335 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
336 spin_unlock(&dentry->d_lock);
337 return 1;
338}
339
340/*
341 * Revalidate is called on every cache lookup. Some of those
342 * cache lookups may actually happen while the dentry is not
343 * yet completely filled in, and revalidate has to delay such
344 * lookups..
345 */
Ian Kent718c6042006-03-27 01:14:42 -0800346static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Ian Kent718c6042006-03-27 01:14:42 -0800348 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
350 int oz_mode = autofs4_oz_mode(sbi);
351 int flags = nd ? nd->flags : 0;
352 int status = 1;
353
354 /* Pending dentry */
355 if (autofs4_ispending(dentry)) {
356 if (!oz_mode)
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800357 status = try_to_fill_dentry(nd->mnt, dentry, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return status;
359 }
360
361 /* Negative dentry.. invalidate if "old" */
362 if (dentry->d_inode == NULL)
363 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
364
365 /* Check for a non-mountpoint directory with no contents */
366 spin_lock(&dcache_lock);
367 if (S_ISDIR(dentry->d_inode->i_mode) &&
368 !d_mountpoint(dentry) &&
369 list_empty(&dentry->d_subdirs)) {
370 DPRINTK("dentry=%p %.*s, emptydir",
371 dentry, dentry->d_name.len, dentry->d_name.name);
372 spin_unlock(&dcache_lock);
373 if (!oz_mode)
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800374 status = try_to_fill_dentry(nd->mnt, dentry, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return status;
376 }
377 spin_unlock(&dcache_lock);
378
379 /* Update the usage list */
380 if (!oz_mode)
Christoph Hellwig9cf6f4b2006-01-09 20:52:02 -0800381 autofs4_update_usage(nd->mnt, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 return 1;
384}
385
386static void autofs4_dentry_release(struct dentry *de)
387{
388 struct autofs_info *inf;
389
390 DPRINTK("releasing %p", de);
391
392 inf = autofs4_dentry_ino(de);
393 de->d_fsdata = NULL;
394
395 if (inf) {
396 inf->dentry = NULL;
397 inf->inode = NULL;
398
399 autofs4_free_ino(inf);
400 }
401}
402
403/* For dentries of directories in the root dir */
404static struct dentry_operations autofs4_root_dentry_operations = {
405 .d_revalidate = autofs4_revalidate,
406 .d_release = autofs4_dentry_release,
407};
408
409/* For other dentries */
410static struct dentry_operations autofs4_dentry_operations = {
411 .d_revalidate = autofs4_revalidate,
412 .d_release = autofs4_dentry_release,
413};
414
415/* Lookups in the root directory */
416static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
417{
418 struct autofs_sb_info *sbi;
419 int oz_mode;
420
421 DPRINTK("name = %.*s",
422 dentry->d_name.len, dentry->d_name.name);
423
Ian Kent718c6042006-03-27 01:14:42 -0800424 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800426 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
432 current->pid, process_group(current), sbi->catatonic, oz_mode);
433
434 /*
435 * Mark the dentry incomplete, but add it. This is needed so
436 * that the VFS layer knows about the dentry, and we can count
437 * on catching any lookups through the revalidate.
438 *
439 * Let all the hard work be done by the revalidate function that
440 * needs to be able to do this anyway..
441 *
442 * We need to do this before we release the directory semaphore.
443 */
444 dentry->d_op = &autofs4_root_dentry_operations;
445
446 if (!oz_mode) {
447 spin_lock(&dentry->d_lock);
448 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
449 spin_unlock(&dentry->d_lock);
450 }
451 dentry->d_fsdata = NULL;
452 d_add(dentry, NULL);
453
454 if (dentry->d_op && dentry->d_op->d_revalidate) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800455 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 (dentry->d_op->d_revalidate)(dentry, nd);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800457 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
460 /*
461 * If we are still pending, check if we had to handle
462 * a signal. If so we can force a restart..
463 */
464 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
465 /* See if we were interrupted */
466 if (signal_pending(current)) {
467 sigset_t *sigset = &current->pending.signal;
468 if (sigismember (sigset, SIGKILL) ||
469 sigismember (sigset, SIGQUIT) ||
470 sigismember (sigset, SIGINT)) {
471 return ERR_PTR(-ERESTARTNOINTR);
472 }
473 }
474 }
475
476 /*
477 * If this dentry is unhashed, then we shouldn't honour this
478 * lookup even if the dentry is positive. Returning ENOENT here
479 * doesn't do the right thing for all system calls, but it should
480 * be OK for the operations we permit from an autofs.
481 */
Ian Kent718c6042006-03-27 01:14:42 -0800482 if (dentry->d_inode && d_unhashed(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return ERR_PTR(-ENOENT);
484
485 return NULL;
486}
487
488static int autofs4_dir_symlink(struct inode *dir,
489 struct dentry *dentry,
490 const char *symname)
491{
492 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
493 struct autofs_info *ino = autofs4_dentry_ino(dentry);
494 struct inode *inode;
495 char *cp;
496
497 DPRINTK("%s <- %.*s", symname,
498 dentry->d_name.len, dentry->d_name.name);
499
500 if (!autofs4_oz_mode(sbi))
501 return -EACCES;
502
503 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
504 if (ino == NULL)
505 return -ENOSPC;
506
507 ino->size = strlen(symname);
508 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
509
510 if (cp == NULL) {
511 kfree(ino);
512 return -ENOSPC;
513 }
514
515 strcpy(cp, symname);
516
517 inode = autofs4_get_inode(dir->i_sb, ino);
518 d_instantiate(dentry, inode);
519
520 if (dir == dir->i_sb->s_root->d_inode)
521 dentry->d_op = &autofs4_root_dentry_operations;
522 else
523 dentry->d_op = &autofs4_dentry_operations;
524
525 dentry->d_fsdata = ino;
526 ino->dentry = dget(dentry);
527 ino->inode = inode;
528
529 dir->i_mtime = CURRENT_TIME;
530
531 return 0;
532}
533
534/*
535 * NOTE!
536 *
537 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
538 * that the file no longer exists. However, doing that means that the
539 * VFS layer can turn the dentry into a negative dentry. We don't want
540 * this, because since the unlink is probably the result of an expire.
541 * We simply d_drop it, which allows the dentry lookup to remount it
542 * if necessary.
543 *
544 * If a process is blocked on the dentry waiting for the expire to finish,
545 * it will invalidate the dentry and try to mount with a new one.
546 *
547 * Also see autofs4_dir_rmdir()..
548 */
549static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
550{
551 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
552 struct autofs_info *ino = autofs4_dentry_ino(dentry);
553
554 /* This allows root to remove symlinks */
555 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
556 return -EACCES;
557
558 dput(ino->dentry);
559
560 dentry->d_inode->i_size = 0;
561 dentry->d_inode->i_nlink = 0;
562
563 dir->i_mtime = CURRENT_TIME;
564
565 d_drop(dentry);
566
567 return 0;
568}
569
570static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
571{
572 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
573 struct autofs_info *ino = autofs4_dentry_ino(dentry);
574
575 if (!autofs4_oz_mode(sbi))
576 return -EACCES;
577
578 spin_lock(&dcache_lock);
579 if (!list_empty(&dentry->d_subdirs)) {
580 spin_unlock(&dcache_lock);
581 return -ENOTEMPTY;
582 }
583 spin_lock(&dentry->d_lock);
584 __d_drop(dentry);
585 spin_unlock(&dentry->d_lock);
586 spin_unlock(&dcache_lock);
587
588 dput(ino->dentry);
589
590 dentry->d_inode->i_size = 0;
591 dentry->d_inode->i_nlink = 0;
592
593 if (dir->i_nlink)
594 dir->i_nlink--;
595
596 return 0;
597}
598
599static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
600{
601 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
602 struct autofs_info *ino = autofs4_dentry_ino(dentry);
603 struct inode *inode;
604
605 if ( !autofs4_oz_mode(sbi) )
606 return -EACCES;
607
608 DPRINTK("dentry %p, creating %.*s",
609 dentry, dentry->d_name.len, dentry->d_name.name);
610
611 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
612 if (ino == NULL)
613 return -ENOSPC;
614
615 inode = autofs4_get_inode(dir->i_sb, ino);
616 d_instantiate(dentry, inode);
617
618 if (dir == dir->i_sb->s_root->d_inode)
619 dentry->d_op = &autofs4_root_dentry_operations;
620 else
621 dentry->d_op = &autofs4_dentry_operations;
622
623 dentry->d_fsdata = ino;
624 ino->dentry = dget(dentry);
625 ino->inode = inode;
626 dir->i_nlink++;
627 dir->i_mtime = CURRENT_TIME;
628
629 return 0;
630}
631
632/* Get/set timeout ioctl() operation */
633static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
634 unsigned long __user *p)
635{
636 int rv;
637 unsigned long ntimeout;
638
639 if ( (rv = get_user(ntimeout, p)) ||
640 (rv = put_user(sbi->exp_timeout/HZ, p)) )
641 return rv;
642
643 if ( ntimeout > ULONG_MAX/HZ )
644 sbi->exp_timeout = 0;
645 else
646 sbi->exp_timeout = ntimeout * HZ;
647
648 return 0;
649}
650
651/* Return protocol version */
652static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
653{
654 return put_user(sbi->version, p);
655}
656
657/* Return protocol sub version */
658static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
659{
660 return put_user(sbi->sub_version, p);
661}
662
663/*
664 * Tells the daemon whether we need to reghost or not. Also, clears
665 * the reghost_needed flag.
666 */
667static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
668{
669 int status;
670
671 DPRINTK("returning %d", sbi->needs_reghost);
672
673 status = put_user(sbi->needs_reghost, p);
674 if ( status )
675 return status;
676
677 sbi->needs_reghost = 0;
678 return 0;
679}
680
681/*
682 * Enable / Disable reghosting ioctl() operation
683 */
684static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
685{
686 int status;
687 int val;
688
689 status = get_user(val, p);
690
691 DPRINTK("reghost = %d", val);
692
693 if (status)
694 return status;
695
696 /* turn on/off reghosting, with the val */
697 sbi->reghost_enabled = val;
698 return 0;
699}
700
701/*
702* Tells the daemon whether it can umount the autofs mount.
703*/
704static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
705{
706 int status = 0;
707
708 if (may_umount(mnt) == 0)
709 status = 1;
710
711 DPRINTK("returning %d", status);
712
713 status = put_user(status, p);
714
715 return status;
716}
717
718/* Identify autofs4_dentries - this is so we can tell if there's
719 an extra dentry refcount or not. We only hold a refcount on the
720 dentry if its non-negative (ie, d_inode != NULL)
721*/
722int is_autofs4_dentry(struct dentry *dentry)
723{
724 return dentry && dentry->d_inode &&
725 (dentry->d_op == &autofs4_root_dentry_operations ||
726 dentry->d_op == &autofs4_dentry_operations) &&
727 dentry->d_fsdata != NULL;
728}
729
730/*
731 * ioctl()'s on the root directory is the chief method for the daemon to
732 * generate kernel reactions
733 */
734static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
735 unsigned int cmd, unsigned long arg)
736{
737 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
738 void __user *p = (void __user *)arg;
739
740 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
741 cmd,arg,sbi,process_group(current));
742
743 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
744 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
745 return -ENOTTY;
746
747 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
748 return -EPERM;
749
750 switch(cmd) {
751 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
752 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
753 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
754 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
755 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
756 autofs4_catatonic_mode(sbi);
757 return 0;
758 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
759 return autofs4_get_protover(sbi, p);
760 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
761 return autofs4_get_protosubver(sbi, p);
762 case AUTOFS_IOC_SETTIMEOUT:
763 return autofs4_get_set_timeout(sbi, p);
764
765 case AUTOFS_IOC_TOGGLEREGHOST:
766 return autofs4_toggle_reghost(sbi, p);
767 case AUTOFS_IOC_ASKREGHOST:
768 return autofs4_ask_reghost(sbi, p);
769
770 case AUTOFS_IOC_ASKUMOUNT:
771 return autofs4_ask_umount(filp->f_vfsmnt, p);
772
773 /* return a single thing to expire */
774 case AUTOFS_IOC_EXPIRE:
775 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
776 /* same as above, but can send multiple expires through pipe */
777 case AUTOFS_IOC_EXPIRE_MULTI:
778 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);
779
780 default:
781 return -ENOSYS;
782 }
783}