root.c 23.8 KB
Newer Older
I
Ian Kent 已提交
1 2 3 4
/*
 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
L
Linus Torvalds 已提交
5 6 7 8
 *
 * 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.
I
Ian Kent 已提交
9
 */
L
Linus Torvalds 已提交
10

11
#include <linux/capability.h>
L
Linus Torvalds 已提交
12 13
#include <linux/errno.h>
#include <linux/stat.h>
14
#include <linux/slab.h>
L
Linus Torvalds 已提交
15 16
#include <linux/param.h>
#include <linux/time.h>
17
#include <linux/compat.h>
A
Arnd Bergmann 已提交
18
#include <linux/mutex.h>
19

L
Linus Torvalds 已提交
20 21
#include "autofs_i.h"

I
Ian Kent 已提交
22 23 24 25 26
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 *, umode_t);
static long autofs4_root_ioctl(struct file *, unsigned int, unsigned long);
27
#ifdef CONFIG_COMPAT
I
Ian Kent 已提交
28 29
static long autofs4_root_compat_ioctl(struct file *,
				      unsigned int, unsigned long);
30
#endif
L
Linus Torvalds 已提交
31
static int autofs4_dir_open(struct inode *inode, struct file *file);
I
Ian Kent 已提交
32 33
static struct dentry *autofs4_lookup(struct inode *,
				     struct dentry *, unsigned int);
I
Ian Kent 已提交
34
static struct vfsmount *autofs4_d_automount(struct path *);
35
static int autofs4_d_manage(struct dentry *, bool);
36
static void autofs4_dentry_release(struct dentry *);
L
Linus Torvalds 已提交
37

38
const struct file_operations autofs4_root_operations = {
L
Linus Torvalds 已提交
39 40 41
	.open		= dcache_dir_open,
	.release	= dcache_dir_close,
	.read		= generic_read_dir,
42
	.iterate	= dcache_readdir,
43
	.llseek		= dcache_dir_lseek,
44
	.unlocked_ioctl	= autofs4_root_ioctl,
45 46 47
#ifdef CONFIG_COMPAT
	.compat_ioctl	= autofs4_root_compat_ioctl,
#endif
L
Linus Torvalds 已提交
48 49
};

50
const struct file_operations autofs4_dir_operations = {
L
Linus Torvalds 已提交
51
	.open		= autofs4_dir_open,
52
	.release	= dcache_dir_close,
L
Linus Torvalds 已提交
53
	.read		= generic_read_dir,
54
	.iterate	= dcache_readdir,
55
	.llseek		= dcache_dir_lseek,
L
Linus Torvalds 已提交
56 57
};

58
const struct inode_operations autofs4_dir_inode_operations = {
L
Linus Torvalds 已提交
59 60 61 62 63 64 65
	.lookup		= autofs4_lookup,
	.unlink		= autofs4_dir_unlink,
	.symlink	= autofs4_dir_symlink,
	.mkdir		= autofs4_dir_mkdir,
	.rmdir		= autofs4_dir_rmdir,
};

I
Ian Kent 已提交
66 67 68 69 70 71
const struct dentry_operations autofs4_dentry_operations = {
	.d_automount	= autofs4_d_automount,
	.d_manage	= autofs4_d_manage,
	.d_release	= autofs4_dentry_release,
};

72 73 74
static void autofs4_add_active(struct dentry *dentry)
{
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
I
Ian Kent 已提交
75 76 77
	struct autofs_info *ino;

	ino = autofs4_dentry_ino(dentry);
78 79 80 81 82 83 84 85 86 87 88 89 90 91
	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);
	}
}

static void autofs4_del_active(struct dentry *dentry)
{
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
I
Ian Kent 已提交
92 93 94
	struct autofs_info *ino;

	ino = autofs4_dentry_ino(dentry);
95 96 97 98 99 100 101 102 103 104 105
	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);
	}
}

L
Linus Torvalds 已提交
106 107
static int autofs4_dir_open(struct inode *inode, struct file *file)
{
108
	struct dentry *dentry = file->f_path.dentry;
L
Linus Torvalds 已提交
109
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
110

111
	DPRINTK("file=%p dentry=%p %pd\n", file, dentry, dentry);
L
Linus Torvalds 已提交
112 113 114 115

	if (autofs4_oz_mode(sbi))
		goto out;

116 117 118 119 120 121 122 123 124
	/*
	 * 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.
	 */
I
Ian Kent 已提交
125
	spin_lock(&sbi->lookup_lock);
126
	if (!d_mountpoint(dentry) && simple_empty(dentry)) {
I
Ian Kent 已提交
127
		spin_unlock(&sbi->lookup_lock);
128
		return -ENOENT;
L
Linus Torvalds 已提交
129
	}
I
Ian Kent 已提交
130
	spin_unlock(&sbi->lookup_lock);
L
Linus Torvalds 已提交
131 132

out:
133
	return dcache_dir_open(inode, file);
L
Linus Torvalds 已提交
134 135
}

136
static void autofs4_dentry_release(struct dentry *de)
L
Linus Torvalds 已提交
137
{
138 139
	struct autofs_info *ino = autofs4_dentry_ino(de);
	struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
L
Linus Torvalds 已提交
140

141
	DPRINTK("releasing %p\n", de);
L
Linus Torvalds 已提交
142

143 144 145 146 147 148 149 150 151 152
	if (!ino)
		return;

	if (sbi) {
		spin_lock(&sbi->lookup_lock);
		if (!list_empty(&ino->active))
			list_del(&ino->active);
		if (!list_empty(&ino->expiring))
			list_del(&ino->expiring);
		spin_unlock(&sbi->lookup_lock);
L
Linus Torvalds 已提交
153
	}
154 155

	autofs4_free_ino(ino);
L
Linus Torvalds 已提交
156 157
}

158
static struct dentry *autofs4_lookup_active(struct dentry *dentry)
159
{
160 161 162
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
	struct dentry *parent = dentry->d_parent;
	struct qstr *name = &dentry->d_name;
163 164 165 166 167 168
	unsigned int len = name->len;
	unsigned int hash = name->hash;
	const unsigned char *str = name->name;
	struct list_head *p, *head;

	head = &sbi->active_list;
169 170 171
	if (list_empty(head))
		return NULL;
	spin_lock(&sbi->lookup_lock);
172 173
	list_for_each(p, head) {
		struct autofs_info *ino;
174
		struct dentry *active;
175 176 177
		struct qstr *qstr;

		ino = list_entry(p, struct autofs_info, active);
178
		active = ino->dentry;
179

180
		spin_lock(&active->d_lock);
181 182

		/* Already gone? */
I
Ian Kent 已提交
183
		if ((int) d_count(active) <= 0)
184 185
			goto next;

186
		qstr = &active->d_name;
187

188
		if (active->d_name.hash != hash)
189
			goto next;
190
		if (active->d_parent != parent)
191 192 193 194 195 196 197
			goto next;

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

198
		if (d_unhashed(active)) {
N
Nick Piggin 已提交
199
			dget_dlock(active);
200 201 202 203
			spin_unlock(&active->d_lock);
			spin_unlock(&sbi->lookup_lock);
			return active;
		}
204
next:
205
		spin_unlock(&active->d_lock);
206 207 208 209 210 211
	}
	spin_unlock(&sbi->lookup_lock);

	return NULL;
}

212 213
static struct dentry *autofs4_lookup_expiring(struct dentry *dentry,
					      bool rcu_walk)
214
{
215 216 217
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
	struct dentry *parent = dentry->d_parent;
	struct qstr *name = &dentry->d_name;
218 219 220 221 222
	unsigned int len = name->len;
	unsigned int hash = name->hash;
	const unsigned char *str = name->name;
	struct list_head *p, *head;

223
	head = &sbi->expiring_list;
224 225 226
	if (list_empty(head))
		return NULL;
	spin_lock(&sbi->lookup_lock);
227 228
	list_for_each(p, head) {
		struct autofs_info *ino;
229
		struct dentry *expiring;
230 231
		struct qstr *qstr;

232 233 234 235 236
		if (rcu_walk) {
			spin_unlock(&sbi->lookup_lock);
			return ERR_PTR(-ECHILD);
		}

237
		ino = list_entry(p, struct autofs_info, expiring);
238
		expiring = ino->dentry;
239

240
		spin_lock(&expiring->d_lock);
241

I
Ian Kent 已提交
242
		/* We've already been dentry_iput or unlinked */
243
		if (d_really_is_negative(expiring))
244 245
			goto next;

246
		qstr = &expiring->d_name;
247

248
		if (expiring->d_name.hash != hash)
249
			goto next;
250
		if (expiring->d_parent != parent)
251 252 253 254 255 256 257
			goto next;

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

258
		if (d_unhashed(expiring)) {
N
Nick Piggin 已提交
259
			dget_dlock(expiring);
260 261 262 263
			spin_unlock(&expiring->d_lock);
			spin_unlock(&sbi->lookup_lock);
			return expiring;
		}
264
next:
265
		spin_unlock(&expiring->d_lock);
266
	}
267
	spin_unlock(&sbi->lookup_lock);
268 269 270 271

	return NULL;
}

272
static int autofs4_mount_wait(struct dentry *dentry, bool rcu_walk)
273 274 275
{
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
276
	int status = 0;
277 278

	if (ino->flags & AUTOFS_INF_PENDING) {
279 280
		if (rcu_walk)
			return -ECHILD;
281
		DPRINTK("waiting for mount name=%pd\n", dentry);
282
		status = autofs4_wait(sbi, dentry, NFY_MOUNT);
283
		DPRINTK("mount wait done status=%d\n", status);
284
	}
285 286
	ino->last_used = jiffies;
	return status;
287 288
}

289
static int do_expire_wait(struct dentry *dentry, bool rcu_walk)
290 291 292
{
	struct dentry *expiring;

293 294 295
	expiring = autofs4_lookup_expiring(dentry, rcu_walk);
	if (IS_ERR(expiring))
		return PTR_ERR(expiring);
296
	if (!expiring)
297
		return autofs4_expire_wait(dentry, rcu_walk);
298 299 300 301 302 303
	else {
		/*
		 * 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, just wait for it.
		 */
304
		autofs4_expire_wait(expiring, 0);
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
		autofs4_del_expiring(expiring);
		dput(expiring);
	}
	return 0;
}

static struct dentry *autofs4_mountpoint_changed(struct path *path)
{
	struct dentry *dentry = path->dentry;
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);

	/*
	 * If this is an indirect mount the dentry could have gone away
	 * as a result of an expire and a new one created.
	 */
	if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
		struct dentry *parent = dentry->d_parent;
322
		struct autofs_info *ino;
I
Ian Kent 已提交
323 324 325
		struct dentry *new;

		new = d_lookup(parent, &dentry->d_name);
326 327
		if (!new)
			return NULL;
328 329
		ino = autofs4_dentry_ino(new);
		ino->last_used = jiffies;
330 331 332 333 334 335
		dput(path->dentry);
		path->dentry = new;
	}
	return path->dentry;
}

I
Ian Kent 已提交
336
static struct vfsmount *autofs4_d_automount(struct path *path)
337 338 339 340 341 342
{
	struct dentry *dentry = path->dentry;
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
	int status;

343
	DPRINTK("dentry=%p %pd\n", dentry, dentry);
344 345 346 347 348 349 350 351 352 353 354 355 356

	/* The daemon never triggers a mount. */
	if (autofs4_oz_mode(sbi))
		return NULL;

	/*
	 * If an expire request is pending everyone must wait.
	 * If the expire fails we're still mounted so continue
	 * the follow and return. A return of -EAGAIN (which only
	 * happens with indirect mounts) means the expire completed
	 * and the directory was removed, so just go ahead and try
	 * the mount.
	 */
357
	status = do_expire_wait(dentry, 0);
358 359 360 361 362 363 364
	if (status && status != -EAGAIN)
		return NULL;

	/* Callback to the daemon to perform the mount or wait */
	spin_lock(&sbi->fs_lock);
	if (ino->flags & AUTOFS_INF_PENDING) {
		spin_unlock(&sbi->fs_lock);
365
		status = autofs4_mount_wait(dentry, 0);
366 367 368 369 370 371 372
		if (status)
			return ERR_PTR(status);
		goto done;
	}

	/*
	 * If the dentry is a symlink it's equivalent to a directory
373
	 * having d_mountpoint() true, so there's no need to call back
374 375
	 * to the daemon.
	 */
376
	if (d_really_is_positive(dentry) && d_is_symlink(dentry)) {
377
		spin_unlock(&sbi->fs_lock);
378
		goto done;
379 380
	}

381 382 383 384 385 386 387 388
	if (!d_mountpoint(dentry)) {
		/*
		 * It's possible that user space hasn't removed directories
		 * after umounting a rootless multi-mount, although it
		 * should. For v5 have_submounts() is sufficient to handle
		 * this because the leaves of the directory tree under the
		 * mount never trigger mounts themselves (they have an autofs
		 * trigger mount mounted on them). But v4 pseudo direct mounts
389
		 * do need the leaves to trigger mounts. In this case we
390 391 392 393
		 * have no choice but to use the list_empty() check and
		 * require user space behave.
		 */
		if (sbi->version > 4) {
394 395
			if (have_submounts(dentry)) {
				spin_unlock(&sbi->fs_lock);
396
				goto done;
397
			}
398
		} else {
399 400
			if (!simple_empty(dentry)) {
				spin_unlock(&sbi->fs_lock);
401
				goto done;
402
			}
403
		}
404 405
		ino->flags |= AUTOFS_INF_PENDING;
		spin_unlock(&sbi->fs_lock);
406
		status = autofs4_mount_wait(dentry, 0);
407 408
		spin_lock(&sbi->fs_lock);
		ino->flags &= ~AUTOFS_INF_PENDING;
409 410 411 412
		if (status) {
			spin_unlock(&sbi->fs_lock);
			return ERR_PTR(status);
		}
413 414
	}
	spin_unlock(&sbi->fs_lock);
415
done:
416 417 418 419 420 421 422 423
	/* Mount succeeded, check if we ended up with a new dentry */
	dentry = autofs4_mountpoint_changed(path);
	if (!dentry)
		return ERR_PTR(-ENOENT);

	return NULL;
}

424
static int autofs4_d_manage(struct dentry *dentry, bool rcu_walk)
425 426
{
	struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
427 428
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
	int status;
429

430
	DPRINTK("dentry=%p %pd\n", dentry, dentry);
431 432

	/* The daemon never waits. */
433
	if (autofs4_oz_mode(sbi)) {
434 435 436 437 438 439
		if (!d_mountpoint(dentry))
			return -EISDIR;
		return 0;
	}

	/* Wait for pending expires */
440 441
	if (do_expire_wait(dentry, rcu_walk) == -ECHILD)
		return -ECHILD;
442 443 444 445 446

	/*
	 * This dentry may be under construction so wait on mount
	 * completion.
	 */
447
	status = autofs4_mount_wait(dentry, rcu_walk);
448 449 450
	if (status)
		return status;

451 452 453 454 455 456 457
	if (rcu_walk) {
		/* We don't need fs_lock in rcu_walk mode,
		 * just testing 'AUTOFS_INFO_NO_RCU' is enough.
		 * simple_empty() takes a spinlock, so leave it
		 * to last.
		 * We only return -EISDIR when certain this isn't
		 * a mount-trap.
458
		 */
459
		struct inode *inode;
I
Ian Kent 已提交
460

461 462 463 464
		if (ino->flags & (AUTOFS_INF_EXPIRING | AUTOFS_INF_NO_RCU))
			return 0;
		if (d_mountpoint(dentry))
			return 0;
465
		inode = d_inode_rcu(dentry);
466 467 468 469 470 471
		if (inode && S_ISLNK(inode->i_mode))
			return -EISDIR;
		if (list_empty(&dentry->d_subdirs))
			return 0;
		if (!simple_empty(dentry))
			return -EISDIR;
472
		return 0;
473
	}
474

475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
	spin_lock(&sbi->fs_lock);
	/*
	 * If the dentry has been selected for expire while we slept
	 * on the lock then it might go away. We'll deal with that in
	 * ->d_automount() and wait on a new mount if the expire
	 * succeeds or return here if it doesn't (since there's no
	 * mount to follow with a rootless multi-mount).
	 */
	if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
		/*
		 * Any needed mounting has been completed and the path
		 * updated so check if this is a rootless multi-mount so
		 * we can avoid needless calls ->d_automount() and avoid
		 * an incorrect ELOOP error return.
		 */
		if ((!d_mountpoint(dentry) && !simple_empty(dentry)) ||
491
		    (d_really_is_positive(dentry) && d_is_symlink(dentry)))
492 493 494 495 496
			status = -EISDIR;
	}
	spin_unlock(&sbi->fs_lock);

	return status;
497 498
}

L
Linus Torvalds 已提交
499
/* Lookups in the root directory */
I
Ian Kent 已提交
500 501
static struct dentry *autofs4_lookup(struct inode *dir,
				     struct dentry *dentry, unsigned int flags)
L
Linus Torvalds 已提交
502 503
{
	struct autofs_sb_info *sbi;
504
	struct autofs_info *ino;
505
	struct dentry *active;
L
Linus Torvalds 已提交
506

507
	DPRINTK("name = %pd\n", dentry);
L
Linus Torvalds 已提交
508

509
	/* File name too long to exist */
L
Linus Torvalds 已提交
510
	if (dentry->d_name.len > NAME_MAX)
511
		return ERR_PTR(-ENAMETOOLONG);
L
Linus Torvalds 已提交
512 513

	sbi = autofs4_sbi(dir->i_sb);
514

515
	DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
516 517
		current->pid, task_pgrp_nr(current), sbi->catatonic,
		autofs4_oz_mode(sbi));
L
Linus Torvalds 已提交
518

519
	active = autofs4_lookup_active(dentry);
I
Ian Kent 已提交
520
	if (active)
521
		return active;
I
Ian Kent 已提交
522
	else {
523
		/*
524 525 526 527
		 * A dentry that is not within the root can never trigger a
		 * mount operation, unless the directory already exists, so we
		 * can return fail immediately.  The daemon however does need
		 * to create directories within the file system.
528
		 */
529 530 531 532
		if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
			return ERR_PTR(-ENOENT);

		/* Mark entries in the root as mount triggers */
I
Ian Kent 已提交
533 534
		if (IS_ROOT(dentry->d_parent) &&
		    autofs_type_indirect(sbi->type))
535
			__managed_dentry_set_managed(dentry);
536

A
Al Viro 已提交
537
		ino = autofs4_new_ino(sbi);
538 539 540 541 542
		if (!ino)
			return ERR_PTR(-ENOMEM);

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

544 545 546 547
		autofs4_add_active(dentry);

		d_instantiate(dentry, NULL);
	}
L
Linus Torvalds 已提交
548 549 550 551 552 553 554 555 556
	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);
557
	struct autofs_info *p_ino;
L
Linus Torvalds 已提交
558
	struct inode *inode;
A
Al Viro 已提交
559
	size_t size = strlen(symname);
L
Linus Torvalds 已提交
560 561
	char *cp;

562
	DPRINTK("%s <- %pd\n", symname, dentry);
L
Linus Torvalds 已提交
563 564 565 566

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

567 568
	BUG_ON(!ino);

A
Al Viro 已提交
569
	autofs4_clean_ino(ino);
L
Linus Torvalds 已提交
570

571 572
	autofs4_del_active(dentry);

A
Al Viro 已提交
573
	cp = kmalloc(size + 1, GFP_KERNEL);
574
	if (!cp)
575
		return -ENOMEM;
L
Linus Torvalds 已提交
576 577 578

	strcpy(cp, symname);

579
	inode = autofs4_get_inode(dir->i_sb, S_IFLNK | 0555);
580 581 582 583 584 585
	if (!inode) {
		kfree(cp);
		if (!dentry->d_fsdata)
			kfree(ino);
		return -ENOMEM;
	}
586
	inode->i_private = cp;
A
Al Viro 已提交
587
	inode->i_size = size;
I
Ian Kent 已提交
588
	d_add(dentry, inode);
L
Linus Torvalds 已提交
589

590
	dget(dentry);
591 592
	atomic_inc(&ino->count);
	p_ino = autofs4_dentry_ino(dentry->d_parent);
593
	if (p_ino && !IS_ROOT(dentry))
594
		atomic_inc(&p_ino->count);
L
Linus Torvalds 已提交
595 596 597 598 599 600 601 602 603 604 605 606

	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
607
 * this, because the unlink is probably the result of an expire.
608 609
 * 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 已提交
610 611 612 613 614 615 616 617 618 619
 *
 * 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);
620
	struct autofs_info *p_ino;
I
Ian Kent 已提交
621

L
Linus Torvalds 已提交
622
	/* This allows root to remove symlinks */
623
	if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
624
		return -EPERM;
L
Linus Torvalds 已提交
625

626 627
	if (atomic_dec_and_test(&ino->count)) {
		p_ino = autofs4_dentry_ino(dentry->d_parent);
628
		if (p_ino && !IS_ROOT(dentry))
629 630
			atomic_dec(&p_ino->count);
	}
L
Linus Torvalds 已提交
631 632
	dput(ino->dentry);

633 634
	d_inode(dentry)->i_size = 0;
	clear_nlink(d_inode(dentry));
L
Linus Torvalds 已提交
635 636 637

	dir->i_mtime = CURRENT_TIME;

I
Ian Kent 已提交
638 639
	spin_lock(&sbi->lookup_lock);
	__autofs4_add_expiring(dentry);
640
	d_drop(dentry);
I
Ian Kent 已提交
641
	spin_unlock(&sbi->lookup_lock);
L
Linus Torvalds 已提交
642 643 644 645

	return 0;
}

646 647 648 649 650 651 652
/*
 * Version 4 of autofs provides a pseudo direct mount implementation
 * that relies on directories at the leaves of a directory tree under
 * an indirect mount to trigger mounts. To allow for this we need to
 * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves
 * of the directory tree. There is no need to clear the automount flag
 * following a mount or restore it after an expire because these mounts
L
Lucas De Marchi 已提交
653
 * are always covered. However, it is necessary to ensure that these
654 655 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
 * flags are clear on non-empty directories to avoid unnecessary calls
 * during path walks.
 */
static void autofs_set_leaf_automount_flags(struct dentry *dentry)
{
	struct dentry *parent;

	/* root and dentrys in the root are already handled */
	if (IS_ROOT(dentry->d_parent))
		return;

	managed_dentry_set_managed(dentry);

	parent = dentry->d_parent;
	/* only consider parents below dentrys in the root */
	if (IS_ROOT(parent->d_parent))
		return;
	managed_dentry_clear_managed(parent);
}

static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
{
	struct list_head *d_child;
	struct dentry *parent;

	/* flags for dentrys in the root are handled elsewhere */
	if (IS_ROOT(dentry->d_parent))
		return;

	managed_dentry_clear_managed(dentry);

	parent = dentry->d_parent;
	/* only consider parents below dentrys in the root */
	if (IS_ROOT(parent->d_parent))
		return;
689
	d_child = &dentry->d_child;
690 691 692 693 694 695
	/* Set parent managed if it's becoming empty */
	if (d_child->next == &parent->d_subdirs &&
	    d_child->prev == &parent->d_subdirs)
		managed_dentry_set_managed(parent);
}

L
Linus Torvalds 已提交
696 697 698 699
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);
700
	struct autofs_info *p_ino;
I
Ian Kent 已提交
701

702
	DPRINTK("dentry %p, removing %pd\n", dentry, dentry);
703

L
Linus Torvalds 已提交
704 705 706
	if (!autofs4_oz_mode(sbi))
		return -EACCES;

N
Nick Piggin 已提交
707
	spin_lock(&sbi->lookup_lock);
708
	if (!simple_empty(dentry)) {
N
Nick Piggin 已提交
709
		spin_unlock(&sbi->lookup_lock);
L
Linus Torvalds 已提交
710 711
		return -ENOTEMPTY;
	}
N
Nick Piggin 已提交
712
	__autofs4_add_expiring(dentry);
713
	d_drop(dentry);
I
Ian Kent 已提交
714
	spin_unlock(&sbi->lookup_lock);
L
Linus Torvalds 已提交
715

716 717 718
	if (sbi->version < 5)
		autofs_clear_leaf_automount_flags(dentry);

719 720 721 722 723
	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 已提交
724
	dput(ino->dentry);
725 726
	d_inode(dentry)->i_size = 0;
	clear_nlink(d_inode(dentry));
L
Linus Torvalds 已提交
727 728

	if (dir->i_nlink)
729
		drop_nlink(dir);
L
Linus Torvalds 已提交
730 731 732 733

	return 0;
}

I
Ian Kent 已提交
734 735
static int autofs4_dir_mkdir(struct inode *dir,
			     struct dentry *dentry, umode_t mode)
L
Linus Torvalds 已提交
736 737 738
{
	struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
	struct autofs_info *ino = autofs4_dentry_ino(dentry);
739
	struct autofs_info *p_ino;
L
Linus Torvalds 已提交
740 741
	struct inode *inode;

742
	if (!autofs4_oz_mode(sbi))
L
Linus Torvalds 已提交
743 744
		return -EACCES;

745
	DPRINTK("dentry %p, creating %pd\n", dentry, dentry);
L
Linus Torvalds 已提交
746

747 748
	BUG_ON(!ino);

A
Al Viro 已提交
749
	autofs4_clean_ino(ino);
750

751 752
	autofs4_del_active(dentry);

753
	inode = autofs4_get_inode(dir->i_sb, S_IFDIR | 0555);
754
	if (!inode)
755
		return -ENOMEM;
I
Ian Kent 已提交
756
	d_add(dentry, inode);
L
Linus Torvalds 已提交
757

758 759 760
	if (sbi->version < 5)
		autofs_set_leaf_automount_flags(dentry);

761
	dget(dentry);
762 763
	atomic_inc(&ino->count);
	p_ino = autofs4_dentry_ino(dentry->d_parent);
764
	if (p_ino && !IS_ROOT(dentry))
765
		atomic_inc(&p_ino->count);
766
	inc_nlink(dir);
L
Linus Torvalds 已提交
767 768 769 770 771 772
	dir->i_mtime = CURRENT_TIME;

	return 0;
}

/* Get/set timeout ioctl() operation */
773 774
#ifdef CONFIG_COMPAT
static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
I
Ian Kent 已提交
775
						 compat_ulong_t __user *p)
776 777
{
	unsigned long ntimeout;
778 779 780 781 782
	int rv;

	rv = get_user(ntimeout, p);
	if (rv)
		goto error;
783

784 785 786
	rv = put_user(sbi->exp_timeout/HZ, p);
	if (rv)
		goto error;
787 788 789 790 791 792 793

	if (ntimeout > UINT_MAX/HZ)
		sbi->exp_timeout = 0;
	else
		sbi->exp_timeout = ntimeout * HZ;

	return 0;
794 795
error:
	return rv;
796 797 798
}
#endif

L
Linus Torvalds 已提交
799
static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
I
Ian Kent 已提交
800
					  unsigned long __user *p)
L
Linus Torvalds 已提交
801 802
{
	unsigned long ntimeout;
803 804 805 806 807
	int rv;

	rv = get_user(ntimeout, p);
	if (rv)
		goto error;
L
Linus Torvalds 已提交
808

809 810 811
	rv = put_user(sbi->exp_timeout/HZ, p);
	if (rv)
		goto error;
L
Linus Torvalds 已提交
812

813
	if (ntimeout > ULONG_MAX/HZ)
L
Linus Torvalds 已提交
814 815 816 817 818
		sbi->exp_timeout = 0;
	else
		sbi->exp_timeout = ntimeout * HZ;

	return 0;
819 820
error:
	return rv;
L
Linus Torvalds 已提交
821 822 823
}

/* Return protocol version */
I
Ian Kent 已提交
824 825
static inline int autofs4_get_protover(struct autofs_sb_info *sbi,
				       int __user *p)
L
Linus Torvalds 已提交
826 827 828 829 830
{
	return put_user(sbi->version, p);
}

/* Return protocol sub version */
I
Ian Kent 已提交
831 832
static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi,
					  int __user *p)
L
Linus Torvalds 已提交
833 834 835 836 837 838 839 840 841 842 843
{
	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;

844
	if (may_umount(mnt))
L
Linus Torvalds 已提交
845 846
		status = 1;

847
	DPRINTK("returning %d\n", status);
L
Linus Torvalds 已提交
848 849 850 851 852 853 854

	status = put_user(status, p);

	return status;
}

/* Identify autofs4_dentries - this is so we can tell if there's
I
Ian Kent 已提交
855 856 857
 * an extra dentry refcount or not.  We only hold a refcount on the
 * dentry if its non-negative (ie, d_inode != NULL)
 */
L
Linus Torvalds 已提交
858 859
int is_autofs4_dentry(struct dentry *dentry)
{
860
	return dentry && d_really_is_positive(dentry) &&
861
		dentry->d_op == &autofs4_dentry_operations &&
L
Linus Torvalds 已提交
862 863 864 865 866 867 868
		dentry->d_fsdata != NULL;
}

/*
 * ioctl()'s on the root directory is the chief method for the daemon to
 * generate kernel reactions
 */
869 870
static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
				       unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
871 872 873 874
{
	struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
	void __user *p = (void __user *)arg;

875
	DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",
I
Ian Kent 已提交
876
		cmd, arg, sbi, task_pgrp_nr(current));
L
Linus Torvalds 已提交
877

878 879
	if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
	     _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
L
Linus Torvalds 已提交
880
		return -ENOTTY;
I
Ian Kent 已提交
881

882
	if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
L
Linus Torvalds 已提交
883
		return -EPERM;
I
Ian Kent 已提交
884

I
Ian Kent 已提交
885
	switch (cmd) {
L
Linus Torvalds 已提交
886
	case AUTOFS_IOC_READY:	/* Wait queue: go ahead and retry */
I
Ian Kent 已提交
887
		return autofs4_wait_release(sbi, (autofs_wqt_t) arg, 0);
L
Linus Torvalds 已提交
888
	case AUTOFS_IOC_FAIL:	/* Wait queue: fail with ENOENT */
I
Ian Kent 已提交
889
		return autofs4_wait_release(sbi, (autofs_wqt_t) arg, -ENOENT);
L
Linus Torvalds 已提交
890 891 892 893 894 895 896 897 898
	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);
899 900 901 902
#ifdef CONFIG_COMPAT
	case AUTOFS_IOC_SETTIMEOUT32:
		return autofs4_compat_get_set_timeout(sbi, p);
#endif
L
Linus Torvalds 已提交
903 904

	case AUTOFS_IOC_ASKUMOUNT:
905
		return autofs4_ask_umount(filp->f_path.mnt, p);
L
Linus Torvalds 已提交
906 907 908

	/* return a single thing to expire */
	case AUTOFS_IOC_EXPIRE:
I
Ian Kent 已提交
909 910
		return autofs4_expire_run(inode->i_sb,
					  filp->f_path.mnt, sbi, p);
L
Linus Torvalds 已提交
911 912
	/* same as above, but can send multiple expires through pipe */
	case AUTOFS_IOC_EXPIRE_MULTI:
I
Ian Kent 已提交
913 914
		return autofs4_expire_multi(inode->i_sb,
					    filp->f_path.mnt, sbi, p);
L
Linus Torvalds 已提交
915 916

	default:
917
		return -EINVAL;
L
Linus Torvalds 已提交
918 919
	}
}
920 921 922 923

static long autofs4_root_ioctl(struct file *filp,
			       unsigned int cmd, unsigned long arg)
{
A
Al Viro 已提交
924
	struct inode *inode = file_inode(filp);
I
Ian Kent 已提交
925

I
Ian Kent 已提交
926
	return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
927
}
928 929 930

#ifdef CONFIG_COMPAT
static long autofs4_root_compat_ioctl(struct file *filp,
I
Ian Kent 已提交
931
				      unsigned int cmd, unsigned long arg)
932
{
A
Al Viro 已提交
933
	struct inode *inode = file_inode(filp);
934 935 936 937 938 939
	int ret;

	if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
		ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
	else
		ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
I
Ian Kent 已提交
940
					      (unsigned long) compat_ptr(arg));
941 942 943 944

	return ret;
}
#endif