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