blob: c245043aa1b951bb31153ccba67ca31b21ced725 [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>
12#include <linux/xattr.h>
13#include <linux/security.h>
14#include <linux/mount.h>
15#include <linux/slab.h>
16#include <linux/parser.h>
17#include <linux/module.h>
18#include <linux/sched.h>
Andy Whitcroftcc259632014-10-24 00:14:38 +020019#include <linux/statfs.h>
Erez Zadokf45827e82014-10-24 00:14:38 +020020#include <linux/seq_file.h>
Miklos Szeredie9be9d52014-10-24 00:14:38 +020021#include "overlayfs.h"
22
23MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24MODULE_DESCRIPTION("Overlay filesystem");
25MODULE_LICENSE("GPL");
26
Miklos Szeredief94b182014-11-20 16:39:59 +010027#define OVERLAYFS_SUPER_MAGIC 0x794c7630
Andy Whitcroftcc259632014-10-24 00:14:38 +020028
Erez Zadokf45827e82014-10-24 00:14:38 +020029struct ovl_config {
30 char *lowerdir;
31 char *upperdir;
32 char *workdir;
33};
34
Miklos Szeredie9be9d52014-10-24 00:14:38 +020035/* private information held for overlayfs's superblock */
36struct ovl_fs {
37 struct vfsmount *upper_mnt;
Miklos Szeredidd662662014-12-13 00:59:43 +010038 unsigned numlower;
39 struct vfsmount **lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020040 struct dentry *workdir;
Andy Whitcroftcc259632014-10-24 00:14:38 +020041 long lower_namelen;
Erez Zadokf45827e82014-10-24 00:14:38 +020042 /* pathnames of lower and upper dirs, for show_options */
43 struct ovl_config config;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020044};
45
46struct ovl_dir_cache;
47
48/* private information held for every overlayfs dentry */
49struct ovl_entry {
50 struct dentry *__upperdentry;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020051 struct ovl_dir_cache *cache;
52 union {
53 struct {
54 u64 version;
55 bool opaque;
56 };
57 struct rcu_head rcu;
58 };
Miklos Szeredidd662662014-12-13 00:59:43 +010059 unsigned numlower;
60 struct path lowerstack[];
Miklos Szeredie9be9d52014-10-24 00:14:38 +020061};
62
63const char *ovl_opaque_xattr = "trusted.overlay.opaque";
64
Miklos Szeredidd662662014-12-13 00:59:43 +010065static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
66{
67 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
68}
Miklos Szeredie9be9d52014-10-24 00:14:38 +020069
70enum ovl_path_type ovl_path_type(struct dentry *dentry)
71{
72 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi1afaba12014-12-13 00:59:42 +010073 enum ovl_path_type type = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020074
75 if (oe->__upperdentry) {
Miklos Szeredi1afaba12014-12-13 00:59:42 +010076 type = __OVL_PATH_UPPER;
77
Miklos Szeredidd662662014-12-13 00:59:43 +010078 if (oe->numlower) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020079 if (S_ISDIR(dentry->d_inode->i_mode))
Miklos Szeredi1afaba12014-12-13 00:59:42 +010080 type |= __OVL_PATH_MERGE;
81 } else if (!oe->opaque) {
82 type |= __OVL_PATH_PURE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020083 }
Miklos Szeredi9d7459d2014-12-13 00:59:44 +010084 } else {
85 if (oe->numlower > 1)
86 type |= __OVL_PATH_MERGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020087 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +010088 return type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020089}
90
91static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
92{
Miklos Szeredi71d50922014-11-20 16:40:01 +010093 return lockless_dereference(oe->__upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020094}
95
96void ovl_path_upper(struct dentry *dentry, struct path *path)
97{
98 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
99 struct ovl_entry *oe = dentry->d_fsdata;
100
101 path->mnt = ofs->upper_mnt;
102 path->dentry = ovl_upperdentry_dereference(oe);
103}
104
105enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
106{
107
108 enum ovl_path_type type = ovl_path_type(dentry);
109
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100110 if (!OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200111 ovl_path_lower(dentry, path);
112 else
113 ovl_path_upper(dentry, path);
114
115 return type;
116}
117
118struct dentry *ovl_dentry_upper(struct dentry *dentry)
119{
120 struct ovl_entry *oe = dentry->d_fsdata;
121
122 return ovl_upperdentry_dereference(oe);
123}
124
125struct dentry *ovl_dentry_lower(struct dentry *dentry)
126{
127 struct ovl_entry *oe = dentry->d_fsdata;
128
Miklos Szeredidd662662014-12-13 00:59:43 +0100129 return __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200130}
131
132struct dentry *ovl_dentry_real(struct dentry *dentry)
133{
134 struct ovl_entry *oe = dentry->d_fsdata;
135 struct dentry *realdentry;
136
137 realdentry = ovl_upperdentry_dereference(oe);
138 if (!realdentry)
Miklos Szeredidd662662014-12-13 00:59:43 +0100139 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200140
141 return realdentry;
142}
143
144struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
145{
146 struct dentry *realdentry;
147
148 realdentry = ovl_upperdentry_dereference(oe);
149 if (realdentry) {
150 *is_upper = true;
151 } else {
Miklos Szeredidd662662014-12-13 00:59:43 +0100152 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200153 *is_upper = false;
154 }
155 return realdentry;
156}
157
158struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
159{
160 struct ovl_entry *oe = dentry->d_fsdata;
161
162 return oe->cache;
163}
164
165void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
166{
167 struct ovl_entry *oe = dentry->d_fsdata;
168
169 oe->cache = cache;
170}
171
172void ovl_path_lower(struct dentry *dentry, struct path *path)
173{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200174 struct ovl_entry *oe = dentry->d_fsdata;
175
Miklos Szeredidd662662014-12-13 00:59:43 +0100176 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200177}
178
179int ovl_want_write(struct dentry *dentry)
180{
181 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
182 return mnt_want_write(ofs->upper_mnt);
183}
184
185void ovl_drop_write(struct dentry *dentry)
186{
187 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
188 mnt_drop_write(ofs->upper_mnt);
189}
190
191struct dentry *ovl_workdir(struct dentry *dentry)
192{
193 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
194 return ofs->workdir;
195}
196
197bool ovl_dentry_is_opaque(struct dentry *dentry)
198{
199 struct ovl_entry *oe = dentry->d_fsdata;
200 return oe->opaque;
201}
202
203void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
204{
205 struct ovl_entry *oe = dentry->d_fsdata;
206 oe->opaque = opaque;
207}
208
209void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
210{
211 struct ovl_entry *oe = dentry->d_fsdata;
212
213 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
214 WARN_ON(oe->__upperdentry);
215 BUG_ON(!upperdentry->d_inode);
216 /*
217 * Make sure upperdentry is consistent before making it visible to
218 * ovl_upperdentry_dereference().
219 */
220 smp_wmb();
221 oe->__upperdentry = upperdentry;
222}
223
224void ovl_dentry_version_inc(struct dentry *dentry)
225{
226 struct ovl_entry *oe = dentry->d_fsdata;
227
228 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
229 oe->version++;
230}
231
232u64 ovl_dentry_version_get(struct dentry *dentry)
233{
234 struct ovl_entry *oe = dentry->d_fsdata;
235
236 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
237 return oe->version;
238}
239
240bool ovl_is_whiteout(struct dentry *dentry)
241{
242 struct inode *inode = dentry->d_inode;
243
244 return inode && IS_WHITEOUT(inode);
245}
246
247static bool ovl_is_opaquedir(struct dentry *dentry)
248{
249 int res;
250 char val;
251 struct inode *inode = dentry->d_inode;
252
253 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
254 return false;
255
256 res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1);
257 if (res == 1 && val == 'y')
258 return true;
259
260 return false;
261}
262
263static void ovl_dentry_release(struct dentry *dentry)
264{
265 struct ovl_entry *oe = dentry->d_fsdata;
266
267 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +0100268 unsigned int i;
269
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200270 dput(oe->__upperdentry);
Miklos Szeredidd662662014-12-13 00:59:43 +0100271 for (i = 0; i < oe->numlower; i++)
272 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200273 kfree_rcu(oe, rcu);
274 }
275}
276
277static const struct dentry_operations ovl_dentry_operations = {
278 .d_release = ovl_dentry_release,
279};
280
Miklos Szeredidd662662014-12-13 00:59:43 +0100281static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200282{
Miklos Szeredidd662662014-12-13 00:59:43 +0100283 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
284 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
285
286 if (oe)
287 oe->numlower = numlower;
288
289 return oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200290}
291
292static inline struct dentry *ovl_lookup_real(struct dentry *dir,
293 struct qstr *name)
294{
295 struct dentry *dentry;
296
297 mutex_lock(&dir->d_inode->i_mutex);
298 dentry = lookup_one_len(name->name, dir, name->len);
299 mutex_unlock(&dir->d_inode->i_mutex);
300
301 if (IS_ERR(dentry)) {
302 if (PTR_ERR(dentry) == -ENOENT)
303 dentry = NULL;
304 } else if (!dentry->d_inode) {
305 dput(dentry);
306 dentry = NULL;
307 }
308 return dentry;
309}
310
Miklos Szeredi5ef88da2014-12-13 00:59:43 +0100311/*
312 * Returns next layer in stack starting from top.
313 * Returns -1 if this is the last layer.
314 */
315int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
316{
317 struct ovl_entry *oe = dentry->d_fsdata;
318
319 BUG_ON(idx < 0);
320 if (idx == 0) {
321 ovl_path_upper(dentry, path);
322 if (path->dentry)
323 return oe->numlower ? 1 : -1;
324 idx++;
325 }
326 BUG_ON(idx > oe->numlower);
327 *path = oe->lowerstack[idx - 1];
328
329 return (idx < oe->numlower) ? idx + 1 : -1;
330}
331
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200332struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
333 unsigned int flags)
334{
335 struct ovl_entry *oe;
336 struct dentry *upperdir;
Miklos Szeredidd662662014-12-13 00:59:43 +0100337 struct path lowerdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200338 struct dentry *upperdentry = NULL;
339 struct dentry *lowerdentry = NULL;
340 struct inode *inode = NULL;
341 int err;
342
343 err = -ENOMEM;
Miklos Szeredidd662662014-12-13 00:59:43 +0100344 oe = ovl_alloc_entry(1);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200345 if (!oe)
346 goto out;
347
348 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredidd662662014-12-13 00:59:43 +0100349 ovl_path_lower(dentry->d_parent, &lowerdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200350
351 if (upperdir) {
352 upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
353 err = PTR_ERR(upperdentry);
354 if (IS_ERR(upperdentry))
355 goto out_put_dir;
356
Miklos Szeredidd662662014-12-13 00:59:43 +0100357 if (lowerdir.dentry && upperdentry) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200358 if (ovl_is_whiteout(upperdentry)) {
359 dput(upperdentry);
360 upperdentry = NULL;
361 oe->opaque = true;
362 } else if (ovl_is_opaquedir(upperdentry)) {
363 oe->opaque = true;
364 }
365 }
366 }
Miklos Szeredidd662662014-12-13 00:59:43 +0100367 if (lowerdir.dentry && !oe->opaque) {
368 lowerdentry = ovl_lookup_real(lowerdir.dentry, &dentry->d_name);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200369 err = PTR_ERR(lowerdentry);
370 if (IS_ERR(lowerdentry))
371 goto out_dput_upper;
372 }
373
374 if (lowerdentry && upperdentry &&
375 (!S_ISDIR(upperdentry->d_inode->i_mode) ||
376 !S_ISDIR(lowerdentry->d_inode->i_mode))) {
377 dput(lowerdentry);
378 lowerdentry = NULL;
379 oe->opaque = true;
380 }
381
382 if (lowerdentry || upperdentry) {
383 struct dentry *realdentry;
384
385 realdentry = upperdentry ? upperdentry : lowerdentry;
386 err = -ENOMEM;
387 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
388 oe);
389 if (!inode)
390 goto out_dput;
391 ovl_copyattr(realdentry->d_inode, inode);
392 }
393
394 oe->__upperdentry = upperdentry;
Miklos Szeredidd662662014-12-13 00:59:43 +0100395 if (lowerdentry) {
396 oe->lowerstack[0].dentry = lowerdentry;
397 oe->lowerstack[0].mnt = lowerdir.mnt;
398 } else {
399 oe->numlower = 0;
400 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200401 dentry->d_fsdata = oe;
402 d_add(dentry, inode);
403
404 return NULL;
405
406out_dput:
407 dput(lowerdentry);
408out_dput_upper:
409 dput(upperdentry);
410out_put_dir:
411 kfree(oe);
412out:
413 return ERR_PTR(err);
414}
415
416struct file *ovl_path_open(struct path *path, int flags)
417{
418 return dentry_open(path, flags, current_cred());
419}
420
421static void ovl_put_super(struct super_block *sb)
422{
423 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100424 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200425
426 dput(ufs->workdir);
427 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100428 for (i = 0; i < ufs->numlower; i++)
429 mntput(ufs->lower_mnt[i]);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200430
Erez Zadokf45827e82014-10-24 00:14:38 +0200431 kfree(ufs->config.lowerdir);
432 kfree(ufs->config.upperdir);
433 kfree(ufs->config.workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200434 kfree(ufs);
435}
436
Andy Whitcroftcc259632014-10-24 00:14:38 +0200437/**
438 * ovl_statfs
439 * @sb: The overlayfs super block
440 * @buf: The struct kstatfs to fill in with stats
441 *
442 * Get the filesystem statistics. As writes always target the upper layer
443 * filesystem pass the statfs to the same filesystem.
444 */
445static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
446{
447 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
448 struct dentry *root_dentry = dentry->d_sb->s_root;
449 struct path path;
450 int err;
451
452 ovl_path_upper(root_dentry, &path);
453
454 err = vfs_statfs(&path, buf);
455 if (!err) {
456 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
457 buf->f_type = OVERLAYFS_SUPER_MAGIC;
458 }
459
460 return err;
461}
462
Erez Zadokf45827e82014-10-24 00:14:38 +0200463/**
464 * ovl_show_options
465 *
466 * Prints the mount options for a given superblock.
467 * Returns zero; does not fail.
468 */
469static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
470{
471 struct super_block *sb = dentry->d_sb;
472 struct ovl_fs *ufs = sb->s_fs_info;
473
474 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
475 seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
476 seq_printf(m, ",workdir=%s", ufs->config.workdir);
477 return 0;
478}
479
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480static const struct super_operations ovl_super_operations = {
481 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200482 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200483 .show_options = ovl_show_options,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200484};
485
486enum {
487 OPT_LOWERDIR,
488 OPT_UPPERDIR,
489 OPT_WORKDIR,
490 OPT_ERR,
491};
492
493static const match_table_t ovl_tokens = {
494 {OPT_LOWERDIR, "lowerdir=%s"},
495 {OPT_UPPERDIR, "upperdir=%s"},
496 {OPT_WORKDIR, "workdir=%s"},
497 {OPT_ERR, NULL}
498};
499
Miklos Szeredi91c77942014-11-20 16:40:00 +0100500static char *ovl_next_opt(char **s)
501{
502 char *sbegin = *s;
503 char *p;
504
505 if (sbegin == NULL)
506 return NULL;
507
508 for (p = sbegin; *p; p++) {
509 if (*p == '\\') {
510 p++;
511 if (!*p)
512 break;
513 } else if (*p == ',') {
514 *p = '\0';
515 *s = p + 1;
516 return sbegin;
517 }
518 }
519 *s = NULL;
520 return sbegin;
521}
522
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200523static int ovl_parse_opt(char *opt, struct ovl_config *config)
524{
525 char *p;
526
Miklos Szeredi91c77942014-11-20 16:40:00 +0100527 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200528 int token;
529 substring_t args[MAX_OPT_ARGS];
530
531 if (!*p)
532 continue;
533
534 token = match_token(p, ovl_tokens, args);
535 switch (token) {
536 case OPT_UPPERDIR:
537 kfree(config->upperdir);
538 config->upperdir = match_strdup(&args[0]);
539 if (!config->upperdir)
540 return -ENOMEM;
541 break;
542
543 case OPT_LOWERDIR:
544 kfree(config->lowerdir);
545 config->lowerdir = match_strdup(&args[0]);
546 if (!config->lowerdir)
547 return -ENOMEM;
548 break;
549
550 case OPT_WORKDIR:
551 kfree(config->workdir);
552 config->workdir = match_strdup(&args[0]);
553 if (!config->workdir)
554 return -ENOMEM;
555 break;
556
557 default:
558 return -EINVAL;
559 }
560 }
561 return 0;
562}
563
564#define OVL_WORKDIR_NAME "work"
565
566static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
567 struct dentry *dentry)
568{
569 struct inode *dir = dentry->d_inode;
570 struct dentry *work;
571 int err;
572 bool retried = false;
573
574 err = mnt_want_write(mnt);
575 if (err)
576 return ERR_PTR(err);
577
578 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
579retry:
580 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
581 strlen(OVL_WORKDIR_NAME));
582
583 if (!IS_ERR(work)) {
584 struct kstat stat = {
585 .mode = S_IFDIR | 0,
586 };
587
588 if (work->d_inode) {
589 err = -EEXIST;
590 if (retried)
591 goto out_dput;
592
593 retried = true;
594 ovl_cleanup(dir, work);
595 dput(work);
596 goto retry;
597 }
598
599 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
600 if (err)
601 goto out_dput;
602 }
603out_unlock:
604 mutex_unlock(&dir->i_mutex);
605 mnt_drop_write(mnt);
606
607 return work;
608
609out_dput:
610 dput(work);
611 work = ERR_PTR(err);
612 goto out_unlock;
613}
614
Miklos Szeredi91c77942014-11-20 16:40:00 +0100615static void ovl_unescape(char *s)
616{
617 char *d = s;
618
619 for (;; s++, d++) {
620 if (*s == '\\')
621 s++;
622 *d = *s;
623 if (!*s)
624 break;
625 }
626}
627
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200628static int ovl_mount_dir(const char *name, struct path *path)
629{
630 int err;
Miklos Szeredi91c77942014-11-20 16:40:00 +0100631 char *tmp = kstrdup(name, GFP_KERNEL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200632
Miklos Szeredi91c77942014-11-20 16:40:00 +0100633 if (!tmp)
634 return -ENOMEM;
635
636 ovl_unescape(tmp);
637 err = kern_path(tmp, LOOKUP_FOLLOW, path);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200638 if (err) {
Miklos Szeredi91c77942014-11-20 16:40:00 +0100639 pr_err("overlayfs: failed to resolve '%s': %i\n", tmp, err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200640 err = -EINVAL;
641 }
Miklos Szeredi91c77942014-11-20 16:40:00 +0100642 kfree(tmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200643 return err;
644}
645
646static bool ovl_is_allowed_fs_type(struct dentry *root)
647{
648 const struct dentry_operations *dop = root->d_op;
649
650 /*
651 * We don't support:
652 * - automount filesystems
653 * - filesystems with revalidate (FIXME for lower layer)
654 * - filesystems with case insensitive names
655 */
656 if (dop &&
657 (dop->d_manage || dop->d_automount ||
658 dop->d_revalidate || dop->d_weak_revalidate ||
659 dop->d_compare || dop->d_hash)) {
660 return false;
661 }
662 return true;
663}
664
665/* Workdir should not be subdir of upperdir and vice versa */
666static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
667{
668 bool ok = false;
669
670 if (workdir != upperdir) {
671 ok = (lock_rename(workdir, upperdir) == NULL);
672 unlock_rename(workdir, upperdir);
673 }
674 return ok;
675}
676
677static int ovl_fill_super(struct super_block *sb, void *data, int silent)
678{
679 struct path lowerpath;
680 struct path upperpath;
681 struct path workpath;
682 struct inode *root_inode;
683 struct dentry *root_dentry;
684 struct ovl_entry *oe;
685 struct ovl_fs *ufs;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200686 struct kstatfs statfs;
Miklos Szeredidd662662014-12-13 00:59:43 +0100687 struct vfsmount *mnt;
688 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200689 int err;
690
Erez Zadokf45827e82014-10-24 00:14:38 +0200691 err = -ENOMEM;
692 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
693 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200694 goto out;
695
Erez Zadokf45827e82014-10-24 00:14:38 +0200696 err = ovl_parse_opt((char *) data, &ufs->config);
697 if (err)
698 goto out_free_config;
699
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200700 /* FIXME: workdir is not needed for a R/O mount */
701 err = -EINVAL;
Erez Zadokf45827e82014-10-24 00:14:38 +0200702 if (!ufs->config.upperdir || !ufs->config.lowerdir ||
703 !ufs->config.workdir) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200704 pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
705 goto out_free_config;
706 }
707
708 err = -ENOMEM;
Miklos Szeredidd662662014-12-13 00:59:43 +0100709 oe = ovl_alloc_entry(1);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200710 if (oe == NULL)
Erez Zadokf45827e82014-10-24 00:14:38 +0200711 goto out_free_config;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200712
Erez Zadokf45827e82014-10-24 00:14:38 +0200713 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200714 if (err)
715 goto out_free_oe;
716
Erez Zadokf45827e82014-10-24 00:14:38 +0200717 err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200718 if (err)
719 goto out_put_upperpath;
720
Erez Zadokf45827e82014-10-24 00:14:38 +0200721 err = ovl_mount_dir(ufs->config.workdir, &workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200722 if (err)
723 goto out_put_lowerpath;
724
725 err = -EINVAL;
726 if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
727 !S_ISDIR(lowerpath.dentry->d_inode->i_mode) ||
728 !S_ISDIR(workpath.dentry->d_inode->i_mode)) {
729 pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n");
730 goto out_put_workpath;
731 }
732
733 if (upperpath.mnt != workpath.mnt) {
734 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
735 goto out_put_workpath;
736 }
737 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
738 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
739 goto out_put_workpath;
740 }
741
742 if (!ovl_is_allowed_fs_type(upperpath.dentry)) {
743 pr_err("overlayfs: filesystem of upperdir is not supported\n");
744 goto out_put_workpath;
745 }
746
747 if (!ovl_is_allowed_fs_type(lowerpath.dentry)) {
748 pr_err("overlayfs: filesystem of lowerdir is not supported\n");
749 goto out_put_workpath;
750 }
751
Andy Whitcroftcc259632014-10-24 00:14:38 +0200752 err = vfs_statfs(&lowerpath, &statfs);
753 if (err) {
754 pr_err("overlayfs: statfs failed on lowerpath\n");
755 goto out_put_workpath;
756 }
757 ufs->lower_namelen = statfs.f_namelen;
758
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200759 sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
760 lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
761
762 err = -EINVAL;
763 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
764 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
765 goto out_put_workpath;
766 }
767
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200768 ufs->upper_mnt = clone_private_mount(&upperpath);
769 err = PTR_ERR(ufs->upper_mnt);
770 if (IS_ERR(ufs->upper_mnt)) {
771 pr_err("overlayfs: failed to clone upperpath\n");
772 goto out_put_workpath;
773 }
774
Miklos Szeredidd662662014-12-13 00:59:43 +0100775 ufs->lower_mnt = kcalloc(1, sizeof(struct vfsmount *), GFP_KERNEL);
776 if (ufs->lower_mnt == NULL)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200777 goto out_put_upper_mnt;
Miklos Szeredidd662662014-12-13 00:59:43 +0100778
779 mnt = clone_private_mount(&lowerpath);
780 err = PTR_ERR(mnt);
781 if (IS_ERR(mnt)) {
782 pr_err("overlayfs: failed to clone lowerpath\n");
783 goto out_put_lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200784 }
Miklos Szeredidd662662014-12-13 00:59:43 +0100785 /*
786 * Make lower_mnt R/O. That way fchmod/fchown on lower file
787 * will fail instead of modifying lower fs.
788 */
789 mnt->mnt_flags |= MNT_READONLY;
790
791 ufs->lower_mnt[0] = mnt;
792 ufs->numlower = 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200793
794 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
795 err = PTR_ERR(ufs->workdir);
796 if (IS_ERR(ufs->workdir)) {
797 pr_err("overlayfs: failed to create directory %s/%s\n",
Erez Zadokf45827e82014-10-24 00:14:38 +0200798 ufs->config.workdir, OVL_WORKDIR_NAME);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200799 goto out_put_lower_mnt;
800 }
801
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200802 /* If the upper fs is r/o, we mark overlayfs r/o too */
803 if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
804 sb->s_flags |= MS_RDONLY;
805
806 sb->s_d_op = &ovl_dentry_operations;
807
808 err = -ENOMEM;
809 root_inode = ovl_new_inode(sb, S_IFDIR, oe);
810 if (!root_inode)
811 goto out_put_workdir;
812
813 root_dentry = d_make_root(root_inode);
814 if (!root_dentry)
815 goto out_put_workdir;
816
817 mntput(upperpath.mnt);
818 mntput(lowerpath.mnt);
819 path_put(&workpath);
820
821 oe->__upperdentry = upperpath.dentry;
Miklos Szeredidd662662014-12-13 00:59:43 +0100822 oe->lowerstack[0].dentry = lowerpath.dentry;
823 oe->lowerstack[0].mnt = ufs->lower_mnt[0];
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200824
825 root_dentry->d_fsdata = oe;
826
Andy Whitcroftcc259632014-10-24 00:14:38 +0200827 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200828 sb->s_op = &ovl_super_operations;
829 sb->s_root = root_dentry;
830 sb->s_fs_info = ufs;
831
832 return 0;
833
834out_put_workdir:
835 dput(ufs->workdir);
836out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +0100837 for (i = 0; i < ufs->numlower; i++)
838 mntput(ufs->lower_mnt[i]);
839 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200840out_put_upper_mnt:
841 mntput(ufs->upper_mnt);
842out_put_workpath:
843 path_put(&workpath);
844out_put_lowerpath:
845 path_put(&lowerpath);
846out_put_upperpath:
847 path_put(&upperpath);
848out_free_oe:
849 kfree(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200850out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +0200851 kfree(ufs->config.lowerdir);
852 kfree(ufs->config.upperdir);
853 kfree(ufs->config.workdir);
854 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200855out:
856 return err;
857}
858
859static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
860 const char *dev_name, void *raw_data)
861{
862 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
863}
864
865static struct file_system_type ovl_fs_type = {
866 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +0100867 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200868 .mount = ovl_mount,
869 .kill_sb = kill_anon_super,
870};
Miklos Szeredief94b182014-11-20 16:39:59 +0100871MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200872
873static int __init ovl_init(void)
874{
875 return register_filesystem(&ovl_fs_type);
876}
877
878static void __exit ovl_exit(void)
879{
880 unregister_filesystem(&ovl_fs_type);
881}
882
883module_init(ovl_init);
884module_exit(ovl_exit);