ntfs_fs.h 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. *
  4. * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
  5. *
  6. */
  7. // clang-format off
  8. #ifndef _LINUX_NTFS3_NTFS_FS_H
  9. #define _LINUX_NTFS3_NTFS_FS_H
  10. #include <linux/blkdev.h>
  11. #include <linux/buffer_head.h>
  12. #include <linux/fs.h>
  13. #include <linux/highmem.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/mutex.h>
  17. #include <linux/page-flags.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/rbtree.h>
  20. #include <linux/rwsem.h>
  21. #include <linux/slab.h>
  22. #include <linux/string.h>
  23. #include <linux/time64.h>
  24. #include <linux/types.h>
  25. #include <linux/uidgid.h>
  26. #include <asm/div64.h>
  27. #include <asm/page.h>
  28. #include "debug.h"
  29. #include "ntfs.h"
  30. struct dentry;
  31. struct fiemap_extent_info;
  32. struct user_namespace;
  33. struct page;
  34. struct writeback_control;
  35. enum utf16_endian;
  36. #define MINUS_ONE_T ((size_t)(-1))
  37. /* Biggest MFT / smallest cluster */
  38. #define MAXIMUM_BYTES_PER_MFT 4096
  39. #define MAXIMUM_SHIFT_BYTES_PER_MFT 12
  40. #define NTFS_BLOCKS_PER_MFT_RECORD (MAXIMUM_BYTES_PER_MFT / 512)
  41. #define MAXIMUM_BYTES_PER_INDEX 4096
  42. #define MAXIMUM_SHIFT_BYTES_PER_INDEX 12
  43. #define NTFS_BLOCKS_PER_INODE (MAXIMUM_BYTES_PER_INDEX / 512)
  44. /* NTFS specific error code when fixup failed. */
  45. #define E_NTFS_FIXUP 555
  46. /* NTFS specific error code about resident->nonresident. */
  47. #define E_NTFS_NONRESIDENT 556
  48. /* NTFS specific error code about punch hole. */
  49. #define E_NTFS_NOTALIGNED 557
  50. /* NTFS specific error code when on-disk struct is corrupted. */
  51. #define E_NTFS_CORRUPT 558
  52. /* sbi->flags */
  53. #define NTFS_FLAGS_NODISCARD 0x00000001
  54. /* ntfs in shutdown state. */
  55. #define NTFS_FLAGS_SHUTDOWN_BIT 0x00000002 /* == 4*/
  56. /* Set when LogFile is replaying. */
  57. #define NTFS_FLAGS_LOG_REPLAYING 0x00000008
  58. /* Set when we changed first MFT's which copy must be updated in $MftMirr. */
  59. #define NTFS_FLAGS_MFTMIRR 0x00001000
  60. #define NTFS_FLAGS_NEED_REPLAY 0x04000000
  61. /* ni->ni_flags */
  62. /*
  63. * Data attribute is external compressed (LZX/Xpress)
  64. * 1 - WOF_COMPRESSION_XPRESS4K
  65. * 2 - WOF_COMPRESSION_XPRESS8K
  66. * 3 - WOF_COMPRESSION_XPRESS16K
  67. * 4 - WOF_COMPRESSION_LZX32K
  68. */
  69. #define NI_FLAG_COMPRESSED_MASK 0x0000000f
  70. /* Data attribute is deduplicated. */
  71. #define NI_FLAG_DEDUPLICATED 0x00000010
  72. #define NI_FLAG_EA 0x00000020
  73. #define NI_FLAG_DIR 0x00000040
  74. #define NI_FLAG_RESIDENT 0x00000080
  75. #define NI_FLAG_UPDATE_PARENT 0x00000100
  76. // clang-format on
  77. struct ntfs_mount_options {
  78. char *nls_name;
  79. struct nls_table *nls;
  80. kuid_t fs_uid;
  81. kgid_t fs_gid;
  82. u16 fs_fmask_inv;
  83. u16 fs_dmask_inv;
  84. unsigned fmask : 1; /* fmask was set. */
  85. unsigned dmask : 1; /*dmask was set. */
  86. unsigned sys_immutable : 1; /* Immutable system files. */
  87. unsigned discard : 1; /* Issue discard requests on deletions. */
  88. unsigned sparse : 1; /* Create sparse files. */
  89. unsigned showmeta : 1; /* Show meta files. */
  90. unsigned nohidden : 1; /* Do not show hidden files. */
  91. unsigned hide_dot_files : 1; /* Set hidden flag on dot files. */
  92. unsigned windows_names : 1; /* Disallow names forbidden by Windows. */
  93. unsigned force : 1; /* RW mount dirty volume. */
  94. unsigned prealloc : 1; /* Preallocate space when file is growing. */
  95. unsigned nocase : 1; /* case insensitive. */
  96. };
  97. /* Special value to unpack and deallocate. */
  98. #define RUN_DEALLOCATE ((struct runs_tree *)(size_t)1)
  99. /* TODO: Use rb tree instead of array. */
  100. struct runs_tree {
  101. struct ntfs_run *runs;
  102. size_t count; /* Currently used size a ntfs_run storage. */
  103. size_t allocated; /* Currently allocated ntfs_run storage size. */
  104. };
  105. struct ntfs_buffers {
  106. /* Biggest MFT / smallest cluster = 4096 / 512 = 8 */
  107. /* Biggest index / smallest cluster = 4096 / 512 = 8 */
  108. struct buffer_head *bh[PAGE_SIZE >> SECTOR_SHIFT];
  109. u32 bytes;
  110. u32 nbufs;
  111. u32 off;
  112. };
  113. enum ALLOCATE_OPT {
  114. ALLOCATE_DEF = 0, // Allocate all clusters.
  115. ALLOCATE_MFT = 1, // Allocate for MFT.
  116. ALLOCATE_ZERO = 2, // Zeroout new allocated clusters
  117. };
  118. enum bitmap_mutex_classes {
  119. BITMAP_MUTEX_CLUSTERS = 0,
  120. BITMAP_MUTEX_MFT = 1,
  121. };
  122. struct wnd_bitmap {
  123. struct super_block *sb;
  124. struct rw_semaphore rw_lock;
  125. struct runs_tree run;
  126. size_t nbits;
  127. size_t total_zeroes; // Total number of free bits.
  128. u16 *free_bits; // Free bits in each window.
  129. size_t nwnd;
  130. u32 bits_last; // Bits in last window.
  131. struct rb_root start_tree; // Extents, sorted by 'start'.
  132. struct rb_root count_tree; // Extents, sorted by 'count + start'.
  133. size_t count; // Extents count.
  134. /*
  135. * -1 Tree is activated but not updated (too many fragments).
  136. * 0 - Tree is not activated.
  137. * 1 - Tree is activated and updated.
  138. */
  139. int uptodated;
  140. size_t extent_min; // Minimal extent used while building.
  141. size_t extent_max; // Upper estimate of biggest free block.
  142. /* Zone [bit, end) */
  143. size_t zone_bit;
  144. size_t zone_end;
  145. bool inited;
  146. };
  147. typedef int (*NTFS_CMP_FUNC)(const void *key1, size_t len1, const void *key2,
  148. size_t len2, const void *param);
  149. enum index_mutex_classed {
  150. INDEX_MUTEX_I30 = 0,
  151. INDEX_MUTEX_SII = 1,
  152. INDEX_MUTEX_SDH = 2,
  153. INDEX_MUTEX_SO = 3,
  154. INDEX_MUTEX_SQ = 4,
  155. INDEX_MUTEX_SR = 5,
  156. INDEX_MUTEX_TOTAL
  157. };
  158. /* ntfs_index - Allocation unit inside directory. */
  159. struct ntfs_index {
  160. struct runs_tree bitmap_run;
  161. struct runs_tree alloc_run;
  162. /* read/write access to 'bitmap_run'/'alloc_run' while ntfs_readdir */
  163. struct rw_semaphore run_lock;
  164. /*TODO: Remove 'cmp'. */
  165. NTFS_CMP_FUNC cmp;
  166. u8 index_bits; // log2(root->index_block_size)
  167. u8 idx2vbn_bits; // log2(root->index_block_clst)
  168. u8 vbn2vbo_bits; // index_block_size < cluster? 9 : cluster_bits
  169. u8 type; // index_mutex_classed
  170. };
  171. /* Minimum MFT zone. */
  172. #define NTFS_MIN_MFT_ZONE 100
  173. /* Step to increase the MFT. */
  174. #define NTFS_MFT_INCREASE_STEP 1024
  175. /* Ntfs file system in-core superblock data. */
  176. struct ntfs_sb_info {
  177. struct super_block *sb;
  178. u32 discard_granularity;
  179. u64 discard_granularity_mask_inv; // ~(discard_granularity_mask_inv-1)
  180. u32 cluster_size; // bytes per cluster
  181. u32 cluster_mask; // == cluster_size - 1
  182. u64 cluster_mask_inv; // ~(cluster_size - 1)
  183. u32 block_mask; // sb->s_blocksize - 1
  184. u32 blocks_per_cluster; // cluster_size / sb->s_blocksize
  185. u32 record_size;
  186. u32 index_size;
  187. u8 cluster_bits;
  188. u8 record_bits;
  189. u64 maxbytes; // Maximum size for normal files.
  190. u64 maxbytes_sparse; // Maximum size for sparse file.
  191. unsigned long flags; // See NTFS_FLAGS_
  192. CLST zone_max; // Maximum MFT zone length in clusters
  193. CLST bad_clusters; // The count of marked bad clusters.
  194. u16 max_bytes_per_attr; // Maximum attribute size in record.
  195. u16 attr_size_tr; // Attribute size threshold (320 bytes).
  196. /* Records in $Extend. */
  197. CLST objid_no;
  198. CLST quota_no;
  199. CLST reparse_no;
  200. CLST usn_jrnl_no;
  201. struct ATTR_DEF_ENTRY *def_table; // Attribute definition table.
  202. u32 def_entries;
  203. u32 ea_max_size;
  204. struct MFT_REC *new_rec;
  205. u16 *upcase;
  206. struct {
  207. u64 lbo, lbo2;
  208. struct ntfs_inode *ni;
  209. struct wnd_bitmap bitmap; // $MFT::Bitmap
  210. /*
  211. * MFT records [11-24) used to expand MFT itself.
  212. * They always marked as used in $MFT::Bitmap
  213. * 'reserved_bitmap' contains real bitmap of these records.
  214. */
  215. ulong reserved_bitmap; // Bitmap of used records [11 - 24)
  216. size_t next_free; // The next record to allocate from
  217. size_t used; // MFT valid size in records.
  218. u32 recs_mirr; // Number of records in MFTMirr
  219. u8 next_reserved;
  220. u8 reserved_bitmap_inited;
  221. } mft;
  222. struct {
  223. struct wnd_bitmap bitmap; // $Bitmap::Data
  224. CLST next_free_lcn;
  225. } used;
  226. struct {
  227. u64 size; // In bytes.
  228. u64 blocks; // In blocks.
  229. u64 ser_num;
  230. struct ntfs_inode *ni;
  231. __le16 flags; // Cached current VOLUME_INFO::flags, VOLUME_FLAG_DIRTY.
  232. u8 major_ver;
  233. u8 minor_ver;
  234. char label[256];
  235. bool real_dirty; // Real fs state.
  236. } volume;
  237. struct {
  238. struct ntfs_index index_sii;
  239. struct ntfs_index index_sdh;
  240. struct ntfs_inode *ni;
  241. u32 next_id;
  242. u64 next_off;
  243. __le32 def_security_id;
  244. } security;
  245. struct {
  246. struct ntfs_index index_r;
  247. struct ntfs_inode *ni;
  248. u64 max_size; // 16K
  249. } reparse;
  250. struct {
  251. struct ntfs_index index_o;
  252. struct ntfs_inode *ni;
  253. } objid;
  254. struct {
  255. struct mutex mtx_lznt;
  256. struct lznt *lznt;
  257. #ifdef CONFIG_NTFS3_LZX_XPRESS
  258. struct mutex mtx_xpress;
  259. struct xpress_decompressor *xpress;
  260. struct mutex mtx_lzx;
  261. struct lzx_decompressor *lzx;
  262. #endif
  263. } compress;
  264. struct ntfs_mount_options *options;
  265. struct ratelimit_state msg_ratelimit;
  266. struct proc_dir_entry *procdir;
  267. };
  268. /* One MFT record(usually 1024 bytes), consists of attributes. */
  269. struct mft_inode {
  270. struct rb_node node;
  271. struct ntfs_sb_info *sbi;
  272. struct MFT_REC *mrec;
  273. struct ntfs_buffers nb;
  274. CLST rno;
  275. bool dirty;
  276. };
  277. /* Nested class for ntfs_inode::ni_lock. */
  278. enum ntfs_inode_mutex_lock_class {
  279. NTFS_INODE_MUTEX_DIRTY = 1,
  280. NTFS_INODE_MUTEX_SECURITY,
  281. NTFS_INODE_MUTEX_OBJID,
  282. NTFS_INODE_MUTEX_REPARSE,
  283. NTFS_INODE_MUTEX_NORMAL,
  284. NTFS_INODE_MUTEX_PARENT,
  285. NTFS_INODE_MUTEX_PARENT2,
  286. };
  287. /*
  288. * struct ntfs_inode
  289. *
  290. * Ntfs inode - extends linux inode. consists of one or more MFT inodes.
  291. */
  292. struct ntfs_inode {
  293. struct mft_inode mi; // base record
  294. /*
  295. * Valid size: [0 - i_valid) - these range in file contains valid data.
  296. * Range [i_valid - inode->i_size) - contains 0.
  297. * Usually i_valid <= inode->i_size.
  298. */
  299. u64 i_valid;
  300. struct timespec64 i_crtime;
  301. struct mutex ni_lock;
  302. /* File attributes from std. */
  303. enum FILE_ATTRIBUTE std_fa;
  304. __le32 std_security_id;
  305. /*
  306. * Tree of mft_inode.
  307. * Not empty when primary MFT record (usually 1024 bytes) can't save all attributes
  308. * e.g. file becomes too fragmented or contains a lot of names.
  309. */
  310. struct rb_root mi_tree;
  311. /*
  312. * This member is used in ntfs_readdir to ensure that all subrecords are loaded
  313. */
  314. u8 mi_loaded;
  315. union {
  316. struct ntfs_index dir;
  317. struct {
  318. struct rw_semaphore run_lock;
  319. struct runs_tree run;
  320. #ifdef CONFIG_NTFS3_LZX_XPRESS
  321. struct folio *offs_folio;
  322. #endif
  323. } file;
  324. };
  325. struct {
  326. struct runs_tree run;
  327. struct ATTR_LIST_ENTRY *le; // 1K aligned memory.
  328. size_t size;
  329. bool dirty;
  330. } attr_list;
  331. size_t ni_flags; // NI_FLAG_XXX
  332. struct inode vfs_inode;
  333. };
  334. struct indx_node {
  335. struct ntfs_buffers nb;
  336. struct INDEX_BUFFER *index;
  337. };
  338. struct ntfs_fnd {
  339. int level;
  340. struct indx_node *nodes[20];
  341. struct NTFS_DE *de[20];
  342. struct NTFS_DE *root_de;
  343. };
  344. enum REPARSE_SIGN {
  345. REPARSE_NONE = 0,
  346. REPARSE_COMPRESSED = 1,
  347. REPARSE_DEDUPLICATED = 2,
  348. REPARSE_LINK = 3
  349. };
  350. /* Functions from attrib.c */
  351. int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run,
  352. CLST vcn, CLST lcn, CLST len, CLST *pre_alloc,
  353. enum ALLOCATE_OPT opt, CLST *alen, const size_t fr,
  354. CLST *new_lcn, CLST *new_len);
  355. int attr_make_nonresident(struct ntfs_inode *ni, struct ATTRIB *attr,
  356. struct ATTR_LIST_ENTRY *le, struct mft_inode *mi,
  357. u64 new_size, struct runs_tree *run,
  358. struct ATTRIB **ins_attr, struct page *page);
  359. int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type,
  360. const __le16 *name, u8 name_len, struct runs_tree *run,
  361. u64 new_size, const u64 *new_valid, bool keep_prealloc,
  362. struct ATTRIB **ret);
  363. int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn,
  364. CLST *len, bool *new, bool zero);
  365. int attr_data_read_resident(struct ntfs_inode *ni, struct folio *folio);
  366. int attr_data_write_resident(struct ntfs_inode *ni, struct folio *folio);
  367. int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type,
  368. const __le16 *name, u8 name_len, struct runs_tree *run,
  369. CLST vcn);
  370. int attr_load_runs_range(struct ntfs_inode *ni, enum ATTR_TYPE type,
  371. const __le16 *name, u8 name_len, struct runs_tree *run,
  372. u64 from, u64 to);
  373. int attr_wof_frame_info(struct ntfs_inode *ni, struct ATTRIB *attr,
  374. struct runs_tree *run, u64 frame, u64 frames,
  375. u8 frame_bits, u32 *ondisk_size, u64 *vbo_data);
  376. int attr_is_frame_compressed(struct ntfs_inode *ni, struct ATTRIB *attr,
  377. CLST frame, CLST *clst_data,
  378. struct runs_tree *run);
  379. int attr_allocate_frame(struct ntfs_inode *ni, CLST frame, size_t compr_size,
  380. u64 new_valid);
  381. int attr_collapse_range(struct ntfs_inode *ni, u64 vbo, u64 bytes);
  382. int attr_insert_range(struct ntfs_inode *ni, u64 vbo, u64 bytes);
  383. int attr_punch_hole(struct ntfs_inode *ni, u64 vbo, u64 bytes, u32 *frame_size);
  384. int attr_force_nonresident(struct ntfs_inode *ni);
  385. int attr_set_compress(struct ntfs_inode *ni, bool compr);
  386. /* Functions from attrlist.c */
  387. void al_destroy(struct ntfs_inode *ni);
  388. bool al_verify(struct ntfs_inode *ni);
  389. int ntfs_load_attr_list(struct ntfs_inode *ni, struct ATTRIB *attr);
  390. struct ATTR_LIST_ENTRY *al_enumerate(struct ntfs_inode *ni,
  391. struct ATTR_LIST_ENTRY *le);
  392. struct ATTR_LIST_ENTRY *al_find_le(struct ntfs_inode *ni,
  393. struct ATTR_LIST_ENTRY *le,
  394. const struct ATTRIB *attr);
  395. struct ATTR_LIST_ENTRY *al_find_ex(struct ntfs_inode *ni,
  396. struct ATTR_LIST_ENTRY *le,
  397. enum ATTR_TYPE type, const __le16 *name,
  398. u8 name_len, const CLST *vcn);
  399. int al_add_le(struct ntfs_inode *ni, enum ATTR_TYPE type, const __le16 *name,
  400. u8 name_len, CLST svcn, __le16 id, const struct MFT_REF *ref,
  401. struct ATTR_LIST_ENTRY **new_le);
  402. bool al_remove_le(struct ntfs_inode *ni, struct ATTR_LIST_ENTRY *le);
  403. int al_update(struct ntfs_inode *ni, int sync);
  404. static inline size_t al_aligned(size_t size)
  405. {
  406. return size_add(size, 1023) & ~(size_t)1023;
  407. }
  408. /* Globals from bitfunc.c */
  409. bool are_bits_clear(const void *map, size_t bit, size_t nbits);
  410. bool are_bits_set(const void *map, size_t bit, size_t nbits);
  411. size_t get_set_bits_ex(const void *map, size_t bit, size_t nbits);
  412. /* Globals from dir.c */
  413. int ntfs_utf16_to_nls(struct ntfs_sb_info *sbi, const __le16 *name, u32 len,
  414. u8 *buf, int buf_len);
  415. int ntfs_nls_to_utf16(struct ntfs_sb_info *sbi, const u8 *name, u32 name_len,
  416. struct cpu_str *uni, u32 max_ulen,
  417. enum utf16_endian endian);
  418. struct inode *dir_search_u(struct inode *dir, const struct cpu_str *uni,
  419. struct ntfs_fnd *fnd);
  420. bool dir_is_empty(struct inode *dir);
  421. extern const struct file_operations ntfs_dir_operations;
  422. extern const struct file_operations ntfs_legacy_dir_operations;
  423. /* Globals from file.c */
  424. int ntfs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
  425. int ntfs_fileattr_set(struct mnt_idmap *idmap, struct dentry *dentry,
  426. struct fileattr *fa);
  427. int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
  428. struct kstat *stat, u32 request_mask, u32 flags);
  429. int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  430. struct iattr *attr);
  431. int ntfs_file_open(struct inode *inode, struct file *file);
  432. int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  433. __u64 start, __u64 len);
  434. long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg);
  435. long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg);
  436. extern const struct inode_operations ntfs_special_inode_operations;
  437. extern const struct inode_operations ntfs_file_inode_operations;
  438. extern const struct file_operations ntfs_file_operations;
  439. extern const struct file_operations ntfs_legacy_file_operations;
  440. /* Globals from frecord.c */
  441. void ni_remove_mi(struct ntfs_inode *ni, struct mft_inode *mi);
  442. struct ATTR_STD_INFO *ni_std(struct ntfs_inode *ni);
  443. struct ATTR_STD_INFO5 *ni_std5(struct ntfs_inode *ni);
  444. void ni_clear(struct ntfs_inode *ni);
  445. int ni_load_mi_ex(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi);
  446. int ni_load_mi(struct ntfs_inode *ni, const struct ATTR_LIST_ENTRY *le,
  447. struct mft_inode **mi);
  448. struct ATTRIB *ni_find_attr(struct ntfs_inode *ni, struct ATTRIB *attr,
  449. struct ATTR_LIST_ENTRY **entry_o,
  450. enum ATTR_TYPE type, const __le16 *name,
  451. u8 name_len, const CLST *vcn,
  452. struct mft_inode **mi);
  453. struct ATTRIB *ni_enum_attr_ex(struct ntfs_inode *ni, struct ATTRIB *attr,
  454. struct ATTR_LIST_ENTRY **le,
  455. struct mft_inode **mi);
  456. struct ATTRIB *ni_load_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
  457. const __le16 *name, u8 name_len, CLST vcn,
  458. struct mft_inode **pmi);
  459. int ni_load_all_mi(struct ntfs_inode *ni);
  460. bool ni_add_subrecord(struct ntfs_inode *ni, CLST rno, struct mft_inode **mi);
  461. int ni_remove_attr(struct ntfs_inode *ni, enum ATTR_TYPE type,
  462. const __le16 *name, u8 name_len, bool base_only,
  463. const __le16 *id);
  464. int ni_create_attr_list(struct ntfs_inode *ni);
  465. int ni_expand_list(struct ntfs_inode *ni);
  466. int ni_insert_nonresident(struct ntfs_inode *ni, enum ATTR_TYPE type,
  467. const __le16 *name, u8 name_len,
  468. const struct runs_tree *run, CLST svcn, CLST len,
  469. __le16 flags, struct ATTRIB **new_attr,
  470. struct mft_inode **mi, struct ATTR_LIST_ENTRY **le);
  471. int ni_insert_resident(struct ntfs_inode *ni, u32 data_size,
  472. enum ATTR_TYPE type, const __le16 *name, u8 name_len,
  473. struct ATTRIB **new_attr, struct mft_inode **mi,
  474. struct ATTR_LIST_ENTRY **le);
  475. void ni_remove_attr_le(struct ntfs_inode *ni, struct ATTRIB *attr,
  476. struct mft_inode *mi, struct ATTR_LIST_ENTRY *le);
  477. int ni_delete_all(struct ntfs_inode *ni);
  478. struct ATTR_FILE_NAME *ni_fname_name(struct ntfs_inode *ni,
  479. const struct le_str *uni,
  480. const struct MFT_REF *home,
  481. struct mft_inode **mi,
  482. struct ATTR_LIST_ENTRY **entry);
  483. struct ATTR_FILE_NAME *ni_fname_type(struct ntfs_inode *ni, u8 name_type,
  484. struct mft_inode **mi,
  485. struct ATTR_LIST_ENTRY **entry);
  486. int ni_new_attr_flags(struct ntfs_inode *ni, enum FILE_ATTRIBUTE new_fa);
  487. enum REPARSE_SIGN ni_parse_reparse(struct ntfs_inode *ni, struct ATTRIB *attr,
  488. struct REPARSE_DATA_BUFFER *buffer);
  489. int ni_write_inode(struct inode *inode, int sync, const char *hint);
  490. #define _ni_write_inode(i, w) ni_write_inode(i, w, __func__)
  491. int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo,
  492. __u64 vbo, __u64 len);
  493. int ni_readpage_cmpr(struct ntfs_inode *ni, struct folio *folio);
  494. int ni_decompress_file(struct ntfs_inode *ni);
  495. int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages,
  496. u32 pages_per_frame);
  497. int ni_write_frame(struct ntfs_inode *ni, struct page **pages,
  498. u32 pages_per_frame);
  499. int ni_remove_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
  500. struct NTFS_DE *de, struct NTFS_DE **de2, int *undo_step);
  501. bool ni_remove_name_undo(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
  502. struct NTFS_DE *de, struct NTFS_DE *de2,
  503. int undo_step);
  504. int ni_add_name(struct ntfs_inode *dir_ni, struct ntfs_inode *ni,
  505. struct NTFS_DE *de);
  506. int ni_rename(struct ntfs_inode *dir_ni, struct ntfs_inode *new_dir_ni,
  507. struct ntfs_inode *ni, struct NTFS_DE *de, struct NTFS_DE *new_de);
  508. bool ni_is_dirty(struct inode *inode);
  509. int ni_set_compress(struct inode *inode, bool compr);
  510. /* Globals from fslog.c */
  511. bool check_index_header(const struct INDEX_HDR *hdr, size_t bytes);
  512. int log_replay(struct ntfs_inode *ni, bool *initialized);
  513. /* Globals from fsntfs.c */
  514. struct buffer_head *ntfs_bread(struct super_block *sb, sector_t block);
  515. bool ntfs_fix_pre_write(struct NTFS_RECORD_HEADER *rhdr, size_t bytes);
  516. int ntfs_fix_post_read(struct NTFS_RECORD_HEADER *rhdr, size_t bytes,
  517. bool simple);
  518. int ntfs_extend_init(struct ntfs_sb_info *sbi);
  519. int ntfs_loadlog_and_replay(struct ntfs_inode *ni, struct ntfs_sb_info *sbi);
  520. int ntfs_look_for_free_space(struct ntfs_sb_info *sbi, CLST lcn, CLST len,
  521. CLST *new_lcn, CLST *new_len,
  522. enum ALLOCATE_OPT opt);
  523. bool ntfs_check_for_free_space(struct ntfs_sb_info *sbi, CLST clen, CLST mlen);
  524. int ntfs_look_free_mft(struct ntfs_sb_info *sbi, CLST *rno, bool mft,
  525. struct ntfs_inode *ni, struct mft_inode **mi);
  526. void ntfs_mark_rec_free(struct ntfs_sb_info *sbi, CLST rno, bool is_mft);
  527. int ntfs_clear_mft_tail(struct ntfs_sb_info *sbi, size_t from, size_t to);
  528. int ntfs_refresh_zone(struct ntfs_sb_info *sbi);
  529. void ntfs_update_mftmirr(struct ntfs_sb_info *sbi, int wait);
  530. void ntfs_bad_inode(struct inode *inode, const char *hint);
  531. #define _ntfs_bad_inode(i) ntfs_bad_inode(i, __func__)
  532. enum NTFS_DIRTY_FLAGS {
  533. NTFS_DIRTY_CLEAR = 0,
  534. NTFS_DIRTY_DIRTY = 1,
  535. NTFS_DIRTY_ERROR = 2,
  536. };
  537. int ntfs_set_state(struct ntfs_sb_info *sbi, enum NTFS_DIRTY_FLAGS dirty);
  538. int ntfs_sb_read(struct super_block *sb, u64 lbo, size_t bytes, void *buffer);
  539. int ntfs_sb_write(struct super_block *sb, u64 lbo, size_t bytes,
  540. const void *buffer, int wait);
  541. int ntfs_sb_write_run(struct ntfs_sb_info *sbi, const struct runs_tree *run,
  542. u64 vbo, const void *buf, size_t bytes, int sync);
  543. struct buffer_head *ntfs_bread_run(struct ntfs_sb_info *sbi,
  544. const struct runs_tree *run, u64 vbo);
  545. int ntfs_read_run_nb(struct ntfs_sb_info *sbi, const struct runs_tree *run,
  546. u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb);
  547. int ntfs_read_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
  548. struct NTFS_RECORD_HEADER *rhdr, u32 bytes,
  549. struct ntfs_buffers *nb);
  550. int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
  551. u32 bytes, struct ntfs_buffers *nb);
  552. int ntfs_write_bh(struct ntfs_sb_info *sbi, struct NTFS_RECORD_HEADER *rhdr,
  553. struct ntfs_buffers *nb, int sync);
  554. int ntfs_bio_pages(struct ntfs_sb_info *sbi, const struct runs_tree *run,
  555. struct page **pages, u32 nr_pages, u64 vbo, u32 bytes,
  556. enum req_op op);
  557. int ntfs_bio_fill_1(struct ntfs_sb_info *sbi, const struct runs_tree *run);
  558. int ntfs_vbo_to_lbo(struct ntfs_sb_info *sbi, const struct runs_tree *run,
  559. u64 vbo, u64 *lbo, u64 *bytes);
  560. struct ntfs_inode *ntfs_new_inode(struct ntfs_sb_info *sbi, CLST nRec,
  561. enum RECORD_FLAG flag);
  562. extern const u8 s_default_security[0x50];
  563. bool is_sd_valid(const struct SECURITY_DESCRIPTOR_RELATIVE *sd, u32 len);
  564. int ntfs_security_init(struct ntfs_sb_info *sbi);
  565. int ntfs_get_security_by_id(struct ntfs_sb_info *sbi, __le32 security_id,
  566. struct SECURITY_DESCRIPTOR_RELATIVE **sd,
  567. size_t *size);
  568. int ntfs_insert_security(struct ntfs_sb_info *sbi,
  569. const struct SECURITY_DESCRIPTOR_RELATIVE *sd,
  570. u32 size, __le32 *security_id, bool *inserted);
  571. int ntfs_reparse_init(struct ntfs_sb_info *sbi);
  572. int ntfs_objid_init(struct ntfs_sb_info *sbi);
  573. int ntfs_objid_remove(struct ntfs_sb_info *sbi, struct GUID *guid);
  574. int ntfs_insert_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
  575. const struct MFT_REF *ref);
  576. int ntfs_remove_reparse(struct ntfs_sb_info *sbi, __le32 rtag,
  577. const struct MFT_REF *ref);
  578. void mark_as_free_ex(struct ntfs_sb_info *sbi, CLST lcn, CLST len, bool trim);
  579. int run_deallocate(struct ntfs_sb_info *sbi, const struct runs_tree *run,
  580. bool trim);
  581. bool valid_windows_name(struct ntfs_sb_info *sbi, const struct le_str *name);
  582. int ntfs_set_label(struct ntfs_sb_info *sbi, u8 *label, int len);
  583. /* Globals from index.c */
  584. int indx_used_bit(struct ntfs_index *indx, struct ntfs_inode *ni, size_t *bit);
  585. void fnd_clear(struct ntfs_fnd *fnd);
  586. static inline struct ntfs_fnd *fnd_get(void)
  587. {
  588. return kzalloc(sizeof(struct ntfs_fnd), GFP_NOFS);
  589. }
  590. static inline void fnd_put(struct ntfs_fnd *fnd)
  591. {
  592. if (fnd) {
  593. fnd_clear(fnd);
  594. kfree(fnd);
  595. }
  596. }
  597. void indx_clear(struct ntfs_index *idx);
  598. int indx_init(struct ntfs_index *indx, struct ntfs_sb_info *sbi,
  599. const struct ATTRIB *attr, enum index_mutex_classed type);
  600. struct INDEX_ROOT *indx_get_root(struct ntfs_index *indx, struct ntfs_inode *ni,
  601. struct ATTRIB **attr, struct mft_inode **mi);
  602. int indx_read(struct ntfs_index *idx, struct ntfs_inode *ni, CLST vbn,
  603. struct indx_node **node);
  604. int indx_find(struct ntfs_index *indx, struct ntfs_inode *dir,
  605. const struct INDEX_ROOT *root, const void *Key, size_t KeyLen,
  606. const void *param, int *diff, struct NTFS_DE **entry,
  607. struct ntfs_fnd *fnd);
  608. int indx_find_sort(struct ntfs_index *indx, struct ntfs_inode *ni,
  609. const struct INDEX_ROOT *root, struct NTFS_DE **entry,
  610. struct ntfs_fnd *fnd);
  611. int indx_find_raw(struct ntfs_index *indx, struct ntfs_inode *ni,
  612. const struct INDEX_ROOT *root, struct NTFS_DE **entry,
  613. size_t *off, struct ntfs_fnd *fnd);
  614. int indx_insert_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
  615. const struct NTFS_DE *new_de, const void *param,
  616. struct ntfs_fnd *fnd, bool undo);
  617. int indx_delete_entry(struct ntfs_index *indx, struct ntfs_inode *ni,
  618. const void *key, u32 key_len, const void *param);
  619. int indx_update_dup(struct ntfs_inode *ni, struct ntfs_sb_info *sbi,
  620. const struct ATTR_FILE_NAME *fname,
  621. const struct NTFS_DUP_INFO *dup, int sync);
  622. /* Globals from inode.c */
  623. struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref,
  624. const struct cpu_str *name);
  625. int ntfs_set_size(struct inode *inode, u64 new_size);
  626. int ntfs_get_block(struct inode *inode, sector_t vbn,
  627. struct buffer_head *bh_result, int create);
  628. int ntfs_write_begin(struct file *file, struct address_space *mapping,
  629. loff_t pos, u32 len, struct folio **foliop, void **fsdata);
  630. int ntfs_write_end(struct file *file, struct address_space *mapping, loff_t pos,
  631. u32 len, u32 copied, struct folio *folio, void *fsdata);
  632. int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc);
  633. int ntfs_sync_inode(struct inode *inode);
  634. int ntfs_flush_inodes(struct super_block *sb, struct inode *i1,
  635. struct inode *i2);
  636. int inode_read_data(struct inode *inode, void *data, size_t bytes);
  637. int ntfs_create_inode(struct mnt_idmap *idmap, struct inode *dir,
  638. struct dentry *dentry, const struct cpu_str *uni,
  639. umode_t mode, dev_t dev, const char *symname, u32 size,
  640. struct ntfs_fnd *fnd);
  641. int ntfs_link_inode(struct inode *inode, struct dentry *dentry);
  642. int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry);
  643. void ntfs_evict_inode(struct inode *inode);
  644. extern const struct inode_operations ntfs_link_inode_operations;
  645. extern const struct address_space_operations ntfs_aops;
  646. extern const struct address_space_operations ntfs_aops_cmpr;
  647. /* Globals from name_i.c */
  648. int fill_name_de(struct ntfs_sb_info *sbi, void *buf, const struct qstr *name,
  649. const struct cpu_str *uni);
  650. struct dentry *ntfs3_get_parent(struct dentry *child);
  651. extern const struct inode_operations ntfs_dir_inode_operations;
  652. extern const struct inode_operations ntfs_special_inode_operations;
  653. extern const struct dentry_operations ntfs_dentry_ops;
  654. /* Globals from record.c */
  655. int mi_get(struct ntfs_sb_info *sbi, CLST rno, struct mft_inode **mi);
  656. void mi_put(struct mft_inode *mi);
  657. int mi_init(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno);
  658. int mi_read(struct mft_inode *mi, bool is_mft);
  659. struct ATTRIB *mi_enum_attr(struct mft_inode *mi, struct ATTRIB *attr);
  660. // TODO: id?
  661. struct ATTRIB *mi_find_attr(struct mft_inode *mi, struct ATTRIB *attr,
  662. enum ATTR_TYPE type, const __le16 *name,
  663. u8 name_len, const __le16 *id);
  664. static inline struct ATTRIB *rec_find_attr_le(struct mft_inode *rec,
  665. struct ATTR_LIST_ENTRY *le)
  666. {
  667. return mi_find_attr(rec, NULL, le->type, le_name(le), le->name_len,
  668. &le->id);
  669. }
  670. int mi_write(struct mft_inode *mi, int wait);
  671. int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
  672. __le16 flags, bool is_mft);
  673. struct ATTRIB *mi_insert_attr(struct mft_inode *mi, enum ATTR_TYPE type,
  674. const __le16 *name, u8 name_len, u32 asize,
  675. u16 name_off);
  676. bool mi_remove_attr(struct ntfs_inode *ni, struct mft_inode *mi,
  677. struct ATTRIB *attr);
  678. bool mi_resize_attr(struct mft_inode *mi, struct ATTRIB *attr, int bytes);
  679. int mi_pack_runs(struct mft_inode *mi, struct ATTRIB *attr,
  680. struct runs_tree *run, CLST len);
  681. static inline bool mi_is_ref(const struct mft_inode *mi,
  682. const struct MFT_REF *ref)
  683. {
  684. if (le32_to_cpu(ref->low) != mi->rno)
  685. return false;
  686. if (ref->seq != mi->mrec->seq)
  687. return false;
  688. #ifdef CONFIG_NTFS3_64BIT_CLUSTER
  689. return le16_to_cpu(ref->high) == (mi->rno >> 32);
  690. #else
  691. return !ref->high;
  692. #endif
  693. }
  694. static inline void mi_get_ref(const struct mft_inode *mi, struct MFT_REF *ref)
  695. {
  696. ref->low = cpu_to_le32(mi->rno);
  697. #ifdef CONFIG_NTFS3_64BIT_CLUSTER
  698. ref->high = cpu_to_le16(mi->rno >> 32);
  699. #else
  700. ref->high = 0;
  701. #endif
  702. ref->seq = mi->mrec->seq;
  703. }
  704. /* Globals from run.c */
  705. bool run_lookup_entry(const struct runs_tree *run, CLST vcn, CLST *lcn,
  706. CLST *len, size_t *index);
  707. void run_truncate(struct runs_tree *run, CLST vcn);
  708. void run_truncate_head(struct runs_tree *run, CLST vcn);
  709. void run_truncate_around(struct runs_tree *run, CLST vcn);
  710. bool run_add_entry(struct runs_tree *run, CLST vcn, CLST lcn, CLST len,
  711. bool is_mft);
  712. bool run_collapse_range(struct runs_tree *run, CLST vcn, CLST len);
  713. bool run_insert_range(struct runs_tree *run, CLST vcn, CLST len);
  714. bool run_get_entry(const struct runs_tree *run, size_t index, CLST *vcn,
  715. CLST *lcn, CLST *len);
  716. bool run_is_mapped_full(const struct runs_tree *run, CLST svcn, CLST evcn);
  717. int run_pack(const struct runs_tree *run, CLST svcn, CLST len, u8 *run_buf,
  718. u32 run_buf_size, CLST *packed_vcns);
  719. int run_unpack(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino,
  720. CLST svcn, CLST evcn, CLST vcn, const u8 *run_buf,
  721. int run_buf_size);
  722. #ifdef NTFS3_CHECK_FREE_CLST
  723. int run_unpack_ex(struct runs_tree *run, struct ntfs_sb_info *sbi, CLST ino,
  724. CLST svcn, CLST evcn, CLST vcn, const u8 *run_buf,
  725. int run_buf_size);
  726. #else
  727. #define run_unpack_ex run_unpack
  728. #endif
  729. int run_get_highest_vcn(CLST vcn, const u8 *run_buf, u64 *highest_vcn);
  730. int run_clone(const struct runs_tree *run, struct runs_tree *new_run);
  731. /* Globals from super.c */
  732. void *ntfs_set_shared(void *ptr, u32 bytes);
  733. void *ntfs_put_shared(void *ptr);
  734. void ntfs_unmap_meta(struct super_block *sb, CLST lcn, CLST len);
  735. int ntfs_discard(struct ntfs_sb_info *sbi, CLST Lcn, CLST Len);
  736. /* Globals from bitmap.c*/
  737. int __init ntfs3_init_bitmap(void);
  738. void ntfs3_exit_bitmap(void);
  739. void wnd_close(struct wnd_bitmap *wnd);
  740. static inline size_t wnd_zeroes(const struct wnd_bitmap *wnd)
  741. {
  742. return wnd->total_zeroes;
  743. }
  744. int wnd_init(struct wnd_bitmap *wnd, struct super_block *sb, size_t nbits);
  745. int wnd_set_free(struct wnd_bitmap *wnd, size_t bit, size_t bits);
  746. int wnd_set_used(struct wnd_bitmap *wnd, size_t bit, size_t bits);
  747. int wnd_set_used_safe(struct wnd_bitmap *wnd, size_t bit, size_t bits,
  748. size_t *done);
  749. bool wnd_is_free(struct wnd_bitmap *wnd, size_t bit, size_t bits);
  750. bool wnd_is_used(struct wnd_bitmap *wnd, size_t bit, size_t bits);
  751. /* Possible values for 'flags' 'wnd_find'. */
  752. #define BITMAP_FIND_MARK_AS_USED 0x01
  753. #define BITMAP_FIND_FULL 0x02
  754. size_t wnd_find(struct wnd_bitmap *wnd, size_t to_alloc, size_t hint,
  755. size_t flags, size_t *allocated);
  756. int wnd_extend(struct wnd_bitmap *wnd, size_t new_bits);
  757. void wnd_zone_set(struct wnd_bitmap *wnd, size_t Lcn, size_t Len);
  758. int ntfs_trim_fs(struct ntfs_sb_info *sbi, struct fstrim_range *range);
  759. void ntfs_bitmap_set_le(void *map, unsigned int start, int len);
  760. void ntfs_bitmap_clear_le(void *map, unsigned int start, int len);
  761. unsigned int ntfs_bitmap_weight_le(const void *bitmap, int bits);
  762. /* Globals from upcase.c */
  763. int ntfs_cmp_names(const __le16 *s1, size_t l1, const __le16 *s2, size_t l2,
  764. const u16 *upcase, bool bothcase);
  765. int ntfs_cmp_names_cpu(const struct cpu_str *uni1, const struct le_str *uni2,
  766. const u16 *upcase, bool bothcase);
  767. unsigned long ntfs_names_hash(const u16 *name, size_t len, const u16 *upcase,
  768. unsigned long hash);
  769. /* globals from xattr.c */
  770. #ifdef CONFIG_NTFS3_FS_POSIX_ACL
  771. struct posix_acl *ntfs_get_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  772. int type);
  773. int ntfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  774. struct posix_acl *acl, int type);
  775. int ntfs_init_acl(struct mnt_idmap *idmap, struct inode *inode,
  776. struct inode *dir);
  777. #else
  778. #define ntfs_get_acl NULL
  779. #define ntfs_set_acl NULL
  780. #endif
  781. int ntfs_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry);
  782. ssize_t ntfs_listxattr(struct dentry *dentry, char *buffer, size_t size);
  783. extern const struct xattr_handler *const ntfs_xattr_handlers[];
  784. int ntfs_save_wsl_perm(struct inode *inode, __le16 *ea_size);
  785. void ntfs_get_wsl_perm(struct inode *inode);
  786. /* globals from lznt.c */
  787. struct lznt *get_lznt_ctx(int level);
  788. size_t compress_lznt(const void *uncompressed, size_t uncompressed_size,
  789. void *compressed, size_t compressed_size,
  790. struct lznt *ctx);
  791. ssize_t decompress_lznt(const void *compressed, size_t compressed_size,
  792. void *uncompressed, size_t uncompressed_size);
  793. static inline bool is_ntfs3(struct ntfs_sb_info *sbi)
  794. {
  795. return sbi->volume.major_ver >= 3;
  796. }
  797. /* (sb->s_flags & SB_ACTIVE) */
  798. static inline bool is_mounted(struct ntfs_sb_info *sbi)
  799. {
  800. return !!sbi->sb->s_root;
  801. }
  802. static inline bool ntfs_is_meta_file(struct ntfs_sb_info *sbi, CLST rno)
  803. {
  804. return rno < MFT_REC_FREE || rno == sbi->objid_no ||
  805. rno == sbi->quota_no || rno == sbi->reparse_no ||
  806. rno == sbi->usn_jrnl_no;
  807. }
  808. static inline size_t wnd_zone_bit(const struct wnd_bitmap *wnd)
  809. {
  810. return wnd->zone_bit;
  811. }
  812. static inline size_t wnd_zone_len(const struct wnd_bitmap *wnd)
  813. {
  814. return wnd->zone_end - wnd->zone_bit;
  815. }
  816. static inline void run_init(struct runs_tree *run)
  817. {
  818. run->runs = NULL;
  819. run->count = 0;
  820. run->allocated = 0;
  821. }
  822. static inline struct runs_tree *run_alloc(void)
  823. {
  824. return kzalloc(sizeof(struct runs_tree), GFP_NOFS);
  825. }
  826. static inline void run_close(struct runs_tree *run)
  827. {
  828. kvfree(run->runs);
  829. memset(run, 0, sizeof(*run));
  830. }
  831. static inline void run_free(struct runs_tree *run)
  832. {
  833. if (run) {
  834. kvfree(run->runs);
  835. kfree(run);
  836. }
  837. }
  838. static inline bool run_is_empty(struct runs_tree *run)
  839. {
  840. return !run->count;
  841. }
  842. /* NTFS uses quad aligned bitmaps. */
  843. static inline size_t ntfs3_bitmap_size(size_t bits)
  844. {
  845. return BITS_TO_U64(bits) * sizeof(u64);
  846. }
  847. #define _100ns2seconds 10000000
  848. #define SecondsToStartOf1970 0x00000002B6109100
  849. #define NTFS_TIME_GRAN 100
  850. /*
  851. * kernel2nt - Converts in-memory kernel timestamp into nt time.
  852. */
  853. static inline __le64 kernel2nt(const struct timespec64 *ts)
  854. {
  855. // 10^7 units of 100 nanoseconds one second
  856. return cpu_to_le64(_100ns2seconds *
  857. (ts->tv_sec + SecondsToStartOf1970) +
  858. ts->tv_nsec / NTFS_TIME_GRAN);
  859. }
  860. /*
  861. * nt2kernel - Converts on-disk nt time into kernel timestamp.
  862. */
  863. static inline void nt2kernel(const __le64 tm, struct timespec64 *ts)
  864. {
  865. u64 t = le64_to_cpu(tm) - _100ns2seconds * SecondsToStartOf1970;
  866. // WARNING: do_div changes its first argument(!)
  867. ts->tv_nsec = do_div(t, _100ns2seconds) * 100;
  868. ts->tv_sec = t;
  869. }
  870. static inline struct ntfs_sb_info *ntfs_sb(struct super_block *sb)
  871. {
  872. return sb->s_fs_info;
  873. }
  874. static inline int ntfs3_forced_shutdown(struct super_block *sb)
  875. {
  876. return test_bit(NTFS_FLAGS_SHUTDOWN_BIT, &ntfs_sb(sb)->flags);
  877. }
  878. /*
  879. * ntfs_up_cluster - Align up on cluster boundary.
  880. */
  881. static inline u64 ntfs_up_cluster(const struct ntfs_sb_info *sbi, u64 size)
  882. {
  883. return (size + sbi->cluster_mask) & sbi->cluster_mask_inv;
  884. }
  885. /*
  886. * ntfs_up_block - Align up on cluster boundary.
  887. */
  888. static inline u64 ntfs_up_block(const struct super_block *sb, u64 size)
  889. {
  890. return (size + sb->s_blocksize - 1) & ~(u64)(sb->s_blocksize - 1);
  891. }
  892. static inline CLST bytes_to_cluster(const struct ntfs_sb_info *sbi, u64 size)
  893. {
  894. return (size + sbi->cluster_mask) >> sbi->cluster_bits;
  895. }
  896. static inline u64 bytes_to_block(const struct super_block *sb, u64 size)
  897. {
  898. return (size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  899. }
  900. static inline struct ntfs_inode *ntfs_i(struct inode *inode)
  901. {
  902. return container_of(inode, struct ntfs_inode, vfs_inode);
  903. }
  904. static inline bool is_compressed(const struct ntfs_inode *ni)
  905. {
  906. return (ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) ||
  907. (ni->ni_flags & NI_FLAG_COMPRESSED_MASK);
  908. }
  909. static inline int ni_ext_compress_bits(const struct ntfs_inode *ni)
  910. {
  911. return 0xb + (ni->ni_flags & NI_FLAG_COMPRESSED_MASK);
  912. }
  913. /* Bits - 0xc, 0xd, 0xe, 0xf, 0x10 */
  914. static inline void ni_set_ext_compress_bits(struct ntfs_inode *ni, u8 bits)
  915. {
  916. ni->ni_flags |= (bits - 0xb) & NI_FLAG_COMPRESSED_MASK;
  917. }
  918. static inline bool is_dedup(const struct ntfs_inode *ni)
  919. {
  920. return ni->ni_flags & NI_FLAG_DEDUPLICATED;
  921. }
  922. static inline bool is_encrypted(const struct ntfs_inode *ni)
  923. {
  924. return ni->std_fa & FILE_ATTRIBUTE_ENCRYPTED;
  925. }
  926. static inline bool is_sparsed(const struct ntfs_inode *ni)
  927. {
  928. return ni->std_fa & FILE_ATTRIBUTE_SPARSE_FILE;
  929. }
  930. static inline int is_resident(struct ntfs_inode *ni)
  931. {
  932. return ni->ni_flags & NI_FLAG_RESIDENT;
  933. }
  934. static inline void le16_sub_cpu(__le16 *var, u16 val)
  935. {
  936. *var = cpu_to_le16(le16_to_cpu(*var) - val);
  937. }
  938. static inline void le32_sub_cpu(__le32 *var, u32 val)
  939. {
  940. *var = cpu_to_le32(le32_to_cpu(*var) - val);
  941. }
  942. static inline void nb_put(struct ntfs_buffers *nb)
  943. {
  944. u32 i, nbufs = nb->nbufs;
  945. if (!nbufs)
  946. return;
  947. for (i = 0; i < nbufs; i++)
  948. put_bh(nb->bh[i]);
  949. nb->nbufs = 0;
  950. }
  951. static inline void put_indx_node(struct indx_node *in)
  952. {
  953. if (!in)
  954. return;
  955. kfree(in->index);
  956. nb_put(&in->nb);
  957. kfree(in);
  958. }
  959. static inline void mi_clear(struct mft_inode *mi)
  960. {
  961. nb_put(&mi->nb);
  962. kfree(mi->mrec);
  963. mi->mrec = NULL;
  964. }
  965. static inline void ni_lock(struct ntfs_inode *ni)
  966. {
  967. mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_NORMAL);
  968. }
  969. static inline void ni_lock_dir(struct ntfs_inode *ni)
  970. {
  971. mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_PARENT);
  972. }
  973. static inline void ni_lock_dir2(struct ntfs_inode *ni)
  974. {
  975. mutex_lock_nested(&ni->ni_lock, NTFS_INODE_MUTEX_PARENT2);
  976. }
  977. static inline void ni_unlock(struct ntfs_inode *ni)
  978. {
  979. mutex_unlock(&ni->ni_lock);
  980. }
  981. static inline int ni_trylock(struct ntfs_inode *ni)
  982. {
  983. return mutex_trylock(&ni->ni_lock);
  984. }
  985. static inline int attr_load_runs_attr(struct ntfs_inode *ni,
  986. struct ATTRIB *attr,
  987. struct runs_tree *run, CLST vcn)
  988. {
  989. return attr_load_runs_vcn(ni, attr->type, attr_name(attr),
  990. attr->name_len, run, vcn);
  991. }
  992. static inline void le64_sub_cpu(__le64 *var, u64 val)
  993. {
  994. *var = cpu_to_le64(le64_to_cpu(*var) - val);
  995. }
  996. #if IS_ENABLED(CONFIG_NTFS_FS)
  997. bool is_legacy_ntfs(struct super_block *sb);
  998. #else
  999. static inline bool is_legacy_ntfs(struct super_block *sb)
  1000. {
  1001. return false;
  1002. }
  1003. #endif
  1004. #endif /* _LINUX_NTFS3_NTFS_FS_H */