Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2011 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2016 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/exportfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/uuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "overlayfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) int ovl_want_write(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	return mnt_want_write(ovl_upper_mnt(ofs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) void ovl_drop_write(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	mnt_drop_write(ovl_upper_mnt(ofs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) struct dentry *ovl_workdir(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	return ofs->workdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) const struct cred *ovl_override_creds(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	if (!ofs->config.override_creds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	return override_creds(ofs->creator_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) void ovl_revert_creds(struct super_block *sb, const struct cred *old_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	if (old_cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		revert_creds(old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * Check if underlying fs supports file handles and try to determine encoding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * type, in order to deduce maximum inode number used by fs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * Return 0 if file handles are not supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * Return -1 if fs uses a non default encoding with unknown inode size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) int ovl_can_decode_fh(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) struct dentry *ovl_indexdir(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	return ofs->indexdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) /* Index all files on copy up. For now only enabled for NFS export */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) bool ovl_index_all(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	return ofs->config.nfs_export && ofs->config.index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) /* Verify lower origin on lookup. For now only enabled for NFS export */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) bool ovl_verify_lower(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	return ofs->config.nfs_export && ofs->config.index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	if (oe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		oe->numlower = numlower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) bool ovl_dentry_remote(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	return dentry->d_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		(DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			     unsigned int mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct ovl_entry *oe = OVL_E(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	unsigned int i, flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (upperdentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		flags |= upperdentry->d_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	for (i = 0; i < oe->numlower; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		flags |= oe->lowerstack[i].dentry->d_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	spin_lock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	dentry->d_flags &= ~mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	dentry->d_flags |= flags & mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	spin_unlock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) bool ovl_dentry_weird(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 				  DCACHE_MANAGE_TRANSIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				  DCACHE_OP_HASH |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				  DCACHE_OP_COMPARE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) enum ovl_path_type ovl_path_type(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct ovl_entry *oe = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	enum ovl_path_type type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (ovl_dentry_upper(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		type = __OVL_PATH_UPPER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		 * Non-dir dentry can hold lower dentry of its copy up origin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		if (oe->numlower) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 				type |= __OVL_PATH_ORIGIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			if (d_is_dir(dentry) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			    !ovl_has_upperdata(d_inode(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				type |= __OVL_PATH_MERGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		if (oe->numlower > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			type |= __OVL_PATH_MERGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) void ovl_path_upper(struct dentry *dentry, struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	path->mnt = ovl_upper_mnt(ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	path->dentry = ovl_dentry_upper(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) void ovl_path_lower(struct dentry *dentry, struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	struct ovl_entry *oe = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (oe->numlower) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		path->mnt = oe->lowerstack[0].layer->mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		path->dentry = oe->lowerstack[0].dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		*path = (struct path) { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct ovl_entry *oe = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (oe->numlower) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		*path = (struct path) { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	enum ovl_path_type type = ovl_path_type(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (!OVL_TYPE_UPPER(type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		ovl_path_lower(dentry, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		ovl_path_upper(dentry, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	return type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct dentry *ovl_dentry_upper(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct dentry *ovl_dentry_lower(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	struct ovl_entry *oe = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return oe->numlower ? oe->lowerstack[0].dentry : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct ovl_entry *oe = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	return oe->numlower ? oe->lowerstack[0].layer : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * ovl_dentry_lower() could return either a data dentry or metacopy dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  * dependig on what is stored in lowerstack[0]. At times we need to find
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  * lower dentry which has data (and not metacopy dentry). This helper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  * returns the lower data dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct ovl_entry *oe = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct dentry *ovl_dentry_real(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct dentry *ovl_i_dentry_upper(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	return ovl_upperdentry_dereference(OVL_I(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct inode *ovl_inode_upper(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	return upperdentry ? d_inode(upperdentry) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct inode *ovl_inode_lower(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	return OVL_I(inode)->lower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct inode *ovl_inode_real(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /* Return inode which contains lower data. Do not return metacopy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct inode *ovl_inode_lowerdata(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (WARN_ON(!S_ISREG(inode->i_mode)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /* Return real inode which contains data. Does not return metacopy inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) struct inode *ovl_inode_realdata(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct inode *upperinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	upperinode = ovl_inode_upper(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (upperinode && ovl_has_upperdata(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		return upperinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	return ovl_inode_lowerdata(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return OVL_I(inode)->cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	OVL_I(inode)->cache = cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	set_bit(flag, &OVL_E(dentry)->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	clear_bit(flag, &OVL_E(dentry)->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	return test_bit(flag, &OVL_E(dentry)->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) bool ovl_dentry_is_opaque(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) bool ovl_dentry_is_whiteout(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) void ovl_dentry_set_opaque(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  * to return positive, while there's no actual upper alias for the inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)  * Copy up code needs to know about the existence of the upper alias, so it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  * can't use ovl_dentry_upper().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) bool ovl_dentry_has_upper_alias(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) void ovl_dentry_set_upper_alias(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static bool ovl_should_check_upperdata(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (!S_ISREG(inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (!ovl_inode_lower(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) bool ovl_has_upperdata(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	if (!ovl_should_check_upperdata(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if (!ovl_test_flag(OVL_UPPERDATA, inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	 * if setting of OVL_UPPERDATA is visible, then effects of writes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	 * before that are visible too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	smp_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) void ovl_set_upperdata(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	 * if OVL_UPPERDATA flag is visible, then effects of write operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	 * before it are visible as well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	ovl_set_flag(OVL_UPPERDATA, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /* Caller should hold ovl_inode->lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if (!ovl_open_flags_need_copy_up(flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (!ovl_open_flags_need_copy_up(flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	return !ovl_has_upperdata(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) bool ovl_redirect_dir(struct super_block *sb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	return ofs->config.redirect_dir && !ofs->noxattr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) const char *ovl_dentry_get_redirect(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	return OVL_I(d_inode(dentry))->redirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	kfree(oi->redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	oi->redirect = redirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	struct inode *upperinode = d_inode(upperdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	WARN_ON(OVL_I(inode)->__upperdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	 * Make sure upperdentry is consistent before making it visible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	smp_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	OVL_I(inode)->__upperdentry = upperdentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (inode_unhashed(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		inode->i_private = upperinode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		__insert_inode_hash(inode, (unsigned long) upperinode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	WARN_ON(!inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	WARN_ON(!d_is_dir(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	 * Version is used by readdir code to keep cache consistent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	 * For merge dirs (or dirs with origin) all changes need to be noted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	 * For non-merge dirs, cache contains only impure entries (i.e. ones
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	 * which have been copied up and have origins), so only need to note
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	 * changes to impure entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (!ovl_dir_is_real(dentry) || impurity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		OVL_I(inode)->version++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) void ovl_dir_modified(struct dentry *dentry, bool impurity)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	/* Copy mtime/ctime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	ovl_dir_version_inc(dentry, impurity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) u64 ovl_dentry_version_get(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	WARN_ON(!inode_is_locked(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	return OVL_I(inode)->version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) bool ovl_is_whiteout(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	struct inode *inode = dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	return inode && IS_WHITEOUT(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) struct file *ovl_path_open(struct path *path, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	struct inode *inode = d_inode(path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	int err, acc_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	if (flags & ~(O_ACCMODE | O_LARGEFILE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	switch (flags & O_ACCMODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	case O_RDONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		acc_mode = MAY_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	case O_WRONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		acc_mode = MAY_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	err = inode_permission(inode, acc_mode | MAY_OPEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	/* O_NOATIME is an optimization, don't fail if not permitted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (inode_owner_or_capable(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		flags |= O_NOATIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	return dentry_open(path, flags, current_cred());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /* Caller should hold ovl_inode->lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if (ovl_dentry_upper(dentry) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	    !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) bool ovl_already_copied_up(struct dentry *dentry, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	 * Check if copy-up has happened as well as for upper alias (in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	 * case of hard links) is there.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	 * Both checks are lockless:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	 *  - false negatives: will recheck under oi->lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	 *  - false positives:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	 *    + ovl_dentry_upper() uses memory barriers to ensure the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	 *      upper dentry is up-to-date
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	 *    + ovl_dentry_has_upper_alias() relies on locking of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	 *      upper parent i_rwsem to prevent reordering copy-up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	 *      with rename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	if (ovl_dentry_upper(dentry) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	    !ovl_dentry_needs_data_copy_up(dentry, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) int ovl_copy_up_start(struct dentry *dentry, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	err = ovl_inode_lock_interruptible(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	if (!err && ovl_already_copied_up_locked(dentry, flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		err = 1; /* Already copied up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		ovl_inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) void ovl_copy_up_end(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	ovl_inode_unlock(d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	ssize_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	/* Zero size value means "copied up but origin unknown" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	if (res >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 			 enum ovl_xattr ox)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	ssize_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	char val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	if (!d_is_dir(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	res = ovl_do_getxattr(OVL_FS(sb), dentry, ox, &val, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	if (res == 1 && val == 'y')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) #define OVL_XATTR_OPAQUE_POSTFIX	"opaque"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) #define OVL_XATTR_REDIRECT_POSTFIX	"redirect"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) #define OVL_XATTR_ORIGIN_POSTFIX	"origin"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) #define OVL_XATTR_IMPURE_POSTFIX	"impure"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) #define OVL_XATTR_NLINK_POSTFIX		"nlink"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) #define OVL_XATTR_UPPER_POSTFIX		"upper"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) #define OVL_XATTR_METACOPY_POSTFIX	"metacopy"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) #define OVL_XATTR_TAB_ENTRY(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	[x] = OVL_XATTR_PREFIX x ## _POSTFIX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) const char *ovl_xattr_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		       enum ovl_xattr ox, const void *value, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		       int xerr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	if (ofs->noxattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		return xerr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	err = ovl_do_setxattr(ofs, upperdentry, ox, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	if (err == -EOPNOTSUPP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 		ofs->noxattr = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 		return xerr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	 * Do not fail when upper doesn't support xattrs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	 * Upper inodes won't have origin nor redirect xattr anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 				 "y", 1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)  * Caller must hold a reference to inode to prevent it from being freed while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)  * it is marked inuse.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) bool ovl_inuse_trylock(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	bool locked = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	if (!(inode->i_state & I_OVL_INUSE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		inode->i_state |= I_OVL_INUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		locked = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	return locked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) void ovl_inuse_unlock(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	if (dentry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		WARN_ON(!(inode->i_state & I_OVL_INUSE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 		inode->i_state &= ~I_OVL_INUSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) bool ovl_is_inuse(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	bool inuse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	spin_lock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	inuse = (inode->i_state & I_OVL_INUSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	spin_unlock(&inode->i_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	return inuse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691)  * Does this overlay dentry need to be indexed on copy up?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) bool ovl_need_index(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	struct dentry *lower = ovl_dentry_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	if (!lower || !ovl_indexdir(dentry->d_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	/* Index all files for NFS export and consistency verification */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	if (ovl_index_all(dentry->d_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	/* Index only lower hardlinks on copy up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) /* Caller must hold OVL_I(inode)->lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) static void ovl_cleanup_index(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	struct inode *dir = indexdir->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	struct dentry *upperdentry = ovl_dentry_upper(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	struct dentry *index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	struct qstr name = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	err = ovl_get_index_name(lowerdentry, &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	inode = d_inode(upperdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 				    upperdentry, inode->i_ino, inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		 * We either have a bug with persistent union nlink or a lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 		 * hardlink was added while overlay is mounted. Adding a lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		 * hardlink and then unlinking all overlay hardlinks would drop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		 * overlay nlink to zero before all upper inodes are unlinked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 		 * As a safety measure, when that situation is detected, set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		 * the overlay nlink to the index inode nlink minus one for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		 * index entry itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 		set_nlink(d_inode(dentry), inode->i_nlink - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		ovl_set_nlink_upper(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	inode_lock_nested(dir, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	index = lookup_one_len(name.name, indexdir, name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	err = PTR_ERR(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	if (IS_ERR(index)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	} else if (ovl_index_all(dentry->d_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		/* Whiteout orphan index to block future open by handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 					       dir, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 		/* Cleanup orphan index entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 		err = ovl_cleanup(dir, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	kfree(name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)  * Operations that change overlay inode and upper inode nlink need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)  * synchronized with copy up for persistent nlink accounting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) int ovl_nlink_start(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	if (WARN_ON(!inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	 * With inodes index is enabled, we store the union overlay nlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	 * in an xattr on the index inode. When whiting out an indexed lower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	 * we need to decrement the overlay persistent nlink, but before the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	 * first copy up, we have no upper index inode to store the xattr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	 * As a workaround, before whiteout/rename over an indexed lower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	 * copy up to create the upper index. Creating the upper index will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	 * initialize the overlay nlink, so it could be dropped if unlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	 * or rename succeeds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	 * TODO: implement metadata only index copy up when called with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	 *       ovl_copy_up_flags(dentry, O_PATH).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 		err = ovl_copy_up(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	err = ovl_inode_lock_interruptible(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 	old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	 * The overlay inode nlink should be incremented/decremented IFF the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	 * upper operation succeeds, along with nlink change of upper inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	 * Therefore, before link/unlink/rename, we store the union nlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	 * value relative to the upper inode nlink in an upper inode xattr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 	err = ovl_set_nlink_upper(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 		ovl_inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) void ovl_nlink_end(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 	if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 		const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 		old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 		ovl_cleanup_index(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 		ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 	ovl_inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	/* Workdir should not be the same as upperdir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	if (workdir == upperdir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	/* Workdir should not be subdir of upperdir and vice versa */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	if (lock_rename(workdir, upperdir) != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 		goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 	unlock_rename(workdir, upperdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	pr_err("failed to lock workdir+upperdir\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	ssize_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	/* Only regular files can have metacopy xattr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 	if (!S_ISREG(d_inode(dentry)->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 	if (res < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 		if (res == -ENODATA || res == -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 	pr_warn_ratelimited("failed to get metacopy (%zi)\n", res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) bool ovl_is_metacopy_dentry(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 	struct ovl_entry *oe = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 	if (!d_is_reg(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	if (ovl_dentry_upper(dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 		if (!ovl_has_upperdata(d_inode(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 			return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	return (oe->numlower > 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 			     int padding)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	char *s, *next, *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	if (res == -ENODATA || res == -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 	if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	if (res == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 		goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 	buf = kzalloc(res + padding + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 	if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 	if (res == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 		goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 	if (buf[0] == '/') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 		for (s = buf; *s++ == '/'; s = next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 			next = strchrnul(s, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 			if (s == next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 				goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 		if (strchr(buf, '/') != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 			goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 	return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) invalid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 	pr_warn_ratelimited("invalid redirect (%s)\n", buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 	res = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 	goto err_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 	pr_warn_ratelimited("failed to get redirect (%i)\n", res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) err_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 	return ERR_PTR(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)  * ovl_sync_status() - Check fs sync status for volatile mounts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952)  * Returns 1 if this is not a volatile mount and a real sync is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954)  * Returns 0 if syncing can be skipped because mount is volatile, and no errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)  * have occurred on the upperdir since the mount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)  * Returns -errno if it is a volatile mount, and the error that occurred since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958)  * the last mount. If the error code changes, it'll return the latest error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)  * code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) int ovl_sync_status(struct ovl_fs *ofs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) 	struct vfsmount *mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) 	if (ovl_should_sync(ofs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) 	mnt = ovl_upper_mnt(ofs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) 	if (!mnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) 	return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) }