1 /* This file is the counterpart of "read.c". It contains the code for writing
2 * insofar as this is not contained in fs_readwrite().
4 * The entry points into this file are
5 * write_map: write a new zone into an inode
6 * clear_zone: erase a zone in the middle of a file
7 * new_block: acquire a new block
8 * zero_block: overwrite a block with zeroes
19 FORWARD
_PROTOTYPE( void wr_indir
, (struct buf
*bp
, int index
, zone_t zone
) );
20 FORWARD
_PROTOTYPE( int empty_indir
, (struct buf
*, struct super_block
*) );
23 /*===========================================================================*
25 *===========================================================================*/
26 PUBLIC
int write_map(rip
, position
, new_zone
, op
)
27 struct inode
*rip
; /* pointer to inode to be changed */
28 off_t position
; /* file address to be mapped */
29 zone_t new_zone
; /* zone # to be inserted */
30 int op
; /* special actions */
32 /* Write a new zone into an inode.
34 * If op includes WMAP_FREE, free the data zone corresponding to that position
35 * in the inode ('new_zone' is ignored then). Also free the indirect block
36 * if that was the last entry in the indirect block.
37 * Also free the double indirect block if that was the last entry in the
38 * double indirect block.
40 int scale
, ind_ex
= 0, new_ind
, new_dbl
,
41 zones
, nr_indirects
, single
, zindex
, ex
;
42 zone_t z
, z1
, z2
= NO_ZONE
, old_zone
;
45 struct buf
*bp_dindir
= NIL_BUF
, *bp
= NIL_BUF
;
47 rip
->i_dirt
= DIRTY
; /* inode will be changed */
48 scale
= rip
->i_sp
->s_log_zone_size
; /* for zone-block conversion */
49 /* relative zone # to insert */
50 zone
= (position
/rip
->i_sp
->s_block_size
) >> scale
;
51 zones
= rip
->i_ndzones
; /* # direct zones in the inode */
52 nr_indirects
= rip
->i_nindirs
;/* # indirect zones per indirect block */
54 /* Is 'position' to be found in the inode itself? */
56 zindex
= (int) zone
; /* we need an integer here */
57 if(rip
->i_zone
[zindex
] != NO_ZONE
&& (op
& WMAP_FREE
)) {
58 free_zone(rip
->i_dev
, rip
->i_zone
[zindex
]);
59 rip
->i_zone
[zindex
] = NO_ZONE
;
61 rip
->i_zone
[zindex
] = new_zone
;
66 /* It is not in the inode, so it must be single or double indirect. */
67 excess
= zone
- zones
; /* first Vx_NR_DZONES don't count */
71 if (excess
< nr_indirects
) {
72 /* 'position' can be located via the single indirect block. */
73 z1
= rip
->i_zone
[zones
]; /* single indirect zone */
76 /* 'position' can be located via the double indirect block. */
77 if ( (z2
= z
= rip
->i_zone
[zones
+1]) == NO_ZONE
&&
79 /* Create the double indirect block. */
80 if ( (z
= alloc_zone(rip
->i_dev
, rip
->i_zone
[0])) == NO_ZONE
)
82 rip
->i_zone
[zones
+1] = z
;
83 new_dbl
= TRUE
; /* set flag for later */
86 /* 'z' is zone number for double indirect block, either old
88 * If there wasn't one and WMAP_FREE is set, 'z' is NO_ZONE.
90 excess
-= nr_indirects
; /* single indirect doesn't count */
91 ind_ex
= (int) (excess
/ nr_indirects
);
92 excess
= excess
% nr_indirects
;
93 if (ind_ex
>= nr_indirects
) return(EFBIG
);
96 /* WMAP_FREE and no double indirect block - then no
97 * single indirect block either.
101 b
= (block_t
) z
<< scale
;
102 bp_dindir
= get_block(rip
->i_dev
, b
, (new_dbl
?NO_READ
:NORMAL
));
103 if (new_dbl
) zero_block(bp_dindir
);
104 z1
= rd_indir(bp_dindir
, ind_ex
);
109 /* z1 is now single indirect zone, or NO_ZONE; 'excess' is index.
110 * We have to create the indirect zone if it's NO_ZONE. Unless
111 * we're freeing (WMAP_FREE).
113 if (z1
== NO_ZONE
&& !(op
& WMAP_FREE
)) {
114 z1
= alloc_zone(rip
->i_dev
, rip
->i_zone
[0]);
116 rip
->i_zone
[zones
] = z1
; /* update inode w. single indirect */
118 wr_indir(bp_dindir
, ind_ex
, z1
); /* update dbl indir */
121 /* If double ind, it is dirty. */
122 if (bp_dindir
!= NIL_BUF
) bp_dindir
->b_dirt
= DIRTY
;
124 /* Release dbl indirect blk. */
125 put_block(bp_dindir
, INDIRECT_BLOCK
);
126 return(err_code
); /* couldn't create single ind */
130 /* z1 is indirect block's zone number (unless it's NO_ZONE when we're
134 ex
= (int) excess
; /* we need an int here */
135 b
= (block_t
) z1
<< scale
;
136 bp
= get_block(rip
->i_dev
, b
, (new_ind
? NO_READ
: NORMAL
) );
137 if (new_ind
) zero_block(bp
);
139 if((old_zone
= rd_indir(bp
, ex
)) != NO_ZONE
) {
140 free_zone(rip
->i_dev
, old_zone
);
141 wr_indir(bp
, ex
, NO_ZONE
);
144 /* Last reference in the indirect block gone? Then
145 * free the indirect block.
147 if(empty_indir(bp
, rip
->i_sp
)) {
148 free_zone(rip
->i_dev
, z1
);
150 /* Update the reference to the indirect block to
151 * NO_ZONE - in the double indirect block if there
152 * is one, otherwise in the inode directly.
155 rip
->i_zone
[zones
] = z1
;
157 wr_indir(bp_dindir
, ind_ex
, z1
);
158 bp_dindir
->b_dirt
= DIRTY
;
162 wr_indir(bp
, ex
, new_zone
);
164 /* z1 equals NO_ZONE only when we are freeing up the indirect block. */
165 bp
->b_dirt
= (z1
== NO_ZONE
) ? CLEAN
: DIRTY
;
166 put_block(bp
, INDIRECT_BLOCK
);
169 /* If the single indirect block isn't there (or was just freed),
170 * see if we have to keep the double indirect block, if any.
171 * If we don't have to keep it, don't bother writing it out.
173 if(z1
== NO_ZONE
&& !single
&& z2
!= NO_ZONE
&&
174 empty_indir(bp_dindir
, rip
->i_sp
)) {
175 bp_dindir
->b_dirt
= CLEAN
;
176 free_zone(rip
->i_dev
, z2
);
177 rip
->i_zone
[zones
+1] = NO_ZONE
;
180 put_block(bp_dindir
, INDIRECT_BLOCK
); /* release double indirect blk */
186 /*===========================================================================*
188 *===========================================================================*/
189 PRIVATE
void wr_indir(bp
, index
, zone
)
190 struct buf
*bp
; /* pointer to indirect block */
191 int index
; /* index into *bp */
192 zone_t zone
; /* zone to write */
194 /* Given a pointer to an indirect block, write one entry. */
196 struct super_block
*sp
;
199 panic("wr_indir() on NIL_BUF");
201 sp
= get_super(bp
->b_dev
); /* need super block to find file sys type */
203 /* write a zone into an indirect block */
204 if (sp
->s_version
== V1
)
205 bp
->b_v1_ind
[index
] = (zone1_t
) conv2(sp
->s_native
, (int) zone
);
207 bp
->b_v2_ind
[index
] = (zone_t
) conv4(sp
->s_native
, (long) zone
);
211 /*===========================================================================*
213 *===========================================================================*/
214 PRIVATE
int empty_indir(bp
, sb
)
215 struct buf
*bp
; /* pointer to indirect block */
216 struct super_block
*sb
; /* superblock of device block resides on */
218 /* Return nonzero if the indirect block pointed to by bp contains
219 * only NO_ZONE entries.
222 if(sb
->s_version
== V1
) {
223 for(i
= 0; i
< V1_INDIRECTS
; i
++)
224 if(bp
->b_v1_ind
[i
] != NO_ZONE
)
227 for(i
= 0; i
< V2_INDIRECTS(sb
->s_block_size
); i
++)
228 if(bp
->b_v2_ind
[i
] != NO_ZONE
)
236 /*===========================================================================*
238 *===========================================================================*/
239 PUBLIC
void clear_zone(rip
, pos
, flag
)
240 register struct inode
*rip
; /* inode to clear */
241 off_t pos
; /* points to block to clear */
242 int flag
; /* 1 if called by new_block, 0 otherwise */
244 /* Zero a zone, possibly starting in the middle. The parameter 'pos' gives
245 * a byte in the first block to be zeroed. Clearzone() is called from
246 * fs_readwrite(), truncate_inode(), and new_block().
249 register struct buf
*bp
;
250 register block_t b
, blo
, bhi
;
253 register zone_t zone_size
;
255 /* If the block size and zone size are the same, clear_zone() not needed. */
256 scale
= rip
->i_sp
->s_log_zone_size
;
257 if (scale
== 0) return;
259 zone_size
= (zone_t
) rip
->i_sp
->s_block_size
<< scale
;
260 if (flag
== 1) pos
= (pos
/zone_size
) * zone_size
;
261 next
= pos
+ rip
->i_sp
->s_block_size
- 1;
263 /* If 'pos' is in the last block of a zone, do not clear the zone. */
264 if (next
/zone_size
!= pos
/zone_size
) return;
265 if ( (blo
= read_map(rip
, next
)) == NO_BLOCK
) return;
266 bhi
= ( ((blo
>>scale
)+1) << scale
) - 1;
268 /* Clear all the blocks between 'blo' and 'bhi'. */
269 for (b
= blo
; b
<= bhi
; b
++) {
270 bp
= get_block(rip
->i_dev
, b
, NO_READ
);
272 put_block(bp
, FULL_DATA_BLOCK
);
277 /*===========================================================================*
279 *===========================================================================*/
280 PUBLIC
struct buf
*new_block(rip
, position
)
281 register struct inode
*rip
; /* pointer to inode */
282 off_t position
; /* file pointer */
284 /* Acquire a new block and return a pointer to it. Doing so may require
285 * allocating a complete zone, and then returning the initial block.
286 * On the other hand, the current zone may still have some unused blocks.
289 register struct buf
*bp
;
290 block_t b
, base_block
;
294 struct super_block
*sp
;
296 /* Is another block available in the current zone? */
297 if ( (b
= read_map(rip
, position
)) == NO_BLOCK
) {
298 if (rip
->i_zsearch
== NO_ZONE
) {
299 /* First search for this file. Start looking from
300 * the file's first data zone to prevent fragmentation
302 if ( (z
= rip
->i_zone
[0]) == NO_ZONE
) {
303 /* no first zone for file either */
304 z
= rip
->i_sp
->s_firstdatazone
; /* let alloc_zone decide */
307 /* searched before, start from last find */
310 if ( (z
= alloc_zone(rip
->i_dev
, z
)) == NO_ZONE
) return(NIL_BUF
);
311 rip
->i_zsearch
= z
; /* store for next lookup */
312 if ( (r
= write_map(rip
, position
, z
, 0)) != OK
) {
313 free_zone(rip
->i_dev
, z
);
318 /* If we are not writing at EOF, clear the zone, just to be safe. */
319 if ( position
!= rip
->i_size
) clear_zone(rip
, position
, 1);
320 scale
= rip
->i_sp
->s_log_zone_size
;
321 base_block
= (block_t
) z
<< scale
;
322 zone_size
= (zone_t
) rip
->i_sp
->s_block_size
<< scale
;
323 b
= base_block
+ (block_t
)((position
% zone_size
)/rip
->i_sp
->s_block_size
);
326 bp
= get_block(rip
->i_dev
, b
, NO_READ
);
332 /*===========================================================================*
334 *===========================================================================*/
335 PUBLIC
void zero_block(bp
)
336 register struct buf
*bp
; /* pointer to buffer to zero */
339 ASSERT(bp
->b_bytes
> 0);
341 memset(bp
->b_data
, 0, bp
->b_bytes
);