orangefs-cache.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) 2001 Clemson University and The University of Chicago
  4. *
  5. * See COPYING in top-level directory.
  6. */
  7. #include "protocol.h"
  8. #include "orangefs-kernel.h"
  9. /* tags assigned to kernel upcall operations */
  10. static __u64 next_tag_value;
  11. static DEFINE_SPINLOCK(next_tag_value_lock);
  12. /* the orangefs memory caches */
  13. /* a cache for orangefs upcall/downcall operations */
  14. static struct kmem_cache *op_cache;
  15. int op_cache_initialize(void)
  16. {
  17. op_cache = kmem_cache_create("orangefs_op_cache",
  18. sizeof(struct orangefs_kernel_op_s),
  19. 0,
  20. ORANGEFS_CACHE_CREATE_FLAGS,
  21. NULL);
  22. if (!op_cache) {
  23. gossip_err("Cannot create orangefs_op_cache\n");
  24. return -ENOMEM;
  25. }
  26. /* initialize our atomic tag counter */
  27. spin_lock(&next_tag_value_lock);
  28. next_tag_value = 100;
  29. spin_unlock(&next_tag_value_lock);
  30. return 0;
  31. }
  32. int op_cache_finalize(void)
  33. {
  34. kmem_cache_destroy(op_cache);
  35. return 0;
  36. }
  37. char *get_opname_string(struct orangefs_kernel_op_s *new_op)
  38. {
  39. if (new_op) {
  40. __s32 type = new_op->upcall.type;
  41. if (type == ORANGEFS_VFS_OP_FILE_IO)
  42. return "OP_FILE_IO";
  43. else if (type == ORANGEFS_VFS_OP_LOOKUP)
  44. return "OP_LOOKUP";
  45. else if (type == ORANGEFS_VFS_OP_CREATE)
  46. return "OP_CREATE";
  47. else if (type == ORANGEFS_VFS_OP_GETATTR)
  48. return "OP_GETATTR";
  49. else if (type == ORANGEFS_VFS_OP_REMOVE)
  50. return "OP_REMOVE";
  51. else if (type == ORANGEFS_VFS_OP_MKDIR)
  52. return "OP_MKDIR";
  53. else if (type == ORANGEFS_VFS_OP_READDIR)
  54. return "OP_READDIR";
  55. else if (type == ORANGEFS_VFS_OP_READDIRPLUS)
  56. return "OP_READDIRPLUS";
  57. else if (type == ORANGEFS_VFS_OP_SETATTR)
  58. return "OP_SETATTR";
  59. else if (type == ORANGEFS_VFS_OP_SYMLINK)
  60. return "OP_SYMLINK";
  61. else if (type == ORANGEFS_VFS_OP_RENAME)
  62. return "OP_RENAME";
  63. else if (type == ORANGEFS_VFS_OP_STATFS)
  64. return "OP_STATFS";
  65. else if (type == ORANGEFS_VFS_OP_TRUNCATE)
  66. return "OP_TRUNCATE";
  67. else if (type == ORANGEFS_VFS_OP_RA_FLUSH)
  68. return "OP_RA_FLUSH";
  69. else if (type == ORANGEFS_VFS_OP_FS_MOUNT)
  70. return "OP_FS_MOUNT";
  71. else if (type == ORANGEFS_VFS_OP_FS_UMOUNT)
  72. return "OP_FS_UMOUNT";
  73. else if (type == ORANGEFS_VFS_OP_GETXATTR)
  74. return "OP_GETXATTR";
  75. else if (type == ORANGEFS_VFS_OP_SETXATTR)
  76. return "OP_SETXATTR";
  77. else if (type == ORANGEFS_VFS_OP_LISTXATTR)
  78. return "OP_LISTXATTR";
  79. else if (type == ORANGEFS_VFS_OP_REMOVEXATTR)
  80. return "OP_REMOVEXATTR";
  81. else if (type == ORANGEFS_VFS_OP_PARAM)
  82. return "OP_PARAM";
  83. else if (type == ORANGEFS_VFS_OP_PERF_COUNT)
  84. return "OP_PERF_COUNT";
  85. else if (type == ORANGEFS_VFS_OP_CANCEL)
  86. return "OP_CANCEL";
  87. else if (type == ORANGEFS_VFS_OP_FSYNC)
  88. return "OP_FSYNC";
  89. else if (type == ORANGEFS_VFS_OP_FSKEY)
  90. return "OP_FSKEY";
  91. else if (type == ORANGEFS_VFS_OP_FEATURES)
  92. return "OP_FEATURES";
  93. }
  94. return "OP_UNKNOWN?";
  95. }
  96. void orangefs_new_tag(struct orangefs_kernel_op_s *op)
  97. {
  98. spin_lock(&next_tag_value_lock);
  99. op->tag = next_tag_value++;
  100. if (next_tag_value == 0)
  101. next_tag_value = 100;
  102. spin_unlock(&next_tag_value_lock);
  103. }
  104. struct orangefs_kernel_op_s *op_alloc(__s32 type)
  105. {
  106. struct orangefs_kernel_op_s *new_op = NULL;
  107. new_op = kmem_cache_zalloc(op_cache, GFP_KERNEL);
  108. if (new_op) {
  109. INIT_LIST_HEAD(&new_op->list);
  110. spin_lock_init(&new_op->lock);
  111. init_completion(&new_op->waitq);
  112. new_op->upcall.type = ORANGEFS_VFS_OP_INVALID;
  113. new_op->downcall.type = ORANGEFS_VFS_OP_INVALID;
  114. new_op->downcall.status = -1;
  115. new_op->op_state = OP_VFS_STATE_UNKNOWN;
  116. /* initialize the op specific tag and upcall credentials */
  117. orangefs_new_tag(new_op);
  118. new_op->upcall.type = type;
  119. new_op->attempts = 0;
  120. gossip_debug(GOSSIP_CACHE_DEBUG,
  121. "Alloced OP (%p: %llu %s)\n",
  122. new_op,
  123. llu(new_op->tag),
  124. get_opname_string(new_op));
  125. new_op->upcall.uid = from_kuid(&init_user_ns,
  126. current_fsuid());
  127. new_op->upcall.gid = from_kgid(&init_user_ns,
  128. current_fsgid());
  129. } else {
  130. gossip_err("op_alloc: kmem_cache_zalloc failed!\n");
  131. }
  132. return new_op;
  133. }
  134. void op_release(struct orangefs_kernel_op_s *orangefs_op)
  135. {
  136. if (orangefs_op) {
  137. gossip_debug(GOSSIP_CACHE_DEBUG,
  138. "Releasing OP (%p: %llu)\n",
  139. orangefs_op,
  140. llu(orangefs_op->tag));
  141. kmem_cache_free(op_cache, orangefs_op);
  142. } else {
  143. gossip_err("NULL pointer in op_release\n");
  144. }
  145. }