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