root.c 28.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/* -*- c -*- --------------------------------------------------------------- *
 *
 * linux/fs/autofs/root.c
 *
 *  Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
 *  Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7
 *  Copyright 2001-2006 Ian Kent <raven@themaw.net>
L
Linus Torvalds 已提交
8 9 10 11 12 13 14
 *
 * This file is part of the Linux kernel and is made available under
 * the terms of the GNU General Public License, version 2, or at your
 * option, any later version, incorporated herein by reference.
 *
 * ------------------------------------------------------------------------- */

15
#include <linux/capability.h>
L
Linus Torvalds 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28
#include <linux/errno.h>
#include <linux/stat.h>
#include <linux/param.h>
#include <linux/time.h>
#include "autofs_i.h"

static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
static int autofs4_dir_unlink(struct inode *,struct dentry *);
static int autofs4_dir_rmdir(struct inode *,struct dentry *);
static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
static int autofs4_dir_open(struct inode *inode, struct file *file);
static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
29
static void *autofs4_follow_link(struct dentry *, struct nameidata *);
L
Linus Torvalds 已提交
30

31 32 33
#define TRIGGER_FLAGS   (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
#define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)

34
const struct file_operations autofs4_root_operations = {
L
Linus Torvalds 已提交
35 36 37
	.open		= dcache_dir_open,
	.release	= dcache_dir_close,
	.read		= generic_read_dir,
I
Ian Kent 已提交
38
	.readdir	= dcache_readdir,
39
	.llseek		= dcache_dir_lseek,
L
Linus Torvalds 已提交
40 41 42
	.ioctl		= autofs4_root_ioctl,
};

43
const struct file_operations autofs4_dir_operations = {
L
Linus Torvalds 已提交
44
	.open		= autofs4_dir_open,
45
	.release	= dcache_dir_close,
L
Linus Torvalds 已提交
46
	.read		= generic_read_dir,
47
	.readdir	= dcache_readdir,
48
	.llseek		= dcache_dir_lseek,
L
Linus Torvalds 已提交
49 50
};

51
const struct inode_operations autofs4_indirect_root_inode_operations = {
L
Linus Torvalds 已提交
52 53 54 55 56 57 58
	.lookup		= autofs4_lookup,
	.unlink		= autofs4_dir_unlink,
	.symlink	= autofs4_dir_symlink,
	.mkdir		= autofs4_dir_mkdir,
	.rmdir		= autofs4_dir_rmdir,
};

59
const struct inode_operations autofs4_direct_root_inode_operations = {
60
	.lookup		= autofs4_lookup,
61 62 63
	.unlink		= autofs4_dir_unlink,
	.mkdir		= autofs4_dir_mkdir,
	.rmdir		= autofs4_dir_rmdir,
64 65 66
	.follow_link	= autofs4_follow_link,
};

67
const struct inode_operations autofs4_dir_inode_operations = {
L
Linus Torvalds 已提交
68 69 70 71 72 73 74
	.lookup		= autofs4_lookup,
	.unlink		= autofs4_dir_unlink,
	.symlink	= autofs4_dir_symlink,
	.mkdir		= autofs4_dir_mkdir,
	.rmdir		= autofs4_dir_rmdir,
};

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
static void autofs4_add_active(struct dentry *dentry)
{
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
	if (ino) {
		spin_lock(&sbi->lookup_lock);
		if (!ino->active_count) {
			if (list_empty(&ino->active))
				list_add(&ino->active, &sbi->active_list);
		}
		ino->active_count++;
		spin_unlock(&sbi->lookup_lock);
	}
	return;
}

static void autofs4_del_active(struct dentry *dentry)
{
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
	if (ino) {
		spin_lock(&sbi->lookup_lock);
		ino->active_count--;
		if (!ino->active_count) {
			if (!list_empty(&ino->active))
				list_del_init(&ino->active);
		}
		spin_unlock(&sbi->lookup_lock);
	}
	return;
}

I
Ian Kent 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
static void autofs4_add_rehash_entry(struct autofs_info *ino,
				     struct rehash_entry *entry)
{
	entry->task = current;
	INIT_LIST_HEAD(&entry->list);
	list_add(&entry->list, &ino->rehash_list);
	return;
}

static void autofs4_remove_rehash_entry(struct autofs_info *ino)
{
	struct list_head *head = &ino->rehash_list;
	struct rehash_entry *entry;
	list_for_each_entry(entry, head, list) {
		if (entry->task == current) {
			list_del(&entry->list);
			kfree(entry);
			break;
		}
	}
	return;
}

static void autofs4_remove_rehash_entrys(struct autofs_info *ino)
{
	struct autofs_sb_info *sbi = ino->sbi;
	struct rehash_entry *entry, *next;
	struct list_head *head;

	spin_lock(&sbi->fs_lock);
	spin_lock(&sbi->lookup_lock);
	if (!(ino->flags & AUTOFS_INF_REHASH)) {
		spin_unlock(&sbi->lookup_lock);
		spin_unlock(&sbi->fs_lock);
		return;
	}
	ino->flags &= ~AUTOFS_INF_REHASH;
	head = &ino->rehash_list;
	list_for_each_entry_safe(entry, next, head, list) {
		list_del(&entry->list);
		kfree(entry);
	}
	spin_unlock(&sbi->lookup_lock);
	spin_unlock(&sbi->fs_lock);
	dput(ino->dentry);

	return;
}

static void autofs4_revalidate_drop(struct dentry *dentry,
				    struct rehash_entry *entry)
{
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
	/*
	 * Add to the active list so we can pick this up in
	 * ->lookup(). Also add an entry to a rehash list so
	 * we know when there are no dentrys in flight so we
	 * know when we can rehash the dentry.
	 */
	spin_lock(&sbi->lookup_lock);
	if (list_empty(&ino->active))
		list_add(&ino->active, &sbi->active_list);
	autofs4_add_rehash_entry(ino, entry);
	spin_unlock(&sbi->lookup_lock);
	if (!(ino->flags & AUTOFS_INF_REHASH)) {
		ino->flags |= AUTOFS_INF_REHASH;
		dget(dentry);
		spin_lock(&dentry->d_lock);
		__d_drop(dentry);
		spin_unlock(&dentry->d_lock);
	}
	return;
}

static void autofs4_revalidate_rehash(struct dentry *dentry)
{
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
	if (ino->flags & AUTOFS_INF_REHASH) {
		spin_lock(&sbi->lookup_lock);
		autofs4_remove_rehash_entry(ino);
		if (list_empty(&ino->rehash_list)) {
			spin_unlock(&sbi->lookup_lock);
			ino->flags &= ~AUTOFS_INF_REHASH;
			d_rehash(dentry);
			dput(ino->dentry);
		} else
			spin_unlock(&sbi->lookup_lock);
	}
	return;
}

200 201 202 203 204 205 206 207
static unsigned int autofs4_need_mount(unsigned int flags)
{
	unsigned int res = 0;
	if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
		res = 1;
	return res;
}

L
Linus Torvalds 已提交
208 209
static int autofs4_dir_open(struct inode *inode, struct file *file)
{
210
	struct dentry *dentry = file->f_path.dentry;
L
Linus Torvalds 已提交
211
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
212

L
Linus Torvalds 已提交
213 214 215 216 217 218
	DPRINTK("file=%p dentry=%p %.*s",
		file, dentry, dentry->d_name.len, dentry->d_name.name);

	if (autofs4_oz_mode(sbi))
		goto out;

219 220 221 222 223 224 225 226 227 228
	/*
	 * An empty directory in an autofs file system is always a
	 * mount point. The daemon must have failed to mount this
	 * during lookup so it doesn't exist. This can happen, for
	 * example, if user space returns an incorrect status for a
	 * mount request. Otherwise we're doing a readdir on the
	 * autofs file system so just let the libfs routines handle
	 * it.
	 */
	spin_lock(&dcache_lock);
229
	if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
L
Linus Torvalds 已提交
230
		spin_unlock(&dcache_lock);
231
		return -ENOENT;
L
Linus Torvalds 已提交
232
	}
233
	spin_unlock(&dcache_lock);
L
Linus Torvalds 已提交
234 235

out:
236
	return dcache_dir_open(inode, file);
L
Linus Torvalds 已提交
237 238
}

I
Ian Kent 已提交
239
static int try_to_fill_dentry(struct dentry *dentry)
L
Linus Torvalds 已提交
240
{
241
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
242
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
243
	int status;
L
Linus Torvalds 已提交
244 245 246 247

	DPRINTK("dentry=%p %.*s ino=%p",
		 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);

248 249 250 251
	/*
	 * Wait for a pending mount, triggering one if there
	 * isn't one already
	 */
I
Ian Kent 已提交
252 253
	DPRINTK("waiting for mount name=%.*s",
		 dentry->d_name.len, dentry->d_name.name);
L
Linus Torvalds 已提交
254

I
Ian Kent 已提交
255
	status = autofs4_wait(sbi, dentry, NFY_MOUNT);
256

I
Ian Kent 已提交
257
	DPRINTK("mount done status=%d", status);
L
Linus Torvalds 已提交
258

I
Ian Kent 已提交
259 260
	/* Update expiry counter */
	ino->last_used = jiffies;
L
Linus Torvalds 已提交
261

I
Ian Kent 已提交
262
	return status;
263 264 265 266 267 268
}

/* For autofs direct mounts the follow link triggers the mount */
static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
{
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
269
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
270 271 272 273 274 275 276
	int oz_mode = autofs4_oz_mode(sbi);
	unsigned int lookup_type;
	int status;

	DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
		dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
		nd->flags);
277 278
	/*
	 * For an expire of a covered direct or offset mount we need
A
Al Viro 已提交
279
	 * to break out of follow_down() at the autofs mount trigger
280 281 282 283 284 285 286 287
	 * (d_mounted--), so we can see the expiring flag, and manage
	 * the blocking and following here until the expire is completed.
	 */
	if (oz_mode) {
		spin_lock(&sbi->fs_lock);
		if (ino->flags & AUTOFS_INF_EXPIRING) {
			spin_unlock(&sbi->fs_lock);
			/* Follow down to our covering mount. */
A
Al Viro 已提交
288
			if (!follow_down(&nd->path))
289
				goto done;
290
			goto follow;
291 292
		}
		spin_unlock(&sbi->fs_lock);
293
		goto done;
294
	}
295

296
	/* If an expire request is pending everyone must wait. */
297
	autofs4_expire_wait(dentry);
298

299
	/* We trigger a mount for almost all flags */
300
	lookup_type = autofs4_need_mount(nd->flags);
301 302 303 304 305
	spin_lock(&sbi->fs_lock);
	spin_lock(&dcache_lock);
	if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
		spin_unlock(&dcache_lock);
		spin_unlock(&sbi->fs_lock);
306
		goto follow;
307
	}
308

309
	/*
310 311 312
	 * If the dentry contains directories then it is an autofs
	 * multi-mount with no root mount offset. So don't try to
	 * mount it again.
313
	 */
314
	if (ino->flags & AUTOFS_INF_PENDING ||
315
	    (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
I
Ian Kent 已提交
316
		ino->flags |= AUTOFS_INF_PENDING;
317
		spin_unlock(&dcache_lock);
318
		spin_unlock(&sbi->fs_lock);
319

I
Ian Kent 已提交
320 321 322 323 324 325
		status = try_to_fill_dentry(dentry);

		spin_lock(&sbi->fs_lock);
		ino->flags &= ~AUTOFS_INF_PENDING;
		spin_unlock(&sbi->fs_lock);

326 327 328
		if (status)
			goto out_error;

329
		goto follow;
330
	}
331
	spin_unlock(&dcache_lock);
332
	spin_unlock(&sbi->fs_lock);
333 334 335 336 337 338 339
follow:
	/*
	 * If there is no root mount it must be an autofs
	 * multi-mount with no root offset so we don't need
	 * to follow it.
	 */
	if (d_mountpoint(dentry)) {
A
Al Viro 已提交
340
		if (!autofs4_follow_mount(&nd->path)) {
341 342 343 344
			status = -ENOENT;
			goto out_error;
		}
	}
345 346 347 348 349

done:
	return NULL;

out_error:
J
Jan Blunck 已提交
350
	path_put(&nd->path);
351
	return ERR_PTR(status);
L
Linus Torvalds 已提交
352 353 354 355 356 357 358 359
}

/*
 * Revalidate is called on every cache lookup.  Some of those
 * cache lookups may actually happen while the dentry is not
 * yet completely filled in, and revalidate has to delay such
 * lookups..
 */
360
static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
L
Linus Torvalds 已提交
361
{
362
	struct inode *dir = dentry->d_parent->d_inode;
L
Linus Torvalds 已提交
363
	struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
I
Ian Kent 已提交
364 365
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
	struct rehash_entry *entry;
L
Linus Torvalds 已提交
366
	int flags = nd ? nd->flags : 0;
I
Ian Kent 已提交
367 368 369 370 371 372 373 374 375 376 377 378 379 380
	unsigned int mutex_aquired;

	DPRINTK("name = %.*s oz_mode = %d",
		dentry->d_name.len, dentry->d_name.name, oz_mode);

	/* Daemon never causes a mount to trigger */
	if (autofs4_oz_mode(sbi))
		return 1;

	entry = kmalloc(sizeof(struct rehash_entry), GFP_KERNEL);
	if (!entry)
		return -ENOMEM;

	mutex_aquired = mutex_trylock(&dir->i_mutex);
L
Linus Torvalds 已提交
381

382
	spin_lock(&sbi->fs_lock);
I
Ian Kent 已提交
383 384
	spin_lock(&dcache_lock);
	/* Pending dentry */
L
Linus Torvalds 已提交
385
	if (autofs4_ispending(dentry)) {
I
Ian Kent 已提交
386
		int status;
387

I
Ian Kent 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
		/*
		 * We can only unhash and send this to ->lookup() if
		 * the directory mutex is held over d_revalidate() and
		 * ->lookup(). This prevents the VFS from incorrectly
		 * seeing the dentry as non-existent.
		 */
		ino->flags |= AUTOFS_INF_PENDING;
		if (!mutex_aquired) {
			autofs4_revalidate_drop(dentry, entry);
			spin_unlock(&dcache_lock);
			spin_unlock(&sbi->fs_lock);
			return 0;
		}
		spin_unlock(&dcache_lock);
		spin_unlock(&sbi->fs_lock);
		mutex_unlock(&dir->i_mutex);
		kfree(entry);
405

406 407 408 409 410 411 412 413
		/*
		 * If the directory has gone away due to an expire
		 * we have been called as ->d_revalidate() and so
		 * we need to return false and proceed to ->lookup().
		 */
		if (autofs4_expire_wait(dentry) == -EAGAIN)
			return 0;

414 415 416 417
		/*
		 * A zero status is success otherwise we have a
		 * negative error code.
		 */
I
Ian Kent 已提交
418 419 420 421 422 423
		status = try_to_fill_dentry(dentry);

		spin_lock(&sbi->fs_lock);
		ino->flags &= ~AUTOFS_INF_PENDING;
		spin_unlock(&sbi->fs_lock);

424
		if (status == 0)
425 426
			return 1;

427
		return status;
L
Linus Torvalds 已提交
428 429 430 431
	}

	/* Check for a non-mountpoint directory with no contents */
	if (S_ISDIR(dentry->d_inode->i_mode) &&
432
	    !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
L
Linus Torvalds 已提交
433 434
		DPRINTK("dentry=%p %.*s, emptydir",
			 dentry, dentry->d_name.len, dentry->d_name.name);
435

I
Ian Kent 已提交
436 437
		if (autofs4_need_mount(flags) || current->link_count) {
			int status;
438

I
Ian Kent 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
			/*
			 * We can only unhash and send this to ->lookup() if
			 * the directory mutex is held over d_revalidate() and
			 * ->lookup(). This prevents the VFS from incorrectly
			 * seeing the dentry as non-existent.
			 */
			ino->flags |= AUTOFS_INF_PENDING;
			if (!mutex_aquired) {
				autofs4_revalidate_drop(dentry, entry);
				spin_unlock(&dcache_lock);
				spin_unlock(&sbi->fs_lock);
				return 0;
			}
			spin_unlock(&dcache_lock);
			spin_unlock(&sbi->fs_lock);
			mutex_unlock(&dir->i_mutex);
			kfree(entry);
456

I
Ian Kent 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
			/*
			 * A zero status is success otherwise we have a
			 * negative error code.
			 */
			status = try_to_fill_dentry(dentry);

			spin_lock(&sbi->fs_lock);
			ino->flags &= ~AUTOFS_INF_PENDING;
			spin_unlock(&sbi->fs_lock);

			if (status == 0)
				return 1;

			return status;
		}
L
Linus Torvalds 已提交
472 473
	}
	spin_unlock(&dcache_lock);
I
Ian Kent 已提交
474 475 476 477 478 479
	spin_unlock(&sbi->fs_lock);

	if (mutex_aquired)
		mutex_unlock(&dir->i_mutex);

	kfree(entry);
L
Linus Torvalds 已提交
480 481 482 483

	return 1;
}

I
Ian Kent 已提交
484 485 486 487 488 489 490 491 492 493
static void autofs4_free_rehash_entrys(struct autofs_info *inf)
{
	struct list_head *head = &inf->rehash_list;
	struct rehash_entry *entry, *next;
	list_for_each_entry_safe(entry, next, head, list) {
		list_del(&entry->list);
		kfree(entry);
	}
}

494
void autofs4_dentry_release(struct dentry *de)
L
Linus Torvalds 已提交
495 496 497 498 499 500 501 502 503
{
	struct autofs_info *inf;

	DPRINTK("releasing %p", de);

	inf = autofs4_dentry_ino(de);
	de->d_fsdata = NULL;

	if (inf) {
504 505 506
		struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);

		if (sbi) {
507
			spin_lock(&sbi->lookup_lock);
508 509
			if (!list_empty(&inf->active))
				list_del(&inf->active);
510 511
			if (!list_empty(&inf->expiring))
				list_del(&inf->expiring);
I
Ian Kent 已提交
512 513
			if (!list_empty(&inf->rehash_list))
				autofs4_free_rehash_entrys(inf);
514
			spin_unlock(&sbi->lookup_lock);
515 516
		}

517 518 519
		inf->dentry = NULL;
		inf->inode = NULL;

L
Linus Torvalds 已提交
520 521 522 523 524
		autofs4_free_ino(inf);
	}
}

/* For dentries of directories in the root dir */
525
static const struct dentry_operations autofs4_root_dentry_operations = {
L
Linus Torvalds 已提交
526 527 528 529 530
	.d_revalidate	= autofs4_revalidate,
	.d_release	= autofs4_dentry_release,
};

/* For other dentries */
531
static const struct dentry_operations autofs4_dentry_operations = {
L
Linus Torvalds 已提交
532 533 534 535
	.d_revalidate	= autofs4_revalidate,
	.d_release	= autofs4_dentry_release,
};

536
static struct dentry *autofs4_lookup_active(struct dentry *dentry)
537
{
538 539 540
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
	struct dentry *parent = dentry->d_parent;
	struct qstr *name = &dentry->d_name;
541 542 543 544 545
	unsigned int len = name->len;
	unsigned int hash = name->hash;
	const unsigned char *str = name->name;
	struct list_head *p, *head;

I
Ian Kent 已提交
546
restart:
547 548 549 550 551
	spin_lock(&dcache_lock);
	spin_lock(&sbi->lookup_lock);
	head = &sbi->active_list;
	list_for_each(p, head) {
		struct autofs_info *ino;
552
		struct dentry *active;
553 554 555
		struct qstr *qstr;

		ino = list_entry(p, struct autofs_info, active);
556
		active = ino->dentry;
557

558
		spin_lock(&active->d_lock);
559 560

		/* Already gone? */
561
		if (atomic_read(&active->d_count) == 0)
562 563
			goto next;

I
Ian Kent 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576
		if (active->d_inode && IS_DEADDIR(active->d_inode)) {
			if (!list_empty(&ino->rehash_list)) {
				dget(active);
				spin_unlock(&active->d_lock);
				spin_unlock(&sbi->lookup_lock);
				spin_unlock(&dcache_lock);
				autofs4_remove_rehash_entrys(ino);
				dput(active);
				goto restart;
			}
			goto next;
		}

577
		qstr = &active->d_name;
578

579
		if (active->d_name.hash != hash)
580
			goto next;
581
		if (active->d_parent != parent)
582 583 584 585 586 587 588
			goto next;

		if (qstr->len != len)
			goto next;
		if (memcmp(qstr->name, str, len))
			goto next;

I
Ian Kent 已提交
589 590 591 592 593
		dget(active);
		spin_unlock(&active->d_lock);
		spin_unlock(&sbi->lookup_lock);
		spin_unlock(&dcache_lock);
		return active;
594
next:
595
		spin_unlock(&active->d_lock);
596 597 598 599 600 601 602
	}
	spin_unlock(&sbi->lookup_lock);
	spin_unlock(&dcache_lock);

	return NULL;
}

603
static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
604
{
605 606 607
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
	struct dentry *parent = dentry->d_parent;
	struct qstr *name = &dentry->d_name;
608 609 610 611 612 613
	unsigned int len = name->len;
	unsigned int hash = name->hash;
	const unsigned char *str = name->name;
	struct list_head *p, *head;

	spin_lock(&dcache_lock);
614 615
	spin_lock(&sbi->lookup_lock);
	head = &sbi->expiring_list;
616 617
	list_for_each(p, head) {
		struct autofs_info *ino;
618
		struct dentry *expiring;
619 620
		struct qstr *qstr;

621
		ino = list_entry(p, struct autofs_info, expiring);
622
		expiring = ino->dentry;
623

624
		spin_lock(&expiring->d_lock);
625 626

		/* Bad luck, we've already been dentry_iput */
627
		if (!expiring->d_inode)
628 629
			goto next;

630
		qstr = &expiring->d_name;
631

632
		if (expiring->d_name.hash != hash)
633
			goto next;
634
		if (expiring->d_parent != parent)
635 636 637 638 639 640 641
			goto next;

		if (qstr->len != len)
			goto next;
		if (memcmp(qstr->name, str, len))
			goto next;

I
Ian Kent 已提交
642 643 644 645 646
		dget(expiring);
		spin_unlock(&expiring->d_lock);
		spin_unlock(&sbi->lookup_lock);
		spin_unlock(&dcache_lock);
		return expiring;
647
next:
648
		spin_unlock(&expiring->d_lock);
649
	}
650
	spin_unlock(&sbi->lookup_lock);
651 652 653 654 655
	spin_unlock(&dcache_lock);

	return NULL;
}

I
Ian Kent 已提交
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
static struct autofs_info *init_new_dentry(struct autofs_sb_info *sbi,
					   struct dentry *dentry, int oz_mode)
{
	struct autofs_info *ino;

	/*
	 * Mark the dentry incomplete but don't hash it. We do this
	 * to serialize our inode creation operations (symlink and
	 * mkdir) which prevents deadlock during the callback to
	 * the daemon. Subsequent user space lookups for the same
	 * dentry are placed on the wait queue while the daemon
	 * itself is allowed passage unresticted so the create
	 * operation itself can then hash the dentry. Finally,
	 * we check for the hashed dentry and return the newly
	 * hashed dentry.
	 */
	dentry->d_op = &autofs4_root_dentry_operations;

	/*
	 * And we need to ensure that the same dentry is used for
	 * all following lookup calls until it is hashed so that
	 * the dentry flags are persistent throughout the request.
	 */
	ino = autofs4_init_ino(NULL, sbi, 0555);
	if (!ino)
		return ERR_PTR(-ENOMEM);

	dentry->d_fsdata = ino;
	ino->dentry = dentry;

	/*
	 * Only set the mount pending flag for new dentrys not created
	 * by the daemon.
	 */
	if (!oz_mode)
		ino->flags |= AUTOFS_INF_PENDING;

	d_instantiate(dentry, NULL);

	return ino;
}

L
Linus Torvalds 已提交
698 699 700 701
/* Lookups in the root directory */
static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
{
	struct autofs_sb_info *sbi;
702
	struct autofs_info *ino;
703
	struct dentry *expiring, *active;
L
Linus Torvalds 已提交
704
	int oz_mode;
I
Ian Kent 已提交
705
	int status = 0;
L
Linus Torvalds 已提交
706 707 708 709

	DPRINTK("name = %.*s",
		dentry->d_name.len, dentry->d_name.name);

710
	/* File name too long to exist */
L
Linus Torvalds 已提交
711
	if (dentry->d_name.len > NAME_MAX)
712
		return ERR_PTR(-ENAMETOOLONG);
L
Linus Torvalds 已提交
713 714 715

	sbi = autofs4_sbi(dir->i_sb);
	oz_mode = autofs4_oz_mode(sbi);
716

L
Linus Torvalds 已提交
717
	DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
P
Pavel Emelianov 已提交
718
		 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
L
Linus Torvalds 已提交
719

I
Ian Kent 已提交
720
	spin_lock(&sbi->fs_lock);
721
	active = autofs4_lookup_active(dentry);
722 723
	if (active) {
		dentry = active;
724
		ino = autofs4_dentry_ino(dentry);
I
Ian Kent 已提交
725 726 727
		/* If this came from revalidate, rehash it */
		autofs4_revalidate_rehash(dentry);
		spin_unlock(&sbi->fs_lock);
728
	} else {
I
Ian Kent 已提交
729 730 731 732
		spin_unlock(&sbi->fs_lock);
		ino = init_new_dentry(sbi, dentry, oz_mode);
		if (IS_ERR(ino))
			return (struct dentry *) ino;
733
	}
734

I
Ian Kent 已提交
735 736
	autofs4_add_active(dentry);

L
Linus Torvalds 已提交
737
	if (!oz_mode) {
738
		expiring = autofs4_lookup_expiring(dentry);
I
Ian Kent 已提交
739
		mutex_unlock(&dir->i_mutex);
I
Ian Kent 已提交
740 741 742 743 744 745 746 747 748
		if (expiring) {
			/*
			 * If we are racing with expire the request might not
			 * be quite complete but the directory has been removed
			 * so it must have been successful, so just wait for it.
			 */
			autofs4_expire_wait(expiring);
			dput(expiring);
		}
I
Ian Kent 已提交
749 750
		status = try_to_fill_dentry(dentry);
		mutex_lock(&dir->i_mutex);
751
		spin_lock(&sbi->fs_lock);
I
Ian Kent 已提交
752
		ino->flags &= ~AUTOFS_INF_PENDING;
753
		spin_unlock(&sbi->fs_lock);
L
Linus Torvalds 已提交
754 755
	}

I
Ian Kent 已提交
756 757
	autofs4_del_active(dentry);

L
Linus Torvalds 已提交
758
	/*
I
Ian Kent 已提交
759
	 * If we had a mount fail, check if we had to handle
L
Linus Torvalds 已提交
760 761
	 * a signal. If so we can force a restart..
	 */
I
Ian Kent 已提交
762
	if (status) {
L
Linus Torvalds 已提交
763 764 765 766 767 768
		/* See if we were interrupted */
		if (signal_pending(current)) {
			sigset_t *sigset = &current->pending.signal;
			if (sigismember (sigset, SIGKILL) ||
			    sigismember (sigset, SIGQUIT) ||
			    sigismember (sigset, SIGINT)) {
769 770
			    if (active)
				dput(active);
L
Linus Torvalds 已提交
771 772 773 774 775 776
			    return ERR_PTR(-ERESTARTNOINTR);
			}
		}
	}

	/*
I
Ian Kent 已提交
777 778 779 780
	 * User space can (and has done in the past) remove and re-create
	 * this directory during the callback. This can leave us with an
	 * unhashed dentry, but a successful mount!  So we need to
	 * perform another cached lookup in case the dentry now exists.
L
Linus Torvalds 已提交
781
	 */
I
Ian Kent 已提交
782 783 784 785 786 787 788 789 790 791 792 793
	if (!oz_mode && !have_submounts(dentry)) {
		struct dentry *new;
		new = d_lookup(dentry->d_parent, &dentry->d_name);
		if (new) {
			if (active)
				dput(active);
			return new;
		} else {
			if (!status)
				status = -ENOENT;
		}
	}
794

I
Ian Kent 已提交
795 796 797 798 799 800 801
	/*
	 * If we had a mount failure, return status to user space.
	 * If the mount succeeded and we used a dentry from the active queue
	 * return it.
	 */
	if (status) {
		dentry = ERR_PTR(status);
802 803
		if (active)
			dput(active);
804
		return dentry;
I
Ian Kent 已提交
805 806 807 808 809 810 811
	} else {
		/*
		 * Valid successful mount, return active dentry or NULL
		 * for a new dentry.
		 */
		if (active)
			return active;
812 813
	}

L
Linus Torvalds 已提交
814 815 816 817 818 819 820 821 822
	return NULL;
}

static int autofs4_dir_symlink(struct inode *dir, 
			       struct dentry *dentry,
			       const char *symname)
{
	struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
823
	struct autofs_info *p_ino;
L
Linus Torvalds 已提交
824 825 826 827 828 829 830 831 832 833
	struct inode *inode;
	char *cp;

	DPRINTK("%s <- %.*s", symname,
		dentry->d_name.len, dentry->d_name.name);

	if (!autofs4_oz_mode(sbi))
		return -EACCES;

	ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
834 835
	if (!ino)
		return -ENOMEM;
L
Linus Torvalds 已提交
836

I
Ian Kent 已提交
837
	ino->size = strlen(symname);
838 839 840 841 842
	cp = kmalloc(ino->size + 1, GFP_KERNEL);
	if (!cp) {
		if (!dentry->d_fsdata)
			kfree(ino);
		return -ENOMEM;
L
Linus Torvalds 已提交
843 844 845 846 847
	}

	strcpy(cp, symname);

	inode = autofs4_get_inode(dir->i_sb, ino);
848 849 850 851 852 853
	if (!inode) {
		kfree(cp);
		if (!dentry->d_fsdata)
			kfree(ino);
		return -ENOMEM;
	}
I
Ian Kent 已提交
854
	d_add(dentry, inode);
L
Linus Torvalds 已提交
855 856 857 858 859 860 861 862

	if (dir == dir->i_sb->s_root->d_inode)
		dentry->d_op = &autofs4_root_dentry_operations;
	else
		dentry->d_op = &autofs4_dentry_operations;

	dentry->d_fsdata = ino;
	ino->dentry = dget(dentry);
863 864 865 866
	atomic_inc(&ino->count);
	p_ino = autofs4_dentry_ino(dentry->d_parent);
	if (p_ino && dentry->d_parent != dentry)
		atomic_inc(&p_ino->count);
L
Linus Torvalds 已提交
867 868
	ino->inode = inode;

869
	ino->u.symlink = cp;
L
Linus Torvalds 已提交
870 871 872 873 874 875 876 877 878 879 880
	dir->i_mtime = CURRENT_TIME;

	return 0;
}

/*
 * NOTE!
 *
 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
 * that the file no longer exists. However, doing that means that the
 * VFS layer can turn the dentry into a negative dentry.  We don't want
881
 * this, because the unlink is probably the result of an expire.
882 883
 * We simply d_drop it and add it to a expiring list in the super block,
 * which allows the dentry lookup to check for an incomplete expire.
L
Linus Torvalds 已提交
884 885 886 887 888 889 890 891 892 893
 *
 * If a process is blocked on the dentry waiting for the expire to finish,
 * it will invalidate the dentry and try to mount with a new one.
 *
 * Also see autofs4_dir_rmdir()..
 */
static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
{
	struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
894
	struct autofs_info *p_ino;
L
Linus Torvalds 已提交
895 896
	
	/* This allows root to remove symlinks */
897
	if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
L
Linus Torvalds 已提交
898 899
		return -EACCES;

900 901 902 903 904
	if (atomic_dec_and_test(&ino->count)) {
		p_ino = autofs4_dentry_ino(dentry->d_parent);
		if (p_ino && dentry->d_parent != dentry)
			atomic_dec(&p_ino->count);
	}
L
Linus Torvalds 已提交
905 906 907
	dput(ino->dentry);

	dentry->d_inode->i_size = 0;
908
	clear_nlink(dentry->d_inode);
L
Linus Torvalds 已提交
909 910 911

	dir->i_mtime = CURRENT_TIME;

912 913 914 915 916
	spin_lock(&dcache_lock);
	spin_lock(&dentry->d_lock);
	__d_drop(dentry);
	spin_unlock(&dentry->d_lock);
	spin_unlock(&dcache_lock);
L
Linus Torvalds 已提交
917 918 919 920 921 922 923 924

	return 0;
}

static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
{
	struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
925
	struct autofs_info *p_ino;
L
Linus Torvalds 已提交
926
	
927 928 929
	DPRINTK("dentry %p, removing %.*s",
		dentry, dentry->d_name.len, dentry->d_name.name);

L
Linus Torvalds 已提交
930 931 932 933 934 935 936 937 938 939 940 941 942
	if (!autofs4_oz_mode(sbi))
		return -EACCES;

	spin_lock(&dcache_lock);
	if (!list_empty(&dentry->d_subdirs)) {
		spin_unlock(&dcache_lock);
		return -ENOTEMPTY;
	}
	spin_lock(&dentry->d_lock);
	__d_drop(dentry);
	spin_unlock(&dentry->d_lock);
	spin_unlock(&dcache_lock);

943 944 945 946 947
	if (atomic_dec_and_test(&ino->count)) {
		p_ino = autofs4_dentry_ino(dentry->d_parent);
		if (p_ino && dentry->d_parent != dentry)
			atomic_dec(&p_ino->count);
	}
L
Linus Torvalds 已提交
948 949
	dput(ino->dentry);
	dentry->d_inode->i_size = 0;
950
	clear_nlink(dentry->d_inode);
L
Linus Torvalds 已提交
951 952

	if (dir->i_nlink)
953
		drop_nlink(dir);
L
Linus Torvalds 已提交
954 955 956 957 958 959 960 961

	return 0;
}

static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
{
	struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
962
	struct autofs_info *p_ino;
L
Linus Torvalds 已提交
963 964
	struct inode *inode;

965
	if (!autofs4_oz_mode(sbi))
L
Linus Torvalds 已提交
966 967 968 969 970 971
		return -EACCES;

	DPRINTK("dentry %p, creating %.*s",
		dentry, dentry->d_name.len, dentry->d_name.name);

	ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
972 973 974
	if (!ino)
		return -ENOMEM;

L
Linus Torvalds 已提交
975
	inode = autofs4_get_inode(dir->i_sb, ino);
976 977 978 979 980
	if (!inode) {
		if (!dentry->d_fsdata)
			kfree(ino);
		return -ENOMEM;
	}
I
Ian Kent 已提交
981
	d_add(dentry, inode);
L
Linus Torvalds 已提交
982 983 984 985 986 987 988 989

	if (dir == dir->i_sb->s_root->d_inode)
		dentry->d_op = &autofs4_root_dentry_operations;
	else
		dentry->d_op = &autofs4_dentry_operations;

	dentry->d_fsdata = ino;
	ino->dentry = dget(dentry);
990 991 992 993
	atomic_inc(&ino->count);
	p_ino = autofs4_dentry_ino(dentry->d_parent);
	if (p_ino && dentry->d_parent != dentry)
		atomic_inc(&p_ino->count);
L
Linus Torvalds 已提交
994
	ino->inode = inode;
995
	inc_nlink(dir);
L
Linus Torvalds 已提交
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	dir->i_mtime = CURRENT_TIME;

	return 0;
}

/* Get/set timeout ioctl() operation */
static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
					 unsigned long __user *p)
{
	int rv;
	unsigned long ntimeout;

1008 1009
	if ((rv = get_user(ntimeout, p)) ||
	     (rv = put_user(sbi->exp_timeout/HZ, p)))
L
Linus Torvalds 已提交
1010 1011
		return rv;

1012
	if (ntimeout > ULONG_MAX/HZ)
L
Linus Torvalds 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
		sbi->exp_timeout = 0;
	else
		sbi->exp_timeout = ntimeout * HZ;

	return 0;
}

/* Return protocol version */
static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
{
	return put_user(sbi->version, p);
}

/* Return protocol sub version */
static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
{
	return put_user(sbi->sub_version, p);
}

/*
* Tells the daemon whether it can umount the autofs mount.
*/
static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
{
	int status = 0;

1039
	if (may_umount(mnt))
L
Linus Torvalds 已提交
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
		status = 1;

	DPRINTK("returning %d", status);

	status = put_user(status, p);

	return status;
}

/* Identify autofs4_dentries - this is so we can tell if there's
   an extra dentry refcount or not.  We only hold a refcount on the
   dentry if its non-negative (ie, d_inode != NULL)
*/
int is_autofs4_dentry(struct dentry *dentry)
{
	return dentry && dentry->d_inode &&
		(dentry->d_op == &autofs4_root_dentry_operations ||
		 dentry->d_op == &autofs4_dentry_operations) &&
		dentry->d_fsdata != NULL;
}

/*
 * ioctl()'s on the root directory is the chief method for the daemon to
 * generate kernel reactions
 */
static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
			     unsigned int cmd, unsigned long arg)
{
	struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
	void __user *p = (void __user *)arg;

	DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
P
Pavel Emelianov 已提交
1072
		cmd,arg,sbi,task_pgrp_nr(current));
L
Linus Torvalds 已提交
1073

1074 1075
	if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
	     _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
L
Linus Torvalds 已提交
1076 1077
		return -ENOTTY;
	
1078
	if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
L
Linus Torvalds 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
		return -EPERM;
	
	switch(cmd) {
	case AUTOFS_IOC_READY:	/* Wait queue: go ahead and retry */
		return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
	case AUTOFS_IOC_FAIL:	/* Wait queue: fail with ENOENT */
		return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
	case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
		autofs4_catatonic_mode(sbi);
		return 0;
	case AUTOFS_IOC_PROTOVER: /* Get protocol version */
		return autofs4_get_protover(sbi, p);
	case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
		return autofs4_get_protosubver(sbi, p);
	case AUTOFS_IOC_SETTIMEOUT:
		return autofs4_get_set_timeout(sbi, p);

	case AUTOFS_IOC_ASKUMOUNT:
1097
		return autofs4_ask_umount(filp->f_path.mnt, p);
L
Linus Torvalds 已提交
1098 1099 1100

	/* return a single thing to expire */
	case AUTOFS_IOC_EXPIRE:
1101
		return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
L
Linus Torvalds 已提交
1102 1103
	/* same as above, but can send multiple expires through pipe */
	case AUTOFS_IOC_EXPIRE_MULTI:
1104
		return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109

	default:
		return -ENOSYS;
	}
}