recovery.c 37 KB

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