util.c 13.4 KB
Newer Older
M
Miklos Szeredi 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (C) 2011 Novell Inc.
 * Copyright (C) 2016 Red Hat, Inc.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

#include <linux/fs.h>
#include <linux/mount.h>
#include <linux/slab.h>
13
#include <linux/cred.h>
M
Miklos Szeredi 已提交
14
#include <linux/xattr.h>
15 16
#include <linux/exportfs.h>
#include <linux/uuid.h>
17 18
#include <linux/namei.h>
#include <linux/ratelimit.h>
M
Miklos Szeredi 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include "overlayfs.h"
#include "ovl_entry.h"

int ovl_want_write(struct dentry *dentry)
{
	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
	return mnt_want_write(ofs->upper_mnt);
}

void ovl_drop_write(struct dentry *dentry)
{
	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
	mnt_drop_write(ofs->upper_mnt);
}

struct dentry *ovl_workdir(struct dentry *dentry)
{
	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
	return ofs->workdir;
}

const struct cred *ovl_override_creds(struct super_block *sb)
{
	struct ovl_fs *ofs = sb->s_fs_info;

	return override_creds(ofs->creator_cred);
}

47 48 49 50 51 52 53
struct super_block *ovl_same_sb(struct super_block *sb)
{
	struct ovl_fs *ofs = sb->s_fs_info;

	return ofs->same_sb;
}

54 55 56 57 58 59 60 61 62 63 64 65 66
bool ovl_can_decode_fh(struct super_block *sb)
{
	return (sb->s_export_op && sb->s_export_op->fh_to_dentry &&
		!uuid_is_null(&sb->s_uuid));
}

struct dentry *ovl_indexdir(struct super_block *sb)
{
	struct ovl_fs *ofs = sb->s_fs_info;

	return ofs->indexdir;
}

M
Miklos Szeredi 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
{
	size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
	struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);

	if (oe)
		oe->numlower = numlower;

	return oe;
}

bool ovl_dentry_remote(struct dentry *dentry)
{
	return dentry->d_flags &
		(DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
		 DCACHE_OP_REAL);
}

bool ovl_dentry_weird(struct dentry *dentry)
{
	return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
				  DCACHE_MANAGE_TRANSIT |
				  DCACHE_OP_HASH |
				  DCACHE_OP_COMPARE);
}

enum ovl_path_type ovl_path_type(struct dentry *dentry)
{
	struct ovl_entry *oe = dentry->d_fsdata;
	enum ovl_path_type type = 0;

98
	if (ovl_dentry_upper(dentry)) {
M
Miklos Szeredi 已提交
99 100 101
		type = __OVL_PATH_UPPER;

		/*
A
Amir Goldstein 已提交
102
		 * Non-dir dentry can hold lower dentry of its copy up origin.
M
Miklos Szeredi 已提交
103
		 */
A
Amir Goldstein 已提交
104 105 106 107 108
		if (oe->numlower) {
			type |= __OVL_PATH_ORIGIN;
			if (d_is_dir(dentry))
				type |= __OVL_PATH_MERGE;
		}
M
Miklos Szeredi 已提交
109 110 111 112 113 114 115 116 117 118 119 120
	} else {
		if (oe->numlower > 1)
			type |= __OVL_PATH_MERGE;
	}
	return type;
}

void ovl_path_upper(struct dentry *dentry, struct path *path)
{
	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;

	path->mnt = ofs->upper_mnt;
121
	path->dentry = ovl_dentry_upper(dentry);
M
Miklos Szeredi 已提交
122 123 124 125 126 127
}

void ovl_path_lower(struct dentry *dentry, struct path *path)
{
	struct ovl_entry *oe = dentry->d_fsdata;

K
Kees Cook 已提交
128
	*path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
M
Miklos Szeredi 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
}

enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
{
	enum ovl_path_type type = ovl_path_type(dentry);

	if (!OVL_TYPE_UPPER(type))
		ovl_path_lower(dentry, path);
	else
		ovl_path_upper(dentry, path);

	return type;
}

struct dentry *ovl_dentry_upper(struct dentry *dentry)
{
145
	return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
M
Miklos Szeredi 已提交
146 147 148 149 150 151
}

struct dentry *ovl_dentry_lower(struct dentry *dentry)
{
	struct ovl_entry *oe = dentry->d_fsdata;

152
	return oe->numlower ? oe->lowerstack[0].dentry : NULL;
M
Miklos Szeredi 已提交
153 154 155 156
}

struct dentry *ovl_dentry_real(struct dentry *dentry)
{
157
	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
M
Miklos Szeredi 已提交
158 159
}

160 161 162 163 164
struct dentry *ovl_i_dentry_upper(struct inode *inode)
{
	return ovl_upperdentry_dereference(OVL_I(inode));
}

165
struct inode *ovl_inode_upper(struct inode *inode)
166
{
167
	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
168

169 170
	return upperdentry ? d_inode(upperdentry) : NULL;
}
171

172 173 174 175
struct inode *ovl_inode_lower(struct inode *inode)
{
	return OVL_I(inode)->lower;
}
176

177 178 179
struct inode *ovl_inode_real(struct inode *inode)
{
	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
180 181
}

182

183
struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
M
Miklos Szeredi 已提交
184
{
185
	return OVL_I(inode)->cache;
M
Miklos Szeredi 已提交
186 187
}

188
void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
M
Miklos Szeredi 已提交
189
{
190
	OVL_I(inode)->cache = cache;
M
Miklos Szeredi 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203
}

bool ovl_dentry_is_opaque(struct dentry *dentry)
{
	struct ovl_entry *oe = dentry->d_fsdata;
	return oe->opaque;
}

bool ovl_dentry_is_whiteout(struct dentry *dentry)
{
	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
}

M
Miklos Szeredi 已提交
204
void ovl_dentry_set_opaque(struct dentry *dentry)
M
Miklos Szeredi 已提交
205 206
{
	struct ovl_entry *oe = dentry->d_fsdata;
M
Miklos Szeredi 已提交
207 208

	oe->opaque = true;
M
Miklos Szeredi 已提交
209 210
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
/*
 * For hard links it's possible for ovl_dentry_upper() to return positive, while
 * there's no actual upper alias for the inode.  Copy up code needs to know
 * about the existence of the upper alias, so it can't use ovl_dentry_upper().
 */
bool ovl_dentry_has_upper_alias(struct dentry *dentry)
{
	struct ovl_entry *oe = dentry->d_fsdata;

	return oe->has_upper;
}

void ovl_dentry_set_upper_alias(struct dentry *dentry)
{
	struct ovl_entry *oe = dentry->d_fsdata;

	oe->has_upper = true;
}

M
Miklos Szeredi 已提交
230 231 232 233
bool ovl_redirect_dir(struct super_block *sb)
{
	struct ovl_fs *ofs = sb->s_fs_info;

234
	return ofs->config.redirect_dir && !ofs->noxattr;
M
Miklos Szeredi 已提交
235 236 237 238
}

const char *ovl_dentry_get_redirect(struct dentry *dentry)
{
M
Miklos Szeredi 已提交
239
	return OVL_I(d_inode(dentry))->redirect;
M
Miklos Szeredi 已提交
240 241 242 243
}

void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
{
M
Miklos Szeredi 已提交
244
	struct ovl_inode *oi = OVL_I(d_inode(dentry));
M
Miklos Szeredi 已提交
245

M
Miklos Szeredi 已提交
246 247
	kfree(oi->redirect);
	oi->redirect = redirect;
M
Miklos Szeredi 已提交
248 249
}

250 251
void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
		    struct dentry *lowerdentry)
M
Miklos Szeredi 已提交
252
{
253 254 255 256
	if (upperdentry)
		OVL_I(inode)->__upperdentry = upperdentry;
	if (lowerdentry)
		OVL_I(inode)->lower = d_inode(lowerdentry);
M
Miklos Szeredi 已提交
257

258
	ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
M
Miklos Szeredi 已提交
259 260
}

261
void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
M
Miklos Szeredi 已提交
262
{
263
	struct inode *upperinode = d_inode(upperdentry);
M
Miklos Szeredi 已提交
264

265 266
	WARN_ON(OVL_I(inode)->__upperdentry);

267
	/*
268
	 * Make sure upperdentry is consistent before making it visible
269 270
	 */
	smp_wmb();
271
	OVL_I(inode)->__upperdentry = upperdentry;
272
	if (!S_ISDIR(upperinode->i_mode) && inode_unhashed(inode)) {
273
		inode->i_private = upperinode;
M
Miklos Szeredi 已提交
274
		__insert_inode_hash(inode, (unsigned long) upperinode);
275
	}
M
Miklos Szeredi 已提交
276 277
}

278
void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
M
Miklos Szeredi 已提交
279
{
280
	struct inode *inode = d_inode(dentry);
M
Miklos Szeredi 已提交
281

282
	WARN_ON(!inode_is_locked(inode));
283 284 285 286 287 288 289 290
	/*
	 * Version is used by readdir code to keep cache consistent.  For merge
	 * dirs all changes need to be noted.  For non-merge dirs, cache only
	 * contains impure (ones which have been copied up and have origins)
	 * entries, so only need to note changes to impure entries.
	 */
	if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
		OVL_I(inode)->version++;
M
Miklos Szeredi 已提交
291 292 293 294
}

u64 ovl_dentry_version_get(struct dentry *dentry)
{
295
	struct inode *inode = d_inode(dentry);
M
Miklos Szeredi 已提交
296

297 298
	WARN_ON(!inode_is_locked(inode));
	return OVL_I(inode)->version;
M
Miklos Szeredi 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311
}

bool ovl_is_whiteout(struct dentry *dentry)
{
	struct inode *inode = dentry->d_inode;

	return inode && IS_WHITEOUT(inode);
}

struct file *ovl_path_open(struct path *path, int flags)
{
	return dentry_open(path, flags | O_NOATIME, current_cred());
}
312 313 314

int ovl_copy_up_start(struct dentry *dentry)
{
315
	struct ovl_inode *oi = OVL_I(d_inode(dentry));
316 317
	int err;

318
	err = mutex_lock_interruptible(&oi->lock);
319
	if (!err && ovl_dentry_has_upper_alias(dentry)) {
320 321
		err = 1; /* Already copied up */
		mutex_unlock(&oi->lock);
322 323 324 325 326 327 328
	}

	return err;
}

void ovl_copy_up_end(struct dentry *dentry)
{
329
	mutex_unlock(&OVL_I(d_inode(dentry))->lock);
330
}
331

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
{
	int res;
	char val;

	if (!d_is_dir(dentry))
		return false;

	res = vfs_getxattr(dentry, name, &val, 1);
	if (res == 1 && val == 'y')
		return true;

	return false;
}

347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
		       const char *name, const void *value, size_t size,
		       int xerr)
{
	int err;
	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;

	if (ofs->noxattr)
		return xerr;

	err = ovl_do_setxattr(upperdentry, name, value, size, 0);

	if (err == -EOPNOTSUPP) {
		pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
		ofs->noxattr = true;
		return xerr;
	}

	return err;
}
367 368 369 370 371

int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
{
	int err;

M
Miklos Szeredi 已提交
372
	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
373 374 375 376 377 378 379 380 381
		return 0;

	/*
	 * Do not fail when upper doesn't support xattrs.
	 * Upper inodes won't have origin nor redirect xattr anyway.
	 */
	err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
				 "y", 1, 0);
	if (!err)
M
Miklos Szeredi 已提交
382
		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
383 384 385

	return err;
}
M
Miklos Szeredi 已提交
386 387 388 389 390 391

void ovl_set_flag(unsigned long flag, struct inode *inode)
{
	set_bit(flag, &OVL_I(inode)->flags);
}

392 393 394 395 396
void ovl_clear_flag(unsigned long flag, struct inode *inode)
{
	clear_bit(flag, &OVL_I(inode)->flags);
}

M
Miklos Szeredi 已提交
397 398 399 400
bool ovl_test_flag(unsigned long flag, struct inode *inode)
{
	return test_bit(flag, &OVL_I(inode)->flags);
}
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431

/**
 * Caller must hold a reference to inode to prevent it from being freed while
 * it is marked inuse.
 */
bool ovl_inuse_trylock(struct dentry *dentry)
{
	struct inode *inode = d_inode(dentry);
	bool locked = false;

	spin_lock(&inode->i_lock);
	if (!(inode->i_state & I_OVL_INUSE)) {
		inode->i_state |= I_OVL_INUSE;
		locked = true;
	}
	spin_unlock(&inode->i_lock);

	return locked;
}

void ovl_inuse_unlock(struct dentry *dentry)
{
	if (dentry) {
		struct inode *inode = d_inode(dentry);

		spin_lock(&inode->i_lock);
		WARN_ON(!(inode->i_state & I_OVL_INUSE));
		inode->i_state &= ~I_OVL_INUSE;
		spin_unlock(&inode->i_lock);
	}
}
432

433
/* Caller must hold OVL_I(inode)->lock */
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
static void ovl_cleanup_index(struct dentry *dentry)
{
	struct inode *dir = ovl_indexdir(dentry->d_sb)->d_inode;
	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
	struct dentry *upperdentry = ovl_dentry_upper(dentry);
	struct dentry *index = NULL;
	struct inode *inode;
	struct qstr name;
	int err;

	err = ovl_get_index_name(lowerdentry, &name);
	if (err)
		goto fail;

	inode = d_inode(upperdentry);
	if (inode->i_nlink != 1) {
		pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
				    upperdentry, inode->i_ino, inode->i_nlink);
		/*
		 * We either have a bug with persistent union nlink or a lower
		 * hardlink was added while overlay is mounted. Adding a lower
		 * hardlink and then unlinking all overlay hardlinks would drop
		 * overlay nlink to zero before all upper inodes are unlinked.
		 * As a safety measure, when that situation is detected, set
		 * the overlay nlink to the index inode nlink minus one for the
		 * index entry itself.
		 */
		set_nlink(d_inode(dentry), inode->i_nlink - 1);
		ovl_set_nlink_upper(dentry);
		goto out;
	}

	inode_lock_nested(dir, I_MUTEX_PARENT);
	/* TODO: whiteout instead of cleanup to block future open by handle */
	index = lookup_one_len(name.name, ovl_indexdir(dentry->d_sb), name.len);
	err = PTR_ERR(index);
	if (!IS_ERR(index))
		err = ovl_cleanup(dir, index);
472 473 474
	else
		index = NULL;

475 476 477 478 479 480 481 482 483 484 485 486 487
	inode_unlock(dir);
	if (err)
		goto fail;

out:
	dput(index);
	return;

fail:
	pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
	goto out;
}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
/*
 * Operations that change overlay inode and upper inode nlink need to be
 * synchronized with copy up for persistent nlink accounting.
 */
int ovl_nlink_start(struct dentry *dentry, bool *locked)
{
	struct ovl_inode *oi = OVL_I(d_inode(dentry));
	const struct cred *old_cred;
	int err;

	if (!d_inode(dentry) || d_is_dir(dentry))
		return 0;

	/*
	 * With inodes index is enabled, we store the union overlay nlink
	 * in an xattr on the index inode. When whiting out lower hardlinks
	 * we need to decrement the overlay persistent nlink, but before the
	 * first copy up, we have no upper index inode to store the xattr.
	 *
	 * As a workaround, before whiteout/rename over of a lower hardlink,
	 * copy up to create the upper index. Creating the upper index will
	 * initialize the overlay nlink, so it could be dropped if unlink
	 * or rename succeeds.
	 *
	 * TODO: implement metadata only index copy up when called with
	 *       ovl_copy_up_flags(dentry, O_PATH).
	 */
	if (ovl_indexdir(dentry->d_sb) && !ovl_dentry_has_upper_alias(dentry) &&
	    d_inode(ovl_dentry_lower(dentry))->i_nlink > 1) {
		err = ovl_copy_up(dentry);
		if (err)
			return err;
	}

	err = mutex_lock_interruptible(&oi->lock);
	if (err)
		return err;

	if (!ovl_test_flag(OVL_INDEX, d_inode(dentry)))
		goto out;

	old_cred = ovl_override_creds(dentry->d_sb);
	/*
	 * The overlay inode nlink should be incremented/decremented IFF the
	 * upper operation succeeds, along with nlink change of upper inode.
	 * Therefore, before link/unlink/rename, we store the union nlink
	 * value relative to the upper inode nlink in an upper inode xattr.
	 */
	err = ovl_set_nlink_upper(dentry);
	revert_creds(old_cred);

out:
	if (err)
		mutex_unlock(&oi->lock);
	else
		*locked = true;

	return err;
}

void ovl_nlink_end(struct dentry *dentry, bool locked)
{
550 551 552 553 554 555 556 557 558 559
	if (locked) {
		if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
		    d_inode(dentry)->i_nlink == 0) {
			const struct cred *old_cred;

			old_cred = ovl_override_creds(dentry->d_sb);
			ovl_cleanup_index(dentry);
			revert_creds(old_cred);
		}

560
		mutex_unlock(&OVL_I(d_inode(dentry))->lock);
561
	}
562
}
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581

int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
{
	/* Workdir should not be the same as upperdir */
	if (workdir == upperdir)
		goto err;

	/* Workdir should not be subdir of upperdir and vice versa */
	if (lock_rename(workdir, upperdir) != NULL)
		goto err_unlock;

	return 0;

err_unlock:
	unlock_rename(workdir, upperdir);
err:
	pr_err("overlayfs: failed to lock workdir+upperdir\n");
	return -EIO;
}