recovery.c 40 KB

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