log.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. // SPDX-License-Identifier: GPL-2.0-only
  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. #include "ubifs.h"
  17. static int dbg_check_bud_bytes(struct ubifs_info *c);
  18. /**
  19. * ubifs_search_bud - search bud LEB.
  20. * @c: UBIFS file-system description object
  21. * @lnum: logical eraseblock number to search
  22. *
  23. * This function searches bud LEB @lnum. Returns bud description object in case
  24. * of success and %NULL if there is no bud with this LEB number.
  25. */
  26. struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
  27. {
  28. struct rb_node *p;
  29. struct ubifs_bud *bud;
  30. spin_lock(&c->buds_lock);
  31. p = c->buds.rb_node;
  32. while (p) {
  33. bud = rb_entry(p, struct ubifs_bud, rb);
  34. if (lnum < bud->lnum)
  35. p = p->rb_left;
  36. else if (lnum > bud->lnum)
  37. p = p->rb_right;
  38. else {
  39. spin_unlock(&c->buds_lock);
  40. return bud;
  41. }
  42. }
  43. spin_unlock(&c->buds_lock);
  44. return NULL;
  45. }
  46. /**
  47. * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
  48. * @c: UBIFS file-system description object
  49. * @lnum: logical eraseblock number to search
  50. *
  51. * This functions returns the wbuf for @lnum or %NULL if there is not one.
  52. */
  53. struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
  54. {
  55. struct rb_node *p;
  56. struct ubifs_bud *bud;
  57. int jhead;
  58. if (!c->jheads)
  59. return NULL;
  60. spin_lock(&c->buds_lock);
  61. p = c->buds.rb_node;
  62. while (p) {
  63. bud = rb_entry(p, struct ubifs_bud, rb);
  64. if (lnum < bud->lnum)
  65. p = p->rb_left;
  66. else if (lnum > bud->lnum)
  67. p = p->rb_right;
  68. else {
  69. jhead = bud->jhead;
  70. spin_unlock(&c->buds_lock);
  71. return &c->jheads[jhead].wbuf;
  72. }
  73. }
  74. spin_unlock(&c->buds_lock);
  75. return NULL;
  76. }
  77. /**
  78. * empty_log_bytes - calculate amount of empty space in the log.
  79. * @c: UBIFS file-system description object
  80. */
  81. static inline long long empty_log_bytes(const struct ubifs_info *c)
  82. {
  83. long long h, t;
  84. h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
  85. t = (long long)c->ltail_lnum * c->leb_size;
  86. if (h > t)
  87. return c->log_bytes - h + t;
  88. else if (h != t)
  89. return t - h;
  90. else if (c->lhead_lnum != c->ltail_lnum)
  91. return 0;
  92. else
  93. return c->log_bytes;
  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(c, 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, 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 a reference node for the new bud LEB @lnum to the log,
  142. * and adds it to the buds trees. 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 code 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, !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. bud->log_hash = NULL;
  205. ref->ch.node_type = UBIFS_REF_NODE;
  206. ref->lnum = cpu_to_le32(bud->lnum);
  207. ref->offs = cpu_to_le32(bud->start);
  208. ref->jhead = cpu_to_le32(jhead);
  209. if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
  210. c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  211. ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
  212. c->lhead_offs = 0;
  213. }
  214. if (c->lhead_offs == 0) {
  215. /* Must ensure next log LEB has been unmapped */
  216. err = ubifs_leb_unmap(c, c->lhead_lnum);
  217. if (err)
  218. goto out_unlock;
  219. }
  220. if (bud->start == 0) {
  221. /*
  222. * Before writing the LEB reference which refers an empty LEB
  223. * to the log, we have to make sure it is mapped, because
  224. * otherwise we'd risk to refer an LEB with garbage in case of
  225. * an unclean reboot, because the target LEB might have been
  226. * unmapped, but not yet physically erased.
  227. */
  228. err = ubifs_leb_map(c, bud->lnum);
  229. if (err)
  230. goto out_unlock;
  231. }
  232. dbg_log("write ref LEB %d:%d",
  233. c->lhead_lnum, c->lhead_offs);
  234. err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
  235. c->lhead_offs);
  236. if (err)
  237. goto out_unlock;
  238. err = ubifs_shash_update(c, c->log_hash, ref, UBIFS_REF_NODE_SZ);
  239. if (err)
  240. goto out_unlock;
  241. err = ubifs_shash_copy_state(c, c->log_hash, c->jheads[jhead].log_hash);
  242. if (err)
  243. goto out_unlock;
  244. c->lhead_offs += c->ref_node_alsz;
  245. ubifs_add_bud(c, bud);
  246. mutex_unlock(&c->log_mutex);
  247. kfree(ref);
  248. return 0;
  249. out_unlock:
  250. mutex_unlock(&c->log_mutex);
  251. kfree(ref);
  252. kfree(bud);
  253. return err;
  254. }
  255. /**
  256. * remove_buds - remove used buds.
  257. * @c: UBIFS file-system description object
  258. *
  259. * This function removes use buds from the buds tree. It does not remove the
  260. * buds which are pointed to by journal heads.
  261. */
  262. static void remove_buds(struct ubifs_info *c)
  263. {
  264. struct rb_node *p;
  265. ubifs_assert(c, list_empty(&c->old_buds));
  266. c->cmt_bud_bytes = 0;
  267. spin_lock(&c->buds_lock);
  268. p = rb_first(&c->buds);
  269. while (p) {
  270. struct rb_node *p1 = p;
  271. struct ubifs_bud *bud;
  272. struct ubifs_wbuf *wbuf;
  273. p = rb_next(p);
  274. bud = rb_entry(p1, struct ubifs_bud, rb);
  275. wbuf = &c->jheads[bud->jhead].wbuf;
  276. if (wbuf->lnum == bud->lnum) {
  277. /*
  278. * Do not remove buds which are pointed to by journal
  279. * heads (non-closed buds).
  280. */
  281. c->cmt_bud_bytes += wbuf->offs - bud->start;
  282. dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
  283. bud->lnum, bud->start, dbg_jhead(bud->jhead),
  284. wbuf->offs - bud->start, c->cmt_bud_bytes);
  285. bud->start = wbuf->offs;
  286. } else {
  287. c->cmt_bud_bytes += c->leb_size - bud->start;
  288. dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
  289. bud->lnum, bud->start, dbg_jhead(bud->jhead),
  290. c->leb_size - bud->start, c->cmt_bud_bytes);
  291. rb_erase(p1, &c->buds);
  292. /*
  293. * If the commit does not finish, the recovery will need
  294. * to replay the journal, in which case the old buds
  295. * must be unchanged. Do not release them until post
  296. * commit i.e. do not allow them to be garbage
  297. * collected.
  298. */
  299. list_move(&bud->list, &c->old_buds);
  300. }
  301. }
  302. spin_unlock(&c->buds_lock);
  303. }
  304. /**
  305. * ubifs_log_start_commit - start commit.
  306. * @c: UBIFS file-system description object
  307. * @ltail_lnum: return new log tail LEB number
  308. *
  309. * The commit operation starts with writing "commit start" node to the log and
  310. * reference nodes for all journal heads which will define new journal after
  311. * the commit has been finished. The commit start and reference nodes are
  312. * written in one go to the nearest empty log LEB (hence, when commit is
  313. * finished UBIFS may safely unmap all the previous log LEBs). This function
  314. * returns zero in case of success and a negative error code in case of
  315. * failure.
  316. */
  317. int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
  318. {
  319. void *buf;
  320. struct ubifs_cs_node *cs;
  321. struct ubifs_ref_node *ref;
  322. int err, i, max_len, len;
  323. err = dbg_check_bud_bytes(c);
  324. if (err)
  325. return err;
  326. max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
  327. max_len = ALIGN(max_len, c->min_io_size);
  328. buf = cs = kmalloc(max_len, GFP_NOFS);
  329. if (!buf)
  330. return -ENOMEM;
  331. cs->ch.node_type = UBIFS_CS_NODE;
  332. cs->cmt_no = cpu_to_le64(c->cmt_no);
  333. ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
  334. err = ubifs_shash_init(c, c->log_hash);
  335. if (err)
  336. goto out;
  337. err = ubifs_shash_update(c, c->log_hash, cs, UBIFS_CS_NODE_SZ);
  338. if (err < 0)
  339. goto out;
  340. /*
  341. * Note, we do not lock 'c->log_mutex' because this is the commit start
  342. * phase and we are exclusively using the log. And we do not lock
  343. * write-buffer because nobody can write to the file-system at this
  344. * phase.
  345. */
  346. len = UBIFS_CS_NODE_SZ;
  347. for (i = 0; i < c->jhead_cnt; i++) {
  348. int lnum = c->jheads[i].wbuf.lnum;
  349. int offs = c->jheads[i].wbuf.offs;
  350. if (lnum == -1 || offs == c->leb_size)
  351. continue;
  352. dbg_log("add ref to LEB %d:%d for jhead %s",
  353. lnum, offs, dbg_jhead(i));
  354. ref = buf + len;
  355. ref->ch.node_type = UBIFS_REF_NODE;
  356. ref->lnum = cpu_to_le32(lnum);
  357. ref->offs = cpu_to_le32(offs);
  358. ref->jhead = cpu_to_le32(i);
  359. ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
  360. len += UBIFS_REF_NODE_SZ;
  361. err = ubifs_shash_update(c, c->log_hash, ref,
  362. UBIFS_REF_NODE_SZ);
  363. if (err)
  364. goto out;
  365. ubifs_shash_copy_state(c, c->log_hash, c->jheads[i].log_hash);
  366. }
  367. ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
  368. /* Switch to the next log LEB */
  369. if (c->lhead_offs) {
  370. c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  371. ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
  372. c->lhead_offs = 0;
  373. }
  374. /* Must ensure next LEB has been unmapped */
  375. err = ubifs_leb_unmap(c, c->lhead_lnum);
  376. if (err)
  377. goto out;
  378. len = ALIGN(len, c->min_io_size);
  379. dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
  380. err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
  381. if (err)
  382. goto out;
  383. *ltail_lnum = c->lhead_lnum;
  384. c->lhead_offs += len;
  385. ubifs_assert(c, c->lhead_offs < c->leb_size);
  386. remove_buds(c);
  387. /*
  388. * We have started the commit and now users may use the rest of the log
  389. * for new writes.
  390. */
  391. c->min_log_bytes = 0;
  392. out:
  393. kfree(buf);
  394. return err;
  395. }
  396. /**
  397. * ubifs_log_end_commit - end commit.
  398. * @c: UBIFS file-system description object
  399. * @ltail_lnum: new log tail LEB number
  400. *
  401. * This function is called on when the commit operation was finished. It
  402. * moves log tail to new position and updates the master node so that it stores
  403. * the new log tail LEB number. Returns zero in case of success and a negative
  404. * error code in case of failure.
  405. */
  406. int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
  407. {
  408. int err;
  409. /*
  410. * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
  411. * writes during commit. Its only short "commit" start phase when
  412. * writers are blocked.
  413. */
  414. mutex_lock(&c->log_mutex);
  415. dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
  416. c->ltail_lnum, ltail_lnum);
  417. c->ltail_lnum = ltail_lnum;
  418. /*
  419. * The commit is finished and from now on it must be guaranteed that
  420. * there is always enough space for the next commit.
  421. */
  422. c->min_log_bytes = c->leb_size;
  423. spin_lock(&c->buds_lock);
  424. c->bud_bytes -= c->cmt_bud_bytes;
  425. spin_unlock(&c->buds_lock);
  426. err = dbg_check_bud_bytes(c);
  427. if (err)
  428. goto out;
  429. err = ubifs_write_master(c);
  430. out:
  431. mutex_unlock(&c->log_mutex);
  432. return err;
  433. }
  434. /**
  435. * ubifs_log_post_commit - things to do after commit is completed.
  436. * @c: UBIFS file-system description object
  437. * @old_ltail_lnum: old log tail LEB number
  438. *
  439. * Release buds only after commit is completed, because they must be unchanged
  440. * if recovery is needed.
  441. *
  442. * Unmap log LEBs only after commit is completed, because they may be needed for
  443. * recovery.
  444. *
  445. * This function returns %0 on success and a negative error code on failure.
  446. */
  447. int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
  448. {
  449. int lnum, err = 0;
  450. while (!list_empty(&c->old_buds)) {
  451. struct ubifs_bud *bud;
  452. bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
  453. err = ubifs_return_leb(c, bud->lnum);
  454. if (err)
  455. return err;
  456. list_del(&bud->list);
  457. kfree(bud->log_hash);
  458. kfree(bud);
  459. }
  460. mutex_lock(&c->log_mutex);
  461. for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
  462. lnum = ubifs_next_log_lnum(c, lnum)) {
  463. dbg_log("unmap log LEB %d", lnum);
  464. err = ubifs_leb_unmap(c, lnum);
  465. if (err)
  466. goto out;
  467. }
  468. out:
  469. mutex_unlock(&c->log_mutex);
  470. return err;
  471. }
  472. /**
  473. * struct done_ref - references that have been done.
  474. * @rb: rb-tree node
  475. * @lnum: LEB number
  476. */
  477. struct done_ref {
  478. struct rb_node rb;
  479. int lnum;
  480. };
  481. /**
  482. * done_already - determine if a reference has been done already.
  483. * @done_tree: rb-tree to store references that have been done
  484. * @lnum: LEB number of reference
  485. *
  486. * This function returns %1 if the reference has been done, %0 if not, otherwise
  487. * a negative error code is returned.
  488. */
  489. static int done_already(struct rb_root *done_tree, int lnum)
  490. {
  491. struct rb_node **p = &done_tree->rb_node, *parent = NULL;
  492. struct done_ref *dr;
  493. while (*p) {
  494. parent = *p;
  495. dr = rb_entry(parent, struct done_ref, rb);
  496. if (lnum < dr->lnum)
  497. p = &(*p)->rb_left;
  498. else if (lnum > dr->lnum)
  499. p = &(*p)->rb_right;
  500. else
  501. return 1;
  502. }
  503. dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
  504. if (!dr)
  505. return -ENOMEM;
  506. dr->lnum = lnum;
  507. rb_link_node(&dr->rb, parent, p);
  508. rb_insert_color(&dr->rb, done_tree);
  509. return 0;
  510. }
  511. /**
  512. * destroy_done_tree - destroy the done tree.
  513. * @done_tree: done tree to destroy
  514. */
  515. static void destroy_done_tree(struct rb_root *done_tree)
  516. {
  517. struct done_ref *dr, *n;
  518. rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb)
  519. kfree(dr);
  520. }
  521. /**
  522. * add_node - add a node to the consolidated log.
  523. * @c: UBIFS file-system description object
  524. * @buf: buffer to which to add
  525. * @lnum: LEB number to which to write is passed and returned here
  526. * @offs: offset to where to write is passed and returned here
  527. * @node: node to add
  528. *
  529. * This function returns %0 on success and a negative error code on failure.
  530. */
  531. static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
  532. void *node)
  533. {
  534. struct ubifs_ch *ch = node;
  535. int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
  536. if (len > remains) {
  537. int sz = ALIGN(*offs, c->min_io_size), err;
  538. ubifs_pad(c, buf + *offs, sz - *offs);
  539. err = ubifs_leb_change(c, *lnum, buf, sz);
  540. if (err)
  541. return err;
  542. *lnum = ubifs_next_log_lnum(c, *lnum);
  543. *offs = 0;
  544. }
  545. memcpy(buf + *offs, node, len);
  546. *offs += ALIGN(len, 8);
  547. return 0;
  548. }
  549. /**
  550. * ubifs_consolidate_log - consolidate the log.
  551. * @c: UBIFS file-system description object
  552. *
  553. * Repeated failed commits could cause the log to be full, but at least 1 LEB is
  554. * needed for commit. This function rewrites the reference nodes in the log
  555. * omitting duplicates, and failed CS nodes, and leaving no gaps.
  556. *
  557. * This function returns %0 on success and a negative error code on failure.
  558. */
  559. int ubifs_consolidate_log(struct ubifs_info *c)
  560. {
  561. struct ubifs_scan_leb *sleb;
  562. struct ubifs_scan_node *snod;
  563. struct rb_root done_tree = RB_ROOT;
  564. int lnum, err, first = 1, write_lnum, offs = 0;
  565. void *buf;
  566. dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
  567. c->lhead_lnum);
  568. buf = vmalloc(c->leb_size);
  569. if (!buf)
  570. return -ENOMEM;
  571. lnum = c->ltail_lnum;
  572. write_lnum = lnum;
  573. while (1) {
  574. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
  575. if (IS_ERR(sleb)) {
  576. err = PTR_ERR(sleb);
  577. goto out_free;
  578. }
  579. list_for_each_entry(snod, &sleb->nodes, list) {
  580. switch (snod->type) {
  581. case UBIFS_REF_NODE: {
  582. struct ubifs_ref_node *ref = snod->node;
  583. int ref_lnum = le32_to_cpu(ref->lnum);
  584. err = done_already(&done_tree, ref_lnum);
  585. if (err < 0)
  586. goto out_scan;
  587. if (err != 1) {
  588. err = add_node(c, buf, &write_lnum,
  589. &offs, snod->node);
  590. if (err)
  591. goto out_scan;
  592. }
  593. break;
  594. }
  595. case UBIFS_CS_NODE:
  596. if (!first)
  597. break;
  598. err = add_node(c, buf, &write_lnum, &offs,
  599. snod->node);
  600. if (err)
  601. goto out_scan;
  602. first = 0;
  603. break;
  604. }
  605. }
  606. ubifs_scan_destroy(sleb);
  607. if (lnum == c->lhead_lnum)
  608. break;
  609. lnum = ubifs_next_log_lnum(c, lnum);
  610. }
  611. if (offs) {
  612. int sz = ALIGN(offs, c->min_io_size);
  613. ubifs_pad(c, buf + offs, sz - offs);
  614. err = ubifs_leb_change(c, write_lnum, buf, sz);
  615. if (err)
  616. goto out_free;
  617. offs = ALIGN(offs, c->min_io_size);
  618. }
  619. destroy_done_tree(&done_tree);
  620. vfree(buf);
  621. if (write_lnum == c->lhead_lnum) {
  622. ubifs_err(c, "log is too full");
  623. return -EINVAL;
  624. }
  625. /* Unmap remaining LEBs */
  626. lnum = write_lnum;
  627. do {
  628. lnum = ubifs_next_log_lnum(c, lnum);
  629. err = ubifs_leb_unmap(c, lnum);
  630. if (err)
  631. return err;
  632. } while (lnum != c->lhead_lnum);
  633. c->lhead_lnum = write_lnum;
  634. c->lhead_offs = offs;
  635. dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
  636. return 0;
  637. out_scan:
  638. ubifs_scan_destroy(sleb);
  639. out_free:
  640. destroy_done_tree(&done_tree);
  641. vfree(buf);
  642. return err;
  643. }
  644. /**
  645. * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
  646. * @c: UBIFS file-system description object
  647. *
  648. * This function makes sure the amount of flash space used by closed buds
  649. * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
  650. * case of failure.
  651. */
  652. static int dbg_check_bud_bytes(struct ubifs_info *c)
  653. {
  654. int i, err = 0;
  655. struct ubifs_bud *bud;
  656. long long bud_bytes = 0;
  657. if (!dbg_is_chk_gen(c))
  658. return 0;
  659. spin_lock(&c->buds_lock);
  660. for (i = 0; i < c->jhead_cnt; i++)
  661. list_for_each_entry(bud, &c->jheads[i].buds_list, list)
  662. bud_bytes += c->leb_size - bud->start;
  663. if (c->bud_bytes != bud_bytes) {
  664. ubifs_err(c, "bad bud_bytes %lld, calculated %lld",
  665. c->bud_bytes, bud_bytes);
  666. err = -EINVAL;
  667. }
  668. spin_unlock(&c->buds_lock);
  669. return err;
  670. }