blob: e8e5e63ac9503b6f42d20681a26fc6565e43641b [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/param.h>
20#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static 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,
Ian Kentaa55ddf2008-07-23 21:30:29 -070039 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040040 .llseek = dcache_dir_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 .ioctl = autofs4_root_ioctl,
42};
43
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080044const struct file_operations autofs4_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .open = autofs4_dir_open,
Ian Kentff9cd492008-07-23 21:30:24 -070046 .release = dcache_dir_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 .read = generic_read_dir,
Ian Kentff9cd492008-07-23 21:30:24 -070048 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040049 .llseek = dcache_dir_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Arjan van de Ven754661f2007-02-12 00:55:38 -080052const struct inode_operations autofs4_indirect_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .lookup = autofs4_lookup,
54 .unlink = autofs4_dir_unlink,
55 .symlink = autofs4_dir_symlink,
56 .mkdir = autofs4_dir_mkdir,
57 .rmdir = autofs4_dir_rmdir,
58};
59
Arjan van de Ven754661f2007-02-12 00:55:38 -080060const struct inode_operations autofs4_direct_root_inode_operations = {
Ian Kent34ca9592006-03-27 01:14:54 -080061 .lookup = autofs4_lookup,
Ian Kent871f9432006-03-27 01:14:58 -080062 .unlink = autofs4_dir_unlink,
63 .mkdir = autofs4_dir_mkdir,
64 .rmdir = autofs4_dir_rmdir,
Ian Kent34ca9592006-03-27 01:14:54 -080065 .follow_link = autofs4_follow_link,
66};
67
Arjan van de Ven754661f2007-02-12 00:55:38 -080068const struct inode_operations autofs4_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .lookup = autofs4_lookup,
70 .unlink = autofs4_dir_unlink,
71 .symlink = autofs4_dir_symlink,
72 .mkdir = autofs4_dir_mkdir,
73 .rmdir = autofs4_dir_rmdir,
74};
75
Ian Kent4f8427d2009-12-15 16:45:42 -080076static void autofs4_add_active(struct dentry *dentry)
77{
78 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
79 struct autofs_info *ino = autofs4_dentry_ino(dentry);
80 if (ino) {
81 spin_lock(&sbi->lookup_lock);
82 if (!ino->active_count) {
83 if (list_empty(&ino->active))
84 list_add(&ino->active, &sbi->active_list);
85 }
86 ino->active_count++;
87 spin_unlock(&sbi->lookup_lock);
88 }
89 return;
90}
91
92static void autofs4_del_active(struct dentry *dentry)
93{
94 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
95 struct autofs_info *ino = autofs4_dentry_ino(dentry);
96 if (ino) {
97 spin_lock(&sbi->lookup_lock);
98 ino->active_count--;
99 if (!ino->active_count) {
100 if (!list_empty(&ino->active))
101 list_del_init(&ino->active);
102 }
103 spin_unlock(&sbi->lookup_lock);
104 }
105 return;
106}
107
Ian Kent36b64132009-12-15 16:45:44 -0800108static unsigned int autofs4_need_mount(unsigned int flags)
109{
110 unsigned int res = 0;
111 if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
112 res = 1;
113 return res;
114}
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static int autofs4_dir_open(struct inode *inode, struct file *file)
117{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800118 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 DPRINTK("file=%p dentry=%p %.*s",
122 file, dentry, dentry->d_name.len, dentry->d_name.name);
123
124 if (autofs4_oz_mode(sbi))
125 goto out;
126
Ian Kentff9cd492008-07-23 21:30:24 -0700127 /*
128 * An empty directory in an autofs file system is always a
129 * mount point. The daemon must have failed to mount this
130 * during lookup so it doesn't exist. This can happen, for
131 * example, if user space returns an incorrect status for a
132 * mount request. Otherwise we're doing a readdir on the
133 * autofs file system so just let the libfs routines handle
134 * it.
135 */
136 spin_lock(&dcache_lock);
Ian Kentc42c7f72009-12-15 16:45:48 -0800137 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 spin_unlock(&dcache_lock);
Ian Kentff9cd492008-07-23 21:30:24 -0700139 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
Ian Kentff9cd492008-07-23 21:30:24 -0700141 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Ian Kentf360ce32006-03-27 01:14:43 -0800143out:
Ian Kentff9cd492008-07-23 21:30:24 -0700144 return dcache_dir_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Al Viro4b1ae272010-03-03 12:58:31 -0500147static int try_to_fill_dentry(struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Ian Kent862b1102006-03-27 01:14:48 -0800149 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800150 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700151 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 DPRINTK("dentry=%p %.*s ino=%p",
154 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
155
Ian Kent718c6042006-03-27 01:14:42 -0800156 /*
157 * Wait for a pending mount, triggering one if there
158 * isn't one already
159 */
Al Viro4b1ae272010-03-03 12:58:31 -0500160 if (dentry->d_inode == NULL) {
161 DPRINTK("waiting for mount name=%.*s",
162 dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Al Viro4b1ae272010-03-03 12:58:31 -0500164 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800165
Al Viro4b1ae272010-03-03 12:58:31 -0500166 DPRINTK("mount done status=%d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Al Viro4b1ae272010-03-03 12:58:31 -0500168 /* Turn this into a real negative dentry? */
169 if (status == -ENOENT) {
170 spin_lock(&sbi->fs_lock);
171 ino->flags &= ~AUTOFS_INF_PENDING;
172 spin_unlock(&sbi->fs_lock);
173 return status;
174 } else if (status) {
175 /* Return a negative dentry, but leave it "pending" */
176 return status;
177 }
178 /* Trigger mount for path component or follow link */
179 } else if (ino->flags & AUTOFS_INF_PENDING ||
Ian Kentf7422462010-05-10 16:46:08 +0800180 autofs4_need_mount(flags)) {
Al Viro4b1ae272010-03-03 12:58:31 -0500181 DPRINTK("waiting for mount name=%.*s",
182 dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Al Viro4b1ae272010-03-03 12:58:31 -0500184 spin_lock(&sbi->fs_lock);
185 ino->flags |= AUTOFS_INF_PENDING;
186 spin_unlock(&sbi->fs_lock);
187 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
188
189 DPRINTK("mount done status=%d", status);
190
191 if (status) {
192 spin_lock(&sbi->fs_lock);
193 ino->flags &= ~AUTOFS_INF_PENDING;
194 spin_unlock(&sbi->fs_lock);
195 return status;
196 }
197 }
198
199 /* Initialize expiry counter after successful mount */
200 if (ino)
201 ino->last_used = jiffies;
202
203 spin_lock(&sbi->fs_lock);
204 ino->flags &= ~AUTOFS_INF_PENDING;
205 spin_unlock(&sbi->fs_lock);
206
207 return 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800208}
209
210/* For autofs direct mounts the follow link triggers the mount */
211static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
212{
213 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kenta5370552006-05-15 09:43:51 -0700214 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent34ca9592006-03-27 01:14:54 -0800215 int oz_mode = autofs4_oz_mode(sbi);
216 unsigned int lookup_type;
217 int status;
218
219 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
220 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
221 nd->flags);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700222 /*
223 * For an expire of a covered direct or offset mount we need
Al Viro9393bd02009-04-18 13:58:15 -0400224 * to break out of follow_down() at the autofs mount trigger
Ian Kent6e60a9a2008-07-23 21:30:27 -0700225 * (d_mounted--), so we can see the expiring flag, and manage
226 * the blocking and following here until the expire is completed.
227 */
228 if (oz_mode) {
229 spin_lock(&sbi->fs_lock);
230 if (ino->flags & AUTOFS_INF_EXPIRING) {
231 spin_unlock(&sbi->fs_lock);
232 /* Follow down to our covering mount. */
Al Viro9393bd02009-04-18 13:58:15 -0400233 if (!follow_down(&nd->path))
Ian Kent6e60a9a2008-07-23 21:30:27 -0700234 goto done;
Ian Kentec6e8c72008-07-23 21:30:28 -0700235 goto follow;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700236 }
237 spin_unlock(&sbi->fs_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800238 goto done;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700239 }
Ian Kent34ca9592006-03-27 01:14:54 -0800240
Ian Kent6e60a9a2008-07-23 21:30:27 -0700241 /* If an expire request is pending everyone must wait. */
Ian Kent06a35982008-07-23 21:30:28 -0700242 autofs4_expire_wait(dentry);
Ian Kent97e74492008-07-23 21:30:26 -0700243
Ian Kent6e60a9a2008-07-23 21:30:27 -0700244 /* We trigger a mount for almost all flags */
Ian Kent36b64132009-12-15 16:45:44 -0800245 lookup_type = autofs4_need_mount(nd->flags);
Ian Kentaa952eb2009-12-15 16:45:45 -0800246 spin_lock(&sbi->fs_lock);
247 spin_lock(&dcache_lock);
248 if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
249 spin_unlock(&dcache_lock);
250 spin_unlock(&sbi->fs_lock);
Ian Kentec6e8c72008-07-23 21:30:28 -0700251 goto follow;
Ian Kentaa952eb2009-12-15 16:45:45 -0800252 }
Ian Kent6e60a9a2008-07-23 21:30:27 -0700253
Ian Kent871f9432006-03-27 01:14:58 -0800254 /*
Ian Kent6e60a9a2008-07-23 21:30:27 -0700255 * If the dentry contains directories then it is an autofs
256 * multi-mount with no root mount offset. So don't try to
257 * mount it again.
Ian Kent871f9432006-03-27 01:14:58 -0800258 */
Ian Kentaa952eb2009-12-15 16:45:45 -0800259 if (ino->flags & AUTOFS_INF_PENDING ||
Ian Kentc42c7f72009-12-15 16:45:48 -0800260 (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
Ian Kent871f9432006-03-27 01:14:58 -0800261 spin_unlock(&dcache_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800262 spin_unlock(&sbi->fs_lock);
Ian Kent871f9432006-03-27 01:14:58 -0800263
Ian Kentf7422462010-05-10 16:46:08 +0800264 status = try_to_fill_dentry(dentry, nd->flags);
Ian Kent871f9432006-03-27 01:14:58 -0800265 if (status)
266 goto out_error;
267
Ian Kent6e60a9a2008-07-23 21:30:27 -0700268 goto follow;
Ian Kent871f9432006-03-27 01:14:58 -0800269 }
270 spin_unlock(&dcache_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800271 spin_unlock(&sbi->fs_lock);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700272follow:
273 /*
274 * If there is no root mount it must be an autofs
275 * multi-mount with no root offset so we don't need
276 * to follow it.
277 */
278 if (d_mountpoint(dentry)) {
Al Viro9393bd02009-04-18 13:58:15 -0400279 if (!autofs4_follow_mount(&nd->path)) {
Ian Kent6e60a9a2008-07-23 21:30:27 -0700280 status = -ENOENT;
281 goto out_error;
282 }
283 }
Ian Kent871f9432006-03-27 01:14:58 -0800284
Ian Kent34ca9592006-03-27 01:14:54 -0800285done:
286 return NULL;
287
288out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800289 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800290 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293/*
294 * Revalidate is called on every cache lookup. Some of those
295 * cache lookups may actually happen while the dentry is not
296 * yet completely filled in, and revalidate has to delay such
297 * lookups..
298 */
Ian Kent718c6042006-03-27 01:14:42 -0800299static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Ian Kent718c6042006-03-27 01:14:42 -0800301 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
Al Viro4b1ae272010-03-03 12:58:31 -0500303 int oz_mode = autofs4_oz_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int flags = nd ? nd->flags : 0;
Al Viro4b1ae272010-03-03 12:58:31 -0500305 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Ian Kent213614d2009-12-15 16:45:51 -0800307 /* Pending dentry */
Al Viro4b1ae272010-03-03 12:58:31 -0500308 spin_lock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (autofs4_ispending(dentry)) {
Al Viro4b1ae272010-03-03 12:58:31 -0500310 /* The daemon never causes a mount to trigger */
Ian Kent213614d2009-12-15 16:45:51 -0800311 spin_unlock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500312
313 if (oz_mode)
314 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700315
316 /*
Ian Kent06a35982008-07-23 21:30:28 -0700317 * If the directory has gone away due to an expire
318 * we have been called as ->d_revalidate() and so
319 * we need to return false and proceed to ->lookup().
320 */
321 if (autofs4_expire_wait(dentry) == -EAGAIN)
322 return 0;
323
324 /*
Ian Kentbcdc5e02006-09-27 01:50:44 -0700325 * A zero status is success otherwise we have a
326 * negative error code.
327 */
Al Viro4b1ae272010-03-03 12:58:31 -0500328 status = try_to_fill_dentry(dentry, flags);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700329 if (status == 0)
Ian Kentf50b6f82007-02-20 13:58:10 -0800330 return 1;
331
Ian Kentbcdc5e02006-09-27 01:50:44 -0700332 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Al Viro4b1ae272010-03-03 12:58:31 -0500334 spin_unlock(&sbi->fs_lock);
335
336 /* Negative dentry.. invalidate if "old" */
337 if (dentry->d_inode == NULL)
338 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* Check for a non-mountpoint directory with no contents */
Al Viro4b1ae272010-03-03 12:58:31 -0500341 spin_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (S_ISDIR(dentry->d_inode->i_mode) &&
Ian Kentc42c7f72009-12-15 16:45:48 -0800343 !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 DPRINTK("dentry=%p %.*s, emptydir",
345 dentry, dentry->d_name.len, dentry->d_name.name);
Al Viro4b1ae272010-03-03 12:58:31 -0500346 spin_unlock(&dcache_lock);
Ian Kent97e74492008-07-23 21:30:26 -0700347
Al Viro4b1ae272010-03-03 12:58:31 -0500348 /* The daemon never causes a mount to trigger */
349 if (oz_mode)
350 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700351
Al Viro4b1ae272010-03-03 12:58:31 -0500352 /*
353 * A zero status is success otherwise we have a
354 * negative error code.
355 */
356 status = try_to_fill_dentry(dentry, flags);
357 if (status == 0)
358 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700359
Al Viro4b1ae272010-03-03 12:58:31 -0500360 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362 spin_unlock(&dcache_lock);
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 1;
365}
366
Ian Kent34ca9592006-03-27 01:14:54 -0800367void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 struct autofs_info *inf;
370
371 DPRINTK("releasing %p", de);
372
373 inf = autofs4_dentry_ino(de);
374 de->d_fsdata = NULL;
375
376 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800377 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
378
Ian Kentf50b6f82007-02-20 13:58:10 -0800379 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700380 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700381 if (!list_empty(&inf->active))
382 list_del(&inf->active);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700383 if (!list_empty(&inf->expiring))
384 list_del(&inf->expiring);
385 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800386 }
387
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700388 inf->dentry = NULL;
389 inf->inode = NULL;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 autofs4_free_ino(inf);
392 }
393}
394
395/* For dentries of directories in the root dir */
Al Viro08f11512009-02-20 05:56:19 +0000396static const struct dentry_operations autofs4_root_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 .d_revalidate = autofs4_revalidate,
398 .d_release = autofs4_dentry_release,
399};
400
401/* For other dentries */
Al Viro08f11512009-02-20 05:56:19 +0000402static const struct dentry_operations autofs4_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 .d_revalidate = autofs4_revalidate,
404 .d_release = autofs4_dentry_release,
405};
406
Ian Kent6510c9d2009-12-15 16:45:47 -0800407static struct dentry *autofs4_lookup_active(struct dentry *dentry)
Ian Kent25767372008-07-23 21:30:12 -0700408{
Ian Kent6510c9d2009-12-15 16:45:47 -0800409 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
410 struct dentry *parent = dentry->d_parent;
411 struct qstr *name = &dentry->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700412 unsigned int len = name->len;
413 unsigned int hash = name->hash;
414 const unsigned char *str = name->name;
415 struct list_head *p, *head;
416
417 spin_lock(&dcache_lock);
418 spin_lock(&sbi->lookup_lock);
419 head = &sbi->active_list;
420 list_for_each(p, head) {
421 struct autofs_info *ino;
Ian Kente4d5ade2009-12-15 16:45:49 -0800422 struct dentry *active;
Ian Kent25767372008-07-23 21:30:12 -0700423 struct qstr *qstr;
424
425 ino = list_entry(p, struct autofs_info, active);
Ian Kente4d5ade2009-12-15 16:45:49 -0800426 active = ino->dentry;
Ian Kent25767372008-07-23 21:30:12 -0700427
Ian Kente4d5ade2009-12-15 16:45:49 -0800428 spin_lock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700429
430 /* Already gone? */
Ian Kente4d5ade2009-12-15 16:45:49 -0800431 if (atomic_read(&active->d_count) == 0)
Ian Kent25767372008-07-23 21:30:12 -0700432 goto next;
433
Ian Kente4d5ade2009-12-15 16:45:49 -0800434 qstr = &active->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700435
Ian Kente4d5ade2009-12-15 16:45:49 -0800436 if (active->d_name.hash != hash)
Ian Kent25767372008-07-23 21:30:12 -0700437 goto next;
Ian Kente4d5ade2009-12-15 16:45:49 -0800438 if (active->d_parent != parent)
Ian Kent25767372008-07-23 21:30:12 -0700439 goto next;
440
441 if (qstr->len != len)
442 goto next;
443 if (memcmp(qstr->name, str, len))
444 goto next;
445
Al Viro4b1ae272010-03-03 12:58:31 -0500446 if (d_unhashed(active)) {
447 dget(active);
448 spin_unlock(&active->d_lock);
449 spin_unlock(&sbi->lookup_lock);
450 spin_unlock(&dcache_lock);
451 return active;
452 }
Ian Kent25767372008-07-23 21:30:12 -0700453next:
Ian Kente4d5ade2009-12-15 16:45:49 -0800454 spin_unlock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700455 }
456 spin_unlock(&sbi->lookup_lock);
457 spin_unlock(&dcache_lock);
458
459 return NULL;
460}
461
Ian Kent6510c9d2009-12-15 16:45:47 -0800462static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
Ian Kentf50b6f82007-02-20 13:58:10 -0800463{
Ian Kent6510c9d2009-12-15 16:45:47 -0800464 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
465 struct dentry *parent = dentry->d_parent;
466 struct qstr *name = &dentry->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800467 unsigned int len = name->len;
468 unsigned int hash = name->hash;
469 const unsigned char *str = name->name;
470 struct list_head *p, *head;
471
472 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700473 spin_lock(&sbi->lookup_lock);
474 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800475 list_for_each(p, head) {
476 struct autofs_info *ino;
Ian Kentcb4b4922009-12-15 16:45:50 -0800477 struct dentry *expiring;
Ian Kentf50b6f82007-02-20 13:58:10 -0800478 struct qstr *qstr;
479
Ian Kent5f6f4f22008-07-23 21:30:09 -0700480 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentcb4b4922009-12-15 16:45:50 -0800481 expiring = ino->dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800482
Ian Kentcb4b4922009-12-15 16:45:50 -0800483 spin_lock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800484
485 /* Bad luck, we've already been dentry_iput */
Ian Kentcb4b4922009-12-15 16:45:50 -0800486 if (!expiring->d_inode)
Ian Kentf50b6f82007-02-20 13:58:10 -0800487 goto next;
488
Ian Kentcb4b4922009-12-15 16:45:50 -0800489 qstr = &expiring->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800490
Ian Kentcb4b4922009-12-15 16:45:50 -0800491 if (expiring->d_name.hash != hash)
Ian Kentf50b6f82007-02-20 13:58:10 -0800492 goto next;
Ian Kentcb4b4922009-12-15 16:45:50 -0800493 if (expiring->d_parent != parent)
Ian Kentf50b6f82007-02-20 13:58:10 -0800494 goto next;
495
496 if (qstr->len != len)
497 goto next;
498 if (memcmp(qstr->name, str, len))
499 goto next;
500
Al Viro4b1ae272010-03-03 12:58:31 -0500501 if (d_unhashed(expiring)) {
502 dget(expiring);
503 spin_unlock(&expiring->d_lock);
504 spin_unlock(&sbi->lookup_lock);
505 spin_unlock(&dcache_lock);
506 return expiring;
507 }
Ian Kentf50b6f82007-02-20 13:58:10 -0800508next:
Ian Kentcb4b4922009-12-15 16:45:50 -0800509 spin_unlock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800510 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700511 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800512 spin_unlock(&dcache_lock);
513
514 return NULL;
515}
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517/* Lookups in the root directory */
518static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
519{
520 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700521 struct autofs_info *ino;
Ian Kent90387c92009-12-15 16:45:46 -0800522 struct dentry *expiring, *active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 int oz_mode;
524
525 DPRINTK("name = %.*s",
526 dentry->d_name.len, dentry->d_name.name);
527
Ian Kent718c6042006-03-27 01:14:42 -0800528 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800530 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700536 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Ian Kent6510c9d2009-12-15 16:45:47 -0800538 active = autofs4_lookup_active(dentry);
Ian Kent90387c92009-12-15 16:45:46 -0800539 if (active) {
540 dentry = active;
Ian Kentaa952eb2009-12-15 16:45:45 -0800541 ino = autofs4_dentry_ino(dentry);
542 } else {
Al Viro4b1ae272010-03-03 12:58:31 -0500543 /*
544 * Mark the dentry incomplete but don't hash it. We do this
545 * to serialize our inode creation operations (symlink and
546 * mkdir) which prevents deadlock during the callback to
547 * the daemon. Subsequent user space lookups for the same
548 * dentry are placed on the wait queue while the daemon
549 * itself is allowed passage unresticted so the create
550 * operation itself can then hash the dentry. Finally,
551 * we check for the hashed dentry and return the newly
552 * hashed dentry.
553 */
554 dentry->d_op = &autofs4_root_dentry_operations;
555
556 /*
557 * And we need to ensure that the same dentry is used for
558 * all following lookup calls until it is hashed so that
559 * the dentry flags are persistent throughout the request.
560 */
561 ino = autofs4_init_ino(NULL, sbi, 0555);
562 if (!ino)
563 return ERR_PTR(-ENOMEM);
564
565 dentry->d_fsdata = ino;
566 ino->dentry = dentry;
567
568 autofs4_add_active(dentry);
569
570 d_instantiate(dentry, NULL);
Ian Kent25767372008-07-23 21:30:12 -0700571 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (!oz_mode) {
Ian Kent213614d2009-12-15 16:45:51 -0800574 mutex_unlock(&dir->i_mutex);
Al Viro4b1ae272010-03-03 12:58:31 -0500575 expiring = autofs4_lookup_expiring(dentry);
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700576 if (expiring) {
577 /*
578 * If we are racing with expire the request might not
579 * be quite complete but the directory has been removed
580 * so it must have been successful, so just wait for it.
581 */
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700582 autofs4_expire_wait(expiring);
Al Viro4b1ae272010-03-03 12:58:31 -0500583 autofs4_del_expiring(expiring);
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700584 dput(expiring);
585 }
Al Viro4b1ae272010-03-03 12:58:31 -0500586
Ian Kent213614d2009-12-15 16:45:51 -0800587 spin_lock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500588 ino->flags |= AUTOFS_INF_PENDING;
Ian Kent213614d2009-12-15 16:45:51 -0800589 spin_unlock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500590 if (dentry->d_op && dentry->d_op->d_revalidate)
591 (dentry->d_op->d_revalidate)(dentry, nd);
592 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
595 /*
Al Viro4b1ae272010-03-03 12:58:31 -0500596 * If we are still pending, check if we had to handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * a signal. If so we can force a restart..
598 */
Al Viro4b1ae272010-03-03 12:58:31 -0500599 if (ino->flags & AUTOFS_INF_PENDING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 /* See if we were interrupted */
601 if (signal_pending(current)) {
602 sigset_t *sigset = &current->pending.signal;
603 if (sigismember (sigset, SIGKILL) ||
604 sigismember (sigset, SIGQUIT) ||
605 sigismember (sigset, SIGINT)) {
Ian Kent90387c92009-12-15 16:45:46 -0800606 if (active)
607 dput(active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return ERR_PTR(-ERESTARTNOINTR);
609 }
610 }
Al Viro4b1ae272010-03-03 12:58:31 -0500611 if (!oz_mode) {
612 spin_lock(&sbi->fs_lock);
613 ino->flags &= ~AUTOFS_INF_PENDING;
614 spin_unlock(&sbi->fs_lock);
Ian Kent25767372008-07-23 21:30:12 -0700615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
618 /*
Al Viro4b1ae272010-03-03 12:58:31 -0500619 * If this dentry is unhashed, then we shouldn't honour this
620 * lookup. Returning ENOENT here doesn't do the right thing
621 * for all system calls, but it should be OK for the operations
622 * we permit from an autofs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
Al Viro4b1ae272010-03-03 12:58:31 -0500624 if (!oz_mode && d_unhashed(dentry)) {
625 /*
626 * A user space application can (and has done in the past)
627 * remove and re-create this directory during the callback.
628 * This can leave us with an unhashed dentry, but a
629 * successful mount! So we need to perform another
630 * cached lookup in case the dentry now exists.
631 */
632 struct dentry *parent = dentry->d_parent;
633 struct dentry *new = d_lookup(parent, &dentry->d_name);
634 if (new != NULL)
635 dentry = new;
636 else
637 dentry = ERR_PTR(-ENOENT);
638
Ian Kent90387c92009-12-15 16:45:46 -0800639 if (active)
640 dput(active);
Al Viro4b1ae272010-03-03 12:58:31 -0500641
Ian Kentc9ffec42007-02-20 13:58:10 -0800642 return dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800643 }
644
Al Viro4b1ae272010-03-03 12:58:31 -0500645 if (active)
646 return active;
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return NULL;
649}
650
651static int autofs4_dir_symlink(struct inode *dir,
652 struct dentry *dentry,
653 const char *symname)
654{
655 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
656 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800657 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct inode *inode;
659 char *cp;
660
661 DPRINTK("%s <- %.*s", symname,
662 dentry->d_name.len, dentry->d_name.name);
663
664 if (!autofs4_oz_mode(sbi))
665 return -EACCES;
666
667 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700668 if (!ino)
669 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Al Viro4b1ae272010-03-03 12:58:31 -0500671 autofs4_del_active(dentry);
672
Ian Kentef581a72008-07-23 21:30:13 -0700673 ino->size = strlen(symname);
Ian Kent25767372008-07-23 21:30:12 -0700674 cp = kmalloc(ino->size + 1, GFP_KERNEL);
675 if (!cp) {
676 if (!dentry->d_fsdata)
677 kfree(ino);
678 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
681 strcpy(cp, symname);
682
683 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700684 if (!inode) {
685 kfree(cp);
686 if (!dentry->d_fsdata)
687 kfree(ino);
688 return -ENOMEM;
689 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700690 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (dir == dir->i_sb->s_root->d_inode)
693 dentry->d_op = &autofs4_root_dentry_operations;
694 else
695 dentry->d_op = &autofs4_dentry_operations;
696
697 dentry->d_fsdata = ino;
698 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800699 atomic_inc(&ino->count);
700 p_ino = autofs4_dentry_ino(dentry->d_parent);
701 if (p_ino && dentry->d_parent != dentry)
702 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 ino->inode = inode;
704
Ian Kent25767372008-07-23 21:30:12 -0700705 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 dir->i_mtime = CURRENT_TIME;
707
708 return 0;
709}
710
711/*
712 * NOTE!
713 *
714 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
715 * that the file no longer exists. However, doing that means that the
716 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800717 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700718 * We simply d_drop it and add it to a expiring list in the super block,
719 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 *
721 * If a process is blocked on the dentry waiting for the expire to finish,
722 * it will invalidate the dentry and try to mount with a new one.
723 *
724 * Also see autofs4_dir_rmdir()..
725 */
726static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
727{
728 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
729 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800730 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700733 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return -EACCES;
735
Ian Kent1aff3c82006-03-27 01:14:46 -0800736 if (atomic_dec_and_test(&ino->count)) {
737 p_ino = autofs4_dentry_ino(dentry->d_parent);
738 if (p_ino && dentry->d_parent != dentry)
739 atomic_dec(&p_ino->count);
740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 dput(ino->dentry);
742
743 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700744 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 dir->i_mtime = CURRENT_TIME;
747
Ian Kentf50b6f82007-02-20 13:58:10 -0800748 spin_lock(&dcache_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500749 autofs4_add_expiring(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800750 spin_lock(&dentry->d_lock);
751 __d_drop(dentry);
752 spin_unlock(&dentry->d_lock);
753 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 return 0;
756}
757
758static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
759{
760 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
761 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800762 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Ian Kentf50b6f82007-02-20 13:58:10 -0800764 DPRINTK("dentry %p, removing %.*s",
765 dentry, dentry->d_name.len, dentry->d_name.name);
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (!autofs4_oz_mode(sbi))
768 return -EACCES;
769
770 spin_lock(&dcache_lock);
771 if (!list_empty(&dentry->d_subdirs)) {
772 spin_unlock(&dcache_lock);
773 return -ENOTEMPTY;
774 }
Al Viro4b1ae272010-03-03 12:58:31 -0500775 autofs4_add_expiring(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 spin_lock(&dentry->d_lock);
777 __d_drop(dentry);
778 spin_unlock(&dentry->d_lock);
779 spin_unlock(&dcache_lock);
780
Ian Kent1aff3c82006-03-27 01:14:46 -0800781 if (atomic_dec_and_test(&ino->count)) {
782 p_ino = autofs4_dentry_ino(dentry->d_parent);
783 if (p_ino && dentry->d_parent != dentry)
784 atomic_dec(&p_ino->count);
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700788 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700791 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 return 0;
794}
795
796static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
797{
798 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
799 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800800 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct inode *inode;
802
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700803 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return -EACCES;
805
806 DPRINTK("dentry %p, creating %.*s",
807 dentry, dentry->d_name.len, dentry->d_name.name);
808
809 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700810 if (!ino)
811 return -ENOMEM;
812
Al Viro4b1ae272010-03-03 12:58:31 -0500813 autofs4_del_active(dentry);
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700816 if (!inode) {
817 if (!dentry->d_fsdata)
818 kfree(ino);
819 return -ENOMEM;
820 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700821 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (dir == dir->i_sb->s_root->d_inode)
824 dentry->d_op = &autofs4_root_dentry_operations;
825 else
826 dentry->d_op = &autofs4_dentry_operations;
827
828 dentry->d_fsdata = ino;
829 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800830 atomic_inc(&ino->count);
831 p_ino = autofs4_dentry_ino(dentry->d_parent);
832 if (p_ino && dentry->d_parent != dentry)
833 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700835 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 dir->i_mtime = CURRENT_TIME;
837
838 return 0;
839}
840
841/* Get/set timeout ioctl() operation */
842static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
843 unsigned long __user *p)
844{
845 int rv;
846 unsigned long ntimeout;
847
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700848 if ((rv = get_user(ntimeout, p)) ||
849 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return rv;
851
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700852 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 sbi->exp_timeout = 0;
854 else
855 sbi->exp_timeout = ntimeout * HZ;
856
857 return 0;
858}
859
860/* Return protocol version */
861static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
862{
863 return put_user(sbi->version, p);
864}
865
866/* Return protocol sub version */
867static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
868{
869 return put_user(sbi->sub_version, p);
870}
871
872/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873* Tells the daemon whether it can umount the autofs mount.
874*/
875static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
876{
877 int status = 0;
878
Ian Kente3474a82006-03-27 01:14:51 -0800879 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 status = 1;
881
882 DPRINTK("returning %d", status);
883
884 status = put_user(status, p);
885
886 return status;
887}
888
889/* Identify autofs4_dentries - this is so we can tell if there's
890 an extra dentry refcount or not. We only hold a refcount on the
891 dentry if its non-negative (ie, d_inode != NULL)
892*/
893int is_autofs4_dentry(struct dentry *dentry)
894{
895 return dentry && dentry->d_inode &&
896 (dentry->d_op == &autofs4_root_dentry_operations ||
897 dentry->d_op == &autofs4_dentry_operations) &&
898 dentry->d_fsdata != NULL;
899}
900
901/*
902 * ioctl()'s on the root directory is the chief method for the daemon to
903 * generate kernel reactions
904 */
905static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
906 unsigned int cmd, unsigned long arg)
907{
908 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
909 void __user *p = (void __user *)arg;
910
911 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700912 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700914 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
915 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -ENOTTY;
917
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700918 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 return -EPERM;
920
921 switch(cmd) {
922 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
923 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
924 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
925 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
926 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
927 autofs4_catatonic_mode(sbi);
928 return 0;
929 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
930 return autofs4_get_protover(sbi, p);
931 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
932 return autofs4_get_protosubver(sbi, p);
933 case AUTOFS_IOC_SETTIMEOUT:
934 return autofs4_get_set_timeout(sbi, p);
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800937 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /* return a single thing to expire */
940 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800941 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* same as above, but can send multiple expires through pipe */
943 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800944 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 default:
947 return -ENOSYS;
948 }
949}