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