fs/ntfs3: Use kernel ALIGN macros over driver specific

The static checkers (Smatch) were complaining because QuadAlign() was
buggy.  If you try to align something higher than UINT_MAX it got
truncated to a u32.

Smatch warning was:
	fs/ntfs3/attrib.c:383 attr_set_size_res()
	warn: was expecting a 64 bit value instead of '~7'

So that this will not happen again we will change all these macros to
kernel made ones. This can also help some other static analyzing tools
to give us better warnings.

Patch was generated with Coccinelle script and after that some style
issue was hand fixed.

Coccinelle script:

virtual patch

@alloc depends on patch@
expression x;
@@
(
-	#define QuadAlign(n)		(((n) + 7u) & (~7u))
|
-	QuadAlign(x)
+	ALIGN(x, 8)
|
-	#define IsQuadAligned(n)	(!((size_t)(n)&7u))
|
-	IsQuadAligned(x)
+	IS_ALIGNED(x, 8)
|
-	#define Quad2Align(n)		(((n) + 15u) & (~15u))
|
-	Quad2Align(x)
+	ALIGN(x, 16)
|
-	#define IsQuad2Aligned(n)	(!((size_t)(n)&15u))
|
-	IsQuad2Aligned(x)
+	IS_ALIGNED(x, 16)
|
-	#define Quad4Align(n)		(((n) + 31u) & (~31u))
|
-	Quad4Align(x)
+	ALIGN(x, 32)
|
-	#define IsSizeTAligned(n)	(!((size_t)(n) & (sizeof(size_t) - 1)))
|
-	IsSizeTAligned(x)
+	IS_ALIGNED(x, sizeof(size_t))
|
-	#define DwordAlign(n)		(((n) + 3u) & (~3u))
|
-	DwordAlign(x)
+	ALIGN(x, 4)
|
-	#define IsDwordAligned(n)	(!((size_t)(n)&3u))
|
-	IsDwordAligned(x)
+	IS_ALIGNED(x, 4)
|
-	#define WordAlign(n)		(((n) + 1u) & (~1u))
|
-	WordAlign(x)
+	ALIGN(x, 2)
|
-	#define IsWordAligned(n)	(!((size_t)(n)&1u))
|
-	IsWordAligned(x)
+	IS_ALIGNED(x, 2)
|
)

Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Kari Argillander <kari.argillander@gmail.com>
Signed-off-by: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
This commit is contained in:
Kari Argillander 2021-08-26 11:56:29 +03:00 committed by Konstantin Komarov
parent 24516d481d
commit fa3cacf544
No known key found for this signature in database
GPG Key ID: A9B0331F832407B6
13 changed files with 56 additions and 67 deletions

View File

@ -380,7 +380,7 @@ static int attr_set_size_res(struct ntfs_inode *ni, struct ATTRIB *attr,
u32 rsize = le32_to_cpu(attr->res.data_size);
u32 tail = used - aoff - asize;
char *next = Add2Ptr(attr, asize);
s64 dsize = QuadAlign(new_size) - QuadAlign(rsize);
s64 dsize = ALIGN(new_size, 8) - ALIGN(rsize, 8);
if (dsize < 0) {
memmove(next + dsize, next, tail);

View File

@ -15,17 +15,6 @@
#define PtrOffset(B, O) ((size_t)((size_t)(O) - (size_t)(B)))
#endif
#define QuadAlign(n) (((n) + 7u) & (~7u))
#define IsQuadAligned(n) (!((size_t)(n)&7u))
#define Quad2Align(n) (((n) + 15u) & (~15u))
#define IsQuad2Aligned(n) (!((size_t)(n)&15u))
#define Quad4Align(n) (((n) + 31u) & (~31u))
#define IsSizeTAligned(n) (!((size_t)(n) & (sizeof(size_t) - 1)))
#define DwordAlign(n) (((n) + 3u) & (~3u))
#define IsDwordAligned(n) (!((size_t)(n)&3u))
#define WordAlign(n) (((n) + 1u) & (~1u))
#define IsWordAligned(n) (!((size_t)(n)&1u))
#ifdef CONFIG_PRINTK
__printf(2, 3)
void ntfs_printk(const struct super_block *sb, const char *fmt, ...);

View File

@ -1249,7 +1249,7 @@ static int ni_expand_mft_list(struct ntfs_inode *ni)
if (err < 0)
goto out;
run_size = QuadAlign(err);
run_size = ALIGN(err, 8);
err = 0;
if (plen < svcn) {
@ -1269,7 +1269,7 @@ static int ni_expand_mft_list(struct ntfs_inode *ni)
if (err < 0)
goto out;
run_size = QuadAlign(err);
run_size = ALIGN(err, 8);
err = 0;
if (plen < evcn + 1 - svcn) {
@ -1392,7 +1392,7 @@ int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
struct ATTRIB *attr;
bool is_ext =
(flags & (ATTR_FLAG_SPARSED | ATTR_FLAG_COMPRESSED)) && !svcn;
u32 name_size = QuadAlign(name_len * sizeof(short));
u32 name_size = ALIGN(name_len * sizeof(short), 8);
u32 name_off = is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT;
u32 run_off = name_off + name_size;
u32 run_size, asize;
@ -1403,7 +1403,7 @@ int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
if (err < 0)
goto out;
run_size = QuadAlign(err);
run_size = ALIGN(err, 8);
if (plen < len) {
err = -EINVAL;
@ -1463,8 +1463,8 @@ int ni_insert_resident(struct ntfs_inode *ni, u32 data_size,
struct ATTRIB **new_attr, struct mft_inode **mi)
{
int err;
u32 name_size = QuadAlign(name_len * sizeof(short));
u32 asize = SIZEOF_RESIDENT + name_size + QuadAlign(data_size);
u32 name_size = ALIGN(name_len * sizeof(short), 8);
u32 asize = SIZEOF_RESIDENT + name_size + ALIGN(data_size, 8);
struct ATTRIB *attr;
err = ni_insert_attr(ni, type, name, name_len, asize, SIZEOF_RESIDENT,
@ -2853,7 +2853,7 @@ static bool ni_update_parent(struct ntfs_inode *ni, struct NTFS_DUP_INFO *dup,
} else if (!attr->non_res) {
u32 data_size = le32_to_cpu(attr->res.data_size);
dup->alloc_size = cpu_to_le64(QuadAlign(data_size));
dup->alloc_size = cpu_to_le64(ALIGN(data_size, 8));
dup->data_size = cpu_to_le64(data_size);
} else {
u64 new_valid = ni->i_valid;

View File

@ -456,7 +456,7 @@ static inline bool is_rst_page_hdr_valid(u32 file_off,
return false;
ro = le16_to_cpu(rhdr->ra_off);
if (!IsQuadAligned(ro) || ro > sys_page)
if (!IS_ALIGNED(ro, 8) || ro > sys_page)
return false;
end_usa = ((sys_page >> SECTOR_SHIFT) + 1) * sizeof(short);
@ -488,7 +488,7 @@ static inline bool is_rst_area_valid(const struct RESTART_HDR *rhdr)
off = le16_to_cpu(ra->client_off);
if (!IsQuadAligned(off) || ro + off > SECTOR_SIZE - sizeof(short))
if (!IS_ALIGNED(off, 8) || ro + off > SECTOR_SIZE - sizeof(short))
return false;
off += cl * sizeof(struct CLIENT_REC);
@ -526,8 +526,8 @@ static inline bool is_rst_area_valid(const struct RESTART_HDR *rhdr)
}
/* The log page data offset and record header length must be quad-aligned */
if (!IsQuadAligned(le16_to_cpu(ra->data_off)) ||
!IsQuadAligned(le16_to_cpu(ra->rec_hdr_len)))
if (!IS_ALIGNED(le16_to_cpu(ra->data_off), 8) ||
!IS_ALIGNED(le16_to_cpu(ra->rec_hdr_len), 8))
return false;
return true;
@ -1355,9 +1355,9 @@ static void log_create(struct ntfs_log *log, u32 l_size, const u64 last_lsn,
log->l_flags |= NTFSLOG_MULTIPLE_PAGE_IO;
/* Compute the log page values */
log->data_off = QuadAlign(
log->data_off = ALIGN(
offsetof(struct RECORD_PAGE_HDR, fixups) +
sizeof(short) * ((log->page_size >> SECTOR_SHIFT) + 1));
sizeof(short) * ((log->page_size >> SECTOR_SHIFT) + 1), 8);
log->data_size = log->page_size - log->data_off;
log->record_header_len = sizeof(struct LFS_RECORD_HDR);
@ -1365,9 +1365,9 @@ static void log_create(struct ntfs_log *log, u32 l_size, const u64 last_lsn,
log->reserved = log->data_size - log->record_header_len;
/* Compute the restart page values. */
log->ra_off = QuadAlign(
log->ra_off = ALIGN(
offsetof(struct RESTART_HDR, fixups) +
sizeof(short) * ((log->sys_page_size >> SECTOR_SHIFT) + 1));
sizeof(short) * ((log->sys_page_size >> SECTOR_SHIFT) + 1), 8);
log->restart_size = log->sys_page_size - log->ra_off;
log->ra_size = struct_size(log->ra, clients, 1);
log->current_openlog_count = open_log_count;
@ -1496,7 +1496,7 @@ static int next_log_lsn(struct ntfs_log *log, const struct LFS_RECORD_HDR *rh,
vbo = hdr_off + log->data_off;
} else {
vbo = QuadAlign(end);
vbo = ALIGN(end, 8);
}
/* Compute the lsn based on the file offset and the sequence count */
@ -2982,7 +2982,7 @@ static struct ATTRIB *attr_create_nonres_log(struct ntfs_sb_info *sbi,
__le16 flags)
{
struct ATTRIB *attr;
u32 name_size = QuadAlign(name_len * sizeof(short));
u32 name_size = ALIGN(name_len * sizeof(short), 8);
bool is_ext = flags & (ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED);
u32 asize = name_size +
(is_ext ? SIZEOF_NONRESIDENT_EX : SIZEOF_NONRESIDENT);
@ -3220,7 +3220,7 @@ skip_load_parent:
goto dirty_vol;
memmove(attr, attr2, dlen);
rec->used = cpu_to_le32(QuadAlign(roff + dlen));
rec->used = cpu_to_le32(ALIGN(roff + dlen, 8));
mi->dirty = true;
break;
@ -3231,7 +3231,7 @@ skip_load_parent:
used = le32_to_cpu(rec->used);
if (!check_if_attr(rec, lrh) || dlen < SIZEOF_RESIDENT ||
!IsQuadAligned(asize) ||
!IS_ALIGNED(asize, 8) ||
Add2Ptr(attr2, asize) > Add2Ptr(lrh, rec_len) ||
dlen > record_size - used) {
goto dirty_vol;
@ -3296,7 +3296,7 @@ skip_load_parent:
if (nsize > asize && nsize - asize > record_size - used)
goto dirty_vol;
nsize = QuadAlign(nsize);
nsize = ALIGN(nsize, 8);
data_off = le16_to_cpu(attr->res.data_off);
if (nsize < asize) {
@ -3341,7 +3341,7 @@ move_data:
goto dirty_vol;
}
nsize = QuadAlign(nsize);
nsize = ALIGN(nsize, 8);
memmove(Add2Ptr(attr, nsize), Add2Ptr(attr, asize),
used - le16_to_cpu(lrh->record_off) - asize);
@ -5103,8 +5103,8 @@ end_reply:
rh->sys_page_size = cpu_to_le32(log->page_size);
rh->page_size = cpu_to_le32(log->page_size);
t16 = QuadAlign(offsetof(struct RESTART_HDR, fixups) +
sizeof(short) * t16);
t16 = ALIGN(offsetof(struct RESTART_HDR, fixups) +
sizeof(short) * t16, 8);
rh->ra_off = cpu_to_le16(t16);
rh->minor_ver = cpu_to_le16(1); // 0x1A:
rh->major_ver = cpu_to_le16(1); // 0x1C:

View File

@ -1944,7 +1944,7 @@ int ntfs_security_init(struct ntfs_sb_info *sbi)
sbi->security.next_id = SECURITY_ID_FIRST;
/* Always write new security at the end of bucket */
sbi->security.next_off =
Quad2Align(sds_size - SecurityDescriptorsBlockSize);
ALIGN(sds_size - SecurityDescriptorsBlockSize, 16);
off = 0;
ne = NULL;
@ -2096,7 +2096,7 @@ int ntfs_insert_security(struct ntfs_sb_info *sbi,
struct NTFS_DE_SII sii_e;
struct SECURITY_HDR *d_security;
u32 new_sec_size = size_sd + SIZEOF_SECURITY_HDR;
u32 aligned_sec_size = Quad2Align(new_sec_size);
u32 aligned_sec_size = ALIGN(new_sec_size, 16);
struct SECURITY_KEY hash_key;
struct ntfs_fnd *fnd_sdh = NULL;
const struct INDEX_ROOT *root_sdh;

View File

@ -702,7 +702,7 @@ next1:
if (max_idx >= nslots) {
u16 *ptr;
int new_slots = QuadAlign(2 * nslots);
int new_slots = ALIGN(2 * nslots, 8);
ptr = ntfs_malloc(sizeof(u16) * new_slots);
if (ptr)
@ -959,7 +959,7 @@ static struct indx_node *indx_new(struct ntfs_index *indx,
index->rhdr.fix_num = cpu_to_le16(fn);
index->vbn = cpu_to_le64(vbn);
hdr = &index->ihdr;
eo = QuadAlign(sizeof(struct INDEX_BUFFER) + fn * sizeof(short));
eo = ALIGN(sizeof(struct INDEX_BUFFER) + fn * sizeof(short), 8);
hdr->de_off = cpu_to_le32(eo);
e = Add2Ptr(hdr, eo);

View File

@ -1335,7 +1335,7 @@ struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
fname->dup.ea_size = fname->dup.reparse = 0;
dsize = le16_to_cpu(new_de->key_size);
asize = QuadAlign(SIZEOF_RESIDENT + dsize);
asize = ALIGN(SIZEOF_RESIDENT + dsize, 8);
attr->type = ATTR_NAME;
attr->size = cpu_to_le32(asize);
@ -1349,7 +1349,7 @@ struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
if (security_id == SECURITY_ID_INVALID) {
/* Insert security attribute */
asize = SIZEOF_RESIDENT + QuadAlign(sd_size);
asize = SIZEOF_RESIDENT + ALIGN(sd_size, 8);
attr->type = ATTR_SECURE;
attr->size = cpu_to_le32(asize);
@ -1472,7 +1472,7 @@ struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
attr->id = cpu_to_le16(aid++);
/* resident or non resident? */
asize = QuadAlign(SIZEOF_RESIDENT + nsize);
asize = ALIGN(SIZEOF_RESIDENT + nsize, 8);
t16 = PtrOffset(rec, attr);
if (asize + t16 + 8 > sbi->record_size) {
@ -1508,7 +1508,7 @@ struct inode *ntfs_create_inode(struct user_namespace *mnt_userns,
goto out5;
}
asize = SIZEOF_NONRESIDENT + QuadAlign(err);
asize = SIZEOF_NONRESIDENT + ALIGN(err, 8);
inode->i_size = nsize;
} else {
attr->res.data_off = SIZEOF_RESIDENT_LE;

View File

@ -57,7 +57,7 @@ int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
fname->type = FILE_NAME_POSIX;
data_size = fname_full_size(fname);
e->size = cpu_to_le16(QuadAlign(data_size) + sizeof(struct NTFS_DE));
e->size = cpu_to_le16(ALIGN(data_size, 8) + sizeof(struct NTFS_DE));
e->key_size = cpu_to_le16(data_size);
e->flags = 0;
e->res = 0;

View File

@ -392,8 +392,8 @@ static inline u64 attr_ondisk_size(const struct ATTRIB *attr)
return attr->non_res ? ((attr->flags &
(ATTR_FLAG_COMPRESSED | ATTR_FLAG_SPARSED)) ?
le64_to_cpu(attr->nres.total_size) :
le64_to_cpu(attr->nres.alloc_size)) :
QuadAlign(le32_to_cpu(attr->res.data_size));
le64_to_cpu(attr->nres.alloc_size))
: ALIGN(le32_to_cpu(attr->res.data_size), 8);
}
static inline u64 attr_size(const struct ATTRIB *attr)
@ -529,8 +529,8 @@ static_assert(sizeof(struct ATTR_LIST_ENTRY) == 0x20);
static inline u32 le_size(u8 name_len)
{
return QuadAlign(offsetof(struct ATTR_LIST_ENTRY, name) +
name_len * sizeof(short));
return ALIGN(offsetof(struct ATTR_LIST_ENTRY, name) +
name_len * sizeof(short), 8);
}
/* returns 0 if 'attr' has the same type and name */
@ -691,10 +691,10 @@ static inline bool de_has_vcn_ex(const struct NTFS_DE *e)
sizeof(__le64)));
}
#define MAX_BYTES_PER_NAME_ENTRY \
QuadAlign(sizeof(struct NTFS_DE) + \
offsetof(struct ATTR_FILE_NAME, name) + \
NTFS_NAME_LEN * sizeof(short))
#define MAX_BYTES_PER_NAME_ENTRY \
ALIGN(sizeof(struct NTFS_DE) + \
offsetof(struct ATTR_FILE_NAME, name) + \
NTFS_NAME_LEN * sizeof(short), 8)
struct INDEX_HDR {
__le32 de_off; // 0x00: The offset from the start of this structure

View File

@ -900,7 +900,7 @@ static inline bool run_is_empty(struct runs_tree *run)
/* NTFS uses quad aligned bitmaps */
static inline size_t bitmap_size(size_t bits)
{
return QuadAlign((bits + 7) >> 3);
return ALIGN((bits + 7) >> 3, 8);
}
#define _100ns2seconds 10000000

View File

@ -206,7 +206,7 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
return NULL;
if (off >= used || off < MFTRECORD_FIXUP_OFFSET_1 ||
!IsDwordAligned(off)) {
!IS_ALIGNED(off, 4)) {
return NULL;
}
@ -235,7 +235,7 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr)
/* Can we use the first field (attr->type) */
if (off + 8 > used) {
static_assert(QuadAlign(sizeof(enum ATTR_TYPE)) == 8);
static_assert(ALIGN(sizeof(enum ATTR_TYPE), 8) == 8);
return NULL;
}
@ -539,7 +539,7 @@ bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes)
next = Add2Ptr(attr, asize);
if (bytes > 0) {
dsize = QuadAlign(bytes);
dsize = ALIGN(bytes, 8);
if (used + dsize > total)
return false;
nsize = asize + dsize;
@ -549,7 +549,7 @@ bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes)
used += dsize;
rsize += dsize;
} else {
dsize = QuadAlign(-bytes);
dsize = ALIGN(-bytes, 8);
if (dsize > asize)
return false;
nsize = asize - dsize;
@ -596,7 +596,7 @@ int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
return err;
}
new_run_size = QuadAlign(err);
new_run_size = ALIGN(err, 8);
memmove(next + new_run_size - run_size, next + dsize, tail);

View File

@ -809,9 +809,9 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
sbi->attr_size_tr = (5 * record_size >> 4); // ~320 bytes
sbi->max_bytes_per_attr =
record_size - QuadAlign(MFTRECORD_FIXUP_OFFSET_1) -
QuadAlign(((record_size >> SECTOR_SHIFT) * sizeof(short))) -
QuadAlign(sizeof(enum ATTR_TYPE));
record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
ALIGN(sizeof(enum ATTR_TYPE), 8);
sbi->index_size = boot->index_size < 0
? 1u << (-boot->index_size)
@ -859,9 +859,9 @@ static int ntfs_init_from_boot(struct super_block *sb, u32 sector_size,
rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
rec->rhdr.fix_num = cpu_to_le16(fn);
ao = QuadAlign(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn);
ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
rec->attr_off = cpu_to_le16(ao);
rec->used = cpu_to_le32(ao + QuadAlign(sizeof(enum ATTR_TYPE)));
rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
rec->total = cpu_to_le32(sbi->record_size);
((struct ATTRIB *)Add2Ptr(rec, ao))->type = ATTR_END;

View File

@ -26,9 +26,9 @@
static inline size_t unpacked_ea_size(const struct EA_FULL *ea)
{
return ea->size ? le32_to_cpu(ea->size)
: DwordAlign(struct_size(
ea, name,
1 + ea->name_len + le16_to_cpu(ea->elength)));
: ALIGN(struct_size(
ea, name,
1 + ea->name_len + le16_to_cpu(ea->elength)), 4);
}
static inline size_t packed_ea_size(const struct EA_FULL *ea)
@ -289,7 +289,7 @@ static noinline int ntfs_set_ea(struct inode *inode, const char *name,
goto out;
}
add = DwordAlign(struct_size(ea_all, name, 1 + name_len + val_size));
add = ALIGN(struct_size(ea_all, name, 1 + name_len + val_size), 4);
err = ntfs_read_ea(ni, &ea_all, add, &info);
if (err)