Commit | Line | Data |
---|---|---|
e9be9d5e MS |
1 | /* |
2 | * | |
3 | * Copyright (C) 2011 Novell Inc. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License version 2 as published by | |
7 | * the Free Software Foundation. | |
8 | */ | |
9 | ||
10 | #include <linux/fs.h> | |
11 | #include <linux/namei.h> | |
12 | #include <linux/xattr.h> | |
13 | #include <linux/security.h> | |
14 | #include <linux/mount.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/parser.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/sched.h> | |
cc259639 | 19 | #include <linux/statfs.h> |
f45827e8 | 20 | #include <linux/seq_file.h> |
e9be9d5e MS |
21 | #include "overlayfs.h" |
22 | ||
23 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); | |
24 | MODULE_DESCRIPTION("Overlay filesystem"); | |
25 | MODULE_LICENSE("GPL"); | |
26 | ||
ef94b186 | 27 | #define OVERLAYFS_SUPER_MAGIC 0x794c7630 |
cc259639 | 28 | |
f45827e8 EZ |
29 | struct ovl_config { |
30 | char *lowerdir; | |
31 | char *upperdir; | |
32 | char *workdir; | |
33 | }; | |
34 | ||
e9be9d5e MS |
35 | /* private information held for overlayfs's superblock */ |
36 | struct ovl_fs { | |
37 | struct vfsmount *upper_mnt; | |
dd662667 MS |
38 | unsigned numlower; |
39 | struct vfsmount **lower_mnt; | |
e9be9d5e | 40 | struct dentry *workdir; |
cc259639 | 41 | long lower_namelen; |
f45827e8 EZ |
42 | /* pathnames of lower and upper dirs, for show_options */ |
43 | struct ovl_config config; | |
e9be9d5e MS |
44 | }; |
45 | ||
46 | struct ovl_dir_cache; | |
47 | ||
48 | /* private information held for every overlayfs dentry */ | |
49 | struct ovl_entry { | |
50 | struct dentry *__upperdentry; | |
e9be9d5e MS |
51 | struct ovl_dir_cache *cache; |
52 | union { | |
53 | struct { | |
54 | u64 version; | |
55 | bool opaque; | |
56 | }; | |
57 | struct rcu_head rcu; | |
58 | }; | |
dd662667 MS |
59 | unsigned numlower; |
60 | struct path lowerstack[]; | |
e9be9d5e MS |
61 | }; |
62 | ||
a78d9f0d MS |
63 | #define OVL_MAX_STACK 500 |
64 | ||
dd662667 MS |
65 | static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe) |
66 | { | |
67 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; | |
68 | } | |
e9be9d5e MS |
69 | |
70 | enum ovl_path_type ovl_path_type(struct dentry *dentry) | |
71 | { | |
72 | struct ovl_entry *oe = dentry->d_fsdata; | |
1afaba1e | 73 | enum ovl_path_type type = 0; |
e9be9d5e MS |
74 | |
75 | if (oe->__upperdentry) { | |
1afaba1e MS |
76 | type = __OVL_PATH_UPPER; |
77 | ||
dd662667 | 78 | if (oe->numlower) { |
e9be9d5e | 79 | if (S_ISDIR(dentry->d_inode->i_mode)) |
1afaba1e MS |
80 | type |= __OVL_PATH_MERGE; |
81 | } else if (!oe->opaque) { | |
82 | type |= __OVL_PATH_PURE; | |
e9be9d5e | 83 | } |
9d7459d8 MS |
84 | } else { |
85 | if (oe->numlower > 1) | |
86 | type |= __OVL_PATH_MERGE; | |
e9be9d5e | 87 | } |
1afaba1e | 88 | return type; |
e9be9d5e MS |
89 | } |
90 | ||
91 | static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) | |
92 | { | |
71d50928 | 93 | return lockless_dereference(oe->__upperdentry); |
e9be9d5e MS |
94 | } |
95 | ||
96 | void ovl_path_upper(struct dentry *dentry, struct path *path) | |
97 | { | |
98 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
99 | struct ovl_entry *oe = dentry->d_fsdata; | |
100 | ||
101 | path->mnt = ofs->upper_mnt; | |
102 | path->dentry = ovl_upperdentry_dereference(oe); | |
103 | } | |
104 | ||
105 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) | |
106 | { | |
e9be9d5e MS |
107 | enum ovl_path_type type = ovl_path_type(dentry); |
108 | ||
1afaba1e | 109 | if (!OVL_TYPE_UPPER(type)) |
e9be9d5e MS |
110 | ovl_path_lower(dentry, path); |
111 | else | |
112 | ovl_path_upper(dentry, path); | |
113 | ||
114 | return type; | |
115 | } | |
116 | ||
117 | struct dentry *ovl_dentry_upper(struct dentry *dentry) | |
118 | { | |
119 | struct ovl_entry *oe = dentry->d_fsdata; | |
120 | ||
121 | return ovl_upperdentry_dereference(oe); | |
122 | } | |
123 | ||
124 | struct dentry *ovl_dentry_lower(struct dentry *dentry) | |
125 | { | |
126 | struct ovl_entry *oe = dentry->d_fsdata; | |
127 | ||
dd662667 | 128 | return __ovl_dentry_lower(oe); |
e9be9d5e MS |
129 | } |
130 | ||
131 | struct dentry *ovl_dentry_real(struct dentry *dentry) | |
132 | { | |
133 | struct ovl_entry *oe = dentry->d_fsdata; | |
134 | struct dentry *realdentry; | |
135 | ||
136 | realdentry = ovl_upperdentry_dereference(oe); | |
137 | if (!realdentry) | |
dd662667 | 138 | realdentry = __ovl_dentry_lower(oe); |
e9be9d5e MS |
139 | |
140 | return realdentry; | |
141 | } | |
142 | ||
143 | struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper) | |
144 | { | |
145 | struct dentry *realdentry; | |
146 | ||
147 | realdentry = ovl_upperdentry_dereference(oe); | |
148 | if (realdentry) { | |
149 | *is_upper = true; | |
150 | } else { | |
dd662667 | 151 | realdentry = __ovl_dentry_lower(oe); |
e9be9d5e MS |
152 | *is_upper = false; |
153 | } | |
154 | return realdentry; | |
155 | } | |
156 | ||
157 | struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) | |
158 | { | |
159 | struct ovl_entry *oe = dentry->d_fsdata; | |
160 | ||
161 | return oe->cache; | |
162 | } | |
163 | ||
164 | void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) | |
165 | { | |
166 | struct ovl_entry *oe = dentry->d_fsdata; | |
167 | ||
168 | oe->cache = cache; | |
169 | } | |
170 | ||
171 | void ovl_path_lower(struct dentry *dentry, struct path *path) | |
172 | { | |
e9be9d5e MS |
173 | struct ovl_entry *oe = dentry->d_fsdata; |
174 | ||
dd662667 | 175 | *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; |
e9be9d5e MS |
176 | } |
177 | ||
178 | int ovl_want_write(struct dentry *dentry) | |
179 | { | |
180 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
181 | return mnt_want_write(ofs->upper_mnt); | |
182 | } | |
183 | ||
184 | void ovl_drop_write(struct dentry *dentry) | |
185 | { | |
186 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
187 | mnt_drop_write(ofs->upper_mnt); | |
188 | } | |
189 | ||
190 | struct dentry *ovl_workdir(struct dentry *dentry) | |
191 | { | |
192 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
193 | return ofs->workdir; | |
194 | } | |
195 | ||
196 | bool ovl_dentry_is_opaque(struct dentry *dentry) | |
197 | { | |
198 | struct ovl_entry *oe = dentry->d_fsdata; | |
199 | return oe->opaque; | |
200 | } | |
201 | ||
202 | void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) | |
203 | { | |
204 | struct ovl_entry *oe = dentry->d_fsdata; | |
205 | oe->opaque = opaque; | |
206 | } | |
207 | ||
208 | void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) | |
209 | { | |
210 | struct ovl_entry *oe = dentry->d_fsdata; | |
211 | ||
212 | WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex)); | |
213 | WARN_ON(oe->__upperdentry); | |
214 | BUG_ON(!upperdentry->d_inode); | |
215 | /* | |
216 | * Make sure upperdentry is consistent before making it visible to | |
217 | * ovl_upperdentry_dereference(). | |
218 | */ | |
219 | smp_wmb(); | |
220 | oe->__upperdentry = upperdentry; | |
221 | } | |
222 | ||
223 | void ovl_dentry_version_inc(struct dentry *dentry) | |
224 | { | |
225 | struct ovl_entry *oe = dentry->d_fsdata; | |
226 | ||
227 | WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); | |
228 | oe->version++; | |
229 | } | |
230 | ||
231 | u64 ovl_dentry_version_get(struct dentry *dentry) | |
232 | { | |
233 | struct ovl_entry *oe = dentry->d_fsdata; | |
234 | ||
235 | WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); | |
236 | return oe->version; | |
237 | } | |
238 | ||
239 | bool ovl_is_whiteout(struct dentry *dentry) | |
240 | { | |
241 | struct inode *inode = dentry->d_inode; | |
242 | ||
243 | return inode && IS_WHITEOUT(inode); | |
244 | } | |
245 | ||
246 | static bool ovl_is_opaquedir(struct dentry *dentry) | |
247 | { | |
248 | int res; | |
249 | char val; | |
250 | struct inode *inode = dentry->d_inode; | |
251 | ||
252 | if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) | |
253 | return false; | |
254 | ||
cead89bb | 255 | res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1); |
e9be9d5e MS |
256 | if (res == 1 && val == 'y') |
257 | return true; | |
258 | ||
259 | return false; | |
260 | } | |
261 | ||
262 | static void ovl_dentry_release(struct dentry *dentry) | |
263 | { | |
264 | struct ovl_entry *oe = dentry->d_fsdata; | |
265 | ||
266 | if (oe) { | |
dd662667 MS |
267 | unsigned int i; |
268 | ||
e9be9d5e | 269 | dput(oe->__upperdentry); |
dd662667 MS |
270 | for (i = 0; i < oe->numlower; i++) |
271 | dput(oe->lowerstack[i].dentry); | |
e9be9d5e MS |
272 | kfree_rcu(oe, rcu); |
273 | } | |
274 | } | |
275 | ||
276 | static const struct dentry_operations ovl_dentry_operations = { | |
277 | .d_release = ovl_dentry_release, | |
278 | }; | |
279 | ||
dd662667 | 280 | static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
e9be9d5e | 281 | { |
dd662667 MS |
282 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); |
283 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); | |
284 | ||
285 | if (oe) | |
286 | oe->numlower = numlower; | |
287 | ||
288 | return oe; | |
e9be9d5e MS |
289 | } |
290 | ||
291 | static inline struct dentry *ovl_lookup_real(struct dentry *dir, | |
292 | struct qstr *name) | |
293 | { | |
294 | struct dentry *dentry; | |
295 | ||
296 | mutex_lock(&dir->d_inode->i_mutex); | |
297 | dentry = lookup_one_len(name->name, dir, name->len); | |
298 | mutex_unlock(&dir->d_inode->i_mutex); | |
299 | ||
300 | if (IS_ERR(dentry)) { | |
301 | if (PTR_ERR(dentry) == -ENOENT) | |
302 | dentry = NULL; | |
303 | } else if (!dentry->d_inode) { | |
304 | dput(dentry); | |
305 | dentry = NULL; | |
306 | } | |
307 | return dentry; | |
308 | } | |
309 | ||
5ef88da5 MS |
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 | ||
e9be9d5e MS |
331 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
332 | unsigned int flags) | |
333 | { | |
334 | struct ovl_entry *oe; | |
3d3c6b89 MS |
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; | |
e9be9d5e | 339 | struct inode *inode = NULL; |
3d3c6b89 MS |
340 | bool upperopaque = false; |
341 | struct dentry *this, *prev = NULL; | |
342 | unsigned int i; | |
e9be9d5e MS |
343 | int err; |
344 | ||
3d3c6b89 | 345 | upperdir = ovl_upperdentry_dereference(poe); |
e9be9d5e | 346 | if (upperdir) { |
3d3c6b89 MS |
347 | this = ovl_lookup_real(upperdir, &dentry->d_name); |
348 | err = PTR_ERR(this); | |
349 | if (IS_ERR(this)) | |
350 | goto out; | |
351 | ||
3e01cee3 | 352 | if (this) { |
3d3c6b89 MS |
353 | if (ovl_is_whiteout(this)) { |
354 | dput(this); | |
355 | this = NULL; | |
356 | upperopaque = true; | |
3e01cee3 | 357 | } else if (poe->numlower && ovl_is_opaquedir(this)) { |
3d3c6b89 | 358 | upperopaque = true; |
e9be9d5e MS |
359 | } |
360 | } | |
3d3c6b89 | 361 | upperdentry = prev = this; |
e9be9d5e | 362 | } |
3d3c6b89 MS |
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; | |
e9be9d5e MS |
369 | } |
370 | ||
3d3c6b89 MS |
371 | for (i = 0; !upperopaque && i < poe->numlower; i++) { |
372 | bool opaque = false; | |
373 | struct path lowerpath = poe->lowerstack[i]; | |
374 | ||
3d3c6b89 MS |
375 | this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name); |
376 | err = PTR_ERR(this); | |
09e10322 MS |
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; | |
3d3c6b89 | 383 | goto out_put; |
09e10322 | 384 | } |
3d3c6b89 MS |
385 | if (!this) |
386 | continue; | |
3e01cee3 MS |
387 | if (ovl_is_whiteout(this)) { |
388 | dput(this); | |
389 | break; | |
390 | } | |
3d3c6b89 | 391 | /* |
3e01cee3 MS |
392 | * Only makes sense to check opaque dir if this is not the |
393 | * lowermost layer. | |
3d3c6b89 | 394 | */ |
3e01cee3 MS |
395 | if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) |
396 | opaque = true; | |
a425c037 | 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 | */ | |
3d3c6b89 MS |
404 | if (prev == upperdentry) |
405 | upperopaque = true; | |
406 | dput(this); | |
407 | break; | |
408 | } | |
a425c037 | 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 | ||
3d3c6b89 MS |
415 | stack[ctr].dentry = this; |
416 | stack[ctr].mnt = lowerpath.mnt; | |
417 | ctr++; | |
418 | prev = this; | |
419 | if (opaque) | |
420 | break; | |
e9be9d5e MS |
421 | } |
422 | ||
3d3c6b89 MS |
423 | oe = ovl_alloc_entry(ctr); |
424 | err = -ENOMEM; | |
425 | if (!oe) | |
426 | goto out_put; | |
427 | ||
428 | if (upperdentry || ctr) { | |
e9be9d5e MS |
429 | struct dentry *realdentry; |
430 | ||
3d3c6b89 MS |
431 | realdentry = upperdentry ? upperdentry : stack[0].dentry; |
432 | ||
e9be9d5e MS |
433 | err = -ENOMEM; |
434 | inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, | |
435 | oe); | |
436 | if (!inode) | |
3d3c6b89 | 437 | goto out_free_oe; |
e9be9d5e MS |
438 | ovl_copyattr(realdentry->d_inode, inode); |
439 | } | |
440 | ||
3d3c6b89 | 441 | oe->opaque = upperopaque; |
e9be9d5e | 442 | oe->__upperdentry = upperdentry; |
3d3c6b89 MS |
443 | memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); |
444 | kfree(stack); | |
e9be9d5e MS |
445 | dentry->d_fsdata = oe; |
446 | d_add(dentry, inode); | |
447 | ||
448 | return NULL; | |
449 | ||
3d3c6b89 | 450 | out_free_oe: |
e9be9d5e | 451 | kfree(oe); |
3d3c6b89 MS |
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); | |
e9be9d5e MS |
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; | |
dd662667 | 470 | unsigned i; |
e9be9d5e MS |
471 | |
472 | dput(ufs->workdir); | |
473 | mntput(ufs->upper_mnt); | |
dd662667 MS |
474 | for (i = 0; i < ufs->numlower; i++) |
475 | mntput(ufs->lower_mnt[i]); | |
e9be9d5e | 476 | |
f45827e8 EZ |
477 | kfree(ufs->config.lowerdir); |
478 | kfree(ufs->config.upperdir); | |
479 | kfree(ufs->config.workdir); | |
e9be9d5e MS |
480 | kfree(ufs); |
481 | } | |
482 | ||
cc259639 AW |
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 | |
4ebc5818 | 489 | * filesystem pass the statfs to the upper filesystem (if it exists) |
cc259639 AW |
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 | ||
4ebc5818 | 498 | ovl_path_real(root_dentry, &path); |
cc259639 AW |
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 | ||
f45827e8 EZ |
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); | |
53a08cb9 MS |
521 | if (ufs->config.upperdir) { |
522 | seq_printf(m, ",upperdir=%s", ufs->config.upperdir); | |
523 | seq_printf(m, ",workdir=%s", ufs->config.workdir); | |
524 | } | |
f45827e8 EZ |
525 | return 0; |
526 | } | |
527 | ||
3cdf6fe9 SL |
528 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
529 | { | |
530 | struct ovl_fs *ufs = sb->s_fs_info; | |
531 | ||
71cbad7e | 532 | if (!(*flags & MS_RDONLY) && !ufs->upper_mnt) |
3cdf6fe9 SL |
533 | return -EROFS; |
534 | ||
535 | return 0; | |
536 | } | |
537 | ||
e9be9d5e MS |
538 | static const struct super_operations ovl_super_operations = { |
539 | .put_super = ovl_put_super, | |
cc259639 | 540 | .statfs = ovl_statfs, |
f45827e8 | 541 | .show_options = ovl_show_options, |
3cdf6fe9 | 542 | .remount_fs = ovl_remount, |
e9be9d5e MS |
543 | }; |
544 | ||
545 | enum { | |
546 | OPT_LOWERDIR, | |
547 | OPT_UPPERDIR, | |
548 | OPT_WORKDIR, | |
549 | OPT_ERR, | |
550 | }; | |
551 | ||
552 | static const match_table_t ovl_tokens = { | |
553 | {OPT_LOWERDIR, "lowerdir=%s"}, | |
554 | {OPT_UPPERDIR, "upperdir=%s"}, | |
555 | {OPT_WORKDIR, "workdir=%s"}, | |
556 | {OPT_ERR, NULL} | |
557 | }; | |
558 | ||
91c77947 MS |
559 | static char *ovl_next_opt(char **s) |
560 | { | |
561 | char *sbegin = *s; | |
562 | char *p; | |
563 | ||
564 | if (sbegin == NULL) | |
565 | return NULL; | |
566 | ||
567 | for (p = sbegin; *p; p++) { | |
568 | if (*p == '\\') { | |
569 | p++; | |
570 | if (!*p) | |
571 | break; | |
572 | } else if (*p == ',') { | |
573 | *p = '\0'; | |
574 | *s = p + 1; | |
575 | return sbegin; | |
576 | } | |
577 | } | |
578 | *s = NULL; | |
579 | return sbegin; | |
580 | } | |
581 | ||
e9be9d5e MS |
582 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
583 | { | |
584 | char *p; | |
585 | ||
91c77947 | 586 | while ((p = ovl_next_opt(&opt)) != NULL) { |
e9be9d5e MS |
587 | int token; |
588 | substring_t args[MAX_OPT_ARGS]; | |
589 | ||
590 | if (!*p) | |
591 | continue; | |
592 | ||
593 | token = match_token(p, ovl_tokens, args); | |
594 | switch (token) { | |
595 | case OPT_UPPERDIR: | |
596 | kfree(config->upperdir); | |
597 | config->upperdir = match_strdup(&args[0]); | |
598 | if (!config->upperdir) | |
599 | return -ENOMEM; | |
600 | break; | |
601 | ||
602 | case OPT_LOWERDIR: | |
603 | kfree(config->lowerdir); | |
604 | config->lowerdir = match_strdup(&args[0]); | |
605 | if (!config->lowerdir) | |
606 | return -ENOMEM; | |
607 | break; | |
608 | ||
609 | case OPT_WORKDIR: | |
610 | kfree(config->workdir); | |
611 | config->workdir = match_strdup(&args[0]); | |
612 | if (!config->workdir) | |
613 | return -ENOMEM; | |
614 | break; | |
615 | ||
616 | default: | |
bead55ef | 617 | pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); |
e9be9d5e MS |
618 | return -EINVAL; |
619 | } | |
620 | } | |
71cbad7e | 621 | |
622 | /* Workdir is useless in non-upper mount */ | |
623 | if (!config->upperdir && config->workdir) { | |
624 | pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", | |
625 | config->workdir); | |
626 | kfree(config->workdir); | |
627 | config->workdir = NULL; | |
628 | } | |
629 | ||
e9be9d5e MS |
630 | return 0; |
631 | } | |
632 | ||
633 | #define OVL_WORKDIR_NAME "work" | |
634 | ||
635 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, | |
636 | struct dentry *dentry) | |
637 | { | |
638 | struct inode *dir = dentry->d_inode; | |
639 | struct dentry *work; | |
640 | int err; | |
641 | bool retried = false; | |
642 | ||
643 | err = mnt_want_write(mnt); | |
644 | if (err) | |
645 | return ERR_PTR(err); | |
646 | ||
647 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); | |
648 | retry: | |
649 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, | |
650 | strlen(OVL_WORKDIR_NAME)); | |
651 | ||
652 | if (!IS_ERR(work)) { | |
653 | struct kstat stat = { | |
654 | .mode = S_IFDIR | 0, | |
655 | }; | |
656 | ||
657 | if (work->d_inode) { | |
658 | err = -EEXIST; | |
659 | if (retried) | |
660 | goto out_dput; | |
661 | ||
662 | retried = true; | |
663 | ovl_cleanup(dir, work); | |
664 | dput(work); | |
665 | goto retry; | |
666 | } | |
667 | ||
668 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); | |
669 | if (err) | |
670 | goto out_dput; | |
671 | } | |
672 | out_unlock: | |
673 | mutex_unlock(&dir->i_mutex); | |
674 | mnt_drop_write(mnt); | |
675 | ||
676 | return work; | |
677 | ||
678 | out_dput: | |
679 | dput(work); | |
680 | work = ERR_PTR(err); | |
681 | goto out_unlock; | |
682 | } | |
683 | ||
91c77947 MS |
684 | static void ovl_unescape(char *s) |
685 | { | |
686 | char *d = s; | |
687 | ||
688 | for (;; s++, d++) { | |
689 | if (*s == '\\') | |
690 | s++; | |
691 | *d = *s; | |
692 | if (!*s) | |
693 | break; | |
694 | } | |
695 | } | |
696 | ||
e9be9d5e MS |
697 | static bool ovl_is_allowed_fs_type(struct dentry *root) |
698 | { | |
699 | const struct dentry_operations *dop = root->d_op; | |
700 | ||
701 | /* | |
702 | * We don't support: | |
703 | * - automount filesystems | |
704 | * - filesystems with revalidate (FIXME for lower layer) | |
705 | * - filesystems with case insensitive names | |
706 | */ | |
707 | if (dop && | |
708 | (dop->d_manage || dop->d_automount || | |
709 | dop->d_revalidate || dop->d_weak_revalidate || | |
710 | dop->d_compare || dop->d_hash)) { | |
711 | return false; | |
712 | } | |
713 | return true; | |
714 | } | |
715 | ||
ab508822 MS |
716 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
717 | { | |
a78d9f0d | 718 | int err = -EINVAL; |
ab508822 | 719 | |
a78d9f0d MS |
720 | if (!*name) { |
721 | pr_err("overlayfs: empty lowerdir\n"); | |
722 | goto out; | |
723 | } | |
ab508822 MS |
724 | err = kern_path(name, LOOKUP_FOLLOW, path); |
725 | if (err) { | |
726 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); | |
727 | goto out; | |
728 | } | |
729 | err = -EINVAL; | |
730 | if (!ovl_is_allowed_fs_type(path->dentry)) { | |
731 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); | |
732 | goto out_put; | |
733 | } | |
734 | if (!S_ISDIR(path->dentry->d_inode->i_mode)) { | |
735 | pr_err("overlayfs: '%s' not a directory\n", name); | |
736 | goto out_put; | |
737 | } | |
738 | return 0; | |
739 | ||
740 | out_put: | |
741 | path_put(path); | |
742 | out: | |
743 | return err; | |
744 | } | |
745 | ||
746 | static int ovl_mount_dir(const char *name, struct path *path) | |
747 | { | |
748 | int err = -ENOMEM; | |
749 | char *tmp = kstrdup(name, GFP_KERNEL); | |
750 | ||
751 | if (tmp) { | |
752 | ovl_unescape(tmp); | |
753 | err = ovl_mount_dir_noesc(tmp, path); | |
754 | kfree(tmp); | |
755 | } | |
756 | return err; | |
757 | } | |
758 | ||
759 | static int ovl_lower_dir(const char *name, struct path *path, long *namelen, | |
760 | int *stack_depth) | |
761 | { | |
762 | int err; | |
763 | struct kstatfs statfs; | |
764 | ||
a78d9f0d | 765 | err = ovl_mount_dir_noesc(name, path); |
ab508822 MS |
766 | if (err) |
767 | goto out; | |
768 | ||
769 | err = vfs_statfs(path, &statfs); | |
770 | if (err) { | |
771 | pr_err("overlayfs: statfs failed on '%s'\n", name); | |
772 | goto out_put; | |
773 | } | |
774 | *namelen = max(*namelen, statfs.f_namelen); | |
775 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); | |
776 | ||
777 | return 0; | |
778 | ||
779 | out_put: | |
780 | path_put(path); | |
781 | out: | |
782 | return err; | |
783 | } | |
784 | ||
e9be9d5e MS |
785 | /* Workdir should not be subdir of upperdir and vice versa */ |
786 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) | |
787 | { | |
788 | bool ok = false; | |
789 | ||
790 | if (workdir != upperdir) { | |
791 | ok = (lock_rename(workdir, upperdir) == NULL); | |
792 | unlock_rename(workdir, upperdir); | |
793 | } | |
794 | return ok; | |
795 | } | |
796 | ||
a78d9f0d MS |
797 | static unsigned int ovl_split_lowerdirs(char *str) |
798 | { | |
799 | unsigned int ctr = 1; | |
800 | char *s, *d; | |
801 | ||
802 | for (s = d = str;; s++, d++) { | |
803 | if (*s == '\\') { | |
804 | s++; | |
805 | } else if (*s == ':') { | |
806 | *d = '\0'; | |
807 | ctr++; | |
808 | continue; | |
809 | } | |
810 | *d = *s; | |
811 | if (!*s) | |
812 | break; | |
813 | } | |
814 | return ctr; | |
815 | } | |
816 | ||
e9be9d5e MS |
817 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
818 | { | |
53a08cb9 MS |
819 | struct path upperpath = { NULL, NULL }; |
820 | struct path workpath = { NULL, NULL }; | |
e9be9d5e MS |
821 | struct dentry *root_dentry; |
822 | struct ovl_entry *oe; | |
823 | struct ovl_fs *ufs; | |
a78d9f0d MS |
824 | struct path *stack = NULL; |
825 | char *lowertmp; | |
826 | char *lower; | |
827 | unsigned int numlower; | |
828 | unsigned int stacklen = 0; | |
dd662667 | 829 | unsigned int i; |
e9be9d5e MS |
830 | int err; |
831 | ||
f45827e8 EZ |
832 | err = -ENOMEM; |
833 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); | |
834 | if (!ufs) | |
e9be9d5e MS |
835 | goto out; |
836 | ||
f45827e8 EZ |
837 | err = ovl_parse_opt((char *) data, &ufs->config); |
838 | if (err) | |
839 | goto out_free_config; | |
840 | ||
e9be9d5e | 841 | err = -EINVAL; |
53a08cb9 MS |
842 | if (!ufs->config.lowerdir) { |
843 | pr_err("overlayfs: missing 'lowerdir'\n"); | |
e9be9d5e MS |
844 | goto out_free_config; |
845 | } | |
846 | ||
53a08cb9 MS |
847 | sb->s_stack_depth = 0; |
848 | if (ufs->config.upperdir) { | |
53a08cb9 MS |
849 | if (!ufs->config.workdir) { |
850 | pr_err("overlayfs: missing 'workdir'\n"); | |
851 | goto out_free_config; | |
852 | } | |
e9be9d5e | 853 | |
53a08cb9 MS |
854 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
855 | if (err) | |
856 | goto out_free_config; | |
e9be9d5e | 857 | |
71cbad7e | 858 | /* Upper fs should not be r/o */ |
859 | if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) { | |
860 | pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); | |
861 | err = -EINVAL; | |
862 | goto out_put_upperpath; | |
863 | } | |
864 | ||
53a08cb9 MS |
865 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
866 | if (err) | |
867 | goto out_put_upperpath; | |
868 | ||
2f83fd8c | 869 | err = -EINVAL; |
53a08cb9 MS |
870 | if (upperpath.mnt != workpath.mnt) { |
871 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); | |
872 | goto out_put_workpath; | |
873 | } | |
874 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { | |
875 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); | |
876 | goto out_put_workpath; | |
877 | } | |
878 | sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; | |
cc259639 | 879 | } |
a78d9f0d MS |
880 | err = -ENOMEM; |
881 | lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); | |
882 | if (!lowertmp) | |
ab508822 | 883 | goto out_put_workpath; |
69c433ed | 884 | |
a78d9f0d MS |
885 | err = -EINVAL; |
886 | stacklen = ovl_split_lowerdirs(lowertmp); | |
6be4506e | 887 | if (stacklen > OVL_MAX_STACK) { |
888 | pr_err("overlayfs: too many lower directries, limit is %d\n", | |
889 | OVL_MAX_STACK); | |
a78d9f0d | 890 | goto out_free_lowertmp; |
6be4506e | 891 | } else if (!ufs->config.upperdir && stacklen == 1) { |
892 | pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); | |
893 | goto out_free_lowertmp; | |
894 | } | |
a78d9f0d MS |
895 | |
896 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); | |
897 | if (!stack) | |
898 | goto out_free_lowertmp; | |
899 | ||
900 | lower = lowertmp; | |
901 | for (numlower = 0; numlower < stacklen; numlower++) { | |
902 | err = ovl_lower_dir(lower, &stack[numlower], | |
903 | &ufs->lower_namelen, &sb->s_stack_depth); | |
904 | if (err) | |
905 | goto out_put_lowerpath; | |
906 | ||
907 | lower = strchr(lower, '\0') + 1; | |
908 | } | |
909 | ||
69c433ed | 910 | err = -EINVAL; |
ab508822 | 911 | sb->s_stack_depth++; |
69c433ed MS |
912 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
913 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); | |
3b7a9a24 | 914 | goto out_put_lowerpath; |
69c433ed MS |
915 | } |
916 | ||
53a08cb9 MS |
917 | if (ufs->config.upperdir) { |
918 | ufs->upper_mnt = clone_private_mount(&upperpath); | |
919 | err = PTR_ERR(ufs->upper_mnt); | |
920 | if (IS_ERR(ufs->upper_mnt)) { | |
921 | pr_err("overlayfs: failed to clone upperpath\n"); | |
922 | goto out_put_lowerpath; | |
923 | } | |
3b7a9a24 | 924 | |
53a08cb9 MS |
925 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); |
926 | err = PTR_ERR(ufs->workdir); | |
927 | if (IS_ERR(ufs->workdir)) { | |
928 | pr_err("overlayfs: failed to create directory %s/%s\n", | |
929 | ufs->config.workdir, OVL_WORKDIR_NAME); | |
930 | goto out_put_upper_mnt; | |
931 | } | |
e9be9d5e MS |
932 | } |
933 | ||
2f83fd8c | 934 | err = -ENOMEM; |
a78d9f0d | 935 | ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); |
dd662667 | 936 | if (ufs->lower_mnt == NULL) |
3b7a9a24 | 937 | goto out_put_workdir; |
a78d9f0d MS |
938 | for (i = 0; i < numlower; i++) { |
939 | struct vfsmount *mnt = clone_private_mount(&stack[i]); | |
dd662667 | 940 | |
2f83fd8c | 941 | err = PTR_ERR(mnt); |
a78d9f0d MS |
942 | if (IS_ERR(mnt)) { |
943 | pr_err("overlayfs: failed to clone lowerpath\n"); | |
944 | goto out_put_lower_mnt; | |
945 | } | |
946 | /* | |
947 | * Make lower_mnt R/O. That way fchmod/fchown on lower file | |
948 | * will fail instead of modifying lower fs. | |
949 | */ | |
950 | mnt->mnt_flags |= MNT_READONLY; | |
dd662667 | 951 | |
a78d9f0d MS |
952 | ufs->lower_mnt[ufs->numlower] = mnt; |
953 | ufs->numlower++; | |
954 | } | |
e9be9d5e | 955 | |
71cbad7e | 956 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
957 | if (!ufs->upper_mnt) | |
e9be9d5e MS |
958 | sb->s_flags |= MS_RDONLY; |
959 | ||
960 | sb->s_d_op = &ovl_dentry_operations; | |
961 | ||
962 | err = -ENOMEM; | |
a78d9f0d | 963 | oe = ovl_alloc_entry(numlower); |
3b7a9a24 MS |
964 | if (!oe) |
965 | goto out_put_lower_mnt; | |
e9be9d5e | 966 | |
3b7a9a24 | 967 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe)); |
e9be9d5e | 968 | if (!root_dentry) |
3b7a9a24 | 969 | goto out_free_oe; |
e9be9d5e MS |
970 | |
971 | mntput(upperpath.mnt); | |
a78d9f0d MS |
972 | for (i = 0; i < numlower; i++) |
973 | mntput(stack[i].mnt); | |
e9be9d5e | 974 | path_put(&workpath); |
a78d9f0d | 975 | kfree(lowertmp); |
e9be9d5e MS |
976 | |
977 | oe->__upperdentry = upperpath.dentry; | |
a78d9f0d MS |
978 | for (i = 0; i < numlower; i++) { |
979 | oe->lowerstack[i].dentry = stack[i].dentry; | |
980 | oe->lowerstack[i].mnt = ufs->lower_mnt[i]; | |
981 | } | |
e9be9d5e MS |
982 | |
983 | root_dentry->d_fsdata = oe; | |
984 | ||
cc259639 | 985 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
e9be9d5e MS |
986 | sb->s_op = &ovl_super_operations; |
987 | sb->s_root = root_dentry; | |
988 | sb->s_fs_info = ufs; | |
989 | ||
990 | return 0; | |
991 | ||
3b7a9a24 MS |
992 | out_free_oe: |
993 | kfree(oe); | |
e9be9d5e | 994 | out_put_lower_mnt: |
dd662667 MS |
995 | for (i = 0; i < ufs->numlower; i++) |
996 | mntput(ufs->lower_mnt[i]); | |
997 | kfree(ufs->lower_mnt); | |
3b7a9a24 MS |
998 | out_put_workdir: |
999 | dput(ufs->workdir); | |
e9be9d5e MS |
1000 | out_put_upper_mnt: |
1001 | mntput(ufs->upper_mnt); | |
e9be9d5e | 1002 | out_put_lowerpath: |
a78d9f0d MS |
1003 | for (i = 0; i < numlower; i++) |
1004 | path_put(&stack[i]); | |
1005 | kfree(stack); | |
1006 | out_free_lowertmp: | |
1007 | kfree(lowertmp); | |
3b7a9a24 MS |
1008 | out_put_workpath: |
1009 | path_put(&workpath); | |
e9be9d5e MS |
1010 | out_put_upperpath: |
1011 | path_put(&upperpath); | |
e9be9d5e | 1012 | out_free_config: |
f45827e8 EZ |
1013 | kfree(ufs->config.lowerdir); |
1014 | kfree(ufs->config.upperdir); | |
1015 | kfree(ufs->config.workdir); | |
1016 | kfree(ufs); | |
e9be9d5e MS |
1017 | out: |
1018 | return err; | |
1019 | } | |
1020 | ||
1021 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, | |
1022 | const char *dev_name, void *raw_data) | |
1023 | { | |
1024 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); | |
1025 | } | |
1026 | ||
1027 | static struct file_system_type ovl_fs_type = { | |
1028 | .owner = THIS_MODULE, | |
ef94b186 | 1029 | .name = "overlay", |
e9be9d5e MS |
1030 | .mount = ovl_mount, |
1031 | .kill_sb = kill_anon_super, | |
1032 | }; | |
ef94b186 | 1033 | MODULE_ALIAS_FS("overlay"); |
e9be9d5e MS |
1034 | |
1035 | static int __init ovl_init(void) | |
1036 | { | |
1037 | return register_filesystem(&ovl_fs_type); | |
1038 | } | |
1039 | ||
1040 | static void __exit ovl_exit(void) | |
1041 | { | |
1042 | unregister_filesystem(&ovl_fs_type); | |
1043 | } | |
1044 | ||
1045 | module_init(ovl_init); | |
1046 | module_exit(ovl_exit); |