1/*
2 * attrib.h - Exports for attribute handling. Originated from the Linux-NTFS project.
3 *
4 * Copyright (c) 2000-2004 Anton Altaparmakov
5 * Copyright (c) 2004-2005 Yura Pakhuchiy
6 * Copyright (c) 2006-2007 Szabolcs Szakacsits
7 *
8 * This program/include file is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program/include file is distributed in the hope that it will be
14 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program (in the main directory of the NTFS-3G
20 * distribution in the file COPYING); if not, write to the Free Software
21 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 */
23
24#ifndef _NTFS_ATTRIB_H
25#define _NTFS_ATTRIB_H
26
27/* Forward declarations */
28typedef struct _ntfs_attr ntfs_attr;
29typedef struct _ntfs_attr_search_ctx ntfs_attr_search_ctx;
30
31#include "types.h"
32#include "inode.h"
33#include "unistr.h"
34#include "runlist.h"
35#include "volume.h"
36#include "debug.h"
37#include "logging.h"
38
39extern ntfschar AT_UNNAMED[];
40
41/**
42 * enum ntfs_lcn_special_values - special return values for ntfs_*_vcn_to_lcn()
43 *
44 * Special return values for ntfs_rl_vcn_to_lcn() and ntfs_attr_vcn_to_lcn().
45 *
46 * TODO: Describe them.
47 */
48typedef enum {
49	LCN_HOLE		= -1,	/* Keep this as highest value or die! */
50	LCN_RL_NOT_MAPPED	= -2,
51	LCN_ENOENT		= -3,
52	LCN_EINVAL		= -4,
53	LCN_EIO			= -5,
54} ntfs_lcn_special_values;
55
56/**
57 * struct ntfs_attr_search_ctx - search context used in attribute search functions
58 * @mrec:	buffer containing mft record to search
59 * @attr:	attribute record in @mrec where to begin/continue search
60 * @is_first:	if true lookup_attr() begins search with @attr, else after @attr
61 *
62 * Structure must be initialized to zero before the first call to one of the
63 * attribute search functions. Initialize @mrec to point to the mft record to
64 * search, and @attr to point to the first attribute within @mrec (not necessary
65 * if calling the _first() functions), and set @is_first to TRUE (not necessary
66 * if calling the _first() functions).
67 *
68 * If @is_first is TRUE, the search begins with @attr. If @is_first is FALSE,
69 * the search begins after @attr. This is so that, after the first call to one
70 * of the search attribute functions, we can call the function again, without
71 * any modification of the search context, to automagically get the next
72 * matching attribute.
73 */
74struct _ntfs_attr_search_ctx {
75	MFT_RECORD *mrec;
76	ATTR_RECORD *attr;
77	BOOL is_first;
78	ntfs_inode *ntfs_ino;
79	ATTR_LIST_ENTRY *al_entry;
80	ntfs_inode *base_ntfs_ino;
81	MFT_RECORD *base_mrec;
82	ATTR_RECORD *base_attr;
83};
84
85extern void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx);
86extern ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni,
87		MFT_RECORD *mrec);
88extern void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx);
89
90extern int ntfs_attr_lookup(const ATTR_TYPES type, const ntfschar *name,
91		const u32 name_len, const IGNORE_CASE_BOOL ic,
92		const VCN lowest_vcn, const u8 *val, const u32 val_len,
93		ntfs_attr_search_ctx *ctx);
94
95extern int ntfs_attr_position(const ATTR_TYPES type, ntfs_attr_search_ctx *ctx);
96
97extern ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
98		const ATTR_TYPES type);
99
100/**
101 * ntfs_attrs_walk - syntactic sugar for walking all attributes in an inode
102 * @ctx:	initialised attribute search context
103 *
104 * Syntactic sugar for walking attributes in an inode.
105 *
106 * Return 0 on success and -1 on error with errno set to the error code from
107 * ntfs_attr_lookup().
108 *
109 * Example: When you want to enumerate all attributes in an open ntfs inode
110 *	    @ni, you can simply do:
111 *
112 *	int err;
113 *	ntfs_attr_search_ctx *ctx = ntfs_attr_get_search_ctx(ni, NULL);
114 *	if (!ctx)
115 *		// Error code is in errno. Handle this case.
116 *	while (!(err = ntfs_attrs_walk(ctx))) {
117 *		ATTR_RECORD *attr = ctx->attr;
118 *		// attr now contains the next attribute. Do whatever you want
119 *		// with it and then just continue with the while loop.
120 *	}
121 *	if (err && errno != ENOENT)
122 *		// Ooops. An error occurred! You should handle this case.
123 *	// Now finished with all attributes in the inode.
124 */
125static __inline__ int ntfs_attrs_walk(ntfs_attr_search_ctx *ctx)
126{
127	return ntfs_attr_lookup(AT_UNUSED, NULL, 0, CASE_SENSITIVE, 0,
128			NULL, 0, ctx);
129}
130
131/**
132 * struct ntfs_attr - ntfs in memory non-resident attribute structure
133 * @rl:			if not NULL, the decompressed runlist
134 * @ni:			base ntfs inode to which this attribute belongs
135 * @type:		attribute type
136 * @name:		Unicode name of the attribute
137 * @name_len:		length of @name in Unicode characters
138 * @state:		NTFS attribute specific flags describing this attribute
139 * @allocated_size:	copy from the attribute record
140 * @data_size:		copy from the attribute record
141 * @initialized_size:	copy from the attribute record
142 * @compressed_size:	copy from the attribute record
143 * @compression_block_size:		size of a compression block (cb)
144 * @compression_block_size_bits:	log2 of the size of a cb
145 * @compression_block_clusters:		number of clusters per cb
146 *
147 * This structure exists purely to provide a mechanism of caching the runlist
148 * of an attribute. If you want to operate on a particular attribute extent,
149 * you should not be using this structure at all. If you want to work with a
150 * resident attribute, you should not be using this structure at all. As a
151 * fail-safe check make sure to test NAttrNonResident() and if it is false, you
152 * know you shouldn't be using this structure.
153 *
154 * If you want to work on a resident attribute or on a specific attribute
155 * extent, you should use ntfs_lookup_attr() to retrieve the attribute (extent)
156 * record, edit that, and then write back the mft record (or set the
157 * corresponding ntfs inode dirty for delayed write back).
158 *
159 * @rl is the decompressed runlist of the attribute described by this
160 * structure. Obviously this only makes sense if the attribute is not resident,
161 * i.e. NAttrNonResident() is true. If the runlist hasn't been decompressed yet
162 * @rl is NULL, so be prepared to cope with @rl == NULL.
163 *
164 * @ni is the base ntfs inode of the attribute described by this structure.
165 *
166 * @type is the attribute type (see layout.h for the definition of ATTR_TYPES),
167 * @name and @name_len are the little endian Unicode name and the name length
168 * in Unicode characters of the attribute, respectively.
169 *
170 * @state contains NTFS attribute specific flags describing this attribute
171 * structure. See ntfs_attr_state_bits above.
172 */
173struct _ntfs_attr {
174	runlist_element *rl;
175	ntfs_inode *ni;
176	ATTR_TYPES type;
177	ntfschar *name;
178	u32 name_len;
179	unsigned long state;
180	s64 allocated_size;
181	s64 data_size;
182	s64 initialized_size;
183	s64 compressed_size;
184	u32 compression_block_size;
185	u8 compression_block_size_bits;
186	u8 compression_block_clusters;
187};
188
189/**
190 * enum ntfs_attr_state_bits - bits for the state field in the ntfs_attr
191 * structure
192 */
193typedef enum {
194	NA_Initialized,		/* 1: structure is initialized. */
195	NA_NonResident,		/* 1: Attribute is not resident. */
196} ntfs_attr_state_bits;
197
198#define  test_nattr_flag(na, flag)	 test_bit(NA_##flag, (na)->state)
199#define   set_nattr_flag(na, flag)	  set_bit(NA_##flag, (na)->state)
200#define clear_nattr_flag(na, flag)	clear_bit(NA_##flag, (na)->state)
201
202#define NAttrInitialized(na)		 test_nattr_flag(na, Initialized)
203#define NAttrSetInitialized(na)		  set_nattr_flag(na, Initialized)
204#define NAttrClearInitialized(na)	clear_nattr_flag(na, Initialized)
205
206#define NAttrNonResident(na)		 test_nattr_flag(na, NonResident)
207#define NAttrSetNonResident(na)		  set_nattr_flag(na, NonResident)
208#define NAttrClearNonResident(na)	clear_nattr_flag(na, NonResident)
209
210#define GenNAttrIno(func_name, flag)			\
211extern int NAttr##func_name(ntfs_attr *na);		\
212extern void NAttrSet##func_name(ntfs_attr *na);		\
213extern void NAttrClear##func_name(ntfs_attr *na);
214
215GenNAttrIno(Compressed, FILE_ATTR_COMPRESSED)
216GenNAttrIno(Encrypted, 	FILE_ATTR_ENCRYPTED)
217GenNAttrIno(Sparse, 	FILE_ATTR_SPARSE_FILE)
218#undef GenNAttrIno
219
220/**
221 * union attr_val - Union of all known attribute values
222 *
223 * For convenience. Used in the attr structure.
224 */
225typedef union {
226	u8 _default;	/* Unnamed u8 to serve as default when just using
227			   a_val without specifying any of the below. */
228	STANDARD_INFORMATION std_inf;
229	ATTR_LIST_ENTRY al_entry;
230	FILE_NAME_ATTR filename;
231	OBJECT_ID_ATTR obj_id;
232	SECURITY_DESCRIPTOR_ATTR sec_desc;
233	VOLUME_NAME vol_name;
234	VOLUME_INFORMATION vol_inf;
235	DATA_ATTR data;
236	INDEX_ROOT index_root;
237	INDEX_BLOCK index_blk;
238	BITMAP_ATTR bmp;
239	REPARSE_POINT reparse;
240	EA_INFORMATION ea_inf;
241	EA_ATTR ea;
242	PROPERTY_SET property_set;
243	LOGGED_UTILITY_STREAM logged_util_stream;
244	EFS_ATTR_HEADER efs;
245} attr_val;
246
247extern void ntfs_attr_init(ntfs_attr *na, const BOOL non_resident,
248		const BOOL compressed, const BOOL encrypted, const BOOL sparse,
249		const s64 allocated_size, const s64 data_size,
250		const s64 initialized_size, const s64 compressed_size,
251		const u8 compression_unit);
252
253extern ntfs_attr *ntfs_attr_open(ntfs_inode *ni, const ATTR_TYPES type,
254		ntfschar *name, u32 name_len);
255extern void ntfs_attr_close(ntfs_attr *na);
256
257extern s64 ntfs_attr_pread(ntfs_attr *na, const s64 pos, s64 count,
258		void *b);
259extern s64 ntfs_attr_pwrite(ntfs_attr *na, const s64 pos, s64 count,
260		const void *b);
261
262extern void *ntfs_attr_readall(ntfs_inode *ni, const ATTR_TYPES type,
263			       ntfschar *name, u32 name_len, s64 *data_size);
264
265extern s64 ntfs_attr_mst_pread(ntfs_attr *na, const s64 pos,
266		const s64 bk_cnt, const u32 bk_size, void *dst);
267extern s64 ntfs_attr_mst_pwrite(ntfs_attr *na, const s64 pos,
268		s64 bk_cnt, const u32 bk_size, void *src);
269
270extern int ntfs_attr_map_runlist(ntfs_attr *na, VCN vcn);
271extern int ntfs_attr_map_whole_runlist(ntfs_attr *na);
272
273extern LCN ntfs_attr_vcn_to_lcn(ntfs_attr *na, const VCN vcn);
274extern runlist_element *ntfs_attr_find_vcn(ntfs_attr *na, const VCN vcn);
275
276extern int ntfs_attr_size_bounds_check(const ntfs_volume *vol,
277		const ATTR_TYPES type, const s64 size);
278extern int ntfs_attr_can_be_non_resident(const ntfs_volume *vol,
279		const ATTR_TYPES type);
280extern int ntfs_attr_can_be_resident(const ntfs_volume *vol,
281		const ATTR_TYPES type);
282
283extern int ntfs_make_room_for_attr(MFT_RECORD *m, u8 *pos, u32 size);
284
285extern int ntfs_resident_attr_record_add(ntfs_inode *ni, ATTR_TYPES type,
286		ntfschar *name, u8 name_len, u8 *val, u32 size,
287		ATTR_FLAGS flags);
288extern int ntfs_non_resident_attr_record_add(ntfs_inode *ni, ATTR_TYPES type,
289		ntfschar *name, u8 name_len, VCN lowest_vcn, int dataruns_size,
290		ATTR_FLAGS flags);
291extern int ntfs_attr_record_rm(ntfs_attr_search_ctx *ctx);
292
293extern int ntfs_attr_add(ntfs_inode *ni, ATTR_TYPES type,
294		ntfschar *name, u8 name_len, u8 *val, s64 size);
295extern int ntfs_attr_rm(ntfs_attr *na);
296
297extern int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size);
298
299extern int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
300		const u32 new_size);
301
302extern int ntfs_attr_record_move_to(ntfs_attr_search_ctx *ctx, ntfs_inode *ni);
303extern int ntfs_attr_record_move_away(ntfs_attr_search_ctx *ctx, int extra);
304
305extern int ntfs_attr_update_mapping_pairs(ntfs_attr *na, VCN from_vcn);
306
307extern int ntfs_attr_truncate(ntfs_attr *na, const s64 newsize);
308
309/**
310 * get_attribute_value_length - return the length of the value of an attribute
311 * @a:	pointer to a buffer containing the attribute record
312 *
313 * Return the byte size of the attribute value of the attribute @a (as it
314 * would be after eventual decompression and filling in of holes if sparse).
315 * If we return 0, check errno. If errno is 0 the actual length was 0,
316 * otherwise errno describes the error.
317 *
318 * FIXME: Describe possible errnos.
319 */
320extern s64 ntfs_get_attribute_value_length(const ATTR_RECORD *a);
321
322/**
323 * get_attribute_value - return the attribute value of an attribute
324 * @vol:	volume on which the attribute is present
325 * @a:		attribute to get the value of
326 * @b:		destination buffer for the attribute value
327 *
328 * Make a copy of the attribute value of the attribute @a into the destination
329 * buffer @b. Note, that the size of @b has to be at least equal to the value
330 * returned by get_attribute_value_length(@a).
331 *
332 * Return number of bytes copied. If this is zero check errno. If errno is 0
333 * then nothing was read due to a zero-length attribute value, otherwise
334 * errno describes the error.
335 */
336extern s64 ntfs_get_attribute_value(const ntfs_volume *vol,
337				    const ATTR_RECORD *a, u8 *b);
338
339extern void  ntfs_attr_name_free(char **name);
340extern char *ntfs_attr_name_get(const ntfschar *uname, const int uname_len);
341extern int   ntfs_attr_exist(ntfs_inode *ni, const ATTR_TYPES type,
342			     ntfschar *name, u32 name_len);
343extern int   ntfs_attr_remove(ntfs_inode *ni, const ATTR_TYPES type,
344			      ntfschar *name, u32 name_len);
345extern s64   ntfs_attr_get_free_bits(ntfs_attr *na);
346
347#endif /* defined _NTFS_ATTRIB_H */
348
349