| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <linux/buffer_head.h> |
| 7 |
#include <linux/fs.h> |
| 8 |
#include "apfs.h" |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
static u64 apfs_fletcher64(void *addr, size_t len) |
| 17 |
{ |
| 18 |
__le32 *buff = addr; |
| 19 |
u64 sum1 = 0; |
| 20 |
u64 sum2 = 0; |
| 21 |
u64 c1, c2; |
| 22 |
int i, count_32; |
| 23 |
|
| 24 |
count_32 = len >> 2; |
| 25 |
for (i = 0; i < count_32; i++) { |
| 26 |
sum1 += le32_to_cpu(buff[i]); |
| 27 |
sum2 += sum1; |
| 28 |
} |
| 29 |
|
| 30 |
c1 = sum1 + sum2; |
| 31 |
c1 = 0xFFFFFFFF - do_div(c1, 0xFFFFFFFF); |
| 32 |
c2 = sum1 + c1; |
| 33 |
c2 = 0xFFFFFFFF - do_div(c2, 0xFFFFFFFF); |
| 34 |
|
| 35 |
return (c2 << 32) | c1; |
| 36 |
} |
| 37 |
|
| 38 |
int apfs_obj_verify_csum(struct super_block *sb, struct buffer_head *bh) |
| 39 |
{ |
| 40 |
|
| 41 |
if (buffer_trans(bh)) |
| 42 |
return 1; |
| 43 |
return apfs_multiblock_verify_csum(bh->b_data, sb->s_blocksize); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
int apfs_multiblock_verify_csum(char *object, u32 size) |
| 54 |
{ |
| 55 |
struct apfs_obj_phys *obj = (struct apfs_obj_phys *)object; |
| 56 |
u64 actual_csum, header_csum; |
| 57 |
|
| 58 |
header_csum = le64_to_cpu(obj->o_cksum); |
| 59 |
actual_csum = apfs_fletcher64(object + APFS_MAX_CKSUM_SIZE, size - APFS_MAX_CKSUM_SIZE); |
| 60 |
return header_csum == actual_csum; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
void apfs_obj_set_csum(struct super_block *sb, struct apfs_obj_phys *obj) |
| 71 |
{ |
| 72 |
apfs_multiblock_set_csum((char *)obj, sb->s_blocksize); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
void apfs_multiblock_set_csum(char *object, u32 size) |
| 81 |
{ |
| 82 |
struct apfs_obj_phys *obj = (struct apfs_obj_phys *)object; |
| 83 |
u64 cksum; |
| 84 |
|
| 85 |
cksum = apfs_fletcher64(object + APFS_MAX_CKSUM_SIZE, size - APFS_MAX_CKSUM_SIZE); |
| 86 |
obj->o_cksum = cpu_to_le64(cksum); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
int apfs_create_cpm_block(struct super_block *sb, u64 bno, struct buffer_head **bh_p) |
| 98 |
{ |
| 99 |
struct apfs_nxsb_info *nxi = APFS_NXI(sb); |
| 100 |
struct apfs_checkpoint_map_phys *cpm = NULL; |
| 101 |
struct buffer_head *bh = NULL; |
| 102 |
int err; |
| 103 |
|
| 104 |
bh = apfs_getblk(sb, bno); |
| 105 |
if (!bh) { |
| 106 |
apfs_err(sb, "failed to map cpm block"); |
| 107 |
return -EIO; |
| 108 |
} |
| 109 |
err = apfs_transaction_join(sb, bh); |
| 110 |
if (err) { |
| 111 |
brelse(bh); |
| 112 |
return err; |
| 113 |
} |
| 114 |
set_buffer_csum(bh); |
| 115 |
|
| 116 |
cpm = (void *)bh->b_data; |
| 117 |
memset(cpm, 0, sb->s_blocksize); |
| 118 |
cpm->cpm_o.o_oid = cpu_to_le64(bno); |
| 119 |
cpm->cpm_o.o_xid = cpu_to_le64(nxi->nx_xid); |
| 120 |
cpm->cpm_o.o_type = cpu_to_le32(APFS_OBJ_PHYSICAL | APFS_OBJECT_TYPE_CHECKPOINT_MAP); |
| 121 |
cpm->cpm_o.o_subtype = cpu_to_le32(APFS_OBJECT_TYPE_INVALID); |
| 122 |
|
| 123 |
|
| 124 |
cpm->cpm_flags = cpu_to_le32(APFS_CHECKPOINT_MAP_LAST); |
| 125 |
cpm->cpm_count = 0; |
| 126 |
|
| 127 |
*bh_p = bh; |
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
int apfs_create_cpoint_map(struct super_block *sb, struct apfs_checkpoint_map_phys *cpm, struct apfs_obj_phys *obj, u64 bno, u32 size) |
| 143 |
{ |
| 144 |
struct apfs_checkpoint_mapping *map = NULL; |
| 145 |
u32 cpm_count; |
| 146 |
|
| 147 |
apfs_assert_in_transaction(sb, &cpm->cpm_o); |
| 148 |
|
| 149 |
cpm_count = le32_to_cpu(cpm->cpm_count); |
| 150 |
if (cpm_count >= apfs_max_maps_per_block(sb)) |
| 151 |
return -ENOSPC; |
| 152 |
map = &cpm->cpm_map[cpm_count]; |
| 153 |
le32_add_cpu(&cpm->cpm_count, 1); |
| 154 |
|
| 155 |
map->cpm_type = obj->o_type; |
| 156 |
map->cpm_subtype = obj->o_subtype; |
| 157 |
map->cpm_size = cpu_to_le32(size); |
| 158 |
map->cpm_pad = 0; |
| 159 |
map->cpm_fs_oid = 0; |
| 160 |
map->cpm_oid = obj->o_oid; |
| 161 |
map->cpm_paddr = cpu_to_le64(bno); |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
u32 apfs_index_in_data_area(struct super_block *sb, u64 bno) |
| 171 |
{ |
| 172 |
struct apfs_nx_superblock *raw_sb = APFS_NXI(sb)->nx_raw; |
| 173 |
u64 data_base = le64_to_cpu(raw_sb->nx_xp_data_base); |
| 174 |
u32 data_index = le32_to_cpu(raw_sb->nx_xp_data_index); |
| 175 |
u32 data_blks = le32_to_cpu(raw_sb->nx_xp_data_blocks); |
| 176 |
u64 tmp; |
| 177 |
|
| 178 |
tmp = bno - data_base + data_blks - data_index; |
| 179 |
return do_div(tmp, data_blks); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
u64 apfs_data_index_to_bno(struct super_block *sb, u32 index) |
| 188 |
{ |
| 189 |
struct apfs_nx_superblock *raw_sb = APFS_NXI(sb)->nx_raw; |
| 190 |
u64 data_base = le64_to_cpu(raw_sb->nx_xp_data_base); |
| 191 |
u32 data_index = le32_to_cpu(raw_sb->nx_xp_data_index); |
| 192 |
u32 data_blks = le32_to_cpu(raw_sb->nx_xp_data_blocks); |
| 193 |
|
| 194 |
return data_base + (index + data_index) % data_blks; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
struct apfs_ephemeral_object_info *apfs_ephemeral_object_lookup(struct super_block *sb, u64 oid) |
| 206 |
{ |
| 207 |
struct apfs_nxsb_info *nxi = APFS_NXI(sb); |
| 208 |
struct apfs_ephemeral_object_info *list = NULL; |
| 209 |
int i; |
| 210 |
|
| 211 |
list = nxi->nx_eph_list; |
| 212 |
for (i = 0; i < nxi->nx_eph_count; ++i) { |
| 213 |
if (list[i].oid == oid) |
| 214 |
return &list[i]; |
| 215 |
} |
| 216 |
apfs_err(sb, "no mapping for oid 0x%llx", oid); |
| 217 |
return ERR_PTR(-EFSCORRUPTED); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
struct buffer_head *apfs_read_object_block(struct super_block *sb, u64 bno, bool write, bool preserve) |
| 232 |
{ |
| 233 |
struct apfs_nxsb_info *nxi = APFS_NXI(sb); |
| 234 |
struct apfs_superblock *vsb_raw = NULL; |
| 235 |
struct buffer_head *bh, *new_bh; |
| 236 |
struct apfs_obj_phys *obj; |
| 237 |
u32 type; |
| 238 |
u64 new_bno; |
| 239 |
int err; |
| 240 |
|
| 241 |
ASSERT(write || !preserve); |
| 242 |
|
| 243 |
bh = apfs_sb_bread(sb, bno); |
| 244 |
if (!bh) { |
| 245 |
apfs_err(sb, "failed to read object block 0x%llx", bno); |
| 246 |
return ERR_PTR(-EIO); |
| 247 |
} |
| 248 |
|
| 249 |
obj = (struct apfs_obj_phys *)bh->b_data; |
| 250 |
type = le32_to_cpu(obj->o_type); |
| 251 |
ASSERT(!(type & APFS_OBJ_EPHEMERAL)); |
| 252 |
if (nxi->nx_flags & APFS_CHECK_NODES && !apfs_obj_verify_csum(sb, bh)) { |
| 253 |
apfs_err(sb, "bad checksum for object in block 0x%llx", bno); |
| 254 |
err = -EFSBADCRC; |
| 255 |
goto fail; |
| 256 |
} |
| 257 |
|
| 258 |
if (!write) |
| 259 |
return bh; |
| 260 |
ASSERT(!(sb->s_flags & SB_RDONLY)); |
| 261 |
|
| 262 |
|
| 263 |
if (obj->o_xid == cpu_to_le64(nxi->nx_xid)) |
| 264 |
return bh; |
| 265 |
|
| 266 |
err = apfs_spaceman_allocate_block(sb, &new_bno, true ); |
| 267 |
if (err) { |
| 268 |
apfs_err(sb, "block allocation failed"); |
| 269 |
goto fail; |
| 270 |
} |
| 271 |
new_bh = apfs_getblk(sb, new_bno); |
| 272 |
if (!new_bh) { |
| 273 |
apfs_err(sb, "failed to map block for CoW (0x%llx)", new_bno); |
| 274 |
err = -EIO; |
| 275 |
goto fail; |
| 276 |
} |
| 277 |
memcpy(new_bh->b_data, bh->b_data, sb->s_blocksize); |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
if (!preserve) { |
| 285 |
err = apfs_free_queue_insert(sb, bh->b_blocknr, 1); |
| 286 |
if (err) |
| 287 |
apfs_err(sb, "free queue insertion failed for 0x%llx", (unsigned long long)bh->b_blocknr); |
| 288 |
} else if ((type & APFS_OBJECT_TYPE_MASK) != APFS_OBJECT_TYPE_FS) { |
| 289 |
vsb_raw = APFS_SB(sb)->s_vsb_raw; |
| 290 |
apfs_assert_in_transaction(sb, &vsb_raw->apfs_o); |
| 291 |
le64_add_cpu(&vsb_raw->apfs_fs_alloc_count, 1); |
| 292 |
le64_add_cpu(&vsb_raw->apfs_total_blocks_alloced, 1); |
| 293 |
} |
| 294 |
|
| 295 |
brelse(bh); |
| 296 |
bh = new_bh; |
| 297 |
new_bh = NULL; |
| 298 |
if (err) |
| 299 |
goto fail; |
| 300 |
obj = (struct apfs_obj_phys *)bh->b_data; |
| 301 |
|
| 302 |
if (type & APFS_OBJ_PHYSICAL) |
| 303 |
obj->o_oid = cpu_to_le64(new_bno); |
| 304 |
obj->o_xid = cpu_to_le64(nxi->nx_xid); |
| 305 |
err = apfs_transaction_join(sb, bh); |
| 306 |
if (err) |
| 307 |
goto fail; |
| 308 |
|
| 309 |
set_buffer_csum(bh); |
| 310 |
return bh; |
| 311 |
|
| 312 |
fail: |
| 313 |
brelse(bh); |
| 314 |
return ERR_PTR(err); |
| 315 |
} |