jffs_fm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. /*
  2. * JFFS -- Journaling Flash File System, Linux implementation.
  3. *
  4. * Copyright (C) 1999, 2000 Axis Communications AB.
  5. *
  6. * Created by Finn Hakansson <finn@axis.com>.
  7. *
  8. * This is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * $Id: jffs_fm.c,v 1.27 2001/09/20 12:29:47 dwmw2 Exp $
  14. *
  15. * Ported to Linux 2.3.x and MTD:
  16. * Copyright (C) 2000 Alexander Larsson (alex@cendio.se), Cendio Systems AB
  17. *
  18. */
  19. #include <linux/slab.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/jffs.h>
  22. #include "jffs_fm.h"
  23. #include "intrep.h"
  24. #if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
  25. static int jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset);
  26. #endif
  27. static struct jffs_fm *jffs_alloc_fm(void);
  28. static void jffs_free_fm(struct jffs_fm *n);
  29. extern kmem_cache_t *fm_cache;
  30. extern kmem_cache_t *node_cache;
  31. #if CONFIG_JFFS_FS_VERBOSE > 0
  32. void
  33. jffs_print_fmcontrol(struct jffs_fmcontrol *fmc)
  34. {
  35. D(printk("struct jffs_fmcontrol: 0x%p\n", fmc));
  36. D(printk("{\n"));
  37. D(printk(" %u, /* flash_size */\n", fmc->flash_size));
  38. D(printk(" %u, /* used_size */\n", fmc->used_size));
  39. D(printk(" %u, /* dirty_size */\n", fmc->dirty_size));
  40. D(printk(" %u, /* free_size */\n", fmc->free_size));
  41. D(printk(" %u, /* sector_size */\n", fmc->sector_size));
  42. D(printk(" %u, /* min_free_size */\n", fmc->min_free_size));
  43. D(printk(" %u, /* max_chunk_size */\n", fmc->max_chunk_size));
  44. D(printk(" 0x%p, /* mtd */\n", fmc->mtd));
  45. D(printk(" 0x%p, /* head */ "
  46. "(head->offset = 0x%08x)\n",
  47. fmc->head, (fmc->head ? fmc->head->offset : 0)));
  48. D(printk(" 0x%p, /* tail */ "
  49. "(tail->offset + tail->size = 0x%08x)\n",
  50. fmc->tail,
  51. (fmc->tail ? fmc->tail->offset + fmc->tail->size : 0)));
  52. D(printk(" 0x%p, /* head_extra */\n", fmc->head_extra));
  53. D(printk(" 0x%p, /* tail_extra */\n", fmc->tail_extra));
  54. D(printk("}\n"));
  55. }
  56. #endif /* CONFIG_JFFS_FS_VERBOSE > 0 */
  57. #if CONFIG_JFFS_FS_VERBOSE > 2
  58. static void
  59. jffs_print_fm(struct jffs_fm *fm)
  60. {
  61. D(printk("struct jffs_fm: 0x%p\n", fm));
  62. D(printk("{\n"));
  63. D(printk(" 0x%08x, /* offset */\n", fm->offset));
  64. D(printk(" %u, /* size */\n", fm->size));
  65. D(printk(" 0x%p, /* prev */\n", fm->prev));
  66. D(printk(" 0x%p, /* next */\n", fm->next));
  67. D(printk(" 0x%p, /* nodes */\n", fm->nodes));
  68. D(printk("}\n"));
  69. }
  70. #endif /* CONFIG_JFFS_FS_VERBOSE > 2 */
  71. #if 0
  72. void
  73. jffs_print_node_ref(struct jffs_node_ref *ref)
  74. {
  75. D(printk("struct jffs_node_ref: 0x%p\n", ref));
  76. D(printk("{\n"));
  77. D(printk(" 0x%p, /* node */\n", ref->node));
  78. D(printk(" 0x%p, /* next */\n", ref->next));
  79. D(printk("}\n"));
  80. }
  81. #endif /* 0 */
  82. /* This function creates a new shiny flash memory control structure. */
  83. struct jffs_fmcontrol *
  84. jffs_build_begin(struct jffs_control *c, int unit)
  85. {
  86. struct jffs_fmcontrol *fmc;
  87. struct mtd_info *mtd;
  88. D3(printk("jffs_build_begin()\n"));
  89. fmc = (struct jffs_fmcontrol *)kmalloc(sizeof(struct jffs_fmcontrol),
  90. GFP_KERNEL);
  91. if (!fmc) {
  92. D(printk("jffs_build_begin(): Allocation of "
  93. "struct jffs_fmcontrol failed!\n"));
  94. return (struct jffs_fmcontrol *)0;
  95. }
  96. DJM(no_jffs_fmcontrol++);
  97. mtd = get_mtd_device(NULL, unit);
  98. if (!mtd) {
  99. kfree(fmc);
  100. DJM(no_jffs_fmcontrol--);
  101. return NULL;
  102. }
  103. /* Retrieve the size of the flash memory. */
  104. fmc->flash_size = mtd->size;
  105. D3(printk(" fmc->flash_size = %d bytes\n", fmc->flash_size));
  106. fmc->used_size = 0;
  107. fmc->dirty_size = 0;
  108. fmc->free_size = mtd->size;
  109. fmc->sector_size = mtd->erasesize;
  110. fmc->max_chunk_size = fmc->sector_size >> 1;
  111. /* min_free_size:
  112. 1 sector, obviously.
  113. + 1 x max_chunk_size, for when a nodes overlaps the end of a sector
  114. + 1 x max_chunk_size again, which ought to be enough to handle
  115. the case where a rename causes a name to grow, and GC has
  116. to write out larger nodes than the ones it's obsoleting.
  117. We should fix it so it doesn't have to write the name
  118. _every_ time. Later.
  119. + another 2 sectors because people keep getting GC stuck and
  120. we don't know why. This scares me - I want formal proof
  121. of correctness of whatever number we put here. dwmw2.
  122. */
  123. fmc->min_free_size = fmc->sector_size << 2;
  124. fmc->mtd = mtd;
  125. fmc->c = c;
  126. fmc->head = NULL;
  127. fmc->tail = NULL;
  128. fmc->head_extra = NULL;
  129. fmc->tail_extra = NULL;
  130. mutex_init(&fmc->biglock);
  131. return fmc;
  132. }
  133. /* When the flash memory scan has completed, this function should be called
  134. before use of the control structure. */
  135. void
  136. jffs_build_end(struct jffs_fmcontrol *fmc)
  137. {
  138. D3(printk("jffs_build_end()\n"));
  139. if (!fmc->head) {
  140. fmc->head = fmc->head_extra;
  141. fmc->tail = fmc->tail_extra;
  142. }
  143. else if (fmc->head_extra) {
  144. fmc->tail_extra->next = fmc->head;
  145. fmc->head->prev = fmc->tail_extra;
  146. fmc->head = fmc->head_extra;
  147. }
  148. fmc->head_extra = NULL; /* These two instructions should be omitted. */
  149. fmc->tail_extra = NULL;
  150. D3(jffs_print_fmcontrol(fmc));
  151. }
  152. /* Call this function when the file system is unmounted. This function
  153. frees all memory used by this module. */
  154. void
  155. jffs_cleanup_fmcontrol(struct jffs_fmcontrol *fmc)
  156. {
  157. if (fmc) {
  158. struct jffs_fm *next = fmc->head;
  159. while (next) {
  160. struct jffs_fm *cur = next;
  161. next = next->next;
  162. jffs_free_fm(cur);
  163. }
  164. put_mtd_device(fmc->mtd);
  165. kfree(fmc);
  166. DJM(no_jffs_fmcontrol--);
  167. }
  168. }
  169. /* This function returns the size of the first chunk of free space on the
  170. flash memory. This function will return something nonzero if the flash
  171. memory contains any free space. */
  172. __u32
  173. jffs_free_size1(struct jffs_fmcontrol *fmc)
  174. {
  175. __u32 head;
  176. __u32 tail;
  177. __u32 end = fmc->flash_size;
  178. if (!fmc->head) {
  179. /* There is nothing on the flash. */
  180. return fmc->flash_size;
  181. }
  182. /* Compute the beginning and ending of the contents of the flash. */
  183. head = fmc->head->offset;
  184. tail = fmc->tail->offset + fmc->tail->size;
  185. if (tail == end) {
  186. tail = 0;
  187. }
  188. ASSERT(else if (tail > end) {
  189. printk(KERN_WARNING "jffs_free_size1(): tail > end\n");
  190. tail = 0;
  191. });
  192. if (head <= tail) {
  193. return end - tail;
  194. }
  195. else {
  196. return head - tail;
  197. }
  198. }
  199. /* This function will return something nonzero in case there are two free
  200. areas on the flash. Like this:
  201. +----------------+------------------+----------------+
  202. | FREE 1 | USED / DIRTY | FREE 2 |
  203. +----------------+------------------+----------------+
  204. fmc->head -----^
  205. fmc->tail ------------------------^
  206. The value returned, will be the size of the first empty area on the
  207. flash, in this case marked "FREE 1". */
  208. __u32
  209. jffs_free_size2(struct jffs_fmcontrol *fmc)
  210. {
  211. if (fmc->head) {
  212. __u32 head = fmc->head->offset;
  213. __u32 tail = fmc->tail->offset + fmc->tail->size;
  214. if (tail == fmc->flash_size) {
  215. tail = 0;
  216. }
  217. if (tail >= head) {
  218. return head;
  219. }
  220. }
  221. return 0;
  222. }
  223. /* Allocate a chunk of flash memory. If there is enough space on the
  224. device, a reference to the associated node is stored in the jffs_fm
  225. struct. */
  226. int
  227. jffs_fmalloc(struct jffs_fmcontrol *fmc, __u32 size, struct jffs_node *node,
  228. struct jffs_fm **result)
  229. {
  230. struct jffs_fm *fm;
  231. __u32 free_chunk_size1;
  232. __u32 free_chunk_size2;
  233. D2(printk("jffs_fmalloc(): fmc = 0x%p, size = %d, "
  234. "node = 0x%p\n", fmc, size, node));
  235. *result = NULL;
  236. if (!(fm = jffs_alloc_fm())) {
  237. D(printk("jffs_fmalloc(): kmalloc() failed! (fm)\n"));
  238. return -ENOMEM;
  239. }
  240. free_chunk_size1 = jffs_free_size1(fmc);
  241. free_chunk_size2 = jffs_free_size2(fmc);
  242. if (free_chunk_size1 + free_chunk_size2 != fmc->free_size) {
  243. printk(KERN_WARNING "Free size accounting screwed\n");
  244. printk(KERN_WARNING "free_chunk_size1 == 0x%x, free_chunk_size2 == 0x%x, fmc->free_size == 0x%x\n", free_chunk_size1, free_chunk_size2, fmc->free_size);
  245. }
  246. D3(printk("jffs_fmalloc(): free_chunk_size1 = %u, "
  247. "free_chunk_size2 = %u\n",
  248. free_chunk_size1, free_chunk_size2));
  249. if (size <= free_chunk_size1) {
  250. if (!(fm->nodes = (struct jffs_node_ref *)
  251. kmalloc(sizeof(struct jffs_node_ref),
  252. GFP_KERNEL))) {
  253. D(printk("jffs_fmalloc(): kmalloc() failed! "
  254. "(node_ref)\n"));
  255. jffs_free_fm(fm);
  256. return -ENOMEM;
  257. }
  258. DJM(no_jffs_node_ref++);
  259. fm->nodes->node = node;
  260. fm->nodes->next = NULL;
  261. if (fmc->tail) {
  262. fm->offset = fmc->tail->offset + fmc->tail->size;
  263. if (fm->offset == fmc->flash_size) {
  264. fm->offset = 0;
  265. }
  266. ASSERT(else if (fm->offset > fmc->flash_size) {
  267. printk(KERN_WARNING "jffs_fmalloc(): "
  268. "offset > flash_end\n");
  269. fm->offset = 0;
  270. });
  271. }
  272. else {
  273. /* There don't have to be files in the file
  274. system yet. */
  275. fm->offset = 0;
  276. }
  277. fm->size = size;
  278. fmc->free_size -= size;
  279. fmc->used_size += size;
  280. }
  281. else if (size > free_chunk_size2) {
  282. printk(KERN_WARNING "JFFS: Tried to allocate a too "
  283. "large flash memory chunk. (size = %u)\n", size);
  284. jffs_free_fm(fm);
  285. return -ENOSPC;
  286. }
  287. else {
  288. fm->offset = fmc->tail->offset + fmc->tail->size;
  289. fm->size = free_chunk_size1;
  290. fm->nodes = NULL;
  291. fmc->free_size -= fm->size;
  292. fmc->dirty_size += fm->size; /* Changed by simonk. This seemingly fixes a
  293. bug that caused infinite garbage collection.
  294. It previously set fmc->dirty_size to size (which is the
  295. size of the requested chunk).
  296. */
  297. }
  298. fm->next = NULL;
  299. if (!fmc->head) {
  300. fm->prev = NULL;
  301. fmc->head = fm;
  302. fmc->tail = fm;
  303. }
  304. else {
  305. fm->prev = fmc->tail;
  306. fmc->tail->next = fm;
  307. fmc->tail = fm;
  308. }
  309. D3(jffs_print_fmcontrol(fmc));
  310. D3(jffs_print_fm(fm));
  311. *result = fm;
  312. return 0;
  313. }
  314. /* The on-flash space is not needed anymore by the passed node. Remove
  315. the reference to the node from the node list. If the data chunk in
  316. the flash memory isn't used by any more nodes anymore (fm->nodes == 0),
  317. then mark that chunk as dirty. */
  318. int
  319. jffs_fmfree(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, struct jffs_node *node)
  320. {
  321. struct jffs_node_ref *ref;
  322. struct jffs_node_ref *prev;
  323. ASSERT(int del = 0);
  324. D2(printk("jffs_fmfree(): node->ino = %u, node->version = %u\n",
  325. node->ino, node->version));
  326. ASSERT(if (!fmc || !fm || !fm->nodes) {
  327. printk(KERN_ERR "jffs_fmfree(): fmc: 0x%p, fm: 0x%p, "
  328. "fm->nodes: 0x%p\n",
  329. fmc, fm, (fm ? fm->nodes : NULL));
  330. return -1;
  331. });
  332. /* Find the reference to the node that is going to be removed
  333. and remove it. */
  334. for (ref = fm->nodes, prev = NULL; ref; ref = ref->next) {
  335. if (ref->node == node) {
  336. if (prev) {
  337. prev->next = ref->next;
  338. }
  339. else {
  340. fm->nodes = ref->next;
  341. }
  342. kfree(ref);
  343. DJM(no_jffs_node_ref--);
  344. ASSERT(del = 1);
  345. break;
  346. }
  347. prev = ref;
  348. }
  349. /* If the data chunk in the flash memory isn't used anymore
  350. just mark it as obsolete. */
  351. if (!fm->nodes) {
  352. /* No node uses this chunk so let's remove it. */
  353. fmc->used_size -= fm->size;
  354. fmc->dirty_size += fm->size;
  355. #if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
  356. if (jffs_mark_obsolete(fmc, fm->offset) < 0) {
  357. D1(printk("jffs_fmfree(): Failed to mark an on-flash "
  358. "node obsolete!\n"));
  359. return -1;
  360. }
  361. #endif
  362. }
  363. ASSERT(if (!del) {
  364. printk(KERN_WARNING "***jffs_fmfree(): "
  365. "Didn't delete any node reference!\n");
  366. });
  367. return 0;
  368. }
  369. /* This allocation function is used during the initialization of
  370. the file system. */
  371. struct jffs_fm *
  372. jffs_fmalloced(struct jffs_fmcontrol *fmc, __u32 offset, __u32 size,
  373. struct jffs_node *node)
  374. {
  375. struct jffs_fm *fm;
  376. D3(printk("jffs_fmalloced()\n"));
  377. if (!(fm = jffs_alloc_fm())) {
  378. D(printk("jffs_fmalloced(0x%p, %u, %u, 0x%p): failed!\n",
  379. fmc, offset, size, node));
  380. return NULL;
  381. }
  382. fm->offset = offset;
  383. fm->size = size;
  384. fm->prev = NULL;
  385. fm->next = NULL;
  386. fm->nodes = NULL;
  387. if (node) {
  388. /* `node' exists and it should be associated with the
  389. jffs_fm structure `fm'. */
  390. if (!(fm->nodes = (struct jffs_node_ref *)
  391. kmalloc(sizeof(struct jffs_node_ref),
  392. GFP_KERNEL))) {
  393. D(printk("jffs_fmalloced(): !fm->nodes\n"));
  394. jffs_free_fm(fm);
  395. return NULL;
  396. }
  397. DJM(no_jffs_node_ref++);
  398. fm->nodes->node = node;
  399. fm->nodes->next = NULL;
  400. fmc->used_size += size;
  401. fmc->free_size -= size;
  402. }
  403. else {
  404. /* If there is no node, then this is just a chunk of dirt. */
  405. fmc->dirty_size += size;
  406. fmc->free_size -= size;
  407. }
  408. if (fmc->head_extra) {
  409. fm->prev = fmc->tail_extra;
  410. fmc->tail_extra->next = fm;
  411. fmc->tail_extra = fm;
  412. }
  413. else if (!fmc->head) {
  414. fmc->head = fm;
  415. fmc->tail = fm;
  416. }
  417. else if (fmc->tail->offset + fmc->tail->size < offset) {
  418. fmc->head_extra = fm;
  419. fmc->tail_extra = fm;
  420. }
  421. else {
  422. fm->prev = fmc->tail;
  423. fmc->tail->next = fm;
  424. fmc->tail = fm;
  425. }
  426. D3(jffs_print_fmcontrol(fmc));
  427. D3(jffs_print_fm(fm));
  428. return fm;
  429. }
  430. /* Add a new node to an already existing jffs_fm struct. */
  431. int
  432. jffs_add_node(struct jffs_node *node)
  433. {
  434. struct jffs_node_ref *ref;
  435. D3(printk("jffs_add_node(): ino = %u\n", node->ino));
  436. ref = (struct jffs_node_ref *)kmalloc(sizeof(struct jffs_node_ref),
  437. GFP_KERNEL);
  438. if (!ref)
  439. return -ENOMEM;
  440. DJM(no_jffs_node_ref++);
  441. ref->node = node;
  442. ref->next = node->fm->nodes;
  443. node->fm->nodes = ref;
  444. return 0;
  445. }
  446. /* Free a part of some allocated space. */
  447. void
  448. jffs_fmfree_partly(struct jffs_fmcontrol *fmc, struct jffs_fm *fm, __u32 size)
  449. {
  450. D1(printk("***jffs_fmfree_partly(): fm = 0x%p, fm->nodes = 0x%p, "
  451. "fm->nodes->node->ino = %u, size = %u\n",
  452. fm, (fm ? fm->nodes : 0),
  453. (!fm ? 0 : (!fm->nodes ? 0 : fm->nodes->node->ino)), size));
  454. if (fm->nodes) {
  455. kfree(fm->nodes);
  456. DJM(no_jffs_node_ref--);
  457. fm->nodes = NULL;
  458. }
  459. fmc->used_size -= fm->size;
  460. if (fm == fmc->tail) {
  461. fm->size -= size;
  462. fmc->free_size += size;
  463. }
  464. fmc->dirty_size += fm->size;
  465. }
  466. /* Find the jffs_fm struct that contains the end of the data chunk that
  467. begins at the logical beginning of the flash memory and spans `size'
  468. bytes. If we want to erase a sector of the flash memory, we use this
  469. function to find where the sector limit cuts a chunk of data. */
  470. struct jffs_fm *
  471. jffs_cut_node(struct jffs_fmcontrol *fmc, __u32 size)
  472. {
  473. struct jffs_fm *fm;
  474. __u32 pos = 0;
  475. if (size == 0) {
  476. return NULL;
  477. }
  478. ASSERT(if (!fmc) {
  479. printk(KERN_ERR "jffs_cut_node(): fmc == NULL\n");
  480. return NULL;
  481. });
  482. fm = fmc->head;
  483. while (fm) {
  484. pos += fm->size;
  485. if (pos < size) {
  486. fm = fm->next;
  487. }
  488. else if (pos > size) {
  489. break;
  490. }
  491. else {
  492. fm = NULL;
  493. break;
  494. }
  495. }
  496. return fm;
  497. }
  498. /* Move the head of the fmc structures and delete the obsolete parts. */
  499. void
  500. jffs_sync_erase(struct jffs_fmcontrol *fmc, int erased_size)
  501. {
  502. struct jffs_fm *fm;
  503. struct jffs_fm *del;
  504. ASSERT(if (!fmc) {
  505. printk(KERN_ERR "jffs_sync_erase(): fmc == NULL\n");
  506. return;
  507. });
  508. fmc->dirty_size -= erased_size;
  509. fmc->free_size += erased_size;
  510. for (fm = fmc->head; fm && (erased_size > 0);) {
  511. if (erased_size >= fm->size) {
  512. erased_size -= fm->size;
  513. del = fm;
  514. fm = fm->next;
  515. fm->prev = NULL;
  516. fmc->head = fm;
  517. jffs_free_fm(del);
  518. }
  519. else {
  520. fm->size -= erased_size;
  521. fm->offset += erased_size;
  522. break;
  523. }
  524. }
  525. }
  526. /* Return the oldest used node in the flash memory. */
  527. struct jffs_node *
  528. jffs_get_oldest_node(struct jffs_fmcontrol *fmc)
  529. {
  530. struct jffs_fm *fm;
  531. struct jffs_node_ref *nref;
  532. struct jffs_node *node = NULL;
  533. ASSERT(if (!fmc) {
  534. printk(KERN_ERR "jffs_get_oldest_node(): fmc == NULL\n");
  535. return NULL;
  536. });
  537. for (fm = fmc->head; fm && !fm->nodes; fm = fm->next);
  538. if (!fm) {
  539. return NULL;
  540. }
  541. /* The oldest node is the last one in the reference list. This list
  542. shouldn't be too long; just one or perhaps two elements. */
  543. for (nref = fm->nodes; nref; nref = nref->next) {
  544. node = nref->node;
  545. }
  546. D2(printk("jffs_get_oldest_node(): ino = %u, version = %u\n",
  547. (node ? node->ino : 0), (node ? node->version : 0)));
  548. return node;
  549. }
  550. #if defined(JFFS_MARK_OBSOLETE) && JFFS_MARK_OBSOLETE
  551. /* Mark an on-flash node as obsolete.
  552. Note that this is just an optimization that isn't necessary for the
  553. filesystem to work. */
  554. static int
  555. jffs_mark_obsolete(struct jffs_fmcontrol *fmc, __u32 fm_offset)
  556. {
  557. /* The `accurate_pos' holds the position of the accurate byte
  558. in the jffs_raw_inode structure that we are going to mark
  559. as obsolete. */
  560. __u32 accurate_pos = fm_offset + JFFS_RAW_INODE_ACCURATE_OFFSET;
  561. unsigned char zero = 0x00;
  562. size_t len;
  563. D3(printk("jffs_mark_obsolete(): accurate_pos = %u\n", accurate_pos));
  564. ASSERT(if (!fmc) {
  565. printk(KERN_ERR "jffs_mark_obsolete(): fmc == NULL\n");
  566. return -1;
  567. });
  568. /* Write 0x00 to the raw inode's accurate member. Don't care
  569. about the return value. */
  570. MTD_WRITE(fmc->mtd, accurate_pos, 1, &len, &zero);
  571. return 0;
  572. }
  573. #endif /* JFFS_MARK_OBSOLETE */
  574. /* check if it's possible to erase the wanted range, and if not, return
  575. * the range that IS erasable, or a negative error code.
  576. */
  577. static long
  578. jffs_flash_erasable_size(struct mtd_info *mtd, __u32 offset, __u32 size)
  579. {
  580. u_long ssize;
  581. /* assume that sector size for a partition is constant even
  582. * if it spans more than one chip (you usually put the same
  583. * type of chips in a system)
  584. */
  585. ssize = mtd->erasesize;
  586. if (offset % ssize) {
  587. printk(KERN_WARNING "jffs_flash_erasable_size() given non-aligned offset %x (erasesize %lx)\n", offset, ssize);
  588. /* The offset is not sector size aligned. */
  589. return -1;
  590. }
  591. else if (offset > mtd->size) {
  592. printk(KERN_WARNING "jffs_flash_erasable_size given offset off the end of device (%x > %x)\n", offset, mtd->size);
  593. return -2;
  594. }
  595. else if (offset + size > mtd->size) {
  596. printk(KERN_WARNING "jffs_flash_erasable_size() given length which runs off the end of device (ofs %x + len %x = %x, > %x)\n", offset,size, offset+size, mtd->size);
  597. return -3;
  598. }
  599. return (size / ssize) * ssize;
  600. }
  601. /* How much dirty flash memory is possible to erase at the moment? */
  602. long
  603. jffs_erasable_size(struct jffs_fmcontrol *fmc)
  604. {
  605. struct jffs_fm *fm;
  606. __u32 size = 0;
  607. long ret;
  608. ASSERT(if (!fmc) {
  609. printk(KERN_ERR "jffs_erasable_size(): fmc = NULL\n");
  610. return -1;
  611. });
  612. if (!fmc->head) {
  613. /* The flash memory is totally empty. No nodes. No dirt.
  614. Just return. */
  615. return 0;
  616. }
  617. /* Calculate how much space that is dirty. */
  618. for (fm = fmc->head; fm && !fm->nodes; fm = fm->next) {
  619. if (size && fm->offset == 0) {
  620. /* We have reached the beginning of the flash. */
  621. break;
  622. }
  623. size += fm->size;
  624. }
  625. /* Someone's signature contained this:
  626. There's a fine line between fishing and just standing on
  627. the shore like an idiot... */
  628. ret = jffs_flash_erasable_size(fmc->mtd, fmc->head->offset, size);
  629. ASSERT(if (ret < 0) {
  630. printk("jffs_erasable_size: flash_erasable_size() "
  631. "returned something less than zero (%ld).\n", ret);
  632. printk("jffs_erasable_size: offset = 0x%08x\n",
  633. fmc->head->offset);
  634. });
  635. /* If there is dirt on the flash (which is the reason to why
  636. this function was called in the first place) but no space is
  637. possible to erase right now, the initial part of the list of
  638. jffs_fm structs, that hold place for dirty space, could perhaps
  639. be shortened. The list's initial "dirty" elements are merged
  640. into just one large dirty jffs_fm struct. This operation must
  641. only be performed if nothing is possible to erase. Otherwise,
  642. jffs_clear_end_of_node() won't work as expected. */
  643. if (ret == 0) {
  644. struct jffs_fm *head = fmc->head;
  645. struct jffs_fm *del;
  646. /* While there are two dirty nodes beside each other.*/
  647. while (head->nodes == 0
  648. && head->next
  649. && head->next->nodes == 0) {
  650. del = head->next;
  651. head->size += del->size;
  652. head->next = del->next;
  653. if (del->next) {
  654. del->next->prev = head;
  655. }
  656. jffs_free_fm(del);
  657. }
  658. }
  659. return (ret >= 0 ? ret : 0);
  660. }
  661. static struct jffs_fm *jffs_alloc_fm(void)
  662. {
  663. struct jffs_fm *fm;
  664. fm = kmem_cache_alloc(fm_cache,GFP_KERNEL);
  665. DJM(if (fm) no_jffs_fm++;);
  666. return fm;
  667. }
  668. static void jffs_free_fm(struct jffs_fm *n)
  669. {
  670. kmem_cache_free(fm_cache,n);
  671. DJM(no_jffs_fm--);
  672. }
  673. struct jffs_node *jffs_alloc_node(void)
  674. {
  675. struct jffs_node *n;
  676. n = (struct jffs_node *)kmem_cache_alloc(node_cache,GFP_KERNEL);
  677. if(n != NULL)
  678. no_jffs_node++;
  679. return n;
  680. }
  681. void jffs_free_node(struct jffs_node *n)
  682. {
  683. kmem_cache_free(node_cache,n);
  684. no_jffs_node--;
  685. }
  686. int jffs_get_node_inuse(void)
  687. {
  688. return no_jffs_node;
  689. }