blob: ae22bde0bbd7e1d5339f6091e63f0ad004892b34 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 DPRINTK("dentry=%p %.*s ino=%p",
134 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
135
Ian Kent718c6042006-03-27 01:14:42 -0800136 /*
137 * Wait for a pending mount, triggering one if there
138 * isn't one already
139 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (dentry->d_inode == NULL) {
141 DPRINTK("waiting for mount name=%.*s",
142 dentry->d_name.len, dentry->d_name.name);
143
144 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 DPRINTK("mount done status=%d", status);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 /* Turn this into a real negative dentry? */
149 if (status == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 spin_lock(&dentry->d_lock);
151 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
152 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800153 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 } else if (status) {
155 /* Return a negative dentry, but leave it "pending" */
Ian Kent34ca9592006-03-27 01:14:54 -0800156 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158 /* Trigger mount for path component or follow link */
Ian Kent26e81b32008-07-23 21:30:25 -0700159 } else if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
160 flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 current->link_count) {
162 DPRINTK("waiting for mount name=%.*s",
163 dentry->d_name.len, dentry->d_name.name);
164
165 spin_lock(&dentry->d_lock);
166 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
167 spin_unlock(&dentry->d_lock);
168 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
169
170 DPRINTK("mount done status=%d", status);
171
172 if (status) {
173 spin_lock(&dentry->d_lock);
174 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
175 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800176 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 }
179
Ian Kente0a7aae2006-03-27 01:14:47 -0800180 /* Initialize expiry counter after successful mount */
181 if (ino)
182 ino->last_used = jiffies;
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 spin_lock(&dentry->d_lock);
185 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
186 spin_unlock(&dentry->d_lock);
Jeff Moyer03379042008-05-01 04:35:08 -0700187
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700188 return 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800189}
190
191/* For autofs direct mounts the follow link triggers the mount */
192static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
193{
194 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kenta5370552006-05-15 09:43:51 -0700195 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent34ca9592006-03-27 01:14:54 -0800196 int oz_mode = autofs4_oz_mode(sbi);
197 unsigned int lookup_type;
198 int status;
199
200 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
201 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
202 nd->flags);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700203 /*
204 * For an expire of a covered direct or offset mount we need
205 * to beeak out of follow_down() at the autofs mount trigger
206 * (d_mounted--), so we can see the expiring flag, and manage
207 * the blocking and following here until the expire is completed.
208 */
209 if (oz_mode) {
210 spin_lock(&sbi->fs_lock);
211 if (ino->flags & AUTOFS_INF_EXPIRING) {
212 spin_unlock(&sbi->fs_lock);
213 /* Follow down to our covering mount. */
214 if (!follow_down(&nd->path.mnt, &nd->path.dentry))
215 goto done;
Ian Kentec6e8c72008-07-23 21:30:28 -0700216 goto follow;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700217 }
218 spin_unlock(&sbi->fs_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800219 goto done;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700220 }
Ian Kent34ca9592006-03-27 01:14:54 -0800221
Ian Kent6e60a9a2008-07-23 21:30:27 -0700222 /* If an expire request is pending everyone must wait. */
Ian Kent06a35982008-07-23 21:30:28 -0700223 autofs4_expire_wait(dentry);
Ian Kent97e74492008-07-23 21:30:26 -0700224
Ian Kent6e60a9a2008-07-23 21:30:27 -0700225 /* We trigger a mount for almost all flags */
226 lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS);
227 if (!(lookup_type || dentry->d_flags & DCACHE_AUTOFS_PENDING))
Ian Kentec6e8c72008-07-23 21:30:28 -0700228 goto follow;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700229
Ian Kent871f9432006-03-27 01:14:58 -0800230 /*
Ian Kent6e60a9a2008-07-23 21:30:27 -0700231 * If the dentry contains directories then it is an autofs
232 * multi-mount with no root mount offset. So don't try to
233 * mount it again.
Ian Kent871f9432006-03-27 01:14:58 -0800234 */
235 spin_lock(&dcache_lock);
Ian Kent26e81b32008-07-23 21:30:25 -0700236 if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
237 (!d_mountpoint(dentry) && __simple_empty(dentry))) {
Ian Kent871f9432006-03-27 01:14:58 -0800238 spin_unlock(&dcache_lock);
239
240 status = try_to_fill_dentry(dentry, 0);
241 if (status)
242 goto out_error;
243
Ian Kent6e60a9a2008-07-23 21:30:27 -0700244 goto follow;
Ian Kent871f9432006-03-27 01:14:58 -0800245 }
246 spin_unlock(&dcache_lock);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700247follow:
248 /*
249 * If there is no root mount it must be an autofs
250 * multi-mount with no root offset so we don't need
251 * to follow it.
252 */
253 if (d_mountpoint(dentry)) {
254 if (!autofs4_follow_mount(&nd->path.mnt,
255 &nd->path.dentry)) {
256 status = -ENOENT;
257 goto out_error;
258 }
259 }
Ian Kent871f9432006-03-27 01:14:58 -0800260
Ian Kent34ca9592006-03-27 01:14:54 -0800261done:
262 return NULL;
263
264out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800265 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800266 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
269/*
270 * Revalidate is called on every cache lookup. Some of those
271 * cache lookups may actually happen while the dentry is not
272 * yet completely filled in, and revalidate has to delay such
273 * lookups..
274 */
Ian Kent718c6042006-03-27 01:14:42 -0800275static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Ian Kent718c6042006-03-27 01:14:42 -0800277 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
279 int oz_mode = autofs4_oz_mode(sbi);
280 int flags = nd ? nd->flags : 0;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700281 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* Pending dentry */
Ian Kent97e74492008-07-23 21:30:26 -0700284 spin_lock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (autofs4_ispending(dentry)) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700286 /* The daemon never causes a mount to trigger */
Ian Kent97e74492008-07-23 21:30:26 -0700287 spin_unlock(&sbi->fs_lock);
288
Ian Kentbcdc5e02006-09-27 01:50:44 -0700289 if (oz_mode)
290 return 1;
291
292 /*
Ian Kent06a35982008-07-23 21:30:28 -0700293 * If the directory has gone away due to an expire
294 * we have been called as ->d_revalidate() and so
295 * we need to return false and proceed to ->lookup().
296 */
297 if (autofs4_expire_wait(dentry) == -EAGAIN)
298 return 0;
299
300 /*
Ian Kentbcdc5e02006-09-27 01:50:44 -0700301 * 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
Ian Kentbcdc5e02006-09-27 01:50:44 -0700308 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Ian Kent97e74492008-07-23 21:30:26 -0700310 spin_unlock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* Negative dentry.. invalidate if "old" */
313 if (dentry->d_inode == NULL)
Ian Kent2d753e62006-03-27 01:14:44 -0800314 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 /* Check for a non-mountpoint directory with no contents */
317 spin_lock(&dcache_lock);
318 if (S_ISDIR(dentry->d_inode->i_mode) &&
319 !d_mountpoint(dentry) &&
Ian Kent90a59c72006-03-27 01:14:50 -0800320 __simple_empty(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 DPRINTK("dentry=%p %.*s, emptydir",
322 dentry, dentry->d_name.len, dentry->d_name.name);
323 spin_unlock(&dcache_lock);
Ian Kent97e74492008-07-23 21:30:26 -0700324
Ian Kentbcdc5e02006-09-27 01:50:44 -0700325 /* The daemon never causes a mount to trigger */
326 if (oz_mode)
327 return 1;
328
329 /*
330 * A zero status is success otherwise we have a
331 * negative error code.
332 */
333 status = try_to_fill_dentry(dentry, flags);
334 if (status == 0)
335 return 1;
336
337 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 spin_unlock(&dcache_lock);
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return 1;
342}
343
Ian Kent34ca9592006-03-27 01:14:54 -0800344void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct autofs_info *inf;
347
348 DPRINTK("releasing %p", de);
349
350 inf = autofs4_dentry_ino(de);
351 de->d_fsdata = NULL;
352
353 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800354 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
355
Ian Kentf50b6f82007-02-20 13:58:10 -0800356 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700357 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700358 if (!list_empty(&inf->active))
359 list_del(&inf->active);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700360 if (!list_empty(&inf->expiring))
361 list_del(&inf->expiring);
362 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800363 }
364
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700365 inf->dentry = NULL;
366 inf->inode = NULL;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 autofs4_free_ino(inf);
369 }
370}
371
372/* For dentries of directories in the root dir */
373static struct dentry_operations autofs4_root_dentry_operations = {
374 .d_revalidate = autofs4_revalidate,
375 .d_release = autofs4_dentry_release,
376};
377
378/* For other dentries */
379static struct dentry_operations autofs4_dentry_operations = {
380 .d_revalidate = autofs4_revalidate,
381 .d_release = autofs4_dentry_release,
382};
383
Ian Kent25767372008-07-23 21:30:12 -0700384static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
385{
386 unsigned int len = name->len;
387 unsigned int hash = name->hash;
388 const unsigned char *str = name->name;
389 struct list_head *p, *head;
390
391 spin_lock(&dcache_lock);
392 spin_lock(&sbi->lookup_lock);
393 head = &sbi->active_list;
394 list_for_each(p, head) {
395 struct autofs_info *ino;
396 struct dentry *dentry;
397 struct qstr *qstr;
398
399 ino = list_entry(p, struct autofs_info, active);
400 dentry = ino->dentry;
401
402 spin_lock(&dentry->d_lock);
403
404 /* Already gone? */
405 if (atomic_read(&dentry->d_count) == 0)
406 goto next;
407
408 qstr = &dentry->d_name;
409
410 if (dentry->d_name.hash != hash)
411 goto next;
412 if (dentry->d_parent != parent)
413 goto next;
414
415 if (qstr->len != len)
416 goto next;
417 if (memcmp(qstr->name, str, len))
418 goto next;
419
420 if (d_unhashed(dentry)) {
421 dget(dentry);
422 spin_unlock(&dentry->d_lock);
423 spin_unlock(&sbi->lookup_lock);
424 spin_unlock(&dcache_lock);
425 return dentry;
426 }
427next:
428 spin_unlock(&dentry->d_lock);
429 }
430 spin_unlock(&sbi->lookup_lock);
431 spin_unlock(&dcache_lock);
432
433 return NULL;
434}
435
Ian Kent5f6f4f22008-07-23 21:30:09 -0700436static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
Ian Kentf50b6f82007-02-20 13:58:10 -0800437{
438 unsigned int len = name->len;
439 unsigned int hash = name->hash;
440 const unsigned char *str = name->name;
441 struct list_head *p, *head;
442
443 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700444 spin_lock(&sbi->lookup_lock);
445 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800446 list_for_each(p, head) {
447 struct autofs_info *ino;
448 struct dentry *dentry;
449 struct qstr *qstr;
450
Ian Kent5f6f4f22008-07-23 21:30:09 -0700451 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800452 dentry = ino->dentry;
453
454 spin_lock(&dentry->d_lock);
455
456 /* Bad luck, we've already been dentry_iput */
457 if (!dentry->d_inode)
458 goto next;
459
460 qstr = &dentry->d_name;
461
462 if (dentry->d_name.hash != hash)
463 goto next;
464 if (dentry->d_parent != parent)
465 goto next;
466
467 if (qstr->len != len)
468 goto next;
469 if (memcmp(qstr->name, str, len))
470 goto next;
471
472 if (d_unhashed(dentry)) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800473 dget(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800474 spin_unlock(&dentry->d_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700475 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800476 spin_unlock(&dcache_lock);
477 return dentry;
478 }
479next:
480 spin_unlock(&dentry->d_lock);
481 }
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
485 return NULL;
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/* Lookups in the root directory */
489static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
490{
491 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700492 struct autofs_info *ino;
493 struct dentry *expiring, *unhashed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 int oz_mode;
495
496 DPRINTK("name = %.*s",
497 dentry->d_name.len, dentry->d_name.name);
498
Ian Kent718c6042006-03-27 01:14:42 -0800499 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800501 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700507 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Ian Kent5f6f4f22008-07-23 21:30:09 -0700509 expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
510 if (expiring) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800511 /*
512 * If we are racing with expire the request might not
513 * be quite complete but the directory has been removed
514 * so it must have been successful, so just wait for it.
515 */
Ian Kent25767372008-07-23 21:30:12 -0700516 ino = autofs4_dentry_ino(expiring);
Ian Kent06a35982008-07-23 21:30:28 -0700517 autofs4_expire_wait(expiring);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700518 spin_lock(&sbi->lookup_lock);
519 if (!list_empty(&ino->expiring))
520 list_del_init(&ino->expiring);
521 spin_unlock(&sbi->lookup_lock);
522 dput(expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Ian Kent25767372008-07-23 21:30:12 -0700525 unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
526 if (unhashed)
527 dentry = unhashed;
528 else {
529 /*
530 * Mark the dentry incomplete but don't hash it. We do this
531 * to serialize our inode creation operations (symlink and
532 * mkdir) which prevents deadlock during the callback to
533 * the daemon. Subsequent user space lookups for the same
534 * dentry are placed on the wait queue while the daemon
535 * itself is allowed passage unresticted so the create
536 * operation itself can then hash the dentry. Finally,
537 * we check for the hashed dentry and return the newly
538 * hashed dentry.
539 */
540 dentry->d_op = &autofs4_root_dentry_operations;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700541
Ian Kent25767372008-07-23 21:30:12 -0700542 /*
543 * And we need to ensure that the same dentry is used for
544 * all following lookup calls until it is hashed so that
545 * the dentry flags are persistent throughout the request.
546 */
547 ino = autofs4_init_ino(NULL, sbi, 0555);
548 if (!ino)
549 return ERR_PTR(-ENOMEM);
550
551 dentry->d_fsdata = ino;
552 ino->dentry = dentry;
553
554 spin_lock(&sbi->lookup_lock);
555 list_add(&ino->active, &sbi->active_list);
556 spin_unlock(&sbi->lookup_lock);
557
558 d_instantiate(dentry, NULL);
559 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (!oz_mode) {
562 spin_lock(&dentry->d_lock);
563 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
564 spin_unlock(&dentry->d_lock);
Ian Kentc432c252008-07-23 21:30:14 -0700565 if (dentry->d_op && dentry->d_op->d_revalidate) {
566 mutex_unlock(&dir->i_mutex);
567 (dentry->d_op->d_revalidate)(dentry, nd);
568 mutex_lock(&dir->i_mutex);
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571
572 /*
573 * If we are still pending, check if we had to handle
574 * a signal. If so we can force a restart..
575 */
576 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
577 /* See if we were interrupted */
578 if (signal_pending(current)) {
579 sigset_t *sigset = &current->pending.signal;
580 if (sigismember (sigset, SIGKILL) ||
581 sigismember (sigset, SIGQUIT) ||
582 sigismember (sigset, SIGINT)) {
Ian Kent25767372008-07-23 21:30:12 -0700583 if (unhashed)
584 dput(unhashed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return ERR_PTR(-ERESTARTNOINTR);
586 }
587 }
Ian Kent25767372008-07-23 21:30:12 -0700588 if (!oz_mode) {
589 spin_lock(&dentry->d_lock);
590 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
591 spin_unlock(&dentry->d_lock);
592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
595 /*
596 * If this dentry is unhashed, then we shouldn't honour this
Ian Kentc9ffec42007-02-20 13:58:10 -0800597 * lookup. Returning ENOENT here doesn't do the right thing
598 * for all system calls, but it should be OK for the operations
599 * we permit from an autofs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Ian Kent1864f7b2007-08-22 14:01:54 -0700601 if (!oz_mode && d_unhashed(dentry)) {
Ian Kentc9ffec42007-02-20 13:58:10 -0800602 /*
603 * A user space application can (and has done in the past)
604 * remove and re-create this directory during the callback.
605 * This can leave us with an unhashed dentry, but a
606 * successful mount! So we need to perform another
607 * cached lookup in case the dentry now exists.
608 */
609 struct dentry *parent = dentry->d_parent;
610 struct dentry *new = d_lookup(parent, &dentry->d_name);
611 if (new != NULL)
612 dentry = new;
613 else
614 dentry = ERR_PTR(-ENOENT);
615
Ian Kent25767372008-07-23 21:30:12 -0700616 if (unhashed)
617 dput(unhashed);
618
Ian Kentc9ffec42007-02-20 13:58:10 -0800619 return dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800620 }
621
Ian Kent25767372008-07-23 21:30:12 -0700622 if (unhashed)
623 return unhashed;
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return NULL;
626}
627
628static int autofs4_dir_symlink(struct inode *dir,
629 struct dentry *dentry,
630 const char *symname)
631{
632 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
633 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800634 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct inode *inode;
636 char *cp;
637
638 DPRINTK("%s <- %.*s", symname,
639 dentry->d_name.len, dentry->d_name.name);
640
641 if (!autofs4_oz_mode(sbi))
642 return -EACCES;
643
644 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700645 if (!ino)
646 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Ian Kent25767372008-07-23 21:30:12 -0700648 spin_lock(&sbi->lookup_lock);
649 if (!list_empty(&ino->active))
650 list_del_init(&ino->active);
651 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Ian Kentef581a72008-07-23 21:30:13 -0700653 ino->size = strlen(symname);
Ian Kent25767372008-07-23 21:30:12 -0700654 cp = kmalloc(ino->size + 1, GFP_KERNEL);
655 if (!cp) {
656 if (!dentry->d_fsdata)
657 kfree(ino);
658 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
661 strcpy(cp, symname);
662
663 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700664 if (!inode) {
665 kfree(cp);
666 if (!dentry->d_fsdata)
667 kfree(ino);
668 return -ENOMEM;
669 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700670 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 if (dir == dir->i_sb->s_root->d_inode)
673 dentry->d_op = &autofs4_root_dentry_operations;
674 else
675 dentry->d_op = &autofs4_dentry_operations;
676
677 dentry->d_fsdata = ino;
678 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800679 atomic_inc(&ino->count);
680 p_ino = autofs4_dentry_ino(dentry->d_parent);
681 if (p_ino && dentry->d_parent != dentry)
682 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 ino->inode = inode;
684
Ian Kent25767372008-07-23 21:30:12 -0700685 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 dir->i_mtime = CURRENT_TIME;
687
688 return 0;
689}
690
691/*
692 * NOTE!
693 *
694 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
695 * that the file no longer exists. However, doing that means that the
696 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800697 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700698 * We simply d_drop it and add it to a expiring list in the super block,
699 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 *
701 * If a process is blocked on the dentry waiting for the expire to finish,
702 * it will invalidate the dentry and try to mount with a new one.
703 *
704 * Also see autofs4_dir_rmdir()..
705 */
706static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
707{
708 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
709 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800710 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700713 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return -EACCES;
715
Ian Kent1aff3c82006-03-27 01:14:46 -0800716 if (atomic_dec_and_test(&ino->count)) {
717 p_ino = autofs4_dentry_ino(dentry->d_parent);
718 if (p_ino && dentry->d_parent != dentry)
719 atomic_dec(&p_ino->count);
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 dput(ino->dentry);
722
723 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700724 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 dir->i_mtime = CURRENT_TIME;
727
Ian Kentf50b6f82007-02-20 13:58:10 -0800728 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700729 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700730 if (list_empty(&ino->expiring))
731 list_add(&ino->expiring, &sbi->expiring_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700732 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800733 spin_lock(&dentry->d_lock);
734 __d_drop(dentry);
735 spin_unlock(&dentry->d_lock);
736 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 return 0;
739}
740
741static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
742{
743 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
744 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800745 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Ian Kentf50b6f82007-02-20 13:58:10 -0800747 DPRINTK("dentry %p, removing %.*s",
748 dentry, dentry->d_name.len, dentry->d_name.name);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (!autofs4_oz_mode(sbi))
751 return -EACCES;
752
753 spin_lock(&dcache_lock);
754 if (!list_empty(&dentry->d_subdirs)) {
755 spin_unlock(&dcache_lock);
756 return -ENOTEMPTY;
757 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700758 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700759 if (list_empty(&ino->expiring))
760 list_add(&ino->expiring, &sbi->expiring_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700761 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 spin_lock(&dentry->d_lock);
763 __d_drop(dentry);
764 spin_unlock(&dentry->d_lock);
765 spin_unlock(&dcache_lock);
766
Ian Kent1aff3c82006-03-27 01:14:46 -0800767 if (atomic_dec_and_test(&ino->count)) {
768 p_ino = autofs4_dentry_ino(dentry->d_parent);
769 if (p_ino && dentry->d_parent != dentry)
770 atomic_dec(&p_ino->count);
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700774 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700777 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 return 0;
780}
781
782static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
783{
784 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
785 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800786 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct inode *inode;
788
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700789 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return -EACCES;
791
792 DPRINTK("dentry %p, creating %.*s",
793 dentry, dentry->d_name.len, dentry->d_name.name);
794
795 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700796 if (!ino)
797 return -ENOMEM;
798
799 spin_lock(&sbi->lookup_lock);
800 if (!list_empty(&ino->active))
801 list_del_init(&ino->active);
802 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700805 if (!inode) {
806 if (!dentry->d_fsdata)
807 kfree(ino);
808 return -ENOMEM;
809 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700810 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (dir == dir->i_sb->s_root->d_inode)
813 dentry->d_op = &autofs4_root_dentry_operations;
814 else
815 dentry->d_op = &autofs4_dentry_operations;
816
817 dentry->d_fsdata = ino;
818 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800819 atomic_inc(&ino->count);
820 p_ino = autofs4_dentry_ino(dentry->d_parent);
821 if (p_ino && dentry->d_parent != dentry)
822 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700824 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 dir->i_mtime = CURRENT_TIME;
826
827 return 0;
828}
829
830/* Get/set timeout ioctl() operation */
831static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
832 unsigned long __user *p)
833{
834 int rv;
835 unsigned long ntimeout;
836
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700837 if ((rv = get_user(ntimeout, p)) ||
838 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return rv;
840
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700841 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 sbi->exp_timeout = 0;
843 else
844 sbi->exp_timeout = ntimeout * HZ;
845
846 return 0;
847}
848
849/* Return protocol version */
850static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
851{
852 return put_user(sbi->version, p);
853}
854
855/* Return protocol sub version */
856static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
857{
858 return put_user(sbi->sub_version, p);
859}
860
861/*
862 * Tells the daemon whether we need to reghost or not. Also, clears
863 * the reghost_needed flag.
864 */
865static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
866{
867 int status;
868
869 DPRINTK("returning %d", sbi->needs_reghost);
870
871 status = put_user(sbi->needs_reghost, p);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700872 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return status;
874
875 sbi->needs_reghost = 0;
876 return 0;
877}
878
879/*
880 * Enable / Disable reghosting ioctl() operation
881 */
882static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
883{
884 int status;
885 int val;
886
887 status = get_user(val, p);
888
889 DPRINTK("reghost = %d", val);
890
891 if (status)
892 return status;
893
894 /* turn on/off reghosting, with the val */
895 sbi->reghost_enabled = val;
896 return 0;
897}
898
899/*
900* Tells the daemon whether it can umount the autofs mount.
901*/
902static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
903{
904 int status = 0;
905
Ian Kente3474a82006-03-27 01:14:51 -0800906 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 status = 1;
908
909 DPRINTK("returning %d", status);
910
911 status = put_user(status, p);
912
913 return status;
914}
915
916/* Identify autofs4_dentries - this is so we can tell if there's
917 an extra dentry refcount or not. We only hold a refcount on the
918 dentry if its non-negative (ie, d_inode != NULL)
919*/
920int is_autofs4_dentry(struct dentry *dentry)
921{
922 return dentry && dentry->d_inode &&
923 (dentry->d_op == &autofs4_root_dentry_operations ||
924 dentry->d_op == &autofs4_dentry_operations) &&
925 dentry->d_fsdata != NULL;
926}
927
928/*
929 * ioctl()'s on the root directory is the chief method for the daemon to
930 * generate kernel reactions
931 */
932static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
933 unsigned int cmd, unsigned long arg)
934{
935 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
936 void __user *p = (void __user *)arg;
937
938 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700939 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700941 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
942 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return -ENOTTY;
944
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700945 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return -EPERM;
947
948 switch(cmd) {
949 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
950 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
951 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
952 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
953 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
954 autofs4_catatonic_mode(sbi);
955 return 0;
956 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
957 return autofs4_get_protover(sbi, p);
958 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
959 return autofs4_get_protosubver(sbi, p);
960 case AUTOFS_IOC_SETTIMEOUT:
961 return autofs4_get_set_timeout(sbi, p);
962
963 case AUTOFS_IOC_TOGGLEREGHOST:
964 return autofs4_toggle_reghost(sbi, p);
965 case AUTOFS_IOC_ASKREGHOST:
966 return autofs4_ask_reghost(sbi, p);
967
968 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800969 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
971 /* return a single thing to expire */
972 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800973 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /* same as above, but can send multiple expires through pipe */
975 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800976 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 default:
979 return -ENOSYS;
980 }
981}