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