blob: 87352654ff4ed3bd1558d54ad428dacd98f97e4d [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
Ian Kent6d5cb922008-07-23 21:30:15 -070034#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
35#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
36
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080037const struct file_operations autofs4_root_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 .open = dcache_dir_open,
39 .release = dcache_dir_close,
40 .read = generic_read_dir,
41 .readdir = autofs4_root_readdir,
42 .ioctl = autofs4_root_ioctl,
43};
44
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080045const struct file_operations autofs4_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 .open = autofs4_dir_open,
47 .release = autofs4_dir_close,
48 .read = generic_read_dir,
49 .readdir = autofs4_dir_readdir,
50};
51
Arjan van de Ven754661f2007-02-12 00:55:38 -080052const struct inode_operations autofs4_indirect_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 .lookup = autofs4_lookup,
54 .unlink = autofs4_dir_unlink,
55 .symlink = autofs4_dir_symlink,
56 .mkdir = autofs4_dir_mkdir,
57 .rmdir = autofs4_dir_rmdir,
58};
59
Arjan van de Ven754661f2007-02-12 00:55:38 -080060const struct inode_operations autofs4_direct_root_inode_operations = {
Ian Kent34ca9592006-03-27 01:14:54 -080061 .lookup = autofs4_lookup,
Ian Kent871f9432006-03-27 01:14:58 -080062 .unlink = autofs4_dir_unlink,
63 .mkdir = autofs4_dir_mkdir,
64 .rmdir = autofs4_dir_rmdir,
Ian Kent34ca9592006-03-27 01:14:54 -080065 .follow_link = autofs4_follow_link,
66};
67
Arjan van de Ven754661f2007-02-12 00:55:38 -080068const struct inode_operations autofs4_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 .lookup = autofs4_lookup,
70 .unlink = autofs4_dir_unlink,
71 .symlink = autofs4_dir_symlink,
72 .mkdir = autofs4_dir_mkdir,
73 .rmdir = autofs4_dir_rmdir,
74};
75
76static int autofs4_root_readdir(struct file *file, void *dirent,
77 filldir_t filldir)
78{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -080079 struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int oz_mode = autofs4_oz_mode(sbi);
81
82 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
83
84 /*
85 * Don't set reghost flag if:
86 * 1) f_pos is larger than zero -- we've already been here.
87 * 2) we haven't even enabled reghosting in the 1st place.
88 * 3) this is the daemon doing a readdir
89 */
90 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
91 sbi->needs_reghost = 1;
92
93 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
94
Ian Kentf360ce32006-03-27 01:14:43 -080095 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int autofs4_dir_open(struct inode *inode, struct file *file)
99{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800100 struct dentry *dentry = file->f_path.dentry;
101 struct vfsmount *mnt = file->f_path.mnt;
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 struct dentry *cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int status;
105
Ian Kentf360ce32006-03-27 01:14:43 -0800106 status = dcache_dir_open(inode, file);
107 if (status)
108 goto out;
109
110 cursor = file->private_data;
111 cursor->d_fsdata = NULL;
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 DPRINTK("file=%p dentry=%p %.*s",
114 file, dentry, dentry->d_name.len, dentry->d_name.name);
115
116 if (autofs4_oz_mode(sbi))
117 goto out;
118
119 if (autofs4_ispending(dentry)) {
120 DPRINTK("dentry busy");
Ian Kentf360ce32006-03-27 01:14:43 -0800121 dcache_dir_close(inode, file);
122 status = -EBUSY;
123 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125
Ian Kentf360ce32006-03-27 01:14:43 -0800126 status = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
128 struct nameidata nd;
Ian Kentf360ce32006-03-27 01:14:43 -0800129 int empty, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /* In case there are stale directory dentrys from a failed mount */
132 spin_lock(&dcache_lock);
133 empty = list_empty(&dentry->d_subdirs);
134 spin_unlock(&dcache_lock);
135
136 if (!empty)
137 d_invalidate(dentry);
138
139 nd.flags = LOOKUP_DIRECTORY;
Ian Kentf360ce32006-03-27 01:14:43 -0800140 ret = (dentry->d_op->d_revalidate)(dentry, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Ian Kentbcdc5e02006-09-27 01:50:44 -0700142 if (ret <= 0) {
143 if (ret < 0)
144 status = ret;
Ian Kentf360ce32006-03-27 01:14:43 -0800145 dcache_dir_close(inode, file);
146 goto out;
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 }
149
150 if (d_mountpoint(dentry)) {
151 struct file *fp = NULL;
Jan Blunck868eb7a2008-05-01 04:35:10 -0700152 struct path fp_path = { .dentry = dentry, .mnt = mnt };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Jan Blunck868eb7a2008-05-01 04:35:10 -0700154 path_get(&fp_path);
155
156 if (!autofs4_follow_mount(&fp_path.mnt, &fp_path.dentry)) {
157 path_put(&fp_path);
Ian Kentf360ce32006-03-27 01:14:43 -0800158 dcache_dir_close(inode, file);
159 goto out;
Ian Kent9b1e3af2005-06-21 17:16:38 -0700160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Jan Blunck868eb7a2008-05-01 04:35:10 -0700162 fp = dentry_open(fp_path.dentry, fp_path.mnt, file->f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 status = PTR_ERR(fp);
164 if (IS_ERR(fp)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800165 dcache_dir_close(inode, file);
166 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Ian Kentf360ce32006-03-27 01:14:43 -0800168 cursor->d_fsdata = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
Ian Kentf360ce32006-03-27 01:14:43 -0800171out:
172 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175static int autofs4_dir_close(struct inode *inode, struct file *file)
176{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800177 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800179 struct dentry *cursor = file->private_data;
180 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 DPRINTK("file=%p dentry=%p %.*s",
183 file, dentry, dentry->d_name.len, dentry->d_name.name);
184
185 if (autofs4_oz_mode(sbi))
186 goto out;
187
188 if (autofs4_ispending(dentry)) {
189 DPRINTK("dentry busy");
Ian Kentf360ce32006-03-27 01:14:43 -0800190 status = -EBUSY;
191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
194 if (d_mountpoint(dentry)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800195 struct file *fp = cursor->d_fsdata;
196 if (!fp) {
197 status = -ENOENT;
198 goto out;
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 filp_close(fp, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
202out:
Ian Kentf360ce32006-03-27 01:14:43 -0800203 dcache_dir_close(inode, file);
204 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
208{
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -0800209 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800211 struct dentry *cursor = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 int status;
213
214 DPRINTK("file=%p dentry=%p %.*s",
215 file, dentry, dentry->d_name.len, dentry->d_name.name);
216
217 if (autofs4_oz_mode(sbi))
218 goto out;
219
220 if (autofs4_ispending(dentry)) {
221 DPRINTK("dentry busy");
222 return -EBUSY;
223 }
224
225 if (d_mountpoint(dentry)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800226 struct file *fp = cursor->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (!fp)
229 return -ENOENT;
230
231 if (!fp->f_op || !fp->f_op->readdir)
232 goto out;
233
234 status = vfs_readdir(fp, filldir, dirent);
235 file->f_pos = fp->f_pos;
236 if (status)
237 autofs4_copy_atime(file, fp);
238 return status;
239 }
240out:
Ian Kentf360ce32006-03-27 01:14:43 -0800241 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Ian Kent862b1102006-03-27 01:14:48 -0800244static int try_to_fill_dentry(struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Ian Kent862b1102006-03-27 01:14:48 -0800246 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800247 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700248 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* Block on any pending expiry here; invalidate the dentry
251 when expiration is done to trigger mount request with a new
252 dentry */
Ian Kent718c6042006-03-27 01:14:42 -0800253 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 DPRINTK("waiting for expire %p name=%.*s",
255 dentry, dentry->d_name.len, dentry->d_name.name);
256
257 status = autofs4_wait(sbi, dentry, NFY_NONE);
Ian Kent718c6042006-03-27 01:14:42 -0800258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 DPRINTK("expire done status=%d", status);
Ian Kent718c6042006-03-27 01:14:42 -0800260
Ian Kent1684b2b2005-06-21 17:16:41 -0700261 /*
262 * If the directory still exists the mount request must
263 * continue otherwise it can't be followed at the right
264 * time during the walk.
265 */
266 status = d_invalidate(dentry);
267 if (status != -EBUSY)
Ian Kentf50b6f82007-02-20 13:58:10 -0800268 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
271 DPRINTK("dentry=%p %.*s ino=%p",
272 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
273
Ian Kent718c6042006-03-27 01:14:42 -0800274 /*
275 * Wait for a pending mount, triggering one if there
276 * isn't one already
277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 if (dentry->d_inode == NULL) {
279 DPRINTK("waiting for mount name=%.*s",
280 dentry->d_name.len, dentry->d_name.name);
281
282 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 DPRINTK("mount done status=%d", status);
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* Turn this into a real negative dentry? */
287 if (status == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 spin_lock(&dentry->d_lock);
289 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
290 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800291 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 } else if (status) {
293 /* Return a negative dentry, but leave it "pending" */
Ian Kent34ca9592006-03-27 01:14:54 -0800294 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296 /* Trigger mount for path component or follow link */
Ian Kent6d5cb922008-07-23 21:30:15 -0700297 } else if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 current->link_count) {
299 DPRINTK("waiting for mount name=%.*s",
300 dentry->d_name.len, dentry->d_name.name);
301
302 spin_lock(&dentry->d_lock);
303 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
304 spin_unlock(&dentry->d_lock);
305 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
306
307 DPRINTK("mount done status=%d", status);
308
309 if (status) {
310 spin_lock(&dentry->d_lock);
311 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
312 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800313 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315 }
316
Ian Kente0a7aae2006-03-27 01:14:47 -0800317 /* Initialize expiry counter after successful mount */
318 if (ino)
319 ino->last_used = jiffies;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 spin_lock(&dentry->d_lock);
322 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
323 spin_unlock(&dentry->d_lock);
Jeff Moyer03379042008-05-01 04:35:08 -0700324
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700325 return 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800326}
327
328/* For autofs direct mounts the follow link triggers the mount */
329static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
330{
331 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kenta5370552006-05-15 09:43:51 -0700332 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent34ca9592006-03-27 01:14:54 -0800333 int oz_mode = autofs4_oz_mode(sbi);
334 unsigned int lookup_type;
335 int status;
336
337 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
338 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
339 nd->flags);
340
341 /* If it's our master or we shouldn't trigger a mount we're done */
Ian Kent6d5cb922008-07-23 21:30:15 -0700342 lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS);
Ian Kent34ca9592006-03-27 01:14:54 -0800343 if (oz_mode || !lookup_type)
344 goto done;
345
Ian Kenta5370552006-05-15 09:43:51 -0700346 /* If an expire request is pending wait for it. */
347 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Ian Kent871f9432006-03-27 01:14:58 -0800348 DPRINTK("waiting for active request %p name=%.*s",
349 dentry, dentry->d_name.len, dentry->d_name.name);
Ian Kent34ca9592006-03-27 01:14:54 -0800350
Ian Kent871f9432006-03-27 01:14:58 -0800351 status = autofs4_wait(sbi, dentry, NFY_NONE);
352
353 DPRINTK("request done status=%d", status);
Ian Kent34ca9592006-03-27 01:14:54 -0800354 }
355
Ian Kent871f9432006-03-27 01:14:58 -0800356 /*
357 * If the dentry contains directories then it is an
358 * autofs multi-mount with no root mount offset. So
359 * don't try to mount it again.
360 */
361 spin_lock(&dcache_lock);
Ian Kentbe3ca7f2006-09-29 02:00:53 -0700362 if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
Ian Kent871f9432006-03-27 01:14:58 -0800363 spin_unlock(&dcache_lock);
364
365 status = try_to_fill_dentry(dentry, 0);
366 if (status)
367 goto out_error;
368
369 /*
370 * The mount succeeded but if there is no root mount
371 * it must be an autofs multi-mount with no root offset
372 * so we don't need to follow the mount.
373 */
374 if (d_mountpoint(dentry)) {
Jan Blunck4ac91372008-02-14 19:34:32 -0800375 if (!autofs4_follow_mount(&nd->path.mnt,
376 &nd->path.dentry)) {
Ian Kent871f9432006-03-27 01:14:58 -0800377 status = -ENOENT;
378 goto out_error;
379 }
380 }
381
382 goto done;
383 }
384 spin_unlock(&dcache_lock);
385
Ian Kent34ca9592006-03-27 01:14:54 -0800386done:
387 return NULL;
388
389out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800390 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800391 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/*
395 * Revalidate is called on every cache lookup. Some of those
396 * cache lookups may actually happen while the dentry is not
397 * yet completely filled in, and revalidate has to delay such
398 * lookups..
399 */
Ian Kent718c6042006-03-27 01:14:42 -0800400static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Ian Kent718c6042006-03-27 01:14:42 -0800402 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
404 int oz_mode = autofs4_oz_mode(sbi);
405 int flags = nd ? nd->flags : 0;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700406 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* Pending dentry */
409 if (autofs4_ispending(dentry)) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700410 /* The daemon never causes a mount to trigger */
411 if (oz_mode)
412 return 1;
413
414 /*
415 * A zero status is success otherwise we have a
416 * negative error code.
417 */
418 status = try_to_fill_dentry(dentry, flags);
419 if (status == 0)
Ian Kentf50b6f82007-02-20 13:58:10 -0800420 return 1;
421
422 /*
423 * A status of EAGAIN here means that the dentry has gone
424 * away while waiting for an expire to complete. If we are
425 * racing with expire lookup will wait for it so this must
426 * be a revalidate and we need to send it to lookup.
427 */
428 if (status == -EAGAIN)
429 return 0;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700430
431 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
434 /* Negative dentry.. invalidate if "old" */
435 if (dentry->d_inode == NULL)
Ian Kent2d753e62006-03-27 01:14:44 -0800436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /* Check for a non-mountpoint directory with no contents */
439 spin_lock(&dcache_lock);
440 if (S_ISDIR(dentry->d_inode->i_mode) &&
441 !d_mountpoint(dentry) &&
Ian Kent90a59c72006-03-27 01:14:50 -0800442 __simple_empty(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 DPRINTK("dentry=%p %.*s, emptydir",
444 dentry, dentry->d_name.len, dentry->d_name.name);
445 spin_unlock(&dcache_lock);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700446 /* The daemon never causes a mount to trigger */
447 if (oz_mode)
448 return 1;
449
450 /*
451 * A zero status is success otherwise we have a
452 * negative error code.
453 */
454 status = try_to_fill_dentry(dentry, flags);
455 if (status == 0)
456 return 1;
457
458 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460 spin_unlock(&dcache_lock);
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 1;
463}
464
Ian Kent34ca9592006-03-27 01:14:54 -0800465void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct autofs_info *inf;
468
469 DPRINTK("releasing %p", de);
470
471 inf = autofs4_dentry_ino(de);
472 de->d_fsdata = NULL;
473
474 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800475 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
476
Ian Kentf50b6f82007-02-20 13:58:10 -0800477 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700478 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700479 if (!list_empty(&inf->active))
480 list_del(&inf->active);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700481 if (!list_empty(&inf->expiring))
482 list_del(&inf->expiring);
483 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800484 }
485
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700486 inf->dentry = NULL;
487 inf->inode = NULL;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 autofs4_free_ino(inf);
490 }
491}
492
493/* For dentries of directories in the root dir */
494static struct dentry_operations autofs4_root_dentry_operations = {
495 .d_revalidate = autofs4_revalidate,
496 .d_release = autofs4_dentry_release,
497};
498
499/* For other dentries */
500static struct dentry_operations autofs4_dentry_operations = {
501 .d_revalidate = autofs4_revalidate,
502 .d_release = autofs4_dentry_release,
503};
504
Ian Kent25767372008-07-23 21:30:12 -0700505static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
506{
507 unsigned int len = name->len;
508 unsigned int hash = name->hash;
509 const unsigned char *str = name->name;
510 struct list_head *p, *head;
511
512 spin_lock(&dcache_lock);
513 spin_lock(&sbi->lookup_lock);
514 head = &sbi->active_list;
515 list_for_each(p, head) {
516 struct autofs_info *ino;
517 struct dentry *dentry;
518 struct qstr *qstr;
519
520 ino = list_entry(p, struct autofs_info, active);
521 dentry = ino->dentry;
522
523 spin_lock(&dentry->d_lock);
524
525 /* Already gone? */
526 if (atomic_read(&dentry->d_count) == 0)
527 goto next;
528
529 qstr = &dentry->d_name;
530
531 if (dentry->d_name.hash != hash)
532 goto next;
533 if (dentry->d_parent != parent)
534 goto next;
535
536 if (qstr->len != len)
537 goto next;
538 if (memcmp(qstr->name, str, len))
539 goto next;
540
541 if (d_unhashed(dentry)) {
542 dget(dentry);
543 spin_unlock(&dentry->d_lock);
544 spin_unlock(&sbi->lookup_lock);
545 spin_unlock(&dcache_lock);
546 return dentry;
547 }
548next:
549 spin_unlock(&dentry->d_lock);
550 }
551 spin_unlock(&sbi->lookup_lock);
552 spin_unlock(&dcache_lock);
553
554 return NULL;
555}
556
Ian Kent5f6f4f22008-07-23 21:30:09 -0700557static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
Ian Kentf50b6f82007-02-20 13:58:10 -0800558{
559 unsigned int len = name->len;
560 unsigned int hash = name->hash;
561 const unsigned char *str = name->name;
562 struct list_head *p, *head;
563
564 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700565 spin_lock(&sbi->lookup_lock);
566 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800567 list_for_each(p, head) {
568 struct autofs_info *ino;
569 struct dentry *dentry;
570 struct qstr *qstr;
571
Ian Kent5f6f4f22008-07-23 21:30:09 -0700572 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800573 dentry = ino->dentry;
574
575 spin_lock(&dentry->d_lock);
576
577 /* Bad luck, we've already been dentry_iput */
578 if (!dentry->d_inode)
579 goto next;
580
581 qstr = &dentry->d_name;
582
583 if (dentry->d_name.hash != hash)
584 goto next;
585 if (dentry->d_parent != parent)
586 goto next;
587
588 if (qstr->len != len)
589 goto next;
590 if (memcmp(qstr->name, str, len))
591 goto next;
592
593 if (d_unhashed(dentry)) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800594 dget(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800595 spin_unlock(&dentry->d_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700596 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800597 spin_unlock(&dcache_lock);
598 return dentry;
599 }
600next:
601 spin_unlock(&dentry->d_lock);
602 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700603 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800604 spin_unlock(&dcache_lock);
605
606 return NULL;
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/* Lookups in the root directory */
610static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
611{
612 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700613 struct autofs_info *ino;
614 struct dentry *expiring, *unhashed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 int oz_mode;
616
617 DPRINTK("name = %.*s",
618 dentry->d_name.len, dentry->d_name.name);
619
Ian Kent718c6042006-03-27 01:14:42 -0800620 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800622 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700628 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Ian Kent5f6f4f22008-07-23 21:30:09 -0700630 expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
631 if (expiring) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800632 /*
633 * If we are racing with expire the request might not
634 * be quite complete but the directory has been removed
635 * so it must have been successful, so just wait for it.
636 */
Ian Kent25767372008-07-23 21:30:12 -0700637 ino = autofs4_dentry_ino(expiring);
Ian Kent1864f7b2007-08-22 14:01:54 -0700638 while (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800639 DPRINTK("wait for incomplete expire %p name=%.*s",
Ian Kent5f6f4f22008-07-23 21:30:09 -0700640 expiring, expiring->d_name.len,
641 expiring->d_name.name);
642 autofs4_wait(sbi, expiring, NFY_NONE);
Ian Kentf50b6f82007-02-20 13:58:10 -0800643 DPRINTK("request completed");
644 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700645 spin_lock(&sbi->lookup_lock);
646 if (!list_empty(&ino->expiring))
647 list_del_init(&ino->expiring);
648 spin_unlock(&sbi->lookup_lock);
649 dput(expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Ian Kent25767372008-07-23 21:30:12 -0700652 unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
653 if (unhashed)
654 dentry = unhashed;
655 else {
656 /*
657 * Mark the dentry incomplete but don't hash it. We do this
658 * to serialize our inode creation operations (symlink and
659 * mkdir) which prevents deadlock during the callback to
660 * the daemon. Subsequent user space lookups for the same
661 * dentry are placed on the wait queue while the daemon
662 * itself is allowed passage unresticted so the create
663 * operation itself can then hash the dentry. Finally,
664 * we check for the hashed dentry and return the newly
665 * hashed dentry.
666 */
667 dentry->d_op = &autofs4_root_dentry_operations;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700668
Ian Kent25767372008-07-23 21:30:12 -0700669 /*
670 * And we need to ensure that the same dentry is used for
671 * all following lookup calls until it is hashed so that
672 * the dentry flags are persistent throughout the request.
673 */
674 ino = autofs4_init_ino(NULL, sbi, 0555);
675 if (!ino)
676 return ERR_PTR(-ENOMEM);
677
678 dentry->d_fsdata = ino;
679 ino->dentry = dentry;
680
681 spin_lock(&sbi->lookup_lock);
682 list_add(&ino->active, &sbi->active_list);
683 spin_unlock(&sbi->lookup_lock);
684
685 d_instantiate(dentry, NULL);
686 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (!oz_mode) {
689 spin_lock(&dentry->d_lock);
690 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
691 spin_unlock(&dentry->d_lock);
Ian Kentc432c252008-07-23 21:30:14 -0700692 if (dentry->d_op && dentry->d_op->d_revalidate) {
693 mutex_unlock(&dir->i_mutex);
694 (dentry->d_op->d_revalidate)(dentry, nd);
695 mutex_lock(&dir->i_mutex);
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 /*
700 * If we are still pending, check if we had to handle
701 * a signal. If so we can force a restart..
702 */
703 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
704 /* See if we were interrupted */
705 if (signal_pending(current)) {
706 sigset_t *sigset = &current->pending.signal;
707 if (sigismember (sigset, SIGKILL) ||
708 sigismember (sigset, SIGQUIT) ||
709 sigismember (sigset, SIGINT)) {
Ian Kent25767372008-07-23 21:30:12 -0700710 if (unhashed)
711 dput(unhashed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return ERR_PTR(-ERESTARTNOINTR);
713 }
714 }
Ian Kent25767372008-07-23 21:30:12 -0700715 if (!oz_mode) {
716 spin_lock(&dentry->d_lock);
717 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
718 spin_unlock(&dentry->d_lock);
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721
722 /*
723 * If this dentry is unhashed, then we shouldn't honour this
Ian Kentc9ffec42007-02-20 13:58:10 -0800724 * lookup. Returning ENOENT here doesn't do the right thing
725 * for all system calls, but it should be OK for the operations
726 * we permit from an autofs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 */
Ian Kent1864f7b2007-08-22 14:01:54 -0700728 if (!oz_mode && d_unhashed(dentry)) {
Ian Kentc9ffec42007-02-20 13:58:10 -0800729 /*
730 * A user space application can (and has done in the past)
731 * remove and re-create this directory during the callback.
732 * This can leave us with an unhashed dentry, but a
733 * successful mount! So we need to perform another
734 * cached lookup in case the dentry now exists.
735 */
736 struct dentry *parent = dentry->d_parent;
737 struct dentry *new = d_lookup(parent, &dentry->d_name);
738 if (new != NULL)
739 dentry = new;
740 else
741 dentry = ERR_PTR(-ENOENT);
742
Ian Kent25767372008-07-23 21:30:12 -0700743 if (unhashed)
744 dput(unhashed);
745
Ian Kentc9ffec42007-02-20 13:58:10 -0800746 return dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800747 }
748
Ian Kent25767372008-07-23 21:30:12 -0700749 if (unhashed)
750 return unhashed;
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return NULL;
753}
754
755static int autofs4_dir_symlink(struct inode *dir,
756 struct dentry *dentry,
757 const char *symname)
758{
759 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
760 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800761 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct inode *inode;
763 char *cp;
764
765 DPRINTK("%s <- %.*s", symname,
766 dentry->d_name.len, dentry->d_name.name);
767
768 if (!autofs4_oz_mode(sbi))
769 return -EACCES;
770
771 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700772 if (!ino)
773 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Ian Kent25767372008-07-23 21:30:12 -0700775 spin_lock(&sbi->lookup_lock);
776 if (!list_empty(&ino->active))
777 list_del_init(&ino->active);
778 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Ian Kentef581a72008-07-23 21:30:13 -0700780 ino->size = strlen(symname);
Ian Kent25767372008-07-23 21:30:12 -0700781 cp = kmalloc(ino->size + 1, GFP_KERNEL);
782 if (!cp) {
783 if (!dentry->d_fsdata)
784 kfree(ino);
785 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
788 strcpy(cp, symname);
789
790 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700791 if (!inode) {
792 kfree(cp);
793 if (!dentry->d_fsdata)
794 kfree(ino);
795 return -ENOMEM;
796 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700797 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 if (dir == dir->i_sb->s_root->d_inode)
800 dentry->d_op = &autofs4_root_dentry_operations;
801 else
802 dentry->d_op = &autofs4_dentry_operations;
803
804 dentry->d_fsdata = ino;
805 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800806 atomic_inc(&ino->count);
807 p_ino = autofs4_dentry_ino(dentry->d_parent);
808 if (p_ino && dentry->d_parent != dentry)
809 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ino->inode = inode;
811
Ian Kent25767372008-07-23 21:30:12 -0700812 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 dir->i_mtime = CURRENT_TIME;
814
815 return 0;
816}
817
818/*
819 * NOTE!
820 *
821 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
822 * that the file no longer exists. However, doing that means that the
823 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800824 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700825 * We simply d_drop it and add it to a expiring list in the super block,
826 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 *
828 * If a process is blocked on the dentry waiting for the expire to finish,
829 * it will invalidate the dentry and try to mount with a new one.
830 *
831 * Also see autofs4_dir_rmdir()..
832 */
833static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
834{
835 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
836 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800837 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700840 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return -EACCES;
842
Ian Kent1aff3c82006-03-27 01:14:46 -0800843 if (atomic_dec_and_test(&ino->count)) {
844 p_ino = autofs4_dentry_ino(dentry->d_parent);
845 if (p_ino && dentry->d_parent != dentry)
846 atomic_dec(&p_ino->count);
847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 dput(ino->dentry);
849
850 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700851 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 dir->i_mtime = CURRENT_TIME;
854
Ian Kentf50b6f82007-02-20 13:58:10 -0800855 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700856 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700857 if (list_empty(&ino->expiring))
858 list_add(&ino->expiring, &sbi->expiring_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700859 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800860 spin_lock(&dentry->d_lock);
861 __d_drop(dentry);
862 spin_unlock(&dentry->d_lock);
863 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 return 0;
866}
867
868static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
869{
870 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
871 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800872 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Ian Kentf50b6f82007-02-20 13:58:10 -0800874 DPRINTK("dentry %p, removing %.*s",
875 dentry, dentry->d_name.len, dentry->d_name.name);
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!autofs4_oz_mode(sbi))
878 return -EACCES;
879
880 spin_lock(&dcache_lock);
881 if (!list_empty(&dentry->d_subdirs)) {
882 spin_unlock(&dcache_lock);
883 return -ENOTEMPTY;
884 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700885 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700886 if (list_empty(&ino->expiring))
887 list_add(&ino->expiring, &sbi->expiring_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700888 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 spin_lock(&dentry->d_lock);
890 __d_drop(dentry);
891 spin_unlock(&dentry->d_lock);
892 spin_unlock(&dcache_lock);
893
Ian Kent1aff3c82006-03-27 01:14:46 -0800894 if (atomic_dec_and_test(&ino->count)) {
895 p_ino = autofs4_dentry_ino(dentry->d_parent);
896 if (p_ino && dentry->d_parent != dentry)
897 atomic_dec(&p_ino->count);
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700901 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700904 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 return 0;
907}
908
909static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
910{
911 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
912 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800913 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct inode *inode;
915
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700916 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 return -EACCES;
918
919 DPRINTK("dentry %p, creating %.*s",
920 dentry, dentry->d_name.len, dentry->d_name.name);
921
922 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700923 if (!ino)
924 return -ENOMEM;
925
926 spin_lock(&sbi->lookup_lock);
927 if (!list_empty(&ino->active))
928 list_del_init(&ino->active);
929 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700932 if (!inode) {
933 if (!dentry->d_fsdata)
934 kfree(ino);
935 return -ENOMEM;
936 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700937 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (dir == dir->i_sb->s_root->d_inode)
940 dentry->d_op = &autofs4_root_dentry_operations;
941 else
942 dentry->d_op = &autofs4_dentry_operations;
943
944 dentry->d_fsdata = ino;
945 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800946 atomic_inc(&ino->count);
947 p_ino = autofs4_dentry_ino(dentry->d_parent);
948 if (p_ino && dentry->d_parent != dentry)
949 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700951 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 dir->i_mtime = CURRENT_TIME;
953
954 return 0;
955}
956
957/* Get/set timeout ioctl() operation */
958static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
959 unsigned long __user *p)
960{
961 int rv;
962 unsigned long ntimeout;
963
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700964 if ((rv = get_user(ntimeout, p)) ||
965 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return rv;
967
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700968 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 sbi->exp_timeout = 0;
970 else
971 sbi->exp_timeout = ntimeout * HZ;
972
973 return 0;
974}
975
976/* Return protocol version */
977static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
978{
979 return put_user(sbi->version, p);
980}
981
982/* Return protocol sub version */
983static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
984{
985 return put_user(sbi->sub_version, p);
986}
987
988/*
989 * Tells the daemon whether we need to reghost or not. Also, clears
990 * the reghost_needed flag.
991 */
992static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
993{
994 int status;
995
996 DPRINTK("returning %d", sbi->needs_reghost);
997
998 status = put_user(sbi->needs_reghost, p);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700999 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 return status;
1001
1002 sbi->needs_reghost = 0;
1003 return 0;
1004}
1005
1006/*
1007 * Enable / Disable reghosting ioctl() operation
1008 */
1009static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
1010{
1011 int status;
1012 int val;
1013
1014 status = get_user(val, p);
1015
1016 DPRINTK("reghost = %d", val);
1017
1018 if (status)
1019 return status;
1020
1021 /* turn on/off reghosting, with the val */
1022 sbi->reghost_enabled = val;
1023 return 0;
1024}
1025
1026/*
1027* Tells the daemon whether it can umount the autofs mount.
1028*/
1029static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
1030{
1031 int status = 0;
1032
Ian Kente3474a82006-03-27 01:14:51 -08001033 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 status = 1;
1035
1036 DPRINTK("returning %d", status);
1037
1038 status = put_user(status, p);
1039
1040 return status;
1041}
1042
1043/* Identify autofs4_dentries - this is so we can tell if there's
1044 an extra dentry refcount or not. We only hold a refcount on the
1045 dentry if its non-negative (ie, d_inode != NULL)
1046*/
1047int is_autofs4_dentry(struct dentry *dentry)
1048{
1049 return dentry && dentry->d_inode &&
1050 (dentry->d_op == &autofs4_root_dentry_operations ||
1051 dentry->d_op == &autofs4_dentry_operations) &&
1052 dentry->d_fsdata != NULL;
1053}
1054
1055/*
1056 * ioctl()'s on the root directory is the chief method for the daemon to
1057 * generate kernel reactions
1058 */
1059static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
1060 unsigned int cmd, unsigned long arg)
1061{
1062 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
1063 void __user *p = (void __user *)arg;
1064
1065 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -07001066 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -07001068 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
1069 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return -ENOTTY;
1071
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -07001072 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return -EPERM;
1074
1075 switch(cmd) {
1076 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
1077 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
1078 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
1079 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
1080 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
1081 autofs4_catatonic_mode(sbi);
1082 return 0;
1083 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
1084 return autofs4_get_protover(sbi, p);
1085 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
1086 return autofs4_get_protosubver(sbi, p);
1087 case AUTOFS_IOC_SETTIMEOUT:
1088 return autofs4_get_set_timeout(sbi, p);
1089
1090 case AUTOFS_IOC_TOGGLEREGHOST:
1091 return autofs4_toggle_reghost(sbi, p);
1092 case AUTOFS_IOC_ASKREGHOST:
1093 return autofs4_ask_reghost(sbi, p);
1094
1095 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001096 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 /* return a single thing to expire */
1099 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001100 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 /* same as above, but can send multiple expires through pipe */
1102 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001103 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 default:
1106 return -ENOSYS;
1107 }
1108}