linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anand Jain <anand.jain@oracle.com>
To: dsterba@suse.cz, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH v5 2/3] btrfs: balance: add args info during start and resume
Date: Tue, 20 Nov 2018 16:12:41 +0800	[thread overview]
Message-ID: <1155f14e-29c5-ae27-151c-e80e82a3cb66@oracle.com> (raw)
In-Reply-To: <20181119170748.GH24115@twin.jikos.cz>



On 11/20/2018 01:07 AM, David Sterba wrote:
> On Wed, Nov 14, 2018 at 09:17:11PM +0800, Anand Jain wrote:
>> Balance arg info is an important information to be reviewed for the
>> system audit. So this patch adds them to the kernel log.
>>
>> Example:
>> ->btrfs bal start -f -mprofiles=raid1,convert=single,soft -dlimit=10..20,usage=50 /btrfs
>>
>> kernel: BTRFS info (device sdb): balance: start -f -dusage=50,limit=10..20 -msoft,profiles=raid1,convert=single -ssoft,profiles=raid1,convert=single
>>
>> Signed-off-by: Anand Jain <anand.jain@oracle.com>
>> ---
>> v4.1->v5: Per David review comment the code..
>>             bp += snprintf(bp, buf - bp + size_buf, "soft,");
>> 	  is not safe if 'buf - bp + size_buf' becomes accidentally
>> 	  negative, so fix this by using following snprintf.
>>
>>   #define CHECK_UPDATE_BP_1(a) \
>>         do { \
>>                 ret = snprintf(bp, size_bp, a); \
>>                 if (ret < 0 || ret >= size_bp) \
>>                         goto out; \
>>                 size_bp -= ret; \
>>                 bp += ret; \
>>         } while (0)
>>
>> 	And initialize the tmp_buf to null.
>>
>> v4->v4.1: Rename last missed sz to size_buf.
>>
>> v3->v4: Change log updated with new example.
>> 	Log format is changed to almost match with the cli.
>> 	snprintf drop %s for inline string.
>> 	Print range as =%u..%u instead of min=%umax=%u.
>> 	soft is under the args now.
>> 	force is printed as -f.
>>
>> v2->v3: Change log updated.
>>          Make use of describe_block_group() added in 1/4
>>          Drop using strcat instead use snprintf.
>>          Logs string format updated as shown in the example above.
>>
>> v1->v2: Change log update.
>>          Move adding the logs for balance complete and end to a new patch
>>
>>   fs/btrfs/volumes.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
>>   1 file changed, 169 insertions(+), 3 deletions(-)
>>
>> diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
>> index 12b3d0625c6a..8397f72bdac7 100644
>> --- a/fs/btrfs/volumes.c
>> +++ b/fs/btrfs/volumes.c
>> @@ -3754,6 +3754,173 @@ static inline int validate_convert_profile(struct btrfs_balance_args *bctl_arg,
>>   		 (bctl_arg->target & ~allowed)));
>>   }
>>   
>> +static void describe_balance_args(struct btrfs_balance_args *bargs, char *buf,
>> +				 u32 size_buf)
>> +{
>> +	int ret;
>> +	u32 size_bp = size_buf;
>> +	char *bp = buf;
>> +	u64 flags = bargs->flags;
>> +	char tmp_buf[128] = {'\0'};
>> +
>> +	if (!flags)
>> +		return;
>> +
>> +#define CHECK_UPDATE_BP_1(a) \
> 
> CHECK_UPDATE_BP_NOARG and the others CHECK_UPDATE_BP_1ARG, though I'm
> thinking about picking another name for the macro like CHECK_APPEND_2ARG
> etc.

  I liked CHECK_APPEND_XXX I have renamed
   CHECK_UPDATE_BP_1 to CHECK_APPEND_NOARG
   CHECK_UPDATE_BP_2 to CHECK_APPEND_1ARG
   CHECK_UPDATE_BP_3 to CHECK_APPEND_2ARG

Hope its better now.

>> +	do { \
>> +		ret = snprintf(bp, size_bp, a); \
>> +		if (ret < 0 || ret >= size_bp) \
>> +			goto out; \
> 
> out_overflow and align \ to the right, please.

  Yep done.

>> +		size_bp -= ret; \
>> +		bp += ret; \
>> +	} while (0)
>> +
>> +#define CHECK_UPDATE_BP_2(a, v1) \
>> +	do { \
>> +		ret = snprintf(bp, size_bp, a, v1); \
>> +		if (ret < 0 || ret >= size_bp) \
>> +			goto out; \
>> +		size_bp -= ret; \
>> +		bp += ret; \
>> +	} while (0)
>> +
>> +#define CHECK_UPDATE_BP_3(a, v1, v2) \
>> +	do { \
>> +		ret = snprintf(bp, size_bp, a, v1, v2); \
>> +		if (ret < 0 || ret >= size_bp) \
>> +			goto out; \
>> +		size_bp -= ret; \
>> +		bp += ret; \
>> +	} while (0)
>> +
>> +	if (flags & BTRFS_BALANCE_ARGS_SOFT) {
>> +		CHECK_UPDATE_BP_1("soft,");
>> +	}
> 
> no { } around single statement

  fixed.

>> +
>> +	if (flags & BTRFS_BALANCE_ARGS_PROFILES) {
>> +		btrfs_describe_block_groups(bargs->profiles, tmp_buf,
>> +					    sizeof(tmp_buf));
>> +		CHECK_UPDATE_BP_2("profiles=%s,", tmp_buf);
>> +	}
>> +
>> +	if (flags & BTRFS_BALANCE_ARGS_USAGE) {
>> +		CHECK_UPDATE_BP_2("usage=%llu,", bargs->usage);
>> +	}
>> +
>> +	if (flags & BTRFS_BALANCE_ARGS_USAGE_RANGE) {
>> +		CHECK_UPDATE_BP_3("usage=%u..%u,",
>> +				  bargs->usage_min, bargs->usage_max);
>> +	}
>> +
>> +	if (flags & BTRFS_BALANCE_ARGS_DEVID) {
>> +		CHECK_UPDATE_BP_2("devid=%llu,", bargs->devid);
>> +	}
>> +
>> +	if (flags & BTRFS_BALANCE_ARGS_DRANGE) {
>> +		CHECK_UPDATE_BP_3("drange=%llu..%llu,",
>> +				  bargs->pstart, bargs->pend);
>> +	}
>> +
>> +	if (flags & BTRFS_BALANCE_ARGS_VRANGE) {
>> +		CHECK_UPDATE_BP_3("vrange%llu..%llu,",
>> +				  bargs->vstart, bargs->vend);
>> +	}
>> +
>> +	if (flags & BTRFS_BALANCE_ARGS_LIMIT) {
>> +		CHECK_UPDATE_BP_2("limit=%llu,", bargs->limit);
>> +	}
>> +
>> +	if (flags & BTRFS_BALANCE_ARGS_LIMIT_RANGE) {
>> +		CHECK_UPDATE_BP_3("limit=%u..%u,",
>> +				bargs->limit_min, bargs->limit_max);
>> +	}
>> +
>> +	if (flags & BTRFS_BALANCE_ARGS_STRIPES_RANGE) {
>> +		CHECK_UPDATE_BP_3("stripes=%u..%u,",
>> +				  bargs->stripes_min, bargs->stripes_max);
>> +	}
>> +
>> +	if (flags & BTRFS_BALANCE_ARGS_CONVERT) {
>> +		int index = btrfs_bg_flags_to_raid_index(bargs->target);
>> +
>> +		CHECK_UPDATE_BP_2("convert=%s,", get_raid_name(index));
>> +	}
>> +
>> +out:
>> +#undef CHECK_UPDATE_BP_3
>> +#undef CHECK_UPDATE_BP_2
>> +#undef CHECK_UPDATE_BP_1
>> +
>> +	if (size_bp < size_buf)
>> +		buf[size_buf - size_bp - 1] = '\0'; /* remove last , */
>> +	else
>> +		buf[0] = '\0';
>> +}
>> +
>> +static void describe_balance_start_or_resume(struct btrfs_fs_info *fs_info)
>> +{
>> +	u32 size_buf = 1024;
>> +	char tmp_buf[192] = {'\0'};
>> +	char *buf;
>> +	char *bp;
>> +	u32 size_bp = size_buf;
>> +	int ret;
>> +	struct btrfs_balance_control *bctl = fs_info->balance_ctl;
>> +
>> +	buf = kzalloc(size_buf, GFP_KERNEL);
>> +	if (!buf)
>> +		return;
>> +
>> +	bp = buf;
>> +#define CHECK_UPDATE_BP_1(a) \
>> +	do { \
>> +		ret = snprintf(bp, size_bp, a); \
>> +		if (ret < 0 || ret >= size_bp) \
>> +			return; \

  Leaking buf above. By using goto out_overflow also fixes this.

Thanks, Anand


>> +		size_bp -= ret; \
>> +		bp += ret; \
>> +	} while (0)
>> +
>> +#define CHECK_UPDATE_BP_2(a, v1) \
>> +	do { \
>> +		ret = snprintf(bp, size_bp, a, v1); \
>> +		if (ret < 0 || ret >= size_bp) \
>> +			return; \
>> +		size_bp -= ret; \
>> +		bp += ret; \
>> +	} while (0)
>> +
>> +	if (bctl->flags & BTRFS_BALANCE_FORCE) {
>> +		CHECK_UPDATE_BP_1("-f ");
>> +	}
>> +
>> +	if (bctl->flags & BTRFS_BALANCE_DATA) {
>> +		describe_balance_args(&bctl->data, tmp_buf, sizeof(tmp_buf));
>> +		CHECK_UPDATE_BP_2("-d%s ", tmp_buf);
>> +	}
>> +
>> +	if (bctl->flags & BTRFS_BALANCE_METADATA) {
>> +		describe_balance_args(&bctl->meta, tmp_buf, sizeof(tmp_buf));
>> +		CHECK_UPDATE_BP_2("-m%s ", tmp_buf);
>> +	}
>> +
>> +	if (bctl->flags & BTRFS_BALANCE_SYSTEM) {
>> +		describe_balance_args(&bctl->sys, tmp_buf, sizeof(tmp_buf));
>> +		CHECK_UPDATE_BP_2("-s%s ", tmp_buf);
>> +	}
>> +
>> +#undef CHECK_UPDATE_BP_2
>> +#undef CHECK_UPDATE_BP_1
>> +
>> +	if (size_bp < size_buf)
>> +		buf[size_buf - size_bp - 1] = '\0'; /* remove last " "*/
>> +	btrfs_info(fs_info, "%s %s",
>> +		   bctl->flags & BTRFS_BALANCE_RESUME ?
>> +		   "balance: resume":"balance: start", buf);
>> +
>> +	kfree(buf);
>> +}
>> +
>>   /*
>>    * Should be called with balance mutexe held
>>    */
>> @@ -3893,6 +4060,7 @@ int btrfs_balance(struct btrfs_fs_info *fs_info,
>>   
>>   	ASSERT(!test_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags));
>>   	set_bit(BTRFS_FS_BALANCE_RUNNING, &fs_info->flags);
>> +	describe_balance_start_or_resume(fs_info);
>>   	mutex_unlock(&fs_info->balance_mutex);
>>   
>>   	ret = __btrfs_balance(fs_info);
>> @@ -3930,10 +4098,8 @@ static int balance_kthread(void *data)
>>   	int ret = 0;
>>   
>>   	mutex_lock(&fs_info->balance_mutex);
>> -	if (fs_info->balance_ctl) {
>> -		btrfs_info(fs_info, "balance: resuming");
>> +	if (fs_info->balance_ctl)
>>   		ret = btrfs_balance(fs_info, fs_info->balance_ctl, NULL);
>> -	}
>>   	mutex_unlock(&fs_info->balance_mutex);
>>   
>>   	return ret;
>> -- 
>> 1.8.3.1

  reply	other threads:[~2018-11-20  8:13 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-14 13:17 [PATCH v5 0/3] btrfs: balance: improve kernel logs Anand Jain
2018-11-14 13:17 ` [PATCH v5 1/3] btrfs: add helper function describe_block_group() Anand Jain
2018-11-19 17:02   ` David Sterba
2018-11-20  8:07     ` Anand Jain
2018-11-14 13:17 ` [PATCH v5 2/3] btrfs: balance: add args info during start and resume Anand Jain
2018-11-19 17:07   ` David Sterba
2018-11-20  8:12     ` Anand Jain [this message]
2018-11-14 13:17 ` [PATCH v5 3/3] btrfs: balance: add kernel log for end or paused Anand Jain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1155f14e-29c5-ae27-151c-e80e82a3cb66@oracle.com \
    --to=anand.jain@oracle.com \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).