log.c 20 KB

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