blob: b90952f528b1cdf7414e49b5613e439d6b89fb7a [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
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +010063#define OVL_MAX_STACK 500
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{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200107 enum ovl_path_type type = ovl_path_type(dentry);
108
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100109 if (!OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200110 ovl_path_lower(dentry, path);
111 else
112 ovl_path_upper(dentry, path);
113
114 return type;
115}
116
117struct dentry *ovl_dentry_upper(struct dentry *dentry)
118{
119 struct ovl_entry *oe = dentry->d_fsdata;
120
121 return ovl_upperdentry_dereference(oe);
122}
123
124struct dentry *ovl_dentry_lower(struct dentry *dentry)
125{
126 struct ovl_entry *oe = dentry->d_fsdata;
127
Miklos Szeredidd662662014-12-13 00:59:43 +0100128 return __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200129}
130
131struct dentry *ovl_dentry_real(struct dentry *dentry)
132{
133 struct ovl_entry *oe = dentry->d_fsdata;
134 struct dentry *realdentry;
135
136 realdentry = ovl_upperdentry_dereference(oe);
137 if (!realdentry)
Miklos Szeredidd662662014-12-13 00:59:43 +0100138 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200139
140 return realdentry;
141}
142
143struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
144{
145 struct dentry *realdentry;
146
147 realdentry = ovl_upperdentry_dereference(oe);
148 if (realdentry) {
149 *is_upper = true;
150 } else {
Miklos Szeredidd662662014-12-13 00:59:43 +0100151 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200152 *is_upper = false;
153 }
154 return realdentry;
155}
156
157struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
158{
159 struct ovl_entry *oe = dentry->d_fsdata;
160
161 return oe->cache;
162}
163
164void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
165{
166 struct ovl_entry *oe = dentry->d_fsdata;
167
168 oe->cache = cache;
169}
170
171void ovl_path_lower(struct dentry *dentry, struct path *path)
172{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200173 struct ovl_entry *oe = dentry->d_fsdata;
174
Miklos Szeredidd662662014-12-13 00:59:43 +0100175 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200176}
177
178int ovl_want_write(struct dentry *dentry)
179{
180 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
181 return mnt_want_write(ofs->upper_mnt);
182}
183
184void ovl_drop_write(struct dentry *dentry)
185{
186 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
187 mnt_drop_write(ofs->upper_mnt);
188}
189
190struct dentry *ovl_workdir(struct dentry *dentry)
191{
192 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
193 return ofs->workdir;
194}
195
196bool ovl_dentry_is_opaque(struct dentry *dentry)
197{
198 struct ovl_entry *oe = dentry->d_fsdata;
199 return oe->opaque;
200}
201
202void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
203{
204 struct ovl_entry *oe = dentry->d_fsdata;
205 oe->opaque = opaque;
206}
207
208void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
209{
210 struct ovl_entry *oe = dentry->d_fsdata;
211
212 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
213 WARN_ON(oe->__upperdentry);
214 BUG_ON(!upperdentry->d_inode);
215 /*
216 * Make sure upperdentry is consistent before making it visible to
217 * ovl_upperdentry_dereference().
218 */
219 smp_wmb();
220 oe->__upperdentry = upperdentry;
221}
222
223void ovl_dentry_version_inc(struct dentry *dentry)
224{
225 struct ovl_entry *oe = dentry->d_fsdata;
226
227 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
228 oe->version++;
229}
230
231u64 ovl_dentry_version_get(struct dentry *dentry)
232{
233 struct ovl_entry *oe = dentry->d_fsdata;
234
235 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
236 return oe->version;
237}
238
239bool ovl_is_whiteout(struct dentry *dentry)
240{
241 struct inode *inode = dentry->d_inode;
242
243 return inode && IS_WHITEOUT(inode);
244}
245
246static bool ovl_is_opaquedir(struct dentry *dentry)
247{
248 int res;
249 char val;
250 struct inode *inode = dentry->d_inode;
251
252 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
253 return false;
254
hujianyangcead89b2014-11-24 18:25:21 +0800255 res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200256 if (res == 1 && val == 'y')
257 return true;
258
259 return false;
260}
261
262static void ovl_dentry_release(struct dentry *dentry)
263{
264 struct ovl_entry *oe = dentry->d_fsdata;
265
266 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +0100267 unsigned int i;
268
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200269 dput(oe->__upperdentry);
Miklos Szeredidd662662014-12-13 00:59:43 +0100270 for (i = 0; i < oe->numlower; i++)
271 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200272 kfree_rcu(oe, rcu);
273 }
274}
275
276static const struct dentry_operations ovl_dentry_operations = {
277 .d_release = ovl_dentry_release,
278};
279
Miklos Szeredidd662662014-12-13 00:59:43 +0100280static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200281{
Miklos Szeredidd662662014-12-13 00:59:43 +0100282 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
283 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
284
285 if (oe)
286 oe->numlower = numlower;
287
288 return oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200289}
290
291static inline struct dentry *ovl_lookup_real(struct dentry *dir,
292 struct qstr *name)
293{
294 struct dentry *dentry;
295
296 mutex_lock(&dir->d_inode->i_mutex);
297 dentry = lookup_one_len(name->name, dir, name->len);
298 mutex_unlock(&dir->d_inode->i_mutex);
299
300 if (IS_ERR(dentry)) {
301 if (PTR_ERR(dentry) == -ENOENT)
302 dentry = NULL;
303 } else if (!dentry->d_inode) {
304 dput(dentry);
305 dentry = NULL;
306 }
307 return dentry;
308}
309
Miklos Szeredi5ef88da2014-12-13 00:59:43 +0100310/*
311 * Returns next layer in stack starting from top.
312 * Returns -1 if this is the last layer.
313 */
314int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
315{
316 struct ovl_entry *oe = dentry->d_fsdata;
317
318 BUG_ON(idx < 0);
319 if (idx == 0) {
320 ovl_path_upper(dentry, path);
321 if (path->dentry)
322 return oe->numlower ? 1 : -1;
323 idx++;
324 }
325 BUG_ON(idx > oe->numlower);
326 *path = oe->lowerstack[idx - 1];
327
328 return (idx < oe->numlower) ? idx + 1 : -1;
329}
330
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200331struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
332 unsigned int flags)
333{
334 struct ovl_entry *oe;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100335 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
336 struct path *stack = NULL;
337 struct dentry *upperdir, *upperdentry = NULL;
338 unsigned int ctr = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200339 struct inode *inode = NULL;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100340 bool upperopaque = false;
341 struct dentry *this, *prev = NULL;
342 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200343 int err;
344
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100345 upperdir = ovl_upperdentry_dereference(poe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200346 if (upperdir) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100347 this = ovl_lookup_real(upperdir, &dentry->d_name);
348 err = PTR_ERR(this);
349 if (IS_ERR(this))
350 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200351
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100352 if (this) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100353 if (ovl_is_whiteout(this)) {
354 dput(this);
355 this = NULL;
356 upperopaque = true;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100357 } else if (poe->numlower && ovl_is_opaquedir(this)) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100358 upperopaque = true;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200359 }
360 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100361 upperdentry = prev = this;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200362 }
363
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100364 if (!upperopaque && poe->numlower) {
365 err = -ENOMEM;
366 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
367 if (!stack)
368 goto out_put_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200369 }
370
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100371 for (i = 0; !upperopaque && i < poe->numlower; i++) {
372 bool opaque = false;
373 struct path lowerpath = poe->lowerstack[i];
374
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100375 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
376 err = PTR_ERR(this);
Miklos Szeredi09e10322014-12-13 00:59:45 +0100377 if (IS_ERR(this)) {
378 /*
379 * If it's positive, then treat ENAMETOOLONG as ENOENT.
380 */
381 if (err == -ENAMETOOLONG && (upperdentry || ctr))
382 continue;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100383 goto out_put;
Miklos Szeredi09e10322014-12-13 00:59:45 +0100384 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100385 if (!this)
386 continue;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100387 if (ovl_is_whiteout(this)) {
388 dput(this);
389 break;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100390 }
391 /*
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100392 * Only makes sense to check opaque dir if this is not the
393 * lowermost layer.
394 */
395 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
396 opaque = true;
hujianyanga425c032015-01-06 16:10:01 +0800397
398 if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
399 !S_ISDIR(this->d_inode->i_mode))) {
400 /*
401 * FIXME: check for upper-opaqueness maybe better done
402 * in remove code.
403 */
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100404 if (prev == upperdentry)
405 upperopaque = true;
406 dput(this);
407 break;
408 }
hujianyanga425c032015-01-06 16:10:01 +0800409 /*
410 * If this is a non-directory then stop here.
411 */
412 if (!S_ISDIR(this->d_inode->i_mode))
413 opaque = true;
414
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100415 stack[ctr].dentry = this;
416 stack[ctr].mnt = lowerpath.mnt;
417 ctr++;
418 prev = this;
419 if (opaque)
420 break;
421 }
422
423 oe = ovl_alloc_entry(ctr);
424 err = -ENOMEM;
425 if (!oe)
426 goto out_put;
427
428 if (upperdentry || ctr) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200429 struct dentry *realdentry;
430
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100431 realdentry = upperdentry ? upperdentry : stack[0].dentry;
432
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200433 err = -ENOMEM;
434 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
435 oe);
436 if (!inode)
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100437 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200438 ovl_copyattr(realdentry->d_inode, inode);
439 }
440
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100441 oe->opaque = upperopaque;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200442 oe->__upperdentry = upperdentry;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100443 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
444 kfree(stack);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200445 dentry->d_fsdata = oe;
446 d_add(dentry, inode);
447
448 return NULL;
449
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100450out_free_oe:
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200451 kfree(oe);
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100452out_put:
453 for (i = 0; i < ctr; i++)
454 dput(stack[i].dentry);
455 kfree(stack);
456out_put_upper:
457 dput(upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200458out:
459 return ERR_PTR(err);
460}
461
462struct file *ovl_path_open(struct path *path, int flags)
463{
464 return dentry_open(path, flags, current_cred());
465}
466
467static void ovl_put_super(struct super_block *sb)
468{
469 struct ovl_fs *ufs = sb->s_fs_info;
Miklos Szeredidd662662014-12-13 00:59:43 +0100470 unsigned i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200471
472 dput(ufs->workdir);
473 mntput(ufs->upper_mnt);
Miklos Szeredidd662662014-12-13 00:59:43 +0100474 for (i = 0; i < ufs->numlower; i++)
475 mntput(ufs->lower_mnt[i]);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200476
Erez Zadokf45827e82014-10-24 00:14:38 +0200477 kfree(ufs->config.lowerdir);
478 kfree(ufs->config.upperdir);
479 kfree(ufs->config.workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200480 kfree(ufs);
481}
482
Andy Whitcroftcc259632014-10-24 00:14:38 +0200483/**
484 * ovl_statfs
485 * @sb: The overlayfs super block
486 * @buf: The struct kstatfs to fill in with stats
487 *
488 * Get the filesystem statistics. As writes always target the upper layer
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100489 * filesystem pass the statfs to the upper filesystem (if it exists)
Andy Whitcroftcc259632014-10-24 00:14:38 +0200490 */
491static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
492{
493 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
494 struct dentry *root_dentry = dentry->d_sb->s_root;
495 struct path path;
496 int err;
497
Miklos Szeredi4ebc5812014-12-13 00:59:46 +0100498 ovl_path_real(root_dentry, &path);
Andy Whitcroftcc259632014-10-24 00:14:38 +0200499
500 err = vfs_statfs(&path, buf);
501 if (!err) {
502 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
503 buf->f_type = OVERLAYFS_SUPER_MAGIC;
504 }
505
506 return err;
507}
508
Erez Zadokf45827e82014-10-24 00:14:38 +0200509/**
510 * ovl_show_options
511 *
512 * Prints the mount options for a given superblock.
513 * Returns zero; does not fail.
514 */
515static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
516{
517 struct super_block *sb = dentry->d_sb;
518 struct ovl_fs *ufs = sb->s_fs_info;
519
520 seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100521 if (ufs->config.upperdir) {
522 seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
523 seq_printf(m, ",workdir=%s", ufs->config.workdir);
524 }
Erez Zadokf45827e82014-10-24 00:14:38 +0200525 return 0;
526}
527
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900528static int ovl_remount(struct super_block *sb, int *flags, char *data)
529{
530 struct ovl_fs *ufs = sb->s_fs_info;
531
532 if (!(*flags & MS_RDONLY) &&
533 (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)))
534 return -EROFS;
535
536 return 0;
537}
538
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200539static const struct super_operations ovl_super_operations = {
540 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200541 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200542 .show_options = ovl_show_options,
Seunghun Lee3cdf6fe2015-01-03 02:26:49 +0900543 .remount_fs = ovl_remount,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200544};
545
546enum {
547 OPT_LOWERDIR,
548 OPT_UPPERDIR,
549 OPT_WORKDIR,
550 OPT_ERR,
551};
552
553static const match_table_t ovl_tokens = {
554 {OPT_LOWERDIR, "lowerdir=%s"},
555 {OPT_UPPERDIR, "upperdir=%s"},
556 {OPT_WORKDIR, "workdir=%s"},
557 {OPT_ERR, NULL}
558};
559
Miklos Szeredi91c77942014-11-20 16:40:00 +0100560static char *ovl_next_opt(char **s)
561{
562 char *sbegin = *s;
563 char *p;
564
565 if (sbegin == NULL)
566 return NULL;
567
568 for (p = sbegin; *p; p++) {
569 if (*p == '\\') {
570 p++;
571 if (!*p)
572 break;
573 } else if (*p == ',') {
574 *p = '\0';
575 *s = p + 1;
576 return sbegin;
577 }
578 }
579 *s = NULL;
580 return sbegin;
581}
582
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200583static int ovl_parse_opt(char *opt, struct ovl_config *config)
584{
585 char *p;
586
Miklos Szeredi91c77942014-11-20 16:40:00 +0100587 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200588 int token;
589 substring_t args[MAX_OPT_ARGS];
590
591 if (!*p)
592 continue;
593
594 token = match_token(p, ovl_tokens, args);
595 switch (token) {
596 case OPT_UPPERDIR:
597 kfree(config->upperdir);
598 config->upperdir = match_strdup(&args[0]);
599 if (!config->upperdir)
600 return -ENOMEM;
601 break;
602
603 case OPT_LOWERDIR:
604 kfree(config->lowerdir);
605 config->lowerdir = match_strdup(&args[0]);
606 if (!config->lowerdir)
607 return -ENOMEM;
608 break;
609
610 case OPT_WORKDIR:
611 kfree(config->workdir);
612 config->workdir = match_strdup(&args[0]);
613 if (!config->workdir)
614 return -ENOMEM;
615 break;
616
617 default:
618 return -EINVAL;
619 }
620 }
621 return 0;
622}
623
624#define OVL_WORKDIR_NAME "work"
625
626static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
627 struct dentry *dentry)
628{
629 struct inode *dir = dentry->d_inode;
630 struct dentry *work;
631 int err;
632 bool retried = false;
633
634 err = mnt_want_write(mnt);
635 if (err)
636 return ERR_PTR(err);
637
638 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
639retry:
640 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
641 strlen(OVL_WORKDIR_NAME));
642
643 if (!IS_ERR(work)) {
644 struct kstat stat = {
645 .mode = S_IFDIR | 0,
646 };
647
648 if (work->d_inode) {
649 err = -EEXIST;
650 if (retried)
651 goto out_dput;
652
653 retried = true;
654 ovl_cleanup(dir, work);
655 dput(work);
656 goto retry;
657 }
658
659 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
660 if (err)
661 goto out_dput;
662 }
663out_unlock:
664 mutex_unlock(&dir->i_mutex);
665 mnt_drop_write(mnt);
666
667 return work;
668
669out_dput:
670 dput(work);
671 work = ERR_PTR(err);
672 goto out_unlock;
673}
674
Miklos Szeredi91c77942014-11-20 16:40:00 +0100675static void ovl_unescape(char *s)
676{
677 char *d = s;
678
679 for (;; s++, d++) {
680 if (*s == '\\')
681 s++;
682 *d = *s;
683 if (!*s)
684 break;
685 }
686}
687
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200688static bool ovl_is_allowed_fs_type(struct dentry *root)
689{
690 const struct dentry_operations *dop = root->d_op;
691
692 /*
693 * We don't support:
694 * - automount filesystems
695 * - filesystems with revalidate (FIXME for lower layer)
696 * - filesystems with case insensitive names
697 */
698 if (dop &&
699 (dop->d_manage || dop->d_automount ||
700 dop->d_revalidate || dop->d_weak_revalidate ||
701 dop->d_compare || dop->d_hash)) {
702 return false;
703 }
704 return true;
705}
706
Miklos Szerediab508822014-12-13 00:59:49 +0100707static int ovl_mount_dir_noesc(const char *name, struct path *path)
708{
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100709 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100710
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100711 if (!*name) {
712 pr_err("overlayfs: empty lowerdir\n");
713 goto out;
714 }
Miklos Szerediab508822014-12-13 00:59:49 +0100715 err = kern_path(name, LOOKUP_FOLLOW, path);
716 if (err) {
717 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
718 goto out;
719 }
720 err = -EINVAL;
721 if (!ovl_is_allowed_fs_type(path->dentry)) {
722 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
723 goto out_put;
724 }
725 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
726 pr_err("overlayfs: '%s' not a directory\n", name);
727 goto out_put;
728 }
729 return 0;
730
731out_put:
732 path_put(path);
733out:
734 return err;
735}
736
737static int ovl_mount_dir(const char *name, struct path *path)
738{
739 int err = -ENOMEM;
740 char *tmp = kstrdup(name, GFP_KERNEL);
741
742 if (tmp) {
743 ovl_unescape(tmp);
744 err = ovl_mount_dir_noesc(tmp, path);
745 kfree(tmp);
746 }
747 return err;
748}
749
750static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
751 int *stack_depth)
752{
753 int err;
754 struct kstatfs statfs;
755
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100756 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100757 if (err)
758 goto out;
759
760 err = vfs_statfs(path, &statfs);
761 if (err) {
762 pr_err("overlayfs: statfs failed on '%s'\n", name);
763 goto out_put;
764 }
765 *namelen = max(*namelen, statfs.f_namelen);
766 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
767
768 return 0;
769
770out_put:
771 path_put(path);
772out:
773 return err;
774}
775
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200776/* Workdir should not be subdir of upperdir and vice versa */
777static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
778{
779 bool ok = false;
780
781 if (workdir != upperdir) {
782 ok = (lock_rename(workdir, upperdir) == NULL);
783 unlock_rename(workdir, upperdir);
784 }
785 return ok;
786}
787
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100788static unsigned int ovl_split_lowerdirs(char *str)
789{
790 unsigned int ctr = 1;
791 char *s, *d;
792
793 for (s = d = str;; s++, d++) {
794 if (*s == '\\') {
795 s++;
796 } else if (*s == ':') {
797 *d = '\0';
798 ctr++;
799 continue;
800 }
801 *d = *s;
802 if (!*s)
803 break;
804 }
805 return ctr;
806}
807
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200808static int ovl_fill_super(struct super_block *sb, void *data, int silent)
809{
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100810 struct path upperpath = { NULL, NULL };
811 struct path workpath = { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200812 struct dentry *root_dentry;
813 struct ovl_entry *oe;
814 struct ovl_fs *ufs;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100815 struct path *stack = NULL;
816 char *lowertmp;
817 char *lower;
818 unsigned int numlower;
819 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100820 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200821 int err;
822
Erez Zadokf45827e82014-10-24 00:14:38 +0200823 err = -ENOMEM;
824 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
825 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200826 goto out;
827
Erez Zadokf45827e82014-10-24 00:14:38 +0200828 err = ovl_parse_opt((char *) data, &ufs->config);
829 if (err)
830 goto out_free_config;
831
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200832 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100833 if (!ufs->config.lowerdir) {
834 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200835 goto out_free_config;
836 }
837
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100838 sb->s_stack_depth = 0;
839 if (ufs->config.upperdir) {
840 /* FIXME: workdir is not needed for a R/O mount */
841 if (!ufs->config.workdir) {
842 pr_err("overlayfs: missing 'workdir'\n");
843 goto out_free_config;
844 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200845
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100846 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
847 if (err)
848 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100849
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100850 err = ovl_mount_dir(ufs->config.workdir, &workpath);
851 if (err)
852 goto out_put_upperpath;
853
hujianyang2f83fd82015-01-06 12:52:13 +0800854 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100855 if (upperpath.mnt != workpath.mnt) {
856 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
857 goto out_put_workpath;
858 }
859 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
860 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
861 goto out_put_workpath;
862 }
863 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100864 }
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100865 err = -ENOMEM;
866 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
867 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100868 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200869
870 err = -EINVAL;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100871 stacklen = ovl_split_lowerdirs(lowertmp);
872 if (stacklen > OVL_MAX_STACK)
873 goto out_free_lowertmp;
874
875 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
876 if (!stack)
877 goto out_free_lowertmp;
878
879 lower = lowertmp;
880 for (numlower = 0; numlower < stacklen; numlower++) {
881 err = ovl_lower_dir(lower, &stack[numlower],
882 &ufs->lower_namelen, &sb->s_stack_depth);
883 if (err)
884 goto out_put_lowerpath;
885
886 lower = strchr(lower, '\0') + 1;
887 }
888
889 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100890 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200891 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
892 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100893 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200894 }
895
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100896 if (ufs->config.upperdir) {
897 ufs->upper_mnt = clone_private_mount(&upperpath);
898 err = PTR_ERR(ufs->upper_mnt);
899 if (IS_ERR(ufs->upper_mnt)) {
900 pr_err("overlayfs: failed to clone upperpath\n");
901 goto out_put_lowerpath;
902 }
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100903
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100904 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
905 err = PTR_ERR(ufs->workdir);
906 if (IS_ERR(ufs->workdir)) {
907 pr_err("overlayfs: failed to create directory %s/%s\n",
908 ufs->config.workdir, OVL_WORKDIR_NAME);
909 goto out_put_upper_mnt;
910 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200911 }
912
hujianyang2f83fd82015-01-06 12:52:13 +0800913 err = -ENOMEM;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100914 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +0100915 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100916 goto out_put_workdir;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100917 for (i = 0; i < numlower; i++) {
918 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +0100919
hujianyang2f83fd82015-01-06 12:52:13 +0800920 err = PTR_ERR(mnt);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100921 if (IS_ERR(mnt)) {
922 pr_err("overlayfs: failed to clone lowerpath\n");
923 goto out_put_lower_mnt;
924 }
925 /*
926 * Make lower_mnt R/O. That way fchmod/fchown on lower file
927 * will fail instead of modifying lower fs.
928 */
929 mnt->mnt_flags |= MNT_READONLY;
930
931 ufs->lower_mnt[ufs->numlower] = mnt;
932 ufs->numlower++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200933 }
934
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100935 /* If the upper fs is r/o or nonexistent, we mark overlayfs r/o too */
936 if (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200937 sb->s_flags |= MS_RDONLY;
938
939 sb->s_d_op = &ovl_dentry_operations;
940
941 err = -ENOMEM;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100942 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100943 if (!oe)
944 goto out_put_lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200945
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100946 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200947 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100948 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200949
950 mntput(upperpath.mnt);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100951 for (i = 0; i < numlower; i++)
952 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200953 path_put(&workpath);
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100954 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200955
956 oe->__upperdentry = upperpath.dentry;
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100957 for (i = 0; i < numlower; i++) {
958 oe->lowerstack[i].dentry = stack[i].dentry;
959 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
960 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200961
962 root_dentry->d_fsdata = oe;
963
Andy Whitcroftcc259632014-10-24 00:14:38 +0200964 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200965 sb->s_op = &ovl_super_operations;
966 sb->s_root = root_dentry;
967 sb->s_fs_info = ufs;
968
969 return 0;
970
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100971out_free_oe:
972 kfree(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200973out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +0100974 for (i = 0; i < ufs->numlower; i++)
975 mntput(ufs->lower_mnt[i]);
976 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100977out_put_workdir:
978 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200979out_put_upper_mnt:
980 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200981out_put_lowerpath:
Miklos Szeredia78d9f0d2014-12-13 00:59:52 +0100982 for (i = 0; i < numlower; i++)
983 path_put(&stack[i]);
984 kfree(stack);
985out_free_lowertmp:
986 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100987out_put_workpath:
988 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200989out_put_upperpath:
990 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200991out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +0200992 kfree(ufs->config.lowerdir);
993 kfree(ufs->config.upperdir);
994 kfree(ufs->config.workdir);
995 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200996out:
997 return err;
998}
999
1000static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1001 const char *dev_name, void *raw_data)
1002{
1003 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1004}
1005
1006static struct file_system_type ovl_fs_type = {
1007 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +01001008 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001009 .mount = ovl_mount,
1010 .kill_sb = kill_anon_super,
1011};
Miklos Szeredief94b182014-11-20 16:39:59 +01001012MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001013
1014static int __init ovl_init(void)
1015{
1016 return register_filesystem(&ovl_fs_type);
1017}
1018
1019static void __exit ovl_exit(void)
1020{
1021 unregister_filesystem(&ovl_fs_type);
1022}
1023
1024module_init(ovl_init);
1025module_exit(ovl_exit);