log.c 19 KB

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