xattr.c 8.2 KB

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