123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- #include <linux/module.h>
- #include <linux/sched.h>
- #include <linux/file.h>
- #include <linux/fs.h>
- #include <linux/fsnotify.h>
- #include <linux/quotaops.h>
- #include <linux/xattr.h>
- #include <linux/slab.h>
- #include "internal.h"
- static const char cachefiles_xattr_cache[] =
- XATTR_USER_PREFIX "CacheFiles.cache";
- int cachefiles_check_object_type(struct cachefiles_object *object)
- {
- struct dentry *dentry = object->dentry;
- char type[3], xtype[3];
- int ret;
- ASSERT(dentry);
- ASSERT(d_backing_inode(dentry));
- if (!object->fscache.cookie)
- strcpy(type, "C3");
- else
- snprintf(type, 3, "%02x", object->fscache.cookie->def->type);
- _enter("%p{%s}", object, type);
-
- ret = vfs_setxattr(dentry, cachefiles_xattr_cache, type, 2,
- XATTR_CREATE);
- if (ret == 0) {
- _debug("SET");
- goto error;
- }
- if (ret != -EEXIST) {
- pr_err("Can't set xattr on %pd [%lu] (err %d)\n",
- dentry, d_backing_inode(dentry)->i_ino,
- -ret);
- goto error;
- }
-
- ret = vfs_getxattr(dentry, cachefiles_xattr_cache, xtype, 3);
- if (ret < 0) {
- if (ret == -ERANGE)
- goto bad_type_length;
- pr_err("Can't read xattr on %pd [%lu] (err %d)\n",
- dentry, d_backing_inode(dentry)->i_ino,
- -ret);
- goto error;
- }
-
- if (ret != 2)
- goto bad_type_length;
- if (xtype[0] != type[0] || xtype[1] != type[1])
- goto bad_type;
- ret = 0;
- error:
- _leave(" = %d", ret);
- return ret;
- bad_type_length:
- pr_err("Cache object %lu type xattr length incorrect\n",
- d_backing_inode(dentry)->i_ino);
- ret = -EIO;
- goto error;
- bad_type:
- xtype[2] = 0;
- pr_err("Cache object %pd [%lu] type %s not %s\n",
- dentry, d_backing_inode(dentry)->i_ino,
- xtype, type);
- ret = -EIO;
- goto error;
- }
- int cachefiles_set_object_xattr(struct cachefiles_object *object,
- struct cachefiles_xattr *auxdata)
- {
- struct dentry *dentry = object->dentry;
- int ret;
- ASSERT(dentry);
- _enter("%p,#%d", object, auxdata->len);
-
- _debug("SET #%u", auxdata->len);
- ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
- &auxdata->type, auxdata->len,
- XATTR_CREATE);
- if (ret < 0 && ret != -ENOMEM)
- cachefiles_io_error_obj(
- object,
- "Failed to set xattr with error %d", ret);
- _leave(" = %d", ret);
- return ret;
- }
- int cachefiles_update_object_xattr(struct cachefiles_object *object,
- struct cachefiles_xattr *auxdata)
- {
- struct dentry *dentry = object->dentry;
- int ret;
- ASSERT(dentry);
- _enter("%p,#%d", object, auxdata->len);
-
- _debug("SET #%u", auxdata->len);
- ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
- &auxdata->type, auxdata->len,
- XATTR_REPLACE);
- if (ret < 0 && ret != -ENOMEM)
- cachefiles_io_error_obj(
- object,
- "Failed to update xattr with error %d", ret);
- _leave(" = %d", ret);
- return ret;
- }
- int cachefiles_check_auxdata(struct cachefiles_object *object)
- {
- struct cachefiles_xattr *auxbuf;
- enum fscache_checkaux validity;
- struct dentry *dentry = object->dentry;
- ssize_t xlen;
- int ret;
- ASSERT(dentry);
- ASSERT(d_backing_inode(dentry));
- ASSERT(object->fscache.cookie->def->check_aux);
- auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, GFP_KERNEL);
- if (!auxbuf)
- return -ENOMEM;
- xlen = vfs_getxattr(dentry, cachefiles_xattr_cache,
- &auxbuf->type, 512 + 1);
- ret = -ESTALE;
- if (xlen < 1 ||
- auxbuf->type != object->fscache.cookie->def->type)
- goto error;
- xlen--;
- validity = fscache_check_aux(&object->fscache, &auxbuf->data, xlen);
- if (validity != FSCACHE_CHECKAUX_OKAY)
- goto error;
- ret = 0;
- error:
- kfree(auxbuf);
- return ret;
- }
- int cachefiles_check_object_xattr(struct cachefiles_object *object,
- struct cachefiles_xattr *auxdata)
- {
- struct cachefiles_xattr *auxbuf;
- struct dentry *dentry = object->dentry;
- int ret;
- _enter("%p,#%d", object, auxdata->len);
- ASSERT(dentry);
- ASSERT(d_backing_inode(dentry));
- auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, cachefiles_gfp);
- if (!auxbuf) {
- _leave(" = -ENOMEM");
- return -ENOMEM;
- }
-
- ret = vfs_getxattr(dentry, cachefiles_xattr_cache,
- &auxbuf->type, 512 + 1);
- if (ret < 0) {
- if (ret == -ENODATA)
- goto stale;
- if (ret == -ERANGE)
- goto bad_type_length;
- cachefiles_io_error_obj(object,
- "Can't read xattr on %lu (err %d)",
- d_backing_inode(dentry)->i_ino, -ret);
- goto error;
- }
-
- if (ret < 1)
- goto bad_type_length;
- if (auxbuf->type != auxdata->type)
- goto stale;
- auxbuf->len = ret;
-
- if (object->fscache.cookie->def->check_aux) {
- enum fscache_checkaux result;
- unsigned int dlen;
- dlen = auxbuf->len - 1;
- _debug("checkaux %s #%u",
- object->fscache.cookie->def->name, dlen);
- result = fscache_check_aux(&object->fscache,
- &auxbuf->data, dlen);
- switch (result) {
-
- case FSCACHE_CHECKAUX_OKAY:
- goto okay;
-
- case FSCACHE_CHECKAUX_NEEDS_UPDATE:
- break;
-
- case FSCACHE_CHECKAUX_OBSOLETE:
- goto stale;
- default:
- BUG();
- }
-
- ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
- &auxdata->type, auxdata->len,
- XATTR_REPLACE);
- if (ret < 0) {
- cachefiles_io_error_obj(object,
- "Can't update xattr on %lu"
- " (error %d)",
- d_backing_inode(dentry)->i_ino, -ret);
- goto error;
- }
- }
- okay:
- ret = 0;
- error:
- kfree(auxbuf);
- _leave(" = %d", ret);
- return ret;
- bad_type_length:
- pr_err("Cache object %lu xattr length incorrect\n",
- d_backing_inode(dentry)->i_ino);
- ret = -EIO;
- goto error;
- stale:
- ret = -ESTALE;
- goto error;
- }
- int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
- struct dentry *dentry)
- {
- int ret;
- ret = vfs_removexattr(dentry, cachefiles_xattr_cache);
- if (ret < 0) {
- if (ret == -ENOENT || ret == -ENODATA)
- ret = 0;
- else if (ret != -ENOMEM)
- cachefiles_io_error(cache,
- "Can't remove xattr from %lu"
- " (error %d)",
- d_backing_inode(dentry)->i_ino, -ret);
- }
- _leave(" = %d", ret);
- return ret;
- }
|