Remove the automount through follow_link() kludge code from pathwalk
[deliverable/linux.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
cc53ce53 237 * to break out of follow_down_one() 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. */
cc53ce53 246 if (!follow_down_one(&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 */
cc53ce53
DH
295 if (d_managed(dentry)) {
296 status = follow_down(&nd->path, false);
297 if (status < 0)
6e60a9ab 298 goto out_error;
6e60a9ab 299 }
34ca959c
IK
300
301done:
302 return NULL;
303
304out_error:
1d957f9b 305 path_put(&nd->path);
34ca959c 306 return ERR_PTR(status);
1da177e4
LT
307}
308
309/*
310 * Revalidate is called on every cache lookup. Some of those
311 * cache lookups may actually happen while the dentry is not
312 * yet completely filled in, and revalidate has to delay such
313 * lookups..
314 */
718c604a 315static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
1da177e4 316{
34286d66
NP
317 struct inode *dir;
318 struct autofs_sb_info *sbi;
319 int oz_mode;
1da177e4 320 int flags = nd ? nd->flags : 0;
4b1ae27a 321 int status = 1;
1da177e4 322
34286d66
NP
323 if (flags & LOOKUP_RCU)
324 return -ECHILD;
325
326 dir = dentry->d_parent->d_inode;
327 sbi = autofs4_sbi(dir->i_sb);
328 oz_mode = autofs4_oz_mode(sbi);
329
213614d5 330 /* Pending dentry */
4b1ae27a 331 spin_lock(&sbi->fs_lock);
1da177e4 332 if (autofs4_ispending(dentry)) {
4b1ae27a 333 /* The daemon never causes a mount to trigger */
213614d5 334 spin_unlock(&sbi->fs_lock);
4b1ae27a
AV
335
336 if (oz_mode)
337 return 1;
bcdc5e01 338
06a35985
IK
339 /*
340 * If the directory has gone away due to an expire
341 * we have been called as ->d_revalidate() and so
342 * we need to return false and proceed to ->lookup().
343 */
344 if (autofs4_expire_wait(dentry) == -EAGAIN)
345 return 0;
346
bcdc5e01
IK
347 /*
348 * A zero status is success otherwise we have a
349 * negative error code.
350 */
4b1ae27a 351 status = try_to_fill_dentry(dentry, flags);
bcdc5e01 352 if (status == 0)
f50b6f86
IK
353 return 1;
354
bcdc5e01 355 return status;
1da177e4 356 }
4b1ae27a
AV
357 spin_unlock(&sbi->fs_lock);
358
359 /* Negative dentry.. invalidate if "old" */
360 if (dentry->d_inode == NULL)
361 return 0;
1da177e4
LT
362
363 /* Check for a non-mountpoint directory with no contents */
b5c84bf6 364 spin_lock(&autofs4_lock);
2fd6b7f5 365 spin_lock(&dentry->d_lock);
1da177e4 366 if (S_ISDIR(dentry->d_inode->i_mode) &&
c42c7f7e 367 !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
1da177e4
LT
368 DPRINTK("dentry=%p %.*s, emptydir",
369 dentry, dentry->d_name.len, dentry->d_name.name);
2fd6b7f5 370 spin_unlock(&dentry->d_lock);
b5c84bf6 371 spin_unlock(&autofs4_lock);
97e7449a 372
4b1ae27a
AV
373 /* The daemon never causes a mount to trigger */
374 if (oz_mode)
375 return 1;
213614d5 376
4b1ae27a
AV
377 /*
378 * A zero status is success otherwise we have a
379 * negative error code.
380 */
381 status = try_to_fill_dentry(dentry, flags);
382 if (status == 0)
383 return 1;
213614d5 384
4b1ae27a 385 return status;
1da177e4 386 }
2fd6b7f5 387 spin_unlock(&dentry->d_lock);
b5c84bf6 388 spin_unlock(&autofs4_lock);
1da177e4 389
1da177e4
LT
390 return 1;
391}
392
34ca959c 393void autofs4_dentry_release(struct dentry *de)
1da177e4
LT
394{
395 struct autofs_info *inf;
396
397 DPRINTK("releasing %p", de);
398
399 inf = autofs4_dentry_ino(de);
400 de->d_fsdata = NULL;
401
402 if (inf) {
f50b6f86
IK
403 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
404
f50b6f86 405 if (sbi) {
5f6f4f28 406 spin_lock(&sbi->lookup_lock);
25767378
IK
407 if (!list_empty(&inf->active))
408 list_del(&inf->active);
5f6f4f28
IK
409 if (!list_empty(&inf->expiring))
410 list_del(&inf->expiring);
411 spin_unlock(&sbi->lookup_lock);
f50b6f86
IK
412 }
413
c3724b12
JM
414 inf->dentry = NULL;
415 inf->inode = NULL;
416
1da177e4
LT
417 autofs4_free_ino(inf);
418 }
419}
420
421/* For dentries of directories in the root dir */
08f11513 422static const struct dentry_operations autofs4_root_dentry_operations = {
1da177e4
LT
423 .d_revalidate = autofs4_revalidate,
424 .d_release = autofs4_dentry_release,
425};
426
427/* For other dentries */
08f11513 428static const struct dentry_operations autofs4_dentry_operations = {
1da177e4
LT
429 .d_revalidate = autofs4_revalidate,
430 .d_release = autofs4_dentry_release,
431};
432
6510c9d8 433static struct dentry *autofs4_lookup_active(struct dentry *dentry)
25767378 434{
6510c9d8
IK
435 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
436 struct dentry *parent = dentry->d_parent;
437 struct qstr *name = &dentry->d_name;
25767378
IK
438 unsigned int len = name->len;
439 unsigned int hash = name->hash;
440 const unsigned char *str = name->name;
441 struct list_head *p, *head;
442
b5c84bf6 443 spin_lock(&autofs4_lock);
25767378
IK
444 spin_lock(&sbi->lookup_lock);
445 head = &sbi->active_list;
446 list_for_each(p, head) {
447 struct autofs_info *ino;
e4d5ade7 448 struct dentry *active;
25767378
IK
449 struct qstr *qstr;
450
451 ino = list_entry(p, struct autofs_info, active);
e4d5ade7 452 active = ino->dentry;
25767378 453
e4d5ade7 454 spin_lock(&active->d_lock);
25767378
IK
455
456 /* Already gone? */
b7ab39f6 457 if (active->d_count == 0)
25767378
IK
458 goto next;
459
e4d5ade7 460 qstr = &active->d_name;
25767378 461
e4d5ade7 462 if (active->d_name.hash != hash)
25767378 463 goto next;
e4d5ade7 464 if (active->d_parent != parent)
25767378
IK
465 goto next;
466
467 if (qstr->len != len)
468 goto next;
469 if (memcmp(qstr->name, str, len))
470 goto next;
471
4b1ae27a 472 if (d_unhashed(active)) {
b7ab39f6 473 dget_dlock(active);
4b1ae27a
AV
474 spin_unlock(&active->d_lock);
475 spin_unlock(&sbi->lookup_lock);
b5c84bf6 476 spin_unlock(&autofs4_lock);
4b1ae27a
AV
477 return active;
478 }
25767378 479next:
e4d5ade7 480 spin_unlock(&active->d_lock);
25767378
IK
481 }
482 spin_unlock(&sbi->lookup_lock);
b5c84bf6 483 spin_unlock(&autofs4_lock);
25767378
IK
484
485 return NULL;
486}
487
6510c9d8 488static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
f50b6f86 489{
6510c9d8
IK
490 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
491 struct dentry *parent = dentry->d_parent;
492 struct qstr *name = &dentry->d_name;
f50b6f86
IK
493 unsigned int len = name->len;
494 unsigned int hash = name->hash;
495 const unsigned char *str = name->name;
496 struct list_head *p, *head;
497
b5c84bf6 498 spin_lock(&autofs4_lock);
5f6f4f28
IK
499 spin_lock(&sbi->lookup_lock);
500 head = &sbi->expiring_list;
f50b6f86
IK
501 list_for_each(p, head) {
502 struct autofs_info *ino;
cb4b492a 503 struct dentry *expiring;
f50b6f86
IK
504 struct qstr *qstr;
505
5f6f4f28 506 ino = list_entry(p, struct autofs_info, expiring);
cb4b492a 507 expiring = ino->dentry;
f50b6f86 508
cb4b492a 509 spin_lock(&expiring->d_lock);
f50b6f86
IK
510
511 /* Bad luck, we've already been dentry_iput */
cb4b492a 512 if (!expiring->d_inode)
f50b6f86
IK
513 goto next;
514
cb4b492a 515 qstr = &expiring->d_name;
f50b6f86 516
cb4b492a 517 if (expiring->d_name.hash != hash)
f50b6f86 518 goto next;
cb4b492a 519 if (expiring->d_parent != parent)
f50b6f86
IK
520 goto next;
521
522 if (qstr->len != len)
523 goto next;
524 if (memcmp(qstr->name, str, len))
525 goto next;
526
4b1ae27a 527 if (d_unhashed(expiring)) {
b7ab39f6 528 dget_dlock(expiring);
4b1ae27a
AV
529 spin_unlock(&expiring->d_lock);
530 spin_unlock(&sbi->lookup_lock);
b5c84bf6 531 spin_unlock(&autofs4_lock);
4b1ae27a
AV
532 return expiring;
533 }
f50b6f86 534next:
cb4b492a 535 spin_unlock(&expiring->d_lock);
f50b6f86 536 }
5f6f4f28 537 spin_unlock(&sbi->lookup_lock);
b5c84bf6 538 spin_unlock(&autofs4_lock);
f50b6f86
IK
539
540 return NULL;
541}
542
1da177e4
LT
543/* Lookups in the root directory */
544static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
545{
546 struct autofs_sb_info *sbi;
25767378 547 struct autofs_info *ino;
90387c9c 548 struct dentry *expiring, *active;
1da177e4
LT
549 int oz_mode;
550
551 DPRINTK("name = %.*s",
552 dentry->d_name.len, dentry->d_name.name);
553
718c604a 554 /* File name too long to exist */
1da177e4 555 if (dentry->d_name.len > NAME_MAX)
718c604a 556 return ERR_PTR(-ENAMETOOLONG);
1da177e4
LT
557
558 sbi = autofs4_sbi(dir->i_sb);
1da177e4 559 oz_mode = autofs4_oz_mode(sbi);
718c604a 560
1da177e4 561 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
a47afb0f 562 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
1da177e4 563
6510c9d8 564 active = autofs4_lookup_active(dentry);
90387c9c
IK
565 if (active) {
566 dentry = active;
aa952eb2
IK
567 ino = autofs4_dentry_ino(dentry);
568 } else {
4b1ae27a
AV
569 /*
570 * Mark the dentry incomplete but don't hash it. We do this
571 * to serialize our inode creation operations (symlink and
572 * mkdir) which prevents deadlock during the callback to
573 * the daemon. Subsequent user space lookups for the same
574 * dentry are placed on the wait queue while the daemon
575 * itself is allowed passage unresticted so the create
576 * operation itself can then hash the dentry. Finally,
577 * we check for the hashed dentry and return the newly
578 * hashed dentry.
579 */
fb045adb 580 d_set_d_op(dentry, &autofs4_root_dentry_operations);
4b1ae27a
AV
581
582 /*
583 * And we need to ensure that the same dentry is used for
584 * all following lookup calls until it is hashed so that
585 * the dentry flags are persistent throughout the request.
586 */
587 ino = autofs4_init_ino(NULL, sbi, 0555);
588 if (!ino)
589 return ERR_PTR(-ENOMEM);
590
591 dentry->d_fsdata = ino;
592 ino->dentry = dentry;
5f6f4f28 593
4b1ae27a
AV
594 autofs4_add_active(dentry);
595
596 d_instantiate(dentry, NULL);
597 }
213614d5 598
1da177e4 599 if (!oz_mode) {
213614d5 600 mutex_unlock(&dir->i_mutex);
4b1ae27a 601 expiring = autofs4_lookup_expiring(dentry);
8f63aaa8
IK
602 if (expiring) {
603 /*
604 * If we are racing with expire the request might not
605 * be quite complete but the directory has been removed
606 * so it must have been successful, so just wait for it.
607 */
8f63aaa8 608 autofs4_expire_wait(expiring);
4b1ae27a 609 autofs4_del_expiring(expiring);
8f63aaa8
IK
610 dput(expiring);
611 }
4b1ae27a 612
aa952eb2 613 spin_lock(&sbi->fs_lock);
4b1ae27a 614 ino->flags |= AUTOFS_INF_PENDING;
aa952eb2 615 spin_unlock(&sbi->fs_lock);
4b1ae27a
AV
616 if (dentry->d_op && dentry->d_op->d_revalidate)
617 (dentry->d_op->d_revalidate)(dentry, nd);
618 mutex_lock(&dir->i_mutex);
1da177e4
LT
619 }
620
621 /*
4b1ae27a 622 * If we are still pending, check if we had to handle
1da177e4
LT
623 * a signal. If so we can force a restart..
624 */
4b1ae27a 625 if (ino->flags & AUTOFS_INF_PENDING) {
1da177e4
LT
626 /* See if we were interrupted */
627 if (signal_pending(current)) {
628 sigset_t *sigset = &current->pending.signal;
629 if (sigismember (sigset, SIGKILL) ||
630 sigismember (sigset, SIGQUIT) ||
631 sigismember (sigset, SIGINT)) {
90387c9c
IK
632 if (active)
633 dput(active);
1da177e4
LT
634 return ERR_PTR(-ERESTARTNOINTR);
635 }
636 }
4b1ae27a
AV
637 if (!oz_mode) {
638 spin_lock(&sbi->fs_lock);
639 ino->flags &= ~AUTOFS_INF_PENDING;
640 spin_unlock(&sbi->fs_lock);
213614d5
IK
641 }
642 }
c9ffec48 643
213614d5 644 /*
4b1ae27a
AV
645 * If this dentry is unhashed, then we shouldn't honour this
646 * lookup. Returning ENOENT here doesn't do the right thing
647 * for all system calls, but it should be OK for the operations
648 * we permit from an autofs.
213614d5 649 */
4b1ae27a 650 if (!oz_mode && d_unhashed(dentry)) {
213614d5 651 /*
4b1ae27a
AV
652 * A user space application can (and has done in the past)
653 * remove and re-create this directory during the callback.
654 * This can leave us with an unhashed dentry, but a
655 * successful mount! So we need to perform another
656 * cached lookup in case the dentry now exists.
213614d5 657 */
4b1ae27a
AV
658 struct dentry *parent = dentry->d_parent;
659 struct dentry *new = d_lookup(parent, &dentry->d_name);
660 if (new != NULL)
661 dentry = new;
662 else
663 dentry = ERR_PTR(-ENOENT);
664
213614d5 665 if (active)
4b1ae27a
AV
666 dput(active);
667
668 return dentry;
f50b6f86
IK
669 }
670
4b1ae27a
AV
671 if (active)
672 return active;
673
1da177e4
LT
674 return NULL;
675}
676
677static int autofs4_dir_symlink(struct inode *dir,
678 struct dentry *dentry,
679 const char *symname)
680{
681 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
682 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1aff3c8b 683 struct autofs_info *p_ino;
1da177e4
LT
684 struct inode *inode;
685 char *cp;
686
687 DPRINTK("%s <- %.*s", symname,
688 dentry->d_name.len, dentry->d_name.name);
689
690 if (!autofs4_oz_mode(sbi))
691 return -EACCES;
692
693 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
25767378
IK
694 if (!ino)
695 return -ENOMEM;
1da177e4 696
4b1ae27a
AV
697 autofs4_del_active(dentry);
698
ef581a74 699 ino->size = strlen(symname);
25767378
IK
700 cp = kmalloc(ino->size + 1, GFP_KERNEL);
701 if (!cp) {
702 if (!dentry->d_fsdata)
703 kfree(ino);
704 return -ENOMEM;
1da177e4
LT
705 }
706
707 strcpy(cp, symname);
708
709 inode = autofs4_get_inode(dir->i_sb, ino);
25767378
IK
710 if (!inode) {
711 kfree(cp);
712 if (!dentry->d_fsdata)
713 kfree(ino);
714 return -ENOMEM;
715 }
1864f7bd 716 d_add(dentry, inode);
1da177e4
LT
717
718 if (dir == dir->i_sb->s_root->d_inode)
fb045adb 719 d_set_d_op(dentry, &autofs4_root_dentry_operations);
1da177e4 720 else
fb045adb 721 d_set_d_op(dentry, &autofs4_dentry_operations);
1da177e4
LT
722
723 dentry->d_fsdata = ino;
724 ino->dentry = dget(dentry);
1aff3c8b
IK
725 atomic_inc(&ino->count);
726 p_ino = autofs4_dentry_ino(dentry->d_parent);
727 if (p_ino && dentry->d_parent != dentry)
728 atomic_inc(&p_ino->count);
1da177e4
LT
729 ino->inode = inode;
730
25767378 731 ino->u.symlink = cp;
1da177e4
LT
732 dir->i_mtime = CURRENT_TIME;
733
734 return 0;
735}
736
737/*
738 * NOTE!
739 *
740 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
741 * that the file no longer exists. However, doing that means that the
742 * VFS layer can turn the dentry into a negative dentry. We don't want
f50b6f86 743 * this, because the unlink is probably the result of an expire.
5f6f4f28
IK
744 * We simply d_drop it and add it to a expiring list in the super block,
745 * which allows the dentry lookup to check for an incomplete expire.
1da177e4
LT
746 *
747 * If a process is blocked on the dentry waiting for the expire to finish,
748 * it will invalidate the dentry and try to mount with a new one.
749 *
750 * Also see autofs4_dir_rmdir()..
751 */
752static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
753{
754 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
755 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1aff3c8b 756 struct autofs_info *p_ino;
1da177e4
LT
757
758 /* This allows root to remove symlinks */
d78e53c8 759 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
1da177e4
LT
760 return -EACCES;
761
1aff3c8b
IK
762 if (atomic_dec_and_test(&ino->count)) {
763 p_ino = autofs4_dentry_ino(dentry->d_parent);
764 if (p_ino && dentry->d_parent != dentry)
765 atomic_dec(&p_ino->count);
766 }
1da177e4
LT
767 dput(ino->dentry);
768
769 dentry->d_inode->i_size = 0;
ce71ec36 770 clear_nlink(dentry->d_inode);
1da177e4
LT
771
772 dir->i_mtime = CURRENT_TIME;
773
b5c84bf6 774 spin_lock(&autofs4_lock);
4b1ae27a 775 autofs4_add_expiring(dentry);
f50b6f86
IK
776 spin_lock(&dentry->d_lock);
777 __d_drop(dentry);
778 spin_unlock(&dentry->d_lock);
b5c84bf6 779 spin_unlock(&autofs4_lock);
1da177e4
LT
780
781 return 0;
782}
783
784static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
785{
786 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
787 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1aff3c8b 788 struct autofs_info *p_ino;
1da177e4 789
f50b6f86
IK
790 DPRINTK("dentry %p, removing %.*s",
791 dentry, dentry->d_name.len, dentry->d_name.name);
792
1da177e4
LT
793 if (!autofs4_oz_mode(sbi))
794 return -EACCES;
795
b5c84bf6 796 spin_lock(&autofs4_lock);
2fd6b7f5
NP
797 spin_lock(&sbi->lookup_lock);
798 spin_lock(&dentry->d_lock);
1da177e4 799 if (!list_empty(&dentry->d_subdirs)) {
2fd6b7f5
NP
800 spin_unlock(&dentry->d_lock);
801 spin_unlock(&sbi->lookup_lock);
b5c84bf6 802 spin_unlock(&autofs4_lock);
1da177e4
LT
803 return -ENOTEMPTY;
804 }
2fd6b7f5
NP
805 __autofs4_add_expiring(dentry);
806 spin_unlock(&sbi->lookup_lock);
1da177e4
LT
807 __d_drop(dentry);
808 spin_unlock(&dentry->d_lock);
b5c84bf6 809 spin_unlock(&autofs4_lock);
1da177e4 810
1aff3c8b
IK
811 if (atomic_dec_and_test(&ino->count)) {
812 p_ino = autofs4_dentry_ino(dentry->d_parent);
813 if (p_ino && dentry->d_parent != dentry)
814 atomic_dec(&p_ino->count);
815 }
1da177e4 816 dput(ino->dentry);
1da177e4 817 dentry->d_inode->i_size = 0;
ce71ec36 818 clear_nlink(dentry->d_inode);
1da177e4
LT
819
820 if (dir->i_nlink)
9a53c3a7 821 drop_nlink(dir);
1da177e4
LT
822
823 return 0;
824}
825
826static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
827{
828 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
829 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1aff3c8b 830 struct autofs_info *p_ino;
1da177e4
LT
831 struct inode *inode;
832
d78e53c8 833 if (!autofs4_oz_mode(sbi))
1da177e4
LT
834 return -EACCES;
835
836 DPRINTK("dentry %p, creating %.*s",
837 dentry, dentry->d_name.len, dentry->d_name.name);
838
839 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
25767378
IK
840 if (!ino)
841 return -ENOMEM;
842
4b1ae27a
AV
843 autofs4_del_active(dentry);
844
1da177e4 845 inode = autofs4_get_inode(dir->i_sb, ino);
25767378
IK
846 if (!inode) {
847 if (!dentry->d_fsdata)
848 kfree(ino);
849 return -ENOMEM;
850 }
1864f7bd 851 d_add(dentry, inode);
1da177e4
LT
852
853 if (dir == dir->i_sb->s_root->d_inode)
fb045adb 854 d_set_d_op(dentry, &autofs4_root_dentry_operations);
1da177e4 855 else
fb045adb 856 d_set_d_op(dentry, &autofs4_dentry_operations);
1da177e4
LT
857
858 dentry->d_fsdata = ino;
859 ino->dentry = dget(dentry);
1aff3c8b
IK
860 atomic_inc(&ino->count);
861 p_ino = autofs4_dentry_ino(dentry->d_parent);
862 if (p_ino && dentry->d_parent != dentry)
863 atomic_inc(&p_ino->count);
1da177e4 864 ino->inode = inode;
d8c76e6f 865 inc_nlink(dir);
1da177e4
LT
866 dir->i_mtime = CURRENT_TIME;
867
868 return 0;
869}
870
871/* Get/set timeout ioctl() operation */
c9243f5b
AB
872#ifdef CONFIG_COMPAT
873static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
874 compat_ulong_t __user *p)
875{
876 int rv;
877 unsigned long ntimeout;
878
879 if ((rv = get_user(ntimeout, p)) ||
880 (rv = put_user(sbi->exp_timeout/HZ, p)))
881 return rv;
882
883 if (ntimeout > UINT_MAX/HZ)
884 sbi->exp_timeout = 0;
885 else
886 sbi->exp_timeout = ntimeout * HZ;
887
888 return 0;
889}
890#endif
891
1da177e4
LT
892static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
893 unsigned long __user *p)
894{
895 int rv;
896 unsigned long ntimeout;
897
d78e53c8
SB
898 if ((rv = get_user(ntimeout, p)) ||
899 (rv = put_user(sbi->exp_timeout/HZ, p)))
1da177e4
LT
900 return rv;
901
d78e53c8 902 if (ntimeout > ULONG_MAX/HZ)
1da177e4
LT
903 sbi->exp_timeout = 0;
904 else
905 sbi->exp_timeout = ntimeout * HZ;
906
907 return 0;
908}
909
910/* Return protocol version */
911static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
912{
913 return put_user(sbi->version, p);
914}
915
916/* Return protocol sub version */
917static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
918{
919 return put_user(sbi->sub_version, p);
920}
921
1da177e4
LT
922/*
923* Tells the daemon whether it can umount the autofs mount.
924*/
925static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
926{
927 int status = 0;
928
e3474a8e 929 if (may_umount(mnt))
1da177e4
LT
930 status = 1;
931
932 DPRINTK("returning %d", status);
933
934 status = put_user(status, p);
935
936 return status;
937}
938
939/* Identify autofs4_dentries - this is so we can tell if there's
940 an extra dentry refcount or not. We only hold a refcount on the
941 dentry if its non-negative (ie, d_inode != NULL)
942*/
943int is_autofs4_dentry(struct dentry *dentry)
944{
945 return dentry && dentry->d_inode &&
946 (dentry->d_op == &autofs4_root_dentry_operations ||
947 dentry->d_op == &autofs4_dentry_operations) &&
948 dentry->d_fsdata != NULL;
949}
950
951/*
952 * ioctl()'s on the root directory is the chief method for the daemon to
953 * generate kernel reactions
954 */
3663df70
FW
955static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
956 unsigned int cmd, unsigned long arg)
1da177e4
LT
957{
958 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
959 void __user *p = (void __user *)arg;
960
961 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
a47afb0f 962 cmd,arg,sbi,task_pgrp_nr(current));
1da177e4 963
d78e53c8
SB
964 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
965 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
1da177e4
LT
966 return -ENOTTY;
967
d78e53c8 968 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
1da177e4
LT
969 return -EPERM;
970
971 switch(cmd) {
972 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
973 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
974 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
975 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
976 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
977 autofs4_catatonic_mode(sbi);
978 return 0;
979 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
980 return autofs4_get_protover(sbi, p);
981 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
982 return autofs4_get_protosubver(sbi, p);
983 case AUTOFS_IOC_SETTIMEOUT:
984 return autofs4_get_set_timeout(sbi, p);
c9243f5b
AB
985#ifdef CONFIG_COMPAT
986 case AUTOFS_IOC_SETTIMEOUT32:
987 return autofs4_compat_get_set_timeout(sbi, p);
988#endif
1da177e4 989
1da177e4 990 case AUTOFS_IOC_ASKUMOUNT:
a4669ed8 991 return autofs4_ask_umount(filp->f_path.mnt, p);
1da177e4
LT
992
993 /* return a single thing to expire */
994 case AUTOFS_IOC_EXPIRE:
a4669ed8 995 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
1da177e4
LT
996 /* same as above, but can send multiple expires through pipe */
997 case AUTOFS_IOC_EXPIRE_MULTI:
a4669ed8 998 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
1da177e4
LT
999
1000 default:
1001 return -ENOSYS;
1002 }
1003}
3663df70
FW
1004
1005static long autofs4_root_ioctl(struct file *filp,
1006 unsigned int cmd, unsigned long arg)
1007{
3663df70 1008 struct inode *inode = filp->f_dentry->d_inode;
de47de74 1009 return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
3663df70 1010}
c9243f5b
AB
1011
1012#ifdef CONFIG_COMPAT
1013static long autofs4_root_compat_ioctl(struct file *filp,
1014 unsigned int cmd, unsigned long arg)
1015{
1016 struct inode *inode = filp->f_path.dentry->d_inode;
1017 int ret;
1018
c9243f5b
AB
1019 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
1020 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
1021 else
1022 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
1023 (unsigned long)compat_ptr(arg));
c9243f5b
AB
1024
1025 return ret;
1026}
1027#endif
This page took 0.553429 seconds and 5 git commands to generate.