2
libparted - a library for manipulating disk partitions
3
Copyright (C) 2004-2005, 2007, 2009-2010 Free Software Foundation,
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 3 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program. If not, see <http://www.gnu.org/licenses/>.
24
#include <parted/parted.h>
25
#include <parted/endian.h>
26
#include <parted/debug.h>
31
# define _(String) dgettext (PACKAGE, String)
33
# define _(String) (String)
34
#endif /* ENABLE_NLS */
37
#include "reloc_plus.h"
41
static int hfsj_vh_replayed = 0;
45
hfsj_calc_checksum(uint8_t *ptr, int len)
50
for (i=0; i < len; i++, ptr++) {
51
cksum = (cksum << 8) ^ (cksum + *ptr);
58
hfsj_update_jib(PedFileSystem* fs, uint32_t block)
60
HfsPPrivateFSData* priv_data = (HfsPPrivateFSData*)
63
priv_data->vh->journal_info_block = PED_CPU_TO_BE32(block);
65
if (!hfsplus_update_vh (fs))
68
priv_data->jib_start_block = block;
73
hfsj_update_jl(PedFileSystem* fs, uint32_t block)
75
uint8_t buf[PED_SECTOR_SIZE_DEFAULT];
78
HfsPPrivateFSData* priv_data = (HfsPPrivateFSData*)
80
HfsJJournalInfoBlock* jib;
83
binsect = HFS_32_TO_CPU(priv_data->vh->block_size, is_le) / PED_SECTOR_SIZE_DEFAULT;
84
sector = (PedSector) priv_data->jib_start_block * binsect;
85
if (!ped_geometry_read(priv_data->plus_geom, buf, sector, 1))
87
jib = (HfsJJournalInfoBlock*) buf;
89
offset = (uint64_t)block * PED_SECTOR_SIZE_DEFAULT * binsect;
90
jib->offset = HFS_CPU_TO_64(offset, is_le);
92
if (!ped_geometry_write(priv_data->plus_geom, buf, sector, 1)
93
|| !ped_geometry_sync(priv_data->plus_geom))
96
priv_data->jl_start_block = block;
100
/* Return the sector in the journal that is after the area read */
103
hfsj_journal_read(PedGeometry* geom, HfsJJournalHeader* jh,
104
PedSector journ_sect, PedSector journ_length,
105
PedSector read_sect, unsigned int nb_sect,
111
r = ped_geometry_read(geom, buf, journ_sect + read_sect, 1);
114
buf = ((uint8_t*)buf) + PED_SECTOR_SIZE_DEFAULT;
116
if (read_sect == journ_length)
117
read_sect = 1; /* skip journal header
118
which is asserted to be
126
hfsj_replay_transaction(PedFileSystem* fs, HfsJJournalHeader* jh,
127
PedSector jsector, PedSector jlength)
129
PedSector start, sector;
130
HfsPPrivateFSData* priv_data = (HfsPPrivateFSData*)
132
HfsJBlockListHeader* blhdr;
134
unsigned int blhdr_nbsect;
136
uint32_t cksum, size;
138
blhdr_nbsect = HFS_32_TO_CPU(jh->blhdr_size, is_le) / PED_SECTOR_SIZE_DEFAULT;
139
blhdr = (HfsJBlockListHeader*)
140
ped_malloc (blhdr_nbsect * PED_SECTOR_SIZE_DEFAULT);
141
if (!blhdr) return 0;
143
start = HFS_64_TO_CPU(jh->start, is_le) / PED_SECTOR_SIZE_DEFAULT;
145
start = hfsj_journal_read(priv_data->plus_geom, jh, jsector,
146
jlength, start, blhdr_nbsect, blhdr);
147
if (!start) goto err_replay;
149
cksum = HFS_32_TO_CPU(blhdr->checksum, is_le);
151
if (cksum!=hfsj_calc_checksum((uint8_t*)blhdr, sizeof(*blhdr))){
152
ped_exception_throw (
154
PED_EXCEPTION_CANCEL,
155
_("Bad block list header checksum."));
158
blhdr->checksum = HFS_CPU_TO_32(cksum, is_le);
160
for (i=1; i < HFS_16_TO_CPU(blhdr->num_blocks, is_le); ++i) {
161
size = HFS_32_TO_CPU(blhdr->binfo[i].bsize, is_le);
162
sector = HFS_64_TO_CPU(blhdr->binfo[i].bnum, is_le);
164
if (size % PED_SECTOR_SIZE_DEFAULT) {
167
PED_EXCEPTION_CANCEL,
168
_("Invalid size of a transaction "
169
"block while replaying the journal "
174
block = (uint8_t*) ped_malloc(size);
175
if (!block) goto err_replay;
176
start = hfsj_journal_read(priv_data->plus_geom, jh,
177
jsector, jlength, start,
178
size / PED_SECTOR_SIZE_DEFAULT,
184
/* the sector stored in the journal seems to be
185
relative to the begin of the block device which
186
contains the hfs+ journaled volume */
188
r = ped_geometry_write (fs->geom, block, sector,
189
size / PED_SECTOR_SIZE_DEFAULT);
193
/* check if wrapper mdb or vh with no wrapper has
195
if ( (sector != ~0LL)
197
&& (2 < sector + size / PED_SECTOR_SIZE_DEFAULT) )
198
hfsj_vh_replayed = 1;
199
/* check if vh of embedded hfs+ has changed */
200
if ( (sector != ~0LL)
201
&& (priv_data->plus_geom != fs->geom)
204
- priv_data->plus_geom->start <= 2)
206
+ size / PED_SECTOR_SIZE_DEFAULT
208
- priv_data->plus_geom->start > 2) )
209
hfsj_vh_replayed = 1;
210
if (!r) goto err_replay;
212
} while (blhdr->binfo[0].next);
214
jh->start = HFS_CPU_TO_64(start * PED_SECTOR_SIZE_DEFAULT, is_le);
217
return (ped_geometry_sync (fs->geom));
224
/* 0 => Failure, don't continue to open ! */
225
/* 1 => Success, the journal has been completly replayed, or don't need to */
227
hfsj_replay_journal(PedFileSystem* fs)
229
uint8_t buf[PED_SECTOR_SIZE_DEFAULT];
230
PedSector sector, length;
231
HfsPPrivateFSData* priv_data = (HfsPPrivateFSData*)
233
HfsJJournalInfoBlock* jib;
234
HfsJJournalHeader* jh;
238
binsect = PED_BE32_TO_CPU(priv_data->vh->block_size) / PED_SECTOR_SIZE_DEFAULT;
239
priv_data->jib_start_block =
240
PED_BE32_TO_CPU(priv_data->vh->journal_info_block);
241
sector = (PedSector) priv_data->jib_start_block * binsect;
242
if (!ped_geometry_read(priv_data->plus_geom, buf, sector, 1))
244
jib = (HfsJJournalInfoBlock*) buf;
246
if ( (jib->flags & PED_CPU_TO_BE32(1 << HFSJ_JOURN_IN_FS))
247
&& !(jib->flags & PED_CPU_TO_BE32(1 << HFSJ_JOURN_OTHER_DEV)) ) {
248
priv_data->jl_start_block = HFS_64_TO_CPU(jib->offset, is_le)
249
/ ( PED_SECTOR_SIZE_DEFAULT * binsect );
252
if (jib->flags & PED_CPU_TO_BE32(1 << HFSJ_JOURN_NEED_INIT))
255
if ( !(jib->flags & PED_CPU_TO_BE32(1 << HFSJ_JOURN_IN_FS))
256
|| (jib->flags & PED_CPU_TO_BE32(1 << HFSJ_JOURN_OTHER_DEV)) ) {
257
ped_exception_throw (
258
PED_EXCEPTION_NO_FEATURE,
259
PED_EXCEPTION_CANCEL,
260
_("Journal stored outside of the volume are "
261
"not supported. Try to desactivate the "
262
"journal and run Parted again."));
266
if ( (PED_BE64_TO_CPU(jib->offset) % PED_SECTOR_SIZE_DEFAULT)
267
|| (PED_BE64_TO_CPU(jib->size) % PED_SECTOR_SIZE_DEFAULT) ) {
268
ped_exception_throw (
269
PED_EXCEPTION_NO_FEATURE,
270
PED_EXCEPTION_CANCEL,
271
_("Journal offset or size is not multiple of "
272
"the sector size."));
276
sector = PED_BE64_TO_CPU(jib->offset) / PED_SECTOR_SIZE_DEFAULT;
277
length = PED_BE64_TO_CPU(jib->size) / PED_SECTOR_SIZE_DEFAULT;
280
if (!ped_geometry_read(priv_data->plus_geom, buf, sector, 1))
282
jh = (HfsJJournalHeader*) buf;
284
if (jh->endian == PED_LE32_TO_CPU(HFSJ_ENDIAN_MAGIC))
287
if ( (jh->magic != HFS_32_TO_CPU(HFSJ_HEADER_MAGIC, is_le))
288
|| (jh->endian != HFS_32_TO_CPU(HFSJ_ENDIAN_MAGIC, is_le)) ) {
289
ped_exception_throw (
291
PED_EXCEPTION_CANCEL,
292
_("Incorrect magic values in the journal header."));
296
if ( (HFS_64_TO_CPU(jh->size, is_le)%PED_SECTOR_SIZE_DEFAULT)
297
|| (HFS_64_TO_CPU(jh->size, is_le)/PED_SECTOR_SIZE_DEFAULT
298
!= (uint64_t)length) ) {
299
ped_exception_throw (
301
PED_EXCEPTION_CANCEL,
302
_("Journal size mismatch between journal info block "
303
"and journal header."));
307
if ( (HFS_64_TO_CPU(jh->start, is_le) % PED_SECTOR_SIZE_DEFAULT)
308
|| (HFS_64_TO_CPU(jh->end, is_le) % PED_SECTOR_SIZE_DEFAULT)
309
|| (HFS_32_TO_CPU(jh->blhdr_size, is_le) % PED_SECTOR_SIZE_DEFAULT)
310
|| (HFS_32_TO_CPU(jh->jhdr_size, is_le) % PED_SECTOR_SIZE_DEFAULT) ) {
311
ped_exception_throw (
313
PED_EXCEPTION_CANCEL,
314
_("Some header fields are not multiple of the sector "
319
if (HFS_32_TO_CPU(jh->jhdr_size, is_le) != PED_SECTOR_SIZE_DEFAULT) {
320
ped_exception_throw (
322
PED_EXCEPTION_CANCEL,
323
_("The sector size stored in the journal is not 512 "
324
"bytes. Parted only supports 512 bytes length "
329
cksum = HFS_32_TO_CPU(jh->checksum, is_le);
331
if (cksum != hfsj_calc_checksum((uint8_t*)jh, sizeof(*jh))) {
332
ped_exception_throw (
334
PED_EXCEPTION_CANCEL,
335
_("Bad journal checksum."));
338
jh->checksum = HFS_CPU_TO_32(cksum, is_le);
340
/* The 2 following test are in the XNU Darwin source code */
341
/* so I assume they're needed */
342
if (jh->start == jh->size)
343
jh->start = HFS_CPU_TO_64(PED_SECTOR_SIZE_DEFAULT, is_le);
344
if (jh->end == jh->size)
345
jh->start = HFS_CPU_TO_64(PED_SECTOR_SIZE_DEFAULT, is_le);
347
if (jh->start == jh->end)
350
if (ped_exception_throw (
351
PED_EXCEPTION_WARNING,
352
PED_EXCEPTION_FIX | PED_EXCEPTION_CANCEL,
353
_("The journal is not empty. Parted must replay the "
354
"transactions before opening the file system. This will "
355
"modify the file system."))
356
!= PED_EXCEPTION_FIX)
359
while (jh->start != jh->end) {
360
/* Replay one complete transaction */
361
if (!hfsj_replay_transaction(fs, jh, sector, length))
364
/* Recalculate cksum of the journal header */
365
jh->checksum = 0; /* need to be 0 while calculating the cksum */
366
cksum = hfsj_calc_checksum((uint8_t*)jh, sizeof(*jh));
367
jh->checksum = HFS_CPU_TO_32(cksum, is_le);
369
/* Update the Journal Header */
370
if (!ped_geometry_write(priv_data->plus_geom, buf, sector, 1)
371
|| !ped_geometry_sync(priv_data->plus_geom))
375
if (hfsj_vh_replayed) {
376
/* probe could have reported incorrect info ! */
377
/* is there a way to ask parted to quit ? */
379
PED_EXCEPTION_WARNING,
381
_("The volume header or the master directory block has "
382
"changed while replaying the journal. You should "
390
#endif /* DISCOVER_ONLY */