Commit | Line | Data |
---|---|---|
e9be9d5e MS |
1 | /* |
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> | |
cc259639 | 19 | #include <linux/statfs.h> |
f45827e8 | 20 | #include <linux/seq_file.h> |
e9be9d5e MS |
21 | #include "overlayfs.h" |
22 | ||
23 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); | |
24 | MODULE_DESCRIPTION("Overlay filesystem"); | |
25 | MODULE_LICENSE("GPL"); | |
26 | ||
ef94b186 | 27 | #define OVERLAYFS_SUPER_MAGIC 0x794c7630 |
cc259639 | 28 | |
f45827e8 EZ |
29 | struct ovl_config { |
30 | char *lowerdir; | |
31 | char *upperdir; | |
32 | char *workdir; | |
33 | }; | |
34 | ||
e9be9d5e MS |
35 | /* private information held for overlayfs's superblock */ |
36 | struct ovl_fs { | |
37 | struct vfsmount *upper_mnt; | |
dd662667 MS |
38 | unsigned numlower; |
39 | struct vfsmount **lower_mnt; | |
e9be9d5e | 40 | struct dentry *workdir; |
cc259639 | 41 | long lower_namelen; |
f45827e8 EZ |
42 | /* pathnames of lower and upper dirs, for show_options */ |
43 | struct ovl_config config; | |
e9be9d5e MS |
44 | }; |
45 | ||
46 | struct ovl_dir_cache; | |
47 | ||
48 | /* private information held for every overlayfs dentry */ | |
49 | struct ovl_entry { | |
50 | struct dentry *__upperdentry; | |
e9be9d5e MS |
51 | struct ovl_dir_cache *cache; |
52 | union { | |
53 | struct { | |
54 | u64 version; | |
55 | bool opaque; | |
56 | }; | |
57 | struct rcu_head rcu; | |
58 | }; | |
dd662667 MS |
59 | unsigned numlower; |
60 | struct path lowerstack[]; | |
e9be9d5e MS |
61 | }; |
62 | ||
a78d9f0d MS |
63 | #define OVL_MAX_STACK 500 |
64 | ||
dd662667 MS |
65 | static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe) |
66 | { | |
67 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; | |
68 | } | |
e9be9d5e MS |
69 | |
70 | enum ovl_path_type ovl_path_type(struct dentry *dentry) | |
71 | { | |
72 | struct ovl_entry *oe = dentry->d_fsdata; | |
1afaba1e | 73 | enum ovl_path_type type = 0; |
e9be9d5e MS |
74 | |
75 | if (oe->__upperdentry) { | |
1afaba1e MS |
76 | type = __OVL_PATH_UPPER; |
77 | ||
dd662667 | 78 | if (oe->numlower) { |
e9be9d5e | 79 | if (S_ISDIR(dentry->d_inode->i_mode)) |
1afaba1e MS |
80 | type |= __OVL_PATH_MERGE; |
81 | } else if (!oe->opaque) { | |
82 | type |= __OVL_PATH_PURE; | |
e9be9d5e | 83 | } |
9d7459d8 MS |
84 | } else { |
85 | if (oe->numlower > 1) | |
86 | type |= __OVL_PATH_MERGE; | |
e9be9d5e | 87 | } |
1afaba1e | 88 | return type; |
e9be9d5e MS |
89 | } |
90 | ||
91 | static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) | |
92 | { | |
71d50928 | 93 | return lockless_dereference(oe->__upperdentry); |
e9be9d5e MS |
94 | } |
95 | ||
96 | void 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 | ||
105 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) | |
106 | { | |
e9be9d5e MS |
107 | enum ovl_path_type type = ovl_path_type(dentry); |
108 | ||
1afaba1e | 109 | if (!OVL_TYPE_UPPER(type)) |
e9be9d5e MS |
110 | ovl_path_lower(dentry, path); |
111 | else | |
112 | ovl_path_upper(dentry, path); | |
113 | ||
114 | return type; | |
115 | } | |
116 | ||
117 | struct 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 | ||
124 | struct dentry *ovl_dentry_lower(struct dentry *dentry) | |
125 | { | |
126 | struct ovl_entry *oe = dentry->d_fsdata; | |
127 | ||
dd662667 | 128 | return __ovl_dentry_lower(oe); |
e9be9d5e MS |
129 | } |
130 | ||
131 | struct 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) | |
dd662667 | 138 | realdentry = __ovl_dentry_lower(oe); |
e9be9d5e MS |
139 | |
140 | return realdentry; | |
141 | } | |
142 | ||
143 | struct 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 { | |
dd662667 | 151 | realdentry = __ovl_dentry_lower(oe); |
e9be9d5e MS |
152 | *is_upper = false; |
153 | } | |
154 | return realdentry; | |
155 | } | |
156 | ||
157 | struct 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 | ||
164 | void 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 | ||
171 | void ovl_path_lower(struct dentry *dentry, struct path *path) | |
172 | { | |
e9be9d5e MS |
173 | struct ovl_entry *oe = dentry->d_fsdata; |
174 | ||
dd662667 | 175 | *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; |
e9be9d5e MS |
176 | } |
177 | ||
178 | int 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 | ||
184 | void 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 | ||
190 | struct dentry *ovl_workdir(struct dentry *dentry) | |
191 | { | |
192 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
193 | return ofs->workdir; | |
194 | } | |
195 | ||
196 | bool ovl_dentry_is_opaque(struct dentry *dentry) | |
197 | { | |
198 | struct ovl_entry *oe = dentry->d_fsdata; | |
199 | return oe->opaque; | |
200 | } | |
201 | ||
202 | void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) | |
203 | { | |
204 | struct ovl_entry *oe = dentry->d_fsdata; | |
205 | oe->opaque = opaque; | |
206 | } | |
207 | ||
208 | void 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 | ||
223 | void 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 | ||
231 | u64 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 | ||
239 | bool ovl_is_whiteout(struct dentry *dentry) | |
240 | { | |
241 | struct inode *inode = dentry->d_inode; | |
242 | ||
243 | return inode && IS_WHITEOUT(inode); | |
244 | } | |
245 | ||
246 | static 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 | ||
cead89bb | 255 | res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1); |
e9be9d5e MS |
256 | if (res == 1 && val == 'y') |
257 | return true; | |
258 | ||
259 | return false; | |
260 | } | |
261 | ||
262 | static void ovl_dentry_release(struct dentry *dentry) | |
263 | { | |
264 | struct ovl_entry *oe = dentry->d_fsdata; | |
265 | ||
266 | if (oe) { | |
dd662667 MS |
267 | unsigned int i; |
268 | ||
e9be9d5e | 269 | dput(oe->__upperdentry); |
dd662667 MS |
270 | for (i = 0; i < oe->numlower; i++) |
271 | dput(oe->lowerstack[i].dentry); | |
e9be9d5e MS |
272 | kfree_rcu(oe, rcu); |
273 | } | |
274 | } | |
275 | ||
276 | static const struct dentry_operations ovl_dentry_operations = { | |
277 | .d_release = ovl_dentry_release, | |
278 | }; | |
279 | ||
dd662667 | 280 | static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
e9be9d5e | 281 | { |
dd662667 MS |
282 | 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; | |
e9be9d5e MS |
289 | } |
290 | ||
291 | static 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; | |
a6f15d9a MS |
306 | } else if (dentry->d_flags & DCACHE_MANAGED_DENTRY) { |
307 | dput(dentry); | |
308 | /* Don't support traversing automounts */ | |
309 | dentry = ERR_PTR(-EREMOTE); | |
e9be9d5e MS |
310 | } |
311 | return dentry; | |
312 | } | |
313 | ||
5ef88da5 MS |
314 | /* |
315 | * Returns next layer in stack starting from top. | |
316 | * Returns -1 if this is the last layer. | |
317 | */ | |
318 | int ovl_path_next(int idx, struct dentry *dentry, struct path *path) | |
319 | { | |
320 | struct ovl_entry *oe = dentry->d_fsdata; | |
321 | ||
322 | BUG_ON(idx < 0); | |
323 | if (idx == 0) { | |
324 | ovl_path_upper(dentry, path); | |
325 | if (path->dentry) | |
326 | return oe->numlower ? 1 : -1; | |
327 | idx++; | |
328 | } | |
329 | BUG_ON(idx > oe->numlower); | |
330 | *path = oe->lowerstack[idx - 1]; | |
331 | ||
332 | return (idx < oe->numlower) ? idx + 1 : -1; | |
333 | } | |
334 | ||
e9be9d5e MS |
335 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
336 | unsigned int flags) | |
337 | { | |
338 | struct ovl_entry *oe; | |
3d3c6b89 MS |
339 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
340 | struct path *stack = NULL; | |
341 | struct dentry *upperdir, *upperdentry = NULL; | |
342 | unsigned int ctr = 0; | |
e9be9d5e | 343 | struct inode *inode = NULL; |
3d3c6b89 MS |
344 | bool upperopaque = false; |
345 | struct dentry *this, *prev = NULL; | |
346 | unsigned int i; | |
e9be9d5e MS |
347 | int err; |
348 | ||
3d3c6b89 | 349 | upperdir = ovl_upperdentry_dereference(poe); |
e9be9d5e | 350 | if (upperdir) { |
3d3c6b89 MS |
351 | this = ovl_lookup_real(upperdir, &dentry->d_name); |
352 | err = PTR_ERR(this); | |
353 | if (IS_ERR(this)) | |
354 | goto out; | |
355 | ||
3e01cee3 | 356 | if (this) { |
3d3c6b89 MS |
357 | if (ovl_is_whiteout(this)) { |
358 | dput(this); | |
359 | this = NULL; | |
360 | upperopaque = true; | |
3e01cee3 | 361 | } else if (poe->numlower && ovl_is_opaquedir(this)) { |
3d3c6b89 | 362 | upperopaque = true; |
e9be9d5e MS |
363 | } |
364 | } | |
3d3c6b89 | 365 | upperdentry = prev = this; |
e9be9d5e | 366 | } |
3d3c6b89 MS |
367 | |
368 | if (!upperopaque && poe->numlower) { | |
369 | err = -ENOMEM; | |
370 | stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL); | |
371 | if (!stack) | |
372 | goto out_put_upper; | |
e9be9d5e MS |
373 | } |
374 | ||
3d3c6b89 MS |
375 | for (i = 0; !upperopaque && i < poe->numlower; i++) { |
376 | bool opaque = false; | |
377 | struct path lowerpath = poe->lowerstack[i]; | |
378 | ||
3d3c6b89 MS |
379 | this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name); |
380 | err = PTR_ERR(this); | |
09e10322 MS |
381 | if (IS_ERR(this)) { |
382 | /* | |
383 | * If it's positive, then treat ENAMETOOLONG as ENOENT. | |
384 | */ | |
385 | if (err == -ENAMETOOLONG && (upperdentry || ctr)) | |
386 | continue; | |
3d3c6b89 | 387 | goto out_put; |
09e10322 | 388 | } |
3d3c6b89 MS |
389 | if (!this) |
390 | continue; | |
3e01cee3 MS |
391 | if (ovl_is_whiteout(this)) { |
392 | dput(this); | |
393 | break; | |
394 | } | |
3d3c6b89 | 395 | /* |
3e01cee3 MS |
396 | * Only makes sense to check opaque dir if this is not the |
397 | * lowermost layer. | |
3d3c6b89 | 398 | */ |
3e01cee3 MS |
399 | if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) |
400 | opaque = true; | |
a425c037 | 401 | |
402 | if (prev && (!S_ISDIR(prev->d_inode->i_mode) || | |
403 | !S_ISDIR(this->d_inode->i_mode))) { | |
404 | /* | |
405 | * FIXME: check for upper-opaqueness maybe better done | |
406 | * in remove code. | |
407 | */ | |
3d3c6b89 MS |
408 | if (prev == upperdentry) |
409 | upperopaque = true; | |
410 | dput(this); | |
411 | break; | |
412 | } | |
a425c037 | 413 | /* |
414 | * If this is a non-directory then stop here. | |
415 | */ | |
416 | if (!S_ISDIR(this->d_inode->i_mode)) | |
417 | opaque = true; | |
418 | ||
3d3c6b89 MS |
419 | stack[ctr].dentry = this; |
420 | stack[ctr].mnt = lowerpath.mnt; | |
421 | ctr++; | |
422 | prev = this; | |
423 | if (opaque) | |
424 | break; | |
e9be9d5e MS |
425 | } |
426 | ||
3d3c6b89 MS |
427 | oe = ovl_alloc_entry(ctr); |
428 | err = -ENOMEM; | |
429 | if (!oe) | |
430 | goto out_put; | |
431 | ||
432 | if (upperdentry || ctr) { | |
e9be9d5e MS |
433 | struct dentry *realdentry; |
434 | ||
3d3c6b89 MS |
435 | realdentry = upperdentry ? upperdentry : stack[0].dentry; |
436 | ||
e9be9d5e MS |
437 | err = -ENOMEM; |
438 | inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, | |
439 | oe); | |
440 | if (!inode) | |
3d3c6b89 | 441 | goto out_free_oe; |
e9be9d5e MS |
442 | ovl_copyattr(realdentry->d_inode, inode); |
443 | } | |
444 | ||
3d3c6b89 | 445 | oe->opaque = upperopaque; |
e9be9d5e | 446 | oe->__upperdentry = upperdentry; |
3d3c6b89 MS |
447 | memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); |
448 | kfree(stack); | |
e9be9d5e MS |
449 | dentry->d_fsdata = oe; |
450 | d_add(dentry, inode); | |
451 | ||
452 | return NULL; | |
453 | ||
3d3c6b89 | 454 | out_free_oe: |
e9be9d5e | 455 | kfree(oe); |
3d3c6b89 MS |
456 | out_put: |
457 | for (i = 0; i < ctr; i++) | |
458 | dput(stack[i].dentry); | |
459 | kfree(stack); | |
460 | out_put_upper: | |
461 | dput(upperdentry); | |
e9be9d5e MS |
462 | out: |
463 | return ERR_PTR(err); | |
464 | } | |
465 | ||
466 | struct file *ovl_path_open(struct path *path, int flags) | |
467 | { | |
468 | return dentry_open(path, flags, current_cred()); | |
469 | } | |
470 | ||
471 | static void ovl_put_super(struct super_block *sb) | |
472 | { | |
473 | struct ovl_fs *ufs = sb->s_fs_info; | |
dd662667 | 474 | unsigned i; |
e9be9d5e MS |
475 | |
476 | dput(ufs->workdir); | |
477 | mntput(ufs->upper_mnt); | |
dd662667 MS |
478 | for (i = 0; i < ufs->numlower; i++) |
479 | mntput(ufs->lower_mnt[i]); | |
e9be9d5e | 480 | |
f45827e8 EZ |
481 | kfree(ufs->config.lowerdir); |
482 | kfree(ufs->config.upperdir); | |
483 | kfree(ufs->config.workdir); | |
e9be9d5e MS |
484 | kfree(ufs); |
485 | } | |
486 | ||
cc259639 AW |
487 | /** |
488 | * ovl_statfs | |
489 | * @sb: The overlayfs super block | |
490 | * @buf: The struct kstatfs to fill in with stats | |
491 | * | |
492 | * Get the filesystem statistics. As writes always target the upper layer | |
4ebc5818 | 493 | * filesystem pass the statfs to the upper filesystem (if it exists) |
cc259639 AW |
494 | */ |
495 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) | |
496 | { | |
497 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
498 | struct dentry *root_dentry = dentry->d_sb->s_root; | |
499 | struct path path; | |
500 | int err; | |
501 | ||
4ebc5818 | 502 | ovl_path_real(root_dentry, &path); |
cc259639 AW |
503 | |
504 | err = vfs_statfs(&path, buf); | |
505 | if (!err) { | |
506 | buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); | |
507 | buf->f_type = OVERLAYFS_SUPER_MAGIC; | |
508 | } | |
509 | ||
510 | return err; | |
511 | } | |
512 | ||
f45827e8 EZ |
513 | /** |
514 | * ovl_show_options | |
515 | * | |
516 | * Prints the mount options for a given superblock. | |
517 | * Returns zero; does not fail. | |
518 | */ | |
519 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) | |
520 | { | |
521 | struct super_block *sb = dentry->d_sb; | |
522 | struct ovl_fs *ufs = sb->s_fs_info; | |
523 | ||
524 | seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir); | |
53a08cb9 MS |
525 | if (ufs->config.upperdir) { |
526 | seq_printf(m, ",upperdir=%s", ufs->config.upperdir); | |
527 | seq_printf(m, ",workdir=%s", ufs->config.workdir); | |
528 | } | |
f45827e8 EZ |
529 | return 0; |
530 | } | |
531 | ||
3cdf6fe9 SL |
532 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
533 | { | |
534 | struct ovl_fs *ufs = sb->s_fs_info; | |
535 | ||
cc6f67bc | 536 | if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir)) |
3cdf6fe9 SL |
537 | return -EROFS; |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
e9be9d5e MS |
542 | static const struct super_operations ovl_super_operations = { |
543 | .put_super = ovl_put_super, | |
cc259639 | 544 | .statfs = ovl_statfs, |
f45827e8 | 545 | .show_options = ovl_show_options, |
3cdf6fe9 | 546 | .remount_fs = ovl_remount, |
e9be9d5e MS |
547 | }; |
548 | ||
549 | enum { | |
550 | OPT_LOWERDIR, | |
551 | OPT_UPPERDIR, | |
552 | OPT_WORKDIR, | |
553 | OPT_ERR, | |
554 | }; | |
555 | ||
556 | static const match_table_t ovl_tokens = { | |
557 | {OPT_LOWERDIR, "lowerdir=%s"}, | |
558 | {OPT_UPPERDIR, "upperdir=%s"}, | |
559 | {OPT_WORKDIR, "workdir=%s"}, | |
560 | {OPT_ERR, NULL} | |
561 | }; | |
562 | ||
91c77947 MS |
563 | static char *ovl_next_opt(char **s) |
564 | { | |
565 | char *sbegin = *s; | |
566 | char *p; | |
567 | ||
568 | if (sbegin == NULL) | |
569 | return NULL; | |
570 | ||
571 | for (p = sbegin; *p; p++) { | |
572 | if (*p == '\\') { | |
573 | p++; | |
574 | if (!*p) | |
575 | break; | |
576 | } else if (*p == ',') { | |
577 | *p = '\0'; | |
578 | *s = p + 1; | |
579 | return sbegin; | |
580 | } | |
581 | } | |
582 | *s = NULL; | |
583 | return sbegin; | |
584 | } | |
585 | ||
e9be9d5e MS |
586 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
587 | { | |
588 | char *p; | |
589 | ||
91c77947 | 590 | while ((p = ovl_next_opt(&opt)) != NULL) { |
e9be9d5e MS |
591 | int token; |
592 | substring_t args[MAX_OPT_ARGS]; | |
593 | ||
594 | if (!*p) | |
595 | continue; | |
596 | ||
597 | token = match_token(p, ovl_tokens, args); | |
598 | switch (token) { | |
599 | case OPT_UPPERDIR: | |
600 | kfree(config->upperdir); | |
601 | config->upperdir = match_strdup(&args[0]); | |
602 | if (!config->upperdir) | |
603 | return -ENOMEM; | |
604 | break; | |
605 | ||
606 | case OPT_LOWERDIR: | |
607 | kfree(config->lowerdir); | |
608 | config->lowerdir = match_strdup(&args[0]); | |
609 | if (!config->lowerdir) | |
610 | return -ENOMEM; | |
611 | break; | |
612 | ||
613 | case OPT_WORKDIR: | |
614 | kfree(config->workdir); | |
615 | config->workdir = match_strdup(&args[0]); | |
616 | if (!config->workdir) | |
617 | return -ENOMEM; | |
618 | break; | |
619 | ||
620 | default: | |
bead55ef | 621 | pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); |
e9be9d5e MS |
622 | return -EINVAL; |
623 | } | |
624 | } | |
71cbad7e | 625 | |
626 | /* Workdir is useless in non-upper mount */ | |
627 | if (!config->upperdir && config->workdir) { | |
628 | pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", | |
629 | config->workdir); | |
630 | kfree(config->workdir); | |
631 | config->workdir = NULL; | |
632 | } | |
633 | ||
e9be9d5e MS |
634 | return 0; |
635 | } | |
636 | ||
637 | #define OVL_WORKDIR_NAME "work" | |
638 | ||
639 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, | |
640 | struct dentry *dentry) | |
641 | { | |
642 | struct inode *dir = dentry->d_inode; | |
643 | struct dentry *work; | |
644 | int err; | |
645 | bool retried = false; | |
646 | ||
647 | err = mnt_want_write(mnt); | |
648 | if (err) | |
649 | return ERR_PTR(err); | |
650 | ||
651 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); | |
652 | retry: | |
653 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, | |
654 | strlen(OVL_WORKDIR_NAME)); | |
655 | ||
656 | if (!IS_ERR(work)) { | |
657 | struct kstat stat = { | |
658 | .mode = S_IFDIR | 0, | |
659 | }; | |
660 | ||
661 | if (work->d_inode) { | |
662 | err = -EEXIST; | |
663 | if (retried) | |
664 | goto out_dput; | |
665 | ||
666 | retried = true; | |
667 | ovl_cleanup(dir, work); | |
668 | dput(work); | |
669 | goto retry; | |
670 | } | |
671 | ||
672 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); | |
673 | if (err) | |
674 | goto out_dput; | |
675 | } | |
676 | out_unlock: | |
677 | mutex_unlock(&dir->i_mutex); | |
678 | mnt_drop_write(mnt); | |
679 | ||
680 | return work; | |
681 | ||
682 | out_dput: | |
683 | dput(work); | |
684 | work = ERR_PTR(err); | |
685 | goto out_unlock; | |
686 | } | |
687 | ||
91c77947 MS |
688 | static void ovl_unescape(char *s) |
689 | { | |
690 | char *d = s; | |
691 | ||
692 | for (;; s++, d++) { | |
693 | if (*s == '\\') | |
694 | s++; | |
695 | *d = *s; | |
696 | if (!*s) | |
697 | break; | |
698 | } | |
699 | } | |
700 | ||
e9be9d5e MS |
701 | static bool ovl_is_allowed_fs_type(struct dentry *root) |
702 | { | |
703 | const struct dentry_operations *dop = root->d_op; | |
704 | ||
705 | /* | |
706 | * We don't support: | |
a6f15d9a | 707 | * - autofs |
e9be9d5e MS |
708 | * - filesystems with revalidate (FIXME for lower layer) |
709 | * - filesystems with case insensitive names | |
710 | */ | |
711 | if (dop && | |
a6f15d9a | 712 | (dop->d_manage || |
e9be9d5e MS |
713 | dop->d_revalidate || dop->d_weak_revalidate || |
714 | dop->d_compare || dop->d_hash)) { | |
715 | return false; | |
716 | } | |
717 | return true; | |
718 | } | |
719 | ||
ab508822 MS |
720 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
721 | { | |
a78d9f0d | 722 | int err = -EINVAL; |
ab508822 | 723 | |
a78d9f0d MS |
724 | if (!*name) { |
725 | pr_err("overlayfs: empty lowerdir\n"); | |
726 | goto out; | |
727 | } | |
ab508822 MS |
728 | err = kern_path(name, LOOKUP_FOLLOW, path); |
729 | if (err) { | |
730 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); | |
731 | goto out; | |
732 | } | |
733 | err = -EINVAL; | |
734 | if (!ovl_is_allowed_fs_type(path->dentry)) { | |
735 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); | |
736 | goto out_put; | |
737 | } | |
738 | if (!S_ISDIR(path->dentry->d_inode->i_mode)) { | |
739 | pr_err("overlayfs: '%s' not a directory\n", name); | |
740 | goto out_put; | |
741 | } | |
742 | return 0; | |
743 | ||
744 | out_put: | |
745 | path_put(path); | |
746 | out: | |
747 | return err; | |
748 | } | |
749 | ||
750 | static int ovl_mount_dir(const char *name, struct path *path) | |
751 | { | |
752 | int err = -ENOMEM; | |
753 | char *tmp = kstrdup(name, GFP_KERNEL); | |
754 | ||
755 | if (tmp) { | |
756 | ovl_unescape(tmp); | |
757 | err = ovl_mount_dir_noesc(tmp, path); | |
758 | kfree(tmp); | |
759 | } | |
760 | return err; | |
761 | } | |
762 | ||
763 | static int ovl_lower_dir(const char *name, struct path *path, long *namelen, | |
764 | int *stack_depth) | |
765 | { | |
766 | int err; | |
767 | struct kstatfs statfs; | |
768 | ||
a78d9f0d | 769 | err = ovl_mount_dir_noesc(name, path); |
ab508822 MS |
770 | if (err) |
771 | goto out; | |
772 | ||
773 | err = vfs_statfs(path, &statfs); | |
774 | if (err) { | |
775 | pr_err("overlayfs: statfs failed on '%s'\n", name); | |
776 | goto out_put; | |
777 | } | |
778 | *namelen = max(*namelen, statfs.f_namelen); | |
779 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); | |
780 | ||
781 | return 0; | |
782 | ||
783 | out_put: | |
784 | path_put(path); | |
785 | out: | |
786 | return err; | |
787 | } | |
788 | ||
e9be9d5e MS |
789 | /* Workdir should not be subdir of upperdir and vice versa */ |
790 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) | |
791 | { | |
792 | bool ok = false; | |
793 | ||
794 | if (workdir != upperdir) { | |
795 | ok = (lock_rename(workdir, upperdir) == NULL); | |
796 | unlock_rename(workdir, upperdir); | |
797 | } | |
798 | return ok; | |
799 | } | |
800 | ||
a78d9f0d MS |
801 | static unsigned int ovl_split_lowerdirs(char *str) |
802 | { | |
803 | unsigned int ctr = 1; | |
804 | char *s, *d; | |
805 | ||
806 | for (s = d = str;; s++, d++) { | |
807 | if (*s == '\\') { | |
808 | s++; | |
809 | } else if (*s == ':') { | |
810 | *d = '\0'; | |
811 | ctr++; | |
812 | continue; | |
813 | } | |
814 | *d = *s; | |
815 | if (!*s) | |
816 | break; | |
817 | } | |
818 | return ctr; | |
819 | } | |
820 | ||
e9be9d5e MS |
821 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
822 | { | |
53a08cb9 MS |
823 | struct path upperpath = { NULL, NULL }; |
824 | struct path workpath = { NULL, NULL }; | |
e9be9d5e MS |
825 | struct dentry *root_dentry; |
826 | struct ovl_entry *oe; | |
827 | struct ovl_fs *ufs; | |
a78d9f0d MS |
828 | struct path *stack = NULL; |
829 | char *lowertmp; | |
830 | char *lower; | |
831 | unsigned int numlower; | |
832 | unsigned int stacklen = 0; | |
dd662667 | 833 | unsigned int i; |
e9be9d5e MS |
834 | int err; |
835 | ||
f45827e8 EZ |
836 | err = -ENOMEM; |
837 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); | |
838 | if (!ufs) | |
e9be9d5e MS |
839 | goto out; |
840 | ||
f45827e8 EZ |
841 | err = ovl_parse_opt((char *) data, &ufs->config); |
842 | if (err) | |
843 | goto out_free_config; | |
844 | ||
e9be9d5e | 845 | err = -EINVAL; |
53a08cb9 MS |
846 | if (!ufs->config.lowerdir) { |
847 | pr_err("overlayfs: missing 'lowerdir'\n"); | |
e9be9d5e MS |
848 | goto out_free_config; |
849 | } | |
850 | ||
53a08cb9 MS |
851 | sb->s_stack_depth = 0; |
852 | if (ufs->config.upperdir) { | |
53a08cb9 MS |
853 | if (!ufs->config.workdir) { |
854 | pr_err("overlayfs: missing 'workdir'\n"); | |
855 | goto out_free_config; | |
856 | } | |
e9be9d5e | 857 | |
53a08cb9 MS |
858 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
859 | if (err) | |
860 | goto out_free_config; | |
e9be9d5e | 861 | |
71cbad7e | 862 | /* Upper fs should not be r/o */ |
863 | if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) { | |
864 | pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); | |
865 | err = -EINVAL; | |
866 | goto out_put_upperpath; | |
867 | } | |
868 | ||
53a08cb9 MS |
869 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
870 | if (err) | |
871 | goto out_put_upperpath; | |
872 | ||
2f83fd8c | 873 | err = -EINVAL; |
53a08cb9 MS |
874 | if (upperpath.mnt != workpath.mnt) { |
875 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); | |
876 | goto out_put_workpath; | |
877 | } | |
878 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { | |
879 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); | |
880 | goto out_put_workpath; | |
881 | } | |
882 | sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; | |
cc259639 | 883 | } |
a78d9f0d MS |
884 | err = -ENOMEM; |
885 | lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); | |
886 | if (!lowertmp) | |
ab508822 | 887 | goto out_put_workpath; |
69c433ed | 888 | |
a78d9f0d MS |
889 | err = -EINVAL; |
890 | stacklen = ovl_split_lowerdirs(lowertmp); | |
6be4506e | 891 | if (stacklen > OVL_MAX_STACK) { |
892 | pr_err("overlayfs: too many lower directries, limit is %d\n", | |
893 | OVL_MAX_STACK); | |
a78d9f0d | 894 | goto out_free_lowertmp; |
6be4506e | 895 | } else if (!ufs->config.upperdir && stacklen == 1) { |
896 | pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); | |
897 | goto out_free_lowertmp; | |
898 | } | |
a78d9f0d MS |
899 | |
900 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); | |
901 | if (!stack) | |
902 | goto out_free_lowertmp; | |
903 | ||
904 | lower = lowertmp; | |
905 | for (numlower = 0; numlower < stacklen; numlower++) { | |
906 | err = ovl_lower_dir(lower, &stack[numlower], | |
907 | &ufs->lower_namelen, &sb->s_stack_depth); | |
908 | if (err) | |
909 | goto out_put_lowerpath; | |
910 | ||
911 | lower = strchr(lower, '\0') + 1; | |
912 | } | |
913 | ||
69c433ed | 914 | err = -EINVAL; |
ab508822 | 915 | sb->s_stack_depth++; |
69c433ed MS |
916 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
917 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); | |
3b7a9a24 | 918 | goto out_put_lowerpath; |
69c433ed MS |
919 | } |
920 | ||
53a08cb9 MS |
921 | if (ufs->config.upperdir) { |
922 | ufs->upper_mnt = clone_private_mount(&upperpath); | |
923 | err = PTR_ERR(ufs->upper_mnt); | |
924 | if (IS_ERR(ufs->upper_mnt)) { | |
925 | pr_err("overlayfs: failed to clone upperpath\n"); | |
926 | goto out_put_lowerpath; | |
927 | } | |
3b7a9a24 | 928 | |
53a08cb9 MS |
929 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); |
930 | err = PTR_ERR(ufs->workdir); | |
931 | if (IS_ERR(ufs->workdir)) { | |
cc6f67bc MS |
932 | pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", |
933 | ufs->config.workdir, OVL_WORKDIR_NAME, -err); | |
934 | sb->s_flags |= MS_RDONLY; | |
935 | ufs->workdir = NULL; | |
53a08cb9 | 936 | } |
e9be9d5e MS |
937 | } |
938 | ||
2f83fd8c | 939 | err = -ENOMEM; |
a78d9f0d | 940 | ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); |
dd662667 | 941 | if (ufs->lower_mnt == NULL) |
3b7a9a24 | 942 | goto out_put_workdir; |
a78d9f0d MS |
943 | for (i = 0; i < numlower; i++) { |
944 | struct vfsmount *mnt = clone_private_mount(&stack[i]); | |
dd662667 | 945 | |
2f83fd8c | 946 | err = PTR_ERR(mnt); |
a78d9f0d MS |
947 | if (IS_ERR(mnt)) { |
948 | pr_err("overlayfs: failed to clone lowerpath\n"); | |
949 | goto out_put_lower_mnt; | |
950 | } | |
951 | /* | |
952 | * Make lower_mnt R/O. That way fchmod/fchown on lower file | |
953 | * will fail instead of modifying lower fs. | |
954 | */ | |
955 | mnt->mnt_flags |= MNT_READONLY; | |
dd662667 | 956 | |
a78d9f0d MS |
957 | ufs->lower_mnt[ufs->numlower] = mnt; |
958 | ufs->numlower++; | |
959 | } | |
e9be9d5e | 960 | |
71cbad7e | 961 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
962 | if (!ufs->upper_mnt) | |
e9be9d5e MS |
963 | sb->s_flags |= MS_RDONLY; |
964 | ||
965 | sb->s_d_op = &ovl_dentry_operations; | |
966 | ||
967 | err = -ENOMEM; | |
a78d9f0d | 968 | oe = ovl_alloc_entry(numlower); |
3b7a9a24 MS |
969 | if (!oe) |
970 | goto out_put_lower_mnt; | |
e9be9d5e | 971 | |
3b7a9a24 | 972 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe)); |
e9be9d5e | 973 | if (!root_dentry) |
3b7a9a24 | 974 | goto out_free_oe; |
e9be9d5e MS |
975 | |
976 | mntput(upperpath.mnt); | |
a78d9f0d MS |
977 | for (i = 0; i < numlower; i++) |
978 | mntput(stack[i].mnt); | |
e9be9d5e | 979 | path_put(&workpath); |
a78d9f0d | 980 | kfree(lowertmp); |
e9be9d5e MS |
981 | |
982 | oe->__upperdentry = upperpath.dentry; | |
a78d9f0d MS |
983 | for (i = 0; i < numlower; i++) { |
984 | oe->lowerstack[i].dentry = stack[i].dentry; | |
985 | oe->lowerstack[i].mnt = ufs->lower_mnt[i]; | |
986 | } | |
e9be9d5e MS |
987 | |
988 | root_dentry->d_fsdata = oe; | |
989 | ||
cc259639 | 990 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
e9be9d5e MS |
991 | sb->s_op = &ovl_super_operations; |
992 | sb->s_root = root_dentry; | |
993 | sb->s_fs_info = ufs; | |
994 | ||
995 | return 0; | |
996 | ||
3b7a9a24 MS |
997 | out_free_oe: |
998 | kfree(oe); | |
e9be9d5e | 999 | out_put_lower_mnt: |
dd662667 MS |
1000 | for (i = 0; i < ufs->numlower; i++) |
1001 | mntput(ufs->lower_mnt[i]); | |
1002 | kfree(ufs->lower_mnt); | |
3b7a9a24 MS |
1003 | out_put_workdir: |
1004 | dput(ufs->workdir); | |
e9be9d5e | 1005 | mntput(ufs->upper_mnt); |
e9be9d5e | 1006 | out_put_lowerpath: |
a78d9f0d MS |
1007 | for (i = 0; i < numlower; i++) |
1008 | path_put(&stack[i]); | |
1009 | kfree(stack); | |
1010 | out_free_lowertmp: | |
1011 | kfree(lowertmp); | |
3b7a9a24 MS |
1012 | out_put_workpath: |
1013 | path_put(&workpath); | |
e9be9d5e MS |
1014 | out_put_upperpath: |
1015 | path_put(&upperpath); | |
e9be9d5e | 1016 | out_free_config: |
f45827e8 EZ |
1017 | kfree(ufs->config.lowerdir); |
1018 | kfree(ufs->config.upperdir); | |
1019 | kfree(ufs->config.workdir); | |
1020 | kfree(ufs); | |
e9be9d5e MS |
1021 | out: |
1022 | return err; | |
1023 | } | |
1024 | ||
1025 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, | |
1026 | const char *dev_name, void *raw_data) | |
1027 | { | |
1028 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); | |
1029 | } | |
1030 | ||
1031 | static struct file_system_type ovl_fs_type = { | |
1032 | .owner = THIS_MODULE, | |
ef94b186 | 1033 | .name = "overlay", |
e9be9d5e MS |
1034 | .mount = ovl_mount, |
1035 | .kill_sb = kill_anon_super, | |
1036 | }; | |
ef94b186 | 1037 | MODULE_ALIAS_FS("overlay"); |
e9be9d5e MS |
1038 | |
1039 | static int __init ovl_init(void) | |
1040 | { | |
1041 | return register_filesystem(&ovl_fs_type); | |
1042 | } | |
1043 | ||
1044 | static void __exit ovl_exit(void) | |
1045 | { | |
1046 | unregister_filesystem(&ovl_fs_type); | |
1047 | } | |
1048 | ||
1049 | module_init(ovl_init); | |
1050 | module_exit(ovl_exit); |