yaffs_yaffs2.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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_guts.h"
  14. #include "yaffs_trace.h"
  15. #include "yaffs_yaffs2.h"
  16. #include "yaffs_checkptrw.h"
  17. #include "yaffs_bitmap.h"
  18. #include "yaffs_nand.h"
  19. #include "yaffs_getblockinfo.h"
  20. #include "yaffs_verify.h"
  21. #include "yaffs_attribs.h"
  22. #include "yaffs_summary.h"
  23. /*
  24. * Checkpoints are really no benefit on very small partitions.
  25. *
  26. * To save space on small partitions don't bother with checkpoints unless
  27. * the partition is at least this big.
  28. */
  29. #define YAFFS_CHECKPOINT_MIN_BLOCKS 60
  30. #define YAFFS_SMALL_HOLE_THRESHOLD 4
  31. /*
  32. * Oldest Dirty Sequence Number handling.
  33. */
  34. /* yaffs_calc_oldest_dirty_seq()
  35. * yaffs2_find_oldest_dirty_seq()
  36. * Calculate the oldest dirty sequence number if we don't know it.
  37. */
  38. void yaffs_calc_oldest_dirty_seq(struct yaffs_dev *dev)
  39. {
  40. int i;
  41. unsigned seq;
  42. unsigned block_no = 0;
  43. struct yaffs_block_info *b;
  44. if (!dev->param.is_yaffs2)
  45. return;
  46. /* Find the oldest dirty sequence number. */
  47. seq = dev->seq_number + 1;
  48. b = dev->block_info;
  49. for (i = dev->internal_start_block; i <= dev->internal_end_block; i++) {
  50. if (b->block_state == YAFFS_BLOCK_STATE_FULL &&
  51. (b->pages_in_use - b->soft_del_pages) <
  52. dev->param.chunks_per_block &&
  53. b->seq_number < seq) {
  54. seq = b->seq_number;
  55. block_no = i;
  56. }
  57. b++;
  58. }
  59. if (block_no) {
  60. dev->oldest_dirty_seq = seq;
  61. dev->oldest_dirty_block = block_no;
  62. }
  63. }
  64. void yaffs2_find_oldest_dirty_seq(struct yaffs_dev *dev)
  65. {
  66. if (!dev->param.is_yaffs2)
  67. return;
  68. if (!dev->oldest_dirty_seq)
  69. yaffs_calc_oldest_dirty_seq(dev);
  70. }
  71. /*
  72. * yaffs_clear_oldest_dirty_seq()
  73. * Called when a block is erased or marked bad. (ie. when its seq_number
  74. * becomes invalid). If the value matches the oldest then we clear
  75. * dev->oldest_dirty_seq to force its recomputation.
  76. */
  77. void yaffs2_clear_oldest_dirty_seq(struct yaffs_dev *dev,
  78. struct yaffs_block_info *bi)
  79. {
  80. if (!dev->param.is_yaffs2)
  81. return;
  82. if (!bi || bi->seq_number == dev->oldest_dirty_seq) {
  83. dev->oldest_dirty_seq = 0;
  84. dev->oldest_dirty_block = 0;
  85. }
  86. }
  87. /*
  88. * yaffs2_update_oldest_dirty_seq()
  89. * Update the oldest dirty sequence number whenever we dirty a block.
  90. * Only do this if the oldest_dirty_seq is actually being tracked.
  91. */
  92. void yaffs2_update_oldest_dirty_seq(struct yaffs_dev *dev, unsigned block_no,
  93. struct yaffs_block_info *bi)
  94. {
  95. if (!dev->param.is_yaffs2)
  96. return;
  97. if (dev->oldest_dirty_seq) {
  98. if (dev->oldest_dirty_seq > bi->seq_number) {
  99. dev->oldest_dirty_seq = bi->seq_number;
  100. dev->oldest_dirty_block = block_no;
  101. }
  102. }
  103. }
  104. int yaffs_block_ok_for_gc(struct yaffs_dev *dev, struct yaffs_block_info *bi)
  105. {
  106. if (!dev->param.is_yaffs2)
  107. return 1; /* disqualification only applies to yaffs2. */
  108. if (!bi->has_shrink_hdr)
  109. return 1; /* can gc */
  110. yaffs2_find_oldest_dirty_seq(dev);
  111. /* Can't do gc of this block if there are any blocks older than this
  112. * one that have discarded pages.
  113. */
  114. return (bi->seq_number <= dev->oldest_dirty_seq);
  115. }
  116. /*
  117. * yaffs2_find_refresh_block()
  118. * periodically finds the oldest full block by sequence number for refreshing.
  119. * Only for yaffs2.
  120. */
  121. u32 yaffs2_find_refresh_block(struct yaffs_dev *dev)
  122. {
  123. u32 b;
  124. u32 oldest = 0;
  125. u32 oldest_seq = 0;
  126. struct yaffs_block_info *bi;
  127. if (!dev->param.is_yaffs2)
  128. return oldest;
  129. /*
  130. * If refresh period < 10 then refreshing is disabled.
  131. */
  132. if (dev->param.refresh_period < 10)
  133. return oldest;
  134. /*
  135. * Fix broken values.
  136. */
  137. if (dev->refresh_skip > dev->param.refresh_period)
  138. dev->refresh_skip = dev->param.refresh_period;
  139. if (dev->refresh_skip > 0)
  140. return oldest;
  141. /*
  142. * Refresh skip is now zero.
  143. * We'll do a refresh this time around....
  144. * Update the refresh skip and find the oldest block.
  145. */
  146. dev->refresh_skip = dev->param.refresh_period;
  147. dev->refresh_count++;
  148. bi = dev->block_info;
  149. for (b = dev->internal_start_block; b <= dev->internal_end_block; b++) {
  150. if (bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  151. if (oldest < 1 || bi->seq_number < oldest_seq) {
  152. oldest = b;
  153. oldest_seq = bi->seq_number;
  154. }
  155. }
  156. bi++;
  157. }
  158. if (oldest > 0) {
  159. yaffs_trace(YAFFS_TRACE_GC,
  160. "GC refresh count %d selected block %d with seq_number %d",
  161. dev->refresh_count, oldest, oldest_seq);
  162. }
  163. return oldest;
  164. }
  165. int yaffs2_checkpt_required(struct yaffs_dev *dev)
  166. {
  167. int nblocks;
  168. if (!dev->param.is_yaffs2)
  169. return 0;
  170. nblocks = dev->internal_end_block - dev->internal_start_block + 1;
  171. return !dev->param.skip_checkpt_wr &&
  172. !dev->read_only && (nblocks >= YAFFS_CHECKPOINT_MIN_BLOCKS);
  173. }
  174. int yaffs_calc_checkpt_blocks_required(struct yaffs_dev *dev)
  175. {
  176. int retval;
  177. int n_bytes = 0;
  178. int n_blocks;
  179. int dev_blocks;
  180. if (!dev->param.is_yaffs2)
  181. return 0;
  182. if (!dev->checkpoint_blocks_required && yaffs2_checkpt_required(dev)) {
  183. /* Not a valid value so recalculate */
  184. dev_blocks = dev->param.end_block - dev->param.start_block + 1;
  185. n_bytes += sizeof(struct yaffs_checkpt_validity);
  186. n_bytes += sizeof(struct yaffs_checkpt_dev);
  187. n_bytes += dev_blocks * sizeof(struct yaffs_block_info);
  188. n_bytes += dev_blocks * dev->chunk_bit_stride;
  189. n_bytes +=
  190. (sizeof(struct yaffs_checkpt_obj) + sizeof(u32)) *
  191. dev->n_obj;
  192. n_bytes += (dev->tnode_size + sizeof(u32)) * dev->n_tnodes;
  193. n_bytes += sizeof(struct yaffs_checkpt_validity);
  194. n_bytes += sizeof(u32); /* checksum */
  195. /* Round up and add 2 blocks to allow for some bad blocks,
  196. * so add 3 */
  197. n_blocks =
  198. (n_bytes /
  199. (dev->data_bytes_per_chunk *
  200. dev->param.chunks_per_block)) + 3;
  201. dev->checkpoint_blocks_required = n_blocks;
  202. }
  203. retval = dev->checkpoint_blocks_required - dev->blocks_in_checkpt;
  204. if (retval < 0)
  205. retval = 0;
  206. return retval;
  207. }
  208. /*--------------------- Checkpointing --------------------*/
  209. static int yaffs2_wr_checkpt_validity_marker(struct yaffs_dev *dev, int head)
  210. {
  211. struct yaffs_checkpt_validity cp;
  212. memset(&cp, 0, sizeof(cp));
  213. cp.struct_type = sizeof(cp);
  214. cp.magic = YAFFS_MAGIC;
  215. cp.version = YAFFS_CHECKPOINT_VERSION;
  216. cp.head = (head) ? 1 : 0;
  217. return (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp)) ? 1 : 0;
  218. }
  219. static int yaffs2_rd_checkpt_validity_marker(struct yaffs_dev *dev, int head)
  220. {
  221. struct yaffs_checkpt_validity cp;
  222. int ok;
  223. ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  224. if (ok)
  225. ok = (cp.struct_type == sizeof(cp)) &&
  226. (cp.magic == YAFFS_MAGIC) &&
  227. (cp.version == YAFFS_CHECKPOINT_VERSION) &&
  228. (cp.head == ((head) ? 1 : 0));
  229. return ok ? 1 : 0;
  230. }
  231. static void yaffs2_dev_to_checkpt_dev(struct yaffs_checkpt_dev *cp,
  232. struct yaffs_dev *dev)
  233. {
  234. cp->n_erased_blocks = dev->n_erased_blocks;
  235. cp->alloc_block = dev->alloc_block;
  236. cp->alloc_page = dev->alloc_page;
  237. cp->n_free_chunks = dev->n_free_chunks;
  238. cp->n_deleted_files = dev->n_deleted_files;
  239. cp->n_unlinked_files = dev->n_unlinked_files;
  240. cp->n_bg_deletions = dev->n_bg_deletions;
  241. cp->seq_number = dev->seq_number;
  242. }
  243. static void yaffs_checkpt_dev_to_dev(struct yaffs_dev *dev,
  244. struct yaffs_checkpt_dev *cp)
  245. {
  246. dev->n_erased_blocks = cp->n_erased_blocks;
  247. dev->alloc_block = cp->alloc_block;
  248. dev->alloc_page = cp->alloc_page;
  249. dev->n_free_chunks = cp->n_free_chunks;
  250. dev->n_deleted_files = cp->n_deleted_files;
  251. dev->n_unlinked_files = cp->n_unlinked_files;
  252. dev->n_bg_deletions = cp->n_bg_deletions;
  253. dev->seq_number = cp->seq_number;
  254. }
  255. static int yaffs2_wr_checkpt_dev(struct yaffs_dev *dev)
  256. {
  257. struct yaffs_checkpt_dev cp;
  258. u32 n_bytes;
  259. u32 n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  260. int ok;
  261. /* Write device runtime values */
  262. yaffs2_dev_to_checkpt_dev(&cp, dev);
  263. cp.struct_type = sizeof(cp);
  264. ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
  265. if (!ok)
  266. return 0;
  267. /* Write block info */
  268. n_bytes = n_blocks * sizeof(struct yaffs_block_info);
  269. ok = (yaffs2_checkpt_wr(dev, dev->block_info, n_bytes) == n_bytes);
  270. if (!ok)
  271. return 0;
  272. /* Write chunk bits */
  273. n_bytes = n_blocks * dev->chunk_bit_stride;
  274. ok = (yaffs2_checkpt_wr(dev, dev->chunk_bits, n_bytes) == n_bytes);
  275. return ok ? 1 : 0;
  276. }
  277. static int yaffs2_rd_checkpt_dev(struct yaffs_dev *dev)
  278. {
  279. struct yaffs_checkpt_dev cp;
  280. u32 n_bytes;
  281. u32 n_blocks =
  282. (dev->internal_end_block - dev->internal_start_block + 1);
  283. int ok;
  284. ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  285. if (!ok)
  286. return 0;
  287. if (cp.struct_type != sizeof(cp))
  288. return 0;
  289. yaffs_checkpt_dev_to_dev(dev, &cp);
  290. n_bytes = n_blocks * sizeof(struct yaffs_block_info);
  291. ok = (yaffs2_checkpt_rd(dev, dev->block_info, n_bytes) == n_bytes);
  292. if (!ok)
  293. return 0;
  294. n_bytes = n_blocks * dev->chunk_bit_stride;
  295. ok = (yaffs2_checkpt_rd(dev, dev->chunk_bits, n_bytes) == n_bytes);
  296. return ok ? 1 : 0;
  297. }
  298. static void yaffs2_obj_checkpt_obj(struct yaffs_checkpt_obj *cp,
  299. struct yaffs_obj *obj)
  300. {
  301. cp->obj_id = obj->obj_id;
  302. cp->parent_id = (obj->parent) ? obj->parent->obj_id : 0;
  303. cp->hdr_chunk = obj->hdr_chunk;
  304. cp->variant_type = obj->variant_type;
  305. cp->deleted = obj->deleted;
  306. cp->soft_del = obj->soft_del;
  307. cp->unlinked = obj->unlinked;
  308. cp->fake = obj->fake;
  309. cp->rename_allowed = obj->rename_allowed;
  310. cp->unlink_allowed = obj->unlink_allowed;
  311. cp->serial = obj->serial;
  312. cp->n_data_chunks = obj->n_data_chunks;
  313. if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  314. cp->size_or_equiv_obj = obj->variant.file_variant.file_size;
  315. else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
  316. cp->size_or_equiv_obj = obj->variant.hardlink_variant.equiv_id;
  317. }
  318. static int yaffs2_checkpt_obj_to_obj(struct yaffs_obj *obj,
  319. struct yaffs_checkpt_obj *cp)
  320. {
  321. struct yaffs_obj *parent;
  322. if (obj->variant_type != cp->variant_type) {
  323. yaffs_trace(YAFFS_TRACE_ERROR,
  324. "Checkpoint read object %d type %d chunk %d does not match existing object type %d",
  325. cp->obj_id, cp->variant_type, cp->hdr_chunk,
  326. obj->variant_type);
  327. return 0;
  328. }
  329. obj->obj_id = cp->obj_id;
  330. if (cp->parent_id)
  331. parent = yaffs_find_or_create_by_number(obj->my_dev,
  332. cp->parent_id,
  333. YAFFS_OBJECT_TYPE_DIRECTORY);
  334. else
  335. parent = NULL;
  336. if (parent) {
  337. if (parent->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY) {
  338. yaffs_trace(YAFFS_TRACE_ALWAYS,
  339. "Checkpoint read object %d parent %d type %d chunk %d Parent type, %d, not directory",
  340. cp->obj_id, cp->parent_id,
  341. cp->variant_type, cp->hdr_chunk,
  342. parent->variant_type);
  343. return 0;
  344. }
  345. yaffs_add_obj_to_dir(parent, obj);
  346. }
  347. obj->hdr_chunk = cp->hdr_chunk;
  348. obj->variant_type = cp->variant_type;
  349. obj->deleted = cp->deleted;
  350. obj->soft_del = cp->soft_del;
  351. obj->unlinked = cp->unlinked;
  352. obj->fake = cp->fake;
  353. obj->rename_allowed = cp->rename_allowed;
  354. obj->unlink_allowed = cp->unlink_allowed;
  355. obj->serial = cp->serial;
  356. obj->n_data_chunks = cp->n_data_chunks;
  357. if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  358. obj->variant.file_variant.file_size = cp->size_or_equiv_obj;
  359. else if (obj->variant_type == YAFFS_OBJECT_TYPE_HARDLINK)
  360. obj->variant.hardlink_variant.equiv_id = cp->size_or_equiv_obj;
  361. if (obj->hdr_chunk > 0)
  362. obj->lazy_loaded = 1;
  363. return 1;
  364. }
  365. static int yaffs2_checkpt_tnode_worker(struct yaffs_obj *in,
  366. struct yaffs_tnode *tn, u32 level,
  367. int chunk_offset)
  368. {
  369. int i;
  370. struct yaffs_dev *dev = in->my_dev;
  371. int ok = 1;
  372. u32 base_offset;
  373. if (!tn)
  374. return 1;
  375. if (level > 0) {
  376. for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++) {
  377. if (!tn->internal[i])
  378. continue;
  379. ok = yaffs2_checkpt_tnode_worker(in,
  380. tn->internal[i],
  381. level - 1,
  382. (chunk_offset <<
  383. YAFFS_TNODES_INTERNAL_BITS) + i);
  384. }
  385. return ok;
  386. }
  387. /* Level 0 tnode */
  388. base_offset = chunk_offset << YAFFS_TNODES_LEVEL0_BITS;
  389. ok = (yaffs2_checkpt_wr(dev, &base_offset, sizeof(base_offset)) ==
  390. sizeof(base_offset));
  391. if (ok)
  392. ok = (yaffs2_checkpt_wr(dev, tn, dev->tnode_size) ==
  393. dev->tnode_size);
  394. return ok;
  395. }
  396. static int yaffs2_wr_checkpt_tnodes(struct yaffs_obj *obj)
  397. {
  398. u32 end_marker = ~0;
  399. int ok = 1;
  400. if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
  401. return ok;
  402. ok = yaffs2_checkpt_tnode_worker(obj,
  403. obj->variant.file_variant.top,
  404. obj->variant.file_variant.
  405. top_level, 0);
  406. if (ok)
  407. ok = (yaffs2_checkpt_wr(obj->my_dev, &end_marker,
  408. sizeof(end_marker)) == sizeof(end_marker));
  409. return ok ? 1 : 0;
  410. }
  411. static int yaffs2_rd_checkpt_tnodes(struct yaffs_obj *obj)
  412. {
  413. u32 base_chunk;
  414. int ok = 1;
  415. struct yaffs_dev *dev = obj->my_dev;
  416. struct yaffs_file_var *file_stuct_ptr = &obj->variant.file_variant;
  417. struct yaffs_tnode *tn;
  418. int nread = 0;
  419. ok = (yaffs2_checkpt_rd(dev, &base_chunk, sizeof(base_chunk)) ==
  420. sizeof(base_chunk));
  421. while (ok && (~base_chunk)) {
  422. nread++;
  423. /* Read level 0 tnode */
  424. tn = yaffs_get_tnode(dev);
  425. if (tn)
  426. ok = (yaffs2_checkpt_rd(dev, tn, dev->tnode_size) ==
  427. dev->tnode_size);
  428. else
  429. ok = 0;
  430. if (tn && ok)
  431. ok = yaffs_add_find_tnode_0(dev,
  432. file_stuct_ptr,
  433. base_chunk, tn) ? 1 : 0;
  434. if (ok)
  435. ok = (yaffs2_checkpt_rd
  436. (dev, &base_chunk,
  437. sizeof(base_chunk)) == sizeof(base_chunk));
  438. }
  439. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  440. "Checkpoint read tnodes %d records, last %d. ok %d",
  441. nread, base_chunk, ok);
  442. return ok ? 1 : 0;
  443. }
  444. static int yaffs2_wr_checkpt_objs(struct yaffs_dev *dev)
  445. {
  446. struct yaffs_obj *obj;
  447. struct yaffs_checkpt_obj cp;
  448. int i;
  449. int ok = 1;
  450. struct list_head *lh;
  451. /* Iterate through the objects in each hash entry,
  452. * dumping them to the checkpointing stream.
  453. */
  454. for (i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++) {
  455. list_for_each(lh, &dev->obj_bucket[i].list) {
  456. obj = list_entry(lh, struct yaffs_obj, hash_link);
  457. if (!obj->defered_free) {
  458. yaffs2_obj_checkpt_obj(&cp, obj);
  459. cp.struct_type = sizeof(cp);
  460. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  461. "Checkpoint write object %d parent %d type %d chunk %d obj addr %p",
  462. cp.obj_id, cp.parent_id,
  463. cp.variant_type, cp.hdr_chunk, obj);
  464. ok = (yaffs2_checkpt_wr(dev, &cp,
  465. sizeof(cp)) == sizeof(cp));
  466. if (ok &&
  467. obj->variant_type ==
  468. YAFFS_OBJECT_TYPE_FILE)
  469. ok = yaffs2_wr_checkpt_tnodes(obj);
  470. }
  471. }
  472. }
  473. /* Dump end of list */
  474. memset(&cp, 0xff, sizeof(struct yaffs_checkpt_obj));
  475. cp.struct_type = sizeof(cp);
  476. if (ok)
  477. ok = (yaffs2_checkpt_wr(dev, &cp, sizeof(cp)) == sizeof(cp));
  478. return ok ? 1 : 0;
  479. }
  480. static int yaffs2_rd_checkpt_objs(struct yaffs_dev *dev)
  481. {
  482. struct yaffs_obj *obj;
  483. struct yaffs_checkpt_obj cp;
  484. int ok = 1;
  485. int done = 0;
  486. LIST_HEAD(hard_list);
  487. while (ok && !done) {
  488. ok = (yaffs2_checkpt_rd(dev, &cp, sizeof(cp)) == sizeof(cp));
  489. if (cp.struct_type != sizeof(cp)) {
  490. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  491. "struct size %d instead of %d ok %d",
  492. cp.struct_type, (int)sizeof(cp), ok);
  493. ok = 0;
  494. }
  495. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  496. "Checkpoint read object %d parent %d type %d chunk %d ",
  497. cp.obj_id, cp.parent_id, cp.variant_type,
  498. cp.hdr_chunk);
  499. if (ok && cp.obj_id == ~0) {
  500. done = 1;
  501. } else if (ok) {
  502. obj =
  503. yaffs_find_or_create_by_number(dev, cp.obj_id,
  504. cp.variant_type);
  505. if (obj) {
  506. ok = yaffs2_checkpt_obj_to_obj(obj, &cp);
  507. if (!ok)
  508. break;
  509. if (obj->variant_type ==
  510. YAFFS_OBJECT_TYPE_FILE) {
  511. ok = yaffs2_rd_checkpt_tnodes(obj);
  512. } else if (obj->variant_type ==
  513. YAFFS_OBJECT_TYPE_HARDLINK) {
  514. list_add(&obj->hard_links, &hard_list);
  515. }
  516. } else {
  517. ok = 0;
  518. }
  519. }
  520. }
  521. if (ok)
  522. yaffs_link_fixup(dev, &hard_list);
  523. return ok ? 1 : 0;
  524. }
  525. static int yaffs2_wr_checkpt_sum(struct yaffs_dev *dev)
  526. {
  527. u32 checkpt_sum;
  528. int ok;
  529. yaffs2_get_checkpt_sum(dev, &checkpt_sum);
  530. ok = (yaffs2_checkpt_wr(dev, &checkpt_sum, sizeof(checkpt_sum)) ==
  531. sizeof(checkpt_sum));
  532. if (!ok)
  533. return 0;
  534. return 1;
  535. }
  536. static int yaffs2_rd_checkpt_sum(struct yaffs_dev *dev)
  537. {
  538. u32 checkpt_sum0;
  539. u32 checkpt_sum1;
  540. int ok;
  541. yaffs2_get_checkpt_sum(dev, &checkpt_sum0);
  542. ok = (yaffs2_checkpt_rd(dev, &checkpt_sum1, sizeof(checkpt_sum1)) ==
  543. sizeof(checkpt_sum1));
  544. if (!ok)
  545. return 0;
  546. if (checkpt_sum0 != checkpt_sum1)
  547. return 0;
  548. return 1;
  549. }
  550. static int yaffs2_wr_checkpt_data(struct yaffs_dev *dev)
  551. {
  552. int ok = 1;
  553. if (!yaffs2_checkpt_required(dev)) {
  554. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  555. "skipping checkpoint write");
  556. ok = 0;
  557. }
  558. if (ok)
  559. ok = yaffs2_checkpt_open(dev, 1);
  560. if (ok) {
  561. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  562. "write checkpoint validity");
  563. ok = yaffs2_wr_checkpt_validity_marker(dev, 1);
  564. }
  565. if (ok) {
  566. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  567. "write checkpoint device");
  568. ok = yaffs2_wr_checkpt_dev(dev);
  569. }
  570. if (ok) {
  571. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  572. "write checkpoint objects");
  573. ok = yaffs2_wr_checkpt_objs(dev);
  574. }
  575. if (ok) {
  576. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  577. "write checkpoint validity");
  578. ok = yaffs2_wr_checkpt_validity_marker(dev, 0);
  579. }
  580. if (ok)
  581. ok = yaffs2_wr_checkpt_sum(dev);
  582. if (!yaffs_checkpt_close(dev))
  583. ok = 0;
  584. if (ok)
  585. dev->is_checkpointed = 1;
  586. else
  587. dev->is_checkpointed = 0;
  588. return dev->is_checkpointed;
  589. }
  590. static int yaffs2_rd_checkpt_data(struct yaffs_dev *dev)
  591. {
  592. int ok = 1;
  593. if (!dev->param.is_yaffs2)
  594. ok = 0;
  595. if (ok && dev->param.skip_checkpt_rd) {
  596. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  597. "skipping checkpoint read");
  598. ok = 0;
  599. }
  600. if (ok)
  601. ok = yaffs2_checkpt_open(dev, 0); /* open for read */
  602. if (ok) {
  603. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  604. "read checkpoint validity");
  605. ok = yaffs2_rd_checkpt_validity_marker(dev, 1);
  606. }
  607. if (ok) {
  608. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  609. "read checkpoint device");
  610. ok = yaffs2_rd_checkpt_dev(dev);
  611. }
  612. if (ok) {
  613. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  614. "read checkpoint objects");
  615. ok = yaffs2_rd_checkpt_objs(dev);
  616. }
  617. if (ok) {
  618. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  619. "read checkpoint validity");
  620. ok = yaffs2_rd_checkpt_validity_marker(dev, 0);
  621. }
  622. if (ok) {
  623. ok = yaffs2_rd_checkpt_sum(dev);
  624. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  625. "read checkpoint checksum %d", ok);
  626. }
  627. if (!yaffs_checkpt_close(dev))
  628. ok = 0;
  629. if (ok)
  630. dev->is_checkpointed = 1;
  631. else
  632. dev->is_checkpointed = 0;
  633. return ok ? 1 : 0;
  634. }
  635. void yaffs2_checkpt_invalidate(struct yaffs_dev *dev)
  636. {
  637. if (dev->is_checkpointed || dev->blocks_in_checkpt > 0) {
  638. dev->is_checkpointed = 0;
  639. yaffs2_checkpt_invalidate_stream(dev);
  640. }
  641. if (dev->param.sb_dirty_fn)
  642. dev->param.sb_dirty_fn(dev);
  643. }
  644. int yaffs_checkpoint_save(struct yaffs_dev *dev)
  645. {
  646. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  647. "save entry: is_checkpointed %d",
  648. dev->is_checkpointed);
  649. yaffs_verify_objects(dev);
  650. yaffs_verify_blocks(dev);
  651. yaffs_verify_free_chunks(dev);
  652. if (!dev->is_checkpointed) {
  653. yaffs2_checkpt_invalidate(dev);
  654. yaffs2_wr_checkpt_data(dev);
  655. }
  656. yaffs_trace(YAFFS_TRACE_CHECKPOINT | YAFFS_TRACE_MOUNT,
  657. "save exit: is_checkpointed %d",
  658. dev->is_checkpointed);
  659. return dev->is_checkpointed;
  660. }
  661. int yaffs2_checkpt_restore(struct yaffs_dev *dev)
  662. {
  663. int retval;
  664. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  665. "restore entry: is_checkpointed %d",
  666. dev->is_checkpointed);
  667. retval = yaffs2_rd_checkpt_data(dev);
  668. if (dev->is_checkpointed) {
  669. yaffs_verify_objects(dev);
  670. yaffs_verify_blocks(dev);
  671. yaffs_verify_free_chunks(dev);
  672. }
  673. yaffs_trace(YAFFS_TRACE_CHECKPOINT,
  674. "restore exit: is_checkpointed %d",
  675. dev->is_checkpointed);
  676. return retval;
  677. }
  678. int yaffs2_handle_hole(struct yaffs_obj *obj, loff_t new_size)
  679. {
  680. /* if new_size > old_file_size.
  681. * We're going to be writing a hole.
  682. * If the hole is small then write zeros otherwise write a start
  683. * of hole marker.
  684. */
  685. loff_t old_file_size;
  686. loff_t increase;
  687. int small_hole;
  688. int result = YAFFS_OK;
  689. struct yaffs_dev *dev = NULL;
  690. u8 *local_buffer = NULL;
  691. int small_increase_ok = 0;
  692. if (!obj)
  693. return YAFFS_FAIL;
  694. if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
  695. return YAFFS_FAIL;
  696. dev = obj->my_dev;
  697. /* Bail out if not yaffs2 mode */
  698. if (!dev->param.is_yaffs2)
  699. return YAFFS_OK;
  700. old_file_size = obj->variant.file_variant.file_size;
  701. if (new_size <= old_file_size)
  702. return YAFFS_OK;
  703. increase = new_size - old_file_size;
  704. if (increase < YAFFS_SMALL_HOLE_THRESHOLD * dev->data_bytes_per_chunk &&
  705. yaffs_check_alloc_available(dev, YAFFS_SMALL_HOLE_THRESHOLD + 1))
  706. small_hole = 1;
  707. else
  708. small_hole = 0;
  709. if (small_hole)
  710. local_buffer = yaffs_get_temp_buffer(dev);
  711. if (local_buffer) {
  712. /* fill hole with zero bytes */
  713. loff_t pos = old_file_size;
  714. int this_write;
  715. int written;
  716. memset(local_buffer, 0, dev->data_bytes_per_chunk);
  717. small_increase_ok = 1;
  718. while (increase > 0 && small_increase_ok) {
  719. this_write = increase;
  720. if (this_write > dev->data_bytes_per_chunk)
  721. this_write = dev->data_bytes_per_chunk;
  722. written =
  723. yaffs_do_file_wr(obj, local_buffer, pos, this_write,
  724. 0);
  725. if (written == this_write) {
  726. pos += this_write;
  727. increase -= this_write;
  728. } else {
  729. small_increase_ok = 0;
  730. }
  731. }
  732. yaffs_release_temp_buffer(dev, local_buffer);
  733. /* If out of space then reverse any chunks we've added */
  734. if (!small_increase_ok)
  735. yaffs_resize_file_down(obj, old_file_size);
  736. }
  737. if (!small_increase_ok &&
  738. obj->parent &&
  739. obj->parent->obj_id != YAFFS_OBJECTID_UNLINKED &&
  740. obj->parent->obj_id != YAFFS_OBJECTID_DELETED) {
  741. /* Write a hole start header with the old file size */
  742. yaffs_update_oh(obj, NULL, 0, 1, 0, NULL);
  743. }
  744. return result;
  745. }
  746. struct yaffs_block_index {
  747. int seq;
  748. int block;
  749. };
  750. static int yaffs2_ybicmp(const void *a, const void *b)
  751. {
  752. int aseq = ((struct yaffs_block_index *)a)->seq;
  753. int bseq = ((struct yaffs_block_index *)b)->seq;
  754. int ablock = ((struct yaffs_block_index *)a)->block;
  755. int bblock = ((struct yaffs_block_index *)b)->block;
  756. if (aseq == bseq)
  757. return ablock - bblock;
  758. return aseq - bseq;
  759. }
  760. static inline int yaffs2_scan_chunk(struct yaffs_dev *dev,
  761. struct yaffs_block_info *bi,
  762. int blk, int chunk_in_block,
  763. int *found_chunks,
  764. u8 *chunk_data,
  765. struct list_head *hard_list,
  766. int summary_available)
  767. {
  768. struct yaffs_obj_hdr *oh;
  769. struct yaffs_obj *in;
  770. struct yaffs_obj *parent;
  771. int equiv_id;
  772. loff_t file_size;
  773. int is_shrink;
  774. int is_unlinked;
  775. struct yaffs_ext_tags tags;
  776. int result;
  777. int alloc_failed = 0;
  778. int chunk = blk * dev->param.chunks_per_block + chunk_in_block;
  779. struct yaffs_file_var *file_var;
  780. struct yaffs_hardlink_var *hl_var;
  781. struct yaffs_symlink_var *sl_var;
  782. if (summary_available) {
  783. result = yaffs_summary_fetch(dev, &tags, chunk_in_block);
  784. tags.seq_number = bi->seq_number;
  785. }
  786. if (!summary_available || tags.obj_id == 0) {
  787. result = yaffs_rd_chunk_tags_nand(dev, chunk, NULL, &tags);
  788. dev->tags_used++;
  789. } else {
  790. dev->summary_used++;
  791. }
  792. /* Let's have a good look at this chunk... */
  793. if (!tags.chunk_used) {
  794. /* An unassigned chunk in the block.
  795. * If there are used chunks after this one, then
  796. * it is a chunk that was skipped due to failing
  797. * the erased check. Just skip it so that it can
  798. * be deleted.
  799. * But, more typically, We get here when this is
  800. * an unallocated chunk and his means that
  801. * either the block is empty or this is the one
  802. * being allocated from
  803. */
  804. if (*found_chunks) {
  805. /* This is a chunk that was skipped due
  806. * to failing the erased check */
  807. } else if (chunk_in_block == 0) {
  808. /* We're looking at the first chunk in
  809. * the block so the block is unused */
  810. bi->block_state = YAFFS_BLOCK_STATE_EMPTY;
  811. dev->n_erased_blocks++;
  812. } else {
  813. if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
  814. bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING) {
  815. if (dev->seq_number == bi->seq_number) {
  816. /* Allocating from this block*/
  817. yaffs_trace(YAFFS_TRACE_SCAN,
  818. " Allocating from %d %d",
  819. blk, chunk_in_block);
  820. bi->block_state =
  821. YAFFS_BLOCK_STATE_ALLOCATING;
  822. dev->alloc_block = blk;
  823. dev->alloc_page = chunk_in_block;
  824. dev->alloc_block_finder = blk;
  825. } else {
  826. /* This is a partially written block
  827. * that is not the current
  828. * allocation block.
  829. */
  830. yaffs_trace(YAFFS_TRACE_SCAN,
  831. "Partially written block %d detected. gc will fix this.",
  832. blk);
  833. }
  834. }
  835. }
  836. dev->n_free_chunks++;
  837. } else if (tags.ecc_result ==
  838. YAFFS_ECC_RESULT_UNFIXED) {
  839. yaffs_trace(YAFFS_TRACE_SCAN,
  840. " Unfixed ECC in chunk(%d:%d), chunk ignored",
  841. blk, chunk_in_block);
  842. dev->n_free_chunks++;
  843. } else if (tags.obj_id > YAFFS_MAX_OBJECT_ID ||
  844. tags.chunk_id > YAFFS_MAX_CHUNK_ID ||
  845. tags.obj_id == YAFFS_OBJECTID_SUMMARY ||
  846. (tags.chunk_id > 0 &&
  847. tags.n_bytes > dev->data_bytes_per_chunk) ||
  848. tags.seq_number != bi->seq_number) {
  849. yaffs_trace(YAFFS_TRACE_SCAN,
  850. "Chunk (%d:%d) with bad tags:obj = %d, chunk_id = %d, n_bytes = %d, ignored",
  851. blk, chunk_in_block, tags.obj_id,
  852. tags.chunk_id, tags.n_bytes);
  853. dev->n_free_chunks++;
  854. } else if (tags.chunk_id > 0) {
  855. /* chunk_id > 0 so it is a data chunk... */
  856. loff_t endpos;
  857. loff_t chunk_base = (tags.chunk_id - 1) *
  858. dev->data_bytes_per_chunk;
  859. *found_chunks = 1;
  860. yaffs_set_chunk_bit(dev, blk, chunk_in_block);
  861. bi->pages_in_use++;
  862. in = yaffs_find_or_create_by_number(dev,
  863. tags.obj_id,
  864. YAFFS_OBJECT_TYPE_FILE);
  865. if (!in)
  866. /* Out of memory */
  867. alloc_failed = 1;
  868. if (in &&
  869. in->variant_type == YAFFS_OBJECT_TYPE_FILE &&
  870. chunk_base < in->variant.file_variant.shrink_size) {
  871. /* This has not been invalidated by
  872. * a resize */
  873. if (!yaffs_put_chunk_in_file(in, tags.chunk_id,
  874. chunk, -1))
  875. alloc_failed = 1;
  876. /* File size is calculated by looking at
  877. * the data chunks if we have not
  878. * seen an object header yet.
  879. * Stop this practice once we find an
  880. * object header.
  881. */
  882. endpos = chunk_base + tags.n_bytes;
  883. if (!in->valid &&
  884. in->variant.file_variant.scanned_size < endpos) {
  885. in->variant.file_variant.
  886. scanned_size = endpos;
  887. in->variant.file_variant.
  888. file_size = endpos;
  889. }
  890. } else if (in) {
  891. /* This chunk has been invalidated by a
  892. * resize, or a past file deletion
  893. * so delete the chunk*/
  894. yaffs_chunk_del(dev, chunk, 1, __LINE__);
  895. }
  896. } else {
  897. /* chunk_id == 0, so it is an ObjectHeader.
  898. * Thus, we read in the object header and make
  899. * the object
  900. */
  901. *found_chunks = 1;
  902. yaffs_set_chunk_bit(dev, blk, chunk_in_block);
  903. bi->pages_in_use++;
  904. oh = NULL;
  905. in = NULL;
  906. if (tags.extra_available) {
  907. in = yaffs_find_or_create_by_number(dev,
  908. tags.obj_id,
  909. tags.extra_obj_type);
  910. if (!in)
  911. alloc_failed = 1;
  912. }
  913. if (!in ||
  914. (!in->valid && dev->param.disable_lazy_load) ||
  915. tags.extra_shadows ||
  916. (!in->valid && (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  917. tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND))) {
  918. /* If we don't have valid info then we
  919. * need to read the chunk
  920. * TODO In future we can probably defer
  921. * reading the chunk and living with
  922. * invalid data until needed.
  923. */
  924. result = yaffs_rd_chunk_tags_nand(dev,
  925. chunk,
  926. chunk_data,
  927. NULL);
  928. oh = (struct yaffs_obj_hdr *)chunk_data;
  929. if (dev->param.inband_tags) {
  930. /* Fix up the header if they got
  931. * corrupted by inband tags */
  932. oh->shadows_obj =
  933. oh->inband_shadowed_obj_id;
  934. oh->is_shrink =
  935. oh->inband_is_shrink;
  936. }
  937. if (!in) {
  938. in = yaffs_find_or_create_by_number(dev,
  939. tags.obj_id, oh->type);
  940. if (!in)
  941. alloc_failed = 1;
  942. }
  943. }
  944. if (!in) {
  945. /* TODO Hoosterman we have a problem! */
  946. yaffs_trace(YAFFS_TRACE_ERROR,
  947. "yaffs tragedy: Could not make object for object %d at chunk %d during scan",
  948. tags.obj_id, chunk);
  949. return YAFFS_FAIL;
  950. }
  951. if (in->valid) {
  952. /* We have already filled this one.
  953. * We have a duplicate that will be
  954. * discarded, but we first have to suck
  955. * out resize info if it is a file.
  956. */
  957. if ((in->variant_type == YAFFS_OBJECT_TYPE_FILE) &&
  958. ((oh && oh->type == YAFFS_OBJECT_TYPE_FILE) ||
  959. (tags.extra_available &&
  960. tags.extra_obj_type == YAFFS_OBJECT_TYPE_FILE)
  961. )) {
  962. loff_t this_size = (oh) ?
  963. yaffs_oh_to_size(oh) :
  964. tags.extra_file_size;
  965. u32 parent_obj_id = (oh) ?
  966. oh->parent_obj_id :
  967. tags.extra_parent_id;
  968. is_shrink = (oh) ?
  969. oh->is_shrink :
  970. tags.extra_is_shrink;
  971. /* If it is deleted (unlinked
  972. * at start also means deleted)
  973. * we treat the file size as
  974. * being zeroed at this point.
  975. */
  976. if (parent_obj_id == YAFFS_OBJECTID_DELETED ||
  977. parent_obj_id == YAFFS_OBJECTID_UNLINKED) {
  978. this_size = 0;
  979. is_shrink = 1;
  980. }
  981. if (is_shrink &&
  982. in->variant.file_variant.shrink_size >
  983. this_size)
  984. in->variant.file_variant.shrink_size =
  985. this_size;
  986. if (is_shrink)
  987. bi->has_shrink_hdr = 1;
  988. }
  989. /* Use existing - destroy this one. */
  990. yaffs_chunk_del(dev, chunk, 1, __LINE__);
  991. }
  992. if (!in->valid && in->variant_type !=
  993. (oh ? oh->type : tags.extra_obj_type)) {
  994. yaffs_trace(YAFFS_TRACE_ERROR,
  995. "yaffs tragedy: Bad type, %d != %d, for object %d at chunk %d during scan",
  996. oh ? oh->type : tags.extra_obj_type,
  997. in->variant_type, tags.obj_id,
  998. chunk);
  999. in = yaffs_retype_obj(in, oh ? oh->type : tags.extra_obj_type);
  1000. }
  1001. if (!in->valid &&
  1002. (tags.obj_id == YAFFS_OBJECTID_ROOT ||
  1003. tags.obj_id == YAFFS_OBJECTID_LOSTNFOUND)) {
  1004. /* We only load some info, don't fiddle
  1005. * with directory structure */
  1006. in->valid = 1;
  1007. if (oh) {
  1008. in->yst_mode = oh->yst_mode;
  1009. yaffs_load_attribs(in, oh);
  1010. in->lazy_loaded = 0;
  1011. } else {
  1012. in->lazy_loaded = 1;
  1013. }
  1014. in->hdr_chunk = chunk;
  1015. } else if (!in->valid) {
  1016. /* we need to load this info */
  1017. in->valid = 1;
  1018. in->hdr_chunk = chunk;
  1019. if (oh) {
  1020. in->variant_type = oh->type;
  1021. in->yst_mode = oh->yst_mode;
  1022. yaffs_load_attribs(in, oh);
  1023. if (oh->shadows_obj > 0)
  1024. yaffs_handle_shadowed_obj(dev,
  1025. oh->shadows_obj, 1);
  1026. yaffs_set_obj_name_from_oh(in, oh);
  1027. parent = yaffs_find_or_create_by_number(dev,
  1028. oh->parent_obj_id,
  1029. YAFFS_OBJECT_TYPE_DIRECTORY);
  1030. file_size = yaffs_oh_to_size(oh);
  1031. is_shrink = oh->is_shrink;
  1032. equiv_id = oh->equiv_id;
  1033. } else {
  1034. in->variant_type = tags.extra_obj_type;
  1035. parent = yaffs_find_or_create_by_number(dev,
  1036. tags.extra_parent_id,
  1037. YAFFS_OBJECT_TYPE_DIRECTORY);
  1038. file_size = tags.extra_file_size;
  1039. is_shrink = tags.extra_is_shrink;
  1040. equiv_id = tags.extra_equiv_id;
  1041. in->lazy_loaded = 1;
  1042. }
  1043. in->dirty = 0;
  1044. if (!parent)
  1045. alloc_failed = 1;
  1046. /* directory stuff...
  1047. * hook up to parent
  1048. */
  1049. if (parent &&
  1050. parent->variant_type == YAFFS_OBJECT_TYPE_UNKNOWN) {
  1051. /* Set up as a directory */
  1052. parent->variant_type =
  1053. YAFFS_OBJECT_TYPE_DIRECTORY;
  1054. INIT_LIST_HEAD(&parent->
  1055. variant.dir_variant.children);
  1056. } else if (!parent ||
  1057. parent->variant_type !=
  1058. YAFFS_OBJECT_TYPE_DIRECTORY) {
  1059. /* Hoosterman, another problem....
  1060. * Trying to use a non-directory as a directory
  1061. */
  1062. yaffs_trace(YAFFS_TRACE_ERROR,
  1063. "yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found."
  1064. );
  1065. parent = dev->lost_n_found;
  1066. }
  1067. yaffs_add_obj_to_dir(parent, in);
  1068. is_unlinked = (parent == dev->del_dir) ||
  1069. (parent == dev->unlinked_dir);
  1070. if (is_shrink)
  1071. /* Mark the block */
  1072. bi->has_shrink_hdr = 1;
  1073. /* Note re hardlinks.
  1074. * Since we might scan a hardlink before its equivalent
  1075. * object is scanned we put them all in a list.
  1076. * After scanning is complete, we should have all the
  1077. * objects, so we run through this list and fix up all
  1078. * the chains.
  1079. */
  1080. switch (in->variant_type) {
  1081. case YAFFS_OBJECT_TYPE_UNKNOWN:
  1082. /* Todo got a problem */
  1083. break;
  1084. case YAFFS_OBJECT_TYPE_FILE:
  1085. file_var = &in->variant.file_variant;
  1086. if (file_var->scanned_size < file_size) {
  1087. /* This covers the case where the file
  1088. * size is greater than the data held.
  1089. * This will happen if the file is
  1090. * resized to be larger than its
  1091. * current data extents.
  1092. */
  1093. file_var->file_size = file_size;
  1094. file_var->scanned_size = file_size;
  1095. }
  1096. if (file_var->shrink_size > file_size)
  1097. file_var->shrink_size = file_size;
  1098. break;
  1099. case YAFFS_OBJECT_TYPE_HARDLINK:
  1100. hl_var = &in->variant.hardlink_variant;
  1101. if (!is_unlinked) {
  1102. hl_var->equiv_id = equiv_id;
  1103. list_add(&in->hard_links, hard_list);
  1104. }
  1105. break;
  1106. case YAFFS_OBJECT_TYPE_DIRECTORY:
  1107. /* Do nothing */
  1108. break;
  1109. case YAFFS_OBJECT_TYPE_SPECIAL:
  1110. /* Do nothing */
  1111. break;
  1112. case YAFFS_OBJECT_TYPE_SYMLINK:
  1113. sl_var = &in->variant.symlink_variant;
  1114. if (oh) {
  1115. sl_var->alias =
  1116. yaffs_clone_str(oh->alias);
  1117. if (!sl_var->alias)
  1118. alloc_failed = 1;
  1119. }
  1120. break;
  1121. }
  1122. }
  1123. }
  1124. return alloc_failed ? YAFFS_FAIL : YAFFS_OK;
  1125. }
  1126. int yaffs2_scan_backwards(struct yaffs_dev *dev)
  1127. {
  1128. int blk;
  1129. int block_iter;
  1130. int start_iter;
  1131. int end_iter;
  1132. int n_to_scan = 0;
  1133. enum yaffs_block_state state;
  1134. int c;
  1135. int deleted;
  1136. LIST_HEAD(hard_list);
  1137. struct yaffs_block_info *bi;
  1138. u32 seq_number;
  1139. int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  1140. u8 *chunk_data;
  1141. int found_chunks;
  1142. int alloc_failed = 0;
  1143. struct yaffs_block_index *block_index = NULL;
  1144. int alt_block_index = 0;
  1145. int summary_available;
  1146. yaffs_trace(YAFFS_TRACE_SCAN,
  1147. "yaffs2_scan_backwards starts intstartblk %d intendblk %d...",
  1148. dev->internal_start_block, dev->internal_end_block);
  1149. dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  1150. block_index =
  1151. kmalloc(n_blocks * sizeof(struct yaffs_block_index), GFP_NOFS);
  1152. if (!block_index) {
  1153. block_index =
  1154. vmalloc(n_blocks * sizeof(struct yaffs_block_index));
  1155. alt_block_index = 1;
  1156. }
  1157. if (!block_index) {
  1158. yaffs_trace(YAFFS_TRACE_SCAN,
  1159. "yaffs2_scan_backwards() could not allocate block index!"
  1160. );
  1161. return YAFFS_FAIL;
  1162. }
  1163. dev->blocks_in_checkpt = 0;
  1164. chunk_data = yaffs_get_temp_buffer(dev);
  1165. /* Scan all the blocks to determine their state */
  1166. bi = dev->block_info;
  1167. for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  1168. blk++) {
  1169. yaffs_clear_chunk_bits(dev, blk);
  1170. bi->pages_in_use = 0;
  1171. bi->soft_del_pages = 0;
  1172. yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  1173. bi->block_state = state;
  1174. bi->seq_number = seq_number;
  1175. if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
  1176. bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  1177. if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  1178. bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  1179. yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
  1180. "Block scanning block %d state %d seq %d",
  1181. blk, bi->block_state, seq_number);
  1182. if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  1183. dev->blocks_in_checkpt++;
  1184. } else if (bi->block_state == YAFFS_BLOCK_STATE_DEAD) {
  1185. yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
  1186. "block %d is bad", blk);
  1187. } else if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  1188. yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
  1189. dev->n_erased_blocks++;
  1190. dev->n_free_chunks += dev->param.chunks_per_block;
  1191. } else if (bi->block_state ==
  1192. YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  1193. /* Determine the highest sequence number */
  1194. if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
  1195. seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
  1196. block_index[n_to_scan].seq = seq_number;
  1197. block_index[n_to_scan].block = blk;
  1198. n_to_scan++;
  1199. if (seq_number >= dev->seq_number)
  1200. dev->seq_number = seq_number;
  1201. } else {
  1202. /* TODO: Nasty sequence number! */
  1203. yaffs_trace(YAFFS_TRACE_SCAN,
  1204. "Block scanning block %d has bad sequence number %d",
  1205. blk, seq_number);
  1206. }
  1207. }
  1208. bi++;
  1209. }
  1210. yaffs_trace(YAFFS_TRACE_ALWAYS, "%d blocks to be sorted...", n_to_scan);
  1211. cond_resched();
  1212. /* Sort the blocks by sequence number */
  1213. sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
  1214. yaffs2_ybicmp, NULL);
  1215. cond_resched();
  1216. yaffs_trace(YAFFS_TRACE_SCAN, "...done");
  1217. /* Now scan the blocks looking at the data. */
  1218. start_iter = 0;
  1219. end_iter = n_to_scan - 1;
  1220. yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan);
  1221. /* For each block.... backwards */
  1222. for (block_iter = end_iter;
  1223. !alloc_failed && block_iter >= start_iter;
  1224. block_iter--) {
  1225. /* Cooperative multitasking! This loop can run for so
  1226. long that watchdog timers expire. */
  1227. cond_resched();
  1228. /* get the block to scan in the correct order */
  1229. blk = block_index[block_iter].block;
  1230. bi = yaffs_get_block_info(dev, blk);
  1231. deleted = 0;
  1232. summary_available = yaffs_summary_read(dev, dev->sum_tags, blk);
  1233. /* For each chunk in each block that needs scanning.... */
  1234. found_chunks = 0;
  1235. if (summary_available)
  1236. c = dev->chunks_per_summary - 1;
  1237. else
  1238. c = dev->param.chunks_per_block - 1;
  1239. for (/* c is already initialised */;
  1240. !alloc_failed && c >= 0 &&
  1241. (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
  1242. bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING);
  1243. c--) {
  1244. /* Scan backwards...
  1245. * Read the tags and decide what to do
  1246. */
  1247. if (yaffs2_scan_chunk(dev, bi, blk, c,
  1248. &found_chunks, chunk_data,
  1249. &hard_list, summary_available) ==
  1250. YAFFS_FAIL)
  1251. alloc_failed = 1;
  1252. }
  1253. if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  1254. /* If we got this far while scanning, then the block
  1255. * is fully allocated. */
  1256. bi->block_state = YAFFS_BLOCK_STATE_FULL;
  1257. }
  1258. /* Now let's see if it was dirty */
  1259. if (bi->pages_in_use == 0 &&
  1260. !bi->has_shrink_hdr &&
  1261. bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  1262. yaffs_block_became_dirty(dev, blk);
  1263. }
  1264. }
  1265. yaffs_skip_rest_of_block(dev);
  1266. if (alt_block_index)
  1267. vfree(block_index);
  1268. else
  1269. kfree(block_index);
  1270. /* Ok, we've done all the scanning.
  1271. * Fix up the hard link chains.
  1272. * We have scanned all the objects, now it's time to add these
  1273. * hardlinks.
  1274. */
  1275. yaffs_link_fixup(dev, &hard_list);
  1276. yaffs_release_temp_buffer(dev, chunk_data);
  1277. if (alloc_failed)
  1278. return YAFFS_FAIL;
  1279. yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends");
  1280. return YAFFS_OK;
  1281. }