blob: 61d1dca1688446b9d26c741cc6542972af020b58 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "autofs_i.h"
21
22static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
23static int autofs4_dir_unlink(struct inode *,struct dentry *);
24static int autofs4_dir_rmdir(struct inode *,struct dentry *);
25static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
26static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
27static int autofs4_dir_open(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
29static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
Ian Kent34ca9592006-03-27 01:14:54 -080030static void *autofs4_follow_link(struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Ian Kent6d5cb922008-07-23 21:30:15 -070032#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
33#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
34
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080035const struct file_operations autofs4_root_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .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
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080043const struct file_operations autofs4_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .open = autofs4_dir_open,
Ian Kentff9cd492008-07-23 21:30:24 -070045 .release = dcache_dir_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 .read = generic_read_dir,
Ian Kentff9cd492008-07-23 21:30:24 -070047 .readdir = dcache_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Arjan van de Ven754661f2007-02-12 00:55:38 -080050const struct 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
Arjan van de Ven754661f2007-02-12 00:55:38 -080058const struct inode_operations autofs4_direct_root_inode_operations = {
Ian Kent34ca9592006-03-27 01:14:54 -080059 .lookup = autofs4_lookup,
Ian Kent871f9432006-03-27 01:14:58 -080060 .unlink = autofs4_dir_unlink,
61 .mkdir = autofs4_dir_mkdir,
62 .rmdir = autofs4_dir_rmdir,
Ian Kent34ca9592006-03-27 01:14:54 -080063 .follow_link = autofs4_follow_link,
64};
65
Arjan van de Ven754661f2007-02-12 00:55:38 -080066const struct inode_operations autofs4_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .lookup = autofs4_lookup,
68 .unlink = autofs4_dir_unlink,
69 .symlink = autofs4_dir_symlink,
70 .mkdir = autofs4_dir_mkdir,
71 .rmdir = autofs4_dir_rmdir,
72};
73
74static int autofs4_root_readdir(struct file *file, void *dirent,
75 filldir_t filldir)
76{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -080077 struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int oz_mode = autofs4_oz_mode(sbi);
79
80 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
81
82 /*
83 * Don't set reghost flag if:
84 * 1) f_pos is larger than zero -- we've already been here.
85 * 2) we haven't even enabled reghosting in the 1st place.
86 * 3) this is the daemon doing a readdir
87 */
88 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
89 sbi->needs_reghost = 1;
90
91 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
92
Ian Kentf360ce32006-03-27 01:14:43 -080093 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int autofs4_dir_open(struct inode *inode, struct file *file)
97{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -080098 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 DPRINTK("file=%p dentry=%p %.*s",
102 file, dentry, dentry->d_name.len, dentry->d_name.name);
103
104 if (autofs4_oz_mode(sbi))
105 goto out;
106
Ian Kentff9cd492008-07-23 21:30:24 -0700107 /*
108 * An empty directory in an autofs file system is always a
109 * mount point. The daemon must have failed to mount this
110 * during lookup so it doesn't exist. This can happen, for
111 * example, if user space returns an incorrect status for a
112 * mount request. Otherwise we're doing a readdir on the
113 * autofs file system so just let the libfs routines handle
114 * it.
115 */
116 spin_lock(&dcache_lock);
117 if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 spin_unlock(&dcache_lock);
Ian Kentff9cd492008-07-23 21:30:24 -0700119 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Ian Kentff9cd492008-07-23 21:30:24 -0700121 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Ian Kentf360ce32006-03-27 01:14:43 -0800123out:
Ian Kentff9cd492008-07-23 21:30:24 -0700124 return dcache_dir_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Ian Kent862b1102006-03-27 01:14:48 -0800127static int try_to_fill_dentry(struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Ian Kent862b1102006-03-27 01:14:48 -0800129 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800130 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700131 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* Block on any pending expiry here; invalidate the dentry
134 when expiration is done to trigger mount request with a new
135 dentry */
Ian Kent718c6042006-03-27 01:14:42 -0800136 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 DPRINTK("waiting for expire %p name=%.*s",
138 dentry, dentry->d_name.len, dentry->d_name.name);
139
140 status = autofs4_wait(sbi, dentry, NFY_NONE);
Ian Kent718c6042006-03-27 01:14:42 -0800141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 DPRINTK("expire done status=%d", status);
Ian Kent718c6042006-03-27 01:14:42 -0800143
Ian Kent1684b2b2005-06-21 17:16:41 -0700144 /*
145 * If the directory still exists the mount request must
146 * continue otherwise it can't be followed at the right
147 * time during the walk.
148 */
149 status = d_invalidate(dentry);
150 if (status != -EBUSY)
Ian Kentf50b6f82007-02-20 13:58:10 -0800151 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
154 DPRINTK("dentry=%p %.*s ino=%p",
155 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
156
Ian Kent718c6042006-03-27 01:14:42 -0800157 /*
158 * Wait for a pending mount, triggering one if there
159 * isn't one already
160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (dentry->d_inode == NULL) {
162 DPRINTK("waiting for mount name=%.*s",
163 dentry->d_name.len, dentry->d_name.name);
164
165 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 DPRINTK("mount done status=%d", status);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* Turn this into a real negative dentry? */
170 if (status == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 spin_lock(&dentry->d_lock);
172 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
173 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800174 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 } else if (status) {
176 /* Return a negative dentry, but leave it "pending" */
Ian Kent34ca9592006-03-27 01:14:54 -0800177 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 /* Trigger mount for path component or follow link */
Ian Kent26e81b32008-07-23 21:30:25 -0700180 } else if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
181 flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 current->link_count) {
183 DPRINTK("waiting for mount name=%.*s",
184 dentry->d_name.len, dentry->d_name.name);
185
186 spin_lock(&dentry->d_lock);
187 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
188 spin_unlock(&dentry->d_lock);
189 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
190
191 DPRINTK("mount done status=%d", status);
192
193 if (status) {
194 spin_lock(&dentry->d_lock);
195 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
196 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800197 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 }
200
Ian Kente0a7aae2006-03-27 01:14:47 -0800201 /* Initialize expiry counter after successful mount */
202 if (ino)
203 ino->last_used = jiffies;
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 spin_lock(&dentry->d_lock);
206 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
207 spin_unlock(&dentry->d_lock);
Jeff Moyer03379042008-05-01 04:35:08 -0700208
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700209 return 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800210}
211
212/* For autofs direct mounts the follow link triggers the mount */
213static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
214{
215 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kenta5370552006-05-15 09:43:51 -0700216 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent34ca9592006-03-27 01:14:54 -0800217 int oz_mode = autofs4_oz_mode(sbi);
218 unsigned int lookup_type;
219 int status;
220
221 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
222 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
223 nd->flags);
224
225 /* If it's our master or we shouldn't trigger a mount we're done */
Ian Kent6d5cb922008-07-23 21:30:15 -0700226 lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS);
Ian Kent26e81b32008-07-23 21:30:25 -0700227 if (oz_mode ||
228 !(lookup_type || dentry->d_flags & DCACHE_AUTOFS_PENDING))
Ian Kent34ca9592006-03-27 01:14:54 -0800229 goto done;
230
Ian Kenta5370552006-05-15 09:43:51 -0700231 /* If an expire request is pending wait for it. */
232 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Ian Kent871f9432006-03-27 01:14:58 -0800233 DPRINTK("waiting for active request %p name=%.*s",
234 dentry, dentry->d_name.len, dentry->d_name.name);
Ian Kent34ca9592006-03-27 01:14:54 -0800235
Ian Kent871f9432006-03-27 01:14:58 -0800236 status = autofs4_wait(sbi, dentry, NFY_NONE);
237
238 DPRINTK("request done status=%d", status);
Ian Kent34ca9592006-03-27 01:14:54 -0800239 }
240
Ian Kent871f9432006-03-27 01:14:58 -0800241 /*
242 * If the dentry contains directories then it is an
243 * autofs multi-mount with no root mount offset. So
244 * don't try to mount it again.
245 */
246 spin_lock(&dcache_lock);
Ian Kent26e81b32008-07-23 21:30:25 -0700247 if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
248 (!d_mountpoint(dentry) && __simple_empty(dentry))) {
Ian Kent871f9432006-03-27 01:14:58 -0800249 spin_unlock(&dcache_lock);
250
251 status = try_to_fill_dentry(dentry, 0);
252 if (status)
253 goto out_error;
254
255 /*
256 * The mount succeeded but if there is no root mount
257 * it must be an autofs multi-mount with no root offset
258 * so we don't need to follow the mount.
259 */
260 if (d_mountpoint(dentry)) {
Jan Blunck4ac91372008-02-14 19:34:32 -0800261 if (!autofs4_follow_mount(&nd->path.mnt,
262 &nd->path.dentry)) {
Ian Kent871f9432006-03-27 01:14:58 -0800263 status = -ENOENT;
264 goto out_error;
265 }
266 }
267
268 goto done;
269 }
270 spin_unlock(&dcache_lock);
271
Ian Kent34ca9592006-03-27 01:14:54 -0800272done:
273 return NULL;
274
275out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800276 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800277 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/*
281 * Revalidate is called on every cache lookup. Some of those
282 * cache lookups may actually happen while the dentry is not
283 * yet completely filled in, and revalidate has to delay such
284 * lookups..
285 */
Ian Kent718c6042006-03-27 01:14:42 -0800286static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Ian Kent718c6042006-03-27 01:14:42 -0800288 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
290 int oz_mode = autofs4_oz_mode(sbi);
291 int flags = nd ? nd->flags : 0;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700292 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 /* Pending dentry */
295 if (autofs4_ispending(dentry)) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700296 /* The daemon never causes a mount to trigger */
297 if (oz_mode)
298 return 1;
299
300 /*
301 * A zero status is success otherwise we have a
302 * negative error code.
303 */
304 status = try_to_fill_dentry(dentry, flags);
305 if (status == 0)
Ian Kentf50b6f82007-02-20 13:58:10 -0800306 return 1;
307
308 /*
309 * A status of EAGAIN here means that the dentry has gone
310 * away while waiting for an expire to complete. If we are
311 * racing with expire lookup will wait for it so this must
312 * be a revalidate and we need to send it to lookup.
313 */
314 if (status == -EAGAIN)
315 return 0;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700316
317 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319
320 /* Negative dentry.. invalidate if "old" */
321 if (dentry->d_inode == NULL)
Ian Kent2d753e62006-03-27 01:14:44 -0800322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* Check for a non-mountpoint directory with no contents */
325 spin_lock(&dcache_lock);
326 if (S_ISDIR(dentry->d_inode->i_mode) &&
327 !d_mountpoint(dentry) &&
Ian Kent90a59c72006-03-27 01:14:50 -0800328 __simple_empty(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 DPRINTK("dentry=%p %.*s, emptydir",
330 dentry, dentry->d_name.len, dentry->d_name.name);
331 spin_unlock(&dcache_lock);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700332 /* The daemon never causes a mount to trigger */
333 if (oz_mode)
334 return 1;
335
336 /*
337 * A zero status is success otherwise we have a
338 * negative error code.
339 */
340 status = try_to_fill_dentry(dentry, flags);
341 if (status == 0)
342 return 1;
343
344 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346 spin_unlock(&dcache_lock);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 1;
349}
350
Ian Kent34ca9592006-03-27 01:14:54 -0800351void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct autofs_info *inf;
354
355 DPRINTK("releasing %p", de);
356
357 inf = autofs4_dentry_ino(de);
358 de->d_fsdata = NULL;
359
360 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800361 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
362
Ian Kentf50b6f82007-02-20 13:58:10 -0800363 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700364 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700365 if (!list_empty(&inf->active))
366 list_del(&inf->active);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700367 if (!list_empty(&inf->expiring))
368 list_del(&inf->expiring);
369 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800370 }
371
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700372 inf->dentry = NULL;
373 inf->inode = NULL;
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 autofs4_free_ino(inf);
376 }
377}
378
379/* For dentries of directories in the root dir */
380static struct dentry_operations autofs4_root_dentry_operations = {
381 .d_revalidate = autofs4_revalidate,
382 .d_release = autofs4_dentry_release,
383};
384
385/* For other dentries */
386static struct dentry_operations autofs4_dentry_operations = {
387 .d_revalidate = autofs4_revalidate,
388 .d_release = autofs4_dentry_release,
389};
390
Ian Kent25767372008-07-23 21:30:12 -0700391static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
392{
393 unsigned int len = name->len;
394 unsigned int hash = name->hash;
395 const unsigned char *str = name->name;
396 struct list_head *p, *head;
397
398 spin_lock(&dcache_lock);
399 spin_lock(&sbi->lookup_lock);
400 head = &sbi->active_list;
401 list_for_each(p, head) {
402 struct autofs_info *ino;
403 struct dentry *dentry;
404 struct qstr *qstr;
405
406 ino = list_entry(p, struct autofs_info, active);
407 dentry = ino->dentry;
408
409 spin_lock(&dentry->d_lock);
410
411 /* Already gone? */
412 if (atomic_read(&dentry->d_count) == 0)
413 goto next;
414
415 qstr = &dentry->d_name;
416
417 if (dentry->d_name.hash != hash)
418 goto next;
419 if (dentry->d_parent != parent)
420 goto next;
421
422 if (qstr->len != len)
423 goto next;
424 if (memcmp(qstr->name, str, len))
425 goto next;
426
427 if (d_unhashed(dentry)) {
428 dget(dentry);
429 spin_unlock(&dentry->d_lock);
430 spin_unlock(&sbi->lookup_lock);
431 spin_unlock(&dcache_lock);
432 return dentry;
433 }
434next:
435 spin_unlock(&dentry->d_lock);
436 }
437 spin_unlock(&sbi->lookup_lock);
438 spin_unlock(&dcache_lock);
439
440 return NULL;
441}
442
Ian Kent5f6f4f22008-07-23 21:30:09 -0700443static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
Ian Kentf50b6f82007-02-20 13:58:10 -0800444{
445 unsigned int len = name->len;
446 unsigned int hash = name->hash;
447 const unsigned char *str = name->name;
448 struct list_head *p, *head;
449
450 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700451 spin_lock(&sbi->lookup_lock);
452 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800453 list_for_each(p, head) {
454 struct autofs_info *ino;
455 struct dentry *dentry;
456 struct qstr *qstr;
457
Ian Kent5f6f4f22008-07-23 21:30:09 -0700458 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800459 dentry = ino->dentry;
460
461 spin_lock(&dentry->d_lock);
462
463 /* Bad luck, we've already been dentry_iput */
464 if (!dentry->d_inode)
465 goto next;
466
467 qstr = &dentry->d_name;
468
469 if (dentry->d_name.hash != hash)
470 goto next;
471 if (dentry->d_parent != parent)
472 goto next;
473
474 if (qstr->len != len)
475 goto next;
476 if (memcmp(qstr->name, str, len))
477 goto next;
478
479 if (d_unhashed(dentry)) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800480 dget(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800481 spin_unlock(&dentry->d_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700482 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800483 spin_unlock(&dcache_lock);
484 return dentry;
485 }
486next:
487 spin_unlock(&dentry->d_lock);
488 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700489 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800490 spin_unlock(&dcache_lock);
491
492 return NULL;
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/* Lookups in the root directory */
496static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
497{
498 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700499 struct autofs_info *ino;
500 struct dentry *expiring, *unhashed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 int oz_mode;
502
503 DPRINTK("name = %.*s",
504 dentry->d_name.len, dentry->d_name.name);
505
Ian Kent718c6042006-03-27 01:14:42 -0800506 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800508 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700514 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Ian Kent5f6f4f22008-07-23 21:30:09 -0700516 expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
517 if (expiring) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800518 /*
519 * If we are racing with expire the request might not
520 * be quite complete but the directory has been removed
521 * so it must have been successful, so just wait for it.
522 */
Ian Kent25767372008-07-23 21:30:12 -0700523 ino = autofs4_dentry_ino(expiring);
Ian Kent1864f7b2007-08-22 14:01:54 -0700524 while (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800525 DPRINTK("wait for incomplete expire %p name=%.*s",
Ian Kent5f6f4f22008-07-23 21:30:09 -0700526 expiring, expiring->d_name.len,
527 expiring->d_name.name);
528 autofs4_wait(sbi, expiring, NFY_NONE);
Ian Kentf50b6f82007-02-20 13:58:10 -0800529 DPRINTK("request completed");
530 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700531 spin_lock(&sbi->lookup_lock);
532 if (!list_empty(&ino->expiring))
533 list_del_init(&ino->expiring);
534 spin_unlock(&sbi->lookup_lock);
535 dput(expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Ian Kent25767372008-07-23 21:30:12 -0700538 unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
539 if (unhashed)
540 dentry = unhashed;
541 else {
542 /*
543 * Mark the dentry incomplete but don't hash it. We do this
544 * to serialize our inode creation operations (symlink and
545 * mkdir) which prevents deadlock during the callback to
546 * the daemon. Subsequent user space lookups for the same
547 * dentry are placed on the wait queue while the daemon
548 * itself is allowed passage unresticted so the create
549 * operation itself can then hash the dentry. Finally,
550 * we check for the hashed dentry and return the newly
551 * hashed dentry.
552 */
553 dentry->d_op = &autofs4_root_dentry_operations;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700554
Ian Kent25767372008-07-23 21:30:12 -0700555 /*
556 * And we need to ensure that the same dentry is used for
557 * all following lookup calls until it is hashed so that
558 * the dentry flags are persistent throughout the request.
559 */
560 ino = autofs4_init_ino(NULL, sbi, 0555);
561 if (!ino)
562 return ERR_PTR(-ENOMEM);
563
564 dentry->d_fsdata = ino;
565 ino->dentry = dentry;
566
567 spin_lock(&sbi->lookup_lock);
568 list_add(&ino->active, &sbi->active_list);
569 spin_unlock(&sbi->lookup_lock);
570
571 d_instantiate(dentry, NULL);
572 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!oz_mode) {
575 spin_lock(&dentry->d_lock);
576 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
577 spin_unlock(&dentry->d_lock);
Ian Kentc432c252008-07-23 21:30:14 -0700578 if (dentry->d_op && dentry->d_op->d_revalidate) {
579 mutex_unlock(&dir->i_mutex);
580 (dentry->d_op->d_revalidate)(dentry, nd);
581 mutex_lock(&dir->i_mutex);
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
585 /*
586 * If we are still pending, check if we had to handle
587 * a signal. If so we can force a restart..
588 */
589 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
590 /* See if we were interrupted */
591 if (signal_pending(current)) {
592 sigset_t *sigset = &current->pending.signal;
593 if (sigismember (sigset, SIGKILL) ||
594 sigismember (sigset, SIGQUIT) ||
595 sigismember (sigset, SIGINT)) {
Ian Kent25767372008-07-23 21:30:12 -0700596 if (unhashed)
597 dput(unhashed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return ERR_PTR(-ERESTARTNOINTR);
599 }
600 }
Ian Kent25767372008-07-23 21:30:12 -0700601 if (!oz_mode) {
602 spin_lock(&dentry->d_lock);
603 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
604 spin_unlock(&dentry->d_lock);
605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 /*
609 * If this dentry is unhashed, then we shouldn't honour this
Ian Kentc9ffec42007-02-20 13:58:10 -0800610 * lookup. Returning ENOENT here doesn't do the right thing
611 * for all system calls, but it should be OK for the operations
612 * we permit from an autofs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Ian Kent1864f7b2007-08-22 14:01:54 -0700614 if (!oz_mode && d_unhashed(dentry)) {
Ian Kentc9ffec42007-02-20 13:58:10 -0800615 /*
616 * A user space application can (and has done in the past)
617 * remove and re-create this directory during the callback.
618 * This can leave us with an unhashed dentry, but a
619 * successful mount! So we need to perform another
620 * cached lookup in case the dentry now exists.
621 */
622 struct dentry *parent = dentry->d_parent;
623 struct dentry *new = d_lookup(parent, &dentry->d_name);
624 if (new != NULL)
625 dentry = new;
626 else
627 dentry = ERR_PTR(-ENOENT);
628
Ian Kent25767372008-07-23 21:30:12 -0700629 if (unhashed)
630 dput(unhashed);
631
Ian Kentc9ffec42007-02-20 13:58:10 -0800632 return dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800633 }
634
Ian Kent25767372008-07-23 21:30:12 -0700635 if (unhashed)
636 return unhashed;
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return NULL;
639}
640
641static int autofs4_dir_symlink(struct inode *dir,
642 struct dentry *dentry,
643 const char *symname)
644{
645 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
646 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800647 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct inode *inode;
649 char *cp;
650
651 DPRINTK("%s <- %.*s", symname,
652 dentry->d_name.len, dentry->d_name.name);
653
654 if (!autofs4_oz_mode(sbi))
655 return -EACCES;
656
657 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700658 if (!ino)
659 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Ian Kent25767372008-07-23 21:30:12 -0700661 spin_lock(&sbi->lookup_lock);
662 if (!list_empty(&ino->active))
663 list_del_init(&ino->active);
664 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Ian Kentef581a72008-07-23 21:30:13 -0700666 ino->size = strlen(symname);
Ian Kent25767372008-07-23 21:30:12 -0700667 cp = kmalloc(ino->size + 1, GFP_KERNEL);
668 if (!cp) {
669 if (!dentry->d_fsdata)
670 kfree(ino);
671 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673
674 strcpy(cp, symname);
675
676 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700677 if (!inode) {
678 kfree(cp);
679 if (!dentry->d_fsdata)
680 kfree(ino);
681 return -ENOMEM;
682 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700683 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if (dir == dir->i_sb->s_root->d_inode)
686 dentry->d_op = &autofs4_root_dentry_operations;
687 else
688 dentry->d_op = &autofs4_dentry_operations;
689
690 dentry->d_fsdata = ino;
691 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800692 atomic_inc(&ino->count);
693 p_ino = autofs4_dentry_ino(dentry->d_parent);
694 if (p_ino && dentry->d_parent != dentry)
695 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ino->inode = inode;
697
Ian Kent25767372008-07-23 21:30:12 -0700698 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 dir->i_mtime = CURRENT_TIME;
700
701 return 0;
702}
703
704/*
705 * NOTE!
706 *
707 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
708 * that the file no longer exists. However, doing that means that the
709 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800710 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700711 * We simply d_drop it and add it to a expiring list in the super block,
712 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 *
714 * If a process is blocked on the dentry waiting for the expire to finish,
715 * it will invalidate the dentry and try to mount with a new one.
716 *
717 * Also see autofs4_dir_rmdir()..
718 */
719static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
720{
721 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
722 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800723 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700726 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -EACCES;
728
Ian Kent1aff3c82006-03-27 01:14:46 -0800729 if (atomic_dec_and_test(&ino->count)) {
730 p_ino = autofs4_dentry_ino(dentry->d_parent);
731 if (p_ino && dentry->d_parent != dentry)
732 atomic_dec(&p_ino->count);
733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 dput(ino->dentry);
735
736 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700737 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 dir->i_mtime = CURRENT_TIME;
740
Ian Kentf50b6f82007-02-20 13:58:10 -0800741 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700742 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700743 if (list_empty(&ino->expiring))
744 list_add(&ino->expiring, &sbi->expiring_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700745 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800746 spin_lock(&dentry->d_lock);
747 __d_drop(dentry);
748 spin_unlock(&dentry->d_lock);
749 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 return 0;
752}
753
754static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
755{
756 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
757 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800758 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Ian Kentf50b6f82007-02-20 13:58:10 -0800760 DPRINTK("dentry %p, removing %.*s",
761 dentry, dentry->d_name.len, dentry->d_name.name);
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (!autofs4_oz_mode(sbi))
764 return -EACCES;
765
766 spin_lock(&dcache_lock);
767 if (!list_empty(&dentry->d_subdirs)) {
768 spin_unlock(&dcache_lock);
769 return -ENOTEMPTY;
770 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700771 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700772 if (list_empty(&ino->expiring))
773 list_add(&ino->expiring, &sbi->expiring_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700774 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 spin_lock(&dentry->d_lock);
776 __d_drop(dentry);
777 spin_unlock(&dentry->d_lock);
778 spin_unlock(&dcache_lock);
779
Ian Kent1aff3c82006-03-27 01:14:46 -0800780 if (atomic_dec_and_test(&ino->count)) {
781 p_ino = autofs4_dentry_ino(dentry->d_parent);
782 if (p_ino && dentry->d_parent != dentry)
783 atomic_dec(&p_ino->count);
784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700787 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700790 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 return 0;
793}
794
795static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
796{
797 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
798 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800799 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct inode *inode;
801
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700802 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return -EACCES;
804
805 DPRINTK("dentry %p, creating %.*s",
806 dentry, dentry->d_name.len, dentry->d_name.name);
807
808 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700809 if (!ino)
810 return -ENOMEM;
811
812 spin_lock(&sbi->lookup_lock);
813 if (!list_empty(&ino->active))
814 list_del_init(&ino->active);
815 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700818 if (!inode) {
819 if (!dentry->d_fsdata)
820 kfree(ino);
821 return -ENOMEM;
822 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700823 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 if (dir == dir->i_sb->s_root->d_inode)
826 dentry->d_op = &autofs4_root_dentry_operations;
827 else
828 dentry->d_op = &autofs4_dentry_operations;
829
830 dentry->d_fsdata = ino;
831 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800832 atomic_inc(&ino->count);
833 p_ino = autofs4_dentry_ino(dentry->d_parent);
834 if (p_ino && dentry->d_parent != dentry)
835 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700837 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 dir->i_mtime = CURRENT_TIME;
839
840 return 0;
841}
842
843/* Get/set timeout ioctl() operation */
844static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
845 unsigned long __user *p)
846{
847 int rv;
848 unsigned long ntimeout;
849
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700850 if ((rv = get_user(ntimeout, p)) ||
851 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return rv;
853
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700854 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 sbi->exp_timeout = 0;
856 else
857 sbi->exp_timeout = ntimeout * HZ;
858
859 return 0;
860}
861
862/* Return protocol version */
863static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
864{
865 return put_user(sbi->version, p);
866}
867
868/* Return protocol sub version */
869static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
870{
871 return put_user(sbi->sub_version, p);
872}
873
874/*
875 * Tells the daemon whether we need to reghost or not. Also, clears
876 * the reghost_needed flag.
877 */
878static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
879{
880 int status;
881
882 DPRINTK("returning %d", sbi->needs_reghost);
883
884 status = put_user(sbi->needs_reghost, p);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700885 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return status;
887
888 sbi->needs_reghost = 0;
889 return 0;
890}
891
892/*
893 * Enable / Disable reghosting ioctl() operation
894 */
895static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
896{
897 int status;
898 int val;
899
900 status = get_user(val, p);
901
902 DPRINTK("reghost = %d", val);
903
904 if (status)
905 return status;
906
907 /* turn on/off reghosting, with the val */
908 sbi->reghost_enabled = val;
909 return 0;
910}
911
912/*
913* Tells the daemon whether it can umount the autofs mount.
914*/
915static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
916{
917 int status = 0;
918
Ian Kente3474a82006-03-27 01:14:51 -0800919 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 status = 1;
921
922 DPRINTK("returning %d", status);
923
924 status = put_user(status, p);
925
926 return status;
927}
928
929/* Identify autofs4_dentries - this is so we can tell if there's
930 an extra dentry refcount or not. We only hold a refcount on the
931 dentry if its non-negative (ie, d_inode != NULL)
932*/
933int is_autofs4_dentry(struct dentry *dentry)
934{
935 return dentry && dentry->d_inode &&
936 (dentry->d_op == &autofs4_root_dentry_operations ||
937 dentry->d_op == &autofs4_dentry_operations) &&
938 dentry->d_fsdata != NULL;
939}
940
941/*
942 * ioctl()'s on the root directory is the chief method for the daemon to
943 * generate kernel reactions
944 */
945static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
946 unsigned int cmd, unsigned long arg)
947{
948 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
949 void __user *p = (void __user *)arg;
950
951 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700952 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700954 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
955 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return -ENOTTY;
957
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700958 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return -EPERM;
960
961 switch(cmd) {
962 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
963 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
964 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
965 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
966 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
967 autofs4_catatonic_mode(sbi);
968 return 0;
969 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
970 return autofs4_get_protover(sbi, p);
971 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
972 return autofs4_get_protosubver(sbi, p);
973 case AUTOFS_IOC_SETTIMEOUT:
974 return autofs4_get_set_timeout(sbi, p);
975
976 case AUTOFS_IOC_TOGGLEREGHOST:
977 return autofs4_toggle_reghost(sbi, p);
978 case AUTOFS_IOC_ASKREGHOST:
979 return autofs4_ask_reghost(sbi, p);
980
981 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800982 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
984 /* return a single thing to expire */
985 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800986 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /* same as above, but can send multiple expires through pipe */
988 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800989 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 default:
992 return -ENOSYS;
993 }
994}