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> |
Andrew Morton | e458bcd | 2016-01-20 15:01:13 -0800 | [diff] [blame^] | 18 | #include <linux/pagemap.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 19 | #include <linux/sched.h> |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 20 | #include <linux/statfs.h> |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 21 | #include <linux/seq_file.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 22 | #include "overlayfs.h" |
| 23 | |
| 24 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); |
| 25 | MODULE_DESCRIPTION("Overlay filesystem"); |
| 26 | MODULE_LICENSE("GPL"); |
| 27 | |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 28 | #define OVERLAYFS_SUPER_MAGIC 0x794c7630 |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 29 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 30 | struct ovl_config { |
| 31 | char *lowerdir; |
| 32 | char *upperdir; |
| 33 | char *workdir; |
| 34 | }; |
| 35 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 36 | /* private information held for overlayfs's superblock */ |
| 37 | struct ovl_fs { |
| 38 | struct vfsmount *upper_mnt; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 39 | unsigned numlower; |
| 40 | struct vfsmount **lower_mnt; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 41 | struct dentry *workdir; |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 42 | long lower_namelen; |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 43 | /* pathnames of lower and upper dirs, for show_options */ |
| 44 | struct ovl_config config; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | struct ovl_dir_cache; |
| 48 | |
| 49 | /* private information held for every overlayfs dentry */ |
| 50 | struct ovl_entry { |
| 51 | struct dentry *__upperdentry; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 52 | struct ovl_dir_cache *cache; |
| 53 | union { |
| 54 | struct { |
| 55 | u64 version; |
| 56 | bool opaque; |
| 57 | }; |
| 58 | struct rcu_head rcu; |
| 59 | }; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 60 | unsigned numlower; |
| 61 | struct path lowerstack[]; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 64 | #define OVL_MAX_STACK 500 |
| 65 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 66 | static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe) |
| 67 | { |
| 68 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; |
| 69 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 70 | |
| 71 | enum ovl_path_type ovl_path_type(struct dentry *dentry) |
| 72 | { |
| 73 | struct ovl_entry *oe = dentry->d_fsdata; |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 74 | enum ovl_path_type type = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 75 | |
| 76 | if (oe->__upperdentry) { |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 77 | type = __OVL_PATH_UPPER; |
| 78 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 79 | if (oe->numlower) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 80 | if (S_ISDIR(dentry->d_inode->i_mode)) |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 81 | type |= __OVL_PATH_MERGE; |
| 82 | } else if (!oe->opaque) { |
| 83 | type |= __OVL_PATH_PURE; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 84 | } |
Miklos Szeredi | 9d7459d | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 85 | } else { |
| 86 | if (oe->numlower > 1) |
| 87 | type |= __OVL_PATH_MERGE; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 88 | } |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 89 | return type; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) |
| 93 | { |
Miklos Szeredi | 71d5092 | 2014-11-20 16:40:01 +0100 | [diff] [blame] | 94 | return lockless_dereference(oe->__upperdentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ovl_path_upper(struct dentry *dentry, struct path *path) |
| 98 | { |
| 99 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 100 | struct ovl_entry *oe = dentry->d_fsdata; |
| 101 | |
| 102 | path->mnt = ofs->upper_mnt; |
| 103 | path->dentry = ovl_upperdentry_dereference(oe); |
| 104 | } |
| 105 | |
| 106 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) |
| 107 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 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 | |
hujianyang | cead89b | 2014-11-24 18:25:21 +0800 | [diff] [blame] | 256 | res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 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 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 277 | static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) |
| 278 | { |
| 279 | struct ovl_entry *oe = dentry->d_fsdata; |
| 280 | unsigned int i; |
| 281 | int ret = 1; |
| 282 | |
| 283 | for (i = 0; i < oe->numlower; i++) { |
| 284 | struct dentry *d = oe->lowerstack[i].dentry; |
| 285 | |
| 286 | if (d->d_flags & DCACHE_OP_REVALIDATE) { |
| 287 | ret = d->d_op->d_revalidate(d, flags); |
| 288 | if (ret < 0) |
| 289 | return ret; |
| 290 | if (!ret) { |
| 291 | if (!(flags & LOOKUP_RCU)) |
| 292 | d_invalidate(d); |
| 293 | return -ESTALE; |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | return 1; |
| 298 | } |
| 299 | |
| 300 | static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) |
| 301 | { |
| 302 | struct ovl_entry *oe = dentry->d_fsdata; |
| 303 | unsigned int i; |
| 304 | int ret = 1; |
| 305 | |
| 306 | for (i = 0; i < oe->numlower; i++) { |
| 307 | struct dentry *d = oe->lowerstack[i].dentry; |
| 308 | |
| 309 | if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { |
| 310 | ret = d->d_op->d_weak_revalidate(d, flags); |
| 311 | if (ret <= 0) |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | return ret; |
| 316 | } |
| 317 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 318 | static const struct dentry_operations ovl_dentry_operations = { |
| 319 | .d_release = ovl_dentry_release, |
David Howells | 4bacc9c | 2015-06-18 14:32:31 +0100 | [diff] [blame] | 320 | .d_select_inode = ovl_d_select_inode, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 321 | }; |
| 322 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 323 | static const struct dentry_operations ovl_reval_dentry_operations = { |
| 324 | .d_release = ovl_dentry_release, |
| 325 | .d_revalidate = ovl_dentry_revalidate, |
| 326 | .d_weak_revalidate = ovl_dentry_weak_revalidate, |
| 327 | }; |
| 328 | |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 329 | static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 330 | { |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 331 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); |
| 332 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); |
| 333 | |
| 334 | if (oe) |
| 335 | oe->numlower = numlower; |
| 336 | |
| 337 | return oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 338 | } |
| 339 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 340 | static bool ovl_dentry_remote(struct dentry *dentry) |
| 341 | { |
| 342 | return dentry->d_flags & |
| 343 | (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE); |
| 344 | } |
| 345 | |
| 346 | static bool ovl_dentry_weird(struct dentry *dentry) |
| 347 | { |
| 348 | return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | |
| 349 | DCACHE_MANAGE_TRANSIT | |
| 350 | DCACHE_OP_HASH | |
| 351 | DCACHE_OP_COMPARE); |
| 352 | } |
| 353 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 354 | static inline struct dentry *ovl_lookup_real(struct dentry *dir, |
| 355 | struct qstr *name) |
| 356 | { |
| 357 | struct dentry *dentry; |
| 358 | |
| 359 | mutex_lock(&dir->d_inode->i_mutex); |
| 360 | dentry = lookup_one_len(name->name, dir, name->len); |
| 361 | mutex_unlock(&dir->d_inode->i_mutex); |
| 362 | |
| 363 | if (IS_ERR(dentry)) { |
| 364 | if (PTR_ERR(dentry) == -ENOENT) |
| 365 | dentry = NULL; |
| 366 | } else if (!dentry->d_inode) { |
| 367 | dput(dentry); |
| 368 | dentry = NULL; |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 369 | } else if (ovl_dentry_weird(dentry)) { |
Miklos Szeredi | a6f15d9 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 370 | dput(dentry); |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 371 | /* Don't support traversing automounts and other weirdness */ |
Miklos Szeredi | a6f15d9 | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 372 | dentry = ERR_PTR(-EREMOTE); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 373 | } |
| 374 | return dentry; |
| 375 | } |
| 376 | |
Miklos Szeredi | 5ef88da | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 377 | /* |
| 378 | * Returns next layer in stack starting from top. |
| 379 | * Returns -1 if this is the last layer. |
| 380 | */ |
| 381 | int ovl_path_next(int idx, struct dentry *dentry, struct path *path) |
| 382 | { |
| 383 | struct ovl_entry *oe = dentry->d_fsdata; |
| 384 | |
| 385 | BUG_ON(idx < 0); |
| 386 | if (idx == 0) { |
| 387 | ovl_path_upper(dentry, path); |
| 388 | if (path->dentry) |
| 389 | return oe->numlower ? 1 : -1; |
| 390 | idx++; |
| 391 | } |
| 392 | BUG_ON(idx > oe->numlower); |
| 393 | *path = oe->lowerstack[idx - 1]; |
| 394 | |
| 395 | return (idx < oe->numlower) ? idx + 1 : -1; |
| 396 | } |
| 397 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 398 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
| 399 | unsigned int flags) |
| 400 | { |
| 401 | struct ovl_entry *oe; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 402 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
| 403 | struct path *stack = NULL; |
| 404 | struct dentry *upperdir, *upperdentry = NULL; |
| 405 | unsigned int ctr = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 406 | struct inode *inode = NULL; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 407 | bool upperopaque = false; |
| 408 | struct dentry *this, *prev = NULL; |
| 409 | unsigned int i; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 410 | int err; |
| 411 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 412 | upperdir = ovl_upperdentry_dereference(poe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 413 | if (upperdir) { |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 414 | this = ovl_lookup_real(upperdir, &dentry->d_name); |
| 415 | err = PTR_ERR(this); |
| 416 | if (IS_ERR(this)) |
| 417 | goto out; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 418 | |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 419 | if (this) { |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 420 | if (unlikely(ovl_dentry_remote(this))) { |
| 421 | dput(this); |
| 422 | err = -EREMOTE; |
| 423 | goto out; |
| 424 | } |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 425 | if (ovl_is_whiteout(this)) { |
| 426 | dput(this); |
| 427 | this = NULL; |
| 428 | upperopaque = true; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 429 | } else if (poe->numlower && ovl_is_opaquedir(this)) { |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 430 | upperopaque = true; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 431 | } |
| 432 | } |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 433 | upperdentry = prev = this; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 436 | if (!upperopaque && poe->numlower) { |
| 437 | err = -ENOMEM; |
| 438 | stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL); |
| 439 | if (!stack) |
| 440 | goto out_put_upper; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 441 | } |
| 442 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 443 | for (i = 0; !upperopaque && i < poe->numlower; i++) { |
| 444 | bool opaque = false; |
| 445 | struct path lowerpath = poe->lowerstack[i]; |
| 446 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 447 | this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name); |
| 448 | err = PTR_ERR(this); |
Miklos Szeredi | 09e1032 | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 449 | if (IS_ERR(this)) { |
| 450 | /* |
| 451 | * If it's positive, then treat ENAMETOOLONG as ENOENT. |
| 452 | */ |
| 453 | if (err == -ENAMETOOLONG && (upperdentry || ctr)) |
| 454 | continue; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 455 | goto out_put; |
Miklos Szeredi | 09e1032 | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 456 | } |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 457 | if (!this) |
| 458 | continue; |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 459 | if (ovl_is_whiteout(this)) { |
| 460 | dput(this); |
| 461 | break; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 462 | } |
| 463 | /* |
Miklos Szeredi | 3e01cee | 2014-12-13 00:59:45 +0100 | [diff] [blame] | 464 | * Only makes sense to check opaque dir if this is not the |
| 465 | * lowermost layer. |
| 466 | */ |
| 467 | if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) |
| 468 | opaque = true; |
hujianyang | a425c03 | 2015-01-06 16:10:01 +0800 | [diff] [blame] | 469 | |
| 470 | if (prev && (!S_ISDIR(prev->d_inode->i_mode) || |
| 471 | !S_ISDIR(this->d_inode->i_mode))) { |
| 472 | /* |
| 473 | * FIXME: check for upper-opaqueness maybe better done |
| 474 | * in remove code. |
| 475 | */ |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 476 | if (prev == upperdentry) |
| 477 | upperopaque = true; |
| 478 | dput(this); |
| 479 | break; |
| 480 | } |
hujianyang | a425c03 | 2015-01-06 16:10:01 +0800 | [diff] [blame] | 481 | /* |
| 482 | * If this is a non-directory then stop here. |
| 483 | */ |
| 484 | if (!S_ISDIR(this->d_inode->i_mode)) |
| 485 | opaque = true; |
| 486 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 487 | stack[ctr].dentry = this; |
| 488 | stack[ctr].mnt = lowerpath.mnt; |
| 489 | ctr++; |
| 490 | prev = this; |
| 491 | if (opaque) |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | oe = ovl_alloc_entry(ctr); |
| 496 | err = -ENOMEM; |
| 497 | if (!oe) |
| 498 | goto out_put; |
| 499 | |
| 500 | if (upperdentry || ctr) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 501 | struct dentry *realdentry; |
| 502 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 503 | realdentry = upperdentry ? upperdentry : stack[0].dentry; |
| 504 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 505 | err = -ENOMEM; |
| 506 | inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, |
| 507 | oe); |
| 508 | if (!inode) |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 509 | goto out_free_oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 510 | ovl_copyattr(realdentry->d_inode, inode); |
| 511 | } |
| 512 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 513 | oe->opaque = upperopaque; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 514 | oe->__upperdentry = upperdentry; |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 515 | memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); |
| 516 | kfree(stack); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 517 | dentry->d_fsdata = oe; |
| 518 | d_add(dentry, inode); |
| 519 | |
| 520 | return NULL; |
| 521 | |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 522 | out_free_oe: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 523 | kfree(oe); |
Miklos Szeredi | 3d3c6b8 | 2014-12-13 00:59:44 +0100 | [diff] [blame] | 524 | out_put: |
| 525 | for (i = 0; i < ctr; i++) |
| 526 | dput(stack[i].dentry); |
| 527 | kfree(stack); |
| 528 | out_put_upper: |
| 529 | dput(upperdentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 530 | out: |
| 531 | return ERR_PTR(err); |
| 532 | } |
| 533 | |
| 534 | struct file *ovl_path_open(struct path *path, int flags) |
| 535 | { |
| 536 | return dentry_open(path, flags, current_cred()); |
| 537 | } |
| 538 | |
| 539 | static void ovl_put_super(struct super_block *sb) |
| 540 | { |
| 541 | struct ovl_fs *ufs = sb->s_fs_info; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 542 | unsigned i; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 543 | |
| 544 | dput(ufs->workdir); |
| 545 | mntput(ufs->upper_mnt); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 546 | for (i = 0; i < ufs->numlower; i++) |
| 547 | mntput(ufs->lower_mnt[i]); |
Konstantin Khlebnikov | 5ffdbe8 | 2015-08-24 15:57:19 +0300 | [diff] [blame] | 548 | kfree(ufs->lower_mnt); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 549 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 550 | kfree(ufs->config.lowerdir); |
| 551 | kfree(ufs->config.upperdir); |
| 552 | kfree(ufs->config.workdir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 553 | kfree(ufs); |
| 554 | } |
| 555 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 556 | /** |
| 557 | * ovl_statfs |
| 558 | * @sb: The overlayfs super block |
| 559 | * @buf: The struct kstatfs to fill in with stats |
| 560 | * |
| 561 | * Get the filesystem statistics. As writes always target the upper layer |
Miklos Szeredi | 4ebc581 | 2014-12-13 00:59:46 +0100 | [diff] [blame] | 562 | * filesystem pass the statfs to the upper filesystem (if it exists) |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 563 | */ |
| 564 | static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 565 | { |
| 566 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 567 | struct dentry *root_dentry = dentry->d_sb->s_root; |
| 568 | struct path path; |
| 569 | int err; |
| 570 | |
Miklos Szeredi | 4ebc581 | 2014-12-13 00:59:46 +0100 | [diff] [blame] | 571 | ovl_path_real(root_dentry, &path); |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 572 | |
| 573 | err = vfs_statfs(&path, buf); |
| 574 | if (!err) { |
| 575 | buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); |
| 576 | buf->f_type = OVERLAYFS_SUPER_MAGIC; |
| 577 | } |
| 578 | |
| 579 | return err; |
| 580 | } |
| 581 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 582 | /** |
| 583 | * ovl_show_options |
| 584 | * |
| 585 | * Prints the mount options for a given superblock. |
| 586 | * Returns zero; does not fail. |
| 587 | */ |
| 588 | static int ovl_show_options(struct seq_file *m, struct dentry *dentry) |
| 589 | { |
| 590 | struct super_block *sb = dentry->d_sb; |
| 591 | struct ovl_fs *ufs = sb->s_fs_info; |
| 592 | |
Kees Cook | a068acf | 2015-09-04 15:44:57 -0700 | [diff] [blame] | 593 | seq_show_option(m, "lowerdir", ufs->config.lowerdir); |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 594 | if (ufs->config.upperdir) { |
Kees Cook | a068acf | 2015-09-04 15:44:57 -0700 | [diff] [blame] | 595 | seq_show_option(m, "upperdir", ufs->config.upperdir); |
| 596 | seq_show_option(m, "workdir", ufs->config.workdir); |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 597 | } |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 598 | return 0; |
| 599 | } |
| 600 | |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 601 | static int ovl_remount(struct super_block *sb, int *flags, char *data) |
| 602 | { |
| 603 | struct ovl_fs *ufs = sb->s_fs_info; |
| 604 | |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 605 | if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir)) |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 606 | return -EROFS; |
| 607 | |
| 608 | return 0; |
| 609 | } |
| 610 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 611 | static const struct super_operations ovl_super_operations = { |
| 612 | .put_super = ovl_put_super, |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 613 | .statfs = ovl_statfs, |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 614 | .show_options = ovl_show_options, |
Seunghun Lee | 3cdf6fe | 2015-01-03 02:26:49 +0900 | [diff] [blame] | 615 | .remount_fs = ovl_remount, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 616 | }; |
| 617 | |
| 618 | enum { |
| 619 | OPT_LOWERDIR, |
| 620 | OPT_UPPERDIR, |
| 621 | OPT_WORKDIR, |
| 622 | OPT_ERR, |
| 623 | }; |
| 624 | |
| 625 | static const match_table_t ovl_tokens = { |
| 626 | {OPT_LOWERDIR, "lowerdir=%s"}, |
| 627 | {OPT_UPPERDIR, "upperdir=%s"}, |
| 628 | {OPT_WORKDIR, "workdir=%s"}, |
| 629 | {OPT_ERR, NULL} |
| 630 | }; |
| 631 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 632 | static char *ovl_next_opt(char **s) |
| 633 | { |
| 634 | char *sbegin = *s; |
| 635 | char *p; |
| 636 | |
| 637 | if (sbegin == NULL) |
| 638 | return NULL; |
| 639 | |
| 640 | for (p = sbegin; *p; p++) { |
| 641 | if (*p == '\\') { |
| 642 | p++; |
| 643 | if (!*p) |
| 644 | break; |
| 645 | } else if (*p == ',') { |
| 646 | *p = '\0'; |
| 647 | *s = p + 1; |
| 648 | return sbegin; |
| 649 | } |
| 650 | } |
| 651 | *s = NULL; |
| 652 | return sbegin; |
| 653 | } |
| 654 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 655 | static int ovl_parse_opt(char *opt, struct ovl_config *config) |
| 656 | { |
| 657 | char *p; |
| 658 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 659 | while ((p = ovl_next_opt(&opt)) != NULL) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 660 | int token; |
| 661 | substring_t args[MAX_OPT_ARGS]; |
| 662 | |
| 663 | if (!*p) |
| 664 | continue; |
| 665 | |
| 666 | token = match_token(p, ovl_tokens, args); |
| 667 | switch (token) { |
| 668 | case OPT_UPPERDIR: |
| 669 | kfree(config->upperdir); |
| 670 | config->upperdir = match_strdup(&args[0]); |
| 671 | if (!config->upperdir) |
| 672 | return -ENOMEM; |
| 673 | break; |
| 674 | |
| 675 | case OPT_LOWERDIR: |
| 676 | kfree(config->lowerdir); |
| 677 | config->lowerdir = match_strdup(&args[0]); |
| 678 | if (!config->lowerdir) |
| 679 | return -ENOMEM; |
| 680 | break; |
| 681 | |
| 682 | case OPT_WORKDIR: |
| 683 | kfree(config->workdir); |
| 684 | config->workdir = match_strdup(&args[0]); |
| 685 | if (!config->workdir) |
| 686 | return -ENOMEM; |
| 687 | break; |
| 688 | |
| 689 | default: |
hujianyang | bead55e | 2015-01-15 13:17:36 +0800 | [diff] [blame] | 690 | pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 691 | return -EINVAL; |
| 692 | } |
| 693 | } |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 694 | |
| 695 | /* Workdir is useless in non-upper mount */ |
| 696 | if (!config->upperdir && config->workdir) { |
| 697 | pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", |
| 698 | config->workdir); |
| 699 | kfree(config->workdir); |
| 700 | config->workdir = NULL; |
| 701 | } |
| 702 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | #define OVL_WORKDIR_NAME "work" |
| 707 | |
| 708 | static struct dentry *ovl_workdir_create(struct vfsmount *mnt, |
| 709 | struct dentry *dentry) |
| 710 | { |
| 711 | struct inode *dir = dentry->d_inode; |
| 712 | struct dentry *work; |
| 713 | int err; |
| 714 | bool retried = false; |
| 715 | |
| 716 | err = mnt_want_write(mnt); |
| 717 | if (err) |
| 718 | return ERR_PTR(err); |
| 719 | |
| 720 | mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT); |
| 721 | retry: |
| 722 | work = lookup_one_len(OVL_WORKDIR_NAME, dentry, |
| 723 | strlen(OVL_WORKDIR_NAME)); |
| 724 | |
| 725 | if (!IS_ERR(work)) { |
| 726 | struct kstat stat = { |
| 727 | .mode = S_IFDIR | 0, |
| 728 | }; |
| 729 | |
| 730 | if (work->d_inode) { |
| 731 | err = -EEXIST; |
| 732 | if (retried) |
| 733 | goto out_dput; |
| 734 | |
| 735 | retried = true; |
| 736 | ovl_cleanup(dir, work); |
| 737 | dput(work); |
| 738 | goto retry; |
| 739 | } |
| 740 | |
| 741 | err = ovl_create_real(dir, work, &stat, NULL, NULL, true); |
| 742 | if (err) |
| 743 | goto out_dput; |
| 744 | } |
| 745 | out_unlock: |
| 746 | mutex_unlock(&dir->i_mutex); |
| 747 | mnt_drop_write(mnt); |
| 748 | |
| 749 | return work; |
| 750 | |
| 751 | out_dput: |
| 752 | dput(work); |
| 753 | work = ERR_PTR(err); |
| 754 | goto out_unlock; |
| 755 | } |
| 756 | |
Miklos Szeredi | 91c7794 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 757 | static void ovl_unescape(char *s) |
| 758 | { |
| 759 | char *d = s; |
| 760 | |
| 761 | for (;; s++, d++) { |
| 762 | if (*s == '\\') |
| 763 | s++; |
| 764 | *d = *s; |
| 765 | if (!*s) |
| 766 | break; |
| 767 | } |
| 768 | } |
| 769 | |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 770 | static int ovl_mount_dir_noesc(const char *name, struct path *path) |
| 771 | { |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 772 | int err = -EINVAL; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 773 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 774 | if (!*name) { |
| 775 | pr_err("overlayfs: empty lowerdir\n"); |
| 776 | goto out; |
| 777 | } |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 778 | err = kern_path(name, LOOKUP_FOLLOW, path); |
| 779 | if (err) { |
| 780 | pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); |
| 781 | goto out; |
| 782 | } |
| 783 | err = -EINVAL; |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 784 | if (ovl_dentry_weird(path->dentry)) { |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 785 | pr_err("overlayfs: filesystem on '%s' not supported\n", name); |
| 786 | goto out_put; |
| 787 | } |
| 788 | if (!S_ISDIR(path->dentry->d_inode->i_mode)) { |
| 789 | pr_err("overlayfs: '%s' not a directory\n", name); |
| 790 | goto out_put; |
| 791 | } |
| 792 | return 0; |
| 793 | |
| 794 | out_put: |
| 795 | path_put(path); |
| 796 | out: |
| 797 | return err; |
| 798 | } |
| 799 | |
| 800 | static int ovl_mount_dir(const char *name, struct path *path) |
| 801 | { |
| 802 | int err = -ENOMEM; |
| 803 | char *tmp = kstrdup(name, GFP_KERNEL); |
| 804 | |
| 805 | if (tmp) { |
| 806 | ovl_unescape(tmp); |
| 807 | err = ovl_mount_dir_noesc(tmp, path); |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 808 | |
| 809 | if (!err) |
| 810 | if (ovl_dentry_remote(path->dentry)) { |
| 811 | pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", |
| 812 | tmp); |
| 813 | path_put(path); |
| 814 | err = -EINVAL; |
| 815 | } |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 816 | kfree(tmp); |
| 817 | } |
| 818 | return err; |
| 819 | } |
| 820 | |
| 821 | static int ovl_lower_dir(const char *name, struct path *path, long *namelen, |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 822 | int *stack_depth, bool *remote) |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 823 | { |
| 824 | int err; |
| 825 | struct kstatfs statfs; |
| 826 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 827 | err = ovl_mount_dir_noesc(name, path); |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 828 | if (err) |
| 829 | goto out; |
| 830 | |
| 831 | err = vfs_statfs(path, &statfs); |
| 832 | if (err) { |
| 833 | pr_err("overlayfs: statfs failed on '%s'\n", name); |
| 834 | goto out_put; |
| 835 | } |
| 836 | *namelen = max(*namelen, statfs.f_namelen); |
| 837 | *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); |
| 838 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 839 | if (ovl_dentry_remote(path->dentry)) |
| 840 | *remote = true; |
| 841 | |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 842 | return 0; |
| 843 | |
| 844 | out_put: |
| 845 | path_put(path); |
| 846 | out: |
| 847 | return err; |
| 848 | } |
| 849 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 850 | /* Workdir should not be subdir of upperdir and vice versa */ |
| 851 | static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) |
| 852 | { |
| 853 | bool ok = false; |
| 854 | |
| 855 | if (workdir != upperdir) { |
| 856 | ok = (lock_rename(workdir, upperdir) == NULL); |
| 857 | unlock_rename(workdir, upperdir); |
| 858 | } |
| 859 | return ok; |
| 860 | } |
| 861 | |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 862 | static unsigned int ovl_split_lowerdirs(char *str) |
| 863 | { |
| 864 | unsigned int ctr = 1; |
| 865 | char *s, *d; |
| 866 | |
| 867 | for (s = d = str;; s++, d++) { |
| 868 | if (*s == '\\') { |
| 869 | s++; |
| 870 | } else if (*s == ':') { |
| 871 | *d = '\0'; |
| 872 | ctr++; |
| 873 | continue; |
| 874 | } |
| 875 | *d = *s; |
| 876 | if (!*s) |
| 877 | break; |
| 878 | } |
| 879 | return ctr; |
| 880 | } |
| 881 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 882 | static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
| 883 | { |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 884 | struct path upperpath = { NULL, NULL }; |
| 885 | struct path workpath = { NULL, NULL }; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 886 | struct dentry *root_dentry; |
| 887 | struct ovl_entry *oe; |
| 888 | struct ovl_fs *ufs; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 889 | struct path *stack = NULL; |
| 890 | char *lowertmp; |
| 891 | char *lower; |
| 892 | unsigned int numlower; |
| 893 | unsigned int stacklen = 0; |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 894 | unsigned int i; |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 895 | bool remote = false; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 896 | int err; |
| 897 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 898 | err = -ENOMEM; |
| 899 | ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); |
| 900 | if (!ufs) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 901 | goto out; |
| 902 | |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 903 | err = ovl_parse_opt((char *) data, &ufs->config); |
| 904 | if (err) |
| 905 | goto out_free_config; |
| 906 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 907 | err = -EINVAL; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 908 | if (!ufs->config.lowerdir) { |
| 909 | pr_err("overlayfs: missing 'lowerdir'\n"); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 910 | goto out_free_config; |
| 911 | } |
| 912 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 913 | sb->s_stack_depth = 0; |
| 914 | if (ufs->config.upperdir) { |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 915 | if (!ufs->config.workdir) { |
| 916 | pr_err("overlayfs: missing 'workdir'\n"); |
| 917 | goto out_free_config; |
| 918 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 919 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 920 | err = ovl_mount_dir(ufs->config.upperdir, &upperpath); |
| 921 | if (err) |
| 922 | goto out_free_config; |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 923 | |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 924 | /* Upper fs should not be r/o */ |
| 925 | if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) { |
| 926 | pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); |
| 927 | err = -EINVAL; |
| 928 | goto out_put_upperpath; |
| 929 | } |
| 930 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 931 | err = ovl_mount_dir(ufs->config.workdir, &workpath); |
| 932 | if (err) |
| 933 | goto out_put_upperpath; |
| 934 | |
hujianyang | 2f83fd8 | 2015-01-06 12:52:13 +0800 | [diff] [blame] | 935 | err = -EINVAL; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 936 | if (upperpath.mnt != workpath.mnt) { |
| 937 | pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); |
| 938 | goto out_put_workpath; |
| 939 | } |
| 940 | if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { |
| 941 | pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); |
| 942 | goto out_put_workpath; |
| 943 | } |
| 944 | sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 945 | } |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 946 | err = -ENOMEM; |
| 947 | lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); |
| 948 | if (!lowertmp) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 949 | goto out_put_workpath; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 950 | |
| 951 | err = -EINVAL; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 952 | stacklen = ovl_split_lowerdirs(lowertmp); |
hujianyang | 6be4506 | 2015-01-15 13:19:21 +0800 | [diff] [blame] | 953 | if (stacklen > OVL_MAX_STACK) { |
| 954 | pr_err("overlayfs: too many lower directries, limit is %d\n", |
| 955 | OVL_MAX_STACK); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 956 | goto out_free_lowertmp; |
hujianyang | 6be4506 | 2015-01-15 13:19:21 +0800 | [diff] [blame] | 957 | } else if (!ufs->config.upperdir && stacklen == 1) { |
| 958 | pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); |
| 959 | goto out_free_lowertmp; |
| 960 | } |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 961 | |
| 962 | stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); |
| 963 | if (!stack) |
| 964 | goto out_free_lowertmp; |
| 965 | |
| 966 | lower = lowertmp; |
| 967 | for (numlower = 0; numlower < stacklen; numlower++) { |
| 968 | err = ovl_lower_dir(lower, &stack[numlower], |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 969 | &ufs->lower_namelen, &sb->s_stack_depth, |
| 970 | &remote); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 971 | if (err) |
| 972 | goto out_put_lowerpath; |
| 973 | |
| 974 | lower = strchr(lower, '\0') + 1; |
| 975 | } |
| 976 | |
| 977 | err = -EINVAL; |
Miklos Szeredi | ab50882 | 2014-12-13 00:59:49 +0100 | [diff] [blame] | 978 | sb->s_stack_depth++; |
Miklos Szeredi | 69c433e | 2014-10-24 00:14:39 +0200 | [diff] [blame] | 979 | if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { |
| 980 | pr_err("overlayfs: maximum fs stacking depth exceeded\n"); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 981 | goto out_put_lowerpath; |
Miklos Szeredi | 69c433e | 2014-10-24 00:14:39 +0200 | [diff] [blame] | 982 | } |
| 983 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 984 | if (ufs->config.upperdir) { |
| 985 | ufs->upper_mnt = clone_private_mount(&upperpath); |
| 986 | err = PTR_ERR(ufs->upper_mnt); |
| 987 | if (IS_ERR(ufs->upper_mnt)) { |
| 988 | pr_err("overlayfs: failed to clone upperpath\n"); |
| 989 | goto out_put_lowerpath; |
| 990 | } |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 991 | |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 992 | ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); |
| 993 | err = PTR_ERR(ufs->workdir); |
| 994 | if (IS_ERR(ufs->workdir)) { |
Miklos Szeredi | cc6f67b | 2015-05-19 14:30:12 +0200 | [diff] [blame] | 995 | pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", |
| 996 | ufs->config.workdir, OVL_WORKDIR_NAME, -err); |
| 997 | sb->s_flags |= MS_RDONLY; |
| 998 | ufs->workdir = NULL; |
Miklos Szeredi | 53a08cb | 2014-12-13 00:59:51 +0100 | [diff] [blame] | 999 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
hujianyang | 2f83fd8 | 2015-01-06 12:52:13 +0800 | [diff] [blame] | 1002 | err = -ENOMEM; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1003 | ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 1004 | if (ufs->lower_mnt == NULL) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1005 | goto out_put_workdir; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1006 | for (i = 0; i < numlower; i++) { |
| 1007 | struct vfsmount *mnt = clone_private_mount(&stack[i]); |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 1008 | |
hujianyang | 2f83fd8 | 2015-01-06 12:52:13 +0800 | [diff] [blame] | 1009 | err = PTR_ERR(mnt); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1010 | if (IS_ERR(mnt)) { |
| 1011 | pr_err("overlayfs: failed to clone lowerpath\n"); |
| 1012 | goto out_put_lower_mnt; |
| 1013 | } |
| 1014 | /* |
| 1015 | * Make lower_mnt R/O. That way fchmod/fchown on lower file |
| 1016 | * will fail instead of modifying lower fs. |
| 1017 | */ |
| 1018 | mnt->mnt_flags |= MNT_READONLY; |
| 1019 | |
| 1020 | ufs->lower_mnt[ufs->numlower] = mnt; |
| 1021 | ufs->numlower++; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1022 | } |
| 1023 | |
hujianyang | 71cbad7 | 2015-01-15 13:20:57 +0800 | [diff] [blame] | 1024 | /* If the upper fs is nonexistent, we mark overlayfs r/o too */ |
| 1025 | if (!ufs->upper_mnt) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1026 | sb->s_flags |= MS_RDONLY; |
| 1027 | |
Miklos Szeredi | 7c03b5d | 2015-06-22 13:53:48 +0200 | [diff] [blame] | 1028 | if (remote) |
| 1029 | sb->s_d_op = &ovl_reval_dentry_operations; |
| 1030 | else |
| 1031 | sb->s_d_op = &ovl_dentry_operations; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1032 | |
| 1033 | err = -ENOMEM; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1034 | oe = ovl_alloc_entry(numlower); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1035 | if (!oe) |
| 1036 | goto out_put_lower_mnt; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1037 | |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1038 | root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe)); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1039 | if (!root_dentry) |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1040 | goto out_free_oe; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1041 | |
| 1042 | mntput(upperpath.mnt); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1043 | for (i = 0; i < numlower; i++) |
| 1044 | mntput(stack[i].mnt); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1045 | path_put(&workpath); |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1046 | kfree(lowertmp); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1047 | |
| 1048 | oe->__upperdentry = upperpath.dentry; |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1049 | for (i = 0; i < numlower; i++) { |
| 1050 | oe->lowerstack[i].dentry = stack[i].dentry; |
| 1051 | oe->lowerstack[i].mnt = ufs->lower_mnt[i]; |
| 1052 | } |
Konstantin Khlebnikov | 0f95502 | 2015-08-24 15:57:18 +0300 | [diff] [blame] | 1053 | kfree(stack); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1054 | |
| 1055 | root_dentry->d_fsdata = oe; |
| 1056 | |
Andy Whitcroft | cc25963 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1057 | sb->s_magic = OVERLAYFS_SUPER_MAGIC; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1058 | sb->s_op = &ovl_super_operations; |
| 1059 | sb->s_root = root_dentry; |
| 1060 | sb->s_fs_info = ufs; |
| 1061 | |
| 1062 | return 0; |
| 1063 | |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1064 | out_free_oe: |
| 1065 | kfree(oe); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1066 | out_put_lower_mnt: |
Miklos Szeredi | dd66266 | 2014-12-13 00:59:43 +0100 | [diff] [blame] | 1067 | for (i = 0; i < ufs->numlower; i++) |
| 1068 | mntput(ufs->lower_mnt[i]); |
| 1069 | kfree(ufs->lower_mnt); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1070 | out_put_workdir: |
| 1071 | dput(ufs->workdir); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1072 | mntput(ufs->upper_mnt); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1073 | out_put_lowerpath: |
Miklos Szeredi | a78d9f0 | 2014-12-13 00:59:52 +0100 | [diff] [blame] | 1074 | for (i = 0; i < numlower; i++) |
| 1075 | path_put(&stack[i]); |
| 1076 | kfree(stack); |
| 1077 | out_free_lowertmp: |
| 1078 | kfree(lowertmp); |
Miklos Szeredi | 3b7a9a2 | 2014-12-13 00:59:48 +0100 | [diff] [blame] | 1079 | out_put_workpath: |
| 1080 | path_put(&workpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1081 | out_put_upperpath: |
| 1082 | path_put(&upperpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1083 | out_free_config: |
Erez Zadok | f45827e8 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1084 | kfree(ufs->config.lowerdir); |
| 1085 | kfree(ufs->config.upperdir); |
| 1086 | kfree(ufs->config.workdir); |
| 1087 | kfree(ufs); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1088 | out: |
| 1089 | return err; |
| 1090 | } |
| 1091 | |
| 1092 | static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, |
| 1093 | const char *dev_name, void *raw_data) |
| 1094 | { |
| 1095 | return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); |
| 1096 | } |
| 1097 | |
| 1098 | static struct file_system_type ovl_fs_type = { |
| 1099 | .owner = THIS_MODULE, |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 1100 | .name = "overlay", |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1101 | .mount = ovl_mount, |
| 1102 | .kill_sb = kill_anon_super, |
| 1103 | }; |
Miklos Szeredi | ef94b18 | 2014-11-20 16:39:59 +0100 | [diff] [blame] | 1104 | MODULE_ALIAS_FS("overlay"); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1105 | |
| 1106 | static int __init ovl_init(void) |
| 1107 | { |
| 1108 | return register_filesystem(&ovl_fs_type); |
| 1109 | } |
| 1110 | |
| 1111 | static void __exit ovl_exit(void) |
| 1112 | { |
| 1113 | unregister_filesystem(&ovl_fs_type); |
| 1114 | } |
| 1115 | |
| 1116 | module_init(ovl_init); |
| 1117 | module_exit(ovl_exit); |