blob: db4117ed78031232b2741a5052a2e383e899e457 [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>
Frederic Weisbecker3663df72010-05-19 15:08:17 +020021#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "autofs_i.h"
23
24static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
25static int autofs4_dir_unlink(struct inode *,struct dentry *);
26static int autofs4_dir_rmdir(struct inode *,struct dentry *);
27static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
Frederic Weisbecker3663df72010-05-19 15:08:17 +020028static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static int autofs4_dir_open(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
Ian Kent34ca9592006-03-27 01:14:54 -080031static void *autofs4_follow_link(struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Ian Kent6d5cb922008-07-23 21:30:15 -070033#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
34#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
35
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080036const struct file_operations autofs4_root_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 .open = dcache_dir_open,
38 .release = dcache_dir_close,
39 .read = generic_read_dir,
Ian Kentaa55ddf2008-07-23 21:30:29 -070040 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040041 .llseek = dcache_dir_lseek,
Frederic Weisbecker3663df72010-05-19 15:08:17 +020042 .unlocked_ioctl = autofs4_root_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080045const struct file_operations autofs4_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 .open = autofs4_dir_open,
Ian Kentff9cd492008-07-23 21:30:24 -070047 .release = dcache_dir_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 .read = generic_read_dir,
Ian Kentff9cd492008-07-23 21:30:24 -070049 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040050 .llseek = dcache_dir_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
Arjan van de Ven754661f2007-02-12 00:55:38 -080053const struct inode_operations autofs4_indirect_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .lookup = autofs4_lookup,
55 .unlink = autofs4_dir_unlink,
56 .symlink = autofs4_dir_symlink,
57 .mkdir = autofs4_dir_mkdir,
58 .rmdir = autofs4_dir_rmdir,
59};
60
Arjan van de Ven754661f2007-02-12 00:55:38 -080061const struct inode_operations autofs4_direct_root_inode_operations = {
Ian Kent34ca9592006-03-27 01:14:54 -080062 .lookup = autofs4_lookup,
Ian Kent871f9432006-03-27 01:14:58 -080063 .unlink = autofs4_dir_unlink,
64 .mkdir = autofs4_dir_mkdir,
65 .rmdir = autofs4_dir_rmdir,
Ian Kent34ca9592006-03-27 01:14:54 -080066 .follow_link = autofs4_follow_link,
67};
68
Arjan van de Ven754661f2007-02-12 00:55:38 -080069const struct inode_operations autofs4_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 .lookup = autofs4_lookup,
71 .unlink = autofs4_dir_unlink,
72 .symlink = autofs4_dir_symlink,
73 .mkdir = autofs4_dir_mkdir,
74 .rmdir = autofs4_dir_rmdir,
75};
76
Ian Kent4f8427d2009-12-15 16:45:42 -080077static void autofs4_add_active(struct dentry *dentry)
78{
79 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
80 struct autofs_info *ino = autofs4_dentry_ino(dentry);
81 if (ino) {
82 spin_lock(&sbi->lookup_lock);
83 if (!ino->active_count) {
84 if (list_empty(&ino->active))
85 list_add(&ino->active, &sbi->active_list);
86 }
87 ino->active_count++;
88 spin_unlock(&sbi->lookup_lock);
89 }
90 return;
91}
92
93static void autofs4_del_active(struct dentry *dentry)
94{
95 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
96 struct autofs_info *ino = autofs4_dentry_ino(dentry);
97 if (ino) {
98 spin_lock(&sbi->lookup_lock);
99 ino->active_count--;
100 if (!ino->active_count) {
101 if (!list_empty(&ino->active))
102 list_del_init(&ino->active);
103 }
104 spin_unlock(&sbi->lookup_lock);
105 }
106 return;
107}
108
Ian Kent36b64132009-12-15 16:45:44 -0800109static unsigned int autofs4_need_mount(unsigned int flags)
110{
111 unsigned int res = 0;
112 if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
113 res = 1;
114 return res;
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static int autofs4_dir_open(struct inode *inode, struct file *file)
118{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800119 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 DPRINTK("file=%p dentry=%p %.*s",
123 file, dentry, dentry->d_name.len, dentry->d_name.name);
124
125 if (autofs4_oz_mode(sbi))
126 goto out;
127
Ian Kentff9cd492008-07-23 21:30:24 -0700128 /*
129 * An empty directory in an autofs file system is always a
130 * mount point. The daemon must have failed to mount this
131 * during lookup so it doesn't exist. This can happen, for
132 * example, if user space returns an incorrect status for a
133 * mount request. Otherwise we're doing a readdir on the
134 * autofs file system so just let the libfs routines handle
135 * it.
136 */
137 spin_lock(&dcache_lock);
Ian Kentc42c7f72009-12-15 16:45:48 -0800138 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 spin_unlock(&dcache_lock);
Ian Kentff9cd492008-07-23 21:30:24 -0700140 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
Ian Kentff9cd492008-07-23 21:30:24 -0700142 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Ian Kentf360ce32006-03-27 01:14:43 -0800144out:
Ian Kentff9cd492008-07-23 21:30:24 -0700145 return dcache_dir_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Al Viro4b1ae272010-03-03 12:58:31 -0500148static int try_to_fill_dentry(struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Ian Kent862b1102006-03-27 01:14:48 -0800150 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800151 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700152 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 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 */
Al Viro4b1ae272010-03-03 12:58:31 -0500161 if (dentry->d_inode == NULL) {
162 DPRINTK("waiting for mount name=%.*s",
163 dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Al Viro4b1ae272010-03-03 12:58:31 -0500165 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800166
Al Viro4b1ae272010-03-03 12:58:31 -0500167 DPRINTK("mount done status=%d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Al Viro4b1ae272010-03-03 12:58:31 -0500169 /* Turn this into a real negative dentry? */
170 if (status == -ENOENT) {
171 spin_lock(&sbi->fs_lock);
172 ino->flags &= ~AUTOFS_INF_PENDING;
173 spin_unlock(&sbi->fs_lock);
174 return status;
175 } else if (status) {
176 /* Return a negative dentry, but leave it "pending" */
177 return status;
178 }
179 /* Trigger mount for path component or follow link */
180 } else if (ino->flags & AUTOFS_INF_PENDING ||
Ian Kentf7422462010-05-10 16:46:08 +0800181 autofs4_need_mount(flags)) {
Al Viro4b1ae272010-03-03 12:58:31 -0500182 DPRINTK("waiting for mount name=%.*s",
183 dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Al Viro4b1ae272010-03-03 12:58:31 -0500185 spin_lock(&sbi->fs_lock);
186 ino->flags |= AUTOFS_INF_PENDING;
187 spin_unlock(&sbi->fs_lock);
188 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
189
190 DPRINTK("mount done status=%d", status);
191
192 if (status) {
193 spin_lock(&sbi->fs_lock);
194 ino->flags &= ~AUTOFS_INF_PENDING;
195 spin_unlock(&sbi->fs_lock);
196 return status;
197 }
198 }
199
200 /* Initialize expiry counter after successful mount */
201 if (ino)
202 ino->last_used = jiffies;
203
204 spin_lock(&sbi->fs_lock);
205 ino->flags &= ~AUTOFS_INF_PENDING;
206 spin_unlock(&sbi->fs_lock);
207
208 return 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800209}
210
211/* For autofs direct mounts the follow link triggers the mount */
212static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
213{
214 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kenta5370552006-05-15 09:43:51 -0700215 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent34ca9592006-03-27 01:14:54 -0800216 int oz_mode = autofs4_oz_mode(sbi);
217 unsigned int lookup_type;
218 int status;
219
220 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
221 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
222 nd->flags);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700223 /*
224 * For an expire of a covered direct or offset mount we need
Al Viro9393bd02009-04-18 13:58:15 -0400225 * to break out of follow_down() at the autofs mount trigger
Ian Kent6e60a9a2008-07-23 21:30:27 -0700226 * (d_mounted--), so we can see the expiring flag, and manage
227 * the blocking and following here until the expire is completed.
228 */
229 if (oz_mode) {
230 spin_lock(&sbi->fs_lock);
231 if (ino->flags & AUTOFS_INF_EXPIRING) {
232 spin_unlock(&sbi->fs_lock);
233 /* Follow down to our covering mount. */
Al Viro9393bd02009-04-18 13:58:15 -0400234 if (!follow_down(&nd->path))
Ian Kent6e60a9a2008-07-23 21:30:27 -0700235 goto done;
Ian Kentec6e8c72008-07-23 21:30:28 -0700236 goto follow;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700237 }
238 spin_unlock(&sbi->fs_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800239 goto done;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700240 }
Ian Kent34ca9592006-03-27 01:14:54 -0800241
Ian Kent6e60a9a2008-07-23 21:30:27 -0700242 /* If an expire request is pending everyone must wait. */
Ian Kent06a35982008-07-23 21:30:28 -0700243 autofs4_expire_wait(dentry);
Ian Kent97e74492008-07-23 21:30:26 -0700244
Ian Kent6e60a9a2008-07-23 21:30:27 -0700245 /* We trigger a mount for almost all flags */
Ian Kent36b64132009-12-15 16:45:44 -0800246 lookup_type = autofs4_need_mount(nd->flags);
Ian Kentaa952eb2009-12-15 16:45:45 -0800247 spin_lock(&sbi->fs_lock);
248 spin_lock(&dcache_lock);
249 if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
250 spin_unlock(&dcache_lock);
251 spin_unlock(&sbi->fs_lock);
Ian Kentec6e8c72008-07-23 21:30:28 -0700252 goto follow;
Ian Kentaa952eb2009-12-15 16:45:45 -0800253 }
Ian Kent6e60a9a2008-07-23 21:30:27 -0700254
Ian Kent871f9432006-03-27 01:14:58 -0800255 /*
Ian Kent6e60a9a2008-07-23 21:30:27 -0700256 * If the dentry contains directories then it is an autofs
257 * multi-mount with no root mount offset. So don't try to
258 * mount it again.
Ian Kent871f9432006-03-27 01:14:58 -0800259 */
Ian Kentaa952eb2009-12-15 16:45:45 -0800260 if (ino->flags & AUTOFS_INF_PENDING ||
Ian Kentc42c7f72009-12-15 16:45:48 -0800261 (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
Ian Kent871f9432006-03-27 01:14:58 -0800262 spin_unlock(&dcache_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800263 spin_unlock(&sbi->fs_lock);
Ian Kent871f9432006-03-27 01:14:58 -0800264
Ian Kentf7422462010-05-10 16:46:08 +0800265 status = try_to_fill_dentry(dentry, nd->flags);
Ian Kent871f9432006-03-27 01:14:58 -0800266 if (status)
267 goto out_error;
268
Ian Kent6e60a9a2008-07-23 21:30:27 -0700269 goto follow;
Ian Kent871f9432006-03-27 01:14:58 -0800270 }
271 spin_unlock(&dcache_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800272 spin_unlock(&sbi->fs_lock);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700273follow:
274 /*
275 * If there is no root mount it must be an autofs
276 * multi-mount with no root offset so we don't need
277 * to follow it.
278 */
279 if (d_mountpoint(dentry)) {
Al Viro9393bd02009-04-18 13:58:15 -0400280 if (!autofs4_follow_mount(&nd->path)) {
Ian Kent6e60a9a2008-07-23 21:30:27 -0700281 status = -ENOENT;
282 goto out_error;
283 }
284 }
Ian Kent871f9432006-03-27 01:14:58 -0800285
Ian Kent34ca9592006-03-27 01:14:54 -0800286done:
287 return NULL;
288
289out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800290 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800291 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/*
295 * Revalidate is called on every cache lookup. Some of those
296 * cache lookups may actually happen while the dentry is not
297 * yet completely filled in, and revalidate has to delay such
298 * lookups..
299 */
Ian Kent718c6042006-03-27 01:14:42 -0800300static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Ian Kent718c6042006-03-27 01:14:42 -0800302 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
Al Viro4b1ae272010-03-03 12:58:31 -0500304 int oz_mode = autofs4_oz_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 int flags = nd ? nd->flags : 0;
Al Viro4b1ae272010-03-03 12:58:31 -0500306 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Ian Kent213614d2009-12-15 16:45:51 -0800308 /* Pending dentry */
Al Viro4b1ae272010-03-03 12:58:31 -0500309 spin_lock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (autofs4_ispending(dentry)) {
Al Viro4b1ae272010-03-03 12:58:31 -0500311 /* The daemon never causes a mount to trigger */
Ian Kent213614d2009-12-15 16:45:51 -0800312 spin_unlock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500313
314 if (oz_mode)
315 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700316
317 /*
Ian Kent06a35982008-07-23 21:30:28 -0700318 * If the directory has gone away due to an expire
319 * we have been called as ->d_revalidate() and so
320 * we need to return false and proceed to ->lookup().
321 */
322 if (autofs4_expire_wait(dentry) == -EAGAIN)
323 return 0;
324
325 /*
Ian Kentbcdc5e02006-09-27 01:50:44 -0700326 * A zero status is success otherwise we have a
327 * negative error code.
328 */
Al Viro4b1ae272010-03-03 12:58:31 -0500329 status = try_to_fill_dentry(dentry, flags);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700330 if (status == 0)
Ian Kentf50b6f82007-02-20 13:58:10 -0800331 return 1;
332
Ian Kentbcdc5e02006-09-27 01:50:44 -0700333 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
Al Viro4b1ae272010-03-03 12:58:31 -0500335 spin_unlock(&sbi->fs_lock);
336
337 /* Negative dentry.. invalidate if "old" */
338 if (dentry->d_inode == NULL)
339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* Check for a non-mountpoint directory with no contents */
Al Viro4b1ae272010-03-03 12:58:31 -0500342 spin_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (S_ISDIR(dentry->d_inode->i_mode) &&
Ian Kentc42c7f72009-12-15 16:45:48 -0800344 !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 DPRINTK("dentry=%p %.*s, emptydir",
346 dentry, dentry->d_name.len, dentry->d_name.name);
Al Viro4b1ae272010-03-03 12:58:31 -0500347 spin_unlock(&dcache_lock);
Ian Kent97e74492008-07-23 21:30:26 -0700348
Al Viro4b1ae272010-03-03 12:58:31 -0500349 /* The daemon never causes a mount to trigger */
350 if (oz_mode)
351 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700352
Al Viro4b1ae272010-03-03 12:58:31 -0500353 /*
354 * A zero status is success otherwise we have a
355 * negative error code.
356 */
357 status = try_to_fill_dentry(dentry, flags);
358 if (status == 0)
359 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700360
Al Viro4b1ae272010-03-03 12:58:31 -0500361 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363 spin_unlock(&dcache_lock);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return 1;
366}
367
Ian Kent34ca9592006-03-27 01:14:54 -0800368void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct autofs_info *inf;
371
372 DPRINTK("releasing %p", de);
373
374 inf = autofs4_dentry_ino(de);
375 de->d_fsdata = NULL;
376
377 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800378 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
379
Ian Kentf50b6f82007-02-20 13:58:10 -0800380 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700381 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700382 if (!list_empty(&inf->active))
383 list_del(&inf->active);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700384 if (!list_empty(&inf->expiring))
385 list_del(&inf->expiring);
386 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800387 }
388
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700389 inf->dentry = NULL;
390 inf->inode = NULL;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 autofs4_free_ino(inf);
393 }
394}
395
396/* For dentries of directories in the root dir */
Al Viro08f11512009-02-20 05:56:19 +0000397static const struct dentry_operations autofs4_root_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 .d_revalidate = autofs4_revalidate,
399 .d_release = autofs4_dentry_release,
400};
401
402/* For other dentries */
Al Viro08f11512009-02-20 05:56:19 +0000403static const struct dentry_operations autofs4_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 .d_revalidate = autofs4_revalidate,
405 .d_release = autofs4_dentry_release,
406};
407
Ian Kent6510c9d2009-12-15 16:45:47 -0800408static struct dentry *autofs4_lookup_active(struct dentry *dentry)
Ian Kent25767372008-07-23 21:30:12 -0700409{
Ian Kent6510c9d2009-12-15 16:45:47 -0800410 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
411 struct dentry *parent = dentry->d_parent;
412 struct qstr *name = &dentry->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700413 unsigned int len = name->len;
414 unsigned int hash = name->hash;
415 const unsigned char *str = name->name;
416 struct list_head *p, *head;
417
418 spin_lock(&dcache_lock);
419 spin_lock(&sbi->lookup_lock);
420 head = &sbi->active_list;
421 list_for_each(p, head) {
422 struct autofs_info *ino;
Ian Kente4d5ade2009-12-15 16:45:49 -0800423 struct dentry *active;
Ian Kent25767372008-07-23 21:30:12 -0700424 struct qstr *qstr;
425
426 ino = list_entry(p, struct autofs_info, active);
Ian Kente4d5ade2009-12-15 16:45:49 -0800427 active = ino->dentry;
Ian Kent25767372008-07-23 21:30:12 -0700428
Ian Kente4d5ade2009-12-15 16:45:49 -0800429 spin_lock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700430
431 /* Already gone? */
Ian Kente4d5ade2009-12-15 16:45:49 -0800432 if (atomic_read(&active->d_count) == 0)
Ian Kent25767372008-07-23 21:30:12 -0700433 goto next;
434
Ian Kente4d5ade2009-12-15 16:45:49 -0800435 qstr = &active->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700436
Ian Kente4d5ade2009-12-15 16:45:49 -0800437 if (active->d_name.hash != hash)
Ian Kent25767372008-07-23 21:30:12 -0700438 goto next;
Ian Kente4d5ade2009-12-15 16:45:49 -0800439 if (active->d_parent != parent)
Ian Kent25767372008-07-23 21:30:12 -0700440 goto next;
441
442 if (qstr->len != len)
443 goto next;
444 if (memcmp(qstr->name, str, len))
445 goto next;
446
Al Viro4b1ae272010-03-03 12:58:31 -0500447 if (d_unhashed(active)) {
448 dget(active);
449 spin_unlock(&active->d_lock);
450 spin_unlock(&sbi->lookup_lock);
451 spin_unlock(&dcache_lock);
452 return active;
453 }
Ian Kent25767372008-07-23 21:30:12 -0700454next:
Ian Kente4d5ade2009-12-15 16:45:49 -0800455 spin_unlock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700456 }
457 spin_unlock(&sbi->lookup_lock);
458 spin_unlock(&dcache_lock);
459
460 return NULL;
461}
462
Ian Kent6510c9d2009-12-15 16:45:47 -0800463static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
Ian Kentf50b6f82007-02-20 13:58:10 -0800464{
Ian Kent6510c9d2009-12-15 16:45:47 -0800465 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
466 struct dentry *parent = dentry->d_parent;
467 struct qstr *name = &dentry->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800468 unsigned int len = name->len;
469 unsigned int hash = name->hash;
470 const unsigned char *str = name->name;
471 struct list_head *p, *head;
472
473 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700474 spin_lock(&sbi->lookup_lock);
475 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800476 list_for_each(p, head) {
477 struct autofs_info *ino;
Ian Kentcb4b4922009-12-15 16:45:50 -0800478 struct dentry *expiring;
Ian Kentf50b6f82007-02-20 13:58:10 -0800479 struct qstr *qstr;
480
Ian Kent5f6f4f22008-07-23 21:30:09 -0700481 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentcb4b4922009-12-15 16:45:50 -0800482 expiring = ino->dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800483
Ian Kentcb4b4922009-12-15 16:45:50 -0800484 spin_lock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800485
486 /* Bad luck, we've already been dentry_iput */
Ian Kentcb4b4922009-12-15 16:45:50 -0800487 if (!expiring->d_inode)
Ian Kentf50b6f82007-02-20 13:58:10 -0800488 goto next;
489
Ian Kentcb4b4922009-12-15 16:45:50 -0800490 qstr = &expiring->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800491
Ian Kentcb4b4922009-12-15 16:45:50 -0800492 if (expiring->d_name.hash != hash)
Ian Kentf50b6f82007-02-20 13:58:10 -0800493 goto next;
Ian Kentcb4b4922009-12-15 16:45:50 -0800494 if (expiring->d_parent != parent)
Ian Kentf50b6f82007-02-20 13:58:10 -0800495 goto next;
496
497 if (qstr->len != len)
498 goto next;
499 if (memcmp(qstr->name, str, len))
500 goto next;
501
Al Viro4b1ae272010-03-03 12:58:31 -0500502 if (d_unhashed(expiring)) {
503 dget(expiring);
504 spin_unlock(&expiring->d_lock);
505 spin_unlock(&sbi->lookup_lock);
506 spin_unlock(&dcache_lock);
507 return expiring;
508 }
Ian Kentf50b6f82007-02-20 13:58:10 -0800509next:
Ian Kentcb4b4922009-12-15 16:45:50 -0800510 spin_unlock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800511 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700512 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800513 spin_unlock(&dcache_lock);
514
515 return NULL;
516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/* Lookups in the root directory */
519static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
520{
521 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700522 struct autofs_info *ino;
Ian Kent90387c92009-12-15 16:45:46 -0800523 struct dentry *expiring, *active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int oz_mode;
525
526 DPRINTK("name = %.*s",
527 dentry->d_name.len, dentry->d_name.name);
528
Ian Kent718c6042006-03-27 01:14:42 -0800529 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800531 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700537 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Ian Kent6510c9d2009-12-15 16:45:47 -0800539 active = autofs4_lookup_active(dentry);
Ian Kent90387c92009-12-15 16:45:46 -0800540 if (active) {
541 dentry = active;
Ian Kentaa952eb2009-12-15 16:45:45 -0800542 ino = autofs4_dentry_ino(dentry);
543 } else {
Al Viro4b1ae272010-03-03 12:58:31 -0500544 /*
545 * Mark the dentry incomplete but don't hash it. We do this
546 * to serialize our inode creation operations (symlink and
547 * mkdir) which prevents deadlock during the callback to
548 * the daemon. Subsequent user space lookups for the same
549 * dentry are placed on the wait queue while the daemon
550 * itself is allowed passage unresticted so the create
551 * operation itself can then hash the dentry. Finally,
552 * we check for the hashed dentry and return the newly
553 * hashed dentry.
554 */
555 dentry->d_op = &autofs4_root_dentry_operations;
556
557 /*
558 * And we need to ensure that the same dentry is used for
559 * all following lookup calls until it is hashed so that
560 * the dentry flags are persistent throughout the request.
561 */
562 ino = autofs4_init_ino(NULL, sbi, 0555);
563 if (!ino)
564 return ERR_PTR(-ENOMEM);
565
566 dentry->d_fsdata = ino;
567 ino->dentry = dentry;
568
569 autofs4_add_active(dentry);
570
571 d_instantiate(dentry, NULL);
Ian Kent25767372008-07-23 21:30:12 -0700572 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (!oz_mode) {
Ian Kent213614d2009-12-15 16:45:51 -0800575 mutex_unlock(&dir->i_mutex);
Al Viro4b1ae272010-03-03 12:58:31 -0500576 expiring = autofs4_lookup_expiring(dentry);
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700577 if (expiring) {
578 /*
579 * If we are racing with expire the request might not
580 * be quite complete but the directory has been removed
581 * so it must have been successful, so just wait for it.
582 */
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700583 autofs4_expire_wait(expiring);
Al Viro4b1ae272010-03-03 12:58:31 -0500584 autofs4_del_expiring(expiring);
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700585 dput(expiring);
586 }
Al Viro4b1ae272010-03-03 12:58:31 -0500587
Ian Kent213614d2009-12-15 16:45:51 -0800588 spin_lock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500589 ino->flags |= AUTOFS_INF_PENDING;
Ian Kent213614d2009-12-15 16:45:51 -0800590 spin_unlock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500591 if (dentry->d_op && dentry->d_op->d_revalidate)
592 (dentry->d_op->d_revalidate)(dentry, nd);
593 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
596 /*
Al Viro4b1ae272010-03-03 12:58:31 -0500597 * If we are still pending, check if we had to handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * a signal. If so we can force a restart..
599 */
Al Viro4b1ae272010-03-03 12:58:31 -0500600 if (ino->flags & AUTOFS_INF_PENDING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* See if we were interrupted */
602 if (signal_pending(current)) {
603 sigset_t *sigset = &current->pending.signal;
604 if (sigismember (sigset, SIGKILL) ||
605 sigismember (sigset, SIGQUIT) ||
606 sigismember (sigset, SIGINT)) {
Ian Kent90387c92009-12-15 16:45:46 -0800607 if (active)
608 dput(active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return ERR_PTR(-ERESTARTNOINTR);
610 }
611 }
Al Viro4b1ae272010-03-03 12:58:31 -0500612 if (!oz_mode) {
613 spin_lock(&sbi->fs_lock);
614 ino->flags &= ~AUTOFS_INF_PENDING;
615 spin_unlock(&sbi->fs_lock);
Ian Kent25767372008-07-23 21:30:12 -0700616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
619 /*
Al Viro4b1ae272010-03-03 12:58:31 -0500620 * If this dentry is unhashed, then we shouldn't honour this
621 * lookup. Returning ENOENT here doesn't do the right thing
622 * for all system calls, but it should be OK for the operations
623 * we permit from an autofs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
Al Viro4b1ae272010-03-03 12:58:31 -0500625 if (!oz_mode && d_unhashed(dentry)) {
626 /*
627 * A user space application can (and has done in the past)
628 * remove and re-create this directory during the callback.
629 * This can leave us with an unhashed dentry, but a
630 * successful mount! So we need to perform another
631 * cached lookup in case the dentry now exists.
632 */
633 struct dentry *parent = dentry->d_parent;
634 struct dentry *new = d_lookup(parent, &dentry->d_name);
635 if (new != NULL)
636 dentry = new;
637 else
638 dentry = ERR_PTR(-ENOENT);
639
Ian Kent90387c92009-12-15 16:45:46 -0800640 if (active)
641 dput(active);
Al Viro4b1ae272010-03-03 12:58:31 -0500642
Ian Kentc9ffec42007-02-20 13:58:10 -0800643 return dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800644 }
645
Al Viro4b1ae272010-03-03 12:58:31 -0500646 if (active)
647 return active;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return NULL;
650}
651
652static int autofs4_dir_symlink(struct inode *dir,
653 struct dentry *dentry,
654 const char *symname)
655{
656 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
657 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800658 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 struct inode *inode;
660 char *cp;
661
662 DPRINTK("%s <- %.*s", symname,
663 dentry->d_name.len, dentry->d_name.name);
664
665 if (!autofs4_oz_mode(sbi))
666 return -EACCES;
667
668 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700669 if (!ino)
670 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Al Viro4b1ae272010-03-03 12:58:31 -0500672 autofs4_del_active(dentry);
673
Ian Kentef581a72008-07-23 21:30:13 -0700674 ino->size = strlen(symname);
Ian Kent25767372008-07-23 21:30:12 -0700675 cp = kmalloc(ino->size + 1, GFP_KERNEL);
676 if (!cp) {
677 if (!dentry->d_fsdata)
678 kfree(ino);
679 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
682 strcpy(cp, symname);
683
684 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700685 if (!inode) {
686 kfree(cp);
687 if (!dentry->d_fsdata)
688 kfree(ino);
689 return -ENOMEM;
690 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700691 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (dir == dir->i_sb->s_root->d_inode)
694 dentry->d_op = &autofs4_root_dentry_operations;
695 else
696 dentry->d_op = &autofs4_dentry_operations;
697
698 dentry->d_fsdata = ino;
699 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800700 atomic_inc(&ino->count);
701 p_ino = autofs4_dentry_ino(dentry->d_parent);
702 if (p_ino && dentry->d_parent != dentry)
703 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 ino->inode = inode;
705
Ian Kent25767372008-07-23 21:30:12 -0700706 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 dir->i_mtime = CURRENT_TIME;
708
709 return 0;
710}
711
712/*
713 * NOTE!
714 *
715 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
716 * that the file no longer exists. However, doing that means that the
717 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800718 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700719 * We simply d_drop it and add it to a expiring list in the super block,
720 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 *
722 * If a process is blocked on the dentry waiting for the expire to finish,
723 * it will invalidate the dentry and try to mount with a new one.
724 *
725 * Also see autofs4_dir_rmdir()..
726 */
727static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
728{
729 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
730 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800731 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700734 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return -EACCES;
736
Ian Kent1aff3c82006-03-27 01:14:46 -0800737 if (atomic_dec_and_test(&ino->count)) {
738 p_ino = autofs4_dentry_ino(dentry->d_parent);
739 if (p_ino && dentry->d_parent != dentry)
740 atomic_dec(&p_ino->count);
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dput(ino->dentry);
743
744 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700745 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 dir->i_mtime = CURRENT_TIME;
748
Ian Kentf50b6f82007-02-20 13:58:10 -0800749 spin_lock(&dcache_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500750 autofs4_add_expiring(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800751 spin_lock(&dentry->d_lock);
752 __d_drop(dentry);
753 spin_unlock(&dentry->d_lock);
754 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 return 0;
757}
758
759static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
760{
761 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
762 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800763 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Ian Kentf50b6f82007-02-20 13:58:10 -0800765 DPRINTK("dentry %p, removing %.*s",
766 dentry, dentry->d_name.len, dentry->d_name.name);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (!autofs4_oz_mode(sbi))
769 return -EACCES;
770
771 spin_lock(&dcache_lock);
772 if (!list_empty(&dentry->d_subdirs)) {
773 spin_unlock(&dcache_lock);
774 return -ENOTEMPTY;
775 }
Al Viro4b1ae272010-03-03 12:58:31 -0500776 autofs4_add_expiring(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 spin_lock(&dentry->d_lock);
778 __d_drop(dentry);
779 spin_unlock(&dentry->d_lock);
780 spin_unlock(&dcache_lock);
781
Ian Kent1aff3c82006-03-27 01:14:46 -0800782 if (atomic_dec_and_test(&ino->count)) {
783 p_ino = autofs4_dentry_ino(dentry->d_parent);
784 if (p_ino && dentry->d_parent != dentry)
785 atomic_dec(&p_ino->count);
786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700789 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700792 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 return 0;
795}
796
797static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
798{
799 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
800 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800801 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct inode *inode;
803
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700804 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return -EACCES;
806
807 DPRINTK("dentry %p, creating %.*s",
808 dentry, dentry->d_name.len, dentry->d_name.name);
809
810 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700811 if (!ino)
812 return -ENOMEM;
813
Al Viro4b1ae272010-03-03 12:58:31 -0500814 autofs4_del_active(dentry);
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700817 if (!inode) {
818 if (!dentry->d_fsdata)
819 kfree(ino);
820 return -ENOMEM;
821 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700822 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 if (dir == dir->i_sb->s_root->d_inode)
825 dentry->d_op = &autofs4_root_dentry_operations;
826 else
827 dentry->d_op = &autofs4_dentry_operations;
828
829 dentry->d_fsdata = ino;
830 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800831 atomic_inc(&ino->count);
832 p_ino = autofs4_dentry_ino(dentry->d_parent);
833 if (p_ino && dentry->d_parent != dentry)
834 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700836 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 dir->i_mtime = CURRENT_TIME;
838
839 return 0;
840}
841
842/* Get/set timeout ioctl() operation */
843static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
844 unsigned long __user *p)
845{
846 int rv;
847 unsigned long ntimeout;
848
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700849 if ((rv = get_user(ntimeout, p)) ||
850 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return rv;
852
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700853 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 sbi->exp_timeout = 0;
855 else
856 sbi->exp_timeout = ntimeout * HZ;
857
858 return 0;
859}
860
861/* Return protocol version */
862static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
863{
864 return put_user(sbi->version, p);
865}
866
867/* Return protocol sub version */
868static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
869{
870 return put_user(sbi->sub_version, p);
871}
872
873/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874* Tells the daemon whether it can umount the autofs mount.
875*/
876static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
877{
878 int status = 0;
879
Ian Kente3474a82006-03-27 01:14:51 -0800880 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 status = 1;
882
883 DPRINTK("returning %d", status);
884
885 status = put_user(status, p);
886
887 return status;
888}
889
890/* Identify autofs4_dentries - this is so we can tell if there's
891 an extra dentry refcount or not. We only hold a refcount on the
892 dentry if its non-negative (ie, d_inode != NULL)
893*/
894int is_autofs4_dentry(struct dentry *dentry)
895{
896 return dentry && dentry->d_inode &&
897 (dentry->d_op == &autofs4_root_dentry_operations ||
898 dentry->d_op == &autofs4_dentry_operations) &&
899 dentry->d_fsdata != NULL;
900}
901
902/*
903 * ioctl()'s on the root directory is the chief method for the daemon to
904 * generate kernel reactions
905 */
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200906static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
907 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
910 void __user *p = (void __user *)arg;
911
912 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700913 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700915 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
916 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return -ENOTTY;
918
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700919 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return -EPERM;
921
922 switch(cmd) {
923 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
924 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
925 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
926 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
927 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
928 autofs4_catatonic_mode(sbi);
929 return 0;
930 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
931 return autofs4_get_protover(sbi, p);
932 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
933 return autofs4_get_protosubver(sbi, p);
934 case AUTOFS_IOC_SETTIMEOUT:
935 return autofs4_get_set_timeout(sbi, p);
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800938 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 /* return a single thing to expire */
941 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800942 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* same as above, but can send multiple expires through pipe */
944 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800945 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 default:
948 return -ENOSYS;
949 }
950}
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200951
952static long autofs4_root_ioctl(struct file *filp,
953 unsigned int cmd, unsigned long arg)
954{
955 long ret;
956 struct inode *inode = filp->f_dentry->d_inode;
957
958 lock_kernel();
959 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
960 unlock_kernel();
961
962 return ret;
963}