1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Defines for NTFS Linux kernel driver. 4 * 5 * Copyright (c) 2001-2014 Anton Altaparmakov and Tuxera Inc. 6 * Copyright (C) 2002 Richard Russon 7 * Copyright (c) 2025 LG Electronics Co., Ltd. 8 */ 9 10 #ifndef _LINUX_NTFS_H 11 #define _LINUX_NTFS_H 12 13 #include <linux/stddef.h> 14 #include <linux/kernel.h> 15 #include <linux/hex.h> 16 #include <linux/module.h> 17 #include <linux/compiler.h> 18 #include <linux/fs.h> 19 #include <linux/nls.h> 20 #include <linux/smp.h> 21 #include <linux/pagemap.h> 22 #include <linux/uidgid.h> 23 24 #include "volume.h" 25 #include "layout.h" 26 #include "inode.h" 27 28 #ifdef pr_fmt 29 #undef pr_fmt 30 #endif 31 32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 33 34 /* 35 * Default pre-allocation size is optimize runlist merge overhead 36 * with small chunk size. 37 */ 38 #define NTFS_DEF_PREALLOC_SIZE 65536 39 40 /* 41 * The log2 of the standard number of clusters per compression block. 42 * A value of 4 corresponds to 16 clusters (1 << 4), which is the 43 * default chunk size used by NTFS LZNT1 compression. 44 */ 45 #define STANDARD_COMPRESSION_UNIT 4 46 47 /* 48 * The maximum cluster size (4KB) allowed for compression to be enabled. 49 * By design, NTFS does not support compression on volumes where the 50 * cluster size exceeds 4096 bytes. 51 */ 52 #define MAX_COMPRESSION_CLUSTER_SIZE 4096 53 54 #define NTFS_B_TO_CLU(vol, b) ((b) >> (vol)->cluster_size_bits) 55 #define NTFS_CLU_TO_B(vol, clu) ((u64)(clu) << (vol)->cluster_size_bits) 56 #define NTFS_B_TO_CLU_OFS(vol, clu) ((u64)(clu) & (vol)->cluster_size_mask) 57 58 #define NTFS_MFT_NR_TO_CLU(vol, mft_no) (((u64)mft_no << (vol)->mft_record_size_bits) >> \ 59 (vol)->cluster_size_bits) 60 #define NTFS_MFT_NR_TO_PIDX(vol, mft_no) (mft_no >> (PAGE_SHIFT - \ 61 (vol)->mft_record_size_bits)) 62 #define NTFS_MFT_NR_TO_POFS(vol, mft_no) (((u64)mft_no << (vol)->mft_record_size_bits) & \ 63 ~PAGE_MASK) 64 65 #define NTFS_PIDX_TO_BLK(vol, idx) (((u64)idx << PAGE_SHIFT) >> \ 66 ((vol)->sb)->s_blocksize_bits) 67 #define NTFS_PIDX_TO_CLU(vol, idx) (((u64)idx << PAGE_SHIFT) >> \ 68 (vol)->cluster_size_bits) 69 #define NTFS_CLU_TO_PIDX(vol, clu) (((u64)(clu) << (vol)->cluster_size_bits) >> \ 70 PAGE_SHIFT) 71 #define NTFS_CLU_TO_POFS(vol, clu) (((u64)(clu) << (vol)->cluster_size_bits) & \ 72 ~PAGE_MASK) 73 74 #define NTFS_B_TO_SECTOR(vol, b) ((b) >> ((vol)->sb)->s_blocksize_bits) 75 76 enum { 77 NTFS_BLOCK_SIZE = 512, 78 NTFS_BLOCK_SIZE_BITS = 9, 79 NTFS_SB_MAGIC = 0x5346544e, /* 'NTFS' */ 80 NTFS_MAX_NAME_LEN = 255, 81 NTFS_MAX_LABEL_LEN = 128, 82 }; 83 84 enum { 85 CASE_SENSITIVE = 0, 86 IGNORE_CASE = 1, 87 }; 88 89 /* 90 * Conversion helpers for NTFS units. 91 */ 92 93 /* Convert bytes to cluster count */ 94 static inline u64 ntfs_bytes_to_cluster(const struct ntfs_volume *vol, 95 s64 bytes) 96 { 97 return bytes >> vol->cluster_size_bits; 98 } 99 100 /* Convert cluster count to bytes */ 101 static inline u64 ntfs_cluster_to_bytes(const struct ntfs_volume *vol, 102 u64 clusters) 103 { 104 return clusters << vol->cluster_size_bits; 105 } 106 107 /* Get the byte offset within a cluster from a linear byte address */ 108 static inline u64 ntfs_bytes_to_cluster_off(const struct ntfs_volume *vol, 109 u64 bytes) 110 { 111 return bytes & vol->cluster_size_mask; 112 } 113 114 /* Calculate the physical cluster number containing a specific MFT record. */ 115 static inline u64 ntfs_mft_no_to_cluster(const struct ntfs_volume *vol, 116 unsigned long mft_no) 117 { 118 return ((u64)mft_no << vol->mft_record_size_bits) >> 119 vol->cluster_size_bits; 120 } 121 122 /* Calculate the folio index where the MFT record resides. */ 123 static inline pgoff_t ntfs_mft_no_to_pidx(const struct ntfs_volume *vol, 124 unsigned long mft_no) 125 { 126 return mft_no >> (PAGE_SHIFT - vol->mft_record_size_bits); 127 } 128 129 /* Calculate the byte offset within a folio for an MFT record. */ 130 static inline u64 ntfs_mft_no_to_poff(const struct ntfs_volume *vol, 131 unsigned long mft_no) 132 { 133 return ((u64)mft_no << vol->mft_record_size_bits) & ~PAGE_MASK; 134 } 135 136 /* Convert folio index to cluster number. */ 137 static inline u64 ntfs_pidx_to_cluster(const struct ntfs_volume *vol, 138 pgoff_t idx) 139 { 140 return ((u64)idx << PAGE_SHIFT) >> vol->cluster_size_bits; 141 } 142 143 /* Convert cluster number to folio index. */ 144 static inline pgoff_t ntfs_cluster_to_pidx(const struct ntfs_volume *vol, 145 u64 clu) 146 { 147 return (clu << vol->cluster_size_bits) >> PAGE_SHIFT; 148 } 149 150 /* Get the byte offset within a folio from a cluster number */ 151 static inline u64 ntfs_cluster_to_poff(const struct ntfs_volume *vol, 152 u64 clu) 153 { 154 return (clu << vol->cluster_size_bits) & ~PAGE_MASK; 155 } 156 157 /* Convert byte offset to sector (block) number. */ 158 static inline sector_t ntfs_bytes_to_sector(const struct ntfs_volume *vol, 159 u64 bytes) 160 { 161 return bytes >> vol->sb->s_blocksize_bits; 162 } 163 164 /* Global variables. */ 165 166 /* Slab caches (from super.c). */ 167 extern struct kmem_cache *ntfs_name_cache; 168 extern struct kmem_cache *ntfs_inode_cache; 169 extern struct kmem_cache *ntfs_big_inode_cache; 170 extern struct kmem_cache *ntfs_attr_ctx_cache; 171 extern struct kmem_cache *ntfs_index_ctx_cache; 172 173 /* The various operations structs defined throughout the driver files. */ 174 extern const struct address_space_operations ntfs_aops; 175 extern const struct address_space_operations ntfs_mft_aops; 176 177 extern const struct file_operations ntfs_file_ops; 178 extern const struct inode_operations ntfs_file_inode_ops; 179 extern const struct inode_operations ntfs_symlink_inode_operations; 180 extern const struct inode_operations ntfs_special_inode_operations; 181 182 extern const struct file_operations ntfs_dir_ops; 183 extern const struct inode_operations ntfs_dir_inode_ops; 184 185 extern const struct file_operations ntfs_empty_file_ops; 186 extern const struct inode_operations ntfs_empty_inode_ops; 187 188 extern const struct export_operations ntfs_export_ops; 189 190 /* 191 * NTFS_SB - return the ntfs volume given a vfs super block 192 * @sb: VFS super block 193 * 194 * NTFS_SB() returns the ntfs volume associated with the VFS super block @sb. 195 */ 196 static inline struct ntfs_volume *NTFS_SB(struct super_block *sb) 197 { 198 return sb->s_fs_info; 199 } 200 201 /* Declarations of functions and global variables. */ 202 203 /* From fs/ntfs/compress.c */ 204 int ntfs_read_compressed_block(struct folio *folio); 205 int allocate_compression_buffers(void); 206 void free_compression_buffers(void); 207 int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count, 208 struct iov_iter *from); 209 210 /* From fs/ntfs/super.c */ 211 #define default_upcase_len 0x10000 212 extern struct mutex ntfs_lock; 213 214 struct option_t { 215 int val; 216 char *str; 217 }; 218 extern const struct option_t on_errors_arr[]; 219 int ntfs_set_volume_flags(struct ntfs_volume *vol, __le16 flags); 220 int ntfs_clear_volume_flags(struct ntfs_volume *vol, __le16 flags); 221 int ntfs_write_volume_label(struct ntfs_volume *vol, char *label); 222 223 /* From fs/ntfs/mst.c */ 224 int post_read_mst_fixup(struct ntfs_record *b, const u32 size); 225 int pre_write_mst_fixup(struct ntfs_record *b, const u32 size); 226 void post_write_mst_fixup(struct ntfs_record *b); 227 228 /* From fs/ntfs/unistr.c */ 229 bool ntfs_are_names_equal(const __le16 *s1, size_t s1_len, 230 const __le16 *s2, size_t s2_len, 231 const u32 ic, 232 const __le16 *upcase, const u32 upcase_size); 233 int ntfs_collate_names(const __le16 *name1, const u32 name1_len, 234 const __le16 *name2, const u32 name2_len, 235 const int err_val, const u32 ic, 236 const __le16 *upcase, const u32 upcase_len); 237 int ntfs_ucsncmp(const __le16 *s1, const __le16 *s2, size_t n); 238 int ntfs_ucsncasecmp(const __le16 *s1, const __le16 *s2, size_t n, 239 const __le16 *upcase, const u32 upcase_size); 240 int ntfs_file_compare_values(const struct file_name_attr *file_name_attr1, 241 const struct file_name_attr *file_name_attr2, 242 const int err_val, const u32 ic, 243 const __le16 *upcase, const u32 upcase_len); 244 int ntfs_nlstoucs(const struct ntfs_volume *vol, const char *ins, 245 const int ins_len, __le16 **outs, int max_name_len); 246 int ntfs_ucstonls(const struct ntfs_volume *vol, const __le16 *ins, 247 const int ins_len, unsigned char **outs, int outs_len); 248 __le16 *ntfs_ucsndup(const __le16 *s, u32 maxlen); 249 bool ntfs_names_are_equal(const __le16 *s1, size_t s1_len, 250 const __le16 *s2, size_t s2_len, 251 const u32 ic, 252 const __le16 *upcase, const u32 upcase_size); 253 int ntfs_force_shutdown(struct super_block *sb, u32 flags); 254 long ntfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg); 255 #ifdef CONFIG_COMPAT 256 long ntfs_compat_ioctl(struct file *filp, unsigned int cmd, 257 unsigned long arg); 258 #endif 259 260 /* From fs/ntfs/upcase.c */ 261 __le16 *generate_default_upcase(void); 262 263 static inline int ntfs_ffs(int x) 264 { 265 int r = 1; 266 267 if (!x) 268 return 0; 269 if (!(x & 0xffff)) { 270 x >>= 16; 271 r += 16; 272 } 273 if (!(x & 0xff)) { 274 x >>= 8; 275 r += 8; 276 } 277 if (!(x & 0xf)) { 278 x >>= 4; 279 r += 4; 280 } 281 if (!(x & 3)) { 282 x >>= 2; 283 r += 2; 284 } 285 if (!(x & 1)) 286 r += 1; 287 return r; 288 } 289 290 /* From fs/ntfs/bdev-io.c */ 291 int ntfs_bdev_read(struct block_device *bdev, char *data, loff_t start, size_t size); 292 int ntfs_bdev_write(struct super_block *sb, void *buf, loff_t start, size_t size); 293 294 #endif /* _LINUX_NTFS_H */ 295