log.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. #ifdef CONFIG_UBIFS_FS_DEBUG
  30. static int dbg_check_bud_bytes(struct ubifs_info *c);
  31. #else
  32. #define dbg_check_bud_bytes(c) 0
  33. #endif
  34. /**
  35. * ubifs_search_bud - search bud LEB.
  36. * @c: UBIFS file-system description object
  37. * @lnum: logical eraseblock number to search
  38. *
  39. * This function searches bud LEB @lnum. Returns bud description object in case
  40. * of success and %NULL if there is no bud with this LEB number.
  41. */
  42. struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
  43. {
  44. struct rb_node *p;
  45. struct ubifs_bud *bud;
  46. spin_lock(&c->buds_lock);
  47. p = c->buds.rb_node;
  48. while (p) {
  49. bud = rb_entry(p, struct ubifs_bud, rb);
  50. if (lnum < bud->lnum)
  51. p = p->rb_left;
  52. else if (lnum > bud->lnum)
  53. p = p->rb_right;
  54. else {
  55. spin_unlock(&c->buds_lock);
  56. return bud;
  57. }
  58. }
  59. spin_unlock(&c->buds_lock);
  60. return NULL;
  61. }
  62. /**
  63. * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
  64. * @c: UBIFS file-system description object
  65. * @lnum: logical eraseblock number to search
  66. *
  67. * This functions returns the wbuf for @lnum or %NULL if there is not one.
  68. */
  69. struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
  70. {
  71. struct rb_node *p;
  72. struct ubifs_bud *bud;
  73. int jhead;
  74. if (!c->jheads)
  75. return NULL;
  76. spin_lock(&c->buds_lock);
  77. p = c->buds.rb_node;
  78. while (p) {
  79. bud = rb_entry(p, struct ubifs_bud, rb);
  80. if (lnum < bud->lnum)
  81. p = p->rb_left;
  82. else if (lnum > bud->lnum)
  83. p = p->rb_right;
  84. else {
  85. jhead = bud->jhead;
  86. spin_unlock(&c->buds_lock);
  87. return &c->jheads[jhead].wbuf;
  88. }
  89. }
  90. spin_unlock(&c->buds_lock);
  91. return NULL;
  92. }
  93. /**
  94. * empty_log_bytes - calculate amount of empty space in the log.
  95. * @c: UBIFS file-system description object
  96. */
  97. static inline long long empty_log_bytes(const struct ubifs_info *c)
  98. {
  99. long long h, t;
  100. h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
  101. t = (long long)c->ltail_lnum * c->leb_size;
  102. if (h >= t)
  103. return c->log_bytes - h + t;
  104. else
  105. return t - h;
  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(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->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 reference node for the new bud LEB @lnum it to the log,
  154. * and adds it to the buds tress. 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 codes 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->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. c->lhead_offs = 0;
  223. }
  224. if (c->lhead_offs == 0) {
  225. /* Must ensure next log LEB has been unmapped */
  226. err = ubifs_leb_unmap(c, c->lhead_lnum);
  227. if (err)
  228. goto out_unlock;
  229. }
  230. if (bud->start == 0) {
  231. /*
  232. * Before writing the LEB reference which refers an empty LEB
  233. * to the log, we have to make sure it is mapped, because
  234. * otherwise we'd risk to refer an LEB with garbage in case of
  235. * an unclean reboot, because the target LEB might have been
  236. * unmapped, but not yet physically erased.
  237. */
  238. err = ubi_leb_map(c->ubi, bud->lnum, UBI_SHORTTERM);
  239. if (err)
  240. goto out_unlock;
  241. }
  242. dbg_log("write ref LEB %d:%d",
  243. c->lhead_lnum, c->lhead_offs);
  244. err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
  245. c->lhead_offs, UBI_SHORTTERM);
  246. if (err)
  247. goto out_unlock;
  248. c->lhead_offs += c->ref_node_alsz;
  249. ubifs_add_bud(c, bud);
  250. mutex_unlock(&c->log_mutex);
  251. kfree(ref);
  252. return 0;
  253. out_unlock:
  254. if (err != -EAGAIN)
  255. ubifs_ro_mode(c, err);
  256. mutex_unlock(&c->log_mutex);
  257. kfree(ref);
  258. kfree(bud);
  259. return err;
  260. }
  261. /**
  262. * remove_buds - remove used buds.
  263. * @c: UBIFS file-system description object
  264. *
  265. * This function removes use buds from the buds tree. It does not remove the
  266. * buds which are pointed to by journal heads.
  267. */
  268. static void remove_buds(struct ubifs_info *c)
  269. {
  270. struct rb_node *p;
  271. ubifs_assert(list_empty(&c->old_buds));
  272. c->cmt_bud_bytes = 0;
  273. spin_lock(&c->buds_lock);
  274. p = rb_first(&c->buds);
  275. while (p) {
  276. struct rb_node *p1 = p;
  277. struct ubifs_bud *bud;
  278. struct ubifs_wbuf *wbuf;
  279. p = rb_next(p);
  280. bud = rb_entry(p1, struct ubifs_bud, rb);
  281. wbuf = &c->jheads[bud->jhead].wbuf;
  282. if (wbuf->lnum == bud->lnum) {
  283. /*
  284. * Do not remove buds which are pointed to by journal
  285. * heads (non-closed buds).
  286. */
  287. c->cmt_bud_bytes += wbuf->offs - bud->start;
  288. dbg_log("preserve %d:%d, jhead %s, bud bytes %d, "
  289. "cmt_bud_bytes %lld", bud->lnum, bud->start,
  290. dbg_jhead(bud->jhead), wbuf->offs - bud->start,
  291. c->cmt_bud_bytes);
  292. bud->start = wbuf->offs;
  293. } else {
  294. c->cmt_bud_bytes += c->leb_size - bud->start;
  295. dbg_log("remove %d:%d, jhead %s, bud bytes %d, "
  296. "cmt_bud_bytes %lld", bud->lnum, bud->start,
  297. dbg_jhead(bud->jhead), c->leb_size - bud->start,
  298. c->cmt_bud_bytes);
  299. rb_erase(p1, &c->buds);
  300. /*
  301. * If the commit does not finish, the recovery will need
  302. * to replay the journal, in which case the old buds
  303. * must be unchanged. Do not release them until post
  304. * commit i.e. do not allow them to be garbage
  305. * collected.
  306. */
  307. list_move(&bud->list, &c->old_buds);
  308. }
  309. }
  310. spin_unlock(&c->buds_lock);
  311. }
  312. /**
  313. * ubifs_log_start_commit - start commit.
  314. * @c: UBIFS file-system description object
  315. * @ltail_lnum: return new log tail LEB number
  316. *
  317. * The commit operation starts with writing "commit start" node to the log and
  318. * reference nodes for all journal heads which will define new journal after
  319. * the commit has been finished. The commit start and reference nodes are
  320. * written in one go to the nearest empty log LEB (hence, when commit is
  321. * finished UBIFS may safely unmap all the previous log LEBs). This function
  322. * returns zero in case of success and a negative error code in case of
  323. * failure.
  324. */
  325. int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
  326. {
  327. void *buf;
  328. struct ubifs_cs_node *cs;
  329. struct ubifs_ref_node *ref;
  330. int err, i, max_len, len;
  331. err = dbg_check_bud_bytes(c);
  332. if (err)
  333. return err;
  334. max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
  335. max_len = ALIGN(max_len, c->min_io_size);
  336. buf = cs = kmalloc(max_len, GFP_NOFS);
  337. if (!buf)
  338. return -ENOMEM;
  339. cs->ch.node_type = UBIFS_CS_NODE;
  340. cs->cmt_no = cpu_to_le64(c->cmt_no);
  341. ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
  342. /*
  343. * Note, we do not lock 'c->log_mutex' because this is the commit start
  344. * phase and we are exclusively using the log. And we do not lock
  345. * write-buffer because nobody can write to the file-system at this
  346. * phase.
  347. */
  348. len = UBIFS_CS_NODE_SZ;
  349. for (i = 0; i < c->jhead_cnt; i++) {
  350. int lnum = c->jheads[i].wbuf.lnum;
  351. int offs = c->jheads[i].wbuf.offs;
  352. if (lnum == -1 || offs == c->leb_size)
  353. continue;
  354. dbg_log("add ref to LEB %d:%d for jhead %s",
  355. lnum, offs, dbg_jhead(i));
  356. ref = buf + len;
  357. ref->ch.node_type = UBIFS_REF_NODE;
  358. ref->lnum = cpu_to_le32(lnum);
  359. ref->offs = cpu_to_le32(offs);
  360. ref->jhead = cpu_to_le32(i);
  361. ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
  362. len += UBIFS_REF_NODE_SZ;
  363. }
  364. ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
  365. /* Switch to the next log LEB */
  366. if (c->lhead_offs) {
  367. c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  368. c->lhead_offs = 0;
  369. }
  370. if (c->lhead_offs == 0) {
  371. /* Must ensure next LEB has been unmapped */
  372. err = ubifs_leb_unmap(c, c->lhead_lnum);
  373. if (err)
  374. goto out;
  375. }
  376. len = ALIGN(len, c->min_io_size);
  377. dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
  378. err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len, UBI_SHORTTERM);
  379. if (err)
  380. goto out;
  381. *ltail_lnum = c->lhead_lnum;
  382. c->lhead_offs += len;
  383. if (c->lhead_offs == c->leb_size) {
  384. c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  385. c->lhead_offs = 0;
  386. }
  387. remove_buds(c);
  388. /*
  389. * We have started the commit and now users may use the rest of the log
  390. * for new writes.
  391. */
  392. c->min_log_bytes = 0;
  393. out:
  394. kfree(buf);
  395. return err;
  396. }
  397. /**
  398. * ubifs_log_end_commit - end commit.
  399. * @c: UBIFS file-system description object
  400. * @ltail_lnum: new log tail LEB number
  401. *
  402. * This function is called on when the commit operation was finished. It
  403. * moves log tail to new position and unmaps LEBs which contain obsolete data.
  404. * Returns zero in case of success and a negative error code in case of
  405. * failure.
  406. */
  407. int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
  408. {
  409. int err;
  410. /*
  411. * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
  412. * writes during commit. Its only short "commit" start phase when
  413. * writers are blocked.
  414. */
  415. mutex_lock(&c->log_mutex);
  416. dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
  417. c->ltail_lnum, ltail_lnum);
  418. c->ltail_lnum = ltail_lnum;
  419. /*
  420. * The commit is finished and from now on it must be guaranteed that
  421. * there is always enough space for the next commit.
  422. */
  423. c->min_log_bytes = c->leb_size;
  424. spin_lock(&c->buds_lock);
  425. c->bud_bytes -= c->cmt_bud_bytes;
  426. spin_unlock(&c->buds_lock);
  427. err = dbg_check_bud_bytes(c);
  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 rb_node *this = done_tree->rb_node;
  514. struct done_ref *dr;
  515. while (this) {
  516. if (this->rb_left) {
  517. this = this->rb_left;
  518. continue;
  519. } else if (this->rb_right) {
  520. this = this->rb_right;
  521. continue;
  522. }
  523. dr = rb_entry(this, struct done_ref, rb);
  524. this = rb_parent(this);
  525. if (this) {
  526. if (this->rb_left == &dr->rb)
  527. this->rb_left = NULL;
  528. else
  529. this->rb_right = NULL;
  530. }
  531. kfree(dr);
  532. }
  533. }
  534. /**
  535. * add_node - add a node to the consolidated log.
  536. * @c: UBIFS file-system description object
  537. * @buf: buffer to which to add
  538. * @lnum: LEB number to which to write is passed and returned here
  539. * @offs: offset to where to write is passed and returned here
  540. * @node: node to add
  541. *
  542. * This function returns %0 on success and a negative error code on failure.
  543. */
  544. static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
  545. void *node)
  546. {
  547. struct ubifs_ch *ch = node;
  548. int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
  549. if (len > remains) {
  550. int sz = ALIGN(*offs, c->min_io_size), err;
  551. ubifs_pad(c, buf + *offs, sz - *offs);
  552. err = ubifs_leb_change(c, *lnum, buf, sz, UBI_SHORTTERM);
  553. if (err)
  554. return err;
  555. *lnum = ubifs_next_log_lnum(c, *lnum);
  556. *offs = 0;
  557. }
  558. memcpy(buf + *offs, node, len);
  559. *offs += ALIGN(len, 8);
  560. return 0;
  561. }
  562. /**
  563. * ubifs_consolidate_log - consolidate the log.
  564. * @c: UBIFS file-system description object
  565. *
  566. * Repeated failed commits could cause the log to be full, but at least 1 LEB is
  567. * needed for commit. This function rewrites the reference nodes in the log
  568. * omitting duplicates, and failed CS nodes, and leaving no gaps.
  569. *
  570. * This function returns %0 on success and a negative error code on failure.
  571. */
  572. int ubifs_consolidate_log(struct ubifs_info *c)
  573. {
  574. struct ubifs_scan_leb *sleb;
  575. struct ubifs_scan_node *snod;
  576. struct rb_root done_tree = RB_ROOT;
  577. int lnum, err, first = 1, write_lnum, offs = 0;
  578. void *buf;
  579. dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
  580. c->lhead_lnum);
  581. buf = vmalloc(c->leb_size);
  582. if (!buf)
  583. return -ENOMEM;
  584. lnum = c->ltail_lnum;
  585. write_lnum = lnum;
  586. while (1) {
  587. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
  588. if (IS_ERR(sleb)) {
  589. err = PTR_ERR(sleb);
  590. goto out_free;
  591. }
  592. list_for_each_entry(snod, &sleb->nodes, list) {
  593. switch (snod->type) {
  594. case UBIFS_REF_NODE: {
  595. struct ubifs_ref_node *ref = snod->node;
  596. int ref_lnum = le32_to_cpu(ref->lnum);
  597. err = done_already(&done_tree, ref_lnum);
  598. if (err < 0)
  599. goto out_scan;
  600. if (err != 1) {
  601. err = add_node(c, buf, &write_lnum,
  602. &offs, snod->node);
  603. if (err)
  604. goto out_scan;
  605. }
  606. break;
  607. }
  608. case UBIFS_CS_NODE:
  609. if (!first)
  610. break;
  611. err = add_node(c, buf, &write_lnum, &offs,
  612. snod->node);
  613. if (err)
  614. goto out_scan;
  615. first = 0;
  616. break;
  617. }
  618. }
  619. ubifs_scan_destroy(sleb);
  620. if (lnum == c->lhead_lnum)
  621. break;
  622. lnum = ubifs_next_log_lnum(c, lnum);
  623. }
  624. if (offs) {
  625. int sz = ALIGN(offs, c->min_io_size);
  626. ubifs_pad(c, buf + offs, sz - offs);
  627. err = ubifs_leb_change(c, write_lnum, buf, sz, UBI_SHORTTERM);
  628. if (err)
  629. goto out_free;
  630. offs = ALIGN(offs, c->min_io_size);
  631. }
  632. destroy_done_tree(&done_tree);
  633. vfree(buf);
  634. if (write_lnum == c->lhead_lnum) {
  635. ubifs_err("log is too full");
  636. return -EINVAL;
  637. }
  638. /* Unmap remaining LEBs */
  639. lnum = write_lnum;
  640. do {
  641. lnum = ubifs_next_log_lnum(c, lnum);
  642. err = ubifs_leb_unmap(c, lnum);
  643. if (err)
  644. return err;
  645. } while (lnum != c->lhead_lnum);
  646. c->lhead_lnum = write_lnum;
  647. c->lhead_offs = offs;
  648. dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
  649. return 0;
  650. out_scan:
  651. ubifs_scan_destroy(sleb);
  652. out_free:
  653. destroy_done_tree(&done_tree);
  654. vfree(buf);
  655. return err;
  656. }
  657. #ifdef CONFIG_UBIFS_FS_DEBUG
  658. /**
  659. * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
  660. * @c: UBIFS file-system description object
  661. *
  662. * This function makes sure the amount of flash space used by closed buds
  663. * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
  664. * case of failure.
  665. */
  666. static int dbg_check_bud_bytes(struct ubifs_info *c)
  667. {
  668. int i, err = 0;
  669. struct ubifs_bud *bud;
  670. long long bud_bytes = 0;
  671. if (!(ubifs_chk_flags & UBIFS_CHK_GEN))
  672. return 0;
  673. spin_lock(&c->buds_lock);
  674. for (i = 0; i < c->jhead_cnt; i++)
  675. list_for_each_entry(bud, &c->jheads[i].buds_list, list)
  676. bud_bytes += c->leb_size - bud->start;
  677. if (c->bud_bytes != bud_bytes) {
  678. ubifs_err("bad bud_bytes %lld, calculated %lld",
  679. c->bud_bytes, bud_bytes);
  680. err = -EINVAL;
  681. }
  682. spin_unlock(&c->buds_lock);
  683. return err;
  684. }
  685. #endif /* CONFIG_UBIFS_FS_DEBUG */