blob: bfe3f2eb684d14741d002d61b9a1d8783cc2a566 [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 *);
Ian Kent34ca9592006-03-27 01:14:54 -080038static void *autofs4_follow_link(struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Ian Kent6d5cb922008-07-23 21:30:15 -070040#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
41#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
42
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080043const struct file_operations autofs4_root_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .open = dcache_dir_open,
45 .release = dcache_dir_close,
46 .read = generic_read_dir,
Ian Kentaa55ddf2008-07-23 21:30:29 -070047 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040048 .llseek = dcache_dir_lseek,
Frederic Weisbecker3663df72010-05-19 15:08:17 +020049 .unlocked_ioctl = autofs4_root_ioctl,
Arnd Bergmannc9243f52010-07-04 00:15:07 +020050#ifdef CONFIG_COMPAT
51 .compat_ioctl = autofs4_root_compat_ioctl,
52#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080055const struct file_operations autofs4_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 .open = autofs4_dir_open,
Ian Kentff9cd492008-07-23 21:30:24 -070057 .release = dcache_dir_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 .read = generic_read_dir,
Ian Kentff9cd492008-07-23 21:30:24 -070059 .readdir = dcache_readdir,
Al Viro59af1582008-08-24 07:24:41 -040060 .llseek = dcache_dir_lseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Arjan van de Ven754661f2007-02-12 00:55:38 -080063const struct inode_operations autofs4_indirect_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 .lookup = autofs4_lookup,
65 .unlink = autofs4_dir_unlink,
66 .symlink = autofs4_dir_symlink,
67 .mkdir = autofs4_dir_mkdir,
68 .rmdir = autofs4_dir_rmdir,
69};
70
Arjan van de Ven754661f2007-02-12 00:55:38 -080071const struct inode_operations autofs4_direct_root_inode_operations = {
Ian Kent34ca9592006-03-27 01:14:54 -080072 .lookup = autofs4_lookup,
Ian Kent871f9432006-03-27 01:14:58 -080073 .unlink = autofs4_dir_unlink,
74 .mkdir = autofs4_dir_mkdir,
75 .rmdir = autofs4_dir_rmdir,
Ian Kent34ca9592006-03-27 01:14:54 -080076 .follow_link = autofs4_follow_link,
77};
78
Arjan van de Ven754661f2007-02-12 00:55:38 -080079const struct inode_operations autofs4_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .lookup = autofs4_lookup,
81 .unlink = autofs4_dir_unlink,
82 .symlink = autofs4_dir_symlink,
83 .mkdir = autofs4_dir_mkdir,
84 .rmdir = autofs4_dir_rmdir,
85};
86
Ian Kent4f8427d2009-12-15 16:45:42 -080087static void autofs4_add_active(struct dentry *dentry)
88{
89 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
90 struct autofs_info *ino = autofs4_dentry_ino(dentry);
91 if (ino) {
92 spin_lock(&sbi->lookup_lock);
93 if (!ino->active_count) {
94 if (list_empty(&ino->active))
95 list_add(&ino->active, &sbi->active_list);
96 }
97 ino->active_count++;
98 spin_unlock(&sbi->lookup_lock);
99 }
100 return;
101}
102
103static void autofs4_del_active(struct dentry *dentry)
104{
105 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
106 struct autofs_info *ino = autofs4_dentry_ino(dentry);
107 if (ino) {
108 spin_lock(&sbi->lookup_lock);
109 ino->active_count--;
110 if (!ino->active_count) {
111 if (!list_empty(&ino->active))
112 list_del_init(&ino->active);
113 }
114 spin_unlock(&sbi->lookup_lock);
115 }
116 return;
117}
118
Ian Kent36b64132009-12-15 16:45:44 -0800119static unsigned int autofs4_need_mount(unsigned int flags)
120{
121 unsigned int res = 0;
122 if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
123 res = 1;
124 return res;
125}
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static int autofs4_dir_open(struct inode *inode, struct file *file)
128{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800129 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 DPRINTK("file=%p dentry=%p %.*s",
133 file, dentry, dentry->d_name.len, dentry->d_name.name);
134
135 if (autofs4_oz_mode(sbi))
136 goto out;
137
Ian Kentff9cd492008-07-23 21:30:24 -0700138 /*
139 * An empty directory in an autofs file system is always a
140 * mount point. The daemon must have failed to mount this
141 * during lookup so it doesn't exist. This can happen, for
142 * example, if user space returns an incorrect status for a
143 * mount request. Otherwise we're doing a readdir on the
144 * autofs file system so just let the libfs routines handle
145 * it.
146 */
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100147 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100148 spin_lock(&dentry->d_lock);
Ian Kentc42c7f72009-12-15 16:45:48 -0800149 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100150 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100151 spin_unlock(&autofs4_lock);
Ian Kentff9cd492008-07-23 21:30:24 -0700152 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100154 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100155 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Ian Kentf360ce32006-03-27 01:14:43 -0800157out:
Ian Kentff9cd492008-07-23 21:30:24 -0700158 return dcache_dir_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Al Viro4b1ae272010-03-03 12:58:31 -0500161static int try_to_fill_dentry(struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Ian Kent862b1102006-03-27 01:14:48 -0800163 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800164 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700165 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 DPRINTK("dentry=%p %.*s ino=%p",
168 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
169
Ian Kent718c6042006-03-27 01:14:42 -0800170 /*
171 * Wait for a pending mount, triggering one if there
172 * isn't one already
173 */
Al Viro4b1ae272010-03-03 12:58:31 -0500174 if (dentry->d_inode == NULL) {
175 DPRINTK("waiting for mount name=%.*s",
176 dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Al Viro4b1ae272010-03-03 12:58:31 -0500178 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800179
Al Viro4b1ae272010-03-03 12:58:31 -0500180 DPRINTK("mount done status=%d", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Al Viro4b1ae272010-03-03 12:58:31 -0500182 /* Turn this into a real negative dentry? */
183 if (status == -ENOENT) {
184 spin_lock(&sbi->fs_lock);
185 ino->flags &= ~AUTOFS_INF_PENDING;
186 spin_unlock(&sbi->fs_lock);
187 return status;
188 } else if (status) {
189 /* Return a negative dentry, but leave it "pending" */
190 return status;
191 }
192 /* Trigger mount for path component or follow link */
193 } else if (ino->flags & AUTOFS_INF_PENDING ||
Ian Kentf7422462010-05-10 16:46:08 +0800194 autofs4_need_mount(flags)) {
Al Viro4b1ae272010-03-03 12:58:31 -0500195 DPRINTK("waiting for mount name=%.*s",
196 dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Al Viro4b1ae272010-03-03 12:58:31 -0500198 spin_lock(&sbi->fs_lock);
199 ino->flags |= AUTOFS_INF_PENDING;
200 spin_unlock(&sbi->fs_lock);
201 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
202
203 DPRINTK("mount done status=%d", status);
204
205 if (status) {
206 spin_lock(&sbi->fs_lock);
207 ino->flags &= ~AUTOFS_INF_PENDING;
208 spin_unlock(&sbi->fs_lock);
209 return status;
210 }
211 }
212
213 /* Initialize expiry counter after successful mount */
Dan Carpenter5fc79d82010-08-10 18:02:04 -0700214 ino->last_used = jiffies;
Al Viro4b1ae272010-03-03 12:58:31 -0500215
216 spin_lock(&sbi->fs_lock);
217 ino->flags &= ~AUTOFS_INF_PENDING;
218 spin_unlock(&sbi->fs_lock);
219
220 return 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800221}
222
223/* For autofs direct mounts the follow link triggers the mount */
224static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
225{
226 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kenta5370552006-05-15 09:43:51 -0700227 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent34ca9592006-03-27 01:14:54 -0800228 int oz_mode = autofs4_oz_mode(sbi);
229 unsigned int lookup_type;
230 int status;
231
232 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
233 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
234 nd->flags);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700235 /*
236 * For an expire of a covered direct or offset mount we need
Al Viro9393bd02009-04-18 13:58:15 -0400237 * to break out of follow_down() at the autofs mount trigger
Ian Kent6e60a9a2008-07-23 21:30:27 -0700238 * (d_mounted--), so we can see the expiring flag, and manage
239 * the blocking and following here until the expire is completed.
240 */
241 if (oz_mode) {
242 spin_lock(&sbi->fs_lock);
243 if (ino->flags & AUTOFS_INF_EXPIRING) {
244 spin_unlock(&sbi->fs_lock);
245 /* Follow down to our covering mount. */
Al Viro9393bd02009-04-18 13:58:15 -0400246 if (!follow_down(&nd->path))
Ian Kent6e60a9a2008-07-23 21:30:27 -0700247 goto done;
Ian Kentec6e8c72008-07-23 21:30:28 -0700248 goto follow;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700249 }
250 spin_unlock(&sbi->fs_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800251 goto done;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700252 }
Ian Kent34ca9592006-03-27 01:14:54 -0800253
Ian Kent6e60a9a2008-07-23 21:30:27 -0700254 /* If an expire request is pending everyone must wait. */
Ian Kent06a35982008-07-23 21:30:28 -0700255 autofs4_expire_wait(dentry);
Ian Kent97e74492008-07-23 21:30:26 -0700256
Ian Kent6e60a9a2008-07-23 21:30:27 -0700257 /* We trigger a mount for almost all flags */
Ian Kent36b64132009-12-15 16:45:44 -0800258 lookup_type = autofs4_need_mount(nd->flags);
Ian Kentaa952eb2009-12-15 16:45:45 -0800259 spin_lock(&sbi->fs_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100260 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100261 spin_lock(&dentry->d_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800262 if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100263 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100264 spin_unlock(&autofs4_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800265 spin_unlock(&sbi->fs_lock);
Ian Kentec6e8c72008-07-23 21:30:28 -0700266 goto follow;
Ian Kentaa952eb2009-12-15 16:45:45 -0800267 }
Ian Kent6e60a9a2008-07-23 21:30:27 -0700268
Ian Kent871f9432006-03-27 01:14:58 -0800269 /*
Ian Kent6e60a9a2008-07-23 21:30:27 -0700270 * If the dentry contains directories then it is an autofs
271 * multi-mount with no root mount offset. So don't try to
272 * mount it again.
Ian Kent871f9432006-03-27 01:14:58 -0800273 */
Ian Kentaa952eb2009-12-15 16:45:45 -0800274 if (ino->flags & AUTOFS_INF_PENDING ||
Ian Kentc42c7f72009-12-15 16:45:48 -0800275 (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100276 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100277 spin_unlock(&autofs4_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800278 spin_unlock(&sbi->fs_lock);
Ian Kent871f9432006-03-27 01:14:58 -0800279
Ian Kentf7422462010-05-10 16:46:08 +0800280 status = try_to_fill_dentry(dentry, nd->flags);
Ian Kent871f9432006-03-27 01:14:58 -0800281 if (status)
282 goto out_error;
283
Ian Kent6e60a9a2008-07-23 21:30:27 -0700284 goto follow;
Ian Kent871f9432006-03-27 01:14:58 -0800285 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100286 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100287 spin_unlock(&autofs4_lock);
Ian Kentaa952eb2009-12-15 16:45:45 -0800288 spin_unlock(&sbi->fs_lock);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700289follow:
290 /*
291 * If there is no root mount it must be an autofs
292 * multi-mount with no root offset so we don't need
293 * to follow it.
294 */
295 if (d_mountpoint(dentry)) {
Al Viro9393bd02009-04-18 13:58:15 -0400296 if (!autofs4_follow_mount(&nd->path)) {
Ian Kent6e60a9a2008-07-23 21:30:27 -0700297 status = -ENOENT;
298 goto out_error;
299 }
300 }
Ian Kent871f9432006-03-27 01:14:58 -0800301
Ian Kent34ca9592006-03-27 01:14:54 -0800302done:
303 return NULL;
304
305out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800306 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800307 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310/*
311 * Revalidate is called on every cache lookup. Some of those
312 * cache lookups may actually happen while the dentry is not
313 * yet completely filled in, and revalidate has to delay such
314 * lookups..
315 */
Ian Kent718c6042006-03-27 01:14:42 -0800316static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
Ian Kent718c6042006-03-27 01:14:42 -0800318 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
Al Viro4b1ae272010-03-03 12:58:31 -0500320 int oz_mode = autofs4_oz_mode(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int flags = nd ? nd->flags : 0;
Al Viro4b1ae272010-03-03 12:58:31 -0500322 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Ian Kent213614d2009-12-15 16:45:51 -0800324 /* Pending dentry */
Al Viro4b1ae272010-03-03 12:58:31 -0500325 spin_lock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (autofs4_ispending(dentry)) {
Al Viro4b1ae272010-03-03 12:58:31 -0500327 /* The daemon never causes a mount to trigger */
Ian Kent213614d2009-12-15 16:45:51 -0800328 spin_unlock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500329
330 if (oz_mode)
331 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700332
333 /*
Ian Kent06a35982008-07-23 21:30:28 -0700334 * If the directory has gone away due to an expire
335 * we have been called as ->d_revalidate() and so
336 * we need to return false and proceed to ->lookup().
337 */
338 if (autofs4_expire_wait(dentry) == -EAGAIN)
339 return 0;
340
341 /*
Ian Kentbcdc5e02006-09-27 01:50:44 -0700342 * A zero status is success otherwise we have a
343 * negative error code.
344 */
Al Viro4b1ae272010-03-03 12:58:31 -0500345 status = try_to_fill_dentry(dentry, flags);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700346 if (status == 0)
Ian Kentf50b6f82007-02-20 13:58:10 -0800347 return 1;
348
Ian Kentbcdc5e02006-09-27 01:50:44 -0700349 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Al Viro4b1ae272010-03-03 12:58:31 -0500351 spin_unlock(&sbi->fs_lock);
352
353 /* Negative dentry.. invalidate if "old" */
354 if (dentry->d_inode == NULL)
355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 /* Check for a non-mountpoint directory with no contents */
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100358 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100359 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (S_ISDIR(dentry->d_inode->i_mode) &&
Ian Kentc42c7f72009-12-15 16:45:48 -0800361 !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 DPRINTK("dentry=%p %.*s, emptydir",
363 dentry, dentry->d_name.len, dentry->d_name.name);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100364 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100365 spin_unlock(&autofs4_lock);
Ian Kent97e74492008-07-23 21:30:26 -0700366
Al Viro4b1ae272010-03-03 12:58:31 -0500367 /* The daemon never causes a mount to trigger */
368 if (oz_mode)
369 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700370
Al Viro4b1ae272010-03-03 12:58:31 -0500371 /*
372 * A zero status is success otherwise we have a
373 * negative error code.
374 */
375 status = try_to_fill_dentry(dentry, flags);
376 if (status == 0)
377 return 1;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700378
Al Viro4b1ae272010-03-03 12:58:31 -0500379 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100381 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100382 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return 1;
385}
386
Ian Kent34ca9592006-03-27 01:14:54 -0800387void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct autofs_info *inf;
390
391 DPRINTK("releasing %p", de);
392
393 inf = autofs4_dentry_ino(de);
394 de->d_fsdata = NULL;
395
396 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800397 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
398
Ian Kentf50b6f82007-02-20 13:58:10 -0800399 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700400 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700401 if (!list_empty(&inf->active))
402 list_del(&inf->active);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700403 if (!list_empty(&inf->expiring))
404 list_del(&inf->expiring);
405 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800406 }
407
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700408 inf->dentry = NULL;
409 inf->inode = NULL;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 autofs4_free_ino(inf);
412 }
413}
414
415/* For dentries of directories in the root dir */
Al Viro08f11512009-02-20 05:56:19 +0000416static const struct dentry_operations autofs4_root_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 .d_revalidate = autofs4_revalidate,
418 .d_release = autofs4_dentry_release,
419};
420
421/* For other dentries */
Al Viro08f11512009-02-20 05:56:19 +0000422static const struct dentry_operations autofs4_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 .d_revalidate = autofs4_revalidate,
424 .d_release = autofs4_dentry_release,
425};
426
Ian Kent6510c9d2009-12-15 16:45:47 -0800427static struct dentry *autofs4_lookup_active(struct dentry *dentry)
Ian Kent25767372008-07-23 21:30:12 -0700428{
Ian Kent6510c9d2009-12-15 16:45:47 -0800429 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
430 struct dentry *parent = dentry->d_parent;
431 struct qstr *name = &dentry->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700432 unsigned int len = name->len;
433 unsigned int hash = name->hash;
434 const unsigned char *str = name->name;
435 struct list_head *p, *head;
436
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100437 spin_lock(&autofs4_lock);
Ian Kent25767372008-07-23 21:30:12 -0700438 spin_lock(&sbi->lookup_lock);
439 head = &sbi->active_list;
440 list_for_each(p, head) {
441 struct autofs_info *ino;
Ian Kente4d5ade2009-12-15 16:45:49 -0800442 struct dentry *active;
Ian Kent25767372008-07-23 21:30:12 -0700443 struct qstr *qstr;
444
445 ino = list_entry(p, struct autofs_info, active);
Ian Kente4d5ade2009-12-15 16:45:49 -0800446 active = ino->dentry;
Ian Kent25767372008-07-23 21:30:12 -0700447
Ian Kente4d5ade2009-12-15 16:45:49 -0800448 spin_lock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700449
450 /* Already gone? */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100451 if (active->d_count == 0)
Ian Kent25767372008-07-23 21:30:12 -0700452 goto next;
453
Ian Kente4d5ade2009-12-15 16:45:49 -0800454 qstr = &active->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700455
Ian Kente4d5ade2009-12-15 16:45:49 -0800456 if (active->d_name.hash != hash)
Ian Kent25767372008-07-23 21:30:12 -0700457 goto next;
Ian Kente4d5ade2009-12-15 16:45:49 -0800458 if (active->d_parent != parent)
Ian Kent25767372008-07-23 21:30:12 -0700459 goto next;
460
461 if (qstr->len != len)
462 goto next;
463 if (memcmp(qstr->name, str, len))
464 goto next;
465
Al Viro4b1ae272010-03-03 12:58:31 -0500466 if (d_unhashed(active)) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100467 dget_dlock(active);
Al Viro4b1ae272010-03-03 12:58:31 -0500468 spin_unlock(&active->d_lock);
469 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100470 spin_unlock(&autofs4_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500471 return active;
472 }
Ian Kent25767372008-07-23 21:30:12 -0700473next:
Ian Kente4d5ade2009-12-15 16:45:49 -0800474 spin_unlock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700475 }
476 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100477 spin_unlock(&autofs4_lock);
Ian Kent25767372008-07-23 21:30:12 -0700478
479 return NULL;
480}
481
Ian Kent6510c9d2009-12-15 16:45:47 -0800482static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
Ian Kentf50b6f82007-02-20 13:58:10 -0800483{
Ian Kent6510c9d2009-12-15 16:45:47 -0800484 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
485 struct dentry *parent = dentry->d_parent;
486 struct qstr *name = &dentry->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800487 unsigned int len = name->len;
488 unsigned int hash = name->hash;
489 const unsigned char *str = name->name;
490 struct list_head *p, *head;
491
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100492 spin_lock(&autofs4_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700493 spin_lock(&sbi->lookup_lock);
494 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800495 list_for_each(p, head) {
496 struct autofs_info *ino;
Ian Kentcb4b4922009-12-15 16:45:50 -0800497 struct dentry *expiring;
Ian Kentf50b6f82007-02-20 13:58:10 -0800498 struct qstr *qstr;
499
Ian Kent5f6f4f22008-07-23 21:30:09 -0700500 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentcb4b4922009-12-15 16:45:50 -0800501 expiring = ino->dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800502
Ian Kentcb4b4922009-12-15 16:45:50 -0800503 spin_lock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800504
505 /* Bad luck, we've already been dentry_iput */
Ian Kentcb4b4922009-12-15 16:45:50 -0800506 if (!expiring->d_inode)
Ian Kentf50b6f82007-02-20 13:58:10 -0800507 goto next;
508
Ian Kentcb4b4922009-12-15 16:45:50 -0800509 qstr = &expiring->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800510
Ian Kentcb4b4922009-12-15 16:45:50 -0800511 if (expiring->d_name.hash != hash)
Ian Kentf50b6f82007-02-20 13:58:10 -0800512 goto next;
Ian Kentcb4b4922009-12-15 16:45:50 -0800513 if (expiring->d_parent != parent)
Ian Kentf50b6f82007-02-20 13:58:10 -0800514 goto next;
515
516 if (qstr->len != len)
517 goto next;
518 if (memcmp(qstr->name, str, len))
519 goto next;
520
Al Viro4b1ae272010-03-03 12:58:31 -0500521 if (d_unhashed(expiring)) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100522 dget_dlock(expiring);
Al Viro4b1ae272010-03-03 12:58:31 -0500523 spin_unlock(&expiring->d_lock);
524 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100525 spin_unlock(&autofs4_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500526 return expiring;
527 }
Ian Kentf50b6f82007-02-20 13:58:10 -0800528next:
Ian Kentcb4b4922009-12-15 16:45:50 -0800529 spin_unlock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800530 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700531 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100532 spin_unlock(&autofs4_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800533
534 return NULL;
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/* Lookups in the root directory */
538static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
539{
540 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700541 struct autofs_info *ino;
Ian Kent90387c92009-12-15 16:45:46 -0800542 struct dentry *expiring, *active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int oz_mode;
544
545 DPRINTK("name = %.*s",
546 dentry->d_name.len, dentry->d_name.name);
547
Ian Kent718c6042006-03-27 01:14:42 -0800548 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800550 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700556 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Ian Kent6510c9d2009-12-15 16:45:47 -0800558 active = autofs4_lookup_active(dentry);
Ian Kent90387c92009-12-15 16:45:46 -0800559 if (active) {
560 dentry = active;
Ian Kentaa952eb2009-12-15 16:45:45 -0800561 ino = autofs4_dentry_ino(dentry);
562 } else {
Al Viro4b1ae272010-03-03 12:58:31 -0500563 /*
564 * Mark the dentry incomplete but don't hash it. We do this
565 * to serialize our inode creation operations (symlink and
566 * mkdir) which prevents deadlock during the callback to
567 * the daemon. Subsequent user space lookups for the same
568 * dentry are placed on the wait queue while the daemon
569 * itself is allowed passage unresticted so the create
570 * operation itself can then hash the dentry. Finally,
571 * we check for the hashed dentry and return the newly
572 * hashed dentry.
573 */
Nick Pigginfb045ad2011-01-07 17:49:55 +1100574 d_set_d_op(dentry, &autofs4_root_dentry_operations);
Al Viro4b1ae272010-03-03 12:58:31 -0500575
576 /*
577 * And we need to ensure that the same dentry is used for
578 * all following lookup calls until it is hashed so that
579 * the dentry flags are persistent throughout the request.
580 */
581 ino = autofs4_init_ino(NULL, sbi, 0555);
582 if (!ino)
583 return ERR_PTR(-ENOMEM);
584
585 dentry->d_fsdata = ino;
586 ino->dentry = dentry;
587
588 autofs4_add_active(dentry);
589
590 d_instantiate(dentry, NULL);
Ian Kent25767372008-07-23 21:30:12 -0700591 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (!oz_mode) {
Ian Kent213614d2009-12-15 16:45:51 -0800594 mutex_unlock(&dir->i_mutex);
Al Viro4b1ae272010-03-03 12:58:31 -0500595 expiring = autofs4_lookup_expiring(dentry);
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700596 if (expiring) {
597 /*
598 * If we are racing with expire the request might not
599 * be quite complete but the directory has been removed
600 * so it must have been successful, so just wait for it.
601 */
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700602 autofs4_expire_wait(expiring);
Al Viro4b1ae272010-03-03 12:58:31 -0500603 autofs4_del_expiring(expiring);
Ian Kent8f63aaa8b2009-03-31 15:24:45 -0700604 dput(expiring);
605 }
Al Viro4b1ae272010-03-03 12:58:31 -0500606
Ian Kent213614d2009-12-15 16:45:51 -0800607 spin_lock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500608 ino->flags |= AUTOFS_INF_PENDING;
Ian Kent213614d2009-12-15 16:45:51 -0800609 spin_unlock(&sbi->fs_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500610 if (dentry->d_op && dentry->d_op->d_revalidate)
611 (dentry->d_op->d_revalidate)(dentry, nd);
612 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
615 /*
Al Viro4b1ae272010-03-03 12:58:31 -0500616 * If we are still pending, check if we had to handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 * a signal. If so we can force a restart..
618 */
Al Viro4b1ae272010-03-03 12:58:31 -0500619 if (ino->flags & AUTOFS_INF_PENDING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* See if we were interrupted */
621 if (signal_pending(current)) {
622 sigset_t *sigset = &current->pending.signal;
623 if (sigismember (sigset, SIGKILL) ||
624 sigismember (sigset, SIGQUIT) ||
625 sigismember (sigset, SIGINT)) {
Ian Kent90387c92009-12-15 16:45:46 -0800626 if (active)
627 dput(active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return ERR_PTR(-ERESTARTNOINTR);
629 }
630 }
Al Viro4b1ae272010-03-03 12:58:31 -0500631 if (!oz_mode) {
632 spin_lock(&sbi->fs_lock);
633 ino->flags &= ~AUTOFS_INF_PENDING;
634 spin_unlock(&sbi->fs_lock);
Ian Kent25767372008-07-23 21:30:12 -0700635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
638 /*
Al Viro4b1ae272010-03-03 12:58:31 -0500639 * If this dentry is unhashed, then we shouldn't honour this
640 * lookup. Returning ENOENT here doesn't do the right thing
641 * for all system calls, but it should be OK for the operations
642 * we permit from an autofs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
Al Viro4b1ae272010-03-03 12:58:31 -0500644 if (!oz_mode && d_unhashed(dentry)) {
645 /*
646 * A user space application can (and has done in the past)
647 * remove and re-create this directory during the callback.
648 * This can leave us with an unhashed dentry, but a
649 * successful mount! So we need to perform another
650 * cached lookup in case the dentry now exists.
651 */
652 struct dentry *parent = dentry->d_parent;
653 struct dentry *new = d_lookup(parent, &dentry->d_name);
654 if (new != NULL)
655 dentry = new;
656 else
657 dentry = ERR_PTR(-ENOENT);
658
Ian Kent90387c92009-12-15 16:45:46 -0800659 if (active)
660 dput(active);
Al Viro4b1ae272010-03-03 12:58:31 -0500661
Ian Kentc9ffec42007-02-20 13:58:10 -0800662 return dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800663 }
664
Al Viro4b1ae272010-03-03 12:58:31 -0500665 if (active)
666 return active;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return NULL;
669}
670
671static int autofs4_dir_symlink(struct inode *dir,
672 struct dentry *dentry,
673 const char *symname)
674{
675 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
676 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800677 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 struct inode *inode;
679 char *cp;
680
681 DPRINTK("%s <- %.*s", symname,
682 dentry->d_name.len, dentry->d_name.name);
683
684 if (!autofs4_oz_mode(sbi))
685 return -EACCES;
686
687 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700688 if (!ino)
689 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Al Viro4b1ae272010-03-03 12:58:31 -0500691 autofs4_del_active(dentry);
692
Ian Kentef581a72008-07-23 21:30:13 -0700693 ino->size = strlen(symname);
Ian Kent25767372008-07-23 21:30:12 -0700694 cp = kmalloc(ino->size + 1, GFP_KERNEL);
695 if (!cp) {
696 if (!dentry->d_fsdata)
697 kfree(ino);
698 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700
701 strcpy(cp, symname);
702
703 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700704 if (!inode) {
705 kfree(cp);
706 if (!dentry->d_fsdata)
707 kfree(ino);
708 return -ENOMEM;
709 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700710 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 if (dir == dir->i_sb->s_root->d_inode)
Nick Pigginfb045ad2011-01-07 17:49:55 +1100713 d_set_d_op(dentry, &autofs4_root_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 else
Nick Pigginfb045ad2011-01-07 17:49:55 +1100715 d_set_d_op(dentry, &autofs4_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 dentry->d_fsdata = ino;
718 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800719 atomic_inc(&ino->count);
720 p_ino = autofs4_dentry_ino(dentry->d_parent);
721 if (p_ino && dentry->d_parent != dentry)
722 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ino->inode = inode;
724
Ian Kent25767372008-07-23 21:30:12 -0700725 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 dir->i_mtime = CURRENT_TIME;
727
728 return 0;
729}
730
731/*
732 * NOTE!
733 *
734 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
735 * that the file no longer exists. However, doing that means that the
736 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800737 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700738 * We simply d_drop it and add it to a expiring list in the super block,
739 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 *
741 * If a process is blocked on the dentry waiting for the expire to finish,
742 * it will invalidate the dentry and try to mount with a new one.
743 *
744 * Also see autofs4_dir_rmdir()..
745 */
746static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
747{
748 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
749 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800750 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700753 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return -EACCES;
755
Ian Kent1aff3c82006-03-27 01:14:46 -0800756 if (atomic_dec_and_test(&ino->count)) {
757 p_ino = autofs4_dentry_ino(dentry->d_parent);
758 if (p_ino && dentry->d_parent != dentry)
759 atomic_dec(&p_ino->count);
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 dput(ino->dentry);
762
763 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700764 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 dir->i_mtime = CURRENT_TIME;
767
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100768 spin_lock(&autofs4_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500769 autofs4_add_expiring(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800770 spin_lock(&dentry->d_lock);
771 __d_drop(dentry);
772 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100773 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 return 0;
776}
777
778static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
779{
780 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
781 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800782 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Ian Kentf50b6f82007-02-20 13:58:10 -0800784 DPRINTK("dentry %p, removing %.*s",
785 dentry, dentry->d_name.len, dentry->d_name.name);
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!autofs4_oz_mode(sbi))
788 return -EACCES;
789
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100790 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100791 spin_lock(&sbi->lookup_lock);
792 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100794 spin_unlock(&dentry->d_lock);
795 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100796 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return -ENOTEMPTY;
798 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100799 __autofs4_add_expiring(dentry);
800 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 __d_drop(dentry);
802 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100803 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Ian Kent1aff3c82006-03-27 01:14:46 -0800805 if (atomic_dec_and_test(&ino->count)) {
806 p_ino = autofs4_dentry_ino(dentry->d_parent);
807 if (p_ino && dentry->d_parent != dentry)
808 atomic_dec(&p_ino->count);
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700812 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700815 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 return 0;
818}
819
820static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
821{
822 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
823 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800824 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 struct inode *inode;
826
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700827 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return -EACCES;
829
830 DPRINTK("dentry %p, creating %.*s",
831 dentry, dentry->d_name.len, dentry->d_name.name);
832
833 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700834 if (!ino)
835 return -ENOMEM;
836
Al Viro4b1ae272010-03-03 12:58:31 -0500837 autofs4_del_active(dentry);
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700840 if (!inode) {
841 if (!dentry->d_fsdata)
842 kfree(ino);
843 return -ENOMEM;
844 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700845 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 if (dir == dir->i_sb->s_root->d_inode)
Nick Pigginfb045ad2011-01-07 17:49:55 +1100848 d_set_d_op(dentry, &autofs4_root_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 else
Nick Pigginfb045ad2011-01-07 17:49:55 +1100850 d_set_d_op(dentry, &autofs4_dentry_operations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 dentry->d_fsdata = ino;
853 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800854 atomic_inc(&ino->count);
855 p_ino = autofs4_dentry_ino(dentry->d_parent);
856 if (p_ino && dentry->d_parent != dentry)
857 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700859 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 dir->i_mtime = CURRENT_TIME;
861
862 return 0;
863}
864
865/* Get/set timeout ioctl() operation */
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200866#ifdef CONFIG_COMPAT
867static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
868 compat_ulong_t __user *p)
869{
870 int rv;
871 unsigned long ntimeout;
872
873 if ((rv = get_user(ntimeout, p)) ||
874 (rv = put_user(sbi->exp_timeout/HZ, p)))
875 return rv;
876
877 if (ntimeout > UINT_MAX/HZ)
878 sbi->exp_timeout = 0;
879 else
880 sbi->exp_timeout = ntimeout * HZ;
881
882 return 0;
883}
884#endif
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
887 unsigned long __user *p)
888{
889 int rv;
890 unsigned long ntimeout;
891
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700892 if ((rv = get_user(ntimeout, p)) ||
893 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return rv;
895
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700896 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 sbi->exp_timeout = 0;
898 else
899 sbi->exp_timeout = ntimeout * HZ;
900
901 return 0;
902}
903
904/* Return protocol version */
905static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
906{
907 return put_user(sbi->version, p);
908}
909
910/* Return protocol sub version */
911static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
912{
913 return put_user(sbi->sub_version, p);
914}
915
916/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917* Tells the daemon whether it can umount the autofs mount.
918*/
919static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
920{
921 int status = 0;
922
Ian Kente3474a82006-03-27 01:14:51 -0800923 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 status = 1;
925
926 DPRINTK("returning %d", status);
927
928 status = put_user(status, p);
929
930 return status;
931}
932
933/* Identify autofs4_dentries - this is so we can tell if there's
934 an extra dentry refcount or not. We only hold a refcount on the
935 dentry if its non-negative (ie, d_inode != NULL)
936*/
937int is_autofs4_dentry(struct dentry *dentry)
938{
939 return dentry && dentry->d_inode &&
940 (dentry->d_op == &autofs4_root_dentry_operations ||
941 dentry->d_op == &autofs4_dentry_operations) &&
942 dentry->d_fsdata != NULL;
943}
944
945/*
946 * ioctl()'s on the root directory is the chief method for the daemon to
947 * generate kernel reactions
948 */
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200949static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
950 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
952 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
953 void __user *p = (void __user *)arg;
954
955 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700956 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700958 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
959 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return -ENOTTY;
961
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700962 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return -EPERM;
964
965 switch(cmd) {
966 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
967 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
968 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
969 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
970 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
971 autofs4_catatonic_mode(sbi);
972 return 0;
973 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
974 return autofs4_get_protover(sbi, p);
975 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
976 return autofs4_get_protosubver(sbi, p);
977 case AUTOFS_IOC_SETTIMEOUT:
978 return autofs4_get_set_timeout(sbi, p);
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200979#ifdef CONFIG_COMPAT
980 case AUTOFS_IOC_SETTIMEOUT32:
981 return autofs4_compat_get_set_timeout(sbi, p);
982#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800985 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 /* return a single thing to expire */
988 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800989 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /* same as above, but can send multiple expires through pipe */
991 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800992 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 default:
995 return -ENOSYS;
996 }
997}
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200998
999static long autofs4_root_ioctl(struct file *filp,
1000 unsigned int cmd, unsigned long arg)
1001{
Frederic Weisbecker3663df72010-05-19 15:08:17 +02001002 struct inode *inode = filp->f_dentry->d_inode;
Ian Kentde47de72010-12-07 13:04:00 +08001003 return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
Frederic Weisbecker3663df72010-05-19 15:08:17 +02001004}
Arnd Bergmannc9243f52010-07-04 00:15:07 +02001005
1006#ifdef CONFIG_COMPAT
1007static long autofs4_root_compat_ioctl(struct file *filp,
1008 unsigned int cmd, unsigned long arg)
1009{
1010 struct inode *inode = filp->f_path.dentry->d_inode;
1011 int ret;
1012
Arnd Bergmannc9243f52010-07-04 00:15:07 +02001013 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
1014 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
1015 else
1016 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
1017 (unsigned long)compat_ptr(arg));
Arnd Bergmannc9243f52010-07-04 00:15:07 +02001018
1019 return ret;
1020}
1021#endif