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