xref: /linux/fs/dlm/memory.c (revision e4dc81ed5a8069b8ae56116058ebbad77ff559ec)
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"
17e7fd4179SDavid Teigland 
183af2326cSAlexander Aring static struct kmem_cache *writequeue_cache;
196c547f26SAlexander Aring static struct kmem_cache *mhandle_cache;
20*e4dc81edSAlexander Aring static struct kmem_cache *msg_cache;
21e18b890bSChristoph Lameter static struct kmem_cache *lkb_cache;
223881ac04SDavid Teigland static struct kmem_cache *rsb_cache;
23e7fd4179SDavid Teigland 
24e7fd4179SDavid Teigland 
2530727174SDenis Cheng int __init dlm_memory_init(void)
26e7fd4179SDavid Teigland {
273af2326cSAlexander Aring 	writequeue_cache = dlm_lowcomms_writequeue_cache_create();
283af2326cSAlexander Aring 	if (!writequeue_cache)
293af2326cSAlexander Aring 		goto out;
303af2326cSAlexander Aring 
316c547f26SAlexander Aring 	mhandle_cache = dlm_midcomms_cache_create();
326c547f26SAlexander Aring 	if (!mhandle_cache)
333af2326cSAlexander Aring 		goto mhandle;
346c547f26SAlexander Aring 
35e7fd4179SDavid Teigland 	lkb_cache = kmem_cache_create("dlm_lkb", sizeof(struct dlm_lkb),
3620c2df83SPaul Mundt 				__alignof__(struct dlm_lkb), 0, NULL);
37e7fd4179SDavid Teigland 	if (!lkb_cache)
386c547f26SAlexander Aring 		goto lkb;
393881ac04SDavid Teigland 
40*e4dc81edSAlexander Aring 	msg_cache = dlm_lowcomms_msg_cache_create();
41*e4dc81edSAlexander Aring 	if (!msg_cache)
42*e4dc81edSAlexander Aring 		goto msg;
43*e4dc81edSAlexander Aring 
443881ac04SDavid Teigland 	rsb_cache = kmem_cache_create("dlm_rsb", sizeof(struct dlm_rsb),
453881ac04SDavid Teigland 				__alignof__(struct dlm_rsb), 0, NULL);
466c547f26SAlexander Aring 	if (!rsb_cache)
476c547f26SAlexander Aring 		goto rsb;
483881ac04SDavid Teigland 
4975af271eSDan Carpenter 	return 0;
506c547f26SAlexander Aring 
516c547f26SAlexander Aring rsb:
52*e4dc81edSAlexander Aring 	kmem_cache_destroy(msg_cache);
53*e4dc81edSAlexander Aring msg:
546c547f26SAlexander Aring 	kmem_cache_destroy(lkb_cache);
556c547f26SAlexander Aring lkb:
566c547f26SAlexander Aring 	kmem_cache_destroy(mhandle_cache);
573af2326cSAlexander Aring mhandle:
583af2326cSAlexander Aring 	kmem_cache_destroy(writequeue_cache);
596c547f26SAlexander Aring out:
606c547f26SAlexander Aring 	return -ENOMEM;
61e7fd4179SDavid Teigland }
62e7fd4179SDavid Teigland 
63e7fd4179SDavid Teigland void dlm_memory_exit(void)
64e7fd4179SDavid Teigland {
653af2326cSAlexander Aring 	kmem_cache_destroy(writequeue_cache);
666c547f26SAlexander Aring 	kmem_cache_destroy(mhandle_cache);
67*e4dc81edSAlexander Aring 	kmem_cache_destroy(msg_cache);
68e7fd4179SDavid Teigland 	kmem_cache_destroy(lkb_cache);
693881ac04SDavid Teigland 	kmem_cache_destroy(rsb_cache);
70e7fd4179SDavid Teigland }
71e7fd4179SDavid Teigland 
7252bda2b5SDavid Teigland char *dlm_allocate_lvb(struct dlm_ls *ls)
73e7fd4179SDavid Teigland {
74e7fd4179SDavid Teigland 	char *p;
75e7fd4179SDavid Teigland 
76573c24c4SDavid Teigland 	p = kzalloc(ls->ls_lvblen, GFP_NOFS);
77e7fd4179SDavid Teigland 	return p;
78e7fd4179SDavid Teigland }
79e7fd4179SDavid Teigland 
8052bda2b5SDavid Teigland void dlm_free_lvb(char *p)
81e7fd4179SDavid Teigland {
82e7fd4179SDavid Teigland 	kfree(p);
83e7fd4179SDavid Teigland }
84e7fd4179SDavid Teigland 
853881ac04SDavid Teigland struct dlm_rsb *dlm_allocate_rsb(struct dlm_ls *ls)
86e7fd4179SDavid Teigland {
87e7fd4179SDavid Teigland 	struct dlm_rsb *r;
88e7fd4179SDavid Teigland 
893881ac04SDavid Teigland 	r = kmem_cache_zalloc(rsb_cache, GFP_NOFS);
90e7fd4179SDavid Teigland 	return r;
91e7fd4179SDavid Teigland }
92e7fd4179SDavid Teigland 
9352bda2b5SDavid Teigland void dlm_free_rsb(struct dlm_rsb *r)
94e7fd4179SDavid Teigland {
95e7fd4179SDavid Teigland 	if (r->res_lvbptr)
9652bda2b5SDavid Teigland 		dlm_free_lvb(r->res_lvbptr);
973881ac04SDavid Teigland 	kmem_cache_free(rsb_cache, r);
98e7fd4179SDavid Teigland }
99e7fd4179SDavid Teigland 
10052bda2b5SDavid Teigland struct dlm_lkb *dlm_allocate_lkb(struct dlm_ls *ls)
101e7fd4179SDavid Teigland {
102e7fd4179SDavid Teigland 	struct dlm_lkb *lkb;
103e7fd4179SDavid Teigland 
104573c24c4SDavid Teigland 	lkb = kmem_cache_zalloc(lkb_cache, GFP_NOFS);
105e7fd4179SDavid Teigland 	return lkb;
106e7fd4179SDavid Teigland }
107e7fd4179SDavid Teigland 
10852bda2b5SDavid Teigland void dlm_free_lkb(struct dlm_lkb *lkb)
109e7fd4179SDavid Teigland {
110597d0caeSDavid Teigland 	if (lkb->lkb_flags & DLM_IFL_USER) {
111597d0caeSDavid Teigland 		struct dlm_user_args *ua;
112d292c0ccSDavid Teigland 		ua = lkb->lkb_ua;
113597d0caeSDavid Teigland 		if (ua) {
114597d0caeSDavid Teigland 			kfree(ua->lksb.sb_lvbptr);
115597d0caeSDavid Teigland 			kfree(ua);
116597d0caeSDavid Teigland 		}
117597d0caeSDavid Teigland 	}
118e7fd4179SDavid Teigland 	kmem_cache_free(lkb_cache, lkb);
119e7fd4179SDavid Teigland }
120e7fd4179SDavid Teigland 
1216c547f26SAlexander Aring struct dlm_mhandle *dlm_allocate_mhandle(void)
1226c547f26SAlexander Aring {
1236c547f26SAlexander Aring 	return kmem_cache_alloc(mhandle_cache, GFP_NOFS);
1246c547f26SAlexander Aring }
1256c547f26SAlexander Aring 
1266c547f26SAlexander Aring void dlm_free_mhandle(struct dlm_mhandle *mhandle)
1276c547f26SAlexander Aring {
1286c547f26SAlexander Aring 	kmem_cache_free(mhandle_cache, mhandle);
1296c547f26SAlexander Aring }
1303af2326cSAlexander Aring 
1313af2326cSAlexander Aring struct writequeue_entry *dlm_allocate_writequeue(void)
1323af2326cSAlexander Aring {
1333af2326cSAlexander Aring 	return kmem_cache_alloc(writequeue_cache, GFP_ATOMIC);
1343af2326cSAlexander Aring }
1353af2326cSAlexander Aring 
1363af2326cSAlexander Aring void dlm_free_writequeue(struct writequeue_entry *writequeue)
1373af2326cSAlexander Aring {
1383af2326cSAlexander Aring 	kmem_cache_free(writequeue_cache, writequeue);
1393af2326cSAlexander Aring }
140*e4dc81edSAlexander Aring 
141*e4dc81edSAlexander Aring struct dlm_msg *dlm_allocate_msg(gfp_t allocation)
142*e4dc81edSAlexander Aring {
143*e4dc81edSAlexander Aring 	return kmem_cache_alloc(msg_cache, allocation);
144*e4dc81edSAlexander Aring }
145*e4dc81edSAlexander Aring 
146*e4dc81edSAlexander Aring void dlm_free_msg(struct dlm_msg *msg)
147*e4dc81edSAlexander Aring {
148*e4dc81edSAlexander Aring 	kmem_cache_free(msg_cache, msg);
149*e4dc81edSAlexander Aring }
150