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/blk_types.h> 23 #include <linux/uidgid.h> 24 25 #include "volume.h" 26 #include "layout.h" 27 #include "inode.h" 28 29 #ifdef pr_fmt 30 #undef pr_fmt 31 #endif 32 33 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 34 35 /* 36 * Default pre-allocation size is optimize runlist merge overhead 37 * with small chunk size. 38 */ 39 #define NTFS_DEF_PREALLOC_SIZE 65536 40 41 /* 42 * The log2 of the standard number of clusters per compression block. 43 * A value of 4 corresponds to 16 clusters (1 << 4), which is the 44 * default chunk size used by NTFS LZNT1 compression. 45 */ 46 #define STANDARD_COMPRESSION_UNIT 4 47 48 /* 49 * The maximum cluster size (4KB) allowed for compression to be enabled. 50 * By design, NTFS does not support compression on volumes where the 51 * cluster size exceeds 4096 bytes. 52 */ 53 #define MAX_COMPRESSION_CLUSTER_SIZE 4096 54 55 #define NTFS_B_TO_CLU(vol, b) ((b) >> (vol)->cluster_size_bits) 56 #define NTFS_CLU_TO_B(vol, clu) ((u64)(clu) << (vol)->cluster_size_bits) 57 #define NTFS_B_TO_CLU_OFS(vol, clu) ((u64)(clu) & (vol)->cluster_size_mask) 58 59 #define NTFS_MFT_NR_TO_CLU(vol, mft_no) (((u64)mft_no << (vol)->mft_record_size_bits) >> \ 60 (vol)->cluster_size_bits) 61 #define NTFS_MFT_NR_TO_PIDX(vol, mft_no) (mft_no >> (PAGE_SHIFT - \ 62 (vol)->mft_record_size_bits)) 63 #define NTFS_MFT_NR_TO_POFS(vol, mft_no) (((u64)mft_no << (vol)->mft_record_size_bits) & \ 64 ~PAGE_MASK) 65 66 #define NTFS_PIDX_TO_BLK(vol, idx) (((u64)idx << PAGE_SHIFT) >> \ 67 ((vol)->sb)->s_blocksize_bits) 68 #define NTFS_PIDX_TO_CLU(vol, idx) (((u64)idx << PAGE_SHIFT) >> \ 69 (vol)->cluster_size_bits) 70 #define NTFS_CLU_TO_PIDX(vol, clu) (((u64)(clu) << (vol)->cluster_size_bits) >> \ 71 PAGE_SHIFT) 72 #define NTFS_CLU_TO_POFS(vol, clu) (((u64)(clu) << (vol)->cluster_size_bits) & \ 73 ~PAGE_MASK) 74 75 enum { 76 NTFS_BLOCK_SIZE = 512, 77 NTFS_BLOCK_SIZE_BITS = 9, 78 NTFS_SB_MAGIC = 0x5346544e, /* 'NTFS' */ 79 NTFS_MAX_NAME_LEN = 255, 80 NTFS_MAX_LABEL_LEN = 128, 81 }; 82 83 #define NTFS_MAX_ATTR_LIST_SIZE (256 * 1024) 84 85 enum { 86 CASE_SENSITIVE = 0, 87 IGNORE_CASE = 1, 88 }; 89 90 /* 91 * Conversion helpers for NTFS units. 92 */ 93 94 /* Convert bytes to cluster count */ 95 static inline u64 ntfs_bytes_to_cluster(const struct ntfs_volume *vol, 96 s64 bytes) 97 { 98 return bytes >> vol->cluster_size_bits; 99 } 100 101 /* Convert cluster count to bytes */ 102 static inline u64 ntfs_cluster_to_bytes(const struct ntfs_volume *vol, 103 u64 clusters) 104 { 105 return clusters << vol->cluster_size_bits; 106 } 107 108 /* Get the byte offset within a cluster from a linear byte address */ 109 static inline u64 ntfs_bytes_to_cluster_off(const struct ntfs_volume *vol, 110 u64 bytes) 111 { 112 return bytes & vol->cluster_size_mask; 113 } 114 115 /* Calculate the physical cluster number containing a specific MFT record. */ 116 static inline u64 ntfs_mft_no_to_cluster(const struct ntfs_volume *vol, 117 unsigned long mft_no) 118 { 119 return ((u64)mft_no << vol->mft_record_size_bits) >> 120 vol->cluster_size_bits; 121 } 122 123 /* Calculate the folio index where the MFT record resides. */ 124 static inline pgoff_t ntfs_mft_no_to_pidx(const struct ntfs_volume *vol, 125 unsigned long mft_no) 126 { 127 return mft_no >> (PAGE_SHIFT - vol->mft_record_size_bits); 128 } 129 130 /* Calculate the byte offset within a folio for an MFT record. */ 131 static inline u64 ntfs_mft_no_to_poff(const struct ntfs_volume *vol, 132 unsigned long mft_no) 133 { 134 return ((u64)mft_no << vol->mft_record_size_bits) & ~PAGE_MASK; 135 } 136 137 /* Convert folio index to cluster number. */ 138 static inline u64 ntfs_pidx_to_cluster(const struct ntfs_volume *vol, 139 pgoff_t idx) 140 { 141 return ((u64)idx << PAGE_SHIFT) >> vol->cluster_size_bits; 142 } 143 144 /* Convert cluster number to folio index. */ 145 static inline pgoff_t ntfs_cluster_to_pidx(const struct ntfs_volume *vol, 146 u64 clu) 147 { 148 return (clu << vol->cluster_size_bits) >> PAGE_SHIFT; 149 } 150 151 /* Get the byte offset within a folio from a cluster number */ 152 static inline u64 ntfs_cluster_to_poff(const struct ntfs_volume *vol, 153 u64 clu) 154 { 155 return (clu << vol->cluster_size_bits) & ~PAGE_MASK; 156 } 157 158 /* Convert a byte offset on the volume to a bio sector number. */ 159 static inline sector_t ntfs_bytes_to_bio_sector(u64 bytes) 160 { 161 return bytes >> SECTOR_SHIFT; 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 int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa); 189 int ntfs_fileattr_set(const struct mnt_idmap *idmap, struct dentry *dentry, 190 struct file_kattr *fa); 191 192 extern const struct export_operations ntfs_export_ops; 193 194 /* 195 * NTFS_SB - return the ntfs volume given a vfs super block 196 * @sb: VFS super block 197 * 198 * NTFS_SB() returns the ntfs volume associated with the VFS super block @sb. 199 */ 200 static inline struct ntfs_volume *NTFS_SB(struct super_block *sb) 201 { 202 return sb->s_fs_info; 203 } 204 205 /* Declarations of functions and global variables. */ 206 207 /* From fs/ntfs/compress.c */ 208 int ntfs_read_compressed_block(struct folio *folio); 209 #ifdef CONFIG_NTFS_FS_WOF_COMPRESSION 210 int ntfs_read_wof_compressed_block(struct folio *folio); 211 void ntfs_wof_free_workspaces(void); 212 #endif 213 int allocate_compression_buffers(void); 214 void free_compression_buffers(void); 215 int ntfs_compress_write(struct ntfs_inode *ni, loff_t pos, size_t count, 216 struct iov_iter *from); 217 218 /* From fs/ntfs/super.c */ 219 #define default_upcase_len 0x10000 220 extern struct mutex ntfs_lock; 221 222 struct option_t { 223 int val; 224 char *str; 225 }; 226 extern const struct option_t on_errors_arr[]; 227 int ntfs_set_volume_flags(struct ntfs_volume *vol, __le16 flags); 228 int ntfs_write_volume_label(struct ntfs_volume *vol, char *label); 229 230 /* From fs/ntfs/mst.c */ 231 int post_read_mst_fixup(struct ntfs_record *b, const u32 size); 232 int pre_write_mst_fixup(struct ntfs_record *b, const u32 size); 233 void post_write_mst_fixup(struct ntfs_record *b); 234 235 /* From fs/ntfs/unistr.c */ 236 bool ntfs_are_names_equal(const __le16 *s1, size_t s1_len, 237 const __le16 *s2, size_t s2_len, 238 const u32 ic, 239 const __le16 *upcase, const u32 upcase_size); 240 int ntfs_collate_names(const __le16 *name1, const u32 name1_len, 241 const __le16 *name2, const u32 name2_len, 242 const bool check_invalid, const u32 ic, 243 const __le16 *upcase, const u32 upcase_len); 244 int ntfs_ucsncmp(const __le16 *s1, const __le16 *s2, size_t n); 245 int ntfs_ucsncasecmp(const __le16 *s1, const __le16 *s2, size_t n, 246 const __le16 *upcase, const u32 upcase_size); 247 int ntfs_file_compare_values(const struct file_name_attr *file_name_attr1, 248 const struct file_name_attr *file_name_attr2, 249 const bool check_invalid, const u32 ic, 250 const __le16 *upcase, const u32 upcase_len); 251 int ntfs_nlstoucs(const struct ntfs_volume *vol, const char *ins, 252 const int ins_len, __le16 **outs, int max_name_len); 253 int ntfs_ucstonls(const struct ntfs_volume *vol, const __le16 *ins, 254 const int ins_len, unsigned char **outs, int outs_len); 255 __le16 *ntfs_ucsndup(const __le16 *s, u32 maxlen); 256 bool ntfs_names_are_equal(const __le16 *s1, size_t s1_len, 257 const __le16 *s2, size_t s2_len, 258 const u32 ic, 259 const __le16 *upcase, const u32 upcase_size); 260 int ntfs_force_shutdown(struct super_block *sb, u32 flags); 261 long ntfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg); 262 #ifdef CONFIG_COMPAT 263 long ntfs_compat_ioctl(struct file *filp, unsigned int cmd, 264 unsigned long arg); 265 #endif 266 267 /* From fs/ntfs/upcase.c */ 268 __le16 *generate_default_upcase(void); 269 270 static inline int ntfs_ffs(int x) 271 { 272 int r = 1; 273 274 if (!x) 275 return 0; 276 if (!(x & 0xffff)) { 277 x >>= 16; 278 r += 16; 279 } 280 if (!(x & 0xff)) { 281 x >>= 8; 282 r += 8; 283 } 284 if (!(x & 0xf)) { 285 x >>= 4; 286 r += 4; 287 } 288 if (!(x & 3)) { 289 x >>= 2; 290 r += 2; 291 } 292 if (!(x & 1)) 293 r += 1; 294 return r; 295 } 296 297 /* From fs/ntfs/aops.c */ 298 struct folio *ntfs_read_metadata_folio(struct address_space *mapping, 299 pgoff_t index); 300 301 /* From fs/ntfs/bdev-io.c */ 302 int ntfs_bdev_read(struct block_device *bdev, char *data, loff_t start, size_t size); 303 int ntfs_bdev_write(struct super_block *sb, void *buf, loff_t start, size_t size); 304 305 #endif /* _LINUX_NTFS_H */ 306