12522fe45SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e7fd4179SDavid Teigland /****************************************************************************** 3e7fd4179SDavid Teigland ******************************************************************************* 4e7fd4179SDavid Teigland ** 5e7fd4179SDavid Teigland ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 652bda2b5SDavid Teigland ** Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. 7e7fd4179SDavid Teigland ** 8e7fd4179SDavid Teigland ** 9e7fd4179SDavid Teigland ******************************************************************************* 10e7fd4179SDavid Teigland ******************************************************************************/ 11e7fd4179SDavid Teigland 12e7fd4179SDavid Teigland #include "dlm_internal.h" 136c547f26SAlexander Aring #include "midcomms.h" 143af2326cSAlexander Aring #include "lowcomms.h" 15e7fd4179SDavid Teigland #include "config.h" 16e7fd4179SDavid Teigland #include "memory.h" 1761bed0baSAlexander Aring #include "ast.h" 18e7fd4179SDavid Teigland 193af2326cSAlexander Aring static struct kmem_cache *writequeue_cache; 206c547f26SAlexander Aring static struct kmem_cache *mhandle_cache; 21e4dc81edSAlexander Aring static struct kmem_cache *msg_cache; 22e18b890bSChristoph Lameter static struct kmem_cache *lkb_cache; 233881ac04SDavid Teigland static struct kmem_cache *rsb_cache; 2461bed0baSAlexander Aring static struct kmem_cache *cb_cache; 25e7fd4179SDavid Teigland 26e7fd4179SDavid Teigland 2730727174SDenis Cheng int __init dlm_memory_init(void) 28e7fd4179SDavid Teigland { 293af2326cSAlexander Aring writequeue_cache = dlm_lowcomms_writequeue_cache_create(); 303af2326cSAlexander Aring if (!writequeue_cache) 313af2326cSAlexander Aring goto out; 323af2326cSAlexander Aring 336c547f26SAlexander Aring mhandle_cache = dlm_midcomms_cache_create(); 346c547f26SAlexander Aring if (!mhandle_cache) 353af2326cSAlexander Aring goto mhandle; 366c547f26SAlexander Aring 37e7fd4179SDavid Teigland lkb_cache = kmem_cache_create("dlm_lkb", sizeof(struct dlm_lkb), 3820c2df83SPaul Mundt __alignof__(struct dlm_lkb), 0, NULL); 39e7fd4179SDavid Teigland if (!lkb_cache) 406c547f26SAlexander Aring goto lkb; 413881ac04SDavid Teigland 42e4dc81edSAlexander Aring msg_cache = dlm_lowcomms_msg_cache_create(); 43e4dc81edSAlexander Aring if (!msg_cache) 44e4dc81edSAlexander Aring goto msg; 45e4dc81edSAlexander Aring 463881ac04SDavid Teigland rsb_cache = kmem_cache_create("dlm_rsb", sizeof(struct dlm_rsb), 473881ac04SDavid Teigland __alignof__(struct dlm_rsb), 0, NULL); 486c547f26SAlexander Aring if (!rsb_cache) 496c547f26SAlexander Aring goto rsb; 503881ac04SDavid Teigland 5161bed0baSAlexander Aring cb_cache = kmem_cache_create("dlm_cb", sizeof(struct dlm_callback), 5261bed0baSAlexander Aring __alignof__(struct dlm_callback), 0, 5361bed0baSAlexander Aring NULL); 5461bed0baSAlexander Aring if (!rsb_cache) 5561bed0baSAlexander Aring goto cb; 5661bed0baSAlexander Aring 5775af271eSDan Carpenter return 0; 586c547f26SAlexander Aring 5961bed0baSAlexander Aring cb: 6061bed0baSAlexander Aring kmem_cache_destroy(rsb_cache); 616c547f26SAlexander Aring rsb: 62e4dc81edSAlexander Aring kmem_cache_destroy(msg_cache); 63e4dc81edSAlexander Aring msg: 646c547f26SAlexander Aring kmem_cache_destroy(lkb_cache); 656c547f26SAlexander Aring lkb: 666c547f26SAlexander Aring kmem_cache_destroy(mhandle_cache); 673af2326cSAlexander Aring mhandle: 683af2326cSAlexander Aring kmem_cache_destroy(writequeue_cache); 696c547f26SAlexander Aring out: 706c547f26SAlexander Aring return -ENOMEM; 71e7fd4179SDavid Teigland } 72e7fd4179SDavid Teigland 73e7fd4179SDavid Teigland void dlm_memory_exit(void) 74e7fd4179SDavid Teigland { 753af2326cSAlexander Aring kmem_cache_destroy(writequeue_cache); 766c547f26SAlexander Aring kmem_cache_destroy(mhandle_cache); 77e4dc81edSAlexander Aring kmem_cache_destroy(msg_cache); 78e7fd4179SDavid Teigland kmem_cache_destroy(lkb_cache); 793881ac04SDavid Teigland kmem_cache_destroy(rsb_cache); 8061bed0baSAlexander Aring kmem_cache_destroy(cb_cache); 81e7fd4179SDavid Teigland } 82e7fd4179SDavid Teigland 8352bda2b5SDavid Teigland char *dlm_allocate_lvb(struct dlm_ls *ls) 84e7fd4179SDavid Teigland { 85e7fd4179SDavid Teigland char *p; 86e7fd4179SDavid Teigland 87573c24c4SDavid Teigland p = kzalloc(ls->ls_lvblen, GFP_NOFS); 88e7fd4179SDavid Teigland return p; 89e7fd4179SDavid Teigland } 90e7fd4179SDavid Teigland 9152bda2b5SDavid Teigland void dlm_free_lvb(char *p) 92e7fd4179SDavid Teigland { 93e7fd4179SDavid Teigland kfree(p); 94e7fd4179SDavid Teigland } 95e7fd4179SDavid Teigland 963881ac04SDavid Teigland struct dlm_rsb *dlm_allocate_rsb(struct dlm_ls *ls) 97e7fd4179SDavid Teigland { 98e7fd4179SDavid Teigland struct dlm_rsb *r; 99e7fd4179SDavid Teigland 1003881ac04SDavid Teigland r = kmem_cache_zalloc(rsb_cache, GFP_NOFS); 101e7fd4179SDavid Teigland return r; 102e7fd4179SDavid Teigland } 103e7fd4179SDavid Teigland 10452bda2b5SDavid Teigland void dlm_free_rsb(struct dlm_rsb *r) 105e7fd4179SDavid Teigland { 106e7fd4179SDavid Teigland if (r->res_lvbptr) 10752bda2b5SDavid Teigland dlm_free_lvb(r->res_lvbptr); 1083881ac04SDavid Teigland kmem_cache_free(rsb_cache, r); 109e7fd4179SDavid Teigland } 110e7fd4179SDavid Teigland 11152bda2b5SDavid Teigland struct dlm_lkb *dlm_allocate_lkb(struct dlm_ls *ls) 112e7fd4179SDavid Teigland { 113e7fd4179SDavid Teigland struct dlm_lkb *lkb; 114e7fd4179SDavid Teigland 115573c24c4SDavid Teigland lkb = kmem_cache_zalloc(lkb_cache, GFP_NOFS); 116e7fd4179SDavid Teigland return lkb; 117e7fd4179SDavid Teigland } 118e7fd4179SDavid Teigland 11952bda2b5SDavid Teigland void dlm_free_lkb(struct dlm_lkb *lkb) 120e7fd4179SDavid Teigland { 121597d0caeSDavid Teigland if (lkb->lkb_flags & DLM_IFL_USER) { 122597d0caeSDavid Teigland struct dlm_user_args *ua; 123d292c0ccSDavid Teigland ua = lkb->lkb_ua; 124597d0caeSDavid Teigland if (ua) { 125597d0caeSDavid Teigland kfree(ua->lksb.sb_lvbptr); 126597d0caeSDavid Teigland kfree(ua); 127597d0caeSDavid Teigland } 128597d0caeSDavid Teigland } 12961bed0baSAlexander Aring 13061bed0baSAlexander Aring /* drop references if they are set */ 13161bed0baSAlexander Aring dlm_callback_set_last_ptr(&lkb->lkb_last_cast, NULL); 13261bed0baSAlexander Aring dlm_callback_set_last_ptr(&lkb->lkb_last_cb, NULL); 13361bed0baSAlexander Aring 134e7fd4179SDavid Teigland kmem_cache_free(lkb_cache, lkb); 135e7fd4179SDavid Teigland } 136e7fd4179SDavid Teigland 137*e1711fe3SAlexander Aring struct dlm_mhandle *dlm_allocate_mhandle(gfp_t allocation) 1386c547f26SAlexander Aring { 139*e1711fe3SAlexander Aring return kmem_cache_alloc(mhandle_cache, allocation); 1406c547f26SAlexander Aring } 1416c547f26SAlexander Aring 1426c547f26SAlexander Aring void dlm_free_mhandle(struct dlm_mhandle *mhandle) 1436c547f26SAlexander Aring { 1446c547f26SAlexander Aring kmem_cache_free(mhandle_cache, mhandle); 1456c547f26SAlexander Aring } 1463af2326cSAlexander Aring 1473af2326cSAlexander Aring struct writequeue_entry *dlm_allocate_writequeue(void) 1483af2326cSAlexander Aring { 1493af2326cSAlexander Aring return kmem_cache_alloc(writequeue_cache, GFP_ATOMIC); 1503af2326cSAlexander Aring } 1513af2326cSAlexander Aring 1523af2326cSAlexander Aring void dlm_free_writequeue(struct writequeue_entry *writequeue) 1533af2326cSAlexander Aring { 1543af2326cSAlexander Aring kmem_cache_free(writequeue_cache, writequeue); 1553af2326cSAlexander Aring } 156e4dc81edSAlexander Aring 157e4dc81edSAlexander Aring struct dlm_msg *dlm_allocate_msg(gfp_t allocation) 158e4dc81edSAlexander Aring { 159e4dc81edSAlexander Aring return kmem_cache_alloc(msg_cache, allocation); 160e4dc81edSAlexander Aring } 161e4dc81edSAlexander Aring 162e4dc81edSAlexander Aring void dlm_free_msg(struct dlm_msg *msg) 163e4dc81edSAlexander Aring { 164e4dc81edSAlexander Aring kmem_cache_free(msg_cache, msg); 165e4dc81edSAlexander Aring } 16661bed0baSAlexander Aring 16761bed0baSAlexander Aring struct dlm_callback *dlm_allocate_cb(void) 16861bed0baSAlexander Aring { 16961bed0baSAlexander Aring return kmem_cache_alloc(cb_cache, GFP_ATOMIC); 17061bed0baSAlexander Aring } 17161bed0baSAlexander Aring 17261bed0baSAlexander Aring void dlm_free_cb(struct dlm_callback *cb) 17361bed0baSAlexander Aring { 17461bed0baSAlexander Aring kmem_cache_free(cb_cache, cb); 17561bed0baSAlexander Aring } 176