log.c 19 KB

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