yaffs_yaffs2.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  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. LIST_HEAD(hard_list);
  1136. struct yaffs_block_info *bi;
  1137. u32 seq_number;
  1138. int n_blocks = dev->internal_end_block - dev->internal_start_block + 1;
  1139. u8 *chunk_data;
  1140. int found_chunks;
  1141. int alloc_failed = 0;
  1142. struct yaffs_block_index *block_index = NULL;
  1143. int alt_block_index = 0;
  1144. int summary_available;
  1145. yaffs_trace(YAFFS_TRACE_SCAN,
  1146. "yaffs2_scan_backwards starts intstartblk %d intendblk %d...",
  1147. dev->internal_start_block, dev->internal_end_block);
  1148. dev->seq_number = YAFFS_LOWEST_SEQUENCE_NUMBER;
  1149. block_index =
  1150. kmalloc(n_blocks * sizeof(struct yaffs_block_index), GFP_NOFS);
  1151. if (!block_index) {
  1152. block_index =
  1153. vmalloc(n_blocks * sizeof(struct yaffs_block_index));
  1154. alt_block_index = 1;
  1155. }
  1156. if (!block_index) {
  1157. yaffs_trace(YAFFS_TRACE_SCAN,
  1158. "yaffs2_scan_backwards() could not allocate block index!"
  1159. );
  1160. return YAFFS_FAIL;
  1161. }
  1162. dev->blocks_in_checkpt = 0;
  1163. chunk_data = yaffs_get_temp_buffer(dev);
  1164. /* Scan all the blocks to determine their state */
  1165. bi = dev->block_info;
  1166. for (blk = dev->internal_start_block; blk <= dev->internal_end_block;
  1167. blk++) {
  1168. yaffs_clear_chunk_bits(dev, blk);
  1169. bi->pages_in_use = 0;
  1170. bi->soft_del_pages = 0;
  1171. yaffs_query_init_block_state(dev, blk, &state, &seq_number);
  1172. bi->block_state = state;
  1173. bi->seq_number = seq_number;
  1174. if (bi->seq_number == YAFFS_SEQUENCE_CHECKPOINT_DATA)
  1175. bi->block_state = YAFFS_BLOCK_STATE_CHECKPOINT;
  1176. if (bi->seq_number == YAFFS_SEQUENCE_BAD_BLOCK)
  1177. bi->block_state = YAFFS_BLOCK_STATE_DEAD;
  1178. yaffs_trace(YAFFS_TRACE_SCAN_DEBUG,
  1179. "Block scanning block %d state %d seq %d",
  1180. blk, bi->block_state, seq_number);
  1181. if (bi->block_state == YAFFS_BLOCK_STATE_CHECKPOINT) {
  1182. dev->blocks_in_checkpt++;
  1183. } else if (bi->block_state == YAFFS_BLOCK_STATE_DEAD) {
  1184. yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
  1185. "block %d is bad", blk);
  1186. } else if (bi->block_state == YAFFS_BLOCK_STATE_EMPTY) {
  1187. yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "Block empty ");
  1188. dev->n_erased_blocks++;
  1189. dev->n_free_chunks += dev->param.chunks_per_block;
  1190. } else if (bi->block_state ==
  1191. YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  1192. /* Determine the highest sequence number */
  1193. if (seq_number >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
  1194. seq_number < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
  1195. block_index[n_to_scan].seq = seq_number;
  1196. block_index[n_to_scan].block = blk;
  1197. n_to_scan++;
  1198. if (seq_number >= dev->seq_number)
  1199. dev->seq_number = seq_number;
  1200. } else {
  1201. /* TODO: Nasty sequence number! */
  1202. yaffs_trace(YAFFS_TRACE_SCAN,
  1203. "Block scanning block %d has bad sequence number %d",
  1204. blk, seq_number);
  1205. }
  1206. }
  1207. bi++;
  1208. }
  1209. yaffs_trace(YAFFS_TRACE_ALWAYS, "%d blocks to be sorted...", n_to_scan);
  1210. cond_resched();
  1211. /* Sort the blocks by sequence number */
  1212. sort(block_index, n_to_scan, sizeof(struct yaffs_block_index),
  1213. yaffs2_ybicmp, NULL);
  1214. cond_resched();
  1215. yaffs_trace(YAFFS_TRACE_SCAN, "...done");
  1216. /* Now scan the blocks looking at the data. */
  1217. start_iter = 0;
  1218. end_iter = n_to_scan - 1;
  1219. yaffs_trace(YAFFS_TRACE_SCAN_DEBUG, "%d blocks to scan", n_to_scan);
  1220. /* For each block.... backwards */
  1221. for (block_iter = end_iter;
  1222. !alloc_failed && block_iter >= start_iter;
  1223. block_iter--) {
  1224. /* Cooperative multitasking! This loop can run for so
  1225. long that watchdog timers expire. */
  1226. cond_resched();
  1227. /* get the block to scan in the correct order */
  1228. blk = block_index[block_iter].block;
  1229. bi = yaffs_get_block_info(dev, blk);
  1230. summary_available = yaffs_summary_read(dev, dev->sum_tags, blk);
  1231. /* For each chunk in each block that needs scanning.... */
  1232. found_chunks = 0;
  1233. if (summary_available)
  1234. c = dev->chunks_per_summary - 1;
  1235. else
  1236. c = dev->param.chunks_per_block - 1;
  1237. for (/* c is already initialised */;
  1238. !alloc_failed && c >= 0 &&
  1239. (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN ||
  1240. bi->block_state == YAFFS_BLOCK_STATE_ALLOCATING);
  1241. c--) {
  1242. /* Scan backwards...
  1243. * Read the tags and decide what to do
  1244. */
  1245. if (yaffs2_scan_chunk(dev, bi, blk, c,
  1246. &found_chunks, chunk_data,
  1247. &hard_list, summary_available) ==
  1248. YAFFS_FAIL)
  1249. alloc_failed = 1;
  1250. }
  1251. if (bi->block_state == YAFFS_BLOCK_STATE_NEEDS_SCAN) {
  1252. /* If we got this far while scanning, then the block
  1253. * is fully allocated. */
  1254. bi->block_state = YAFFS_BLOCK_STATE_FULL;
  1255. }
  1256. /* Now let's see if it was dirty */
  1257. if (bi->pages_in_use == 0 &&
  1258. !bi->has_shrink_hdr &&
  1259. bi->block_state == YAFFS_BLOCK_STATE_FULL) {
  1260. yaffs_block_became_dirty(dev, blk);
  1261. }
  1262. }
  1263. yaffs_skip_rest_of_block(dev);
  1264. if (alt_block_index)
  1265. vfree(block_index);
  1266. else
  1267. kfree(block_index);
  1268. /* Ok, we've done all the scanning.
  1269. * Fix up the hard link chains.
  1270. * We have scanned all the objects, now it's time to add these
  1271. * hardlinks.
  1272. */
  1273. yaffs_link_fixup(dev, &hard_list);
  1274. yaffs_release_temp_buffer(dev, chunk_data);
  1275. if (alloc_failed)
  1276. return YAFFS_FAIL;
  1277. yaffs_trace(YAFFS_TRACE_SCAN, "yaffs2_scan_backwards ends");
  1278. return YAFFS_OK;
  1279. }