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