]>
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 | ||
cc259639 AW |
27 | #define OVERLAYFS_SUPER_MAGIC 0x794c764f |
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; | |
38 | struct vfsmount *lower_mnt; | |
39 | struct dentry *workdir; | |
cc259639 | 40 | long lower_namelen; |
f45827e8 EZ |
41 | /* pathnames of lower and upper dirs, for show_options */ |
42 | struct ovl_config config; | |
e9be9d5e MS |
43 | }; |
44 | ||
45 | struct ovl_dir_cache; | |
46 | ||
47 | /* private information held for every overlayfs dentry */ | |
48 | struct ovl_entry { | |
49 | struct dentry *__upperdentry; | |
50 | struct dentry *lowerdentry; | |
51 | struct ovl_dir_cache *cache; | |
52 | union { | |
53 | struct { | |
54 | u64 version; | |
55 | bool opaque; | |
56 | }; | |
57 | struct rcu_head rcu; | |
58 | }; | |
59 | }; | |
60 | ||
61 | const char *ovl_opaque_xattr = "trusted.overlay.opaque"; | |
62 | ||
63 | ||
64 | enum ovl_path_type ovl_path_type(struct dentry *dentry) | |
65 | { | |
66 | struct ovl_entry *oe = dentry->d_fsdata; | |
67 | ||
68 | if (oe->__upperdentry) { | |
69 | if (oe->lowerdentry) { | |
70 | if (S_ISDIR(dentry->d_inode->i_mode)) | |
71 | return OVL_PATH_MERGE; | |
72 | else | |
73 | return OVL_PATH_UPPER; | |
74 | } else { | |
75 | if (oe->opaque) | |
76 | return OVL_PATH_UPPER; | |
77 | else | |
78 | return OVL_PATH_PURE_UPPER; | |
79 | } | |
80 | } else { | |
81 | return OVL_PATH_LOWER; | |
82 | } | |
83 | } | |
84 | ||
85 | static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) | |
86 | { | |
87 | struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry); | |
88 | /* | |
89 | * Make sure to order reads to upperdentry wrt ovl_dentry_update() | |
90 | */ | |
91 | smp_read_barrier_depends(); | |
92 | return upperdentry; | |
93 | } | |
94 | ||
95 | void ovl_path_upper(struct dentry *dentry, struct path *path) | |
96 | { | |
97 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
98 | struct ovl_entry *oe = dentry->d_fsdata; | |
99 | ||
100 | path->mnt = ofs->upper_mnt; | |
101 | path->dentry = ovl_upperdentry_dereference(oe); | |
102 | } | |
103 | ||
104 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) | |
105 | { | |
106 | ||
107 | enum ovl_path_type type = ovl_path_type(dentry); | |
108 | ||
109 | if (type == OVL_PATH_LOWER) | |
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 | ||
128 | return oe->lowerdentry; | |
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) | |
138 | realdentry = oe->lowerdentry; | |
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 { | |
151 | realdentry = oe->lowerdentry; | |
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 | { | |
173 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
174 | struct ovl_entry *oe = dentry->d_fsdata; | |
175 | ||
176 | path->mnt = ofs->lower_mnt; | |
177 | path->dentry = oe->lowerdentry; | |
178 | } | |
179 | ||
180 | int ovl_want_write(struct dentry *dentry) | |
181 | { | |
182 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
183 | return mnt_want_write(ofs->upper_mnt); | |
184 | } | |
185 | ||
186 | void ovl_drop_write(struct dentry *dentry) | |
187 | { | |
188 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
189 | mnt_drop_write(ofs->upper_mnt); | |
190 | } | |
191 | ||
192 | struct dentry *ovl_workdir(struct dentry *dentry) | |
193 | { | |
194 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
195 | return ofs->workdir; | |
196 | } | |
197 | ||
198 | bool ovl_dentry_is_opaque(struct dentry *dentry) | |
199 | { | |
200 | struct ovl_entry *oe = dentry->d_fsdata; | |
201 | return oe->opaque; | |
202 | } | |
203 | ||
204 | void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) | |
205 | { | |
206 | struct ovl_entry *oe = dentry->d_fsdata; | |
207 | oe->opaque = opaque; | |
208 | } | |
209 | ||
210 | void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) | |
211 | { | |
212 | struct ovl_entry *oe = dentry->d_fsdata; | |
213 | ||
214 | WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex)); | |
215 | WARN_ON(oe->__upperdentry); | |
216 | BUG_ON(!upperdentry->d_inode); | |
217 | /* | |
218 | * Make sure upperdentry is consistent before making it visible to | |
219 | * ovl_upperdentry_dereference(). | |
220 | */ | |
221 | smp_wmb(); | |
222 | oe->__upperdentry = upperdentry; | |
223 | } | |
224 | ||
225 | void ovl_dentry_version_inc(struct dentry *dentry) | |
226 | { | |
227 | struct ovl_entry *oe = dentry->d_fsdata; | |
228 | ||
229 | WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); | |
230 | oe->version++; | |
231 | } | |
232 | ||
233 | u64 ovl_dentry_version_get(struct dentry *dentry) | |
234 | { | |
235 | struct ovl_entry *oe = dentry->d_fsdata; | |
236 | ||
237 | WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); | |
238 | return oe->version; | |
239 | } | |
240 | ||
241 | bool ovl_is_whiteout(struct dentry *dentry) | |
242 | { | |
243 | struct inode *inode = dentry->d_inode; | |
244 | ||
245 | return inode && IS_WHITEOUT(inode); | |
246 | } | |
247 | ||
248 | static bool ovl_is_opaquedir(struct dentry *dentry) | |
249 | { | |
250 | int res; | |
251 | char val; | |
252 | struct inode *inode = dentry->d_inode; | |
253 | ||
254 | if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) | |
255 | return false; | |
256 | ||
257 | res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1); | |
258 | if (res == 1 && val == 'y') | |
259 | return true; | |
260 | ||
261 | return false; | |
262 | } | |
263 | ||
264 | static void ovl_dentry_release(struct dentry *dentry) | |
265 | { | |
266 | struct ovl_entry *oe = dentry->d_fsdata; | |
267 | ||
268 | if (oe) { | |
269 | dput(oe->__upperdentry); | |
270 | dput(oe->lowerdentry); | |
271 | kfree_rcu(oe, rcu); | |
272 | } | |
273 | } | |
274 | ||
275 | static const struct dentry_operations ovl_dentry_operations = { | |
276 | .d_release = ovl_dentry_release, | |
277 | }; | |
278 | ||
279 | static struct ovl_entry *ovl_alloc_entry(void) | |
280 | { | |
281 | return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL); | |
282 | } | |
283 | ||
284 | static inline struct dentry *ovl_lookup_real(struct dentry *dir, | |
285 | struct qstr *name) | |
286 | { | |
287 | struct dentry *dentry; | |
288 | ||
289 | mutex_lock(&dir->d_inode->i_mutex); | |
290 | dentry = lookup_one_len(name->name, dir, name->len); | |
291 | mutex_unlock(&dir->d_inode->i_mutex); | |
292 | ||
293 | if (IS_ERR(dentry)) { | |
294 | if (PTR_ERR(dentry) == -ENOENT) | |
295 | dentry = NULL; | |
296 | } else if (!dentry->d_inode) { | |
297 | dput(dentry); | |
298 | dentry = NULL; | |
299 | } | |
300 | return dentry; | |
301 | } | |
302 | ||
303 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, | |
304 | unsigned int flags) | |
305 | { | |
306 | struct ovl_entry *oe; | |
307 | struct dentry *upperdir; | |
308 | struct dentry *lowerdir; | |
309 | struct dentry *upperdentry = NULL; | |
310 | struct dentry *lowerdentry = NULL; | |
311 | struct inode *inode = NULL; | |
312 | int err; | |
313 | ||
314 | err = -ENOMEM; | |
315 | oe = ovl_alloc_entry(); | |
316 | if (!oe) | |
317 | goto out; | |
318 | ||
319 | upperdir = ovl_dentry_upper(dentry->d_parent); | |
320 | lowerdir = ovl_dentry_lower(dentry->d_parent); | |
321 | ||
322 | if (upperdir) { | |
323 | upperdentry = ovl_lookup_real(upperdir, &dentry->d_name); | |
324 | err = PTR_ERR(upperdentry); | |
325 | if (IS_ERR(upperdentry)) | |
326 | goto out_put_dir; | |
327 | ||
328 | if (lowerdir && upperdentry) { | |
329 | if (ovl_is_whiteout(upperdentry)) { | |
330 | dput(upperdentry); | |
331 | upperdentry = NULL; | |
332 | oe->opaque = true; | |
333 | } else if (ovl_is_opaquedir(upperdentry)) { | |
334 | oe->opaque = true; | |
335 | } | |
336 | } | |
337 | } | |
338 | if (lowerdir && !oe->opaque) { | |
339 | lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name); | |
340 | err = PTR_ERR(lowerdentry); | |
341 | if (IS_ERR(lowerdentry)) | |
342 | goto out_dput_upper; | |
343 | } | |
344 | ||
345 | if (lowerdentry && upperdentry && | |
346 | (!S_ISDIR(upperdentry->d_inode->i_mode) || | |
347 | !S_ISDIR(lowerdentry->d_inode->i_mode))) { | |
348 | dput(lowerdentry); | |
349 | lowerdentry = NULL; | |
350 | oe->opaque = true; | |
351 | } | |
352 | ||
353 | if (lowerdentry || upperdentry) { | |
354 | struct dentry *realdentry; | |
355 | ||
356 | realdentry = upperdentry ? upperdentry : lowerdentry; | |
357 | err = -ENOMEM; | |
358 | inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, | |
359 | oe); | |
360 | if (!inode) | |
361 | goto out_dput; | |
362 | ovl_copyattr(realdentry->d_inode, inode); | |
363 | } | |
364 | ||
365 | oe->__upperdentry = upperdentry; | |
366 | oe->lowerdentry = lowerdentry; | |
367 | ||
368 | dentry->d_fsdata = oe; | |
369 | d_add(dentry, inode); | |
370 | ||
371 | return NULL; | |
372 | ||
373 | out_dput: | |
374 | dput(lowerdentry); | |
375 | out_dput_upper: | |
376 | dput(upperdentry); | |
377 | out_put_dir: | |
378 | kfree(oe); | |
379 | out: | |
380 | return ERR_PTR(err); | |
381 | } | |
382 | ||
383 | struct file *ovl_path_open(struct path *path, int flags) | |
384 | { | |
385 | return dentry_open(path, flags, current_cred()); | |
386 | } | |
387 | ||
388 | static void ovl_put_super(struct super_block *sb) | |
389 | { | |
390 | struct ovl_fs *ufs = sb->s_fs_info; | |
391 | ||
392 | dput(ufs->workdir); | |
393 | mntput(ufs->upper_mnt); | |
394 | mntput(ufs->lower_mnt); | |
395 | ||
f45827e8 EZ |
396 | kfree(ufs->config.lowerdir); |
397 | kfree(ufs->config.upperdir); | |
398 | kfree(ufs->config.workdir); | |
e9be9d5e MS |
399 | kfree(ufs); |
400 | } | |
401 | ||
cc259639 AW |
402 | /** |
403 | * ovl_statfs | |
404 | * @sb: The overlayfs super block | |
405 | * @buf: The struct kstatfs to fill in with stats | |
406 | * | |
407 | * Get the filesystem statistics. As writes always target the upper layer | |
408 | * filesystem pass the statfs to the same filesystem. | |
409 | */ | |
410 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) | |
411 | { | |
412 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
413 | struct dentry *root_dentry = dentry->d_sb->s_root; | |
414 | struct path path; | |
415 | int err; | |
416 | ||
417 | ovl_path_upper(root_dentry, &path); | |
418 | ||
419 | err = vfs_statfs(&path, buf); | |
420 | if (!err) { | |
421 | buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); | |
422 | buf->f_type = OVERLAYFS_SUPER_MAGIC; | |
423 | } | |
424 | ||
425 | return err; | |
426 | } | |
427 | ||
f45827e8 EZ |
428 | /** |
429 | * ovl_show_options | |
430 | * | |
431 | * Prints the mount options for a given superblock. | |
432 | * Returns zero; does not fail. | |
433 | */ | |
434 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) | |
435 | { | |
436 | struct super_block *sb = dentry->d_sb; | |
437 | struct ovl_fs *ufs = sb->s_fs_info; | |
438 | ||
439 | seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir); | |
440 | seq_printf(m, ",upperdir=%s", ufs->config.upperdir); | |
441 | seq_printf(m, ",workdir=%s", ufs->config.workdir); | |
442 | return 0; | |
443 | } | |
444 | ||
e9be9d5e MS |
445 | static const struct super_operations ovl_super_operations = { |
446 | .put_super = ovl_put_super, | |
cc259639 | 447 | .statfs = ovl_statfs, |
f45827e8 | 448 | .show_options = ovl_show_options, |
e9be9d5e MS |
449 | }; |
450 | ||
451 | enum { | |
452 | OPT_LOWERDIR, | |
453 | OPT_UPPERDIR, | |
454 | OPT_WORKDIR, | |
455 | OPT_ERR, | |
456 | }; | |
457 | ||
458 | static const match_table_t ovl_tokens = { | |
459 | {OPT_LOWERDIR, "lowerdir=%s"}, | |
460 | {OPT_UPPERDIR, "upperdir=%s"}, | |
461 | {OPT_WORKDIR, "workdir=%s"}, | |
462 | {OPT_ERR, NULL} | |
463 | }; | |
464 | ||
465 | static int ovl_parse_opt(char *opt, struct ovl_config *config) | |
466 | { | |
467 | char *p; | |
468 | ||
e9be9d5e MS |
469 | while ((p = strsep(&opt, ",")) != NULL) { |
470 | int token; | |
471 | substring_t args[MAX_OPT_ARGS]; | |
472 | ||
473 | if (!*p) | |
474 | continue; | |
475 | ||
476 | token = match_token(p, ovl_tokens, args); | |
477 | switch (token) { | |
478 | case OPT_UPPERDIR: | |
479 | kfree(config->upperdir); | |
480 | config->upperdir = match_strdup(&args[0]); | |
481 | if (!config->upperdir) | |
482 | return -ENOMEM; | |
483 | break; | |
484 | ||
485 | case OPT_LOWERDIR: | |
486 | kfree(config->lowerdir); | |
487 | config->lowerdir = match_strdup(&args[0]); | |
488 | if (!config->lowerdir) | |
489 | return -ENOMEM; | |
490 | break; | |
491 | ||
492 | case OPT_WORKDIR: | |
493 | kfree(config->workdir); | |
494 | config->workdir = match_strdup(&args[0]); | |
495 | if (!config->workdir) | |
496 | return -ENOMEM; | |
497 | break; | |
498 | ||
499 | default: | |
500 | return -EINVAL; | |
501 | } | |
502 | } | |
503 | return 0; | |
504 | } | |
505 | ||
506 | #define OVL_WORKDIR_NAME "work" | |
507 | ||
508 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, | |
509 | struct dentry *dentry) | |
510 | { | |
511 | struct inode *dir = dentry->d_inode; | |
512 | struct dentry *work; | |
513 | int err; | |
514 | bool retried = false; | |
515 | ||
516 | err = mnt_want_write(mnt); | |
517 | if (err) | |
518 | return ERR_PTR(err); | |
519 | ||
520 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); | |
521 | retry: | |
522 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, | |
523 | strlen(OVL_WORKDIR_NAME)); | |
524 | ||
525 | if (!IS_ERR(work)) { | |
526 | struct kstat stat = { | |
527 | .mode = S_IFDIR | 0, | |
528 | }; | |
529 | ||
530 | if (work->d_inode) { | |
531 | err = -EEXIST; | |
532 | if (retried) | |
533 | goto out_dput; | |
534 | ||
535 | retried = true; | |
536 | ovl_cleanup(dir, work); | |
537 | dput(work); | |
538 | goto retry; | |
539 | } | |
540 | ||
541 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); | |
542 | if (err) | |
543 | goto out_dput; | |
544 | } | |
545 | out_unlock: | |
546 | mutex_unlock(&dir->i_mutex); | |
547 | mnt_drop_write(mnt); | |
548 | ||
549 | return work; | |
550 | ||
551 | out_dput: | |
552 | dput(work); | |
553 | work = ERR_PTR(err); | |
554 | goto out_unlock; | |
555 | } | |
556 | ||
557 | static int ovl_mount_dir(const char *name, struct path *path) | |
558 | { | |
559 | int err; | |
560 | ||
561 | err = kern_path(name, LOOKUP_FOLLOW, path); | |
562 | if (err) { | |
563 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); | |
564 | err = -EINVAL; | |
565 | } | |
566 | return err; | |
567 | } | |
568 | ||
569 | static bool ovl_is_allowed_fs_type(struct dentry *root) | |
570 | { | |
571 | const struct dentry_operations *dop = root->d_op; | |
572 | ||
573 | /* | |
574 | * We don't support: | |
575 | * - automount filesystems | |
576 | * - filesystems with revalidate (FIXME for lower layer) | |
577 | * - filesystems with case insensitive names | |
578 | */ | |
579 | if (dop && | |
580 | (dop->d_manage || dop->d_automount || | |
581 | dop->d_revalidate || dop->d_weak_revalidate || | |
582 | dop->d_compare || dop->d_hash)) { | |
583 | return false; | |
584 | } | |
585 | return true; | |
586 | } | |
587 | ||
588 | /* Workdir should not be subdir of upperdir and vice versa */ | |
589 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) | |
590 | { | |
591 | bool ok = false; | |
592 | ||
593 | if (workdir != upperdir) { | |
594 | ok = (lock_rename(workdir, upperdir) == NULL); | |
595 | unlock_rename(workdir, upperdir); | |
596 | } | |
597 | return ok; | |
598 | } | |
599 | ||
600 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) | |
601 | { | |
602 | struct path lowerpath; | |
603 | struct path upperpath; | |
604 | struct path workpath; | |
605 | struct inode *root_inode; | |
606 | struct dentry *root_dentry; | |
607 | struct ovl_entry *oe; | |
608 | struct ovl_fs *ufs; | |
cc259639 | 609 | struct kstatfs statfs; |
e9be9d5e MS |
610 | int err; |
611 | ||
f45827e8 EZ |
612 | err = -ENOMEM; |
613 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); | |
614 | if (!ufs) | |
e9be9d5e MS |
615 | goto out; |
616 | ||
f45827e8 EZ |
617 | err = ovl_parse_opt((char *) data, &ufs->config); |
618 | if (err) | |
619 | goto out_free_config; | |
620 | ||
e9be9d5e MS |
621 | /* FIXME: workdir is not needed for a R/O mount */ |
622 | err = -EINVAL; | |
f45827e8 EZ |
623 | if (!ufs->config.upperdir || !ufs->config.lowerdir || |
624 | !ufs->config.workdir) { | |
e9be9d5e MS |
625 | pr_err("overlayfs: missing upperdir or lowerdir or workdir\n"); |
626 | goto out_free_config; | |
627 | } | |
628 | ||
629 | err = -ENOMEM; | |
e9be9d5e MS |
630 | oe = ovl_alloc_entry(); |
631 | if (oe == NULL) | |
f45827e8 | 632 | goto out_free_config; |
e9be9d5e | 633 | |
f45827e8 | 634 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
e9be9d5e MS |
635 | if (err) |
636 | goto out_free_oe; | |
637 | ||
f45827e8 | 638 | err = ovl_mount_dir(ufs->config.lowerdir, &lowerpath); |
e9be9d5e MS |
639 | if (err) |
640 | goto out_put_upperpath; | |
641 | ||
f45827e8 | 642 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
e9be9d5e MS |
643 | if (err) |
644 | goto out_put_lowerpath; | |
645 | ||
646 | err = -EINVAL; | |
647 | if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) || | |
648 | !S_ISDIR(lowerpath.dentry->d_inode->i_mode) || | |
649 | !S_ISDIR(workpath.dentry->d_inode->i_mode)) { | |
650 | pr_err("overlayfs: upperdir or lowerdir or workdir not a directory\n"); | |
651 | goto out_put_workpath; | |
652 | } | |
653 | ||
654 | if (upperpath.mnt != workpath.mnt) { | |
655 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); | |
656 | goto out_put_workpath; | |
657 | } | |
658 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { | |
659 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); | |
660 | goto out_put_workpath; | |
661 | } | |
662 | ||
663 | if (!ovl_is_allowed_fs_type(upperpath.dentry)) { | |
664 | pr_err("overlayfs: filesystem of upperdir is not supported\n"); | |
665 | goto out_put_workpath; | |
666 | } | |
667 | ||
668 | if (!ovl_is_allowed_fs_type(lowerpath.dentry)) { | |
669 | pr_err("overlayfs: filesystem of lowerdir is not supported\n"); | |
670 | goto out_put_workpath; | |
671 | } | |
672 | ||
cc259639 AW |
673 | err = vfs_statfs(&lowerpath, &statfs); |
674 | if (err) { | |
675 | pr_err("overlayfs: statfs failed on lowerpath\n"); | |
676 | goto out_put_workpath; | |
677 | } | |
678 | ufs->lower_namelen = statfs.f_namelen; | |
679 | ||
e9be9d5e MS |
680 | ufs->upper_mnt = clone_private_mount(&upperpath); |
681 | err = PTR_ERR(ufs->upper_mnt); | |
682 | if (IS_ERR(ufs->upper_mnt)) { | |
683 | pr_err("overlayfs: failed to clone upperpath\n"); | |
684 | goto out_put_workpath; | |
685 | } | |
686 | ||
687 | ufs->lower_mnt = clone_private_mount(&lowerpath); | |
688 | err = PTR_ERR(ufs->lower_mnt); | |
689 | if (IS_ERR(ufs->lower_mnt)) { | |
690 | pr_err("overlayfs: failed to clone lowerpath\n"); | |
691 | goto out_put_upper_mnt; | |
692 | } | |
693 | ||
694 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); | |
695 | err = PTR_ERR(ufs->workdir); | |
696 | if (IS_ERR(ufs->workdir)) { | |
697 | pr_err("overlayfs: failed to create directory %s/%s\n", | |
f45827e8 | 698 | ufs->config.workdir, OVL_WORKDIR_NAME); |
e9be9d5e MS |
699 | goto out_put_lower_mnt; |
700 | } | |
701 | ||
702 | /* | |
703 | * Make lower_mnt R/O. That way fchmod/fchown on lower file | |
704 | * will fail instead of modifying lower fs. | |
705 | */ | |
706 | ufs->lower_mnt->mnt_flags |= MNT_READONLY; | |
707 | ||
708 | /* If the upper fs is r/o, we mark overlayfs r/o too */ | |
709 | if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY) | |
710 | sb->s_flags |= MS_RDONLY; | |
711 | ||
712 | sb->s_d_op = &ovl_dentry_operations; | |
713 | ||
714 | err = -ENOMEM; | |
715 | root_inode = ovl_new_inode(sb, S_IFDIR, oe); | |
716 | if (!root_inode) | |
717 | goto out_put_workdir; | |
718 | ||
719 | root_dentry = d_make_root(root_inode); | |
720 | if (!root_dentry) | |
721 | goto out_put_workdir; | |
722 | ||
723 | mntput(upperpath.mnt); | |
724 | mntput(lowerpath.mnt); | |
725 | path_put(&workpath); | |
726 | ||
727 | oe->__upperdentry = upperpath.dentry; | |
728 | oe->lowerdentry = lowerpath.dentry; | |
729 | ||
730 | root_dentry->d_fsdata = oe; | |
731 | ||
cc259639 | 732 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
e9be9d5e MS |
733 | sb->s_op = &ovl_super_operations; |
734 | sb->s_root = root_dentry; | |
735 | sb->s_fs_info = ufs; | |
736 | ||
737 | return 0; | |
738 | ||
739 | out_put_workdir: | |
740 | dput(ufs->workdir); | |
741 | out_put_lower_mnt: | |
742 | mntput(ufs->lower_mnt); | |
743 | out_put_upper_mnt: | |
744 | mntput(ufs->upper_mnt); | |
745 | out_put_workpath: | |
746 | path_put(&workpath); | |
747 | out_put_lowerpath: | |
748 | path_put(&lowerpath); | |
749 | out_put_upperpath: | |
750 | path_put(&upperpath); | |
751 | out_free_oe: | |
752 | kfree(oe); | |
e9be9d5e | 753 | out_free_config: |
f45827e8 EZ |
754 | kfree(ufs->config.lowerdir); |
755 | kfree(ufs->config.upperdir); | |
756 | kfree(ufs->config.workdir); | |
757 | kfree(ufs); | |
e9be9d5e MS |
758 | out: |
759 | return err; | |
760 | } | |
761 | ||
762 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, | |
763 | const char *dev_name, void *raw_data) | |
764 | { | |
765 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); | |
766 | } | |
767 | ||
768 | static struct file_system_type ovl_fs_type = { | |
769 | .owner = THIS_MODULE, | |
770 | .name = "overlayfs", | |
771 | .mount = ovl_mount, | |
772 | .kill_sb = kill_anon_super, | |
773 | }; | |
774 | MODULE_ALIAS_FS("overlayfs"); | |
775 | ||
776 | static int __init ovl_init(void) | |
777 | { | |
778 | return register_filesystem(&ovl_fs_type); | |
779 | } | |
780 | ||
781 | static void __exit ovl_exit(void) | |
782 | { | |
783 | unregister_filesystem(&ovl_fs_type); | |
784 | } | |
785 | ||
786 | module_init(ovl_init); | |
787 | module_exit(ovl_exit); |