Skip to content

Commit a81f47c

Browse files
fs/ntfs3: Code refactoring
Check functions arguments. Use u8 instead of size_t for ntfs names, more consts and other. Signed-off-by: Konstantin Komarov <[email protected]>
1 parent f037776 commit a81f47c

File tree

7 files changed

+88
-81
lines changed

7 files changed

+88
-81
lines changed

fs/ntfs3/attrlist.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,7 @@ bool al_remove_le(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le)
375375
* al_delete_le - Delete first le from the list which matches its parameters.
376376
*/
377377
bool al_delete_le(struct ntfs_inode *ni, enum ATTR_TYPE type, CLST vcn,
378-
const __le16 *name, size_t name_len,
379-
const struct MFT_REF *ref)
378+
const __le16 *name, u8 name_len, const struct MFT_REF *ref)
380379
{
381380
u16 size;
382381
struct ATTR_LIST_ENTRY *le;

fs/ntfs3/frecord.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi)
384384
* ni_remove_attr - Remove all attributes for the given type/name/id.
385385
*/
386386
int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
387-
const __le16 *name, size_t name_len, bool base_only,
387+
const __le16 *name, u8 name_len, bool base_only,
388388
const __le16 *id)
389389
{
390390
int err;

fs/ntfs3/fsntfs.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,8 @@ int ntfs_vbo_to_lbo(struct ntfs_sb_info *sbi, const struct runs_tree *run,
16611661
return 0;
16621662
}
16631663

1664-
struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST rno, bool dir)
1664+
struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST rno,
1665+
enum RECORD_FLAG flag)
16651666
{
16661667
int err = 0;
16671668
struct super_block *sb = sbi->sb;
@@ -1673,8 +1674,7 @@ struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST rno, bool dir)
16731674

16741675
ni = ntfs_i(inode);
16751676

1676-
err = mi_format_new(&ni->mi, sbi, rno, dir ? RECORD_FLAG_DIR : 0,
1677-
false);
1677+
err = mi_format_new(&ni->mi, sbi, rno, flag, false);
16781678
if (err)
16791679
goto out;
16801680

@@ -1937,7 +1937,7 @@ int ntfs_security_init(struct ntfs_sb_info *sbi)
19371937
break;
19381938

19391939
sii_e = (struct NTFS_DE_SII *)ne;
1940-
if (le16_to_cpu(ne->view.data_size) < SIZEOF_SECURITY_HDR)
1940+
if (le16_to_cpu(ne->view.data_size) < sizeof(sii_e->sec_hdr))
19411941
continue;
19421942

19431943
next_id = le32_to_cpu(sii_e->sec_id) + 1;
@@ -1998,18 +1998,18 @@ int ntfs_get_security_by_id(struct ntfs_sb_info *sbi, __le32 security_id,
19981998
goto out;
19991999

20002000
t32 = le32_to_cpu(sii_e->sec_hdr.size);
2001-
if (t32 < SIZEOF_SECURITY_HDR) {
2001+
if (t32 < sizeof(struct SECURITY_HDR)) {
20022002
err = -EINVAL;
20032003
goto out;
20042004
}
20052005

2006-
if (t32 > SIZEOF_SECURITY_HDR + 0x10000) {
2006+
if (t32 > sizeof(struct SECURITY_HDR) + 0x10000) {
20072007
/* Looks like too big security. 0x10000 - is arbitrary big number. */
20082008
err = -EFBIG;
20092009
goto out;
20102010
}
20112011

2012-
*size = t32 - SIZEOF_SECURITY_HDR;
2012+
*size = t32 - sizeof(struct SECURITY_HDR);
20132013

20142014
p = kmalloc(*size, GFP_NOFS);
20152015
if (!p) {
@@ -2023,14 +2023,14 @@ int ntfs_get_security_by_id(struct ntfs_sb_info *sbi, __le32 security_id,
20232023
if (err)
20242024
goto out;
20252025

2026-
if (memcmp(&d_security, &sii_e->sec_hdr, SIZEOF_SECURITY_HDR)) {
2026+
if (memcmp(&d_security, &sii_e->sec_hdr, sizeof(d_security))) {
20272027
err = -EINVAL;
20282028
goto out;
20292029
}
20302030

20312031
err = ntfs_read_run_nb(sbi, &ni->file.run,
20322032
le64_to_cpu(sii_e->sec_hdr.off) +
2033-
SIZEOF_SECURITY_HDR,
2033+
sizeof(struct SECURITY_HDR),
20342034
p, *size, NULL);
20352035
if (err)
20362036
goto out;
@@ -2069,7 +2069,7 @@ int ntfs_insert_security(struct ntfs_sb_info *sbi,
20692069
struct NTFS_DE_SDH sdh_e;
20702070
struct NTFS_DE_SII sii_e;
20712071
struct SECURITY_HDR *d_security;
2072-
u32 new_sec_size = size_sd + SIZEOF_SECURITY_HDR;
2072+
u32 new_sec_size = size_sd + sizeof(struct SECURITY_HDR);
20732073
u32 aligned_sec_size = ALIGN(new_sec_size, 16);
20742074
struct SECURITY_KEY hash_key;
20752075
struct ntfs_fnd *fnd_sdh = NULL;
@@ -2207,14 +2207,14 @@ int ntfs_insert_security(struct ntfs_sb_info *sbi,
22072207
/* Fill SII entry. */
22082208
sii_e.de.view.data_off =
22092209
cpu_to_le16(offsetof(struct NTFS_DE_SII, sec_hdr));
2210-
sii_e.de.view.data_size = cpu_to_le16(SIZEOF_SECURITY_HDR);
2210+
sii_e.de.view.data_size = cpu_to_le16(sizeof(struct SECURITY_HDR));
22112211
sii_e.de.view.res = 0;
2212-
sii_e.de.size = cpu_to_le16(SIZEOF_SII_DIRENTRY);
2212+
sii_e.de.size = cpu_to_le16(sizeof(struct NTFS_DE_SII));
22132213
sii_e.de.key_size = cpu_to_le16(sizeof(d_security->key.sec_id));
22142214
sii_e.de.flags = 0;
22152215
sii_e.de.res = 0;
22162216
sii_e.sec_id = d_security->key.sec_id;
2217-
memcpy(&sii_e.sec_hdr, d_security, SIZEOF_SECURITY_HDR);
2217+
memcpy(&sii_e.sec_hdr, d_security, sizeof(struct SECURITY_HDR));
22182218

22192219
err = indx_insert_entry(indx_sii, ni, &sii_e.de, NULL, NULL, 0);
22202220
if (err)
@@ -2223,15 +2223,15 @@ int ntfs_insert_security(struct ntfs_sb_info *sbi,
22232223
/* Fill SDH entry. */
22242224
sdh_e.de.view.data_off =
22252225
cpu_to_le16(offsetof(struct NTFS_DE_SDH, sec_hdr));
2226-
sdh_e.de.view.data_size = cpu_to_le16(SIZEOF_SECURITY_HDR);
2226+
sdh_e.de.view.data_size = cpu_to_le16(sizeof(struct SECURITY_HDR));
22272227
sdh_e.de.view.res = 0;
22282228
sdh_e.de.size = cpu_to_le16(SIZEOF_SDH_DIRENTRY);
22292229
sdh_e.de.key_size = cpu_to_le16(sizeof(sdh_e.key));
22302230
sdh_e.de.flags = 0;
22312231
sdh_e.de.res = 0;
22322232
sdh_e.key.hash = d_security->key.hash;
22332233
sdh_e.key.sec_id = d_security->key.sec_id;
2234-
memcpy(&sdh_e.sec_hdr, d_security, SIZEOF_SECURITY_HDR);
2234+
memcpy(&sdh_e.sec_hdr, d_security, sizeof(struct SECURITY_HDR));
22352235
sdh_e.magic[0] = cpu_to_le16('I');
22362236
sdh_e.magic[1] = cpu_to_le16('I');
22372237

@@ -2522,7 +2522,8 @@ void mark_as_free_ex(struct ntfs_sb_info *sbi, CLST lcn, CLST len, bool trim)
25222522
/*
25232523
* run_deallocate - Deallocate clusters.
25242524
*/
2525-
int run_deallocate(struct ntfs_sb_info *sbi, struct runs_tree *run, bool trim)
2525+
int run_deallocate(struct ntfs_sb_info *sbi, const struct runs_tree *run,
2526+
bool trim)
25262527
{
25272528
CLST lcn, len;
25282529
size_t idx = 0;
@@ -2578,13 +2579,13 @@ static inline bool name_has_forbidden_chars(const struct le_str *fname)
25782579
return false;
25792580
}
25802581

2581-
static inline bool is_reserved_name(struct ntfs_sb_info *sbi,
2582+
static inline bool is_reserved_name(const struct ntfs_sb_info *sbi,
25822583
const struct le_str *fname)
25832584
{
25842585
int port_digit;
25852586
const __le16 *name = fname->name;
25862587
int len = fname->len;
2587-
u16 *upcase = sbi->upcase;
2588+
const u16 *upcase = sbi->upcase;
25882589

25892590
/* check for 3 chars reserved names (device names) */
25902591
/* name by itself or with any extension is forbidden */

fs/ntfs3/inode.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,7 @@ struct inode *ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir,
13091309
if (err)
13101310
goto out2;
13111311

1312-
ni = ntfs_new_inode(sbi, ino, fa & FILE_ATTRIBUTE_DIRECTORY);
1312+
ni = ntfs_new_inode(sbi, ino, S_ISDIR(mode) ? RECORD_FLAG_DIR : 0);
13131313
if (IS_ERR(ni)) {
13141314
err = PTR_ERR(ni);
13151315
ni = NULL;
@@ -1437,8 +1437,7 @@ struct inode *ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir,
14371437

14381438
root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
14391439
memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
1440-
root->ihdr.de_off =
1441-
cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
1440+
root->ihdr.de_off = cpu_to_le32(sizeof(struct INDEX_HDR));
14421441
root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
14431442
sizeof(struct NTFS_DE));
14441443
root->ihdr.total = root->ihdr.used;

fs/ntfs3/ntfs.h

Lines changed: 58 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,10 @@ enum RECORD_NUM {
9595
MFT_REC_BITMAP = 6,
9696
MFT_REC_BOOT = 7,
9797
MFT_REC_BADCLUST = 8,
98-
//MFT_REC_QUOTA = 9,
99-
MFT_REC_SECURE = 9, // NTFS 3.0
98+
MFT_REC_SECURE = 9,
10099
MFT_REC_UPCASE = 10,
101-
MFT_REC_EXTEND = 11, // NTFS 3.0
102-
MFT_REC_RESERVED = 11,
100+
MFT_REC_EXTEND = 11,
101+
MFT_REC_RESERVED = 12,
103102
MFT_REC_FREE = 16,
104103
MFT_REC_USER = 24,
105104
};
@@ -109,7 +108,6 @@ enum ATTR_TYPE {
109108
ATTR_STD = cpu_to_le32(0x10),
110109
ATTR_LIST = cpu_to_le32(0x20),
111110
ATTR_NAME = cpu_to_le32(0x30),
112-
// ATTR_VOLUME_VERSION on Nt4
113111
ATTR_ID = cpu_to_le32(0x40),
114112
ATTR_SECURE = cpu_to_le32(0x50),
115113
ATTR_LABEL = cpu_to_le32(0x60),
@@ -118,7 +116,6 @@ enum ATTR_TYPE {
118116
ATTR_ROOT = cpu_to_le32(0x90),
119117
ATTR_ALLOC = cpu_to_le32(0xA0),
120118
ATTR_BITMAP = cpu_to_le32(0xB0),
121-
// ATTR_SYMLINK on Nt4
122119
ATTR_REPARSE = cpu_to_le32(0xC0),
123120
ATTR_EA_INFO = cpu_to_le32(0xD0),
124121
ATTR_EA = cpu_to_le32(0xE0),
@@ -144,6 +141,7 @@ enum FILE_ATTRIBUTE {
144141
FILE_ATTRIBUTE_ENCRYPTED = cpu_to_le32(0x00004000),
145142
FILE_ATTRIBUTE_VALID_FLAGS = cpu_to_le32(0x00007fb7),
146143
FILE_ATTRIBUTE_DIRECTORY = cpu_to_le32(0x10000000),
144+
FILE_ATTRIBUTE_INDEX = cpu_to_le32(0x20000000)
147145
};
148146

149147
static_assert(sizeof(enum FILE_ATTRIBUTE) == 4);
@@ -266,7 +264,7 @@ enum RECORD_FLAG {
266264
RECORD_FLAG_IN_USE = cpu_to_le16(0x0001),
267265
RECORD_FLAG_DIR = cpu_to_le16(0x0002),
268266
RECORD_FLAG_SYSTEM = cpu_to_le16(0x0004),
269-
RECORD_FLAG_UNKNOWN = cpu_to_le16(0x0008),
267+
RECORD_FLAG_INDEX = cpu_to_le16(0x0008),
270268
};
271269

272270
/* MFT Record structure. */
@@ -331,18 +329,18 @@ struct ATTR_NONRESIDENT {
331329
__le64 svcn; // 0x10: Starting VCN of this segment.
332330
__le64 evcn; // 0x18: End VCN of this segment.
333331
__le16 run_off; // 0x20: Offset to packed runs.
334-
// Unit of Compression size for this stream, expressed
335-
// as a log of the cluster size.
332+
// Unit of Compression size for this stream, expressed
333+
// as a log of the cluster size.
336334
//
337-
// 0 means file is not compressed
338-
// 1, 2, 3, and 4 are potentially legal values if the
339-
// stream is compressed, however the implementation
340-
// may only choose to use 4, or possibly 3. Note
341-
// that 4 means cluster size time 16. If convenient
342-
// the implementation may wish to accept a
343-
// reasonable range of legal values here (1-5?),
344-
// even if the implementation only generates
345-
// a smaller set of values itself.
335+
// 0 means file is not compressed
336+
// 1, 2, 3, and 4 are potentially legal values if the
337+
// stream is compressed, however the implementation
338+
// may only choose to use 4, or possibly 3.
339+
// Note that 4 means cluster size time 16.
340+
// If convenient the implementation may wish to accept a
341+
// reasonable range of legal values here (1-5?),
342+
// even if the implementation only generates
343+
// a smaller set of values itself.
346344
u8 c_unit; // 0x22:
347345
u8 res1[5]; // 0x23:
348346
__le64 alloc_size; // 0x28: The allocated size of attribute in bytes.
@@ -836,16 +834,22 @@ static_assert(sizeof(struct ATTR_DEF_ENTRY) == 0xa0);
836834
/* Object ID (0x40) */
837835
struct OBJECT_ID {
838836
struct GUID ObjId; // 0x00: Unique Id assigned to file.
839-
struct GUID BirthVolumeId; // 0x10: Birth Volume Id is the Object Id of the Volume on.
840-
// which the Object Id was allocated. It never changes.
841-
struct GUID BirthObjectId; // 0x20: Birth Object Id is the first Object Id that was
842-
// ever assigned to this MFT Record. I.e. If the Object Id
843-
// is changed for some reason, this field will reflect the
844-
// original value of the Object Id.
845-
struct GUID DomainId; // 0x30: Domain Id is currently unused but it is intended to be
846-
// used in a network environment where the local machine is
847-
// part of a Windows 2000 Domain. This may be used in a Windows
848-
// 2000 Advanced Server managed domain.
837+
838+
// Birth Volume Id is the Object Id of the Volume on.
839+
// which the Object Id was allocated. It never changes.
840+
struct GUID BirthVolumeId; //0x10:
841+
842+
// Birth Object Id is the first Object Id that was
843+
// ever assigned to this MFT Record. I.e. If the Object Id
844+
// is changed for some reason, this field will reflect the
845+
// original value of the Object Id.
846+
struct GUID BirthObjectId; // 0x20:
847+
848+
// Domain Id is currently unused but it is intended to be
849+
// used in a network environment where the local machine is
850+
// part of a Windows 2000 Domain. This may be used in a Windows
851+
// 2000 Advanced Server managed domain.
852+
struct GUID DomainId; // 0x30:
849853
};
850854

851855
static_assert(sizeof(struct OBJECT_ID) == 0x40);
@@ -855,42 +859,45 @@ struct NTFS_DE_O {
855859
struct NTFS_DE de;
856860
struct GUID ObjId; // 0x10: Unique Id assigned to file.
857861
struct MFT_REF ref; // 0x20: MFT record number with this file.
858-
struct GUID BirthVolumeId; // 0x28: Birth Volume Id is the Object Id of the Volume on
859-
// which the Object Id was allocated. It never changes.
860-
struct GUID BirthObjectId; // 0x38: Birth Object Id is the first Object Id that was
861-
// ever assigned to this MFT Record. I.e. If the Object Id
862-
// is changed for some reason, this field will reflect the
863-
// original value of the Object Id.
864-
// This field is valid if data_size == 0x48.
865-
struct GUID BirthDomainId; // 0x48: Domain Id is currently unused but it is intended
866-
// to be used in a network environment where the local
867-
// machine is part of a Windows 2000 Domain. This may be
868-
// used in a Windows 2000 Advanced Server managed domain.
862+
863+
// Birth Volume Id is the Object Id of the Volume on
864+
// which the Object Id was allocated. It never changes.
865+
struct GUID BirthVolumeId; // 0x28:
866+
867+
// Birth Object Id is the first Object Id that was
868+
// ever assigned to this MFT Record. I.e. If the Object Id
869+
// is changed for some reason, this field will reflect the
870+
// original value of the Object Id.
871+
// This field is valid if data_size == 0x48.
872+
struct GUID BirthObjectId; // 0x38:
873+
874+
// Domain Id is currently unused but it is intended
875+
// to be used in a network environment where the local
876+
// machine is part of a Windows 2000 Domain. This may be
877+
// used in a Windows 2000 Advanced Server managed domain.
878+
struct GUID BirthDomainId; // 0x48:
869879
};
870880

871881
static_assert(sizeof(struct NTFS_DE_O) == 0x58);
872882

873-
#define NTFS_OBJECT_ENTRY_DATA_SIZE1 \
874-
0x38 // struct NTFS_DE_O.BirthDomainId is not used
875-
#define NTFS_OBJECT_ENTRY_DATA_SIZE2 \
876-
0x48 // struct NTFS_DE_O.BirthDomainId is used
877-
878883
/* Q Directory entry structure ( rule = 0x11 ) */
879884
struct NTFS_DE_Q {
880885
struct NTFS_DE de;
881886
__le32 owner_id; // 0x10: Unique Id assigned to file
887+
888+
/* here is 0x30 bytes of user quota. NOTE: 4 byte aligned! */
882889
__le32 Version; // 0x14: 0x02
883-
__le32 flags2; // 0x18: Quota flags, see above
890+
__le32 Flags; // 0x18: Quota flags, see above
884891
__le64 BytesUsed; // 0x1C:
885892
__le64 ChangeTime; // 0x24:
886893
__le64 WarningLimit; // 0x28:
887894
__le64 HardLimit; // 0x34:
888895
__le64 ExceededTime; // 0x3C:
889896

890897
// SID is placed here
891-
}; // sizeof() = 0x44
898+
}__packed; // sizeof() = 0x44
892899

893-
#define SIZEOF_NTFS_DE_Q 0x44
900+
static_assert(sizeof(struct NTFS_DE_Q) == 0x44);
894901

895902
#define SecurityDescriptorsBlockSize 0x40000 // 256K
896903
#define SecurityDescriptorMaxSize 0x20000 // 128K
@@ -912,7 +919,7 @@ struct SECURITY_HDR {
912919
*/
913920
} __packed;
914921

915-
#define SIZEOF_SECURITY_HDR 0x14
922+
static_assert(sizeof(struct SECURITY_HDR) == 0x14);
916923

917924
/* SII Directory entry structure */
918925
struct NTFS_DE_SII {
@@ -921,7 +928,8 @@ struct NTFS_DE_SII {
921928
struct SECURITY_HDR sec_hdr; // 0x14:
922929
} __packed;
923930

924-
#define SIZEOF_SII_DIRENTRY 0x28
931+
static_assert(offsetof(struct NTFS_DE_SII, sec_hdr) == 0x14);
932+
static_assert(sizeof(struct NTFS_DE_SII) == 0x28);
925933

926934
/* SDH Directory entry structure */
927935
struct NTFS_DE_SDH {
@@ -1155,7 +1163,7 @@ struct REPARSE_DATA_BUFFER {
11551163

11561164
#define FILE_NEED_EA 0x80 // See ntifs.h
11571165
/*
1158-
*FILE_NEED_EA, indicates that the file to which the EA belongs cannot be
1166+
* FILE_NEED_EA, indicates that the file to which the EA belongs cannot be
11591167
* interpreted without understanding the associated extended attributes.
11601168
*/
11611169
struct EA_INFO {

0 commit comments

Comments
 (0)