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