malloc.c 7.4 KB

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