1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
/*
* Copyright (C) 2019 Ernesto A. Fernández <ernesto.mnd.fernandez@gmail.com>
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <apfs/checksum.h>
#include <apfs/raw.h>
#include "apfsck.h"
#include "btree.h"
#include "htable.h"
#include "object.h"
#include "super.h"
int obj_verify_csum(struct apfs_obj_phys *obj)
{
return (le64_to_cpu(obj->o_cksum) ==
fletcher64((char *) obj + APFS_MAX_CKSUM_SIZE,
sb->s_blocksize - APFS_MAX_CKSUM_SIZE));
}
/**
* read_object_nocheck_internal - Read an object header from disk
* @bno: block number for the object
* @obj: object struct to receive the results
* @noheader: does this object have no header?
*
* Returns a pointer to the raw data of the object in memory, without running
* any checks other than the Fletcher verification.
*/
static void *read_object_nocheck_internal(u64 bno, struct object *obj, bool noheader)
{
struct apfs_obj_phys *raw;
raw = mmap(NULL, sb->s_blocksize, PROT_READ, MAP_PRIVATE,
fd, bno * sb->s_blocksize);
if (raw == MAP_FAILED)
system_error();
if (noheader) {
struct apfs_obj_phys zeroes = {0};
if (memcmp(raw, &zeroes, sizeof(*raw)) != 0)
report("No-header object", "has a header.");
return raw;
}
/* This one check is always needed */
if (!obj_verify_csum(raw)) {
report("Object header", "bad checksum in block 0x%llx.",
(unsigned long long)bno);
}
obj->oid = le64_to_cpu(raw->o_oid);
obj->xid = le64_to_cpu(raw->o_xid);
obj->block_nr = bno;
obj->type = le32_to_cpu(raw->o_type) & APFS_OBJECT_TYPE_MASK;
obj->flags = le32_to_cpu(raw->o_type) & APFS_OBJECT_TYPE_FLAGS_MASK;
obj->subtype = le32_to_cpu(raw->o_subtype);
return raw;
}
void *read_object_nocheck(u64 bno, struct object *obj)
{
return read_object_nocheck_internal(bno, obj, false /* noheader */);
}
/**
* parse_object_flags - Check consistency of object flags
* @flags: the flags
* @encrypted: is the object encrypted?
*
* Returns the storage type flags to be checked by the caller.
*/
u32 parse_object_flags(u32 flags, bool encrypted)
{
if ((flags & APFS_OBJECT_TYPE_FLAGS_DEFINED_MASK) != flags)
report("Object header", "undefined flag in use.");
if (flags & APFS_OBJ_NONPERSISTENT)
report("Object header", "nonpersistent flag is set.");
if (flags & APFS_OBJ_NOHEADER)
report("Object header", "noheader flag is set.");
/*
* So-called encrypted objects don't actually appear to be encrypted at
* all, no idea what this is about.
*/
if ((bool)(flags & APFS_OBJ_ENCRYPTED) != encrypted)
report("Object header", "wrong encryption flag.");
return flags & APFS_OBJ_STORAGETYPE_MASK;
}
/**
* read_object_internal - Read an object header from disk and run some checks
* @oid: object id
* @omap_table: hash table for the object map (NULL if no translation is needed)
* @obj: object struct to receive the results
* @noheader: does this object lack a header?
*
* Returns a pointer to the raw data of the object in memory, after checking
* the consistency of some of its fields.
*/
static void *read_object_internal(u64 oid, struct htable_entry **omap_table, struct object *obj, bool noheader)
{
struct apfs_obj_phys *raw;
struct omap_record *omap_rec = NULL;
u64 bno;
u64 xid;
u32 storage_type;
assert(omap_table || !noheader);
if (omap_table) {
omap_rec = get_latest_omap_record(oid, sb->s_xid, omap_table);
if (!omap_rec || !omap_rec->bno)
report("Object map", "record missing for id 0x%llx.", (unsigned long long)oid);
if ((bool)(omap_rec->flags & APFS_OMAP_VAL_NOHEADER) != noheader)
report("Object map", "wrong setting for noheader flag.");
if (vsb && vsb->v_in_snapshot) {
if (omap_rec->seen_for_snap)
report("Object map record", "oid used twice for same snapshot.");
omap_rec->seen_for_snap = true;
} else {
if (omap_rec->seen_for_latest)
report("Object map record", "oid used twice in latest checkpoint.");
omap_rec->seen_for_latest = true;
}
bno = omap_rec->bno;
} else {
bno = oid;
}
if (noheader) {
raw = read_object_nocheck_internal(bno, obj, noheader);
obj->oid = oid;
obj->block_nr = bno;
obj->xid = omap_rec->xid;
} else {
raw = read_object_nocheck(bno, obj);
}
if (!ongoing_query) { /* Query code will revisit already parsed nodes */
if ((obj->type == APFS_OBJECT_TYPE_SPACEMAN_CIB) ||
(obj->type == APFS_OBJECT_TYPE_SPACEMAN_CAB)) {
ip_bmap_mark_as_used(bno, 1 /* length */);
} else if (omap_table) {
/* Virtual objects may be shared between snapshots */
if (!omap_rec->seen) {
container_bmap_mark_as_used(bno, 1 /* length */);
/* The volume super itself doesn't count here */
if (vsb && obj->type != APFS_OBJECT_TYPE_FS)
++vsb->v_block_count;
}
omap_rec->seen = true;
} else {
container_bmap_mark_as_used(bno, 1 /* length */);
/* Volume superblocks in snapshots don't count either */
if (vsb && obj->type != APFS_OBJECT_TYPE_FS)
++vsb->v_block_count;
}
}
if (oid != obj->oid)
report("Object header", "wrong object id in block 0x%llx.",
(unsigned long long)bno);
if (oid < APFS_OID_RESERVED_COUNT)
report("Object header", "reserved object id in block 0x%llx.",
(unsigned long long)bno);
if (omap_table && oid >= sb->s_next_oid)
report("Object header", "unassigned object id in block 0x%llx.",
(unsigned long long)bno);
xid = obj->xid;
if (!xid)
report("Object header", "null transaction id in block 0x%llx.", (unsigned long long)bno);
if (sb->s_xid < xid) {
/*
* When a snapshot is deleted, the following one is given its
* physical extents; so its extent reference tree gets altered
* under the current transaction.
*/
if (!vsb->v_in_snapshot || obj->subtype != APFS_OBJECT_TYPE_BLOCKREFTREE)
report("Object header", "bad transaction id in block 0x%llx.", (unsigned long long)bno);
}
if (vsb && vsb->v_first_xid > xid)
report_weird("Transaction id in block is older than volume");
if (omap_table && xid != omap_rec->xid)
report("Object header",
"transaction id in omap key doesn't match block 0x%llx.",
(unsigned long long)bno);
storage_type = parse_object_flags(obj->flags, vsb && vsb->v_encrypted && obj->subtype == APFS_OBJECT_TYPE_FSTREE);
/* Ephemeral objects are handled by read_ephemeral_object() */
if (omap_table && storage_type != APFS_OBJ_VIRTUAL)
report("Object header", "wrong flag for virtual object.");
if (!omap_table && storage_type != APFS_OBJ_PHYSICAL)
report("Object header", "wrong flag for physical object.");
return raw;
}
void *read_object(u64 oid, struct htable_entry **omap_table, struct object *obj)
{
return read_object_internal(oid, omap_table, obj, false /* noheader */);
}
void *read_object_noheader(u64 oid, struct htable_entry **omap_table, struct object *obj)
{
return read_object_internal(oid, omap_table, obj, true /* noheader */);
}
/**
* free_cpoint_map - Free a map structure after performing some final checks
* @entry: the entry to free
*/
static void free_cpoint_map(struct htable_entry *entry)
{
struct cpoint_map *map = (struct cpoint_map *)entry;
u32 blk_count = map->m_size >> sb->s_blocksize_bits;
u64 obj_start = map->m_paddr;
u64 obj_end = map->m_paddr + blk_count; /* Objects can't wrap, right? */
u64 data_start = sb->s_data_base;
u64 data_end = sb->s_data_base + sb->s_data_blocks;
u64 valid_start;
if (!map->m_seen)
report("Checkpoint map", "no object for mapping.");
if (obj_start < data_start || obj_end > data_end)
report("Checkpoint map", "block number is out of range.");
/* Not all blocks in the data area belong to the current checkpoint */
valid_start = sb->s_data_base + sb->s_data_index;
if (obj_start >= valid_start && obj_end > valid_start + sb->s_data_len)
report("Checkpoint map", "block number outside valid range.");
if (obj_start < valid_start &&
obj_end + sb->s_data_blocks > valid_start + sb->s_data_len)
report("Checkpoint map", "block number outside valid range.");
if (map->m_oid < APFS_OID_RESERVED_COUNT)
report("Checkpoint map", "reserved object id.");
if (map->m_oid >= sb->s_next_oid)
report("Checkpoint map", "unassigned object id.");
free(entry);
}
/**
* free_cpoint_map_table - Free the checkpoint map table and all its entries
* @table: table to free
*/
void free_cpoint_map_table(struct htable_entry **table)
{
free_htable(table, free_cpoint_map);
}
/**
* get_cpoint_map - Find or create a map structure in the checkpoint map table
* @oid: ephemeral object id
*
* Returns the checkpoint mapping structure, after creating it if necessary.
*/
struct cpoint_map *get_cpoint_map(u64 oid)
{
struct htable_entry *entry;
entry = get_htable_entry(oid, sizeof(struct cpoint_map),
sb->s_cpoint_map_table);
return (struct cpoint_map *)entry;
}
/**
* read_ephemeral_object - Read an ephemeral object header from disk
* @oid: object id
* @obj: object struct to receive the results
*
* Returns a pointer to the raw data of the object in memory, after checking
* the consistency of some of its fields.
*/
void *read_ephemeral_object(u64 oid, struct object *obj)
{
struct apfs_obj_phys *raw;
struct cpoint_map *map;
u32 storage_type;
assert(sb->s_cpoint_map_table);
assert(sb->s_xid);
map = get_cpoint_map(oid);
if (!map->m_paddr)
report("Ephemeral object", "missing checkpoint mapping.");
if (map->m_seen)
report("Checkpoint map", "an ephemeral object id was reused.");
map->m_seen = true;
/* Multiblock ephemeral objects may exist, but are not supported yet */
raw = read_object_nocheck(map->m_paddr, obj);
if ((obj->type | obj->flags) != map->m_type)
report("Ephemeral object", "type field doesn't match mapping.");
if (obj->subtype != map->m_subtype)
report("Ephemeral object", "subtype doesn't match mapping.");
if (obj->oid != oid)
report("Ephemeral object", "wrong object id.");
if (obj->xid != sb->s_xid)
report("Ephemeral object", "not part of latest transaction.");
storage_type = parse_object_flags(obj->flags, false);
if (storage_type != APFS_OBJ_EPHEMERAL)
report("Object header", "wrong flag for ephemeral object.");
return raw;
}
|