volume.c 3.3 KB

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