xattr.c 6.4 KB

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