File: log_read.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 (606 lines) | stat: -rw-r--r-- 21,004 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*
 *   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
 *
 *   MODULE_NAME:		log_read.c
 *
 *   COMPONENT_NAME: 	jfslib
 *
 *   FUNCTIONS:
 *              findEndOfLog()
 *              pageVal
 *              getLogpage
 *              setLogpage
 *              logRead
 *              moveWords()
 *
 */

#include "jfs_types.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include "jfs_byteorder.h"
#include "jfs_filsys.h"
#include "jfs_superblock.h"
#include "jfs_dinode.h"
#include "jfs_dtree.h"
#include "jfs_xtree.h"
#include "jfs_logmgr.h"
#include "jfs_dmap.h"
#include "jfs_imap.h"
#include "jfs_debug.h"
#include "logredo.h"
#include "devices.h"
#include "debug.h"

#include "fsckmsgc.h"		/* for fsck message logging facility */

 /* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  *
  * For the fsck message logging facility
  *
  *      defined in xchkdsk.c
  */
extern char message_parm_0[];
extern char message_parm_1[];
extern char message_parm_2[];
extern char message_parm_3[];
extern char message_parm_4[];
extern char message_parm_5[];
extern char message_parm_6[];
extern char message_parm_7[];
extern char message_parm_8[];
extern char message_parm_9[];

extern char     *msgprms[];
extern int16_t  msgprmidx[];

extern char *terse_msg_ptr;
extern char *verbose_msg_ptr;

extern char *MsgText[];

 /* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  *
  *    R E M E M B E R    M E M O R Y    A L L O C    F A I L U R E
  *
  */
extern int32_t  Insuff_memory_for_maps;
extern char     *available_stg_addr;
extern int32_t  available_stg_bytes;
extern char     *bmap_stg_addr;
extern int32_t  bmap_stg_bytes;

 /* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  *
  *   L O C A L   M A C R O    D E F I N I T I O N S
  *
  */
#define BTOLOGPN(x)  ((unsigned)(x) >> L2LOGPSIZE)

#define LOGPNTOB(x)  ((x)<<L2LOGPSIZE)
#define min(A,B) ((A) < (B) ? (A) : (B))

 /* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
  *
  *    S T U F F    F O R    T H E    L O G
  *
  *       externals defined in logredo.c
  */
extern int16_t  loglocation;    /* 1 = inlinelog, 2 = outlinelog */
extern int32_t  logminor;       /* minor number of log device */
extern int32_t  logfd;          /* file descriptor for log */
extern int32_t  logsize;        /* size of log in pages */
int32_t  loglastp;              /* last page of log read       */
int32_t  lognumread;            /* number of log pages read    */

/*
 *      open file system aggregate/lv array
 *     Defined in logredo.c
 */
extern struct vopen vopen[];              /* (88) */

/*
 *      log page buffer cache
 *
 * log has its own 4 page buffer pool.
 *   externals defined in logredo.c
 */

int  nextrep;                 /* next log buffer pool slot to replace */
int  logptr[4];               /* log pages currently held in logp */
logpage_t  logp[4];           /* log page buffer pool  */

/*
 * external references
 */
extern int errno;
extern int logError( int , int );
extern int32_t alloc_storage( int32_t, void **, int32_t * );

extern void fsck_send_msg( int, int, int );		/* defined in fsckmsg.c */
extern void ujfs_swap_lrd_t( lrd_t * );

/*
 * forward references
 */
int32_t findEndOfLog(void);
int     pageVal(int, int*, int*);
int32_t getLogpage(int);
int     setLogpage(int32_t pno,  int32_t *,   int32_t *, int32_t );
int     logRead( int32_t , struct lrd *, char* );
int32_t moveWords( int32_t  , int32_t *, int32_t *,  int32_t *);


/*
 * NAME:        findEndOfLog()
 *
 * FUNCTION:    Returns the address of the end of the last record in the log.
 *              (i.e. the address of the byte following its descriptor).
 *
 *        Note: At the first release, log page is not written in a ping pong
 *              manner, so the logend is the binary search result
 *
 *              The end of the log is found by finding the page with the
 *              highest page number and for which h.eor == t.eor and
 *              h.eor > 8 (i.e. a record ends on the page).
 *              Page numbers are compared by comparing their difference
 *              with zero (necessary because page numbers are allowed to wrap.)
 *
 * RETURNS:     >0              - byte offset of last record
 *              REFORMAT_ERROR(-3)      - i/o error, reformat the log
 *              MAJOR_ERROR(-2) - other major errors other than EIO.
 */
int32_t findEndOfLog()
{

    int32_t  left, right, pmax, pval, eormax, eorval, k, rc;

    /* binary search for logend */
    left = 2;               /* first page containing log records
                               since page 0 is never used, page 1 is
                               log superblock */
    right = logsize - 1;    /* last page containing log records */

    if ((rc = pageVal(left, &eormax, &pmax)) < 0) {
        sprintf( message_parm_0, "(d) %d", rc  );
        msgprms[0] = message_parm_0;
        msgprmidx[0] = 0;
        fsck_send_msg( lrdo_FEOLPGV1FAIL, 0, 1 );
        return(rc);
    }

    while ((right - left) > 1) {
        k = (left + right ) >> 1;
        if ((rc = pageVal(k, &eorval, &pval)) < 0) {
            sprintf( message_parm_0, "(d) %d", rc  );
            msgprms[0] = message_parm_0;
            msgprmidx[0] = 0;
            fsck_send_msg( lrdo_FEOLPGV2FAIL, 0, 1 );
            return(rc);
        }

        if (pval - pmax > 0 ) {       
            left = k;
            pmax = pval;
            eormax = eorval;
        } else
            right = k;
    }
    if ((rc = pageVal(right, &eorval, &pval)) < 0) {
        sprintf( message_parm_0, "(d) %d", rc  );
        msgprms[0] = message_parm_0;
        msgprmidx[0] = 0;
        fsck_send_msg( lrdo_FEOLPGV3FAIL, 0, 1 );
        return(rc);
    }

    /*
     * the last thing to determine is whether it is the first page of
     * the last long log record and system was crashed when its second
     * page is written. If the eor of the chosen page is LOGPHDRSIZE,
     * then this page contains a partial log record, ( otherwise, the
     * the long log record's second page should be chosen ).
     * This page should be thrown away. its previous page will be
     * the real last log page.
     */

    if (( pval - pmax ) > 0 ) {
        if (eorval == LOGPHDRSIZE ) {
            if ((rc = pageVal(right-1, &eorval, &pval)) < 0) {
                sprintf( message_parm_0, "(d) %d", rc  );
                msgprms[0] = message_parm_0;
                msgprmidx[0] = 0;
                fsck_send_msg( lrdo_FEOLPGV4FAIL, 0, 1 );
                return(rc);
            }
            return(LOGPNTOB(right-1) + eorval);
        } else
           return(LOGPNTOB(right) + eorval);
    } else {
        if ( eormax == LOGPHDRSIZE ) {
            left = (left == 2) ? logsize -1 : left - 1;
            if ((rc = pageVal(left, &eormax, &pmax)) < 0) {
                sprintf( message_parm_0, "(d) %d", rc  );
                msgprms[0] = message_parm_0;
                msgprmidx[0] = 0;
                fsck_send_msg( lrdo_FEOLPGV4AFAIL, 0, 1 );
                return(rc);
            }
        }
        return(LOGPNTOB(left) + eormax);
    }
}


/*
 * NAME:        pageVal(pno, eor, pmax)
 *
 * FUNCTION:    Read the page into the log buffer pool and call setLogpage
 *              to form consistent log page.
 *
 * RETURNS:     0                       - ok
 *              REFORMAT_ERROR(-3)      - I/O error, reformat the log
 *              MAJOR_ERROR(-2)         - other major errors other than EIO.
 */
int pageVal( int  pno,      /* page number in log           */
             int  *eor,     /* corresponding eor value      */
             int  *pmax)    /* pointer to returned page number */
{
    int  buf0;              /* logp[] buffer element number         */

    /* Read the page into the log buffer pool. */
    if ((buf0 = getLogpage(pno)) < 0) {
        sprintf( message_parm_0, "(d) %d", pno  );
        msgprms[0] = message_parm_0;
        msgprmidx[0] = 0;
        sprintf( message_parm_1, "(d) %d", buf0  );
        msgprms[1] = message_parm_1;
        msgprmidx[1] = 0;
        fsck_send_msg( lrdo_PVGETPGFAIL, 0, 2 );

        return(buf0);
    }
    return(setLogpage(pno, eor, pmax, buf0));
}


/*
 * NAME:        getLogpage(pno)
 *
 * FUNCTION:    if the specified log page is in buffer pool, return its
 *              index. Otherwise read log page into buffer pool.
 *
 * PARAMETERS:  pno -   log page number to look for.
 *
 * RETURNS:     0 - 3   - index of the buffer pool the page located
 *              REFORMAT_ERROR(-3)      - I/O error, reformat the log
 *              MAJOR_ERROR(-2)         - other major errors other than EIO.
 */
int32_t getLogpage(int  pno)  /* page of log */
{
    int32_t  k, rc;

    /*
     * is it in buffer pool ?
     */
    for (k = 0; k <= 3; k++)
        if (logptr[k] == pno) return(k);

    /*
     * read page into buffer pool into next slot
     * don't have to use llseek() here.  log dev will never be > 2 gig
     */
    nextrep = (nextrep + 1) % 4;
    if ( loglocation == INLINELOG )
        rc = ujfs_rw_diskblocks( logfd,
                                 (uint64_t)(vopen[logminor].logxaddr+LOGPNTOB(pno)),
                                 (unsigned)LOGPSIZE,
                                 (char *)&logp[nextrep],
                                 GET);
    else
        rc = ujfs_rw_diskblocks( logfd,
                                 (uint64_t)LOGPNTOB(pno),
                                 (unsigned)LOGPSIZE,
                                 (char *)&logp[nextrep],
                                 GET);

    if (rc !=  0 ) {
        return(JLOG_READERROR1);
    }

    logptr[nextrep] = pno;
    return(nextrep);
}


/*
 * NAME:        setLogpage(pno, eor, pmax, buf)
 *
 * FUNCTION:    Forms consistent log page and returns eor and pmax values.
 *
 *              During the first release the following conditions are
 *              assumed:
 *              1) No corrupted write during power failure
 *              2) No split write
 *              3) No out-of-order sector write
 *
 *              If the header and trailer in the page are not equal, a
 *              system crash happened during this page write. It
 *              is reconciled as follows:
 *
 *              1) if h.page != t.page, the smaller value is taken and
 *                 the eor fields set to LOGPHDSIZE.
 *                 reason: This can happen when a old page is over-written
 *                 by a new page and the system crashed. So this page
 *                 should be considered not written.
 *              2) if h.eor != t.eor, the smaller value is taken.
 *                 reason: The last log page was rewritten for each
 *                 commit record. A system crash happened during the
 *                 page rewriting. Since we assume that no corrupted write
 *                 no split write and out-of-order sector write, the
 *                 previous successfuly writing is still good
 *              3) if no record ends on the page (eor = 8), still return it.
 *                 Let the caller determine whether a) a good long log record
 *                 ends on the next log page. or b) it is the first page of the
 *                 last long log record and system was crashed when its second
 *                 page is written.
 *
 *
 * RETURNS:     0                       - ok
 *              REFORMAT_ERROR(-3)      - I/O error, reformat log
 *              MAJOR_ERROR(-2)         - other major error
 */
int setLogpage( int32_t  pno,      /* page number of log           */
                int32_t  *eor,     /* log header eor to return     */
                int32_t  *pmax,    /* log header page number to return */
                int32_t  buf)      /* logp[] index number for page */
{
    int32_t  diff1, diff2, rc;

    /* check that header and trailer are the same */
    if ((diff1 = (__le32_to_cpu(logp[buf].h.page) -
		  __le32_to_cpu(logp[buf].t.page))) != 0) {       
        if (diff1 > 0)
            logp[buf].h.page = logp[buf].t.page; /* Both little-endian */
        else
            logp[buf].t.page = logp[buf].h.page; /* Both little-endian */

        logp[buf].h.eor = logp[buf].t.eor = __cpu_to_le16(LOGPHDRSIZE);
                                                /* empty page */
    }

    if ((diff2 = (__le16_to_cpu(logp[buf].h.eor) -
		  __le16_to_cpu(logp[buf].t.eor))) != 0) {       
        if (diff2 > 0)
            logp[buf].h.eor = logp[buf].t.eor; /* Both little-endian */
        else
            logp[buf].t.eor = logp[buf].h.eor; /* Both little-endian */
    }

    /* if any difference write the page out */
    if (diff1 || diff2) {
        rc = ujfs_rw_diskblocks(logfd,
                                (uint64_t)(vopen[logminor].logxaddr+LOGPNTOB(pno)),
                                (unsigned long)LOGPSIZE,
                                (char *)&logp[buf],
                                PUT);
        if ( rc != 0 ) {
            sprintf( message_parm_0, "(d) %d", pno  );
            msgprms[0] = message_parm_0;
            msgprmidx[0] = 0;
            sprintf( message_parm_1, "(d) %d", rc  );
            msgprms[1] = message_parm_1;
            msgprmidx[1] = 0;
            fsck_send_msg( lrdo_SLPWRITEFAIL, 0, 2 );

            return(JLOG_WRITEERROR1);
        }
    }

    /*
     * At this point, it is still possible that logp[buf].h.eor
     * is LOGPHDRSIZE, but we return it anyway. The caller will make
     * decision.
     */

    *eor = __le16_to_cpu(logp[buf].h.eor);
    *pmax = __le32_to_cpu(logp[buf].h.page);

    return (0);
}

 /*
 * NAME:        logRead(logaddr , ld, dataptr)
 *
 * FUNCTION:    reads the log record addressed by logaddr and
 *              returns the address of the preceding log record.
 *
 * PARAMETERS:  logaddr -  address of the end of log record to read
 *                                 Note: log is read backward, so this is
 *                                 the address starting to read
 *              ld      - pointer to a log record descriptor
 *              dataptr - pointer to data buffer
 *
 * RETURNS:     < 0     - there is an i/o error in reading
 *              > 0     - the address of the end of the preceding log record
 */
int logRead( int32_t     logaddr,      /* address of log record to read */
             struct lrd  *ld,          /* pointer to a log record descriptor */
             char        * dataptr)    /* pointer to buffer.  LOGPSIZE*2 long */
{
    int  buf,off,rc,nwords, pno;

    /* get page containing logaddr into log buffer pool */
    pno = BTOLOGPN(logaddr);
    if (pno != loglastp) {
        loglastp = pno;
        lognumread += 1;
        if (lognumread > logsize - 2) {
            logError(LOGWRAP,0);
            sprintf( message_parm_0, "(d) %d", lognumread  );
            msgprms[0] = message_parm_0;
            msgprmidx[0] = 0;
            fsck_send_msg( lrdo_LRLOGWRAP, 0, 1 );

            return(JLOG_LOGWRAP);
        }
    }

    buf = getLogpage(pno);
    if (buf < 0) {
        sprintf( message_parm_0, "(d) %d", pno  );
        msgprms[0] = message_parm_0;
        msgprmidx[0] = 0;
        sprintf( message_parm_1, "(d) %d", buf  );
        msgprms[1] = message_parm_1;
        msgprmidx[1] = 0;
        fsck_send_msg( lrdo_LRREADFAIL, 0, 2 );

        return(buf);
    }

    /* read the descriptor */
    off = logaddr & (LOGPSIZE - 1) ;  /* offset just past desc. */
    rc = moveWords(LOGRDSIZE/4, (int32_t *)ld, &buf, &off);
    if (rc < 0) {
        sprintf( message_parm_0, "(d) %d", rc  );
        msgprms[0] = message_parm_0;
        msgprmidx[0] = 0;
        fsck_send_msg( lrdo_LRMWFAIL1, 0, 1 );
        return(rc);
    }

#if __BYTE_ORDER == __BIG_ENDIAN
    ujfs_swap_lrd_t( ld );
#endif

    /* read the data if there is any */
    if (ld->length > 0) {
        if( ld->length > LOGPSIZE*2 ) {                   
            rc = READLOGERROR;                          
            sprintf( message_parm_0, "(d) %d", pno  );  
            msgprms[0] = message_parm_0;                
            msgprmidx[0] = 0;                           
            fsck_send_msg( lrdo_LRMWFAIL3, 0, 1 );      
            return(rc);                                 
        }                                               

        nwords = (ld->length + 3)/4; /* if length is partial word, still
                                        read it   */
        rc = moveWords(nwords,(int32_t *)dataptr,&buf,&off);
        if (rc < 0) {
            sprintf( message_parm_0, "(d) %d", rc  );
            msgprms[0] = message_parm_0;
            msgprmidx[0] = 0;
            fsck_send_msg( lrdo_LRMWFAIL2, 0, 1 );
            return(rc);
        }
    }

    return(LOGPNTOB(logptr[buf]) + off);
}


/*
 * NAME:        moveWords()
 *
 * FUNCTION:    moves nwords from buffer pool to target. data
 *              is moved in backwards direction starting at offset.
 *              If partial log record is on the previous page,
 *              or we have exhaust the current page (all bytes were read),
 *              the previous page is read into the buffer pool.
 *              On exit buf will point to this page in the buffer pool
 *              and offset to where the move stopped.
 *
 *              Note: the previous page is fetched whenever
 *              the current page is exhausted (all bytes were read)
 *              even if all the words required to satisfy this move
 *              are on the current page.
 *
 * PARAMETERS:  nwords  - number of 4-byte words to move
 *              target  - address of target (begin address)
 *              buf     - index in buffer pool of current page
 *              offset  - initial offset in buffer pool page, this offset
 *                        includes the page head size
 *
 * RETURNS:     = 0             - ok
 *              < 0             - error returned from getLogpage
 */
int32_t moveWords( int32_t  nwords,   /* number of 4-byte words to move */
                   int32_t  *target,  /* address of target (begin address) */
                   int32_t  *buf,     /* index in buffer pool of current page */
                   int32_t  *offset)  /* initial offset in buffer pool page */
{
    int  n,j,words,pno;
    int  *ptr;

    j = (*offset - LOGPHDRSIZE)/4 - 1; /* index in log page data area
                                          of first word to move      */
    words  = min(nwords,j + 1);  /* words on this page to move */
    ptr = target + nwords - 1; /* last word of target */
    for (n = 0; n < words; n++) {
        *ptr = logp[*buf].data[j];
        j = j - 1;
        ptr = ptr - 1;
    }
    *offset = *offset - 4*words;

    /*
     * If partial log record is on the previous page,
     * or we have read all the log records in the current page,
     * get the previous page
     */

    if ( words != nwords    /* we get less than nwords */
         || j < 0)  {        /* or exhaust the page, so offset is just */
                            /* the page head, then j < 0              */
        /* get previous page */
        pno = logptr[*buf];
        pno = pno - 1;
        /* if we hit beginning location of the log, go wrapped,
        read log record from the end location of the log   */
        if (pno == 1) pno = logsize - 1;
        *buf  = getLogpage(pno);
        if (*buf < 0) {
            sprintf( message_parm_0, "(d) %d", pno  );
            msgprms[0] = message_parm_0;
            msgprmidx[0] = 0;
            sprintf( message_parm_1, "(d) %d", (*buf)  );
            msgprms[1] = message_parm_1;
            msgprmidx[1] = 0;
            fsck_send_msg( lrdo_MWREADFAIL, 0, 2 );

            return(*buf);
        }
        *offset = LOGPSIZE - LOGPTLRSIZE;
        j = LOGPSIZE/4 - 4 - 1; /* index last word of data area */
        /* move rest of nwords if any. this will never
           exhaust the page.                          */
        for (n = 0; n < nwords - words ; n++) {
            *ptr = logp[*buf].data[j];
            j = j - 1;
            ptr = ptr - 1;
        }
        *offset = *offset - 4*(nwords - words);
    }

    return(0);
}