recovery.c 42 KB

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