recovery.c 37 KB

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