]>
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> | |
e9be9d5e | 13 | #include <linux/mount.h> |
e9be9d5e MS |
14 | #include <linux/parser.h> |
15 | #include <linux/module.h> | |
cc259639 | 16 | #include <linux/statfs.h> |
f45827e8 | 17 | #include <linux/seq_file.h> |
d837a49b | 18 | #include <linux/posix_acl_xattr.h> |
e9be9d5e | 19 | #include "overlayfs.h" |
bbb1e54d | 20 | #include "ovl_entry.h" |
e9be9d5e MS |
21 | |
22 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); | |
23 | MODULE_DESCRIPTION("Overlay filesystem"); | |
24 | MODULE_LICENSE("GPL"); | |
25 | ||
e9be9d5e MS |
26 | |
27 | struct ovl_dir_cache; | |
28 | ||
a78d9f0d MS |
29 | #define OVL_MAX_STACK 500 |
30 | ||
e9be9d5e MS |
31 | |
32 | static void ovl_dentry_release(struct dentry *dentry) | |
33 | { | |
34 | struct ovl_entry *oe = dentry->d_fsdata; | |
35 | ||
36 | if (oe) { | |
dd662667 MS |
37 | unsigned int i; |
38 | ||
e9be9d5e | 39 | dput(oe->__upperdentry); |
dd662667 MS |
40 | for (i = 0; i < oe->numlower; i++) |
41 | dput(oe->lowerstack[i].dentry); | |
e9be9d5e MS |
42 | kfree_rcu(oe, rcu); |
43 | } | |
44 | } | |
45 | ||
2d902671 MS |
46 | static struct dentry *ovl_d_real(struct dentry *dentry, |
47 | const struct inode *inode, | |
48 | unsigned int open_flags) | |
d101a125 MS |
49 | { |
50 | struct dentry *real; | |
51 | ||
ca4c8a3a | 52 | if (!d_is_reg(dentry)) { |
d101a125 MS |
53 | if (!inode || inode == d_inode(dentry)) |
54 | return dentry; | |
55 | goto bug; | |
56 | } | |
57 | ||
2d902671 MS |
58 | if (d_is_negative(dentry)) |
59 | return dentry; | |
60 | ||
61 | if (open_flags) { | |
62 | int err = ovl_open_maybe_copy_up(dentry, open_flags); | |
63 | ||
64 | if (err) | |
65 | return ERR_PTR(err); | |
66 | } | |
67 | ||
d101a125 MS |
68 | real = ovl_dentry_upper(dentry); |
69 | if (real && (!inode || inode == d_inode(real))) | |
70 | return real; | |
71 | ||
72 | real = ovl_dentry_lower(dentry); | |
73 | if (!real) | |
74 | goto bug; | |
75 | ||
c4fcfc16 MS |
76 | /* Handle recursion */ |
77 | real = d_real(real, inode, open_flags); | |
78 | ||
d101a125 MS |
79 | if (!inode || inode == d_inode(real)) |
80 | return real; | |
d101a125 | 81 | bug: |
656189d2 | 82 | WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry, |
d101a125 MS |
83 | inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0); |
84 | return dentry; | |
85 | } | |
86 | ||
7c03b5d4 MS |
87 | static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
88 | { | |
89 | struct ovl_entry *oe = dentry->d_fsdata; | |
90 | unsigned int i; | |
91 | int ret = 1; | |
92 | ||
93 | for (i = 0; i < oe->numlower; i++) { | |
94 | struct dentry *d = oe->lowerstack[i].dentry; | |
95 | ||
96 | if (d->d_flags & DCACHE_OP_REVALIDATE) { | |
97 | ret = d->d_op->d_revalidate(d, flags); | |
98 | if (ret < 0) | |
99 | return ret; | |
100 | if (!ret) { | |
101 | if (!(flags & LOOKUP_RCU)) | |
102 | d_invalidate(d); | |
103 | return -ESTALE; | |
104 | } | |
105 | } | |
106 | } | |
107 | return 1; | |
108 | } | |
109 | ||
110 | static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) | |
111 | { | |
112 | struct ovl_entry *oe = dentry->d_fsdata; | |
113 | unsigned int i; | |
114 | int ret = 1; | |
115 | ||
116 | for (i = 0; i < oe->numlower; i++) { | |
117 | struct dentry *d = oe->lowerstack[i].dentry; | |
118 | ||
119 | if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { | |
120 | ret = d->d_op->d_weak_revalidate(d, flags); | |
121 | if (ret <= 0) | |
122 | break; | |
123 | } | |
124 | } | |
125 | return ret; | |
126 | } | |
127 | ||
e9be9d5e MS |
128 | static const struct dentry_operations ovl_dentry_operations = { |
129 | .d_release = ovl_dentry_release, | |
d101a125 | 130 | .d_real = ovl_d_real, |
e9be9d5e MS |
131 | }; |
132 | ||
7c03b5d4 MS |
133 | static const struct dentry_operations ovl_reval_dentry_operations = { |
134 | .d_release = ovl_dentry_release, | |
d101a125 | 135 | .d_real = ovl_d_real, |
7c03b5d4 MS |
136 | .d_revalidate = ovl_dentry_revalidate, |
137 | .d_weak_revalidate = ovl_dentry_weak_revalidate, | |
138 | }; | |
139 | ||
e9be9d5e MS |
140 | static void ovl_put_super(struct super_block *sb) |
141 | { | |
142 | struct ovl_fs *ufs = sb->s_fs_info; | |
dd662667 | 143 | unsigned i; |
e9be9d5e MS |
144 | |
145 | dput(ufs->workdir); | |
146 | mntput(ufs->upper_mnt); | |
dd662667 MS |
147 | for (i = 0; i < ufs->numlower; i++) |
148 | mntput(ufs->lower_mnt[i]); | |
5ffdbe8b | 149 | kfree(ufs->lower_mnt); |
e9be9d5e | 150 | |
f45827e8 EZ |
151 | kfree(ufs->config.lowerdir); |
152 | kfree(ufs->config.upperdir); | |
153 | kfree(ufs->config.workdir); | |
3fe6e52f | 154 | put_cred(ufs->creator_cred); |
e9be9d5e MS |
155 | kfree(ufs); |
156 | } | |
157 | ||
cc259639 AW |
158 | /** |
159 | * ovl_statfs | |
160 | * @sb: The overlayfs super block | |
161 | * @buf: The struct kstatfs to fill in with stats | |
162 | * | |
163 | * Get the filesystem statistics. As writes always target the upper layer | |
4ebc5818 | 164 | * filesystem pass the statfs to the upper filesystem (if it exists) |
cc259639 AW |
165 | */ |
166 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) | |
167 | { | |
168 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; | |
169 | struct dentry *root_dentry = dentry->d_sb->s_root; | |
170 | struct path path; | |
171 | int err; | |
172 | ||
4ebc5818 | 173 | ovl_path_real(root_dentry, &path); |
cc259639 AW |
174 | |
175 | err = vfs_statfs(&path, buf); | |
176 | if (!err) { | |
6b2d5fe4 | 177 | buf->f_namelen = ofs->namelen; |
cc259639 AW |
178 | buf->f_type = OVERLAYFS_SUPER_MAGIC; |
179 | } | |
180 | ||
181 | return err; | |
182 | } | |
183 | ||
f45827e8 EZ |
184 | /** |
185 | * ovl_show_options | |
186 | * | |
187 | * Prints the mount options for a given superblock. | |
188 | * Returns zero; does not fail. | |
189 | */ | |
190 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) | |
191 | { | |
192 | struct super_block *sb = dentry->d_sb; | |
193 | struct ovl_fs *ufs = sb->s_fs_info; | |
194 | ||
a068acf2 | 195 | seq_show_option(m, "lowerdir", ufs->config.lowerdir); |
53a08cb9 | 196 | if (ufs->config.upperdir) { |
a068acf2 KC |
197 | seq_show_option(m, "upperdir", ufs->config.upperdir); |
198 | seq_show_option(m, "workdir", ufs->config.workdir); | |
53a08cb9 | 199 | } |
8d3095f4 MS |
200 | if (ufs->config.default_permissions) |
201 | seq_puts(m, ",default_permissions"); | |
f45827e8 EZ |
202 | return 0; |
203 | } | |
204 | ||
3cdf6fe9 SL |
205 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
206 | { | |
207 | struct ovl_fs *ufs = sb->s_fs_info; | |
208 | ||
cc6f67bc | 209 | if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir)) |
3cdf6fe9 SL |
210 | return -EROFS; |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
e9be9d5e MS |
215 | static const struct super_operations ovl_super_operations = { |
216 | .put_super = ovl_put_super, | |
cc259639 | 217 | .statfs = ovl_statfs, |
f45827e8 | 218 | .show_options = ovl_show_options, |
3cdf6fe9 | 219 | .remount_fs = ovl_remount, |
eead4f2d | 220 | .drop_inode = generic_delete_inode, |
e9be9d5e MS |
221 | }; |
222 | ||
223 | enum { | |
224 | OPT_LOWERDIR, | |
225 | OPT_UPPERDIR, | |
226 | OPT_WORKDIR, | |
8d3095f4 | 227 | OPT_DEFAULT_PERMISSIONS, |
e9be9d5e MS |
228 | OPT_ERR, |
229 | }; | |
230 | ||
231 | static const match_table_t ovl_tokens = { | |
232 | {OPT_LOWERDIR, "lowerdir=%s"}, | |
233 | {OPT_UPPERDIR, "upperdir=%s"}, | |
234 | {OPT_WORKDIR, "workdir=%s"}, | |
8d3095f4 | 235 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
e9be9d5e MS |
236 | {OPT_ERR, NULL} |
237 | }; | |
238 | ||
91c77947 MS |
239 | static char *ovl_next_opt(char **s) |
240 | { | |
241 | char *sbegin = *s; | |
242 | char *p; | |
243 | ||
244 | if (sbegin == NULL) | |
245 | return NULL; | |
246 | ||
247 | for (p = sbegin; *p; p++) { | |
248 | if (*p == '\\') { | |
249 | p++; | |
250 | if (!*p) | |
251 | break; | |
252 | } else if (*p == ',') { | |
253 | *p = '\0'; | |
254 | *s = p + 1; | |
255 | return sbegin; | |
256 | } | |
257 | } | |
258 | *s = NULL; | |
259 | return sbegin; | |
260 | } | |
261 | ||
e9be9d5e MS |
262 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
263 | { | |
264 | char *p; | |
265 | ||
91c77947 | 266 | while ((p = ovl_next_opt(&opt)) != NULL) { |
e9be9d5e MS |
267 | int token; |
268 | substring_t args[MAX_OPT_ARGS]; | |
269 | ||
270 | if (!*p) | |
271 | continue; | |
272 | ||
273 | token = match_token(p, ovl_tokens, args); | |
274 | switch (token) { | |
275 | case OPT_UPPERDIR: | |
276 | kfree(config->upperdir); | |
277 | config->upperdir = match_strdup(&args[0]); | |
278 | if (!config->upperdir) | |
279 | return -ENOMEM; | |
280 | break; | |
281 | ||
282 | case OPT_LOWERDIR: | |
283 | kfree(config->lowerdir); | |
284 | config->lowerdir = match_strdup(&args[0]); | |
285 | if (!config->lowerdir) | |
286 | return -ENOMEM; | |
287 | break; | |
288 | ||
289 | case OPT_WORKDIR: | |
290 | kfree(config->workdir); | |
291 | config->workdir = match_strdup(&args[0]); | |
292 | if (!config->workdir) | |
293 | return -ENOMEM; | |
294 | break; | |
295 | ||
8d3095f4 MS |
296 | case OPT_DEFAULT_PERMISSIONS: |
297 | config->default_permissions = true; | |
298 | break; | |
299 | ||
e9be9d5e | 300 | default: |
bead55ef | 301 | pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); |
e9be9d5e MS |
302 | return -EINVAL; |
303 | } | |
304 | } | |
71cbad7e | 305 | |
306 | /* Workdir is useless in non-upper mount */ | |
307 | if (!config->upperdir && config->workdir) { | |
308 | pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", | |
309 | config->workdir); | |
310 | kfree(config->workdir); | |
311 | config->workdir = NULL; | |
312 | } | |
313 | ||
e9be9d5e MS |
314 | return 0; |
315 | } | |
316 | ||
317 | #define OVL_WORKDIR_NAME "work" | |
318 | ||
319 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, | |
320 | struct dentry *dentry) | |
321 | { | |
322 | struct inode *dir = dentry->d_inode; | |
323 | struct dentry *work; | |
324 | int err; | |
325 | bool retried = false; | |
326 | ||
327 | err = mnt_want_write(mnt); | |
328 | if (err) | |
329 | return ERR_PTR(err); | |
330 | ||
5955102c | 331 | inode_lock_nested(dir, I_MUTEX_PARENT); |
e9be9d5e MS |
332 | retry: |
333 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, | |
334 | strlen(OVL_WORKDIR_NAME)); | |
335 | ||
336 | if (!IS_ERR(work)) { | |
337 | struct kstat stat = { | |
338 | .mode = S_IFDIR | 0, | |
339 | }; | |
c11b9fdd MS |
340 | struct iattr attr = { |
341 | .ia_valid = ATTR_MODE, | |
342 | .ia_mode = stat.mode, | |
343 | }; | |
e9be9d5e MS |
344 | |
345 | if (work->d_inode) { | |
346 | err = -EEXIST; | |
347 | if (retried) | |
348 | goto out_dput; | |
349 | ||
350 | retried = true; | |
eea2fb48 | 351 | ovl_workdir_cleanup(dir, mnt, work, 0); |
e9be9d5e MS |
352 | dput(work); |
353 | goto retry; | |
354 | } | |
355 | ||
356 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); | |
357 | if (err) | |
358 | goto out_dput; | |
c11b9fdd | 359 | |
cb348edb MS |
360 | /* |
361 | * Try to remove POSIX ACL xattrs from workdir. We are good if: | |
362 | * | |
363 | * a) success (there was a POSIX ACL xattr and was removed) | |
364 | * b) -ENODATA (there was no POSIX ACL xattr) | |
365 | * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported) | |
366 | * | |
367 | * There are various other error values that could effectively | |
368 | * mean that the xattr doesn't exist (e.g. -ERANGE is returned | |
369 | * if the xattr name is too long), but the set of filesystems | |
370 | * allowed as upper are limited to "normal" ones, where checking | |
371 | * for the above two errors is sufficient. | |
372 | */ | |
c11b9fdd | 373 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT); |
e1ff3dd1 | 374 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
c11b9fdd MS |
375 | goto out_dput; |
376 | ||
377 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS); | |
e1ff3dd1 | 378 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
c11b9fdd MS |
379 | goto out_dput; |
380 | ||
381 | /* Clear any inherited mode bits */ | |
382 | inode_lock(work->d_inode); | |
383 | err = notify_change(work, &attr, NULL); | |
384 | inode_unlock(work->d_inode); | |
385 | if (err) | |
386 | goto out_dput; | |
e9be9d5e MS |
387 | } |
388 | out_unlock: | |
5955102c | 389 | inode_unlock(dir); |
e9be9d5e MS |
390 | mnt_drop_write(mnt); |
391 | ||
392 | return work; | |
393 | ||
394 | out_dput: | |
395 | dput(work); | |
396 | work = ERR_PTR(err); | |
397 | goto out_unlock; | |
398 | } | |
399 | ||
91c77947 MS |
400 | static void ovl_unescape(char *s) |
401 | { | |
402 | char *d = s; | |
403 | ||
404 | for (;; s++, d++) { | |
405 | if (*s == '\\') | |
406 | s++; | |
407 | *d = *s; | |
408 | if (!*s) | |
409 | break; | |
410 | } | |
411 | } | |
412 | ||
ab508822 MS |
413 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
414 | { | |
a78d9f0d | 415 | int err = -EINVAL; |
ab508822 | 416 | |
a78d9f0d MS |
417 | if (!*name) { |
418 | pr_err("overlayfs: empty lowerdir\n"); | |
419 | goto out; | |
420 | } | |
ab508822 MS |
421 | err = kern_path(name, LOOKUP_FOLLOW, path); |
422 | if (err) { | |
423 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); | |
424 | goto out; | |
425 | } | |
426 | err = -EINVAL; | |
7c03b5d4 | 427 | if (ovl_dentry_weird(path->dentry)) { |
ab508822 MS |
428 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); |
429 | goto out_put; | |
430 | } | |
2b8c30e9 | 431 | if (!d_is_dir(path->dentry)) { |
ab508822 MS |
432 | pr_err("overlayfs: '%s' not a directory\n", name); |
433 | goto out_put; | |
434 | } | |
435 | return 0; | |
436 | ||
437 | out_put: | |
438 | path_put(path); | |
439 | out: | |
440 | return err; | |
441 | } | |
442 | ||
443 | static int ovl_mount_dir(const char *name, struct path *path) | |
444 | { | |
445 | int err = -ENOMEM; | |
446 | char *tmp = kstrdup(name, GFP_KERNEL); | |
447 | ||
448 | if (tmp) { | |
449 | ovl_unescape(tmp); | |
450 | err = ovl_mount_dir_noesc(tmp, path); | |
7c03b5d4 MS |
451 | |
452 | if (!err) | |
453 | if (ovl_dentry_remote(path->dentry)) { | |
454 | pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", | |
455 | tmp); | |
456 | path_put(path); | |
457 | err = -EINVAL; | |
458 | } | |
ab508822 MS |
459 | kfree(tmp); |
460 | } | |
461 | return err; | |
462 | } | |
463 | ||
6b2d5fe4 MS |
464 | static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs, |
465 | const char *name) | |
ab508822 | 466 | { |
ab508822 | 467 | struct kstatfs statfs; |
6b2d5fe4 MS |
468 | int err = vfs_statfs(path, &statfs); |
469 | ||
470 | if (err) | |
471 | pr_err("overlayfs: statfs failed on '%s'\n", name); | |
472 | else | |
473 | ofs->namelen = max(ofs->namelen, statfs.f_namelen); | |
474 | ||
475 | return err; | |
476 | } | |
477 | ||
478 | static int ovl_lower_dir(const char *name, struct path *path, | |
479 | struct ovl_fs *ofs, int *stack_depth, bool *remote) | |
480 | { | |
481 | int err; | |
ab508822 | 482 | |
a78d9f0d | 483 | err = ovl_mount_dir_noesc(name, path); |
ab508822 MS |
484 | if (err) |
485 | goto out; | |
486 | ||
6b2d5fe4 MS |
487 | err = ovl_check_namelen(path, ofs, name); |
488 | if (err) | |
ab508822 | 489 | goto out_put; |
6b2d5fe4 | 490 | |
ab508822 MS |
491 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); |
492 | ||
7c03b5d4 MS |
493 | if (ovl_dentry_remote(path->dentry)) |
494 | *remote = true; | |
495 | ||
ab508822 MS |
496 | return 0; |
497 | ||
498 | out_put: | |
499 | path_put(path); | |
500 | out: | |
501 | return err; | |
502 | } | |
503 | ||
e9be9d5e MS |
504 | /* Workdir should not be subdir of upperdir and vice versa */ |
505 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) | |
506 | { | |
507 | bool ok = false; | |
508 | ||
509 | if (workdir != upperdir) { | |
510 | ok = (lock_rename(workdir, upperdir) == NULL); | |
511 | unlock_rename(workdir, upperdir); | |
512 | } | |
513 | return ok; | |
514 | } | |
515 | ||
a78d9f0d MS |
516 | static unsigned int ovl_split_lowerdirs(char *str) |
517 | { | |
518 | unsigned int ctr = 1; | |
519 | char *s, *d; | |
520 | ||
521 | for (s = d = str;; s++, d++) { | |
522 | if (*s == '\\') { | |
523 | s++; | |
524 | } else if (*s == ':') { | |
525 | *d = '\0'; | |
526 | ctr++; | |
527 | continue; | |
528 | } | |
529 | *d = *s; | |
530 | if (!*s) | |
531 | break; | |
532 | } | |
533 | return ctr; | |
534 | } | |
535 | ||
0eb45fc3 AG |
536 | static int __maybe_unused |
537 | ovl_posix_acl_xattr_get(const struct xattr_handler *handler, | |
538 | struct dentry *dentry, struct inode *inode, | |
539 | const char *name, void *buffer, size_t size) | |
540 | { | |
541 | return ovl_xattr_get(dentry, handler->name, buffer, size); | |
542 | } | |
543 | ||
0c97be22 AG |
544 | static int __maybe_unused |
545 | ovl_posix_acl_xattr_set(const struct xattr_handler *handler, | |
546 | struct dentry *dentry, struct inode *inode, | |
547 | const char *name, const void *value, | |
548 | size_t size, int flags) | |
d837a49b MS |
549 | { |
550 | struct dentry *workdir = ovl_workdir(dentry); | |
551 | struct inode *realinode = ovl_inode_real(inode, NULL); | |
552 | struct posix_acl *acl = NULL; | |
553 | int err; | |
554 | ||
555 | /* Check that everything is OK before copy-up */ | |
556 | if (value) { | |
557 | acl = posix_acl_from_xattr(&init_user_ns, value, size); | |
558 | if (IS_ERR(acl)) | |
559 | return PTR_ERR(acl); | |
560 | } | |
561 | err = -EOPNOTSUPP; | |
562 | if (!IS_POSIXACL(d_inode(workdir))) | |
563 | goto out_acl_release; | |
564 | if (!realinode->i_op->set_acl) | |
565 | goto out_acl_release; | |
566 | if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) { | |
567 | err = acl ? -EACCES : 0; | |
568 | goto out_acl_release; | |
569 | } | |
570 | err = -EPERM; | |
571 | if (!inode_owner_or_capable(inode)) | |
572 | goto out_acl_release; | |
573 | ||
574 | posix_acl_release(acl); | |
575 | ||
fd3220d3 MS |
576 | /* |
577 | * Check if sgid bit needs to be cleared (actual setacl operation will | |
578 | * be done with mounter's capabilities and so that won't do it for us). | |
579 | */ | |
580 | if (unlikely(inode->i_mode & S_ISGID) && | |
581 | handler->flags == ACL_TYPE_ACCESS && | |
582 | !in_group_p(inode->i_gid) && | |
583 | !capable_wrt_inode_uidgid(inode, CAP_FSETID)) { | |
584 | struct iattr iattr = { .ia_valid = ATTR_KILL_SGID }; | |
585 | ||
586 | err = ovl_setattr(dentry, &iattr); | |
587 | if (err) | |
588 | return err; | |
589 | } | |
590 | ||
ce31513a MS |
591 | err = ovl_xattr_set(dentry, handler->name, value, size, flags); |
592 | if (!err) | |
593 | ovl_copyattr(ovl_inode_real(inode, NULL), inode); | |
594 | ||
595 | return err; | |
d837a49b MS |
596 | |
597 | out_acl_release: | |
598 | posix_acl_release(acl); | |
599 | return err; | |
600 | } | |
601 | ||
0eb45fc3 AG |
602 | static int ovl_own_xattr_get(const struct xattr_handler *handler, |
603 | struct dentry *dentry, struct inode *inode, | |
604 | const char *name, void *buffer, size_t size) | |
605 | { | |
606 | return -EPERM; | |
607 | } | |
608 | ||
d837a49b MS |
609 | static int ovl_own_xattr_set(const struct xattr_handler *handler, |
610 | struct dentry *dentry, struct inode *inode, | |
611 | const char *name, const void *value, | |
612 | size_t size, int flags) | |
613 | { | |
614 | return -EPERM; | |
615 | } | |
616 | ||
0eb45fc3 AG |
617 | static int ovl_other_xattr_get(const struct xattr_handler *handler, |
618 | struct dentry *dentry, struct inode *inode, | |
619 | const char *name, void *buffer, size_t size) | |
620 | { | |
621 | return ovl_xattr_get(dentry, name, buffer, size); | |
622 | } | |
623 | ||
0e585ccc AG |
624 | static int ovl_other_xattr_set(const struct xattr_handler *handler, |
625 | struct dentry *dentry, struct inode *inode, | |
626 | const char *name, const void *value, | |
627 | size_t size, int flags) | |
628 | { | |
629 | return ovl_xattr_set(dentry, name, value, size, flags); | |
630 | } | |
631 | ||
0c97be22 AG |
632 | static const struct xattr_handler __maybe_unused |
633 | ovl_posix_acl_access_xattr_handler = { | |
d837a49b MS |
634 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
635 | .flags = ACL_TYPE_ACCESS, | |
0eb45fc3 | 636 | .get = ovl_posix_acl_xattr_get, |
d837a49b MS |
637 | .set = ovl_posix_acl_xattr_set, |
638 | }; | |
639 | ||
0c97be22 AG |
640 | static const struct xattr_handler __maybe_unused |
641 | ovl_posix_acl_default_xattr_handler = { | |
d837a49b MS |
642 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
643 | .flags = ACL_TYPE_DEFAULT, | |
0eb45fc3 | 644 | .get = ovl_posix_acl_xattr_get, |
d837a49b MS |
645 | .set = ovl_posix_acl_xattr_set, |
646 | }; | |
647 | ||
648 | static const struct xattr_handler ovl_own_xattr_handler = { | |
649 | .prefix = OVL_XATTR_PREFIX, | |
0eb45fc3 | 650 | .get = ovl_own_xattr_get, |
d837a49b MS |
651 | .set = ovl_own_xattr_set, |
652 | }; | |
653 | ||
654 | static const struct xattr_handler ovl_other_xattr_handler = { | |
655 | .prefix = "", /* catch all */ | |
0eb45fc3 | 656 | .get = ovl_other_xattr_get, |
d837a49b MS |
657 | .set = ovl_other_xattr_set, |
658 | }; | |
659 | ||
660 | static const struct xattr_handler *ovl_xattr_handlers[] = { | |
0c97be22 | 661 | #ifdef CONFIG_FS_POSIX_ACL |
d837a49b MS |
662 | &ovl_posix_acl_access_xattr_handler, |
663 | &ovl_posix_acl_default_xattr_handler, | |
0c97be22 | 664 | #endif |
d837a49b MS |
665 | &ovl_own_xattr_handler, |
666 | &ovl_other_xattr_handler, | |
667 | NULL | |
668 | }; | |
669 | ||
e9be9d5e MS |
670 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
671 | { | |
53a08cb9 MS |
672 | struct path upperpath = { NULL, NULL }; |
673 | struct path workpath = { NULL, NULL }; | |
e9be9d5e | 674 | struct dentry *root_dentry; |
39b681f8 | 675 | struct inode *realinode; |
e9be9d5e MS |
676 | struct ovl_entry *oe; |
677 | struct ovl_fs *ufs; | |
a78d9f0d MS |
678 | struct path *stack = NULL; |
679 | char *lowertmp; | |
680 | char *lower; | |
681 | unsigned int numlower; | |
682 | unsigned int stacklen = 0; | |
dd662667 | 683 | unsigned int i; |
7c03b5d4 | 684 | bool remote = false; |
e9be9d5e MS |
685 | int err; |
686 | ||
f45827e8 EZ |
687 | err = -ENOMEM; |
688 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); | |
689 | if (!ufs) | |
e9be9d5e MS |
690 | goto out; |
691 | ||
f45827e8 EZ |
692 | err = ovl_parse_opt((char *) data, &ufs->config); |
693 | if (err) | |
694 | goto out_free_config; | |
695 | ||
e9be9d5e | 696 | err = -EINVAL; |
53a08cb9 | 697 | if (!ufs->config.lowerdir) { |
07f2af7b KK |
698 | if (!silent) |
699 | pr_err("overlayfs: missing 'lowerdir'\n"); | |
e9be9d5e MS |
700 | goto out_free_config; |
701 | } | |
702 | ||
53a08cb9 | 703 | sb->s_stack_depth = 0; |
cf9a6784 | 704 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
53a08cb9 | 705 | if (ufs->config.upperdir) { |
53a08cb9 MS |
706 | if (!ufs->config.workdir) { |
707 | pr_err("overlayfs: missing 'workdir'\n"); | |
708 | goto out_free_config; | |
709 | } | |
e9be9d5e | 710 | |
53a08cb9 MS |
711 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
712 | if (err) | |
713 | goto out_free_config; | |
e9be9d5e | 714 | |
71cbad7e | 715 | /* Upper fs should not be r/o */ |
716 | if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) { | |
717 | pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); | |
718 | err = -EINVAL; | |
719 | goto out_put_upperpath; | |
720 | } | |
721 | ||
6b2d5fe4 MS |
722 | err = ovl_check_namelen(&upperpath, ufs, ufs->config.upperdir); |
723 | if (err) | |
724 | goto out_put_upperpath; | |
725 | ||
53a08cb9 MS |
726 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
727 | if (err) | |
728 | goto out_put_upperpath; | |
729 | ||
2f83fd8c | 730 | err = -EINVAL; |
53a08cb9 MS |
731 | if (upperpath.mnt != workpath.mnt) { |
732 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); | |
733 | goto out_put_workpath; | |
734 | } | |
735 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { | |
736 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); | |
737 | goto out_put_workpath; | |
738 | } | |
739 | sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; | |
cc259639 | 740 | } |
a78d9f0d MS |
741 | err = -ENOMEM; |
742 | lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); | |
743 | if (!lowertmp) | |
ab508822 | 744 | goto out_put_workpath; |
69c433ed | 745 | |
a78d9f0d MS |
746 | err = -EINVAL; |
747 | stacklen = ovl_split_lowerdirs(lowertmp); | |
6be4506e | 748 | if (stacklen > OVL_MAX_STACK) { |
fd36570a | 749 | pr_err("overlayfs: too many lower directories, limit is %d\n", |
6be4506e | 750 | OVL_MAX_STACK); |
a78d9f0d | 751 | goto out_free_lowertmp; |
6be4506e | 752 | } else if (!ufs->config.upperdir && stacklen == 1) { |
753 | pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); | |
754 | goto out_free_lowertmp; | |
755 | } | |
a78d9f0d MS |
756 | |
757 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); | |
758 | if (!stack) | |
759 | goto out_free_lowertmp; | |
760 | ||
761 | lower = lowertmp; | |
762 | for (numlower = 0; numlower < stacklen; numlower++) { | |
6b2d5fe4 MS |
763 | err = ovl_lower_dir(lower, &stack[numlower], ufs, |
764 | &sb->s_stack_depth, &remote); | |
a78d9f0d MS |
765 | if (err) |
766 | goto out_put_lowerpath; | |
767 | ||
768 | lower = strchr(lower, '\0') + 1; | |
769 | } | |
770 | ||
69c433ed | 771 | err = -EINVAL; |
ab508822 | 772 | sb->s_stack_depth++; |
69c433ed MS |
773 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
774 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); | |
3b7a9a24 | 775 | goto out_put_lowerpath; |
69c433ed MS |
776 | } |
777 | ||
53a08cb9 MS |
778 | if (ufs->config.upperdir) { |
779 | ufs->upper_mnt = clone_private_mount(&upperpath); | |
780 | err = PTR_ERR(ufs->upper_mnt); | |
781 | if (IS_ERR(ufs->upper_mnt)) { | |
782 | pr_err("overlayfs: failed to clone upperpath\n"); | |
783 | goto out_put_lowerpath; | |
784 | } | |
d719e8f2 MS |
785 | /* Don't inherit atime flags */ |
786 | ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); | |
787 | ||
788 | sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran; | |
3b7a9a24 | 789 | |
53a08cb9 MS |
790 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); |
791 | err = PTR_ERR(ufs->workdir); | |
792 | if (IS_ERR(ufs->workdir)) { | |
cc6f67bc MS |
793 | pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", |
794 | ufs->config.workdir, OVL_WORKDIR_NAME, -err); | |
795 | sb->s_flags |= MS_RDONLY; | |
796 | ufs->workdir = NULL; | |
53a08cb9 | 797 | } |
45aebeaf VG |
798 | |
799 | /* | |
800 | * Upper should support d_type, else whiteouts are visible. | |
801 | * Given workdir and upper are on same fs, we can do | |
21765194 VG |
802 | * iterate_dir() on workdir. This check requires successful |
803 | * creation of workdir in previous step. | |
45aebeaf | 804 | */ |
21765194 VG |
805 | if (ufs->workdir) { |
806 | err = ovl_check_d_type_supported(&workpath); | |
807 | if (err < 0) | |
808 | goto out_put_workdir; | |
45aebeaf | 809 | |
e7c0b599 VG |
810 | /* |
811 | * We allowed this configuration and don't want to | |
812 | * break users over kernel upgrade. So warn instead | |
813 | * of erroring out. | |
814 | */ | |
815 | if (!err) | |
816 | pr_warn("overlayfs: upper fs needs to support d_type.\n"); | |
45aebeaf | 817 | } |
e9be9d5e MS |
818 | } |
819 | ||
2f83fd8c | 820 | err = -ENOMEM; |
a78d9f0d | 821 | ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); |
dd662667 | 822 | if (ufs->lower_mnt == NULL) |
3b7a9a24 | 823 | goto out_put_workdir; |
a78d9f0d MS |
824 | for (i = 0; i < numlower; i++) { |
825 | struct vfsmount *mnt = clone_private_mount(&stack[i]); | |
dd662667 | 826 | |
2f83fd8c | 827 | err = PTR_ERR(mnt); |
a78d9f0d MS |
828 | if (IS_ERR(mnt)) { |
829 | pr_err("overlayfs: failed to clone lowerpath\n"); | |
830 | goto out_put_lower_mnt; | |
831 | } | |
832 | /* | |
833 | * Make lower_mnt R/O. That way fchmod/fchown on lower file | |
834 | * will fail instead of modifying lower fs. | |
835 | */ | |
d719e8f2 | 836 | mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; |
dd662667 | 837 | |
a78d9f0d MS |
838 | ufs->lower_mnt[ufs->numlower] = mnt; |
839 | ufs->numlower++; | |
840 | } | |
e9be9d5e | 841 | |
71cbad7e | 842 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
843 | if (!ufs->upper_mnt) | |
e9be9d5e MS |
844 | sb->s_flags |= MS_RDONLY; |
845 | ||
7c03b5d4 MS |
846 | if (remote) |
847 | sb->s_d_op = &ovl_reval_dentry_operations; | |
848 | else | |
849 | sb->s_d_op = &ovl_dentry_operations; | |
e9be9d5e | 850 | |
3fe6e52f AM |
851 | ufs->creator_cred = prepare_creds(); |
852 | if (!ufs->creator_cred) | |
853 | goto out_put_lower_mnt; | |
854 | ||
e9be9d5e | 855 | err = -ENOMEM; |
a78d9f0d | 856 | oe = ovl_alloc_entry(numlower); |
3b7a9a24 | 857 | if (!oe) |
3fe6e52f | 858 | goto out_put_cred; |
e9be9d5e | 859 | |
655042cc VG |
860 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
861 | sb->s_op = &ovl_super_operations; | |
862 | sb->s_xattr = ovl_xattr_handlers; | |
863 | sb->s_fs_info = ufs; | |
864 | sb->s_flags |= MS_POSIXACL | MS_NOREMOTELOCK; | |
865 | ||
ca4c8a3a | 866 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); |
e9be9d5e | 867 | if (!root_dentry) |
3b7a9a24 | 868 | goto out_free_oe; |
e9be9d5e MS |
869 | |
870 | mntput(upperpath.mnt); | |
a78d9f0d MS |
871 | for (i = 0; i < numlower; i++) |
872 | mntput(stack[i].mnt); | |
e9be9d5e | 873 | path_put(&workpath); |
a78d9f0d | 874 | kfree(lowertmp); |
e9be9d5e MS |
875 | |
876 | oe->__upperdentry = upperpath.dentry; | |
a78d9f0d MS |
877 | for (i = 0; i < numlower; i++) { |
878 | oe->lowerstack[i].dentry = stack[i].dentry; | |
879 | oe->lowerstack[i].mnt = ufs->lower_mnt[i]; | |
880 | } | |
0f95502a | 881 | kfree(stack); |
e9be9d5e MS |
882 | |
883 | root_dentry->d_fsdata = oe; | |
884 | ||
39b681f8 MS |
885 | realinode = d_inode(ovl_dentry_real(root_dentry)); |
886 | ovl_inode_init(d_inode(root_dentry), realinode, !!upperpath.dentry); | |
887 | ovl_copyattr(realinode, d_inode(root_dentry)); | |
ed06e069 | 888 | |
e9be9d5e | 889 | sb->s_root = root_dentry; |
e9be9d5e MS |
890 | |
891 | return 0; | |
892 | ||
3b7a9a24 MS |
893 | out_free_oe: |
894 | kfree(oe); | |
3fe6e52f AM |
895 | out_put_cred: |
896 | put_cred(ufs->creator_cred); | |
e9be9d5e | 897 | out_put_lower_mnt: |
dd662667 MS |
898 | for (i = 0; i < ufs->numlower; i++) |
899 | mntput(ufs->lower_mnt[i]); | |
900 | kfree(ufs->lower_mnt); | |
3b7a9a24 MS |
901 | out_put_workdir: |
902 | dput(ufs->workdir); | |
e9be9d5e | 903 | mntput(ufs->upper_mnt); |
e9be9d5e | 904 | out_put_lowerpath: |
a78d9f0d MS |
905 | for (i = 0; i < numlower; i++) |
906 | path_put(&stack[i]); | |
907 | kfree(stack); | |
908 | out_free_lowertmp: | |
909 | kfree(lowertmp); | |
3b7a9a24 MS |
910 | out_put_workpath: |
911 | path_put(&workpath); | |
e9be9d5e MS |
912 | out_put_upperpath: |
913 | path_put(&upperpath); | |
e9be9d5e | 914 | out_free_config: |
f45827e8 EZ |
915 | kfree(ufs->config.lowerdir); |
916 | kfree(ufs->config.upperdir); | |
917 | kfree(ufs->config.workdir); | |
918 | kfree(ufs); | |
e9be9d5e MS |
919 | out: |
920 | return err; | |
921 | } | |
922 | ||
923 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, | |
924 | const char *dev_name, void *raw_data) | |
925 | { | |
926 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); | |
927 | } | |
928 | ||
929 | static struct file_system_type ovl_fs_type = { | |
930 | .owner = THIS_MODULE, | |
ef94b186 | 931 | .name = "overlay", |
e9be9d5e MS |
932 | .mount = ovl_mount, |
933 | .kill_sb = kill_anon_super, | |
934 | }; | |
ef94b186 | 935 | MODULE_ALIAS_FS("overlay"); |
e9be9d5e MS |
936 | |
937 | static int __init ovl_init(void) | |
938 | { | |
939 | return register_filesystem(&ovl_fs_type); | |
940 | } | |
941 | ||
942 | static void __exit ovl_exit(void) | |
943 | { | |
944 | unregister_filesystem(&ovl_fs_type); | |
945 | } | |
946 | ||
947 | module_init(ovl_init); | |
948 | module_exit(ovl_exit); |