blob: e062ee5a3ed5e220f23a3769e7e760f3bcf9ebca [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>
18#include <linux/param.h>
19#include <linux/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "autofs_i.h"
21
22static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
23static int autofs4_dir_unlink(struct inode *,struct dentry *);
24static int autofs4_dir_rmdir(struct inode *,struct dentry *);
25static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
26static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
27static int autofs4_dir_open(struct inode *inode, struct file *file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
29static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
Ian Kent34ca9592006-03-27 01:14:54 -080030static void *autofs4_follow_link(struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Ian Kent6d5cb922008-07-23 21:30:15 -070032#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
33#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
34
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080035const struct file_operations autofs4_root_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .open = dcache_dir_open,
37 .release = dcache_dir_close,
38 .read = generic_read_dir,
39 .readdir = autofs4_root_readdir,
40 .ioctl = autofs4_root_ioctl,
41};
42
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080043const struct file_operations autofs4_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .open = autofs4_dir_open,
Ian Kentff9cd492008-07-23 21:30:24 -070045 .release = dcache_dir_close,
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 .read = generic_read_dir,
Ian Kentff9cd492008-07-23 21:30:24 -070047 .readdir = dcache_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Arjan van de Ven754661f2007-02-12 00:55:38 -080050const struct inode_operations autofs4_indirect_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 .lookup = autofs4_lookup,
52 .unlink = autofs4_dir_unlink,
53 .symlink = autofs4_dir_symlink,
54 .mkdir = autofs4_dir_mkdir,
55 .rmdir = autofs4_dir_rmdir,
56};
57
Arjan van de Ven754661f2007-02-12 00:55:38 -080058const struct inode_operations autofs4_direct_root_inode_operations = {
Ian Kent34ca9592006-03-27 01:14:54 -080059 .lookup = autofs4_lookup,
Ian Kent871f9432006-03-27 01:14:58 -080060 .unlink = autofs4_dir_unlink,
61 .mkdir = autofs4_dir_mkdir,
62 .rmdir = autofs4_dir_rmdir,
Ian Kent34ca9592006-03-27 01:14:54 -080063 .follow_link = autofs4_follow_link,
64};
65
Arjan van de Ven754661f2007-02-12 00:55:38 -080066const struct inode_operations autofs4_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 .lookup = autofs4_lookup,
68 .unlink = autofs4_dir_unlink,
69 .symlink = autofs4_dir_symlink,
70 .mkdir = autofs4_dir_mkdir,
71 .rmdir = autofs4_dir_rmdir,
72};
73
74static int autofs4_root_readdir(struct file *file, void *dirent,
75 filldir_t filldir)
76{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -080077 struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int oz_mode = autofs4_oz_mode(sbi);
79
80 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
81
82 /*
83 * Don't set reghost flag if:
84 * 1) f_pos is larger than zero -- we've already been here.
85 * 2) we haven't even enabled reghosting in the 1st place.
86 * 3) this is the daemon doing a readdir
87 */
88 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
89 sbi->needs_reghost = 1;
90
91 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
92
Ian Kentf360ce32006-03-27 01:14:43 -080093 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int autofs4_dir_open(struct inode *inode, struct file *file)
97{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -080098 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 DPRINTK("file=%p dentry=%p %.*s",
102 file, dentry, dentry->d_name.len, dentry->d_name.name);
103
104 if (autofs4_oz_mode(sbi))
105 goto out;
106
Ian Kentff9cd492008-07-23 21:30:24 -0700107 /*
108 * An empty directory in an autofs file system is always a
109 * mount point. The daemon must have failed to mount this
110 * during lookup so it doesn't exist. This can happen, for
111 * example, if user space returns an incorrect status for a
112 * mount request. Otherwise we're doing a readdir on the
113 * autofs file system so just let the libfs routines handle
114 * it.
115 */
116 spin_lock(&dcache_lock);
117 if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 spin_unlock(&dcache_lock);
Ian Kentff9cd492008-07-23 21:30:24 -0700119 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Ian Kentff9cd492008-07-23 21:30:24 -0700121 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Ian Kentf360ce32006-03-27 01:14:43 -0800123out:
Ian Kentff9cd492008-07-23 21:30:24 -0700124 return dcache_dir_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Ian Kent862b1102006-03-27 01:14:48 -0800127static int try_to_fill_dentry(struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Ian Kent862b1102006-03-27 01:14:48 -0800129 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800130 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700131 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* Block on any pending expiry here; invalidate the dentry
134 when expiration is done to trigger mount request with a new
135 dentry */
Ian Kent97e74492008-07-23 21:30:26 -0700136 spin_lock(&sbi->fs_lock);
137 if (ino->flags & AUTOFS_INF_EXPIRING) {
138 spin_unlock(&sbi->fs_lock);
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 DPRINTK("waiting for expire %p name=%.*s",
141 dentry, dentry->d_name.len, dentry->d_name.name);
142
143 status = autofs4_wait(sbi, dentry, NFY_NONE);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700144 wait_for_completion(&ino->expire_complete);
Ian Kent718c6042006-03-27 01:14:42 -0800145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 DPRINTK("expire done status=%d", status);
Ian Kent718c6042006-03-27 01:14:42 -0800147
Ian Kent1684b2b2005-06-21 17:16:41 -0700148 /*
149 * If the directory still exists the mount request must
150 * continue otherwise it can't be followed at the right
151 * time during the walk.
152 */
153 status = d_invalidate(dentry);
154 if (status != -EBUSY)
Ian Kentf50b6f82007-02-20 13:58:10 -0800155 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Ian Kent97e74492008-07-23 21:30:26 -0700157 goto cont;
158 }
159 spin_unlock(&sbi->fs_lock);
160cont:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 DPRINTK("dentry=%p %.*s ino=%p",
162 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
163
Ian Kent718c6042006-03-27 01:14:42 -0800164 /*
165 * Wait for a pending mount, triggering one if there
166 * isn't one already
167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (dentry->d_inode == NULL) {
169 DPRINTK("waiting for mount name=%.*s",
170 dentry->d_name.len, dentry->d_name.name);
171
172 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 DPRINTK("mount done status=%d", status);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 /* Turn this into a real negative dentry? */
177 if (status == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 spin_lock(&dentry->d_lock);
179 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
180 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800181 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 } else if (status) {
183 /* Return a negative dentry, but leave it "pending" */
Ian Kent34ca9592006-03-27 01:14:54 -0800184 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186 /* Trigger mount for path component or follow link */
Ian Kent26e81b32008-07-23 21:30:25 -0700187 } else if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
188 flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 current->link_count) {
190 DPRINTK("waiting for mount name=%.*s",
191 dentry->d_name.len, dentry->d_name.name);
192
193 spin_lock(&dentry->d_lock);
194 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
195 spin_unlock(&dentry->d_lock);
196 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
197
198 DPRINTK("mount done status=%d", status);
199
200 if (status) {
201 spin_lock(&dentry->d_lock);
202 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
203 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800204 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 }
207
Ian Kente0a7aae2006-03-27 01:14:47 -0800208 /* Initialize expiry counter after successful mount */
209 if (ino)
210 ino->last_used = jiffies;
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 spin_lock(&dentry->d_lock);
213 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
214 spin_unlock(&dentry->d_lock);
Jeff Moyer03379042008-05-01 04:35:08 -0700215
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700216 return 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800217}
218
219/* For autofs direct mounts the follow link triggers the mount */
220static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
221{
222 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kenta5370552006-05-15 09:43:51 -0700223 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent34ca9592006-03-27 01:14:54 -0800224 int oz_mode = autofs4_oz_mode(sbi);
225 unsigned int lookup_type;
226 int status;
227
228 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
229 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
230 nd->flags);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700231 /*
232 * For an expire of a covered direct or offset mount we need
233 * to beeak out of follow_down() at the autofs mount trigger
234 * (d_mounted--), so we can see the expiring flag, and manage
235 * the blocking and following here until the expire is completed.
236 */
237 if (oz_mode) {
238 spin_lock(&sbi->fs_lock);
239 if (ino->flags & AUTOFS_INF_EXPIRING) {
240 spin_unlock(&sbi->fs_lock);
241 /* Follow down to our covering mount. */
242 if (!follow_down(&nd->path.mnt, &nd->path.dentry))
243 goto done;
Ian Kentec6e8c72008-07-23 21:30:28 -0700244 goto follow;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700245 }
246 spin_unlock(&sbi->fs_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800247 goto done;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700248 }
Ian Kent34ca9592006-03-27 01:14:54 -0800249
Ian Kent6e60a9a2008-07-23 21:30:27 -0700250 /* If an expire request is pending everyone must wait. */
Ian Kent97e74492008-07-23 21:30:26 -0700251 spin_lock(&sbi->fs_lock);
252 if (ino->flags & AUTOFS_INF_EXPIRING) {
253 spin_unlock(&sbi->fs_lock);
254
Ian Kent871f9432006-03-27 01:14:58 -0800255 DPRINTK("waiting for active request %p name=%.*s",
256 dentry, dentry->d_name.len, dentry->d_name.name);
Ian Kent34ca9592006-03-27 01:14:54 -0800257
Ian Kent871f9432006-03-27 01:14:58 -0800258 status = autofs4_wait(sbi, dentry, NFY_NONE);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700259 wait_for_completion(&ino->expire_complete);
Ian Kent871f9432006-03-27 01:14:58 -0800260
261 DPRINTK("request done status=%d", status);
Ian Kent34ca9592006-03-27 01:14:54 -0800262
Ian Kent97e74492008-07-23 21:30:26 -0700263 goto cont;
264 }
265 spin_unlock(&sbi->fs_lock);
266cont:
Ian Kent6e60a9a2008-07-23 21:30:27 -0700267 /* We trigger a mount for almost all flags */
268 lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS);
269 if (!(lookup_type || dentry->d_flags & DCACHE_AUTOFS_PENDING))
Ian Kentec6e8c72008-07-23 21:30:28 -0700270 goto follow;
Ian Kent6e60a9a2008-07-23 21:30:27 -0700271
Ian Kent871f9432006-03-27 01:14:58 -0800272 /*
Ian Kent6e60a9a2008-07-23 21:30:27 -0700273 * If the dentry contains directories then it is an autofs
274 * multi-mount with no root mount offset. So don't try to
275 * mount it again.
Ian Kent871f9432006-03-27 01:14:58 -0800276 */
277 spin_lock(&dcache_lock);
Ian Kent26e81b32008-07-23 21:30:25 -0700278 if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
279 (!d_mountpoint(dentry) && __simple_empty(dentry))) {
Ian Kent871f9432006-03-27 01:14:58 -0800280 spin_unlock(&dcache_lock);
281
282 status = try_to_fill_dentry(dentry, 0);
283 if (status)
284 goto out_error;
285
Ian Kent6e60a9a2008-07-23 21:30:27 -0700286 goto follow;
Ian Kent871f9432006-03-27 01:14:58 -0800287 }
288 spin_unlock(&dcache_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)) {
296 if (!autofs4_follow_mount(&nd->path.mnt,
297 &nd->path.dentry)) {
298 status = -ENOENT;
299 goto out_error;
300 }
301 }
Ian Kent871f9432006-03-27 01:14:58 -0800302
Ian Kent34ca9592006-03-27 01:14:54 -0800303done:
304 return NULL;
305
306out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800307 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800308 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311/*
312 * Revalidate is called on every cache lookup. Some of those
313 * cache lookups may actually happen while the dentry is not
314 * yet completely filled in, and revalidate has to delay such
315 * lookups..
316 */
Ian Kent718c6042006-03-27 01:14:42 -0800317static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Ian Kent718c6042006-03-27 01:14:42 -0800319 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
321 int oz_mode = autofs4_oz_mode(sbi);
322 int flags = nd ? nd->flags : 0;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700323 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 /* Pending dentry */
Ian Kent97e74492008-07-23 21:30:26 -0700326 spin_lock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (autofs4_ispending(dentry)) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700328 /* The daemon never causes a mount to trigger */
Ian Kent97e74492008-07-23 21:30:26 -0700329 spin_unlock(&sbi->fs_lock);
330
Ian Kentbcdc5e02006-09-27 01:50:44 -0700331 if (oz_mode)
332 return 1;
333
334 /*
335 * A zero status is success otherwise we have a
336 * negative error code.
337 */
338 status = try_to_fill_dentry(dentry, flags);
339 if (status == 0)
Ian Kentf50b6f82007-02-20 13:58:10 -0800340 return 1;
341
342 /*
343 * A status of EAGAIN here means that the dentry has gone
344 * away while waiting for an expire to complete. If we are
345 * racing with expire lookup will wait for it so this must
346 * be a revalidate and we need to send it to lookup.
347 */
348 if (status == -EAGAIN)
349 return 0;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700350
351 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Ian Kent97e74492008-07-23 21:30:26 -0700353 spin_unlock(&sbi->fs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* Negative dentry.. invalidate if "old" */
356 if (dentry->d_inode == NULL)
Ian Kent2d753e62006-03-27 01:14:44 -0800357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* Check for a non-mountpoint directory with no contents */
360 spin_lock(&dcache_lock);
361 if (S_ISDIR(dentry->d_inode->i_mode) &&
362 !d_mountpoint(dentry) &&
Ian Kent90a59c72006-03-27 01:14:50 -0800363 __simple_empty(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 DPRINTK("dentry=%p %.*s, emptydir",
365 dentry, dentry->d_name.len, dentry->d_name.name);
366 spin_unlock(&dcache_lock);
Ian Kent97e74492008-07-23 21:30:26 -0700367
Ian Kentbcdc5e02006-09-27 01:50:44 -0700368 /* The daemon never causes a mount to trigger */
369 if (oz_mode)
370 return 1;
371
372 /*
373 * A zero status is success otherwise we have a
374 * negative error code.
375 */
376 status = try_to_fill_dentry(dentry, flags);
377 if (status == 0)
378 return 1;
379
380 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382 spin_unlock(&dcache_lock);
383
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 */
416static struct dentry_operations autofs4_root_dentry_operations = {
417 .d_revalidate = autofs4_revalidate,
418 .d_release = autofs4_dentry_release,
419};
420
421/* For other dentries */
422static struct dentry_operations autofs4_dentry_operations = {
423 .d_revalidate = autofs4_revalidate,
424 .d_release = autofs4_dentry_release,
425};
426
Ian Kent25767372008-07-23 21:30:12 -0700427static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
428{
429 unsigned int len = name->len;
430 unsigned int hash = name->hash;
431 const unsigned char *str = name->name;
432 struct list_head *p, *head;
433
434 spin_lock(&dcache_lock);
435 spin_lock(&sbi->lookup_lock);
436 head = &sbi->active_list;
437 list_for_each(p, head) {
438 struct autofs_info *ino;
439 struct dentry *dentry;
440 struct qstr *qstr;
441
442 ino = list_entry(p, struct autofs_info, active);
443 dentry = ino->dentry;
444
445 spin_lock(&dentry->d_lock);
446
447 /* Already gone? */
448 if (atomic_read(&dentry->d_count) == 0)
449 goto next;
450
451 qstr = &dentry->d_name;
452
453 if (dentry->d_name.hash != hash)
454 goto next;
455 if (dentry->d_parent != parent)
456 goto next;
457
458 if (qstr->len != len)
459 goto next;
460 if (memcmp(qstr->name, str, len))
461 goto next;
462
463 if (d_unhashed(dentry)) {
464 dget(dentry);
465 spin_unlock(&dentry->d_lock);
466 spin_unlock(&sbi->lookup_lock);
467 spin_unlock(&dcache_lock);
468 return dentry;
469 }
470next:
471 spin_unlock(&dentry->d_lock);
472 }
473 spin_unlock(&sbi->lookup_lock);
474 spin_unlock(&dcache_lock);
475
476 return NULL;
477}
478
Ian Kent5f6f4f22008-07-23 21:30:09 -0700479static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
Ian Kentf50b6f82007-02-20 13:58:10 -0800480{
481 unsigned int len = name->len;
482 unsigned int hash = name->hash;
483 const unsigned char *str = name->name;
484 struct list_head *p, *head;
485
486 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700487 spin_lock(&sbi->lookup_lock);
488 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800489 list_for_each(p, head) {
490 struct autofs_info *ino;
491 struct dentry *dentry;
492 struct qstr *qstr;
493
Ian Kent5f6f4f22008-07-23 21:30:09 -0700494 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800495 dentry = ino->dentry;
496
497 spin_lock(&dentry->d_lock);
498
499 /* Bad luck, we've already been dentry_iput */
500 if (!dentry->d_inode)
501 goto next;
502
503 qstr = &dentry->d_name;
504
505 if (dentry->d_name.hash != hash)
506 goto next;
507 if (dentry->d_parent != parent)
508 goto next;
509
510 if (qstr->len != len)
511 goto next;
512 if (memcmp(qstr->name, str, len))
513 goto next;
514
515 if (d_unhashed(dentry)) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800516 dget(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800517 spin_unlock(&dentry->d_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700518 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800519 spin_unlock(&dcache_lock);
520 return dentry;
521 }
522next:
523 spin_unlock(&dentry->d_lock);
524 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700525 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800526 spin_unlock(&dcache_lock);
527
528 return NULL;
529}
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531/* Lookups in the root directory */
532static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
533{
534 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700535 struct autofs_info *ino;
536 struct dentry *expiring, *unhashed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int oz_mode;
538
539 DPRINTK("name = %.*s",
540 dentry->d_name.len, dentry->d_name.name);
541
Ian Kent718c6042006-03-27 01:14:42 -0800542 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800544 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700550 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Ian Kent5f6f4f22008-07-23 21:30:09 -0700552 expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
553 if (expiring) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800554 /*
555 * If we are racing with expire the request might not
556 * be quite complete but the directory has been removed
557 * so it must have been successful, so just wait for it.
558 */
Ian Kent25767372008-07-23 21:30:12 -0700559 ino = autofs4_dentry_ino(expiring);
Ian Kent97e74492008-07-23 21:30:26 -0700560 spin_lock(&sbi->fs_lock);
561 if (ino->flags & AUTOFS_INF_EXPIRING) {
562 spin_unlock(&sbi->fs_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800563 DPRINTK("wait for incomplete expire %p name=%.*s",
Ian Kent5f6f4f22008-07-23 21:30:09 -0700564 expiring, expiring->d_name.len,
565 expiring->d_name.name);
566 autofs4_wait(sbi, expiring, NFY_NONE);
Ian Kent6e60a9a2008-07-23 21:30:27 -0700567 wait_for_completion(&ino->expire_complete);
Ian Kentf50b6f82007-02-20 13:58:10 -0800568 DPRINTK("request completed");
Ian Kent97e74492008-07-23 21:30:26 -0700569 goto cont;
Ian Kentf50b6f82007-02-20 13:58:10 -0800570 }
Ian Kent97e74492008-07-23 21:30:26 -0700571 spin_unlock(&sbi->fs_lock);
572cont:
Ian Kent5f6f4f22008-07-23 21:30:09 -0700573 spin_lock(&sbi->lookup_lock);
574 if (!list_empty(&ino->expiring))
575 list_del_init(&ino->expiring);
576 spin_unlock(&sbi->lookup_lock);
577 dput(expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Ian Kent25767372008-07-23 21:30:12 -0700580 unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
581 if (unhashed)
582 dentry = unhashed;
583 else {
584 /*
585 * Mark the dentry incomplete but don't hash it. We do this
586 * to serialize our inode creation operations (symlink and
587 * mkdir) which prevents deadlock during the callback to
588 * the daemon. Subsequent user space lookups for the same
589 * dentry are placed on the wait queue while the daemon
590 * itself is allowed passage unresticted so the create
591 * operation itself can then hash the dentry. Finally,
592 * we check for the hashed dentry and return the newly
593 * hashed dentry.
594 */
595 dentry->d_op = &autofs4_root_dentry_operations;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700596
Ian Kent25767372008-07-23 21:30:12 -0700597 /*
598 * And we need to ensure that the same dentry is used for
599 * all following lookup calls until it is hashed so that
600 * the dentry flags are persistent throughout the request.
601 */
602 ino = autofs4_init_ino(NULL, sbi, 0555);
603 if (!ino)
604 return ERR_PTR(-ENOMEM);
605
606 dentry->d_fsdata = ino;
607 ino->dentry = dentry;
608
609 spin_lock(&sbi->lookup_lock);
610 list_add(&ino->active, &sbi->active_list);
611 spin_unlock(&sbi->lookup_lock);
612
613 d_instantiate(dentry, NULL);
614 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (!oz_mode) {
617 spin_lock(&dentry->d_lock);
618 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
619 spin_unlock(&dentry->d_lock);
Ian Kentc432c252008-07-23 21:30:14 -0700620 if (dentry->d_op && dentry->d_op->d_revalidate) {
621 mutex_unlock(&dir->i_mutex);
622 (dentry->d_op->d_revalidate)(dentry, nd);
623 mutex_lock(&dir->i_mutex);
624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
627 /*
628 * If we are still pending, check if we had to handle
629 * a signal. If so we can force a restart..
630 */
631 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
632 /* See if we were interrupted */
633 if (signal_pending(current)) {
634 sigset_t *sigset = &current->pending.signal;
635 if (sigismember (sigset, SIGKILL) ||
636 sigismember (sigset, SIGQUIT) ||
637 sigismember (sigset, SIGINT)) {
Ian Kent25767372008-07-23 21:30:12 -0700638 if (unhashed)
639 dput(unhashed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return ERR_PTR(-ERESTARTNOINTR);
641 }
642 }
Ian Kent25767372008-07-23 21:30:12 -0700643 if (!oz_mode) {
644 spin_lock(&dentry->d_lock);
645 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
646 spin_unlock(&dentry->d_lock);
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 /*
651 * If this dentry is unhashed, then we shouldn't honour this
Ian Kentc9ffec42007-02-20 13:58:10 -0800652 * lookup. Returning ENOENT here doesn't do the right thing
653 * for all system calls, but it should be OK for the operations
654 * we permit from an autofs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
Ian Kent1864f7b2007-08-22 14:01:54 -0700656 if (!oz_mode && d_unhashed(dentry)) {
Ian Kentc9ffec42007-02-20 13:58:10 -0800657 /*
658 * A user space application can (and has done in the past)
659 * remove and re-create this directory during the callback.
660 * This can leave us with an unhashed dentry, but a
661 * successful mount! So we need to perform another
662 * cached lookup in case the dentry now exists.
663 */
664 struct dentry *parent = dentry->d_parent;
665 struct dentry *new = d_lookup(parent, &dentry->d_name);
666 if (new != NULL)
667 dentry = new;
668 else
669 dentry = ERR_PTR(-ENOENT);
670
Ian Kent25767372008-07-23 21:30:12 -0700671 if (unhashed)
672 dput(unhashed);
673
Ian Kentc9ffec42007-02-20 13:58:10 -0800674 return dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800675 }
676
Ian Kent25767372008-07-23 21:30:12 -0700677 if (unhashed)
678 return unhashed;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return NULL;
681}
682
683static int autofs4_dir_symlink(struct inode *dir,
684 struct dentry *dentry,
685 const char *symname)
686{
687 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
688 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800689 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct inode *inode;
691 char *cp;
692
693 DPRINTK("%s <- %.*s", symname,
694 dentry->d_name.len, dentry->d_name.name);
695
696 if (!autofs4_oz_mode(sbi))
697 return -EACCES;
698
699 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700700 if (!ino)
701 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Ian Kent25767372008-07-23 21:30:12 -0700703 spin_lock(&sbi->lookup_lock);
704 if (!list_empty(&ino->active))
705 list_del_init(&ino->active);
706 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Ian Kentef581a72008-07-23 21:30:13 -0700708 ino->size = strlen(symname);
Ian Kent25767372008-07-23 21:30:12 -0700709 cp = kmalloc(ino->size + 1, GFP_KERNEL);
710 if (!cp) {
711 if (!dentry->d_fsdata)
712 kfree(ino);
713 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
716 strcpy(cp, symname);
717
718 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700719 if (!inode) {
720 kfree(cp);
721 if (!dentry->d_fsdata)
722 kfree(ino);
723 return -ENOMEM;
724 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700725 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (dir == dir->i_sb->s_root->d_inode)
728 dentry->d_op = &autofs4_root_dentry_operations;
729 else
730 dentry->d_op = &autofs4_dentry_operations;
731
732 dentry->d_fsdata = ino;
733 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800734 atomic_inc(&ino->count);
735 p_ino = autofs4_dentry_ino(dentry->d_parent);
736 if (p_ino && dentry->d_parent != dentry)
737 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 ino->inode = inode;
739
Ian Kent25767372008-07-23 21:30:12 -0700740 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 dir->i_mtime = CURRENT_TIME;
742
743 return 0;
744}
745
746/*
747 * NOTE!
748 *
749 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
750 * that the file no longer exists. However, doing that means that the
751 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800752 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700753 * We simply d_drop it and add it to a expiring list in the super block,
754 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 *
756 * If a process is blocked on the dentry waiting for the expire to finish,
757 * it will invalidate the dentry and try to mount with a new one.
758 *
759 * Also see autofs4_dir_rmdir()..
760 */
761static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
762{
763 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
764 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800765 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700768 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return -EACCES;
770
Ian Kent1aff3c82006-03-27 01:14:46 -0800771 if (atomic_dec_and_test(&ino->count)) {
772 p_ino = autofs4_dentry_ino(dentry->d_parent);
773 if (p_ino && dentry->d_parent != dentry)
774 atomic_dec(&p_ino->count);
775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 dput(ino->dentry);
777
778 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700779 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 dir->i_mtime = CURRENT_TIME;
782
Ian Kentf50b6f82007-02-20 13:58:10 -0800783 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700784 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700785 if (list_empty(&ino->expiring))
786 list_add(&ino->expiring, &sbi->expiring_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700787 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800788 spin_lock(&dentry->d_lock);
789 __d_drop(dentry);
790 spin_unlock(&dentry->d_lock);
791 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 return 0;
794}
795
796static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
797{
798 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
799 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800800 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Ian Kentf50b6f82007-02-20 13:58:10 -0800802 DPRINTK("dentry %p, removing %.*s",
803 dentry, dentry->d_name.len, dentry->d_name.name);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (!autofs4_oz_mode(sbi))
806 return -EACCES;
807
808 spin_lock(&dcache_lock);
809 if (!list_empty(&dentry->d_subdirs)) {
810 spin_unlock(&dcache_lock);
811 return -ENOTEMPTY;
812 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700813 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700814 if (list_empty(&ino->expiring))
815 list_add(&ino->expiring, &sbi->expiring_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700816 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 spin_lock(&dentry->d_lock);
818 __d_drop(dentry);
819 spin_unlock(&dentry->d_lock);
820 spin_unlock(&dcache_lock);
821
Ian Kent1aff3c82006-03-27 01:14:46 -0800822 if (atomic_dec_and_test(&ino->count)) {
823 p_ino = autofs4_dentry_ino(dentry->d_parent);
824 if (p_ino && dentry->d_parent != dentry)
825 atomic_dec(&p_ino->count);
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700829 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700832 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 return 0;
835}
836
837static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
838{
839 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
840 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800841 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 struct inode *inode;
843
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700844 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return -EACCES;
846
847 DPRINTK("dentry %p, creating %.*s",
848 dentry, dentry->d_name.len, dentry->d_name.name);
849
850 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700851 if (!ino)
852 return -ENOMEM;
853
854 spin_lock(&sbi->lookup_lock);
855 if (!list_empty(&ino->active))
856 list_del_init(&ino->active);
857 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700860 if (!inode) {
861 if (!dentry->d_fsdata)
862 kfree(ino);
863 return -ENOMEM;
864 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700865 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 if (dir == dir->i_sb->s_root->d_inode)
868 dentry->d_op = &autofs4_root_dentry_operations;
869 else
870 dentry->d_op = &autofs4_dentry_operations;
871
872 dentry->d_fsdata = ino;
873 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800874 atomic_inc(&ino->count);
875 p_ino = autofs4_dentry_ino(dentry->d_parent);
876 if (p_ino && dentry->d_parent != dentry)
877 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700879 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 dir->i_mtime = CURRENT_TIME;
881
882 return 0;
883}
884
885/* Get/set timeout ioctl() operation */
886static 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/*
917 * Tells the daemon whether we need to reghost or not. Also, clears
918 * the reghost_needed flag.
919 */
920static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
921{
922 int status;
923
924 DPRINTK("returning %d", sbi->needs_reghost);
925
926 status = put_user(sbi->needs_reghost, p);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700927 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return status;
929
930 sbi->needs_reghost = 0;
931 return 0;
932}
933
934/*
935 * Enable / Disable reghosting ioctl() operation
936 */
937static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
938{
939 int status;
940 int val;
941
942 status = get_user(val, p);
943
944 DPRINTK("reghost = %d", val);
945
946 if (status)
947 return status;
948
949 /* turn on/off reghosting, with the val */
950 sbi->reghost_enabled = val;
951 return 0;
952}
953
954/*
955* Tells the daemon whether it can umount the autofs mount.
956*/
957static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
958{
959 int status = 0;
960
Ian Kente3474a82006-03-27 01:14:51 -0800961 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 status = 1;
963
964 DPRINTK("returning %d", status);
965
966 status = put_user(status, p);
967
968 return status;
969}
970
971/* Identify autofs4_dentries - this is so we can tell if there's
972 an extra dentry refcount or not. We only hold a refcount on the
973 dentry if its non-negative (ie, d_inode != NULL)
974*/
975int is_autofs4_dentry(struct dentry *dentry)
976{
977 return dentry && dentry->d_inode &&
978 (dentry->d_op == &autofs4_root_dentry_operations ||
979 dentry->d_op == &autofs4_dentry_operations) &&
980 dentry->d_fsdata != NULL;
981}
982
983/*
984 * ioctl()'s on the root directory is the chief method for the daemon to
985 * generate kernel reactions
986 */
987static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
988 unsigned int cmd, unsigned long arg)
989{
990 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
991 void __user *p = (void __user *)arg;
992
993 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700994 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700996 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
997 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return -ENOTTY;
999
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -07001000 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return -EPERM;
1002
1003 switch(cmd) {
1004 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
1005 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
1006 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
1007 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
1008 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
1009 autofs4_catatonic_mode(sbi);
1010 return 0;
1011 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
1012 return autofs4_get_protover(sbi, p);
1013 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
1014 return autofs4_get_protosubver(sbi, p);
1015 case AUTOFS_IOC_SETTIMEOUT:
1016 return autofs4_get_set_timeout(sbi, p);
1017
1018 case AUTOFS_IOC_TOGGLEREGHOST:
1019 return autofs4_toggle_reghost(sbi, p);
1020 case AUTOFS_IOC_ASKREGHOST:
1021 return autofs4_ask_reghost(sbi, p);
1022
1023 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001024 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 /* return a single thing to expire */
1027 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001028 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 /* same as above, but can send multiple expires through pipe */
1030 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001031 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 default:
1034 return -ENOSYS;
1035 }
1036}