/
subr.c
204 lines (188 loc) · 3.59 KB
/
subr.c
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
#include "../h/param.h"
#include "../h/systm.h"
#include "../h/conf.h"
#include "../h/inode.h"
#include "../h/dir.h"
#include "../h/user.h"
#include "../h/buf.h"
/*
* Bmap defines the structure of file system storage
* by returning the physical block number on a device given the
* inode and the logical block number in a file.
* When convenient, it also leaves the physical
* block number of the next block of the file in rablock
* for use in read-ahead.
*/
daddr_t
bmap(ip, bn, rwflg)
register struct inode *ip;
daddr_t bn;
{
register i;
struct buf *bp, *nbp;
int j, sh;
daddr_t nb, *bap;
dev_t dev;
if(bn < 0) {
u.u_error = EFBIG;
return((daddr_t)0);
}
dev = ip->i_dev;
rablock = 0;
/*
* blocks 0..NADDR-4 are direct blocks
*/
if(bn < NADDR-3) {
i = bn;
nb = ip->i_un.i_addr[i];
if(nb == 0) {
if(rwflg==B_READ || (bp = alloc(dev))==NULL)
return((daddr_t)-1);
nb = bp->b_blkno;
bdwrite(bp);
ip->i_un.i_addr[i] = nb;
ip->i_flag |= IUPD|ICHG;
}
if(i < NADDR-4)
rablock = ip->i_un.i_addr[i+1];
return(nb);
}
/*
* addresses NADDR-3, NADDR-2, and NADDR-1
* have single, double, triple indirect blocks.
* the first step is to determine
* how many levels of indirection.
*/
sh = 0;
nb = 1;
bn -= NADDR-3;
for(j=3; j>0; j--) {
sh += NSHIFT;
nb <<= NSHIFT;
if(bn < nb)
break;
bn -= nb;
}
if(j == 0) {
u.u_error = EFBIG;
return((daddr_t)0);
}
/*
* fetch the address from the inode
*/
nb = ip->i_un.i_addr[NADDR-j];
if(nb == 0) {
if(rwflg==B_READ || (bp = alloc(dev))==NULL)
return((daddr_t)-1);
nb = bp->b_blkno;
bdwrite(bp);
ip->i_un.i_addr[NADDR-j] = nb;
ip->i_flag |= IUPD|ICHG;
}
/*
* fetch through the indirect blocks
*/
for(; j<=3; j++) {
bp = bread(dev, nb);
if(bp->b_flags & B_ERROR) {
brelse(bp);
return((daddr_t)0);
}
bap = bp->b_un.b_daddr;
sh -= NSHIFT;
i = (bn>>sh) & NMASK;
nb = bap[i];
if(nb == 0) {
if(rwflg==B_READ || (nbp = alloc(dev))==NULL) {
brelse(bp);
return((daddr_t)-1);
}
nb = nbp->b_blkno;
bdwrite(nbp);
bap[i] = nb;
bdwrite(bp);
} else
brelse(bp);
}
/*
* calculate read-ahead.
*/
if(i < NINDIR-1)
rablock = bap[i+1];
return(nb);
}
/*
* Pass back c to the user at his location u_base;
* update u_base, u_count, and u_offset. Return -1
* on the last character of the user's read.
* u_base is in the user address space unless u_segflg is set.
*/
passc(c)
register c;
{
register id;
if((id = u.u_segflg) == 1)
*u.u_base = c;
else
if(id?suibyte(u.u_base, c):subyte(u.u_base, c) < 0) {
u.u_error = EFAULT;
return(-1);
}
u.u_count--;
u.u_offset++;
u.u_base++;
return(u.u_count == 0? -1: 0);
}
/*
* Pick up and return the next character from the user's
* write call at location u_base;
* update u_base, u_count, and u_offset. Return -1
* when u_count is exhausted. u_base is in the user's
* address space unless u_segflg is set.
*/
cpass()
{
register c, id;
if(u.u_count == 0)
return(-1);
if((id = u.u_segflg) == 1)
c = *u.u_base;
else
if((c = id==0?fubyte(u.u_base):fuibyte(u.u_base)) < 0) {
u.u_error = EFAULT;
return(-1);
}
u.u_count--;
u.u_offset++;
u.u_base++;
return(c&0377);
}
/*
* Routine which sets a user error; placed in
* illegal entries in the bdevsw and cdevsw tables.
*/
nodev()
{
u.u_error = ENODEV;
}
/*
* Null routine; placed in insignificant entries
* in the bdevsw and cdevsw tables.
*/
nulldev()
{
}
/*
* copy count bytes from from to to.
*/
bcopy(from, to, count)
caddr_t from, to;
register count;
{
register char *f, *t;
f = from;
t = to;
do
*t++ = *f++;
while(--count);
}