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