log.c 20 KB

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