blob: 44c4510f5adf6cde4e021d15ae08163fed348e37 [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 Szeredia78d9f02014-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
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200148static void ovl_inode_init(struct inode *inode, struct inode *realinode,
149 bool is_upper)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200150{
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200151 WRITE_ONCE(inode->i_private, (unsigned long) realinode |
152 (is_upper ? OVL_ISUPPER_MASK : 0));
Vivek Goyal39a25b22016-07-01 16:34:26 -0400153}
154
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200155struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode,
156 bool is_upper)
157{
158 if (is_upper) {
159 struct ovl_fs *ofs = inode->i_sb->s_fs_info;
160
161 return ofs->upper_mnt;
162 } else {
163 return oe->numlower ? oe->lowerstack[0].mnt : NULL;
164 }
165}
166
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200167struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
168{
169 struct ovl_entry *oe = dentry->d_fsdata;
170
171 return oe->cache;
172}
173
174void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
175{
176 struct ovl_entry *oe = dentry->d_fsdata;
177
178 oe->cache = cache;
179}
180
181void ovl_path_lower(struct dentry *dentry, struct path *path)
182{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200183 struct ovl_entry *oe = dentry->d_fsdata;
184
Miklos Szeredidd662662014-12-13 00:59:43 +0100185 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200186}
187
188int ovl_want_write(struct dentry *dentry)
189{
190 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
191 return mnt_want_write(ofs->upper_mnt);
192}
193
194void ovl_drop_write(struct dentry *dentry)
195{
196 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
197 mnt_drop_write(ofs->upper_mnt);
198}
199
200struct dentry *ovl_workdir(struct dentry *dentry)
201{
202 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
203 return ofs->workdir;
204}
205
206bool ovl_dentry_is_opaque(struct dentry *dentry)
207{
208 struct ovl_entry *oe = dentry->d_fsdata;
209 return oe->opaque;
210}
211
212void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
213{
214 struct ovl_entry *oe = dentry->d_fsdata;
215 oe->opaque = opaque;
216}
217
218void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
219{
220 struct ovl_entry *oe = dentry->d_fsdata;
221
Al Viro59551022016-01-22 15:40:57 -0500222 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200223 WARN_ON(oe->__upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200224 /*
225 * Make sure upperdentry is consistent before making it visible to
226 * ovl_upperdentry_dereference().
227 */
228 smp_wmb();
229 oe->__upperdentry = upperdentry;
230}
231
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200232void ovl_inode_update(struct inode *inode, struct inode *upperinode)
233{
234 WARN_ON(!upperinode);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200235 WARN_ON(!inode_unhashed(inode));
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200236 WRITE_ONCE(inode->i_private,
237 (unsigned long) upperinode | OVL_ISUPPER_MASK);
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200238 if (!S_ISDIR(upperinode->i_mode))
239 __insert_inode_hash(inode, (unsigned long) upperinode);
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200240}
241
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200242void ovl_dentry_version_inc(struct dentry *dentry)
243{
244 struct ovl_entry *oe = dentry->d_fsdata;
245
Al Viro59551022016-01-22 15:40:57 -0500246 WARN_ON(!inode_is_locked(dentry->d_inode));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200247 oe->version++;
248}
249
250u64 ovl_dentry_version_get(struct dentry *dentry)
251{
252 struct ovl_entry *oe = dentry->d_fsdata;
253
Al Viro59551022016-01-22 15:40:57 -0500254 WARN_ON(!inode_is_locked(dentry->d_inode));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200255 return oe->version;
256}
257
258bool ovl_is_whiteout(struct dentry *dentry)
259{
260 struct inode *inode = dentry->d_inode;
261
262 return inode && IS_WHITEOUT(inode);
263}
264
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200265const struct cred *ovl_override_creds(struct super_block *sb)
266{
267 struct ovl_fs *ofs = sb->s_fs_info;
268
269 return override_creds(ofs->creator_cred);
270}
271
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200272static bool ovl_is_opaquedir(struct dentry *dentry)
273{
274 int res;
275 char val;
276 struct inode *inode = dentry->d_inode;
277
278 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
279 return false;
280
Al Viroce23e642016-04-11 00:48:00 -0400281 res = inode->i_op->getxattr(dentry, inode, OVL_XATTR_OPAQUE, &val, 1);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200282 if (res == 1 && val == 'y')
283 return true;
284
285 return false;
286}
287
288static void ovl_dentry_release(struct dentry *dentry)
289{
290 struct ovl_entry *oe = dentry->d_fsdata;
291
292 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +0100293 unsigned int i;
294
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200295 dput(oe->__upperdentry);
Miklos Szeredidd662662014-12-13 00:59:43 +0100296 for (i = 0; i < oe->numlower; i++)
297 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200298 kfree_rcu(oe, rcu);
299 }
300}
301
Miklos Szeredi2d902672016-06-30 08:53:27 +0200302static struct dentry *ovl_d_real(struct dentry *dentry,
303 const struct inode *inode,
304 unsigned int open_flags)
Miklos Szeredid101a122016-03-26 16:14:37 -0400305{
306 struct dentry *real;
307
308 if (d_is_dir(dentry)) {
309 if (!inode || inode == d_inode(dentry))
310 return dentry;
311 goto bug;
312 }
313
Miklos Szeredi2d902672016-06-30 08:53:27 +0200314 if (d_is_negative(dentry))
315 return dentry;
316
317 if (open_flags) {
318 int err = ovl_open_maybe_copy_up(dentry, open_flags);
319
320 if (err)
321 return ERR_PTR(err);
322 }
323
Miklos Szeredid101a122016-03-26 16:14:37 -0400324 real = ovl_dentry_upper(dentry);
325 if (real && (!inode || inode == d_inode(real)))
326 return real;
327
328 real = ovl_dentry_lower(dentry);
329 if (!real)
330 goto bug;
331
332 if (!inode || inode == d_inode(real))
333 return real;
334
335 /* Handle recursion */
Miklos Szeredi2d902672016-06-30 08:53:27 +0200336 return d_real(real, inode, open_flags);
Miklos Szeredid101a122016-03-26 16:14:37 -0400337bug:
338 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry,
339 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
340 return dentry;
341}
342
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200343static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
344{
345 struct ovl_entry *oe = dentry->d_fsdata;
346 unsigned int i;
347 int ret = 1;
348
349 for (i = 0; i < oe->numlower; i++) {
350 struct dentry *d = oe->lowerstack[i].dentry;
351
352 if (d->d_flags & DCACHE_OP_REVALIDATE) {
353 ret = d->d_op->d_revalidate(d, flags);
354 if (ret < 0)
355 return ret;
356 if (!ret) {
357 if (!(flags & LOOKUP_RCU))
358 d_invalidate(d);
359 return -ESTALE;
360 }
361 }
362 }
363 return 1;
364}
365
366static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
367{
368 struct ovl_entry *oe = dentry->d_fsdata;
369 unsigned int i;
370 int ret = 1;
371
372 for (i = 0; i < oe->numlower; i++) {
373 struct dentry *d = oe->lowerstack[i].dentry;
374
375 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
376 ret = d->d_op->d_weak_revalidate(d, flags);
377 if (ret <= 0)
378 break;
379 }
380 }
381 return ret;
382}
383
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200384static const struct dentry_operations ovl_dentry_operations = {
385 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400386 .d_real = ovl_d_real,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200387};
388
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200389static const struct dentry_operations ovl_reval_dentry_operations = {
390 .d_release = ovl_dentry_release,
Miklos Szeredid101a122016-03-26 16:14:37 -0400391 .d_real = ovl_d_real,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200392 .d_revalidate = ovl_dentry_revalidate,
393 .d_weak_revalidate = ovl_dentry_weak_revalidate,
394};
395
Miklos Szeredidd662662014-12-13 00:59:43 +0100396static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200397{
Miklos Szeredidd662662014-12-13 00:59:43 +0100398 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
399 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
400
401 if (oe)
402 oe->numlower = numlower;
403
404 return oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200405}
406
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200407static bool ovl_dentry_remote(struct dentry *dentry)
408{
409 return dentry->d_flags &
410 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
411}
412
413static bool ovl_dentry_weird(struct dentry *dentry)
414{
415 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
416 DCACHE_MANAGE_TRANSIT |
417 DCACHE_OP_HASH |
418 DCACHE_OP_COMPARE);
419}
420
Miklos Szeredic1b2cc12016-07-29 12:05:22 +0200421static inline struct dentry *ovl_lookup_real(struct super_block *ovl_sb,
422 struct dentry *dir,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200423 struct qstr *name)
424{
Miklos Szeredic1b2cc12016-07-29 12:05:22 +0200425 const struct cred *old_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200426 struct dentry *dentry;
427
Miklos Szeredic1b2cc12016-07-29 12:05:22 +0200428 old_cred = ovl_override_creds(ovl_sb);
429 dentry = lookup_one_len_unlocked(name->name, dir, name->len);
430 revert_creds(old_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200431
432 if (IS_ERR(dentry)) {
433 if (PTR_ERR(dentry) == -ENOENT)
434 dentry = NULL;
435 } else if (!dentry->d_inode) {
436 dput(dentry);
437 dentry = NULL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200438 } else if (ovl_dentry_weird(dentry)) {
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200439 dput(dentry);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200440 /* Don't support traversing automounts and other weirdness */
Miklos Szeredia6f15d92015-06-22 13:53:48 +0200441 dentry = ERR_PTR(-EREMOTE);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200442 }
443 return dentry;
444}
445
Miklos Szeredi5ef88da2014-12-13 00:59:43 +0100446/*
447 * Returns next layer in stack starting from top.
448 * Returns -1 if this is the last layer.
449 */
450int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
451{
452 struct ovl_entry *oe = dentry->d_fsdata;
453
454 BUG_ON(idx < 0);
455 if (idx == 0) {
456 ovl_path_upper(dentry, path);
457 if (path->dentry)
458 return oe->numlower ? 1 : -1;
459 idx++;
460 }
461 BUG_ON(idx > oe->numlower);
462 *path = oe->lowerstack[idx - 1];
463
464 return (idx < oe->numlower) ? idx + 1 : -1;
465}
466
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200467struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
468 unsigned int flags)
469{
470 struct ovl_entry *oe;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100471 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
472 struct path *stack = NULL;
473 struct dentry *upperdir, *upperdentry = NULL;
474 unsigned int ctr = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200475 struct inode *inode = NULL;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100476 bool upperopaque = false;
477 struct dentry *this, *prev = NULL;
478 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200479 int err;
480
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100481 upperdir = ovl_upperdentry_dereference(poe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200482 if (upperdir) {
Miklos Szeredic1b2cc12016-07-29 12:05:22 +0200483 this = ovl_lookup_real(dentry->d_sb, upperdir, &dentry->d_name);
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100484 err = PTR_ERR(this);
485 if (IS_ERR(this))
486 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200487
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100488 if (this) {
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200489 if (unlikely(ovl_dentry_remote(this))) {
490 dput(this);
491 err = -EREMOTE;
492 goto out;
493 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100494 if (ovl_is_whiteout(this)) {
495 dput(this);
496 this = NULL;
497 upperopaque = true;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100498 } else if (poe->numlower && ovl_is_opaquedir(this)) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100499 upperopaque = true;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200500 }
501 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100502 upperdentry = prev = this;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200503 }
504
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100505 if (!upperopaque && poe->numlower) {
506 err = -ENOMEM;
507 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
508 if (!stack)
509 goto out_put_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200510 }
511
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100512 for (i = 0; !upperopaque && i < poe->numlower; i++) {
513 bool opaque = false;
514 struct path lowerpath = poe->lowerstack[i];
515
Miklos Szeredic1b2cc12016-07-29 12:05:22 +0200516 this = ovl_lookup_real(dentry->d_sb,
517 lowerpath.dentry, &dentry->d_name);
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100518 err = PTR_ERR(this);
Miklos Szeredi09e10322014-12-13 00:59:45 +0100519 if (IS_ERR(this)) {
520 /*
521 * If it's positive, then treat ENAMETOOLONG as ENOENT.
522 */
523 if (err == -ENAMETOOLONG && (upperdentry || ctr))
524 continue;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100525 goto out_put;
Miklos Szeredi09e10322014-12-13 00:59:45 +0100526 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100527 if (!this)
528 continue;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100529 if (ovl_is_whiteout(this)) {
530 dput(this);
531 break;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100532 }
533 /*
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100534 * Only makes sense to check opaque dir if this is not the
535 * lowermost layer.
536 */
537 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
538 opaque = true;
hujianyanga425c032015-01-06 16:10:01 +0800539
540 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
541 !S_ISDIR(this->d_inode->i_mode))) {
542 /*
543 * FIXME: check for upper-opaqueness maybe better done
544 * in remove code.
545 */
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100546 if (prev == upperdentry)
547 upperopaque = true;
548 dput(this);
549 break;
550 }
hujianyanga425c032015-01-06 16:10:01 +0800551 /*
552 * If this is a non-directory then stop here.
553 */
554 if (!S_ISDIR(this->d_inode->i_mode))
555 opaque = true;
556
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100557 stack[ctr].dentry = this;
558 stack[ctr].mnt = lowerpath.mnt;
559 ctr++;
560 prev = this;
561 if (opaque)
562 break;
563 }
564
565 oe = ovl_alloc_entry(ctr);
566 err = -ENOMEM;
567 if (!oe)
568 goto out_put;
569
570 if (upperdentry || ctr) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200571 struct dentry *realdentry;
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200572 struct inode *realinode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200573
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100574 realdentry = upperdentry ? upperdentry : stack[0].dentry;
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200575 realinode = d_inode(realdentry);
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100576
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200577 err = -ENOMEM;
Miklos Szeredi51f7e522016-07-29 12:05:24 +0200578 if (upperdentry && !d_is_dir(upperdentry)) {
579 inode = ovl_get_inode(dentry->d_sb, realinode);
580 } else {
581 inode = ovl_new_inode(dentry->d_sb, realinode->i_mode);
582 if (inode)
583 ovl_inode_init(inode, realinode, !!upperdentry);
584 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200585 if (!inode)
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100586 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200587 ovl_copyattr(realdentry->d_inode, inode);
588 }
589
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100590 oe->opaque = upperopaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200591 oe->__upperdentry = upperdentry;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100592 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
593 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200594 dentry->d_fsdata = oe;
595 d_add(dentry, inode);
596
597 return NULL;
598
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100599out_free_oe:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200600 kfree(oe);
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100601out_put:
602 for (i = 0; i < ctr; i++)
603 dput(stack[i].dentry);
604 kfree(stack);
605out_put_upper:
606 dput(upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200607out:
608 return ERR_PTR(err);
609}
610
611struct file *ovl_path_open(struct path *path, int flags)
612{
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200613 return dentry_open(path, flags | O_NOATIME, current_cred());
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200614}
615
616static void ovl_put_super(struct super_block *sb)
617{
618 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100619 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200620
621 dput(ufs->workdir);
622 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100623 for (i = 0; i < ufs->numlower; i++)
624 mntput(ufs->lower_mnt[i]);
Konstantin Khlebnikov5ffdbe82015-08-24 15:57:19 +0300625 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200626
Erez Zadokf45827e82014-10-24 00:14:38 +0200627 kfree(ufs->config.lowerdir);
628 kfree(ufs->config.upperdir);
629 kfree(ufs->config.workdir);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +0200630 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200631 kfree(ufs);
632}
633
Andy Whitcroftcc259632014-10-24 00:14:38 +0200634/**
635 * ovl_statfs
636 * @sb: The overlayfs super block
637 * @buf: The struct kstatfs to fill in with stats
638 *
639 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100640 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200641 */
642static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
643{
644 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
645 struct dentry *root_dentry = dentry->d_sb->s_root;
646 struct path path;
647 int err;
648
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100649 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200650
651 err = vfs_statfs(&path, buf);
652 if (!err) {
653 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
654 buf->f_type = OVERLAYFS_SUPER_MAGIC;
655 }
656
657 return err;
658}
659
Erez Zadokf45827e82014-10-24 00:14:38 +0200660/**
661 * ovl_show_options
662 *
663 * Prints the mount options for a given superblock.
664 * Returns zero; does not fail.
665 */
666static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
667{
668 struct super_block *sb = dentry->d_sb;
669 struct ovl_fs *ufs = sb->s_fs_info;
670
Kees Cooka068acf2015-09-04 15:44:57 -0700671 seq_show_option(m, "lowerdir", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100672 if (ufs->config.upperdir) {
Kees Cooka068acf2015-09-04 15:44:57 -0700673 seq_show_option(m, "upperdir", ufs->config.upperdir);
674 seq_show_option(m, "workdir", ufs->config.workdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100675 }
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200676 if (ufs->config.default_permissions)
677 seq_puts(m, ",default_permissions");
Erez Zadokf45827e82014-10-24 00:14:38 +0200678 return 0;
679}
680
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900681static int ovl_remount(struct super_block *sb, int *flags, char *data)
682{
683 struct ovl_fs *ufs = sb->s_fs_info;
684
Miklos Szeredicc6f67b2015-05-19 14:30:12 +0200685 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir))
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900686 return -EROFS;
687
688 return 0;
689}
690
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200691static const struct super_operations ovl_super_operations = {
692 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200693 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200694 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900695 .remount_fs = ovl_remount,
Miklos Szeredieead4f22016-07-29 12:05:22 +0200696 .drop_inode = generic_delete_inode,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200697};
698
699enum {
700 OPT_LOWERDIR,
701 OPT_UPPERDIR,
702 OPT_WORKDIR,
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200703 OPT_DEFAULT_PERMISSIONS,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200704 OPT_ERR,
705};
706
707static const match_table_t ovl_tokens = {
708 {OPT_LOWERDIR, "lowerdir=%s"},
709 {OPT_UPPERDIR, "upperdir=%s"},
710 {OPT_WORKDIR, "workdir=%s"},
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200711 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200712 {OPT_ERR, NULL}
713};
714
Miklos Szeredi91c77942014-11-20 16:40:00 +0100715static char *ovl_next_opt(char **s)
716{
717 char *sbegin = *s;
718 char *p;
719
720 if (sbegin == NULL)
721 return NULL;
722
723 for (p = sbegin; *p; p++) {
724 if (*p == '\\') {
725 p++;
726 if (!*p)
727 break;
728 } else if (*p == ',') {
729 *p = '\0';
730 *s = p + 1;
731 return sbegin;
732 }
733 }
734 *s = NULL;
735 return sbegin;
736}
737
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200738static int ovl_parse_opt(char *opt, struct ovl_config *config)
739{
740 char *p;
741
Miklos Szeredi91c77942014-11-20 16:40:00 +0100742 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200743 int token;
744 substring_t args[MAX_OPT_ARGS];
745
746 if (!*p)
747 continue;
748
749 token = match_token(p, ovl_tokens, args);
750 switch (token) {
751 case OPT_UPPERDIR:
752 kfree(config->upperdir);
753 config->upperdir = match_strdup(&args[0]);
754 if (!config->upperdir)
755 return -ENOMEM;
756 break;
757
758 case OPT_LOWERDIR:
759 kfree(config->lowerdir);
760 config->lowerdir = match_strdup(&args[0]);
761 if (!config->lowerdir)
762 return -ENOMEM;
763 break;
764
765 case OPT_WORKDIR:
766 kfree(config->workdir);
767 config->workdir = match_strdup(&args[0]);
768 if (!config->workdir)
769 return -ENOMEM;
770 break;
771
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200772 case OPT_DEFAULT_PERMISSIONS:
773 config->default_permissions = true;
774 break;
775
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200776 default:
hujianyangbead55e2015-01-15 13:17:36 +0800777 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200778 return -EINVAL;
779 }
780 }
hujianyang71cbad72015-01-15 13:20:57 +0800781
782 /* Workdir is useless in non-upper mount */
783 if (!config->upperdir && config->workdir) {
784 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
785 config->workdir);
786 kfree(config->workdir);
787 config->workdir = NULL;
788 }
789
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200790 return 0;
791}
792
793#define OVL_WORKDIR_NAME "work"
794
795static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
796 struct dentry *dentry)
797{
798 struct inode *dir = dentry->d_inode;
799 struct dentry *work;
800 int err;
801 bool retried = false;
802
803 err = mnt_want_write(mnt);
804 if (err)
805 return ERR_PTR(err);
806
Al Viro59551022016-01-22 15:40:57 -0500807 inode_lock_nested(dir, I_MUTEX_PARENT);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200808retry:
809 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
810 strlen(OVL_WORKDIR_NAME));
811
812 if (!IS_ERR(work)) {
813 struct kstat stat = {
814 .mode = S_IFDIR | 0,
815 };
816
817 if (work->d_inode) {
818 err = -EEXIST;
819 if (retried)
820 goto out_dput;
821
822 retried = true;
823 ovl_cleanup(dir, work);
824 dput(work);
825 goto retry;
826 }
827
828 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
829 if (err)
830 goto out_dput;
831 }
832out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500833 inode_unlock(dir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200834 mnt_drop_write(mnt);
835
836 return work;
837
838out_dput:
839 dput(work);
840 work = ERR_PTR(err);
841 goto out_unlock;
842}
843
Miklos Szeredi91c77942014-11-20 16:40:00 +0100844static void ovl_unescape(char *s)
845{
846 char *d = s;
847
848 for (;; s++, d++) {
849 if (*s == '\\')
850 s++;
851 *d = *s;
852 if (!*s)
853 break;
854 }
855}
856
Miklos Szerediab508822014-12-13 00:59:49 +0100857static int ovl_mount_dir_noesc(const char *name, struct path *path)
858{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100859 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100860
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100861 if (!*name) {
862 pr_err("overlayfs: empty lowerdir\n");
863 goto out;
864 }
Miklos Szerediab508822014-12-13 00:59:49 +0100865 err = kern_path(name, LOOKUP_FOLLOW, path);
866 if (err) {
867 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
868 goto out;
869 }
870 err = -EINVAL;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200871 if (ovl_dentry_weird(path->dentry)) {
Miklos Szerediab508822014-12-13 00:59:49 +0100872 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
873 goto out_put;
874 }
875 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
876 pr_err("overlayfs: '%s' not a directory\n", name);
877 goto out_put;
878 }
879 return 0;
880
881out_put:
882 path_put(path);
883out:
884 return err;
885}
886
887static int ovl_mount_dir(const char *name, struct path *path)
888{
889 int err = -ENOMEM;
890 char *tmp = kstrdup(name, GFP_KERNEL);
891
892 if (tmp) {
893 ovl_unescape(tmp);
894 err = ovl_mount_dir_noesc(tmp, path);
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200895
896 if (!err)
897 if (ovl_dentry_remote(path->dentry)) {
898 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
899 tmp);
900 path_put(path);
901 err = -EINVAL;
902 }
Miklos Szerediab508822014-12-13 00:59:49 +0100903 kfree(tmp);
904 }
905 return err;
906}
907
908static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200909 int *stack_depth, bool *remote)
Miklos Szerediab508822014-12-13 00:59:49 +0100910{
911 int err;
912 struct kstatfs statfs;
913
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100914 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100915 if (err)
916 goto out;
917
918 err = vfs_statfs(path, &statfs);
919 if (err) {
920 pr_err("overlayfs: statfs failed on '%s'\n", name);
921 goto out_put;
922 }
923 *namelen = max(*namelen, statfs.f_namelen);
924 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
925
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200926 if (ovl_dentry_remote(path->dentry))
927 *remote = true;
928
Miklos Szerediab508822014-12-13 00:59:49 +0100929 return 0;
930
931out_put:
932 path_put(path);
933out:
934 return err;
935}
936
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200937/* Workdir should not be subdir of upperdir and vice versa */
938static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
939{
940 bool ok = false;
941
942 if (workdir != upperdir) {
943 ok = (lock_rename(workdir, upperdir) == NULL);
944 unlock_rename(workdir, upperdir);
945 }
946 return ok;
947}
948
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100949static unsigned int ovl_split_lowerdirs(char *str)
950{
951 unsigned int ctr = 1;
952 char *s, *d;
953
954 for (s = d = str;; s++, d++) {
955 if (*s == '\\') {
956 s++;
957 } else if (*s == ':') {
958 *d = '\0';
959 ctr++;
960 continue;
961 }
962 *d = *s;
963 if (!*s)
964 break;
965 }
966 return ctr;
967}
968
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200969static int ovl_fill_super(struct super_block *sb, void *data, int silent)
970{
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100971 struct path upperpath = { NULL, NULL };
972 struct path workpath = { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200973 struct dentry *root_dentry;
Miklos Szeredi39b681f2016-07-29 12:05:24 +0200974 struct inode *realinode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200975 struct ovl_entry *oe;
976 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100977 struct path *stack = NULL;
978 char *lowertmp;
979 char *lower;
980 unsigned int numlower;
981 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100982 unsigned int i;
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +0200983 bool remote = false;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200984 int err;
985
Erez Zadokf45827e82014-10-24 00:14:38 +0200986 err = -ENOMEM;
987 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
988 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200989 goto out;
990
Erez Zadokf45827e82014-10-24 00:14:38 +0200991 err = ovl_parse_opt((char *) data, &ufs->config);
992 if (err)
993 goto out_free_config;
994
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200995 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100996 if (!ufs->config.lowerdir) {
Konstantin Khlebnikov07f2af72015-06-29 20:18:56 +0300997 if (!silent)
998 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200999 goto out_free_config;
1000 }
1001
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001002 sb->s_stack_depth = 0;
Miklos Szeredicf9a6782015-12-11 16:30:49 +01001003 sb->s_maxbytes = MAX_LFS_FILESIZE;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001004 if (ufs->config.upperdir) {
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001005 if (!ufs->config.workdir) {
1006 pr_err("overlayfs: missing 'workdir'\n");
1007 goto out_free_config;
1008 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001009
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001010 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
1011 if (err)
1012 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001013
hujianyang71cbad72015-01-15 13:20:57 +08001014 /* Upper fs should not be r/o */
1015 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) {
1016 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
1017 err = -EINVAL;
1018 goto out_put_upperpath;
1019 }
1020
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001021 err = ovl_mount_dir(ufs->config.workdir, &workpath);
1022 if (err)
1023 goto out_put_upperpath;
1024
hujianyang2f83fd82015-01-06 12:52:13 +08001025 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001026 if (upperpath.mnt != workpath.mnt) {
1027 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1028 goto out_put_workpath;
1029 }
1030 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
1031 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1032 goto out_put_workpath;
1033 }
1034 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +01001035 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001036 err = -ENOMEM;
1037 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
1038 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001039 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001040
1041 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001042 stacklen = ovl_split_lowerdirs(lowertmp);
hujianyang6be45062015-01-15 13:19:21 +08001043 if (stacklen > OVL_MAX_STACK) {
1044 pr_err("overlayfs: too many lower directries, limit is %d\n",
1045 OVL_MAX_STACK);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001046 goto out_free_lowertmp;
hujianyang6be45062015-01-15 13:19:21 +08001047 } else if (!ufs->config.upperdir && stacklen == 1) {
1048 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1049 goto out_free_lowertmp;
1050 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001051
1052 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1053 if (!stack)
1054 goto out_free_lowertmp;
1055
1056 lower = lowertmp;
1057 for (numlower = 0; numlower < stacklen; numlower++) {
1058 err = ovl_lower_dir(lower, &stack[numlower],
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001059 &ufs->lower_namelen, &sb->s_stack_depth,
1060 &remote);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001061 if (err)
1062 goto out_put_lowerpath;
1063
1064 lower = strchr(lower, '\0') + 1;
1065 }
1066
1067 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +01001068 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +02001069 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1070 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001071 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +02001072 }
1073
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001074 if (ufs->config.upperdir) {
1075 ufs->upper_mnt = clone_private_mount(&upperpath);
1076 err = PTR_ERR(ufs->upper_mnt);
1077 if (IS_ERR(ufs->upper_mnt)) {
1078 pr_err("overlayfs: failed to clone upperpath\n");
1079 goto out_put_lowerpath;
1080 }
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001081 /* Don't inherit atime flags */
1082 ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1083
1084 sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001085
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001086 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
1087 err = PTR_ERR(ufs->workdir);
1088 if (IS_ERR(ufs->workdir)) {
Miklos Szeredicc6f67b2015-05-19 14:30:12 +02001089 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
1090 ufs->config.workdir, OVL_WORKDIR_NAME, -err);
1091 sb->s_flags |= MS_RDONLY;
1092 ufs->workdir = NULL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +01001093 }
Vivek Goyal45aebea2016-02-22 09:28:34 -05001094
1095 /*
1096 * Upper should support d_type, else whiteouts are visible.
1097 * Given workdir and upper are on same fs, we can do
Vivek Goyal21765192016-05-20 09:04:26 -04001098 * iterate_dir() on workdir. This check requires successful
1099 * creation of workdir in previous step.
Vivek Goyal45aebea2016-02-22 09:28:34 -05001100 */
Vivek Goyal21765192016-05-20 09:04:26 -04001101 if (ufs->workdir) {
1102 err = ovl_check_d_type_supported(&workpath);
1103 if (err < 0)
1104 goto out_put_workdir;
Vivek Goyal45aebea2016-02-22 09:28:34 -05001105
Vivek Goyale7c0b592016-07-01 10:02:44 -04001106 /*
1107 * We allowed this configuration and don't want to
1108 * break users over kernel upgrade. So warn instead
1109 * of erroring out.
1110 */
1111 if (!err)
1112 pr_warn("overlayfs: upper fs needs to support d_type.\n");
Vivek Goyal45aebea2016-02-22 09:28:34 -05001113 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001114 }
1115
hujianyang2f83fd82015-01-06 12:52:13 +08001116 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001117 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +01001118 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001119 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001120 for (i = 0; i < numlower; i++) {
1121 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +01001122
hujianyang2f83fd82015-01-06 12:52:13 +08001123 err = PTR_ERR(mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001124 if (IS_ERR(mnt)) {
1125 pr_err("overlayfs: failed to clone lowerpath\n");
1126 goto out_put_lower_mnt;
1127 }
1128 /*
1129 * Make lower_mnt R/O. That way fchmod/fchown on lower file
1130 * will fail instead of modifying lower fs.
1131 */
Miklos Szeredid719e8f2016-07-29 12:05:23 +02001132 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001133
1134 ufs->lower_mnt[ufs->numlower] = mnt;
1135 ufs->numlower++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001136 }
1137
hujianyang71cbad72015-01-15 13:20:57 +08001138 /* If the upper fs is nonexistent, we mark overlayfs r/o too */
1139 if (!ufs->upper_mnt)
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001140 sb->s_flags |= MS_RDONLY;
1141
Miklos Szeredi7c03b5d2015-06-22 13:53:48 +02001142 if (remote)
1143 sb->s_d_op = &ovl_reval_dentry_operations;
1144 else
1145 sb->s_d_op = &ovl_dentry_operations;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001146
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001147 ufs->creator_cred = prepare_creds();
1148 if (!ufs->creator_cred)
1149 goto out_put_lower_mnt;
1150
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001151 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001152 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001153 if (!oe)
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001154 goto out_put_cred;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001155
Miklos Szeredi39b681f2016-07-29 12:05:24 +02001156 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR));
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001157 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001158 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001159
1160 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001161 for (i = 0; i < numlower; i++)
1162 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001163 path_put(&workpath);
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001164 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001165
1166 oe->__upperdentry = upperpath.dentry;
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001167 for (i = 0; i < numlower; i++) {
1168 oe->lowerstack[i].dentry = stack[i].dentry;
1169 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
1170 }
Konstantin Khlebnikov0f955022015-08-24 15:57:18 +03001171 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001172
1173 root_dentry->d_fsdata = oe;
1174
Miklos Szeredi39b681f2016-07-29 12:05:24 +02001175 realinode = d_inode(ovl_dentry_real(root_dentry));
1176 ovl_inode_init(d_inode(root_dentry), realinode, !!upperpath.dentry);
1177 ovl_copyattr(realinode, d_inode(root_dentry));
Miklos Szeredied06e062015-12-09 16:11:59 +01001178
Andy Whitcroftcc259632014-10-24 00:14:38 +02001179 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001180 sb->s_op = &ovl_super_operations;
1181 sb->s_root = root_dentry;
1182 sb->s_fs_info = ufs;
Vivek Goyal39a25b22016-07-01 16:34:26 -04001183 sb->s_flags |= MS_POSIXACL;
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001184
1185 return 0;
1186
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001187out_free_oe:
1188 kfree(oe);
Antonio Murdaca3fe6e522016-04-07 15:48:25 +02001189out_put_cred:
1190 put_cred(ufs->creator_cred);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001191out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +01001192 for (i = 0; i < ufs->numlower; i++)
1193 mntput(ufs->lower_mnt[i]);
1194 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001195out_put_workdir:
1196 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001197 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001198out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +01001199 for (i = 0; i < numlower; i++)
1200 path_put(&stack[i]);
1201 kfree(stack);
1202out_free_lowertmp:
1203 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +01001204out_put_workpath:
1205 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001206out_put_upperpath:
1207 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001208out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +02001209 kfree(ufs->config.lowerdir);
1210 kfree(ufs->config.upperdir);
1211 kfree(ufs->config.workdir);
1212 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001213out:
1214 return err;
1215}
1216
1217static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1218 const char *dev_name, void *raw_data)
1219{
1220 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1221}
1222
1223static struct file_system_type ovl_fs_type = {
1224 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001225 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001226 .mount = ovl_mount,
1227 .kill_sb = kill_anon_super,
1228};
Miklos Szeredief94b182014-11-20 16:39:59 +01001229MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001230
1231static int __init ovl_init(void)
1232{
1233 return register_filesystem(&ovl_fs_type);
1234}
1235
1236static void __exit ovl_exit(void)
1237{
1238 unregister_filesystem(&ovl_fs_type);
1239}
1240
1241module_init(ovl_init);
1242module_exit(ovl_exit);