fs/udf/inode.c int shift sign extends
Subsystems (2)
PATCH 1/1 fs/udf/inode.c int shift sign extends
Ben F:
Oi. fs/udf/inode.c includes an always-false test:
int type = laarr[curr].extLength & ~UDF_EXTENT_LENGTH_MASK;
...
if ((type >> 30) == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))I say always false because (type >> 30) is in -2 -1 0 1, since type is int, not unsigned int. (0x... >> 30) is in 0 1 2 3 by definition. -2 -1 never equal 2 3 of course.
Agreed?
If yes, then do you believe the patch below changes source to actually work more as intended?
Courtesy your past work, I have test cases which provoke type = EXT_NOT_RECORDED_NOT_ALLOCATED = 0x80000000. Via udf_debug I then see yes indeed ((type >> 30) != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)).
To confirm my experience of C elsewhere applies here with lk gcc, I also wrote the attached demo of (type = BITS) failing to imply ((type >> 30) == (BITS >> 30)).
Pat LaVarre
fs/udf/inode.c +3 -3
@@ -612,7 +612,7 @@ static void udf_split_extents(struct inoif (offset) { -if ((type >>30 ) == (EXT_NOT_RECORDED_ALLOCATED >>30 )) +if (type ==EXT_NOT_RECORDED_ALLOCATED ) {udf_free_blocks (inode->i_sb, inode, laarr[curr].extLocation,0 , offset); laarr[curr].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
@@ -629,7 +629,7 @@ static void udf_split_extents(struct ino } laarr[curr].extLocation.logicalBlockNum = newblocknum; -if ((type >>30 ) == (EXT_NOT_RECORDED_NOT_ALLOCATED >>30 )) +if (type ==EXT_NOT_RECORDED_NOT_ALLOCATED ) laarr[curr].extLocation.partitionReferenceNum =UDF_I_LOCATION (inode).partitionReferenceNum; laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
@@ -638,7 +638,7 @@ static void udf_split_extents(struct inoif (blen != offset +1 ) { -if ((type >>30 ) == (EXT_NOT_RECORDED_ALLOCATED >>30 )) +if (type ==EXT_NOT_RECORDED_ALLOCATED ) laarr[curr].extLocation.logicalBlockNum += (offset +1 ); laarr[curr].extLength = type | ((blen - (offset +1 )) << inode->i_sb->s_blocksize_bits);
#include <stdio.h>
#define EXT_RECORDED_ALLOCATED 0x00000000
#define EXT_NOT_RECORDED_ALLOCATED 0x40000000
#define EXT_NOT_RECORDED_NOT_ALLOCATED 0x80000000
#define EXT_NEXT_EXTENT_ALLOCDECS 0xC0000000
#define DEMO(BITS) do { \
int type = BITS; \
printf("%d\n", ((type >> 30) == (BITS >> 30))); \
} while (0)int main(void) {
DEMO(EXT_RECORDED_ALLOCATED);
DEMO(EXT_NOT_RECORDED_ALLOCATED);
DEMO(EXT_NOT_RECORDED_NOT_ALLOCATED);
DEMO(EXT_NEXT_EXTENT_ALLOCDECS);
return 0;
}Ben F:
Quoted 5 lines
Oi.
fs/udf/inode.cincludes an always-false test:int type = laarr[curr].extLength & ~UDF_EXTENT_LENGTH_MASK; ... if ((type >> 30) == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
I mean to say, always false because:
a) 2 is not in -2 -1 0 1.
b) The implicitly unsigned (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30) is 2.
c) The implicitly signed (type >> 30) is in -2 -1 0 1.
The following rewrite of this same patch also adds unrelated comments nearby in that same patched source file.
These comments I made as I continued the http://udfko.blog-city.com/
kernelnewbie hunt for why udf.ko can't yet write more than 112 fragments per file without offending the Philips udfct_1.0r2/src/udf_tester/udf_test fsck of udf.
The comments not enclosed in [square brackets] may be correct and helpful.
Pat LaVarre
fs/udf/inode.c +55 -5
@@ -612,7 +612,7 @@ static void udf_split_extents(struct inoif (offset) { -if ((type >>30 ) == (EXT_NOT_RECORDED_ALLOCATED >>30 )) +if (type ==EXT_NOT_RECORDED_ALLOCATED ) {udf_free_blocks (inode->i_sb, inode, laarr[curr].extLocation,0 , offset); laarr[curr].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
@@ -629,7 +629,7 @@ static void udf_split_extents(struct ino } laarr[curr].extLocation.logicalBlockNum = newblocknum; -if ((type >>30 ) == (EXT_NOT_RECORDED_NOT_ALLOCATED >>30 )) +if (type ==EXT_NOT_RECORDED_NOT_ALLOCATED ) laarr[curr].extLocation.partitionReferenceNum =UDF_I_LOCATION (inode).partitionReferenceNum; laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
@@ -638,7 +638,7 @@ static void udf_split_extents(struct inoif (blen != offset +1 ) { -if ((type >>30 ) == (EXT_NOT_RECORDED_ALLOCATED >>30 )) +if (type ==EXT_NOT_RECORDED_ALLOCATED ) laarr[curr].extLocation.logicalBlockNum += (offset +1 ); laarr[curr].extLength = type | ((blen - (offset +1 )) << inode->i_sb->s_blocksize_bits);
@@ -1596,6 +1596,13 @@ udf_iget(struct super_block *sb, lb_addrreturn inode; } +/* [Wrap udf_write_aext for good reasons which include:] +* +* Construct a new { elen, eloc } AD. +* +* Return (elen << 30) to succeed, else -1. +*/ +int8_t udf_add_aext (struct inode *inode,lb_addr *bloc,int *extoffset,lb_addr eloc,uint32_t elen,struct buffer_head **bh,int inc) {
@@ -1606,11 +1613,15 @@ int8_t udf_add_aext(struct inode *inode,int8_t etype;uint8_t *ptr; +/* Name the byte past the last AD (in FE else in AED). */ +if (!*bh) ptr =UDF_I_DATA (inode) + *extoffset -udf_file_entry_alloc_offset (inode) +UDF_I_LENEATTR (inode);else ptr = (*bh)->b_data + *extoffset; +/* Count bytes per AD. */ +if (UDF_I_ALLOCTYPE (inode) ==ICBTAG_FLAG_AD_SHORT ) adsize =sizeof (short_ad);else if (UDF_I_ALLOCTYPE (inode) ==ICBTAG_FLAG_AD_LONG )
@@ -1618,13 +1629,17 @@ int8_t udf_add_aext(struct inode *inode,else return -1 ; +/* If less than two more AD fit ... */ +if (*extoffset + (2 * adsize) > inode->i_sb->s_blocksize) {char *sptr, *dptr;struct buffer_head *nbh;int err, loffset; -lb_addr obloc = *bloc; +/* Allot nearby (and zero) a next extent of AD. */ + +lb_addr obloc = *bloc;if (!(bloc->logicalBlockNum =udf_new_block (inode->i_sb, inode, obloc.partitionReferenceNum, obloc.logicalBlockNum, &err))) {
@@ -1641,9 +1656,14 @@ int8_t udf_add_aext(struct inode *inode,unlock_buffer (nbh);mark_buffer_dirty_inode (nbh, inode); +/* Link back if affordable. */ + aed = (struct allocExtDesc *)(nbh->b_data);if (!UDF_QUERY_FLAG (inode->i_sb,UDF_FLAG_STRICT )) aed->previousAllocExtLocation =cpu_to_le32 (obloc.logicalBlockNum); + +/* Move AD before overwrite, else append. */ +if (*extoffset + adsize > inode->i_sb->s_blocksize) { loffset = *extoffset;
@@ -1659,7 +1679,6 @@ int8_t udf_add_aext(struct inode *inode, aed->lengthAllocDescs =cpu_to_le32 (0 ); sptr = ptr; *extoffset =sizeof (struct allocExtDesc ); -if (*bh) { aed = (struct allocExtDesc *)(*bh)->b_data;
@@ -1672,12 +1691,18 @@ int8_t udf_add_aext(struct inode *inode,mark_inode_dirty (inode); } } + +/* Tag the new AED. */ +if (UDF_SB_UDFREV (inode->i_sb) >=0x0200 )udf_new_tag (nbh->b_data,TAG_IDENT_AED ,3 ,1 , bloc->logicalBlockNum,sizeof (tag));else udf_new_tag (nbh->b_data,TAG_IDENT_AED ,2 ,1 , bloc->logicalBlockNum,sizeof (tag)); + +/* Construct an AD to link to next extent of AD. */ +switch (UDF_I_ALLOCTYPE (inode)) {case ICBTAG_FLAG_AD_SHORT :
@@ -1700,6 +1725,9 @@ int8_t udf_add_aext(struct inode *inode,break ; } } + +/* Switch focus to the new AED. */ +if (*bh) {if (!UDF_QUERY_FLAG (inode->i_sb,UDF_FLAG_STRICT ) ||UDF_SB_UDFREV (inode->i_sb) >=0x0201 )
@@ -1714,8 +1742,12 @@ int8_t udf_add_aext(struct inode *inode, *bh = nbh; } +/* Construct a new { elen, eloc } AD. */ + etype =udf_write_aext (inode, *bloc, extoffset, eloc, elen, *bh, inc); +/* Claim the space occupied by the new AD. */ +if (!*bh) {UDF_I_LENALLOC (inode) += adsize;
@@ -1733,15 +1765,24 @@ int8_t udf_add_aext(struct inode *inode,mark_buffer_dirty_inode (*bh, inode); } +/* [Pass back success or failure.] */ +return etype; } +/* Construct a new { elen, eloc } AD. +* +* Return (elen << 30) to succeed, else -1. +*/ +int8_t udf_write_aext (struct inode *inode,lb_addr bloc,int *extoffset,lb_addr eloc,uint32_t elen,struct buffer_head *bh,int inc) {int adsize;uint8_t *ptr; +/* Name the byte past the last AD (in FE else in AED). */ +if (!bh) ptr =UDF_I_DATA (inode) + *extoffset -udf_file_entry_alloc_offset (inode) +UDF_I_LENEATTR (inode);else
@@ -1750,6 +1791,8 @@ int8_t udf_write_aext(struct inode *inodatomic_inc (&bh->b_count); } +/* Construct AD (and count bytes constructed). */ +switch (UDF_I_ALLOCTYPE (inode)) {case ICBTAG_FLAG_AD_SHORT :
@@ -1773,6 +1816,8 @@ int8_t udf_write_aext(struct inode *inodreturn -1 ; } +/* [Do something for some good reason.] */ +if (bh) {if (!UDF_QUERY_FLAG (inode->i_sb,UDF_FLAG_STRICT ) ||UDF_SB_UDFREV (inode->i_sb) >=0x0201 )
@@ -1787,8 +1832,13 @@ int8_t udf_write_aext(struct inode *inodelse mark_inode_dirty (inode); +/* [Give some definite meaning to the inc parameter.] */ +if (inc) *extoffset += adsize; + +/* [Succeed by returning nonnegative.] */ +return (elen >>30 ); }