Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Novell Inc. |
| 4 | * Copyright (C) 2016 Red Hat, Inc. |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/fs.h> |
| 8 | #include <linux/mount.h> |
| 9 | #include <linux/slab.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 10 | #include <linux/cred.h> |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 11 | #include <linux/xattr.h> |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 12 | #include <linux/exportfs.h> |
| 13 | #include <linux/uuid.h> |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 14 | #include <linux/namei.h> |
| 15 | #include <linux/ratelimit.h> |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 16 | #include "overlayfs.h" |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 17 | |
| 18 | int ovl_want_write(struct dentry *dentry) |
| 19 | { |
| 20 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 21 | return mnt_want_write(ovl_upper_mnt(ofs)); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | void ovl_drop_write(struct dentry *dentry) |
| 25 | { |
| 26 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 27 | mnt_drop_write(ovl_upper_mnt(ofs)); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | struct dentry *ovl_workdir(struct dentry *dentry) |
| 31 | { |
| 32 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 33 | return ofs->workdir; |
| 34 | } |
| 35 | |
| 36 | const struct cred *ovl_override_creds(struct super_block *sb) |
| 37 | { |
| 38 | struct ovl_fs *ofs = sb->s_fs_info; |
| 39 | |
| 40 | return override_creds(ofs->creator_cred); |
| 41 | } |
| 42 | |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 43 | /* |
| 44 | * Check if underlying fs supports file handles and try to determine encoding |
| 45 | * type, in order to deduce maximum inode number used by fs. |
| 46 | * |
| 47 | * Return 0 if file handles are not supported. |
| 48 | * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding. |
| 49 | * Return -1 if fs uses a non default encoding with unknown inode size. |
| 50 | */ |
| 51 | int ovl_can_decode_fh(struct super_block *sb) |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 52 | { |
Amir Goldstein | 9df085f | 2018-09-03 09:12:09 +0300 | [diff] [blame] | 53 | if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry) |
Amir Goldstein | e487d88 | 2017-11-07 13:55:04 +0200 | [diff] [blame] | 54 | return 0; |
| 55 | |
| 56 | return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN; |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | struct dentry *ovl_indexdir(struct super_block *sb) |
| 60 | { |
| 61 | struct ovl_fs *ofs = sb->s_fs_info; |
| 62 | |
| 63 | return ofs->indexdir; |
| 64 | } |
| 65 | |
Amir Goldstein | f168f10 | 2018-01-19 11:26:53 +0200 | [diff] [blame] | 66 | /* Index all files on copy up. For now only enabled for NFS export */ |
| 67 | bool ovl_index_all(struct super_block *sb) |
| 68 | { |
| 69 | struct ovl_fs *ofs = sb->s_fs_info; |
| 70 | |
| 71 | return ofs->config.nfs_export && ofs->config.index; |
| 72 | } |
| 73 | |
| 74 | /* Verify lower origin on lookup. For now only enabled for NFS export */ |
| 75 | bool ovl_verify_lower(struct super_block *sb) |
| 76 | { |
| 77 | struct ovl_fs *ofs = sb->s_fs_info; |
| 78 | |
| 79 | return ofs->config.nfs_export && ofs->config.index; |
| 80 | } |
| 81 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 82 | struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
| 83 | { |
| 84 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); |
| 85 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); |
| 86 | |
| 87 | if (oe) |
| 88 | oe->numlower = numlower; |
| 89 | |
| 90 | return oe; |
| 91 | } |
| 92 | |
| 93 | bool ovl_dentry_remote(struct dentry *dentry) |
| 94 | { |
| 95 | return dentry->d_flags & |
Miklos Szeredi | 7925dad | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 96 | (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 97 | } |
| 98 | |
Miklos Szeredi | f428884 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 99 | void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry, |
| 100 | unsigned int mask) |
| 101 | { |
| 102 | struct ovl_entry *oe = OVL_E(dentry); |
| 103 | unsigned int i, flags = 0; |
| 104 | |
Miklos Szeredi | bccece1 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 105 | if (upperdentry) |
| 106 | flags |= upperdentry->d_flags; |
Miklos Szeredi | f428884 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 107 | for (i = 0; i < oe->numlower; i++) |
| 108 | flags |= oe->lowerstack[i].dentry->d_flags; |
| 109 | |
| 110 | spin_lock(&dentry->d_lock); |
| 111 | dentry->d_flags &= ~mask; |
| 112 | dentry->d_flags |= flags & mask; |
| 113 | spin_unlock(&dentry->d_lock); |
| 114 | } |
| 115 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 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) { |
Vivek Goyal | 6012487 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 136 | if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry))) |
| 137 | type |= __OVL_PATH_ORIGIN; |
Vivek Goyal | 0b17c28 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 138 | if (d_is_dir(dentry) || |
| 139 | !ovl_has_upperdata(d_inode(dentry))) |
Amir Goldstein | 5954850 | 2017-04-23 23:12:34 +0300 | [diff] [blame] | 140 | type |= __OVL_PATH_MERGE; |
| 141 | } |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 142 | } else { |
| 143 | if (oe->numlower > 1) |
| 144 | type |= __OVL_PATH_MERGE; |
| 145 | } |
| 146 | return type; |
| 147 | } |
| 148 | |
| 149 | void ovl_path_upper(struct dentry *dentry, struct path *path) |
| 150 | { |
| 151 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 152 | |
Miklos Szeredi | 08f4c7c | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 153 | path->mnt = ovl_upper_mnt(ofs); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 154 | path->dentry = ovl_dentry_upper(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | void ovl_path_lower(struct dentry *dentry, struct path *path) |
| 158 | { |
| 159 | struct ovl_entry *oe = dentry->d_fsdata; |
| 160 | |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 161 | if (oe->numlower) { |
| 162 | path->mnt = oe->lowerstack[0].layer->mnt; |
| 163 | path->dentry = oe->lowerstack[0].dentry; |
| 164 | } else { |
| 165 | *path = (struct path) { }; |
| 166 | } |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Vivek Goyal | 4f93b42 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 169 | void ovl_path_lowerdata(struct dentry *dentry, struct path *path) |
| 170 | { |
| 171 | struct ovl_entry *oe = dentry->d_fsdata; |
| 172 | |
| 173 | if (oe->numlower) { |
| 174 | path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt; |
| 175 | path->dentry = oe->lowerstack[oe->numlower - 1].dentry; |
| 176 | } else { |
| 177 | *path = (struct path) { }; |
| 178 | } |
| 179 | } |
| 180 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 181 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) |
| 182 | { |
| 183 | enum ovl_path_type type = ovl_path_type(dentry); |
| 184 | |
| 185 | if (!OVL_TYPE_UPPER(type)) |
| 186 | ovl_path_lower(dentry, path); |
| 187 | else |
| 188 | ovl_path_upper(dentry, path); |
| 189 | |
| 190 | return type; |
| 191 | } |
| 192 | |
| 193 | struct dentry *ovl_dentry_upper(struct dentry *dentry) |
| 194 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 195 | return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | struct dentry *ovl_dentry_lower(struct dentry *dentry) |
| 199 | { |
| 200 | struct ovl_entry *oe = dentry->d_fsdata; |
| 201 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 202 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Miklos Szeredi | 1346416 | 2020-01-24 09:46:45 +0100 | [diff] [blame] | 205 | const struct ovl_layer *ovl_layer_lower(struct dentry *dentry) |
Amir Goldstein | da309e8 | 2017-11-08 19:39:51 +0200 | [diff] [blame] | 206 | { |
| 207 | struct ovl_entry *oe = dentry->d_fsdata; |
| 208 | |
| 209 | return oe->numlower ? oe->lowerstack[0].layer : NULL; |
| 210 | } |
| 211 | |
Vivek Goyal | 647d253 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 212 | /* |
| 213 | * ovl_dentry_lower() could return either a data dentry or metacopy dentry |
| 214 | * dependig on what is stored in lowerstack[0]. At times we need to find |
| 215 | * lower dentry which has data (and not metacopy dentry). This helper |
| 216 | * returns the lower data dentry. |
| 217 | */ |
| 218 | struct dentry *ovl_dentry_lowerdata(struct dentry *dentry) |
| 219 | { |
| 220 | struct ovl_entry *oe = dentry->d_fsdata; |
| 221 | |
| 222 | return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL; |
| 223 | } |
| 224 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 225 | struct dentry *ovl_dentry_real(struct dentry *dentry) |
| 226 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 227 | return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 230 | struct dentry *ovl_i_dentry_upper(struct inode *inode) |
| 231 | { |
| 232 | return ovl_upperdentry_dereference(OVL_I(inode)); |
| 233 | } |
| 234 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 235 | struct inode *ovl_inode_upper(struct inode *inode) |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 236 | { |
Miklos Szeredi | 1d88f18 | 2017-07-20 11:08:21 +0200 | [diff] [blame] | 237 | struct dentry *upperdentry = ovl_i_dentry_upper(inode); |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 238 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 239 | return upperdentry ? d_inode(upperdentry) : NULL; |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 242 | struct inode *ovl_inode_lower(struct inode *inode) |
| 243 | { |
| 244 | return OVL_I(inode)->lower; |
| 245 | } |
| 246 | |
| 247 | struct inode *ovl_inode_real(struct inode *inode) |
| 248 | { |
| 249 | return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); |
| 250 | } |
| 251 | |
Vivek Goyal | 2664bd0 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 252 | /* Return inode which contains lower data. Do not return metacopy */ |
| 253 | struct inode *ovl_inode_lowerdata(struct inode *inode) |
| 254 | { |
| 255 | if (WARN_ON(!S_ISREG(inode->i_mode))) |
| 256 | return NULL; |
| 257 | |
| 258 | return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode); |
| 259 | } |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 260 | |
Vivek Goyal | 4823d49 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 261 | /* Return real inode which contains data. Does not return metacopy inode */ |
| 262 | struct inode *ovl_inode_realdata(struct inode *inode) |
| 263 | { |
| 264 | struct inode *upperinode; |
| 265 | |
| 266 | upperinode = ovl_inode_upper(inode); |
| 267 | if (upperinode && ovl_has_upperdata(inode)) |
| 268 | return upperinode; |
| 269 | |
| 270 | return ovl_inode_lowerdata(inode); |
| 271 | } |
| 272 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 273 | struct ovl_dir_cache *ovl_dir_cache(struct inode *inode) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 274 | { |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 275 | return OVL_I(inode)->cache; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 278 | 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] | 279 | { |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 280 | OVL_I(inode)->cache = cache; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 283 | void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry) |
| 284 | { |
| 285 | set_bit(flag, &OVL_E(dentry)->flags); |
| 286 | } |
| 287 | |
| 288 | void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry) |
| 289 | { |
| 290 | clear_bit(flag, &OVL_E(dentry)->flags); |
| 291 | } |
| 292 | |
| 293 | bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry) |
| 294 | { |
| 295 | return test_bit(flag, &OVL_E(dentry)->flags); |
| 296 | } |
| 297 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 298 | bool ovl_dentry_is_opaque(struct dentry *dentry) |
| 299 | { |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 300 | return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | bool ovl_dentry_is_whiteout(struct dentry *dentry) |
| 304 | { |
| 305 | return !dentry->d_inode && ovl_dentry_is_opaque(dentry); |
| 306 | } |
| 307 | |
Miklos Szeredi | 5cf5b47 | 2016-12-16 11:02:57 +0100 | [diff] [blame] | 308 | void ovl_dentry_set_opaque(struct dentry *dentry) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 309 | { |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 310 | ovl_dentry_set_flag(OVL_E_OPAQUE, dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Miklos Szeredi | 55acc66 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 313 | /* |
Amir Goldstein | aa3ff3c | 2017-10-15 18:00:20 +0300 | [diff] [blame] | 314 | * For hard links and decoded file handles, it's possible for ovl_dentry_upper() |
| 315 | * to return positive, while there's no actual upper alias for the inode. |
| 316 | * Copy up code needs to know about the existence of the upper alias, so it |
| 317 | * can't use ovl_dentry_upper(). |
Miklos Szeredi | 55acc66 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 318 | */ |
| 319 | bool ovl_dentry_has_upper_alias(struct dentry *dentry) |
| 320 | { |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 321 | return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry); |
Miklos Szeredi | 55acc66 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void ovl_dentry_set_upper_alias(struct dentry *dentry) |
| 325 | { |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 326 | ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry); |
Miklos Szeredi | 55acc66 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 329 | static bool ovl_should_check_upperdata(struct inode *inode) |
| 330 | { |
| 331 | if (!S_ISREG(inode->i_mode)) |
| 332 | return false; |
| 333 | |
| 334 | if (!ovl_inode_lower(inode)) |
| 335 | return false; |
| 336 | |
| 337 | return true; |
| 338 | } |
| 339 | |
| 340 | bool ovl_has_upperdata(struct inode *inode) |
| 341 | { |
| 342 | if (!ovl_should_check_upperdata(inode)) |
| 343 | return true; |
| 344 | |
| 345 | if (!ovl_test_flag(OVL_UPPERDATA, inode)) |
| 346 | return false; |
| 347 | /* |
| 348 | * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of |
| 349 | * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure |
| 350 | * if setting of OVL_UPPERDATA is visible, then effects of writes |
| 351 | * before that are visible too. |
| 352 | */ |
| 353 | smp_rmb(); |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | void ovl_set_upperdata(struct inode *inode) |
| 358 | { |
| 359 | /* |
| 360 | * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure |
| 361 | * if OVL_UPPERDATA flag is visible, then effects of write operations |
| 362 | * before it are visible as well. |
| 363 | */ |
| 364 | smp_wmb(); |
| 365 | ovl_set_flag(OVL_UPPERDATA, inode); |
| 366 | } |
| 367 | |
| 368 | /* Caller should hold ovl_inode->lock */ |
| 369 | bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags) |
| 370 | { |
| 371 | if (!ovl_open_flags_need_copy_up(flags)) |
| 372 | return false; |
| 373 | |
| 374 | return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry)); |
| 375 | } |
| 376 | |
| 377 | bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags) |
| 378 | { |
| 379 | if (!ovl_open_flags_need_copy_up(flags)) |
| 380 | return false; |
| 381 | |
| 382 | return !ovl_has_upperdata(d_inode(dentry)); |
| 383 | } |
| 384 | |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 385 | bool ovl_redirect_dir(struct super_block *sb) |
| 386 | { |
| 387 | struct ovl_fs *ofs = sb->s_fs_info; |
| 388 | |
Amir Goldstein | 21a2287 | 2017-05-17 00:12:41 +0300 | [diff] [blame] | 389 | return ofs->config.redirect_dir && !ofs->noxattr; |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | const char *ovl_dentry_get_redirect(struct dentry *dentry) |
| 393 | { |
Miklos Szeredi | cf31c46 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 394 | return OVL_I(d_inode(dentry))->redirect; |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) |
| 398 | { |
Miklos Szeredi | cf31c46 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 399 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 400 | |
Miklos Szeredi | cf31c46 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 401 | kfree(oi->redirect); |
| 402 | oi->redirect = redirect; |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 403 | } |
| 404 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 405 | void ovl_inode_update(struct inode *inode, struct dentry *upperdentry) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 406 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 407 | struct inode *upperinode = d_inode(upperdentry); |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 408 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 409 | WARN_ON(OVL_I(inode)->__upperdentry); |
| 410 | |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 411 | /* |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 412 | * Make sure upperdentry is consistent before making it visible |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 413 | */ |
| 414 | smp_wmb(); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 415 | OVL_I(inode)->__upperdentry = upperdentry; |
Amir Goldstein | 31747ed | 2018-01-14 18:35:40 +0200 | [diff] [blame] | 416 | if (inode_unhashed(inode)) { |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 417 | inode->i_private = upperinode; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 418 | __insert_inode_hash(inode, (unsigned long) upperinode); |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 419 | } |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Miklos Szeredi | d9854c8 | 2018-07-18 15:44:40 +0200 | [diff] [blame] | 422 | static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 423 | { |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 424 | struct inode *inode = d_inode(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 425 | |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 426 | WARN_ON(!inode_is_locked(inode)); |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 427 | /* |
| 428 | * Version is used by readdir code to keep cache consistent. For merge |
| 429 | * dirs all changes need to be noted. For non-merge dirs, cache only |
| 430 | * contains impure (ones which have been copied up and have origins) |
| 431 | * entries, so only need to note changes to impure entries. |
| 432 | */ |
| 433 | if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity) |
| 434 | OVL_I(inode)->version++; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 435 | } |
| 436 | |
Miklos Szeredi | d9854c8 | 2018-07-18 15:44:40 +0200 | [diff] [blame] | 437 | void ovl_dir_modified(struct dentry *dentry, bool impurity) |
| 438 | { |
| 439 | /* Copy mtime/ctime */ |
| 440 | ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry)); |
| 441 | |
| 442 | ovl_dentry_version_inc(dentry, impurity); |
| 443 | } |
| 444 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 445 | u64 ovl_dentry_version_get(struct dentry *dentry) |
| 446 | { |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 447 | struct inode *inode = d_inode(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 448 | |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 449 | WARN_ON(!inode_is_locked(inode)); |
| 450 | return OVL_I(inode)->version; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | bool ovl_is_whiteout(struct dentry *dentry) |
| 454 | { |
| 455 | struct inode *inode = dentry->d_inode; |
| 456 | |
| 457 | return inode && IS_WHITEOUT(inode); |
| 458 | } |
| 459 | |
| 460 | struct file *ovl_path_open(struct path *path, int flags) |
| 461 | { |
Miklos Szeredi | 56230d9 | 2020-06-02 22:20:26 +0200 | [diff] [blame] | 462 | struct inode *inode = d_inode(path->dentry); |
| 463 | int err, acc_mode; |
| 464 | |
| 465 | if (flags & ~(O_ACCMODE | O_LARGEFILE)) |
| 466 | BUG(); |
| 467 | |
| 468 | switch (flags & O_ACCMODE) { |
| 469 | case O_RDONLY: |
| 470 | acc_mode = MAY_READ; |
| 471 | break; |
| 472 | case O_WRONLY: |
| 473 | acc_mode = MAY_WRITE; |
| 474 | break; |
| 475 | default: |
| 476 | BUG(); |
| 477 | } |
| 478 | |
| 479 | err = inode_permission(inode, acc_mode | MAY_OPEN); |
| 480 | if (err) |
| 481 | return ERR_PTR(err); |
| 482 | |
| 483 | /* O_NOATIME is an optimization, don't fail if not permitted */ |
| 484 | if (inode_owner_or_capable(inode)) |
| 485 | flags |= O_NOATIME; |
| 486 | |
| 487 | return dentry_open(path, flags, current_cred()); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 488 | } |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 489 | |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 490 | /* Caller should hold ovl_inode->lock */ |
| 491 | static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags) |
| 492 | { |
| 493 | bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; |
| 494 | |
| 495 | if (ovl_dentry_upper(dentry) && |
| 496 | (ovl_dentry_has_upper_alias(dentry) || disconnected) && |
| 497 | !ovl_dentry_needs_data_copy_up_locked(dentry, flags)) |
| 498 | return true; |
| 499 | |
| 500 | return false; |
| 501 | } |
| 502 | |
| 503 | bool ovl_already_copied_up(struct dentry *dentry, int flags) |
Vivek Goyal | 2002df8 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 504 | { |
| 505 | bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED; |
| 506 | |
| 507 | /* |
| 508 | * Check if copy-up has happened as well as for upper alias (in |
| 509 | * case of hard links) is there. |
| 510 | * |
| 511 | * Both checks are lockless: |
| 512 | * - false negatives: will recheck under oi->lock |
| 513 | * - false positives: |
| 514 | * + ovl_dentry_upper() uses memory barriers to ensure the |
| 515 | * upper dentry is up-to-date |
| 516 | * + ovl_dentry_has_upper_alias() relies on locking of |
| 517 | * upper parent i_rwsem to prevent reordering copy-up |
| 518 | * with rename. |
| 519 | */ |
| 520 | if (ovl_dentry_upper(dentry) && |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 521 | (ovl_dentry_has_upper_alias(dentry) || disconnected) && |
| 522 | !ovl_dentry_needs_data_copy_up(dentry, flags)) |
Vivek Goyal | 2002df8 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 523 | return true; |
| 524 | |
| 525 | return false; |
| 526 | } |
| 527 | |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 528 | int ovl_copy_up_start(struct dentry *dentry, int flags) |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 529 | { |
Amir Goldstein | 1e92e30 | 2018-10-18 18:37:14 +0300 | [diff] [blame] | 530 | struct inode *inode = d_inode(dentry); |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 531 | int err; |
| 532 | |
Amir Goldstein | 531d304 | 2020-03-02 15:03:35 +0200 | [diff] [blame] | 533 | err = ovl_inode_lock_interruptible(inode); |
Vivek Goyal | 0c28887 | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 534 | if (!err && ovl_already_copied_up_locked(dentry, flags)) { |
Amir Goldstein | a015daf | 2017-06-21 15:28:51 +0300 | [diff] [blame] | 535 | err = 1; /* Already copied up */ |
Amir Goldstein | 1e92e30 | 2018-10-18 18:37:14 +0300 | [diff] [blame] | 536 | ovl_inode_unlock(inode); |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 537 | } |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 538 | |
| 539 | return err; |
| 540 | } |
| 541 | |
| 542 | void ovl_copy_up_end(struct dentry *dentry) |
| 543 | { |
Amir Goldstein | 1e92e30 | 2018-10-18 18:37:14 +0300 | [diff] [blame] | 544 | ovl_inode_unlock(d_inode(dentry)); |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 545 | } |
Amir Goldstein | 82b749b | 2017-05-17 00:12:40 +0300 | [diff] [blame] | 546 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame^] | 547 | bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry) |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 548 | { |
| 549 | int res; |
| 550 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame^] | 551 | res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0); |
Amir Goldstein | b79e05a | 2017-06-25 16:37:17 +0300 | [diff] [blame] | 552 | |
| 553 | /* Zero size value means "copied up but origin unknown" */ |
| 554 | if (res >= 0) |
| 555 | return true; |
| 556 | |
| 557 | return false; |
| 558 | } |
| 559 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame^] | 560 | bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry, |
| 561 | const char *name) |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 562 | { |
| 563 | int res; |
| 564 | char val; |
| 565 | |
| 566 | if (!d_is_dir(dentry)) |
| 567 | return false; |
| 568 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame^] | 569 | res = ovl_do_getxattr(OVL_FS(sb), dentry, name, &val, 1); |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 570 | if (res == 1 && val == 'y') |
| 571 | return true; |
| 572 | |
| 573 | return false; |
| 574 | } |
| 575 | |
Amir Goldstein | 82b749b | 2017-05-17 00:12:40 +0300 | [diff] [blame] | 576 | int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, |
| 577 | const char *name, const void *value, size_t size, |
| 578 | int xerr) |
| 579 | { |
| 580 | int err; |
| 581 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 582 | |
| 583 | if (ofs->noxattr) |
| 584 | return xerr; |
| 585 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame^] | 586 | err = ovl_do_setxattr(ofs, upperdentry, name, value, size); |
Amir Goldstein | 82b749b | 2017-05-17 00:12:40 +0300 | [diff] [blame] | 587 | |
| 588 | if (err == -EOPNOTSUPP) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 589 | pr_warn("cannot set %s xattr on upper\n", name); |
Amir Goldstein | 82b749b | 2017-05-17 00:12:40 +0300 | [diff] [blame] | 590 | ofs->noxattr = true; |
| 591 | return xerr; |
| 592 | } |
| 593 | |
| 594 | return err; |
| 595 | } |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 596 | |
| 597 | int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) |
| 598 | { |
| 599 | int err; |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 600 | |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 601 | if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 602 | return 0; |
| 603 | |
| 604 | /* |
| 605 | * Do not fail when upper doesn't support xattrs. |
| 606 | * Upper inodes won't have origin nor redirect xattr anyway. |
| 607 | */ |
| 608 | err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE, |
| 609 | "y", 1, 0); |
| 610 | if (!err) |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 611 | ovl_set_flag(OVL_IMPURE, d_inode(dentry)); |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 612 | |
| 613 | return err; |
| 614 | } |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 615 | |
| 616 | void ovl_set_flag(unsigned long flag, struct inode *inode) |
| 617 | { |
| 618 | set_bit(flag, &OVL_I(inode)->flags); |
| 619 | } |
| 620 | |
Miklos Szeredi | 4edb83b | 2017-07-27 21:54:06 +0200 | [diff] [blame] | 621 | void ovl_clear_flag(unsigned long flag, struct inode *inode) |
| 622 | { |
| 623 | clear_bit(flag, &OVL_I(inode)->flags); |
| 624 | } |
| 625 | |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 626 | bool ovl_test_flag(unsigned long flag, struct inode *inode) |
| 627 | { |
| 628 | return test_bit(flag, &OVL_I(inode)->flags); |
| 629 | } |
Amir Goldstein | ad0af71 | 2017-06-21 15:28:32 +0300 | [diff] [blame] | 630 | |
| 631 | /** |
| 632 | * Caller must hold a reference to inode to prevent it from being freed while |
| 633 | * it is marked inuse. |
| 634 | */ |
| 635 | bool ovl_inuse_trylock(struct dentry *dentry) |
| 636 | { |
| 637 | struct inode *inode = d_inode(dentry); |
| 638 | bool locked = false; |
| 639 | |
| 640 | spin_lock(&inode->i_lock); |
| 641 | if (!(inode->i_state & I_OVL_INUSE)) { |
| 642 | inode->i_state |= I_OVL_INUSE; |
| 643 | locked = true; |
| 644 | } |
| 645 | spin_unlock(&inode->i_lock); |
| 646 | |
| 647 | return locked; |
| 648 | } |
| 649 | |
| 650 | void ovl_inuse_unlock(struct dentry *dentry) |
| 651 | { |
| 652 | if (dentry) { |
| 653 | struct inode *inode = d_inode(dentry); |
| 654 | |
| 655 | spin_lock(&inode->i_lock); |
| 656 | WARN_ON(!(inode->i_state & I_OVL_INUSE)); |
| 657 | inode->i_state &= ~I_OVL_INUSE; |
| 658 | spin_unlock(&inode->i_lock); |
| 659 | } |
| 660 | } |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 661 | |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 662 | bool ovl_is_inuse(struct dentry *dentry) |
| 663 | { |
| 664 | struct inode *inode = d_inode(dentry); |
| 665 | bool inuse; |
| 666 | |
| 667 | spin_lock(&inode->i_lock); |
| 668 | inuse = (inode->i_state & I_OVL_INUSE); |
| 669 | spin_unlock(&inode->i_lock); |
| 670 | |
| 671 | return inuse; |
| 672 | } |
| 673 | |
Amir Goldstein | 24b33ee | 2017-09-26 07:55:26 +0300 | [diff] [blame] | 674 | /* |
| 675 | * Does this overlay dentry need to be indexed on copy up? |
| 676 | */ |
| 677 | bool ovl_need_index(struct dentry *dentry) |
| 678 | { |
| 679 | struct dentry *lower = ovl_dentry_lower(dentry); |
| 680 | |
| 681 | if (!lower || !ovl_indexdir(dentry->d_sb)) |
| 682 | return false; |
| 683 | |
Amir Goldstein | fbd2d20 | 2017-11-22 00:08:21 +0200 | [diff] [blame] | 684 | /* Index all files for NFS export and consistency verification */ |
Amir Goldstein | 016b720 | 2018-01-11 14:01:08 +0200 | [diff] [blame] | 685 | if (ovl_index_all(dentry->d_sb)) |
Amir Goldstein | fbd2d20 | 2017-11-22 00:08:21 +0200 | [diff] [blame] | 686 | return true; |
| 687 | |
Amir Goldstein | 24b33ee | 2017-09-26 07:55:26 +0300 | [diff] [blame] | 688 | /* Index only lower hardlinks on copy up */ |
| 689 | if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1) |
| 690 | return true; |
| 691 | |
| 692 | return false; |
| 693 | } |
| 694 | |
Amir Goldstein | 9f4ec90 | 2017-09-24 17:36:26 +0300 | [diff] [blame] | 695 | /* Caller must hold OVL_I(inode)->lock */ |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 696 | static void ovl_cleanup_index(struct dentry *dentry) |
| 697 | { |
Amir Goldstein | e7dd0e7 | 2017-10-24 17:38:33 +0300 | [diff] [blame] | 698 | struct dentry *indexdir = ovl_indexdir(dentry->d_sb); |
| 699 | struct inode *dir = indexdir->d_inode; |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 700 | struct dentry *lowerdentry = ovl_dentry_lower(dentry); |
| 701 | struct dentry *upperdentry = ovl_dentry_upper(dentry); |
| 702 | struct dentry *index = NULL; |
| 703 | struct inode *inode; |
Amir Goldstein | 63e1325 | 2018-09-18 16:34:31 +0300 | [diff] [blame] | 704 | struct qstr name = { }; |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 705 | int err; |
| 706 | |
| 707 | err = ovl_get_index_name(lowerdentry, &name); |
| 708 | if (err) |
| 709 | goto fail; |
| 710 | |
| 711 | inode = d_inode(upperdentry); |
Amir Goldstein | 89a1755 | 2017-09-26 07:40:37 +0300 | [diff] [blame] | 712 | if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 713 | pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n", |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 714 | upperdentry, inode->i_ino, inode->i_nlink); |
| 715 | /* |
| 716 | * We either have a bug with persistent union nlink or a lower |
| 717 | * hardlink was added while overlay is mounted. Adding a lower |
| 718 | * hardlink and then unlinking all overlay hardlinks would drop |
| 719 | * overlay nlink to zero before all upper inodes are unlinked. |
| 720 | * As a safety measure, when that situation is detected, set |
| 721 | * the overlay nlink to the index inode nlink minus one for the |
| 722 | * index entry itself. |
| 723 | */ |
| 724 | set_nlink(d_inode(dentry), inode->i_nlink - 1); |
| 725 | ovl_set_nlink_upper(dentry); |
| 726 | goto out; |
| 727 | } |
| 728 | |
| 729 | inode_lock_nested(dir, I_MUTEX_PARENT); |
Amir Goldstein | e7dd0e7 | 2017-10-24 17:38:33 +0300 | [diff] [blame] | 730 | index = lookup_one_len(name.name, indexdir, name.len); |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 731 | err = PTR_ERR(index); |
Amir Goldstein | e7dd0e7 | 2017-10-24 17:38:33 +0300 | [diff] [blame] | 732 | if (IS_ERR(index)) { |
Amir Goldstein | 9f4ec90 | 2017-09-24 17:36:26 +0300 | [diff] [blame] | 733 | index = NULL; |
Amir Goldstein | e7dd0e7 | 2017-10-24 17:38:33 +0300 | [diff] [blame] | 734 | } else if (ovl_index_all(dentry->d_sb)) { |
| 735 | /* Whiteout orphan index to block future open by handle */ |
Chengguang Xu | c21c839 | 2020-04-24 10:55:17 +0800 | [diff] [blame] | 736 | err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb), |
| 737 | dir, index); |
Amir Goldstein | e7dd0e7 | 2017-10-24 17:38:33 +0300 | [diff] [blame] | 738 | } else { |
| 739 | /* Cleanup orphan index entries */ |
| 740 | err = ovl_cleanup(dir, index); |
| 741 | } |
Amir Goldstein | 9f4ec90 | 2017-09-24 17:36:26 +0300 | [diff] [blame] | 742 | |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 743 | inode_unlock(dir); |
| 744 | if (err) |
| 745 | goto fail; |
| 746 | |
| 747 | out: |
Amir Goldstein | 63e1325 | 2018-09-18 16:34:31 +0300 | [diff] [blame] | 748 | kfree(name.name); |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 749 | dput(index); |
| 750 | return; |
| 751 | |
| 752 | fail: |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 753 | pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err); |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 754 | goto out; |
| 755 | } |
| 756 | |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 757 | /* |
| 758 | * Operations that change overlay inode and upper inode nlink need to be |
| 759 | * synchronized with copy up for persistent nlink accounting. |
| 760 | */ |
Amir Goldstein | 0e32992 | 2018-10-18 18:37:13 +0300 | [diff] [blame] | 761 | int ovl_nlink_start(struct dentry *dentry) |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 762 | { |
Amir Goldstein | 1e92e30 | 2018-10-18 18:37:14 +0300 | [diff] [blame] | 763 | struct inode *inode = d_inode(dentry); |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 764 | const struct cred *old_cred; |
| 765 | int err; |
| 766 | |
Amir Goldstein | 1e92e30 | 2018-10-18 18:37:14 +0300 | [diff] [blame] | 767 | if (WARN_ON(!inode)) |
Amir Goldstein | 0e32992 | 2018-10-18 18:37:13 +0300 | [diff] [blame] | 768 | return -ENOENT; |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 769 | |
| 770 | /* |
| 771 | * With inodes index is enabled, we store the union overlay nlink |
Amir Goldstein | 24b33ee | 2017-09-26 07:55:26 +0300 | [diff] [blame] | 772 | * 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] | 773 | * we need to decrement the overlay persistent nlink, but before the |
| 774 | * first copy up, we have no upper index inode to store the xattr. |
| 775 | * |
Amir Goldstein | 24b33ee | 2017-09-26 07:55:26 +0300 | [diff] [blame] | 776 | * As a workaround, before whiteout/rename over an indexed lower, |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 777 | * copy up to create the upper index. Creating the upper index will |
| 778 | * initialize the overlay nlink, so it could be dropped if unlink |
| 779 | * or rename succeeds. |
| 780 | * |
| 781 | * TODO: implement metadata only index copy up when called with |
| 782 | * ovl_copy_up_flags(dentry, O_PATH). |
| 783 | */ |
Amir Goldstein | 24b33ee | 2017-09-26 07:55:26 +0300 | [diff] [blame] | 784 | if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) { |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 785 | err = ovl_copy_up(dentry); |
| 786 | if (err) |
| 787 | return err; |
| 788 | } |
| 789 | |
Amir Goldstein | 531d304 | 2020-03-02 15:03:35 +0200 | [diff] [blame] | 790 | err = ovl_inode_lock_interruptible(inode); |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 791 | if (err) |
| 792 | return err; |
| 793 | |
Amir Goldstein | 1e92e30 | 2018-10-18 18:37:14 +0300 | [diff] [blame] | 794 | if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode)) |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 795 | goto out; |
| 796 | |
| 797 | old_cred = ovl_override_creds(dentry->d_sb); |
| 798 | /* |
| 799 | * The overlay inode nlink should be incremented/decremented IFF the |
| 800 | * upper operation succeeds, along with nlink change of upper inode. |
| 801 | * Therefore, before link/unlink/rename, we store the union nlink |
| 802 | * value relative to the upper inode nlink in an upper inode xattr. |
| 803 | */ |
| 804 | err = ovl_set_nlink_upper(dentry); |
| 805 | revert_creds(old_cred); |
| 806 | |
| 807 | out: |
| 808 | if (err) |
Amir Goldstein | 1e92e30 | 2018-10-18 18:37:14 +0300 | [diff] [blame] | 809 | ovl_inode_unlock(inode); |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 810 | |
| 811 | return err; |
| 812 | } |
| 813 | |
Amir Goldstein | 0e32992 | 2018-10-18 18:37:13 +0300 | [diff] [blame] | 814 | void ovl_nlink_end(struct dentry *dentry) |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 815 | { |
Amir Goldstein | 1e92e30 | 2018-10-18 18:37:14 +0300 | [diff] [blame] | 816 | struct inode *inode = d_inode(dentry); |
| 817 | |
| 818 | if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) { |
Amir Goldstein | 0e32992 | 2018-10-18 18:37:13 +0300 | [diff] [blame] | 819 | const struct cred *old_cred; |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 820 | |
Amir Goldstein | 0e32992 | 2018-10-18 18:37:13 +0300 | [diff] [blame] | 821 | old_cred = ovl_override_creds(dentry->d_sb); |
| 822 | ovl_cleanup_index(dentry); |
| 823 | revert_creds(old_cred); |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 824 | } |
Amir Goldstein | 0e32992 | 2018-10-18 18:37:13 +0300 | [diff] [blame] | 825 | |
Amir Goldstein | 1e92e30 | 2018-10-18 18:37:14 +0300 | [diff] [blame] | 826 | ovl_inode_unlock(inode); |
Amir Goldstein | 5f8415d | 2017-06-20 15:35:14 +0300 | [diff] [blame] | 827 | } |
Amir Goldstein | 5820dc0 | 2017-09-25 16:39:55 +0300 | [diff] [blame] | 828 | |
| 829 | int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir) |
| 830 | { |
| 831 | /* Workdir should not be the same as upperdir */ |
| 832 | if (workdir == upperdir) |
| 833 | goto err; |
| 834 | |
| 835 | /* Workdir should not be subdir of upperdir and vice versa */ |
| 836 | if (lock_rename(workdir, upperdir) != NULL) |
| 837 | goto err_unlock; |
| 838 | |
| 839 | return 0; |
| 840 | |
| 841 | err_unlock: |
| 842 | unlock_rename(workdir, upperdir); |
| 843 | err: |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 844 | pr_err("failed to lock workdir+upperdir\n"); |
Amir Goldstein | 5820dc0 | 2017-09-25 16:39:55 +0300 | [diff] [blame] | 845 | return -EIO; |
| 846 | } |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 847 | |
| 848 | /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */ |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame^] | 849 | int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry) |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 850 | { |
| 851 | int res; |
| 852 | |
| 853 | /* Only regular files can have metacopy xattr */ |
| 854 | if (!S_ISREG(d_inode(dentry)->i_mode)) |
| 855 | return 0; |
| 856 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame^] | 857 | res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0); |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 858 | if (res < 0) { |
| 859 | if (res == -ENODATA || res == -EOPNOTSUPP) |
| 860 | return 0; |
| 861 | goto out; |
| 862 | } |
| 863 | |
| 864 | return 1; |
| 865 | out: |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 866 | pr_warn_ratelimited("failed to get metacopy (%i)\n", res); |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 867 | return res; |
| 868 | } |
Vivek Goyal | 67d756c2 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 869 | |
| 870 | bool ovl_is_metacopy_dentry(struct dentry *dentry) |
| 871 | { |
| 872 | struct ovl_entry *oe = dentry->d_fsdata; |
| 873 | |
| 874 | if (!d_is_reg(dentry)) |
| 875 | return false; |
| 876 | |
| 877 | if (ovl_dentry_upper(dentry)) { |
| 878 | if (!ovl_has_upperdata(d_inode(dentry))) |
| 879 | return true; |
| 880 | return false; |
| 881 | } |
| 882 | |
| 883 | return (oe->numlower > 1); |
| 884 | } |
Vivek Goyal | 0a2d0d3 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 885 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame^] | 886 | char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry, |
| 887 | int padding) |
Vivek Goyal | 0a2d0d3 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 888 | { |
| 889 | int res; |
| 890 | char *s, *next, *buf = NULL; |
| 891 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame^] | 892 | res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0); |
Miklos Szeredi | 92f0d6c | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 893 | if (res == -ENODATA || res == -EOPNOTSUPP) |
Vivek Goyal | 993a0b2 | 2019-01-30 14:01:57 -0500 | [diff] [blame] | 894 | return NULL; |
Vivek Goyal | 0a2d0d3 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 895 | if (res < 0) |
Miklos Szeredi | 92f0d6c | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 896 | goto fail; |
| 897 | if (res == 0) |
| 898 | goto invalid; |
| 899 | |
| 900 | buf = kzalloc(res + padding + 1, GFP_KERNEL); |
| 901 | if (!buf) |
| 902 | return ERR_PTR(-ENOMEM); |
| 903 | |
Miklos Szeredi | 610afc0 | 2020-09-02 10:58:49 +0200 | [diff] [blame^] | 904 | res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res); |
Miklos Szeredi | 92f0d6c | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 905 | if (res < 0) |
| 906 | goto fail; |
Vivek Goyal | 0a2d0d3 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 907 | if (res == 0) |
| 908 | goto invalid; |
| 909 | |
| 910 | if (buf[0] == '/') { |
| 911 | for (s = buf; *s++ == '/'; s = next) { |
| 912 | next = strchrnul(s, '/'); |
| 913 | if (s == next) |
| 914 | goto invalid; |
| 915 | } |
| 916 | } else { |
| 917 | if (strchr(buf, '/') != NULL) |
| 918 | goto invalid; |
| 919 | } |
| 920 | |
| 921 | return buf; |
Vivek Goyal | 0a2d0d3 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 922 | invalid: |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 923 | pr_warn_ratelimited("invalid redirect (%s)\n", buf); |
Vivek Goyal | 0a2d0d3 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 924 | res = -EINVAL; |
Miklos Szeredi | 92f0d6c | 2020-09-02 10:58:48 +0200 | [diff] [blame] | 925 | goto err_free; |
| 926 | fail: |
| 927 | pr_warn_ratelimited("failed to get redirect (%i)\n", res); |
| 928 | err_free: |
Vivek Goyal | 993a0b2 | 2019-01-30 14:01:57 -0500 | [diff] [blame] | 929 | kfree(buf); |
| 930 | return ERR_PTR(res); |
Vivek Goyal | 0a2d0d3 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 931 | } |