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