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