recovery.c 43 KB

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