log.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /**
  30. * ubifs_search_bud - search bud LEB.
  31. * @c: UBIFS file-system description object
  32. * @lnum: logical eraseblock number to search
  33. *
  34. * This function searches bud LEB @lnum. Returns bud description object in case
  35. * of success and %NULL if there is no bud with this LEB number.
  36. */
  37. struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
  38. {
  39. struct rb_node *p;
  40. struct ubifs_bud *bud;
  41. spin_lock(&c->buds_lock);
  42. p = c->buds.rb_node;
  43. while (p) {
  44. bud = rb_entry(p, struct ubifs_bud, rb);
  45. if (lnum < bud->lnum)
  46. p = p->rb_left;
  47. else if (lnum > bud->lnum)
  48. p = p->rb_right;
  49. else {
  50. spin_unlock(&c->buds_lock);
  51. return bud;
  52. }
  53. }
  54. spin_unlock(&c->buds_lock);
  55. return NULL;
  56. }
  57. /**
  58. * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
  59. * @c: UBIFS file-system description object
  60. * @bud: the bud to add
  61. */
  62. void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
  63. {
  64. struct rb_node **p, *parent = NULL;
  65. struct ubifs_bud *b;
  66. struct ubifs_jhead *jhead;
  67. spin_lock(&c->buds_lock);
  68. p = &c->buds.rb_node;
  69. while (*p) {
  70. parent = *p;
  71. b = rb_entry(parent, struct ubifs_bud, rb);
  72. ubifs_assert(bud->lnum != b->lnum);
  73. if (bud->lnum < b->lnum)
  74. p = &(*p)->rb_left;
  75. else
  76. p = &(*p)->rb_right;
  77. }
  78. rb_link_node(&bud->rb, parent, p);
  79. rb_insert_color(&bud->rb, &c->buds);
  80. if (c->jheads) {
  81. jhead = &c->jheads[bud->jhead];
  82. list_add_tail(&bud->list, &jhead->buds_list);
  83. } else
  84. ubifs_assert(c->replaying && (c->vfs_sb->s_flags & MS_RDONLY));
  85. /*
  86. * Note, although this is a new bud, we anyway account this space now,
  87. * before any data has been written to it, because this is about to
  88. * guarantee fixed mount time, and this bud will anyway be read and
  89. * scanned.
  90. */
  91. c->bud_bytes += c->leb_size - bud->start;
  92. dbg_log("LEB %d:%d, jhead %d, bud_bytes %lld", bud->lnum,
  93. bud->start, bud->jhead, c->bud_bytes);
  94. spin_unlock(&c->buds_lock);
  95. }