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