blob: dbb70d5a48827f0516db8a1455bea8b4a29b6cbb [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 Moyer9d2de6a2008-05-01 04:35:09 -0700245 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* Block on any pending expiry here; invalidate the dentry
248 when expiration is done to trigger mount request with a new
249 dentry */
Ian Kent718c6042006-03-27 01:14:42 -0800250 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 DPRINTK("waiting for expire %p name=%.*s",
252 dentry, dentry->d_name.len, dentry->d_name.name);
253
254 status = autofs4_wait(sbi, dentry, NFY_NONE);
Ian Kent718c6042006-03-27 01:14:42 -0800255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 DPRINTK("expire done status=%d", status);
Ian Kent718c6042006-03-27 01:14:42 -0800257
Ian Kent1684b2b2005-06-21 17:16:41 -0700258 /*
259 * If the directory still exists the mount request must
260 * continue otherwise it can't be followed at the right
261 * time during the walk.
262 */
263 status = d_invalidate(dentry);
264 if (status != -EBUSY)
Ian Kentf50b6f82007-02-20 13:58:10 -0800265 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
268 DPRINTK("dentry=%p %.*s ino=%p",
269 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
270
Ian Kent718c6042006-03-27 01:14:42 -0800271 /*
272 * Wait for a pending mount, triggering one if there
273 * isn't one already
274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (dentry->d_inode == NULL) {
276 DPRINTK("waiting for mount name=%.*s",
277 dentry->d_name.len, dentry->d_name.name);
278
279 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 DPRINTK("mount done status=%d", status);
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /* Turn this into a real negative dentry? */
284 if (status == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 spin_lock(&dentry->d_lock);
286 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
287 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800288 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 } else if (status) {
290 /* Return a negative dentry, but leave it "pending" */
Ian Kent34ca9592006-03-27 01:14:54 -0800291 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293 /* Trigger mount for path component or follow link */
294 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
295 current->link_count) {
296 DPRINTK("waiting for mount name=%.*s",
297 dentry->d_name.len, dentry->d_name.name);
298
299 spin_lock(&dentry->d_lock);
300 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
301 spin_unlock(&dentry->d_lock);
302 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
303
304 DPRINTK("mount done status=%d", status);
305
306 if (status) {
307 spin_lock(&dentry->d_lock);
308 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
309 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800310 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312 }
313
Ian Kente0a7aae2006-03-27 01:14:47 -0800314 /* Initialize expiry counter after successful mount */
315 if (ino)
316 ino->last_used = jiffies;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 spin_lock(&dentry->d_lock);
319 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
320 spin_unlock(&dentry->d_lock);
Jeff Moyer03379042008-05-01 04:35:08 -0700321
Jeff Moyer9d2de6a2008-05-01 04:35:09 -0700322 return 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800323}
324
325/* For autofs direct mounts the follow link triggers the mount */
326static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
327{
328 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kenta5370552006-05-15 09:43:51 -0700329 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent34ca9592006-03-27 01:14:54 -0800330 int oz_mode = autofs4_oz_mode(sbi);
331 unsigned int lookup_type;
332 int status;
333
334 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
335 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
336 nd->flags);
337
338 /* If it's our master or we shouldn't trigger a mount we're done */
339 lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
340 if (oz_mode || !lookup_type)
341 goto done;
342
Ian Kenta5370552006-05-15 09:43:51 -0700343 /* If an expire request is pending wait for it. */
344 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Ian Kent871f9432006-03-27 01:14:58 -0800345 DPRINTK("waiting for active request %p name=%.*s",
346 dentry, dentry->d_name.len, dentry->d_name.name);
Ian Kent34ca9592006-03-27 01:14:54 -0800347
Ian Kent871f9432006-03-27 01:14:58 -0800348 status = autofs4_wait(sbi, dentry, NFY_NONE);
349
350 DPRINTK("request done status=%d", status);
Ian Kent34ca9592006-03-27 01:14:54 -0800351 }
352
Ian Kent871f9432006-03-27 01:14:58 -0800353 /*
354 * If the dentry contains directories then it is an
355 * autofs multi-mount with no root mount offset. So
356 * don't try to mount it again.
357 */
358 spin_lock(&dcache_lock);
Ian Kentbe3ca7f2006-09-29 02:00:53 -0700359 if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
Ian Kent871f9432006-03-27 01:14:58 -0800360 spin_unlock(&dcache_lock);
361
362 status = try_to_fill_dentry(dentry, 0);
363 if (status)
364 goto out_error;
365
366 /*
367 * The mount succeeded but if there is no root mount
368 * it must be an autofs multi-mount with no root offset
369 * so we don't need to follow the mount.
370 */
371 if (d_mountpoint(dentry)) {
Jan Blunck4ac91372008-02-14 19:34:32 -0800372 if (!autofs4_follow_mount(&nd->path.mnt,
373 &nd->path.dentry)) {
Ian Kent871f9432006-03-27 01:14:58 -0800374 status = -ENOENT;
375 goto out_error;
376 }
377 }
378
379 goto done;
380 }
381 spin_unlock(&dcache_lock);
382
Ian Kent34ca9592006-03-27 01:14:54 -0800383done:
384 return NULL;
385
386out_error:
Jan Blunck1d957f92008-02-14 19:34:35 -0800387 path_put(&nd->path);
Ian Kent34ca9592006-03-27 01:14:54 -0800388 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391/*
392 * Revalidate is called on every cache lookup. Some of those
393 * cache lookups may actually happen while the dentry is not
394 * yet completely filled in, and revalidate has to delay such
395 * lookups..
396 */
Ian Kent718c6042006-03-27 01:14:42 -0800397static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Ian Kent718c6042006-03-27 01:14:42 -0800399 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
401 int oz_mode = autofs4_oz_mode(sbi);
402 int flags = nd ? nd->flags : 0;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700403 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /* Pending dentry */
406 if (autofs4_ispending(dentry)) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700407 /* The daemon never causes a mount to trigger */
408 if (oz_mode)
409 return 1;
410
411 /*
412 * A zero status is success otherwise we have a
413 * negative error code.
414 */
415 status = try_to_fill_dentry(dentry, flags);
416 if (status == 0)
Ian Kentf50b6f82007-02-20 13:58:10 -0800417 return 1;
418
419 /*
420 * A status of EAGAIN here means that the dentry has gone
421 * away while waiting for an expire to complete. If we are
422 * racing with expire lookup will wait for it so this must
423 * be a revalidate and we need to send it to lookup.
424 */
425 if (status == -EAGAIN)
426 return 0;
Ian Kentbcdc5e02006-09-27 01:50:44 -0700427
428 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
431 /* Negative dentry.. invalidate if "old" */
432 if (dentry->d_inode == NULL)
Ian Kent2d753e62006-03-27 01:14:44 -0800433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* Check for a non-mountpoint directory with no contents */
436 spin_lock(&dcache_lock);
437 if (S_ISDIR(dentry->d_inode->i_mode) &&
438 !d_mountpoint(dentry) &&
Ian Kent90a59c72006-03-27 01:14:50 -0800439 __simple_empty(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 DPRINTK("dentry=%p %.*s, emptydir",
441 dentry, dentry->d_name.len, dentry->d_name.name);
442 spin_unlock(&dcache_lock);
Ian Kentbcdc5e02006-09-27 01:50:44 -0700443 /* The daemon never causes a mount to trigger */
444 if (oz_mode)
445 return 1;
446
447 /*
448 * A zero status is success otherwise we have a
449 * negative error code.
450 */
451 status = try_to_fill_dentry(dentry, flags);
452 if (status == 0)
453 return 1;
454
455 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 spin_unlock(&dcache_lock);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 1;
460}
461
Ian Kent34ca9592006-03-27 01:14:54 -0800462void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct autofs_info *inf;
465
466 DPRINTK("releasing %p", de);
467
468 inf = autofs4_dentry_ino(de);
469 de->d_fsdata = NULL;
470
471 if (inf) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800472 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
473
Ian Kentf50b6f82007-02-20 13:58:10 -0800474 if (sbi) {
Ian Kent5f6f4f22008-07-23 21:30:09 -0700475 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700476 if (!list_empty(&inf->active))
477 list_del(&inf->active);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700478 if (!list_empty(&inf->expiring))
479 list_del(&inf->expiring);
480 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800481 }
482
Jeff Mahoneyc3724b12007-04-11 23:28:46 -0700483 inf->dentry = NULL;
484 inf->inode = NULL;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 autofs4_free_ino(inf);
487 }
488}
489
490/* For dentries of directories in the root dir */
491static struct dentry_operations autofs4_root_dentry_operations = {
492 .d_revalidate = autofs4_revalidate,
493 .d_release = autofs4_dentry_release,
494};
495
496/* For other dentries */
497static struct dentry_operations autofs4_dentry_operations = {
498 .d_revalidate = autofs4_revalidate,
499 .d_release = autofs4_dentry_release,
500};
501
Ian Kent25767372008-07-23 21:30:12 -0700502static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
503{
504 unsigned int len = name->len;
505 unsigned int hash = name->hash;
506 const unsigned char *str = name->name;
507 struct list_head *p, *head;
508
509 spin_lock(&dcache_lock);
510 spin_lock(&sbi->lookup_lock);
511 head = &sbi->active_list;
512 list_for_each(p, head) {
513 struct autofs_info *ino;
514 struct dentry *dentry;
515 struct qstr *qstr;
516
517 ino = list_entry(p, struct autofs_info, active);
518 dentry = ino->dentry;
519
520 spin_lock(&dentry->d_lock);
521
522 /* Already gone? */
523 if (atomic_read(&dentry->d_count) == 0)
524 goto next;
525
526 qstr = &dentry->d_name;
527
528 if (dentry->d_name.hash != hash)
529 goto next;
530 if (dentry->d_parent != parent)
531 goto next;
532
533 if (qstr->len != len)
534 goto next;
535 if (memcmp(qstr->name, str, len))
536 goto next;
537
538 if (d_unhashed(dentry)) {
539 dget(dentry);
540 spin_unlock(&dentry->d_lock);
541 spin_unlock(&sbi->lookup_lock);
542 spin_unlock(&dcache_lock);
543 return dentry;
544 }
545next:
546 spin_unlock(&dentry->d_lock);
547 }
548 spin_unlock(&sbi->lookup_lock);
549 spin_unlock(&dcache_lock);
550
551 return NULL;
552}
553
Ian Kent5f6f4f22008-07-23 21:30:09 -0700554static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
Ian Kentf50b6f82007-02-20 13:58:10 -0800555{
556 unsigned int len = name->len;
557 unsigned int hash = name->hash;
558 const unsigned char *str = name->name;
559 struct list_head *p, *head;
560
561 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700562 spin_lock(&sbi->lookup_lock);
563 head = &sbi->expiring_list;
Ian Kentf50b6f82007-02-20 13:58:10 -0800564 list_for_each(p, head) {
565 struct autofs_info *ino;
566 struct dentry *dentry;
567 struct qstr *qstr;
568
Ian Kent5f6f4f22008-07-23 21:30:09 -0700569 ino = list_entry(p, struct autofs_info, expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800570 dentry = ino->dentry;
571
572 spin_lock(&dentry->d_lock);
573
574 /* Bad luck, we've already been dentry_iput */
575 if (!dentry->d_inode)
576 goto next;
577
578 qstr = &dentry->d_name;
579
580 if (dentry->d_name.hash != hash)
581 goto next;
582 if (dentry->d_parent != parent)
583 goto next;
584
585 if (qstr->len != len)
586 goto next;
587 if (memcmp(qstr->name, str, len))
588 goto next;
589
590 if (d_unhashed(dentry)) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800591 dget(dentry);
Ian Kentf50b6f82007-02-20 13:58:10 -0800592 spin_unlock(&dentry->d_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700593 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800594 spin_unlock(&dcache_lock);
595 return dentry;
596 }
597next:
598 spin_unlock(&dentry->d_lock);
599 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700600 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800601 spin_unlock(&dcache_lock);
602
603 return NULL;
604}
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606/* Lookups in the root directory */
607static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
608{
609 struct autofs_sb_info *sbi;
Ian Kent25767372008-07-23 21:30:12 -0700610 struct autofs_info *ino;
611 struct dentry *expiring, *unhashed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 int oz_mode;
613
614 DPRINTK("name = %.*s",
615 dentry->d_name.len, dentry->d_name.name);
616
Ian Kent718c6042006-03-27 01:14:42 -0800617 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800619 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
Pavel Emelianova47afb02007-10-18 23:39:46 -0700625 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Ian Kent5f6f4f22008-07-23 21:30:09 -0700627 expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
628 if (expiring) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800629 /*
630 * If we are racing with expire the request might not
631 * be quite complete but the directory has been removed
632 * so it must have been successful, so just wait for it.
633 */
Ian Kent25767372008-07-23 21:30:12 -0700634 ino = autofs4_dentry_ino(expiring);
Ian Kent1864f7b2007-08-22 14:01:54 -0700635 while (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Ian Kentf50b6f82007-02-20 13:58:10 -0800636 DPRINTK("wait for incomplete expire %p name=%.*s",
Ian Kent5f6f4f22008-07-23 21:30:09 -0700637 expiring, expiring->d_name.len,
638 expiring->d_name.name);
639 autofs4_wait(sbi, expiring, NFY_NONE);
Ian Kentf50b6f82007-02-20 13:58:10 -0800640 DPRINTK("request completed");
641 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700642 spin_lock(&sbi->lookup_lock);
643 if (!list_empty(&ino->expiring))
644 list_del_init(&ino->expiring);
645 spin_unlock(&sbi->lookup_lock);
646 dput(expiring);
Ian Kentf50b6f82007-02-20 13:58:10 -0800647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Ian Kent25767372008-07-23 21:30:12 -0700649 unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
650 if (unhashed)
651 dentry = unhashed;
652 else {
653 /*
654 * Mark the dentry incomplete but don't hash it. We do this
655 * to serialize our inode creation operations (symlink and
656 * mkdir) which prevents deadlock during the callback to
657 * the daemon. Subsequent user space lookups for the same
658 * dentry are placed on the wait queue while the daemon
659 * itself is allowed passage unresticted so the create
660 * operation itself can then hash the dentry. Finally,
661 * we check for the hashed dentry and return the newly
662 * hashed dentry.
663 */
664 dentry->d_op = &autofs4_root_dentry_operations;
Ian Kent5f6f4f22008-07-23 21:30:09 -0700665
Ian Kent25767372008-07-23 21:30:12 -0700666 /*
667 * And we need to ensure that the same dentry is used for
668 * all following lookup calls until it is hashed so that
669 * the dentry flags are persistent throughout the request.
670 */
671 ino = autofs4_init_ino(NULL, sbi, 0555);
672 if (!ino)
673 return ERR_PTR(-ENOMEM);
674
675 dentry->d_fsdata = ino;
676 ino->dentry = dentry;
677
678 spin_lock(&sbi->lookup_lock);
679 list_add(&ino->active, &sbi->active_list);
680 spin_unlock(&sbi->lookup_lock);
681
682 d_instantiate(dentry, NULL);
683 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (!oz_mode) {
686 spin_lock(&dentry->d_lock);
687 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
688 spin_unlock(&dentry->d_lock);
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (dentry->d_op && dentry->d_op->d_revalidate) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800692 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 (dentry->d_op->d_revalidate)(dentry, nd);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800694 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 /*
698 * If we are still pending, check if we had to handle
699 * a signal. If so we can force a restart..
700 */
701 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
702 /* See if we were interrupted */
703 if (signal_pending(current)) {
704 sigset_t *sigset = &current->pending.signal;
705 if (sigismember (sigset, SIGKILL) ||
706 sigismember (sigset, SIGQUIT) ||
707 sigismember (sigset, SIGINT)) {
Ian Kent25767372008-07-23 21:30:12 -0700708 if (unhashed)
709 dput(unhashed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return ERR_PTR(-ERESTARTNOINTR);
711 }
712 }
Ian Kent25767372008-07-23 21:30:12 -0700713 if (!oz_mode) {
714 spin_lock(&dentry->d_lock);
715 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
716 spin_unlock(&dentry->d_lock);
717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
720 /*
721 * If this dentry is unhashed, then we shouldn't honour this
Ian Kentc9ffec42007-02-20 13:58:10 -0800722 * lookup. Returning ENOENT here doesn't do the right thing
723 * for all system calls, but it should be OK for the operations
724 * we permit from an autofs.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
Ian Kent1864f7b2007-08-22 14:01:54 -0700726 if (!oz_mode && d_unhashed(dentry)) {
Ian Kentc9ffec42007-02-20 13:58:10 -0800727 /*
728 * A user space application can (and has done in the past)
729 * remove and re-create this directory during the callback.
730 * This can leave us with an unhashed dentry, but a
731 * successful mount! So we need to perform another
732 * cached lookup in case the dentry now exists.
733 */
734 struct dentry *parent = dentry->d_parent;
735 struct dentry *new = d_lookup(parent, &dentry->d_name);
736 if (new != NULL)
737 dentry = new;
738 else
739 dentry = ERR_PTR(-ENOENT);
740
Ian Kent25767372008-07-23 21:30:12 -0700741 if (unhashed)
742 dput(unhashed);
743
Ian Kentc9ffec42007-02-20 13:58:10 -0800744 return dentry;
Ian Kentf50b6f82007-02-20 13:58:10 -0800745 }
746
Ian Kent25767372008-07-23 21:30:12 -0700747 if (unhashed)
748 return unhashed;
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return NULL;
751}
752
753static int autofs4_dir_symlink(struct inode *dir,
754 struct dentry *dentry,
755 const char *symname)
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 struct inode *inode;
761 char *cp;
762
763 DPRINTK("%s <- %.*s", symname,
764 dentry->d_name.len, dentry->d_name.name);
765
766 if (!autofs4_oz_mode(sbi))
767 return -EACCES;
768
769 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700770 if (!ino)
771 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Ian Kent25767372008-07-23 21:30:12 -0700773 spin_lock(&sbi->lookup_lock);
774 if (!list_empty(&ino->active))
775 list_del_init(&ino->active);
776 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Ian Kent25767372008-07-23 21:30:12 -0700778 cp = kmalloc(ino->size + 1, GFP_KERNEL);
779 if (!cp) {
780 if (!dentry->d_fsdata)
781 kfree(ino);
782 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784
785 strcpy(cp, symname);
786
787 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700788 if (!inode) {
789 kfree(cp);
790 if (!dentry->d_fsdata)
791 kfree(ino);
792 return -ENOMEM;
793 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700794 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 if (dir == dir->i_sb->s_root->d_inode)
797 dentry->d_op = &autofs4_root_dentry_operations;
798 else
799 dentry->d_op = &autofs4_dentry_operations;
800
801 dentry->d_fsdata = ino;
802 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800803 atomic_inc(&ino->count);
804 p_ino = autofs4_dentry_ino(dentry->d_parent);
805 if (p_ino && dentry->d_parent != dentry)
806 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ino->inode = inode;
808
Ian Kent25767372008-07-23 21:30:12 -0700809 ino->size = strlen(symname);
810 ino->u.symlink = cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 dir->i_mtime = CURRENT_TIME;
812
813 return 0;
814}
815
816/*
817 * NOTE!
818 *
819 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
820 * that the file no longer exists. However, doing that means that the
821 * VFS layer can turn the dentry into a negative dentry. We don't want
Ian Kentf50b6f82007-02-20 13:58:10 -0800822 * this, because the unlink is probably the result of an expire.
Ian Kent5f6f4f22008-07-23 21:30:09 -0700823 * We simply d_drop it and add it to a expiring list in the super block,
824 * which allows the dentry lookup to check for an incomplete expire.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 *
826 * If a process is blocked on the dentry waiting for the expire to finish,
827 * it will invalidate the dentry and try to mount with a new one.
828 *
829 * Also see autofs4_dir_rmdir()..
830 */
831static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
832{
833 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
834 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800835 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 /* This allows root to remove symlinks */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700838 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -EACCES;
840
Ian Kent1aff3c82006-03-27 01:14:46 -0800841 if (atomic_dec_and_test(&ino->count)) {
842 p_ino = autofs4_dentry_ino(dentry->d_parent);
843 if (p_ino && dentry->d_parent != dentry)
844 atomic_dec(&p_ino->count);
845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 dput(ino->dentry);
847
848 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700849 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 dir->i_mtime = CURRENT_TIME;
852
Ian Kentf50b6f82007-02-20 13:58:10 -0800853 spin_lock(&dcache_lock);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700854 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700855 if (list_empty(&ino->expiring))
856 list_add(&ino->expiring, &sbi->expiring_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700857 spin_unlock(&sbi->lookup_lock);
Ian Kentf50b6f82007-02-20 13:58:10 -0800858 spin_lock(&dentry->d_lock);
859 __d_drop(dentry);
860 spin_unlock(&dentry->d_lock);
861 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 return 0;
864}
865
866static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
867{
868 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
869 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800870 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Ian Kentf50b6f82007-02-20 13:58:10 -0800872 DPRINTK("dentry %p, removing %.*s",
873 dentry, dentry->d_name.len, dentry->d_name.name);
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!autofs4_oz_mode(sbi))
876 return -EACCES;
877
878 spin_lock(&dcache_lock);
879 if (!list_empty(&dentry->d_subdirs)) {
880 spin_unlock(&dcache_lock);
881 return -ENOTEMPTY;
882 }
Ian Kent5f6f4f22008-07-23 21:30:09 -0700883 spin_lock(&sbi->lookup_lock);
Ian Kent25767372008-07-23 21:30:12 -0700884 if (list_empty(&ino->expiring))
885 list_add(&ino->expiring, &sbi->expiring_list);
Ian Kent5f6f4f22008-07-23 21:30:09 -0700886 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 spin_lock(&dentry->d_lock);
888 __d_drop(dentry);
889 spin_unlock(&dentry->d_lock);
890 spin_unlock(&dcache_lock);
891
Ian Kent1aff3c82006-03-27 01:14:46 -0800892 if (atomic_dec_and_test(&ino->count)) {
893 p_ino = autofs4_dentry_ino(dentry->d_parent);
894 if (p_ino && dentry->d_parent != dentry)
895 atomic_dec(&p_ino->count);
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 dentry->d_inode->i_size = 0;
Dave Hansence71ec32006-09-30 23:29:06 -0700899 clear_nlink(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 if (dir->i_nlink)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700902 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 return 0;
905}
906
907static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
908{
909 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
910 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800911 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 struct inode *inode;
913
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700914 if (!autofs4_oz_mode(sbi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return -EACCES;
916
917 DPRINTK("dentry %p, creating %.*s",
918 dentry, dentry->d_name.len, dentry->d_name.name);
919
920 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
Ian Kent25767372008-07-23 21:30:12 -0700921 if (!ino)
922 return -ENOMEM;
923
924 spin_lock(&sbi->lookup_lock);
925 if (!list_empty(&ino->active))
926 list_del_init(&ino->active);
927 spin_unlock(&sbi->lookup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 inode = autofs4_get_inode(dir->i_sb, ino);
Ian Kent25767372008-07-23 21:30:12 -0700930 if (!inode) {
931 if (!dentry->d_fsdata)
932 kfree(ino);
933 return -ENOMEM;
934 }
Ian Kent1864f7b2007-08-22 14:01:54 -0700935 d_add(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (dir == dir->i_sb->s_root->d_inode)
938 dentry->d_op = &autofs4_root_dentry_operations;
939 else
940 dentry->d_op = &autofs4_dentry_operations;
941
942 dentry->d_fsdata = ino;
943 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800944 atomic_inc(&ino->count);
945 p_ino = autofs4_dentry_ino(dentry->d_parent);
946 if (p_ino && dentry->d_parent != dentry)
947 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 ino->inode = inode;
Dave Hansend8c76e62006-09-30 23:29:04 -0700949 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 dir->i_mtime = CURRENT_TIME;
951
952 return 0;
953}
954
955/* Get/set timeout ioctl() operation */
956static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
957 unsigned long __user *p)
958{
959 int rv;
960 unsigned long ntimeout;
961
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700962 if ((rv = get_user(ntimeout, p)) ||
963 (rv = put_user(sbi->exp_timeout/HZ, p)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return rv;
965
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700966 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 sbi->exp_timeout = 0;
968 else
969 sbi->exp_timeout = ntimeout * HZ;
970
971 return 0;
972}
973
974/* Return protocol version */
975static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
976{
977 return put_user(sbi->version, p);
978}
979
980/* Return protocol sub version */
981static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
982{
983 return put_user(sbi->sub_version, p);
984}
985
986/*
987 * Tells the daemon whether we need to reghost or not. Also, clears
988 * the reghost_needed flag.
989 */
990static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
991{
992 int status;
993
994 DPRINTK("returning %d", sbi->needs_reghost);
995
996 status = put_user(sbi->needs_reghost, p);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700997 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return status;
999
1000 sbi->needs_reghost = 0;
1001 return 0;
1002}
1003
1004/*
1005 * Enable / Disable reghosting ioctl() operation
1006 */
1007static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
1008{
1009 int status;
1010 int val;
1011
1012 status = get_user(val, p);
1013
1014 DPRINTK("reghost = %d", val);
1015
1016 if (status)
1017 return status;
1018
1019 /* turn on/off reghosting, with the val */
1020 sbi->reghost_enabled = val;
1021 return 0;
1022}
1023
1024/*
1025* Tells the daemon whether it can umount the autofs mount.
1026*/
1027static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
1028{
1029 int status = 0;
1030
Ian Kente3474a82006-03-27 01:14:51 -08001031 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 status = 1;
1033
1034 DPRINTK("returning %d", status);
1035
1036 status = put_user(status, p);
1037
1038 return status;
1039}
1040
1041/* Identify autofs4_dentries - this is so we can tell if there's
1042 an extra dentry refcount or not. We only hold a refcount on the
1043 dentry if its non-negative (ie, d_inode != NULL)
1044*/
1045int is_autofs4_dentry(struct dentry *dentry)
1046{
1047 return dentry && dentry->d_inode &&
1048 (dentry->d_op == &autofs4_root_dentry_operations ||
1049 dentry->d_op == &autofs4_dentry_operations) &&
1050 dentry->d_fsdata != NULL;
1051}
1052
1053/*
1054 * ioctl()'s on the root directory is the chief method for the daemon to
1055 * generate kernel reactions
1056 */
1057static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
1058 unsigned int cmd, unsigned long arg)
1059{
1060 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
1061 void __user *p = (void __user *)arg;
1062
1063 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
Pavel Emelianova47afb02007-10-18 23:39:46 -07001064 cmd,arg,sbi,task_pgrp_nr(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -07001066 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
1067 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return -ENOTTY;
1069
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -07001070 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return -EPERM;
1072
1073 switch(cmd) {
1074 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
1075 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
1076 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
1077 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
1078 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
1079 autofs4_catatonic_mode(sbi);
1080 return 0;
1081 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
1082 return autofs4_get_protover(sbi, p);
1083 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
1084 return autofs4_get_protosubver(sbi, p);
1085 case AUTOFS_IOC_SETTIMEOUT:
1086 return autofs4_get_set_timeout(sbi, p);
1087
1088 case AUTOFS_IOC_TOGGLEREGHOST:
1089 return autofs4_toggle_reghost(sbi, p);
1090 case AUTOFS_IOC_ASKREGHOST:
1091 return autofs4_ask_reghost(sbi, p);
1092
1093 case AUTOFS_IOC_ASKUMOUNT:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001094 return autofs4_ask_umount(filp->f_path.mnt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 /* return a single thing to expire */
1097 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001098 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /* same as above, but can send multiple expires through pipe */
1100 case AUTOFS_IOC_EXPIRE_MULTI:
Josef "Jeff" Sipeka4669ed2006-12-08 02:36:46 -08001101 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 default:
1104 return -ENOSYS;
1105 }
1106}