summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/OR_USING_C/13.3.c
blob: a0dc4b13a01c77fc2a931e7a01a122be29a55f9f (plain)
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

#include	<sys/param.h>
#include	<sys/time.h>
#include	<sys/vnode.h>
#include	<ufs/inode.h>
#include	<ufs/fs.h>

#define sblock    sb_un.u_sblock

/*
 * The super block.  We allow enough room for
 * a complete disk block.
 */
union {
    struct  fs u_sblock;
    char    dummy[SBSIZE];
} sb_un;

read_blocks(dp)
struct dinode *dp;
{
    int count;
    register int i, n;
    char dblock[MAXBSIZE];

    count = dp->di_size;

    /*
     * For each direct block in the file (NDADDR indicates
     * the number of direct addresses stored)...
     */
    for (i = 0; (i < NDADDR) && (count > 0); i++) {
        /* 
         * Read in the block from disk.  Read in count
         * bytes or a disk block, whichever is less.
         */
        bread(fsbtodb(&sblock, dp->di_db[i]), dblock,
              n = min(count, sblock.fs_bsize));
        count -= n;

        /* process data block ... */
   
    }

    /* 
     * Now start reading the indirect blocks.  NIADDR is 
     * the number of indirect addresses.  Recall that 
     * the first indirect address is singly indirect,
     * the second is doubly indirect, an so on.
     */
    for (i = 0; (i < NIADDR) && (count > 0); i++)
        read_indirect (dp->di_ib[i], i, &count);
}

/*
 * read_indirect--read the indirect blocks of the file.  The
 * level argument indicates our level of indirection; 0 is 
 * singly indirect, 1 is doubly indirect, and so on.
 */
read_indirect (blkno, level, count)
ino_t blkno;
int *count;
int level;
{
    register int i, n;
    char dblock[MAXBSIZE];
    daddr_t idblk[MAXBSIZE / sizeof(daddr_t)];

    /*
     * Read in the block from disk.
     */
    if (blkno)
        bread(fsbtodb(&sblock, blkno), idblk, sblock.fs_bsize);
     else
        bzero(idblk, sblock.fs_bsize);

    /* 
     * If level is zero, then this block contains disk block
     * addresses, since blkno was a singly indirect address.
     * If level is non-zero, then this block contains addresses
     * of more indirect blocks.
     */
    if (level <= 0) {
        /*
         * For each disk block (the NINDIR macro returns
         * the number of indirect addresses in a block)...
         */
        for (i = 0; (i < NINDIR(&sblock)) && (*count > 0); i++) {
            /*
             * Read in the block from disk.
             */
            bread(fsbtodb(&sblock, idblk[i]), dblock,
                  n = min(*count, sblock.fs_bsize));
            *count -= n;

            /* process data block ... */
      }

      /*
       * Done processing.
       */
      return;
  }

     /*
      * Decrement the level we're at.
      */
     level--;

     /*
      * Handle the next level of indirection by calling
      * ourselves recursively with each address in this
      * block.
      */
     for (i = 0; i < NINDIR(&sblock); i++)  
         read_indirect(idblk[i], level, count);
}