log.c 20 KB

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