recovery.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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 "ubifs.h"
  32. /**
  33. * is_empty - determine whether a buffer is empty (contains all 0xff).
  34. * @buf: buffer to clean
  35. * @len: length of buffer
  36. *
  37. * This function returns %1 if the buffer is empty (contains all 0xff) otherwise
  38. * %0 is returned.
  39. */
  40. static int is_empty(void *buf, int len)
  41. {
  42. uint8_t *p = buf;
  43. int i;
  44. for (i = 0; i < len; i++)
  45. if (*p++ != 0xff)
  46. return 0;
  47. return 1;
  48. }
  49. /**
  50. * get_master_node - get the last valid master node allowing for corruption.
  51. * @c: UBIFS file-system description object
  52. * @lnum: LEB number
  53. * @pbuf: buffer containing the LEB read, is returned here
  54. * @mst: master node, if found, is returned here
  55. * @cor: corruption, if found, is returned here
  56. *
  57. * This function allocates a buffer, reads the LEB into it, and finds and
  58. * returns the last valid master node allowing for one area of corruption.
  59. * The corrupt area, if there is one, must be consistent with the assumption
  60. * that it is the result of an unclean unmount while the master node was being
  61. * written. Under those circumstances, it is valid to use the previously written
  62. * master node.
  63. *
  64. * This function returns %0 on success and a negative error code on failure.
  65. */
  66. static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
  67. struct ubifs_mst_node **mst, void **cor)
  68. {
  69. const int sz = c->mst_node_alsz;
  70. int err, offs, len;
  71. void *sbuf, *buf;
  72. sbuf = vmalloc(c->leb_size);
  73. if (!sbuf)
  74. return -ENOMEM;
  75. err = ubi_read(c->ubi, lnum, sbuf, 0, c->leb_size);
  76. if (err && err != -EBADMSG)
  77. goto out_free;
  78. /* Find the first position that is definitely not a node */
  79. offs = 0;
  80. buf = sbuf;
  81. len = c->leb_size;
  82. while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
  83. struct ubifs_ch *ch = buf;
  84. if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
  85. break;
  86. offs += sz;
  87. buf += sz;
  88. len -= sz;
  89. }
  90. /* See if there was a valid master node before that */
  91. if (offs) {
  92. int ret;
  93. offs -= sz;
  94. buf -= sz;
  95. len += sz;
  96. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
  97. if (ret != SCANNED_A_NODE && offs) {
  98. /* Could have been corruption so check one place back */
  99. offs -= sz;
  100. buf -= sz;
  101. len += sz;
  102. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
  103. if (ret != SCANNED_A_NODE)
  104. /*
  105. * We accept only one area of corruption because
  106. * we are assuming that it was caused while
  107. * trying to write a master node.
  108. */
  109. goto out_err;
  110. }
  111. if (ret == SCANNED_A_NODE) {
  112. struct ubifs_ch *ch = buf;
  113. if (ch->node_type != UBIFS_MST_NODE)
  114. goto out_err;
  115. dbg_rcvry("found a master node at %d:%d", lnum, offs);
  116. *mst = buf;
  117. offs += sz;
  118. buf += sz;
  119. len -= sz;
  120. }
  121. }
  122. /* Check for corruption */
  123. if (offs < c->leb_size) {
  124. if (!is_empty(buf, min_t(int, len, sz))) {
  125. *cor = buf;
  126. dbg_rcvry("found corruption at %d:%d", lnum, offs);
  127. }
  128. offs += sz;
  129. buf += sz;
  130. len -= sz;
  131. }
  132. /* Check remaining empty space */
  133. if (offs < c->leb_size)
  134. if (!is_empty(buf, len))
  135. goto out_err;
  136. *pbuf = sbuf;
  137. return 0;
  138. out_err:
  139. err = -EINVAL;
  140. out_free:
  141. vfree(sbuf);
  142. *mst = NULL;
  143. *cor = NULL;
  144. return err;
  145. }
  146. /**
  147. * write_rcvrd_mst_node - write recovered master node.
  148. * @c: UBIFS file-system description object
  149. * @mst: master node
  150. *
  151. * This function returns %0 on success and a negative error code on failure.
  152. */
  153. static int write_rcvrd_mst_node(struct ubifs_info *c,
  154. struct ubifs_mst_node *mst)
  155. {
  156. int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
  157. __le32 save_flags;
  158. dbg_rcvry("recovery");
  159. save_flags = mst->flags;
  160. mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY);
  161. ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1);
  162. err = ubi_leb_change(c->ubi, lnum, mst, sz, UBI_SHORTTERM);
  163. if (err)
  164. goto out;
  165. err = ubi_leb_change(c->ubi, lnum + 1, mst, sz, UBI_SHORTTERM);
  166. if (err)
  167. goto out;
  168. out:
  169. mst->flags = save_flags;
  170. return err;
  171. }
  172. /**
  173. * ubifs_recover_master_node - recover the master node.
  174. * @c: UBIFS file-system description object
  175. *
  176. * This function recovers the master node from corruption that may occur due to
  177. * an unclean unmount.
  178. *
  179. * This function returns %0 on success and a negative error code on failure.
  180. */
  181. int ubifs_recover_master_node(struct ubifs_info *c)
  182. {
  183. void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
  184. struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
  185. const int sz = c->mst_node_alsz;
  186. int err, offs1, offs2;
  187. dbg_rcvry("recovery");
  188. err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
  189. if (err)
  190. goto out_free;
  191. err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
  192. if (err)
  193. goto out_free;
  194. if (mst1) {
  195. offs1 = (void *)mst1 - buf1;
  196. if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
  197. (offs1 == 0 && !cor1)) {
  198. /*
  199. * mst1 was written by recovery at offset 0 with no
  200. * corruption.
  201. */
  202. dbg_rcvry("recovery recovery");
  203. mst = mst1;
  204. } else if (mst2) {
  205. offs2 = (void *)mst2 - buf2;
  206. if (offs1 == offs2) {
  207. /* Same offset, so must be the same */
  208. if (memcmp((void *)mst1 + UBIFS_CH_SZ,
  209. (void *)mst2 + UBIFS_CH_SZ,
  210. UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
  211. goto out_err;
  212. mst = mst1;
  213. } else if (offs2 + sz == offs1) {
  214. /* 1st LEB was written, 2nd was not */
  215. if (cor1)
  216. goto out_err;
  217. mst = mst1;
  218. } else if (offs1 == 0 && offs2 + sz >= c->leb_size) {
  219. /* 1st LEB was unmapped and written, 2nd not */
  220. if (cor1)
  221. goto out_err;
  222. mst = mst1;
  223. } else
  224. goto out_err;
  225. } else {
  226. /*
  227. * 2nd LEB was unmapped and about to be written, so
  228. * there must be only one master node in the first LEB
  229. * and no corruption.
  230. */
  231. if (offs1 != 0 || cor1)
  232. goto out_err;
  233. mst = mst1;
  234. }
  235. } else {
  236. if (!mst2)
  237. goto out_err;
  238. /*
  239. * 1st LEB was unmapped and about to be written, so there must
  240. * be no room left in 2nd LEB.
  241. */
  242. offs2 = (void *)mst2 - buf2;
  243. if (offs2 + sz + sz <= c->leb_size)
  244. goto out_err;
  245. mst = mst2;
  246. }
  247. dbg_rcvry("recovered master node from LEB %d",
  248. (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
  249. memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
  250. if ((c->vfs_sb->s_flags & MS_RDONLY)) {
  251. /* Read-only mode. Keep a copy for switching to rw mode */
  252. c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
  253. if (!c->rcvrd_mst_node) {
  254. err = -ENOMEM;
  255. goto out_free;
  256. }
  257. memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
  258. }
  259. vfree(buf2);
  260. vfree(buf1);
  261. return 0;
  262. out_err:
  263. err = -EINVAL;
  264. out_free:
  265. ubifs_err("failed to recover master node");
  266. if (mst1) {
  267. dbg_err("dumping first master node");
  268. dbg_dump_node(c, mst1);
  269. }
  270. if (mst2) {
  271. dbg_err("dumping second master node");
  272. dbg_dump_node(c, mst2);
  273. }
  274. vfree(buf2);
  275. vfree(buf1);
  276. return err;
  277. }
  278. /**
  279. * ubifs_write_rcvrd_mst_node - write the recovered master node.
  280. * @c: UBIFS file-system description object
  281. *
  282. * This function writes the master node that was recovered during mounting in
  283. * read-only mode and must now be written because we are remounting rw.
  284. *
  285. * This function returns %0 on success and a negative error code on failure.
  286. */
  287. int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
  288. {
  289. int err;
  290. if (!c->rcvrd_mst_node)
  291. return 0;
  292. c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  293. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  294. err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
  295. if (err)
  296. return err;
  297. kfree(c->rcvrd_mst_node);
  298. c->rcvrd_mst_node = NULL;
  299. return 0;
  300. }
  301. /**
  302. * is_last_write - determine if an offset was in the last write to a LEB.
  303. * @c: UBIFS file-system description object
  304. * @buf: buffer to check
  305. * @offs: offset to check
  306. *
  307. * This function returns %1 if @offs was in the last write to the LEB whose data
  308. * is in @buf, otherwise %0 is returned. The determination is made by checking
  309. * for subsequent empty space starting from the next min_io_size boundary (or a
  310. * bit less than the common header size if min_io_size is one).
  311. */
  312. static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
  313. {
  314. int empty_offs;
  315. int check_len;
  316. uint8_t *p;
  317. if (c->min_io_size == 1) {
  318. check_len = c->leb_size - offs;
  319. p = buf + check_len;
  320. for (; check_len > 0; check_len--)
  321. if (*--p != 0xff)
  322. break;
  323. /*
  324. * 'check_len' is the size of the corruption which cannot be
  325. * more than the size of 1 node if it was caused by an unclean
  326. * unmount.
  327. */
  328. if (check_len > UBIFS_MAX_NODE_SZ)
  329. return 0;
  330. return 1;
  331. }
  332. /*
  333. * Round up to the next c->min_io_size boundary i.e. 'offs' is in the
  334. * last wbuf written. After that should be empty space.
  335. */
  336. empty_offs = ALIGN(offs + 1, c->min_io_size);
  337. check_len = c->leb_size - empty_offs;
  338. p = buf + empty_offs - offs;
  339. for (; check_len > 0; check_len--)
  340. if (*p++ != 0xff)
  341. return 0;
  342. return 1;
  343. }
  344. /**
  345. * clean_buf - clean the data from an LEB sitting in a buffer.
  346. * @c: UBIFS file-system description object
  347. * @buf: buffer to clean
  348. * @lnum: LEB number to clean
  349. * @offs: offset from which to clean
  350. * @len: length of buffer
  351. *
  352. * This function pads up to the next min_io_size boundary (if there is one) and
  353. * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
  354. * min_io_size boundary (if there is one).
  355. */
  356. static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
  357. int *offs, int *len)
  358. {
  359. int empty_offs, pad_len;
  360. lnum = lnum;
  361. dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
  362. if (c->min_io_size == 1) {
  363. memset(*buf, 0xff, c->leb_size - *offs);
  364. return;
  365. }
  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. }
  441. return 0;
  442. }
  443. /**
  444. * drop_incomplete_group - drop nodes from an incomplete group.
  445. * @sleb: scanned LEB information
  446. * @offs: offset of dropped nodes is returned here
  447. *
  448. * This function returns %1 if nodes are dropped and %0 otherwise.
  449. */
  450. static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
  451. {
  452. int dropped = 0;
  453. while (!list_empty(&sleb->nodes)) {
  454. struct ubifs_scan_node *snod;
  455. struct ubifs_ch *ch;
  456. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  457. list);
  458. ch = snod->node;
  459. if (ch->group_type != UBIFS_IN_NODE_GROUP)
  460. return dropped;
  461. dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
  462. *offs = snod->offs;
  463. list_del(&snod->list);
  464. kfree(snod);
  465. sleb->nodes_cnt -= 1;
  466. dropped = 1;
  467. }
  468. return dropped;
  469. }
  470. /**
  471. * ubifs_recover_leb - scan and recover a LEB.
  472. * @c: UBIFS file-system description object
  473. * @lnum: LEB number
  474. * @offs: offset
  475. * @sbuf: LEB-sized buffer to use
  476. * @grouped: nodes may be grouped for recovery
  477. *
  478. * This function does a scan of a LEB, but caters for errors that might have
  479. * been caused by the unclean unmount from which we are attempting to recover.
  480. *
  481. * This function returns %0 on success and a negative error code on failure.
  482. */
  483. struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
  484. int offs, void *sbuf, int grouped)
  485. {
  486. int err, len = c->leb_size - offs, need_clean = 0, quiet = 1;
  487. int empty_chkd = 0, start = offs;
  488. struct ubifs_scan_leb *sleb;
  489. void *buf = sbuf + offs;
  490. dbg_rcvry("%d:%d", lnum, offs);
  491. sleb = ubifs_start_scan(c, lnum, offs, sbuf);
  492. if (IS_ERR(sleb))
  493. return sleb;
  494. if (sleb->ecc)
  495. need_clean = 1;
  496. while (len >= 8) {
  497. int ret;
  498. dbg_scan("look at LEB %d:%d (%d bytes left)",
  499. lnum, offs, len);
  500. cond_resched();
  501. /*
  502. * Scan quietly until there is an error from which we cannot
  503. * recover
  504. */
  505. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  506. if (ret == SCANNED_A_NODE) {
  507. /* A valid node, and not a padding node */
  508. struct ubifs_ch *ch = buf;
  509. int node_len;
  510. err = ubifs_add_snod(c, sleb, buf, offs);
  511. if (err)
  512. goto error;
  513. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  514. offs += node_len;
  515. buf += node_len;
  516. len -= node_len;
  517. continue;
  518. }
  519. if (ret > 0) {
  520. /* Padding bytes or a valid padding node */
  521. offs += ret;
  522. buf += ret;
  523. len -= ret;
  524. continue;
  525. }
  526. if (ret == SCANNED_EMPTY_SPACE) {
  527. if (!is_empty(buf, len)) {
  528. if (!is_last_write(c, buf, offs))
  529. break;
  530. clean_buf(c, &buf, lnum, &offs, &len);
  531. need_clean = 1;
  532. }
  533. empty_chkd = 1;
  534. break;
  535. }
  536. if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE)
  537. if (is_last_write(c, buf, offs)) {
  538. clean_buf(c, &buf, lnum, &offs, &len);
  539. need_clean = 1;
  540. empty_chkd = 1;
  541. break;
  542. }
  543. if (ret == SCANNED_A_CORRUPT_NODE)
  544. if (no_more_nodes(c, buf, len, lnum, offs)) {
  545. clean_buf(c, &buf, lnum, &offs, &len);
  546. need_clean = 1;
  547. empty_chkd = 1;
  548. break;
  549. }
  550. if (quiet) {
  551. /* Redo the last scan but noisily */
  552. quiet = 0;
  553. continue;
  554. }
  555. switch (ret) {
  556. case SCANNED_GARBAGE:
  557. dbg_err("garbage");
  558. goto corrupted;
  559. case SCANNED_A_CORRUPT_NODE:
  560. case SCANNED_A_BAD_PAD_NODE:
  561. dbg_err("bad node");
  562. goto corrupted;
  563. default:
  564. dbg_err("unknown");
  565. goto corrupted;
  566. }
  567. }
  568. if (!empty_chkd && !is_empty(buf, len)) {
  569. if (is_last_write(c, buf, offs)) {
  570. clean_buf(c, &buf, lnum, &offs, &len);
  571. need_clean = 1;
  572. } else {
  573. ubifs_err("corrupt empty space at LEB %d:%d",
  574. lnum, offs);
  575. goto corrupted;
  576. }
  577. }
  578. /* Drop nodes from incomplete group */
  579. if (grouped && drop_incomplete_group(sleb, &offs)) {
  580. buf = sbuf + offs;
  581. len = c->leb_size - offs;
  582. clean_buf(c, &buf, lnum, &offs, &len);
  583. need_clean = 1;
  584. }
  585. if (offs % c->min_io_size) {
  586. clean_buf(c, &buf, lnum, &offs, &len);
  587. need_clean = 1;
  588. }
  589. ubifs_end_scan(c, sleb, lnum, offs);
  590. if (need_clean) {
  591. err = fix_unclean_leb(c, sleb, start);
  592. if (err)
  593. goto error;
  594. }
  595. return sleb;
  596. corrupted:
  597. ubifs_scanned_corruption(c, lnum, offs, buf);
  598. err = -EUCLEAN;
  599. error:
  600. ubifs_err("LEB %d scanning failed", lnum);
  601. ubifs_scan_destroy(sleb);
  602. return ERR_PTR(err);
  603. }
  604. /**
  605. * get_cs_sqnum - get commit start sequence number.
  606. * @c: UBIFS file-system description object
  607. * @lnum: LEB number of commit start node
  608. * @offs: offset of commit start node
  609. * @cs_sqnum: commit start sequence number is returned here
  610. *
  611. * This function returns %0 on success and a negative error code on failure.
  612. */
  613. static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
  614. unsigned long long *cs_sqnum)
  615. {
  616. struct ubifs_cs_node *cs_node = NULL;
  617. int err, ret;
  618. dbg_rcvry("at %d:%d", lnum, offs);
  619. cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
  620. if (!cs_node)
  621. return -ENOMEM;
  622. if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
  623. goto out_err;
  624. err = ubi_read(c->ubi, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ);
  625. if (err && err != -EBADMSG)
  626. goto out_free;
  627. ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
  628. if (ret != SCANNED_A_NODE) {
  629. dbg_err("Not a valid node");
  630. goto out_err;
  631. }
  632. if (cs_node->ch.node_type != UBIFS_CS_NODE) {
  633. dbg_err("Node a CS node, type is %d", cs_node->ch.node_type);
  634. goto out_err;
  635. }
  636. if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
  637. dbg_err("CS node cmt_no %llu != current cmt_no %llu",
  638. (unsigned long long)le64_to_cpu(cs_node->cmt_no),
  639. c->cmt_no);
  640. goto out_err;
  641. }
  642. *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
  643. dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
  644. kfree(cs_node);
  645. return 0;
  646. out_err:
  647. err = -EINVAL;
  648. out_free:
  649. ubifs_err("failed to get CS sqnum");
  650. kfree(cs_node);
  651. return err;
  652. }
  653. /**
  654. * ubifs_recover_log_leb - scan and recover a log LEB.
  655. * @c: UBIFS file-system description object
  656. * @lnum: LEB number
  657. * @offs: offset
  658. * @sbuf: LEB-sized buffer to use
  659. *
  660. * This function does a scan of a LEB, but caters for errors that might have
  661. * been caused by the unclean unmount from which we are attempting to recover.
  662. *
  663. * This function returns %0 on success and a negative error code on failure.
  664. */
  665. struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
  666. int offs, void *sbuf)
  667. {
  668. struct ubifs_scan_leb *sleb;
  669. int next_lnum;
  670. dbg_rcvry("LEB %d", lnum);
  671. next_lnum = lnum + 1;
  672. if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  673. next_lnum = UBIFS_LOG_LNUM;
  674. if (next_lnum != c->ltail_lnum) {
  675. /*
  676. * We can only recover at the end of the log, so check that the
  677. * next log LEB is empty or out of date.
  678. */
  679. sleb = ubifs_scan(c, next_lnum, 0, sbuf);
  680. if (IS_ERR(sleb))
  681. return sleb;
  682. if (sleb->nodes_cnt) {
  683. struct ubifs_scan_node *snod;
  684. unsigned long long cs_sqnum = c->cs_sqnum;
  685. snod = list_entry(sleb->nodes.next,
  686. struct ubifs_scan_node, list);
  687. if (cs_sqnum == 0) {
  688. int err;
  689. err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
  690. if (err) {
  691. ubifs_scan_destroy(sleb);
  692. return ERR_PTR(err);
  693. }
  694. }
  695. if (snod->sqnum > cs_sqnum) {
  696. ubifs_err("unrecoverable log corruption "
  697. "in LEB %d", lnum);
  698. ubifs_scan_destroy(sleb);
  699. return ERR_PTR(-EUCLEAN);
  700. }
  701. }
  702. ubifs_scan_destroy(sleb);
  703. }
  704. return ubifs_recover_leb(c, lnum, offs, sbuf, 0);
  705. }
  706. /**
  707. * recover_head - recover a head.
  708. * @c: UBIFS file-system description object
  709. * @lnum: LEB number of head to recover
  710. * @offs: offset of head to recover
  711. * @sbuf: LEB-sized buffer to use
  712. *
  713. * This function ensures that there is no data on the flash at a head location.
  714. *
  715. * This function returns %0 on success and a negative error code on failure.
  716. */
  717. static int recover_head(const struct ubifs_info *c, int lnum, int offs,
  718. void *sbuf)
  719. {
  720. int len, err, need_clean = 0;
  721. if (c->min_io_size > 1)
  722. len = c->min_io_size;
  723. else
  724. len = 512;
  725. if (offs + len > c->leb_size)
  726. len = c->leb_size - offs;
  727. if (!len)
  728. return 0;
  729. /* Read at the head location and check it is empty flash */
  730. err = ubi_read(c->ubi, lnum, sbuf, offs, len);
  731. if (err)
  732. need_clean = 1;
  733. else {
  734. uint8_t *p = sbuf;
  735. while (len--)
  736. if (*p++ != 0xff) {
  737. need_clean = 1;
  738. break;
  739. }
  740. }
  741. if (need_clean) {
  742. dbg_rcvry("cleaning head at %d:%d", lnum, offs);
  743. if (offs == 0)
  744. return ubifs_leb_unmap(c, lnum);
  745. err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
  746. if (err)
  747. return err;
  748. return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
  749. }
  750. return 0;
  751. }
  752. /**
  753. * ubifs_recover_inl_heads - recover index and LPT heads.
  754. * @c: UBIFS file-system description object
  755. * @sbuf: LEB-sized buffer to use
  756. *
  757. * This function ensures that there is no data on the flash at the index and
  758. * LPT head locations.
  759. *
  760. * This deals with the recovery of a half-completed journal commit. UBIFS is
  761. * careful never to overwrite the last version of the index or the LPT. Because
  762. * the index and LPT are wandering trees, data from a half-completed commit will
  763. * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
  764. * assumed to be empty and will be unmapped anyway before use, or in the index
  765. * and LPT heads.
  766. *
  767. * This function returns %0 on success and a negative error code on failure.
  768. */
  769. int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
  770. {
  771. int err;
  772. ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY) || c->remounting_rw);
  773. dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
  774. err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
  775. if (err)
  776. return err;
  777. dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
  778. err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
  779. if (err)
  780. return err;
  781. return 0;
  782. }
  783. /**
  784. * clean_an_unclean_leb - read and write a LEB to remove corruption.
  785. * @c: UBIFS file-system description object
  786. * @ucleb: unclean LEB information
  787. * @sbuf: LEB-sized buffer to use
  788. *
  789. * This function reads a LEB up to a point pre-determined by the mount recovery,
  790. * checks the nodes, and writes the result back to the flash, thereby cleaning
  791. * off any following corruption, or non-fatal ECC errors.
  792. *
  793. * This function returns %0 on success and a negative error code on failure.
  794. */
  795. static int clean_an_unclean_leb(const struct ubifs_info *c,
  796. struct ubifs_unclean_leb *ucleb, void *sbuf)
  797. {
  798. int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
  799. void *buf = sbuf;
  800. dbg_rcvry("LEB %d len %d", lnum, len);
  801. if (len == 0) {
  802. /* Nothing to read, just unmap it */
  803. err = ubifs_leb_unmap(c, lnum);
  804. if (err)
  805. return err;
  806. return 0;
  807. }
  808. err = ubi_read(c->ubi, lnum, buf, offs, len);
  809. if (err && err != -EBADMSG)
  810. return err;
  811. while (len >= 8) {
  812. int ret;
  813. cond_resched();
  814. /* Scan quietly until there is an error */
  815. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  816. if (ret == SCANNED_A_NODE) {
  817. /* A valid node, and not a padding node */
  818. struct ubifs_ch *ch = buf;
  819. int node_len;
  820. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  821. offs += node_len;
  822. buf += node_len;
  823. len -= node_len;
  824. continue;
  825. }
  826. if (ret > 0) {
  827. /* Padding bytes or a valid padding node */
  828. offs += ret;
  829. buf += ret;
  830. len -= ret;
  831. continue;
  832. }
  833. if (ret == SCANNED_EMPTY_SPACE) {
  834. ubifs_err("unexpected empty space at %d:%d",
  835. lnum, offs);
  836. return -EUCLEAN;
  837. }
  838. if (quiet) {
  839. /* Redo the last scan but noisily */
  840. quiet = 0;
  841. continue;
  842. }
  843. ubifs_scanned_corruption(c, lnum, offs, buf);
  844. return -EUCLEAN;
  845. }
  846. /* Pad to min_io_size */
  847. len = ALIGN(ucleb->endpt, c->min_io_size);
  848. if (len > ucleb->endpt) {
  849. int pad_len = len - ALIGN(ucleb->endpt, 8);
  850. if (pad_len > 0) {
  851. buf = c->sbuf + len - pad_len;
  852. ubifs_pad(c, buf, pad_len);
  853. }
  854. }
  855. /* Write back the LEB atomically */
  856. err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
  857. if (err)
  858. return err;
  859. dbg_rcvry("cleaned LEB %d", lnum);
  860. return 0;
  861. }
  862. /**
  863. * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
  864. * @c: UBIFS file-system description object
  865. * @sbuf: LEB-sized buffer to use
  866. *
  867. * This function cleans a LEB identified during recovery that needs to be
  868. * written but was not because UBIFS was mounted read-only. This happens when
  869. * remounting to read-write mode.
  870. *
  871. * This function returns %0 on success and a negative error code on failure.
  872. */
  873. int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
  874. {
  875. dbg_rcvry("recovery");
  876. while (!list_empty(&c->unclean_leb_list)) {
  877. struct ubifs_unclean_leb *ucleb;
  878. int err;
  879. ucleb = list_entry(c->unclean_leb_list.next,
  880. struct ubifs_unclean_leb, list);
  881. err = clean_an_unclean_leb(c, ucleb, sbuf);
  882. if (err)
  883. return err;
  884. list_del(&ucleb->list);
  885. kfree(ucleb);
  886. }
  887. return 0;
  888. }
  889. /**
  890. * struct size_entry - inode size information for recovery.
  891. * @rb: link in the RB-tree of sizes
  892. * @inum: inode number
  893. * @i_size: size on inode
  894. * @d_size: maximum size based on data nodes
  895. * @exists: indicates whether the inode exists
  896. * @inode: inode if pinned in memory awaiting rw mode to fix it
  897. */
  898. struct size_entry {
  899. struct rb_node rb;
  900. ino_t inum;
  901. loff_t i_size;
  902. loff_t d_size;
  903. int exists;
  904. struct inode *inode;
  905. };
  906. /**
  907. * add_ino - add an entry to the size tree.
  908. * @c: UBIFS file-system description object
  909. * @inum: inode number
  910. * @i_size: size on inode
  911. * @d_size: maximum size based on data nodes
  912. * @exists: indicates whether the inode exists
  913. */
  914. static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
  915. loff_t d_size, int exists)
  916. {
  917. struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
  918. struct size_entry *e;
  919. while (*p) {
  920. parent = *p;
  921. e = rb_entry(parent, struct size_entry, rb);
  922. if (inum < e->inum)
  923. p = &(*p)->rb_left;
  924. else
  925. p = &(*p)->rb_right;
  926. }
  927. e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
  928. if (!e)
  929. return -ENOMEM;
  930. e->inum = inum;
  931. e->i_size = i_size;
  932. e->d_size = d_size;
  933. e->exists = exists;
  934. rb_link_node(&e->rb, parent, p);
  935. rb_insert_color(&e->rb, &c->size_tree);
  936. return 0;
  937. }
  938. /**
  939. * find_ino - find an entry on the size tree.
  940. * @c: UBIFS file-system description object
  941. * @inum: inode number
  942. */
  943. static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
  944. {
  945. struct rb_node *p = c->size_tree.rb_node;
  946. struct size_entry *e;
  947. while (p) {
  948. e = rb_entry(p, struct size_entry, rb);
  949. if (inum < e->inum)
  950. p = p->rb_left;
  951. else if (inum > e->inum)
  952. p = p->rb_right;
  953. else
  954. return e;
  955. }
  956. return NULL;
  957. }
  958. /**
  959. * remove_ino - remove an entry from the size tree.
  960. * @c: UBIFS file-system description object
  961. * @inum: inode number
  962. */
  963. static void remove_ino(struct ubifs_info *c, ino_t inum)
  964. {
  965. struct size_entry *e = find_ino(c, inum);
  966. if (!e)
  967. return;
  968. rb_erase(&e->rb, &c->size_tree);
  969. kfree(e);
  970. }
  971. /**
  972. * ubifs_recover_size_accum - accumulate inode sizes for recovery.
  973. * @c: UBIFS file-system description object
  974. * @key: node key
  975. * @deletion: node is for a deletion
  976. * @new_size: inode size
  977. *
  978. * This function has two purposes:
  979. * 1) to ensure there are no data nodes that fall outside the inode size
  980. * 2) to ensure there are no data nodes for inodes that do not exist
  981. * To accomplish those purposes, a rb-tree is constructed containing an entry
  982. * for each inode number in the journal that has not been deleted, and recording
  983. * the size from the inode node, the maximum size of any data node (also altered
  984. * by truncations) and a flag indicating a inode number for which no inode node
  985. * was present in the journal.
  986. *
  987. * Note that there is still the possibility that there are data nodes that have
  988. * been committed that are beyond the inode size, however the only way to find
  989. * them would be to scan the entire index. Alternatively, some provision could
  990. * be made to record the size of inodes at the start of commit, which would seem
  991. * very cumbersome for a scenario that is quite unlikely and the only negative
  992. * consequence of which is wasted space.
  993. *
  994. * This functions returns %0 on success and a negative error code on failure.
  995. */
  996. int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
  997. int deletion, loff_t new_size)
  998. {
  999. ino_t inum = key_inum(c, key);
  1000. struct size_entry *e;
  1001. int err;
  1002. switch (key_type(c, key)) {
  1003. case UBIFS_INO_KEY:
  1004. if (deletion)
  1005. remove_ino(c, inum);
  1006. else {
  1007. e = find_ino(c, inum);
  1008. if (e) {
  1009. e->i_size = new_size;
  1010. e->exists = 1;
  1011. } else {
  1012. err = add_ino(c, inum, new_size, 0, 1);
  1013. if (err)
  1014. return err;
  1015. }
  1016. }
  1017. break;
  1018. case UBIFS_DATA_KEY:
  1019. e = find_ino(c, inum);
  1020. if (e) {
  1021. if (new_size > e->d_size)
  1022. e->d_size = new_size;
  1023. } else {
  1024. err = add_ino(c, inum, 0, new_size, 0);
  1025. if (err)
  1026. return err;
  1027. }
  1028. break;
  1029. case UBIFS_TRUN_KEY:
  1030. e = find_ino(c, inum);
  1031. if (e)
  1032. e->d_size = new_size;
  1033. break;
  1034. }
  1035. return 0;
  1036. }
  1037. /**
  1038. * ubifs_recover_size - recover inode size.
  1039. * @c: UBIFS file-system description object
  1040. *
  1041. * This function attempts to fix inode size discrepancies identified by the
  1042. * 'ubifs_recover_size_accum()' function.
  1043. *
  1044. * This functions returns %0 on success and a negative error code on failure.
  1045. */
  1046. int ubifs_recover_size(struct ubifs_info *c)
  1047. {
  1048. struct rb_node *this = rb_first(&c->size_tree);
  1049. while (this) {
  1050. struct size_entry *e;
  1051. int err;
  1052. e = rb_entry(this, struct size_entry, rb);
  1053. if (!e->exists) {
  1054. union ubifs_key key;
  1055. ino_key_init(c, &key, e->inum);
  1056. err = ubifs_tnc_lookup(c, &key, c->sbuf);
  1057. if (err && err != -ENOENT)
  1058. return err;
  1059. if (err == -ENOENT) {
  1060. /* Remove data nodes that have no inode */
  1061. dbg_rcvry("removing ino %lu",
  1062. (unsigned long)e->inum);
  1063. err = ubifs_tnc_remove_ino(c, e->inum);
  1064. if (err)
  1065. return err;
  1066. } else {
  1067. struct ubifs_ino_node *ino = c->sbuf;
  1068. e->exists = 1;
  1069. e->i_size = le64_to_cpu(ino->size);
  1070. }
  1071. }
  1072. if (e->exists && e->i_size < e->d_size) {
  1073. if (!e->inode && (c->vfs_sb->s_flags & MS_RDONLY)) {
  1074. /* Fix the inode size and pin it in memory */
  1075. struct inode *inode;
  1076. inode = ubifs_iget(c->vfs_sb, e->inum);
  1077. if (IS_ERR(inode))
  1078. return PTR_ERR(inode);
  1079. if (inode->i_size < e->d_size) {
  1080. dbg_rcvry("ino %lu size %lld -> %lld",
  1081. (unsigned long)e->inum,
  1082. e->d_size, inode->i_size);
  1083. inode->i_size = e->d_size;
  1084. ubifs_inode(inode)->ui_size = e->d_size;
  1085. e->inode = inode;
  1086. this = rb_next(this);
  1087. continue;
  1088. }
  1089. iput(inode);
  1090. }
  1091. }
  1092. this = rb_next(this);
  1093. rb_erase(&e->rb, &c->size_tree);
  1094. kfree(e);
  1095. }
  1096. return 0;
  1097. }