blob: f241b4ee3d8a50b1c03ae4b1e1848b772fd26af1 [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>
18#include "overlayfs.h"
19
20struct ovl_cache_entry {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020021 unsigned int len;
22 unsigned int type;
23 u64 ino;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020024 struct list_head l_node;
25 struct rb_node node;
Miklos Szeredicdb67272015-06-22 13:53:48 +020026 struct ovl_cache_entry *next_maybe_whiteout;
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;
35};
36
37struct ovl_readdir_data {
38 struct dir_context ctx;
Antonio Murdaca3fe6e522016-04-07 15:48:25 +020039 struct dentry *dentry;
Miklos Szeredi56656e92016-03-21 17:31:46 +010040 bool is_lowest;
Al Viro49be4fb2014-10-23 23:00:53 -040041 struct rb_root root;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020042 struct list_head *list;
Al Virodb6ec212014-10-23 23:03:03 -040043 struct list_head middle;
Miklos Szeredicdb67272015-06-22 13:53:48 +020044 struct ovl_cache_entry *first_maybe_whiteout;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020045 int count;
46 int err;
Vivek Goyal45aebea2016-02-22 09:28:34 -050047 bool d_type_supported;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020048};
49
50struct ovl_dir_file {
51 bool is_real;
52 bool is_upper;
53 struct ovl_dir_cache *cache;
hujianyang43303972014-12-11 10:30:18 +080054 struct list_head *cursor;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020055 struct file *realfile;
56 struct file *upperfile;
57};
58
59static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
60{
61 return container_of(n, struct ovl_cache_entry, node);
62}
63
64static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
65 const char *name, int len)
66{
67 struct rb_node *node = root->rb_node;
68 int cmp;
69
70 while (node) {
71 struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
72
73 cmp = strncmp(name, p->name, len);
74 if (cmp > 0)
75 node = p->node.rb_right;
76 else if (cmp < 0 || len < p->len)
77 node = p->node.rb_left;
78 else
79 return p;
80 }
81
82 return NULL;
83}
84
Miklos Szeredicdb67272015-06-22 13:53:48 +020085static struct ovl_cache_entry *ovl_cache_entry_new(struct ovl_readdir_data *rdd,
Miklos Szeredi3e01cee2014-12-13 00:59:45 +010086 const char *name, int len,
Miklos Szeredie9be9d52014-10-24 00:14:38 +020087 u64 ino, unsigned int d_type)
88{
89 struct ovl_cache_entry *p;
Al Viro68bf8612014-10-23 22:58:56 -040090 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020091
Al Viro68bf8612014-10-23 22:58:56 -040092 p = kmalloc(size, GFP_KERNEL);
Miklos Szeredi3e01cee2014-12-13 00:59:45 +010093 if (!p)
94 return NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020095
Miklos Szeredi3e01cee2014-12-13 00:59:45 +010096 memcpy(p->name, name, len);
97 p->name[len] = '\0';
98 p->len = len;
99 p->type = d_type;
100 p->ino = ino;
101 p->is_whiteout = false;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100102
103 if (d_type == DT_CHR) {
Miklos Szeredicdb67272015-06-22 13:53:48 +0200104 p->next_maybe_whiteout = rdd->first_maybe_whiteout;
105 rdd->first_maybe_whiteout = p;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100106 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200107 return p;
108}
109
110static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
111 const char *name, int len, u64 ino,
112 unsigned int d_type)
113{
Al Viro49be4fb2014-10-23 23:00:53 -0400114 struct rb_node **newp = &rdd->root.rb_node;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200115 struct rb_node *parent = NULL;
116 struct ovl_cache_entry *p;
117
118 while (*newp) {
119 int cmp;
120 struct ovl_cache_entry *tmp;
121
122 parent = *newp;
123 tmp = ovl_cache_entry_from_node(*newp);
124 cmp = strncmp(name, tmp->name, len);
125 if (cmp > 0)
126 newp = &tmp->node.rb_right;
127 else if (cmp < 0 || len < tmp->len)
128 newp = &tmp->node.rb_left;
129 else
130 return 0;
131 }
132
Miklos Szeredicdb67272015-06-22 13:53:48 +0200133 p = ovl_cache_entry_new(rdd, name, len, ino, d_type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200134 if (p == NULL)
135 return -ENOMEM;
136
137 list_add_tail(&p->l_node, rdd->list);
138 rb_link_node(&p->node, parent, newp);
Al Viro49be4fb2014-10-23 23:00:53 -0400139 rb_insert_color(&p->node, &rdd->root);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200140
141 return 0;
142}
143
Miklos Szeredi56656e92016-03-21 17:31:46 +0100144static int ovl_fill_lowest(struct ovl_readdir_data *rdd,
145 const char *name, int namelen,
146 loff_t offset, u64 ino, unsigned int d_type)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200147{
148 struct ovl_cache_entry *p;
149
Al Viro49be4fb2014-10-23 23:00:53 -0400150 p = ovl_cache_entry_find(&rdd->root, name, namelen);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200151 if (p) {
Al Virodb6ec212014-10-23 23:03:03 -0400152 list_move_tail(&p->l_node, &rdd->middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200153 } else {
Miklos Szeredicdb67272015-06-22 13:53:48 +0200154 p = ovl_cache_entry_new(rdd, name, namelen, ino, d_type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200155 if (p == NULL)
156 rdd->err = -ENOMEM;
157 else
Al Virodb6ec212014-10-23 23:03:03 -0400158 list_add_tail(&p->l_node, &rdd->middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200159 }
160
161 return rdd->err;
162}
163
164void ovl_cache_free(struct list_head *list)
165{
166 struct ovl_cache_entry *p;
167 struct ovl_cache_entry *n;
168
169 list_for_each_entry_safe(p, n, list, l_node)
170 kfree(p);
171
172 INIT_LIST_HEAD(list);
173}
174
175static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
176{
177 struct ovl_dir_cache *cache = od->cache;
178
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200179 WARN_ON(cache->refcount <= 0);
180 cache->refcount--;
181 if (!cache->refcount) {
182 if (ovl_dir_cache(dentry) == cache)
183 ovl_set_dir_cache(dentry, NULL);
184
185 ovl_cache_free(&cache->entries);
186 kfree(cache);
187 }
188}
189
Miklos Szerediac7576f2014-10-30 17:37:34 +0100190static int ovl_fill_merge(struct dir_context *ctx, const char *name,
191 int namelen, loff_t offset, u64 ino,
192 unsigned int d_type)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200193{
Miklos Szerediac7576f2014-10-30 17:37:34 +0100194 struct ovl_readdir_data *rdd =
195 container_of(ctx, struct ovl_readdir_data, ctx);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200196
197 rdd->count++;
Miklos Szeredi56656e92016-03-21 17:31:46 +0100198 if (!rdd->is_lowest)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200199 return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
200 else
Miklos Szeredi56656e92016-03-21 17:31:46 +0100201 return ovl_fill_lowest(rdd, name, namelen, offset, ino, d_type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200202}
203
Miklos Szeredicdb67272015-06-22 13:53:48 +0200204static int ovl_check_whiteouts(struct dentry *dir, struct ovl_readdir_data *rdd)
205{
206 int err;
207 struct ovl_cache_entry *p;
208 struct dentry *dentry;
209 const struct cred *old_cred;
Miklos Szeredicdb67272015-06-22 13:53:48 +0200210
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200211 old_cred = ovl_override_creds(rdd->dentry->d_sb);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200212
Al Viro00235412016-05-26 00:05:12 -0400213 err = down_write_killable(&dir->d_inode->i_rwsem);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200214 if (!err) {
215 while (rdd->first_maybe_whiteout) {
216 p = rdd->first_maybe_whiteout;
217 rdd->first_maybe_whiteout = p->next_maybe_whiteout;
218 dentry = lookup_one_len(p->name, dir, p->len);
219 if (!IS_ERR(dentry)) {
220 p->is_whiteout = ovl_is_whiteout(dentry);
221 dput(dentry);
222 }
223 }
Al Viro59551022016-01-22 15:40:57 -0500224 inode_unlock(dir->d_inode);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200225 }
226 revert_creds(old_cred);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200227
228 return err;
229}
230
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200231static inline int ovl_dir_read(struct path *realpath,
232 struct ovl_readdir_data *rdd)
233{
234 struct file *realfile;
235 int err;
236
237 realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
238 if (IS_ERR(realfile))
239 return PTR_ERR(realfile);
240
Miklos Szeredicdb67272015-06-22 13:53:48 +0200241 rdd->first_maybe_whiteout = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200242 rdd->ctx.pos = 0;
243 do {
244 rdd->count = 0;
245 rdd->err = 0;
246 err = iterate_dir(realfile, &rdd->ctx);
247 if (err >= 0)
248 err = rdd->err;
249 } while (!err && rdd->count);
Miklos Szeredicdb67272015-06-22 13:53:48 +0200250
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200251 if (!err && rdd->first_maybe_whiteout && rdd->dentry)
Miklos Szeredicdb67272015-06-22 13:53:48 +0200252 err = ovl_check_whiteouts(realpath->dentry, rdd);
253
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200254 fput(realfile);
255
256 return err;
257}
258
259static void ovl_dir_reset(struct file *file)
260{
261 struct ovl_dir_file *od = file->private_data;
262 struct ovl_dir_cache *cache = od->cache;
263 struct dentry *dentry = file->f_path.dentry;
264 enum ovl_path_type type = ovl_path_type(dentry);
265
266 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
267 ovl_cache_put(od, dentry);
268 od->cache = NULL;
hujianyang43303972014-12-11 10:30:18 +0800269 od->cursor = NULL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200270 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100271 WARN_ON(!od->is_real && !OVL_TYPE_MERGE(type));
272 if (od->is_real && OVL_TYPE_MERGE(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200273 od->is_real = false;
274}
275
Miklos Szeredic9f00fd2014-11-20 16:40:01 +0100276static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200277{
278 int err;
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100279 struct path realpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200280 struct ovl_readdir_data rdd = {
281 .ctx.actor = ovl_fill_merge,
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200282 .dentry = dentry,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200283 .list = list,
Al Viro49be4fb2014-10-23 23:00:53 -0400284 .root = RB_ROOT,
Miklos Szeredi56656e92016-03-21 17:31:46 +0100285 .is_lowest = false,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200286 };
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100287 int idx, next;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200288
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100289 for (idx = 0; idx != -1; idx = next) {
290 next = ovl_path_next(idx, dentry, &realpath);
Miklos Szeredic9f00fd2014-11-20 16:40:01 +0100291
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100292 if (next != -1) {
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100293 err = ovl_dir_read(&realpath, &rdd);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200294 if (err)
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100295 break;
296 } else {
297 /*
298 * Insert lowest layer entries before upper ones, this
299 * allows offsets to be reasonably constant
300 */
301 list_add(&rdd.middle, rdd.list);
Miklos Szeredi56656e92016-03-21 17:31:46 +0100302 rdd.is_lowest = true;
Miklos Szeredi9d7459d2014-12-13 00:59:44 +0100303 err = ovl_dir_read(&realpath, &rdd);
304 list_del(&rdd.middle);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200305 }
306 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200307 return err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200308}
309
310static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
311{
hujianyang43303972014-12-11 10:30:18 +0800312 struct list_head *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200313 loff_t off = 0;
314
hujianyang43303972014-12-11 10:30:18 +0800315 list_for_each(p, &od->cache->entries) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200316 if (off >= pos)
317 break;
318 off++;
319 }
hujianyang43303972014-12-11 10:30:18 +0800320 /* Cursor is safe since the cache is stable */
321 od->cursor = p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200322}
323
324static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
325{
326 int res;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200327 struct ovl_dir_cache *cache;
328
329 cache = ovl_dir_cache(dentry);
330 if (cache && ovl_dentry_version_get(dentry) == cache->version) {
331 cache->refcount++;
332 return cache;
333 }
334 ovl_set_dir_cache(dentry, NULL);
335
336 cache = kzalloc(sizeof(struct ovl_dir_cache), GFP_KERNEL);
337 if (!cache)
338 return ERR_PTR(-ENOMEM);
339
340 cache->refcount = 1;
341 INIT_LIST_HEAD(&cache->entries);
342
Miklos Szeredic9f00fd2014-11-20 16:40:01 +0100343 res = ovl_dir_read_merged(dentry, &cache->entries);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200344 if (res) {
345 ovl_cache_free(&cache->entries);
346 kfree(cache);
347 return ERR_PTR(res);
348 }
349
350 cache->version = ovl_dentry_version_get(dentry);
351 ovl_set_dir_cache(dentry, cache);
352
353 return cache;
354}
355
356static int ovl_iterate(struct file *file, struct dir_context *ctx)
357{
358 struct ovl_dir_file *od = file->private_data;
359 struct dentry *dentry = file->f_path.dentry;
hujianyang43303972014-12-11 10:30:18 +0800360 struct ovl_cache_entry *p;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200361
362 if (!ctx->pos)
363 ovl_dir_reset(file);
364
365 if (od->is_real)
366 return iterate_dir(od->realfile, ctx);
367
368 if (!od->cache) {
369 struct ovl_dir_cache *cache;
370
371 cache = ovl_cache_get(dentry);
372 if (IS_ERR(cache))
373 return PTR_ERR(cache);
374
375 od->cache = cache;
376 ovl_seek_cursor(od, ctx->pos);
377 }
378
hujianyang43303972014-12-11 10:30:18 +0800379 while (od->cursor != &od->cache->entries) {
380 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
381 if (!p->is_whiteout)
382 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
383 break;
384 od->cursor = p->l_node.next;
385 ctx->pos++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200386 }
387 return 0;
388}
389
390static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
391{
392 loff_t res;
393 struct ovl_dir_file *od = file->private_data;
394
Al Viro59551022016-01-22 15:40:57 -0500395 inode_lock(file_inode(file));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200396 if (!file->f_pos)
397 ovl_dir_reset(file);
398
399 if (od->is_real) {
400 res = vfs_llseek(od->realfile, offset, origin);
401 file->f_pos = od->realfile->f_pos;
402 } else {
403 res = -EINVAL;
404
405 switch (origin) {
406 case SEEK_CUR:
407 offset += file->f_pos;
408 break;
409 case SEEK_SET:
410 break;
411 default:
412 goto out_unlock;
413 }
414 if (offset < 0)
415 goto out_unlock;
416
417 if (offset != file->f_pos) {
418 file->f_pos = offset;
419 if (od->cache)
420 ovl_seek_cursor(od, offset);
421 }
422 res = offset;
423 }
424out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500425 inode_unlock(file_inode(file));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200426
427 return res;
428}
429
430static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
431 int datasync)
432{
433 struct ovl_dir_file *od = file->private_data;
434 struct dentry *dentry = file->f_path.dentry;
435 struct file *realfile = od->realfile;
436
437 /*
438 * Need to check if we started out being a lower dir, but got copied up
439 */
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100440 if (!od->is_upper && OVL_TYPE_UPPER(ovl_path_type(dentry))) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200441 struct inode *inode = file_inode(file);
442
Miklos Szeredi76768952014-11-20 16:40:02 +0100443 realfile = lockless_dereference(od->upperfile);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200444 if (!realfile) {
445 struct path upperpath;
446
447 ovl_path_upper(dentry, &upperpath);
448 realfile = ovl_path_open(&upperpath, O_RDONLY);
Al Virod45f00a2014-10-28 18:27:28 -0400449 smp_mb__before_spinlock();
Al Viro59551022016-01-22 15:40:57 -0500450 inode_lock(inode);
Al Viro3d268c92014-10-23 22:56:05 -0400451 if (!od->upperfile) {
452 if (IS_ERR(realfile)) {
Al Viro59551022016-01-22 15:40:57 -0500453 inode_unlock(inode);
Al Viro3d268c92014-10-23 22:56:05 -0400454 return PTR_ERR(realfile);
455 }
456 od->upperfile = realfile;
457 } else {
458 /* somebody has beaten us to it */
459 if (!IS_ERR(realfile))
460 fput(realfile);
461 realfile = od->upperfile;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200462 }
Al Viro59551022016-01-22 15:40:57 -0500463 inode_unlock(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200464 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200465 }
466
467 return vfs_fsync_range(realfile, start, end, datasync);
468}
469
470static int ovl_dir_release(struct inode *inode, struct file *file)
471{
472 struct ovl_dir_file *od = file->private_data;
473
474 if (od->cache) {
Al Viro59551022016-01-22 15:40:57 -0500475 inode_lock(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200476 ovl_cache_put(od, file->f_path.dentry);
Al Viro59551022016-01-22 15:40:57 -0500477 inode_unlock(inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200478 }
479 fput(od->realfile);
480 if (od->upperfile)
481 fput(od->upperfile);
482 kfree(od);
483
484 return 0;
485}
486
487static int ovl_dir_open(struct inode *inode, struct file *file)
488{
489 struct path realpath;
490 struct file *realfile;
491 struct ovl_dir_file *od;
492 enum ovl_path_type type;
493
494 od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
495 if (!od)
496 return -ENOMEM;
497
498 type = ovl_path_real(file->f_path.dentry, &realpath);
499 realfile = ovl_path_open(&realpath, file->f_flags);
500 if (IS_ERR(realfile)) {
501 kfree(od);
502 return PTR_ERR(realfile);
503 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200504 od->realfile = realfile;
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100505 od->is_real = !OVL_TYPE_MERGE(type);
506 od->is_upper = OVL_TYPE_UPPER(type);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200507 file->private_data = od;
508
509 return 0;
510}
511
512const struct file_operations ovl_dir_operations = {
513 .read = generic_read_dir,
514 .open = ovl_dir_open,
515 .iterate = ovl_iterate,
516 .llseek = ovl_dir_llseek,
517 .fsync = ovl_dir_fsync,
518 .release = ovl_dir_release,
519};
520
521int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
522{
523 int err;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200524 struct ovl_cache_entry *p;
525
Miklos Szeredic9f00fd2014-11-20 16:40:01 +0100526 err = ovl_dir_read_merged(dentry, list);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200527 if (err)
528 return err;
529
530 err = 0;
531
532 list_for_each_entry(p, list, l_node) {
533 if (p->is_whiteout)
534 continue;
535
536 if (p->name[0] == '.') {
537 if (p->len == 1)
538 continue;
539 if (p->len == 2 && p->name[1] == '.')
540 continue;
541 }
542 err = -ENOTEMPTY;
543 break;
544 }
545
546 return err;
547}
548
549void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list)
550{
551 struct ovl_cache_entry *p;
552
Al Viro59551022016-01-22 15:40:57 -0500553 inode_lock_nested(upper->d_inode, I_MUTEX_CHILD);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200554 list_for_each_entry(p, list, l_node) {
555 struct dentry *dentry;
556
557 if (!p->is_whiteout)
558 continue;
559
560 dentry = lookup_one_len(p->name, upper, p->len);
561 if (IS_ERR(dentry)) {
562 pr_err("overlayfs: lookup '%s/%.*s' failed (%i)\n",
563 upper->d_name.name, p->len, p->name,
564 (int) PTR_ERR(dentry));
565 continue;
566 }
Konstantin Khlebnikov84889d42015-11-16 18:44:11 +0300567 if (dentry->d_inode)
568 ovl_cleanup(upper->d_inode, dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200569 dput(dentry);
570 }
Al Viro59551022016-01-22 15:40:57 -0500571 inode_unlock(upper->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200572}
Vivek Goyal45aebea2016-02-22 09:28:34 -0500573
574static int ovl_check_d_type(struct dir_context *ctx, const char *name,
575 int namelen, loff_t offset, u64 ino,
576 unsigned int d_type)
577{
578 struct ovl_readdir_data *rdd =
579 container_of(ctx, struct ovl_readdir_data, ctx);
580
581 /* Even if d_type is not supported, DT_DIR is returned for . and .. */
582 if (!strncmp(name, ".", namelen) || !strncmp(name, "..", namelen))
583 return 0;
584
585 if (d_type != DT_UNKNOWN)
586 rdd->d_type_supported = true;
587
588 return 0;
589}
590
591/*
592 * Returns 1 if d_type is supported, 0 not supported/unknown. Negative values
593 * if error is encountered.
594 */
595int ovl_check_d_type_supported(struct path *realpath)
596{
597 int err;
598 struct ovl_readdir_data rdd = {
599 .ctx.actor = ovl_check_d_type,
600 .d_type_supported = false,
601 };
602
603 err = ovl_dir_read(realpath, &rdd);
604 if (err)
605 return err;
606
607 return rdd.d_type_supported;
608}
Miklos Szeredieea2fb42016-09-01 11:11:59 +0200609
610static void ovl_workdir_cleanup_recurse(struct path *path, int level)
611{
612 int err;
613 struct inode *dir = path->dentry->d_inode;
614 LIST_HEAD(list);
615 struct ovl_cache_entry *p;
616 struct ovl_readdir_data rdd = {
617 .ctx.actor = ovl_fill_merge,
618 .dentry = NULL,
619 .list = &list,
620 .root = RB_ROOT,
621 .is_lowest = false,
622 };
623
624 err = ovl_dir_read(path, &rdd);
625 if (err)
626 goto out;
627
628 inode_lock_nested(dir, I_MUTEX_PARENT);
629 list_for_each_entry(p, &list, l_node) {
630 struct dentry *dentry;
631
632 if (p->name[0] == '.') {
633 if (p->len == 1)
634 continue;
635 if (p->len == 2 && p->name[1] == '.')
636 continue;
637 }
638 dentry = lookup_one_len(p->name, path->dentry, p->len);
639 if (IS_ERR(dentry))
640 continue;
641 if (dentry->d_inode)
642 ovl_workdir_cleanup(dir, path->mnt, dentry, level);
643 dput(dentry);
644 }
645 inode_unlock(dir);
646out:
647 ovl_cache_free(&list);
648}
649
650void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
651 struct dentry *dentry, int level)
652{
653 int err;
654
655 if (!d_is_dir(dentry) || level > 1) {
656 ovl_cleanup(dir, dentry);
657 return;
658 }
659
660 err = ovl_do_rmdir(dir, dentry);
661 if (err) {
662 struct path path = { .mnt = mnt, .dentry = dentry };
663
664 inode_unlock(dir);
665 ovl_workdir_cleanup_recurse(&path, level + 1);
666 inode_lock_nested(dir, I_MUTEX_PARENT);
667 ovl_cleanup(dir, dentry);
668 }
669}