volume.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Volume handling.
  3. *
  4. * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/slab.h>
  9. #include <linux/namei.h>
  10. #include "internal.h"
  11. #include <trace/events/fscache.h>
  12. /*
  13. * Allocate and set up a volume representation. We make sure all the fanout
  14. * directories are created and pinned.
  15. */
  16. void cachefiles_acquire_volume(struct fscache_volume *vcookie)
  17. {
  18. struct cachefiles_volume *volume;
  19. struct cachefiles_cache *cache = vcookie->cache->cache_priv;
  20. const struct cred *saved_cred;
  21. struct dentry *vdentry, *fan;
  22. size_t len;
  23. char *name;
  24. bool is_new = false;
  25. int ret, n_accesses, i;
  26. _enter("");
  27. volume = kzalloc_obj(struct cachefiles_volume);
  28. if (!volume)
  29. return;
  30. volume->vcookie = vcookie;
  31. volume->cache = cache;
  32. INIT_LIST_HEAD(&volume->cache_link);
  33. cachefiles_begin_secure(cache, &saved_cred);
  34. len = vcookie->key[0];
  35. name = kmalloc(len + 3, GFP_NOFS);
  36. if (!name)
  37. goto error_vol;
  38. name[0] = 'I';
  39. memcpy(name + 1, vcookie->key + 1, len);
  40. name[len + 1] = 0;
  41. retry:
  42. vdentry = cachefiles_get_directory(cache, cache->store, name, &is_new);
  43. if (IS_ERR(vdentry))
  44. goto error_name;
  45. volume->dentry = vdentry;
  46. if (is_new) {
  47. if (!cachefiles_set_volume_xattr(volume))
  48. goto error_dir;
  49. } else {
  50. ret = cachefiles_check_volume_xattr(volume);
  51. if (ret < 0) {
  52. if (ret != -ESTALE)
  53. goto error_dir;
  54. vdentry = start_removing_dentry(cache->store, vdentry);
  55. if (!IS_ERR(vdentry))
  56. cachefiles_bury_object(cache, NULL, cache->store,
  57. vdentry,
  58. FSCACHE_VOLUME_IS_WEIRD);
  59. cachefiles_put_directory(volume->dentry);
  60. cond_resched();
  61. goto retry;
  62. }
  63. }
  64. for (i = 0; i < 256; i++) {
  65. sprintf(name, "@%02x", i);
  66. fan = cachefiles_get_directory(cache, vdentry, name, NULL);
  67. if (IS_ERR(fan))
  68. goto error_fan;
  69. volume->fanout[i] = fan;
  70. }
  71. cachefiles_end_secure(cache, saved_cred);
  72. vcookie->cache_priv = volume;
  73. n_accesses = atomic_inc_return(&vcookie->n_accesses); /* Stop wakeups on dec-to-0 */
  74. trace_fscache_access_volume(vcookie->debug_id, 0,
  75. refcount_read(&vcookie->ref),
  76. n_accesses, fscache_access_cache_pin);
  77. spin_lock(&cache->object_list_lock);
  78. list_add(&volume->cache_link, &volume->cache->volumes);
  79. spin_unlock(&cache->object_list_lock);
  80. kfree(name);
  81. return;
  82. error_fan:
  83. for (i = 0; i < 256; i++)
  84. cachefiles_put_directory(volume->fanout[i]);
  85. error_dir:
  86. cachefiles_put_directory(volume->dentry);
  87. error_name:
  88. kfree(name);
  89. error_vol:
  90. kfree(volume);
  91. cachefiles_end_secure(cache, saved_cred);
  92. }
  93. /*
  94. * Release a volume representation.
  95. */
  96. static void __cachefiles_free_volume(struct cachefiles_volume *volume)
  97. {
  98. int i;
  99. _enter("");
  100. volume->vcookie->cache_priv = NULL;
  101. for (i = 0; i < 256; i++)
  102. cachefiles_put_directory(volume->fanout[i]);
  103. cachefiles_put_directory(volume->dentry);
  104. kfree(volume);
  105. }
  106. void cachefiles_free_volume(struct fscache_volume *vcookie)
  107. {
  108. struct cachefiles_volume *volume = vcookie->cache_priv;
  109. if (volume) {
  110. spin_lock(&volume->cache->object_list_lock);
  111. list_del_init(&volume->cache_link);
  112. spin_unlock(&volume->cache->object_list_lock);
  113. __cachefiles_free_volume(volume);
  114. }
  115. }
  116. void cachefiles_withdraw_volume(struct cachefiles_volume *volume)
  117. {
  118. cachefiles_set_volume_xattr(volume);
  119. __cachefiles_free_volume(volume);
  120. }