xattr.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* CacheFiles extended attribute management
  3. *
  4. * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. */
  7. #include <linux/module.h>
  8. #include <linux/sched.h>
  9. #include <linux/file.h>
  10. #include <linux/fs.h>
  11. #include <linux/fsnotify.h>
  12. #include <linux/quotaops.h>
  13. #include <linux/xattr.h>
  14. #include <linux/slab.h>
  15. #include "internal.h"
  16. #define CACHEFILES_COOKIE_TYPE_DATA 1
  17. struct cachefiles_xattr {
  18. __be64 object_size; /* Actual size of the object */
  19. __be64 zero_point; /* Size after which server has no data not written by us */
  20. __u8 type; /* Type of object */
  21. __u8 content; /* Content presence (enum cachefiles_content) */
  22. __u8 data[]; /* netfs coherency data */
  23. } __packed;
  24. static const char cachefiles_xattr_cache[] =
  25. XATTR_USER_PREFIX "CacheFiles.cache";
  26. struct cachefiles_vol_xattr {
  27. __be32 reserved; /* Reserved, should be 0 */
  28. __u8 data[]; /* netfs volume coherency data */
  29. } __packed;
  30. /*
  31. * set the state xattr on a cache file
  32. */
  33. int cachefiles_set_object_xattr(struct cachefiles_object *object)
  34. {
  35. struct cachefiles_xattr *buf;
  36. struct dentry *dentry;
  37. struct file *file = object->file;
  38. unsigned int len = object->cookie->aux_len;
  39. int ret;
  40. if (!file)
  41. return -ESTALE;
  42. dentry = file->f_path.dentry;
  43. _enter("%x,#%d", object->debug_id, len);
  44. buf = kmalloc(sizeof(struct cachefiles_xattr) + len, GFP_KERNEL);
  45. if (!buf)
  46. return -ENOMEM;
  47. buf->object_size = cpu_to_be64(object->cookie->object_size);
  48. buf->zero_point = 0;
  49. buf->type = CACHEFILES_COOKIE_TYPE_DATA;
  50. buf->content = object->content_info;
  51. if (test_bit(FSCACHE_COOKIE_LOCAL_WRITE, &object->cookie->flags))
  52. buf->content = CACHEFILES_CONTENT_DIRTY;
  53. if (len > 0)
  54. memcpy(buf->data, fscache_get_aux(object->cookie), len);
  55. ret = cachefiles_inject_write_error();
  56. if (ret == 0)
  57. ret = vfs_setxattr(&init_user_ns, dentry, cachefiles_xattr_cache,
  58. buf, sizeof(struct cachefiles_xattr) + len, 0);
  59. if (ret < 0) {
  60. trace_cachefiles_vfs_error(object, file_inode(file), ret,
  61. cachefiles_trace_setxattr_error);
  62. trace_cachefiles_coherency(object, file_inode(file)->i_ino,
  63. buf->content,
  64. cachefiles_coherency_set_fail);
  65. if (ret != -ENOMEM)
  66. cachefiles_io_error_obj(
  67. object,
  68. "Failed to set xattr with error %d", ret);
  69. } else {
  70. trace_cachefiles_coherency(object, file_inode(file)->i_ino,
  71. buf->content,
  72. cachefiles_coherency_set_ok);
  73. }
  74. kfree(buf);
  75. _leave(" = %d", ret);
  76. return ret;
  77. }
  78. /*
  79. * check the consistency between the backing cache and the FS-Cache cookie
  80. */
  81. int cachefiles_check_auxdata(struct cachefiles_object *object, struct file *file)
  82. {
  83. struct cachefiles_xattr *buf;
  84. struct dentry *dentry = file->f_path.dentry;
  85. unsigned int len = object->cookie->aux_len, tlen;
  86. const void *p = fscache_get_aux(object->cookie);
  87. enum cachefiles_coherency_trace why;
  88. ssize_t xlen;
  89. int ret = -ESTALE;
  90. tlen = sizeof(struct cachefiles_xattr) + len;
  91. buf = kmalloc(tlen, GFP_KERNEL);
  92. if (!buf)
  93. return -ENOMEM;
  94. xlen = cachefiles_inject_read_error();
  95. if (xlen == 0)
  96. xlen = vfs_getxattr(&init_user_ns, dentry, cachefiles_xattr_cache, buf, tlen);
  97. if (xlen != tlen) {
  98. if (xlen < 0)
  99. trace_cachefiles_vfs_error(object, file_inode(file), xlen,
  100. cachefiles_trace_getxattr_error);
  101. if (xlen == -EIO)
  102. cachefiles_io_error_obj(
  103. object,
  104. "Failed to read aux with error %zd", xlen);
  105. why = cachefiles_coherency_check_xattr;
  106. } else if (buf->type != CACHEFILES_COOKIE_TYPE_DATA) {
  107. why = cachefiles_coherency_check_type;
  108. } else if (memcmp(buf->data, p, len) != 0) {
  109. why = cachefiles_coherency_check_aux;
  110. } else if (be64_to_cpu(buf->object_size) != object->cookie->object_size) {
  111. why = cachefiles_coherency_check_objsize;
  112. } else if (buf->content == CACHEFILES_CONTENT_DIRTY) {
  113. // TODO: Begin conflict resolution
  114. pr_warn("Dirty object in cache\n");
  115. why = cachefiles_coherency_check_dirty;
  116. } else {
  117. why = cachefiles_coherency_check_ok;
  118. ret = 0;
  119. }
  120. trace_cachefiles_coherency(object, file_inode(file)->i_ino,
  121. buf->content, why);
  122. kfree(buf);
  123. return ret;
  124. }
  125. /*
  126. * remove the object's xattr to mark it stale
  127. */
  128. int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
  129. struct cachefiles_object *object,
  130. struct dentry *dentry)
  131. {
  132. int ret;
  133. ret = cachefiles_inject_remove_error();
  134. if (ret == 0)
  135. ret = vfs_removexattr(&init_user_ns, dentry, cachefiles_xattr_cache);
  136. if (ret < 0) {
  137. trace_cachefiles_vfs_error(object, d_inode(dentry), ret,
  138. cachefiles_trace_remxattr_error);
  139. if (ret == -ENOENT || ret == -ENODATA)
  140. ret = 0;
  141. else if (ret != -ENOMEM)
  142. cachefiles_io_error(cache,
  143. "Can't remove xattr from %lu"
  144. " (error %d)",
  145. d_backing_inode(dentry)->i_ino, -ret);
  146. }
  147. _leave(" = %d", ret);
  148. return ret;
  149. }
  150. /*
  151. * Stick a marker on the cache object to indicate that it's dirty.
  152. */
  153. void cachefiles_prepare_to_write(struct fscache_cookie *cookie)
  154. {
  155. const struct cred *saved_cred;
  156. struct cachefiles_object *object = cookie->cache_priv;
  157. struct cachefiles_cache *cache = object->volume->cache;
  158. _enter("c=%08x", object->cookie->debug_id);
  159. if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
  160. cachefiles_begin_secure(cache, &saved_cred);
  161. cachefiles_set_object_xattr(object);
  162. cachefiles_end_secure(cache, saved_cred);
  163. }
  164. }
  165. /*
  166. * Set the state xattr on a volume directory.
  167. */
  168. bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume)
  169. {
  170. struct cachefiles_vol_xattr *buf;
  171. unsigned int len = volume->vcookie->coherency_len;
  172. const void *p = volume->vcookie->coherency;
  173. struct dentry *dentry = volume->dentry;
  174. int ret;
  175. _enter("%x,#%d", volume->vcookie->debug_id, len);
  176. len += sizeof(*buf);
  177. buf = kmalloc(len, GFP_KERNEL);
  178. if (!buf)
  179. return false;
  180. buf->reserved = cpu_to_be32(0);
  181. memcpy(buf->data, p, volume->vcookie->coherency_len);
  182. ret = cachefiles_inject_write_error();
  183. if (ret == 0)
  184. ret = vfs_setxattr(&init_user_ns, dentry, cachefiles_xattr_cache,
  185. buf, len, 0);
  186. if (ret < 0) {
  187. trace_cachefiles_vfs_error(NULL, d_inode(dentry), ret,
  188. cachefiles_trace_setxattr_error);
  189. trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino,
  190. cachefiles_coherency_vol_set_fail);
  191. if (ret != -ENOMEM)
  192. cachefiles_io_error(
  193. volume->cache, "Failed to set xattr with error %d", ret);
  194. } else {
  195. trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino,
  196. cachefiles_coherency_vol_set_ok);
  197. }
  198. kfree(buf);
  199. _leave(" = %d", ret);
  200. return ret == 0;
  201. }
  202. /*
  203. * Check the consistency between the backing cache and the volume cookie.
  204. */
  205. int cachefiles_check_volume_xattr(struct cachefiles_volume *volume)
  206. {
  207. struct cachefiles_vol_xattr *buf;
  208. struct dentry *dentry = volume->dentry;
  209. unsigned int len = volume->vcookie->coherency_len;
  210. const void *p = volume->vcookie->coherency;
  211. enum cachefiles_coherency_trace why;
  212. ssize_t xlen;
  213. int ret = -ESTALE;
  214. _enter("");
  215. len += sizeof(*buf);
  216. buf = kmalloc(len, GFP_KERNEL);
  217. if (!buf)
  218. return -ENOMEM;
  219. xlen = cachefiles_inject_read_error();
  220. if (xlen == 0)
  221. xlen = vfs_getxattr(&init_user_ns, dentry, cachefiles_xattr_cache, buf, len);
  222. if (xlen != len) {
  223. if (xlen < 0) {
  224. trace_cachefiles_vfs_error(NULL, d_inode(dentry), xlen,
  225. cachefiles_trace_getxattr_error);
  226. if (xlen == -EIO)
  227. cachefiles_io_error(
  228. volume->cache,
  229. "Failed to read xattr with error %zd", xlen);
  230. }
  231. why = cachefiles_coherency_vol_check_xattr;
  232. } else if (buf->reserved != cpu_to_be32(0)) {
  233. why = cachefiles_coherency_vol_check_resv;
  234. } else if (memcmp(buf->data, p, len - sizeof(*buf)) != 0) {
  235. why = cachefiles_coherency_vol_check_cmp;
  236. } else {
  237. why = cachefiles_coherency_vol_check_ok;
  238. ret = 0;
  239. }
  240. trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino, why);
  241. kfree(buf);
  242. _leave(" = %d", ret);
  243. return ret;
  244. }