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