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