recovery.c 38 KB

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