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