blob: 9ead2279df4fad5b70306074ab50a2e7d66b8874 [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);
28static int autofs4_dir_close(struct inode *inode, struct file *file);
29static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
30static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
31static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
Ian Kent34ca9592006-03-27 01:14:54 -080032static void *autofs4_follow_link(struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080034const struct file_operations autofs4_root_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .open = dcache_dir_open,
36 .release = dcache_dir_close,
37 .read = generic_read_dir,
38 .readdir = autofs4_root_readdir,
39 .ioctl = autofs4_root_ioctl,
40};
41
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080042const struct file_operations autofs4_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 .open = autofs4_dir_open,
44 .release = autofs4_dir_close,
45 .read = generic_read_dir,
46 .readdir = autofs4_dir_readdir,
47};
48
Arjan van de Ven754661f2007-02-12 00:55:38 -080049const struct inode_operations autofs4_indirect_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 .lookup = autofs4_lookup,
51 .unlink = autofs4_dir_unlink,
52 .symlink = autofs4_dir_symlink,
53 .mkdir = autofs4_dir_mkdir,
54 .rmdir = autofs4_dir_rmdir,
55};
56
Arjan van de Ven754661f2007-02-12 00:55:38 -080057const struct inode_operations autofs4_direct_root_inode_operations = {
Ian Kent34ca9592006-03-27 01:14:54 -080058 .lookup = autofs4_lookup,
Ian Kent871f9432006-03-27 01:14:58 -080059 .unlink = autofs4_dir_unlink,
60 .mkdir = autofs4_dir_mkdir,
61 .rmdir = autofs4_dir_rmdir,
Ian Kent34ca9592006-03-27 01:14:54 -080062 .follow_link = autofs4_follow_link,
63};
64
Arjan van de Ven754661f2007-02-12 00:55:38 -080065const struct inode_operations autofs4_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 .lookup = autofs4_lookup,
67 .unlink = autofs4_dir_unlink,
68 .symlink = autofs4_dir_symlink,
69 .mkdir = autofs4_dir_mkdir,
70 .rmdir = autofs4_dir_rmdir,
71};
72
73static int autofs4_root_readdir(struct file *file, void *dirent,
74 filldir_t filldir)
75{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -080076 struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 int oz_mode = autofs4_oz_mode(sbi);
78
79 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
80
81 /*
82 * Don't set reghost flag if:
83 * 1) f_pos is larger than zero -- we've already been here.
84 * 2) we haven't even enabled reghosting in the 1st place.
85 * 3) this is the daemon doing a readdir
86 */
87 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
88 sbi->needs_reghost = 1;
89
90 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
91
Ian Kentf360ce32006-03-27 01:14:43 -080092 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int autofs4_dir_open(struct inode *inode, struct file *file)
96{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -080097 struct dentry *dentry = file->f_path.dentry;
98 struct vfsmount *mnt = file->f_path.mnt;
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 struct dentry *cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int status;
102
Ian Kentf360ce32006-03-27 01:14:43 -0800103 status = dcache_dir_open(inode, file);
104 if (status)
105 goto out;
106
107 cursor = file->private_data;
108 cursor->d_fsdata = NULL;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 DPRINTK("file=%p dentry=%p %.*s",
111 file, dentry, dentry->d_name.len, dentry->d_name.name);
112
113 if (autofs4_oz_mode(sbi))
114 goto out;
115
116 if (autofs4_ispending(dentry)) {
117 DPRINTK("dentry busy");
Ian Kentf360ce32006-03-27 01:14:43 -0800118 dcache_dir_close(inode, file);
119 status = -EBUSY;
120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
Ian Kentf360ce32006-03-27 01:14:43 -0800123 status = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
125 struct nameidata nd;
Ian Kentf360ce32006-03-27 01:14:43 -0800126 int empty, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 /* In case there are stale directory dentrys from a failed mount */
129 spin_lock(&dcache_lock);
130 empty = list_empty(&dentry->d_subdirs);
131 spin_unlock(&dcache_lock);
132
133 if (!empty)
134 d_invalidate(dentry);
135
136 nd.flags = LOOKUP_DIRECTORY;
Ian Kentf360ce32006-03-27 01:14:43 -0800137 ret = (dentry->d_op->d_revalidate)(dentry, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Ian Kentbcdc5e02006-09-27 01:50:44 -0700139 if (ret <= 0) {
140 if (ret < 0)
141 status = ret;
Ian Kentf360ce32006-03-27 01:14:43 -0800142 dcache_dir_close(inode, file);
143 goto out;
144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
147 if (d_mountpoint(dentry)) {
148 struct file *fp = NULL;
Jan Blunck868eb7a2008-05-01 04:35:10 -0700149 struct path fp_path = { .dentry = dentry, .mnt = mnt };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Jan Blunck868eb7a2008-05-01 04:35:10 -0700151 path_get(&fp_path);
152
153 if (!autofs4_follow_mount(&fp_path.mnt, &fp_path.dentry)) {
154 path_put(&fp_path);
Ian Kentf360ce32006-03-27 01:14:43 -0800155 dcache_dir_close(inode, file);
156 goto out;
Ian Kent9b1e3af2005-06-21 17:16:38 -0700157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jan Blunck868eb7a2008-05-01 04:35:10 -0700159 fp = dentry_open(fp_path.dentry, fp_path.mnt, file->f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 status = PTR_ERR(fp);
161 if (IS_ERR(fp)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800162 dcache_dir_close(inode, file);
163 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 }
Ian Kentf360ce32006-03-27 01:14:43 -0800165 cursor->d_fsdata = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
Ian Kentf360ce32006-03-27 01:14:43 -0800168out:
169 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172static int autofs4_dir_close(struct inode *inode, struct file *file)
173{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800174 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800176 struct dentry *cursor = file->private_data;
177 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 DPRINTK("file=%p dentry=%p %.*s",
180 file, dentry, dentry->d_name.len, dentry->d_name.name);
181
182 if (autofs4_oz_mode(sbi))
183 goto out;
184
185 if (autofs4_ispending(dentry)) {
186 DPRINTK("dentry busy");
Ian Kentf360ce32006-03-27 01:14:43 -0800187 status = -EBUSY;
188 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
191 if (d_mountpoint(dentry)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800192 struct file *fp = cursor->d_fsdata;
193 if (!fp) {
194 status = -ENOENT;
195 goto out;
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 filp_close(fp, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199out:
Ian Kentf360ce32006-03-27 01:14:43 -0800200 dcache_dir_close(inode, file);
201 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
205{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800206 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800208 struct dentry *cursor = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int status;
210
211 DPRINTK("file=%p dentry=%p %.*s",
212 file, dentry, dentry->d_name.len, dentry->d_name.name);
213
214 if (autofs4_oz_mode(sbi))
215 goto out;
216
217 if (autofs4_ispending(dentry)) {
218 DPRINTK("dentry busy");
219 return -EBUSY;
220 }
221
222 if (d_mountpoint(dentry)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800223 struct file *fp = cursor->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (!fp)
226 return -ENOENT;
227
228 if (!fp->f_op || !fp->f_op->readdir)
229 goto out;
230
231 status = vfs_readdir(fp, filldir, dirent);
232 file->f_pos = fp->f_pos;
233 if (status)
234 autofs4_copy_atime(file, fp);
235 return status;
236 }
237out:
Ian Kentf360ce32006-03-27 01:14:43 -0800238 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Ian Kent862b1102006-03-27 01:14:48 -0800241static int try_to_fill_dentry(struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Ian Kent862b1102006-03-27 01:14:48 -0800243 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800244 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Jeff Moyer03379042008-05-01 04:35:08 -0700245 struct dentry *new;
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700246 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* Block on any pending expiry here; invalidate the dentry
249 when expiration is done to trigger mount request with a new
250 dentry */
Ian Kent718c6042006-03-27 01:14:42 -0800251 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 DPRINTK("waiting for expire %p name=%.*s",
253 dentry, dentry->d_name.len, dentry->d_name.name);
254
255 status = autofs4_wait(sbi, dentry, NFY_NONE);
Ian Kent718c6042006-03-27 01:14:42 -0800256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 DPRINTK("expire done status=%d", status);
Ian Kent718c6042006-03-27 01:14:42 -0800258
Ian Kent1684b2b2005-06-21 17:16:41 -0700259 /*
260 * If the directory still exists the mount request must
261 * continue otherwise it can't be followed at the right
262 * time during the walk.
263 */
264 status = d_invalidate(dentry);
265 if (status != -EBUSY)
Ian Kentf50b6f82007-02-20 13:58:10 -0800266 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
269 DPRINTK("dentry=%p %.*s ino=%p",
270 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
271
Ian Kent718c6042006-03-27 01:14:42 -0800272 /*
273 * Wait for a pending mount, triggering one if there
274 * isn't one already
275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (dentry->d_inode == NULL) {
277 DPRINTK("waiting for mount name=%.*s",
278 dentry->d_name.len, dentry->d_name.name);
279
280 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 DPRINTK("mount done status=%d", status);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* Turn this into a real negative dentry? */
285 if (status == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 spin_lock(&dentry->d_lock);
287 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
288 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800289 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 } else if (status) {
291 /* Return a negative dentry, but leave it "pending" */
Ian Kent34ca9592006-03-27 01:14:54 -0800292 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 /* Trigger mount for path component or follow link */
295 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
296 current->link_count) {
297 DPRINTK("waiting for mount name=%.*s",
298 dentry->d_name.len, dentry->d_name.name);
299
300 spin_lock(&dentry->d_lock);
301 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
302 spin_unlock(&dentry->d_lock);
303 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
304
305 DPRINTK("mount done status=%d", status);
306
307 if (status) {
308 spin_lock(&dentry->d_lock);
309 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
310 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800311 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313 }
314
Ian Kente0a7aae2006-03-27 01:14:47 -0800315 /* Initialize expiry counter after successful mount */
316 if (ino)
317 ino->last_used = jiffies;
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 spin_lock(&dentry->d_lock);
320 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
321 spin_unlock(&dentry->d_lock);
Jeff Moyer03379042008-05-01 04:35:08 -0700322
323 /*
324 * The dentry that is passed in from lookup may not be the one
325 * we end up using, as mkdir can create a new one. If this
326 * happens, and another process tries the lookup at the same time,
327 * it will set the PENDING flag on this new dentry, but add itself
328 * to our waitq. Then, if after the lookup succeeds, the first
329 * process that requested the mount performs another lookup of the
330 * same directory, it will show up as still pending! So, we need
331 * to redo the lookup here and clear pending on that dentry.
332 */
333 if (d_unhashed(dentry)) {
334 new = d_lookup(dentry->d_parent, &dentry->d_name);
335 if (new) {
336 spin_lock(&new->d_lock);
337 new->d_flags &= ~DCACHE_AUTOFS_PENDING;
338 spin_unlock(&new->d_lock);
339 dput(new);
340 }
341 }
342
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700343 return 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800344}
345
346/* For autofs direct mounts the follow link triggers the mount */
347static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
348{
349 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kenta5370552006-05-15 09:43:51 -0700350 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent34ca9592006-03-27 01:14:54 -0800351 int oz_mode = autofs4_oz_mode(sbi);
352 unsigned int lookup_type;
353 int status;
354
355 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
356 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
357 nd->flags);
358
359 /* If it's our master or we shouldn't trigger a mount we're done */
360 lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
361 if (oz_mode || !lookup_type)
362 goto done;
363
Ian Kenta5370552006-05-15 09:43:51 -0700364 /* If an expire request is pending wait for it. */
365 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Ian Kent871f9432006-03-27 01:14:58 -0800366 DPRINTK("waiting for active request %p name=%.*s",
367 dentry, dentry->d_name.len, dentry->d_name.name);
Ian Kent34ca9592006-03-27 01:14:54 -0800368
Ian Kent871f9432006-03-27 01:14:58 -0800369 status = autofs4_wait(sbi, dentry, NFY_NONE);
370
371 DPRINTK("request done status=%d", status);
Ian Kent34ca9592006-03-27 01:14:54 -0800372 }
373
Ian Kent871f9432006-03-27 01:14:58 -0800374 /*
375 * If the dentry contains directories then it is an
376 * autofs multi-mount with no root mount offset. So
377 * don't try to mount it again.
378 */
379 spin_lock(&dcache_lock);
Ian Kentbe3ca7f2006-09-29 02:00:53 -0700380 if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
Ian Kent871f9432006-03-27 01:14:58 -0800381 spin_unlock(&dcache_lock);
382
383 status = try_to_fill_dentry(dentry, 0);
384 if (status)
385 goto out_error;
386
387 /*
388 * The mount succeeded but if there is no root mount
389 * it must be an autofs multi-mount with no root offset
390 * so we don't need to follow the mount.
391 */
392 if (d_mountpoint(dentry)) {
Jan Blunck4ac91372008-02-14 19:34:32 -0800393 if (!autofs4_follow_mount(&nd->path.mnt,
394 &nd->path.dentry)) {
Ian Kent871f9432006-03-27 01:14:58 -0800395 status = -ENOENT;
396 goto out_error;
397 }
398 }
399
400 goto done;
401 }
402 spin_unlock(&dcache_lock);
403
Ian Kent34ca9592006-03-27 01:14:54 -0800404done:
405 return NULL;
406
407out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800408 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800409 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412/*
413 * Revalidate is called on every cache lookup. Some of those
414 * cache lookups may actually happen while the dentry is not
415 * yet completely filled in, and revalidate has to delay such
416 * lookups..
417 */
Ian Kent718c6042006-03-27 01:14:42 -0800418static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Ian Kent718c6042006-03-27 01:14:42 -0800420 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
422 int oz_mode = autofs4_oz_mode(sbi);
423 int flags = nd ? nd->flags : 0;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700424 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* Pending dentry */
427 if (autofs4_ispending(dentry)) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700428 /* The daemon never causes a mount to trigger */
429 if (oz_mode)
430 return 1;
431
432 /*
433 * A zero status is success otherwise we have a
434 * negative error code.
435 */
436 status = try_to_fill_dentry(dentry, flags);
437 if (status == 0)
Ian Kentf50b6f82007-02-20 13:58:10 -0800438 return 1;
439
440 /*
441 * A status of EAGAIN here means that the dentry has gone
442 * away while waiting for an expire to complete. If we are
443 * racing with expire lookup will wait for it so this must
444 * be a revalidate and we need to send it to lookup.
445 */
446 if (status == -EAGAIN)
447 return 0;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700448
449 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451
452 /* Negative dentry.. invalidate if "old" */
453 if (dentry->d_inode == NULL)
Ian Kent2d753e62006-03-27 01:14:44 -0800454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 /* Check for a non-mountpoint directory with no contents */
457 spin_lock(&dcache_lock);
458 if (S_ISDIR(dentry->d_inode->i_mode) &&
459 !d_mountpoint(dentry) &&
Ian Kent90a59c72006-03-27 01:14:50 -0800460 __simple_empty(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 DPRINTK("dentry=%p %.*s, emptydir",
462 dentry, dentry->d_name.len, dentry->d_name.name);
463 spin_unlock(&dcache_lock);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700464 /* The daemon never causes a mount to trigger */
465 if (oz_mode)
466 return 1;
467
468 /*
469 * A zero status is success otherwise we have a
470 * negative error code.
471 */
472 status = try_to_fill_dentry(dentry, flags);
473 if (status == 0)
474 return 1;
475
476 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 spin_unlock(&dcache_lock);
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 1;
481}
482
Ian Kent34ca9592006-03-27 01:14:54 -0800483void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct autofs_info *inf;
486
487 DPRINTK("releasing %p", de);
488
489 inf = autofs4_dentry_ino(de);
490 de->d_fsdata = NULL;
491
492 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800493 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
494
Ian Kentf50b6f82007-02-20 13:58:10 -0800495 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700496 spin_lock(&sbi->lookup_lock);
497 if (!list_empty(&inf->expiring))
498 list_del(&inf->expiring);
499 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800500 }
501
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700502 inf->dentry = NULL;
503 inf->inode = NULL;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 autofs4_free_ino(inf);
506 }
507}
508
509/* For dentries of directories in the root dir */
510static struct dentry_operations autofs4_root_dentry_operations = {
511 .d_revalidate = autofs4_revalidate,
512 .d_release = autofs4_dentry_release,
513};
514
515/* For other dentries */
516static struct dentry_operations autofs4_dentry_operations = {
517 .d_revalidate = autofs4_revalidate,
518 .d_release = autofs4_dentry_release,
519};
520
Ian Kent5f6f4f22008-07-23 21:30:09 -0700521static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
Ian Kentf50b6f82007-02-20 13:58:10 -0800522{
523 unsigned int len = name->len;
524 unsigned int hash = name->hash;
525 const unsigned char *str = name->name;
526 struct list_head *p, *head;
527
528 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700529 spin_lock(&sbi->lookup_lock);
530 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800531 list_for_each(p, head) {
532 struct autofs_info *ino;
533 struct dentry *dentry;
534 struct qstr *qstr;
535
Ian Kent5f6f4f22008-07-23 21:30:09 -0700536 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800537 dentry = ino->dentry;
538
539 spin_lock(&dentry->d_lock);
540
541 /* Bad luck, we've already been dentry_iput */
542 if (!dentry->d_inode)
543 goto next;
544
545 qstr = &dentry->d_name;
546
547 if (dentry->d_name.hash != hash)
548 goto next;
549 if (dentry->d_parent != parent)
550 goto next;
551
552 if (qstr->len != len)
553 goto next;
554 if (memcmp(qstr->name, str, len))
555 goto next;
556
557 if (d_unhashed(dentry)) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800558 dget(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800559 spin_unlock(&dentry->d_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700560 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800561 spin_unlock(&dcache_lock);
562 return dentry;
563 }
564next:
565 spin_unlock(&dentry->d_lock);
566 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700567 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800568 spin_unlock(&dcache_lock);
569
570 return NULL;
571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/* Lookups in the root directory */
574static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
575{
576 struct autofs_sb_info *sbi;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700577 struct dentry *expiring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int oz_mode;
579
580 DPRINTK("name = %.*s",
581 dentry->d_name.len, dentry->d_name.name);
582
Ian Kent718c6042006-03-27 01:14:42 -0800583 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800585 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700591 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
Ian Kent5f6f4f22008-07-23 21:30:09 -0700593 expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
594 if (expiring) {
595 struct autofs_info *ino = autofs4_dentry_ino(expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800596 /*
597 * If we are racing with expire the request might not
598 * be quite complete but the directory has been removed
599 * so it must have been successful, so just wait for it.
600 */
Ian Kent1864f7b2007-08-22 14:01:54 -0700601 while (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800602 DPRINTK("wait for incomplete expire %p name=%.*s",
Ian Kent5f6f4f22008-07-23 21:30:09 -0700603 expiring, expiring->d_name.len,
604 expiring->d_name.name);
605 autofs4_wait(sbi, expiring, NFY_NONE);
Ian Kentf50b6f82007-02-20 13:58:10 -0800606 DPRINTK("request completed");
607 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700608 spin_lock(&sbi->lookup_lock);
609 if (!list_empty(&ino->expiring))
610 list_del_init(&ino->expiring);
611 spin_unlock(&sbi->lookup_lock);
612 dput(expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Ian Kent5f6f4f22008-07-23 21:30:09 -0700615 /*
616 * Mark the dentry incomplete but don't hash it. We do this
617 * to serialize our inode creation operations (symlink and
618 * mkdir) which prevents deadlock during the callback to
619 * the daemon. Subsequent user space lookups for the same
620 * dentry are placed on the wait queue while the daemon
621 * itself is allowed passage unresticted so the create
622 * operation itself can then hash the dentry. Finally,
623 * we check for the hashed dentry and return the newly
624 * hashed dentry.
625 */
626 dentry->d_op = &autofs4_root_dentry_operations;
627
628 dentry->d_fsdata = NULL;
629 d_instantiate(dentry, NULL);
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (!oz_mode) {
632 spin_lock(&dentry->d_lock);
633 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
634 spin_unlock(&dentry->d_lock);
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (dentry->d_op && dentry->d_op->d_revalidate) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800638 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 (dentry->d_op->d_revalidate)(dentry, nd);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800640 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
643 /*
644 * If we are still pending, check if we had to handle
645 * a signal. If so we can force a restart..
646 */
647 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
648 /* See if we were interrupted */
649 if (signal_pending(current)) {
650 sigset_t *sigset = &current->pending.signal;
651 if (sigismember (sigset, SIGKILL) ||
652 sigismember (sigset, SIGQUIT) ||
653 sigismember (sigset, SIGINT)) {
654 return ERR_PTR(-ERESTARTNOINTR);
655 }
656 }
Ian Kent44938af2006-09-29 02:00:54 -0700657 spin_lock(&dentry->d_lock);
658 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
659 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662 /*
663 * If this dentry is unhashed, then we shouldn't honour this
Ian Kentc9ffec42007-02-20 13:58:10 -0800664 * lookup. Returning ENOENT here doesn't do the right thing
665 * for all system calls, but it should be OK for the operations
666 * we permit from an autofs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
Ian Kent1864f7b2007-08-22 14:01:54 -0700668 if (!oz_mode && d_unhashed(dentry)) {
Ian Kentc9ffec42007-02-20 13:58:10 -0800669 /*
670 * A user space application can (and has done in the past)
671 * remove and re-create this directory during the callback.
672 * This can leave us with an unhashed dentry, but a
673 * successful mount! So we need to perform another
674 * cached lookup in case the dentry now exists.
675 */
676 struct dentry *parent = dentry->d_parent;
677 struct dentry *new = d_lookup(parent, &dentry->d_name);
678 if (new != NULL)
679 dentry = new;
680 else
681 dentry = ERR_PTR(-ENOENT);
682
Ian Kentc9ffec42007-02-20 13:58:10 -0800683 return dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800684 }
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return NULL;
687}
688
689static int autofs4_dir_symlink(struct inode *dir,
690 struct dentry *dentry,
691 const char *symname)
692{
693 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
694 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800695 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct inode *inode;
697 char *cp;
698
699 DPRINTK("%s <- %.*s", symname,
700 dentry->d_name.len, dentry->d_name.name);
701
702 if (!autofs4_oz_mode(sbi))
703 return -EACCES;
704
705 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
706 if (ino == NULL)
707 return -ENOSPC;
708
709 ino->size = strlen(symname);
710 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
711
712 if (cp == NULL) {
713 kfree(ino);
714 return -ENOSPC;
715 }
716
717 strcpy(cp, symname);
718
719 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent1864f7b2007-08-22 14:01:54 -0700720 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (dir == dir->i_sb->s_root->d_inode)
723 dentry->d_op = &autofs4_root_dentry_operations;
724 else
725 dentry->d_op = &autofs4_dentry_operations;
726
727 dentry->d_fsdata = ino;
728 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800729 atomic_inc(&ino->count);
730 p_ino = autofs4_dentry_ino(dentry->d_parent);
731 if (p_ino && dentry->d_parent != dentry)
732 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 ino->inode = inode;
734
735 dir->i_mtime = CURRENT_TIME;
736
737 return 0;
738}
739
740/*
741 * NOTE!
742 *
743 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
744 * that the file no longer exists. However, doing that means that the
745 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800746 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700747 * We simply d_drop it and add it to a expiring list in the super block,
748 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 *
750 * If a process is blocked on the dentry waiting for the expire to finish,
751 * it will invalidate the dentry and try to mount with a new one.
752 *
753 * Also see autofs4_dir_rmdir()..
754 */
755static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
756{
757 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
758 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800759 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700762 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -EACCES;
764
Ian Kent1aff3c82006-03-27 01:14:46 -0800765 if (atomic_dec_and_test(&ino->count)) {
766 p_ino = autofs4_dentry_ino(dentry->d_parent);
767 if (p_ino && dentry->d_parent != dentry)
768 atomic_dec(&p_ino->count);
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 dput(ino->dentry);
771
772 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700773 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 dir->i_mtime = CURRENT_TIME;
776
Ian Kentf50b6f82007-02-20 13:58:10 -0800777 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700778 spin_lock(&sbi->lookup_lock);
779 list_add(&ino->expiring, &sbi->expiring_list);
780 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800781 spin_lock(&dentry->d_lock);
782 __d_drop(dentry);
783 spin_unlock(&dentry->d_lock);
784 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 return 0;
787}
788
789static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
790{
791 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
792 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800793 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Ian Kentf50b6f82007-02-20 13:58:10 -0800795 DPRINTK("dentry %p, removing %.*s",
796 dentry, dentry->d_name.len, dentry->d_name.name);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!autofs4_oz_mode(sbi))
799 return -EACCES;
800
801 spin_lock(&dcache_lock);
802 if (!list_empty(&dentry->d_subdirs)) {
803 spin_unlock(&dcache_lock);
804 return -ENOTEMPTY;
805 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700806 spin_lock(&sbi->lookup_lock);
807 list_add(&ino->expiring, &sbi->expiring_list);
808 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 spin_lock(&dentry->d_lock);
810 __d_drop(dentry);
811 spin_unlock(&dentry->d_lock);
812 spin_unlock(&dcache_lock);
813
Ian Kent1aff3c82006-03-27 01:14:46 -0800814 if (atomic_dec_and_test(&ino->count)) {
815 p_ino = autofs4_dentry_ino(dentry->d_parent);
816 if (p_ino && dentry->d_parent != dentry)
817 atomic_dec(&p_ino->count);
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700821 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700824 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 return 0;
827}
828
829static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
830{
831 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
832 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800833 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 struct inode *inode;
835
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700836 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -EACCES;
838
839 DPRINTK("dentry %p, creating %.*s",
840 dentry, dentry->d_name.len, dentry->d_name.name);
841
842 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
843 if (ino == NULL)
844 return -ENOSPC;
845
846 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent1864f7b2007-08-22 14:01:54 -0700847 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 if (dir == dir->i_sb->s_root->d_inode)
850 dentry->d_op = &autofs4_root_dentry_operations;
851 else
852 dentry->d_op = &autofs4_dentry_operations;
853
854 dentry->d_fsdata = ino;
855 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800856 atomic_inc(&ino->count);
857 p_ino = autofs4_dentry_ino(dentry->d_parent);
858 if (p_ino && dentry->d_parent != dentry)
859 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700861 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 dir->i_mtime = CURRENT_TIME;
863
864 return 0;
865}
866
867/* Get/set timeout ioctl() operation */
868static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
869 unsigned long __user *p)
870{
871 int rv;
872 unsigned long ntimeout;
873
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700874 if ((rv = get_user(ntimeout, p)) ||
875 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return rv;
877
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700878 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 sbi->exp_timeout = 0;
880 else
881 sbi->exp_timeout = ntimeout * HZ;
882
883 return 0;
884}
885
886/* Return protocol version */
887static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
888{
889 return put_user(sbi->version, p);
890}
891
892/* Return protocol sub version */
893static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
894{
895 return put_user(sbi->sub_version, p);
896}
897
898/*
899 * Tells the daemon whether we need to reghost or not. Also, clears
900 * the reghost_needed flag.
901 */
902static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
903{
904 int status;
905
906 DPRINTK("returning %d", sbi->needs_reghost);
907
908 status = put_user(sbi->needs_reghost, p);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700909 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 return status;
911
912 sbi->needs_reghost = 0;
913 return 0;
914}
915
916/*
917 * Enable / Disable reghosting ioctl() operation
918 */
919static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
920{
921 int status;
922 int val;
923
924 status = get_user(val, p);
925
926 DPRINTK("reghost = %d", val);
927
928 if (status)
929 return status;
930
931 /* turn on/off reghosting, with the val */
932 sbi->reghost_enabled = val;
933 return 0;
934}
935
936/*
937* Tells the daemon whether it can umount the autofs mount.
938*/
939static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
940{
941 int status = 0;
942
Ian Kente3474a82006-03-27 01:14:51 -0800943 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 status = 1;
945
946 DPRINTK("returning %d", status);
947
948 status = put_user(status, p);
949
950 return status;
951}
952
953/* Identify autofs4_dentries - this is so we can tell if there's
954 an extra dentry refcount or not. We only hold a refcount on the
955 dentry if its non-negative (ie, d_inode != NULL)
956*/
957int is_autofs4_dentry(struct dentry *dentry)
958{
959 return dentry && dentry->d_inode &&
960 (dentry->d_op == &autofs4_root_dentry_operations ||
961 dentry->d_op == &autofs4_dentry_operations) &&
962 dentry->d_fsdata != NULL;
963}
964
965/*
966 * ioctl()'s on the root directory is the chief method for the daemon to
967 * generate kernel reactions
968 */
969static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
970 unsigned int cmd, unsigned long arg)
971{
972 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
973 void __user *p = (void __user *)arg;
974
975 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700976 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700978 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
979 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return -ENOTTY;
981
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700982 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return -EPERM;
984
985 switch(cmd) {
986 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
987 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
988 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
989 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
990 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
991 autofs4_catatonic_mode(sbi);
992 return 0;
993 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
994 return autofs4_get_protover(sbi, p);
995 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
996 return autofs4_get_protosubver(sbi, p);
997 case AUTOFS_IOC_SETTIMEOUT:
998 return autofs4_get_set_timeout(sbi, p);
999
1000 case AUTOFS_IOC_TOGGLEREGHOST:
1001 return autofs4_toggle_reghost(sbi, p);
1002 case AUTOFS_IOC_ASKREGHOST:
1003 return autofs4_ask_reghost(sbi, p);
1004
1005 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001006 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 /* return a single thing to expire */
1009 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001010 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* same as above, but can send multiple expires through pipe */
1012 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001013 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
1015 default:
1016 return -ENOSYS;
1017 }
1018}