1 /* 2 * This file is part of UBIFS. 3 * 4 * Copyright (C) 2006-2008 Nokia Corporation. 5 * 6 * SPDX-License-Identifier: GPL-2.0+ 7 * 8 * Authors: Artem Bityutskiy (Битюцкий Артём) 9 * Adrian Hunter 10 */ 11 12 #ifndef __UBIFS_DEBUG_H__ 13 #define __UBIFS_DEBUG_H__ 14 15 #define __UBOOT__ 16 /* Checking helper functions */ 17 typedef int (*dbg_leaf_callback)(struct ubifs_info *c, 18 struct ubifs_zbranch *zbr, void *priv); 19 typedef int (*dbg_znode_callback)(struct ubifs_info *c, 20 struct ubifs_znode *znode, void *priv); 21 22 /* 23 * The UBIFS debugfs directory name pattern and maximum name length (3 for "ubi" 24 * + 1 for "_" and plus 2x2 for 2 UBI numbers and 1 for the trailing zero byte. 25 */ 26 #define UBIFS_DFS_DIR_NAME "ubi%d_%d" 27 #define UBIFS_DFS_DIR_LEN (3 + 1 + 2*2 + 1) 28 29 /** 30 * ubifs_debug_info - per-FS debugging information. 31 * @old_zroot: old index root - used by 'dbg_check_old_index()' 32 * @old_zroot_level: old index root level - used by 'dbg_check_old_index()' 33 * @old_zroot_sqnum: old index root sqnum - used by 'dbg_check_old_index()' 34 * 35 * @pc_happened: non-zero if an emulated power cut happened 36 * @pc_delay: 0=>don't delay, 1=>delay a time, 2=>delay a number of calls 37 * @pc_timeout: time in jiffies when delay of failure mode expires 38 * @pc_cnt: current number of calls to failure mode I/O functions 39 * @pc_cnt_max: number of calls by which to delay failure mode 40 * 41 * @chk_lpt_sz: used by LPT tree size checker 42 * @chk_lpt_sz2: used by LPT tree size checker 43 * @chk_lpt_wastage: used by LPT tree size checker 44 * @chk_lpt_lebs: used by LPT tree size checker 45 * @new_nhead_offs: used by LPT tree size checker 46 * @new_ihead_lnum: used by debugging to check @c->ihead_lnum 47 * @new_ihead_offs: used by debugging to check @c->ihead_offs 48 * 49 * @saved_lst: saved lprops statistics (used by 'dbg_save_space_info()') 50 * @saved_bi: saved budgeting information 51 * @saved_free: saved amount of free space 52 * @saved_idx_gc_cnt: saved value of @c->idx_gc_cnt 53 * 54 * @chk_gen: if general extra checks are enabled 55 * @chk_index: if index xtra checks are enabled 56 * @chk_orph: if orphans extra checks are enabled 57 * @chk_lprops: if lprops extra checks are enabled 58 * @chk_fs: if UBIFS contents extra checks are enabled 59 * @tst_rcvry: if UBIFS recovery testing mode enabled 60 * 61 * @dfs_dir_name: name of debugfs directory containing this file-system's files 62 * @dfs_dir: direntry object of the file-system debugfs directory 63 * @dfs_dump_lprops: "dump lprops" debugfs knob 64 * @dfs_dump_budg: "dump budgeting information" debugfs knob 65 * @dfs_dump_tnc: "dump TNC" debugfs knob 66 * @dfs_chk_gen: debugfs knob to enable UBIFS general extra checks 67 * @dfs_chk_index: debugfs knob to enable UBIFS index extra checks 68 * @dfs_chk_orph: debugfs knob to enable UBIFS orphans extra checks 69 * @dfs_chk_lprops: debugfs knob to enable UBIFS LEP properties extra checks 70 * @dfs_chk_fs: debugfs knob to enable UBIFS contents extra checks 71 * @dfs_tst_rcvry: debugfs knob to enable UBIFS recovery testing 72 * @dfs_ro_error: debugfs knob to switch UBIFS to R/O mode (different to 73 * re-mounting to R/O mode because it does not flush any buffers 74 * and UBIFS just starts returning -EROFS on all write 75 * operations) 76 */ 77 struct ubifs_debug_info { 78 struct ubifs_zbranch old_zroot; 79 int old_zroot_level; 80 unsigned long long old_zroot_sqnum; 81 82 int pc_happened; 83 int pc_delay; 84 unsigned long pc_timeout; 85 unsigned int pc_cnt; 86 unsigned int pc_cnt_max; 87 88 long long chk_lpt_sz; 89 long long chk_lpt_sz2; 90 long long chk_lpt_wastage; 91 int chk_lpt_lebs; 92 int new_nhead_offs; 93 int new_ihead_lnum; 94 int new_ihead_offs; 95 96 struct ubifs_lp_stats saved_lst; 97 struct ubifs_budg_info saved_bi; 98 long long saved_free; 99 int saved_idx_gc_cnt; 100 101 unsigned int chk_gen:1; 102 unsigned int chk_index:1; 103 unsigned int chk_orph:1; 104 unsigned int chk_lprops:1; 105 unsigned int chk_fs:1; 106 unsigned int tst_rcvry:1; 107 108 char dfs_dir_name[UBIFS_DFS_DIR_LEN + 1]; 109 struct dentry *dfs_dir; 110 struct dentry *dfs_dump_lprops; 111 struct dentry *dfs_dump_budg; 112 struct dentry *dfs_dump_tnc; 113 struct dentry *dfs_chk_gen; 114 struct dentry *dfs_chk_index; 115 struct dentry *dfs_chk_orph; 116 struct dentry *dfs_chk_lprops; 117 struct dentry *dfs_chk_fs; 118 struct dentry *dfs_tst_rcvry; 119 struct dentry *dfs_ro_error; 120 }; 121 122 /** 123 * ubifs_global_debug_info - global (not per-FS) UBIFS debugging information. 124 * 125 * @chk_gen: if general extra checks are enabled 126 * @chk_index: if index xtra checks are enabled 127 * @chk_orph: if orphans extra checks are enabled 128 * @chk_lprops: if lprops extra checks are enabled 129 * @chk_fs: if UBIFS contents extra checks are enabled 130 * @tst_rcvry: if UBIFS recovery testing mode enabled 131 */ 132 struct ubifs_global_debug_info { 133 unsigned int chk_gen:1; 134 unsigned int chk_index:1; 135 unsigned int chk_orph:1; 136 unsigned int chk_lprops:1; 137 unsigned int chk_fs:1; 138 unsigned int tst_rcvry:1; 139 }; 140 141 #ifndef __UBOOT__ 142 #define ubifs_assert(expr) do { \ 143 if (unlikely(!(expr))) { \ 144 pr_crit("UBIFS assert failed in %s at %u (pid %d)\n", \ 145 __func__, __LINE__, current->pid); \ 146 dump_stack(); \ 147 } \ 148 } while (0) 149 150 #define ubifs_assert_cmt_locked(c) do { \ 151 if (unlikely(down_write_trylock(&(c)->commit_sem))) { \ 152 up_write(&(c)->commit_sem); \ 153 pr_crit("commit lock is not locked!\n"); \ 154 ubifs_assert(0); \ 155 } \ 156 } while (0) 157 158 #define ubifs_dbg_msg(type, fmt, ...) \ 159 pr_debug("UBIFS DBG " type " (pid %d): " fmt "\n", current->pid, \ 160 ##__VA_ARGS__) 161 162 #define DBG_KEY_BUF_LEN 48 163 #define ubifs_dbg_msg_key(type, key, fmt, ...) do { \ 164 char __tmp_key_buf[DBG_KEY_BUF_LEN]; \ 165 pr_debug("UBIFS DBG " type " (pid %d): " fmt "%s\n", current->pid, \ 166 ##__VA_ARGS__, \ 167 dbg_snprintf_key(c, key, __tmp_key_buf, DBG_KEY_BUF_LEN)); \ 168 } while (0) 169 #else 170 #define ubifs_assert(expr) do { \ 171 if (unlikely(!(expr))) { \ 172 pr_crit("UBIFS assert failed in %s at %u\n", \ 173 __func__, __LINE__); \ 174 dump_stack(); \ 175 } \ 176 } while (0) 177 178 #define ubifs_assert_cmt_locked(c) do { \ 179 if (unlikely(down_write_trylock(&(c)->commit_sem))) { \ 180 up_write(&(c)->commit_sem); \ 181 pr_crit("commit lock is not locked!\n"); \ 182 ubifs_assert(0); \ 183 } \ 184 } while (0) 185 186 #define ubifs_dbg_msg(type, fmt, ...) \ 187 pr_debug("UBIFS DBG " type ": " fmt "\n", \ 188 ##__VA_ARGS__) 189 190 #define DBG_KEY_BUF_LEN 48 191 #if defined CONFIG_MTD_DEBUG 192 #define ubifs_dbg_msg_key(type, key, fmt, ...) do { \ 193 char __tmp_key_buf[DBG_KEY_BUF_LEN]; \ 194 pr_debug("UBIFS DBG " type ": " fmt "%s\n", \ 195 ##__VA_ARGS__, \ 196 dbg_snprintf_key(c, key, __tmp_key_buf, DBG_KEY_BUF_LEN)); \ 197 } while (0) 198 #else 199 #define ubifs_dbg_msg_key(type, key, fmt, ...) do { \ 200 pr_debug("UBIFS DBG\n"); \ 201 } while (0) 202 203 #endif 204 205 #endif 206 207 /* General messages */ 208 #define dbg_gen(fmt, ...) ubifs_dbg_msg("gen", fmt, ##__VA_ARGS__) 209 /* Additional journal messages */ 210 #define dbg_jnl(fmt, ...) ubifs_dbg_msg("jnl", fmt, ##__VA_ARGS__) 211 #define dbg_jnlk(key, fmt, ...) \ 212 ubifs_dbg_msg_key("jnl", key, fmt, ##__VA_ARGS__) 213 /* Additional TNC messages */ 214 #define dbg_tnc(fmt, ...) ubifs_dbg_msg("tnc", fmt, ##__VA_ARGS__) 215 #define dbg_tnck(key, fmt, ...) \ 216 ubifs_dbg_msg_key("tnc", key, fmt, ##__VA_ARGS__) 217 /* Additional lprops messages */ 218 #define dbg_lp(fmt, ...) ubifs_dbg_msg("lp", fmt, ##__VA_ARGS__) 219 /* Additional LEB find messages */ 220 #define dbg_find(fmt, ...) ubifs_dbg_msg("find", fmt, ##__VA_ARGS__) 221 /* Additional mount messages */ 222 #define dbg_mnt(fmt, ...) ubifs_dbg_msg("mnt", fmt, ##__VA_ARGS__) 223 #define dbg_mntk(key, fmt, ...) \ 224 ubifs_dbg_msg_key("mnt", key, fmt, ##__VA_ARGS__) 225 /* Additional I/O messages */ 226 #define dbg_io(fmt, ...) ubifs_dbg_msg("io", fmt, ##__VA_ARGS__) 227 /* Additional commit messages */ 228 #define dbg_cmt(fmt, ...) ubifs_dbg_msg("cmt", fmt, ##__VA_ARGS__) 229 /* Additional budgeting messages */ 230 #define dbg_budg(fmt, ...) ubifs_dbg_msg("budg", fmt, ##__VA_ARGS__) 231 /* Additional log messages */ 232 #define dbg_log(fmt, ...) ubifs_dbg_msg("log", fmt, ##__VA_ARGS__) 233 /* Additional gc messages */ 234 #define dbg_gc(fmt, ...) ubifs_dbg_msg("gc", fmt, ##__VA_ARGS__) 235 /* Additional scan messages */ 236 #define dbg_scan(fmt, ...) ubifs_dbg_msg("scan", fmt, ##__VA_ARGS__) 237 /* Additional recovery messages */ 238 #define dbg_rcvry(fmt, ...) ubifs_dbg_msg("rcvry", fmt, ##__VA_ARGS__) 239 240 #ifndef __UBOOT__ 241 extern struct ubifs_global_debug_info ubifs_dbg; 242 243 static inline int dbg_is_chk_gen(const struct ubifs_info *c) 244 { 245 return !!(ubifs_dbg.chk_gen || c->dbg->chk_gen); 246 } 247 static inline int dbg_is_chk_index(const struct ubifs_info *c) 248 { 249 return !!(ubifs_dbg.chk_index || c->dbg->chk_index); 250 } 251 static inline int dbg_is_chk_orph(const struct ubifs_info *c) 252 { 253 return !!(ubifs_dbg.chk_orph || c->dbg->chk_orph); 254 } 255 static inline int dbg_is_chk_lprops(const struct ubifs_info *c) 256 { 257 return !!(ubifs_dbg.chk_lprops || c->dbg->chk_lprops); 258 } 259 static inline int dbg_is_chk_fs(const struct ubifs_info *c) 260 { 261 return !!(ubifs_dbg.chk_fs || c->dbg->chk_fs); 262 } 263 static inline int dbg_is_tst_rcvry(const struct ubifs_info *c) 264 { 265 return !!(ubifs_dbg.tst_rcvry || c->dbg->tst_rcvry); 266 } 267 static inline int dbg_is_power_cut(const struct ubifs_info *c) 268 { 269 return !!c->dbg->pc_happened; 270 } 271 272 int ubifs_debugging_init(struct ubifs_info *c); 273 void ubifs_debugging_exit(struct ubifs_info *c); 274 #else 275 static inline int dbg_is_chk_gen(const struct ubifs_info *c) 276 { 277 return 0; 278 } 279 static inline int dbg_is_chk_index(const struct ubifs_info *c) 280 { 281 return 0; 282 } 283 static inline int dbg_is_chk_orph(const struct ubifs_info *c) 284 { 285 return 0; 286 } 287 static inline int dbg_is_chk_lprops(const struct ubifs_info *c) 288 { 289 return 0; 290 } 291 static inline int dbg_is_chk_fs(const struct ubifs_info *c) 292 { 293 return 0; 294 } 295 static inline int dbg_is_tst_rcvry(const struct ubifs_info *c) 296 { 297 return 0; 298 } 299 static inline int dbg_is_power_cut(const struct ubifs_info *c) 300 { 301 return 0; 302 } 303 304 int ubifs_debugging_init(struct ubifs_info *c); 305 void ubifs_debugging_exit(struct ubifs_info *c); 306 307 #endif 308 309 /* Dump functions */ 310 const char *dbg_ntype(int type); 311 const char *dbg_cstate(int cmt_state); 312 const char *dbg_jhead(int jhead); 313 const char *dbg_get_key_dump(const struct ubifs_info *c, 314 const union ubifs_key *key); 315 const char *dbg_snprintf_key(const struct ubifs_info *c, 316 const union ubifs_key *key, char *buffer, int len); 317 void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode); 318 void ubifs_dump_node(const struct ubifs_info *c, const void *node); 319 void ubifs_dump_budget_req(const struct ubifs_budget_req *req); 320 void ubifs_dump_lstats(const struct ubifs_lp_stats *lst); 321 void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi); 322 void ubifs_dump_lprop(const struct ubifs_info *c, 323 const struct ubifs_lprops *lp); 324 void ubifs_dump_lprops(struct ubifs_info *c); 325 void ubifs_dump_lpt_info(struct ubifs_info *c); 326 void ubifs_dump_leb(const struct ubifs_info *c, int lnum); 327 void ubifs_dump_sleb(const struct ubifs_info *c, 328 const struct ubifs_scan_leb *sleb, int offs); 329 void ubifs_dump_znode(const struct ubifs_info *c, 330 const struct ubifs_znode *znode); 331 void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, 332 int cat); 333 void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, 334 struct ubifs_nnode *parent, int iip); 335 void ubifs_dump_tnc(struct ubifs_info *c); 336 void ubifs_dump_index(struct ubifs_info *c); 337 void ubifs_dump_lpt_lebs(const struct ubifs_info *c); 338 339 int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb, 340 dbg_znode_callback znode_cb, void *priv); 341 342 /* Checking functions */ 343 void dbg_save_space_info(struct ubifs_info *c); 344 int dbg_check_space_info(struct ubifs_info *c); 345 int dbg_check_lprops(struct ubifs_info *c); 346 int dbg_old_index_check_init(struct ubifs_info *c, struct ubifs_zbranch *zroot); 347 int dbg_check_old_index(struct ubifs_info *c, struct ubifs_zbranch *zroot); 348 int dbg_check_cats(struct ubifs_info *c); 349 int dbg_check_ltab(struct ubifs_info *c); 350 int dbg_chk_lpt_free_spc(struct ubifs_info *c); 351 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len); 352 int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode); 353 int dbg_check_dir(struct ubifs_info *c, const struct inode *dir); 354 int dbg_check_tnc(struct ubifs_info *c, int extra); 355 int dbg_check_idx_size(struct ubifs_info *c, long long idx_size); 356 int dbg_check_filesystem(struct ubifs_info *c); 357 void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat, 358 int add_pos); 359 int dbg_check_lpt_nodes(struct ubifs_info *c, struct ubifs_cnode *cnode, 360 int row, int col); 361 int dbg_check_inode_size(struct ubifs_info *c, const struct inode *inode, 362 loff_t size); 363 int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head); 364 int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head); 365 366 int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf, int offs, 367 int len); 368 int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf, int len); 369 int dbg_leb_unmap(struct ubifs_info *c, int lnum); 370 int dbg_leb_map(struct ubifs_info *c, int lnum); 371 372 /* Debugfs-related stuff */ 373 int dbg_debugfs_init(void); 374 void dbg_debugfs_exit(void); 375 int dbg_debugfs_init_fs(struct ubifs_info *c); 376 void dbg_debugfs_exit_fs(struct ubifs_info *c); 377 378 #endif /* !__UBIFS_DEBUG_H__ */ 379