xattr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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(dentry);
  95. _enter("%p,#%d", object, auxdata->len);
  96. /* attempt to install the cache metadata directly */
  97. _debug("SET #%u", 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(dentry);
  117. _enter("%p,#%d", object, auxdata->len);
  118. /* attempt to install the cache metadata directly */
  119. _debug("SET #%u", auxdata->len);
  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(dentry->d_inode);
  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. if (validity != FSCACHE_CHECKAUX_OKAY)
  155. goto error;
  156. ret = 0;
  157. error:
  158. kfree(auxbuf);
  159. return ret;
  160. }
  161. /*
  162. * check the state xattr on a cache file
  163. * - return -ESTALE if the object should be deleted
  164. */
  165. int cachefiles_check_object_xattr(struct cachefiles_object *object,
  166. struct cachefiles_xattr *auxdata)
  167. {
  168. struct cachefiles_xattr *auxbuf;
  169. struct dentry *dentry = object->dentry;
  170. int ret;
  171. _enter("%p,#%d", object, auxdata->len);
  172. ASSERT(dentry);
  173. ASSERT(dentry->d_inode);
  174. auxbuf = kmalloc(sizeof(struct cachefiles_xattr) + 512, cachefiles_gfp);
  175. if (!auxbuf) {
  176. _leave(" = -ENOMEM");
  177. return -ENOMEM;
  178. }
  179. /* read the current type label */
  180. ret = vfs_getxattr(dentry, cachefiles_xattr_cache,
  181. &auxbuf->type, 512 + 1);
  182. if (ret < 0) {
  183. if (ret == -ENODATA)
  184. goto stale; /* no attribute - power went off
  185. * mid-cull? */
  186. if (ret == -ERANGE)
  187. goto bad_type_length;
  188. cachefiles_io_error_obj(object,
  189. "Can't read xattr on %lu (err %d)",
  190. dentry->d_inode->i_ino, -ret);
  191. goto error;
  192. }
  193. /* check the on-disk object */
  194. if (ret < 1)
  195. goto bad_type_length;
  196. if (auxbuf->type != auxdata->type)
  197. goto stale;
  198. auxbuf->len = ret;
  199. /* consult the netfs */
  200. if (object->fscache.cookie->def->check_aux) {
  201. enum fscache_checkaux result;
  202. unsigned int dlen;
  203. dlen = auxbuf->len - 1;
  204. _debug("checkaux %s #%u",
  205. object->fscache.cookie->def->name, dlen);
  206. result = fscache_check_aux(&object->fscache,
  207. &auxbuf->data, dlen);
  208. switch (result) {
  209. /* entry okay as is */
  210. case FSCACHE_CHECKAUX_OKAY:
  211. goto okay;
  212. /* entry requires update */
  213. case FSCACHE_CHECKAUX_NEEDS_UPDATE:
  214. break;
  215. /* entry requires deletion */
  216. case FSCACHE_CHECKAUX_OBSOLETE:
  217. goto stale;
  218. default:
  219. BUG();
  220. }
  221. /* update the current label */
  222. ret = vfs_setxattr(dentry, cachefiles_xattr_cache,
  223. &auxdata->type, auxdata->len,
  224. XATTR_REPLACE);
  225. if (ret < 0) {
  226. cachefiles_io_error_obj(object,
  227. "Can't update xattr on %lu"
  228. " (error %d)",
  229. dentry->d_inode->i_ino, -ret);
  230. goto error;
  231. }
  232. }
  233. okay:
  234. ret = 0;
  235. error:
  236. kfree(auxbuf);
  237. _leave(" = %d", ret);
  238. return ret;
  239. bad_type_length:
  240. kerror("Cache object %lu xattr length incorrect",
  241. dentry->d_inode->i_ino);
  242. ret = -EIO;
  243. goto error;
  244. stale:
  245. ret = -ESTALE;
  246. goto error;
  247. }
  248. /*
  249. * remove the object's xattr to mark it stale
  250. */
  251. int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
  252. struct dentry *dentry)
  253. {
  254. int ret;
  255. ret = vfs_removexattr(dentry, cachefiles_xattr_cache);
  256. if (ret < 0) {
  257. if (ret == -ENOENT || ret == -ENODATA)
  258. ret = 0;
  259. else if (ret != -ENOMEM)
  260. cachefiles_io_error(cache,
  261. "Can't remove xattr from %lu"
  262. " (error %d)",
  263. dentry->d_inode->i_ino, -ret);
  264. }
  265. _leave(" = %d", ret);
  266. return ret;
  267. }