diff options
Diffstat (limited to 'fs/ubifs/debug.c')
| -rw-r--r-- | fs/ubifs/debug.c | 1813 | 
1 files changed, 1030 insertions, 783 deletions
diff --git a/fs/ubifs/debug.c b/fs/ubifs/debug.c index 0bee4dbffc3..177b0152fef 100644 --- a/fs/ubifs/debug.c +++ b/fs/ubifs/debug.c @@ -27,33 +27,14 @@   * various local functions of those subsystems.   */ -#define UBIFS_DBG_PRESERVE_UBI - -#include "ubifs.h"  #include <linux/module.h> -#include <linux/moduleparam.h>  #include <linux/debugfs.h>  #include <linux/math64.h> -#include <linux/slab.h> - -#ifdef CONFIG_UBIFS_FS_DEBUG - -DEFINE_SPINLOCK(dbg_lock); - -static char dbg_key_buf0[128]; -static char dbg_key_buf1[128]; - -unsigned int ubifs_msg_flags = UBIFS_MSG_FLAGS_DEFAULT; -unsigned int ubifs_chk_flags = UBIFS_CHK_FLAGS_DEFAULT; -unsigned int ubifs_tst_flags; - -module_param_named(debug_msgs, ubifs_msg_flags, uint, S_IRUGO | S_IWUSR); -module_param_named(debug_chks, ubifs_chk_flags, uint, S_IRUGO | S_IWUSR); -module_param_named(debug_tsts, ubifs_tst_flags, uint, S_IRUGO | S_IWUSR); +#include <linux/uaccess.h> +#include <linux/random.h> +#include "ubifs.h" -MODULE_PARM_DESC(debug_msgs, "Debug message type flags"); -MODULE_PARM_DESC(debug_chks, "Debug check flags"); -MODULE_PARM_DESC(debug_tsts, "Debug special test flags"); +static DEFINE_SPINLOCK(dbg_lock);  static const char *get_key_fmt(int fmt)  { @@ -95,8 +76,30 @@ static const char *get_key_type(int type)  	}  } -static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key, -			char *buffer) +static const char *get_dent_type(int type) +{ +	switch (type) { +	case UBIFS_ITYPE_REG: +		return "file"; +	case UBIFS_ITYPE_DIR: +		return "dir"; +	case UBIFS_ITYPE_LNK: +		return "symlink"; +	case UBIFS_ITYPE_BLK: +		return "blkdev"; +	case UBIFS_ITYPE_CHR: +		return "char dev"; +	case UBIFS_ITYPE_FIFO: +		return "fifo"; +	case UBIFS_ITYPE_SOCK: +		return "socket"; +	default: +		return "unknown/invalid type"; +	} +} + +const char *dbg_snprintf_key(const struct ubifs_info *c, +			     const union ubifs_key *key, char *buffer, int len)  {  	char *p = buffer;  	int type = key_type(c, key); @@ -104,45 +107,34 @@ static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,  	if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {  		switch (type) {  		case UBIFS_INO_KEY: -			sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key), -			       get_key_type(type)); +			len -= snprintf(p, len, "(%lu, %s)", +					(unsigned long)key_inum(c, key), +					get_key_type(type));  			break;  		case UBIFS_DENT_KEY:  		case UBIFS_XENT_KEY: -			sprintf(p, "(%lu, %s, %#08x)", -				(unsigned long)key_inum(c, key), -				get_key_type(type), key_hash(c, key)); +			len -= snprintf(p, len, "(%lu, %s, %#08x)", +					(unsigned long)key_inum(c, key), +					get_key_type(type), key_hash(c, key));  			break;  		case UBIFS_DATA_KEY: -			sprintf(p, "(%lu, %s, %u)", -				(unsigned long)key_inum(c, key), -				get_key_type(type), key_block(c, key)); +			len -= snprintf(p, len, "(%lu, %s, %u)", +					(unsigned long)key_inum(c, key), +					get_key_type(type), key_block(c, key));  			break;  		case UBIFS_TRUN_KEY: -			sprintf(p, "(%lu, %s)", -				(unsigned long)key_inum(c, key), -				get_key_type(type)); +			len -= snprintf(p, len, "(%lu, %s)", +					(unsigned long)key_inum(c, key), +					get_key_type(type));  			break;  		default: -			sprintf(p, "(bad key type: %#08x, %#08x)", -				key->u32[0], key->u32[1]); +			len -= snprintf(p, len, "(bad key type: %#08x, %#08x)", +					key->u32[0], key->u32[1]);  		}  	} else -		sprintf(p, "bad key format %d", c->key_fmt); -} - -const char *dbg_key_str0(const struct ubifs_info *c, const union ubifs_key *key) -{ -	/* dbg_lock must be held */ -	sprintf_key(c, key, dbg_key_buf0); -	return dbg_key_buf0; -} - -const char *dbg_key_str1(const struct ubifs_info *c, const union ubifs_key *key) -{ -	/* dbg_lock must be held */ -	sprintf_key(c, key, dbg_key_buf1); -	return dbg_key_buf1; +		len -= snprintf(p, len, "bad key format %d", c->key_fmt); +	ubifs_assert(len > 0); +	return p;  }  const char *dbg_ntype(int type) @@ -227,68 +219,96 @@ const char *dbg_jhead(int jhead)  static void dump_ch(const struct ubifs_ch *ch)  { -	printk(KERN_DEBUG "\tmagic          %#x\n", le32_to_cpu(ch->magic)); -	printk(KERN_DEBUG "\tcrc            %#x\n", le32_to_cpu(ch->crc)); -	printk(KERN_DEBUG "\tnode_type      %d (%s)\n", ch->node_type, +	pr_err("\tmagic          %#x\n", le32_to_cpu(ch->magic)); +	pr_err("\tcrc            %#x\n", le32_to_cpu(ch->crc)); +	pr_err("\tnode_type      %d (%s)\n", ch->node_type,  	       dbg_ntype(ch->node_type)); -	printk(KERN_DEBUG "\tgroup_type     %d (%s)\n", ch->group_type, +	pr_err("\tgroup_type     %d (%s)\n", ch->group_type,  	       dbg_gtype(ch->group_type)); -	printk(KERN_DEBUG "\tsqnum          %llu\n", +	pr_err("\tsqnum          %llu\n",  	       (unsigned long long)le64_to_cpu(ch->sqnum)); -	printk(KERN_DEBUG "\tlen            %u\n", le32_to_cpu(ch->len)); +	pr_err("\tlen            %u\n", le32_to_cpu(ch->len));  } -void dbg_dump_inode(const struct ubifs_info *c, const struct inode *inode) +void ubifs_dump_inode(struct ubifs_info *c, const struct inode *inode)  {  	const struct ubifs_inode *ui = ubifs_inode(inode); +	struct qstr nm = { .name = NULL }; +	union ubifs_key key; +	struct ubifs_dent_node *dent, *pdent = NULL; +	int count = 2; -	printk(KERN_DEBUG "Dump in-memory inode:"); -	printk(KERN_DEBUG "\tinode          %lu\n", inode->i_ino); -	printk(KERN_DEBUG "\tsize           %llu\n", +	pr_err("Dump in-memory inode:"); +	pr_err("\tinode          %lu\n", inode->i_ino); +	pr_err("\tsize           %llu\n",  	       (unsigned long long)i_size_read(inode)); -	printk(KERN_DEBUG "\tnlink          %u\n", inode->i_nlink); -	printk(KERN_DEBUG "\tuid            %u\n", (unsigned int)inode->i_uid); -	printk(KERN_DEBUG "\tgid            %u\n", (unsigned int)inode->i_gid); -	printk(KERN_DEBUG "\tatime          %u.%u\n", +	pr_err("\tnlink          %u\n", inode->i_nlink); +	pr_err("\tuid            %u\n", (unsigned int)i_uid_read(inode)); +	pr_err("\tgid            %u\n", (unsigned int)i_gid_read(inode)); +	pr_err("\tatime          %u.%u\n",  	       (unsigned int)inode->i_atime.tv_sec,  	       (unsigned int)inode->i_atime.tv_nsec); -	printk(KERN_DEBUG "\tmtime          %u.%u\n", +	pr_err("\tmtime          %u.%u\n",  	       (unsigned int)inode->i_mtime.tv_sec,  	       (unsigned int)inode->i_mtime.tv_nsec); -	printk(KERN_DEBUG "\tctime          %u.%u\n", +	pr_err("\tctime          %u.%u\n",  	       (unsigned int)inode->i_ctime.tv_sec,  	       (unsigned int)inode->i_ctime.tv_nsec); -	printk(KERN_DEBUG "\tcreat_sqnum    %llu\n", ui->creat_sqnum); -	printk(KERN_DEBUG "\txattr_size     %u\n", ui->xattr_size); -	printk(KERN_DEBUG "\txattr_cnt      %u\n", ui->xattr_cnt); -	printk(KERN_DEBUG "\txattr_names    %u\n", ui->xattr_names); -	printk(KERN_DEBUG "\tdirty          %u\n", ui->dirty); -	printk(KERN_DEBUG "\txattr          %u\n", ui->xattr); -	printk(KERN_DEBUG "\tbulk_read      %u\n", ui->xattr); -	printk(KERN_DEBUG "\tsynced_i_size  %llu\n", +	pr_err("\tcreat_sqnum    %llu\n", ui->creat_sqnum); +	pr_err("\txattr_size     %u\n", ui->xattr_size); +	pr_err("\txattr_cnt      %u\n", ui->xattr_cnt); +	pr_err("\txattr_names    %u\n", ui->xattr_names); +	pr_err("\tdirty          %u\n", ui->dirty); +	pr_err("\txattr          %u\n", ui->xattr); +	pr_err("\tbulk_read      %u\n", ui->xattr); +	pr_err("\tsynced_i_size  %llu\n",  	       (unsigned long long)ui->synced_i_size); -	printk(KERN_DEBUG "\tui_size        %llu\n", +	pr_err("\tui_size        %llu\n",  	       (unsigned long long)ui->ui_size); -	printk(KERN_DEBUG "\tflags          %d\n", ui->flags); -	printk(KERN_DEBUG "\tcompr_type     %d\n", ui->compr_type); -	printk(KERN_DEBUG "\tlast_page_read %lu\n", ui->last_page_read); -	printk(KERN_DEBUG "\tread_in_a_row  %lu\n", ui->read_in_a_row); -	printk(KERN_DEBUG "\tdata_len       %d\n", ui->data_len); +	pr_err("\tflags          %d\n", ui->flags); +	pr_err("\tcompr_type     %d\n", ui->compr_type); +	pr_err("\tlast_page_read %lu\n", ui->last_page_read); +	pr_err("\tread_in_a_row  %lu\n", ui->read_in_a_row); +	pr_err("\tdata_len       %d\n", ui->data_len); + +	if (!S_ISDIR(inode->i_mode)) +		return; + +	pr_err("List of directory entries:\n"); +	ubifs_assert(!mutex_is_locked(&c->tnc_mutex)); + +	lowest_dent_key(c, &key, inode->i_ino); +	while (1) { +		dent = ubifs_tnc_next_ent(c, &key, &nm); +		if (IS_ERR(dent)) { +			if (PTR_ERR(dent) != -ENOENT) +				pr_err("error %ld\n", PTR_ERR(dent)); +			break; +		} + +		pr_err("\t%d: %s (%s)\n", +		       count++, dent->name, get_dent_type(dent->type)); + +		nm.name = dent->name; +		nm.len = le16_to_cpu(dent->nlen); +		kfree(pdent); +		pdent = dent; +		key_read(c, &dent->key, &key); +	} +	kfree(pdent);  } -void dbg_dump_node(const struct ubifs_info *c, const void *node) +void ubifs_dump_node(const struct ubifs_info *c, const void *node)  {  	int i, n;  	union ubifs_key key;  	const struct ubifs_ch *ch = node; - -	if (dbg_failure_mode) -		return; +	char key_buf[DBG_KEY_BUF_LEN];  	/* If the magic is incorrect, just hexdump the first bytes */  	if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) { -		printk(KERN_DEBUG "Not a node, first %zu bytes:", UBIFS_CH_SZ); -		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, +		pr_err("Not a node, first %zu bytes:", UBIFS_CH_SZ); +		print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 32, 1,  			       (void *)node, UBIFS_CH_SZ, 1);  		return;  	} @@ -301,8 +321,7 @@ void dbg_dump_node(const struct ubifs_info *c, const void *node)  	{  		const struct ubifs_pad_node *pad = node; -		printk(KERN_DEBUG "\tpad_len        %u\n", -		       le32_to_cpu(pad->pad_len)); +		pr_err("\tpad_len        %u\n", le32_to_cpu(pad->pad_len));  		break;  	}  	case UBIFS_SB_NODE: @@ -310,110 +329,77 @@ void dbg_dump_node(const struct ubifs_info *c, const void *node)  		const struct ubifs_sb_node *sup = node;  		unsigned int sup_flags = le32_to_cpu(sup->flags); -		printk(KERN_DEBUG "\tkey_hash       %d (%s)\n", +		pr_err("\tkey_hash       %d (%s)\n",  		       (int)sup->key_hash, get_key_hash(sup->key_hash)); -		printk(KERN_DEBUG "\tkey_fmt        %d (%s)\n", +		pr_err("\tkey_fmt        %d (%s)\n",  		       (int)sup->key_fmt, get_key_fmt(sup->key_fmt)); -		printk(KERN_DEBUG "\tflags          %#x\n", sup_flags); -		printk(KERN_DEBUG "\t  big_lpt      %u\n", +		pr_err("\tflags          %#x\n", sup_flags); +		pr_err("\t  big_lpt      %u\n",  		       !!(sup_flags & UBIFS_FLG_BIGLPT)); -		printk(KERN_DEBUG "\tmin_io_size    %u\n", -		       le32_to_cpu(sup->min_io_size)); -		printk(KERN_DEBUG "\tleb_size       %u\n", -		       le32_to_cpu(sup->leb_size)); -		printk(KERN_DEBUG "\tleb_cnt        %u\n", -		       le32_to_cpu(sup->leb_cnt)); -		printk(KERN_DEBUG "\tmax_leb_cnt    %u\n", -		       le32_to_cpu(sup->max_leb_cnt)); -		printk(KERN_DEBUG "\tmax_bud_bytes  %llu\n", +		pr_err("\t  space_fixup  %u\n", +		       !!(sup_flags & UBIFS_FLG_SPACE_FIXUP)); +		pr_err("\tmin_io_size    %u\n", le32_to_cpu(sup->min_io_size)); +		pr_err("\tleb_size       %u\n", le32_to_cpu(sup->leb_size)); +		pr_err("\tleb_cnt        %u\n", le32_to_cpu(sup->leb_cnt)); +		pr_err("\tmax_leb_cnt    %u\n", le32_to_cpu(sup->max_leb_cnt)); +		pr_err("\tmax_bud_bytes  %llu\n",  		       (unsigned long long)le64_to_cpu(sup->max_bud_bytes)); -		printk(KERN_DEBUG "\tlog_lebs       %u\n", -		       le32_to_cpu(sup->log_lebs)); -		printk(KERN_DEBUG "\tlpt_lebs       %u\n", -		       le32_to_cpu(sup->lpt_lebs)); -		printk(KERN_DEBUG "\torph_lebs      %u\n", -		       le32_to_cpu(sup->orph_lebs)); -		printk(KERN_DEBUG "\tjhead_cnt      %u\n", -		       le32_to_cpu(sup->jhead_cnt)); -		printk(KERN_DEBUG "\tfanout         %u\n", -		       le32_to_cpu(sup->fanout)); -		printk(KERN_DEBUG "\tlsave_cnt      %u\n", -		       le32_to_cpu(sup->lsave_cnt)); -		printk(KERN_DEBUG "\tdefault_compr  %u\n", +		pr_err("\tlog_lebs       %u\n", le32_to_cpu(sup->log_lebs)); +		pr_err("\tlpt_lebs       %u\n", le32_to_cpu(sup->lpt_lebs)); +		pr_err("\torph_lebs      %u\n", le32_to_cpu(sup->orph_lebs)); +		pr_err("\tjhead_cnt      %u\n", le32_to_cpu(sup->jhead_cnt)); +		pr_err("\tfanout         %u\n", le32_to_cpu(sup->fanout)); +		pr_err("\tlsave_cnt      %u\n", le32_to_cpu(sup->lsave_cnt)); +		pr_err("\tdefault_compr  %u\n",  		       (int)le16_to_cpu(sup->default_compr)); -		printk(KERN_DEBUG "\trp_size        %llu\n", +		pr_err("\trp_size        %llu\n",  		       (unsigned long long)le64_to_cpu(sup->rp_size)); -		printk(KERN_DEBUG "\trp_uid         %u\n", -		       le32_to_cpu(sup->rp_uid)); -		printk(KERN_DEBUG "\trp_gid         %u\n", -		       le32_to_cpu(sup->rp_gid)); -		printk(KERN_DEBUG "\tfmt_version    %u\n", -		       le32_to_cpu(sup->fmt_version)); -		printk(KERN_DEBUG "\ttime_gran      %u\n", -		       le32_to_cpu(sup->time_gran)); -		printk(KERN_DEBUG "\tUUID           %pUB\n", -		       sup->uuid); +		pr_err("\trp_uid         %u\n", le32_to_cpu(sup->rp_uid)); +		pr_err("\trp_gid         %u\n", le32_to_cpu(sup->rp_gid)); +		pr_err("\tfmt_version    %u\n", le32_to_cpu(sup->fmt_version)); +		pr_err("\ttime_gran      %u\n", le32_to_cpu(sup->time_gran)); +		pr_err("\tUUID           %pUB\n", sup->uuid);  		break;  	}  	case UBIFS_MST_NODE:  	{  		const struct ubifs_mst_node *mst = node; -		printk(KERN_DEBUG "\thighest_inum   %llu\n", +		pr_err("\thighest_inum   %llu\n",  		       (unsigned long long)le64_to_cpu(mst->highest_inum)); -		printk(KERN_DEBUG "\tcommit number  %llu\n", +		pr_err("\tcommit number  %llu\n",  		       (unsigned long long)le64_to_cpu(mst->cmt_no)); -		printk(KERN_DEBUG "\tflags          %#x\n", -		       le32_to_cpu(mst->flags)); -		printk(KERN_DEBUG "\tlog_lnum       %u\n", -		       le32_to_cpu(mst->log_lnum)); -		printk(KERN_DEBUG "\troot_lnum      %u\n", -		       le32_to_cpu(mst->root_lnum)); -		printk(KERN_DEBUG "\troot_offs      %u\n", -		       le32_to_cpu(mst->root_offs)); -		printk(KERN_DEBUG "\troot_len       %u\n", -		       le32_to_cpu(mst->root_len)); -		printk(KERN_DEBUG "\tgc_lnum        %u\n", -		       le32_to_cpu(mst->gc_lnum)); -		printk(KERN_DEBUG "\tihead_lnum     %u\n", -		       le32_to_cpu(mst->ihead_lnum)); -		printk(KERN_DEBUG "\tihead_offs     %u\n", -		       le32_to_cpu(mst->ihead_offs)); -		printk(KERN_DEBUG "\tindex_size     %llu\n", +		pr_err("\tflags          %#x\n", le32_to_cpu(mst->flags)); +		pr_err("\tlog_lnum       %u\n", le32_to_cpu(mst->log_lnum)); +		pr_err("\troot_lnum      %u\n", le32_to_cpu(mst->root_lnum)); +		pr_err("\troot_offs      %u\n", le32_to_cpu(mst->root_offs)); +		pr_err("\troot_len       %u\n", le32_to_cpu(mst->root_len)); +		pr_err("\tgc_lnum        %u\n", le32_to_cpu(mst->gc_lnum)); +		pr_err("\tihead_lnum     %u\n", le32_to_cpu(mst->ihead_lnum)); +		pr_err("\tihead_offs     %u\n", le32_to_cpu(mst->ihead_offs)); +		pr_err("\tindex_size     %llu\n",  		       (unsigned long long)le64_to_cpu(mst->index_size)); -		printk(KERN_DEBUG "\tlpt_lnum       %u\n", -		       le32_to_cpu(mst->lpt_lnum)); -		printk(KERN_DEBUG "\tlpt_offs       %u\n", -		       le32_to_cpu(mst->lpt_offs)); -		printk(KERN_DEBUG "\tnhead_lnum     %u\n", -		       le32_to_cpu(mst->nhead_lnum)); -		printk(KERN_DEBUG "\tnhead_offs     %u\n", -		       le32_to_cpu(mst->nhead_offs)); -		printk(KERN_DEBUG "\tltab_lnum      %u\n", -		       le32_to_cpu(mst->ltab_lnum)); -		printk(KERN_DEBUG "\tltab_offs      %u\n", -		       le32_to_cpu(mst->ltab_offs)); -		printk(KERN_DEBUG "\tlsave_lnum     %u\n", -		       le32_to_cpu(mst->lsave_lnum)); -		printk(KERN_DEBUG "\tlsave_offs     %u\n", -		       le32_to_cpu(mst->lsave_offs)); -		printk(KERN_DEBUG "\tlscan_lnum     %u\n", -		       le32_to_cpu(mst->lscan_lnum)); -		printk(KERN_DEBUG "\tleb_cnt        %u\n", -		       le32_to_cpu(mst->leb_cnt)); -		printk(KERN_DEBUG "\tempty_lebs     %u\n", -		       le32_to_cpu(mst->empty_lebs)); -		printk(KERN_DEBUG "\tidx_lebs       %u\n", -		       le32_to_cpu(mst->idx_lebs)); -		printk(KERN_DEBUG "\ttotal_free     %llu\n", +		pr_err("\tlpt_lnum       %u\n", le32_to_cpu(mst->lpt_lnum)); +		pr_err("\tlpt_offs       %u\n", le32_to_cpu(mst->lpt_offs)); +		pr_err("\tnhead_lnum     %u\n", le32_to_cpu(mst->nhead_lnum)); +		pr_err("\tnhead_offs     %u\n", le32_to_cpu(mst->nhead_offs)); +		pr_err("\tltab_lnum      %u\n", le32_to_cpu(mst->ltab_lnum)); +		pr_err("\tltab_offs      %u\n", le32_to_cpu(mst->ltab_offs)); +		pr_err("\tlsave_lnum     %u\n", le32_to_cpu(mst->lsave_lnum)); +		pr_err("\tlsave_offs     %u\n", le32_to_cpu(mst->lsave_offs)); +		pr_err("\tlscan_lnum     %u\n", le32_to_cpu(mst->lscan_lnum)); +		pr_err("\tleb_cnt        %u\n", le32_to_cpu(mst->leb_cnt)); +		pr_err("\tempty_lebs     %u\n", le32_to_cpu(mst->empty_lebs)); +		pr_err("\tidx_lebs       %u\n", le32_to_cpu(mst->idx_lebs)); +		pr_err("\ttotal_free     %llu\n",  		       (unsigned long long)le64_to_cpu(mst->total_free)); -		printk(KERN_DEBUG "\ttotal_dirty    %llu\n", +		pr_err("\ttotal_dirty    %llu\n",  		       (unsigned long long)le64_to_cpu(mst->total_dirty)); -		printk(KERN_DEBUG "\ttotal_used     %llu\n", +		pr_err("\ttotal_used     %llu\n",  		       (unsigned long long)le64_to_cpu(mst->total_used)); -		printk(KERN_DEBUG "\ttotal_dead     %llu\n", +		pr_err("\ttotal_dead     %llu\n",  		       (unsigned long long)le64_to_cpu(mst->total_dead)); -		printk(KERN_DEBUG "\ttotal_dark     %llu\n", +		pr_err("\ttotal_dark     %llu\n",  		       (unsigned long long)le64_to_cpu(mst->total_dark));  		break;  	} @@ -421,12 +407,9 @@ void dbg_dump_node(const struct ubifs_info *c, const void *node)  	{  		const struct ubifs_ref_node *ref = node; -		printk(KERN_DEBUG "\tlnum           %u\n", -		       le32_to_cpu(ref->lnum)); -		printk(KERN_DEBUG "\toffs           %u\n", -		       le32_to_cpu(ref->offs)); -		printk(KERN_DEBUG "\tjhead          %u\n", -		       le32_to_cpu(ref->jhead)); +		pr_err("\tlnum           %u\n", le32_to_cpu(ref->lnum)); +		pr_err("\toffs           %u\n", le32_to_cpu(ref->offs)); +		pr_err("\tjhead          %u\n", le32_to_cpu(ref->jhead));  		break;  	}  	case UBIFS_INO_NODE: @@ -434,40 +417,32 @@ void dbg_dump_node(const struct ubifs_info *c, const void *node)  		const struct ubifs_ino_node *ino = node;  		key_read(c, &ino->key, &key); -		printk(KERN_DEBUG "\tkey            %s\n", DBGKEY(&key)); -		printk(KERN_DEBUG "\tcreat_sqnum    %llu\n", +		pr_err("\tkey            %s\n", +		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN)); +		pr_err("\tcreat_sqnum    %llu\n",  		       (unsigned long long)le64_to_cpu(ino->creat_sqnum)); -		printk(KERN_DEBUG "\tsize           %llu\n", +		pr_err("\tsize           %llu\n",  		       (unsigned long long)le64_to_cpu(ino->size)); -		printk(KERN_DEBUG "\tnlink          %u\n", -		       le32_to_cpu(ino->nlink)); -		printk(KERN_DEBUG "\tatime          %lld.%u\n", +		pr_err("\tnlink          %u\n", le32_to_cpu(ino->nlink)); +		pr_err("\tatime          %lld.%u\n",  		       (long long)le64_to_cpu(ino->atime_sec),  		       le32_to_cpu(ino->atime_nsec)); -		printk(KERN_DEBUG "\tmtime          %lld.%u\n", +		pr_err("\tmtime          %lld.%u\n",  		       (long long)le64_to_cpu(ino->mtime_sec),  		       le32_to_cpu(ino->mtime_nsec)); -		printk(KERN_DEBUG "\tctime          %lld.%u\n", +		pr_err("\tctime          %lld.%u\n",  		       (long long)le64_to_cpu(ino->ctime_sec),  		       le32_to_cpu(ino->ctime_nsec)); -		printk(KERN_DEBUG "\tuid            %u\n", -		       le32_to_cpu(ino->uid)); -		printk(KERN_DEBUG "\tgid            %u\n", -		       le32_to_cpu(ino->gid)); -		printk(KERN_DEBUG "\tmode           %u\n", -		       le32_to_cpu(ino->mode)); -		printk(KERN_DEBUG "\tflags          %#x\n", -		       le32_to_cpu(ino->flags)); -		printk(KERN_DEBUG "\txattr_cnt      %u\n", -		       le32_to_cpu(ino->xattr_cnt)); -		printk(KERN_DEBUG "\txattr_size     %u\n", -		       le32_to_cpu(ino->xattr_size)); -		printk(KERN_DEBUG "\txattr_names    %u\n", -		       le32_to_cpu(ino->xattr_names)); -		printk(KERN_DEBUG "\tcompr_type     %#x\n", +		pr_err("\tuid            %u\n", le32_to_cpu(ino->uid)); +		pr_err("\tgid            %u\n", le32_to_cpu(ino->gid)); +		pr_err("\tmode           %u\n", le32_to_cpu(ino->mode)); +		pr_err("\tflags          %#x\n", le32_to_cpu(ino->flags)); +		pr_err("\txattr_cnt      %u\n", le32_to_cpu(ino->xattr_cnt)); +		pr_err("\txattr_size     %u\n", le32_to_cpu(ino->xattr_size)); +		pr_err("\txattr_names    %u\n", le32_to_cpu(ino->xattr_names)); +		pr_err("\tcompr_type     %#x\n",  		       (int)le16_to_cpu(ino->compr_type)); -		printk(KERN_DEBUG "\tdata len       %u\n", -		       le32_to_cpu(ino->data_len)); +		pr_err("\tdata len       %u\n", le32_to_cpu(ino->data_len));  		break;  	}  	case UBIFS_DENT_NODE: @@ -477,21 +452,21 @@ void dbg_dump_node(const struct ubifs_info *c, const void *node)  		int nlen = le16_to_cpu(dent->nlen);  		key_read(c, &dent->key, &key); -		printk(KERN_DEBUG "\tkey            %s\n", DBGKEY(&key)); -		printk(KERN_DEBUG "\tinum           %llu\n", +		pr_err("\tkey            %s\n", +		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN)); +		pr_err("\tinum           %llu\n",  		       (unsigned long long)le64_to_cpu(dent->inum)); -		printk(KERN_DEBUG "\ttype           %d\n", (int)dent->type); -		printk(KERN_DEBUG "\tnlen           %d\n", nlen); -		printk(KERN_DEBUG "\tname           "); +		pr_err("\ttype           %d\n", (int)dent->type); +		pr_err("\tnlen           %d\n", nlen); +		pr_err("\tname           ");  		if (nlen > UBIFS_MAX_NLEN) -			printk(KERN_DEBUG "(bad name length, not printing, " -					  "bad or corrupted node)"); +			pr_err("(bad name length, not printing, bad or corrupted node)");  		else {  			for (i = 0; i < nlen && dent->name[i]; i++) -				printk(KERN_CONT "%c", dent->name[i]); +				pr_cont("%c", dent->name[i]);  		} -		printk(KERN_CONT "\n"); +		pr_cont("\n");  		break;  	} @@ -501,15 +476,14 @@ void dbg_dump_node(const struct ubifs_info *c, const void *node)  		int dlen = le32_to_cpu(ch->len) - UBIFS_DATA_NODE_SZ;  		key_read(c, &dn->key, &key); -		printk(KERN_DEBUG "\tkey            %s\n", DBGKEY(&key)); -		printk(KERN_DEBUG "\tsize           %u\n", -		       le32_to_cpu(dn->size)); -		printk(KERN_DEBUG "\tcompr_typ      %d\n", +		pr_err("\tkey            %s\n", +		       dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN)); +		pr_err("\tsize           %u\n", le32_to_cpu(dn->size)); +		pr_err("\tcompr_typ      %d\n",  		       (int)le16_to_cpu(dn->compr_type)); -		printk(KERN_DEBUG "\tdata size      %d\n", -		       dlen); -		printk(KERN_DEBUG "\tdata:\n"); -		print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_OFFSET, 32, 1, +		pr_err("\tdata size      %d\n", dlen); +		pr_err("\tdata:\n"); +		print_hex_dump(KERN_ERR, "\t", DUMP_PREFIX_OFFSET, 32, 1,  			       (void *)&dn->data, dlen, 0);  		break;  	} @@ -517,11 +491,10 @@ void dbg_dump_node(const struct ubifs_info *c, const void *node)  	{  		const struct ubifs_trun_node *trun = node; -		printk(KERN_DEBUG "\tinum           %u\n", -		       le32_to_cpu(trun->inum)); -		printk(KERN_DEBUG "\told_size       %llu\n", +		pr_err("\tinum           %u\n", le32_to_cpu(trun->inum)); +		pr_err("\told_size       %llu\n",  		       (unsigned long long)le64_to_cpu(trun->old_size)); -		printk(KERN_DEBUG "\tnew_size       %llu\n", +		pr_err("\tnew_size       %llu\n",  		       (unsigned long long)le64_to_cpu(trun->new_size));  		break;  	} @@ -530,19 +503,20 @@ void dbg_dump_node(const struct ubifs_info *c, const void *node)  		const struct ubifs_idx_node *idx = node;  		n = le16_to_cpu(idx->child_cnt); -		printk(KERN_DEBUG "\tchild_cnt      %d\n", n); -		printk(KERN_DEBUG "\tlevel          %d\n", -		       (int)le16_to_cpu(idx->level)); -		printk(KERN_DEBUG "\tBranches:\n"); +		pr_err("\tchild_cnt      %d\n", n); +		pr_err("\tlevel          %d\n", (int)le16_to_cpu(idx->level)); +		pr_err("\tBranches:\n");  		for (i = 0; i < n && i < c->fanout - 1; i++) {  			const struct ubifs_branch *br;  			br = ubifs_idx_branch(c, idx, i);  			key_read(c, &br->key, &key); -			printk(KERN_DEBUG "\t%d: LEB %d:%d len %d key %s\n", +			pr_err("\t%d: LEB %d:%d len %d key %s\n",  			       i, le32_to_cpu(br->lnum), le32_to_cpu(br->offs), -			       le32_to_cpu(br->len), DBGKEY(&key)); +			       le32_to_cpu(br->len), +			       dbg_snprintf_key(c, &key, key_buf, +						DBG_KEY_BUF_LEN));  		}  		break;  	} @@ -552,57 +526,55 @@ void dbg_dump_node(const struct ubifs_info *c, const void *node)  	{  		const struct ubifs_orph_node *orph = node; -		printk(KERN_DEBUG "\tcommit number  %llu\n", +		pr_err("\tcommit number  %llu\n",  		       (unsigned long long)  				le64_to_cpu(orph->cmt_no) & LLONG_MAX); -		printk(KERN_DEBUG "\tlast node flag %llu\n", +		pr_err("\tlast node flag %llu\n",  		       (unsigned long long)(le64_to_cpu(orph->cmt_no)) >> 63);  		n = (le32_to_cpu(ch->len) - UBIFS_ORPH_NODE_SZ) >> 3; -		printk(KERN_DEBUG "\t%d orphan inode numbers:\n", n); +		pr_err("\t%d orphan inode numbers:\n", n);  		for (i = 0; i < n; i++) -			printk(KERN_DEBUG "\t  ino %llu\n", +			pr_err("\t  ino %llu\n",  			       (unsigned long long)le64_to_cpu(orph->inos[i]));  		break;  	}  	default: -		printk(KERN_DEBUG "node type %d was not recognized\n", +		pr_err("node type %d was not recognized\n",  		       (int)ch->node_type);  	}  	spin_unlock(&dbg_lock);  } -void dbg_dump_budget_req(const struct ubifs_budget_req *req) +void ubifs_dump_budget_req(const struct ubifs_budget_req *req)  {  	spin_lock(&dbg_lock); -	printk(KERN_DEBUG "Budgeting request: new_ino %d, dirtied_ino %d\n", +	pr_err("Budgeting request: new_ino %d, dirtied_ino %d\n",  	       req->new_ino, req->dirtied_ino); -	printk(KERN_DEBUG "\tnew_ino_d   %d, dirtied_ino_d %d\n", +	pr_err("\tnew_ino_d   %d, dirtied_ino_d %d\n",  	       req->new_ino_d, req->dirtied_ino_d); -	printk(KERN_DEBUG "\tnew_page    %d, dirtied_page %d\n", +	pr_err("\tnew_page    %d, dirtied_page %d\n",  	       req->new_page, req->dirtied_page); -	printk(KERN_DEBUG "\tnew_dent    %d, mod_dent     %d\n", +	pr_err("\tnew_dent    %d, mod_dent     %d\n",  	       req->new_dent, req->mod_dent); -	printk(KERN_DEBUG "\tidx_growth  %d\n", req->idx_growth); -	printk(KERN_DEBUG "\tdata_growth %d dd_growth     %d\n", +	pr_err("\tidx_growth  %d\n", req->idx_growth); +	pr_err("\tdata_growth %d dd_growth     %d\n",  	       req->data_growth, req->dd_growth);  	spin_unlock(&dbg_lock);  } -void dbg_dump_lstats(const struct ubifs_lp_stats *lst) +void ubifs_dump_lstats(const struct ubifs_lp_stats *lst)  {  	spin_lock(&dbg_lock); -	printk(KERN_DEBUG "(pid %d) Lprops statistics: empty_lebs %d, " -	       "idx_lebs  %d\n", current->pid, lst->empty_lebs, lst->idx_lebs); -	printk(KERN_DEBUG "\ttaken_empty_lebs %d, total_free %lld, " -	       "total_dirty %lld\n", lst->taken_empty_lebs, lst->total_free, -	       lst->total_dirty); -	printk(KERN_DEBUG "\ttotal_used %lld, total_dark %lld, " -	       "total_dead %lld\n", lst->total_used, lst->total_dark, -	       lst->total_dead); +	pr_err("(pid %d) Lprops statistics: empty_lebs %d, idx_lebs  %d\n", +	       current->pid, lst->empty_lebs, lst->idx_lebs); +	pr_err("\ttaken_empty_lebs %d, total_free %lld, total_dirty %lld\n", +	       lst->taken_empty_lebs, lst->total_free, lst->total_dirty); +	pr_err("\ttotal_used %lld, total_dark %lld, total_dead %lld\n", +	       lst->total_used, lst->total_dark, lst->total_dead);  	spin_unlock(&dbg_lock);  } -void dbg_dump_budg(struct ubifs_info *c) +void ubifs_dump_budg(struct ubifs_info *c, const struct ubifs_budg_info *bi)  {  	int i;  	struct rb_node *rb; @@ -610,54 +582,67 @@ void dbg_dump_budg(struct ubifs_info *c)  	struct ubifs_gced_idx_leb *idx_gc;  	long long available, outstanding, free; -	ubifs_assert(spin_is_locked(&c->space_lock)); +	spin_lock(&c->space_lock);  	spin_lock(&dbg_lock); -	printk(KERN_DEBUG "(pid %d) Budgeting info: budg_data_growth %lld, " -	       "budg_dd_growth %lld, budg_idx_growth %lld\n", current->pid, -	       c->budg_data_growth, c->budg_dd_growth, c->budg_idx_growth); -	printk(KERN_DEBUG "\tdata budget sum %lld, total budget sum %lld, " -	       "freeable_cnt %d\n", c->budg_data_growth + c->budg_dd_growth, -	       c->budg_data_growth + c->budg_dd_growth + c->budg_idx_growth, -	       c->freeable_cnt); -	printk(KERN_DEBUG "\tmin_idx_lebs %d, old_idx_sz %lld, " -	       "calc_idx_sz %lld, idx_gc_cnt %d\n", c->min_idx_lebs, -	       c->old_idx_sz, c->calc_idx_sz, c->idx_gc_cnt); -	printk(KERN_DEBUG "\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, " -	       "clean_zn_cnt %ld\n", atomic_long_read(&c->dirty_pg_cnt), +	pr_err("(pid %d) Budgeting info: data budget sum %lld, total budget sum %lld\n", +	       current->pid, bi->data_growth + bi->dd_growth, +	       bi->data_growth + bi->dd_growth + bi->idx_growth); +	pr_err("\tbudg_data_growth %lld, budg_dd_growth %lld, budg_idx_growth %lld\n", +	       bi->data_growth, bi->dd_growth, bi->idx_growth); +	pr_err("\tmin_idx_lebs %d, old_idx_sz %llu, uncommitted_idx %lld\n", +	       bi->min_idx_lebs, bi->old_idx_sz, bi->uncommitted_idx); +	pr_err("\tpage_budget %d, inode_budget %d, dent_budget %d\n", +	       bi->page_budget, bi->inode_budget, bi->dent_budget); +	pr_err("\tnospace %u, nospace_rp %u\n", bi->nospace, bi->nospace_rp); +	pr_err("\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n", +	       c->dark_wm, c->dead_wm, c->max_idx_node_sz); + +	if (bi != &c->bi) +		/* +		 * If we are dumping saved budgeting data, do not print +		 * additional information which is about the current state, not +		 * the old one which corresponded to the saved budgeting data. +		 */ +		goto out_unlock; + +	pr_err("\tfreeable_cnt %d, calc_idx_sz %lld, idx_gc_cnt %d\n", +	       c->freeable_cnt, c->calc_idx_sz, c->idx_gc_cnt); +	pr_err("\tdirty_pg_cnt %ld, dirty_zn_cnt %ld, clean_zn_cnt %ld\n", +	       atomic_long_read(&c->dirty_pg_cnt),  	       atomic_long_read(&c->dirty_zn_cnt),  	       atomic_long_read(&c->clean_zn_cnt)); -	printk(KERN_DEBUG "\tdark_wm %d, dead_wm %d, max_idx_node_sz %d\n", -	       c->dark_wm, c->dead_wm, c->max_idx_node_sz); -	printk(KERN_DEBUG "\tgc_lnum %d, ihead_lnum %d\n", -	       c->gc_lnum, c->ihead_lnum); +	pr_err("\tgc_lnum %d, ihead_lnum %d\n", c->gc_lnum, c->ihead_lnum); +  	/* If we are in R/O mode, journal heads do not exist */  	if (c->jheads)  		for (i = 0; i < c->jhead_cnt; i++) -			printk(KERN_DEBUG "\tjhead %s\t LEB %d\n", +			pr_err("\tjhead %s\t LEB %d\n",  			       dbg_jhead(c->jheads[i].wbuf.jhead),  			       c->jheads[i].wbuf.lnum);  	for (rb = rb_first(&c->buds); rb; rb = rb_next(rb)) {  		bud = rb_entry(rb, struct ubifs_bud, rb); -		printk(KERN_DEBUG "\tbud LEB %d\n", bud->lnum); +		pr_err("\tbud LEB %d\n", bud->lnum);  	}  	list_for_each_entry(bud, &c->old_buds, list) -		printk(KERN_DEBUG "\told bud LEB %d\n", bud->lnum); +		pr_err("\told bud LEB %d\n", bud->lnum);  	list_for_each_entry(idx_gc, &c->idx_gc, list) -		printk(KERN_DEBUG "\tGC'ed idx LEB %d unmap %d\n", +		pr_err("\tGC'ed idx LEB %d unmap %d\n",  		       idx_gc->lnum, idx_gc->unmap); -	printk(KERN_DEBUG "\tcommit state %d\n", c->cmt_state); +	pr_err("\tcommit state %d\n", c->cmt_state);  	/* Print budgeting predictions */ -	available = ubifs_calc_available(c, c->min_idx_lebs); -	outstanding = c->budg_data_growth + c->budg_dd_growth; +	available = ubifs_calc_available(c, c->bi.min_idx_lebs); +	outstanding = c->bi.data_growth + c->bi.dd_growth;  	free = ubifs_get_free_space_nolock(c); -	printk(KERN_DEBUG "Budgeting predictions:\n"); -	printk(KERN_DEBUG "\tavailable: %lld, outstanding %lld, free %lld\n", +	pr_err("Budgeting predictions:\n"); +	pr_err("\tavailable: %lld, outstanding %lld, free %lld\n",  	       available, outstanding, free); +out_unlock:  	spin_unlock(&dbg_lock); +	spin_unlock(&c->space_lock);  } -void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp) +void ubifs_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)  {  	int i, spc, dark = 0, dead = 0;  	struct rb_node *rb; @@ -670,21 +655,19 @@ void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)  		dark = ubifs_calc_dark(c, spc);  	if (lp->flags & LPROPS_INDEX) -		printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d " -		       "free + dirty %-8d flags %#x (", lp->lnum, lp->free, -		       lp->dirty, c->leb_size - spc, spc, lp->flags); +		pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d flags %#x (", +		       lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc, +		       lp->flags);  	else -		printk(KERN_DEBUG "LEB %-7d free %-8d dirty %-8d used %-8d " -		       "free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d " -		       "flags %#-4x (", lp->lnum, lp->free, lp->dirty, -		       c->leb_size - spc, spc, dark, dead, -		       (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags); +		pr_err("LEB %-7d free %-8d dirty %-8d used %-8d free + dirty %-8d dark %-4d dead %-4d nodes fit %-3d flags %#-4x (", +		       lp->lnum, lp->free, lp->dirty, c->leb_size - spc, spc, +		       dark, dead, (int)(spc / UBIFS_MAX_NODE_SZ), lp->flags);  	if (lp->flags & LPROPS_TAKEN) {  		if (lp->flags & LPROPS_INDEX) -			printk(KERN_CONT "index, taken"); +			pr_cont("index, taken");  		else -			printk(KERN_CONT "taken"); +			pr_cont("taken");  	} else {  		const char *s; @@ -721,7 +704,7 @@ void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)  				break;  			}  		} -		printk(KERN_CONT "%s", s); +		pr_cont("%s", s);  	}  	for (rb = rb_first((struct rb_root *)&c->buds); rb; rb = rb_next(rb)) { @@ -729,120 +712,147 @@ void dbg_dump_lprop(const struct ubifs_info *c, const struct ubifs_lprops *lp)  		if (bud->lnum == lp->lnum) {  			int head = 0;  			for (i = 0; i < c->jhead_cnt; i++) { -				if (lp->lnum == c->jheads[i].wbuf.lnum) { -					printk(KERN_CONT ", jhead %s", -					       dbg_jhead(i)); +				/* +				 * Note, if we are in R/O mode or in the middle +				 * of mounting/re-mounting, the write-buffers do +				 * not exist. +				 */ +				if (c->jheads && +				    lp->lnum == c->jheads[i].wbuf.lnum) { +					pr_cont(", jhead %s", dbg_jhead(i));  					head = 1;  				}  			}  			if (!head) -				printk(KERN_CONT ", bud of jhead %s", +				pr_cont(", bud of jhead %s",  				       dbg_jhead(bud->jhead));  		}  	}  	if (lp->lnum == c->gc_lnum) -		printk(KERN_CONT ", GC LEB"); -	printk(KERN_CONT ")\n"); +		pr_cont(", GC LEB"); +	pr_cont(")\n");  } -void dbg_dump_lprops(struct ubifs_info *c) +void ubifs_dump_lprops(struct ubifs_info *c)  {  	int lnum, err;  	struct ubifs_lprops lp;  	struct ubifs_lp_stats lst; -	printk(KERN_DEBUG "(pid %d) start dumping LEB properties\n", -	       current->pid); +	pr_err("(pid %d) start dumping LEB properties\n", current->pid);  	ubifs_get_lp_stats(c, &lst); -	dbg_dump_lstats(&lst); +	ubifs_dump_lstats(&lst);  	for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {  		err = ubifs_read_one_lp(c, lnum, &lp); -		if (err) +		if (err) {  			ubifs_err("cannot read lprops for LEB %d", lnum); +			continue; +		} -		dbg_dump_lprop(c, &lp); +		ubifs_dump_lprop(c, &lp);  	} -	printk(KERN_DEBUG "(pid %d) finish dumping LEB properties\n", -	       current->pid); +	pr_err("(pid %d) finish dumping LEB properties\n", current->pid);  } -void dbg_dump_lpt_info(struct ubifs_info *c) +void ubifs_dump_lpt_info(struct ubifs_info *c)  {  	int i;  	spin_lock(&dbg_lock); -	printk(KERN_DEBUG "(pid %d) dumping LPT information\n", current->pid); -	printk(KERN_DEBUG "\tlpt_sz:        %lld\n", c->lpt_sz); -	printk(KERN_DEBUG "\tpnode_sz:      %d\n", c->pnode_sz); -	printk(KERN_DEBUG "\tnnode_sz:      %d\n", c->nnode_sz); -	printk(KERN_DEBUG "\tltab_sz:       %d\n", c->ltab_sz); -	printk(KERN_DEBUG "\tlsave_sz:      %d\n", c->lsave_sz); -	printk(KERN_DEBUG "\tbig_lpt:       %d\n", c->big_lpt); -	printk(KERN_DEBUG "\tlpt_hght:      %d\n", c->lpt_hght); -	printk(KERN_DEBUG "\tpnode_cnt:     %d\n", c->pnode_cnt); -	printk(KERN_DEBUG "\tnnode_cnt:     %d\n", c->nnode_cnt); -	printk(KERN_DEBUG "\tdirty_pn_cnt:  %d\n", c->dirty_pn_cnt); -	printk(KERN_DEBUG "\tdirty_nn_cnt:  %d\n", c->dirty_nn_cnt); -	printk(KERN_DEBUG "\tlsave_cnt:     %d\n", c->lsave_cnt); -	printk(KERN_DEBUG "\tspace_bits:    %d\n", c->space_bits); -	printk(KERN_DEBUG "\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits); -	printk(KERN_DEBUG "\tlpt_offs_bits: %d\n", c->lpt_offs_bits); -	printk(KERN_DEBUG "\tlpt_spc_bits:  %d\n", c->lpt_spc_bits); -	printk(KERN_DEBUG "\tpcnt_bits:     %d\n", c->pcnt_bits); -	printk(KERN_DEBUG "\tlnum_bits:     %d\n", c->lnum_bits); -	printk(KERN_DEBUG "\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs); -	printk(KERN_DEBUG "\tLPT head is at %d:%d\n", +	pr_err("(pid %d) dumping LPT information\n", current->pid); +	pr_err("\tlpt_sz:        %lld\n", c->lpt_sz); +	pr_err("\tpnode_sz:      %d\n", c->pnode_sz); +	pr_err("\tnnode_sz:      %d\n", c->nnode_sz); +	pr_err("\tltab_sz:       %d\n", c->ltab_sz); +	pr_err("\tlsave_sz:      %d\n", c->lsave_sz); +	pr_err("\tbig_lpt:       %d\n", c->big_lpt); +	pr_err("\tlpt_hght:      %d\n", c->lpt_hght); +	pr_err("\tpnode_cnt:     %d\n", c->pnode_cnt); +	pr_err("\tnnode_cnt:     %d\n", c->nnode_cnt); +	pr_err("\tdirty_pn_cnt:  %d\n", c->dirty_pn_cnt); +	pr_err("\tdirty_nn_cnt:  %d\n", c->dirty_nn_cnt); +	pr_err("\tlsave_cnt:     %d\n", c->lsave_cnt); +	pr_err("\tspace_bits:    %d\n", c->space_bits); +	pr_err("\tlpt_lnum_bits: %d\n", c->lpt_lnum_bits); +	pr_err("\tlpt_offs_bits: %d\n", c->lpt_offs_bits); +	pr_err("\tlpt_spc_bits:  %d\n", c->lpt_spc_bits); +	pr_err("\tpcnt_bits:     %d\n", c->pcnt_bits); +	pr_err("\tlnum_bits:     %d\n", c->lnum_bits); +	pr_err("\tLPT root is at %d:%d\n", c->lpt_lnum, c->lpt_offs); +	pr_err("\tLPT head is at %d:%d\n",  	       c->nhead_lnum, c->nhead_offs); -	printk(KERN_DEBUG "\tLPT ltab is at %d:%d\n", -	       c->ltab_lnum, c->ltab_offs); +	pr_err("\tLPT ltab is at %d:%d\n", c->ltab_lnum, c->ltab_offs);  	if (c->big_lpt) -		printk(KERN_DEBUG "\tLPT lsave is at %d:%d\n", +		pr_err("\tLPT lsave is at %d:%d\n",  		       c->lsave_lnum, c->lsave_offs);  	for (i = 0; i < c->lpt_lebs; i++) -		printk(KERN_DEBUG "\tLPT LEB %d free %d dirty %d tgc %d " -		       "cmt %d\n", i + c->lpt_first, c->ltab[i].free, -		       c->ltab[i].dirty, c->ltab[i].tgc, c->ltab[i].cmt); +		pr_err("\tLPT LEB %d free %d dirty %d tgc %d cmt %d\n", +		       i + c->lpt_first, c->ltab[i].free, c->ltab[i].dirty, +		       c->ltab[i].tgc, c->ltab[i].cmt);  	spin_unlock(&dbg_lock);  } -void dbg_dump_leb(const struct ubifs_info *c, int lnum) +void ubifs_dump_sleb(const struct ubifs_info *c, +		     const struct ubifs_scan_leb *sleb, int offs) +{ +	struct ubifs_scan_node *snod; + +	pr_err("(pid %d) start dumping scanned data from LEB %d:%d\n", +	       current->pid, sleb->lnum, offs); + +	list_for_each_entry(snod, &sleb->nodes, list) { +		cond_resched(); +		pr_err("Dumping node at LEB %d:%d len %d\n", +		       sleb->lnum, snod->offs, snod->len); +		ubifs_dump_node(c, snod->node); +	} +} + +void ubifs_dump_leb(const struct ubifs_info *c, int lnum)  {  	struct ubifs_scan_leb *sleb;  	struct ubifs_scan_node *snod; +	void *buf; + +	pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum); -	if (dbg_failure_mode) +	buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); +	if (!buf) { +		ubifs_err("cannot allocate memory for dumping LEB %d", lnum);  		return; +	} -	printk(KERN_DEBUG "(pid %d) start dumping LEB %d\n", -	       current->pid, lnum); -	sleb = ubifs_scan(c, lnum, 0, c->dbg->buf, 0); +	sleb = ubifs_scan(c, lnum, 0, buf, 0);  	if (IS_ERR(sleb)) {  		ubifs_err("scan error %d", (int)PTR_ERR(sleb)); -		return; +		goto out;  	} -	printk(KERN_DEBUG "LEB %d has %d nodes ending at %d\n", lnum, +	pr_err("LEB %d has %d nodes ending at %d\n", lnum,  	       sleb->nodes_cnt, sleb->endpt);  	list_for_each_entry(snod, &sleb->nodes, list) {  		cond_resched(); -		printk(KERN_DEBUG "Dumping node at LEB %d:%d len %d\n", lnum, +		pr_err("Dumping node at LEB %d:%d len %d\n", lnum,  		       snod->offs, snod->len); -		dbg_dump_node(c, snod->node); +		ubifs_dump_node(c, snod->node);  	} -	printk(KERN_DEBUG "(pid %d) finish dumping LEB %d\n", -	       current->pid, lnum); +	pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);  	ubifs_scan_destroy(sleb); + +out: +	vfree(buf);  	return;  } -void dbg_dump_znode(const struct ubifs_info *c, -		    const struct ubifs_znode *znode) +void ubifs_dump_znode(const struct ubifs_info *c, +		      const struct ubifs_znode *znode)  {  	int n;  	const struct ubifs_zbranch *zbr; +	char key_buf[DBG_KEY_BUF_LEN];  	spin_lock(&dbg_lock);  	if (znode->parent) @@ -850,103 +860,102 @@ void dbg_dump_znode(const struct ubifs_info *c,  	else  		zbr = &c->zroot; -	printk(KERN_DEBUG "znode %p, LEB %d:%d len %d parent %p iip %d level %d" -	       " child_cnt %d flags %lx\n", znode, zbr->lnum, zbr->offs, -	       zbr->len, znode->parent, znode->iip, znode->level, -	       znode->child_cnt, znode->flags); +	pr_err("znode %p, LEB %d:%d len %d parent %p iip %d level %d child_cnt %d flags %lx\n", +	       znode, zbr->lnum, zbr->offs, zbr->len, znode->parent, znode->iip, +	       znode->level, znode->child_cnt, znode->flags);  	if (znode->child_cnt <= 0 || znode->child_cnt > c->fanout) {  		spin_unlock(&dbg_lock);  		return;  	} -	printk(KERN_DEBUG "zbranches:\n"); +	pr_err("zbranches:\n");  	for (n = 0; n < znode->child_cnt; n++) {  		zbr = &znode->zbranch[n];  		if (znode->level > 0) -			printk(KERN_DEBUG "\t%d: znode %p LEB %d:%d len %d key " -					  "%s\n", n, zbr->znode, zbr->lnum, -					  zbr->offs, zbr->len, -					  DBGKEY(&zbr->key)); +			pr_err("\t%d: znode %p LEB %d:%d len %d key %s\n", +			       n, zbr->znode, zbr->lnum, zbr->offs, zbr->len, +			       dbg_snprintf_key(c, &zbr->key, key_buf, +						DBG_KEY_BUF_LEN));  		else -			printk(KERN_DEBUG "\t%d: LNC %p LEB %d:%d len %d key " -					  "%s\n", n, zbr->znode, zbr->lnum, -					  zbr->offs, zbr->len, -					  DBGKEY(&zbr->key)); +			pr_err("\t%d: LNC %p LEB %d:%d len %d key %s\n", +			       n, zbr->znode, zbr->lnum, zbr->offs, zbr->len, +			       dbg_snprintf_key(c, &zbr->key, key_buf, +						DBG_KEY_BUF_LEN));  	}  	spin_unlock(&dbg_lock);  } -void dbg_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat) +void ubifs_dump_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat)  {  	int i; -	printk(KERN_DEBUG "(pid %d) start dumping heap cat %d (%d elements)\n", +	pr_err("(pid %d) start dumping heap cat %d (%d elements)\n",  	       current->pid, cat, heap->cnt);  	for (i = 0; i < heap->cnt; i++) {  		struct ubifs_lprops *lprops = heap->arr[i]; -		printk(KERN_DEBUG "\t%d. LEB %d hpos %d free %d dirty %d " -		       "flags %d\n", i, lprops->lnum, lprops->hpos, -		       lprops->free, lprops->dirty, lprops->flags); +		pr_err("\t%d. LEB %d hpos %d free %d dirty %d flags %d\n", +		       i, lprops->lnum, lprops->hpos, lprops->free, +		       lprops->dirty, lprops->flags);  	} -	printk(KERN_DEBUG "(pid %d) finish dumping heap\n", current->pid); +	pr_err("(pid %d) finish dumping heap\n", current->pid);  } -void dbg_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, -		    struct ubifs_nnode *parent, int iip) +void ubifs_dump_pnode(struct ubifs_info *c, struct ubifs_pnode *pnode, +		      struct ubifs_nnode *parent, int iip)  {  	int i; -	printk(KERN_DEBUG "(pid %d) dumping pnode:\n", current->pid); -	printk(KERN_DEBUG "\taddress %zx parent %zx cnext %zx\n", +	pr_err("(pid %d) dumping pnode:\n", current->pid); +	pr_err("\taddress %zx parent %zx cnext %zx\n",  	       (size_t)pnode, (size_t)parent, (size_t)pnode->cnext); -	printk(KERN_DEBUG "\tflags %lu iip %d level %d num %d\n", +	pr_err("\tflags %lu iip %d level %d num %d\n",  	       pnode->flags, iip, pnode->level, pnode->num);  	for (i = 0; i < UBIFS_LPT_FANOUT; i++) {  		struct ubifs_lprops *lp = &pnode->lprops[i]; -		printk(KERN_DEBUG "\t%d: free %d dirty %d flags %d lnum %d\n", +		pr_err("\t%d: free %d dirty %d flags %d lnum %d\n",  		       i, lp->free, lp->dirty, lp->flags, lp->lnum);  	}  } -void dbg_dump_tnc(struct ubifs_info *c) +void ubifs_dump_tnc(struct ubifs_info *c)  {  	struct ubifs_znode *znode;  	int level; -	printk(KERN_DEBUG "\n"); -	printk(KERN_DEBUG "(pid %d) start dumping TNC tree\n", current->pid); +	pr_err("\n"); +	pr_err("(pid %d) start dumping TNC tree\n", current->pid);  	znode = ubifs_tnc_levelorder_next(c->zroot.znode, NULL);  	level = znode->level; -	printk(KERN_DEBUG "== Level %d ==\n", level); +	pr_err("== Level %d ==\n", level);  	while (znode) {  		if (level != znode->level) {  			level = znode->level; -			printk(KERN_DEBUG "== Level %d ==\n", level); +			pr_err("== Level %d ==\n", level);  		} -		dbg_dump_znode(c, znode); +		ubifs_dump_znode(c, znode);  		znode = ubifs_tnc_levelorder_next(c->zroot.znode, znode);  	} -	printk(KERN_DEBUG "(pid %d) finish dumping TNC tree\n", current->pid); +	pr_err("(pid %d) finish dumping TNC tree\n", current->pid);  }  static int dump_znode(struct ubifs_info *c, struct ubifs_znode *znode,  		      void *priv)  { -	dbg_dump_znode(c, znode); +	ubifs_dump_znode(c, znode);  	return 0;  }  /** - * dbg_dump_index - dump the on-flash index. + * ubifs_dump_index - dump the on-flash index.   * @c: UBIFS file-system description object   * - * This function dumps whole UBIFS indexing B-tree, unlike 'dbg_dump_tnc()' + * This function dumps whole UBIFS indexing B-tree, unlike 'ubifs_dump_tnc()'   * which dumps only in-memory znodes and does not read znodes which from flash.   */ -void dbg_dump_index(struct ubifs_info *c) +void ubifs_dump_index(struct ubifs_info *c)  {  	dbg_walk_index(c, NULL, dump_znode, NULL);  } @@ -961,11 +970,41 @@ void dbg_dump_index(struct ubifs_info *c)  void dbg_save_space_info(struct ubifs_info *c)  {  	struct ubifs_debug_info *d = c->dbg; - -	ubifs_get_lp_stats(c, &d->saved_lst); +	int freeable_cnt;  	spin_lock(&c->space_lock); +	memcpy(&d->saved_lst, &c->lst, sizeof(struct ubifs_lp_stats)); +	memcpy(&d->saved_bi, &c->bi, sizeof(struct ubifs_budg_info)); +	d->saved_idx_gc_cnt = c->idx_gc_cnt; + +	/* +	 * We use a dirty hack here and zero out @c->freeable_cnt, because it +	 * affects the free space calculations, and UBIFS might not know about +	 * all freeable eraseblocks. Indeed, we know about freeable eraseblocks +	 * only when we read their lprops, and we do this only lazily, upon the +	 * need. So at any given point of time @c->freeable_cnt might be not +	 * exactly accurate. +	 * +	 * Just one example about the issue we hit when we did not zero +	 * @c->freeable_cnt. +	 * 1. The file-system is mounted R/O, c->freeable_cnt is %0. We save the +	 *    amount of free space in @d->saved_free +	 * 2. We re-mount R/W, which makes UBIFS to read the "lsave" +	 *    information from flash, where we cache LEBs from various +	 *    categories ('ubifs_remount_fs()' -> 'ubifs_lpt_init()' +	 *    -> 'lpt_init_wr()' -> 'read_lsave()' -> 'ubifs_lpt_lookup()' +	 *    -> 'ubifs_get_pnode()' -> 'update_cats()' +	 *    -> 'ubifs_add_to_cat()'). +	 * 3. Lsave contains a freeable eraseblock, and @c->freeable_cnt +	 *    becomes %1. +	 * 4. We calculate the amount of free space when the re-mount is +	 *    finished in 'dbg_check_space_info()' and it does not match +	 *    @d->saved_free. +	 */ +	freeable_cnt = c->freeable_cnt; +	c->freeable_cnt = 0;  	d->saved_free = ubifs_get_free_space_nolock(c); +	c->freeable_cnt = freeable_cnt;  	spin_unlock(&c->space_lock);  } @@ -982,12 +1021,15 @@ int dbg_check_space_info(struct ubifs_info *c)  {  	struct ubifs_debug_info *d = c->dbg;  	struct ubifs_lp_stats lst; -	long long avail, free; +	long long free; +	int freeable_cnt;  	spin_lock(&c->space_lock); -	avail = ubifs_calc_available(c, c->min_idx_lebs); +	freeable_cnt = c->freeable_cnt; +	c->freeable_cnt = 0; +	free = ubifs_get_free_space_nolock(c); +	c->freeable_cnt = freeable_cnt;  	spin_unlock(&c->space_lock); -	free = ubifs_get_free_space(c);  	if (free != d->saved_free) {  		ubifs_err("free space changed from %lld to %lld", @@ -999,21 +1041,22 @@ int dbg_check_space_info(struct ubifs_info *c)  out:  	ubifs_msg("saved lprops statistics dump"); -	dbg_dump_lstats(&d->saved_lst); -	ubifs_get_lp_stats(c, &lst); - +	ubifs_dump_lstats(&d->saved_lst); +	ubifs_msg("saved budgeting info dump"); +	ubifs_dump_budg(c, &d->saved_bi); +	ubifs_msg("saved idx_gc_cnt %d", d->saved_idx_gc_cnt);  	ubifs_msg("current lprops statistics dump"); -	dbg_dump_lstats(&lst); - -	spin_lock(&c->space_lock); -	dbg_dump_budg(c); -	spin_unlock(&c->space_lock); +	ubifs_get_lp_stats(c, &lst); +	ubifs_dump_lstats(&lst); +	ubifs_msg("current budgeting info dump"); +	ubifs_dump_budg(c, &c->bi);  	dump_stack();  	return -EINVAL;  }  /**   * dbg_check_synced_i_size - check synchronized inode size. + * @c: UBIFS file-system description object   * @inode: inode to check   *   * If inode is clean, synchronized inode size has to be equivalent to current @@ -1021,12 +1064,12 @@ out:   * has to be locked). Returns %0 if synchronized inode size if correct, and   * %-EINVAL if not.   */ -int dbg_check_synced_i_size(struct inode *inode) +int dbg_check_synced_i_size(const struct ubifs_info *c, struct inode *inode)  {  	int err = 0;  	struct ubifs_inode *ui = ubifs_inode(inode); -	if (!(ubifs_chk_flags & UBIFS_CHK_GEN)) +	if (!dbg_is_chk_gen(c))  		return 0;  	if (!S_ISREG(inode->i_mode))  		return 0; @@ -1034,11 +1077,11 @@ int dbg_check_synced_i_size(struct inode *inode)  	mutex_lock(&ui->ui_mutex);  	spin_lock(&ui->ui_lock);  	if (ui->ui_size != ui->synced_i_size && !ui->dirty) { -		ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode " -			  "is clean", ui->ui_size, ui->synced_i_size); +		ubifs_err("ui_size is %lld, synced_i_size is %lld, but inode is clean", +			  ui->ui_size, ui->synced_i_size);  		ubifs_err("i_ino %lu, i_mode %#x, i_size %lld", inode->i_ino,  			  inode->i_mode, i_size_read(inode)); -		dbg_dump_stack(); +		dump_stack();  		err = -EINVAL;  	}  	spin_unlock(&ui->ui_lock); @@ -1059,7 +1102,7 @@ int dbg_check_synced_i_size(struct inode *inode)   * Note, it is good idea to make sure the @dir->i_mutex is locked before   * calling this function.   */ -int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir) +int dbg_check_dir(struct ubifs_info *c, const struct inode *dir)  {  	unsigned int nlink = 2;  	union ubifs_key key; @@ -1067,7 +1110,7 @@ int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)  	struct qstr nm = { .name = NULL };  	loff_t size = UBIFS_INO_NODE_SZ; -	if (!(ubifs_chk_flags & UBIFS_CHK_GEN)) +	if (!dbg_is_chk_gen(c))  		return 0;  	if (!S_ISDIR(dir->i_mode)) @@ -1097,16 +1140,17 @@ int dbg_check_dir_size(struct ubifs_info *c, const struct inode *dir)  	kfree(pdent);  	if (i_size_read(dir) != size) { -		ubifs_err("directory inode %lu has size %llu, " -			  "but calculated size is %llu", dir->i_ino, -			  (unsigned long long)i_size_read(dir), +		ubifs_err("directory inode %lu has size %llu, but calculated size is %llu", +			  dir->i_ino, (unsigned long long)i_size_read(dir),  			  (unsigned long long)size); +		ubifs_dump_inode(c, dir);  		dump_stack();  		return -EINVAL;  	}  	if (dir->i_nlink != nlink) { -		ubifs_err("directory inode %lu has nlink %u, but calculated " -			  "nlink is %u", dir->i_ino, dir->i_nlink, nlink); +		ubifs_err("directory inode %lu has nlink %u, but calculated nlink is %u", +			  dir->i_ino, dir->i_nlink, nlink); +		ubifs_dump_inode(c, dir);  		dump_stack();  		return -EINVAL;  	} @@ -1133,6 +1177,7 @@ static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,  	int err, nlen1, nlen2, cmp;  	struct ubifs_dent_node *dent1, *dent2;  	union ubifs_key key; +	char key_buf[DBG_KEY_BUF_LEN];  	ubifs_assert(!keys_cmp(c, &zbr1->key, &zbr2->key));  	dent1 = kmalloc(UBIFS_MAX_DENT_NODE_SZ, GFP_NOFS); @@ -1162,21 +1207,25 @@ static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,  	err = 1;  	key_read(c, &dent1->key, &key);  	if (keys_cmp(c, &zbr1->key, &key)) { -		dbg_err("1st entry at %d:%d has key %s", zbr1->lnum, -			zbr1->offs, DBGKEY(&key)); -		dbg_err("but it should have key %s according to tnc", -			DBGKEY(&zbr1->key)); -		dbg_dump_node(c, dent1); +		ubifs_err("1st entry at %d:%d has key %s", zbr1->lnum, +			  zbr1->offs, dbg_snprintf_key(c, &key, key_buf, +						       DBG_KEY_BUF_LEN)); +		ubifs_err("but it should have key %s according to tnc", +			  dbg_snprintf_key(c, &zbr1->key, key_buf, +					   DBG_KEY_BUF_LEN)); +		ubifs_dump_node(c, dent1);  		goto out_free;  	}  	key_read(c, &dent2->key, &key);  	if (keys_cmp(c, &zbr2->key, &key)) { -		dbg_err("2nd entry at %d:%d has key %s", zbr1->lnum, -			zbr1->offs, DBGKEY(&key)); -		dbg_err("but it should have key %s according to tnc", -			DBGKEY(&zbr2->key)); -		dbg_dump_node(c, dent2); +		ubifs_err("2nd entry at %d:%d has key %s", zbr1->lnum, +			  zbr1->offs, dbg_snprintf_key(c, &key, key_buf, +						       DBG_KEY_BUF_LEN)); +		ubifs_err("but it should have key %s according to tnc", +			  dbg_snprintf_key(c, &zbr2->key, key_buf, +					   DBG_KEY_BUF_LEN)); +		ubifs_dump_node(c, dent2);  		goto out_free;  	} @@ -1189,15 +1238,15 @@ static int dbg_check_key_order(struct ubifs_info *c, struct ubifs_zbranch *zbr1,  		goto out_free;  	}  	if (cmp == 0 && nlen1 == nlen2) -		dbg_err("2 xent/dent nodes with the same name"); +		ubifs_err("2 xent/dent nodes with the same name");  	else -		dbg_err("bad order of colliding key %s", -			DBGKEY(&key)); +		ubifs_err("bad order of colliding key %s", +			  dbg_snprintf_key(c, &key, key_buf, DBG_KEY_BUF_LEN));  	ubifs_msg("first node at %d:%d\n", zbr1->lnum, zbr1->offs); -	dbg_dump_node(c, dent1); +	ubifs_dump_node(c, dent1);  	ubifs_msg("second node at %d:%d\n", zbr2->lnum, zbr2->offs); -	dbg_dump_node(c, dent2); +	ubifs_dump_node(c, dent2);  out_free:  	kfree(dent2); @@ -1400,10 +1449,10 @@ static int dbg_check_znode(struct ubifs_info *c, struct ubifs_zbranch *zbr)  out:  	ubifs_err("failed, error %d", err);  	ubifs_msg("dump of the znode"); -	dbg_dump_znode(c, znode); +	ubifs_dump_znode(c, znode);  	if (zp) {  		ubifs_msg("dump of the parent znode"); -		dbg_dump_znode(c, zp); +		ubifs_dump_znode(c, zp);  	}  	dump_stack();  	return -EINVAL; @@ -1423,7 +1472,7 @@ int dbg_check_tnc(struct ubifs_info *c, int extra)  	long clean_cnt = 0, dirty_cnt = 0;  	int err, last; -	if (!(ubifs_chk_flags & UBIFS_CHK_TNC)) +	if (!dbg_is_chk_index(c))  		return 0;  	ubifs_assert(mutex_is_locked(&c->tnc_mutex)); @@ -1470,9 +1519,9 @@ int dbg_check_tnc(struct ubifs_info *c, int extra)  				return err;  			if (err) {  				ubifs_msg("first znode"); -				dbg_dump_znode(c, prev); +				ubifs_dump_znode(c, prev);  				ubifs_msg("second znode"); -				dbg_dump_znode(c, znode); +				ubifs_dump_znode(c, znode);  				return -EINVAL;  			}  		} @@ -1559,9 +1608,9 @@ int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,  		if (znode_cb) {  			err = znode_cb(c, znode, priv);  			if (err) { -				ubifs_err("znode checking function returned " -					  "error %d", err); -				dbg_dump_znode(c, znode); +				ubifs_err("znode checking function returned error %d", +					  err); +				ubifs_dump_znode(c, znode);  				goto out_dump;  			}  		} @@ -1570,9 +1619,7 @@ int dbg_walk_index(struct ubifs_info *c, dbg_leaf_callback leaf_cb,  				zbr = &znode->zbranch[idx];  				err = leaf_cb(c, zbr, priv);  				if (err) { -					ubifs_err("leaf checking function " -						  "returned error %d, for leaf " -						  "at LEB %d:%d", +					ubifs_err("leaf checking function returned error %d, for leaf at LEB %d:%d",  						  err, zbr->lnum, zbr->offs);  					goto out_dump;  				} @@ -1629,7 +1676,7 @@ out_dump:  	else  		zbr = &c->zroot;  	ubifs_msg("dump of znode at LEB %d:%d", zbr->lnum, zbr->offs); -	dbg_dump_znode(c, znode); +	ubifs_dump_znode(c, znode);  out_unlock:  	mutex_unlock(&c->tnc_mutex);  	return err; @@ -1670,7 +1717,7 @@ int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)  	int err;  	long long calc = 0; -	if (!(ubifs_chk_flags & UBIFS_CHK_IDX_SZ)) +	if (!dbg_is_chk_index(c))  		return 0;  	err = dbg_walk_index(c, NULL, add_size, &calc); @@ -1680,8 +1727,8 @@ int dbg_check_idx_size(struct ubifs_info *c, long long idx_size)  	}  	if (calc != idx_size) { -		ubifs_err("index size check failed: calculated size is %lld, " -			  "should be %lld", calc, idx_size); +		ubifs_err("index size check failed: calculated size is %lld, should be %lld", +			  calc, idx_size);  		dump_stack();  		return -EINVAL;  	} @@ -1751,6 +1798,8 @@ static struct fsck_inode *add_inode(struct ubifs_info *c,  	struct rb_node **p, *parent = NULL;  	struct fsck_inode *fscki;  	ino_t inum = key_inum_flash(c, &ino->key); +	struct inode *inode; +	struct ubifs_inode *ui;  	p = &fsckd->inodes.rb_node;  	while (*p) { @@ -1774,19 +1823,46 @@ static struct fsck_inode *add_inode(struct ubifs_info *c,  	if (!fscki)  		return ERR_PTR(-ENOMEM); +	inode = ilookup(c->vfs_sb, inum); +  	fscki->inum = inum; -	fscki->nlink = le32_to_cpu(ino->nlink); -	fscki->size = le64_to_cpu(ino->size); -	fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt); -	fscki->xattr_sz = le32_to_cpu(ino->xattr_size); -	fscki->xattr_nms = le32_to_cpu(ino->xattr_names); -	fscki->mode = le32_to_cpu(ino->mode); +	/* +	 * If the inode is present in the VFS inode cache, use it instead of +	 * the on-flash inode which might be out-of-date. E.g., the size might +	 * be out-of-date. If we do not do this, the following may happen, for +	 * example: +	 *   1. A power cut happens +	 *   2. We mount the file-system R/O, the replay process fixes up the +	 *      inode size in the VFS cache, but on on-flash. +	 *   3. 'check_leaf()' fails because it hits a data node beyond inode +	 *      size. +	 */ +	if (!inode) { +		fscki->nlink = le32_to_cpu(ino->nlink); +		fscki->size = le64_to_cpu(ino->size); +		fscki->xattr_cnt = le32_to_cpu(ino->xattr_cnt); +		fscki->xattr_sz = le32_to_cpu(ino->xattr_size); +		fscki->xattr_nms = le32_to_cpu(ino->xattr_names); +		fscki->mode = le32_to_cpu(ino->mode); +	} else { +		ui = ubifs_inode(inode); +		fscki->nlink = inode->i_nlink; +		fscki->size = inode->i_size; +		fscki->xattr_cnt = ui->xattr_cnt; +		fscki->xattr_sz = ui->xattr_size; +		fscki->xattr_nms = ui->xattr_names; +		fscki->mode = inode->i_mode; +		iput(inode); +	} +  	if (S_ISDIR(fscki->mode)) {  		fscki->calc_sz = UBIFS_INO_NODE_SZ;  		fscki->calc_cnt = 2;  	} +  	rb_link_node(&fscki->rb, parent, p);  	rb_insert_color(&fscki->rb, &fsckd->inodes); +  	return fscki;  } @@ -1964,8 +2040,7 @@ static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,  		fscki = read_add_inode(c, priv, inum);  		if (IS_ERR(fscki)) {  			err = PTR_ERR(fscki); -			ubifs_err("error %d while processing data node and " -				  "trying to find inode node %lu", +			ubifs_err("error %d while processing data node and trying to find inode node %lu",  				  err, (unsigned long)inum);  			goto out_dump;  		} @@ -1975,9 +2050,8 @@ static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,  		blk_offs <<= UBIFS_BLOCK_SHIFT;  		blk_offs += le32_to_cpu(dn->size);  		if (blk_offs > fscki->size) { -			ubifs_err("data node at LEB %d:%d is not within inode " -				  "size %lld", zbr->lnum, zbr->offs, -				  fscki->size); +			ubifs_err("data node at LEB %d:%d is not within inode size %lld", +				  zbr->lnum, zbr->offs, fscki->size);  			err = -EINVAL;  			goto out_dump;  		} @@ -1998,8 +2072,7 @@ static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,  		fscki = read_add_inode(c, priv, inum);  		if (IS_ERR(fscki)) {  			err = PTR_ERR(fscki); -			ubifs_err("error %d while processing entry node and " -				  "trying to find inode node %lu", +			ubifs_err("error %d while processing entry node and trying to find inode node %lu",  				  err, (unsigned long)inum);  			goto out_dump;  		} @@ -2011,8 +2084,7 @@ static int check_leaf(struct ubifs_info *c, struct ubifs_zbranch *zbr,  		fscki1 = read_add_inode(c, priv, inum);  		if (IS_ERR(fscki1)) {  			err = PTR_ERR(fscki1); -			ubifs_err("error %d while processing entry node and " -				  "trying to find parent inode node %lu", +			ubifs_err("error %d while processing entry node and trying to find parent inode node %lu",  				  err, (unsigned long)inum);  			goto out_dump;  		} @@ -2036,7 +2108,7 @@ out:  out_dump:  	ubifs_msg("dump of node at LEB %d:%d", zbr->lnum, zbr->offs); -	dbg_dump_node(c, node); +	ubifs_dump_node(c, node);  out_free:  	kfree(node);  	return err; @@ -2048,26 +2120,10 @@ out_free:   */  static void free_inodes(struct fsck_data *fsckd)  { -	struct rb_node *this = fsckd->inodes.rb_node; -	struct fsck_inode *fscki; +	struct fsck_inode *fscki, *n; -	while (this) { -		if (this->rb_left) -			this = this->rb_left; -		else if (this->rb_right) -			this = this->rb_right; -		else { -			fscki = rb_entry(this, struct fsck_inode, rb); -			this = rb_parent(this); -			if (this) { -				if (this->rb_left == &fscki->rb) -					this->rb_left = NULL; -				else -					this->rb_right = NULL; -			} -			kfree(fscki); -		} -	} +	rbtree_postorder_for_each_entry_safe(fscki, n, &fsckd->inodes, rb) +		kfree(fscki);  }  /** @@ -2102,61 +2158,52 @@ static int check_inodes(struct ubifs_info *c, struct fsck_data *fsckd)  			 */  			if (fscki->inum != UBIFS_ROOT_INO &&  			    fscki->references != 1) { -				ubifs_err("directory inode %lu has %d " -					  "direntries which refer it, but " -					  "should be 1", +				ubifs_err("directory inode %lu has %d direntries which refer it, but should be 1",  					  (unsigned long)fscki->inum,  					  fscki->references);  				goto out_dump;  			}  			if (fscki->inum == UBIFS_ROOT_INO &&  			    fscki->references != 0) { -				ubifs_err("root inode %lu has non-zero (%d) " -					  "direntries which refer it", +				ubifs_err("root inode %lu has non-zero (%d) direntries which refer it",  					  (unsigned long)fscki->inum,  					  fscki->references);  				goto out_dump;  			}  			if (fscki->calc_sz != fscki->size) { -				ubifs_err("directory inode %lu size is %lld, " -					  "but calculated size is %lld", +				ubifs_err("directory inode %lu size is %lld, but calculated size is %lld",  					  (unsigned long)fscki->inum,  					  fscki->size, fscki->calc_sz);  				goto out_dump;  			}  			if (fscki->calc_cnt != fscki->nlink) { -				ubifs_err("directory inode %lu nlink is %d, " -					  "but calculated nlink is %d", +				ubifs_err("directory inode %lu nlink is %d, but calculated nlink is %d",  					  (unsigned long)fscki->inum,  					  fscki->nlink, fscki->calc_cnt);  				goto out_dump;  			}  		} else {  			if (fscki->references != fscki->nlink) { -				ubifs_err("inode %lu nlink is %d, but " -					  "calculated nlink is %d", +				ubifs_err("inode %lu nlink is %d, but calculated nlink is %d",  					  (unsigned long)fscki->inum,  					  fscki->nlink, fscki->references);  				goto out_dump;  			}  		}  		if (fscki->xattr_sz != fscki->calc_xsz) { -			ubifs_err("inode %lu has xattr size %u, but " -				  "calculated size is %lld", +			ubifs_err("inode %lu has xattr size %u, but calculated size is %lld",  				  (unsigned long)fscki->inum, fscki->xattr_sz,  				  fscki->calc_xsz);  			goto out_dump;  		}  		if (fscki->xattr_cnt != fscki->calc_xcnt) { -			ubifs_err("inode %lu has %u xattrs, but " -				  "calculated count is %lld", +			ubifs_err("inode %lu has %u xattrs, but calculated count is %lld",  				  (unsigned long)fscki->inum,  				  fscki->xattr_cnt, fscki->calc_xcnt);  			goto out_dump;  		}  		if (fscki->xattr_nms != fscki->calc_xnms) { -			ubifs_err("inode %lu has xattr names' size %u, but " -				  "calculated names' size is %lld", +			ubifs_err("inode %lu has xattr names' size %u, but calculated names' size is %lld",  				  (unsigned long)fscki->inum, fscki->xattr_nms,  				  fscki->calc_xnms);  			goto out_dump; @@ -2194,7 +2241,7 @@ out_dump:  	ubifs_msg("dump of the inode %lu sitting in LEB %d:%d",  		  (unsigned long)fscki->inum, zbr->lnum, zbr->offs); -	dbg_dump_node(c, ino); +	ubifs_dump_node(c, ino);  	kfree(ino);  	return -EINVAL;  } @@ -2217,7 +2264,7 @@ int dbg_check_filesystem(struct ubifs_info *c)  	int err;  	struct fsck_data fsckd; -	if (!(ubifs_chk_flags & UBIFS_CHK_FS)) +	if (!dbg_is_chk_fs(c))  		return 0;  	fsckd.inodes = RB_ROOT; @@ -2252,7 +2299,7 @@ int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)  	struct list_head *cur;  	struct ubifs_scan_node *sa, *sb; -	if (!(ubifs_chk_flags & UBIFS_CHK_GEN)) +	if (!dbg_is_chk_gen(c))  		return 0;  	for (cur = head->next; cur->next != head; cur = cur->next) { @@ -2265,12 +2312,12 @@ int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)  		if (sa->type != UBIFS_DATA_NODE) {  			ubifs_err("bad node type %d", sa->type); -			dbg_dump_node(c, sa->node); +			ubifs_dump_node(c, sa->node);  			return -EINVAL;  		}  		if (sb->type != UBIFS_DATA_NODE) {  			ubifs_err("bad node type %d", sb->type); -			dbg_dump_node(c, sb->node); +			ubifs_dump_node(c, sb->node);  			return -EINVAL;  		} @@ -2301,8 +2348,8 @@ int dbg_check_data_nodes_order(struct ubifs_info *c, struct list_head *head)  	return 0;  error_dump: -	dbg_dump_node(c, sa->node); -	dbg_dump_node(c, sb->node); +	ubifs_dump_node(c, sa->node); +	ubifs_dump_node(c, sb->node);  	return -EINVAL;  } @@ -2319,7 +2366,7 @@ int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)  	struct list_head *cur;  	struct ubifs_scan_node *sa, *sb; -	if (!(ubifs_chk_flags & UBIFS_CHK_GEN)) +	if (!dbg_is_chk_gen(c))  		return 0;  	for (cur = head->next; cur->next != head; cur = cur->next) { @@ -2333,13 +2380,13 @@ int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)  		if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&  		    sa->type != UBIFS_XENT_NODE) {  			ubifs_err("bad node type %d", sa->type); -			dbg_dump_node(c, sa->node); +			ubifs_dump_node(c, sa->node);  			return -EINVAL;  		}  		if (sa->type != UBIFS_INO_NODE && sa->type != UBIFS_DENT_NODE &&  		    sa->type != UBIFS_XENT_NODE) {  			ubifs_err("bad node type %d", sb->type); -			dbg_dump_node(c, sb->node); +			ubifs_dump_node(c, sb->node);  			return -EINVAL;  		} @@ -2379,7 +2426,8 @@ int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)  		hashb = key_block(c, &sb->key);  		if (hasha > hashb) { -			ubifs_err("larger hash %u goes before %u", hasha, hashb); +			ubifs_err("larger hash %u goes before %u", +				  hasha, hashb);  			goto error_dump;  		}  	} @@ -2388,400 +2436,363 @@ int dbg_check_nondata_nodes_order(struct ubifs_info *c, struct list_head *head)  error_dump:  	ubifs_msg("dumping first node"); -	dbg_dump_node(c, sa->node); +	ubifs_dump_node(c, sa->node);  	ubifs_msg("dumping second node"); -	dbg_dump_node(c, sb->node); +	ubifs_dump_node(c, sb->node);  	return -EINVAL;  	return 0;  } -static int invocation_cnt; - -int dbg_force_in_the_gaps(void) -{ -	if (!dbg_force_in_the_gaps_enabled) -		return 0; -	/* Force in-the-gaps every 8th commit */ -	return !((invocation_cnt++) & 0x7); -} - -/* Failure mode for recovery testing */ - -#define chance(n, d) (simple_rand() <= (n) * 32768LL / (d)) - -struct failure_mode_info { -	struct list_head list; -	struct ubifs_info *c; -}; - -static LIST_HEAD(fmi_list); -static DEFINE_SPINLOCK(fmi_lock); - -static unsigned int next; - -static int simple_rand(void) -{ -	if (next == 0) -		next = current->pid; -	next = next * 1103515245 + 12345; -	return (next >> 16) & 32767; -} - -static void failure_mode_init(struct ubifs_info *c) +static inline int chance(unsigned int n, unsigned int out_of)  { -	struct failure_mode_info *fmi; +	return !!((prandom_u32() % out_of) + 1 <= n); -	fmi = kmalloc(sizeof(struct failure_mode_info), GFP_NOFS); -	if (!fmi) { -		ubifs_err("Failed to register failure mode - no memory"); -		return; -	} -	fmi->c = c; -	spin_lock(&fmi_lock); -	list_add_tail(&fmi->list, &fmi_list); -	spin_unlock(&fmi_lock);  } -static void failure_mode_exit(struct ubifs_info *c) +static int power_cut_emulated(struct ubifs_info *c, int lnum, int write)  { -	struct failure_mode_info *fmi, *tmp; - -	spin_lock(&fmi_lock); -	list_for_each_entry_safe(fmi, tmp, &fmi_list, list) -		if (fmi->c == c) { -			list_del(&fmi->list); -			kfree(fmi); -		} -	spin_unlock(&fmi_lock); -} - -static struct ubifs_info *dbg_find_info(struct ubi_volume_desc *desc) -{ -	struct failure_mode_info *fmi; - -	spin_lock(&fmi_lock); -	list_for_each_entry(fmi, &fmi_list, list) -		if (fmi->c->ubi == desc) { -			struct ubifs_info *c = fmi->c; - -			spin_unlock(&fmi_lock); -			return c; -		} -	spin_unlock(&fmi_lock); -	return NULL; -} - -static int in_failure_mode(struct ubi_volume_desc *desc) -{ -	struct ubifs_info *c = dbg_find_info(desc); - -	if (c && dbg_failure_mode) -		return c->dbg->failure_mode; -	return 0; -} +	struct ubifs_debug_info *d = c->dbg; -static int do_fail(struct ubi_volume_desc *desc, int lnum, int write) -{ -	struct ubifs_info *c = dbg_find_info(desc); -	struct ubifs_debug_info *d; +	ubifs_assert(dbg_is_tst_rcvry(c)); -	if (!c || !dbg_failure_mode) -		return 0; -	d = c->dbg; -	if (d->failure_mode) -		return 1; -	if (!d->fail_cnt) { -		/* First call - decide delay to failure */ +	if (!d->pc_cnt) { +		/* First call - decide delay to the power cut */  		if (chance(1, 2)) { -			unsigned int delay = 1 << (simple_rand() >> 11); +			unsigned long delay;  			if (chance(1, 2)) { -				d->fail_delay = 1; -				d->fail_timeout = jiffies + -						  msecs_to_jiffies(delay); -				dbg_rcvry("failing after %ums", delay); +				d->pc_delay = 1; +				/* Fail withing 1 minute */ +				delay = prandom_u32() % 60000; +				d->pc_timeout = jiffies; +				d->pc_timeout += msecs_to_jiffies(delay); +				ubifs_warn("failing after %lums", delay);  			} else { -				d->fail_delay = 2; -				d->fail_cnt_max = delay; -				dbg_rcvry("failing after %u calls", delay); +				d->pc_delay = 2; +				delay = prandom_u32() % 10000; +				/* Fail within 10000 operations */ +				d->pc_cnt_max = delay; +				ubifs_warn("failing after %lu calls", delay);  			}  		} -		d->fail_cnt += 1; + +		d->pc_cnt += 1;  	} +  	/* Determine if failure delay has expired */ -	if (d->fail_delay == 1) { -		if (time_before(jiffies, d->fail_timeout)) +	if (d->pc_delay == 1 && time_before(jiffies, d->pc_timeout))  			return 0; -	} else if (d->fail_delay == 2) -		if (d->fail_cnt++ < d->fail_cnt_max) +	if (d->pc_delay == 2 && d->pc_cnt++ < d->pc_cnt_max)  			return 0; +  	if (lnum == UBIFS_SB_LNUM) { -		if (write) { -			if (chance(1, 2)) -				return 0; -		} else if (chance(19, 20)) +		if (write && chance(1, 2))  			return 0; -		dbg_rcvry("failing in super block LEB %d", lnum); +		if (chance(19, 20)) +			return 0; +		ubifs_warn("failing in super block LEB %d", lnum);  	} else if (lnum == UBIFS_MST_LNUM || lnum == UBIFS_MST_LNUM + 1) {  		if (chance(19, 20))  			return 0; -		dbg_rcvry("failing in master LEB %d", lnum); +		ubifs_warn("failing in master LEB %d", lnum);  	} else if (lnum >= UBIFS_LOG_LNUM && lnum <= c->log_last) { -		if (write) { -			if (chance(99, 100)) -				return 0; -		} else if (chance(399, 400)) +		if (write && chance(99, 100)) +			return 0; +		if (chance(399, 400))  			return 0; -		dbg_rcvry("failing in log LEB %d", lnum); +		ubifs_warn("failing in log LEB %d", lnum);  	} else if (lnum >= c->lpt_first && lnum <= c->lpt_last) { -		if (write) { -			if (chance(7, 8)) -				return 0; -		} else if (chance(19, 20)) +		if (write && chance(7, 8)) +			return 0; +		if (chance(19, 20))  			return 0; -		dbg_rcvry("failing in LPT LEB %d", lnum); +		ubifs_warn("failing in LPT LEB %d", lnum);  	} else if (lnum >= c->orph_first && lnum <= c->orph_last) { -		if (write) { -			if (chance(1, 2)) -				return 0; -		} else if (chance(9, 10)) +		if (write && chance(1, 2))  			return 0; -		dbg_rcvry("failing in orphan LEB %d", lnum); +		if (chance(9, 10)) +			return 0; +		ubifs_warn("failing in orphan LEB %d", lnum);  	} else if (lnum == c->ihead_lnum) {  		if (chance(99, 100))  			return 0; -		dbg_rcvry("failing in index head LEB %d", lnum); +		ubifs_warn("failing in index head LEB %d", lnum);  	} else if (c->jheads && lnum == c->jheads[GCHD].wbuf.lnum) {  		if (chance(9, 10))  			return 0; -		dbg_rcvry("failing in GC head LEB %d", lnum); +		ubifs_warn("failing in GC head LEB %d", lnum);  	} else if (write && !RB_EMPTY_ROOT(&c->buds) &&  		   !ubifs_search_bud(c, lnum)) {  		if (chance(19, 20))  			return 0; -		dbg_rcvry("failing in non-bud LEB %d", lnum); +		ubifs_warn("failing in non-bud LEB %d", lnum);  	} else if (c->cmt_state == COMMIT_RUNNING_BACKGROUND ||  		   c->cmt_state == COMMIT_RUNNING_REQUIRED) {  		if (chance(999, 1000))  			return 0; -		dbg_rcvry("failing in bud LEB %d commit running", lnum); +		ubifs_warn("failing in bud LEB %d commit running", lnum);  	} else {  		if (chance(9999, 10000))  			return 0; -		dbg_rcvry("failing in bud LEB %d commit not running", lnum); +		ubifs_warn("failing in bud LEB %d commit not running", lnum);  	} -	ubifs_err("*** SETTING FAILURE MODE ON (LEB %d) ***", lnum); -	d->failure_mode = 1; + +	d->pc_happened = 1; +	ubifs_warn("========== Power cut emulated ==========");  	dump_stack();  	return 1;  } -static void cut_data(const void *buf, int len) +static int corrupt_data(const struct ubifs_info *c, const void *buf, +			unsigned int len)  { -	int flen, i; +	unsigned int from, to, ffs = chance(1, 2);  	unsigned char *p = (void *)buf; -	flen = (len * (long long)simple_rand()) >> 15; -	for (i = flen; i < len; i++) -		p[i] = 0xff; -} +	from = prandom_u32() % len; +	/* Corruption span max to end of write unit */ +	to = min(len, ALIGN(from + 1, c->max_write_size)); -int dbg_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset, -		 int len, int check) -{ -	if (in_failure_mode(desc)) -		return -EIO; -	return ubi_leb_read(desc, lnum, buf, offset, len, check); +	ubifs_warn("filled bytes %u-%u with %s", from, to - 1, +		   ffs ? "0xFFs" : "random data"); + +	if (ffs) +		memset(p + from, 0xFF, to - from); +	else +		prandom_bytes(p + from, to - from); + +	return to;  } -int dbg_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf, -		  int offset, int len, int dtype) +int dbg_leb_write(struct ubifs_info *c, int lnum, const void *buf, +		  int offs, int len)  {  	int err, failing; -	if (in_failure_mode(desc)) -		return -EIO; -	failing = do_fail(desc, lnum, 1); -	if (failing) -		cut_data(buf, len); -	err = ubi_leb_write(desc, lnum, buf, offset, len, dtype); +	if (c->dbg->pc_happened) +		return -EROFS; + +	failing = power_cut_emulated(c, lnum, 1); +	if (failing) { +		len = corrupt_data(c, buf, len); +		ubifs_warn("actually write %d bytes to LEB %d:%d (the buffer was corrupted)", +			   len, lnum, offs); +	} +	err = ubi_leb_write(c->ubi, lnum, buf, offs, len);  	if (err)  		return err;  	if (failing) -		return -EIO; +		return -EROFS;  	return 0;  } -int dbg_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf, -		   int len, int dtype) +int dbg_leb_change(struct ubifs_info *c, int lnum, const void *buf, +		   int len)  {  	int err; -	if (do_fail(desc, lnum, 1)) -		return -EIO; -	err = ubi_leb_change(desc, lnum, buf, len, dtype); +	if (c->dbg->pc_happened) +		return -EROFS; +	if (power_cut_emulated(c, lnum, 1)) +		return -EROFS; +	err = ubi_leb_change(c->ubi, lnum, buf, len);  	if (err)  		return err; -	if (do_fail(desc, lnum, 1)) -		return -EIO; +	if (power_cut_emulated(c, lnum, 1)) +		return -EROFS;  	return 0;  } -int dbg_leb_erase(struct ubi_volume_desc *desc, int lnum) +int dbg_leb_unmap(struct ubifs_info *c, int lnum)  {  	int err; -	if (do_fail(desc, lnum, 0)) -		return -EIO; -	err = ubi_leb_erase(desc, lnum); +	if (c->dbg->pc_happened) +		return -EROFS; +	if (power_cut_emulated(c, lnum, 0)) +		return -EROFS; +	err = ubi_leb_unmap(c->ubi, lnum);  	if (err)  		return err; -	if (do_fail(desc, lnum, 0)) -		return -EIO; +	if (power_cut_emulated(c, lnum, 0)) +		return -EROFS;  	return 0;  } -int dbg_leb_unmap(struct ubi_volume_desc *desc, int lnum) +int dbg_leb_map(struct ubifs_info *c, int lnum)  {  	int err; -	if (do_fail(desc, lnum, 0)) -		return -EIO; -	err = ubi_leb_unmap(desc, lnum); +	if (c->dbg->pc_happened) +		return -EROFS; +	if (power_cut_emulated(c, lnum, 0)) +		return -EROFS; +	err = ubi_leb_map(c->ubi, lnum);  	if (err)  		return err; -	if (do_fail(desc, lnum, 0)) -		return -EIO; +	if (power_cut_emulated(c, lnum, 0)) +		return -EROFS;  	return 0;  } -int dbg_is_mapped(struct ubi_volume_desc *desc, int lnum) -{ -	if (in_failure_mode(desc)) -		return -EIO; -	return ubi_is_mapped(desc, lnum); -} +/* + * Root directory for UBIFS stuff in debugfs. Contains sub-directories which + * contain the stuff specific to particular file-system mounts. + */ +static struct dentry *dfs_rootdir; -int dbg_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype) +static int dfs_file_open(struct inode *inode, struct file *file)  { -	int err; - -	if (do_fail(desc, lnum, 0)) -		return -EIO; -	err = ubi_leb_map(desc, lnum, dtype); -	if (err) -		return err; -	if (do_fail(desc, lnum, 0)) -		return -EIO; -	return 0; +	file->private_data = inode->i_private; +	return nonseekable_open(inode, file);  }  /** - * ubifs_debugging_init - initialize UBIFS debugging. - * @c: UBIFS file-system description object + * provide_user_output - provide output to the user reading a debugfs file. + * @val: boolean value for the answer + * @u: the buffer to store the answer at + * @count: size of the buffer + * @ppos: position in the @u output buffer   * - * This function initializes debugging-related data for the file system. - * Returns zero in case of success and a negative error code in case of + * This is a simple helper function which stores @val boolean value in the user + * buffer when the user reads one of UBIFS debugfs files. Returns amount of + * bytes written to @u in case of success and a negative error code in case of   * failure.   */ -int ubifs_debugging_init(struct ubifs_info *c) +static int provide_user_output(int val, char __user *u, size_t count, +			       loff_t *ppos)  { -	c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL); -	if (!c->dbg) -		return -ENOMEM; - -	c->dbg->buf = vmalloc(c->leb_size); -	if (!c->dbg->buf) -		goto out; +	char buf[3]; -	failure_mode_init(c); -	return 0; +	if (val) +		buf[0] = '1'; +	else +		buf[0] = '0'; +	buf[1] = '\n'; +	buf[2] = 0x00; -out: -	kfree(c->dbg); -	return -ENOMEM; +	return simple_read_from_buffer(u, count, ppos, buf, 2);  } -/** - * ubifs_debugging_exit - free debugging data. - * @c: UBIFS file-system description object - */ -void ubifs_debugging_exit(struct ubifs_info *c) +static ssize_t dfs_file_read(struct file *file, char __user *u, size_t count, +			     loff_t *ppos)  { -	failure_mode_exit(c); -	vfree(c->dbg->buf); -	kfree(c->dbg); -} +	struct dentry *dent = file->f_path.dentry; +	struct ubifs_info *c = file->private_data; +	struct ubifs_debug_info *d = c->dbg; +	int val; + +	if (dent == d->dfs_chk_gen) +		val = d->chk_gen; +	else if (dent == d->dfs_chk_index) +		val = d->chk_index; +	else if (dent == d->dfs_chk_orph) +		val = d->chk_orph; +	else if (dent == d->dfs_chk_lprops) +		val = d->chk_lprops; +	else if (dent == d->dfs_chk_fs) +		val = d->chk_fs; +	else if (dent == d->dfs_tst_rcvry) +		val = d->tst_rcvry; +	else if (dent == d->dfs_ro_error) +		val = c->ro_error; +	else +		return -EINVAL; -/* - * Root directory for UBIFS stuff in debugfs. Contains sub-directories which - * contain the stuff specific to particular file-system mounts. - */ -static struct dentry *dfs_rootdir; +	return provide_user_output(val, u, count, ppos); +}  /** - * dbg_debugfs_init - initialize debugfs file-system. + * interpret_user_input - interpret user debugfs file input. + * @u: user-provided buffer with the input + * @count: buffer size   * - * UBIFS uses debugfs file-system to expose various debugging knobs to - * user-space. This function creates "ubifs" directory in the debugfs - * file-system. Returns zero in case of success and a negative error code in - * case of failure. + * This is a helper function which interpret user input to a boolean UBIFS + * debugfs file. Returns %0 or %1 in case of success and a negative error code + * in case of failure.   */ -int dbg_debugfs_init(void) +static int interpret_user_input(const char __user *u, size_t count)  { -	dfs_rootdir = debugfs_create_dir("ubifs", NULL); -	if (IS_ERR(dfs_rootdir)) { -		int err = PTR_ERR(dfs_rootdir); -		ubifs_err("cannot create \"ubifs\" debugfs directory, " -			  "error %d\n", err); -		return err; -	} +	size_t buf_size; +	char buf[8]; -	return 0; -} +	buf_size = min_t(size_t, count, (sizeof(buf) - 1)); +	if (copy_from_user(buf, u, buf_size)) +		return -EFAULT; -/** - * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system. - */ -void dbg_debugfs_exit(void) -{ -	debugfs_remove(dfs_rootdir); -} +	if (buf[0] == '1') +		return 1; +	else if (buf[0] == '0') +		return 0; -static int open_debugfs_file(struct inode *inode, struct file *file) -{ -	file->private_data = inode->i_private; -	return 0; +	return -EINVAL;  } -static ssize_t write_debugfs_file(struct file *file, const char __user *buf, -				  size_t count, loff_t *ppos) +static ssize_t dfs_file_write(struct file *file, const char __user *u, +			      size_t count, loff_t *ppos)  {  	struct ubifs_info *c = file->private_data;  	struct ubifs_debug_info *d = c->dbg; +	struct dentry *dent = file->f_path.dentry; +	int val; -	if (file->f_path.dentry == d->dfs_dump_lprops) -		dbg_dump_lprops(c); -	else if (file->f_path.dentry == d->dfs_dump_budg) { -		spin_lock(&c->space_lock); -		dbg_dump_budg(c); -		spin_unlock(&c->space_lock); -	} else if (file->f_path.dentry == d->dfs_dump_tnc) { +	/* +	 * TODO: this is racy - the file-system might have already been +	 * unmounted and we'd oops in this case. The plan is to fix it with +	 * help of 'iterate_supers_type()' which we should have in v3.0: when +	 * a debugfs opened, we rember FS's UUID in file->private_data. Then +	 * whenever we access the FS via a debugfs file, we iterate all UBIFS +	 * superblocks and fine the one with the same UUID, and take the +	 * locking right. +	 * +	 * The other way to go suggested by Al Viro is to create a separate +	 * 'ubifs-debug' file-system instead. +	 */ +	if (file->f_path.dentry == d->dfs_dump_lprops) { +		ubifs_dump_lprops(c); +		return count; +	} +	if (file->f_path.dentry == d->dfs_dump_budg) { +		ubifs_dump_budg(c, &c->bi); +		return count; +	} +	if (file->f_path.dentry == d->dfs_dump_tnc) {  		mutex_lock(&c->tnc_mutex); -		dbg_dump_tnc(c); +		ubifs_dump_tnc(c);  		mutex_unlock(&c->tnc_mutex); -	} else +		return count; +	} + +	val = interpret_user_input(u, count); +	if (val < 0) +		return val; + +	if (dent == d->dfs_chk_gen) +		d->chk_gen = val; +	else if (dent == d->dfs_chk_index) +		d->chk_index = val; +	else if (dent == d->dfs_chk_orph) +		d->chk_orph = val; +	else if (dent == d->dfs_chk_lprops) +		d->chk_lprops = val; +	else if (dent == d->dfs_chk_fs) +		d->chk_fs = val; +	else if (dent == d->dfs_tst_rcvry) +		d->tst_rcvry = val; +	else if (dent == d->dfs_ro_error) +		c->ro_error = !!val; +	else  		return -EINVAL; -	*ppos += count;  	return count;  }  static const struct file_operations dfs_fops = { -	.open = open_debugfs_file, -	.write = write_debugfs_file, +	.open = dfs_file_open, +	.read = dfs_file_read, +	.write = dfs_file_write,  	.owner = THIS_MODULE, -	.llseek = default_llseek, +	.llseek = no_llseek,  };  /** @@ -2798,46 +2809,104 @@ static const struct file_operations dfs_fops = {   */  int dbg_debugfs_init_fs(struct ubifs_info *c)  { -	int err; +	int err, n;  	const char *fname;  	struct dentry *dent;  	struct ubifs_debug_info *d = c->dbg; -	sprintf(d->dfs_dir_name, "ubi%d_%d", c->vi.ubi_num, c->vi.vol_id); -	d->dfs_dir = debugfs_create_dir(d->dfs_dir_name, dfs_rootdir); -	if (IS_ERR(d->dfs_dir)) { -		err = PTR_ERR(d->dfs_dir); -		ubifs_err("cannot create \"%s\" debugfs directory, error %d\n", -			  d->dfs_dir_name, err); +	if (!IS_ENABLED(CONFIG_DEBUG_FS)) +		return 0; + +	n = snprintf(d->dfs_dir_name, UBIFS_DFS_DIR_LEN + 1, UBIFS_DFS_DIR_NAME, +		     c->vi.ubi_num, c->vi.vol_id); +	if (n == UBIFS_DFS_DIR_LEN) { +		/* The array size is too small */ +		fname = UBIFS_DFS_DIR_NAME; +		dent = ERR_PTR(-EINVAL);  		goto out;  	} +	fname = d->dfs_dir_name; +	dent = debugfs_create_dir(fname, dfs_rootdir); +	if (IS_ERR_OR_NULL(dent)) +		goto out; +	d->dfs_dir = dent; +  	fname = "dump_lprops"; -	dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops); -	if (IS_ERR(dent)) +	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); +	if (IS_ERR_OR_NULL(dent))  		goto out_remove;  	d->dfs_dump_lprops = dent;  	fname = "dump_budg"; -	dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops); -	if (IS_ERR(dent)) +	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); +	if (IS_ERR_OR_NULL(dent))  		goto out_remove;  	d->dfs_dump_budg = dent;  	fname = "dump_tnc"; -	dent = debugfs_create_file(fname, S_IWUGO, d->dfs_dir, c, &dfs_fops); -	if (IS_ERR(dent)) +	dent = debugfs_create_file(fname, S_IWUSR, d->dfs_dir, c, &dfs_fops); +	if (IS_ERR_OR_NULL(dent))  		goto out_remove;  	d->dfs_dump_tnc = dent; +	fname = "chk_general"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, +				   &dfs_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	d->dfs_chk_gen = dent; + +	fname = "chk_index"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, +				   &dfs_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	d->dfs_chk_index = dent; + +	fname = "chk_orphans"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, +				   &dfs_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	d->dfs_chk_orph = dent; + +	fname = "chk_lprops"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, +				   &dfs_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	d->dfs_chk_lprops = dent; + +	fname = "chk_fs"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, +				   &dfs_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	d->dfs_chk_fs = dent; + +	fname = "tst_recovery"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, +				   &dfs_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	d->dfs_tst_rcvry = dent; + +	fname = "ro_error"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, d->dfs_dir, c, +				   &dfs_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	d->dfs_ro_error = dent; +  	return 0;  out_remove: -	err = PTR_ERR(dent); -	ubifs_err("cannot create \"%s\" debugfs directory, error %d\n", -		  fname, err);  	debugfs_remove_recursive(d->dfs_dir);  out: +	err = dent ? PTR_ERR(dent) : -ENODEV; +	ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n", +		  fname, err);  	return err;  } @@ -2847,7 +2916,185 @@ out:   */  void dbg_debugfs_exit_fs(struct ubifs_info *c)  { -	debugfs_remove_recursive(c->dbg->dfs_dir); +	if (IS_ENABLED(CONFIG_DEBUG_FS)) +		debugfs_remove_recursive(c->dbg->dfs_dir); +} + +struct ubifs_global_debug_info ubifs_dbg; + +static struct dentry *dfs_chk_gen; +static struct dentry *dfs_chk_index; +static struct dentry *dfs_chk_orph; +static struct dentry *dfs_chk_lprops; +static struct dentry *dfs_chk_fs; +static struct dentry *dfs_tst_rcvry; + +static ssize_t dfs_global_file_read(struct file *file, char __user *u, +				    size_t count, loff_t *ppos) +{ +	struct dentry *dent = file->f_path.dentry; +	int val; + +	if (dent == dfs_chk_gen) +		val = ubifs_dbg.chk_gen; +	else if (dent == dfs_chk_index) +		val = ubifs_dbg.chk_index; +	else if (dent == dfs_chk_orph) +		val = ubifs_dbg.chk_orph; +	else if (dent == dfs_chk_lprops) +		val = ubifs_dbg.chk_lprops; +	else if (dent == dfs_chk_fs) +		val = ubifs_dbg.chk_fs; +	else if (dent == dfs_tst_rcvry) +		val = ubifs_dbg.tst_rcvry; +	else +		return -EINVAL; + +	return provide_user_output(val, u, count, ppos);  } -#endif /* CONFIG_UBIFS_FS_DEBUG */ +static ssize_t dfs_global_file_write(struct file *file, const char __user *u, +				     size_t count, loff_t *ppos) +{ +	struct dentry *dent = file->f_path.dentry; +	int val; + +	val = interpret_user_input(u, count); +	if (val < 0) +		return val; + +	if (dent == dfs_chk_gen) +		ubifs_dbg.chk_gen = val; +	else if (dent == dfs_chk_index) +		ubifs_dbg.chk_index = val; +	else if (dent == dfs_chk_orph) +		ubifs_dbg.chk_orph = val; +	else if (dent == dfs_chk_lprops) +		ubifs_dbg.chk_lprops = val; +	else if (dent == dfs_chk_fs) +		ubifs_dbg.chk_fs = val; +	else if (dent == dfs_tst_rcvry) +		ubifs_dbg.tst_rcvry = val; +	else +		return -EINVAL; + +	return count; +} + +static const struct file_operations dfs_global_fops = { +	.read = dfs_global_file_read, +	.write = dfs_global_file_write, +	.owner = THIS_MODULE, +	.llseek = no_llseek, +}; + +/** + * dbg_debugfs_init - initialize debugfs file-system. + * + * UBIFS uses debugfs file-system to expose various debugging knobs to + * user-space. This function creates "ubifs" directory in the debugfs + * file-system. Returns zero in case of success and a negative error code in + * case of failure. + */ +int dbg_debugfs_init(void) +{ +	int err; +	const char *fname; +	struct dentry *dent; + +	if (!IS_ENABLED(CONFIG_DEBUG_FS)) +		return 0; + +	fname = "ubifs"; +	dent = debugfs_create_dir(fname, NULL); +	if (IS_ERR_OR_NULL(dent)) +		goto out; +	dfs_rootdir = dent; + +	fname = "chk_general"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, +				   &dfs_global_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	dfs_chk_gen = dent; + +	fname = "chk_index"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, +				   &dfs_global_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	dfs_chk_index = dent; + +	fname = "chk_orphans"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, +				   &dfs_global_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	dfs_chk_orph = dent; + +	fname = "chk_lprops"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, +				   &dfs_global_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	dfs_chk_lprops = dent; + +	fname = "chk_fs"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, +				   &dfs_global_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	dfs_chk_fs = dent; + +	fname = "tst_recovery"; +	dent = debugfs_create_file(fname, S_IRUSR | S_IWUSR, dfs_rootdir, NULL, +				   &dfs_global_fops); +	if (IS_ERR_OR_NULL(dent)) +		goto out_remove; +	dfs_tst_rcvry = dent; + +	return 0; + +out_remove: +	debugfs_remove_recursive(dfs_rootdir); +out: +	err = dent ? PTR_ERR(dent) : -ENODEV; +	ubifs_err("cannot create \"%s\" debugfs file or directory, error %d\n", +		  fname, err); +	return err; +} + +/** + * dbg_debugfs_exit - remove the "ubifs" directory from debugfs file-system. + */ +void dbg_debugfs_exit(void) +{ +	if (IS_ENABLED(CONFIG_DEBUG_FS)) +		debugfs_remove_recursive(dfs_rootdir); +} + +/** + * ubifs_debugging_init - initialize UBIFS debugging. + * @c: UBIFS file-system description object + * + * This function initializes debugging-related data for the file system. + * Returns zero in case of success and a negative error code in case of + * failure. + */ +int ubifs_debugging_init(struct ubifs_info *c) +{ +	c->dbg = kzalloc(sizeof(struct ubifs_debug_info), GFP_KERNEL); +	if (!c->dbg) +		return -ENOMEM; + +	return 0; +} + +/** + * ubifs_debugging_exit - free debugging data. + * @c: UBIFS file-system description object + */ +void ubifs_debugging_exit(struct ubifs_info *c) +{ +	kfree(c->dbg); +}  | 
