recovery.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * This file is part of UBIFS.
  4. *
  5. * Copyright (C) 2006-2008 Nokia Corporation
  6. *
  7. * Authors: Adrian Hunter
  8. * Artem Bityutskiy (Битюцкий Артём)
  9. */
  10. /*
  11. * This file implements functions needed to recover from unclean un-mounts.
  12. * When UBIFS is mounted, it checks a flag on the master node to determine if
  13. * an un-mount was completed successfully. If not, the process of mounting
  14. * incorporates additional checking and fixing of on-flash data structures.
  15. * UBIFS always cleans away all remnants of an unclean un-mount, so that
  16. * errors do not accumulate. However UBIFS defers recovery if it is mounted
  17. * read-only, and the flash is not modified in that case.
  18. *
  19. * The general UBIFS approach to the recovery is that it recovers from
  20. * corruptions which could be caused by power cuts, but it refuses to recover
  21. * from corruption caused by other reasons. And UBIFS tries to distinguish
  22. * between these 2 reasons of corruptions and silently recover in the former
  23. * case and loudly complain in the latter case.
  24. *
  25. * UBIFS writes only to erased LEBs, so it writes only to the flash space
  26. * containing only 0xFFs. UBIFS also always writes strictly from the beginning
  27. * of the LEB to the end. And UBIFS assumes that the underlying flash media
  28. * writes in @c->max_write_size bytes at a time.
  29. *
  30. * Hence, if UBIFS finds a corrupted node at offset X, it expects only the min.
  31. * I/O unit corresponding to offset X to contain corrupted data, all the
  32. * following min. I/O units have to contain empty space (all 0xFFs). If this is
  33. * not true, the corruption cannot be the result of a power cut, and UBIFS
  34. * refuses to mount.
  35. */
  36. #ifndef __UBOOT__
  37. #include <dm/devres.h>
  38. #include <linux/crc32.h>
  39. #include <linux/slab.h>
  40. #include <u-boot/crc.h>
  41. #else
  42. #include <linux/err.h>
  43. #endif
  44. #include "ubifs.h"
  45. /**
  46. * is_empty - determine whether a buffer is empty (contains all 0xff).
  47. * @buf: buffer to clean
  48. * @len: length of buffer
  49. *
  50. * This function returns %1 if the buffer is empty (contains all 0xff) otherwise
  51. * %0 is returned.
  52. */
  53. static int is_empty(void *buf, int len)
  54. {
  55. uint8_t *p = buf;
  56. int i;
  57. for (i = 0; i < len; i++)
  58. if (*p++ != 0xff)
  59. return 0;
  60. return 1;
  61. }
  62. /**
  63. * first_non_ff - find offset of the first non-0xff byte.
  64. * @buf: buffer to search in
  65. * @len: length of buffer
  66. *
  67. * This function returns offset of the first non-0xff byte in @buf or %-1 if
  68. * the buffer contains only 0xff bytes.
  69. */
  70. static int first_non_ff(void *buf, int len)
  71. {
  72. uint8_t *p = buf;
  73. int i;
  74. for (i = 0; i < len; i++)
  75. if (*p++ != 0xff)
  76. return i;
  77. return -1;
  78. }
  79. /**
  80. * get_master_node - get the last valid master node allowing for corruption.
  81. * @c: UBIFS file-system description object
  82. * @lnum: LEB number
  83. * @pbuf: buffer containing the LEB read, is returned here
  84. * @mst: master node, if found, is returned here
  85. * @cor: corruption, if found, is returned here
  86. *
  87. * This function allocates a buffer, reads the LEB into it, and finds and
  88. * returns the last valid master node allowing for one area of corruption.
  89. * The corrupt area, if there is one, must be consistent with the assumption
  90. * that it is the result of an unclean unmount while the master node was being
  91. * written. Under those circumstances, it is valid to use the previously written
  92. * master node.
  93. *
  94. * This function returns %0 on success and a negative error code on failure.
  95. */
  96. static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
  97. struct ubifs_mst_node **mst, void **cor)
  98. {
  99. const int sz = c->mst_node_alsz;
  100. int err, offs, len;
  101. void *sbuf, *buf;
  102. sbuf = vmalloc(c->leb_size);
  103. if (!sbuf)
  104. return -ENOMEM;
  105. err = ubifs_leb_read(c, lnum, sbuf, 0, c->leb_size, 0);
  106. if (err && err != -EBADMSG)
  107. goto out_free;
  108. /* Find the first position that is definitely not a node */
  109. offs = 0;
  110. buf = sbuf;
  111. len = c->leb_size;
  112. while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
  113. struct ubifs_ch *ch = buf;
  114. if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
  115. break;
  116. offs += sz;
  117. buf += sz;
  118. len -= sz;
  119. }
  120. /* See if there was a valid master node before that */
  121. if (offs) {
  122. int ret;
  123. offs -= sz;
  124. buf -= sz;
  125. len += sz;
  126. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
  127. if (ret != SCANNED_A_NODE && offs) {
  128. /* Could have been corruption so check one place back */
  129. offs -= sz;
  130. buf -= sz;
  131. len += sz;
  132. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
  133. if (ret != SCANNED_A_NODE)
  134. /*
  135. * We accept only one area of corruption because
  136. * we are assuming that it was caused while
  137. * trying to write a master node.
  138. */
  139. goto out_err;
  140. }
  141. if (ret == SCANNED_A_NODE) {
  142. struct ubifs_ch *ch = buf;
  143. if (ch->node_type != UBIFS_MST_NODE)
  144. goto out_err;
  145. dbg_rcvry("found a master node at %d:%d", lnum, offs);
  146. *mst = buf;
  147. offs += sz;
  148. buf += sz;
  149. len -= sz;
  150. }
  151. }
  152. /* Check for corruption */
  153. if (offs < c->leb_size) {
  154. if (!is_empty(buf, min_t(int, len, sz))) {
  155. *cor = buf;
  156. dbg_rcvry("found corruption at %d:%d", lnum, offs);
  157. }
  158. offs += sz;
  159. buf += sz;
  160. len -= sz;
  161. }
  162. /* Check remaining empty space */
  163. if (offs < c->leb_size)
  164. if (!is_empty(buf, len))
  165. goto out_err;
  166. *pbuf = sbuf;
  167. return 0;
  168. out_err:
  169. err = -EINVAL;
  170. out_free:
  171. vfree(sbuf);
  172. *mst = NULL;
  173. *cor = NULL;
  174. return err;
  175. }
  176. /**
  177. * write_rcvrd_mst_node - write recovered master node.
  178. * @c: UBIFS file-system description object
  179. * @mst: master node
  180. *
  181. * This function returns %0 on success and a negative error code on failure.
  182. */
  183. static int write_rcvrd_mst_node(struct ubifs_info *c,
  184. struct ubifs_mst_node *mst)
  185. {
  186. int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
  187. __le32 save_flags;
  188. dbg_rcvry("recovery");
  189. save_flags = mst->flags;
  190. mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY);
  191. ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1);
  192. err = ubifs_leb_change(c, lnum, mst, sz);
  193. if (err)
  194. goto out;
  195. err = ubifs_leb_change(c, lnum + 1, mst, sz);
  196. if (err)
  197. goto out;
  198. out:
  199. mst->flags = save_flags;
  200. return err;
  201. }
  202. /**
  203. * ubifs_recover_master_node - recover the master node.
  204. * @c: UBIFS file-system description object
  205. *
  206. * This function recovers the master node from corruption that may occur due to
  207. * an unclean unmount.
  208. *
  209. * This function returns %0 on success and a negative error code on failure.
  210. */
  211. int ubifs_recover_master_node(struct ubifs_info *c)
  212. {
  213. void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
  214. struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
  215. const int sz = c->mst_node_alsz;
  216. int err, offs1, offs2;
  217. dbg_rcvry("recovery");
  218. err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
  219. if (err)
  220. goto out_free;
  221. err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
  222. if (err)
  223. goto out_free;
  224. if (mst1) {
  225. offs1 = (void *)mst1 - buf1;
  226. if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
  227. (offs1 == 0 && !cor1)) {
  228. /*
  229. * mst1 was written by recovery at offset 0 with no
  230. * corruption.
  231. */
  232. dbg_rcvry("recovery recovery");
  233. mst = mst1;
  234. } else if (mst2) {
  235. offs2 = (void *)mst2 - buf2;
  236. if (offs1 == offs2) {
  237. /* Same offset, so must be the same */
  238. if (memcmp((void *)mst1 + UBIFS_CH_SZ,
  239. (void *)mst2 + UBIFS_CH_SZ,
  240. UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
  241. goto out_err;
  242. mst = mst1;
  243. } else if (offs2 + sz == offs1) {
  244. /* 1st LEB was written, 2nd was not */
  245. if (cor1)
  246. goto out_err;
  247. mst = mst1;
  248. } else if (offs1 == 0 &&
  249. c->leb_size - offs2 - sz < sz) {
  250. /* 1st LEB was unmapped and written, 2nd not */
  251. if (cor1)
  252. goto out_err;
  253. mst = mst1;
  254. } else
  255. goto out_err;
  256. } else {
  257. /*
  258. * 2nd LEB was unmapped and about to be written, so
  259. * there must be only one master node in the first LEB
  260. * and no corruption.
  261. */
  262. if (offs1 != 0 || cor1)
  263. goto out_err;
  264. mst = mst1;
  265. }
  266. } else {
  267. if (!mst2)
  268. goto out_err;
  269. /*
  270. * 1st LEB was unmapped and about to be written, so there must
  271. * be no room left in 2nd LEB.
  272. */
  273. offs2 = (void *)mst2 - buf2;
  274. if (offs2 + sz + sz <= c->leb_size)
  275. goto out_err;
  276. mst = mst2;
  277. }
  278. ubifs_msg(c, "recovered master node from LEB %d",
  279. (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
  280. memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
  281. if (c->ro_mount) {
  282. /* Read-only mode. Keep a copy for switching to rw mode */
  283. c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
  284. if (!c->rcvrd_mst_node) {
  285. err = -ENOMEM;
  286. goto out_free;
  287. }
  288. memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
  289. /*
  290. * We had to recover the master node, which means there was an
  291. * unclean reboot. However, it is possible that the master node
  292. * is clean at this point, i.e., %UBIFS_MST_DIRTY is not set.
  293. * E.g., consider the following chain of events:
  294. *
  295. * 1. UBIFS was cleanly unmounted, so the master node is clean
  296. * 2. UBIFS is being mounted R/W and starts changing the master
  297. * node in the first (%UBIFS_MST_LNUM). A power cut happens,
  298. * so this LEB ends up with some amount of garbage at the
  299. * end.
  300. * 3. UBIFS is being mounted R/O. We reach this place and
  301. * recover the master node from the second LEB
  302. * (%UBIFS_MST_LNUM + 1). But we cannot update the media
  303. * because we are being mounted R/O. We have to defer the
  304. * operation.
  305. * 4. However, this master node (@c->mst_node) is marked as
  306. * clean (since the step 1). And if we just return, the
  307. * mount code will be confused and won't recover the master
  308. * node when it is re-mounter R/W later.
  309. *
  310. * Thus, to force the recovery by marking the master node as
  311. * dirty.
  312. */
  313. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  314. #ifndef __UBOOT__
  315. } else {
  316. /* Write the recovered master node */
  317. c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
  318. err = write_rcvrd_mst_node(c, c->mst_node);
  319. if (err)
  320. goto out_free;
  321. #endif
  322. }
  323. vfree(buf2);
  324. vfree(buf1);
  325. return 0;
  326. out_err:
  327. err = -EINVAL;
  328. out_free:
  329. ubifs_err(c, "failed to recover master node");
  330. if (mst1) {
  331. ubifs_err(c, "dumping first master node");
  332. ubifs_dump_node(c, mst1);
  333. }
  334. if (mst2) {
  335. ubifs_err(c, "dumping second master node");
  336. ubifs_dump_node(c, mst2);
  337. }
  338. vfree(buf2);
  339. vfree(buf1);
  340. return err;
  341. }
  342. /**
  343. * ubifs_write_rcvrd_mst_node - write the recovered master node.
  344. * @c: UBIFS file-system description object
  345. *
  346. * This function writes the master node that was recovered during mounting in
  347. * read-only mode and must now be written because we are remounting rw.
  348. *
  349. * This function returns %0 on success and a negative error code on failure.
  350. */
  351. int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
  352. {
  353. int err;
  354. if (!c->rcvrd_mst_node)
  355. return 0;
  356. c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  357. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  358. err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
  359. if (err)
  360. return err;
  361. kfree(c->rcvrd_mst_node);
  362. c->rcvrd_mst_node = NULL;
  363. return 0;
  364. }
  365. /**
  366. * is_last_write - determine if an offset was in the last write to a LEB.
  367. * @c: UBIFS file-system description object
  368. * @buf: buffer to check
  369. * @offs: offset to check
  370. *
  371. * This function returns %1 if @offs was in the last write to the LEB whose data
  372. * is in @buf, otherwise %0 is returned. The determination is made by checking
  373. * for subsequent empty space starting from the next @c->max_write_size
  374. * boundary.
  375. */
  376. static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
  377. {
  378. int empty_offs, check_len;
  379. uint8_t *p;
  380. /*
  381. * Round up to the next @c->max_write_size boundary i.e. @offs is in
  382. * the last wbuf written. After that should be empty space.
  383. */
  384. empty_offs = ALIGN(offs + 1, c->max_write_size);
  385. check_len = c->leb_size - empty_offs;
  386. p = buf + empty_offs - offs;
  387. return is_empty(p, check_len);
  388. }
  389. /**
  390. * clean_buf - clean the data from an LEB sitting in a buffer.
  391. * @c: UBIFS file-system description object
  392. * @buf: buffer to clean
  393. * @lnum: LEB number to clean
  394. * @offs: offset from which to clean
  395. * @len: length of buffer
  396. *
  397. * This function pads up to the next min_io_size boundary (if there is one) and
  398. * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
  399. * @c->min_io_size boundary.
  400. */
  401. static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
  402. int *offs, int *len)
  403. {
  404. int empty_offs, pad_len;
  405. lnum = lnum;
  406. dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
  407. ubifs_assert(!(*offs & 7));
  408. empty_offs = ALIGN(*offs, c->min_io_size);
  409. pad_len = empty_offs - *offs;
  410. ubifs_pad(c, *buf, pad_len);
  411. *offs += pad_len;
  412. *buf += pad_len;
  413. *len -= pad_len;
  414. memset(*buf, 0xff, c->leb_size - empty_offs);
  415. }
  416. /**
  417. * no_more_nodes - determine if there are no more nodes in a buffer.
  418. * @c: UBIFS file-system description object
  419. * @buf: buffer to check
  420. * @len: length of buffer
  421. * @lnum: LEB number of the LEB from which @buf was read
  422. * @offs: offset from which @buf was read
  423. *
  424. * This function ensures that the corrupted node at @offs is the last thing
  425. * written to a LEB. This function returns %1 if more data is not found and
  426. * %0 if more data is found.
  427. */
  428. static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
  429. int lnum, int offs)
  430. {
  431. struct ubifs_ch *ch = buf;
  432. int skip, dlen = le32_to_cpu(ch->len);
  433. /* Check for empty space after the corrupt node's common header */
  434. skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs;
  435. if (is_empty(buf + skip, len - skip))
  436. return 1;
  437. /*
  438. * The area after the common header size is not empty, so the common
  439. * header must be intact. Check it.
  440. */
  441. if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) {
  442. dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
  443. return 0;
  444. }
  445. /* Now we know the corrupt node's length we can skip over it */
  446. skip = ALIGN(offs + dlen, c->max_write_size) - offs;
  447. /* After which there should be empty space */
  448. if (is_empty(buf + skip, len - skip))
  449. return 1;
  450. dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
  451. return 0;
  452. }
  453. /**
  454. * fix_unclean_leb - fix an unclean LEB.
  455. * @c: UBIFS file-system description object
  456. * @sleb: scanned LEB information
  457. * @start: offset where scan started
  458. */
  459. static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  460. int start)
  461. {
  462. int lnum = sleb->lnum, endpt = start;
  463. /* Get the end offset of the last node we are keeping */
  464. if (!list_empty(&sleb->nodes)) {
  465. struct ubifs_scan_node *snod;
  466. snod = list_entry(sleb->nodes.prev,
  467. struct ubifs_scan_node, list);
  468. endpt = snod->offs + snod->len;
  469. }
  470. if (c->ro_mount && !c->remounting_rw) {
  471. /* Add to recovery list */
  472. struct ubifs_unclean_leb *ucleb;
  473. dbg_rcvry("need to fix LEB %d start %d endpt %d",
  474. lnum, start, sleb->endpt);
  475. ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
  476. if (!ucleb)
  477. return -ENOMEM;
  478. ucleb->lnum = lnum;
  479. ucleb->endpt = endpt;
  480. list_add_tail(&ucleb->list, &c->unclean_leb_list);
  481. #ifndef __UBOOT__
  482. } else {
  483. /* Write the fixed LEB back to flash */
  484. int err;
  485. dbg_rcvry("fixing LEB %d start %d endpt %d",
  486. lnum, start, sleb->endpt);
  487. if (endpt == 0) {
  488. err = ubifs_leb_unmap(c, lnum);
  489. if (err)
  490. return err;
  491. } else {
  492. int len = ALIGN(endpt, c->min_io_size);
  493. if (start) {
  494. err = ubifs_leb_read(c, lnum, sleb->buf, 0,
  495. start, 1);
  496. if (err)
  497. return err;
  498. }
  499. /* Pad to min_io_size */
  500. if (len > endpt) {
  501. int pad_len = len - ALIGN(endpt, 8);
  502. if (pad_len > 0) {
  503. void *buf = sleb->buf + len - pad_len;
  504. ubifs_pad(c, buf, pad_len);
  505. }
  506. }
  507. err = ubifs_leb_change(c, lnum, sleb->buf, len);
  508. if (err)
  509. return err;
  510. }
  511. #endif
  512. }
  513. return 0;
  514. }
  515. /**
  516. * drop_last_group - drop the last group of nodes.
  517. * @sleb: scanned LEB information
  518. * @offs: offset of dropped nodes is returned here
  519. *
  520. * This is a helper function for 'ubifs_recover_leb()' which drops the last
  521. * group of nodes of the scanned LEB.
  522. */
  523. static void drop_last_group(struct ubifs_scan_leb *sleb, int *offs)
  524. {
  525. while (!list_empty(&sleb->nodes)) {
  526. struct ubifs_scan_node *snod;
  527. struct ubifs_ch *ch;
  528. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  529. list);
  530. ch = snod->node;
  531. if (ch->group_type != UBIFS_IN_NODE_GROUP)
  532. break;
  533. dbg_rcvry("dropping grouped node at %d:%d",
  534. sleb->lnum, snod->offs);
  535. *offs = snod->offs;
  536. list_del(&snod->list);
  537. kfree(snod);
  538. sleb->nodes_cnt -= 1;
  539. }
  540. }
  541. /**
  542. * drop_last_node - drop the last node.
  543. * @sleb: scanned LEB information
  544. * @offs: offset of dropped nodes is returned here
  545. *
  546. * This is a helper function for 'ubifs_recover_leb()' which drops the last
  547. * node of the scanned LEB.
  548. */
  549. static void drop_last_node(struct ubifs_scan_leb *sleb, int *offs)
  550. {
  551. struct ubifs_scan_node *snod;
  552. if (!list_empty(&sleb->nodes)) {
  553. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  554. list);
  555. dbg_rcvry("dropping last node at %d:%d",
  556. sleb->lnum, snod->offs);
  557. *offs = snod->offs;
  558. list_del(&snod->list);
  559. kfree(snod);
  560. sleb->nodes_cnt -= 1;
  561. }
  562. }
  563. /**
  564. * ubifs_recover_leb - scan and recover a LEB.
  565. * @c: UBIFS file-system description object
  566. * @lnum: LEB number
  567. * @offs: offset
  568. * @sbuf: LEB-sized buffer to use
  569. * @jhead: journal head number this LEB belongs to (%-1 if the LEB does not
  570. * belong to any journal head)
  571. *
  572. * This function does a scan of a LEB, but caters for errors that might have
  573. * been caused by the unclean unmount from which we are attempting to recover.
  574. * Returns the scanned information on success and a negative error code on
  575. * failure.
  576. */
  577. struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
  578. int offs, void *sbuf, int jhead)
  579. {
  580. int ret = 0, err, len = c->leb_size - offs, start = offs, min_io_unit;
  581. int grouped = jhead == -1 ? 0 : c->jheads[jhead].grouped;
  582. struct ubifs_scan_leb *sleb;
  583. void *buf = sbuf + offs;
  584. dbg_rcvry("%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped);
  585. sleb = ubifs_start_scan(c, lnum, offs, sbuf);
  586. if (IS_ERR(sleb))
  587. return sleb;
  588. ubifs_assert(len >= 8);
  589. while (len >= 8) {
  590. dbg_scan("look at LEB %d:%d (%d bytes left)",
  591. lnum, offs, len);
  592. cond_resched();
  593. /*
  594. * Scan quietly until there is an error from which we cannot
  595. * recover
  596. */
  597. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
  598. if (ret == SCANNED_A_NODE) {
  599. /* A valid node, and not a padding node */
  600. struct ubifs_ch *ch = buf;
  601. int node_len;
  602. err = ubifs_add_snod(c, sleb, buf, offs);
  603. if (err)
  604. goto error;
  605. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  606. offs += node_len;
  607. buf += node_len;
  608. len -= node_len;
  609. } else if (ret > 0) {
  610. /* Padding bytes or a valid padding node */
  611. offs += ret;
  612. buf += ret;
  613. len -= ret;
  614. } else if (ret == SCANNED_EMPTY_SPACE ||
  615. ret == SCANNED_GARBAGE ||
  616. ret == SCANNED_A_BAD_PAD_NODE ||
  617. ret == SCANNED_A_CORRUPT_NODE) {
  618. dbg_rcvry("found corruption (%d) at %d:%d",
  619. ret, lnum, offs);
  620. break;
  621. } else {
  622. ubifs_err(c, "unexpected return value %d", ret);
  623. err = -EINVAL;
  624. goto error;
  625. }
  626. }
  627. if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE) {
  628. if (!is_last_write(c, buf, offs))
  629. goto corrupted_rescan;
  630. } else if (ret == SCANNED_A_CORRUPT_NODE) {
  631. if (!no_more_nodes(c, buf, len, lnum, offs))
  632. goto corrupted_rescan;
  633. } else if (!is_empty(buf, len)) {
  634. if (!is_last_write(c, buf, offs)) {
  635. int corruption = first_non_ff(buf, len);
  636. /*
  637. * See header comment for this file for more
  638. * explanations about the reasons we have this check.
  639. */
  640. ubifs_err(c, "corrupt empty space LEB %d:%d, corruption starts at %d",
  641. lnum, offs, corruption);
  642. /* Make sure we dump interesting non-0xFF data */
  643. offs += corruption;
  644. buf += corruption;
  645. goto corrupted;
  646. }
  647. }
  648. min_io_unit = round_down(offs, c->min_io_size);
  649. if (grouped)
  650. /*
  651. * If nodes are grouped, always drop the incomplete group at
  652. * the end.
  653. */
  654. drop_last_group(sleb, &offs);
  655. if (jhead == GCHD) {
  656. /*
  657. * If this LEB belongs to the GC head then while we are in the
  658. * middle of the same min. I/O unit keep dropping nodes. So
  659. * basically, what we want is to make sure that the last min.
  660. * I/O unit where we saw the corruption is dropped completely
  661. * with all the uncorrupted nodes which may possibly sit there.
  662. *
  663. * In other words, let's name the min. I/O unit where the
  664. * corruption starts B, and the previous min. I/O unit A. The
  665. * below code tries to deal with a situation when half of B
  666. * contains valid nodes or the end of a valid node, and the
  667. * second half of B contains corrupted data or garbage. This
  668. * means that UBIFS had been writing to B just before the power
  669. * cut happened. I do not know how realistic is this scenario
  670. * that half of the min. I/O unit had been written successfully
  671. * and the other half not, but this is possible in our 'failure
  672. * mode emulation' infrastructure at least.
  673. *
  674. * So what is the problem, why we need to drop those nodes? Why
  675. * can't we just clean-up the second half of B by putting a
  676. * padding node there? We can, and this works fine with one
  677. * exception which was reproduced with power cut emulation
  678. * testing and happens extremely rarely.
  679. *
  680. * Imagine the file-system is full, we run GC which starts
  681. * moving valid nodes from LEB X to LEB Y (obviously, LEB Y is
  682. * the current GC head LEB). The @c->gc_lnum is -1, which means
  683. * that GC will retain LEB X and will try to continue. Imagine
  684. * that LEB X is currently the dirtiest LEB, and the amount of
  685. * used space in LEB Y is exactly the same as amount of free
  686. * space in LEB X.
  687. *
  688. * And a power cut happens when nodes are moved from LEB X to
  689. * LEB Y. We are here trying to recover LEB Y which is the GC
  690. * head LEB. We find the min. I/O unit B as described above.
  691. * Then we clean-up LEB Y by padding min. I/O unit. And later
  692. * 'ubifs_rcvry_gc_commit()' function fails, because it cannot
  693. * find a dirty LEB which could be GC'd into LEB Y! Even LEB X
  694. * does not match because the amount of valid nodes there does
  695. * not fit the free space in LEB Y any more! And this is
  696. * because of the padding node which we added to LEB Y. The
  697. * user-visible effect of this which I once observed and
  698. * analysed is that we cannot mount the file-system with
  699. * -ENOSPC error.
  700. *
  701. * So obviously, to make sure that situation does not happen we
  702. * should free min. I/O unit B in LEB Y completely and the last
  703. * used min. I/O unit in LEB Y should be A. This is basically
  704. * what the below code tries to do.
  705. */
  706. while (offs > min_io_unit)
  707. drop_last_node(sleb, &offs);
  708. }
  709. buf = sbuf + offs;
  710. len = c->leb_size - offs;
  711. clean_buf(c, &buf, lnum, &offs, &len);
  712. ubifs_end_scan(c, sleb, lnum, offs);
  713. err = fix_unclean_leb(c, sleb, start);
  714. if (err)
  715. goto error;
  716. return sleb;
  717. corrupted_rescan:
  718. /* Re-scan the corrupted data with verbose messages */
  719. ubifs_err(c, "corruption %d", ret);
  720. ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
  721. corrupted:
  722. ubifs_scanned_corruption(c, lnum, offs, buf);
  723. err = -EUCLEAN;
  724. error:
  725. ubifs_err(c, "LEB %d scanning failed", lnum);
  726. ubifs_scan_destroy(sleb);
  727. return ERR_PTR(err);
  728. }
  729. /**
  730. * get_cs_sqnum - get commit start sequence number.
  731. * @c: UBIFS file-system description object
  732. * @lnum: LEB number of commit start node
  733. * @offs: offset of commit start node
  734. * @cs_sqnum: commit start sequence number is returned here
  735. *
  736. * This function returns %0 on success and a negative error code on failure.
  737. */
  738. static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
  739. unsigned long long *cs_sqnum)
  740. {
  741. struct ubifs_cs_node *cs_node = NULL;
  742. int err, ret;
  743. dbg_rcvry("at %d:%d", lnum, offs);
  744. cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
  745. if (!cs_node)
  746. return -ENOMEM;
  747. if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
  748. goto out_err;
  749. err = ubifs_leb_read(c, lnum, (void *)cs_node, offs,
  750. UBIFS_CS_NODE_SZ, 0);
  751. if (err && err != -EBADMSG)
  752. goto out_free;
  753. ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
  754. if (ret != SCANNED_A_NODE) {
  755. ubifs_err(c, "Not a valid node");
  756. goto out_err;
  757. }
  758. if (cs_node->ch.node_type != UBIFS_CS_NODE) {
  759. ubifs_err(c, "Node a CS node, type is %d", cs_node->ch.node_type);
  760. goto out_err;
  761. }
  762. if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
  763. ubifs_err(c, "CS node cmt_no %llu != current cmt_no %llu",
  764. (unsigned long long)le64_to_cpu(cs_node->cmt_no),
  765. c->cmt_no);
  766. goto out_err;
  767. }
  768. *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
  769. dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
  770. kfree(cs_node);
  771. return 0;
  772. out_err:
  773. err = -EINVAL;
  774. out_free:
  775. ubifs_err(c, "failed to get CS sqnum");
  776. kfree(cs_node);
  777. return err;
  778. }
  779. /**
  780. * ubifs_recover_log_leb - scan and recover a log LEB.
  781. * @c: UBIFS file-system description object
  782. * @lnum: LEB number
  783. * @offs: offset
  784. * @sbuf: LEB-sized buffer to use
  785. *
  786. * This function does a scan of a LEB, but caters for errors that might have
  787. * been caused by unclean reboots from which we are attempting to recover
  788. * (assume that only the last log LEB can be corrupted by an unclean reboot).
  789. *
  790. * This function returns %0 on success and a negative error code on failure.
  791. */
  792. struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
  793. int offs, void *sbuf)
  794. {
  795. struct ubifs_scan_leb *sleb;
  796. int next_lnum;
  797. dbg_rcvry("LEB %d", lnum);
  798. next_lnum = lnum + 1;
  799. if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  800. next_lnum = UBIFS_LOG_LNUM;
  801. if (next_lnum != c->ltail_lnum) {
  802. /*
  803. * We can only recover at the end of the log, so check that the
  804. * next log LEB is empty or out of date.
  805. */
  806. sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0);
  807. if (IS_ERR(sleb))
  808. return sleb;
  809. if (sleb->nodes_cnt) {
  810. struct ubifs_scan_node *snod;
  811. unsigned long long cs_sqnum = c->cs_sqnum;
  812. snod = list_entry(sleb->nodes.next,
  813. struct ubifs_scan_node, list);
  814. if (cs_sqnum == 0) {
  815. int err;
  816. err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
  817. if (err) {
  818. ubifs_scan_destroy(sleb);
  819. return ERR_PTR(err);
  820. }
  821. }
  822. if (snod->sqnum > cs_sqnum) {
  823. ubifs_err(c, "unrecoverable log corruption in LEB %d",
  824. lnum);
  825. ubifs_scan_destroy(sleb);
  826. return ERR_PTR(-EUCLEAN);
  827. }
  828. }
  829. ubifs_scan_destroy(sleb);
  830. }
  831. return ubifs_recover_leb(c, lnum, offs, sbuf, -1);
  832. }
  833. /**
  834. * recover_head - recover a head.
  835. * @c: UBIFS file-system description object
  836. * @lnum: LEB number of head to recover
  837. * @offs: offset of head to recover
  838. * @sbuf: LEB-sized buffer to use
  839. *
  840. * This function ensures that there is no data on the flash at a head location.
  841. *
  842. * This function returns %0 on success and a negative error code on failure.
  843. */
  844. static int recover_head(struct ubifs_info *c, int lnum, int offs, void *sbuf)
  845. {
  846. int len = c->max_write_size, err;
  847. if (offs + len > c->leb_size)
  848. len = c->leb_size - offs;
  849. if (!len)
  850. return 0;
  851. /* Read at the head location and check it is empty flash */
  852. err = ubifs_leb_read(c, lnum, sbuf, offs, len, 1);
  853. if (err || !is_empty(sbuf, len)) {
  854. dbg_rcvry("cleaning head at %d:%d", lnum, offs);
  855. if (offs == 0)
  856. return ubifs_leb_unmap(c, lnum);
  857. err = ubifs_leb_read(c, lnum, sbuf, 0, offs, 1);
  858. if (err)
  859. return err;
  860. return ubifs_leb_change(c, lnum, sbuf, offs);
  861. }
  862. return 0;
  863. }
  864. /**
  865. * ubifs_recover_inl_heads - recover index and LPT heads.
  866. * @c: UBIFS file-system description object
  867. * @sbuf: LEB-sized buffer to use
  868. *
  869. * This function ensures that there is no data on the flash at the index and
  870. * LPT head locations.
  871. *
  872. * This deals with the recovery of a half-completed journal commit. UBIFS is
  873. * careful never to overwrite the last version of the index or the LPT. Because
  874. * the index and LPT are wandering trees, data from a half-completed commit will
  875. * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
  876. * assumed to be empty and will be unmapped anyway before use, or in the index
  877. * and LPT heads.
  878. *
  879. * This function returns %0 on success and a negative error code on failure.
  880. */
  881. int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf)
  882. {
  883. int err;
  884. ubifs_assert(!c->ro_mount || c->remounting_rw);
  885. dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
  886. err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
  887. if (err)
  888. return err;
  889. dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
  890. return recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
  891. }
  892. /**
  893. * clean_an_unclean_leb - read and write a LEB to remove corruption.
  894. * @c: UBIFS file-system description object
  895. * @ucleb: unclean LEB information
  896. * @sbuf: LEB-sized buffer to use
  897. *
  898. * This function reads a LEB up to a point pre-determined by the mount recovery,
  899. * checks the nodes, and writes the result back to the flash, thereby cleaning
  900. * off any following corruption, or non-fatal ECC errors.
  901. *
  902. * This function returns %0 on success and a negative error code on failure.
  903. */
  904. static int clean_an_unclean_leb(struct ubifs_info *c,
  905. struct ubifs_unclean_leb *ucleb, void *sbuf)
  906. {
  907. int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
  908. void *buf = sbuf;
  909. dbg_rcvry("LEB %d len %d", lnum, len);
  910. if (len == 0) {
  911. /* Nothing to read, just unmap it */
  912. return ubifs_leb_unmap(c, lnum);
  913. }
  914. err = ubifs_leb_read(c, lnum, buf, offs, len, 0);
  915. if (err && err != -EBADMSG)
  916. return err;
  917. while (len >= 8) {
  918. int ret;
  919. cond_resched();
  920. /* Scan quietly until there is an error */
  921. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  922. if (ret == SCANNED_A_NODE) {
  923. /* A valid node, and not a padding node */
  924. struct ubifs_ch *ch = buf;
  925. int node_len;
  926. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  927. offs += node_len;
  928. buf += node_len;
  929. len -= node_len;
  930. continue;
  931. }
  932. if (ret > 0) {
  933. /* Padding bytes or a valid padding node */
  934. offs += ret;
  935. buf += ret;
  936. len -= ret;
  937. continue;
  938. }
  939. if (ret == SCANNED_EMPTY_SPACE) {
  940. ubifs_err(c, "unexpected empty space at %d:%d",
  941. lnum, offs);
  942. return -EUCLEAN;
  943. }
  944. if (quiet) {
  945. /* Redo the last scan but noisily */
  946. quiet = 0;
  947. continue;
  948. }
  949. ubifs_scanned_corruption(c, lnum, offs, buf);
  950. return -EUCLEAN;
  951. }
  952. /* Pad to min_io_size */
  953. len = ALIGN(ucleb->endpt, c->min_io_size);
  954. if (len > ucleb->endpt) {
  955. int pad_len = len - ALIGN(ucleb->endpt, 8);
  956. if (pad_len > 0) {
  957. buf = c->sbuf + len - pad_len;
  958. ubifs_pad(c, buf, pad_len);
  959. }
  960. }
  961. /* Write back the LEB atomically */
  962. err = ubifs_leb_change(c, lnum, sbuf, len);
  963. if (err)
  964. return err;
  965. dbg_rcvry("cleaned LEB %d", lnum);
  966. return 0;
  967. }
  968. /**
  969. * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
  970. * @c: UBIFS file-system description object
  971. * @sbuf: LEB-sized buffer to use
  972. *
  973. * This function cleans a LEB identified during recovery that needs to be
  974. * written but was not because UBIFS was mounted read-only. This happens when
  975. * remounting to read-write mode.
  976. *
  977. * This function returns %0 on success and a negative error code on failure.
  978. */
  979. int ubifs_clean_lebs(struct ubifs_info *c, void *sbuf)
  980. {
  981. dbg_rcvry("recovery");
  982. while (!list_empty(&c->unclean_leb_list)) {
  983. struct ubifs_unclean_leb *ucleb;
  984. int err;
  985. ucleb = list_entry(c->unclean_leb_list.next,
  986. struct ubifs_unclean_leb, list);
  987. err = clean_an_unclean_leb(c, ucleb, sbuf);
  988. if (err)
  989. return err;
  990. list_del(&ucleb->list);
  991. kfree(ucleb);
  992. }
  993. return 0;
  994. }
  995. #ifndef __UBOOT__
  996. /**
  997. * grab_empty_leb - grab an empty LEB to use as GC LEB and run commit.
  998. * @c: UBIFS file-system description object
  999. *
  1000. * This is a helper function for 'ubifs_rcvry_gc_commit()' which grabs an empty
  1001. * LEB to be used as GC LEB (@c->gc_lnum), and then runs the commit. Returns
  1002. * zero in case of success and a negative error code in case of failure.
  1003. */
  1004. static int grab_empty_leb(struct ubifs_info *c)
  1005. {
  1006. int lnum, err;
  1007. /*
  1008. * Note, it is very important to first search for an empty LEB and then
  1009. * run the commit, not vice-versa. The reason is that there might be
  1010. * only one empty LEB at the moment, the one which has been the
  1011. * @c->gc_lnum just before the power cut happened. During the regular
  1012. * UBIFS operation (not now) @c->gc_lnum is marked as "taken", so no
  1013. * one but GC can grab it. But at this moment this single empty LEB is
  1014. * not marked as taken, so if we run commit - what happens? Right, the
  1015. * commit will grab it and write the index there. Remember that the
  1016. * index always expands as long as there is free space, and it only
  1017. * starts consolidating when we run out of space.
  1018. *
  1019. * IOW, if we run commit now, we might not be able to find a free LEB
  1020. * after this.
  1021. */
  1022. lnum = ubifs_find_free_leb_for_idx(c);
  1023. if (lnum < 0) {
  1024. ubifs_err(c, "could not find an empty LEB");
  1025. ubifs_dump_lprops(c);
  1026. ubifs_dump_budg(c, &c->bi);
  1027. return lnum;
  1028. }
  1029. /* Reset the index flag */
  1030. err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
  1031. LPROPS_INDEX, 0);
  1032. if (err)
  1033. return err;
  1034. c->gc_lnum = lnum;
  1035. dbg_rcvry("found empty LEB %d, run commit", lnum);
  1036. return ubifs_run_commit(c);
  1037. }
  1038. /**
  1039. * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
  1040. * @c: UBIFS file-system description object
  1041. *
  1042. * Out-of-place garbage collection requires always one empty LEB with which to
  1043. * start garbage collection. The LEB number is recorded in c->gc_lnum and is
  1044. * written to the master node on unmounting. In the case of an unclean unmount
  1045. * the value of gc_lnum recorded in the master node is out of date and cannot
  1046. * be used. Instead, recovery must allocate an empty LEB for this purpose.
  1047. * However, there may not be enough empty space, in which case it must be
  1048. * possible to GC the dirtiest LEB into the GC head LEB.
  1049. *
  1050. * This function also runs the commit which causes the TNC updates from
  1051. * size-recovery and orphans to be written to the flash. That is important to
  1052. * ensure correct replay order for subsequent mounts.
  1053. *
  1054. * This function returns %0 on success and a negative error code on failure.
  1055. */
  1056. int ubifs_rcvry_gc_commit(struct ubifs_info *c)
  1057. {
  1058. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  1059. struct ubifs_lprops lp;
  1060. int err;
  1061. dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs);
  1062. c->gc_lnum = -1;
  1063. if (wbuf->lnum == -1 || wbuf->offs == c->leb_size)
  1064. return grab_empty_leb(c);
  1065. err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
  1066. if (err) {
  1067. if (err != -ENOSPC)
  1068. return err;
  1069. dbg_rcvry("could not find a dirty LEB");
  1070. return grab_empty_leb(c);
  1071. }
  1072. ubifs_assert(!(lp.flags & LPROPS_INDEX));
  1073. ubifs_assert(lp.free + lp.dirty >= wbuf->offs);
  1074. /*
  1075. * We run the commit before garbage collection otherwise subsequent
  1076. * mounts will see the GC and orphan deletion in a different order.
  1077. */
  1078. dbg_rcvry("committing");
  1079. err = ubifs_run_commit(c);
  1080. if (err)
  1081. return err;
  1082. dbg_rcvry("GC'ing LEB %d", lp.lnum);
  1083. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  1084. err = ubifs_garbage_collect_leb(c, &lp);
  1085. if (err >= 0) {
  1086. int err2 = ubifs_wbuf_sync_nolock(wbuf);
  1087. if (err2)
  1088. err = err2;
  1089. }
  1090. mutex_unlock(&wbuf->io_mutex);
  1091. if (err < 0) {
  1092. ubifs_err(c, "GC failed, error %d", err);
  1093. if (err == -EAGAIN)
  1094. err = -EINVAL;
  1095. return err;
  1096. }
  1097. ubifs_assert(err == LEB_RETAINED);
  1098. if (err != LEB_RETAINED)
  1099. return -EINVAL;
  1100. err = ubifs_leb_unmap(c, c->gc_lnum);
  1101. if (err)
  1102. return err;
  1103. dbg_rcvry("allocated LEB %d for GC", lp.lnum);
  1104. return 0;
  1105. }
  1106. #else
  1107. int ubifs_rcvry_gc_commit(struct ubifs_info *c)
  1108. {
  1109. return 0;
  1110. }
  1111. #endif
  1112. /**
  1113. * struct size_entry - inode size information for recovery.
  1114. * @rb: link in the RB-tree of sizes
  1115. * @inum: inode number
  1116. * @i_size: size on inode
  1117. * @d_size: maximum size based on data nodes
  1118. * @exists: indicates whether the inode exists
  1119. * @inode: inode if pinned in memory awaiting rw mode to fix it
  1120. */
  1121. struct size_entry {
  1122. struct rb_node rb;
  1123. ino_t inum;
  1124. loff_t i_size;
  1125. loff_t d_size;
  1126. int exists;
  1127. struct inode *inode;
  1128. };
  1129. /**
  1130. * add_ino - add an entry to the size tree.
  1131. * @c: UBIFS file-system description object
  1132. * @inum: inode number
  1133. * @i_size: size on inode
  1134. * @d_size: maximum size based on data nodes
  1135. * @exists: indicates whether the inode exists
  1136. */
  1137. static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
  1138. loff_t d_size, int exists)
  1139. {
  1140. struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
  1141. struct size_entry *e;
  1142. while (*p) {
  1143. parent = *p;
  1144. e = rb_entry(parent, struct size_entry, rb);
  1145. if (inum < e->inum)
  1146. p = &(*p)->rb_left;
  1147. else
  1148. p = &(*p)->rb_right;
  1149. }
  1150. e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
  1151. if (!e)
  1152. return -ENOMEM;
  1153. e->inum = inum;
  1154. e->i_size = i_size;
  1155. e->d_size = d_size;
  1156. e->exists = exists;
  1157. rb_link_node(&e->rb, parent, p);
  1158. rb_insert_color(&e->rb, &c->size_tree);
  1159. return 0;
  1160. }
  1161. /**
  1162. * find_ino - find an entry on the size tree.
  1163. * @c: UBIFS file-system description object
  1164. * @inum: inode number
  1165. */
  1166. static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
  1167. {
  1168. struct rb_node *p = c->size_tree.rb_node;
  1169. struct size_entry *e;
  1170. while (p) {
  1171. e = rb_entry(p, struct size_entry, rb);
  1172. if (inum < e->inum)
  1173. p = p->rb_left;
  1174. else if (inum > e->inum)
  1175. p = p->rb_right;
  1176. else
  1177. return e;
  1178. }
  1179. return NULL;
  1180. }
  1181. /**
  1182. * remove_ino - remove an entry from the size tree.
  1183. * @c: UBIFS file-system description object
  1184. * @inum: inode number
  1185. */
  1186. static void remove_ino(struct ubifs_info *c, ino_t inum)
  1187. {
  1188. struct size_entry *e = find_ino(c, inum);
  1189. if (!e)
  1190. return;
  1191. rb_erase(&e->rb, &c->size_tree);
  1192. kfree(e);
  1193. }
  1194. /**
  1195. * ubifs_destroy_size_tree - free resources related to the size tree.
  1196. * @c: UBIFS file-system description object
  1197. */
  1198. void ubifs_destroy_size_tree(struct ubifs_info *c)
  1199. {
  1200. struct size_entry *e, *n;
  1201. rbtree_postorder_for_each_entry_safe(e, n, &c->size_tree, rb) {
  1202. if (e->inode)
  1203. iput(e->inode);
  1204. kfree(e);
  1205. }
  1206. c->size_tree = RB_ROOT;
  1207. }
  1208. /**
  1209. * ubifs_recover_size_accum - accumulate inode sizes for recovery.
  1210. * @c: UBIFS file-system description object
  1211. * @key: node key
  1212. * @deletion: node is for a deletion
  1213. * @new_size: inode size
  1214. *
  1215. * This function has two purposes:
  1216. * 1) to ensure there are no data nodes that fall outside the inode size
  1217. * 2) to ensure there are no data nodes for inodes that do not exist
  1218. * To accomplish those purposes, a rb-tree is constructed containing an entry
  1219. * for each inode number in the journal that has not been deleted, and recording
  1220. * the size from the inode node, the maximum size of any data node (also altered
  1221. * by truncations) and a flag indicating a inode number for which no inode node
  1222. * was present in the journal.
  1223. *
  1224. * Note that there is still the possibility that there are data nodes that have
  1225. * been committed that are beyond the inode size, however the only way to find
  1226. * them would be to scan the entire index. Alternatively, some provision could
  1227. * be made to record the size of inodes at the start of commit, which would seem
  1228. * very cumbersome for a scenario that is quite unlikely and the only negative
  1229. * consequence of which is wasted space.
  1230. *
  1231. * This functions returns %0 on success and a negative error code on failure.
  1232. */
  1233. int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
  1234. int deletion, loff_t new_size)
  1235. {
  1236. ino_t inum = key_inum(c, key);
  1237. struct size_entry *e;
  1238. int err;
  1239. switch (key_type(c, key)) {
  1240. case UBIFS_INO_KEY:
  1241. if (deletion)
  1242. remove_ino(c, inum);
  1243. else {
  1244. e = find_ino(c, inum);
  1245. if (e) {
  1246. e->i_size = new_size;
  1247. e->exists = 1;
  1248. } else {
  1249. err = add_ino(c, inum, new_size, 0, 1);
  1250. if (err)
  1251. return err;
  1252. }
  1253. }
  1254. break;
  1255. case UBIFS_DATA_KEY:
  1256. e = find_ino(c, inum);
  1257. if (e) {
  1258. if (new_size > e->d_size)
  1259. e->d_size = new_size;
  1260. } else {
  1261. err = add_ino(c, inum, 0, new_size, 0);
  1262. if (err)
  1263. return err;
  1264. }
  1265. break;
  1266. case UBIFS_TRUN_KEY:
  1267. e = find_ino(c, inum);
  1268. if (e)
  1269. e->d_size = new_size;
  1270. break;
  1271. }
  1272. return 0;
  1273. }
  1274. #ifndef __UBOOT__
  1275. /**
  1276. * fix_size_in_place - fix inode size in place on flash.
  1277. * @c: UBIFS file-system description object
  1278. * @e: inode size information for recovery
  1279. */
  1280. static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
  1281. {
  1282. struct ubifs_ino_node *ino = c->sbuf;
  1283. unsigned char *p;
  1284. union ubifs_key key;
  1285. int err, lnum, offs, len;
  1286. loff_t i_size;
  1287. uint32_t crc;
  1288. /* Locate the inode node LEB number and offset */
  1289. ino_key_init(c, &key, e->inum);
  1290. err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
  1291. if (err)
  1292. goto out;
  1293. /*
  1294. * If the size recorded on the inode node is greater than the size that
  1295. * was calculated from nodes in the journal then don't change the inode.
  1296. */
  1297. i_size = le64_to_cpu(ino->size);
  1298. if (i_size >= e->d_size)
  1299. return 0;
  1300. /* Read the LEB */
  1301. err = ubifs_leb_read(c, lnum, c->sbuf, 0, c->leb_size, 1);
  1302. if (err)
  1303. goto out;
  1304. /* Change the size field and recalculate the CRC */
  1305. ino = c->sbuf + offs;
  1306. ino->size = cpu_to_le64(e->d_size);
  1307. len = le32_to_cpu(ino->ch.len);
  1308. crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
  1309. ino->ch.crc = cpu_to_le32(crc);
  1310. /* Work out where data in the LEB ends and free space begins */
  1311. p = c->sbuf;
  1312. len = c->leb_size - 1;
  1313. while (p[len] == 0xff)
  1314. len -= 1;
  1315. len = ALIGN(len + 1, c->min_io_size);
  1316. /* Atomically write the fixed LEB back again */
  1317. err = ubifs_leb_change(c, lnum, c->sbuf, len);
  1318. if (err)
  1319. goto out;
  1320. dbg_rcvry("inode %lu at %d:%d size %lld -> %lld",
  1321. (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
  1322. return 0;
  1323. out:
  1324. ubifs_warn(c, "inode %lu failed to fix size %lld -> %lld error %d",
  1325. (unsigned long)e->inum, e->i_size, e->d_size, err);
  1326. return err;
  1327. }
  1328. #endif
  1329. /**
  1330. * ubifs_recover_size - recover inode size.
  1331. * @c: UBIFS file-system description object
  1332. *
  1333. * This function attempts to fix inode size discrepancies identified by the
  1334. * 'ubifs_recover_size_accum()' function.
  1335. *
  1336. * This functions returns %0 on success and a negative error code on failure.
  1337. */
  1338. int ubifs_recover_size(struct ubifs_info *c)
  1339. {
  1340. struct rb_node *this = rb_first(&c->size_tree);
  1341. while (this) {
  1342. struct size_entry *e;
  1343. int err;
  1344. e = rb_entry(this, struct size_entry, rb);
  1345. if (!e->exists) {
  1346. union ubifs_key key;
  1347. ino_key_init(c, &key, e->inum);
  1348. err = ubifs_tnc_lookup(c, &key, c->sbuf);
  1349. if (err && err != -ENOENT)
  1350. return err;
  1351. if (err == -ENOENT) {
  1352. /* Remove data nodes that have no inode */
  1353. dbg_rcvry("removing ino %lu",
  1354. (unsigned long)e->inum);
  1355. err = ubifs_tnc_remove_ino(c, e->inum);
  1356. if (err)
  1357. return err;
  1358. } else {
  1359. struct ubifs_ino_node *ino = c->sbuf;
  1360. e->exists = 1;
  1361. e->i_size = le64_to_cpu(ino->size);
  1362. }
  1363. }
  1364. if (e->exists && e->i_size < e->d_size) {
  1365. if (c->ro_mount) {
  1366. /* Fix the inode size and pin it in memory */
  1367. struct inode *inode;
  1368. struct ubifs_inode *ui;
  1369. ubifs_assert(!e->inode);
  1370. inode = ubifs_iget(c->vfs_sb, e->inum);
  1371. if (IS_ERR(inode))
  1372. return PTR_ERR(inode);
  1373. ui = ubifs_inode(inode);
  1374. if (inode->i_size < e->d_size) {
  1375. dbg_rcvry("ino %lu size %lld -> %lld",
  1376. (unsigned long)e->inum,
  1377. inode->i_size, e->d_size);
  1378. inode->i_size = e->d_size;
  1379. ui->ui_size = e->d_size;
  1380. ui->synced_i_size = e->d_size;
  1381. e->inode = inode;
  1382. this = rb_next(this);
  1383. continue;
  1384. }
  1385. iput(inode);
  1386. #ifndef __UBOOT__
  1387. } else {
  1388. /* Fix the size in place */
  1389. err = fix_size_in_place(c, e);
  1390. if (err)
  1391. return err;
  1392. if (e->inode)
  1393. iput(e->inode);
  1394. #endif
  1395. }
  1396. }
  1397. this = rb_next(this);
  1398. rb_erase(&e->rb, &c->size_tree);
  1399. kfree(e);
  1400. }
  1401. return 0;
  1402. }