1
0

yaffs_allocator.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2011 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. /*
  18. * Each entry in yaffs_tnode_list and yaffs_obj_list hold blocks
  19. * of approx 100 objects that are themn allocated singly.
  20. * This is basically a simplified slab allocator.
  21. *
  22. * We don't use the Linux slab allocator because slab does not allow
  23. * us to dump all the objects in one hit when we do a umount and tear
  24. * down all the tnodes and objects. slab requires that we first free
  25. * the individual objects.
  26. *
  27. * Once yaffs has been mainlined I shall try to motivate for a change
  28. * to slab to provide the extra features we need here.
  29. */
  30. struct yaffs_tnode_list {
  31. struct yaffs_tnode_list *next;
  32. struct yaffs_tnode *tnodes;
  33. };
  34. struct yaffs_obj_list {
  35. struct yaffs_obj_list *next;
  36. struct yaffs_obj *objects;
  37. };
  38. struct yaffs_allocator {
  39. int n_tnodes_created;
  40. struct yaffs_tnode *free_tnodes;
  41. int n_free_tnodes;
  42. struct yaffs_tnode_list *alloc_tnode_list;
  43. int n_obj_created;
  44. struct list_head free_objs;
  45. int n_free_objects;
  46. struct yaffs_obj_list *allocated_obj_list;
  47. };
  48. static void yaffs_deinit_raw_tnodes(struct yaffs_dev *dev)
  49. {
  50. struct yaffs_allocator *allocator =
  51. (struct yaffs_allocator *)dev->allocator;
  52. struct yaffs_tnode_list *tmp;
  53. if (!allocator) {
  54. BUG();
  55. return;
  56. }
  57. while (allocator->alloc_tnode_list) {
  58. tmp = allocator->alloc_tnode_list->next;
  59. kfree(allocator->alloc_tnode_list->tnodes);
  60. kfree(allocator->alloc_tnode_list);
  61. allocator->alloc_tnode_list = tmp;
  62. }
  63. allocator->free_tnodes = NULL;
  64. allocator->n_free_tnodes = 0;
  65. allocator->n_tnodes_created = 0;
  66. }
  67. static void yaffs_init_raw_tnodes(struct yaffs_dev *dev)
  68. {
  69. struct yaffs_allocator *allocator = dev->allocator;
  70. if (!allocator) {
  71. BUG();
  72. return;
  73. }
  74. allocator->alloc_tnode_list = NULL;
  75. allocator->free_tnodes = NULL;
  76. allocator->n_free_tnodes = 0;
  77. allocator->n_tnodes_created = 0;
  78. }
  79. static int yaffs_create_tnodes(struct yaffs_dev *dev, int n_tnodes)
  80. {
  81. struct yaffs_allocator *allocator =
  82. (struct yaffs_allocator *)dev->allocator;
  83. int i;
  84. struct yaffs_tnode *new_tnodes;
  85. u8 *mem;
  86. struct yaffs_tnode *curr;
  87. struct yaffs_tnode *next;
  88. struct yaffs_tnode_list *tnl;
  89. if (!allocator) {
  90. BUG();
  91. return YAFFS_FAIL;
  92. }
  93. if (n_tnodes < 1)
  94. return YAFFS_OK;
  95. /* make these things */
  96. new_tnodes = kmalloc(n_tnodes * dev->tnode_size, GFP_NOFS);
  97. mem = (u8 *) new_tnodes;
  98. if (!new_tnodes) {
  99. yaffs_trace(YAFFS_TRACE_ERROR,
  100. "yaffs: Could not allocate Tnodes");
  101. return YAFFS_FAIL;
  102. }
  103. /* New hookup for wide tnodes */
  104. for (i = 0; i < n_tnodes - 1; i++) {
  105. curr = (struct yaffs_tnode *)&mem[i * dev->tnode_size];
  106. next = (struct yaffs_tnode *)&mem[(i + 1) * dev->tnode_size];
  107. curr->internal[0] = next;
  108. }
  109. curr = (struct yaffs_tnode *)&mem[(n_tnodes - 1) * dev->tnode_size];
  110. curr->internal[0] = allocator->free_tnodes;
  111. allocator->free_tnodes = (struct yaffs_tnode *)mem;
  112. allocator->n_free_tnodes += n_tnodes;
  113. allocator->n_tnodes_created += n_tnodes;
  114. /* Now add this bunch of tnodes to a list for freeing up.
  115. * NB If we can't add this to the management list it isn't fatal
  116. * but it just means we can't free this bunch of tnodes later.
  117. */
  118. tnl = kmalloc(sizeof(struct yaffs_tnode_list), GFP_NOFS);
  119. if (!tnl) {
  120. yaffs_trace(YAFFS_TRACE_ERROR,
  121. "Could not add tnodes to management list");
  122. return YAFFS_FAIL;
  123. } else {
  124. tnl->tnodes = new_tnodes;
  125. tnl->next = allocator->alloc_tnode_list;
  126. allocator->alloc_tnode_list = tnl;
  127. }
  128. yaffs_trace(YAFFS_TRACE_ALLOCATE, "Tnodes added");
  129. return YAFFS_OK;
  130. }
  131. struct yaffs_tnode *yaffs_alloc_raw_tnode(struct yaffs_dev *dev)
  132. {
  133. struct yaffs_allocator *allocator =
  134. (struct yaffs_allocator *)dev->allocator;
  135. struct yaffs_tnode *tn = NULL;
  136. if (!allocator) {
  137. BUG();
  138. return NULL;
  139. }
  140. /* If there are none left make more */
  141. if (!allocator->free_tnodes)
  142. yaffs_create_tnodes(dev, YAFFS_ALLOCATION_NTNODES);
  143. if (allocator->free_tnodes) {
  144. tn = allocator->free_tnodes;
  145. allocator->free_tnodes = allocator->free_tnodes->internal[0];
  146. allocator->n_free_tnodes--;
  147. }
  148. return tn;
  149. }
  150. /* FreeTnode frees up a tnode and puts it back on the free list */
  151. void yaffs_free_raw_tnode(struct yaffs_dev *dev, struct yaffs_tnode *tn)
  152. {
  153. struct yaffs_allocator *allocator = dev->allocator;
  154. if (!allocator) {
  155. BUG();
  156. return;
  157. }
  158. if (tn) {
  159. tn->internal[0] = allocator->free_tnodes;
  160. allocator->free_tnodes = tn;
  161. allocator->n_free_tnodes++;
  162. }
  163. dev->checkpoint_blocks_required = 0; /* force recalculation */
  164. }
  165. /*--------------- yaffs_obj alloaction ------------------------
  166. *
  167. * Free yaffs_objs are stored in a list using obj->siblings.
  168. * The blocks of allocated objects are stored in a linked list.
  169. */
  170. static void yaffs_init_raw_objs(struct yaffs_dev *dev)
  171. {
  172. struct yaffs_allocator *allocator = dev->allocator;
  173. if (!allocator) {
  174. BUG();
  175. return;
  176. }
  177. allocator->allocated_obj_list = NULL;
  178. INIT_LIST_HEAD(&allocator->free_objs);
  179. allocator->n_free_objects = 0;
  180. }
  181. static void yaffs_deinit_raw_objs(struct yaffs_dev *dev)
  182. {
  183. struct yaffs_allocator *allocator = dev->allocator;
  184. struct yaffs_obj_list *tmp;
  185. if (!allocator) {
  186. BUG();
  187. return;
  188. }
  189. while (allocator->allocated_obj_list) {
  190. tmp = allocator->allocated_obj_list->next;
  191. kfree(allocator->allocated_obj_list->objects);
  192. kfree(allocator->allocated_obj_list);
  193. allocator->allocated_obj_list = tmp;
  194. }
  195. INIT_LIST_HEAD(&allocator->free_objs);
  196. allocator->n_free_objects = 0;
  197. allocator->n_obj_created = 0;
  198. }
  199. static int yaffs_create_free_objs(struct yaffs_dev *dev, int n_obj)
  200. {
  201. struct yaffs_allocator *allocator = dev->allocator;
  202. int i;
  203. struct yaffs_obj *new_objs;
  204. struct yaffs_obj_list *list;
  205. if (!allocator) {
  206. BUG();
  207. return YAFFS_FAIL;
  208. }
  209. if (n_obj < 1)
  210. return YAFFS_OK;
  211. /* make these things */
  212. new_objs = kmalloc(n_obj * sizeof(struct yaffs_obj), GFP_NOFS);
  213. list = kmalloc(sizeof(struct yaffs_obj_list), GFP_NOFS);
  214. if (!new_objs || !list) {
  215. kfree(new_objs);
  216. new_objs = NULL;
  217. kfree(list);
  218. list = NULL;
  219. yaffs_trace(YAFFS_TRACE_ALLOCATE,
  220. "Could not allocate more objects");
  221. return YAFFS_FAIL;
  222. }
  223. /* Hook them into the free list */
  224. for (i = 0; i < n_obj; i++)
  225. list_add(&new_objs[i].siblings, &allocator->free_objs);
  226. allocator->n_free_objects += n_obj;
  227. allocator->n_obj_created += n_obj;
  228. /* Now add this bunch of Objects to a list for freeing up. */
  229. list->objects = new_objs;
  230. list->next = allocator->allocated_obj_list;
  231. allocator->allocated_obj_list = list;
  232. return YAFFS_OK;
  233. }
  234. struct yaffs_obj *yaffs_alloc_raw_obj(struct yaffs_dev *dev)
  235. {
  236. struct yaffs_obj *obj = NULL;
  237. struct list_head *lh;
  238. struct yaffs_allocator *allocator = dev->allocator;
  239. if (!allocator) {
  240. BUG();
  241. return obj;
  242. }
  243. /* If there are none left make more */
  244. if (list_empty(&allocator->free_objs))
  245. yaffs_create_free_objs(dev, YAFFS_ALLOCATION_NOBJECTS);
  246. if (!list_empty(&allocator->free_objs)) {
  247. lh = allocator->free_objs.next;
  248. obj = list_entry(lh, struct yaffs_obj, siblings);
  249. list_del_init(lh);
  250. allocator->n_free_objects--;
  251. }
  252. return obj;
  253. }
  254. void yaffs_free_raw_obj(struct yaffs_dev *dev, struct yaffs_obj *obj)
  255. {
  256. struct yaffs_allocator *allocator = dev->allocator;
  257. if (!allocator) {
  258. BUG();
  259. return;
  260. }
  261. /* Link into the free list. */
  262. list_add(&obj->siblings, &allocator->free_objs);
  263. allocator->n_free_objects++;
  264. }
  265. void yaffs_deinit_raw_tnodes_and_objs(struct yaffs_dev *dev)
  266. {
  267. if (!dev->allocator) {
  268. BUG();
  269. return;
  270. }
  271. yaffs_deinit_raw_tnodes(dev);
  272. yaffs_deinit_raw_objs(dev);
  273. kfree(dev->allocator);
  274. dev->allocator = NULL;
  275. }
  276. void yaffs_init_raw_tnodes_and_objs(struct yaffs_dev *dev)
  277. {
  278. struct yaffs_allocator *allocator;
  279. if (dev->allocator) {
  280. BUG();
  281. return;
  282. }
  283. allocator = kmalloc(sizeof(struct yaffs_allocator), GFP_NOFS);
  284. if (allocator) {
  285. dev->allocator = allocator;
  286. yaffs_init_raw_tnodes(dev);
  287. yaffs_init_raw_objs(dev);
  288. }
  289. }