blob: 5c495a17a5a3e2dff1ca52782f54a5e7bec646cd [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 Szeredia78d9f02014-12-13 00:59:52 +010063#define OVL_MAX_STACK 500
64
Miklos Szeredie9be9d52014-10-24 00:14:38 +020065const char *ovl_opaque_xattr = "trusted.overlay.opaque";
66
Miklos Szeredidd662662014-12-13 00:59:43 +010067static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
68{
69 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
70}
Miklos Szeredie9be9d52014-10-24 00:14:38 +020071
72enum ovl_path_type ovl_path_type(struct dentry *dentry)
73{
74 struct ovl_entry *oe = dentry->d_fsdata;
Miklos Szeredi1afaba12014-12-13 00:59:42 +010075 enum ovl_path_type type = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020076
77 if (oe->__upperdentry) {
Miklos Szeredi1afaba12014-12-13 00:59:42 +010078 type = __OVL_PATH_UPPER;
79
Miklos Szeredidd662662014-12-13 00:59:43 +010080 if (oe->numlower) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +020081 if (S_ISDIR(dentry->d_inode->i_mode))
Miklos Szeredi1afaba12014-12-13 00:59:42 +010082 type |= __OVL_PATH_MERGE;
83 } else if (!oe->opaque) {
84 type |= __OVL_PATH_PURE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020085 }
Miklos Szeredi9d7459d2014-12-13 00:59:44 +010086 } else {
87 if (oe->numlower > 1)
88 type |= __OVL_PATH_MERGE;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020089 }
Miklos Szeredi1afaba12014-12-13 00:59:42 +010090 return type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +020091}
92
93static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
94{
Miklos Szeredi71d50922014-11-20 16:40:01 +010095 return lockless_dereference(oe->__upperdentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020096}
97
98void ovl_path_upper(struct dentry *dentry, struct path *path)
99{
100 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
101 struct ovl_entry *oe = dentry->d_fsdata;
102
103 path->mnt = ofs->upper_mnt;
104 path->dentry = ovl_upperdentry_dereference(oe);
105}
106
107enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
108{
109
110 enum ovl_path_type type = ovl_path_type(dentry);
111
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100112 if (!OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200113 ovl_path_lower(dentry, path);
114 else
115 ovl_path_upper(dentry, path);
116
117 return type;
118}
119
120struct dentry *ovl_dentry_upper(struct dentry *dentry)
121{
122 struct ovl_entry *oe = dentry->d_fsdata;
123
124 return ovl_upperdentry_dereference(oe);
125}
126
127struct dentry *ovl_dentry_lower(struct dentry *dentry)
128{
129 struct ovl_entry *oe = dentry->d_fsdata;
130
Miklos Szeredidd662662014-12-13 00:59:43 +0100131 return __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200132}
133
134struct dentry *ovl_dentry_real(struct dentry *dentry)
135{
136 struct ovl_entry *oe = dentry->d_fsdata;
137 struct dentry *realdentry;
138
139 realdentry = ovl_upperdentry_dereference(oe);
140 if (!realdentry)
Miklos Szeredidd662662014-12-13 00:59:43 +0100141 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200142
143 return realdentry;
144}
145
146struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
147{
148 struct dentry *realdentry;
149
150 realdentry = ovl_upperdentry_dereference(oe);
151 if (realdentry) {
152 *is_upper = true;
153 } else {
Miklos Szeredidd662662014-12-13 00:59:43 +0100154 realdentry = __ovl_dentry_lower(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200155 *is_upper = false;
156 }
157 return realdentry;
158}
159
160struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
161{
162 struct ovl_entry *oe = dentry->d_fsdata;
163
164 return oe->cache;
165}
166
167void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
168{
169 struct ovl_entry *oe = dentry->d_fsdata;
170
171 oe->cache = cache;
172}
173
174void ovl_path_lower(struct dentry *dentry, struct path *path)
175{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200176 struct ovl_entry *oe = dentry->d_fsdata;
177
Miklos Szeredidd662662014-12-13 00:59:43 +0100178 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200179}
180
181int ovl_want_write(struct dentry *dentry)
182{
183 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
184 return mnt_want_write(ofs->upper_mnt);
185}
186
187void ovl_drop_write(struct dentry *dentry)
188{
189 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
190 mnt_drop_write(ofs->upper_mnt);
191}
192
193struct dentry *ovl_workdir(struct dentry *dentry)
194{
195 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
196 return ofs->workdir;
197}
198
199bool ovl_dentry_is_opaque(struct dentry *dentry)
200{
201 struct ovl_entry *oe = dentry->d_fsdata;
202 return oe->opaque;
203}
204
205void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
206{
207 struct ovl_entry *oe = dentry->d_fsdata;
208 oe->opaque = opaque;
209}
210
211void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
212{
213 struct ovl_entry *oe = dentry->d_fsdata;
214
215 WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
216 WARN_ON(oe->__upperdentry);
217 BUG_ON(!upperdentry->d_inode);
218 /*
219 * Make sure upperdentry is consistent before making it visible to
220 * ovl_upperdentry_dereference().
221 */
222 smp_wmb();
223 oe->__upperdentry = upperdentry;
224}
225
226void ovl_dentry_version_inc(struct dentry *dentry)
227{
228 struct ovl_entry *oe = dentry->d_fsdata;
229
230 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
231 oe->version++;
232}
233
234u64 ovl_dentry_version_get(struct dentry *dentry)
235{
236 struct ovl_entry *oe = dentry->d_fsdata;
237
238 WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
239 return oe->version;
240}
241
242bool ovl_is_whiteout(struct dentry *dentry)
243{
244 struct inode *inode = dentry->d_inode;
245
246 return inode && IS_WHITEOUT(inode);
247}
248
249static bool ovl_is_opaquedir(struct dentry *dentry)
250{
251 int res;
252 char val;
253 struct inode *inode = dentry->d_inode;
254
255 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr)
256 return false;
257
258 res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1);
259 if (res == 1 && val == 'y')
260 return true;
261
262 return false;
263}
264
265static void ovl_dentry_release(struct dentry *dentry)
266{
267 struct ovl_entry *oe = dentry->d_fsdata;
268
269 if (oe) {
Miklos Szeredidd662662014-12-13 00:59:43 +0100270 unsigned int i;
271
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200272 dput(oe->__upperdentry);
Miklos Szeredidd662662014-12-13 00:59:43 +0100273 for (i = 0; i < oe->numlower; i++)
274 dput(oe->lowerstack[i].dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200275 kfree_rcu(oe, rcu);
276 }
277}
278
279static const struct dentry_operations ovl_dentry_operations = {
280 .d_release = ovl_dentry_release,
281};
282
Miklos Szeredidd662662014-12-13 00:59:43 +0100283static struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200284{
Miklos Szeredidd662662014-12-13 00:59:43 +0100285 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
286 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
287
288 if (oe)
289 oe->numlower = numlower;
290
291 return oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200292}
293
294static inline struct dentry *ovl_lookup_real(struct dentry *dir,
295 struct qstr *name)
296{
297 struct dentry *dentry;
298
299 mutex_lock(&dir->d_inode->i_mutex);
300 dentry = lookup_one_len(name->name, dir, name->len);
301 mutex_unlock(&dir->d_inode->i_mutex);
302
303 if (IS_ERR(dentry)) {
304 if (PTR_ERR(dentry) == -ENOENT)
305 dentry = NULL;
306 } else if (!dentry->d_inode) {
307 dput(dentry);
308 dentry = NULL;
309 }
310 return dentry;
311}
312
Miklos Szeredi5ef88da2014-12-13 00:59:43 +0100313/*
314 * Returns next layer in stack starting from top.
315 * Returns -1 if this is the last layer.
316 */
317int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
318{
319 struct ovl_entry *oe = dentry->d_fsdata;
320
321 BUG_ON(idx < 0);
322 if (idx == 0) {
323 ovl_path_upper(dentry, path);
324 if (path->dentry)
325 return oe->numlower ? 1 : -1;
326 idx++;
327 }
328 BUG_ON(idx > oe->numlower);
329 *path = oe->lowerstack[idx - 1];
330
331 return (idx < oe->numlower) ? idx + 1 : -1;
332}
333
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200334struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
335 unsigned int flags)
336{
337 struct ovl_entry *oe;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100338 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
339 struct path *stack = NULL;
340 struct dentry *upperdir, *upperdentry = NULL;
341 unsigned int ctr = 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200342 struct inode *inode = NULL;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100343 bool upperopaque = false;
344 struct dentry *this, *prev = NULL;
345 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200346 int err;
347
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100348 upperdir = ovl_upperdentry_dereference(poe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200349 if (upperdir) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100350 this = ovl_lookup_real(upperdir, &dentry->d_name);
351 err = PTR_ERR(this);
352 if (IS_ERR(this))
353 goto out;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200354
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100355 if (this) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100356 if (ovl_is_whiteout(this)) {
357 dput(this);
358 this = NULL;
359 upperopaque = true;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100360 } else if (poe->numlower && ovl_is_opaquedir(this)) {
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100361 upperopaque = true;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200362 }
363 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100364 upperdentry = prev = this;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200365 }
366
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100367 if (!upperopaque && poe->numlower) {
368 err = -ENOMEM;
369 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL);
370 if (!stack)
371 goto out_put_upper;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200372 }
373
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100374 for (i = 0; !upperopaque && i < poe->numlower; i++) {
375 bool opaque = false;
376 struct path lowerpath = poe->lowerstack[i];
377
378 opaque = false;
379 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name);
380 err = PTR_ERR(this);
Miklos Szeredi09e10322014-12-13 00:59:45 +0100381 if (IS_ERR(this)) {
382 /*
383 * If it's positive, then treat ENAMETOOLONG as ENOENT.
384 */
385 if (err == -ENAMETOOLONG && (upperdentry || ctr))
386 continue;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100387 goto out_put;
Miklos Szeredi09e10322014-12-13 00:59:45 +0100388 }
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100389 if (!this)
390 continue;
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100391 if (ovl_is_whiteout(this)) {
392 dput(this);
393 break;
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100394 }
395 /*
Miklos Szeredi3e01cee2014-12-13 00:59:45 +0100396 * Only makes sense to check opaque dir if this is not the
397 * lowermost layer.
398 */
399 if (i < poe->numlower - 1 && ovl_is_opaquedir(this))
400 opaque = true;
401 /*
Miklos Szeredi3d3c6b82014-12-13 00:59:44 +0100402 * If this is a non-directory then stop here.
403 *
404 * FIXME: check for opaqueness maybe better done in remove code.
405 */
406 if (!S_ISDIR(this->d_inode->i_mode)) {
407 opaque = true;
408 } else if (prev && (!S_ISDIR(prev->d_inode->i_mode) ||
409 !S_ISDIR(this->d_inode->i_mode))) {
410 if (prev == upperdentry)
411 upperopaque = true;
412 dput(this);
413 break;
414 }
415 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
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200528static const struct super_operations ovl_super_operations = {
529 .put_super = ovl_put_super,
Andy Whitcroftcc259632014-10-24 00:14:38 +0200530 .statfs = ovl_statfs,
Erez Zadokf45827e82014-10-24 00:14:38 +0200531 .show_options = ovl_show_options,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200532};
533
534enum {
535 OPT_LOWERDIR,
536 OPT_UPPERDIR,
537 OPT_WORKDIR,
538 OPT_ERR,
539};
540
541static const match_table_t ovl_tokens = {
542 {OPT_LOWERDIR, "lowerdir=%s"},
543 {OPT_UPPERDIR, "upperdir=%s"},
544 {OPT_WORKDIR, "workdir=%s"},
545 {OPT_ERR, NULL}
546};
547
Miklos Szeredi91c77942014-11-20 16:40:00 +0100548static char *ovl_next_opt(char **s)
549{
550 char *sbegin = *s;
551 char *p;
552
553 if (sbegin == NULL)
554 return NULL;
555
556 for (p = sbegin; *p; p++) {
557 if (*p == '\\') {
558 p++;
559 if (!*p)
560 break;
561 } else if (*p == ',') {
562 *p = '\0';
563 *s = p + 1;
564 return sbegin;
565 }
566 }
567 *s = NULL;
568 return sbegin;
569}
570
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200571static int ovl_parse_opt(char *opt, struct ovl_config *config)
572{
573 char *p;
574
Miklos Szeredi91c77942014-11-20 16:40:00 +0100575 while ((p = ovl_next_opt(&opt)) != NULL) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200576 int token;
577 substring_t args[MAX_OPT_ARGS];
578
579 if (!*p)
580 continue;
581
582 token = match_token(p, ovl_tokens, args);
583 switch (token) {
584 case OPT_UPPERDIR:
585 kfree(config->upperdir);
586 config->upperdir = match_strdup(&args[0]);
587 if (!config->upperdir)
588 return -ENOMEM;
589 break;
590
591 case OPT_LOWERDIR:
592 kfree(config->lowerdir);
593 config->lowerdir = match_strdup(&args[0]);
594 if (!config->lowerdir)
595 return -ENOMEM;
596 break;
597
598 case OPT_WORKDIR:
599 kfree(config->workdir);
600 config->workdir = match_strdup(&args[0]);
601 if (!config->workdir)
602 return -ENOMEM;
603 break;
604
605 default:
606 return -EINVAL;
607 }
608 }
609 return 0;
610}
611
612#define OVL_WORKDIR_NAME "work"
613
614static struct dentry *ovl_workdir_create(struct vfsmount *mnt,
615 struct dentry *dentry)
616{
617 struct inode *dir = dentry->d_inode;
618 struct dentry *work;
619 int err;
620 bool retried = false;
621
622 err = mnt_want_write(mnt);
623 if (err)
624 return ERR_PTR(err);
625
626 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
627retry:
628 work = lookup_one_len(OVL_WORKDIR_NAME, dentry,
629 strlen(OVL_WORKDIR_NAME));
630
631 if (!IS_ERR(work)) {
632 struct kstat stat = {
633 .mode = S_IFDIR | 0,
634 };
635
636 if (work->d_inode) {
637 err = -EEXIST;
638 if (retried)
639 goto out_dput;
640
641 retried = true;
642 ovl_cleanup(dir, work);
643 dput(work);
644 goto retry;
645 }
646
647 err = ovl_create_real(dir, work, &stat, NULL, NULL, true);
648 if (err)
649 goto out_dput;
650 }
651out_unlock:
652 mutex_unlock(&dir->i_mutex);
653 mnt_drop_write(mnt);
654
655 return work;
656
657out_dput:
658 dput(work);
659 work = ERR_PTR(err);
660 goto out_unlock;
661}
662
Miklos Szeredi91c77942014-11-20 16:40:00 +0100663static void ovl_unescape(char *s)
664{
665 char *d = s;
666
667 for (;; s++, d++) {
668 if (*s == '\\')
669 s++;
670 *d = *s;
671 if (!*s)
672 break;
673 }
674}
675
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200676static bool ovl_is_allowed_fs_type(struct dentry *root)
677{
678 const struct dentry_operations *dop = root->d_op;
679
680 /*
681 * We don't support:
682 * - automount filesystems
683 * - filesystems with revalidate (FIXME for lower layer)
684 * - filesystems with case insensitive names
685 */
686 if (dop &&
687 (dop->d_manage || dop->d_automount ||
688 dop->d_revalidate || dop->d_weak_revalidate ||
689 dop->d_compare || dop->d_hash)) {
690 return false;
691 }
692 return true;
693}
694
Miklos Szerediab508822014-12-13 00:59:49 +0100695static int ovl_mount_dir_noesc(const char *name, struct path *path)
696{
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100697 int err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100698
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100699 if (!*name) {
700 pr_err("overlayfs: empty lowerdir\n");
701 goto out;
702 }
Miklos Szerediab508822014-12-13 00:59:49 +0100703 err = kern_path(name, LOOKUP_FOLLOW, path);
704 if (err) {
705 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
706 goto out;
707 }
708 err = -EINVAL;
709 if (!ovl_is_allowed_fs_type(path->dentry)) {
710 pr_err("overlayfs: filesystem on '%s' not supported\n", name);
711 goto out_put;
712 }
713 if (!S_ISDIR(path->dentry->d_inode->i_mode)) {
714 pr_err("overlayfs: '%s' not a directory\n", name);
715 goto out_put;
716 }
717 return 0;
718
719out_put:
720 path_put(path);
721out:
722 return err;
723}
724
725static int ovl_mount_dir(const char *name, struct path *path)
726{
727 int err = -ENOMEM;
728 char *tmp = kstrdup(name, GFP_KERNEL);
729
730 if (tmp) {
731 ovl_unescape(tmp);
732 err = ovl_mount_dir_noesc(tmp, path);
733 kfree(tmp);
734 }
735 return err;
736}
737
738static int ovl_lower_dir(const char *name, struct path *path, long *namelen,
739 int *stack_depth)
740{
741 int err;
742 struct kstatfs statfs;
743
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100744 err = ovl_mount_dir_noesc(name, path);
Miklos Szerediab508822014-12-13 00:59:49 +0100745 if (err)
746 goto out;
747
748 err = vfs_statfs(path, &statfs);
749 if (err) {
750 pr_err("overlayfs: statfs failed on '%s'\n", name);
751 goto out_put;
752 }
753 *namelen = max(*namelen, statfs.f_namelen);
754 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
755
756 return 0;
757
758out_put:
759 path_put(path);
760out:
761 return err;
762}
763
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200764/* Workdir should not be subdir of upperdir and vice versa */
765static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
766{
767 bool ok = false;
768
769 if (workdir != upperdir) {
770 ok = (lock_rename(workdir, upperdir) == NULL);
771 unlock_rename(workdir, upperdir);
772 }
773 return ok;
774}
775
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100776static unsigned int ovl_split_lowerdirs(char *str)
777{
778 unsigned int ctr = 1;
779 char *s, *d;
780
781 for (s = d = str;; s++, d++) {
782 if (*s == '\\') {
783 s++;
784 } else if (*s == ':') {
785 *d = '\0';
786 ctr++;
787 continue;
788 }
789 *d = *s;
790 if (!*s)
791 break;
792 }
793 return ctr;
794}
795
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200796static int ovl_fill_super(struct super_block *sb, void *data, int silent)
797{
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100798 struct path upperpath = { NULL, NULL };
799 struct path workpath = { NULL, NULL };
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200800 struct dentry *root_dentry;
801 struct ovl_entry *oe;
802 struct ovl_fs *ufs;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100803 struct path *stack = NULL;
804 char *lowertmp;
805 char *lower;
806 unsigned int numlower;
807 unsigned int stacklen = 0;
Miklos Szeredidd662662014-12-13 00:59:43 +0100808 unsigned int i;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200809 int err;
810
Erez Zadokf45827e82014-10-24 00:14:38 +0200811 err = -ENOMEM;
812 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
813 if (!ufs)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200814 goto out;
815
Erez Zadokf45827e82014-10-24 00:14:38 +0200816 err = ovl_parse_opt((char *) data, &ufs->config);
817 if (err)
818 goto out_free_config;
819
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200820 err = -EINVAL;
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100821 if (!ufs->config.lowerdir) {
822 pr_err("overlayfs: missing 'lowerdir'\n");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200823 goto out_free_config;
824 }
825
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100826 sb->s_stack_depth = 0;
827 if (ufs->config.upperdir) {
828 /* FIXME: workdir is not needed for a R/O mount */
829 if (!ufs->config.workdir) {
830 pr_err("overlayfs: missing 'workdir'\n");
831 goto out_free_config;
832 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200833
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100834 err = ovl_mount_dir(ufs->config.upperdir, &upperpath);
835 if (err)
836 goto out_free_config;
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100837
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100838 err = ovl_mount_dir(ufs->config.workdir, &workpath);
839 if (err)
840 goto out_put_upperpath;
841
842 if (upperpath.mnt != workpath.mnt) {
843 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
844 goto out_put_workpath;
845 }
846 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) {
847 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
848 goto out_put_workpath;
849 }
850 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth;
Miklos Szerediab508822014-12-13 00:59:49 +0100851 }
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100852 err = -ENOMEM;
853 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL);
854 if (!lowertmp)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100855 goto out_put_workpath;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200856
857 err = -EINVAL;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100858 stacklen = ovl_split_lowerdirs(lowertmp);
859 if (stacklen > OVL_MAX_STACK)
860 goto out_free_lowertmp;
861
862 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
863 if (!stack)
864 goto out_free_lowertmp;
865
866 lower = lowertmp;
867 for (numlower = 0; numlower < stacklen; numlower++) {
868 err = ovl_lower_dir(lower, &stack[numlower],
869 &ufs->lower_namelen, &sb->s_stack_depth);
870 if (err)
871 goto out_put_lowerpath;
872
873 lower = strchr(lower, '\0') + 1;
874 }
875
876 err = -EINVAL;
Miklos Szerediab508822014-12-13 00:59:49 +0100877 sb->s_stack_depth++;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200878 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
879 pr_err("overlayfs: maximum fs stacking depth exceeded\n");
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100880 goto out_put_lowerpath;
Miklos Szeredi69c433e2014-10-24 00:14:39 +0200881 }
882
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100883 if (ufs->config.upperdir) {
884 ufs->upper_mnt = clone_private_mount(&upperpath);
885 err = PTR_ERR(ufs->upper_mnt);
886 if (IS_ERR(ufs->upper_mnt)) {
887 pr_err("overlayfs: failed to clone upperpath\n");
888 goto out_put_lowerpath;
889 }
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100890
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100891 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry);
892 err = PTR_ERR(ufs->workdir);
893 if (IS_ERR(ufs->workdir)) {
894 pr_err("overlayfs: failed to create directory %s/%s\n",
895 ufs->config.workdir, OVL_WORKDIR_NAME);
896 goto out_put_upper_mnt;
897 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200898 }
899
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100900 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL);
Miklos Szeredidd662662014-12-13 00:59:43 +0100901 if (ufs->lower_mnt == NULL)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100902 goto out_put_workdir;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100903 for (i = 0; i < numlower; i++) {
904 struct vfsmount *mnt = clone_private_mount(&stack[i]);
Miklos Szeredidd662662014-12-13 00:59:43 +0100905
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100906 if (IS_ERR(mnt)) {
907 pr_err("overlayfs: failed to clone lowerpath\n");
908 goto out_put_lower_mnt;
909 }
910 /*
911 * Make lower_mnt R/O. That way fchmod/fchown on lower file
912 * will fail instead of modifying lower fs.
913 */
914 mnt->mnt_flags |= MNT_READONLY;
915
916 ufs->lower_mnt[ufs->numlower] = mnt;
917 ufs->numlower++;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200918 }
919
Miklos Szeredi53a08cb2014-12-13 00:59:51 +0100920 /* If the upper fs is r/o or nonexistent, we mark overlayfs r/o too */
921 if (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200922 sb->s_flags |= MS_RDONLY;
923
924 sb->s_d_op = &ovl_dentry_operations;
925
926 err = -ENOMEM;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100927 oe = ovl_alloc_entry(numlower);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100928 if (!oe)
929 goto out_put_lower_mnt;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200930
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100931 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200932 if (!root_dentry)
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100933 goto out_free_oe;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200934
935 mntput(upperpath.mnt);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100936 for (i = 0; i < numlower; i++)
937 mntput(stack[i].mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200938 path_put(&workpath);
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100939 kfree(lowertmp);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200940
941 oe->__upperdentry = upperpath.dentry;
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100942 for (i = 0; i < numlower; i++) {
943 oe->lowerstack[i].dentry = stack[i].dentry;
944 oe->lowerstack[i].mnt = ufs->lower_mnt[i];
945 }
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200946
947 root_dentry->d_fsdata = oe;
948
Andy Whitcroftcc259632014-10-24 00:14:38 +0200949 sb->s_magic = OVERLAYFS_SUPER_MAGIC;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200950 sb->s_op = &ovl_super_operations;
951 sb->s_root = root_dentry;
952 sb->s_fs_info = ufs;
953
954 return 0;
955
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100956out_free_oe:
957 kfree(oe);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200958out_put_lower_mnt:
Miklos Szeredidd662662014-12-13 00:59:43 +0100959 for (i = 0; i < ufs->numlower; i++)
960 mntput(ufs->lower_mnt[i]);
961 kfree(ufs->lower_mnt);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100962out_put_workdir:
963 dput(ufs->workdir);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200964out_put_upper_mnt:
965 mntput(ufs->upper_mnt);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200966out_put_lowerpath:
Miklos Szeredia78d9f02014-12-13 00:59:52 +0100967 for (i = 0; i < numlower; i++)
968 path_put(&stack[i]);
969 kfree(stack);
970out_free_lowertmp:
971 kfree(lowertmp);
Miklos Szeredi3b7a9a22014-12-13 00:59:48 +0100972out_put_workpath:
973 path_put(&workpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200974out_put_upperpath:
975 path_put(&upperpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200976out_free_config:
Erez Zadokf45827e82014-10-24 00:14:38 +0200977 kfree(ufs->config.lowerdir);
978 kfree(ufs->config.upperdir);
979 kfree(ufs->config.workdir);
980 kfree(ufs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200981out:
982 return err;
983}
984
985static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
986 const char *dev_name, void *raw_data)
987{
988 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
989}
990
991static struct file_system_type ovl_fs_type = {
992 .owner = THIS_MODULE,
Miklos Szeredief94b182014-11-20 16:39:59 +0100993 .name = "overlay",
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200994 .mount = ovl_mount,
995 .kill_sb = kill_anon_super,
996};
Miklos Szeredief94b182014-11-20 16:39:59 +0100997MODULE_ALIAS_FS("overlay");
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200998
999static int __init ovl_init(void)
1000{
1001 return register_filesystem(&ovl_fs_type);
1002}
1003
1004static void __exit ovl_exit(void)
1005{
1006 unregister_filesystem(&ovl_fs_type);
1007}
1008
1009module_init(ovl_init);
1010module_exit(ovl_exit);