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 | ||
63 | const char *ovl_opaque_xattr = "trusted.overlay.opaque"; | |
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 | } |
e9be9d5e | 84 | } |
1afaba1e | 85 | return type; |
e9be9d5e MS |
86 | } |
87 | ||
88 | static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) | |
89 | { | |
71d50928 | 90 | return lockless_dereference(oe->__upperdentry); |
e9be9d5e MS |
91 | } |
92 | ||
93 | void ovl_path_upper(struct dentry *dentry, struct path *path) | |
94 | { | |
95 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
96 | struct ovl_entry *oe = dentry->d_fsdata; | |
97 | ||
98 | path->mnt = ofs->upper_mnt; | |
99 | path->dentry = ovl_upperdentry_dereference(oe); | |
100 | } | |
101 | ||
102 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) | |
103 | { | |
104 | ||
105 | enum ovl_path_type type = ovl_path_type(dentry); | |
106 | ||
1afaba1e | 107 | if (!OVL_TYPE_UPPER(type)) |
e9be9d5e MS |
108 | ovl_path_lower(dentry, path); |
109 | else | |
110 | ovl_path_upper(dentry, path); | |
111 | ||
112 | return type; | |
113 | } | |
114 | ||
115 | struct dentry *ovl_dentry_upper(struct dentry *dentry) | |
116 | { | |
117 | struct ovl_entry *oe = dentry->d_fsdata; | |
118 | ||
119 | return ovl_upperdentry_dereference(oe); | |
120 | } | |
121 | ||
122 | struct dentry *ovl_dentry_lower(struct dentry *dentry) | |
123 | { | |
124 | struct ovl_entry *oe = dentry->d_fsdata; | |
125 | ||
dd662667 | 126 | return __ovl_dentry_lower(oe); |
e9be9d5e MS |
127 | } |
128 | ||
129 | struct dentry *ovl_dentry_real(struct dentry *dentry) | |
130 | { | |
131 | struct ovl_entry *oe = dentry->d_fsdata; | |
132 | struct dentry *realdentry; | |
133 | ||
134 | realdentry = ovl_upperdentry_dereference(oe); | |
135 | if (!realdentry) | |
dd662667 | 136 | realdentry = __ovl_dentry_lower(oe); |
e9be9d5e MS |
137 | |
138 | return realdentry; | |
139 | } | |
140 | ||
141 | struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper) | |
142 | { | |
143 | struct dentry *realdentry; | |
144 | ||
145 | realdentry = ovl_upperdentry_dereference(oe); | |
146 | if (realdentry) { | |
147 | *is_upper = true; | |
148 | } else { | |
dd662667 | 149 | realdentry = __ovl_dentry_lower(oe); |
e9be9d5e MS |
150 | *is_upper = false; |
151 | } | |
152 | return realdentry; | |
153 | } | |
154 | ||
155 | struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) | |
156 | { | |
157 | struct ovl_entry *oe = dentry->d_fsdata; | |
158 | ||
159 | return oe->cache; | |
160 | } | |
161 | ||
162 | void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) | |
163 | { | |
164 | struct ovl_entry *oe = dentry->d_fsdata; | |
165 | ||
166 | oe->cache = cache; | |
167 | } | |
168 | ||
169 | void ovl_path_lower(struct dentry *dentry, struct path *path) | |
170 | { | |
e9be9d5e MS |
171 | struct ovl_entry *oe = dentry->d_fsdata; |
172 | ||
dd662667 | 173 | *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; |
e9be9d5e MS |
174 | } |
175 | ||
176 | int ovl_want_write(struct dentry *dentry) | |
177 | { | |
178 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
179 | return mnt_want_write(ofs->upper_mnt); | |
180 | } | |
181 | ||
182 | void ovl_drop_write(struct dentry *dentry) | |
183 | { | |
184 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
185 | mnt_drop_write(ofs->upper_mnt); | |
186 | } | |
187 | ||
188 | struct dentry *ovl_workdir(struct dentry *dentry) | |
189 | { | |
190 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
191 | return ofs->workdir; | |
192 | } | |
193 | ||
194 | bool ovl_dentry_is_opaque(struct dentry *dentry) | |
195 | { | |
196 | struct ovl_entry *oe = dentry->d_fsdata; | |
197 | return oe->opaque; | |
198 | } | |
199 | ||
200 | void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) | |
201 | { | |
202 | struct ovl_entry *oe = dentry->d_fsdata; | |
203 | oe->opaque = opaque; | |
204 | } | |
205 | ||
206 | void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) | |
207 | { | |
208 | struct ovl_entry *oe = dentry->d_fsdata; | |
209 | ||
210 | WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex)); | |
211 | WARN_ON(oe->__upperdentry); | |
212 | BUG_ON(!upperdentry->d_inode); | |
213 | /* | |
214 | * Make sure upperdentry is consistent before making it visible to | |
215 | * ovl_upperdentry_dereference(). | |
216 | */ | |
217 | smp_wmb(); | |
218 | oe->__upperdentry = upperdentry; | |
219 | } | |
220 | ||
221 | void ovl_dentry_version_inc(struct dentry *dentry) | |
222 | { | |
223 | struct ovl_entry *oe = dentry->d_fsdata; | |
224 | ||
225 | WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); | |
226 | oe->version++; | |
227 | } | |
228 | ||
229 | u64 ovl_dentry_version_get(struct dentry *dentry) | |
230 | { | |
231 | struct ovl_entry *oe = dentry->d_fsdata; | |
232 | ||
233 | WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); | |
234 | return oe->version; | |
235 | } | |
236 | ||
237 | bool ovl_is_whiteout(struct dentry *dentry) | |
238 | { | |
239 | struct inode *inode = dentry->d_inode; | |
240 | ||
241 | return inode && IS_WHITEOUT(inode); | |
242 | } | |
243 | ||
244 | static bool ovl_is_opaquedir(struct dentry *dentry) | |
245 | { | |
246 | int res; | |
247 | char val; | |
248 | struct inode *inode = dentry->d_inode; | |
249 | ||
250 | if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) | |
251 | return false; | |
252 | ||
253 | res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1); | |
254 | if (res == 1 && val == 'y') | |
255 | return true; | |
256 | ||
257 | return false; | |
258 | } | |
259 | ||
260 | static void ovl_dentry_release(struct dentry *dentry) | |
261 | { | |
262 | struct ovl_entry *oe = dentry->d_fsdata; | |
263 | ||
264 | if (oe) { | |
dd662667 MS |
265 | unsigned int i; |
266 | ||
e9be9d5e | 267 | dput(oe->__upperdentry); |
dd662667 MS |
268 | for (i = 0; i < oe->numlower; i++) |
269 | dput(oe->lowerstack[i].dentry); | |
e9be9d5e MS |
270 | kfree_rcu(oe, rcu); |
271 | } | |
272 | } | |
273 | ||
274 | static const struct dentry_operations ovl_dentry_operations = { | |
275 | .d_release = ovl_dentry_release, | |
276 | }; | |
277 | ||
dd662667 | 278 | static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
e9be9d5e | 279 | { |
dd662667 MS |
280 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); |
281 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); | |
282 | ||
283 | if (oe) | |
284 | oe->numlower = numlower; | |
285 | ||
286 | return oe; | |
e9be9d5e MS |
287 | } |
288 | ||
289 | static inline struct dentry *ovl_lookup_real(struct dentry *dir, | |
290 | struct qstr *name) | |
291 | { | |
292 | struct dentry *dentry; | |
293 | ||
294 | mutex_lock(&dir->d_inode->i_mutex); | |
295 | dentry = lookup_one_len(name->name, dir, name->len); | |
296 | mutex_unlock(&dir->d_inode->i_mutex); | |
297 | ||
298 | if (IS_ERR(dentry)) { | |
299 | if (PTR_ERR(dentry) == -ENOENT) | |
300 | dentry = NULL; | |
301 | } else if (!dentry->d_inode) { | |
302 | dput(dentry); | |
303 | dentry = NULL; | |
304 | } | |
305 | return dentry; | |
306 | } | |
307 | ||
5ef88da5 MS |
308 | /* |
309 | * Returns next layer in stack starting from top. | |
310 | * Returns -1 if this is the last layer. | |
311 | */ | |
312 | int ovl_path_next(int idx, struct dentry *dentry, struct path *path) | |
313 | { | |
314 | struct ovl_entry *oe = dentry->d_fsdata; | |
315 | ||
316 | BUG_ON(idx < 0); | |
317 | if (idx == 0) { | |
318 | ovl_path_upper(dentry, path); | |
319 | if (path->dentry) | |
320 | return oe->numlower ? 1 : -1; | |
321 | idx++; | |
322 | } | |
323 | BUG_ON(idx > oe->numlower); | |
324 | *path = oe->lowerstack[idx - 1]; | |
325 | ||
326 | return (idx < oe->numlower) ? idx + 1 : -1; | |
327 | } | |
328 | ||
e9be9d5e MS |
329 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
330 | unsigned int flags) | |
331 | { | |
332 | struct ovl_entry *oe; | |
333 | struct dentry *upperdir; | |
dd662667 | 334 | struct path lowerdir; |
e9be9d5e MS |
335 | struct dentry *upperdentry = NULL; |
336 | struct dentry *lowerdentry = NULL; | |
337 | struct inode *inode = NULL; | |
338 | int err; | |
339 | ||
340 | err = -ENOMEM; | |
dd662667 | 341 | oe = ovl_alloc_entry(1); |
e9be9d5e MS |
342 | if (!oe) |
343 | goto out; | |
344 | ||
345 | upperdir = ovl_dentry_upper(dentry->d_parent); | |
dd662667 | 346 | ovl_path_lower(dentry->d_parent, &lowerdir); |
e9be9d5e MS |
347 | |
348 | if (upperdir) { | |
349 | upperdentry = ovl_lookup_real(upperdir, &dentry->d_name); | |
350 | err = PTR_ERR(upperdentry); | |
351 | if (IS_ERR(upperdentry)) | |
352 | goto out_put_dir; | |
353 | ||
dd662667 | 354 | if (lowerdir.dentry && upperdentry) { |
e9be9d5e MS |
355 | if (ovl_is_whiteout(upperdentry)) { |
356 | dput(upperdentry); | |
357 | upperdentry = NULL; | |
358 | oe->opaque = true; | |
359 | } else if (ovl_is_opaquedir(upperdentry)) { | |
360 | oe->opaque = true; | |
361 | } | |
362 | } | |
363 | } | |
dd662667 MS |
364 | if (lowerdir.dentry && !oe->opaque) { |
365 | lowerdentry = ovl_lookup_real(lowerdir.dentry, &dentry->d_name); | |
e9be9d5e MS |
366 | err = PTR_ERR(lowerdentry); |
367 | if (IS_ERR(lowerdentry)) | |
368 | goto out_dput_upper; | |
369 | } | |
370 | ||
371 | if (lowerdentry && upperdentry && | |
372 | (!S_ISDIR(upperdentry->d_inode->i_mode) || | |
373 | !S_ISDIR(lowerdentry->d_inode->i_mode))) { | |
374 | dput(lowerdentry); | |
375 | lowerdentry = NULL; | |
376 | oe->opaque = true; | |
377 | } | |
378 | ||
379 | if (lowerdentry || upperdentry) { | |
380 | struct dentry *realdentry; | |
381 | ||
382 | realdentry = upperdentry ? upperdentry : lowerdentry; | |
383 | err = -ENOMEM; | |
384 | inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, | |
385 | oe); | |
386 | if (!inode) | |
387 | goto out_dput; | |
388 | ovl_copyattr(realdentry->d_inode, inode); | |
389 | } | |
390 | ||
391 | oe->__upperdentry = upperdentry; | |
dd662667 MS |
392 | if (lowerdentry) { |
393 | oe->lowerstack[0].dentry = lowerdentry; | |
394 | oe->lowerstack[0].mnt = lowerdir.mnt; | |
395 | } else { | |
396 | oe->numlower = 0; | |
397 | } | |
e9be9d5e MS |
398 | dentry->d_fsdata = oe; |
399 | d_add(dentry, inode); | |
400 | ||
401 | return NULL; | |
402 | ||
403 | out_dput: | |
404 | dput(lowerdentry); | |
405 | out_dput_upper: | |
406 | dput(upperdentry); | |
407 | out_put_dir: | |
408 | kfree(oe); | |
409 | out: | |
410 | return ERR_PTR(err); | |
411 | } | |
412 | ||
413 | struct file *ovl_path_open(struct path *path, int flags) | |
414 | { | |
415 | return dentry_open(path, flags, current_cred()); | |
416 | } | |
417 | ||
418 | static void ovl_put_super(struct super_block *sb) | |
419 | { | |
420 | struct ovl_fs *ufs = sb->s_fs_info; | |
dd662667 | 421 | unsigned i; |
e9be9d5e MS |
422 | |
423 | dput(ufs->workdir); | |
424 | mntput(ufs->upper_mnt); | |
dd662667 MS |
425 | for (i = 0; i < ufs->numlower; i++) |
426 | mntput(ufs->lower_mnt[i]); | |
e9be9d5e | 427 | |
f45827e8 EZ |
428 | kfree(ufs->config.lowerdir); |
429 | kfree(ufs->config.upperdir); | |
430 | kfree(ufs->config.workdir); | |
e9be9d5e MS |
431 | kfree(ufs); |
432 | } | |
433 | ||
cc259639 AW |
434 | /** |
435 | * ovl_statfs | |
436 | * @sb: The overlayfs super block | |
437 | * @buf: The struct kstatfs to fill in with stats | |
438 | * | |
439 | * Get the filesystem statistics. As writes always target the upper layer | |
440 | * filesystem pass the statfs to the same filesystem. | |
441 | */ | |
442 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) | |
443 | { | |
444 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
445 | struct dentry *root_dentry = dentry->d_sb->s_root; | |
446 | struct path path; | |
447 | int err; | |
448 | ||
449 | ovl_path_upper(root_dentry, &path); | |
450 | ||
451 | err = vfs_statfs(&path, buf); | |
452 | if (!err) { | |
453 | buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); | |
454 | buf->f_type = OVERLAYFS_SUPER_MAGIC; | |
455 | } | |
456 | ||
457 | return err; | |
458 | } | |
459 | ||
f45827e8 EZ |
460 | /** |
461 | * ovl_show_options | |
462 | * | |
463 | * Prints the mount options for a given superblock. | |
464 | * Returns zero; does not fail. | |
465 | */ | |
466 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) | |
467 | { | |
468 | struct super_block *sb = dentry->d_sb; | |
469 | struct ovl_fs *ufs = sb->s_fs_info; | |
470 | ||
471 | seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir); | |
472 | seq_printf(m, ",upperdir=%s", ufs->config.upperdir); | |
473 | seq_printf(m, ",workdir=%s", ufs->config.workdir); | |
474 | return 0; | |
475 | } | |
476 | ||
e9be9d5e MS |
477 | static const struct super_operations ovl_super_operations = { |
478 | .put_super = ovl_put_super, | |
cc259639 | 479 | .statfs = ovl_statfs, |
f45827e8 | 480 | .show_options = ovl_show_options, |
e9be9d5e MS |
481 | }; |
482 | ||
483 | enum { | |
484 | OPT_LOWERDIR, | |
485 | OPT_UPPERDIR, | |
486 | OPT_WORKDIR, | |
487 | OPT_ERR, | |
488 | }; | |
489 | ||
490 | static const match_table_t ovl_tokens = { | |
491 | {OPT_LOWERDIR, "lowerdir=%s"}, | |
492 | {OPT_UPPERDIR, "upperdir=%s"}, | |
493 | {OPT_WORKDIR, "workdir=%s"}, | |
494 | {OPT_ERR, NULL} | |
495 | }; | |
496 | ||
91c77947 MS |
497 | static char *ovl_next_opt(char **s) |
498 | { | |
499 | char *sbegin = *s; | |
500 | char *p; | |
501 | ||
502 | if (sbegin == NULL) | |
503 | return NULL; | |
504 | ||
505 | for (p = sbegin; *p; p++) { | |
506 | if (*p == '\\') { | |
507 | p++; | |
508 | if (!*p) | |
509 | break; | |
510 | } else if (*p == ',') { | |
511 | *p = '\0'; | |
512 | *s = p + 1; | |
513 | return sbegin; | |
514 | } | |
515 | } | |
516 | *s = NULL; | |
517 | return sbegin; | |
518 | } | |
519 | ||
e9be9d5e MS |
520 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
521 | { | |
522 | char *p; | |
523 | ||
91c77947 | 524 | while ((p = ovl_next_opt(&opt)) != NULL) { |
e9be9d5e MS |
525 | int token; |
526 | substring_t args[MAX_OPT_ARGS]; | |
527 | ||
528 | if (!*p) | |
529 | continue; | |
530 | ||
531 | token = match_token(p, ovl_tokens, args); | |
532 | switch (token) { | |
533 | case OPT_UPPERDIR: | |
534 | kfree(config->upperdir); | |
535 | config->upperdir = match_strdup(&args[0]); | |
536 | if (!config->upperdir) | |
537 | return -ENOMEM; | |
538 | break; | |
539 | ||
540 | case OPT_LOWERDIR: | |
541 | kfree(config->lowerdir); | |
542 | config->lowerdir = match_strdup(&args[0]); | |
543 | if (!config->lowerdir) | |
544 | return -ENOMEM; | |
545 | break; | |
546 | ||
547 | case OPT_WORKDIR: | |
548 | kfree(config->workdir); | |
549 | config->workdir = match_strdup(&args[0]); | |
550 | if (!config->workdir) | |
551 | return -ENOMEM; | |
552 | break; | |
553 | ||
554 | default: | |
555 | return -EINVAL; | |
556 | } | |
557 | } | |
558 | return 0; | |
559 | } | |
560 | ||
561 | #define OVL_WORKDIR_NAME "work" | |
562 | ||
563 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, | |
564 | struct dentry *dentry) | |
565 | { | |
566 | struct inode *dir = dentry->d_inode; | |
567 | struct dentry *work; | |
568 | int err; | |
569 | bool retried = false; | |
570 | ||
571 | err = mnt_want_write(mnt); | |
572 | if (err) | |
573 | return ERR_PTR(err); | |
574 | ||
575 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); | |
576 | retry: | |
577 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, | |
578 | strlen(OVL_WORKDIR_NAME)); | |
579 | ||
580 | if (!IS_ERR(work)) { | |
581 | struct kstat stat = { | |
582 | .mode = S_IFDIR | 0, | |
583 | }; | |
584 | ||
585 | if (work->d_inode) { | |
586 | err = -EEXIST; | |
587 | if (retried) | |
588 | goto out_dput; | |
589 | ||
590 | retried = true; | |
591 | ovl_cleanup(dir, work); | |
592 | dput(work); | |
593 | goto retry; | |
594 | } | |
595 | ||
596 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); | |
597 | if (err) | |
598 | goto out_dput; | |
599 | } | |
600 | out_unlock: | |
601 | mutex_unlock(&dir->i_mutex); | |
602 | mnt_drop_write(mnt); | |
603 | ||
604 | return work; | |
605 | ||
606 | out_dput: | |
607 | dput(work); | |
608 | work = ERR_PTR(err); | |
609 | goto out_unlock; | |
610 | } | |
611 | ||
91c77947 MS |
612 | static void ovl_unescape(char *s) |
613 | { | |
614 | char *d = s; | |
615 | ||
616 | for (;; s++, d++) { | |
617 | if (*s == '\\') | |
618 | s++; | |
619 | *d = *s; | |
620 | if (!*s) | |
621 | break; | |
622 | } | |
623 | } | |
624 | ||
e9be9d5e MS |
625 | static int ovl_mount_dir(const char *name, struct path *path) |
626 | { | |
627 | int err; | |
91c77947 MS |
628 | char *tmp = kstrdup(name, GFP_KERNEL); |
629 | ||
630 | if (!tmp) | |
631 | return -ENOMEM; | |
e9be9d5e | 632 | |
91c77947 MS |
633 | ovl_unescape(tmp); |
634 | err = kern_path(tmp, LOOKUP_FOLLOW, path); | |
e9be9d5e | 635 | if (err) { |
91c77947 | 636 | pr_err("overlayfs: failed to resolve '%s': %i\n", tmp, err); |
e9be9d5e MS |
637 | err = -EINVAL; |
638 | } | |
91c77947 | 639 | kfree(tmp); |
e9be9d5e MS |
640 | return err; |
641 | } | |
642 | ||
643 | static bool ovl_is_allowed_fs_type(struct dentry *root) | |
644 | { | |
645 | const struct dentry_operations *dop = root->d_op; | |
646 | ||
647 | /* | |
648 | * We don't support: | |
649 | * - automount filesystems | |
650 | * - filesystems with revalidate (FIXME for lower layer) | |
651 | * - filesystems with case insensitive names | |
652 | */ | |
653 | if (dop && | |
654 | (dop->d_manage || dop->d_automount || | |
655 | dop->d_revalidate || dop->d_weak_revalidate || | |
656 | dop->d_compare || dop->d_hash)) { | |
657 | return false; | |
658 | } | |
659 | return true; | |
660 | } | |
661 | ||
662 | /* Workdir should not be subdir of upperdir and vice versa */ | |
663 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) | |
664 | { | |
665 | bool ok = false; | |
666 | ||
667 | if (workdir != upperdir) { | |
668 | ok = (lock_rename(workdir, upperdir) == NULL); | |
669 | unlock_rename(workdir, upperdir); | |
670 | } | |
671 | return ok; | |
672 | } | |
673 | ||
674 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) | |
675 | { | |
676 | struct path lowerpath; | |
677 | struct path upperpath; | |
678 | struct path workpath; | |
679 | struct inode *root_inode; | |
680 | struct dentry *root_dentry; | |
681 | struct ovl_entry *oe; | |
682 | struct ovl_fs *ufs; | |
cc259639 | 683 | struct kstatfs statfs; |
dd662667 MS |
684 | struct vfsmount *mnt; |
685 | unsigned int i; | |
e9be9d5e MS |
686 | int err; |
687 | ||
f45827e8 EZ |
688 | err = -ENOMEM; |
689 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); | |
690 | if (!ufs) | |
e9be9d5e MS |
691 | goto out; |
692 | ||
f45827e8 EZ |
693 | err = ovl_parse_opt((char *) data, &ufs->config); |
694 | if (err) | |
695 | goto out_free_config; | |
696 | ||
e9be9d5e MS |
697 | /* FIXME: workdir is not needed for a R/O mount */ |
698 | err = -EINVAL; | |
f45827e8 EZ |
699 | if (!ufs->config.upperdir || !ufs->config.lowerdir || |
700 | !ufs->config.workdir) { | |
e9be9d5e MS |
701 | pr_err("overlayfs: missing upperdir or lowerdir or workdir\n"); |
702 | goto out_free_config; | |
703 | } | |
704 | ||
705 | err = -ENOMEM; | |
dd662667 | 706 | oe = ovl_alloc_entry(1); |
e9be9d5e | 707 | if (oe == NULL) |
f45827e8 | 708 | goto out_free_config; |
e9be9d5e | 709 | |
f45827e8 | 710 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
e9be9d5e MS |
711 | if (err) |
712 | goto out_free_oe; | |
713 | ||
f45827e8 | 714 | err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath); |
e9be9d5e MS |
715 | if (err) |
716 | goto out_put_upperpath; | |
717 | ||
f45827e8 | 718 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
e9be9d5e MS |
719 | if (err) |
720 | goto out_put_lowerpath; | |
721 | ||
722 | err = -EINVAL; | |
723 | if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) || | |
724 | !S_ISDIR(lowerpath.dentry->d_inode->i_mode) || | |
725 | !S_ISDIR(workpath.dentry->d_inode->i_mode)) { | |
726 | pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n"); | |
727 | goto out_put_workpath; | |
728 | } | |
729 | ||
730 | if (upperpath.mnt != workpath.mnt) { | |
731 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); | |
732 | goto out_put_workpath; | |
733 | } | |
734 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { | |
735 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); | |
736 | goto out_put_workpath; | |
737 | } | |
738 | ||
739 | if (!ovl_is_allowed_fs_type(upperpath.dentry)) { | |
740 | pr_err("overlayfs: filesystem of upperdir is not supported\n"); | |
741 | goto out_put_workpath; | |
742 | } | |
743 | ||
744 | if (!ovl_is_allowed_fs_type(lowerpath.dentry)) { | |
745 | pr_err("overlayfs: filesystem of lowerdir is not supported\n"); | |
746 | goto out_put_workpath; | |
747 | } | |
748 | ||
cc259639 AW |
749 | err = vfs_statfs(&lowerpath, &statfs); |
750 | if (err) { | |
751 | pr_err("overlayfs: statfs failed on lowerpath\n"); | |
752 | goto out_put_workpath; | |
753 | } | |
754 | ufs->lower_namelen = statfs.f_namelen; | |
755 | ||
69c433ed MS |
756 | sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth, |
757 | lowerpath.mnt->mnt_sb->s_stack_depth) + 1; | |
758 | ||
759 | err = -EINVAL; | |
760 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { | |
761 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); | |
762 | goto out_put_workpath; | |
763 | } | |
764 | ||
e9be9d5e MS |
765 | ufs->upper_mnt = clone_private_mount(&upperpath); |
766 | err = PTR_ERR(ufs->upper_mnt); | |
767 | if (IS_ERR(ufs->upper_mnt)) { | |
768 | pr_err("overlayfs: failed to clone upperpath\n"); | |
769 | goto out_put_workpath; | |
770 | } | |
771 | ||
dd662667 MS |
772 | ufs->lower_mnt = kcalloc(1, sizeof(struct vfsmount *), GFP_KERNEL); |
773 | if (ufs->lower_mnt == NULL) | |
e9be9d5e | 774 | goto out_put_upper_mnt; |
dd662667 MS |
775 | |
776 | mnt = clone_private_mount(&lowerpath); | |
777 | err = PTR_ERR(mnt); | |
778 | if (IS_ERR(mnt)) { | |
779 | pr_err("overlayfs: failed to clone lowerpath\n"); | |
780 | goto out_put_lower_mnt; | |
e9be9d5e | 781 | } |
dd662667 MS |
782 | /* |
783 | * Make lower_mnt R/O. That way fchmod/fchown on lower file | |
784 | * will fail instead of modifying lower fs. | |
785 | */ | |
786 | mnt->mnt_flags |= MNT_READONLY; | |
787 | ||
788 | ufs->lower_mnt[0] = mnt; | |
789 | ufs->numlower = 1; | |
e9be9d5e MS |
790 | |
791 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); | |
792 | err = PTR_ERR(ufs->workdir); | |
793 | if (IS_ERR(ufs->workdir)) { | |
794 | pr_err("overlayfs: failed to create directory %s/%s\n", | |
f45827e8 | 795 | ufs->config.workdir, OVL_WORKDIR_NAME); |
e9be9d5e MS |
796 | goto out_put_lower_mnt; |
797 | } | |
798 | ||
e9be9d5e MS |
799 | /* If the upper fs is r/o, we mark overlayfs r/o too */ |
800 | if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY) | |
801 | sb->s_flags |= MS_RDONLY; | |
802 | ||
803 | sb->s_d_op = &ovl_dentry_operations; | |
804 | ||
805 | err = -ENOMEM; | |
806 | root_inode = ovl_new_inode(sb, S_IFDIR, oe); | |
807 | if (!root_inode) | |
808 | goto out_put_workdir; | |
809 | ||
810 | root_dentry = d_make_root(root_inode); | |
811 | if (!root_dentry) | |
812 | goto out_put_workdir; | |
813 | ||
814 | mntput(upperpath.mnt); | |
815 | mntput(lowerpath.mnt); | |
816 | path_put(&workpath); | |
817 | ||
818 | oe->__upperdentry = upperpath.dentry; | |
dd662667 MS |
819 | oe->lowerstack[0].dentry = lowerpath.dentry; |
820 | oe->lowerstack[0].mnt = ufs->lower_mnt[0]; | |
e9be9d5e MS |
821 | |
822 | root_dentry->d_fsdata = oe; | |
823 | ||
cc259639 | 824 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
e9be9d5e MS |
825 | sb->s_op = &ovl_super_operations; |
826 | sb->s_root = root_dentry; | |
827 | sb->s_fs_info = ufs; | |
828 | ||
829 | return 0; | |
830 | ||
831 | out_put_workdir: | |
832 | dput(ufs->workdir); | |
833 | out_put_lower_mnt: | |
dd662667 MS |
834 | for (i = 0; i < ufs->numlower; i++) |
835 | mntput(ufs->lower_mnt[i]); | |
836 | kfree(ufs->lower_mnt); | |
e9be9d5e MS |
837 | out_put_upper_mnt: |
838 | mntput(ufs->upper_mnt); | |
839 | out_put_workpath: | |
840 | path_put(&workpath); | |
841 | out_put_lowerpath: | |
842 | path_put(&lowerpath); | |
843 | out_put_upperpath: | |
844 | path_put(&upperpath); | |
845 | out_free_oe: | |
846 | kfree(oe); | |
e9be9d5e | 847 | out_free_config: |
f45827e8 EZ |
848 | kfree(ufs->config.lowerdir); |
849 | kfree(ufs->config.upperdir); | |
850 | kfree(ufs->config.workdir); | |
851 | kfree(ufs); | |
e9be9d5e MS |
852 | out: |
853 | return err; | |
854 | } | |
855 | ||
856 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, | |
857 | const char *dev_name, void *raw_data) | |
858 | { | |
859 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); | |
860 | } | |
861 | ||
862 | static struct file_system_type ovl_fs_type = { | |
863 | .owner = THIS_MODULE, | |
ef94b186 | 864 | .name = "overlay", |
e9be9d5e MS |
865 | .mount = ovl_mount, |
866 | .kill_sb = kill_anon_super, | |
867 | }; | |
ef94b186 | 868 | MODULE_ALIAS_FS("overlay"); |
e9be9d5e MS |
869 | |
870 | static int __init ovl_init(void) | |
871 | { | |
872 | return register_filesystem(&ovl_fs_type); | |
873 | } | |
874 | ||
875 | static void __exit ovl_exit(void) | |
876 | { | |
877 | unregister_filesystem(&ovl_fs_type); | |
878 | } | |
879 | ||
880 | module_init(ovl_init); | |
881 | module_exit(ovl_exit); |