memory.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. *******************************************************************************
  4. **
  5. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  6. ** Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
  7. **
  8. **
  9. *******************************************************************************
  10. ******************************************************************************/
  11. #include "dlm_internal.h"
  12. #include "midcomms.h"
  13. #include "lowcomms.h"
  14. #include "config.h"
  15. #include "memory.h"
  16. static struct kmem_cache *writequeue_cache;
  17. static struct kmem_cache *mhandle_cache;
  18. static struct kmem_cache *msg_cache;
  19. static struct kmem_cache *lkb_cache;
  20. static struct kmem_cache *rsb_cache;
  21. int __init dlm_memory_init(void)
  22. {
  23. writequeue_cache = dlm_lowcomms_writequeue_cache_create();
  24. if (!writequeue_cache)
  25. goto out;
  26. mhandle_cache = dlm_midcomms_cache_create();
  27. if (!mhandle_cache)
  28. goto mhandle;
  29. lkb_cache = kmem_cache_create("dlm_lkb", sizeof(struct dlm_lkb),
  30. __alignof__(struct dlm_lkb), 0, NULL);
  31. if (!lkb_cache)
  32. goto lkb;
  33. msg_cache = dlm_lowcomms_msg_cache_create();
  34. if (!msg_cache)
  35. goto msg;
  36. rsb_cache = kmem_cache_create("dlm_rsb", sizeof(struct dlm_rsb),
  37. __alignof__(struct dlm_rsb), 0, NULL);
  38. if (!rsb_cache)
  39. goto rsb;
  40. return 0;
  41. rsb:
  42. kmem_cache_destroy(msg_cache);
  43. msg:
  44. kmem_cache_destroy(lkb_cache);
  45. lkb:
  46. kmem_cache_destroy(mhandle_cache);
  47. mhandle:
  48. kmem_cache_destroy(writequeue_cache);
  49. out:
  50. return -ENOMEM;
  51. }
  52. void dlm_memory_exit(void)
  53. {
  54. kmem_cache_destroy(writequeue_cache);
  55. kmem_cache_destroy(mhandle_cache);
  56. kmem_cache_destroy(msg_cache);
  57. kmem_cache_destroy(lkb_cache);
  58. kmem_cache_destroy(rsb_cache);
  59. }
  60. char *dlm_allocate_lvb(struct dlm_ls *ls)
  61. {
  62. char *p;
  63. p = kzalloc(ls->ls_lvblen, GFP_NOFS);
  64. return p;
  65. }
  66. void dlm_free_lvb(char *p)
  67. {
  68. kfree(p);
  69. }
  70. struct dlm_rsb *dlm_allocate_rsb(struct dlm_ls *ls)
  71. {
  72. struct dlm_rsb *r;
  73. r = kmem_cache_zalloc(rsb_cache, GFP_NOFS);
  74. return r;
  75. }
  76. void dlm_free_rsb(struct dlm_rsb *r)
  77. {
  78. if (r->res_lvbptr)
  79. dlm_free_lvb(r->res_lvbptr);
  80. kmem_cache_free(rsb_cache, r);
  81. }
  82. struct dlm_lkb *dlm_allocate_lkb(struct dlm_ls *ls)
  83. {
  84. struct dlm_lkb *lkb;
  85. lkb = kmem_cache_zalloc(lkb_cache, GFP_NOFS);
  86. return lkb;
  87. }
  88. void dlm_free_lkb(struct dlm_lkb *lkb)
  89. {
  90. if (lkb->lkb_flags & DLM_IFL_USER) {
  91. struct dlm_user_args *ua;
  92. ua = lkb->lkb_ua;
  93. if (ua) {
  94. kfree(ua->lksb.sb_lvbptr);
  95. kfree(ua);
  96. }
  97. }
  98. kmem_cache_free(lkb_cache, lkb);
  99. }
  100. struct dlm_mhandle *dlm_allocate_mhandle(void)
  101. {
  102. return kmem_cache_alloc(mhandle_cache, GFP_NOFS);
  103. }
  104. void dlm_free_mhandle(struct dlm_mhandle *mhandle)
  105. {
  106. kmem_cache_free(mhandle_cache, mhandle);
  107. }
  108. struct writequeue_entry *dlm_allocate_writequeue(void)
  109. {
  110. return kmem_cache_alloc(writequeue_cache, GFP_ATOMIC);
  111. }
  112. void dlm_free_writequeue(struct writequeue_entry *writequeue)
  113. {
  114. kmem_cache_free(writequeue_cache, writequeue);
  115. }
  116. struct dlm_msg *dlm_allocate_msg(gfp_t allocation)
  117. {
  118. return kmem_cache_alloc(msg_cache, allocation);
  119. }
  120. void dlm_free_msg(struct dlm_msg *msg)
  121. {
  122. kmem_cache_free(msg_cache, msg);
  123. }