Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * Copyright (C) 2011 Novell Inc. |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 7 | #include <uapi/linux/magic.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 8 | #include <linux/fs.h> |
| 9 | #include <linux/namei.h> |
| 10 | #include <linux/xattr.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 11 | #include <linux/mount.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 12 | #include <linux/parser.h> |
| 13 | #include <linux/module.h> |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 14 | #include <linux/statfs.h> |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 15 | #include <linux/seq_file.h> |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 16 | #include <linux/posix_acl_xattr.h> |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 17 | #include <linux/exportfs.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 18 | #include "overlayfs.h" |
| 19 | |
| 20 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); |
| 21 | MODULE_DESCRIPTION("Overlay filesystem"); |
| 22 | MODULE_LICENSE("GPL"); |
| 23 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 24 | |
| 25 | struct ovl_dir_cache; |
| 26 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 27 | #define OVL_MAX_STACK 500 |
| 28 | |
Miklos Szeredi | 688ea0e | 2016-12-16 11:02:57 +0100 | [diff] [blame] | 29 | static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR); |
| 30 | module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644); |
Nicolas Schier | 253e748 | 2019-06-17 09:39:00 +0200 | [diff] [blame] | 31 | MODULE_PARM_DESC(redirect_dir, |
Miklos Szeredi | 688ea0e | 2016-12-16 11:02:57 +0100 | [diff] [blame] | 32 | "Default to on or off for the redirect_dir feature"); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 33 | |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 34 | static bool ovl_redirect_always_follow = |
| 35 | IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW); |
| 36 | module_param_named(redirect_always_follow, ovl_redirect_always_follow, |
| 37 | bool, 0644); |
Nicolas Schier | 253e748 | 2019-06-17 09:39:00 +0200 | [diff] [blame] | 38 | MODULE_PARM_DESC(redirect_always_follow, |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 39 | "Follow redirects even if redirect_dir feature is turned off"); |
| 40 | |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 41 | static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX); |
| 42 | module_param_named(index, ovl_index_def, bool, 0644); |
Nicolas Schier | 253e748 | 2019-06-17 09:39:00 +0200 | [diff] [blame] | 43 | MODULE_PARM_DESC(index, |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 44 | "Default to on or off for the inodes index feature"); |
| 45 | |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 46 | static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT); |
| 47 | module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644); |
Nicolas Schier | 253e748 | 2019-06-17 09:39:00 +0200 | [diff] [blame] | 48 | MODULE_PARM_DESC(nfs_export, |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 49 | "Default to on or off for the NFS export feature"); |
| 50 | |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 51 | static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO); |
| 52 | module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644); |
Nicolas Schier | 253e748 | 2019-06-17 09:39:00 +0200 | [diff] [blame] | 53 | MODULE_PARM_DESC(xino_auto, |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 54 | "Auto enable xino feature"); |
| 55 | |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 56 | static void ovl_entry_stack_free(struct ovl_entry *oe) |
| 57 | { |
| 58 | unsigned int i; |
| 59 | |
| 60 | for (i = 0; i < oe->numlower; i++) |
| 61 | dput(oe->lowerstack[i].dentry); |
| 62 | } |
| 63 | |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 64 | static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY); |
| 65 | module_param_named(metacopy, ovl_metacopy_def, bool, 0644); |
Nicolas Schier | 253e748 | 2019-06-17 09:39:00 +0200 | [diff] [blame] | 66 | MODULE_PARM_DESC(metacopy, |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 67 | "Default to on or off for the metadata only copy up feature"); |
| 68 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 69 | static void ovl_dentry_release(struct dentry *dentry) |
| 70 | { |
| 71 | struct ovl_entry *oe = dentry->d_fsdata; |
| 72 | |
| 73 | if (oe) { |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 74 | ovl_entry_stack_free(oe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 75 | kfree_rcu(oe, rcu); |
| 76 | } |
| 77 | } |
| 78 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 79 | static struct dentry *ovl_d_real(struct dentry *dentry, |
Miklos Szeredi | fb16043 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 80 | const struct inode *inode) |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 81 | { |
| 82 | struct dentry *real; |
| 83 | |
Miklos Szeredi | e8c985b | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 84 | /* It's an overlay file */ |
| 85 | if (inode && d_inode(dentry) == inode) |
| 86 | return dentry; |
| 87 | |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 88 | if (!d_is_reg(dentry)) { |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 89 | if (!inode || inode == d_inode(dentry)) |
| 90 | return dentry; |
| 91 | goto bug; |
| 92 | } |
| 93 | |
| 94 | real = ovl_dentry_upper(dentry); |
Vivek Goyal | 2c3d735 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 95 | if (real && (inode == d_inode(real))) |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 96 | return real; |
| 97 | |
Vivek Goyal | 2c3d735 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 98 | if (real && !inode && ovl_has_upperdata(d_inode(dentry))) |
| 99 | return real; |
| 100 | |
| 101 | real = ovl_dentry_lowerdata(dentry); |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 102 | if (!real) |
| 103 | goto bug; |
| 104 | |
Miklos Szeredi | c4fcfc1 | 2016-11-29 10:20:24 +0100 | [diff] [blame] | 105 | /* Handle recursion */ |
Miklos Szeredi | fb16043 | 2018-07-18 15:44:44 +0200 | [diff] [blame] | 106 | real = d_real(real, inode); |
Miklos Szeredi | c4fcfc1 | 2016-11-29 10:20:24 +0100 | [diff] [blame] | 107 | |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 108 | if (!inode || inode == d_inode(real)) |
| 109 | return real; |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 110 | bug: |
Miklos Szeredi | 656189d | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 111 | WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry, |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 112 | inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0); |
| 113 | return dentry; |
| 114 | } |
| 115 | |
Miklos Szeredi | 3bb7df9 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 116 | static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak) |
| 117 | { |
| 118 | int ret = 1; |
| 119 | |
| 120 | if (weak) { |
| 121 | if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) |
| 122 | ret = d->d_op->d_weak_revalidate(d, flags); |
| 123 | } else if (d->d_flags & DCACHE_OP_REVALIDATE) { |
| 124 | ret = d->d_op->d_revalidate(d, flags); |
| 125 | if (!ret) { |
| 126 | if (!(flags & LOOKUP_RCU)) |
| 127 | d_invalidate(d); |
| 128 | ret = -ESTALE; |
| 129 | } |
| 130 | } |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | static int ovl_dentry_revalidate_common(struct dentry *dentry, |
| 135 | unsigned int flags, bool weak) |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 136 | { |
| 137 | struct ovl_entry *oe = dentry->d_fsdata; |
Miklos Szeredi | bccece1 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 138 | struct dentry *upper; |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 139 | unsigned int i; |
| 140 | int ret = 1; |
| 141 | |
Miklos Szeredi | bccece1 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 142 | upper = ovl_dentry_upper(dentry); |
| 143 | if (upper) |
| 144 | ret = ovl_revalidate_real(upper, flags, weak); |
| 145 | |
Miklos Szeredi | 3bb7df9 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 146 | for (i = 0; ret > 0 && i < oe->numlower; i++) { |
| 147 | ret = ovl_revalidate_real(oe->lowerstack[i].dentry, flags, |
| 148 | weak); |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 149 | } |
Miklos Szeredi | 3bb7df9 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
| 154 | { |
| 155 | return ovl_dentry_revalidate_common(dentry, flags, false); |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) |
| 159 | { |
Miklos Szeredi | 3bb7df9 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 160 | return ovl_dentry_revalidate_common(dentry, flags, true); |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 163 | static const struct dentry_operations ovl_dentry_operations = { |
| 164 | .d_release = ovl_dentry_release, |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 165 | .d_real = ovl_d_real, |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 166 | .d_revalidate = ovl_dentry_revalidate, |
| 167 | .d_weak_revalidate = ovl_dentry_weak_revalidate, |
| 168 | }; |
| 169 | |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 170 | static struct kmem_cache *ovl_inode_cachep; |
| 171 | |
| 172 | static struct inode *ovl_alloc_inode(struct super_block *sb) |
| 173 | { |
| 174 | struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL); |
| 175 | |
Hirofumi Nakagawa | b3885bd | 2017-09-26 03:09:53 +0900 | [diff] [blame] | 176 | if (!oi) |
| 177 | return NULL; |
| 178 | |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 179 | oi->cache = NULL; |
Miklos Szeredi | cf31c46 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 180 | oi->redirect = NULL; |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 181 | oi->version = 0; |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 182 | oi->flags = 0; |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 183 | oi->__upperdentry = NULL; |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 184 | oi->lower = NULL; |
Vivek Goyal | 2664bd0 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 185 | oi->lowerdata = NULL; |
Amir Goldstein | a015daf | 2017-06-21 15:28:51 +0300 | [diff] [blame] | 186 | mutex_init(&oi->lock); |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 187 | |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 188 | return &oi->vfs_inode; |
| 189 | } |
| 190 | |
Al Viro | 0b269de | 2019-04-15 22:52:17 -0400 | [diff] [blame] | 191 | static void ovl_free_inode(struct inode *inode) |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 192 | { |
Al Viro | 0b269de | 2019-04-15 22:52:17 -0400 | [diff] [blame] | 193 | struct ovl_inode *oi = OVL_I(inode); |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 194 | |
Al Viro | 0b269de | 2019-04-15 22:52:17 -0400 | [diff] [blame] | 195 | kfree(oi->redirect); |
| 196 | mutex_destroy(&oi->lock); |
| 197 | kmem_cache_free(ovl_inode_cachep, oi); |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | static void ovl_destroy_inode(struct inode *inode) |
| 201 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 202 | struct ovl_inode *oi = OVL_I(inode); |
| 203 | |
| 204 | dput(oi->__upperdentry); |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 205 | iput(oi->lower); |
Vivek Goyal | 2664bd0 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 206 | if (S_ISDIR(inode->i_mode)) |
| 207 | ovl_dir_cache_free(inode); |
| 208 | else |
| 209 | iput(oi->lowerdata); |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 210 | } |
| 211 | |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 212 | static void ovl_free_fs(struct ovl_fs *ofs) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 213 | { |
Miklos Szeredi | df820f8 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 214 | struct vfsmount **mounts; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 215 | unsigned i; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 216 | |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 217 | iput(ofs->workbasedir_trap); |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 218 | iput(ofs->indexdir_trap); |
| 219 | iput(ofs->workdir_trap); |
Chengguang Xu | c21c839 | 2020-04-24 10:55:17 +0800 | [diff] [blame] | 220 | dput(ofs->whiteout); |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 221 | dput(ofs->indexdir); |
| 222 | dput(ofs->workdir); |
| 223 | if (ofs->workdir_locked) |
| 224 | ovl_inuse_unlock(ofs->workbasedir); |
| 225 | dput(ofs->workbasedir); |
| 226 | if (ofs->upperdir_locked) |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 227 | ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root); |
Miklos Szeredi | df820f8 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 228 | |
| 229 | /* Hack! Reuse ofs->layers as a vfsmount array before freeing it */ |
| 230 | mounts = (struct vfsmount **) ofs->layers; |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 231 | for (i = 0; i < ofs->numlayer; i++) { |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 232 | iput(ofs->layers[i].trap); |
Miklos Szeredi | df820f8 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 233 | mounts[i] = ofs->layers[i].mnt; |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 234 | } |
Miklos Szeredi | df820f8 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 235 | kern_unmount_array(mounts, ofs->numlayer); |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 236 | kfree(ofs->layers); |
Amir Goldstein | b7bf990 | 2020-01-14 22:17:25 +0200 | [diff] [blame] | 237 | for (i = 0; i < ofs->numfs; i++) |
Amir Goldstein | 07f1e59 | 2020-01-14 21:59:22 +0200 | [diff] [blame] | 238 | free_anon_bdev(ofs->fs[i].pseudo_dev); |
| 239 | kfree(ofs->fs); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 240 | |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 241 | kfree(ofs->config.lowerdir); |
| 242 | kfree(ofs->config.upperdir); |
| 243 | kfree(ofs->config.workdir); |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 244 | kfree(ofs->config.redirect_mode); |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 245 | if (ofs->creator_cred) |
| 246 | put_cred(ofs->creator_cred); |
| 247 | kfree(ofs); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Miklos Szeredi | a9075cd | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 250 | static void ovl_put_super(struct super_block *sb) |
| 251 | { |
| 252 | struct ovl_fs *ofs = sb->s_fs_info; |
| 253 | |
| 254 | ovl_free_fs(ofs); |
| 255 | } |
| 256 | |
Chengguang Xu | e8d4bfe | 2017-11-29 10:01:32 +0800 | [diff] [blame] | 257 | /* Sync real dirty inodes in upper filesystem (if it exists) */ |
Amir Goldstein | e593b2b | 2017-01-23 14:32:21 +0200 | [diff] [blame] | 258 | static int ovl_sync_fs(struct super_block *sb, int wait) |
| 259 | { |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 260 | struct ovl_fs *ofs = sb->s_fs_info; |
Amir Goldstein | e593b2b | 2017-01-23 14:32:21 +0200 | [diff] [blame] | 261 | struct super_block *upper_sb; |
| 262 | int ret; |
| 263 | |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 264 | if (!ovl_upper_mnt(ofs)) |
Amir Goldstein | e593b2b | 2017-01-23 14:32:21 +0200 | [diff] [blame] | 265 | return 0; |
Chengguang Xu | e8d4bfe | 2017-11-29 10:01:32 +0800 | [diff] [blame] | 266 | |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 267 | if (!ovl_should_sync(ofs)) |
| 268 | return 0; |
Chengguang Xu | e8d4bfe | 2017-11-29 10:01:32 +0800 | [diff] [blame] | 269 | /* |
Konstantin Khlebnikov | 32b1924b2 | 2020-04-09 11:29:47 +0300 | [diff] [blame] | 270 | * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC). |
| 271 | * All the super blocks will be iterated, including upper_sb. |
Chengguang Xu | e8d4bfe | 2017-11-29 10:01:32 +0800 | [diff] [blame] | 272 | * |
| 273 | * If this is a syncfs(2) call, then we do need to call |
| 274 | * sync_filesystem() on upper_sb, but enough if we do it when being |
| 275 | * called with wait == 1. |
| 276 | */ |
| 277 | if (!wait) |
Amir Goldstein | e593b2b | 2017-01-23 14:32:21 +0200 | [diff] [blame] | 278 | return 0; |
| 279 | |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 280 | upper_sb = ovl_upper_mnt(ofs)->mnt_sb; |
Chengguang Xu | e8d4bfe | 2017-11-29 10:01:32 +0800 | [diff] [blame] | 281 | |
Amir Goldstein | e593b2b | 2017-01-23 14:32:21 +0200 | [diff] [blame] | 282 | down_read(&upper_sb->s_umount); |
Chengguang Xu | e8d4bfe | 2017-11-29 10:01:32 +0800 | [diff] [blame] | 283 | ret = sync_filesystem(upper_sb); |
Amir Goldstein | e593b2b | 2017-01-23 14:32:21 +0200 | [diff] [blame] | 284 | up_read(&upper_sb->s_umount); |
Chengguang Xu | e8d4bfe | 2017-11-29 10:01:32 +0800 | [diff] [blame] | 285 | |
Amir Goldstein | e593b2b | 2017-01-23 14:32:21 +0200 | [diff] [blame] | 286 | return ret; |
| 287 | } |
| 288 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 289 | /** |
| 290 | * ovl_statfs |
| 291 | * @sb: The overlayfs super block |
| 292 | * @buf: The struct kstatfs to fill in with stats |
| 293 | * |
| 294 | * Get the filesystem statistics. As writes always target the upper layer |
Miklos Szeredi | 4ebc581 | 2014-12-13 00:59:46 +0100 | [diff] [blame] | 295 | * filesystem pass the statfs to the upper filesystem (if it exists) |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 296 | */ |
| 297 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 298 | { |
| 299 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 300 | struct dentry *root_dentry = dentry->d_sb->s_root; |
| 301 | struct path path; |
| 302 | int err; |
| 303 | |
Miklos Szeredi | 4ebc581 | 2014-12-13 00:59:46 +0100 | [diff] [blame] | 304 | ovl_path_real(root_dentry, &path); |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 305 | |
| 306 | err = vfs_statfs(&path, buf); |
| 307 | if (!err) { |
Miklos Szeredi | 6b2d5fe | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 308 | buf->f_namelen = ofs->namelen; |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 309 | buf->f_type = OVERLAYFS_SUPER_MAGIC; |
| 310 | } |
| 311 | |
| 312 | return err; |
| 313 | } |
| 314 | |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 315 | /* Will this overlay be forced to mount/remount ro? */ |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 316 | static bool ovl_force_readonly(struct ovl_fs *ofs) |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 317 | { |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 318 | return (!ovl_upper_mnt(ofs) || !ofs->workdir); |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 319 | } |
| 320 | |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 321 | static const char *ovl_redirect_mode_def(void) |
| 322 | { |
| 323 | return ovl_redirect_dir_def ? "on" : "off"; |
| 324 | } |
| 325 | |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 326 | static const char * const ovl_xino_str[] = { |
| 327 | "off", |
| 328 | "auto", |
| 329 | "on", |
| 330 | }; |
| 331 | |
| 332 | static inline int ovl_xino_def(void) |
| 333 | { |
| 334 | return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF; |
| 335 | } |
| 336 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 337 | /** |
| 338 | * ovl_show_options |
| 339 | * |
| 340 | * Prints the mount options for a given superblock. |
| 341 | * Returns zero; does not fail. |
| 342 | */ |
| 343 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) |
| 344 | { |
| 345 | struct super_block *sb = dentry->d_sb; |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 346 | struct ovl_fs *ofs = sb->s_fs_info; |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 347 | |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 348 | seq_show_option(m, "lowerdir", ofs->config.lowerdir); |
| 349 | if (ofs->config.upperdir) { |
| 350 | seq_show_option(m, "upperdir", ofs->config.upperdir); |
| 351 | seq_show_option(m, "workdir", ofs->config.workdir); |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 352 | } |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 353 | if (ofs->config.default_permissions) |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 354 | seq_puts(m, ",default_permissions"); |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 355 | if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0) |
| 356 | seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode); |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 357 | if (ofs->config.index != ovl_index_def) |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 358 | seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off"); |
Pavel Tikhomirov | 5830fb6 | 2020-10-13 17:59:54 +0300 | [diff] [blame^] | 359 | if (!ofs->config.uuid) |
| 360 | seq_puts(m, ",uuid=off"); |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 361 | if (ofs->config.nfs_export != ovl_nfs_export_def) |
| 362 | seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ? |
| 363 | "on" : "off"); |
Amir Goldstein | 0f831ec | 2019-11-16 18:14:41 +0200 | [diff] [blame] | 364 | if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(sb)) |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 365 | seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]); |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 366 | if (ofs->config.metacopy != ovl_metacopy_def) |
| 367 | seq_printf(m, ",metacopy=%s", |
| 368 | ofs->config.metacopy ? "on" : "off"); |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 369 | if (ofs->config.ovl_volatile) |
| 370 | seq_puts(m, ",volatile"); |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 374 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
| 375 | { |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 376 | struct ovl_fs *ofs = sb->s_fs_info; |
Chengguang Xu | 399c109 | 2020-04-22 12:28:43 +0800 | [diff] [blame] | 377 | struct super_block *upper_sb; |
| 378 | int ret = 0; |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 379 | |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 380 | if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs)) |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 381 | return -EROFS; |
| 382 | |
Chengguang Xu | 399c109 | 2020-04-22 12:28:43 +0800 | [diff] [blame] | 383 | if (*flags & SB_RDONLY && !sb_rdonly(sb)) { |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 384 | upper_sb = ovl_upper_mnt(ofs)->mnt_sb; |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 385 | if (ovl_should_sync(ofs)) { |
| 386 | down_read(&upper_sb->s_umount); |
| 387 | ret = sync_filesystem(upper_sb); |
| 388 | up_read(&upper_sb->s_umount); |
| 389 | } |
Chengguang Xu | 399c109 | 2020-04-22 12:28:43 +0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | return ret; |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 393 | } |
| 394 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 395 | static const struct super_operations ovl_super_operations = { |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 396 | .alloc_inode = ovl_alloc_inode, |
Al Viro | 0b269de | 2019-04-15 22:52:17 -0400 | [diff] [blame] | 397 | .free_inode = ovl_free_inode, |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 398 | .destroy_inode = ovl_destroy_inode, |
| 399 | .drop_inode = generic_delete_inode, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 400 | .put_super = ovl_put_super, |
Amir Goldstein | e593b2b | 2017-01-23 14:32:21 +0200 | [diff] [blame] | 401 | .sync_fs = ovl_sync_fs, |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 402 | .statfs = ovl_statfs, |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 403 | .show_options = ovl_show_options, |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 404 | .remount_fs = ovl_remount, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 405 | }; |
| 406 | |
| 407 | enum { |
| 408 | OPT_LOWERDIR, |
| 409 | OPT_UPPERDIR, |
| 410 | OPT_WORKDIR, |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 411 | OPT_DEFAULT_PERMISSIONS, |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 412 | OPT_REDIRECT_DIR, |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 413 | OPT_INDEX_ON, |
| 414 | OPT_INDEX_OFF, |
Pavel Tikhomirov | 5830fb6 | 2020-10-13 17:59:54 +0300 | [diff] [blame^] | 415 | OPT_UUID_ON, |
| 416 | OPT_UUID_OFF, |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 417 | OPT_NFS_EXPORT_ON, |
| 418 | OPT_NFS_EXPORT_OFF, |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 419 | OPT_XINO_ON, |
| 420 | OPT_XINO_OFF, |
| 421 | OPT_XINO_AUTO, |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 422 | OPT_METACOPY_ON, |
| 423 | OPT_METACOPY_OFF, |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 424 | OPT_VOLATILE, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 425 | OPT_ERR, |
| 426 | }; |
| 427 | |
| 428 | static const match_table_t ovl_tokens = { |
| 429 | {OPT_LOWERDIR, "lowerdir=%s"}, |
| 430 | {OPT_UPPERDIR, "upperdir=%s"}, |
| 431 | {OPT_WORKDIR, "workdir=%s"}, |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 432 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 433 | {OPT_REDIRECT_DIR, "redirect_dir=%s"}, |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 434 | {OPT_INDEX_ON, "index=on"}, |
| 435 | {OPT_INDEX_OFF, "index=off"}, |
Pavel Tikhomirov | 5830fb6 | 2020-10-13 17:59:54 +0300 | [diff] [blame^] | 436 | {OPT_UUID_ON, "uuid=on"}, |
| 437 | {OPT_UUID_OFF, "uuid=off"}, |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 438 | {OPT_NFS_EXPORT_ON, "nfs_export=on"}, |
| 439 | {OPT_NFS_EXPORT_OFF, "nfs_export=off"}, |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 440 | {OPT_XINO_ON, "xino=on"}, |
| 441 | {OPT_XINO_OFF, "xino=off"}, |
| 442 | {OPT_XINO_AUTO, "xino=auto"}, |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 443 | {OPT_METACOPY_ON, "metacopy=on"}, |
| 444 | {OPT_METACOPY_OFF, "metacopy=off"}, |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 445 | {OPT_VOLATILE, "volatile"}, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 446 | {OPT_ERR, NULL} |
| 447 | }; |
| 448 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 449 | static char *ovl_next_opt(char **s) |
| 450 | { |
| 451 | char *sbegin = *s; |
| 452 | char *p; |
| 453 | |
| 454 | if (sbegin == NULL) |
| 455 | return NULL; |
| 456 | |
| 457 | for (p = sbegin; *p; p++) { |
| 458 | if (*p == '\\') { |
| 459 | p++; |
| 460 | if (!*p) |
| 461 | break; |
| 462 | } else if (*p == ',') { |
| 463 | *p = '\0'; |
| 464 | *s = p + 1; |
| 465 | return sbegin; |
| 466 | } |
| 467 | } |
| 468 | *s = NULL; |
| 469 | return sbegin; |
| 470 | } |
| 471 | |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 472 | static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode) |
| 473 | { |
| 474 | if (strcmp(mode, "on") == 0) { |
| 475 | config->redirect_dir = true; |
| 476 | /* |
| 477 | * Does not make sense to have redirect creation without |
| 478 | * redirect following. |
| 479 | */ |
| 480 | config->redirect_follow = true; |
| 481 | } else if (strcmp(mode, "follow") == 0) { |
| 482 | config->redirect_follow = true; |
| 483 | } else if (strcmp(mode, "off") == 0) { |
| 484 | if (ovl_redirect_always_follow) |
| 485 | config->redirect_follow = true; |
| 486 | } else if (strcmp(mode, "nofollow") != 0) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 487 | pr_err("bad mount option \"redirect_dir=%s\"\n", |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 488 | mode); |
| 489 | return -EINVAL; |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 495 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
| 496 | { |
| 497 | char *p; |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 498 | int err; |
Miklos Szeredi | d47748e | 2018-11-01 21:31:39 +0100 | [diff] [blame] | 499 | bool metacopy_opt = false, redirect_opt = false; |
Amir Goldstein | b0def88 | 2020-04-09 18:58:34 +0300 | [diff] [blame] | 500 | bool nfs_export_opt = false, index_opt = false; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 501 | |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 502 | config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL); |
| 503 | if (!config->redirect_mode) |
| 504 | return -ENOMEM; |
| 505 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 506 | while ((p = ovl_next_opt(&opt)) != NULL) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 507 | int token; |
| 508 | substring_t args[MAX_OPT_ARGS]; |
| 509 | |
| 510 | if (!*p) |
| 511 | continue; |
| 512 | |
| 513 | token = match_token(p, ovl_tokens, args); |
| 514 | switch (token) { |
| 515 | case OPT_UPPERDIR: |
| 516 | kfree(config->upperdir); |
| 517 | config->upperdir = match_strdup(&args[0]); |
| 518 | if (!config->upperdir) |
| 519 | return -ENOMEM; |
| 520 | break; |
| 521 | |
| 522 | case OPT_LOWERDIR: |
| 523 | kfree(config->lowerdir); |
| 524 | config->lowerdir = match_strdup(&args[0]); |
| 525 | if (!config->lowerdir) |
| 526 | return -ENOMEM; |
| 527 | break; |
| 528 | |
| 529 | case OPT_WORKDIR: |
| 530 | kfree(config->workdir); |
| 531 | config->workdir = match_strdup(&args[0]); |
| 532 | if (!config->workdir) |
| 533 | return -ENOMEM; |
| 534 | break; |
| 535 | |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 536 | case OPT_DEFAULT_PERMISSIONS: |
| 537 | config->default_permissions = true; |
| 538 | break; |
| 539 | |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 540 | case OPT_REDIRECT_DIR: |
| 541 | kfree(config->redirect_mode); |
| 542 | config->redirect_mode = match_strdup(&args[0]); |
| 543 | if (!config->redirect_mode) |
| 544 | return -ENOMEM; |
Miklos Szeredi | d47748e | 2018-11-01 21:31:39 +0100 | [diff] [blame] | 545 | redirect_opt = true; |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 546 | break; |
| 547 | |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 548 | case OPT_INDEX_ON: |
| 549 | config->index = true; |
Amir Goldstein | b0def88 | 2020-04-09 18:58:34 +0300 | [diff] [blame] | 550 | index_opt = true; |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 551 | break; |
| 552 | |
| 553 | case OPT_INDEX_OFF: |
| 554 | config->index = false; |
Amir Goldstein | b0def88 | 2020-04-09 18:58:34 +0300 | [diff] [blame] | 555 | index_opt = true; |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 556 | break; |
| 557 | |
Pavel Tikhomirov | 5830fb6 | 2020-10-13 17:59:54 +0300 | [diff] [blame^] | 558 | case OPT_UUID_ON: |
| 559 | config->uuid = true; |
| 560 | break; |
| 561 | |
| 562 | case OPT_UUID_OFF: |
| 563 | config->uuid = false; |
| 564 | break; |
| 565 | |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 566 | case OPT_NFS_EXPORT_ON: |
| 567 | config->nfs_export = true; |
Amir Goldstein | b0def88 | 2020-04-09 18:58:34 +0300 | [diff] [blame] | 568 | nfs_export_opt = true; |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 569 | break; |
| 570 | |
| 571 | case OPT_NFS_EXPORT_OFF: |
| 572 | config->nfs_export = false; |
Amir Goldstein | b0def88 | 2020-04-09 18:58:34 +0300 | [diff] [blame] | 573 | nfs_export_opt = true; |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 574 | break; |
| 575 | |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 576 | case OPT_XINO_ON: |
| 577 | config->xino = OVL_XINO_ON; |
| 578 | break; |
| 579 | |
| 580 | case OPT_XINO_OFF: |
| 581 | config->xino = OVL_XINO_OFF; |
| 582 | break; |
| 583 | |
| 584 | case OPT_XINO_AUTO: |
| 585 | config->xino = OVL_XINO_AUTO; |
| 586 | break; |
| 587 | |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 588 | case OPT_METACOPY_ON: |
| 589 | config->metacopy = true; |
Miklos Szeredi | d47748e | 2018-11-01 21:31:39 +0100 | [diff] [blame] | 590 | metacopy_opt = true; |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 591 | break; |
| 592 | |
| 593 | case OPT_METACOPY_OFF: |
| 594 | config->metacopy = false; |
Amir Goldstein | b0def88 | 2020-04-09 18:58:34 +0300 | [diff] [blame] | 595 | metacopy_opt = true; |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 596 | break; |
| 597 | |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 598 | case OPT_VOLATILE: |
| 599 | config->ovl_volatile = true; |
| 600 | break; |
| 601 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 602 | default: |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 603 | pr_err("unrecognized mount option \"%s\" or missing value\n", |
| 604 | p); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 605 | return -EINVAL; |
| 606 | } |
| 607 | } |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 608 | |
Amir Goldstein | f0e1266e | 2020-07-13 17:19:44 +0300 | [diff] [blame] | 609 | /* Workdir/index are useless in non-upper mount */ |
| 610 | if (!config->upperdir) { |
| 611 | if (config->workdir) { |
| 612 | pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n", |
| 613 | config->workdir); |
| 614 | kfree(config->workdir); |
| 615 | config->workdir = NULL; |
| 616 | } |
| 617 | if (config->index && index_opt) { |
| 618 | pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n"); |
| 619 | index_opt = false; |
| 620 | } |
| 621 | config->index = false; |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 622 | } |
| 623 | |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 624 | if (!config->upperdir && config->ovl_volatile) { |
| 625 | pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n"); |
| 626 | config->ovl_volatile = false; |
| 627 | } |
| 628 | |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 629 | err = ovl_parse_redirect_mode(config, config->redirect_mode); |
| 630 | if (err) |
| 631 | return err; |
| 632 | |
Miklos Szeredi | d47748e | 2018-11-01 21:31:39 +0100 | [diff] [blame] | 633 | /* |
| 634 | * This is to make the logic below simpler. It doesn't make any other |
| 635 | * difference, since config->redirect_dir is only used for upper. |
| 636 | */ |
| 637 | if (!config->upperdir && config->redirect_follow) |
| 638 | config->redirect_dir = true; |
| 639 | |
| 640 | /* Resolve metacopy -> redirect_dir dependency */ |
| 641 | if (config->metacopy && !config->redirect_dir) { |
| 642 | if (metacopy_opt && redirect_opt) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 643 | pr_err("conflicting options: metacopy=on,redirect_dir=%s\n", |
Miklos Szeredi | d47748e | 2018-11-01 21:31:39 +0100 | [diff] [blame] | 644 | config->redirect_mode); |
| 645 | return -EINVAL; |
| 646 | } |
| 647 | if (redirect_opt) { |
| 648 | /* |
| 649 | * There was an explicit redirect_dir=... that resulted |
| 650 | * in this conflict. |
| 651 | */ |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 652 | pr_info("disabling metacopy due to redirect_dir=%s\n", |
Miklos Szeredi | d47748e | 2018-11-01 21:31:39 +0100 | [diff] [blame] | 653 | config->redirect_mode); |
| 654 | config->metacopy = false; |
| 655 | } else { |
| 656 | /* Automatically enable redirect otherwise. */ |
| 657 | config->redirect_follow = config->redirect_dir = true; |
| 658 | } |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 659 | } |
| 660 | |
Amir Goldstein | b0def88 | 2020-04-09 18:58:34 +0300 | [diff] [blame] | 661 | /* Resolve nfs_export -> index dependency */ |
| 662 | if (config->nfs_export && !config->index) { |
Amir Goldstein | f0e1266e | 2020-07-13 17:19:44 +0300 | [diff] [blame] | 663 | if (!config->upperdir && config->redirect_follow) { |
| 664 | pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n"); |
| 665 | config->nfs_export = false; |
| 666 | } else if (nfs_export_opt && index_opt) { |
Amir Goldstein | b0def88 | 2020-04-09 18:58:34 +0300 | [diff] [blame] | 667 | pr_err("conflicting options: nfs_export=on,index=off\n"); |
| 668 | return -EINVAL; |
Amir Goldstein | f0e1266e | 2020-07-13 17:19:44 +0300 | [diff] [blame] | 669 | } else if (index_opt) { |
Amir Goldstein | b0def88 | 2020-04-09 18:58:34 +0300 | [diff] [blame] | 670 | /* |
| 671 | * There was an explicit index=off that resulted |
| 672 | * in this conflict. |
| 673 | */ |
| 674 | pr_info("disabling nfs_export due to index=off\n"); |
| 675 | config->nfs_export = false; |
| 676 | } else { |
| 677 | /* Automatically enable index otherwise. */ |
| 678 | config->index = true; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | /* Resolve nfs_export -> !metacopy dependency */ |
| 683 | if (config->nfs_export && config->metacopy) { |
| 684 | if (nfs_export_opt && metacopy_opt) { |
| 685 | pr_err("conflicting options: nfs_export=on,metacopy=on\n"); |
| 686 | return -EINVAL; |
| 687 | } |
| 688 | if (metacopy_opt) { |
| 689 | /* |
| 690 | * There was an explicit metacopy=on that resulted |
| 691 | * in this conflict. |
| 692 | */ |
| 693 | pr_info("disabling nfs_export due to metacopy=on\n"); |
| 694 | config->nfs_export = false; |
| 695 | } else { |
| 696 | /* |
| 697 | * There was an explicit nfs_export=on that resulted |
| 698 | * in this conflict. |
| 699 | */ |
| 700 | pr_info("disabling metacopy due to nfs_export=on\n"); |
| 701 | config->metacopy = false; |
| 702 | } |
| 703 | } |
| 704 | |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 705 | return 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | #define OVL_WORKDIR_NAME "work" |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 709 | #define OVL_INDEXDIR_NAME "index" |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 710 | |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 711 | static struct dentry *ovl_workdir_create(struct ovl_fs *ofs, |
Amir Goldstein | 6b8aa12 | 2017-06-21 15:28:35 +0300 | [diff] [blame] | 712 | const char *name, bool persist) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 713 | { |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 714 | struct inode *dir = ofs->workbasedir->d_inode; |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 715 | struct vfsmount *mnt = ovl_upper_mnt(ofs); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 716 | struct dentry *work; |
| 717 | int err; |
| 718 | bool retried = false; |
| 719 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 720 | inode_lock_nested(dir, I_MUTEX_PARENT); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 721 | retry: |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 722 | work = lookup_one_len(name, ofs->workbasedir, strlen(name)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 723 | |
| 724 | if (!IS_ERR(work)) { |
Miklos Szeredi | c11b9fd | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 725 | struct iattr attr = { |
| 726 | .ia_valid = ATTR_MODE, |
Al Viro | 32a3d84 | 2016-12-04 17:33:17 +0000 | [diff] [blame] | 727 | .ia_mode = S_IFDIR | 0, |
Miklos Szeredi | c11b9fd | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 728 | }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 729 | |
| 730 | if (work->d_inode) { |
| 731 | err = -EEXIST; |
| 732 | if (retried) |
| 733 | goto out_dput; |
| 734 | |
Amir Goldstein | 6b8aa12 | 2017-06-21 15:28:35 +0300 | [diff] [blame] | 735 | if (persist) |
| 736 | goto out_unlock; |
| 737 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 738 | retried = true; |
Amir Goldstein | 235ce9e | 2020-08-30 23:28:03 +0300 | [diff] [blame] | 739 | err = ovl_workdir_cleanup(dir, mnt, work, 0); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 740 | dput(work); |
Amir Goldstein | 235ce9e | 2020-08-30 23:28:03 +0300 | [diff] [blame] | 741 | if (err == -EINVAL) { |
| 742 | work = ERR_PTR(err); |
| 743 | goto out_unlock; |
| 744 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 745 | goto retry; |
| 746 | } |
| 747 | |
Miklos Szeredi | 95a1c81 | 2018-05-16 17:51:25 +0300 | [diff] [blame] | 748 | work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode)); |
| 749 | err = PTR_ERR(work); |
| 750 | if (IS_ERR(work)) |
| 751 | goto out_err; |
Miklos Szeredi | c11b9fd | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 752 | |
Miklos Szeredi | cb348ed | 2016-10-04 14:40:44 +0200 | [diff] [blame] | 753 | /* |
| 754 | * Try to remove POSIX ACL xattrs from workdir. We are good if: |
| 755 | * |
| 756 | * a) success (there was a POSIX ACL xattr and was removed) |
| 757 | * b) -ENODATA (there was no POSIX ACL xattr) |
| 758 | * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported) |
| 759 | * |
| 760 | * There are various other error values that could effectively |
| 761 | * mean that the xattr doesn't exist (e.g. -ERANGE is returned |
| 762 | * if the xattr name is too long), but the set of filesystems |
| 763 | * allowed as upper are limited to "normal" ones, where checking |
| 764 | * for the above two errors is sufficient. |
| 765 | */ |
Miklos Szeredi | c11b9fd | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 766 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT); |
Miklos Szeredi | e1ff3dd | 2016-09-05 13:55:20 +0200 | [diff] [blame] | 767 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
Miklos Szeredi | c11b9fd | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 768 | goto out_dput; |
| 769 | |
| 770 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS); |
Miklos Szeredi | e1ff3dd | 2016-09-05 13:55:20 +0200 | [diff] [blame] | 771 | if (err && err != -ENODATA && err != -EOPNOTSUPP) |
Miklos Szeredi | c11b9fd | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 772 | goto out_dput; |
| 773 | |
| 774 | /* Clear any inherited mode bits */ |
| 775 | inode_lock(work->d_inode); |
| 776 | err = notify_change(work, &attr, NULL); |
| 777 | inode_unlock(work->d_inode); |
| 778 | if (err) |
| 779 | goto out_dput; |
Amir Goldstein | 6b8aa12 | 2017-06-21 15:28:35 +0300 | [diff] [blame] | 780 | } else { |
| 781 | err = PTR_ERR(work); |
| 782 | goto out_err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 783 | } |
| 784 | out_unlock: |
youngjun | 2068cf7 | 2020-06-07 02:04:06 -0700 | [diff] [blame] | 785 | inode_unlock(dir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 786 | return work; |
| 787 | |
| 788 | out_dput: |
| 789 | dput(work); |
Amir Goldstein | 6b8aa12 | 2017-06-21 15:28:35 +0300 | [diff] [blame] | 790 | out_err: |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 791 | pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n", |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 792 | ofs->config.workdir, name, -err); |
Amir Goldstein | 6b8aa12 | 2017-06-21 15:28:35 +0300 | [diff] [blame] | 793 | work = NULL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 794 | goto out_unlock; |
| 795 | } |
| 796 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 797 | static void ovl_unescape(char *s) |
| 798 | { |
| 799 | char *d = s; |
| 800 | |
| 801 | for (;; s++, d++) { |
| 802 | if (*s == '\\') |
| 803 | s++; |
| 804 | *d = *s; |
| 805 | if (!*s) |
| 806 | break; |
| 807 | } |
| 808 | } |
| 809 | |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 810 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
| 811 | { |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 812 | int err = -EINVAL; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 813 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 814 | if (!*name) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 815 | pr_err("empty lowerdir\n"); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 816 | goto out; |
| 817 | } |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 818 | err = kern_path(name, LOOKUP_FOLLOW, path); |
| 819 | if (err) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 820 | pr_err("failed to resolve '%s': %i\n", name, err); |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 821 | goto out; |
| 822 | } |
| 823 | err = -EINVAL; |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 824 | if (ovl_dentry_weird(path->dentry)) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 825 | pr_err("filesystem on '%s' not supported\n", name); |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 826 | goto out_put; |
| 827 | } |
Miklos Szeredi | 2b8c30e | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 828 | if (!d_is_dir(path->dentry)) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 829 | pr_err("'%s' not a directory\n", name); |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 830 | goto out_put; |
| 831 | } |
| 832 | return 0; |
| 833 | |
| 834 | out_put: |
Miklos Szeredi | 8aafcb5 | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 835 | path_put_init(path); |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 836 | out: |
| 837 | return err; |
| 838 | } |
| 839 | |
| 840 | static int ovl_mount_dir(const char *name, struct path *path) |
| 841 | { |
| 842 | int err = -ENOMEM; |
| 843 | char *tmp = kstrdup(name, GFP_KERNEL); |
| 844 | |
| 845 | if (tmp) { |
| 846 | ovl_unescape(tmp); |
| 847 | err = ovl_mount_dir_noesc(tmp, path); |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 848 | |
Miklos Szeredi | bccece1 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 849 | if (!err && path->dentry->d_flags & DCACHE_OP_REAL) { |
Miklos Szeredi | 7925dad | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 850 | pr_err("filesystem on '%s' not supported as upperdir\n", |
| 851 | tmp); |
| 852 | path_put_init(path); |
| 853 | err = -EINVAL; |
| 854 | } |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 855 | kfree(tmp); |
| 856 | } |
| 857 | return err; |
| 858 | } |
| 859 | |
Miklos Szeredi | 6b2d5fe | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 860 | static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs, |
| 861 | const char *name) |
| 862 | { |
| 863 | struct kstatfs statfs; |
| 864 | int err = vfs_statfs(path, &statfs); |
| 865 | |
| 866 | if (err) |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 867 | pr_err("statfs failed on '%s'\n", name); |
Miklos Szeredi | 6b2d5fe | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 868 | else |
| 869 | ofs->namelen = max(ofs->namelen, statfs.f_namelen); |
| 870 | |
| 871 | return err; |
| 872 | } |
| 873 | |
| 874 | static int ovl_lower_dir(const char *name, struct path *path, |
Miklos Szeredi | f428884 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 875 | struct ovl_fs *ofs, int *stack_depth) |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 876 | { |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 877 | int fh_type; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 878 | int err; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 879 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 880 | err = ovl_mount_dir_noesc(name, path); |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 881 | if (err) |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 882 | return err; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 883 | |
Miklos Szeredi | 6b2d5fe | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 884 | err = ovl_check_namelen(path, ofs, name); |
| 885 | if (err) |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 886 | return err; |
Miklos Szeredi | 6b2d5fe | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 887 | |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 888 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); |
| 889 | |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 890 | /* |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 891 | * The inodes index feature and NFS export need to encode and decode |
| 892 | * file handles, so they require that all layers support them. |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 893 | */ |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 894 | fh_type = ovl_can_decode_fh(path->dentry->d_sb); |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 895 | if ((ofs->config.nfs_export || |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 896 | (ofs->config.index && ofs->config.upperdir)) && !fh_type) { |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 897 | ofs->config.index = false; |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 898 | ofs->config.nfs_export = false; |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 899 | pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n", |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 900 | name); |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 901 | } |
| 902 | |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 903 | /* Check if lower fs has 32bit inode numbers */ |
| 904 | if (fh_type != FILEID_INO32_GEN) |
Amir Goldstein | 0f831ec | 2019-11-16 18:14:41 +0200 | [diff] [blame] | 905 | ofs->xino_mode = -1; |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 906 | |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 907 | return 0; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 908 | } |
| 909 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 910 | /* Workdir should not be subdir of upperdir and vice versa */ |
| 911 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) |
| 912 | { |
| 913 | bool ok = false; |
| 914 | |
| 915 | if (workdir != upperdir) { |
| 916 | ok = (lock_rename(workdir, upperdir) == NULL); |
| 917 | unlock_rename(workdir, upperdir); |
| 918 | } |
| 919 | return ok; |
| 920 | } |
| 921 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 922 | static unsigned int ovl_split_lowerdirs(char *str) |
| 923 | { |
| 924 | unsigned int ctr = 1; |
| 925 | char *s, *d; |
| 926 | |
| 927 | for (s = d = str;; s++, d++) { |
| 928 | if (*s == '\\') { |
| 929 | s++; |
| 930 | } else if (*s == ':') { |
| 931 | *d = '\0'; |
| 932 | ctr++; |
| 933 | continue; |
| 934 | } |
| 935 | *d = *s; |
| 936 | if (!*s) |
| 937 | break; |
| 938 | } |
| 939 | return ctr; |
| 940 | } |
| 941 | |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 942 | static int __maybe_unused |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 943 | ovl_posix_acl_xattr_get(const struct xattr_handler *handler, |
| 944 | struct dentry *dentry, struct inode *inode, |
| 945 | const char *name, void *buffer, size_t size) |
| 946 | { |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 947 | return ovl_xattr_get(dentry, inode, handler->name, buffer, size); |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | static int __maybe_unused |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 951 | ovl_posix_acl_xattr_set(const struct xattr_handler *handler, |
| 952 | struct dentry *dentry, struct inode *inode, |
| 953 | const char *name, const void *value, |
| 954 | size_t size, int flags) |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 955 | { |
| 956 | struct dentry *workdir = ovl_workdir(dentry); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 957 | struct inode *realinode = ovl_inode_real(inode); |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 958 | struct posix_acl *acl = NULL; |
| 959 | int err; |
| 960 | |
| 961 | /* Check that everything is OK before copy-up */ |
| 962 | if (value) { |
| 963 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
| 964 | if (IS_ERR(acl)) |
| 965 | return PTR_ERR(acl); |
| 966 | } |
| 967 | err = -EOPNOTSUPP; |
| 968 | if (!IS_POSIXACL(d_inode(workdir))) |
| 969 | goto out_acl_release; |
| 970 | if (!realinode->i_op->set_acl) |
| 971 | goto out_acl_release; |
| 972 | if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) { |
| 973 | err = acl ? -EACCES : 0; |
| 974 | goto out_acl_release; |
| 975 | } |
| 976 | err = -EPERM; |
| 977 | if (!inode_owner_or_capable(inode)) |
| 978 | goto out_acl_release; |
| 979 | |
| 980 | posix_acl_release(acl); |
| 981 | |
Miklos Szeredi | fd3220d | 2016-10-31 14:42:14 +0100 | [diff] [blame] | 982 | /* |
| 983 | * Check if sgid bit needs to be cleared (actual setacl operation will |
| 984 | * be done with mounter's capabilities and so that won't do it for us). |
| 985 | */ |
| 986 | if (unlikely(inode->i_mode & S_ISGID) && |
| 987 | handler->flags == ACL_TYPE_ACCESS && |
| 988 | !in_group_p(inode->i_gid) && |
| 989 | !capable_wrt_inode_uidgid(inode, CAP_FSETID)) { |
| 990 | struct iattr iattr = { .ia_valid = ATTR_KILL_SGID }; |
| 991 | |
| 992 | err = ovl_setattr(dentry, &iattr); |
| 993 | if (err) |
| 994 | return err; |
| 995 | } |
| 996 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 997 | err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags); |
Miklos Szeredi | ce31513 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 998 | if (!err) |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 999 | ovl_copyattr(ovl_inode_real(inode), inode); |
Miklos Szeredi | ce31513 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 1000 | |
| 1001 | return err; |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1002 | |
| 1003 | out_acl_release: |
| 1004 | posix_acl_release(acl); |
| 1005 | return err; |
| 1006 | } |
| 1007 | |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 1008 | static int ovl_own_xattr_get(const struct xattr_handler *handler, |
| 1009 | struct dentry *dentry, struct inode *inode, |
| 1010 | const char *name, void *buffer, size_t size) |
| 1011 | { |
Amir Goldstein | 48fab5d | 2016-11-16 11:22:39 +0200 | [diff] [blame] | 1012 | return -EOPNOTSUPP; |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1015 | static int ovl_own_xattr_set(const struct xattr_handler *handler, |
| 1016 | struct dentry *dentry, struct inode *inode, |
| 1017 | const char *name, const void *value, |
| 1018 | size_t size, int flags) |
| 1019 | { |
Amir Goldstein | 48fab5d | 2016-11-16 11:22:39 +0200 | [diff] [blame] | 1020 | return -EOPNOTSUPP; |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1021 | } |
| 1022 | |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 1023 | static int ovl_other_xattr_get(const struct xattr_handler *handler, |
| 1024 | struct dentry *dentry, struct inode *inode, |
| 1025 | const char *name, void *buffer, size_t size) |
| 1026 | { |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 1027 | return ovl_xattr_get(dentry, inode, name, buffer, size); |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 1030 | static int ovl_other_xattr_set(const struct xattr_handler *handler, |
| 1031 | struct dentry *dentry, struct inode *inode, |
| 1032 | const char *name, const void *value, |
| 1033 | size_t size, int flags) |
| 1034 | { |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 1035 | return ovl_xattr_set(dentry, inode, name, value, size, flags); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 1038 | static const struct xattr_handler __maybe_unused |
| 1039 | ovl_posix_acl_access_xattr_handler = { |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1040 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
| 1041 | .flags = ACL_TYPE_ACCESS, |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 1042 | .get = ovl_posix_acl_xattr_get, |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1043 | .set = ovl_posix_acl_xattr_set, |
| 1044 | }; |
| 1045 | |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 1046 | static const struct xattr_handler __maybe_unused |
| 1047 | ovl_posix_acl_default_xattr_handler = { |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1048 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
| 1049 | .flags = ACL_TYPE_DEFAULT, |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 1050 | .get = ovl_posix_acl_xattr_get, |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1051 | .set = ovl_posix_acl_xattr_set, |
| 1052 | }; |
| 1053 | |
| 1054 | static const struct xattr_handler ovl_own_xattr_handler = { |
| 1055 | .prefix = OVL_XATTR_PREFIX, |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 1056 | .get = ovl_own_xattr_get, |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1057 | .set = ovl_own_xattr_set, |
| 1058 | }; |
| 1059 | |
| 1060 | static const struct xattr_handler ovl_other_xattr_handler = { |
| 1061 | .prefix = "", /* catch all */ |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 1062 | .get = ovl_other_xattr_get, |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1063 | .set = ovl_other_xattr_set, |
| 1064 | }; |
| 1065 | |
| 1066 | static const struct xattr_handler *ovl_xattr_handlers[] = { |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 1067 | #ifdef CONFIG_FS_POSIX_ACL |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1068 | &ovl_posix_acl_access_xattr_handler, |
| 1069 | &ovl_posix_acl_default_xattr_handler, |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 1070 | #endif |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1071 | &ovl_own_xattr_handler, |
| 1072 | &ovl_other_xattr_handler, |
| 1073 | NULL |
| 1074 | }; |
| 1075 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1076 | static int ovl_setup_trap(struct super_block *sb, struct dentry *dir, |
| 1077 | struct inode **ptrap, const char *name) |
| 1078 | { |
| 1079 | struct inode *trap; |
| 1080 | int err; |
| 1081 | |
| 1082 | trap = ovl_get_trap_inode(sb, dir); |
Arnd Bergmann | 1dac6f5b | 2019-06-17 14:39:29 +0200 | [diff] [blame] | 1083 | err = PTR_ERR_OR_ZERO(trap); |
| 1084 | if (err) { |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1085 | if (err == -ELOOP) |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1086 | pr_err("conflicting %s path\n", name); |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1087 | return err; |
| 1088 | } |
| 1089 | |
| 1090 | *ptrap = trap; |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1094 | /* |
| 1095 | * Determine how we treat concurrent use of upperdir/workdir based on the |
| 1096 | * index feature. This is papering over mount leaks of container runtimes, |
| 1097 | * for example, an old overlay mount is leaked and now its upperdir is |
| 1098 | * attempted to be used as a lower layer in a new overlay mount. |
| 1099 | */ |
| 1100 | static int ovl_report_in_use(struct ovl_fs *ofs, const char *name) |
| 1101 | { |
| 1102 | if (ofs->config.index) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1103 | pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n", |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1104 | name); |
| 1105 | return -EBUSY; |
| 1106 | } else { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1107 | pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n", |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1108 | name); |
| 1109 | return 0; |
| 1110 | } |
| 1111 | } |
| 1112 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1113 | static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs, |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1114 | struct ovl_layer *upper_layer, struct path *upperpath) |
Miklos Szeredi | 6ee8acf | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1115 | { |
Miklos Szeredi | 5064975 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1116 | struct vfsmount *upper_mnt; |
Miklos Szeredi | 6ee8acf | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1117 | int err; |
| 1118 | |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1119 | err = ovl_mount_dir(ofs->config.upperdir, upperpath); |
Miklos Szeredi | 6ee8acf | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1120 | if (err) |
| 1121 | goto out; |
| 1122 | |
| 1123 | /* Upper fs should not be r/o */ |
| 1124 | if (sb_rdonly(upperpath->mnt->mnt_sb)) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1125 | pr_err("upper fs is r/o, try multi-lower layers mount\n"); |
Miklos Szeredi | 6ee8acf | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1126 | err = -EINVAL; |
| 1127 | goto out; |
| 1128 | } |
| 1129 | |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1130 | err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir); |
Miklos Szeredi | 6ee8acf | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1131 | if (err) |
| 1132 | goto out; |
| 1133 | |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1134 | err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap, |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1135 | "upperdir"); |
| 1136 | if (err) |
| 1137 | goto out; |
| 1138 | |
Miklos Szeredi | 5064975 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1139 | upper_mnt = clone_private_mount(upperpath); |
| 1140 | err = PTR_ERR(upper_mnt); |
| 1141 | if (IS_ERR(upper_mnt)) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1142 | pr_err("failed to clone upperpath\n"); |
Miklos Szeredi | 5064975 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1143 | goto out; |
| 1144 | } |
| 1145 | |
| 1146 | /* Don't inherit atime flags */ |
| 1147 | upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1148 | upper_layer->mnt = upper_mnt; |
| 1149 | upper_layer->idx = 0; |
| 1150 | upper_layer->fsid = 0; |
Miklos Szeredi | 8c25741 | 2018-09-10 11:43:29 +0200 | [diff] [blame] | 1151 | |
Jeffle Xu | 654255f | 2020-04-23 19:06:55 +0800 | [diff] [blame] | 1152 | /* |
| 1153 | * Inherit SB_NOSEC flag from upperdir. |
| 1154 | * |
| 1155 | * This optimization changes behavior when a security related attribute |
| 1156 | * (suid/sgid/security.*) is changed on an underlying layer. This is |
| 1157 | * okay because we don't yet have guarantees in that case, but it will |
| 1158 | * need careful treatment once we want to honour changes to underlying |
| 1159 | * filesystems. |
| 1160 | */ |
| 1161 | if (upper_mnt->mnt_sb->s_flags & SB_NOSEC) |
| 1162 | sb->s_flags |= SB_NOSEC; |
| 1163 | |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1164 | if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) { |
Miklos Szeredi | 8c25741 | 2018-09-10 11:43:29 +0200 | [diff] [blame] | 1165 | ofs->upperdir_locked = true; |
Miklos Szeredi | 8c25741 | 2018-09-10 11:43:29 +0200 | [diff] [blame] | 1166 | } else { |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1167 | err = ovl_report_in_use(ofs, "upperdir"); |
| 1168 | if (err) |
| 1169 | goto out; |
Miklos Szeredi | 8c25741 | 2018-09-10 11:43:29 +0200 | [diff] [blame] | 1170 | } |
| 1171 | |
Miklos Szeredi | 6ee8acf | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1172 | err = 0; |
| 1173 | out: |
| 1174 | return err; |
| 1175 | } |
| 1176 | |
Amir Goldstein | cad218a | 2020-02-20 09:00:19 +0200 | [diff] [blame] | 1177 | /* |
| 1178 | * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and |
| 1179 | * negative values if error is encountered. |
| 1180 | */ |
| 1181 | static int ovl_check_rename_whiteout(struct dentry *workdir) |
| 1182 | { |
| 1183 | struct inode *dir = d_inode(workdir); |
| 1184 | struct dentry *temp; |
| 1185 | struct dentry *dest; |
| 1186 | struct dentry *whiteout; |
| 1187 | struct name_snapshot name; |
| 1188 | int err; |
| 1189 | |
| 1190 | inode_lock_nested(dir, I_MUTEX_PARENT); |
| 1191 | |
| 1192 | temp = ovl_create_temp(workdir, OVL_CATTR(S_IFREG | 0)); |
| 1193 | err = PTR_ERR(temp); |
| 1194 | if (IS_ERR(temp)) |
| 1195 | goto out_unlock; |
| 1196 | |
| 1197 | dest = ovl_lookup_temp(workdir); |
| 1198 | err = PTR_ERR(dest); |
| 1199 | if (IS_ERR(dest)) { |
| 1200 | dput(temp); |
| 1201 | goto out_unlock; |
| 1202 | } |
| 1203 | |
| 1204 | /* Name is inline and stable - using snapshot as a copy helper */ |
| 1205 | take_dentry_name_snapshot(&name, temp); |
| 1206 | err = ovl_do_rename(dir, temp, dir, dest, RENAME_WHITEOUT); |
| 1207 | if (err) { |
| 1208 | if (err == -EINVAL) |
| 1209 | err = 0; |
| 1210 | goto cleanup_temp; |
| 1211 | } |
| 1212 | |
| 1213 | whiteout = lookup_one_len(name.name.name, workdir, name.name.len); |
| 1214 | err = PTR_ERR(whiteout); |
| 1215 | if (IS_ERR(whiteout)) |
| 1216 | goto cleanup_temp; |
| 1217 | |
| 1218 | err = ovl_is_whiteout(whiteout); |
| 1219 | |
| 1220 | /* Best effort cleanup of whiteout and temp file */ |
| 1221 | if (err) |
| 1222 | ovl_cleanup(dir, whiteout); |
| 1223 | dput(whiteout); |
| 1224 | |
| 1225 | cleanup_temp: |
| 1226 | ovl_cleanup(dir, temp); |
| 1227 | release_dentry_name_snapshot(&name); |
| 1228 | dput(temp); |
| 1229 | dput(dest); |
| 1230 | |
| 1231 | out_unlock: |
| 1232 | inode_unlock(dir); |
| 1233 | |
| 1234 | return err; |
| 1235 | } |
| 1236 | |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 1237 | static struct dentry *ovl_lookup_or_create(struct dentry *parent, |
| 1238 | const char *name, umode_t mode) |
| 1239 | { |
| 1240 | size_t len = strlen(name); |
| 1241 | struct dentry *child; |
| 1242 | |
| 1243 | inode_lock_nested(parent->d_inode, I_MUTEX_PARENT); |
| 1244 | child = lookup_one_len(name, parent, len); |
| 1245 | if (!IS_ERR(child) && !child->d_inode) |
| 1246 | child = ovl_create_real(parent->d_inode, child, |
| 1247 | OVL_CATTR(mode)); |
| 1248 | inode_unlock(parent->d_inode); |
| 1249 | dput(parent); |
| 1250 | |
| 1251 | return child; |
| 1252 | } |
| 1253 | |
| 1254 | /* |
| 1255 | * Creates $workdir/work/incompat/volatile/dirty file if it is not already |
| 1256 | * present. |
| 1257 | */ |
| 1258 | static int ovl_create_volatile_dirty(struct ovl_fs *ofs) |
| 1259 | { |
| 1260 | unsigned int ctr; |
| 1261 | struct dentry *d = dget(ofs->workbasedir); |
| 1262 | static const char *const volatile_path[] = { |
| 1263 | OVL_WORKDIR_NAME, "incompat", "volatile", "dirty" |
| 1264 | }; |
| 1265 | const char *const *name = volatile_path; |
| 1266 | |
| 1267 | for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) { |
| 1268 | d = ovl_lookup_or_create(d, *name, ctr > 1 ? S_IFDIR : S_IFREG); |
| 1269 | if (IS_ERR(d)) |
| 1270 | return PTR_ERR(d); |
| 1271 | } |
| 1272 | dput(d); |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1276 | static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, |
| 1277 | struct path *workpath) |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1278 | { |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1279 | struct vfsmount *mnt = ovl_upper_mnt(ofs); |
Amir Goldstein | 235ce9e | 2020-08-30 23:28:03 +0300 | [diff] [blame] | 1280 | struct dentry *temp, *workdir; |
Amir Goldstein | d80172c | 2020-02-20 09:03:08 +0200 | [diff] [blame] | 1281 | bool rename_whiteout; |
| 1282 | bool d_type; |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 1283 | int fh_type; |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1284 | int err; |
| 1285 | |
Amir Goldstein | 2ba9d57 | 2018-01-03 18:54:41 +0200 | [diff] [blame] | 1286 | err = mnt_want_write(mnt); |
| 1287 | if (err) |
| 1288 | return err; |
| 1289 | |
Amir Goldstein | 235ce9e | 2020-08-30 23:28:03 +0300 | [diff] [blame] | 1290 | workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false); |
| 1291 | err = PTR_ERR(workdir); |
| 1292 | if (IS_ERR_OR_NULL(workdir)) |
Amir Goldstein | 2ba9d57 | 2018-01-03 18:54:41 +0200 | [diff] [blame] | 1293 | goto out; |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1294 | |
Amir Goldstein | 235ce9e | 2020-08-30 23:28:03 +0300 | [diff] [blame] | 1295 | ofs->workdir = workdir; |
| 1296 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1297 | err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir"); |
| 1298 | if (err) |
| 1299 | goto out; |
| 1300 | |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1301 | /* |
| 1302 | * Upper should support d_type, else whiteouts are visible. Given |
| 1303 | * workdir and upper are on same fs, we can do iterate_dir() on |
| 1304 | * workdir. This check requires successful creation of workdir in |
| 1305 | * previous step. |
| 1306 | */ |
| 1307 | err = ovl_check_d_type_supported(workpath); |
| 1308 | if (err < 0) |
Amir Goldstein | 2ba9d57 | 2018-01-03 18:54:41 +0200 | [diff] [blame] | 1309 | goto out; |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1310 | |
Amir Goldstein | d80172c | 2020-02-20 09:03:08 +0200 | [diff] [blame] | 1311 | d_type = err; |
| 1312 | if (!d_type) |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1313 | pr_warn("upper fs needs to support d_type.\n"); |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1314 | |
| 1315 | /* Check if upper/work fs supports O_TMPFILE */ |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1316 | temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0); |
| 1317 | ofs->tmpfile = !IS_ERR(temp); |
| 1318 | if (ofs->tmpfile) |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1319 | dput(temp); |
| 1320 | else |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1321 | pr_warn("upper fs does not support tmpfile.\n"); |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1322 | |
Amir Goldstein | cad218a | 2020-02-20 09:00:19 +0200 | [diff] [blame] | 1323 | |
| 1324 | /* Check if upper/work fs supports RENAME_WHITEOUT */ |
| 1325 | err = ovl_check_rename_whiteout(ofs->workdir); |
| 1326 | if (err < 0) |
| 1327 | goto out; |
| 1328 | |
Amir Goldstein | d80172c | 2020-02-20 09:03:08 +0200 | [diff] [blame] | 1329 | rename_whiteout = err; |
| 1330 | if (!rename_whiteout) |
Amir Goldstein | cad218a | 2020-02-20 09:00:19 +0200 | [diff] [blame] | 1331 | pr_warn("upper fs does not support RENAME_WHITEOUT.\n"); |
| 1332 | |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1333 | /* |
| 1334 | * Check if upper/work fs supports trusted.overlay.* xattr |
| 1335 | */ |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 1336 | err = ovl_do_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1); |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1337 | if (err) { |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1338 | ofs->noxattr = true; |
Amir Goldstein | a683737 | 2017-09-19 12:14:18 +0300 | [diff] [blame] | 1339 | ofs->config.index = false; |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 1340 | ofs->config.metacopy = false; |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1341 | pr_warn("upper fs does not support xattr, falling back to index=off and metacopy=off.\n"); |
Amir Goldstein | 2ba9d57 | 2018-01-03 18:54:41 +0200 | [diff] [blame] | 1342 | err = 0; |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1343 | } else { |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 1344 | ovl_do_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE); |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1345 | } |
| 1346 | |
Amir Goldstein | d80172c | 2020-02-20 09:03:08 +0200 | [diff] [blame] | 1347 | /* |
| 1348 | * We allowed sub-optimal upper fs configuration and don't want to break |
| 1349 | * users over kernel upgrade, but we never allowed remote upper fs, so |
| 1350 | * we can enforce strict requirements for remote upper fs. |
| 1351 | */ |
| 1352 | if (ovl_dentry_remote(ofs->workdir) && |
| 1353 | (!d_type || !rename_whiteout || ofs->noxattr)) { |
| 1354 | pr_err("upper fs missing required features.\n"); |
| 1355 | err = -EINVAL; |
| 1356 | goto out; |
| 1357 | } |
| 1358 | |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 1359 | /* |
| 1360 | * For volatile mount, create a incompat/volatile/dirty file to keep |
| 1361 | * track of it. |
| 1362 | */ |
| 1363 | if (ofs->config.ovl_volatile) { |
| 1364 | err = ovl_create_volatile_dirty(ofs); |
| 1365 | if (err < 0) { |
| 1366 | pr_err("Failed to create volatile/dirty file.\n"); |
| 1367 | goto out; |
| 1368 | } |
| 1369 | } |
| 1370 | |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1371 | /* Check if upper/work fs supports file handles */ |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 1372 | fh_type = ovl_can_decode_fh(ofs->workdir->d_sb); |
| 1373 | if (ofs->config.index && !fh_type) { |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1374 | ofs->config.index = false; |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1375 | pr_warn("upper fs does not support file handles, falling back to index=off.\n"); |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1376 | } |
| 1377 | |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 1378 | /* Check if upper fs has 32bit inode numbers */ |
| 1379 | if (fh_type != FILEID_INO32_GEN) |
Amir Goldstein | 0f831ec | 2019-11-16 18:14:41 +0200 | [diff] [blame] | 1380 | ofs->xino_mode = -1; |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 1381 | |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 1382 | /* NFS export of r/w mount depends on index */ |
| 1383 | if (ofs->config.nfs_export && !ofs->config.index) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1384 | pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n"); |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 1385 | ofs->config.nfs_export = false; |
| 1386 | } |
Amir Goldstein | 2ba9d57 | 2018-01-03 18:54:41 +0200 | [diff] [blame] | 1387 | out: |
| 1388 | mnt_drop_write(mnt); |
| 1389 | return err; |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1392 | static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs, |
| 1393 | struct path *upperpath) |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1394 | { |
| 1395 | int err; |
Miklos Szeredi | bca44b5 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1396 | struct path workpath = { }; |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1397 | |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1398 | err = ovl_mount_dir(ofs->config.workdir, &workpath); |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1399 | if (err) |
| 1400 | goto out; |
| 1401 | |
| 1402 | err = -EINVAL; |
Miklos Szeredi | bca44b5 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1403 | if (upperpath->mnt != workpath.mnt) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1404 | pr_err("workdir and upperdir must reside under the same mount\n"); |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1405 | goto out; |
| 1406 | } |
Miklos Szeredi | bca44b5 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1407 | if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1408 | pr_err("workdir and upperdir must be separate subtrees\n"); |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1409 | goto out; |
| 1410 | } |
| 1411 | |
Miklos Szeredi | 8c25741 | 2018-09-10 11:43:29 +0200 | [diff] [blame] | 1412 | ofs->workbasedir = dget(workpath.dentry); |
| 1413 | |
Miklos Szeredi | 8c25741 | 2018-09-10 11:43:29 +0200 | [diff] [blame] | 1414 | if (ovl_inuse_trylock(ofs->workbasedir)) { |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1415 | ofs->workdir_locked = true; |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1416 | } else { |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1417 | err = ovl_report_in_use(ofs, "workdir"); |
| 1418 | if (err) |
| 1419 | goto out; |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1420 | } |
| 1421 | |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1422 | err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap, |
| 1423 | "workdir"); |
| 1424 | if (err) |
| 1425 | goto out; |
| 1426 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1427 | err = ovl_make_workdir(sb, ofs, &workpath); |
Miklos Szeredi | bca44b5 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1428 | |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1429 | out: |
Miklos Szeredi | bca44b5 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1430 | path_put(&workpath); |
| 1431 | |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1432 | return err; |
| 1433 | } |
| 1434 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1435 | static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs, |
| 1436 | struct ovl_entry *oe, struct path *upperpath) |
Miklos Szeredi | f7e3a7d | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1437 | { |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1438 | struct vfsmount *mnt = ovl_upper_mnt(ofs); |
Amir Goldstein | 235ce9e | 2020-08-30 23:28:03 +0300 | [diff] [blame] | 1439 | struct dentry *indexdir; |
Miklos Szeredi | f7e3a7d | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1440 | int err; |
| 1441 | |
Amir Goldstein | 2ba9d57 | 2018-01-03 18:54:41 +0200 | [diff] [blame] | 1442 | err = mnt_want_write(mnt); |
| 1443 | if (err) |
| 1444 | return err; |
| 1445 | |
Miklos Szeredi | f7e3a7d | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1446 | /* Verify lower root is upper root origin */ |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 1447 | err = ovl_verify_origin(ofs, upperpath->dentry, |
| 1448 | oe->lowerstack[0].dentry, true); |
Miklos Szeredi | f7e3a7d | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1449 | if (err) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1450 | pr_err("failed to verify upper root origin\n"); |
Miklos Szeredi | f7e3a7d | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1451 | goto out; |
| 1452 | } |
| 1453 | |
Amir Goldstein | 470c156 | 2020-07-13 17:19:43 +0300 | [diff] [blame] | 1454 | /* index dir will act also as workdir */ |
| 1455 | iput(ofs->workdir_trap); |
| 1456 | ofs->workdir_trap = NULL; |
| 1457 | dput(ofs->workdir); |
| 1458 | ofs->workdir = NULL; |
Amir Goldstein | 235ce9e | 2020-08-30 23:28:03 +0300 | [diff] [blame] | 1459 | indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true); |
| 1460 | if (IS_ERR(indexdir)) { |
| 1461 | err = PTR_ERR(indexdir); |
| 1462 | } else if (indexdir) { |
| 1463 | ofs->indexdir = indexdir; |
| 1464 | ofs->workdir = dget(indexdir); |
Amir Goldstein | 2039636 | 2020-06-21 09:37:59 +0300 | [diff] [blame] | 1465 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1466 | err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap, |
| 1467 | "indexdir"); |
| 1468 | if (err) |
| 1469 | goto out; |
| 1470 | |
Amir Goldstein | ad1d615 | 2018-01-11 10:47:03 +0200 | [diff] [blame] | 1471 | /* |
| 1472 | * Verify upper root is exclusively associated with index dir. |
| 1473 | * Older kernels stored upper fh in "trusted.overlay.origin" |
| 1474 | * xattr. If that xattr exists, verify that it is a match to |
| 1475 | * upper dir file handle. In any case, verify or set xattr |
| 1476 | * "trusted.overlay.upper" to indicate that index may have |
| 1477 | * directory entries. |
| 1478 | */ |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 1479 | if (ovl_check_origin_xattr(ofs, ofs->indexdir)) { |
| 1480 | err = ovl_verify_set_fh(ofs, ofs->indexdir, |
| 1481 | OVL_XATTR_ORIGIN, |
Amir Goldstein | ad1d615 | 2018-01-11 10:47:03 +0200 | [diff] [blame] | 1482 | upperpath->dentry, true, false); |
| 1483 | if (err) |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1484 | pr_err("failed to verify index dir 'origin' xattr\n"); |
Amir Goldstein | ad1d615 | 2018-01-11 10:47:03 +0200 | [diff] [blame] | 1485 | } |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 1486 | err = ovl_verify_upper(ofs, ofs->indexdir, upperpath->dentry, |
| 1487 | true); |
Miklos Szeredi | f7e3a7d | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1488 | if (err) |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1489 | pr_err("failed to verify index dir 'upper' xattr\n"); |
Miklos Szeredi | f7e3a7d | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1490 | |
| 1491 | /* Cleanup bad/stale/orphan index entries */ |
| 1492 | if (!err) |
Amir Goldstein | 1eff1a1 | 2017-12-12 22:40:46 +0200 | [diff] [blame] | 1493 | err = ovl_indexdir_cleanup(ofs); |
Miklos Szeredi | f7e3a7d | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1494 | } |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1495 | if (err || !ofs->indexdir) |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1496 | pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n"); |
Miklos Szeredi | f7e3a7d | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1497 | |
| 1498 | out: |
Amir Goldstein | 2ba9d57 | 2018-01-03 18:54:41 +0200 | [diff] [blame] | 1499 | mnt_drop_write(mnt); |
Miklos Szeredi | f7e3a7d | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1500 | return err; |
| 1501 | } |
| 1502 | |
Amir Goldstein | 9df085f | 2018-09-03 09:12:09 +0300 | [diff] [blame] | 1503 | static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid) |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1504 | { |
| 1505 | unsigned int i; |
Amir Goldstein | 9df085f | 2018-09-03 09:12:09 +0300 | [diff] [blame] | 1506 | |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1507 | if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs)) |
Amir Goldstein | 9df085f | 2018-09-03 09:12:09 +0300 | [diff] [blame] | 1508 | return true; |
| 1509 | |
Amir Goldstein | a888db3 | 2020-07-08 16:16:13 +0300 | [diff] [blame] | 1510 | /* |
| 1511 | * We allow using single lower with null uuid for index and nfs_export |
| 1512 | * for example to support those features with single lower squashfs. |
| 1513 | * To avoid regressions in setups of overlay with re-formatted lower |
| 1514 | * squashfs, do not allow decoding origin with lower null uuid unless |
| 1515 | * user opted-in to one of the new features that require following the |
| 1516 | * lower inode of non-dir upper. |
| 1517 | */ |
| 1518 | if (!ofs->config.index && !ofs->config.metacopy && !ofs->config.xino && |
| 1519 | uuid_is_null(uuid)) |
| 1520 | return false; |
| 1521 | |
Amir Goldstein | 1b81ddd | 2019-11-16 18:52:20 +0200 | [diff] [blame] | 1522 | for (i = 0; i < ofs->numfs; i++) { |
Amir Goldstein | 9df085f | 2018-09-03 09:12:09 +0300 | [diff] [blame] | 1523 | /* |
| 1524 | * We use uuid to associate an overlay lower file handle with a |
| 1525 | * lower layer, so we can accept lower fs with null uuid as long |
| 1526 | * as all lower layers with null uuid are on the same fs. |
Amir Goldstein | 7e63c87 | 2019-11-14 22:28:41 +0200 | [diff] [blame] | 1527 | * if we detect multiple lower fs with the same uuid, we |
| 1528 | * disable lower file handle decoding on all of them. |
Amir Goldstein | 9df085f | 2018-09-03 09:12:09 +0300 | [diff] [blame] | 1529 | */ |
Amir Goldstein | 1b81ddd | 2019-11-16 18:52:20 +0200 | [diff] [blame] | 1530 | if (ofs->fs[i].is_lower && |
| 1531 | uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) { |
Amir Goldstein | 07f1e59 | 2020-01-14 21:59:22 +0200 | [diff] [blame] | 1532 | ofs->fs[i].bad_uuid = true; |
Amir Goldstein | 9df085f | 2018-09-03 09:12:09 +0300 | [diff] [blame] | 1533 | return false; |
Amir Goldstein | 7e63c87 | 2019-11-14 22:28:41 +0200 | [diff] [blame] | 1534 | } |
Amir Goldstein | 9df085f | 2018-09-03 09:12:09 +0300 | [diff] [blame] | 1535 | } |
| 1536 | return true; |
| 1537 | } |
| 1538 | |
| 1539 | /* Get a unique fsid for the layer */ |
| 1540 | static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path) |
| 1541 | { |
| 1542 | struct super_block *sb = path->mnt->mnt_sb; |
| 1543 | unsigned int i; |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1544 | dev_t dev; |
| 1545 | int err; |
Amir Goldstein | 7e63c87 | 2019-11-14 22:28:41 +0200 | [diff] [blame] | 1546 | bool bad_uuid = false; |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1547 | |
Amir Goldstein | 07f1e59 | 2020-01-14 21:59:22 +0200 | [diff] [blame] | 1548 | for (i = 0; i < ofs->numfs; i++) { |
| 1549 | if (ofs->fs[i].sb == sb) |
| 1550 | return i; |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1551 | } |
| 1552 | |
Amir Goldstein | 9df085f | 2018-09-03 09:12:09 +0300 | [diff] [blame] | 1553 | if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) { |
Amir Goldstein | 7e63c87 | 2019-11-14 22:28:41 +0200 | [diff] [blame] | 1554 | bad_uuid = true; |
| 1555 | if (ofs->config.index || ofs->config.nfs_export) { |
| 1556 | ofs->config.index = false; |
| 1557 | ofs->config.nfs_export = false; |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1558 | pr_warn("%s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n", |
Amir Goldstein | 7e63c87 | 2019-11-14 22:28:41 +0200 | [diff] [blame] | 1559 | uuid_is_null(&sb->s_uuid) ? "null" : |
| 1560 | "conflicting", |
| 1561 | path->dentry); |
| 1562 | } |
Amir Goldstein | 9df085f | 2018-09-03 09:12:09 +0300 | [diff] [blame] | 1563 | } |
| 1564 | |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1565 | err = get_anon_bdev(&dev); |
| 1566 | if (err) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1567 | pr_err("failed to get anonymous bdev for lowerpath\n"); |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1568 | return err; |
| 1569 | } |
| 1570 | |
Amir Goldstein | 07f1e59 | 2020-01-14 21:59:22 +0200 | [diff] [blame] | 1571 | ofs->fs[ofs->numfs].sb = sb; |
| 1572 | ofs->fs[ofs->numfs].pseudo_dev = dev; |
| 1573 | ofs->fs[ofs->numfs].bad_uuid = bad_uuid; |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1574 | |
Amir Goldstein | 07f1e59 | 2020-01-14 21:59:22 +0200 | [diff] [blame] | 1575 | return ofs->numfs++; |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1576 | } |
| 1577 | |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 1578 | static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs, |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1579 | struct path *stack, unsigned int numlower, |
| 1580 | struct ovl_layer *layers) |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1581 | { |
| 1582 | int err; |
| 1583 | unsigned int i; |
| 1584 | |
| 1585 | err = -ENOMEM; |
Amir Goldstein | 07f1e59 | 2020-01-14 21:59:22 +0200 | [diff] [blame] | 1586 | ofs->fs = kcalloc(numlower + 1, sizeof(struct ovl_sb), GFP_KERNEL); |
| 1587 | if (ofs->fs == NULL) |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1588 | goto out; |
| 1589 | |
Amir Goldstein | 07f1e59 | 2020-01-14 21:59:22 +0200 | [diff] [blame] | 1590 | /* idx/fsid 0 are reserved for upper fs even with lower only overlay */ |
| 1591 | ofs->numfs++; |
| 1592 | |
Amir Goldstein | 07f1e59 | 2020-01-14 21:59:22 +0200 | [diff] [blame] | 1593 | /* |
Amir Goldstein | b7bf990 | 2020-01-14 22:17:25 +0200 | [diff] [blame] | 1594 | * All lower layers that share the same fs as upper layer, use the same |
| 1595 | * pseudo_dev as upper layer. Allocate fs[0].pseudo_dev even for lower |
| 1596 | * only overlay to simplify ovl_fs_free(). |
Amir Goldstein | 1b81ddd | 2019-11-16 18:52:20 +0200 | [diff] [blame] | 1597 | * is_lower will be set if upper fs is shared with a lower layer. |
Amir Goldstein | 07f1e59 | 2020-01-14 21:59:22 +0200 | [diff] [blame] | 1598 | */ |
Amir Goldstein | b7bf990 | 2020-01-14 22:17:25 +0200 | [diff] [blame] | 1599 | err = get_anon_bdev(&ofs->fs[0].pseudo_dev); |
| 1600 | if (err) { |
| 1601 | pr_err("failed to get anonymous bdev for upper fs\n"); |
| 1602 | goto out; |
| 1603 | } |
| 1604 | |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1605 | if (ovl_upper_mnt(ofs)) { |
| 1606 | ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb; |
Amir Goldstein | 1b81ddd | 2019-11-16 18:52:20 +0200 | [diff] [blame] | 1607 | ofs->fs[0].is_lower = false; |
Amir Goldstein | 07f1e59 | 2020-01-14 21:59:22 +0200 | [diff] [blame] | 1608 | } |
| 1609 | |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1610 | for (i = 0; i < numlower; i++) { |
| 1611 | struct vfsmount *mnt; |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1612 | struct inode *trap; |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1613 | int fsid; |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1614 | |
Amir Goldstein | 9df085f | 2018-09-03 09:12:09 +0300 | [diff] [blame] | 1615 | err = fsid = ovl_get_fsid(ofs, &stack[i]); |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1616 | if (err < 0) |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1617 | goto out; |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1618 | |
youngjun | 24f1400 | 2020-06-16 17:30:43 +0900 | [diff] [blame] | 1619 | /* |
| 1620 | * Check if lower root conflicts with this overlay layers before |
| 1621 | * checking if it is in-use as upperdir/workdir of "another" |
| 1622 | * mount, because we do not bother to check in ovl_is_inuse() if |
| 1623 | * the upperdir/workdir is in fact in-use by our |
| 1624 | * upperdir/workdir. |
| 1625 | */ |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1626 | err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir"); |
| 1627 | if (err) |
| 1628 | goto out; |
| 1629 | |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1630 | if (ovl_is_inuse(stack[i].dentry)) { |
| 1631 | err = ovl_report_in_use(ofs, "lowerdir"); |
youngjun | 24f1400 | 2020-06-16 17:30:43 +0900 | [diff] [blame] | 1632 | if (err) { |
| 1633 | iput(trap); |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1634 | goto out; |
youngjun | 24f1400 | 2020-06-16 17:30:43 +0900 | [diff] [blame] | 1635 | } |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1636 | } |
| 1637 | |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1638 | mnt = clone_private_mount(&stack[i]); |
| 1639 | err = PTR_ERR(mnt); |
| 1640 | if (IS_ERR(mnt)) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1641 | pr_err("failed to clone lowerpath\n"); |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1642 | iput(trap); |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1643 | goto out; |
| 1644 | } |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame] | 1645 | |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1646 | /* |
| 1647 | * Make lower layers R/O. That way fchmod/fchown on lower file |
| 1648 | * will fail instead of modifying lower fs. |
| 1649 | */ |
| 1650 | mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; |
| 1651 | |
Miklos Szeredi | 1346416 | 2020-01-24 09:46:45 +0100 | [diff] [blame] | 1652 | layers[ofs->numlayer].trap = trap; |
| 1653 | layers[ofs->numlayer].mnt = mnt; |
| 1654 | layers[ofs->numlayer].idx = ofs->numlayer; |
| 1655 | layers[ofs->numlayer].fsid = fsid; |
| 1656 | layers[ofs->numlayer].fs = &ofs->fs[fsid]; |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 1657 | ofs->numlayer++; |
Amir Goldstein | 1b81ddd | 2019-11-16 18:52:20 +0200 | [diff] [blame] | 1658 | ofs->fs[fsid].is_lower = true; |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1659 | } |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 1660 | |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 1661 | /* |
| 1662 | * When all layers on same fs, overlay can use real inode numbers. |
Amir Goldstein | 926e94d | 2020-02-21 16:34:45 +0200 | [diff] [blame] | 1663 | * With mount option "xino=<on|auto>", mounter declares that there are |
| 1664 | * enough free high bits in underlying fs to hold the unique fsid. |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 1665 | * If overlayfs does encounter underlying inodes using the high xino |
| 1666 | * bits reserved for fsid, it emits a warning and uses the original |
Amir Goldstein | dfe51d4 | 2020-02-21 16:34:44 +0200 | [diff] [blame] | 1667 | * inode number or a non persistent inode number allocated from a |
| 1668 | * dedicated range. |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 1669 | */ |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1670 | if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) { |
Amir Goldstein | 0f831ec | 2019-11-16 18:14:41 +0200 | [diff] [blame] | 1671 | if (ofs->config.xino == OVL_XINO_ON) |
| 1672 | pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n"); |
| 1673 | ofs->xino_mode = 0; |
Amir Goldstein | 53afcd3 | 2020-02-21 16:34:42 +0200 | [diff] [blame] | 1674 | } else if (ofs->config.xino == OVL_XINO_OFF) { |
| 1675 | ofs->xino_mode = -1; |
Amir Goldstein | 926e94d | 2020-02-21 16:34:45 +0200 | [diff] [blame] | 1676 | } else if (ofs->xino_mode < 0) { |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 1677 | /* |
Amir Goldstein | 07f1e59 | 2020-01-14 21:59:22 +0200 | [diff] [blame] | 1678 | * This is a roundup of number of bits needed for encoding |
Amir Goldstein | dfe51d4 | 2020-02-21 16:34:44 +0200 | [diff] [blame] | 1679 | * fsid, where fsid 0 is reserved for upper fs (even with |
| 1680 | * lower only overlay) +1 extra bit is reserved for the non |
| 1681 | * persistent inode number range that is used for resolving |
| 1682 | * xino lower bits overflow. |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 1683 | */ |
Amir Goldstein | dfe51d4 | 2020-02-21 16:34:44 +0200 | [diff] [blame] | 1684 | BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30); |
| 1685 | ofs->xino_mode = ilog2(ofs->numfs - 1) + 2; |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 1686 | } |
| 1687 | |
Amir Goldstein | 0f831ec | 2019-11-16 18:14:41 +0200 | [diff] [blame] | 1688 | if (ofs->xino_mode > 0) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1689 | pr_info("\"xino\" feature enabled using %d upper inode bits.\n", |
Amir Goldstein | 0f831ec | 2019-11-16 18:14:41 +0200 | [diff] [blame] | 1690 | ofs->xino_mode); |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 1691 | } |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 1692 | |
Miklos Szeredi | 520d7c8 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1693 | err = 0; |
| 1694 | out: |
| 1695 | return err; |
| 1696 | } |
| 1697 | |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1698 | static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb, |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1699 | const char *lower, unsigned int numlower, |
| 1700 | struct ovl_fs *ofs, struct ovl_layer *layers) |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1701 | { |
| 1702 | int err; |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1703 | struct path *stack = NULL; |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1704 | unsigned int i; |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1705 | struct ovl_entry *oe; |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1706 | |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1707 | if (!ofs->config.upperdir && numlower == 1) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1708 | pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n"); |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1709 | return ERR_PTR(-EINVAL); |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1710 | } |
| 1711 | |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1712 | stack = kcalloc(numlower, sizeof(struct path), GFP_KERNEL); |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1713 | if (!stack) |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1714 | return ERR_PTR(-ENOMEM); |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1715 | |
| 1716 | err = -EINVAL; |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1717 | for (i = 0; i < numlower; i++) { |
| 1718 | err = ovl_lower_dir(lower, &stack[i], ofs, &sb->s_stack_depth); |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1719 | if (err) |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1720 | goto out_err; |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1721 | |
| 1722 | lower = strchr(lower, '\0') + 1; |
| 1723 | } |
| 1724 | |
| 1725 | err = -EINVAL; |
| 1726 | sb->s_stack_depth++; |
| 1727 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1728 | pr_err("maximum fs stacking depth exceeded\n"); |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1729 | goto out_err; |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1730 | } |
| 1731 | |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1732 | err = ovl_get_layers(sb, ofs, stack, numlower, layers); |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1733 | if (err) |
| 1734 | goto out_err; |
| 1735 | |
| 1736 | err = -ENOMEM; |
| 1737 | oe = ovl_alloc_entry(numlower); |
| 1738 | if (!oe) |
| 1739 | goto out_err; |
| 1740 | |
| 1741 | for (i = 0; i < numlower; i++) { |
| 1742 | oe->lowerstack[i].dentry = dget(stack[i].dentry); |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 1743 | oe->lowerstack[i].layer = &ofs->layers[i+1]; |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1744 | } |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1745 | |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1746 | out: |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1747 | for (i = 0; i < numlower; i++) |
| 1748 | path_put(&stack[i]); |
| 1749 | kfree(stack); |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1750 | |
| 1751 | return oe; |
| 1752 | |
| 1753 | out_err: |
| 1754 | oe = ERR_PTR(err); |
Miklos Szeredi | 53dbb0b | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1755 | goto out; |
| 1756 | } |
| 1757 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1758 | /* |
| 1759 | * Check if this layer root is a descendant of: |
| 1760 | * - another layer of this overlayfs instance |
| 1761 | * - upper/work dir of any overlayfs instance |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1762 | */ |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1763 | static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs, |
| 1764 | struct dentry *dentry, const char *name) |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1765 | { |
Miklos Szeredi | 9179c21d | 2019-06-18 15:06:16 +0200 | [diff] [blame] | 1766 | struct dentry *next = dentry, *parent; |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1767 | int err = 0; |
| 1768 | |
Miklos Szeredi | 9179c21d | 2019-06-18 15:06:16 +0200 | [diff] [blame] | 1769 | if (!dentry) |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1770 | return 0; |
| 1771 | |
Miklos Szeredi | 9179c21d | 2019-06-18 15:06:16 +0200 | [diff] [blame] | 1772 | parent = dget_parent(next); |
| 1773 | |
| 1774 | /* Walk back ancestors to root (inclusive) looking for traps */ |
| 1775 | while (!err && parent != next) { |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1776 | if (ovl_lookup_trap_inode(sb, parent)) { |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1777 | err = -ELOOP; |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1778 | pr_err("overlapping %s path\n", name); |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1779 | } else if (ovl_is_inuse(parent)) { |
| 1780 | err = ovl_report_in_use(ofs, name); |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1781 | } |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1782 | next = parent; |
Miklos Szeredi | 9179c21d | 2019-06-18 15:06:16 +0200 | [diff] [blame] | 1783 | parent = dget_parent(next); |
| 1784 | dput(next); |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1785 | } |
| 1786 | |
Miklos Szeredi | 9179c21d | 2019-06-18 15:06:16 +0200 | [diff] [blame] | 1787 | dput(parent); |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1788 | |
| 1789 | return err; |
| 1790 | } |
| 1791 | |
| 1792 | /* |
| 1793 | * Check if any of the layers or work dirs overlap. |
| 1794 | */ |
| 1795 | static int ovl_check_overlapping_layers(struct super_block *sb, |
| 1796 | struct ovl_fs *ofs) |
| 1797 | { |
| 1798 | int i, err; |
| 1799 | |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1800 | if (ovl_upper_mnt(ofs)) { |
| 1801 | err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root, |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1802 | "upperdir"); |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1803 | if (err) |
| 1804 | return err; |
| 1805 | |
| 1806 | /* |
| 1807 | * Checking workbasedir avoids hitting ovl_is_inuse(parent) of |
| 1808 | * this instance and covers overlapping work and index dirs, |
| 1809 | * unless work or index dir have been moved since created inside |
| 1810 | * workbasedir. In that case, we already have their traps in |
| 1811 | * inode cache and we will catch that case on lookup. |
| 1812 | */ |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1813 | err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir"); |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1814 | if (err) |
| 1815 | return err; |
| 1816 | } |
| 1817 | |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 1818 | for (i = 1; i < ofs->numlayer; i++) { |
Amir Goldstein | 0be0bfd | 2019-07-12 15:24:34 +0300 | [diff] [blame] | 1819 | err = ovl_check_layer(sb, ofs, |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 1820 | ofs->layers[i].mnt->mnt_root, |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1821 | "lowerdir"); |
| 1822 | if (err) |
| 1823 | return err; |
| 1824 | } |
| 1825 | |
| 1826 | return 0; |
| 1827 | } |
| 1828 | |
Amir Goldstein | 2effc5c | 2019-11-19 17:49:17 +0200 | [diff] [blame] | 1829 | static struct dentry *ovl_get_root(struct super_block *sb, |
| 1830 | struct dentry *upperdentry, |
| 1831 | struct ovl_entry *oe) |
| 1832 | { |
| 1833 | struct dentry *root; |
Amir Goldstein | 62c832e | 2019-11-19 15:31:46 +0200 | [diff] [blame] | 1834 | struct ovl_path *lowerpath = &oe->lowerstack[0]; |
| 1835 | unsigned long ino = d_inode(lowerpath->dentry)->i_ino; |
| 1836 | int fsid = lowerpath->layer->fsid; |
| 1837 | struct ovl_inode_params oip = { |
| 1838 | .upperdentry = upperdentry, |
| 1839 | .lowerpath = lowerpath, |
| 1840 | }; |
Amir Goldstein | 2effc5c | 2019-11-19 17:49:17 +0200 | [diff] [blame] | 1841 | |
| 1842 | root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); |
| 1843 | if (!root) |
| 1844 | return NULL; |
| 1845 | |
| 1846 | root->d_fsdata = oe; |
| 1847 | |
| 1848 | if (upperdentry) { |
Amir Goldstein | 62c832e | 2019-11-19 15:31:46 +0200 | [diff] [blame] | 1849 | /* Root inode uses upper st_ino/i_ino */ |
| 1850 | ino = d_inode(upperdentry)->i_ino; |
| 1851 | fsid = 0; |
Amir Goldstein | 2effc5c | 2019-11-19 17:49:17 +0200 | [diff] [blame] | 1852 | ovl_dentry_set_upper_alias(root); |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame] | 1853 | if (ovl_is_impuredir(sb, upperdentry)) |
Amir Goldstein | 2effc5c | 2019-11-19 17:49:17 +0200 | [diff] [blame] | 1854 | ovl_set_flag(OVL_IMPURE, d_inode(root)); |
| 1855 | } |
| 1856 | |
| 1857 | /* Root is always merge -> can have whiteouts */ |
| 1858 | ovl_set_flag(OVL_WHITEOUTS, d_inode(root)); |
| 1859 | ovl_dentry_set_flag(OVL_E_CONNECTED, root); |
| 1860 | ovl_set_upperdata(d_inode(root)); |
Amir Goldstein | 62c832e | 2019-11-19 15:31:46 +0200 | [diff] [blame] | 1861 | ovl_inode_init(d_inode(root), &oip, ino, fsid); |
Miklos Szeredi | f428884 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 1862 | ovl_dentry_update_reval(root, upperdentry, DCACHE_OP_WEAK_REVALIDATE); |
Amir Goldstein | 2effc5c | 2019-11-19 17:49:17 +0200 | [diff] [blame] | 1863 | |
| 1864 | return root; |
| 1865 | } |
| 1866 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1867 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
| 1868 | { |
Kees Cook | 33006cd | 2017-03-29 14:02:19 -0700 | [diff] [blame] | 1869 | struct path upperpath = { }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1870 | struct dentry *root_dentry; |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1871 | struct ovl_entry *oe; |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1872 | struct ovl_fs *ofs; |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1873 | struct ovl_layer *layers; |
Konstantin Khlebnikov | 51f8f3c | 2017-01-10 21:30:21 +0300 | [diff] [blame] | 1874 | struct cred *cred; |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1875 | char *splitlower = NULL; |
| 1876 | unsigned int numlower; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1877 | int err; |
| 1878 | |
Miklos Szeredi | f428884 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 1879 | sb->s_d_op = &ovl_dentry_operations; |
| 1880 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1881 | err = -ENOMEM; |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1882 | ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); |
| 1883 | if (!ofs) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1884 | goto out; |
| 1885 | |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1886 | ofs->creator_cred = cred = prepare_creds(); |
Miklos Szeredi | c6fe625 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1887 | if (!cred) |
| 1888 | goto out_err; |
| 1889 | |
Chengguang Xu | c21c839 | 2020-04-24 10:55:17 +0800 | [diff] [blame] | 1890 | /* Is there a reason anyone would want not to share whiteouts? */ |
| 1891 | ofs->share_whiteout = true; |
| 1892 | |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1893 | ofs->config.index = ovl_index_def; |
Pavel Tikhomirov | 5830fb6 | 2020-10-13 17:59:54 +0300 | [diff] [blame^] | 1894 | ofs->config.uuid = true; |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 1895 | ofs->config.nfs_export = ovl_nfs_export_def; |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 1896 | ofs->config.xino = ovl_xino_def(); |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 1897 | ofs->config.metacopy = ovl_metacopy_def; |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1898 | err = ovl_parse_opt((char *) data, &ofs->config); |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1899 | if (err) |
Miklos Szeredi | a9075cd | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1900 | goto out_err; |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1901 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1902 | err = -EINVAL; |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1903 | if (!ofs->config.lowerdir) { |
Konstantin Khlebnikov | 07f2af7 | 2015-06-29 20:18:56 +0300 | [diff] [blame] | 1904 | if (!silent) |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1905 | pr_err("missing 'lowerdir'\n"); |
Miklos Szeredi | a9075cd | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1906 | goto out_err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1907 | } |
| 1908 | |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1909 | err = -ENOMEM; |
| 1910 | splitlower = kstrdup(ofs->config.lowerdir, GFP_KERNEL); |
| 1911 | if (!splitlower) |
| 1912 | goto out_err; |
| 1913 | |
| 1914 | numlower = ovl_split_lowerdirs(splitlower); |
| 1915 | if (numlower > OVL_MAX_STACK) { |
| 1916 | pr_err("too many lower directories, limit is %d\n", |
| 1917 | OVL_MAX_STACK); |
| 1918 | goto out_err; |
| 1919 | } |
| 1920 | |
| 1921 | layers = kcalloc(numlower + 1, sizeof(struct ovl_layer), GFP_KERNEL); |
| 1922 | if (!layers) |
| 1923 | goto out_err; |
| 1924 | |
| 1925 | ofs->layers = layers; |
| 1926 | /* Layer 0 is reserved for upper even if there's no upper */ |
| 1927 | ofs->numlayer = 1; |
| 1928 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1929 | sb->s_stack_depth = 0; |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 1930 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Amir Goldstein | 4d314f7 | 2020-02-21 16:34:43 +0200 | [diff] [blame] | 1931 | atomic_long_set(&ofs->last_ino, 1); |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 1932 | /* Assume underlaying fs uses 32bit inodes unless proven otherwise */ |
Amir Goldstein | 53afcd3 | 2020-02-21 16:34:42 +0200 | [diff] [blame] | 1933 | if (ofs->config.xino != OVL_XINO_OFF) { |
Amir Goldstein | 0f831ec | 2019-11-16 18:14:41 +0200 | [diff] [blame] | 1934 | ofs->xino_mode = BITS_PER_LONG - 32; |
Amir Goldstein | 53afcd3 | 2020-02-21 16:34:42 +0200 | [diff] [blame] | 1935 | if (!ofs->xino_mode) { |
| 1936 | pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n"); |
| 1937 | ofs->config.xino = OVL_XINO_OFF; |
| 1938 | } |
| 1939 | } |
Amir Goldstein | 795939a | 2018-03-29 09:08:18 +0300 | [diff] [blame] | 1940 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1941 | /* alloc/destroy_inode needed for setting up traps in inode cache */ |
| 1942 | sb->s_op = &ovl_super_operations; |
| 1943 | |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1944 | if (ofs->config.upperdir) { |
| 1945 | if (!ofs->config.workdir) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1946 | pr_err("missing 'workdir'\n"); |
Miklos Szeredi | a9075cd | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1947 | goto out_err; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1948 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1949 | |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1950 | err = ovl_get_upper(sb, ofs, &layers[0], &upperpath); |
Miklos Szeredi | 21a3b31 | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1951 | if (err) |
Miklos Szeredi | a9075cd | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1952 | goto out_err; |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 1953 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1954 | err = ovl_get_workdir(sb, ofs, &upperpath); |
Miklos Szeredi | 8ed61dc | 2017-11-09 10:23:28 +0100 | [diff] [blame] | 1955 | if (err) |
Miklos Szeredi | a9075cd | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1956 | goto out_err; |
Miklos Szeredi | c6fe625 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1957 | |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1958 | if (!ofs->workdir) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1959 | sb->s_flags |= SB_RDONLY; |
Miklos Szeredi | 6e88256 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1960 | |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1961 | sb->s_stack_depth = ovl_upper_mnt(ofs)->mnt_sb->s_stack_depth; |
| 1962 | sb->s_time_gran = ovl_upper_mnt(ofs)->mnt_sb->s_time_gran; |
Miklos Szeredi | c6fe625 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1963 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1964 | } |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1965 | oe = ovl_get_lowerstack(sb, splitlower, numlower, ofs, layers); |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1966 | err = PTR_ERR(oe); |
| 1967 | if (IS_ERR(oe)) |
Miklos Szeredi | a9075cd | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1968 | goto out_err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1969 | |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 1970 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1971 | if (!ovl_upper_mnt(ofs)) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1972 | sb->s_flags |= SB_RDONLY; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1973 | |
Pavel Tikhomirov | 5830fb6 | 2020-10-13 17:59:54 +0300 | [diff] [blame^] | 1974 | if (!ofs->config.uuid && ofs->numfs > 1) { |
| 1975 | pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=on.\n"); |
| 1976 | ofs->config.uuid = true; |
| 1977 | } |
| 1978 | |
Amir Goldstein | 470c156 | 2020-07-13 17:19:43 +0300 | [diff] [blame] | 1979 | if (!ovl_force_readonly(ofs) && ofs->config.index) { |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1980 | err = ovl_get_indexdir(sb, ofs, oe, &upperpath); |
Amir Goldstein | 54fb347 | 2017-06-21 15:28:38 +0300 | [diff] [blame] | 1981 | if (err) |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1982 | goto out_free_oe; |
Miklos Szeredi | 6e88256 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 1983 | |
Amir Goldstein | 972d009 | 2017-09-19 12:14:18 +0300 | [diff] [blame] | 1984 | /* Force r/o mount with no index dir */ |
Amir Goldstein | 2039636 | 2020-06-21 09:37:59 +0300 | [diff] [blame] | 1985 | if (!ofs->indexdir) |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 1986 | sb->s_flags |= SB_RDONLY; |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 1987 | } |
| 1988 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 1989 | err = ovl_check_overlapping_layers(sb, ofs); |
| 1990 | if (err) |
| 1991 | goto out_free_oe; |
| 1992 | |
Amir Goldstein | 972d009 | 2017-09-19 12:14:18 +0300 | [diff] [blame] | 1993 | /* Show index=off in /proc/mounts for forced r/o mount */ |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 1994 | if (!ofs->indexdir) { |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 1995 | ofs->config.index = false; |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 1996 | if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 1997 | pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n"); |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 1998 | ofs->config.nfs_export = false; |
| 1999 | } |
| 2000 | } |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 2001 | |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 2002 | if (ofs->config.metacopy && ofs->config.nfs_export) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 2003 | pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n"); |
Vivek Goyal | d579104 | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 2004 | ofs->config.nfs_export = false; |
| 2005 | } |
| 2006 | |
Amir Goldstein | 8383f17 | 2017-10-02 11:31:42 +0300 | [diff] [blame] | 2007 | if (ofs->config.nfs_export) |
| 2008 | sb->s_export_op = &ovl_export_operations; |
| 2009 | |
Konstantin Khlebnikov | 51f8f3c | 2017-01-10 21:30:21 +0300 | [diff] [blame] | 2010 | /* Never override disk quota limits or use reserved space */ |
| 2011 | cap_lower(cred->cap_effective, CAP_SYS_RESOURCE); |
| 2012 | |
Vivek Goyal | 655042c | 2016-10-14 03:03:36 +0200 | [diff] [blame] | 2013 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
Vivek Goyal | 655042c | 2016-10-14 03:03:36 +0200 | [diff] [blame] | 2014 | sb->s_xattr = ovl_xattr_handlers; |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 2015 | sb->s_fs_info = ofs; |
Miklos Szeredi | de2a4a5 | 2018-07-18 15:44:43 +0200 | [diff] [blame] | 2016 | sb->s_flags |= SB_POSIXACL; |
Konstantin Khlebnikov | 32b1924b2 | 2020-04-09 11:29:47 +0300 | [diff] [blame] | 2017 | sb->s_iflags |= SB_I_SKIP_SYNC; |
Vivek Goyal | 655042c | 2016-10-14 03:03:36 +0200 | [diff] [blame] | 2018 | |
Miklos Szeredi | c6fe625 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 2019 | err = -ENOMEM; |
Amir Goldstein | 2effc5c | 2019-11-19 17:49:17 +0200 | [diff] [blame] | 2020 | root_dentry = ovl_get_root(sb, upperpath.dentry, oe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2021 | if (!root_dentry) |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 2022 | goto out_free_oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2023 | |
| 2024 | mntput(upperpath.mnt); |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 2025 | kfree(splitlower); |
Miklos Szeredi | ed06e06 | 2015-12-09 16:11:59 +0100 | [diff] [blame] | 2026 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2027 | sb->s_root = root_dentry; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2028 | |
| 2029 | return 0; |
| 2030 | |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 2031 | out_free_oe: |
| 2032 | ovl_entry_stack_free(oe); |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 2033 | kfree(oe); |
Miklos Szeredi | 4155c10 | 2017-11-10 09:39:15 +0100 | [diff] [blame] | 2034 | out_err: |
Miklos Szeredi | b8e42a6 | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 2035 | kfree(splitlower); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2036 | path_put(&upperpath); |
Miklos Szeredi | ad20448 | 2017-11-10 09:39:16 +0100 | [diff] [blame] | 2037 | ovl_free_fs(ofs); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2038 | out: |
| 2039 | return err; |
| 2040 | } |
| 2041 | |
| 2042 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, |
| 2043 | const char *dev_name, void *raw_data) |
| 2044 | { |
| 2045 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); |
| 2046 | } |
| 2047 | |
| 2048 | static struct file_system_type ovl_fs_type = { |
| 2049 | .owner = THIS_MODULE, |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 2050 | .name = "overlay", |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2051 | .mount = ovl_mount, |
| 2052 | .kill_sb = kill_anon_super, |
| 2053 | }; |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 2054 | MODULE_ALIAS_FS("overlay"); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2055 | |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 2056 | static void ovl_inode_init_once(void *foo) |
| 2057 | { |
| 2058 | struct ovl_inode *oi = foo; |
| 2059 | |
| 2060 | inode_init_once(&oi->vfs_inode); |
| 2061 | } |
| 2062 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2063 | static int __init ovl_init(void) |
| 2064 | { |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 2065 | int err; |
| 2066 | |
| 2067 | ovl_inode_cachep = kmem_cache_create("ovl_inode", |
| 2068 | sizeof(struct ovl_inode), 0, |
| 2069 | (SLAB_RECLAIM_ACCOUNT| |
| 2070 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), |
| 2071 | ovl_inode_init_once); |
| 2072 | if (ovl_inode_cachep == NULL) |
| 2073 | return -ENOMEM; |
| 2074 | |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 2075 | err = ovl_aio_request_cache_init(); |
| 2076 | if (!err) { |
| 2077 | err = register_filesystem(&ovl_fs_type); |
| 2078 | if (!err) |
| 2079 | return 0; |
| 2080 | |
| 2081 | ovl_aio_request_cache_destroy(); |
| 2082 | } |
| 2083 | kmem_cache_destroy(ovl_inode_cachep); |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 2084 | |
| 2085 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2086 | } |
| 2087 | |
| 2088 | static void __exit ovl_exit(void) |
| 2089 | { |
| 2090 | unregister_filesystem(&ovl_fs_type); |
Amir Goldstein | 13cf199 | 2017-06-12 09:54:40 +0300 | [diff] [blame] | 2091 | |
| 2092 | /* |
| 2093 | * Make sure all delayed rcu free inodes are flushed before we |
| 2094 | * destroy cache. |
| 2095 | */ |
| 2096 | rcu_barrier(); |
| 2097 | kmem_cache_destroy(ovl_inode_cachep); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 2098 | ovl_aio_request_cache_destroy(); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | module_init(ovl_init); |
| 2102 | module_exit(ovl_exit); |