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