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