blob: 9a7693d5f8fffb38bd4a07804e4627ed7f50361a [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10#include <linux/fs.h>
11#include <linux/namei.h>
Miklos Szeredicf9a6782015-12-11 16:30:49 +010012#include <linux/pagemap.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020013#include <linux/xattr.h>
14#include <linux/security.h>
15#include <linux/mount.h>
16#include <linux/slab.h>
17#include <linux/parser.h>
18#include <linux/module.h>
Andrew Mortone458bcd2016-01-20 15:01:13 -080019#include <linux/pagemap.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020020#include <linux/sched.h>
Andy Whitcroftcc259632014-10-24 00:14:38 +020021#include <linux/statfs.h>
Erez Zadokf45827e82014-10-24 00:14:38 +020022#include <linux/seq_file.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020023#include "overlayfs.h"
24
25MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
26MODULE_DESCRIPTION("Overlay filesystem");
27MODULE_LICENSE("GPL");
28
Erez Zadokf45827e82014-10-24 00:14:38 +020029struct ovl_config {
30 char *lowerdir;
31 char *upperdir;
32 char *workdir;
Miklos Szeredi8d3095f2015-10-12 17:11:44 +020033 bool default_permissions;
Erez Zadokf45827e82014-10-24 00:14:38 +020034};
35
Miklos Szeredie9be9d52014-10-24 00:14:38 +020036/* private information held for overlayfs's superblock */
37struct ovl_fs {
38 struct vfsmount *upper_mnt;
Miklos Szeredidd662662014-12-13 00:59:43 +010039 unsigned numlower;
40 struct vfsmount **lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020041 struct dentry *workdir;
Andy Whitcroftcc259632014-10-24 00:14:38 +020042 long lower_namelen;
Erez Zadokf45827e82014-10-24 00:14:38 +020043 /* pathnames of lower and upper dirs, for show_options */
44 struct ovl_config config;
Antonio Murdaca3fe6e522016-04-07 15:48:25 +020045 /* creds of process who forced instantiation of super block */
46 const struct cred *creator_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020047};
48
49struct ovl_dir_cache;
50
51/* private information held for every overlayfs dentry */
52struct ovl_entry {
53 struct dentry *__upperdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020054 struct ovl_dir_cache *cache;
55 union {
56 struct {
57 u64 version;
58 bool opaque;
59 };
60 struct rcu_head rcu;
61 };
Miklos Szeredidd662662014-12-13 00:59:43 +010062 unsigned numlower;
63 struct path lowerstack[];
Miklos Szeredie9be9d52014-10-24 00:14:38 +020064};
65
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +010066#define OVL_MAX_STACK 500
67
Miklos Szeredidd662662014-12-13 00:59:43 +010068static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
69{
70 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
71}
Miklos Szeredie9be9d52014-10-24 00:14:38 +020072
73enum ovl_path_type ovl_path_type(struct dentry *dentry)
74{
75 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi1afaba12014-12-13 00:59:42 +010076 enum ovl_path_type type = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020077
78 if (oe->__upperdentry) {
Miklos Szeredi1afaba12014-12-13 00:59:42 +010079 type = __OVL_PATH_UPPER;
80
Konstantin Khlebnikov45d11732016-01-31 16:17:53 +030081 /*
82 * Non-dir dentry can hold lower dentry from previous
83 * location. Its purity depends only on opaque flag.
84 */
85 if (oe->numlower && S_ISDIR(dentry->d_inode->i_mode))
86 type |= __OVL_PATH_MERGE;
87 else if (!oe->opaque)
Miklos Szeredi1afaba12014-12-13 00:59:42 +010088 type |= __OVL_PATH_PURE;
Miklos Szeredi9d7459d2014-12-13 00:59:44 +010089 } else {
90 if (oe->numlower > 1)
91 type |= __OVL_PATH_MERGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020092 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +010093 return type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020094}
95
96static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
97{
Miklos Szeredi71d50922014-11-20 16:40:01 +010098 return lockless_dereference(oe->__upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020099}
100
101void ovl_path_upper(struct dentry *dentry, struct path *path)
102{
103 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
104 struct ovl_entry *oe = dentry->d_fsdata;
105
106 path->mnt = ofs->upper_mnt;
107 path->dentry = ovl_upperdentry_dereference(oe);
108}
109
110enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
111{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200112 enum ovl_path_type type = ovl_path_type(dentry);
113
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100114 if (!OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200115 ovl_path_lower(dentry, path);
116 else
117 ovl_path_upper(dentry, path);
118
119 return type;
120}
121
122struct dentry *ovl_dentry_upper(struct dentry *dentry)
123{
124 struct ovl_entry *oe = dentry->d_fsdata;
125
126 return ovl_upperdentry_dereference(oe);
127}
128
129struct dentry *ovl_dentry_lower(struct dentry *dentry)
130{
131 struct ovl_entry *oe = dentry->d_fsdata;
132
Miklos Szeredidd662662014-12-13 00:59:43 +0100133 return __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200134}
135
136struct dentry *ovl_dentry_real(struct dentry *dentry)
137{
138 struct ovl_entry *oe = dentry->d_fsdata;
139 struct dentry *realdentry;
140
141 realdentry = ovl_upperdentry_dereference(oe);
142 if (!realdentry)
Miklos Szeredidd662662014-12-13 00:59:43 +0100143 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200144
145 return realdentry;
146}
147
148struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
149{
150 struct dentry *realdentry;
151
152 realdentry = ovl_upperdentry_dereference(oe);
153 if (realdentry) {
154 *is_upper = true;
155 } else {
Miklos Szeredidd662662014-12-13 00:59:43 +0100156 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200157 *is_upper = false;
158 }
159 return realdentry;
160}
161
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200162struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode,
163 bool is_upper)
164{
165 if (is_upper) {
166 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
167
168 return ofs->upper_mnt;
169 } else {
170 return oe->numlower ? oe->lowerstack[0].mnt : NULL;
171 }
172}
173
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200174struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
175{
176 struct ovl_entry *oe = dentry->d_fsdata;
177
178 return oe->cache;
179}
180
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200181bool ovl_is_default_permissions(struct inode *inode)
182{
183 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
184
185 return ofs->config.default_permissions;
186}
187
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200188void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
189{
190 struct ovl_entry *oe = dentry->d_fsdata;
191
192 oe->cache = cache;
193}
194
195void ovl_path_lower(struct dentry *dentry, struct path *path)
196{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200197 struct ovl_entry *oe = dentry->d_fsdata;
198
Miklos Szeredidd662662014-12-13 00:59:43 +0100199 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200200}
201
202int ovl_want_write(struct dentry *dentry)
203{
204 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
205 return mnt_want_write(ofs->upper_mnt);
206}
207
208void ovl_drop_write(struct dentry *dentry)
209{
210 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
211 mnt_drop_write(ofs->upper_mnt);
212}
213
214struct dentry *ovl_workdir(struct dentry *dentry)
215{
216 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
217 return ofs->workdir;
218}
219
220bool ovl_dentry_is_opaque(struct dentry *dentry)
221{
222 struct ovl_entry *oe = dentry->d_fsdata;
223 return oe->opaque;
224}
225
226void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
227{
228 struct ovl_entry *oe = dentry->d_fsdata;
229 oe->opaque = opaque;
230}
231
232void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
233{
234 struct ovl_entry *oe = dentry->d_fsdata;
235
Al Viro59551022016-01-22 15:40:57 -0500236 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200237 WARN_ON(oe->__upperdentry);
238 BUG_ON(!upperdentry->d_inode);
239 /*
240 * Make sure upperdentry is consistent before making it visible to
241 * ovl_upperdentry_dereference().
242 */
243 smp_wmb();
244 oe->__upperdentry = upperdentry;
245}
246
247void ovl_dentry_version_inc(struct dentry *dentry)
248{
249 struct ovl_entry *oe = dentry->d_fsdata;
250
Al Viro59551022016-01-22 15:40:57 -0500251 WARN_ON(!inode_is_locked(dentry->d_inode));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200252 oe->version++;
253}
254
255u64 ovl_dentry_version_get(struct dentry *dentry)
256{
257 struct ovl_entry *oe = dentry->d_fsdata;
258
Al Viro59551022016-01-22 15:40:57 -0500259 WARN_ON(!inode_is_locked(dentry->d_inode));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200260 return oe->version;
261}
262
263bool ovl_is_whiteout(struct dentry *dentry)
264{
265 struct inode *inode = dentry->d_inode;
266
267 return inode && IS_WHITEOUT(inode);
268}
269
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200270const struct cred *ovl_override_creds(struct super_block *sb)
271{
272 struct ovl_fs *ofs = sb->s_fs_info;
273
274 return override_creds(ofs->creator_cred);
275}
276
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200277static bool ovl_is_opaquedir(struct dentry *dentry)
278{
279 int res;
280 char val;
281 struct inode *inode = dentry->d_inode;
282
283 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
284 return false;
285
Al Viroce23e642016-04-11 00:48:00 -0400286 res = inode->i_op->getxattr(dentry, inode, OVL_XATTR_OPAQUE, &val, 1);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200287 if (res == 1 && val == 'y')
288 return true;
289
290 return false;
291}
292
293static void ovl_dentry_release(struct dentry *dentry)
294{
295 struct ovl_entry *oe = dentry->d_fsdata;
296
297 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +0100298 unsigned int i;
299
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200300 dput(oe->__upperdentry);
Miklos Szeredidd662662014-12-13 00:59:43 +0100301 for (i = 0; i < oe->numlower; i++)
302 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200303 kfree_rcu(oe, rcu);
304 }
305}
306
Miklos Szeredid101a122016-03-26 16:14:37 -0400307static struct dentry *ovl_d_real(struct dentry *dentry, struct inode *inode)
308{
309 struct dentry *real;
310
311 if (d_is_dir(dentry)) {
312 if (!inode || inode == d_inode(dentry))
313 return dentry;
314 goto bug;
315 }
316
317 real = ovl_dentry_upper(dentry);
318 if (real && (!inode || inode == d_inode(real)))
319 return real;
320
321 real = ovl_dentry_lower(dentry);
322 if (!real)
323 goto bug;
324
325 if (!inode || inode == d_inode(real))
326 return real;
327
328 /* Handle recursion */
329 if (real->d_flags & DCACHE_OP_REAL)
330 return real->d_op->d_real(real, inode);
331
332bug:
333 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry,
334 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
335 return dentry;
336}
337
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200338static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
339{
340 struct ovl_entry *oe = dentry->d_fsdata;
341 unsigned int i;
342 int ret = 1;
343
344 for (i = 0; i < oe->numlower; i++) {
345 struct dentry *d = oe->lowerstack[i].dentry;
346
347 if (d->d_flags & DCACHE_OP_REVALIDATE) {
348 ret = d->d_op->d_revalidate(d, flags);
349 if (ret < 0)
350 return ret;
351 if (!ret) {
352 if (!(flags & LOOKUP_RCU))
353 d_invalidate(d);
354 return -ESTALE;
355 }
356 }
357 }
358 return 1;
359}
360
361static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
362{
363 struct ovl_entry *oe = dentry->d_fsdata;
364 unsigned int i;
365 int ret = 1;
366
367 for (i = 0; i < oe->numlower; i++) {
368 struct dentry *d = oe->lowerstack[i].dentry;
369
370 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
371 ret = d->d_op->d_weak_revalidate(d, flags);
372 if (ret <= 0)
373 break;
374 }
375 }
376 return ret;
377}
378
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200379static const struct dentry_operations ovl_dentry_operations = {
380 .d_release = ovl_dentry_release,
David Howells4bacc9c2015-06-18 14:32:31 +0100381 .d_select_inode = ovl_d_select_inode,
Miklos Szeredid101a122016-03-26 16:14:37 -0400382 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200383};
384
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200385static const struct dentry_operations ovl_reval_dentry_operations = {
386 .d_release = ovl_dentry_release,
Konstantin Khlebnikovb5891cf2016-01-31 16:22:16 +0300387 .d_select_inode = ovl_d_select_inode,
Miklos Szeredid101a122016-03-26 16:14:37 -0400388 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200389 .d_revalidate = ovl_dentry_revalidate,
390 .d_weak_revalidate = ovl_dentry_weak_revalidate,
391};
392
Miklos Szeredidd662662014-12-13 00:59:43 +0100393static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200394{
Miklos Szeredidd662662014-12-13 00:59:43 +0100395 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
396 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
397
398 if (oe)
399 oe->numlower = numlower;
400
401 return oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200402}
403
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200404static bool ovl_dentry_remote(struct dentry *dentry)
405{
406 return dentry->d_flags &
407 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
408}
409
410static bool ovl_dentry_weird(struct dentry *dentry)
411{
412 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
413 DCACHE_MANAGE_TRANSIT |
414 DCACHE_OP_HASH |
415 DCACHE_OP_COMPARE);
416}
417
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200418static inline struct dentry *ovl_lookup_real(struct dentry *dir,
419 struct qstr *name)
420{
421 struct dentry *dentry;
422
Miklos Szeredi38b78a52016-05-11 01:16:37 +0200423 dentry = lookup_hash(name, dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200424
425 if (IS_ERR(dentry)) {
426 if (PTR_ERR(dentry) == -ENOENT)
427 dentry = NULL;
428 } else if (!dentry->d_inode) {
429 dput(dentry);
430 dentry = NULL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200431 } else if (ovl_dentry_weird(dentry)) {
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200432 dput(dentry);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200433 /* Don't support traversing automounts and other weirdness */
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200434 dentry = ERR_PTR(-EREMOTE);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200435 }
436 return dentry;
437}
438
Miklos Szeredi5ef88da2014-12-13 00:59:43 +0100439/*
440 * Returns next layer in stack starting from top.
441 * Returns -1 if this is the last layer.
442 */
443int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
444{
445 struct ovl_entry *oe = dentry->d_fsdata;
446
447 BUG_ON(idx < 0);
448 if (idx == 0) {
449 ovl_path_upper(dentry, path);
450 if (path->dentry)
451 return oe->numlower ? 1 : -1;
452 idx++;
453 }
454 BUG_ON(idx > oe->numlower);
455 *path = oe->lowerstack[idx - 1];
456
457 return (idx < oe->numlower) ? idx + 1 : -1;
458}
459
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200460struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
461 unsigned int flags)
462{
463 struct ovl_entry *oe;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100464 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
465 struct path *stack = NULL;
466 struct dentry *upperdir, *upperdentry = NULL;
467 unsigned int ctr = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200468 struct inode *inode = NULL;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100469 bool upperopaque = false;
470 struct dentry *this, *prev = NULL;
471 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200472 int err;
473
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100474 upperdir = ovl_upperdentry_dereference(poe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200475 if (upperdir) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100476 this = ovl_lookup_real(upperdir, &dentry->d_name);
477 err = PTR_ERR(this);
478 if (IS_ERR(this))
479 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100481 if (this) {
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200482 if (unlikely(ovl_dentry_remote(this))) {
483 dput(this);
484 err = -EREMOTE;
485 goto out;
486 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100487 if (ovl_is_whiteout(this)) {
488 dput(this);
489 this = NULL;
490 upperopaque = true;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100491 } else if (poe->numlower && ovl_is_opaquedir(this)) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100492 upperopaque = true;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200493 }
494 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100495 upperdentry = prev = this;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200496 }
497
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100498 if (!upperopaque && poe->numlower) {
499 err = -ENOMEM;
500 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
501 if (!stack)
502 goto out_put_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200503 }
504
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100505 for (i = 0; !upperopaque && i < poe->numlower; i++) {
506 bool opaque = false;
507 struct path lowerpath = poe->lowerstack[i];
508
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100509 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
510 err = PTR_ERR(this);
Miklos Szeredi09e10322014-12-13 00:59:45 +0100511 if (IS_ERR(this)) {
512 /*
513 * If it's positive, then treat ENAMETOOLONG as ENOENT.
514 */
515 if (err == -ENAMETOOLONG && (upperdentry || ctr))
516 continue;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100517 goto out_put;
Miklos Szeredi09e10322014-12-13 00:59:45 +0100518 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100519 if (!this)
520 continue;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100521 if (ovl_is_whiteout(this)) {
522 dput(this);
523 break;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100524 }
525 /*
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100526 * Only makes sense to check opaque dir if this is not the
527 * lowermost layer.
528 */
529 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
530 opaque = true;
hujianyanga425c032015-01-06 16:10:01 +0800531
532 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
533 !S_ISDIR(this->d_inode->i_mode))) {
534 /*
535 * FIXME: check for upper-opaqueness maybe better done
536 * in remove code.
537 */
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100538 if (prev == upperdentry)
539 upperopaque = true;
540 dput(this);
541 break;
542 }
hujianyanga425c032015-01-06 16:10:01 +0800543 /*
544 * If this is a non-directory then stop here.
545 */
546 if (!S_ISDIR(this->d_inode->i_mode))
547 opaque = true;
548
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100549 stack[ctr].dentry = this;
550 stack[ctr].mnt = lowerpath.mnt;
551 ctr++;
552 prev = this;
553 if (opaque)
554 break;
555 }
556
557 oe = ovl_alloc_entry(ctr);
558 err = -ENOMEM;
559 if (!oe)
560 goto out_put;
561
562 if (upperdentry || ctr) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200563 struct dentry *realdentry;
564
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100565 realdentry = upperdentry ? upperdentry : stack[0].dentry;
566
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200567 err = -ENOMEM;
568 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
569 oe);
570 if (!inode)
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100571 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200572 ovl_copyattr(realdentry->d_inode, inode);
573 }
574
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100575 oe->opaque = upperopaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200576 oe->__upperdentry = upperdentry;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100577 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
578 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200579 dentry->d_fsdata = oe;
580 d_add(dentry, inode);
581
582 return NULL;
583
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100584out_free_oe:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200585 kfree(oe);
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100586out_put:
587 for (i = 0; i < ctr; i++)
588 dput(stack[i].dentry);
589 kfree(stack);
590out_put_upper:
591 dput(upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200592out:
593 return ERR_PTR(err);
594}
595
596struct file *ovl_path_open(struct path *path, int flags)
597{
598 return dentry_open(path, flags, current_cred());
599}
600
601static void ovl_put_super(struct super_block *sb)
602{
603 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100604 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200605
606 dput(ufs->workdir);
607 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100608 for (i = 0; i < ufs->numlower; i++)
609 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300610 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200611
Erez Zadokf45827e82014-10-24 00:14:38 +0200612 kfree(ufs->config.lowerdir);
613 kfree(ufs->config.upperdir);
614 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200615 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200616 kfree(ufs);
617}
618
Andy Whitcroftcc259632014-10-24 00:14:38 +0200619/**
620 * ovl_statfs
621 * @sb: The overlayfs super block
622 * @buf: The struct kstatfs to fill in with stats
623 *
624 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100625 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200626 */
627static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
628{
629 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
630 struct dentry *root_dentry = dentry->d_sb->s_root;
631 struct path path;
632 int err;
633
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100634 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200635
636 err = vfs_statfs(&path, buf);
637 if (!err) {
638 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
639 buf->f_type = OVERLAYFS_SUPER_MAGIC;
640 }
641
642 return err;
643}
644
Erez Zadokf45827e82014-10-24 00:14:38 +0200645/**
646 * ovl_show_options
647 *
648 * Prints the mount options for a given superblock.
649 * Returns zero; does not fail.
650 */
651static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
652{
653 struct super_block *sb = dentry->d_sb;
654 struct ovl_fs *ufs = sb->s_fs_info;
655
Kees Cooka068acf2015-09-04 15:44:57 -0700656 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100657 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700658 seq_show_option(m, "upperdir", ufs->config.upperdir);
659 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100660 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200661 if (ufs->config.default_permissions)
662 seq_puts(m, ",default_permissions");
Erez Zadokf45827e82014-10-24 00:14:38 +0200663 return 0;
664}
665
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900666static int ovl_remount(struct super_block *sb, int *flags, char *data)
667{
668 struct ovl_fs *ufs = sb->s_fs_info;
669
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200670 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900671 return -EROFS;
672
673 return 0;
674}
675
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200676static const struct super_operations ovl_super_operations = {
677 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200678 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200679 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900680 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200681};
682
683enum {
684 OPT_LOWERDIR,
685 OPT_UPPERDIR,
686 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200687 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200688 OPT_ERR,
689};
690
691static const match_table_t ovl_tokens = {
692 {OPT_LOWERDIR, "lowerdir=%s"},
693 {OPT_UPPERDIR, "upperdir=%s"},
694 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200695 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200696 {OPT_ERR, NULL}
697};
698
Miklos Szeredi91c77942014-11-20 16:40:00 +0100699static char *ovl_next_opt(char **s)
700{
701 char *sbegin = *s;
702 char *p;
703
704 if (sbegin == NULL)
705 return NULL;
706
707 for (p = sbegin; *p; p++) {
708 if (*p == '\\') {
709 p++;
710 if (!*p)
711 break;
712 } else if (*p == ',') {
713 *p = '\0';
714 *s = p + 1;
715 return sbegin;
716 }
717 }
718 *s = NULL;
719 return sbegin;
720}
721
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200722static int ovl_parse_opt(char *opt, struct ovl_config *config)
723{
724 char *p;
725
Miklos Szeredi91c77942014-11-20 16:40:00 +0100726 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200727 int token;
728 substring_t args[MAX_OPT_ARGS];
729
730 if (!*p)
731 continue;
732
733 token = match_token(p, ovl_tokens, args);
734 switch (token) {
735 case OPT_UPPERDIR:
736 kfree(config->upperdir);
737 config->upperdir = match_strdup(&args[0]);
738 if (!config->upperdir)
739 return -ENOMEM;
740 break;
741
742 case OPT_LOWERDIR:
743 kfree(config->lowerdir);
744 config->lowerdir = match_strdup(&args[0]);
745 if (!config->lowerdir)
746 return -ENOMEM;
747 break;
748
749 case OPT_WORKDIR:
750 kfree(config->workdir);
751 config->workdir = match_strdup(&args[0]);
752 if (!config->workdir)
753 return -ENOMEM;
754 break;
755
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200756 case OPT_DEFAULT_PERMISSIONS:
757 config->default_permissions = true;
758 break;
759
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200760 default:
hujianyangbead55e2015-01-15 13:17:36 +0800761 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200762 return -EINVAL;
763 }
764 }
hujianyang71cbad72015-01-15 13:20:57 +0800765
766 /* Workdir is useless in non-upper mount */
767 if (!config->upperdir && config->workdir) {
768 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
769 config->workdir);
770 kfree(config->workdir);
771 config->workdir = NULL;
772 }
773
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200774 return 0;
775}
776
777#define OVL_WORKDIR_NAME "work"
778
779static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
780 struct dentry *dentry)
781{
782 struct inode *dir = dentry->d_inode;
783 struct dentry *work;
784 int err;
785 bool retried = false;
786
787 err = mnt_want_write(mnt);
788 if (err)
789 return ERR_PTR(err);
790
Al Viro59551022016-01-22 15:40:57 -0500791 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200792retry:
793 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
794 strlen(OVL_WORKDIR_NAME));
795
796 if (!IS_ERR(work)) {
797 struct kstat stat = {
798 .mode = S_IFDIR | 0,
799 };
800
801 if (work->d_inode) {
802 err = -EEXIST;
803 if (retried)
804 goto out_dput;
805
806 retried = true;
807 ovl_cleanup(dir, work);
808 dput(work);
809 goto retry;
810 }
811
812 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
813 if (err)
814 goto out_dput;
815 }
816out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500817 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200818 mnt_drop_write(mnt);
819
820 return work;
821
822out_dput:
823 dput(work);
824 work = ERR_PTR(err);
825 goto out_unlock;
826}
827
Miklos Szeredi91c77942014-11-20 16:40:00 +0100828static void ovl_unescape(char *s)
829{
830 char *d = s;
831
832 for (;; s++, d++) {
833 if (*s == '\\')
834 s++;
835 *d = *s;
836 if (!*s)
837 break;
838 }
839}
840
Miklos Szerediab508822014-12-13 00:59:49 +0100841static int ovl_mount_dir_noesc(const char *name, struct path *path)
842{
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100843 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100844
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100845 if (!*name) {
846 pr_err("overlayfs: empty lowerdir\n");
847 goto out;
848 }
Miklos Szerediab508822014-12-13 00:59:49 +0100849 err = kern_path(name, LOOKUP_FOLLOW, path);
850 if (err) {
851 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
852 goto out;
853 }
854 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200855 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100856 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
857 goto out_put;
858 }
859 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
860 pr_err("overlayfs: '%s' not a directory\n", name);
861 goto out_put;
862 }
863 return 0;
864
865out_put:
866 path_put(path);
867out:
868 return err;
869}
870
871static int ovl_mount_dir(const char *name, struct path *path)
872{
873 int err = -ENOMEM;
874 char *tmp = kstrdup(name, GFP_KERNEL);
875
876 if (tmp) {
877 ovl_unescape(tmp);
878 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200879
880 if (!err)
881 if (ovl_dentry_remote(path->dentry)) {
882 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
883 tmp);
884 path_put(path);
885 err = -EINVAL;
886 }
Miklos Szerediab508822014-12-13 00:59:49 +0100887 kfree(tmp);
888 }
889 return err;
890}
891
892static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200893 int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100894{
895 int err;
896 struct kstatfs statfs;
897
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100898 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100899 if (err)
900 goto out;
901
902 err = vfs_statfs(path, &statfs);
903 if (err) {
904 pr_err("overlayfs: statfs failed on '%s'\n", name);
905 goto out_put;
906 }
907 *namelen = max(*namelen, statfs.f_namelen);
908 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
909
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200910 if (ovl_dentry_remote(path->dentry))
911 *remote = true;
912
Miklos Szerediab508822014-12-13 00:59:49 +0100913 return 0;
914
915out_put:
916 path_put(path);
917out:
918 return err;
919}
920
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200921/* Workdir should not be subdir of upperdir and vice versa */
922static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
923{
924 bool ok = false;
925
926 if (workdir != upperdir) {
927 ok = (lock_rename(workdir, upperdir) == NULL);
928 unlock_rename(workdir, upperdir);
929 }
930 return ok;
931}
932
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100933static unsigned int ovl_split_lowerdirs(char *str)
934{
935 unsigned int ctr = 1;
936 char *s, *d;
937
938 for (s = d = str;; s++, d++) {
939 if (*s == '\\') {
940 s++;
941 } else if (*s == ':') {
942 *d = '\0';
943 ctr++;
944 continue;
945 }
946 *d = *s;
947 if (!*s)
948 break;
949 }
950 return ctr;
951}
952
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200953static int ovl_fill_super(struct super_block *sb, void *data, int silent)
954{
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100955 struct path upperpath = { NULL, NULL };
956 struct path workpath = { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200957 struct dentry *root_dentry;
958 struct ovl_entry *oe;
959 struct ovl_fs *ufs;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100960 struct path *stack = NULL;
961 char *lowertmp;
962 char *lower;
963 unsigned int numlower;
964 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100965 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200966 bool remote = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200967 int err;
968
Erez Zadokf45827e82014-10-24 00:14:38 +0200969 err = -ENOMEM;
970 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
971 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200972 goto out;
973
Erez Zadokf45827e82014-10-24 00:14:38 +0200974 err = ovl_parse_opt((char *) data, &ufs->config);
975 if (err)
976 goto out_free_config;
977
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200978 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100979 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300980 if (!silent)
981 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200982 goto out_free_config;
983 }
984
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100985 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +0100986 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100987 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100988 if (!ufs->config.workdir) {
989 pr_err("overlayfs: missing 'workdir'\n");
990 goto out_free_config;
991 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200992
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100993 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
994 if (err)
995 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100996
hujianyang71cbad72015-01-15 13:20:57 +0800997 /* Upper fs should not be r/o */
998 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
999 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1000 err = -EINVAL;
1001 goto out_put_upperpath;
1002 }
1003
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001004 err = ovl_mount_dir(ufs->config.workdir, &workpath);
1005 if (err)
1006 goto out_put_upperpath;
1007
hujianyang2f83fd82015-01-06 12:52:13 +08001008 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001009 if (upperpath.mnt != workpath.mnt) {
1010 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1011 goto out_put_workpath;
1012 }
1013 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
1014 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1015 goto out_put_workpath;
1016 }
1017 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +01001018 }
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001019 err = -ENOMEM;
1020 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1021 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001022 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001023
1024 err = -EINVAL;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001025 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +08001026 if (stacklen > OVL_MAX_STACK) {
1027 pr_err("overlayfs: too many lower directries, limit is %d\n",
1028 OVL_MAX_STACK);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001029 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +08001030 } else if (!ufs->config.upperdir && stacklen == 1) {
1031 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1032 goto out_free_lowertmp;
1033 }
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001034
1035 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1036 if (!stack)
1037 goto out_free_lowertmp;
1038
1039 lower = lowertmp;
1040 for (numlower = 0; numlower < stacklen; numlower++) {
1041 err = ovl_lower_dir(lower, &stack[numlower],
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001042 &ufs->lower_namelen, &sb->s_stack_depth,
1043 &remote);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001044 if (err)
1045 goto out_put_lowerpath;
1046
1047 lower = strchr(lower, '\0') + 1;
1048 }
1049
1050 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +01001051 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +02001052 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1053 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001054 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +02001055 }
1056
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001057 if (ufs->config.upperdir) {
1058 ufs->upper_mnt = clone_private_mount(&upperpath);
1059 err = PTR_ERR(ufs->upper_mnt);
1060 if (IS_ERR(ufs->upper_mnt)) {
1061 pr_err("overlayfs: failed to clone upperpath\n");
1062 goto out_put_lowerpath;
1063 }
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001064
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001065 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1066 err = PTR_ERR(ufs->workdir);
1067 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +02001068 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1069 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1070 sb->s_flags |= MS_RDONLY;
1071 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001072 }
Vivek Goyal45aebea2016-02-22 09:28:34 -05001073
1074 /*
1075 * Upper should support d_type, else whiteouts are visible.
1076 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -04001077 * iterate_dir() on workdir. This check requires successful
1078 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -05001079 */
Vivek Goyal21765192016-05-20 09:04:26 -04001080 if (ufs->workdir) {
1081 err = ovl_check_d_type_supported(&workpath);
1082 if (err < 0)
1083 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -05001084
Vivek Goyale7c0b592016-07-01 10:02:44 -04001085 /*
1086 * We allowed this configuration and don't want to
1087 * break users over kernel upgrade. So warn instead
1088 * of erroring out.
1089 */
1090 if (!err)
1091 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Vivek Goyal45aebea2016-02-22 09:28:34 -05001092 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001093 }
1094
hujianyang2f83fd82015-01-06 12:52:13 +08001095 err = -ENOMEM;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001096 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +01001097 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001098 goto out_put_workdir;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001099 for (i = 0; i < numlower; i++) {
1100 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +01001101
hujianyang2f83fd82015-01-06 12:52:13 +08001102 err = PTR_ERR(mnt);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001103 if (IS_ERR(mnt)) {
1104 pr_err("overlayfs: failed to clone lowerpath\n");
1105 goto out_put_lower_mnt;
1106 }
1107 /*
1108 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1109 * will fail instead of modifying lower fs.
1110 */
1111 mnt->mnt_flags |= MNT_READONLY;
1112
1113 ufs->lower_mnt[ufs->numlower] = mnt;
1114 ufs->numlower++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001115 }
1116
hujianyang71cbad72015-01-15 13:20:57 +08001117 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1118 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001119 sb->s_flags |= MS_RDONLY;
1120
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001121 if (remote)
1122 sb->s_d_op = &ovl_reval_dentry_operations;
1123 else
1124 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001125
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001126 ufs->creator_cred = prepare_creds();
1127 if (!ufs->creator_cred)
1128 goto out_put_lower_mnt;
1129
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001130 err = -ENOMEM;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001131 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001132 if (!oe)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001133 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001134
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001135 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001136 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001137 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001138
1139 mntput(upperpath.mnt);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001140 for (i = 0; i < numlower; i++)
1141 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001142 path_put(&workpath);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001143 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001144
1145 oe->__upperdentry = upperpath.dentry;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001146 for (i = 0; i < numlower; i++) {
1147 oe->lowerstack[i].dentry = stack[i].dentry;
1148 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1149 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001150 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001151
1152 root_dentry->d_fsdata = oe;
1153
Miklos Szeredied06e062015-12-09 16:11:59 +01001154 ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode,
1155 root_dentry->d_inode);
1156
Andy Whitcroftcc259632014-10-24 00:14:38 +02001157 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001158 sb->s_op = &ovl_super_operations;
1159 sb->s_root = root_dentry;
1160 sb->s_fs_info = ufs;
1161
1162 return 0;
1163
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001164out_free_oe:
1165 kfree(oe);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001166out_put_cred:
1167 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001168out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001169 for (i = 0; i < ufs->numlower; i++)
1170 mntput(ufs->lower_mnt[i]);
1171 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001172out_put_workdir:
1173 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001174 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001175out_put_lowerpath:
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +01001176 for (i = 0; i < numlower; i++)
1177 path_put(&stack[i]);
1178 kfree(stack);
1179out_free_lowertmp:
1180 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001181out_put_workpath:
1182 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001183out_put_upperpath:
1184 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001185out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001186 kfree(ufs->config.lowerdir);
1187 kfree(ufs->config.upperdir);
1188 kfree(ufs->config.workdir);
1189 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001190out:
1191 return err;
1192}
1193
1194static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1195 const char *dev_name, void *raw_data)
1196{
1197 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1198}
1199
1200static struct file_system_type ovl_fs_type = {
1201 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001202 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001203 .mount = ovl_mount,
1204 .kill_sb = kill_anon_super,
1205};
Miklos Szeredief94b182014-11-20 16:39:59 +01001206MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001207
1208static int __init ovl_init(void)
1209{
1210 return register_filesystem(&ovl_fs_type);
1211}
1212
1213static void __exit ovl_exit(void)
1214{
1215 unregister_filesystem(&ovl_fs_type);
1216}
1217
1218module_init(ovl_init);
1219module_exit(ovl_exit);