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> |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 17 | #include "overlayfs.h" |
| 18 | #include "ovl_entry.h" |
| 19 | |
| 20 | int ovl_want_write(struct dentry *dentry) |
| 21 | { |
| 22 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 23 | return mnt_want_write(ofs->upper_mnt); |
| 24 | } |
| 25 | |
| 26 | void ovl_drop_write(struct dentry *dentry) |
| 27 | { |
| 28 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 29 | mnt_drop_write(ofs->upper_mnt); |
| 30 | } |
| 31 | |
| 32 | struct dentry *ovl_workdir(struct dentry *dentry) |
| 33 | { |
| 34 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 35 | return ofs->workdir; |
| 36 | } |
| 37 | |
| 38 | const struct cred *ovl_override_creds(struct super_block *sb) |
| 39 | { |
| 40 | struct ovl_fs *ofs = sb->s_fs_info; |
| 41 | |
| 42 | return override_creds(ofs->creator_cred); |
| 43 | } |
| 44 | |
Amir Goldstein | 7bcd74b | 2017-03-22 08:42:21 -0400 | [diff] [blame] | 45 | struct super_block *ovl_same_sb(struct super_block *sb) |
| 46 | { |
| 47 | struct ovl_fs *ofs = sb->s_fs_info; |
| 48 | |
| 49 | return ofs->same_sb; |
| 50 | } |
| 51 | |
Amir Goldstein | 02bcd15 | 2017-06-21 15:28:36 +0300 | [diff] [blame] | 52 | bool ovl_can_decode_fh(struct super_block *sb) |
| 53 | { |
| 54 | return (sb->s_export_op && sb->s_export_op->fh_to_dentry && |
| 55 | !uuid_is_null(&sb->s_uuid)); |
| 56 | } |
| 57 | |
| 58 | struct dentry *ovl_indexdir(struct super_block *sb) |
| 59 | { |
| 60 | struct ovl_fs *ofs = sb->s_fs_info; |
| 61 | |
| 62 | return ofs->indexdir; |
| 63 | } |
| 64 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 65 | struct ovl_entry *ovl_alloc_entry(unsigned int numlower) |
| 66 | { |
| 67 | size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); |
| 68 | struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); |
| 69 | |
| 70 | if (oe) |
| 71 | oe->numlower = numlower; |
| 72 | |
| 73 | return oe; |
| 74 | } |
| 75 | |
| 76 | bool ovl_dentry_remote(struct dentry *dentry) |
| 77 | { |
| 78 | return dentry->d_flags & |
| 79 | (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE | |
| 80 | DCACHE_OP_REAL); |
| 81 | } |
| 82 | |
| 83 | bool ovl_dentry_weird(struct dentry *dentry) |
| 84 | { |
| 85 | return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | |
| 86 | DCACHE_MANAGE_TRANSIT | |
| 87 | DCACHE_OP_HASH | |
| 88 | DCACHE_OP_COMPARE); |
| 89 | } |
| 90 | |
| 91 | enum ovl_path_type ovl_path_type(struct dentry *dentry) |
| 92 | { |
| 93 | struct ovl_entry *oe = dentry->d_fsdata; |
| 94 | enum ovl_path_type type = 0; |
| 95 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 96 | if (ovl_dentry_upper(dentry)) { |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 97 | type = __OVL_PATH_UPPER; |
| 98 | |
| 99 | /* |
Amir Goldstein | 5954850 | 2017-04-23 23:12:34 +0300 | [diff] [blame] | 100 | * Non-dir dentry can hold lower dentry of its copy up origin. |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 101 | */ |
Amir Goldstein | 5954850 | 2017-04-23 23:12:34 +0300 | [diff] [blame] | 102 | if (oe->numlower) { |
| 103 | type |= __OVL_PATH_ORIGIN; |
| 104 | if (d_is_dir(dentry)) |
| 105 | type |= __OVL_PATH_MERGE; |
| 106 | } |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 107 | } else { |
| 108 | if (oe->numlower > 1) |
| 109 | type |= __OVL_PATH_MERGE; |
| 110 | } |
| 111 | return type; |
| 112 | } |
| 113 | |
| 114 | void ovl_path_upper(struct dentry *dentry, struct path *path) |
| 115 | { |
| 116 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 117 | |
| 118 | path->mnt = ofs->upper_mnt; |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 119 | path->dentry = ovl_dentry_upper(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void ovl_path_lower(struct dentry *dentry, struct path *path) |
| 123 | { |
| 124 | struct ovl_entry *oe = dentry->d_fsdata; |
| 125 | |
Kees Cook | 33006cd | 2017-03-29 14:02:19 -0700 | [diff] [blame] | 126 | *path = oe->numlower ? oe->lowerstack[0] : (struct path) { }; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) |
| 130 | { |
| 131 | enum ovl_path_type type = ovl_path_type(dentry); |
| 132 | |
| 133 | if (!OVL_TYPE_UPPER(type)) |
| 134 | ovl_path_lower(dentry, path); |
| 135 | else |
| 136 | ovl_path_upper(dentry, path); |
| 137 | |
| 138 | return type; |
| 139 | } |
| 140 | |
| 141 | struct dentry *ovl_dentry_upper(struct dentry *dentry) |
| 142 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 143 | return ovl_upperdentry_dereference(OVL_I(d_inode(dentry))); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | struct dentry *ovl_dentry_lower(struct dentry *dentry) |
| 147 | { |
| 148 | struct ovl_entry *oe = dentry->d_fsdata; |
| 149 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 150 | return oe->numlower ? oe->lowerstack[0].dentry : NULL; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | struct dentry *ovl_dentry_real(struct dentry *dentry) |
| 154 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 155 | return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 158 | struct inode *ovl_inode_upper(struct inode *inode) |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 159 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 160 | struct dentry *upperdentry = ovl_upperdentry_dereference(OVL_I(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 | return upperdentry ? d_inode(upperdentry) : NULL; |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 163 | } |
| 164 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 165 | struct inode *ovl_inode_lower(struct inode *inode) |
| 166 | { |
| 167 | return OVL_I(inode)->lower; |
| 168 | } |
| 169 | |
| 170 | struct inode *ovl_inode_real(struct inode *inode) |
| 171 | { |
| 172 | return ovl_inode_upper(inode) ?: ovl_inode_lower(inode); |
| 173 | } |
| 174 | |
| 175 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 176 | struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) |
| 177 | { |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 178 | return OVL_I(d_inode(dentry))->cache; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) |
| 182 | { |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 183 | OVL_I(d_inode(dentry))->cache = cache; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | bool ovl_dentry_is_opaque(struct dentry *dentry) |
| 187 | { |
| 188 | struct ovl_entry *oe = dentry->d_fsdata; |
| 189 | return oe->opaque; |
| 190 | } |
| 191 | |
| 192 | bool ovl_dentry_is_whiteout(struct dentry *dentry) |
| 193 | { |
| 194 | return !dentry->d_inode && ovl_dentry_is_opaque(dentry); |
| 195 | } |
| 196 | |
Miklos Szeredi | 5cf5b47 | 2016-12-16 11:02:57 +0100 | [diff] [blame] | 197 | void ovl_dentry_set_opaque(struct dentry *dentry) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 198 | { |
| 199 | struct ovl_entry *oe = dentry->d_fsdata; |
Miklos Szeredi | 5cf5b47 | 2016-12-16 11:02:57 +0100 | [diff] [blame] | 200 | |
| 201 | oe->opaque = true; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Miklos Szeredi | 55acc66 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 204 | /* |
| 205 | * For hard links it's possible for ovl_dentry_upper() to return positive, while |
| 206 | * there's no actual upper alias for the inode. Copy up code needs to know |
| 207 | * about the existence of the upper alias, so it can't use ovl_dentry_upper(). |
| 208 | */ |
| 209 | bool ovl_dentry_has_upper_alias(struct dentry *dentry) |
| 210 | { |
| 211 | struct ovl_entry *oe = dentry->d_fsdata; |
| 212 | |
| 213 | return oe->has_upper; |
| 214 | } |
| 215 | |
| 216 | void ovl_dentry_set_upper_alias(struct dentry *dentry) |
| 217 | { |
| 218 | struct ovl_entry *oe = dentry->d_fsdata; |
| 219 | |
| 220 | oe->has_upper = true; |
| 221 | } |
| 222 | |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 223 | bool ovl_redirect_dir(struct super_block *sb) |
| 224 | { |
| 225 | struct ovl_fs *ofs = sb->s_fs_info; |
| 226 | |
Amir Goldstein | 21a2287 | 2017-05-17 00:12:41 +0300 | [diff] [blame] | 227 | return ofs->config.redirect_dir && !ofs->noxattr; |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | const char *ovl_dentry_get_redirect(struct dentry *dentry) |
| 231 | { |
Miklos Szeredi | cf31c46 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 232 | return OVL_I(d_inode(dentry))->redirect; |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect) |
| 236 | { |
Miklos Szeredi | cf31c46 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 237 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 238 | |
Miklos Szeredi | cf31c46 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 239 | kfree(oi->redirect); |
| 240 | oi->redirect = redirect; |
Miklos Szeredi | a6c6065 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 243 | void ovl_inode_init(struct inode *inode, struct dentry *upperdentry, |
| 244 | struct dentry *lowerdentry) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 245 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 246 | if (upperdentry) |
| 247 | OVL_I(inode)->__upperdentry = upperdentry; |
| 248 | if (lowerdentry) |
| 249 | OVL_I(inode)->lower = d_inode(lowerdentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 250 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 251 | ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode); |
Miklos Szeredi | bbb1e54 | 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_update(struct inode *inode, struct dentry *upperdentry) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 255 | { |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 256 | struct inode *upperinode = d_inode(upperdentry); |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 257 | |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 258 | WARN_ON(OVL_I(inode)->__upperdentry); |
| 259 | |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 260 | /* |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 261 | * Make sure upperdentry is consistent before making it visible |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 262 | */ |
| 263 | smp_wmb(); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 264 | OVL_I(inode)->__upperdentry = upperdentry; |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 265 | if (!S_ISDIR(upperinode->i_mode) && inode_unhashed(inode)) { |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 266 | inode->i_private = upperinode; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 267 | __insert_inode_hash(inode, (unsigned long) upperinode); |
Miklos Szeredi | 25b7713 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 268 | } |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void ovl_dentry_version_inc(struct dentry *dentry) |
| 272 | { |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 273 | struct inode *inode = d_inode(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 274 | |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 275 | WARN_ON(!inode_is_locked(inode)); |
| 276 | OVL_I(inode)->version++; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | u64 ovl_dentry_version_get(struct dentry *dentry) |
| 280 | { |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 281 | struct inode *inode = d_inode(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 282 | |
Miklos Szeredi | 04a01ac | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 283 | WARN_ON(!inode_is_locked(inode)); |
| 284 | return OVL_I(inode)->version; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | bool ovl_is_whiteout(struct dentry *dentry) |
| 288 | { |
| 289 | struct inode *inode = dentry->d_inode; |
| 290 | |
| 291 | return inode && IS_WHITEOUT(inode); |
| 292 | } |
| 293 | |
| 294 | struct file *ovl_path_open(struct path *path, int flags) |
| 295 | { |
| 296 | return dentry_open(path, flags | O_NOATIME, current_cred()); |
| 297 | } |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 298 | |
| 299 | int ovl_copy_up_start(struct dentry *dentry) |
| 300 | { |
Amir Goldstein | a015daf | 2017-06-21 15:28:51 +0300 | [diff] [blame] | 301 | struct ovl_inode *oi = OVL_I(d_inode(dentry)); |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 302 | int err; |
| 303 | |
Amir Goldstein | a015daf | 2017-06-21 15:28:51 +0300 | [diff] [blame] | 304 | err = mutex_lock_interruptible(&oi->lock); |
Amir Goldstein | 59be097 | 2017-06-20 15:25:46 +0300 | [diff] [blame^] | 305 | if (!err && ovl_dentry_has_upper_alias(dentry)) { |
Amir Goldstein | a015daf | 2017-06-21 15:28:51 +0300 | [diff] [blame] | 306 | err = 1; /* Already copied up */ |
| 307 | mutex_unlock(&oi->lock); |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 308 | } |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 309 | |
| 310 | return err; |
| 311 | } |
| 312 | |
| 313 | void ovl_copy_up_end(struct dentry *dentry) |
| 314 | { |
Amir Goldstein | a015daf | 2017-06-21 15:28:51 +0300 | [diff] [blame] | 315 | mutex_unlock(&OVL_I(d_inode(dentry))->lock); |
Amir Goldstein | 39d3d60 | 2017-01-17 06:34:56 +0200 | [diff] [blame] | 316 | } |
Amir Goldstein | 82b749b | 2017-05-17 00:12:40 +0300 | [diff] [blame] | 317 | |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 318 | bool ovl_check_dir_xattr(struct dentry *dentry, const char *name) |
| 319 | { |
| 320 | int res; |
| 321 | char val; |
| 322 | |
| 323 | if (!d_is_dir(dentry)) |
| 324 | return false; |
| 325 | |
| 326 | res = vfs_getxattr(dentry, name, &val, 1); |
| 327 | if (res == 1 && val == 'y') |
| 328 | return true; |
| 329 | |
| 330 | return false; |
| 331 | } |
| 332 | |
Amir Goldstein | 82b749b | 2017-05-17 00:12:40 +0300 | [diff] [blame] | 333 | int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry, |
| 334 | const char *name, const void *value, size_t size, |
| 335 | int xerr) |
| 336 | { |
| 337 | int err; |
| 338 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 339 | |
| 340 | if (ofs->noxattr) |
| 341 | return xerr; |
| 342 | |
| 343 | err = ovl_do_setxattr(upperdentry, name, value, size, 0); |
| 344 | |
| 345 | if (err == -EOPNOTSUPP) { |
| 346 | pr_warn("overlayfs: cannot set %s xattr on upper\n", name); |
| 347 | ofs->noxattr = true; |
| 348 | return xerr; |
| 349 | } |
| 350 | |
| 351 | return err; |
| 352 | } |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 353 | |
| 354 | int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry) |
| 355 | { |
| 356 | int err; |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 357 | |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 358 | if (ovl_test_flag(OVL_IMPURE, d_inode(dentry))) |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 359 | return 0; |
| 360 | |
| 361 | /* |
| 362 | * Do not fail when upper doesn't support xattrs. |
| 363 | * Upper inodes won't have origin nor redirect xattr anyway. |
| 364 | */ |
| 365 | err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE, |
| 366 | "y", 1, 0); |
| 367 | if (!err) |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 368 | ovl_set_flag(OVL_IMPURE, d_inode(dentry)); |
Amir Goldstein | f3a1568 | 2017-05-24 15:29:33 +0300 | [diff] [blame] | 369 | |
| 370 | return err; |
| 371 | } |
Miklos Szeredi | 13c7207 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 372 | |
| 373 | void ovl_set_flag(unsigned long flag, struct inode *inode) |
| 374 | { |
| 375 | set_bit(flag, &OVL_I(inode)->flags); |
| 376 | } |
| 377 | |
| 378 | bool ovl_test_flag(unsigned long flag, struct inode *inode) |
| 379 | { |
| 380 | return test_bit(flag, &OVL_I(inode)->flags); |
| 381 | } |
Amir Goldstein | ad0af71 | 2017-06-21 15:28:32 +0300 | [diff] [blame] | 382 | |
| 383 | /** |
| 384 | * Caller must hold a reference to inode to prevent it from being freed while |
| 385 | * it is marked inuse. |
| 386 | */ |
| 387 | bool ovl_inuse_trylock(struct dentry *dentry) |
| 388 | { |
| 389 | struct inode *inode = d_inode(dentry); |
| 390 | bool locked = false; |
| 391 | |
| 392 | spin_lock(&inode->i_lock); |
| 393 | if (!(inode->i_state & I_OVL_INUSE)) { |
| 394 | inode->i_state |= I_OVL_INUSE; |
| 395 | locked = true; |
| 396 | } |
| 397 | spin_unlock(&inode->i_lock); |
| 398 | |
| 399 | return locked; |
| 400 | } |
| 401 | |
| 402 | void ovl_inuse_unlock(struct dentry *dentry) |
| 403 | { |
| 404 | if (dentry) { |
| 405 | struct inode *inode = d_inode(dentry); |
| 406 | |
| 407 | spin_lock(&inode->i_lock); |
| 408 | WARN_ON(!(inode->i_state & I_OVL_INUSE)); |
| 409 | inode->i_state &= ~I_OVL_INUSE; |
| 410 | spin_unlock(&inode->i_lock); |
| 411 | } |
| 412 | } |