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