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