2023-05-08 12:47:28

by Konstantin Komarov

[permalink] [raw]
Subject: [PATCH 00/10] Refactoring and bugfix

This series contains various fixes and refactoring for ntfs3.
The loading has been slightly revised.
Added support /proc/fs/ntfs3/<dev>/volinfo and /proc/fs/ntfs3/<dev>/label.

Konstantin Komarov (10):
  fs/ntfs3: Correct checking while generating attr_list
  fs/ntfs3: Fix ntfs_atomic_open
  fs/ntfs3: Mark ntfs dirty when on-disk struct is corrupted
  fs/ntfs3: Alternative boot if primary boot is corrupted
  fs/ntfs3: Do not update primary boot in ntfs_init_from_boot()
  fs/ntfs3: Code formatting
  fs/ntfs3: Code refactoring
  fs/ntfs3: Add ability to format new mft records with bigger/smaller
    header
  fs/ntfs3: Fix endian problem
  fs/ntfs3: Add support /proc/fs/ntfs3/<dev>/volinfo and
    /proc/fs/ntfs3/<dev>/label

 fs/ntfs3/attrib.c   |   2 +-
 fs/ntfs3/attrlist.c |   3 +-
 fs/ntfs3/bitmap.c   |  10 +-
 fs/ntfs3/file.c     |   4 +-
 fs/ntfs3/frecord.c  |  54 +++++----
 fs/ntfs3/fslog.c    |  40 +++----
 fs/ntfs3/fsntfs.c   |  99 ++++++++++++----
 fs/ntfs3/index.c    |  20 ++--
 fs/ntfs3/inode.c    |  23 ++--
 fs/ntfs3/lznt.c     |   6 +-
 fs/ntfs3/namei.c    |  31 ++---
 fs/ntfs3/ntfs.h     | 117 +++++++++++--------
 fs/ntfs3/ntfs_fs.h  |  31 ++---
 fs/ntfs3/record.c   |  10 +-
 fs/ntfs3/run.c      |   4 +-
 fs/ntfs3/super.c    | 279 ++++++++++++++++++++++++++++++++++++++------
 fs/ntfs3/xattr.c    |  16 ++-
 17 files changed, 516 insertions(+), 233 deletions(-)


2023-05-08 12:49:26

by Konstantin Komarov

[permalink] [raw]
Subject: [PATCH 04/10] fs/ntfs3: Alternative boot if primary boot is corrupted

Some code refactoring added also.

Signed-off-by: Konstantin Komarov <[email protected]>
---
 fs/ntfs3/super.c | 98 +++++++++++++++++++++++++++++++++++-------------
 1 file changed, 71 insertions(+), 27 deletions(-)

diff --git a/fs/ntfs3/super.c b/fs/ntfs3/super.c
index 5158dd31fd97..ecf899d571d8 100644
--- a/fs/ntfs3/super.c
+++ b/fs/ntfs3/super.c
@@ -724,6 +724,8 @@ static int ntfs_init_from_boot(struct super_block
*sb, u32 sector_size,
     struct MFT_REC *rec;
     u16 fn, ao;
     u8 cluster_bits;
+    u32 boot_off = 0;
+    const char *hint = "Primary boot";

     sbi->volume.blocks = dev_size >> PAGE_SHIFT;

@@ -731,11 +733,12 @@ static int ntfs_init_from_boot(struct super_block
*sb, u32 sector_size,
     if (!bh)
         return -EIO;

+check_boot:
     err = -EINVAL;
-    boot = (struct NTFS_BOOT *)bh->b_data;
+    boot = (struct NTFS_BOOT *)Add2Ptr(bh->b_data, boot_off);

     if (memcmp(boot->system_id, "NTFS    ", sizeof("NTFS    ") - 1)) {
-        ntfs_err(sb, "Boot's signature is not NTFS.");
+        ntfs_err(sb, "%s signature is not NTFS.", hint);
         goto out;
     }

@@ -748,14 +751,16 @@ static int ntfs_init_from_boot(struct super_block
*sb, u32 sector_size,
                boot->bytes_per_sector[0];
     if (boot_sector_size < SECTOR_SIZE ||
         !is_power_of_2(boot_sector_size)) {
-        ntfs_err(sb, "Invalid bytes per sector %u.", boot_sector_size);
+        ntfs_err(sb, "%s: invalid bytes per sector %u.", hint,
+             boot_sector_size);
         goto out;
     }

     /* cluster size: 512, 1K, 2K, 4K, ... 2M */
     sct_per_clst = true_sectors_per_clst(boot);
     if ((int)sct_per_clst < 0 || !is_power_of_2(sct_per_clst)) {
-        ntfs_err(sb, "Invalid sectors per cluster %u.", sct_per_clst);
+        ntfs_err(sb, "%s: invalid sectors per cluster %u.", hint,
+             sct_per_clst);
         goto out;
     }

@@ -771,8 +776,8 @@ static int ntfs_init_from_boot(struct super_block
*sb, u32 sector_size,
     if (mlcn * sct_per_clst >= sectors || mlcn2 * sct_per_clst >=
sectors) {
         ntfs_err(
             sb,
-            "Start of MFT 0x%llx (0x%llx) is out of volume 0x%llx.",
-            mlcn, mlcn2, sectors);
+            "%s: start of MFT 0x%llx (0x%llx) is out of volume 0x%llx.",
+            hint, mlcn, mlcn2, sectors);
         goto out;
     }

@@ -784,7 +789,7 @@ static int ntfs_init_from_boot(struct super_block
*sb, u32 sector_size,

     /* Check MFT record size. */
     if (record_size < SECTOR_SIZE || !is_power_of_2(record_size)) {
-        ntfs_err(sb, "Invalid bytes per MFT record %u (%d).",
+        ntfs_err(sb, "%s: invalid bytes per MFT record %u (%d).", hint,
              record_size, boot->record_size);
         goto out;
     }
@@ -801,13 +806,13 @@ static int ntfs_init_from_boot(struct super_block
*sb, u32 sector_size,

     /* Check index record size. */
     if (sbi->index_size < SECTOR_SIZE ||
!is_power_of_2(sbi->index_size)) {
-        ntfs_err(sb, "Invalid bytes per index %u(%d).", sbi->index_size,
-             boot->index_size);
+        ntfs_err(sb, "%s: invalid bytes per index %u(%d).", hint,
+             sbi->index_size, boot->index_size);
         goto out;
     }

     if (sbi->index_size > MAXIMUM_BYTES_PER_INDEX) {
-        ntfs_err(sb, "Unsupported bytes per index %u.",
+        ntfs_err(sb, "%s: unsupported bytes per index %u.", hint,
              sbi->index_size);
         goto out;
     }
@@ -834,7 +839,7 @@ static int ntfs_init_from_boot(struct super_block
*sb, u32 sector_size,

     /* Compare boot's cluster and sector. */
     if (sbi->cluster_size < boot_sector_size) {
-        ntfs_err(sb, "Invalid bytes per cluster (%u).",
+        ntfs_err(sb, "%s: invalid bytes per cluster (%u).", hint,
              sbi->cluster_size);
         goto out;
     }
@@ -930,7 +935,46 @@ static int ntfs_init_from_boot(struct super_block
*sb, u32 sector_size,

     err = 0;

+    if (bh->b_blocknr && !sb_rdonly(sb)) {
+        /*
+          * Alternative boot is ok but primary is not ok.
+          * Update primary boot.
+         */
+        struct buffer_head *bh0 = sb_getblk(sb, 0);
+        if (bh0) {
+            if (buffer_locked(bh0))
+                __wait_on_buffer(bh0);
+
+            lock_buffer(bh0);
+            memcpy(bh0->b_data, boot, sizeof(*boot));
+            set_buffer_uptodate(bh0);
+            mark_buffer_dirty(bh0);
+            unlock_buffer(bh0);
+            if (!sync_dirty_buffer(bh0))
+                ntfs_warn(sb, "primary boot is updated");
+            put_bh(bh0);
+        }
+    }
+
 out:
+    if (err == -EINVAL && !bh->b_blocknr && dev_size > PAGE_SHIFT) {
+        u32 block_size = min_t(u32, sector_size, PAGE_SIZE);
+        u64 lbo = dev_size - sizeof(*boot);
+
+        /*
+          * Try alternative boot (last sector)
+         */
+        brelse(bh);
+
+        sb_set_blocksize(sb, block_size);
+        bh = ntfs_bread(sb, lbo >> blksize_bits(block_size));
+        if (!bh)
+            return -EINVAL;
+
+        boot_off = lbo & (block_size - 1);
+        hint = "Alternative boot";
+        goto check_boot;
+    }
     brelse(bh);

     return err;
@@ -955,6 +999,7 @@ static int ntfs_fill_super(struct super_block *sb,
struct fs_context *fc)
     struct ATTR_DEF_ENTRY *t;
     u16 *shared;
     struct MFT_REF ref;
+    bool ro = sb_rdonly(sb);

     ref.high = 0;

@@ -1035,6 +1080,10 @@ static int ntfs_fill_super(struct super_block
*sb, struct fs_context *fc)
     sbi->volume.minor_ver = info->minor_ver;
     sbi->volume.flags = info->flags;
     sbi->volume.ni = ni;
+    if (info->flags & VOLUME_FLAG_DIRTY) {
+        sbi->volume.real_dirty = true;
+        ntfs_info(sb, "It is recommened to use chkdsk.");
+    }

     /* Load $MFTMirr to estimate recs_mirr. */
     ref.low = cpu_to_le32(MFT_REC_MIRR);
@@ -1069,21 +1118,16 @@ static int ntfs_fill_super(struct super_block
*sb, struct fs_context *fc)

     iput(inode);

-    if (sbi->flags & NTFS_FLAGS_NEED_REPLAY) {
-        if (!sb_rdonly(sb)) {
-            ntfs_warn(sb,
-                  "failed to replay log file. Can't mount rw!");
-            err = -EINVAL;
-            goto out;
-        }
-    } else if (sbi->volume.flags & VOLUME_FLAG_DIRTY) {
-        if (!sb_rdonly(sb) && !options->force) {
-            ntfs_warn(
-                sb,
-                "volume is dirty and \"force\" flag is not set!");
-            err = -EINVAL;
-            goto out;
-        }
+    if ((sbi->flags & NTFS_FLAGS_NEED_REPLAY) && !ro) {
+        ntfs_warn(sb, "failed to replay log file. Can't mount rw!");
+        err = -EINVAL;
+        goto out;
+    }
+
+    if ((sbi->volume.flags & VOLUME_FLAG_DIRTY) && !ro &&
!options->force) {
+        ntfs_warn(sb, "volume is dirty and \"force\" flag is not set!");
+        err = -EINVAL;
+        goto out;
     }

     /* Load $MFT. */
@@ -1173,7 +1217,7 @@ static int ntfs_fill_super(struct super_block *sb,
struct fs_context *fc)

         bad_len += len;
         bad_frags += 1;
-        if (sb_rdonly(sb))
+        if (ro)
             continue;

         if (wnd_set_used_safe(&sbi->used.bitmap, lcn, len, &tt) || tt) {
--
2.34.1

2023-05-08 12:53:42

by Konstantin Komarov

[permalink] [raw]
Subject: [PATCH 08/10] fs/ntfs3: Add ability to format new mft records with bigger/smaller header

Just define in ntfs.h
    #define MFTRECORD_FIXUP_OFFSET  MFTRECORD_FIXUP_OFFSET_1
or
    #define MFTRECORD_FIXUP_OFFSET  MFTRECORD_FIXUP_OFFSET_3

Signed-off-by: Konstantin Komarov <[email protected]>
---
 fs/ntfs3/ntfs.h   | 9 +++++++++
 fs/ntfs3/record.c | 2 ++
 fs/ntfs3/super.c  | 6 +++---
 3 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/fs/ntfs3/ntfs.h b/fs/ntfs3/ntfs.h
index 3ec2eaf31996..98b76d1b09e7 100644
--- a/fs/ntfs3/ntfs.h
+++ b/fs/ntfs3/ntfs.h
@@ -288,6 +288,15 @@ struct MFT_REC {

 #define MFTRECORD_FIXUP_OFFSET_1 offsetof(struct MFT_REC, res)
 #define MFTRECORD_FIXUP_OFFSET_3 offsetof(struct MFT_REC, fixups)
+/*
+ * define MFTRECORD_FIXUP_OFFSET as MFTRECORD_FIXUP_OFFSET_3 (0x30)
+ * to format new mft records with bigger header (as current ntfs.sys does)
+ *
+ * define MFTRECORD_FIXUP_OFFSET as MFTRECORD_FIXUP_OFFSET_1 (0x2A)
+ * to format new mft records with smaller header (as old ntfs.sys did)
+ * Both variants are valid.
+ */
+#define MFTRECORD_FIXUP_OFFSET  MFTRECORD_FIXUP_OFFSET_1

 static_assert(MFTRECORD_FIXUP_OFFSET_1 == 0x2A);
 static_assert(MFTRECORD_FIXUP_OFFSET_3 == 0x30);
diff --git a/fs/ntfs3/record.c b/fs/ntfs3/record.c
index e73ca2df42eb..c12ebffc94da 100644
--- a/fs/ntfs3/record.c
+++ b/fs/ntfs3/record.c
@@ -388,6 +388,8 @@ int mi_format_new(struct mft_inode *mi, struct
ntfs_sb_info *sbi, CLST rno,

     rec->seq = cpu_to_le16(seq);
     rec->flags = RECORD_FLAG_IN_USE | flags;
+    if (MFTRECORD_FIXUP_OFFSET == MFTRECORD_FIXUP_OFFSET_3)
+        rec->mft_record = cpu_to_le32(rno);

     mi->dirty = true;

diff --git a/fs/ntfs3/super.c b/fs/ntfs3/super.c
index 12019bfe1325..7ab0a79c7d84 100644
--- a/fs/ntfs3/super.c
+++ b/fs/ntfs3/super.c
@@ -867,7 +867,7 @@ static int ntfs_init_from_boot(struct super_block
*sb, u32 sector_size,
     }

     sbi->max_bytes_per_attr =
-        record_size - ALIGN(MFTRECORD_FIXUP_OFFSET_1, 8) -
+        record_size - ALIGN(MFTRECORD_FIXUP_OFFSET, 8) -
         ALIGN(((record_size >> SECTOR_SHIFT) * sizeof(short)), 8) -
         ALIGN(sizeof(enum ATTR_TYPE), 8);

@@ -909,10 +909,10 @@ static int ntfs_init_from_boot(struct super_block
*sb, u32 sector_size,

     sbi->new_rec = rec;
     rec->rhdr.sign = NTFS_FILE_SIGNATURE;
-    rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET_1);
+    rec->rhdr.fix_off = cpu_to_le16(MFTRECORD_FIXUP_OFFSET);
     fn = (sbi->record_size >> SECTOR_SHIFT) + 1;
     rec->rhdr.fix_num = cpu_to_le16(fn);
-    ao = ALIGN(MFTRECORD_FIXUP_OFFSET_1 + sizeof(short) * fn, 8);
+    ao = ALIGN(MFTRECORD_FIXUP_OFFSET + sizeof(short) * fn, 8);
     rec->attr_off = cpu_to_le16(ao);
     rec->used = cpu_to_le32(ao + ALIGN(sizeof(enum ATTR_TYPE), 8));
     rec->total = cpu_to_le32(sbi->record_size);
--
2.34.1

2023-05-08 12:58:05

by Konstantin Komarov

[permalink] [raw]
Subject: [PATCH 09/10] fs/ntfs3: Fix endian problem

Signed-off-by: Konstantin Komarov <[email protected]>
---
 fs/ntfs3/frecord.c | 11 +++++------
 fs/ntfs3/ntfs_fs.h |  2 +-
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/fs/ntfs3/frecord.c b/fs/ntfs3/frecord.c
index be59bd399fd1..16bd9faa2d28 100644
--- a/fs/ntfs3/frecord.c
+++ b/fs/ntfs3/frecord.c
@@ -236,6 +236,7 @@ struct ATTRIB *ni_find_attr(struct ntfs_inode *ni,
struct ATTRIB *attr,
     return attr;

 out:
+    ntfs_inode_err(&ni->vfs_inode, "failed to parse mft record");
     ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_ERROR);
     return NULL;
 }
@@ -1643,14 +1644,13 @@ int ni_delete_all(struct ntfs_inode *ni)
  * Return: File name attribute by its value.
  */
 struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
-                     const struct cpu_str *uni,
+                     const struct le_str *uni,
                      const struct MFT_REF *home_dir,
                      struct mft_inode **mi,
                      struct ATTR_LIST_ENTRY **le)
 {
     struct ATTRIB *attr = NULL;
     struct ATTR_FILE_NAME *fname;
-    struct le_str *fns;

     if (le)
         *le = NULL;
@@ -1674,10 +1674,9 @@ struct ATTR_FILE_NAME *ni_fname_name(struct
ntfs_inode *ni,
     if (uni->len != fname->name_len)
         goto next;

-    fns = (struct le_str *)&fname->name_len;
-    if (ntfs_cmp_names_cpu(uni, fns, NULL, false))
+    if (ntfs_cmp_names(uni->name, uni->len, fname->name, uni->len, NULL,
+               false))
         goto next;
-
     return fname;
 }

@@ -2915,7 +2914,7 @@ int ni_remove_name(struct ntfs_inode *dir_ni,
struct ntfs_inode *ni,
     /* Find name in record. */
     mi_get_ref(&dir_ni->mi, &de_name->home);

-    fname = ni_fname_name(ni, (struct cpu_str *)&de_name->name_len,
+    fname = ni_fname_name(ni, (struct le_str *)&de_name->name_len,
                   &de_name->home, &mi, &le);
     if (!fname)
         return -ENOENT;
diff --git a/fs/ntfs3/ntfs_fs.h b/fs/ntfs3/ntfs_fs.h
index 98b61e4b3215..00fa782fcada 100644
--- a/fs/ntfs3/ntfs_fs.h
+++ b/fs/ntfs3/ntfs_fs.h
@@ -543,7 +543,7 @@ void ni_remove_attr_le(struct ntfs_inode *ni, struct
ATTRIB *attr,
                struct mft_inode *mi, struct ATTR_LIST_ENTRY *le);
 int ni_delete_all(struct ntfs_inode *ni);
 struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
-                     const struct cpu_str *uni,
+                     const struct le_str *uni,
                      const struct MFT_REF *home,
                      struct mft_inode **mi,
                      struct ATTR_LIST_ENTRY **entry);
--
2.34.1

2023-05-08 13:02:51

by Konstantin Komarov

[permalink] [raw]
Subject: [PATCH 10/10] fs/ntfs3: Add support /proc/fs/ntfs3/<dev>/volinfo and /proc/fs/ntfs3/<dev>/label

Metafile /proc/fs/ntfs3/<dev>/label allows to read/write current ntfs label.

Signed-off-by: Konstantin Komarov <[email protected]>
---
 fs/ntfs3/fsntfs.c  |  58 ++++++++++++++++++++
 fs/ntfs3/ntfs_fs.h |   5 +-
 fs/ntfs3/super.c   | 134 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 195 insertions(+), 2 deletions(-)

diff --git a/fs/ntfs3/fsntfs.c b/fs/ntfs3/fsntfs.c
index 1c05c088d1c6..33afee0f5559 100644
--- a/fs/ntfs3/fsntfs.c
+++ b/fs/ntfs3/fsntfs.c
@@ -9,6 +9,7 @@
 #include <linux/buffer_head.h>
 #include <linux/fs.h>
 #include <linux/kernel.h>
+#include <linux/nls.h>

 #include "debug.h"
 #include "ntfs.h"
@@ -2619,3 +2620,60 @@ bool valid_windows_name(struct ntfs_sb_info *sbi,
const struct le_str *fname)
     return !name_has_forbidden_chars(fname) &&
            !is_reserved_name(sbi, fname);
 }
+
+/*
+ * ntfs_set_label - updates current ntfs label.
+ */
+int ntfs_set_label(struct ntfs_sb_info *sbi, u8 *label, int len)
+{
+    int err;
+    struct ATTRIB *attr;
+    struct ntfs_inode *ni = sbi->volume.ni;
+    const u8 max_ulen = 0x80; /* TODO: use attrdef to get maximum length */
+    /* Allocate PATH_MAX bytes. */
+    struct cpu_str *uni = __getname();
+
+    if (!uni)
+        return -ENOMEM;
+
+    err = ntfs_nls_to_utf16(sbi, label, len, uni, (PATH_MAX - 2) / 2,
+                UTF16_LITTLE_ENDIAN);
+    if (err < 0)
+        goto out;
+
+    if (uni->len > max_ulen) {
+        ntfs_warn(sbi->sb, "new label is too long");
+        err = -EFBIG;
+        goto out;
+    }
+
+    ni_lock(ni);
+
+    /* Ignore any errors. */
+    ni_remove_attr(ni, ATTR_LABEL, NULL, 0, false, NULL);
+
+    err = ni_insert_resident(ni, uni->len * sizeof(u16), ATTR_LABEL, NULL,
+                 0, &attr, NULL, NULL);
+    if (err < 0)
+        goto unlock_out;
+
+    /* write new label in on-disk struct. */
+    memcpy(resident_data(attr), uni->name, uni->len * sizeof(u16));
+
+    /* update cached value of current label. */
+    if (len >= ARRAY_SIZE(sbi->volume.label))
+        len = ARRAY_SIZE(sbi->volume.label) - 1;
+    memcpy(sbi->volume.label, label, len);
+    sbi->volume.label[len] = 0;
+    mark_inode_dirty_sync(&ni->vfs_inode);
+
+unlock_out:
+    ni_unlock(ni);
+
+    if (!err)
+        err = _ni_write_inode(&ni->vfs_inode, 0);
+
+out:
+    __putname(uni);
+    return err;
+}
\ No newline at end of file
diff --git a/fs/ntfs3/ntfs_fs.h b/fs/ntfs3/ntfs_fs.h
index 00fa782fcada..629403ede6e5 100644
--- a/fs/ntfs3/ntfs_fs.h
+++ b/fs/ntfs3/ntfs_fs.h
@@ -276,7 +276,7 @@ struct ntfs_sb_info {
         __le16 flags; // Cached current VOLUME_INFO::flags,
VOLUME_FLAG_DIRTY.
         u8 major_ver;
         u8 minor_ver;
-        char label[65];
+        char label[256];
         bool real_dirty; // Real fs state.
     } volume;

@@ -286,7 +286,6 @@ struct ntfs_sb_info {
         struct ntfs_inode *ni;
         u32 next_id;
         u64 next_off;
-
         __le32 def_security_id;
     } security;

@@ -314,6 +313,7 @@ struct ntfs_sb_info {

     struct ntfs_mount_options *options;
     struct ratelimit_state msg_ratelimit;
+    struct proc_dir_entry *procdir;
 };

 /* One MFT record(usually 1024 bytes), consists of attributes. */
@@ -651,6 +651,7 @@ void mark_as_free_ex(struct ntfs_sb_info *sbi, CLST
lcn, CLST len, bool trim);
 int run_deallocate(struct ntfs_sb_info *sbi, const struct runs_tree *run,
            bool trim);
 bool valid_windows_name(struct ntfs_sb_info *sbi, const struct le_str
*name);
+int ntfs_set_label(struct ntfs_sb_info *sbi, u8 *label, int len);

 /* Globals from index.c */
 int indx_used_bit(struct ntfs_index *indx, struct ntfs_inode *ni,
size_t *bit);
diff --git a/fs/ntfs3/super.c b/fs/ntfs3/super.c
index 7ab0a79c7d84..e36769eac7de 100644
--- a/fs/ntfs3/super.c
+++ b/fs/ntfs3/super.c
@@ -57,6 +57,7 @@
 #include <linux/minmax.h>
 #include <linux/module.h>
 #include <linux/nls.h>
+#include <linux/proc_fs.h>
 #include <linux/seq_file.h>
 #include <linux/statfs.h>

@@ -441,6 +442,103 @@ static int ntfs_fs_reconfigure(struct fs_context *fc)
     return 0;
 }

+#ifdef CONFIG_PROC_FS
+static struct proc_dir_entry *proc_info_root;
+
+/*
+ * ntfs3_volinfo:
+ *
+ * The content of /proc/fs/ntfs3/<dev>/volinfo
+ *
+ * ntfs3.1
+ * cluster size
+ * number of clusters
+*/
+static int ntfs3_volinfo(struct seq_file *m, void *o)
+{
+    struct super_block *sb = m->private;
+    struct ntfs_sb_info *sbi = sb->s_fs_info;
+
+    seq_printf(m, "ntfs%d.%d\n%u\n%zu\n", sbi->volume.major_ver,
+           sbi->volume.minor_ver, sbi->cluster_size,
+           sbi->used.bitmap.nbits);
+
+    return 0;
+}
+
+static int ntfs3_volinfo_open(struct inode *inode, struct file *file)
+{
+    return single_open(file, ntfs3_volinfo, pde_data(inode));
+}
+
+/* read /proc/fs/ntfs3/<dev>/label */
+static int ntfs3_label_show(struct seq_file *m, void *o)
+{
+    struct super_block *sb = m->private;
+    struct ntfs_sb_info *sbi = sb->s_fs_info;
+
+    seq_printf(m, "%s\n", sbi->volume.label);
+
+    return 0;
+}
+
+/* write /proc/fs/ntfs3/<dev>/label */
+static ssize_t ntfs3_label_write(struct file *file, const char __user
*buffer,
+                 size_t count, loff_t *ppos)
+{
+    int err;
+    struct super_block *sb = pde_data(file_inode(file));
+    struct ntfs_sb_info *sbi = sb->s_fs_info;
+    ssize_t ret = count;
+    u8 *label = kmalloc(count, GFP_NOFS);
+
+    if (!label)
+        return -ENOMEM;
+
+    if (copy_from_user(label, buffer, ret)) {
+        ret = -EFAULT;
+        goto out;
+    }
+    while (ret > 0 && label[ret - 1] == '\n')
+        ret -= 1;
+
+    err = ntfs_set_label(sbi, label, ret);
+
+    if (err < 0) {
+        ntfs_err(sb, "failed (%d) to write label", err);
+        ret = err;
+        goto out;
+    }
+
+    *ppos += count;
+    ret = count;
+out:
+    kfree(label);
+    return ret;
+}
+
+static int ntfs3_label_open(struct inode *inode, struct file *file)
+{
+    return single_open(file, ntfs3_label_show, pde_data(inode));
+}
+
+static const struct proc_ops ntfs3_volinfo_fops = {
+    .proc_read = seq_read,
+    .proc_lseek = seq_lseek,
+    .proc_release = single_release,
+    .proc_open = ntfs3_volinfo_open,
+};
+
+static const struct proc_ops ntfs3_label_fops = {
+    .proc_read = seq_read,
+    .proc_lseek = seq_lseek,
+    .proc_release = single_release,
+    .proc_open = ntfs3_label_open,
+    .proc_write = ntfs3_label_write,
+};
+
+#endif
+
 static struct kmem_cache *ntfs_inode_cachep;

 static struct inode *ntfs_alloc_inode(struct super_block *sb)
@@ -515,6 +613,16 @@ static void ntfs_put_super(struct super_block *sb)
 {
     struct ntfs_sb_info *sbi = sb->s_fs_info;

+#ifdef CONFIG_PROC_FS
+    // Remove /proc/fs/ntfs3/..
+    if (sbi->procdir) {
+        remove_proc_entry("label", sbi->procdir);
+        remove_proc_entry("volinfo", sbi->procdir);
+        remove_proc_entry(sb->s_id, proc_info_root);
+        sbi->procdir = NULL;
+    }
+#endif
+
     /* Mark rw ntfs as clear, if possible. */
     ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);

@@ -1436,6 +1544,20 @@ static int ntfs_fill_super(struct super_block
*sb, struct fs_context *fc)
         kfree(boot2);
     }

+#ifdef CONFIG_PROC_FS
+    /* Create /proc/fs/ntfs3/.. */
+    if (proc_info_root) {
+        struct proc_dir_entry *e = proc_mkdir(sb->s_id, proc_info_root);
+        if (e) {
+            proc_create_data("volinfo", S_IFREG | S_IRUGO, e,
+                     &ntfs3_volinfo_fops, sb);
+            proc_create_data("label", S_IFREG | S_IRUGO | S_IWUGO,
+                     e, &ntfs3_label_fops, sb);
+            sbi->procdir = e;
+        }
+    }
+#endif
+
     return 0;

 put_inode_out:
@@ -1630,6 +1752,12 @@ static int __init init_ntfs_fs(void)
     if (IS_ENABLED(CONFIG_NTFS3_LZX_XPRESS))
         pr_info("ntfs3: Read-only LZX/Xpress compression included\n");

+
+#ifdef CONFIG_PROC_FS
+    /* Create "/proc/fs/ntfs3" */
+    proc_info_root = proc_mkdir("fs/ntfs3", NULL);
+#endif
+
     err = ntfs3_init_bitmap();
     if (err)
         return err;
@@ -1661,6 +1789,12 @@ static void __exit exit_ntfs_fs(void)
     kmem_cache_destroy(ntfs_inode_cachep);
     unregister_filesystem(&ntfs_fs_type);
     ntfs3_exit_bitmap();
+
+#ifdef CONFIG_PROC_FS
+    if (proc_info_root)
+        remove_proc_entry("fs/ntfs3", NULL);
+#endif
+
 }

 MODULE_LICENSE("GPL");
--
2.34.1

2023-05-08 13:11:52

by Konstantin Komarov

[permalink] [raw]
Subject: [PATCH 07/10] 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]>
---
 fs/ntfs3/attrlist.c |   3 +-
 fs/ntfs3/frecord.c  |   2 +-
 fs/ntfs3/fsntfs.c   |  37 +++++++--------
 fs/ntfs3/inode.c    |   5 +-
 fs/ntfs3/ntfs.h     | 108 ++++++++++++++++++++++++--------------------
 fs/ntfs3/ntfs_fs.h  |  12 ++---
 fs/ntfs3/record.c   |   2 +-
 7 files changed, 88 insertions(+), 81 deletions(-)

diff --git a/fs/ntfs3/attrlist.c b/fs/ntfs3/attrlist.c
index 81c22df27c72..42631b31adf1 100644
--- a/fs/ntfs3/attrlist.c
+++ b/fs/ntfs3/attrlist.c
@@ -375,8 +375,7 @@ bool al_remove_le(struct ntfs_inode *ni, struct
ATTR_LIST_ENTRY *le)
  * al_delete_le - Delete first le from the list which matches its
parameters.
  */
 bool al_delete_le(struct ntfs_inode *ni, enum ATTR_TYPE type, CLST vcn,
-          const __le16 *name, size_t name_len,
-          const struct MFT_REF *ref)
+          const __le16 *name, u8 name_len, const struct MFT_REF *ref)
 {
     u16 size;
     struct ATTR_LIST_ENTRY *le;
diff --git a/fs/ntfs3/frecord.c b/fs/ntfs3/frecord.c
index 4227e3f590a5..be59bd399fd1 100644
--- a/fs/ntfs3/frecord.c
+++ b/fs/ntfs3/frecord.c
@@ -384,7 +384,7 @@ bool ni_add_subrecord(struct ntfs_inode *ni, CLST
rno, struct mft_inode **mi)
  * ni_remove_attr - Remove all attributes for the given type/name/id.
  */
 int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
-           const __le16 *name, size_t name_len, bool base_only,
+           const __le16 *name, u8 name_len, bool base_only,
            const __le16 *id)
 {
     int err;
diff --git a/fs/ntfs3/fsntfs.c b/fs/ntfs3/fsntfs.c
index 1a0527e81ebb..1c05c088d1c6 100644
--- a/fs/ntfs3/fsntfs.c
+++ b/fs/ntfs3/fsntfs.c
@@ -1661,7 +1661,8 @@ int ntfs_vbo_to_lbo(struct ntfs_sb_info *sbi,
const struct runs_tree *run,
     return 0;
 }

-struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST rno,
bool dir)
+struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST rno,
+                  enum RECORD_FLAG flag)
 {
     int err = 0;
     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)

     ni = ntfs_i(inode);

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

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

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

         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,
         goto out;

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

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

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

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

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

     err = ntfs_read_run_nb(sbi, &ni->file.run,
                    le64_to_cpu(sii_e->sec_hdr.off) +
-                       SIZEOF_SECURITY_HDR,
+                       sizeof(struct SECURITY_HDR),
                    p, *size, NULL);
     if (err)
         goto out;
@@ -2069,7 +2069,7 @@ int ntfs_insert_security(struct ntfs_sb_info *sbi,
     struct NTFS_DE_SDH sdh_e;
     struct NTFS_DE_SII sii_e;
     struct SECURITY_HDR *d_security;
-    u32 new_sec_size = size_sd + SIZEOF_SECURITY_HDR;
+    u32 new_sec_size = size_sd + sizeof(struct SECURITY_HDR);
     u32 aligned_sec_size = ALIGN(new_sec_size, 16);
     struct SECURITY_KEY hash_key;
     struct ntfs_fnd *fnd_sdh = NULL;
@@ -2207,14 +2207,14 @@ int ntfs_insert_security(struct ntfs_sb_info *sbi,
     /* Fill SII entry. */
     sii_e.de.view.data_off =
         cpu_to_le16(offsetof(struct NTFS_DE_SII, sec_hdr));
-    sii_e.de.view.data_size = cpu_to_le16(SIZEOF_SECURITY_HDR);
+    sii_e.de.view.data_size = cpu_to_le16(sizeof(struct SECURITY_HDR));
     sii_e.de.view.res = 0;
-    sii_e.de.size = cpu_to_le16(SIZEOF_SII_DIRENTRY);
+    sii_e.de.size = cpu_to_le16(sizeof(struct NTFS_DE_SII));
     sii_e.de.key_size = cpu_to_le16(sizeof(d_security->key.sec_id));
     sii_e.de.flags = 0;
     sii_e.de.res = 0;
     sii_e.sec_id = d_security->key.sec_id;
-    memcpy(&sii_e.sec_hdr, d_security, SIZEOF_SECURITY_HDR);
+    memcpy(&sii_e.sec_hdr, d_security, sizeof(struct SECURITY_HDR));

     err = indx_insert_entry(indx_sii, ni, &sii_e.de, NULL, NULL, 0);
     if (err)
@@ -2223,7 +2223,7 @@ int ntfs_insert_security(struct ntfs_sb_info *sbi,
     /* Fill SDH entry. */
     sdh_e.de.view.data_off =
         cpu_to_le16(offsetof(struct NTFS_DE_SDH, sec_hdr));
-    sdh_e.de.view.data_size = cpu_to_le16(SIZEOF_SECURITY_HDR);
+    sdh_e.de.view.data_size = cpu_to_le16(sizeof(struct SECURITY_HDR));
     sdh_e.de.view.res = 0;
     sdh_e.de.size = cpu_to_le16(SIZEOF_SDH_DIRENTRY);
     sdh_e.de.key_size = cpu_to_le16(sizeof(sdh_e.key));
@@ -2231,7 +2231,7 @@ int ntfs_insert_security(struct ntfs_sb_info *sbi,
     sdh_e.de.res = 0;
     sdh_e.key.hash = d_security->key.hash;
     sdh_e.key.sec_id = d_security->key.sec_id;
-    memcpy(&sdh_e.sec_hdr, d_security, SIZEOF_SECURITY_HDR);
+    memcpy(&sdh_e.sec_hdr, d_security, sizeof(struct SECURITY_HDR));
     sdh_e.magic[0] = cpu_to_le16('I');
     sdh_e.magic[1] = cpu_to_le16('I');

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

-static inline bool is_reserved_name(struct ntfs_sb_info *sbi,
+static inline bool is_reserved_name(const struct ntfs_sb_info *sbi,
                     const struct le_str *fname)
 {
     int port_digit;
     const __le16 *name = fname->name;
     int len = fname->len;
-    u16 *upcase = sbi->upcase;
+    const u16 *upcase = sbi->upcase;

     /* check for 3 chars reserved names (device names) */
     /* name by itself or with any extension is forbidden */
diff --git a/fs/ntfs3/inode.c b/fs/ntfs3/inode.c
index f699cc053655..dc7e7ab701c6 100644
--- a/fs/ntfs3/inode.c
+++ b/fs/ntfs3/inode.c
@@ -1309,7 +1309,7 @@ struct inode *ntfs_create_inode(struct mnt_idmap
*idmap, struct inode *dir,
     if (err)
         goto out2;

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

         root = Add2Ptr(attr, sizeof(I30_NAME) + SIZEOF_RESIDENT);
         memcpy(root, dir_root, offsetof(struct INDEX_ROOT, ihdr));
-        root->ihdr.de_off =
-            cpu_to_le32(sizeof(struct INDEX_HDR)); // 0x10
+        root->ihdr.de_off = cpu_to_le32(sizeof(struct INDEX_HDR));
         root->ihdr.used = cpu_to_le32(sizeof(struct INDEX_HDR) +
                           sizeof(struct NTFS_DE));
         root->ihdr.total = root->ihdr.used;
diff --git a/fs/ntfs3/ntfs.h b/fs/ntfs3/ntfs.h
index 90151e56c122..3ec2eaf31996 100644
--- a/fs/ntfs3/ntfs.h
+++ b/fs/ntfs3/ntfs.h
@@ -95,11 +95,10 @@ enum RECORD_NUM {
     MFT_REC_BITMAP        = 6,
     MFT_REC_BOOT        = 7,
     MFT_REC_BADCLUST    = 8,
-    //MFT_REC_QUOTA        = 9,
-    MFT_REC_SECURE        = 9, // NTFS 3.0
+    MFT_REC_SECURE        = 9,
     MFT_REC_UPCASE        = 10,
-    MFT_REC_EXTEND        = 11, // NTFS 3.0
-    MFT_REC_RESERVED    = 11,
+    MFT_REC_EXTEND        = 11,
+    MFT_REC_RESERVED    = 12,
     MFT_REC_FREE        = 16,
     MFT_REC_USER        = 24,
 };
@@ -109,7 +108,6 @@ enum ATTR_TYPE {
     ATTR_STD        = cpu_to_le32(0x10),
     ATTR_LIST        = cpu_to_le32(0x20),
     ATTR_NAME        = cpu_to_le32(0x30),
-    // ATTR_VOLUME_VERSION on Nt4
     ATTR_ID            = cpu_to_le32(0x40),
     ATTR_SECURE        = cpu_to_le32(0x50),
     ATTR_LABEL        = cpu_to_le32(0x60),
@@ -118,7 +116,6 @@ enum ATTR_TYPE {
     ATTR_ROOT        = cpu_to_le32(0x90),
     ATTR_ALLOC        = cpu_to_le32(0xA0),
     ATTR_BITMAP        = cpu_to_le32(0xB0),
-    // ATTR_SYMLINK on Nt4
     ATTR_REPARSE        = cpu_to_le32(0xC0),
     ATTR_EA_INFO        = cpu_to_le32(0xD0),
     ATTR_EA            = cpu_to_le32(0xE0),
@@ -144,6 +141,7 @@ enum FILE_ATTRIBUTE {
     FILE_ATTRIBUTE_ENCRYPTED    = cpu_to_le32(0x00004000),
     FILE_ATTRIBUTE_VALID_FLAGS    = cpu_to_le32(0x00007fb7),
     FILE_ATTRIBUTE_DIRECTORY    = cpu_to_le32(0x10000000),
+    FILE_ATTRIBUTE_INDEX        = cpu_to_le32(0x20000000)
 };

 static_assert(sizeof(enum FILE_ATTRIBUTE) == 4);
@@ -266,7 +264,7 @@ enum RECORD_FLAG {
     RECORD_FLAG_IN_USE    = cpu_to_le16(0x0001),
     RECORD_FLAG_DIR        = cpu_to_le16(0x0002),
     RECORD_FLAG_SYSTEM    = cpu_to_le16(0x0004),
-    RECORD_FLAG_UNKNOWN    = cpu_to_le16(0x0008),
+    RECORD_FLAG_INDEX    = cpu_to_le16(0x0008),
 };

 /* MFT Record structure. */
@@ -331,18 +329,18 @@ struct ATTR_NONRESIDENT {
     __le64 svcn;        // 0x10: Starting VCN of this segment.
     __le64 evcn;        // 0x18: End VCN of this segment.
     __le16 run_off;        // 0x20: Offset to packed runs.
-    //  Unit of Compression size for this stream, expressed
-    //  as a log of the cluster size.
+    // Unit of Compression size for this stream, expressed
+    // as a log of the cluster size.
     //
-    //    0 means file is not compressed
-    //    1, 2, 3, and 4 are potentially legal values if the
-    //        stream is compressed, however the implementation
-    //        may only choose to use 4, or possibly 3.  Note
-    //        that 4 means cluster size time 16.    If convenient
-    //        the implementation may wish to accept a
-    //        reasonable range of legal values here (1-5?),
-    //        even if the implementation only generates
-    //        a smaller set of values itself.
+    // 0 means file is not compressed
+    // 1, 2, 3, and 4 are potentially legal values if the
+    // stream is compressed, however the implementation
+    // may only choose to use 4, or possibly 3.
+        // Note that 4 means cluster size time 16.
+        // If convenient the implementation may wish to accept a
+    // reasonable range of legal values here (1-5?),
+    // even if the implementation only generates
+    // a smaller set of values itself.
     u8 c_unit;        // 0x22:
     u8 res1[5];        // 0x23:
     __le64 alloc_size;    // 0x28: The allocated size of attribute in
bytes.
@@ -836,16 +834,22 @@ static_assert(sizeof(struct ATTR_DEF_ENTRY) == 0xa0);
 /* Object ID (0x40) */
 struct OBJECT_ID {
     struct GUID ObjId;    // 0x00: Unique Id assigned to file.
-    struct GUID BirthVolumeId; // 0x10: Birth Volume Id is the Object
Id of the Volume on.
-                // which the Object Id was allocated. It never changes.
-    struct GUID BirthObjectId; // 0x20: Birth Object Id is the first
Object Id that was
-                // ever assigned to this MFT Record. I.e. If the Object Id
-                // is changed for some reason, this field will reflect the
-                // original value of the Object Id.
-    struct GUID DomainId;    // 0x30: Domain Id is currently unused but
it is intended to be
-                // used in a network environment where the local machine is
-                // part of a Windows 2000 Domain. This may be used in a
Windows
-                // 2000 Advanced Server managed domain.
+
+    // Birth Volume Id is the Object Id of the Volume on.
+    // which the Object Id was allocated. It never changes.
+    struct GUID BirthVolumeId; //0x10:
+
+    // Birth Object Id is the first Object Id that was
+    // ever assigned to this MFT Record. I.e. If the Object Id
+    // is changed for some reason, this field will reflect the
+    // original value of the Object Id.
+    struct GUID BirthObjectId; // 0x20:
+
+    // Domain Id is currently unused but it is intended to be
+    // used in a network environment where the local machine is
+    // part of a Windows 2000 Domain. This may be used in a Windows
+    // 2000 Advanced Server managed domain.
+    struct GUID DomainId;    // 0x30:
 };

 static_assert(sizeof(struct OBJECT_ID) == 0x40);
@@ -855,32 +859,35 @@ struct NTFS_DE_O {
     struct NTFS_DE de;
     struct GUID ObjId;    // 0x10: Unique Id assigned to file.
     struct MFT_REF ref;    // 0x20: MFT record number with this file.
-    struct GUID BirthVolumeId; // 0x28: Birth Volume Id is the Object
Id of the Volume on
-                // which the Object Id was allocated. It never changes.
-    struct GUID BirthObjectId; // 0x38: Birth Object Id is the first
Object Id that was
-                // ever assigned to this MFT Record. I.e. If the Object Id
-                // is changed for some reason, this field will reflect the
-                // original value of the Object Id.
-                // This field is valid if data_size == 0x48.
-    struct GUID BirthDomainId; // 0x48: Domain Id is currently unused
but it is intended
-                // to be used in a network environment where the local
-                // machine is part of a Windows 2000 Domain. This may be
-                // used in a Windows 2000 Advanced Server managed domain.
+
+    // Birth Volume Id is the Object Id of the Volume on
+    // which the Object Id was allocated. It never changes.
+    struct GUID BirthVolumeId; // 0x28:
+
+    // Birth Object Id is the first Object Id that was
+    // ever assigned to this MFT Record. I.e. If the Object Id
+    // is changed for some reason, this field will reflect the
+    // original value of the Object Id.
+    // This field is valid if data_size == 0x48.
+    struct GUID BirthObjectId; // 0x38:
+
+    // Domain Id is currently unused but it is intended
+    // to be used in a network environment where the local
+    // machine is part of a Windows 2000 Domain. This may be
+    // used in a Windows 2000 Advanced Server managed domain.
+    struct GUID BirthDomainId; // 0x48:
 };

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

-#define NTFS_OBJECT_ENTRY_DATA_SIZE1                           \
-    0x38 // struct NTFS_DE_O.BirthDomainId is not used
-#define NTFS_OBJECT_ENTRY_DATA_SIZE2                           \
-    0x48 // struct NTFS_DE_O.BirthDomainId is used
-
 /* Q Directory entry structure ( rule = 0x11 ) */
 struct NTFS_DE_Q {
     struct NTFS_DE de;
     __le32 owner_id;    // 0x10: Unique Id assigned to file
+
+    /* here is 0x30 bytes of user quota. NOTE: 4 byte aligned! */
     __le32 Version;        // 0x14: 0x02
-    __le32 flags2;        // 0x18: Quota flags, see above
+    __le32 Flags;        // 0x18: Quota flags, see above
     __le64 BytesUsed;    // 0x1C:
     __le64 ChangeTime;    // 0x24:
     __le64 WarningLimit;    // 0x28:
@@ -888,9 +895,9 @@ struct NTFS_DE_Q {
     __le64 ExceededTime;    // 0x3C:

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

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

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

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

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

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

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

 #define FILE_NEED_EA 0x80 // See ntifs.h
 /*
- *FILE_NEED_EA, indicates that the file to which the EA belongs cannot be
+ * FILE_NEED_EA, indicates that the file to which the EA belongs cannot be
  * interpreted without understanding the associated extended attributes.
  */
 struct EA_INFO {
diff --git a/fs/ntfs3/ntfs_fs.h b/fs/ntfs3/ntfs_fs.h
index 6667a75411fc..98b61e4b3215 100644
--- a/fs/ntfs3/ntfs_fs.h
+++ b/fs/ntfs3/ntfs_fs.h
@@ -467,8 +467,7 @@ int al_add_le(struct ntfs_inode *ni, enum ATTR_TYPE
type, const __le16 *name,
           struct ATTR_LIST_ENTRY **new_le);
 bool al_remove_le(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le);
 bool al_delete_le(struct ntfs_inode *ni, enum ATTR_TYPE type, CLST vcn,
-          const __le16 *name, size_t name_len,
-          const struct MFT_REF *ref);
+          const __le16 *name, u8 name_len, const struct MFT_REF *ref);
 int al_update(struct ntfs_inode *ni, int sync);
 static inline size_t al_aligned(size_t size)
 {
@@ -527,7 +526,7 @@ struct ATTRIB *ni_load_attr(struct ntfs_inode *ni,
enum ATTR_TYPE type,
 int ni_load_all_mi(struct ntfs_inode *ni);
 bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct
mft_inode **mi);
 int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
-           const __le16 *name, size_t name_len, bool base_only,
+           const __le16 *name, u8 name_len, bool base_only,
            const __le16 *id);
 int ni_create_attr_list(struct ntfs_inode *ni);
 int ni_expand_list(struct ntfs_inode *ni);
@@ -631,7 +630,7 @@ int ntfs_bio_fill_1(struct ntfs_sb_info *sbi, const
struct runs_tree *run);
 int ntfs_vbo_to_lbo(struct ntfs_sb_info *sbi, const struct runs_tree *run,
             u64 vbo, u64 *lbo, u64 *bytes);
 struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST nRec,
-                  bool dir);
+                  enum RECORD_FLAG flag);
 extern const u8 s_default_security[0x50];
 bool is_sd_valid(const struct SECURITY_DESCRIPTOR_RELATIVE *sd, u32 len);
 int ntfs_security_init(struct ntfs_sb_info *sbi);
@@ -649,7 +648,8 @@ int ntfs_insert_reparse(struct ntfs_sb_info *sbi,
__le32 rtag,
 int ntfs_remove_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
             const struct MFT_REF *ref);
 void mark_as_free_ex(struct ntfs_sb_info *sbi, CLST lcn, CLST len,
bool trim);
-int run_deallocate(struct ntfs_sb_info *sbi, struct runs_tree *run,
bool trim);
+int run_deallocate(struct ntfs_sb_info *sbi, const struct runs_tree *run,
+           bool trim);
 bool valid_windows_name(struct ntfs_sb_info *sbi, const struct le_str
*name);

 /* Globals from index.c */
@@ -738,7 +738,7 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi,
struct ATTRIB *attr);
 // TODO: id?
 struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
                 enum ATTR_TYPE type, const __le16 *name,
-                size_t name_len, const __le16 *id);
+                u8 name_len, const __le16 *id);
 static inline struct ATTRIB *rec_find_attr_le(struct mft_inode *rec,
                           struct ATTR_LIST_ENTRY *le)
 {
diff --git a/fs/ntfs3/record.c b/fs/ntfs3/record.c
index 7974ca35a15c..e73ca2df42eb 100644
--- a/fs/ntfs3/record.c
+++ b/fs/ntfs3/record.c
@@ -302,7 +302,7 @@ struct ATTRIB *mi_enum_attr(struct mft_inode *mi,
struct ATTRIB *attr)
  */
 struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
                 enum ATTR_TYPE type, const __le16 *name,
-                size_t name_len, const __le16 *id)
+                u8 name_len, const __le16 *id)
 {
     u32 type_in = le32_to_cpu(type);
     u32 atype;
--
2.34.1