recovery.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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 = ubi_read(c->ubi, lnum, sbuf, 0, c->leb_size);
  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 = ubi_leb_change(c->ubi, lnum, mst, sz, UBI_SHORTTERM);
  199. if (err)
  200. goto out;
  201. err = ubi_leb_change(c->ubi, lnum + 1, mst, sz, UBI_SHORTTERM);
  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 && offs2 + sz >= c->leb_size) {
  255. /* 1st LEB was unmapped and written, 2nd not */
  256. if (cor1)
  257. goto out_err;
  258. mst = mst1;
  259. } else
  260. goto out_err;
  261. } else {
  262. /*
  263. * 2nd LEB was unmapped and about to be written, so
  264. * there must be only one master node in the first LEB
  265. * and no corruption.
  266. */
  267. if (offs1 != 0 || cor1)
  268. goto out_err;
  269. mst = mst1;
  270. }
  271. } else {
  272. if (!mst2)
  273. goto out_err;
  274. /*
  275. * 1st LEB was unmapped and about to be written, so there must
  276. * be no room left in 2nd LEB.
  277. */
  278. offs2 = (void *)mst2 - buf2;
  279. if (offs2 + sz + sz <= c->leb_size)
  280. goto out_err;
  281. mst = mst2;
  282. }
  283. ubifs_msg("recovered master node from LEB %d",
  284. (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
  285. memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
  286. if (c->ro_mount) {
  287. /* Read-only mode. Keep a copy for switching to rw mode */
  288. c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
  289. if (!c->rcvrd_mst_node) {
  290. err = -ENOMEM;
  291. goto out_free;
  292. }
  293. memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
  294. } else {
  295. /* Write the recovered master node */
  296. c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
  297. err = write_rcvrd_mst_node(c, c->mst_node);
  298. if (err)
  299. goto out_free;
  300. }
  301. vfree(buf2);
  302. vfree(buf1);
  303. return 0;
  304. out_err:
  305. err = -EINVAL;
  306. out_free:
  307. ubifs_err("failed to recover master node");
  308. if (mst1) {
  309. dbg_err("dumping first master node");
  310. dbg_dump_node(c, mst1);
  311. }
  312. if (mst2) {
  313. dbg_err("dumping second master node");
  314. dbg_dump_node(c, mst2);
  315. }
  316. vfree(buf2);
  317. vfree(buf1);
  318. return err;
  319. }
  320. /**
  321. * ubifs_write_rcvrd_mst_node - write the recovered master node.
  322. * @c: UBIFS file-system description object
  323. *
  324. * This function writes the master node that was recovered during mounting in
  325. * read-only mode and must now be written because we are remounting rw.
  326. *
  327. * This function returns %0 on success and a negative error code on failure.
  328. */
  329. int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
  330. {
  331. int err;
  332. if (!c->rcvrd_mst_node)
  333. return 0;
  334. c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  335. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  336. err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
  337. if (err)
  338. return err;
  339. kfree(c->rcvrd_mst_node);
  340. c->rcvrd_mst_node = NULL;
  341. return 0;
  342. }
  343. /**
  344. * is_last_write - determine if an offset was in the last write to a LEB.
  345. * @c: UBIFS file-system description object
  346. * @buf: buffer to check
  347. * @offs: offset to check
  348. *
  349. * This function returns %1 if @offs was in the last write to the LEB whose data
  350. * is in @buf, otherwise %0 is returned. The determination is made by checking
  351. * for subsequent empty space starting from the next @c->max_write_size
  352. * boundary.
  353. */
  354. static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
  355. {
  356. int empty_offs, check_len;
  357. uint8_t *p;
  358. /*
  359. * Round up to the next @c->max_write_size boundary i.e. @offs is in
  360. * the last wbuf written. After that should be empty space.
  361. */
  362. empty_offs = ALIGN(offs + 1, c->max_write_size);
  363. check_len = c->leb_size - empty_offs;
  364. p = buf + empty_offs - offs;
  365. return is_empty(p, check_len);
  366. }
  367. /**
  368. * clean_buf - clean the data from an LEB sitting in a buffer.
  369. * @c: UBIFS file-system description object
  370. * @buf: buffer to clean
  371. * @lnum: LEB number to clean
  372. * @offs: offset from which to clean
  373. * @len: length of buffer
  374. *
  375. * This function pads up to the next min_io_size boundary (if there is one) and
  376. * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
  377. * @c->min_io_size boundary.
  378. */
  379. static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
  380. int *offs, int *len)
  381. {
  382. int empty_offs, pad_len;
  383. lnum = lnum;
  384. dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
  385. ubifs_assert(!(*offs & 7));
  386. empty_offs = ALIGN(*offs, c->min_io_size);
  387. pad_len = empty_offs - *offs;
  388. ubifs_pad(c, *buf, pad_len);
  389. *offs += pad_len;
  390. *buf += pad_len;
  391. *len -= pad_len;
  392. memset(*buf, 0xff, c->leb_size - empty_offs);
  393. }
  394. /**
  395. * no_more_nodes - determine if there are no more nodes in a buffer.
  396. * @c: UBIFS file-system description object
  397. * @buf: buffer to check
  398. * @len: length of buffer
  399. * @lnum: LEB number of the LEB from which @buf was read
  400. * @offs: offset from which @buf was read
  401. *
  402. * This function ensures that the corrupted node at @offs is the last thing
  403. * written to a LEB. This function returns %1 if more data is not found and
  404. * %0 if more data is found.
  405. */
  406. static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
  407. int lnum, int offs)
  408. {
  409. struct ubifs_ch *ch = buf;
  410. int skip, dlen = le32_to_cpu(ch->len);
  411. /* Check for empty space after the corrupt node's common header */
  412. skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs;
  413. if (is_empty(buf + skip, len - skip))
  414. return 1;
  415. /*
  416. * The area after the common header size is not empty, so the common
  417. * header must be intact. Check it.
  418. */
  419. if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) {
  420. dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
  421. return 0;
  422. }
  423. /* Now we know the corrupt node's length we can skip over it */
  424. skip = ALIGN(offs + dlen, c->max_write_size) - offs;
  425. /* After which there should be empty space */
  426. if (is_empty(buf + skip, len - skip))
  427. return 1;
  428. dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
  429. return 0;
  430. }
  431. /**
  432. * fix_unclean_leb - fix an unclean LEB.
  433. * @c: UBIFS file-system description object
  434. * @sleb: scanned LEB information
  435. * @start: offset where scan started
  436. */
  437. static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  438. int start)
  439. {
  440. int lnum = sleb->lnum, endpt = start;
  441. /* Get the end offset of the last node we are keeping */
  442. if (!list_empty(&sleb->nodes)) {
  443. struct ubifs_scan_node *snod;
  444. snod = list_entry(sleb->nodes.prev,
  445. struct ubifs_scan_node, list);
  446. endpt = snod->offs + snod->len;
  447. }
  448. if (c->ro_mount && !c->remounting_rw) {
  449. /* Add to recovery list */
  450. struct ubifs_unclean_leb *ucleb;
  451. dbg_rcvry("need to fix LEB %d start %d endpt %d",
  452. lnum, start, sleb->endpt);
  453. ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
  454. if (!ucleb)
  455. return -ENOMEM;
  456. ucleb->lnum = lnum;
  457. ucleb->endpt = endpt;
  458. list_add_tail(&ucleb->list, &c->unclean_leb_list);
  459. } else {
  460. /* Write the fixed LEB back to flash */
  461. int err;
  462. dbg_rcvry("fixing LEB %d start %d endpt %d",
  463. lnum, start, sleb->endpt);
  464. if (endpt == 0) {
  465. err = ubifs_leb_unmap(c, lnum);
  466. if (err)
  467. return err;
  468. } else {
  469. int len = ALIGN(endpt, c->min_io_size);
  470. if (start) {
  471. err = ubi_read(c->ubi, lnum, sleb->buf, 0,
  472. start);
  473. if (err)
  474. return err;
  475. }
  476. /* Pad to min_io_size */
  477. if (len > endpt) {
  478. int pad_len = len - ALIGN(endpt, 8);
  479. if (pad_len > 0) {
  480. void *buf = sleb->buf + len - pad_len;
  481. ubifs_pad(c, buf, pad_len);
  482. }
  483. }
  484. err = ubi_leb_change(c->ubi, lnum, sleb->buf, len,
  485. UBI_UNKNOWN);
  486. if (err)
  487. return err;
  488. }
  489. }
  490. return 0;
  491. }
  492. /**
  493. * drop_incomplete_group - drop nodes from an incomplete group.
  494. * @sleb: scanned LEB information
  495. * @offs: offset of dropped nodes is returned here
  496. *
  497. * This function returns %1 if nodes are dropped and %0 otherwise.
  498. */
  499. static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
  500. {
  501. int dropped = 0;
  502. while (!list_empty(&sleb->nodes)) {
  503. struct ubifs_scan_node *snod;
  504. struct ubifs_ch *ch;
  505. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  506. list);
  507. ch = snod->node;
  508. if (ch->group_type != UBIFS_IN_NODE_GROUP)
  509. return dropped;
  510. dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
  511. *offs = snod->offs;
  512. list_del(&snod->list);
  513. kfree(snod);
  514. sleb->nodes_cnt -= 1;
  515. dropped = 1;
  516. }
  517. return dropped;
  518. }
  519. /**
  520. * ubifs_recover_leb - scan and recover a LEB.
  521. * @c: UBIFS file-system description object
  522. * @lnum: LEB number
  523. * @offs: offset
  524. * @sbuf: LEB-sized buffer to use
  525. * @grouped: nodes may be grouped for recovery
  526. *
  527. * This function does a scan of a LEB, but caters for errors that might have
  528. * been caused by the unclean unmount from which we are attempting to recover.
  529. * Returns %0 in case of success, %-EUCLEAN if an unrecoverable corruption is
  530. * found, and a negative error code in case of failure.
  531. */
  532. struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
  533. int offs, void *sbuf, int grouped)
  534. {
  535. int err, len = c->leb_size - offs, need_clean = 0, quiet = 1;
  536. int empty_chkd = 0, start = offs;
  537. struct ubifs_scan_leb *sleb;
  538. void *buf = sbuf + offs;
  539. dbg_rcvry("%d:%d", lnum, offs);
  540. sleb = ubifs_start_scan(c, lnum, offs, sbuf);
  541. if (IS_ERR(sleb))
  542. return sleb;
  543. if (sleb->ecc)
  544. need_clean = 1;
  545. while (len >= 8) {
  546. int ret;
  547. dbg_scan("look at LEB %d:%d (%d bytes left)",
  548. lnum, offs, len);
  549. cond_resched();
  550. /*
  551. * Scan quietly until there is an error from which we cannot
  552. * recover
  553. */
  554. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  555. if (ret == SCANNED_A_NODE) {
  556. /* A valid node, and not a padding node */
  557. struct ubifs_ch *ch = buf;
  558. int node_len;
  559. err = ubifs_add_snod(c, sleb, buf, offs);
  560. if (err)
  561. goto error;
  562. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  563. offs += node_len;
  564. buf += node_len;
  565. len -= node_len;
  566. continue;
  567. }
  568. if (ret > 0) {
  569. /* Padding bytes or a valid padding node */
  570. offs += ret;
  571. buf += ret;
  572. len -= ret;
  573. continue;
  574. }
  575. if (ret == SCANNED_EMPTY_SPACE) {
  576. if (!is_empty(buf, len)) {
  577. if (!is_last_write(c, buf, offs))
  578. break;
  579. clean_buf(c, &buf, lnum, &offs, &len);
  580. need_clean = 1;
  581. }
  582. empty_chkd = 1;
  583. break;
  584. }
  585. if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE)
  586. if (is_last_write(c, buf, offs)) {
  587. clean_buf(c, &buf, lnum, &offs, &len);
  588. need_clean = 1;
  589. empty_chkd = 1;
  590. break;
  591. }
  592. if (ret == SCANNED_A_CORRUPT_NODE)
  593. if (no_more_nodes(c, buf, len, lnum, offs)) {
  594. clean_buf(c, &buf, lnum, &offs, &len);
  595. need_clean = 1;
  596. empty_chkd = 1;
  597. break;
  598. }
  599. if (quiet) {
  600. /* Redo the last scan but noisily */
  601. quiet = 0;
  602. continue;
  603. }
  604. switch (ret) {
  605. case SCANNED_GARBAGE:
  606. dbg_err("garbage");
  607. goto corrupted;
  608. case SCANNED_A_CORRUPT_NODE:
  609. case SCANNED_A_BAD_PAD_NODE:
  610. dbg_err("bad node");
  611. goto corrupted;
  612. default:
  613. dbg_err("unknown");
  614. err = -EINVAL;
  615. goto error;
  616. }
  617. }
  618. if (!empty_chkd && !is_empty(buf, len)) {
  619. if (is_last_write(c, buf, offs)) {
  620. clean_buf(c, &buf, lnum, &offs, &len);
  621. need_clean = 1;
  622. } else {
  623. int corruption = first_non_ff(buf, len);
  624. /*
  625. * See header comment for this file for more
  626. * explanations about the reasons we have this check.
  627. */
  628. ubifs_err("corrupt empty space LEB %d:%d, corruption "
  629. "starts at %d", lnum, offs, corruption);
  630. /* Make sure we dump interesting non-0xFF data */
  631. offs += corruption;
  632. buf += corruption;
  633. goto corrupted;
  634. }
  635. }
  636. /* Drop nodes from incomplete group */
  637. if (grouped && drop_incomplete_group(sleb, &offs)) {
  638. buf = sbuf + offs;
  639. len = c->leb_size - offs;
  640. clean_buf(c, &buf, lnum, &offs, &len);
  641. need_clean = 1;
  642. }
  643. if (offs % c->min_io_size) {
  644. clean_buf(c, &buf, lnum, &offs, &len);
  645. need_clean = 1;
  646. }
  647. ubifs_end_scan(c, sleb, lnum, offs);
  648. if (need_clean) {
  649. err = fix_unclean_leb(c, sleb, start);
  650. if (err)
  651. goto error;
  652. }
  653. return sleb;
  654. corrupted:
  655. ubifs_scanned_corruption(c, lnum, offs, buf);
  656. err = -EUCLEAN;
  657. error:
  658. ubifs_err("LEB %d scanning failed", lnum);
  659. ubifs_scan_destroy(sleb);
  660. return ERR_PTR(err);
  661. }
  662. /**
  663. * get_cs_sqnum - get commit start sequence number.
  664. * @c: UBIFS file-system description object
  665. * @lnum: LEB number of commit start node
  666. * @offs: offset of commit start node
  667. * @cs_sqnum: commit start sequence number is returned here
  668. *
  669. * This function returns %0 on success and a negative error code on failure.
  670. */
  671. static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
  672. unsigned long long *cs_sqnum)
  673. {
  674. struct ubifs_cs_node *cs_node = NULL;
  675. int err, ret;
  676. dbg_rcvry("at %d:%d", lnum, offs);
  677. cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
  678. if (!cs_node)
  679. return -ENOMEM;
  680. if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
  681. goto out_err;
  682. err = ubi_read(c->ubi, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ);
  683. if (err && err != -EBADMSG)
  684. goto out_free;
  685. ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
  686. if (ret != SCANNED_A_NODE) {
  687. dbg_err("Not a valid node");
  688. goto out_err;
  689. }
  690. if (cs_node->ch.node_type != UBIFS_CS_NODE) {
  691. dbg_err("Node a CS node, type is %d", cs_node->ch.node_type);
  692. goto out_err;
  693. }
  694. if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
  695. dbg_err("CS node cmt_no %llu != current cmt_no %llu",
  696. (unsigned long long)le64_to_cpu(cs_node->cmt_no),
  697. c->cmt_no);
  698. goto out_err;
  699. }
  700. *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
  701. dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
  702. kfree(cs_node);
  703. return 0;
  704. out_err:
  705. err = -EINVAL;
  706. out_free:
  707. ubifs_err("failed to get CS sqnum");
  708. kfree(cs_node);
  709. return err;
  710. }
  711. /**
  712. * ubifs_recover_log_leb - scan and recover a log LEB.
  713. * @c: UBIFS file-system description object
  714. * @lnum: LEB number
  715. * @offs: offset
  716. * @sbuf: LEB-sized buffer to use
  717. *
  718. * This function does a scan of a LEB, but caters for errors that might have
  719. * been caused by unclean reboots from which we are attempting to recover
  720. * (assume that only the last log LEB can be corrupted by an unclean reboot).
  721. *
  722. * This function returns %0 on success and a negative error code on failure.
  723. */
  724. struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
  725. int offs, void *sbuf)
  726. {
  727. struct ubifs_scan_leb *sleb;
  728. int next_lnum;
  729. dbg_rcvry("LEB %d", lnum);
  730. next_lnum = lnum + 1;
  731. if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  732. next_lnum = UBIFS_LOG_LNUM;
  733. if (next_lnum != c->ltail_lnum) {
  734. /*
  735. * We can only recover at the end of the log, so check that the
  736. * next log LEB is empty or out of date.
  737. */
  738. sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0);
  739. if (IS_ERR(sleb))
  740. return sleb;
  741. if (sleb->nodes_cnt) {
  742. struct ubifs_scan_node *snod;
  743. unsigned long long cs_sqnum = c->cs_sqnum;
  744. snod = list_entry(sleb->nodes.next,
  745. struct ubifs_scan_node, list);
  746. if (cs_sqnum == 0) {
  747. int err;
  748. err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
  749. if (err) {
  750. ubifs_scan_destroy(sleb);
  751. return ERR_PTR(err);
  752. }
  753. }
  754. if (snod->sqnum > cs_sqnum) {
  755. ubifs_err("unrecoverable log corruption "
  756. "in LEB %d", lnum);
  757. ubifs_scan_destroy(sleb);
  758. return ERR_PTR(-EUCLEAN);
  759. }
  760. }
  761. ubifs_scan_destroy(sleb);
  762. }
  763. return ubifs_recover_leb(c, lnum, offs, sbuf, 0);
  764. }
  765. /**
  766. * recover_head - recover a head.
  767. * @c: UBIFS file-system description object
  768. * @lnum: LEB number of head to recover
  769. * @offs: offset of head to recover
  770. * @sbuf: LEB-sized buffer to use
  771. *
  772. * This function ensures that there is no data on the flash at a head location.
  773. *
  774. * This function returns %0 on success and a negative error code on failure.
  775. */
  776. static int recover_head(const struct ubifs_info *c, int lnum, int offs,
  777. void *sbuf)
  778. {
  779. int len = c->max_write_size, err;
  780. if (offs + len > c->leb_size)
  781. len = c->leb_size - offs;
  782. if (!len)
  783. return 0;
  784. /* Read at the head location and check it is empty flash */
  785. err = ubi_read(c->ubi, lnum, sbuf, offs, len);
  786. if (err || !is_empty(sbuf, len)) {
  787. dbg_rcvry("cleaning head at %d:%d", lnum, offs);
  788. if (offs == 0)
  789. return ubifs_leb_unmap(c, lnum);
  790. err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
  791. if (err)
  792. return err;
  793. return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
  794. }
  795. return 0;
  796. }
  797. /**
  798. * ubifs_recover_inl_heads - recover index and LPT heads.
  799. * @c: UBIFS file-system description object
  800. * @sbuf: LEB-sized buffer to use
  801. *
  802. * This function ensures that there is no data on the flash at the index and
  803. * LPT head locations.
  804. *
  805. * This deals with the recovery of a half-completed journal commit. UBIFS is
  806. * careful never to overwrite the last version of the index or the LPT. Because
  807. * the index and LPT are wandering trees, data from a half-completed commit will
  808. * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
  809. * assumed to be empty and will be unmapped anyway before use, or in the index
  810. * and LPT heads.
  811. *
  812. * This function returns %0 on success and a negative error code on failure.
  813. */
  814. int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
  815. {
  816. int err;
  817. ubifs_assert(!c->ro_mount || c->remounting_rw);
  818. dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
  819. err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
  820. if (err)
  821. return err;
  822. dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
  823. err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
  824. if (err)
  825. return err;
  826. return 0;
  827. }
  828. /**
  829. * clean_an_unclean_leb - read and write a LEB to remove corruption.
  830. * @c: UBIFS file-system description object
  831. * @ucleb: unclean LEB information
  832. * @sbuf: LEB-sized buffer to use
  833. *
  834. * This function reads a LEB up to a point pre-determined by the mount recovery,
  835. * checks the nodes, and writes the result back to the flash, thereby cleaning
  836. * off any following corruption, or non-fatal ECC errors.
  837. *
  838. * This function returns %0 on success and a negative error code on failure.
  839. */
  840. static int clean_an_unclean_leb(const struct ubifs_info *c,
  841. struct ubifs_unclean_leb *ucleb, void *sbuf)
  842. {
  843. int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
  844. void *buf = sbuf;
  845. dbg_rcvry("LEB %d len %d", lnum, len);
  846. if (len == 0) {
  847. /* Nothing to read, just unmap it */
  848. err = ubifs_leb_unmap(c, lnum);
  849. if (err)
  850. return err;
  851. return 0;
  852. }
  853. err = ubi_read(c->ubi, lnum, buf, offs, len);
  854. if (err && err != -EBADMSG)
  855. return err;
  856. while (len >= 8) {
  857. int ret;
  858. cond_resched();
  859. /* Scan quietly until there is an error */
  860. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  861. if (ret == SCANNED_A_NODE) {
  862. /* A valid node, and not a padding node */
  863. struct ubifs_ch *ch = buf;
  864. int node_len;
  865. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  866. offs += node_len;
  867. buf += node_len;
  868. len -= node_len;
  869. continue;
  870. }
  871. if (ret > 0) {
  872. /* Padding bytes or a valid padding node */
  873. offs += ret;
  874. buf += ret;
  875. len -= ret;
  876. continue;
  877. }
  878. if (ret == SCANNED_EMPTY_SPACE) {
  879. ubifs_err("unexpected empty space at %d:%d",
  880. lnum, offs);
  881. return -EUCLEAN;
  882. }
  883. if (quiet) {
  884. /* Redo the last scan but noisily */
  885. quiet = 0;
  886. continue;
  887. }
  888. ubifs_scanned_corruption(c, lnum, offs, buf);
  889. return -EUCLEAN;
  890. }
  891. /* Pad to min_io_size */
  892. len = ALIGN(ucleb->endpt, c->min_io_size);
  893. if (len > ucleb->endpt) {
  894. int pad_len = len - ALIGN(ucleb->endpt, 8);
  895. if (pad_len > 0) {
  896. buf = c->sbuf + len - pad_len;
  897. ubifs_pad(c, buf, pad_len);
  898. }
  899. }
  900. /* Write back the LEB atomically */
  901. err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
  902. if (err)
  903. return err;
  904. dbg_rcvry("cleaned LEB %d", lnum);
  905. return 0;
  906. }
  907. /**
  908. * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
  909. * @c: UBIFS file-system description object
  910. * @sbuf: LEB-sized buffer to use
  911. *
  912. * This function cleans a LEB identified during recovery that needs to be
  913. * written but was not because UBIFS was mounted read-only. This happens when
  914. * remounting to read-write mode.
  915. *
  916. * This function returns %0 on success and a negative error code on failure.
  917. */
  918. int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
  919. {
  920. dbg_rcvry("recovery");
  921. while (!list_empty(&c->unclean_leb_list)) {
  922. struct ubifs_unclean_leb *ucleb;
  923. int err;
  924. ucleb = list_entry(c->unclean_leb_list.next,
  925. struct ubifs_unclean_leb, list);
  926. err = clean_an_unclean_leb(c, ucleb, sbuf);
  927. if (err)
  928. return err;
  929. list_del(&ucleb->list);
  930. kfree(ucleb);
  931. }
  932. return 0;
  933. }
  934. /**
  935. * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
  936. * @c: UBIFS file-system description object
  937. *
  938. * Out-of-place garbage collection requires always one empty LEB with which to
  939. * start garbage collection. The LEB number is recorded in c->gc_lnum and is
  940. * written to the master node on unmounting. In the case of an unclean unmount
  941. * the value of gc_lnum recorded in the master node is out of date and cannot
  942. * be used. Instead, recovery must allocate an empty LEB for this purpose.
  943. * However, there may not be enough empty space, in which case it must be
  944. * possible to GC the dirtiest LEB into the GC head LEB.
  945. *
  946. * This function also runs the commit which causes the TNC updates from
  947. * size-recovery and orphans to be written to the flash. That is important to
  948. * ensure correct replay order for subsequent mounts.
  949. *
  950. * This function returns %0 on success and a negative error code on failure.
  951. */
  952. int ubifs_rcvry_gc_commit(struct ubifs_info *c)
  953. {
  954. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  955. struct ubifs_lprops lp;
  956. int lnum, err;
  957. c->gc_lnum = -1;
  958. if (wbuf->lnum == -1) {
  959. dbg_rcvry("no GC head LEB");
  960. goto find_free;
  961. }
  962. /*
  963. * See whether the used space in the dirtiest LEB fits in the GC head
  964. * LEB.
  965. */
  966. if (wbuf->offs == c->leb_size) {
  967. dbg_rcvry("no room in GC head LEB");
  968. goto find_free;
  969. }
  970. err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
  971. if (err) {
  972. /*
  973. * There are no dirty or empty LEBs subject to here being
  974. * enough for the index. Try to use
  975. * 'ubifs_find_free_leb_for_idx()', which will return any empty
  976. * LEBs (ignoring index requirements). If the index then
  977. * doesn't have enough LEBs the recovery commit will fail -
  978. * which is the same result anyway i.e. recovery fails. So
  979. * there is no problem ignoring index requirements and just
  980. * grabbing a free LEB since we have already established there
  981. * is not a dirty LEB we could have used instead.
  982. */
  983. if (err == -ENOSPC) {
  984. dbg_rcvry("could not find a dirty LEB");
  985. goto find_free;
  986. }
  987. return err;
  988. }
  989. ubifs_assert(!(lp.flags & LPROPS_INDEX));
  990. lnum = lp.lnum;
  991. if (lp.free + lp.dirty == c->leb_size) {
  992. /* An empty LEB was returned */
  993. if (lp.free != c->leb_size) {
  994. err = ubifs_change_one_lp(c, lnum, c->leb_size,
  995. 0, 0, 0, 0);
  996. if (err)
  997. return err;
  998. }
  999. err = ubifs_leb_unmap(c, lnum);
  1000. if (err)
  1001. return err;
  1002. c->gc_lnum = lnum;
  1003. dbg_rcvry("allocated LEB %d for GC", lnum);
  1004. /* Run the commit */
  1005. dbg_rcvry("committing");
  1006. return ubifs_run_commit(c);
  1007. }
  1008. /*
  1009. * There was no empty LEB so the used space in the dirtiest LEB must fit
  1010. * in the GC head LEB.
  1011. */
  1012. if (lp.free + lp.dirty < wbuf->offs) {
  1013. dbg_rcvry("LEB %d doesn't fit in GC head LEB %d:%d",
  1014. lnum, wbuf->lnum, wbuf->offs);
  1015. err = ubifs_return_leb(c, lnum);
  1016. if (err)
  1017. return err;
  1018. goto find_free;
  1019. }
  1020. /*
  1021. * We run the commit before garbage collection otherwise subsequent
  1022. * mounts will see the GC and orphan deletion in a different order.
  1023. */
  1024. dbg_rcvry("committing");
  1025. err = ubifs_run_commit(c);
  1026. if (err)
  1027. return err;
  1028. /*
  1029. * The data in the dirtiest LEB fits in the GC head LEB, so do the GC
  1030. * - use locking to keep 'ubifs_assert()' happy.
  1031. */
  1032. dbg_rcvry("GC'ing LEB %d", lnum);
  1033. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  1034. err = ubifs_garbage_collect_leb(c, &lp);
  1035. if (err >= 0) {
  1036. int err2 = ubifs_wbuf_sync_nolock(wbuf);
  1037. if (err2)
  1038. err = err2;
  1039. }
  1040. mutex_unlock(&wbuf->io_mutex);
  1041. if (err < 0) {
  1042. dbg_err("GC failed, error %d", err);
  1043. if (err == -EAGAIN)
  1044. err = -EINVAL;
  1045. return err;
  1046. }
  1047. if (err != LEB_RETAINED) {
  1048. dbg_err("GC returned %d", err);
  1049. return -EINVAL;
  1050. }
  1051. err = ubifs_leb_unmap(c, c->gc_lnum);
  1052. if (err)
  1053. return err;
  1054. dbg_rcvry("allocated LEB %d for GC", lnum);
  1055. return 0;
  1056. find_free:
  1057. /*
  1058. * There is no GC head LEB or the free space in the GC head LEB is too
  1059. * small, or there are not dirty LEBs. Allocate gc_lnum by calling
  1060. * 'ubifs_find_free_leb_for_idx()' so GC is not run.
  1061. */
  1062. lnum = ubifs_find_free_leb_for_idx(c);
  1063. if (lnum < 0) {
  1064. dbg_err("could not find an empty LEB");
  1065. return lnum;
  1066. }
  1067. /* And reset the index flag */
  1068. err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
  1069. LPROPS_INDEX, 0);
  1070. if (err)
  1071. return err;
  1072. c->gc_lnum = lnum;
  1073. dbg_rcvry("allocated LEB %d for GC", lnum);
  1074. /* Run the commit */
  1075. dbg_rcvry("committing");
  1076. return ubifs_run_commit(c);
  1077. }
  1078. /**
  1079. * struct size_entry - inode size information for recovery.
  1080. * @rb: link in the RB-tree of sizes
  1081. * @inum: inode number
  1082. * @i_size: size on inode
  1083. * @d_size: maximum size based on data nodes
  1084. * @exists: indicates whether the inode exists
  1085. * @inode: inode if pinned in memory awaiting rw mode to fix it
  1086. */
  1087. struct size_entry {
  1088. struct rb_node rb;
  1089. ino_t inum;
  1090. loff_t i_size;
  1091. loff_t d_size;
  1092. int exists;
  1093. struct inode *inode;
  1094. };
  1095. /**
  1096. * add_ino - add an entry to the size tree.
  1097. * @c: UBIFS file-system description object
  1098. * @inum: inode number
  1099. * @i_size: size on inode
  1100. * @d_size: maximum size based on data nodes
  1101. * @exists: indicates whether the inode exists
  1102. */
  1103. static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
  1104. loff_t d_size, int exists)
  1105. {
  1106. struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
  1107. struct size_entry *e;
  1108. while (*p) {
  1109. parent = *p;
  1110. e = rb_entry(parent, struct size_entry, rb);
  1111. if (inum < e->inum)
  1112. p = &(*p)->rb_left;
  1113. else
  1114. p = &(*p)->rb_right;
  1115. }
  1116. e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
  1117. if (!e)
  1118. return -ENOMEM;
  1119. e->inum = inum;
  1120. e->i_size = i_size;
  1121. e->d_size = d_size;
  1122. e->exists = exists;
  1123. rb_link_node(&e->rb, parent, p);
  1124. rb_insert_color(&e->rb, &c->size_tree);
  1125. return 0;
  1126. }
  1127. /**
  1128. * find_ino - find an entry on the size tree.
  1129. * @c: UBIFS file-system description object
  1130. * @inum: inode number
  1131. */
  1132. static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
  1133. {
  1134. struct rb_node *p = c->size_tree.rb_node;
  1135. struct size_entry *e;
  1136. while (p) {
  1137. e = rb_entry(p, struct size_entry, rb);
  1138. if (inum < e->inum)
  1139. p = p->rb_left;
  1140. else if (inum > e->inum)
  1141. p = p->rb_right;
  1142. else
  1143. return e;
  1144. }
  1145. return NULL;
  1146. }
  1147. /**
  1148. * remove_ino - remove an entry from the size tree.
  1149. * @c: UBIFS file-system description object
  1150. * @inum: inode number
  1151. */
  1152. static void remove_ino(struct ubifs_info *c, ino_t inum)
  1153. {
  1154. struct size_entry *e = find_ino(c, inum);
  1155. if (!e)
  1156. return;
  1157. rb_erase(&e->rb, &c->size_tree);
  1158. kfree(e);
  1159. }
  1160. /**
  1161. * ubifs_destroy_size_tree - free resources related to the size tree.
  1162. * @c: UBIFS file-system description object
  1163. */
  1164. void ubifs_destroy_size_tree(struct ubifs_info *c)
  1165. {
  1166. struct rb_node *this = c->size_tree.rb_node;
  1167. struct size_entry *e;
  1168. while (this) {
  1169. if (this->rb_left) {
  1170. this = this->rb_left;
  1171. continue;
  1172. } else if (this->rb_right) {
  1173. this = this->rb_right;
  1174. continue;
  1175. }
  1176. e = rb_entry(this, struct size_entry, rb);
  1177. if (e->inode)
  1178. iput(e->inode);
  1179. this = rb_parent(this);
  1180. if (this) {
  1181. if (this->rb_left == &e->rb)
  1182. this->rb_left = NULL;
  1183. else
  1184. this->rb_right = NULL;
  1185. }
  1186. kfree(e);
  1187. }
  1188. c->size_tree = RB_ROOT;
  1189. }
  1190. /**
  1191. * ubifs_recover_size_accum - accumulate inode sizes for recovery.
  1192. * @c: UBIFS file-system description object
  1193. * @key: node key
  1194. * @deletion: node is for a deletion
  1195. * @new_size: inode size
  1196. *
  1197. * This function has two purposes:
  1198. * 1) to ensure there are no data nodes that fall outside the inode size
  1199. * 2) to ensure there are no data nodes for inodes that do not exist
  1200. * To accomplish those purposes, a rb-tree is constructed containing an entry
  1201. * for each inode number in the journal that has not been deleted, and recording
  1202. * the size from the inode node, the maximum size of any data node (also altered
  1203. * by truncations) and a flag indicating a inode number for which no inode node
  1204. * was present in the journal.
  1205. *
  1206. * Note that there is still the possibility that there are data nodes that have
  1207. * been committed that are beyond the inode size, however the only way to find
  1208. * them would be to scan the entire index. Alternatively, some provision could
  1209. * be made to record the size of inodes at the start of commit, which would seem
  1210. * very cumbersome for a scenario that is quite unlikely and the only negative
  1211. * consequence of which is wasted space.
  1212. *
  1213. * This functions returns %0 on success and a negative error code on failure.
  1214. */
  1215. int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
  1216. int deletion, loff_t new_size)
  1217. {
  1218. ino_t inum = key_inum(c, key);
  1219. struct size_entry *e;
  1220. int err;
  1221. switch (key_type(c, key)) {
  1222. case UBIFS_INO_KEY:
  1223. if (deletion)
  1224. remove_ino(c, inum);
  1225. else {
  1226. e = find_ino(c, inum);
  1227. if (e) {
  1228. e->i_size = new_size;
  1229. e->exists = 1;
  1230. } else {
  1231. err = add_ino(c, inum, new_size, 0, 1);
  1232. if (err)
  1233. return err;
  1234. }
  1235. }
  1236. break;
  1237. case UBIFS_DATA_KEY:
  1238. e = find_ino(c, inum);
  1239. if (e) {
  1240. if (new_size > e->d_size)
  1241. e->d_size = new_size;
  1242. } else {
  1243. err = add_ino(c, inum, 0, new_size, 0);
  1244. if (err)
  1245. return err;
  1246. }
  1247. break;
  1248. case UBIFS_TRUN_KEY:
  1249. e = find_ino(c, inum);
  1250. if (e)
  1251. e->d_size = new_size;
  1252. break;
  1253. }
  1254. return 0;
  1255. }
  1256. /**
  1257. * fix_size_in_place - fix inode size in place on flash.
  1258. * @c: UBIFS file-system description object
  1259. * @e: inode size information for recovery
  1260. */
  1261. static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
  1262. {
  1263. struct ubifs_ino_node *ino = c->sbuf;
  1264. unsigned char *p;
  1265. union ubifs_key key;
  1266. int err, lnum, offs, len;
  1267. loff_t i_size;
  1268. uint32_t crc;
  1269. /* Locate the inode node LEB number and offset */
  1270. ino_key_init(c, &key, e->inum);
  1271. err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
  1272. if (err)
  1273. goto out;
  1274. /*
  1275. * If the size recorded on the inode node is greater than the size that
  1276. * was calculated from nodes in the journal then don't change the inode.
  1277. */
  1278. i_size = le64_to_cpu(ino->size);
  1279. if (i_size >= e->d_size)
  1280. return 0;
  1281. /* Read the LEB */
  1282. err = ubi_read(c->ubi, lnum, c->sbuf, 0, c->leb_size);
  1283. if (err)
  1284. goto out;
  1285. /* Change the size field and recalculate the CRC */
  1286. ino = c->sbuf + offs;
  1287. ino->size = cpu_to_le64(e->d_size);
  1288. len = le32_to_cpu(ino->ch.len);
  1289. crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
  1290. ino->ch.crc = cpu_to_le32(crc);
  1291. /* Work out where data in the LEB ends and free space begins */
  1292. p = c->sbuf;
  1293. len = c->leb_size - 1;
  1294. while (p[len] == 0xff)
  1295. len -= 1;
  1296. len = ALIGN(len + 1, c->min_io_size);
  1297. /* Atomically write the fixed LEB back again */
  1298. err = ubi_leb_change(c->ubi, lnum, c->sbuf, len, UBI_UNKNOWN);
  1299. if (err)
  1300. goto out;
  1301. dbg_rcvry("inode %lu at %d:%d size %lld -> %lld ",
  1302. (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
  1303. return 0;
  1304. out:
  1305. ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d",
  1306. (unsigned long)e->inum, e->i_size, e->d_size, err);
  1307. return err;
  1308. }
  1309. /**
  1310. * ubifs_recover_size - recover inode size.
  1311. * @c: UBIFS file-system description object
  1312. *
  1313. * This function attempts to fix inode size discrepancies identified by the
  1314. * 'ubifs_recover_size_accum()' function.
  1315. *
  1316. * This functions returns %0 on success and a negative error code on failure.
  1317. */
  1318. int ubifs_recover_size(struct ubifs_info *c)
  1319. {
  1320. struct rb_node *this = rb_first(&c->size_tree);
  1321. while (this) {
  1322. struct size_entry *e;
  1323. int err;
  1324. e = rb_entry(this, struct size_entry, rb);
  1325. if (!e->exists) {
  1326. union ubifs_key key;
  1327. ino_key_init(c, &key, e->inum);
  1328. err = ubifs_tnc_lookup(c, &key, c->sbuf);
  1329. if (err && err != -ENOENT)
  1330. return err;
  1331. if (err == -ENOENT) {
  1332. /* Remove data nodes that have no inode */
  1333. dbg_rcvry("removing ino %lu",
  1334. (unsigned long)e->inum);
  1335. err = ubifs_tnc_remove_ino(c, e->inum);
  1336. if (err)
  1337. return err;
  1338. } else {
  1339. struct ubifs_ino_node *ino = c->sbuf;
  1340. e->exists = 1;
  1341. e->i_size = le64_to_cpu(ino->size);
  1342. }
  1343. }
  1344. if (e->exists && e->i_size < e->d_size) {
  1345. if (!e->inode && c->ro_mount) {
  1346. /* Fix the inode size and pin it in memory */
  1347. struct inode *inode;
  1348. inode = ubifs_iget(c->vfs_sb, e->inum);
  1349. if (IS_ERR(inode))
  1350. return PTR_ERR(inode);
  1351. if (inode->i_size < e->d_size) {
  1352. dbg_rcvry("ino %lu size %lld -> %lld",
  1353. (unsigned long)e->inum,
  1354. e->d_size, inode->i_size);
  1355. inode->i_size = e->d_size;
  1356. ubifs_inode(inode)->ui_size = e->d_size;
  1357. e->inode = inode;
  1358. this = rb_next(this);
  1359. continue;
  1360. }
  1361. iput(inode);
  1362. } else {
  1363. /* Fix the size in place */
  1364. err = fix_size_in_place(c, e);
  1365. if (err)
  1366. return err;
  1367. if (e->inode)
  1368. iput(e->inode);
  1369. }
  1370. }
  1371. this = rb_next(this);
  1372. rb_erase(&e->rb, &c->size_tree);
  1373. kfree(e);
  1374. }
  1375. return 0;
  1376. }