History

Merge branch 'overlayfs-next' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs into for-next

Al Viro committed 11 years ago

Commit ce7b9fa2 parents a95104f 43303977 files · +517 −319Browse files

Unified · Split

7 files
  1. Documentation/filesystems/overlayfs.txt +28 −0
  2. fs/overlayfs/copy_up.c +2 −3
  3. fs/overlayfs/dir.c +14 −14
  4. fs/overlayfs/inode.c +7 −5
  5. fs/overlayfs/overlayfs.h +13 −5
  6. fs/overlayfs/readdir.c +75 −106
  7. fs/overlayfs/super.c +378 −186
Documentation/filesystems/overlayfs.txt+28−0
@@ -159,6 +159,22 @@ overlay filesystem (though an operation on the name of the file such as · 158 unmodified lines
159159 rename or unlink will of course be noticed and handled).
160160
161161
162+Multiple lower layers
163+---------------------
164+
165+Multiple lower layers can now be given using the the colon (":") as a
166+separator character between the directory names. For example:
167+
168+ mount -t overlay overlay -olowerdir=/lower1:/lower2:/lower3 /merged
169+
170+As the example shows, "upperdir=" and "workdir=" may be omitted. In
171+that case the overlay will be read-only.
172+
173+The specified lower directories will be stacked beginning from the
174+rightmost one and going left. In the above example lower1 will be the
175+top, lower2 the middle and lower3 the bottom layer.
176+
177+
162178 Non-standard behavior
163179 ---------------------
164180
@@ -196,3 +212,15 @@ Changes to the underlying filesystems while part of a mounted overlay · 31 unmodified lines
196212 filesystem are not allowed. If the underlying filesystem is changed,
197213 the behavior of the overlay is undefined, though it will not result in
198214 a crash or deadlock.
215+
216+Testsuite
217+---------
218+
219+There's testsuite developed by David Howells at:
220+
221+ git://git.infradead.org/users/dhowells/unionmount-testsuite.git
222+
223+Run as root:
224+
225+ # cd unionmount-testsuite
226+ # ./run --ov
fs/overlayfs/copy_up.c+2−3
@@ -191,7 +191,6 @@ int ovl_set_attr(struct dentry *upperdentry, struct kstat *stat) · 190 unmodified lines
191191 ovl_set_timestamps(upperdentry, stat);
192192
193193 return err;
194−
195194 }
196195
197196 static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,
@@ -385,7 +384,7 @@ int ovl_copy_up(struct dentry *dentry) · 187 unmodified lines
385384 struct kstat stat;
386385 enum ovl_path_type type = ovl_path_type(dentry);
387386
388− if (type != OVL_PATH_LOWER)
387+ if (OVL_TYPE_UPPER(type))
389388 break;
390389
391390 next = dget(dentry);
@@ -394,7 +393,7 @@ int ovl_copy_up(struct dentry *dentry) · 2 unmodified lines
394393 parent = dget_parent(next);
395394
396395 type = ovl_path_type(parent);
397− if (type != OVL_PATH_LOWER)
396+ if (OVL_TYPE_UPPER(type))
398397 break;
399398
400399 dput(next);
fs/overlayfs/dir.c+14−14
@@ -118,14 +118,14 @@ int ovl_create_real(struct inode *dir, struct dentry *newdentry, · 117 unmodified lines
118118
119119 static int ovl_set_opaque(struct dentry *upperdentry)
120120 {
121− return ovl_do_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
121+ return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
122122 }
123123
124124 static void ovl_remove_opaque(struct dentry *upperdentry)
125125 {
126126 int err;
127127
128− err = ovl_do_removexattr(upperdentry, ovl_opaque_xattr);
128+ err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE);
129129 if (err) {
130130 pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n",
131131 upperdentry->d_name.name, err);
@@ -152,7 +152,7 @@ static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry, · 20 unmodified lines
152152 * correct link count. nlink=1 seems to pacify 'find' and
153153 * other utilities.
154154 */
155− if (type == OVL_PATH_MERGE)
155+ if (OVL_TYPE_MERGE(type))
156156 stat->nlink = 1;
157157
158158 return 0;
@@ -506,7 +506,7 @@ static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir) · 347 unmodified lines
506506 struct dentry *opaquedir = NULL;
507507 int err;
508508
509− if (is_dir) {
509+ if (is_dir && OVL_TYPE_MERGE_OR_LOWER(ovl_path_type(dentry))) {
510510 opaquedir = ovl_check_empty_and_clear(dentry);
511511 err = PTR_ERR(opaquedir);
512512 if (IS_ERR(opaquedir))
@@ -630,7 +630,7 @@ static int ovl_do_remove(struct dentry *dentry, bool is_dir) · 117 unmodified lines
630630 goto out_drop_write;
631631
632632 type = ovl_path_type(dentry);
633− if (type == OVL_PATH_PURE_UPPER) {
633+ if (OVL_TYPE_PURE_UPPER(type)) {
634634 err = ovl_remove_upper(dentry, is_dir);
635635 } else {
636636 const struct cred *old_cred;
@@ -712,7 +712,7 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old, · 75 unmodified lines
712712 /* Don't copy up directory trees */
713713 old_type = ovl_path_type(old);
714714 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)
716716 goto out;
717717
718718 if (new->d_inode) {
@@ -725,25 +725,25 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old, · 6 unmodified lines
725725
726726 new_type = ovl_path_type(new);
727727 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)
729729 goto out;
730730
731731 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)) {
733733 if (ovl_dentry_lower(old)->d_inode ==
734734 ovl_dentry_lower(new)->d_inode)
735735 goto out;
736736 }
737− if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
737+ if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
738738 if (ovl_dentry_upper(old)->d_inode ==
739739 ovl_dentry_upper(new)->d_inode)
740740 goto out;
741741 }
742742 } else {
743743 if (ovl_dentry_is_opaque(new))
744− new_type = OVL_PATH_UPPER;
744+ new_type = __OVL_PATH_UPPER;
745745 else
746− new_type = OVL_PATH_PURE_UPPER;
746+ new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
747747 }
748748
749749 err = ovl_want_write(old);
@@ -763,8 +763,8 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old, · 13 unmodified lines
763763 goto out_drop_write;
764764 }
765765
766− old_opaque = old_type != OVL_PATH_PURE_UPPER;
767− new_opaque = new_type != OVL_PATH_PURE_UPPER;
766+ old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
767+ new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
768768
769769 if (old_opaque || new_opaque) {
770770 err = -ENOMEM;
@@ -787,7 +787,7 @@ static int ovl_rename2(struct inode *olddir, struct dentry *old, · 16 unmodified lines
787787 old_cred = override_creds(override_cred);
788788 }
789789
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) {
791791 opaquedir = ovl_check_empty_and_clear(new);
792792 err = PTR_ERR(opaquedir);
793793 if (IS_ERR(opaquedir)) {
fs/overlayfs/inode.c+7−5
@@ -205,7 +205,7 @@ static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz) · 204 unmodified lines
205205
206206 static bool ovl_is_private_xattr(const char *name)
207207 {
208− return strncmp(name, "trusted.overlay.", 14) == 0;
208+ return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
209209 }
210210
211211 int ovl_setxattr(struct dentry *dentry, const char *name,
@@ -238,7 +238,10 @@ out: · 26 unmodified lines
238238 static bool ovl_need_xattr_filter(struct dentry *dentry,
239239 enum ovl_path_type type)
240240 {
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;
242245 }
243246
244247 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
@@ -299,7 +302,7 @@ int ovl_removexattr(struct dentry *dentry, const char *name) · 54 unmodified lines
299302 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
300303 goto out_drop_write;
301304
302− if (type == OVL_PATH_LOWER) {
305+ if (!OVL_TYPE_UPPER(type)) {
303306 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
304307 if (err < 0)
305308 goto out_drop_write;
@@ -321,7 +324,7 @@ out: · 15 unmodified lines
321324 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
322325 struct dentry *realdentry)
323326 {
324− if (type != OVL_PATH_LOWER)
327+ if (OVL_TYPE_UPPER(type))
325328 return false;
326329
327330 if (special_file(realdentry->d_inode->i_mode))
@@ -430,5 +433,4 @@ struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, · 102 unmodified lines
430433 }
431434
432435 return inode;
433−
434436 }
fs/overlayfs/overlayfs.h+13−5
@@ -12,13 +12,20 @@ · 11 unmodified lines
1212 struct ovl_entry;
1313
1414 enum ovl_path_type {
15− OVL_PATH_PURE_UPPER,
16− OVL_PATH_UPPER,
17− OVL_PATH_MERGE,
18− OVL_PATH_LOWER,
15+ __OVL_PATH_PURE = (1 << 0),
16+ __OVL_PATH_UPPER = (1 << 1),
17+ __OVL_PATH_MERGE = (1 << 2),
1918 };
2019
21−extern 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"
2229
2330 static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry)
2431 {
@@ -130,6 +137,7 @@ void ovl_dentry_version_inc(struct dentry *dentry); · 105 unmodified lines
130137 void ovl_path_upper(struct dentry *dentry, struct path *path);
131138 void ovl_path_lower(struct dentry *dentry, struct path *path);
132139 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
140+int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
133141 struct dentry *ovl_dentry_upper(struct dentry *dentry);
134142 struct dentry *ovl_dentry_lower(struct dentry *dentry);
135143 struct dentry *ovl_dentry_real(struct dentry *dentry);
fs/overlayfs/readdir.c+75−106
@@ -24,7 +24,6 @@ struct ovl_cache_entry { · 23 unmodified lines
2424 struct list_head l_node;
2525 struct rb_node node;
2626 bool is_whiteout;
27− bool is_cursor;
2827 char name[];
2928 };
3029
@@ -40,6 +39,7 @@ struct ovl_readdir_data { · 9 unmodified lines
4039 struct rb_root root;
4140 struct list_head *list;
4241 struct list_head middle;
42+ struct dentry *dir;
4343 int count;
4444 int err;
4545 };
@@ -48,7 +48,7 @@ struct ovl_dir_file { · 2 unmodified lines
4848 bool is_real;
4949 bool is_upper;
5050 struct ovl_dir_cache *cache;
51− struct ovl_cache_entry cursor;
51+ struct list_head *cursor;
5252 struct file *realfile;
5353 struct file *upperfile;
5454 };
@@ -79,23 +79,49 @@ static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root, · 24 unmodified lines
7979 return NULL;
8080 }
8181
82−static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
82+static struct ovl_cache_entry *ovl_cache_entry_new(struct dentry *dir,
83+ const char *name, int len,
8384 u64 ino, unsigned int d_type)
8485 {
8586 struct ovl_cache_entry *p;
8687 size_t size = offsetof(struct ovl_cache_entry, name[len + 1]);
8788
8889 p = kmalloc(size, GFP_KERNEL);
89− if (p) {
90− memcpy(p->name, name, len);
91− p->name[len] = '\0';
92− p->len = len;
93− p->type = d_type;
94− p->ino = ino;
95− p->is_whiteout = false;
96− p->is_cursor = false;
97− }
90+ if (!p)
91+ return NULL;
92+
93+ memcpy(p->name, name, len);
94+ p->name[len] = '\0';
95+ p->len = len;
96+ p->type = d_type;
97+ p->ino = ino;
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);
98116
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+ }
99125 return p;
100126 }
101127
@@ -122,7 +148,7 @@ static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd, · 20 unmodified lines
122148 return 0;
123149 }
124150
125− p = ovl_cache_entry_new(name, len, ino, d_type);
151+ p = ovl_cache_entry_new(rdd->dir, name, len, ino, d_type);
126152 if (p == NULL)
127153 return -ENOMEM;
128154
@@ -143,7 +169,7 @@ static int ovl_fill_lower(struct ovl_readdir_data *rdd, · 14 unmodified lines
143169 if (p) {
144170 list_move_tail(&p->l_node, &rdd->middle);
145171 } 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);
147173 if (p == NULL)
148174 rdd->err = -ENOMEM;
149175 else
@@ -168,7 +194,6 @@ static void ovl_cache_put(struct ovl_dir_file *od, struct dentry *dentry) · 18 unmodified lines
168194 {
169195 struct ovl_dir_cache *cache = od->cache;
170196
171− list_del_init(&od->cursor.l_node);
172197 WARN_ON(cache->refcount <= 0);
173198 cache->refcount--;
174199 if (!cache->refcount) {
@@ -204,6 +229,7 @@ static inline int ovl_dir_read(struct path *realpath, · 29 unmodified lines
204229 if (IS_ERR(realfile))
205230 return PTR_ERR(realfile);
206231
232+ rdd->dir = realpath->dentry;
207233 rdd->ctx.pos = 0;
208234 do {
209235 rdd->count = 0;
@@ -227,108 +253,58 @@ static void ovl_dir_reset(struct file *file) · 17 unmodified lines
227253 if (cache && ovl_dentry_version_get(dentry) != cache->version) {
228254 ovl_cache_put(od, dentry);
229255 od->cache = NULL;
256+ od->cursor = NULL;
230257 }
231− WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
232− if (od->is_real && type == OVL_PATH_MERGE)
258+ WARN_ON(!od->is_real && !OVL_TYPE_MERGE(type));
259+ if (od->is_real && OVL_TYPE_MERGE(type))
233260 od->is_real = false;
234261 }
235262
236−static 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−
279263 static int ovl_dir_read_merged(struct dentry *dentry, struct list_head *list)
280264 {
281265 int err;
282− struct path lowerpath;
283− struct path upperpath;
266+ struct path realpath;
284267 struct ovl_readdir_data rdd = {
285268 .ctx.actor = ovl_fill_merge,
286269 .list = list,
287270 .root = RB_ROOT,
288271 .is_merge = false,
289272 };
273+ int idx, next;
290274
291− ovl_path_lower(dentry, &lowerpath);
292− ovl_path_upper(dentry, &upperpath);
275+ for (idx = 0; idx != -1; idx = next) {
276+ next = ovl_path_next(idx, dentry, &realpath);
293277
294− if (upperpath.dentry) {
295− err = ovl_dir_read(&upperpath, &rdd);
296− if (err)
297− goto out;
298−
299− if (lowerpath.dentry) {
300− err = ovl_dir_mark_whiteouts(upperpath.dentry, &rdd);
278+ if (next != -1) {
279+ err = ovl_dir_read(&realpath, &rdd);
301280 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);
303291 }
304292 }
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− }
315−out:
316293 return err;
317294 }
318295
319296 static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
320297 {
321− struct ovl_cache_entry *p;
298+ struct list_head *p;
322299 loff_t off = 0;
323300
324− list_for_each_entry(p, &od->cache->entries, l_node) {
325− if (p->is_cursor)
326− continue;
301+ list_for_each(p, &od->cache->entries) {
327302 if (off >= pos)
328303 break;
329304 off++;
330305 }
331− list_move_tail(&od->cursor.l_node, &p->l_node);
306+ /* Cursor is safe since the cache is stable */
307+ od->cursor = p;
332308 }
333309
334310 static 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) · 32 unmodified lines
367343 {
368344 struct ovl_dir_file *od = file->private_data;
369345 struct dentry *dentry = file->f_path.dentry;
346+ struct ovl_cache_entry *p;
370347
371348 if (!ctx->pos)
372349 ovl_dir_reset(file);
@@ -385,19 +362,13 @@ static int ovl_iterate(struct file *file, struct dir_context *ctx) · 12 unmodified lines
385362 ovl_seek_cursor(od, ctx->pos);
386363 }
387364
388− while (od->cursor.l_node.next != &od->cache->entries) {
389− struct ovl_cache_entry *p;
390−
391− p = list_entry(od->cursor.l_node.next, struct ovl_cache_entry, l_node);
392− /* Skip cursors */
393− if (!p->is_cursor) {
394− if (!p->is_whiteout) {
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);
365+ while (od->cursor != &od->cache->entries) {
366+ p = list_entry(od->cursor, struct ovl_cache_entry, l_node);
367+ if (!p->is_whiteout)
368+ if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
369+ break;
370+ od->cursor = p->l_node.next;
371+ ctx->pos++;
401372 }
402373 return 0;
403374 }
@@ -452,7 +423,7 @@ static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end, · 48 unmodified lines
452423 /*
453424 * Need to check if we started out being a lower dir, but got copied up
454425 */
455− if (!od->is_upper && ovl_path_type(dentry) != OVL_PATH_LOWER) {
426+ if (!od->is_upper && OVL_TYPE_UPPER(ovl_path_type(dentry))) {
456427 struct inode *inode = file_inode(file);
457428
458429 realfile = lockless_dereference(od->upperfile);
@@ -516,11 +487,9 @@ static int ovl_dir_open(struct inode *inode, struct file *file) · 57 unmodified lines
516487 kfree(od);
517488 return PTR_ERR(realfile);
518489 }
519− INIT_LIST_HEAD(&od->cursor.l_node);
520490 od->realfile = realfile;
521− od->is_real = (type != OVL_PATH_MERGE);
522− od->is_upper = (type != OVL_PATH_LOWER);
523− od->cursor.is_cursor = true;
491+ od->is_real = !OVL_TYPE_MERGE(type);
492+ od->is_upper = OVL_TYPE_UPPER(type);
524493 file->private_data = od;
525494
526495 return 0;
fs/overlayfs/super.c+378−186
@@ -35,7 +35,8 @@ struct ovl_config { · 34 unmodified lines
3535 /* private information held for overlayfs's superblock */
3636 struct ovl_fs {
3737 struct vfsmount *upper_mnt;
38− struct vfsmount *lower_mnt;
38+ unsigned numlower;
39+ struct vfsmount **lower_mnt;
3940 struct dentry *workdir;
4041 long lower_namelen;
4142 /* pathnames of lower and upper dirs, for show_options */
@@ -47,7 +48,6 @@ struct ovl_dir_cache; · 5 unmodified lines
4748 /* private information held for every overlayfs dentry */
4849 struct ovl_entry {
4950 struct dentry *__upperdentry;
50− struct dentry *lowerdentry;
5151 struct ovl_dir_cache *cache;
5252 union {
5353 struct {
@@ -56,30 +56,36 @@ struct ovl_entry { · 2 unmodified lines
5656 };
5757 struct rcu_head rcu;
5858 };
59+ unsigned numlower;
60+ struct path lowerstack[];
5961 };
6062
61−const char *ovl_opaque_xattr = "trusted.overlay.opaque";
63+#define OVL_MAX_STACK 500
6264
65+static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
66+{
67+ return oe->numlower ? oe->lowerstack[0].dentry : NULL;
68+}
6369
6470 enum ovl_path_type ovl_path_type(struct dentry *dentry)
6571 {
6672 struct ovl_entry *oe = dentry->d_fsdata;
73+ enum ovl_path_type type = 0;
6774
6875 if (oe->__upperdentry) {
69− if (oe->lowerdentry) {
76+ type = __OVL_PATH_UPPER;
77+
78+ if (oe->numlower) {
7079 if (S_ISDIR(dentry->d_inode->i_mode))
71− return OVL_PATH_MERGE;
72− else
73− return OVL_PATH_UPPER;
74− } else {
75− if (oe->opaque)
76− return OVL_PATH_UPPER;
77− else
78− return OVL_PATH_PURE_UPPER;
80+ type |= __OVL_PATH_MERGE;
81+ } else if (!oe->opaque) {
82+ type |= __OVL_PATH_PURE;
7983 }
8084 } else {
81− return OVL_PATH_LOWER;
85+ if (oe->numlower > 1)
86+ type |= __OVL_PATH_MERGE;
8287 }
88+ return type;
8389 }
8490
8591 static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
@@ -98,10 +104,9 @@ void ovl_path_upper(struct dentry *dentry, struct path *path) · 12 unmodified lines
98104
99105 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
100106 {
101−
102107 enum ovl_path_type type = ovl_path_type(dentry);
103108
104− if (type == OVL_PATH_LOWER)
109+ if (!OVL_TYPE_UPPER(type))
105110 ovl_path_lower(dentry, path);
106111 else
107112 ovl_path_upper(dentry, path);
@@ -120,7 +125,7 @@ struct dentry *ovl_dentry_lower(struct dentry *dentry) · 12 unmodified lines
120125 {
121126 struct ovl_entry *oe = dentry->d_fsdata;
122127
123− return oe->lowerdentry;
128+ return __ovl_dentry_lower(oe);
124129 }
125130
126131 struct dentry *ovl_dentry_real(struct dentry *dentry)
@@ -130,7 +135,7 @@ struct dentry *ovl_dentry_real(struct dentry *dentry) · 3 unmodified lines
130135
131136 realdentry = ovl_upperdentry_dereference(oe);
132137 if (!realdentry)
133− realdentry = oe->lowerdentry;
138+ realdentry = __ovl_dentry_lower(oe);
134139
135140 return realdentry;
136141 }
@@ -143,7 +148,7 @@ struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper) · 6 unmodified lines
143148 if (realdentry) {
144149 *is_upper = true;
145150 } else {
146− realdentry = oe->lowerdentry;
151+ realdentry = __ovl_dentry_lower(oe);
147152 *is_upper = false;
148153 }
149154 return realdentry;
@@ -165,11 +170,9 @@ void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) · 15 unmodified lines
165170
166171 void ovl_path_lower(struct dentry *dentry, struct path *path)
167172 {
168− struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
169173 struct ovl_entry *oe = dentry->d_fsdata;
170174
171− path->mnt = ofs->lower_mnt;
172− path->dentry = oe->lowerdentry;
175+ *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
173176 }
174177
175178 int ovl_want_write(struct dentry *dentry)
@@ -249,7 +252,7 @@ static bool ovl_is_opaquedir(struct dentry *dentry) · 73 unmodified lines
249252 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
250253 return false;
251254
252− res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1);
255+ res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
253256 if (res == 1 && val == 'y')
254257 return true;
255258
@@ -261,8 +264,11 @@ static void ovl_dentry_release(struct dentry *dentry) · 5 unmodified lines
261264 struct ovl_entry *oe = dentry->d_fsdata;
262265
263266 if (oe) {
267+ unsigned int i;
268+
264269 dput(oe->__upperdentry);
265− dput(oe->lowerdentry);
270+ for (i = 0; i < oe->numlower; i++)
271+ dput(oe->lowerstack[i].dentry);
266272 kfree_rcu(oe, rcu);
267273 }
268274 }
@@ -271,9 +277,15 @@ static const struct dentry_operations ovl_dentry_operations = { · 2 unmodified lines
271277 .d_release = ovl_dentry_release,
272278 };
273279
274−static struct ovl_entry *ovl_alloc_entry(void)
280+static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
275281 {
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;
277289 }
278290
279291 static inline struct dentry *ovl_lookup_real(struct dentry *dir,
@@ -295,82 +307,154 @@ static inline struct dentry *ovl_lookup_real(struct dentry *dir, · 15 unmodified lines
295307 return dentry;
296308 }
297309
310+/*
311+ * Returns next layer in stack starting from top.
312+ * Returns -1 if this is the last layer.
313+ */
314+int 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+
298331 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
299332 unsigned int flags)
300333 {
301334 struct ovl_entry *oe;
302− struct dentry *upperdir;
303− struct dentry *lowerdir;
304− struct dentry *upperdentry = NULL;
305− struct dentry *lowerdentry = NULL;
335+ struct ovl_entry *poe = dentry->d_parent->d_fsdata;
336+ struct path *stack = NULL;
337+ struct dentry *upperdir, *upperdentry = NULL;
338+ unsigned int ctr = 0;
306339 struct inode *inode = NULL;
340+ bool upperopaque = false;
341+ struct dentry *this, *prev = NULL;
342+ unsigned int i;
307343 int err;
308344
309− err = -ENOMEM;
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−
345+ upperdir = ovl_upperdentry_dereference(poe);
317346 if (upperdir) {
318− upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
319− err = PTR_ERR(upperdentry);
320− if (IS_ERR(upperdentry))
321− goto out_put_dir;
322−
323− if (lowerdir && upperdentry) {
324− if (ovl_is_whiteout(upperdentry)) {
325− dput(upperdentry);
326− upperdentry = NULL;
327− oe->opaque = true;
328− } else if (ovl_is_opaquedir(upperdentry)) {
329− oe->opaque = true;
347+ this = ovl_lookup_real(upperdir, &dentry->d_name);
348+ err = PTR_ERR(this);
349+ if (IS_ERR(this))
350+ goto out;
351+
352+ if (this) {
353+ if (ovl_is_whiteout(this)) {
354+ dput(this);
355+ this = NULL;
356+ upperopaque = true;
357+ } else if (poe->numlower && ovl_is_opaquedir(this)) {
358+ upperopaque = true;
330359 }
331360 }
361+ upperdentry = prev = this;
332362 }
333− if (lowerdir && !oe->opaque) {
334− lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
335− err = PTR_ERR(lowerdentry);
336− if (IS_ERR(lowerdentry))
337− goto out_dput_upper;
363+
364+ if (!upperopaque && poe->numlower) {
365+ err = -ENOMEM;
366+ stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
367+ if (!stack)
368+ goto out_put_upper;
338369 }
339370
340− if (lowerdentry && upperdentry &&
341− (!S_ISDIR(upperdentry->d_inode->i_mode) ||
342− !S_ISDIR(lowerdentry->d_inode->i_mode))) {
343− dput(lowerdentry);
344− lowerdentry = NULL;
345− oe->opaque = true;
371+ for (i = 0; !upperopaque && i < poe->numlower; i++) {
372+ bool opaque = false;
373+ struct path lowerpath = poe->lowerstack[i];
374+
375+ this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
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;
346421 }
347422
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) {
349429 struct dentry *realdentry;
350430
351− realdentry = upperdentry ? upperdentry : lowerdentry;
431+ realdentry = upperdentry ? upperdentry : stack[0].dentry;
432+
352433 err = -ENOMEM;
353434 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
354435 oe);
355436 if (!inode)
356− goto out_dput;
437+ goto out_free_oe;
357438 ovl_copyattr(realdentry->d_inode, inode);
358439 }
359440
441+ oe->opaque = upperopaque;
360442 oe->__upperdentry = upperdentry;
361− oe->lowerdentry = lowerdentry;
362−
443+ memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
444+ kfree(stack);
363445 dentry->d_fsdata = oe;
364446 d_add(dentry, inode);
365447
366448 return NULL;
367449
368−out_dput:
369− dput(lowerdentry);
370−out_dput_upper:
371− dput(upperdentry);
372−out_put_dir:
450+out_free_oe:
373451 kfree(oe);
452+out_put:
453+ for (i = 0; i < ctr; i++)
454+ dput(stack[i].dentry);
455+ kfree(stack);
456+out_put_upper:
457+ dput(upperdentry);
374458 out:
375459 return ERR_PTR(err);
376460 }
@@ -383,10 +467,12 @@ struct file *ovl_path_open(struct path *path, int flags) · 6 unmodified lines
383467 static void ovl_put_super(struct super_block *sb)
384468 {
385469 struct ovl_fs *ufs = sb->s_fs_info;
470+ unsigned i;
386471
387472 dput(ufs->workdir);
388473 mntput(ufs->upper_mnt);
389− mntput(ufs->lower_mnt);
474+ for (i = 0; i < ufs->numlower; i++)
475+ mntput(ufs->lower_mnt[i]);
390476
391477 kfree(ufs->config.lowerdir);
392478 kfree(ufs->config.upperdir);
@@ -400,7 +486,7 @@ static void ovl_put_super(struct super_block *sb) · 7 unmodified lines
400486 * @buf: The struct kstatfs to fill in with stats
401487 *
402488 * 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)
404490 */
405491 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
406492 {
@@ -409,7 +495,7 @@ static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) · 2 unmodified lines
409495 struct path path;
410496 int err;
411497
412− ovl_path_upper(root_dentry, &path);
498+ ovl_path_real(root_dentry, &path);
413499
414500 err = vfs_statfs(&path, buf);
415501 if (!err) {
@@ -432,8 +518,21 @@ static int ovl_show_options(struct seq_file *m, struct dentry *dentry) · 16 unmodified lines
432518 struct ovl_fs *ufs = sb->s_fs_info;
433519
434520 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
435− seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
436− seq_printf(m, ",workdir=%s", ufs->config.workdir);
521+ if (ufs->config.upperdir) {
522+ seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
523+ seq_printf(m, ",workdir=%s", ufs->config.workdir);
524+ }
525+ return 0;
526+}
527+
528+static 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+
437536 return 0;
438537 }
439538
@@ -441,6 +540,7 @@ static const struct super_operations ovl_super_operations = { · 1 unmodified line
441540 .put_super = ovl_put_super,
442541 .statfs = ovl_statfs,
443542 .show_options = ovl_show_options,
543+ .remount_fs = ovl_remount,
444544 };
445545
446546 enum {
@@ -585,24 +685,6 @@ static void ovl_unescape(char *s) · 138 unmodified lines
585685 }
586686 }
587687
588−static 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−
606688 static bool ovl_is_allowed_fs_type(struct dentry *root)
607689 {
608690 const struct dentry_operations *dop = root->d_op;
@@ -622,6 +704,75 @@ static bool ovl_is_allowed_fs_type(struct dentry *root) · 13 unmodified lines
622704 return true;
623705 }
624706
707+static 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+
731+out_put:
732+ path_put(path);
733+out:
734+ return err;
735+}
736+
737+static 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+
750+static 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+
770+out_put:
771+ path_put(path);
772+out:
773+ return err;
774+}
775+
625776 /* Workdir should not be subdir of upperdir and vice versa */
626777 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
627778 {
@@ -634,16 +785,39 @@ static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) · 6 unmodified lines
634785 return ok;
635786 }
636787
788+static 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+
637808 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
638809 {
639− struct path lowerpath;
640− struct path upperpath;
641− struct path workpath;
642− struct inode *root_inode;
810+ struct path upperpath = { NULL, NULL };
811+ struct path workpath = { NULL, NULL };
643812 struct dentry *root_dentry;
644813 struct ovl_entry *oe;
645814 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;
647821 int err;
648822
649823 err = -ENOMEM;
@@ -655,123 +829,135 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent) · 5 unmodified lines
655829 if (err)
656830 goto out_free_config;
657831
658− /* FIXME: workdir is not needed for a R/O mount */
659832 err = -EINVAL;
660− if (!ufs->config.upperdir || !ufs->config.lowerdir ||
661− !ufs->config.workdir) {
662− pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
833+ if (!ufs->config.lowerdir) {
834+ pr_err("overlayfs: missing 'lowerdir'\n");
663835 goto out_free_config;
664836 }
665837
666− err = -ENOMEM;
667− oe = ovl_alloc_entry();
668− if (oe == NULL)
669− goto out_free_config;
670−
671− err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
672− if (err)
673− goto out_free_oe;
674−
675− err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath);
676− if (err)
677− goto out_put_upperpath;
838+ sb->s_stack_depth = 0;
839+ if (ufs->config.upperdir) {
840+ /* FIXME: workdir is not needed for a R/O mount */
841+ if (!ufs->config.workdir) {
842+ pr_err("overlayfs: missing 'workdir'\n");
843+ goto out_free_config;
844+ }
678845
679− err = ovl_mount_dir(ufs->config.workdir, &workpath);
680− if (err)
681− goto out_put_lowerpath;
846+ err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
847+ if (err)
848+ goto out_free_config;
682849
683− err = -EINVAL;
684− if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
685− !S_ISDIR(lowerpath.dentry->d_inode->i_mode) ||
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− }
850+ err = ovl_mount_dir(ufs->config.workdir, &workpath);
851+ if (err)
852+ goto out_put_upperpath;
690853
691− if (upperpath.mnt != workpath.mnt) {
692− pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
693− goto out_put_workpath;
694− }
695− if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
696− pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
697− goto out_put_workpath;
854+ err = -EINVAL;
855+ if (upperpath.mnt != workpath.mnt) {
856+ pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
857+ goto out_put_workpath;
858+ }
859+ if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
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;
698864 }
699−
700− if (!ovl_is_allowed_fs_type(upperpath.dentry)) {
701− pr_err("overlayfs: filesystem of upperdir is not supported\n");
865+ err = -ENOMEM;
866+ lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
867+ if (!lowertmp)
702868 goto out_put_workpath;
703− }
704869
705− if (!ovl_is_allowed_fs_type(lowerpath.dentry)) {
706− pr_err("overlayfs: filesystem of lowerdir is not supported\n");
707− goto out_put_workpath;
708− }
870+ err = -EINVAL;
871+ stacklen = ovl_split_lowerdirs(lowertmp);
872+ if (stacklen > OVL_MAX_STACK)
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;
709885
710− err = vfs_statfs(&lowerpath, &statfs);
711− if (err) {
712− pr_err("overlayfs: statfs failed on lowerpath\n");
713− goto out_put_workpath;
886+ lower = strchr(lower, '\0') + 1;
714887 }
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;
719888
720889 err = -EINVAL;
890+ sb->s_stack_depth++;
721891 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
722892 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
723− goto out_put_workpath;
893+ goto out_put_lowerpath;
724894 }
725895
726− ufs->upper_mnt = clone_private_mount(&upperpath);
727− err = PTR_ERR(ufs->upper_mnt);
728− if (IS_ERR(ufs->upper_mnt)) {
729− pr_err("overlayfs: failed to clone upperpath\n");
730− goto out_put_workpath;
731− }
896+ if (ufs->config.upperdir) {
897+ ufs->upper_mnt = clone_private_mount(&upperpath);
898+ err = PTR_ERR(ufs->upper_mnt);
899+ if (IS_ERR(ufs->upper_mnt)) {
900+ pr_err("overlayfs: failed to clone upperpath\n");
901+ goto out_put_lowerpath;
902+ }
732903
733− ufs->lower_mnt = clone_private_mount(&lowerpath);
734− err = PTR_ERR(ufs->lower_mnt);
735− if (IS_ERR(ufs->lower_mnt)) {
736− pr_err("overlayfs: failed to clone lowerpath\n");
737− goto out_put_upper_mnt;
904+ ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
905+ err = PTR_ERR(ufs->workdir);
906+ if (IS_ERR(ufs->workdir)) {
907+ pr_err("overlayfs: failed to create directory %s/%s\n",
908+ ufs->config.workdir, OVL_WORKDIR_NAME);
909+ goto out_put_upper_mnt;
910+ }
738911 }
739912
740− ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
741− err = PTR_ERR(ufs->workdir);
742− if (IS_ERR(ufs->workdir)) {
743− pr_err("overlayfs: failed to create directory %s/%s\n",
744− ufs->config.workdir, OVL_WORKDIR_NAME);
745− goto out_put_lower_mnt;
746− }
913+ err = -ENOMEM;
914+ ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
915+ if (ufs->lower_mnt == NULL)
916+ goto out_put_workdir;
917+ for (i = 0; i < numlower; i++) {
918+ struct vfsmount *mnt = clone_private_mount(&stack[i]);
747919
748− /*
749− * Make lower_mnt R/O. That way fchmod/fchown on lower file
750− * will fail instead of modifying lower fs.
751− */
752− ufs->lower_mnt->mnt_flags |= MNT_READONLY;
920+ err = PTR_ERR(mnt);
921+ if (IS_ERR(mnt)) {
922+ pr_err("overlayfs: failed to clone lowerpath\n");
923+ goto out_put_lower_mnt;
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+ }
753934
754− /* If the upper fs is r/o, we mark overlayfs r/o too */
755− if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
935+ /* If the upper fs is r/o or nonexistent, we mark overlayfs r/o too */
936+ if (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY))
756937 sb->s_flags |= MS_RDONLY;
757938
758939 sb->s_d_op = &ovl_dentry_operations;
759940
760941 err = -ENOMEM;
761− root_inode = ovl_new_inode(sb, S_IFDIR, oe);
762− if (!root_inode)
763− goto out_put_workdir;
942+ oe = ovl_alloc_entry(numlower);
943+ if (!oe)
944+ goto out_put_lower_mnt;
764945
765− root_dentry = d_make_root(root_inode);
946+ root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
766947 if (!root_dentry)
767− goto out_put_workdir;
948+ goto out_free_oe;
768949
769950 mntput(upperpath.mnt);
770− mntput(lowerpath.mnt);
951+ for (i = 0; i < numlower; i++)
952+ mntput(stack[i].mnt);
771953 path_put(&workpath);
954+ kfree(lowertmp);
772955
773956 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+ }
775961
776962 root_dentry->d_fsdata = oe;
777963
@@ -782,20 +968,26 @@ static int ovl_fill_super(struct super_block *sb, void *data, int silent) · 4 unmodified lines
782968
783969 return 0;
784970
971+out_free_oe:
972+ kfree(oe);
973+out_put_lower_mnt:
974+ for (i = 0; i < ufs->numlower; i++)
975+ mntput(ufs->lower_mnt[i]);
976+ kfree(ufs->lower_mnt);
785977 out_put_workdir:
786978 dput(ufs->workdir);
787−out_put_lower_mnt:
788− mntput(ufs->lower_mnt);
789979 out_put_upper_mnt:
790980 mntput(ufs->upper_mnt);
981+out_put_lowerpath:
982+ for (i = 0; i < numlower; i++)
983+ path_put(&stack[i]);
984+ kfree(stack);
985+out_free_lowertmp:
986+ kfree(lowertmp);
791987 out_put_workpath:
792988 path_put(&workpath);
793−out_put_lowerpath:
794− path_put(&lowerpath);
795989 out_put_upperpath:
796990 path_put(&upperpath);
797−out_free_oe:
798− kfree(oe);
799991 out_free_config:
800992 kfree(ufs->config.lowerdir);
801993 kfree(ufs->config.upperdir);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t