log.c 19 KB

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