malloc.c 7.3 KB

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