blob: 3f0048582248cad53bf930206e7bf6b0e888ed2d [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>
Ian Kent34ca9592006-03-27 01:14:54 -08007 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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 *);
Ian Kent34ca9592006-03-27 01:14:54 -080033static void *autofs4_follow_link(struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
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
Ian Kent34ca9592006-03-27 01:14:54 -080050struct inode_operations autofs4_indirect_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 .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
Ian Kent34ca9592006-03-27 01:14:54 -080058struct inode_operations autofs4_direct_root_inode_operations = {
59 .lookup = autofs4_lookup,
60 .follow_link = autofs4_follow_link,
61};
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063struct inode_operations autofs4_dir_inode_operations = {
64 .lookup = autofs4_lookup,
65 .unlink = autofs4_dir_unlink,
66 .symlink = autofs4_dir_symlink,
67 .mkdir = autofs4_dir_mkdir,
68 .rmdir = autofs4_dir_rmdir,
69};
70
71static int autofs4_root_readdir(struct file *file, void *dirent,
72 filldir_t filldir)
73{
74 struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb);
75 int oz_mode = autofs4_oz_mode(sbi);
76
77 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
78
79 /*
80 * Don't set reghost flag if:
81 * 1) f_pos is larger than zero -- we've already been here.
82 * 2) we haven't even enabled reghosting in the 1st place.
83 * 3) this is the daemon doing a readdir
84 */
85 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
86 sbi->needs_reghost = 1;
87
88 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
89
Ian Kentf360ce32006-03-27 01:14:43 -080090 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int autofs4_dir_open(struct inode *inode, struct file *file)
94{
95 struct dentry *dentry = file->f_dentry;
96 struct vfsmount *mnt = file->f_vfsmnt;
97 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -080098 struct dentry *cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 int status;
100
Ian Kentf360ce32006-03-27 01:14:43 -0800101 status = dcache_dir_open(inode, file);
102 if (status)
103 goto out;
104
105 cursor = file->private_data;
106 cursor->d_fsdata = NULL;
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 DPRINTK("file=%p dentry=%p %.*s",
109 file, dentry, dentry->d_name.len, dentry->d_name.name);
110
111 if (autofs4_oz_mode(sbi))
112 goto out;
113
114 if (autofs4_ispending(dentry)) {
115 DPRINTK("dentry busy");
Ian Kentf360ce32006-03-27 01:14:43 -0800116 dcache_dir_close(inode, file);
117 status = -EBUSY;
118 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120
Ian Kentf360ce32006-03-27 01:14:43 -0800121 status = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
123 struct nameidata nd;
Ian Kentf360ce32006-03-27 01:14:43 -0800124 int empty, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 /* In case there are stale directory dentrys from a failed mount */
127 spin_lock(&dcache_lock);
128 empty = list_empty(&dentry->d_subdirs);
129 spin_unlock(&dcache_lock);
130
131 if (!empty)
132 d_invalidate(dentry);
133
134 nd.flags = LOOKUP_DIRECTORY;
Ian Kentf360ce32006-03-27 01:14:43 -0800135 ret = (dentry->d_op->d_revalidate)(dentry, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Ian Kentf360ce32006-03-27 01:14:43 -0800137 if (!ret) {
138 dcache_dir_close(inode, file);
139 goto out;
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
143 if (d_mountpoint(dentry)) {
144 struct file *fp = NULL;
145 struct vfsmount *fp_mnt = mntget(mnt);
146 struct dentry *fp_dentry = dget(dentry);
147
Ian Kent9b1e3af2005-06-21 17:16:38 -0700148 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
149 dput(fp_dentry);
150 mntput(fp_mnt);
Ian Kentf360ce32006-03-27 01:14:43 -0800151 dcache_dir_close(inode, file);
152 goto out;
Ian Kent9b1e3af2005-06-21 17:16:38 -0700153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
156 status = PTR_ERR(fp);
157 if (IS_ERR(fp)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800158 dcache_dir_close(inode, file);
159 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
Ian Kentf360ce32006-03-27 01:14:43 -0800161 cursor->d_fsdata = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return 0;
Ian Kentf360ce32006-03-27 01:14:43 -0800164out:
165 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
168static int autofs4_dir_close(struct inode *inode, struct file *file)
169{
170 struct dentry *dentry = file->f_dentry;
171 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800172 struct dentry *cursor = file->private_data;
173 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
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");
Ian Kentf360ce32006-03-27 01:14:43 -0800183 status = -EBUSY;
184 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
187 if (d_mountpoint(dentry)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800188 struct file *fp = cursor->d_fsdata;
189 if (!fp) {
190 status = -ENOENT;
191 goto out;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 filp_close(fp, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195out:
Ian Kentf360ce32006-03-27 01:14:43 -0800196 dcache_dir_close(inode, file);
197 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
201{
202 struct dentry *dentry = file->f_dentry;
203 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800204 struct dentry *cursor = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int status;
206
207 DPRINTK("file=%p dentry=%p %.*s",
208 file, dentry, dentry->d_name.len, dentry->d_name.name);
209
210 if (autofs4_oz_mode(sbi))
211 goto out;
212
213 if (autofs4_ispending(dentry)) {
214 DPRINTK("dentry busy");
215 return -EBUSY;
216 }
217
218 if (d_mountpoint(dentry)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800219 struct file *fp = cursor->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 if (!fp)
222 return -ENOENT;
223
224 if (!fp->f_op || !fp->f_op->readdir)
225 goto out;
226
227 status = vfs_readdir(fp, filldir, dirent);
228 file->f_pos = fp->f_pos;
229 if (status)
230 autofs4_copy_atime(file, fp);
231 return status;
232 }
233out:
Ian Kentf360ce32006-03-27 01:14:43 -0800234 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Ian Kent862b1102006-03-27 01:14:48 -0800237static int try_to_fill_dentry(struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Ian Kent862b1102006-03-27 01:14:48 -0800239 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800240 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 int status = 0;
242
243 /* Block on any pending expiry here; invalidate the dentry
244 when expiration is done to trigger mount request with a new
245 dentry */
Ian Kent718c6042006-03-27 01:14:42 -0800246 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 DPRINTK("waiting for expire %p name=%.*s",
248 dentry, dentry->d_name.len, dentry->d_name.name);
249
250 status = autofs4_wait(sbi, dentry, NFY_NONE);
Ian Kent718c6042006-03-27 01:14:42 -0800251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 DPRINTK("expire done status=%d", status);
Ian Kent718c6042006-03-27 01:14:42 -0800253
Ian Kent1684b2b2005-06-21 17:16:41 -0700254 /*
255 * If the directory still exists the mount request must
256 * continue otherwise it can't be followed at the right
257 * time during the walk.
258 */
259 status = d_invalidate(dentry);
260 if (status != -EBUSY)
Ian Kent34ca9592006-03-27 01:14:54 -0800261 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
264 DPRINTK("dentry=%p %.*s ino=%p",
265 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
266
Ian Kent718c6042006-03-27 01:14:42 -0800267 /*
268 * Wait for a pending mount, triggering one if there
269 * isn't one already
270 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (dentry->d_inode == NULL) {
272 DPRINTK("waiting for mount name=%.*s",
273 dentry->d_name.len, dentry->d_name.name);
274
275 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 DPRINTK("mount done status=%d", status);
278
279 if (status && dentry->d_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800280 return status; /* Try to get the kernel to invalidate this dentry */
Ian Kent718c6042006-03-27 01:14:42 -0800281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 /* Turn this into a real negative dentry? */
283 if (status == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 spin_lock(&dentry->d_lock);
285 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
286 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800287 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 } else if (status) {
289 /* Return a negative dentry, but leave it "pending" */
Ian Kent34ca9592006-03-27 01:14:54 -0800290 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292 /* Trigger mount for path component or follow link */
293 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
294 current->link_count) {
295 DPRINTK("waiting for mount name=%.*s",
296 dentry->d_name.len, dentry->d_name.name);
297
298 spin_lock(&dentry->d_lock);
299 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
300 spin_unlock(&dentry->d_lock);
301 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
302
303 DPRINTK("mount done status=%d", status);
304
305 if (status) {
306 spin_lock(&dentry->d_lock);
307 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
308 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800309 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311 }
312
Ian Kente0a7aae2006-03-27 01:14:47 -0800313 /* Initialize expiry counter after successful mount */
314 if (ino)
315 ino->last_used = jiffies;
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 spin_lock(&dentry->d_lock);
318 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
319 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800320 return status;
321}
322
323/* For autofs direct mounts the follow link triggers the mount */
324static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
325{
326 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
327 int oz_mode = autofs4_oz_mode(sbi);
328 unsigned int lookup_type;
329 int status;
330
331 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
332 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
333 nd->flags);
334
335 /* If it's our master or we shouldn't trigger a mount we're done */
336 lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
337 if (oz_mode || !lookup_type)
338 goto done;
339
340 status = try_to_fill_dentry(dentry, 0);
341 if (status)
342 goto out_error;
343
344 if (!autofs4_follow_mount(&nd->mnt, &nd->dentry)) {
345 status = -ENOENT;
346 goto out_error;
347 }
348
349done:
350 return NULL;
351
352out_error:
353 path_release(nd);
354 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/*
358 * Revalidate is called on every cache lookup. Some of those
359 * cache lookups may actually happen while the dentry is not
360 * yet completely filled in, and revalidate has to delay such
361 * lookups..
362 */
Ian Kent718c6042006-03-27 01:14:42 -0800363static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Ian Kent718c6042006-03-27 01:14:42 -0800365 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
367 int oz_mode = autofs4_oz_mode(sbi);
368 int flags = nd ? nd->flags : 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800369 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* Pending dentry */
372 if (autofs4_ispending(dentry)) {
373 if (!oz_mode)
Ian Kent862b1102006-03-27 01:14:48 -0800374 status = try_to_fill_dentry(dentry, flags);
Ian Kent34ca9592006-03-27 01:14:54 -0800375 return !status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 /* Negative dentry.. invalidate if "old" */
379 if (dentry->d_inode == NULL)
Ian Kent2d753e62006-03-27 01:14:44 -0800380 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /* Check for a non-mountpoint directory with no contents */
383 spin_lock(&dcache_lock);
384 if (S_ISDIR(dentry->d_inode->i_mode) &&
385 !d_mountpoint(dentry) &&
Ian Kent90a59c72006-03-27 01:14:50 -0800386 __simple_empty(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 DPRINTK("dentry=%p %.*s, emptydir",
388 dentry, dentry->d_name.len, dentry->d_name.name);
389 spin_unlock(&dcache_lock);
390 if (!oz_mode)
Ian Kent862b1102006-03-27 01:14:48 -0800391 status = try_to_fill_dentry(dentry, flags);
Ian Kent34ca9592006-03-27 01:14:54 -0800392 return !status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 spin_unlock(&dcache_lock);
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return 1;
397}
398
Ian Kent34ca9592006-03-27 01:14:54 -0800399void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct autofs_info *inf;
402
403 DPRINTK("releasing %p", de);
404
405 inf = autofs4_dentry_ino(de);
406 de->d_fsdata = NULL;
407
408 if (inf) {
409 inf->dentry = NULL;
410 inf->inode = NULL;
411
412 autofs4_free_ino(inf);
413 }
414}
415
416/* For dentries of directories in the root dir */
417static struct dentry_operations autofs4_root_dentry_operations = {
418 .d_revalidate = autofs4_revalidate,
419 .d_release = autofs4_dentry_release,
420};
421
422/* For other dentries */
423static struct dentry_operations autofs4_dentry_operations = {
424 .d_revalidate = autofs4_revalidate,
425 .d_release = autofs4_dentry_release,
426};
427
428/* Lookups in the root directory */
429static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
430{
431 struct autofs_sb_info *sbi;
432 int oz_mode;
433
434 DPRINTK("name = %.*s",
435 dentry->d_name.len, dentry->d_name.name);
436
Ian Kent718c6042006-03-27 01:14:42 -0800437 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800439 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
445 current->pid, process_group(current), sbi->catatonic, oz_mode);
446
447 /*
448 * Mark the dentry incomplete, but add it. This is needed so
449 * that the VFS layer knows about the dentry, and we can count
450 * on catching any lookups through the revalidate.
451 *
452 * Let all the hard work be done by the revalidate function that
453 * needs to be able to do this anyway..
454 *
455 * We need to do this before we release the directory semaphore.
456 */
457 dentry->d_op = &autofs4_root_dentry_operations;
458
459 if (!oz_mode) {
460 spin_lock(&dentry->d_lock);
461 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
462 spin_unlock(&dentry->d_lock);
463 }
464 dentry->d_fsdata = NULL;
465 d_add(dentry, NULL);
466
467 if (dentry->d_op && dentry->d_op->d_revalidate) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800468 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 (dentry->d_op->d_revalidate)(dentry, nd);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800470 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 /*
474 * If we are still pending, check if we had to handle
475 * a signal. If so we can force a restart..
476 */
477 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
478 /* See if we were interrupted */
479 if (signal_pending(current)) {
480 sigset_t *sigset = &current->pending.signal;
481 if (sigismember (sigset, SIGKILL) ||
482 sigismember (sigset, SIGQUIT) ||
483 sigismember (sigset, SIGINT)) {
484 return ERR_PTR(-ERESTARTNOINTR);
485 }
486 }
487 }
488
489 /*
490 * If this dentry is unhashed, then we shouldn't honour this
491 * lookup even if the dentry is positive. Returning ENOENT here
492 * doesn't do the right thing for all system calls, but it should
493 * be OK for the operations we permit from an autofs.
494 */
Ian Kent718c6042006-03-27 01:14:42 -0800495 if (dentry->d_inode && d_unhashed(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return ERR_PTR(-ENOENT);
497
498 return NULL;
499}
500
501static int autofs4_dir_symlink(struct inode *dir,
502 struct dentry *dentry,
503 const char *symname)
504{
505 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
506 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800507 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 struct inode *inode;
509 char *cp;
510
511 DPRINTK("%s <- %.*s", symname,
512 dentry->d_name.len, dentry->d_name.name);
513
514 if (!autofs4_oz_mode(sbi))
515 return -EACCES;
516
517 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
518 if (ino == NULL)
519 return -ENOSPC;
520
521 ino->size = strlen(symname);
522 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
523
524 if (cp == NULL) {
525 kfree(ino);
526 return -ENOSPC;
527 }
528
529 strcpy(cp, symname);
530
531 inode = autofs4_get_inode(dir->i_sb, ino);
532 d_instantiate(dentry, inode);
533
534 if (dir == dir->i_sb->s_root->d_inode)
535 dentry->d_op = &autofs4_root_dentry_operations;
536 else
537 dentry->d_op = &autofs4_dentry_operations;
538
539 dentry->d_fsdata = ino;
540 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800541 atomic_inc(&ino->count);
542 p_ino = autofs4_dentry_ino(dentry->d_parent);
543 if (p_ino && dentry->d_parent != dentry)
544 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 ino->inode = inode;
546
547 dir->i_mtime = CURRENT_TIME;
548
549 return 0;
550}
551
552/*
553 * NOTE!
554 *
555 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
556 * that the file no longer exists. However, doing that means that the
557 * VFS layer can turn the dentry into a negative dentry. We don't want
558 * this, because since the unlink is probably the result of an expire.
559 * We simply d_drop it, which allows the dentry lookup to remount it
560 * if necessary.
561 *
562 * If a process is blocked on the dentry waiting for the expire to finish,
563 * it will invalidate the dentry and try to mount with a new one.
564 *
565 * Also see autofs4_dir_rmdir()..
566 */
567static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
568{
569 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
570 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800571 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* This allows root to remove symlinks */
574 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
575 return -EACCES;
576
Ian Kent1aff3c82006-03-27 01:14:46 -0800577 if (atomic_dec_and_test(&ino->count)) {
578 p_ino = autofs4_dentry_ino(dentry->d_parent);
579 if (p_ino && dentry->d_parent != dentry)
580 atomic_dec(&p_ino->count);
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 dput(ino->dentry);
583
584 dentry->d_inode->i_size = 0;
585 dentry->d_inode->i_nlink = 0;
586
587 dir->i_mtime = CURRENT_TIME;
588
589 d_drop(dentry);
590
591 return 0;
592}
593
594static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
595{
596 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
597 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800598 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 if (!autofs4_oz_mode(sbi))
601 return -EACCES;
602
603 spin_lock(&dcache_lock);
604 if (!list_empty(&dentry->d_subdirs)) {
605 spin_unlock(&dcache_lock);
606 return -ENOTEMPTY;
607 }
608 spin_lock(&dentry->d_lock);
609 __d_drop(dentry);
610 spin_unlock(&dentry->d_lock);
611 spin_unlock(&dcache_lock);
612
Ian Kent1aff3c82006-03-27 01:14:46 -0800613 if (atomic_dec_and_test(&ino->count)) {
614 p_ino = autofs4_dentry_ino(dentry->d_parent);
615 if (p_ino && dentry->d_parent != dentry)
616 atomic_dec(&p_ino->count);
617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 dentry->d_inode->i_size = 0;
620 dentry->d_inode->i_nlink = 0;
621
622 if (dir->i_nlink)
623 dir->i_nlink--;
624
625 return 0;
626}
627
628static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
629{
630 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
631 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800632 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct inode *inode;
634
635 if ( !autofs4_oz_mode(sbi) )
636 return -EACCES;
637
638 DPRINTK("dentry %p, creating %.*s",
639 dentry, dentry->d_name.len, dentry->d_name.name);
640
641 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
642 if (ino == NULL)
643 return -ENOSPC;
644
645 inode = autofs4_get_inode(dir->i_sb, ino);
646 d_instantiate(dentry, inode);
647
648 if (dir == dir->i_sb->s_root->d_inode)
649 dentry->d_op = &autofs4_root_dentry_operations;
650 else
651 dentry->d_op = &autofs4_dentry_operations;
652
653 dentry->d_fsdata = ino;
654 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800655 atomic_inc(&ino->count);
656 p_ino = autofs4_dentry_ino(dentry->d_parent);
657 if (p_ino && dentry->d_parent != dentry)
658 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 ino->inode = inode;
660 dir->i_nlink++;
661 dir->i_mtime = CURRENT_TIME;
662
663 return 0;
664}
665
666/* Get/set timeout ioctl() operation */
667static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
668 unsigned long __user *p)
669{
670 int rv;
671 unsigned long ntimeout;
672
673 if ( (rv = get_user(ntimeout, p)) ||
674 (rv = put_user(sbi->exp_timeout/HZ, p)) )
675 return rv;
676
677 if ( ntimeout > ULONG_MAX/HZ )
678 sbi->exp_timeout = 0;
679 else
680 sbi->exp_timeout = ntimeout * HZ;
681
682 return 0;
683}
684
685/* Return protocol version */
686static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
687{
688 return put_user(sbi->version, p);
689}
690
691/* Return protocol sub version */
692static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
693{
694 return put_user(sbi->sub_version, p);
695}
696
697/*
698 * Tells the daemon whether we need to reghost or not. Also, clears
699 * the reghost_needed flag.
700 */
701static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
702{
703 int status;
704
705 DPRINTK("returning %d", sbi->needs_reghost);
706
707 status = put_user(sbi->needs_reghost, p);
708 if ( status )
709 return status;
710
711 sbi->needs_reghost = 0;
712 return 0;
713}
714
715/*
716 * Enable / Disable reghosting ioctl() operation
717 */
718static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
719{
720 int status;
721 int val;
722
723 status = get_user(val, p);
724
725 DPRINTK("reghost = %d", val);
726
727 if (status)
728 return status;
729
730 /* turn on/off reghosting, with the val */
731 sbi->reghost_enabled = val;
732 return 0;
733}
734
735/*
736* Tells the daemon whether it can umount the autofs mount.
737*/
738static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
739{
740 int status = 0;
741
Ian Kente3474a82006-03-27 01:14:51 -0800742 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 status = 1;
744
745 DPRINTK("returning %d", status);
746
747 status = put_user(status, p);
748
749 return status;
750}
751
752/* Identify autofs4_dentries - this is so we can tell if there's
753 an extra dentry refcount or not. We only hold a refcount on the
754 dentry if its non-negative (ie, d_inode != NULL)
755*/
756int is_autofs4_dentry(struct dentry *dentry)
757{
758 return dentry && dentry->d_inode &&
759 (dentry->d_op == &autofs4_root_dentry_operations ||
760 dentry->d_op == &autofs4_dentry_operations) &&
761 dentry->d_fsdata != NULL;
762}
763
764/*
765 * ioctl()'s on the root directory is the chief method for the daemon to
766 * generate kernel reactions
767 */
768static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
769 unsigned int cmd, unsigned long arg)
770{
771 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
772 void __user *p = (void __user *)arg;
773
774 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
775 cmd,arg,sbi,process_group(current));
776
777 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
778 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
779 return -ENOTTY;
780
781 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
782 return -EPERM;
783
784 switch(cmd) {
785 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
786 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
787 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
788 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
789 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
790 autofs4_catatonic_mode(sbi);
791 return 0;
792 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
793 return autofs4_get_protover(sbi, p);
794 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
795 return autofs4_get_protosubver(sbi, p);
796 case AUTOFS_IOC_SETTIMEOUT:
797 return autofs4_get_set_timeout(sbi, p);
798
799 case AUTOFS_IOC_TOGGLEREGHOST:
800 return autofs4_toggle_reghost(sbi, p);
801 case AUTOFS_IOC_ASKREGHOST:
802 return autofs4_ask_reghost(sbi, p);
803
804 case AUTOFS_IOC_ASKUMOUNT:
805 return autofs4_ask_umount(filp->f_vfsmnt, p);
806
807 /* return a single thing to expire */
808 case AUTOFS_IOC_EXPIRE:
809 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
810 /* same as above, but can send multiple expires through pipe */
811 case AUTOFS_IOC_EXPIRE_MULTI:
812 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);
813
814 default:
815 return -ENOSYS;
816 }
817}