blob: cb1bd38dc08cb10c47650e1e08c2a8fe13730ab2 [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>
Frederic Weisbecker3663df72010-05-19 15:08:17 +020022#include <linux/smp_lock.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);
Arnd Bergmannc9243f52010-07-04 00:15:07 +020031static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int autofs4_dir_open(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
Ian Kent34ca9592006-03-27 01:14:54 -080034static void *autofs4_follow_link(struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Ian Kent6d5cb922008-07-23 21:30:15 -070036#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
37#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
38
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080039const struct file_operations autofs4_root_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 .open = dcache_dir_open,
41 .release = dcache_dir_close,
42 .read = generic_read_dir,
Ian Kentaa55ddf2008-07-23 21:30:29 -070043 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040044 .llseek = dcache_dir_lseek,
Frederic Weisbecker3663df72010-05-19 15:08:17 +020045 .unlocked_ioctl = autofs4_root_ioctl,
Arnd Bergmannc9243f52010-07-04 00:15:07 +020046#ifdef CONFIG_COMPAT
47 .compat_ioctl = autofs4_root_compat_ioctl,
48#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080051const struct file_operations autofs4_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 .open = autofs4_dir_open,
Ian Kentff9cd492008-07-23 21:30:24 -070053 .release = dcache_dir_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .read = generic_read_dir,
Ian Kentff9cd492008-07-23 21:30:24 -070055 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040056 .llseek = dcache_dir_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
Arjan van de Ven754661f2007-02-12 00:55:38 -080059const struct inode_operations autofs4_indirect_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .lookup = autofs4_lookup,
61 .unlink = autofs4_dir_unlink,
62 .symlink = autofs4_dir_symlink,
63 .mkdir = autofs4_dir_mkdir,
64 .rmdir = autofs4_dir_rmdir,
65};
66
Arjan van de Ven754661f2007-02-12 00:55:38 -080067const struct inode_operations autofs4_direct_root_inode_operations = {
Ian Kent34ca9592006-03-27 01:14:54 -080068 .lookup = autofs4_lookup,
Ian Kent871f9432006-03-27 01:14:58 -080069 .unlink = autofs4_dir_unlink,
70 .mkdir = autofs4_dir_mkdir,
71 .rmdir = autofs4_dir_rmdir,
Ian Kent34ca9592006-03-27 01:14:54 -080072 .follow_link = autofs4_follow_link,
73};
74
Arjan van de Ven754661f2007-02-12 00:55:38 -080075const struct inode_operations autofs4_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 .lookup = autofs4_lookup,
77 .unlink = autofs4_dir_unlink,
78 .symlink = autofs4_dir_symlink,
79 .mkdir = autofs4_dir_mkdir,
80 .rmdir = autofs4_dir_rmdir,
81};
82
Ian Kent4f8427d2009-12-15 16:45:42 -080083static void autofs4_add_active(struct dentry *dentry)
84{
85 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
86 struct autofs_info *ino = autofs4_dentry_ino(dentry);
87 if (ino) {
88 spin_lock(&sbi->lookup_lock);
89 if (!ino->active_count) {
90 if (list_empty(&ino->active))
91 list_add(&ino->active, &sbi->active_list);
92 }
93 ino->active_count++;
94 spin_unlock(&sbi->lookup_lock);
95 }
96 return;
97}
98
99static void autofs4_del_active(struct dentry *dentry)
100{
101 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
102 struct autofs_info *ino = autofs4_dentry_ino(dentry);
103 if (ino) {
104 spin_lock(&sbi->lookup_lock);
105 ino->active_count--;
106 if (!ino->active_count) {
107 if (!list_empty(&ino->active))
108 list_del_init(&ino->active);
109 }
110 spin_unlock(&sbi->lookup_lock);
111 }
112 return;
113}
114
Ian Kent36b64132009-12-15 16:45:44 -0800115static unsigned int autofs4_need_mount(unsigned int flags)
116{
117 unsigned int res = 0;
118 if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
119 res = 1;
120 return res;
121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static int autofs4_dir_open(struct inode *inode, struct file *file)
124{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800125 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 DPRINTK("file=%p dentry=%p %.*s",
129 file, dentry, dentry->d_name.len, dentry->d_name.name);
130
131 if (autofs4_oz_mode(sbi))
132 goto out;
133
Ian Kentff9cd492008-07-23 21:30:24 -0700134 /*
135 * An empty directory in an autofs file system is always a
136 * mount point. The daemon must have failed to mount this
137 * during lookup so it doesn't exist. This can happen, for
138 * example, if user space returns an incorrect status for a
139 * mount request. Otherwise we're doing a readdir on the
140 * autofs file system so just let the libfs routines handle
141 * it.
142 */
143 spin_lock(&dcache_lock);
Ian Kentc42c7f72009-12-15 16:45:48 -0800144 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 spin_unlock(&dcache_lock);
Ian Kentff9cd492008-07-23 21:30:24 -0700146 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
Ian Kentff9cd492008-07-23 21:30:24 -0700148 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Ian Kentf360ce32006-03-27 01:14:43 -0800150out:
Ian Kentff9cd492008-07-23 21:30:24 -0700151 return dcache_dir_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Al Viro4b1ae272010-03-03 12:58:31 -0500154static int try_to_fill_dentry(struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Ian Kent862b1102006-03-27 01:14:48 -0800156 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800157 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700158 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 DPRINTK("dentry=%p %.*s ino=%p",
161 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
162
Ian Kent718c6042006-03-27 01:14:42 -0800163 /*
164 * Wait for a pending mount, triggering one if there
165 * isn't one already
166 */
Al Viro4b1ae272010-03-03 12:58:31 -0500167 if (dentry->d_inode == NULL) {
168 DPRINTK("waiting for mount name=%.*s",
169 dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Al Viro4b1ae272010-03-03 12:58:31 -0500171 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800172
Al Viro4b1ae272010-03-03 12:58:31 -0500173 DPRINTK("mount done status=%d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Al Viro4b1ae272010-03-03 12:58:31 -0500175 /* Turn this into a real negative dentry? */
176 if (status == -ENOENT) {
177 spin_lock(&sbi->fs_lock);
178 ino->flags &= ~AUTOFS_INF_PENDING;
179 spin_unlock(&sbi->fs_lock);
180 return status;
181 } else if (status) {
182 /* Return a negative dentry, but leave it "pending" */
183 return status;
184 }
185 /* Trigger mount for path component or follow link */
186 } else if (ino->flags & AUTOFS_INF_PENDING ||
Ian Kentf7422462010-05-10 16:46:08 +0800187 autofs4_need_mount(flags)) {
Al Viro4b1ae272010-03-03 12:58:31 -0500188 DPRINTK("waiting for mount name=%.*s",
189 dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Al Viro4b1ae272010-03-03 12:58:31 -0500191 spin_lock(&sbi->fs_lock);
192 ino->flags |= AUTOFS_INF_PENDING;
193 spin_unlock(&sbi->fs_lock);
194 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
195
196 DPRINTK("mount done status=%d", status);
197
198 if (status) {
199 spin_lock(&sbi->fs_lock);
200 ino->flags &= ~AUTOFS_INF_PENDING;
201 spin_unlock(&sbi->fs_lock);
202 return status;
203 }
204 }
205
206 /* Initialize expiry counter after successful mount */
Dan Carpenter5fc79d82010-08-10 18:02:04 -0700207 ino->last_used = jiffies;
Al Viro4b1ae272010-03-03 12:58:31 -0500208
209 spin_lock(&sbi->fs_lock);
210 ino->flags &= ~AUTOFS_INF_PENDING;
211 spin_unlock(&sbi->fs_lock);
212
213 return 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800214}
215
216/* For autofs direct mounts the follow link triggers the mount */
217static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
218{
219 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kenta5370552006-05-15 09:43:51 -0700220 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent34ca9592006-03-27 01:14:54 -0800221 int oz_mode = autofs4_oz_mode(sbi);
222 unsigned int lookup_type;
223 int status;
224
225 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
226 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
227 nd->flags);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700228 /*
229 * For an expire of a covered direct or offset mount we need
Al Viro9393bd02009-04-18 13:58:15 -0400230 * to break out of follow_down() at the autofs mount trigger
Ian Kent6e60a9a2008-07-23 21:30:27 -0700231 * (d_mounted--), so we can see the expiring flag, and manage
232 * the blocking and following here until the expire is completed.
233 */
234 if (oz_mode) {
235 spin_lock(&sbi->fs_lock);
236 if (ino->flags & AUTOFS_INF_EXPIRING) {
237 spin_unlock(&sbi->fs_lock);
238 /* Follow down to our covering mount. */
Al Viro9393bd02009-04-18 13:58:15 -0400239 if (!follow_down(&nd->path))
Ian Kent6e60a9a2008-07-23 21:30:27 -0700240 goto done;
Ian Kentec6e8c72008-07-23 21:30:28 -0700241 goto follow;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700242 }
243 spin_unlock(&sbi->fs_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800244 goto done;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700245 }
Ian Kent34ca9592006-03-27 01:14:54 -0800246
Ian Kent6e60a9a2008-07-23 21:30:27 -0700247 /* If an expire request is pending everyone must wait. */
Ian Kent06a35982008-07-23 21:30:28 -0700248 autofs4_expire_wait(dentry);
Ian Kent97e74492008-07-23 21:30:26 -0700249
Ian Kent6e60a9a2008-07-23 21:30:27 -0700250 /* We trigger a mount for almost all flags */
Ian Kent36b64132009-12-15 16:45:44 -0800251 lookup_type = autofs4_need_mount(nd->flags);
Ian Kentaa952eb2009-12-15 16:45:45 -0800252 spin_lock(&sbi->fs_lock);
253 spin_lock(&dcache_lock);
254 if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
255 spin_unlock(&dcache_lock);
256 spin_unlock(&sbi->fs_lock);
Ian Kentec6e8c72008-07-23 21:30:28 -0700257 goto follow;
Ian Kentaa952eb2009-12-15 16:45:45 -0800258 }
Ian Kent6e60a9a2008-07-23 21:30:27 -0700259
Ian Kent871f9432006-03-27 01:14:58 -0800260 /*
Ian Kent6e60a9a2008-07-23 21:30:27 -0700261 * If the dentry contains directories then it is an autofs
262 * multi-mount with no root mount offset. So don't try to
263 * mount it again.
Ian Kent871f9432006-03-27 01:14:58 -0800264 */
Ian Kentaa952eb2009-12-15 16:45:45 -0800265 if (ino->flags & AUTOFS_INF_PENDING ||
Ian Kentc42c7f72009-12-15 16:45:48 -0800266 (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
Ian Kent871f9432006-03-27 01:14:58 -0800267 spin_unlock(&dcache_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800268 spin_unlock(&sbi->fs_lock);
Ian Kent871f9432006-03-27 01:14:58 -0800269
Ian Kentf7422462010-05-10 16:46:08 +0800270 status = try_to_fill_dentry(dentry, nd->flags);
Ian Kent871f9432006-03-27 01:14:58 -0800271 if (status)
272 goto out_error;
273
Ian Kent6e60a9a2008-07-23 21:30:27 -0700274 goto follow;
Ian Kent871f9432006-03-27 01:14:58 -0800275 }
276 spin_unlock(&dcache_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800277 spin_unlock(&sbi->fs_lock);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700278follow:
279 /*
280 * If there is no root mount it must be an autofs
281 * multi-mount with no root offset so we don't need
282 * to follow it.
283 */
284 if (d_mountpoint(dentry)) {
Al Viro9393bd02009-04-18 13:58:15 -0400285 if (!autofs4_follow_mount(&nd->path)) {
Ian Kent6e60a9a2008-07-23 21:30:27 -0700286 status = -ENOENT;
287 goto out_error;
288 }
289 }
Ian Kent871f9432006-03-27 01:14:58 -0800290
Ian Kent34ca9592006-03-27 01:14:54 -0800291done:
292 return NULL;
293
294out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800295 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800296 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299/*
300 * Revalidate is called on every cache lookup. Some of those
301 * cache lookups may actually happen while the dentry is not
302 * yet completely filled in, and revalidate has to delay such
303 * lookups..
304 */
Ian Kent718c6042006-03-27 01:14:42 -0800305static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Ian Kent718c6042006-03-27 01:14:42 -0800307 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
Al Viro4b1ae272010-03-03 12:58:31 -0500309 int oz_mode = autofs4_oz_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 int flags = nd ? nd->flags : 0;
Al Viro4b1ae272010-03-03 12:58:31 -0500311 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Ian Kent213614d2009-12-15 16:45:51 -0800313 /* Pending dentry */
Al Viro4b1ae272010-03-03 12:58:31 -0500314 spin_lock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (autofs4_ispending(dentry)) {
Al Viro4b1ae272010-03-03 12:58:31 -0500316 /* The daemon never causes a mount to trigger */
Ian Kent213614d2009-12-15 16:45:51 -0800317 spin_unlock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500318
319 if (oz_mode)
320 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700321
322 /*
Ian Kent06a35982008-07-23 21:30:28 -0700323 * If the directory has gone away due to an expire
324 * we have been called as ->d_revalidate() and so
325 * we need to return false and proceed to ->lookup().
326 */
327 if (autofs4_expire_wait(dentry) == -EAGAIN)
328 return 0;
329
330 /*
Ian Kentbcdc5e02006-09-27 01:50:44 -0700331 * A zero status is success otherwise we have a
332 * negative error code.
333 */
Al Viro4b1ae272010-03-03 12:58:31 -0500334 status = try_to_fill_dentry(dentry, flags);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700335 if (status == 0)
Ian Kentf50b6f82007-02-20 13:58:10 -0800336 return 1;
337
Ian Kentbcdc5e02006-09-27 01:50:44 -0700338 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Al Viro4b1ae272010-03-03 12:58:31 -0500340 spin_unlock(&sbi->fs_lock);
341
342 /* Negative dentry.. invalidate if "old" */
343 if (dentry->d_inode == NULL)
344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* Check for a non-mountpoint directory with no contents */
Al Viro4b1ae272010-03-03 12:58:31 -0500347 spin_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (S_ISDIR(dentry->d_inode->i_mode) &&
Ian Kentc42c7f72009-12-15 16:45:48 -0800349 !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 DPRINTK("dentry=%p %.*s, emptydir",
351 dentry, dentry->d_name.len, dentry->d_name.name);
Al Viro4b1ae272010-03-03 12:58:31 -0500352 spin_unlock(&dcache_lock);
Ian Kent97e74492008-07-23 21:30:26 -0700353
Al Viro4b1ae272010-03-03 12:58:31 -0500354 /* The daemon never causes a mount to trigger */
355 if (oz_mode)
356 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700357
Al Viro4b1ae272010-03-03 12:58:31 -0500358 /*
359 * A zero status is success otherwise we have a
360 * negative error code.
361 */
362 status = try_to_fill_dentry(dentry, flags);
363 if (status == 0)
364 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700365
Al Viro4b1ae272010-03-03 12:58:31 -0500366 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368 spin_unlock(&dcache_lock);
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return 1;
371}
372
Ian Kent34ca9592006-03-27 01:14:54 -0800373void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 struct autofs_info *inf;
376
377 DPRINTK("releasing %p", de);
378
379 inf = autofs4_dentry_ino(de);
380 de->d_fsdata = NULL;
381
382 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800383 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
384
Ian Kentf50b6f82007-02-20 13:58:10 -0800385 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700386 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700387 if (!list_empty(&inf->active))
388 list_del(&inf->active);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700389 if (!list_empty(&inf->expiring))
390 list_del(&inf->expiring);
391 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800392 }
393
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700394 inf->dentry = NULL;
395 inf->inode = NULL;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 autofs4_free_ino(inf);
398 }
399}
400
401/* For dentries of directories in the root dir */
Al Viro08f11512009-02-20 05:56:19 +0000402static const struct dentry_operations autofs4_root_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 .d_revalidate = autofs4_revalidate,
404 .d_release = autofs4_dentry_release,
405};
406
407/* For other dentries */
Al Viro08f11512009-02-20 05:56:19 +0000408static const struct dentry_operations autofs4_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 .d_revalidate = autofs4_revalidate,
410 .d_release = autofs4_dentry_release,
411};
412
Ian Kent6510c9d2009-12-15 16:45:47 -0800413static struct dentry *autofs4_lookup_active(struct dentry *dentry)
Ian Kent25767372008-07-23 21:30:12 -0700414{
Ian Kent6510c9d2009-12-15 16:45:47 -0800415 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
416 struct dentry *parent = dentry->d_parent;
417 struct qstr *name = &dentry->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700418 unsigned int len = name->len;
419 unsigned int hash = name->hash;
420 const unsigned char *str = name->name;
421 struct list_head *p, *head;
422
423 spin_lock(&dcache_lock);
424 spin_lock(&sbi->lookup_lock);
425 head = &sbi->active_list;
426 list_for_each(p, head) {
427 struct autofs_info *ino;
Ian Kente4d5ade2009-12-15 16:45:49 -0800428 struct dentry *active;
Ian Kent25767372008-07-23 21:30:12 -0700429 struct qstr *qstr;
430
431 ino = list_entry(p, struct autofs_info, active);
Ian Kente4d5ade2009-12-15 16:45:49 -0800432 active = ino->dentry;
Ian Kent25767372008-07-23 21:30:12 -0700433
Ian Kente4d5ade2009-12-15 16:45:49 -0800434 spin_lock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700435
436 /* Already gone? */
Ian Kente4d5ade2009-12-15 16:45:49 -0800437 if (atomic_read(&active->d_count) == 0)
Ian Kent25767372008-07-23 21:30:12 -0700438 goto next;
439
Ian Kente4d5ade2009-12-15 16:45:49 -0800440 qstr = &active->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700441
Ian Kente4d5ade2009-12-15 16:45:49 -0800442 if (active->d_name.hash != hash)
Ian Kent25767372008-07-23 21:30:12 -0700443 goto next;
Ian Kente4d5ade2009-12-15 16:45:49 -0800444 if (active->d_parent != parent)
Ian Kent25767372008-07-23 21:30:12 -0700445 goto next;
446
447 if (qstr->len != len)
448 goto next;
449 if (memcmp(qstr->name, str, len))
450 goto next;
451
Al Viro4b1ae272010-03-03 12:58:31 -0500452 if (d_unhashed(active)) {
453 dget(active);
454 spin_unlock(&active->d_lock);
455 spin_unlock(&sbi->lookup_lock);
456 spin_unlock(&dcache_lock);
457 return active;
458 }
Ian Kent25767372008-07-23 21:30:12 -0700459next:
Ian Kente4d5ade2009-12-15 16:45:49 -0800460 spin_unlock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700461 }
462 spin_unlock(&sbi->lookup_lock);
463 spin_unlock(&dcache_lock);
464
465 return NULL;
466}
467
Ian Kent6510c9d2009-12-15 16:45:47 -0800468static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
Ian Kentf50b6f82007-02-20 13:58:10 -0800469{
Ian Kent6510c9d2009-12-15 16:45:47 -0800470 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
471 struct dentry *parent = dentry->d_parent;
472 struct qstr *name = &dentry->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800473 unsigned int len = name->len;
474 unsigned int hash = name->hash;
475 const unsigned char *str = name->name;
476 struct list_head *p, *head;
477
478 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700479 spin_lock(&sbi->lookup_lock);
480 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800481 list_for_each(p, head) {
482 struct autofs_info *ino;
Ian Kentcb4b4922009-12-15 16:45:50 -0800483 struct dentry *expiring;
Ian Kentf50b6f82007-02-20 13:58:10 -0800484 struct qstr *qstr;
485
Ian Kent5f6f4f22008-07-23 21:30:09 -0700486 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentcb4b4922009-12-15 16:45:50 -0800487 expiring = ino->dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800488
Ian Kentcb4b4922009-12-15 16:45:50 -0800489 spin_lock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800490
491 /* Bad luck, we've already been dentry_iput */
Ian Kentcb4b4922009-12-15 16:45:50 -0800492 if (!expiring->d_inode)
Ian Kentf50b6f82007-02-20 13:58:10 -0800493 goto next;
494
Ian Kentcb4b4922009-12-15 16:45:50 -0800495 qstr = &expiring->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800496
Ian Kentcb4b4922009-12-15 16:45:50 -0800497 if (expiring->d_name.hash != hash)
Ian Kentf50b6f82007-02-20 13:58:10 -0800498 goto next;
Ian Kentcb4b4922009-12-15 16:45:50 -0800499 if (expiring->d_parent != parent)
Ian Kentf50b6f82007-02-20 13:58:10 -0800500 goto next;
501
502 if (qstr->len != len)
503 goto next;
504 if (memcmp(qstr->name, str, len))
505 goto next;
506
Al Viro4b1ae272010-03-03 12:58:31 -0500507 if (d_unhashed(expiring)) {
508 dget(expiring);
509 spin_unlock(&expiring->d_lock);
510 spin_unlock(&sbi->lookup_lock);
511 spin_unlock(&dcache_lock);
512 return expiring;
513 }
Ian Kentf50b6f82007-02-20 13:58:10 -0800514next:
Ian Kentcb4b4922009-12-15 16:45:50 -0800515 spin_unlock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800516 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700517 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800518 spin_unlock(&dcache_lock);
519
520 return NULL;
521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/* Lookups in the root directory */
524static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
525{
526 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700527 struct autofs_info *ino;
Ian Kent90387c92009-12-15 16:45:46 -0800528 struct dentry *expiring, *active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 int oz_mode;
530
531 DPRINTK("name = %.*s",
532 dentry->d_name.len, dentry->d_name.name);
533
Ian Kent718c6042006-03-27 01:14:42 -0800534 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800536 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700542 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Ian Kent6510c9d2009-12-15 16:45:47 -0800544 active = autofs4_lookup_active(dentry);
Ian Kent90387c92009-12-15 16:45:46 -0800545 if (active) {
546 dentry = active;
Ian Kentaa952eb2009-12-15 16:45:45 -0800547 ino = autofs4_dentry_ino(dentry);
548 } else {
Al Viro4b1ae272010-03-03 12:58:31 -0500549 /*
550 * Mark the dentry incomplete but don't hash it. We do this
551 * to serialize our inode creation operations (symlink and
552 * mkdir) which prevents deadlock during the callback to
553 * the daemon. Subsequent user space lookups for the same
554 * dentry are placed on the wait queue while the daemon
555 * itself is allowed passage unresticted so the create
556 * operation itself can then hash the dentry. Finally,
557 * we check for the hashed dentry and return the newly
558 * hashed dentry.
559 */
560 dentry->d_op = &autofs4_root_dentry_operations;
561
562 /*
563 * And we need to ensure that the same dentry is used for
564 * all following lookup calls until it is hashed so that
565 * the dentry flags are persistent throughout the request.
566 */
567 ino = autofs4_init_ino(NULL, sbi, 0555);
568 if (!ino)
569 return ERR_PTR(-ENOMEM);
570
571 dentry->d_fsdata = ino;
572 ino->dentry = dentry;
573
574 autofs4_add_active(dentry);
575
576 d_instantiate(dentry, NULL);
Ian Kent25767372008-07-23 21:30:12 -0700577 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (!oz_mode) {
Ian Kent213614d2009-12-15 16:45:51 -0800580 mutex_unlock(&dir->i_mutex);
Al Viro4b1ae272010-03-03 12:58:31 -0500581 expiring = autofs4_lookup_expiring(dentry);
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700582 if (expiring) {
583 /*
584 * If we are racing with expire the request might not
585 * be quite complete but the directory has been removed
586 * so it must have been successful, so just wait for it.
587 */
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700588 autofs4_expire_wait(expiring);
Al Viro4b1ae272010-03-03 12:58:31 -0500589 autofs4_del_expiring(expiring);
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700590 dput(expiring);
591 }
Al Viro4b1ae272010-03-03 12:58:31 -0500592
Ian Kent213614d2009-12-15 16:45:51 -0800593 spin_lock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500594 ino->flags |= AUTOFS_INF_PENDING;
Ian Kent213614d2009-12-15 16:45:51 -0800595 spin_unlock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500596 if (dentry->d_op && dentry->d_op->d_revalidate)
597 (dentry->d_op->d_revalidate)(dentry, nd);
598 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
600
601 /*
Al Viro4b1ae272010-03-03 12:58:31 -0500602 * If we are still pending, check if we had to handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * a signal. If so we can force a restart..
604 */
Al Viro4b1ae272010-03-03 12:58:31 -0500605 if (ino->flags & AUTOFS_INF_PENDING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /* See if we were interrupted */
607 if (signal_pending(current)) {
608 sigset_t *sigset = &current->pending.signal;
609 if (sigismember (sigset, SIGKILL) ||
610 sigismember (sigset, SIGQUIT) ||
611 sigismember (sigset, SIGINT)) {
Ian Kent90387c92009-12-15 16:45:46 -0800612 if (active)
613 dput(active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 return ERR_PTR(-ERESTARTNOINTR);
615 }
616 }
Al Viro4b1ae272010-03-03 12:58:31 -0500617 if (!oz_mode) {
618 spin_lock(&sbi->fs_lock);
619 ino->flags &= ~AUTOFS_INF_PENDING;
620 spin_unlock(&sbi->fs_lock);
Ian Kent25767372008-07-23 21:30:12 -0700621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
624 /*
Al Viro4b1ae272010-03-03 12:58:31 -0500625 * If this dentry is unhashed, then we shouldn't honour this
626 * lookup. Returning ENOENT here doesn't do the right thing
627 * for all system calls, but it should be OK for the operations
628 * we permit from an autofs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 */
Al Viro4b1ae272010-03-03 12:58:31 -0500630 if (!oz_mode && d_unhashed(dentry)) {
631 /*
632 * A user space application can (and has done in the past)
633 * remove and re-create this directory during the callback.
634 * This can leave us with an unhashed dentry, but a
635 * successful mount! So we need to perform another
636 * cached lookup in case the dentry now exists.
637 */
638 struct dentry *parent = dentry->d_parent;
639 struct dentry *new = d_lookup(parent, &dentry->d_name);
640 if (new != NULL)
641 dentry = new;
642 else
643 dentry = ERR_PTR(-ENOENT);
644
Ian Kent90387c92009-12-15 16:45:46 -0800645 if (active)
646 dput(active);
Al Viro4b1ae272010-03-03 12:58:31 -0500647
Ian Kentc9ffec42007-02-20 13:58:10 -0800648 return dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800649 }
650
Al Viro4b1ae272010-03-03 12:58:31 -0500651 if (active)
652 return active;
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return NULL;
655}
656
657static int autofs4_dir_symlink(struct inode *dir,
658 struct dentry *dentry,
659 const char *symname)
660{
661 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
662 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800663 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 struct inode *inode;
665 char *cp;
666
667 DPRINTK("%s <- %.*s", symname,
668 dentry->d_name.len, dentry->d_name.name);
669
670 if (!autofs4_oz_mode(sbi))
671 return -EACCES;
672
673 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700674 if (!ino)
675 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Al Viro4b1ae272010-03-03 12:58:31 -0500677 autofs4_del_active(dentry);
678
Ian Kentef581a72008-07-23 21:30:13 -0700679 ino->size = strlen(symname);
Ian Kent25767372008-07-23 21:30:12 -0700680 cp = kmalloc(ino->size + 1, GFP_KERNEL);
681 if (!cp) {
682 if (!dentry->d_fsdata)
683 kfree(ino);
684 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
687 strcpy(cp, symname);
688
689 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700690 if (!inode) {
691 kfree(cp);
692 if (!dentry->d_fsdata)
693 kfree(ino);
694 return -ENOMEM;
695 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700696 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 if (dir == dir->i_sb->s_root->d_inode)
699 dentry->d_op = &autofs4_root_dentry_operations;
700 else
701 dentry->d_op = &autofs4_dentry_operations;
702
703 dentry->d_fsdata = ino;
704 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800705 atomic_inc(&ino->count);
706 p_ino = autofs4_dentry_ino(dentry->d_parent);
707 if (p_ino && dentry->d_parent != dentry)
708 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 ino->inode = inode;
710
Ian Kent25767372008-07-23 21:30:12 -0700711 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 dir->i_mtime = CURRENT_TIME;
713
714 return 0;
715}
716
717/*
718 * NOTE!
719 *
720 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
721 * that the file no longer exists. However, doing that means that the
722 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800723 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700724 * We simply d_drop it and add it to a expiring list in the super block,
725 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 *
727 * If a process is blocked on the dentry waiting for the expire to finish,
728 * it will invalidate the dentry and try to mount with a new one.
729 *
730 * Also see autofs4_dir_rmdir()..
731 */
732static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
733{
734 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
735 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800736 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700739 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EACCES;
741
Ian Kent1aff3c82006-03-27 01:14:46 -0800742 if (atomic_dec_and_test(&ino->count)) {
743 p_ino = autofs4_dentry_ino(dentry->d_parent);
744 if (p_ino && dentry->d_parent != dentry)
745 atomic_dec(&p_ino->count);
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 dput(ino->dentry);
748
749 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700750 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 dir->i_mtime = CURRENT_TIME;
753
Ian Kentf50b6f82007-02-20 13:58:10 -0800754 spin_lock(&dcache_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500755 autofs4_add_expiring(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800756 spin_lock(&dentry->d_lock);
757 __d_drop(dentry);
758 spin_unlock(&dentry->d_lock);
759 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 return 0;
762}
763
764static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
765{
766 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
767 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800768 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Ian Kentf50b6f82007-02-20 13:58:10 -0800770 DPRINTK("dentry %p, removing %.*s",
771 dentry, dentry->d_name.len, dentry->d_name.name);
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!autofs4_oz_mode(sbi))
774 return -EACCES;
775
776 spin_lock(&dcache_lock);
777 if (!list_empty(&dentry->d_subdirs)) {
778 spin_unlock(&dcache_lock);
779 return -ENOTEMPTY;
780 }
Al Viro4b1ae272010-03-03 12:58:31 -0500781 autofs4_add_expiring(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 spin_lock(&dentry->d_lock);
783 __d_drop(dentry);
784 spin_unlock(&dentry->d_lock);
785 spin_unlock(&dcache_lock);
786
Ian Kent1aff3c82006-03-27 01:14:46 -0800787 if (atomic_dec_and_test(&ino->count)) {
788 p_ino = autofs4_dentry_ino(dentry->d_parent);
789 if (p_ino && dentry->d_parent != dentry)
790 atomic_dec(&p_ino->count);
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700794 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700797 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 return 0;
800}
801
802static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
803{
804 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
805 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800806 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct inode *inode;
808
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700809 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return -EACCES;
811
812 DPRINTK("dentry %p, creating %.*s",
813 dentry, dentry->d_name.len, dentry->d_name.name);
814
815 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700816 if (!ino)
817 return -ENOMEM;
818
Al Viro4b1ae272010-03-03 12:58:31 -0500819 autofs4_del_active(dentry);
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700822 if (!inode) {
823 if (!dentry->d_fsdata)
824 kfree(ino);
825 return -ENOMEM;
826 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700827 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (dir == dir->i_sb->s_root->d_inode)
830 dentry->d_op = &autofs4_root_dentry_operations;
831 else
832 dentry->d_op = &autofs4_dentry_operations;
833
834 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;
Dave Hansend8c76e62006-09-30 23:29:04 -0700841 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 dir->i_mtime = CURRENT_TIME;
843
844 return 0;
845}
846
847/* Get/set timeout ioctl() operation */
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200848#ifdef CONFIG_COMPAT
849static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
850 compat_ulong_t __user *p)
851{
852 int rv;
853 unsigned long ntimeout;
854
855 if ((rv = get_user(ntimeout, p)) ||
856 (rv = put_user(sbi->exp_timeout/HZ, p)))
857 return rv;
858
859 if (ntimeout > UINT_MAX/HZ)
860 sbi->exp_timeout = 0;
861 else
862 sbi->exp_timeout = ntimeout * HZ;
863
864 return 0;
865}
866#endif
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
869 unsigned long __user *p)
870{
871 int rv;
872 unsigned long ntimeout;
873
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700874 if ((rv = get_user(ntimeout, p)) ||
875 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return rv;
877
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700878 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 sbi->exp_timeout = 0;
880 else
881 sbi->exp_timeout = ntimeout * HZ;
882
883 return 0;
884}
885
886/* Return protocol version */
887static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
888{
889 return put_user(sbi->version, p);
890}
891
892/* Return protocol sub version */
893static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
894{
895 return put_user(sbi->sub_version, p);
896}
897
898/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899* Tells the daemon whether it can umount the autofs mount.
900*/
901static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
902{
903 int status = 0;
904
Ian Kente3474a82006-03-27 01:14:51 -0800905 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 status = 1;
907
908 DPRINTK("returning %d", status);
909
910 status = put_user(status, p);
911
912 return status;
913}
914
915/* Identify autofs4_dentries - this is so we can tell if there's
916 an extra dentry refcount or not. We only hold a refcount on the
917 dentry if its non-negative (ie, d_inode != NULL)
918*/
919int is_autofs4_dentry(struct dentry *dentry)
920{
921 return dentry && dentry->d_inode &&
922 (dentry->d_op == &autofs4_root_dentry_operations ||
923 dentry->d_op == &autofs4_dentry_operations) &&
924 dentry->d_fsdata != NULL;
925}
926
927/*
928 * ioctl()'s on the root directory is the chief method for the daemon to
929 * generate kernel reactions
930 */
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200931static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
932 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
934 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
935 void __user *p = (void __user *)arg;
936
937 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700938 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700940 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
941 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return -ENOTTY;
943
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700944 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return -EPERM;
946
947 switch(cmd) {
948 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
949 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
950 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
951 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
952 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
953 autofs4_catatonic_mode(sbi);
954 return 0;
955 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
956 return autofs4_get_protover(sbi, p);
957 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
958 return autofs4_get_protosubver(sbi, p);
959 case AUTOFS_IOC_SETTIMEOUT:
960 return autofs4_get_set_timeout(sbi, p);
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200961#ifdef CONFIG_COMPAT
962 case AUTOFS_IOC_SETTIMEOUT32:
963 return autofs4_compat_get_set_timeout(sbi, p);
964#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800967 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /* return a single thing to expire */
970 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800971 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* same as above, but can send multiple expires through pipe */
973 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800974 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 default:
977 return -ENOSYS;
978 }
979}
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200980
981static long autofs4_root_ioctl(struct file *filp,
982 unsigned int cmd, unsigned long arg)
983{
984 long ret;
985 struct inode *inode = filp->f_dentry->d_inode;
986
987 lock_kernel();
988 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
989 unlock_kernel();
990
991 return ret;
992}
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200993
994#ifdef CONFIG_COMPAT
995static long autofs4_root_compat_ioctl(struct file *filp,
996 unsigned int cmd, unsigned long arg)
997{
998 struct inode *inode = filp->f_path.dentry->d_inode;
999 int ret;
1000
1001 lock_kernel();
1002 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
1003 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
1004 else
1005 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
1006 (unsigned long)compat_ptr(arg));
1007 unlock_kernel();
1008
1009 return ret;
1010}
1011#endif