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 static void wr_indir(struct buf
*bp
, int index
, zone_t zone
);
20 static int empty_indir(struct buf
*, struct super_block
*);
23 /*===========================================================================*
25 *===========================================================================*/
26 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
= NULL
, *bp
= NULL
;
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
);
95 if(z
== NO_ZONE
&& (op
& WMAP_FREE
)) {
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
!= NULL
) MARKDIRTY(bp_dindir
);
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 MARKDIRTY(bp_dindir
);
162 wr_indir(bp
, ex
, new_zone
);
164 /* z1 equals NO_ZONE only when we are freeing up the indirect block. */
165 if(z1
== NO_ZONE
) { MARKCLEAN(bp
); } else { MARKDIRTY(bp
); }
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 MARKCLEAN(bp_dindir
);
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 static 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 NULL");
201 sp
= get_super(lmfs_dev(bp
)); /* need super block to find file sys type */
203 /* write a zone into an indirect block */
204 if (sp
->s_version
== V1
)
205 b_v1_ind(bp
)[index
] = (zone1_t
) conv2(sp
->s_native
, (int) zone
);
207 b_v2_ind(bp
)[index
] = (zone_t
) conv4(sp
->s_native
, (long) zone
);
211 /*===========================================================================*
213 *===========================================================================*/
214 static 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 for(i
= 0; i
< V2_INDIRECTS(sb
->s_block_size
); i
++)
223 if( b_v2_ind(bp
)[i
] != NO_ZONE
)
230 /*===========================================================================*
232 *===========================================================================*/
233 void clear_zone(rip
, pos
, flag
)
234 register struct inode
*rip
; /* inode to clear */
235 off_t pos
; /* points to block to clear */
236 int flag
; /* 1 if called by new_block, 0 otherwise */
238 /* Zero a zone, possibly starting in the middle. The parameter 'pos' gives
239 * a byte in the first block to be zeroed. Clearzone() is called from
240 * fs_readwrite(), truncate_inode(), and new_block().
246 int scale
, zone_size
;
248 /* If the block size and zone size are the same, clear_zone() not needed. */
249 scale
= rip
->i_sp
->s_log_zone_size
;
250 if (scale
== 0) return;
252 zone_size
= rip
->i_sp
->s_block_size
<< scale
;
253 if (flag
== 1) pos
= (off_t
) ((pos
/zone_size
) * zone_size
);
254 next
= pos
+ rip
->i_sp
->s_block_size
- 1;
256 /* If 'pos' is in the last block of a zone, do not clear the zone. */
257 if (next
/zone_size
!= pos
/zone_size
) return;
258 if ( (blo
= read_map(rip
, next
)) == NO_BLOCK
) return;
259 bhi
= (block_t
) ( ((blo
>>scale
)+1) << scale
) - 1;
261 /* Clear all the blocks between 'blo' and 'bhi'. */
262 for (b
= blo
; b
<= bhi
; b
++) {
263 bp
= get_block(rip
->i_dev
, b
, NO_READ
);
265 put_block(bp
, FULL_DATA_BLOCK
);
270 /*===========================================================================*
272 *===========================================================================*/
273 struct buf
*new_block(rip
, position
)
274 register struct inode
*rip
; /* pointer to inode */
275 off_t position
; /* file pointer */
277 /* Acquire a new block and return a pointer to it. Doing so may require
278 * allocating a complete zone, and then returning the initial block.
279 * On the other hand, the current zone may still have some unused blocks.
282 register struct buf
*bp
;
283 block_t b
, base_block
;
288 /* Is another block available in the current zone? */
289 if ( (b
= read_map(rip
, position
)) == NO_BLOCK
) {
290 if (rip
->i_zsearch
== NO_ZONE
) {
291 /* First search for this file. Start looking from
292 * the file's first data zone to prevent fragmentation
294 if ( (z
= rip
->i_zone
[0]) == NO_ZONE
) {
295 /* No first zone for file either, let alloc_zone
297 z
= (zone_t
) rip
->i_sp
->s_firstdatazone
;
300 /* searched before, start from last find */
303 if ( (z
= alloc_zone(rip
->i_dev
, z
)) == NO_ZONE
) return(NULL
);
304 rip
->i_zsearch
= z
; /* store for next lookup */
305 if ( (r
= write_map(rip
, position
, z
, 0)) != OK
) {
306 free_zone(rip
->i_dev
, z
);
311 /* If we are not writing at EOF, clear the zone, just to be safe. */
312 if ( position
!= rip
->i_size
) clear_zone(rip
, position
, 1);
313 scale
= rip
->i_sp
->s_log_zone_size
;
314 base_block
= (block_t
) z
<< scale
;
315 zone_size
= (zone_t
) rip
->i_sp
->s_block_size
<< scale
;
316 b
= base_block
+ (block_t
)((position
% zone_size
)/rip
->i_sp
->s_block_size
);
319 bp
= get_block(rip
->i_dev
, b
, NO_READ
);
325 /*===========================================================================*
327 *===========================================================================*/
329 register struct buf
*bp
; /* pointer to buffer to zero */
332 ASSERT(lmfs_bytes(bp
) > 0);
334 memset(b_data(bp
), 0, (size_t) lmfs_bytes(bp
));