blob: ef1fe42ff7bb3a4e38e1d75714a1ef1bad4acf42 [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell 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/slab.h>
12#include <linux/namei.h>
13#include <linux/file.h>
14#include <linux/xattr.h>
15#include <linux/rbtree.h>
16#include <linux/security.h>
17#include <linux/cred.h>
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +030018#include <linux/ratelimit.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020019#include "overlayfs.h"
20
21struct ovl_cache_entry {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020022 unsigned int len;
23 unsigned int type;
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +030024 u64 real_ino;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020025 u64 ino;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020026 struct list_head l_node;
27 struct rb_node node;
Miklos Szeredicdb67272015-06-22 13:53:48 +020028 struct ovl_cache_entry *next_maybe_whiteout;
zhangyi (F)95e598e2017-10-31 22:57:00 +020029 bool is_upper;
Miklos Szeredic2096532014-10-27 13:48:48 +010030 bool is_whiteout;
Al Viro68bf8612014-10-23 22:58:56 -040031 char name[];
Miklos Szeredie9be9d52014-10-24 00:14:38 +020032};
33
34struct ovl_dir_cache {
35 long refcount;
36 u64 version;
37 struct list_head entries;
Miklos Szeredi4edb83b2017-07-27 21:54:06 +020038 struct rb_root root;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020039};
40
41struct ovl_readdir_data {
42 struct dir_context ctx;
Antonio Murdaca3fe6e522016-04-07 15:48:25 +020043 struct dentry *dentry;
Miklos Szeredi56656e92016-03-21 17:31:46 +010044 bool is_lowest;
Miklos Szeredi4edb83b2017-07-27 21:54:06 +020045 struct rb_root *root;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020046 struct list_head *list;
Al Virodb6ec212014-10-23 23:03:03 -040047 struct list_head middle;
Miklos Szeredicdb67272015-06-22 13:53:48 +020048 struct ovl_cache_entry *first_maybe_whiteout;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020049 int count;
50 int err;
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +030051 bool is_upper;
Vivek Goyal45aebea2016-02-22 09:28:34 -050052 bool d_type_supported;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020053};
54
55struct ovl_dir_file {
56 bool is_real;
57 bool is_upper;
58 struct ovl_dir_cache *cache;
hujianyang43303972014-12-11 10:30:18 +080059 struct list_head *cursor;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020060 struct file *realfile;
61 struct file *upperfile;
62};
63
64static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
65{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +020066 return rb_entry(n, struct ovl_cache_entry, node);
67}
68
69static bool ovl_cache_entry_find_link(const char *name, int len,
70 struct rb_node ***link,
71 struct rb_node **parent)
72{
73 bool found = false;
74 struct rb_node **newp = *link;
75
76 while (!found && *newp) {
77 int cmp;
78 struct ovl_cache_entry *tmp;
79
80 *parent = *newp;
81 tmp = ovl_cache_entry_from_node(*newp);
82 cmp = strncmp(name, tmp->name, len);
83 if (cmp > 0)
84 newp = &tmp->node.rb_right;
85 else if (cmp < 0 || len < tmp->len)
86 newp = &tmp->node.rb_left;
87 else
88 found = true;
89 }
90 *link = newp;
91
92 return found;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020093}
94
95static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
96 const char *name, int len)
97{
98 struct rb_node *node = root->rb_node;
99 int cmp;
100
101 while (node) {
102 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
103
104 cmp = strncmp(name, p->name, len);
105 if (cmp > 0)
106 node = p->node.rb_right;
107 else if (cmp < 0 || len < p->len)
108 node = p->node.rb_left;
109 else
110 return p;
111 }
112
113 return NULL;
114}
115
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300116static bool ovl_calc_d_ino(struct ovl_readdir_data *rdd,
117 struct ovl_cache_entry *p)
118{
119 /* Don't care if not doing ovl_iter() */
120 if (!rdd->dentry)
121 return false;
122
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200123 /* Always recalc d_ino when remapping lower inode numbers */
124 if (ovl_xino_bits(rdd->dentry->d_sb))
125 return true;
126
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300127 /* Always recalc d_ino for parent */
128 if (strcmp(p->name, "..") == 0)
129 return true;
130
131 /* If this is lower, then native d_ino will do */
132 if (!rdd->is_upper)
133 return false;
134
135 /*
136 * Recalc d_ino for '.' and for all entries if dir is impure (contains
137 * copied up entries)
138 */
139 if ((p->name[0] == '.' && p->len == 1) ||
140 ovl_test_flag(OVL_IMPURE, d_inode(rdd->dentry)))
141 return true;
142
143 return false;
144}
145
Miklos Szeredicdb67272015-06-22 13:53:48 +0200146static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100147 const char *name, int len,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200148 u64 ino, unsigned int d_type)
149{
150 struct ovl_cache_entry *p;
Al Viro68bf8612014-10-23 22:58:56 -0400151 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200152
Al Viro68bf8612014-10-23 22:58:56 -0400153 p = kmalloc(size, GFP_KERNEL);
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100154 if (!p)
155 return NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200156
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100157 memcpy(p->name, name, len);
158 p->name[len] = '\0';
159 p->len = len;
160 p->type = d_type;
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300161 p->real_ino = ino;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100162 p->ino = ino;
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300163 /* Defer setting d_ino for upper entry to ovl_iterate() */
164 if (ovl_calc_d_ino(rdd, p))
165 p->ino = 0;
zhangyi (F)95e598e2017-10-31 22:57:00 +0200166 p->is_upper = rdd->is_upper;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100167 p->is_whiteout = false;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100168
169 if (d_type == DT_CHR) {
Miklos Szeredicdb67272015-06-22 13:53:48 +0200170 p->next_maybe_whiteout = rdd->first_maybe_whiteout;
171 rdd->first_maybe_whiteout = p;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100172 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200173 return p;
174}
175
176static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
177 const char *name, int len, u64 ino,
178 unsigned int d_type)
179{
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200180 struct rb_node **newp = &rdd->root->rb_node;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200181 struct rb_node *parent = NULL;
182 struct ovl_cache_entry *p;
183
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200184 if (ovl_cache_entry_find_link(name, len, &newp, &parent))
185 return 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200186
Miklos Szeredicdb67272015-06-22 13:53:48 +0200187 p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
Miklos Szeredi31e8cce2017-07-27 21:54:06 +0200188 if (p == NULL) {
189 rdd->err = -ENOMEM;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200190 return -ENOMEM;
Miklos Szeredi31e8cce2017-07-27 21:54:06 +0200191 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200192
193 list_add_tail(&p->l_node, rdd->list);
194 rb_link_node(&p->node, parent, newp);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200195 rb_insert_color(&p->node, rdd->root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200196
197 return 0;
198}
199
Miklos Szeredi56656e92016-03-21 17:31:46 +0100200static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
201 const char *name, int namelen,
202 loff_t offset, u64 ino, unsigned int d_type)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200203{
204 struct ovl_cache_entry *p;
205
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200206 p = ovl_cache_entry_find(rdd->root, name, namelen);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200207 if (p) {
Al Virodb6ec212014-10-23 23:03:03 -0400208 list_move_tail(&p->l_node, &rdd->middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200209 } else {
Miklos Szeredicdb67272015-06-22 13:53:48 +0200210 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200211 if (p == NULL)
212 rdd->err = -ENOMEM;
213 else
Al Virodb6ec212014-10-23 23:03:03 -0400214 list_add_tail(&p->l_node, &rdd->middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200215 }
216
217 return rdd->err;
218}
219
220void ovl_cache_free(struct list_head *list)
221{
222 struct ovl_cache_entry *p;
223 struct ovl_cache_entry *n;
224
225 list_for_each_entry_safe(p, n, list, l_node)
226 kfree(p);
227
228 INIT_LIST_HEAD(list);
229}
230
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200231void ovl_dir_cache_free(struct inode *inode)
232{
233 struct ovl_dir_cache *cache = ovl_dir_cache(inode);
234
235 if (cache) {
236 ovl_cache_free(&cache->entries);
237 kfree(cache);
238 }
239}
240
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200241static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
242{
243 struct ovl_dir_cache *cache = od->cache;
244
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200245 WARN_ON(cache->refcount <= 0);
246 cache->refcount--;
247 if (!cache->refcount) {
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200248 if (ovl_dir_cache(d_inode(dentry)) == cache)
249 ovl_set_dir_cache(d_inode(dentry), NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200250
251 ovl_cache_free(&cache->entries);
252 kfree(cache);
253 }
254}
255
Miklos Szerediac7576f2014-10-30 17:37:34 +0100256static int ovl_fill_merge(struct dir_context *ctx, const char *name,
257 int namelen, loff_t offset, u64 ino,
258 unsigned int d_type)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200259{
Miklos Szerediac7576f2014-10-30 17:37:34 +0100260 struct ovl_readdir_data *rdd =
261 container_of(ctx, struct ovl_readdir_data, ctx);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200262
263 rdd->count++;
Miklos Szeredi56656e92016-03-21 17:31:46 +0100264 if (!rdd->is_lowest)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200265 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
266 else
Miklos Szeredi56656e92016-03-21 17:31:46 +0100267 return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200268}
269
Miklos Szeredicdb67272015-06-22 13:53:48 +0200270static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
271{
272 int err;
273 struct ovl_cache_entry *p;
274 struct dentry *dentry;
275 const struct cred *old_cred;
Miklos Szeredicdb67272015-06-22 13:53:48 +0200276
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200277 old_cred = ovl_override_creds(rdd->dentry->d_sb);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200278
Al Viro00235412016-05-26 00:05:12 -0400279 err = down_write_killable(&dir->d_inode->i_rwsem);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200280 if (!err) {
281 while (rdd->first_maybe_whiteout) {
282 p = rdd->first_maybe_whiteout;
283 rdd->first_maybe_whiteout = p->next_maybe_whiteout;
284 dentry = lookup_one_len(p->name, dir, p->len);
285 if (!IS_ERR(dentry)) {
286 p->is_whiteout = ovl_is_whiteout(dentry);
287 dput(dentry);
288 }
289 }
Al Viro59551022016-01-22 15:40:57 -0500290 inode_unlock(dir->d_inode);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200291 }
292 revert_creds(old_cred);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200293
294 return err;
295}
296
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200297static inline int ovl_dir_read(struct path *realpath,
298 struct ovl_readdir_data *rdd)
299{
300 struct file *realfile;
301 int err;
302
303 realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
304 if (IS_ERR(realfile))
305 return PTR_ERR(realfile);
306
Miklos Szeredicdb67272015-06-22 13:53:48 +0200307 rdd->first_maybe_whiteout = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200308 rdd->ctx.pos = 0;
309 do {
310 rdd->count = 0;
311 rdd->err = 0;
312 err = iterate_dir(realfile, &rdd->ctx);
313 if (err >= 0)
314 err = rdd->err;
315 } while (!err && rdd->count);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200316
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200317 if (!err && rdd->first_maybe_whiteout && rdd->dentry)
Miklos Szeredicdb67272015-06-22 13:53:48 +0200318 err = ovl_check_whiteouts(realpath->dentry, rdd);
319
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200320 fput(realfile);
321
322 return err;
323}
324
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300325/*
326 * Can we iterate real dir directly?
327 *
328 * Non-merge dir may contain whiteouts from a time it was a merge upper, before
329 * lower dir was removed under it and possibly before it was rotated from upper
330 * to lower layer.
331 */
332static bool ovl_dir_is_real(struct dentry *dir)
333{
334 return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir));
335}
336
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200337static void ovl_dir_reset(struct file *file)
338{
339 struct ovl_dir_file *od = file->private_data;
340 struct ovl_dir_cache *cache = od->cache;
341 struct dentry *dentry = file->f_path.dentry;
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300342 bool is_real;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200343
344 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
345 ovl_cache_put(od, dentry);
346 od->cache = NULL;
hujianyang43303972014-12-11 10:30:18 +0800347 od->cursor = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200348 }
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300349 is_real = ovl_dir_is_real(dentry);
350 if (od->is_real != is_real) {
351 /* is_real can only become false when dir is copied up */
352 if (WARN_ON(is_real))
353 return;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200354 od->is_real = false;
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300355 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200356}
357
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200358static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list,
359 struct rb_root *root)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200360{
361 int err;
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100362 struct path realpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200363 struct ovl_readdir_data rdd = {
364 .ctx.actor = ovl_fill_merge,
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200365 .dentry = dentry,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200366 .list = list,
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200367 .root = root,
Miklos Szeredi56656e92016-03-21 17:31:46 +0100368 .is_lowest = false,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200369 };
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100370 int idx, next;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200371
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100372 for (idx = 0; idx != -1; idx = next) {
373 next = ovl_path_next(idx, dentry, &realpath);
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300374 rdd.is_upper = ovl_dentry_upper(dentry) == realpath.dentry;
Miklos Szeredic9f00fd2014-11-20 16:40:01 +0100375
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100376 if (next != -1) {
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100377 err = ovl_dir_read(&realpath, &rdd);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200378 if (err)
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100379 break;
380 } else {
381 /*
382 * Insert lowest layer entries before upper ones, this
383 * allows offsets to be reasonably constant
384 */
385 list_add(&rdd.middle, rdd.list);
Miklos Szeredi56656e92016-03-21 17:31:46 +0100386 rdd.is_lowest = true;
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100387 err = ovl_dir_read(&realpath, &rdd);
388 list_del(&rdd.middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200389 }
390 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200391 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200392}
393
394static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
395{
hujianyang43303972014-12-11 10:30:18 +0800396 struct list_head *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200397 loff_t off = 0;
398
hujianyang43303972014-12-11 10:30:18 +0800399 list_for_each(p, &od->cache->entries) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200400 if (off >= pos)
401 break;
402 off++;
403 }
hujianyang43303972014-12-11 10:30:18 +0800404 /* Cursor is safe since the cache is stable */
405 od->cursor = p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200406}
407
408static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
409{
410 int res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200411 struct ovl_dir_cache *cache;
412
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200413 cache = ovl_dir_cache(d_inode(dentry));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200414 if (cache && ovl_dentry_version_get(dentry) == cache->version) {
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200415 WARN_ON(!cache->refcount);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200416 cache->refcount++;
417 return cache;
418 }
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200419 ovl_set_dir_cache(d_inode(dentry), NULL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200420
421 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
422 if (!cache)
423 return ERR_PTR(-ENOMEM);
424
425 cache->refcount = 1;
426 INIT_LIST_HEAD(&cache->entries);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200427 cache->root = RB_ROOT;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200428
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200429 res = ovl_dir_read_merged(dentry, &cache->entries, &cache->root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200430 if (res) {
431 ovl_cache_free(&cache->entries);
432 kfree(cache);
433 return ERR_PTR(res);
434 }
435
436 cache->version = ovl_dentry_version_get(dentry);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200437 ovl_set_dir_cache(d_inode(dentry), cache);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200438
439 return cache;
440}
441
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200442/* Map inode number to lower fs unique range */
443static u64 ovl_remap_lower_ino(u64 ino, int xinobits, int fsid,
444 const char *name, int namelen)
445{
446 if (ino >> (64 - xinobits)) {
447 pr_warn_ratelimited("overlayfs: d_ino too big (%.*s, ino=%llu, xinobits=%d)\n",
448 namelen, name, ino, xinobits);
449 return ino;
450 }
451
452 return ino | ((u64)fsid) << (64 - xinobits);
453}
454
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300455/*
456 * Set d_ino for upper entries. Non-upper entries should always report
457 * the uppermost real inode ino and should not call this function.
458 *
459 * When not all layer are on same fs, report real ino also for upper.
460 *
461 * When all layers are on the same fs, and upper has a reference to
462 * copy up origin, call vfs_getattr() on the overlay entry to make
463 * sure that d_ino will be consistent with st_ino from stat(2).
464 */
465static int ovl_cache_update_ino(struct path *path, struct ovl_cache_entry *p)
466
467{
468 struct dentry *dir = path->dentry;
469 struct dentry *this = NULL;
470 enum ovl_path_type type;
471 u64 ino = p->real_ino;
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200472 int xinobits = ovl_xino_bits(dir->d_sb);
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300473 int err = 0;
474
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200475 if (!ovl_same_sb(dir->d_sb) && !xinobits)
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300476 goto out;
477
478 if (p->name[0] == '.') {
479 if (p->len == 1) {
480 this = dget(dir);
481 goto get;
482 }
483 if (p->len == 2 && p->name[1] == '.') {
484 /* we shall not be moved */
485 this = dget(dir->d_parent);
486 goto get;
487 }
488 }
489 this = lookup_one_len(p->name, dir, p->len);
490 if (IS_ERR_OR_NULL(this) || !this->d_inode) {
491 if (IS_ERR(this)) {
492 err = PTR_ERR(this);
493 this = NULL;
494 goto fail;
495 }
496 goto out;
497 }
498
499get:
500 type = ovl_path_type(this);
501 if (OVL_TYPE_ORIGIN(type)) {
502 struct kstat stat;
503 struct path statpath = *path;
504
505 statpath.dentry = this;
506 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
507 if (err)
508 goto fail;
509
510 WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
511 ino = stat.ino;
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200512 } else if (xinobits && !OVL_TYPE_UPPER(type)) {
513 ino = ovl_remap_lower_ino(ino, xinobits,
514 ovl_layer_lower(this)->fsid,
515 p->name, p->len);
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300516 }
517
518out:
519 p->ino = ino;
520 dput(this);
521 return err;
522
523fail:
Amir Goldsteinda2e6b72017-11-22 20:27:34 +0200524 pr_warn_ratelimited("overlayfs: failed to look up (%s) for ino (%i)\n",
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300525 p->name, err);
526 goto out;
527}
528
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200529static int ovl_fill_plain(struct dir_context *ctx, const char *name,
530 int namelen, loff_t offset, u64 ino,
531 unsigned int d_type)
532{
533 struct ovl_cache_entry *p;
534 struct ovl_readdir_data *rdd =
535 container_of(ctx, struct ovl_readdir_data, ctx);
536
537 rdd->count++;
538 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
539 if (p == NULL) {
540 rdd->err = -ENOMEM;
541 return -ENOMEM;
542 }
543 list_add_tail(&p->l_node, rdd->list);
544
545 return 0;
546}
547
548static int ovl_dir_read_impure(struct path *path, struct list_head *list,
549 struct rb_root *root)
550{
551 int err;
552 struct path realpath;
553 struct ovl_cache_entry *p, *n;
554 struct ovl_readdir_data rdd = {
555 .ctx.actor = ovl_fill_plain,
556 .list = list,
557 .root = root,
558 };
559
560 INIT_LIST_HEAD(list);
561 *root = RB_ROOT;
562 ovl_path_upper(path->dentry, &realpath);
563
564 err = ovl_dir_read(&realpath, &rdd);
565 if (err)
566 return err;
567
568 list_for_each_entry_safe(p, n, list, l_node) {
569 if (strcmp(p->name, ".") != 0 &&
570 strcmp(p->name, "..") != 0) {
571 err = ovl_cache_update_ino(path, p);
572 if (err)
573 return err;
574 }
575 if (p->ino == p->real_ino) {
576 list_del(&p->l_node);
577 kfree(p);
578 } else {
579 struct rb_node **newp = &root->rb_node;
580 struct rb_node *parent = NULL;
581
582 if (WARN_ON(ovl_cache_entry_find_link(p->name, p->len,
583 &newp, &parent)))
584 return -EIO;
585
586 rb_link_node(&p->node, parent, newp);
587 rb_insert_color(&p->node, root);
588 }
589 }
590 return 0;
591}
592
593static struct ovl_dir_cache *ovl_cache_get_impure(struct path *path)
594{
595 int res;
596 struct dentry *dentry = path->dentry;
597 struct ovl_dir_cache *cache;
598
599 cache = ovl_dir_cache(d_inode(dentry));
600 if (cache && ovl_dentry_version_get(dentry) == cache->version)
601 return cache;
602
603 /* Impure cache is not refcounted, free it here */
604 ovl_dir_cache_free(d_inode(dentry));
605 ovl_set_dir_cache(d_inode(dentry), NULL);
606
607 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
608 if (!cache)
609 return ERR_PTR(-ENOMEM);
610
611 res = ovl_dir_read_impure(path, &cache->entries, &cache->root);
612 if (res) {
613 ovl_cache_free(&cache->entries);
614 kfree(cache);
615 return ERR_PTR(res);
616 }
617 if (list_empty(&cache->entries)) {
Amir Goldsteina5a927a2018-01-03 18:54:42 +0200618 /*
619 * A good opportunity to get rid of an unneeded "impure" flag.
620 * Removing the "impure" xattr is best effort.
621 */
622 if (!ovl_want_write(dentry)) {
623 ovl_do_removexattr(ovl_dentry_upper(dentry),
624 OVL_XATTR_IMPURE);
625 ovl_drop_write(dentry);
626 }
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200627 ovl_clear_flag(OVL_IMPURE, d_inode(dentry));
628 kfree(cache);
629 return NULL;
630 }
631
632 cache->version = ovl_dentry_version_get(dentry);
633 ovl_set_dir_cache(d_inode(dentry), cache);
634
635 return cache;
636}
637
638struct ovl_readdir_translate {
639 struct dir_context *orig_ctx;
640 struct ovl_dir_cache *cache;
641 struct dir_context ctx;
642 u64 parent_ino;
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200643 int fsid;
644 int xinobits;
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200645};
646
647static int ovl_fill_real(struct dir_context *ctx, const char *name,
648 int namelen, loff_t offset, u64 ino,
649 unsigned int d_type)
650{
651 struct ovl_readdir_translate *rdt =
652 container_of(ctx, struct ovl_readdir_translate, ctx);
653 struct dir_context *orig_ctx = rdt->orig_ctx;
654
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200655 if (rdt->parent_ino && strcmp(name, "..") == 0) {
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200656 ino = rdt->parent_ino;
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200657 } else if (rdt->cache) {
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200658 struct ovl_cache_entry *p;
659
660 p = ovl_cache_entry_find(&rdt->cache->root, name, namelen);
661 if (p)
662 ino = p->ino;
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200663 } else if (rdt->xinobits) {
664 ino = ovl_remap_lower_ino(ino, rdt->xinobits, rdt->fsid,
665 name, namelen);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200666 }
667
668 return orig_ctx->actor(orig_ctx, name, namelen, offset, ino, d_type);
669}
670
671static int ovl_iterate_real(struct file *file, struct dir_context *ctx)
672{
673 int err;
674 struct ovl_dir_file *od = file->private_data;
675 struct dentry *dir = file->f_path.dentry;
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200676 struct ovl_layer *lower_layer = ovl_layer_lower(dir);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200677 struct ovl_readdir_translate rdt = {
678 .ctx.actor = ovl_fill_real,
679 .orig_ctx = ctx,
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200680 .xinobits = ovl_xino_bits(dir->d_sb),
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200681 };
682
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200683 if (rdt.xinobits && lower_layer)
684 rdt.fsid = lower_layer->fsid;
685
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200686 if (OVL_TYPE_MERGE(ovl_path_type(dir->d_parent))) {
687 struct kstat stat;
688 struct path statpath = file->f_path;
689
690 statpath.dentry = dir->d_parent;
691 err = vfs_getattr(&statpath, &stat, STATX_INO, 0);
692 if (err)
693 return err;
694
695 WARN_ON_ONCE(dir->d_sb->s_dev != stat.dev);
696 rdt.parent_ino = stat.ino;
697 }
698
699 if (ovl_test_flag(OVL_IMPURE, d_inode(dir))) {
700 rdt.cache = ovl_cache_get_impure(&file->f_path);
701 if (IS_ERR(rdt.cache))
702 return PTR_ERR(rdt.cache);
703 }
704
Amir Goldsteinb02a16e2017-11-29 07:35:21 +0200705 err = iterate_dir(od->realfile, &rdt.ctx);
706 ctx->pos = rdt.ctx.pos;
707
708 return err;
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200709}
710
711
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200712static int ovl_iterate(struct file *file, struct dir_context *ctx)
713{
714 struct ovl_dir_file *od = file->private_data;
715 struct dentry *dentry = file->f_path.dentry;
hujianyang43303972014-12-11 10:30:18 +0800716 struct ovl_cache_entry *p;
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300717 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200718
719 if (!ctx->pos)
720 ovl_dir_reset(file);
721
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200722 if (od->is_real) {
723 /*
724 * If parent is merge, then need to adjust d_ino for '..', if
725 * dir is impure then need to adjust d_ino for copied up
726 * entries.
727 */
Amir Goldsteinadbf4f72017-11-06 16:48:02 +0200728 if (ovl_xino_bits(dentry->d_sb) ||
729 (ovl_same_sb(dentry->d_sb) &&
730 (ovl_test_flag(OVL_IMPURE, d_inode(dentry)) ||
731 OVL_TYPE_MERGE(ovl_path_type(dentry->d_parent))))) {
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200732 return ovl_iterate_real(file, ctx);
733 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200734 return iterate_dir(od->realfile, ctx);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200735 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200736
737 if (!od->cache) {
738 struct ovl_dir_cache *cache;
739
740 cache = ovl_cache_get(dentry);
741 if (IS_ERR(cache))
742 return PTR_ERR(cache);
743
744 od->cache = cache;
745 ovl_seek_cursor(od, ctx->pos);
746 }
747
hujianyang43303972014-12-11 10:30:18 +0800748 while (od->cursor != &od->cache->entries) {
749 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300750 if (!p->is_whiteout) {
751 if (!p->ino) {
752 err = ovl_cache_update_ino(&file->f_path, p);
753 if (err)
754 return err;
755 }
hujianyang43303972014-12-11 10:30:18 +0800756 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
757 break;
Amir Goldsteinb5efccbe2017-05-11 16:42:27 +0300758 }
hujianyang43303972014-12-11 10:30:18 +0800759 od->cursor = p->l_node.next;
760 ctx->pos++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200761 }
762 return 0;
763}
764
765static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
766{
767 loff_t res;
768 struct ovl_dir_file *od = file->private_data;
769
Al Viro59551022016-01-22 15:40:57 -0500770 inode_lock(file_inode(file));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200771 if (!file->f_pos)
772 ovl_dir_reset(file);
773
774 if (od->is_real) {
775 res = vfs_llseek(od->realfile, offset, origin);
776 file->f_pos = od->realfile->f_pos;
777 } else {
778 res = -EINVAL;
779
780 switch (origin) {
781 case SEEK_CUR:
782 offset += file->f_pos;
783 break;
784 case SEEK_SET:
785 break;
786 default:
787 goto out_unlock;
788 }
789 if (offset < 0)
790 goto out_unlock;
791
792 if (offset != file->f_pos) {
793 file->f_pos = offset;
794 if (od->cache)
795 ovl_seek_cursor(od, offset);
796 }
797 res = offset;
798 }
799out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500800 inode_unlock(file_inode(file));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200801
802 return res;
803}
804
805static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
806 int datasync)
807{
808 struct ovl_dir_file *od = file->private_data;
809 struct dentry *dentry = file->f_path.dentry;
810 struct file *realfile = od->realfile;
811
Amir Goldsteind796e772017-11-08 09:39:46 +0200812 /* Nothing to sync for lower */
813 if (!OVL_TYPE_UPPER(ovl_path_type(dentry)))
814 return 0;
815
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200816 /*
817 * Need to check if we started out being a lower dir, but got copied up
818 */
Amir Goldsteind796e772017-11-08 09:39:46 +0200819 if (!od->is_upper) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200820 struct inode *inode = file_inode(file);
821
Will Deacon506458e2017-10-24 11:22:48 +0100822 realfile = READ_ONCE(od->upperfile);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200823 if (!realfile) {
824 struct path upperpath;
825
826 ovl_path_upper(dentry, &upperpath);
827 realfile = ovl_path_open(&upperpath, O_RDONLY);
Peter Zijlstraff7a5fb2017-06-07 17:43:46 +0200828
Al Viro59551022016-01-22 15:40:57 -0500829 inode_lock(inode);
Al Viro3d268c92014-10-23 22:56:05 -0400830 if (!od->upperfile) {
831 if (IS_ERR(realfile)) {
Al Viro59551022016-01-22 15:40:57 -0500832 inode_unlock(inode);
Al Viro3d268c92014-10-23 22:56:05 -0400833 return PTR_ERR(realfile);
834 }
Peter Zijlstraff7a5fb2017-06-07 17:43:46 +0200835 smp_store_release(&od->upperfile, realfile);
Al Viro3d268c92014-10-23 22:56:05 -0400836 } else {
837 /* somebody has beaten us to it */
838 if (!IS_ERR(realfile))
839 fput(realfile);
840 realfile = od->upperfile;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200841 }
Al Viro59551022016-01-22 15:40:57 -0500842 inode_unlock(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200843 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200844 }
845
846 return vfs_fsync_range(realfile, start, end, datasync);
847}
848
849static int ovl_dir_release(struct inode *inode, struct file *file)
850{
851 struct ovl_dir_file *od = file->private_data;
852
853 if (od->cache) {
Al Viro59551022016-01-22 15:40:57 -0500854 inode_lock(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200855 ovl_cache_put(od, file->f_path.dentry);
Al Viro59551022016-01-22 15:40:57 -0500856 inode_unlock(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200857 }
858 fput(od->realfile);
859 if (od->upperfile)
860 fput(od->upperfile);
861 kfree(od);
862
863 return 0;
864}
865
866static int ovl_dir_open(struct inode *inode, struct file *file)
867{
868 struct path realpath;
869 struct file *realfile;
870 struct ovl_dir_file *od;
871 enum ovl_path_type type;
872
873 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
874 if (!od)
875 return -ENOMEM;
876
877 type = ovl_path_real(file->f_path.dentry, &realpath);
878 realfile = ovl_path_open(&realpath, file->f_flags);
879 if (IS_ERR(realfile)) {
880 kfree(od);
881 return PTR_ERR(realfile);
882 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200883 od->realfile = realfile;
Amir Goldsteinb79e05a2017-06-25 16:37:17 +0300884 od->is_real = ovl_dir_is_real(file->f_path.dentry);
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100885 od->is_upper = OVL_TYPE_UPPER(type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200886 file->private_data = od;
887
888 return 0;
889}
890
891const struct file_operations ovl_dir_operations = {
892 .read = generic_read_dir,
893 .open = ovl_dir_open,
894 .iterate = ovl_iterate,
895 .llseek = ovl_dir_llseek,
896 .fsync = ovl_dir_fsync,
897 .release = ovl_dir_release,
898};
899
900int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
901{
902 int err;
zhangyi (F)95e598e2017-10-31 22:57:00 +0200903 struct ovl_cache_entry *p, *n;
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200904 struct rb_root root = RB_ROOT;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200905 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200906
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200907 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200908 err = ovl_dir_read_merged(dentry, list, &root);
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200909 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200910 if (err)
911 return err;
912
913 err = 0;
914
zhangyi (F)95e598e2017-10-31 22:57:00 +0200915 list_for_each_entry_safe(p, n, list, l_node) {
916 /*
917 * Select whiteouts in upperdir, they should
918 * be cleared when deleting this directory.
919 */
920 if (p->is_whiteout) {
921 if (p->is_upper)
922 continue;
923 goto del_entry;
924 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200925
926 if (p->name[0] == '.') {
927 if (p->len == 1)
zhangyi (F)95e598e2017-10-31 22:57:00 +0200928 goto del_entry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200929 if (p->len == 2 && p->name[1] == '.')
zhangyi (F)95e598e2017-10-31 22:57:00 +0200930 goto del_entry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200931 }
932 err = -ENOTEMPTY;
933 break;
zhangyi (F)95e598e2017-10-31 22:57:00 +0200934
935del_entry:
936 list_del(&p->l_node);
937 kfree(p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200938 }
939
940 return err;
941}
942
943void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
944{
945 struct ovl_cache_entry *p;
946
Al Viro59551022016-01-22 15:40:57 -0500947 inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200948 list_for_each_entry(p, list, l_node) {
949 struct dentry *dentry;
950
zhangyi (F)95e598e2017-10-31 22:57:00 +0200951 if (WARN_ON(!p->is_whiteout || !p->is_upper))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200952 continue;
953
954 dentry = lookup_one_len(p->name, upper, p->len);
955 if (IS_ERR(dentry)) {
956 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
957 upper->d_name.name, p->len, p->name,
958 (int) PTR_ERR(dentry));
959 continue;
960 }
Konstantin Khlebnikov84889d42015-11-16 18:44:11 +0300961 if (dentry->d_inode)
962 ovl_cleanup(upper->d_inode, dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200963 dput(dentry);
964 }
Al Viro59551022016-01-22 15:40:57 -0500965 inode_unlock(upper->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200966}
Vivek Goyal45aebea2016-02-22 09:28:34 -0500967
968static int ovl_check_d_type(struct dir_context *ctx, const char *name,
969 int namelen, loff_t offset, u64 ino,
970 unsigned int d_type)
971{
972 struct ovl_readdir_data *rdd =
973 container_of(ctx, struct ovl_readdir_data, ctx);
974
975 /* Even if d_type is not supported, DT_DIR is returned for . and .. */
976 if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
977 return 0;
978
979 if (d_type != DT_UNKNOWN)
980 rdd->d_type_supported = true;
981
982 return 0;
983}
984
985/*
986 * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
987 * if error is encountered.
988 */
989int ovl_check_d_type_supported(struct path *realpath)
990{
991 int err;
992 struct ovl_readdir_data rdd = {
993 .ctx.actor = ovl_check_d_type,
994 .d_type_supported = false,
995 };
996
997 err = ovl_dir_read(realpath, &rdd);
998 if (err)
999 return err;
1000
1001 return rdd.d_type_supported;
1002}
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001003
1004static void ovl_workdir_cleanup_recurse(struct path *path, int level)
1005{
1006 int err;
1007 struct inode *dir = path->dentry->d_inode;
1008 LIST_HEAD(list);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +02001009 struct rb_root root = RB_ROOT;
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001010 struct ovl_cache_entry *p;
1011 struct ovl_readdir_data rdd = {
1012 .ctx.actor = ovl_fill_merge,
1013 .dentry = NULL,
1014 .list = &list,
Miklos Szeredi4edb83b2017-07-27 21:54:06 +02001015 .root = &root,
Miklos Szeredieea2fb42016-09-01 11:11:59 +02001016 .is_lowest = false,
1017 };
1018
1019 err = ovl_dir_read(path, &rdd);
1020 if (err)
1021 goto out;
1022
1023 inode_lock_nested(dir, I_MUTEX_PARENT);
1024 list_for_each_entry(p, &list, l_node) {
1025 struct dentry *dentry;
1026
1027 if (p->name[0] == '.') {
1028 if (p->len == 1)
1029 continue;
1030 if (p->len == 2 && p->name[1] == '.')
1031 continue;
1032 }
1033 dentry = lookup_one_len(p->name, path->dentry, p->len);
1034 if (IS_ERR(dentry))
1035 continue;
1036 if (dentry->d_inode)
1037 ovl_workdir_cleanup(dir, path->mnt, dentry, level);
1038 dput(dentry);
1039 }
1040 inode_unlock(dir);
1041out:
1042 ovl_cache_free(&list);
1043}
1044
1045void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
1046 struct dentry *dentry, int level)
1047{
1048 int err;
1049
1050 if (!d_is_dir(dentry) || level > 1) {
1051 ovl_cleanup(dir, dentry);
1052 return;
1053 }
1054
1055 err = ovl_do_rmdir(dir, dentry);
1056 if (err) {
1057 struct path path = { .mnt = mnt, .dentry = dentry };
1058
1059 inode_unlock(dir);
1060 ovl_workdir_cleanup_recurse(&path, level + 1);
1061 inode_lock_nested(dir, I_MUTEX_PARENT);
1062 ovl_cleanup(dir, dentry);
1063 }
1064}
Amir Goldstein415543d2017-06-21 15:28:42 +03001065
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001066int ovl_indexdir_cleanup(struct ovl_fs *ofs)
Amir Goldstein415543d2017-06-21 15:28:42 +03001067{
1068 int err;
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001069 struct dentry *indexdir = ofs->indexdir;
Amir Goldsteindc7ab672017-09-24 22:19:10 +03001070 struct dentry *index = NULL;
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001071 struct inode *dir = indexdir->d_inode;
1072 struct path path = { .mnt = ofs->upper_mnt, .dentry = indexdir };
Amir Goldstein415543d2017-06-21 15:28:42 +03001073 LIST_HEAD(list);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +02001074 struct rb_root root = RB_ROOT;
Amir Goldstein415543d2017-06-21 15:28:42 +03001075 struct ovl_cache_entry *p;
1076 struct ovl_readdir_data rdd = {
1077 .ctx.actor = ovl_fill_merge,
1078 .dentry = NULL,
1079 .list = &list,
Miklos Szeredi4edb83b2017-07-27 21:54:06 +02001080 .root = &root,
Amir Goldstein415543d2017-06-21 15:28:42 +03001081 .is_lowest = false,
1082 };
1083
1084 err = ovl_dir_read(&path, &rdd);
1085 if (err)
1086 goto out;
1087
1088 inode_lock_nested(dir, I_MUTEX_PARENT);
1089 list_for_each_entry(p, &list, l_node) {
Amir Goldstein415543d2017-06-21 15:28:42 +03001090 if (p->name[0] == '.') {
1091 if (p->len == 1)
1092 continue;
1093 if (p->len == 2 && p->name[1] == '.')
1094 continue;
1095 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001096 index = lookup_one_len(p->name, indexdir, p->len);
Amir Goldstein415543d2017-06-21 15:28:42 +03001097 if (IS_ERR(index)) {
1098 err = PTR_ERR(index);
Amir Goldsteindc7ab672017-09-24 22:19:10 +03001099 index = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +03001100 break;
1101 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +02001102 err = ovl_verify_index(ofs, index);
Amir Goldstein24f0b172018-01-11 15:33:51 +02001103 if (!err) {
1104 goto next;
1105 } else if (err == -ESTALE) {
1106 /* Cleanup stale index entries */
Amir Goldstein415543d2017-06-21 15:28:42 +03001107 err = ovl_cleanup(dir, index);
Amir Goldstein24f0b172018-01-11 15:33:51 +02001108 } else if (err != -ENOENT) {
1109 /*
1110 * Abort mount to avoid corrupting the index if
1111 * an incompatible index entry was found or on out
1112 * of memory.
1113 */
1114 break;
1115 } else if (ofs->config.nfs_export) {
1116 /*
1117 * Whiteout orphan index to block future open by
1118 * handle after overlay nlink dropped to zero.
1119 */
1120 err = ovl_cleanup_and_whiteout(indexdir, dir, index);
1121 } else {
1122 /* Cleanup orphan index entries */
1123 err = ovl_cleanup(dir, index);
1124 }
1125
Amir Goldsteinfa0096e2017-10-24 12:24:11 +03001126 if (err)
1127 break;
1128
Amir Goldstein24f0b172018-01-11 15:33:51 +02001129next:
Amir Goldstein415543d2017-06-21 15:28:42 +03001130 dput(index);
Amir Goldsteindc7ab672017-09-24 22:19:10 +03001131 index = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +03001132 }
Amir Goldsteindc7ab672017-09-24 22:19:10 +03001133 dput(index);
Amir Goldstein415543d2017-06-21 15:28:42 +03001134 inode_unlock(dir);
1135out:
1136 ovl_cache_free(&list);
1137 if (err)
1138 pr_err("overlayfs: failed index dir cleanup (%i)\n", err);
1139 return err;
1140}