blob: f1076b91a0faea7428c8e07b2ee9ee242fa291db [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>
Arnd Bergmannc9243f52010-07-04 00:15:07 +020021#include <linux/compat.h>
Arnd Bergmann00e300e2010-09-14 23:00:34 +020022#include <linux/mutex.h>
Arnd Bergmannc9243f52010-07-04 00:15:07 +020023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "autofs_i.h"
25
Nick Pigginb5c84bf2011-01-07 17:49:38 +110026DEFINE_SPINLOCK(autofs4_lock);
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
29static int autofs4_dir_unlink(struct inode *,struct dentry *);
30static int autofs4_dir_rmdir(struct inode *,struct dentry *);
31static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
Frederic Weisbecker3663df72010-05-19 15:08:17 +020032static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
Felipe Contreras5a44a732010-09-24 15:22:23 +020033#ifdef CONFIG_COMPAT
Arnd Bergmannc9243f52010-07-04 00:15:07 +020034static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
Felipe Contreras5a44a732010-09-24 15:22:23 +020035#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int autofs4_dir_open(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Ian Kent6d5cb922008-07-23 21:30:15 -070039#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
40#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
41
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080042const struct file_operations autofs4_root_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .open = dcache_dir_open,
44 .release = dcache_dir_close,
45 .read = generic_read_dir,
Ian Kentaa55ddf2008-07-23 21:30:29 -070046 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040047 .llseek = dcache_dir_lseek,
Frederic Weisbecker3663df72010-05-19 15:08:17 +020048 .unlocked_ioctl = autofs4_root_ioctl,
Arnd Bergmannc9243f52010-07-04 00:15:07 +020049#ifdef CONFIG_COMPAT
50 .compat_ioctl = autofs4_root_compat_ioctl,
51#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080054const struct file_operations autofs4_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 .open = autofs4_dir_open,
Ian Kentff9cd492008-07-23 21:30:24 -070056 .release = dcache_dir_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .read = generic_read_dir,
Ian Kentff9cd492008-07-23 21:30:24 -070058 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040059 .llseek = dcache_dir_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Arjan van de Ven754661f2007-02-12 00:55:38 -080062const struct inode_operations autofs4_indirect_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 .lookup = autofs4_lookup,
64 .unlink = autofs4_dir_unlink,
65 .symlink = autofs4_dir_symlink,
66 .mkdir = autofs4_dir_mkdir,
67 .rmdir = autofs4_dir_rmdir,
68};
69
Arjan van de Ven754661f2007-02-12 00:55:38 -080070const struct inode_operations autofs4_direct_root_inode_operations = {
Ian Kent34ca9592006-03-27 01:14:54 -080071 .lookup = autofs4_lookup,
Ian Kent871f9432006-03-27 01:14:58 -080072 .unlink = autofs4_dir_unlink,
73 .mkdir = autofs4_dir_mkdir,
74 .rmdir = autofs4_dir_rmdir,
Ian Kent34ca9592006-03-27 01:14:54 -080075};
76
Arjan van de Ven754661f2007-02-12 00:55:38 -080077const struct inode_operations autofs4_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .lookup = autofs4_lookup,
79 .unlink = autofs4_dir_unlink,
80 .symlink = autofs4_dir_symlink,
81 .mkdir = autofs4_dir_mkdir,
82 .rmdir = autofs4_dir_rmdir,
83};
84
Ian Kent4f8427d2009-12-15 16:45:42 -080085static void autofs4_add_active(struct dentry *dentry)
86{
87 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
88 struct autofs_info *ino = autofs4_dentry_ino(dentry);
89 if (ino) {
90 spin_lock(&sbi->lookup_lock);
91 if (!ino->active_count) {
92 if (list_empty(&ino->active))
93 list_add(&ino->active, &sbi->active_list);
94 }
95 ino->active_count++;
96 spin_unlock(&sbi->lookup_lock);
97 }
98 return;
99}
100
101static void autofs4_del_active(struct dentry *dentry)
102{
103 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
104 struct autofs_info *ino = autofs4_dentry_ino(dentry);
105 if (ino) {
106 spin_lock(&sbi->lookup_lock);
107 ino->active_count--;
108 if (!ino->active_count) {
109 if (!list_empty(&ino->active))
110 list_del_init(&ino->active);
111 }
112 spin_unlock(&sbi->lookup_lock);
113 }
114 return;
115}
116
Ian Kent36b64132009-12-15 16:45:44 -0800117static unsigned int autofs4_need_mount(unsigned int flags)
118{
119 unsigned int res = 0;
120 if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
121 res = 1;
122 return res;
123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static int autofs4_dir_open(struct inode *inode, struct file *file)
126{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800127 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 DPRINTK("file=%p dentry=%p %.*s",
131 file, dentry, dentry->d_name.len, dentry->d_name.name);
132
133 if (autofs4_oz_mode(sbi))
134 goto out;
135
Ian Kentff9cd492008-07-23 21:30:24 -0700136 /*
137 * An empty directory in an autofs file system is always a
138 * mount point. The daemon must have failed to mount this
139 * during lookup so it doesn't exist. This can happen, for
140 * example, if user space returns an incorrect status for a
141 * mount request. Otherwise we're doing a readdir on the
142 * autofs file system so just let the libfs routines handle
143 * it.
144 */
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100145 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100146 spin_lock(&dentry->d_lock);
Ian Kentc42c7f72009-12-15 16:45:48 -0800147 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100148 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100149 spin_unlock(&autofs4_lock);
Ian Kentff9cd492008-07-23 21:30:24 -0700150 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100152 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100153 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Ian Kentf360ce32006-03-27 01:14:43 -0800155out:
Ian Kentff9cd492008-07-23 21:30:24 -0700156 return dcache_dir_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Al Viro4b1ae272010-03-03 12:58:31 -0500159static int try_to_fill_dentry(struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Ian Kent862b1102006-03-27 01:14:48 -0800161 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800162 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700163 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 DPRINTK("dentry=%p %.*s ino=%p",
166 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
167
Ian Kent718c6042006-03-27 01:14:42 -0800168 /*
169 * Wait for a pending mount, triggering one if there
170 * isn't one already
171 */
Al Viro4b1ae272010-03-03 12:58:31 -0500172 if (dentry->d_inode == NULL) {
173 DPRINTK("waiting for mount name=%.*s",
174 dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Al Viro4b1ae272010-03-03 12:58:31 -0500176 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800177
Al Viro4b1ae272010-03-03 12:58:31 -0500178 DPRINTK("mount done status=%d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Al Viro4b1ae272010-03-03 12:58:31 -0500180 /* Turn this into a real negative dentry? */
181 if (status == -ENOENT) {
182 spin_lock(&sbi->fs_lock);
183 ino->flags &= ~AUTOFS_INF_PENDING;
184 spin_unlock(&sbi->fs_lock);
185 return status;
186 } else if (status) {
187 /* Return a negative dentry, but leave it "pending" */
188 return status;
189 }
190 /* Trigger mount for path component or follow link */
191 } else if (ino->flags & AUTOFS_INF_PENDING ||
Ian Kentf7422462010-05-10 16:46:08 +0800192 autofs4_need_mount(flags)) {
Al Viro4b1ae272010-03-03 12:58:31 -0500193 DPRINTK("waiting for mount name=%.*s",
194 dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Al Viro4b1ae272010-03-03 12:58:31 -0500196 spin_lock(&sbi->fs_lock);
197 ino->flags |= AUTOFS_INF_PENDING;
198 spin_unlock(&sbi->fs_lock);
199 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
200
201 DPRINTK("mount done status=%d", status);
202
203 if (status) {
204 spin_lock(&sbi->fs_lock);
205 ino->flags &= ~AUTOFS_INF_PENDING;
206 spin_unlock(&sbi->fs_lock);
207 return status;
208 }
209 }
210
211 /* Initialize expiry counter after successful mount */
Dan Carpenter5fc79d82010-08-10 18:02:04 -0700212 ino->last_used = jiffies;
Al Viro4b1ae272010-03-03 12:58:31 -0500213
214 spin_lock(&sbi->fs_lock);
215 ino->flags &= ~AUTOFS_INF_PENDING;
216 spin_unlock(&sbi->fs_lock);
217
218 return 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800219}
220
221/* For autofs direct mounts the follow link triggers the mount */
222static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
223{
224 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kenta5370552006-05-15 09:43:51 -0700225 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent34ca9592006-03-27 01:14:54 -0800226 int oz_mode = autofs4_oz_mode(sbi);
227 unsigned int lookup_type;
228 int status;
229
230 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
231 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
232 nd->flags);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700233 /*
234 * For an expire of a covered direct or offset mount we need
David Howellscc53ce52011-01-14 18:45:26 +0000235 * to break out of follow_down_one() at the autofs mount trigger
Ian Kent6e60a9a2008-07-23 21:30:27 -0700236 * (d_mounted--), so we can see the expiring flag, and manage
237 * the blocking and following here until the expire is completed.
238 */
239 if (oz_mode) {
240 spin_lock(&sbi->fs_lock);
241 if (ino->flags & AUTOFS_INF_EXPIRING) {
242 spin_unlock(&sbi->fs_lock);
243 /* Follow down to our covering mount. */
David Howellscc53ce52011-01-14 18:45:26 +0000244 if (!follow_down_one(&nd->path))
Ian Kent6e60a9a2008-07-23 21:30:27 -0700245 goto done;
Ian Kentec6e8c72008-07-23 21:30:28 -0700246 goto follow;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700247 }
248 spin_unlock(&sbi->fs_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800249 goto done;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700250 }
Ian Kent34ca9592006-03-27 01:14:54 -0800251
Ian Kent6e60a9a2008-07-23 21:30:27 -0700252 /* If an expire request is pending everyone must wait. */
Ian Kent06a35982008-07-23 21:30:28 -0700253 autofs4_expire_wait(dentry);
Ian Kent97e74492008-07-23 21:30:26 -0700254
Ian Kent6e60a9a2008-07-23 21:30:27 -0700255 /* We trigger a mount for almost all flags */
Ian Kent36b64132009-12-15 16:45:44 -0800256 lookup_type = autofs4_need_mount(nd->flags);
Ian Kentaa952eb2009-12-15 16:45:45 -0800257 spin_lock(&sbi->fs_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100258 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100259 spin_lock(&dentry->d_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800260 if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100261 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100262 spin_unlock(&autofs4_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800263 spin_unlock(&sbi->fs_lock);
Ian Kentec6e8c72008-07-23 21:30:28 -0700264 goto follow;
Ian Kentaa952eb2009-12-15 16:45:45 -0800265 }
Ian Kent6e60a9a2008-07-23 21:30:27 -0700266
Ian Kent871f9432006-03-27 01:14:58 -0800267 /*
Ian Kent6e60a9a2008-07-23 21:30:27 -0700268 * If the dentry contains directories then it is an autofs
269 * multi-mount with no root mount offset. So don't try to
270 * mount it again.
Ian Kent871f9432006-03-27 01:14:58 -0800271 */
Ian Kentaa952eb2009-12-15 16:45:45 -0800272 if (ino->flags & AUTOFS_INF_PENDING ||
Ian Kentc42c7f72009-12-15 16:45:48 -0800273 (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100274 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100275 spin_unlock(&autofs4_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800276 spin_unlock(&sbi->fs_lock);
Ian Kent871f9432006-03-27 01:14:58 -0800277
Ian Kentf7422462010-05-10 16:46:08 +0800278 status = try_to_fill_dentry(dentry, nd->flags);
Ian Kent871f9432006-03-27 01:14:58 -0800279 if (status)
280 goto out_error;
281
Ian Kent6e60a9a2008-07-23 21:30:27 -0700282 goto follow;
Ian Kent871f9432006-03-27 01:14:58 -0800283 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100284 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100285 spin_unlock(&autofs4_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800286 spin_unlock(&sbi->fs_lock);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700287follow:
288 /*
289 * If there is no root mount it must be an autofs
290 * multi-mount with no root offset so we don't need
291 * to follow it.
292 */
David Howellscc53ce52011-01-14 18:45:26 +0000293 if (d_managed(dentry)) {
294 status = follow_down(&nd->path, false);
295 if (status < 0)
Ian Kent6e60a9a2008-07-23 21:30:27 -0700296 goto out_error;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700297 }
Ian Kent871f9432006-03-27 01:14:58 -0800298
Ian Kent34ca9592006-03-27 01:14:54 -0800299done:
300 return NULL;
301
302out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800303 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800304 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307/*
308 * Revalidate is called on every cache lookup. Some of those
309 * cache lookups may actually happen while the dentry is not
310 * yet completely filled in, and revalidate has to delay such
311 * lookups..
312 */
Ian Kent718c6042006-03-27 01:14:42 -0800313static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Nick Piggin34286d62011-01-07 17:49:57 +1100315 struct inode *dir;
316 struct autofs_sb_info *sbi;
317 int oz_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 int flags = nd ? nd->flags : 0;
Al Viro4b1ae272010-03-03 12:58:31 -0500319 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Nick Piggin34286d62011-01-07 17:49:57 +1100321 if (flags & LOOKUP_RCU)
322 return -ECHILD;
323
324 dir = dentry->d_parent->d_inode;
325 sbi = autofs4_sbi(dir->i_sb);
326 oz_mode = autofs4_oz_mode(sbi);
327
Ian Kent213614d2009-12-15 16:45:51 -0800328 /* Pending dentry */
Al Viro4b1ae272010-03-03 12:58:31 -0500329 spin_lock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (autofs4_ispending(dentry)) {
Al Viro4b1ae272010-03-03 12:58:31 -0500331 /* The daemon never causes a mount to trigger */
Ian Kent213614d2009-12-15 16:45:51 -0800332 spin_unlock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500333
334 if (oz_mode)
335 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700336
337 /*
Ian Kent06a35982008-07-23 21:30:28 -0700338 * If the directory has gone away due to an expire
339 * we have been called as ->d_revalidate() and so
340 * we need to return false and proceed to ->lookup().
341 */
342 if (autofs4_expire_wait(dentry) == -EAGAIN)
343 return 0;
344
345 /*
Ian Kentbcdc5e02006-09-27 01:50:44 -0700346 * A zero status is success otherwise we have a
347 * negative error code.
348 */
Al Viro4b1ae272010-03-03 12:58:31 -0500349 status = try_to_fill_dentry(dentry, flags);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700350 if (status == 0)
Ian Kentf50b6f82007-02-20 13:58:10 -0800351 return 1;
352
Ian Kentbcdc5e02006-09-27 01:50:44 -0700353 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Al Viro4b1ae272010-03-03 12:58:31 -0500355 spin_unlock(&sbi->fs_lock);
356
357 /* Negative dentry.. invalidate if "old" */
358 if (dentry->d_inode == NULL)
359 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* Check for a non-mountpoint directory with no contents */
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100362 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100363 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (S_ISDIR(dentry->d_inode->i_mode) &&
Ian Kentc42c7f72009-12-15 16:45:48 -0800365 !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 DPRINTK("dentry=%p %.*s, emptydir",
367 dentry, dentry->d_name.len, dentry->d_name.name);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100368 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100369 spin_unlock(&autofs4_lock);
Ian Kent97e74492008-07-23 21:30:26 -0700370
Al Viro4b1ae272010-03-03 12:58:31 -0500371 /* The daemon never causes a mount to trigger */
372 if (oz_mode)
373 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700374
Al Viro4b1ae272010-03-03 12:58:31 -0500375 /*
376 * A zero status is success otherwise we have a
377 * negative error code.
378 */
379 status = try_to_fill_dentry(dentry, flags);
380 if (status == 0)
381 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700382
Al Viro4b1ae272010-03-03 12:58:31 -0500383 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100385 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100386 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 1;
389}
390
Ian Kent34ca9592006-03-27 01:14:54 -0800391void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct autofs_info *inf;
394
395 DPRINTK("releasing %p", de);
396
397 inf = autofs4_dentry_ino(de);
398 de->d_fsdata = NULL;
399
400 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800401 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
402
Ian Kentf50b6f82007-02-20 13:58:10 -0800403 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700404 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700405 if (!list_empty(&inf->active))
406 list_del(&inf->active);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700407 if (!list_empty(&inf->expiring))
408 list_del(&inf->expiring);
409 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800410 }
411
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700412 inf->dentry = NULL;
413 inf->inode = NULL;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 autofs4_free_ino(inf);
416 }
417}
418
419/* For dentries of directories in the root dir */
Al Viro08f11512009-02-20 05:56:19 +0000420static const struct dentry_operations autofs4_root_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 .d_release = autofs4_dentry_release,
422};
423
424/* For other dentries */
Al Viro08f11512009-02-20 05:56:19 +0000425static const struct dentry_operations autofs4_dentry_operations = {
Ian Kent10584212011-01-14 18:45:58 +0000426 .d_automount = autofs4_d_automount,
Ian Kentb5b80172011-01-14 18:46:03 +0000427 .d_manage = autofs4_d_manage,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 .d_release = autofs4_dentry_release,
429};
430
Ian Kent6510c9d2009-12-15 16:45:47 -0800431static struct dentry *autofs4_lookup_active(struct dentry *dentry)
Ian Kent25767372008-07-23 21:30:12 -0700432{
Ian Kent6510c9d2009-12-15 16:45:47 -0800433 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
434 struct dentry *parent = dentry->d_parent;
435 struct qstr *name = &dentry->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700436 unsigned int len = name->len;
437 unsigned int hash = name->hash;
438 const unsigned char *str = name->name;
439 struct list_head *p, *head;
440
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100441 spin_lock(&autofs4_lock);
Ian Kent25767372008-07-23 21:30:12 -0700442 spin_lock(&sbi->lookup_lock);
443 head = &sbi->active_list;
444 list_for_each(p, head) {
445 struct autofs_info *ino;
Ian Kente4d5ade2009-12-15 16:45:49 -0800446 struct dentry *active;
Ian Kent25767372008-07-23 21:30:12 -0700447 struct qstr *qstr;
448
449 ino = list_entry(p, struct autofs_info, active);
Ian Kente4d5ade2009-12-15 16:45:49 -0800450 active = ino->dentry;
Ian Kent25767372008-07-23 21:30:12 -0700451
Ian Kente4d5ade2009-12-15 16:45:49 -0800452 spin_lock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700453
454 /* Already gone? */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100455 if (active->d_count == 0)
Ian Kent25767372008-07-23 21:30:12 -0700456 goto next;
457
Ian Kente4d5ade2009-12-15 16:45:49 -0800458 qstr = &active->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700459
Ian Kente4d5ade2009-12-15 16:45:49 -0800460 if (active->d_name.hash != hash)
Ian Kent25767372008-07-23 21:30:12 -0700461 goto next;
Ian Kente4d5ade2009-12-15 16:45:49 -0800462 if (active->d_parent != parent)
Ian Kent25767372008-07-23 21:30:12 -0700463 goto next;
464
465 if (qstr->len != len)
466 goto next;
467 if (memcmp(qstr->name, str, len))
468 goto next;
469
Al Viro4b1ae272010-03-03 12:58:31 -0500470 if (d_unhashed(active)) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100471 dget_dlock(active);
Al Viro4b1ae272010-03-03 12:58:31 -0500472 spin_unlock(&active->d_lock);
473 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100474 spin_unlock(&autofs4_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500475 return active;
476 }
Ian Kent25767372008-07-23 21:30:12 -0700477next:
Ian Kente4d5ade2009-12-15 16:45:49 -0800478 spin_unlock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700479 }
480 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100481 spin_unlock(&autofs4_lock);
Ian Kent25767372008-07-23 21:30:12 -0700482
483 return NULL;
484}
485
Ian Kent6510c9d2009-12-15 16:45:47 -0800486static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
Ian Kentf50b6f82007-02-20 13:58:10 -0800487{
Ian Kent6510c9d2009-12-15 16:45:47 -0800488 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
489 struct dentry *parent = dentry->d_parent;
490 struct qstr *name = &dentry->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800491 unsigned int len = name->len;
492 unsigned int hash = name->hash;
493 const unsigned char *str = name->name;
494 struct list_head *p, *head;
495
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100496 spin_lock(&autofs4_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700497 spin_lock(&sbi->lookup_lock);
498 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800499 list_for_each(p, head) {
500 struct autofs_info *ino;
Ian Kentcb4b4922009-12-15 16:45:50 -0800501 struct dentry *expiring;
Ian Kentf50b6f82007-02-20 13:58:10 -0800502 struct qstr *qstr;
503
Ian Kent5f6f4f22008-07-23 21:30:09 -0700504 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentcb4b4922009-12-15 16:45:50 -0800505 expiring = ino->dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800506
Ian Kentcb4b4922009-12-15 16:45:50 -0800507 spin_lock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800508
509 /* Bad luck, we've already been dentry_iput */
Ian Kentcb4b4922009-12-15 16:45:50 -0800510 if (!expiring->d_inode)
Ian Kentf50b6f82007-02-20 13:58:10 -0800511 goto next;
512
Ian Kentcb4b4922009-12-15 16:45:50 -0800513 qstr = &expiring->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800514
Ian Kentcb4b4922009-12-15 16:45:50 -0800515 if (expiring->d_name.hash != hash)
Ian Kentf50b6f82007-02-20 13:58:10 -0800516 goto next;
Ian Kentcb4b4922009-12-15 16:45:50 -0800517 if (expiring->d_parent != parent)
Ian Kentf50b6f82007-02-20 13:58:10 -0800518 goto next;
519
520 if (qstr->len != len)
521 goto next;
522 if (memcmp(qstr->name, str, len))
523 goto next;
524
Al Viro4b1ae272010-03-03 12:58:31 -0500525 if (d_unhashed(expiring)) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100526 dget_dlock(expiring);
Al Viro4b1ae272010-03-03 12:58:31 -0500527 spin_unlock(&expiring->d_lock);
528 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100529 spin_unlock(&autofs4_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500530 return expiring;
531 }
Ian Kentf50b6f82007-02-20 13:58:10 -0800532next:
Ian Kentcb4b4922009-12-15 16:45:50 -0800533 spin_unlock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800534 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700535 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100536 spin_unlock(&autofs4_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800537
538 return NULL;
539}
540
Ian Kent10584212011-01-14 18:45:58 +0000541static int autofs4_mount_wait(struct dentry *dentry)
542{
543 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
544 struct autofs_info *ino = autofs4_dentry_ino(dentry);
545 int status;
546
547 if (ino->flags & AUTOFS_INF_PENDING) {
548 DPRINTK("waiting for mount name=%.*s",
549 dentry->d_name.len, dentry->d_name.name);
550 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
551 DPRINTK("mount wait done status=%d", status);
552 ino->last_used = jiffies;
553 return status;
554 }
555 return 0;
556}
557
558static int do_expire_wait(struct dentry *dentry)
559{
560 struct dentry *expiring;
561
562 expiring = autofs4_lookup_expiring(dentry);
563 if (!expiring)
564 return autofs4_expire_wait(dentry);
565 else {
566 /*
567 * If we are racing with expire the request might not
568 * be quite complete, but the directory has been removed
569 * so it must have been successful, just wait for it.
570 */
571 autofs4_expire_wait(expiring);
572 autofs4_del_expiring(expiring);
573 dput(expiring);
574 }
575 return 0;
576}
577
578static struct dentry *autofs4_mountpoint_changed(struct path *path)
579{
580 struct dentry *dentry = path->dentry;
581 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
582
583 /*
584 * If this is an indirect mount the dentry could have gone away
585 * as a result of an expire and a new one created.
586 */
587 if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
588 struct dentry *parent = dentry->d_parent;
589 struct dentry *new = d_lookup(parent, &dentry->d_name);
590 if (!new)
591 return NULL;
592 dput(path->dentry);
593 path->dentry = new;
594 }
595 return path->dentry;
596}
597
598struct vfsmount *autofs4_d_automount(struct path *path)
599{
600 struct dentry *dentry = path->dentry;
601 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
602 struct autofs_info *ino = autofs4_dentry_ino(dentry);
603 int status;
604
605 DPRINTK("dentry=%p %.*s",
606 dentry, dentry->d_name.len, dentry->d_name.name);
607
Ian Kentb5b80172011-01-14 18:46:03 +0000608 /*
609 * Someone may have manually umounted this or it was a submount
610 * that has gone away.
611 */
612 spin_lock(&dentry->d_lock);
613 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
614 if (!(dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
615 (dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
616 __managed_dentry_set_transit(path->dentry);
617 }
618 spin_unlock(&dentry->d_lock);
619
Ian Kent10584212011-01-14 18:45:58 +0000620 /* The daemon never triggers a mount. */
621 if (autofs4_oz_mode(sbi))
622 return NULL;
623
624 /*
625 * If an expire request is pending everyone must wait.
626 * If the expire fails we're still mounted so continue
627 * the follow and return. A return of -EAGAIN (which only
628 * happens with indirect mounts) means the expire completed
629 * and the directory was removed, so just go ahead and try
630 * the mount.
631 */
632 status = do_expire_wait(dentry);
633 if (status && status != -EAGAIN)
634 return NULL;
635
636 /* Callback to the daemon to perform the mount or wait */
637 spin_lock(&sbi->fs_lock);
638 if (ino->flags & AUTOFS_INF_PENDING) {
639 spin_unlock(&sbi->fs_lock);
640 status = autofs4_mount_wait(dentry);
641 if (status)
642 return ERR_PTR(status);
643 spin_lock(&sbi->fs_lock);
644 goto done;
645 }
646
647 /*
648 * If the dentry is a symlink it's equivalent to a directory
Ian Kentb5b80172011-01-14 18:46:03 +0000649 * having d_mountpoint() true, so there's no need to call back
Ian Kent10584212011-01-14 18:45:58 +0000650 * to the daemon.
651 */
652 if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))
653 goto done;
Ian Kentb5b80172011-01-14 18:46:03 +0000654 if (!d_mountpoint(dentry)) {
655 /*
656 * It's possible that user space hasn't removed directories
657 * after umounting a rootless multi-mount, although it
658 * should. For v5 have_submounts() is sufficient to handle
659 * this because the leaves of the directory tree under the
660 * mount never trigger mounts themselves (they have an autofs
661 * trigger mount mounted on them). But v4 pseudo direct mounts
662 * do need the leaves to to trigger mounts. In this case we
663 * have no choice but to use the list_empty() check and
664 * require user space behave.
665 */
666 if (sbi->version > 4) {
667 if (have_submounts(dentry))
668 goto done;
669 } else {
670 spin_lock(&dentry->d_lock);
671 if (!list_empty(&dentry->d_subdirs)) {
672 spin_unlock(&dentry->d_lock);
673 goto done;
674 }
675 spin_unlock(&dentry->d_lock);
676 }
Ian Kent10584212011-01-14 18:45:58 +0000677 ino->flags |= AUTOFS_INF_PENDING;
Ian Kent10584212011-01-14 18:45:58 +0000678 spin_unlock(&sbi->fs_lock);
679 status = autofs4_mount_wait(dentry);
680 if (status)
681 return ERR_PTR(status);
682 spin_lock(&sbi->fs_lock);
683 ino->flags &= ~AUTOFS_INF_PENDING;
Ian Kent10584212011-01-14 18:45:58 +0000684 }
Ian Kent10584212011-01-14 18:45:58 +0000685done:
Ian Kentb5b80172011-01-14 18:46:03 +0000686 if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
687 /*
688 * Any needed mounting has been completed and the path updated
689 * so turn this into a normal dentry so we don't continually
690 * call ->d_automount() and ->d_manage().
691 */
692 spin_lock(&dentry->d_lock);
693 __managed_dentry_clear_transit(dentry);
694 /*
695 * Only clear DMANAGED_AUTOMOUNT for rootless multi-mounts and
696 * symlinks as in all other cases the dentry will be covered by
697 * an actual mount so ->d_automount() won't be called during
698 * the follow.
699 */
700 if ((!d_mountpoint(dentry) &&
701 !list_empty(&dentry->d_subdirs)) ||
702 (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)))
703 __managed_dentry_clear_automount(dentry);
704 spin_unlock(&dentry->d_lock);
705 }
Ian Kent10584212011-01-14 18:45:58 +0000706 spin_unlock(&sbi->fs_lock);
707
708 /* Mount succeeded, check if we ended up with a new dentry */
709 dentry = autofs4_mountpoint_changed(path);
710 if (!dentry)
711 return ERR_PTR(-ENOENT);
712
713 return NULL;
714}
715
Ian Kentb5b80172011-01-14 18:46:03 +0000716int autofs4_d_manage(struct dentry *dentry, bool mounting_here)
717{
718 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
719
720 DPRINTK("dentry=%p %.*s",
721 dentry, dentry->d_name.len, dentry->d_name.name);
722
723 /* The daemon never waits. */
724 if (autofs4_oz_mode(sbi) || mounting_here) {
725 if (!d_mountpoint(dentry))
726 return -EISDIR;
727 return 0;
728 }
729
730 /* Wait for pending expires */
731 do_expire_wait(dentry);
732
733 /*
734 * This dentry may be under construction so wait on mount
735 * completion.
736 */
737 return autofs4_mount_wait(dentry);
738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740/* Lookups in the root directory */
741static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
742{
743 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700744 struct autofs_info *ino;
Ian Kent10584212011-01-14 18:45:58 +0000745 struct dentry *active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Ian Kent10584212011-01-14 18:45:58 +0000747 DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Ian Kent718c6042006-03-27 01:14:42 -0800749 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800751 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 sbi = autofs4_sbi(dir->i_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Ian Kent10584212011-01-14 18:45:58 +0000756 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Ian Kent6510c9d2009-12-15 16:45:47 -0800758 active = autofs4_lookup_active(dentry);
Ian Kent90387c92009-12-15 16:45:46 -0800759 if (active) {
Ian Kent10584212011-01-14 18:45:58 +0000760 return active;
Ian Kentaa952eb2009-12-15 16:45:45 -0800761 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100762 d_set_d_op(dentry, &autofs4_root_dentry_operations);
Al Viro4b1ae272010-03-03 12:58:31 -0500763
764 /*
Ian Kent10584212011-01-14 18:45:58 +0000765 * A dentry that is not within the root can never trigger a
766 * mount operation, unless the directory already exists, so we
767 * can return fail immediately. The daemon however does need
768 * to create directories within the file system.
Al Viro4b1ae272010-03-03 12:58:31 -0500769 */
Ian Kent10584212011-01-14 18:45:58 +0000770 if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
771 return ERR_PTR(-ENOENT);
772
773 /* Mark entries in the root as mount triggers */
774 if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent)) {
775 d_set_d_op(dentry, &autofs4_dentry_operations);
Ian Kentb5b80172011-01-14 18:46:03 +0000776 __managed_dentry_set_managed(dentry);
Ian Kent10584212011-01-14 18:45:58 +0000777 }
778
Al Viro4b1ae272010-03-03 12:58:31 -0500779 ino = autofs4_init_ino(NULL, sbi, 0555);
780 if (!ino)
781 return ERR_PTR(-ENOMEM);
782
783 dentry->d_fsdata = ino;
784 ino->dentry = dentry;
785
786 autofs4_add_active(dentry);
787
788 d_instantiate(dentry, NULL);
Ian Kent25767372008-07-23 21:30:12 -0700789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 return NULL;
791}
792
793static int autofs4_dir_symlink(struct inode *dir,
794 struct dentry *dentry,
795 const char *symname)
796{
797 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
798 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800799 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct inode *inode;
801 char *cp;
802
803 DPRINTK("%s <- %.*s", symname,
804 dentry->d_name.len, dentry->d_name.name);
805
806 if (!autofs4_oz_mode(sbi))
807 return -EACCES;
808
809 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700810 if (!ino)
811 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Al Viro4b1ae272010-03-03 12:58:31 -0500813 autofs4_del_active(dentry);
814
Ian Kentef581a72008-07-23 21:30:13 -0700815 ino->size = strlen(symname);
Ian Kent25767372008-07-23 21:30:12 -0700816 cp = kmalloc(ino->size + 1, GFP_KERNEL);
817 if (!cp) {
818 if (!dentry->d_fsdata)
819 kfree(ino);
820 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
823 strcpy(cp, symname);
824
825 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700826 if (!inode) {
827 kfree(cp);
828 if (!dentry->d_fsdata)
829 kfree(ino);
830 return -ENOMEM;
831 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700832 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 dentry->d_fsdata = ino;
835 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800836 atomic_inc(&ino->count);
837 p_ino = autofs4_dentry_ino(dentry->d_parent);
838 if (p_ino && dentry->d_parent != dentry)
839 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ino->inode = inode;
841
Ian Kent25767372008-07-23 21:30:12 -0700842 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 dir->i_mtime = CURRENT_TIME;
844
845 return 0;
846}
847
848/*
849 * NOTE!
850 *
851 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
852 * that the file no longer exists. However, doing that means that the
853 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800854 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700855 * We simply d_drop it and add it to a expiring list in the super block,
856 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 *
858 * If a process is blocked on the dentry waiting for the expire to finish,
859 * it will invalidate the dentry and try to mount with a new one.
860 *
861 * Also see autofs4_dir_rmdir()..
862 */
863static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
864{
865 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
866 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800867 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700870 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return -EACCES;
872
Ian Kent1aff3c82006-03-27 01:14:46 -0800873 if (atomic_dec_and_test(&ino->count)) {
874 p_ino = autofs4_dentry_ino(dentry->d_parent);
875 if (p_ino && dentry->d_parent != dentry)
876 atomic_dec(&p_ino->count);
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 dput(ino->dentry);
879
880 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700881 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 dir->i_mtime = CURRENT_TIME;
884
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100885 spin_lock(&autofs4_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500886 autofs4_add_expiring(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800887 spin_lock(&dentry->d_lock);
888 __d_drop(dentry);
889 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100890 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 return 0;
893}
894
895static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
896{
897 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
898 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800899 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Ian Kentf50b6f82007-02-20 13:58:10 -0800901 DPRINTK("dentry %p, removing %.*s",
902 dentry, dentry->d_name.len, dentry->d_name.name);
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (!autofs4_oz_mode(sbi))
905 return -EACCES;
906
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100907 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100908 spin_lock(&sbi->lookup_lock);
909 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100911 spin_unlock(&dentry->d_lock);
912 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100913 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return -ENOTEMPTY;
915 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100916 __autofs4_add_expiring(dentry);
917 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 __d_drop(dentry);
919 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100920 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Ian Kent1aff3c82006-03-27 01:14:46 -0800922 if (atomic_dec_and_test(&ino->count)) {
923 p_ino = autofs4_dentry_ino(dentry->d_parent);
924 if (p_ino && dentry->d_parent != dentry)
925 atomic_dec(&p_ino->count);
926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700929 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700932 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 return 0;
935}
936
937static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
938{
939 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
940 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800941 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct inode *inode;
943
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700944 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -EACCES;
946
947 DPRINTK("dentry %p, creating %.*s",
948 dentry, dentry->d_name.len, dentry->d_name.name);
949
950 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700951 if (!ino)
952 return -ENOMEM;
953
Al Viro4b1ae272010-03-03 12:58:31 -0500954 autofs4_del_active(dentry);
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700957 if (!inode) {
958 if (!dentry->d_fsdata)
959 kfree(ino);
960 return -ENOMEM;
961 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700962 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 dentry->d_fsdata = ino;
965 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800966 atomic_inc(&ino->count);
967 p_ino = autofs4_dentry_ino(dentry->d_parent);
968 if (p_ino && dentry->d_parent != dentry)
969 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700971 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 dir->i_mtime = CURRENT_TIME;
973
974 return 0;
975}
976
977/* Get/set timeout ioctl() operation */
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200978#ifdef CONFIG_COMPAT
979static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
980 compat_ulong_t __user *p)
981{
982 int rv;
983 unsigned long ntimeout;
984
985 if ((rv = get_user(ntimeout, p)) ||
986 (rv = put_user(sbi->exp_timeout/HZ, p)))
987 return rv;
988
989 if (ntimeout > UINT_MAX/HZ)
990 sbi->exp_timeout = 0;
991 else
992 sbi->exp_timeout = ntimeout * HZ;
993
994 return 0;
995}
996#endif
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
999 unsigned long __user *p)
1000{
1001 int rv;
1002 unsigned long ntimeout;
1003
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -07001004 if ((rv = get_user(ntimeout, p)) ||
1005 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return rv;
1007
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -07001008 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 sbi->exp_timeout = 0;
1010 else
1011 sbi->exp_timeout = ntimeout * HZ;
1012
1013 return 0;
1014}
1015
1016/* Return protocol version */
1017static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
1018{
1019 return put_user(sbi->version, p);
1020}
1021
1022/* Return protocol sub version */
1023static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
1024{
1025 return put_user(sbi->sub_version, p);
1026}
1027
1028/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029* Tells the daemon whether it can umount the autofs mount.
1030*/
1031static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
1032{
1033 int status = 0;
1034
Ian Kente3474a82006-03-27 01:14:51 -08001035 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 status = 1;
1037
1038 DPRINTK("returning %d", status);
1039
1040 status = put_user(status, p);
1041
1042 return status;
1043}
1044
1045/* Identify autofs4_dentries - this is so we can tell if there's
1046 an extra dentry refcount or not. We only hold a refcount on the
1047 dentry if its non-negative (ie, d_inode != NULL)
1048*/
1049int is_autofs4_dentry(struct dentry *dentry)
1050{
1051 return dentry && dentry->d_inode &&
1052 (dentry->d_op == &autofs4_root_dentry_operations ||
1053 dentry->d_op == &autofs4_dentry_operations) &&
1054 dentry->d_fsdata != NULL;
1055}
1056
1057/*
1058 * ioctl()'s on the root directory is the chief method for the daemon to
1059 * generate kernel reactions
1060 */
Frederic Weisbecker3663df72010-05-19 15:08:17 +02001061static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
1062 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
1065 void __user *p = (void __user *)arg;
1066
1067 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -07001068 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -07001070 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
1071 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return -ENOTTY;
1073
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -07001074 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return -EPERM;
1076
1077 switch(cmd) {
1078 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
1079 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
1080 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
1081 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
1082 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
1083 autofs4_catatonic_mode(sbi);
1084 return 0;
1085 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
1086 return autofs4_get_protover(sbi, p);
1087 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
1088 return autofs4_get_protosubver(sbi, p);
1089 case AUTOFS_IOC_SETTIMEOUT:
1090 return autofs4_get_set_timeout(sbi, p);
Arnd Bergmannc9243f52010-07-04 00:15:07 +02001091#ifdef CONFIG_COMPAT
1092 case AUTOFS_IOC_SETTIMEOUT32:
1093 return autofs4_compat_get_set_timeout(sbi, p);
1094#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001097 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /* return a single thing to expire */
1100 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001101 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 /* same as above, but can send multiple expires through pipe */
1103 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001104 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 default:
1107 return -ENOSYS;
1108 }
1109}
Frederic Weisbecker3663df72010-05-19 15:08:17 +02001110
1111static long autofs4_root_ioctl(struct file *filp,
1112 unsigned int cmd, unsigned long arg)
1113{
Frederic Weisbecker3663df72010-05-19 15:08:17 +02001114 struct inode *inode = filp->f_dentry->d_inode;
Ian Kentde47de72010-12-07 13:04:00 +08001115 return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
Frederic Weisbecker3663df72010-05-19 15:08:17 +02001116}
Arnd Bergmannc9243f52010-07-04 00:15:07 +02001117
1118#ifdef CONFIG_COMPAT
1119static long autofs4_root_compat_ioctl(struct file *filp,
1120 unsigned int cmd, unsigned long arg)
1121{
1122 struct inode *inode = filp->f_path.dentry->d_inode;
1123 int ret;
1124
Arnd Bergmannc9243f52010-07-04 00:15:07 +02001125 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
1126 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
1127 else
1128 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
1129 (unsigned long)compat_ptr(arg));
Arnd Bergmannc9243f52010-07-04 00:15:07 +02001130
1131 return ret;
1132}
1133#endif