18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 2011 Novell Inc.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/kernel.h>
88c2ecf20Sopenharmony_ci#include <linux/uuid.h>
98c2ecf20Sopenharmony_ci#include <linux/fs.h>
108c2ecf20Sopenharmony_ci#include "ovl_entry.h"
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#undef pr_fmt
138c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "overlayfs: " fmt
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_cienum ovl_path_type {
168c2ecf20Sopenharmony_ci	__OVL_PATH_UPPER	= (1 << 0),
178c2ecf20Sopenharmony_ci	__OVL_PATH_MERGE	= (1 << 1),
188c2ecf20Sopenharmony_ci	__OVL_PATH_ORIGIN	= (1 << 2),
198c2ecf20Sopenharmony_ci};
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define OVL_TYPE_UPPER(type)	((type) & __OVL_PATH_UPPER)
228c2ecf20Sopenharmony_ci#define OVL_TYPE_MERGE(type)	((type) & __OVL_PATH_MERGE)
238c2ecf20Sopenharmony_ci#define OVL_TYPE_ORIGIN(type)	((type) & __OVL_PATH_ORIGIN)
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay."
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cienum ovl_xattr {
288c2ecf20Sopenharmony_ci	OVL_XATTR_OPAQUE,
298c2ecf20Sopenharmony_ci	OVL_XATTR_REDIRECT,
308c2ecf20Sopenharmony_ci	OVL_XATTR_ORIGIN,
318c2ecf20Sopenharmony_ci	OVL_XATTR_IMPURE,
328c2ecf20Sopenharmony_ci	OVL_XATTR_NLINK,
338c2ecf20Sopenharmony_ci	OVL_XATTR_UPPER,
348c2ecf20Sopenharmony_ci	OVL_XATTR_METACOPY,
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cienum ovl_inode_flag {
388c2ecf20Sopenharmony_ci	/* Pure upper dir that may contain non pure upper entries */
398c2ecf20Sopenharmony_ci	OVL_IMPURE,
408c2ecf20Sopenharmony_ci	/* Non-merge dir that may contain whiteout entries */
418c2ecf20Sopenharmony_ci	OVL_WHITEOUTS,
428c2ecf20Sopenharmony_ci	OVL_INDEX,
438c2ecf20Sopenharmony_ci	OVL_UPPERDATA,
448c2ecf20Sopenharmony_ci	/* Inode number will remain constant over copy up. */
458c2ecf20Sopenharmony_ci	OVL_CONST_INO,
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cienum ovl_entry_flag {
498c2ecf20Sopenharmony_ci	OVL_E_UPPER_ALIAS,
508c2ecf20Sopenharmony_ci	OVL_E_OPAQUE,
518c2ecf20Sopenharmony_ci	OVL_E_CONNECTED,
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cienum {
558c2ecf20Sopenharmony_ci	OVL_XINO_OFF,
568c2ecf20Sopenharmony_ci	OVL_XINO_AUTO,
578c2ecf20Sopenharmony_ci	OVL_XINO_ON,
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/*
618c2ecf20Sopenharmony_ci * The tuple (fh,uuid) is a universal unique identifier for a copy up origin,
628c2ecf20Sopenharmony_ci * where:
638c2ecf20Sopenharmony_ci * origin.fh	- exported file handle of the lower file
648c2ecf20Sopenharmony_ci * origin.uuid	- uuid of the lower filesystem
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_ci#define OVL_FH_VERSION	0
678c2ecf20Sopenharmony_ci#define OVL_FH_MAGIC	0xfb
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci/* CPU byte order required for fid decoding:  */
708c2ecf20Sopenharmony_ci#define OVL_FH_FLAG_BIG_ENDIAN	(1 << 0)
718c2ecf20Sopenharmony_ci#define OVL_FH_FLAG_ANY_ENDIAN	(1 << 1)
728c2ecf20Sopenharmony_ci/* Is the real inode encoded in fid an upper inode? */
738c2ecf20Sopenharmony_ci#define OVL_FH_FLAG_PATH_UPPER	(1 << 2)
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \
768c2ecf20Sopenharmony_ci			 OVL_FH_FLAG_PATH_UPPER)
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#if defined(__LITTLE_ENDIAN)
798c2ecf20Sopenharmony_ci#define OVL_FH_FLAG_CPU_ENDIAN 0
808c2ecf20Sopenharmony_ci#elif defined(__BIG_ENDIAN)
818c2ecf20Sopenharmony_ci#define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN
828c2ecf20Sopenharmony_ci#else
838c2ecf20Sopenharmony_ci#error Endianness not defined
848c2ecf20Sopenharmony_ci#endif
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/* The type used to be returned by overlay exportfs for misaligned fid */
878c2ecf20Sopenharmony_ci#define OVL_FILEID_V0	0xfb
888c2ecf20Sopenharmony_ci/* The type returned by overlay exportfs for 32bit aligned fid */
898c2ecf20Sopenharmony_ci#define OVL_FILEID_V1	0xf8
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/* On-disk format for "origin" file handle */
928c2ecf20Sopenharmony_cistruct ovl_fb {
938c2ecf20Sopenharmony_ci	u8 version;	/* 0 */
948c2ecf20Sopenharmony_ci	u8 magic;	/* 0xfb */
958c2ecf20Sopenharmony_ci	u8 len;		/* size of this header + size of fid */
968c2ecf20Sopenharmony_ci	u8 flags;	/* OVL_FH_FLAG_* */
978c2ecf20Sopenharmony_ci	u8 type;	/* fid_type of fid */
988c2ecf20Sopenharmony_ci	uuid_t uuid;	/* uuid of filesystem */
998c2ecf20Sopenharmony_ci	u32 fid[];	/* file identifier should be 32bit aligned in-memory */
1008c2ecf20Sopenharmony_ci} __packed;
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/* In-memory and on-wire format for overlay file handle */
1038c2ecf20Sopenharmony_cistruct ovl_fh {
1048c2ecf20Sopenharmony_ci	u8 padding[3];	/* make sure fb.fid is 32bit aligned */
1058c2ecf20Sopenharmony_ci	union {
1068c2ecf20Sopenharmony_ci		struct ovl_fb fb;
1078c2ecf20Sopenharmony_ci		u8 buf[0];
1088c2ecf20Sopenharmony_ci	};
1098c2ecf20Sopenharmony_ci} __packed;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#define OVL_FH_WIRE_OFFSET	offsetof(struct ovl_fh, fb)
1128c2ecf20Sopenharmony_ci#define OVL_FH_LEN(fh)		(OVL_FH_WIRE_OFFSET + (fh)->fb.len)
1138c2ecf20Sopenharmony_ci#define OVL_FH_FID_OFFSET	(OVL_FH_WIRE_OFFSET + \
1148c2ecf20Sopenharmony_ci				 offsetof(struct ovl_fb, fid))
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ciextern const char *ovl_xattr_table[];
1178c2ecf20Sopenharmony_cistatic inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	return ovl_xattr_table[ox];
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	int err = vfs_rmdir(dir, dentry);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	pr_debug("rmdir(%pd2) = %i\n", dentry, err);
1278c2ecf20Sopenharmony_ci	return err;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic inline int ovl_do_unlink(struct inode *dir, struct dentry *dentry)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	int err = vfs_unlink(dir, dentry, NULL);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	pr_debug("unlink(%pd2) = %i\n", dentry, err);
1358c2ecf20Sopenharmony_ci	return err;
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic inline int ovl_do_link(struct dentry *old_dentry, struct inode *dir,
1398c2ecf20Sopenharmony_ci			      struct dentry *new_dentry)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	int err = vfs_link(old_dentry, dir, new_dentry, NULL);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err);
1448c2ecf20Sopenharmony_ci	return err;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic inline int ovl_do_create(struct inode *dir, struct dentry *dentry,
1488c2ecf20Sopenharmony_ci				umode_t mode)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	int err = vfs_create(dir, dentry, mode, true);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err);
1538c2ecf20Sopenharmony_ci	return err;
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_cistatic inline int ovl_do_mkdir(struct inode *dir, struct dentry *dentry,
1578c2ecf20Sopenharmony_ci			       umode_t mode)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	int err = vfs_mkdir(dir, dentry, mode);
1608c2ecf20Sopenharmony_ci	pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err);
1618c2ecf20Sopenharmony_ci	return err;
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic inline int ovl_do_mknod(struct inode *dir, struct dentry *dentry,
1658c2ecf20Sopenharmony_ci			       umode_t mode, dev_t dev)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	int err = vfs_mknod(dir, dentry, mode, dev);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err);
1708c2ecf20Sopenharmony_ci	return err;
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic inline int ovl_do_symlink(struct inode *dir, struct dentry *dentry,
1748c2ecf20Sopenharmony_ci				 const char *oldname)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	int err = vfs_symlink(dir, dentry, oldname);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err);
1798c2ecf20Sopenharmony_ci	return err;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic inline ssize_t ovl_do_getxattr(struct ovl_fs *ofs, struct dentry *dentry,
1838c2ecf20Sopenharmony_ci				      enum ovl_xattr ox, void *value,
1848c2ecf20Sopenharmony_ci				      size_t size)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	const char *name = ovl_xattr(ofs, ox);
1878c2ecf20Sopenharmony_ci	return vfs_getxattr(dentry, name, value, size);
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
1918c2ecf20Sopenharmony_ci				  enum ovl_xattr ox, const void *value,
1928c2ecf20Sopenharmony_ci				  size_t size)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	const char *name = ovl_xattr(ofs, ox);
1958c2ecf20Sopenharmony_ci	int err = vfs_setxattr(dentry, name, value, size, 0);
1968c2ecf20Sopenharmony_ci	pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n",
1978c2ecf20Sopenharmony_ci		 dentry, name, min((int)size, 48), value, size, err);
1988c2ecf20Sopenharmony_ci	return err;
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
2028c2ecf20Sopenharmony_ci				     enum ovl_xattr ox)
2038c2ecf20Sopenharmony_ci{
2048c2ecf20Sopenharmony_ci	const char *name = ovl_xattr(ofs, ox);
2058c2ecf20Sopenharmony_ci	int err = vfs_removexattr(dentry, name);
2068c2ecf20Sopenharmony_ci	pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err);
2078c2ecf20Sopenharmony_ci	return err;
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic inline int ovl_do_rename(struct inode *olddir, struct dentry *olddentry,
2118c2ecf20Sopenharmony_ci				struct inode *newdir, struct dentry *newdentry,
2128c2ecf20Sopenharmony_ci				unsigned int flags)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	int err;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	pr_debug("rename(%pd2, %pd2, 0x%x)\n", olddentry, newdentry, flags);
2178c2ecf20Sopenharmony_ci	err = vfs_rename(olddir, olddentry, newdir, newdentry, NULL, flags);
2188c2ecf20Sopenharmony_ci	if (err) {
2198c2ecf20Sopenharmony_ci		pr_debug("...rename(%pd2, %pd2, ...) = %i\n",
2208c2ecf20Sopenharmony_ci			 olddentry, newdentry, err);
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci	return err;
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic inline int ovl_do_whiteout(struct inode *dir, struct dentry *dentry)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	int err = vfs_whiteout(dir, dentry);
2288c2ecf20Sopenharmony_ci	pr_debug("whiteout(%pd2) = %i\n", dentry, err);
2298c2ecf20Sopenharmony_ci	return err;
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic inline struct dentry *ovl_do_tmpfile(struct dentry *dentry, umode_t mode)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	struct dentry *ret = vfs_tmpfile(dentry, mode, 0);
2358c2ecf20Sopenharmony_ci	int err = PTR_ERR_OR_ZERO(ret);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err);
2388c2ecf20Sopenharmony_ci	return ret;
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cistatic inline bool ovl_open_flags_need_copy_up(int flags)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	if (!flags)
2448c2ecf20Sopenharmony_ci		return false;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC));
2478c2ecf20Sopenharmony_ci}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci/* util.c */
2508c2ecf20Sopenharmony_ciint ovl_want_write(struct dentry *dentry);
2518c2ecf20Sopenharmony_civoid ovl_drop_write(struct dentry *dentry);
2528c2ecf20Sopenharmony_cistruct dentry *ovl_workdir(struct dentry *dentry);
2538c2ecf20Sopenharmony_ciconst struct cred *ovl_override_creds(struct super_block *sb);
2548c2ecf20Sopenharmony_ciint ovl_can_decode_fh(struct super_block *sb);
2558c2ecf20Sopenharmony_cistruct dentry *ovl_indexdir(struct super_block *sb);
2568c2ecf20Sopenharmony_cibool ovl_index_all(struct super_block *sb);
2578c2ecf20Sopenharmony_cibool ovl_verify_lower(struct super_block *sb);
2588c2ecf20Sopenharmony_cistruct ovl_entry *ovl_alloc_entry(unsigned int numlower);
2598c2ecf20Sopenharmony_cibool ovl_dentry_remote(struct dentry *dentry);
2608c2ecf20Sopenharmony_civoid ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry);
2618c2ecf20Sopenharmony_civoid ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry);
2628c2ecf20Sopenharmony_civoid ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
2638c2ecf20Sopenharmony_ci			   unsigned int mask);
2648c2ecf20Sopenharmony_cibool ovl_dentry_weird(struct dentry *dentry);
2658c2ecf20Sopenharmony_cienum ovl_path_type ovl_path_type(struct dentry *dentry);
2668c2ecf20Sopenharmony_civoid ovl_path_upper(struct dentry *dentry, struct path *path);
2678c2ecf20Sopenharmony_civoid ovl_path_lower(struct dentry *dentry, struct path *path);
2688c2ecf20Sopenharmony_civoid ovl_path_lowerdata(struct dentry *dentry, struct path *path);
2698c2ecf20Sopenharmony_cienum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
2708c2ecf20Sopenharmony_cistruct dentry *ovl_dentry_upper(struct dentry *dentry);
2718c2ecf20Sopenharmony_cistruct dentry *ovl_dentry_lower(struct dentry *dentry);
2728c2ecf20Sopenharmony_cistruct dentry *ovl_dentry_lowerdata(struct dentry *dentry);
2738c2ecf20Sopenharmony_ciconst struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
2748c2ecf20Sopenharmony_cistruct dentry *ovl_dentry_real(struct dentry *dentry);
2758c2ecf20Sopenharmony_cistruct dentry *ovl_i_dentry_upper(struct inode *inode);
2768c2ecf20Sopenharmony_cistruct inode *ovl_inode_upper(struct inode *inode);
2778c2ecf20Sopenharmony_cistruct inode *ovl_inode_lower(struct inode *inode);
2788c2ecf20Sopenharmony_cistruct inode *ovl_inode_lowerdata(struct inode *inode);
2798c2ecf20Sopenharmony_cistruct inode *ovl_inode_real(struct inode *inode);
2808c2ecf20Sopenharmony_cistruct inode *ovl_inode_realdata(struct inode *inode);
2818c2ecf20Sopenharmony_cistruct ovl_dir_cache *ovl_dir_cache(struct inode *inode);
2828c2ecf20Sopenharmony_civoid ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache);
2838c2ecf20Sopenharmony_civoid ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry);
2848c2ecf20Sopenharmony_civoid ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry);
2858c2ecf20Sopenharmony_cibool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry);
2868c2ecf20Sopenharmony_cibool ovl_dentry_is_opaque(struct dentry *dentry);
2878c2ecf20Sopenharmony_cibool ovl_dentry_is_whiteout(struct dentry *dentry);
2888c2ecf20Sopenharmony_civoid ovl_dentry_set_opaque(struct dentry *dentry);
2898c2ecf20Sopenharmony_cibool ovl_dentry_has_upper_alias(struct dentry *dentry);
2908c2ecf20Sopenharmony_civoid ovl_dentry_set_upper_alias(struct dentry *dentry);
2918c2ecf20Sopenharmony_cibool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags);
2928c2ecf20Sopenharmony_cibool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags);
2938c2ecf20Sopenharmony_cibool ovl_has_upperdata(struct inode *inode);
2948c2ecf20Sopenharmony_civoid ovl_set_upperdata(struct inode *inode);
2958c2ecf20Sopenharmony_cibool ovl_redirect_dir(struct super_block *sb);
2968c2ecf20Sopenharmony_ciconst char *ovl_dentry_get_redirect(struct dentry *dentry);
2978c2ecf20Sopenharmony_civoid ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
2988c2ecf20Sopenharmony_civoid ovl_inode_update(struct inode *inode, struct dentry *upperdentry);
2998c2ecf20Sopenharmony_civoid ovl_dir_modified(struct dentry *dentry, bool impurity);
3008c2ecf20Sopenharmony_ciu64 ovl_dentry_version_get(struct dentry *dentry);
3018c2ecf20Sopenharmony_cibool ovl_is_whiteout(struct dentry *dentry);
3028c2ecf20Sopenharmony_cistruct file *ovl_path_open(struct path *path, int flags);
3038c2ecf20Sopenharmony_ciint ovl_copy_up_start(struct dentry *dentry, int flags);
3048c2ecf20Sopenharmony_civoid ovl_copy_up_end(struct dentry *dentry);
3058c2ecf20Sopenharmony_cibool ovl_already_copied_up(struct dentry *dentry, int flags);
3068c2ecf20Sopenharmony_cibool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry);
3078c2ecf20Sopenharmony_cibool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
3088c2ecf20Sopenharmony_ci			 enum ovl_xattr ox);
3098c2ecf20Sopenharmony_ciint ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
3108c2ecf20Sopenharmony_ci		       enum ovl_xattr ox, const void *value, size_t size,
3118c2ecf20Sopenharmony_ci		       int xerr);
3128c2ecf20Sopenharmony_ciint ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
3138c2ecf20Sopenharmony_cibool ovl_inuse_trylock(struct dentry *dentry);
3148c2ecf20Sopenharmony_civoid ovl_inuse_unlock(struct dentry *dentry);
3158c2ecf20Sopenharmony_cibool ovl_is_inuse(struct dentry *dentry);
3168c2ecf20Sopenharmony_cibool ovl_need_index(struct dentry *dentry);
3178c2ecf20Sopenharmony_ciint ovl_nlink_start(struct dentry *dentry);
3188c2ecf20Sopenharmony_civoid ovl_nlink_end(struct dentry *dentry);
3198c2ecf20Sopenharmony_ciint ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
3208c2ecf20Sopenharmony_ciint ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry);
3218c2ecf20Sopenharmony_cibool ovl_is_metacopy_dentry(struct dentry *dentry);
3228c2ecf20Sopenharmony_cichar *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
3238c2ecf20Sopenharmony_ci			     int padding);
3248c2ecf20Sopenharmony_ciint ovl_sync_status(struct ovl_fs *ofs);
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_cistatic inline void ovl_set_flag(unsigned long flag, struct inode *inode)
3278c2ecf20Sopenharmony_ci{
3288c2ecf20Sopenharmony_ci	set_bit(flag, &OVL_I(inode)->flags);
3298c2ecf20Sopenharmony_ci}
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_cistatic inline void ovl_clear_flag(unsigned long flag, struct inode *inode)
3328c2ecf20Sopenharmony_ci{
3338c2ecf20Sopenharmony_ci	clear_bit(flag, &OVL_I(inode)->flags);
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic inline bool ovl_test_flag(unsigned long flag, struct inode *inode)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	return test_bit(flag, &OVL_I(inode)->flags);
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic inline bool ovl_is_impuredir(struct super_block *sb,
3428c2ecf20Sopenharmony_ci				    struct dentry *dentry)
3438c2ecf20Sopenharmony_ci{
3448c2ecf20Sopenharmony_ci	return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_IMPURE);
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci/*
3488c2ecf20Sopenharmony_ci * With xino=auto, we do best effort to keep all inodes on same st_dev and
3498c2ecf20Sopenharmony_ci * d_ino consistent with st_ino.
3508c2ecf20Sopenharmony_ci * With xino=on, we do the same effort but we warn if we failed.
3518c2ecf20Sopenharmony_ci */
3528c2ecf20Sopenharmony_cistatic inline bool ovl_xino_warn(struct super_block *sb)
3538c2ecf20Sopenharmony_ci{
3548c2ecf20Sopenharmony_ci	return OVL_FS(sb)->config.xino == OVL_XINO_ON;
3558c2ecf20Sopenharmony_ci}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci/* All layers on same fs? */
3588c2ecf20Sopenharmony_cistatic inline bool ovl_same_fs(struct super_block *sb)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	return OVL_FS(sb)->xino_mode == 0;
3618c2ecf20Sopenharmony_ci}
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci/* All overlay inodes have same st_dev? */
3648c2ecf20Sopenharmony_cistatic inline bool ovl_same_dev(struct super_block *sb)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	return OVL_FS(sb)->xino_mode >= 0;
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_cistatic inline unsigned int ovl_xino_bits(struct super_block *sb)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	return ovl_same_dev(sb) ? OVL_FS(sb)->xino_mode : 0;
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cistatic inline void ovl_inode_lock(struct inode *inode)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci	mutex_lock(&OVL_I(inode)->lock);
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic inline int ovl_inode_lock_interruptible(struct inode *inode)
3808c2ecf20Sopenharmony_ci{
3818c2ecf20Sopenharmony_ci	return mutex_lock_interruptible(&OVL_I(inode)->lock);
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_cistatic inline void ovl_inode_unlock(struct inode *inode)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	mutex_unlock(&OVL_I(inode)->lock);
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci/* namei.c */
3918c2ecf20Sopenharmony_ciint ovl_check_fb_len(struct ovl_fb *fb, int fb_len);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_cistatic inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	if (fh_len < sizeof(struct ovl_fh))
3968c2ecf20Sopenharmony_ci		return -EINVAL;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET);
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistruct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
4028c2ecf20Sopenharmony_ci				  bool connected);
4038c2ecf20Sopenharmony_ciint ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
4048c2ecf20Sopenharmony_ci			struct dentry *upperdentry, struct ovl_path **stackp);
4058c2ecf20Sopenharmony_ciint ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
4068c2ecf20Sopenharmony_ci		      enum ovl_xattr ox, struct dentry *real, bool is_upper,
4078c2ecf20Sopenharmony_ci		      bool set);
4088c2ecf20Sopenharmony_cistruct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index);
4098c2ecf20Sopenharmony_ciint ovl_verify_index(struct ovl_fs *ofs, struct dentry *index);
4108c2ecf20Sopenharmony_ciint ovl_get_index_name(struct dentry *origin, struct qstr *name);
4118c2ecf20Sopenharmony_cistruct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh);
4128c2ecf20Sopenharmony_cistruct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
4138c2ecf20Sopenharmony_ci				struct dentry *origin, bool verify);
4148c2ecf20Sopenharmony_ciint ovl_path_next(int idx, struct dentry *dentry, struct path *path);
4158c2ecf20Sopenharmony_cistruct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
4168c2ecf20Sopenharmony_ci			  unsigned int flags);
4178c2ecf20Sopenharmony_cibool ovl_lower_positive(struct dentry *dentry);
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_cistatic inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper,
4208c2ecf20Sopenharmony_ci				    struct dentry *origin, bool set)
4218c2ecf20Sopenharmony_ci{
4228c2ecf20Sopenharmony_ci	return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, origin,
4238c2ecf20Sopenharmony_ci				 false, set);
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_cistatic inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index,
4278c2ecf20Sopenharmony_ci				   struct dentry *upper, bool set)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	return ovl_verify_set_fh(ofs, index, OVL_XATTR_UPPER, upper, true, set);
4308c2ecf20Sopenharmony_ci}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci/* readdir.c */
4338c2ecf20Sopenharmony_ciextern const struct file_operations ovl_dir_operations;
4348c2ecf20Sopenharmony_cistruct file *ovl_dir_real_file(const struct file *file, bool want_upper);
4358c2ecf20Sopenharmony_ciint ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
4368c2ecf20Sopenharmony_civoid ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list);
4378c2ecf20Sopenharmony_civoid ovl_cache_free(struct list_head *list);
4388c2ecf20Sopenharmony_civoid ovl_dir_cache_free(struct inode *inode);
4398c2ecf20Sopenharmony_ciint ovl_check_d_type_supported(struct path *realpath);
4408c2ecf20Sopenharmony_ciint ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
4418c2ecf20Sopenharmony_ci			struct dentry *dentry, int level);
4428c2ecf20Sopenharmony_ciint ovl_indexdir_cleanup(struct ovl_fs *ofs);
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci/*
4458c2ecf20Sopenharmony_ci * Can we iterate real dir directly?
4468c2ecf20Sopenharmony_ci *
4478c2ecf20Sopenharmony_ci * Non-merge dir may contain whiteouts from a time it was a merge upper, before
4488c2ecf20Sopenharmony_ci * lower dir was removed under it and possibly before it was rotated from upper
4498c2ecf20Sopenharmony_ci * to lower layer.
4508c2ecf20Sopenharmony_ci */
4518c2ecf20Sopenharmony_cistatic inline bool ovl_dir_is_real(struct dentry *dir)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir));
4548c2ecf20Sopenharmony_ci}
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci/* inode.c */
4578c2ecf20Sopenharmony_ciint ovl_set_nlink_upper(struct dentry *dentry);
4588c2ecf20Sopenharmony_ciint ovl_set_nlink_lower(struct dentry *dentry);
4598c2ecf20Sopenharmony_ciunsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
4608c2ecf20Sopenharmony_ci			   struct dentry *upperdentry,
4618c2ecf20Sopenharmony_ci			   unsigned int fallback);
4628c2ecf20Sopenharmony_ciint ovl_setattr(struct dentry *dentry, struct iattr *attr);
4638c2ecf20Sopenharmony_ciint ovl_getattr(const struct path *path, struct kstat *stat,
4648c2ecf20Sopenharmony_ci		u32 request_mask, unsigned int flags);
4658c2ecf20Sopenharmony_ciint ovl_permission(struct inode *inode, int mask);
4668c2ecf20Sopenharmony_ciint ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
4678c2ecf20Sopenharmony_ci		  const void *value, size_t size, int flags);
4688c2ecf20Sopenharmony_ciint ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
4698c2ecf20Sopenharmony_ci		  void *value, size_t size);
4708c2ecf20Sopenharmony_cissize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
4718c2ecf20Sopenharmony_cistruct posix_acl *ovl_get_acl(struct inode *inode, int type);
4728c2ecf20Sopenharmony_ciint ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags);
4738c2ecf20Sopenharmony_cibool ovl_is_private_xattr(struct super_block *sb, const char *name);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_cistruct ovl_inode_params {
4768c2ecf20Sopenharmony_ci	struct inode *newinode;
4778c2ecf20Sopenharmony_ci	struct dentry *upperdentry;
4788c2ecf20Sopenharmony_ci	struct ovl_path *lowerpath;
4798c2ecf20Sopenharmony_ci	bool index;
4808c2ecf20Sopenharmony_ci	unsigned int numlower;
4818c2ecf20Sopenharmony_ci	char *redirect;
4828c2ecf20Sopenharmony_ci	struct dentry *lowerdata;
4838c2ecf20Sopenharmony_ci};
4848c2ecf20Sopenharmony_civoid ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
4858c2ecf20Sopenharmony_ci		    unsigned long ino, int fsid);
4868c2ecf20Sopenharmony_cistruct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
4878c2ecf20Sopenharmony_cistruct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
4888c2ecf20Sopenharmony_ci			       bool is_upper);
4898c2ecf20Sopenharmony_cibool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir);
4908c2ecf20Sopenharmony_cistruct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir);
4918c2ecf20Sopenharmony_cistruct inode *ovl_get_inode(struct super_block *sb,
4928c2ecf20Sopenharmony_ci			    struct ovl_inode_params *oip);
4938c2ecf20Sopenharmony_cistatic inline void ovl_copyattr(struct inode *from, struct inode *to)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	to->i_uid = from->i_uid;
4968c2ecf20Sopenharmony_ci	to->i_gid = from->i_gid;
4978c2ecf20Sopenharmony_ci	to->i_mode = from->i_mode;
4988c2ecf20Sopenharmony_ci	to->i_atime = from->i_atime;
4998c2ecf20Sopenharmony_ci	to->i_mtime = from->i_mtime;
5008c2ecf20Sopenharmony_ci	to->i_ctime = from->i_ctime;
5018c2ecf20Sopenharmony_ci	i_size_write(to, i_size_read(from));
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic inline void ovl_copyflags(struct inode *from, struct inode *to)
5058c2ecf20Sopenharmony_ci{
5068c2ecf20Sopenharmony_ci	unsigned int mask = S_SYNC | S_IMMUTABLE | S_APPEND | S_NOATIME;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	inode_set_flags(to, from->i_flags & mask, mask);
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci/* dir.c */
5128c2ecf20Sopenharmony_ciextern const struct inode_operations ovl_dir_inode_operations;
5138c2ecf20Sopenharmony_ciint ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
5148c2ecf20Sopenharmony_ci			     struct dentry *dentry);
5158c2ecf20Sopenharmony_cistruct ovl_cattr {
5168c2ecf20Sopenharmony_ci	dev_t rdev;
5178c2ecf20Sopenharmony_ci	umode_t mode;
5188c2ecf20Sopenharmony_ci	const char *link;
5198c2ecf20Sopenharmony_ci	struct dentry *hardlink;
5208c2ecf20Sopenharmony_ci};
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci#define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) })
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ciint ovl_mkdir_real(struct inode *dir, struct dentry **newdentry, umode_t mode);
5258c2ecf20Sopenharmony_cistruct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
5268c2ecf20Sopenharmony_ci			       struct ovl_cattr *attr);
5278c2ecf20Sopenharmony_ciint ovl_cleanup(struct inode *dir, struct dentry *dentry);
5288c2ecf20Sopenharmony_cistruct dentry *ovl_lookup_temp(struct dentry *workdir);
5298c2ecf20Sopenharmony_cistruct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr);
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci/* file.c */
5328c2ecf20Sopenharmony_ciextern const struct file_operations ovl_file_operations;
5338c2ecf20Sopenharmony_ciint __init ovl_aio_request_cache_init(void);
5348c2ecf20Sopenharmony_civoid ovl_aio_request_cache_destroy(void);
5358c2ecf20Sopenharmony_cilong ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
5368c2ecf20Sopenharmony_cilong ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci/* copy_up.c */
5398c2ecf20Sopenharmony_ciint ovl_copy_up(struct dentry *dentry);
5408c2ecf20Sopenharmony_ciint ovl_copy_up_with_data(struct dentry *dentry);
5418c2ecf20Sopenharmony_ciint ovl_maybe_copy_up(struct dentry *dentry, int flags);
5428c2ecf20Sopenharmony_ciint ovl_copy_xattr(struct super_block *sb, struct dentry *old,
5438c2ecf20Sopenharmony_ci		   struct dentry *new);
5448c2ecf20Sopenharmony_ciint ovl_set_attr(struct dentry *upper, struct kstat *stat);
5458c2ecf20Sopenharmony_cistruct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper);
5468c2ecf20Sopenharmony_ciint ovl_set_origin(struct dentry *dentry, struct dentry *lower,
5478c2ecf20Sopenharmony_ci		   struct dentry *upper);
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci/* export.c */
5508c2ecf20Sopenharmony_ciextern const struct export_operations ovl_export_operations;
551