kernel: remove linux 3.18 support
[openwrt/openwrt.git] / target / linux / generic / pending-4.4 / 051-0005-ovl-proper-cleanup-of-workdir.patch
1 From eea2fb4851e9dcbab6b991aaf47e2e024f1f55a0 Mon Sep 17 00:00:00 2001
2 From: Miklos Szeredi <mszeredi@redhat.com>
3 Date: Thu, 1 Sep 2016 11:11:59 +0200
4 Subject: [PATCH] ovl: proper cleanup of workdir
5
6 When mounting overlayfs it needs a clean "work" directory under the
7 supplied workdir.
8
9 Previously the mount code removed this directory if it already existed and
10 created a new one. If the removal failed (e.g. directory was not empty)
11 then it fell back to a read-only mount not using the workdir.
12
13 While this has never been reported, it is possible to get a non-empty
14 "work" dir from a previous mount of overlayfs in case of crash in the
15 middle of an operation using the work directory.
16
17 In this case the left over state should be discarded and the overlay
18 filesystem will be consistent, guaranteed by the atomicity of operations on
19 moving to/from the workdir to the upper layer.
20
21 This patch implements cleaning out any files left in workdir. It is
22 implemented using real recursion for simplicity, but the depth is limited
23 to 2, because the worst case is that of a directory containing whiteouts
24 under "work".
25
26 Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
27 Cc: <stable@vger.kernel.org>
28 ---
29 fs/overlayfs/overlayfs.h | 2 ++
30 fs/overlayfs/readdir.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++-
31 fs/overlayfs/super.c | 2 +-
32 3 files changed, 65 insertions(+), 2 deletions(-)
33
34 --- a/fs/overlayfs/overlayfs.h
35 +++ b/fs/overlayfs/overlayfs.h
36 @@ -164,6 +164,8 @@ extern const struct file_operations ovl_
37 int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
38 void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list);
39 void ovl_cache_free(struct list_head *list);
40 +void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
41 + struct dentry *dentry, int level);
42
43 /* inode.c */
44 int ovl_setattr(struct dentry *dentry, struct iattr *attr);
45 --- a/fs/overlayfs/readdir.c
46 +++ b/fs/overlayfs/readdir.c
47 @@ -247,7 +247,7 @@ static inline int ovl_dir_read(struct pa
48 err = rdd->err;
49 } while (!err && rdd->count);
50
51 - if (!err && rdd->first_maybe_whiteout)
52 + if (!err && rdd->first_maybe_whiteout && rdd->dentry)
53 err = ovl_check_whiteouts(realpath->dentry, rdd);
54
55 fput(realfile);
56 @@ -573,3 +573,64 @@ void ovl_cleanup_whiteouts(struct dentry
57 }
58 mutex_unlock(&upper->d_inode->i_mutex);
59 }
60 +
61 +static void ovl_workdir_cleanup_recurse(struct path *path, int level)
62 +{
63 + int err;
64 + struct inode *dir = path->dentry->d_inode;
65 + LIST_HEAD(list);
66 + struct ovl_cache_entry *p;
67 + struct ovl_readdir_data rdd = {
68 + .ctx.actor = ovl_fill_merge,
69 + .dentry = NULL,
70 + .list = &list,
71 + .root = RB_ROOT,
72 + .is_lowest = false,
73 + };
74 +
75 + err = ovl_dir_read(path, &rdd);
76 + if (err)
77 + goto out;
78 +
79 + mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
80 + list_for_each_entry(p, &list, l_node) {
81 + struct dentry *dentry;
82 +
83 + if (p->name[0] == '.') {
84 + if (p->len == 1)
85 + continue;
86 + if (p->len == 2 && p->name[1] == '.')
87 + continue;
88 + }
89 + dentry = lookup_one_len(p->name, path->dentry, p->len);
90 + if (IS_ERR(dentry))
91 + continue;
92 + if (dentry->d_inode)
93 + ovl_workdir_cleanup(dir, path->mnt, dentry, level);
94 + dput(dentry);
95 + }
96 + mutex_unlock(&dir->i_mutex);
97 +out:
98 + ovl_cache_free(&list);
99 +}
100 +
101 +void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
102 + struct dentry *dentry, int level)
103 +{
104 + int err;
105 +
106 + if (!d_is_dir(dentry) || level > 1) {
107 + ovl_cleanup(dir, dentry);
108 + return;
109 + }
110 +
111 + err = ovl_do_rmdir(dir, dentry);
112 + if (err) {
113 + struct path path = { .mnt = mnt, .dentry = dentry };
114 +
115 + mutex_unlock(&dir->i_mutex);
116 + ovl_workdir_cleanup_recurse(&path, level + 1);
117 + mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
118 + ovl_cleanup(dir, dentry);
119 + }
120 +}
121 --- a/fs/overlayfs/super.c
122 +++ b/fs/overlayfs/super.c
123 @@ -784,7 +784,7 @@ retry:
124 goto out_dput;
125
126 retried = true;
127 - ovl_cleanup(dir, work);
128 + ovl_workdir_cleanup(dir, mnt, work, 0);
129 dput(work);
130 goto retry;
131 }