aboutsummaryrefslogtreecommitdiffstats
path: root/fs/overlayfs
diff options
context:
space:
mode:
authorAl Viro <viro@zeniv.linux.org.uk>2015-02-20 04:58:52 -0500
committerAl Viro <viro@zeniv.linux.org.uk>2015-02-20 04:58:52 -0500
commitce7b9facdf43b42fb95bdff9069aefeddb7d0a69 (patch)
treedb49cfb72630e88172b51e586acfa9ddb22b5dc1 /fs/overlayfs
parenta95104fd3393080e8bcca348f51996f5f0f5ccb6 (diff)
parent4330397e4e8a662f36d101659e2a59ce32e76ff4 (diff)
Merge branch 'overlayfs-next' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs into for-next
Diffstat (limited to 'fs/overlayfs')
-rw-r--r--fs/overlayfs/copy_up.c5
-rw-r--r--fs/overlayfs/dir.c28
-rw-r--r--fs/overlayfs/inode.c12
-rw-r--r--fs/overlayfs/overlayfs.h18
-rw-r--r--fs/overlayfs/readdir.c181
-rw-r--r--fs/overlayfs/super.c564
6 files changed, 489 insertions, 319 deletions
diff --git a/fs/overlayfs/copy_up.c b/fs/overlayfs/copy_up.c
index ea10a8719107..24f640441bd9 100644
--- a/fs/overlayfs/copy_up.c
+++ b/fs/overlayfs/copy_up.c
@@ -191,7 +191,6 @@ int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat)
191 ovl_set_timestamps(upperdentry, stat); 191 ovl_set_timestamps(upperdentry, stat);
192 192
193 return err; 193 return err;
194
195} 194}
196 195
197static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, 196static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
@@ -385,7 +384,7 @@ int ovl_copy_up(struct dentry *dentry)
385 struct kstat stat; 384 struct kstat stat;
386 enum ovl_path_type type = ovl_path_type(dentry); 385 enum ovl_path_type type = ovl_path_type(dentry);
387 386
388 if (type != OVL_PATH_LOWER) 387 if (OVL_TYPE_UPPER(type))
389 break; 388 break;
390 389
391 next = dget(dentry); 390 next = dget(dentry);
@@ -394,7 +393,7 @@ int ovl_copy_up(struct dentry *dentry)
394 parent = dget_parent(next); 393 parent = dget_parent(next);
395 394
396 type = ovl_path_type(parent); 395 type = ovl_path_type(parent);
397 if (type != OVL_PATH_LOWER) 396 if (OVL_TYPE_UPPER(type))
398 break; 397 break;
399 398
400 dput(next); 399 dput(next);
diff --git a/fs/overlayfs/dir.c b/fs/overlayfs/dir.c
index 8ffc4b980f1b..0dc4c33a0a1b 100644
--- a/fs/overlayfs/dir.c
+++ b/fs/overlayfs/dir.c
@@ -118,14 +118,14 @@ int ovl_create_real(struct inode *dir, struct dentry *newdentry,
118 118
119static int ovl_set_opaque(struct dentry *upperdentry) 119static int ovl_set_opaque(struct dentry *upperdentry)
120{ 120{
121 return ovl_do_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0); 121 return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
122} 122}
123 123
124static void ovl_remove_opaque(struct dentry *upperdentry) 124static void ovl_remove_opaque(struct dentry *upperdentry)
125{ 125{
126 int err; 126 int err;
127 127
128 err = ovl_do_removexattr(upperdentry, ovl_opaque_xattr); 128 err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE);
129 if (err) { 129 if (err) {
130 pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n", 130 pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n",
131 upperdentry->d_name.name, err); 131 upperdentry->d_name.name, err);
@@ -152,7 +152,7 @@ static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
152 * correct link count. nlink=1 seems to pacify 'find' and 152 * correct link count. nlink=1 seems to pacify 'find' and
153 * other utilities. 153 * other utilities.
154 */ 154 */
155 if (type == OVL_PATH_MERGE) 155 if (OVL_TYPE_MERGE(type))
156 stat->nlink = 1; 156 stat->nlink = 1;
157 157
158 return 0; 158 return 0;
@@ -506,7 +506,7 @@ static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
506 struct dentry *opaquedir = NULL; 506 struct dentry *opaquedir = NULL;
507 int err; 507 int err;
508 508
509 if (is_dir) { 509 if (is_dir && OVL_TYPE_MERGE_OR_LOWER(ovl_path_type(dentry))) {
510 opaquedir = ovl_check_empty_and_clear(dentry); 510 opaquedir = ovl_check_empty_and_clear(dentry);
511 err = PTR_ERR(opaquedir); 511 err = PTR_ERR(opaquedir);
512 if (IS_ERR(opaquedir)) 512 if (IS_ERR(opaquedir))
@@ -630,7 +630,7 @@ static int ovl_do_remove(struct dentry *dentry, bool is_dir)
630 goto out_drop_write; 630 goto out_drop_write;
631 631
632 type = ovl_path_type(dentry); 632 type = ovl_path_type(dentry);
633 if (type == OVL_PATH_PURE_UPPER) { 633 if (OVL_TYPE_PURE_UPPER(type)) {
634 err = ovl_remove_upper(dentry, is_dir); 634 err = ovl_remove_upper(dentry, is_dir);
635 } else { 635 } else {
636 const struct cred *old_cred; 636 const struct cred *old_cred;
@@ -712,7 +712,7 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
712 /* Don't copy up directory trees */ 712 /* Don't copy up directory trees */
713 old_type = ovl_path_type(old); 713 old_type = ovl_path_type(old);
714 err = -EXDEV; 714 err = -EXDEV;
715 if ((old_type == OVL_PATH_LOWER || old_type == OVL_PATH_MERGE) && is_dir) 715 if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir)
716 goto out; 716 goto out;
717 717
718 if (new->d_inode) { 718 if (new->d_inode) {
@@ -725,25 +725,25 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
725 725
726 new_type = ovl_path_type(new); 726 new_type = ovl_path_type(new);
727 err = -EXDEV; 727 err = -EXDEV;
728 if (!overwrite && (new_type == OVL_PATH_LOWER || new_type == OVL_PATH_MERGE) && new_is_dir) 728 if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir)
729 goto out; 729 goto out;
730 730
731 err = 0; 731 err = 0;
732 if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) { 732 if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) {
733 if (ovl_dentry_lower(old)->d_inode == 733 if (ovl_dentry_lower(old)->d_inode ==
734 ovl_dentry_lower(new)->d_inode) 734 ovl_dentry_lower(new)->d_inode)
735 goto out; 735 goto out;
736 } 736 }
737 if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) { 737 if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
738 if (ovl_dentry_upper(old)->d_inode == 738 if (ovl_dentry_upper(old)->d_inode ==
739 ovl_dentry_upper(new)->d_inode) 739 ovl_dentry_upper(new)->d_inode)
740 goto out; 740 goto out;
741 } 741 }
742 } else { 742 } else {
743 if (ovl_dentry_is_opaque(new)) 743 if (ovl_dentry_is_opaque(new))
744 new_type = OVL_PATH_UPPER; 744 new_type = __OVL_PATH_UPPER;
745 else 745 else
746 new_type = OVL_PATH_PURE_UPPER; 746 new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
747 } 747 }
748 748
749 err = ovl_want_write(old); 749 err = ovl_want_write(old);
@@ -763,8 +763,8 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
763 goto out_drop_write; 763 goto out_drop_write;
764 } 764 }
765 765
766 old_opaque = old_type != OVL_PATH_PURE_UPPER; 766 old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
767 new_opaque = new_type != OVL_PATH_PURE_UPPER; 767 new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
768 768
769 if (old_opaque || new_opaque) { 769 if (old_opaque || new_opaque) {
770 err = -ENOMEM; 770 err = -ENOMEM;
@@ -787,7 +787,7 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old,
787 old_cred = override_creds(override_cred); 787 old_cred = override_creds(override_cred);
788 } 788 }
789 789
790 if (overwrite && (new_type == OVL_PATH_LOWER || new_type == OVL_PATH_MERGE) && new_is_dir) { 790 if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
791 opaquedir = ovl_check_empty_and_clear(new); 791 opaquedir = ovl_check_empty_and_clear(new);
792 err = PTR_ERR(opaquedir); 792 err = PTR_ERR(opaquedir);
793 if (IS_ERR(opaquedir)) { 793 if (IS_ERR(opaquedir)) {
diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c
index 07d74b24913b..04f124884687 100644
--- a/fs/overlayfs/inode.c
+++ b/fs/overlayfs/inode.c
@@ -205,7 +205,7 @@ static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
205 205
206static bool ovl_is_private_xattr(const char *name) 206static bool ovl_is_private_xattr(const char *name)
207{ 207{
208 return strncmp(name, "trusted.overlay.", 14) == 0; 208 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
209} 209}
210 210
211int ovl_setxattr(struct dentry *dentry, const char *name, 211int ovl_setxattr(struct dentry *dentry, const char *name,
@@ -238,7 +238,10 @@ out:
238static bool ovl_need_xattr_filter(struct dentry *dentry, 238static bool ovl_need_xattr_filter(struct dentry *dentry,
239 enum ovl_path_type type) 239 enum ovl_path_type type)
240{ 240{
241 return type == OVL_PATH_UPPER && S_ISDIR(dentry->d_inode->i_mode); 241 if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
242 return S_ISDIR(dentry->d_inode->i_mode);
243 else
244 return false;
242} 245}
243 246
244ssize_t ovl_getxattr(struct dentry *dentry, const char *name, 247ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
@@ -299,7 +302,7 @@ int ovl_removexattr(struct dentry *dentry, const char *name)
299 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name)) 302 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
300 goto out_drop_write; 303 goto out_drop_write;
301 304
302 if (type == OVL_PATH_LOWER) { 305 if (!OVL_TYPE_UPPER(type)) {
303 err = vfs_getxattr(realpath.dentry, name, NULL, 0); 306 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
304 if (err < 0) 307 if (err < 0)
305 goto out_drop_write; 308 goto out_drop_write;
@@ -321,7 +324,7 @@ out:
321static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type, 324static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
322 struct dentry *realdentry) 325 struct dentry *realdentry)
323{ 326{
324 if (type != OVL_PATH_LOWER) 327 if (OVL_TYPE_UPPER(type))
325 return false; 328 return false;
326 329
327 if (special_file(realdentry->d_inode->i_mode)) 330 if (special_file(realdentry->d_inode->i_mode))
@@ -430,5 +433,4 @@ struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
430 } 433 }
431 434
432 return inode; 435 return inode;
433
434} 436}
diff --git a/fs/overlayfs/overlayfs.h b/fs/overlayfs/overlayfs.h
index 814bed33dd07..17ac5afc9ffb 100644
--- a/fs/overlayfs/overlayfs.h
+++ b/fs/overlayfs/overlayfs.h
@@ -12,13 +12,20 @@
12struct ovl_entry; 12struct ovl_entry;
13 13
14enum ovl_path_type { 14enum ovl_path_type {
15 OVL_PATH_PURE_UPPER, 15 __OVL_PATH_PURE = (1 << 0),
16 OVL_PATH_UPPER, 16 __OVL_PATH_UPPER = (1 << 1),
17 OVL_PATH_MERGE, 17 __OVL_PATH_MERGE = (1 << 2),
18 OVL_PATH_LOWER,
19}; 18};
20 19
21extern const char *ovl_opaque_xattr; 20#define OVL_TYPE_UPPER(type) ((type) & __OVL_PATH_UPPER)
21#define OVL_TYPE_MERGE(type) ((type) & __OVL_PATH_MERGE)
22#define OVL_TYPE_PURE_UPPER(type) ((type) & __OVL_PATH_PURE)
23#define OVL_TYPE_MERGE_OR_LOWER(type) \
24 (OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type))
25
26#define OVL_XATTR_PRE_NAME "trusted.overlay."
27#define OVL_XATTR_PRE_LEN 16
28#define OVL_XATTR_OPAQUE OVL_XATTR_PRE_NAME"opaque"
22 29
23static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry) 30static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry)
24{ 31{
@@ -130,6 +137,7 @@ void ovl_dentry_version_inc(struct dentry *dentry);
130void ovl_path_upper(struct dentry *dentry, struct path *path); 137void ovl_path_upper(struct dentry *dentry, struct path *path);
131void ovl_path_lower(struct dentry *dentry, struct path *path); 138void ovl_path_lower(struct dentry *dentry, struct path *path);
132enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path); 139enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
140int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
133struct dentry *ovl_dentry_upper(struct dentry *dentry); 141struct dentry *ovl_dentry_upper(struct dentry *dentry);
134struct dentry *ovl_dentry_lower(struct dentry *dentry); 142struct dentry *ovl_dentry_lower(struct dentry *dentry);
135struct dentry *ovl_dentry_real(struct dentry *dentry); 143struct dentry *ovl_dentry_real(struct dentry *dentry);
diff --git a/fs/overlayfs/readdir.c b/fs/overlayfs/readdir.c
index c0205990a9f5..907870e81a72 100644
--- a/fs/overlayfs/readdir.c
+++ b/fs/overlayfs/readdir.c
@@ -24,7 +24,6 @@ struct ovl_cache_entry {
24 struct list_head l_node; 24 struct list_head l_node;
25 struct rb_node node; 25 struct rb_node node;
26 bool is_whiteout; 26 bool is_whiteout;
27 bool is_cursor;
28 char name[]; 27 char name[];
29}; 28};
30 29
@@ -40,6 +39,7 @@ struct ovl_readdir_data {
40 struct rb_root root; 39 struct rb_root root;
41 struct list_head *list; 40 struct list_head *list;
42 struct list_head middle; 41 struct list_head middle;
42 struct dentry *dir;
43 int count; 43 int count;
44 int err; 44 int err;
45}; 45};
@@ -48,7 +48,7 @@ struct ovl_dir_file {
48 bool is_real; 48 bool is_real;
49 bool is_upper; 49 bool is_upper;
50 struct ovl_dir_cache *cache; 50 struct ovl_dir_cache *cache;
51 struct ovl_cache_entry cursor; 51 struct list_head *cursor;
52 struct file *realfile; 52 struct file *realfile;
53 struct file *upperfile; 53 struct file *upperfile;
54}; 54};
@@ -79,23 +79,49 @@ static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
79 return NULL; 79 return NULL;
80} 80}
81 81
82static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len, 82static struct ovl_cache_entry *ovl_cache_entry_new(struct dentry *dir,
83 const char *name, int len,
83 u64 ino, unsigned int d_type) 84 u64 ino, unsigned int d_type)
84{ 85{
85 struct ovl_cache_entry *p; 86 struct ovl_cache_entry *p;
86 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]); 87 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
87 88
88 p = kmalloc(size, GFP_KERNEL); 89 p = kmalloc(size, GFP_KERNEL);
89 if (p) { 90 if (!p)
90 memcpy(p->name, name, len); 91 return NULL;
91 p->name[len] = '\0'; 92
92 p->len = len; 93 memcpy(p->name, name, len);
93 p->type = d_type; 94 p->name[len] = '\0';
94 p->ino = ino; 95 p->len = len;
95 p->is_whiteout = false; 96 p->type = d_type;
96 p->is_cursor = false; 97 p->ino = ino;
97 } 98 p->is_whiteout = false;
99
100 if (d_type == DT_CHR) {
101 struct dentry *dentry;
102 const struct cred *old_cred;
103 struct cred *override_cred;
104
105 override_cred = prepare_creds();
106 if (!override_cred) {
107 kfree(p);
108 return NULL;
109 }
110
111 /*
112 * CAP_DAC_OVERRIDE for lookup
113 */
114 cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
115 old_cred = override_creds(override_cred);
98 116
117 dentry = lookup_one_len(name, dir, len);
118 if (!IS_ERR(dentry)) {
119 p->is_whiteout = ovl_is_whiteout(dentry);
120 dput(dentry);
121 }
122 revert_creds(old_cred);
123 put_cred(override_cred);
124 }
99 return p; 125 return p;
100} 126}
101 127
@@ -122,7 +148,7 @@ static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
122 return 0; 148 return 0;
123 } 149 }
124 150
125 p = ovl_cache_entry_new(name, len, ino, d_type); 151 p = ovl_cache_entry_new(rdd->dir, name, len, ino, d_type);
126 if (p == NULL) 152 if (p == NULL)
127 return -ENOMEM; 153 return -ENOMEM;
128 154
@@ -143,7 +169,7 @@ static int ovl_fill_lower(struct ovl_readdir_data *rdd,
143 if (p) { 169 if (p) {
144 list_move_tail(&p->l_node, &rdd->middle); 170 list_move_tail(&p->l_node, &rdd->middle);
145 } else { 171 } else {
146 p = ovl_cache_entry_new(name, namelen, ino, d_type); 172 p = ovl_cache_entry_new(rdd->dir, name, namelen, ino, d_type);
147 if (p == NULL) 173 if (p == NULL)
148 rdd->err = -ENOMEM; 174 rdd->err = -ENOMEM;
149 else 175 else
@@ -168,7 +194,6 @@ static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry)
168{ 194{
169 struct ovl_dir_cache *cache = od->cache; 195 struct ovl_dir_cache *cache = od->cache;
170 196
171 list_del_init(&od->cursor.l_node);
172 WARN_ON(cache->refcount <= 0); 197 WARN_ON(cache->refcount <= 0);
173 cache->refcount--; 198 cache->refcount--;
174 if (!cache->refcount) { 199 if (!cache->refcount) {
@@ -204,6 +229,7 @@ static inline int ovl_dir_read(struct path *realpath,
204 if (IS_ERR(realfile)) 229 if (IS_ERR(realfile))
205 return PTR_ERR(realfile); 230 return PTR_ERR(realfile);
206 231
232 rdd->dir = realpath->dentry;
207 rdd->ctx.pos = 0; 233 rdd->ctx.pos = 0;
208 do { 234 do {
209 rdd->count = 0; 235 rdd->count = 0;
@@ -227,108 +253,58 @@ static void ovl_dir_reset(struct file *file)
227 if (cache && ovl_dentry_version_get(dentry) != cache->version) { 253 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
228 ovl_cache_put(od, dentry); 254 ovl_cache_put(od, dentry);
229 od->cache = NULL; 255 od->cache = NULL;
256 od->cursor = NULL;
230 } 257 }
231 WARN_ON(!od->is_real && type != OVL_PATH_MERGE); 258 WARN_ON(!od->is_real && !OVL_TYPE_MERGE(type));
232 if (od->is_real && type == OVL_PATH_MERGE) 259 if (od->is_real && OVL_TYPE_MERGE(type))
233 od->is_real = false; 260 od->is_real = false;
234} 261}
235 262
236static int ovl_dir_mark_whiteouts(struct dentry *dir,
237 struct ovl_readdir_data *rdd)
238{
239 struct ovl_cache_entry *p;
240 struct dentry *dentry;
241 const struct cred *old_cred;
242 struct cred *override_cred;
243
244 override_cred = prepare_creds();
245 if (!override_cred) {
246 ovl_cache_free(rdd->list);
247 return -ENOMEM;
248 }
249
250 /*
251 * CAP_DAC_OVERRIDE for lookup
252 */
253 cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
254 old_cred = override_creds(override_cred);
255
256 mutex_lock(&dir->d_inode->i_mutex);
257 list_for_each_entry(p, rdd->list, l_node) {
258 if (p->is_cursor)
259 continue;
260
261 if (p->type != DT_CHR)
262 continue;
263
264 dentry = lookup_one_len(p->name, dir, p->len);
265 if (IS_ERR(dentry))
266 continue;
267
268 p->is_whiteout = ovl_is_whiteout(dentry);
269 dput(dentry);
270 }
271 mutex_unlock(&dir->d_inode->i_mutex);
272
273 revert_creds(old_cred);
274 put_cred(override_cred);
275
276 return 0;
277}
278
279static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list) 263static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
280{ 264{
281 int err; 265 int err;
282 struct path lowerpath; 266 struct path realpath;
283 struct path upperpath;
284 struct ovl_readdir_data rdd = { 267 struct ovl_readdir_data rdd = {
285 .ctx.actor = ovl_fill_merge, 268 .ctx.actor = ovl_fill_merge,
286 .list = list, 269 .list = list,
287 .root = RB_ROOT, 270 .root = RB_ROOT,
288 .is_merge = false, 271 .is_merge = false,
289 }; 272 };
273 int idx, next;
290 274
291 ovl_path_lower(dentry, &lowerpath); 275 for (idx = 0; idx != -1; idx = next) {
292 ovl_path_upper(dentry, &upperpath); 276 next = ovl_path_next(idx, dentry, &realpath);
293 277
294 if (upperpath.dentry) { 278 if (next != -1) {
295 err = ovl_dir_read(&upperpath, &rdd); 279 err = ovl_dir_read(&realpath, &rdd);
296 if (err)
297 goto out;
298
299 if (lowerpath.dentry) {
300 err = ovl_dir_mark_whiteouts(upperpath.dentry, &rdd);
301 if (err) 280 if (err)
302 goto out; 281 break;
282 } else {
283 /*
284 * Insert lowest layer entries before upper ones, this
285 * allows offsets to be reasonably constant
286 */
287 list_add(&rdd.middle, rdd.list);
288 rdd.is_merge = true;
289 err = ovl_dir_read(&realpath, &rdd);
290 list_del(&rdd.middle);
303 } 291 }
304 } 292 }
305 if (lowerpath.dentry) {
306 /*
307 * Insert lowerpath entries before upperpath ones, this allows
308 * offsets to be reasonably constant
309 */
310 list_add(&rdd.middle, rdd.list);
311 rdd.is_merge = true;
312 err = ovl_dir_read(&lowerpath, &rdd);
313 list_del(&rdd.middle);
314 }
315out:
316 return err; 293 return err;
317} 294}
318 295
319static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos) 296static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
320{ 297{
321 struct ovl_cache_entry *p; 298 struct list_head *p;
322 loff_t off = 0; 299 loff_t off = 0;
323 300
324 list_for_each_entry(p, &od->cache->entries, l_node) { 301 list_for_each(p, &od->cache->entries) {
325 if (p->is_cursor)
326 continue;
327 if (off >= pos) 302 if (off >= pos)
328 break; 303 break;
329 off++; 304 off++;
330 } 305 }
331 list_move_tail(&od->cursor.l_node, &p->l_node); 306 /* Cursor is safe since the cache is stable */
307 od->cursor = p;
332} 308}
333 309
334static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry) 310static struct ovl_dir_cache *ovl_cache_get(struct dentry *dentry)
@@ -367,6 +343,7 @@ static int ovl_iterate(struct file *file, struct dir_context *ctx)
367{ 343{
368 struct ovl_dir_file *od = file->private_data; 344 struct ovl_dir_file *od = file->private_data;
369 struct dentry *dentry = file->f_path.dentry; 345 struct dentry *dentry = file->f_path.dentry;
346 struct ovl_cache_entry *p;
370 347
371 if (!ctx->pos) 348 if (!ctx->pos)
372 ovl_dir_reset(file); 349 ovl_dir_reset(file);
@@ -385,19 +362,13 @@ static int ovl_iterate(struct file *file, struct dir_context *ctx)
385 ovl_seek_cursor(od, ctx->pos); 362 ovl_seek_cursor(od, ctx->pos);
386 } 363 }
387 364
388 while (od->cursor.l_node.next != &od->cache->entries) { 365 while (od->cursor != &od->cache->entries) {
389 struct ovl_cache_entry *p; 366 p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
390 367 if (!p->is_whiteout)
391 p = list_entry(od->cursor.l_node.next, struct ovl_cache_entry, l_node); 368 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
392 /* Skip cursors */ 369 break;
393 if (!p->is_cursor) { 370 od->cursor = p->l_node.next;
394 if (!p->is_whiteout) { 371 ctx->pos++;
395 if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
396 break;
397 }
398 ctx->pos++;
399 }
400 list_move(&od->cursor.l_node, &p->l_node);
401 } 372 }
402 return 0; 373 return 0;
403} 374}
@@ -452,7 +423,7 @@ static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
452 /* 423 /*
453 * Need to check if we started out being a lower dir, but got copied up 424 * Need to check if we started out being a lower dir, but got copied up
454 */ 425 */
455 if (!od->is_upper && ovl_path_type(dentry) != OVL_PATH_LOWER) { 426 if (!od->is_upper && OVL_TYPE_UPPER(ovl_path_type(dentry))) {
456 struct inode *inode = file_inode(file); 427 struct inode *inode = file_inode(file);
457 428
458 realfile = lockless_dereference(od->upperfile); 429 realfile = lockless_dereference(od->upperfile);
@@ -516,11 +487,9 @@ static int ovl_dir_open(struct inode *inode, struct file *file)
516 kfree(od); 487 kfree(od);
517 return PTR_ERR(realfile); 488 return PTR_ERR(realfile);
518 } 489 }
519 INIT_LIST_HEAD(&od->cursor.l_node);
520 od->realfile = realfile; 490 od->realfile = realfile;
521 od->is_real = (type != OVL_PATH_MERGE); 491 od->is_real = !OVL_TYPE_MERGE(type);
522 od->is_upper = (type != OVL_PATH_LOWER); 492 od->is_upper = OVL_TYPE_UPPER(type);
523 od->cursor.is_cursor = true;
524 file->private_data = od; 493 file->private_data = od;
525 494
526 return 0; 495 return 0;
diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c
index f16d318b71f8..b90952f528b1 100644
--- a/fs/overlayfs/super.c
+++ b/fs/overlayfs/super.c
@@ -35,7 +35,8 @@ struct ovl_config {
35/* private information held for overlayfs's superblock */ 35/* private information held for overlayfs's superblock */
36struct ovl_fs { 36struct ovl_fs {
37 struct vfsmount *upper_mnt; 37 struct vfsmount *upper_mnt;
38 struct vfsmount *lower_mnt; 38 unsigned numlower;
39 struct vfsmount **lower_mnt;
39 struct dentry *workdir; 40 struct dentry *workdir;
40 long lower_namelen; 41 long lower_namelen;
41 /* pathnames of lower and upper dirs, for show_options */ 42 /* pathnames of lower and upper dirs, for show_options */
@@ -47,7 +48,6 @@ struct ovl_dir_cache;
47/* private information held for every overlayfs dentry */ 48/* private information held for every overlayfs dentry */
48struct ovl_entry { 49struct ovl_entry {
49 struct dentry *__upperdentry; 50 struct dentry *__upperdentry;
50 struct dentry *lowerdentry;
51 struct ovl_dir_cache *cache; 51 struct ovl_dir_cache *cache;
52 union { 52 union {
53 struct { 53 struct {
@@ -56,30 +56,36 @@ struct ovl_entry {
56 }; 56 };
57 struct rcu_head rcu; 57 struct rcu_head rcu;
58 }; 58 };
59 unsigned numlower;
60 struct path lowerstack[];
59}; 61};
60 62
61const char *ovl_opaque_xattr = "trusted.overlay.opaque"; 63#define OVL_MAX_STACK 500
62 64
65static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
66{
67 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
68}
63 69
64enum ovl_path_type ovl_path_type(struct dentry *dentry) 70enum ovl_path_type ovl_path_type(struct dentry *dentry)
65{ 71{
66 struct ovl_entry *oe = dentry->d_fsdata; 72 struct ovl_entry *oe = dentry->d_fsdata;
73 enum ovl_path_type type = 0;
67 74
68 if (oe->__upperdentry) { 75 if (oe->__upperdentry) {
69 if (oe->lowerdentry) { 76 type = __OVL_PATH_UPPER;
77
78 if (oe->numlower) {
70 if (S_ISDIR(dentry->d_inode->i_mode)) 79 if (S_ISDIR(dentry->d_inode->i_mode))
71 return OVL_PATH_MERGE; 80 type |= __OVL_PATH_MERGE;
72 else 81 } else if (!oe->opaque) {
73 return OVL_PATH_UPPER; 82 type |= __OVL_PATH_PURE;
74 } else {
75 if (oe->opaque)
76 return OVL_PATH_UPPER;
77 else
78 return OVL_PATH_PURE_UPPER;
79 } 83 }
80 } else { 84 } else {
81 return OVL_PATH_LOWER; 85 if (oe->numlower > 1)
86 type |= __OVL_PATH_MERGE;
82 } 87 }
88 return type;
83} 89}
84 90
85static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) 91static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
@@ -98,10 +104,9 @@ void ovl_path_upper(struct dentry *dentry, struct path *path)
98 104
99enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 105enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
100{ 106{
101
102 enum ovl_path_type type = ovl_path_type(dentry); 107 enum ovl_path_type type = ovl_path_type(dentry);
103 108
104 if (type == OVL_PATH_LOWER) 109 if (!OVL_TYPE_UPPER(type))
105 ovl_path_lower(dentry, path); 110 ovl_path_lower(dentry, path);
106 else 111 else
107 ovl_path_upper(dentry, path); 112 ovl_path_upper(dentry, path);
@@ -120,7 +125,7 @@ struct dentry *ovl_dentry_lower(struct dentry *dentry)
120{ 125{
121 struct ovl_entry *oe = dentry->d_fsdata; 126 struct ovl_entry *oe = dentry->d_fsdata;
122 127
123 return oe->lowerdentry; 128 return __ovl_dentry_lower(oe);
124} 129}
125 130
126struct dentry *ovl_dentry_real(struct dentry *dentry) 131struct dentry *ovl_dentry_real(struct dentry *dentry)
@@ -130,7 +135,7 @@ struct dentry *ovl_dentry_real(struct dentry *dentry)
130 135
131 realdentry = ovl_upperdentry_dereference(oe); 136 realdentry = ovl_upperdentry_dereference(oe);
132 if (!realdentry) 137 if (!realdentry)
133 realdentry = oe->lowerdentry; 138 realdentry = __ovl_dentry_lower(oe);
134 139
135 return realdentry; 140 return realdentry;
136} 141}
@@ -143,7 +148,7 @@ struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
143 if (realdentry) { 148 if (realdentry) {
144 *is_upper = true; 149 *is_upper = true;
145 } else { 150 } else {
146 realdentry = oe->lowerdentry; 151 realdentry = __ovl_dentry_lower(oe);
147 *is_upper = false; 152 *is_upper = false;
148 } 153 }
149 return realdentry; 154 return realdentry;
@@ -165,11 +170,9 @@ void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
165 170
166void ovl_path_lower(struct dentry *dentry, struct path *path) 171void ovl_path_lower(struct dentry *dentry, struct path *path)
167{ 172{
168 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
169 struct ovl_entry *oe = dentry->d_fsdata; 173 struct ovl_entry *oe = dentry->d_fsdata;
170 174
171 path->mnt = ofs->lower_mnt; 175 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
172 path->dentry = oe->lowerdentry;
173} 176}
174 177
175int ovl_want_write(struct dentry *dentry) 178int ovl_want_write(struct dentry *dentry)
@@ -249,7 +252,7 @@ static bool ovl_is_opaquedir(struct dentry *dentry)
249 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) 252 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
250 return false; 253 return false;
251 254
252 res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1); 255 res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
253 if (res == 1 && val == 'y') 256 if (res == 1 && val == 'y')
254 return true; 257 return true;
255 258
@@ -261,8 +264,11 @@ static void ovl_dentry_release(struct dentry *dentry)
261 struct ovl_entry *oe = dentry->d_fsdata; 264 struct ovl_entry *oe = dentry->d_fsdata;
262 265
263 if (oe) { 266 if (oe) {
267 unsigned int i;
268
264 dput(oe->__upperdentry); 269 dput(oe->__upperdentry);
265 dput(oe->lowerdentry); 270 for (i = 0; i < oe->numlower; i++)
271 dput(oe->lowerstack[i].dentry);
266 kfree_rcu(oe, rcu); 272 kfree_rcu(oe, rcu);
267 } 273 }
268} 274}
@@ -271,9 +277,15 @@ static const struct dentry_operations ovl_dentry_operations = {
271 .d_release = ovl_dentry_release, 277 .d_release = ovl_dentry_release,
272}; 278};
273 279
274static struct ovl_entry *ovl_alloc_entry(void) 280static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
275{ 281{
276 return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL); 282 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
283 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
284
285 if (oe)
286 oe->numlower = numlower;
287
288 return oe;
277} 289}
278 290
279static inline struct dentry *ovl_lookup_real(struct dentry *dir, 291static inline struct dentry *ovl_lookup_real(struct dentry *dir,
@@ -295,82 +307,154 @@ static inline struct dentry *ovl_lookup_real(struct dentry *dir,
295 return dentry; 307 return dentry;
296} 308}
297 309
310/*
311 * Returns next layer in stack starting from top.
312 * Returns -1 if this is the last layer.
313 */
314int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
315{
316 struct ovl_entry *oe = dentry->d_fsdata;
317
318 BUG_ON(idx < 0);
319 if (idx == 0) {
320 ovl_path_upper(dentry, path);
321 if (path->dentry)
322 return oe->numlower ? 1 : -1;
323 idx++;
324 }
325 BUG_ON(idx > oe->numlower);
326 *path = oe->lowerstack[idx - 1];
327
328 return (idx < oe->numlower) ? idx + 1 : -1;
329}
330
298struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 331struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
299 unsigned int flags) 332 unsigned int flags)
300{ 333{
301 struct ovl_entry *oe; 334 struct ovl_entry *oe;
302 struct dentry *upperdir; 335 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
303 struct dentry *lowerdir; 336 struct path *stack = NULL;
304 struct dentry *upperdentry = NULL; 337 struct dentry *upperdir, *upperdentry = NULL;
305 struct dentry *lowerdentry = NULL; 338 unsigned int ctr = 0;
306 struct inode *inode = NULL; 339 struct inode *inode = NULL;
340 bool upperopaque = false;
341 struct dentry *this, *prev = NULL;
342 unsigned int i;
307 int err; 343 int err;
308 344
309 err = -ENOMEM; 345 upperdir = ovl_upperdentry_dereference(poe);
310 oe = ovl_alloc_entry();
311 if (!oe)
312 goto out;
313
314 upperdir = ovl_dentry_upper(dentry->d_parent);
315 lowerdir = ovl_dentry_lower(dentry->d_parent);
316
317 if (upperdir) { 346 if (upperdir) {
318 upperdentry = ovl_lookup_real(upperdir, &dentry->d_name); 347 this = ovl_lookup_real(upperdir, &dentry->d_name);
319 err = PTR_ERR(upperdentry); 348 err = PTR_ERR(this);
320 if (IS_ERR(upperdentry)) 349 if (IS_ERR(this))
321 goto out_put_dir; 350 goto out;
322 351
323 if (lowerdir && upperdentry) { 352 if (this) {
324 if (ovl_is_whiteout(upperdentry)) { 353 if (ovl_is_whiteout(this)) {
325 dput(upperdentry); 354 dput(this);
326 upperdentry = NULL; 355 this = NULL;
327 oe->opaque = true; 356 upperopaque = true;
328 } else if (ovl_is_opaquedir(upperdentry)) { 357 } else if (poe->numlower && ovl_is_opaquedir(this)) {
329 oe->opaque = true; 358 upperopaque = true;
330 } 359 }
331 } 360 }
361 upperdentry = prev = this;
332 } 362 }
333 if (lowerdir && !oe->opaque) { 363
334 lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name); 364 if (!upperopaque && poe->numlower) {
335 err = PTR_ERR(lowerdentry); 365 err = -ENOMEM;
336 if (IS_ERR(lowerdentry)) 366 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
337 goto out_dput_upper; 367 if (!stack)
368 goto out_put_upper;
338 } 369 }
339 370
340 if (lowerdentry && upperdentry && 371 for (i = 0; !upperopaque && i < poe->numlower; i++) {
341 (!S_ISDIR(upperdentry->d_inode->i_mode) || 372 bool opaque = false;
342 !S_ISDIR(lowerdentry->d_inode->i_mode))) { 373 struct path lowerpath = poe->lowerstack[i];
343 dput(lowerdentry); 374
344 lowerdentry = NULL; 375 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
345 oe->opaque = true; 376 err = PTR_ERR(this);
377 if (IS_ERR(this)) {
378 /*
379 * If it's positive, then treat ENAMETOOLONG as ENOENT.
380 */
381 if (err == -ENAMETOOLONG && (upperdentry || ctr))
382 continue;
383 goto out_put;
384 }
385 if (!this)
386 continue;
387 if (ovl_is_whiteout(this)) {
388 dput(this);
389 break;
390 }
391 /*
392 * Only makes sense to check opaque dir if this is not the
393 * lowermost layer.
394 */
395 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
396 opaque = true;
397
398 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
399 !S_ISDIR(this->d_inode->i_mode))) {
400 /*
401 * FIXME: check for upper-opaqueness maybe better done
402 * in remove code.
403 */
404 if (prev == upperdentry)
405 upperopaque = true;
406 dput(this);
407 break;
408 }
409 /*
410 * If this is a non-directory then stop here.
411 */
412 if (!S_ISDIR(this->d_inode->i_mode))
413 opaque = true;
414
415 stack[ctr].dentry = this;
416 stack[ctr].mnt = lowerpath.mnt;
417 ctr++;
418 prev = this;
419 if (opaque)
420 break;
346 } 421 }
347 422
348 if (lowerdentry || upperdentry) { 423 oe = ovl_alloc_entry(ctr);
424 err = -ENOMEM;
425 if (!oe)
426 goto out_put;
427
428 if (upperdentry || ctr) {
349 struct dentry *realdentry; 429 struct dentry *realdentry;
350 430
351 realdentry = upperdentry ? upperdentry : lowerdentry; 431 realdentry = upperdentry ? upperdentry : stack[0].dentry;
432
352 err = -ENOMEM; 433 err = -ENOMEM;
353 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, 434 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
354 oe); 435 oe);
355 if (!inode) 436 if (!inode)
356 goto out_dput; 437 goto out_free_oe;
357 ovl_copyattr(realdentry->d_inode, inode); 438 ovl_copyattr(realdentry->d_inode, inode);
358 } 439 }
359 440
441 oe->opaque = upperopaque;
360 oe->__upperdentry = upperdentry; 442 oe->__upperdentry = upperdentry;
361 oe->lowerdentry = lowerdentry; 443 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
362 444 kfree(stack);
363 dentry->d_fsdata = oe; 445 dentry->d_fsdata = oe;
364 d_add(dentry, inode); 446 d_add(dentry, inode);
365 447
366 return NULL; 448 return NULL;
367 449
368out_dput: 450out_free_oe:
369 dput(lowerdentry);
370out_dput_upper:
371 dput(upperdentry);
372out_put_dir:
373 kfree(oe); 451 kfree(oe);
452out_put:
453 for (i = 0; i < ctr; i++)
454 dput(stack[i].dentry);
455 kfree(stack);
456out_put_upper:
457 dput(upperdentry);
374out: 458out:
375 return ERR_PTR(err); 459 return ERR_PTR(err);
376} 460}
@@ -383,10 +467,12 @@ struct file *ovl_path_open(struct path *path, int flags)
383static void ovl_put_super(struct super_block *sb) 467static void ovl_put_super(struct super_block *sb)
384{ 468{
385 struct ovl_fs *ufs = sb->s_fs_info; 469 struct ovl_fs *ufs = sb->s_fs_info;
470 unsigned i;
386 471
387 dput(ufs->workdir); 472 dput(ufs->workdir);
388 mntput(ufs->upper_mnt); 473 mntput(ufs->upper_mnt);
389 mntput(ufs->lower_mnt); 474 for (i = 0; i < ufs->numlower; i++)
475 mntput(ufs->lower_mnt[i]);
390 476
391 kfree(ufs->config.lowerdir); 477 kfree(ufs->config.lowerdir);
392 kfree(ufs->config.upperdir); 478 kfree(ufs->config.upperdir);
@@ -400,7 +486,7 @@ static void ovl_put_super(struct super_block *sb)
400 * @buf: The struct kstatfs to fill in with stats 486 * @buf: The struct kstatfs to fill in with stats
401 * 487 *
402 * Get the filesystem statistics. As writes always target the upper layer 488 * Get the filesystem statistics. As writes always target the upper layer
403 * filesystem pass the statfs to the same filesystem. 489 * filesystem pass the statfs to the upper filesystem (if it exists)
404 */ 490 */
405static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) 491static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
406{ 492{
@@ -409,7 +495,7 @@ static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
409 struct path path; 495 struct path path;
410 int err; 496 int err;
411 497
412 ovl_path_upper(root_dentry, &path); 498 ovl_path_real(root_dentry, &path);
413 499
414 err = vfs_statfs(&path, buf); 500 err = vfs_statfs(&path, buf);
415 if (!err) { 501 if (!err) {
@@ -432,8 +518,21 @@ static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
432 struct ovl_fs *ufs = sb->s_fs_info; 518 struct ovl_fs *ufs = sb->s_fs_info;
433 519
434 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir); 520 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
435 seq_printf(m, ",upperdir=%s", ufs->config.upperdir); 521 if (ufs->config.upperdir) {
436 seq_printf(m, ",workdir=%s", ufs->config.workdir); 522 seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
523 seq_printf(m, ",workdir=%s", ufs->config.workdir);
524 }
525 return 0;
526}
527
528static int ovl_remount(struct super_block *sb, int *flags, char *data)
529{
530 struct ovl_fs *ufs = sb->s_fs_info;
531
532 if (!(*flags & MS_RDONLY) &&
533 (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)))
534 return -EROFS;
535
437 return 0; 536 return 0;
438} 537}
439 538
@@ -441,6 +540,7 @@ static const struct super_operations ovl_super_operations = {
441 .put_super = ovl_put_super, 540 .put_super = ovl_put_super,
442 .statfs = ovl_statfs, 541 .statfs = ovl_statfs,
443 .show_options = ovl_show_options, 542 .show_options = ovl_show_options,
543 .remount_fs = ovl_remount,
444}; 544};
445 545
446enum { 546enum {
@@ -585,24 +685,6 @@ static void ovl_unescape(char *s)
585 } 685 }
586} 686}
587 687
588static int ovl_mount_dir(const char *name, struct path *path)
589{
590 int err;
591 char *tmp = kstrdup(name, GFP_KERNEL);
592
593 if (!tmp)
594 return -ENOMEM;
595
596 ovl_unescape(tmp);
597 err = kern_path(tmp, LOOKUP_FOLLOW, path);
598 if (err) {
599 pr_err("overlayfs: failed to resolve '%s': %i\n", tmp, err);
600 err = -EINVAL;
601 }
602 kfree(tmp);
603 return err;
604}
605
606static bool ovl_is_allowed_fs_type(struct dentry *root) 688static bool ovl_is_allowed_fs_type(struct dentry *root)
607{ 689{
608 const struct dentry_operations *dop = root->d_op; 690 const struct dentry_operations *dop = root->d_op;
@@ -622,6 +704,75 @@ static bool ovl_is_allowed_fs_type(struct dentry *root)
622 return true; 704 return true;
623} 705}
624 706
707static int ovl_mount_dir_noesc(const char *name, struct path *path)
708{
709 int err = -EINVAL;
710
711 if (!*name) {
712 pr_err("overlayfs: empty lowerdir\n");
713 goto out;
714 }
715 err = kern_path(name, LOOKUP_FOLLOW, path);
716 if (err) {
717 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
718 goto out;
719 }
720 err = -EINVAL;
721 if (!ovl_is_allowed_fs_type(path->dentry)) {
722 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
723 goto out_put;
724 }
725 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
726 pr_err("overlayfs: '%s' not a directory\n", name);
727 goto out_put;
728 }
729 return 0;
730
731out_put:
732 path_put(path);
733out:
734 return err;
735}
736
737static int ovl_mount_dir(const char *name, struct path *path)
738{
739 int err = -ENOMEM;
740 char *tmp = kstrdup(name, GFP_KERNEL);
741
742 if (tmp) {
743 ovl_unescape(tmp);
744 err = ovl_mount_dir_noesc(tmp, path);
745 kfree(tmp);
746 }
747 return err;
748}
749
750static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
751 int *stack_depth)
752{
753 int err;
754 struct kstatfs statfs;
755
756 err = ovl_mount_dir_noesc(name, path);
757 if (err)
758 goto out;
759
760 err = vfs_statfs(path, &statfs);
761 if (err) {
762 pr_err("overlayfs: statfs failed on '%s'\n", name);
763 goto out_put;
764 }
765 *namelen = max(*namelen, statfs.f_namelen);
766 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
767
768 return 0;
769
770out_put:
771 path_put(path);
772out:
773 return err;
774}
775
625/* Workdir should not be subdir of upperdir and vice versa */ 776/* Workdir should not be subdir of upperdir and vice versa */
626static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) 777static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
627{ 778{
@@ -634,16 +785,39 @@ static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
634 return ok; 785 return ok;
635} 786}
636 787
788static unsigned int ovl_split_lowerdirs(char *str)
789{
790 unsigned int ctr = 1;
791 char *s, *d;
792
793 for (s = d = str;; s++, d++) {
794 if (*s == '\\') {
795 s++;
796 } else if (*s == ':') {
797 *d = '\0';
798 ctr++;
799 continue;
800 }
801 *d = *s;
802 if (!*s)
803 break;
804 }
805 return ctr;
806}
807
637static int ovl_fill_super(struct super_block *sb, void *data, int silent) 808static int ovl_fill_super(struct super_block *sb, void *data, int silent)
638{ 809{
639 struct path lowerpath; 810 struct path upperpath = { NULL, NULL };
640 struct path upperpath; 811 struct path workpath = { NULL, NULL };
641 struct path workpath;
642 struct inode *root_inode;
643 struct dentry *root_dentry; 812 struct dentry *root_dentry;
644 struct ovl_entry *oe; 813 struct ovl_entry *oe;
645 struct ovl_fs *ufs; 814 struct ovl_fs *ufs;
646 struct kstatfs statfs; 815 struct path *stack = NULL;
816 char *lowertmp;
817 char *lower;
818 unsigned int numlower;
819 unsigned int stacklen = 0;
820 unsigned int i;
647 int err; 821 int err;
648 822
649 err = -ENOMEM; 823 err = -ENOMEM;
@@ -655,123 +829,135 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
655 if (err) 829 if (err)
656 goto out_free_config; 830 goto out_free_config;
657 831
658 /* FIXME: workdir is not needed for a R/O mount */
659 err = -EINVAL; 832 err = -EINVAL;
660 if (!ufs->config.upperdir || !ufs->config.lowerdir || 833 if (!ufs->config.lowerdir) {
661 !ufs->config.workdir) { 834 pr_err("overlayfs: missing 'lowerdir'\n");
662 pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
663 goto out_free_config; 835 goto out_free_config;
664 } 836 }
665 837
666 err = -ENOMEM; 838 sb->s_stack_depth = 0;
667 oe = ovl_alloc_entry(); 839 if (ufs->config.upperdir) {
668 if (oe == NULL) 840 /* FIXME: workdir is not needed for a R/O mount */
669 goto out_free_config; 841 if (!ufs->config.workdir) {
670 842 pr_err("overlayfs: missing 'workdir'\n");
671 err = ovl_mount_dir(ufs->config.upperdir, &upperpath); 843 goto out_free_config;
672 if (err) 844 }
673 goto out_free_oe;
674
675 err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath);
676 if (err)
677 goto out_put_upperpath;
678 845
679 err = ovl_mount_dir(ufs->config.workdir, &workpath); 846 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
680 if (err) 847 if (err)
681 goto out_put_lowerpath; 848 goto out_free_config;
682 849
683 err = -EINVAL; 850 err = ovl_mount_dir(ufs->config.workdir, &workpath);
684 if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) || 851 if (err)
685 !S_ISDIR(lowerpath.dentry->d_inode->i_mode) || 852 goto out_put_upperpath;
686 !S_ISDIR(workpath.dentry->d_inode->i_mode)) {
687 pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n");
688 goto out_put_workpath;
689 }
690 853
691 if (upperpath.mnt != workpath.mnt) { 854 err = -EINVAL;
692 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); 855 if (upperpath.mnt != workpath.mnt) {
693 goto out_put_workpath; 856 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
694 } 857 goto out_put_workpath;
695 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { 858 }
696 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); 859 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
697 goto out_put_workpath; 860 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
861 goto out_put_workpath;
862 }
863 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
698 } 864 }
699 865 err = -ENOMEM;
700 if (!ovl_is_allowed_fs_type(upperpath.dentry)) { 866 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
701 pr_err("overlayfs: filesystem of upperdir is not supported\n"); 867 if (!lowertmp)
702 goto out_put_workpath; 868 goto out_put_workpath;
703 }
704 869
705 if (!ovl_is_allowed_fs_type(lowerpath.dentry)) { 870 err = -EINVAL;
706 pr_err("overlayfs: filesystem of lowerdir is not supported\n"); 871 stacklen = ovl_split_lowerdirs(lowertmp);
707 goto out_put_workpath; 872 if (stacklen > OVL_MAX_STACK)
708 } 873 goto out_free_lowertmp;
874
875 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
876 if (!stack)
877 goto out_free_lowertmp;
878
879 lower = lowertmp;
880 for (numlower = 0; numlower < stacklen; numlower++) {
881 err = ovl_lower_dir(lower, &stack[numlower],
882 &ufs->lower_namelen, &sb->s_stack_depth);
883 if (err)
884 goto out_put_lowerpath;
709 885
710 err = vfs_statfs(&lowerpath, &statfs); 886 lower = strchr(lower, '\0') + 1;
711 if (err) {
712 pr_err("overlayfs: statfs failed on lowerpath\n");
713 goto out_put_workpath;
714 } 887 }
715 ufs->lower_namelen = statfs.f_namelen;
716
717 sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
718 lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
719 888
720 err = -EINVAL; 889 err = -EINVAL;
890 sb->s_stack_depth++;
721 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { 891 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
722 pr_err("overlayfs: maximum fs stacking depth exceeded\n"); 892 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
723 goto out_put_workpath; 893 goto out_put_lowerpath;
724 } 894 }
725 895
726 ufs->upper_mnt = clone_private_mount(&upperpath); 896 if (ufs->config.upperdir) {
727 err = PTR_ERR(ufs->upper_mnt); 897 ufs->upper_mnt = clone_private_mount(&upperpath);
728 if (IS_ERR(ufs->upper_mnt)) { 898 err = PTR_ERR(ufs->upper_mnt);
729 pr_err("overlayfs: failed to clone upperpath\n"); 899 if (IS_ERR(ufs->upper_mnt)) {
730 goto out_put_workpath; 900 pr_err("overlayfs: failed to clone upperpath\n");
731 } 901 goto out_put_lowerpath;
902 }
732 903
733 ufs->lower_mnt = clone_private_mount(&lowerpath); 904 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
734 err = PTR_ERR(ufs->lower_mnt); 905 err = PTR_ERR(ufs->workdir);
735 if (IS_ERR(ufs->lower_mnt)) { 906 if (IS_ERR(ufs->workdir)) {
736 pr_err("overlayfs: failed to clone lowerpath\n"); 907 pr_err("overlayfs: failed to create directory %s/%s\n",
737 goto out_put_upper_mnt; 908 ufs->config.workdir, OVL_WORKDIR_NAME);
909 goto out_put_upper_mnt;
910 }
738 } 911 }
739 912
740 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); 913 err = -ENOMEM;
741 err = PTR_ERR(ufs->workdir); 914 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
742 if (IS_ERR(ufs->workdir)) { 915 if (ufs->lower_mnt == NULL)
743 pr_err("overlayfs: failed to create directory %s/%s\n", 916 goto out_put_workdir;
744 ufs->config.workdir, OVL_WORKDIR_NAME); 917 for (i = 0; i < numlower; i++) {
745 goto out_put_lower_mnt; 918 struct vfsmount *mnt = clone_private_mount(&stack[i]);
746 }
747 919
748 /* 920 err = PTR_ERR(mnt);
749 * Make lower_mnt R/O. That way fchmod/fchown on lower file 921 if (IS_ERR(mnt)) {
750 * will fail instead of modifying lower fs. 922 pr_err("overlayfs: failed to clone lowerpath\n");
751 */ 923 goto out_put_lower_mnt;
752 ufs->lower_mnt->mnt_flags |= MNT_READONLY; 924 }
925 /*
926 * Make lower_mnt R/O. That way fchmod/fchown on lower file
927 * will fail instead of modifying lower fs.
928 */
929 mnt->mnt_flags |= MNT_READONLY;
930
931 ufs->lower_mnt[ufs->numlower] = mnt;
932 ufs->numlower++;
933 }
753 934
754 /* If the upper fs is r/o, we mark overlayfs r/o too */ 935 /* If the upper fs is r/o or nonexistent, we mark overlayfs r/o too */
755 if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY) 936 if (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY))
756 sb->s_flags |= MS_RDONLY; 937 sb->s_flags |= MS_RDONLY;
757 938
758 sb->s_d_op = &ovl_dentry_operations; 939 sb->s_d_op = &ovl_dentry_operations;
759 940
760 err = -ENOMEM; 941 err = -ENOMEM;
761 root_inode = ovl_new_inode(sb, S_IFDIR, oe); 942 oe = ovl_alloc_entry(numlower);
762 if (!root_inode) 943 if (!oe)
763 goto out_put_workdir; 944 goto out_put_lower_mnt;
764 945
765 root_dentry = d_make_root(root_inode); 946 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
766 if (!root_dentry) 947 if (!root_dentry)
767 goto out_put_workdir; 948 goto out_free_oe;
768 949
769 mntput(upperpath.mnt); 950 mntput(upperpath.mnt);
770 mntput(lowerpath.mnt); 951 for (i = 0; i < numlower; i++)
952 mntput(stack[i].mnt);
771 path_put(&workpath); 953 path_put(&workpath);
954 kfree(lowertmp);
772 955
773 oe->__upperdentry = upperpath.dentry; 956 oe->__upperdentry = upperpath.dentry;
774 oe->lowerdentry = lowerpath.dentry; 957 for (i = 0; i < numlower; i++) {
958 oe->lowerstack[i].dentry = stack[i].dentry;
959 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
960 }
775 961
776 root_dentry->d_fsdata = oe; 962 root_dentry->d_fsdata = oe;
777 963
@@ -782,20 +968,26 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent)
782 968
783 return 0; 969 return 0;
784 970
971out_free_oe:
972 kfree(oe);
973out_put_lower_mnt:
974 for (i = 0; i < ufs->numlower; i++)
975 mntput(ufs->lower_mnt[i]);
976 kfree(ufs->lower_mnt);
785out_put_workdir: 977out_put_workdir:
786 dput(ufs->workdir); 978 dput(ufs->workdir);
787out_put_lower_mnt:
788 mntput(ufs->lower_mnt);
789out_put_upper_mnt: 979out_put_upper_mnt:
790 mntput(ufs->upper_mnt); 980 mntput(ufs->upper_mnt);
981out_put_lowerpath:
982 for (i = 0; i < numlower; i++)
983 path_put(&stack[i]);
984 kfree(stack);
985out_free_lowertmp:
986 kfree(lowertmp);
791out_put_workpath: 987out_put_workpath:
792 path_put(&workpath); 988 path_put(&workpath);
793out_put_lowerpath:
794 path_put(&lowerpath);
795out_put_upperpath: 989out_put_upperpath:
796 path_put(&upperpath); 990 path_put(&upperpath);
797out_free_oe:
798 kfree(oe);
799out_free_config: 991out_free_config:
800 kfree(ufs->config.lowerdir); 992 kfree(ufs->config.lowerdir);
801 kfree(ufs->config.upperdir); 993 kfree(ufs->config.upperdir);