1*e7fd4179SDavid Teigland /****************************************************************************** 2*e7fd4179SDavid Teigland ******************************************************************************* 3*e7fd4179SDavid Teigland ** 4*e7fd4179SDavid Teigland ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 5*e7fd4179SDavid Teigland ** Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. 6*e7fd4179SDavid Teigland ** 7*e7fd4179SDavid Teigland ** This copyrighted material is made available to anyone wishing to use, 8*e7fd4179SDavid Teigland ** modify, copy, or redistribute it subject to the terms and conditions 9*e7fd4179SDavid Teigland ** of the GNU General Public License v.2. 10*e7fd4179SDavid Teigland ** 11*e7fd4179SDavid Teigland ******************************************************************************* 12*e7fd4179SDavid Teigland ******************************************************************************/ 13*e7fd4179SDavid Teigland 14*e7fd4179SDavid Teigland #include "dlm_internal.h" 15*e7fd4179SDavid Teigland #include "config.h" 16*e7fd4179SDavid Teigland #include "memory.h" 17*e7fd4179SDavid Teigland 18*e7fd4179SDavid Teigland static kmem_cache_t *lkb_cache; 19*e7fd4179SDavid Teigland 20*e7fd4179SDavid Teigland 21*e7fd4179SDavid Teigland int dlm_memory_init(void) 22*e7fd4179SDavid Teigland { 23*e7fd4179SDavid Teigland int ret = 0; 24*e7fd4179SDavid Teigland 25*e7fd4179SDavid Teigland lkb_cache = kmem_cache_create("dlm_lkb", sizeof(struct dlm_lkb), 26*e7fd4179SDavid Teigland __alignof__(struct dlm_lkb), 0, NULL, NULL); 27*e7fd4179SDavid Teigland if (!lkb_cache) 28*e7fd4179SDavid Teigland ret = -ENOMEM; 29*e7fd4179SDavid Teigland return ret; 30*e7fd4179SDavid Teigland } 31*e7fd4179SDavid Teigland 32*e7fd4179SDavid Teigland void dlm_memory_exit(void) 33*e7fd4179SDavid Teigland { 34*e7fd4179SDavid Teigland if (lkb_cache) 35*e7fd4179SDavid Teigland kmem_cache_destroy(lkb_cache); 36*e7fd4179SDavid Teigland } 37*e7fd4179SDavid Teigland 38*e7fd4179SDavid Teigland char *allocate_lvb(struct dlm_ls *ls) 39*e7fd4179SDavid Teigland { 40*e7fd4179SDavid Teigland char *p; 41*e7fd4179SDavid Teigland 42*e7fd4179SDavid Teigland p = kmalloc(ls->ls_lvblen, GFP_KERNEL); 43*e7fd4179SDavid Teigland if (p) 44*e7fd4179SDavid Teigland memset(p, 0, ls->ls_lvblen); 45*e7fd4179SDavid Teigland return p; 46*e7fd4179SDavid Teigland } 47*e7fd4179SDavid Teigland 48*e7fd4179SDavid Teigland void free_lvb(char *p) 49*e7fd4179SDavid Teigland { 50*e7fd4179SDavid Teigland kfree(p); 51*e7fd4179SDavid Teigland } 52*e7fd4179SDavid Teigland 53*e7fd4179SDavid Teigland uint64_t *allocate_range(struct dlm_ls *ls) 54*e7fd4179SDavid Teigland { 55*e7fd4179SDavid Teigland int ralen = 4*sizeof(uint64_t); 56*e7fd4179SDavid Teigland uint64_t *p; 57*e7fd4179SDavid Teigland 58*e7fd4179SDavid Teigland p = kmalloc(ralen, GFP_KERNEL); 59*e7fd4179SDavid Teigland if (p) 60*e7fd4179SDavid Teigland memset(p, 0, ralen); 61*e7fd4179SDavid Teigland return p; 62*e7fd4179SDavid Teigland } 63*e7fd4179SDavid Teigland 64*e7fd4179SDavid Teigland void free_range(uint64_t *p) 65*e7fd4179SDavid Teigland { 66*e7fd4179SDavid Teigland kfree(p); 67*e7fd4179SDavid Teigland } 68*e7fd4179SDavid Teigland 69*e7fd4179SDavid Teigland /* FIXME: have some minimal space built-in to rsb for the name and 70*e7fd4179SDavid Teigland kmalloc a separate name if needed, like dentries are done */ 71*e7fd4179SDavid Teigland 72*e7fd4179SDavid Teigland struct dlm_rsb *allocate_rsb(struct dlm_ls *ls, int namelen) 73*e7fd4179SDavid Teigland { 74*e7fd4179SDavid Teigland struct dlm_rsb *r; 75*e7fd4179SDavid Teigland 76*e7fd4179SDavid Teigland DLM_ASSERT(namelen <= DLM_RESNAME_MAXLEN,); 77*e7fd4179SDavid Teigland 78*e7fd4179SDavid Teigland r = kmalloc(sizeof(*r) + namelen, GFP_KERNEL); 79*e7fd4179SDavid Teigland if (r) 80*e7fd4179SDavid Teigland memset(r, 0, sizeof(*r) + namelen); 81*e7fd4179SDavid Teigland return r; 82*e7fd4179SDavid Teigland } 83*e7fd4179SDavid Teigland 84*e7fd4179SDavid Teigland void free_rsb(struct dlm_rsb *r) 85*e7fd4179SDavid Teigland { 86*e7fd4179SDavid Teigland if (r->res_lvbptr) 87*e7fd4179SDavid Teigland free_lvb(r->res_lvbptr); 88*e7fd4179SDavid Teigland kfree(r); 89*e7fd4179SDavid Teigland } 90*e7fd4179SDavid Teigland 91*e7fd4179SDavid Teigland struct dlm_lkb *allocate_lkb(struct dlm_ls *ls) 92*e7fd4179SDavid Teigland { 93*e7fd4179SDavid Teigland struct dlm_lkb *lkb; 94*e7fd4179SDavid Teigland 95*e7fd4179SDavid Teigland lkb = kmem_cache_alloc(lkb_cache, GFP_KERNEL); 96*e7fd4179SDavid Teigland if (lkb) 97*e7fd4179SDavid Teigland memset(lkb, 0, sizeof(*lkb)); 98*e7fd4179SDavid Teigland return lkb; 99*e7fd4179SDavid Teigland } 100*e7fd4179SDavid Teigland 101*e7fd4179SDavid Teigland void free_lkb(struct dlm_lkb *lkb) 102*e7fd4179SDavid Teigland { 103*e7fd4179SDavid Teigland kmem_cache_free(lkb_cache, lkb); 104*e7fd4179SDavid Teigland } 105*e7fd4179SDavid Teigland 106*e7fd4179SDavid Teigland struct dlm_direntry *allocate_direntry(struct dlm_ls *ls, int namelen) 107*e7fd4179SDavid Teigland { 108*e7fd4179SDavid Teigland struct dlm_direntry *de; 109*e7fd4179SDavid Teigland 110*e7fd4179SDavid Teigland DLM_ASSERT(namelen <= DLM_RESNAME_MAXLEN,); 111*e7fd4179SDavid Teigland 112*e7fd4179SDavid Teigland de = kmalloc(sizeof(*de) + namelen, GFP_KERNEL); 113*e7fd4179SDavid Teigland if (de) 114*e7fd4179SDavid Teigland memset(de, 0, sizeof(*de) + namelen); 115*e7fd4179SDavid Teigland return de; 116*e7fd4179SDavid Teigland } 117*e7fd4179SDavid Teigland 118*e7fd4179SDavid Teigland void free_direntry(struct dlm_direntry *de) 119*e7fd4179SDavid Teigland { 120*e7fd4179SDavid Teigland kfree(de); 121*e7fd4179SDavid Teigland } 122*e7fd4179SDavid Teigland 123