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