Linux Audio

Check our new training course

Loading...
v6.13.7
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) 2011 Novell Inc.
   4 * Copyright (C) 2016 Red Hat, Inc.
   5 */
   6
   7#include <linux/fs.h>
   8#include <linux/mount.h>
   9#include <linux/slab.h>
  10#include <linux/cred.h>
  11#include <linux/xattr.h>
  12#include <linux/exportfs.h>
  13#include <linux/file.h>
  14#include <linux/fileattr.h>
  15#include <linux/uuid.h>
  16#include <linux/namei.h>
  17#include <linux/ratelimit.h>
  18#include "overlayfs.h"
  19
  20/* Get write access to upper mnt - may fail if upper sb was remounted ro */
  21int ovl_get_write_access(struct dentry *dentry)
  22{
  23	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  24	return mnt_get_write_access(ovl_upper_mnt(ofs));
  25}
  26
  27/* Get write access to upper sb - may block if upper sb is frozen */
  28void ovl_start_write(struct dentry *dentry)
  29{
  30	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  31	sb_start_write(ovl_upper_mnt(ofs)->mnt_sb);
  32}
  33
  34int ovl_want_write(struct dentry *dentry)
  35{
  36	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  37	return mnt_want_write(ovl_upper_mnt(ofs));
  38}
  39
  40void ovl_put_write_access(struct dentry *dentry)
  41{
  42	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  43	mnt_put_write_access(ovl_upper_mnt(ofs));
  44}
  45
  46void ovl_end_write(struct dentry *dentry)
  47{
  48	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  49	sb_end_write(ovl_upper_mnt(ofs)->mnt_sb);
  50}
  51
  52void ovl_drop_write(struct dentry *dentry)
  53{
  54	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  55	mnt_drop_write(ovl_upper_mnt(ofs));
  56}
  57
  58struct dentry *ovl_workdir(struct dentry *dentry)
  59{
  60	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
  61	return ofs->workdir;
  62}
  63
  64const struct cred *ovl_override_creds(struct super_block *sb)
  65{
  66	struct ovl_fs *ofs = OVL_FS(sb);
  67
  68	return override_creds_light(ofs->creator_cred);
  69}
  70
  71void ovl_revert_creds(const struct cred *old_cred)
  72{
  73	revert_creds_light(old_cred);
  74}
  75
  76/*
  77 * Check if underlying fs supports file handles and try to determine encoding
  78 * type, in order to deduce maximum inode number used by fs.
  79 *
  80 * Return 0 if file handles are not supported.
  81 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
  82 * Return -1 if fs uses a non default encoding with unknown inode size.
  83 */
  84int ovl_can_decode_fh(struct super_block *sb)
  85{
  86	if (!capable(CAP_DAC_READ_SEARCH))
  87		return 0;
  88
  89	if (!exportfs_can_decode_fh(sb->s_export_op))
  90		return 0;
  91
  92	return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
  93}
  94
  95struct dentry *ovl_indexdir(struct super_block *sb)
  96{
  97	struct ovl_fs *ofs = OVL_FS(sb);
  98
  99	return ofs->config.index ? ofs->workdir : NULL;
 100}
 101
 102/* Index all files on copy up. For now only enabled for NFS export */
 103bool ovl_index_all(struct super_block *sb)
 104{
 105	struct ovl_fs *ofs = OVL_FS(sb);
 106
 107	return ofs->config.nfs_export && ofs->config.index;
 108}
 109
 110/* Verify lower origin on lookup. For now only enabled for NFS export */
 111bool ovl_verify_lower(struct super_block *sb)
 112{
 113	struct ovl_fs *ofs = OVL_FS(sb);
 114
 115	return ofs->config.nfs_export && ofs->config.index;
 116}
 117
 118struct ovl_path *ovl_stack_alloc(unsigned int n)
 119{
 120	return kcalloc(n, sizeof(struct ovl_path), GFP_KERNEL);
 121}
 122
 123void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n)
 124{
 125	unsigned int i;
 126
 127	memcpy(dst, src, sizeof(struct ovl_path) * n);
 128	for (i = 0; i < n; i++)
 129		dget(src[i].dentry);
 130}
 131
 132void ovl_stack_put(struct ovl_path *stack, unsigned int n)
 133{
 134	unsigned int i;
 135
 136	for (i = 0; stack && i < n; i++)
 137		dput(stack[i].dentry);
 138}
 139
 140void ovl_stack_free(struct ovl_path *stack, unsigned int n)
 141{
 142	ovl_stack_put(stack, n);
 143	kfree(stack);
 144}
 145
 146struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
 147{
 148	size_t size = offsetof(struct ovl_entry, __lowerstack[numlower]);
 149	struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
 150
 151	if (oe)
 152		oe->__numlower = numlower;
 153
 154	return oe;
 155}
 156
 157void ovl_free_entry(struct ovl_entry *oe)
 158{
 159	ovl_stack_put(ovl_lowerstack(oe), ovl_numlower(oe));
 160	kfree(oe);
 161}
 162
 163#define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
 164
 165bool ovl_dentry_remote(struct dentry *dentry)
 166{
 167	return dentry->d_flags & OVL_D_REVALIDATE;
 168}
 169
 170void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
 171{
 172	if (!ovl_dentry_remote(realdentry))
 173		return;
 174
 175	spin_lock(&dentry->d_lock);
 176	dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
 177	spin_unlock(&dentry->d_lock);
 178}
 179
 180void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
 181			   struct ovl_entry *oe)
 182{
 183	return ovl_dentry_init_flags(dentry, upperdentry, oe, OVL_D_REVALIDATE);
 184}
 185
 186void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
 187			   struct ovl_entry *oe, unsigned int mask)
 188{
 189	struct ovl_path *lowerstack = ovl_lowerstack(oe);
 190	unsigned int i, flags = 0;
 191
 192	if (upperdentry)
 193		flags |= upperdentry->d_flags;
 194	for (i = 0; i < ovl_numlower(oe) && lowerstack[i].dentry; i++)
 195		flags |= lowerstack[i].dentry->d_flags;
 196
 197	spin_lock(&dentry->d_lock);
 198	dentry->d_flags &= ~mask;
 199	dentry->d_flags |= flags & mask;
 200	spin_unlock(&dentry->d_lock);
 201}
 202
 203bool ovl_dentry_weird(struct dentry *dentry)
 204{
 205	if (!d_can_lookup(dentry) && !d_is_file(dentry) && !d_is_symlink(dentry))
 206		return true;
 207
 208	return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
 209				  DCACHE_MANAGE_TRANSIT |
 210				  DCACHE_OP_HASH |
 211				  DCACHE_OP_COMPARE);
 212}
 213
 214enum ovl_path_type ovl_path_type(struct dentry *dentry)
 215{
 216	struct ovl_entry *oe = OVL_E(dentry);
 217	enum ovl_path_type type = 0;
 218
 219	if (ovl_dentry_upper(dentry)) {
 220		type = __OVL_PATH_UPPER;
 221
 222		/*
 223		 * Non-dir dentry can hold lower dentry of its copy up origin.
 224		 */
 225		if (ovl_numlower(oe)) {
 226			if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
 227				type |= __OVL_PATH_ORIGIN;
 228			if (d_is_dir(dentry) ||
 229			    !ovl_has_upperdata(d_inode(dentry)))
 230				type |= __OVL_PATH_MERGE;
 231		}
 232	} else {
 233		if (ovl_numlower(oe) > 1)
 234			type |= __OVL_PATH_MERGE;
 235	}
 236	return type;
 237}
 238
 239void ovl_path_upper(struct dentry *dentry, struct path *path)
 240{
 241	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
 242
 243	path->mnt = ovl_upper_mnt(ofs);
 244	path->dentry = ovl_dentry_upper(dentry);
 245}
 246
 247void ovl_path_lower(struct dentry *dentry, struct path *path)
 248{
 249	struct ovl_entry *oe = OVL_E(dentry);
 250	struct ovl_path *lowerpath = ovl_lowerstack(oe);
 251
 252	if (ovl_numlower(oe)) {
 253		path->mnt = lowerpath->layer->mnt;
 254		path->dentry = lowerpath->dentry;
 255	} else {
 256		*path = (struct path) { };
 257	}
 258}
 259
 260void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
 261{
 262	struct ovl_entry *oe = OVL_E(dentry);
 263	struct ovl_path *lowerdata = ovl_lowerdata(oe);
 264	struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe);
 265
 266	if (lowerdata_dentry) {
 267		path->dentry = lowerdata_dentry;
 268		/*
 269		 * Pairs with smp_wmb() in ovl_dentry_set_lowerdata().
 270		 * Make sure that if lowerdata->dentry is visible, then
 271		 * datapath->layer is visible as well.
 272		 */
 273		smp_rmb();
 274		path->mnt = READ_ONCE(lowerdata->layer)->mnt;
 275	} else {
 276		*path = (struct path) { };
 277	}
 278}
 279
 280enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
 281{
 282	enum ovl_path_type type = ovl_path_type(dentry);
 283
 284	if (!OVL_TYPE_UPPER(type))
 285		ovl_path_lower(dentry, path);
 286	else
 287		ovl_path_upper(dentry, path);
 288
 289	return type;
 290}
 291
 292enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
 293{
 294	enum ovl_path_type type = ovl_path_type(dentry);
 295
 296	WARN_ON_ONCE(d_is_dir(dentry));
 297
 298	if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
 299		ovl_path_lowerdata(dentry, path);
 300	else
 301		ovl_path_upper(dentry, path);
 302
 303	return type;
 304}
 305
 306struct dentry *ovl_dentry_upper(struct dentry *dentry)
 307{
 308	return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
 309}
 310
 311struct dentry *ovl_dentry_lower(struct dentry *dentry)
 312{
 313	struct ovl_entry *oe = OVL_E(dentry);
 314
 315	return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL;
 316}
 317
 318const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
 319{
 320	struct ovl_entry *oe = OVL_E(dentry);
 321
 322	return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL;
 323}
 324
 325/*
 326 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
 327 * depending on what is stored in lowerstack[0]. At times we need to find
 328 * lower dentry which has data (and not metacopy dentry). This helper
 329 * returns the lower data dentry.
 330 */
 331struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
 332{
 333	return ovl_lowerdata_dentry(OVL_E(dentry));
 334}
 335
 336int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath)
 337{
 338	struct ovl_entry *oe = OVL_E(dentry);
 339	struct ovl_path *lowerdata = ovl_lowerdata(oe);
 340	struct dentry *datadentry = datapath->dentry;
 341
 342	if (WARN_ON_ONCE(ovl_numlower(oe) <= 1))
 343		return -EIO;
 344
 345	WRITE_ONCE(lowerdata->layer, datapath->layer);
 346	/*
 347	 * Pairs with smp_rmb() in ovl_path_lowerdata().
 348	 * Make sure that if lowerdata->dentry is visible, then
 349	 * lowerdata->layer is visible as well.
 350	 */
 351	smp_wmb();
 352	WRITE_ONCE(lowerdata->dentry, dget(datadentry));
 353
 354	ovl_dentry_update_reval(dentry, datadentry);
 355
 356	return 0;
 357}
 358
 359struct dentry *ovl_dentry_real(struct dentry *dentry)
 360{
 361	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
 362}
 363
 364struct dentry *ovl_i_dentry_upper(struct inode *inode)
 365{
 366	return ovl_upperdentry_dereference(OVL_I(inode));
 367}
 368
 369struct inode *ovl_i_path_real(struct inode *inode, struct path *path)
 370{
 371	struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
 372
 373	path->dentry = ovl_i_dentry_upper(inode);
 374	if (!path->dentry) {
 375		path->dentry = lowerpath->dentry;
 376		path->mnt = lowerpath->layer->mnt;
 377	} else {
 378		path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
 379	}
 380
 381	return path->dentry ? d_inode_rcu(path->dentry) : NULL;
 382}
 383
 384struct inode *ovl_inode_upper(struct inode *inode)
 385{
 386	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
 387
 388	return upperdentry ? d_inode(upperdentry) : NULL;
 389}
 390
 391struct inode *ovl_inode_lower(struct inode *inode)
 392{
 393	struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
 394
 395	return lowerpath ? d_inode(lowerpath->dentry) : NULL;
 396}
 397
 398struct inode *ovl_inode_real(struct inode *inode)
 399{
 400	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
 401}
 402
 403/* Return inode which contains lower data. Do not return metacopy */
 404struct inode *ovl_inode_lowerdata(struct inode *inode)
 405{
 406	struct dentry *lowerdata = ovl_lowerdata_dentry(OVL_I_E(inode));
 407
 408	if (WARN_ON(!S_ISREG(inode->i_mode)))
 409		return NULL;
 410
 411	return lowerdata ? d_inode(lowerdata) : NULL;
 412}
 413
 414/* Return real inode which contains data. Does not return metacopy inode */
 415struct inode *ovl_inode_realdata(struct inode *inode)
 416{
 417	struct inode *upperinode;
 418
 419	upperinode = ovl_inode_upper(inode);
 420	if (upperinode && ovl_has_upperdata(inode))
 421		return upperinode;
 422
 423	return ovl_inode_lowerdata(inode);
 424}
 425
 426const char *ovl_lowerdata_redirect(struct inode *inode)
 427{
 428	return inode && S_ISREG(inode->i_mode) ?
 429		OVL_I(inode)->lowerdata_redirect : NULL;
 430}
 431
 432struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
 433{
 434	return inode && S_ISDIR(inode->i_mode) ? OVL_I(inode)->cache : NULL;
 435}
 436
 437void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
 438{
 439	OVL_I(inode)->cache = cache;
 440}
 441
 442void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
 443{
 444	set_bit(flag, OVL_E_FLAGS(dentry));
 445}
 446
 447void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
 448{
 449	clear_bit(flag, OVL_E_FLAGS(dentry));
 450}
 451
 452bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
 453{
 454	return test_bit(flag, OVL_E_FLAGS(dentry));
 455}
 456
 457bool ovl_dentry_is_opaque(struct dentry *dentry)
 458{
 459	return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
 460}
 461
 462bool ovl_dentry_is_whiteout(struct dentry *dentry)
 463{
 464	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
 465}
 466
 467void ovl_dentry_set_opaque(struct dentry *dentry)
 468{
 469	ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
 470}
 471
 472bool ovl_dentry_has_xwhiteouts(struct dentry *dentry)
 473{
 474	return ovl_dentry_test_flag(OVL_E_XWHITEOUTS, dentry);
 475}
 476
 477void ovl_dentry_set_xwhiteouts(struct dentry *dentry)
 478{
 479	ovl_dentry_set_flag(OVL_E_XWHITEOUTS, dentry);
 480}
 481
 482/*
 483 * ovl_layer_set_xwhiteouts() is called before adding the overlay dir
 484 * dentry to dcache, while readdir of that same directory happens after
 485 * the overlay dir dentry is in dcache, so if some cpu observes that
 486 * ovl_dentry_is_xwhiteouts(), it will also observe layer->has_xwhiteouts
 487 * for the layers where xwhiteouts marker was found in that merge dir.
 488 */
 489void ovl_layer_set_xwhiteouts(struct ovl_fs *ofs,
 490			      const struct ovl_layer *layer)
 491{
 492	if (layer->has_xwhiteouts)
 493		return;
 494
 495	/* Write once to read-mostly layer properties */
 496	ofs->layers[layer->idx].has_xwhiteouts = true;
 497}
 498
 499/*
 500 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
 501 * to return positive, while there's no actual upper alias for the inode.
 502 * Copy up code needs to know about the existence of the upper alias, so it
 503 * can't use ovl_dentry_upper().
 504 */
 505bool ovl_dentry_has_upper_alias(struct dentry *dentry)
 506{
 507	return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
 508}
 509
 510void ovl_dentry_set_upper_alias(struct dentry *dentry)
 511{
 512	ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
 513}
 514
 515static bool ovl_should_check_upperdata(struct inode *inode)
 516{
 517	if (!S_ISREG(inode->i_mode))
 518		return false;
 519
 520	if (!ovl_inode_lower(inode))
 521		return false;
 522
 523	return true;
 524}
 525
 526bool ovl_has_upperdata(struct inode *inode)
 527{
 528	if (!ovl_should_check_upperdata(inode))
 529		return true;
 530
 531	if (!ovl_test_flag(OVL_UPPERDATA, inode))
 532		return false;
 533	/*
 534	 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
 535	 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
 536	 * if setting of OVL_UPPERDATA is visible, then effects of writes
 537	 * before that are visible too.
 538	 */
 539	smp_rmb();
 540	return true;
 541}
 542
 543void ovl_set_upperdata(struct inode *inode)
 544{
 545	/*
 546	 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
 547	 * if OVL_UPPERDATA flag is visible, then effects of write operations
 548	 * before it are visible as well.
 549	 */
 550	smp_wmb();
 551	ovl_set_flag(OVL_UPPERDATA, inode);
 552}
 553
 554/* Caller should hold ovl_inode->lock */
 555bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
 556{
 557	if (!ovl_open_flags_need_copy_up(flags))
 558		return false;
 559
 560	return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
 561}
 562
 563bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
 564{
 565	if (!ovl_open_flags_need_copy_up(flags))
 566		return false;
 567
 568	return !ovl_has_upperdata(d_inode(dentry));
 569}
 570
 
 
 
 
 
 
 
 571const char *ovl_dentry_get_redirect(struct dentry *dentry)
 572{
 573	return OVL_I(d_inode(dentry))->redirect;
 574}
 575
 576void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
 577{
 578	struct ovl_inode *oi = OVL_I(d_inode(dentry));
 579
 580	kfree(oi->redirect);
 581	oi->redirect = redirect;
 582}
 583
 584void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
 585{
 586	struct inode *upperinode = d_inode(upperdentry);
 587
 588	WARN_ON(OVL_I(inode)->__upperdentry);
 589
 590	/*
 591	 * Make sure upperdentry is consistent before making it visible
 592	 */
 593	smp_wmb();
 594	OVL_I(inode)->__upperdentry = upperdentry;
 595	if (inode_unhashed(inode)) {
 596		inode->i_private = upperinode;
 597		__insert_inode_hash(inode, (unsigned long) upperinode);
 598	}
 599}
 600
 601static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
 602{
 603	struct inode *inode = d_inode(dentry);
 604
 605	WARN_ON(!inode_is_locked(inode));
 606	WARN_ON(!d_is_dir(dentry));
 607	/*
 608	 * Version is used by readdir code to keep cache consistent.
 609	 * For merge dirs (or dirs with origin) all changes need to be noted.
 610	 * For non-merge dirs, cache contains only impure entries (i.e. ones
 611	 * which have been copied up and have origins), so only need to note
 612	 * changes to impure entries.
 613	 */
 614	if (!ovl_dir_is_real(inode) || impurity)
 615		OVL_I(inode)->version++;
 616}
 617
 618void ovl_dir_modified(struct dentry *dentry, bool impurity)
 619{
 620	/* Copy mtime/ctime */
 621	ovl_copyattr(d_inode(dentry));
 622
 623	ovl_dir_version_inc(dentry, impurity);
 624}
 625
 626u64 ovl_inode_version_get(struct inode *inode)
 627{
 
 
 628	WARN_ON(!inode_is_locked(inode));
 629	return OVL_I(inode)->version;
 630}
 631
 632bool ovl_is_whiteout(struct dentry *dentry)
 633{
 634	struct inode *inode = dentry->d_inode;
 635
 636	return inode && IS_WHITEOUT(inode);
 637}
 638
 639/*
 640 * Use this over ovl_is_whiteout for upper and lower files, as it also
 641 * handles overlay.whiteout xattr whiteout files.
 642 */
 643bool ovl_path_is_whiteout(struct ovl_fs *ofs, const struct path *path)
 644{
 645	return ovl_is_whiteout(path->dentry) ||
 646		ovl_path_check_xwhiteout_xattr(ofs, path);
 647}
 648
 649struct file *ovl_path_open(const struct path *path, int flags)
 650{
 651	struct inode *inode = d_inode(path->dentry);
 652	struct mnt_idmap *real_idmap = mnt_idmap(path->mnt);
 653	int err, acc_mode;
 654
 655	if (flags & ~(O_ACCMODE | O_LARGEFILE))
 656		BUG();
 657
 658	switch (flags & O_ACCMODE) {
 659	case O_RDONLY:
 660		acc_mode = MAY_READ;
 661		break;
 662	case O_WRONLY:
 663		acc_mode = MAY_WRITE;
 664		break;
 665	default:
 666		BUG();
 667	}
 668
 669	err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN);
 670	if (err)
 671		return ERR_PTR(err);
 672
 673	/* O_NOATIME is an optimization, don't fail if not permitted */
 674	if (inode_owner_or_capable(real_idmap, inode))
 675		flags |= O_NOATIME;
 676
 677	return dentry_open(path, flags, current_cred());
 678}
 679
 680/* Caller should hold ovl_inode->lock */
 681static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
 682{
 683	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
 684
 685	if (ovl_dentry_upper(dentry) &&
 686	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
 687	    !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
 688		return true;
 689
 690	return false;
 691}
 692
 693bool ovl_already_copied_up(struct dentry *dentry, int flags)
 694{
 695	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
 696
 697	/*
 698	 * Check if copy-up has happened as well as for upper alias (in
 699	 * case of hard links) is there.
 700	 *
 701	 * Both checks are lockless:
 702	 *  - false negatives: will recheck under oi->lock
 703	 *  - false positives:
 704	 *    + ovl_dentry_upper() uses memory barriers to ensure the
 705	 *      upper dentry is up-to-date
 706	 *    + ovl_dentry_has_upper_alias() relies on locking of
 707	 *      upper parent i_rwsem to prevent reordering copy-up
 708	 *      with rename.
 709	 */
 710	if (ovl_dentry_upper(dentry) &&
 711	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
 712	    !ovl_dentry_needs_data_copy_up(dentry, flags))
 713		return true;
 714
 715	return false;
 716}
 717
 718/*
 719 * The copy up "transaction" keeps an elevated mnt write count on upper mnt,
 720 * but leaves taking freeze protection on upper sb to lower level helpers.
 721 */
 722int ovl_copy_up_start(struct dentry *dentry, int flags)
 723{
 724	struct inode *inode = d_inode(dentry);
 725	int err;
 726
 727	err = ovl_inode_lock_interruptible(inode);
 728	if (err)
 729		return err;
 730
 731	if (ovl_already_copied_up_locked(dentry, flags))
 732		err = 1; /* Already copied up */
 733	else
 734		err = ovl_get_write_access(dentry);
 735	if (err)
 736		goto out_unlock;
 737
 738	return 0;
 739
 740out_unlock:
 741	ovl_inode_unlock(inode);
 742	return err;
 743}
 744
 745void ovl_copy_up_end(struct dentry *dentry)
 746{
 747	ovl_put_write_access(dentry);
 748	ovl_inode_unlock(d_inode(dentry));
 749}
 750
 751bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
 752{
 753	int res;
 754
 755	res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
 756
 757	/* Zero size value means "copied up but origin unknown" */
 758	if (res >= 0)
 759		return true;
 760
 761	return false;
 762}
 763
 764bool ovl_path_check_xwhiteout_xattr(struct ovl_fs *ofs, const struct path *path)
 765{
 766	struct dentry *dentry = path->dentry;
 767	int res;
 
 768
 769	/* xattr.whiteout must be a zero size regular file */
 770	if (!d_is_reg(dentry) || i_size_read(d_inode(dentry)) != 0)
 771		return false;
 772
 773	res = ovl_path_getxattr(ofs, path, OVL_XATTR_XWHITEOUT, NULL, 0);
 774	return res >= 0;
 775}
 776
 777/*
 778 * Load persistent uuid from xattr into s_uuid if found, or store a new
 779 * random generated value in s_uuid and in xattr.
 780 */
 781bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
 782			 const struct path *upperpath)
 783{
 784	bool set = false;
 785	uuid_t uuid;
 786	int res;
 787
 788	/* Try to load existing persistent uuid */
 789	res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_UUID, uuid.b,
 790				UUID_SIZE);
 791	if (res == UUID_SIZE)
 792		goto set_uuid;
 793
 794	if (res != -ENODATA)
 795		goto fail;
 796
 797	/*
 798	 * With uuid=auto, if uuid xattr is found, it will be used.
 799	 * If uuid xattrs is not found, generate a persistent uuid only on mount
 800	 * of new overlays where upper root dir is not yet marked as impure.
 801	 * An upper dir is marked as impure on copy up or lookup of its subdirs.
 802	 */
 803	if (ofs->config.uuid == OVL_UUID_AUTO) {
 804		res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_IMPURE, NULL,
 805					0);
 806		if (res > 0) {
 807			/* Any mount of old overlay - downgrade to uuid=null */
 808			ofs->config.uuid = OVL_UUID_NULL;
 809			return true;
 810		} else if (res == -ENODATA) {
 811			/* First mount of new overlay - upgrade to uuid=on */
 812			ofs->config.uuid = OVL_UUID_ON;
 813		} else if (res < 0) {
 814			goto fail;
 815		}
 816
 817	}
 818
 819	/* Generate overlay instance uuid */
 820	uuid_gen(&uuid);
 821
 822	/* Try to store persistent uuid */
 823	set = true;
 824	res = ovl_setxattr(ofs, upperpath->dentry, OVL_XATTR_UUID, uuid.b,
 825			   UUID_SIZE);
 826	if (res)
 827		goto fail;
 828
 829set_uuid:
 830	super_set_uuid(sb, uuid.b, sizeof(uuid));
 831	return true;
 832
 833fail:
 834	ofs->config.uuid = OVL_UUID_NULL;
 835	pr_warn("failed to %s uuid (%pd2, err=%i); falling back to uuid=null.\n",
 836		set ? "set" : "get", upperpath->dentry, res);
 837	return false;
 838}
 839
 840char ovl_get_dir_xattr_val(struct ovl_fs *ofs, const struct path *path,
 841			   enum ovl_xattr ox)
 842{
 843	int res;
 844	char val;
 845
 846	if (!d_is_dir(path->dentry))
 847		return 0;
 848
 849	res = ovl_path_getxattr(ofs, path, ox, &val, 1);
 850	return res == 1 ? val : 0;
 851}
 852
 853#define OVL_XATTR_OPAQUE_POSTFIX	"opaque"
 854#define OVL_XATTR_REDIRECT_POSTFIX	"redirect"
 855#define OVL_XATTR_ORIGIN_POSTFIX	"origin"
 856#define OVL_XATTR_IMPURE_POSTFIX	"impure"
 857#define OVL_XATTR_NLINK_POSTFIX		"nlink"
 858#define OVL_XATTR_UPPER_POSTFIX		"upper"
 859#define OVL_XATTR_UUID_POSTFIX		"uuid"
 860#define OVL_XATTR_METACOPY_POSTFIX	"metacopy"
 861#define OVL_XATTR_PROTATTR_POSTFIX	"protattr"
 862#define OVL_XATTR_XWHITEOUT_POSTFIX	"whiteout"
 863
 864#define OVL_XATTR_TAB_ENTRY(x) \
 865	[x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
 866		[true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
 867
 868const char *const ovl_xattr_table[][2] = {
 869	OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
 870	OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
 871	OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
 872	OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
 873	OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
 874	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
 875	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UUID),
 876	OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
 877	OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
 878	OVL_XATTR_TAB_ENTRY(OVL_XATTR_XWHITEOUT),
 879};
 880
 881int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
 882		       enum ovl_xattr ox, const void *value, size_t size,
 883		       int xerr)
 884{
 885	int err;
 
 886
 887	if (ofs->noxattr)
 888		return xerr;
 889
 890	err = ovl_setxattr(ofs, upperdentry, ox, value, size);
 891
 892	if (err == -EOPNOTSUPP) {
 893		pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
 894		ofs->noxattr = true;
 895		return xerr;
 896	}
 897
 898	return err;
 899}
 900
 901int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
 902{
 903	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
 904	int err;
 905
 906	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
 907		return 0;
 908
 909	/*
 910	 * Do not fail when upper doesn't support xattrs.
 911	 * Upper inodes won't have origin nor redirect xattr anyway.
 912	 */
 913	err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
 
 914	if (!err)
 915		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
 916
 917	return err;
 918}
 919
 920
 921#define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
 922
 923void ovl_check_protattr(struct inode *inode, struct dentry *upper)
 924{
 925	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
 926	u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
 927	char buf[OVL_PROTATTR_MAX+1];
 928	int res, n;
 929
 930	res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
 931				 OVL_PROTATTR_MAX);
 932	if (res < 0)
 933		return;
 934
 935	/*
 936	 * Initialize inode flags from overlay.protattr xattr and upper inode
 937	 * flags.  If upper inode has those fileattr flags set (i.e. from old
 938	 * kernel), we do not clear them on ovl_get_inode(), but we will clear
 939	 * them on next fileattr_set().
 940	 */
 941	for (n = 0; n < res; n++) {
 942		if (buf[n] == 'a')
 943			iflags |= S_APPEND;
 944		else if (buf[n] == 'i')
 945			iflags |= S_IMMUTABLE;
 946		else
 947			break;
 948	}
 949
 950	if (!res || n < res) {
 951		pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
 952				    upper, res);
 953	} else {
 954		inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
 955	}
 956}
 957
 958int ovl_set_protattr(struct inode *inode, struct dentry *upper,
 959		      struct fileattr *fa)
 960{
 961	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
 962	char buf[OVL_PROTATTR_MAX];
 963	int len = 0, err = 0;
 964	u32 iflags = 0;
 965
 966	BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
 967
 968	if (fa->flags & FS_APPEND_FL) {
 969		buf[len++] = 'a';
 970		iflags |= S_APPEND;
 971	}
 972	if (fa->flags & FS_IMMUTABLE_FL) {
 973		buf[len++] = 'i';
 974		iflags |= S_IMMUTABLE;
 975	}
 976
 977	/*
 978	 * Do not allow to set protection flags when upper doesn't support
 979	 * xattrs, because we do not set those fileattr flags on upper inode.
 980	 * Remove xattr if it exist and all protection flags are cleared.
 981	 */
 982	if (len) {
 983		err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
 984					 buf, len, -EPERM);
 985	} else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
 986		err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
 987		if (err == -EOPNOTSUPP || err == -ENODATA)
 988			err = 0;
 989	}
 990	if (err)
 991		return err;
 992
 993	inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
 994
 995	/* Mask out the fileattr flags that should not be set in upper inode */
 996	fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
 997	fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
 998
 999	return 0;
 
 
1000}
1001
1002/*
1003 * Caller must hold a reference to inode to prevent it from being freed while
1004 * it is marked inuse.
1005 */
1006bool ovl_inuse_trylock(struct dentry *dentry)
1007{
1008	struct inode *inode = d_inode(dentry);
1009	bool locked = false;
1010
1011	spin_lock(&inode->i_lock);
1012	if (!(inode->i_state & I_OVL_INUSE)) {
1013		inode->i_state |= I_OVL_INUSE;
1014		locked = true;
1015	}
1016	spin_unlock(&inode->i_lock);
1017
1018	return locked;
1019}
1020
1021void ovl_inuse_unlock(struct dentry *dentry)
1022{
1023	if (dentry) {
1024		struct inode *inode = d_inode(dentry);
1025
1026		spin_lock(&inode->i_lock);
1027		WARN_ON(!(inode->i_state & I_OVL_INUSE));
1028		inode->i_state &= ~I_OVL_INUSE;
1029		spin_unlock(&inode->i_lock);
1030	}
1031}
1032
1033bool ovl_is_inuse(struct dentry *dentry)
1034{
1035	struct inode *inode = d_inode(dentry);
1036	bool inuse;
1037
1038	spin_lock(&inode->i_lock);
1039	inuse = (inode->i_state & I_OVL_INUSE);
1040	spin_unlock(&inode->i_lock);
1041
1042	return inuse;
1043}
1044
1045/*
1046 * Does this overlay dentry need to be indexed on copy up?
1047 */
1048bool ovl_need_index(struct dentry *dentry)
1049{
1050	struct dentry *lower = ovl_dentry_lower(dentry);
1051
1052	if (!lower || !ovl_indexdir(dentry->d_sb))
1053		return false;
1054
1055	/* Index all files for NFS export and consistency verification */
1056	if (ovl_index_all(dentry->d_sb))
1057		return true;
1058
1059	/* Index only lower hardlinks on copy up */
1060	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
1061		return true;
1062
1063	return false;
1064}
1065
1066/* Caller must hold OVL_I(inode)->lock */
1067static void ovl_cleanup_index(struct dentry *dentry)
1068{
1069	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1070	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
1071	struct inode *dir = indexdir->d_inode;
1072	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
1073	struct dentry *upperdentry = ovl_dentry_upper(dentry);
1074	struct dentry *index = NULL;
1075	struct inode *inode;
1076	struct qstr name = { };
1077	bool got_write = false;
1078	int err;
1079
1080	err = ovl_get_index_name(ofs, lowerdentry, &name);
1081	if (err)
1082		goto fail;
1083
1084	err = ovl_want_write(dentry);
1085	if (err)
1086		goto fail;
1087
1088	got_write = true;
1089	inode = d_inode(upperdentry);
1090	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
1091		pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
1092				    upperdentry, inode->i_ino, inode->i_nlink);
1093		/*
1094		 * We either have a bug with persistent union nlink or a lower
1095		 * hardlink was added while overlay is mounted. Adding a lower
1096		 * hardlink and then unlinking all overlay hardlinks would drop
1097		 * overlay nlink to zero before all upper inodes are unlinked.
1098		 * As a safety measure, when that situation is detected, set
1099		 * the overlay nlink to the index inode nlink minus one for the
1100		 * index entry itself.
1101		 */
1102		set_nlink(d_inode(dentry), inode->i_nlink - 1);
1103		ovl_set_nlink_upper(dentry);
1104		goto out;
1105	}
1106
1107	inode_lock_nested(dir, I_MUTEX_PARENT);
1108	index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
1109	err = PTR_ERR(index);
1110	if (IS_ERR(index)) {
1111		index = NULL;
1112	} else if (ovl_index_all(dentry->d_sb)) {
1113		/* Whiteout orphan index to block future open by handle */
1114		err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
1115					       dir, index);
1116	} else {
1117		/* Cleanup orphan index entries */
1118		err = ovl_cleanup(ofs, dir, index);
1119	}
1120
1121	inode_unlock(dir);
1122	if (err)
1123		goto fail;
1124
1125out:
1126	if (got_write)
1127		ovl_drop_write(dentry);
1128	kfree(name.name);
1129	dput(index);
1130	return;
1131
1132fail:
1133	pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
1134	goto out;
1135}
1136
1137/*
1138 * Operations that change overlay inode and upper inode nlink need to be
1139 * synchronized with copy up for persistent nlink accounting.
1140 */
1141int ovl_nlink_start(struct dentry *dentry)
1142{
1143	struct inode *inode = d_inode(dentry);
1144	const struct cred *old_cred;
1145	int err;
1146
1147	if (WARN_ON(!inode))
1148		return -ENOENT;
1149
1150	/*
1151	 * With inodes index is enabled, we store the union overlay nlink
1152	 * in an xattr on the index inode. When whiting out an indexed lower,
1153	 * we need to decrement the overlay persistent nlink, but before the
1154	 * first copy up, we have no upper index inode to store the xattr.
1155	 *
1156	 * As a workaround, before whiteout/rename over an indexed lower,
1157	 * copy up to create the upper index. Creating the upper index will
1158	 * initialize the overlay nlink, so it could be dropped if unlink
1159	 * or rename succeeds.
1160	 *
1161	 * TODO: implement metadata only index copy up when called with
1162	 *       ovl_copy_up_flags(dentry, O_PATH).
1163	 */
1164	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
1165		err = ovl_copy_up(dentry);
1166		if (err)
1167			return err;
1168	}
1169
1170	err = ovl_inode_lock_interruptible(inode);
1171	if (err)
1172		return err;
1173
1174	err = ovl_want_write(dentry);
1175	if (err)
1176		goto out_unlock;
1177
1178	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
1179		return 0;
1180
1181	old_cred = ovl_override_creds(dentry->d_sb);
1182	/*
1183	 * The overlay inode nlink should be incremented/decremented IFF the
1184	 * upper operation succeeds, along with nlink change of upper inode.
1185	 * Therefore, before link/unlink/rename, we store the union nlink
1186	 * value relative to the upper inode nlink in an upper inode xattr.
1187	 */
1188	err = ovl_set_nlink_upper(dentry);
1189	ovl_revert_creds(old_cred);
1190	if (err)
1191		goto out_drop_write;
1192
1193	return 0;
1194
1195out_drop_write:
1196	ovl_drop_write(dentry);
1197out_unlock:
1198	ovl_inode_unlock(inode);
1199
1200	return err;
1201}
1202
1203void ovl_nlink_end(struct dentry *dentry)
1204{
1205	struct inode *inode = d_inode(dentry);
1206
1207	ovl_drop_write(dentry);
1208
1209	if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
1210		const struct cred *old_cred;
1211
1212		old_cred = ovl_override_creds(dentry->d_sb);
1213		ovl_cleanup_index(dentry);
1214		ovl_revert_creds(old_cred);
1215	}
1216
1217	ovl_inode_unlock(inode);
1218}
1219
1220int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
1221{
1222	struct dentry *trap;
1223
1224	/* Workdir should not be the same as upperdir */
1225	if (workdir == upperdir)
1226		goto err;
1227
1228	/* Workdir should not be subdir of upperdir and vice versa */
1229	trap = lock_rename(workdir, upperdir);
1230	if (IS_ERR(trap))
1231		goto err;
1232	if (trap)
1233		goto err_unlock;
1234
1235	return 0;
1236
1237err_unlock:
1238	unlock_rename(workdir, upperdir);
1239err:
1240	pr_err("failed to lock workdir+upperdir\n");
1241	return -EIO;
1242}
1243
1244/*
1245 * err < 0, 0 if no metacopy xattr, metacopy data size if xattr found.
1246 * an empty xattr returns OVL_METACOPY_MIN_SIZE to distinguish from no xattr value.
1247 */
1248int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path,
1249			     struct ovl_metacopy *data)
1250{
1251	int res;
1252
1253	/* Only regular files can have metacopy xattr */
1254	if (!S_ISREG(d_inode(path->dentry)->i_mode))
1255		return 0;
1256
1257	res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY,
1258				data, data ? OVL_METACOPY_MAX_SIZE : 0);
1259	if (res < 0) {
1260		if (res == -ENODATA || res == -EOPNOTSUPP)
1261			return 0;
1262		/*
1263		 * getxattr on user.* may fail with EACCES in case there's no
1264		 * read permission on the inode.  Not much we can do, other than
1265		 * tell the caller that this is not a metacopy inode.
1266		 */
1267		if (ofs->config.userxattr && res == -EACCES)
1268			return 0;
1269		goto out;
1270	}
1271
1272	if (res == 0) {
1273		/* Emulate empty data for zero size metacopy xattr */
1274		res = OVL_METACOPY_MIN_SIZE;
1275		if (data) {
1276			memset(data, 0, res);
1277			data->len = res;
1278		}
1279	} else if (res < OVL_METACOPY_MIN_SIZE) {
1280		pr_warn_ratelimited("metacopy file '%pd' has too small xattr\n",
1281				    path->dentry);
1282		return -EIO;
1283	} else if (data) {
1284		if (data->version != 0) {
1285			pr_warn_ratelimited("metacopy file '%pd' has unsupported version\n",
1286					    path->dentry);
1287			return -EIO;
1288		}
1289		if (res != data->len) {
1290			pr_warn_ratelimited("metacopy file '%pd' has invalid xattr size\n",
1291					    path->dentry);
1292			return -EIO;
1293		}
1294	}
1295
1296	return res;
1297out:
1298	pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
1299	return res;
1300}
1301
1302int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d, struct ovl_metacopy *metacopy)
1303{
1304	size_t len = metacopy->len;
1305
1306	/* If no flags or digest fall back to empty metacopy file */
1307	if (metacopy->version == 0 && metacopy->flags == 0 && metacopy->digest_algo == 0)
1308		len = 0;
1309
1310	return ovl_check_setxattr(ofs, d, OVL_XATTR_METACOPY,
1311				  metacopy, len, -EOPNOTSUPP);
1312}
1313
1314bool ovl_is_metacopy_dentry(struct dentry *dentry)
1315{
1316	struct ovl_entry *oe = OVL_E(dentry);
1317
1318	if (!d_is_reg(dentry))
1319		return false;
1320
1321	if (ovl_dentry_upper(dentry)) {
1322		if (!ovl_has_upperdata(d_inode(dentry)))
1323			return true;
1324		return false;
1325	}
1326
1327	return (ovl_numlower(oe) > 1);
1328}
1329
1330char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
 
1331{
1332	int res;
1333	char *s, *next, *buf = NULL;
1334
1335	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
1336	if (res == -ENODATA || res == -EOPNOTSUPP)
1337		return NULL;
1338	if (res < 0)
1339		goto fail;
1340	if (res == 0)
1341		goto invalid;
 
 
 
 
 
 
 
 
 
 
1342
1343	buf = kzalloc(res + padding + 1, GFP_KERNEL);
1344	if (!buf)
1345		return ERR_PTR(-ENOMEM);
 
 
 
 
 
 
 
 
 
 
1346
1347	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
 
 
1348	if (res < 0)
1349		goto fail;
1350	if (res == 0)
1351		goto invalid;
1352
1353	if (buf[0] == '/') {
1354		for (s = buf; *s++ == '/'; s = next) {
1355			next = strchrnul(s, '/');
1356			if (s == next)
1357				goto invalid;
1358		}
1359	} else {
1360		if (strchr(buf, '/') != NULL)
1361			goto invalid;
1362	}
1363
1364	return buf;
1365invalid:
1366	pr_warn_ratelimited("invalid redirect (%s)\n", buf);
1367	res = -EINVAL;
1368	goto err_free;
1369fail:
1370	pr_warn_ratelimited("failed to get redirect (%i)\n", res);
1371err_free:
1372	kfree(buf);
1373	return ERR_PTR(res);
1374}
1375
1376/* Call with mounter creds as it may open the file */
1377int ovl_ensure_verity_loaded(struct path *datapath)
1378{
1379	struct inode *inode = d_inode(datapath->dentry);
1380	struct file *filp;
1381
1382	if (!fsverity_active(inode) && IS_VERITY(inode)) {
1383		/*
1384		 * If this inode was not yet opened, the verity info hasn't been
1385		 * loaded yet, so we need to do that here to force it into memory.
1386		 */
1387		filp = kernel_file_open(datapath, O_RDONLY, current_cred());
1388		if (IS_ERR(filp))
1389			return PTR_ERR(filp);
1390		fput(filp);
1391	}
1392
1393	return 0;
1394}
1395
1396int ovl_validate_verity(struct ovl_fs *ofs,
1397			struct path *metapath,
1398			struct path *datapath)
1399{
1400	struct ovl_metacopy metacopy_data;
1401	u8 actual_digest[FS_VERITY_MAX_DIGEST_SIZE];
1402	int xattr_digest_size, digest_size;
1403	int xattr_size, err;
1404	u8 verity_algo;
1405
1406	if (!ofs->config.verity_mode ||
1407	    /* Verity only works on regular files */
1408	    !S_ISREG(d_inode(metapath->dentry)->i_mode))
1409		return 0;
1410
1411	xattr_size = ovl_check_metacopy_xattr(ofs, metapath, &metacopy_data);
1412	if (xattr_size < 0)
1413		return xattr_size;
1414
1415	if (!xattr_size || !metacopy_data.digest_algo) {
1416		if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1417			pr_warn_ratelimited("metacopy file '%pd' has no digest specified\n",
1418					    metapath->dentry);
1419			return -EIO;
1420		}
1421		return 0;
1422	}
1423
1424	xattr_digest_size = ovl_metadata_digest_size(&metacopy_data);
1425
1426	err = ovl_ensure_verity_loaded(datapath);
1427	if (err < 0) {
1428		pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1429				    datapath->dentry);
1430		return -EIO;
1431	}
1432
1433	digest_size = fsverity_get_digest(d_inode(datapath->dentry), actual_digest,
1434					  &verity_algo, NULL);
1435	if (digest_size == 0) {
1436		pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n", datapath->dentry);
1437		return -EIO;
1438	}
1439
1440	if (xattr_digest_size != digest_size ||
1441	    metacopy_data.digest_algo != verity_algo ||
1442	    memcmp(metacopy_data.digest, actual_digest, xattr_digest_size) != 0) {
1443		pr_warn_ratelimited("lower file '%pd' has the wrong fs-verity digest\n",
1444				    datapath->dentry);
1445		return -EIO;
1446	}
1447
1448	return 0;
1449}
1450
1451int ovl_get_verity_digest(struct ovl_fs *ofs, struct path *src,
1452			  struct ovl_metacopy *metacopy)
1453{
1454	int err, digest_size;
1455
1456	if (!ofs->config.verity_mode || !S_ISREG(d_inode(src->dentry)->i_mode))
1457		return 0;
1458
1459	err = ovl_ensure_verity_loaded(src);
1460	if (err < 0) {
1461		pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1462				    src->dentry);
1463		return -EIO;
1464	}
1465
1466	digest_size = fsverity_get_digest(d_inode(src->dentry),
1467					  metacopy->digest, &metacopy->digest_algo, NULL);
1468	if (digest_size == 0 ||
1469	    WARN_ON_ONCE(digest_size > FS_VERITY_MAX_DIGEST_SIZE)) {
1470		if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1471			pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n",
1472					    src->dentry);
1473			return -EIO;
1474		}
1475		return 0;
1476	}
1477
1478	metacopy->len += digest_size;
1479	return 0;
1480}
1481
1482/*
1483 * ovl_sync_status() - Check fs sync status for volatile mounts
1484 *
1485 * Returns 1 if this is not a volatile mount and a real sync is required.
1486 *
1487 * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1488 * have occurred on the upperdir since the mount.
1489 *
1490 * Returns -errno if it is a volatile mount, and the error that occurred since
1491 * the last mount. If the error code changes, it'll return the latest error
1492 * code.
1493 */
1494
1495int ovl_sync_status(struct ovl_fs *ofs)
1496{
1497	struct vfsmount *mnt;
1498
1499	if (ovl_should_sync(ofs))
1500		return 1;
1501
1502	mnt = ovl_upper_mnt(ofs);
1503	if (!mnt)
1504		return 0;
1505
1506	return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1507}
1508
1509/*
1510 * ovl_copyattr() - copy inode attributes from layer to ovl inode
1511 *
1512 * When overlay copies inode information from an upper or lower layer to the
1513 * relevant overlay inode it will apply the idmapping of the upper or lower
1514 * layer when doing so ensuring that the ovl inode ownership will correctly
1515 * reflect the ownership of the idmapped upper or lower layer. For example, an
1516 * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
1517 * map any lower or upper inode owned by id 1001 to id 1000. These mapping
1518 * helpers are nops when the relevant layer isn't idmapped.
1519 */
1520void ovl_copyattr(struct inode *inode)
1521{
1522	struct path realpath;
1523	struct inode *realinode;
1524	struct mnt_idmap *real_idmap;
1525	vfsuid_t vfsuid;
1526	vfsgid_t vfsgid;
1527
1528	realinode = ovl_i_path_real(inode, &realpath);
1529	real_idmap = mnt_idmap(realpath.mnt);
1530
1531	spin_lock(&inode->i_lock);
1532	vfsuid = i_uid_into_vfsuid(real_idmap, realinode);
1533	vfsgid = i_gid_into_vfsgid(real_idmap, realinode);
1534
1535	inode->i_uid = vfsuid_into_kuid(vfsuid);
1536	inode->i_gid = vfsgid_into_kgid(vfsgid);
1537	inode->i_mode = realinode->i_mode;
1538	inode_set_atime_to_ts(inode, inode_get_atime(realinode));
1539	inode_set_mtime_to_ts(inode, inode_get_mtime(realinode));
1540	inode_set_ctime_to_ts(inode, inode_get_ctime(realinode));
1541	i_size_write(inode, i_size_read(realinode));
1542	spin_unlock(&inode->i_lock);
1543}
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Copyright (C) 2011 Novell Inc.
  4 * Copyright (C) 2016 Red Hat, Inc.
  5 */
  6
  7#include <linux/fs.h>
  8#include <linux/mount.h>
  9#include <linux/slab.h>
 10#include <linux/cred.h>
 11#include <linux/xattr.h>
 12#include <linux/exportfs.h>
 
 
 13#include <linux/uuid.h>
 14#include <linux/namei.h>
 15#include <linux/ratelimit.h>
 16#include "overlayfs.h"
 17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 18int ovl_want_write(struct dentry *dentry)
 19{
 20	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
 21	return mnt_want_write(ovl_upper_mnt(ofs));
 22}
 23
 
 
 
 
 
 
 
 
 
 
 
 
 24void ovl_drop_write(struct dentry *dentry)
 25{
 26	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
 27	mnt_drop_write(ovl_upper_mnt(ofs));
 28}
 29
 30struct dentry *ovl_workdir(struct dentry *dentry)
 31{
 32	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
 33	return ofs->workdir;
 34}
 35
 36const struct cred *ovl_override_creds(struct super_block *sb)
 37{
 38	struct ovl_fs *ofs = sb->s_fs_info;
 39
 40	return override_creds(ofs->creator_cred);
 
 
 
 
 
 41}
 42
 43/*
 44 * Check if underlying fs supports file handles and try to determine encoding
 45 * type, in order to deduce maximum inode number used by fs.
 46 *
 47 * Return 0 if file handles are not supported.
 48 * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
 49 * Return -1 if fs uses a non default encoding with unknown inode size.
 50 */
 51int ovl_can_decode_fh(struct super_block *sb)
 52{
 53	if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
 
 
 
 54		return 0;
 55
 56	return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
 57}
 58
 59struct dentry *ovl_indexdir(struct super_block *sb)
 60{
 61	struct ovl_fs *ofs = sb->s_fs_info;
 62
 63	return ofs->indexdir;
 64}
 65
 66/* Index all files on copy up. For now only enabled for NFS export */
 67bool ovl_index_all(struct super_block *sb)
 68{
 69	struct ovl_fs *ofs = sb->s_fs_info;
 70
 71	return ofs->config.nfs_export && ofs->config.index;
 72}
 73
 74/* Verify lower origin on lookup. For now only enabled for NFS export */
 75bool ovl_verify_lower(struct super_block *sb)
 76{
 77	struct ovl_fs *ofs = sb->s_fs_info;
 78
 79	return ofs->config.nfs_export && ofs->config.index;
 80}
 81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 82struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
 83{
 84	size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
 85	struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
 86
 87	if (oe)
 88		oe->numlower = numlower;
 89
 90	return oe;
 91}
 92
 
 
 
 
 
 
 
 
 93bool ovl_dentry_remote(struct dentry *dentry)
 94{
 95	return dentry->d_flags &
 96		(DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 97}
 98
 99void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
100			     unsigned int mask)
101{
102	struct ovl_entry *oe = OVL_E(dentry);
103	unsigned int i, flags = 0;
104
105	if (upperdentry)
106		flags |= upperdentry->d_flags;
107	for (i = 0; i < oe->numlower; i++)
108		flags |= oe->lowerstack[i].dentry->d_flags;
109
110	spin_lock(&dentry->d_lock);
111	dentry->d_flags &= ~mask;
112	dentry->d_flags |= flags & mask;
113	spin_unlock(&dentry->d_lock);
114}
115
116bool ovl_dentry_weird(struct dentry *dentry)
117{
 
 
 
118	return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
119				  DCACHE_MANAGE_TRANSIT |
120				  DCACHE_OP_HASH |
121				  DCACHE_OP_COMPARE);
122}
123
124enum ovl_path_type ovl_path_type(struct dentry *dentry)
125{
126	struct ovl_entry *oe = dentry->d_fsdata;
127	enum ovl_path_type type = 0;
128
129	if (ovl_dentry_upper(dentry)) {
130		type = __OVL_PATH_UPPER;
131
132		/*
133		 * Non-dir dentry can hold lower dentry of its copy up origin.
134		 */
135		if (oe->numlower) {
136			if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
137				type |= __OVL_PATH_ORIGIN;
138			if (d_is_dir(dentry) ||
139			    !ovl_has_upperdata(d_inode(dentry)))
140				type |= __OVL_PATH_MERGE;
141		}
142	} else {
143		if (oe->numlower > 1)
144			type |= __OVL_PATH_MERGE;
145	}
146	return type;
147}
148
149void ovl_path_upper(struct dentry *dentry, struct path *path)
150{
151	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
152
153	path->mnt = ovl_upper_mnt(ofs);
154	path->dentry = ovl_dentry_upper(dentry);
155}
156
157void ovl_path_lower(struct dentry *dentry, struct path *path)
158{
159	struct ovl_entry *oe = dentry->d_fsdata;
 
160
161	if (oe->numlower) {
162		path->mnt = oe->lowerstack[0].layer->mnt;
163		path->dentry = oe->lowerstack[0].dentry;
164	} else {
165		*path = (struct path) { };
166	}
167}
168
169void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
170{
171	struct ovl_entry *oe = dentry->d_fsdata;
 
 
172
173	if (oe->numlower) {
174		path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
175		path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
 
 
 
 
 
 
176	} else {
177		*path = (struct path) { };
178	}
179}
180
181enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
182{
183	enum ovl_path_type type = ovl_path_type(dentry);
184
185	if (!OVL_TYPE_UPPER(type))
186		ovl_path_lower(dentry, path);
187	else
188		ovl_path_upper(dentry, path);
189
190	return type;
191}
192
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193struct dentry *ovl_dentry_upper(struct dentry *dentry)
194{
195	return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
196}
197
198struct dentry *ovl_dentry_lower(struct dentry *dentry)
199{
200	struct ovl_entry *oe = dentry->d_fsdata;
201
202	return oe->numlower ? oe->lowerstack[0].dentry : NULL;
203}
204
205const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
206{
207	struct ovl_entry *oe = dentry->d_fsdata;
208
209	return oe->numlower ? oe->lowerstack[0].layer : NULL;
210}
211
212/*
213 * ovl_dentry_lower() could return either a data dentry or metacopy dentry
214 * dependig on what is stored in lowerstack[0]. At times we need to find
215 * lower dentry which has data (and not metacopy dentry). This helper
216 * returns the lower data dentry.
217 */
218struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
219{
220	struct ovl_entry *oe = dentry->d_fsdata;
 
221
222	return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223}
224
225struct dentry *ovl_dentry_real(struct dentry *dentry)
226{
227	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
228}
229
230struct dentry *ovl_i_dentry_upper(struct inode *inode)
231{
232	return ovl_upperdentry_dereference(OVL_I(inode));
233}
234
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235struct inode *ovl_inode_upper(struct inode *inode)
236{
237	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
238
239	return upperdentry ? d_inode(upperdentry) : NULL;
240}
241
242struct inode *ovl_inode_lower(struct inode *inode)
243{
244	return OVL_I(inode)->lower;
 
 
245}
246
247struct inode *ovl_inode_real(struct inode *inode)
248{
249	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
250}
251
252/* Return inode which contains lower data. Do not return metacopy */
253struct inode *ovl_inode_lowerdata(struct inode *inode)
254{
 
 
255	if (WARN_ON(!S_ISREG(inode->i_mode)))
256		return NULL;
257
258	return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
259}
260
261/* Return real inode which contains data. Does not return metacopy inode */
262struct inode *ovl_inode_realdata(struct inode *inode)
263{
264	struct inode *upperinode;
265
266	upperinode = ovl_inode_upper(inode);
267	if (upperinode && ovl_has_upperdata(inode))
268		return upperinode;
269
270	return ovl_inode_lowerdata(inode);
271}
272
 
 
 
 
 
 
273struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
274{
275	return OVL_I(inode)->cache;
276}
277
278void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
279{
280	OVL_I(inode)->cache = cache;
281}
282
283void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
284{
285	set_bit(flag, &OVL_E(dentry)->flags);
286}
287
288void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
289{
290	clear_bit(flag, &OVL_E(dentry)->flags);
291}
292
293bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
294{
295	return test_bit(flag, &OVL_E(dentry)->flags);
296}
297
298bool ovl_dentry_is_opaque(struct dentry *dentry)
299{
300	return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
301}
302
303bool ovl_dentry_is_whiteout(struct dentry *dentry)
304{
305	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
306}
307
308void ovl_dentry_set_opaque(struct dentry *dentry)
309{
310	ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
311}
312
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313/*
314 * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
315 * to return positive, while there's no actual upper alias for the inode.
316 * Copy up code needs to know about the existence of the upper alias, so it
317 * can't use ovl_dentry_upper().
318 */
319bool ovl_dentry_has_upper_alias(struct dentry *dentry)
320{
321	return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
322}
323
324void ovl_dentry_set_upper_alias(struct dentry *dentry)
325{
326	ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
327}
328
329static bool ovl_should_check_upperdata(struct inode *inode)
330{
331	if (!S_ISREG(inode->i_mode))
332		return false;
333
334	if (!ovl_inode_lower(inode))
335		return false;
336
337	return true;
338}
339
340bool ovl_has_upperdata(struct inode *inode)
341{
342	if (!ovl_should_check_upperdata(inode))
343		return true;
344
345	if (!ovl_test_flag(OVL_UPPERDATA, inode))
346		return false;
347	/*
348	 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
349	 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
350	 * if setting of OVL_UPPERDATA is visible, then effects of writes
351	 * before that are visible too.
352	 */
353	smp_rmb();
354	return true;
355}
356
357void ovl_set_upperdata(struct inode *inode)
358{
359	/*
360	 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
361	 * if OVL_UPPERDATA flag is visible, then effects of write operations
362	 * before it are visible as well.
363	 */
364	smp_wmb();
365	ovl_set_flag(OVL_UPPERDATA, inode);
366}
367
368/* Caller should hold ovl_inode->lock */
369bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
370{
371	if (!ovl_open_flags_need_copy_up(flags))
372		return false;
373
374	return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
375}
376
377bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
378{
379	if (!ovl_open_flags_need_copy_up(flags))
380		return false;
381
382	return !ovl_has_upperdata(d_inode(dentry));
383}
384
385bool ovl_redirect_dir(struct super_block *sb)
386{
387	struct ovl_fs *ofs = sb->s_fs_info;
388
389	return ofs->config.redirect_dir && !ofs->noxattr;
390}
391
392const char *ovl_dentry_get_redirect(struct dentry *dentry)
393{
394	return OVL_I(d_inode(dentry))->redirect;
395}
396
397void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
398{
399	struct ovl_inode *oi = OVL_I(d_inode(dentry));
400
401	kfree(oi->redirect);
402	oi->redirect = redirect;
403}
404
405void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
406{
407	struct inode *upperinode = d_inode(upperdentry);
408
409	WARN_ON(OVL_I(inode)->__upperdentry);
410
411	/*
412	 * Make sure upperdentry is consistent before making it visible
413	 */
414	smp_wmb();
415	OVL_I(inode)->__upperdentry = upperdentry;
416	if (inode_unhashed(inode)) {
417		inode->i_private = upperinode;
418		__insert_inode_hash(inode, (unsigned long) upperinode);
419	}
420}
421
422static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
423{
424	struct inode *inode = d_inode(dentry);
425
426	WARN_ON(!inode_is_locked(inode));
 
427	/*
428	 * Version is used by readdir code to keep cache consistent.  For merge
429	 * dirs all changes need to be noted.  For non-merge dirs, cache only
430	 * contains impure (ones which have been copied up and have origins)
431	 * entries, so only need to note changes to impure entries.
 
432	 */
433	if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
434		OVL_I(inode)->version++;
435}
436
437void ovl_dir_modified(struct dentry *dentry, bool impurity)
438{
439	/* Copy mtime/ctime */
440	ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
441
442	ovl_dentry_version_inc(dentry, impurity);
443}
444
445u64 ovl_dentry_version_get(struct dentry *dentry)
446{
447	struct inode *inode = d_inode(dentry);
448
449	WARN_ON(!inode_is_locked(inode));
450	return OVL_I(inode)->version;
451}
452
453bool ovl_is_whiteout(struct dentry *dentry)
454{
455	struct inode *inode = dentry->d_inode;
456
457	return inode && IS_WHITEOUT(inode);
458}
459
460struct file *ovl_path_open(struct path *path, int flags)
 
 
 
 
 
 
 
 
 
 
461{
462	struct inode *inode = d_inode(path->dentry);
 
463	int err, acc_mode;
464
465	if (flags & ~(O_ACCMODE | O_LARGEFILE))
466		BUG();
467
468	switch (flags & O_ACCMODE) {
469	case O_RDONLY:
470		acc_mode = MAY_READ;
471		break;
472	case O_WRONLY:
473		acc_mode = MAY_WRITE;
474		break;
475	default:
476		BUG();
477	}
478
479	err = inode_permission(inode, acc_mode | MAY_OPEN);
480	if (err)
481		return ERR_PTR(err);
482
483	/* O_NOATIME is an optimization, don't fail if not permitted */
484	if (inode_owner_or_capable(inode))
485		flags |= O_NOATIME;
486
487	return dentry_open(path, flags, current_cred());
488}
489
490/* Caller should hold ovl_inode->lock */
491static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
492{
493	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
494
495	if (ovl_dentry_upper(dentry) &&
496	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
497	    !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
498		return true;
499
500	return false;
501}
502
503bool ovl_already_copied_up(struct dentry *dentry, int flags)
504{
505	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
506
507	/*
508	 * Check if copy-up has happened as well as for upper alias (in
509	 * case of hard links) is there.
510	 *
511	 * Both checks are lockless:
512	 *  - false negatives: will recheck under oi->lock
513	 *  - false positives:
514	 *    + ovl_dentry_upper() uses memory barriers to ensure the
515	 *      upper dentry is up-to-date
516	 *    + ovl_dentry_has_upper_alias() relies on locking of
517	 *      upper parent i_rwsem to prevent reordering copy-up
518	 *      with rename.
519	 */
520	if (ovl_dentry_upper(dentry) &&
521	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
522	    !ovl_dentry_needs_data_copy_up(dentry, flags))
523		return true;
524
525	return false;
526}
527
 
 
 
 
528int ovl_copy_up_start(struct dentry *dentry, int flags)
529{
530	struct inode *inode = d_inode(dentry);
531	int err;
532
533	err = ovl_inode_lock_interruptible(inode);
534	if (!err && ovl_already_copied_up_locked(dentry, flags)) {
 
 
 
535		err = 1; /* Already copied up */
536		ovl_inode_unlock(inode);
537	}
 
 
 
 
538
 
 
539	return err;
540}
541
542void ovl_copy_up_end(struct dentry *dentry)
543{
 
544	ovl_inode_unlock(d_inode(dentry));
545}
546
547bool ovl_check_origin_xattr(struct dentry *dentry)
548{
549	int res;
550
551	res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
552
553	/* Zero size value means "copied up but origin unknown" */
554	if (res >= 0)
555		return true;
556
557	return false;
558}
559
560bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
561{
 
562	int res;
563	char val;
564
565	if (!d_is_dir(dentry))
 
566		return false;
567
568	res = vfs_getxattr(dentry, name, &val, 1);
569	if (res == 1 && val == 'y')
570		return true;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
571
 
 
 
 
572	return false;
573}
574
575int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
576		       const char *name, const void *value, size_t size,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
577		       int xerr)
578{
579	int err;
580	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
581
582	if (ofs->noxattr)
583		return xerr;
584
585	err = ovl_do_setxattr(upperdentry, name, value, size, 0);
586
587	if (err == -EOPNOTSUPP) {
588		pr_warn("cannot set %s xattr on upper\n", name);
589		ofs->noxattr = true;
590		return xerr;
591	}
592
593	return err;
594}
595
596int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
597{
 
598	int err;
599
600	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
601		return 0;
602
603	/*
604	 * Do not fail when upper doesn't support xattrs.
605	 * Upper inodes won't have origin nor redirect xattr anyway.
606	 */
607	err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
608				 "y", 1, 0);
609	if (!err)
610		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
611
612	return err;
613}
614
615void ovl_set_flag(unsigned long flag, struct inode *inode)
 
 
 
616{
617	set_bit(flag, &OVL_I(inode)->flags);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618}
619
620void ovl_clear_flag(unsigned long flag, struct inode *inode)
 
621{
622	clear_bit(flag, &OVL_I(inode)->flags);
623}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
624
625bool ovl_test_flag(unsigned long flag, struct inode *inode)
626{
627	return test_bit(flag, &OVL_I(inode)->flags);
628}
629
630/**
631 * Caller must hold a reference to inode to prevent it from being freed while
632 * it is marked inuse.
633 */
634bool ovl_inuse_trylock(struct dentry *dentry)
635{
636	struct inode *inode = d_inode(dentry);
637	bool locked = false;
638
639	spin_lock(&inode->i_lock);
640	if (!(inode->i_state & I_OVL_INUSE)) {
641		inode->i_state |= I_OVL_INUSE;
642		locked = true;
643	}
644	spin_unlock(&inode->i_lock);
645
646	return locked;
647}
648
649void ovl_inuse_unlock(struct dentry *dentry)
650{
651	if (dentry) {
652		struct inode *inode = d_inode(dentry);
653
654		spin_lock(&inode->i_lock);
655		WARN_ON(!(inode->i_state & I_OVL_INUSE));
656		inode->i_state &= ~I_OVL_INUSE;
657		spin_unlock(&inode->i_lock);
658	}
659}
660
661bool ovl_is_inuse(struct dentry *dentry)
662{
663	struct inode *inode = d_inode(dentry);
664	bool inuse;
665
666	spin_lock(&inode->i_lock);
667	inuse = (inode->i_state & I_OVL_INUSE);
668	spin_unlock(&inode->i_lock);
669
670	return inuse;
671}
672
673/*
674 * Does this overlay dentry need to be indexed on copy up?
675 */
676bool ovl_need_index(struct dentry *dentry)
677{
678	struct dentry *lower = ovl_dentry_lower(dentry);
679
680	if (!lower || !ovl_indexdir(dentry->d_sb))
681		return false;
682
683	/* Index all files for NFS export and consistency verification */
684	if (ovl_index_all(dentry->d_sb))
685		return true;
686
687	/* Index only lower hardlinks on copy up */
688	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
689		return true;
690
691	return false;
692}
693
694/* Caller must hold OVL_I(inode)->lock */
695static void ovl_cleanup_index(struct dentry *dentry)
696{
 
697	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
698	struct inode *dir = indexdir->d_inode;
699	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
700	struct dentry *upperdentry = ovl_dentry_upper(dentry);
701	struct dentry *index = NULL;
702	struct inode *inode;
703	struct qstr name = { };
 
704	int err;
705
706	err = ovl_get_index_name(lowerdentry, &name);
707	if (err)
708		goto fail;
709
 
 
 
 
 
710	inode = d_inode(upperdentry);
711	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
712		pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
713				    upperdentry, inode->i_ino, inode->i_nlink);
714		/*
715		 * We either have a bug with persistent union nlink or a lower
716		 * hardlink was added while overlay is mounted. Adding a lower
717		 * hardlink and then unlinking all overlay hardlinks would drop
718		 * overlay nlink to zero before all upper inodes are unlinked.
719		 * As a safety measure, when that situation is detected, set
720		 * the overlay nlink to the index inode nlink minus one for the
721		 * index entry itself.
722		 */
723		set_nlink(d_inode(dentry), inode->i_nlink - 1);
724		ovl_set_nlink_upper(dentry);
725		goto out;
726	}
727
728	inode_lock_nested(dir, I_MUTEX_PARENT);
729	index = lookup_one_len(name.name, indexdir, name.len);
730	err = PTR_ERR(index);
731	if (IS_ERR(index)) {
732		index = NULL;
733	} else if (ovl_index_all(dentry->d_sb)) {
734		/* Whiteout orphan index to block future open by handle */
735		err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
736					       dir, index);
737	} else {
738		/* Cleanup orphan index entries */
739		err = ovl_cleanup(dir, index);
740	}
741
742	inode_unlock(dir);
743	if (err)
744		goto fail;
745
746out:
 
 
747	kfree(name.name);
748	dput(index);
749	return;
750
751fail:
752	pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
753	goto out;
754}
755
756/*
757 * Operations that change overlay inode and upper inode nlink need to be
758 * synchronized with copy up for persistent nlink accounting.
759 */
760int ovl_nlink_start(struct dentry *dentry)
761{
762	struct inode *inode = d_inode(dentry);
763	const struct cred *old_cred;
764	int err;
765
766	if (WARN_ON(!inode))
767		return -ENOENT;
768
769	/*
770	 * With inodes index is enabled, we store the union overlay nlink
771	 * in an xattr on the index inode. When whiting out an indexed lower,
772	 * we need to decrement the overlay persistent nlink, but before the
773	 * first copy up, we have no upper index inode to store the xattr.
774	 *
775	 * As a workaround, before whiteout/rename over an indexed lower,
776	 * copy up to create the upper index. Creating the upper index will
777	 * initialize the overlay nlink, so it could be dropped if unlink
778	 * or rename succeeds.
779	 *
780	 * TODO: implement metadata only index copy up when called with
781	 *       ovl_copy_up_flags(dentry, O_PATH).
782	 */
783	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
784		err = ovl_copy_up(dentry);
785		if (err)
786			return err;
787	}
788
789	err = ovl_inode_lock_interruptible(inode);
790	if (err)
791		return err;
792
 
 
 
 
793	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
794		goto out;
795
796	old_cred = ovl_override_creds(dentry->d_sb);
797	/*
798	 * The overlay inode nlink should be incremented/decremented IFF the
799	 * upper operation succeeds, along with nlink change of upper inode.
800	 * Therefore, before link/unlink/rename, we store the union nlink
801	 * value relative to the upper inode nlink in an upper inode xattr.
802	 */
803	err = ovl_set_nlink_upper(dentry);
804	revert_creds(old_cred);
 
 
805
806out:
807	if (err)
808		ovl_inode_unlock(inode);
 
 
 
809
810	return err;
811}
812
813void ovl_nlink_end(struct dentry *dentry)
814{
815	struct inode *inode = d_inode(dentry);
816
 
 
817	if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
818		const struct cred *old_cred;
819
820		old_cred = ovl_override_creds(dentry->d_sb);
821		ovl_cleanup_index(dentry);
822		revert_creds(old_cred);
823	}
824
825	ovl_inode_unlock(inode);
826}
827
828int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
829{
 
 
830	/* Workdir should not be the same as upperdir */
831	if (workdir == upperdir)
832		goto err;
833
834	/* Workdir should not be subdir of upperdir and vice versa */
835	if (lock_rename(workdir, upperdir) != NULL)
 
 
 
836		goto err_unlock;
837
838	return 0;
839
840err_unlock:
841	unlock_rename(workdir, upperdir);
842err:
843	pr_err("failed to lock workdir+upperdir\n");
844	return -EIO;
845}
846
847/* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
848int ovl_check_metacopy_xattr(struct dentry *dentry)
 
 
 
 
849{
850	int res;
851
852	/* Only regular files can have metacopy xattr */
853	if (!S_ISREG(d_inode(dentry)->i_mode))
854		return 0;
855
856	res = vfs_getxattr(dentry, OVL_XATTR_METACOPY, NULL, 0);
 
857	if (res < 0) {
858		if (res == -ENODATA || res == -EOPNOTSUPP)
859			return 0;
 
 
 
 
 
 
 
860		goto out;
861	}
862
863	return 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
864out:
865	pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
866	return res;
867}
868
 
 
 
 
 
 
 
 
 
 
 
 
869bool ovl_is_metacopy_dentry(struct dentry *dentry)
870{
871	struct ovl_entry *oe = dentry->d_fsdata;
872
873	if (!d_is_reg(dentry))
874		return false;
875
876	if (ovl_dentry_upper(dentry)) {
877		if (!ovl_has_upperdata(d_inode(dentry)))
878			return true;
879		return false;
880	}
881
882	return (oe->numlower > 1);
883}
884
885ssize_t ovl_getxattr(struct dentry *dentry, char *name, char **value,
886		     size_t padding)
887{
888	ssize_t res;
889	char *buf = NULL;
890
891	res = vfs_getxattr(dentry, name, NULL, 0);
892	if (res < 0) {
893		if (res == -ENODATA || res == -EOPNOTSUPP)
894			return -ENODATA;
895		goto fail;
896	}
897
898	if (res != 0) {
899		buf = kzalloc(res + padding, GFP_KERNEL);
900		if (!buf)
901			return -ENOMEM;
902
903		res = vfs_getxattr(dentry, name, buf, res);
904		if (res < 0)
905			goto fail;
906	}
907	*value = buf;
908
909	return res;
910
911fail:
912	pr_warn_ratelimited("failed to get xattr %s: err=%zi)\n",
913			    name, res);
914	kfree(buf);
915	return res;
916}
917
918char *ovl_get_redirect_xattr(struct dentry *dentry, int padding)
919{
920	int res;
921	char *s, *next, *buf = NULL;
922
923	res = ovl_getxattr(dentry, OVL_XATTR_REDIRECT, &buf, padding + 1);
924	if (res == -ENODATA)
925		return NULL;
926	if (res < 0)
927		return ERR_PTR(res);
928	if (res == 0)
929		goto invalid;
930
931	if (buf[0] == '/') {
932		for (s = buf; *s++ == '/'; s = next) {
933			next = strchrnul(s, '/');
934			if (s == next)
935				goto invalid;
936		}
937	} else {
938		if (strchr(buf, '/') != NULL)
939			goto invalid;
940	}
941
942	return buf;
943invalid:
944	pr_warn_ratelimited("invalid redirect (%s)\n", buf);
945	res = -EINVAL;
 
 
 
 
946	kfree(buf);
947	return ERR_PTR(res);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
948}