inode.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * inode.h - Defines for inode structures NTFS Linux kernel driver. Part of
  4. * the Linux-NTFS project.
  5. *
  6. * Copyright (c) 2001-2007 Anton Altaparmakov
  7. * Copyright (c) 2002 Richard Russon
  8. */
  9. #ifndef _LINUX_NTFS_INODE_H
  10. #define _LINUX_NTFS_INODE_H
  11. #include <linux/atomic.h>
  12. #include <linux/fs.h>
  13. #include <linux/list.h>
  14. #include <linux/mm.h>
  15. #include <linux/mutex.h>
  16. #include <linux/seq_file.h>
  17. #include "layout.h"
  18. #include "volume.h"
  19. #include "types.h"
  20. #include "runlist.h"
  21. #include "debug.h"
  22. typedef struct _ntfs_inode ntfs_inode;
  23. /*
  24. * The NTFS in-memory inode structure. It is just used as an extension to the
  25. * fields already provided in the VFS inode.
  26. */
  27. struct _ntfs_inode {
  28. rwlock_t size_lock; /* Lock serializing access to inode sizes. */
  29. s64 initialized_size; /* Copy from the attribute record. */
  30. s64 allocated_size; /* Copy from the attribute record. */
  31. unsigned long state; /* NTFS specific flags describing this inode.
  32. See ntfs_inode_state_bits below. */
  33. unsigned long mft_no; /* Number of the mft record / inode. */
  34. u16 seq_no; /* Sequence number of the mft record. */
  35. atomic_t count; /* Inode reference count for book keeping. */
  36. ntfs_volume *vol; /* Pointer to the ntfs volume of this inode. */
  37. /*
  38. * If NInoAttr() is true, the below fields describe the attribute which
  39. * this fake inode belongs to. The actual inode of this attribute is
  40. * pointed to by base_ntfs_ino and nr_extents is always set to -1 (see
  41. * below). For real inodes, we also set the type (AT_DATA for files and
  42. * AT_INDEX_ALLOCATION for directories), with the name = NULL and
  43. * name_len = 0 for files and name = I30 (global constant) and
  44. * name_len = 4 for directories.
  45. */
  46. ATTR_TYPE type; /* Attribute type of this fake inode. */
  47. ntfschar *name; /* Attribute name of this fake inode. */
  48. u32 name_len; /* Attribute name length of this fake inode. */
  49. runlist runlist; /* If state has the NI_NonResident bit set,
  50. the runlist of the unnamed data attribute
  51. (if a file) or of the index allocation
  52. attribute (directory) or of the attribute
  53. described by the fake inode (if NInoAttr()).
  54. If runlist.rl is NULL, the runlist has not
  55. been read in yet or has been unmapped. If
  56. NI_NonResident is clear, the attribute is
  57. resident (file and fake inode) or there is
  58. no $I30 index allocation attribute
  59. (small directory). In the latter case
  60. runlist.rl is always NULL.*/
  61. /*
  62. * The following fields are only valid for real inodes and extent
  63. * inodes.
  64. */
  65. struct mutex mrec_lock; /* Lock for serializing access to the
  66. mft record belonging to this inode. */
  67. struct page *page; /* The page containing the mft record of the
  68. inode. This should only be touched by the
  69. (un)map_mft_record*() functions. */
  70. int page_ofs; /* Offset into the page at which the mft record
  71. begins. This should only be touched by the
  72. (un)map_mft_record*() functions. */
  73. /*
  74. * Attribute list support (only for use by the attribute lookup
  75. * functions). Setup during read_inode for all inodes with attribute
  76. * lists. Only valid if NI_AttrList is set in state, and attr_list_rl is
  77. * further only valid if NI_AttrListNonResident is set.
  78. */
  79. u32 attr_list_size; /* Length of attribute list value in bytes. */
  80. u8 *attr_list; /* Attribute list value itself. */
  81. runlist attr_list_rl; /* Run list for the attribute list value. */
  82. union {
  83. struct { /* It is a directory, $MFT, or an index inode. */
  84. u32 block_size; /* Size of an index block. */
  85. u32 vcn_size; /* Size of a vcn in this
  86. index. */
  87. COLLATION_RULE collation_rule; /* The collation rule
  88. for the index. */
  89. u8 block_size_bits; /* Log2 of the above. */
  90. u8 vcn_size_bits; /* Log2 of the above. */
  91. } index;
  92. struct { /* It is a compressed/sparse file/attribute inode. */
  93. s64 size; /* Copy of compressed_size from
  94. $DATA. */
  95. u32 block_size; /* Size of a compression block
  96. (cb). */
  97. u8 block_size_bits; /* Log2 of the size of a cb. */
  98. u8 block_clusters; /* Number of clusters per cb. */
  99. } compressed;
  100. } itype;
  101. struct mutex extent_lock; /* Lock for accessing/modifying the
  102. below . */
  103. s32 nr_extents; /* For a base mft record, the number of attached extent
  104. inodes (0 if none), for extent records and for fake
  105. inodes describing an attribute this is -1. */
  106. union { /* This union is only used if nr_extents != 0. */
  107. ntfs_inode **extent_ntfs_inos; /* For nr_extents > 0, array of
  108. the ntfs inodes of the extent
  109. mft records belonging to
  110. this base inode which have
  111. been loaded. */
  112. ntfs_inode *base_ntfs_ino; /* For nr_extents == -1, the
  113. ntfs inode of the base mft
  114. record. For fake inodes, the
  115. real (base) inode to which
  116. the attribute belongs. */
  117. } ext;
  118. };
  119. /*
  120. * Defined bits for the state field in the ntfs_inode structure.
  121. * (f) = files only, (d) = directories only, (a) = attributes/fake inodes only
  122. */
  123. typedef enum {
  124. NI_Dirty, /* 1: Mft record needs to be written to disk. */
  125. NI_AttrList, /* 1: Mft record contains an attribute list. */
  126. NI_AttrListNonResident, /* 1: Attribute list is non-resident. Implies
  127. NI_AttrList is set. */
  128. NI_Attr, /* 1: Fake inode for attribute i/o.
  129. 0: Real inode or extent inode. */
  130. NI_MstProtected, /* 1: Attribute is protected by MST fixups.
  131. 0: Attribute is not protected by fixups. */
  132. NI_NonResident, /* 1: Unnamed data attr is non-resident (f).
  133. 1: Attribute is non-resident (a). */
  134. NI_IndexAllocPresent = NI_NonResident, /* 1: $I30 index alloc attr is
  135. present (d). */
  136. NI_Compressed, /* 1: Unnamed data attr is compressed (f).
  137. 1: Create compressed files by default (d).
  138. 1: Attribute is compressed (a). */
  139. NI_Encrypted, /* 1: Unnamed data attr is encrypted (f).
  140. 1: Create encrypted files by default (d).
  141. 1: Attribute is encrypted (a). */
  142. NI_Sparse, /* 1: Unnamed data attr is sparse (f).
  143. 1: Create sparse files by default (d).
  144. 1: Attribute is sparse (a). */
  145. NI_SparseDisabled, /* 1: May not create sparse regions. */
  146. NI_TruncateFailed, /* 1: Last ntfs_truncate() call failed. */
  147. } ntfs_inode_state_bits;
  148. /*
  149. * NOTE: We should be adding dirty mft records to a list somewhere and they
  150. * should be independent of the (ntfs/vfs) inode structure so that an inode can
  151. * be removed but the record can be left dirty for syncing later.
  152. */
  153. /*
  154. * Macro tricks to expand the NInoFoo(), NInoSetFoo(), and NInoClearFoo()
  155. * functions.
  156. */
  157. #define NINO_FNS(flag) \
  158. static inline int NIno##flag(ntfs_inode *ni) \
  159. { \
  160. return test_bit(NI_##flag, &(ni)->state); \
  161. } \
  162. static inline void NInoSet##flag(ntfs_inode *ni) \
  163. { \
  164. set_bit(NI_##flag, &(ni)->state); \
  165. } \
  166. static inline void NInoClear##flag(ntfs_inode *ni) \
  167. { \
  168. clear_bit(NI_##flag, &(ni)->state); \
  169. }
  170. /*
  171. * As above for NInoTestSetFoo() and NInoTestClearFoo().
  172. */
  173. #define TAS_NINO_FNS(flag) \
  174. static inline int NInoTestSet##flag(ntfs_inode *ni) \
  175. { \
  176. return test_and_set_bit(NI_##flag, &(ni)->state); \
  177. } \
  178. static inline int NInoTestClear##flag(ntfs_inode *ni) \
  179. { \
  180. return test_and_clear_bit(NI_##flag, &(ni)->state); \
  181. }
  182. /* Emit the ntfs inode bitops functions. */
  183. NINO_FNS(Dirty)
  184. TAS_NINO_FNS(Dirty)
  185. NINO_FNS(AttrList)
  186. NINO_FNS(AttrListNonResident)
  187. NINO_FNS(Attr)
  188. NINO_FNS(MstProtected)
  189. NINO_FNS(NonResident)
  190. NINO_FNS(IndexAllocPresent)
  191. NINO_FNS(Compressed)
  192. NINO_FNS(Encrypted)
  193. NINO_FNS(Sparse)
  194. NINO_FNS(SparseDisabled)
  195. NINO_FNS(TruncateFailed)
  196. /*
  197. * The full structure containing a ntfs_inode and a vfs struct inode. Used for
  198. * all real and fake inodes but not for extent inodes which lack the vfs struct
  199. * inode.
  200. */
  201. typedef struct {
  202. ntfs_inode ntfs_inode;
  203. struct inode vfs_inode; /* The vfs inode structure. */
  204. } big_ntfs_inode;
  205. /**
  206. * NTFS_I - return the ntfs inode given a vfs inode
  207. * @inode: VFS inode
  208. *
  209. * NTFS_I() returns the ntfs inode associated with the VFS @inode.
  210. */
  211. static inline ntfs_inode *NTFS_I(struct inode *inode)
  212. {
  213. return (ntfs_inode *)container_of(inode, big_ntfs_inode, vfs_inode);
  214. }
  215. static inline struct inode *VFS_I(ntfs_inode *ni)
  216. {
  217. return &((big_ntfs_inode *)ni)->vfs_inode;
  218. }
  219. /**
  220. * ntfs_attr - ntfs in memory attribute structure
  221. * @mft_no: mft record number of the base mft record of this attribute
  222. * @name: Unicode name of the attribute (NULL if unnamed)
  223. * @name_len: length of @name in Unicode characters (0 if unnamed)
  224. * @type: attribute type (see layout.h)
  225. *
  226. * This structure exists only to provide a small structure for the
  227. * ntfs_{attr_}iget()/ntfs_test_inode()/ntfs_init_locked_inode() mechanism.
  228. *
  229. * NOTE: Elements are ordered by size to make the structure as compact as
  230. * possible on all architectures.
  231. */
  232. typedef struct {
  233. unsigned long mft_no;
  234. ntfschar *name;
  235. u32 name_len;
  236. ATTR_TYPE type;
  237. } ntfs_attr;
  238. extern int ntfs_test_inode(struct inode *vi, void *data);
  239. extern struct inode *ntfs_iget(struct super_block *sb, unsigned long mft_no);
  240. extern struct inode *ntfs_attr_iget(struct inode *base_vi, ATTR_TYPE type,
  241. ntfschar *name, u32 name_len);
  242. extern struct inode *ntfs_index_iget(struct inode *base_vi, ntfschar *name,
  243. u32 name_len);
  244. extern struct inode *ntfs_alloc_big_inode(struct super_block *sb);
  245. extern void ntfs_free_big_inode(struct inode *inode);
  246. extern void ntfs_evict_big_inode(struct inode *vi);
  247. extern void __ntfs_init_inode(struct super_block *sb, ntfs_inode *ni);
  248. static inline void ntfs_init_big_inode(struct inode *vi)
  249. {
  250. ntfs_inode *ni = NTFS_I(vi);
  251. ntfs_debug("Entering.");
  252. __ntfs_init_inode(vi->i_sb, ni);
  253. ni->mft_no = vi->i_ino;
  254. }
  255. extern ntfs_inode *ntfs_new_extent_inode(struct super_block *sb,
  256. unsigned long mft_no);
  257. extern void ntfs_clear_extent_inode(ntfs_inode *ni);
  258. extern int ntfs_read_inode_mount(struct inode *vi);
  259. extern int ntfs_show_options(struct seq_file *sf, struct dentry *root);
  260. #ifdef NTFS_RW
  261. extern int ntfs_truncate(struct inode *vi);
  262. extern void ntfs_truncate_vfs(struct inode *vi);
  263. extern int ntfs_setattr(struct dentry *dentry, struct iattr *attr);
  264. extern int __ntfs_write_inode(struct inode *vi, int sync);
  265. static inline void ntfs_commit_inode(struct inode *vi)
  266. {
  267. if (!is_bad_inode(vi))
  268. __ntfs_write_inode(vi, 1);
  269. return;
  270. }
  271. #else
  272. static inline void ntfs_truncate_vfs(struct inode *vi) {}
  273. #endif /* NTFS_RW */
  274. #endif /* _LINUX_NTFS_INODE_H */