Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 Novell Inc. |
| 3 | * Copyright (C) 2016 Red Hat, 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/mount.h> |
| 12 | #include <linux/slab.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 13 | #include <linux/cred.h> |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 14 | #include <linux/xattr.h> |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 15 | #include <linux/exportfs.h> |
| 16 | #include <linux/uuid.h> |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 17 | #include <linux/namei.h> |
| 18 | #include <linux/ratelimit.h> |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 19 | #include "overlayfs.h" |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 20 | |
| 21 | int ovl_want_write(struct dentry *dentry) |
| 22 | { |
| 23 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 24 | return mnt_want_write(ofs->upper_mnt); |
| 25 | } |
| 26 | |
| 27 | void ovl_drop_write(struct dentry *dentry) |
| 28 | { |
| 29 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 30 | mnt_drop_write(ofs->upper_mnt); |
| 31 | } |
| 32 | |
| 33 | struct dentry *ovl_workdir(struct dentry *dentry) |
| 34 | { |
| 35 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 36 | return ofs->workdir; |
| 37 | } |
| 38 | |
| 39 | const struct cred *ovl_override_creds(struct super_block *sb) |
| 40 | { |
| 41 | struct ovl_fs *ofs = sb->s_fs_info; |
| 42 | |
| 43 | return override_creds(ofs->creator_cred); |
| 44 | } |
| 45 | |
Amir Goldstein | 7bcd74b | 2017-03-22 08:42:21 -0400 | [diff] [blame] | 46 | struct super_block *ovl_same_sb(struct super_block *sb) |
| 47 | { |
| 48 | struct ovl_fs *ofs = sb->s_fs_info; |
| 49 | |
Amir Goldstein | 5148626 | 2018-03-28 20:22:41 +0300 | [diff] [blame^] | 50 | if (!ofs->numlowerfs) |
| 51 | return ofs->upper_mnt->mnt_sb; |
| 52 | else if (ofs->numlowerfs == 1 && !ofs->upper_mnt) |
| 53 | return ofs->lower_fs[0].sb; |
| 54 | else |
| 55 | return NULL; |
Amir Goldstein | 7bcd74b | 2017-03-22 08:42:21 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 58 | bool ovl_can_decode_fh(struct super_block *sb) |
| 59 | { |
| 60 | return (sb->s_export_op && sb->s_export_op->fh_to_dentry && |
| 61 | !uuid_is_null(&sb->s_uuid)); |
| 62 | } |
| 63 | |
| 64 | struct dentry *ovl_indexdir(struct super_block *sb) |
| 65 | { |
| 66 | struct ovl_fs *ofs = sb->s_fs_info; |
| 67 | |
| 68 | return ofs->indexdir; |
| 69 | } |
| 70 | |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 71 | /* Index all files on copy up. For now only enabled for NFS export */ |
| 72 | bool ovl_index_all(struct super_block *sb) |
| 73 | { |
| 74 | struct ovl_fs *ofs = sb->s_fs_info; |
| 75 | |
| 76 | return ofs->config.nfs_export && ofs->config.index; |
| 77 | } |
| 78 | |
| 79 | /* Verify lower origin on lookup. For now only enabled for NFS export */ |
| 80 | bool ovl_verify_lower(struct super_block *sb) |
| 81 | { |
| 82 | struct ovl_fs *ofs = sb->s_fs_info; |
| 83 | |
| 84 | return ofs->config.nfs_export && ofs->config.index; |
| 85 | } |
| 86 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 87 | struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
| 88 | { |
| 89 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); |
| 90 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); |
| 91 | |
| 92 | if (oe) |
| 93 | oe->numlower = numlower; |
| 94 | |
| 95 | return oe; |
| 96 | } |
| 97 | |
| 98 | bool ovl_dentry_remote(struct dentry *dentry) |
| 99 | { |
| 100 | return dentry->d_flags & |
| 101 | (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE | |
| 102 | DCACHE_OP_REAL); |
| 103 | } |
| 104 | |
| 105 | bool ovl_dentry_weird(struct dentry *dentry) |
| 106 | { |
| 107 | return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | |
| 108 | DCACHE_MANAGE_TRANSIT | |
| 109 | DCACHE_OP_HASH | |
| 110 | DCACHE_OP_COMPARE); |
| 111 | } |
| 112 | |
| 113 | enum ovl_path_type ovl_path_type(struct dentry *dentry) |
| 114 | { |
| 115 | struct ovl_entry *oe = dentry->d_fsdata; |
| 116 | enum ovl_path_type type = 0; |
| 117 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 118 | if (ovl_dentry_upper(dentry)) { |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 119 | type = __OVL_PATH_UPPER; |
| 120 | |
| 121 | /* |
Amir Goldstein | 5954850 | 2017-04-23 23:12:34 +0300 | [diff] [blame] | 122 | * Non-dir dentry can hold lower dentry of its copy up origin. |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 123 | */ |
Amir Goldstein | 5954850 | 2017-04-23 23:12:34 +0300 | [diff] [blame] | 124 | if (oe->numlower) { |
| 125 | type |= __OVL_PATH_ORIGIN; |
| 126 | if (d_is_dir(dentry)) |
| 127 | type |= __OVL_PATH_MERGE; |
| 128 | } |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 129 | } else { |
| 130 | if (oe->numlower > 1) |
| 131 | type |= __OVL_PATH_MERGE; |
| 132 | } |
| 133 | return type; |
| 134 | } |
| 135 | |
| 136 | void ovl_path_upper(struct dentry *dentry, struct path *path) |
| 137 | { |
| 138 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 139 | |
| 140 | path->mnt = ofs->upper_mnt; |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 141 | path->dentry = ovl_dentry_upper(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void ovl_path_lower(struct dentry *dentry, struct path *path) |
| 145 | { |
| 146 | struct ovl_entry *oe = dentry->d_fsdata; |
| 147 | |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 148 | if (oe->numlower) { |
| 149 | path->mnt = oe->lowerstack[0].layer->mnt; |
| 150 | path->dentry = oe->lowerstack[0].dentry; |
| 151 | } else { |
| 152 | *path = (struct path) { }; |
| 153 | } |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) |
| 157 | { |
| 158 | enum ovl_path_type type = ovl_path_type(dentry); |
| 159 | |
| 160 | if (!OVL_TYPE_UPPER(type)) |
| 161 | ovl_path_lower(dentry, path); |
| 162 | else |
| 163 | ovl_path_upper(dentry, path); |
| 164 | |
| 165 | return type; |
| 166 | } |
| 167 | |
| 168 | struct dentry *ovl_dentry_upper(struct dentry *dentry) |
| 169 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 170 | return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | struct dentry *ovl_dentry_lower(struct dentry *dentry) |
| 174 | { |
| 175 | struct ovl_entry *oe = dentry->d_fsdata; |
| 176 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 177 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 180 | struct ovl_layer *ovl_layer_lower(struct dentry *dentry) |
| 181 | { |
| 182 | struct ovl_entry *oe = dentry->d_fsdata; |
| 183 | |
| 184 | return oe->numlower ? oe->lowerstack[0].layer : NULL; |
| 185 | } |
| 186 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 187 | struct dentry *ovl_dentry_real(struct dentry *dentry) |
| 188 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 189 | return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 192 | struct dentry *ovl_i_dentry_upper(struct inode *inode) |
| 193 | { |
| 194 | return ovl_upperdentry_dereference(OVL_I(inode)); |
| 195 | } |
| 196 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 197 | struct inode *ovl_inode_upper(struct inode *inode) |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 198 | { |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 199 | struct dentry *upperdentry = ovl_i_dentry_upper(inode); |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 200 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 201 | return upperdentry ? d_inode(upperdentry) : NULL; |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 202 | } |
| 203 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 204 | struct inode *ovl_inode_lower(struct inode *inode) |
| 205 | { |
| 206 | return OVL_I(inode)->lower; |
| 207 | } |
| 208 | |
| 209 | struct inode *ovl_inode_real(struct inode *inode) |
| 210 | { |
| 211 | return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); |
| 212 | } |
| 213 | |
| 214 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 215 | struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 216 | { |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 217 | return OVL_I(inode)->cache; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 218 | } |
| 219 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 220 | void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 221 | { |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 222 | OVL_I(inode)->cache = cache; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 225 | void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) |
| 226 | { |
| 227 | set_bit(flag, &OVL_E(dentry)->flags); |
| 228 | } |
| 229 | |
| 230 | void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) |
| 231 | { |
| 232 | clear_bit(flag, &OVL_E(dentry)->flags); |
| 233 | } |
| 234 | |
| 235 | bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) |
| 236 | { |
| 237 | return test_bit(flag, &OVL_E(dentry)->flags); |
| 238 | } |
| 239 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 240 | bool ovl_dentry_is_opaque(struct dentry *dentry) |
| 241 | { |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 242 | return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | bool ovl_dentry_is_whiteout(struct dentry *dentry) |
| 246 | { |
| 247 | return !dentry->d_inode && ovl_dentry_is_opaque(dentry); |
| 248 | } |
| 249 | |
Miklos Szeredi | 5cf5b47 | 2016-12-16 11:02:57 +0100 | [diff] [blame] | 250 | void ovl_dentry_set_opaque(struct dentry *dentry) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 251 | { |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 252 | ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Miklos Szeredi | 55acc66 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 255 | /* |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 256 | * For hard links and decoded file handles, it's possible for ovl_dentry_upper() |
| 257 | * to return positive, while there's no actual upper alias for the inode. |
| 258 | * Copy up code needs to know about the existence of the upper alias, so it |
| 259 | * can't use ovl_dentry_upper(). |
Miklos Szeredi | 55acc66 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 260 | */ |
| 261 | bool ovl_dentry_has_upper_alias(struct dentry *dentry) |
| 262 | { |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 263 | return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); |
Miklos Szeredi | 55acc66 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void ovl_dentry_set_upper_alias(struct dentry *dentry) |
| 267 | { |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 268 | ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); |
Miklos Szeredi | 55acc66 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 269 | } |
| 270 | |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 271 | bool ovl_redirect_dir(struct super_block *sb) |
| 272 | { |
| 273 | struct ovl_fs *ofs = sb->s_fs_info; |
| 274 | |
Amir Goldstein | 21a2287 | 2017-05-17 00:12:41 +0300 | [diff] [blame] | 275 | return ofs->config.redirect_dir && !ofs->noxattr; |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | const char *ovl_dentry_get_redirect(struct dentry *dentry) |
| 279 | { |
Miklos Szeredi | cf31c46 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 280 | return OVL_I(d_inode(dentry))->redirect; |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) |
| 284 | { |
Miklos Szeredi | cf31c46 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 285 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 286 | |
Miklos Szeredi | cf31c46 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 287 | kfree(oi->redirect); |
| 288 | oi->redirect = redirect; |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 289 | } |
| 290 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 291 | void ovl_inode_init(struct inode *inode, struct dentry *upperdentry, |
| 292 | struct dentry *lowerdentry) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 293 | { |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 294 | struct inode *realinode = d_inode(upperdentry ?: lowerdentry); |
| 295 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 296 | if (upperdentry) |
| 297 | OVL_I(inode)->__upperdentry = upperdentry; |
| 298 | if (lowerdentry) |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 299 | OVL_I(inode)->lower = igrab(d_inode(lowerdentry)); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 300 | |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 301 | ovl_copyattr(realinode, inode); |
| 302 | if (!inode->i_ino) |
| 303 | inode->i_ino = realinode->i_ino; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 304 | } |
| 305 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 306 | void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 307 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 308 | struct inode *upperinode = d_inode(upperdentry); |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 309 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 310 | WARN_ON(OVL_I(inode)->__upperdentry); |
| 311 | |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 312 | /* |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 313 | * Make sure upperdentry is consistent before making it visible |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 314 | */ |
| 315 | smp_wmb(); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 316 | OVL_I(inode)->__upperdentry = upperdentry; |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 317 | if (inode_unhashed(inode)) { |
Amir Goldstein | 695b46e | 2018-03-15 23:39:01 +0200 | [diff] [blame] | 318 | if (!inode->i_ino) |
| 319 | inode->i_ino = upperinode->i_ino; |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 320 | inode->i_private = upperinode; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 321 | __insert_inode_hash(inode, (unsigned long) upperinode); |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 322 | } |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 325 | void ovl_dentry_version_inc(struct dentry *dentry, bool impurity) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 326 | { |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 327 | struct inode *inode = d_inode(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 328 | |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 329 | WARN_ON(!inode_is_locked(inode)); |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 330 | /* |
| 331 | * Version is used by readdir code to keep cache consistent. For merge |
| 332 | * dirs all changes need to be noted. For non-merge dirs, cache only |
| 333 | * contains impure (ones which have been copied up and have origins) |
| 334 | * entries, so only need to note changes to impure entries. |
| 335 | */ |
| 336 | if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity) |
| 337 | OVL_I(inode)->version++; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | u64 ovl_dentry_version_get(struct dentry *dentry) |
| 341 | { |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 342 | struct inode *inode = d_inode(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 343 | |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 344 | WARN_ON(!inode_is_locked(inode)); |
| 345 | return OVL_I(inode)->version; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | bool ovl_is_whiteout(struct dentry *dentry) |
| 349 | { |
| 350 | struct inode *inode = dentry->d_inode; |
| 351 | |
| 352 | return inode && IS_WHITEOUT(inode); |
| 353 | } |
| 354 | |
| 355 | struct file *ovl_path_open(struct path *path, int flags) |
| 356 | { |
| 357 | return dentry_open(path, flags | O_NOATIME, current_cred()); |
| 358 | } |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 359 | |
| 360 | int ovl_copy_up_start(struct dentry *dentry) |
| 361 | { |
Amir Goldstein | a015daf | 2017-06-21 15:28:51 +0300 | [diff] [blame] | 362 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 363 | int err; |
| 364 | |
Amir Goldstein | a015daf | 2017-06-21 15:28:51 +0300 | [diff] [blame] | 365 | err = mutex_lock_interruptible(&oi->lock); |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame] | 366 | if (!err && ovl_dentry_has_upper_alias(dentry)) { |
Amir Goldstein | a015daf | 2017-06-21 15:28:51 +0300 | [diff] [blame] | 367 | err = 1; /* Already copied up */ |
| 368 | mutex_unlock(&oi->lock); |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 369 | } |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 370 | |
| 371 | return err; |
| 372 | } |
| 373 | |
| 374 | void ovl_copy_up_end(struct dentry *dentry) |
| 375 | { |
Amir Goldstein | a015daf | 2017-06-21 15:28:51 +0300 | [diff] [blame] | 376 | mutex_unlock(&OVL_I(d_inode(dentry))->lock); |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 377 | } |
Amir Goldstein | 82b749b | 2017-05-17 00:12:40 +0300 | [diff] [blame] | 378 | |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 379 | bool ovl_check_origin_xattr(struct dentry *dentry) |
| 380 | { |
| 381 | int res; |
| 382 | |
| 383 | res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0); |
| 384 | |
| 385 | /* Zero size value means "copied up but origin unknown" */ |
| 386 | if (res >= 0) |
| 387 | return true; |
| 388 | |
| 389 | return false; |
| 390 | } |
| 391 | |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 392 | bool ovl_check_dir_xattr(struct dentry *dentry, const char *name) |
| 393 | { |
| 394 | int res; |
| 395 | char val; |
| 396 | |
| 397 | if (!d_is_dir(dentry)) |
| 398 | return false; |
| 399 | |
| 400 | res = vfs_getxattr(dentry, name, &val, 1); |
| 401 | if (res == 1 && val == 'y') |
| 402 | return true; |
| 403 | |
| 404 | return false; |
| 405 | } |
| 406 | |
Amir Goldstein | 82b749b | 2017-05-17 00:12:40 +0300 | [diff] [blame] | 407 | int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, |
| 408 | const char *name, const void *value, size_t size, |
| 409 | int xerr) |
| 410 | { |
| 411 | int err; |
| 412 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 413 | |
| 414 | if (ofs->noxattr) |
| 415 | return xerr; |
| 416 | |
| 417 | err = ovl_do_setxattr(upperdentry, name, value, size, 0); |
| 418 | |
| 419 | if (err == -EOPNOTSUPP) { |
| 420 | pr_warn("overlayfs: cannot set %s xattr on upper\n", name); |
| 421 | ofs->noxattr = true; |
| 422 | return xerr; |
| 423 | } |
| 424 | |
| 425 | return err; |
| 426 | } |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 427 | |
| 428 | int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) |
| 429 | { |
| 430 | int err; |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 431 | |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 432 | if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 433 | return 0; |
| 434 | |
| 435 | /* |
| 436 | * Do not fail when upper doesn't support xattrs. |
| 437 | * Upper inodes won't have origin nor redirect xattr anyway. |
| 438 | */ |
| 439 | err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE, |
| 440 | "y", 1, 0); |
| 441 | if (!err) |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 442 | ovl_set_flag(OVL_IMPURE, d_inode(dentry)); |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 443 | |
| 444 | return err; |
| 445 | } |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 446 | |
| 447 | void ovl_set_flag(unsigned long flag, struct inode *inode) |
| 448 | { |
| 449 | set_bit(flag, &OVL_I(inode)->flags); |
| 450 | } |
| 451 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 452 | void ovl_clear_flag(unsigned long flag, struct inode *inode) |
| 453 | { |
| 454 | clear_bit(flag, &OVL_I(inode)->flags); |
| 455 | } |
| 456 | |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 457 | bool ovl_test_flag(unsigned long flag, struct inode *inode) |
| 458 | { |
| 459 | return test_bit(flag, &OVL_I(inode)->flags); |
| 460 | } |
Amir Goldstein | ad0af71 | 2017-06-21 15:28:32 +0300 | [diff] [blame] | 461 | |
| 462 | /** |
| 463 | * Caller must hold a reference to inode to prevent it from being freed while |
| 464 | * it is marked inuse. |
| 465 | */ |
| 466 | bool ovl_inuse_trylock(struct dentry *dentry) |
| 467 | { |
| 468 | struct inode *inode = d_inode(dentry); |
| 469 | bool locked = false; |
| 470 | |
| 471 | spin_lock(&inode->i_lock); |
| 472 | if (!(inode->i_state & I_OVL_INUSE)) { |
| 473 | inode->i_state |= I_OVL_INUSE; |
| 474 | locked = true; |
| 475 | } |
| 476 | spin_unlock(&inode->i_lock); |
| 477 | |
| 478 | return locked; |
| 479 | } |
| 480 | |
| 481 | void ovl_inuse_unlock(struct dentry *dentry) |
| 482 | { |
| 483 | if (dentry) { |
| 484 | struct inode *inode = d_inode(dentry); |
| 485 | |
| 486 | spin_lock(&inode->i_lock); |
| 487 | WARN_ON(!(inode->i_state & I_OVL_INUSE)); |
| 488 | inode->i_state &= ~I_OVL_INUSE; |
| 489 | spin_unlock(&inode->i_lock); |
| 490 | } |
| 491 | } |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 492 | |
Amir Goldstein | 24b33ee | 2017-09-26 07:55:26 +0300 | [diff] [blame] | 493 | /* |
| 494 | * Does this overlay dentry need to be indexed on copy up? |
| 495 | */ |
| 496 | bool ovl_need_index(struct dentry *dentry) |
| 497 | { |
| 498 | struct dentry *lower = ovl_dentry_lower(dentry); |
| 499 | |
| 500 | if (!lower || !ovl_indexdir(dentry->d_sb)) |
| 501 | return false; |
| 502 | |
Amir Goldstein | fbd2d20 | 2017-11-22 00:08:21 +0200 | [diff] [blame] | 503 | /* Index all files for NFS export and consistency verification */ |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 504 | if (ovl_index_all(dentry->d_sb)) |
Amir Goldstein | fbd2d20 | 2017-11-22 00:08:21 +0200 | [diff] [blame] | 505 | return true; |
| 506 | |
Amir Goldstein | 24b33ee | 2017-09-26 07:55:26 +0300 | [diff] [blame] | 507 | /* Index only lower hardlinks on copy up */ |
| 508 | if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) |
| 509 | return true; |
| 510 | |
| 511 | return false; |
| 512 | } |
| 513 | |
Amir Goldstein | 9f4ec90 | 2017-09-24 17:36:26 +0300 | [diff] [blame] | 514 | /* Caller must hold OVL_I(inode)->lock */ |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 515 | static void ovl_cleanup_index(struct dentry *dentry) |
| 516 | { |
Amir Goldstein | e7dd0e7 | 2017-10-24 17:38:33 +0300 | [diff] [blame] | 517 | struct dentry *indexdir = ovl_indexdir(dentry->d_sb); |
| 518 | struct inode *dir = indexdir->d_inode; |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 519 | struct dentry *lowerdentry = ovl_dentry_lower(dentry); |
| 520 | struct dentry *upperdentry = ovl_dentry_upper(dentry); |
| 521 | struct dentry *index = NULL; |
| 522 | struct inode *inode; |
| 523 | struct qstr name; |
| 524 | int err; |
| 525 | |
| 526 | err = ovl_get_index_name(lowerdentry, &name); |
| 527 | if (err) |
| 528 | goto fail; |
| 529 | |
| 530 | inode = d_inode(upperdentry); |
Amir Goldstein | 89a1755 | 2017-09-26 07:40:37 +0300 | [diff] [blame] | 531 | if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 532 | pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", |
| 533 | upperdentry, inode->i_ino, inode->i_nlink); |
| 534 | /* |
| 535 | * We either have a bug with persistent union nlink or a lower |
| 536 | * hardlink was added while overlay is mounted. Adding a lower |
| 537 | * hardlink and then unlinking all overlay hardlinks would drop |
| 538 | * overlay nlink to zero before all upper inodes are unlinked. |
| 539 | * As a safety measure, when that situation is detected, set |
| 540 | * the overlay nlink to the index inode nlink minus one for the |
| 541 | * index entry itself. |
| 542 | */ |
| 543 | set_nlink(d_inode(dentry), inode->i_nlink - 1); |
| 544 | ovl_set_nlink_upper(dentry); |
| 545 | goto out; |
| 546 | } |
| 547 | |
| 548 | inode_lock_nested(dir, I_MUTEX_PARENT); |
Amir Goldstein | e7dd0e7 | 2017-10-24 17:38:33 +0300 | [diff] [blame] | 549 | index = lookup_one_len(name.name, indexdir, name.len); |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 550 | err = PTR_ERR(index); |
Amir Goldstein | e7dd0e7 | 2017-10-24 17:38:33 +0300 | [diff] [blame] | 551 | if (IS_ERR(index)) { |
Amir Goldstein | 9f4ec90 | 2017-09-24 17:36:26 +0300 | [diff] [blame] | 552 | index = NULL; |
Amir Goldstein | e7dd0e7 | 2017-10-24 17:38:33 +0300 | [diff] [blame] | 553 | } else if (ovl_index_all(dentry->d_sb)) { |
| 554 | /* Whiteout orphan index to block future open by handle */ |
| 555 | err = ovl_cleanup_and_whiteout(indexdir, dir, index); |
| 556 | } else { |
| 557 | /* Cleanup orphan index entries */ |
| 558 | err = ovl_cleanup(dir, index); |
| 559 | } |
Amir Goldstein | 9f4ec90 | 2017-09-24 17:36:26 +0300 | [diff] [blame] | 560 | |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 561 | inode_unlock(dir); |
| 562 | if (err) |
| 563 | goto fail; |
| 564 | |
| 565 | out: |
| 566 | dput(index); |
| 567 | return; |
| 568 | |
| 569 | fail: |
| 570 | pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err); |
| 571 | goto out; |
| 572 | } |
| 573 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 574 | /* |
| 575 | * Operations that change overlay inode and upper inode nlink need to be |
| 576 | * synchronized with copy up for persistent nlink accounting. |
| 577 | */ |
| 578 | int ovl_nlink_start(struct dentry *dentry, bool *locked) |
| 579 | { |
| 580 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); |
| 581 | const struct cred *old_cred; |
| 582 | int err; |
| 583 | |
Amir Goldstein | 89a1755 | 2017-09-26 07:40:37 +0300 | [diff] [blame] | 584 | if (!d_inode(dentry)) |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 585 | return 0; |
| 586 | |
| 587 | /* |
| 588 | * With inodes index is enabled, we store the union overlay nlink |
Amir Goldstein | 24b33ee | 2017-09-26 07:55:26 +0300 | [diff] [blame] | 589 | * in an xattr on the index inode. When whiting out an indexed lower, |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 590 | * we need to decrement the overlay persistent nlink, but before the |
| 591 | * first copy up, we have no upper index inode to store the xattr. |
| 592 | * |
Amir Goldstein | 24b33ee | 2017-09-26 07:55:26 +0300 | [diff] [blame] | 593 | * As a workaround, before whiteout/rename over an indexed lower, |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 594 | * copy up to create the upper index. Creating the upper index will |
| 595 | * initialize the overlay nlink, so it could be dropped if unlink |
| 596 | * or rename succeeds. |
| 597 | * |
| 598 | * TODO: implement metadata only index copy up when called with |
| 599 | * ovl_copy_up_flags(dentry, O_PATH). |
| 600 | */ |
Amir Goldstein | 24b33ee | 2017-09-26 07:55:26 +0300 | [diff] [blame] | 601 | if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 602 | err = ovl_copy_up(dentry); |
| 603 | if (err) |
| 604 | return err; |
| 605 | } |
| 606 | |
| 607 | err = mutex_lock_interruptible(&oi->lock); |
| 608 | if (err) |
| 609 | return err; |
| 610 | |
Amir Goldstein | 89a1755 | 2017-09-26 07:40:37 +0300 | [diff] [blame] | 611 | if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 612 | goto out; |
| 613 | |
| 614 | old_cred = ovl_override_creds(dentry->d_sb); |
| 615 | /* |
| 616 | * The overlay inode nlink should be incremented/decremented IFF the |
| 617 | * upper operation succeeds, along with nlink change of upper inode. |
| 618 | * Therefore, before link/unlink/rename, we store the union nlink |
| 619 | * value relative to the upper inode nlink in an upper inode xattr. |
| 620 | */ |
| 621 | err = ovl_set_nlink_upper(dentry); |
| 622 | revert_creds(old_cred); |
| 623 | |
| 624 | out: |
| 625 | if (err) |
| 626 | mutex_unlock(&oi->lock); |
| 627 | else |
| 628 | *locked = true; |
| 629 | |
| 630 | return err; |
| 631 | } |
| 632 | |
| 633 | void ovl_nlink_end(struct dentry *dentry, bool locked) |
| 634 | { |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 635 | if (locked) { |
| 636 | if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) && |
| 637 | d_inode(dentry)->i_nlink == 0) { |
| 638 | const struct cred *old_cred; |
| 639 | |
| 640 | old_cred = ovl_override_creds(dentry->d_sb); |
| 641 | ovl_cleanup_index(dentry); |
| 642 | revert_creds(old_cred); |
| 643 | } |
| 644 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 645 | mutex_unlock(&OVL_I(d_inode(dentry))->lock); |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 646 | } |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 647 | } |
Amir Goldstein | 5820dc0 | 2017-09-25 16:39:55 +0300 | [diff] [blame] | 648 | |
| 649 | int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) |
| 650 | { |
| 651 | /* Workdir should not be the same as upperdir */ |
| 652 | if (workdir == upperdir) |
| 653 | goto err; |
| 654 | |
| 655 | /* Workdir should not be subdir of upperdir and vice versa */ |
| 656 | if (lock_rename(workdir, upperdir) != NULL) |
| 657 | goto err_unlock; |
| 658 | |
| 659 | return 0; |
| 660 | |
| 661 | err_unlock: |
| 662 | unlock_rename(workdir, upperdir); |
| 663 | err: |
| 664 | pr_err("overlayfs: failed to lock workdir+upperdir\n"); |
| 665 | return -EIO; |
| 666 | } |