fs: dcache remove dcache_lock
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / autofs4 / root.c
CommitLineData
1da177e4
LT
1/* -*- 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>
34ca959c 7 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
1da177e4
LT
8 *
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
16f7e0fe 15#include <linux/capability.h>
1da177e4
LT
16#include <linux/errno.h>
17#include <linux/stat.h>
5a0e3ad6 18#include <linux/slab.h>
1da177e4
LT
19#include <linux/param.h>
20#include <linux/time.h>
c9243f5b 21#include <linux/compat.h>
00e300e1 22#include <linux/mutex.h>
c9243f5b 23
1da177e4
LT
24#include "autofs_i.h"
25
b5c84bf6
NP
26DEFINE_SPINLOCK(autofs4_lock);
27
1da177e4
LT
28static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
29static int autofs4_dir_unlink(struct inode *,struct dentry *);
30static int autofs4_dir_rmdir(struct inode *,struct dentry *);
31static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
3663df70 32static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
5a44a73b 33#ifdef CONFIG_COMPAT
c9243f5b 34static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
5a44a73b 35#endif
1da177e4 36static int autofs4_dir_open(struct inode *inode, struct file *file);
1da177e4 37static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
34ca959c 38static void *autofs4_follow_link(struct dentry *, struct nameidata *);
1da177e4 39
6d5cb926
IK
40#define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
41#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
42
4b6f5d20 43const struct file_operations autofs4_root_operations = {
1da177e4
LT
44 .open = dcache_dir_open,
45 .release = dcache_dir_close,
46 .read = generic_read_dir,
aa55ddf3 47 .readdir = dcache_readdir,
59af1584 48 .llseek = dcache_dir_lseek,
3663df70 49 .unlocked_ioctl = autofs4_root_ioctl,
c9243f5b
AB
50#ifdef CONFIG_COMPAT
51 .compat_ioctl = autofs4_root_compat_ioctl,
52#endif
1da177e4
LT
53};
54
4b6f5d20 55const struct file_operations autofs4_dir_operations = {
1da177e4 56 .open = autofs4_dir_open,
ff9cd499 57 .release = dcache_dir_close,
1da177e4 58 .read = generic_read_dir,
ff9cd499 59 .readdir = dcache_readdir,
59af1584 60 .llseek = dcache_dir_lseek,
1da177e4
LT
61};
62
754661f1 63const struct inode_operations autofs4_indirect_root_inode_operations = {
1da177e4
LT
64 .lookup = autofs4_lookup,
65 .unlink = autofs4_dir_unlink,
66 .symlink = autofs4_dir_symlink,
67 .mkdir = autofs4_dir_mkdir,
68 .rmdir = autofs4_dir_rmdir,
69};
70
754661f1 71const struct inode_operations autofs4_direct_root_inode_operations = {
34ca959c 72 .lookup = autofs4_lookup,
871f9434
IK
73 .unlink = autofs4_dir_unlink,
74 .mkdir = autofs4_dir_mkdir,
75 .rmdir = autofs4_dir_rmdir,
34ca959c
IK
76 .follow_link = autofs4_follow_link,
77};
78
754661f1 79const struct inode_operations autofs4_dir_inode_operations = {
1da177e4
LT
80 .lookup = autofs4_lookup,
81 .unlink = autofs4_dir_unlink,
82 .symlink = autofs4_dir_symlink,
83 .mkdir = autofs4_dir_mkdir,
84 .rmdir = autofs4_dir_rmdir,
85};
86
4f8427d1
IK
87static void autofs4_add_active(struct dentry *dentry)
88{
89 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
90 struct autofs_info *ino = autofs4_dentry_ino(dentry);
91 if (ino) {
92 spin_lock(&sbi->lookup_lock);
93 if (!ino->active_count) {
94 if (list_empty(&ino->active))
95 list_add(&ino->active, &sbi->active_list);
96 }
97 ino->active_count++;
98 spin_unlock(&sbi->lookup_lock);
99 }
100 return;
101}
102
103static void autofs4_del_active(struct dentry *dentry)
104{
105 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
106 struct autofs_info *ino = autofs4_dentry_ino(dentry);
107 if (ino) {
108 spin_lock(&sbi->lookup_lock);
109 ino->active_count--;
110 if (!ino->active_count) {
111 if (!list_empty(&ino->active))
112 list_del_init(&ino->active);
113 }
114 spin_unlock(&sbi->lookup_lock);
115 }
116 return;
117}
118
36b6413e
IK
119static unsigned int autofs4_need_mount(unsigned int flags)
120{
121 unsigned int res = 0;
122 if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
123 res = 1;
124 return res;
125}
126
1da177e4
LT
127static int autofs4_dir_open(struct inode *inode, struct file *file)
128{
a4669ed8 129 struct dentry *dentry = file->f_path.dentry;
1da177e4 130 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
f360ce3b 131
1da177e4
LT
132 DPRINTK("file=%p dentry=%p %.*s",
133 file, dentry, dentry->d_name.len, dentry->d_name.name);
134
135 if (autofs4_oz_mode(sbi))
136 goto out;
137
ff9cd499
IK
138 /*
139 * An empty directory in an autofs file system is always a
140 * mount point. The daemon must have failed to mount this
141 * during lookup so it doesn't exist. This can happen, for
142 * example, if user space returns an incorrect status for a
143 * mount request. Otherwise we're doing a readdir on the
144 * autofs file system so just let the libfs routines handle
145 * it.
146 */
b5c84bf6 147 spin_lock(&autofs4_lock);
2fd6b7f5 148 spin_lock(&dentry->d_lock);
c42c7f7e 149 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
2fd6b7f5 150 spin_unlock(&dentry->d_lock);
b5c84bf6 151 spin_unlock(&autofs4_lock);
ff9cd499 152 return -ENOENT;
1da177e4 153 }
2fd6b7f5 154 spin_unlock(&dentry->d_lock);
b5c84bf6 155 spin_unlock(&autofs4_lock);
1da177e4 156
1da177e4 157out:
ff9cd499 158 return dcache_dir_open(inode, file);
1da177e4
LT
159}
160
4b1ae27a 161static int try_to_fill_dentry(struct dentry *dentry, int flags)
1da177e4 162{
862b110f 163 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
718c604a 164 struct autofs_info *ino = autofs4_dentry_ino(dentry);
9d2de6ad 165 int status;
1da177e4 166
1da177e4
LT
167 DPRINTK("dentry=%p %.*s ino=%p",
168 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
169
718c604a
IK
170 /*
171 * Wait for a pending mount, triggering one if there
172 * isn't one already
173 */
4b1ae27a
AV
174 if (dentry->d_inode == NULL) {
175 DPRINTK("waiting for mount name=%.*s",
176 dentry->d_name.len, dentry->d_name.name);
1da177e4 177
4b1ae27a 178 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
718c604a 179
4b1ae27a 180 DPRINTK("mount done status=%d", status);
1da177e4 181
4b1ae27a
AV
182 /* Turn this into a real negative dentry? */
183 if (status == -ENOENT) {
184 spin_lock(&sbi->fs_lock);
185 ino->flags &= ~AUTOFS_INF_PENDING;
186 spin_unlock(&sbi->fs_lock);
187 return status;
188 } else if (status) {
189 /* Return a negative dentry, but leave it "pending" */
190 return status;
191 }
192 /* Trigger mount for path component or follow link */
193 } else if (ino->flags & AUTOFS_INF_PENDING ||
f7422464 194 autofs4_need_mount(flags)) {
4b1ae27a
AV
195 DPRINTK("waiting for mount name=%.*s",
196 dentry->d_name.len, dentry->d_name.name);
1da177e4 197
4b1ae27a
AV
198 spin_lock(&sbi->fs_lock);
199 ino->flags |= AUTOFS_INF_PENDING;
200 spin_unlock(&sbi->fs_lock);
201 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
202
203 DPRINTK("mount done status=%d", status);
204
205 if (status) {
206 spin_lock(&sbi->fs_lock);
207 ino->flags &= ~AUTOFS_INF_PENDING;
208 spin_unlock(&sbi->fs_lock);
209 return status;
210 }
211 }
212
213 /* Initialize expiry counter after successful mount */
5fc79d85 214 ino->last_used = jiffies;
4b1ae27a
AV
215
216 spin_lock(&sbi->fs_lock);
217 ino->flags &= ~AUTOFS_INF_PENDING;
218 spin_unlock(&sbi->fs_lock);
219
220 return 0;
34ca959c
IK
221}
222
223/* For autofs direct mounts the follow link triggers the mount */
224static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
225{
226 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
a5370553 227 struct autofs_info *ino = autofs4_dentry_ino(dentry);
34ca959c
IK
228 int oz_mode = autofs4_oz_mode(sbi);
229 unsigned int lookup_type;
230 int status;
231
232 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
233 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
234 nd->flags);
6e60a9ab
IK
235 /*
236 * For an expire of a covered direct or offset mount we need
9393bd07 237 * to break out of follow_down() at the autofs mount trigger
6e60a9ab
IK
238 * (d_mounted--), so we can see the expiring flag, and manage
239 * the blocking and following here until the expire is completed.
240 */
241 if (oz_mode) {
242 spin_lock(&sbi->fs_lock);
243 if (ino->flags & AUTOFS_INF_EXPIRING) {
244 spin_unlock(&sbi->fs_lock);
245 /* Follow down to our covering mount. */
9393bd07 246 if (!follow_down(&nd->path))
6e60a9ab 247 goto done;
ec6e8c7d 248 goto follow;
6e60a9ab
IK
249 }
250 spin_unlock(&sbi->fs_lock);
34ca959c 251 goto done;
6e60a9ab 252 }
34ca959c 253
6e60a9ab 254 /* If an expire request is pending everyone must wait. */
06a35985 255 autofs4_expire_wait(dentry);
871f9434 256
6e60a9ab 257 /* We trigger a mount for almost all flags */
36b6413e 258 lookup_type = autofs4_need_mount(nd->flags);
aa952eb2 259 spin_lock(&sbi->fs_lock);
b5c84bf6 260 spin_lock(&autofs4_lock);
2fd6b7f5 261 spin_lock(&dentry->d_lock);
aa952eb2 262 if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
2fd6b7f5 263 spin_unlock(&dentry->d_lock);
b5c84bf6 264 spin_unlock(&autofs4_lock);
aa952eb2 265 spin_unlock(&sbi->fs_lock);
ec6e8c7d 266 goto follow;
aa952eb2 267 }
6e60a9ab 268
871f9434 269 /*
6e60a9ab
IK
270 * If the dentry contains directories then it is an autofs
271 * multi-mount with no root mount offset. So don't try to
272 * mount it again.
871f9434 273 */
aa952eb2 274 if (ino->flags & AUTOFS_INF_PENDING ||
c42c7f7e 275 (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
2fd6b7f5 276 spin_unlock(&dentry->d_lock);
b5c84bf6 277 spin_unlock(&autofs4_lock);
aa952eb2 278 spin_unlock(&sbi->fs_lock);
871f9434 279
f7422464 280 status = try_to_fill_dentry(dentry, nd->flags);
871f9434
IK
281 if (status)
282 goto out_error;
283
6e60a9ab 284 goto follow;
34ca959c 285 }
2fd6b7f5 286 spin_unlock(&dentry->d_lock);
b5c84bf6 287 spin_unlock(&autofs4_lock);
aa952eb2 288 spin_unlock(&sbi->fs_lock);
6e60a9ab
IK
289follow:
290 /*
291 * If there is no root mount it must be an autofs
292 * multi-mount with no root offset so we don't need
293 * to follow it.
294 */
295 if (d_mountpoint(dentry)) {
9393bd07 296 if (!autofs4_follow_mount(&nd->path)) {
6e60a9ab
IK
297 status = -ENOENT;
298 goto out_error;
299 }
300 }
34ca959c
IK
301
302done:
303 return NULL;
304
305out_error:
1d957f9b 306 path_put(&nd->path);
34ca959c 307 return ERR_PTR(status);
1da177e4
LT
308}
309
310/*
311 * Revalidate is called on every cache lookup. Some of those
312 * cache lookups may actually happen while the dentry is not
313 * yet completely filled in, and revalidate has to delay such
314 * lookups..
315 */
718c604a 316static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
1da177e4 317{
718c604a 318 struct inode *dir = dentry->d_parent->d_inode;
1da177e4 319 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
4b1ae27a 320 int oz_mode = autofs4_oz_mode(sbi);
1da177e4 321 int flags = nd ? nd->flags : 0;
4b1ae27a 322 int status = 1;
1da177e4 323
213614d5 324 /* Pending dentry */
4b1ae27a 325 spin_lock(&sbi->fs_lock);
1da177e4 326 if (autofs4_ispending(dentry)) {
4b1ae27a 327 /* The daemon never causes a mount to trigger */
213614d5 328 spin_unlock(&sbi->fs_lock);
4b1ae27a
AV
329
330 if (oz_mode)
331 return 1;
bcdc5e01 332
06a35985
IK
333 /*
334 * If the directory has gone away due to an expire
335 * we have been called as ->d_revalidate() and so
336 * we need to return false and proceed to ->lookup().
337 */
338 if (autofs4_expire_wait(dentry) == -EAGAIN)
339 return 0;
340
bcdc5e01
IK
341 /*
342 * A zero status is success otherwise we have a
343 * negative error code.
344 */
4b1ae27a 345 status = try_to_fill_dentry(dentry, flags);
bcdc5e01 346 if (status == 0)
f50b6f86
IK
347 return 1;
348
bcdc5e01 349 return status;
1da177e4 350 }
4b1ae27a
AV
351 spin_unlock(&sbi->fs_lock);
352
353 /* Negative dentry.. invalidate if "old" */
354 if (dentry->d_inode == NULL)
355 return 0;
1da177e4
LT
356
357 /* Check for a non-mountpoint directory with no contents */
b5c84bf6 358 spin_lock(&autofs4_lock);
2fd6b7f5 359 spin_lock(&dentry->d_lock);
1da177e4 360 if (S_ISDIR(dentry->d_inode->i_mode) &&
c42c7f7e 361 !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
1da177e4
LT
362 DPRINTK("dentry=%p %.*s, emptydir",
363 dentry, dentry->d_name.len, dentry->d_name.name);
2fd6b7f5 364 spin_unlock(&dentry->d_lock);
b5c84bf6 365 spin_unlock(&autofs4_lock);
97e7449a 366
4b1ae27a
AV
367 /* The daemon never causes a mount to trigger */
368 if (oz_mode)
369 return 1;
213614d5 370
4b1ae27a
AV
371 /*
372 * A zero status is success otherwise we have a
373 * negative error code.
374 */
375 status = try_to_fill_dentry(dentry, flags);
376 if (status == 0)
377 return 1;
213614d5 378
4b1ae27a 379 return status;
1da177e4 380 }
2fd6b7f5 381 spin_unlock(&dentry->d_lock);
b5c84bf6 382 spin_unlock(&autofs4_lock);
1da177e4 383
1da177e4
LT
384 return 1;
385}
386
34ca959c 387void autofs4_dentry_release(struct dentry *de)
1da177e4
LT
388{
389 struct autofs_info *inf;
390
391 DPRINTK("releasing %p", de);
392
393 inf = autofs4_dentry_ino(de);
394 de->d_fsdata = NULL;
395
396 if (inf) {
f50b6f86
IK
397 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
398
f50b6f86 399 if (sbi) {
5f6f4f28 400 spin_lock(&sbi->lookup_lock);
25767378
IK
401 if (!list_empty(&inf->active))
402 list_del(&inf->active);
5f6f4f28
IK
403 if (!list_empty(&inf->expiring))
404 list_del(&inf->expiring);
405 spin_unlock(&sbi->lookup_lock);
f50b6f86
IK
406 }
407
c3724b12
JM
408 inf->dentry = NULL;
409 inf->inode = NULL;
410
1da177e4
LT
411 autofs4_free_ino(inf);
412 }
413}
414
415/* For dentries of directories in the root dir */
08f11513 416static const struct dentry_operations autofs4_root_dentry_operations = {
1da177e4
LT
417 .d_revalidate = autofs4_revalidate,
418 .d_release = autofs4_dentry_release,
419};
420
421/* For other dentries */
08f11513 422static const struct dentry_operations autofs4_dentry_operations = {
1da177e4
LT
423 .d_revalidate = autofs4_revalidate,
424 .d_release = autofs4_dentry_release,
425};
426
6510c9d8 427static struct dentry *autofs4_lookup_active(struct dentry *dentry)
25767378 428{
6510c9d8
IK
429 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
430 struct dentry *parent = dentry->d_parent;
431 struct qstr *name = &dentry->d_name;
25767378
IK
432 unsigned int len = name->len;
433 unsigned int hash = name->hash;
434 const unsigned char *str = name->name;
435 struct list_head *p, *head;
436
b5c84bf6 437 spin_lock(&autofs4_lock);
25767378
IK
438 spin_lock(&sbi->lookup_lock);
439 head = &sbi->active_list;
440 list_for_each(p, head) {
441 struct autofs_info *ino;
e4d5ade7 442 struct dentry *active;
25767378
IK
443 struct qstr *qstr;
444
445 ino = list_entry(p, struct autofs_info, active);
e4d5ade7 446 active = ino->dentry;
25767378 447
e4d5ade7 448 spin_lock(&active->d_lock);
25767378
IK
449
450 /* Already gone? */
b7ab39f6 451 if (active->d_count == 0)
25767378
IK
452 goto next;
453
e4d5ade7 454 qstr = &active->d_name;
25767378 455
e4d5ade7 456 if (active->d_name.hash != hash)
25767378 457 goto next;
e4d5ade7 458 if (active->d_parent != parent)
25767378
IK
459 goto next;
460
461 if (qstr->len != len)
462 goto next;
463 if (memcmp(qstr->name, str, len))
464 goto next;
465
4b1ae27a 466 if (d_unhashed(active)) {
b7ab39f6 467 dget_dlock(active);
4b1ae27a
AV
468 spin_unlock(&active->d_lock);
469 spin_unlock(&sbi->lookup_lock);
b5c84bf6 470 spin_unlock(&autofs4_lock);
4b1ae27a
AV
471 return active;
472 }
25767378 473next:
e4d5ade7 474 spin_unlock(&active->d_lock);
25767378
IK
475 }
476 spin_unlock(&sbi->lookup_lock);
b5c84bf6 477 spin_unlock(&autofs4_lock);
25767378
IK
478
479 return NULL;
480}
481
6510c9d8 482static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
f50b6f86 483{
6510c9d8
IK
484 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
485 struct dentry *parent = dentry->d_parent;
486 struct qstr *name = &dentry->d_name;
f50b6f86
IK
487 unsigned int len = name->len;
488 unsigned int hash = name->hash;
489 const unsigned char *str = name->name;
490 struct list_head *p, *head;
491
b5c84bf6 492 spin_lock(&autofs4_lock);
5f6f4f28
IK
493 spin_lock(&sbi->lookup_lock);
494 head = &sbi->expiring_list;
f50b6f86
IK
495 list_for_each(p, head) {
496 struct autofs_info *ino;
cb4b492a 497 struct dentry *expiring;
f50b6f86
IK
498 struct qstr *qstr;
499
5f6f4f28 500 ino = list_entry(p, struct autofs_info, expiring);
cb4b492a 501 expiring = ino->dentry;
f50b6f86 502
cb4b492a 503 spin_lock(&expiring->d_lock);
f50b6f86
IK
504
505 /* Bad luck, we've already been dentry_iput */
cb4b492a 506 if (!expiring->d_inode)
f50b6f86
IK
507 goto next;
508
cb4b492a 509 qstr = &expiring->d_name;
f50b6f86 510
cb4b492a 511 if (expiring->d_name.hash != hash)
f50b6f86 512 goto next;
cb4b492a 513 if (expiring->d_parent != parent)
f50b6f86
IK
514 goto next;
515
516 if (qstr->len != len)
517 goto next;
518 if (memcmp(qstr->name, str, len))
519 goto next;
520
4b1ae27a 521 if (d_unhashed(expiring)) {
b7ab39f6 522 dget_dlock(expiring);
4b1ae27a
AV
523 spin_unlock(&expiring->d_lock);
524 spin_unlock(&sbi->lookup_lock);
b5c84bf6 525 spin_unlock(&autofs4_lock);
4b1ae27a
AV
526 return expiring;
527 }
f50b6f86 528next:
cb4b492a 529 spin_unlock(&expiring->d_lock);
f50b6f86 530 }
5f6f4f28 531 spin_unlock(&sbi->lookup_lock);
b5c84bf6 532 spin_unlock(&autofs4_lock);
f50b6f86
IK
533
534 return NULL;
535}
536
1da177e4
LT
537/* Lookups in the root directory */
538static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
539{
540 struct autofs_sb_info *sbi;
25767378 541 struct autofs_info *ino;
90387c9c 542 struct dentry *expiring, *active;
1da177e4
LT
543 int oz_mode;
544
545 DPRINTK("name = %.*s",
546 dentry->d_name.len, dentry->d_name.name);
547
718c604a 548 /* File name too long to exist */
1da177e4 549 if (dentry->d_name.len > NAME_MAX)
718c604a 550 return ERR_PTR(-ENAMETOOLONG);
1da177e4
LT
551
552 sbi = autofs4_sbi(dir->i_sb);
1da177e4 553 oz_mode = autofs4_oz_mode(sbi);
718c604a 554
1da177e4 555 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
a47afb0f 556 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
1da177e4 557
6510c9d8 558 active = autofs4_lookup_active(dentry);
90387c9c
IK
559 if (active) {
560 dentry = active;
aa952eb2
IK
561 ino = autofs4_dentry_ino(dentry);
562 } else {
4b1ae27a
AV
563 /*
564 * Mark the dentry incomplete but don't hash it. We do this
565 * to serialize our inode creation operations (symlink and
566 * mkdir) which prevents deadlock during the callback to
567 * the daemon. Subsequent user space lookups for the same
568 * dentry are placed on the wait queue while the daemon
569 * itself is allowed passage unresticted so the create
570 * operation itself can then hash the dentry. Finally,
571 * we check for the hashed dentry and return the newly
572 * hashed dentry.
573 */
574 dentry->d_op = &autofs4_root_dentry_operations;
575
576 /*
577 * And we need to ensure that the same dentry is used for
578 * all following lookup calls until it is hashed so that
579 * the dentry flags are persistent throughout the request.
580 */
581 ino = autofs4_init_ino(NULL, sbi, 0555);
582 if (!ino)
583 return ERR_PTR(-ENOMEM);
584
585 dentry->d_fsdata = ino;
586 ino->dentry = dentry;
5f6f4f28 587
4b1ae27a
AV
588 autofs4_add_active(dentry);
589
590 d_instantiate(dentry, NULL);
591 }
213614d5 592
1da177e4 593 if (!oz_mode) {
213614d5 594 mutex_unlock(&dir->i_mutex);
4b1ae27a 595 expiring = autofs4_lookup_expiring(dentry);
8f63aaa8
IK
596 if (expiring) {
597 /*
598 * If we are racing with expire the request might not
599 * be quite complete but the directory has been removed
600 * so it must have been successful, so just wait for it.
601 */
8f63aaa8 602 autofs4_expire_wait(expiring);
4b1ae27a 603 autofs4_del_expiring(expiring);
8f63aaa8
IK
604 dput(expiring);
605 }
4b1ae27a 606
aa952eb2 607 spin_lock(&sbi->fs_lock);
4b1ae27a 608 ino->flags |= AUTOFS_INF_PENDING;
aa952eb2 609 spin_unlock(&sbi->fs_lock);
4b1ae27a
AV
610 if (dentry->d_op && dentry->d_op->d_revalidate)
611 (dentry->d_op->d_revalidate)(dentry, nd);
612 mutex_lock(&dir->i_mutex);
1da177e4
LT
613 }
614
615 /*
4b1ae27a 616 * If we are still pending, check if we had to handle
1da177e4
LT
617 * a signal. If so we can force a restart..
618 */
4b1ae27a 619 if (ino->flags & AUTOFS_INF_PENDING) {
1da177e4
LT
620 /* See if we were interrupted */
621 if (signal_pending(current)) {
622 sigset_t *sigset = &current->pending.signal;
623 if (sigismember (sigset, SIGKILL) ||
624 sigismember (sigset, SIGQUIT) ||
625 sigismember (sigset, SIGINT)) {
90387c9c
IK
626 if (active)
627 dput(active);
1da177e4
LT
628 return ERR_PTR(-ERESTARTNOINTR);
629 }
630 }
4b1ae27a
AV
631 if (!oz_mode) {
632 spin_lock(&sbi->fs_lock);
633 ino->flags &= ~AUTOFS_INF_PENDING;
634 spin_unlock(&sbi->fs_lock);
213614d5
IK
635 }
636 }
c9ffec48 637
213614d5 638 /*
4b1ae27a
AV
639 * If this dentry is unhashed, then we shouldn't honour this
640 * lookup. Returning ENOENT here doesn't do the right thing
641 * for all system calls, but it should be OK for the operations
642 * we permit from an autofs.
213614d5 643 */
4b1ae27a 644 if (!oz_mode && d_unhashed(dentry)) {
213614d5 645 /*
4b1ae27a
AV
646 * A user space application can (and has done in the past)
647 * remove and re-create this directory during the callback.
648 * This can leave us with an unhashed dentry, but a
649 * successful mount! So we need to perform another
650 * cached lookup in case the dentry now exists.
213614d5 651 */
4b1ae27a
AV
652 struct dentry *parent = dentry->d_parent;
653 struct dentry *new = d_lookup(parent, &dentry->d_name);
654 if (new != NULL)
655 dentry = new;
656 else
657 dentry = ERR_PTR(-ENOENT);
658
213614d5 659 if (active)
4b1ae27a
AV
660 dput(active);
661
662 return dentry;
f50b6f86
IK
663 }
664
4b1ae27a
AV
665 if (active)
666 return active;
667
1da177e4
LT
668 return NULL;
669}
670
671static int autofs4_dir_symlink(struct inode *dir,
672 struct dentry *dentry,
673 const char *symname)
674{
675 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
676 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1aff3c8b 677 struct autofs_info *p_ino;
1da177e4
LT
678 struct inode *inode;
679 char *cp;
680
681 DPRINTK("%s <- %.*s", symname,
682 dentry->d_name.len, dentry->d_name.name);
683
684 if (!autofs4_oz_mode(sbi))
685 return -EACCES;
686
687 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
25767378
IK
688 if (!ino)
689 return -ENOMEM;
1da177e4 690
4b1ae27a
AV
691 autofs4_del_active(dentry);
692
ef581a74 693 ino->size = strlen(symname);
25767378
IK
694 cp = kmalloc(ino->size + 1, GFP_KERNEL);
695 if (!cp) {
696 if (!dentry->d_fsdata)
697 kfree(ino);
698 return -ENOMEM;
1da177e4
LT
699 }
700
701 strcpy(cp, symname);
702
703 inode = autofs4_get_inode(dir->i_sb, ino);
25767378
IK
704 if (!inode) {
705 kfree(cp);
706 if (!dentry->d_fsdata)
707 kfree(ino);
708 return -ENOMEM;
709 }
1864f7bd 710 d_add(dentry, inode);
1da177e4
LT
711
712 if (dir == dir->i_sb->s_root->d_inode)
713 dentry->d_op = &autofs4_root_dentry_operations;
714 else
715 dentry->d_op = &autofs4_dentry_operations;
716
717 dentry->d_fsdata = ino;
718 ino->dentry = dget(dentry);
1aff3c8b
IK
719 atomic_inc(&ino->count);
720 p_ino = autofs4_dentry_ino(dentry->d_parent);
721 if (p_ino && dentry->d_parent != dentry)
722 atomic_inc(&p_ino->count);
1da177e4
LT
723 ino->inode = inode;
724
25767378 725 ino->u.symlink = cp;
1da177e4
LT
726 dir->i_mtime = CURRENT_TIME;
727
728 return 0;
729}
730
731/*
732 * NOTE!
733 *
734 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
735 * that the file no longer exists. However, doing that means that the
736 * VFS layer can turn the dentry into a negative dentry. We don't want
f50b6f86 737 * this, because the unlink is probably the result of an expire.
5f6f4f28
IK
738 * We simply d_drop it and add it to a expiring list in the super block,
739 * which allows the dentry lookup to check for an incomplete expire.
1da177e4
LT
740 *
741 * If a process is blocked on the dentry waiting for the expire to finish,
742 * it will invalidate the dentry and try to mount with a new one.
743 *
744 * Also see autofs4_dir_rmdir()..
745 */
746static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
747{
748 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
749 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1aff3c8b 750 struct autofs_info *p_ino;
1da177e4
LT
751
752 /* This allows root to remove symlinks */
d78e53c8 753 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
1da177e4
LT
754 return -EACCES;
755
1aff3c8b
IK
756 if (atomic_dec_and_test(&ino->count)) {
757 p_ino = autofs4_dentry_ino(dentry->d_parent);
758 if (p_ino && dentry->d_parent != dentry)
759 atomic_dec(&p_ino->count);
760 }
1da177e4
LT
761 dput(ino->dentry);
762
763 dentry->d_inode->i_size = 0;
ce71ec36 764 clear_nlink(dentry->d_inode);
1da177e4
LT
765
766 dir->i_mtime = CURRENT_TIME;
767
b5c84bf6 768 spin_lock(&autofs4_lock);
4b1ae27a 769 autofs4_add_expiring(dentry);
f50b6f86
IK
770 spin_lock(&dentry->d_lock);
771 __d_drop(dentry);
772 spin_unlock(&dentry->d_lock);
b5c84bf6 773 spin_unlock(&autofs4_lock);
1da177e4
LT
774
775 return 0;
776}
777
778static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
779{
780 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
781 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1aff3c8b 782 struct autofs_info *p_ino;
1da177e4 783
f50b6f86
IK
784 DPRINTK("dentry %p, removing %.*s",
785 dentry, dentry->d_name.len, dentry->d_name.name);
786
1da177e4
LT
787 if (!autofs4_oz_mode(sbi))
788 return -EACCES;
789
b5c84bf6 790 spin_lock(&autofs4_lock);
2fd6b7f5
NP
791 spin_lock(&sbi->lookup_lock);
792 spin_lock(&dentry->d_lock);
1da177e4 793 if (!list_empty(&dentry->d_subdirs)) {
2fd6b7f5
NP
794 spin_unlock(&dentry->d_lock);
795 spin_unlock(&sbi->lookup_lock);
b5c84bf6 796 spin_unlock(&autofs4_lock);
1da177e4
LT
797 return -ENOTEMPTY;
798 }
2fd6b7f5
NP
799 __autofs4_add_expiring(dentry);
800 spin_unlock(&sbi->lookup_lock);
1da177e4
LT
801 __d_drop(dentry);
802 spin_unlock(&dentry->d_lock);
b5c84bf6 803 spin_unlock(&autofs4_lock);
1da177e4 804
1aff3c8b
IK
805 if (atomic_dec_and_test(&ino->count)) {
806 p_ino = autofs4_dentry_ino(dentry->d_parent);
807 if (p_ino && dentry->d_parent != dentry)
808 atomic_dec(&p_ino->count);
809 }
1da177e4 810 dput(ino->dentry);
1da177e4 811 dentry->d_inode->i_size = 0;
ce71ec36 812 clear_nlink(dentry->d_inode);
1da177e4
LT
813
814 if (dir->i_nlink)
9a53c3a7 815 drop_nlink(dir);
1da177e4
LT
816
817 return 0;
818}
819
820static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
821{
822 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
823 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1aff3c8b 824 struct autofs_info *p_ino;
1da177e4
LT
825 struct inode *inode;
826
d78e53c8 827 if (!autofs4_oz_mode(sbi))
1da177e4
LT
828 return -EACCES;
829
830 DPRINTK("dentry %p, creating %.*s",
831 dentry, dentry->d_name.len, dentry->d_name.name);
832
833 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
25767378
IK
834 if (!ino)
835 return -ENOMEM;
836
4b1ae27a
AV
837 autofs4_del_active(dentry);
838
1da177e4 839 inode = autofs4_get_inode(dir->i_sb, ino);
25767378
IK
840 if (!inode) {
841 if (!dentry->d_fsdata)
842 kfree(ino);
843 return -ENOMEM;
844 }
1864f7bd 845 d_add(dentry, inode);
1da177e4
LT
846
847 if (dir == dir->i_sb->s_root->d_inode)
848 dentry->d_op = &autofs4_root_dentry_operations;
849 else
850 dentry->d_op = &autofs4_dentry_operations;
851
852 dentry->d_fsdata = ino;
853 ino->dentry = dget(dentry);
1aff3c8b
IK
854 atomic_inc(&ino->count);
855 p_ino = autofs4_dentry_ino(dentry->d_parent);
856 if (p_ino && dentry->d_parent != dentry)
857 atomic_inc(&p_ino->count);
1da177e4 858 ino->inode = inode;
d8c76e6f 859 inc_nlink(dir);
1da177e4
LT
860 dir->i_mtime = CURRENT_TIME;
861
862 return 0;
863}
864
865/* Get/set timeout ioctl() operation */
c9243f5b
AB
866#ifdef CONFIG_COMPAT
867static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
868 compat_ulong_t __user *p)
869{
870 int rv;
871 unsigned long ntimeout;
872
873 if ((rv = get_user(ntimeout, p)) ||
874 (rv = put_user(sbi->exp_timeout/HZ, p)))
875 return rv;
876
877 if (ntimeout > UINT_MAX/HZ)
878 sbi->exp_timeout = 0;
879 else
880 sbi->exp_timeout = ntimeout * HZ;
881
882 return 0;
883}
884#endif
885
1da177e4
LT
886static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
887 unsigned long __user *p)
888{
889 int rv;
890 unsigned long ntimeout;
891
d78e53c8
SB
892 if ((rv = get_user(ntimeout, p)) ||
893 (rv = put_user(sbi->exp_timeout/HZ, p)))
1da177e4
LT
894 return rv;
895
d78e53c8 896 if (ntimeout > ULONG_MAX/HZ)
1da177e4
LT
897 sbi->exp_timeout = 0;
898 else
899 sbi->exp_timeout = ntimeout * HZ;
900
901 return 0;
902}
903
904/* Return protocol version */
905static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
906{
907 return put_user(sbi->version, p);
908}
909
910/* Return protocol sub version */
911static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
912{
913 return put_user(sbi->sub_version, p);
914}
915
1da177e4
LT
916/*
917* Tells the daemon whether it can umount the autofs mount.
918*/
919static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
920{
921 int status = 0;
922
e3474a8e 923 if (may_umount(mnt))
1da177e4
LT
924 status = 1;
925
926 DPRINTK("returning %d", status);
927
928 status = put_user(status, p);
929
930 return status;
931}
932
933/* Identify autofs4_dentries - this is so we can tell if there's
934 an extra dentry refcount or not. We only hold a refcount on the
935 dentry if its non-negative (ie, d_inode != NULL)
936*/
937int is_autofs4_dentry(struct dentry *dentry)
938{
939 return dentry && dentry->d_inode &&
940 (dentry->d_op == &autofs4_root_dentry_operations ||
941 dentry->d_op == &autofs4_dentry_operations) &&
942 dentry->d_fsdata != NULL;
943}
944
945/*
946 * ioctl()'s on the root directory is the chief method for the daemon to
947 * generate kernel reactions
948 */
3663df70
FW
949static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
950 unsigned int cmd, unsigned long arg)
1da177e4
LT
951{
952 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
953 void __user *p = (void __user *)arg;
954
955 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
a47afb0f 956 cmd,arg,sbi,task_pgrp_nr(current));
1da177e4 957
d78e53c8
SB
958 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
959 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
1da177e4
LT
960 return -ENOTTY;
961
d78e53c8 962 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
1da177e4
LT
963 return -EPERM;
964
965 switch(cmd) {
966 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
967 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
968 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
969 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
970 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
971 autofs4_catatonic_mode(sbi);
972 return 0;
973 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
974 return autofs4_get_protover(sbi, p);
975 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
976 return autofs4_get_protosubver(sbi, p);
977 case AUTOFS_IOC_SETTIMEOUT:
978 return autofs4_get_set_timeout(sbi, p);
c9243f5b
AB
979#ifdef CONFIG_COMPAT
980 case AUTOFS_IOC_SETTIMEOUT32:
981 return autofs4_compat_get_set_timeout(sbi, p);
982#endif
1da177e4 983
1da177e4 984 case AUTOFS_IOC_ASKUMOUNT:
a4669ed8 985 return autofs4_ask_umount(filp->f_path.mnt, p);
1da177e4
LT
986
987 /* return a single thing to expire */
988 case AUTOFS_IOC_EXPIRE:
a4669ed8 989 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
1da177e4
LT
990 /* same as above, but can send multiple expires through pipe */
991 case AUTOFS_IOC_EXPIRE_MULTI:
a4669ed8 992 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
1da177e4
LT
993
994 default:
995 return -ENOSYS;
996 }
997}
3663df70
FW
998
999static long autofs4_root_ioctl(struct file *filp,
1000 unsigned int cmd, unsigned long arg)
1001{
3663df70 1002 struct inode *inode = filp->f_dentry->d_inode;
de47de74 1003 return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
3663df70 1004}
c9243f5b
AB
1005
1006#ifdef CONFIG_COMPAT
1007static long autofs4_root_compat_ioctl(struct file *filp,
1008 unsigned int cmd, unsigned long arg)
1009{
1010 struct inode *inode = filp->f_path.dentry->d_inode;
1011 int ret;
1012
c9243f5b
AB
1013 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
1014 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
1015 else
1016 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
1017 (unsigned long)compat_ptr(arg));
c9243f5b
AB
1018
1019 return ret;
1020}
1021#endif