yaffs_allocator.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2010 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include "yaffs_allocator.h"
  14. #include "yaffs_guts.h"
  15. #include "yaffs_trace.h"
  16. #include "yportenv.h"
  17. #ifdef CONFIG_YAFFS_KMALLOC_ALLOCATOR
  18. void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
  19. {
  20. dev = dev;
  21. }
  22. void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
  23. {
  24. dev = dev;
  25. }
  26. struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
  27. {
  28. return (struct yaffs_tnode *)kmalloc(dev->tnode_size, GFP_NOFS);
  29. }
  30. void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  31. {
  32. dev = dev;
  33. kfree(tn);
  34. }
  35. void yaffs_init_raw_objs(struct yaffs_dev *dev)
  36. {
  37. dev = dev;
  38. }
  39. void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
  40. {
  41. dev = dev;
  42. }
  43. struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
  44. {
  45. dev = dev;
  46. return (struct yaffs_obj *)kmalloc(sizeof(struct yaffs_obj));
  47. }
  48. void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
  49. {
  50. dev = dev;
  51. kfree(obj);
  52. }
  53. #else
  54. struct yaffs_tnode_list {
  55. struct yaffs_tnode_list *next;
  56. struct yaffs_tnode *tnodes;
  57. };
  58. struct yaffs_obj_list {
  59. struct yaffs_obj_list *next;
  60. struct yaffs_obj *objects;
  61. };
  62. struct yaffs_allocator {
  63. int n_tnodes_created;
  64. struct yaffs_tnode *free_tnodes;
  65. int n_free_tnodes;
  66. struct yaffs_tnode_list *alloc_tnode_list;
  67. int n_obj_created;
  68. struct yaffs_obj *free_objs;
  69. int n_free_objects;
  70. struct yaffs_obj_list *allocated_obj_list;
  71. };
  72. static void yaffs_deinit_raw_tnodes(struct yaffs_dev *dev)
  73. {
  74. struct yaffs_allocator *allocator =
  75. (struct yaffs_allocator *)dev->allocator;
  76. struct yaffs_tnode_list *tmp;
  77. if (!allocator) {
  78. YBUG();
  79. return;
  80. }
  81. while (allocator->alloc_tnode_list) {
  82. tmp = allocator->alloc_tnode_list->next;
  83. kfree(allocator->alloc_tnode_list->tnodes);
  84. kfree(allocator->alloc_tnode_list);
  85. allocator->alloc_tnode_list = tmp;
  86. }
  87. allocator->free_tnodes = NULL;
  88. allocator->n_free_tnodes = 0;
  89. allocator->n_tnodes_created = 0;
  90. }
  91. static void yaffs_init_raw_tnodes(struct yaffs_dev *dev)
  92. {
  93. struct yaffs_allocator *allocator = dev->allocator;
  94. if (allocator) {
  95. allocator->alloc_tnode_list = NULL;
  96. allocator->free_tnodes = NULL;
  97. allocator->n_free_tnodes = 0;
  98. allocator->n_tnodes_created = 0;
  99. } else {
  100. YBUG();
  101. }
  102. }
  103. static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes)
  104. {
  105. struct yaffs_allocator *allocator =
  106. (struct yaffs_allocator *)dev->allocator;
  107. int i;
  108. struct yaffs_tnode *new_tnodes;
  109. u8 *mem;
  110. struct yaffs_tnode *curr;
  111. struct yaffs_tnode *next;
  112. struct yaffs_tnode_list *tnl;
  113. if (!allocator) {
  114. YBUG();
  115. return YAFFS_FAIL;
  116. }
  117. if (n_tnodes < 1)
  118. return YAFFS_OK;
  119. /* make these things */
  120. new_tnodes = kmalloc(n_tnodes * dev->tnode_size, GFP_NOFS);
  121. mem = (u8 *) new_tnodes;
  122. if (!new_tnodes) {
  123. yaffs_trace(YAFFS_TRACE_ERROR,
  124. "yaffs: Could not allocate Tnodes");
  125. return YAFFS_FAIL;
  126. }
  127. /* New hookup for wide tnodes */
  128. for (i = 0; i < n_tnodes - 1; i++) {
  129. curr = (struct yaffs_tnode *)&mem[i * dev->tnode_size];
  130. next = (struct yaffs_tnode *)&mem[(i + 1) * dev->tnode_size];
  131. curr->internal[0] = next;
  132. }
  133. curr = (struct yaffs_tnode *)&mem[(n_tnodes - 1) * dev->tnode_size];
  134. curr->internal[0] = allocator->free_tnodes;
  135. allocator->free_tnodes = (struct yaffs_tnode *)mem;
  136. allocator->n_free_tnodes += n_tnodes;
  137. allocator->n_tnodes_created += n_tnodes;
  138. /* Now add this bunch of tnodes to a list for freeing up.
  139. * NB If we can't add this to the management list it isn't fatal
  140. * but it just means we can't free this bunch of tnodes later.
  141. */
  142. tnl = kmalloc(sizeof(struct yaffs_tnode_list), GFP_NOFS);
  143. if (!tnl) {
  144. yaffs_trace(YAFFS_TRACE_ERROR,
  145. "Could not add tnodes to management list");
  146. return YAFFS_FAIL;
  147. } else {
  148. tnl->tnodes = new_tnodes;
  149. tnl->next = allocator->alloc_tnode_list;
  150. allocator->alloc_tnode_list = tnl;
  151. }
  152. yaffs_trace(YAFFS_TRACE_ALLOCATE,"Tnodes added");
  153. return YAFFS_OK;
  154. }
  155. struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
  156. {
  157. struct yaffs_allocator *allocator =
  158. (struct yaffs_allocator *)dev->allocator;
  159. struct yaffs_tnode *tn = NULL;
  160. if (!allocator) {
  161. YBUG();
  162. return NULL;
  163. }
  164. /* If there are none left make more */
  165. if (!allocator->free_tnodes)
  166. yaffs_create_tnodes(dev, YAFFS_ALLOCATION_NTNODES);
  167. if (allocator->free_tnodes) {
  168. tn = allocator->free_tnodes;
  169. allocator->free_tnodes = allocator->free_tnodes->internal[0];
  170. allocator->n_free_tnodes--;
  171. }
  172. return tn;
  173. }
  174. /* FreeTnode frees up a tnode and puts it back on the free list */
  175. void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  176. {
  177. struct yaffs_allocator *allocator = dev->allocator;
  178. if (!allocator) {
  179. YBUG();
  180. return;
  181. }
  182. if (tn) {
  183. tn->internal[0] = allocator->free_tnodes;
  184. allocator->free_tnodes = tn;
  185. allocator->n_free_tnodes++;
  186. }
  187. dev->checkpoint_blocks_required = 0; /* force recalculation */
  188. }
  189. static void yaffs_init_raw_objs(struct yaffs_dev *dev)
  190. {
  191. struct yaffs_allocator *allocator = dev->allocator;
  192. if (allocator) {
  193. allocator->allocated_obj_list = NULL;
  194. allocator->free_objs = NULL;
  195. allocator->n_free_objects = 0;
  196. } else {
  197. YBUG();
  198. }
  199. }
  200. static void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
  201. {
  202. struct yaffs_allocator *allocator = dev->allocator;
  203. struct yaffs_obj_list *tmp;
  204. if (!allocator) {
  205. YBUG();
  206. return;
  207. }
  208. while (allocator->allocated_obj_list) {
  209. tmp = allocator->allocated_obj_list->next;
  210. kfree(allocator->allocated_obj_list->objects);
  211. kfree(allocator->allocated_obj_list);
  212. allocator->allocated_obj_list = tmp;
  213. }
  214. allocator->free_objs = NULL;
  215. allocator->n_free_objects = 0;
  216. allocator->n_obj_created = 0;
  217. }
  218. static int yaffs_create_free_objs(struct yaffs_dev *dev, int n_obj)
  219. {
  220. struct yaffs_allocator *allocator = dev->allocator;
  221. int i;
  222. struct yaffs_obj *new_objs;
  223. struct yaffs_obj_list *list;
  224. if (!allocator) {
  225. YBUG();
  226. return YAFFS_FAIL;
  227. }
  228. if (n_obj < 1)
  229. return YAFFS_OK;
  230. /* make these things */
  231. new_objs = kmalloc(n_obj * sizeof(struct yaffs_obj), GFP_NOFS);
  232. list = kmalloc(sizeof(struct yaffs_obj_list), GFP_NOFS);
  233. if (!new_objs || !list) {
  234. if (new_objs) {
  235. kfree(new_objs);
  236. new_objs = NULL;
  237. }
  238. if (list) {
  239. kfree(list);
  240. list = NULL;
  241. }
  242. yaffs_trace(YAFFS_TRACE_ALLOCATE,
  243. "Could not allocate more objects");
  244. return YAFFS_FAIL;
  245. }
  246. /* Hook them into the free list */
  247. for (i = 0; i < n_obj - 1; i++) {
  248. new_objs[i].siblings.next =
  249. (struct list_head *)(&new_objs[i + 1]);
  250. }
  251. new_objs[n_obj - 1].siblings.next = (void *)allocator->free_objs;
  252. allocator->free_objs = new_objs;
  253. allocator->n_free_objects += n_obj;
  254. allocator->n_obj_created += n_obj;
  255. /* Now add this bunch of Objects to a list for freeing up. */
  256. list->objects = new_objs;
  257. list->next = allocator->allocated_obj_list;
  258. allocator->allocated_obj_list = list;
  259. return YAFFS_OK;
  260. }
  261. struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
  262. {
  263. struct yaffs_obj *obj = NULL;
  264. struct yaffs_allocator *allocator = dev->allocator;
  265. if (!allocator) {
  266. YBUG();
  267. return obj;
  268. }
  269. /* If there are none left make more */
  270. if (!allocator->free_objs)
  271. yaffs_create_free_objs(dev, YAFFS_ALLOCATION_NOBJECTS);
  272. if (allocator->free_objs) {
  273. obj = allocator->free_objs;
  274. allocator->free_objs =
  275. (struct yaffs_obj *)(allocator->free_objs->siblings.next);
  276. allocator->n_free_objects--;
  277. }
  278. return obj;
  279. }
  280. void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
  281. {
  282. struct yaffs_allocator *allocator = dev->allocator;
  283. if (!allocator)
  284. YBUG();
  285. else {
  286. /* Link into the free list. */
  287. obj->siblings.next = (struct list_head *)(allocator->free_objs);
  288. allocator->free_objs = obj;
  289. allocator->n_free_objects++;
  290. }
  291. }
  292. void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
  293. {
  294. if (dev->allocator) {
  295. yaffs_deinit_raw_tnodes(dev);
  296. yaffs_deinit_raw_objs(dev);
  297. kfree(dev->allocator);
  298. dev->allocator = NULL;
  299. } else {
  300. YBUG();
  301. }
  302. }
  303. void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
  304. {
  305. struct yaffs_allocator *allocator;
  306. if (!dev->allocator) {
  307. allocator = kmalloc(sizeof(struct yaffs_allocator), GFP_NOFS);
  308. if (allocator) {
  309. dev->allocator = allocator;
  310. yaffs_init_raw_tnodes(dev);
  311. yaffs_init_raw_objs(dev);
  312. }
  313. } else {
  314. YBUG();
  315. }
  316. }
  317. #endif