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