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> |
Andrew Morton | e458bcd | 2016-01-20 15:01:13 -0800 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 20 | #include <linux/sched.h> |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 21 | #include <linux/statfs.h> |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 22 | #include <linux/seq_file.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 | a78d9f0d | 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 | |
| 148 | struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper) |
| 149 | { |
| 150 | struct dentry *realdentry; |
| 151 | |
| 152 | realdentry = ovl_upperdentry_dereference(oe); |
| 153 | if (realdentry) { |
| 154 | *is_upper = true; |
| 155 | } else { |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 156 | realdentry = __ovl_dentry_lower(oe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 157 | *is_upper = false; |
| 158 | } |
| 159 | return realdentry; |
| 160 | } |
| 161 | |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 162 | struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode, |
| 163 | bool is_upper) |
| 164 | { |
| 165 | if (is_upper) { |
| 166 | struct ovl_fs *ofs = inode->i_sb->s_fs_info; |
| 167 | |
| 168 | return ofs->upper_mnt; |
| 169 | } else { |
| 170 | return oe->numlower ? oe->lowerstack[0].mnt : NULL; |
| 171 | } |
| 172 | } |
| 173 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 174 | struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) |
| 175 | { |
| 176 | struct ovl_entry *oe = dentry->d_fsdata; |
| 177 | |
| 178 | return oe->cache; |
| 179 | } |
| 180 | |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 181 | bool ovl_is_default_permissions(struct inode *inode) |
| 182 | { |
| 183 | struct ovl_fs *ofs = inode->i_sb->s_fs_info; |
| 184 | |
| 185 | return ofs->config.default_permissions; |
| 186 | } |
| 187 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 188 | void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) |
| 189 | { |
| 190 | struct ovl_entry *oe = dentry->d_fsdata; |
| 191 | |
| 192 | oe->cache = cache; |
| 193 | } |
| 194 | |
| 195 | void ovl_path_lower(struct dentry *dentry, struct path *path) |
| 196 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 197 | struct ovl_entry *oe = dentry->d_fsdata; |
| 198 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 199 | *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | int ovl_want_write(struct dentry *dentry) |
| 203 | { |
| 204 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 205 | return mnt_want_write(ofs->upper_mnt); |
| 206 | } |
| 207 | |
| 208 | void ovl_drop_write(struct dentry *dentry) |
| 209 | { |
| 210 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 211 | mnt_drop_write(ofs->upper_mnt); |
| 212 | } |
| 213 | |
| 214 | struct dentry *ovl_workdir(struct dentry *dentry) |
| 215 | { |
| 216 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 217 | return ofs->workdir; |
| 218 | } |
| 219 | |
| 220 | bool ovl_dentry_is_opaque(struct dentry *dentry) |
| 221 | { |
| 222 | struct ovl_entry *oe = dentry->d_fsdata; |
| 223 | return oe->opaque; |
| 224 | } |
| 225 | |
| 226 | void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) |
| 227 | { |
| 228 | struct ovl_entry *oe = dentry->d_fsdata; |
| 229 | oe->opaque = opaque; |
| 230 | } |
| 231 | |
| 232 | void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) |
| 233 | { |
| 234 | struct ovl_entry *oe = dentry->d_fsdata; |
| 235 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 236 | WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 237 | WARN_ON(oe->__upperdentry); |
| 238 | BUG_ON(!upperdentry->d_inode); |
| 239 | /* |
| 240 | * Make sure upperdentry is consistent before making it visible to |
| 241 | * ovl_upperdentry_dereference(). |
| 242 | */ |
| 243 | smp_wmb(); |
| 244 | oe->__upperdentry = upperdentry; |
| 245 | } |
| 246 | |
| 247 | void ovl_dentry_version_inc(struct dentry *dentry) |
| 248 | { |
| 249 | struct ovl_entry *oe = dentry->d_fsdata; |
| 250 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 251 | WARN_ON(!inode_is_locked(dentry->d_inode)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 252 | oe->version++; |
| 253 | } |
| 254 | |
| 255 | u64 ovl_dentry_version_get(struct dentry *dentry) |
| 256 | { |
| 257 | struct ovl_entry *oe = dentry->d_fsdata; |
| 258 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 259 | WARN_ON(!inode_is_locked(dentry->d_inode)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 260 | return oe->version; |
| 261 | } |
| 262 | |
| 263 | bool ovl_is_whiteout(struct dentry *dentry) |
| 264 | { |
| 265 | struct inode *inode = dentry->d_inode; |
| 266 | |
| 267 | return inode && IS_WHITEOUT(inode); |
| 268 | } |
| 269 | |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 270 | const struct cred *ovl_override_creds(struct super_block *sb) |
| 271 | { |
| 272 | struct ovl_fs *ofs = sb->s_fs_info; |
| 273 | |
| 274 | return override_creds(ofs->creator_cred); |
| 275 | } |
| 276 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 277 | static bool ovl_is_opaquedir(struct dentry *dentry) |
| 278 | { |
| 279 | int res; |
| 280 | char val; |
| 281 | struct inode *inode = dentry->d_inode; |
| 282 | |
| 283 | if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) |
| 284 | return false; |
| 285 | |
Al Viro | ce23e64 | 2016-04-11 00:48:00 -0400 | [diff] [blame] | 286 | res = inode->i_op->getxattr(dentry, inode, OVL_XATTR_OPAQUE, &val, 1); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 287 | if (res == 1 && val == 'y') |
| 288 | return true; |
| 289 | |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | static void ovl_dentry_release(struct dentry *dentry) |
| 294 | { |
| 295 | struct ovl_entry *oe = dentry->d_fsdata; |
| 296 | |
| 297 | if (oe) { |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 298 | unsigned int i; |
| 299 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 300 | dput(oe->__upperdentry); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 301 | for (i = 0; i < oe->numlower; i++) |
| 302 | dput(oe->lowerstack[i].dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 303 | kfree_rcu(oe, rcu); |
| 304 | } |
| 305 | } |
| 306 | |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 307 | static struct dentry *ovl_d_real(struct dentry *dentry, struct inode *inode) |
| 308 | { |
| 309 | struct dentry *real; |
| 310 | |
| 311 | if (d_is_dir(dentry)) { |
| 312 | if (!inode || inode == d_inode(dentry)) |
| 313 | return dentry; |
| 314 | goto bug; |
| 315 | } |
| 316 | |
| 317 | real = ovl_dentry_upper(dentry); |
| 318 | if (real && (!inode || inode == d_inode(real))) |
| 319 | return real; |
| 320 | |
| 321 | real = ovl_dentry_lower(dentry); |
| 322 | if (!real) |
| 323 | goto bug; |
| 324 | |
| 325 | if (!inode || inode == d_inode(real)) |
| 326 | return real; |
| 327 | |
| 328 | /* Handle recursion */ |
| 329 | if (real->d_flags & DCACHE_OP_REAL) |
| 330 | return real->d_op->d_real(real, inode); |
| 331 | |
| 332 | bug: |
| 333 | WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry, |
| 334 | inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0); |
| 335 | return dentry; |
| 336 | } |
| 337 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 338 | static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
| 339 | { |
| 340 | struct ovl_entry *oe = dentry->d_fsdata; |
| 341 | unsigned int i; |
| 342 | int ret = 1; |
| 343 | |
| 344 | for (i = 0; i < oe->numlower; i++) { |
| 345 | struct dentry *d = oe->lowerstack[i].dentry; |
| 346 | |
| 347 | if (d->d_flags & DCACHE_OP_REVALIDATE) { |
| 348 | ret = d->d_op->d_revalidate(d, flags); |
| 349 | if (ret < 0) |
| 350 | return ret; |
| 351 | if (!ret) { |
| 352 | if (!(flags & LOOKUP_RCU)) |
| 353 | d_invalidate(d); |
| 354 | return -ESTALE; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 361 | static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) |
| 362 | { |
| 363 | struct ovl_entry *oe = dentry->d_fsdata; |
| 364 | unsigned int i; |
| 365 | int ret = 1; |
| 366 | |
| 367 | for (i = 0; i < oe->numlower; i++) { |
| 368 | struct dentry *d = oe->lowerstack[i].dentry; |
| 369 | |
| 370 | if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { |
| 371 | ret = d->d_op->d_weak_revalidate(d, flags); |
| 372 | if (ret <= 0) |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | return ret; |
| 377 | } |
| 378 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 379 | static const struct dentry_operations ovl_dentry_operations = { |
| 380 | .d_release = ovl_dentry_release, |
David Howells | 4bacc9c | 2015-06-18 14:32:31 +0100 | [diff] [blame] | 381 | .d_select_inode = ovl_d_select_inode, |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 382 | .d_real = ovl_d_real, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 383 | }; |
| 384 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 385 | static const struct dentry_operations ovl_reval_dentry_operations = { |
| 386 | .d_release = ovl_dentry_release, |
Konstantin Khlebnikov | b5891cf | 2016-01-31 16:22:16 +0300 | [diff] [blame] | 387 | .d_select_inode = ovl_d_select_inode, |
Miklos Szeredi | d101a12 | 2016-03-26 16:14:37 -0400 | [diff] [blame] | 388 | .d_real = ovl_d_real, |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 389 | .d_revalidate = ovl_dentry_revalidate, |
| 390 | .d_weak_revalidate = ovl_dentry_weak_revalidate, |
| 391 | }; |
| 392 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 393 | static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 394 | { |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 395 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); |
| 396 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); |
| 397 | |
| 398 | if (oe) |
| 399 | oe->numlower = numlower; |
| 400 | |
| 401 | return oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 404 | static bool ovl_dentry_remote(struct dentry *dentry) |
| 405 | { |
| 406 | return dentry->d_flags & |
| 407 | (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE); |
| 408 | } |
| 409 | |
| 410 | static bool ovl_dentry_weird(struct dentry *dentry) |
| 411 | { |
| 412 | return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | |
| 413 | DCACHE_MANAGE_TRANSIT | |
| 414 | DCACHE_OP_HASH | |
| 415 | DCACHE_OP_COMPARE); |
| 416 | } |
| 417 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 418 | static inline struct dentry *ovl_lookup_real(struct dentry *dir, |
| 419 | struct qstr *name) |
| 420 | { |
| 421 | struct dentry *dentry; |
| 422 | |
Miklos Szeredi | 38b78a5 | 2016-05-11 01:16:37 +0200 | [diff] [blame] | 423 | dentry = lookup_hash(name, dir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 424 | |
| 425 | if (IS_ERR(dentry)) { |
| 426 | if (PTR_ERR(dentry) == -ENOENT) |
| 427 | dentry = NULL; |
| 428 | } else if (!dentry->d_inode) { |
| 429 | dput(dentry); |
| 430 | dentry = NULL; |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 431 | } else if (ovl_dentry_weird(dentry)) { |
Miklos Szeredi | a6f15d9 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 432 | dput(dentry); |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 433 | /* Don't support traversing automounts and other weirdness */ |
Miklos Szeredi | a6f15d9 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 434 | dentry = ERR_PTR(-EREMOTE); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 435 | } |
| 436 | return dentry; |
| 437 | } |
| 438 | |
Miklos Szeredi | 5ef88da | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 439 | /* |
| 440 | * Returns next layer in stack starting from top. |
| 441 | * Returns -1 if this is the last layer. |
| 442 | */ |
| 443 | int ovl_path_next(int idx, struct dentry *dentry, struct path *path) |
| 444 | { |
| 445 | struct ovl_entry *oe = dentry->d_fsdata; |
| 446 | |
| 447 | BUG_ON(idx < 0); |
| 448 | if (idx == 0) { |
| 449 | ovl_path_upper(dentry, path); |
| 450 | if (path->dentry) |
| 451 | return oe->numlower ? 1 : -1; |
| 452 | idx++; |
| 453 | } |
| 454 | BUG_ON(idx > oe->numlower); |
| 455 | *path = oe->lowerstack[idx - 1]; |
| 456 | |
| 457 | return (idx < oe->numlower) ? idx + 1 : -1; |
| 458 | } |
| 459 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 460 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
| 461 | unsigned int flags) |
| 462 | { |
| 463 | struct ovl_entry *oe; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 464 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
| 465 | struct path *stack = NULL; |
| 466 | struct dentry *upperdir, *upperdentry = NULL; |
| 467 | unsigned int ctr = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 468 | struct inode *inode = NULL; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 469 | bool upperopaque = false; |
| 470 | struct dentry *this, *prev = NULL; |
| 471 | unsigned int i; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 472 | int err; |
| 473 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 474 | upperdir = ovl_upperdentry_dereference(poe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 475 | if (upperdir) { |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 476 | this = ovl_lookup_real(upperdir, &dentry->d_name); |
| 477 | err = PTR_ERR(this); |
| 478 | if (IS_ERR(this)) |
| 479 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 480 | |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 481 | if (this) { |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 482 | if (unlikely(ovl_dentry_remote(this))) { |
| 483 | dput(this); |
| 484 | err = -EREMOTE; |
| 485 | goto out; |
| 486 | } |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 487 | if (ovl_is_whiteout(this)) { |
| 488 | dput(this); |
| 489 | this = NULL; |
| 490 | upperopaque = true; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 491 | } else if (poe->numlower && ovl_is_opaquedir(this)) { |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 492 | upperopaque = true; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 493 | } |
| 494 | } |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 495 | upperdentry = prev = this; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 496 | } |
| 497 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 498 | if (!upperopaque && poe->numlower) { |
| 499 | err = -ENOMEM; |
| 500 | stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL); |
| 501 | if (!stack) |
| 502 | goto out_put_upper; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 505 | for (i = 0; !upperopaque && i < poe->numlower; i++) { |
| 506 | bool opaque = false; |
| 507 | struct path lowerpath = poe->lowerstack[i]; |
| 508 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 509 | this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name); |
| 510 | err = PTR_ERR(this); |
Miklos Szeredi | 09e1032 | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 511 | if (IS_ERR(this)) { |
| 512 | /* |
| 513 | * If it's positive, then treat ENAMETOOLONG as ENOENT. |
| 514 | */ |
| 515 | if (err == -ENAMETOOLONG && (upperdentry || ctr)) |
| 516 | continue; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 517 | goto out_put; |
Miklos Szeredi | 09e1032 | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 518 | } |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 519 | if (!this) |
| 520 | continue; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 521 | if (ovl_is_whiteout(this)) { |
| 522 | dput(this); |
| 523 | break; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 524 | } |
| 525 | /* |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 526 | * Only makes sense to check opaque dir if this is not the |
| 527 | * lowermost layer. |
| 528 | */ |
| 529 | if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) |
| 530 | opaque = true; |
hujianyang | a425c03 | 2015-01-06 16:10:01 +0800 | [diff] [blame] | 531 | |
| 532 | if (prev && (!S_ISDIR(prev->d_inode->i_mode) || |
| 533 | !S_ISDIR(this->d_inode->i_mode))) { |
| 534 | /* |
| 535 | * FIXME: check for upper-opaqueness maybe better done |
| 536 | * in remove code. |
| 537 | */ |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 538 | if (prev == upperdentry) |
| 539 | upperopaque = true; |
| 540 | dput(this); |
| 541 | break; |
| 542 | } |
hujianyang | a425c03 | 2015-01-06 16:10:01 +0800 | [diff] [blame] | 543 | /* |
| 544 | * If this is a non-directory then stop here. |
| 545 | */ |
| 546 | if (!S_ISDIR(this->d_inode->i_mode)) |
| 547 | opaque = true; |
| 548 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 549 | stack[ctr].dentry = this; |
| 550 | stack[ctr].mnt = lowerpath.mnt; |
| 551 | ctr++; |
| 552 | prev = this; |
| 553 | if (opaque) |
| 554 | break; |
| 555 | } |
| 556 | |
| 557 | oe = ovl_alloc_entry(ctr); |
| 558 | err = -ENOMEM; |
| 559 | if (!oe) |
| 560 | goto out_put; |
| 561 | |
| 562 | if (upperdentry || ctr) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 563 | struct dentry *realdentry; |
| 564 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 565 | realdentry = upperdentry ? upperdentry : stack[0].dentry; |
| 566 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 567 | err = -ENOMEM; |
| 568 | inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, |
| 569 | oe); |
| 570 | if (!inode) |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 571 | goto out_free_oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 572 | ovl_copyattr(realdentry->d_inode, inode); |
| 573 | } |
| 574 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 575 | oe->opaque = upperopaque; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 576 | oe->__upperdentry = upperdentry; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 577 | memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); |
| 578 | kfree(stack); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 579 | dentry->d_fsdata = oe; |
| 580 | d_add(dentry, inode); |
| 581 | |
| 582 | return NULL; |
| 583 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 584 | out_free_oe: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 585 | kfree(oe); |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 586 | out_put: |
| 587 | for (i = 0; i < ctr; i++) |
| 588 | dput(stack[i].dentry); |
| 589 | kfree(stack); |
| 590 | out_put_upper: |
| 591 | dput(upperdentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 592 | out: |
| 593 | return ERR_PTR(err); |
| 594 | } |
| 595 | |
| 596 | struct file *ovl_path_open(struct path *path, int flags) |
| 597 | { |
| 598 | return dentry_open(path, flags, current_cred()); |
| 599 | } |
| 600 | |
| 601 | static void ovl_put_super(struct super_block *sb) |
| 602 | { |
| 603 | struct ovl_fs *ufs = sb->s_fs_info; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 604 | unsigned i; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 605 | |
| 606 | dput(ufs->workdir); |
| 607 | mntput(ufs->upper_mnt); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 608 | for (i = 0; i < ufs->numlower; i++) |
| 609 | mntput(ufs->lower_mnt[i]); |
Konstantin Khlebnikov | 5ffdbe8 | 2015-08-24 15:57:19 +0300 | [diff] [blame] | 610 | kfree(ufs->lower_mnt); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 611 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 612 | kfree(ufs->config.lowerdir); |
| 613 | kfree(ufs->config.upperdir); |
| 614 | kfree(ufs->config.workdir); |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 615 | put_cred(ufs->creator_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 616 | kfree(ufs); |
| 617 | } |
| 618 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 619 | /** |
| 620 | * ovl_statfs |
| 621 | * @sb: The overlayfs super block |
| 622 | * @buf: The struct kstatfs to fill in with stats |
| 623 | * |
| 624 | * Get the filesystem statistics. As writes always target the upper layer |
Miklos Szeredi | 4ebc581 | 2014-12-13 00:59:46 +0100 | [diff] [blame] | 625 | * filesystem pass the statfs to the upper filesystem (if it exists) |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 626 | */ |
| 627 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 628 | { |
| 629 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 630 | struct dentry *root_dentry = dentry->d_sb->s_root; |
| 631 | struct path path; |
| 632 | int err; |
| 633 | |
Miklos Szeredi | 4ebc581 | 2014-12-13 00:59:46 +0100 | [diff] [blame] | 634 | ovl_path_real(root_dentry, &path); |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 635 | |
| 636 | err = vfs_statfs(&path, buf); |
| 637 | if (!err) { |
| 638 | buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); |
| 639 | buf->f_type = OVERLAYFS_SUPER_MAGIC; |
| 640 | } |
| 641 | |
| 642 | return err; |
| 643 | } |
| 644 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 645 | /** |
| 646 | * ovl_show_options |
| 647 | * |
| 648 | * Prints the mount options for a given superblock. |
| 649 | * Returns zero; does not fail. |
| 650 | */ |
| 651 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) |
| 652 | { |
| 653 | struct super_block *sb = dentry->d_sb; |
| 654 | struct ovl_fs *ufs = sb->s_fs_info; |
| 655 | |
Kees Cook | a068acf | 2015-09-04 15:44:57 -0700 | [diff] [blame] | 656 | seq_show_option(m, "lowerdir", ufs->config.lowerdir); |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 657 | if (ufs->config.upperdir) { |
Kees Cook | a068acf | 2015-09-04 15:44:57 -0700 | [diff] [blame] | 658 | seq_show_option(m, "upperdir", ufs->config.upperdir); |
| 659 | seq_show_option(m, "workdir", ufs->config.workdir); |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 660 | } |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 661 | if (ufs->config.default_permissions) |
| 662 | seq_puts(m, ",default_permissions"); |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 663 | return 0; |
| 664 | } |
| 665 | |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 666 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
| 667 | { |
| 668 | struct ovl_fs *ufs = sb->s_fs_info; |
| 669 | |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 670 | if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir)) |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 671 | return -EROFS; |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 676 | static const struct super_operations ovl_super_operations = { |
| 677 | .put_super = ovl_put_super, |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 678 | .statfs = ovl_statfs, |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 679 | .show_options = ovl_show_options, |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 680 | .remount_fs = ovl_remount, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 681 | }; |
| 682 | |
| 683 | enum { |
| 684 | OPT_LOWERDIR, |
| 685 | OPT_UPPERDIR, |
| 686 | OPT_WORKDIR, |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 687 | OPT_DEFAULT_PERMISSIONS, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 688 | OPT_ERR, |
| 689 | }; |
| 690 | |
| 691 | static const match_table_t ovl_tokens = { |
| 692 | {OPT_LOWERDIR, "lowerdir=%s"}, |
| 693 | {OPT_UPPERDIR, "upperdir=%s"}, |
| 694 | {OPT_WORKDIR, "workdir=%s"}, |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 695 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 696 | {OPT_ERR, NULL} |
| 697 | }; |
| 698 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 699 | static char *ovl_next_opt(char **s) |
| 700 | { |
| 701 | char *sbegin = *s; |
| 702 | char *p; |
| 703 | |
| 704 | if (sbegin == NULL) |
| 705 | return NULL; |
| 706 | |
| 707 | for (p = sbegin; *p; p++) { |
| 708 | if (*p == '\\') { |
| 709 | p++; |
| 710 | if (!*p) |
| 711 | break; |
| 712 | } else if (*p == ',') { |
| 713 | *p = '\0'; |
| 714 | *s = p + 1; |
| 715 | return sbegin; |
| 716 | } |
| 717 | } |
| 718 | *s = NULL; |
| 719 | return sbegin; |
| 720 | } |
| 721 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 722 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
| 723 | { |
| 724 | char *p; |
| 725 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 726 | while ((p = ovl_next_opt(&opt)) != NULL) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 727 | int token; |
| 728 | substring_t args[MAX_OPT_ARGS]; |
| 729 | |
| 730 | if (!*p) |
| 731 | continue; |
| 732 | |
| 733 | token = match_token(p, ovl_tokens, args); |
| 734 | switch (token) { |
| 735 | case OPT_UPPERDIR: |
| 736 | kfree(config->upperdir); |
| 737 | config->upperdir = match_strdup(&args[0]); |
| 738 | if (!config->upperdir) |
| 739 | return -ENOMEM; |
| 740 | break; |
| 741 | |
| 742 | case OPT_LOWERDIR: |
| 743 | kfree(config->lowerdir); |
| 744 | config->lowerdir = match_strdup(&args[0]); |
| 745 | if (!config->lowerdir) |
| 746 | return -ENOMEM; |
| 747 | break; |
| 748 | |
| 749 | case OPT_WORKDIR: |
| 750 | kfree(config->workdir); |
| 751 | config->workdir = match_strdup(&args[0]); |
| 752 | if (!config->workdir) |
| 753 | return -ENOMEM; |
| 754 | break; |
| 755 | |
Miklos Szeredi | 8d3095f | 2015-10-12 17:11:44 +0200 | [diff] [blame] | 756 | case OPT_DEFAULT_PERMISSIONS: |
| 757 | config->default_permissions = true; |
| 758 | break; |
| 759 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 760 | default: |
hujianyang | bead55e | 2015-01-15 13:17:36 +0800 | [diff] [blame] | 761 | pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 762 | return -EINVAL; |
| 763 | } |
| 764 | } |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 765 | |
| 766 | /* Workdir is useless in non-upper mount */ |
| 767 | if (!config->upperdir && config->workdir) { |
| 768 | pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", |
| 769 | config->workdir); |
| 770 | kfree(config->workdir); |
| 771 | config->workdir = NULL; |
| 772 | } |
| 773 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | #define OVL_WORKDIR_NAME "work" |
| 778 | |
| 779 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, |
| 780 | struct dentry *dentry) |
| 781 | { |
| 782 | struct inode *dir = dentry->d_inode; |
| 783 | struct dentry *work; |
| 784 | int err; |
| 785 | bool retried = false; |
| 786 | |
| 787 | err = mnt_want_write(mnt); |
| 788 | if (err) |
| 789 | return ERR_PTR(err); |
| 790 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 791 | inode_lock_nested(dir, I_MUTEX_PARENT); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 792 | retry: |
| 793 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, |
| 794 | strlen(OVL_WORKDIR_NAME)); |
| 795 | |
| 796 | if (!IS_ERR(work)) { |
| 797 | struct kstat stat = { |
| 798 | .mode = S_IFDIR | 0, |
| 799 | }; |
| 800 | |
| 801 | if (work->d_inode) { |
| 802 | err = -EEXIST; |
| 803 | if (retried) |
| 804 | goto out_dput; |
| 805 | |
| 806 | retried = true; |
| 807 | ovl_cleanup(dir, work); |
| 808 | dput(work); |
| 809 | goto retry; |
| 810 | } |
| 811 | |
| 812 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); |
| 813 | if (err) |
| 814 | goto out_dput; |
| 815 | } |
| 816 | out_unlock: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 817 | inode_unlock(dir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 818 | mnt_drop_write(mnt); |
| 819 | |
| 820 | return work; |
| 821 | |
| 822 | out_dput: |
| 823 | dput(work); |
| 824 | work = ERR_PTR(err); |
| 825 | goto out_unlock; |
| 826 | } |
| 827 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 828 | static void ovl_unescape(char *s) |
| 829 | { |
| 830 | char *d = s; |
| 831 | |
| 832 | for (;; s++, d++) { |
| 833 | if (*s == '\\') |
| 834 | s++; |
| 835 | *d = *s; |
| 836 | if (!*s) |
| 837 | break; |
| 838 | } |
| 839 | } |
| 840 | |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 841 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
| 842 | { |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 843 | int err = -EINVAL; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 844 | |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 845 | if (!*name) { |
| 846 | pr_err("overlayfs: empty lowerdir\n"); |
| 847 | goto out; |
| 848 | } |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 849 | err = kern_path(name, LOOKUP_FOLLOW, path); |
| 850 | if (err) { |
| 851 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); |
| 852 | goto out; |
| 853 | } |
| 854 | err = -EINVAL; |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 855 | if (ovl_dentry_weird(path->dentry)) { |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 856 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); |
| 857 | goto out_put; |
| 858 | } |
| 859 | if (!S_ISDIR(path->dentry->d_inode->i_mode)) { |
| 860 | pr_err("overlayfs: '%s' not a directory\n", name); |
| 861 | goto out_put; |
| 862 | } |
| 863 | return 0; |
| 864 | |
| 865 | out_put: |
| 866 | path_put(path); |
| 867 | out: |
| 868 | return err; |
| 869 | } |
| 870 | |
| 871 | static int ovl_mount_dir(const char *name, struct path *path) |
| 872 | { |
| 873 | int err = -ENOMEM; |
| 874 | char *tmp = kstrdup(name, GFP_KERNEL); |
| 875 | |
| 876 | if (tmp) { |
| 877 | ovl_unescape(tmp); |
| 878 | err = ovl_mount_dir_noesc(tmp, path); |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 879 | |
| 880 | if (!err) |
| 881 | if (ovl_dentry_remote(path->dentry)) { |
| 882 | pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", |
| 883 | tmp); |
| 884 | path_put(path); |
| 885 | err = -EINVAL; |
| 886 | } |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 887 | kfree(tmp); |
| 888 | } |
| 889 | return err; |
| 890 | } |
| 891 | |
| 892 | 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] | 893 | int *stack_depth, bool *remote) |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 894 | { |
| 895 | int err; |
| 896 | struct kstatfs statfs; |
| 897 | |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 898 | err = ovl_mount_dir_noesc(name, path); |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 899 | if (err) |
| 900 | goto out; |
| 901 | |
| 902 | err = vfs_statfs(path, &statfs); |
| 903 | if (err) { |
| 904 | pr_err("overlayfs: statfs failed on '%s'\n", name); |
| 905 | goto out_put; |
| 906 | } |
| 907 | *namelen = max(*namelen, statfs.f_namelen); |
| 908 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); |
| 909 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 910 | if (ovl_dentry_remote(path->dentry)) |
| 911 | *remote = true; |
| 912 | |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 913 | return 0; |
| 914 | |
| 915 | out_put: |
| 916 | path_put(path); |
| 917 | out: |
| 918 | return err; |
| 919 | } |
| 920 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 921 | /* Workdir should not be subdir of upperdir and vice versa */ |
| 922 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) |
| 923 | { |
| 924 | bool ok = false; |
| 925 | |
| 926 | if (workdir != upperdir) { |
| 927 | ok = (lock_rename(workdir, upperdir) == NULL); |
| 928 | unlock_rename(workdir, upperdir); |
| 929 | } |
| 930 | return ok; |
| 931 | } |
| 932 | |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 933 | static unsigned int ovl_split_lowerdirs(char *str) |
| 934 | { |
| 935 | unsigned int ctr = 1; |
| 936 | char *s, *d; |
| 937 | |
| 938 | for (s = d = str;; s++, d++) { |
| 939 | if (*s == '\\') { |
| 940 | s++; |
| 941 | } else if (*s == ':') { |
| 942 | *d = '\0'; |
| 943 | ctr++; |
| 944 | continue; |
| 945 | } |
| 946 | *d = *s; |
| 947 | if (!*s) |
| 948 | break; |
| 949 | } |
| 950 | return ctr; |
| 951 | } |
| 952 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 953 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
| 954 | { |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 955 | struct path upperpath = { NULL, NULL }; |
| 956 | struct path workpath = { NULL, NULL }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 957 | struct dentry *root_dentry; |
| 958 | struct ovl_entry *oe; |
| 959 | struct ovl_fs *ufs; |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 960 | struct path *stack = NULL; |
| 961 | char *lowertmp; |
| 962 | char *lower; |
| 963 | unsigned int numlower; |
| 964 | unsigned int stacklen = 0; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 965 | unsigned int i; |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 966 | bool remote = false; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 967 | int err; |
| 968 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 969 | err = -ENOMEM; |
| 970 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); |
| 971 | if (!ufs) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 972 | goto out; |
| 973 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 974 | err = ovl_parse_opt((char *) data, &ufs->config); |
| 975 | if (err) |
| 976 | goto out_free_config; |
| 977 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 978 | err = -EINVAL; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 979 | if (!ufs->config.lowerdir) { |
Konstantin Khlebnikov | 07f2af7 | 2015-06-29 20:18:56 +0300 | [diff] [blame] | 980 | if (!silent) |
| 981 | pr_err("overlayfs: missing 'lowerdir'\n"); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 982 | goto out_free_config; |
| 983 | } |
| 984 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 985 | sb->s_stack_depth = 0; |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 986 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 987 | if (ufs->config.upperdir) { |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 988 | if (!ufs->config.workdir) { |
| 989 | pr_err("overlayfs: missing 'workdir'\n"); |
| 990 | goto out_free_config; |
| 991 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 992 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 993 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
| 994 | if (err) |
| 995 | goto out_free_config; |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 996 | |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 997 | /* Upper fs should not be r/o */ |
| 998 | if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) { |
| 999 | pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); |
| 1000 | err = -EINVAL; |
| 1001 | goto out_put_upperpath; |
| 1002 | } |
| 1003 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1004 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
| 1005 | if (err) |
| 1006 | goto out_put_upperpath; |
| 1007 | |
hujianyang | 2f83fd8 | 2015-01-06 12:52:13 +0800 | [diff] [blame] | 1008 | err = -EINVAL; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1009 | if (upperpath.mnt != workpath.mnt) { |
| 1010 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); |
| 1011 | goto out_put_workpath; |
| 1012 | } |
| 1013 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { |
| 1014 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); |
| 1015 | goto out_put_workpath; |
| 1016 | } |
| 1017 | sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 1018 | } |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1019 | err = -ENOMEM; |
| 1020 | lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); |
| 1021 | if (!lowertmp) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1022 | goto out_put_workpath; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1023 | |
| 1024 | err = -EINVAL; |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1025 | stacklen = ovl_split_lowerdirs(lowertmp); |
hujianyang | 6be4506 | 2015-01-15 13:19:21 +0800 | [diff] [blame] | 1026 | if (stacklen > OVL_MAX_STACK) { |
| 1027 | pr_err("overlayfs: too many lower directries, limit is %d\n", |
| 1028 | OVL_MAX_STACK); |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1029 | goto out_free_lowertmp; |
hujianyang | 6be4506 | 2015-01-15 13:19:21 +0800 | [diff] [blame] | 1030 | } else if (!ufs->config.upperdir && stacklen == 1) { |
| 1031 | pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); |
| 1032 | goto out_free_lowertmp; |
| 1033 | } |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1034 | |
| 1035 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); |
| 1036 | if (!stack) |
| 1037 | goto out_free_lowertmp; |
| 1038 | |
| 1039 | lower = lowertmp; |
| 1040 | for (numlower = 0; numlower < stacklen; numlower++) { |
| 1041 | err = ovl_lower_dir(lower, &stack[numlower], |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 1042 | &ufs->lower_namelen, &sb->s_stack_depth, |
| 1043 | &remote); |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1044 | if (err) |
| 1045 | goto out_put_lowerpath; |
| 1046 | |
| 1047 | lower = strchr(lower, '\0') + 1; |
| 1048 | } |
| 1049 | |
| 1050 | err = -EINVAL; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 1051 | sb->s_stack_depth++; |
Miklos Szeredi | 69c433e | 2014-10-24 00:14:39 +0200 | [diff] [blame] | 1052 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
| 1053 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1054 | goto out_put_lowerpath; |
Miklos Szeredi | 69c433e | 2014-10-24 00:14:39 +0200 | [diff] [blame] | 1055 | } |
| 1056 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1057 | if (ufs->config.upperdir) { |
| 1058 | ufs->upper_mnt = clone_private_mount(&upperpath); |
| 1059 | err = PTR_ERR(ufs->upper_mnt); |
| 1060 | if (IS_ERR(ufs->upper_mnt)) { |
| 1061 | pr_err("overlayfs: failed to clone upperpath\n"); |
| 1062 | goto out_put_lowerpath; |
| 1063 | } |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1064 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1065 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); |
| 1066 | err = PTR_ERR(ufs->workdir); |
| 1067 | if (IS_ERR(ufs->workdir)) { |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 1068 | pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", |
| 1069 | ufs->config.workdir, OVL_WORKDIR_NAME, -err); |
| 1070 | sb->s_flags |= MS_RDONLY; |
| 1071 | ufs->workdir = NULL; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 1072 | } |
Vivek Goyal | 45aebea | 2016-02-22 09:28:34 -0500 | [diff] [blame] | 1073 | |
| 1074 | /* |
| 1075 | * Upper should support d_type, else whiteouts are visible. |
| 1076 | * Given workdir and upper are on same fs, we can do |
Vivek Goyal | 2176519 | 2016-05-20 09:04:26 -0400 | [diff] [blame] | 1077 | * iterate_dir() on workdir. This check requires successful |
| 1078 | * creation of workdir in previous step. |
Vivek Goyal | 45aebea | 2016-02-22 09:28:34 -0500 | [diff] [blame] | 1079 | */ |
Vivek Goyal | 2176519 | 2016-05-20 09:04:26 -0400 | [diff] [blame] | 1080 | if (ufs->workdir) { |
| 1081 | err = ovl_check_d_type_supported(&workpath); |
| 1082 | if (err < 0) |
| 1083 | goto out_put_workdir; |
Vivek Goyal | 45aebea | 2016-02-22 09:28:34 -0500 | [diff] [blame] | 1084 | |
Vivek Goyal | e7c0b59 | 2016-07-01 10:02:44 -0400 | [diff] [blame] | 1085 | /* |
| 1086 | * We allowed this configuration and don't want to |
| 1087 | * break users over kernel upgrade. So warn instead |
| 1088 | * of erroring out. |
| 1089 | */ |
| 1090 | if (!err) |
| 1091 | pr_warn("overlayfs: upper fs needs to support d_type.\n"); |
Vivek Goyal | 45aebea | 2016-02-22 09:28:34 -0500 | [diff] [blame] | 1092 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
hujianyang | 2f83fd8 | 2015-01-06 12:52:13 +0800 | [diff] [blame] | 1095 | err = -ENOMEM; |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1096 | ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 1097 | if (ufs->lower_mnt == NULL) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1098 | goto out_put_workdir; |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1099 | for (i = 0; i < numlower; i++) { |
| 1100 | struct vfsmount *mnt = clone_private_mount(&stack[i]); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 1101 | |
hujianyang | 2f83fd8 | 2015-01-06 12:52:13 +0800 | [diff] [blame] | 1102 | err = PTR_ERR(mnt); |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1103 | if (IS_ERR(mnt)) { |
| 1104 | pr_err("overlayfs: failed to clone lowerpath\n"); |
| 1105 | goto out_put_lower_mnt; |
| 1106 | } |
| 1107 | /* |
| 1108 | * Make lower_mnt R/O. That way fchmod/fchown on lower file |
| 1109 | * will fail instead of modifying lower fs. |
| 1110 | */ |
| 1111 | mnt->mnt_flags |= MNT_READONLY; |
| 1112 | |
| 1113 | ufs->lower_mnt[ufs->numlower] = mnt; |
| 1114 | ufs->numlower++; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1115 | } |
| 1116 | |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 1117 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
| 1118 | if (!ufs->upper_mnt) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1119 | sb->s_flags |= MS_RDONLY; |
| 1120 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 1121 | if (remote) |
| 1122 | sb->s_d_op = &ovl_reval_dentry_operations; |
| 1123 | else |
| 1124 | sb->s_d_op = &ovl_dentry_operations; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1125 | |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 1126 | ufs->creator_cred = prepare_creds(); |
| 1127 | if (!ufs->creator_cred) |
| 1128 | goto out_put_lower_mnt; |
| 1129 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1130 | err = -ENOMEM; |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1131 | oe = ovl_alloc_entry(numlower); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1132 | if (!oe) |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 1133 | goto out_put_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1134 | |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1135 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1136 | if (!root_dentry) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1137 | goto out_free_oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1138 | |
| 1139 | mntput(upperpath.mnt); |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1140 | for (i = 0; i < numlower; i++) |
| 1141 | mntput(stack[i].mnt); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1142 | path_put(&workpath); |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1143 | kfree(lowertmp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1144 | |
| 1145 | oe->__upperdentry = upperpath.dentry; |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1146 | for (i = 0; i < numlower; i++) { |
| 1147 | oe->lowerstack[i].dentry = stack[i].dentry; |
| 1148 | oe->lowerstack[i].mnt = ufs->lower_mnt[i]; |
| 1149 | } |
Konstantin Khlebnikov | 0f95502 | 2015-08-24 15:57:18 +0300 | [diff] [blame] | 1150 | kfree(stack); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1151 | |
| 1152 | root_dentry->d_fsdata = oe; |
| 1153 | |
Miklos Szeredi | ed06e06 | 2015-12-09 16:11:59 +0100 | [diff] [blame] | 1154 | ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode, |
| 1155 | root_dentry->d_inode); |
| 1156 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1157 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1158 | sb->s_op = &ovl_super_operations; |
| 1159 | sb->s_root = root_dentry; |
| 1160 | sb->s_fs_info = ufs; |
| 1161 | |
| 1162 | return 0; |
| 1163 | |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1164 | out_free_oe: |
| 1165 | kfree(oe); |
Antonio Murdaca | 3fe6e52 | 2016-04-07 15:48:25 +0200 | [diff] [blame] | 1166 | out_put_cred: |
| 1167 | put_cred(ufs->creator_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1168 | out_put_lower_mnt: |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 1169 | for (i = 0; i < ufs->numlower; i++) |
| 1170 | mntput(ufs->lower_mnt[i]); |
| 1171 | kfree(ufs->lower_mnt); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1172 | out_put_workdir: |
| 1173 | dput(ufs->workdir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1174 | mntput(ufs->upper_mnt); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1175 | out_put_lowerpath: |
Miklos Szeredi | a78d9f0d | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1176 | for (i = 0; i < numlower; i++) |
| 1177 | path_put(&stack[i]); |
| 1178 | kfree(stack); |
| 1179 | out_free_lowertmp: |
| 1180 | kfree(lowertmp); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1181 | out_put_workpath: |
| 1182 | path_put(&workpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1183 | out_put_upperpath: |
| 1184 | path_put(&upperpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1185 | out_free_config: |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1186 | kfree(ufs->config.lowerdir); |
| 1187 | kfree(ufs->config.upperdir); |
| 1188 | kfree(ufs->config.workdir); |
| 1189 | kfree(ufs); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1190 | out: |
| 1191 | return err; |
| 1192 | } |
| 1193 | |
| 1194 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, |
| 1195 | const char *dev_name, void *raw_data) |
| 1196 | { |
| 1197 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); |
| 1198 | } |
| 1199 | |
| 1200 | static struct file_system_type ovl_fs_type = { |
| 1201 | .owner = THIS_MODULE, |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 1202 | .name = "overlay", |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1203 | .mount = ovl_mount, |
| 1204 | .kill_sb = kill_anon_super, |
| 1205 | }; |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 1206 | MODULE_ALIAS_FS("overlay"); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1207 | |
| 1208 | static int __init ovl_init(void) |
| 1209 | { |
| 1210 | return register_filesystem(&ovl_fs_type); |
| 1211 | } |
| 1212 | |
| 1213 | static void __exit ovl_exit(void) |
| 1214 | { |
| 1215 | unregister_filesystem(&ovl_fs_type); |
| 1216 | } |
| 1217 | |
| 1218 | module_init(ovl_init); |
| 1219 | module_exit(ovl_exit); |