Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ntfs: update in-memory, on-disk structures and headers

Update the NTFS filesystem driver's in-memory and on-disk structures:

- Introduce the infrastructure and initial support for reparse
points and EA attribute.
- Refactor the core ntfs_inode and ntfs_volume structures to support
new features such as iomap.
- Remove the unnecessary types.h and endian.h headers.
- Reorganize the comments in headers for better readability, including
fixing warnings from checkpatch.pl.

Reviewed-by: Christoph Hellwig <hch@lst.de>
Acked-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>

+2769 -2658
-88
fs/ntfs/aops.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 - /* 3 - * aops.h - Defines for NTFS kernel address space operations and page cache 4 - * handling. Part of the Linux-NTFS project. 5 - * 6 - * Copyright (c) 2001-2004 Anton Altaparmakov 7 - * Copyright (c) 2002 Richard Russon 8 - */ 9 - 10 - #ifndef _LINUX_NTFS_AOPS_H 11 - #define _LINUX_NTFS_AOPS_H 12 - 13 - #include <linux/mm.h> 14 - #include <linux/highmem.h> 15 - #include <linux/pagemap.h> 16 - #include <linux/fs.h> 17 - 18 - #include "inode.h" 19 - 20 - /** 21 - * ntfs_unmap_page - release a page that was mapped using ntfs_map_page() 22 - * @page: the page to release 23 - * 24 - * Unpin, unmap and release a page that was obtained from ntfs_map_page(). 25 - */ 26 - static inline void ntfs_unmap_page(struct page *page) 27 - { 28 - kunmap(page); 29 - put_page(page); 30 - } 31 - 32 - /** 33 - * ntfs_map_page - map a page into accessible memory, reading it if necessary 34 - * @mapping: address space for which to obtain the page 35 - * @index: index into the page cache for @mapping of the page to map 36 - * 37 - * Read a page from the page cache of the address space @mapping at position 38 - * @index, where @index is in units of PAGE_SIZE, and not in bytes. 39 - * 40 - * If the page is not in memory it is loaded from disk first using the 41 - * read_folio method defined in the address space operations of @mapping 42 - * and the page is added to the page cache of @mapping in the process. 43 - * 44 - * If the page belongs to an mst protected attribute and it is marked as such 45 - * in its ntfs inode (NInoMstProtected()) the mst fixups are applied but no 46 - * error checking is performed. This means the caller has to verify whether 47 - * the ntfs record(s) contained in the page are valid or not using one of the 48 - * ntfs_is_XXXX_record{,p}() macros, where XXXX is the record type you are 49 - * expecting to see. (For details of the macros, see fs/ntfs/layout.h.) 50 - * 51 - * If the page is in high memory it is mapped into memory directly addressible 52 - * by the kernel. 53 - * 54 - * Finally the page count is incremented, thus pinning the page into place. 55 - * 56 - * The above means that page_address(page) can be used on all pages obtained 57 - * with ntfs_map_page() to get the kernel virtual address of the page. 58 - * 59 - * When finished with the page, the caller has to call ntfs_unmap_page() to 60 - * unpin, unmap and release the page. 61 - * 62 - * Note this does not grant exclusive access. If such is desired, the caller 63 - * must provide it independently of the ntfs_{un}map_page() calls by using 64 - * a {rw_}semaphore or other means of serialization. A spin lock cannot be 65 - * used as ntfs_map_page() can block. 66 - * 67 - * The unlocked and uptodate page is returned on success or an encoded error 68 - * on failure. Caller has to test for error using the IS_ERR() macro on the 69 - * return value. If that evaluates to 'true', the negative error code can be 70 - * obtained using PTR_ERR() on the return value of ntfs_map_page(). 71 - */ 72 - static inline struct page *ntfs_map_page(struct address_space *mapping, 73 - unsigned long index) 74 - { 75 - struct page *page = read_mapping_page(mapping, index, NULL); 76 - 77 - if (!IS_ERR(page)) 78 - kmap(page); 79 - return page; 80 - } 81 - 82 - #ifdef NTFS_RW 83 - 84 - extern void mark_ntfs_record_dirty(struct page *page, const unsigned int ofs); 85 - 86 - #endif /* NTFS_RW */ 87 - 88 - #endif /* _LINUX_NTFS_AOPS_H */
+125 -63
fs/ntfs/attrib.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * attrib.h - Defines for attribute handling in NTFS Linux kernel driver. 4 - * Part of the Linux-NTFS project. 3 + * Defines for attribute handling in NTFS Linux kernel driver. 5 4 * 6 5 * Copyright (c) 2001-2005 Anton Altaparmakov 7 6 * Copyright (c) 2002 Richard Russon 7 + * Copyright (c) 2025 LG Electronics Co., Ltd. 8 8 */ 9 9 10 10 #ifndef _LINUX_NTFS_ATTRIB_H 11 11 #define _LINUX_NTFS_ATTRIB_H 12 12 13 - #include "endian.h" 14 - #include "types.h" 15 - #include "layout.h" 16 - #include "inode.h" 17 - #include "runlist.h" 18 - #include "volume.h" 13 + #include "ntfs.h" 14 + #include "dir.h" 19 15 20 - /** 16 + extern __le16 AT_UNNAMED[]; 17 + 18 + /* 21 19 * ntfs_attr_search_ctx - used in attribute search functions 22 - * @mrec: buffer containing mft record to search 23 - * @attr: attribute record in @mrec where to begin/continue search 24 - * @is_first: if true ntfs_attr_lookup() begins search with @attr, else after 20 + * @mrec: buffer containing mft record to search 21 + * @mapped_mrec: true if @mrec was mapped by the search functions 22 + * @attr: attribute record in @mrec where to begin/continue search 23 + * @is_first: if true ntfs_attr_lookup() begins search with @attr, else after 24 + * @ntfs_ino: Inode owning this attribute search 25 + * @al_entry: Current attribute list entry 26 + * @base_ntfs_ino: Base inode 27 + * @mapped_base_mrec: true if @base_mrec was mapped by the search 28 + * @base_attr: Base attribute record pointer 25 29 * 26 30 * Structure must be initialized to zero before the first call to one of the 27 31 * attribute search functions. Initialize @mrec to point to the mft record to ··· 39 35 * any modification of the search context, to automagically get the next 40 36 * matching attribute. 41 37 */ 42 - typedef struct { 43 - MFT_RECORD *mrec; 44 - ATTR_RECORD *attr; 38 + struct ntfs_attr_search_ctx { 39 + struct mft_record *mrec; 40 + bool mapped_mrec; 41 + struct attr_record *attr; 45 42 bool is_first; 46 - ntfs_inode *ntfs_ino; 47 - ATTR_LIST_ENTRY *al_entry; 48 - ntfs_inode *base_ntfs_ino; 49 - MFT_RECORD *base_mrec; 50 - ATTR_RECORD *base_attr; 51 - } ntfs_attr_search_ctx; 43 + struct ntfs_inode *ntfs_ino; 44 + struct attr_list_entry *al_entry; 45 + struct ntfs_inode *base_ntfs_ino; 46 + struct mft_record *base_mrec; 47 + bool mapped_base_mrec; 48 + struct attr_record *base_attr; 49 + }; 52 50 53 - extern int ntfs_map_runlist_nolock(ntfs_inode *ni, VCN vcn, 54 - ntfs_attr_search_ctx *ctx); 55 - extern int ntfs_map_runlist(ntfs_inode *ni, VCN vcn); 51 + enum { /* ways of processing holes when expanding */ 52 + HOLES_NO, 53 + HOLES_OK, 54 + }; 56 55 57 - extern LCN ntfs_attr_vcn_to_lcn_nolock(ntfs_inode *ni, const VCN vcn, 56 + int ntfs_map_runlist_nolock(struct ntfs_inode *ni, s64 vcn, 57 + struct ntfs_attr_search_ctx *ctx); 58 + int ntfs_map_runlist(struct ntfs_inode *ni, s64 vcn); 59 + s64 ntfs_attr_vcn_to_lcn_nolock(struct ntfs_inode *ni, const s64 vcn, 58 60 const bool write_locked); 61 + struct runlist_element *ntfs_attr_find_vcn_nolock(struct ntfs_inode *ni, 62 + const s64 vcn, struct ntfs_attr_search_ctx *ctx); 63 + struct runlist_element *__ntfs_attr_find_vcn_nolock(struct runlist *runlist, 64 + const s64 vcn); 65 + int ntfs_attr_map_whole_runlist(struct ntfs_inode *ni); 66 + int ntfs_attr_lookup(const __le32 type, const __le16 *name, 67 + const u32 name_len, const u32 ic, 68 + const s64 lowest_vcn, const u8 *val, const u32 val_len, 69 + struct ntfs_attr_search_ctx *ctx); 70 + int load_attribute_list(struct ntfs_inode *base_ni, 71 + u8 *al_start, const s64 size); 59 72 60 - extern runlist_element *ntfs_attr_find_vcn_nolock(ntfs_inode *ni, 61 - const VCN vcn, ntfs_attr_search_ctx *ctx); 62 - 63 - int ntfs_attr_lookup(const ATTR_TYPE type, const ntfschar *name, 64 - const u32 name_len, const IGNORE_CASE_BOOL ic, 65 - const VCN lowest_vcn, const u8 *val, const u32 val_len, 66 - ntfs_attr_search_ctx *ctx); 67 - 68 - extern int load_attribute_list(ntfs_volume *vol, runlist *rl, u8 *al_start, 69 - const s64 size, const s64 initialized_size); 70 - 71 - static inline s64 ntfs_attr_size(const ATTR_RECORD *a) 73 + static inline s64 ntfs_attr_size(const struct attr_record *a) 72 74 { 73 75 if (!a->non_resident) 74 76 return (s64)le32_to_cpu(a->data.resident.value_length); 75 - return sle64_to_cpu(a->data.non_resident.data_size); 77 + return le64_to_cpu(a->data.non_resident.data_size); 76 78 } 77 79 78 - extern void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx); 79 - extern ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni, 80 - MFT_RECORD *mrec); 81 - extern void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx); 82 - 83 - #ifdef NTFS_RW 84 - 85 - extern int ntfs_attr_size_bounds_check(const ntfs_volume *vol, 86 - const ATTR_TYPE type, const s64 size); 87 - extern int ntfs_attr_can_be_non_resident(const ntfs_volume *vol, 88 - const ATTR_TYPE type); 89 - extern int ntfs_attr_can_be_resident(const ntfs_volume *vol, 90 - const ATTR_TYPE type); 91 - 92 - extern int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size); 93 - extern int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a, 80 + void ntfs_attr_reinit_search_ctx(struct ntfs_attr_search_ctx *ctx); 81 + struct ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(struct ntfs_inode *ni, 82 + struct mft_record *mrec); 83 + void ntfs_attr_put_search_ctx(struct ntfs_attr_search_ctx *ctx); 84 + int ntfs_attr_size_bounds_check(const struct ntfs_volume *vol, 85 + const __le32 type, const s64 size); 86 + int ntfs_attr_can_be_resident(const struct ntfs_volume *vol, 87 + const __le32 type); 88 + int ntfs_attr_map_cluster(struct ntfs_inode *ni, s64 vcn_start, s64 *lcn_start, 89 + s64 *lcn_count, s64 max_clu_count, bool *balloc, bool update_mp, bool skip_holes); 90 + int ntfs_attr_record_resize(struct mft_record *m, struct attr_record *a, u32 new_size); 91 + int ntfs_resident_attr_value_resize(struct mft_record *m, struct attr_record *a, 94 92 const u32 new_size); 95 - 96 - extern int ntfs_attr_make_non_resident(ntfs_inode *ni, const u32 data_size); 97 - 98 - extern s64 ntfs_attr_extend_allocation(ntfs_inode *ni, s64 new_alloc_size, 99 - const s64 new_data_size, const s64 data_start); 100 - 101 - extern int ntfs_attr_set(ntfs_inode *ni, const s64 ofs, const s64 cnt, 93 + int ntfs_attr_make_non_resident(struct ntfs_inode *ni, const u32 data_size); 94 + int ntfs_attr_set(struct ntfs_inode *ni, const s64 ofs, const s64 cnt, 102 95 const u8 val); 96 + int ntfs_attr_set_initialized_size(struct ntfs_inode *ni, loff_t new_size); 97 + int ntfs_attr_open(struct ntfs_inode *ni, const __le32 type, 98 + __le16 *name, u32 name_len); 99 + void ntfs_attr_close(struct ntfs_inode *n); 100 + int ntfs_attr_fallocate(struct ntfs_inode *ni, loff_t start, loff_t byte_len, bool keep_size); 101 + int ntfs_non_resident_attr_insert_range(struct ntfs_inode *ni, s64 start_vcn, s64 len); 102 + int ntfs_non_resident_attr_collapse_range(struct ntfs_inode *ni, s64 start_vcn, s64 len); 103 + int ntfs_non_resident_attr_punch_hole(struct ntfs_inode *ni, s64 start_vcn, s64 len); 104 + int __ntfs_attr_truncate_vfs(struct ntfs_inode *ni, const s64 newsize, 105 + const s64 i_size); 106 + int ntfs_attr_expand(struct ntfs_inode *ni, const s64 newsize, const s64 prealloc_size); 107 + int ntfs_attr_truncate_i(struct ntfs_inode *ni, const s64 newsize, unsigned int holes); 108 + int ntfs_attr_truncate(struct ntfs_inode *ni, const s64 newsize); 109 + int ntfs_attr_rm(struct ntfs_inode *ni); 110 + int ntfs_attr_exist(struct ntfs_inode *ni, const __le32 type, __le16 *name, 111 + u32 name_len); 112 + int ntfs_attr_remove(struct ntfs_inode *ni, const __le32 type, __le16 *name, 113 + u32 name_len); 114 + int ntfs_attr_record_rm(struct ntfs_attr_search_ctx *ctx); 115 + int ntfs_attr_record_move_to(struct ntfs_attr_search_ctx *ctx, struct ntfs_inode *ni); 116 + int ntfs_attr_add(struct ntfs_inode *ni, __le32 type, 117 + __le16 *name, u8 name_len, u8 *val, s64 size); 118 + int ntfs_attr_record_move_away(struct ntfs_attr_search_ctx *ctx, int extra); 119 + char *ntfs_attr_name_get(const struct ntfs_volume *vol, const __le16 *uname, 120 + const int uname_len); 121 + void ntfs_attr_name_free(unsigned char **name); 122 + void *ntfs_attr_readall(struct ntfs_inode *ni, const __le32 type, 123 + __le16 *name, u32 name_len, s64 *data_size); 124 + int ntfs_resident_attr_record_add(struct ntfs_inode *ni, __le32 type, 125 + __le16 *name, u8 name_len, u8 *val, u32 size, 126 + __le16 flags); 127 + int ntfs_attr_update_mapping_pairs(struct ntfs_inode *ni, s64 from_vcn); 128 + struct runlist_element *ntfs_attr_vcn_to_rl(struct ntfs_inode *ni, s64 vcn, s64 *lcn); 103 129 104 - #endif /* NTFS_RW */ 105 - 130 + /* 131 + * ntfs_attrs_walk - syntactic sugar for walking all attributes in an inode 132 + * @ctx: initialised attribute search context 133 + * 134 + * Syntactic sugar for walking attributes in an inode. 135 + * 136 + * Return 0 on success and -1 on error with errno set to the error code from 137 + * ntfs_attr_lookup(). 138 + * 139 + * Example: When you want to enumerate all attributes in an open ntfs inode 140 + * @ni, you can simply do: 141 + * 142 + * int err; 143 + * struct ntfs_attr_search_ctx *ctx = ntfs_attr_get_search_ctx(ni, NULL); 144 + * if (!ctx) 145 + * // Error code is in errno. Handle this case. 146 + * while (!(err = ntfs_attrs_walk(ctx))) { 147 + * struct attr_record *attr = ctx->attr; 148 + * // attr now contains the next attribute. Do whatever you want 149 + * // with it and then just continue with the while loop. 150 + * } 151 + * if (err && errno != ENOENT) 152 + * // Ooops. An error occurred! You should handle this case. 153 + * // Now finished with all attributes in the inode. 154 + */ 155 + static inline int ntfs_attrs_walk(struct ntfs_attr_search_ctx *ctx) 156 + { 157 + return ntfs_attr_lookup(AT_UNUSED, NULL, 0, CASE_SENSITIVE, 0, 158 + NULL, 0, ctx); 159 + } 106 160 #endif /* _LINUX_NTFS_ATTRIB_H */
+20
fs/ntfs/attrlist.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * Exports for attribute list attribute handling. 4 + * 5 + * Copyright (c) 2004 Anton Altaparmakov 6 + * Copyright (c) 2004 Yura Pakhuchiy 7 + * Copyright (c) 2025 LG Electronics Co., Ltd. 8 + */ 9 + 10 + #ifndef _NTFS_ATTRLIST_H 11 + #define _NTFS_ATTRLIST_H 12 + 13 + #include "attrib.h" 14 + 15 + int ntfs_attrlist_need(struct ntfs_inode *ni); 16 + int ntfs_attrlist_entry_add(struct ntfs_inode *ni, struct attr_record *attr); 17 + int ntfs_attrlist_entry_rm(struct ntfs_attr_search_ctx *ctx); 18 + int ntfs_attrlist_update(struct ntfs_inode *base_ni); 19 + 20 + #endif /* defined _NTFS_ATTRLIST_H */
+9 -13
fs/ntfs/bitmap.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * bitmap.h - Defines for NTFS kernel bitmap handling. Part of the Linux-NTFS 4 - * project. 3 + * Defines for NTFS kernel bitmap handling. 5 4 * 6 5 * Copyright (c) 2004 Anton Altaparmakov 7 6 */ ··· 8 9 #ifndef _LINUX_NTFS_BITMAP_H 9 10 #define _LINUX_NTFS_BITMAP_H 10 11 11 - #ifdef NTFS_RW 12 - 13 12 #include <linux/fs.h> 14 13 15 - #include "types.h" 14 + #include "volume.h" 16 15 17 - extern int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit, 16 + int ntfs_trim_fs(struct ntfs_volume *vol, struct fstrim_range *range); 17 + int __ntfs_bitmap_set_bits_in_run(struct inode *vi, const s64 start_bit, 18 18 const s64 count, const u8 value, const bool is_rollback); 19 19 20 - /** 20 + /* 21 21 * ntfs_bitmap_set_bits_in_run - set a run of bits in a bitmap to a value 22 22 * @vi: vfs inode describing the bitmap 23 23 * @start_bit: first bit to set ··· 35 37 false); 36 38 } 37 39 38 - /** 40 + /* 39 41 * ntfs_bitmap_set_run - set a run of bits in a bitmap 40 42 * @vi: vfs inode describing the bitmap 41 43 * @start_bit: first bit to set ··· 52 54 return ntfs_bitmap_set_bits_in_run(vi, start_bit, count, 1); 53 55 } 54 56 55 - /** 57 + /* 56 58 * ntfs_bitmap_clear_run - clear a run of bits in a bitmap 57 59 * @vi: vfs inode describing the bitmap 58 60 * @start_bit: first bit to clear ··· 69 71 return ntfs_bitmap_set_bits_in_run(vi, start_bit, count, 0); 70 72 } 71 73 72 - /** 74 + /* 73 75 * ntfs_bitmap_set_bit - set a bit in a bitmap 74 76 * @vi: vfs inode describing the bitmap 75 77 * @bit: bit to set ··· 83 85 return ntfs_bitmap_set_run(vi, bit, 1); 84 86 } 85 87 86 - /** 88 + /* 87 89 * ntfs_bitmap_clear_bit - clear a bit in a bitmap 88 90 * @vi: vfs inode describing the bitmap 89 91 * @bit: bit to clear ··· 96 98 { 97 99 return ntfs_bitmap_clear_run(vi, bit, 1); 98 100 } 99 - 100 - #endif /* NTFS_RW */ 101 101 102 102 #endif /* defined _LINUX_NTFS_BITMAP_H */
+13 -13
fs/ntfs/collate.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * collate.h - Defines for NTFS kernel collation handling. Part of the 4 - * Linux-NTFS project. 3 + * Defines for NTFS kernel collation handling. 5 4 * 6 5 * Copyright (c) 2004 Anton Altaparmakov 6 + * 7 + * Part of this file is based on code from the NTFS-3G. 8 + * and is copyrighted by the respective authors below: 9 + * Copyright (c) 2004 Anton Altaparmakov 10 + * Copyright (c) 2005 Yura Pakhuchiy 7 11 */ 8 12 9 13 #ifndef _LINUX_NTFS_COLLATE_H 10 14 #define _LINUX_NTFS_COLLATE_H 11 15 12 - #include "types.h" 13 16 #include "volume.h" 14 17 15 - static inline bool ntfs_is_collation_rule_supported(COLLATION_RULE cr) { 18 + static inline bool ntfs_is_collation_rule_supported(__le32 cr) 19 + { 16 20 int i; 17 21 18 - /* 19 - * FIXME: At the moment we only support COLLATION_BINARY and 20 - * COLLATION_NTOFS_ULONG, so we return false for everything else for 21 - * now. 22 - */ 23 - if (unlikely(cr != COLLATION_BINARY && cr != COLLATION_NTOFS_ULONG)) 22 + if (unlikely(cr != COLLATION_BINARY && cr != COLLATION_NTOFS_ULONG && 23 + cr != COLLATION_FILE_NAME) && cr != COLLATION_NTOFS_ULONGS) 24 24 return false; 25 25 i = le32_to_cpu(cr); 26 26 if (likely(((i >= 0) && (i <= 0x02)) || ··· 29 29 return false; 30 30 } 31 31 32 - extern int ntfs_collate(ntfs_volume *vol, COLLATION_RULE cr, 33 - const void *data1, const int data1_len, 34 - const void *data2, const int data2_len); 32 + int ntfs_collate(struct ntfs_volume *vol, __le32 cr, 33 + const void *data1, const u32 data1_len, 34 + const void *data2, const u32 data2_len); 35 35 36 36 #endif /* _LINUX_NTFS_COLLATE_H */
+11 -5
fs/ntfs/debug.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * debug.h - NTFS kernel debug support. Part of the Linux-NTFS project. 3 + * NTFS kernel debug support. 4 4 * 5 5 * Copyright (c) 2001-2004 Anton Altaparmakov 6 6 */ ··· 19 19 extern __printf(4, 5) 20 20 void __ntfs_debug(const char *file, int line, const char *function, 21 21 const char *format, ...); 22 - /** 22 + /* 23 23 * ntfs_debug - write a debug level message to syslog 24 24 * @f: a printf format string containing the message 25 25 * @...: the variables to substitute into @f ··· 30 30 #define ntfs_debug(f, a...) \ 31 31 __ntfs_debug(__FILE__, __LINE__, __func__, f, ##a) 32 32 33 - extern void ntfs_debug_dump_runlist(const runlist_element *rl); 33 + void ntfs_debug_dump_runlist(const struct runlist_element *rl); 34 34 35 35 #else /* !DEBUG */ 36 36 ··· 40 40 no_printk(fmt, ##__VA_ARGS__); \ 41 41 } while (0) 42 42 43 - #define ntfs_debug_dump_runlist(rl) do {} while (0) 43 + #define ntfs_debug_dump_runlist(rl) \ 44 + do { \ 45 + if (0) \ 46 + (void)rl; \ 47 + } while (0) 44 48 45 49 #endif /* !DEBUG */ 46 50 ··· 54 50 #define ntfs_warning(sb, f, a...) __ntfs_warning(__func__, sb, f, ##a) 55 51 56 52 extern __printf(3, 4) 57 - void __ntfs_error(const char *function, const struct super_block *sb, 53 + void __ntfs_error(const char *function, struct super_block *sb, 58 54 const char *fmt, ...); 59 55 #define ntfs_error(sb, f, a...) __ntfs_error(__func__, sb, f, ##a) 56 + 57 + void ntfs_handle_error(struct super_block *sb); 60 58 61 59 #endif /* _LINUX_NTFS_DEBUG_H */
+10 -12
fs/ntfs/dir.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * dir.h - Defines for directory handling in NTFS Linux kernel driver. Part of 4 - * the Linux-NTFS project. 3 + * Defines for directory handling in NTFS Linux kernel driver. 5 4 * 6 5 * Copyright (c) 2002-2004 Anton Altaparmakov 7 6 */ ··· 8 9 #ifndef _LINUX_NTFS_DIR_H 9 10 #define _LINUX_NTFS_DIR_H 10 11 11 - #include "layout.h" 12 12 #include "inode.h" 13 - #include "types.h" 14 13 15 14 /* 16 15 * ntfs_name is used to return the file name to the caller of 17 16 * ntfs_lookup_inode_by_name() in order for the caller (namei.c::ntfs_lookup()) 18 17 * to be able to deal with dcache aliasing issues. 19 18 */ 20 - typedef struct { 21 - MFT_REF mref; 22 - FILE_NAME_TYPE_FLAGS type; 19 + struct ntfs_name { 20 + u64 mref; 21 + u8 type; 23 22 u8 len; 24 - ntfschar name[0]; 25 - } __attribute__ ((__packed__)) ntfs_name; 23 + __le16 name[]; 24 + } __packed; 26 25 27 26 /* The little endian Unicode string $I30 as a global constant. */ 28 - extern ntfschar I30[5]; 27 + extern __le16 I30[5]; 29 28 30 - extern MFT_REF ntfs_lookup_inode_by_name(ntfs_inode *dir_ni, 31 - const ntfschar *uname, const int uname_len, ntfs_name **res); 29 + u64 ntfs_lookup_inode_by_name(struct ntfs_inode *dir_ni, 30 + const __le16 *uname, const int uname_len, struct ntfs_name **res); 31 + int ntfs_check_empty_dir(struct ntfs_inode *ni, struct mft_record *ni_mrec); 32 32 33 33 #endif /* _LINUX_NTFS_FS_DIR_H */
+30
fs/ntfs/ea.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + 3 + #ifndef _LINUX_NTFS_EA_H 4 + #define _LINUX_NTFS_EA_H 5 + 6 + #define NTFS_EA_UID BIT(1) 7 + #define NTFS_EA_GID BIT(2) 8 + #define NTFS_EA_MODE BIT(3) 9 + 10 + extern const struct xattr_handler *const ntfs_xattr_handlers[]; 11 + 12 + int ntfs_ea_set_wsl_not_symlink(struct ntfs_inode *ni, mode_t mode, dev_t dev); 13 + int ntfs_ea_get_wsl_inode(struct inode *inode, dev_t *rdevp, unsigned int flags); 14 + int ntfs_ea_set_wsl_inode(struct inode *inode, dev_t rdev, __le16 *ea_size, 15 + unsigned int flags); 16 + ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size); 17 + 18 + #ifdef CONFIG_NTFS_FS_POSIX_ACL 19 + struct posix_acl *ntfs_get_acl(struct mnt_idmap *idmap, struct dentry *dentry, 20 + int type); 21 + int ntfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 22 + struct posix_acl *acl, int type); 23 + int ntfs_init_acl(struct mnt_idmap *idmap, struct inode *inode, 24 + struct inode *dir); 25 + #else 26 + #define ntfs_get_acl NULL 27 + #define ntfs_set_acl NULL 28 + #endif 29 + 30 + #endif /* _LINUX_NTFS_EA_H */
-79
fs/ntfs/endian.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 - /* 3 - * endian.h - Defines for endianness handling in NTFS Linux kernel driver. 4 - * Part of the Linux-NTFS project. 5 - * 6 - * Copyright (c) 2001-2004 Anton Altaparmakov 7 - */ 8 - 9 - #ifndef _LINUX_NTFS_ENDIAN_H 10 - #define _LINUX_NTFS_ENDIAN_H 11 - 12 - #include <asm/byteorder.h> 13 - #include "types.h" 14 - 15 - /* 16 - * Signed endianness conversion functions. 17 - */ 18 - 19 - static inline s16 sle16_to_cpu(sle16 x) 20 - { 21 - return le16_to_cpu((__force le16)x); 22 - } 23 - 24 - static inline s32 sle32_to_cpu(sle32 x) 25 - { 26 - return le32_to_cpu((__force le32)x); 27 - } 28 - 29 - static inline s64 sle64_to_cpu(sle64 x) 30 - { 31 - return le64_to_cpu((__force le64)x); 32 - } 33 - 34 - static inline s16 sle16_to_cpup(sle16 *x) 35 - { 36 - return le16_to_cpu(*(__force le16*)x); 37 - } 38 - 39 - static inline s32 sle32_to_cpup(sle32 *x) 40 - { 41 - return le32_to_cpu(*(__force le32*)x); 42 - } 43 - 44 - static inline s64 sle64_to_cpup(sle64 *x) 45 - { 46 - return le64_to_cpu(*(__force le64*)x); 47 - } 48 - 49 - static inline sle16 cpu_to_sle16(s16 x) 50 - { 51 - return (__force sle16)cpu_to_le16(x); 52 - } 53 - 54 - static inline sle32 cpu_to_sle32(s32 x) 55 - { 56 - return (__force sle32)cpu_to_le32(x); 57 - } 58 - 59 - static inline sle64 cpu_to_sle64(s64 x) 60 - { 61 - return (__force sle64)cpu_to_le64(x); 62 - } 63 - 64 - static inline sle16 cpu_to_sle16p(s16 *x) 65 - { 66 - return (__force sle16)cpu_to_le16(*x); 67 - } 68 - 69 - static inline sle32 cpu_to_sle32p(s32 *x) 70 - { 71 - return (__force sle32)cpu_to_le32(*x); 72 - } 73 - 74 - static inline sle64 cpu_to_sle64p(s64 *x) 75 - { 76 - return (__force sle64)cpu_to_le64(*x); 77 - } 78 - 79 - #endif /* _LINUX_NTFS_ENDIAN_H */
+56 -79
fs/ntfs/index.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * index.h - Defines for NTFS kernel index handling. Part of the Linux-NTFS 4 - * project. 3 + * Defines for NTFS kernel index handling. 5 4 * 6 5 * Copyright (c) 2004 Anton Altaparmakov 7 6 */ ··· 10 11 11 12 #include <linux/fs.h> 12 13 13 - #include "types.h" 14 - #include "layout.h" 15 - #include "inode.h" 16 14 #include "attrib.h" 17 15 #include "mft.h" 18 - #include "aops.h" 19 16 20 - /** 17 + #define VCN_INDEX_ROOT_PARENT ((s64)-2) 18 + 19 + #define MAX_PARENT_VCN 32 20 + 21 + /* 21 22 * @idx_ni: index inode containing the @entry described by this context 23 + * @name: Unicode name of the indexed attribute 24 + * (usually $I30 for directories) 25 + * @name_len: length of @name in Unicode characters 22 26 * @entry: index entry (points into @ir or @ia) 27 + * @cr: creation time of the entry (for sorting/validation) 23 28 * @data: index entry data (points into @entry) 24 29 * @data_len: length in bytes of @data 25 30 * @is_in_root: 'true' if @entry is in @ir and 'false' if it is in @ia 26 31 * @ir: index root if @is_in_root and NULL otherwise 27 32 * @actx: attribute search context if @is_in_root and NULL otherwise 28 - * @base_ni: base inode if @is_in_root and NULL otherwise 29 - * @ia: index block if @is_in_root is 'false' and NULL otherwise 30 - * @page: page if @is_in_root is 'false' and NULL otherwise 33 + * @ib: index block header (valid when @is_in_root is 'false') 34 + * @ia_ni: index allocation inode (extent inode) for @ia 35 + * @parent_pos: array of parent entry positions in the B-tree nodes 36 + * @parent_vcn: VCNs of parent index blocks in the B-tree traversal 37 + * @pindex: current depth (number of parent nodes) in the traversal 38 + * (maximum is MAX_PARENT_VCN) 39 + * @ib_dirty: true if the current index block (@ia/@ib) was modified 40 + * @block_size: size of index blocks in bytes (from $INDEX_ROOT or $Boot) 41 + * @vcn_size_bits: log2(cluster size) 42 + * @sync_write: true if synchronous writeback is requested for this context 31 43 * 32 44 * @idx_ni is the index inode this context belongs to. 33 45 * ··· 63 53 * When finished with the @entry and its @data, call ntfs_index_ctx_put() to 64 54 * free the context and other associated resources. 65 55 * 66 - * If the index entry was modified, call flush_dcache_index_entry_page() 67 - * immediately after the modification and either ntfs_index_entry_mark_dirty() 56 + * If the index entry was modified, ntfs_index_entry_mark_dirty() 68 57 * or ntfs_index_entry_write() before the call to ntfs_index_ctx_put() to 69 58 * ensure that the changes are written to disk. 70 59 */ 71 - typedef struct { 72 - ntfs_inode *idx_ni; 73 - INDEX_ENTRY *entry; 60 + struct ntfs_index_context { 61 + struct ntfs_inode *idx_ni; 62 + __le16 *name; 63 + u32 name_len; 64 + struct index_entry *entry; 65 + __le32 cr; 74 66 void *data; 75 67 u16 data_len; 76 68 bool is_in_root; 77 - INDEX_ROOT *ir; 78 - ntfs_attr_search_ctx *actx; 79 - ntfs_inode *base_ni; 80 - INDEX_ALLOCATION *ia; 81 - struct page *page; 82 - } ntfs_index_context; 69 + struct index_root *ir; 70 + struct ntfs_attr_search_ctx *actx; 71 + struct index_block *ib; 72 + struct ntfs_inode *ia_ni; 73 + int parent_pos[MAX_PARENT_VCN]; 74 + s64 parent_vcn[MAX_PARENT_VCN]; 75 + int pindex; 76 + bool ib_dirty; 77 + u32 block_size; 78 + u8 vcn_size_bits; 79 + bool sync_write; 80 + }; 83 81 84 - extern ntfs_index_context *ntfs_index_ctx_get(ntfs_inode *idx_ni); 85 - extern void ntfs_index_ctx_put(ntfs_index_context *ictx); 82 + int ntfs_index_entry_inconsistent(struct ntfs_index_context *icx, struct ntfs_volume *vol, 83 + const struct index_entry *ie, __le32 collation_rule, u64 inum); 84 + struct ntfs_index_context *ntfs_index_ctx_get(struct ntfs_inode *ni, __le16 *name, 85 + u32 name_len); 86 + void ntfs_index_ctx_put(struct ntfs_index_context *ictx); 87 + int ntfs_index_lookup(const void *key, const u32 key_len, 88 + struct ntfs_index_context *ictx); 86 89 87 - extern int ntfs_index_lookup(const void *key, const int key_len, 88 - ntfs_index_context *ictx); 89 - 90 - #ifdef NTFS_RW 91 - 92 - /** 93 - * ntfs_index_entry_flush_dcache_page - flush_dcache_page() for index entries 94 - * @ictx: ntfs index context describing the index entry 95 - * 96 - * Call flush_dcache_page() for the page in which an index entry resides. 97 - * 98 - * This must be called every time an index entry is modified, just after the 99 - * modification. 100 - * 101 - * If the index entry is in the index root attribute, simply flush the page 102 - * containing the mft record containing the index root attribute. 103 - * 104 - * If the index entry is in an index block belonging to the index allocation 105 - * attribute, simply flush the page cache page containing the index block. 106 - */ 107 - static inline void ntfs_index_entry_flush_dcache_page(ntfs_index_context *ictx) 108 - { 109 - if (ictx->is_in_root) 110 - flush_dcache_mft_record_page(ictx->actx->ntfs_ino); 111 - else 112 - flush_dcache_page(ictx->page); 113 - } 114 - 115 - /** 116 - * ntfs_index_entry_mark_dirty - mark an index entry dirty 117 - * @ictx: ntfs index context describing the index entry 118 - * 119 - * Mark the index entry described by the index entry context @ictx dirty. 120 - * 121 - * If the index entry is in the index root attribute, simply mark the mft 122 - * record containing the index root attribute dirty. This ensures the mft 123 - * record, and hence the index root attribute, will be written out to disk 124 - * later. 125 - * 126 - * If the index entry is in an index block belonging to the index allocation 127 - * attribute, mark the buffers belonging to the index record as well as the 128 - * page cache page the index block is in dirty. This automatically marks the 129 - * VFS inode of the ntfs index inode to which the index entry belongs dirty, 130 - * too (I_DIRTY_PAGES) and this in turn ensures the page buffers, and hence the 131 - * dirty index block, will be written out to disk later. 132 - */ 133 - static inline void ntfs_index_entry_mark_dirty(ntfs_index_context *ictx) 134 - { 135 - if (ictx->is_in_root) 136 - mark_mft_record_dirty(ictx->actx->ntfs_ino); 137 - else 138 - mark_ntfs_record_dirty(ictx->page, 139 - (u8*)ictx->ia - (u8*)page_address(ictx->page)); 140 - } 141 - 142 - #endif /* NTFS_RW */ 90 + void ntfs_index_entry_mark_dirty(struct ntfs_index_context *ictx); 91 + int ntfs_index_add_filename(struct ntfs_inode *ni, struct file_name_attr *fn, u64 mref); 92 + int ntfs_index_remove(struct ntfs_inode *ni, const void *key, const u32 keylen); 93 + struct ntfs_inode *ntfs_ia_open(struct ntfs_index_context *icx, struct ntfs_inode *ni); 94 + struct index_entry *ntfs_index_walk_down(struct index_entry *ie, struct ntfs_index_context *ictx); 95 + struct index_entry *ntfs_index_next(struct index_entry *ie, struct ntfs_index_context *ictx); 96 + int ntfs_index_rm(struct ntfs_index_context *icx); 97 + void ntfs_index_ctx_reinit(struct ntfs_index_context *icx); 98 + int ntfs_ie_add(struct ntfs_index_context *icx, struct index_entry *ie); 99 + int ntfs_icx_ib_sync_write(struct ntfs_index_context *icx); 143 100 144 101 #endif /* _LINUX_NTFS_INDEX_H */
+250 -201
fs/ntfs/inode.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * inode.h - Defines for inode structures NTFS Linux kernel driver. Part of 4 - * the Linux-NTFS project. 3 + * Defines for inode structures NTFS Linux kernel driver. 5 4 * 6 5 * Copyright (c) 2001-2007 Anton Altaparmakov 7 6 * Copyright (c) 2002 Richard Russon 7 + * Copyright (c) 2025 LG Electronics Co., Ltd. 8 8 */ 9 9 10 10 #ifndef _LINUX_NTFS_INODE_H 11 11 #define _LINUX_NTFS_INODE_H 12 12 13 - #include <linux/atomic.h> 14 - 15 - #include <linux/fs.h> 16 - #include <linux/list.h> 17 - #include <linux/mm.h> 18 - #include <linux/mutex.h> 19 - #include <linux/seq_file.h> 20 - 21 - #include "layout.h" 22 - #include "volume.h" 23 - #include "types.h" 24 - #include "runlist.h" 25 13 #include "debug.h" 26 14 27 - typedef struct _ntfs_inode ntfs_inode; 15 + enum ntfs_inode_mutex_lock_class { 16 + NTFS_INODE_MUTEX_PARENT, 17 + NTFS_INODE_MUTEX_NORMAL, 18 + NTFS_INODE_MUTEX_NORMAL_CHILD, 19 + NTFS_INODE_MUTEX_PARENT_2, 20 + NTFS_INODE_MUTEX_NORMAL_2, 21 + NTFS_EXTEND_MUTEX_PARENT, 22 + NTFS_EA_MUTEX_NORMAL 23 + }; 28 24 29 25 /* 30 26 * The NTFS in-memory inode structure. It is just used as an extension to the 31 27 * fields already provided in the VFS inode. 28 + * @size_lock: Lock serializing access to inode sizes. 29 + * @state: NTFS specific flags describing this inode. 30 + * @flags: Flags describing the file. (Copy from STANDARD_INFORMATION). 31 + * @mft_no: Number of the mft record / inode. 32 + * @seq_no: Sequence number of the mft record. 33 + * @count: Inode reference count for book keeping. 34 + * @vol: Pointer to the ntfs volume of this inode. 35 + * 36 + * If NInoAttr() is true, the below fields describe the attribute which 37 + * this fake inode belongs to. The actual inode of this attribute is 38 + * pointed to by base_ntfs_ino and nr_extents is always set to -1 (see 39 + * below). For real inodes, we also set the type (AT_DATA for files and 40 + * AT_INDEX_ALLOCATION for directories), with the name = NULL and 41 + * name_len = 0 for files and name = I30 (global constant) and 42 + * name_len = 4 for directories. 43 + * @type: Attribute type of this fake inode. 44 + * @name: Attribute name of this fake inode. 45 + * @name_len: Attribute name length of this fake inode. 46 + * @runlist: If state has the NI_NonResident bit set, the runlist of 47 + * the unnamed data attribute (if a file) or of the index allocation 48 + * attribute (directory) or of the attribute described by the fake inode 49 + * (if NInoAttr()). If runlist.rl is NULL, the runlist has not been read 50 + * in yet or has been unmapped. If NI_NonResident is clear, the attribute 51 + * is resident (file and fake inode) or there is no $I30 index allocation 52 + * attribute (small directory). In the latter case runlist.rl is always 53 + * NULL. 54 + * @data_size: Copy from the attribute record. 55 + * @initialized_size: Copy from the attribute record. 56 + * @allocated_size: Copy from the attribute record. 57 + * @i_crtime: File Creation time. 58 + * @mrec: MFT record 59 + * @mrec_lock: Lock for serializing access to the mft record belonging to 60 + * this inode. 61 + * @folio: The folio containing the mft record of the inode. 62 + * @folio_ofs: Offset into the folio at which the mft record begins. 63 + * @mft_lcn: Number containing the mft record. 64 + * @mft_lcn_count: Number of clusters per mft record. 65 + * 66 + * Attribute list support (only for use by the attribute lookup 67 + * functions). Setup during read_inode for all inodes with attribute 68 + * lists. Only valid if NI_AttrList is set in state. 69 + * @attr_list_size: Length of attribute list value in bytes. 70 + * @attr_list: Attribute list value itself. 71 + * 72 + * It is a directory, $MFT, or an index inode. 73 + * @block_size: Size of an index block. 74 + * @vcn_size: Size of a vcn in this index. 75 + * @collation_rule: The collation rule for the index. 76 + * @block_size_bits: Log2 of the above. 77 + * @vcn_size_bits: Log2 of the above. 78 + * 79 + * It is a compressed/sparse file/attribute inode. 80 + * @size: Copy of compressed_size from $DATA. 81 + * @block_size: Size of a compression block (cb). 82 + * @block_size_bits: Log2 of the size of a cb. 83 + * @block_clusters: Number of clusters per cb. 84 + * @extent_lock: Lock for accessing/modifying the below. 85 + * @nr_extents: For a base mft record, the number of attached extent inodes 86 + * (0 if none), for extent records and for fake inodes describing an 87 + * attribute this is -1. 88 + * 89 + * This union is only used if nr_extents != 0. 90 + * @extent_ntfs_inos: For nr_extents > 0, array of the ntfs inodes of 91 + * the extent mft records belonging to this base inode which have been 92 + * loaded. 93 + * @base_ntfs_ino: For nr_extents == -1, the ntfs inode of the base mft 94 + * record. For fake inodes, the real (base) inode to which the attribute 95 + * belongs. 96 + * @i_dealloc_clusters: delayed allocated clusters. 97 + * @target: symlink buffer. 32 98 */ 33 - struct _ntfs_inode { 34 - rwlock_t size_lock; /* Lock serializing access to inode sizes. */ 35 - s64 initialized_size; /* Copy from the attribute record. */ 36 - s64 allocated_size; /* Copy from the attribute record. */ 37 - unsigned long state; /* NTFS specific flags describing this inode. 38 - See ntfs_inode_state_bits below. */ 39 - unsigned long mft_no; /* Number of the mft record / inode. */ 40 - u16 seq_no; /* Sequence number of the mft record. */ 41 - atomic_t count; /* Inode reference count for book keeping. */ 42 - ntfs_volume *vol; /* Pointer to the ntfs volume of this inode. */ 43 - /* 44 - * If NInoAttr() is true, the below fields describe the attribute which 45 - * this fake inode belongs to. The actual inode of this attribute is 46 - * pointed to by base_ntfs_ino and nr_extents is always set to -1 (see 47 - * below). For real inodes, we also set the type (AT_DATA for files and 48 - * AT_INDEX_ALLOCATION for directories), with the name = NULL and 49 - * name_len = 0 for files and name = I30 (global constant) and 50 - * name_len = 4 for directories. 51 - */ 52 - ATTR_TYPE type; /* Attribute type of this fake inode. */ 53 - ntfschar *name; /* Attribute name of this fake inode. */ 54 - u32 name_len; /* Attribute name length of this fake inode. */ 55 - runlist runlist; /* If state has the NI_NonResident bit set, 56 - the runlist of the unnamed data attribute 57 - (if a file) or of the index allocation 58 - attribute (directory) or of the attribute 59 - described by the fake inode (if NInoAttr()). 60 - If runlist.rl is NULL, the runlist has not 61 - been read in yet or has been unmapped. If 62 - NI_NonResident is clear, the attribute is 63 - resident (file and fake inode) or there is 64 - no $I30 index allocation attribute 65 - (small directory). In the latter case 66 - runlist.rl is always NULL.*/ 67 - /* 68 - * The following fields are only valid for real inodes and extent 69 - * inodes. 70 - */ 71 - struct mutex mrec_lock; /* Lock for serializing access to the 72 - mft record belonging to this inode. */ 73 - struct page *page; /* The page containing the mft record of the 74 - inode. This should only be touched by the 75 - (un)map_mft_record*() functions. */ 76 - int page_ofs; /* Offset into the page at which the mft record 77 - begins. This should only be touched by the 78 - (un)map_mft_record*() functions. */ 79 - /* 80 - * Attribute list support (only for use by the attribute lookup 81 - * functions). Setup during read_inode for all inodes with attribute 82 - * lists. Only valid if NI_AttrList is set in state, and attr_list_rl is 83 - * further only valid if NI_AttrListNonResident is set. 84 - */ 85 - u32 attr_list_size; /* Length of attribute list value in bytes. */ 86 - u8 *attr_list; /* Attribute list value itself. */ 87 - runlist attr_list_rl; /* Run list for the attribute list value. */ 99 + struct ntfs_inode { 100 + rwlock_t size_lock; 101 + unsigned long state; 102 + __le32 flags; 103 + unsigned long mft_no; 104 + u16 seq_no; 105 + atomic_t count; 106 + struct ntfs_volume *vol; 107 + __le32 type; 108 + __le16 *name; 109 + u32 name_len; 110 + struct runlist runlist; 111 + s64 data_size; 112 + s64 initialized_size; 113 + s64 allocated_size; 114 + struct timespec64 i_crtime; 115 + void *mrec; 116 + struct mutex mrec_lock; 117 + struct folio *folio; 118 + int folio_ofs; 119 + s64 mft_lcn[2]; 120 + unsigned int mft_lcn_count; 121 + u32 attr_list_size; 122 + u8 *attr_list; 88 123 union { 89 - struct { /* It is a directory, $MFT, or an index inode. */ 90 - u32 block_size; /* Size of an index block. */ 91 - u32 vcn_size; /* Size of a vcn in this 92 - index. */ 93 - COLLATION_RULE collation_rule; /* The collation rule 94 - for the index. */ 95 - u8 block_size_bits; /* Log2 of the above. */ 96 - u8 vcn_size_bits; /* Log2 of the above. */ 124 + struct { 125 + u32 block_size; 126 + u32 vcn_size; 127 + __le32 collation_rule; 128 + u8 block_size_bits; 129 + u8 vcn_size_bits; 97 130 } index; 98 - struct { /* It is a compressed/sparse file/attribute inode. */ 99 - s64 size; /* Copy of compressed_size from 100 - $DATA. */ 101 - u32 block_size; /* Size of a compression block 102 - (cb). */ 103 - u8 block_size_bits; /* Log2 of the size of a cb. */ 104 - u8 block_clusters; /* Number of clusters per cb. */ 131 + struct { 132 + s64 size; 133 + u32 block_size; 134 + u8 block_size_bits; 135 + u8 block_clusters; 105 136 } compressed; 106 137 } itype; 107 - struct mutex extent_lock; /* Lock for accessing/modifying the 108 - below . */ 109 - s32 nr_extents; /* For a base mft record, the number of attached extent 110 - inodes (0 if none), for extent records and for fake 111 - inodes describing an attribute this is -1. */ 112 - union { /* This union is only used if nr_extents != 0. */ 113 - ntfs_inode **extent_ntfs_inos; /* For nr_extents > 0, array of 114 - the ntfs inodes of the extent 115 - mft records belonging to 116 - this base inode which have 117 - been loaded. */ 118 - ntfs_inode *base_ntfs_ino; /* For nr_extents == -1, the 119 - ntfs inode of the base mft 120 - record. For fake inodes, the 121 - real (base) inode to which 122 - the attribute belongs. */ 138 + struct mutex extent_lock; 139 + s32 nr_extents; 140 + union { 141 + struct ntfs_inode **extent_ntfs_inos; 142 + struct ntfs_inode *base_ntfs_ino; 123 143 } ext; 144 + unsigned int i_dealloc_clusters; 145 + char *target; 124 146 }; 125 147 126 148 /* 127 149 * Defined bits for the state field in the ntfs_inode structure. 128 150 * (f) = files only, (d) = directories only, (a) = attributes/fake inodes only 151 + * 152 + * NI_Dirty Mft record needs to be written to disk. 153 + * NI_AttrListDirty Mft record contains an attribute list. 154 + * NI_AttrList Mft record contains an attribute list. 155 + * NI_AttrListNonResident Attribute list is non-resident. Implies 156 + * NI_AttrList is set. 157 + * NI_Attr 1: Fake inode for attribute i/o. 158 + * 0: Real inode or extent inode. 159 + * NI_MstProtected Attribute is protected by MST fixups. 160 + * NI_NonResident Unnamed data attr is non-resident (f) 161 + * Attribute is non-resident (a). 162 + * NI_IndexAllocPresent $I30 index alloc attr is present (d). 163 + * NI_Compressed Unnamed data attr is compressed (f). 164 + * Create compressed files by default (d). 165 + * Attribute is compressed (a). 166 + * NI_Encrypted Unnamed data attr is encrypted (f). 167 + * Create encrypted files by default (d). 168 + * Attribute is encrypted (a). 169 + * NI_Sparse Unnamed data attr is sparse (f). 170 + * Create sparse files by default (d). 171 + * Attribute is sparse (a). 172 + * NI_SparseDisabled May not create sparse regions. 173 + * NI_FullyMapped Runlist is fully mapped. 174 + * NI_FileNameDirty FILE_NAME attributes need to be updated. 175 + * NI_BeingDeleted ntfs inode is being delated. 176 + * NI_BeingCreated ntfs inode is being created. 177 + * NI_HasEA ntfs inode has EA attribute. 178 + * NI_RunlistDirty runlist need to be updated. 129 179 */ 130 - typedef enum { 131 - NI_Dirty, /* 1: Mft record needs to be written to disk. */ 132 - NI_AttrList, /* 1: Mft record contains an attribute list. */ 133 - NI_AttrListNonResident, /* 1: Attribute list is non-resident. Implies 134 - NI_AttrList is set. */ 135 - 136 - NI_Attr, /* 1: Fake inode for attribute i/o. 137 - 0: Real inode or extent inode. */ 138 - 139 - NI_MstProtected, /* 1: Attribute is protected by MST fixups. 140 - 0: Attribute is not protected by fixups. */ 141 - NI_NonResident, /* 1: Unnamed data attr is non-resident (f). 142 - 1: Attribute is non-resident (a). */ 143 - NI_IndexAllocPresent = NI_NonResident, /* 1: $I30 index alloc attr is 144 - present (d). */ 145 - NI_Compressed, /* 1: Unnamed data attr is compressed (f). 146 - 1: Create compressed files by default (d). 147 - 1: Attribute is compressed (a). */ 148 - NI_Encrypted, /* 1: Unnamed data attr is encrypted (f). 149 - 1: Create encrypted files by default (d). 150 - 1: Attribute is encrypted (a). */ 151 - NI_Sparse, /* 1: Unnamed data attr is sparse (f). 152 - 1: Create sparse files by default (d). 153 - 1: Attribute is sparse (a). */ 154 - NI_SparseDisabled, /* 1: May not create sparse regions. */ 155 - NI_TruncateFailed, /* 1: Last ntfs_truncate() call failed. */ 156 - } ntfs_inode_state_bits; 180 + enum { 181 + NI_Dirty, 182 + NI_AttrListDirty, 183 + NI_AttrList, 184 + NI_AttrListNonResident, 185 + NI_Attr, 186 + NI_MstProtected, 187 + NI_NonResident, 188 + NI_IndexAllocPresent, 189 + NI_Compressed, 190 + NI_Encrypted, 191 + NI_Sparse, 192 + NI_SparseDisabled, 193 + NI_FullyMapped, 194 + NI_FileNameDirty, 195 + NI_BeingDeleted, 196 + NI_BeingCreated, 197 + NI_HasEA, 198 + NI_RunlistDirty, 199 + }; 157 200 158 201 /* 159 202 * NOTE: We should be adding dirty mft records to a list somewhere and they ··· 208 165 * Macro tricks to expand the NInoFoo(), NInoSetFoo(), and NInoClearFoo() 209 166 * functions. 210 167 */ 211 - #define NINO_FNS(flag) \ 212 - static inline int NIno##flag(ntfs_inode *ni) \ 213 - { \ 214 - return test_bit(NI_##flag, &(ni)->state); \ 215 - } \ 216 - static inline void NInoSet##flag(ntfs_inode *ni) \ 217 - { \ 218 - set_bit(NI_##flag, &(ni)->state); \ 219 - } \ 220 - static inline void NInoClear##flag(ntfs_inode *ni) \ 221 - { \ 222 - clear_bit(NI_##flag, &(ni)->state); \ 168 + #define NINO_FNS(flag) \ 169 + static inline int NIno##flag(struct ntfs_inode *ni) \ 170 + { \ 171 + return test_bit(NI_##flag, &(ni)->state); \ 172 + } \ 173 + static inline void NInoSet##flag(struct ntfs_inode *ni) \ 174 + { \ 175 + set_bit(NI_##flag, &(ni)->state); \ 176 + } \ 177 + static inline void NInoClear##flag(struct ntfs_inode *ni) \ 178 + { \ 179 + clear_bit(NI_##flag, &(ni)->state); \ 223 180 } 224 181 225 182 /* 226 183 * As above for NInoTestSetFoo() and NInoTestClearFoo(). 227 184 */ 228 - #define TAS_NINO_FNS(flag) \ 229 - static inline int NInoTestSet##flag(ntfs_inode *ni) \ 230 - { \ 231 - return test_and_set_bit(NI_##flag, &(ni)->state); \ 232 - } \ 233 - static inline int NInoTestClear##flag(ntfs_inode *ni) \ 234 - { \ 235 - return test_and_clear_bit(NI_##flag, &(ni)->state); \ 185 + #define TAS_NINO_FNS(flag) \ 186 + static inline int NInoTestSet##flag(struct ntfs_inode *ni) \ 187 + { \ 188 + return test_and_set_bit(NI_##flag, &(ni)->state); \ 189 + } \ 190 + static inline int NInoTestClear##flag(struct ntfs_inode *ni) \ 191 + { \ 192 + return test_and_clear_bit(NI_##flag, &(ni)->state); \ 236 193 } 237 194 238 195 /* Emit the ntfs inode bitops functions. */ 239 196 NINO_FNS(Dirty) 240 197 TAS_NINO_FNS(Dirty) 241 198 NINO_FNS(AttrList) 199 + NINO_FNS(AttrListDirty) 242 200 NINO_FNS(AttrListNonResident) 243 201 NINO_FNS(Attr) 244 202 NINO_FNS(MstProtected) ··· 249 205 NINO_FNS(Encrypted) 250 206 NINO_FNS(Sparse) 251 207 NINO_FNS(SparseDisabled) 252 - NINO_FNS(TruncateFailed) 208 + NINO_FNS(FullyMapped) 209 + NINO_FNS(FileNameDirty) 210 + TAS_NINO_FNS(FileNameDirty) 211 + NINO_FNS(BeingDeleted) 212 + NINO_FNS(HasEA) 213 + NINO_FNS(RunlistDirty) 253 214 254 215 /* 255 216 * The full structure containing a ntfs_inode and a vfs struct inode. Used for 256 217 * all real and fake inodes but not for extent inodes which lack the vfs struct 257 218 * inode. 258 219 */ 259 - typedef struct { 260 - ntfs_inode ntfs_inode; 220 + struct big_ntfs_inode { 221 + struct ntfs_inode ntfs_inode; 261 222 struct inode vfs_inode; /* The vfs inode structure. */ 262 - } big_ntfs_inode; 223 + }; 263 224 264 - /** 225 + /* 265 226 * NTFS_I - return the ntfs inode given a vfs inode 266 227 * @inode: VFS inode 267 228 * 268 229 * NTFS_I() returns the ntfs inode associated with the VFS @inode. 269 230 */ 270 - static inline ntfs_inode *NTFS_I(struct inode *inode) 231 + static inline struct ntfs_inode *NTFS_I(struct inode *inode) 271 232 { 272 - return (ntfs_inode *)container_of(inode, big_ntfs_inode, vfs_inode); 233 + return &container_of(inode, struct big_ntfs_inode, vfs_inode)->ntfs_inode; 273 234 } 274 235 275 - static inline struct inode *VFS_I(ntfs_inode *ni) 236 + static inline struct inode *VFS_I(struct ntfs_inode *ni) 276 237 { 277 - return &((big_ntfs_inode *)ni)->vfs_inode; 238 + return &container_of(ni, struct big_ntfs_inode, ntfs_inode)->vfs_inode; 278 239 } 279 240 280 - /** 241 + /* 281 242 * ntfs_attr - ntfs in memory attribute structure 282 - * @mft_no: mft record number of the base mft record of this attribute 283 - * @name: Unicode name of the attribute (NULL if unnamed) 284 - * @name_len: length of @name in Unicode characters (0 if unnamed) 285 - * @type: attribute type (see layout.h) 286 243 * 287 244 * This structure exists only to provide a small structure for the 288 245 * ntfs_{attr_}iget()/ntfs_test_inode()/ntfs_init_locked_inode() mechanism. ··· 291 246 * NOTE: Elements are ordered by size to make the structure as compact as 292 247 * possible on all architectures. 293 248 */ 294 - typedef struct { 249 + struct ntfs_attr { 295 250 unsigned long mft_no; 296 - ntfschar *name; 251 + __le16 *name; 297 252 u32 name_len; 298 - ATTR_TYPE type; 299 - } ntfs_attr; 253 + __le32 type; 254 + unsigned long state; 255 + }; 300 256 301 - extern int ntfs_test_inode(struct inode *vi, void *data); 302 - 303 - extern struct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no); 304 - extern struct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPE type, 305 - ntfschar *name, u32 name_len); 306 - extern struct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name, 257 + int ntfs_test_inode(struct inode *vi, void *data); 258 + struct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no); 259 + struct inode *ntfs_attr_iget(struct inode *base_vi, __le32 type, 260 + __le16 *name, u32 name_len); 261 + struct inode *ntfs_index_iget(struct inode *base_vi, __le16 *name, 307 262 u32 name_len); 308 - 309 - extern struct inode *ntfs_alloc_big_inode(struct super_block *sb); 310 - extern void ntfs_free_big_inode(struct inode *inode); 311 - extern void ntfs_evict_big_inode(struct inode *vi); 312 - 313 - extern void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni); 263 + struct inode *ntfs_alloc_big_inode(struct super_block *sb); 264 + void ntfs_free_big_inode(struct inode *inode); 265 + int ntfs_drop_big_inode(struct inode *inode); 266 + void ntfs_evict_big_inode(struct inode *vi); 267 + void __ntfs_init_inode(struct super_block *sb, struct ntfs_inode *ni); 314 268 315 269 static inline void ntfs_init_big_inode(struct inode *vi) 316 270 { 317 - ntfs_inode *ni = NTFS_I(vi); 271 + struct ntfs_inode *ni = NTFS_I(vi); 318 272 319 273 ntfs_debug("Entering."); 320 274 __ntfs_init_inode(vi->i_sb, ni); 321 275 ni->mft_no = vi->i_ino; 322 276 } 323 277 324 - extern ntfs_inode *ntfs_new_extent_inode(struct super_block *sb, 278 + struct ntfs_inode *ntfs_new_extent_inode(struct super_block *sb, 325 279 unsigned long mft_no); 326 - extern void ntfs_clear_extent_inode(ntfs_inode *ni); 280 + void ntfs_clear_extent_inode(struct ntfs_inode *ni); 281 + int ntfs_read_inode_mount(struct inode *vi); 282 + int ntfs_show_options(struct seq_file *sf, struct dentry *root); 283 + int ntfs_truncate_vfs(struct inode *vi, loff_t new_size, loff_t i_size); 327 284 328 - extern int ntfs_read_inode_mount(struct inode *vi); 285 + int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 286 + struct iattr *attr); 287 + int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path, 288 + struct kstat *stat, unsigned int request_mask, 289 + unsigned int query_flags); 329 290 330 - extern int ntfs_show_options(struct seq_file *sf, struct dentry *root); 331 - 332 - #ifdef NTFS_RW 333 - 334 - extern int ntfs_truncate(struct inode *vi); 335 - extern void ntfs_truncate_vfs(struct inode *vi); 336 - 337 - extern int ntfs_setattr(struct mnt_idmap *idmap, 338 - struct dentry *dentry, struct iattr *attr); 339 - 340 - extern int __ntfs_write_inode(struct inode *vi, int sync); 291 + int ntfs_get_block_mft_record(struct ntfs_inode *mft_ni, struct ntfs_inode *ni); 292 + int __ntfs_write_inode(struct inode *vi, int sync); 293 + int ntfs_inode_attach_all_extents(struct ntfs_inode *ni); 294 + int ntfs_inode_add_attrlist(struct ntfs_inode *ni); 295 + void ntfs_destroy_ext_inode(struct ntfs_inode *ni); 296 + int ntfs_inode_free_space(struct ntfs_inode *ni, int size); 297 + s64 ntfs_inode_attr_pread(struct inode *vi, s64 pos, s64 count, u8 *buf); 298 + s64 ntfs_inode_attr_pwrite(struct inode *vi, s64 pos, s64 count, u8 *buf, 299 + bool sync); 300 + int ntfs_inode_close(struct ntfs_inode *ni); 341 301 342 302 static inline void ntfs_commit_inode(struct inode *vi) 343 303 { 344 - if (!is_bad_inode(vi)) 345 - __ntfs_write_inode(vi, 1); 346 - return; 304 + __ntfs_write_inode(vi, 1); 347 305 } 348 306 349 - #else 350 - 351 - static inline void ntfs_truncate_vfs(struct inode *vi) {} 352 - 353 - #endif /* NTFS_RW */ 307 + int ntfs_inode_sync_filename(struct ntfs_inode *ni); 308 + int ntfs_extend_initialized_size(struct inode *vi, const loff_t offset, 309 + const loff_t new_size, bool bsync); 310 + void ntfs_set_vfs_operations(struct inode *inode, mode_t mode, dev_t dev); 311 + struct folio *ntfs_get_locked_folio(struct address_space *mapping, 312 + pgoff_t index, pgoff_t end_index, struct file_ra_state *ra); 354 313 355 314 #endif /* _LINUX_NTFS_INODE_H */
+23
fs/ntfs/iomap.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * Copyright (c) 2025 LG Electronics Co., Ltd. 4 + */ 5 + 6 + #ifndef _LINUX_NTFS_IOMAP_H 7 + #define _LINUX_NTFS_IOMAP_H 8 + 9 + #include <linux/pagemap.h> 10 + #include <linux/iomap.h> 11 + 12 + #include "volume.h" 13 + #include "inode.h" 14 + 15 + extern const struct iomap_ops ntfs_write_iomap_ops; 16 + extern const struct iomap_ops ntfs_read_iomap_ops; 17 + extern const struct iomap_ops ntfs_seek_iomap_ops; 18 + extern const struct iomap_ops ntfs_page_mkwrite_iomap_ops; 19 + extern const struct iomap_ops ntfs_dio_iomap_ops; 20 + extern const struct iomap_writeback_ops ntfs_writeback_ops; 21 + extern const struct iomap_write_ops ntfs_iomap_folio_ops; 22 + extern int ntfs_dio_zero_range(struct inode *inode, loff_t offset, loff_t length); 23 + #endif /* _LINUX_NTFS_IOMAP_H */
+1418 -1493
fs/ntfs/layout.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * layout.h - All NTFS associated on-disk structures. Part of the Linux-NTFS 4 - * project. 3 + * All NTFS associated on-disk structures. 5 4 * 6 5 * Copyright (c) 2001-2005 Anton Altaparmakov 7 6 * Copyright (c) 2002 Richard Russon ··· 13 14 #include <linux/bitops.h> 14 15 #include <linux/list.h> 15 16 #include <asm/byteorder.h> 16 - 17 - #include "types.h" 18 17 19 18 /* The NTFS oem_id "NTFS " */ 20 19 #define magicNTFS cpu_to_le64(0x202020205346544eULL) ··· 30 33 31 34 /* 32 35 * BIOS parameter block (bpb) structure. 36 + * 37 + * @bytes_per_sector: Size of a sector in bytes (usually 512). 38 + * Matches the logical sector size of the underlying device. 39 + * @sectors_per_cluster: Size of a cluster in sectors (NTFS cluster size / sector size). 40 + * @reserved_sectors: Number of reserved sectors at the beginning of the volume. 41 + * Always set to 0 in NTFS. 42 + * @fats: Number of FAT tables. 43 + * Always 0 in NTFS (no FAT tables exist). 44 + * @root_entries: Number of entries in the root directory. 45 + * Always 0 in NTFS. 46 + * @sectors: Total number of sectors on the volume. 47 + * Always 0 in NTFS (use @large_sectors instead). 48 + * @media_type: Media descriptor byte. 49 + * 0xF8 for hard disk (fixed media) in NTFS. 50 + * @sectors_per_fat: Number of sectors per FAT table. 51 + * Always 0 in NTFS. 52 + * @sectors_per_track: Number of sectors per track. 53 + * Irrelevant for NTFS. 54 + * @heads: Number of heads (CHS geometry). 55 + * Irrelevant for NTFS. 56 + * @hidden_sectors: Number of hidden sectors before the start of the partition. 57 + * Always 0 in NTFS boot sector. 58 + * @large_sectors: Total number of sectors on the volume. 33 59 */ 34 - typedef struct { 35 - le16 bytes_per_sector; /* Size of a sector in bytes. */ 36 - u8 sectors_per_cluster; /* Size of a cluster in sectors. */ 37 - le16 reserved_sectors; /* zero */ 38 - u8 fats; /* zero */ 39 - le16 root_entries; /* zero */ 40 - le16 sectors; /* zero */ 41 - u8 media_type; /* 0xf8 = hard disk */ 42 - le16 sectors_per_fat; /* zero */ 43 - le16 sectors_per_track; /* irrelevant */ 44 - le16 heads; /* irrelevant */ 45 - le32 hidden_sectors; /* zero */ 46 - le32 large_sectors; /* zero */ 47 - } __attribute__ ((__packed__)) BIOS_PARAMETER_BLOCK; 60 + struct bios_parameter_block { 61 + __le16 bytes_per_sector; 62 + u8 sectors_per_cluster; 63 + __le16 reserved_sectors; 64 + u8 fats; 65 + __le16 root_entries; 66 + __le16 sectors; 67 + u8 media_type; 68 + __le16 sectors_per_fat; 69 + __le16 sectors_per_track; 70 + __le16 heads; 71 + __le32 hidden_sectors; 72 + __le32 large_sectors; 73 + } __packed; 48 74 49 75 /* 50 76 * NTFS boot sector structure. 77 + * 78 + * @jump: 3-byte jump instruction to boot code (irrelevant for NTFS). 79 + * Typically 0xEB 0x52 0x90 or similar. 80 + * @oem_id: OEM identifier string (8 bytes). 81 + * Always "NTFS " (with trailing spaces) in NTFS volumes. 82 + * @bpb: Legacy BIOS Parameter Block (see struct bios_parameter_block). 83 + * Mostly zeroed or set to fixed values for NTFS compatibility. 84 + * @unused: 4 bytes, reserved/unused. 85 + * NTFS disk editors show it as: 86 + * - physical_drive (0x80 for fixed disk) 87 + * - current_head (0) 88 + * - extended_boot_signature (0x80 or 0x28) 89 + * - unused (0) 90 + * Always zero in practice for NTFS. 91 + * @number_of_sectors: Number of sectors in volume. Gives maximum volume 92 + * size of 2^63 sectors. Assuming standard sector 93 + * size of 512 bytes, the maximum byte size is 94 + * approx. 4.7x10^21 bytes. (-; 95 + * @mft_lcn: Logical cluster number (LCN) of the $MFT data attribute. 96 + * Location of the Master File Table. 97 + * @mftmirr_lcn: LCN of the $MFTMirr (first 3-4 MFT records copy). 98 + * Mirror for boot-time recovery. 99 + * @clusters_per_mft_record: 100 + * Size of each MFT record in clusters. 101 + * @reserved0: 3 bytes, reserved/zero. 102 + * @clusters_per_index_record: 103 + * Size of each index block/record in clusters. 104 + * @reserved1: 3 bytes, reserved/zero. 105 + * @volume_serial_number: 106 + * 64-bit volume serial number. 107 + * Used for identification (irrelevant for NTFS operation). 108 + * @checksum: 32-bit checksum of the boot sector (excluding this field). 109 + * Used to detect boot sector corruption. 110 + * @bootstrap: 426 bytes of bootstrap code. 111 + * Irrelevant for NTFS (contains x86 boot loader stub). 112 + * @end_of_sector_marker: 113 + * 2-byte end-of-sector signature. 114 + * Always 0xAA55 (little-endian magic number). 51 115 */ 52 - typedef struct { 53 - u8 jump[3]; /* Irrelevant (jump to boot up code).*/ 54 - le64 oem_id; /* Magic "NTFS ". */ 55 - BIOS_PARAMETER_BLOCK bpb; /* See BIOS_PARAMETER_BLOCK. */ 56 - u8 unused[4]; /* zero, NTFS diskedit.exe states that 57 - this is actually: 58 - __u8 physical_drive; // 0x80 59 - __u8 current_head; // zero 60 - __u8 extended_boot_signature; 61 - // 0x80 62 - __u8 unused; // zero 63 - */ 64 - /*0x28*/sle64 number_of_sectors; /* Number of sectors in volume. Gives 65 - maximum volume size of 2^63 sectors. 66 - Assuming standard sector size of 512 67 - bytes, the maximum byte size is 68 - approx. 4.7x10^21 bytes. (-; */ 69 - sle64 mft_lcn; /* Cluster location of mft data. */ 70 - sle64 mftmirr_lcn; /* Cluster location of copy of mft. */ 71 - s8 clusters_per_mft_record; /* Mft record size in clusters. */ 72 - u8 reserved0[3]; /* zero */ 73 - s8 clusters_per_index_record; /* Index block size in clusters. */ 74 - u8 reserved1[3]; /* zero */ 75 - le64 volume_serial_number; /* Irrelevant (serial number). */ 76 - le32 checksum; /* Boot sector checksum. */ 77 - /*0x54*/u8 bootstrap[426]; /* Irrelevant (boot up code). */ 78 - le16 end_of_sector_marker; /* End of bootsector magic. Always is 79 - 0xaa55 in little endian. */ 80 - /* sizeof() = 512 (0x200) bytes */ 81 - } __attribute__ ((__packed__)) NTFS_BOOT_SECTOR; 116 + struct ntfs_boot_sector { 117 + u8 jump[3]; 118 + __le64 oem_id; 119 + struct bios_parameter_block bpb; 120 + u8 unused[4]; 121 + __le64 number_of_sectors; 122 + __le64 mft_lcn; 123 + __le64 mftmirr_lcn; 124 + s8 clusters_per_mft_record; 125 + u8 reserved0[3]; 126 + s8 clusters_per_index_record; 127 + u8 reserved1[3]; 128 + __le64 volume_serial_number; 129 + __le32 checksum; 130 + u8 bootstrap[426]; 131 + __le16 end_of_sector_marker; 132 + } __packed; 133 + 134 + static_assert(sizeof(struct ntfs_boot_sector) == 512); 82 135 83 136 /* 84 137 * Magic identifiers present at the beginning of all ntfs record containing 85 138 * records (like mft records for example). 139 + * 140 + * magic_FILE: MFT entry header ("FILE" in ASCII). 141 + * Used in $MFT/$DATA for all master file table records. 142 + * magic_INDX: Index buffer header ("INDX" in ASCII). 143 + * Used in $INDEX_ALLOCATION attributes (directories, $I30 indexes). 144 + * magic_HOLE: Hole marker ("HOLE" in ASCII). 145 + * Introduced in NTFS 3.0+, used for sparse/hole regions in some contexts. 146 + * magic_RSTR: Restart page header ("RSTR" in ASCII). 147 + * Used in LogFile for restart pages (transaction log recovery). 148 + * magic_RCRD: Log record page header ("RCRD" in ASCII). 149 + * Used in LogFile for individual log record pages. 150 + * magic_CHKD: Chkdsk modified marker ("CHKD" in ASCII). 151 + * Set by chkdsk when it modifies a record; indicates repair was done. 152 + * magic_BAAD: Bad record marker ("BAAD" in ASCII). 153 + * Indicates a multi-sector transfer failure was detected. 154 + * The record is corrupted/unusable; often set during I/O errors. 155 + * magic_empty: Empty/uninitialized page marker (0xffffffff). 156 + * Used in LogFile when a page is filled with 0xff bytes 157 + * and has not yet been initialized. Must be formatted before use. 86 158 */ 87 159 enum { 88 - /* Found in $MFT/$DATA. */ 89 - magic_FILE = cpu_to_le32(0x454c4946), /* Mft entry. */ 90 - magic_INDX = cpu_to_le32(0x58444e49), /* Index buffer. */ 91 - magic_HOLE = cpu_to_le32(0x454c4f48), /* ? (NTFS 3.0+?) */ 92 - 93 - /* Found in $LogFile/$DATA. */ 94 - magic_RSTR = cpu_to_le32(0x52545352), /* Restart page. */ 95 - magic_RCRD = cpu_to_le32(0x44524352), /* Log record page. */ 96 - 97 - /* Found in $LogFile/$DATA. (May be found in $MFT/$DATA, also?) */ 98 - magic_CHKD = cpu_to_le32(0x444b4843), /* Modified by chkdsk. */ 99 - 100 - /* Found in all ntfs record containing records. */ 101 - magic_BAAD = cpu_to_le32(0x44414142), /* Failed multi sector 102 - transfer was detected. */ 103 - /* 104 - * Found in $LogFile/$DATA when a page is full of 0xff bytes and is 105 - * thus not initialized. Page must be initialized before using it. 106 - */ 107 - magic_empty = cpu_to_le32(0xffffffff) /* Record is empty. */ 160 + magic_FILE = cpu_to_le32(0x454c4946), 161 + magic_INDX = cpu_to_le32(0x58444e49), 162 + magic_HOLE = cpu_to_le32(0x454c4f48), 163 + magic_RSTR = cpu_to_le32(0x52545352), 164 + magic_RCRD = cpu_to_le32(0x44524352), 165 + magic_CHKD = cpu_to_le32(0x444b4843), 166 + magic_BAAD = cpu_to_le32(0x44414142), 167 + magic_empty = cpu_to_le32(0xffffffff) 108 168 }; 109 - 110 - typedef le32 NTFS_RECORD_TYPE; 111 169 112 170 /* 113 171 * Generic magic comparison macros. Finally found a use for the ## preprocessor 114 172 * operator! (-8 115 173 */ 116 174 117 - static inline bool __ntfs_is_magic(le32 x, NTFS_RECORD_TYPE r) 175 + static inline bool __ntfs_is_magic(__le32 x, __le32 r) 118 176 { 119 177 return (x == r); 120 178 } 121 179 #define ntfs_is_magic(x, m) __ntfs_is_magic(x, magic_##m) 122 180 123 - static inline bool __ntfs_is_magicp(le32 *p, NTFS_RECORD_TYPE r) 181 + static inline bool __ntfs_is_magicp(__le32 *p, __le32 r) 124 182 { 125 183 return (*p == r); 126 184 } ··· 184 132 /* 185 133 * Specialised magic comparison macros for the NTFS_RECORD_TYPEs defined above. 186 134 */ 187 - #define ntfs_is_file_record(x) ( ntfs_is_magic (x, FILE) ) 188 - #define ntfs_is_file_recordp(p) ( ntfs_is_magicp(p, FILE) ) 189 - #define ntfs_is_mft_record(x) ( ntfs_is_file_record (x) ) 190 - #define ntfs_is_mft_recordp(p) ( ntfs_is_file_recordp(p) ) 191 - #define ntfs_is_indx_record(x) ( ntfs_is_magic (x, INDX) ) 192 - #define ntfs_is_indx_recordp(p) ( ntfs_is_magicp(p, INDX) ) 193 - #define ntfs_is_hole_record(x) ( ntfs_is_magic (x, HOLE) ) 194 - #define ntfs_is_hole_recordp(p) ( ntfs_is_magicp(p, HOLE) ) 135 + #define ntfs_is_file_record(x) (ntfs_is_magic(x, FILE)) 136 + #define ntfs_is_file_recordp(p) (ntfs_is_magicp(p, FILE)) 137 + #define ntfs_is_mft_record(x) (ntfs_is_file_record(x)) 138 + #define ntfs_is_mft_recordp(p) (ntfs_is_file_recordp(p)) 139 + #define ntfs_is_indx_record(x) (ntfs_is_magic(x, INDX)) 140 + #define ntfs_is_indx_recordp(p) (ntfs_is_magicp(p, INDX)) 141 + #define ntfs_is_hole_record(x) (ntfs_is_magic(x, HOLE)) 142 + #define ntfs_is_hole_recordp(p) (ntfs_is_magicp(p, HOLE)) 195 143 196 - #define ntfs_is_rstr_record(x) ( ntfs_is_magic (x, RSTR) ) 197 - #define ntfs_is_rstr_recordp(p) ( ntfs_is_magicp(p, RSTR) ) 198 - #define ntfs_is_rcrd_record(x) ( ntfs_is_magic (x, RCRD) ) 199 - #define ntfs_is_rcrd_recordp(p) ( ntfs_is_magicp(p, RCRD) ) 144 + #define ntfs_is_rstr_record(x) (ntfs_is_magic(x, RSTR)) 145 + #define ntfs_is_rstr_recordp(p) (ntfs_is_magicp(p, RSTR)) 146 + #define ntfs_is_rcrd_record(x) (ntfs_is_magic(x, RCRD)) 147 + #define ntfs_is_rcrd_recordp(p) (ntfs_is_magicp(p, RCRD)) 200 148 201 - #define ntfs_is_chkd_record(x) ( ntfs_is_magic (x, CHKD) ) 202 - #define ntfs_is_chkd_recordp(p) ( ntfs_is_magicp(p, CHKD) ) 149 + #define ntfs_is_chkd_record(x) (ntfs_is_magic(x, CHKD)) 150 + #define ntfs_is_chkd_recordp(p) (ntfs_is_magicp(p, CHKD)) 203 151 204 - #define ntfs_is_baad_record(x) ( ntfs_is_magic (x, BAAD) ) 205 - #define ntfs_is_baad_recordp(p) ( ntfs_is_magicp(p, BAAD) ) 152 + #define ntfs_is_baad_record(x) (ntfs_is_magic(x, BAAD)) 153 + #define ntfs_is_baad_recordp(p) (ntfs_is_magicp(p, BAAD)) 206 154 207 - #define ntfs_is_empty_record(x) ( ntfs_is_magic (x, empty) ) 208 - #define ntfs_is_empty_recordp(p) ( ntfs_is_magicp(p, empty) ) 155 + #define ntfs_is_empty_record(x) (ntfs_is_magic(x, empty)) 156 + #define ntfs_is_empty_recordp(p) (ntfs_is_magicp(p, empty)) 209 157 210 158 /* 211 - * The Update Sequence Array (usa) is an array of the le16 values which belong 159 + * struct ntfs_record - Common header for all multi-sector protected NTFS records 160 + * 161 + * @magic: 4-byte magic identifier for the record type and/or status. 162 + * Common values are defined in the magic_* enum (FILE, INDX, RSTR, 163 + * RCRD, CHKD, BAAD, HOLE, empty). 164 + * - "FILE" = MFT record 165 + * - "INDX" = Index allocation block 166 + * - "BAAD" = Record corrupted (multi-sector fixup failed) 167 + * - 0xffffffff = Uninitialized/empty page 168 + * @usa_ofs: Offset (in bytes) from the start of this record to the Update 169 + * Sequence Array (USA). 170 + * The USA is located at record + usa_ofs. 171 + * @usa_count: Number of 16-bit entries in the USA array (including the Update 172 + * Sequence Number itself). 173 + * - Number of fixup locations = usa_count - 1 174 + * - Each fixup location is a 16-bit value in the record that needs 175 + * protection against torn writes. 176 + * 177 + * The Update Sequence Array (usa) is an array of the __le16 values which belong 212 178 * to the end of each sector protected by the update sequence record in which 213 179 * this array is contained. Note that the first entry is the Update Sequence 214 180 * Number (usn), a cyclic counter of how many times the protected record has ··· 236 166 * transfer has occurred when the data was written. 237 167 * The maximum size for the update sequence array is fixed to: 238 168 * maximum size = usa_ofs + (usa_count * 2) = 510 bytes 239 - * The 510 bytes comes from the fact that the last le16 in the array has to 240 - * (obviously) finish before the last le16 of the first 512-byte sector. 169 + * The 510 bytes comes from the fact that the last __le16 in the array has to 170 + * (obviously) finish before the last __le16 of the first 512-byte sector. 241 171 * This formula can be used as a consistency check in that usa_ofs + 242 172 * (usa_count * 2) has to be less than or equal to 510. 243 173 */ 244 - typedef struct { 245 - NTFS_RECORD_TYPE magic; /* A four-byte magic identifying the record 246 - type and/or status. */ 247 - le16 usa_ofs; /* Offset to the Update Sequence Array (usa) 248 - from the start of the ntfs record. */ 249 - le16 usa_count; /* Number of le16 sized entries in the usa 250 - including the Update Sequence Number (usn), 251 - thus the number of fixups is the usa_count 252 - minus 1. */ 253 - } __attribute__ ((__packed__)) NTFS_RECORD; 174 + struct ntfs_record { 175 + __le32 magic; 176 + __le16 usa_ofs; 177 + __le16 usa_count; 178 + } __packed; 254 179 255 180 /* 256 181 * System files mft record numbers. All these files are always marked as used ··· 253 188 * allocation for random other mft records. Also, the sequence number for each 254 189 * of the system files is always equal to their mft record number and it is 255 190 * never modified. 191 + * 192 + * FILE_MFT: Master File Table (MFT) itself. 193 + * Data attribute contains all MFT entries; 194 + * Bitmap attribute tracks which records are in use (bit==1). 195 + * FILE_MFTMirr: MFT mirror: copy of the first four (or more) MFT records 196 + * in its data attribute. 197 + * If cluster size > 4 KiB, copies first N records where 198 + * N = cluster_size / mft_record_size. 199 + * FILE_LogFile: Journaling log (LogFile) in data attribute. 200 + * Used for transaction logging and recovery. 201 + * FILE_Volume: Volume information and name. 202 + * Contains $VolumeName (label) and $VolumeInformation 203 + * (flags, NTFS version). Windows calls this the volume DASD. 204 + * FILE_AttrDef: Attribute definitions array in data attribute. 205 + * Defines all possible attribute types and their properties. 206 + * FILE_root: Root directory ($Root). 207 + * The top-level directory of the filesystem. 208 + * FILE_Bitmap: Cluster allocation bitmap ($Bitmap) in data attribute. 209 + * Tracks free/used clusters (LCNs) on the volume. 210 + * FILE_Boot: Boot sector ($Boot) in data attribute. 211 + * Always located at cluster 0; contains BPB and NTFS parameters. 212 + * FILE_BadClus: Bad cluster list ($BadClus) in non-resident data attribute. 213 + * Marks all known bad clusters. 214 + * FILE_Secure: Security descriptors ($Secure). 215 + * Contains shared $SDS (security descriptors) and two indexes 216 + * ($SDH, $SII). Introduced in Windows 2000. 217 + * Before that, it was called $Quota but was unused. 218 + * FILE_UpCase: Uppercase table ($UpCase) in data attribute. 219 + * Maps all 65536 Unicode characters to their uppercase forms. 220 + * FILE_Extend: System directory ($Extend). 221 + * Contains additional system files ($ObjId, $Quota, $Reparse, 222 + * $UsnJrnl, etc.). Introduced in NTFS 3.0 (Windows 2000). 223 + * FILE_reserved12: Reserved for future use (MFT records 12–15). 224 + * FILE_reserved13: Reserved. 225 + * FILE_reserved14: Reserved. 226 + * FILE_reserved15: Reserved. 227 + * FILE_first_user: First possible user-created file MFT record number. 228 + * Used as a boundary to distinguish system files from user files. 256 229 */ 257 - typedef enum { 258 - FILE_MFT = 0, /* Master file table (mft). Data attribute 259 - contains the entries and bitmap attribute 260 - records which ones are in use (bit==1). */ 261 - FILE_MFTMirr = 1, /* Mft mirror: copy of first four mft records 262 - in data attribute. If cluster size > 4kiB, 263 - copy of first N mft records, with 264 - N = cluster_size / mft_record_size. */ 265 - FILE_LogFile = 2, /* Journalling log in data attribute. */ 266 - FILE_Volume = 3, /* Volume name attribute and volume information 267 - attribute (flags and ntfs version). Windows 268 - refers to this file as volume DASD (Direct 269 - Access Storage Device). */ 270 - FILE_AttrDef = 4, /* Array of attribute definitions in data 271 - attribute. */ 272 - FILE_root = 5, /* Root directory. */ 273 - FILE_Bitmap = 6, /* Allocation bitmap of all clusters (lcns) in 274 - data attribute. */ 275 - FILE_Boot = 7, /* Boot sector (always at cluster 0) in data 276 - attribute. */ 277 - FILE_BadClus = 8, /* Contains all bad clusters in the non-resident 278 - data attribute. */ 279 - FILE_Secure = 9, /* Shared security descriptors in data attribute 280 - and two indexes into the descriptors. 281 - Appeared in Windows 2000. Before that, this 282 - file was named $Quota but was unused. */ 283 - FILE_UpCase = 10, /* Uppercase equivalents of all 65536 Unicode 284 - characters in data attribute. */ 285 - FILE_Extend = 11, /* Directory containing other system files (eg. 286 - $ObjId, $Quota, $Reparse and $UsnJrnl). This 287 - is new to NTFS3.0. */ 288 - FILE_reserved12 = 12, /* Reserved for future use (records 12-15). */ 230 + enum { 231 + FILE_MFT = 0, 232 + FILE_MFTMirr = 1, 233 + FILE_LogFile = 2, 234 + FILE_Volume = 3, 235 + FILE_AttrDef = 4, 236 + FILE_root = 5, 237 + FILE_Bitmap = 6, 238 + FILE_Boot = 7, 239 + FILE_BadClus = 8, 240 + FILE_Secure = 9, 241 + FILE_UpCase = 10, 242 + FILE_Extend = 11, 243 + FILE_reserved12 = 12, 289 244 FILE_reserved13 = 13, 290 245 FILE_reserved14 = 14, 291 246 FILE_reserved15 = 15, 292 - FILE_first_user = 16, /* First user file, used as test limit for 293 - whether to allow opening a file or not. */ 294 - } NTFS_SYSTEM_FILES; 247 + FILE_first_user = 16, 248 + }; 295 249 296 250 /* 251 + * enum - Flags for MFT record header 252 + * 297 253 * These are the so far known MFT_RECORD_* flags (16-bit) which contain 298 254 * information about the mft record in which they are present. 255 + * 256 + * MFT_RECORD_IN_USE: This MFT record is allocated and in use. 257 + * (bit set = record is valid/used; clear = free) 258 + * MFT_RECORD_IS_DIRECTORY: This MFT record represents a directory. 259 + * (Used to quickly distinguish files from directories) 260 + * MFT_RECORD_IS_4: Indicates the record is a special "record 4" type. 261 + * (Rarely used; related to NTFS internal special cases, 262 + * often for $AttrDef or early system files) 263 + * MFT_RECORD_IS_VIEW_INDEX: This MFT record is used as a view index. 264 + * (Specific to NTFS indexed views or object ID indexes) 265 + * MFT_REC_SPACE_FILLER: Dummy value to force the enum to be 16-bit wide. 266 + * (Not a real flag; just a sentinel to ensure the type 267 + * is __le16 and no higher bits are accidentally used) 299 268 */ 300 269 enum { 301 - MFT_RECORD_IN_USE = cpu_to_le16(0x0001), 302 - MFT_RECORD_IS_DIRECTORY = cpu_to_le16(0x0002), 303 - } __attribute__ ((__packed__)); 304 - 305 - typedef le16 MFT_RECORD_FLAGS; 270 + MFT_RECORD_IN_USE = cpu_to_le16(0x0001), 271 + MFT_RECORD_IS_DIRECTORY = cpu_to_le16(0x0002), 272 + MFT_RECORD_IS_4 = cpu_to_le16(0x0004), 273 + MFT_RECORD_IS_VIEW_INDEX = cpu_to_le16(0x0008), 274 + MFT_REC_SPACE_FILLER = cpu_to_le16(0xffff), /*Just to make flags 16-bit.*/ 275 + } __packed; 306 276 307 277 /* 308 278 * mft references (aka file references or file record segment references) are ··· 351 251 * The sequence number is a circular counter (skipping 0) describing how many 352 252 * times the referenced mft record has been (re)used. This has to match the 353 253 * sequence number of the mft record being referenced, otherwise the reference 354 - * is considered stale and removed (FIXME: only ntfsck or the driver itself?). 254 + * is considered stale and removed. 355 255 * 356 256 * If the sequence number is zero it is assumed that no sequence number 357 257 * consistency checking should be performed. 358 - * 359 - * FIXME: Since inodes are 32-bit as of now, the driver needs to always check 360 - * for high_part being 0 and if not either BUG(), cause a panic() or handle 361 - * the situation in some other way. This shouldn't be a problem as a volume has 362 - * to become HUGE in order to need more than 32-bits worth of mft records. 363 - * Assuming the standard mft record size of 1kb only the records (never mind 364 - * the non-resident attributes, etc.) would require 4Tb of space on their own 365 - * for the first 32 bits worth of records. This is only if some strange person 366 - * doesn't decide to foul play and make the mft sparse which would be a really 367 - * horrible thing to do as it would trash our current driver implementation. )-: 368 - * Do I hear screams "we want 64-bit inodes!" ?!? (-; 369 - * 370 - * FIXME: The mft zone is defined as the first 12% of the volume. This space is 371 - * reserved so that the mft can grow contiguously and hence doesn't become 372 - * fragmented. Volume free space includes the empty part of the mft zone and 373 - * when the volume's free 88% are used up, the mft zone is shrunk by a factor 374 - * of 2, thus making more space available for more files/data. This process is 375 - * repeated every time there is no more free space except for the mft zone until 376 - * there really is no more free space. 377 258 */ 378 259 379 260 /* 380 - * Typedef the MFT_REF as a 64-bit value for easier handling. 381 - * Also define two unpacking macros to get to the reference (MREF) and 261 + * Define two unpacking macros to get to the reference (MREF) and 382 262 * sequence number (MSEQNO) respectively. 383 263 * The _LE versions are to be applied on little endian MFT_REFs. 384 264 * Note: The _LE versions will return a CPU endian formatted value! ··· 366 286 #define MFT_REF_MASK_CPU 0x0000ffffffffffffULL 367 287 #define MFT_REF_MASK_LE cpu_to_le64(MFT_REF_MASK_CPU) 368 288 369 - typedef u64 MFT_REF; 370 - typedef le64 leMFT_REF; 371 - 372 - #define MK_MREF(m, s) ((MFT_REF)(((MFT_REF)(s) << 48) | \ 373 - ((MFT_REF)(m) & MFT_REF_MASK_CPU))) 289 + #define MK_MREF(m, s) ((u64)(((u64)(s) << 48) | \ 290 + ((u64)(m) & MFT_REF_MASK_CPU))) 374 291 #define MK_LE_MREF(m, s) cpu_to_le64(MK_MREF(m, s)) 375 292 376 293 #define MREF(x) ((unsigned long)((x) & MFT_REF_MASK_CPU)) 377 294 #define MSEQNO(x) ((u16)(((x) >> 48) & 0xffff)) 378 295 #define MREF_LE(x) ((unsigned long)(le64_to_cpu(x) & MFT_REF_MASK_CPU)) 296 + #define MREF_INO(x) ((unsigned long)MREF_LE(x)) 379 297 #define MSEQNO_LE(x) ((u16)((le64_to_cpu(x) >> 48) & 0xffff)) 380 298 381 299 #define IS_ERR_MREF(x) (((x) & 0x0000800000000000ULL) ? true : false) ··· 381 303 #define MREF_ERR(x) ((int)((s64)(x))) 382 304 383 305 /* 306 + * struct mft_record - NTFS Master File Table (MFT) record header 307 + * 384 308 * The mft record header present at the beginning of every record in the mft. 385 309 * This is followed by a sequence of variable length attribute records which 386 310 * is terminated by an attribute of type AT_END which is a truncated attribute 387 311 * in that it only consists of the attribute type code AT_END and none of the 388 312 * other members of the attribute structure are present. 313 + * 314 + * magic: Record magic ("FILE" for valid MFT entries). 315 + * See ntfs_record magic enum for other values. 316 + * usa_ofs: Offset to Update Sequence Array (see ntfs_record). 317 + * usa_count: Number of entries in USA (see ntfs_record). 318 + * lsn: Log sequence number (LSN) from LogFile. 319 + * Incremented on every modification to this record. 320 + * sequence_number: Reuse count of this MFT record slot. 321 + * Incremented (skipping zero) when the file is deleted. 322 + * Zero means never reused or special case. 323 + * Part of MFT reference (together with record number). 324 + * link_count: Number of hard links (directory entries) to this file. 325 + * Only meaningful in base MFT records. 326 + * When deleting a directory entry: 327 + * - If link_count == 1, delete the whole file 328 + * - Else remove only the $FILE_NAME attribute and decrement 329 + * attrs_offset: Byte offset from start of MFT record to first attribute. 330 + * Must be 8-byte aligned. 331 + * flags: Bit array of MFT_RECORD_* flags (see MFT_RECORD_IN_USE enum). 332 + * MFT_RECORD_IN_USE cleared when record is freed/deleted. 333 + * bytes_in_use: Number of bytes actually used in this MFT record. 334 + * Must be 8-byte aligned. 335 + * Includes header + all attributes + padding. 336 + * bytes_allocated: Total allocated size of this MFT record. 337 + * Usually equal to MFT record size (1024 bytes or cluster size). 338 + * base_mft_record: MFT reference to the base record. 339 + * 0 for base records. 340 + * Non-zero for extension records → points to base record 341 + * containing the $ATTRIBUTE_LIST that describes this extension. 342 + * next_attr_instance: Next attribute instance number to assign. 343 + * Incremented after each use. 344 + * Reset to 0 when MFT record is reused. 345 + * First instance is always 0. 346 + * reserved: Reserved for alignment (NTFS 3.1+). 347 + * mft_record_number: This MFT record's number (index in $MFT). 348 + * Only present in NTFS 3.1+ (Windows XP and above). 389 349 */ 390 - typedef struct { 391 - /*Ofs*/ 392 - /* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */ 393 - NTFS_RECORD_TYPE magic; /* Usually the magic is "FILE". */ 394 - le16 usa_ofs; /* See NTFS_RECORD definition above. */ 395 - le16 usa_count; /* See NTFS_RECORD definition above. */ 350 + struct mft_record { 351 + __le32 magic; 352 + __le16 usa_ofs; 353 + __le16 usa_count; 396 354 397 - /* 8*/ le64 lsn; /* $LogFile sequence number for this record. 398 - Changed every time the record is modified. */ 399 - /* 16*/ le16 sequence_number; /* Number of times this mft record has been 400 - reused. (See description for MFT_REF 401 - above.) NOTE: The increment (skipping zero) 402 - is done when the file is deleted. NOTE: If 403 - this is zero it is left zero. */ 404 - /* 18*/ le16 link_count; /* Number of hard links, i.e. the number of 405 - directory entries referencing this record. 406 - NOTE: Only used in mft base records. 407 - NOTE: When deleting a directory entry we 408 - check the link_count and if it is 1 we 409 - delete the file. Otherwise we delete the 410 - FILE_NAME_ATTR being referenced by the 411 - directory entry from the mft record and 412 - decrement the link_count. 413 - FIXME: Careful with Win32 + DOS names! */ 414 - /* 20*/ le16 attrs_offset; /* Byte offset to the first attribute in this 415 - mft record from the start of the mft record. 416 - NOTE: Must be aligned to 8-byte boundary. */ 417 - /* 22*/ MFT_RECORD_FLAGS flags; /* Bit array of MFT_RECORD_FLAGS. When a file 418 - is deleted, the MFT_RECORD_IN_USE flag is 419 - set to zero. */ 420 - /* 24*/ le32 bytes_in_use; /* Number of bytes used in this mft record. 421 - NOTE: Must be aligned to 8-byte boundary. */ 422 - /* 28*/ le32 bytes_allocated; /* Number of bytes allocated for this mft 423 - record. This should be equal to the mft 424 - record size. */ 425 - /* 32*/ leMFT_REF base_mft_record;/* This is zero for base mft records. 426 - When it is not zero it is a mft reference 427 - pointing to the base mft record to which 428 - this record belongs (this is then used to 429 - locate the attribute list attribute present 430 - in the base record which describes this 431 - extension record and hence might need 432 - modification when the extension record 433 - itself is modified, also locating the 434 - attribute list also means finding the other 435 - potential extents, belonging to the non-base 436 - mft record). */ 437 - /* 40*/ le16 next_attr_instance;/* The instance number that will be assigned to 438 - the next attribute added to this mft record. 439 - NOTE: Incremented each time after it is used. 440 - NOTE: Every time the mft record is reused 441 - this number is set to zero. NOTE: The first 442 - instance number is always 0. */ 443 - /* The below fields are specific to NTFS 3.1+ (Windows XP and above): */ 444 - /* 42*/ le16 reserved; /* Reserved/alignment. */ 445 - /* 44*/ le32 mft_record_number; /* Number of this mft record. */ 446 - /* sizeof() = 48 bytes */ 447 - /* 448 - * When (re)using the mft record, we place the update sequence array at this 449 - * offset, i.e. before we start with the attributes. This also makes sense, 450 - * otherwise we could run into problems with the update sequence array 451 - * containing in itself the last two bytes of a sector which would mean that 452 - * multi sector transfer protection wouldn't work. As you can't protect data 453 - * by overwriting it since you then can't get it back... 454 - * When reading we obviously use the data from the ntfs record header. 355 + __le64 lsn; 356 + __le16 sequence_number; 357 + __le16 link_count; 358 + __le16 attrs_offset; 359 + __le16 flags; 360 + __le32 bytes_in_use; 361 + __le32 bytes_allocated; 362 + __le64 base_mft_record; 363 + __le16 next_attr_instance; 364 + __le16 reserved; 365 + __le32 mft_record_number; 366 + } __packed; 367 + 368 + static_assert(sizeof(struct mft_record) == 48); 369 + 370 + /**x 371 + * struct mft_record_old - Old NTFS MFT record header (pre-NTFS 3.1 / Windows XP) 372 + * 373 + * This is the older version of the MFT record header used in NTFS versions 374 + * prior to 3.1 (Windows XP and later). It lacks the additional fields 375 + * @reserved and @mft_record_number that were added in NTFS 3.1+. 376 + * 377 + * @magic: Record magic ("FILE" for valid MFT entries). 378 + * See ntfs_record magic enum for other values. 379 + * @usa_ofs: Offset to Update Sequence Array (see ntfs_record). 380 + * @usa_count: Number of entries in USA (see ntfs_record). 381 + * @lsn: Log sequence number (LSN) from LogFile. 382 + * Incremented on every modification to this record. 383 + * @sequence_number: Reuse count of this MFT record slot. 384 + * Incremented (skipping zero) when the file is deleted. 385 + * Zero means never reused or special case. 386 + * Part of MFT reference (together with record number). 387 + * @link_count: Number of hard links (directory entries) to this file. 388 + * Only meaningful in base MFT records. 389 + * When deleting a directory entry: 390 + * - If link_count == 1, delete the whole file 391 + * - Else remove only the $FILE_NAME attribute and decrement 392 + * @attrs_offset: Byte offset from start of MFT record to first attribute. 393 + * Must be 8-byte aligned. 394 + * @flags: Bit array of MFT_RECORD_* flags (see MFT_RECORD_IN_USE enum). 395 + * MFT_RECORD_IN_USE cleared when record is freed/deleted. 396 + * @bytes_in_use: Number of bytes actually used in this MFT record. 397 + * Must be 8-byte aligned. 398 + * Includes header + all attributes + padding. 399 + * @bytes_allocated: Total allocated size of this MFT record. 400 + * Usually equal to MFT record size (1024 bytes or cluster size). 401 + * @base_mft_record: MFT reference to the base record. 402 + * 0 for base records. 403 + * Non-zero for extension records → points to base record 404 + * containing the $ATTRIBUTE_LIST that describes this extension. 405 + * @next_attr_instance: Next attribute instance number to assign. 406 + * Incremented after each use. 407 + * Reset to 0 when MFT record is reused. 408 + * First instance is always 0. 455 409 */ 456 - } __attribute__ ((__packed__)) MFT_RECORD; 410 + struct mft_record_old { 411 + __le32 magic; 412 + __le16 usa_ofs; 413 + __le16 usa_count; 457 414 458 - /* This is the version without the NTFS 3.1+ specific fields. */ 459 - typedef struct { 460 - /*Ofs*/ 461 - /* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */ 462 - NTFS_RECORD_TYPE magic; /* Usually the magic is "FILE". */ 463 - le16 usa_ofs; /* See NTFS_RECORD definition above. */ 464 - le16 usa_count; /* See NTFS_RECORD definition above. */ 415 + __le64 lsn; 416 + __le16 sequence_number; 417 + __le16 link_count; 418 + __le16 attrs_offset; 419 + __le16 flags; 420 + __le32 bytes_in_use; 421 + __le32 bytes_allocated; 422 + __le64 base_mft_record; 423 + __le16 next_attr_instance; 424 + } __packed; 465 425 466 - /* 8*/ le64 lsn; /* $LogFile sequence number for this record. 467 - Changed every time the record is modified. */ 468 - /* 16*/ le16 sequence_number; /* Number of times this mft record has been 469 - reused. (See description for MFT_REF 470 - above.) NOTE: The increment (skipping zero) 471 - is done when the file is deleted. NOTE: If 472 - this is zero it is left zero. */ 473 - /* 18*/ le16 link_count; /* Number of hard links, i.e. the number of 474 - directory entries referencing this record. 475 - NOTE: Only used in mft base records. 476 - NOTE: When deleting a directory entry we 477 - check the link_count and if it is 1 we 478 - delete the file. Otherwise we delete the 479 - FILE_NAME_ATTR being referenced by the 480 - directory entry from the mft record and 481 - decrement the link_count. 482 - FIXME: Careful with Win32 + DOS names! */ 483 - /* 20*/ le16 attrs_offset; /* Byte offset to the first attribute in this 484 - mft record from the start of the mft record. 485 - NOTE: Must be aligned to 8-byte boundary. */ 486 - /* 22*/ MFT_RECORD_FLAGS flags; /* Bit array of MFT_RECORD_FLAGS. When a file 487 - is deleted, the MFT_RECORD_IN_USE flag is 488 - set to zero. */ 489 - /* 24*/ le32 bytes_in_use; /* Number of bytes used in this mft record. 490 - NOTE: Must be aligned to 8-byte boundary. */ 491 - /* 28*/ le32 bytes_allocated; /* Number of bytes allocated for this mft 492 - record. This should be equal to the mft 493 - record size. */ 494 - /* 32*/ leMFT_REF base_mft_record;/* This is zero for base mft records. 495 - When it is not zero it is a mft reference 496 - pointing to the base mft record to which 497 - this record belongs (this is then used to 498 - locate the attribute list attribute present 499 - in the base record which describes this 500 - extension record and hence might need 501 - modification when the extension record 502 - itself is modified, also locating the 503 - attribute list also means finding the other 504 - potential extents, belonging to the non-base 505 - mft record). */ 506 - /* 40*/ le16 next_attr_instance;/* The instance number that will be assigned to 507 - the next attribute added to this mft record. 508 - NOTE: Incremented each time after it is used. 509 - NOTE: Every time the mft record is reused 510 - this number is set to zero. NOTE: The first 511 - instance number is always 0. */ 512 - /* sizeof() = 42 bytes */ 513 - /* 514 - * When (re)using the mft record, we place the update sequence array at this 515 - * offset, i.e. before we start with the attributes. This also makes sense, 516 - * otherwise we could run into problems with the update sequence array 517 - * containing in itself the last two bytes of a sector which would mean that 518 - * multi sector transfer protection wouldn't work. As you can't protect data 519 - * by overwriting it since you then can't get it back... 520 - * When reading we obviously use the data from the ntfs record header. 521 - */ 522 - } __attribute__ ((__packed__)) MFT_RECORD_OLD; 426 + static_assert(sizeof(struct mft_record_old) == 42); 523 427 524 428 /* 525 429 * System defined attributes (32-bit). Each attribute type has a corresponding ··· 512 452 * a revealing choice of symbol I do not know what is... (-; 513 453 */ 514 454 enum { 515 - AT_UNUSED = cpu_to_le32( 0), 516 - AT_STANDARD_INFORMATION = cpu_to_le32( 0x10), 517 - AT_ATTRIBUTE_LIST = cpu_to_le32( 0x20), 518 - AT_FILE_NAME = cpu_to_le32( 0x30), 519 - AT_OBJECT_ID = cpu_to_le32( 0x40), 520 - AT_SECURITY_DESCRIPTOR = cpu_to_le32( 0x50), 521 - AT_VOLUME_NAME = cpu_to_le32( 0x60), 522 - AT_VOLUME_INFORMATION = cpu_to_le32( 0x70), 523 - AT_DATA = cpu_to_le32( 0x80), 524 - AT_INDEX_ROOT = cpu_to_le32( 0x90), 525 - AT_INDEX_ALLOCATION = cpu_to_le32( 0xa0), 526 - AT_BITMAP = cpu_to_le32( 0xb0), 527 - AT_REPARSE_POINT = cpu_to_le32( 0xc0), 528 - AT_EA_INFORMATION = cpu_to_le32( 0xd0), 529 - AT_EA = cpu_to_le32( 0xe0), 530 - AT_PROPERTY_SET = cpu_to_le32( 0xf0), 531 - AT_LOGGED_UTILITY_STREAM = cpu_to_le32( 0x100), 532 - AT_FIRST_USER_DEFINED_ATTRIBUTE = cpu_to_le32( 0x1000), 455 + AT_UNUSED = cpu_to_le32(0), 456 + AT_STANDARD_INFORMATION = cpu_to_le32(0x10), 457 + AT_ATTRIBUTE_LIST = cpu_to_le32(0x20), 458 + AT_FILE_NAME = cpu_to_le32(0x30), 459 + AT_OBJECT_ID = cpu_to_le32(0x40), 460 + AT_SECURITY_DESCRIPTOR = cpu_to_le32(0x50), 461 + AT_VOLUME_NAME = cpu_to_le32(0x60), 462 + AT_VOLUME_INFORMATION = cpu_to_le32(0x70), 463 + AT_DATA = cpu_to_le32(0x80), 464 + AT_INDEX_ROOT = cpu_to_le32(0x90), 465 + AT_INDEX_ALLOCATION = cpu_to_le32(0xa0), 466 + AT_BITMAP = cpu_to_le32(0xb0), 467 + AT_REPARSE_POINT = cpu_to_le32(0xc0), 468 + AT_EA_INFORMATION = cpu_to_le32(0xd0), 469 + AT_EA = cpu_to_le32(0xe0), 470 + AT_PROPERTY_SET = cpu_to_le32(0xf0), 471 + AT_LOGGED_UTILITY_STREAM = cpu_to_le32(0x100), 472 + AT_FIRST_USER_DEFINED_ATTRIBUTE = cpu_to_le32(0x1000), 533 473 AT_END = cpu_to_le32(0xffffffff) 534 474 }; 535 - 536 - typedef le32 ATTR_TYPE; 537 475 538 476 /* 539 477 * The collation rules for sorting views/indexes/etc (32-bit). ··· 548 490 * unistr.c::ntfs_collate_names() and unistr.c::legal_ansi_char_array[] 549 491 * for what I mean but COLLATION_UNICODE_STRING would not give any special 550 492 * treatment to any characters at all, but this is speculation. 551 - * COLLATION_NTOFS_ULONG - Sorting is done according to ascending le32 key 493 + * COLLATION_NTOFS_ULONG - Sorting is done according to ascending __le32 key 552 494 * values. E.g. used for $SII index in FILE_Secure, which sorts by 553 495 * security_id (le32). 554 496 * COLLATION_NTOFS_SID - Sorting is done according to ascending SID values. ··· 557 499 * values and second by ascending security_id values. E.g. used for $SDH 558 500 * index in FILE_Secure. 559 501 * COLLATION_NTOFS_ULONGS - Sorting is done according to a sequence of ascending 560 - * le32 key values. E.g. used for $O index in FILE_Extend/$ObjId, which 502 + * __le32 key values. E.g. used for $O index in FILE_Extend/$ObjId, which 561 503 * sorts by object_id (16-byte), by splitting up the object_id in four 562 - * le32 values and using them as individual keys. E.g. take the following 504 + * __le32 values and using them as individual keys. E.g. take the following 563 505 * two security_ids, stored as follows on disk: 564 506 * 1st: a1 61 65 b7 65 7b d4 11 9e 3d 00 e0 81 10 42 59 565 507 * 2nd: 38 14 37 d2 d2 f3 d4 11 a5 21 c8 6b 79 b1 97 45 566 - * To compare them, they are split into four le32 values each, like so: 508 + * To compare them, they are split into four __le32 values each, like so: 567 509 * 1st: 0xb76561a1 0x11d47b65 0xe0003d9e 0x59421081 568 510 * 2nd: 0xd2371438 0x11d4f3d2 0x6bc821a5 0x4597b179 569 511 * Now, it is apparent why the 2nd object_id collates after the 1st: the 570 - * first le32 value of the 1st object_id is less than the first le32 of 571 - * the 2nd object_id. If the first le32 values of both object_ids were 572 - * equal then the second le32 values would be compared, etc. 512 + * first __le32 value of the 1st object_id is less than the first __le32 of 513 + * the 2nd object_id. If the first __le32 values of both object_ids were 514 + * equal then the second __le32 values would be compared, etc. 573 515 */ 574 516 enum { 575 517 COLLATION_BINARY = cpu_to_le32(0x00), ··· 581 523 COLLATION_NTOFS_ULONGS = cpu_to_le32(0x13), 582 524 }; 583 525 584 - typedef le32 COLLATION_RULE; 585 - 586 526 /* 527 + * enum - Attribute definition flags 528 + * 587 529 * The flags (32-bit) describing attribute properties in the attribute 588 - * definition structure. FIXME: This information is based on Regis's 589 - * information and, according to him, it is not certain and probably 590 - * incomplete. The INDEXABLE flag is fairly certainly correct as only the file 530 + * definition structure. 531 + * The INDEXABLE flag is fairly certainly correct as only the file 591 532 * name attribute has this flag set and this is the only attribute indexed in 592 533 * NT4. 534 + * 535 + * ATTR_DEF_INDEXABLE: Attribute can be indexed. 536 + * (Used for creating indexes like $I30, $SDH, etc.) 537 + * ATTR_DEF_MULTIPLE: Attribute type can be present multiple times 538 + * in the MFT record of an inode. 539 + * (e.g., multiple $FILE_NAME, $DATA streams) 540 + * ATTR_DEF_NOT_ZERO: Attribute value must contain at least one non-zero byte. 541 + * (Prevents empty or all-zero values) 542 + * ATTR_DEF_INDEXED_UNIQUE: Attribute must be indexed and the value must be unique 543 + * for this attribute type across all MFT records of an inode. 544 + * (e.g., security descriptor IDs in $Secure) 545 + * ATTR_DEF_NAMED_UNIQUE: Attribute must be named and the name must be unique 546 + * for this attribute type across all MFT records of an inode. 547 + * (e.g., named $DATA streams or alternate data streams) 548 + * ATTR_DEF_RESIDENT: Attribute must be resident (stored in MFT record). 549 + * (Cannot be non-resident/sparse/compressed) 550 + * ATTR_DEF_ALWAYS_LOG: Always log modifications to this attribute in LogFile, 551 + * regardless of whether it is resident or non-resident. 552 + * Without this flag, modifications are logged only if resident. 553 + * (Used for critical metadata attributes) 593 554 */ 594 555 enum { 595 - ATTR_DEF_INDEXABLE = cpu_to_le32(0x02), /* Attribute can be 596 - indexed. */ 597 - ATTR_DEF_MULTIPLE = cpu_to_le32(0x04), /* Attribute type 598 - can be present multiple times in the 599 - mft records of an inode. */ 600 - ATTR_DEF_NOT_ZERO = cpu_to_le32(0x08), /* Attribute value 601 - must contain at least one non-zero 602 - byte. */ 603 - ATTR_DEF_INDEXED_UNIQUE = cpu_to_le32(0x10), /* Attribute must be 604 - indexed and the attribute value must be 605 - unique for the attribute type in all of 606 - the mft records of an inode. */ 607 - ATTR_DEF_NAMED_UNIQUE = cpu_to_le32(0x20), /* Attribute must be 608 - named and the name must be unique for 609 - the attribute type in all of the mft 610 - records of an inode. */ 611 - ATTR_DEF_RESIDENT = cpu_to_le32(0x40), /* Attribute must be 612 - resident. */ 613 - ATTR_DEF_ALWAYS_LOG = cpu_to_le32(0x80), /* Always log 614 - modifications to this attribute, 615 - regardless of whether it is resident or 616 - non-resident. Without this, only log 617 - modifications if the attribute is 618 - resident. */ 556 + ATTR_DEF_INDEXABLE = cpu_to_le32(0x02), 557 + ATTR_DEF_MULTIPLE = cpu_to_le32(0x04), 558 + ATTR_DEF_NOT_ZERO = cpu_to_le32(0x08), 559 + ATTR_DEF_INDEXED_UNIQUE = cpu_to_le32(0x10), 560 + ATTR_DEF_NAMED_UNIQUE = cpu_to_le32(0x20), 561 + ATTR_DEF_RESIDENT = cpu_to_le32(0x40), 562 + ATTR_DEF_ALWAYS_LOG = cpu_to_le32(0x80), 619 563 }; 620 564 621 - typedef le32 ATTR_DEF_FLAGS; 622 - 623 565 /* 566 + * struct attr_def - Attribute definition entry ($AttrDef array) 567 + * 624 568 * The data attribute of FILE_AttrDef contains a sequence of attribute 625 569 * definitions for the NTFS volume. With this, it is supposed to be safe for an 626 570 * older NTFS driver to mount a volume containing a newer NTFS version without ··· 630 570 * Entries are sorted by attribute type. The flags describe whether the 631 571 * attribute can be resident/non-resident and possibly other things, but the 632 572 * actual bits are unknown. 573 + * 574 + * @name: Unicode (UTF-16LE) name of the attribute (e.g. "$DATA", "$FILE_NAME"). 575 + * Zero-terminated string, maximum 0x40 characters (128 bytes). 576 + * Used for human-readable display and debugging. 577 + * @type: Attribute type code (ATTR_TYPE_* constants). 578 + * Defines which attribute this entry describes. 579 + * @display_rule: Default display rule (usually 0; rarely used in modern NTFS). 580 + * Controls how the attribute is displayed in tools (legacy). 581 + * @collation_rule: Default collation rule for indexing this attribute. 582 + * Determines sort order when indexed (e.g. CASE_SENSITIVE, UNICODE). 583 + * Used in $I30, $SDH, $SII, etc. 584 + * @flags: Bit array of attribute constraints (ATTR_DEF_* flags). 585 + * See ATTR_DEF_INDEXABLE, ATTR_DEF_MULTIPLE, etc. 586 + * Defines whether the attribute can be indexed, multiple, resident-only, etc. 587 + * @min_size: Optional minimum size of the attribute value (in bytes). 588 + * 0 means no minimum enforced. 589 + * @max_size: Maximum allowed size of the attribute value (in bytes). 633 590 */ 634 - typedef struct { 635 - /*hex ofs*/ 636 - /* 0*/ ntfschar name[0x40]; /* Unicode name of the attribute. Zero 637 - terminated. */ 638 - /* 80*/ ATTR_TYPE type; /* Type of the attribute. */ 639 - /* 84*/ le32 display_rule; /* Default display rule. 640 - FIXME: What does it mean? (AIA) */ 641 - /* 88*/ COLLATION_RULE collation_rule; /* Default collation rule. */ 642 - /* 8c*/ ATTR_DEF_FLAGS flags; /* Flags describing the attribute. */ 643 - /* 90*/ sle64 min_size; /* Optional minimum attribute size. */ 644 - /* 98*/ sle64 max_size; /* Maximum size of attribute. */ 645 - /* sizeof() = 0xa0 or 160 bytes */ 646 - } __attribute__ ((__packed__)) ATTR_DEF; 591 + struct attr_def { 592 + __le16 name[0x40]; 593 + __le32 type; 594 + __le32 display_rule; 595 + __le32 collation_rule; 596 + __le32 flags; 597 + __le64 min_size; 598 + __le64 max_size; 599 + } __packed; 600 + 601 + static_assert(sizeof(struct attr_def) == 160); 647 602 648 603 /* 649 - * Attribute flags (16-bit). 604 + * enum - Attribute flags (16-bit) for non-resident attributes 605 + * 606 + * ATTR_IS_COMPRESSED: Attribute is compressed. 607 + * If set, data is compressed using the method in 608 + * ATTR_COMPRESSION_MASK. 609 + * ATTR_COMPRESSION_MASK: Mask for compression method. 610 + * Valid values are defined in NTFS compression types 611 + * (e.g., 0x02 = LZNT1, etc.). 612 + * Also serves as the first illegal value for method. 613 + * ATTR_IS_ENCRYPTED: Attribute is encrypted. 614 + * Data is encrypted using EFS (Encrypting File System). 615 + * ATTR_IS_SPARSE: Attribute is sparse. 616 + * Contains holes (unallocated regions) that read as zeros. 650 617 */ 651 618 enum { 652 619 ATTR_IS_COMPRESSED = cpu_to_le16(0x0001), 653 - ATTR_COMPRESSION_MASK = cpu_to_le16(0x00ff), /* Compression method 654 - mask. Also, first 655 - illegal value. */ 620 + ATTR_COMPRESSION_MASK = cpu_to_le16(0x00ff), 656 621 ATTR_IS_ENCRYPTED = cpu_to_le16(0x4000), 657 622 ATTR_IS_SPARSE = cpu_to_le16(0x8000), 658 - } __attribute__ ((__packed__)); 659 - 660 - typedef le16 ATTR_FLAGS; 623 + } __packed; 661 624 662 625 /* 663 626 * Attribute compression. ··· 750 667 */ 751 668 752 669 /* 753 - * Flags of resident attributes (8-bit). 670 + * enum - Flags for resident attributes (8-bit) 671 + * 672 + * RESIDENT_ATTR_IS_INDEXED: Attribute is referenced in an index. 673 + * (e.g., part of an index key or entry) 674 + * Has implications for deletion and modification: 675 + * - Cannot be freely removed if indexed 676 + * - Index must be updated when value changes 677 + * - Used for attributes like $FILE_NAME in directories 754 678 */ 755 679 enum { 756 - RESIDENT_ATTR_IS_INDEXED = 0x01, /* Attribute is referenced in an index 757 - (has implications for deleting and 758 - modifying the attribute). */ 759 - } __attribute__ ((__packed__)); 760 - 761 - typedef u8 RESIDENT_ATTR_FLAGS; 680 + RESIDENT_ATTR_IS_INDEXED = 0x01, 681 + } __packed; 762 682 763 683 /* 764 - * Attribute record header. Always aligned to 8-byte boundary. 684 + * struct attr_record - NTFS attribute record header 685 + * 686 + * Common header for both resident and non-resident attributes. 687 + * Always aligned to an 8-byte boundary on disk. 688 + * Located at attrs_offset in the MFT record (see struct mft_record). 689 + * 690 + * @type: 32-bit attribute type (ATTR_TYPE_* constants). 691 + * Identifies the attribute 692 + * (e.g. 0x10 = $STANDARD_INFORMATION). 693 + * @length: Total byte size of this attribute record (resident). 694 + * 8-byte aligned; used to locate the next attribute. 695 + * @non_resident: 0 = resident attribute 696 + * 1 = non-resident attribute 697 + * @name_length: Number of Unicode characters in the attribute name. 698 + * 0 if unnamed (most system attributes are unnamed). 699 + * @name_offset: Byte offset from start of attribute record to the name. 700 + * 8-byte aligned; when creating, place at end of header. 701 + * @flags: Attribute flags (see ATTR_IS_COMPRESSED, 702 + * ATTR_IS_ENCRYPTED, etc.). 703 + * For resident: see RESIDENT_ATTR_* flags. 704 + * @instance: Unique instance number within this MFT record. 705 + * Incremented via next_attr_instance; unique per record. 706 + * 707 + * Resident attributes (when @non_resident == 0): 708 + * @data.resident.value_length: Byte size of the attribute value. 709 + * @data.resident.value_offset: Byte offset from start of attribute 710 + * record to the value data. 711 + * 8-byte aligned if name present. 712 + * @data.resident.flags: Resident-specific flags 713 + * @data.resident.reserved: Reserved/alignment to 8 bytes. 714 + * 715 + * Non-resident attributes (when @non_resident == 1): 716 + * @data.non_resident.lowest_vcn: Lowest valid VCN in this extent. 717 + * Usually 0 unless attribute list is used. 718 + * @data.non_resident.highest_vcn: Highest valid VCN in this extent. 719 + * -1 for zero-length, 0 for single extent. 720 + * @data.non_resident.mapping_pairs_offset: 721 + * Byte offset to mapping pairs array 722 + * (VCN → LCN mappings). 723 + * 8-byte aligned when creating. 724 + * @data.non_resident.compression_unit: 725 + * Log2 of clusters per compression unit. 726 + * 0 = not compressed. 727 + * WinNT4 used 4; sparse files use 0 728 + * on XP SP2+. 729 + * @data.non_resident.reserved: 5 bytes for 8-byte alignment. 730 + * @data.non_resident.allocated_size: 731 + * Allocated disk space in bytes. 732 + * For compressed: logical allocated size. 733 + * @data.non_resident.data_size: Logical attribute value size in bytes. 734 + * Can be larger than allocated_size if 735 + * compressed/sparse. 736 + * @data.non_resident.initialized_size: 737 + * Initialized portion size in bytes. 738 + * Usually equals data_size. 739 + * @data.non_resident.compressed_size: 740 + * Compressed on-disk size in bytes. 741 + * Only present when compressed or sparse. 742 + * Actual disk usage. 765 743 */ 766 - typedef struct { 767 - /*Ofs*/ 768 - /* 0*/ ATTR_TYPE type; /* The (32-bit) type of the attribute. */ 769 - /* 4*/ le32 length; /* Byte size of the resident part of the 770 - attribute (aligned to 8-byte boundary). 771 - Used to get to the next attribute. */ 772 - /* 8*/ u8 non_resident; /* If 0, attribute is resident. 773 - If 1, attribute is non-resident. */ 774 - /* 9*/ u8 name_length; /* Unicode character size of name of attribute. 775 - 0 if unnamed. */ 776 - /* 10*/ le16 name_offset; /* If name_length != 0, the byte offset to the 777 - beginning of the name from the attribute 778 - record. Note that the name is stored as a 779 - Unicode string. When creating, place offset 780 - just at the end of the record header. Then, 781 - follow with attribute value or mapping pairs 782 - array, resident and non-resident attributes 783 - respectively, aligning to an 8-byte 784 - boundary. */ 785 - /* 12*/ ATTR_FLAGS flags; /* Flags describing the attribute. */ 786 - /* 14*/ le16 instance; /* The instance of this attribute record. This 787 - number is unique within this mft record (see 788 - MFT_RECORD/next_attribute_instance notes in 789 - mft.h for more details). */ 790 - /* 16*/ union { 791 - /* Resident attributes. */ 744 + struct attr_record { 745 + __le32 type; 746 + __le32 length; 747 + u8 non_resident; 748 + u8 name_length; 749 + __le16 name_offset; 750 + __le16 flags; 751 + __le16 instance; 752 + union { 792 753 struct { 793 - /* 16 */ le32 value_length;/* Byte size of attribute value. */ 794 - /* 20 */ le16 value_offset;/* Byte offset of the attribute 795 - value from the start of the 796 - attribute record. When creating, 797 - align to 8-byte boundary if we 798 - have a name present as this might 799 - not have a length of a multiple 800 - of 8-bytes. */ 801 - /* 22 */ RESIDENT_ATTR_FLAGS flags; /* See above. */ 802 - /* 23 */ s8 reserved; /* Reserved/alignment to 8-byte 803 - boundary. */ 804 - } __attribute__ ((__packed__)) resident; 805 - /* Non-resident attributes. */ 754 + __le32 value_length; 755 + __le16 value_offset; 756 + u8 flags; 757 + s8 reserved; 758 + } __packed resident; 806 759 struct { 807 - /* 16*/ leVCN lowest_vcn;/* Lowest valid virtual cluster number 808 - for this portion of the attribute value or 809 - 0 if this is the only extent (usually the 810 - case). - Only when an attribute list is used 811 - does lowest_vcn != 0 ever occur. */ 812 - /* 24*/ leVCN highest_vcn;/* Highest valid vcn of this extent of 813 - the attribute value. - Usually there is only one 814 - portion, so this usually equals the attribute 815 - value size in clusters minus 1. Can be -1 for 816 - zero length files. Can be 0 for "single extent" 817 - attributes. */ 818 - /* 32*/ le16 mapping_pairs_offset; /* Byte offset from the 819 - beginning of the structure to the mapping pairs 820 - array which contains the mappings between the 821 - vcns and the logical cluster numbers (lcns). 822 - When creating, place this at the end of this 823 - record header aligned to 8-byte boundary. */ 824 - /* 34*/ u8 compression_unit; /* The compression unit expressed 825 - as the log to the base 2 of the number of 826 - clusters in a compression unit. 0 means not 827 - compressed. (This effectively limits the 828 - compression unit size to be a power of two 829 - clusters.) WinNT4 only uses a value of 4. 830 - Sparse files have this set to 0 on XPSP2. */ 831 - /* 35*/ u8 reserved[5]; /* Align to 8-byte boundary. */ 832 - /* The sizes below are only used when lowest_vcn is zero, as otherwise it would 833 - be difficult to keep them up-to-date.*/ 834 - /* 40*/ sle64 allocated_size; /* Byte size of disk space 835 - allocated to hold the attribute value. Always 836 - is a multiple of the cluster size. When a file 837 - is compressed, this field is a multiple of the 838 - compression block size (2^compression_unit) and 839 - it represents the logically allocated space 840 - rather than the actual on disk usage. For this 841 - use the compressed_size (see below). */ 842 - /* 48*/ sle64 data_size; /* Byte size of the attribute 843 - value. Can be larger than allocated_size if 844 - attribute value is compressed or sparse. */ 845 - /* 56*/ sle64 initialized_size; /* Byte size of initialized 846 - portion of the attribute value. Usually equals 847 - data_size. */ 848 - /* sizeof(uncompressed attr) = 64*/ 849 - /* 64*/ sle64 compressed_size; /* Byte size of the attribute 850 - value after compression. Only present when 851 - compressed or sparse. Always is a multiple of 852 - the cluster size. Represents the actual amount 853 - of disk space being used on the disk. */ 854 - /* sizeof(compressed attr) = 72*/ 855 - } __attribute__ ((__packed__)) non_resident; 856 - } __attribute__ ((__packed__)) data; 857 - } __attribute__ ((__packed__)) ATTR_RECORD; 858 - 859 - typedef ATTR_RECORD ATTR_REC; 760 + __le64 lowest_vcn; 761 + __le64 highest_vcn; 762 + __le16 mapping_pairs_offset; 763 + u8 compression_unit; 764 + u8 reserved[5]; 765 + __le64 allocated_size; 766 + __le64 data_size; 767 + __le64 initialized_size; 768 + __le64 compressed_size; 769 + } __packed non_resident; 770 + } __packed data; 771 + } __packed; 860 772 861 773 /* 774 + * enum - NTFS file attribute flags (32-bit) 775 + * 862 776 * File attribute flags (32-bit) appearing in the file_attributes fields of the 863 777 * STANDARD_INFORMATION attribute of MFT_RECORDs and the FILENAME_ATTR 864 778 * attributes of MFT_RECORDs and directory index entries. ··· 864 784 * appear in the STANDARD_INFORMATION attribute whilst only some others appear 865 785 * in the FILENAME_ATTR attribute of MFT_RECORDs. Unless otherwise stated the 866 786 * flags appear in all of the above. 787 + * 788 + * FILE_ATTR_READONLY: File is read-only. 789 + * FILE_ATTR_HIDDEN: File is hidden (not shown by default). 790 + * FILE_ATTR_SYSTEM: System file (protected by OS). 791 + * FILE_ATTR_DIRECTORY: Directory flag (reserved in NT; use MFT flag instead). 792 + * FILE_ATTR_ARCHIVE: File needs archiving (backup flag). 793 + * FILE_ATTR_DEVICE: Device file (rarely used). 794 + * FILE_ATTR_NORMAL: Normal file (no special attributes). 795 + * FILE_ATTR_TEMPORARY: Temporary file (delete on close). 796 + * FILE_ATTR_SPARSE_FILE: Sparse file (contains holes). 797 + * FILE_ATTR_REPARSE_POINT: Reparse point (junction, symlink, mount point). 798 + * FILE_ATTR_COMPRESSED: File is compressed. 799 + * FILE_ATTR_OFFLINE: File data is offline (not locally available). 800 + * FILE_ATTR_NOT_CONTENT_INDEXED: 801 + * File is excluded from content indexing. 802 + * FILE_ATTR_ENCRYPTED: File is encrypted (EFS). 803 + * FILE_ATTR_VALID_FLAGS: Mask of all valid flags for reading. 804 + * FILE_ATTR_VALID_SET_FLAGS: Mask of flags that can be set by user. 805 + * FILE_ATTRIBUTE_RECALL_ON_OPEN: 806 + * Recall data on open (cloud/HSM related). 807 + * FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT: 808 + * $FILE_NAME has duplicate index entry. 809 + * FILE_ATTR_DUP_VIEW_INDEX_PRESENT: 810 + * Duplicate view index present (object ID, quota, etc.). 867 811 */ 868 812 enum { 869 813 FILE_ATTR_READONLY = cpu_to_le32(0x00000001), 870 814 FILE_ATTR_HIDDEN = cpu_to_le32(0x00000002), 871 815 FILE_ATTR_SYSTEM = cpu_to_le32(0x00000004), 872 816 /* Old DOS volid. Unused in NT. = cpu_to_le32(0x00000008), */ 873 - 874 817 FILE_ATTR_DIRECTORY = cpu_to_le32(0x00000010), 875 - /* Note, FILE_ATTR_DIRECTORY is not considered valid in NT. It is 876 - reserved for the DOS SUBDIRECTORY flag. */ 877 818 FILE_ATTR_ARCHIVE = cpu_to_le32(0x00000020), 878 819 FILE_ATTR_DEVICE = cpu_to_le32(0x00000040), 879 820 FILE_ATTR_NORMAL = cpu_to_le32(0x00000080), ··· 909 808 FILE_ATTR_ENCRYPTED = cpu_to_le32(0x00004000), 910 809 911 810 FILE_ATTR_VALID_FLAGS = cpu_to_le32(0x00007fb7), 912 - /* Note, FILE_ATTR_VALID_FLAGS masks out the old DOS VolId and the 913 - FILE_ATTR_DEVICE and preserves everything else. This mask is used 914 - to obtain all flags that are valid for reading. */ 915 811 FILE_ATTR_VALID_SET_FLAGS = cpu_to_le32(0x000031a7), 916 - /* Note, FILE_ATTR_VALID_SET_FLAGS masks out the old DOS VolId, the 917 - F_A_DEVICE, F_A_DIRECTORY, F_A_SPARSE_FILE, F_A_REPARSE_POINT, 918 - F_A_COMPRESSED, and F_A_ENCRYPTED and preserves the rest. This mask 919 - is used to obtain all flags that are valid for setting. */ 920 - /* 921 - * The flag FILE_ATTR_DUP_FILENAME_INDEX_PRESENT is present in all 922 - * FILENAME_ATTR attributes but not in the STANDARD_INFORMATION 923 - * attribute of an mft record. 924 - */ 812 + FILE_ATTRIBUTE_RECALL_ON_OPEN = cpu_to_le32(0x00040000), 925 813 FILE_ATTR_DUP_FILE_NAME_INDEX_PRESENT = cpu_to_le32(0x10000000), 926 - /* Note, this is a copy of the corresponding bit from the mft record, 927 - telling us whether this is a directory or not, i.e. whether it has 928 - an index root attribute or not. */ 929 814 FILE_ATTR_DUP_VIEW_INDEX_PRESENT = cpu_to_le32(0x20000000), 930 - /* Note, this is a copy of the corresponding bit from the mft record, 931 - telling us whether this file has a view index present (eg. object id 932 - index, quota index, one of the security indexes or the encrypting 933 - filesystem related indexes). */ 934 815 }; 935 - 936 - typedef le32 FILE_ATTR_FLAGS; 937 816 938 817 /* 939 818 * NOTE on times in NTFS: All times are in MS standard time format, i.e. they ··· 923 842 */ 924 843 925 844 /* 926 - * Attribute: Standard information (0x10). 845 + * struct standard_information - $STANDARD_INFORMATION attribute content 927 846 * 928 847 * NOTE: Always resident. 929 848 * NOTE: Present in all base file records on a volume. ··· 931 850 * fields but the meaning as defined below has been verified to be 932 851 * correct by practical experimentation on Windows NT4 SP6a and is hence 933 852 * assumed to be the one and only correct interpretation. 853 + * 854 + * @creation_time: File creation time (NTFS timestamp). 855 + * Updated on filename change(?). 856 + * @last_data_change_time: Last modification time of data streams. 857 + * @last_mft_change_time: Last modification time of this MFT record. 858 + * @last_access_time: Last access time (approximate). 859 + * Not updated on read-only volumes; can be disabled. 860 + * @file_attributes: File attribute flags (FILE_ATTR_* bits). 861 + * 862 + * Union (version-specific fields): 863 + * @ver.v1.reserved12: 12 bytes reserved/alignment (NTFS 1.2 only). 864 + * 865 + * @ver.v3 (NTFS 3.x / Windows 2000+): 866 + * @maximum_versions: Max allowed file versions (0 = disabled). 867 + * @version_number: Current version number (0 if disabled). 868 + * @class_id: Class ID (from bidirectional index?). 869 + * @owner_id: Owner ID (maps to $Quota via $Q index). 870 + * @security_id: Security ID (maps to $Secure $SII/$SDS). 871 + * @quota_charged: Quota charge in bytes (0 if quotas disabled). 872 + * @usn: Last USN from $UsnJrnl (0 if disabled). 934 873 */ 935 - typedef struct { 936 - /*Ofs*/ 937 - /* 0*/ sle64 creation_time; /* Time file was created. Updated when 938 - a filename is changed(?). */ 939 - /* 8*/ sle64 last_data_change_time; /* Time the data attribute was last 940 - modified. */ 941 - /* 16*/ sle64 last_mft_change_time; /* Time this mft record was last 942 - modified. */ 943 - /* 24*/ sle64 last_access_time; /* Approximate time when the file was 944 - last accessed (obviously this is not 945 - updated on read-only volumes). In 946 - Windows this is only updated when 947 - accessed if some time delta has 948 - passed since the last update. Also, 949 - last access time updates can be 950 - disabled altogether for speed. */ 951 - /* 32*/ FILE_ATTR_FLAGS file_attributes; /* Flags describing the file. */ 952 - /* 36*/ union { 953 - /* NTFS 1.2 */ 874 + struct standard_information { 875 + __le64 creation_time; 876 + __le64 last_data_change_time; 877 + __le64 last_mft_change_time; 878 + __le64 last_access_time; 879 + __le32 file_attributes; 880 + union { 954 881 struct { 955 - /* 36*/ u8 reserved12[12]; /* Reserved/alignment to 8-byte 956 - boundary. */ 957 - } __attribute__ ((__packed__)) v1; 958 - /* sizeof() = 48 bytes */ 959 - /* NTFS 3.x */ 882 + u8 reserved12[12]; 883 + } __packed v1; 960 884 struct { 961 - /* 962 - * If a volume has been upgraded from a previous NTFS version, then these 963 - * fields are present only if the file has been accessed since the upgrade. 964 - * Recognize the difference by comparing the length of the resident attribute 965 - * value. If it is 48, then the following fields are missing. If it is 72 then 966 - * the fields are present. Maybe just check like this: 967 - * if (resident.ValueLength < sizeof(STANDARD_INFORMATION)) { 968 - * Assume NTFS 1.2- format. 969 - * If (volume version is 3.x) 970 - * Upgrade attribute to NTFS 3.x format. 971 - * else 972 - * Use NTFS 1.2- format for access. 973 - * } else 974 - * Use NTFS 3.x format for access. 975 - * Only problem is that it might be legal to set the length of the value to 976 - * arbitrarily large values thus spoiling this check. - But chkdsk probably 977 - * views that as a corruption, assuming that it behaves like this for all 978 - * attributes. 979 - */ 980 - /* 36*/ le32 maximum_versions; /* Maximum allowed versions for 981 - file. Zero if version numbering is disabled. */ 982 - /* 40*/ le32 version_number; /* This file's version (if any). 983 - Set to zero if maximum_versions is zero. */ 984 - /* 44*/ le32 class_id; /* Class id from bidirectional 985 - class id index (?). */ 986 - /* 48*/ le32 owner_id; /* Owner_id of the user owning 987 - the file. Translate via $Q index in FILE_Extend 988 - /$Quota to the quota control entry for the user 989 - owning the file. Zero if quotas are disabled. */ 990 - /* 52*/ le32 security_id; /* Security_id for the file. 991 - Translate via $SII index and $SDS data stream 992 - in FILE_Secure to the security descriptor. */ 993 - /* 56*/ le64 quota_charged; /* Byte size of the charge to 994 - the quota for all streams of the file. Note: Is 995 - zero if quotas are disabled. */ 996 - /* 64*/ leUSN usn; /* Last update sequence number 997 - of the file. This is a direct index into the 998 - transaction log file ($UsnJrnl). It is zero if 999 - the usn journal is disabled or this file has 1000 - not been subject to logging yet. See usnjrnl.h 1001 - for details. */ 1002 - } __attribute__ ((__packed__)) v3; 1003 - /* sizeof() = 72 bytes (NTFS 3.x) */ 1004 - } __attribute__ ((__packed__)) ver; 1005 - } __attribute__ ((__packed__)) STANDARD_INFORMATION; 885 + __le32 maximum_versions; 886 + __le32 version_number; 887 + __le32 class_id; 888 + __le32 owner_id; 889 + __le32 security_id; 890 + __le64 quota_charged; 891 + __le64 usn; 892 + } __packed v3; 893 + } __packed ver; 894 + } __packed; 1006 895 1007 896 /* 1008 - * Attribute: Attribute list (0x20). 897 + * struct attr_list_entry - Entry in $ATTRIBUTE_LIST attribute. 898 + * 899 + * @type: Attribute type code (ATTR_TYPE_*). 900 + * @length: Byte size of this entry (8-byte aligned). 901 + * @name_length: Unicode char count of attribute name (0 if unnamed). 902 + * @name_offset: Byte offset from start of entry to name (always set). 903 + * @lowest_vcn: Lowest VCN of this attribute extent (usually 0). 904 + * Signed value; non-zero when attribute spans extents. 905 + * @mft_reference: MFT record reference holding this attribute extent. 906 + * @instance: Attribute instance number (if lowest_vcn == 0); else 0. 907 + * @name: Variable Unicode name (use @name_offset when reading). 1009 908 * 1010 909 * - Can be either resident or non-resident. 1011 910 * - Value consists of a sequence of variable length, 8-byte aligned, ··· 998 937 * itself. The list is sorted: first by attribute type, second by attribute 999 938 * name (if present), third by instance number. The extents of one 1000 939 * non-resident attribute (if present) immediately follow after the initial 1001 - * extent. They are ordered by lowest_vcn and have their instace set to zero. 940 + * extent. They are ordered by lowest_vcn and have their instance set to zero. 1002 941 * It is not allowed to have two attributes with all sorting keys equal. 1003 942 * - Further restrictions: 1004 943 * - If not resident, the vcn to lcn mapping array has to fit inside the ··· 1016 955 * NTFS 3.0 volumes). 1017 956 * - There are many named streams. 1018 957 */ 1019 - typedef struct { 1020 - /*Ofs*/ 1021 - /* 0*/ ATTR_TYPE type; /* Type of referenced attribute. */ 1022 - /* 4*/ le16 length; /* Byte size of this entry (8-byte aligned). */ 1023 - /* 6*/ u8 name_length; /* Size in Unicode chars of the name of the 1024 - attribute or 0 if unnamed. */ 1025 - /* 7*/ u8 name_offset; /* Byte offset to beginning of attribute name 1026 - (always set this to where the name would 1027 - start even if unnamed). */ 1028 - /* 8*/ leVCN lowest_vcn; /* Lowest virtual cluster number of this portion 1029 - of the attribute value. This is usually 0. It 1030 - is non-zero for the case where one attribute 1031 - does not fit into one mft record and thus 1032 - several mft records are allocated to hold 1033 - this attribute. In the latter case, each mft 1034 - record holds one extent of the attribute and 1035 - there is one attribute list entry for each 1036 - extent. NOTE: This is DEFINITELY a signed 1037 - value! The windows driver uses cmp, followed 1038 - by jg when comparing this, thus it treats it 1039 - as signed. */ 1040 - /* 16*/ leMFT_REF mft_reference;/* The reference of the mft record holding 1041 - the ATTR_RECORD for this portion of the 1042 - attribute value. */ 1043 - /* 24*/ le16 instance; /* If lowest_vcn = 0, the instance of the 1044 - attribute being referenced; otherwise 0. */ 1045 - /* 26*/ ntfschar name[0]; /* Use when creating only. When reading use 1046 - name_offset to determine the location of the 1047 - name. */ 1048 - /* sizeof() = 26 + (attribute_name_length * 2) bytes */ 1049 - } __attribute__ ((__packed__)) ATTR_LIST_ENTRY; 958 + struct attr_list_entry { 959 + __le32 type; 960 + __le16 length; 961 + u8 name_length; 962 + u8 name_offset; 963 + __le64 lowest_vcn; 964 + __le64 mft_reference; 965 + __le16 instance; 966 + __le16 name[]; 967 + } __packed; 1050 968 1051 969 /* 1052 970 * The maximum allowed length for a file name. ··· 1033 993 #define MAXIMUM_FILE_NAME_LENGTH 255 1034 994 1035 995 /* 1036 - * Possible namespaces for filenames in ntfs (8-bit). 996 + * enum - Possible namespaces for filenames in ntfs (8-bit). 997 + * 998 + * FILE_NAME_POSIX POSIX namespace (case sensitive, most permissive). 999 + * Allows all Unicode except '\0' and '/'. 1000 + * WinNT/2k/2003 default utilities ignore case 1001 + * differences. SFU (Services For Unix) enables true 1002 + * case sensitivity. 1003 + * SFU restricts some chars: '"', '/', '<', '>', '\'. 1004 + * FILE_NAME_WIN32 Standard WinNT/2k long filename namespace 1005 + * (case insensitive). 1006 + * Disallows '\0', '"', '*', '/', ':', '<', '>', '?', 1007 + * '\', '|'. Names cannot end with '.' or space. 1008 + * FILE_NAME_DOS DOS 8.3 namespace (uppercase only). 1009 + * Allows 8-bit chars > space except '"', '*', '+', 1010 + * ',', '/', ':', ';', '<', '=', '>', '?', '\'. 1011 + * FILE_NAME_WIN32_AND_DOS 1012 + * Win32 and DOS names are identical (single record). 1013 + * Value 0x03 indicates both are stored in one entry. 1037 1014 */ 1038 1015 enum { 1039 1016 FILE_NAME_POSIX = 0x00, 1040 - /* This is the largest namespace. It is case sensitive and allows all 1041 - Unicode characters except for: '\0' and '/'. Beware that in 1042 - WinNT/2k/2003 by default files which eg have the same name except 1043 - for their case will not be distinguished by the standard utilities 1044 - and thus a "del filename" will delete both "filename" and "fileName" 1045 - without warning. However if for example Services For Unix (SFU) are 1046 - installed and the case sensitive option was enabled at installation 1047 - time, then you can create/access/delete such files. 1048 - Note that even SFU places restrictions on the filenames beyond the 1049 - '\0' and '/' and in particular the following set of characters is 1050 - not allowed: '"', '/', '<', '>', '\'. All other characters, 1051 - including the ones no allowed in WIN32 namespace are allowed. 1052 - Tested with SFU 3.5 (this is now free) running on Windows XP. */ 1053 1017 FILE_NAME_WIN32 = 0x01, 1054 - /* The standard WinNT/2k NTFS long filenames. Case insensitive. All 1055 - Unicode chars except: '\0', '"', '*', '/', ':', '<', '>', '?', '\', 1056 - and '|'. Further, names cannot end with a '.' or a space. */ 1057 1018 FILE_NAME_DOS = 0x02, 1058 - /* The standard DOS filenames (8.3 format). Uppercase only. All 8-bit 1059 - characters greater space, except: '"', '*', '+', ',', '/', ':', ';', 1060 - '<', '=', '>', '?', and '\'. */ 1061 1019 FILE_NAME_WIN32_AND_DOS = 0x03, 1062 - /* 3 means that both the Win32 and the DOS filenames are identical and 1063 - hence have been saved in this single filename record. */ 1064 - } __attribute__ ((__packed__)); 1065 - 1066 - typedef u8 FILE_NAME_TYPE_FLAGS; 1020 + } __packed; 1067 1021 1068 1022 /* 1069 - * Attribute: Filename (0x30). 1023 + * struct file_name_attr - $FILE_NAME attribute content 1070 1024 * 1071 1025 * NOTE: Always resident. 1072 1026 * NOTE: All fields, except the parent_directory, are only updated when the ··· 1071 1037 * fields but the meaning as defined below has been verified to be 1072 1038 * correct by practical experimentation on Windows NT4 SP6a and is hence 1073 1039 * assumed to be the one and only correct interpretation. 1040 + * 1041 + * @parent_directory: MFT reference to parent directory. 1042 + * @creation_time: File creation time (NTFS timestamp). 1043 + * @last_data_change_time: 1044 + * Last data modification time. 1045 + * @last_mft_change_time: 1046 + * Last MFT record modification time. 1047 + * @last_access_time: Last access time (approximate; may not 1048 + * update always). 1049 + * @allocated_size: On-disk allocated size for unnamed $DATA. 1050 + * Equals compressed_size if compressed/sparse. 1051 + * 0 for directories or no $DATA. 1052 + * Multiple of cluster size. 1053 + * @data_size: Logical size of unnamed $DATA. 1054 + * 0 for directories or no $DATA. 1055 + * @file_attributes: File attribute flags (FILE_ATTR_* bits). 1056 + * @type.ea.packed_ea_size: 1057 + * Size needed to pack EAs (if present). 1058 + * @type.ea.reserved: Alignment padding. 1059 + * @type.rp.reparse_point_tag: 1060 + * Reparse point type (if reparse point, no EAs). 1061 + * @file_name_length: Length of filename in Unicode characters. 1062 + * @file_name_type: Namespace (FILE_NAME_POSIX, WIN32, DOS, etc.). 1063 + * @file_name: Variable-length Unicode filename. 1074 1064 */ 1075 - typedef struct { 1076 - /*hex ofs*/ 1077 - /* 0*/ leMFT_REF parent_directory; /* Directory this filename is 1078 - referenced from. */ 1079 - /* 8*/ sle64 creation_time; /* Time file was created. */ 1080 - /* 10*/ sle64 last_data_change_time; /* Time the data attribute was last 1081 - modified. */ 1082 - /* 18*/ sle64 last_mft_change_time; /* Time this mft record was last 1083 - modified. */ 1084 - /* 20*/ sle64 last_access_time; /* Time this mft record was last 1085 - accessed. */ 1086 - /* 28*/ sle64 allocated_size; /* Byte size of on-disk allocated space 1087 - for the unnamed data attribute. So 1088 - for normal $DATA, this is the 1089 - allocated_size from the unnamed 1090 - $DATA attribute and for compressed 1091 - and/or sparse $DATA, this is the 1092 - compressed_size from the unnamed 1093 - $DATA attribute. For a directory or 1094 - other inode without an unnamed $DATA 1095 - attribute, this is always 0. NOTE: 1096 - This is a multiple of the cluster 1097 - size. */ 1098 - /* 30*/ sle64 data_size; /* Byte size of actual data in unnamed 1099 - data attribute. For a directory or 1100 - other inode without an unnamed $DATA 1101 - attribute, this is always 0. */ 1102 - /* 38*/ FILE_ATTR_FLAGS file_attributes; /* Flags describing the file. */ 1103 - /* 3c*/ union { 1104 - /* 3c*/ struct { 1105 - /* 3c*/ le16 packed_ea_size; /* Size of the buffer needed to 1106 - pack the extended attributes 1107 - (EAs), if such are present.*/ 1108 - /* 3e*/ le16 reserved; /* Reserved for alignment. */ 1109 - } __attribute__ ((__packed__)) ea; 1110 - /* 3c*/ struct { 1111 - /* 3c*/ le32 reparse_point_tag; /* Type of reparse point, 1112 - present only in reparse 1113 - points and only if there are 1114 - no EAs. */ 1115 - } __attribute__ ((__packed__)) rp; 1116 - } __attribute__ ((__packed__)) type; 1117 - /* 40*/ u8 file_name_length; /* Length of file name in 1118 - (Unicode) characters. */ 1119 - /* 41*/ FILE_NAME_TYPE_FLAGS file_name_type; /* Namespace of the file name.*/ 1120 - /* 42*/ ntfschar file_name[0]; /* File name in Unicode. */ 1121 - } __attribute__ ((__packed__)) FILE_NAME_ATTR; 1065 + struct file_name_attr { 1066 + __le64 parent_directory; 1067 + __le64 creation_time; 1068 + __le64 last_data_change_time; 1069 + __le64 last_mft_change_time; 1070 + __le64 last_access_time; 1071 + __le64 allocated_size; 1072 + __le64 data_size; 1073 + __le32 file_attributes; 1074 + union { 1075 + struct { 1076 + __le16 packed_ea_size; 1077 + __le16 reserved; 1078 + } __packed ea; 1079 + struct { 1080 + __le32 reparse_point_tag; 1081 + } __packed rp; 1082 + } __packed type; 1083 + u8 file_name_length; 1084 + u8 file_name_type; 1085 + __le16 file_name[]; 1086 + } __packed; 1122 1087 1123 1088 /* 1089 + * struct guid - Globally Unique Identifier (GUID) structure 1090 + * 1124 1091 * GUID structures store globally unique identifiers (GUID). A GUID is a 1125 1092 * 128-bit value consisting of one group of eight hexadecimal digits, followed 1126 1093 * by three groups of four hexadecimal digits each, followed by one group of ··· 1129 1094 * distributed computing environment (DCE) universally unique identifier (UUID). 1130 1095 * Example of a GUID: 1131 1096 * 1F010768-5A73-BC91-0010A52216A7 1097 + * 1098 + * @data1: First 32 bits (first 8 hex digits). 1099 + * @data2: Next 16 bits (first group of 4 hex digits). 1100 + * @data3: Next 16 bits (second group of 4 hex digits). 1101 + * @data4: Final 64 bits (third group of 4 + last 12 hex digits). 1102 + * data4[0-1]: third group; data4[2-7]: remaining part. 1132 1103 */ 1133 - typedef struct { 1134 - le32 data1; /* The first eight hexadecimal digits of the GUID. */ 1135 - le16 data2; /* The first group of four hexadecimal digits. */ 1136 - le16 data3; /* The second group of four hexadecimal digits. */ 1137 - u8 data4[8]; /* The first two bytes are the third group of four 1138 - hexadecimal digits. The remaining six bytes are the 1139 - final 12 hexadecimal digits. */ 1140 - } __attribute__ ((__packed__)) GUID; 1104 + struct guid { 1105 + __le32 data1; 1106 + __le16 data2; 1107 + __le16 data3; 1108 + u8 data4[8]; 1109 + } __packed; 1141 1110 1142 1111 /* 1143 - * FILE_Extend/$ObjId contains an index named $O. This index contains all 1144 - * object_ids present on the volume as the index keys and the corresponding 1145 - * mft_record numbers as the index entry data parts. The data part (defined 1146 - * below) also contains three other object_ids: 1147 - * birth_volume_id - object_id of FILE_Volume on which the file was first 1148 - * created. Optional (i.e. can be zero). 1149 - * birth_object_id - object_id of file when it was first created. Usually 1150 - * equals the object_id. Optional (i.e. can be zero). 1151 - * domain_id - Reserved (always zero). 1152 - */ 1153 - typedef struct { 1154 - leMFT_REF mft_reference;/* Mft record containing the object_id in 1155 - the index entry key. */ 1156 - union { 1157 - struct { 1158 - GUID birth_volume_id; 1159 - GUID birth_object_id; 1160 - GUID domain_id; 1161 - } __attribute__ ((__packed__)) origin; 1162 - u8 extended_info[48]; 1163 - } __attribute__ ((__packed__)) opt; 1164 - } __attribute__ ((__packed__)) OBJ_ID_INDEX_DATA; 1165 - 1166 - /* 1167 - * Attribute: Object id (NTFS 3.0+) (0x40). 1112 + * struct object_id_attr - $OBJECT_ID attribute content (NTFS 3.0+) 1168 1113 * 1169 1114 * NOTE: Always resident. 1115 + * 1116 + * @object_id: Unique 128-bit GUID assigned to the file. 1117 + * Core identifier; always present. 1118 + * 1119 + * Optional extended info (union; total value size 16–64 bytes): 1120 + * @extended_info.birth_volume_id: 1121 + * Birth volume GUID (where file was first created). 1122 + * @extended_info.birth_object_id: 1123 + * Birth object GUID (original ID before copy/move). 1124 + * @extended_info.domain_id: 1125 + * Domain GUID (usually zero; reserved). 1170 1126 */ 1171 - typedef struct { 1172 - GUID object_id; /* Unique id assigned to the 1173 - file.*/ 1174 - /* The following fields are optional. The attribute value size is 16 1175 - bytes, i.e. sizeof(GUID), if these are not present at all. Note, 1176 - the entries can be present but one or more (or all) can be zero 1177 - meaning that that particular value(s) is(are) not defined. */ 1127 + struct object_id_attr { 1128 + struct guid object_id; 1178 1129 union { 1179 1130 struct { 1180 - GUID birth_volume_id; /* Unique id of volume on which 1181 - the file was first created.*/ 1182 - GUID birth_object_id; /* Unique id of file when it was 1183 - first created. */ 1184 - GUID domain_id; /* Reserved, zero. */ 1185 - } __attribute__ ((__packed__)) origin; 1131 + struct guid birth_volume_id; 1132 + struct guid birth_object_id; 1133 + struct guid domain_id; 1134 + } __packed; 1186 1135 u8 extended_info[48]; 1187 - } __attribute__ ((__packed__)) opt; 1188 - } __attribute__ ((__packed__)) OBJECT_ID_ATTR; 1136 + } __packed; 1137 + } __packed; 1189 1138 1190 1139 /* 1191 - * The pre-defined IDENTIFIER_AUTHORITIES used as SID_IDENTIFIER_AUTHORITY in 1192 - * the SID structure (see below). 1193 - */ 1194 - //typedef enum { /* SID string prefix. */ 1195 - // SECURITY_NULL_SID_AUTHORITY = {0, 0, 0, 0, 0, 0}, /* S-1-0 */ 1196 - // SECURITY_WORLD_SID_AUTHORITY = {0, 0, 0, 0, 0, 1}, /* S-1-1 */ 1197 - // SECURITY_LOCAL_SID_AUTHORITY = {0, 0, 0, 0, 0, 2}, /* S-1-2 */ 1198 - // SECURITY_CREATOR_SID_AUTHORITY = {0, 0, 0, 0, 0, 3}, /* S-1-3 */ 1199 - // SECURITY_NON_UNIQUE_AUTHORITY = {0, 0, 0, 0, 0, 4}, /* S-1-4 */ 1200 - // SECURITY_NT_SID_AUTHORITY = {0, 0, 0, 0, 0, 5}, /* S-1-5 */ 1201 - //} IDENTIFIER_AUTHORITIES; 1202 - 1203 - /* 1140 + * enum - RIDs (Relative Identifiers) in Windows/NTFS security 1141 + * 1204 1142 * These relative identifiers (RIDs) are used with the above identifier 1205 1143 * authorities to make up universal well-known SIDs. 1144 + * 1145 + * SECURITY_NULL_RID S-1-0 (Null authority) 1146 + * SECURITY_WORLD_RID S-1-1 (World/Everyone) 1147 + * SECURITY_LOCAL_RID S-1-2 (Local) 1148 + * SECURITY_CREATOR_OWNER_RID S-1-3-0 (Creator Owner) 1149 + * SECURITY_CREATOR_GROUP_RID S-1-3-1 (Creator Group) 1150 + * SECURITY_CREATOR_OWNER_SERVER_RID S-1-3-2 (Server Creator Owner) 1151 + * SECURITY_CREATOR_GROUP_SERVER_RID S-1-3-3 (Server Creator Group) 1152 + * SECURITY_DIALUP_RID S-1-5-1 (Dialup) 1153 + * SECURITY_NETWORK_RID S-1-5-2 (Network) 1154 + * SECURITY_BATCH_RID S-1-5-3 (Batch) 1155 + * SECURITY_INTERACTIVE_RID S-1-5-4 (Interactive) 1156 + * SECURITY_SERVICE_RID S-1-5-6 (Service) 1157 + * SECURITY_ANONYMOUS_LOGON_RID S-1-5-7 (Anonymous Logon) 1158 + * SECURITY_PROXY_RID S-1-5-8 (Proxy) 1159 + * SECURITY_ENTERPRISE_CONTROLLERS_RID S-1-5-9 (Enterprise DCs) 1160 + * SECURITY_SERVER_LOGON_RID S-1-5-9 (Server Logon alias) 1161 + * SECURITY_PRINCIPAL_SELF_RID S-1-5-10 (Self/PrincipalSelf) 1162 + * SECURITY_AUTHENTICATED_USER_RID S-1-5-11 (Authenticated Users) 1163 + * SECURITY_RESTRICTED_CODE_RID S-1-5-12 (Restricted Code) 1164 + * SECURITY_TERMINAL_SERVER_RID S-1-5-13 (Terminal Server) 1165 + * SECURITY_LOGON_IDS_RID S-1-5-5 (Logon session IDs base) 1166 + * SECURITY_LOCAL_SYSTEM_RID S-1-5-18 (Local System) 1167 + * SECURITY_NT_NON_UNIQUE S-1-5-21 (NT non-unique authority) 1168 + * SECURITY_BUILTIN_DOMAIN_RID S-1-5-32 (Built-in domain) 1169 + * 1170 + * Built-in domain relative RIDs (S-1-5-32-...): 1171 + * Users: 1172 + * DOMAIN_USER_RID_ADMIN Administrator 1173 + * DOMAIN_USER_RID_GUEST Guest 1174 + * DOMAIN_USER_RID_KRBTGT krbtgt (Kerberos ticket-granting) 1175 + * 1176 + * Groups: 1177 + * DOMAIN_GROUP_RID_ADMINS Administrators 1178 + * DOMAIN_GROUP_RID_USERS Users 1179 + * DOMAIN_GROUP_RID_GUESTS Guests 1180 + * DOMAIN_GROUP_RID_COMPUTERS Computers 1181 + * DOMAIN_GROUP_RID_CONTROLLERS Domain Controllers 1182 + * DOMAIN_GROUP_RID_CERT_ADMINS Cert Publishers 1183 + * DOMAIN_GROUP_RID_SCHEMA_ADMINS Schema Admins 1184 + * DOMAIN_GROUP_RID_ENTERPRISE_ADMINS Enterprise Admins 1185 + * DOMAIN_GROUP_RID_POLICY_ADMINS Policy Admins (if present) 1186 + * 1187 + * Aliases: 1188 + * DOMAIN_ALIAS_RID_ADMINS Administrators alias 1189 + * DOMAIN_ALIAS_RID_USERS Users alias 1190 + * DOMAIN_ALIAS_RID_GUESTS Guests alias 1191 + * DOMAIN_ALIAS_RID_POWER_USERS Power Users 1192 + * DOMAIN_ALIAS_RID_ACCOUNT_OPS Account Operators 1193 + * DOMAIN_ALIAS_RID_SYSTEM_OPS Server Operators 1194 + * DOMAIN_ALIAS_RID_PRINT_OPS Print Operators 1195 + * DOMAIN_ALIAS_RID_BACKUP_OPS Backup Operators 1196 + * DOMAIN_ALIAS_RID_REPLICATOR Replicator 1197 + * DOMAIN_ALIAS_RID_RAS_SERVERS RAS Servers 1198 + * DOMAIN_ALIAS_RID_PREW2KCOMPACCESS Pre-Windows 2000 Compatible Access 1206 1199 * 1207 1200 * Note: The relative identifier (RID) refers to the portion of a SID, which 1208 1201 * identifies a user or group in relation to the authority that issued the SID. ··· 1238 1175 * made up of the identifier authority SECURITY_CREATOR_SID_AUTHORITY (3) and 1239 1176 * the relative identifier SECURITY_CREATOR_OWNER_RID (0). 1240 1177 */ 1241 - typedef enum { /* Identifier authority. */ 1242 - SECURITY_NULL_RID = 0, /* S-1-0 */ 1243 - SECURITY_WORLD_RID = 0, /* S-1-1 */ 1244 - SECURITY_LOCAL_RID = 0, /* S-1-2 */ 1178 + enum { /* Identifier authority. */ 1179 + SECURITY_NULL_RID = 0, /* S-1-0 */ 1180 + SECURITY_WORLD_RID = 0, /* S-1-1 */ 1181 + SECURITY_LOCAL_RID = 0, /* S-1-2 */ 1245 1182 1246 - SECURITY_CREATOR_OWNER_RID = 0, /* S-1-3 */ 1247 - SECURITY_CREATOR_GROUP_RID = 1, /* S-1-3 */ 1183 + SECURITY_CREATOR_OWNER_RID = 0, /* S-1-3 */ 1184 + SECURITY_CREATOR_GROUP_RID = 1, /* S-1-3 */ 1248 1185 1249 - SECURITY_CREATOR_OWNER_SERVER_RID = 2, /* S-1-3 */ 1250 - SECURITY_CREATOR_GROUP_SERVER_RID = 3, /* S-1-3 */ 1186 + SECURITY_CREATOR_OWNER_SERVER_RID = 2, /* S-1-3 */ 1187 + SECURITY_CREATOR_GROUP_SERVER_RID = 3, /* S-1-3 */ 1251 1188 1252 - SECURITY_DIALUP_RID = 1, 1253 - SECURITY_NETWORK_RID = 2, 1254 - SECURITY_BATCH_RID = 3, 1255 - SECURITY_INTERACTIVE_RID = 4, 1256 - SECURITY_SERVICE_RID = 6, 1257 - SECURITY_ANONYMOUS_LOGON_RID = 7, 1258 - SECURITY_PROXY_RID = 8, 1259 - SECURITY_ENTERPRISE_CONTROLLERS_RID=9, 1260 - SECURITY_SERVER_LOGON_RID = 9, 1261 - SECURITY_PRINCIPAL_SELF_RID = 0xa, 1262 - SECURITY_AUTHENTICATED_USER_RID = 0xb, 1263 - SECURITY_RESTRICTED_CODE_RID = 0xc, 1264 - SECURITY_TERMINAL_SERVER_RID = 0xd, 1189 + SECURITY_DIALUP_RID = 1, 1190 + SECURITY_NETWORK_RID = 2, 1191 + SECURITY_BATCH_RID = 3, 1192 + SECURITY_INTERACTIVE_RID = 4, 1193 + SECURITY_SERVICE_RID = 6, 1194 + SECURITY_ANONYMOUS_LOGON_RID = 7, 1195 + SECURITY_PROXY_RID = 8, 1196 + SECURITY_ENTERPRISE_CONTROLLERS_RID = 9, 1197 + SECURITY_SERVER_LOGON_RID = 9, 1198 + SECURITY_PRINCIPAL_SELF_RID = 0xa, 1199 + SECURITY_AUTHENTICATED_USER_RID = 0xb, 1200 + SECURITY_RESTRICTED_CODE_RID = 0xc, 1201 + SECURITY_TERMINAL_SERVER_RID = 0xd, 1265 1202 1266 - SECURITY_LOGON_IDS_RID = 5, 1267 - SECURITY_LOGON_IDS_RID_COUNT = 3, 1203 + SECURITY_LOGON_IDS_RID = 5, 1204 + SECURITY_LOGON_IDS_RID_COUNT = 3, 1268 1205 1269 - SECURITY_LOCAL_SYSTEM_RID = 0x12, 1206 + SECURITY_LOCAL_SYSTEM_RID = 0x12, 1270 1207 1271 - SECURITY_NT_NON_UNIQUE = 0x15, 1208 + SECURITY_NT_NON_UNIQUE = 0x15, 1272 1209 1273 - SECURITY_BUILTIN_DOMAIN_RID = 0x20, 1210 + SECURITY_BUILTIN_DOMAIN_RID = 0x20, 1274 1211 1275 1212 /* 1276 1213 * Well-known domain relative sub-authority values (RIDs). 1277 1214 */ 1278 1215 1279 1216 /* Users. */ 1280 - DOMAIN_USER_RID_ADMIN = 0x1f4, 1281 - DOMAIN_USER_RID_GUEST = 0x1f5, 1282 - DOMAIN_USER_RID_KRBTGT = 0x1f6, 1217 + DOMAIN_USER_RID_ADMIN = 0x1f4, 1218 + DOMAIN_USER_RID_GUEST = 0x1f5, 1219 + DOMAIN_USER_RID_KRBTGT = 0x1f6, 1283 1220 1284 1221 /* Groups. */ 1285 - DOMAIN_GROUP_RID_ADMINS = 0x200, 1286 - DOMAIN_GROUP_RID_USERS = 0x201, 1287 - DOMAIN_GROUP_RID_GUESTS = 0x202, 1288 - DOMAIN_GROUP_RID_COMPUTERS = 0x203, 1289 - DOMAIN_GROUP_RID_CONTROLLERS = 0x204, 1290 - DOMAIN_GROUP_RID_CERT_ADMINS = 0x205, 1291 - DOMAIN_GROUP_RID_SCHEMA_ADMINS = 0x206, 1292 - DOMAIN_GROUP_RID_ENTERPRISE_ADMINS= 0x207, 1293 - DOMAIN_GROUP_RID_POLICY_ADMINS = 0x208, 1222 + DOMAIN_GROUP_RID_ADMINS = 0x200, 1223 + DOMAIN_GROUP_RID_USERS = 0x201, 1224 + DOMAIN_GROUP_RID_GUESTS = 0x202, 1225 + DOMAIN_GROUP_RID_COMPUTERS = 0x203, 1226 + DOMAIN_GROUP_RID_CONTROLLERS = 0x204, 1227 + DOMAIN_GROUP_RID_CERT_ADMINS = 0x205, 1228 + DOMAIN_GROUP_RID_SCHEMA_ADMINS = 0x206, 1229 + DOMAIN_GROUP_RID_ENTERPRISE_ADMINS = 0x207, 1230 + DOMAIN_GROUP_RID_POLICY_ADMINS = 0x208, 1294 1231 1295 1232 /* Aliases. */ 1296 - DOMAIN_ALIAS_RID_ADMINS = 0x220, 1297 - DOMAIN_ALIAS_RID_USERS = 0x221, 1298 - DOMAIN_ALIAS_RID_GUESTS = 0x222, 1299 - DOMAIN_ALIAS_RID_POWER_USERS = 0x223, 1233 + DOMAIN_ALIAS_RID_ADMINS = 0x220, 1234 + DOMAIN_ALIAS_RID_USERS = 0x221, 1235 + DOMAIN_ALIAS_RID_GUESTS = 0x222, 1236 + DOMAIN_ALIAS_RID_POWER_USERS = 0x223, 1300 1237 1301 - DOMAIN_ALIAS_RID_ACCOUNT_OPS = 0x224, 1302 - DOMAIN_ALIAS_RID_SYSTEM_OPS = 0x225, 1303 - DOMAIN_ALIAS_RID_PRINT_OPS = 0x226, 1304 - DOMAIN_ALIAS_RID_BACKUP_OPS = 0x227, 1238 + DOMAIN_ALIAS_RID_ACCOUNT_OPS = 0x224, 1239 + DOMAIN_ALIAS_RID_SYSTEM_OPS = 0x225, 1240 + DOMAIN_ALIAS_RID_PRINT_OPS = 0x226, 1241 + DOMAIN_ALIAS_RID_BACKUP_OPS = 0x227, 1305 1242 1306 - DOMAIN_ALIAS_RID_REPLICATOR = 0x228, 1307 - DOMAIN_ALIAS_RID_RAS_SERVERS = 0x229, 1308 - DOMAIN_ALIAS_RID_PREW2KCOMPACCESS = 0x22a, 1309 - } RELATIVE_IDENTIFIERS; 1243 + DOMAIN_ALIAS_RID_REPLICATOR = 0x228, 1244 + DOMAIN_ALIAS_RID_RAS_SERVERS = 0x229, 1245 + DOMAIN_ALIAS_RID_PREW2KCOMPACCESS = 0x22a, 1246 + }; 1310 1247 1311 1248 /* 1312 1249 * The universal well-known SIDs: ··· 1346 1283 */ 1347 1284 1348 1285 /* 1349 - * The SID_IDENTIFIER_AUTHORITY is a 48-bit value used in the SID structure. 1286 + * struct ntfs_sid - Security Identifier (SID) structure 1350 1287 * 1351 - * NOTE: This is stored as a big endian number, hence the high_part comes 1352 - * before the low_part. 1353 - */ 1354 - typedef union { 1355 - struct { 1356 - u16 high_part; /* High 16-bits. */ 1357 - u32 low_part; /* Low 32-bits. */ 1358 - } __attribute__ ((__packed__)) parts; 1359 - u8 value[6]; /* Value as individual bytes. */ 1360 - } __attribute__ ((__packed__)) SID_IDENTIFIER_AUTHORITY; 1361 - 1362 - /* 1288 + * @revision: SID revision level (usually 1). 1289 + * @sub_authority_count: Number of sub-authorities (1 or more). 1290 + * @identifier_authority: 1291 + * 48-bit identifier authority (S-1-x-...). 1292 + * @parts.high_part: high 16 bits. 1293 + * @parts.low_part: low 32 bits. 1294 + * @value: raw 6-byte array. 1295 + * @sub_authority: Variable array of 32-bit RIDs. 1296 + * At least one; defines the SID relative to authority. 1297 + * 1363 1298 * The SID structure is a variable-length structure used to uniquely identify 1364 1299 * users or groups. SID stands for security identifier. 1365 1300 * ··· 1381 1320 * sub_authority[0] = 32, // SECURITY_BUILTIN_DOMAIN_RID 1382 1321 * sub_authority[1] = 544 // DOMAIN_ALIAS_RID_ADMINS 1383 1322 */ 1384 - typedef struct { 1323 + struct ntfs_sid { 1385 1324 u8 revision; 1386 1325 u8 sub_authority_count; 1387 - SID_IDENTIFIER_AUTHORITY identifier_authority; 1388 - le32 sub_authority[1]; /* At least one sub_authority. */ 1389 - } __attribute__ ((__packed__)) SID; 1326 + union { 1327 + struct { 1328 + u16 high_part; 1329 + u32 low_part; 1330 + } __packed parts; 1331 + u8 value[6]; 1332 + } identifier_authority; 1333 + __le32 sub_authority[]; 1334 + } __packed; 1390 1335 1391 1336 /* 1392 - * Current constants for SIDs. 1393 - */ 1394 - typedef enum { 1395 - SID_REVISION = 1, /* Current revision level. */ 1396 - SID_MAX_SUB_AUTHORITIES = 15, /* Maximum number of those. */ 1397 - SID_RECOMMENDED_SUB_AUTHORITIES = 1, /* Will change to around 6 in 1398 - a future revision. */ 1399 - } SID_CONSTANTS; 1400 - 1401 - /* 1402 - * The predefined ACE types (8-bit, see below). 1337 + * enum - Predefined ACE types (8-bit) for NTFS security descriptors 1338 + * 1339 + * ACCESS_MIN_MS_ACE_TYPE: Minimum MS ACE type (0). 1340 + * ACCESS_ALLOWED_ACE_TYPE: Allow access (standard ACE). 1341 + * ACCESS_DENIED_ACE_TYPE: Deny access (standard ACE). 1342 + * SYSTEM_AUDIT_ACE_TYPE: Audit successful/failed access. 1343 + * SYSTEM_ALARM_ACE_TYPE: Alarm on access (not in Win2k+). 1344 + * ACCESS_MAX_MS_V2_ACE_TYPE: Max for V2 ACE types. 1345 + * ACCESS_ALLOWED_COMPOUND_ACE_TYPE: 1346 + * Compound ACE (legacy). 1347 + * ACCESS_MAX_MS_V3_ACE_TYPE: Max for V3 ACE types. 1348 + * ACCESS_MIN_MS_OBJECT_ACE_TYPE: Min for object ACE types (Win2k+). 1349 + * ACCESS_ALLOWED_OBJECT_ACE_TYPE: Allow with object-specific rights. 1350 + * ACCESS_DENIED_OBJECT_ACE_TYPE: Deny with object-specific rights. 1351 + * SYSTEM_AUDIT_OBJECT_ACE_TYPE: Audit with object-specific rights. 1352 + * SYSTEM_ALARM_OBJECT_ACE_TYPE: Alarm with object-specific rights. 1353 + * ACCESS_MAX_MS_OBJECT_ACE_TYPE: Max for object ACE types. 1354 + * ACCESS_MAX_MS_V4_ACE_TYPE: Max for V4 ACE types. 1355 + * ACCESS_MAX_MS_ACE_TYPE: Overall max ACE type (WinNT/2k). 1403 1356 */ 1404 1357 enum { 1405 - ACCESS_MIN_MS_ACE_TYPE = 0, 1406 - ACCESS_ALLOWED_ACE_TYPE = 0, 1407 - ACCESS_DENIED_ACE_TYPE = 1, 1408 - SYSTEM_AUDIT_ACE_TYPE = 2, 1409 - SYSTEM_ALARM_ACE_TYPE = 3, /* Not implemented as of Win2k. */ 1410 - ACCESS_MAX_MS_V2_ACE_TYPE = 3, 1358 + ACCESS_MIN_MS_ACE_TYPE = 0, 1359 + ACCESS_ALLOWED_ACE_TYPE = 0, 1360 + ACCESS_DENIED_ACE_TYPE = 1, 1361 + SYSTEM_AUDIT_ACE_TYPE = 2, 1362 + SYSTEM_ALARM_ACE_TYPE = 3, 1363 + ACCESS_MAX_MS_V2_ACE_TYPE = 3, 1411 1364 1412 - ACCESS_ALLOWED_COMPOUND_ACE_TYPE= 4, 1413 - ACCESS_MAX_MS_V3_ACE_TYPE = 4, 1365 + ACCESS_ALLOWED_COMPOUND_ACE_TYPE = 4, 1366 + ACCESS_MAX_MS_V3_ACE_TYPE = 4, 1367 + ACCESS_MIN_MS_OBJECT_ACE_TYPE = 5, 1368 + ACCESS_ALLOWED_OBJECT_ACE_TYPE = 5, 1369 + ACCESS_DENIED_OBJECT_ACE_TYPE = 6, 1370 + SYSTEM_AUDIT_OBJECT_ACE_TYPE = 7, 1371 + SYSTEM_ALARM_OBJECT_ACE_TYPE = 8, 1372 + ACCESS_MAX_MS_OBJECT_ACE_TYPE = 8, 1414 1373 1415 - /* The following are Win2k only. */ 1416 - ACCESS_MIN_MS_OBJECT_ACE_TYPE = 5, 1417 - ACCESS_ALLOWED_OBJECT_ACE_TYPE = 5, 1418 - ACCESS_DENIED_OBJECT_ACE_TYPE = 6, 1419 - SYSTEM_AUDIT_OBJECT_ACE_TYPE = 7, 1420 - SYSTEM_ALARM_OBJECT_ACE_TYPE = 8, 1421 - ACCESS_MAX_MS_OBJECT_ACE_TYPE = 8, 1422 - 1423 - ACCESS_MAX_MS_V4_ACE_TYPE = 8, 1424 - 1425 - /* This one is for WinNT/2k. */ 1426 - ACCESS_MAX_MS_ACE_TYPE = 8, 1427 - } __attribute__ ((__packed__)); 1428 - 1429 - typedef u8 ACE_TYPES; 1374 + ACCESS_MAX_MS_V4_ACE_TYPE = 8, 1375 + ACCESS_MAX_MS_ACE_TYPE = 8, 1376 + } __packed; 1430 1377 1431 1378 /* 1432 - * The ACE flags (8-bit) for audit and inheritance (see below). 1379 + * enum - ACE inheritance and audit flags (8-bit) 1380 + * 1381 + * OBJECT_INHERIT_ACE: Object inherit (files inherit this ACE). 1382 + * CONTAINER_INHERIT_ACE: Container inherit (subdirectories inherit). 1383 + * NO_PROPAGATE_INHERIT_ACE: No propagation (stop inheritance after this level). 1384 + * INHERIT_ONLY_ACE: Inherit only (not applied to current object). 1385 + * INHERITED_ACE: ACE was inherited (Win2k+ only). 1386 + * VALID_INHERIT_FLAGS: Mask of all valid inheritance flags (0x1f). 1387 + * SUCCESSFUL_ACCESS_ACE_FLAG: Audit successful access (system audit ACE). 1388 + * FAILED_ACCESS_ACE_FLAG: Audit failed access (system audit ACE). 1433 1389 * 1434 1390 * SUCCESSFUL_ACCESS_ACE_FLAG is only used with system audit and alarm ACE 1435 1391 * types to indicate that a message is generated (in Windows!) for successful ··· 1456 1378 * to indicate that a message is generated (in Windows!) for failed accesses. 1457 1379 */ 1458 1380 enum { 1459 - /* The inheritance flags. */ 1460 1381 OBJECT_INHERIT_ACE = 0x01, 1461 1382 CONTAINER_INHERIT_ACE = 0x02, 1462 1383 NO_PROPAGATE_INHERIT_ACE = 0x04, 1463 1384 INHERIT_ONLY_ACE = 0x08, 1464 - INHERITED_ACE = 0x10, /* Win2k only. */ 1385 + INHERITED_ACE = 0x10, 1465 1386 VALID_INHERIT_FLAGS = 0x1f, 1466 - 1467 - /* The audit flags. */ 1468 1387 SUCCESSFUL_ACCESS_ACE_FLAG = 0x40, 1469 1388 FAILED_ACCESS_ACE_FLAG = 0x80, 1470 - } __attribute__ ((__packed__)); 1471 - 1472 - typedef u8 ACE_FLAGS; 1389 + } __packed; 1473 1390 1474 1391 /* 1475 - * An ACE is an access-control entry in an access-control list (ACL). 1476 - * An ACE defines access to an object for a specific user or group or defines 1477 - * the types of access that generate system-administration messages or alarms 1478 - * for a specific user or group. The user or group is identified by a security 1479 - * identifier (SID). 1392 + * enum - NTFS access rights masks (32-bit) 1480 1393 * 1481 - * Each ACE starts with an ACE_HEADER structure (aligned on 4-byte boundary), 1482 - * which specifies the type and size of the ACE. The format of the subsequent 1483 - * data depends on the ACE type. 1484 - */ 1485 - typedef struct { 1486 - /*Ofs*/ 1487 - /* 0*/ ACE_TYPES type; /* Type of the ACE. */ 1488 - /* 1*/ ACE_FLAGS flags; /* Flags describing the ACE. */ 1489 - /* 2*/ le16 size; /* Size in bytes of the ACE. */ 1490 - } __attribute__ ((__packed__)) ACE_HEADER; 1491 - 1492 - /* 1493 - * The access mask (32-bit). Defines the access rights. 1394 + * FILE_READ_DATA / FILE_LIST_DIRECTORY: Read file data / list dir contents. 1395 + * FILE_WRITE_DATA / FILE_ADD_FILE: Write file data / create file in dir. 1396 + * FILE_APPEND_DATA / FILE_ADD_SUBDIRECTORY: Append data / create subdir. 1397 + * FILE_READ_EA: Read extended attributes. 1398 + * FILE_WRITE_EA: Write extended attributes. 1399 + * FILE_EXECUTE / FILE_TRAVERSE: Execute file / traverse dir. 1400 + * FILE_DELETE_CHILD: Delete children in dir. 1401 + * FILE_READ_ATTRIBUTES: Read attributes. 1402 + * FILE_WRITE_ATTRIBUTES: Write attributes. 1403 + * 1404 + * Standard rights (object-independent): 1405 + * DELETE: Delete object. 1406 + * READ_CONTROL: Read security descriptor/owner. 1407 + * WRITE_DAC: Modify DACL. 1408 + * WRITE_OWNER: Change owner. 1409 + * SYNCHRONIZE: Wait on object signal state. 1410 + * 1411 + * Combinations: 1412 + * STANDARD_RIGHTS_READ / WRITE / EXECUTE: Aliases for READ_CONTROL. 1413 + * STANDARD_RIGHTS_REQUIRED: DELETE + READ_CONTROL + 1414 + * WRITE_DAC + WRITE_OWNER. 1415 + * STANDARD_RIGHTS_ALL: Above + SYNCHRONIZE. 1416 + * 1417 + * System/access types: 1418 + * ACCESS_SYSTEM_SECURITY: Access system ACL. 1419 + * MAXIMUM_ALLOWED: Maximum allowed access. 1420 + * 1421 + * Generic rights (high bits, map to specific/standard): 1422 + * GENERIC_ALL: Full access. 1423 + * GENERIC_EXECUTE: Execute/traverse. 1424 + * GENERIC_WRITE: Write (append, attrs, data, EA, etc.). 1425 + * GENERIC_READ: Read (attrs, data, EA, etc.). 1494 1426 * 1495 1427 * The specific rights (bits 0 to 15). These depend on the type of the object 1496 1428 * being secured by the ACE. 1497 1429 */ 1498 1430 enum { 1499 - /* Specific rights for files and directories are as follows: */ 1500 - 1501 - /* Right to read data from the file. (FILE) */ 1502 1431 FILE_READ_DATA = cpu_to_le32(0x00000001), 1503 - /* Right to list contents of a directory. (DIRECTORY) */ 1504 1432 FILE_LIST_DIRECTORY = cpu_to_le32(0x00000001), 1505 - 1506 - /* Right to write data to the file. (FILE) */ 1507 1433 FILE_WRITE_DATA = cpu_to_le32(0x00000002), 1508 - /* Right to create a file in the directory. (DIRECTORY) */ 1509 1434 FILE_ADD_FILE = cpu_to_le32(0x00000002), 1510 - 1511 - /* Right to append data to the file. (FILE) */ 1512 1435 FILE_APPEND_DATA = cpu_to_le32(0x00000004), 1513 - /* Right to create a subdirectory. (DIRECTORY) */ 1514 1436 FILE_ADD_SUBDIRECTORY = cpu_to_le32(0x00000004), 1515 - 1516 - /* Right to read extended attributes. (FILE/DIRECTORY) */ 1517 1437 FILE_READ_EA = cpu_to_le32(0x00000008), 1518 - 1519 - /* Right to write extended attributes. (FILE/DIRECTORY) */ 1520 1438 FILE_WRITE_EA = cpu_to_le32(0x00000010), 1521 - 1522 - /* Right to execute a file. (FILE) */ 1523 1439 FILE_EXECUTE = cpu_to_le32(0x00000020), 1524 - /* Right to traverse the directory. (DIRECTORY) */ 1525 1440 FILE_TRAVERSE = cpu_to_le32(0x00000020), 1526 - 1527 - /* 1528 - * Right to delete a directory and all the files it contains (its 1529 - * children), even if the files are read-only. (DIRECTORY) 1530 - */ 1531 1441 FILE_DELETE_CHILD = cpu_to_le32(0x00000040), 1532 - 1533 - /* Right to read file attributes. (FILE/DIRECTORY) */ 1534 1442 FILE_READ_ATTRIBUTES = cpu_to_le32(0x00000080), 1535 - 1536 - /* Right to change file attributes. (FILE/DIRECTORY) */ 1537 1443 FILE_WRITE_ATTRIBUTES = cpu_to_le32(0x00000100), 1538 - 1539 - /* 1540 - * The standard rights (bits 16 to 23). These are independent of the 1541 - * type of object being secured. 1542 - */ 1543 - 1544 - /* Right to delete the object. */ 1545 1444 DELETE = cpu_to_le32(0x00010000), 1546 - 1547 - /* 1548 - * Right to read the information in the object's security descriptor, 1549 - * not including the information in the SACL, i.e. right to read the 1550 - * security descriptor and owner. 1551 - */ 1552 1445 READ_CONTROL = cpu_to_le32(0x00020000), 1553 - 1554 - /* Right to modify the DACL in the object's security descriptor. */ 1555 1446 WRITE_DAC = cpu_to_le32(0x00040000), 1556 - 1557 - /* Right to change the owner in the object's security descriptor. */ 1558 1447 WRITE_OWNER = cpu_to_le32(0x00080000), 1559 - 1560 - /* 1561 - * Right to use the object for synchronization. Enables a process to 1562 - * wait until the object is in the signalled state. Some object types 1563 - * do not support this access right. 1564 - */ 1565 1448 SYNCHRONIZE = cpu_to_le32(0x00100000), 1566 - 1567 - /* 1568 - * The following STANDARD_RIGHTS_* are combinations of the above for 1569 - * convenience and are defined by the Win32 API. 1570 - */ 1571 - 1572 - /* These are currently defined to READ_CONTROL. */ 1573 1449 STANDARD_RIGHTS_READ = cpu_to_le32(0x00020000), 1574 1450 STANDARD_RIGHTS_WRITE = cpu_to_le32(0x00020000), 1575 1451 STANDARD_RIGHTS_EXECUTE = cpu_to_le32(0x00020000), 1576 - 1577 - /* Combines DELETE, READ_CONTROL, WRITE_DAC, and WRITE_OWNER access. */ 1578 1452 STANDARD_RIGHTS_REQUIRED = cpu_to_le32(0x000f0000), 1579 - 1580 - /* 1581 - * Combines DELETE, READ_CONTROL, WRITE_DAC, WRITE_OWNER, and 1582 - * SYNCHRONIZE access. 1583 - */ 1584 1453 STANDARD_RIGHTS_ALL = cpu_to_le32(0x001f0000), 1585 - 1586 - /* 1587 - * The access system ACL and maximum allowed access types (bits 24 to 1588 - * 25, bits 26 to 27 are reserved). 1589 - */ 1590 1454 ACCESS_SYSTEM_SECURITY = cpu_to_le32(0x01000000), 1591 1455 MAXIMUM_ALLOWED = cpu_to_le32(0x02000000), 1592 - 1593 - /* 1594 - * The generic rights (bits 28 to 31). These map onto the standard and 1595 - * specific rights. 1596 - */ 1597 - 1598 - /* Read, write, and execute access. */ 1599 1456 GENERIC_ALL = cpu_to_le32(0x10000000), 1600 - 1601 - /* Execute access. */ 1602 1457 GENERIC_EXECUTE = cpu_to_le32(0x20000000), 1603 - 1604 - /* 1605 - * Write access. For files, this maps onto: 1606 - * FILE_APPEND_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_DATA | 1607 - * FILE_WRITE_EA | STANDARD_RIGHTS_WRITE | SYNCHRONIZE 1608 - * For directories, the mapping has the same numerical value. See 1609 - * above for the descriptions of the rights granted. 1610 - */ 1611 1458 GENERIC_WRITE = cpu_to_le32(0x40000000), 1612 - 1613 - /* 1614 - * Read access. For files, this maps onto: 1615 - * FILE_READ_ATTRIBUTES | FILE_READ_DATA | FILE_READ_EA | 1616 - * STANDARD_RIGHTS_READ | SYNCHRONIZE 1617 - * For directories, the mapping has the same numberical value. See 1618 - * above for the descriptions of the rights granted. 1619 - */ 1620 1459 GENERIC_READ = cpu_to_le32(0x80000000), 1621 1460 }; 1622 1461 1623 - typedef le32 ACCESS_MASK; 1624 - 1625 1462 /* 1626 - * The generic mapping array. Used to denote the mapping of each generic 1627 - * access right to a specific access mask. 1463 + * struct ntfs_ace - Access Control Entry (ACE) structure 1628 1464 * 1629 - * FIXME: What exactly is this and what is it for? (AIA) 1465 + * @type: ACE type (ACCESS_ALLOWED_ACE_TYPE, ACCESS_DENIED_ACE_TYPE, etc.). 1466 + * @flags: Inheritance and audit flags (OBJECT_INHERIT_ACE, etc.). 1467 + * @size: Total byte size of this ACE (header + SID + variable data). 1468 + * @mask: Access rights mask (FILE_READ_DATA, DELETE, GENERIC_ALL, etc.). 1469 + * @sid: Security Identifier (SID) this ACE applies to. 1630 1470 */ 1631 - typedef struct { 1632 - ACCESS_MASK generic_read; 1633 - ACCESS_MASK generic_write; 1634 - ACCESS_MASK generic_execute; 1635 - ACCESS_MASK generic_all; 1636 - } __attribute__ ((__packed__)) GENERIC_MAPPING; 1637 - 1638 - /* 1639 - * The predefined ACE type structures are as defined below. 1640 - */ 1641 - 1642 - /* 1643 - * ACCESS_ALLOWED_ACE, ACCESS_DENIED_ACE, SYSTEM_AUDIT_ACE, SYSTEM_ALARM_ACE 1644 - */ 1645 - typedef struct { 1646 - /* 0 ACE_HEADER; -- Unfolded here as gcc doesn't like unnamed structs. */ 1647 - ACE_TYPES type; /* Type of the ACE. */ 1648 - ACE_FLAGS flags; /* Flags describing the ACE. */ 1649 - le16 size; /* Size in bytes of the ACE. */ 1650 - /* 4*/ ACCESS_MASK mask; /* Access mask associated with the ACE. */ 1651 - 1652 - /* 8*/ SID sid; /* The SID associated with the ACE. */ 1653 - } __attribute__ ((__packed__)) ACCESS_ALLOWED_ACE, ACCESS_DENIED_ACE, 1654 - SYSTEM_AUDIT_ACE, SYSTEM_ALARM_ACE; 1471 + struct ntfs_ace { 1472 + u8 type; 1473 + u8 flags; 1474 + __le16 size; 1475 + __le32 mask; 1476 + struct ntfs_sid sid; 1477 + } __packed; 1655 1478 1656 1479 /* 1657 1480 * The object ACE flags (32-bit). ··· 1562 1583 ACE_INHERITED_OBJECT_TYPE_PRESENT = cpu_to_le32(2), 1563 1584 }; 1564 1585 1565 - typedef le32 OBJECT_ACE_FLAGS; 1566 - 1567 - typedef struct { 1568 - /* 0 ACE_HEADER; -- Unfolded here as gcc doesn't like unnamed structs. */ 1569 - ACE_TYPES type; /* Type of the ACE. */ 1570 - ACE_FLAGS flags; /* Flags describing the ACE. */ 1571 - le16 size; /* Size in bytes of the ACE. */ 1572 - /* 4*/ ACCESS_MASK mask; /* Access mask associated with the ACE. */ 1573 - 1574 - /* 8*/ OBJECT_ACE_FLAGS object_flags; /* Flags describing the object ACE. */ 1575 - /* 12*/ GUID object_type; 1576 - /* 28*/ GUID inherited_object_type; 1577 - 1578 - /* 44*/ SID sid; /* The SID associated with the ACE. */ 1579 - } __attribute__ ((__packed__)) ACCESS_ALLOWED_OBJECT_ACE, 1580 - ACCESS_DENIED_OBJECT_ACE, 1581 - SYSTEM_AUDIT_OBJECT_ACE, 1582 - SYSTEM_ALARM_OBJECT_ACE; 1583 - 1584 1586 /* 1587 + * struct ntfs_acl - NTFS Access Control List (ACL) header 1588 + * 1585 1589 * An ACL is an access-control list (ACL). 1586 1590 * An ACL starts with an ACL header structure, which specifies the size of 1587 1591 * the ACL and the number of ACEs it contains. The ACL header is followed by 1588 1592 * zero or more access control entries (ACEs). The ACL as well as each ACE 1589 1593 * are aligned on 4-byte boundaries. 1594 + * 1595 + * @revision: ACL revision level (usually 2 or 4). 1596 + * @alignment1: Padding/alignment byte (zero). 1597 + * @size: Total allocated size in bytes (header + all ACEs + 1598 + * free space). 1599 + * @ace_count: Number of ACE entries following the header. 1600 + * @alignment2: Padding/alignment (zero). 1590 1601 */ 1591 - typedef struct { 1592 - u8 revision; /* Revision of this ACL. */ 1602 + struct ntfs_acl { 1603 + u8 revision; 1593 1604 u8 alignment1; 1594 - le16 size; /* Allocated space in bytes for ACL. Includes this 1595 - header, the ACEs and the remaining free space. */ 1596 - le16 ace_count; /* Number of ACEs in the ACL. */ 1597 - le16 alignment2; 1598 - /* sizeof() = 8 bytes */ 1599 - } __attribute__ ((__packed__)) ACL; 1605 + __le16 size; 1606 + __le16 ace_count; 1607 + __le16 alignment2; 1608 + } __packed; 1600 1609 1601 - /* 1602 - * Current constants for ACLs. 1603 - */ 1604 - typedef enum { 1605 - /* Current revision. */ 1606 - ACL_REVISION = 2, 1607 - ACL_REVISION_DS = 4, 1608 - 1609 - /* History of revisions. */ 1610 - ACL_REVISION1 = 1, 1611 - MIN_ACL_REVISION = 2, 1612 - ACL_REVISION2 = 2, 1613 - ACL_REVISION3 = 3, 1614 - ACL_REVISION4 = 4, 1615 - MAX_ACL_REVISION = 4, 1616 - } ACL_CONSTANTS; 1610 + static_assert(sizeof(struct ntfs_acl) == 8); 1617 1611 1618 1612 /* 1619 1613 * The security descriptor control flags (16-bit). ··· 1650 1698 SE_SACL_PROTECTED = cpu_to_le16(0x2000), 1651 1699 SE_RM_CONTROL_VALID = cpu_to_le16(0x4000), 1652 1700 SE_SELF_RELATIVE = cpu_to_le16(0x8000) 1653 - } __attribute__ ((__packed__)); 1654 - 1655 - typedef le16 SECURITY_DESCRIPTOR_CONTROL; 1701 + } __packed; 1656 1702 1657 1703 /* 1704 + * struct security_descriptor_relative - Relative security descriptor 1705 + * 1658 1706 * Self-relative security descriptor. Contains the owner and group SIDs as well 1659 1707 * as the sacl and dacl ACLs inside the security descriptor itself. 1660 - */ 1661 - typedef struct { 1662 - u8 revision; /* Revision level of the security descriptor. */ 1663 - u8 alignment; 1664 - SECURITY_DESCRIPTOR_CONTROL control; /* Flags qualifying the type of 1665 - the descriptor as well as the following fields. */ 1666 - le32 owner; /* Byte offset to a SID representing an object's 1667 - owner. If this is NULL, no owner SID is present in 1668 - the descriptor. */ 1669 - le32 group; /* Byte offset to a SID representing an object's 1670 - primary group. If this is NULL, no primary group 1671 - SID is present in the descriptor. */ 1672 - le32 sacl; /* Byte offset to a system ACL. Only valid, if 1673 - SE_SACL_PRESENT is set in the control field. If 1674 - SE_SACL_PRESENT is set but sacl is NULL, a NULL ACL 1675 - is specified. */ 1676 - le32 dacl; /* Byte offset to a discretionary ACL. Only valid, if 1677 - SE_DACL_PRESENT is set in the control field. If 1678 - SE_DACL_PRESENT is set but dacl is NULL, a NULL ACL 1679 - (unconditionally granting access) is specified. */ 1680 - /* sizeof() = 0x14 bytes */ 1681 - } __attribute__ ((__packed__)) SECURITY_DESCRIPTOR_RELATIVE; 1682 - 1683 - /* 1684 - * Absolute security descriptor. Does not contain the owner and group SIDs, nor 1685 - * the sacl and dacl ACLs inside the security descriptor. Instead, it contains 1686 - * pointers to these structures in memory. Obviously, absolute security 1687 - * descriptors are only useful for in memory representations of security 1688 - * descriptors. On disk, a self-relative security descriptor is used. 1689 - */ 1690 - typedef struct { 1691 - u8 revision; /* Revision level of the security descriptor. */ 1692 - u8 alignment; 1693 - SECURITY_DESCRIPTOR_CONTROL control; /* Flags qualifying the type of 1694 - the descriptor as well as the following fields. */ 1695 - SID *owner; /* Points to a SID representing an object's owner. If 1696 - this is NULL, no owner SID is present in the 1697 - descriptor. */ 1698 - SID *group; /* Points to a SID representing an object's primary 1699 - group. If this is NULL, no primary group SID is 1700 - present in the descriptor. */ 1701 - ACL *sacl; /* Points to a system ACL. Only valid, if 1702 - SE_SACL_PRESENT is set in the control field. If 1703 - SE_SACL_PRESENT is set but sacl is NULL, a NULL ACL 1704 - is specified. */ 1705 - ACL *dacl; /* Points to a discretionary ACL. Only valid, if 1706 - SE_DACL_PRESENT is set in the control field. If 1707 - SE_DACL_PRESENT is set but dacl is NULL, a NULL ACL 1708 - (unconditionally granting access) is specified. */ 1709 - } __attribute__ ((__packed__)) SECURITY_DESCRIPTOR; 1710 - 1711 - /* 1712 - * Current constants for security descriptors. 1713 - */ 1714 - typedef enum { 1715 - /* Current revision. */ 1716 - SECURITY_DESCRIPTOR_REVISION = 1, 1717 - SECURITY_DESCRIPTOR_REVISION1 = 1, 1718 - 1719 - /* The sizes of both the absolute and relative security descriptors is 1720 - the same as pointers, at least on ia32 architecture are 32-bit. */ 1721 - SECURITY_DESCRIPTOR_MIN_LENGTH = sizeof(SECURITY_DESCRIPTOR), 1722 - } SECURITY_DESCRIPTOR_CONSTANTS; 1723 - 1724 - /* 1725 - * Attribute: Security descriptor (0x50). A standard self-relative security 1726 - * descriptor. 1727 1708 * 1728 - * NOTE: Can be resident or non-resident. 1729 - * NOTE: Not used in NTFS 3.0+, as security descriptors are stored centrally 1730 - * in FILE_Secure and the correct descriptor is found using the security_id 1731 - * from the standard information attribute. 1709 + * @revision: Security descriptor revision (usually 1). 1710 + * @alignment: Padding/alignment byte (zero). 1711 + * @control: Control flags (SE_OWNER_DEFAULTED, SE_DACL_PRESENT, 1712 + * SE_SACL_PRESENT, SE_SACL_AUTO_INHERITED, etc.). 1713 + * @owner: Byte offset to owner SID (from start of descriptor). 1714 + * 0 if no owner SID present. 1715 + * @group: Byte offset to primary group SID. 1716 + * 0 if no group SID present. 1717 + * @sacl: Byte offset to System ACL (SACL). 1718 + * Valid only if SE_SACL_PRESENT in @control. 1719 + * 0 means NULL SACL. 1720 + * @dacl: Byte offset to Discretionary ACL (DACL). 1721 + * Valid only if SE_DACL_PRESENT in @control. 1722 + * 0 means NULL DACL (full access granted). 1732 1723 */ 1733 - typedef SECURITY_DESCRIPTOR_RELATIVE SECURITY_DESCRIPTOR_ATTR; 1724 + struct security_descriptor_relative { 1725 + u8 revision; 1726 + u8 alignment; 1727 + __le16 control; 1728 + __le32 owner; 1729 + __le32 group; 1730 + __le32 sacl; 1731 + __le32 dacl; 1732 + } __packed; 1733 + 1734 + static_assert(sizeof(struct security_descriptor_relative) == 20); 1734 1735 1735 1736 /* 1736 1737 * On NTFS 3.0+, all security descriptors are stored in FILE_Secure. Only one ··· 1725 1820 */ 1726 1821 1727 1822 /* 1728 - * This header precedes each security descriptor in the $SDS data stream. 1729 - * This is also the index entry data part of both the $SII and $SDH indexes. 1730 - */ 1731 - typedef struct { 1732 - le32 hash; /* Hash of the security descriptor. */ 1733 - le32 security_id; /* The security_id assigned to the descriptor. */ 1734 - le64 offset; /* Byte offset of this entry in the $SDS stream. */ 1735 - le32 length; /* Size in bytes of this entry in $SDS stream. */ 1736 - } __attribute__ ((__packed__)) SECURITY_DESCRIPTOR_HEADER; 1737 - 1738 - /* 1739 - * The $SDS data stream contains the security descriptors, aligned on 16-byte 1740 - * boundaries, sorted by security_id in a B+ tree. Security descriptors cannot 1741 - * cross 256kib boundaries (this restriction is imposed by the Windows cache 1742 - * manager). Each security descriptor is contained in a SDS_ENTRY structure. 1743 - * Also, each security descriptor is stored twice in the $SDS stream with a 1744 - * fixed offset of 0x40000 bytes (256kib, the Windows cache manager's max size) 1745 - * between them; i.e. if a SDS_ENTRY specifies an offset of 0x51d0, then the 1746 - * first copy of the security descriptor will be at offset 0x51d0 in the 1747 - * $SDS data stream and the second copy will be at offset 0x451d0. 1748 - */ 1749 - typedef struct { 1750 - /*Ofs*/ 1751 - /* 0 SECURITY_DESCRIPTOR_HEADER; -- Unfolded here as gcc doesn't like 1752 - unnamed structs. */ 1753 - le32 hash; /* Hash of the security descriptor. */ 1754 - le32 security_id; /* The security_id assigned to the descriptor. */ 1755 - le64 offset; /* Byte offset of this entry in the $SDS stream. */ 1756 - le32 length; /* Size in bytes of this entry in $SDS stream. */ 1757 - /* 20*/ SECURITY_DESCRIPTOR_RELATIVE sid; /* The self-relative security 1758 - descriptor. */ 1759 - } __attribute__ ((__packed__)) SDS_ENTRY; 1760 - 1761 - /* 1823 + * struct sii_index_key - Key for $SII index in $Secure file 1824 + * 1762 1825 * The index entry key used in the $SII index. The collation type is 1763 1826 * COLLATION_NTOFS_ULONG. 1827 + * 1828 + * @security_id: 32-bit security identifier. 1829 + * Unique ID assigned to a security descriptor. 1764 1830 */ 1765 - typedef struct { 1766 - le32 security_id; /* The security_id assigned to the descriptor. */ 1767 - } __attribute__ ((__packed__)) SII_INDEX_KEY; 1831 + struct sii_index_key { 1832 + __le32 security_id; 1833 + } __packed; 1768 1834 1769 1835 /* 1836 + * struct sdh_index_key - Key for $SDH index in $Secure file 1837 + * 1770 1838 * The index entry key used in the $SDH index. The keys are sorted first by 1771 1839 * hash and then by security_id. The collation rule is 1772 1840 * COLLATION_NTOFS_SECURITY_HASH. 1773 - */ 1774 - typedef struct { 1775 - le32 hash; /* Hash of the security descriptor. */ 1776 - le32 security_id; /* The security_id assigned to the descriptor. */ 1777 - } __attribute__ ((__packed__)) SDH_INDEX_KEY; 1778 - 1779 - /* 1780 - * Attribute: Volume name (0x60). 1781 1841 * 1782 - * NOTE: Always resident. 1783 - * NOTE: Present only in FILE_Volume. 1842 + * @hash: 32-bit hash of the security descriptor. 1843 + * Used for quick collision checks and indexing. 1844 + * @security_id: 32-bit security identifier. 1845 + * Unique ID assigned to the descriptor. 1784 1846 */ 1785 - typedef struct { 1786 - ntfschar name[0]; /* The name of the volume in Unicode. */ 1787 - } __attribute__ ((__packed__)) VOLUME_NAME; 1847 + struct sdh_index_key { 1848 + __le32 hash; 1849 + __le32 security_id; 1850 + } __packed; 1788 1851 1789 1852 /* 1790 - * Possible flags for the volume (16-bit). 1853 + * enum - NTFS volume flags (16-bit) 1854 + * 1855 + * These flags are stored in $VolumeInformation attribute. 1856 + * They indicate volume state and required actions. 1857 + * 1858 + * VOLUME_IS_DIRTY: Volume is dirty (needs chkdsk). 1859 + * VOLUME_RESIZE_LOG_FILE: Resize LogFile on next mount. 1860 + * VOLUME_UPGRADE_ON_MOUNT: Upgrade volume on mount (old NTFS). 1861 + * VOLUME_MOUNTED_ON_NT4: Mounted on NT4 (compatibility flag). 1862 + * VOLUME_DELETE_USN_UNDERWAY: USN journal deletion in progress. 1863 + * VOLUME_REPAIR_OBJECT_ID: Repair $ObjId on next mount. 1864 + * VOLUME_CHKDSK_UNDERWAY: Chkdsk is running. 1865 + * VOLUME_MODIFIED_BY_CHKDSK: Modified by chkdsk. 1866 + * VOLUME_FLAGS_MASK: Mask of all valid flags (0xc03f). 1867 + * VOLUME_MUST_MOUNT_RO_MASK: Flags forcing read-only mount (0xc027). 1868 + * If any set, mount read-only. 1791 1869 */ 1792 1870 enum { 1793 1871 VOLUME_IS_DIRTY = cpu_to_le16(0x0001), ··· 1786 1898 1787 1899 VOLUME_FLAGS_MASK = cpu_to_le16(0xc03f), 1788 1900 1789 - /* To make our life easier when checking if we must mount read-only. */ 1790 1901 VOLUME_MUST_MOUNT_RO_MASK = cpu_to_le16(0xc027), 1791 - } __attribute__ ((__packed__)); 1792 - 1793 - typedef le16 VOLUME_FLAGS; 1902 + } __packed; 1794 1903 1795 1904 /* 1796 - * Attribute: Volume information (0x70). 1905 + * struct volume_information - $VOLUME_INFORMATION (0x70) 1906 + * 1907 + * @reserved: Reserved 64-bit field (currently unused). 1908 + * @major_ver: Major NTFS version number (e.g., 3 for NTFS 3.1). 1909 + * @minor_ver: Minor NTFS version number (e.g., 1 for NTFS 3.1). 1910 + * @flags: Volume flags (VOLUME_IS_DIRTY, VOLUME_CHKDSK_UNDERWAY, etc.). 1911 + * See volume flags enum for details. 1797 1912 * 1798 1913 * NOTE: Always resident. 1799 1914 * NOTE: Present only in FILE_Volume. 1800 1915 * NOTE: Windows 2000 uses NTFS 3.0 while Windows NT4 service pack 6a uses 1801 1916 * NTFS 1.2. I haven't personally seen other values yet. 1802 1917 */ 1803 - typedef struct { 1804 - le64 reserved; /* Not used (yet?). */ 1805 - u8 major_ver; /* Major version of the ntfs format. */ 1806 - u8 minor_ver; /* Minor version of the ntfs format. */ 1807 - VOLUME_FLAGS flags; /* Bit array of VOLUME_* flags. */ 1808 - } __attribute__ ((__packed__)) VOLUME_INFORMATION; 1918 + struct volume_information { 1919 + __le64 reserved; 1920 + u8 major_ver; 1921 + u8 minor_ver; 1922 + __le16 flags; 1923 + } __packed; 1809 1924 1810 1925 /* 1811 - * Attribute: Data attribute (0x80). 1926 + * enum - Index header flags 1812 1927 * 1813 - * NOTE: Can be resident or non-resident. 1928 + * These flags are stored in the index header (INDEX_HEADER.flags) for both 1929 + * index root ($INDEX_ROOT) and index allocation blocks ($INDEX_ALLOCATION). 1814 1930 * 1815 - * Data contents of a file (i.e. the unnamed stream) or of a named stream. 1816 - */ 1817 - typedef struct { 1818 - u8 data[0]; /* The file's data contents. */ 1819 - } __attribute__ ((__packed__)) DATA_ATTR; 1820 - 1821 - /* 1822 - * Index header flags (8-bit). 1931 + * For index root ($INDEX_ROOT attribute): 1932 + * SMALL_INDEX: Index fits entirely in root attribute (no $INDEX_ALLOCATION). 1933 + * LARGE_INDEX: Index too large for root; $INDEX_ALLOCATION present. 1934 + * 1935 + * For index blocks ($INDEX_ALLOCATION): 1936 + * LEAF_NODE: Leaf node (no child nodes; contains actual entries). 1937 + * INDEX_NODE: Internal node (indexes other nodes; contains keys/pointers). 1938 + * 1939 + * NODE_MASK: Mask to extract node type bits (0x01). 1823 1940 */ 1824 1941 enum { 1825 - /* 1826 - * When index header is in an index root attribute: 1827 - */ 1828 - SMALL_INDEX = 0, /* The index is small enough to fit inside the index 1829 - root attribute and there is no index allocation 1830 - attribute present. */ 1831 - LARGE_INDEX = 1, /* The index is too large to fit in the index root 1832 - attribute and/or an index allocation attribute is 1833 - present. */ 1834 - /* 1835 - * When index header is in an index block, i.e. is part of index 1836 - * allocation attribute: 1837 - */ 1838 - LEAF_NODE = 0, /* This is a leaf node, i.e. there are no more nodes 1839 - branching off it. */ 1840 - INDEX_NODE = 1, /* This node indexes other nodes, i.e. it is not a leaf 1841 - node. */ 1842 - NODE_MASK = 1, /* Mask for accessing the *_NODE bits. */ 1843 - } __attribute__ ((__packed__)); 1844 - 1845 - typedef u8 INDEX_HEADER_FLAGS; 1942 + SMALL_INDEX = 0, 1943 + LARGE_INDEX = 1, 1944 + LEAF_NODE = 0, 1945 + INDEX_NODE = 1, 1946 + NODE_MASK = 1, 1947 + } __packed; 1846 1948 1847 1949 /* 1950 + * struct index_header - Common header for index root and index blocks 1951 + * 1952 + * entries_offset: Byte offset to first INDEX_ENTRY (8-byte aligned). 1953 + * index_length: Bytes used by index entries (8-byte aligned). 1954 + * From entries_offset to end of used data. 1955 + * allocated_size: Total allocated bytes for this index block. 1956 + * Fixed size in index allocation; dynamic in root. 1957 + * flags: Index flags (SMALL_INDEX, LARGE_INDEX, LEAF_NODE, etc.). 1958 + * See INDEX_HEADER_FLAGS enum. 1959 + * reserved: 3 bytes reserved/padding (zero, 8-byte aligned). 1960 + * 1848 1961 * This is the header for indexes, describing the INDEX_ENTRY records, which 1849 - * follow the INDEX_HEADER. Together the index header and the index entries 1962 + * follow the index_header. Together the index header and the index entries 1850 1963 * make up a complete index. 1851 1964 * 1852 1965 * IMPORTANT NOTE: The offset, length and size structure members are counted 1853 1966 * relative to the start of the index header structure and not relative to the 1854 1967 * start of the index root or index allocation structures themselves. 1968 + * 1969 + * For the index root attribute, the above two numbers are always 1970 + * equal, as the attribute is resident and it is resized as needed. In 1971 + * the case of the index allocation attribute the attribute is not 1972 + * resident and hence the allocated_size is a fixed value and must 1973 + * equal the index_block_size specified by the INDEX_ROOT attribute 1974 + * corresponding to the INDEX_ALLOCATION attribute this INDEX_BLOCK 1975 + * belongs to. 1855 1976 */ 1856 - typedef struct { 1857 - le32 entries_offset; /* Byte offset to first INDEX_ENTRY 1858 - aligned to 8-byte boundary. */ 1859 - le32 index_length; /* Data size of the index in bytes, 1860 - i.e. bytes used from allocated 1861 - size, aligned to 8-byte boundary. */ 1862 - le32 allocated_size; /* Byte size of this index (block), 1863 - multiple of 8 bytes. */ 1864 - /* NOTE: For the index root attribute, the above two numbers are always 1865 - equal, as the attribute is resident and it is resized as needed. In 1866 - the case of the index allocation attribute the attribute is not 1867 - resident and hence the allocated_size is a fixed value and must 1868 - equal the index_block_size specified by the INDEX_ROOT attribute 1869 - corresponding to the INDEX_ALLOCATION attribute this INDEX_BLOCK 1870 - belongs to. */ 1871 - INDEX_HEADER_FLAGS flags; /* Bit field of INDEX_HEADER_FLAGS. */ 1872 - u8 reserved[3]; /* Reserved/align to 8-byte boundary. */ 1873 - } __attribute__ ((__packed__)) INDEX_HEADER; 1977 + struct index_header { 1978 + __le32 entries_offset; 1979 + __le32 index_length; 1980 + __le32 allocated_size; 1981 + u8 flags; 1982 + u8 reserved[3]; 1983 + } __packed; 1874 1984 1875 1985 /* 1876 - * Attribute: Index root (0x90). 1986 + * struct index_root - $INDEX_ROOT attribute (0x90). 1987 + * 1988 + * @type: Indexed attribute type ($FILE_NAME for dirs, 1989 + * 0 for view indexes). 1990 + * @collation_rule: Collation rule for sorting entries 1991 + * (COLLATION_FILE_NAME for $FILE_NAME). 1992 + * @index_block_size: Size of each index block in bytes 1993 + * (in $INDEX_ALLOCATION). 1994 + * @clusters_per_index_block: 1995 + * Clusters per index block (or log2(bytes) 1996 + * if < cluster). 1997 + * Power of 2; used for encoding block size. 1998 + * @reserved: 3 bytes reserved/alignment (zero). 1999 + * @index: Index header for root entries (entries follow 2000 + * immediately). 1877 2001 * 1878 2002 * NOTE: Always resident. 1879 2003 * ··· 1903 2003 * NOTE: The root directory (FILE_root) contains an entry for itself. Other 1904 2004 * directories do not contain entries for themselves, though. 1905 2005 */ 1906 - typedef struct { 1907 - ATTR_TYPE type; /* Type of the indexed attribute. Is 1908 - $FILE_NAME for directories, zero 1909 - for view indexes. No other values 1910 - allowed. */ 1911 - COLLATION_RULE collation_rule; /* Collation rule used to sort the 1912 - index entries. If type is $FILE_NAME, 1913 - this must be COLLATION_FILE_NAME. */ 1914 - le32 index_block_size; /* Size of each index block in bytes (in 1915 - the index allocation attribute). */ 1916 - u8 clusters_per_index_block; /* Cluster size of each index block (in 1917 - the index allocation attribute), when 1918 - an index block is >= than a cluster, 1919 - otherwise this will be the log of 1920 - the size (like how the encoding of 1921 - the mft record size and the index 1922 - record size found in the boot sector 1923 - work). Has to be a power of 2. */ 1924 - u8 reserved[3]; /* Reserved/align to 8-byte boundary. */ 1925 - INDEX_HEADER index; /* Index header describing the 1926 - following index entries. */ 1927 - } __attribute__ ((__packed__)) INDEX_ROOT; 2006 + struct index_root { 2007 + __le32 type; 2008 + __le32 collation_rule; 2009 + __le32 index_block_size; 2010 + u8 clusters_per_index_block; 2011 + u8 reserved[3]; 2012 + struct index_header index; 2013 + } __packed; 1928 2014 1929 2015 /* 1930 - * Attribute: Index allocation (0xa0). 2016 + * struct index_block - Index allocation (0xa0). 1931 2017 * 1932 - * NOTE: Always non-resident (doesn't make sense to be resident anyway!). 2018 + * @magic: Magic value "INDX" (see magic_INDX). 2019 + * @usa_ofs: Offset to Update Sequence Array (see ntfs_record). 2020 + * @usa_count: Number of USA entries (see ntfs_record). 2021 + * @lsn: Log sequence number of last modification. 2022 + * @index_block_vcn: VCN of this index block. 2023 + * Units: clusters if cluster_size <= index_block_size; 2024 + * sectors otherwise. 2025 + * @index: Index header describing entries in this block. 1933 2026 * 1934 - * This is an array of index blocks. Each index block starts with an 1935 - * INDEX_BLOCK structure containing an index header, followed by a sequence of 1936 - * index entries (INDEX_ENTRY structures), as described by the INDEX_HEADER. 1937 - */ 1938 - typedef struct { 1939 - /* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */ 1940 - NTFS_RECORD_TYPE magic; /* Magic is "INDX". */ 1941 - le16 usa_ofs; /* See NTFS_RECORD definition. */ 1942 - le16 usa_count; /* See NTFS_RECORD definition. */ 1943 - 1944 - /* 8*/ sle64 lsn; /* $LogFile sequence number of the last 1945 - modification of this index block. */ 1946 - /* 16*/ leVCN index_block_vcn; /* Virtual cluster number of the index block. 1947 - If the cluster_size on the volume is <= the 1948 - index_block_size of the directory, 1949 - index_block_vcn counts in units of clusters, 1950 - and in units of sectors otherwise. */ 1951 - /* 24*/ INDEX_HEADER index; /* Describes the following index entries. */ 1952 - /* sizeof()= 40 (0x28) bytes */ 1953 - /* 1954 2027 * When creating the index block, we place the update sequence array at this 1955 2028 * offset, i.e. before we start with the index entries. This also makes sense, 1956 2029 * otherwise we could run into problems with the update sequence array ··· 1931 2058 * multi sector transfer protection wouldn't work. As you can't protect data 1932 2059 * by overwriting it since you then can't get it back... 1933 2060 * When reading use the data from the ntfs record header. 2061 + * 2062 + * NOTE: Always non-resident (doesn't make sense to be resident anyway!). 2063 + * 2064 + * This is an array of index blocks. Each index block starts with an 2065 + * index_block structure containing an index header, followed by a sequence of 2066 + * index entries (INDEX_ENTRY structures), as described by the struct index_header. 1934 2067 */ 1935 - } __attribute__ ((__packed__)) INDEX_BLOCK; 2068 + struct index_block { 2069 + __le32 magic; 2070 + __le16 usa_ofs; 2071 + __le16 usa_count; 2072 + __le64 lsn; 2073 + __le64 index_block_vcn; 2074 + struct index_header index; 2075 + } __packed; 1936 2076 1937 - typedef INDEX_BLOCK INDEX_ALLOCATION; 2077 + static_assert(sizeof(struct index_block) == 40); 1938 2078 1939 2079 /* 2080 + * struct reparse_index_key - Key for $R reparse index in $Extend/$Reparse 2081 + * 2082 + * @reparse_tag: Reparse point type (including flags, REPARSE_TAG_*). 2083 + * @file_id: MFT record number of the file with $REPARSE_POINT 2084 + * attribute. 2085 + * 1940 2086 * The system file FILE_Extend/$Reparse contains an index named $R listing 1941 2087 * all reparse points on the volume. The index entry keys are as defined 1942 2088 * below. Note, that there is no index data associated with the index entries. 1943 2089 * 1944 2090 * The index entries are sorted by the index key file_id. The collation rule is 1945 - * COLLATION_NTOFS_ULONGS. FIXME: Verify whether the reparse_tag is not the 1946 - * primary key / is not a key at all. (AIA) 2091 + * COLLATION_NTOFS_ULONGS. 1947 2092 */ 1948 - typedef struct { 1949 - le32 reparse_tag; /* Reparse point type (inc. flags). */ 1950 - leMFT_REF file_id; /* Mft record of the file containing the 1951 - reparse point attribute. */ 1952 - } __attribute__ ((__packed__)) REPARSE_INDEX_KEY; 2093 + struct reparse_index_key { 2094 + __le32 reparse_tag; 2095 + __le64 file_id; 2096 + } __packed; 1953 2097 1954 2098 /* 1955 - * Quota flags (32-bit). 2099 + * enum - Quota entry flags (32-bit) in $Quota/$Q 1956 2100 * 1957 - * The user quota flags. Names explain meaning. 2101 + * These flags are stored in quota control entries ($Quota file). 2102 + * They control quota tracking, limits, and state. 2103 + * 2104 + * User quota flags (mask 0x00000007): 2105 + * @QUOTA_FLAG_DEFAULT_LIMITS: Use default limits. 2106 + * @QUOTA_FLAG_LIMIT_REACHED: Quota limit reached. 2107 + * @QUOTA_FLAG_ID_DELETED: Quota ID deleted. 2108 + * @QUOTA_FLAG_USER_MASK: Mask for user quota flags (0x00000007). 2109 + * 2110 + * Default entry flags (owner_id = QUOTA_DEFAULTS_ID): 2111 + * @QUOTA_FLAG_TRACKING_ENABLED: Quota tracking enabled. 2112 + * @QUOTA_FLAG_ENFORCEMENT_ENABLED: Quota enforcement enabled. 2113 + * @QUOTA_FLAG_TRACKING_REQUESTED: Tracking requested (pending). 2114 + * @QUOTA_FLAG_LOG_THRESHOLD: Log when threshold reached. 2115 + * @QUOTA_FLAG_LOG_LIMIT: Log when limit reached. 2116 + * @QUOTA_FLAG_OUT_OF_DATE: Quota data out of date. 2117 + * @QUOTA_FLAG_CORRUPT: Quota entry corrupt. 2118 + * @QUOTA_FLAG_PENDING_DELETES: Pending quota deletes. 2119 + * 1958 2120 */ 1959 2121 enum { 1960 2122 QUOTA_FLAG_DEFAULT_LIMITS = cpu_to_le32(0x00000001), ··· 1997 2089 QUOTA_FLAG_ID_DELETED = cpu_to_le32(0x00000004), 1998 2090 1999 2091 QUOTA_FLAG_USER_MASK = cpu_to_le32(0x00000007), 2000 - /* This is a bit mask for the user quota flags. */ 2001 - 2002 - /* 2003 - * These flags are only present in the quota defaults index entry, i.e. 2004 - * in the entry where owner_id = QUOTA_DEFAULTS_ID. 2005 - */ 2006 2092 QUOTA_FLAG_TRACKING_ENABLED = cpu_to_le32(0x00000010), 2007 2093 QUOTA_FLAG_ENFORCEMENT_ENABLED = cpu_to_le32(0x00000020), 2008 2094 QUOTA_FLAG_TRACKING_REQUESTED = cpu_to_le32(0x00000040), ··· 2008 2106 QUOTA_FLAG_PENDING_DELETES = cpu_to_le32(0x00000800), 2009 2107 }; 2010 2108 2011 - typedef le32 QUOTA_FLAGS; 2012 - 2013 2109 /* 2110 + * struct quota_control_entry - Quota entry in $Quota/$Q 2111 + * 2112 + * @version: Currently 2. 2113 + * @flags: Quota flags (QUOTA_FLAG_* bits). 2114 + * @bytes_used: Current quota usage in bytes. 2115 + * @change_time: Last modification time (NTFS timestamp). 2116 + * @threshold: Soft quota limit (-1 = unlimited). 2117 + * @limit: Hard quota limit (-1 = unlimited). 2118 + * @exceeded_time: Time soft quota has been exceeded. 2119 + * @sid: SID of user/object (zero for defaults entry). 2120 + * 2014 2121 * The system file FILE_Extend/$Quota contains two indexes $O and $Q. Quotas 2015 2122 * are on a per volume and per user basis. 2016 2123 * ··· 2040 2129 * 2041 2130 * The $Q index entry data is the quota control entry and is defined below. 2042 2131 */ 2043 - typedef struct { 2044 - le32 version; /* Currently equals 2. */ 2045 - QUOTA_FLAGS flags; /* Flags describing this quota entry. */ 2046 - le64 bytes_used; /* How many bytes of the quota are in use. */ 2047 - sle64 change_time; /* Last time this quota entry was changed. */ 2048 - sle64 threshold; /* Soft quota (-1 if not limited). */ 2049 - sle64 limit; /* Hard quota (-1 if not limited). */ 2050 - sle64 exceeded_time; /* How long the soft quota has been exceeded. */ 2051 - SID sid; /* The SID of the user/object associated with 2052 - this quota entry. Equals zero for the quota 2053 - defaults entry (and in fact on a WinXP 2054 - volume, it is not present at all). */ 2055 - } __attribute__ ((__packed__)) QUOTA_CONTROL_ENTRY; 2132 + struct quota_control_entry { 2133 + __le32 version; 2134 + __le32 flags; 2135 + __le64 bytes_used; 2136 + __le64 change_time; 2137 + __le64 threshold; 2138 + __le64 limit; 2139 + __le64 exceeded_time; 2140 + struct ntfs_sid sid; 2141 + } __packed; 2056 2142 2057 2143 /* 2058 2144 * Predefined owner_id values (32-bit). ··· 2063 2155 /* 2064 2156 * Current constants for quota control entries. 2065 2157 */ 2066 - typedef enum { 2158 + enum { 2067 2159 /* Current version. */ 2068 2160 QUOTA_VERSION = 2, 2069 - } QUOTA_CONTROL_ENTRY_CONSTANTS; 2161 + }; 2070 2162 2071 2163 /* 2072 - * Index entry flags (16-bit). 2164 + * enum - Index entry flags (16-bit) 2165 + * 2166 + * These flags are in INDEX_ENTRY.flags (after key data). 2167 + * They describe entry type and status in index blocks/root. 2168 + * 2169 + * @INDEX_ENTRY_NODE: Entry points to a sub-node (index block VCN). 2170 + * (Not a leaf entry; internal node reference.) 2171 + * i.e. a reference to an index block in form of 2172 + * a virtual cluster number 2173 + * @INDEX_ENTRY_END: Last entry in index block/root. 2174 + * Does not represent a real file; can point to sub-node. 2175 + * @INDEX_ENTRY_SPACE_FILLER: 2176 + * Dummy value to force enum to 16-bit width. 2073 2177 */ 2074 2178 enum { 2075 - INDEX_ENTRY_NODE = cpu_to_le16(1), /* This entry contains a 2076 - sub-node, i.e. a reference to an index block in form of 2077 - a virtual cluster number (see below). */ 2078 - INDEX_ENTRY_END = cpu_to_le16(2), /* This signifies the last 2079 - entry in an index block. The index entry does not 2080 - represent a file but it can point to a sub-node. */ 2081 - 2082 - INDEX_ENTRY_SPACE_FILLER = cpu_to_le16(0xffff), /* gcc: Force 2083 - enum bit width to 16-bit. */ 2084 - } __attribute__ ((__packed__)); 2085 - 2086 - typedef le16 INDEX_ENTRY_FLAGS; 2179 + INDEX_ENTRY_NODE = cpu_to_le16(1), 2180 + INDEX_ENTRY_END = cpu_to_le16(2), 2181 + INDEX_ENTRY_SPACE_FILLER = cpu_to_le16(0xffff), 2182 + } __packed; 2087 2183 2088 2184 /* 2089 - * This the index entry header (see below). 2185 + * struct index_entry_header - Common header for all NTFS index entries 2186 + * 2187 + * This is the fixed header at the start of every INDEX_ENTRY in index 2188 + * blocks or index root. It is followed by the variable key, data, and 2189 + * sub-node VCN. 2190 + * 2191 + * Union @data: 2192 + * - When INDEX_ENTRY_END is not set: 2193 + * @data.dir.indexed_file: MFT reference of the file described by 2194 + * this entry. Used in directory indexes ($I30). 2195 + * - When INDEX_ENTRY_END is set or for view indexes: 2196 + * @data.vi.data_offset: Byte offset from end of this header to 2197 + * entry data. 2198 + * @data.vi.data_length: Length of data in bytes. 2199 + * @data.vi.reservedV: Reserved (zero). 2200 + * 2201 + * @length: Total byte size of this index entry 2202 + * (multiple of 8 bytes). 2203 + * @key_length: Byte size of the key (not multiple of 8 bytes). 2204 + * Key follows the header immediately. 2205 + * @flags: Bit field of INDEX_ENTRY_* flags (INDEX_ENTRY_NODE, etc.). 2206 + * @reserved: Reserved/padding (zero; align to 8 bytes). 2090 2207 */ 2091 - typedef struct { 2092 - /* 0*/ union { 2093 - struct { /* Only valid when INDEX_ENTRY_END is not set. */ 2094 - leMFT_REF indexed_file; /* The mft reference of the file 2095 - described by this index 2096 - entry. Used for directory 2097 - indexes. */ 2098 - } __attribute__ ((__packed__)) dir; 2099 - struct { /* Used for views/indexes to find the entry's data. */ 2100 - le16 data_offset; /* Data byte offset from this 2101 - INDEX_ENTRY. Follows the 2102 - index key. */ 2103 - le16 data_length; /* Data length in bytes. */ 2104 - le32 reservedV; /* Reserved (zero). */ 2105 - } __attribute__ ((__packed__)) vi; 2106 - } __attribute__ ((__packed__)) data; 2107 - /* 8*/ le16 length; /* Byte size of this index entry, multiple of 2108 - 8-bytes. */ 2109 - /* 10*/ le16 key_length; /* Byte size of the key value, which is in the 2110 - index entry. It follows field reserved. Not 2111 - multiple of 8-bytes. */ 2112 - /* 12*/ INDEX_ENTRY_FLAGS flags; /* Bit field of INDEX_ENTRY_* flags. */ 2113 - /* 14*/ le16 reserved; /* Reserved/align to 8-byte boundary. */ 2114 - /* sizeof() = 16 bytes */ 2115 - } __attribute__ ((__packed__)) INDEX_ENTRY_HEADER; 2208 + struct index_entry_header { 2209 + union { 2210 + struct { 2211 + __le64 indexed_file; 2212 + } __packed dir; 2213 + struct { 2214 + __le16 data_offset; 2215 + __le16 data_length; 2216 + __le32 reservedV; 2217 + } __packed vi; 2218 + } __packed data; 2219 + __le16 length; 2220 + __le16 key_length; 2221 + __le16 flags; 2222 + __le16 reserved; 2223 + } __packed; 2224 + 2225 + static_assert(sizeof(struct index_entry_header) == 16); 2116 2226 2117 2227 /* 2118 - * This is an index entry. A sequence of such entries follows each INDEX_HEADER 2228 + * struct index_entry - NTFS index entry structure 2229 + * 2230 + * This is an index entry. A sequence of such entries follows each index_header 2119 2231 * structure. Together they make up a complete index. The index follows either 2120 2232 * an index root attribute or an index allocation attribute. 2121 2233 * 2234 + * Union @data (valid when INDEX_ENTRY_END not set): 2235 + * @data.dir.indexed_file: MFT ref of file (for directory indexes). 2236 + * @data.vi.data_offset: Offset to data after key. 2237 + * @data.vi.data_length: Length of data in bytes. 2238 + * @data.vi.reservedV: Reserved (zero). 2239 + * 2240 + * Fields: 2241 + * @length: Total byte size of entry (multiple of 8 bytes). 2242 + * @key_length: Byte size of key (not multiple of 8). 2243 + * @flags: INDEX_ENTRY_* flags (NODE, END, etc.). 2244 + * @reserved: Reserved/padding (zero). 2245 + * 2246 + * Union @key (valid when INDEX_ENTRY_END not set) 2247 + * The key of the indexed attribute. NOTE: Only present 2248 + * if INDEX_ENTRY_END bit in flags is not set. NOTE: On 2249 + * NTFS versions before 3.0 the only valid key is the 2250 + * struct file_name_attr. On NTFS 3.0+ the following 2251 + * additional index keys are defined: 2252 + * @key.file_name: $FILE_NAME attr (for $I30 directory indexes). 2253 + * @key.sii: $SII key (for $Secure $SII index). 2254 + * @key.sdh: $SDH key (for $Secure $SDH index). 2255 + * @key.object_id: GUID (for $ObjId $O index). 2256 + * @key.reparse: Reparse tag + file ID (for $Reparse $R). 2257 + * @key.sid: SID (for $Quota $O index). 2258 + * @key.owner_id: User ID (for $Quota $Q index). 2259 + * 2260 + * The (optional) index data is inserted here when creating. 2261 + * __le64 vcn; If INDEX_ENTRY_NODE bit in flags is set, the last 2262 + * eight bytes of this index entry contain the virtual 2263 + * cluster number of the index block that holds the 2264 + * entries immediately preceding the current entry (the 2265 + * vcn references the corresponding cluster in the data 2266 + * of the non-resident index allocation attribute). If 2267 + * the key_length is zero, then the vcn immediately 2268 + * follows the INDEX_ENTRY_HEADER. Regardless of 2269 + * key_length, the address of the 8-byte boundary 2270 + * aligned vcn of INDEX_ENTRY{_HEADER} *ie is given by 2271 + * (char*)ie + le16_to_cpu(ie*)->length) - sizeof(VCN), 2272 + * where sizeof(VCN) can be hardcoded as 8 if wanted. 2273 + * 2122 2274 * NOTE: Before NTFS 3.0 only filename attributes were indexed. 2123 2275 */ 2124 - typedef struct { 2125 - /*Ofs*/ 2126 - /* 0 INDEX_ENTRY_HEADER; -- Unfolded here as gcc dislikes unnamed structs. */ 2276 + struct index_entry { 2127 2277 union { 2128 - struct { /* Only valid when INDEX_ENTRY_END is not set. */ 2129 - leMFT_REF indexed_file; /* The mft reference of the file 2130 - described by this index 2131 - entry. Used for directory 2132 - indexes. */ 2133 - } __attribute__ ((__packed__)) dir; 2134 - struct { /* Used for views/indexes to find the entry's data. */ 2135 - le16 data_offset; /* Data byte offset from this 2136 - INDEX_ENTRY. Follows the 2137 - index key. */ 2138 - le16 data_length; /* Data length in bytes. */ 2139 - le32 reservedV; /* Reserved (zero). */ 2140 - } __attribute__ ((__packed__)) vi; 2141 - } __attribute__ ((__packed__)) data; 2142 - le16 length; /* Byte size of this index entry, multiple of 2143 - 8-bytes. */ 2144 - le16 key_length; /* Byte size of the key value, which is in the 2145 - index entry. It follows field reserved. Not 2146 - multiple of 8-bytes. */ 2147 - INDEX_ENTRY_FLAGS flags; /* Bit field of INDEX_ENTRY_* flags. */ 2148 - le16 reserved; /* Reserved/align to 8-byte boundary. */ 2149 - 2150 - /* 16*/ union { /* The key of the indexed attribute. NOTE: Only present 2151 - if INDEX_ENTRY_END bit in flags is not set. NOTE: On 2152 - NTFS versions before 3.0 the only valid key is the 2153 - FILE_NAME_ATTR. On NTFS 3.0+ the following 2154 - additional index keys are defined: */ 2155 - FILE_NAME_ATTR file_name;/* $I30 index in directories. */ 2156 - SII_INDEX_KEY sii; /* $SII index in $Secure. */ 2157 - SDH_INDEX_KEY sdh; /* $SDH index in $Secure. */ 2158 - GUID object_id; /* $O index in FILE_Extend/$ObjId: The 2159 - object_id of the mft record found in 2160 - the data part of the index. */ 2161 - REPARSE_INDEX_KEY reparse; /* $R index in 2162 - FILE_Extend/$Reparse. */ 2163 - SID sid; /* $O index in FILE_Extend/$Quota: 2164 - SID of the owner of the user_id. */ 2165 - le32 owner_id; /* $Q index in FILE_Extend/$Quota: 2166 - user_id of the owner of the quota 2167 - control entry in the data part of 2168 - the index. */ 2169 - } __attribute__ ((__packed__)) key; 2170 - /* The (optional) index data is inserted here when creating. */ 2171 - // leVCN vcn; /* If INDEX_ENTRY_NODE bit in flags is set, the last 2172 - // eight bytes of this index entry contain the virtual 2173 - // cluster number of the index block that holds the 2174 - // entries immediately preceding the current entry (the 2175 - // vcn references the corresponding cluster in the data 2176 - // of the non-resident index allocation attribute). If 2177 - // the key_length is zero, then the vcn immediately 2178 - // follows the INDEX_ENTRY_HEADER. Regardless of 2179 - // key_length, the address of the 8-byte boundary 2180 - // aligned vcn of INDEX_ENTRY{_HEADER} *ie is given by 2181 - // (char*)ie + le16_to_cpu(ie*)->length) - sizeof(VCN), 2182 - // where sizeof(VCN) can be hardcoded as 8 if wanted. */ 2183 - } __attribute__ ((__packed__)) INDEX_ENTRY; 2184 - 2185 - /* 2186 - * Attribute: Bitmap (0xb0). 2187 - * 2188 - * Contains an array of bits (aka a bitfield). 2189 - * 2190 - * When used in conjunction with the index allocation attribute, each bit 2191 - * corresponds to one index block within the index allocation attribute. Thus 2192 - * the number of bits in the bitmap * index block size / cluster size is the 2193 - * number of clusters in the index allocation attribute. 2194 - */ 2195 - typedef struct { 2196 - u8 bitmap[0]; /* Array of bits. */ 2197 - } __attribute__ ((__packed__)) BITMAP_ATTR; 2278 + struct { 2279 + __le64 indexed_file; 2280 + } __packed dir; 2281 + struct { 2282 + __le16 data_offset; 2283 + __le16 data_length; 2284 + __le32 reservedV; 2285 + } __packed vi; 2286 + } __packed data; 2287 + __le16 length; 2288 + __le16 key_length; 2289 + __le16 flags; 2290 + __le16 reserved; 2291 + union { 2292 + struct file_name_attr file_name; 2293 + struct sii_index_key sii; 2294 + struct sdh_index_key sdh; 2295 + struct guid object_id; 2296 + struct reparse_index_key reparse; 2297 + struct ntfs_sid sid; 2298 + __le32 owner_id; 2299 + } __packed key; 2300 + } __packed; 2198 2301 2199 2302 /* 2200 2303 * The reparse point tag defines the type of the reparse point. It also ··· 2213 2294 * 2214 2295 * The reparse point tag is an unsigned 32-bit value divided in three parts: 2215 2296 * 2216 - * 1. The least significant 16 bits (i.e. bits 0 to 15) specifiy the type of 2297 + * 1. The least significant 16 bits (i.e. bits 0 to 15) specify the type of 2217 2298 * the reparse point. 2218 - * 2. The 13 bits after this (i.e. bits 16 to 28) are reserved for future use. 2219 - * 3. The most significant three bits are flags describing the reparse point. 2299 + * 2. The 12 bits after this (i.e. bits 16 to 27) are reserved for future use. 2300 + * 3. The most significant four bits are flags describing the reparse point. 2220 2301 * They are defined as follows: 2302 + * bit 28: Directory bit. If set, the directory is not a surrogate 2303 + * and can be used the usual way. 2221 2304 * bit 29: Name surrogate bit. If set, the filename is an alias for 2222 2305 * another object in the system. 2223 2306 * bit 30: High-latency bit. If set, accessing the first byte of data will 2224 2307 * be slow. (E.g. the data is stored on a tape drive.) 2225 2308 * bit 31: Microsoft bit. If set, the tag is owned by Microsoft. User 2226 2309 * defined tags have to use zero here. 2227 - * 2228 - * These are the predefined reparse point tags: 2310 + * 4. Moreover, on Windows 10 : 2311 + * Some flags may be used in bits 12 to 15 to further describe the 2312 + * reparse point. 2229 2313 */ 2230 2314 enum { 2315 + IO_REPARSE_TAG_DIRECTORY = cpu_to_le32(0x10000000), 2231 2316 IO_REPARSE_TAG_IS_ALIAS = cpu_to_le32(0x20000000), 2232 2317 IO_REPARSE_TAG_IS_HIGH_LATENCY = cpu_to_le32(0x40000000), 2233 2318 IO_REPARSE_TAG_IS_MICROSOFT = cpu_to_le32(0x80000000), ··· 2240 2317 IO_REPARSE_TAG_RESERVED_ONE = cpu_to_le32(0x00000001), 2241 2318 IO_REPARSE_TAG_RESERVED_RANGE = cpu_to_le32(0x00000001), 2242 2319 2243 - IO_REPARSE_TAG_NSS = cpu_to_le32(0x68000005), 2244 - IO_REPARSE_TAG_NSS_RECOVER = cpu_to_le32(0x68000006), 2245 - IO_REPARSE_TAG_SIS = cpu_to_le32(0x68000007), 2246 - IO_REPARSE_TAG_DFS = cpu_to_le32(0x68000008), 2320 + IO_REPARSE_TAG_CSV = cpu_to_le32(0x80000009), 2321 + IO_REPARSE_TAG_DEDUP = cpu_to_le32(0x80000013), 2322 + IO_REPARSE_TAG_DFS = cpu_to_le32(0x8000000A), 2323 + IO_REPARSE_TAG_DFSR = cpu_to_le32(0x80000012), 2324 + IO_REPARSE_TAG_HSM = cpu_to_le32(0xC0000004), 2325 + IO_REPARSE_TAG_HSM2 = cpu_to_le32(0x80000006), 2326 + IO_REPARSE_TAG_MOUNT_POINT = cpu_to_le32(0xA0000003), 2327 + IO_REPARSE_TAG_NFS = cpu_to_le32(0x80000014), 2328 + IO_REPARSE_TAG_SIS = cpu_to_le32(0x80000007), 2329 + IO_REPARSE_TAG_SYMLINK = cpu_to_le32(0xA000000C), 2330 + IO_REPARSE_TAG_WIM = cpu_to_le32(0x80000008), 2331 + IO_REPARSE_TAG_DFM = cpu_to_le32(0x80000016), 2332 + IO_REPARSE_TAG_WOF = cpu_to_le32(0x80000017), 2333 + IO_REPARSE_TAG_WCI = cpu_to_le32(0x80000018), 2334 + IO_REPARSE_TAG_CLOUD = cpu_to_le32(0x9000001A), 2335 + IO_REPARSE_TAG_APPEXECLINK = cpu_to_le32(0x8000001B), 2336 + IO_REPARSE_TAG_GVFS = cpu_to_le32(0x9000001C), 2337 + IO_REPARSE_TAG_LX_SYMLINK = cpu_to_le32(0xA000001D), 2338 + IO_REPARSE_TAG_AF_UNIX = cpu_to_le32(0x80000023), 2339 + IO_REPARSE_TAG_LX_FIFO = cpu_to_le32(0x80000024), 2340 + IO_REPARSE_TAG_LX_CHR = cpu_to_le32(0x80000025), 2341 + IO_REPARSE_TAG_LX_BLK = cpu_to_le32(0x80000026), 2247 2342 2248 - IO_REPARSE_TAG_MOUNT_POINT = cpu_to_le32(0x88000003), 2249 - 2250 - IO_REPARSE_TAG_HSM = cpu_to_le32(0xa8000004), 2251 - 2252 - IO_REPARSE_TAG_SYMBOLIC_LINK = cpu_to_le32(0xe8000000), 2253 - 2254 - IO_REPARSE_TAG_VALID_VALUES = cpu_to_le32(0xe000ffff), 2343 + IO_REPARSE_TAG_VALID_VALUES = cpu_to_le32(0xf000ffff), 2344 + IO_REPARSE_PLUGIN_SELECT = cpu_to_le32(0xffff0fff), 2255 2345 }; 2256 2346 2257 2347 /* 2258 - * Attribute: Reparse point (0xc0). 2348 + * struct reparse_point - $REPARSE_POINT attribute content (0xc0)\ 2349 + * 2350 + * @reparse_tag: Reparse point type (with flags; REPARSE_TAG_*). 2351 + * @reparse_data_length: Byte size of @reparse_data. 2352 + * @reserved: Reserved/padding (zero; 8-byte alignment). 2353 + * @reparse_data: Variable reparse data (meaning depends on @reparse_tag). 2354 + * - Symbolic link/junction: struct reparse_symlink 2355 + * - Mount point: similar symlink structure 2356 + * - Other tags: vendor-specific or extended data 2259 2357 * 2260 2358 * NOTE: Can be resident or non-resident. 2261 2359 */ 2262 - typedef struct { 2263 - le32 reparse_tag; /* Reparse point type (inc. flags). */ 2264 - le16 reparse_data_length; /* Byte size of reparse data. */ 2265 - le16 reserved; /* Align to 8-byte boundary. */ 2266 - u8 reparse_data[0]; /* Meaning depends on reparse_tag. */ 2267 - } __attribute__ ((__packed__)) REPARSE_POINT; 2360 + struct reparse_point { 2361 + __le32 reparse_tag; 2362 + __le16 reparse_data_length; 2363 + __le16 reserved; 2364 + u8 reparse_data[]; 2365 + } __packed; 2268 2366 2269 2367 /* 2270 - * Attribute: Extended attribute (EA) information (0xd0). 2368 + * struct ea_information - $EA_INFORMATION attribute content (0xd0) 2369 + * 2370 + * @ea_length: Byte size of packed EAs. 2371 + * @need_ea_count: Number of EAs with NEED_EA bit set. 2372 + * @ea_query_length: Byte size needed to unpack/query EAs via ZwQueryEaFile(). 2373 + * (Unpacked format size.) 2271 2374 * 2272 2375 * NOTE: Always resident. (Is this true???) 2273 2376 */ 2274 - typedef struct { 2275 - le16 ea_length; /* Byte size of the packed extended 2276 - attributes. */ 2277 - le16 need_ea_count; /* The number of extended attributes which have 2278 - the NEED_EA bit set. */ 2279 - le32 ea_query_length; /* Byte size of the buffer required to query 2280 - the extended attributes when calling 2281 - ZwQueryEaFile() in Windows NT/2k. I.e. the 2282 - byte size of the unpacked extended 2283 - attributes. */ 2284 - } __attribute__ ((__packed__)) EA_INFORMATION; 2377 + struct ea_information { 2378 + __le16 ea_length; 2379 + __le16 need_ea_count; 2380 + __le32 ea_query_length; 2381 + } __packed; 2285 2382 2286 2383 /* 2287 - * Extended attribute flags (8-bit). 2384 + * enum - Extended attribute flags (8-bit) 2385 + * 2386 + * These flags are stored in the EA header of each extended attribute 2387 + * (in $EA attribute, type 0xe0). 2388 + * 2389 + * @NEED_EA: If set, the file cannot be properly interpreted 2390 + * without understanding its associated EAs. 2391 + * (Critical EA; applications must process it.) 2288 2392 */ 2289 2393 enum { 2290 - NEED_EA = 0x80 /* If set the file to which the EA belongs 2291 - cannot be interpreted without understanding 2292 - the associates extended attributes. */ 2293 - } __attribute__ ((__packed__)); 2294 - 2295 - typedef u8 EA_FLAGS; 2394 + NEED_EA = 0x80 2395 + } __packed; 2296 2396 2297 2397 /* 2298 - * Attribute: Extended attribute (EA) (0xe0). 2398 + * struct ea_attr - Extended attribute (EA) entry (0xe0) 2399 + * 2400 + * @next_entry_offset: Byte offset to the next EA_ATTR entry. 2401 + * (From start of current entry.) 2402 + * @flags: EA flags (NEED_EA = 0x80 if critical). 2403 + * @ea_name_length: Length of @ea_name in bytes (excluding '\0'). 2404 + * @ea_value_length: Byte size of the EA value. 2405 + * @ea_name: ASCII name of the EA (zero-terminated). 2406 + * Value immediately follows the name. 2407 + * u8 ea_value[]; The value of the EA. Immediately follows the name. 2408 + * 2409 + * This is one variable-length record in the $EA attribute value. 2410 + * The attribute can be resident or non-resident. 2411 + * Sequence of these entries forms the packed EA list. 2299 2412 * 2300 2413 * NOTE: Can be resident or non-resident. 2301 - * 2302 - * Like the attribute list and the index buffer list, the EA attribute value is 2303 - * a sequence of EA_ATTR variable length records. 2304 2414 */ 2305 - typedef struct { 2306 - le32 next_entry_offset; /* Offset to the next EA_ATTR. */ 2307 - EA_FLAGS flags; /* Flags describing the EA. */ 2308 - u8 ea_name_length; /* Length of the name of the EA in bytes 2309 - excluding the '\0' byte terminator. */ 2310 - le16 ea_value_length; /* Byte size of the EA's value. */ 2311 - u8 ea_name[0]; /* Name of the EA. Note this is ASCII, not 2312 - Unicode and it is zero terminated. */ 2313 - u8 ea_value[0]; /* The value of the EA. Immediately follows 2314 - the name. */ 2315 - } __attribute__ ((__packed__)) EA_ATTR; 2316 - 2317 - /* 2318 - * Attribute: Property set (0xf0). 2319 - * 2320 - * Intended to support Native Structure Storage (NSS) - a feature removed from 2321 - * NTFS 3.0 during beta testing. 2322 - */ 2323 - typedef struct { 2324 - /* Irrelevant as feature unused. */ 2325 - } __attribute__ ((__packed__)) PROPERTY_SET; 2326 - 2327 - /* 2328 - * Attribute: Logged utility stream (0x100). 2329 - * 2330 - * NOTE: Can be resident or non-resident. 2331 - * 2332 - * Operations on this attribute are logged to the journal ($LogFile) like 2333 - * normal metadata changes. 2334 - * 2335 - * Used by the Encrypting File System (EFS). All encrypted files have this 2336 - * attribute with the name $EFS. 2337 - */ 2338 - typedef struct { 2339 - /* Can be anything the creator chooses. */ 2340 - /* EFS uses it as follows: */ 2341 - // FIXME: Type this info, verifying it along the way. (AIA) 2342 - } __attribute__ ((__packed__)) LOGGED_UTILITY_STREAM, EFS_ATTR; 2415 + struct ea_attr { 2416 + __le32 next_entry_offset; 2417 + u8 flags; 2418 + u8 ea_name_length; 2419 + __le16 ea_value_length; 2420 + u8 ea_name[]; 2421 + } __packed; 2343 2422 2344 2423 #endif /* _LINUX_NTFS_LAYOUT_H */
+35 -32
fs/ntfs/lcnalloc.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * lcnalloc.h - Exports for NTFS kernel cluster (de)allocation. Part of the 4 - * Linux-NTFS project. 3 + * Exports for NTFS kernel cluster (de)allocation. 5 4 * 6 5 * Copyright (c) 2004-2005 Anton Altaparmakov 7 6 */ ··· 8 9 #ifndef _LINUX_NTFS_LCNALLOC_H 9 10 #define _LINUX_NTFS_LCNALLOC_H 10 11 11 - #ifdef NTFS_RW 12 - 13 - #include <linux/fs.h> 12 + #include <linux/sched/mm.h> 14 13 15 14 #include "attrib.h" 16 - #include "types.h" 17 - #include "inode.h" 18 - #include "runlist.h" 19 - #include "volume.h" 20 15 21 - typedef enum { 22 - FIRST_ZONE = 0, /* For sanity checking. */ 23 - MFT_ZONE = 0, /* Allocate from $MFT zone. */ 24 - DATA_ZONE = 1, /* Allocate from $DATA zone. */ 25 - LAST_ZONE = 1, /* For sanity checking. */ 26 - } NTFS_CLUSTER_ALLOCATION_ZONES; 16 + /* 17 + * enum zone_type - Zone identifiers for cluster allocation policy 18 + * 19 + * FIRST_ZONE For sanity checking. 20 + * MFT_ZONE Allocate from $MFT zone. 21 + * DATA_ZONE Allocate from $DATA zone. 22 + * LAST_ZONE For sanity checking. 23 + */ 24 + enum { 25 + FIRST_ZONE = 0, 26 + MFT_ZONE = 0, 27 + DATA_ZONE = 1, 28 + LAST_ZONE = 1, 29 + }; 27 30 28 - extern runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, 29 - const VCN start_vcn, const s64 count, const LCN start_lcn, 30 - const NTFS_CLUSTER_ALLOCATION_ZONES zone, 31 - const bool is_extension); 31 + struct runlist_element *ntfs_cluster_alloc(struct ntfs_volume *vol, 32 + const s64 start_vcn, const s64 count, const s64 start_lcn, 33 + const int zone, 34 + const bool is_extension, 35 + const bool is_contig, 36 + const bool is_dealloc); 37 + s64 __ntfs_cluster_free(struct ntfs_inode *ni, const s64 start_vcn, 38 + s64 count, struct ntfs_attr_search_ctx *ctx, const bool is_rollback); 32 39 33 - extern s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, 34 - s64 count, ntfs_attr_search_ctx *ctx, const bool is_rollback); 35 - 36 - /** 40 + /* 37 41 * ntfs_cluster_free - free clusters on an ntfs volume 38 42 * @ni: ntfs inode whose runlist describes the clusters to free 39 43 * @start_vcn: vcn in the runlist of @ni at which to start freeing clusters ··· 92 90 * - If @ctx is not NULL, the base mft record must be mapped on entry 93 91 * and it will be left mapped on return. 94 92 */ 95 - static inline s64 ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, 96 - s64 count, ntfs_attr_search_ctx *ctx) 93 + static inline s64 ntfs_cluster_free(struct ntfs_inode *ni, const s64 start_vcn, 94 + s64 count, struct ntfs_attr_search_ctx *ctx) 97 95 { 98 96 return __ntfs_cluster_free(ni, start_vcn, count, ctx, false); 99 97 } 100 98 101 - extern int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol, 102 - const runlist_element *rl); 99 + int ntfs_cluster_free_from_rl_nolock(struct ntfs_volume *vol, 100 + const struct runlist_element *rl); 103 101 104 - /** 102 + /* 105 103 * ntfs_cluster_free_from_rl - free clusters from runlist 106 104 * @vol: mounted ntfs volume on which to free the clusters 107 105 * @rl: runlist describing the clusters to free ··· 117 115 * - The caller must have locked the runlist @rl for reading or 118 116 * writing. 119 117 */ 120 - static inline int ntfs_cluster_free_from_rl(ntfs_volume *vol, 121 - const runlist_element *rl) 118 + static inline int ntfs_cluster_free_from_rl(struct ntfs_volume *vol, 119 + const struct runlist_element *rl) 122 120 { 123 121 int ret; 122 + unsigned int memalloc_flags; 124 123 124 + memalloc_flags = memalloc_nofs_save(); 125 125 down_write(&vol->lcnbmp_lock); 126 126 ret = ntfs_cluster_free_from_rl_nolock(vol, rl); 127 127 up_write(&vol->lcnbmp_lock); 128 + memalloc_nofs_restore(memalloc_flags); 128 129 return ret; 129 130 } 130 - 131 - #endif /* NTFS_RW */ 132 131 133 132 #endif /* defined _LINUX_NTFS_LCNALLOC_H */
+175 -225
fs/ntfs/logfile.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * logfile.h - Defines for NTFS kernel journal ($LogFile) handling. Part of 4 - * the Linux-NTFS project. 3 + * Defines for NTFS kernel journal (LogFile) handling. 5 4 * 6 5 * Copyright (c) 2000-2005 Anton Altaparmakov 7 6 */ ··· 8 9 #ifndef _LINUX_NTFS_LOGFILE_H 9 10 #define _LINUX_NTFS_LOGFILE_H 10 11 11 - #ifdef NTFS_RW 12 - 13 - #include <linux/fs.h> 14 - 15 - #include "types.h" 16 - #include "endian.h" 17 12 #include "layout.h" 18 13 19 14 /* 20 - * Journal ($LogFile) organization: 15 + * Journal (LogFile) organization: 21 16 * 22 17 * Two restart areas present in the first two pages (restart pages, one restart 23 18 * area in each page). When the volume is dismounted they should be identical, ··· 35 42 * reinitialize the logfile and start again with version 1.1. 36 43 */ 37 44 38 - /* Some $LogFile related constants. */ 45 + /* Some LogFile related constants. */ 39 46 #define MaxLogFileSize 0x100000000ULL 40 47 #define DefaultLogPageSize 4096 41 48 #define MinLogRecordPages 48 42 49 43 50 /* 44 51 * Log file restart page header (begins the restart area). 52 + * 53 + * @magic: The magic is "RSTR". 54 + * @usa_ofs: See ntfs_record struct definition in layout.h. When creating, 55 + * set this to be immediately after this header structure (without any 56 + * alignment). 57 + * @usa_count: See ntfs_record struct definition in layout.h. 58 + * @chkdsk_lsn: The last log file sequence number found by chkdsk. Only 59 + * used when the magic is changed to "CHKD". Otherwise this is zero. 60 + * @system_page_size: Byte size of system pages when the log file was 61 + * created, has to be >= 512 and a power of 2. Use this to calculate 62 + * the required size of the usa (usa_count) and add it to usa_ofs. Then 63 + * verify that the result is less than the value of 64 + * the restart_area_offset. 65 + * @log_page_size: Byte size of log file pages, has to be >= 512 and 66 + * a power of 2. The default is 4096 and is used when the system page 67 + * size is between 4096 and 8192. Otherwise this is set to the system 68 + * page size instead. 69 + * @restart_area_offset: Byte offset from the start of this header to 70 + * the RESTART_AREA. Value has to be aligned to 8-byte boundary. When 71 + * creating, set this to be after the usa. 72 + * @minor_ver: Log file minor version. Only check if major version is 1. 73 + * @major_ver: Log file major version. We only support version 1.1. 45 74 */ 46 - typedef struct { 47 - /*Ofs*/ 48 - /* 0 NTFS_RECORD; -- Unfolded here as gcc doesn't like unnamed structs. */ 49 - /* 0*/ NTFS_RECORD_TYPE magic; /* The magic is "RSTR". */ 50 - /* 4*/ le16 usa_ofs; /* See NTFS_RECORD definition in layout.h. 51 - When creating, set this to be immediately 52 - after this header structure (without any 53 - alignment). */ 54 - /* 6*/ le16 usa_count; /* See NTFS_RECORD definition in layout.h. */ 55 - 56 - /* 8*/ leLSN chkdsk_lsn; /* The last log file sequence number found by 57 - chkdsk. Only used when the magic is changed 58 - to "CHKD". Otherwise this is zero. */ 59 - /* 16*/ le32 system_page_size; /* Byte size of system pages when the log file 60 - was created, has to be >= 512 and a power of 61 - 2. Use this to calculate the required size 62 - of the usa (usa_count) and add it to usa_ofs. 63 - Then verify that the result is less than the 64 - value of the restart_area_offset. */ 65 - /* 20*/ le32 log_page_size; /* Byte size of log file pages, has to be >= 66 - 512 and a power of 2. The default is 4096 67 - and is used when the system page size is 68 - between 4096 and 8192. Otherwise this is 69 - set to the system page size instead. */ 70 - /* 24*/ le16 restart_area_offset;/* Byte offset from the start of this header to 71 - the RESTART_AREA. Value has to be aligned 72 - to 8-byte boundary. When creating, set this 73 - to be after the usa. */ 74 - /* 26*/ sle16 minor_ver; /* Log file minor version. Only check if major 75 - version is 1. */ 76 - /* 28*/ sle16 major_ver; /* Log file major version. We only support 77 - version 1.1. */ 78 - /* sizeof() = 30 (0x1e) bytes */ 79 - } __attribute__ ((__packed__)) RESTART_PAGE_HEADER; 75 + struct restart_page_header { 76 + __le32 magic; 77 + __le16 usa_ofs; 78 + __le16 usa_count; 79 + __le64 chkdsk_lsn; 80 + __le32 system_page_size; 81 + __le32 log_page_size; 82 + __le16 restart_area_offset; 83 + __le16 minor_ver; 84 + __le16 major_ver; 85 + } __packed; 80 86 81 87 /* 82 88 * Constant for the log client indices meaning that there are no client records ··· 88 96 /* 89 97 * These are the so far known RESTART_AREA_* flags (16-bit) which contain 90 98 * information about the log file in which they are present. 99 + * gcc: Force enum bit width to 16. 91 100 */ 92 101 enum { 93 102 RESTART_VOLUME_IS_CLEAN = cpu_to_le16(0x0002), 94 - RESTART_SPACE_FILLER = cpu_to_le16(0xffff), /* gcc: Force enum bit width to 16. */ 95 - } __attribute__ ((__packed__)); 96 - 97 - typedef le16 RESTART_AREA_FLAGS; 103 + RESTART_SPACE_FILLER = cpu_to_le16(0xffff), 104 + } __packed; 98 105 99 106 /* 100 107 * Log file restart area record. The offset of this record is found by adding 101 108 * the offset of the RESTART_PAGE_HEADER to the restart_area_offset value found 102 109 * in it. See notes at restart_area_offset above. 110 + * 111 + * @current_lsn: The current, i.e. last LSN inside the log when 112 + * the restart area was last written. This happens often but what is 113 + * the interval? Is it just fixed time or is it every time a check point 114 + * is written or somethine else? On create set to 0. 115 + * @log_clients: Number of log client records in the array of log client 116 + * records which follows this restart area. Must be 1. 117 + * @client_free_list: The index of the first free log client record in 118 + * the array of log client records. LOGFILE_NO_CLIENT means that there 119 + * are no free log client records in the array. If != LOGFILE_NO_CLIENT, 120 + * check that log_clients > client_free_list. On Win2k and presumably 121 + * earlier, on a clean volume this is != LOGFILE_NO_CLIENT, and it should 122 + * be 0, i.e. the first (and only) client record is free and thus 123 + * the logfile is closed and hence clean. A dirty volume would have left 124 + * the logfile open and hence this would be LOGFILE_NO_CLIENT. On WinXP 125 + * and presumably later, the logfile is always open, even on clean 126 + * shutdown so this should always be LOGFILE_NO_CLIENT. 127 + * @client_in_use_list: The index of the first in-use log client record in 128 + * the array of log client records. LOGFILE_NO_CLIENT means that there 129 + * are no in-use log client records in the array. 130 + * If != LOGFILE_NO_CLIENT check that log_clients > client_in_use_list. 131 + * On Win2k and presumably earlier, on a clean volume this is 132 + * LOGFILE_NO_CLIENT, i.e. there are no client records in use and thus 133 + * the logfile is closed and hence clean. A dirty volume would have left 134 + * the logfile open and hence this would be != LOGFILE_NO_CLIENT, and it 135 + * should be 0, i.e. the first (and only) client record is in use. On 136 + * WinXP and presumably later, the logfile is always open, even on clean 137 + * shutdown so this should always be 0. 138 + * @flags: Flags modifying LFS behaviour. On Win2k and presumably earlier 139 + * this is always 0. On WinXP and presumably later, if the logfile was 140 + * shutdown cleanly, the second bit, RESTART_VOLUME_IS_CLEAN, is set. 141 + * This bit is cleared when the volume is mounted by WinXP and set when 142 + * the volume is dismounted, thus if the logfile is dirty, this bit is 143 + * clear. Thus we don't need to check the Windows version to determine 144 + * if the logfile is clean. Instead if the logfile is closed, we know 145 + * it must be clean. If it is open and this bit is set, we also know 146 + * it must be clean. If on the other hand the logfile is open and this 147 + * bit is clear, we can be almost certain that the logfile is dirty. 148 + * @seq_number_bits: How many bits to use for the sequence number. This 149 + * is calculated as 67 - the number of bits required to store the logfile 150 + * size in bytes and this can be used in with the specified file_size as 151 + * a consistency check. 152 + * @restart_area_length: Length of the restart area including the client 153 + * array. Following checks required if version matches. Otherwise, 154 + * skip them. restart_area_offset + restart_area_length has to be 155 + * <= system_page_size. Also, restart_area_length has to be >= 156 + * client_array_offset + (log_clients * sizeof(log client record)). 157 + * @client_array_offset: Offset from the start of this record to the first 158 + * log client record if versions are matched. When creating, set this 159 + * to be after this restart area structure, aligned to 8-bytes boundary. 160 + * If the versions do not match, this is ignored and the offset is 161 + * assumed to be (sizeof(RESTART_AREA) + 7) & ~7, i.e. rounded up to 162 + * first 8-byte boundary. Either way, client_array_offset has to be 163 + * aligned to an 8-byte boundary. Also, restart_area_offset + 164 + * client_array_offset has to be <= 510. Finally, client_array_offset + 165 + * (log_clients * sizeof(log client record)) has to be <= system_page_size. 166 + * On Win2k and presumably earlier, this is 0x30, i.e. immediately 167 + * following this record. On WinXP and presumably later, this is 0x40, 168 + * i.e. there are 16 extra bytes between this record and the client 169 + * array. This probably means that the RESTART_AREA record is actually 170 + * bigger in WinXP and later. 171 + * @file_size: Usable byte size of the log file. 172 + * If the restart_area_offset + the offset of the file_size are > 510 173 + * then corruption has occurred. This is the very first check when 174 + * starting with the restart_area as if it fails it means that some of 175 + * the above values will be corrupted by the multi sector transfer 176 + * protection. The file_size has to be rounded down to be a multiple 177 + * of the log_page_size in the RESTART_PAGE_HEADER and then it has to be 178 + * at least big enough to store the two restart pages and 48 (0x30) log 179 + * record pages. 180 + * @last_lsn_data_length: Length of data of last LSN, not including the log 181 + * record header. On create set to 0. 182 + * @log_record_header_length: Byte size of the log record header. If the 183 + * version matches then check that the value of log_record_header_length 184 + * is a multiple of 8, i.e. (log_record_header_length + 7) & ~7 == 185 + * log_record_header_length. When creating set it to 186 + * sizeof(LOG_RECORD_HEADER), aligned to 8 bytes. 187 + * @log_page_data_offset: Offset to the start of data in a log record page. 188 + * Must be a multiple of 8. On create set it to immediately after 189 + * the update sequence array of the log record page. 190 + * @restart_log_open_count: A counter that gets incremented every time 191 + * the logfile is restarted which happens at mount time when the logfile 192 + * is opened. When creating set to a random value. Win2k sets it to 193 + * the low 32 bits of the current system time in NTFS format (see time.h). 194 + * @reserved: Reserved/alignment to 8-byte boundary. 103 195 */ 104 - typedef struct { 105 - /*Ofs*/ 106 - /* 0*/ leLSN current_lsn; /* The current, i.e. last LSN inside the log 107 - when the restart area was last written. 108 - This happens often but what is the interval? 109 - Is it just fixed time or is it every time a 110 - check point is written or somethine else? 111 - On create set to 0. */ 112 - /* 8*/ le16 log_clients; /* Number of log client records in the array of 113 - log client records which follows this 114 - restart area. Must be 1. */ 115 - /* 10*/ le16 client_free_list; /* The index of the first free log client record 116 - in the array of log client records. 117 - LOGFILE_NO_CLIENT means that there are no 118 - free log client records in the array. 119 - If != LOGFILE_NO_CLIENT, check that 120 - log_clients > client_free_list. On Win2k 121 - and presumably earlier, on a clean volume 122 - this is != LOGFILE_NO_CLIENT, and it should 123 - be 0, i.e. the first (and only) client 124 - record is free and thus the logfile is 125 - closed and hence clean. A dirty volume 126 - would have left the logfile open and hence 127 - this would be LOGFILE_NO_CLIENT. On WinXP 128 - and presumably later, the logfile is always 129 - open, even on clean shutdown so this should 130 - always be LOGFILE_NO_CLIENT. */ 131 - /* 12*/ le16 client_in_use_list;/* The index of the first in-use log client 132 - record in the array of log client records. 133 - LOGFILE_NO_CLIENT means that there are no 134 - in-use log client records in the array. If 135 - != LOGFILE_NO_CLIENT check that log_clients 136 - > client_in_use_list. On Win2k and 137 - presumably earlier, on a clean volume this 138 - is LOGFILE_NO_CLIENT, i.e. there are no 139 - client records in use and thus the logfile 140 - is closed and hence clean. A dirty volume 141 - would have left the logfile open and hence 142 - this would be != LOGFILE_NO_CLIENT, and it 143 - should be 0, i.e. the first (and only) 144 - client record is in use. On WinXP and 145 - presumably later, the logfile is always 146 - open, even on clean shutdown so this should 147 - always be 0. */ 148 - /* 14*/ RESTART_AREA_FLAGS flags;/* Flags modifying LFS behaviour. On Win2k 149 - and presumably earlier this is always 0. On 150 - WinXP and presumably later, if the logfile 151 - was shutdown cleanly, the second bit, 152 - RESTART_VOLUME_IS_CLEAN, is set. This bit 153 - is cleared when the volume is mounted by 154 - WinXP and set when the volume is dismounted, 155 - thus if the logfile is dirty, this bit is 156 - clear. Thus we don't need to check the 157 - Windows version to determine if the logfile 158 - is clean. Instead if the logfile is closed, 159 - we know it must be clean. If it is open and 160 - this bit is set, we also know it must be 161 - clean. If on the other hand the logfile is 162 - open and this bit is clear, we can be almost 163 - certain that the logfile is dirty. */ 164 - /* 16*/ le32 seq_number_bits; /* How many bits to use for the sequence 165 - number. This is calculated as 67 - the 166 - number of bits required to store the logfile 167 - size in bytes and this can be used in with 168 - the specified file_size as a consistency 169 - check. */ 170 - /* 20*/ le16 restart_area_length;/* Length of the restart area including the 171 - client array. Following checks required if 172 - version matches. Otherwise, skip them. 173 - restart_area_offset + restart_area_length 174 - has to be <= system_page_size. Also, 175 - restart_area_length has to be >= 176 - client_array_offset + (log_clients * 177 - sizeof(log client record)). */ 178 - /* 22*/ le16 client_array_offset;/* Offset from the start of this record to 179 - the first log client record if versions are 180 - matched. When creating, set this to be 181 - after this restart area structure, aligned 182 - to 8-bytes boundary. If the versions do not 183 - match, this is ignored and the offset is 184 - assumed to be (sizeof(RESTART_AREA) + 7) & 185 - ~7, i.e. rounded up to first 8-byte 186 - boundary. Either way, client_array_offset 187 - has to be aligned to an 8-byte boundary. 188 - Also, restart_area_offset + 189 - client_array_offset has to be <= 510. 190 - Finally, client_array_offset + (log_clients 191 - * sizeof(log client record)) has to be <= 192 - system_page_size. On Win2k and presumably 193 - earlier, this is 0x30, i.e. immediately 194 - following this record. On WinXP and 195 - presumably later, this is 0x40, i.e. there 196 - are 16 extra bytes between this record and 197 - the client array. This probably means that 198 - the RESTART_AREA record is actually bigger 199 - in WinXP and later. */ 200 - /* 24*/ sle64 file_size; /* Usable byte size of the log file. If the 201 - restart_area_offset + the offset of the 202 - file_size are > 510 then corruption has 203 - occurred. This is the very first check when 204 - starting with the restart_area as if it 205 - fails it means that some of the above values 206 - will be corrupted by the multi sector 207 - transfer protection. The file_size has to 208 - be rounded down to be a multiple of the 209 - log_page_size in the RESTART_PAGE_HEADER and 210 - then it has to be at least big enough to 211 - store the two restart pages and 48 (0x30) 212 - log record pages. */ 213 - /* 32*/ le32 last_lsn_data_length;/* Length of data of last LSN, not including 214 - the log record header. On create set to 215 - 0. */ 216 - /* 36*/ le16 log_record_header_length;/* Byte size of the log record header. 217 - If the version matches then check that the 218 - value of log_record_header_length is a 219 - multiple of 8, i.e. 220 - (log_record_header_length + 7) & ~7 == 221 - log_record_header_length. When creating set 222 - it to sizeof(LOG_RECORD_HEADER), aligned to 223 - 8 bytes. */ 224 - /* 38*/ le16 log_page_data_offset;/* Offset to the start of data in a log record 225 - page. Must be a multiple of 8. On create 226 - set it to immediately after the update 227 - sequence array of the log record page. */ 228 - /* 40*/ le32 restart_log_open_count;/* A counter that gets incremented every 229 - time the logfile is restarted which happens 230 - at mount time when the logfile is opened. 231 - When creating set to a random value. Win2k 232 - sets it to the low 32 bits of the current 233 - system time in NTFS format (see time.h). */ 234 - /* 44*/ le32 reserved; /* Reserved/alignment to 8-byte boundary. */ 235 - /* sizeof() = 48 (0x30) bytes */ 236 - } __attribute__ ((__packed__)) RESTART_AREA; 196 + struct restart_area { 197 + __le64 current_lsn; 198 + __le16 log_clients; 199 + __le16 client_free_list; 200 + __le16 client_in_use_list; 201 + __le16 flags; 202 + __le32 seq_number_bits; 203 + __le16 restart_area_length; 204 + __le16 client_array_offset; 205 + __le64 file_size; 206 + __le32 last_lsn_data_length; 207 + __le16 log_record_header_length; 208 + __le16 log_page_data_offset; 209 + __le32 restart_log_open_count; 210 + __le32 reserved; 211 + } __packed; 237 212 238 213 /* 239 214 * Log client record. The offset of this record is found by adding the offset 240 215 * of the RESTART_AREA to the client_array_offset value found in it. 216 + * 217 + * @oldest_lsn: Oldest LSN needed by this client. On create set to 0. 218 + * @client_restart_lsn: LSN at which this client needs to restart 219 + * the volume, i.e. the current position within the log file. 220 + * At present, if clean this should = current_lsn in restart area but it 221 + * probably also = current_lsn when dirty most of the time. 222 + * At create set to 0. 223 + * @prev_client: The offset to the previous log client record in the array 224 + * of log client records. LOGFILE_NO_CLIENT means there is no previous 225 + * client record, i.e. this is the first one. This is always 226 + * LOGFILE_NO_CLIENT. 227 + * @next_client: The offset to the next log client record in the array of 228 + * log client records. LOGFILE_NO_CLIENT means there are no next client 229 + * records, i.e. this is the last one. This is always LOGFILE_NO_CLIENT. 230 + * @seq_number: On Win2k and presumably earlier, this is set to zero every 231 + * time the logfile is restarted and it is incremented when the logfile 232 + * is closed at dismount time. Thus it is 0 when dirty and 1 when clean. 233 + * On WinXP and presumably later, this is always 0. 234 + * @reserved[6]: Reserved/alignment. 235 + * @client_name_length: Length of client name in bytes. Should always be 8. 236 + * @client_name[64]: Name of the client in Unicode. Should always be "NTFS" 237 + * with the remaining bytes set to 0. 241 238 */ 242 - typedef struct { 243 - /*Ofs*/ 244 - /* 0*/ leLSN oldest_lsn; /* Oldest LSN needed by this client. On create 245 - set to 0. */ 246 - /* 8*/ leLSN client_restart_lsn;/* LSN at which this client needs to restart 247 - the volume, i.e. the current position within 248 - the log file. At present, if clean this 249 - should = current_lsn in restart area but it 250 - probably also = current_lsn when dirty most 251 - of the time. At create set to 0. */ 252 - /* 16*/ le16 prev_client; /* The offset to the previous log client record 253 - in the array of log client records. 254 - LOGFILE_NO_CLIENT means there is no previous 255 - client record, i.e. this is the first one. 256 - This is always LOGFILE_NO_CLIENT. */ 257 - /* 18*/ le16 next_client; /* The offset to the next log client record in 258 - the array of log client records. 259 - LOGFILE_NO_CLIENT means there are no next 260 - client records, i.e. this is the last one. 261 - This is always LOGFILE_NO_CLIENT. */ 262 - /* 20*/ le16 seq_number; /* On Win2k and presumably earlier, this is set 263 - to zero every time the logfile is restarted 264 - and it is incremented when the logfile is 265 - closed at dismount time. Thus it is 0 when 266 - dirty and 1 when clean. On WinXP and 267 - presumably later, this is always 0. */ 268 - /* 22*/ u8 reserved[6]; /* Reserved/alignment. */ 269 - /* 28*/ le32 client_name_length;/* Length of client name in bytes. Should 270 - always be 8. */ 271 - /* 32*/ ntfschar client_name[64];/* Name of the client in Unicode. Should 272 - always be "NTFS" with the remaining bytes 273 - set to 0. */ 274 - /* sizeof() = 160 (0xa0) bytes */ 275 - } __attribute__ ((__packed__)) LOG_CLIENT_RECORD; 239 + struct log_client_record { 240 + __le64 oldest_lsn; 241 + __le64 client_restart_lsn; 242 + __le16 prev_client; 243 + __le16 next_client; 244 + __le16 seq_number; 245 + u8 reserved[6]; 246 + __le32 client_name_length; 247 + __le16 client_name[64]; 248 + } __packed; 276 249 277 - extern bool ntfs_check_logfile(struct inode *log_vi, 278 - RESTART_PAGE_HEADER **rp); 279 - 280 - extern bool ntfs_is_logfile_clean(struct inode *log_vi, 281 - const RESTART_PAGE_HEADER *rp); 282 - 283 - extern bool ntfs_empty_logfile(struct inode *log_vi); 284 - 285 - #endif /* NTFS_RW */ 286 - 250 + bool ntfs_check_logfile(struct inode *log_vi, 251 + struct restart_page_header **rp); 252 + bool ntfs_empty_logfile(struct inode *log_vi); 287 253 #endif /* _LINUX_NTFS_LOGFILE_H */
+32 -48
fs/ntfs/mft.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * mft.h - Defines for mft record handling in NTFS Linux kernel driver. 4 - * Part of the Linux-NTFS project. 3 + * Defines for mft record handling in NTFS Linux kernel driver. 5 4 * 6 5 * Copyright (c) 2001-2004 Anton Altaparmakov 7 6 */ ··· 8 9 #ifndef _LINUX_NTFS_MFT_H 9 10 #define _LINUX_NTFS_MFT_H 10 11 11 - #include <linux/fs.h> 12 12 #include <linux/highmem.h> 13 13 #include <linux/pagemap.h> 14 14 15 15 #include "inode.h" 16 16 17 - extern MFT_RECORD *map_mft_record(ntfs_inode *ni); 18 - extern void unmap_mft_record(ntfs_inode *ni); 17 + struct mft_record *map_mft_record(struct ntfs_inode *ni); 18 + void unmap_mft_record(struct ntfs_inode *ni); 19 + struct mft_record *map_extent_mft_record(struct ntfs_inode *base_ni, u64 mref, 20 + struct ntfs_inode **ntfs_ino); 19 21 20 - extern MFT_RECORD *map_extent_mft_record(ntfs_inode *base_ni, MFT_REF mref, 21 - ntfs_inode **ntfs_ino); 22 - 23 - static inline void unmap_extent_mft_record(ntfs_inode *ni) 22 + static inline void unmap_extent_mft_record(struct ntfs_inode *ni) 24 23 { 25 24 unmap_mft_record(ni); 26 - return; 27 25 } 28 26 29 - #ifdef NTFS_RW 27 + void __mark_mft_record_dirty(struct ntfs_inode *ni); 30 28 31 - /** 32 - * flush_dcache_mft_record_page - flush_dcache_page() for mft records 33 - * @ni: ntfs inode structure of mft record 34 - * 35 - * Call flush_dcache_page() for the page in which an mft record resides. 36 - * 37 - * This must be called every time an mft record is modified, just after the 38 - * modification. 39 - */ 40 - static inline void flush_dcache_mft_record_page(ntfs_inode *ni) 41 - { 42 - flush_dcache_page(ni->page); 43 - } 44 - 45 - extern void __mark_mft_record_dirty(ntfs_inode *ni); 46 - 47 - /** 29 + /* 48 30 * mark_mft_record_dirty - set the mft record and the page containing it dirty 49 31 * @ni: ntfs inode describing the mapped mft record 50 32 * ··· 36 56 * 37 57 * NOTE: Do not do anything if the mft record is already marked dirty. 38 58 */ 39 - static inline void mark_mft_record_dirty(ntfs_inode *ni) 59 + static inline void mark_mft_record_dirty(struct ntfs_inode *ni) 40 60 { 41 61 if (!NInoTestSetDirty(ni)) 42 62 __mark_mft_record_dirty(ni); 43 63 } 44 64 45 - extern int ntfs_sync_mft_mirror(ntfs_volume *vol, const unsigned long mft_no, 46 - MFT_RECORD *m, int sync); 65 + int ntfs_sync_mft_mirror(struct ntfs_volume *vol, const unsigned long mft_no, 66 + struct mft_record *m); 67 + int write_mft_record_nolock(struct ntfs_inode *ni, struct mft_record *m, int sync); 47 68 48 - extern int write_mft_record_nolock(ntfs_inode *ni, MFT_RECORD *m, int sync); 49 - 50 - /** 69 + /* 51 70 * write_mft_record - write out a mapped (extent) mft record 52 71 * @ni: ntfs inode describing the mapped (extent) mft record 53 72 * @m: mapped (extent) mft record to write ··· 64 85 * On success, clean the mft record and return 0. On error, leave the mft 65 86 * record dirty and return -errno. 66 87 */ 67 - static inline int write_mft_record(ntfs_inode *ni, MFT_RECORD *m, int sync) 88 + static inline int write_mft_record(struct ntfs_inode *ni, struct mft_record *m, int sync) 68 89 { 69 - struct page *page = ni->page; 90 + struct folio *folio = ni->folio; 70 91 int err; 71 92 72 - BUG_ON(!page); 73 - lock_page(page); 93 + folio_lock(folio); 74 94 err = write_mft_record_nolock(ni, m, sync); 75 - unlock_page(page); 95 + folio_unlock(folio); 96 + 76 97 return err; 77 98 } 78 99 79 - extern bool ntfs_may_write_mft_record(ntfs_volume *vol, 80 - const unsigned long mft_no, const MFT_RECORD *m, 81 - ntfs_inode **locked_ni); 82 - 83 - extern ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode, 84 - ntfs_inode *base_ni, MFT_RECORD **mrec); 85 - extern int ntfs_extent_mft_record_free(ntfs_inode *ni, MFT_RECORD *m); 86 - 87 - #endif /* NTFS_RW */ 100 + bool ntfs_may_write_mft_record(struct ntfs_volume *vol, 101 + const unsigned long mft_no, const struct mft_record *m, 102 + struct ntfs_inode **locked_ni, struct inode **ref_vi); 103 + int ntfs_mft_record_alloc(struct ntfs_volume *vol, const int mode, 104 + struct ntfs_inode **ni, struct ntfs_inode *base_ni, 105 + struct mft_record **ni_mrec); 106 + int ntfs_mft_record_free(struct ntfs_volume *vol, struct ntfs_inode *ni); 107 + int ntfs_mft_records_write(const struct ntfs_volume *vol, const u64 mref, 108 + const s64 count, struct mft_record *b); 109 + int ntfs_mft_record_check(const struct ntfs_volume *vol, struct mft_record *m, 110 + unsigned long mft_no); 111 + int ntfs_mft_writepages(struct address_space *mapping, 112 + struct writeback_control *wbc); 113 + void ntfs_mft_mark_dirty(struct folio *folio); 88 114 89 115 #endif /* _LINUX_NTFS_MFT_H */
+191 -47
fs/ntfs/ntfs.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * ntfs.h - Defines for NTFS Linux kernel driver. 3 + * Defines for NTFS Linux kernel driver. 4 4 * 5 5 * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc. 6 6 * Copyright (C) 2002 Richard Russon 7 + * Copyright (c) 2025 LG Electronics Co., Ltd. 7 8 */ 8 9 9 10 #ifndef _LINUX_NTFS_H ··· 12 11 13 12 #include <linux/stddef.h> 14 13 #include <linux/kernel.h> 14 + #include <linux/hex.h> 15 15 #include <linux/module.h> 16 16 #include <linux/compiler.h> 17 17 #include <linux/fs.h> 18 18 #include <linux/nls.h> 19 19 #include <linux/smp.h> 20 20 #include <linux/pagemap.h> 21 + #include <linux/uidgid.h> 21 22 22 - #include "types.h" 23 23 #include "volume.h" 24 24 #include "layout.h" 25 + #include "inode.h" 25 26 26 - typedef enum { 27 + #ifdef pr_fmt 28 + #undef pr_fmt 29 + #endif 30 + 31 + #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 32 + 33 + /* 34 + * Default pre-allocation size is optimize runlist merge overhead 35 + * with small chunk size. 36 + */ 37 + #define NTFS_DEF_PREALLOC_SIZE 65536 38 + 39 + /* 40 + * The log2 of the standard number of clusters per compression block. 41 + * A value of 4 corresponds to 16 clusters (1 << 4), which is the 42 + * default chunk size used by NTFS LZNT1 compression. 43 + */ 44 + #define STANDARD_COMPRESSION_UNIT 4 45 + 46 + /* 47 + * The maximum cluster size (4KB) allowed for compression to be enabled. 48 + * By design, NTFS does not support compression on volumes where the 49 + * cluster size exceeds 4096 bytes. 50 + */ 51 + #define MAX_COMPRESSION_CLUSTER_SIZE 4096 52 + 53 + #define NTFS_B_TO_CLU(vol, b) ((b) >> (vol)->cluster_size_bits) 54 + #define NTFS_CLU_TO_B(vol, clu) ((u64)(clu) << (vol)->cluster_size_bits) 55 + #define NTFS_B_TO_CLU_OFS(vol, clu) ((u64)(clu) & (vol)->cluster_size_mask) 56 + 57 + #define NTFS_MFT_NR_TO_CLU(vol, mft_no) (((u64)mft_no << (vol)->mft_record_size_bits) >> \ 58 + (vol)->cluster_size_bits) 59 + #define NTFS_MFT_NR_TO_PIDX(vol, mft_no) (mft_no >> (PAGE_SHIFT - \ 60 + (vol)->mft_record_size_bits)) 61 + #define NTFS_MFT_NR_TO_POFS(vol, mft_no) (((u64)mft_no << (vol)->mft_record_size_bits) & \ 62 + ~PAGE_MASK) 63 + 64 + #define NTFS_PIDX_TO_BLK(vol, idx) (((u64)idx << PAGE_SHIFT) >> \ 65 + ((vol)->sb)->s_blocksize_bits) 66 + #define NTFS_PIDX_TO_CLU(vol, idx) (((u64)idx << PAGE_SHIFT) >> \ 67 + (vol)->cluster_size_bits) 68 + #define NTFS_CLU_TO_PIDX(vol, clu) (((u64)(clu) << (vol)->cluster_size_bits) >> \ 69 + PAGE_SHIFT) 70 + #define NTFS_CLU_TO_POFS(vol, clu) (((u64)(clu) << (vol)->cluster_size_bits) & \ 71 + ~PAGE_MASK) 72 + 73 + #define NTFS_B_TO_SECTOR(vol, b) ((b) >> ((vol)->sb)->s_blocksize_bits) 74 + 75 + enum { 27 76 NTFS_BLOCK_SIZE = 512, 28 77 NTFS_BLOCK_SIZE_BITS = 9, 29 78 NTFS_SB_MAGIC = 0x5346544e, /* 'NTFS' */ 30 79 NTFS_MAX_NAME_LEN = 255, 31 - NTFS_MAX_ATTR_NAME_LEN = 255, 32 - NTFS_MAX_CLUSTER_SIZE = 64 * 1024, /* 64kiB */ 33 - NTFS_MAX_PAGES_PER_CLUSTER = NTFS_MAX_CLUSTER_SIZE / PAGE_SIZE, 34 - } NTFS_CONSTANTS; 80 + NTFS_MAX_LABEL_LEN = 128, 81 + }; 82 + 83 + enum { 84 + CASE_SENSITIVE = 0, 85 + IGNORE_CASE = 1, 86 + }; 87 + 88 + /* 89 + * Conversion helpers for NTFS units. 90 + */ 91 + 92 + /* Convert bytes to cluster count */ 93 + static inline u64 ntfs_bytes_to_cluster(const struct ntfs_volume *vol, 94 + s64 bytes) 95 + { 96 + return bytes >> vol->cluster_size_bits; 97 + } 98 + 99 + /* Convert cluster count to bytes */ 100 + static inline u64 ntfs_cluster_to_bytes(const struct ntfs_volume *vol, 101 + u64 clusters) 102 + { 103 + return clusters << vol->cluster_size_bits; 104 + } 105 + 106 + /* Get the byte offset within a cluster from a linear byte address */ 107 + static inline u64 ntfs_bytes_to_cluster_off(const struct ntfs_volume *vol, 108 + u64 bytes) 109 + { 110 + return bytes & vol->cluster_size_mask; 111 + } 112 + 113 + /* Calculate the physical cluster number containing a specific MFT record. */ 114 + static inline u64 ntfs_mft_no_to_cluster(const struct ntfs_volume *vol, 115 + unsigned long mft_no) 116 + { 117 + return ((u64)mft_no << vol->mft_record_size_bits) >> 118 + vol->cluster_size_bits; 119 + } 120 + 121 + /* Calculate the folio index where the MFT record resides. */ 122 + static inline pgoff_t ntfs_mft_no_to_pidx(const struct ntfs_volume *vol, 123 + unsigned long mft_no) 124 + { 125 + return mft_no >> (PAGE_SHIFT - vol->mft_record_size_bits); 126 + } 127 + 128 + /* Calculate the byte offset within a folio for an MFT record. */ 129 + static inline u64 ntfs_mft_no_to_poff(const struct ntfs_volume *vol, 130 + unsigned long mft_no) 131 + { 132 + return ((u64)mft_no << vol->mft_record_size_bits) & ~PAGE_MASK; 133 + } 134 + 135 + /* Convert folio index to cluster number. */ 136 + static inline u64 ntfs_pidx_to_cluster(const struct ntfs_volume *vol, 137 + pgoff_t idx) 138 + { 139 + return ((u64)idx << PAGE_SHIFT) >> vol->cluster_size_bits; 140 + } 141 + 142 + /* Convert cluster number to folio index. */ 143 + static inline pgoff_t ntfs_cluster_to_pidx(const struct ntfs_volume *vol, 144 + u64 clu) 145 + { 146 + return (clu << vol->cluster_size_bits) >> PAGE_SHIFT; 147 + } 148 + 149 + /* Get the byte offset within a folio from a cluster number */ 150 + static inline u64 ntfs_cluster_to_poff(const struct ntfs_volume *vol, 151 + u64 clu) 152 + { 153 + return (clu << vol->cluster_size_bits) & ~PAGE_MASK; 154 + } 155 + 156 + /* Convert byte offset to sector (block) number. */ 157 + static inline sector_t ntfs_bytes_to_sector(const struct ntfs_volume *vol, 158 + u64 bytes) 159 + { 160 + return bytes >> vol->sb->s_blocksize_bits; 161 + } 35 162 36 163 /* Global variables. */ 37 164 ··· 171 42 extern struct kmem_cache *ntfs_index_ctx_cache; 172 43 173 44 /* The various operations structs defined throughout the driver files. */ 174 - extern const struct address_space_operations ntfs_normal_aops; 175 - extern const struct address_space_operations ntfs_compressed_aops; 176 - extern const struct address_space_operations ntfs_mst_aops; 45 + extern const struct address_space_operations ntfs_aops; 46 + extern const struct address_space_operations ntfs_mft_aops; 177 47 178 48 extern const struct file_operations ntfs_file_ops; 179 49 extern const struct inode_operations ntfs_file_inode_ops; 50 + extern const struct inode_operations ntfs_symlink_inode_operations; 51 + extern const struct inode_operations ntfs_special_inode_operations; 180 52 181 53 extern const struct file_operations ntfs_dir_ops; 182 54 extern const struct inode_operations ntfs_dir_inode_ops; ··· 187 57 188 58 extern const struct export_operations ntfs_export_ops; 189 59 190 - /** 60 + /* 191 61 * NTFS_SB - return the ntfs volume given a vfs super block 192 62 * @sb: VFS super block 193 63 * 194 64 * NTFS_SB() returns the ntfs volume associated with the VFS super block @sb. 195 65 */ 196 - static inline ntfs_volume *NTFS_SB(struct super_block *sb) 66 + static inline struct ntfs_volume *NTFS_SB(struct super_block *sb) 197 67 { 198 68 return sb->s_fs_info; 199 69 } ··· 201 71 /* Declarations of functions and global variables. */ 202 72 203 73 /* From fs/ntfs/compress.c */ 204 - extern int ntfs_read_compressed_block(struct page *page); 205 - extern int allocate_compression_buffers(void); 206 - extern void free_compression_buffers(void); 74 + int ntfs_read_compressed_block(struct folio *folio); 75 + int allocate_compression_buffers(void); 76 + void free_compression_buffers(void); 77 + int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count, 78 + struct iov_iter *from); 207 79 208 80 /* From fs/ntfs/super.c */ 209 81 #define default_upcase_len 0x10000 210 82 extern struct mutex ntfs_lock; 211 83 212 - typedef struct { 84 + struct option_t { 213 85 int val; 214 86 char *str; 215 - } option_t; 216 - extern const option_t on_errors_arr[]; 87 + }; 88 + extern const struct option_t on_errors_arr[]; 89 + int ntfs_set_volume_flags(struct ntfs_volume *vol, __le16 flags); 90 + int ntfs_clear_volume_flags(struct ntfs_volume *vol, __le16 flags); 91 + int ntfs_write_volume_label(struct ntfs_volume *vol, char *label); 217 92 218 93 /* From fs/ntfs/mst.c */ 219 - extern int post_read_mst_fixup(NTFS_RECORD *b, const u32 size); 220 - extern int pre_write_mst_fixup(NTFS_RECORD *b, const u32 size); 221 - extern void post_write_mst_fixup(NTFS_RECORD *b); 94 + int post_read_mst_fixup(struct ntfs_record *b, const u32 size); 95 + int pre_write_mst_fixup(struct ntfs_record *b, const u32 size); 96 + void post_write_mst_fixup(struct ntfs_record *b); 222 97 223 98 /* From fs/ntfs/unistr.c */ 224 - extern bool ntfs_are_names_equal(const ntfschar *s1, size_t s1_len, 225 - const ntfschar *s2, size_t s2_len, 226 - const IGNORE_CASE_BOOL ic, 227 - const ntfschar *upcase, const u32 upcase_size); 228 - extern int ntfs_collate_names(const ntfschar *name1, const u32 name1_len, 229 - const ntfschar *name2, const u32 name2_len, 230 - const int err_val, const IGNORE_CASE_BOOL ic, 231 - const ntfschar *upcase, const u32 upcase_len); 232 - extern int ntfs_ucsncmp(const ntfschar *s1, const ntfschar *s2, size_t n); 233 - extern int ntfs_ucsncasecmp(const ntfschar *s1, const ntfschar *s2, size_t n, 234 - const ntfschar *upcase, const u32 upcase_size); 235 - extern void ntfs_upcase_name(ntfschar *name, u32 name_len, 236 - const ntfschar *upcase, const u32 upcase_len); 237 - extern void ntfs_file_upcase_value(FILE_NAME_ATTR *file_name_attr, 238 - const ntfschar *upcase, const u32 upcase_len); 239 - extern int ntfs_file_compare_values(FILE_NAME_ATTR *file_name_attr1, 240 - FILE_NAME_ATTR *file_name_attr2, 241 - const int err_val, const IGNORE_CASE_BOOL ic, 242 - const ntfschar *upcase, const u32 upcase_len); 243 - extern int ntfs_nlstoucs(const ntfs_volume *vol, const char *ins, 244 - const int ins_len, ntfschar **outs); 245 - extern int ntfs_ucstonls(const ntfs_volume *vol, const ntfschar *ins, 99 + bool ntfs_are_names_equal(const __le16 *s1, size_t s1_len, 100 + const __le16 *s2, size_t s2_len, 101 + const u32 ic, 102 + const __le16 *upcase, const u32 upcase_size); 103 + int ntfs_collate_names(const __le16 *name1, const u32 name1_len, 104 + const __le16 *name2, const u32 name2_len, 105 + const int err_val, const u32 ic, 106 + const __le16 *upcase, const u32 upcase_len); 107 + int ntfs_ucsncmp(const __le16 *s1, const __le16 *s2, size_t n); 108 + int ntfs_ucsncasecmp(const __le16 *s1, const __le16 *s2, size_t n, 109 + const __le16 *upcase, const u32 upcase_size); 110 + int ntfs_file_compare_values(const struct file_name_attr *file_name_attr1, 111 + const struct file_name_attr *file_name_attr2, 112 + const int err_val, const u32 ic, 113 + const __le16 *upcase, const u32 upcase_len); 114 + int ntfs_nlstoucs(const struct ntfs_volume *vol, const char *ins, 115 + const int ins_len, __le16 **outs, int max_name_len); 116 + int ntfs_ucstonls(const struct ntfs_volume *vol, const __le16 *ins, 246 117 const int ins_len, unsigned char **outs, int outs_len); 118 + __le16 *ntfs_ucsndup(const __le16 *s, u32 maxlen); 119 + bool ntfs_names_are_equal(const __le16 *s1, size_t s1_len, 120 + const __le16 *s2, size_t s2_len, 121 + const u32 ic, 122 + const __le16 *upcase, const u32 upcase_size); 123 + int ntfs_force_shutdown(struct super_block *sb, u32 flags); 124 + long ntfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg); 125 + #ifdef CONFIG_COMPAT 126 + long ntfs_compat_ioctl(struct file *filp, unsigned int cmd, 127 + unsigned long arg); 128 + #endif 247 129 248 130 /* From fs/ntfs/upcase.c */ 249 - extern ntfschar *generate_default_upcase(void); 131 + __le16 *generate_default_upcase(void); 250 132 251 133 static inline int ntfs_ffs(int x) 252 134 { ··· 282 140 x >>= 2; 283 141 r += 2; 284 142 } 285 - if (!(x & 1)) { 286 - x >>= 1; 143 + if (!(x & 1)) 287 144 r += 1; 288 - } 289 145 return r; 290 146 } 147 + 148 + /* From fs/ntfs/bdev-io.c */ 149 + int ntfs_bdev_read(struct block_device *bdev, char *data, loff_t start, size_t size); 150 + int ntfs_bdev_write(struct super_block *sb, void *buf, loff_t start, size_t size); 291 151 292 152 #endif /* _LINUX_NTFS_H */
+14
fs/ntfs/object_id.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * Copyright (c) 2008-2021 Jean-Pierre Andre 4 + * Copyright (c) 2025 LG Electronics Co., Ltd. 5 + */ 6 + 7 + #ifndef _LINUX_NTFS_OBJECT_ID_H 8 + #define _LINUX_NTFS_OBJECT_ID_H 9 + 10 + extern __le16 objid_index_name[]; 11 + 12 + int ntfs_delete_object_id_index(struct ntfs_inode *ni); 13 + 14 + #endif /* _LINUX_NTFS_OBJECT_ID_H */
+2 -8
fs/ntfs/quota.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * quota.h - Defines for NTFS kernel quota ($Quota) handling. Part of the 4 - * Linux-NTFS project. 3 + * Defines for NTFS kernel quota ($Quota) handling. 5 4 * 6 5 * Copyright (c) 2004 Anton Altaparmakov 7 6 */ ··· 8 9 #ifndef _LINUX_NTFS_QUOTA_H 9 10 #define _LINUX_NTFS_QUOTA_H 10 11 11 - #ifdef NTFS_RW 12 - 13 - #include "types.h" 14 12 #include "volume.h" 15 13 16 - extern bool ntfs_mark_quotas_out_of_date(ntfs_volume *vol); 17 - 18 - #endif /* NTFS_RW */ 14 + bool ntfs_mark_quotas_out_of_date(struct ntfs_volume *vol); 19 15 20 16 #endif /* _LINUX_NTFS_QUOTA_H */
+20
fs/ntfs/reparse.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + /* 3 + * Copyright (c) 2008-2021 Jean-Pierre Andre 4 + * Copyright (c) 2025 LG Electronics Co., Ltd. 5 + */ 6 + 7 + #ifndef _LINUX_NTFS_REPARSE_H 8 + #define _LINUX_NTFS_REPARSE_H 9 + 10 + extern __le16 reparse_index_name[]; 11 + 12 + unsigned int ntfs_make_symlink(struct ntfs_inode *ni); 13 + unsigned int ntfs_reparse_tag_dt_types(struct ntfs_volume *vol, unsigned long mref); 14 + int ntfs_reparse_set_wsl_symlink(struct ntfs_inode *ni, 15 + const __le16 *target, int target_len); 16 + int ntfs_reparse_set_wsl_not_symlink(struct ntfs_inode *ni, mode_t mode); 17 + int ntfs_delete_reparse_index(struct ntfs_inode *ni); 18 + int ntfs_remove_ntfs_reparse_data(struct ntfs_inode *ni); 19 + 20 + #endif /* _LINUX_NTFS_REPARSE_H */
+61 -52
fs/ntfs/runlist.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * runlist.h - Defines for runlist handling in NTFS Linux kernel driver. 4 - * Part of the Linux-NTFS project. 3 + * Defines for runlist handling in NTFS Linux kernel driver. 5 4 * 6 5 * Copyright (c) 2001-2005 Anton Altaparmakov 7 6 * Copyright (c) 2002 Richard Russon 7 + * Copyright (c) 2025 LG Electronics Co., Ltd. 8 8 */ 9 9 10 10 #ifndef _LINUX_NTFS_RUNLIST_H 11 11 #define _LINUX_NTFS_RUNLIST_H 12 12 13 - #include "types.h" 14 - #include "layout.h" 15 13 #include "volume.h" 16 14 17 - /** 15 + /* 18 16 * runlist_element - in memory vcn to lcn mapping array element 19 17 * @vcn: starting vcn of the current array element 20 18 * @lcn: starting lcn of the current array element ··· 22 24 * 23 25 * When lcn == -1 this means that the count vcns starting at vcn are not 24 26 * physically allocated (i.e. this is a hole / data is sparse). 27 + * 28 + * In memory vcn to lcn mapping structure element. 29 + * @vcn: vcn = Starting virtual cluster number. 30 + * @lcn: lcn = Starting logical cluster number. 31 + * @length: Run length in clusters. 25 32 */ 26 - typedef struct { /* In memory vcn to lcn mapping structure element. */ 27 - VCN vcn; /* vcn = Starting virtual cluster number. */ 28 - LCN lcn; /* lcn = Starting logical cluster number. */ 29 - s64 length; /* Run length in clusters. */ 30 - } runlist_element; 33 + struct runlist_element { 34 + s64 vcn; 35 + s64 lcn; 36 + s64 length; 37 + }; 31 38 32 - /** 39 + /* 33 40 * runlist - in memory vcn to lcn mapping array including a read/write lock 34 41 * @rl: pointer to an array of runlist elements 35 42 * @lock: read/write spinlock for serializing access to @rl 36 - * 43 + * @rl_hint: hint/cache pointing to the last accessed runlist element 37 44 */ 38 - typedef struct { 39 - runlist_element *rl; 45 + struct runlist { 46 + struct runlist_element *rl; 40 47 struct rw_semaphore lock; 41 - } runlist; 48 + size_t count; 49 + int rl_hint; 50 + }; 42 51 43 - static inline void ntfs_init_runlist(runlist *rl) 52 + static inline void ntfs_init_runlist(struct runlist *rl) 44 53 { 45 54 rl->rl = NULL; 46 55 init_rwsem(&rl->lock); 56 + rl->count = 0; 57 + rl->rl_hint = -1; 47 58 } 48 59 49 - typedef enum { 50 - LCN_HOLE = -1, /* Keep this as highest value or die! */ 51 - LCN_RL_NOT_MAPPED = -2, 52 - LCN_ENOENT = -3, 53 - LCN_ENOMEM = -4, 54 - LCN_EIO = -5, 55 - } LCN_SPECIAL_VALUES; 60 + enum { 61 + LCN_DELALLOC = -1, 62 + LCN_HOLE = -2, 63 + LCN_RL_NOT_MAPPED = -3, 64 + LCN_ENOENT = -4, 65 + LCN_ENOMEM = -5, 66 + LCN_EIO = -6, 67 + LCN_EINVAL = -7, 68 + }; 56 69 57 - extern runlist_element *ntfs_runlists_merge(runlist_element *drl, 58 - runlist_element *srl); 59 - 60 - extern runlist_element *ntfs_mapping_pairs_decompress(const ntfs_volume *vol, 61 - const ATTR_RECORD *attr, runlist_element *old_rl); 62 - 63 - extern LCN ntfs_rl_vcn_to_lcn(const runlist_element *rl, const VCN vcn); 64 - 65 - #ifdef NTFS_RW 66 - 67 - extern runlist_element *ntfs_rl_find_vcn_nolock(runlist_element *rl, 68 - const VCN vcn); 69 - 70 - extern int ntfs_get_size_for_mapping_pairs(const ntfs_volume *vol, 71 - const runlist_element *rl, const VCN first_vcn, 72 - const VCN last_vcn); 73 - 74 - extern int ntfs_mapping_pairs_build(const ntfs_volume *vol, s8 *dst, 75 - const int dst_len, const runlist_element *rl, 76 - const VCN first_vcn, const VCN last_vcn, VCN *const stop_vcn); 77 - 78 - extern int ntfs_rl_truncate_nolock(const ntfs_volume *vol, 79 - runlist *const runlist, const s64 new_length); 80 - 81 - int ntfs_rl_punch_nolock(const ntfs_volume *vol, runlist *const runlist, 82 - const VCN start, const s64 length); 83 - 84 - #endif /* NTFS_RW */ 85 - 70 + struct runlist_element *ntfs_runlists_merge(struct runlist *d_runlist, 71 + struct runlist_element *srl, size_t s_rl_count, 72 + size_t *new_rl_count); 73 + struct runlist_element *ntfs_mapping_pairs_decompress(const struct ntfs_volume *vol, 74 + const struct attr_record *attr, struct runlist *old_runlist, 75 + size_t *new_rl_count); 76 + s64 ntfs_rl_vcn_to_lcn(const struct runlist_element *rl, const s64 vcn); 77 + struct runlist_element *ntfs_rl_find_vcn_nolock(struct runlist_element *rl, const s64 vcn); 78 + int ntfs_get_size_for_mapping_pairs(const struct ntfs_volume *vol, 79 + const struct runlist_element *rl, const s64 first_vcn, 80 + const s64 last_vcn, int max_mp_size); 81 + int ntfs_mapping_pairs_build(const struct ntfs_volume *vol, s8 *dst, 82 + const int dst_len, const struct runlist_element *rl, 83 + const s64 first_vcn, const s64 last_vcn, s64 *const stop_vcn, 84 + struct runlist_element **stop_rl, unsigned int *de_cluster_count); 85 + int ntfs_rl_truncate_nolock(const struct ntfs_volume *vol, 86 + struct runlist *const runlist, const s64 new_length); 87 + int ntfs_rl_sparse(struct runlist_element *rl); 88 + s64 ntfs_rl_get_compressed_size(struct ntfs_volume *vol, struct runlist_element *rl); 89 + struct runlist_element *ntfs_rl_insert_range(struct runlist_element *dst_rl, int dst_cnt, 90 + struct runlist_element *src_rl, int src_cnt, size_t *new_cnt); 91 + struct runlist_element *ntfs_rl_punch_hole(struct runlist_element *dst_rl, int dst_cnt, 92 + s64 start_vcn, s64 len, struct runlist_element **punch_rl, 93 + size_t *new_rl_cnt); 94 + struct runlist_element *ntfs_rl_collapse_range(struct runlist_element *dst_rl, int dst_cnt, 95 + s64 start_vcn, s64 len, struct runlist_element **punch_rl, 96 + size_t *new_rl_cnt); 97 + struct runlist_element *ntfs_rl_realloc(struct runlist_element *rl, int old_size, 98 + int new_size); 86 99 #endif /* _LINUX_NTFS_RUNLIST_H */
+3 -4
fs/ntfs/sysctl.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * sysctl.h - Defines for sysctl handling in NTFS Linux kernel driver. Part of 4 - * the Linux-NTFS project. Adapted from the old NTFS driver, 5 - * Copyright (C) 1997 Martin von Löwis, Régis Duchesne 3 + * Defines for sysctl handling in NTFS Linux kernel driver. 6 4 * 5 + * Copyright (C) 1997 Martin von Löwis, Régis Duchesne 7 6 * Copyright (c) 2002-2004 Anton Altaparmakov 8 7 */ 9 8 ··· 12 13 13 14 #if defined(DEBUG) && defined(CONFIG_SYSCTL) 14 15 15 - extern int ntfs_sysctl(int add); 16 + int ntfs_sysctl(int add); 16 17 17 18 #else 18 19
+15 -17
fs/ntfs/time.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * time.h - NTFS time conversion functions. Part of the Linux-NTFS project. 3 + * NTFS time conversion functions. 4 4 * 5 5 * Copyright (c) 2001-2005 Anton Altaparmakov 6 6 */ ··· 8 8 #ifndef _LINUX_NTFS_TIME_H 9 9 #define _LINUX_NTFS_TIME_H 10 10 11 - #include <linux/time.h> /* For current_kernel_time(). */ 11 + #include <linux/time.h> 12 12 #include <asm/div64.h> /* For do_div(). */ 13 13 14 - #include "endian.h" 14 + #define NTFS_TIME_OFFSET ((s64)(369 * 365 + 89) * 24 * 3600) 15 15 16 - #define NTFS_TIME_OFFSET ((s64)(369 * 365 + 89) * 24 * 3600 * 10000000) 17 - 18 - /** 16 + /* 19 17 * utc2ntfs - convert Linux UTC time to NTFS time 20 18 * @ts: Linux UTC time to convert to NTFS time 21 19 * ··· 29 31 * measured as the number of 100-nano-second intervals since 1st January 1601, 30 32 * 00:00:00 UTC. 31 33 */ 32 - static inline sle64 utc2ntfs(const struct timespec64 ts) 34 + static inline __le64 utc2ntfs(const struct timespec64 ts) 33 35 { 34 36 /* 35 37 * Convert the seconds to 100ns intervals, add the nano-seconds 36 38 * converted to 100ns intervals, and then add the NTFS time offset. 37 39 */ 38 - return cpu_to_sle64((s64)ts.tv_sec * 10000000 + ts.tv_nsec / 100 + 39 - NTFS_TIME_OFFSET); 40 + return cpu_to_le64((u64)(ts.tv_sec + NTFS_TIME_OFFSET) * 10000000 + 41 + ts.tv_nsec / 100); 40 42 } 41 43 42 - /** 44 + /* 43 45 * get_current_ntfs_time - get the current time in little endian NTFS format 44 46 * 45 47 * Get the current time from the Linux kernel, convert it to its corresponding 46 48 * NTFS time and return that in little endian format. 47 49 */ 48 - static inline sle64 get_current_ntfs_time(void) 50 + static inline __le64 get_current_ntfs_time(void) 49 51 { 50 52 struct timespec64 ts; 51 53 ··· 53 55 return utc2ntfs(ts); 54 56 } 55 57 56 - /** 58 + /* 57 59 * ntfs2utc - convert NTFS time to Linux time 58 60 * @time: NTFS time (little endian) to convert to Linux UTC 59 61 * ··· 69 71 * measured as the number of 100 nano-second intervals since 1st January 1601, 70 72 * 00:00:00 UTC. 71 73 */ 72 - static inline struct timespec64 ntfs2utc(const sle64 time) 74 + static inline struct timespec64 ntfs2utc(const __le64 time) 73 75 { 74 76 struct timespec64 ts; 77 + s32 t32; 75 78 76 79 /* Subtract the NTFS time offset. */ 77 - u64 t = (u64)(sle64_to_cpu(time) - NTFS_TIME_OFFSET); 80 + s64 t = le64_to_cpu(time) - NTFS_TIME_OFFSET * 10000000; 78 81 /* 79 82 * Convert the time to 1-second intervals and the remainder to 80 83 * 1-nano-second intervals. 81 84 */ 82 - ts.tv_nsec = do_div(t, 10000000) * 100; 83 - ts.tv_sec = t; 85 + ts.tv_sec = div_s64_rem(t, 10000000, &t32); 86 + ts.tv_nsec = t32 * 100; 84 87 return ts; 85 88 } 86 - 87 89 #endif /* _LINUX_NTFS_TIME_H */
-55
fs/ntfs/types.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 - /* 3 - * types.h - Defines for NTFS Linux kernel driver specific types. 4 - * Part of the Linux-NTFS project. 5 - * 6 - * Copyright (c) 2001-2005 Anton Altaparmakov 7 - */ 8 - 9 - #ifndef _LINUX_NTFS_TYPES_H 10 - #define _LINUX_NTFS_TYPES_H 11 - 12 - #include <linux/types.h> 13 - 14 - typedef __le16 le16; 15 - typedef __le32 le32; 16 - typedef __le64 le64; 17 - typedef __u16 __bitwise sle16; 18 - typedef __u32 __bitwise sle32; 19 - typedef __u64 __bitwise sle64; 20 - 21 - /* 2-byte Unicode character type. */ 22 - typedef le16 ntfschar; 23 - #define UCHAR_T_SIZE_BITS 1 24 - 25 - /* 26 - * Clusters are signed 64-bit values on NTFS volumes. We define two types, LCN 27 - * and VCN, to allow for type checking and better code readability. 28 - */ 29 - typedef s64 VCN; 30 - typedef sle64 leVCN; 31 - typedef s64 LCN; 32 - typedef sle64 leLCN; 33 - 34 - /* 35 - * The NTFS journal $LogFile uses log sequence numbers which are signed 64-bit 36 - * values. We define our own type LSN, to allow for type checking and better 37 - * code readability. 38 - */ 39 - typedef s64 LSN; 40 - typedef sle64 leLSN; 41 - 42 - /* 43 - * The NTFS transaction log $UsnJrnl uses usn which are signed 64-bit values. 44 - * We define our own type USN, to allow for type checking and better code 45 - * readability. 46 - */ 47 - typedef s64 USN; 48 - typedef sle64 leUSN; 49 - 50 - typedef enum { 51 - CASE_SENSITIVE = 0, 52 - IGNORE_CASE = 1, 53 - } IGNORE_CASE_BOOL; 54 - 55 - #endif /* _LINUX_NTFS_TYPES_H */
+256 -124
fs/ntfs/volume.h
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * volume.h - Defines for volume structures in NTFS Linux kernel driver. Part 4 - * of the Linux-NTFS project. 3 + * Defines for volume structures in NTFS Linux kernel driver. 5 4 * 6 5 * Copyright (c) 2001-2006 Anton Altaparmakov 7 6 * Copyright (c) 2002 Richard Russon 7 + * Copyright (c) 2025 LG Electronics Co., Ltd. 8 8 */ 9 9 10 10 #ifndef _LINUX_NTFS_VOLUME_H 11 11 #define _LINUX_NTFS_VOLUME_H 12 12 13 13 #include <linux/rwsem.h> 14 + #include <linux/sched.h> 15 + #include <linux/wait.h> 14 16 #include <linux/uidgid.h> 17 + #include <linux/workqueue.h> 18 + #include <linux/errseq.h> 15 19 16 - #include "types.h" 17 20 #include "layout.h" 21 + 22 + #define NTFS_VOL_UID BIT(1) 23 + #define NTFS_VOL_GID BIT(2) 18 24 19 25 /* 20 26 * The NTFS in memory super block structure. 27 + * 28 + * @sb: Pointer back to the super_block. 29 + * @nr_blocks: Number of sb->s_blocksize bytes sized blocks on the device. 30 + * @flags: Miscellaneous flags, see below. 31 + * @uid: uid that files will be mounted as. 32 + * @gid: gid that files will be mounted as. 33 + * @fmask: The mask for file permissions. 34 + * @dmask: The mask for directory permissions. 35 + * @mft_zone_multiplier: Initial mft zone multiplier. 36 + * @on_errors: What to do on filesystem errors. 37 + * @wb_err: Writeback error tracking. 38 + * @sector_size: in bytes 39 + * @sector_size_bits: log2(sector_size) 40 + * @cluster_size: in bytes 41 + * @cluster_size_mask: cluster_size - 1 42 + * @cluster_size_bits: log2(cluster_size) 43 + * @mft_record_size: in bytes 44 + * @mft_record_size_mask: mft_record_size - 1 45 + * @mft_record_size_bits: log2(mft_record_size) 46 + * @index_record_size: in bytes 47 + * @index_record_size_mask: index_record_size - 1 48 + * @index_record_size_bits: log2(index_record_size) 49 + * @nr_clusters: Volume size in clusters == number of bits in lcn bitmap. 50 + * @mft_lcn: Cluster location of mft data. 51 + * @mftmirr_lcn: Cluster location of copy of mft. 52 + * @serial_no: The volume serial number. 53 + * @upcase_len: Number of entries in upcase[]. 54 + * @upcase: The upcase table. 55 + * @attrdef_size: Size of the attribute definition table in bytes. 56 + * @attrdef: Table of attribute definitions. Obtained from FILE_AttrDef. 57 + * @mft_data_pos: Mft record number at which to allocate the next mft record. 58 + * @mft_zone_start: First cluster of the mft zone. 59 + * @mft_zone_end: First cluster beyond the mft zone. 60 + * @mft_zone_pos: Current position in the mft zone. 61 + * @data1_zone_pos: Current position in the first data zone. 62 + * @data2_zone_pos: Current position in the second data zone. 63 + * @mft_ino: The VFS inode of $MFT. 64 + * @mftbmp_ino: Attribute inode for $MFT/$BITMAP. 65 + * @mftbmp_lock: Lock for serializing accesses to the mft record bitmap. 66 + * @mftmirr_ino: The VFS inode of $MFTMirr. 67 + * @mftmirr_size: Size of mft mirror in mft records. 68 + * @logfile_ino: The VFS inode of LogFile. 69 + * @lcnbmp_ino: The VFS inode of $Bitmap. 70 + * @lcnbmp_lock: Lock for serializing accesses to the cluster bitmap 71 + * @vol_ino: The VFS inode of $Volume. 72 + * @vol_flags: Volume flags. 73 + * @major_ver: Ntfs major version of volume. 74 + * @minor_ver: Ntfs minor version of volume. 75 + * @volume_label: volume label. 76 + * @root_ino: The VFS inode of the root directory. 77 + * @secure_ino: The VFS inode of $Secure (NTFS3.0+ only, otherwise NULL). 78 + * @extend_ino: The VFS inode of $Extend (NTFS3.0+ only, otherwise NULL). 79 + * @quota_ino: The VFS inode of $Quota. 80 + * @quota_q_ino: Attribute inode for $Quota/$Q. 81 + * @nls_map: NLS (National Language Support) table. 82 + * @nls_utf8: NLS table for UTF-8. 83 + * @free_waitq: Wait queue for threads waiting for free clusters or MFT records. 84 + * @free_clusters: Track the number of free clusters. 85 + * @free_mft_records: Track the free mft records. 86 + * @dirty_clusters: Number of clusters that are dirty. 87 + * @sparse_compression_unit: Size of compression/sparse unit in clusters. 88 + * @lcn_empty_bits_per_page: Number of empty bits per page in the LCN bitmap. 89 + * @precalc_work: Work structure for background pre-calculation tasks. 90 + * @preallocated_size: reallocation size (in bytes). 21 91 */ 22 - typedef struct { 23 - /* 24 - * FIXME: Reorder to have commonly used together element within the 25 - * same cache line, aiming at a cache line size of 32 bytes. Aim for 26 - * 64 bytes for less commonly used together elements. Put most commonly 27 - * used elements to front of structure. Obviously do this only when the 28 - * structure has stabilized... (AIA) 29 - */ 30 - /* Device specifics. */ 31 - struct super_block *sb; /* Pointer back to the super_block. */ 32 - LCN nr_blocks; /* Number of sb->s_blocksize bytes 33 - sized blocks on the device. */ 34 - /* Configuration provided by user at mount time. */ 35 - unsigned long flags; /* Miscellaneous flags, see below. */ 36 - kuid_t uid; /* uid that files will be mounted as. */ 37 - kgid_t gid; /* gid that files will be mounted as. */ 38 - umode_t fmask; /* The mask for file permissions. */ 39 - umode_t dmask; /* The mask for directory 40 - permissions. */ 41 - u8 mft_zone_multiplier; /* Initial mft zone multiplier. */ 42 - u8 on_errors; /* What to do on filesystem errors. */ 43 - /* NTFS bootsector provided information. */ 44 - u16 sector_size; /* in bytes */ 45 - u8 sector_size_bits; /* log2(sector_size) */ 46 - u32 cluster_size; /* in bytes */ 47 - u32 cluster_size_mask; /* cluster_size - 1 */ 48 - u8 cluster_size_bits; /* log2(cluster_size) */ 49 - u32 mft_record_size; /* in bytes */ 50 - u32 mft_record_size_mask; /* mft_record_size - 1 */ 51 - u8 mft_record_size_bits; /* log2(mft_record_size) */ 52 - u32 index_record_size; /* in bytes */ 53 - u32 index_record_size_mask; /* index_record_size - 1 */ 54 - u8 index_record_size_bits; /* log2(index_record_size) */ 55 - LCN nr_clusters; /* Volume size in clusters == number of 56 - bits in lcn bitmap. */ 57 - LCN mft_lcn; /* Cluster location of mft data. */ 58 - LCN mftmirr_lcn; /* Cluster location of copy of mft. */ 59 - u64 serial_no; /* The volume serial number. */ 60 - /* Mount specific NTFS information. */ 61 - u32 upcase_len; /* Number of entries in upcase[]. */ 62 - ntfschar *upcase; /* The upcase table. */ 63 - 64 - s32 attrdef_size; /* Size of the attribute definition 65 - table in bytes. */ 66 - ATTR_DEF *attrdef; /* Table of attribute definitions. 67 - Obtained from FILE_AttrDef. */ 68 - 69 - #ifdef NTFS_RW 70 - /* Variables used by the cluster and mft allocators. */ 71 - s64 mft_data_pos; /* Mft record number at which to 72 - allocate the next mft record. */ 73 - LCN mft_zone_start; /* First cluster of the mft zone. */ 74 - LCN mft_zone_end; /* First cluster beyond the mft zone. */ 75 - LCN mft_zone_pos; /* Current position in the mft zone. */ 76 - LCN data1_zone_pos; /* Current position in the first data 77 - zone. */ 78 - LCN data2_zone_pos; /* Current position in the second data 79 - zone. */ 80 - #endif /* NTFS_RW */ 81 - 82 - struct inode *mft_ino; /* The VFS inode of $MFT. */ 83 - 84 - struct inode *mftbmp_ino; /* Attribute inode for $MFT/$BITMAP. */ 85 - struct rw_semaphore mftbmp_lock; /* Lock for serializing accesses to the 86 - mft record bitmap ($MFT/$BITMAP). */ 87 - #ifdef NTFS_RW 88 - struct inode *mftmirr_ino; /* The VFS inode of $MFTMirr. */ 89 - int mftmirr_size; /* Size of mft mirror in mft records. */ 90 - 91 - struct inode *logfile_ino; /* The VFS inode of $LogFile. */ 92 - #endif /* NTFS_RW */ 93 - 94 - struct inode *lcnbmp_ino; /* The VFS inode of $Bitmap. */ 95 - struct rw_semaphore lcnbmp_lock; /* Lock for serializing accesses to the 96 - cluster bitmap ($Bitmap/$DATA). */ 97 - 98 - struct inode *vol_ino; /* The VFS inode of $Volume. */ 99 - VOLUME_FLAGS vol_flags; /* Volume flags. */ 100 - u8 major_ver; /* Ntfs major version of volume. */ 101 - u8 minor_ver; /* Ntfs minor version of volume. */ 102 - 103 - struct inode *root_ino; /* The VFS inode of the root 104 - directory. */ 105 - struct inode *secure_ino; /* The VFS inode of $Secure (NTFS3.0+ 106 - only, otherwise NULL). */ 107 - struct inode *extend_ino; /* The VFS inode of $Extend (NTFS3.0+ 108 - only, otherwise NULL). */ 109 - #ifdef NTFS_RW 110 - /* $Quota stuff is NTFS3.0+ specific. Unused/NULL otherwise. */ 111 - struct inode *quota_ino; /* The VFS inode of $Quota. */ 112 - struct inode *quota_q_ino; /* Attribute inode for $Quota/$Q. */ 113 - /* $UsnJrnl stuff is NTFS3.0+ specific. Unused/NULL otherwise. */ 114 - struct inode *usnjrnl_ino; /* The VFS inode of $UsnJrnl. */ 115 - struct inode *usnjrnl_max_ino; /* Attribute inode for $UsnJrnl/$Max. */ 116 - struct inode *usnjrnl_j_ino; /* Attribute inode for $UsnJrnl/$J. */ 117 - #endif /* NTFS_RW */ 92 + struct ntfs_volume { 93 + struct super_block *sb; 94 + s64 nr_blocks; 95 + unsigned long flags; 96 + kuid_t uid; 97 + kgid_t gid; 98 + umode_t fmask; 99 + umode_t dmask; 100 + u8 mft_zone_multiplier; 101 + u8 on_errors; 102 + errseq_t wb_err; 103 + u16 sector_size; 104 + u8 sector_size_bits; 105 + u32 cluster_size; 106 + u32 cluster_size_mask; 107 + u8 cluster_size_bits; 108 + u32 mft_record_size; 109 + u32 mft_record_size_mask; 110 + u8 mft_record_size_bits; 111 + u32 index_record_size; 112 + u32 index_record_size_mask; 113 + u8 index_record_size_bits; 114 + s64 nr_clusters; 115 + s64 mft_lcn; 116 + s64 mftmirr_lcn; 117 + u64 serial_no; 118 + u32 upcase_len; 119 + __le16 *upcase; 120 + s32 attrdef_size; 121 + struct attr_def *attrdef; 122 + s64 mft_data_pos; 123 + s64 mft_zone_start; 124 + s64 mft_zone_end; 125 + s64 mft_zone_pos; 126 + s64 data1_zone_pos; 127 + s64 data2_zone_pos; 128 + struct inode *mft_ino; 129 + struct inode *mftbmp_ino; 130 + struct rw_semaphore mftbmp_lock; 131 + struct inode *mftmirr_ino; 132 + int mftmirr_size; 133 + struct inode *logfile_ino; 134 + struct inode *lcnbmp_ino; 135 + struct rw_semaphore lcnbmp_lock; 136 + struct inode *vol_ino; 137 + __le16 vol_flags; 138 + u8 major_ver; 139 + u8 minor_ver; 140 + unsigned char *volume_label; 141 + struct inode *root_ino; 142 + struct inode *secure_ino; 143 + struct inode *extend_ino; 144 + struct inode *quota_ino; 145 + struct inode *quota_q_ino; 118 146 struct nls_table *nls_map; 119 - } ntfs_volume; 147 + bool nls_utf8; 148 + wait_queue_head_t free_waitq; 149 + atomic64_t free_clusters; 150 + atomic64_t free_mft_records; 151 + atomic64_t dirty_clusters; 152 + u8 sparse_compression_unit; 153 + unsigned int *lcn_empty_bits_per_page; 154 + struct work_struct precalc_work; 155 + loff_t preallocated_size; 156 + }; 120 157 121 158 /* 122 159 * Defined bits for the flags field in the ntfs_volume structure. 160 + * 161 + * NV_Errors Volume has errors, prevent remount rw. 162 + * NV_ShowSystemFiles Return system files in ntfs_readdir(). 163 + * NV_CaseSensitive Treat file names as case sensitive and 164 + * create filenames in the POSIX namespace. 165 + * Otherwise be case insensitive but still 166 + * create file names in POSIX namespace. 167 + * NV_LogFileEmpty LogFile journal is empty. 168 + * NV_QuotaOutOfDate Quota is out of date. 169 + * NV_UsnJrnlStamped UsnJrnl has been stamped. 170 + * NV_ReadOnly Volume is mounted read-only. 171 + * NV_Compression Volume supports compression. 172 + * NV_FreeClusterKnown Free cluster count is known and up-to-date. 173 + * NV_Shutdown Volume is in shutdown state 174 + * NV_SysImmutable Protect system files from deletion. 175 + * NV_ShowHiddenFiles Return hidden files in ntfs_readdir(). 176 + * NV_HideDotFiles Hide names beginning with a dot ("."). 177 + * NV_CheckWindowsNames Refuse creation/rename of files with 178 + * Windows-reserved names (CON, AUX, NUL, COM1, 179 + * LPT1, etc.) or invalid characters. 180 + * 181 + * NV_Discard Issue discard/TRIM commands for freed clusters. 182 + * NV_DisableSparse Disable creation of sparse regions. 123 183 */ 124 - typedef enum { 125 - NV_Errors, /* 1: Volume has errors, prevent remount rw. */ 126 - NV_ShowSystemFiles, /* 1: Return system files in ntfs_readdir(). */ 127 - NV_CaseSensitive, /* 1: Treat file names as case sensitive and 128 - create filenames in the POSIX namespace. 129 - Otherwise be case insensitive but still 130 - create file names in POSIX namespace. */ 131 - NV_LogFileEmpty, /* 1: $LogFile journal is empty. */ 132 - NV_QuotaOutOfDate, /* 1: $Quota is out of date. */ 133 - NV_UsnJrnlStamped, /* 1: $UsnJrnl has been stamped. */ 134 - NV_SparseEnabled, /* 1: May create sparse files. */ 135 - } ntfs_volume_flags; 184 + enum { 185 + NV_Errors, 186 + NV_ShowSystemFiles, 187 + NV_CaseSensitive, 188 + NV_LogFileEmpty, 189 + NV_QuotaOutOfDate, 190 + NV_UsnJrnlStamped, 191 + NV_ReadOnly, 192 + NV_Compression, 193 + NV_FreeClusterKnown, 194 + NV_Shutdown, 195 + NV_SysImmutable, 196 + NV_ShowHiddenFiles, 197 + NV_HideDotFiles, 198 + NV_CheckWindowsNames, 199 + NV_Discard, 200 + NV_DisableSparse, 201 + }; 136 202 137 203 /* 138 204 * Macro tricks to expand the NVolFoo(), NVolSetFoo(), and NVolClearFoo() 139 205 * functions. 140 206 */ 141 207 #define DEFINE_NVOL_BIT_OPS(flag) \ 142 - static inline int NVol##flag(ntfs_volume *vol) \ 143 - { \ 144 - return test_bit(NV_##flag, &(vol)->flags); \ 145 - } \ 146 - static inline void NVolSet##flag(ntfs_volume *vol) \ 147 - { \ 148 - set_bit(NV_##flag, &(vol)->flags); \ 149 - } \ 150 - static inline void NVolClear##flag(ntfs_volume *vol) \ 151 - { \ 152 - clear_bit(NV_##flag, &(vol)->flags); \ 208 + static inline int NVol##flag(struct ntfs_volume *vol) \ 209 + { \ 210 + return test_bit(NV_##flag, &(vol)->flags); \ 211 + } \ 212 + static inline void NVolSet##flag(struct ntfs_volume *vol) \ 213 + { \ 214 + set_bit(NV_##flag, &(vol)->flags); \ 215 + } \ 216 + static inline void NVolClear##flag(struct ntfs_volume *vol) \ 217 + { \ 218 + clear_bit(NV_##flag, &(vol)->flags); \ 153 219 } 154 220 155 221 /* Emit the ntfs volume bitops functions. */ ··· 225 159 DEFINE_NVOL_BIT_OPS(LogFileEmpty) 226 160 DEFINE_NVOL_BIT_OPS(QuotaOutOfDate) 227 161 DEFINE_NVOL_BIT_OPS(UsnJrnlStamped) 228 - DEFINE_NVOL_BIT_OPS(SparseEnabled) 162 + DEFINE_NVOL_BIT_OPS(ReadOnly) 163 + DEFINE_NVOL_BIT_OPS(Compression) 164 + DEFINE_NVOL_BIT_OPS(FreeClusterKnown) 165 + DEFINE_NVOL_BIT_OPS(Shutdown) 166 + DEFINE_NVOL_BIT_OPS(SysImmutable) 167 + DEFINE_NVOL_BIT_OPS(ShowHiddenFiles) 168 + DEFINE_NVOL_BIT_OPS(HideDotFiles) 169 + DEFINE_NVOL_BIT_OPS(CheckWindowsNames) 170 + DEFINE_NVOL_BIT_OPS(Discard) 171 + DEFINE_NVOL_BIT_OPS(DisableSparse) 229 172 173 + static inline void ntfs_inc_free_clusters(struct ntfs_volume *vol, s64 nr) 174 + { 175 + if (!NVolFreeClusterKnown(vol)) 176 + wait_event(vol->free_waitq, NVolFreeClusterKnown(vol)); 177 + atomic64_add(nr, &vol->free_clusters); 178 + } 179 + 180 + static inline void ntfs_dec_free_clusters(struct ntfs_volume *vol, s64 nr) 181 + { 182 + if (!NVolFreeClusterKnown(vol)) 183 + wait_event(vol->free_waitq, NVolFreeClusterKnown(vol)); 184 + atomic64_sub(nr, &vol->free_clusters); 185 + } 186 + 187 + static inline void ntfs_inc_free_mft_records(struct ntfs_volume *vol, s64 nr) 188 + { 189 + if (!NVolFreeClusterKnown(vol)) 190 + return; 191 + 192 + atomic64_add(nr, &vol->free_mft_records); 193 + } 194 + 195 + static inline void ntfs_dec_free_mft_records(struct ntfs_volume *vol, s64 nr) 196 + { 197 + if (!NVolFreeClusterKnown(vol)) 198 + return; 199 + 200 + atomic64_sub(nr, &vol->free_mft_records); 201 + } 202 + 203 + static inline void ntfs_set_lcn_empty_bits(struct ntfs_volume *vol, unsigned long index, 204 + u8 val, unsigned int count) 205 + { 206 + if (!NVolFreeClusterKnown(vol)) 207 + wait_event(vol->free_waitq, NVolFreeClusterKnown(vol)); 208 + 209 + if (val) 210 + vol->lcn_empty_bits_per_page[index] -= count; 211 + else 212 + vol->lcn_empty_bits_per_page[index] += count; 213 + } 214 + 215 + static __always_inline void ntfs_hold_dirty_clusters(struct ntfs_volume *vol, s64 nr_clusters) 216 + { 217 + atomic64_add(nr_clusters, &vol->dirty_clusters); 218 + } 219 + 220 + static __always_inline void ntfs_release_dirty_clusters(struct ntfs_volume *vol, s64 nr_clusters) 221 + { 222 + if (atomic64_read(&vol->dirty_clusters) < nr_clusters) 223 + atomic64_set(&vol->dirty_clusters, 0); 224 + else 225 + atomic64_sub(nr_clusters, &vol->dirty_clusters); 226 + } 227 + 228 + s64 ntfs_available_clusters_count(struct ntfs_volume *vol, s64 nr_clusters); 229 + s64 get_nr_free_clusters(struct ntfs_volume *vol); 230 230 #endif /* _LINUX_NTFS_VOLUME_H */