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