xref: /openbmc/linux/fs/overlayfs/util.c (revision 174cd4b1)
1 /*
2  * Copyright (C) 2011 Novell Inc.
3  * Copyright (C) 2016 Red Hat, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9 
10 #include <linux/fs.h>
11 #include <linux/mount.h>
12 #include <linux/slab.h>
13 #include <linux/cred.h>
14 #include <linux/xattr.h>
15 #include "overlayfs.h"
16 #include "ovl_entry.h"
17 
18 int ovl_want_write(struct dentry *dentry)
19 {
20 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
21 	return mnt_want_write(ofs->upper_mnt);
22 }
23 
24 void ovl_drop_write(struct dentry *dentry)
25 {
26 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
27 	mnt_drop_write(ofs->upper_mnt);
28 }
29 
30 struct dentry *ovl_workdir(struct dentry *dentry)
31 {
32 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
33 	return ofs->workdir;
34 }
35 
36 const struct cred *ovl_override_creds(struct super_block *sb)
37 {
38 	struct ovl_fs *ofs = sb->s_fs_info;
39 
40 	return override_creds(ofs->creator_cred);
41 }
42 
43 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
44 {
45 	size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
46 	struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
47 
48 	if (oe)
49 		oe->numlower = numlower;
50 
51 	return oe;
52 }
53 
54 bool ovl_dentry_remote(struct dentry *dentry)
55 {
56 	return dentry->d_flags &
57 		(DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
58 		 DCACHE_OP_REAL);
59 }
60 
61 bool ovl_dentry_weird(struct dentry *dentry)
62 {
63 	return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
64 				  DCACHE_MANAGE_TRANSIT |
65 				  DCACHE_OP_HASH |
66 				  DCACHE_OP_COMPARE);
67 }
68 
69 enum ovl_path_type ovl_path_type(struct dentry *dentry)
70 {
71 	struct ovl_entry *oe = dentry->d_fsdata;
72 	enum ovl_path_type type = 0;
73 
74 	if (oe->__upperdentry) {
75 		type = __OVL_PATH_UPPER;
76 
77 		/*
78 		 * Non-dir dentry can hold lower dentry from previous
79 		 * location.
80 		 */
81 		if (oe->numlower && d_is_dir(dentry))
82 			type |= __OVL_PATH_MERGE;
83 	} else {
84 		if (oe->numlower > 1)
85 			type |= __OVL_PATH_MERGE;
86 	}
87 	return type;
88 }
89 
90 void ovl_path_upper(struct dentry *dentry, struct path *path)
91 {
92 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
93 	struct ovl_entry *oe = dentry->d_fsdata;
94 
95 	path->mnt = ofs->upper_mnt;
96 	path->dentry = ovl_upperdentry_dereference(oe);
97 }
98 
99 void ovl_path_lower(struct dentry *dentry, struct path *path)
100 {
101 	struct ovl_entry *oe = dentry->d_fsdata;
102 
103 	*path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
104 }
105 
106 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
107 {
108 	enum ovl_path_type type = ovl_path_type(dentry);
109 
110 	if (!OVL_TYPE_UPPER(type))
111 		ovl_path_lower(dentry, path);
112 	else
113 		ovl_path_upper(dentry, path);
114 
115 	return type;
116 }
117 
118 struct dentry *ovl_dentry_upper(struct dentry *dentry)
119 {
120 	struct ovl_entry *oe = dentry->d_fsdata;
121 
122 	return ovl_upperdentry_dereference(oe);
123 }
124 
125 static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
126 {
127 	return oe->numlower ? oe->lowerstack[0].dentry : NULL;
128 }
129 
130 struct dentry *ovl_dentry_lower(struct dentry *dentry)
131 {
132 	struct ovl_entry *oe = dentry->d_fsdata;
133 
134 	return __ovl_dentry_lower(oe);
135 }
136 
137 struct dentry *ovl_dentry_real(struct dentry *dentry)
138 {
139 	struct ovl_entry *oe = dentry->d_fsdata;
140 	struct dentry *realdentry;
141 
142 	realdentry = ovl_upperdentry_dereference(oe);
143 	if (!realdentry)
144 		realdentry = __ovl_dentry_lower(oe);
145 
146 	return realdentry;
147 }
148 
149 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
150 {
151 	struct ovl_entry *oe = dentry->d_fsdata;
152 
153 	return oe->cache;
154 }
155 
156 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
157 {
158 	struct ovl_entry *oe = dentry->d_fsdata;
159 
160 	oe->cache = cache;
161 }
162 
163 bool ovl_dentry_is_opaque(struct dentry *dentry)
164 {
165 	struct ovl_entry *oe = dentry->d_fsdata;
166 	return oe->opaque;
167 }
168 
169 bool ovl_dentry_is_whiteout(struct dentry *dentry)
170 {
171 	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
172 }
173 
174 void ovl_dentry_set_opaque(struct dentry *dentry)
175 {
176 	struct ovl_entry *oe = dentry->d_fsdata;
177 
178 	oe->opaque = true;
179 }
180 
181 bool ovl_redirect_dir(struct super_block *sb)
182 {
183 	struct ovl_fs *ofs = sb->s_fs_info;
184 
185 	return ofs->config.redirect_dir;
186 }
187 
188 void ovl_clear_redirect_dir(struct super_block *sb)
189 {
190 	struct ovl_fs *ofs = sb->s_fs_info;
191 
192 	ofs->config.redirect_dir = false;
193 }
194 
195 const char *ovl_dentry_get_redirect(struct dentry *dentry)
196 {
197 	struct ovl_entry *oe = dentry->d_fsdata;
198 
199 	return oe->redirect;
200 }
201 
202 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
203 {
204 	struct ovl_entry *oe = dentry->d_fsdata;
205 
206 	kfree(oe->redirect);
207 	oe->redirect = redirect;
208 }
209 
210 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
211 {
212 	struct ovl_entry *oe = dentry->d_fsdata;
213 
214 	WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
215 	WARN_ON(oe->__upperdentry);
216 	/*
217 	 * Make sure upperdentry is consistent before making it visible to
218 	 * ovl_upperdentry_dereference().
219 	 */
220 	smp_wmb();
221 	oe->__upperdentry = upperdentry;
222 }
223 
224 void ovl_inode_init(struct inode *inode, struct inode *realinode, bool is_upper)
225 {
226 	WRITE_ONCE(inode->i_private, (unsigned long) realinode |
227 		   (is_upper ? OVL_ISUPPER_MASK : 0));
228 }
229 
230 void ovl_inode_update(struct inode *inode, struct inode *upperinode)
231 {
232 	WARN_ON(!upperinode);
233 	WARN_ON(!inode_unhashed(inode));
234 	WRITE_ONCE(inode->i_private,
235 		   (unsigned long) upperinode | OVL_ISUPPER_MASK);
236 	if (!S_ISDIR(upperinode->i_mode))
237 		__insert_inode_hash(inode, (unsigned long) upperinode);
238 }
239 
240 void ovl_dentry_version_inc(struct dentry *dentry)
241 {
242 	struct ovl_entry *oe = dentry->d_fsdata;
243 
244 	WARN_ON(!inode_is_locked(dentry->d_inode));
245 	oe->version++;
246 }
247 
248 u64 ovl_dentry_version_get(struct dentry *dentry)
249 {
250 	struct ovl_entry *oe = dentry->d_fsdata;
251 
252 	WARN_ON(!inode_is_locked(dentry->d_inode));
253 	return oe->version;
254 }
255 
256 bool ovl_is_whiteout(struct dentry *dentry)
257 {
258 	struct inode *inode = dentry->d_inode;
259 
260 	return inode && IS_WHITEOUT(inode);
261 }
262 
263 struct file *ovl_path_open(struct path *path, int flags)
264 {
265 	return dentry_open(path, flags | O_NOATIME, current_cred());
266 }
267