recovery.c 38 KB

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