blob: 52af410a831dc09ec95adccb9db2005754053e5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- c -*- --------------------------------------------------------------- *
2 *
3 * linux/fs/autofs/root.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
Ian Kent34ca9592006-03-27 01:14:54 -08007 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This file is part of the Linux kernel and is made available under
10 * the terms of the GNU General Public License, version 2, or at your
11 * option, any later version, incorporated herein by reference.
12 *
13 * ------------------------------------------------------------------------- */
14
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080015#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/errno.h>
17#include <linux/stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/param.h>
20#include <linux/time.h>
Arnd Bergmannc9243f52010-07-04 00:15:07 +020021#include <linux/compat.h>
Arnd Bergmann00e300e2010-09-14 23:00:34 +020022#include <linux/mutex.h>
Arnd Bergmannc9243f52010-07-04 00:15:07 +020023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "autofs_i.h"
25
Nick Pigginb5c84bf2011-01-07 17:49:38 +110026DEFINE_SPINLOCK(autofs4_lock);
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
29static int autofs4_dir_unlink(struct inode *,struct dentry *);
30static int autofs4_dir_rmdir(struct inode *,struct dentry *);
31static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
Frederic Weisbecker3663df72010-05-19 15:08:17 +020032static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
Felipe Contreras5a44a732010-09-24 15:22:23 +020033#ifdef CONFIG_COMPAT
Arnd Bergmannc9243f52010-07-04 00:15:07 +020034static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
Felipe Contreras5a44a732010-09-24 15:22:23 +020035#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static int autofs4_dir_open(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
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_dir_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
Ian Kent4f8427d2009-12-15 16:45:42 -080067static void autofs4_add_active(struct dentry *dentry)
68{
69 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
70 struct autofs_info *ino = autofs4_dentry_ino(dentry);
71 if (ino) {
72 spin_lock(&sbi->lookup_lock);
73 if (!ino->active_count) {
74 if (list_empty(&ino->active))
75 list_add(&ino->active, &sbi->active_list);
76 }
77 ino->active_count++;
78 spin_unlock(&sbi->lookup_lock);
79 }
80 return;
81}
82
83static void autofs4_del_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 ino->active_count--;
90 if (!ino->active_count) {
91 if (!list_empty(&ino->active))
92 list_del_init(&ino->active);
93 }
94 spin_unlock(&sbi->lookup_lock);
95 }
96 return;
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int autofs4_dir_open(struct inode *inode, struct file *file)
100{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800101 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 DPRINTK("file=%p dentry=%p %.*s",
105 file, dentry, dentry->d_name.len, dentry->d_name.name);
106
107 if (autofs4_oz_mode(sbi))
108 goto out;
109
Ian Kentff9cd492008-07-23 21:30:24 -0700110 /*
111 * An empty directory in an autofs file system is always a
112 * mount point. The daemon must have failed to mount this
113 * during lookup so it doesn't exist. This can happen, for
114 * example, if user space returns an incorrect status for a
115 * mount request. Otherwise we're doing a readdir on the
116 * autofs file system so just let the libfs routines handle
117 * it.
118 */
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100119 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100120 spin_lock(&dentry->d_lock);
Ian Kentc42c7f72009-12-15 16:45:48 -0800121 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100122 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100123 spin_unlock(&autofs4_lock);
Ian Kentff9cd492008-07-23 21:30:24 -0700124 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100126 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100127 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Ian Kentf360ce32006-03-27 01:14:43 -0800129out:
Ian Kentff9cd492008-07-23 21:30:24 -0700130 return dcache_dir_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
Ian Kent34ca9592006-03-27 01:14:54 -0800133void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 struct autofs_info *inf;
136
137 DPRINTK("releasing %p", de);
138
139 inf = autofs4_dentry_ino(de);
140 de->d_fsdata = NULL;
141
142 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800143 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
144
Ian Kentf50b6f82007-02-20 13:58:10 -0800145 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700146 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700147 if (!list_empty(&inf->active))
148 list_del(&inf->active);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700149 if (!list_empty(&inf->expiring))
150 list_del(&inf->expiring);
151 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800152 }
153
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700154 inf->dentry = NULL;
155 inf->inode = NULL;
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 autofs4_free_ino(inf);
158 }
159}
160
161/* For dentries of directories in the root dir */
Al Viro08f11512009-02-20 05:56:19 +0000162static const struct dentry_operations autofs4_root_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 .d_release = autofs4_dentry_release,
164};
165
166/* For other dentries */
Al Viro08f11512009-02-20 05:56:19 +0000167static const struct dentry_operations autofs4_dentry_operations = {
Ian Kent10584212011-01-14 18:45:58 +0000168 .d_automount = autofs4_d_automount,
Ian Kentb5b80172011-01-14 18:46:03 +0000169 .d_manage = autofs4_d_manage,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 .d_release = autofs4_dentry_release,
171};
172
Ian Kent6510c9d2009-12-15 16:45:47 -0800173static struct dentry *autofs4_lookup_active(struct dentry *dentry)
Ian Kent25767372008-07-23 21:30:12 -0700174{
Ian Kent6510c9d2009-12-15 16:45:47 -0800175 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
176 struct dentry *parent = dentry->d_parent;
177 struct qstr *name = &dentry->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700178 unsigned int len = name->len;
179 unsigned int hash = name->hash;
180 const unsigned char *str = name->name;
181 struct list_head *p, *head;
182
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100183 spin_lock(&autofs4_lock);
Ian Kent25767372008-07-23 21:30:12 -0700184 spin_lock(&sbi->lookup_lock);
185 head = &sbi->active_list;
186 list_for_each(p, head) {
187 struct autofs_info *ino;
Ian Kente4d5ade2009-12-15 16:45:49 -0800188 struct dentry *active;
Ian Kent25767372008-07-23 21:30:12 -0700189 struct qstr *qstr;
190
191 ino = list_entry(p, struct autofs_info, active);
Ian Kente4d5ade2009-12-15 16:45:49 -0800192 active = ino->dentry;
Ian Kent25767372008-07-23 21:30:12 -0700193
Ian Kente4d5ade2009-12-15 16:45:49 -0800194 spin_lock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700195
196 /* Already gone? */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100197 if (active->d_count == 0)
Ian Kent25767372008-07-23 21:30:12 -0700198 goto next;
199
Ian Kente4d5ade2009-12-15 16:45:49 -0800200 qstr = &active->d_name;
Ian Kent25767372008-07-23 21:30:12 -0700201
Ian Kente4d5ade2009-12-15 16:45:49 -0800202 if (active->d_name.hash != hash)
Ian Kent25767372008-07-23 21:30:12 -0700203 goto next;
Ian Kente4d5ade2009-12-15 16:45:49 -0800204 if (active->d_parent != parent)
Ian Kent25767372008-07-23 21:30:12 -0700205 goto next;
206
207 if (qstr->len != len)
208 goto next;
209 if (memcmp(qstr->name, str, len))
210 goto next;
211
Al Viro4b1ae272010-03-03 12:58:31 -0500212 if (d_unhashed(active)) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100213 dget_dlock(active);
Al Viro4b1ae272010-03-03 12:58:31 -0500214 spin_unlock(&active->d_lock);
215 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100216 spin_unlock(&autofs4_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500217 return active;
218 }
Ian Kent25767372008-07-23 21:30:12 -0700219next:
Ian Kente4d5ade2009-12-15 16:45:49 -0800220 spin_unlock(&active->d_lock);
Ian Kent25767372008-07-23 21:30:12 -0700221 }
222 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100223 spin_unlock(&autofs4_lock);
Ian Kent25767372008-07-23 21:30:12 -0700224
225 return NULL;
226}
227
Ian Kent6510c9d2009-12-15 16:45:47 -0800228static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
Ian Kentf50b6f82007-02-20 13:58:10 -0800229{
Ian Kent6510c9d2009-12-15 16:45:47 -0800230 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
231 struct dentry *parent = dentry->d_parent;
232 struct qstr *name = &dentry->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800233 unsigned int len = name->len;
234 unsigned int hash = name->hash;
235 const unsigned char *str = name->name;
236 struct list_head *p, *head;
237
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100238 spin_lock(&autofs4_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700239 spin_lock(&sbi->lookup_lock);
240 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800241 list_for_each(p, head) {
242 struct autofs_info *ino;
Ian Kentcb4b4922009-12-15 16:45:50 -0800243 struct dentry *expiring;
Ian Kentf50b6f82007-02-20 13:58:10 -0800244 struct qstr *qstr;
245
Ian Kent5f6f4f22008-07-23 21:30:09 -0700246 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentcb4b4922009-12-15 16:45:50 -0800247 expiring = ino->dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800248
Ian Kentcb4b4922009-12-15 16:45:50 -0800249 spin_lock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800250
251 /* Bad luck, we've already been dentry_iput */
Ian Kentcb4b4922009-12-15 16:45:50 -0800252 if (!expiring->d_inode)
Ian Kentf50b6f82007-02-20 13:58:10 -0800253 goto next;
254
Ian Kentcb4b4922009-12-15 16:45:50 -0800255 qstr = &expiring->d_name;
Ian Kentf50b6f82007-02-20 13:58:10 -0800256
Ian Kentcb4b4922009-12-15 16:45:50 -0800257 if (expiring->d_name.hash != hash)
Ian Kentf50b6f82007-02-20 13:58:10 -0800258 goto next;
Ian Kentcb4b4922009-12-15 16:45:50 -0800259 if (expiring->d_parent != parent)
Ian Kentf50b6f82007-02-20 13:58:10 -0800260 goto next;
261
262 if (qstr->len != len)
263 goto next;
264 if (memcmp(qstr->name, str, len))
265 goto next;
266
Al Viro4b1ae272010-03-03 12:58:31 -0500267 if (d_unhashed(expiring)) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100268 dget_dlock(expiring);
Al Viro4b1ae272010-03-03 12:58:31 -0500269 spin_unlock(&expiring->d_lock);
270 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100271 spin_unlock(&autofs4_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500272 return expiring;
273 }
Ian Kentf50b6f82007-02-20 13:58:10 -0800274next:
Ian Kentcb4b4922009-12-15 16:45:50 -0800275 spin_unlock(&expiring->d_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800276 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700277 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100278 spin_unlock(&autofs4_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800279
280 return NULL;
281}
282
Ian Kent10584212011-01-14 18:45:58 +0000283static int autofs4_mount_wait(struct dentry *dentry)
284{
285 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
286 struct autofs_info *ino = autofs4_dentry_ino(dentry);
287 int status;
288
289 if (ino->flags & AUTOFS_INF_PENDING) {
290 DPRINTK("waiting for mount name=%.*s",
291 dentry->d_name.len, dentry->d_name.name);
292 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
293 DPRINTK("mount wait done status=%d", status);
294 ino->last_used = jiffies;
295 return status;
296 }
297 return 0;
298}
299
300static int do_expire_wait(struct dentry *dentry)
301{
302 struct dentry *expiring;
303
304 expiring = autofs4_lookup_expiring(dentry);
305 if (!expiring)
306 return autofs4_expire_wait(dentry);
307 else {
308 /*
309 * If we are racing with expire the request might not
310 * be quite complete, but the directory has been removed
311 * so it must have been successful, just wait for it.
312 */
313 autofs4_expire_wait(expiring);
314 autofs4_del_expiring(expiring);
315 dput(expiring);
316 }
317 return 0;
318}
319
320static struct dentry *autofs4_mountpoint_changed(struct path *path)
321{
322 struct dentry *dentry = path->dentry;
323 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
324
325 /*
326 * If this is an indirect mount the dentry could have gone away
327 * as a result of an expire and a new one created.
328 */
329 if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
330 struct dentry *parent = dentry->d_parent;
331 struct dentry *new = d_lookup(parent, &dentry->d_name);
332 if (!new)
333 return NULL;
334 dput(path->dentry);
335 path->dentry = new;
336 }
337 return path->dentry;
338}
339
340struct vfsmount *autofs4_d_automount(struct path *path)
341{
342 struct dentry *dentry = path->dentry;
343 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
344 struct autofs_info *ino = autofs4_dentry_ino(dentry);
345 int status;
346
347 DPRINTK("dentry=%p %.*s",
348 dentry, dentry->d_name.len, dentry->d_name.name);
349
Ian Kentb5b80172011-01-14 18:46:03 +0000350 /*
351 * Someone may have manually umounted this or it was a submount
352 * that has gone away.
353 */
354 spin_lock(&dentry->d_lock);
355 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
356 if (!(dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
357 (dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
358 __managed_dentry_set_transit(path->dentry);
359 }
360 spin_unlock(&dentry->d_lock);
361
Ian Kent10584212011-01-14 18:45:58 +0000362 /* The daemon never triggers a mount. */
363 if (autofs4_oz_mode(sbi))
364 return NULL;
365
366 /*
367 * If an expire request is pending everyone must wait.
368 * If the expire fails we're still mounted so continue
369 * the follow and return. A return of -EAGAIN (which only
370 * happens with indirect mounts) means the expire completed
371 * and the directory was removed, so just go ahead and try
372 * the mount.
373 */
374 status = do_expire_wait(dentry);
375 if (status && status != -EAGAIN)
376 return NULL;
377
378 /* Callback to the daemon to perform the mount or wait */
379 spin_lock(&sbi->fs_lock);
380 if (ino->flags & AUTOFS_INF_PENDING) {
381 spin_unlock(&sbi->fs_lock);
382 status = autofs4_mount_wait(dentry);
383 if (status)
384 return ERR_PTR(status);
385 spin_lock(&sbi->fs_lock);
386 goto done;
387 }
388
389 /*
390 * If the dentry is a symlink it's equivalent to a directory
Ian Kentb5b80172011-01-14 18:46:03 +0000391 * having d_mountpoint() true, so there's no need to call back
Ian Kent10584212011-01-14 18:45:58 +0000392 * to the daemon.
393 */
394 if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))
395 goto done;
Ian Kentb5b80172011-01-14 18:46:03 +0000396 if (!d_mountpoint(dentry)) {
397 /*
398 * It's possible that user space hasn't removed directories
399 * after umounting a rootless multi-mount, although it
400 * should. For v5 have_submounts() is sufficient to handle
401 * this because the leaves of the directory tree under the
402 * mount never trigger mounts themselves (they have an autofs
403 * trigger mount mounted on them). But v4 pseudo direct mounts
404 * do need the leaves to to trigger mounts. In this case we
405 * have no choice but to use the list_empty() check and
406 * require user space behave.
407 */
408 if (sbi->version > 4) {
409 if (have_submounts(dentry))
410 goto done;
411 } else {
412 spin_lock(&dentry->d_lock);
413 if (!list_empty(&dentry->d_subdirs)) {
414 spin_unlock(&dentry->d_lock);
415 goto done;
416 }
417 spin_unlock(&dentry->d_lock);
418 }
Ian Kent10584212011-01-14 18:45:58 +0000419 ino->flags |= AUTOFS_INF_PENDING;
Ian Kent10584212011-01-14 18:45:58 +0000420 spin_unlock(&sbi->fs_lock);
421 status = autofs4_mount_wait(dentry);
422 if (status)
423 return ERR_PTR(status);
424 spin_lock(&sbi->fs_lock);
425 ino->flags &= ~AUTOFS_INF_PENDING;
Ian Kent10584212011-01-14 18:45:58 +0000426 }
Ian Kent10584212011-01-14 18:45:58 +0000427done:
Ian Kentb5b80172011-01-14 18:46:03 +0000428 if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
429 /*
430 * Any needed mounting has been completed and the path updated
431 * so turn this into a normal dentry so we don't continually
432 * call ->d_automount() and ->d_manage().
433 */
434 spin_lock(&dentry->d_lock);
435 __managed_dentry_clear_transit(dentry);
436 /*
437 * Only clear DMANAGED_AUTOMOUNT for rootless multi-mounts and
438 * symlinks as in all other cases the dentry will be covered by
439 * an actual mount so ->d_automount() won't be called during
440 * the follow.
441 */
442 if ((!d_mountpoint(dentry) &&
443 !list_empty(&dentry->d_subdirs)) ||
444 (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)))
445 __managed_dentry_clear_automount(dentry);
446 spin_unlock(&dentry->d_lock);
447 }
Ian Kent10584212011-01-14 18:45:58 +0000448 spin_unlock(&sbi->fs_lock);
449
450 /* Mount succeeded, check if we ended up with a new dentry */
451 dentry = autofs4_mountpoint_changed(path);
452 if (!dentry)
453 return ERR_PTR(-ENOENT);
454
455 return NULL;
456}
457
Ian Kentb5b80172011-01-14 18:46:03 +0000458int autofs4_d_manage(struct dentry *dentry, bool mounting_here)
459{
460 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
461
462 DPRINTK("dentry=%p %.*s",
463 dentry, dentry->d_name.len, dentry->d_name.name);
464
465 /* The daemon never waits. */
466 if (autofs4_oz_mode(sbi) || mounting_here) {
467 if (!d_mountpoint(dentry))
468 return -EISDIR;
469 return 0;
470 }
471
472 /* Wait for pending expires */
473 do_expire_wait(dentry);
474
475 /*
476 * This dentry may be under construction so wait on mount
477 * completion.
478 */
479 return autofs4_mount_wait(dentry);
480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482/* Lookups in the root directory */
483static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
484{
485 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700486 struct autofs_info *ino;
Ian Kent10584212011-01-14 18:45:58 +0000487 struct dentry *active;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Ian Kent10584212011-01-14 18:45:58 +0000489 DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Ian Kent718c6042006-03-27 01:14:42 -0800491 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800493 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 sbi = autofs4_sbi(dir->i_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Ian Kent10584212011-01-14 18:45:58 +0000498 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Ian Kent6510c9d2009-12-15 16:45:47 -0800500 active = autofs4_lookup_active(dentry);
Ian Kent90387c92009-12-15 16:45:46 -0800501 if (active) {
Ian Kent10584212011-01-14 18:45:58 +0000502 return active;
Ian Kentaa952eb2009-12-15 16:45:45 -0800503 } else {
Nick Pigginfb045ad2011-01-07 17:49:55 +1100504 d_set_d_op(dentry, &autofs4_root_dentry_operations);
Al Viro4b1ae272010-03-03 12:58:31 -0500505
506 /*
Ian Kent10584212011-01-14 18:45:58 +0000507 * A dentry that is not within the root can never trigger a
508 * mount operation, unless the directory already exists, so we
509 * can return fail immediately. The daemon however does need
510 * to create directories within the file system.
Al Viro4b1ae272010-03-03 12:58:31 -0500511 */
Ian Kent10584212011-01-14 18:45:58 +0000512 if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
513 return ERR_PTR(-ENOENT);
514
515 /* Mark entries in the root as mount triggers */
516 if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent)) {
517 d_set_d_op(dentry, &autofs4_dentry_operations);
Ian Kentb5b80172011-01-14 18:46:03 +0000518 __managed_dentry_set_managed(dentry);
Ian Kent10584212011-01-14 18:45:58 +0000519 }
520
Al Viro4b1ae272010-03-03 12:58:31 -0500521 ino = autofs4_init_ino(NULL, sbi, 0555);
522 if (!ino)
523 return ERR_PTR(-ENOMEM);
524
525 dentry->d_fsdata = ino;
526 ino->dentry = dentry;
527
528 autofs4_add_active(dentry);
529
530 d_instantiate(dentry, NULL);
Ian Kent25767372008-07-23 21:30:12 -0700531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return NULL;
533}
534
535static int autofs4_dir_symlink(struct inode *dir,
536 struct dentry *dentry,
537 const char *symname)
538{
539 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
540 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800541 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct inode *inode;
543 char *cp;
544
545 DPRINTK("%s <- %.*s", symname,
546 dentry->d_name.len, dentry->d_name.name);
547
548 if (!autofs4_oz_mode(sbi))
549 return -EACCES;
550
551 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700552 if (!ino)
553 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Al Viro4b1ae272010-03-03 12:58:31 -0500555 autofs4_del_active(dentry);
556
Ian Kentef581a72008-07-23 21:30:13 -0700557 ino->size = strlen(symname);
Ian Kent25767372008-07-23 21:30:12 -0700558 cp = kmalloc(ino->size + 1, GFP_KERNEL);
559 if (!cp) {
560 if (!dentry->d_fsdata)
561 kfree(ino);
562 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 strcpy(cp, symname);
566
567 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700568 if (!inode) {
569 kfree(cp);
570 if (!dentry->d_fsdata)
571 kfree(ino);
572 return -ENOMEM;
573 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700574 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 dentry->d_fsdata = ino;
577 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800578 atomic_inc(&ino->count);
579 p_ino = autofs4_dentry_ino(dentry->d_parent);
580 if (p_ino && dentry->d_parent != dentry)
581 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ino->inode = inode;
583
Ian Kent25767372008-07-23 21:30:12 -0700584 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 dir->i_mtime = CURRENT_TIME;
586
587 return 0;
588}
589
590/*
591 * NOTE!
592 *
593 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
594 * that the file no longer exists. However, doing that means that the
595 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800596 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700597 * We simply d_drop it and add it to a expiring list in the super block,
598 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 *
600 * If a process is blocked on the dentry waiting for the expire to finish,
601 * it will invalidate the dentry and try to mount with a new one.
602 *
603 * Also see autofs4_dir_rmdir()..
604 */
605static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
606{
607 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
608 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800609 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700612 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return -EACCES;
614
Ian Kent1aff3c82006-03-27 01:14:46 -0800615 if (atomic_dec_and_test(&ino->count)) {
616 p_ino = autofs4_dentry_ino(dentry->d_parent);
617 if (p_ino && dentry->d_parent != dentry)
618 atomic_dec(&p_ino->count);
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 dput(ino->dentry);
621
622 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700623 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 dir->i_mtime = CURRENT_TIME;
626
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100627 spin_lock(&autofs4_lock);
Al Viro4b1ae272010-03-03 12:58:31 -0500628 autofs4_add_expiring(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800629 spin_lock(&dentry->d_lock);
630 __d_drop(dentry);
631 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100632 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 return 0;
635}
636
637static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
638{
639 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
640 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800641 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Ian Kentf50b6f82007-02-20 13:58:10 -0800643 DPRINTK("dentry %p, removing %.*s",
644 dentry, dentry->d_name.len, dentry->d_name.name);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (!autofs4_oz_mode(sbi))
647 return -EACCES;
648
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100649 spin_lock(&autofs4_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100650 spin_lock(&sbi->lookup_lock);
651 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100653 spin_unlock(&dentry->d_lock);
654 spin_unlock(&sbi->lookup_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100655 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -ENOTEMPTY;
657 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100658 __autofs4_add_expiring(dentry);
659 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 __d_drop(dentry);
661 spin_unlock(&dentry->d_lock);
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100662 spin_unlock(&autofs4_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Ian Kent1aff3c82006-03-27 01:14:46 -0800664 if (atomic_dec_and_test(&ino->count)) {
665 p_ino = autofs4_dentry_ino(dentry->d_parent);
666 if (p_ino && dentry->d_parent != dentry)
667 atomic_dec(&p_ino->count);
668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700671 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700674 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 return 0;
677}
678
679static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
680{
681 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
682 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800683 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct inode *inode;
685
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700686 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -EACCES;
688
689 DPRINTK("dentry %p, creating %.*s",
690 dentry, dentry->d_name.len, dentry->d_name.name);
691
692 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700693 if (!ino)
694 return -ENOMEM;
695
Al Viro4b1ae272010-03-03 12:58:31 -0500696 autofs4_del_active(dentry);
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700699 if (!inode) {
700 if (!dentry->d_fsdata)
701 kfree(ino);
702 return -ENOMEM;
703 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700704 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 dentry->d_fsdata = ino;
707 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800708 atomic_inc(&ino->count);
709 p_ino = autofs4_dentry_ino(dentry->d_parent);
710 if (p_ino && dentry->d_parent != dentry)
711 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700713 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 dir->i_mtime = CURRENT_TIME;
715
716 return 0;
717}
718
719/* Get/set timeout ioctl() operation */
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200720#ifdef CONFIG_COMPAT
721static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
722 compat_ulong_t __user *p)
723{
724 int rv;
725 unsigned long ntimeout;
726
727 if ((rv = get_user(ntimeout, p)) ||
728 (rv = put_user(sbi->exp_timeout/HZ, p)))
729 return rv;
730
731 if (ntimeout > UINT_MAX/HZ)
732 sbi->exp_timeout = 0;
733 else
734 sbi->exp_timeout = ntimeout * HZ;
735
736 return 0;
737}
738#endif
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
741 unsigned long __user *p)
742{
743 int rv;
744 unsigned long ntimeout;
745
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700746 if ((rv = get_user(ntimeout, p)) ||
747 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return rv;
749
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700750 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 sbi->exp_timeout = 0;
752 else
753 sbi->exp_timeout = ntimeout * HZ;
754
755 return 0;
756}
757
758/* Return protocol version */
759static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
760{
761 return put_user(sbi->version, p);
762}
763
764/* Return protocol sub version */
765static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
766{
767 return put_user(sbi->sub_version, p);
768}
769
770/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771* Tells the daemon whether it can umount the autofs mount.
772*/
773static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
774{
775 int status = 0;
776
Ian Kente3474a82006-03-27 01:14:51 -0800777 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 status = 1;
779
780 DPRINTK("returning %d", status);
781
782 status = put_user(status, p);
783
784 return status;
785}
786
787/* Identify autofs4_dentries - this is so we can tell if there's
788 an extra dentry refcount or not. We only hold a refcount on the
789 dentry if its non-negative (ie, d_inode != NULL)
790*/
791int is_autofs4_dentry(struct dentry *dentry)
792{
793 return dentry && dentry->d_inode &&
794 (dentry->d_op == &autofs4_root_dentry_operations ||
795 dentry->d_op == &autofs4_dentry_operations) &&
796 dentry->d_fsdata != NULL;
797}
798
799/*
800 * ioctl()'s on the root directory is the chief method for the daemon to
801 * generate kernel reactions
802 */
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200803static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
804 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
807 void __user *p = (void __user *)arg;
808
809 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700810 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700812 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
813 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -ENOTTY;
815
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700816 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return -EPERM;
818
819 switch(cmd) {
820 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
821 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
822 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
823 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
824 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
825 autofs4_catatonic_mode(sbi);
826 return 0;
827 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
828 return autofs4_get_protover(sbi, p);
829 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
830 return autofs4_get_protosubver(sbi, p);
831 case AUTOFS_IOC_SETTIMEOUT:
832 return autofs4_get_set_timeout(sbi, p);
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200833#ifdef CONFIG_COMPAT
834 case AUTOFS_IOC_SETTIMEOUT32:
835 return autofs4_compat_get_set_timeout(sbi, p);
836#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800839 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* return a single thing to expire */
842 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800843 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* same as above, but can send multiple expires through pipe */
845 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800846 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 default:
849 return -ENOSYS;
850 }
851}
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200852
853static long autofs4_root_ioctl(struct file *filp,
854 unsigned int cmd, unsigned long arg)
855{
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200856 struct inode *inode = filp->f_dentry->d_inode;
Ian Kentde47de72010-12-07 13:04:00 +0800857 return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
Frederic Weisbecker3663df72010-05-19 15:08:17 +0200858}
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200859
860#ifdef CONFIG_COMPAT
861static long autofs4_root_compat_ioctl(struct file *filp,
862 unsigned int cmd, unsigned long arg)
863{
864 struct inode *inode = filp->f_path.dentry->d_inode;
865 int ret;
866
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200867 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
868 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
869 else
870 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
871 (unsigned long)compat_ptr(arg));
Arnd Bergmannc9243f52010-07-04 00:15:07 +0200872
873 return ret;
874}
875#endif