recovery.c 38 KB

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