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