blob: 84e030c8ddd0f99047473308afc4d26f555e2c14 [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>
20#include <linux/smp_lock.h>
21#include "autofs_i.h"
22
23static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
24static int autofs4_dir_unlink(struct inode *,struct dentry *);
25static int autofs4_dir_rmdir(struct inode *,struct dentry *);
26static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
27static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
28static int autofs4_dir_open(struct inode *inode, struct file *file);
29static int autofs4_dir_close(struct inode *inode, struct file *file);
30static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
31static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
32static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
Ian Kent34ca9592006-03-27 01:14:54 -080033static void *autofs4_follow_link(struct dentry *, struct nameidata *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080035const struct file_operations autofs4_root_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .open = dcache_dir_open,
37 .release = dcache_dir_close,
38 .read = generic_read_dir,
39 .readdir = autofs4_root_readdir,
40 .ioctl = autofs4_root_ioctl,
41};
42
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080043const struct file_operations autofs4_dir_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 .open = autofs4_dir_open,
45 .release = autofs4_dir_close,
46 .read = generic_read_dir,
47 .readdir = autofs4_dir_readdir,
48};
49
Ian Kent34ca9592006-03-27 01:14:54 -080050struct inode_operations autofs4_indirect_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 .lookup = autofs4_lookup,
52 .unlink = autofs4_dir_unlink,
53 .symlink = autofs4_dir_symlink,
54 .mkdir = autofs4_dir_mkdir,
55 .rmdir = autofs4_dir_rmdir,
56};
57
Ian Kent34ca9592006-03-27 01:14:54 -080058struct inode_operations autofs4_direct_root_inode_operations = {
59 .lookup = autofs4_lookup,
Ian Kent871f9432006-03-27 01:14:58 -080060 .unlink = autofs4_dir_unlink,
61 .mkdir = autofs4_dir_mkdir,
62 .rmdir = autofs4_dir_rmdir,
Ian Kent34ca9592006-03-27 01:14:54 -080063 .follow_link = autofs4_follow_link,
64};
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066struct inode_operations autofs4_dir_inode_operations = {
67 .lookup = autofs4_lookup,
68 .unlink = autofs4_dir_unlink,
69 .symlink = autofs4_dir_symlink,
70 .mkdir = autofs4_dir_mkdir,
71 .rmdir = autofs4_dir_rmdir,
72};
73
74static int autofs4_root_readdir(struct file *file, void *dirent,
75 filldir_t filldir)
76{
77 struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb);
78 int oz_mode = autofs4_oz_mode(sbi);
79
80 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
81
82 /*
83 * Don't set reghost flag if:
84 * 1) f_pos is larger than zero -- we've already been here.
85 * 2) we haven't even enabled reghosting in the 1st place.
86 * 3) this is the daemon doing a readdir
87 */
88 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
89 sbi->needs_reghost = 1;
90
91 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
92
Ian Kentf360ce32006-03-27 01:14:43 -080093 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int autofs4_dir_open(struct inode *inode, struct file *file)
97{
98 struct dentry *dentry = file->f_dentry;
99 struct vfsmount *mnt = file->f_vfsmnt;
100 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800101 struct dentry *cursor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 int status;
103
Ian Kentf360ce32006-03-27 01:14:43 -0800104 status = dcache_dir_open(inode, file);
105 if (status)
106 goto out;
107
108 cursor = file->private_data;
109 cursor->d_fsdata = NULL;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 DPRINTK("file=%p dentry=%p %.*s",
112 file, dentry, dentry->d_name.len, dentry->d_name.name);
113
114 if (autofs4_oz_mode(sbi))
115 goto out;
116
117 if (autofs4_ispending(dentry)) {
118 DPRINTK("dentry busy");
Ian Kentf360ce32006-03-27 01:14:43 -0800119 dcache_dir_close(inode, file);
120 status = -EBUSY;
121 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Ian Kentf360ce32006-03-27 01:14:43 -0800124 status = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
126 struct nameidata nd;
Ian Kentf360ce32006-03-27 01:14:43 -0800127 int empty, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /* In case there are stale directory dentrys from a failed mount */
130 spin_lock(&dcache_lock);
131 empty = list_empty(&dentry->d_subdirs);
132 spin_unlock(&dcache_lock);
133
134 if (!empty)
135 d_invalidate(dentry);
136
137 nd.flags = LOOKUP_DIRECTORY;
Ian Kentf360ce32006-03-27 01:14:43 -0800138 ret = (dentry->d_op->d_revalidate)(dentry, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Ian Kentf360ce32006-03-27 01:14:43 -0800140 if (!ret) {
141 dcache_dir_close(inode, file);
142 goto out;
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
146 if (d_mountpoint(dentry)) {
147 struct file *fp = NULL;
148 struct vfsmount *fp_mnt = mntget(mnt);
149 struct dentry *fp_dentry = dget(dentry);
150
Ian Kent9b1e3af2005-06-21 17:16:38 -0700151 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
152 dput(fp_dentry);
153 mntput(fp_mnt);
Ian Kentf360ce32006-03-27 01:14:43 -0800154 dcache_dir_close(inode, file);
155 goto out;
Ian Kent9b1e3af2005-06-21 17:16:38 -0700156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
159 status = PTR_ERR(fp);
160 if (IS_ERR(fp)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800161 dcache_dir_close(inode, file);
162 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
Ian Kentf360ce32006-03-27 01:14:43 -0800164 cursor->d_fsdata = fp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return 0;
Ian Kentf360ce32006-03-27 01:14:43 -0800167out:
168 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171static int autofs4_dir_close(struct inode *inode, struct file *file)
172{
173 struct dentry *dentry = file->f_dentry;
174 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800175 struct dentry *cursor = file->private_data;
176 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 DPRINTK("file=%p dentry=%p %.*s",
179 file, dentry, dentry->d_name.len, dentry->d_name.name);
180
181 if (autofs4_oz_mode(sbi))
182 goto out;
183
184 if (autofs4_ispending(dentry)) {
185 DPRINTK("dentry busy");
Ian Kentf360ce32006-03-27 01:14:43 -0800186 status = -EBUSY;
187 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
190 if (d_mountpoint(dentry)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800191 struct file *fp = cursor->d_fsdata;
192 if (!fp) {
193 status = -ENOENT;
194 goto out;
195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 filp_close(fp, current->files);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
198out:
Ian Kentf360ce32006-03-27 01:14:43 -0800199 dcache_dir_close(inode, file);
200 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
204{
205 struct dentry *dentry = file->f_dentry;
206 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kentf360ce32006-03-27 01:14:43 -0800207 struct dentry *cursor = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int status;
209
210 DPRINTK("file=%p dentry=%p %.*s",
211 file, dentry, dentry->d_name.len, dentry->d_name.name);
212
213 if (autofs4_oz_mode(sbi))
214 goto out;
215
216 if (autofs4_ispending(dentry)) {
217 DPRINTK("dentry busy");
218 return -EBUSY;
219 }
220
221 if (d_mountpoint(dentry)) {
Ian Kentf360ce32006-03-27 01:14:43 -0800222 struct file *fp = cursor->d_fsdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 if (!fp)
225 return -ENOENT;
226
227 if (!fp->f_op || !fp->f_op->readdir)
228 goto out;
229
230 status = vfs_readdir(fp, filldir, dirent);
231 file->f_pos = fp->f_pos;
232 if (status)
233 autofs4_copy_atime(file, fp);
234 return status;
235 }
236out:
Ian Kentf360ce32006-03-27 01:14:43 -0800237 return dcache_readdir(file, dirent, filldir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Ian Kent862b1102006-03-27 01:14:48 -0800240static int try_to_fill_dentry(struct dentry *dentry, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Ian Kent862b1102006-03-27 01:14:48 -0800242 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
Ian Kent718c6042006-03-27 01:14:42 -0800243 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 int status = 0;
245
246 /* Block on any pending expiry here; invalidate the dentry
247 when expiration is done to trigger mount request with a new
248 dentry */
Ian Kent718c6042006-03-27 01:14:42 -0800249 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 DPRINTK("waiting for expire %p name=%.*s",
251 dentry, dentry->d_name.len, dentry->d_name.name);
252
253 status = autofs4_wait(sbi, dentry, NFY_NONE);
Ian Kent718c6042006-03-27 01:14:42 -0800254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 DPRINTK("expire done status=%d", status);
Ian Kent718c6042006-03-27 01:14:42 -0800256
Ian Kent1684b2b2005-06-21 17:16:41 -0700257 /*
258 * If the directory still exists the mount request must
259 * continue otherwise it can't be followed at the right
260 * time during the walk.
261 */
262 status = d_invalidate(dentry);
263 if (status != -EBUSY)
Ian Kent34ca9592006-03-27 01:14:54 -0800264 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
267 DPRINTK("dentry=%p %.*s ino=%p",
268 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
269
Ian Kent718c6042006-03-27 01:14:42 -0800270 /*
271 * Wait for a pending mount, triggering one if there
272 * isn't one already
273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 if (dentry->d_inode == NULL) {
275 DPRINTK("waiting for mount name=%.*s",
276 dentry->d_name.len, dentry->d_name.name);
277
278 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
Ian Kent718c6042006-03-27 01:14:42 -0800279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 DPRINTK("mount done status=%d", status);
281
282 if (status && dentry->d_inode)
Ian Kent34ca9592006-03-27 01:14:54 -0800283 return status; /* Try to get the kernel to invalidate this dentry */
Ian Kent718c6042006-03-27 01:14:42 -0800284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* Turn this into a real negative dentry? */
286 if (status == -ENOENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 spin_lock(&dentry->d_lock);
288 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
289 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800290 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 } else if (status) {
292 /* Return a negative dentry, but leave it "pending" */
Ian Kent34ca9592006-03-27 01:14:54 -0800293 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295 /* Trigger mount for path component or follow link */
296 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
297 current->link_count) {
298 DPRINTK("waiting for mount name=%.*s",
299 dentry->d_name.len, dentry->d_name.name);
300
301 spin_lock(&dentry->d_lock);
302 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
303 spin_unlock(&dentry->d_lock);
304 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
305
306 DPRINTK("mount done status=%d", status);
307
308 if (status) {
309 spin_lock(&dentry->d_lock);
310 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
311 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800312 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314 }
315
Ian Kente0a7aae2006-03-27 01:14:47 -0800316 /* Initialize expiry counter after successful mount */
317 if (ino)
318 ino->last_used = jiffies;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 spin_lock(&dentry->d_lock);
321 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
322 spin_unlock(&dentry->d_lock);
Ian Kent34ca9592006-03-27 01:14:54 -0800323 return status;
324}
325
326/* For autofs direct mounts the follow link triggers the mount */
327static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
328{
329 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
330 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 Kent871f9432006-03-27 01:14:58 -0800343 /*
344 * If a request is pending wait for it.
345 * If it's a mount then it won't be expired till at least
346 * a liitle later and if it's an expire then we might need
347 * to mount it again.
348 */
349 if (autofs4_ispending(dentry)) {
350 DPRINTK("waiting for active request %p name=%.*s",
351 dentry, dentry->d_name.len, dentry->d_name.name);
Ian Kent34ca9592006-03-27 01:14:54 -0800352
Ian Kent871f9432006-03-27 01:14:58 -0800353 status = autofs4_wait(sbi, dentry, NFY_NONE);
354
355 DPRINTK("request done status=%d", status);
Ian Kent34ca9592006-03-27 01:14:54 -0800356 }
357
Ian Kent871f9432006-03-27 01:14:58 -0800358 /*
359 * If the dentry contains directories then it is an
360 * autofs multi-mount with no root mount offset. So
361 * don't try to mount it again.
362 */
363 spin_lock(&dcache_lock);
364 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
365 spin_unlock(&dcache_lock);
366
367 status = try_to_fill_dentry(dentry, 0);
368 if (status)
369 goto out_error;
370
371 /*
372 * The mount succeeded but if there is no root mount
373 * it must be an autofs multi-mount with no root offset
374 * so we don't need to follow the mount.
375 */
376 if (d_mountpoint(dentry)) {
377 if (!autofs4_follow_mount(&nd->mnt, &nd->dentry)) {
378 status = -ENOENT;
379 goto out_error;
380 }
381 }
382
383 goto done;
384 }
385 spin_unlock(&dcache_lock);
386
Ian Kent34ca9592006-03-27 01:14:54 -0800387done:
388 return NULL;
389
390out_error:
391 path_release(nd);
392 return ERR_PTR(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/*
396 * Revalidate is called on every cache lookup. Some of those
397 * cache lookups may actually happen while the dentry is not
398 * yet completely filled in, and revalidate has to delay such
399 * lookups..
400 */
Ian Kent718c6042006-03-27 01:14:42 -0800401static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Ian Kent718c6042006-03-27 01:14:42 -0800403 struct inode *dir = dentry->d_parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
405 int oz_mode = autofs4_oz_mode(sbi);
406 int flags = nd ? nd->flags : 0;
Ian Kent34ca9592006-03-27 01:14:54 -0800407 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* Pending dentry */
410 if (autofs4_ispending(dentry)) {
411 if (!oz_mode)
Ian Kent862b1102006-03-27 01:14:48 -0800412 status = try_to_fill_dentry(dentry, flags);
Ian Kent34ca9592006-03-27 01:14:54 -0800413 return !status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
416 /* Negative dentry.. invalidate if "old" */
417 if (dentry->d_inode == NULL)
Ian Kent2d753e62006-03-27 01:14:44 -0800418 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 /* Check for a non-mountpoint directory with no contents */
421 spin_lock(&dcache_lock);
422 if (S_ISDIR(dentry->d_inode->i_mode) &&
423 !d_mountpoint(dentry) &&
Ian Kent90a59c72006-03-27 01:14:50 -0800424 __simple_empty(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 DPRINTK("dentry=%p %.*s, emptydir",
426 dentry, dentry->d_name.len, dentry->d_name.name);
427 spin_unlock(&dcache_lock);
428 if (!oz_mode)
Ian Kent862b1102006-03-27 01:14:48 -0800429 status = try_to_fill_dentry(dentry, flags);
Ian Kent34ca9592006-03-27 01:14:54 -0800430 return !status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432 spin_unlock(&dcache_lock);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 1;
435}
436
Ian Kent34ca9592006-03-27 01:14:54 -0800437void autofs4_dentry_release(struct dentry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 struct autofs_info *inf;
440
441 DPRINTK("releasing %p", de);
442
443 inf = autofs4_dentry_ino(de);
444 de->d_fsdata = NULL;
445
446 if (inf) {
447 inf->dentry = NULL;
448 inf->inode = NULL;
449
450 autofs4_free_ino(inf);
451 }
452}
453
454/* For dentries of directories in the root dir */
455static struct dentry_operations autofs4_root_dentry_operations = {
456 .d_revalidate = autofs4_revalidate,
457 .d_release = autofs4_dentry_release,
458};
459
460/* For other dentries */
461static struct dentry_operations autofs4_dentry_operations = {
462 .d_revalidate = autofs4_revalidate,
463 .d_release = autofs4_dentry_release,
464};
465
466/* Lookups in the root directory */
467static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
468{
469 struct autofs_sb_info *sbi;
470 int oz_mode;
471
472 DPRINTK("name = %.*s",
473 dentry->d_name.len, dentry->d_name.name);
474
Ian Kent718c6042006-03-27 01:14:42 -0800475 /* File name too long to exist */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (dentry->d_name.len > NAME_MAX)
Ian Kent718c6042006-03-27 01:14:42 -0800477 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 sbi = autofs4_sbi(dir->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 oz_mode = autofs4_oz_mode(sbi);
Ian Kent718c6042006-03-27 01:14:42 -0800481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
483 current->pid, process_group(current), sbi->catatonic, oz_mode);
484
485 /*
486 * Mark the dentry incomplete, but add it. This is needed so
487 * that the VFS layer knows about the dentry, and we can count
488 * on catching any lookups through the revalidate.
489 *
490 * Let all the hard work be done by the revalidate function that
491 * needs to be able to do this anyway..
492 *
493 * We need to do this before we release the directory semaphore.
494 */
495 dentry->d_op = &autofs4_root_dentry_operations;
496
497 if (!oz_mode) {
498 spin_lock(&dentry->d_lock);
499 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
500 spin_unlock(&dentry->d_lock);
501 }
502 dentry->d_fsdata = NULL;
503 d_add(dentry, NULL);
504
505 if (dentry->d_op && dentry->d_op->d_revalidate) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800506 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 (dentry->d_op->d_revalidate)(dentry, nd);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800508 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
511 /*
512 * If we are still pending, check if we had to handle
513 * a signal. If so we can force a restart..
514 */
515 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
516 /* See if we were interrupted */
517 if (signal_pending(current)) {
518 sigset_t *sigset = &current->pending.signal;
519 if (sigismember (sigset, SIGKILL) ||
520 sigismember (sigset, SIGQUIT) ||
521 sigismember (sigset, SIGINT)) {
522 return ERR_PTR(-ERESTARTNOINTR);
523 }
524 }
525 }
526
527 /*
528 * If this dentry is unhashed, then we shouldn't honour this
529 * lookup even if the dentry is positive. Returning ENOENT here
530 * doesn't do the right thing for all system calls, but it should
531 * be OK for the operations we permit from an autofs.
532 */
Ian Kent718c6042006-03-27 01:14:42 -0800533 if (dentry->d_inode && d_unhashed(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return ERR_PTR(-ENOENT);
535
536 return NULL;
537}
538
539static int autofs4_dir_symlink(struct inode *dir,
540 struct dentry *dentry,
541 const char *symname)
542{
543 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
544 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800545 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct inode *inode;
547 char *cp;
548
549 DPRINTK("%s <- %.*s", symname,
550 dentry->d_name.len, dentry->d_name.name);
551
552 if (!autofs4_oz_mode(sbi))
553 return -EACCES;
554
555 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
556 if (ino == NULL)
557 return -ENOSPC;
558
559 ino->size = strlen(symname);
560 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
561
562 if (cp == NULL) {
563 kfree(ino);
564 return -ENOSPC;
565 }
566
567 strcpy(cp, symname);
568
569 inode = autofs4_get_inode(dir->i_sb, ino);
570 d_instantiate(dentry, inode);
571
572 if (dir == dir->i_sb->s_root->d_inode)
573 dentry->d_op = &autofs4_root_dentry_operations;
574 else
575 dentry->d_op = &autofs4_dentry_operations;
576
577 dentry->d_fsdata = ino;
578 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800579 atomic_inc(&ino->count);
580 p_ino = autofs4_dentry_ino(dentry->d_parent);
581 if (p_ino && dentry->d_parent != dentry)
582 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 ino->inode = inode;
584
585 dir->i_mtime = CURRENT_TIME;
586
587 return 0;
588}
589
590/*
591 * NOTE!
592 *
593 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
594 * that the file no longer exists. However, doing that means that the
595 * VFS layer can turn the dentry into a negative dentry. We don't want
596 * this, because since the unlink is probably the result of an expire.
597 * We simply d_drop it, which allows the dentry lookup to remount it
598 * if necessary.
599 *
600 * If a process is blocked on the dentry waiting for the expire to finish,
601 * it will invalidate the dentry and try to mount with a new one.
602 *
603 * Also see autofs4_dir_rmdir()..
604 */
605static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
606{
607 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
608 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800609 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /* This allows root to remove symlinks */
612 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
613 return -EACCES;
614
Ian Kent1aff3c82006-03-27 01:14:46 -0800615 if (atomic_dec_and_test(&ino->count)) {
616 p_ino = autofs4_dentry_ino(dentry->d_parent);
617 if (p_ino && dentry->d_parent != dentry)
618 atomic_dec(&p_ino->count);
619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 dput(ino->dentry);
621
622 dentry->d_inode->i_size = 0;
623 dentry->d_inode->i_nlink = 0;
624
625 dir->i_mtime = CURRENT_TIME;
626
627 d_drop(dentry);
628
629 return 0;
630}
631
632static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
633{
634 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
635 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800636 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (!autofs4_oz_mode(sbi))
639 return -EACCES;
640
641 spin_lock(&dcache_lock);
642 if (!list_empty(&dentry->d_subdirs)) {
643 spin_unlock(&dcache_lock);
644 return -ENOTEMPTY;
645 }
646 spin_lock(&dentry->d_lock);
647 __d_drop(dentry);
648 spin_unlock(&dentry->d_lock);
649 spin_unlock(&dcache_lock);
650
Ian Kent1aff3c82006-03-27 01:14:46 -0800651 if (atomic_dec_and_test(&ino->count)) {
652 p_ino = autofs4_dentry_ino(dentry->d_parent);
653 if (p_ino && dentry->d_parent != dentry)
654 atomic_dec(&p_ino->count);
655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 dput(ino->dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 dentry->d_inode->i_size = 0;
658 dentry->d_inode->i_nlink = 0;
659
660 if (dir->i_nlink)
661 dir->i_nlink--;
662
663 return 0;
664}
665
666static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
667{
668 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
669 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800670 struct autofs_info *p_ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct inode *inode;
672
673 if ( !autofs4_oz_mode(sbi) )
674 return -EACCES;
675
676 DPRINTK("dentry %p, creating %.*s",
677 dentry, dentry->d_name.len, dentry->d_name.name);
678
679 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
680 if (ino == NULL)
681 return -ENOSPC;
682
683 inode = autofs4_get_inode(dir->i_sb, ino);
684 d_instantiate(dentry, inode);
685
686 if (dir == dir->i_sb->s_root->d_inode)
687 dentry->d_op = &autofs4_root_dentry_operations;
688 else
689 dentry->d_op = &autofs4_dentry_operations;
690
691 dentry->d_fsdata = ino;
692 ino->dentry = dget(dentry);
Ian Kent1aff3c82006-03-27 01:14:46 -0800693 atomic_inc(&ino->count);
694 p_ino = autofs4_dentry_ino(dentry->d_parent);
695 if (p_ino && dentry->d_parent != dentry)
696 atomic_inc(&p_ino->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 ino->inode = inode;
698 dir->i_nlink++;
699 dir->i_mtime = CURRENT_TIME;
700
701 return 0;
702}
703
704/* Get/set timeout ioctl() operation */
705static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
706 unsigned long __user *p)
707{
708 int rv;
709 unsigned long ntimeout;
710
711 if ( (rv = get_user(ntimeout, p)) ||
712 (rv = put_user(sbi->exp_timeout/HZ, p)) )
713 return rv;
714
715 if ( ntimeout > ULONG_MAX/HZ )
716 sbi->exp_timeout = 0;
717 else
718 sbi->exp_timeout = ntimeout * HZ;
719
720 return 0;
721}
722
723/* Return protocol version */
724static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
725{
726 return put_user(sbi->version, p);
727}
728
729/* Return protocol sub version */
730static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
731{
732 return put_user(sbi->sub_version, p);
733}
734
735/*
736 * Tells the daemon whether we need to reghost or not. Also, clears
737 * the reghost_needed flag.
738 */
739static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
740{
741 int status;
742
743 DPRINTK("returning %d", sbi->needs_reghost);
744
745 status = put_user(sbi->needs_reghost, p);
746 if ( status )
747 return status;
748
749 sbi->needs_reghost = 0;
750 return 0;
751}
752
753/*
754 * Enable / Disable reghosting ioctl() operation
755 */
756static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
757{
758 int status;
759 int val;
760
761 status = get_user(val, p);
762
763 DPRINTK("reghost = %d", val);
764
765 if (status)
766 return status;
767
768 /* turn on/off reghosting, with the val */
769 sbi->reghost_enabled = val;
770 return 0;
771}
772
773/*
774* Tells the daemon whether it can umount the autofs mount.
775*/
776static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
777{
778 int status = 0;
779
Ian Kente3474a82006-03-27 01:14:51 -0800780 if (may_umount(mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 status = 1;
782
783 DPRINTK("returning %d", status);
784
785 status = put_user(status, p);
786
787 return status;
788}
789
790/* Identify autofs4_dentries - this is so we can tell if there's
791 an extra dentry refcount or not. We only hold a refcount on the
792 dentry if its non-negative (ie, d_inode != NULL)
793*/
794int is_autofs4_dentry(struct dentry *dentry)
795{
796 return dentry && dentry->d_inode &&
797 (dentry->d_op == &autofs4_root_dentry_operations ||
798 dentry->d_op == &autofs4_dentry_operations) &&
799 dentry->d_fsdata != NULL;
800}
801
802/*
803 * ioctl()'s on the root directory is the chief method for the daemon to
804 * generate kernel reactions
805 */
806static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
807 unsigned int cmd, unsigned long arg)
808{
809 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
810 void __user *p = (void __user *)arg;
811
812 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
813 cmd,arg,sbi,process_group(current));
814
815 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
816 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
817 return -ENOTTY;
818
819 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
820 return -EPERM;
821
822 switch(cmd) {
823 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
824 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
825 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
826 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
827 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
828 autofs4_catatonic_mode(sbi);
829 return 0;
830 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
831 return autofs4_get_protover(sbi, p);
832 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
833 return autofs4_get_protosubver(sbi, p);
834 case AUTOFS_IOC_SETTIMEOUT:
835 return autofs4_get_set_timeout(sbi, p);
836
837 case AUTOFS_IOC_TOGGLEREGHOST:
838 return autofs4_toggle_reghost(sbi, p);
839 case AUTOFS_IOC_ASKREGHOST:
840 return autofs4_ask_reghost(sbi, p);
841
842 case AUTOFS_IOC_ASKUMOUNT:
843 return autofs4_ask_umount(filp->f_vfsmnt, p);
844
845 /* return a single thing to expire */
846 case AUTOFS_IOC_EXPIRE:
847 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
848 /* same as above, but can send multiple expires through pipe */
849 case AUTOFS_IOC_EXPIRE_MULTI:
850 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);
851
852 default:
853 return -ENOSYS;
854 }
855}