]>
Commit | Line | Data |
---|---|---|
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> | |
19 | #include <linux/statfs.h> | |
20 | #include <linux/seq_file.h> | |
21 | #include "overlayfs.h" | |
22 | ||
23 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); | |
24 | MODULE_DESCRIPTION("Overlay filesystem"); | |
25 | MODULE_LICENSE("GPL"); | |
26 | ||
27 | #define OVERLAYFS_SUPER_MAGIC 0x794c7630 | |
28 | ||
29 | struct ovl_config { | |
30 | char *lowerdir; | |
31 | char *upperdir; | |
32 | char *workdir; | |
33 | }; | |
34 | ||
35 | /* private information held for overlayfs's superblock */ | |
36 | struct ovl_fs { | |
37 | struct vfsmount *upper_mnt; | |
38 | unsigned numlower; | |
39 | struct vfsmount **lower_mnt; | |
40 | struct dentry *workdir; | |
41 | long lower_namelen; | |
42 | /* pathnames of lower and upper dirs, for show_options */ | |
43 | struct ovl_config config; | |
44 | }; | |
45 | ||
46 | struct ovl_dir_cache; | |
47 | ||
48 | /* private information held for every overlayfs dentry */ | |
49 | struct ovl_entry { | |
50 | struct dentry *__upperdentry; | |
51 | struct ovl_dir_cache *cache; | |
52 | union { | |
53 | struct { | |
54 | u64 version; | |
55 | bool opaque; | |
56 | }; | |
57 | struct rcu_head rcu; | |
58 | }; | |
59 | unsigned numlower; | |
60 | struct path lowerstack[]; | |
61 | }; | |
62 | ||
63 | #define OVL_MAX_STACK 500 | |
64 | ||
65 | static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe) | |
66 | { | |
67 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; | |
68 | } | |
69 | ||
70 | enum ovl_path_type ovl_path_type(struct dentry *dentry) | |
71 | { | |
72 | struct ovl_entry *oe = dentry->d_fsdata; | |
73 | enum ovl_path_type type = 0; | |
74 | ||
75 | if (oe->__upperdentry) { | |
76 | type = __OVL_PATH_UPPER; | |
77 | ||
78 | if (oe->numlower) { | |
79 | if (S_ISDIR(dentry->d_inode->i_mode)) | |
80 | type |= __OVL_PATH_MERGE; | |
81 | } else if (!oe->opaque) { | |
82 | type |= __OVL_PATH_PURE; | |
83 | } | |
84 | } else { | |
85 | if (oe->numlower > 1) | |
86 | type |= __OVL_PATH_MERGE; | |
87 | } | |
88 | return type; | |
89 | } | |
90 | ||
91 | static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) | |
92 | { | |
93 | return lockless_dereference(oe->__upperdentry); | |
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 | { | |
107 | enum ovl_path_type type = ovl_path_type(dentry); | |
108 | ||
109 | if (!OVL_TYPE_UPPER(type)) | |
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 __ovl_dentry_lower(oe); | |
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 = __ovl_dentry_lower(oe); | |
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 = __ovl_dentry_lower(oe); | |
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_entry *oe = dentry->d_fsdata; | |
174 | ||
175 | *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; | |
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 | ||
255 | res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1); | |
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) { | |
267 | unsigned int i; | |
268 | ||
269 | dput(oe->__upperdentry); | |
270 | for (i = 0; i < oe->numlower; i++) | |
271 | dput(oe->lowerstack[i].dentry); | |
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 | ||
280 | static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) | |
281 | { | |
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; | |
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; | |
306 | } | |
307 | return dentry; | |
308 | } | |
309 | ||
310 | /* | |
311 | * Returns next layer in stack starting from top. | |
312 | * Returns -1 if this is the last layer. | |
313 | */ | |
314 | int ovl_path_next(int idx, struct dentry *dentry, struct path *path) | |
315 | { | |
316 | struct ovl_entry *oe = dentry->d_fsdata; | |
317 | ||
318 | BUG_ON(idx < 0); | |
319 | if (idx == 0) { | |
320 | ovl_path_upper(dentry, path); | |
321 | if (path->dentry) | |
322 | return oe->numlower ? 1 : -1; | |
323 | idx++; | |
324 | } | |
325 | BUG_ON(idx > oe->numlower); | |
326 | *path = oe->lowerstack[idx - 1]; | |
327 | ||
328 | return (idx < oe->numlower) ? idx + 1 : -1; | |
329 | } | |
330 | ||
331 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, | |
332 | unsigned int flags) | |
333 | { | |
334 | struct ovl_entry *oe; | |
335 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; | |
336 | struct path *stack = NULL; | |
337 | struct dentry *upperdir, *upperdentry = NULL; | |
338 | unsigned int ctr = 0; | |
339 | struct inode *inode = NULL; | |
340 | bool upperopaque = false; | |
341 | struct dentry *this, *prev = NULL; | |
342 | unsigned int i; | |
343 | int err; | |
344 | ||
345 | upperdir = ovl_upperdentry_dereference(poe); | |
346 | if (upperdir) { | |
347 | this = ovl_lookup_real(upperdir, &dentry->d_name); | |
348 | err = PTR_ERR(this); | |
349 | if (IS_ERR(this)) | |
350 | goto out; | |
351 | ||
352 | if (this) { | |
353 | if (ovl_is_whiteout(this)) { | |
354 | dput(this); | |
355 | this = NULL; | |
356 | upperopaque = true; | |
357 | } else if (poe->numlower && ovl_is_opaquedir(this)) { | |
358 | upperopaque = true; | |
359 | } | |
360 | } | |
361 | upperdentry = prev = this; | |
362 | } | |
363 | ||
364 | if (!upperopaque && poe->numlower) { | |
365 | err = -ENOMEM; | |
366 | stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL); | |
367 | if (!stack) | |
368 | goto out_put_upper; | |
369 | } | |
370 | ||
371 | for (i = 0; !upperopaque && i < poe->numlower; i++) { | |
372 | bool opaque = false; | |
373 | struct path lowerpath = poe->lowerstack[i]; | |
374 | ||
375 | this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name); | |
376 | err = PTR_ERR(this); | |
377 | if (IS_ERR(this)) { | |
378 | /* | |
379 | * If it's positive, then treat ENAMETOOLONG as ENOENT. | |
380 | */ | |
381 | if (err == -ENAMETOOLONG && (upperdentry || ctr)) | |
382 | continue; | |
383 | goto out_put; | |
384 | } | |
385 | if (!this) | |
386 | continue; | |
387 | if (ovl_is_whiteout(this)) { | |
388 | dput(this); | |
389 | break; | |
390 | } | |
391 | /* | |
392 | * Only makes sense to check opaque dir if this is not the | |
393 | * lowermost layer. | |
394 | */ | |
395 | if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) | |
396 | opaque = true; | |
397 | ||
398 | if (prev && (!S_ISDIR(prev->d_inode->i_mode) || | |
399 | !S_ISDIR(this->d_inode->i_mode))) { | |
400 | /* | |
401 | * FIXME: check for upper-opaqueness maybe better done | |
402 | * in remove code. | |
403 | */ | |
404 | if (prev == upperdentry) | |
405 | upperopaque = true; | |
406 | dput(this); | |
407 | break; | |
408 | } | |
409 | /* | |
410 | * If this is a non-directory then stop here. | |
411 | */ | |
412 | if (!S_ISDIR(this->d_inode->i_mode)) | |
413 | opaque = true; | |
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) { | |
429 | struct dentry *realdentry; | |
430 | ||
431 | realdentry = upperdentry ? upperdentry : stack[0].dentry; | |
432 | ||
433 | err = -ENOMEM; | |
434 | inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, | |
435 | oe); | |
436 | if (!inode) | |
437 | goto out_free_oe; | |
438 | ovl_copyattr(realdentry->d_inode, inode); | |
439 | } | |
440 | ||
441 | oe->opaque = upperopaque; | |
442 | oe->__upperdentry = upperdentry; | |
443 | memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); | |
444 | kfree(stack); | |
445 | dentry->d_fsdata = oe; | |
446 | d_add(dentry, inode); | |
447 | ||
448 | return NULL; | |
449 | ||
450 | out_free_oe: | |
451 | kfree(oe); | |
452 | out_put: | |
453 | for (i = 0; i < ctr; i++) | |
454 | dput(stack[i].dentry); | |
455 | kfree(stack); | |
456 | out_put_upper: | |
457 | dput(upperdentry); | |
458 | out: | |
459 | return ERR_PTR(err); | |
460 | } | |
461 | ||
462 | struct file *ovl_path_open(struct path *path, int flags) | |
463 | { | |
464 | return dentry_open(path, flags, current_cred()); | |
465 | } | |
466 | ||
467 | static void ovl_put_super(struct super_block *sb) | |
468 | { | |
469 | struct ovl_fs *ufs = sb->s_fs_info; | |
470 | unsigned i; | |
471 | ||
472 | dput(ufs->workdir); | |
473 | mntput(ufs->upper_mnt); | |
474 | for (i = 0; i < ufs->numlower; i++) | |
475 | mntput(ufs->lower_mnt[i]); | |
476 | ||
477 | kfree(ufs->config.lowerdir); | |
478 | kfree(ufs->config.upperdir); | |
479 | kfree(ufs->config.workdir); | |
480 | kfree(ufs); | |
481 | } | |
482 | ||
483 | /** | |
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 | |
489 | * filesystem pass the statfs to the upper filesystem (if it exists) | |
490 | */ | |
491 | static 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 | ||
498 | ovl_path_real(root_dentry, &path); | |
499 | ||
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 | ||
509 | /** | |
510 | * ovl_show_options | |
511 | * | |
512 | * Prints the mount options for a given superblock. | |
513 | * Returns zero; does not fail. | |
514 | */ | |
515 | static 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); | |
521 | if (ufs->config.upperdir) { | |
522 | seq_printf(m, ",upperdir=%s", ufs->config.upperdir); | |
523 | seq_printf(m, ",workdir=%s", ufs->config.workdir); | |
524 | } | |
525 | return 0; | |
526 | } | |
527 | ||
528 | static const struct super_operations ovl_super_operations = { | |
529 | .put_super = ovl_put_super, | |
530 | .statfs = ovl_statfs, | |
531 | .show_options = ovl_show_options, | |
532 | }; | |
533 | ||
534 | enum { | |
535 | OPT_LOWERDIR, | |
536 | OPT_UPPERDIR, | |
537 | OPT_WORKDIR, | |
538 | OPT_ERR, | |
539 | }; | |
540 | ||
541 | static 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 | ||
548 | static 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 | ||
571 | static int ovl_parse_opt(char *opt, struct ovl_config *config) | |
572 | { | |
573 | char *p; | |
574 | ||
575 | while ((p = ovl_next_opt(&opt)) != NULL) { | |
576 | 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 | ||
614 | static 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); | |
627 | retry: | |
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 | } | |
651 | out_unlock: | |
652 | mutex_unlock(&dir->i_mutex); | |
653 | mnt_drop_write(mnt); | |
654 | ||
655 | return work; | |
656 | ||
657 | out_dput: | |
658 | dput(work); | |
659 | work = ERR_PTR(err); | |
660 | goto out_unlock; | |
661 | } | |
662 | ||
663 | static 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 | ||
676 | static 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 | ||
695 | static int ovl_mount_dir_noesc(const char *name, struct path *path) | |
696 | { | |
697 | int err = -EINVAL; | |
698 | ||
699 | if (!*name) { | |
700 | pr_err("overlayfs: empty lowerdir\n"); | |
701 | goto out; | |
702 | } | |
703 | 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 | ||
719 | out_put: | |
720 | path_put(path); | |
721 | out: | |
722 | return err; | |
723 | } | |
724 | ||
725 | static 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 | ||
738 | static 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 | ||
744 | err = ovl_mount_dir_noesc(name, path); | |
745 | 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 | ||
758 | out_put: | |
759 | path_put(path); | |
760 | out: | |
761 | return err; | |
762 | } | |
763 | ||
764 | /* Workdir should not be subdir of upperdir and vice versa */ | |
765 | static 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 | ||
776 | static 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 | ||
796 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) | |
797 | { | |
798 | struct path upperpath = { NULL, NULL }; | |
799 | struct path workpath = { NULL, NULL }; | |
800 | struct dentry *root_dentry; | |
801 | struct ovl_entry *oe; | |
802 | struct ovl_fs *ufs; | |
803 | struct path *stack = NULL; | |
804 | char *lowertmp; | |
805 | char *lower; | |
806 | unsigned int numlower; | |
807 | unsigned int stacklen = 0; | |
808 | unsigned int i; | |
809 | int err; | |
810 | ||
811 | err = -ENOMEM; | |
812 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); | |
813 | if (!ufs) | |
814 | goto out; | |
815 | ||
816 | err = ovl_parse_opt((char *) data, &ufs->config); | |
817 | if (err) | |
818 | goto out_free_config; | |
819 | ||
820 | err = -EINVAL; | |
821 | if (!ufs->config.lowerdir) { | |
822 | pr_err("overlayfs: missing 'lowerdir'\n"); | |
823 | goto out_free_config; | |
824 | } | |
825 | ||
826 | 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 | } | |
833 | ||
834 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); | |
835 | if (err) | |
836 | goto out_free_config; | |
837 | ||
838 | err = ovl_mount_dir(ufs->config.workdir, &workpath); | |
839 | if (err) | |
840 | goto out_put_upperpath; | |
841 | ||
842 | err = -EINVAL; | |
843 | if (upperpath.mnt != workpath.mnt) { | |
844 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); | |
845 | goto out_put_workpath; | |
846 | } | |
847 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { | |
848 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); | |
849 | goto out_put_workpath; | |
850 | } | |
851 | sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; | |
852 | } | |
853 | err = -ENOMEM; | |
854 | lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); | |
855 | if (!lowertmp) | |
856 | goto out_put_workpath; | |
857 | ||
858 | err = -EINVAL; | |
859 | stacklen = ovl_split_lowerdirs(lowertmp); | |
860 | if (stacklen > OVL_MAX_STACK) | |
861 | goto out_free_lowertmp; | |
862 | ||
863 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); | |
864 | if (!stack) | |
865 | goto out_free_lowertmp; | |
866 | ||
867 | lower = lowertmp; | |
868 | for (numlower = 0; numlower < stacklen; numlower++) { | |
869 | err = ovl_lower_dir(lower, &stack[numlower], | |
870 | &ufs->lower_namelen, &sb->s_stack_depth); | |
871 | if (err) | |
872 | goto out_put_lowerpath; | |
873 | ||
874 | lower = strchr(lower, '\0') + 1; | |
875 | } | |
876 | ||
877 | err = -EINVAL; | |
878 | sb->s_stack_depth++; | |
879 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { | |
880 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); | |
881 | goto out_put_lowerpath; | |
882 | } | |
883 | ||
884 | if (ufs->config.upperdir) { | |
885 | ufs->upper_mnt = clone_private_mount(&upperpath); | |
886 | err = PTR_ERR(ufs->upper_mnt); | |
887 | if (IS_ERR(ufs->upper_mnt)) { | |
888 | pr_err("overlayfs: failed to clone upperpath\n"); | |
889 | goto out_put_lowerpath; | |
890 | } | |
891 | ||
892 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); | |
893 | err = PTR_ERR(ufs->workdir); | |
894 | if (IS_ERR(ufs->workdir)) { | |
895 | pr_err("overlayfs: failed to create directory %s/%s\n", | |
896 | ufs->config.workdir, OVL_WORKDIR_NAME); | |
897 | goto out_put_upper_mnt; | |
898 | } | |
899 | } | |
900 | ||
901 | err = -ENOMEM; | |
902 | ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); | |
903 | if (ufs->lower_mnt == NULL) | |
904 | goto out_put_workdir; | |
905 | for (i = 0; i < numlower; i++) { | |
906 | struct vfsmount *mnt = clone_private_mount(&stack[i]); | |
907 | ||
908 | err = PTR_ERR(mnt); | |
909 | if (IS_ERR(mnt)) { | |
910 | pr_err("overlayfs: failed to clone lowerpath\n"); | |
911 | goto out_put_lower_mnt; | |
912 | } | |
913 | /* | |
914 | * Make lower_mnt R/O. That way fchmod/fchown on lower file | |
915 | * will fail instead of modifying lower fs. | |
916 | */ | |
917 | mnt->mnt_flags |= MNT_READONLY; | |
918 | ||
919 | ufs->lower_mnt[ufs->numlower] = mnt; | |
920 | ufs->numlower++; | |
921 | } | |
922 | ||
923 | /* If the upper fs is r/o or nonexistent, we mark overlayfs r/o too */ | |
924 | if (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)) | |
925 | sb->s_flags |= MS_RDONLY; | |
926 | ||
927 | sb->s_d_op = &ovl_dentry_operations; | |
928 | ||
929 | err = -ENOMEM; | |
930 | oe = ovl_alloc_entry(numlower); | |
931 | if (!oe) | |
932 | goto out_put_lower_mnt; | |
933 | ||
934 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe)); | |
935 | if (!root_dentry) | |
936 | goto out_free_oe; | |
937 | ||
938 | mntput(upperpath.mnt); | |
939 | for (i = 0; i < numlower; i++) | |
940 | mntput(stack[i].mnt); | |
941 | path_put(&workpath); | |
942 | kfree(lowertmp); | |
943 | ||
944 | oe->__upperdentry = upperpath.dentry; | |
945 | for (i = 0; i < numlower; i++) { | |
946 | oe->lowerstack[i].dentry = stack[i].dentry; | |
947 | oe->lowerstack[i].mnt = ufs->lower_mnt[i]; | |
948 | } | |
949 | ||
950 | root_dentry->d_fsdata = oe; | |
951 | ||
952 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; | |
953 | sb->s_op = &ovl_super_operations; | |
954 | sb->s_root = root_dentry; | |
955 | sb->s_fs_info = ufs; | |
956 | ||
957 | return 0; | |
958 | ||
959 | out_free_oe: | |
960 | kfree(oe); | |
961 | out_put_lower_mnt: | |
962 | for (i = 0; i < ufs->numlower; i++) | |
963 | mntput(ufs->lower_mnt[i]); | |
964 | kfree(ufs->lower_mnt); | |
965 | out_put_workdir: | |
966 | dput(ufs->workdir); | |
967 | out_put_upper_mnt: | |
968 | mntput(ufs->upper_mnt); | |
969 | out_put_lowerpath: | |
970 | for (i = 0; i < numlower; i++) | |
971 | path_put(&stack[i]); | |
972 | kfree(stack); | |
973 | out_free_lowertmp: | |
974 | kfree(lowertmp); | |
975 | out_put_workpath: | |
976 | path_put(&workpath); | |
977 | out_put_upperpath: | |
978 | path_put(&upperpath); | |
979 | out_free_config: | |
980 | kfree(ufs->config.lowerdir); | |
981 | kfree(ufs->config.upperdir); | |
982 | kfree(ufs->config.workdir); | |
983 | kfree(ufs); | |
984 | out: | |
985 | return err; | |
986 | } | |
987 | ||
988 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, | |
989 | const char *dev_name, void *raw_data) | |
990 | { | |
991 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); | |
992 | } | |
993 | ||
994 | static struct file_system_type ovl_fs_type = { | |
995 | .owner = THIS_MODULE, | |
996 | .name = "overlay", | |
997 | .mount = ovl_mount, | |
998 | .kill_sb = kill_anon_super, | |
999 | }; | |
1000 | MODULE_ALIAS_FS("overlay"); | |
1001 | ||
1002 | static int __init ovl_init(void) | |
1003 | { | |
1004 | return register_filesystem(&ovl_fs_type); | |
1005 | } | |
1006 | ||
1007 | static void __exit ovl_exit(void) | |
1008 | { | |
1009 | unregister_filesystem(&ovl_fs_type); | |
1010 | } | |
1011 | ||
1012 | module_init(ovl_init); | |
1013 | module_exit(ovl_exit); |