File: logform.c

package info (click to toggle)
jfsutils 1.0.14-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,132 kB
  • ctags: 4,667
  • sloc: ansic: 41,422; sh: 335; makefile: 126
file content (290 lines) | stat: -rw-r--r-- 10,267 bytes parent folder | download
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
/*
 *   Copyright (c) International Business Machines  Corp., 2000
 *
 *   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
 *
 *  format log
 *
 * XJFS decided to put log inside the file system.
 * The following logform utility is used to support both inline log file 
 * system and outline log file system.
 * For logform command, it should only be used when it is an outline
 * log file system.
 * in addtion, logform is called from mkfs or logredo, or extendfs 
 * (kernel code).
 *
 * command:
 *	xlogform lvname [nblocks]
 *
 * where 
 * lvname	- the name of a logical volume of the outline log device;
 * [nblocks]	- the size of the log in 512 byte blocks
 * 		  (optional paramter);
 *		  this parameter is here for bringup purpose. For
 *		  normal use, always put zero.
 *	
 * the following disk pages starting from the beginning of the log
 * are formatted:
 *
 * page 0 - not changed              
 * page 1 - log superblock
 * page 2 - A SYNC log record is written into this page at logform time
 * pages 3-N  - set to empty log pages
 */

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

#include "jfs_byteorder.h"
#include "jfs_types.h"
#include "devices.h"

#include "jfs_logmgr.h" 
#include "message.h"

/* endian routines */
extern uint32_t type_jfs;
extern void ujfs_swap_logsuper_t( logsuper_t  * );
extern void ujfs_swap_logpage_t( logpage_t *, uint8_t );

/*
 * NAME:	jfs_logform
 * FUNCTION:	format file system log
 *
 * RETURN:	0 -	successful
 *		   -1 -	error occur 
 *
 */
int32_t jfs_logform( int32_t   fd,             /* for inline log, this is a file descriptor
                                                * for an opened device to write log.
                                                * for outline log, it is -1 */
                     int32_t   aggr_blk_size,  /* aggregate block size in bytes */
                     int32_t   s_l2bsize,      /* log2 of aggregate block size in bytes */
                     uint32_t  s_flag,         /* fs superblock s_flag is passed in   */
                     int64_t   log_start,      /* offset of the start of inline log in 
                                                * number of aggr. blocks. for outline log
                                                * it is set as zero */
                     int32_t   log_len,        /* inline log length in number of aggr. blks 
                                                * for outline log, it is zero */
                     char      *dev_name,      /* logical volume of the outline log device  
                                                * for inline log, it is NULL */
                     int32_t   nblks           /* size of the outline log in 512 byte blocks
                                                * for inline log, it is zero */
                   )
{
    int64_t    log_len_in_bytes;
    int32_t    npages,rc,k;
    char       logpages[4 * LOGPSIZE];
    logpage_t  *logp;        /* array of 4 log pages */
    static logsuper_t  log_sup;
    struct lrd  *lrd_ptr;
    int64_t    log_begin;  /* the location of the beginning of the log inside
                  * of the file system. ( in bytes )
                  */
    int64_t    log_contwt;
    int16_t    inlinelog = (s_flag & JFS_INLINELOG );
    int        Working_counter;
    char       *Working[5];

#define LOGBUFSIZE	4 * LOGPSIZE
    logp = (logpage_t *) &logpages;
    Working[0] = "   |\r";
    Working[1] = "   /\r";
    Working[2] = "   -\r";
    Working[3] = "   \\\r";

    /* 
     * if it is an outline log, do device check and open device
     */
    if (!inlinelog ) {
        return -1;
    } else {  /* the fs has an inlinelog  */
        log_len_in_bytes = ((int64_t) log_len) << s_l2bsize;
        npages = log_len_in_bytes / LOGPSIZE; 
    }

    /* 
     * init log superblock: log page 1
     */
    log_sup.magic = LOGMAGIC;
    log_sup.version = LOGVERSION;
    log_sup.state = LOGREDONE;
    log_sup.flag = s_flag;  /* assign fs s_flag to log superblock.
                 * currently s_flag carries the inlinelog
                 * info and commit option ( i.e. group commit
                 * or lazy commit, etc.. )
                 */
    log_sup.size = npages;
    log_sup.bsize = aggr_blk_size;
    log_sup.l2bsize = s_l2bsize; 
    log_sup.end = 2*LOGPSIZE + LOGPHDRSIZE + LOGRDSIZE;

    /* find the log superblock location 
     */
    log_begin = log_start << s_l2bsize;

    /* swap if on big endian machine */
    if (type_jfs & JFS_SWAP_BYTES) {
      ujfs_swap_logsuper_t( &log_sup );
    }

    rc = ujfs_rw_diskblocks(fd, (log_begin+LOGPSIZE), (unsigned)LOGPSIZE,
                            (char *)&log_sup, PUT);
    if ( rc != 0 )
        return -1;

    /*
     * init device pages 2 to npages-1 as log data pages:
     *
     * log page sequence number (lpsn) initialization:
     * the N (= npages-2) data pages of the log is maintained as 
     * a circular file for the log records;
     * lpsn grows by 1 monotonically as each log page is written 
     * to the circular file of the log;
     * Since the AIX DUMMY log record is dropped for this XJFS, 
     * and setLogpage() will not reset the page number even if
     * the eor is equal to LOGPHDRSIZE. In order for binary search
     * still work in find log end process, we have to simulate the
     * log wrap situation at the log format time.
     * The 1st log page written will have the highest lpsn. Then
     * the succeeding log pages will have ascending order of
     * the lspn starting from 0, ... (N-2)
     */

    /* 
        initialize 1st 2 log pages to be written: lpsn = N-1, 0
        and also a SYNCPT log record is written to the N-1 page 
    
        Since the log is always an even number of meg, if we
        write 2 pages before entering the loop, we are assured
        that the log will end after a 4 page buffer.
    */

    logp[0].h.eor = logp[0].t.eor = LOGPHDRSIZE + LOGRDSIZE;
    logp[0].h.page = logp[0].t.page = npages - 3;
    lrd_ptr = (struct lrd *)&logp[0].data;
    lrd_ptr->logtid = 0;
    lrd_ptr->backchain = 0;
    /* swap log page data here if needed */
    lrd_ptr->type = __le16_to_cpu(LOG_SYNCPT);
    lrd_ptr->length = 0;
    lrd_ptr->log.syncpt.sync = 0;

    logp[1].h.eor = logp[1].t.eor = LOGPHDRSIZE;
    logp[1].h.page = logp[1].t.page = 0;
    lrd_ptr = (struct lrd *)&logp[1].data;
    lrd_ptr->logtid = 0;
    lrd_ptr->backchain = 0;
    /* swap log page data here if needed */
    lrd_ptr->type = __le16_to_cpu(LOG_SYNCPT);
    lrd_ptr->length = 0;
    lrd_ptr->log.syncpt.sync = 0;

    /* swap if on big endian machine */
    if (type_jfs & JFS_SWAP_BYTES) {
        /* copy to buffer for swap */
        logpage_t *buffer;
        buffer = malloc(2*LOGPSIZE);
        if ( buffer == NULL) {
            message_user(MSG_OSO_INSUFF_MEMORY, NULL, 0, STDOUT_CODE, NO_RESPONSE, OSO_MSG);
            return( ENOMEM );
        }
        memcpy( buffer, logp, 2*LOGPSIZE );
        ujfs_swap_logpage_t( buffer, 2 );
        rc = ujfs_rw_diskblocks(fd, (log_begin+2*LOGPSIZE), 
                                (unsigned) 2*LOGPSIZE,
                                (char *)buffer, PUT);
        free( buffer );
    } else {
        rc = ujfs_rw_diskblocks(fd, (log_begin+2*LOGPSIZE), 
                                (unsigned) 2*LOGPSIZE,
                                (char *)&(logp[0]), PUT);
    }
    if ( rc != 0 )
        return -1;

    /* initialize buffer to write 4 pages at a time */
    logp[0].h.eor = logp[0].t.eor = LOGPHDRSIZE;

    logp[2].h.eor = logp[2].t.eor = LOGPHDRSIZE;
    lrd_ptr = (struct lrd *)&logp[2].data;
    lrd_ptr->logtid = 0;
    lrd_ptr->backchain = 0;
    /* swap log page data here if needed */
    lrd_ptr->type = __le16_to_cpu(LOG_SYNCPT);
    lrd_ptr->length = 0;
    lrd_ptr->log.syncpt.sync = 0;

    logp[3].h.eor = logp[3].t.eor = LOGPHDRSIZE;
    lrd_ptr = (struct lrd *)&logp[3].data;
    lrd_ptr->logtid = 0;
    lrd_ptr->backchain = 0;
    /* swap log page data here if needed */
    lrd_ptr->type = __le16_to_cpu(LOG_SYNCPT);
    lrd_ptr->length = 0;
    lrd_ptr->log.syncpt.sync = 0;

    /* initialize succeeding log  pages: lpsn = 1, 2, ..., (N-2) */
    Working_counter = 0;
    log_contwt = log_begin + LOGBUFSIZE;
    for ( k = 1; k < npages - 4; k+=4 ) {
        logp[0].h.page = logp[0].t.page = k;
        logp[1].h.page = logp[1].t.page = k + 1;
        logp[2].h.page = logp[2].t.page = k + 2;
        logp[3].h.page = logp[3].t.page = k + 3;

        /* swap if on big endian machine */
        if (type_jfs & JFS_SWAP_BYTES) {
            /* copy to buffer for swap */
            ujfs_swap_logpage_t( logp, 4 );
        }

        rc = ujfs_rw_diskblocks(fd, log_contwt, 
                                (unsigned) LOGBUFSIZE, (char *)&(logp[0]), PUT);
        if (rc != 0 )
            return -1;

        log_contwt += LOGBUFSIZE;

        Working_counter++;
        switch ( Working_counter ) {
            case( 100 ):
                printf("%s",Working[0]);
                fflush( stdout );
                break;
            case( 200 ):
                printf("%s",Working[1]);
                fflush( stdout );
                break;
            case( 300 ):
                printf("%s",Working[2]);

                fflush( stdout );
                break;
            case( 400 ):
                printf("%s",Working[3]);
                fflush( stdout );
                Working_counter = 0;
                break;
            default:
                break;
        }
    }

    return(0);

}