log.c 19 KB

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