log.c 21 KB

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