recovery.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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 = ubi_read(c->ubi, lnum, sbuf, 0, c->leb_size);
  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 = ubi_leb_change(c->ubi, lnum, mst, sz, UBI_SHORTTERM);
  199. if (err)
  200. goto out;
  201. err = ubi_leb_change(c->ubi, 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 && offs2 + sz >= c->leb_size) {
  255. /* 1st LEB was unmapped and written, 2nd not */
  256. if (cor1)
  257. goto out_err;
  258. mst = mst1;
  259. } else
  260. goto out_err;
  261. } else {
  262. /*
  263. * 2nd LEB was unmapped and about to be written, so
  264. * there must be only one master node in the first LEB
  265. * and no corruption.
  266. */
  267. if (offs1 != 0 || cor1)
  268. goto out_err;
  269. mst = mst1;
  270. }
  271. } else {
  272. if (!mst2)
  273. goto out_err;
  274. /*
  275. * 1st LEB was unmapped and about to be written, so there must
  276. * be no room left in 2nd LEB.
  277. */
  278. offs2 = (void *)mst2 - buf2;
  279. if (offs2 + sz + sz <= c->leb_size)
  280. goto out_err;
  281. mst = mst2;
  282. }
  283. ubifs_msg("recovered master node from LEB %d",
  284. (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
  285. memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
  286. if (c->ro_mount) {
  287. /* Read-only mode. Keep a copy for switching to rw mode */
  288. c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
  289. if (!c->rcvrd_mst_node) {
  290. err = -ENOMEM;
  291. goto out_free;
  292. }
  293. memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
  294. /*
  295. * We had to recover the master node, which means there was an
  296. * unclean reboot. However, it is possible that the master node
  297. * is clean at this point, i.e., %UBIFS_MST_DIRTY is not set.
  298. * E.g., consider the following chain of events:
  299. *
  300. * 1. UBIFS was cleanly unmounted, so the master node is clean
  301. * 2. UBIFS is being mounted R/W and starts changing the master
  302. * node in the first (%UBIFS_MST_LNUM). A power cut happens,
  303. * so this LEB ends up with some amount of garbage at the
  304. * end.
  305. * 3. UBIFS is being mounted R/O. We reach this place and
  306. * recover the master node from the second LEB
  307. * (%UBIFS_MST_LNUM + 1). But we cannot update the media
  308. * because we are being mounted R/O. We have to defer the
  309. * operation.
  310. * 4. However, this master node (@c->mst_node) is marked as
  311. * clean (since the step 1). And if we just return, the
  312. * mount code will be confused and won't recover the master
  313. * node when it is re-mounter R/W later.
  314. *
  315. * Thus, to force the recovery by marking the master node as
  316. * dirty.
  317. */
  318. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  319. } else {
  320. /* Write the recovered master node */
  321. c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
  322. err = write_rcvrd_mst_node(c, c->mst_node);
  323. if (err)
  324. goto out_free;
  325. }
  326. vfree(buf2);
  327. vfree(buf1);
  328. return 0;
  329. out_err:
  330. err = -EINVAL;
  331. out_free:
  332. ubifs_err("failed to recover master node");
  333. if (mst1) {
  334. dbg_err("dumping first master node");
  335. dbg_dump_node(c, mst1);
  336. }
  337. if (mst2) {
  338. dbg_err("dumping second master node");
  339. dbg_dump_node(c, mst2);
  340. }
  341. vfree(buf2);
  342. vfree(buf1);
  343. return err;
  344. }
  345. /**
  346. * ubifs_write_rcvrd_mst_node - write the recovered master node.
  347. * @c: UBIFS file-system description object
  348. *
  349. * This function writes the master node that was recovered during mounting in
  350. * read-only mode and must now be written because we are remounting rw.
  351. *
  352. * This function returns %0 on success and a negative error code on failure.
  353. */
  354. int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
  355. {
  356. int err;
  357. if (!c->rcvrd_mst_node)
  358. return 0;
  359. c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  360. c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
  361. err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
  362. if (err)
  363. return err;
  364. kfree(c->rcvrd_mst_node);
  365. c->rcvrd_mst_node = NULL;
  366. return 0;
  367. }
  368. /**
  369. * is_last_write - determine if an offset was in the last write to a LEB.
  370. * @c: UBIFS file-system description object
  371. * @buf: buffer to check
  372. * @offs: offset to check
  373. *
  374. * This function returns %1 if @offs was in the last write to the LEB whose data
  375. * is in @buf, otherwise %0 is returned. The determination is made by checking
  376. * for subsequent empty space starting from the next @c->max_write_size
  377. * boundary.
  378. */
  379. static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
  380. {
  381. int empty_offs, check_len;
  382. uint8_t *p;
  383. /*
  384. * Round up to the next @c->max_write_size boundary i.e. @offs is in
  385. * the last wbuf written. After that should be empty space.
  386. */
  387. empty_offs = ALIGN(offs + 1, c->max_write_size);
  388. check_len = c->leb_size - empty_offs;
  389. p = buf + empty_offs - offs;
  390. return is_empty(p, check_len);
  391. }
  392. /**
  393. * clean_buf - clean the data from an LEB sitting in a buffer.
  394. * @c: UBIFS file-system description object
  395. * @buf: buffer to clean
  396. * @lnum: LEB number to clean
  397. * @offs: offset from which to clean
  398. * @len: length of buffer
  399. *
  400. * This function pads up to the next min_io_size boundary (if there is one) and
  401. * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
  402. * @c->min_io_size boundary.
  403. */
  404. static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
  405. int *offs, int *len)
  406. {
  407. int empty_offs, pad_len;
  408. lnum = lnum;
  409. dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
  410. ubifs_assert(!(*offs & 7));
  411. empty_offs = ALIGN(*offs, c->min_io_size);
  412. pad_len = empty_offs - *offs;
  413. ubifs_pad(c, *buf, pad_len);
  414. *offs += pad_len;
  415. *buf += pad_len;
  416. *len -= pad_len;
  417. memset(*buf, 0xff, c->leb_size - empty_offs);
  418. }
  419. /**
  420. * no_more_nodes - determine if there are no more nodes in a buffer.
  421. * @c: UBIFS file-system description object
  422. * @buf: buffer to check
  423. * @len: length of buffer
  424. * @lnum: LEB number of the LEB from which @buf was read
  425. * @offs: offset from which @buf was read
  426. *
  427. * This function ensures that the corrupted node at @offs is the last thing
  428. * written to a LEB. This function returns %1 if more data is not found and
  429. * %0 if more data is found.
  430. */
  431. static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
  432. int lnum, int offs)
  433. {
  434. struct ubifs_ch *ch = buf;
  435. int skip, dlen = le32_to_cpu(ch->len);
  436. /* Check for empty space after the corrupt node's common header */
  437. skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs;
  438. if (is_empty(buf + skip, len - skip))
  439. return 1;
  440. /*
  441. * The area after the common header size is not empty, so the common
  442. * header must be intact. Check it.
  443. */
  444. if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) {
  445. dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
  446. return 0;
  447. }
  448. /* Now we know the corrupt node's length we can skip over it */
  449. skip = ALIGN(offs + dlen, c->max_write_size) - offs;
  450. /* After which there should be empty space */
  451. if (is_empty(buf + skip, len - skip))
  452. return 1;
  453. dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
  454. return 0;
  455. }
  456. /**
  457. * fix_unclean_leb - fix an unclean LEB.
  458. * @c: UBIFS file-system description object
  459. * @sleb: scanned LEB information
  460. * @start: offset where scan started
  461. */
  462. static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
  463. int start)
  464. {
  465. int lnum = sleb->lnum, endpt = start;
  466. /* Get the end offset of the last node we are keeping */
  467. if (!list_empty(&sleb->nodes)) {
  468. struct ubifs_scan_node *snod;
  469. snod = list_entry(sleb->nodes.prev,
  470. struct ubifs_scan_node, list);
  471. endpt = snod->offs + snod->len;
  472. }
  473. if (c->ro_mount && !c->remounting_rw) {
  474. /* Add to recovery list */
  475. struct ubifs_unclean_leb *ucleb;
  476. dbg_rcvry("need to fix LEB %d start %d endpt %d",
  477. lnum, start, sleb->endpt);
  478. ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
  479. if (!ucleb)
  480. return -ENOMEM;
  481. ucleb->lnum = lnum;
  482. ucleb->endpt = endpt;
  483. list_add_tail(&ucleb->list, &c->unclean_leb_list);
  484. } else {
  485. /* Write the fixed LEB back to flash */
  486. int err;
  487. dbg_rcvry("fixing LEB %d start %d endpt %d",
  488. lnum, start, sleb->endpt);
  489. if (endpt == 0) {
  490. err = ubifs_leb_unmap(c, lnum);
  491. if (err)
  492. return err;
  493. } else {
  494. int len = ALIGN(endpt, c->min_io_size);
  495. if (start) {
  496. err = ubi_read(c->ubi, lnum, sleb->buf, 0,
  497. start);
  498. if (err)
  499. return err;
  500. }
  501. /* Pad to min_io_size */
  502. if (len > endpt) {
  503. int pad_len = len - ALIGN(endpt, 8);
  504. if (pad_len > 0) {
  505. void *buf = sleb->buf + len - pad_len;
  506. ubifs_pad(c, buf, pad_len);
  507. }
  508. }
  509. err = ubi_leb_change(c->ubi, lnum, sleb->buf, len,
  510. UBI_UNKNOWN);
  511. if (err)
  512. return err;
  513. }
  514. }
  515. return 0;
  516. }
  517. /**
  518. * drop_incomplete_group - drop nodes from an incomplete group.
  519. * @sleb: scanned LEB information
  520. * @offs: offset of dropped nodes is returned here
  521. *
  522. * This function returns %1 if nodes are dropped and %0 otherwise.
  523. */
  524. static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
  525. {
  526. int dropped = 0;
  527. while (!list_empty(&sleb->nodes)) {
  528. struct ubifs_scan_node *snod;
  529. struct ubifs_ch *ch;
  530. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  531. list);
  532. ch = snod->node;
  533. if (ch->group_type != UBIFS_IN_NODE_GROUP)
  534. return dropped;
  535. dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
  536. *offs = snod->offs;
  537. list_del(&snod->list);
  538. kfree(snod);
  539. sleb->nodes_cnt -= 1;
  540. dropped = 1;
  541. }
  542. return dropped;
  543. }
  544. /**
  545. * ubifs_recover_leb - scan and recover a LEB.
  546. * @c: UBIFS file-system description object
  547. * @lnum: LEB number
  548. * @offs: offset
  549. * @sbuf: LEB-sized buffer to use
  550. * @grouped: nodes may be grouped for recovery
  551. *
  552. * This function does a scan of a LEB, but caters for errors that might have
  553. * been caused by the unclean unmount from which we are attempting to recover.
  554. * Returns %0 in case of success, %-EUCLEAN if an unrecoverable corruption is
  555. * found, and a negative error code in case of failure.
  556. */
  557. struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
  558. int offs, void *sbuf, int grouped)
  559. {
  560. int err, len = c->leb_size - offs, need_clean = 0, quiet = 1;
  561. int empty_chkd = 0, start = offs;
  562. struct ubifs_scan_leb *sleb;
  563. void *buf = sbuf + offs;
  564. dbg_rcvry("%d:%d", lnum, offs);
  565. sleb = ubifs_start_scan(c, lnum, offs, sbuf);
  566. if (IS_ERR(sleb))
  567. return sleb;
  568. if (sleb->ecc)
  569. need_clean = 1;
  570. while (len >= 8) {
  571. int ret;
  572. dbg_scan("look at LEB %d:%d (%d bytes left)",
  573. lnum, offs, len);
  574. cond_resched();
  575. /*
  576. * Scan quietly until there is an error from which we cannot
  577. * recover
  578. */
  579. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  580. if (ret == SCANNED_A_NODE) {
  581. /* A valid node, and not a padding node */
  582. struct ubifs_ch *ch = buf;
  583. int node_len;
  584. err = ubifs_add_snod(c, sleb, buf, offs);
  585. if (err)
  586. goto error;
  587. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  588. offs += node_len;
  589. buf += node_len;
  590. len -= node_len;
  591. continue;
  592. }
  593. if (ret > 0) {
  594. /* Padding bytes or a valid padding node */
  595. offs += ret;
  596. buf += ret;
  597. len -= ret;
  598. continue;
  599. }
  600. if (ret == SCANNED_EMPTY_SPACE) {
  601. if (!is_empty(buf, len)) {
  602. if (!is_last_write(c, buf, offs))
  603. break;
  604. clean_buf(c, &buf, lnum, &offs, &len);
  605. need_clean = 1;
  606. }
  607. empty_chkd = 1;
  608. break;
  609. }
  610. if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE)
  611. if (is_last_write(c, buf, offs)) {
  612. clean_buf(c, &buf, lnum, &offs, &len);
  613. need_clean = 1;
  614. empty_chkd = 1;
  615. break;
  616. }
  617. if (ret == SCANNED_A_CORRUPT_NODE)
  618. if (no_more_nodes(c, buf, len, lnum, offs)) {
  619. clean_buf(c, &buf, lnum, &offs, &len);
  620. need_clean = 1;
  621. empty_chkd = 1;
  622. break;
  623. }
  624. if (quiet) {
  625. /* Redo the last scan but noisily */
  626. quiet = 0;
  627. continue;
  628. }
  629. switch (ret) {
  630. case SCANNED_GARBAGE:
  631. dbg_err("garbage");
  632. goto corrupted;
  633. case SCANNED_A_CORRUPT_NODE:
  634. case SCANNED_A_BAD_PAD_NODE:
  635. dbg_err("bad node");
  636. goto corrupted;
  637. default:
  638. dbg_err("unknown");
  639. err = -EINVAL;
  640. goto error;
  641. }
  642. }
  643. if (!empty_chkd && !is_empty(buf, len)) {
  644. if (is_last_write(c, buf, offs)) {
  645. clean_buf(c, &buf, lnum, &offs, &len);
  646. need_clean = 1;
  647. } else {
  648. int corruption = first_non_ff(buf, len);
  649. /*
  650. * See header comment for this file for more
  651. * explanations about the reasons we have this check.
  652. */
  653. ubifs_err("corrupt empty space LEB %d:%d, corruption "
  654. "starts at %d", lnum, offs, corruption);
  655. /* Make sure we dump interesting non-0xFF data */
  656. offs += corruption;
  657. buf += corruption;
  658. goto corrupted;
  659. }
  660. }
  661. /* Drop nodes from incomplete group */
  662. if (grouped && drop_incomplete_group(sleb, &offs)) {
  663. buf = sbuf + offs;
  664. len = c->leb_size - offs;
  665. clean_buf(c, &buf, lnum, &offs, &len);
  666. need_clean = 1;
  667. }
  668. if (offs % c->min_io_size) {
  669. clean_buf(c, &buf, lnum, &offs, &len);
  670. need_clean = 1;
  671. }
  672. ubifs_end_scan(c, sleb, lnum, offs);
  673. if (need_clean) {
  674. err = fix_unclean_leb(c, sleb, start);
  675. if (err)
  676. goto error;
  677. }
  678. return sleb;
  679. corrupted:
  680. ubifs_scanned_corruption(c, lnum, offs, buf);
  681. err = -EUCLEAN;
  682. error:
  683. ubifs_err("LEB %d scanning failed", lnum);
  684. ubifs_scan_destroy(sleb);
  685. return ERR_PTR(err);
  686. }
  687. /**
  688. * get_cs_sqnum - get commit start sequence number.
  689. * @c: UBIFS file-system description object
  690. * @lnum: LEB number of commit start node
  691. * @offs: offset of commit start node
  692. * @cs_sqnum: commit start sequence number is returned here
  693. *
  694. * This function returns %0 on success and a negative error code on failure.
  695. */
  696. static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
  697. unsigned long long *cs_sqnum)
  698. {
  699. struct ubifs_cs_node *cs_node = NULL;
  700. int err, ret;
  701. dbg_rcvry("at %d:%d", lnum, offs);
  702. cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
  703. if (!cs_node)
  704. return -ENOMEM;
  705. if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
  706. goto out_err;
  707. err = ubi_read(c->ubi, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ);
  708. if (err && err != -EBADMSG)
  709. goto out_free;
  710. ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
  711. if (ret != SCANNED_A_NODE) {
  712. dbg_err("Not a valid node");
  713. goto out_err;
  714. }
  715. if (cs_node->ch.node_type != UBIFS_CS_NODE) {
  716. dbg_err("Node a CS node, type is %d", cs_node->ch.node_type);
  717. goto out_err;
  718. }
  719. if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
  720. dbg_err("CS node cmt_no %llu != current cmt_no %llu",
  721. (unsigned long long)le64_to_cpu(cs_node->cmt_no),
  722. c->cmt_no);
  723. goto out_err;
  724. }
  725. *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
  726. dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
  727. kfree(cs_node);
  728. return 0;
  729. out_err:
  730. err = -EINVAL;
  731. out_free:
  732. ubifs_err("failed to get CS sqnum");
  733. kfree(cs_node);
  734. return err;
  735. }
  736. /**
  737. * ubifs_recover_log_leb - scan and recover a log LEB.
  738. * @c: UBIFS file-system description object
  739. * @lnum: LEB number
  740. * @offs: offset
  741. * @sbuf: LEB-sized buffer to use
  742. *
  743. * This function does a scan of a LEB, but caters for errors that might have
  744. * been caused by unclean reboots from which we are attempting to recover
  745. * (assume that only the last log LEB can be corrupted by an unclean reboot).
  746. *
  747. * This function returns %0 on success and a negative error code on failure.
  748. */
  749. struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
  750. int offs, void *sbuf)
  751. {
  752. struct ubifs_scan_leb *sleb;
  753. int next_lnum;
  754. dbg_rcvry("LEB %d", lnum);
  755. next_lnum = lnum + 1;
  756. if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  757. next_lnum = UBIFS_LOG_LNUM;
  758. if (next_lnum != c->ltail_lnum) {
  759. /*
  760. * We can only recover at the end of the log, so check that the
  761. * next log LEB is empty or out of date.
  762. */
  763. sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0);
  764. if (IS_ERR(sleb))
  765. return sleb;
  766. if (sleb->nodes_cnt) {
  767. struct ubifs_scan_node *snod;
  768. unsigned long long cs_sqnum = c->cs_sqnum;
  769. snod = list_entry(sleb->nodes.next,
  770. struct ubifs_scan_node, list);
  771. if (cs_sqnum == 0) {
  772. int err;
  773. err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
  774. if (err) {
  775. ubifs_scan_destroy(sleb);
  776. return ERR_PTR(err);
  777. }
  778. }
  779. if (snod->sqnum > cs_sqnum) {
  780. ubifs_err("unrecoverable log corruption "
  781. "in LEB %d", lnum);
  782. ubifs_scan_destroy(sleb);
  783. return ERR_PTR(-EUCLEAN);
  784. }
  785. }
  786. ubifs_scan_destroy(sleb);
  787. }
  788. return ubifs_recover_leb(c, lnum, offs, sbuf, 0);
  789. }
  790. /**
  791. * recover_head - recover a head.
  792. * @c: UBIFS file-system description object
  793. * @lnum: LEB number of head to recover
  794. * @offs: offset of head to recover
  795. * @sbuf: LEB-sized buffer to use
  796. *
  797. * This function ensures that there is no data on the flash at a head location.
  798. *
  799. * This function returns %0 on success and a negative error code on failure.
  800. */
  801. static int recover_head(const struct ubifs_info *c, int lnum, int offs,
  802. void *sbuf)
  803. {
  804. int len = c->max_write_size, err;
  805. if (offs + len > c->leb_size)
  806. len = c->leb_size - offs;
  807. if (!len)
  808. return 0;
  809. /* Read at the head location and check it is empty flash */
  810. err = ubi_read(c->ubi, lnum, sbuf, offs, len);
  811. if (err || !is_empty(sbuf, len)) {
  812. dbg_rcvry("cleaning head at %d:%d", lnum, offs);
  813. if (offs == 0)
  814. return ubifs_leb_unmap(c, lnum);
  815. err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
  816. if (err)
  817. return err;
  818. return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
  819. }
  820. return 0;
  821. }
  822. /**
  823. * ubifs_recover_inl_heads - recover index and LPT heads.
  824. * @c: UBIFS file-system description object
  825. * @sbuf: LEB-sized buffer to use
  826. *
  827. * This function ensures that there is no data on the flash at the index and
  828. * LPT head locations.
  829. *
  830. * This deals with the recovery of a half-completed journal commit. UBIFS is
  831. * careful never to overwrite the last version of the index or the LPT. Because
  832. * the index and LPT are wandering trees, data from a half-completed commit will
  833. * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
  834. * assumed to be empty and will be unmapped anyway before use, or in the index
  835. * and LPT heads.
  836. *
  837. * This function returns %0 on success and a negative error code on failure.
  838. */
  839. int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
  840. {
  841. int err;
  842. ubifs_assert(!c->ro_mount || c->remounting_rw);
  843. dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
  844. err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
  845. if (err)
  846. return err;
  847. dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
  848. err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
  849. if (err)
  850. return err;
  851. return 0;
  852. }
  853. /**
  854. * clean_an_unclean_leb - read and write a LEB to remove corruption.
  855. * @c: UBIFS file-system description object
  856. * @ucleb: unclean LEB information
  857. * @sbuf: LEB-sized buffer to use
  858. *
  859. * This function reads a LEB up to a point pre-determined by the mount recovery,
  860. * checks the nodes, and writes the result back to the flash, thereby cleaning
  861. * off any following corruption, or non-fatal ECC errors.
  862. *
  863. * This function returns %0 on success and a negative error code on failure.
  864. */
  865. static int clean_an_unclean_leb(const struct ubifs_info *c,
  866. struct ubifs_unclean_leb *ucleb, void *sbuf)
  867. {
  868. int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
  869. void *buf = sbuf;
  870. dbg_rcvry("LEB %d len %d", lnum, len);
  871. if (len == 0) {
  872. /* Nothing to read, just unmap it */
  873. err = ubifs_leb_unmap(c, lnum);
  874. if (err)
  875. return err;
  876. return 0;
  877. }
  878. err = ubi_read(c->ubi, lnum, buf, offs, len);
  879. if (err && err != -EBADMSG)
  880. return err;
  881. while (len >= 8) {
  882. int ret;
  883. cond_resched();
  884. /* Scan quietly until there is an error */
  885. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  886. if (ret == SCANNED_A_NODE) {
  887. /* A valid node, and not a padding node */
  888. struct ubifs_ch *ch = buf;
  889. int node_len;
  890. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  891. offs += node_len;
  892. buf += node_len;
  893. len -= node_len;
  894. continue;
  895. }
  896. if (ret > 0) {
  897. /* Padding bytes or a valid padding node */
  898. offs += ret;
  899. buf += ret;
  900. len -= ret;
  901. continue;
  902. }
  903. if (ret == SCANNED_EMPTY_SPACE) {
  904. ubifs_err("unexpected empty space at %d:%d",
  905. lnum, offs);
  906. return -EUCLEAN;
  907. }
  908. if (quiet) {
  909. /* Redo the last scan but noisily */
  910. quiet = 0;
  911. continue;
  912. }
  913. ubifs_scanned_corruption(c, lnum, offs, buf);
  914. return -EUCLEAN;
  915. }
  916. /* Pad to min_io_size */
  917. len = ALIGN(ucleb->endpt, c->min_io_size);
  918. if (len > ucleb->endpt) {
  919. int pad_len = len - ALIGN(ucleb->endpt, 8);
  920. if (pad_len > 0) {
  921. buf = c->sbuf + len - pad_len;
  922. ubifs_pad(c, buf, pad_len);
  923. }
  924. }
  925. /* Write back the LEB atomically */
  926. err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
  927. if (err)
  928. return err;
  929. dbg_rcvry("cleaned LEB %d", lnum);
  930. return 0;
  931. }
  932. /**
  933. * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
  934. * @c: UBIFS file-system description object
  935. * @sbuf: LEB-sized buffer to use
  936. *
  937. * This function cleans a LEB identified during recovery that needs to be
  938. * written but was not because UBIFS was mounted read-only. This happens when
  939. * remounting to read-write mode.
  940. *
  941. * This function returns %0 on success and a negative error code on failure.
  942. */
  943. int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
  944. {
  945. dbg_rcvry("recovery");
  946. while (!list_empty(&c->unclean_leb_list)) {
  947. struct ubifs_unclean_leb *ucleb;
  948. int err;
  949. ucleb = list_entry(c->unclean_leb_list.next,
  950. struct ubifs_unclean_leb, list);
  951. err = clean_an_unclean_leb(c, ucleb, sbuf);
  952. if (err)
  953. return err;
  954. list_del(&ucleb->list);
  955. kfree(ucleb);
  956. }
  957. return 0;
  958. }
  959. /**
  960. * grab_empty_leb - grab an empty LEB to use as GC LEB and run commit.
  961. * @c: UBIFS file-system description object
  962. *
  963. * This is a helper function for 'ubifs_rcvry_gc_commit()' which grabs an empty
  964. * LEB to be used as GC LEB (@c->gc_lnum), and then runs the commit. Returns
  965. * zero in case of success and a negative error code in case of failure.
  966. */
  967. static int grab_empty_leb(struct ubifs_info *c)
  968. {
  969. int lnum, err;
  970. /*
  971. * Note, it is very important to first search for an empty LEB and then
  972. * run the commit, not vice-versa. The reason is that there might be
  973. * only one empty LEB at the moment, the one which has been the
  974. * @c->gc_lnum just before the power cut happened. During the regular
  975. * UBIFS operation (not now) @c->gc_lnum is marked as "taken", so no
  976. * one but GC can grab it. But at this moment this single empty LEB is
  977. * not marked as taken, so if we run commit - what happens? Right, the
  978. * commit will grab it and write the index there. Remember that the
  979. * index always expands as long as there is free space, and it only
  980. * starts consolidating when we run out of space.
  981. *
  982. * IOW, if we run commit now, we might not be able to find a free LEB
  983. * after this.
  984. */
  985. lnum = ubifs_find_free_leb_for_idx(c);
  986. if (lnum < 0) {
  987. dbg_err("could not find an empty LEB");
  988. dbg_dump_lprops(c);
  989. dbg_dump_budg(c, &c->bi);
  990. return lnum;
  991. }
  992. /* Reset the index flag */
  993. err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
  994. LPROPS_INDEX, 0);
  995. if (err)
  996. return err;
  997. c->gc_lnum = lnum;
  998. dbg_rcvry("found empty LEB %d, run commit", lnum);
  999. return ubifs_run_commit(c);
  1000. }
  1001. /**
  1002. * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
  1003. * @c: UBIFS file-system description object
  1004. *
  1005. * Out-of-place garbage collection requires always one empty LEB with which to
  1006. * start garbage collection. The LEB number is recorded in c->gc_lnum and is
  1007. * written to the master node on unmounting. In the case of an unclean unmount
  1008. * the value of gc_lnum recorded in the master node is out of date and cannot
  1009. * be used. Instead, recovery must allocate an empty LEB for this purpose.
  1010. * However, there may not be enough empty space, in which case it must be
  1011. * possible to GC the dirtiest LEB into the GC head LEB.
  1012. *
  1013. * This function also runs the commit which causes the TNC updates from
  1014. * size-recovery and orphans to be written to the flash. That is important to
  1015. * ensure correct replay order for subsequent mounts.
  1016. *
  1017. * This function returns %0 on success and a negative error code on failure.
  1018. */
  1019. int ubifs_rcvry_gc_commit(struct ubifs_info *c)
  1020. {
  1021. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  1022. struct ubifs_lprops lp;
  1023. int lnum, err;
  1024. c->gc_lnum = -1;
  1025. if (wbuf->lnum == -1) {
  1026. dbg_rcvry("no GC head LEB");
  1027. return grab_empty_leb(c);
  1028. }
  1029. /*
  1030. * See whether the used space in the dirtiest LEB fits in the GC head
  1031. * LEB.
  1032. */
  1033. if (wbuf->offs == c->leb_size) {
  1034. dbg_rcvry("no room in GC head LEB");
  1035. return grab_empty_leb(c);
  1036. }
  1037. err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
  1038. if (err) {
  1039. /*
  1040. * There are no dirty or empty LEBs subject to here being
  1041. * enough for the index. Try to use
  1042. * 'ubifs_find_free_leb_for_idx()', which will return any empty
  1043. * LEBs (ignoring index requirements). If the index then
  1044. * doesn't have enough LEBs the recovery commit will fail -
  1045. * which is the same result anyway i.e. recovery fails. So
  1046. * there is no problem ignoring index requirements and just
  1047. * grabbing a free LEB since we have already established there
  1048. * is not a dirty LEB we could have used instead.
  1049. */
  1050. if (err == -ENOSPC) {
  1051. dbg_rcvry("could not find a dirty LEB");
  1052. return grab_empty_leb(c);
  1053. }
  1054. return err;
  1055. }
  1056. ubifs_assert(!(lp.flags & LPROPS_INDEX));
  1057. ubifs_assert(lp.free + lp.dirty >= wbuf->offs);
  1058. lnum = lp.lnum;
  1059. /*
  1060. * We run the commit before garbage collection otherwise subsequent
  1061. * mounts will see the GC and orphan deletion in a different order.
  1062. */
  1063. dbg_rcvry("committing");
  1064. err = ubifs_run_commit(c);
  1065. if (err)
  1066. return err;
  1067. /*
  1068. * The data in the dirtiest LEB fits in the GC head LEB, so do the GC
  1069. * - use locking to keep 'ubifs_assert()' happy.
  1070. */
  1071. dbg_rcvry("GC'ing LEB %d", lnum);
  1072. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  1073. err = ubifs_garbage_collect_leb(c, &lp);
  1074. if (err >= 0) {
  1075. int err2 = ubifs_wbuf_sync_nolock(wbuf);
  1076. if (err2)
  1077. err = err2;
  1078. }
  1079. mutex_unlock(&wbuf->io_mutex);
  1080. if (err < 0) {
  1081. dbg_err("GC failed, error %d", err);
  1082. if (err == -EAGAIN)
  1083. err = -EINVAL;
  1084. return err;
  1085. }
  1086. if (err != LEB_RETAINED) {
  1087. dbg_err("GC returned %d", err);
  1088. return -EINVAL;
  1089. }
  1090. err = ubifs_leb_unmap(c, c->gc_lnum);
  1091. if (err)
  1092. return err;
  1093. dbg_rcvry("allocated LEB %d for GC", lnum);
  1094. return 0;
  1095. }
  1096. /**
  1097. * struct size_entry - inode size information for recovery.
  1098. * @rb: link in the RB-tree of sizes
  1099. * @inum: inode number
  1100. * @i_size: size on inode
  1101. * @d_size: maximum size based on data nodes
  1102. * @exists: indicates whether the inode exists
  1103. * @inode: inode if pinned in memory awaiting rw mode to fix it
  1104. */
  1105. struct size_entry {
  1106. struct rb_node rb;
  1107. ino_t inum;
  1108. loff_t i_size;
  1109. loff_t d_size;
  1110. int exists;
  1111. struct inode *inode;
  1112. };
  1113. /**
  1114. * add_ino - add an entry to the size tree.
  1115. * @c: UBIFS file-system description object
  1116. * @inum: inode number
  1117. * @i_size: size on inode
  1118. * @d_size: maximum size based on data nodes
  1119. * @exists: indicates whether the inode exists
  1120. */
  1121. static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
  1122. loff_t d_size, int exists)
  1123. {
  1124. struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
  1125. struct size_entry *e;
  1126. while (*p) {
  1127. parent = *p;
  1128. e = rb_entry(parent, struct size_entry, rb);
  1129. if (inum < e->inum)
  1130. p = &(*p)->rb_left;
  1131. else
  1132. p = &(*p)->rb_right;
  1133. }
  1134. e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
  1135. if (!e)
  1136. return -ENOMEM;
  1137. e->inum = inum;
  1138. e->i_size = i_size;
  1139. e->d_size = d_size;
  1140. e->exists = exists;
  1141. rb_link_node(&e->rb, parent, p);
  1142. rb_insert_color(&e->rb, &c->size_tree);
  1143. return 0;
  1144. }
  1145. /**
  1146. * find_ino - find an entry on the size tree.
  1147. * @c: UBIFS file-system description object
  1148. * @inum: inode number
  1149. */
  1150. static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
  1151. {
  1152. struct rb_node *p = c->size_tree.rb_node;
  1153. struct size_entry *e;
  1154. while (p) {
  1155. e = rb_entry(p, struct size_entry, rb);
  1156. if (inum < e->inum)
  1157. p = p->rb_left;
  1158. else if (inum > e->inum)
  1159. p = p->rb_right;
  1160. else
  1161. return e;
  1162. }
  1163. return NULL;
  1164. }
  1165. /**
  1166. * remove_ino - remove an entry from the size tree.
  1167. * @c: UBIFS file-system description object
  1168. * @inum: inode number
  1169. */
  1170. static void remove_ino(struct ubifs_info *c, ino_t inum)
  1171. {
  1172. struct size_entry *e = find_ino(c, inum);
  1173. if (!e)
  1174. return;
  1175. rb_erase(&e->rb, &c->size_tree);
  1176. kfree(e);
  1177. }
  1178. /**
  1179. * ubifs_destroy_size_tree - free resources related to the size tree.
  1180. * @c: UBIFS file-system description object
  1181. */
  1182. void ubifs_destroy_size_tree(struct ubifs_info *c)
  1183. {
  1184. struct rb_node *this = c->size_tree.rb_node;
  1185. struct size_entry *e;
  1186. while (this) {
  1187. if (this->rb_left) {
  1188. this = this->rb_left;
  1189. continue;
  1190. } else if (this->rb_right) {
  1191. this = this->rb_right;
  1192. continue;
  1193. }
  1194. e = rb_entry(this, struct size_entry, rb);
  1195. if (e->inode)
  1196. iput(e->inode);
  1197. this = rb_parent(this);
  1198. if (this) {
  1199. if (this->rb_left == &e->rb)
  1200. this->rb_left = NULL;
  1201. else
  1202. this->rb_right = NULL;
  1203. }
  1204. kfree(e);
  1205. }
  1206. c->size_tree = RB_ROOT;
  1207. }
  1208. /**
  1209. * ubifs_recover_size_accum - accumulate inode sizes for recovery.
  1210. * @c: UBIFS file-system description object
  1211. * @key: node key
  1212. * @deletion: node is for a deletion
  1213. * @new_size: inode size
  1214. *
  1215. * This function has two purposes:
  1216. * 1) to ensure there are no data nodes that fall outside the inode size
  1217. * 2) to ensure there are no data nodes for inodes that do not exist
  1218. * To accomplish those purposes, a rb-tree is constructed containing an entry
  1219. * for each inode number in the journal that has not been deleted, and recording
  1220. * the size from the inode node, the maximum size of any data node (also altered
  1221. * by truncations) and a flag indicating a inode number for which no inode node
  1222. * was present in the journal.
  1223. *
  1224. * Note that there is still the possibility that there are data nodes that have
  1225. * been committed that are beyond the inode size, however the only way to find
  1226. * them would be to scan the entire index. Alternatively, some provision could
  1227. * be made to record the size of inodes at the start of commit, which would seem
  1228. * very cumbersome for a scenario that is quite unlikely and the only negative
  1229. * consequence of which is wasted space.
  1230. *
  1231. * This functions returns %0 on success and a negative error code on failure.
  1232. */
  1233. int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
  1234. int deletion, loff_t new_size)
  1235. {
  1236. ino_t inum = key_inum(c, key);
  1237. struct size_entry *e;
  1238. int err;
  1239. switch (key_type(c, key)) {
  1240. case UBIFS_INO_KEY:
  1241. if (deletion)
  1242. remove_ino(c, inum);
  1243. else {
  1244. e = find_ino(c, inum);
  1245. if (e) {
  1246. e->i_size = new_size;
  1247. e->exists = 1;
  1248. } else {
  1249. err = add_ino(c, inum, new_size, 0, 1);
  1250. if (err)
  1251. return err;
  1252. }
  1253. }
  1254. break;
  1255. case UBIFS_DATA_KEY:
  1256. e = find_ino(c, inum);
  1257. if (e) {
  1258. if (new_size > e->d_size)
  1259. e->d_size = new_size;
  1260. } else {
  1261. err = add_ino(c, inum, 0, new_size, 0);
  1262. if (err)
  1263. return err;
  1264. }
  1265. break;
  1266. case UBIFS_TRUN_KEY:
  1267. e = find_ino(c, inum);
  1268. if (e)
  1269. e->d_size = new_size;
  1270. break;
  1271. }
  1272. return 0;
  1273. }
  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 = ubi_read(c->ubi, lnum, c->sbuf, 0, c->leb_size);
  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 = ubi_leb_change(c->ubi, lnum, c->sbuf, len, UBI_UNKNOWN);
  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("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. /**
  1328. * ubifs_recover_size - recover inode size.
  1329. * @c: UBIFS file-system description object
  1330. *
  1331. * This function attempts to fix inode size discrepancies identified by the
  1332. * 'ubifs_recover_size_accum()' function.
  1333. *
  1334. * This functions returns %0 on success and a negative error code on failure.
  1335. */
  1336. int ubifs_recover_size(struct ubifs_info *c)
  1337. {
  1338. struct rb_node *this = rb_first(&c->size_tree);
  1339. while (this) {
  1340. struct size_entry *e;
  1341. int err;
  1342. e = rb_entry(this, struct size_entry, rb);
  1343. if (!e->exists) {
  1344. union ubifs_key key;
  1345. ino_key_init(c, &key, e->inum);
  1346. err = ubifs_tnc_lookup(c, &key, c->sbuf);
  1347. if (err && err != -ENOENT)
  1348. return err;
  1349. if (err == -ENOENT) {
  1350. /* Remove data nodes that have no inode */
  1351. dbg_rcvry("removing ino %lu",
  1352. (unsigned long)e->inum);
  1353. err = ubifs_tnc_remove_ino(c, e->inum);
  1354. if (err)
  1355. return err;
  1356. } else {
  1357. struct ubifs_ino_node *ino = c->sbuf;
  1358. e->exists = 1;
  1359. e->i_size = le64_to_cpu(ino->size);
  1360. }
  1361. }
  1362. if (e->exists && e->i_size < e->d_size) {
  1363. if (!e->inode && c->ro_mount) {
  1364. /* Fix the inode size and pin it in memory */
  1365. struct inode *inode;
  1366. inode = ubifs_iget(c->vfs_sb, e->inum);
  1367. if (IS_ERR(inode))
  1368. return PTR_ERR(inode);
  1369. if (inode->i_size < e->d_size) {
  1370. dbg_rcvry("ino %lu size %lld -> %lld",
  1371. (unsigned long)e->inum,
  1372. e->d_size, inode->i_size);
  1373. inode->i_size = e->d_size;
  1374. ubifs_inode(inode)->ui_size = e->d_size;
  1375. e->inode = inode;
  1376. this = rb_next(this);
  1377. continue;
  1378. }
  1379. iput(inode);
  1380. } else {
  1381. /* Fix the size in place */
  1382. err = fix_size_in_place(c, e);
  1383. if (err)
  1384. return err;
  1385. if (e->inode)
  1386. iput(e->inode);
  1387. }
  1388. }
  1389. this = rb_next(this);
  1390. rb_erase(&e->rb, &c->size_tree);
  1391. kfree(e);
  1392. }
  1393. return 0;
  1394. }