recovery.c 43 KB

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