1/*
2 * inode.h - Defines for NTFS inode handling. Originated from the Linux-NTFS project.
3 *
4 * Copyright (c) 2001-2004 Anton Altaparmakov
5 * Copyright (c) 2004-2007 Yura Pakhuchiy
6 * Copyright (c) 2004-2005 Richard Russon
7 * Copyright (c) 2006-2008 Szabolcs Szakacsits
8 *
9 * This program/include file is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program/include file is distributed in the hope that it will be
15 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program (in the main directory of the NTFS-3G
21 * distribution in the file COPYING); if not, write to the Free Software
22 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25#ifndef _NTFS_INODE_H
26#define _NTFS_INODE_H
27
28/* Forward declaration */
29typedef struct _ntfs_inode ntfs_inode;
30
31#include "types.h"
32#include "layout.h"
33#include "support.h"
34#include "volume.h"
35#include "ntfstime.h"
36
37/**
38 * enum ntfs_inode_state_bits -
39 *
40 * Defined bits for the state field in the ntfs_inode structure.
41 * (f) = files only, (d) = directories only
42 */
43typedef enum {
44	NI_Dirty,		/* 1: Mft record needs to be written to disk. */
45
46	/* The NI_AttrList* tests only make sense for base inodes. */
47	NI_AttrList,		/* 1: Mft record contains an attribute list. */
48	NI_AttrListDirty,	/* 1: Attribute list needs to be written to the
49				      mft record and then to disk. */
50	NI_FileNameDirty,	/* 1: FILE_NAME attributes need to be updated
51				      in the index. */
52	NI_v3_Extensions,	/* 1: JPA v3.x extensions present. */
53	NI_TimesSet,		/* 1: Use times which were set */
54	NI_KnownSize,		/* 1: Set if sizes are meaningful */
55} ntfs_inode_state_bits;
56
57#define  test_nino_flag(ni, flag)	   test_bit(NI_##flag, (ni)->state)
58#define   set_nino_flag(ni, flag)	    set_bit(NI_##flag, (ni)->state)
59#define clear_nino_flag(ni, flag)	  clear_bit(NI_##flag, (ni)->state)
60
61#define test_and_set_nino_flag(ni, flag)	\
62				   test_and_set_bit(NI_##flag, (ni)->state)
63#define test_and_clear_nino_flag(ni, flag)	\
64				 test_and_clear_bit(NI_##flag, (ni)->state)
65
66#define NInoDirty(ni)				  test_nino_flag(ni, Dirty)
67#define NInoSetDirty(ni)			   set_nino_flag(ni, Dirty)
68#define NInoClearDirty(ni)			 clear_nino_flag(ni, Dirty)
69#define NInoTestAndSetDirty(ni)		  test_and_set_nino_flag(ni, Dirty)
70#define NInoTestAndClearDirty(ni)	test_and_clear_nino_flag(ni, Dirty)
71
72#define NInoAttrList(ni)			  test_nino_flag(ni, AttrList)
73#define NInoSetAttrList(ni)			   set_nino_flag(ni, AttrList)
74#define NInoClearAttrList(ni)			 clear_nino_flag(ni, AttrList)
75
76
77#define  test_nino_al_flag(ni, flag)	 test_nino_flag(ni, AttrList##flag)
78#define   set_nino_al_flag(ni, flag)	  set_nino_flag(ni, AttrList##flag)
79#define clear_nino_al_flag(ni, flag)	clear_nino_flag(ni, AttrList##flag)
80
81#define test_and_set_nino_al_flag(ni, flag)	\
82				 test_and_set_nino_flag(ni, AttrList##flag)
83#define test_and_clear_nino_al_flag(ni, flag)	\
84			       test_and_clear_nino_flag(ni, AttrList##flag)
85
86#define NInoAttrListDirty(ni)			    test_nino_al_flag(ni, Dirty)
87#define NInoAttrListSetDirty(ni)		     set_nino_al_flag(ni, Dirty)
88#define NInoAttrListClearDirty(ni)		   clear_nino_al_flag(ni, Dirty)
89#define NInoAttrListTestAndSetDirty(ni)	    test_and_set_nino_al_flag(ni, Dirty)
90#define NInoAttrListTestAndClearDirty(ni) test_and_clear_nino_al_flag(ni, Dirty)
91
92#define NInoFileNameDirty(ni)                 test_nino_flag(ni, FileNameDirty)
93#define NInoFileNameSetDirty(ni)               set_nino_flag(ni, FileNameDirty)
94#define NInoFileNameClearDirty(ni)           clear_nino_flag(ni, FileNameDirty)
95#define NInoFileNameTestAndSetDirty(ni)		\
96				      test_and_set_nino_flag(ni, FileNameDirty)
97#define NInoFileNameTestAndClearDirty(ni)	\
98				    test_and_clear_nino_flag(ni, FileNameDirty)
99
100/**
101 * struct _ntfs_inode - The NTFS in-memory inode structure.
102 *
103 * It is just used as an extension to the fields already provided in the VFS
104 * inode.
105 */
106struct _ntfs_inode {
107	u64 mft_no;		/* Inode / mft record number. */
108	MFT_RECORD *mrec;	/* The actual mft record of the inode. */
109	ntfs_volume *vol;	/* Pointer to the ntfs volume of this inode. */
110	unsigned long state;	/* NTFS specific flags describing this inode.
111				   See ntfs_inode_state_bits above. */
112	FILE_ATTR_FLAGS flags;	/* Flags describing the file.
113				   (Copy from STANDARD_INFORMATION) */
114	/*
115	 * Attribute list support (for use by the attribute lookup functions).
116	 * Setup during ntfs_open_inode() for all inodes with attribute lists.
117	 * Only valid if NI_AttrList is set in state.
118	 */
119	u32 attr_list_size;	/* Length of attribute list value in bytes. */
120	u8 *attr_list;		/* Attribute list value itself. */
121	/* Below fields are always valid. */
122	s32 nr_extents;		/* For a base mft record, the number of
123				   attached extent inodes (0 if none), for
124				   extent records this is -1. */
125	union {		/* This union is only used if nr_extents != 0. */
126		ntfs_inode **extent_nis;/* For nr_extents > 0, array of the
127					   ntfs inodes of the extent mft
128					   records belonging to this base
129					   inode which have been loaded. */
130		ntfs_inode *base_ni;	/* For nr_extents == -1, the ntfs
131					   inode of the base mft record. */
132	};
133
134	/* Below fields are valid only for base inode. */
135
136	/*
137	 * These two fields are used to sync filename index and guaranteed to be
138	 * correct, however value in index itself maybe wrong (windows itself
139	 * do not update them properly).
140	 * For directories, they hold the index size, provided the
141	 * flag KnownSize is set.
142	 */
143	s64 data_size;		/* Data size of unnamed DATA attribute
144				   (or INDEX_ROOT for directories) */
145	s64 allocated_size;	/* Allocated size stored in the filename
146				   index. (NOTE: Equal to allocated size of
147				   the unnamed data attribute for normal or
148				   encrypted files and to compressed size
149				   of the unnamed data attribute for sparse or
150				   compressed files.) */
151
152	/*
153	 * These four fields are copy of relevant fields from
154	 * STANDARD_INFORMATION attribute and used to sync it and FILE_NAME
155	 * attribute in the index.
156	 */
157	ntfs_time creation_time;
158	ntfs_time last_data_change_time;
159	ntfs_time last_mft_change_time;
160	ntfs_time last_access_time;
161				/* NTFS 3.x extensions added by JPA */
162				/* only if NI_v3_Extensions is set in state */
163 	le32 owner_id;
164	le32 security_id;
165	le64 quota_charged;
166	le64 usn;
167};
168
169typedef enum {
170	NTFS_UPDATE_ATIME = 1 << 0,
171	NTFS_UPDATE_MTIME = 1 << 1,
172	NTFS_UPDATE_CTIME = 1 << 2,
173} ntfs_time_update_flags;
174
175#define NTFS_UPDATE_MCTIME  (NTFS_UPDATE_MTIME | NTFS_UPDATE_CTIME)
176#define NTFS_UPDATE_AMCTIME (NTFS_UPDATE_ATIME | NTFS_UPDATE_MCTIME)
177
178extern ntfs_inode *ntfs_inode_base(ntfs_inode *ni);
179
180extern ntfs_inode *ntfs_inode_allocate(ntfs_volume *vol);
181
182extern ntfs_inode *ntfs_inode_open(ntfs_volume *vol, const MFT_REF mref);
183
184extern int ntfs_inode_close(ntfs_inode *ni);
185extern int ntfs_inode_close_in_dir(ntfs_inode *ni, ntfs_inode *dir_ni);
186
187#if CACHE_NIDATA_SIZE
188
189struct CACHED_GENERIC;
190
191extern int ntfs_inode_real_close(ntfs_inode *ni);
192extern void ntfs_inode_invalidate(ntfs_volume *vol, const MFT_REF mref);
193extern void ntfs_inode_nidata_free(const struct CACHED_GENERIC *cached);
194extern int ntfs_inode_nidata_hash(const struct CACHED_GENERIC *item);
195
196#endif
197
198
199extern ntfs_inode *ntfs_extent_inode_open(ntfs_inode *base_ni,
200		const leMFT_REF mref);
201
202extern int ntfs_inode_attach_all_extents(ntfs_inode *ni);
203
204extern void ntfs_inode_mark_dirty(ntfs_inode *ni);
205
206extern void ntfs_inode_update_times(ntfs_inode *ni, ntfs_time_update_flags mask);
207
208extern int ntfs_inode_sync(ntfs_inode *ni);
209
210extern int ntfs_inode_add_attrlist(ntfs_inode *ni);
211
212extern int ntfs_inode_free_space(ntfs_inode *ni, int size);
213
214extern int ntfs_inode_badclus_bad(u64 mft_no, ATTR_RECORD *a);
215
216extern int ntfs_inode_get_times(ntfs_inode *ni, char *value, size_t size);
217
218extern int ntfs_inode_set_times(ntfs_inode *ni, const char *value,
219			size_t size, int flags);
220
221/* debugging */
222#define debug_double_inode(num, type)
223#define debug_cached_inode(ni)
224
225#endif /* defined _NTFS_INODE_H */
226