recovery.c 38 KB

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