Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2011 Novell Inc. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/namei.h> |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 12 | #include <linux/pagemap.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 13 | #include <linux/xattr.h> |
| 14 | #include <linux/security.h> |
| 15 | #include <linux/mount.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/parser.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/sched.h> |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 20 | #include <linux/statfs.h> |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 21 | #include <linux/seq_file.h> |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 22 | #include <linux/posix_acl_xattr.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 23 | #include "overlayfs.h" |
| 24 | |
| 25 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); |
| 26 | MODULE_DESCRIPTION("Overlay filesystem"); |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 29 | struct ovl_config { |
| 30 | char *lowerdir; |
| 31 | char *upperdir; |
| 32 | char *workdir; |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 33 | bool default_permissions; |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 36 | /* private information held for overlayfs's superblock */ |
| 37 | struct ovl_fs { |
| 38 | struct vfsmount *upper_mnt; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 39 | unsigned numlower; |
| 40 | struct vfsmount **lower_mnt; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 41 | struct dentry *workdir; |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 42 | long lower_namelen; |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 43 | /* pathnames of lower and upper dirs, for show_options */ |
| 44 | struct ovl_config config; |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 45 | /* creds of process who forced instantiation of super block */ |
| 46 | const struct cred *creator_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | struct ovl_dir_cache; |
| 50 | |
| 51 | /* private information held for every overlayfs dentry */ |
| 52 | struct ovl_entry { |
| 53 | struct dentry *__upperdentry; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 54 | struct ovl_dir_cache *cache; |
| 55 | union { |
| 56 | struct { |
| 57 | u64 version; |
| 58 | bool opaque; |
| 59 | }; |
| 60 | struct rcu_head rcu; |
| 61 | }; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 62 | unsigned numlower; |
| 63 | struct path lowerstack[]; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 64 | }; |
| 65 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 66 | #define OVL_MAX_STACK 500 |
| 67 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 68 | static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe) |
| 69 | { |
| 70 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; |
| 71 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 72 | |
| 73 | enum ovl_path_type ovl_path_type(struct dentry *dentry) |
| 74 | { |
| 75 | struct ovl_entry *oe = dentry->d_fsdata; |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 76 | enum ovl_path_type type = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 77 | |
| 78 | if (oe->__upperdentry) { |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 79 | type = __OVL_PATH_UPPER; |
| 80 | |
Konstantin Khlebnikov | 45d1173 | 2016-01-31 16:17:53 +0300 | [diff] [blame] | 81 | /* |
| 82 | * Non-dir dentry can hold lower dentry from previous |
| 83 | * location. Its purity depends only on opaque flag. |
| 84 | */ |
| 85 | if (oe->numlower && S_ISDIR(dentry->d_inode->i_mode)) |
| 86 | type |= __OVL_PATH_MERGE; |
| 87 | else if (!oe->opaque) |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 88 | type |= __OVL_PATH_PURE; |
Miklos Szeredi | 9d7459d | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 89 | } else { |
| 90 | if (oe->numlower > 1) |
| 91 | type |= __OVL_PATH_MERGE; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 92 | } |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 93 | return type; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) |
| 97 | { |
Miklos Szeredi | 71d5092 | 2014-11-20 16:40:01 +0100 | [diff] [blame] | 98 | return lockless_dereference(oe->__upperdentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | void ovl_path_upper(struct dentry *dentry, struct path *path) |
| 102 | { |
| 103 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 104 | struct ovl_entry *oe = dentry->d_fsdata; |
| 105 | |
| 106 | path->mnt = ofs->upper_mnt; |
| 107 | path->dentry = ovl_upperdentry_dereference(oe); |
| 108 | } |
| 109 | |
| 110 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) |
| 111 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 112 | enum ovl_path_type type = ovl_path_type(dentry); |
| 113 | |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 114 | if (!OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 115 | ovl_path_lower(dentry, path); |
| 116 | else |
| 117 | ovl_path_upper(dentry, path); |
| 118 | |
| 119 | return type; |
| 120 | } |
| 121 | |
| 122 | struct dentry *ovl_dentry_upper(struct dentry *dentry) |
| 123 | { |
| 124 | struct ovl_entry *oe = dentry->d_fsdata; |
| 125 | |
| 126 | return ovl_upperdentry_dereference(oe); |
| 127 | } |
| 128 | |
| 129 | struct dentry *ovl_dentry_lower(struct dentry *dentry) |
| 130 | { |
| 131 | struct ovl_entry *oe = dentry->d_fsdata; |
| 132 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 133 | return __ovl_dentry_lower(oe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | struct dentry *ovl_dentry_real(struct dentry *dentry) |
| 137 | { |
| 138 | struct ovl_entry *oe = dentry->d_fsdata; |
| 139 | struct dentry *realdentry; |
| 140 | |
| 141 | realdentry = ovl_upperdentry_dereference(oe); |
| 142 | if (!realdentry) |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 143 | realdentry = __ovl_dentry_lower(oe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 144 | |
| 145 | return realdentry; |
| 146 | } |
| 147 | |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 148 | static void ovl_inode_init(struct inode *inode, struct inode *realinode, |
| 149 | bool is_upper) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 150 | { |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 151 | WRITE_ONCE(inode->i_private, (unsigned long) realinode | |
| 152 | (is_upper ? OVL_ISUPPER_MASK : 0)); |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 155 | struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode, |
| 156 | bool is_upper) |
| 157 | { |
| 158 | if (is_upper) { |
| 159 | struct ovl_fs *ofs = inode->i_sb->s_fs_info; |
| 160 | |
| 161 | return ofs->upper_mnt; |
| 162 | } else { |
| 163 | return oe->numlower ? oe->lowerstack[0].mnt : NULL; |
| 164 | } |
| 165 | } |
| 166 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 167 | struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) |
| 168 | { |
| 169 | struct ovl_entry *oe = dentry->d_fsdata; |
| 170 | |
| 171 | return oe->cache; |
| 172 | } |
| 173 | |
| 174 | void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) |
| 175 | { |
| 176 | struct ovl_entry *oe = dentry->d_fsdata; |
| 177 | |
| 178 | oe->cache = cache; |
| 179 | } |
| 180 | |
| 181 | void ovl_path_lower(struct dentry *dentry, struct path *path) |
| 182 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 183 | struct ovl_entry *oe = dentry->d_fsdata; |
| 184 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 185 | *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | int ovl_want_write(struct dentry *dentry) |
| 189 | { |
| 190 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 191 | return mnt_want_write(ofs->upper_mnt); |
| 192 | } |
| 193 | |
| 194 | void ovl_drop_write(struct dentry *dentry) |
| 195 | { |
| 196 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 197 | mnt_drop_write(ofs->upper_mnt); |
| 198 | } |
| 199 | |
| 200 | struct dentry *ovl_workdir(struct dentry *dentry) |
| 201 | { |
| 202 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 203 | return ofs->workdir; |
| 204 | } |
| 205 | |
| 206 | bool ovl_dentry_is_opaque(struct dentry *dentry) |
| 207 | { |
| 208 | struct ovl_entry *oe = dentry->d_fsdata; |
| 209 | return oe->opaque; |
| 210 | } |
| 211 | |
| 212 | void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) |
| 213 | { |
| 214 | struct ovl_entry *oe = dentry->d_fsdata; |
| 215 | oe->opaque = opaque; |
| 216 | } |
| 217 | |
| 218 | void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) |
| 219 | { |
| 220 | struct ovl_entry *oe = dentry->d_fsdata; |
| 221 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 222 | WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 223 | WARN_ON(oe->__upperdentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 224 | /* |
| 225 | * Make sure upperdentry is consistent before making it visible to |
| 226 | * ovl_upperdentry_dereference(). |
| 227 | */ |
| 228 | smp_wmb(); |
| 229 | oe->__upperdentry = upperdentry; |
| 230 | } |
| 231 | |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 232 | void ovl_inode_update(struct inode *inode, struct inode *upperinode) |
| 233 | { |
| 234 | WARN_ON(!upperinode); |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 235 | WARN_ON(!inode_unhashed(inode)); |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 236 | WRITE_ONCE(inode->i_private, |
| 237 | (unsigned long) upperinode | OVL_ISUPPER_MASK); |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 238 | if (!S_ISDIR(upperinode->i_mode)) |
| 239 | __insert_inode_hash(inode, (unsigned long) upperinode); |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 242 | void ovl_dentry_version_inc(struct dentry *dentry) |
| 243 | { |
| 244 | struct ovl_entry *oe = dentry->d_fsdata; |
| 245 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 246 | WARN_ON(!inode_is_locked(dentry->d_inode)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 247 | oe->version++; |
| 248 | } |
| 249 | |
| 250 | u64 ovl_dentry_version_get(struct dentry *dentry) |
| 251 | { |
| 252 | struct ovl_entry *oe = dentry->d_fsdata; |
| 253 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 254 | WARN_ON(!inode_is_locked(dentry->d_inode)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 255 | return oe->version; |
| 256 | } |
| 257 | |
| 258 | bool ovl_is_whiteout(struct dentry *dentry) |
| 259 | { |
| 260 | struct inode *inode = dentry->d_inode; |
| 261 | |
| 262 | return inode && IS_WHITEOUT(inode); |
| 263 | } |
| 264 | |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 265 | const struct cred *ovl_override_creds(struct super_block *sb) |
| 266 | { |
| 267 | struct ovl_fs *ofs = sb->s_fs_info; |
| 268 | |
| 269 | return override_creds(ofs->creator_cred); |
| 270 | } |
| 271 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 272 | static bool ovl_is_opaquedir(struct dentry *dentry) |
| 273 | { |
| 274 | int res; |
| 275 | char val; |
| 276 | struct inode *inode = dentry->d_inode; |
| 277 | |
| 278 | if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) |
| 279 | return false; |
| 280 | |
Al Viro | ce23e64 | 2016-04-11 00:48:00 -0400 | [diff] [blame] | 281 | res = inode->i_op->getxattr(dentry, inode, OVL_XATTR_OPAQUE, &val, 1); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 282 | if (res == 1 && val == 'y') |
| 283 | return true; |
| 284 | |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | static void ovl_dentry_release(struct dentry *dentry) |
| 289 | { |
| 290 | struct ovl_entry *oe = dentry->d_fsdata; |
| 291 | |
| 292 | if (oe) { |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 293 | unsigned int i; |
| 294 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 295 | dput(oe->__upperdentry); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 296 | for (i = 0; i < oe->numlower; i++) |
| 297 | dput(oe->lowerstack[i].dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 298 | kfree_rcu(oe, rcu); |
| 299 | } |
| 300 | } |
| 301 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 302 | static struct dentry *ovl_d_real(struct dentry *dentry, |
| 303 | const struct inode *inode, |
| 304 | unsigned int open_flags) |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 305 | { |
| 306 | struct dentry *real; |
| 307 | |
| 308 | if (d_is_dir(dentry)) { |
| 309 | if (!inode || inode == d_inode(dentry)) |
| 310 | return dentry; |
| 311 | goto bug; |
| 312 | } |
| 313 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 314 | if (d_is_negative(dentry)) |
| 315 | return dentry; |
| 316 | |
| 317 | if (open_flags) { |
| 318 | int err = ovl_open_maybe_copy_up(dentry, open_flags); |
| 319 | |
| 320 | if (err) |
| 321 | return ERR_PTR(err); |
| 322 | } |
| 323 | |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 324 | real = ovl_dentry_upper(dentry); |
| 325 | if (real && (!inode || inode == d_inode(real))) |
| 326 | return real; |
| 327 | |
| 328 | real = ovl_dentry_lower(dentry); |
| 329 | if (!real) |
| 330 | goto bug; |
| 331 | |
| 332 | if (!inode || inode == d_inode(real)) |
| 333 | return real; |
| 334 | |
| 335 | /* Handle recursion */ |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 336 | return d_real(real, inode, open_flags); |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 337 | bug: |
Miklos Szeredi | 656189d | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 338 | 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] | 339 | inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0); |
| 340 | return dentry; |
| 341 | } |
| 342 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 343 | static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
| 344 | { |
| 345 | struct ovl_entry *oe = dentry->d_fsdata; |
| 346 | unsigned int i; |
| 347 | int ret = 1; |
| 348 | |
| 349 | for (i = 0; i < oe->numlower; i++) { |
| 350 | struct dentry *d = oe->lowerstack[i].dentry; |
| 351 | |
| 352 | if (d->d_flags & DCACHE_OP_REVALIDATE) { |
| 353 | ret = d->d_op->d_revalidate(d, flags); |
| 354 | if (ret < 0) |
| 355 | return ret; |
| 356 | if (!ret) { |
| 357 | if (!(flags & LOOKUP_RCU)) |
| 358 | d_invalidate(d); |
| 359 | return -ESTALE; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | return 1; |
| 364 | } |
| 365 | |
| 366 | static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) |
| 367 | { |
| 368 | struct ovl_entry *oe = dentry->d_fsdata; |
| 369 | unsigned int i; |
| 370 | int ret = 1; |
| 371 | |
| 372 | for (i = 0; i < oe->numlower; i++) { |
| 373 | struct dentry *d = oe->lowerstack[i].dentry; |
| 374 | |
| 375 | if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { |
| 376 | ret = d->d_op->d_weak_revalidate(d, flags); |
| 377 | if (ret <= 0) |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | return ret; |
| 382 | } |
| 383 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 384 | static const struct dentry_operations ovl_dentry_operations = { |
| 385 | .d_release = ovl_dentry_release, |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 386 | .d_real = ovl_d_real, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 387 | }; |
| 388 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 389 | static const struct dentry_operations ovl_reval_dentry_operations = { |
| 390 | .d_release = ovl_dentry_release, |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 391 | .d_real = ovl_d_real, |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 392 | .d_revalidate = ovl_dentry_revalidate, |
| 393 | .d_weak_revalidate = ovl_dentry_weak_revalidate, |
| 394 | }; |
| 395 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 396 | static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 397 | { |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 398 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); |
| 399 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); |
| 400 | |
| 401 | if (oe) |
| 402 | oe->numlower = numlower; |
| 403 | |
| 404 | return oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 405 | } |
| 406 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 407 | static bool ovl_dentry_remote(struct dentry *dentry) |
| 408 | { |
| 409 | return dentry->d_flags & |
Miklos Szeredi | 76bc8e2 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 410 | (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE | |
| 411 | DCACHE_OP_REAL); |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static bool ovl_dentry_weird(struct dentry *dentry) |
| 415 | { |
| 416 | return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | |
| 417 | DCACHE_MANAGE_TRANSIT | |
| 418 | DCACHE_OP_HASH | |
| 419 | DCACHE_OP_COMPARE); |
| 420 | } |
| 421 | |
Miklos Szeredi | c1b2cc1 | 2016-07-29 12:05:22 +0200 | [diff] [blame] | 422 | static inline struct dentry *ovl_lookup_real(struct super_block *ovl_sb, |
| 423 | struct dentry *dir, |
Al Viro | 29c42e8 | 2016-07-20 22:36:53 -0400 | [diff] [blame] | 424 | const struct qstr *name) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 425 | { |
Miklos Szeredi | c1b2cc1 | 2016-07-29 12:05:22 +0200 | [diff] [blame] | 426 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 427 | struct dentry *dentry; |
| 428 | |
Miklos Szeredi | c1b2cc1 | 2016-07-29 12:05:22 +0200 | [diff] [blame] | 429 | old_cred = ovl_override_creds(ovl_sb); |
| 430 | dentry = lookup_one_len_unlocked(name->name, dir, name->len); |
| 431 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 432 | |
| 433 | if (IS_ERR(dentry)) { |
| 434 | if (PTR_ERR(dentry) == -ENOENT) |
| 435 | dentry = NULL; |
| 436 | } else if (!dentry->d_inode) { |
| 437 | dput(dentry); |
| 438 | dentry = NULL; |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 439 | } else if (ovl_dentry_weird(dentry)) { |
Miklos Szeredi | a6f15d9 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 440 | dput(dentry); |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 441 | /* Don't support traversing automounts and other weirdness */ |
Miklos Szeredi | a6f15d9 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 442 | dentry = ERR_PTR(-EREMOTE); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 443 | } |
| 444 | return dentry; |
| 445 | } |
| 446 | |
Miklos Szeredi | 5ef88da | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 447 | /* |
| 448 | * Returns next layer in stack starting from top. |
| 449 | * Returns -1 if this is the last layer. |
| 450 | */ |
| 451 | int ovl_path_next(int idx, struct dentry *dentry, struct path *path) |
| 452 | { |
| 453 | struct ovl_entry *oe = dentry->d_fsdata; |
| 454 | |
| 455 | BUG_ON(idx < 0); |
| 456 | if (idx == 0) { |
| 457 | ovl_path_upper(dentry, path); |
| 458 | if (path->dentry) |
| 459 | return oe->numlower ? 1 : -1; |
| 460 | idx++; |
| 461 | } |
| 462 | BUG_ON(idx > oe->numlower); |
| 463 | *path = oe->lowerstack[idx - 1]; |
| 464 | |
| 465 | return (idx < oe->numlower) ? idx + 1 : -1; |
| 466 | } |
| 467 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 468 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
| 469 | unsigned int flags) |
| 470 | { |
| 471 | struct ovl_entry *oe; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 472 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
| 473 | struct path *stack = NULL; |
| 474 | struct dentry *upperdir, *upperdentry = NULL; |
| 475 | unsigned int ctr = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 476 | struct inode *inode = NULL; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 477 | bool upperopaque = false; |
| 478 | struct dentry *this, *prev = NULL; |
| 479 | unsigned int i; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 480 | int err; |
| 481 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 482 | upperdir = ovl_upperdentry_dereference(poe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 483 | if (upperdir) { |
Miklos Szeredi | c1b2cc1 | 2016-07-29 12:05:22 +0200 | [diff] [blame] | 484 | this = ovl_lookup_real(dentry->d_sb, upperdir, &dentry->d_name); |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 485 | err = PTR_ERR(this); |
| 486 | if (IS_ERR(this)) |
| 487 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 488 | |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 489 | if (this) { |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 490 | if (unlikely(ovl_dentry_remote(this))) { |
| 491 | dput(this); |
| 492 | err = -EREMOTE; |
| 493 | goto out; |
| 494 | } |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 495 | if (ovl_is_whiteout(this)) { |
| 496 | dput(this); |
| 497 | this = NULL; |
| 498 | upperopaque = true; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 499 | } else if (poe->numlower && ovl_is_opaquedir(this)) { |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 500 | upperopaque = true; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 501 | } |
| 502 | } |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 503 | upperdentry = prev = this; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 506 | if (!upperopaque && poe->numlower) { |
| 507 | err = -ENOMEM; |
| 508 | stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL); |
| 509 | if (!stack) |
| 510 | goto out_put_upper; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 511 | } |
| 512 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 513 | for (i = 0; !upperopaque && i < poe->numlower; i++) { |
| 514 | bool opaque = false; |
| 515 | struct path lowerpath = poe->lowerstack[i]; |
| 516 | |
Miklos Szeredi | c1b2cc1 | 2016-07-29 12:05:22 +0200 | [diff] [blame] | 517 | this = ovl_lookup_real(dentry->d_sb, |
| 518 | lowerpath.dentry, &dentry->d_name); |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 519 | err = PTR_ERR(this); |
Miklos Szeredi | 09e1032 | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 520 | if (IS_ERR(this)) { |
| 521 | /* |
| 522 | * If it's positive, then treat ENAMETOOLONG as ENOENT. |
| 523 | */ |
| 524 | if (err == -ENAMETOOLONG && (upperdentry || ctr)) |
| 525 | continue; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 526 | goto out_put; |
Miklos Szeredi | 09e1032 | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 527 | } |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 528 | if (!this) |
| 529 | continue; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 530 | if (ovl_is_whiteout(this)) { |
| 531 | dput(this); |
| 532 | break; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 533 | } |
| 534 | /* |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 535 | * Only makes sense to check opaque dir if this is not the |
| 536 | * lowermost layer. |
| 537 | */ |
| 538 | if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) |
| 539 | opaque = true; |
hujianyang | a425c03 | 2015-01-06 16:10:01 +0800 | [diff] [blame] | 540 | |
| 541 | if (prev && (!S_ISDIR(prev->d_inode->i_mode) || |
| 542 | !S_ISDIR(this->d_inode->i_mode))) { |
| 543 | /* |
| 544 | * FIXME: check for upper-opaqueness maybe better done |
| 545 | * in remove code. |
| 546 | */ |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 547 | if (prev == upperdentry) |
| 548 | upperopaque = true; |
| 549 | dput(this); |
| 550 | break; |
| 551 | } |
hujianyang | a425c03 | 2015-01-06 16:10:01 +0800 | [diff] [blame] | 552 | /* |
| 553 | * If this is a non-directory then stop here. |
| 554 | */ |
| 555 | if (!S_ISDIR(this->d_inode->i_mode)) |
| 556 | opaque = true; |
| 557 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 558 | stack[ctr].dentry = this; |
| 559 | stack[ctr].mnt = lowerpath.mnt; |
| 560 | ctr++; |
| 561 | prev = this; |
| 562 | if (opaque) |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | oe = ovl_alloc_entry(ctr); |
| 567 | err = -ENOMEM; |
| 568 | if (!oe) |
| 569 | goto out_put; |
| 570 | |
| 571 | if (upperdentry || ctr) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 572 | struct dentry *realdentry; |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 573 | struct inode *realinode; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 574 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 575 | realdentry = upperdentry ? upperdentry : stack[0].dentry; |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 576 | realinode = d_inode(realdentry); |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 577 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 578 | err = -ENOMEM; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 579 | if (upperdentry && !d_is_dir(upperdentry)) { |
| 580 | inode = ovl_get_inode(dentry->d_sb, realinode); |
| 581 | } else { |
| 582 | inode = ovl_new_inode(dentry->d_sb, realinode->i_mode); |
| 583 | if (inode) |
| 584 | ovl_inode_init(inode, realinode, !!upperdentry); |
| 585 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 586 | if (!inode) |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 587 | goto out_free_oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 588 | ovl_copyattr(realdentry->d_inode, inode); |
| 589 | } |
| 590 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 591 | oe->opaque = upperopaque; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 592 | oe->__upperdentry = upperdentry; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 593 | memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); |
| 594 | kfree(stack); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 595 | dentry->d_fsdata = oe; |
| 596 | d_add(dentry, inode); |
| 597 | |
| 598 | return NULL; |
| 599 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 600 | out_free_oe: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 601 | kfree(oe); |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 602 | out_put: |
| 603 | for (i = 0; i < ctr; i++) |
| 604 | dput(stack[i].dentry); |
| 605 | kfree(stack); |
| 606 | out_put_upper: |
| 607 | dput(upperdentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 608 | out: |
| 609 | return ERR_PTR(err); |
| 610 | } |
| 611 | |
| 612 | struct file *ovl_path_open(struct path *path, int flags) |
| 613 | { |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 614 | return dentry_open(path, flags | O_NOATIME, current_cred()); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | static void ovl_put_super(struct super_block *sb) |
| 618 | { |
| 619 | struct ovl_fs *ufs = sb->s_fs_info; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 620 | unsigned i; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 621 | |
| 622 | dput(ufs->workdir); |
| 623 | mntput(ufs->upper_mnt); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 624 | for (i = 0; i < ufs->numlower; i++) |
| 625 | mntput(ufs->lower_mnt[i]); |
Konstantin Khlebnikov | 5ffdbe8 | 2015-08-24 15:57:19 +0300 | [diff] [blame] | 626 | kfree(ufs->lower_mnt); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 627 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 628 | kfree(ufs->config.lowerdir); |
| 629 | kfree(ufs->config.upperdir); |
| 630 | kfree(ufs->config.workdir); |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 631 | put_cred(ufs->creator_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 632 | kfree(ufs); |
| 633 | } |
| 634 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 635 | /** |
| 636 | * ovl_statfs |
| 637 | * @sb: The overlayfs super block |
| 638 | * @buf: The struct kstatfs to fill in with stats |
| 639 | * |
| 640 | * Get the filesystem statistics. As writes always target the upper layer |
Miklos Szeredi | 4ebc581 | 2014-12-13 00:59:46 +0100 | [diff] [blame] | 641 | * filesystem pass the statfs to the upper filesystem (if it exists) |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 642 | */ |
| 643 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 644 | { |
| 645 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 646 | struct dentry *root_dentry = dentry->d_sb->s_root; |
| 647 | struct path path; |
| 648 | int err; |
| 649 | |
Miklos Szeredi | 4ebc581 | 2014-12-13 00:59:46 +0100 | [diff] [blame] | 650 | ovl_path_real(root_dentry, &path); |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 651 | |
| 652 | err = vfs_statfs(&path, buf); |
| 653 | if (!err) { |
| 654 | buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); |
| 655 | buf->f_type = OVERLAYFS_SUPER_MAGIC; |
| 656 | } |
| 657 | |
| 658 | return err; |
| 659 | } |
| 660 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 661 | /** |
| 662 | * ovl_show_options |
| 663 | * |
| 664 | * Prints the mount options for a given superblock. |
| 665 | * Returns zero; does not fail. |
| 666 | */ |
| 667 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) |
| 668 | { |
| 669 | struct super_block *sb = dentry->d_sb; |
| 670 | struct ovl_fs *ufs = sb->s_fs_info; |
| 671 | |
Kees Cook | a068acf | 2015-09-04 15:44:57 -0700 | [diff] [blame] | 672 | seq_show_option(m, "lowerdir", ufs->config.lowerdir); |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 673 | if (ufs->config.upperdir) { |
Kees Cook | a068acf | 2015-09-04 15:44:57 -0700 | [diff] [blame] | 674 | seq_show_option(m, "upperdir", ufs->config.upperdir); |
| 675 | seq_show_option(m, "workdir", ufs->config.workdir); |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 676 | } |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 677 | if (ufs->config.default_permissions) |
| 678 | seq_puts(m, ",default_permissions"); |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 679 | return 0; |
| 680 | } |
| 681 | |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 682 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
| 683 | { |
| 684 | struct ovl_fs *ufs = sb->s_fs_info; |
| 685 | |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 686 | if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir)) |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 687 | return -EROFS; |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 692 | static const struct super_operations ovl_super_operations = { |
| 693 | .put_super = ovl_put_super, |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 694 | .statfs = ovl_statfs, |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 695 | .show_options = ovl_show_options, |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 696 | .remount_fs = ovl_remount, |
Miklos Szeredi | eead4f2 | 2016-07-29 12:05:22 +0200 | [diff] [blame] | 697 | .drop_inode = generic_delete_inode, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 698 | }; |
| 699 | |
| 700 | enum { |
| 701 | OPT_LOWERDIR, |
| 702 | OPT_UPPERDIR, |
| 703 | OPT_WORKDIR, |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 704 | OPT_DEFAULT_PERMISSIONS, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 705 | OPT_ERR, |
| 706 | }; |
| 707 | |
| 708 | static const match_table_t ovl_tokens = { |
| 709 | {OPT_LOWERDIR, "lowerdir=%s"}, |
| 710 | {OPT_UPPERDIR, "upperdir=%s"}, |
| 711 | {OPT_WORKDIR, "workdir=%s"}, |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 712 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 713 | {OPT_ERR, NULL} |
| 714 | }; |
| 715 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 716 | static char *ovl_next_opt(char **s) |
| 717 | { |
| 718 | char *sbegin = *s; |
| 719 | char *p; |
| 720 | |
| 721 | if (sbegin == NULL) |
| 722 | return NULL; |
| 723 | |
| 724 | for (p = sbegin; *p; p++) { |
| 725 | if (*p == '\\') { |
| 726 | p++; |
| 727 | if (!*p) |
| 728 | break; |
| 729 | } else if (*p == ',') { |
| 730 | *p = '\0'; |
| 731 | *s = p + 1; |
| 732 | return sbegin; |
| 733 | } |
| 734 | } |
| 735 | *s = NULL; |
| 736 | return sbegin; |
| 737 | } |
| 738 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 739 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
| 740 | { |
| 741 | char *p; |
| 742 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 743 | while ((p = ovl_next_opt(&opt)) != NULL) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 744 | int token; |
| 745 | substring_t args[MAX_OPT_ARGS]; |
| 746 | |
| 747 | if (!*p) |
| 748 | continue; |
| 749 | |
| 750 | token = match_token(p, ovl_tokens, args); |
| 751 | switch (token) { |
| 752 | case OPT_UPPERDIR: |
| 753 | kfree(config->upperdir); |
| 754 | config->upperdir = match_strdup(&args[0]); |
| 755 | if (!config->upperdir) |
| 756 | return -ENOMEM; |
| 757 | break; |
| 758 | |
| 759 | case OPT_LOWERDIR: |
| 760 | kfree(config->lowerdir); |
| 761 | config->lowerdir = match_strdup(&args[0]); |
| 762 | if (!config->lowerdir) |
| 763 | return -ENOMEM; |
| 764 | break; |
| 765 | |
| 766 | case OPT_WORKDIR: |
| 767 | kfree(config->workdir); |
| 768 | config->workdir = match_strdup(&args[0]); |
| 769 | if (!config->workdir) |
| 770 | return -ENOMEM; |
| 771 | break; |
| 772 | |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 773 | case OPT_DEFAULT_PERMISSIONS: |
| 774 | config->default_permissions = true; |
| 775 | break; |
| 776 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 777 | default: |
hujianyang | bead55e | 2015-01-15 13:17:36 +0800 | [diff] [blame] | 778 | pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 779 | return -EINVAL; |
| 780 | } |
| 781 | } |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 782 | |
| 783 | /* Workdir is useless in non-upper mount */ |
| 784 | if (!config->upperdir && config->workdir) { |
| 785 | pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", |
| 786 | config->workdir); |
| 787 | kfree(config->workdir); |
| 788 | config->workdir = NULL; |
| 789 | } |
| 790 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | #define OVL_WORKDIR_NAME "work" |
| 795 | |
| 796 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, |
| 797 | struct dentry *dentry) |
| 798 | { |
| 799 | struct inode *dir = dentry->d_inode; |
| 800 | struct dentry *work; |
| 801 | int err; |
| 802 | bool retried = false; |
| 803 | |
| 804 | err = mnt_want_write(mnt); |
| 805 | if (err) |
| 806 | return ERR_PTR(err); |
| 807 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 808 | inode_lock_nested(dir, I_MUTEX_PARENT); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 809 | retry: |
| 810 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, |
| 811 | strlen(OVL_WORKDIR_NAME)); |
| 812 | |
| 813 | if (!IS_ERR(work)) { |
| 814 | struct kstat stat = { |
| 815 | .mode = S_IFDIR | 0, |
| 816 | }; |
Miklos Szeredi | c11b9fd | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 817 | struct iattr attr = { |
| 818 | .ia_valid = ATTR_MODE, |
| 819 | .ia_mode = stat.mode, |
| 820 | }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 821 | |
| 822 | if (work->d_inode) { |
| 823 | err = -EEXIST; |
| 824 | if (retried) |
| 825 | goto out_dput; |
| 826 | |
| 827 | retried = true; |
Miklos Szeredi | eea2fb4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 828 | ovl_workdir_cleanup(dir, mnt, work, 0); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 829 | dput(work); |
| 830 | goto retry; |
| 831 | } |
| 832 | |
| 833 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); |
| 834 | if (err) |
| 835 | goto out_dput; |
Miklos Szeredi | c11b9fd | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 836 | |
| 837 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT); |
| 838 | if (err && err != -ENODATA) |
| 839 | goto out_dput; |
| 840 | |
| 841 | err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS); |
| 842 | if (err && err != -ENODATA) |
| 843 | goto out_dput; |
| 844 | |
| 845 | /* Clear any inherited mode bits */ |
| 846 | inode_lock(work->d_inode); |
| 847 | err = notify_change(work, &attr, NULL); |
| 848 | inode_unlock(work->d_inode); |
| 849 | if (err) |
| 850 | goto out_dput; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 851 | } |
| 852 | out_unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 853 | inode_unlock(dir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 854 | mnt_drop_write(mnt); |
| 855 | |
| 856 | return work; |
| 857 | |
| 858 | out_dput: |
| 859 | dput(work); |
| 860 | work = ERR_PTR(err); |
| 861 | goto out_unlock; |
| 862 | } |
| 863 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 864 | static void ovl_unescape(char *s) |
| 865 | { |
| 866 | char *d = s; |
| 867 | |
| 868 | for (;; s++, d++) { |
| 869 | if (*s == '\\') |
| 870 | s++; |
| 871 | *d = *s; |
| 872 | if (!*s) |
| 873 | break; |
| 874 | } |
| 875 | } |
| 876 | |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 877 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
| 878 | { |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 879 | int err = -EINVAL; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 880 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 881 | if (!*name) { |
| 882 | pr_err("overlayfs: empty lowerdir\n"); |
| 883 | goto out; |
| 884 | } |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 885 | err = kern_path(name, LOOKUP_FOLLOW, path); |
| 886 | if (err) { |
| 887 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); |
| 888 | goto out; |
| 889 | } |
| 890 | err = -EINVAL; |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 891 | if (ovl_dentry_weird(path->dentry)) { |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 892 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); |
| 893 | goto out_put; |
| 894 | } |
| 895 | if (!S_ISDIR(path->dentry->d_inode->i_mode)) { |
| 896 | pr_err("overlayfs: '%s' not a directory\n", name); |
| 897 | goto out_put; |
| 898 | } |
| 899 | return 0; |
| 900 | |
| 901 | out_put: |
| 902 | path_put(path); |
| 903 | out: |
| 904 | return err; |
| 905 | } |
| 906 | |
| 907 | static int ovl_mount_dir(const char *name, struct path *path) |
| 908 | { |
| 909 | int err = -ENOMEM; |
| 910 | char *tmp = kstrdup(name, GFP_KERNEL); |
| 911 | |
| 912 | if (tmp) { |
| 913 | ovl_unescape(tmp); |
| 914 | err = ovl_mount_dir_noesc(tmp, path); |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 915 | |
| 916 | if (!err) |
| 917 | if (ovl_dentry_remote(path->dentry)) { |
| 918 | pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", |
| 919 | tmp); |
| 920 | path_put(path); |
| 921 | err = -EINVAL; |
| 922 | } |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 923 | kfree(tmp); |
| 924 | } |
| 925 | return err; |
| 926 | } |
| 927 | |
| 928 | static int ovl_lower_dir(const char *name, struct path *path, long *namelen, |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 929 | int *stack_depth, bool *remote) |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 930 | { |
| 931 | int err; |
| 932 | struct kstatfs statfs; |
| 933 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 934 | err = ovl_mount_dir_noesc(name, path); |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 935 | if (err) |
| 936 | goto out; |
| 937 | |
| 938 | err = vfs_statfs(path, &statfs); |
| 939 | if (err) { |
| 940 | pr_err("overlayfs: statfs failed on '%s'\n", name); |
| 941 | goto out_put; |
| 942 | } |
| 943 | *namelen = max(*namelen, statfs.f_namelen); |
| 944 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); |
| 945 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 946 | if (ovl_dentry_remote(path->dentry)) |
| 947 | *remote = true; |
| 948 | |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 949 | return 0; |
| 950 | |
| 951 | out_put: |
| 952 | path_put(path); |
| 953 | out: |
| 954 | return err; |
| 955 | } |
| 956 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 957 | /* Workdir should not be subdir of upperdir and vice versa */ |
| 958 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) |
| 959 | { |
| 960 | bool ok = false; |
| 961 | |
| 962 | if (workdir != upperdir) { |
| 963 | ok = (lock_rename(workdir, upperdir) == NULL); |
| 964 | unlock_rename(workdir, upperdir); |
| 965 | } |
| 966 | return ok; |
| 967 | } |
| 968 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 969 | static unsigned int ovl_split_lowerdirs(char *str) |
| 970 | { |
| 971 | unsigned int ctr = 1; |
| 972 | char *s, *d; |
| 973 | |
| 974 | for (s = d = str;; s++, d++) { |
| 975 | if (*s == '\\') { |
| 976 | s++; |
| 977 | } else if (*s == ':') { |
| 978 | *d = '\0'; |
| 979 | ctr++; |
| 980 | continue; |
| 981 | } |
| 982 | *d = *s; |
| 983 | if (!*s) |
| 984 | break; |
| 985 | } |
| 986 | return ctr; |
| 987 | } |
| 988 | |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 989 | static int __maybe_unused |
| 990 | ovl_posix_acl_xattr_set(const struct xattr_handler *handler, |
| 991 | struct dentry *dentry, struct inode *inode, |
| 992 | const char *name, const void *value, |
| 993 | size_t size, int flags) |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 994 | { |
| 995 | struct dentry *workdir = ovl_workdir(dentry); |
| 996 | struct inode *realinode = ovl_inode_real(inode, NULL); |
| 997 | struct posix_acl *acl = NULL; |
| 998 | int err; |
| 999 | |
| 1000 | /* Check that everything is OK before copy-up */ |
| 1001 | if (value) { |
| 1002 | acl = posix_acl_from_xattr(&init_user_ns, value, size); |
| 1003 | if (IS_ERR(acl)) |
| 1004 | return PTR_ERR(acl); |
| 1005 | } |
| 1006 | err = -EOPNOTSUPP; |
| 1007 | if (!IS_POSIXACL(d_inode(workdir))) |
| 1008 | goto out_acl_release; |
| 1009 | if (!realinode->i_op->set_acl) |
| 1010 | goto out_acl_release; |
| 1011 | if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) { |
| 1012 | err = acl ? -EACCES : 0; |
| 1013 | goto out_acl_release; |
| 1014 | } |
| 1015 | err = -EPERM; |
| 1016 | if (!inode_owner_or_capable(inode)) |
| 1017 | goto out_acl_release; |
| 1018 | |
| 1019 | posix_acl_release(acl); |
| 1020 | |
Miklos Szeredi | ce31513 | 2016-09-01 11:12:00 +0200 | [diff] [blame^] | 1021 | err = ovl_xattr_set(dentry, handler->name, value, size, flags); |
| 1022 | if (!err) |
| 1023 | ovl_copyattr(ovl_inode_real(inode, NULL), inode); |
| 1024 | |
| 1025 | return err; |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1026 | |
| 1027 | out_acl_release: |
| 1028 | posix_acl_release(acl); |
| 1029 | return err; |
| 1030 | } |
| 1031 | |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1032 | static int ovl_own_xattr_set(const struct xattr_handler *handler, |
| 1033 | struct dentry *dentry, struct inode *inode, |
| 1034 | const char *name, const void *value, |
| 1035 | size_t size, int flags) |
| 1036 | { |
| 1037 | return -EPERM; |
| 1038 | } |
| 1039 | |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 1040 | static int ovl_other_xattr_set(const struct xattr_handler *handler, |
| 1041 | struct dentry *dentry, struct inode *inode, |
| 1042 | const char *name, const void *value, |
| 1043 | size_t size, int flags) |
| 1044 | { |
| 1045 | return ovl_xattr_set(dentry, name, value, size, flags); |
| 1046 | } |
| 1047 | |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 1048 | static const struct xattr_handler __maybe_unused |
| 1049 | ovl_posix_acl_access_xattr_handler = { |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1050 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
| 1051 | .flags = ACL_TYPE_ACCESS, |
| 1052 | .set = ovl_posix_acl_xattr_set, |
| 1053 | }; |
| 1054 | |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 1055 | static const struct xattr_handler __maybe_unused |
| 1056 | ovl_posix_acl_default_xattr_handler = { |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1057 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
| 1058 | .flags = ACL_TYPE_DEFAULT, |
| 1059 | .set = ovl_posix_acl_xattr_set, |
| 1060 | }; |
| 1061 | |
| 1062 | static const struct xattr_handler ovl_own_xattr_handler = { |
| 1063 | .prefix = OVL_XATTR_PREFIX, |
| 1064 | .set = ovl_own_xattr_set, |
| 1065 | }; |
| 1066 | |
| 1067 | static const struct xattr_handler ovl_other_xattr_handler = { |
| 1068 | .prefix = "", /* catch all */ |
| 1069 | .set = ovl_other_xattr_set, |
| 1070 | }; |
| 1071 | |
| 1072 | static const struct xattr_handler *ovl_xattr_handlers[] = { |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 1073 | #ifdef CONFIG_FS_POSIX_ACL |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1074 | &ovl_posix_acl_access_xattr_handler, |
| 1075 | &ovl_posix_acl_default_xattr_handler, |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 1076 | #endif |
Miklos Szeredi | d837a49 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1077 | &ovl_own_xattr_handler, |
| 1078 | &ovl_other_xattr_handler, |
| 1079 | NULL |
| 1080 | }; |
| 1081 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1082 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
| 1083 | { |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1084 | struct path upperpath = { NULL, NULL }; |
| 1085 | struct path workpath = { NULL, NULL }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1086 | struct dentry *root_dentry; |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1087 | struct inode *realinode; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1088 | struct ovl_entry *oe; |
| 1089 | struct ovl_fs *ufs; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1090 | struct path *stack = NULL; |
| 1091 | char *lowertmp; |
| 1092 | char *lower; |
| 1093 | unsigned int numlower; |
| 1094 | unsigned int stacklen = 0; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 1095 | unsigned int i; |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 1096 | bool remote = false; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1097 | int err; |
| 1098 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1099 | err = -ENOMEM; |
| 1100 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); |
| 1101 | if (!ufs) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1102 | goto out; |
| 1103 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1104 | err = ovl_parse_opt((char *) data, &ufs->config); |
| 1105 | if (err) |
| 1106 | goto out_free_config; |
| 1107 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1108 | err = -EINVAL; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1109 | if (!ufs->config.lowerdir) { |
Konstantin Khlebnikov | 07f2af7 | 2015-06-29 20:18:56 +0300 | [diff] [blame] | 1110 | if (!silent) |
| 1111 | pr_err("overlayfs: missing 'lowerdir'\n"); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1112 | goto out_free_config; |
| 1113 | } |
| 1114 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1115 | sb->s_stack_depth = 0; |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 1116 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1117 | if (ufs->config.upperdir) { |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1118 | if (!ufs->config.workdir) { |
| 1119 | pr_err("overlayfs: missing 'workdir'\n"); |
| 1120 | goto out_free_config; |
| 1121 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1122 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1123 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
| 1124 | if (err) |
| 1125 | goto out_free_config; |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1126 | |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 1127 | /* Upper fs should not be r/o */ |
| 1128 | if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) { |
| 1129 | pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); |
| 1130 | err = -EINVAL; |
| 1131 | goto out_put_upperpath; |
| 1132 | } |
| 1133 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1134 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
| 1135 | if (err) |
| 1136 | goto out_put_upperpath; |
| 1137 | |
hujianyang | 2f83fd8 | 2015-01-06 12:52:13 +0800 | [diff] [blame] | 1138 | err = -EINVAL; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1139 | if (upperpath.mnt != workpath.mnt) { |
| 1140 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); |
| 1141 | goto out_put_workpath; |
| 1142 | } |
| 1143 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { |
| 1144 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); |
| 1145 | goto out_put_workpath; |
| 1146 | } |
| 1147 | sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 1148 | } |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1149 | err = -ENOMEM; |
| 1150 | lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); |
| 1151 | if (!lowertmp) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1152 | goto out_put_workpath; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1153 | |
| 1154 | err = -EINVAL; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1155 | stacklen = ovl_split_lowerdirs(lowertmp); |
hujianyang | 6be4506 | 2015-01-15 13:19:21 +0800 | [diff] [blame] | 1156 | if (stacklen > OVL_MAX_STACK) { |
Colin Ian King | fd36570 | 2016-08-18 16:58:35 +0100 | [diff] [blame] | 1157 | pr_err("overlayfs: too many lower directories, limit is %d\n", |
hujianyang | 6be4506 | 2015-01-15 13:19:21 +0800 | [diff] [blame] | 1158 | OVL_MAX_STACK); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1159 | goto out_free_lowertmp; |
hujianyang | 6be4506 | 2015-01-15 13:19:21 +0800 | [diff] [blame] | 1160 | } else if (!ufs->config.upperdir && stacklen == 1) { |
| 1161 | pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); |
| 1162 | goto out_free_lowertmp; |
| 1163 | } |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1164 | |
| 1165 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); |
| 1166 | if (!stack) |
| 1167 | goto out_free_lowertmp; |
| 1168 | |
| 1169 | lower = lowertmp; |
| 1170 | for (numlower = 0; numlower < stacklen; numlower++) { |
| 1171 | err = ovl_lower_dir(lower, &stack[numlower], |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 1172 | &ufs->lower_namelen, &sb->s_stack_depth, |
| 1173 | &remote); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1174 | if (err) |
| 1175 | goto out_put_lowerpath; |
| 1176 | |
| 1177 | lower = strchr(lower, '\0') + 1; |
| 1178 | } |
| 1179 | |
| 1180 | err = -EINVAL; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 1181 | sb->s_stack_depth++; |
Miklos Szeredi | 69c433e | 2014-10-24 00:14:39 +0200 | [diff] [blame] | 1182 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
| 1183 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1184 | goto out_put_lowerpath; |
Miklos Szeredi | 69c433e | 2014-10-24 00:14:39 +0200 | [diff] [blame] | 1185 | } |
| 1186 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1187 | if (ufs->config.upperdir) { |
| 1188 | ufs->upper_mnt = clone_private_mount(&upperpath); |
| 1189 | err = PTR_ERR(ufs->upper_mnt); |
| 1190 | if (IS_ERR(ufs->upper_mnt)) { |
| 1191 | pr_err("overlayfs: failed to clone upperpath\n"); |
| 1192 | goto out_put_lowerpath; |
| 1193 | } |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 1194 | /* Don't inherit atime flags */ |
| 1195 | ufs->upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); |
| 1196 | |
| 1197 | sb->s_time_gran = ufs->upper_mnt->mnt_sb->s_time_gran; |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1198 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1199 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); |
| 1200 | err = PTR_ERR(ufs->workdir); |
| 1201 | if (IS_ERR(ufs->workdir)) { |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 1202 | pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", |
| 1203 | ufs->config.workdir, OVL_WORKDIR_NAME, -err); |
| 1204 | sb->s_flags |= MS_RDONLY; |
| 1205 | ufs->workdir = NULL; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1206 | } |
Vivek Goyal | 45aebea | 2016-02-22 09:28:34 -0500 | [diff] [blame] | 1207 | |
| 1208 | /* |
| 1209 | * Upper should support d_type, else whiteouts are visible. |
| 1210 | * Given workdir and upper are on same fs, we can do |
Vivek Goyal | 2176519 | 2016-05-20 09:04:26 -0400 | [diff] [blame] | 1211 | * iterate_dir() on workdir. This check requires successful |
| 1212 | * creation of workdir in previous step. |
Vivek Goyal | 45aebea | 2016-02-22 09:28:34 -0500 | [diff] [blame] | 1213 | */ |
Vivek Goyal | 2176519 | 2016-05-20 09:04:26 -0400 | [diff] [blame] | 1214 | if (ufs->workdir) { |
| 1215 | err = ovl_check_d_type_supported(&workpath); |
| 1216 | if (err < 0) |
| 1217 | goto out_put_workdir; |
Vivek Goyal | 45aebea | 2016-02-22 09:28:34 -0500 | [diff] [blame] | 1218 | |
Vivek Goyal | e7c0b59 | 2016-07-01 10:02:44 -0400 | [diff] [blame] | 1219 | /* |
| 1220 | * We allowed this configuration and don't want to |
| 1221 | * break users over kernel upgrade. So warn instead |
| 1222 | * of erroring out. |
| 1223 | */ |
| 1224 | if (!err) |
| 1225 | pr_warn("overlayfs: upper fs needs to support d_type.\n"); |
Vivek Goyal | 45aebea | 2016-02-22 09:28:34 -0500 | [diff] [blame] | 1226 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1227 | } |
| 1228 | |
hujianyang | 2f83fd8 | 2015-01-06 12:52:13 +0800 | [diff] [blame] | 1229 | err = -ENOMEM; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1230 | ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 1231 | if (ufs->lower_mnt == NULL) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1232 | goto out_put_workdir; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1233 | for (i = 0; i < numlower; i++) { |
| 1234 | struct vfsmount *mnt = clone_private_mount(&stack[i]); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 1235 | |
hujianyang | 2f83fd8 | 2015-01-06 12:52:13 +0800 | [diff] [blame] | 1236 | err = PTR_ERR(mnt); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1237 | if (IS_ERR(mnt)) { |
| 1238 | pr_err("overlayfs: failed to clone lowerpath\n"); |
| 1239 | goto out_put_lower_mnt; |
| 1240 | } |
| 1241 | /* |
| 1242 | * Make lower_mnt R/O. That way fchmod/fchown on lower file |
| 1243 | * will fail instead of modifying lower fs. |
| 1244 | */ |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 1245 | mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1246 | |
| 1247 | ufs->lower_mnt[ufs->numlower] = mnt; |
| 1248 | ufs->numlower++; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1249 | } |
| 1250 | |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 1251 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
| 1252 | if (!ufs->upper_mnt) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1253 | sb->s_flags |= MS_RDONLY; |
| 1254 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 1255 | if (remote) |
| 1256 | sb->s_d_op = &ovl_reval_dentry_operations; |
| 1257 | else |
| 1258 | sb->s_d_op = &ovl_dentry_operations; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1259 | |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 1260 | ufs->creator_cred = prepare_creds(); |
| 1261 | if (!ufs->creator_cred) |
| 1262 | goto out_put_lower_mnt; |
| 1263 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1264 | err = -ENOMEM; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1265 | oe = ovl_alloc_entry(numlower); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1266 | if (!oe) |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 1267 | goto out_put_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1268 | |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1269 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1270 | if (!root_dentry) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1271 | goto out_free_oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1272 | |
| 1273 | mntput(upperpath.mnt); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1274 | for (i = 0; i < numlower; i++) |
| 1275 | mntput(stack[i].mnt); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1276 | path_put(&workpath); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1277 | kfree(lowertmp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1278 | |
| 1279 | oe->__upperdentry = upperpath.dentry; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1280 | for (i = 0; i < numlower; i++) { |
| 1281 | oe->lowerstack[i].dentry = stack[i].dentry; |
| 1282 | oe->lowerstack[i].mnt = ufs->lower_mnt[i]; |
| 1283 | } |
Konstantin Khlebnikov | 0f95502 | 2015-08-24 15:57:18 +0300 | [diff] [blame] | 1284 | kfree(stack); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1285 | |
| 1286 | root_dentry->d_fsdata = oe; |
| 1287 | |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 1288 | realinode = d_inode(ovl_dentry_real(root_dentry)); |
| 1289 | ovl_inode_init(d_inode(root_dentry), realinode, !!upperpath.dentry); |
| 1290 | ovl_copyattr(realinode, d_inode(root_dentry)); |
Miklos Szeredi | ed06e06 | 2015-12-09 16:11:59 +0100 | [diff] [blame] | 1291 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1292 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1293 | sb->s_op = &ovl_super_operations; |
Andreas Gruenbacher | 0c97be2 | 2016-08-22 16:36:49 +0200 | [diff] [blame] | 1294 | sb->s_xattr = ovl_xattr_handlers; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1295 | sb->s_root = root_dentry; |
| 1296 | sb->s_fs_info = ufs; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 1297 | sb->s_flags |= MS_POSIXACL; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1298 | |
| 1299 | return 0; |
| 1300 | |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1301 | out_free_oe: |
| 1302 | kfree(oe); |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 1303 | out_put_cred: |
| 1304 | put_cred(ufs->creator_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1305 | out_put_lower_mnt: |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 1306 | for (i = 0; i < ufs->numlower; i++) |
| 1307 | mntput(ufs->lower_mnt[i]); |
| 1308 | kfree(ufs->lower_mnt); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1309 | out_put_workdir: |
| 1310 | dput(ufs->workdir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1311 | mntput(ufs->upper_mnt); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1312 | out_put_lowerpath: |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1313 | for (i = 0; i < numlower; i++) |
| 1314 | path_put(&stack[i]); |
| 1315 | kfree(stack); |
| 1316 | out_free_lowertmp: |
| 1317 | kfree(lowertmp); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1318 | out_put_workpath: |
| 1319 | path_put(&workpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1320 | out_put_upperpath: |
| 1321 | path_put(&upperpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1322 | out_free_config: |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1323 | kfree(ufs->config.lowerdir); |
| 1324 | kfree(ufs->config.upperdir); |
| 1325 | kfree(ufs->config.workdir); |
| 1326 | kfree(ufs); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1327 | out: |
| 1328 | return err; |
| 1329 | } |
| 1330 | |
| 1331 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, |
| 1332 | const char *dev_name, void *raw_data) |
| 1333 | { |
| 1334 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); |
| 1335 | } |
| 1336 | |
| 1337 | static struct file_system_type ovl_fs_type = { |
| 1338 | .owner = THIS_MODULE, |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 1339 | .name = "overlay", |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1340 | .mount = ovl_mount, |
| 1341 | .kill_sb = kill_anon_super, |
| 1342 | }; |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 1343 | MODULE_ALIAS_FS("overlay"); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1344 | |
| 1345 | static int __init ovl_init(void) |
| 1346 | { |
| 1347 | return register_filesystem(&ovl_fs_type); |
| 1348 | } |
| 1349 | |
| 1350 | static void __exit ovl_exit(void) |
| 1351 | { |
| 1352 | unregister_filesystem(&ovl_fs_type); |
| 1353 | } |
| 1354 | |
| 1355 | module_init(ovl_init); |
| 1356 | module_exit(ovl_exit); |