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> |
| 12 | #include <linux/xattr.h> |
| 13 | #include <linux/security.h> |
| 14 | #include <linux/mount.h> |
| 15 | #include <linux/slab.h> |
| 16 | #include <linux/parser.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/sched.h> |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 19 | #include <linux/statfs.h> |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 20 | #include <linux/seq_file.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 21 | #include "overlayfs.h" |
| 22 | |
| 23 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); |
| 24 | MODULE_DESCRIPTION("Overlay filesystem"); |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 27 | #define OVERLAYFS_SUPER_MAGIC 0x794c7630 |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 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; |
| 33 | }; |
| 34 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 35 | /* private information held for overlayfs's superblock */ |
| 36 | struct ovl_fs { |
| 37 | struct vfsmount *upper_mnt; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 38 | unsigned numlower; |
| 39 | struct vfsmount **lower_mnt; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 40 | struct dentry *workdir; |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 41 | long lower_namelen; |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 42 | /* pathnames of lower and upper dirs, for show_options */ |
| 43 | struct ovl_config config; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct ovl_dir_cache; |
| 47 | |
| 48 | /* private information held for every overlayfs dentry */ |
| 49 | struct ovl_entry { |
| 50 | struct dentry *__upperdentry; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 51 | struct ovl_dir_cache *cache; |
| 52 | union { |
| 53 | struct { |
| 54 | u64 version; |
| 55 | bool opaque; |
| 56 | }; |
| 57 | struct rcu_head rcu; |
| 58 | }; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 59 | unsigned numlower; |
| 60 | struct path lowerstack[]; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 63 | #define OVL_MAX_STACK 500 |
| 64 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 65 | const char *ovl_opaque_xattr = "trusted.overlay.opaque"; |
| 66 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 67 | static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe) |
| 68 | { |
| 69 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; |
| 70 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 71 | |
| 72 | enum ovl_path_type ovl_path_type(struct dentry *dentry) |
| 73 | { |
| 74 | struct ovl_entry *oe = dentry->d_fsdata; |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 75 | enum ovl_path_type type = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 76 | |
| 77 | if (oe->__upperdentry) { |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 78 | type = __OVL_PATH_UPPER; |
| 79 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 80 | if (oe->numlower) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 81 | if (S_ISDIR(dentry->d_inode->i_mode)) |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 82 | type |= __OVL_PATH_MERGE; |
| 83 | } else if (!oe->opaque) { |
| 84 | type |= __OVL_PATH_PURE; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 85 | } |
Miklos Szeredi | 9d7459d | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 86 | } else { |
| 87 | if (oe->numlower > 1) |
| 88 | type |= __OVL_PATH_MERGE; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 89 | } |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 90 | return type; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) |
| 94 | { |
Miklos Szeredi | 71d5092 | 2014-11-20 16:40:01 +0100 | [diff] [blame] | 95 | return lockless_dereference(oe->__upperdentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void ovl_path_upper(struct dentry *dentry, struct path *path) |
| 99 | { |
| 100 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 101 | struct ovl_entry *oe = dentry->d_fsdata; |
| 102 | |
| 103 | path->mnt = ofs->upper_mnt; |
| 104 | path->dentry = ovl_upperdentry_dereference(oe); |
| 105 | } |
| 106 | |
| 107 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) |
| 108 | { |
| 109 | |
| 110 | enum ovl_path_type type = ovl_path_type(dentry); |
| 111 | |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 112 | if (!OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 113 | ovl_path_lower(dentry, path); |
| 114 | else |
| 115 | ovl_path_upper(dentry, path); |
| 116 | |
| 117 | return type; |
| 118 | } |
| 119 | |
| 120 | struct dentry *ovl_dentry_upper(struct dentry *dentry) |
| 121 | { |
| 122 | struct ovl_entry *oe = dentry->d_fsdata; |
| 123 | |
| 124 | return ovl_upperdentry_dereference(oe); |
| 125 | } |
| 126 | |
| 127 | struct dentry *ovl_dentry_lower(struct dentry *dentry) |
| 128 | { |
| 129 | struct ovl_entry *oe = dentry->d_fsdata; |
| 130 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 131 | return __ovl_dentry_lower(oe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | struct dentry *ovl_dentry_real(struct dentry *dentry) |
| 135 | { |
| 136 | struct ovl_entry *oe = dentry->d_fsdata; |
| 137 | struct dentry *realdentry; |
| 138 | |
| 139 | realdentry = ovl_upperdentry_dereference(oe); |
| 140 | if (!realdentry) |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 141 | realdentry = __ovl_dentry_lower(oe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 142 | |
| 143 | return realdentry; |
| 144 | } |
| 145 | |
| 146 | struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper) |
| 147 | { |
| 148 | struct dentry *realdentry; |
| 149 | |
| 150 | realdentry = ovl_upperdentry_dereference(oe); |
| 151 | if (realdentry) { |
| 152 | *is_upper = true; |
| 153 | } else { |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 154 | realdentry = __ovl_dentry_lower(oe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 155 | *is_upper = false; |
| 156 | } |
| 157 | return realdentry; |
| 158 | } |
| 159 | |
| 160 | struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) |
| 161 | { |
| 162 | struct ovl_entry *oe = dentry->d_fsdata; |
| 163 | |
| 164 | return oe->cache; |
| 165 | } |
| 166 | |
| 167 | void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) |
| 168 | { |
| 169 | struct ovl_entry *oe = dentry->d_fsdata; |
| 170 | |
| 171 | oe->cache = cache; |
| 172 | } |
| 173 | |
| 174 | void ovl_path_lower(struct dentry *dentry, struct path *path) |
| 175 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 176 | struct ovl_entry *oe = dentry->d_fsdata; |
| 177 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 178 | *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | int ovl_want_write(struct dentry *dentry) |
| 182 | { |
| 183 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 184 | return mnt_want_write(ofs->upper_mnt); |
| 185 | } |
| 186 | |
| 187 | void ovl_drop_write(struct dentry *dentry) |
| 188 | { |
| 189 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 190 | mnt_drop_write(ofs->upper_mnt); |
| 191 | } |
| 192 | |
| 193 | struct dentry *ovl_workdir(struct dentry *dentry) |
| 194 | { |
| 195 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 196 | return ofs->workdir; |
| 197 | } |
| 198 | |
| 199 | bool ovl_dentry_is_opaque(struct dentry *dentry) |
| 200 | { |
| 201 | struct ovl_entry *oe = dentry->d_fsdata; |
| 202 | return oe->opaque; |
| 203 | } |
| 204 | |
| 205 | void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) |
| 206 | { |
| 207 | struct ovl_entry *oe = dentry->d_fsdata; |
| 208 | oe->opaque = opaque; |
| 209 | } |
| 210 | |
| 211 | void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) |
| 212 | { |
| 213 | struct ovl_entry *oe = dentry->d_fsdata; |
| 214 | |
| 215 | WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex)); |
| 216 | WARN_ON(oe->__upperdentry); |
| 217 | BUG_ON(!upperdentry->d_inode); |
| 218 | /* |
| 219 | * Make sure upperdentry is consistent before making it visible to |
| 220 | * ovl_upperdentry_dereference(). |
| 221 | */ |
| 222 | smp_wmb(); |
| 223 | oe->__upperdentry = upperdentry; |
| 224 | } |
| 225 | |
| 226 | void ovl_dentry_version_inc(struct dentry *dentry) |
| 227 | { |
| 228 | struct ovl_entry *oe = dentry->d_fsdata; |
| 229 | |
| 230 | WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); |
| 231 | oe->version++; |
| 232 | } |
| 233 | |
| 234 | u64 ovl_dentry_version_get(struct dentry *dentry) |
| 235 | { |
| 236 | struct ovl_entry *oe = dentry->d_fsdata; |
| 237 | |
| 238 | WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); |
| 239 | return oe->version; |
| 240 | } |
| 241 | |
| 242 | bool ovl_is_whiteout(struct dentry *dentry) |
| 243 | { |
| 244 | struct inode *inode = dentry->d_inode; |
| 245 | |
| 246 | return inode && IS_WHITEOUT(inode); |
| 247 | } |
| 248 | |
| 249 | static bool ovl_is_opaquedir(struct dentry *dentry) |
| 250 | { |
| 251 | int res; |
| 252 | char val; |
| 253 | struct inode *inode = dentry->d_inode; |
| 254 | |
| 255 | if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) |
| 256 | return false; |
| 257 | |
| 258 | res = inode->i_op->getxattr(dentry, ovl_opaque_xattr, &val, 1); |
| 259 | if (res == 1 && val == 'y') |
| 260 | return true; |
| 261 | |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | static void ovl_dentry_release(struct dentry *dentry) |
| 266 | { |
| 267 | struct ovl_entry *oe = dentry->d_fsdata; |
| 268 | |
| 269 | if (oe) { |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 270 | unsigned int i; |
| 271 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 272 | dput(oe->__upperdentry); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 273 | for (i = 0; i < oe->numlower; i++) |
| 274 | dput(oe->lowerstack[i].dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 275 | kfree_rcu(oe, rcu); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | static const struct dentry_operations ovl_dentry_operations = { |
| 280 | .d_release = ovl_dentry_release, |
| 281 | }; |
| 282 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 283 | static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 284 | { |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 285 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); |
| 286 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); |
| 287 | |
| 288 | if (oe) |
| 289 | oe->numlower = numlower; |
| 290 | |
| 291 | return oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static inline struct dentry *ovl_lookup_real(struct dentry *dir, |
| 295 | struct qstr *name) |
| 296 | { |
| 297 | struct dentry *dentry; |
| 298 | |
| 299 | mutex_lock(&dir->d_inode->i_mutex); |
| 300 | dentry = lookup_one_len(name->name, dir, name->len); |
| 301 | mutex_unlock(&dir->d_inode->i_mutex); |
| 302 | |
| 303 | if (IS_ERR(dentry)) { |
| 304 | if (PTR_ERR(dentry) == -ENOENT) |
| 305 | dentry = NULL; |
| 306 | } else if (!dentry->d_inode) { |
| 307 | dput(dentry); |
| 308 | dentry = NULL; |
| 309 | } |
| 310 | return dentry; |
| 311 | } |
| 312 | |
Miklos Szeredi | 5ef88da | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 313 | /* |
| 314 | * Returns next layer in stack starting from top. |
| 315 | * Returns -1 if this is the last layer. |
| 316 | */ |
| 317 | int ovl_path_next(int idx, struct dentry *dentry, struct path *path) |
| 318 | { |
| 319 | struct ovl_entry *oe = dentry->d_fsdata; |
| 320 | |
| 321 | BUG_ON(idx < 0); |
| 322 | if (idx == 0) { |
| 323 | ovl_path_upper(dentry, path); |
| 324 | if (path->dentry) |
| 325 | return oe->numlower ? 1 : -1; |
| 326 | idx++; |
| 327 | } |
| 328 | BUG_ON(idx > oe->numlower); |
| 329 | *path = oe->lowerstack[idx - 1]; |
| 330 | |
| 331 | return (idx < oe->numlower) ? idx + 1 : -1; |
| 332 | } |
| 333 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 334 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
| 335 | unsigned int flags) |
| 336 | { |
| 337 | struct ovl_entry *oe; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 338 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
| 339 | struct path *stack = NULL; |
| 340 | struct dentry *upperdir, *upperdentry = NULL; |
| 341 | unsigned int ctr = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 342 | struct inode *inode = NULL; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 343 | bool upperopaque = false; |
| 344 | struct dentry *this, *prev = NULL; |
| 345 | unsigned int i; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 346 | int err; |
| 347 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 348 | upperdir = ovl_upperdentry_dereference(poe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 349 | if (upperdir) { |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 350 | this = ovl_lookup_real(upperdir, &dentry->d_name); |
| 351 | err = PTR_ERR(this); |
| 352 | if (IS_ERR(this)) |
| 353 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 354 | |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 355 | if (this) { |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 356 | if (ovl_is_whiteout(this)) { |
| 357 | dput(this); |
| 358 | this = NULL; |
| 359 | upperopaque = true; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 360 | } else if (poe->numlower && ovl_is_opaquedir(this)) { |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 361 | upperopaque = true; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 362 | } |
| 363 | } |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 364 | upperdentry = prev = this; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 365 | } |
| 366 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 367 | if (!upperopaque && poe->numlower) { |
| 368 | err = -ENOMEM; |
| 369 | stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL); |
| 370 | if (!stack) |
| 371 | goto out_put_upper; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 374 | for (i = 0; !upperopaque && i < poe->numlower; i++) { |
| 375 | bool opaque = false; |
| 376 | struct path lowerpath = poe->lowerstack[i]; |
| 377 | |
| 378 | opaque = false; |
| 379 | this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name); |
| 380 | err = PTR_ERR(this); |
Miklos Szeredi | 09e1032 | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 381 | if (IS_ERR(this)) { |
| 382 | /* |
| 383 | * If it's positive, then treat ENAMETOOLONG as ENOENT. |
| 384 | */ |
| 385 | if (err == -ENAMETOOLONG && (upperdentry || ctr)) |
| 386 | continue; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 387 | goto out_put; |
Miklos Szeredi | 09e1032 | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 388 | } |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 389 | if (!this) |
| 390 | continue; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 391 | if (ovl_is_whiteout(this)) { |
| 392 | dput(this); |
| 393 | break; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 394 | } |
| 395 | /* |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 396 | * Only makes sense to check opaque dir if this is not the |
| 397 | * lowermost layer. |
| 398 | */ |
| 399 | if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) |
| 400 | opaque = true; |
| 401 | /* |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 402 | * If this is a non-directory then stop here. |
| 403 | * |
| 404 | * FIXME: check for opaqueness maybe better done in remove code. |
| 405 | */ |
| 406 | if (!S_ISDIR(this->d_inode->i_mode)) { |
| 407 | opaque = true; |
| 408 | } else if (prev && (!S_ISDIR(prev->d_inode->i_mode) || |
| 409 | !S_ISDIR(this->d_inode->i_mode))) { |
| 410 | if (prev == upperdentry) |
| 411 | upperopaque = true; |
| 412 | dput(this); |
| 413 | break; |
| 414 | } |
| 415 | stack[ctr].dentry = this; |
| 416 | stack[ctr].mnt = lowerpath.mnt; |
| 417 | ctr++; |
| 418 | prev = this; |
| 419 | if (opaque) |
| 420 | break; |
| 421 | } |
| 422 | |
| 423 | oe = ovl_alloc_entry(ctr); |
| 424 | err = -ENOMEM; |
| 425 | if (!oe) |
| 426 | goto out_put; |
| 427 | |
| 428 | if (upperdentry || ctr) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 429 | struct dentry *realdentry; |
| 430 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 431 | realdentry = upperdentry ? upperdentry : stack[0].dentry; |
| 432 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 433 | err = -ENOMEM; |
| 434 | inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, |
| 435 | oe); |
| 436 | if (!inode) |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 437 | goto out_free_oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 438 | ovl_copyattr(realdentry->d_inode, inode); |
| 439 | } |
| 440 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 441 | oe->opaque = upperopaque; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 442 | oe->__upperdentry = upperdentry; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 443 | memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); |
| 444 | kfree(stack); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 445 | dentry->d_fsdata = oe; |
| 446 | d_add(dentry, inode); |
| 447 | |
| 448 | return NULL; |
| 449 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 450 | out_free_oe: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 451 | kfree(oe); |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 452 | out_put: |
| 453 | for (i = 0; i < ctr; i++) |
| 454 | dput(stack[i].dentry); |
| 455 | kfree(stack); |
| 456 | out_put_upper: |
| 457 | dput(upperdentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 458 | out: |
| 459 | return ERR_PTR(err); |
| 460 | } |
| 461 | |
| 462 | struct file *ovl_path_open(struct path *path, int flags) |
| 463 | { |
| 464 | return dentry_open(path, flags, current_cred()); |
| 465 | } |
| 466 | |
| 467 | static void ovl_put_super(struct super_block *sb) |
| 468 | { |
| 469 | struct ovl_fs *ufs = sb->s_fs_info; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 470 | unsigned i; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 471 | |
| 472 | dput(ufs->workdir); |
| 473 | mntput(ufs->upper_mnt); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 474 | for (i = 0; i < ufs->numlower; i++) |
| 475 | mntput(ufs->lower_mnt[i]); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 476 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 477 | kfree(ufs->config.lowerdir); |
| 478 | kfree(ufs->config.upperdir); |
| 479 | kfree(ufs->config.workdir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 480 | kfree(ufs); |
| 481 | } |
| 482 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 483 | /** |
| 484 | * ovl_statfs |
| 485 | * @sb: The overlayfs super block |
| 486 | * @buf: The struct kstatfs to fill in with stats |
| 487 | * |
| 488 | * Get the filesystem statistics. As writes always target the upper layer |
Miklos Szeredi | 4ebc581 | 2014-12-13 00:59:46 +0100 | [diff] [blame] | 489 | * filesystem pass the statfs to the upper filesystem (if it exists) |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 490 | */ |
| 491 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 492 | { |
| 493 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 494 | struct dentry *root_dentry = dentry->d_sb->s_root; |
| 495 | struct path path; |
| 496 | int err; |
| 497 | |
Miklos Szeredi | 4ebc581 | 2014-12-13 00:59:46 +0100 | [diff] [blame] | 498 | ovl_path_real(root_dentry, &path); |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 499 | |
| 500 | err = vfs_statfs(&path, buf); |
| 501 | if (!err) { |
| 502 | buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); |
| 503 | buf->f_type = OVERLAYFS_SUPER_MAGIC; |
| 504 | } |
| 505 | |
| 506 | return err; |
| 507 | } |
| 508 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 509 | /** |
| 510 | * ovl_show_options |
| 511 | * |
| 512 | * Prints the mount options for a given superblock. |
| 513 | * Returns zero; does not fail. |
| 514 | */ |
| 515 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) |
| 516 | { |
| 517 | struct super_block *sb = dentry->d_sb; |
| 518 | struct ovl_fs *ufs = sb->s_fs_info; |
| 519 | |
| 520 | seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir); |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 521 | if (ufs->config.upperdir) { |
| 522 | seq_printf(m, ",upperdir=%s", ufs->config.upperdir); |
| 523 | seq_printf(m, ",workdir=%s", ufs->config.workdir); |
| 524 | } |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 525 | return 0; |
| 526 | } |
| 527 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 528 | static const struct super_operations ovl_super_operations = { |
| 529 | .put_super = ovl_put_super, |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 530 | .statfs = ovl_statfs, |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 531 | .show_options = ovl_show_options, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 532 | }; |
| 533 | |
| 534 | enum { |
| 535 | OPT_LOWERDIR, |
| 536 | OPT_UPPERDIR, |
| 537 | OPT_WORKDIR, |
| 538 | OPT_ERR, |
| 539 | }; |
| 540 | |
| 541 | static const match_table_t ovl_tokens = { |
| 542 | {OPT_LOWERDIR, "lowerdir=%s"}, |
| 543 | {OPT_UPPERDIR, "upperdir=%s"}, |
| 544 | {OPT_WORKDIR, "workdir=%s"}, |
| 545 | {OPT_ERR, NULL} |
| 546 | }; |
| 547 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 548 | static char *ovl_next_opt(char **s) |
| 549 | { |
| 550 | char *sbegin = *s; |
| 551 | char *p; |
| 552 | |
| 553 | if (sbegin == NULL) |
| 554 | return NULL; |
| 555 | |
| 556 | for (p = sbegin; *p; p++) { |
| 557 | if (*p == '\\') { |
| 558 | p++; |
| 559 | if (!*p) |
| 560 | break; |
| 561 | } else if (*p == ',') { |
| 562 | *p = '\0'; |
| 563 | *s = p + 1; |
| 564 | return sbegin; |
| 565 | } |
| 566 | } |
| 567 | *s = NULL; |
| 568 | return sbegin; |
| 569 | } |
| 570 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 571 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
| 572 | { |
| 573 | char *p; |
| 574 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 575 | while ((p = ovl_next_opt(&opt)) != NULL) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 576 | int token; |
| 577 | substring_t args[MAX_OPT_ARGS]; |
| 578 | |
| 579 | if (!*p) |
| 580 | continue; |
| 581 | |
| 582 | token = match_token(p, ovl_tokens, args); |
| 583 | switch (token) { |
| 584 | case OPT_UPPERDIR: |
| 585 | kfree(config->upperdir); |
| 586 | config->upperdir = match_strdup(&args[0]); |
| 587 | if (!config->upperdir) |
| 588 | return -ENOMEM; |
| 589 | break; |
| 590 | |
| 591 | case OPT_LOWERDIR: |
| 592 | kfree(config->lowerdir); |
| 593 | config->lowerdir = match_strdup(&args[0]); |
| 594 | if (!config->lowerdir) |
| 595 | return -ENOMEM; |
| 596 | break; |
| 597 | |
| 598 | case OPT_WORKDIR: |
| 599 | kfree(config->workdir); |
| 600 | config->workdir = match_strdup(&args[0]); |
| 601 | if (!config->workdir) |
| 602 | return -ENOMEM; |
| 603 | break; |
| 604 | |
| 605 | default: |
| 606 | return -EINVAL; |
| 607 | } |
| 608 | } |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | #define OVL_WORKDIR_NAME "work" |
| 613 | |
| 614 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, |
| 615 | struct dentry *dentry) |
| 616 | { |
| 617 | struct inode *dir = dentry->d_inode; |
| 618 | struct dentry *work; |
| 619 | int err; |
| 620 | bool retried = false; |
| 621 | |
| 622 | err = mnt_want_write(mnt); |
| 623 | if (err) |
| 624 | return ERR_PTR(err); |
| 625 | |
| 626 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
| 627 | retry: |
| 628 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, |
| 629 | strlen(OVL_WORKDIR_NAME)); |
| 630 | |
| 631 | if (!IS_ERR(work)) { |
| 632 | struct kstat stat = { |
| 633 | .mode = S_IFDIR | 0, |
| 634 | }; |
| 635 | |
| 636 | if (work->d_inode) { |
| 637 | err = -EEXIST; |
| 638 | if (retried) |
| 639 | goto out_dput; |
| 640 | |
| 641 | retried = true; |
| 642 | ovl_cleanup(dir, work); |
| 643 | dput(work); |
| 644 | goto retry; |
| 645 | } |
| 646 | |
| 647 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); |
| 648 | if (err) |
| 649 | goto out_dput; |
| 650 | } |
| 651 | out_unlock: |
| 652 | mutex_unlock(&dir->i_mutex); |
| 653 | mnt_drop_write(mnt); |
| 654 | |
| 655 | return work; |
| 656 | |
| 657 | out_dput: |
| 658 | dput(work); |
| 659 | work = ERR_PTR(err); |
| 660 | goto out_unlock; |
| 661 | } |
| 662 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 663 | static void ovl_unescape(char *s) |
| 664 | { |
| 665 | char *d = s; |
| 666 | |
| 667 | for (;; s++, d++) { |
| 668 | if (*s == '\\') |
| 669 | s++; |
| 670 | *d = *s; |
| 671 | if (!*s) |
| 672 | break; |
| 673 | } |
| 674 | } |
| 675 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 676 | static bool ovl_is_allowed_fs_type(struct dentry *root) |
| 677 | { |
| 678 | const struct dentry_operations *dop = root->d_op; |
| 679 | |
| 680 | /* |
| 681 | * We don't support: |
| 682 | * - automount filesystems |
| 683 | * - filesystems with revalidate (FIXME for lower layer) |
| 684 | * - filesystems with case insensitive names |
| 685 | */ |
| 686 | if (dop && |
| 687 | (dop->d_manage || dop->d_automount || |
| 688 | dop->d_revalidate || dop->d_weak_revalidate || |
| 689 | dop->d_compare || dop->d_hash)) { |
| 690 | return false; |
| 691 | } |
| 692 | return true; |
| 693 | } |
| 694 | |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 695 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
| 696 | { |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 697 | int err = -EINVAL; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 698 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 699 | if (!*name) { |
| 700 | pr_err("overlayfs: empty lowerdir\n"); |
| 701 | goto out; |
| 702 | } |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 703 | err = kern_path(name, LOOKUP_FOLLOW, path); |
| 704 | if (err) { |
| 705 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); |
| 706 | goto out; |
| 707 | } |
| 708 | err = -EINVAL; |
| 709 | if (!ovl_is_allowed_fs_type(path->dentry)) { |
| 710 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); |
| 711 | goto out_put; |
| 712 | } |
| 713 | if (!S_ISDIR(path->dentry->d_inode->i_mode)) { |
| 714 | pr_err("overlayfs: '%s' not a directory\n", name); |
| 715 | goto out_put; |
| 716 | } |
| 717 | return 0; |
| 718 | |
| 719 | out_put: |
| 720 | path_put(path); |
| 721 | out: |
| 722 | return err; |
| 723 | } |
| 724 | |
| 725 | static int ovl_mount_dir(const char *name, struct path *path) |
| 726 | { |
| 727 | int err = -ENOMEM; |
| 728 | char *tmp = kstrdup(name, GFP_KERNEL); |
| 729 | |
| 730 | if (tmp) { |
| 731 | ovl_unescape(tmp); |
| 732 | err = ovl_mount_dir_noesc(tmp, path); |
| 733 | kfree(tmp); |
| 734 | } |
| 735 | return err; |
| 736 | } |
| 737 | |
| 738 | static int ovl_lower_dir(const char *name, struct path *path, long *namelen, |
| 739 | int *stack_depth) |
| 740 | { |
| 741 | int err; |
| 742 | struct kstatfs statfs; |
| 743 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 744 | err = ovl_mount_dir_noesc(name, path); |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 745 | if (err) |
| 746 | goto out; |
| 747 | |
| 748 | err = vfs_statfs(path, &statfs); |
| 749 | if (err) { |
| 750 | pr_err("overlayfs: statfs failed on '%s'\n", name); |
| 751 | goto out_put; |
| 752 | } |
| 753 | *namelen = max(*namelen, statfs.f_namelen); |
| 754 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); |
| 755 | |
| 756 | return 0; |
| 757 | |
| 758 | out_put: |
| 759 | path_put(path); |
| 760 | out: |
| 761 | return err; |
| 762 | } |
| 763 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 764 | /* Workdir should not be subdir of upperdir and vice versa */ |
| 765 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) |
| 766 | { |
| 767 | bool ok = false; |
| 768 | |
| 769 | if (workdir != upperdir) { |
| 770 | ok = (lock_rename(workdir, upperdir) == NULL); |
| 771 | unlock_rename(workdir, upperdir); |
| 772 | } |
| 773 | return ok; |
| 774 | } |
| 775 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 776 | static unsigned int ovl_split_lowerdirs(char *str) |
| 777 | { |
| 778 | unsigned int ctr = 1; |
| 779 | char *s, *d; |
| 780 | |
| 781 | for (s = d = str;; s++, d++) { |
| 782 | if (*s == '\\') { |
| 783 | s++; |
| 784 | } else if (*s == ':') { |
| 785 | *d = '\0'; |
| 786 | ctr++; |
| 787 | continue; |
| 788 | } |
| 789 | *d = *s; |
| 790 | if (!*s) |
| 791 | break; |
| 792 | } |
| 793 | return ctr; |
| 794 | } |
| 795 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 796 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
| 797 | { |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 798 | struct path upperpath = { NULL, NULL }; |
| 799 | struct path workpath = { NULL, NULL }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 800 | struct dentry *root_dentry; |
| 801 | struct ovl_entry *oe; |
| 802 | struct ovl_fs *ufs; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 803 | struct path *stack = NULL; |
| 804 | char *lowertmp; |
| 805 | char *lower; |
| 806 | unsigned int numlower; |
| 807 | unsigned int stacklen = 0; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 808 | unsigned int i; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 809 | int err; |
| 810 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 811 | err = -ENOMEM; |
| 812 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); |
| 813 | if (!ufs) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 814 | goto out; |
| 815 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 816 | err = ovl_parse_opt((char *) data, &ufs->config); |
| 817 | if (err) |
| 818 | goto out_free_config; |
| 819 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 820 | err = -EINVAL; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 821 | if (!ufs->config.lowerdir) { |
| 822 | pr_err("overlayfs: missing 'lowerdir'\n"); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 823 | goto out_free_config; |
| 824 | } |
| 825 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 826 | sb->s_stack_depth = 0; |
| 827 | if (ufs->config.upperdir) { |
| 828 | /* FIXME: workdir is not needed for a R/O mount */ |
| 829 | if (!ufs->config.workdir) { |
| 830 | pr_err("overlayfs: missing 'workdir'\n"); |
| 831 | goto out_free_config; |
| 832 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 833 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 834 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
| 835 | if (err) |
| 836 | goto out_free_config; |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 837 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 838 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
| 839 | if (err) |
| 840 | goto out_put_upperpath; |
| 841 | |
| 842 | if (upperpath.mnt != workpath.mnt) { |
| 843 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); |
| 844 | goto out_put_workpath; |
| 845 | } |
| 846 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { |
| 847 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); |
| 848 | goto out_put_workpath; |
| 849 | } |
| 850 | sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 851 | } |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 852 | err = -ENOMEM; |
| 853 | lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); |
| 854 | if (!lowertmp) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 855 | goto out_put_workpath; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 856 | |
| 857 | err = -EINVAL; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 858 | stacklen = ovl_split_lowerdirs(lowertmp); |
| 859 | if (stacklen > OVL_MAX_STACK) |
| 860 | goto out_free_lowertmp; |
| 861 | |
| 862 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); |
| 863 | if (!stack) |
| 864 | goto out_free_lowertmp; |
| 865 | |
| 866 | lower = lowertmp; |
| 867 | for (numlower = 0; numlower < stacklen; numlower++) { |
| 868 | err = ovl_lower_dir(lower, &stack[numlower], |
| 869 | &ufs->lower_namelen, &sb->s_stack_depth); |
| 870 | if (err) |
| 871 | goto out_put_lowerpath; |
| 872 | |
| 873 | lower = strchr(lower, '\0') + 1; |
| 874 | } |
| 875 | |
| 876 | err = -EINVAL; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 877 | sb->s_stack_depth++; |
Miklos Szeredi | 69c433e | 2014-10-24 00:14:39 +0200 | [diff] [blame] | 878 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
| 879 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 880 | goto out_put_lowerpath; |
Miklos Szeredi | 69c433e | 2014-10-24 00:14:39 +0200 | [diff] [blame] | 881 | } |
| 882 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 883 | if (ufs->config.upperdir) { |
| 884 | ufs->upper_mnt = clone_private_mount(&upperpath); |
| 885 | err = PTR_ERR(ufs->upper_mnt); |
| 886 | if (IS_ERR(ufs->upper_mnt)) { |
| 887 | pr_err("overlayfs: failed to clone upperpath\n"); |
| 888 | goto out_put_lowerpath; |
| 889 | } |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 890 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 891 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); |
| 892 | err = PTR_ERR(ufs->workdir); |
| 893 | if (IS_ERR(ufs->workdir)) { |
| 894 | pr_err("overlayfs: failed to create directory %s/%s\n", |
| 895 | ufs->config.workdir, OVL_WORKDIR_NAME); |
| 896 | goto out_put_upper_mnt; |
| 897 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 898 | } |
| 899 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 900 | ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 901 | if (ufs->lower_mnt == NULL) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 902 | goto out_put_workdir; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 903 | for (i = 0; i < numlower; i++) { |
| 904 | struct vfsmount *mnt = clone_private_mount(&stack[i]); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 905 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 906 | if (IS_ERR(mnt)) { |
| 907 | pr_err("overlayfs: failed to clone lowerpath\n"); |
| 908 | goto out_put_lower_mnt; |
| 909 | } |
| 910 | /* |
| 911 | * Make lower_mnt R/O. That way fchmod/fchown on lower file |
| 912 | * will fail instead of modifying lower fs. |
| 913 | */ |
| 914 | mnt->mnt_flags |= MNT_READONLY; |
| 915 | |
| 916 | ufs->lower_mnt[ufs->numlower] = mnt; |
| 917 | ufs->numlower++; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 918 | } |
| 919 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 920 | /* If the upper fs is r/o or nonexistent, we mark overlayfs r/o too */ |
| 921 | if (!ufs->upper_mnt || (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 922 | sb->s_flags |= MS_RDONLY; |
| 923 | |
| 924 | sb->s_d_op = &ovl_dentry_operations; |
| 925 | |
| 926 | err = -ENOMEM; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 927 | oe = ovl_alloc_entry(numlower); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 928 | if (!oe) |
| 929 | goto out_put_lower_mnt; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 930 | |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 931 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 932 | if (!root_dentry) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 933 | goto out_free_oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 934 | |
| 935 | mntput(upperpath.mnt); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 936 | for (i = 0; i < numlower; i++) |
| 937 | mntput(stack[i].mnt); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 938 | path_put(&workpath); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 939 | kfree(lowertmp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 940 | |
| 941 | oe->__upperdentry = upperpath.dentry; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 942 | for (i = 0; i < numlower; i++) { |
| 943 | oe->lowerstack[i].dentry = stack[i].dentry; |
| 944 | oe->lowerstack[i].mnt = ufs->lower_mnt[i]; |
| 945 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 946 | |
| 947 | root_dentry->d_fsdata = oe; |
| 948 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 949 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 950 | sb->s_op = &ovl_super_operations; |
| 951 | sb->s_root = root_dentry; |
| 952 | sb->s_fs_info = ufs; |
| 953 | |
| 954 | return 0; |
| 955 | |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 956 | out_free_oe: |
| 957 | kfree(oe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 958 | out_put_lower_mnt: |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 959 | for (i = 0; i < ufs->numlower; i++) |
| 960 | mntput(ufs->lower_mnt[i]); |
| 961 | kfree(ufs->lower_mnt); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 962 | out_put_workdir: |
| 963 | dput(ufs->workdir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 964 | out_put_upper_mnt: |
| 965 | mntput(ufs->upper_mnt); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 966 | out_put_lowerpath: |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame^] | 967 | for (i = 0; i < numlower; i++) |
| 968 | path_put(&stack[i]); |
| 969 | kfree(stack); |
| 970 | out_free_lowertmp: |
| 971 | kfree(lowertmp); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 972 | out_put_workpath: |
| 973 | path_put(&workpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 974 | out_put_upperpath: |
| 975 | path_put(&upperpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 976 | out_free_config: |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 977 | kfree(ufs->config.lowerdir); |
| 978 | kfree(ufs->config.upperdir); |
| 979 | kfree(ufs->config.workdir); |
| 980 | kfree(ufs); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 981 | out: |
| 982 | return err; |
| 983 | } |
| 984 | |
| 985 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, |
| 986 | const char *dev_name, void *raw_data) |
| 987 | { |
| 988 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); |
| 989 | } |
| 990 | |
| 991 | static struct file_system_type ovl_fs_type = { |
| 992 | .owner = THIS_MODULE, |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 993 | .name = "overlay", |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 994 | .mount = ovl_mount, |
| 995 | .kill_sb = kill_anon_super, |
| 996 | }; |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 997 | MODULE_ALIAS_FS("overlay"); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 998 | |
| 999 | static int __init ovl_init(void) |
| 1000 | { |
| 1001 | return register_filesystem(&ovl_fs_type); |
| 1002 | } |
| 1003 | |
| 1004 | static void __exit ovl_exit(void) |
| 1005 | { |
| 1006 | unregister_filesystem(&ovl_fs_type); |
| 1007 | } |
| 1008 | |
| 1009 | module_init(ovl_init); |
| 1010 | module_exit(ovl_exit); |