Unverified Commit fa3cacf5 authored by Kari Argillander's avatar Kari Argillander Committed by Konstantin Komarov
Browse files

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: default avatarDan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: default avatarKari Argillander <kari.argillander@gmail.com>
Signed-off-by: default avatarKonstantin Komarov <almaz.alexandrovich@paragon-software.com>
parent 24516d48
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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);
+0 −11
Original line number Diff line number Diff line
@@ -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, ...);
+7 −7
Original line number Diff line number Diff line
@@ -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;
+16 −16
Original line number Diff line number Diff line
@@ -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 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
			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 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
		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 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
		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 @@ static int do_action(struct ntfs_log *log, struct OPEN_ATTR_ENRTY *oe,
			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 @@ int log_replay(struct ntfs_inode *ni, bool *initialized)
	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:
+2 −2
Original line number Diff line number Diff line
@@ -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;
Loading