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