xattr.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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(d_backing_inode(dentry));
  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. pr_err("Can't set xattr on %pd [%lu] (err %d)\n",
  47. dentry, d_backing_inode(dentry)->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. pr_err("Can't read xattr on %pd [%lu] (err %d)\n",
  57. dentry, d_backing_inode(dentry)->i_ino,
  58. -ret);
  59. goto error;
  60. }
  61. /* check the type is what we're expecting */
  62. if (ret != 2)
  63. goto bad_type_length;
  64. if (xtype[0] != type[0] || xtype[1] != type[1])
  65. goto bad_type;
  66. ret = 0;
  67. error:
  68. _leave(" = %d", ret);
  69. return ret;
  70. bad_type_length:
  71. pr_err("Cache object %lu type xattr length incorrect\n",
  72. d_backing_inode(dentry)->i_ino);
  73. ret = -EIO;
  74. goto error;
  75. bad_type:
  76. xtype[2] = 0;
  77. pr_err("Cache object %pd [%lu] type %s not %s\n",
  78. dentry, d_backing_inode(dentry)->i_ino,
  79. xtype, type);
  80. ret = -EIO;
  81. goto error;
  82. }
  83. /*
  84. * set the state xattr on a cache file
  85. */
  86. int cachefiles_set_object_xattr(struct cachefiles_object *object,
  87. struct cachefiles_xattr *auxdata)
  88. {
  89. struct dentry *dentry = object->dentry;
  90. int ret;
  91. ASSERT(dentry);
  92. _enter("%p,#%d", object, auxdata->len);
  93. /* attempt to install the cache metadata directly */
  94. _debug("SET #%u", auxdata->len);
  95. clear_bit(FSCACHE_COOKIE_AUX_UPDATED, &object->fscache.cookie->flags);
  96. ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
  97. &auxdata->type, auxdata->len,
  98. XATTR_CREATE);
  99. if (ret < 0 && ret != -ENOMEM)
  100. cachefiles_io_error_obj(
  101. object,
  102. "Failed to set xattr with error %d", ret);
  103. _leave(" = %d", ret);
  104. return ret;
  105. }
  106. /*
  107. * update the state xattr on a cache file
  108. */
  109. int cachefiles_update_object_xattr(struct cachefiles_object *object,
  110. struct cachefiles_xattr *auxdata)
  111. {
  112. struct dentry *dentry = object->dentry;
  113. int ret;
  114. if (!dentry)
  115. return -ESTALE;
  116. _enter("%p,#%d", object, auxdata->len);
  117. /* attempt to install the cache metadata directly */
  118. _debug("SET #%u", auxdata->len);
  119. clear_bit(FSCACHE_COOKIE_AUX_UPDATED, &object->fscache.cookie->flags);
  120. ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
  121. &auxdata->type, auxdata->len,
  122. XATTR_REPLACE);
  123. if (ret < 0 && ret != -ENOMEM)
  124. cachefiles_io_error_obj(
  125. object,
  126. "Failed to update xattr with error %d", ret);
  127. _leave(" = %d", ret);
  128. return ret;
  129. }
  130. /*
  131. * check the consistency between the backing cache and the FS-Cache cookie
  132. */
  133. int cachefiles_check_auxdata(struct cachefiles_object *object)
  134. {
  135. struct cachefiles_xattr *auxbuf;
  136. enum fscache_checkaux validity;
  137. struct dentry *dentry = object->dentry;
  138. ssize_t xlen;
  139. int ret;
  140. ASSERT(dentry);
  141. ASSERT(d_backing_inode(dentry));
  142. ASSERT(object->fscache.cookie->def->check_aux);
  143. auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, GFP_KERNEL);
  144. if (!auxbuf)
  145. return -ENOMEM;
  146. xlen = vfs_getxattr(dentry, cachefiles_xattr_cache,
  147. &auxbuf->type, 512 + 1);
  148. ret = -ESTALE;
  149. if (xlen < 1 ||
  150. auxbuf->type != object->fscache.cookie->def->type)
  151. goto error;
  152. xlen--;
  153. validity = fscache_check_aux(&object->fscache, &auxbuf->data, xlen,
  154. i_size_read(d_backing_inode(dentry)));
  155. if (validity != FSCACHE_CHECKAUX_OKAY)
  156. goto error;
  157. ret = 0;
  158. error:
  159. kfree(auxbuf);
  160. return ret;
  161. }
  162. /*
  163. * check the state xattr on a cache file
  164. * - return -ESTALE if the object should be deleted
  165. */
  166. int cachefiles_check_object_xattr(struct cachefiles_object *object,
  167. struct cachefiles_xattr *auxdata)
  168. {
  169. struct cachefiles_xattr *auxbuf;
  170. struct dentry *dentry = object->dentry;
  171. int ret;
  172. _enter("%p,#%d", object, auxdata->len);
  173. ASSERT(dentry);
  174. ASSERT(d_backing_inode(dentry));
  175. auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, cachefiles_gfp);
  176. if (!auxbuf) {
  177. _leave(" = -ENOMEM");
  178. return -ENOMEM;
  179. }
  180. /* read the current type label */
  181. ret = vfs_getxattr(dentry, cachefiles_xattr_cache,
  182. &auxbuf->type, 512 + 1);
  183. if (ret < 0) {
  184. if (ret == -ENODATA)
  185. goto stale; /* no attribute - power went off
  186. * mid-cull? */
  187. if (ret == -ERANGE)
  188. goto bad_type_length;
  189. cachefiles_io_error_obj(object,
  190. "Can't read xattr on %lu (err %d)",
  191. d_backing_inode(dentry)->i_ino, -ret);
  192. goto error;
  193. }
  194. /* check the on-disk object */
  195. if (ret < 1)
  196. goto bad_type_length;
  197. if (auxbuf->type != auxdata->type)
  198. goto stale;
  199. auxbuf->len = ret;
  200. /* consult the netfs */
  201. if (object->fscache.cookie->def->check_aux) {
  202. enum fscache_checkaux result;
  203. unsigned int dlen;
  204. dlen = auxbuf->len - 1;
  205. _debug("checkaux %s #%u",
  206. object->fscache.cookie->def->name, dlen);
  207. result = fscache_check_aux(&object->fscache,
  208. &auxbuf->data, dlen,
  209. i_size_read(d_backing_inode(dentry)));
  210. switch (result) {
  211. /* entry okay as is */
  212. case FSCACHE_CHECKAUX_OKAY:
  213. goto okay;
  214. /* entry requires update */
  215. case FSCACHE_CHECKAUX_NEEDS_UPDATE:
  216. break;
  217. /* entry requires deletion */
  218. case FSCACHE_CHECKAUX_OBSOLETE:
  219. goto stale;
  220. default:
  221. BUG();
  222. }
  223. /* update the current label */
  224. ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
  225. &auxdata->type, auxdata->len,
  226. XATTR_REPLACE);
  227. if (ret < 0) {
  228. cachefiles_io_error_obj(object,
  229. "Can't update xattr on %lu"
  230. " (error %d)",
  231. d_backing_inode(dentry)->i_ino, -ret);
  232. goto error;
  233. }
  234. }
  235. okay:
  236. ret = 0;
  237. error:
  238. kfree(auxbuf);
  239. _leave(" = %d", ret);
  240. return ret;
  241. bad_type_length:
  242. pr_err("Cache object %lu xattr length incorrect\n",
  243. d_backing_inode(dentry)->i_ino);
  244. ret = -EIO;
  245. goto error;
  246. stale:
  247. ret = -ESTALE;
  248. goto error;
  249. }
  250. /*
  251. * remove the object's xattr to mark it stale
  252. */
  253. int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
  254. struct dentry *dentry)
  255. {
  256. int ret;
  257. ret = vfs_removexattr(dentry, cachefiles_xattr_cache);
  258. if (ret < 0) {
  259. if (ret == -ENOENT || ret == -ENODATA)
  260. ret = 0;
  261. else if (ret != -ENOMEM)
  262. cachefiles_io_error(cache,
  263. "Can't remove xattr from %lu"
  264. " (error %d)",
  265. d_backing_inode(dentry)->i_ino, -ret);
  266. }
  267. _leave(" = %d", ret);
  268. return ret;
  269. }