blob: 460d866b97a2d841bf76f0b00944a1cba850c50e [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 Szeredie9be9d52014-10-24 00:14:38 +020084 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +010085 return type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020086}
87
88static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
89{
Miklos Szeredi71d50922014-11-20 16:40:01 +010090 return lockless_dereference(oe->__upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020091}
92
93void ovl_path_upper(struct dentry *dentry, struct path *path)
94{
95 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
96 struct ovl_entry *oe = dentry->d_fsdata;
97
98 path->mnt = ofs->upper_mnt;
99 path->dentry = ovl_upperdentry_dereference(oe);
100}
101
102enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
103{
104
105 enum ovl_path_type type = ovl_path_type(dentry);
106
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100107 if (!OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200108 ovl_path_lower(dentry, path);
109 else
110 ovl_path_upper(dentry, path);
111
112 return type;
113}
114
115struct dentry *ovl_dentry_upper(struct dentry *dentry)
116{
117 struct ovl_entry *oe = dentry->d_fsdata;
118
119 return ovl_upperdentry_dereference(oe);
120}
121
122struct dentry *ovl_dentry_lower(struct dentry *dentry)
123{
124 struct ovl_entry *oe = dentry->d_fsdata;
125
Miklos Szeredidd662662014-12-13 00:59:43 +0100126 return __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200127}
128
129struct dentry *ovl_dentry_real(struct dentry *dentry)
130{
131 struct ovl_entry *oe = dentry->d_fsdata;
132 struct dentry *realdentry;
133
134 realdentry = ovl_upperdentry_dereference(oe);
135 if (!realdentry)
Miklos Szeredidd662662014-12-13 00:59:43 +0100136 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200137
138 return realdentry;
139}
140
141struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
142{
143 struct dentry *realdentry;
144
145 realdentry = ovl_upperdentry_dereference(oe);
146 if (realdentry) {
147 *is_upper = true;
148 } else {
Miklos Szeredidd662662014-12-13 00:59:43 +0100149 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200150 *is_upper = false;
151 }
152 return realdentry;
153}
154
155struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
156{
157 struct ovl_entry *oe = dentry->d_fsdata;
158
159 return oe->cache;
160}
161
162void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
163{
164 struct ovl_entry *oe = dentry->d_fsdata;
165
166 oe->cache = cache;
167}
168
169void ovl_path_lower(struct dentry *dentry, struct path *path)
170{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200171 struct ovl_entry *oe = dentry->d_fsdata;
172
Miklos Szeredidd662662014-12-13 00:59:43 +0100173 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200174}
175
176int ovl_want_write(struct dentry *dentry)
177{
178 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
179 return mnt_want_write(ofs->upper_mnt);
180}
181
182void ovl_drop_write(struct dentry *dentry)
183{
184 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
185 mnt_drop_write(ofs->upper_mnt);
186}
187
188struct dentry *ovl_workdir(struct dentry *dentry)
189{
190 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
191 return ofs->workdir;
192}
193
194bool ovl_dentry_is_opaque(struct dentry *dentry)
195{
196 struct ovl_entry *oe = dentry->d_fsdata;
197 return oe->opaque;
198}
199
200void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
201{
202 struct ovl_entry *oe = dentry->d_fsdata;
203 oe->opaque = opaque;
204}
205
206void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
207{
208 struct ovl_entry *oe = dentry->d_fsdata;
209
210 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
211 WARN_ON(oe->__upperdentry);
212 BUG_ON(!upperdentry->d_inode);
213 /*
214 * Make sure upperdentry is consistent before making it visible to
215 * ovl_upperdentry_dereference().
216 */
217 smp_wmb();
218 oe->__upperdentry = upperdentry;
219}
220
221void ovl_dentry_version_inc(struct dentry *dentry)
222{
223 struct ovl_entry *oe = dentry->d_fsdata;
224
225 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
226 oe->version++;
227}
228
229u64 ovl_dentry_version_get(struct dentry *dentry)
230{
231 struct ovl_entry *oe = dentry->d_fsdata;
232
233 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
234 return oe->version;
235}
236
237bool ovl_is_whiteout(struct dentry *dentry)
238{
239 struct inode *inode = dentry->d_inode;
240
241 return inode && IS_WHITEOUT(inode);
242}
243
244static bool ovl_is_opaquedir(struct dentry *dentry)
245{
246 int res;
247 char val;
248 struct inode *inode = dentry->d_inode;
249
250 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
251 return false;
252
253 res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1);
254 if (res == 1 && val == 'y')
255 return true;
256
257 return false;
258}
259
260static void ovl_dentry_release(struct dentry *dentry)
261{
262 struct ovl_entry *oe = dentry->d_fsdata;
263
264 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +0100265 unsigned int i;
266
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200267 dput(oe->__upperdentry);
Miklos Szeredidd662662014-12-13 00:59:43 +0100268 for (i = 0; i < oe->numlower; i++)
269 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200270 kfree_rcu(oe, rcu);
271 }
272}
273
274static const struct dentry_operations ovl_dentry_operations = {
275 .d_release = ovl_dentry_release,
276};
277
Miklos Szeredidd662662014-12-13 00:59:43 +0100278static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200279{
Miklos Szeredidd662662014-12-13 00:59:43 +0100280 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
281 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
282
283 if (oe)
284 oe->numlower = numlower;
285
286 return oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200287}
288
289static inline struct dentry *ovl_lookup_real(struct dentry *dir,
290 struct qstr *name)
291{
292 struct dentry *dentry;
293
294 mutex_lock(&dir->d_inode->i_mutex);
295 dentry = lookup_one_len(name->name, dir, name->len);
296 mutex_unlock(&dir->d_inode->i_mutex);
297
298 if (IS_ERR(dentry)) {
299 if (PTR_ERR(dentry) == -ENOENT)
300 dentry = NULL;
301 } else if (!dentry->d_inode) {
302 dput(dentry);
303 dentry = NULL;
304 }
305 return dentry;
306}
307
308struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
309 unsigned int flags)
310{
311 struct ovl_entry *oe;
312 struct dentry *upperdir;
Miklos Szeredidd662662014-12-13 00:59:43 +0100313 struct path lowerdir;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200314 struct dentry *upperdentry = NULL;
315 struct dentry *lowerdentry = NULL;
316 struct inode *inode = NULL;
317 int err;
318
319 err = -ENOMEM;
Miklos Szeredidd662662014-12-13 00:59:43 +0100320 oe = ovl_alloc_entry(1);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200321 if (!oe)
322 goto out;
323
324 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredidd662662014-12-13 00:59:43 +0100325 ovl_path_lower(dentry->d_parent, &lowerdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200326
327 if (upperdir) {
328 upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
329 err = PTR_ERR(upperdentry);
330 if (IS_ERR(upperdentry))
331 goto out_put_dir;
332
Miklos Szeredidd662662014-12-13 00:59:43 +0100333 if (lowerdir.dentry && upperdentry) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200334 if (ovl_is_whiteout(upperdentry)) {
335 dput(upperdentry);
336 upperdentry = NULL;
337 oe->opaque = true;
338 } else if (ovl_is_opaquedir(upperdentry)) {
339 oe->opaque = true;
340 }
341 }
342 }
Miklos Szeredidd662662014-12-13 00:59:43 +0100343 if (lowerdir.dentry && !oe->opaque) {
344 lowerdentry = ovl_lookup_real(lowerdir.dentry, &dentry->d_name);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200345 err = PTR_ERR(lowerdentry);
346 if (IS_ERR(lowerdentry))
347 goto out_dput_upper;
348 }
349
350 if (lowerdentry && upperdentry &&
351 (!S_ISDIR(upperdentry->d_inode->i_mode) ||
352 !S_ISDIR(lowerdentry->d_inode->i_mode))) {
353 dput(lowerdentry);
354 lowerdentry = NULL;
355 oe->opaque = true;
356 }
357
358 if (lowerdentry || upperdentry) {
359 struct dentry *realdentry;
360
361 realdentry = upperdentry ? upperdentry : lowerdentry;
362 err = -ENOMEM;
363 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
364 oe);
365 if (!inode)
366 goto out_dput;
367 ovl_copyattr(realdentry->d_inode, inode);
368 }
369
370 oe->__upperdentry = upperdentry;
Miklos Szeredidd662662014-12-13 00:59:43 +0100371 if (lowerdentry) {
372 oe->lowerstack[0].dentry = lowerdentry;
373 oe->lowerstack[0].mnt = lowerdir.mnt;
374 } else {
375 oe->numlower = 0;
376 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200377 dentry->d_fsdata = oe;
378 d_add(dentry, inode);
379
380 return NULL;
381
382out_dput:
383 dput(lowerdentry);
384out_dput_upper:
385 dput(upperdentry);
386out_put_dir:
387 kfree(oe);
388out:
389 return ERR_PTR(err);
390}
391
392struct file *ovl_path_open(struct path *path, int flags)
393{
394 return dentry_open(path, flags, current_cred());
395}
396
397static void ovl_put_super(struct super_block *sb)
398{
399 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100400 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200401
402 dput(ufs->workdir);
403 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100404 for (i = 0; i < ufs->numlower; i++)
405 mntput(ufs->lower_mnt[i]);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200406
Erez Zadokf45827e82014-10-24 00:14:38 +0200407 kfree(ufs->config.lowerdir);
408 kfree(ufs->config.upperdir);
409 kfree(ufs->config.workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200410 kfree(ufs);
411}
412
Andy Whitcroftcc259632014-10-24 00:14:38 +0200413/**
414 * ovl_statfs
415 * @sb: The overlayfs super block
416 * @buf: The struct kstatfs to fill in with stats
417 *
418 * Get the filesystem statistics. As writes always target the upper layer
419 * filesystem pass the statfs to the same filesystem.
420 */
421static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
422{
423 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
424 struct dentry *root_dentry = dentry->d_sb->s_root;
425 struct path path;
426 int err;
427
428 ovl_path_upper(root_dentry, &path);
429
430 err = vfs_statfs(&path, buf);
431 if (!err) {
432 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
433 buf->f_type = OVERLAYFS_SUPER_MAGIC;
434 }
435
436 return err;
437}
438
Erez Zadokf45827e82014-10-24 00:14:38 +0200439/**
440 * ovl_show_options
441 *
442 * Prints the mount options for a given superblock.
443 * Returns zero; does not fail.
444 */
445static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
446{
447 struct super_block *sb = dentry->d_sb;
448 struct ovl_fs *ufs = sb->s_fs_info;
449
450 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
451 seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
452 seq_printf(m, ",workdir=%s", ufs->config.workdir);
453 return 0;
454}
455
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200456static const struct super_operations ovl_super_operations = {
457 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200458 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200459 .show_options = ovl_show_options,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200460};
461
462enum {
463 OPT_LOWERDIR,
464 OPT_UPPERDIR,
465 OPT_WORKDIR,
466 OPT_ERR,
467};
468
469static const match_table_t ovl_tokens = {
470 {OPT_LOWERDIR, "lowerdir=%s"},
471 {OPT_UPPERDIR, "upperdir=%s"},
472 {OPT_WORKDIR, "workdir=%s"},
473 {OPT_ERR, NULL}
474};
475
Miklos Szeredi91c77942014-11-20 16:40:00 +0100476static char *ovl_next_opt(char **s)
477{
478 char *sbegin = *s;
479 char *p;
480
481 if (sbegin == NULL)
482 return NULL;
483
484 for (p = sbegin; *p; p++) {
485 if (*p == '\\') {
486 p++;
487 if (!*p)
488 break;
489 } else if (*p == ',') {
490 *p = '\0';
491 *s = p + 1;
492 return sbegin;
493 }
494 }
495 *s = NULL;
496 return sbegin;
497}
498
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200499static int ovl_parse_opt(char *opt, struct ovl_config *config)
500{
501 char *p;
502
Miklos Szeredi91c77942014-11-20 16:40:00 +0100503 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200504 int token;
505 substring_t args[MAX_OPT_ARGS];
506
507 if (!*p)
508 continue;
509
510 token = match_token(p, ovl_tokens, args);
511 switch (token) {
512 case OPT_UPPERDIR:
513 kfree(config->upperdir);
514 config->upperdir = match_strdup(&args[0]);
515 if (!config->upperdir)
516 return -ENOMEM;
517 break;
518
519 case OPT_LOWERDIR:
520 kfree(config->lowerdir);
521 config->lowerdir = match_strdup(&args[0]);
522 if (!config->lowerdir)
523 return -ENOMEM;
524 break;
525
526 case OPT_WORKDIR:
527 kfree(config->workdir);
528 config->workdir = match_strdup(&args[0]);
529 if (!config->workdir)
530 return -ENOMEM;
531 break;
532
533 default:
534 return -EINVAL;
535 }
536 }
537 return 0;
538}
539
540#define OVL_WORKDIR_NAME "work"
541
542static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
543 struct dentry *dentry)
544{
545 struct inode *dir = dentry->d_inode;
546 struct dentry *work;
547 int err;
548 bool retried = false;
549
550 err = mnt_want_write(mnt);
551 if (err)
552 return ERR_PTR(err);
553
554 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
555retry:
556 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
557 strlen(OVL_WORKDIR_NAME));
558
559 if (!IS_ERR(work)) {
560 struct kstat stat = {
561 .mode = S_IFDIR | 0,
562 };
563
564 if (work->d_inode) {
565 err = -EEXIST;
566 if (retried)
567 goto out_dput;
568
569 retried = true;
570 ovl_cleanup(dir, work);
571 dput(work);
572 goto retry;
573 }
574
575 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
576 if (err)
577 goto out_dput;
578 }
579out_unlock:
580 mutex_unlock(&dir->i_mutex);
581 mnt_drop_write(mnt);
582
583 return work;
584
585out_dput:
586 dput(work);
587 work = ERR_PTR(err);
588 goto out_unlock;
589}
590
Miklos Szeredi91c77942014-11-20 16:40:00 +0100591static void ovl_unescape(char *s)
592{
593 char *d = s;
594
595 for (;; s++, d++) {
596 if (*s == '\\')
597 s++;
598 *d = *s;
599 if (!*s)
600 break;
601 }
602}
603
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200604static int ovl_mount_dir(const char *name, struct path *path)
605{
606 int err;
Miklos Szeredi91c77942014-11-20 16:40:00 +0100607 char *tmp = kstrdup(name, GFP_KERNEL);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200608
Miklos Szeredi91c77942014-11-20 16:40:00 +0100609 if (!tmp)
610 return -ENOMEM;
611
612 ovl_unescape(tmp);
613 err = kern_path(tmp, LOOKUP_FOLLOW, path);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200614 if (err) {
Miklos Szeredi91c77942014-11-20 16:40:00 +0100615 pr_err("overlayfs: failed to resolve '%s': %i\n", tmp, err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200616 err = -EINVAL;
617 }
Miklos Szeredi91c77942014-11-20 16:40:00 +0100618 kfree(tmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200619 return err;
620}
621
622static bool ovl_is_allowed_fs_type(struct dentry *root)
623{
624 const struct dentry_operations *dop = root->d_op;
625
626 /*
627 * We don't support:
628 * - automount filesystems
629 * - filesystems with revalidate (FIXME for lower layer)
630 * - filesystems with case insensitive names
631 */
632 if (dop &&
633 (dop->d_manage || dop->d_automount ||
634 dop->d_revalidate || dop->d_weak_revalidate ||
635 dop->d_compare || dop->d_hash)) {
636 return false;
637 }
638 return true;
639}
640
641/* Workdir should not be subdir of upperdir and vice versa */
642static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
643{
644 bool ok = false;
645
646 if (workdir != upperdir) {
647 ok = (lock_rename(workdir, upperdir) == NULL);
648 unlock_rename(workdir, upperdir);
649 }
650 return ok;
651}
652
653static int ovl_fill_super(struct super_block *sb, void *data, int silent)
654{
655 struct path lowerpath;
656 struct path upperpath;
657 struct path workpath;
658 struct inode *root_inode;
659 struct dentry *root_dentry;
660 struct ovl_entry *oe;
661 struct ovl_fs *ufs;
Andy Whitcroftcc259632014-10-24 00:14:38 +0200662 struct kstatfs statfs;
Miklos Szeredidd662662014-12-13 00:59:43 +0100663 struct vfsmount *mnt;
664 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200665 int err;
666
Erez Zadokf45827e82014-10-24 00:14:38 +0200667 err = -ENOMEM;
668 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
669 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200670 goto out;
671
Erez Zadokf45827e82014-10-24 00:14:38 +0200672 err = ovl_parse_opt((char *) data, &ufs->config);
673 if (err)
674 goto out_free_config;
675
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200676 /* FIXME: workdir is not needed for a R/O mount */
677 err = -EINVAL;
Erez Zadokf45827e82014-10-24 00:14:38 +0200678 if (!ufs->config.upperdir || !ufs->config.lowerdir ||
679 !ufs->config.workdir) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200680 pr_err("overlayfs: missing upperdir or lowerdir or workdir\n");
681 goto out_free_config;
682 }
683
684 err = -ENOMEM;
Miklos Szeredidd662662014-12-13 00:59:43 +0100685 oe = ovl_alloc_entry(1);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200686 if (oe == NULL)
Erez Zadokf45827e82014-10-24 00:14:38 +0200687 goto out_free_config;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200688
Erez Zadokf45827e82014-10-24 00:14:38 +0200689 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200690 if (err)
691 goto out_free_oe;
692
Erez Zadokf45827e82014-10-24 00:14:38 +0200693 err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200694 if (err)
695 goto out_put_upperpath;
696
Erez Zadokf45827e82014-10-24 00:14:38 +0200697 err = ovl_mount_dir(ufs->config.workdir, &workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200698 if (err)
699 goto out_put_lowerpath;
700
701 err = -EINVAL;
702 if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
703 !S_ISDIR(lowerpath.dentry->d_inode->i_mode) ||
704 !S_ISDIR(workpath.dentry->d_inode->i_mode)) {
705 pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n");
706 goto out_put_workpath;
707 }
708
709 if (upperpath.mnt != workpath.mnt) {
710 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
711 goto out_put_workpath;
712 }
713 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
714 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
715 goto out_put_workpath;
716 }
717
718 if (!ovl_is_allowed_fs_type(upperpath.dentry)) {
719 pr_err("overlayfs: filesystem of upperdir is not supported\n");
720 goto out_put_workpath;
721 }
722
723 if (!ovl_is_allowed_fs_type(lowerpath.dentry)) {
724 pr_err("overlayfs: filesystem of lowerdir is not supported\n");
725 goto out_put_workpath;
726 }
727
Andy Whitcroftcc259632014-10-24 00:14:38 +0200728 err = vfs_statfs(&lowerpath, &statfs);
729 if (err) {
730 pr_err("overlayfs: statfs failed on lowerpath\n");
731 goto out_put_workpath;
732 }
733 ufs->lower_namelen = statfs.f_namelen;
734
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200735 sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
736 lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
737
738 err = -EINVAL;
739 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
740 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
741 goto out_put_workpath;
742 }
743
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200744 ufs->upper_mnt = clone_private_mount(&upperpath);
745 err = PTR_ERR(ufs->upper_mnt);
746 if (IS_ERR(ufs->upper_mnt)) {
747 pr_err("overlayfs: failed to clone upperpath\n");
748 goto out_put_workpath;
749 }
750
Miklos Szeredidd662662014-12-13 00:59:43 +0100751 ufs->lower_mnt = kcalloc(1, sizeof(struct vfsmount *), GFP_KERNEL);
752 if (ufs->lower_mnt == NULL)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200753 goto out_put_upper_mnt;
Miklos Szeredidd662662014-12-13 00:59:43 +0100754
755 mnt = clone_private_mount(&lowerpath);
756 err = PTR_ERR(mnt);
757 if (IS_ERR(mnt)) {
758 pr_err("overlayfs: failed to clone lowerpath\n");
759 goto out_put_lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200760 }
Miklos Szeredidd662662014-12-13 00:59:43 +0100761 /*
762 * Make lower_mnt R/O. That way fchmod/fchown on lower file
763 * will fail instead of modifying lower fs.
764 */
765 mnt->mnt_flags |= MNT_READONLY;
766
767 ufs->lower_mnt[0] = mnt;
768 ufs->numlower = 1;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200769
770 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
771 err = PTR_ERR(ufs->workdir);
772 if (IS_ERR(ufs->workdir)) {
773 pr_err("overlayfs: failed to create directory %s/%s\n",
Erez Zadokf45827e82014-10-24 00:14:38 +0200774 ufs->config.workdir, OVL_WORKDIR_NAME);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200775 goto out_put_lower_mnt;
776 }
777
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200778 /* If the upper fs is r/o, we mark overlayfs r/o too */
779 if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
780 sb->s_flags |= MS_RDONLY;
781
782 sb->s_d_op = &ovl_dentry_operations;
783
784 err = -ENOMEM;
785 root_inode = ovl_new_inode(sb, S_IFDIR, oe);
786 if (!root_inode)
787 goto out_put_workdir;
788
789 root_dentry = d_make_root(root_inode);
790 if (!root_dentry)
791 goto out_put_workdir;
792
793 mntput(upperpath.mnt);
794 mntput(lowerpath.mnt);
795 path_put(&workpath);
796
797 oe->__upperdentry = upperpath.dentry;
Miklos Szeredidd662662014-12-13 00:59:43 +0100798 oe->lowerstack[0].dentry = lowerpath.dentry;
799 oe->lowerstack[0].mnt = ufs->lower_mnt[0];
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200800
801 root_dentry->d_fsdata = oe;
802
Andy Whitcroftcc259632014-10-24 00:14:38 +0200803 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200804 sb->s_op = &ovl_super_operations;
805 sb->s_root = root_dentry;
806 sb->s_fs_info = ufs;
807
808 return 0;
809
810out_put_workdir:
811 dput(ufs->workdir);
812out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +0100813 for (i = 0; i < ufs->numlower; i++)
814 mntput(ufs->lower_mnt[i]);
815 kfree(ufs->lower_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200816out_put_upper_mnt:
817 mntput(ufs->upper_mnt);
818out_put_workpath:
819 path_put(&workpath);
820out_put_lowerpath:
821 path_put(&lowerpath);
822out_put_upperpath:
823 path_put(&upperpath);
824out_free_oe:
825 kfree(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200826out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +0200827 kfree(ufs->config.lowerdir);
828 kfree(ufs->config.upperdir);
829 kfree(ufs->config.workdir);
830 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200831out:
832 return err;
833}
834
835static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
836 const char *dev_name, void *raw_data)
837{
838 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
839}
840
841static struct file_system_type ovl_fs_type = {
842 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +0100843 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200844 .mount = ovl_mount,
845 .kill_sb = kill_anon_super,
846};
Miklos Szeredief94b182014-11-20 16:39:59 +0100847MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200848
849static int __init ovl_init(void)
850{
851 return register_filesystem(&ovl_fs_type);
852}
853
854static void __exit ovl_exit(void)
855{
856 unregister_filesystem(&ovl_fs_type);
857}
858
859module_init(ovl_init);
860module_exit(ovl_exit);