log.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * This file is part of UBIFS.
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. *
  8. * Authors: Artem Bityutskiy (Битюцкий Артём)
  9. * Adrian Hunter
  10. */
  11. /*
  12. * This file is a part of UBIFS journal implementation and contains various
  13. * functions which manipulate the log. The log is a fixed area on the flash
  14. * which does not contain any data but refers to buds. The log is a part of the
  15. * journal.
  16. */
  17. #ifdef __UBOOT__
  18. #include <linux/err.h>
  19. #endif
  20. #include "ubifs.h"
  21. static int dbg_check_bud_bytes(struct ubifs_info *c);
  22. /**
  23. * ubifs_search_bud - search bud LEB.
  24. * @c: UBIFS file-system description object
  25. * @lnum: logical eraseblock number to search
  26. *
  27. * This function searches bud LEB @lnum. Returns bud description object in case
  28. * of success and %NULL if there is no bud with this LEB number.
  29. */
  30. struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
  31. {
  32. struct rb_node *p;
  33. struct ubifs_bud *bud;
  34. spin_lock(&c->buds_lock);
  35. p = c->buds.rb_node;
  36. while (p) {
  37. bud = rb_entry(p, struct ubifs_bud, rb);
  38. if (lnum < bud->lnum)
  39. p = p->rb_left;
  40. else if (lnum > bud->lnum)
  41. p = p->rb_right;
  42. else {
  43. spin_unlock(&c->buds_lock);
  44. return bud;
  45. }
  46. }
  47. spin_unlock(&c->buds_lock);
  48. return NULL;
  49. }
  50. /**
  51. * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
  52. * @c: UBIFS file-system description object
  53. * @lnum: logical eraseblock number to search
  54. *
  55. * This functions returns the wbuf for @lnum or %NULL if there is not one.
  56. */
  57. struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
  58. {
  59. struct rb_node *p;
  60. struct ubifs_bud *bud;
  61. int jhead;
  62. if (!c->jheads)
  63. return NULL;
  64. spin_lock(&c->buds_lock);
  65. p = c->buds.rb_node;
  66. while (p) {
  67. bud = rb_entry(p, struct ubifs_bud, rb);
  68. if (lnum < bud->lnum)
  69. p = p->rb_left;
  70. else if (lnum > bud->lnum)
  71. p = p->rb_right;
  72. else {
  73. jhead = bud->jhead;
  74. spin_unlock(&c->buds_lock);
  75. return &c->jheads[jhead].wbuf;
  76. }
  77. }
  78. spin_unlock(&c->buds_lock);
  79. return NULL;
  80. }
  81. /**
  82. * empty_log_bytes - calculate amount of empty space in the log.
  83. * @c: UBIFS file-system description object
  84. */
  85. static inline long long empty_log_bytes(const struct ubifs_info *c)
  86. {
  87. long long h, t;
  88. h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
  89. t = (long long)c->ltail_lnum * c->leb_size;
  90. if (h >= t)
  91. return c->log_bytes - h + t;
  92. else
  93. return t - h;
  94. }
  95. /**
  96. * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
  97. * @c: UBIFS file-system description object
  98. * @bud: the bud to add
  99. */
  100. void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
  101. {
  102. struct rb_node **p, *parent = NULL;
  103. struct ubifs_bud *b;
  104. struct ubifs_jhead *jhead;
  105. spin_lock(&c->buds_lock);
  106. p = &c->buds.rb_node;
  107. while (*p) {
  108. parent = *p;
  109. b = rb_entry(parent, struct ubifs_bud, rb);
  110. ubifs_assert(bud->lnum != b->lnum);
  111. if (bud->lnum < b->lnum)
  112. p = &(*p)->rb_left;
  113. else
  114. p = &(*p)->rb_right;
  115. }
  116. rb_link_node(&bud->rb, parent, p);
  117. rb_insert_color(&bud->rb, &c->buds);
  118. if (c->jheads) {
  119. jhead = &c->jheads[bud->jhead];
  120. list_add_tail(&bud->list, &jhead->buds_list);
  121. } else
  122. ubifs_assert(c->replaying && c->ro_mount);
  123. /*
  124. * Note, although this is a new bud, we anyway account this space now,
  125. * before any data has been written to it, because this is about to
  126. * guarantee fixed mount time, and this bud will anyway be read and
  127. * scanned.
  128. */
  129. c->bud_bytes += c->leb_size - bud->start;
  130. dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum,
  131. bud->start, dbg_jhead(bud->jhead), c->bud_bytes);
  132. spin_unlock(&c->buds_lock);
  133. }
  134. /**
  135. * ubifs_add_bud_to_log - add a new bud to the log.
  136. * @c: UBIFS file-system description object
  137. * @jhead: journal head the bud belongs to
  138. * @lnum: LEB number of the bud
  139. * @offs: starting offset of the bud
  140. *
  141. * This function writes reference node for the new bud LEB @lnum it to the log,
  142. * and adds it to the buds tress. It also makes sure that log size does not
  143. * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
  144. * %-EAGAIN if commit is required, and a negative error codes in case of
  145. * failure.
  146. */
  147. int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
  148. {
  149. int err;
  150. struct ubifs_bud *bud;
  151. struct ubifs_ref_node *ref;
  152. bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
  153. if (!bud)
  154. return -ENOMEM;
  155. ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
  156. if (!ref) {
  157. kfree(bud);
  158. return -ENOMEM;
  159. }
  160. mutex_lock(&c->log_mutex);
  161. ubifs_assert(!c->ro_media && !c->ro_mount);
  162. if (c->ro_error) {
  163. err = -EROFS;
  164. goto out_unlock;
  165. }
  166. /* Make sure we have enough space in the log */
  167. if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
  168. dbg_log("not enough log space - %lld, required %d",
  169. empty_log_bytes(c), c->min_log_bytes);
  170. ubifs_commit_required(c);
  171. err = -EAGAIN;
  172. goto out_unlock;
  173. }
  174. /*
  175. * Make sure the amount of space in buds will not exceed the
  176. * 'c->max_bud_bytes' limit, because we want to guarantee mount time
  177. * limits.
  178. *
  179. * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
  180. * because we are holding @c->log_mutex. All @c->bud_bytes take place
  181. * when both @c->log_mutex and @c->bud_bytes are locked.
  182. */
  183. if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
  184. dbg_log("bud bytes %lld (%lld max), require commit",
  185. c->bud_bytes, c->max_bud_bytes);
  186. ubifs_commit_required(c);
  187. err = -EAGAIN;
  188. goto out_unlock;
  189. }
  190. /*
  191. * If the journal is full enough - start background commit. Note, it is
  192. * OK to read 'c->cmt_state' without spinlock because integer reads
  193. * are atomic in the kernel.
  194. */
  195. if (c->bud_bytes >= c->bg_bud_bytes &&
  196. c->cmt_state == COMMIT_RESTING) {
  197. dbg_log("bud bytes %lld (%lld max), initiate BG commit",
  198. c->bud_bytes, c->max_bud_bytes);
  199. ubifs_request_bg_commit(c);
  200. }
  201. bud->lnum = lnum;
  202. bud->start = offs;
  203. bud->jhead = jhead;
  204. ref->ch.node_type = UBIFS_REF_NODE;
  205. ref->lnum = cpu_to_le32(bud->lnum);
  206. ref->offs = cpu_to_le32(bud->start);
  207. ref->jhead = cpu_to_le32(jhead);
  208. if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
  209. c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  210. c->lhead_offs = 0;
  211. }
  212. if (c->lhead_offs == 0) {
  213. /* Must ensure next log LEB has been unmapped */
  214. err = ubifs_leb_unmap(c, c->lhead_lnum);
  215. if (err)
  216. goto out_unlock;
  217. }
  218. if (bud->start == 0) {
  219. /*
  220. * Before writing the LEB reference which refers an empty LEB
  221. * to the log, we have to make sure it is mapped, because
  222. * otherwise we'd risk to refer an LEB with garbage in case of
  223. * an unclean reboot, because the target LEB might have been
  224. * unmapped, but not yet physically erased.
  225. */
  226. err = ubifs_leb_map(c, bud->lnum);
  227. if (err)
  228. goto out_unlock;
  229. }
  230. dbg_log("write ref LEB %d:%d",
  231. c->lhead_lnum, c->lhead_offs);
  232. err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
  233. c->lhead_offs);
  234. if (err)
  235. goto out_unlock;
  236. c->lhead_offs += c->ref_node_alsz;
  237. ubifs_add_bud(c, bud);
  238. mutex_unlock(&c->log_mutex);
  239. kfree(ref);
  240. return 0;
  241. out_unlock:
  242. mutex_unlock(&c->log_mutex);
  243. kfree(ref);
  244. kfree(bud);
  245. return err;
  246. }
  247. /**
  248. * remove_buds - remove used buds.
  249. * @c: UBIFS file-system description object
  250. *
  251. * This function removes use buds from the buds tree. It does not remove the
  252. * buds which are pointed to by journal heads.
  253. */
  254. static void remove_buds(struct ubifs_info *c)
  255. {
  256. struct rb_node *p;
  257. ubifs_assert(list_empty(&c->old_buds));
  258. c->cmt_bud_bytes = 0;
  259. spin_lock(&c->buds_lock);
  260. p = rb_first(&c->buds);
  261. while (p) {
  262. struct rb_node *p1 = p;
  263. struct ubifs_bud *bud;
  264. struct ubifs_wbuf *wbuf;
  265. p = rb_next(p);
  266. bud = rb_entry(p1, struct ubifs_bud, rb);
  267. wbuf = &c->jheads[bud->jhead].wbuf;
  268. if (wbuf->lnum == bud->lnum) {
  269. /*
  270. * Do not remove buds which are pointed to by journal
  271. * heads (non-closed buds).
  272. */
  273. c->cmt_bud_bytes += wbuf->offs - bud->start;
  274. dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
  275. bud->lnum, bud->start, dbg_jhead(bud->jhead),
  276. wbuf->offs - bud->start, c->cmt_bud_bytes);
  277. bud->start = wbuf->offs;
  278. } else {
  279. c->cmt_bud_bytes += c->leb_size - bud->start;
  280. dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
  281. bud->lnum, bud->start, dbg_jhead(bud->jhead),
  282. c->leb_size - bud->start, c->cmt_bud_bytes);
  283. rb_erase(p1, &c->buds);
  284. /*
  285. * If the commit does not finish, the recovery will need
  286. * to replay the journal, in which case the old buds
  287. * must be unchanged. Do not release them until post
  288. * commit i.e. do not allow them to be garbage
  289. * collected.
  290. */
  291. list_move(&bud->list, &c->old_buds);
  292. }
  293. }
  294. spin_unlock(&c->buds_lock);
  295. }
  296. /**
  297. * ubifs_log_start_commit - start commit.
  298. * @c: UBIFS file-system description object
  299. * @ltail_lnum: return new log tail LEB number
  300. *
  301. * The commit operation starts with writing "commit start" node to the log and
  302. * reference nodes for all journal heads which will define new journal after
  303. * the commit has been finished. The commit start and reference nodes are
  304. * written in one go to the nearest empty log LEB (hence, when commit is
  305. * finished UBIFS may safely unmap all the previous log LEBs). This function
  306. * returns zero in case of success and a negative error code in case of
  307. * failure.
  308. */
  309. int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
  310. {
  311. void *buf;
  312. struct ubifs_cs_node *cs;
  313. struct ubifs_ref_node *ref;
  314. int err, i, max_len, len;
  315. err = dbg_check_bud_bytes(c);
  316. if (err)
  317. return err;
  318. max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
  319. max_len = ALIGN(max_len, c->min_io_size);
  320. buf = cs = kmalloc(max_len, GFP_NOFS);
  321. if (!buf)
  322. return -ENOMEM;
  323. cs->ch.node_type = UBIFS_CS_NODE;
  324. cs->cmt_no = cpu_to_le64(c->cmt_no);
  325. ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
  326. /*
  327. * Note, we do not lock 'c->log_mutex' because this is the commit start
  328. * phase and we are exclusively using the log. And we do not lock
  329. * write-buffer because nobody can write to the file-system at this
  330. * phase.
  331. */
  332. len = UBIFS_CS_NODE_SZ;
  333. for (i = 0; i < c->jhead_cnt; i++) {
  334. int lnum = c->jheads[i].wbuf.lnum;
  335. int offs = c->jheads[i].wbuf.offs;
  336. if (lnum == -1 || offs == c->leb_size)
  337. continue;
  338. dbg_log("add ref to LEB %d:%d for jhead %s",
  339. lnum, offs, dbg_jhead(i));
  340. ref = buf + len;
  341. ref->ch.node_type = UBIFS_REF_NODE;
  342. ref->lnum = cpu_to_le32(lnum);
  343. ref->offs = cpu_to_le32(offs);
  344. ref->jhead = cpu_to_le32(i);
  345. ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
  346. len += UBIFS_REF_NODE_SZ;
  347. }
  348. ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
  349. /* Switch to the next log LEB */
  350. if (c->lhead_offs) {
  351. c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  352. c->lhead_offs = 0;
  353. }
  354. if (c->lhead_offs == 0) {
  355. /* Must ensure next LEB has been unmapped */
  356. err = ubifs_leb_unmap(c, c->lhead_lnum);
  357. if (err)
  358. goto out;
  359. }
  360. len = ALIGN(len, c->min_io_size);
  361. dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
  362. err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
  363. if (err)
  364. goto out;
  365. *ltail_lnum = c->lhead_lnum;
  366. c->lhead_offs += len;
  367. if (c->lhead_offs == c->leb_size) {
  368. c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  369. c->lhead_offs = 0;
  370. }
  371. remove_buds(c);
  372. /*
  373. * We have started the commit and now users may use the rest of the log
  374. * for new writes.
  375. */
  376. c->min_log_bytes = 0;
  377. out:
  378. kfree(buf);
  379. return err;
  380. }
  381. /**
  382. * ubifs_log_end_commit - end commit.
  383. * @c: UBIFS file-system description object
  384. * @ltail_lnum: new log tail LEB number
  385. *
  386. * This function is called on when the commit operation was finished. It
  387. * moves log tail to new position and unmaps LEBs which contain obsolete data.
  388. * Returns zero in case of success and a negative error code in case of
  389. * failure.
  390. */
  391. int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
  392. {
  393. int err;
  394. /*
  395. * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
  396. * writes during commit. Its only short "commit" start phase when
  397. * writers are blocked.
  398. */
  399. mutex_lock(&c->log_mutex);
  400. dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
  401. c->ltail_lnum, ltail_lnum);
  402. c->ltail_lnum = ltail_lnum;
  403. /*
  404. * The commit is finished and from now on it must be guaranteed that
  405. * there is always enough space for the next commit.
  406. */
  407. c->min_log_bytes = c->leb_size;
  408. spin_lock(&c->buds_lock);
  409. c->bud_bytes -= c->cmt_bud_bytes;
  410. spin_unlock(&c->buds_lock);
  411. err = dbg_check_bud_bytes(c);
  412. mutex_unlock(&c->log_mutex);
  413. return err;
  414. }
  415. /**
  416. * ubifs_log_post_commit - things to do after commit is completed.
  417. * @c: UBIFS file-system description object
  418. * @old_ltail_lnum: old log tail LEB number
  419. *
  420. * Release buds only after commit is completed, because they must be unchanged
  421. * if recovery is needed.
  422. *
  423. * Unmap log LEBs only after commit is completed, because they may be needed for
  424. * recovery.
  425. *
  426. * This function returns %0 on success and a negative error code on failure.
  427. */
  428. int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
  429. {
  430. int lnum, err = 0;
  431. while (!list_empty(&c->old_buds)) {
  432. struct ubifs_bud *bud;
  433. bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
  434. err = ubifs_return_leb(c, bud->lnum);
  435. if (err)
  436. return err;
  437. list_del(&bud->list);
  438. kfree(bud);
  439. }
  440. mutex_lock(&c->log_mutex);
  441. for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
  442. lnum = ubifs_next_log_lnum(c, lnum)) {
  443. dbg_log("unmap log LEB %d", lnum);
  444. err = ubifs_leb_unmap(c, lnum);
  445. if (err)
  446. goto out;
  447. }
  448. out:
  449. mutex_unlock(&c->log_mutex);
  450. return err;
  451. }
  452. /**
  453. * struct done_ref - references that have been done.
  454. * @rb: rb-tree node
  455. * @lnum: LEB number
  456. */
  457. struct done_ref {
  458. struct rb_node rb;
  459. int lnum;
  460. };
  461. /**
  462. * done_already - determine if a reference has been done already.
  463. * @done_tree: rb-tree to store references that have been done
  464. * @lnum: LEB number of reference
  465. *
  466. * This function returns %1 if the reference has been done, %0 if not, otherwise
  467. * a negative error code is returned.
  468. */
  469. static int done_already(struct rb_root *done_tree, int lnum)
  470. {
  471. struct rb_node **p = &done_tree->rb_node, *parent = NULL;
  472. struct done_ref *dr;
  473. while (*p) {
  474. parent = *p;
  475. dr = rb_entry(parent, struct done_ref, rb);
  476. if (lnum < dr->lnum)
  477. p = &(*p)->rb_left;
  478. else if (lnum > dr->lnum)
  479. p = &(*p)->rb_right;
  480. else
  481. return 1;
  482. }
  483. dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
  484. if (!dr)
  485. return -ENOMEM;
  486. dr->lnum = lnum;
  487. rb_link_node(&dr->rb, parent, p);
  488. rb_insert_color(&dr->rb, done_tree);
  489. return 0;
  490. }
  491. /**
  492. * destroy_done_tree - destroy the done tree.
  493. * @done_tree: done tree to destroy
  494. */
  495. static void destroy_done_tree(struct rb_root *done_tree)
  496. {
  497. struct done_ref *dr, *n;
  498. rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb)
  499. kfree(dr);
  500. }
  501. /**
  502. * add_node - add a node to the consolidated log.
  503. * @c: UBIFS file-system description object
  504. * @buf: buffer to which to add
  505. * @lnum: LEB number to which to write is passed and returned here
  506. * @offs: offset to where to write is passed and returned here
  507. * @node: node to add
  508. *
  509. * This function returns %0 on success and a negative error code on failure.
  510. */
  511. static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
  512. void *node)
  513. {
  514. struct ubifs_ch *ch = node;
  515. int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
  516. if (len > remains) {
  517. int sz = ALIGN(*offs, c->min_io_size), err;
  518. ubifs_pad(c, buf + *offs, sz - *offs);
  519. err = ubifs_leb_change(c, *lnum, buf, sz);
  520. if (err)
  521. return err;
  522. *lnum = ubifs_next_log_lnum(c, *lnum);
  523. *offs = 0;
  524. }
  525. memcpy(buf + *offs, node, len);
  526. *offs += ALIGN(len, 8);
  527. return 0;
  528. }
  529. /**
  530. * ubifs_consolidate_log - consolidate the log.
  531. * @c: UBIFS file-system description object
  532. *
  533. * Repeated failed commits could cause the log to be full, but at least 1 LEB is
  534. * needed for commit. This function rewrites the reference nodes in the log
  535. * omitting duplicates, and failed CS nodes, and leaving no gaps.
  536. *
  537. * This function returns %0 on success and a negative error code on failure.
  538. */
  539. int ubifs_consolidate_log(struct ubifs_info *c)
  540. {
  541. struct ubifs_scan_leb *sleb;
  542. struct ubifs_scan_node *snod;
  543. struct rb_root done_tree = RB_ROOT;
  544. int lnum, err, first = 1, write_lnum, offs = 0;
  545. void *buf;
  546. dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
  547. c->lhead_lnum);
  548. buf = vmalloc(c->leb_size);
  549. if (!buf)
  550. return -ENOMEM;
  551. lnum = c->ltail_lnum;
  552. write_lnum = lnum;
  553. while (1) {
  554. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
  555. if (IS_ERR(sleb)) {
  556. err = PTR_ERR(sleb);
  557. goto out_free;
  558. }
  559. list_for_each_entry(snod, &sleb->nodes, list) {
  560. switch (snod->type) {
  561. case UBIFS_REF_NODE: {
  562. struct ubifs_ref_node *ref = snod->node;
  563. int ref_lnum = le32_to_cpu(ref->lnum);
  564. err = done_already(&done_tree, ref_lnum);
  565. if (err < 0)
  566. goto out_scan;
  567. if (err != 1) {
  568. err = add_node(c, buf, &write_lnum,
  569. &offs, snod->node);
  570. if (err)
  571. goto out_scan;
  572. }
  573. break;
  574. }
  575. case UBIFS_CS_NODE:
  576. if (!first)
  577. break;
  578. err = add_node(c, buf, &write_lnum, &offs,
  579. snod->node);
  580. if (err)
  581. goto out_scan;
  582. first = 0;
  583. break;
  584. }
  585. }
  586. ubifs_scan_destroy(sleb);
  587. if (lnum == c->lhead_lnum)
  588. break;
  589. lnum = ubifs_next_log_lnum(c, lnum);
  590. }
  591. if (offs) {
  592. int sz = ALIGN(offs, c->min_io_size);
  593. ubifs_pad(c, buf + offs, sz - offs);
  594. err = ubifs_leb_change(c, write_lnum, buf, sz);
  595. if (err)
  596. goto out_free;
  597. offs = ALIGN(offs, c->min_io_size);
  598. }
  599. destroy_done_tree(&done_tree);
  600. vfree(buf);
  601. if (write_lnum == c->lhead_lnum) {
  602. ubifs_err("log is too full");
  603. return -EINVAL;
  604. }
  605. /* Unmap remaining LEBs */
  606. lnum = write_lnum;
  607. do {
  608. lnum = ubifs_next_log_lnum(c, lnum);
  609. err = ubifs_leb_unmap(c, lnum);
  610. if (err)
  611. return err;
  612. } while (lnum != c->lhead_lnum);
  613. c->lhead_lnum = write_lnum;
  614. c->lhead_offs = offs;
  615. dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
  616. return 0;
  617. out_scan:
  618. ubifs_scan_destroy(sleb);
  619. out_free:
  620. destroy_done_tree(&done_tree);
  621. vfree(buf);
  622. return err;
  623. }
  624. /**
  625. * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
  626. * @c: UBIFS file-system description object
  627. *
  628. * This function makes sure the amount of flash space used by closed buds
  629. * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
  630. * case of failure.
  631. */
  632. static int dbg_check_bud_bytes(struct ubifs_info *c)
  633. {
  634. int i, err = 0;
  635. struct ubifs_bud *bud;
  636. long long bud_bytes = 0;
  637. if (!dbg_is_chk_gen(c))
  638. return 0;
  639. spin_lock(&c->buds_lock);
  640. for (i = 0; i < c->jhead_cnt; i++)
  641. list_for_each_entry(bud, &c->jheads[i].buds_list, list)
  642. bud_bytes += c->leb_size - bud->start;
  643. if (c->bud_bytes != bud_bytes) {
  644. ubifs_err("bad bud_bytes %lld, calculated %lld",
  645. c->bud_bytes, bud_bytes);
  646. err = -EINVAL;
  647. }
  648. spin_unlock(&c->buds_lock);
  649. return err;
  650. }