xattr.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 (dhowells@redhat.com)
  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 = mnt_want_write_file(file);
  58. if (ret == 0) {
  59. ret = vfs_setxattr(&nop_mnt_idmap, dentry,
  60. cachefiles_xattr_cache, buf,
  61. sizeof(struct cachefiles_xattr) + len, 0);
  62. mnt_drop_write_file(file);
  63. }
  64. }
  65. if (ret < 0) {
  66. trace_cachefiles_vfs_error(object, file_inode(file), ret,
  67. cachefiles_trace_setxattr_error);
  68. trace_cachefiles_coherency(object, file_inode(file)->i_ino,
  69. buf->content,
  70. cachefiles_coherency_set_fail);
  71. if (ret != -ENOMEM)
  72. cachefiles_io_error_obj(
  73. object,
  74. "Failed to set xattr with error %d", ret);
  75. } else {
  76. trace_cachefiles_coherency(object, file_inode(file)->i_ino,
  77. buf->content,
  78. cachefiles_coherency_set_ok);
  79. }
  80. kfree(buf);
  81. _leave(" = %d", ret);
  82. return ret;
  83. }
  84. /*
  85. * check the consistency between the backing cache and the FS-Cache cookie
  86. */
  87. int cachefiles_check_auxdata(struct cachefiles_object *object, struct file *file)
  88. {
  89. struct cachefiles_xattr *buf;
  90. struct dentry *dentry = file->f_path.dentry;
  91. unsigned int len = object->cookie->aux_len, tlen;
  92. const void *p = fscache_get_aux(object->cookie);
  93. enum cachefiles_coherency_trace why;
  94. ssize_t xlen;
  95. int ret = -ESTALE;
  96. tlen = sizeof(struct cachefiles_xattr) + len;
  97. buf = kmalloc(tlen, GFP_KERNEL);
  98. if (!buf)
  99. return -ENOMEM;
  100. xlen = cachefiles_inject_read_error();
  101. if (xlen == 0)
  102. xlen = vfs_getxattr(&nop_mnt_idmap, dentry, cachefiles_xattr_cache, buf, tlen);
  103. if (xlen != tlen) {
  104. if (xlen < 0) {
  105. ret = xlen;
  106. trace_cachefiles_vfs_error(object, file_inode(file), xlen,
  107. cachefiles_trace_getxattr_error);
  108. }
  109. if (xlen == -EIO)
  110. cachefiles_io_error_obj(
  111. object,
  112. "Failed to read aux with error %zd", xlen);
  113. why = cachefiles_coherency_check_xattr;
  114. } else if (buf->type != CACHEFILES_COOKIE_TYPE_DATA) {
  115. why = cachefiles_coherency_check_type;
  116. } else if (memcmp(buf->data, p, len) != 0) {
  117. why = cachefiles_coherency_check_aux;
  118. } else if (be64_to_cpu(buf->object_size) != object->cookie->object_size) {
  119. why = cachefiles_coherency_check_objsize;
  120. } else if (buf->content == CACHEFILES_CONTENT_DIRTY) {
  121. // TODO: Begin conflict resolution
  122. pr_warn("Dirty object in cache\n");
  123. why = cachefiles_coherency_check_dirty;
  124. } else {
  125. why = cachefiles_coherency_check_ok;
  126. ret = 0;
  127. }
  128. trace_cachefiles_coherency(object, file_inode(file)->i_ino,
  129. buf->content, why);
  130. kfree(buf);
  131. return ret;
  132. }
  133. /*
  134. * remove the object's xattr to mark it stale
  135. */
  136. int cachefiles_remove_object_xattr(struct cachefiles_cache *cache,
  137. struct cachefiles_object *object,
  138. struct dentry *dentry)
  139. {
  140. int ret;
  141. ret = cachefiles_inject_remove_error();
  142. if (ret == 0) {
  143. ret = mnt_want_write(cache->mnt);
  144. if (ret == 0) {
  145. ret = vfs_removexattr(&nop_mnt_idmap, dentry,
  146. cachefiles_xattr_cache);
  147. mnt_drop_write(cache->mnt);
  148. }
  149. }
  150. if (ret < 0) {
  151. trace_cachefiles_vfs_error(object, d_inode(dentry), ret,
  152. cachefiles_trace_remxattr_error);
  153. if (ret == -ENOENT || ret == -ENODATA)
  154. ret = 0;
  155. else if (ret != -ENOMEM)
  156. cachefiles_io_error(cache,
  157. "Can't remove xattr from %lu"
  158. " (error %d)",
  159. d_backing_inode(dentry)->i_ino, -ret);
  160. }
  161. _leave(" = %d", ret);
  162. return ret;
  163. }
  164. /*
  165. * Stick a marker on the cache object to indicate that it's dirty.
  166. */
  167. void cachefiles_prepare_to_write(struct fscache_cookie *cookie)
  168. {
  169. const struct cred *saved_cred;
  170. struct cachefiles_object *object = cookie->cache_priv;
  171. struct cachefiles_cache *cache = object->volume->cache;
  172. _enter("c=%08x", object->cookie->debug_id);
  173. if (!test_bit(CACHEFILES_OBJECT_USING_TMPFILE, &object->flags)) {
  174. cachefiles_begin_secure(cache, &saved_cred);
  175. cachefiles_set_object_xattr(object);
  176. cachefiles_end_secure(cache, saved_cred);
  177. }
  178. }
  179. /*
  180. * Set the state xattr on a volume directory.
  181. */
  182. bool cachefiles_set_volume_xattr(struct cachefiles_volume *volume)
  183. {
  184. struct cachefiles_vol_xattr *buf;
  185. unsigned int len = volume->vcookie->coherency_len;
  186. const void *p = volume->vcookie->coherency;
  187. struct dentry *dentry = volume->dentry;
  188. int ret;
  189. _enter("%x,#%d", volume->vcookie->debug_id, len);
  190. len += sizeof(*buf);
  191. buf = kmalloc(len, GFP_KERNEL);
  192. if (!buf)
  193. return false;
  194. buf->reserved = cpu_to_be32(0);
  195. memcpy(buf->data, p, volume->vcookie->coherency_len);
  196. ret = cachefiles_inject_write_error();
  197. if (ret == 0) {
  198. ret = mnt_want_write(volume->cache->mnt);
  199. if (ret == 0) {
  200. ret = vfs_setxattr(&nop_mnt_idmap, dentry,
  201. cachefiles_xattr_cache,
  202. buf, len, 0);
  203. mnt_drop_write(volume->cache->mnt);
  204. }
  205. }
  206. if (ret < 0) {
  207. trace_cachefiles_vfs_error(NULL, d_inode(dentry), ret,
  208. cachefiles_trace_setxattr_error);
  209. trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino,
  210. cachefiles_coherency_vol_set_fail);
  211. if (ret != -ENOMEM)
  212. cachefiles_io_error(
  213. volume->cache, "Failed to set xattr with error %d", ret);
  214. } else {
  215. trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino,
  216. cachefiles_coherency_vol_set_ok);
  217. }
  218. kfree(buf);
  219. _leave(" = %d", ret);
  220. return ret == 0;
  221. }
  222. /*
  223. * Check the consistency between the backing cache and the volume cookie.
  224. */
  225. int cachefiles_check_volume_xattr(struct cachefiles_volume *volume)
  226. {
  227. struct cachefiles_vol_xattr *buf;
  228. struct dentry *dentry = volume->dentry;
  229. unsigned int len = volume->vcookie->coherency_len;
  230. const void *p = volume->vcookie->coherency;
  231. enum cachefiles_coherency_trace why;
  232. ssize_t xlen;
  233. int ret = -ESTALE;
  234. _enter("");
  235. len += sizeof(*buf);
  236. buf = kmalloc(len, GFP_KERNEL);
  237. if (!buf)
  238. return -ENOMEM;
  239. xlen = cachefiles_inject_read_error();
  240. if (xlen == 0)
  241. xlen = vfs_getxattr(&nop_mnt_idmap, dentry, cachefiles_xattr_cache, buf, len);
  242. if (xlen != len) {
  243. if (xlen < 0) {
  244. ret = xlen;
  245. trace_cachefiles_vfs_error(NULL, d_inode(dentry), xlen,
  246. cachefiles_trace_getxattr_error);
  247. if (xlen == -EIO)
  248. cachefiles_io_error(
  249. volume->cache,
  250. "Failed to read xattr with error %zd", xlen);
  251. }
  252. why = cachefiles_coherency_vol_check_xattr;
  253. } else if (buf->reserved != cpu_to_be32(0)) {
  254. why = cachefiles_coherency_vol_check_resv;
  255. } else if (memcmp(buf->data, p, len - sizeof(*buf)) != 0) {
  256. why = cachefiles_coherency_vol_check_cmp;
  257. } else {
  258. why = cachefiles_coherency_vol_check_ok;
  259. ret = 0;
  260. }
  261. trace_cachefiles_vol_coherency(volume, d_inode(dentry)->i_ino, why);
  262. kfree(buf);
  263. _leave(" = %d", ret);
  264. return ret;
  265. }