copy_up.c 18.0 KB
Newer Older
M
Miklos Szeredi 已提交
1 2 3 4 5 6 7 8 9
/*
 *
 * Copyright (C) 2011 Novell 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.
 */

10
#include <linux/module.h>
M
Miklos Szeredi 已提交
11 12 13 14 15 16 17
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/file.h>
#include <linux/splice.h>
#include <linux/xattr.h>
#include <linux/security.h>
#include <linux/uaccess.h>
18
#include <linux/sched/signal.h>
19
#include <linux/cred.h>
M
Miklos Szeredi 已提交
20
#include <linux/namei.h>
21 22
#include <linux/fdtable.h>
#include <linux/ratelimit.h>
23
#include <linux/exportfs.h>
M
Miklos Szeredi 已提交
24 25 26 27
#include "overlayfs.h"

#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)

28 29 30 31 32 33 34 35 36 37
static bool __read_mostly ovl_check_copy_up;
module_param_named(check_copy_up, ovl_check_copy_up, bool,
		   S_IWUSR | S_IRUGO);
MODULE_PARM_DESC(ovl_check_copy_up,
		 "Warn on copy-up when causing process also has a R/O fd open");

static int ovl_check_fd(const void *data, struct file *f, unsigned int fd)
{
	const struct dentry *dentry = data;

A
Al Viro 已提交
38
	if (file_inode(f) == d_inode(dentry))
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
		pr_warn_ratelimited("overlayfs: Warning: Copying up %pD, but open R/O on fd %u which will cease to be coherent [pid=%d %s]\n",
				    f, fd, current->pid, current->comm);
	return 0;
}

/*
 * Check the fds open by this process and warn if something like the following
 * scenario is about to occur:
 *
 *	fd1 = open("foo", O_RDONLY);
 *	fd2 = open("foo", O_RDWR);
 */
static void ovl_do_check_copy_up(struct dentry *dentry)
{
	if (ovl_check_copy_up)
		iterate_fd(current->files, 0, ovl_check_fd, dentry);
}

M
Miklos Szeredi 已提交
57 58
int ovl_copy_xattr(struct dentry *old, struct dentry *new)
{
59 60 61
	ssize_t list_size, size, value_size = 0;
	char *buf, *name, *value = NULL;
	int uninitialized_var(error);
62
	size_t slen;
M
Miklos Szeredi 已提交
63

64 65
	if (!(old->d_inode->i_opflags & IOP_XATTR) ||
	    !(new->d_inode->i_opflags & IOP_XATTR))
M
Miklos Szeredi 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
		return 0;

	list_size = vfs_listxattr(old, NULL, 0);
	if (list_size <= 0) {
		if (list_size == -EOPNOTSUPP)
			return 0;
		return list_size;
	}

	buf = kzalloc(list_size, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	list_size = vfs_listxattr(old, buf, list_size);
	if (list_size <= 0) {
		error = list_size;
82
		goto out;
M
Miklos Szeredi 已提交
83 84
	}

85 86 87 88 89 90 91 92 93 94
	for (name = buf; list_size; name += slen) {
		slen = strnlen(name, list_size) + 1;

		/* underlying fs providing us with an broken xattr list? */
		if (WARN_ON(slen > list_size)) {
			error = -EIO;
			break;
		}
		list_size -= slen;

M
Miklos Szeredi 已提交
95 96
		if (ovl_is_private_xattr(name))
			continue;
97 98 99 100 101
retry:
		size = vfs_getxattr(old, name, value, value_size);
		if (size == -ERANGE)
			size = vfs_getxattr(old, name, NULL, 0);

M
Miklos Szeredi 已提交
102
		if (size < 0) {
M
Miklos Szeredi 已提交
103
			error = size;
104
			break;
M
Miklos Szeredi 已提交
105
		}
106 107 108 109 110 111 112 113 114 115 116 117 118 119

		if (size > value_size) {
			void *new;

			new = krealloc(value, size, GFP_KERNEL);
			if (!new) {
				error = -ENOMEM;
				break;
			}
			value = new;
			value_size = size;
			goto retry;
		}

120 121 122 123 124 125 126
		error = security_inode_copy_up_xattr(name);
		if (error < 0 && error != -EOPNOTSUPP)
			break;
		if (error == 1) {
			error = 0;
			continue; /* Discard */
		}
M
Miklos Szeredi 已提交
127 128
		error = vfs_setxattr(new, name, value, size, 0);
		if (error)
129
			break;
M
Miklos Szeredi 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	}
	kfree(value);
out:
	kfree(buf);
	return error;
}

static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
{
	struct file *old_file;
	struct file *new_file;
	loff_t old_pos = 0;
	loff_t new_pos = 0;
	int error = 0;

	if (len == 0)
		return 0;

148
	old_file = ovl_path_open(old, O_LARGEFILE | O_RDONLY);
M
Miklos Szeredi 已提交
149 150 151
	if (IS_ERR(old_file))
		return PTR_ERR(old_file);

152
	new_file = ovl_path_open(new, O_LARGEFILE | O_WRONLY);
M
Miklos Szeredi 已提交
153 154 155 156 157
	if (IS_ERR(new_file)) {
		error = PTR_ERR(new_file);
		goto out_fput;
	}

158 159 160 161 162 163 164
	/* Try to use clone_file_range to clone up within the same fs */
	error = vfs_clone_file_range(old_file, 0, new_file, 0, len);
	if (!error)
		goto out;
	/* Couldn't clone, so now we try to copy the data */
	error = 0;

M
Miklos Szeredi 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	/* FIXME: copy up sparse files efficiently */
	while (len) {
		size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
		long bytes;

		if (len < this_len)
			this_len = len;

		if (signal_pending_state(TASK_KILLABLE, current)) {
			error = -EINTR;
			break;
		}

		bytes = do_splice_direct(old_file, &old_pos,
					 new_file, &new_pos,
					 this_len, SPLICE_F_MOVE);
		if (bytes <= 0) {
			error = bytes;
			break;
		}
		WARN_ON(old_pos != new_pos);

		len -= bytes;
	}
189
out:
M
Miklos Szeredi 已提交
190 191
	if (!error)
		error = vfs_fsync(new_file, 0);
M
Miklos Szeredi 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
	fput(new_file);
out_fput:
	fput(old_file);
	return error;
}

static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
{
	struct iattr attr = {
		.ia_valid =
		     ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
		.ia_atime = stat->atime,
		.ia_mtime = stat->mtime,
	};

	return notify_change(upperdentry, &attr, NULL);
}

int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
{
	int err = 0;

	if (!S_ISLNK(stat->mode)) {
		struct iattr attr = {
			.ia_valid = ATTR_MODE,
			.ia_mode = stat->mode,
		};
		err = notify_change(upperdentry, &attr, NULL);
	}
	if (!err) {
		struct iattr attr = {
			.ia_valid = ATTR_UID | ATTR_GID,
			.ia_uid = stat->uid,
			.ia_gid = stat->gid,
		};
		err = notify_change(upperdentry, &attr, NULL);
	}
	if (!err)
		ovl_set_timestamps(upperdentry, stat);

	return err;
}

235
struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper)
236 237 238 239 240
{
	struct ovl_fh *fh;
	int fh_type, fh_len, dwords;
	void *buf;
	int buflen = MAX_HANDLE_SZ;
241
	uuid_t *uuid = &real->d_sb->s_uuid;
242

243
	buf = kmalloc(buflen, GFP_KERNEL);
244 245 246 247 248 249 250 251 252
	if (!buf)
		return ERR_PTR(-ENOMEM);

	/*
	 * We encode a non-connectable file handle for non-dir, because we
	 * only need to find the lower inode number and we don't want to pay
	 * the price or reconnecting the dentry.
	 */
	dwords = buflen >> 2;
253
	fh_type = exportfs_encode_fh(real, buf, &dwords, 0);
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	buflen = (dwords << 2);

	fh = ERR_PTR(-EIO);
	if (WARN_ON(fh_type < 0) ||
	    WARN_ON(buflen > MAX_HANDLE_SZ) ||
	    WARN_ON(fh_type == FILEID_INVALID))
		goto out;

	BUILD_BUG_ON(MAX_HANDLE_SZ + offsetof(struct ovl_fh, fid) > 255);
	fh_len = offsetof(struct ovl_fh, fid) + buflen;
	fh = kmalloc(fh_len, GFP_KERNEL);
	if (!fh) {
		fh = ERR_PTR(-ENOMEM);
		goto out;
	}

	fh->version = OVL_FH_VERSION;
	fh->magic = OVL_FH_MAGIC;
	fh->type = fh_type;
	fh->flags = OVL_FH_FLAG_CPU_ENDIAN;
274 275 276 277 278 279 280 281
	/*
	 * When we will want to decode an overlay dentry from this handle
	 * and all layers are on the same fs, if we get a disconncted real
	 * dentry when we decode fid, the only way to tell if we should assign
	 * it to upperdentry or to lowerstack is by checking this flag.
	 */
	if (is_upper)
		fh->flags |= OVL_FH_FLAG_PATH_UPPER;
282 283 284 285 286 287 288 289 290
	fh->len = fh_len;
	fh->uuid = *uuid;
	memcpy(fh->fid, buf, buflen);

out:
	kfree(buf);
	return fh;
}

291 292
int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
		   struct dentry *upper)
293 294 295 296 297 298 299 300 301
{
	const struct ovl_fh *fh = NULL;
	int err;

	/*
	 * When lower layer doesn't support export operations store a 'null' fh,
	 * so we can use the overlay.origin xattr to distignuish between a copy
	 * up and a pure upper inode.
	 */
302
	if (ovl_can_decode_fh(lower->d_sb)) {
303
		fh = ovl_encode_real_fh(lower, false);
304 305 306 307
		if (IS_ERR(fh))
			return PTR_ERR(fh);
	}

308 309 310 311 312
	/*
	 * Do not fail when upper doesn't support xattrs.
	 */
	err = ovl_check_setxattr(dentry, upper, OVL_XATTR_ORIGIN, fh,
				 fh ? fh->len : 0, 0);
313 314 315 316 317
	kfree(fh);

	return err;
}

318 319 320 321 322 323
/* Store file handle of @upper dir in @index dir entry */
static int ovl_set_upper_fh(struct dentry *upper, struct dentry *index)
{
	const struct ovl_fh *fh;
	int err;

324
	fh = ovl_encode_real_fh(upper, true);
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	if (IS_ERR(fh))
		return PTR_ERR(fh);

	err = ovl_do_setxattr(index, OVL_XATTR_UPPER, fh, fh->len, 0);

	kfree(fh);
	return err;
}

/*
 * Create and install index entry.
 *
 * Caller must hold i_mutex on indexdir.
 */
static int ovl_create_index(struct dentry *dentry, struct dentry *origin,
			    struct dentry *upper)
{
	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
	struct inode *dir = d_inode(indexdir);
	struct dentry *index = NULL;
	struct dentry *temp = NULL;
	struct qstr name = { };
	int err;

	/*
	 * For now this is only used for creating index entry for directories,
	 * because non-dir are copied up directly to index and then hardlinked
	 * to upper dir.
	 *
	 * TODO: implement create index for non-dir, so we can call it when
	 * encoding file handle for non-dir in case index does not exist.
	 */
	if (WARN_ON(!d_is_dir(dentry)))
		return -EIO;

	/* Directory not expected to be indexed before copy up */
	if (WARN_ON(ovl_test_flag(OVL_INDEX, d_inode(dentry))))
		return -EIO;

	err = ovl_get_index_name(origin, &name);
	if (err)
		return err;

368
	temp = ovl_create_temp(indexdir, OVL_CATTR(S_IFDIR | 0));
369
	err = PTR_ERR(temp);
370
	if (IS_ERR(temp))
371
		goto free_name;
372 373 374

	err = ovl_set_upper_fh(upper, temp);
	if (err)
375
		goto out;
376 377 378 379 380 381 382 383 384

	index = lookup_one_len(name.name, indexdir, name.len);
	if (IS_ERR(index)) {
		err = PTR_ERR(index);
	} else {
		err = ovl_do_rename(dir, temp, dir, index, 0);
		dput(index);
	}
out:
385 386
	if (err)
		ovl_cleanup(dir, temp);
387
	dput(temp);
388
free_name:
389 390 391 392
	kfree(name.name);
	return err;
}

393 394 395 396 397 398 399 400 401 402 403 404
struct ovl_copy_up_ctx {
	struct dentry *parent;
	struct dentry *dentry;
	struct path lowerpath;
	struct kstat stat;
	struct kstat pstat;
	const char *link;
	struct dentry *destdir;
	struct qstr destname;
	struct dentry *workdir;
	bool tmpfile;
	bool origin;
405
	bool indexed;
406 407 408
};

static int ovl_link_up(struct ovl_copy_up_ctx *c)
409 410 411
{
	int err;
	struct dentry *upper;
412
	struct dentry *upperdir = ovl_dentry_upper(c->parent);
413 414
	struct inode *udir = d_inode(upperdir);

415 416 417 418 419 420
	/* Mark parent "impure" because it may now contain non-pure upper */
	err = ovl_set_impure(c->parent, upperdir);
	if (err)
		return err;

	err = ovl_set_nlink_lower(c->dentry);
421 422 423
	if (err)
		return err;

424
	inode_lock_nested(udir, I_MUTEX_PARENT);
425 426
	upper = lookup_one_len(c->dentry->d_name.name, upperdir,
			       c->dentry->d_name.len);
427 428
	err = PTR_ERR(upper);
	if (!IS_ERR(upper)) {
429
		err = ovl_do_link(ovl_dentry_upper(c->dentry), udir, upper);
430 431
		dput(upper);

432 433 434 435 436
		if (!err) {
			/* Restore timestamps on parent (best effort) */
			ovl_set_timestamps(upperdir, &c->pstat);
			ovl_dentry_set_upper_alias(c->dentry);
		}
437 438
	}
	inode_unlock(udir);
439 440 441 442
	if (err)
		return err;

	err = ovl_set_nlink_upper(c->dentry);
443 444 445 446

	return err;
}

447 448
static int ovl_install_temp(struct ovl_copy_up_ctx *c, struct dentry *temp,
			    struct dentry **newdentry)
449 450 451
{
	int err;
	struct dentry *upper;
452
	struct inode *udir = d_inode(c->destdir);
453

454
	upper = lookup_one_len(c->destname.name, c->destdir, c->destname.len);
455 456 457
	if (IS_ERR(upper))
		return PTR_ERR(upper);

458
	if (c->tmpfile)
459
		err = ovl_do_link(temp, udir, upper);
460
	else
461
		err = ovl_do_rename(d_inode(c->workdir), temp, udir, upper, 0);
462

463
	if (!err)
464
		*newdentry = dget(c->tmpfile ? upper : temp);
465 466 467 468 469
	dput(upper);

	return err;
}

470
static struct dentry *ovl_get_tmpfile(struct ovl_copy_up_ctx *c)
M
Miklos Szeredi 已提交
471 472
{
	int err;
473
	struct dentry *temp;
474 475
	const struct cred *old_creds = NULL;
	struct cred *new_creds = NULL;
A
Amir Goldstein 已提交
476
	struct ovl_cattr cattr = {
A
Al Viro 已提交
477
		/* Can't properly set mode on creation because of the umask */
478 479 480
		.mode = c->stat.mode & S_IFMT,
		.rdev = c->stat.rdev,
		.link = c->link
A
Al Viro 已提交
481
	};
M
Miklos Szeredi 已提交
482

483
	err = security_inode_copy_up(c->dentry, &new_creds);
484
	temp = ERR_PTR(err);
485
	if (err < 0)
486
		goto out;
487 488 489 490

	if (new_creds)
		old_creds = override_creds(new_creds);

491
	if (c->tmpfile)
492
		temp = ovl_do_tmpfile(c->workdir, c->stat.mode);
493 494
	else
		temp = ovl_create_temp(c->workdir, &cattr);
495
out:
496 497 498 499 500
	if (new_creds) {
		revert_creds(old_creds);
		put_cred(new_creds);
	}

501
	return temp;
502 503
}

504
static int ovl_copy_up_inode(struct ovl_copy_up_ctx *c, struct dentry *temp)
505 506 507
{
	int err;

508
	if (S_ISREG(c->stat.mode)) {
M
Miklos Szeredi 已提交
509
		struct path upperpath;
510

511
		ovl_path_upper(c->dentry, &upperpath);
M
Miklos Szeredi 已提交
512
		BUG_ON(upperpath.dentry != NULL);
513
		upperpath.dentry = temp;
M
Miklos Szeredi 已提交
514

515
		err = ovl_copy_up_data(&c->lowerpath, &upperpath, c->stat.size);
M
Miklos Szeredi 已提交
516
		if (err)
517
			return err;
M
Miklos Szeredi 已提交
518 519
	}

520
	err = ovl_copy_xattr(c->lowerpath.dentry, temp);
M
Miklos Szeredi 已提交
521
	if (err)
522
		return err;
M
Miklos Szeredi 已提交
523

524
	inode_lock(temp->d_inode);
525
	err = ovl_set_attr(temp, &c->stat);
526
	inode_unlock(temp->d_inode);
M
Miklos Szeredi 已提交
527
	if (err)
528
		return err;
M
Miklos Szeredi 已提交
529

530 531 532
	/*
	 * Store identifier of lower inode in upper inode xattr to
	 * allow lookup of the copy up origin inode.
533 534 535
	 *
	 * Don't set origin when we are breaking the association with a lower
	 * hard link.
536
	 */
537
	if (c->origin) {
538
		err = ovl_set_origin(c->dentry, c->lowerpath.dentry, temp);
539
		if (err)
540
			return err;
541
	}
542

543 544 545
	return 0;
}

546
static int ovl_copy_up_locked(struct ovl_copy_up_ctx *c)
547
{
548
	struct inode *udir = c->destdir->d_inode;
549
	struct inode *inode;
550
	struct dentry *newdentry = NULL;
551
	struct dentry *temp;
552 553
	int err;

554 555 556
	temp = ovl_get_tmpfile(c);
	if (IS_ERR(temp))
		return PTR_ERR(temp);
557

558
	err = ovl_copy_up_inode(c, temp);
559
	if (err)
560
		goto out;
561

562 563 564
	if (S_ISDIR(c->stat.mode) && c->indexed) {
		err = ovl_create_index(c->dentry, c->lowerpath.dentry, temp);
		if (err)
565
			goto out;
566 567
	}

568
	if (c->tmpfile) {
569
		inode_lock_nested(udir, I_MUTEX_PARENT);
570
		err = ovl_install_temp(c, temp, &newdentry);
571 572
		inode_unlock(udir);
	} else {
573
		err = ovl_install_temp(c, temp, &newdentry);
574
	}
M
Miklos Szeredi 已提交
575
	if (err)
576
		goto out;
M
Miklos Szeredi 已提交
577

578 579 580 581 582
	inode = d_inode(c->dentry);
	ovl_inode_update(inode, newdentry);
	if (S_ISDIR(inode->i_mode))
		ovl_set_flag(OVL_WHITEOUTS, inode);

583
out:
584 585
	if (err && !c->tmpfile)
		ovl_cleanup(d_inode(c->workdir), temp);
586
	dput(temp);
M
Miklos Szeredi 已提交
587 588 589 590 591 592 593
	return err;

}

/*
 * Copy up a single dentry
 *
M
Miklos Szeredi 已提交
594 595 596 597 598
 * All renames start with copy up of source if necessary.  The actual
 * rename will only proceed once the copy up was successful.  Copy up uses
 * upper parent i_mutex for exclusion.  Since rename can change d_parent it
 * is possible that the copy up will lock the old parent.  At that point
 * the file will have already been copied up anyway.
M
Miklos Szeredi 已提交
599
 */
M
Miklos Szeredi 已提交
600
static int ovl_do_copy_up(struct ovl_copy_up_ctx *c)
M
Miklos Szeredi 已提交
601 602
{
	int err;
603
	struct ovl_fs *ofs = c->dentry->d_sb->s_fs_info;
604
	bool to_index = false;
605

606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	/*
	 * Indexed non-dir is copied up directly to the index entry and then
	 * hardlinked to upper dir. Indexed dir is copied up to indexdir,
	 * then index entry is created and then copied up dir installed.
	 * Copying dir up to indexdir instead of workdir simplifies locking.
	 */
	if (ovl_need_index(c->dentry)) {
		c->indexed = true;
		if (S_ISDIR(c->stat.mode))
			c->workdir = ovl_indexdir(c->dentry->d_sb);
		else
			to_index = true;
	}

	if (S_ISDIR(c->stat.mode) || c->stat.nlink == 1 || to_index)
621 622
		c->origin = true;

623
	if (to_index) {
624 625 626 627
		c->destdir = ovl_indexdir(c->dentry->d_sb);
		err = ovl_get_index_name(c->lowerpath.dentry, &c->destname);
		if (err)
			return err;
628 629 630
	} else if (WARN_ON(!c->parent)) {
		/* Disconnected dentry must be copied up to index dir */
		return -EIO;
631 632 633 634 635 636 637 638 639
	} else {
		/*
		 * Mark parent "impure" because it may now contain non-pure
		 * upper
		 */
		err = ovl_set_impure(c->parent, c->destdir);
		if (err)
			return err;
	}
M
Miklos Szeredi 已提交
640

641
	/* Should we copyup with O_TMPFILE or with workdir? */
642 643
	if (S_ISREG(c->stat.mode) && ofs->tmpfile) {
		c->tmpfile = true;
644 645
		err = ovl_copy_up_locked(c);
	} else {
646 647
		err = ovl_lock_rename_workdir(c->workdir, c->destdir);
		if (!err) {
648 649 650
			err = ovl_copy_up_locked(c);
			unlock_rename(c->workdir, c->destdir);
		}
651 652
	}

653 654 655 656 657

	if (err)
		goto out;

	if (c->indexed)
658 659 660
		ovl_set_flag(OVL_INDEX, d_inode(c->dentry));

	if (to_index) {
661 662 663
		/* Initialize nlink for copy up of disconnected dentry */
		err = ovl_set_nlink_upper(c->dentry);
	} else {
664 665 666 667 668 669 670 671
		struct inode *udir = d_inode(c->destdir);

		/* Restore timestamps on parent (best effort) */
		inode_lock(udir);
		ovl_set_timestamps(c->destdir, &c->pstat);
		inode_unlock(udir);

		ovl_dentry_set_upper_alias(c->dentry);
M
Miklos Szeredi 已提交
672 673
	}

674 675 676
out:
	if (to_index)
		kfree(c->destname.name);
M
Miklos Szeredi 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	return err;
}

static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
			   int flags)
{
	int err;
	DEFINE_DELAYED_CALL(done);
	struct path parentpath;
	struct ovl_copy_up_ctx ctx = {
		.parent = parent,
		.dentry = dentry,
		.workdir = ovl_workdir(dentry),
	};

	if (WARN_ON(!ctx.workdir))
		return -EROFS;

	ovl_path_lower(dentry, &ctx.lowerpath);
	err = vfs_getattr(&ctx.lowerpath, &ctx.stat,
			  STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
	if (err)
		return err;

701 702 703 704
	if (parent) {
		ovl_path_upper(parent, &parentpath);
		ctx.destdir = parentpath.dentry;
		ctx.destname = dentry->d_name;
M
Miklos Szeredi 已提交
705

706 707 708 709 710 711
		err = vfs_getattr(&parentpath, &ctx.pstat,
				  STATX_ATIME | STATX_MTIME,
				  AT_STATX_SYNC_AS_STAT);
		if (err)
			return err;
	}
M
Miklos Szeredi 已提交
712 713 714 715 716 717 718 719 720 721 722 723

	/* maybe truncate regular file. this has no effect on dirs */
	if (flags & O_TRUNC)
		ctx.stat.size = 0;

	if (S_ISLNK(ctx.stat.mode)) {
		ctx.link = vfs_get_link(ctx.lowerpath.dentry, &done);
		if (IS_ERR(ctx.link))
			return PTR_ERR(ctx.link);
	}
	ovl_do_check_copy_up(ctx.lowerpath.dentry);

M
Miklos Szeredi 已提交
724 725 726 727 728 729
	err = ovl_copy_up_start(dentry);
	/* err < 0: interrupted, err > 0: raced with another copy-up */
	if (unlikely(err)) {
		if (err > 0)
			err = 0;
	} else {
730 731
		if (!ovl_dentry_upper(dentry))
			err = ovl_do_copy_up(&ctx);
732
		if (!err && parent && !ovl_dentry_has_upper_alias(dentry))
733
			err = ovl_link_up(&ctx);
M
Miklos Szeredi 已提交
734 735
		ovl_copy_up_end(dentry);
	}
M
Miklos Szeredi 已提交
736
	do_delayed_call(&done);
M
Miklos Szeredi 已提交
737 738 739 740

	return err;
}

741
int ovl_copy_up_flags(struct dentry *dentry, int flags)
M
Miklos Szeredi 已提交
742
{
743 744
	int err = 0;
	const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
745 746 747 748 749 750 751 752 753
	bool disconnected = (dentry->d_flags & DCACHE_DISCONNECTED);

	/*
	 * With NFS export, copy up can get called for a disconnected non-dir.
	 * In this case, we will copy up lower inode to index dir without
	 * linking it to upper dir.
	 */
	if (WARN_ON(disconnected && d_is_dir(dentry)))
		return -EIO;
M
Miklos Szeredi 已提交
754 755 756

	while (!err) {
		struct dentry *next;
757
		struct dentry *parent = NULL;
M
Miklos Szeredi 已提交
758

759 760 761 762 763 764 765 766 767 768 769 770 771 772
		/*
		 * Check if copy-up has happened as well as for upper alias (in
		 * case of hard links) is there.
		 *
		 * Both checks are lockless:
		 *  - false negatives: will recheck under oi->lock
		 *  - false positives:
		 *    + ovl_dentry_upper() uses memory barriers to ensure the
		 *      upper dentry is up-to-date
		 *    + ovl_dentry_has_upper_alias() relies on locking of
		 *      upper parent i_rwsem to prevent reordering copy-up
		 *      with rename.
		 */
		if (ovl_dentry_upper(dentry) &&
773
		    (ovl_dentry_has_upper_alias(dentry) || disconnected))
M
Miklos Szeredi 已提交
774 775 776 777
			break;

		next = dget(dentry);
		/* find the topmost dentry not yet copied up */
778
		for (; !disconnected;) {
M
Miklos Szeredi 已提交
779 780
			parent = dget_parent(next);

781
			if (ovl_dentry_upper(parent))
M
Miklos Szeredi 已提交
782 783 784 785 786 787
				break;

			dput(next);
			next = parent;
		}

M
Miklos Szeredi 已提交
788
		err = ovl_copy_up_one(parent, next, flags);
M
Miklos Szeredi 已提交
789 790 791 792

		dput(parent);
		dput(next);
	}
793
	revert_creds(old_cred);
M
Miklos Szeredi 已提交
794 795 796

	return err;
}
797 798 799 800 801

int ovl_copy_up(struct dentry *dentry)
{
	return ovl_copy_up_flags(dentry, 0);
}