xref: /OK3568_Linux_fs/kernel/fs/overlayfs/ovl_entry.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (C) 2011 Novell Inc.
5*4882a593Smuzhiyun  * Copyright (C) 2016 Red Hat, Inc.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun struct ovl_config {
9*4882a593Smuzhiyun 	char *lowerdir;
10*4882a593Smuzhiyun 	char *upperdir;
11*4882a593Smuzhiyun 	char *workdir;
12*4882a593Smuzhiyun 	bool default_permissions;
13*4882a593Smuzhiyun 	bool redirect_dir;
14*4882a593Smuzhiyun 	bool redirect_follow;
15*4882a593Smuzhiyun 	const char *redirect_mode;
16*4882a593Smuzhiyun 	bool index;
17*4882a593Smuzhiyun 	bool nfs_export;
18*4882a593Smuzhiyun 	int xino;
19*4882a593Smuzhiyun 	bool metacopy;
20*4882a593Smuzhiyun 	bool ovl_volatile;
21*4882a593Smuzhiyun 	bool override_creds;
22*4882a593Smuzhiyun };
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun struct ovl_sb {
25*4882a593Smuzhiyun 	struct super_block *sb;
26*4882a593Smuzhiyun 	dev_t pseudo_dev;
27*4882a593Smuzhiyun 	/* Unusable (conflicting) uuid */
28*4882a593Smuzhiyun 	bool bad_uuid;
29*4882a593Smuzhiyun 	/* Used as a lower layer (but maybe also as upper) */
30*4882a593Smuzhiyun 	bool is_lower;
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun struct ovl_layer {
34*4882a593Smuzhiyun 	struct vfsmount *mnt;
35*4882a593Smuzhiyun 	/* Trap in ovl inode cache */
36*4882a593Smuzhiyun 	struct inode *trap;
37*4882a593Smuzhiyun 	struct ovl_sb *fs;
38*4882a593Smuzhiyun 	/* Index of this layer in fs root (upper idx == 0) */
39*4882a593Smuzhiyun 	int idx;
40*4882a593Smuzhiyun 	/* One fsid per unique underlying sb (upper fsid == 0) */
41*4882a593Smuzhiyun 	int fsid;
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun struct ovl_path {
45*4882a593Smuzhiyun 	const struct ovl_layer *layer;
46*4882a593Smuzhiyun 	struct dentry *dentry;
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /* private information held for overlayfs's superblock */
50*4882a593Smuzhiyun struct ovl_fs {
51*4882a593Smuzhiyun 	unsigned int numlayer;
52*4882a593Smuzhiyun 	/* Number of unique fs among layers including upper fs */
53*4882a593Smuzhiyun 	unsigned int numfs;
54*4882a593Smuzhiyun 	const struct ovl_layer *layers;
55*4882a593Smuzhiyun 	struct ovl_sb *fs;
56*4882a593Smuzhiyun 	/* workbasedir is the path at workdir= mount option */
57*4882a593Smuzhiyun 	struct dentry *workbasedir;
58*4882a593Smuzhiyun 	/* workdir is the 'work' directory under workbasedir */
59*4882a593Smuzhiyun 	struct dentry *workdir;
60*4882a593Smuzhiyun 	/* index directory listing overlay inodes by origin file handle */
61*4882a593Smuzhiyun 	struct dentry *indexdir;
62*4882a593Smuzhiyun 	long namelen;
63*4882a593Smuzhiyun 	/* pathnames of lower and upper dirs, for show_options */
64*4882a593Smuzhiyun 	struct ovl_config config;
65*4882a593Smuzhiyun 	/* creds of process who forced instantiation of super block */
66*4882a593Smuzhiyun 	const struct cred *creator_cred;
67*4882a593Smuzhiyun 	bool tmpfile;
68*4882a593Smuzhiyun 	bool noxattr;
69*4882a593Smuzhiyun 	/* Did we take the inuse lock? */
70*4882a593Smuzhiyun 	bool upperdir_locked;
71*4882a593Smuzhiyun 	bool workdir_locked;
72*4882a593Smuzhiyun 	bool share_whiteout;
73*4882a593Smuzhiyun 	/* Traps in ovl inode cache */
74*4882a593Smuzhiyun 	struct inode *workbasedir_trap;
75*4882a593Smuzhiyun 	struct inode *workdir_trap;
76*4882a593Smuzhiyun 	struct inode *indexdir_trap;
77*4882a593Smuzhiyun 	/* -1: disabled, 0: same fs, 1..32: number of unused ino bits */
78*4882a593Smuzhiyun 	int xino_mode;
79*4882a593Smuzhiyun 	/* For allocation of non-persistent inode numbers */
80*4882a593Smuzhiyun 	atomic_long_t last_ino;
81*4882a593Smuzhiyun 	/* Whiteout dentry cache */
82*4882a593Smuzhiyun 	struct dentry *whiteout;
83*4882a593Smuzhiyun 	/* r/o snapshot of upperdir sb's only taken on volatile mounts */
84*4882a593Smuzhiyun 	errseq_t errseq;
85*4882a593Smuzhiyun };
86*4882a593Smuzhiyun 
ovl_upper_mnt(struct ovl_fs * ofs)87*4882a593Smuzhiyun static inline struct vfsmount *ovl_upper_mnt(struct ovl_fs *ofs)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	return ofs->layers[0].mnt;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun 
OVL_FS(struct super_block * sb)92*4882a593Smuzhiyun static inline struct ovl_fs *OVL_FS(struct super_block *sb)
93*4882a593Smuzhiyun {
94*4882a593Smuzhiyun 	return (struct ovl_fs *)sb->s_fs_info;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
ovl_should_sync(struct ovl_fs * ofs)97*4882a593Smuzhiyun static inline bool ovl_should_sync(struct ovl_fs *ofs)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	return !ofs->config.ovl_volatile;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun /* private information held for every overlayfs dentry */
103*4882a593Smuzhiyun struct ovl_entry {
104*4882a593Smuzhiyun 	union {
105*4882a593Smuzhiyun 		struct {
106*4882a593Smuzhiyun 			unsigned long flags;
107*4882a593Smuzhiyun 		};
108*4882a593Smuzhiyun 		struct rcu_head rcu;
109*4882a593Smuzhiyun 	};
110*4882a593Smuzhiyun 	unsigned numlower;
111*4882a593Smuzhiyun 	struct ovl_path lowerstack[];
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
115*4882a593Smuzhiyun 
OVL_E(struct dentry * dentry)116*4882a593Smuzhiyun static inline struct ovl_entry *OVL_E(struct dentry *dentry)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun 	return (struct ovl_entry *) dentry->d_fsdata;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun struct ovl_inode {
122*4882a593Smuzhiyun 	union {
123*4882a593Smuzhiyun 		struct ovl_dir_cache *cache;	/* directory */
124*4882a593Smuzhiyun 		struct inode *lowerdata;	/* regular file */
125*4882a593Smuzhiyun 	};
126*4882a593Smuzhiyun 	const char *redirect;
127*4882a593Smuzhiyun 	u64 version;
128*4882a593Smuzhiyun 	unsigned long flags;
129*4882a593Smuzhiyun 	struct inode vfs_inode;
130*4882a593Smuzhiyun 	struct dentry *__upperdentry;
131*4882a593Smuzhiyun 	struct inode *lower;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	/* synchronize copy up and more */
134*4882a593Smuzhiyun 	struct mutex lock;
135*4882a593Smuzhiyun };
136*4882a593Smuzhiyun 
OVL_I(struct inode * inode)137*4882a593Smuzhiyun static inline struct ovl_inode *OVL_I(struct inode *inode)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun 	return container_of(inode, struct ovl_inode, vfs_inode);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
ovl_upperdentry_dereference(struct ovl_inode * oi)142*4882a593Smuzhiyun static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi)
143*4882a593Smuzhiyun {
144*4882a593Smuzhiyun 	return READ_ONCE(oi->__upperdentry);
145*4882a593Smuzhiyun }
146