Message ID | 1426854565-5701-1-git-send-email-colin.king@canonical.com |
---|---|
State | New |
Headers | show |
On 20.03.2015 13:29, Colin King wrote: > From: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com> > > BugLink: http://bugs.launchpad.net/bugs/1434528 > > Rediffed remaining parts of original patch from Anand Jain. This makes > sure to avoid trailing newlines in the btrfs label output > > reproducer.sh: > > =============================================================================== > > TEST_DEV=/dev/vdb > TEST_DIR=/home/sat/mnt > > umount /home/sat/mnt > > mkfs.btrfs -f $TEST_DEV > UUID=$(btrfs fi show $TEST_DEV | head -1 | sed -e 's/.*uuid: \([-0-9a-z]*\)$/\1/') > mount $TEST_DEV $TEST_DIR > LABELFILE=/sys/fs/btrfs/$UUID/label > > echo "Test for empty label..." >&2 > LINES="$(cat $LABELFILE | wc -l | awk '{print $1}')" > RET=0 > > if [ $LINES -eq 0 ] ; then > echo '[PASS] Trailing \n is removed correctly.' >&2 > else > echo '[FAIL] Trailing \n still exists.' >&2 > RET=1 > fi > > echo "Test for non-empty label..." >&2 > > echo testlabel >$LABELFILE > LINES="$(cat $LABELFILE | wc -l | awk '{print $1}')" > > if [ $LINES -eq 1 ] ; then > echo '[PASS] Trailing \n is removed correctly.' >&2 > else > echo '[FAIL] Trailing \n still exists.' >&2 > RET=1 > fi > > exit $RET > =============================================================================== > > Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com> > Signed-off-by: Chris Mason <clm@fb.com> > (cherry pick from upstream commit 48fcc3ff7dce0138c053833adf81670494f177f3) > Signed-off-by: Colin Ian King <colin.king@canonical.com> > --- > fs/btrfs/sysfs.c | 15 ++++++++++++--- > 1 file changed, 12 insertions(+), 3 deletions(-) > > diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c > index 12e5355..82a968f 100644 > --- a/fs/btrfs/sysfs.c > +++ b/fs/btrfs/sysfs.c > @@ -363,7 +363,8 @@ static ssize_t btrfs_label_show(struct kobject *kobj, > struct kobj_attribute *a, char *buf) > { > struct btrfs_fs_info *fs_info = to_fs_info(kobj); > - return snprintf(buf, PAGE_SIZE, "%s\n", fs_info->super_copy->label); > + char *label = fs_info->super_copy->label; > + return snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label); > } > > static ssize_t btrfs_label_store(struct kobject *kobj, > @@ -374,8 +375,15 @@ static ssize_t btrfs_label_store(struct kobject *kobj, > struct btrfs_trans_handle *trans; > struct btrfs_root *root = fs_info->fs_root; > int ret; > + size_t p_len; > > - if (len >= BTRFS_LABEL_SIZE) > + /* > + * p_len is the len until the first occurrence of either > + * '\n' or '\0' > + */ > + p_len = strcspn(buf, "\n"); > + > + if (p_len >= BTRFS_LABEL_SIZE) > return -EINVAL; > > trans = btrfs_start_transaction(root, 0); > @@ -383,7 +391,8 @@ static ssize_t btrfs_label_store(struct kobject *kobj, > return PTR_ERR(trans); > > spin_lock(&root->fs_info->super_lock); > - strcpy(fs_info->super_copy->label, buf); > + memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); > + memcpy(fs_info->super_copy->label, buf, p_len); > spin_unlock(&root->fs_info->super_lock); > ret = btrfs_commit_transaction(trans, root); > >
On Fri, Mar 20, 2015 at 12:29:25PM +0000, Colin King wrote: > From: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com> > > BugLink: http://bugs.launchpad.net/bugs/1434528 > > Rediffed remaining parts of original patch from Anand Jain. This makes > sure to avoid trailing newlines in the btrfs label output > > reproducer.sh: > > =============================================================================== > > TEST_DEV=/dev/vdb > TEST_DIR=/home/sat/mnt > > umount /home/sat/mnt > > mkfs.btrfs -f $TEST_DEV > UUID=$(btrfs fi show $TEST_DEV | head -1 | sed -e 's/.*uuid: \([-0-9a-z]*\)$/\1/') > mount $TEST_DEV $TEST_DIR > LABELFILE=/sys/fs/btrfs/$UUID/label > > echo "Test for empty label..." >&2 > LINES="$(cat $LABELFILE | wc -l | awk '{print $1}')" > RET=0 > > if [ $LINES -eq 0 ] ; then > echo '[PASS] Trailing \n is removed correctly.' >&2 > else > echo '[FAIL] Trailing \n still exists.' >&2 > RET=1 > fi > > echo "Test for non-empty label..." >&2 > > echo testlabel >$LABELFILE > LINES="$(cat $LABELFILE | wc -l | awk '{print $1}')" > > if [ $LINES -eq 1 ] ; then > echo '[PASS] Trailing \n is removed correctly.' >&2 > else > echo '[FAIL] Trailing \n still exists.' >&2 > RET=1 > fi > > exit $RET > =============================================================================== > > Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com> > Signed-off-by: Chris Mason <clm@fb.com> > (cherry pick from upstream commit 48fcc3ff7dce0138c053833adf81670494f177f3) > Signed-off-by: Colin Ian King <colin.king@canonical.com> > --- > fs/btrfs/sysfs.c | 15 ++++++++++++--- > 1 file changed, 12 insertions(+), 3 deletions(-) > > diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c > index 12e5355..82a968f 100644 > --- a/fs/btrfs/sysfs.c > +++ b/fs/btrfs/sysfs.c > @@ -363,7 +363,8 @@ static ssize_t btrfs_label_show(struct kobject *kobj, > struct kobj_attribute *a, char *buf) > { > struct btrfs_fs_info *fs_info = to_fs_info(kobj); > - return snprintf(buf, PAGE_SIZE, "%s\n", fs_info->super_copy->label); > + char *label = fs_info->super_copy->label; > + return snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label); > } > > static ssize_t btrfs_label_store(struct kobject *kobj, > @@ -374,8 +375,15 @@ static ssize_t btrfs_label_store(struct kobject *kobj, > struct btrfs_trans_handle *trans; > struct btrfs_root *root = fs_info->fs_root; > int ret; > + size_t p_len; > > - if (len >= BTRFS_LABEL_SIZE) > + /* > + * p_len is the len until the first occurrence of either > + * '\n' or '\0' > + */ > + p_len = strcspn(buf, "\n"); > + > + if (p_len >= BTRFS_LABEL_SIZE) > return -EINVAL; > > trans = btrfs_start_transaction(root, 0); > @@ -383,7 +391,8 @@ static ssize_t btrfs_label_store(struct kobject *kobj, > return PTR_ERR(trans); > > spin_lock(&root->fs_info->super_lock); > - strcpy(fs_info->super_copy->label, buf); > + memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); > + memcpy(fs_info->super_copy->label, buf, p_len); > spin_unlock(&root->fs_info->super_lock); > ret = btrfs_commit_transaction(trans, root); > Looks to do what is claimed, if a little subtle with newline handling on read. Easily testable. Acked-by: Andy Whitcroft <apw@canonical.com> -apw
Applied to utopic. Cheers, -- Luís
=============================================================================== TEST_DEV=/dev/vdb TEST_DIR=/home/sat/mnt umount /home/sat/mnt mkfs.btrfs -f $TEST_DEV UUID=$(btrfs fi show $TEST_DEV | head -1 | sed -e 's/.*uuid: \([-0-9a-z]*\)$/\1/') mount $TEST_DEV $TEST_DIR LABELFILE=/sys/fs/btrfs/$UUID/label echo "Test for empty label..." >&2 LINES="$(cat $LABELFILE | wc -l | awk '{print $1}')" RET=0 if [ $LINES -eq 0 ] ; then echo '[PASS] Trailing \n is removed correctly.' >&2 else echo '[FAIL] Trailing \n still exists.' >&2 RET=1 fi echo "Test for non-empty label..." >&2 echo testlabel >$LABELFILE LINES="$(cat $LABELFILE | wc -l | awk '{print $1}')" if [ $LINES -eq 1 ] ; then echo '[PASS] Trailing \n is removed correctly.' >&2 else echo '[FAIL] Trailing \n still exists.' >&2 RET=1 fi exit $RET =============================================================================== Signed-off-by: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com> Signed-off-by: Chris Mason <clm@fb.com> (cherry pick from upstream commit 48fcc3ff7dce0138c053833adf81670494f177f3) Signed-off-by: Colin Ian King <colin.king@canonical.com> --- fs/btrfs/sysfs.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/sysfs.c b/fs/btrfs/sysfs.c index 12e5355..82a968f 100644 --- a/fs/btrfs/sysfs.c +++ b/fs/btrfs/sysfs.c @@ -363,7 +363,8 @@ static ssize_t btrfs_label_show(struct kobject *kobj, struct kobj_attribute *a, char *buf) { struct btrfs_fs_info *fs_info = to_fs_info(kobj); - return snprintf(buf, PAGE_SIZE, "%s\n", fs_info->super_copy->label); + char *label = fs_info->super_copy->label; + return snprintf(buf, PAGE_SIZE, label[0] ? "%s\n" : "%s", label); } static ssize_t btrfs_label_store(struct kobject *kobj, @@ -374,8 +375,15 @@ static ssize_t btrfs_label_store(struct kobject *kobj, struct btrfs_trans_handle *trans; struct btrfs_root *root = fs_info->fs_root; int ret; + size_t p_len; - if (len >= BTRFS_LABEL_SIZE) + /* + * p_len is the len until the first occurrence of either + * '\n' or '\0' + */ + p_len = strcspn(buf, "\n"); + + if (p_len >= BTRFS_LABEL_SIZE) return -EINVAL; trans = btrfs_start_transaction(root, 0); @@ -383,7 +391,8 @@ static ssize_t btrfs_label_store(struct kobject *kobj, return PTR_ERR(trans); spin_lock(&root->fs_info->super_lock); - strcpy(fs_info->super_copy->label, buf); + memset(fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE); + memcpy(fs_info->super_copy->label, buf, p_len); spin_unlock(&root->fs_info->super_lock); ret = btrfs_commit_transaction(trans, root);
From: Satoru Takeuchi <takeuchi_satoru@jp.fujitsu.com> BugLink: http://bugs.launchpad.net/bugs/1434528 Rediffed remaining parts of original patch from Anand Jain. This makes sure to avoid trailing newlines in the btrfs label output reproducer.sh: