xref: /haiku/src/add-ons/kernel/file_systems/ntfs/libntfs/attrib.h (revision 2cad94c1c30b6223ad8c08710b26e071d32e9979)
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  * Copyright (c) 2010      Jean-Pierre Andre
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_ATTRIB_H
26 #define _NTFS_ATTRIB_H
27 
28 /* Forward declarations */
29 typedef struct _ntfs_attr ntfs_attr;
30 typedef struct _ntfs_attr_search_ctx ntfs_attr_search_ctx;
31 
32 #include "types.h"
33 #include "inode.h"
34 #include "unistr.h"
35 #include "runlist.h"
36 #include "volume.h"
37 #include "debug.h"
38 #include "logging.h"
39 
40 extern ntfschar AT_UNNAMED[];
41 extern ntfschar STREAM_SDS[];
42 
43 /* The little endian Unicode string $TXF_DATA as a global constant. */
44 extern ntfschar TXF_DATA[10];
45 
46 /**
47  * enum ntfs_lcn_special_values - special return values for ntfs_*_vcn_to_lcn()
48  *
49  * Special return values for ntfs_rl_vcn_to_lcn() and ntfs_attr_vcn_to_lcn().
50  *
51  * TODO: Describe them.
52  */
53 typedef enum {
54 	LCN_HOLE		= -1,	/* Keep this as highest value or die! */
55 	LCN_RL_NOT_MAPPED	= -2,
56 	LCN_ENOENT		= -3,
57 	LCN_EINVAL		= -4,
58 	LCN_EIO			= -5,
59 } ntfs_lcn_special_values;
60 
61 typedef enum {			/* ways of processing holes when expanding */
62 	HOLES_NO,
63 	HOLES_OK,
64 	HOLES_DELAY,
65 	HOLES_NONRES
66 } hole_type;
67 
68 /**
69  * struct ntfs_attr_search_ctx - search context used in attribute search functions
70  * @mrec:	buffer containing mft record to search
71  * @attr:	attribute record in @mrec where to begin/continue search
72  * @is_first:	if true lookup_attr() begins search with @attr, else after @attr
73  *
74  * Structure must be initialized to zero before the first call to one of the
75  * attribute search functions. Initialize @mrec to point to the mft record to
76  * search, and @attr to point to the first attribute within @mrec (not necessary
77  * if calling the _first() functions), and set @is_first to TRUE (not necessary
78  * if calling the _first() functions).
79  *
80  * If @is_first is TRUE, the search begins with @attr. If @is_first is FALSE,
81  * the search begins after @attr. This is so that, after the first call to one
82  * of the search attribute functions, we can call the function again, without
83  * any modification of the search context, to automagically get the next
84  * matching attribute.
85  */
86 struct _ntfs_attr_search_ctx {
87 	MFT_RECORD *mrec;
88 	ATTR_RECORD *attr;
89 	BOOL is_first;
90 	ntfs_inode *ntfs_ino;
91 	ATTR_LIST_ENTRY *al_entry;
92 	ntfs_inode *base_ntfs_ino;
93 	MFT_RECORD *base_mrec;
94 	ATTR_RECORD *base_attr;
95 };
96 
97 extern void ntfs_attr_reinit_search_ctx(ntfs_attr_search_ctx *ctx);
98 extern ntfs_attr_search_ctx *ntfs_attr_get_search_ctx(ntfs_inode *ni,
99 		MFT_RECORD *mrec);
100 extern void ntfs_attr_put_search_ctx(ntfs_attr_search_ctx *ctx);
101 
102 extern int ntfs_attr_lookup(const ATTR_TYPES type, const ntfschar *name,
103 		const u32 name_len, const IGNORE_CASE_BOOL ic,
104 		const VCN lowest_vcn, const u8 *val, const u32 val_len,
105 		ntfs_attr_search_ctx *ctx);
106 
107 extern int ntfs_attr_position(const ATTR_TYPES type, ntfs_attr_search_ctx *ctx);
108 
109 extern ATTR_DEF *ntfs_attr_find_in_attrdef(const ntfs_volume *vol,
110 		const ATTR_TYPES type);
111 
112 /**
113  * ntfs_attrs_walk - syntactic sugar for walking all attributes in an inode
114  * @ctx:	initialised attribute search context
115  *
116  * Syntactic sugar for walking attributes in an inode.
117  *
118  * Return 0 on success and -1 on error with errno set to the error code from
119  * ntfs_attr_lookup().
120  *
121  * Example: When you want to enumerate all attributes in an open ntfs inode
122  *	    @ni, you can simply do:
123  *
124  *	int err;
125  *	ntfs_attr_search_ctx *ctx = ntfs_attr_get_search_ctx(ni, NULL);
126  *	if (!ctx)
127  *		// Error code is in errno. Handle this case.
128  *	while (!(err = ntfs_attrs_walk(ctx))) {
129  *		ATTR_RECORD *attr = ctx->attr;
130  *		// attr now contains the next attribute. Do whatever you want
131  *		// with it and then just continue with the while loop.
132  *	}
133  *	if (err && errno != ENOENT)
134  *		// Ooops. An error occurred! You should handle this case.
135  *	// Now finished with all attributes in the inode.
136  */
137 static __inline__ int ntfs_attrs_walk(ntfs_attr_search_ctx *ctx)
138 {
139 	return ntfs_attr_lookup(AT_UNUSED, NULL, 0, CASE_SENSITIVE, 0,
140 			NULL, 0, ctx);
141 }
142 
143 /**
144  * struct ntfs_attr - ntfs in memory non-resident attribute structure
145  * @rl:			if not NULL, the decompressed runlist
146  * @ni:			base ntfs inode to which this attribute belongs
147  * @type:		attribute type
148  * @name:		Unicode name of the attribute
149  * @name_len:		length of @name in Unicode characters
150  * @state:		NTFS attribute specific flags describing this attribute
151  * @allocated_size:	copy from the attribute record
152  * @data_size:		copy from the attribute record
153  * @initialized_size:	copy from the attribute record
154  * @compressed_size:	copy from the attribute record
155  * @compression_block_size:		size of a compression block (cb)
156  * @compression_block_size_bits:	log2 of the size of a cb
157  * @compression_block_clusters:		number of clusters per cb
158  *
159  * This structure exists purely to provide a mechanism of caching the runlist
160  * of an attribute. If you want to operate on a particular attribute extent,
161  * you should not be using this structure at all. If you want to work with a
162  * resident attribute, you should not be using this structure at all. As a
163  * fail-safe check make sure to test NAttrNonResident() and if it is false, you
164  * know you shouldn't be using this structure.
165  *
166  * If you want to work on a resident attribute or on a specific attribute
167  * extent, you should use ntfs_lookup_attr() to retrieve the attribute (extent)
168  * record, edit that, and then write back the mft record (or set the
169  * corresponding ntfs inode dirty for delayed write back).
170  *
171  * @rl is the decompressed runlist of the attribute described by this
172  * structure. Obviously this only makes sense if the attribute is not resident,
173  * i.e. NAttrNonResident() is true. If the runlist hasn't been decompressed yet
174  * @rl is NULL, so be prepared to cope with @rl == NULL.
175  *
176  * @ni is the base ntfs inode of the attribute described by this structure.
177  *
178  * @type is the attribute type (see layout.h for the definition of ATTR_TYPES),
179  * @name and @name_len are the little endian Unicode name and the name length
180  * in Unicode characters of the attribute, respectively.
181  *
182  * @state contains NTFS attribute specific flags describing this attribute
183  * structure. See ntfs_attr_state_bits above.
184  */
185 struct _ntfs_attr {
186 	runlist_element *rl;
187 	ntfs_inode *ni;
188 	ATTR_TYPES type;
189 	ATTR_FLAGS data_flags;
190 	ntfschar *name;
191 	u32 name_len;
192 	unsigned long state;
193 	s64 allocated_size;
194 	s64 data_size;
195 	s64 initialized_size;
196 	s64 compressed_size;
197 	u32 compression_block_size;
198 	u8 compression_block_size_bits;
199 	u8 compression_block_clusters;
200 	s8 unused_runs; /* pre-reserved entries available */
201 };
202 
203 /**
204  * enum ntfs_attr_state_bits - bits for the state field in the ntfs_attr
205  * structure
206  */
207 typedef enum {
208 	NA_Initialized,		/* 1: structure is initialized. */
209 	NA_NonResident,		/* 1: Attribute is not resident. */
210 	NA_BeingNonResident,	/* 1: Attribute is being made not resident. */
211 	NA_FullyMapped,		/* 1: Attribute has been fully mapped */
212 	NA_DataAppending,	/* 1: Attribute is being appended to */
213 	NA_ComprClosing,	/* 1: Compressed attribute is being closed */
214 } ntfs_attr_state_bits;
215 
216 #define  test_nattr_flag(na, flag)	 test_bit(NA_##flag, (na)->state)
217 #define   set_nattr_flag(na, flag)	  set_bit(NA_##flag, (na)->state)
218 #define clear_nattr_flag(na, flag)	clear_bit(NA_##flag, (na)->state)
219 
220 #define NAttrInitialized(na)		 test_nattr_flag(na, Initialized)
221 #define NAttrSetInitialized(na)		  set_nattr_flag(na, Initialized)
222 #define NAttrClearInitialized(na)	clear_nattr_flag(na, Initialized)
223 
224 #define NAttrNonResident(na)		 test_nattr_flag(na, NonResident)
225 #define NAttrSetNonResident(na)		  set_nattr_flag(na, NonResident)
226 #define NAttrClearNonResident(na)	clear_nattr_flag(na, NonResident)
227 
228 #define NAttrBeingNonResident(na)	test_nattr_flag(na, BeingNonResident)
229 #define NAttrSetBeingNonResident(na)	set_nattr_flag(na, BeingNonResident)
230 #define NAttrClearBeingNonResident(na)	clear_nattr_flag(na, BeingNonResident)
231 
232 #define NAttrFullyMapped(na)		test_nattr_flag(na, FullyMapped)
233 #define NAttrSetFullyMapped(na)		set_nattr_flag(na, FullyMapped)
234 #define NAttrClearFullyMapped(na)	clear_nattr_flag(na, FullyMapped)
235 
236 #define NAttrDataAppending(na)		test_nattr_flag(na, DataAppending)
237 #define NAttrSetDataAppending(na)	set_nattr_flag(na, DataAppending)
238 #define NAttrClearDataAppending(na)	clear_nattr_flag(na, DataAppending)
239 
240 #define NAttrComprClosing(na)		test_nattr_flag(na, ComprClosing)
241 #define NAttrSetComprClosing(na)	set_nattr_flag(na, ComprClosing)
242 #define NAttrClearComprClosing(na)	clear_nattr_flag(na, ComprClosing)
243 
244 #define GenNAttrIno(func_name, flag)			\
245 extern int NAttr##func_name(ntfs_attr *na);		\
246 extern void NAttrSet##func_name(ntfs_attr *na);		\
247 extern void NAttrClear##func_name(ntfs_attr *na);
248 
249 GenNAttrIno(Compressed, FILE_ATTR_COMPRESSED)
250 GenNAttrIno(Encrypted, 	FILE_ATTR_ENCRYPTED)
251 GenNAttrIno(Sparse, 	FILE_ATTR_SPARSE_FILE)
252 #undef GenNAttrIno
253 
254 /**
255  * union attr_val - Union of all known attribute values
256  *
257  * For convenience. Used in the attr structure.
258  */
259 typedef union {
260 	u8 _default;	/* Unnamed u8 to serve as default when just using
261 			   a_val without specifying any of the below. */
262 	STANDARD_INFORMATION std_inf;
263 	ATTR_LIST_ENTRY al_entry;
264 	FILE_NAME_ATTR filename;
265 	OBJECT_ID_ATTR obj_id;
266 	SECURITY_DESCRIPTOR_ATTR sec_desc;
267 	VOLUME_NAME vol_name;
268 	VOLUME_INFORMATION vol_inf;
269 	DATA_ATTR data;
270 	INDEX_ROOT index_root;
271 	INDEX_BLOCK index_blk;
272 	BITMAP_ATTR bmp;
273 	REPARSE_POINT reparse;
274 	EA_INFORMATION ea_inf;
275 	EA_ATTR ea;
276 	PROPERTY_SET property_set;
277 	LOGGED_UTILITY_STREAM logged_util_stream;
278 	EFS_ATTR_HEADER efs;
279 } attr_val;
280 
281 extern void ntfs_attr_init(ntfs_attr *na, const BOOL non_resident,
282 		const ATTR_FLAGS data_flags, const BOOL encrypted,
283 		const BOOL sparse,
284 		const s64 allocated_size, const s64 data_size,
285 		const s64 initialized_size, const s64 compressed_size,
286 		const u8 compression_unit);
287 
288 	/* warning : in the following "name" has to be freeable */
289 	/* or one of constants AT_UNNAMED, NTFS_INDEX_I30 or STREAM_SDS */
290 extern ntfs_attr *ntfs_attr_open(ntfs_inode *ni, const ATTR_TYPES type,
291 		ntfschar *name, u32 name_len);
292 extern void ntfs_attr_close(ntfs_attr *na);
293 
294 extern s64 ntfs_attr_pread(ntfs_attr *na, const s64 pos, s64 count,
295 		void *b);
296 extern s64 ntfs_attr_pwrite(ntfs_attr *na, const s64 pos, s64 count,
297 		const void *b);
298 extern int ntfs_attr_pclose(ntfs_attr *na);
299 
300 extern void *ntfs_attr_readall(ntfs_inode *ni, const ATTR_TYPES type,
301 			       ntfschar *name, u32 name_len, s64 *data_size);
302 
303 extern s64 ntfs_attr_mst_pread(ntfs_attr *na, const s64 pos,
304 		const s64 bk_cnt, const u32 bk_size, void *dst);
305 extern s64 ntfs_attr_mst_pwrite(ntfs_attr *na, const s64 pos,
306 		s64 bk_cnt, const u32 bk_size, void *src);
307 
308 extern int ntfs_attr_map_runlist(ntfs_attr *na, VCN vcn);
309 extern int ntfs_attr_map_whole_runlist(ntfs_attr *na);
310 
311 extern LCN ntfs_attr_vcn_to_lcn(ntfs_attr *na, const VCN vcn);
312 extern runlist_element *ntfs_attr_find_vcn(ntfs_attr *na, const VCN vcn);
313 
314 extern int ntfs_attr_size_bounds_check(const ntfs_volume *vol,
315 		const ATTR_TYPES type, const s64 size);
316 extern int ntfs_attr_can_be_resident(const ntfs_volume *vol,
317 		const ATTR_TYPES type);
318 int ntfs_attr_make_non_resident(ntfs_attr *na,
319 		ntfs_attr_search_ctx *ctx);
320 int ntfs_attr_force_non_resident(ntfs_attr *na);
321 extern int ntfs_make_room_for_attr(MFT_RECORD *m, u8 *pos, u32 size);
322 
323 extern int ntfs_resident_attr_record_add(ntfs_inode *ni, ATTR_TYPES type,
324 		const ntfschar *name, u8 name_len, const u8 *val, u32 size,
325 		ATTR_FLAGS flags);
326 extern int ntfs_non_resident_attr_record_add(ntfs_inode *ni, ATTR_TYPES type,
327 		const ntfschar *name, u8 name_len, VCN lowest_vcn,
328 		int dataruns_size, ATTR_FLAGS flags);
329 extern int ntfs_attr_record_rm(ntfs_attr_search_ctx *ctx);
330 
331 extern int ntfs_attr_add(ntfs_inode *ni, ATTR_TYPES type,
332 		ntfschar *name, u8 name_len, const u8 *val, s64 size);
333 extern int ntfs_attr_set_flags(ntfs_inode *ni, ATTR_TYPES type,
334 		const ntfschar *name, u8 name_len, ATTR_FLAGS flags,
335 		ATTR_FLAGS mask);
336 extern int ntfs_attr_rm(ntfs_attr *na);
337 
338 extern int ntfs_attr_record_resize(MFT_RECORD *m, ATTR_RECORD *a, u32 new_size);
339 
340 extern int ntfs_resident_attr_value_resize(MFT_RECORD *m, ATTR_RECORD *a,
341 		const u32 new_size);
342 
343 extern int ntfs_attr_record_move_to(ntfs_attr_search_ctx *ctx, ntfs_inode *ni);
344 extern int ntfs_attr_record_move_away(ntfs_attr_search_ctx *ctx, int extra);
345 
346 extern int ntfs_attr_update_mapping_pairs(ntfs_attr *na, VCN from_vcn);
347 
348 extern int ntfs_attr_truncate(ntfs_attr *na, const s64 newsize);
349 extern int ntfs_attr_truncate_solid(ntfs_attr *na, const s64 newsize);
350 
351 /**
352  * get_attribute_value_length - return the length of the value of an attribute
353  * @a:	pointer to a buffer containing the attribute record
354  *
355  * Return the byte size of the attribute value of the attribute @a (as it
356  * would be after eventual decompression and filling in of holes if sparse).
357  * If we return 0, check errno. If errno is 0 the actual length was 0,
358  * otherwise errno describes the error.
359  *
360  * FIXME: Describe possible errnos.
361  */
362 extern s64 ntfs_get_attribute_value_length(const ATTR_RECORD *a);
363 
364 /**
365  * get_attribute_value - return the attribute value of an attribute
366  * @vol:	volume on which the attribute is present
367  * @a:		attribute to get the value of
368  * @b:		destination buffer for the attribute value
369  *
370  * Make a copy of the attribute value of the attribute @a into the destination
371  * buffer @b. Note, that the size of @b has to be at least equal to the value
372  * returned by get_attribute_value_length(@a).
373  *
374  * Return number of bytes copied. If this is zero check errno. If errno is 0
375  * then nothing was read due to a zero-length attribute value, otherwise
376  * errno describes the error.
377  */
378 extern s64 ntfs_get_attribute_value(const ntfs_volume *vol,
379 				    const ATTR_RECORD *a, u8 *b);
380 
381 extern void  ntfs_attr_name_free(char **name);
382 extern char *ntfs_attr_name_get(const ntfschar *uname, const int uname_len);
383 extern int   ntfs_attr_exist(ntfs_inode *ni, const ATTR_TYPES type,
384 		const ntfschar *name, u32 name_len);
385 extern int   ntfs_attr_remove(ntfs_inode *ni, const ATTR_TYPES type,
386 			      ntfschar *name, u32 name_len);
387 extern s64   ntfs_attr_get_free_bits(ntfs_attr *na);
388 extern int ntfs_attr_data_read(ntfs_inode *ni,
389 		ntfschar *stream_name, int stream_name_len,
390 		char *buf, size_t size, off_t offset);
391 extern int ntfs_attr_data_write(ntfs_inode *ni,
392 		ntfschar *stream_name, int stream_name_len,
393 		const char *buf, size_t size, off_t offset);
394 
395 #endif /* defined _NTFS_ATTRIB_H */
396 
397