xref: /linux/fs/dlm/memory.c (revision 3af2326ca0a13cf84aeb75e001e757ff3cefeae9)
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"
14*3af2326cSAlexander Aring #include "lowcomms.h"
15e7fd4179SDavid Teigland #include "config.h"
16e7fd4179SDavid Teigland #include "memory.h"
17e7fd4179SDavid Teigland 
18*3af2326cSAlexander Aring static struct kmem_cache *writequeue_cache;
196c547f26SAlexander Aring static struct kmem_cache *mhandle_cache;
20e18b890bSChristoph Lameter static struct kmem_cache *lkb_cache;
213881ac04SDavid Teigland static struct kmem_cache *rsb_cache;
22e7fd4179SDavid Teigland 
23e7fd4179SDavid Teigland 
2430727174SDenis Cheng int __init dlm_memory_init(void)
25e7fd4179SDavid Teigland {
26*3af2326cSAlexander Aring 	writequeue_cache = dlm_lowcomms_writequeue_cache_create();
27*3af2326cSAlexander Aring 	if (!writequeue_cache)
28*3af2326cSAlexander Aring 		goto out;
29*3af2326cSAlexander Aring 
306c547f26SAlexander Aring 	mhandle_cache = dlm_midcomms_cache_create();
316c547f26SAlexander Aring 	if (!mhandle_cache)
32*3af2326cSAlexander Aring 		goto mhandle;
336c547f26SAlexander Aring 
34e7fd4179SDavid Teigland 	lkb_cache = kmem_cache_create("dlm_lkb", sizeof(struct dlm_lkb),
3520c2df83SPaul Mundt 				__alignof__(struct dlm_lkb), 0, NULL);
36e7fd4179SDavid Teigland 	if (!lkb_cache)
376c547f26SAlexander Aring 		goto lkb;
383881ac04SDavid Teigland 
393881ac04SDavid Teigland 	rsb_cache = kmem_cache_create("dlm_rsb", sizeof(struct dlm_rsb),
403881ac04SDavid Teigland 				__alignof__(struct dlm_rsb), 0, NULL);
416c547f26SAlexander Aring 	if (!rsb_cache)
426c547f26SAlexander Aring 		goto rsb;
433881ac04SDavid Teigland 
4475af271eSDan Carpenter 	return 0;
456c547f26SAlexander Aring 
466c547f26SAlexander Aring rsb:
476c547f26SAlexander Aring 	kmem_cache_destroy(lkb_cache);
486c547f26SAlexander Aring lkb:
496c547f26SAlexander Aring 	kmem_cache_destroy(mhandle_cache);
50*3af2326cSAlexander Aring mhandle:
51*3af2326cSAlexander Aring 	kmem_cache_destroy(writequeue_cache);
526c547f26SAlexander Aring out:
536c547f26SAlexander Aring 	return -ENOMEM;
54e7fd4179SDavid Teigland }
55e7fd4179SDavid Teigland 
56e7fd4179SDavid Teigland void dlm_memory_exit(void)
57e7fd4179SDavid Teigland {
58*3af2326cSAlexander Aring 	kmem_cache_destroy(writequeue_cache);
596c547f26SAlexander Aring 	kmem_cache_destroy(mhandle_cache);
60e7fd4179SDavid Teigland 	kmem_cache_destroy(lkb_cache);
613881ac04SDavid Teigland 	kmem_cache_destroy(rsb_cache);
62e7fd4179SDavid Teigland }
63e7fd4179SDavid Teigland 
6452bda2b5SDavid Teigland char *dlm_allocate_lvb(struct dlm_ls *ls)
65e7fd4179SDavid Teigland {
66e7fd4179SDavid Teigland 	char *p;
67e7fd4179SDavid Teigland 
68573c24c4SDavid Teigland 	p = kzalloc(ls->ls_lvblen, GFP_NOFS);
69e7fd4179SDavid Teigland 	return p;
70e7fd4179SDavid Teigland }
71e7fd4179SDavid Teigland 
7252bda2b5SDavid Teigland void dlm_free_lvb(char *p)
73e7fd4179SDavid Teigland {
74e7fd4179SDavid Teigland 	kfree(p);
75e7fd4179SDavid Teigland }
76e7fd4179SDavid Teigland 
773881ac04SDavid Teigland struct dlm_rsb *dlm_allocate_rsb(struct dlm_ls *ls)
78e7fd4179SDavid Teigland {
79e7fd4179SDavid Teigland 	struct dlm_rsb *r;
80e7fd4179SDavid Teigland 
813881ac04SDavid Teigland 	r = kmem_cache_zalloc(rsb_cache, GFP_NOFS);
82e7fd4179SDavid Teigland 	return r;
83e7fd4179SDavid Teigland }
84e7fd4179SDavid Teigland 
8552bda2b5SDavid Teigland void dlm_free_rsb(struct dlm_rsb *r)
86e7fd4179SDavid Teigland {
87e7fd4179SDavid Teigland 	if (r->res_lvbptr)
8852bda2b5SDavid Teigland 		dlm_free_lvb(r->res_lvbptr);
893881ac04SDavid Teigland 	kmem_cache_free(rsb_cache, r);
90e7fd4179SDavid Teigland }
91e7fd4179SDavid Teigland 
9252bda2b5SDavid Teigland struct dlm_lkb *dlm_allocate_lkb(struct dlm_ls *ls)
93e7fd4179SDavid Teigland {
94e7fd4179SDavid Teigland 	struct dlm_lkb *lkb;
95e7fd4179SDavid Teigland 
96573c24c4SDavid Teigland 	lkb = kmem_cache_zalloc(lkb_cache, GFP_NOFS);
97e7fd4179SDavid Teigland 	return lkb;
98e7fd4179SDavid Teigland }
99e7fd4179SDavid Teigland 
10052bda2b5SDavid Teigland void dlm_free_lkb(struct dlm_lkb *lkb)
101e7fd4179SDavid Teigland {
102597d0caeSDavid Teigland 	if (lkb->lkb_flags & DLM_IFL_USER) {
103597d0caeSDavid Teigland 		struct dlm_user_args *ua;
104d292c0ccSDavid Teigland 		ua = lkb->lkb_ua;
105597d0caeSDavid Teigland 		if (ua) {
106597d0caeSDavid Teigland 			kfree(ua->lksb.sb_lvbptr);
107597d0caeSDavid Teigland 			kfree(ua);
108597d0caeSDavid Teigland 		}
109597d0caeSDavid Teigland 	}
110e7fd4179SDavid Teigland 	kmem_cache_free(lkb_cache, lkb);
111e7fd4179SDavid Teigland }
112e7fd4179SDavid Teigland 
1136c547f26SAlexander Aring struct dlm_mhandle *dlm_allocate_mhandle(void)
1146c547f26SAlexander Aring {
1156c547f26SAlexander Aring 	return kmem_cache_alloc(mhandle_cache, GFP_NOFS);
1166c547f26SAlexander Aring }
1176c547f26SAlexander Aring 
1186c547f26SAlexander Aring void dlm_free_mhandle(struct dlm_mhandle *mhandle)
1196c547f26SAlexander Aring {
1206c547f26SAlexander Aring 	kmem_cache_free(mhandle_cache, mhandle);
1216c547f26SAlexander Aring }
122*3af2326cSAlexander Aring 
123*3af2326cSAlexander Aring struct writequeue_entry *dlm_allocate_writequeue(void)
124*3af2326cSAlexander Aring {
125*3af2326cSAlexander Aring 	return kmem_cache_alloc(writequeue_cache, GFP_ATOMIC);
126*3af2326cSAlexander Aring }
127*3af2326cSAlexander Aring 
128*3af2326cSAlexander Aring void dlm_free_writequeue(struct writequeue_entry *writequeue)
129*3af2326cSAlexander Aring {
130*3af2326cSAlexander Aring 	kmem_cache_free(writequeue_cache, writequeue);
131*3af2326cSAlexander Aring }
132