recovery.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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 scans @buf for more nodes and returns %0 is a node is found and
  384. * %1 if no more nodes are found.
  385. */
  386. static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
  387. int lnum, int offs)
  388. {
  389. int skip, next_offs = 0;
  390. if (len > UBIFS_DATA_NODE_SZ) {
  391. struct ubifs_ch *ch = buf;
  392. int dlen = le32_to_cpu(ch->len);
  393. if (ch->node_type == UBIFS_DATA_NODE && dlen >= UBIFS_CH_SZ &&
  394. dlen <= UBIFS_MAX_DATA_NODE_SZ)
  395. /* The corrupt node looks like a data node */
  396. next_offs = ALIGN(offs + dlen, 8);
  397. }
  398. if (c->min_io_size == 1)
  399. skip = 8;
  400. else
  401. skip = ALIGN(offs + 1, c->min_io_size) - offs;
  402. offs += skip;
  403. buf += skip;
  404. len -= skip;
  405. while (len > 8) {
  406. struct ubifs_ch *ch = buf;
  407. uint32_t magic = le32_to_cpu(ch->magic);
  408. int ret;
  409. if (magic == UBIFS_NODE_MAGIC) {
  410. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
  411. if (ret == SCANNED_A_NODE || ret > 0) {
  412. /*
  413. * There is a small chance this is just data in
  414. * a data node, so check that possibility. e.g.
  415. * this is part of a file that itself contains
  416. * a UBIFS image.
  417. */
  418. if (next_offs && offs + le32_to_cpu(ch->len) <=
  419. next_offs)
  420. continue;
  421. dbg_rcvry("unexpected node at %d:%d", lnum,
  422. offs);
  423. return 0;
  424. }
  425. }
  426. offs += 8;
  427. buf += 8;
  428. len -= 8;
  429. }
  430. return 1;
  431. }
  432. /**
  433. * fix_unclean_leb - fix an unclean LEB.
  434. * @c: UBIFS file-system description object
  435. * @sleb: scanned LEB information
  436. * @start: offset where scan started
  437. */
  438. static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  439. int start)
  440. {
  441. int lnum = sleb->lnum, endpt = start;
  442. /* Get the end offset of the last node we are keeping */
  443. if (!list_empty(&sleb->nodes)) {
  444. struct ubifs_scan_node *snod;
  445. snod = list_entry(sleb->nodes.prev,
  446. struct ubifs_scan_node, list);
  447. endpt = snod->offs + snod->len;
  448. }
  449. if ((c->vfs_sb->s_flags & MS_RDONLY) && !c->remounting_rw) {
  450. /* Add to recovery list */
  451. struct ubifs_unclean_leb *ucleb;
  452. dbg_rcvry("need to fix LEB %d start %d endpt %d",
  453. lnum, start, sleb->endpt);
  454. ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
  455. if (!ucleb)
  456. return -ENOMEM;
  457. ucleb->lnum = lnum;
  458. ucleb->endpt = endpt;
  459. list_add_tail(&ucleb->list, &c->unclean_leb_list);
  460. }
  461. return 0;
  462. }
  463. /**
  464. * drop_incomplete_group - drop nodes from an incomplete group.
  465. * @sleb: scanned LEB information
  466. * @offs: offset of dropped nodes is returned here
  467. *
  468. * This function returns %1 if nodes are dropped and %0 otherwise.
  469. */
  470. static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
  471. {
  472. int dropped = 0;
  473. while (!list_empty(&sleb->nodes)) {
  474. struct ubifs_scan_node *snod;
  475. struct ubifs_ch *ch;
  476. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  477. list);
  478. ch = snod->node;
  479. if (ch->group_type != UBIFS_IN_NODE_GROUP)
  480. return dropped;
  481. dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
  482. *offs = snod->offs;
  483. list_del(&snod->list);
  484. kfree(snod);
  485. sleb->nodes_cnt -= 1;
  486. dropped = 1;
  487. }
  488. return dropped;
  489. }
  490. /**
  491. * ubifs_recover_leb - scan and recover a LEB.
  492. * @c: UBIFS file-system description object
  493. * @lnum: LEB number
  494. * @offs: offset
  495. * @sbuf: LEB-sized buffer to use
  496. * @grouped: nodes may be grouped for recovery
  497. *
  498. * This function does a scan of a LEB, but caters for errors that might have
  499. * been caused by the unclean unmount from which we are attempting to recover.
  500. *
  501. * This function returns %0 on success and a negative error code on failure.
  502. */
  503. struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
  504. int offs, void *sbuf, int grouped)
  505. {
  506. int err, len = c->leb_size - offs, need_clean = 0, quiet = 1;
  507. int empty_chkd = 0, start = offs;
  508. struct ubifs_scan_leb *sleb;
  509. void *buf = sbuf + offs;
  510. dbg_rcvry("%d:%d", lnum, offs);
  511. sleb = ubifs_start_scan(c, lnum, offs, sbuf);
  512. if (IS_ERR(sleb))
  513. return sleb;
  514. if (sleb->ecc)
  515. need_clean = 1;
  516. while (len >= 8) {
  517. int ret;
  518. dbg_scan("look at LEB %d:%d (%d bytes left)",
  519. lnum, offs, len);
  520. cond_resched();
  521. /*
  522. * Scan quietly until there is an error from which we cannot
  523. * recover
  524. */
  525. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  526. if (ret == SCANNED_A_NODE) {
  527. /* A valid node, and not a padding node */
  528. struct ubifs_ch *ch = buf;
  529. int node_len;
  530. err = ubifs_add_snod(c, sleb, buf, offs);
  531. if (err)
  532. goto error;
  533. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  534. offs += node_len;
  535. buf += node_len;
  536. len -= node_len;
  537. continue;
  538. }
  539. if (ret > 0) {
  540. /* Padding bytes or a valid padding node */
  541. offs += ret;
  542. buf += ret;
  543. len -= ret;
  544. continue;
  545. }
  546. if (ret == SCANNED_EMPTY_SPACE) {
  547. if (!is_empty(buf, len)) {
  548. if (!is_last_write(c, buf, offs))
  549. break;
  550. clean_buf(c, &buf, lnum, &offs, &len);
  551. need_clean = 1;
  552. }
  553. empty_chkd = 1;
  554. break;
  555. }
  556. if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE)
  557. if (is_last_write(c, buf, offs)) {
  558. clean_buf(c, &buf, lnum, &offs, &len);
  559. need_clean = 1;
  560. empty_chkd = 1;
  561. break;
  562. }
  563. if (ret == SCANNED_A_CORRUPT_NODE)
  564. if (no_more_nodes(c, buf, len, lnum, offs)) {
  565. clean_buf(c, &buf, lnum, &offs, &len);
  566. need_clean = 1;
  567. empty_chkd = 1;
  568. break;
  569. }
  570. if (quiet) {
  571. /* Redo the last scan but noisily */
  572. quiet = 0;
  573. continue;
  574. }
  575. switch (ret) {
  576. case SCANNED_GARBAGE:
  577. dbg_err("garbage");
  578. goto corrupted;
  579. case SCANNED_A_CORRUPT_NODE:
  580. case SCANNED_A_BAD_PAD_NODE:
  581. dbg_err("bad node");
  582. goto corrupted;
  583. default:
  584. dbg_err("unknown");
  585. goto corrupted;
  586. }
  587. }
  588. if (!empty_chkd && !is_empty(buf, len)) {
  589. if (is_last_write(c, buf, offs)) {
  590. clean_buf(c, &buf, lnum, &offs, &len);
  591. need_clean = 1;
  592. } else {
  593. ubifs_err("corrupt empty space at LEB %d:%d",
  594. lnum, offs);
  595. goto corrupted;
  596. }
  597. }
  598. /* Drop nodes from incomplete group */
  599. if (grouped && drop_incomplete_group(sleb, &offs)) {
  600. buf = sbuf + offs;
  601. len = c->leb_size - offs;
  602. clean_buf(c, &buf, lnum, &offs, &len);
  603. need_clean = 1;
  604. }
  605. if (offs % c->min_io_size) {
  606. clean_buf(c, &buf, lnum, &offs, &len);
  607. need_clean = 1;
  608. }
  609. ubifs_end_scan(c, sleb, lnum, offs);
  610. if (need_clean) {
  611. err = fix_unclean_leb(c, sleb, start);
  612. if (err)
  613. goto error;
  614. }
  615. return sleb;
  616. corrupted:
  617. ubifs_scanned_corruption(c, lnum, offs, buf);
  618. err = -EUCLEAN;
  619. error:
  620. ubifs_err("LEB %d scanning failed", lnum);
  621. ubifs_scan_destroy(sleb);
  622. return ERR_PTR(err);
  623. }
  624. /**
  625. * get_cs_sqnum - get commit start sequence number.
  626. * @c: UBIFS file-system description object
  627. * @lnum: LEB number of commit start node
  628. * @offs: offset of commit start node
  629. * @cs_sqnum: commit start sequence number is returned here
  630. *
  631. * This function returns %0 on success and a negative error code on failure.
  632. */
  633. static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
  634. unsigned long long *cs_sqnum)
  635. {
  636. struct ubifs_cs_node *cs_node = NULL;
  637. int err, ret;
  638. dbg_rcvry("at %d:%d", lnum, offs);
  639. cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
  640. if (!cs_node)
  641. return -ENOMEM;
  642. if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
  643. goto out_err;
  644. err = ubi_read(c->ubi, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ);
  645. if (err && err != -EBADMSG)
  646. goto out_free;
  647. ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
  648. if (ret != SCANNED_A_NODE) {
  649. dbg_err("Not a valid node");
  650. goto out_err;
  651. }
  652. if (cs_node->ch.node_type != UBIFS_CS_NODE) {
  653. dbg_err("Node a CS node, type is %d", cs_node->ch.node_type);
  654. goto out_err;
  655. }
  656. if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
  657. dbg_err("CS node cmt_no %llu != current cmt_no %llu",
  658. (unsigned long long)le64_to_cpu(cs_node->cmt_no),
  659. c->cmt_no);
  660. goto out_err;
  661. }
  662. *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
  663. dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
  664. kfree(cs_node);
  665. return 0;
  666. out_err:
  667. err = -EINVAL;
  668. out_free:
  669. ubifs_err("failed to get CS sqnum");
  670. kfree(cs_node);
  671. return err;
  672. }
  673. /**
  674. * ubifs_recover_log_leb - scan and recover a log LEB.
  675. * @c: UBIFS file-system description object
  676. * @lnum: LEB number
  677. * @offs: offset
  678. * @sbuf: LEB-sized buffer to use
  679. *
  680. * This function does a scan of a LEB, but caters for errors that might have
  681. * been caused by the unclean unmount from which we are attempting to recover.
  682. *
  683. * This function returns %0 on success and a negative error code on failure.
  684. */
  685. struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
  686. int offs, void *sbuf)
  687. {
  688. struct ubifs_scan_leb *sleb;
  689. int next_lnum;
  690. dbg_rcvry("LEB %d", lnum);
  691. next_lnum = lnum + 1;
  692. if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  693. next_lnum = UBIFS_LOG_LNUM;
  694. if (next_lnum != c->ltail_lnum) {
  695. /*
  696. * We can only recover at the end of the log, so check that the
  697. * next log LEB is empty or out of date.
  698. */
  699. sleb = ubifs_scan(c, next_lnum, 0, sbuf);
  700. if (IS_ERR(sleb))
  701. return sleb;
  702. if (sleb->nodes_cnt) {
  703. struct ubifs_scan_node *snod;
  704. unsigned long long cs_sqnum = c->cs_sqnum;
  705. snod = list_entry(sleb->nodes.next,
  706. struct ubifs_scan_node, list);
  707. if (cs_sqnum == 0) {
  708. int err;
  709. err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
  710. if (err) {
  711. ubifs_scan_destroy(sleb);
  712. return ERR_PTR(err);
  713. }
  714. }
  715. if (snod->sqnum > cs_sqnum) {
  716. ubifs_err("unrecoverable log corruption "
  717. "in LEB %d", lnum);
  718. ubifs_scan_destroy(sleb);
  719. return ERR_PTR(-EUCLEAN);
  720. }
  721. }
  722. ubifs_scan_destroy(sleb);
  723. }
  724. return ubifs_recover_leb(c, lnum, offs, sbuf, 0);
  725. }
  726. /**
  727. * recover_head - recover a head.
  728. * @c: UBIFS file-system description object
  729. * @lnum: LEB number of head to recover
  730. * @offs: offset of head to recover
  731. * @sbuf: LEB-sized buffer to use
  732. *
  733. * This function ensures that there is no data on the flash at a head location.
  734. *
  735. * This function returns %0 on success and a negative error code on failure.
  736. */
  737. static int recover_head(const struct ubifs_info *c, int lnum, int offs,
  738. void *sbuf)
  739. {
  740. int len, err, need_clean = 0;
  741. if (c->min_io_size > 1)
  742. len = c->min_io_size;
  743. else
  744. len = 512;
  745. if (offs + len > c->leb_size)
  746. len = c->leb_size - offs;
  747. if (!len)
  748. return 0;
  749. /* Read at the head location and check it is empty flash */
  750. err = ubi_read(c->ubi, lnum, sbuf, offs, len);
  751. if (err)
  752. need_clean = 1;
  753. else {
  754. uint8_t *p = sbuf;
  755. while (len--)
  756. if (*p++ != 0xff) {
  757. need_clean = 1;
  758. break;
  759. }
  760. }
  761. if (need_clean) {
  762. dbg_rcvry("cleaning head at %d:%d", lnum, offs);
  763. if (offs == 0)
  764. return ubifs_leb_unmap(c, lnum);
  765. err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
  766. if (err)
  767. return err;
  768. return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
  769. }
  770. return 0;
  771. }
  772. /**
  773. * ubifs_recover_inl_heads - recover index and LPT heads.
  774. * @c: UBIFS file-system description object
  775. * @sbuf: LEB-sized buffer to use
  776. *
  777. * This function ensures that there is no data on the flash at the index and
  778. * LPT head locations.
  779. *
  780. * This deals with the recovery of a half-completed journal commit. UBIFS is
  781. * careful never to overwrite the last version of the index or the LPT. Because
  782. * the index and LPT are wandering trees, data from a half-completed commit will
  783. * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
  784. * assumed to be empty and will be unmapped anyway before use, or in the index
  785. * and LPT heads.
  786. *
  787. * This function returns %0 on success and a negative error code on failure.
  788. */
  789. int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
  790. {
  791. int err;
  792. ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY) || c->remounting_rw);
  793. dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
  794. err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
  795. if (err)
  796. return err;
  797. dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
  798. err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
  799. if (err)
  800. return err;
  801. return 0;
  802. }
  803. /**
  804. * clean_an_unclean_leb - read and write a LEB to remove corruption.
  805. * @c: UBIFS file-system description object
  806. * @ucleb: unclean LEB information
  807. * @sbuf: LEB-sized buffer to use
  808. *
  809. * This function reads a LEB up to a point pre-determined by the mount recovery,
  810. * checks the nodes, and writes the result back to the flash, thereby cleaning
  811. * off any following corruption, or non-fatal ECC errors.
  812. *
  813. * This function returns %0 on success and a negative error code on failure.
  814. */
  815. static int clean_an_unclean_leb(const struct ubifs_info *c,
  816. struct ubifs_unclean_leb *ucleb, void *sbuf)
  817. {
  818. int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
  819. void *buf = sbuf;
  820. dbg_rcvry("LEB %d len %d", lnum, len);
  821. if (len == 0) {
  822. /* Nothing to read, just unmap it */
  823. err = ubifs_leb_unmap(c, lnum);
  824. if (err)
  825. return err;
  826. return 0;
  827. }
  828. err = ubi_read(c->ubi, lnum, buf, offs, len);
  829. if (err && err != -EBADMSG)
  830. return err;
  831. while (len >= 8) {
  832. int ret;
  833. cond_resched();
  834. /* Scan quietly until there is an error */
  835. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  836. if (ret == SCANNED_A_NODE) {
  837. /* A valid node, and not a padding node */
  838. struct ubifs_ch *ch = buf;
  839. int node_len;
  840. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  841. offs += node_len;
  842. buf += node_len;
  843. len -= node_len;
  844. continue;
  845. }
  846. if (ret > 0) {
  847. /* Padding bytes or a valid padding node */
  848. offs += ret;
  849. buf += ret;
  850. len -= ret;
  851. continue;
  852. }
  853. if (ret == SCANNED_EMPTY_SPACE) {
  854. ubifs_err("unexpected empty space at %d:%d",
  855. lnum, offs);
  856. return -EUCLEAN;
  857. }
  858. if (quiet) {
  859. /* Redo the last scan but noisily */
  860. quiet = 0;
  861. continue;
  862. }
  863. ubifs_scanned_corruption(c, lnum, offs, buf);
  864. return -EUCLEAN;
  865. }
  866. /* Pad to min_io_size */
  867. len = ALIGN(ucleb->endpt, c->min_io_size);
  868. if (len > ucleb->endpt) {
  869. int pad_len = len - ALIGN(ucleb->endpt, 8);
  870. if (pad_len > 0) {
  871. buf = c->sbuf + len - pad_len;
  872. ubifs_pad(c, buf, pad_len);
  873. }
  874. }
  875. /* Write back the LEB atomically */
  876. err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
  877. if (err)
  878. return err;
  879. dbg_rcvry("cleaned LEB %d", lnum);
  880. return 0;
  881. }
  882. /**
  883. * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
  884. * @c: UBIFS file-system description object
  885. * @sbuf: LEB-sized buffer to use
  886. *
  887. * This function cleans a LEB identified during recovery that needs to be
  888. * written but was not because UBIFS was mounted read-only. This happens when
  889. * remounting to read-write mode.
  890. *
  891. * This function returns %0 on success and a negative error code on failure.
  892. */
  893. int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
  894. {
  895. dbg_rcvry("recovery");
  896. while (!list_empty(&c->unclean_leb_list)) {
  897. struct ubifs_unclean_leb *ucleb;
  898. int err;
  899. ucleb = list_entry(c->unclean_leb_list.next,
  900. struct ubifs_unclean_leb, list);
  901. err = clean_an_unclean_leb(c, ucleb, sbuf);
  902. if (err)
  903. return err;
  904. list_del(&ucleb->list);
  905. kfree(ucleb);
  906. }
  907. return 0;
  908. }
  909. /**
  910. * struct size_entry - inode size information for recovery.
  911. * @rb: link in the RB-tree of sizes
  912. * @inum: inode number
  913. * @i_size: size on inode
  914. * @d_size: maximum size based on data nodes
  915. * @exists: indicates whether the inode exists
  916. * @inode: inode if pinned in memory awaiting rw mode to fix it
  917. */
  918. struct size_entry {
  919. struct rb_node rb;
  920. ino_t inum;
  921. loff_t i_size;
  922. loff_t d_size;
  923. int exists;
  924. struct inode *inode;
  925. };
  926. /**
  927. * add_ino - add an entry to the size tree.
  928. * @c: UBIFS file-system description object
  929. * @inum: inode number
  930. * @i_size: size on inode
  931. * @d_size: maximum size based on data nodes
  932. * @exists: indicates whether the inode exists
  933. */
  934. static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
  935. loff_t d_size, int exists)
  936. {
  937. struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
  938. struct size_entry *e;
  939. while (*p) {
  940. parent = *p;
  941. e = rb_entry(parent, struct size_entry, rb);
  942. if (inum < e->inum)
  943. p = &(*p)->rb_left;
  944. else
  945. p = &(*p)->rb_right;
  946. }
  947. e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
  948. if (!e)
  949. return -ENOMEM;
  950. e->inum = inum;
  951. e->i_size = i_size;
  952. e->d_size = d_size;
  953. e->exists = exists;
  954. rb_link_node(&e->rb, parent, p);
  955. rb_insert_color(&e->rb, &c->size_tree);
  956. return 0;
  957. }
  958. /**
  959. * find_ino - find an entry on the size tree.
  960. * @c: UBIFS file-system description object
  961. * @inum: inode number
  962. */
  963. static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
  964. {
  965. struct rb_node *p = c->size_tree.rb_node;
  966. struct size_entry *e;
  967. while (p) {
  968. e = rb_entry(p, struct size_entry, rb);
  969. if (inum < e->inum)
  970. p = p->rb_left;
  971. else if (inum > e->inum)
  972. p = p->rb_right;
  973. else
  974. return e;
  975. }
  976. return NULL;
  977. }
  978. /**
  979. * remove_ino - remove an entry from the size tree.
  980. * @c: UBIFS file-system description object
  981. * @inum: inode number
  982. */
  983. static void remove_ino(struct ubifs_info *c, ino_t inum)
  984. {
  985. struct size_entry *e = find_ino(c, inum);
  986. if (!e)
  987. return;
  988. rb_erase(&e->rb, &c->size_tree);
  989. kfree(e);
  990. }
  991. /**
  992. * ubifs_recover_size_accum - accumulate inode sizes for recovery.
  993. * @c: UBIFS file-system description object
  994. * @key: node key
  995. * @deletion: node is for a deletion
  996. * @new_size: inode size
  997. *
  998. * This function has two purposes:
  999. * 1) to ensure there are no data nodes that fall outside the inode size
  1000. * 2) to ensure there are no data nodes for inodes that do not exist
  1001. * To accomplish those purposes, a rb-tree is constructed containing an entry
  1002. * for each inode number in the journal that has not been deleted, and recording
  1003. * the size from the inode node, the maximum size of any data node (also altered
  1004. * by truncations) and a flag indicating a inode number for which no inode node
  1005. * was present in the journal.
  1006. *
  1007. * Note that there is still the possibility that there are data nodes that have
  1008. * been committed that are beyond the inode size, however the only way to find
  1009. * them would be to scan the entire index. Alternatively, some provision could
  1010. * be made to record the size of inodes at the start of commit, which would seem
  1011. * very cumbersome for a scenario that is quite unlikely and the only negative
  1012. * consequence of which is wasted space.
  1013. *
  1014. * This functions returns %0 on success and a negative error code on failure.
  1015. */
  1016. int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
  1017. int deletion, loff_t new_size)
  1018. {
  1019. ino_t inum = key_inum(c, key);
  1020. struct size_entry *e;
  1021. int err;
  1022. switch (key_type(c, key)) {
  1023. case UBIFS_INO_KEY:
  1024. if (deletion)
  1025. remove_ino(c, inum);
  1026. else {
  1027. e = find_ino(c, inum);
  1028. if (e) {
  1029. e->i_size = new_size;
  1030. e->exists = 1;
  1031. } else {
  1032. err = add_ino(c, inum, new_size, 0, 1);
  1033. if (err)
  1034. return err;
  1035. }
  1036. }
  1037. break;
  1038. case UBIFS_DATA_KEY:
  1039. e = find_ino(c, inum);
  1040. if (e) {
  1041. if (new_size > e->d_size)
  1042. e->d_size = new_size;
  1043. } else {
  1044. err = add_ino(c, inum, 0, new_size, 0);
  1045. if (err)
  1046. return err;
  1047. }
  1048. break;
  1049. case UBIFS_TRUN_KEY:
  1050. e = find_ino(c, inum);
  1051. if (e)
  1052. e->d_size = new_size;
  1053. break;
  1054. }
  1055. return 0;
  1056. }
  1057. /**
  1058. * ubifs_recover_size - recover inode size.
  1059. * @c: UBIFS file-system description object
  1060. *
  1061. * This function attempts to fix inode size discrepancies identified by the
  1062. * 'ubifs_recover_size_accum()' function.
  1063. *
  1064. * This functions returns %0 on success and a negative error code on failure.
  1065. */
  1066. int ubifs_recover_size(struct ubifs_info *c)
  1067. {
  1068. struct rb_node *this = rb_first(&c->size_tree);
  1069. while (this) {
  1070. struct size_entry *e;
  1071. int err;
  1072. e = rb_entry(this, struct size_entry, rb);
  1073. if (!e->exists) {
  1074. union ubifs_key key;
  1075. ino_key_init(c, &key, e->inum);
  1076. err = ubifs_tnc_lookup(c, &key, c->sbuf);
  1077. if (err && err != -ENOENT)
  1078. return err;
  1079. if (err == -ENOENT) {
  1080. /* Remove data nodes that have no inode */
  1081. dbg_rcvry("removing ino %lu",
  1082. (unsigned long)e->inum);
  1083. err = ubifs_tnc_remove_ino(c, e->inum);
  1084. if (err)
  1085. return err;
  1086. } else {
  1087. struct ubifs_ino_node *ino = c->sbuf;
  1088. e->exists = 1;
  1089. e->i_size = le64_to_cpu(ino->size);
  1090. }
  1091. }
  1092. if (e->exists && e->i_size < e->d_size) {
  1093. if (!e->inode && (c->vfs_sb->s_flags & MS_RDONLY)) {
  1094. /* Fix the inode size and pin it in memory */
  1095. struct inode *inode;
  1096. inode = ubifs_iget(c->vfs_sb, e->inum);
  1097. if (IS_ERR(inode))
  1098. return PTR_ERR(inode);
  1099. if (inode->i_size < e->d_size) {
  1100. dbg_rcvry("ino %lu size %lld -> %lld",
  1101. (unsigned long)e->inum,
  1102. e->d_size, inode->i_size);
  1103. inode->i_size = e->d_size;
  1104. ubifs_inode(inode)->ui_size = e->d_size;
  1105. e->inode = inode;
  1106. this = rb_next(this);
  1107. continue;
  1108. }
  1109. iput(inode);
  1110. }
  1111. }
  1112. this = rb_next(this);
  1113. rb_erase(&e->rb, &c->size_tree);
  1114. kfree(e);
  1115. }
  1116. return 0;
  1117. }