debug.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 implements most of the debugging stuff which is compiled in only
  24. * when it is enabled. But some debugging check functions are implemented in
  25. * corresponding subsystem, just because they are closely related and utilize
  26. * various local functions of those subsystems.
  27. */
  28. #define UBIFS_DBG_PRESERVE_UBI
  29. #include "ubifs.h"
  30. #ifdef CONFIG_UBIFS_FS_DEBUG
  31. DEFINE_SPINLOCK(dbg_lock);
  32. static char dbg_key_buf0[128];
  33. static char dbg_key_buf1[128];
  34. unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT;
  35. unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT;
  36. unsigned int ubifs_tst_flags;
  37. module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR);
  38. module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR);
  39. module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR);
  40. MODULE_PARM_DESC(debug_msgs, "Debug message type flags");
  41. MODULE_PARM_DESC(debug_chks, "Debug check flags");
  42. MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
  43. static const char *get_key_type(int type)
  44. {
  45. switch (type) {
  46. case UBIFS_INO_KEY:
  47. return "inode";
  48. case UBIFS_DENT_KEY:
  49. return "direntry";
  50. case UBIFS_XENT_KEY:
  51. return "xentry";
  52. case UBIFS_DATA_KEY:
  53. return "data";
  54. case UBIFS_TRUN_KEY:
  55. return "truncate";
  56. default:
  57. return "unknown/invalid key";
  58. }
  59. }
  60. static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
  61. char *buffer)
  62. {
  63. char *p = buffer;
  64. int type = key_type(c, key);
  65. if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
  66. switch (type) {
  67. case UBIFS_INO_KEY:
  68. sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
  69. get_key_type(type));
  70. break;
  71. case UBIFS_DENT_KEY:
  72. case UBIFS_XENT_KEY:
  73. sprintf(p, "(%lu, %s, %#08x)",
  74. (unsigned long)key_inum(c, key),
  75. get_key_type(type), key_hash(c, key));
  76. break;
  77. case UBIFS_DATA_KEY:
  78. sprintf(p, "(%lu, %s, %u)",
  79. (unsigned long)key_inum(c, key),
  80. get_key_type(type), key_block(c, key));
  81. break;
  82. case UBIFS_TRUN_KEY:
  83. sprintf(p, "(%lu, %s)",
  84. (unsigned long)key_inum(c, key),
  85. get_key_type(type));
  86. break;
  87. default:
  88. sprintf(p, "(bad key type: %#08x, %#08x)",
  89. key->u32[0], key->u32[1]);
  90. }
  91. } else
  92. sprintf(p, "bad key format %d", c->key_fmt);
  93. }
  94. const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key)
  95. {
  96. /* dbg_lock must be held */
  97. sprintf_key(c, key, dbg_key_buf0);
  98. return dbg_key_buf0;
  99. }
  100. const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key)
  101. {
  102. /* dbg_lock must be held */
  103. sprintf_key(c, key, dbg_key_buf1);
  104. return dbg_key_buf1;
  105. }
  106. /**
  107. * ubifs_debugging_init - initialize UBIFS debugging.
  108. * @c: UBIFS file-system description object
  109. *
  110. * This function initializes debugging-related data for the file system.
  111. * Returns zero in case of success and a negative error code in case of
  112. * failure.
  113. */
  114. int ubifs_debugging_init(struct ubifs_info *c)
  115. {
  116. c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL);
  117. if (!c->dbg)
  118. return -ENOMEM;
  119. c->dbg->buf = vmalloc(c->leb_size);
  120. if (!c->dbg->buf)
  121. goto out;
  122. return 0;
  123. out:
  124. kfree(c->dbg);
  125. return -ENOMEM;
  126. }
  127. /**
  128. * ubifs_debugging_exit - free debugging data.
  129. * @c: UBIFS file-system description object
  130. */
  131. void ubifs_debugging_exit(struct ubifs_info *c)
  132. {
  133. vfree(c->dbg->buf);
  134. kfree(c->dbg);
  135. }
  136. #endif /* CONFIG_UBIFS_FS_DEBUG */