recovery.c 42 KB

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