log.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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, 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. /*
  335. * Note, we do not lock 'c->log_mutex' because this is the commit start
  336. * phase and we are exclusively using the log. And we do not lock
  337. * write-buffer because nobody can write to the file-system at this
  338. * phase.
  339. */
  340. len = UBIFS_CS_NODE_SZ;
  341. for (i = 0; i < c->jhead_cnt; i++) {
  342. int lnum = c->jheads[i].wbuf.lnum;
  343. int offs = c->jheads[i].wbuf.offs;
  344. if (lnum == -1 || offs == c->leb_size)
  345. continue;
  346. dbg_log("add ref to LEB %d:%d for jhead %s",
  347. lnum, offs, dbg_jhead(i));
  348. ref = buf + len;
  349. ref->ch.node_type = UBIFS_REF_NODE;
  350. ref->lnum = cpu_to_le32(lnum);
  351. ref->offs = cpu_to_le32(offs);
  352. ref->jhead = cpu_to_le32(i);
  353. ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
  354. len += UBIFS_REF_NODE_SZ;
  355. }
  356. ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
  357. /* Switch to the next log LEB */
  358. if (c->lhead_offs) {
  359. c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  360. c->lhead_offs = 0;
  361. }
  362. if (c->lhead_offs == 0) {
  363. /* Must ensure next LEB has been unmapped */
  364. err = ubifs_leb_unmap(c, c->lhead_lnum);
  365. if (err)
  366. goto out;
  367. }
  368. len = ALIGN(len, c->min_io_size);
  369. dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
  370. err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
  371. if (err)
  372. goto out;
  373. *ltail_lnum = c->lhead_lnum;
  374. c->lhead_offs += len;
  375. if (c->lhead_offs == c->leb_size) {
  376. c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
  377. c->lhead_offs = 0;
  378. }
  379. remove_buds(c);
  380. /*
  381. * We have started the commit and now users may use the rest of the log
  382. * for new writes.
  383. */
  384. c->min_log_bytes = 0;
  385. out:
  386. kfree(buf);
  387. return err;
  388. }
  389. /**
  390. * ubifs_log_end_commit - end commit.
  391. * @c: UBIFS file-system description object
  392. * @ltail_lnum: new log tail LEB number
  393. *
  394. * This function is called on when the commit operation was finished. It
  395. * moves log tail to new position and unmaps LEBs which contain obsolete data.
  396. * Returns zero in case of success and a negative error code in case of
  397. * failure.
  398. */
  399. int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
  400. {
  401. int err;
  402. /*
  403. * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
  404. * writes during commit. Its only short "commit" start phase when
  405. * writers are blocked.
  406. */
  407. mutex_lock(&c->log_mutex);
  408. dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
  409. c->ltail_lnum, ltail_lnum);
  410. c->ltail_lnum = ltail_lnum;
  411. /*
  412. * The commit is finished and from now on it must be guaranteed that
  413. * there is always enough space for the next commit.
  414. */
  415. c->min_log_bytes = c->leb_size;
  416. spin_lock(&c->buds_lock);
  417. c->bud_bytes -= c->cmt_bud_bytes;
  418. spin_unlock(&c->buds_lock);
  419. err = dbg_check_bud_bytes(c);
  420. mutex_unlock(&c->log_mutex);
  421. return err;
  422. }
  423. /**
  424. * ubifs_log_post_commit - things to do after commit is completed.
  425. * @c: UBIFS file-system description object
  426. * @old_ltail_lnum: old log tail LEB number
  427. *
  428. * Release buds only after commit is completed, because they must be unchanged
  429. * if recovery is needed.
  430. *
  431. * Unmap log LEBs only after commit is completed, because they may be needed for
  432. * recovery.
  433. *
  434. * This function returns %0 on success and a negative error code on failure.
  435. */
  436. int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
  437. {
  438. int lnum, err = 0;
  439. while (!list_empty(&c->old_buds)) {
  440. struct ubifs_bud *bud;
  441. bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
  442. err = ubifs_return_leb(c, bud->lnum);
  443. if (err)
  444. return err;
  445. list_del(&bud->list);
  446. kfree(bud);
  447. }
  448. mutex_lock(&c->log_mutex);
  449. for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
  450. lnum = ubifs_next_log_lnum(c, lnum)) {
  451. dbg_log("unmap log LEB %d", lnum);
  452. err = ubifs_leb_unmap(c, lnum);
  453. if (err)
  454. goto out;
  455. }
  456. out:
  457. mutex_unlock(&c->log_mutex);
  458. return err;
  459. }
  460. /**
  461. * struct done_ref - references that have been done.
  462. * @rb: rb-tree node
  463. * @lnum: LEB number
  464. */
  465. struct done_ref {
  466. struct rb_node rb;
  467. int lnum;
  468. };
  469. /**
  470. * done_already - determine if a reference has been done already.
  471. * @done_tree: rb-tree to store references that have been done
  472. * @lnum: LEB number of reference
  473. *
  474. * This function returns %1 if the reference has been done, %0 if not, otherwise
  475. * a negative error code is returned.
  476. */
  477. static int done_already(struct rb_root *done_tree, int lnum)
  478. {
  479. struct rb_node **p = &done_tree->rb_node, *parent = NULL;
  480. struct done_ref *dr;
  481. while (*p) {
  482. parent = *p;
  483. dr = rb_entry(parent, struct done_ref, rb);
  484. if (lnum < dr->lnum)
  485. p = &(*p)->rb_left;
  486. else if (lnum > dr->lnum)
  487. p = &(*p)->rb_right;
  488. else
  489. return 1;
  490. }
  491. dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
  492. if (!dr)
  493. return -ENOMEM;
  494. dr->lnum = lnum;
  495. rb_link_node(&dr->rb, parent, p);
  496. rb_insert_color(&dr->rb, done_tree);
  497. return 0;
  498. }
  499. /**
  500. * destroy_done_tree - destroy the done tree.
  501. * @done_tree: done tree to destroy
  502. */
  503. static void destroy_done_tree(struct rb_root *done_tree)
  504. {
  505. struct rb_node *this = done_tree->rb_node;
  506. struct done_ref *dr;
  507. while (this) {
  508. if (this->rb_left) {
  509. this = this->rb_left;
  510. continue;
  511. } else if (this->rb_right) {
  512. this = this->rb_right;
  513. continue;
  514. }
  515. dr = rb_entry(this, struct done_ref, rb);
  516. this = rb_parent(this);
  517. if (this) {
  518. if (this->rb_left == &dr->rb)
  519. this->rb_left = NULL;
  520. else
  521. this->rb_right = NULL;
  522. }
  523. kfree(dr);
  524. }
  525. }
  526. /**
  527. * add_node - add a node to the consolidated log.
  528. * @c: UBIFS file-system description object
  529. * @buf: buffer to which to add
  530. * @lnum: LEB number to which to write is passed and returned here
  531. * @offs: offset to where to write is passed and returned here
  532. * @node: node to add
  533. *
  534. * This function returns %0 on success and a negative error code on failure.
  535. */
  536. static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
  537. void *node)
  538. {
  539. struct ubifs_ch *ch = node;
  540. int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
  541. if (len > remains) {
  542. int sz = ALIGN(*offs, c->min_io_size), err;
  543. ubifs_pad(c, buf + *offs, sz - *offs);
  544. err = ubifs_leb_change(c, *lnum, buf, sz);
  545. if (err)
  546. return err;
  547. *lnum = ubifs_next_log_lnum(c, *lnum);
  548. *offs = 0;
  549. }
  550. memcpy(buf + *offs, node, len);
  551. *offs += ALIGN(len, 8);
  552. return 0;
  553. }
  554. /**
  555. * ubifs_consolidate_log - consolidate the log.
  556. * @c: UBIFS file-system description object
  557. *
  558. * Repeated failed commits could cause the log to be full, but at least 1 LEB is
  559. * needed for commit. This function rewrites the reference nodes in the log
  560. * omitting duplicates, and failed CS nodes, and leaving no gaps.
  561. *
  562. * This function returns %0 on success and a negative error code on failure.
  563. */
  564. int ubifs_consolidate_log(struct ubifs_info *c)
  565. {
  566. struct ubifs_scan_leb *sleb;
  567. struct ubifs_scan_node *snod;
  568. struct rb_root done_tree = RB_ROOT;
  569. int lnum, err, first = 1, write_lnum, offs = 0;
  570. void *buf;
  571. dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
  572. c->lhead_lnum);
  573. buf = vmalloc(c->leb_size);
  574. if (!buf)
  575. return -ENOMEM;
  576. lnum = c->ltail_lnum;
  577. write_lnum = lnum;
  578. while (1) {
  579. sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
  580. if (IS_ERR(sleb)) {
  581. err = PTR_ERR(sleb);
  582. goto out_free;
  583. }
  584. list_for_each_entry(snod, &sleb->nodes, list) {
  585. switch (snod->type) {
  586. case UBIFS_REF_NODE: {
  587. struct ubifs_ref_node *ref = snod->node;
  588. int ref_lnum = le32_to_cpu(ref->lnum);
  589. err = done_already(&done_tree, ref_lnum);
  590. if (err < 0)
  591. goto out_scan;
  592. if (err != 1) {
  593. err = add_node(c, buf, &write_lnum,
  594. &offs, snod->node);
  595. if (err)
  596. goto out_scan;
  597. }
  598. break;
  599. }
  600. case UBIFS_CS_NODE:
  601. if (!first)
  602. break;
  603. err = add_node(c, buf, &write_lnum, &offs,
  604. snod->node);
  605. if (err)
  606. goto out_scan;
  607. first = 0;
  608. break;
  609. }
  610. }
  611. ubifs_scan_destroy(sleb);
  612. if (lnum == c->lhead_lnum)
  613. break;
  614. lnum = ubifs_next_log_lnum(c, lnum);
  615. }
  616. if (offs) {
  617. int sz = ALIGN(offs, c->min_io_size);
  618. ubifs_pad(c, buf + offs, sz - offs);
  619. err = ubifs_leb_change(c, write_lnum, buf, sz);
  620. if (err)
  621. goto out_free;
  622. offs = ALIGN(offs, c->min_io_size);
  623. }
  624. destroy_done_tree(&done_tree);
  625. vfree(buf);
  626. if (write_lnum == c->lhead_lnum) {
  627. ubifs_err("log is too full");
  628. return -EINVAL;
  629. }
  630. /* Unmap remaining LEBs */
  631. lnum = write_lnum;
  632. do {
  633. lnum = ubifs_next_log_lnum(c, lnum);
  634. err = ubifs_leb_unmap(c, lnum);
  635. if (err)
  636. return err;
  637. } while (lnum != c->lhead_lnum);
  638. c->lhead_lnum = write_lnum;
  639. c->lhead_offs = offs;
  640. dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
  641. return 0;
  642. out_scan:
  643. ubifs_scan_destroy(sleb);
  644. out_free:
  645. destroy_done_tree(&done_tree);
  646. vfree(buf);
  647. return err;
  648. }
  649. /**
  650. * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
  651. * @c: UBIFS file-system description object
  652. *
  653. * This function makes sure the amount of flash space used by closed buds
  654. * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
  655. * case of failure.
  656. */
  657. static int dbg_check_bud_bytes(struct ubifs_info *c)
  658. {
  659. int i, err = 0;
  660. struct ubifs_bud *bud;
  661. long long bud_bytes = 0;
  662. if (!dbg_is_chk_gen(c))
  663. return 0;
  664. spin_lock(&c->buds_lock);
  665. for (i = 0; i < c->jhead_cnt; i++)
  666. list_for_each_entry(bud, &c->jheads[i].buds_list, list)
  667. bud_bytes += c->leb_size - bud->start;
  668. if (c->bud_bytes != bud_bytes) {
  669. ubifs_err("bad bud_bytes %lld, calculated %lld",
  670. c->bud_bytes, bud_bytes);
  671. err = -EINVAL;
  672. }
  673. spin_unlock(&c->buds_lock);
  674. return err;
  675. }