orangefs-cache.c 4.2 KB

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