blob: 7a9ed6b8829149b179b54c4ab32741418e115b6d [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
26static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
27static int autofs4_dir_unlink(struct inode *,struct dentry *);
28static int autofs4_dir_rmdir(struct inode *,struct dentry *);
29static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
Frederic Weisbecker3663df72010-05-19 15:08:17 +020030static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
Felipe Contreras5a44a732010-09-24 15:22:23 +020031#ifdef CONFIG_COMPAT
Arnd Bergmannc9243f52010-07-04 00:15:07 +020032static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
Felipe Contreras5a44a732010-09-24 15:22:23 +020033#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static int autofs4_dir_open(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
Ian Kent34ca9592006-03-27 01:14:54 -080036static void *autofs4_follow_link(struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Ian Kent6d5cb922008-07-23 21:30:15 -070038#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
39#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
40
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080041const struct file_operations autofs4_root_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 .open = dcache_dir_open,
43 .release = dcache_dir_close,
44 .read = generic_read_dir,
Ian Kentaa55ddf2008-07-23 21:30:29 -070045 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040046 .llseek = dcache_dir_lseek,
Frederic Weisbecker3663df72010-05-19 15:08:17 +020047 .unlocked_ioctl = autofs4_root_ioctl,
Arnd Bergmannc9243f52010-07-04 00:15:07 +020048#ifdef CONFIG_COMPAT
49 .compat_ioctl = autofs4_root_compat_ioctl,
50#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080053const struct file_operations autofs4_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .open = autofs4_dir_open,
Ian Kentff9cd492008-07-23 21:30:24 -070055 .release = dcache_dir_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .read = generic_read_dir,
Ian Kentff9cd492008-07-23 21:30:24 -070057 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040058 .llseek = dcache_dir_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Arjan van de Ven754661f2007-02-12 00:55:38 -080061const struct inode_operations autofs4_indirect_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 .lookup = autofs4_lookup,
63 .unlink = autofs4_dir_unlink,
64 .symlink = autofs4_dir_symlink,
65 .mkdir = autofs4_dir_mkdir,
66 .rmdir = autofs4_dir_rmdir,
67};
68
Arjan van de Ven754661f2007-02-12 00:55:38 -080069const struct inode_operations autofs4_direct_root_inode_operations = {
Ian Kent34ca9592006-03-27 01:14:54 -080070 .lookup = autofs4_lookup,
Ian Kent871f9432006-03-27 01:14:58 -080071 .unlink = autofs4_dir_unlink,
72 .mkdir = autofs4_dir_mkdir,
73 .rmdir = autofs4_dir_rmdir,
Ian Kent34ca9592006-03-27 01:14:54 -080074 .follow_link = autofs4_follow_link,
75};
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 */
145 spin_lock(&dcache_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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 spin_unlock(&dcache_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);
Ian Kentff9cd492008-07-23 21:30:24 -0700153 spin_unlock(&dcache_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
Al Viro9393bd02009-04-18 13:58:15 -0400235 * to break out of follow_down() 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. */
Al Viro9393bd02009-04-18 13:58:15 -0400244 if (!follow_down(&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);
258 spin_lock(&dcache_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);
Ian Kentaa952eb2009-12-15 16:45:45 -0800262 spin_unlock(&dcache_lock);
263 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);
Ian Kent871f9432006-03-27 01:14:58 -0800275 spin_unlock(&dcache_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);
Ian Kent871f9432006-03-27 01:14:58 -0800285 spin_unlock(&dcache_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 */
293 if (d_mountpoint(dentry)) {
Al Viro9393bd02009-04-18 13:58:15 -0400294 if (!autofs4_follow_mount(&nd->path)) {
Ian Kent6e60a9a2008-07-23 21:30:27 -0700295 status = -ENOENT;
296 goto out_error;
297 }
298 }
Ian Kent871f9432006-03-27 01:14:58 -0800299
Ian Kent34ca9592006-03-27 01:14:54 -0800300done:
301 return NULL;
302
303out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800304 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800305 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308/*
309 * Revalidate is called on every cache lookup. Some of those
310 * cache lookups may actually happen while the dentry is not
311 * yet completely filled in, and revalidate has to delay such
312 * lookups..
313 */
Ian Kent718c6042006-03-27 01:14:42 -0800314static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Ian Kent718c6042006-03-27 01:14:42 -0800316 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
Al Viro4b1ae272010-03-03 12:58:31 -0500318 int oz_mode = autofs4_oz_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int flags = nd ? nd->flags : 0;
Al Viro4b1ae272010-03-03 12:58:31 -0500320 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Ian Kent213614d2009-12-15 16:45:51 -0800322 /* Pending dentry */
Al Viro4b1ae272010-03-03 12:58:31 -0500323 spin_lock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (autofs4_ispending(dentry)) {
Al Viro4b1ae272010-03-03 12:58:31 -0500325 /* The daemon never causes a mount to trigger */
Ian Kent213614d2009-12-15 16:45:51 -0800326 spin_unlock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500327
328 if (oz_mode)
329 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700330
331 /*
Ian Kent06a35982008-07-23 21:30:28 -0700332 * If the directory has gone away due to an expire
333 * we have been called as ->d_revalidate() and so
334 * we need to return false and proceed to ->lookup().
335 */
336 if (autofs4_expire_wait(dentry) == -EAGAIN)
337 return 0;
338
339 /*
Ian Kentbcdc5e02006-09-27 01:50:44 -0700340 * A zero status is success otherwise we have a
341 * negative error code.
342 */
Al Viro4b1ae272010-03-03 12:58:31 -0500343 status = try_to_fill_dentry(dentry, flags);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700344 if (status == 0)
Ian Kentf50b6f82007-02-20 13:58:10 -0800345 return 1;
346
Ian Kentbcdc5e02006-09-27 01:50:44 -0700347 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
Al Viro4b1ae272010-03-03 12:58:31 -0500349 spin_unlock(&sbi->fs_lock);
350
351 /* Negative dentry.. invalidate if "old" */
352 if (dentry->d_inode == NULL)
353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* Check for a non-mountpoint directory with no contents */
Al Viro4b1ae272010-03-03 12:58:31 -0500356 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100357 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (S_ISDIR(dentry->d_inode->i_mode) &&
Ian Kentc42c7f72009-12-15 16:45:48 -0800359 !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 DPRINTK("dentry=%p %.*s, emptydir",
361 dentry, dentry->d_name.len, dentry->d_name.name);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100362 spin_unlock(&dentry->d_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500363 spin_unlock(&dcache_lock);
Ian Kent97e74492008-07-23 21:30:26 -0700364
Al Viro4b1ae272010-03-03 12:58:31 -0500365 /* The daemon never causes a mount to trigger */
366 if (oz_mode)
367 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700368
Al Viro4b1ae272010-03-03 12:58:31 -0500369 /*
370 * A zero status is success otherwise we have a
371 * negative error code.
372 */
373 status = try_to_fill_dentry(dentry, flags);
374 if (status == 0)
375 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700376
Al Viro4b1ae272010-03-03 12:58:31 -0500377 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100379 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_unlock(&dcache_lock);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 1;
383}
384
Ian Kent34ca9592006-03-27 01:14:54 -0800385void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
387 struct autofs_info *inf;
388
389 DPRINTK("releasing %p", de);
390
391 inf = autofs4_dentry_ino(de);
392 de->d_fsdata = NULL;
393
394 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800395 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
396
Ian Kentf50b6f82007-02-20 13:58:10 -0800397 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700398 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700399 if (!list_empty(&inf->active))
400 list_del(&inf->active);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700401 if (!list_empty(&inf->expiring))
402 list_del(&inf->expiring);
403 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800404 }
405
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700406 inf->dentry = NULL;
407 inf->inode = NULL;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 autofs4_free_ino(inf);
410 }
411}
412
413/* For dentries of directories in the root dir */
Al Viro08f11512009-02-20 05:56:19 +0000414static const struct dentry_operations autofs4_root_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 .d_revalidate = autofs4_revalidate,
416 .d_release = autofs4_dentry_release,
417};
418
419/* For other dentries */
Al Viro08f11512009-02-20 05:56:19 +0000420static const struct dentry_operations autofs4_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 .d_revalidate = autofs4_revalidate,
422 .d_release = autofs4_dentry_release,
423};
424
Ian Kent6510c9d2009-12-15 16:45:47 -0800425static struct dentry *autofs4_lookup_active(struct dentry *dentry)
Ian Kent25767372008-07-23 21:30:12 -0700426{
Ian Kent6510c9d2009-12-15 16:45:47 -0800427 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
428 struct dentry *parent = dentry->d_parent;
429 struct qstr *name = &dentry->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700430 unsigned int len = name->len;
431 unsigned int hash = name->hash;
432 const unsigned char *str = name->name;
433 struct list_head *p, *head;
434
435 spin_lock(&dcache_lock);
436 spin_lock(&sbi->lookup_lock);
437 head = &sbi->active_list;
438 list_for_each(p, head) {
439 struct autofs_info *ino;
Ian Kente4d5ade2009-12-15 16:45:49 -0800440 struct dentry *active;
Ian Kent25767372008-07-23 21:30:12 -0700441 struct qstr *qstr;
442
443 ino = list_entry(p, struct autofs_info, active);
Ian Kente4d5ade2009-12-15 16:45:49 -0800444 active = ino->dentry;
Ian Kent25767372008-07-23 21:30:12 -0700445
Ian Kente4d5ade2009-12-15 16:45:49 -0800446 spin_lock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700447
448 /* Already gone? */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100449 if (active->d_count == 0)
Ian Kent25767372008-07-23 21:30:12 -0700450 goto next;
451
Ian Kente4d5ade2009-12-15 16:45:49 -0800452 qstr = &active->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700453
Ian Kente4d5ade2009-12-15 16:45:49 -0800454 if (active->d_name.hash != hash)
Ian Kent25767372008-07-23 21:30:12 -0700455 goto next;
Ian Kente4d5ade2009-12-15 16:45:49 -0800456 if (active->d_parent != parent)
Ian Kent25767372008-07-23 21:30:12 -0700457 goto next;
458
459 if (qstr->len != len)
460 goto next;
461 if (memcmp(qstr->name, str, len))
462 goto next;
463
Al Viro4b1ae272010-03-03 12:58:31 -0500464 if (d_unhashed(active)) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100465 dget_dlock(active);
Al Viro4b1ae272010-03-03 12:58:31 -0500466 spin_unlock(&active->d_lock);
467 spin_unlock(&sbi->lookup_lock);
468 spin_unlock(&dcache_lock);
469 return active;
470 }
Ian Kent25767372008-07-23 21:30:12 -0700471next:
Ian Kente4d5ade2009-12-15 16:45:49 -0800472 spin_unlock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700473 }
474 spin_unlock(&sbi->lookup_lock);
475 spin_unlock(&dcache_lock);
476
477 return NULL;
478}
479
Ian Kent6510c9d2009-12-15 16:45:47 -0800480static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
Ian Kentf50b6f82007-02-20 13:58:10 -0800481{
Ian Kent6510c9d2009-12-15 16:45:47 -0800482 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
483 struct dentry *parent = dentry->d_parent;
484 struct qstr *name = &dentry->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800485 unsigned int len = name->len;
486 unsigned int hash = name->hash;
487 const unsigned char *str = name->name;
488 struct list_head *p, *head;
489
490 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700491 spin_lock(&sbi->lookup_lock);
492 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800493 list_for_each(p, head) {
494 struct autofs_info *ino;
Ian Kentcb4b4922009-12-15 16:45:50 -0800495 struct dentry *expiring;
Ian Kentf50b6f82007-02-20 13:58:10 -0800496 struct qstr *qstr;
497
Ian Kent5f6f4f22008-07-23 21:30:09 -0700498 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentcb4b4922009-12-15 16:45:50 -0800499 expiring = ino->dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800500
Ian Kentcb4b4922009-12-15 16:45:50 -0800501 spin_lock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800502
503 /* Bad luck, we've already been dentry_iput */
Ian Kentcb4b4922009-12-15 16:45:50 -0800504 if (!expiring->d_inode)
Ian Kentf50b6f82007-02-20 13:58:10 -0800505 goto next;
506
Ian Kentcb4b4922009-12-15 16:45:50 -0800507 qstr = &expiring->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800508
Ian Kentcb4b4922009-12-15 16:45:50 -0800509 if (expiring->d_name.hash != hash)
Ian Kentf50b6f82007-02-20 13:58:10 -0800510 goto next;
Ian Kentcb4b4922009-12-15 16:45:50 -0800511 if (expiring->d_parent != parent)
Ian Kentf50b6f82007-02-20 13:58:10 -0800512 goto next;
513
514 if (qstr->len != len)
515 goto next;
516 if (memcmp(qstr->name, str, len))
517 goto next;
518
Al Viro4b1ae272010-03-03 12:58:31 -0500519 if (d_unhashed(expiring)) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100520 dget_dlock(expiring);
Al Viro4b1ae272010-03-03 12:58:31 -0500521 spin_unlock(&expiring->d_lock);
522 spin_unlock(&sbi->lookup_lock);
523 spin_unlock(&dcache_lock);
524 return expiring;
525 }
Ian Kentf50b6f82007-02-20 13:58:10 -0800526next:
Ian Kentcb4b4922009-12-15 16:45:50 -0800527 spin_unlock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800528 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700529 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800530 spin_unlock(&dcache_lock);
531
532 return NULL;
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/* Lookups in the root directory */
536static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
537{
538 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700539 struct autofs_info *ino;
Ian Kent90387c92009-12-15 16:45:46 -0800540 struct dentry *expiring, *active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int oz_mode;
542
543 DPRINTK("name = %.*s",
544 dentry->d_name.len, dentry->d_name.name);
545
Ian Kent718c6042006-03-27 01:14:42 -0800546 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800548 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700554 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Ian Kent6510c9d2009-12-15 16:45:47 -0800556 active = autofs4_lookup_active(dentry);
Ian Kent90387c92009-12-15 16:45:46 -0800557 if (active) {
558 dentry = active;
Ian Kentaa952eb2009-12-15 16:45:45 -0800559 ino = autofs4_dentry_ino(dentry);
560 } else {
Al Viro4b1ae272010-03-03 12:58:31 -0500561 /*
562 * Mark the dentry incomplete but don't hash it. We do this
563 * to serialize our inode creation operations (symlink and
564 * mkdir) which prevents deadlock during the callback to
565 * the daemon. Subsequent user space lookups for the same
566 * dentry are placed on the wait queue while the daemon
567 * itself is allowed passage unresticted so the create
568 * operation itself can then hash the dentry. Finally,
569 * we check for the hashed dentry and return the newly
570 * hashed dentry.
571 */
572 dentry->d_op = &autofs4_root_dentry_operations;
573
574 /*
575 * And we need to ensure that the same dentry is used for
576 * all following lookup calls until it is hashed so that
577 * the dentry flags are persistent throughout the request.
578 */
579 ino = autofs4_init_ino(NULL, sbi, 0555);
580 if (!ino)
581 return ERR_PTR(-ENOMEM);
582
583 dentry->d_fsdata = ino;
584 ino->dentry = dentry;
585
586 autofs4_add_active(dentry);
587
588 d_instantiate(dentry, NULL);
Ian Kent25767372008-07-23 21:30:12 -0700589 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (!oz_mode) {
Ian Kent213614d2009-12-15 16:45:51 -0800592 mutex_unlock(&dir->i_mutex);
Al Viro4b1ae272010-03-03 12:58:31 -0500593 expiring = autofs4_lookup_expiring(dentry);
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700594 if (expiring) {
595 /*
596 * If we are racing with expire the request might not
597 * be quite complete but the directory has been removed
598 * so it must have been successful, so just wait for it.
599 */
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700600 autofs4_expire_wait(expiring);
Al Viro4b1ae272010-03-03 12:58:31 -0500601 autofs4_del_expiring(expiring);
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700602 dput(expiring);
603 }
Al Viro4b1ae272010-03-03 12:58:31 -0500604
Ian Kent213614d2009-12-15 16:45:51 -0800605 spin_lock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500606 ino->flags |= AUTOFS_INF_PENDING;
Ian Kent213614d2009-12-15 16:45:51 -0800607 spin_unlock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500608 if (dentry->d_op && dentry->d_op->d_revalidate)
609 (dentry->d_op->d_revalidate)(dentry, nd);
610 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
613 /*
Al Viro4b1ae272010-03-03 12:58:31 -0500614 * If we are still pending, check if we had to handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * a signal. If so we can force a restart..
616 */
Al Viro4b1ae272010-03-03 12:58:31 -0500617 if (ino->flags & AUTOFS_INF_PENDING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* See if we were interrupted */
619 if (signal_pending(current)) {
620 sigset_t *sigset = &current->pending.signal;
621 if (sigismember (sigset, SIGKILL) ||
622 sigismember (sigset, SIGQUIT) ||
623 sigismember (sigset, SIGINT)) {
Ian Kent90387c92009-12-15 16:45:46 -0800624 if (active)
625 dput(active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return ERR_PTR(-ERESTARTNOINTR);
627 }
628 }
Al Viro4b1ae272010-03-03 12:58:31 -0500629 if (!oz_mode) {
630 spin_lock(&sbi->fs_lock);
631 ino->flags &= ~AUTOFS_INF_PENDING;
632 spin_unlock(&sbi->fs_lock);
Ian Kent25767372008-07-23 21:30:12 -0700633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635
636 /*
Al Viro4b1ae272010-03-03 12:58:31 -0500637 * If this dentry is unhashed, then we shouldn't honour this
638 * lookup. Returning ENOENT here doesn't do the right thing
639 * for all system calls, but it should be OK for the operations
640 * we permit from an autofs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
Al Viro4b1ae272010-03-03 12:58:31 -0500642 if (!oz_mode && d_unhashed(dentry)) {
643 /*
644 * A user space application can (and has done in the past)
645 * remove and re-create this directory during the callback.
646 * This can leave us with an unhashed dentry, but a
647 * successful mount! So we need to perform another
648 * cached lookup in case the dentry now exists.
649 */
650 struct dentry *parent = dentry->d_parent;
651 struct dentry *new = d_lookup(parent, &dentry->d_name);
652 if (new != NULL)
653 dentry = new;
654 else
655 dentry = ERR_PTR(-ENOENT);
656
Ian Kent90387c92009-12-15 16:45:46 -0800657 if (active)
658 dput(active);
Al Viro4b1ae272010-03-03 12:58:31 -0500659
Ian Kentc9ffec42007-02-20 13:58:10 -0800660 return dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800661 }
662
Al Viro4b1ae272010-03-03 12:58:31 -0500663 if (active)
664 return active;
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return NULL;
667}
668
669static int autofs4_dir_symlink(struct inode *dir,
670 struct dentry *dentry,
671 const char *symname)
672{
673 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
674 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800675 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct inode *inode;
677 char *cp;
678
679 DPRINTK("%s <- %.*s", symname,
680 dentry->d_name.len, dentry->d_name.name);
681
682 if (!autofs4_oz_mode(sbi))
683 return -EACCES;
684
685 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700686 if (!ino)
687 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Al Viro4b1ae272010-03-03 12:58:31 -0500689 autofs4_del_active(dentry);
690
Ian Kentef581a72008-07-23 21:30:13 -0700691 ino->size = strlen(symname);
Ian Kent25767372008-07-23 21:30:12 -0700692 cp = kmalloc(ino->size + 1, GFP_KERNEL);
693 if (!cp) {
694 if (!dentry->d_fsdata)
695 kfree(ino);
696 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 strcpy(cp, symname);
700
701 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700702 if (!inode) {
703 kfree(cp);
704 if (!dentry->d_fsdata)
705 kfree(ino);
706 return -ENOMEM;
707 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700708 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710 if (dir == dir->i_sb->s_root->d_inode)
711 dentry->d_op = &autofs4_root_dentry_operations;
712 else
713 dentry->d_op = &autofs4_dentry_operations;
714
715 dentry->d_fsdata = ino;
716 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800717 atomic_inc(&ino->count);
718 p_ino = autofs4_dentry_ino(dentry->d_parent);
719 if (p_ino && dentry->d_parent != dentry)
720 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 ino->inode = inode;
722
Ian Kent25767372008-07-23 21:30:12 -0700723 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 dir->i_mtime = CURRENT_TIME;
725
726 return 0;
727}
728
729/*
730 * NOTE!
731 *
732 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
733 * that the file no longer exists. However, doing that means that the
734 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800735 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700736 * We simply d_drop it and add it to a expiring list in the super block,
737 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 *
739 * If a process is blocked on the dentry waiting for the expire to finish,
740 * it will invalidate the dentry and try to mount with a new one.
741 *
742 * Also see autofs4_dir_rmdir()..
743 */
744static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
745{
746 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
747 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800748 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700751 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return -EACCES;
753
Ian Kent1aff3c82006-03-27 01:14:46 -0800754 if (atomic_dec_and_test(&ino->count)) {
755 p_ino = autofs4_dentry_ino(dentry->d_parent);
756 if (p_ino && dentry->d_parent != dentry)
757 atomic_dec(&p_ino->count);
758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 dput(ino->dentry);
760
761 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700762 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 dir->i_mtime = CURRENT_TIME;
765
Ian Kentf50b6f82007-02-20 13:58:10 -0800766 spin_lock(&dcache_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500767 autofs4_add_expiring(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800768 spin_lock(&dentry->d_lock);
769 __d_drop(dentry);
770 spin_unlock(&dentry->d_lock);
771 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 return 0;
774}
775
776static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
777{
778 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
779 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800780 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Ian Kentf50b6f82007-02-20 13:58:10 -0800782 DPRINTK("dentry %p, removing %.*s",
783 dentry, dentry->d_name.len, dentry->d_name.name);
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (!autofs4_oz_mode(sbi))
786 return -EACCES;
787
788 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100789 spin_lock(&sbi->lookup_lock);
790 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100792 spin_unlock(&dentry->d_lock);
793 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 spin_unlock(&dcache_lock);
795 return -ENOTEMPTY;
796 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100797 __autofs4_add_expiring(dentry);
798 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 __d_drop(dentry);
800 spin_unlock(&dentry->d_lock);
801 spin_unlock(&dcache_lock);
802
Ian Kent1aff3c82006-03-27 01:14:46 -0800803 if (atomic_dec_and_test(&ino->count)) {
804 p_ino = autofs4_dentry_ino(dentry->d_parent);
805 if (p_ino && dentry->d_parent != dentry)
806 atomic_dec(&p_ino->count);
807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700810 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700813 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 return 0;
816}
817
818static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
819{
820 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
821 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800822 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 struct inode *inode;
824
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700825 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return -EACCES;
827
828 DPRINTK("dentry %p, creating %.*s",
829 dentry, dentry->d_name.len, dentry->d_name.name);
830
831 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700832 if (!ino)
833 return -ENOMEM;
834
Al Viro4b1ae272010-03-03 12:58:31 -0500835 autofs4_del_active(dentry);
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700838 if (!inode) {
839 if (!dentry->d_fsdata)
840 kfree(ino);
841 return -ENOMEM;
842 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700843 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 if (dir == dir->i_sb->s_root->d_inode)
846 dentry->d_op = &autofs4_root_dentry_operations;
847 else
848 dentry->d_op = &autofs4_dentry_operations;
849
850 dentry->d_fsdata = ino;
851 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800852 atomic_inc(&ino->count);
853 p_ino = autofs4_dentry_ino(dentry->d_parent);
854 if (p_ino && dentry->d_parent != dentry)
855 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700857 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 dir->i_mtime = CURRENT_TIME;
859
860 return 0;
861}
862
863/* Get/set timeout ioctl() operation */
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200864#ifdef CONFIG_COMPAT
865static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
866 compat_ulong_t __user *p)
867{
868 int rv;
869 unsigned long ntimeout;
870
871 if ((rv = get_user(ntimeout, p)) ||
872 (rv = put_user(sbi->exp_timeout/HZ, p)))
873 return rv;
874
875 if (ntimeout > UINT_MAX/HZ)
876 sbi->exp_timeout = 0;
877 else
878 sbi->exp_timeout = ntimeout * HZ;
879
880 return 0;
881}
882#endif
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
885 unsigned long __user *p)
886{
887 int rv;
888 unsigned long ntimeout;
889
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700890 if ((rv = get_user(ntimeout, p)) ||
891 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return rv;
893
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700894 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 sbi->exp_timeout = 0;
896 else
897 sbi->exp_timeout = ntimeout * HZ;
898
899 return 0;
900}
901
902/* Return protocol version */
903static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
904{
905 return put_user(sbi->version, p);
906}
907
908/* Return protocol sub version */
909static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
910{
911 return put_user(sbi->sub_version, p);
912}
913
914/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915* Tells the daemon whether it can umount the autofs mount.
916*/
917static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
918{
919 int status = 0;
920
Ian Kente3474a82006-03-27 01:14:51 -0800921 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 status = 1;
923
924 DPRINTK("returning %d", status);
925
926 status = put_user(status, p);
927
928 return status;
929}
930
931/* Identify autofs4_dentries - this is so we can tell if there's
932 an extra dentry refcount or not. We only hold a refcount on the
933 dentry if its non-negative (ie, d_inode != NULL)
934*/
935int is_autofs4_dentry(struct dentry *dentry)
936{
937 return dentry && dentry->d_inode &&
938 (dentry->d_op == &autofs4_root_dentry_operations ||
939 dentry->d_op == &autofs4_dentry_operations) &&
940 dentry->d_fsdata != NULL;
941}
942
943/*
944 * ioctl()'s on the root directory is the chief method for the daemon to
945 * generate kernel reactions
946 */
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200947static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
948 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
950 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
951 void __user *p = (void __user *)arg;
952
953 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700954 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700956 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
957 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return -ENOTTY;
959
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700960 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -EPERM;
962
963 switch(cmd) {
964 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
965 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
966 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
967 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
968 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
969 autofs4_catatonic_mode(sbi);
970 return 0;
971 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
972 return autofs4_get_protover(sbi, p);
973 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
974 return autofs4_get_protosubver(sbi, p);
975 case AUTOFS_IOC_SETTIMEOUT:
976 return autofs4_get_set_timeout(sbi, p);
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200977#ifdef CONFIG_COMPAT
978 case AUTOFS_IOC_SETTIMEOUT32:
979 return autofs4_compat_get_set_timeout(sbi, p);
980#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800983 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 /* return a single thing to expire */
986 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800987 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* same as above, but can send multiple expires through pipe */
989 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800990 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 default:
993 return -ENOSYS;
994 }
995}
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200996
997static long autofs4_root_ioctl(struct file *filp,
998 unsigned int cmd, unsigned long arg)
999{
Frederic Weisbecker3663df72010-05-19 15:08:17 +02001000 struct inode *inode = filp->f_dentry->d_inode;
Ian Kentde47de72010-12-07 13:04:00 +08001001 return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
Frederic Weisbecker3663df72010-05-19 15:08:17 +02001002}
Arnd Bergmannc9243f52010-07-04 00:15:07 +02001003
1004#ifdef CONFIG_COMPAT
1005static long autofs4_root_compat_ioctl(struct file *filp,
1006 unsigned int cmd, unsigned long arg)
1007{
1008 struct inode *inode = filp->f_path.dentry->d_inode;
1009 int ret;
1010
Arnd Bergmannc9243f52010-07-04 00:15:07 +02001011 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
1012 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
1013 else
1014 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
1015 (unsigned long)compat_ptr(arg));
Arnd Bergmannc9243f52010-07-04 00:15:07 +02001016
1017 return ret;
1018}
1019#endif