Thomas Gleixner | b4d0d23 | 2019-05-20 19:08:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 2 | /* CacheFiles extended attribute management |
| 3 | * |
| 4 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 5 | * Written by David Howells (dhowells@redhat.com) |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/sched.h> |
| 10 | #include <linux/file.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/fsnotify.h> |
| 13 | #include <linux/quotaops.h> |
| 14 | #include <linux/xattr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 16 | #include "internal.h" |
| 17 | |
| 18 | static const char cachefiles_xattr_cache[] = |
| 19 | XATTR_USER_PREFIX "CacheFiles.cache"; |
| 20 | |
| 21 | /* |
| 22 | * check the type label on an object |
| 23 | * - done using xattrs |
| 24 | */ |
| 25 | int cachefiles_check_object_type(struct cachefiles_object *object) |
| 26 | { |
| 27 | struct dentry *dentry = object->dentry; |
| 28 | char type[3], xtype[3]; |
| 29 | int ret; |
| 30 | |
| 31 | ASSERT(dentry); |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 32 | ASSERT(d_backing_inode(dentry)); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 33 | |
| 34 | if (!object->fscache.cookie) |
| 35 | strcpy(type, "C3"); |
| 36 | else |
| 37 | snprintf(type, 3, "%02x", object->fscache.cookie->def->type); |
| 38 | |
David Howells | 8beabdd | 2020-10-19 21:40:32 +0100 | [diff] [blame] | 39 | _enter("%x{%s}", object->fscache.debug_id, type); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 40 | |
| 41 | /* attempt to install a type label directly */ |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 42 | ret = vfs_setxattr(&init_user_ns, dentry, cachefiles_xattr_cache, type, |
| 43 | 2, XATTR_CREATE); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 44 | if (ret == 0) { |
| 45 | _debug("SET"); /* we succeeded */ |
| 46 | goto error; |
| 47 | } |
| 48 | |
| 49 | if (ret != -EEXIST) { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 50 | pr_err("Can't set xattr on %pd [%lu] (err %d)\n", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 51 | dentry, d_backing_inode(dentry)->i_ino, |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 52 | -ret); |
| 53 | goto error; |
| 54 | } |
| 55 | |
| 56 | /* read the current type label */ |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 57 | ret = vfs_getxattr(&init_user_ns, dentry, cachefiles_xattr_cache, xtype, |
| 58 | 3); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 59 | if (ret < 0) { |
| 60 | if (ret == -ERANGE) |
| 61 | goto bad_type_length; |
| 62 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 63 | pr_err("Can't read xattr on %pd [%lu] (err %d)\n", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 64 | dentry, d_backing_inode(dentry)->i_ino, |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 65 | -ret); |
| 66 | goto error; |
| 67 | } |
| 68 | |
| 69 | /* check the type is what we're expecting */ |
| 70 | if (ret != 2) |
| 71 | goto bad_type_length; |
| 72 | |
| 73 | if (xtype[0] != type[0] || xtype[1] != type[1]) |
| 74 | goto bad_type; |
| 75 | |
| 76 | ret = 0; |
| 77 | |
| 78 | error: |
| 79 | _leave(" = %d", ret); |
| 80 | return ret; |
| 81 | |
| 82 | bad_type_length: |
Fabian Frederick | 6ff66ac | 2014-09-25 16:05:27 -0700 | [diff] [blame] | 83 | pr_err("Cache object %lu type xattr length incorrect\n", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 84 | d_backing_inode(dentry)->i_ino); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 85 | ret = -EIO; |
| 86 | goto error; |
| 87 | |
| 88 | bad_type: |
| 89 | xtype[2] = 0; |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 90 | pr_err("Cache object %pd [%lu] type %s not %s\n", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 91 | dentry, d_backing_inode(dentry)->i_ino, |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 92 | xtype, type); |
| 93 | ret = -EIO; |
| 94 | goto error; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * set the state xattr on a cache file |
| 99 | */ |
| 100 | int cachefiles_set_object_xattr(struct cachefiles_object *object, |
| 101 | struct cachefiles_xattr *auxdata) |
| 102 | { |
| 103 | struct dentry *dentry = object->dentry; |
| 104 | int ret; |
| 105 | |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 106 | ASSERT(dentry); |
| 107 | |
| 108 | _enter("%p,#%d", object, auxdata->len); |
| 109 | |
| 110 | /* attempt to install the cache metadata directly */ |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 111 | _debug("SET #%u", auxdata->len); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 112 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 113 | clear_bit(FSCACHE_COOKIE_AUX_UPDATED, &object->fscache.cookie->flags); |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 114 | ret = vfs_setxattr(&init_user_ns, dentry, cachefiles_xattr_cache, |
| 115 | &auxdata->type, auxdata->len, XATTR_CREATE); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 116 | if (ret < 0 && ret != -ENOMEM) |
| 117 | cachefiles_io_error_obj( |
| 118 | object, |
| 119 | "Failed to set xattr with error %d", ret); |
| 120 | |
| 121 | _leave(" = %d", ret); |
| 122 | return ret; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * update the state xattr on a cache file |
| 127 | */ |
| 128 | int cachefiles_update_object_xattr(struct cachefiles_object *object, |
| 129 | struct cachefiles_xattr *auxdata) |
| 130 | { |
| 131 | struct dentry *dentry = object->dentry; |
| 132 | int ret; |
| 133 | |
David Howells | e6bc06f | 2018-11-27 16:34:55 +0000 | [diff] [blame] | 134 | if (!dentry) |
| 135 | return -ESTALE; |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 136 | |
David Howells | 8beabdd | 2020-10-19 21:40:32 +0100 | [diff] [blame] | 137 | _enter("%x,#%d", object->fscache.debug_id, auxdata->len); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 138 | |
| 139 | /* attempt to install the cache metadata directly */ |
David Howells | 1362729 | 2013-05-10 19:50:26 +0100 | [diff] [blame] | 140 | _debug("SET #%u", auxdata->len); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 141 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 142 | clear_bit(FSCACHE_COOKIE_AUX_UPDATED, &object->fscache.cookie->flags); |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 143 | ret = vfs_setxattr(&init_user_ns, dentry, cachefiles_xattr_cache, |
| 144 | &auxdata->type, auxdata->len, XATTR_REPLACE); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 145 | if (ret < 0 && ret != -ENOMEM) |
| 146 | cachefiles_io_error_obj( |
| 147 | object, |
| 148 | "Failed to update xattr with error %d", ret); |
| 149 | |
| 150 | _leave(" = %d", ret); |
| 151 | return ret; |
| 152 | } |
| 153 | |
| 154 | /* |
David Howells | 5002d7b | 2013-08-21 17:29:21 -0400 | [diff] [blame] | 155 | * check the consistency between the backing cache and the FS-Cache cookie |
| 156 | */ |
| 157 | int cachefiles_check_auxdata(struct cachefiles_object *object) |
| 158 | { |
| 159 | struct cachefiles_xattr *auxbuf; |
Josh Boyer | 607566a | 2013-09-20 14:17:52 +0100 | [diff] [blame] | 160 | enum fscache_checkaux validity; |
David Howells | 5002d7b | 2013-08-21 17:29:21 -0400 | [diff] [blame] | 161 | struct dentry *dentry = object->dentry; |
Josh Boyer | 607566a | 2013-09-20 14:17:52 +0100 | [diff] [blame] | 162 | ssize_t xlen; |
David Howells | 5002d7b | 2013-08-21 17:29:21 -0400 | [diff] [blame] | 163 | int ret; |
| 164 | |
| 165 | ASSERT(dentry); |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 166 | ASSERT(d_backing_inode(dentry)); |
David Howells | 5002d7b | 2013-08-21 17:29:21 -0400 | [diff] [blame] | 167 | ASSERT(object->fscache.cookie->def->check_aux); |
| 168 | |
| 169 | auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, GFP_KERNEL); |
| 170 | if (!auxbuf) |
| 171 | return -ENOMEM; |
| 172 | |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 173 | xlen = vfs_getxattr(&init_user_ns, dentry, cachefiles_xattr_cache, |
Josh Boyer | 607566a | 2013-09-20 14:17:52 +0100 | [diff] [blame] | 174 | &auxbuf->type, 512 + 1); |
| 175 | ret = -ESTALE; |
| 176 | if (xlen < 1 || |
| 177 | auxbuf->type != object->fscache.cookie->def->type) |
| 178 | goto error; |
David Howells | 5002d7b | 2013-08-21 17:29:21 -0400 | [diff] [blame] | 179 | |
Josh Boyer | 607566a | 2013-09-20 14:17:52 +0100 | [diff] [blame] | 180 | xlen--; |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 181 | validity = fscache_check_aux(&object->fscache, &auxbuf->data, xlen, |
| 182 | i_size_read(d_backing_inode(dentry))); |
Josh Boyer | 607566a | 2013-09-20 14:17:52 +0100 | [diff] [blame] | 183 | if (validity != FSCACHE_CHECKAUX_OKAY) |
| 184 | goto error; |
David Howells | 5002d7b | 2013-08-21 17:29:21 -0400 | [diff] [blame] | 185 | |
Josh Boyer | 607566a | 2013-09-20 14:17:52 +0100 | [diff] [blame] | 186 | ret = 0; |
| 187 | error: |
David Howells | 5002d7b | 2013-08-21 17:29:21 -0400 | [diff] [blame] | 188 | kfree(auxbuf); |
Josh Boyer | 607566a | 2013-09-20 14:17:52 +0100 | [diff] [blame] | 189 | return ret; |
David Howells | 5002d7b | 2013-08-21 17:29:21 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 193 | * check the state xattr on a cache file |
| 194 | * - return -ESTALE if the object should be deleted |
| 195 | */ |
| 196 | int cachefiles_check_object_xattr(struct cachefiles_object *object, |
| 197 | struct cachefiles_xattr *auxdata) |
| 198 | { |
| 199 | struct cachefiles_xattr *auxbuf; |
| 200 | struct dentry *dentry = object->dentry; |
| 201 | int ret; |
| 202 | |
| 203 | _enter("%p,#%d", object, auxdata->len); |
| 204 | |
| 205 | ASSERT(dentry); |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 206 | ASSERT(d_backing_inode(dentry)); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 207 | |
David Howells | 5f4f9f4 | 2012-12-20 21:52:33 +0000 | [diff] [blame] | 208 | auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, cachefiles_gfp); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 209 | if (!auxbuf) { |
| 210 | _leave(" = -ENOMEM"); |
| 211 | return -ENOMEM; |
| 212 | } |
| 213 | |
| 214 | /* read the current type label */ |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 215 | ret = vfs_getxattr(&init_user_ns, dentry, cachefiles_xattr_cache, |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 216 | &auxbuf->type, 512 + 1); |
| 217 | if (ret < 0) { |
| 218 | if (ret == -ENODATA) |
| 219 | goto stale; /* no attribute - power went off |
| 220 | * mid-cull? */ |
| 221 | |
| 222 | if (ret == -ERANGE) |
| 223 | goto bad_type_length; |
| 224 | |
| 225 | cachefiles_io_error_obj(object, |
| 226 | "Can't read xattr on %lu (err %d)", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 227 | d_backing_inode(dentry)->i_ino, -ret); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 228 | goto error; |
| 229 | } |
| 230 | |
| 231 | /* check the on-disk object */ |
| 232 | if (ret < 1) |
| 233 | goto bad_type_length; |
| 234 | |
| 235 | if (auxbuf->type != auxdata->type) |
| 236 | goto stale; |
| 237 | |
| 238 | auxbuf->len = ret; |
| 239 | |
| 240 | /* consult the netfs */ |
| 241 | if (object->fscache.cookie->def->check_aux) { |
| 242 | enum fscache_checkaux result; |
| 243 | unsigned int dlen; |
| 244 | |
| 245 | dlen = auxbuf->len - 1; |
| 246 | |
| 247 | _debug("checkaux %s #%u", |
| 248 | object->fscache.cookie->def->name, dlen); |
| 249 | |
| 250 | result = fscache_check_aux(&object->fscache, |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 251 | &auxbuf->data, dlen, |
| 252 | i_size_read(d_backing_inode(dentry))); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 253 | |
| 254 | switch (result) { |
| 255 | /* entry okay as is */ |
| 256 | case FSCACHE_CHECKAUX_OKAY: |
| 257 | goto okay; |
| 258 | |
| 259 | /* entry requires update */ |
| 260 | case FSCACHE_CHECKAUX_NEEDS_UPDATE: |
| 261 | break; |
| 262 | |
| 263 | /* entry requires deletion */ |
| 264 | case FSCACHE_CHECKAUX_OBSOLETE: |
| 265 | goto stale; |
| 266 | |
| 267 | default: |
| 268 | BUG(); |
| 269 | } |
| 270 | |
| 271 | /* update the current label */ |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 272 | ret = vfs_setxattr(&init_user_ns, dentry, |
| 273 | cachefiles_xattr_cache, &auxdata->type, |
| 274 | auxdata->len, XATTR_REPLACE); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 275 | if (ret < 0) { |
| 276 | cachefiles_io_error_obj(object, |
| 277 | "Can't update xattr on %lu" |
| 278 | " (error %d)", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 279 | d_backing_inode(dentry)->i_ino, -ret); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 280 | goto error; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | okay: |
| 285 | ret = 0; |
| 286 | |
| 287 | error: |
| 288 | kfree(auxbuf); |
| 289 | _leave(" = %d", ret); |
| 290 | return ret; |
| 291 | |
| 292 | bad_type_length: |
Fabian Frederick | 6ff66ac | 2014-09-25 16:05:27 -0700 | [diff] [blame] | 293 | pr_err("Cache object %lu xattr length incorrect\n", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 294 | d_backing_inode(dentry)->i_ino); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 295 | ret = -EIO; |
| 296 | goto error; |
| 297 | |
| 298 | stale: |
| 299 | ret = -ESTALE; |
| 300 | goto error; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * remove the object's xattr to mark it stale |
| 305 | */ |
| 306 | int cachefiles_remove_object_xattr(struct cachefiles_cache *cache, |
| 307 | struct dentry *dentry) |
| 308 | { |
| 309 | int ret; |
| 310 | |
Tycho Andersen | c7c7a1a1 | 2021-01-21 14:19:28 +0100 | [diff] [blame] | 311 | ret = vfs_removexattr(&init_user_ns, dentry, cachefiles_xattr_cache); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 312 | if (ret < 0) { |
| 313 | if (ret == -ENOENT || ret == -ENODATA) |
| 314 | ret = 0; |
| 315 | else if (ret != -ENOMEM) |
| 316 | cachefiles_io_error(cache, |
| 317 | "Can't remove xattr from %lu" |
| 318 | " (error %d)", |
David Howells | 466b77b | 2015-03-17 22:26:21 +0000 | [diff] [blame] | 319 | d_backing_inode(dentry)->i_ino, -ret); |
David Howells | 9ae326a | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | _leave(" = %d", ret); |
| 323 | return ret; |
| 324 | } |