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