xattr.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* CacheFiles extended attribute management
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/file.h>
  14. #include <linux/fs.h>
  15. #include <linux/fsnotify.h>
  16. #include <linux/quotaops.h>
  17. #include <linux/xattr.h>
  18. #include <linux/slab.h>
  19. #include "internal.h"
  20. static const char cachefiles_xattr_cache[] =
  21. XATTR_USER_PREFIX "CacheFiles.cache";
  22. /*
  23. * check the type label on an object
  24. * - done using xattrs
  25. */
  26. int cachefiles_check_object_type(struct cachefiles_object *object)
  27. {
  28. struct dentry *dentry = object->dentry;
  29. char type[3], xtype[3];
  30. int ret;
  31. ASSERT(dentry);
  32. ASSERT(dentry->d_inode);
  33. if (!object->fscache.cookie)
  34. strcpy(type, "C3");
  35. else
  36. snprintf(type, 3, "%02x", object->fscache.cookie->def->type);
  37. _enter("%p{%s}", object, type);
  38. /* attempt to install a type label directly */
  39. ret = vfs_setxattr(dentry, cachefiles_xattr_cache, type, 2,
  40. XATTR_CREATE);
  41. if (ret == 0) {
  42. _debug("SET"); /* we succeeded */
  43. goto error;
  44. }
  45. if (ret != -EEXIST) {
  46. kerror("Can't set xattr on %*.*s [%lu] (err %d)",
  47. dentry->d_name.len, dentry->d_name.len,
  48. dentry->d_name.name, dentry->d_inode->i_ino,
  49. -ret);
  50. goto error;
  51. }
  52. /* read the current type label */
  53. ret = vfs_getxattr(dentry, cachefiles_xattr_cache, xtype, 3);
  54. if (ret < 0) {
  55. if (ret == -ERANGE)
  56. goto bad_type_length;
  57. kerror("Can't read xattr on %*.*s [%lu] (err %d)",
  58. dentry->d_name.len, dentry->d_name.len,
  59. dentry->d_name.name, dentry->d_inode->i_ino,
  60. -ret);
  61. goto error;
  62. }
  63. /* check the type is what we're expecting */
  64. if (ret != 2)
  65. goto bad_type_length;
  66. if (xtype[0] != type[0] || xtype[1] != type[1])
  67. goto bad_type;
  68. ret = 0;
  69. error:
  70. _leave(" = %d", ret);
  71. return ret;
  72. bad_type_length:
  73. kerror("Cache object %lu type xattr length incorrect",
  74. dentry->d_inode->i_ino);
  75. ret = -EIO;
  76. goto error;
  77. bad_type:
  78. xtype[2] = 0;
  79. kerror("Cache object %*.*s [%lu] type %s not %s",
  80. dentry->d_name.len, dentry->d_name.len,
  81. dentry->d_name.name, dentry->d_inode->i_ino,
  82. xtype, type);
  83. ret = -EIO;
  84. goto error;
  85. }
  86. /*
  87. * set the state xattr on a cache file
  88. */
  89. int cachefiles_set_object_xattr(struct cachefiles_object *object,
  90. struct cachefiles_xattr *auxdata)
  91. {
  92. struct dentry *dentry = object->dentry;
  93. int ret;
  94. ASSERT(object->fscache.cookie);
  95. ASSERT(dentry);
  96. _enter("%p,#%d", object, auxdata->len);
  97. /* attempt to install the cache metadata directly */
  98. _debug("SET %s #%u", object->fscache.cookie->def->name, auxdata->len);
  99. ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
  100. &auxdata->type, auxdata->len,
  101. XATTR_CREATE);
  102. if (ret < 0 && ret != -ENOMEM)
  103. cachefiles_io_error_obj(
  104. object,
  105. "Failed to set xattr with error %d", ret);
  106. _leave(" = %d", ret);
  107. return ret;
  108. }
  109. /*
  110. * update the state xattr on a cache file
  111. */
  112. int cachefiles_update_object_xattr(struct cachefiles_object *object,
  113. struct cachefiles_xattr *auxdata)
  114. {
  115. struct dentry *dentry = object->dentry;
  116. int ret;
  117. ASSERT(object->fscache.cookie);
  118. ASSERT(dentry);
  119. _enter("%p,#%d", object, auxdata->len);
  120. /* attempt to install the cache metadata directly */
  121. _debug("SET %s #%u", object->fscache.cookie->def->name, auxdata->len);
  122. ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
  123. &auxdata->type, auxdata->len,
  124. XATTR_REPLACE);
  125. if (ret < 0 && ret != -ENOMEM)
  126. cachefiles_io_error_obj(
  127. object,
  128. "Failed to update xattr with error %d", ret);
  129. _leave(" = %d", ret);
  130. return ret;
  131. }
  132. /*
  133. * check the state xattr on a cache file
  134. * - return -ESTALE if the object should be deleted
  135. */
  136. int cachefiles_check_object_xattr(struct cachefiles_object *object,
  137. struct cachefiles_xattr *auxdata)
  138. {
  139. struct cachefiles_xattr *auxbuf;
  140. struct dentry *dentry = object->dentry;
  141. int ret;
  142. _enter("%p,#%d", object, auxdata->len);
  143. ASSERT(dentry);
  144. ASSERT(dentry->d_inode);
  145. auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, GFP_KERNEL);
  146. if (!auxbuf) {
  147. _leave(" = -ENOMEM");
  148. return -ENOMEM;
  149. }
  150. /* read the current type label */
  151. ret = vfs_getxattr(dentry, cachefiles_xattr_cache,
  152. &auxbuf->type, 512 + 1);
  153. if (ret < 0) {
  154. if (ret == -ENODATA)
  155. goto stale; /* no attribute - power went off
  156. * mid-cull? */
  157. if (ret == -ERANGE)
  158. goto bad_type_length;
  159. cachefiles_io_error_obj(object,
  160. "Can't read xattr on %lu (err %d)",
  161. dentry->d_inode->i_ino, -ret);
  162. goto error;
  163. }
  164. /* check the on-disk object */
  165. if (ret < 1)
  166. goto bad_type_length;
  167. if (auxbuf->type != auxdata->type)
  168. goto stale;
  169. auxbuf->len = ret;
  170. /* consult the netfs */
  171. if (object->fscache.cookie->def->check_aux) {
  172. enum fscache_checkaux result;
  173. unsigned int dlen;
  174. dlen = auxbuf->len - 1;
  175. _debug("checkaux %s #%u",
  176. object->fscache.cookie->def->name, dlen);
  177. result = fscache_check_aux(&object->fscache,
  178. &auxbuf->data, dlen);
  179. switch (result) {
  180. /* entry okay as is */
  181. case FSCACHE_CHECKAUX_OKAY:
  182. goto okay;
  183. /* entry requires update */
  184. case FSCACHE_CHECKAUX_NEEDS_UPDATE:
  185. break;
  186. /* entry requires deletion */
  187. case FSCACHE_CHECKAUX_OBSOLETE:
  188. goto stale;
  189. default:
  190. BUG();
  191. }
  192. /* update the current label */
  193. ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
  194. &auxdata->type, auxdata->len,
  195. XATTR_REPLACE);
  196. if (ret < 0) {
  197. cachefiles_io_error_obj(object,
  198. "Can't update xattr on %lu"
  199. " (error %d)",
  200. dentry->d_inode->i_ino, -ret);
  201. goto error;
  202. }
  203. }
  204. okay:
  205. ret = 0;
  206. error:
  207. kfree(auxbuf);
  208. _leave(" = %d", ret);
  209. return ret;
  210. bad_type_length:
  211. kerror("Cache object %lu xattr length incorrect",
  212. dentry->d_inode->i_ino);
  213. ret = -EIO;
  214. goto error;
  215. stale:
  216. ret = -ESTALE;
  217. goto error;
  218. }
  219. /*
  220. * remove the object's xattr to mark it stale
  221. */
  222. int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
  223. struct dentry *dentry)
  224. {
  225. int ret;
  226. ret = vfs_removexattr(dentry, cachefiles_xattr_cache);
  227. if (ret < 0) {
  228. if (ret == -ENOENT || ret == -ENODATA)
  229. ret = 0;
  230. else if (ret != -ENOMEM)
  231. cachefiles_io_error(cache,
  232. "Can't remove xattr from %lu"
  233. " (error %d)",
  234. dentry->d_inode->i_ino, -ret);
  235. }
  236. _leave(" = %d", ret);
  237. return ret;
  238. }