recovery.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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_last_group - drop the last group of nodes.
  519. * @sleb: scanned LEB information
  520. * @offs: offset of dropped nodes is returned here
  521. *
  522. * This is a helper function for 'ubifs_recover_leb()' which drops the last
  523. * group of nodes of the scanned LEB.
  524. */
  525. static void drop_last_group(struct ubifs_scan_leb *sleb, int *offs)
  526. {
  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. break;
  535. dbg_rcvry("dropping grouped node at %d:%d",
  536. sleb->lnum, snod->offs);
  537. *offs = snod->offs;
  538. list_del(&snod->list);
  539. kfree(snod);
  540. sleb->nodes_cnt -= 1;
  541. }
  542. }
  543. /**
  544. * drop_last_node - drop the last node.
  545. * @sleb: scanned LEB information
  546. * @offs: offset of dropped nodes is returned here
  547. * @grouped: non-zero if whole group of nodes have to be dropped
  548. *
  549. * This is a helper function for 'ubifs_recover_leb()' which drops the last
  550. * node of the scanned LEB.
  551. */
  552. static void drop_last_node(struct ubifs_scan_leb *sleb, int *offs)
  553. {
  554. struct ubifs_scan_node *snod;
  555. if (!list_empty(&sleb->nodes)) {
  556. snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
  557. list);
  558. dbg_rcvry("dropping last node at %d:%d", sleb->lnum, snod->offs);
  559. *offs = snod->offs;
  560. list_del(&snod->list);
  561. kfree(snod);
  562. sleb->nodes_cnt -= 1;
  563. }
  564. }
  565. /**
  566. * ubifs_recover_leb - scan and recover a LEB.
  567. * @c: UBIFS file-system description object
  568. * @lnum: LEB number
  569. * @offs: offset
  570. * @sbuf: LEB-sized buffer to use
  571. * @jhead: journal head number this LEB belongs to (%-1 if the LEB does not
  572. * belong to any journal head)
  573. *
  574. * This function does a scan of a LEB, but caters for errors that might have
  575. * been caused by the unclean unmount from which we are attempting to recover.
  576. * Returns %0 in case of success, %-EUCLEAN if an unrecoverable corruption is
  577. * found, and a negative error code in case of failure.
  578. */
  579. struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
  580. int offs, void *sbuf, int jhead)
  581. {
  582. int ret = 0, err, len = c->leb_size - offs, start = offs, min_io_unit;
  583. int grouped = jhead == -1 ? 0 : c->jheads[jhead].grouped;
  584. struct ubifs_scan_leb *sleb;
  585. void *buf = sbuf + offs;
  586. dbg_rcvry("%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped);
  587. sleb = ubifs_start_scan(c, lnum, offs, sbuf);
  588. if (IS_ERR(sleb))
  589. return sleb;
  590. ubifs_assert(len >= 8);
  591. while (len >= 8) {
  592. dbg_scan("look at LEB %d:%d (%d bytes left)",
  593. lnum, offs, len);
  594. cond_resched();
  595. /*
  596. * Scan quietly until there is an error from which we cannot
  597. * recover
  598. */
  599. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
  600. if (ret == SCANNED_A_NODE) {
  601. /* A valid node, and not a padding node */
  602. struct ubifs_ch *ch = buf;
  603. int node_len;
  604. err = ubifs_add_snod(c, sleb, buf, offs);
  605. if (err)
  606. goto error;
  607. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  608. offs += node_len;
  609. buf += node_len;
  610. len -= node_len;
  611. } else if (ret > 0) {
  612. /* Padding bytes or a valid padding node */
  613. offs += ret;
  614. buf += ret;
  615. len -= ret;
  616. } else if (ret == SCANNED_EMPTY_SPACE ||
  617. ret == SCANNED_GARBAGE ||
  618. ret == SCANNED_A_BAD_PAD_NODE ||
  619. ret == SCANNED_A_CORRUPT_NODE) {
  620. dbg_rcvry("found corruption - %d", ret);
  621. break;
  622. } else {
  623. dbg_err("unexpected return value %d", ret);
  624. err = -EINVAL;
  625. goto error;
  626. }
  627. }
  628. if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE) {
  629. if (!is_last_write(c, buf, offs))
  630. goto corrupted_rescan;
  631. } else if (ret == SCANNED_A_CORRUPT_NODE) {
  632. if (!no_more_nodes(c, buf, len, lnum, offs))
  633. goto corrupted_rescan;
  634. } else if (!is_empty(buf, len)) {
  635. if (!is_last_write(c, buf, offs)) {
  636. int corruption = first_non_ff(buf, len);
  637. /*
  638. * See header comment for this file for more
  639. * explanations about the reasons we have this check.
  640. */
  641. ubifs_err("corrupt empty space LEB %d:%d, corruption "
  642. "starts at %d", lnum, offs, corruption);
  643. /* Make sure we dump interesting non-0xFF data */
  644. offs += corruption;
  645. buf += corruption;
  646. goto corrupted;
  647. }
  648. }
  649. min_io_unit = round_down(offs, c->min_io_size);
  650. if (grouped)
  651. /*
  652. * If nodes are grouped, always drop the incomplete group at
  653. * the end.
  654. */
  655. drop_last_group(sleb, &offs);
  656. if (jhead == GCHD) {
  657. /*
  658. * If this LEB belongs to the GC head then while we are in the
  659. * middle of the same min. I/O unit keep dropping nodes. So
  660. * basically, what we want is to make sure that the last min.
  661. * I/O unit where we saw the corruption is dropped completely
  662. * with all the uncorrupted nodes which may possibly sit there.
  663. *
  664. * In other words, let's name the min. I/O unit where the
  665. * corruption starts B, and the previous min. I/O unit A. The
  666. * below code tries to deal with a situation when half of B
  667. * contains valid nodes or the end of a valid node, and the
  668. * second half of B contains corrupted data or garbage. This
  669. * means that UBIFS had been writing to B just before the power
  670. * cut happened. I do not know how realistic is this scenario
  671. * that half of the min. I/O unit had been written successfully
  672. * and the other half not, but this is possible in our 'failure
  673. * mode emulation' infrastructure at least.
  674. *
  675. * So what is the problem, why we need to drop those nodes? Why
  676. * can't we just clean-up the second half of B by putting a
  677. * padding node there? We can, and this works fine with one
  678. * exception which was reproduced with power cut emulation
  679. * testing and happens extremely rarely.
  680. *
  681. * Imagine the file-system is full, we run GC which starts
  682. * moving valid nodes from LEB X to LEB Y (obviously, LEB Y is
  683. * the current GC head LEB). The @c->gc_lnum is -1, which means
  684. * that GC will retain LEB X and will try to continue. Imagine
  685. * that LEB X is currently the dirtiest LEB, and the amount of
  686. * used space in LEB Y is exactly the same as amount of free
  687. * space in LEB X.
  688. *
  689. * And a power cut happens when nodes are moved from LEB X to
  690. * LEB Y. We are here trying to recover LEB Y which is the GC
  691. * head LEB. We find the min. I/O unit B as described above.
  692. * Then we clean-up LEB Y by padding min. I/O unit. And later
  693. * 'ubifs_rcvry_gc_commit()' function fails, because it cannot
  694. * find a dirty LEB which could be GC'd into LEB Y! Even LEB X
  695. * does not match because the amount of valid nodes there does
  696. * not fit the free space in LEB Y any more! And this is
  697. * because of the padding node which we added to LEB Y. The
  698. * user-visible effect of this which I once observed and
  699. * analysed is that we cannot mount the file-system with
  700. * -ENOSPC error.
  701. *
  702. * So obviously, to make sure that situation does not happen we
  703. * should free min. I/O unit B in LEB Y completely and the last
  704. * used min. I/O unit in LEB Y should be A. This is basically
  705. * what the below code tries to do.
  706. */
  707. while (offs > min_io_unit)
  708. drop_last_node(sleb, &offs);
  709. }
  710. buf = sbuf + offs;
  711. len = c->leb_size - offs;
  712. clean_buf(c, &buf, lnum, &offs, &len);
  713. ubifs_end_scan(c, sleb, lnum, offs);
  714. err = fix_unclean_leb(c, sleb, start);
  715. if (err)
  716. goto error;
  717. return sleb;
  718. corrupted_rescan:
  719. /* Re-scan the corrupted data with verbose messages */
  720. dbg_err("corruptio %d", ret);
  721. ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
  722. corrupted:
  723. ubifs_scanned_corruption(c, lnum, offs, buf);
  724. err = -EUCLEAN;
  725. error:
  726. ubifs_err("LEB %d scanning failed", lnum);
  727. ubifs_scan_destroy(sleb);
  728. return ERR_PTR(err);
  729. }
  730. /**
  731. * get_cs_sqnum - get commit start sequence number.
  732. * @c: UBIFS file-system description object
  733. * @lnum: LEB number of commit start node
  734. * @offs: offset of commit start node
  735. * @cs_sqnum: commit start sequence number is returned here
  736. *
  737. * This function returns %0 on success and a negative error code on failure.
  738. */
  739. static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
  740. unsigned long long *cs_sqnum)
  741. {
  742. struct ubifs_cs_node *cs_node = NULL;
  743. int err, ret;
  744. dbg_rcvry("at %d:%d", lnum, offs);
  745. cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
  746. if (!cs_node)
  747. return -ENOMEM;
  748. if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
  749. goto out_err;
  750. err = ubi_read(c->ubi, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ);
  751. if (err && err != -EBADMSG)
  752. goto out_free;
  753. ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
  754. if (ret != SCANNED_A_NODE) {
  755. dbg_err("Not a valid node");
  756. goto out_err;
  757. }
  758. if (cs_node->ch.node_type != UBIFS_CS_NODE) {
  759. dbg_err("Node a CS node, type is %d", cs_node->ch.node_type);
  760. goto out_err;
  761. }
  762. if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
  763. dbg_err("CS node cmt_no %llu != current cmt_no %llu",
  764. (unsigned long long)le64_to_cpu(cs_node->cmt_no),
  765. c->cmt_no);
  766. goto out_err;
  767. }
  768. *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
  769. dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
  770. kfree(cs_node);
  771. return 0;
  772. out_err:
  773. err = -EINVAL;
  774. out_free:
  775. ubifs_err("failed to get CS sqnum");
  776. kfree(cs_node);
  777. return err;
  778. }
  779. /**
  780. * ubifs_recover_log_leb - scan and recover a log LEB.
  781. * @c: UBIFS file-system description object
  782. * @lnum: LEB number
  783. * @offs: offset
  784. * @sbuf: LEB-sized buffer to use
  785. *
  786. * This function does a scan of a LEB, but caters for errors that might have
  787. * been caused by unclean reboots from which we are attempting to recover
  788. * (assume that only the last log LEB can be corrupted by an unclean reboot).
  789. *
  790. * This function returns %0 on success and a negative error code on failure.
  791. */
  792. struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
  793. int offs, void *sbuf)
  794. {
  795. struct ubifs_scan_leb *sleb;
  796. int next_lnum;
  797. dbg_rcvry("LEB %d", lnum);
  798. next_lnum = lnum + 1;
  799. if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
  800. next_lnum = UBIFS_LOG_LNUM;
  801. if (next_lnum != c->ltail_lnum) {
  802. /*
  803. * We can only recover at the end of the log, so check that the
  804. * next log LEB is empty or out of date.
  805. */
  806. sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0);
  807. if (IS_ERR(sleb))
  808. return sleb;
  809. if (sleb->nodes_cnt) {
  810. struct ubifs_scan_node *snod;
  811. unsigned long long cs_sqnum = c->cs_sqnum;
  812. snod = list_entry(sleb->nodes.next,
  813. struct ubifs_scan_node, list);
  814. if (cs_sqnum == 0) {
  815. int err;
  816. err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
  817. if (err) {
  818. ubifs_scan_destroy(sleb);
  819. return ERR_PTR(err);
  820. }
  821. }
  822. if (snod->sqnum > cs_sqnum) {
  823. ubifs_err("unrecoverable log corruption "
  824. "in LEB %d", lnum);
  825. ubifs_scan_destroy(sleb);
  826. return ERR_PTR(-EUCLEAN);
  827. }
  828. }
  829. ubifs_scan_destroy(sleb);
  830. }
  831. return ubifs_recover_leb(c, lnum, offs, sbuf, -1);
  832. }
  833. /**
  834. * recover_head - recover a head.
  835. * @c: UBIFS file-system description object
  836. * @lnum: LEB number of head to recover
  837. * @offs: offset of head to recover
  838. * @sbuf: LEB-sized buffer to use
  839. *
  840. * This function ensures that there is no data on the flash at a head location.
  841. *
  842. * This function returns %0 on success and a negative error code on failure.
  843. */
  844. static int recover_head(const struct ubifs_info *c, int lnum, int offs,
  845. void *sbuf)
  846. {
  847. int len = c->max_write_size, err;
  848. if (offs + len > c->leb_size)
  849. len = c->leb_size - offs;
  850. if (!len)
  851. return 0;
  852. /* Read at the head location and check it is empty flash */
  853. err = ubi_read(c->ubi, lnum, sbuf, offs, len);
  854. if (err || !is_empty(sbuf, len)) {
  855. dbg_rcvry("cleaning head at %d:%d", lnum, offs);
  856. if (offs == 0)
  857. return ubifs_leb_unmap(c, lnum);
  858. err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
  859. if (err)
  860. return err;
  861. return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
  862. }
  863. return 0;
  864. }
  865. /**
  866. * ubifs_recover_inl_heads - recover index and LPT heads.
  867. * @c: UBIFS file-system description object
  868. * @sbuf: LEB-sized buffer to use
  869. *
  870. * This function ensures that there is no data on the flash at the index and
  871. * LPT head locations.
  872. *
  873. * This deals with the recovery of a half-completed journal commit. UBIFS is
  874. * careful never to overwrite the last version of the index or the LPT. Because
  875. * the index and LPT are wandering trees, data from a half-completed commit will
  876. * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
  877. * assumed to be empty and will be unmapped anyway before use, or in the index
  878. * and LPT heads.
  879. *
  880. * This function returns %0 on success and a negative error code on failure.
  881. */
  882. int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
  883. {
  884. int err;
  885. ubifs_assert(!c->ro_mount || c->remounting_rw);
  886. dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
  887. err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
  888. if (err)
  889. return err;
  890. dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
  891. err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
  892. if (err)
  893. return err;
  894. return 0;
  895. }
  896. /**
  897. * clean_an_unclean_leb - read and write a LEB to remove corruption.
  898. * @c: UBIFS file-system description object
  899. * @ucleb: unclean LEB information
  900. * @sbuf: LEB-sized buffer to use
  901. *
  902. * This function reads a LEB up to a point pre-determined by the mount recovery,
  903. * checks the nodes, and writes the result back to the flash, thereby cleaning
  904. * off any following corruption, or non-fatal ECC errors.
  905. *
  906. * This function returns %0 on success and a negative error code on failure.
  907. */
  908. static int clean_an_unclean_leb(const struct ubifs_info *c,
  909. struct ubifs_unclean_leb *ucleb, void *sbuf)
  910. {
  911. int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
  912. void *buf = sbuf;
  913. dbg_rcvry("LEB %d len %d", lnum, len);
  914. if (len == 0) {
  915. /* Nothing to read, just unmap it */
  916. err = ubifs_leb_unmap(c, lnum);
  917. if (err)
  918. return err;
  919. return 0;
  920. }
  921. err = ubi_read(c->ubi, lnum, buf, offs, len);
  922. if (err && err != -EBADMSG)
  923. return err;
  924. while (len >= 8) {
  925. int ret;
  926. cond_resched();
  927. /* Scan quietly until there is an error */
  928. ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
  929. if (ret == SCANNED_A_NODE) {
  930. /* A valid node, and not a padding node */
  931. struct ubifs_ch *ch = buf;
  932. int node_len;
  933. node_len = ALIGN(le32_to_cpu(ch->len), 8);
  934. offs += node_len;
  935. buf += node_len;
  936. len -= node_len;
  937. continue;
  938. }
  939. if (ret > 0) {
  940. /* Padding bytes or a valid padding node */
  941. offs += ret;
  942. buf += ret;
  943. len -= ret;
  944. continue;
  945. }
  946. if (ret == SCANNED_EMPTY_SPACE) {
  947. ubifs_err("unexpected empty space at %d:%d",
  948. lnum, offs);
  949. return -EUCLEAN;
  950. }
  951. if (quiet) {
  952. /* Redo the last scan but noisily */
  953. quiet = 0;
  954. continue;
  955. }
  956. ubifs_scanned_corruption(c, lnum, offs, buf);
  957. return -EUCLEAN;
  958. }
  959. /* Pad to min_io_size */
  960. len = ALIGN(ucleb->endpt, c->min_io_size);
  961. if (len > ucleb->endpt) {
  962. int pad_len = len - ALIGN(ucleb->endpt, 8);
  963. if (pad_len > 0) {
  964. buf = c->sbuf + len - pad_len;
  965. ubifs_pad(c, buf, pad_len);
  966. }
  967. }
  968. /* Write back the LEB atomically */
  969. err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
  970. if (err)
  971. return err;
  972. dbg_rcvry("cleaned LEB %d", lnum);
  973. return 0;
  974. }
  975. /**
  976. * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
  977. * @c: UBIFS file-system description object
  978. * @sbuf: LEB-sized buffer to use
  979. *
  980. * This function cleans a LEB identified during recovery that needs to be
  981. * written but was not because UBIFS was mounted read-only. This happens when
  982. * remounting to read-write mode.
  983. *
  984. * This function returns %0 on success and a negative error code on failure.
  985. */
  986. int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
  987. {
  988. dbg_rcvry("recovery");
  989. while (!list_empty(&c->unclean_leb_list)) {
  990. struct ubifs_unclean_leb *ucleb;
  991. int err;
  992. ucleb = list_entry(c->unclean_leb_list.next,
  993. struct ubifs_unclean_leb, list);
  994. err = clean_an_unclean_leb(c, ucleb, sbuf);
  995. if (err)
  996. return err;
  997. list_del(&ucleb->list);
  998. kfree(ucleb);
  999. }
  1000. return 0;
  1001. }
  1002. /**
  1003. * grab_empty_leb - grab an empty LEB to use as GC LEB and run commit.
  1004. * @c: UBIFS file-system description object
  1005. *
  1006. * This is a helper function for 'ubifs_rcvry_gc_commit()' which grabs an empty
  1007. * LEB to be used as GC LEB (@c->gc_lnum), and then runs the commit. Returns
  1008. * zero in case of success and a negative error code in case of failure.
  1009. */
  1010. static int grab_empty_leb(struct ubifs_info *c)
  1011. {
  1012. int lnum, err;
  1013. /*
  1014. * Note, it is very important to first search for an empty LEB and then
  1015. * run the commit, not vice-versa. The reason is that there might be
  1016. * only one empty LEB at the moment, the one which has been the
  1017. * @c->gc_lnum just before the power cut happened. During the regular
  1018. * UBIFS operation (not now) @c->gc_lnum is marked as "taken", so no
  1019. * one but GC can grab it. But at this moment this single empty LEB is
  1020. * not marked as taken, so if we run commit - what happens? Right, the
  1021. * commit will grab it and write the index there. Remember that the
  1022. * index always expands as long as there is free space, and it only
  1023. * starts consolidating when we run out of space.
  1024. *
  1025. * IOW, if we run commit now, we might not be able to find a free LEB
  1026. * after this.
  1027. */
  1028. lnum = ubifs_find_free_leb_for_idx(c);
  1029. if (lnum < 0) {
  1030. dbg_err("could not find an empty LEB");
  1031. dbg_dump_lprops(c);
  1032. dbg_dump_budg(c, &c->bi);
  1033. return lnum;
  1034. }
  1035. /* Reset the index flag */
  1036. err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
  1037. LPROPS_INDEX, 0);
  1038. if (err)
  1039. return err;
  1040. c->gc_lnum = lnum;
  1041. dbg_rcvry("found empty LEB %d, run commit", lnum);
  1042. return ubifs_run_commit(c);
  1043. }
  1044. /**
  1045. * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
  1046. * @c: UBIFS file-system description object
  1047. *
  1048. * Out-of-place garbage collection requires always one empty LEB with which to
  1049. * start garbage collection. The LEB number is recorded in c->gc_lnum and is
  1050. * written to the master node on unmounting. In the case of an unclean unmount
  1051. * the value of gc_lnum recorded in the master node is out of date and cannot
  1052. * be used. Instead, recovery must allocate an empty LEB for this purpose.
  1053. * However, there may not be enough empty space, in which case it must be
  1054. * possible to GC the dirtiest LEB into the GC head LEB.
  1055. *
  1056. * This function also runs the commit which causes the TNC updates from
  1057. * size-recovery and orphans to be written to the flash. That is important to
  1058. * ensure correct replay order for subsequent mounts.
  1059. *
  1060. * This function returns %0 on success and a negative error code on failure.
  1061. */
  1062. int ubifs_rcvry_gc_commit(struct ubifs_info *c)
  1063. {
  1064. struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
  1065. struct ubifs_lprops lp;
  1066. int err;
  1067. dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs);
  1068. c->gc_lnum = -1;
  1069. if (wbuf->lnum == -1 || wbuf->offs == c->leb_size)
  1070. return grab_empty_leb(c);
  1071. err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
  1072. if (err) {
  1073. if (err != -ENOSPC)
  1074. return err;
  1075. dbg_rcvry("could not find a dirty LEB");
  1076. return grab_empty_leb(c);
  1077. }
  1078. ubifs_assert(!(lp.flags & LPROPS_INDEX));
  1079. ubifs_assert(lp.free + lp.dirty >= wbuf->offs);
  1080. /*
  1081. * We run the commit before garbage collection otherwise subsequent
  1082. * mounts will see the GC and orphan deletion in a different order.
  1083. */
  1084. dbg_rcvry("committing");
  1085. err = ubifs_run_commit(c);
  1086. if (err)
  1087. return err;
  1088. dbg_rcvry("GC'ing LEB %d", lp.lnum);
  1089. mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
  1090. err = ubifs_garbage_collect_leb(c, &lp);
  1091. if (err >= 0) {
  1092. int err2 = ubifs_wbuf_sync_nolock(wbuf);
  1093. if (err2)
  1094. err = err2;
  1095. }
  1096. mutex_unlock(&wbuf->io_mutex);
  1097. if (err < 0) {
  1098. dbg_err("GC failed, error %d", err);
  1099. if (err == -EAGAIN)
  1100. err = -EINVAL;
  1101. return err;
  1102. }
  1103. ubifs_assert(err == LEB_RETAINED);
  1104. if (err != LEB_RETAINED)
  1105. return -EINVAL;
  1106. err = ubifs_leb_unmap(c, c->gc_lnum);
  1107. if (err)
  1108. return err;
  1109. dbg_rcvry("allocated LEB %d for GC", lp.lnum);
  1110. return 0;
  1111. }
  1112. /**
  1113. * struct size_entry - inode size information for recovery.
  1114. * @rb: link in the RB-tree of sizes
  1115. * @inum: inode number
  1116. * @i_size: size on inode
  1117. * @d_size: maximum size based on data nodes
  1118. * @exists: indicates whether the inode exists
  1119. * @inode: inode if pinned in memory awaiting rw mode to fix it
  1120. */
  1121. struct size_entry {
  1122. struct rb_node rb;
  1123. ino_t inum;
  1124. loff_t i_size;
  1125. loff_t d_size;
  1126. int exists;
  1127. struct inode *inode;
  1128. };
  1129. /**
  1130. * add_ino - add an entry to the size tree.
  1131. * @c: UBIFS file-system description object
  1132. * @inum: inode number
  1133. * @i_size: size on inode
  1134. * @d_size: maximum size based on data nodes
  1135. * @exists: indicates whether the inode exists
  1136. */
  1137. static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
  1138. loff_t d_size, int exists)
  1139. {
  1140. struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
  1141. struct size_entry *e;
  1142. while (*p) {
  1143. parent = *p;
  1144. e = rb_entry(parent, struct size_entry, rb);
  1145. if (inum < e->inum)
  1146. p = &(*p)->rb_left;
  1147. else
  1148. p = &(*p)->rb_right;
  1149. }
  1150. e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
  1151. if (!e)
  1152. return -ENOMEM;
  1153. e->inum = inum;
  1154. e->i_size = i_size;
  1155. e->d_size = d_size;
  1156. e->exists = exists;
  1157. rb_link_node(&e->rb, parent, p);
  1158. rb_insert_color(&e->rb, &c->size_tree);
  1159. return 0;
  1160. }
  1161. /**
  1162. * find_ino - find an entry on the size tree.
  1163. * @c: UBIFS file-system description object
  1164. * @inum: inode number
  1165. */
  1166. static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
  1167. {
  1168. struct rb_node *p = c->size_tree.rb_node;
  1169. struct size_entry *e;
  1170. while (p) {
  1171. e = rb_entry(p, struct size_entry, rb);
  1172. if (inum < e->inum)
  1173. p = p->rb_left;
  1174. else if (inum > e->inum)
  1175. p = p->rb_right;
  1176. else
  1177. return e;
  1178. }
  1179. return NULL;
  1180. }
  1181. /**
  1182. * remove_ino - remove an entry from the size tree.
  1183. * @c: UBIFS file-system description object
  1184. * @inum: inode number
  1185. */
  1186. static void remove_ino(struct ubifs_info *c, ino_t inum)
  1187. {
  1188. struct size_entry *e = find_ino(c, inum);
  1189. if (!e)
  1190. return;
  1191. rb_erase(&e->rb, &c->size_tree);
  1192. kfree(e);
  1193. }
  1194. /**
  1195. * ubifs_destroy_size_tree - free resources related to the size tree.
  1196. * @c: UBIFS file-system description object
  1197. */
  1198. void ubifs_destroy_size_tree(struct ubifs_info *c)
  1199. {
  1200. struct rb_node *this = c->size_tree.rb_node;
  1201. struct size_entry *e;
  1202. while (this) {
  1203. if (this->rb_left) {
  1204. this = this->rb_left;
  1205. continue;
  1206. } else if (this->rb_right) {
  1207. this = this->rb_right;
  1208. continue;
  1209. }
  1210. e = rb_entry(this, struct size_entry, rb);
  1211. if (e->inode)
  1212. iput(e->inode);
  1213. this = rb_parent(this);
  1214. if (this) {
  1215. if (this->rb_left == &e->rb)
  1216. this->rb_left = NULL;
  1217. else
  1218. this->rb_right = NULL;
  1219. }
  1220. kfree(e);
  1221. }
  1222. c->size_tree = RB_ROOT;
  1223. }
  1224. /**
  1225. * ubifs_recover_size_accum - accumulate inode sizes for recovery.
  1226. * @c: UBIFS file-system description object
  1227. * @key: node key
  1228. * @deletion: node is for a deletion
  1229. * @new_size: inode size
  1230. *
  1231. * This function has two purposes:
  1232. * 1) to ensure there are no data nodes that fall outside the inode size
  1233. * 2) to ensure there are no data nodes for inodes that do not exist
  1234. * To accomplish those purposes, a rb-tree is constructed containing an entry
  1235. * for each inode number in the journal that has not been deleted, and recording
  1236. * the size from the inode node, the maximum size of any data node (also altered
  1237. * by truncations) and a flag indicating a inode number for which no inode node
  1238. * was present in the journal.
  1239. *
  1240. * Note that there is still the possibility that there are data nodes that have
  1241. * been committed that are beyond the inode size, however the only way to find
  1242. * them would be to scan the entire index. Alternatively, some provision could
  1243. * be made to record the size of inodes at the start of commit, which would seem
  1244. * very cumbersome for a scenario that is quite unlikely and the only negative
  1245. * consequence of which is wasted space.
  1246. *
  1247. * This functions returns %0 on success and a negative error code on failure.
  1248. */
  1249. int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
  1250. int deletion, loff_t new_size)
  1251. {
  1252. ino_t inum = key_inum(c, key);
  1253. struct size_entry *e;
  1254. int err;
  1255. switch (key_type(c, key)) {
  1256. case UBIFS_INO_KEY:
  1257. if (deletion)
  1258. remove_ino(c, inum);
  1259. else {
  1260. e = find_ino(c, inum);
  1261. if (e) {
  1262. e->i_size = new_size;
  1263. e->exists = 1;
  1264. } else {
  1265. err = add_ino(c, inum, new_size, 0, 1);
  1266. if (err)
  1267. return err;
  1268. }
  1269. }
  1270. break;
  1271. case UBIFS_DATA_KEY:
  1272. e = find_ino(c, inum);
  1273. if (e) {
  1274. if (new_size > e->d_size)
  1275. e->d_size = new_size;
  1276. } else {
  1277. err = add_ino(c, inum, 0, new_size, 0);
  1278. if (err)
  1279. return err;
  1280. }
  1281. break;
  1282. case UBIFS_TRUN_KEY:
  1283. e = find_ino(c, inum);
  1284. if (e)
  1285. e->d_size = new_size;
  1286. break;
  1287. }
  1288. return 0;
  1289. }
  1290. /**
  1291. * fix_size_in_place - fix inode size in place on flash.
  1292. * @c: UBIFS file-system description object
  1293. * @e: inode size information for recovery
  1294. */
  1295. static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
  1296. {
  1297. struct ubifs_ino_node *ino = c->sbuf;
  1298. unsigned char *p;
  1299. union ubifs_key key;
  1300. int err, lnum, offs, len;
  1301. loff_t i_size;
  1302. uint32_t crc;
  1303. /* Locate the inode node LEB number and offset */
  1304. ino_key_init(c, &key, e->inum);
  1305. err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
  1306. if (err)
  1307. goto out;
  1308. /*
  1309. * If the size recorded on the inode node is greater than the size that
  1310. * was calculated from nodes in the journal then don't change the inode.
  1311. */
  1312. i_size = le64_to_cpu(ino->size);
  1313. if (i_size >= e->d_size)
  1314. return 0;
  1315. /* Read the LEB */
  1316. err = ubi_read(c->ubi, lnum, c->sbuf, 0, c->leb_size);
  1317. if (err)
  1318. goto out;
  1319. /* Change the size field and recalculate the CRC */
  1320. ino = c->sbuf + offs;
  1321. ino->size = cpu_to_le64(e->d_size);
  1322. len = le32_to_cpu(ino->ch.len);
  1323. crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
  1324. ino->ch.crc = cpu_to_le32(crc);
  1325. /* Work out where data in the LEB ends and free space begins */
  1326. p = c->sbuf;
  1327. len = c->leb_size - 1;
  1328. while (p[len] == 0xff)
  1329. len -= 1;
  1330. len = ALIGN(len + 1, c->min_io_size);
  1331. /* Atomically write the fixed LEB back again */
  1332. err = ubi_leb_change(c->ubi, lnum, c->sbuf, len, UBI_UNKNOWN);
  1333. if (err)
  1334. goto out;
  1335. dbg_rcvry("inode %lu at %d:%d size %lld -> %lld",
  1336. (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
  1337. return 0;
  1338. out:
  1339. ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d",
  1340. (unsigned long)e->inum, e->i_size, e->d_size, err);
  1341. return err;
  1342. }
  1343. /**
  1344. * ubifs_recover_size - recover inode size.
  1345. * @c: UBIFS file-system description object
  1346. *
  1347. * This function attempts to fix inode size discrepancies identified by the
  1348. * 'ubifs_recover_size_accum()' function.
  1349. *
  1350. * This functions returns %0 on success and a negative error code on failure.
  1351. */
  1352. int ubifs_recover_size(struct ubifs_info *c)
  1353. {
  1354. struct rb_node *this = rb_first(&c->size_tree);
  1355. while (this) {
  1356. struct size_entry *e;
  1357. int err;
  1358. e = rb_entry(this, struct size_entry, rb);
  1359. if (!e->exists) {
  1360. union ubifs_key key;
  1361. ino_key_init(c, &key, e->inum);
  1362. err = ubifs_tnc_lookup(c, &key, c->sbuf);
  1363. if (err && err != -ENOENT)
  1364. return err;
  1365. if (err == -ENOENT) {
  1366. /* Remove data nodes that have no inode */
  1367. dbg_rcvry("removing ino %lu",
  1368. (unsigned long)e->inum);
  1369. err = ubifs_tnc_remove_ino(c, e->inum);
  1370. if (err)
  1371. return err;
  1372. } else {
  1373. struct ubifs_ino_node *ino = c->sbuf;
  1374. e->exists = 1;
  1375. e->i_size = le64_to_cpu(ino->size);
  1376. }
  1377. }
  1378. if (e->exists && e->i_size < e->d_size) {
  1379. if (c->ro_mount) {
  1380. /* Fix the inode size and pin it in memory */
  1381. struct inode *inode;
  1382. struct ubifs_inode *ui;
  1383. ubifs_assert(!e->inode);
  1384. inode = ubifs_iget(c->vfs_sb, e->inum);
  1385. if (IS_ERR(inode))
  1386. return PTR_ERR(inode);
  1387. ui = ubifs_inode(inode);
  1388. if (inode->i_size < e->d_size) {
  1389. dbg_rcvry("ino %lu size %lld -> %lld",
  1390. (unsigned long)e->inum,
  1391. inode->i_size, e->d_size);
  1392. inode->i_size = e->d_size;
  1393. ui->ui_size = e->d_size;
  1394. ui->synced_i_size = e->d_size;
  1395. e->inode = inode;
  1396. this = rb_next(this);
  1397. continue;
  1398. }
  1399. iput(inode);
  1400. } else {
  1401. /* Fix the size in place */
  1402. err = fix_size_in_place(c, e);
  1403. if (err)
  1404. return err;
  1405. if (e->inode)
  1406. iput(e->inode);
  1407. }
  1408. }
  1409. this = rb_next(this);
  1410. rb_erase(&e->rb, &c->size_tree);
  1411. kfree(e);
  1412. }
  1413. return 0;
  1414. }