source: trunk/minix/servers/fs/write.c@ 20

Last change on this file since 20 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 10.7 KB
Line 
1/* This file is the counterpart of "read.c". It contains the code for writing
2 * insofar as this is not contained in read_write().
3 *
4 * The entry points into this file are
5 * do_write: call read_write to perform the WRITE system call
6 * clear_zone: erase a zone in the middle of a file
7 * new_block: acquire a new block
8 */
9
10#include "fs.h"
11#include <string.h>
12#include "buf.h"
13#include "file.h"
14#include "fproc.h"
15#include "inode.h"
16#include "super.h"
17
18FORWARD _PROTOTYPE( void wr_indir, (struct buf *bp, int index, zone_t zone) );
19FORWARD _PROTOTYPE( int empty_indir, (struct buf *, struct super_block *) );
20
21/*===========================================================================*
22 * do_write *
23 *===========================================================================*/
24PUBLIC int do_write()
25{
26/* Perform the write(fd, buffer, nbytes) system call. */
27
28 return(read_write(WRITING));
29}
30
31/*===========================================================================*
32 * write_map *
33 *===========================================================================*/
34PUBLIC int write_map(rip, position, new_zone, op)
35struct inode *rip; /* pointer to inode to be changed */
36off_t position; /* file address to be mapped */
37zone_t new_zone; /* zone # to be inserted */
38int op; /* special actions */
39{
40/* Write a new zone into an inode.
41 *
42 * If op includes WMAP_FREE, free the data zone corresponding to that position
43 * in the inode ('new_zone' is ignored then). Also free the indirect block
44 * if that was the last entry in the indirect block.
45 * Also free the double indirect block if that was the last entry in the
46 * double indirect block.
47 */
48 int scale, ind_ex, new_ind, new_dbl, zones, nr_indirects, single, zindex, ex;
49 zone_t z, z1, z2 = NO_ZONE, old_zone;
50 register block_t b;
51 long excess, zone;
52 struct buf *bp_dindir = NIL_BUF, *bp = NIL_BUF;
53
54 rip->i_dirt = DIRTY; /* inode will be changed */
55 scale = rip->i_sp->s_log_zone_size; /* for zone-block conversion */
56 /* relative zone # to insert */
57 zone = (position/rip->i_sp->s_block_size) >> scale;
58 zones = rip->i_ndzones; /* # direct zones in the inode */
59 nr_indirects = rip->i_nindirs;/* # indirect zones per indirect block */
60
61 /* Is 'position' to be found in the inode itself? */
62 if (zone < zones) {
63 zindex = (int) zone; /* we need an integer here */
64 if(rip->i_zone[zindex] != NO_ZONE && (op & WMAP_FREE)) {
65 free_zone(rip->i_dev, rip->i_zone[zindex]);
66 rip->i_zone[zindex] = NO_ZONE;
67 } else {
68 rip->i_zone[zindex] = new_zone;
69 }
70 return(OK);
71 }
72
73 /* It is not in the inode, so it must be single or double indirect. */
74 excess = zone - zones; /* first Vx_NR_DZONES don't count */
75 new_ind = FALSE;
76 new_dbl = FALSE;
77
78 if (excess < nr_indirects) {
79 /* 'position' can be located via the single indirect block. */
80 z1 = rip->i_zone[zones]; /* single indirect zone */
81 single = TRUE;
82 } else {
83 /* 'position' can be located via the double indirect block. */
84 if ( (z2 = z = rip->i_zone[zones+1]) == NO_ZONE &&
85 !(op & WMAP_FREE)) {
86 /* Create the double indirect block. */
87 if ( (z = alloc_zone(rip->i_dev, rip->i_zone[0])) == NO_ZONE)
88 return(err_code);
89 rip->i_zone[zones+1] = z;
90 new_dbl = TRUE; /* set flag for later */
91 }
92
93 /* 'z' is zone number for double indirect block, either old
94 * or newly created.
95 * If there wasn't one and WMAP_FREE is set, 'z' is NO_ZONE.
96 */
97 excess -= nr_indirects; /* single indirect doesn't count */
98 ind_ex = (int) (excess / nr_indirects);
99 excess = excess % nr_indirects;
100 if (ind_ex >= nr_indirects) return(EFBIG);
101
102 if(z == NO_ZONE) {
103 /* WMAP_FREE and no double indirect block - then no
104 * single indirect block either.
105 */
106 z1 = NO_ZONE;
107 } else {
108 b = (block_t) z << scale;
109 bp_dindir = get_block(rip->i_dev, b, (new_dbl?NO_READ:NORMAL));
110 if (new_dbl) zero_block(bp_dindir);
111 z1 = rd_indir(bp_dindir, ind_ex);
112 }
113 single = FALSE;
114 }
115
116 /* z1 is now single indirect zone, or NO_ZONE; 'excess' is index.
117 * We have to create the indirect zone if it's NO_ZONE. Unless
118 * we're freeing (WMAP_FREE).
119 */
120 if (z1 == NO_ZONE && !(op & WMAP_FREE)) {
121 z1 = alloc_zone(rip->i_dev, rip->i_zone[0]);
122 if (single)
123 rip->i_zone[zones] = z1; /* update inode w. single indirect */
124 else
125 wr_indir(bp_dindir, ind_ex, z1); /* update dbl indir */
126
127 new_ind = TRUE;
128 /* If double ind, it is dirty. */
129 if (bp_dindir != NIL_BUF) bp_dindir->b_dirt = DIRTY;
130 if (z1 == NO_ZONE) {
131 /* Release dbl indirect blk. */
132 put_block(bp_dindir, INDIRECT_BLOCK);
133 return(err_code); /* couldn't create single ind */
134 }
135 }
136
137 /* z1 is indirect block's zone number (unless it's NO_ZONE when we're
138 * freeing).
139 */
140 if(z1 != NO_ZONE) {
141 ex = (int) excess; /* we need an int here */
142 b = (block_t) z1 << scale;
143 bp = get_block(rip->i_dev, b, (new_ind ? NO_READ : NORMAL) );
144 if (new_ind) zero_block(bp);
145 if(op & WMAP_FREE) {
146 if((old_zone = rd_indir(bp, ex)) != NO_ZONE) {
147 free_zone(rip->i_dev, old_zone);
148 wr_indir(bp, ex, NO_ZONE);
149 }
150
151 /* Last reference in the indirect block gone? Then
152 * Free the indirect block.
153 */
154 if(empty_indir(bp, rip->i_sp)) {
155 free_zone(rip->i_dev, z1);
156 z1 = NO_ZONE;
157 /* Update the reference to the indirect block to
158 * NO_ZONE - in the double indirect block if there
159 * is one, otherwise in the inode directly.
160 */
161 if(single) {
162 rip->i_zone[zones] = z1;
163 } else {
164 wr_indir(bp_dindir, ind_ex, z1);
165 bp_dindir->b_dirt = DIRTY;
166 }
167 }
168 } else {
169 wr_indir(bp, ex, new_zone);
170 }
171 bp->b_dirt = DIRTY;
172 put_block(bp, INDIRECT_BLOCK);
173 }
174
175 /* If the single indirect block isn't there (or was just freed),
176 * see if we have to keep the double indirect block.
177 */
178 if(z1 == NO_ZONE && !single && empty_indir(bp_dindir, rip->i_sp) &&
179 z2 != NO_ZONE) {
180 free_zone(rip->i_dev, z2);
181 rip->i_zone[zones+1] = NO_ZONE;
182 }
183
184 put_block(bp_dindir, INDIRECT_BLOCK); /* release double indirect blk */
185
186 return(OK);
187}
188
189/*===========================================================================*
190 * wr_indir *
191 *===========================================================================*/
192PRIVATE void wr_indir(bp, index, zone)
193struct buf *bp; /* pointer to indirect block */
194int index; /* index into *bp */
195zone_t zone; /* zone to write */
196{
197/* Given a pointer to an indirect block, write one entry. */
198
199 struct super_block *sp;
200
201 if(bp == NIL_BUF)
202 panic(__FILE__, "wr_indir() on NIL_BUF", NO_NUM);
203
204 sp = get_super(bp->b_dev); /* need super block to find file sys type */
205
206 /* write a zone into an indirect block */
207 if (sp->s_version == V1)
208 bp->b_v1_ind[index] = (zone1_t) conv2(sp->s_native, (int) zone);
209 else
210 bp->b_v2_ind[index] = (zone_t) conv4(sp->s_native, (long) zone);
211}
212
213/*===========================================================================*
214 * empty_indir *
215 *===========================================================================*/
216PRIVATE int empty_indir(bp, sb)
217struct buf *bp; /* pointer to indirect block */
218struct super_block *sb; /* superblock of device block resides on */
219{
220/* Return nonzero if the indirect block pointed to by bp contains
221 * only NO_ZONE entries.
222 */
223 int i;
224 if(sb->s_version == V1) {
225 for(i = 0; i < V1_INDIRECTS; i++)
226 if(bp->b_v1_ind[i] != NO_ZONE)
227 return 0;
228 } else {
229 for(i = 0; i < V2_INDIRECTS(sb->s_block_size); i++)
230 if(bp->b_v2_ind[i] != NO_ZONE)
231 return 0;
232 }
233
234 return 1;
235}
236
237/*===========================================================================*
238 * clear_zone *
239 *===========================================================================*/
240PUBLIC void clear_zone(rip, pos, flag)
241register struct inode *rip; /* inode to clear */
242off_t pos; /* points to block to clear */
243int flag; /* 0 if called by read_write, 1 by new_block */
244{
245/* Zero a zone, possibly starting in the middle. The parameter 'pos' gives
246 * a byte in the first block to be zeroed. Clearzone() is called from
247 * read_write and new_block().
248 */
249
250 register struct buf *bp;
251 register block_t b, blo, bhi;
252 register off_t next;
253 register int scale;
254 register zone_t zone_size;
255
256 /* If the block size and zone size are the same, clear_zone() not needed. */
257 scale = rip->i_sp->s_log_zone_size;
258 if (scale == 0) return;
259
260 zone_size = (zone_t) rip->i_sp->s_block_size << scale;
261 if (flag == 1) pos = (pos/zone_size) * zone_size;
262 next = pos + rip->i_sp->s_block_size - 1;
263
264 /* If 'pos' is in the last block of a zone, do not clear the zone. */
265 if (next/zone_size != pos/zone_size) return;
266 if ( (blo = read_map(rip, next)) == NO_BLOCK) return;
267 bhi = ( ((blo>>scale)+1) << scale) - 1;
268
269 /* Clear all the blocks between 'blo' and 'bhi'. */
270 for (b = blo; b <= bhi; b++) {
271 bp = get_block(rip->i_dev, b, NO_READ);
272 zero_block(bp);
273 put_block(bp, FULL_DATA_BLOCK);
274 }
275}
276
277/*===========================================================================*
278 * new_block *
279 *===========================================================================*/
280PUBLIC struct buf *new_block(rip, position)
281register struct inode *rip; /* pointer to inode */
282off_t position; /* file pointer */
283{
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.
287 */
288
289 register struct buf *bp;
290 block_t b, base_block;
291 zone_t z;
292 zone_t zone_size;
293 int scale, r;
294 struct super_block *sp;
295
296 /* Is another block available in the current zone? */
297 if ( (b = read_map(rip, position)) == NO_BLOCK) {
298 /* Choose first zone if possible. */
299 /* Lose if the file is nonempty but the first zone number is NO_ZONE
300 * corresponding to a zone full of zeros. It would be better to
301 * search near the last real zone.
302 */
303 if (rip->i_zone[0] == NO_ZONE) {
304 sp = rip->i_sp;
305 z = sp->s_firstdatazone;
306 } else {
307 z = rip->i_zone[0]; /* hunt near first zone */
308 }
309 if ( (z = alloc_zone(rip->i_dev, z)) == NO_ZONE) return(NIL_BUF);
310 if ( (r = write_map(rip, position, z, 0)) != OK) {
311 free_zone(rip->i_dev, z);
312 err_code = r;
313 return(NIL_BUF);
314 }
315
316 /* If we are not writing at EOF, clear the zone, just to be safe. */
317 if ( position != rip->i_size) clear_zone(rip, position, 1);
318 scale = rip->i_sp->s_log_zone_size;
319 base_block = (block_t) z << scale;
320 zone_size = (zone_t) rip->i_sp->s_block_size << scale;
321 b = base_block + (block_t)((position % zone_size)/rip->i_sp->s_block_size);
322 }
323
324 bp = get_block(rip->i_dev, b, NO_READ);
325 zero_block(bp);
326 return(bp);
327}
328
329/*===========================================================================*
330 * zero_block *
331 *===========================================================================*/
332PUBLIC void zero_block(bp)
333register struct buf *bp; /* pointer to buffer to zero */
334{
335/* Zero a block. */
336 memset(bp->b_data, 0, _MAX_BLOCK_SIZE);
337 bp->b_dirt = DIRTY;
338}
Note: See TracBrowser for help on using the repository browser.