File: commit.c

package info (click to toggle)
evms 2.5.2-1.sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,248 kB
  • ctags: 15,488
  • sloc: ansic: 201,340; perl: 12,421; sh: 4,262; makefile: 1,516; yacc: 316; sed: 16
file content (411 lines) | stat: -rw-r--r-- 14,616 bytes parent folder | download | duplicates (2)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 *
 *   (C) Copyright IBM Corp. 2001, 2003
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *   Module: libgpt.so
 *
 *   File: commit.c
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <plugin.h>

#include "gpt.h"
#include "checks.h"
#include "helpers.h"
#include "commit.h"


/*
 *  Called to convert a GPT header from disk format to the arch specific
 *  format.
 */
static void cpu_gpt_header_to_disk( gpt_header *gh )
{

        gh->signature              = CPU_TO_DISK64(gh->signature);
        gh->version                = CPU_TO_DISK32(gh->version);
        gh->size                   = CPU_TO_DISK32(gh->size);
        gh->crc                    = CPU_TO_DISK32(gh->crc);
        gh->reserve                = CPU_TO_DISK32(gh->reserve);
        gh->my_lba                 = CPU_TO_DISK64(gh->my_lba);
        gh->alternate_lba          = CPU_TO_DISK64(gh->alternate_lba);
        gh->start_useable          = CPU_TO_DISK64(gh->start_useable);
        gh->end_useable            = CPU_TO_DISK64(gh->end_useable);

        gh->disk_id.time_low       = CPU_TO_DISK32(gh->disk_id.time_low);
        gh->disk_id.time_mid       = CPU_TO_DISK16(gh->disk_id.time_mid);
        gh->disk_id.time_high      = CPU_TO_DISK16(gh->disk_id.time_high);

        gh->ptable_lba             = CPU_TO_DISK64(gh->ptable_lba);
        gh->ptable_count           = CPU_TO_DISK32(gh->ptable_count);
        gh->ptable_entry_size      = CPU_TO_DISK32(gh->ptable_entry_size);
        gh->ptable_crc             = CPU_TO_DISK32(gh->ptable_crc);

}


/* UNCOMMENT FOR DEBUGGING
static void Display_GPT_Partition_Record( gpt_partition *part, int i)
{
    if (part) {

        LOG_DEBUG("GPT PARTITION INFO ...Minor= %d\n", i);
        LOG_DEBUG("     Type ... \n");
        LOG_DEBUG("        Time  Low   = 0x%X\n", DISK_TO_CPU32(part->type.time_low) );
        LOG_DEBUG("        Time  Mid   = 0x%X\n", DISK_TO_CPU16(part->type.time_mid) );
        LOG_DEBUG("        Time High   = 0x%X\n", DISK_TO_CPU16(part->type.time_high) );
        LOG_DEBUG("        Clk  High   = 0x%X\n", part->type.clock_seq_high );
        LOG_DEBUG("        Clk   Low   = 0x%X\n", part->type.clock_seq_low );
        LOG_DEBUG("        Node        = %X %X %X %X %X %X\n",
                        part->type.node[0], part->type.node[1], part->type.node[2],
                        part->type.node[3], part->type.node[4], part->type.node[5] );
        LOG_DEBUG("     ID ... \n");
        LOG_DEBUG("        Time  Low   = 0x%X\n", DISK_TO_CPU32(part->id.time_low) );
        LOG_DEBUG("        Time  Mid   = 0x%X\n", DISK_TO_CPU16(part->id.time_mid) );
        LOG_DEBUG("        Time High   = 0x%X\n", DISK_TO_CPU16(part->id.time_high) );
        LOG_DEBUG("        Clk  High   = 0x%X\n", part->id.clock_seq_high );
        LOG_DEBUG("        Clk   Low   = 0x%X\n", part->id.clock_seq_low );
        LOG_DEBUG("        Node        = %X %X %X %X %X %X\n",
                        part->id.node[0], part->id.node[1], part->id.node[2],
                        part->id.node[3], part->id.node[4], part->id.node[5] );
        LOG_DEBUG("     Start LBA         = %"PRIu64"\n", DISK_TO_CPU64(part->start));
        LOG_DEBUG("     End   LBA         = %"PRIu64"\n", DISK_TO_CPU64(part->end));
    }
}

*/

/*
 *  Called to walk a segment storage object list and return
 *  the metadata segment that corresponds to the commit phase.
 *
 *  Returns: metadata segment ptr if successful
 *           null ptr if not successful
 */
static int  build_gpt_partition_table_from_seglist( list_anchor_t   seglist,
                                                    gpt_header     *gh,
                                                    gpt_partition  *ptable )
{
        int                   rc = ENOSYS;
        DISKSEG              *seg=NULL;
        SEG_PRIVATE_DATA *pdata=NULL;
        gpt_partition        *part=(gpt_partition *)malloc(gh->ptable_entry_size);
        char                 *cptr=(char *)ptable;
        list_element_t        iter;

        LOG_ENTRY();

        if (seglist!=NULL && gh!=NULL && ptable !=NULL) {
                rc = 0;
        }

        LIST_FOR_EACH( seglist, iter, seg ) {

                if ( seg->data_type == DATA_TYPE ) {

                        memset(part,0,gh->ptable_entry_size);

                        pdata = (SEG_PRIVATE_DATA *)seg->private_data;

                        memcpy( &part->type, &pdata->guid_type, sizeof(guid_t) );
                        cpu_guid_to_disk( &part->type );

                        memcpy( &part->id, &pdata->guid_id, sizeof(guid_t) );
                        cpu_guid_to_disk( &part->id );

                        memcpy( &part->name, &pdata->name, sizeof(utf16_char_t)*GPT_PNAME_SIZE );

                        part->attributes = CPU_TO_DISK64(pdata->attributes);

                        part->start = CPU_TO_DISK64(seg->start);
                        part->end   = CPU_TO_DISK64(seg->start + seg->size - 1);

                        cptr = ((char *)(ptable)) + (gh->ptable_entry_size * (pdata->minor-1));
                        memcpy(cptr, part, gh->ptable_entry_size);

                }

        }

        /* UNCOMMENT FOR DEBUGGING
        {
            int i;
            for (i=0, part=ptable; i<gh->ptable_count; i++) {
                if (part->end != 0) {
                    Display_GPT_Partition_Record( part, i+1);
                }
                ++part;
            }
        }
        */

        LOG_EXIT_INT(rc);
        return rc;
}


/*
 *  Called to walk a segment storage object list and return
 *  the metadata segment that corresponds to the commit phase.
 *
 *  Returns: metadata segment ptr if successful
 *           null ptr if not successful
 */
static DISKSEG * get_metadata_seg_from_seglist( list_anchor_t seglist, uint phase )
{
        DISKSEG *metadata=NULL;
        DISKSEG *seg;
        char    *mstring;
        list_element_t iter;

        LIST_FOR_EACH( seglist, iter, seg ) {

                if ( seg->data_type == META_DATA_TYPE ) {
                        mstring = strstr( seg->name, "metadata" );  // object called xxx_metadataN ?
                        if (mstring) {
                                if ( atoi(&mstring[8]) == phase) {  // N == commit phase ?
                                        metadata = seg;
                                        break;
                                }
                        }
                }

        }

        return metadata;
}



/*
 *  Called to commit a copy of the gpt header along with
 *  its corresponding partition table.
 *
 *  Returns:  0 if successful
 *            error codes otherwise
 */
static int  write_metadata(LOGICALDISK *ld, DISK_PRIVATE_DATA *disk_pdata,
                           storage_object_t *parent, uint phase, boolean backup)
{
        int rc = EINVAL;
        gpt_header     *gh=NULL;
        gpt_partition  *ptable=NULL;
        struct plugin_functions_s *DevFuncs;
        u_int32_t       crc;
        DISKSEG        *md;
        u_int32_t       gh_size = 0;

        LOG_ENTRY();

        //
        // get metadata segment corresponding to the commit phase
        // and use it to get gpt header
        //
        md = get_metadata_seg_from_seglist( ld->parent_objects, phase);
        if (md) {

                gh = (void *) calloc(1, EVMS_VSECTOR_SIZE );
                if (gh) {
                        memcpy( gh,
                                ((SEG_PRIVATE_DATA *)md->private_data)->gh,
                                ((SEG_PRIVATE_DATA *)md->private_data)->gh->size );
                }

        }

        if (gh) {

                // build the gpt partition table
                ptable = (gpt_partition *) calloc( 1, (md->size-1)*EVMS_VSECTOR_SIZE );

                if (ptable) {
                        rc = build_gpt_partition_table_from_seglist( ld->parent_objects, gh, ptable );
                }
                else {
                        rc = ENOMEM;
                }

                DevFuncs = (struct plugin_functions_s *) ld->plugin->functions.plugin;

                // commit the partition table
                if (rc == 0) {
                        if (backup) {
                                rc = EngFncs->save_metadata( parent->name, ld->name,
                                                             gh->ptable_lba, (md->size - 1), ptable );
                        } else {
                                rc = DevFuncs->write( ld, gh->ptable_lba, (md->size - 1), (void *) ptable );
                        }
                }

                // commit the gpt header
                if (rc == 0) {
                        crc = EngFncs->calculate_CRC(  EVMS_INITIAL_CRC,
                                                             ptable,
                                                             gh->ptable_count * gh->ptable_entry_size);
                        gh->ptable_crc = ~crc;
                        gh_size        = gh->size;
                        cpu_gpt_header_to_disk( gh );
                        gh->crc = 0;
                        crc = EngFncs->calculate_CRC(  EVMS_INITIAL_CRC,
                                                             gh,
                                                             gh_size );
                        gh->crc = CPU_TO_DISK32(~crc);

                        if (backup) {
                                rc = EngFncs->save_metadata( parent->name, ld->name,
                                                             DISK_TO_CPU64(gh->my_lba), 1, gh );
                        } else {
                                rc = DevFuncs->write( ld, DISK_TO_CPU64(gh->my_lba), 1, (void *) gh );
                        }
                }

        }

        if (ptable) free(ptable);
        if (gh) free(gh);

        LOG_EXIT_INT(rc);
        return rc;
}



/*
 *   Called to commit the PMBR on the specified disk.
 *
 *   Returns:  0 if successful
 *             error codes otherwise
 */
static int  write_protective_mbr( LOGICALDISK *ld,
                                  storage_object_t *parent,
                                  boolean backup )
{

        master_boot_record         *boot_sector;
        struct plugin_functions_s  *DevFuncs=NULL;
        int                         rc,i;
        msdos_partition            *part;
        chs_t                       chs;


        LOG_ENTRY();

        boot_sector = (master_boot_record *) malloc(EVMS_VSECTOR_SIZE);

        if (boot_sector) {

                DevFuncs = (struct plugin_functions_s *) ld->plugin->functions.plugin;

                rc = DevFuncs->read( ld, 0, 1, (void *) boot_sector );
                if (rc == 0) {

                        boot_sector->signature = CPU_TO_DISK16(MSDOS_DISKMAGIC);  // just to be sure

                        for (i=0; i<4; i++)  memset(&boot_sector->partition_table[i], 0, sizeof(msdos_partition));

                        part = &boot_sector->partition_table[0];

                        SYS_IND(part)     = GPT_PARTITION;
                        BOOT_IND(part)    = 0;

                        START_LBA(part)   = CPU_TO_DISK32( 1 );
                        NR_SECTS(part)    = CPU_TO_DISK32( ld->size - 1 );

                        LBA_to_Ptable_CHS(ld, 1, &chs);

                        START_CYL(part)   = (unsigned char) ( chs.cylinder  & 0x000000ff);
                        START_HEADS(part) = (unsigned char) ( chs.head      & 0x000000ff);
                        START_SECT(part)  = (unsigned char) ( chs.sector    & 0x0000003f ) |
                                            (( chs.cylinder >> 2 ) & 0xC0) ;

                        LBA_to_Ptable_CHS(ld, (ld->size - 1 ) , &chs);

                        END_CYL(part)     = (unsigned char) ( chs.cylinder  & 0x000000ff);
                        END_HEADS(part)   = (unsigned char) ( chs.head      & 0x000000ff);
                        END_SECT(part)    = (unsigned char) ( chs.sector    & 0x0000003f ) |
                                            (( chs.cylinder >> 2 ) & 0xC0) ;

                        LOG_DEBUG("Committing PMBR to disk ...\n");

                        if (backup) {
                                rc = EngFncs->save_metadata( parent->name, ld->name,
                                                             0, 1, boot_sector );
                        } else {
                                rc = DevFuncs->write( ld, 0, 1, (void *) boot_sector );
                        }

                }

        }
        else {
                rc = ENOMEM;
        }

        LOG_EXIT_INT(rc);
        return rc;
}


/*
 *  Called by SEG_COMMIT() to write out partition tables and dlat tables for
 *  the specified disk.
 */
int  commit_guid_partition_tables( LOGICALDISK *ld, storage_object_t *parent,
                                   uint commit_phase, boolean backup )
{
        int rc=0;
        DISK_PRIVATE_DATA *disk_pdata;

        LOG_ENTRY();

        disk_pdata = get_gpt_disk_private_data(ld);

        if (disk_pdata) {

                switch (commit_phase) {
                
                case 1:
                        rc = write_protective_mbr( ld, parent, backup );
                        if (rc == 0) {
                                rc = write_metadata(ld, disk_pdata, parent,
                                                    commit_phase, backup);
                        }
                        break;

                case 2:
                        rc = write_metadata(ld, disk_pdata, parent,
                                            commit_phase, backup);
                        break;

                default:
                        LOG_DEBUG("nothing to do, commit phase = %d\n", commit_phase );
                        rc = 0;
                        break;

                }

        }

        LOG_EXIT_INT(rc);
        return rc;
}