blob: 150fdf3bc68d4c812fe45a886c724e58c56f84aa [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002/*
3 *
4 * Copyright (C) 2011 Novell Inc.
Miklos Szeredie9be9d52014-10-24 00:14:38 +02005 */
6
7#include <linux/fs.h>
8#include <linux/slab.h>
9#include <linux/namei.h>
10#include <linux/file.h>
11#include <linux/xattr.h>
12#include <linux/rbtree.h>
13#include <linux/security.h>
14#include <linux/cred.h>
Amir Goldsteinb5efccb2017-05-11 16:42:27 +030015#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020016#include "overlayfs.h"
17
18struct ovl_cache_entry {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020019 unsigned int len;
20 unsigned int type;
Amir Goldsteinb5efccb2017-05-11 16:42:27 +030021 u64 real_ino;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020022 u64 ino;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020023 struct list_head l_node;
24 struct rb_node node;
Miklos Szeredicdb67272015-06-22 13:53:48 +020025 struct ovl_cache_entry *next_maybe_whiteout;
zhangyi (F)95e598e2017-10-31 22:57:00 +020026 bool is_upper;
Miklos Szeredic2096532014-10-27 13:48:48 +010027 bool is_whiteout;
Al Viro68bf8612014-10-23 22:58:56 -040028 char name[];
Miklos Szeredie9be9d52014-10-24 00:14:38 +020029};
30
31struct ovl_dir_cache {
32 long refcount;
33 u64 version;
34 struct list_head entries;
Miklos Szeredi4edb83b2017-07-27 21:54:06 +020035 struct rb_root root;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020036};
37
38struct ovl_readdir_data {
39 struct dir_context ctx;
Antonio Murdaca3fe6e522016-04-07 15:48:25 +020040 struct dentry *dentry;
Miklos Szeredi56656e92016-03-21 17:31:46 +010041 bool is_lowest;
Miklos Szeredi4edb83b2017-07-27 21:54:06 +020042 struct rb_root *root;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020043 struct list_head *list;
Al Virodb6ec212014-10-23 23:03:03 -040044 struct list_head middle;
Miklos Szeredicdb67272015-06-22 13:53:48 +020045 struct ovl_cache_entry *first_maybe_whiteout;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020046 int count;
47 int err;
Amir Goldsteinb5efccb2017-05-11 16:42:27 +030048 bool is_upper;
Vivek Goyal45aebea2016-02-22 09:28:34 -050049 bool d_type_supported;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020050};
51
52struct ovl_dir_file {
53 bool is_real;
54 bool is_upper;
55 struct ovl_dir_cache *cache;
hujianyang43303972014-12-11 10:30:18 +080056 struct list_head *cursor;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020057 struct file *realfile;
58 struct file *upperfile;
59};
60
61static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
62{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +020063 return rb_entry(n, struct ovl_cache_entry, node);
64}
65
66static bool ovl_cache_entry_find_link(const char *name, int len,
67 struct rb_node ***link,
68 struct rb_node **parent)
69{
70 bool found = false;
71 struct rb_node **newp = *link;
72
73 while (!found && *newp) {
74 int cmp;
75 struct ovl_cache_entry *tmp;
76
77 *parent = *newp;
78 tmp = ovl_cache_entry_from_node(*newp);
79 cmp = strncmp(name, tmp->name, len);
80 if (cmp > 0)
81 newp = &tmp->node.rb_right;
82 else if (cmp < 0 || len < tmp->len)
83 newp = &tmp->node.rb_left;
84 else
85 found = true;
86 }
87 *link = newp;
88
89 return found;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020090}
91
92static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
93 const char *name, int len)
94{
95 struct rb_node *node = root->rb_node;
96 int cmp;
97
98 while (node) {
99 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
100
101 cmp = strncmp(name, p->name, len);
102 if (cmp > 0)
103 node = p->node.rb_right;
104 else if (cmp < 0 || len < p->len)
105 node = p->node.rb_left;
106 else
107 return p;
108 }
109
110 return NULL;
111}
112
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300113static bool ovl_calc_d_ino(struct ovl_readdir_data *rdd,
114 struct ovl_cache_entry *p)
115{
116 /* Don't care if not doing ovl_iter() */
117 if (!rdd->dentry)
118 return false;
119
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200120 /* Always recalc d_ino when remapping lower inode numbers */
121 if (ovl_xino_bits(rdd->dentry->d_sb))
122 return true;
123
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300124 /* Always recalc d_ino for parent */
125 if (strcmp(p->name, "..") == 0)
126 return true;
127
128 /* If this is lower, then native d_ino will do */
129 if (!rdd->is_upper)
130 return false;
131
132 /*
133 * Recalc d_ino for '.' and for all entries if dir is impure (contains
134 * copied up entries)
135 */
136 if ((p->name[0] == '.' && p->len == 1) ||
137 ovl_test_flag(OVL_IMPURE, d_inode(rdd->dentry)))
138 return true;
139
140 return false;
141}
142
Miklos Szeredicdb67272015-06-22 13:53:48 +0200143static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100144 const char *name, int len,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200145 u64 ino, unsigned int d_type)
146{
147 struct ovl_cache_entry *p;
Al Viro68bf8612014-10-23 22:58:56 -0400148 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200149
Al Viro68bf8612014-10-23 22:58:56 -0400150 p = kmalloc(size, GFP_KERNEL);
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100151 if (!p)
152 return NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200153
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100154 memcpy(p->name, name, len);
155 p->name[len] = '\0';
156 p->len = len;
157 p->type = d_type;
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300158 p->real_ino = ino;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100159 p->ino = ino;
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300160 /* Defer setting d_ino for upper entry to ovl_iterate() */
161 if (ovl_calc_d_ino(rdd, p))
162 p->ino = 0;
zhangyi (F)95e598e2017-10-31 22:57:00 +0200163 p->is_upper = rdd->is_upper;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100164 p->is_whiteout = false;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100165
166 if (d_type == DT_CHR) {
Miklos Szeredicdb67272015-06-22 13:53:48 +0200167 p->next_maybe_whiteout = rdd->first_maybe_whiteout;
168 rdd->first_maybe_whiteout = p;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100169 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200170 return p;
171}
172
173static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
174 const char *name, int len, u64 ino,
175 unsigned int d_type)
176{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200177 struct rb_node **newp = &rdd->root->rb_node;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200178 struct rb_node *parent = NULL;
179 struct ovl_cache_entry *p;
180
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200181 if (ovl_cache_entry_find_link(name, len, &newp, &parent))
182 return 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200183
Miklos Szeredicdb67272015-06-22 13:53:48 +0200184 p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
Miklos Szeredi31e8cce2017-07-27 21:54:06 +0200185 if (p == NULL) {
186 rdd->err = -ENOMEM;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200187 return -ENOMEM;
Miklos Szeredi31e8cce2017-07-27 21:54:06 +0200188 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200189
190 list_add_tail(&p->l_node, rdd->list);
191 rb_link_node(&p->node, parent, newp);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200192 rb_insert_color(&p->node, rdd->root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200193
194 return 0;
195}
196
Miklos Szeredi56656e92016-03-21 17:31:46 +0100197static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
198 const char *name, int namelen,
199 loff_t offset, u64 ino, unsigned int d_type)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200200{
201 struct ovl_cache_entry *p;
202
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200203 p = ovl_cache_entry_find(rdd->root, name, namelen);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200204 if (p) {
Al Virodb6ec212014-10-23 23:03:03 -0400205 list_move_tail(&p->l_node, &rdd->middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200206 } else {
Miklos Szeredicdb67272015-06-22 13:53:48 +0200207 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200208 if (p == NULL)
209 rdd->err = -ENOMEM;
210 else
Al Virodb6ec212014-10-23 23:03:03 -0400211 list_add_tail(&p->l_node, &rdd->middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200212 }
213
214 return rdd->err;
215}
216
217void ovl_cache_free(struct list_head *list)
218{
219 struct ovl_cache_entry *p;
220 struct ovl_cache_entry *n;
221
222 list_for_each_entry_safe(p, n, list, l_node)
223 kfree(p);
224
225 INIT_LIST_HEAD(list);
226}
227
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200228void ovl_dir_cache_free(struct inode *inode)
229{
230 struct ovl_dir_cache *cache = ovl_dir_cache(inode);
231
232 if (cache) {
233 ovl_cache_free(&cache->entries);
234 kfree(cache);
235 }
236}
237
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200238static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
239{
240 struct ovl_dir_cache *cache = od->cache;
241
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200242 WARN_ON(cache->refcount <= 0);
243 cache->refcount--;
244 if (!cache->refcount) {
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200245 if (ovl_dir_cache(d_inode(dentry)) == cache)
246 ovl_set_dir_cache(d_inode(dentry), NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200247
248 ovl_cache_free(&cache->entries);
249 kfree(cache);
250 }
251}
252
Miklos Szerediac7576f2014-10-30 17:37:34 +0100253static int ovl_fill_merge(struct dir_context *ctx, const char *name,
254 int namelen, loff_t offset, u64 ino,
255 unsigned int d_type)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200256{
Miklos Szerediac7576f2014-10-30 17:37:34 +0100257 struct ovl_readdir_data *rdd =
258 container_of(ctx, struct ovl_readdir_data, ctx);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200259
260 rdd->count++;
Miklos Szeredi56656e92016-03-21 17:31:46 +0100261 if (!rdd->is_lowest)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200262 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
263 else
Miklos Szeredi56656e92016-03-21 17:31:46 +0100264 return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200265}
266
Miklos Szeredicdb67272015-06-22 13:53:48 +0200267static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
268{
269 int err;
270 struct ovl_cache_entry *p;
271 struct dentry *dentry;
272 const struct cred *old_cred;
Miklos Szeredicdb67272015-06-22 13:53:48 +0200273
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200274 old_cred = ovl_override_creds(rdd->dentry->d_sb);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200275
Al Viro00235412016-05-26 00:05:12 -0400276 err = down_write_killable(&dir->d_inode->i_rwsem);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200277 if (!err) {
278 while (rdd->first_maybe_whiteout) {
279 p = rdd->first_maybe_whiteout;
280 rdd->first_maybe_whiteout = p->next_maybe_whiteout;
281 dentry = lookup_one_len(p->name, dir, p->len);
282 if (!IS_ERR(dentry)) {
283 p->is_whiteout = ovl_is_whiteout(dentry);
284 dput(dentry);
285 }
286 }
Al Viro59551022016-01-22 15:40:57 -0500287 inode_unlock(dir->d_inode);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200288 }
289 revert_creds(old_cred);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200290
291 return err;
292}
293
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200294static inline int ovl_dir_read(struct path *realpath,
295 struct ovl_readdir_data *rdd)
296{
297 struct file *realfile;
298 int err;
299
Miklos Szeredi130fdbc2020-06-02 22:20:25 +0200300 realfile = ovl_path_open(realpath, O_RDONLY | O_LARGEFILE);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200301 if (IS_ERR(realfile))
302 return PTR_ERR(realfile);
303
Miklos Szeredicdb67272015-06-22 13:53:48 +0200304 rdd->first_maybe_whiteout = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305 rdd->ctx.pos = 0;
306 do {
307 rdd->count = 0;
308 rdd->err = 0;
309 err = iterate_dir(realfile, &rdd->ctx);
310 if (err >= 0)
311 err = rdd->err;
312 } while (!err && rdd->count);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200313
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200314 if (!err && rdd->first_maybe_whiteout && rdd->dentry)
Miklos Szeredicdb67272015-06-22 13:53:48 +0200315 err = ovl_check_whiteouts(realpath->dentry, rdd);
316
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200317 fput(realfile);
318
319 return err;
320}
321
322static void ovl_dir_reset(struct file *file)
323{
324 struct ovl_dir_file *od = file->private_data;
325 struct ovl_dir_cache *cache = od->cache;
326 struct dentry *dentry = file->f_path.dentry;
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300327 bool is_real;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200328
329 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
330 ovl_cache_put(od, dentry);
331 od->cache = NULL;
hujianyang43303972014-12-11 10:30:18 +0800332 od->cursor = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200333 }
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300334 is_real = ovl_dir_is_real(dentry);
335 if (od->is_real != is_real) {
336 /* is_real can only become false when dir is copied up */
337 if (WARN_ON(is_real))
338 return;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339 od->is_real = false;
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300340 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200341}
342
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200343static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list,
344 struct rb_root *root)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200345{
346 int err;
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100347 struct path realpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200348 struct ovl_readdir_data rdd = {
349 .ctx.actor = ovl_fill_merge,
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200350 .dentry = dentry,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200351 .list = list,
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200352 .root = root,
Miklos Szeredi56656e92016-03-21 17:31:46 +0100353 .is_lowest = false,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200354 };
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100355 int idx, next;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200356
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100357 for (idx = 0; idx != -1; idx = next) {
358 next = ovl_path_next(idx, dentry, &realpath);
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300359 rdd.is_upper = ovl_dentry_upper(dentry) == realpath.dentry;
Miklos Szeredic9f00fd2014-11-20 16:40:01 +0100360
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100361 if (next != -1) {
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100362 err = ovl_dir_read(&realpath, &rdd);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200363 if (err)
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100364 break;
365 } else {
366 /*
367 * Insert lowest layer entries before upper ones, this
368 * allows offsets to be reasonably constant
369 */
370 list_add(&rdd.middle, rdd.list);
Miklos Szeredi56656e92016-03-21 17:31:46 +0100371 rdd.is_lowest = true;
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100372 err = ovl_dir_read(&realpath, &rdd);
373 list_del(&rdd.middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200374 }
375 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200376 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200377}
378
379static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
380{
hujianyang43303972014-12-11 10:30:18 +0800381 struct list_head *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200382 loff_t off = 0;
383
hujianyang43303972014-12-11 10:30:18 +0800384 list_for_each(p, &od->cache->entries) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200385 if (off >= pos)
386 break;
387 off++;
388 }
hujianyang43303972014-12-11 10:30:18 +0800389 /* Cursor is safe since the cache is stable */
390 od->cursor = p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200391}
392
393static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
394{
395 int res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200396 struct ovl_dir_cache *cache;
397
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200398 cache = ovl_dir_cache(d_inode(dentry));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200399 if (cache && ovl_dentry_version_get(dentry) == cache->version) {
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200400 WARN_ON(!cache->refcount);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200401 cache->refcount++;
402 return cache;
403 }
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200404 ovl_set_dir_cache(d_inode(dentry), NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200405
406 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
407 if (!cache)
408 return ERR_PTR(-ENOMEM);
409
410 cache->refcount = 1;
411 INIT_LIST_HEAD(&cache->entries);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200412 cache->root = RB_ROOT;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200413
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200414 res = ovl_dir_read_merged(dentry, &cache->entries, &cache->root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200415 if (res) {
416 ovl_cache_free(&cache->entries);
417 kfree(cache);
418 return ERR_PTR(res);
419 }
420
421 cache->version = ovl_dentry_version_get(dentry);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200422 ovl_set_dir_cache(d_inode(dentry), cache);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200423
424 return cache;
425}
426
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200427/* Map inode number to lower fs unique range */
428static u64 ovl_remap_lower_ino(u64 ino, int xinobits, int fsid,
Amir Goldstein926e94d2020-02-21 16:34:45 +0200429 const char *name, int namelen, bool warn)
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200430{
Amir Goldsteindfe51d42020-02-21 16:34:44 +0200431 unsigned int xinoshift = 64 - xinobits;
432
433 if (unlikely(ino >> xinoshift)) {
Amir Goldstein926e94d2020-02-21 16:34:45 +0200434 if (warn) {
435 pr_warn_ratelimited("d_ino too big (%.*s, ino=%llu, xinobits=%d)\n",
436 namelen, name, ino, xinobits);
437 }
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200438 return ino;
439 }
440
Amir Goldsteindfe51d42020-02-21 16:34:44 +0200441 /*
442 * The lowest xinobit is reserved for mapping the non-peresistent inode
443 * numbers range, but this range is only exposed via st_ino, not here.
444 */
445 return ino | ((u64)fsid) << (xinoshift + 1);
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200446}
447
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300448/*
449 * Set d_ino for upper entries. Non-upper entries should always report
450 * the uppermost real inode ino and should not call this function.
451 *
452 * When not all layer are on same fs, report real ino also for upper.
453 *
454 * When all layers are on the same fs, and upper has a reference to
455 * copy up origin, call vfs_getattr() on the overlay entry to make
456 * sure that d_ino will be consistent with st_ino from stat(2).
457 */
458static int ovl_cache_update_ino(struct path *path, struct ovl_cache_entry *p)
459
460{
461 struct dentry *dir = path->dentry;
462 struct dentry *this = NULL;
463 enum ovl_path_type type;
464 u64 ino = p->real_ino;
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200465 int xinobits = ovl_xino_bits(dir->d_sb);
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300466 int err = 0;
467
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200468 if (!ovl_same_dev(dir->d_sb))
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300469 goto out;
470
471 if (p->name[0] == '.') {
472 if (p->len == 1) {
473 this = dget(dir);
474 goto get;
475 }
476 if (p->len == 2 && p->name[1] == '.') {
477 /* we shall not be moved */
478 this = dget(dir->d_parent);
479 goto get;
480 }
481 }
482 this = lookup_one_len(p->name, dir, p->len);
483 if (IS_ERR_OR_NULL(this) || !this->d_inode) {
Amir Goldstein9011c272021-04-26 18:20:21 +0300484 /* Mark a stale entry */
485 p->is_whiteout = true;
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300486 if (IS_ERR(this)) {
487 err = PTR_ERR(this);
488 this = NULL;
489 goto fail;
490 }
491 goto out;
492 }
493
494get:
495 type = ovl_path_type(this);
496 if (OVL_TYPE_ORIGIN(type)) {
497 struct kstat stat;
498 struct path statpath = *path;
499
500 statpath.dentry = this;
501 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
502 if (err)
503 goto fail;
504
Amir Goldstein4c37e712019-12-22 22:47:54 +0200505 /*
506 * Directory inode is always on overlay st_dev.
507 * Non-dir with ovl_same_dev() could be on pseudo st_dev in case
508 * of xino bits overflow.
509 */
510 WARN_ON_ONCE(S_ISDIR(stat.mode) &&
511 dir->d_sb->s_dev != stat.dev);
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300512 ino = stat.ino;
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200513 } else if (xinobits && !OVL_TYPE_UPPER(type)) {
514 ino = ovl_remap_lower_ino(ino, xinobits,
515 ovl_layer_lower(this)->fsid,
Amir Goldstein926e94d2020-02-21 16:34:45 +0200516 p->name, p->len,
517 ovl_xino_warn(dir->d_sb));
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300518 }
519
520out:
521 p->ino = ino;
522 dput(this);
523 return err;
524
525fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800526 pr_warn_ratelimited("failed to look up (%s) for ino (%i)\n",
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300527 p->name, err);
528 goto out;
529}
530
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200531static int ovl_fill_plain(struct dir_context *ctx, const char *name,
532 int namelen, loff_t offset, u64 ino,
533 unsigned int d_type)
534{
535 struct ovl_cache_entry *p;
536 struct ovl_readdir_data *rdd =
537 container_of(ctx, struct ovl_readdir_data, ctx);
538
539 rdd->count++;
540 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
541 if (p == NULL) {
542 rdd->err = -ENOMEM;
543 return -ENOMEM;
544 }
545 list_add_tail(&p->l_node, rdd->list);
546
547 return 0;
548}
549
550static int ovl_dir_read_impure(struct path *path, struct list_head *list,
551 struct rb_root *root)
552{
553 int err;
554 struct path realpath;
555 struct ovl_cache_entry *p, *n;
556 struct ovl_readdir_data rdd = {
557 .ctx.actor = ovl_fill_plain,
558 .list = list,
559 .root = root,
560 };
561
562 INIT_LIST_HEAD(list);
563 *root = RB_ROOT;
564 ovl_path_upper(path->dentry, &realpath);
565
566 err = ovl_dir_read(&realpath, &rdd);
567 if (err)
568 return err;
569
570 list_for_each_entry_safe(p, n, list, l_node) {
571 if (strcmp(p->name, ".") != 0 &&
572 strcmp(p->name, "..") != 0) {
573 err = ovl_cache_update_ino(path, p);
574 if (err)
575 return err;
576 }
577 if (p->ino == p->real_ino) {
578 list_del(&p->l_node);
579 kfree(p);
580 } else {
581 struct rb_node **newp = &root->rb_node;
582 struct rb_node *parent = NULL;
583
584 if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len,
585 &newp, &parent)))
586 return -EIO;
587
588 rb_link_node(&p->node, parent, newp);
589 rb_insert_color(&p->node, root);
590 }
591 }
592 return 0;
593}
594
595static struct ovl_dir_cache *ovl_cache_get_impure(struct path *path)
596{
597 int res;
598 struct dentry *dentry = path->dentry;
Miklos Szeredi610afc02020-09-02 10:58:49 +0200599 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200600 struct ovl_dir_cache *cache;
601
602 cache = ovl_dir_cache(d_inode(dentry));
603 if (cache && ovl_dentry_version_get(dentry) == cache->version)
604 return cache;
605
606 /* Impure cache is not refcounted, free it here */
607 ovl_dir_cache_free(d_inode(dentry));
608 ovl_set_dir_cache(d_inode(dentry), NULL);
609
610 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
611 if (!cache)
612 return ERR_PTR(-ENOMEM);
613
614 res = ovl_dir_read_impure(path, &cache->entries, &cache->root);
615 if (res) {
616 ovl_cache_free(&cache->entries);
617 kfree(cache);
618 return ERR_PTR(res);
619 }
620 if (list_empty(&cache->entries)) {
Amir Goldsteina5a927a2018-01-03 18:54:42 +0200621 /*
622 * A good opportunity to get rid of an unneeded "impure" flag.
623 * Removing the "impure" xattr is best effort.
624 */
625 if (!ovl_want_write(dentry)) {
Miklos Szeredi610afc02020-09-02 10:58:49 +0200626 ovl_do_removexattr(ofs, ovl_dentry_upper(dentry),
Amir Goldsteina5a927a2018-01-03 18:54:42 +0200627 OVL_XATTR_IMPURE);
628 ovl_drop_write(dentry);
629 }
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200630 ovl_clear_flag(OVL_IMPURE, d_inode(dentry));
631 kfree(cache);
632 return NULL;
633 }
634
635 cache->version = ovl_dentry_version_get(dentry);
636 ovl_set_dir_cache(d_inode(dentry), cache);
637
638 return cache;
639}
640
641struct ovl_readdir_translate {
642 struct dir_context *orig_ctx;
643 struct ovl_dir_cache *cache;
644 struct dir_context ctx;
645 u64 parent_ino;
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200646 int fsid;
647 int xinobits;
Amir Goldstein926e94d2020-02-21 16:34:45 +0200648 bool xinowarn;
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200649};
650
651static int ovl_fill_real(struct dir_context *ctx, const char *name,
652 int namelen, loff_t offset, u64 ino,
653 unsigned int d_type)
654{
655 struct ovl_readdir_translate *rdt =
656 container_of(ctx, struct ovl_readdir_translate, ctx);
657 struct dir_context *orig_ctx = rdt->orig_ctx;
658
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200659 if (rdt->parent_ino && strcmp(name, "..") == 0) {
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200660 ino = rdt->parent_ino;
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200661 } else if (rdt->cache) {
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200662 struct ovl_cache_entry *p;
663
664 p = ovl_cache_entry_find(&rdt->cache->root, name, namelen);
665 if (p)
666 ino = p->ino;
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200667 } else if (rdt->xinobits) {
668 ino = ovl_remap_lower_ino(ino, rdt->xinobits, rdt->fsid,
Amir Goldstein926e94d2020-02-21 16:34:45 +0200669 name, namelen, rdt->xinowarn);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200670 }
671
672 return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type);
673}
674
Amir Goldstein67810692018-07-17 16:05:38 +0300675static bool ovl_is_impure_dir(struct file *file)
676{
677 struct ovl_dir_file *od = file->private_data;
678 struct inode *dir = d_inode(file->f_path.dentry);
679
680 /*
681 * Only upper dir can be impure, but if we are in the middle of
682 * iterating a lower real dir, dir could be copied up and marked
683 * impure. We only want the impure cache if we started iterating
684 * a real upper dir to begin with.
685 */
686 return od->is_upper && ovl_test_flag(OVL_IMPURE, dir);
687
688}
689
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200690static int ovl_iterate_real(struct file *file, struct dir_context *ctx)
691{
692 int err;
693 struct ovl_dir_file *od = file->private_data;
694 struct dentry *dir = file->f_path.dentry;
Miklos Szeredi13464162020-01-24 09:46:45 +0100695 const struct ovl_layer *lower_layer = ovl_layer_lower(dir);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200696 struct ovl_readdir_translate rdt = {
697 .ctx.actor = ovl_fill_real,
698 .orig_ctx = ctx,
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200699 .xinobits = ovl_xino_bits(dir->d_sb),
Amir Goldstein926e94d2020-02-21 16:34:45 +0200700 .xinowarn = ovl_xino_warn(dir->d_sb),
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200701 };
702
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200703 if (rdt.xinobits && lower_layer)
704 rdt.fsid = lower_layer->fsid;
705
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200706 if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) {
707 struct kstat stat;
708 struct path statpath = file->f_path;
709
710 statpath.dentry = dir->d_parent;
711 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
712 if (err)
713 return err;
714
715 WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
716 rdt.parent_ino = stat.ino;
717 }
718
Amir Goldstein67810692018-07-17 16:05:38 +0300719 if (ovl_is_impure_dir(file)) {
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200720 rdt.cache = ovl_cache_get_impure(&file->f_path);
721 if (IS_ERR(rdt.cache))
722 return PTR_ERR(rdt.cache);
723 }
724
Amir Goldsteinb02a16e2017-11-29 07:35:21 +0200725 err = iterate_dir(od->realfile, &rdt.ctx);
726 ctx->pos = rdt.ctx.pos;
727
728 return err;
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200729}
730
731
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200732static int ovl_iterate(struct file *file, struct dir_context *ctx)
733{
734 struct ovl_dir_file *od = file->private_data;
735 struct dentry *dentry = file->f_path.dentry;
hujianyang43303972014-12-11 10:30:18 +0800736 struct ovl_cache_entry *p;
Miklos Szeredi48bd0242020-06-02 22:20:25 +0200737 const struct cred *old_cred;
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300738 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200739
Miklos Szeredi48bd0242020-06-02 22:20:25 +0200740 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200741 if (!ctx->pos)
742 ovl_dir_reset(file);
743
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200744 if (od->is_real) {
745 /*
746 * If parent is merge, then need to adjust d_ino for '..', if
747 * dir is impure then need to adjust d_ino for copied up
748 * entries.
749 */
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200750 if (ovl_xino_bits(dentry->d_sb) ||
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200751 (ovl_same_fs(dentry->d_sb) &&
Amir Goldstein67810692018-07-17 16:05:38 +0300752 (ovl_is_impure_dir(file) ||
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200753 OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent))))) {
Miklos Szeredi48bd0242020-06-02 22:20:25 +0200754 err = ovl_iterate_real(file, ctx);
755 } else {
756 err = iterate_dir(od->realfile, ctx);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200757 }
Miklos Szeredi48bd0242020-06-02 22:20:25 +0200758 goto out;
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200759 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200760
761 if (!od->cache) {
762 struct ovl_dir_cache *cache;
763
764 cache = ovl_cache_get(dentry);
Miklos Szeredi48bd0242020-06-02 22:20:25 +0200765 err = PTR_ERR(cache);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200766 if (IS_ERR(cache))
Miklos Szeredi48bd0242020-06-02 22:20:25 +0200767 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200768
769 od->cache = cache;
770 ovl_seek_cursor(od, ctx->pos);
771 }
772
hujianyang43303972014-12-11 10:30:18 +0800773 while (od->cursor != &od->cache->entries) {
774 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300775 if (!p->is_whiteout) {
776 if (!p->ino) {
777 err = ovl_cache_update_ino(&file->f_path, p);
778 if (err)
Miklos Szeredi48bd0242020-06-02 22:20:25 +0200779 goto out;
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300780 }
Amir Goldstein9011c272021-04-26 18:20:21 +0300781 }
782 /* ovl_cache_update_ino() sets is_whiteout on stale entry */
783 if (!p->is_whiteout) {
hujianyang43303972014-12-11 10:30:18 +0800784 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
785 break;
Amir Goldsteinb5efccb2017-05-11 16:42:27 +0300786 }
hujianyang43303972014-12-11 10:30:18 +0800787 od->cursor = p->l_node.next;
788 ctx->pos++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200789 }
Miklos Szeredi48bd0242020-06-02 22:20:25 +0200790 err = 0;
791out:
792 revert_creds(old_cred);
793 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200794}
795
796static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
797{
798 loff_t res;
799 struct ovl_dir_file *od = file->private_data;
800
Al Viro59551022016-01-22 15:40:57 -0500801 inode_lock(file_inode(file));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200802 if (!file->f_pos)
803 ovl_dir_reset(file);
804
805 if (od->is_real) {
806 res = vfs_llseek(od->realfile, offset, origin);
807 file->f_pos = od->realfile->f_pos;
808 } else {
809 res = -EINVAL;
810
811 switch (origin) {
812 case SEEK_CUR:
813 offset += file->f_pos;
814 break;
815 case SEEK_SET:
816 break;
817 default:
818 goto out_unlock;
819 }
820 if (offset < 0)
821 goto out_unlock;
822
823 if (offset != file->f_pos) {
824 file->f_pos = offset;
825 if (od->cache)
826 ovl_seek_cursor(od, offset);
827 }
828 res = offset;
829 }
830out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500831 inode_unlock(file_inode(file));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200832
833 return res;
834}
835
Amir Goldstein61536be2020-09-29 15:28:47 +0800836static struct file *ovl_dir_open_realfile(const struct file *file,
Miklos Szeredi130fdbc2020-06-02 22:20:25 +0200837 struct path *realpath)
838{
Miklos Szeredi48bd0242020-06-02 22:20:25 +0200839 struct file *res;
840 const struct cred *old_cred;
841
842 old_cred = ovl_override_creds(file_inode(file)->i_sb);
843 res = ovl_path_open(realpath, O_RDONLY | (file->f_flags & O_LARGEFILE));
844 revert_creds(old_cred);
845
846 return res;
Miklos Szeredi130fdbc2020-06-02 22:20:25 +0200847}
848
Amir Goldstein61536be2020-09-29 15:28:47 +0800849/*
850 * Like ovl_real_fdget(), returns upperfile if dir was copied up since open.
851 * Unlike ovl_real_fdget(), this caches upperfile in file->private_data.
852 *
853 * TODO: use same abstract type for file->private_data of dir and file so
854 * upperfile could also be cached for files as well.
855 */
856struct file *ovl_dir_real_file(const struct file *file, bool want_upper)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200857{
Amir Goldstein61536be2020-09-29 15:28:47 +0800858
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200859 struct ovl_dir_file *od = file->private_data;
860 struct dentry *dentry = file->f_path.dentry;
Miklos Szeredib854cc62021-01-05 08:36:11 +0800861 struct file *old, *realfile = od->realfile;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200862
Amir Goldsteind796e772017-11-08 09:39:46 +0200863 if (!OVL_TYPE_UPPER(ovl_path_type(dentry)))
Amir Goldstein61536be2020-09-29 15:28:47 +0800864 return want_upper ? NULL : realfile;
Vivek Goyalc86243b02020-08-31 14:15:29 -0400865
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200866 /*
867 * Need to check if we started out being a lower dir, but got copied up
868 */
Amir Goldsteind796e772017-11-08 09:39:46 +0200869 if (!od->is_upper) {
Will Deacon506458e2017-10-24 11:22:48 +0100870 realfile = READ_ONCE(od->upperfile);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200871 if (!realfile) {
872 struct path upperpath;
873
874 ovl_path_upper(dentry, &upperpath);
Miklos Szeredi130fdbc2020-06-02 22:20:25 +0200875 realfile = ovl_dir_open_realfile(file, &upperpath);
Miklos Szeredib854cc62021-01-05 08:36:11 +0800876 if (IS_ERR(realfile))
877 return realfile;
Peter Zijlstraff7a5fb2017-06-07 17:43:46 +0200878
Miklos Szeredib854cc62021-01-05 08:36:11 +0800879 old = cmpxchg_release(&od->upperfile, NULL, realfile);
880 if (old) {
881 fput(realfile);
882 realfile = old;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200883 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200884 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200885 }
886
Amir Goldstein61536be2020-09-29 15:28:47 +0800887 return realfile;
888}
889
890static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
891 int datasync)
892{
893 struct file *realfile;
894 int err;
895
Sargun Dhillon335d3fc2021-01-07 16:10:43 -0800896 err = ovl_sync_status(OVL_FS(file->f_path.dentry->d_sb));
897 if (err <= 0)
898 return err;
Amir Goldstein61536be2020-09-29 15:28:47 +0800899
900 realfile = ovl_dir_real_file(file, true);
901 err = PTR_ERR_OR_ZERO(realfile);
902
903 /* Nothing to sync for lower */
904 if (!realfile || err)
905 return err;
906
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200907 return vfs_fsync_range(realfile, start, end, datasync);
908}
909
910static int ovl_dir_release(struct inode *inode, struct file *file)
911{
912 struct ovl_dir_file *od = file->private_data;
913
914 if (od->cache) {
Al Viro59551022016-01-22 15:40:57 -0500915 inode_lock(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200916 ovl_cache_put(od, file->f_path.dentry);
Al Viro59551022016-01-22 15:40:57 -0500917 inode_unlock(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200918 }
919 fput(od->realfile);
920 if (od->upperfile)
921 fput(od->upperfile);
922 kfree(od);
923
924 return 0;
925}
926
927static int ovl_dir_open(struct inode *inode, struct file *file)
928{
929 struct path realpath;
930 struct file *realfile;
931 struct ovl_dir_file *od;
932 enum ovl_path_type type;
933
934 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
935 if (!od)
936 return -ENOMEM;
937
938 type = ovl_path_real(file->f_path.dentry, &realpath);
Miklos Szeredi130fdbc2020-06-02 22:20:25 +0200939 realfile = ovl_dir_open_realfile(file, &realpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200940 if (IS_ERR(realfile)) {
941 kfree(od);
942 return PTR_ERR(realfile);
943 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200944 od->realfile = realfile;
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300945 od->is_real = ovl_dir_is_real(file->f_path.dentry);
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100946 od->is_upper = OVL_TYPE_UPPER(type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200947 file->private_data = od;
948
949 return 0;
950}
951
952const struct file_operations ovl_dir_operations = {
953 .read = generic_read_dir,
954 .open = ovl_dir_open,
955 .iterate = ovl_iterate,
956 .llseek = ovl_dir_llseek,
957 .fsync = ovl_dir_fsync,
958 .release = ovl_dir_release,
959};
960
961int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
962{
963 int err;
zhangyi (F)95e598e2017-10-31 22:57:00 +0200964 struct ovl_cache_entry *p, *n;
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200965 struct rb_root root = RB_ROOT;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200966 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200967
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200968 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200969 err = ovl_dir_read_merged(dentry, list, &root);
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200970 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200971 if (err)
972 return err;
973
974 err = 0;
975
zhangyi (F)95e598e2017-10-31 22:57:00 +0200976 list_for_each_entry_safe(p, n, list, l_node) {
977 /*
978 * Select whiteouts in upperdir, they should
979 * be cleared when deleting this directory.
980 */
981 if (p->is_whiteout) {
982 if (p->is_upper)
983 continue;
984 goto del_entry;
985 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200986
987 if (p->name[0] == '.') {
988 if (p->len == 1)
zhangyi (F)95e598e2017-10-31 22:57:00 +0200989 goto del_entry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200990 if (p->len == 2 && p->name[1] == '.')
zhangyi (F)95e598e2017-10-31 22:57:00 +0200991 goto del_entry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200992 }
993 err = -ENOTEMPTY;
994 break;
zhangyi (F)95e598e2017-10-31 22:57:00 +0200995
996del_entry:
997 list_del(&p->l_node);
998 kfree(p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200999 }
1000
1001 return err;
1002}
1003
1004void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
1005{
1006 struct ovl_cache_entry *p;
1007
Al Viro59551022016-01-22 15:40:57 -05001008 inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001009 list_for_each_entry(p, list, l_node) {
1010 struct dentry *dentry;
1011
zhangyi (F)95e598e2017-10-31 22:57:00 +02001012 if (WARN_ON(!p->is_whiteout || !p->is_upper))
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001013 continue;
1014
1015 dentry = lookup_one_len(p->name, upper, p->len);
1016 if (IS_ERR(dentry)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +08001017 pr_err("lookup '%s/%.*s' failed (%i)\n",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001018 upper->d_name.name, p->len, p->name,
1019 (int) PTR_ERR(dentry));
1020 continue;
1021 }
Konstantin Khlebnikov84889d42015-11-16 18:44:11 +03001022 if (dentry->d_inode)
1023 ovl_cleanup(upper->d_inode, dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001024 dput(dentry);
1025 }
Al Viro59551022016-01-22 15:40:57 -05001026 inode_unlock(upper->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001027}
Vivek Goyal45aebea2016-02-22 09:28:34 -05001028
1029static int ovl_check_d_type(struct dir_context *ctx, const char *name,
1030 int namelen, loff_t offset, u64 ino,
1031 unsigned int d_type)
1032{
1033 struct ovl_readdir_data *rdd =
1034 container_of(ctx, struct ovl_readdir_data, ctx);
1035
1036 /* Even if d_type is not supported, DT_DIR is returned for . and .. */
1037 if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
1038 return 0;
1039
1040 if (d_type != DT_UNKNOWN)
1041 rdd->d_type_supported = true;
1042
1043 return 0;
1044}
1045
1046/*
1047 * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
1048 * if error is encountered.
1049 */
1050int ovl_check_d_type_supported(struct path *realpath)
1051{
1052 int err;
1053 struct ovl_readdir_data rdd = {
1054 .ctx.actor = ovl_check_d_type,
1055 .d_type_supported = false,
1056 };
1057
1058 err = ovl_dir_read(realpath, &rdd);
1059 if (err)
1060 return err;
1061
1062 return rdd.d_type_supported;
1063}
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001064
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001065#define OVL_INCOMPATDIR_NAME "incompat"
1066
1067static int ovl_workdir_cleanup_recurse(struct path *path, int level)
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001068{
1069 int err;
1070 struct inode *dir = path->dentry->d_inode;
1071 LIST_HEAD(list);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +02001072 struct rb_root root = RB_ROOT;
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001073 struct ovl_cache_entry *p;
1074 struct ovl_readdir_data rdd = {
1075 .ctx.actor = ovl_fill_merge,
1076 .dentry = NULL,
1077 .list = &list,
Miklos Szeredi4edb83b2017-07-27 21:54:06 +02001078 .root = &root,
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001079 .is_lowest = false,
1080 };
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001081 bool incompat = false;
1082
1083 /*
1084 * The "work/incompat" directory is treated specially - if it is not
1085 * empty, instead of printing a generic error and mounting read-only,
1086 * we will error about incompat features and fail the mount.
1087 *
1088 * When called from ovl_indexdir_cleanup(), path->dentry->d_name.name
1089 * starts with '#'.
1090 */
1091 if (level == 2 &&
1092 !strcmp(path->dentry->d_name.name, OVL_INCOMPATDIR_NAME))
1093 incompat = true;
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001094
1095 err = ovl_dir_read(path, &rdd);
1096 if (err)
1097 goto out;
1098
1099 inode_lock_nested(dir, I_MUTEX_PARENT);
1100 list_for_each_entry(p, &list, l_node) {
1101 struct dentry *dentry;
1102
1103 if (p->name[0] == '.') {
1104 if (p->len == 1)
1105 continue;
1106 if (p->len == 2 && p->name[1] == '.')
1107 continue;
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001108 } else if (incompat) {
1109 pr_err("overlay with incompat feature '%s' cannot be mounted\n",
1110 p->name);
1111 err = -EINVAL;
1112 break;
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001113 }
1114 dentry = lookup_one_len(p->name, path->dentry, p->len);
1115 if (IS_ERR(dentry))
1116 continue;
1117 if (dentry->d_inode)
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001118 err = ovl_workdir_cleanup(dir, path->mnt, dentry, level);
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001119 dput(dentry);
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001120 if (err)
1121 break;
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001122 }
1123 inode_unlock(dir);
1124out:
1125 ovl_cache_free(&list);
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001126 return err;
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001127}
1128
Amir Goldstein30116452020-04-03 07:58:49 +03001129int ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001130 struct dentry *dentry, int level)
1131{
1132 int err;
1133
1134 if (!d_is_dir(dentry) || level > 1) {
Amir Goldstein30116452020-04-03 07:58:49 +03001135 return ovl_cleanup(dir, dentry);
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001136 }
1137
1138 err = ovl_do_rmdir(dir, dentry);
1139 if (err) {
1140 struct path path = { .mnt = mnt, .dentry = dentry };
1141
1142 inode_unlock(dir);
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001143 err = ovl_workdir_cleanup_recurse(&path, level + 1);
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001144 inode_lock_nested(dir, I_MUTEX_PARENT);
Amir Goldstein235ce9e2020-08-30 23:28:03 +03001145 if (!err)
1146 err = ovl_cleanup(dir, dentry);
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001147 }
Amir Goldstein30116452020-04-03 07:58:49 +03001148
1149 return err;
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001150}
Amir Goldstein415543d2017-06-21 15:28:42 +03001151
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001152int ovl_indexdir_cleanup(struct ovl_fs *ofs)
Amir Goldstein415543d2017-06-21 15:28:42 +03001153{
1154 int err;
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001155 struct dentry *indexdir = ofs->indexdir;
Amir Goldsteindc7ab672017-09-24 22:19:10 +03001156 struct dentry *index = NULL;
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001157 struct inode *dir = indexdir->d_inode;
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +02001158 struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = indexdir };
Amir Goldstein415543d2017-06-21 15:28:42 +03001159 LIST_HEAD(list);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +02001160 struct rb_root root = RB_ROOT;
Amir Goldstein415543d2017-06-21 15:28:42 +03001161 struct ovl_cache_entry *p;
1162 struct ovl_readdir_data rdd = {
1163 .ctx.actor = ovl_fill_merge,
1164 .dentry = NULL,
1165 .list = &list,
Miklos Szeredi4edb83b2017-07-27 21:54:06 +02001166 .root = &root,
Amir Goldstein415543d2017-06-21 15:28:42 +03001167 .is_lowest = false,
1168 };
1169
1170 err = ovl_dir_read(&path, &rdd);
1171 if (err)
1172 goto out;
1173
1174 inode_lock_nested(dir, I_MUTEX_PARENT);
1175 list_for_each_entry(p, &list, l_node) {
Amir Goldstein415543d2017-06-21 15:28:42 +03001176 if (p->name[0] == '.') {
1177 if (p->len == 1)
1178 continue;
1179 if (p->len == 2 && p->name[1] == '.')
1180 continue;
1181 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001182 index = lookup_one_len(p->name, indexdir, p->len);
Amir Goldstein415543d2017-06-21 15:28:42 +03001183 if (IS_ERR(index)) {
1184 err = PTR_ERR(index);
Amir Goldsteindc7ab672017-09-24 22:19:10 +03001185 index = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +03001186 break;
1187 }
Amir Goldstein30116452020-04-03 07:58:49 +03001188 /* Cleanup leftover from index create/cleanup attempt */
1189 if (index->d_name.name[0] == '#') {
1190 err = ovl_workdir_cleanup(dir, path.mnt, index, 1);
1191 if (err)
1192 break;
1193 goto next;
1194 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001195 err = ovl_verify_index(ofs, index);
Amir Goldstein24f0b172018-01-11 15:33:51 +02001196 if (!err) {
1197 goto next;
1198 } else if (err == -ESTALE) {
1199 /* Cleanup stale index entries */
Amir Goldstein415543d2017-06-21 15:28:42 +03001200 err = ovl_cleanup(dir, index);
Amir Goldstein24f0b172018-01-11 15:33:51 +02001201 } else if (err != -ENOENT) {
1202 /*
1203 * Abort mount to avoid corrupting the index if
1204 * an incompatible index entry was found or on out
1205 * of memory.
1206 */
1207 break;
1208 } else if (ofs->config.nfs_export) {
1209 /*
1210 * Whiteout orphan index to block future open by
1211 * handle after overlay nlink dropped to zero.
1212 */
Chengguang Xuc21c8392020-04-24 10:55:17 +08001213 err = ovl_cleanup_and_whiteout(ofs, dir, index);
Amir Goldstein24f0b172018-01-11 15:33:51 +02001214 } else {
1215 /* Cleanup orphan index entries */
1216 err = ovl_cleanup(dir, index);
1217 }
1218
Amir Goldsteinfa0096e2017-10-24 12:24:11 +03001219 if (err)
1220 break;
1221
Amir Goldstein24f0b172018-01-11 15:33:51 +02001222next:
Amir Goldstein415543d2017-06-21 15:28:42 +03001223 dput(index);
Amir Goldsteindc7ab672017-09-24 22:19:10 +03001224 index = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +03001225 }
Amir Goldsteindc7ab672017-09-24 22:19:10 +03001226 dput(index);
Amir Goldstein415543d2017-06-21 15:28:42 +03001227 inode_unlock(dir);
1228out:
1229 ovl_cache_free(&list);
1230 if (err)
lijiazi1bd0a3a2019-12-16 19:12:32 +08001231 pr_err("failed index dir cleanup (%i)\n", err);
Amir Goldstein415543d2017-06-21 15:28:42 +03001232 return err;
1233}