malloc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright © 2001-2007 Red Hat, Inc.
  5. *
  6. * Created by David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * For licensing information, see the file 'LICENCE' in this directory.
  9. *
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/jffs2.h>
  16. #include "nodelist.h"
  17. /* These are initialised to NULL in the kernel startup code.
  18. If you're porting to other operating systems, beware */
  19. static struct kmem_cache *full_dnode_slab;
  20. static struct kmem_cache *raw_dirent_slab;
  21. static struct kmem_cache *raw_inode_slab;
  22. static struct kmem_cache *tmp_dnode_info_slab;
  23. static struct kmem_cache *raw_node_ref_slab;
  24. static struct kmem_cache *node_frag_slab;
  25. static struct kmem_cache *inode_cache_slab;
  26. #ifdef CONFIG_JFFS2_FS_XATTR
  27. static struct kmem_cache *xattr_datum_cache;
  28. static struct kmem_cache *xattr_ref_cache;
  29. #endif
  30. int __init jffs2_create_slab_caches(void)
  31. {
  32. full_dnode_slab = KMEM_CACHE(jffs2_full_dnode, 0);
  33. if (!full_dnode_slab)
  34. goto err;
  35. raw_dirent_slab = KMEM_CACHE(jffs2_raw_dirent, SLAB_HWCACHE_ALIGN);
  36. if (!raw_dirent_slab)
  37. goto err;
  38. raw_inode_slab = KMEM_CACHE(jffs2_raw_inode, SLAB_HWCACHE_ALIGN);
  39. if (!raw_inode_slab)
  40. goto err;
  41. tmp_dnode_info_slab = KMEM_CACHE(jffs2_tmp_dnode_info, 0);
  42. if (!tmp_dnode_info_slab)
  43. goto err;
  44. raw_node_ref_slab = kmem_cache_create("jffs2_refblock",
  45. sizeof(struct jffs2_raw_node_ref) * (REFS_PER_BLOCK + 1),
  46. 0, 0, NULL);
  47. if (!raw_node_ref_slab)
  48. goto err;
  49. node_frag_slab = KMEM_CACHE(jffs2_node_frag, 0);
  50. if (!node_frag_slab)
  51. goto err;
  52. inode_cache_slab = KMEM_CACHE(jffs2_inode_cache, 0);
  53. if (!inode_cache_slab)
  54. goto err;
  55. #ifdef CONFIG_JFFS2_FS_XATTR
  56. xattr_datum_cache = KMEM_CACHE(jffs2_xattr_datum, 0);
  57. if (!xattr_datum_cache)
  58. goto err;
  59. xattr_ref_cache = KMEM_CACHE(jffs2_xattr_ref, 0);
  60. if (!xattr_ref_cache)
  61. goto err;
  62. #endif
  63. return 0;
  64. err:
  65. jffs2_destroy_slab_caches();
  66. return -ENOMEM;
  67. }
  68. void jffs2_destroy_slab_caches(void)
  69. {
  70. kmem_cache_destroy(full_dnode_slab);
  71. kmem_cache_destroy(raw_dirent_slab);
  72. kmem_cache_destroy(raw_inode_slab);
  73. kmem_cache_destroy(tmp_dnode_info_slab);
  74. kmem_cache_destroy(raw_node_ref_slab);
  75. kmem_cache_destroy(node_frag_slab);
  76. kmem_cache_destroy(inode_cache_slab);
  77. #ifdef CONFIG_JFFS2_FS_XATTR
  78. kmem_cache_destroy(xattr_datum_cache);
  79. kmem_cache_destroy(xattr_ref_cache);
  80. #endif
  81. }
  82. struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
  83. {
  84. struct jffs2_full_dirent *ret;
  85. ret = kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
  86. dbg_memalloc("%p\n", ret);
  87. return ret;
  88. }
  89. void jffs2_free_full_dirent(struct jffs2_full_dirent *x)
  90. {
  91. dbg_memalloc("%p\n", x);
  92. kfree(x);
  93. }
  94. struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
  95. {
  96. struct jffs2_full_dnode *ret;
  97. ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
  98. dbg_memalloc("%p\n", ret);
  99. return ret;
  100. }
  101. void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
  102. {
  103. dbg_memalloc("%p\n", x);
  104. kmem_cache_free(full_dnode_slab, x);
  105. }
  106. struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
  107. {
  108. struct jffs2_raw_dirent *ret;
  109. ret = kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
  110. dbg_memalloc("%p\n", ret);
  111. return ret;
  112. }
  113. void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
  114. {
  115. dbg_memalloc("%p\n", x);
  116. kmem_cache_free(raw_dirent_slab, x);
  117. }
  118. struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
  119. {
  120. struct jffs2_raw_inode *ret;
  121. ret = kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
  122. dbg_memalloc("%p\n", ret);
  123. return ret;
  124. }
  125. void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
  126. {
  127. dbg_memalloc("%p\n", x);
  128. kmem_cache_free(raw_inode_slab, x);
  129. }
  130. struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
  131. {
  132. struct jffs2_tmp_dnode_info *ret;
  133. ret = kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
  134. dbg_memalloc("%p\n",
  135. ret);
  136. return ret;
  137. }
  138. void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
  139. {
  140. dbg_memalloc("%p\n", x);
  141. kmem_cache_free(tmp_dnode_info_slab, x);
  142. }
  143. static struct jffs2_raw_node_ref *jffs2_alloc_refblock(void)
  144. {
  145. struct jffs2_raw_node_ref *ret;
  146. ret = kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
  147. if (ret) {
  148. int i = 0;
  149. for (i=0; i < REFS_PER_BLOCK; i++) {
  150. ret[i].flash_offset = REF_EMPTY_NODE;
  151. ret[i].next_in_ino = NULL;
  152. }
  153. ret[i].flash_offset = REF_LINK_NODE;
  154. ret[i].next_in_ino = NULL;
  155. }
  156. return ret;
  157. }
  158. int jffs2_prealloc_raw_node_refs(struct jffs2_sb_info *c,
  159. struct jffs2_eraseblock *jeb, int nr)
  160. {
  161. struct jffs2_raw_node_ref **p, *ref;
  162. int i = nr;
  163. dbg_memalloc("%d\n", nr);
  164. p = &jeb->last_node;
  165. ref = *p;
  166. dbg_memalloc("Reserving %d refs for block @0x%08x\n", nr, jeb->offset);
  167. /* If jeb->last_node is really a valid node then skip over it */
  168. if (ref && ref->flash_offset != REF_EMPTY_NODE)
  169. ref++;
  170. while (i) {
  171. if (!ref) {
  172. dbg_memalloc("Allocating new refblock linked from %p\n", p);
  173. ref = *p = jffs2_alloc_refblock();
  174. if (!ref)
  175. return -ENOMEM;
  176. }
  177. if (ref->flash_offset == REF_LINK_NODE) {
  178. p = &ref->next_in_ino;
  179. ref = *p;
  180. continue;
  181. }
  182. i--;
  183. ref++;
  184. }
  185. jeb->allocated_refs = nr;
  186. dbg_memalloc("Reserved %d refs for block @0x%08x, last_node is %p (%08x,%p)\n",
  187. nr, jeb->offset, jeb->last_node, jeb->last_node->flash_offset,
  188. jeb->last_node->next_in_ino);
  189. return 0;
  190. }
  191. void jffs2_free_refblock(struct jffs2_raw_node_ref *x)
  192. {
  193. dbg_memalloc("%p\n", x);
  194. kmem_cache_free(raw_node_ref_slab, x);
  195. }
  196. struct jffs2_node_frag *jffs2_alloc_node_frag(void)
  197. {
  198. struct jffs2_node_frag *ret;
  199. ret = kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
  200. dbg_memalloc("%p\n", ret);
  201. return ret;
  202. }
  203. void jffs2_free_node_frag(struct jffs2_node_frag *x)
  204. {
  205. dbg_memalloc("%p\n", x);
  206. kmem_cache_free(node_frag_slab, x);
  207. }
  208. struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
  209. {
  210. struct jffs2_inode_cache *ret;
  211. ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
  212. dbg_memalloc("%p\n", ret);
  213. return ret;
  214. }
  215. void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
  216. {
  217. dbg_memalloc("%p\n", x);
  218. kmem_cache_free(inode_cache_slab, x);
  219. }
  220. #ifdef CONFIG_JFFS2_FS_XATTR
  221. struct jffs2_xattr_datum *jffs2_alloc_xattr_datum(void)
  222. {
  223. struct jffs2_xattr_datum *xd;
  224. xd = kmem_cache_zalloc(xattr_datum_cache, GFP_KERNEL);
  225. dbg_memalloc("%p\n", xd);
  226. if (!xd)
  227. return NULL;
  228. xd->class = RAWNODE_CLASS_XATTR_DATUM;
  229. xd->node = (void *)xd;
  230. INIT_LIST_HEAD(&xd->xindex);
  231. return xd;
  232. }
  233. void jffs2_free_xattr_datum(struct jffs2_xattr_datum *xd)
  234. {
  235. dbg_memalloc("%p\n", xd);
  236. kmem_cache_free(xattr_datum_cache, xd);
  237. }
  238. struct jffs2_xattr_ref *jffs2_alloc_xattr_ref(void)
  239. {
  240. struct jffs2_xattr_ref *ref;
  241. ref = kmem_cache_zalloc(xattr_ref_cache, GFP_KERNEL);
  242. dbg_memalloc("%p\n", ref);
  243. if (!ref)
  244. return NULL;
  245. ref->class = RAWNODE_CLASS_XATTR_REF;
  246. ref->node = (void *)ref;
  247. return ref;
  248. }
  249. void jffs2_free_xattr_ref(struct jffs2_xattr_ref *ref)
  250. {
  251. dbg_memalloc("%p\n", ref);
  252. kmem_cache_free(xattr_ref_cache, ref);
  253. }
  254. #endif