. service tells you which device it couldn't stat
[minix3.git] / servers / mfs / write.c
blobc51cb0c79c0cdc19e8fd591512539fc7b06adca9
2 /* This file is the counterpart of "read.c". It contains the code for writing
3 * insofar as this is not contained in read_write().
5 * The entry points into this file are
6 * do_write: call read_write to perform the WRITE system call
7 * clear_zone: erase a zone in the middle of a file
8 * new_block: acquire a new block
9 */
11 #include "fs.h"
12 #include <string.h>
13 #include "buf.h"
14 #include "inode.h"
15 #include "super.h"
18 FORWARD _PROTOTYPE( void wr_indir, (struct buf *bp, int index, zone_t zone) );
19 FORWARD _PROTOTYPE( int empty_indir, (struct buf *, struct super_block *) );
22 /*===========================================================================*
23 * write_map *
24 *===========================================================================*/
25 PUBLIC int write_map(rip, position, new_zone, op)
26 struct inode *rip; /* pointer to inode to be changed */
27 off_t position; /* file address to be mapped */
28 zone_t new_zone; /* zone # to be inserted */
29 int op; /* special actions */
31 /* Write a new zone into an inode.
33 * If op includes WMAP_FREE, free the data zone corresponding to that position
34 * in the inode ('new_zone' is ignored then). Also free the indirect block
35 * if that was the last entry in the indirect block.
36 * Also free the double indirect block if that was the last entry in the
37 * double indirect block.
39 int scale, ind_ex = 0, new_ind, new_dbl,
40 zones, nr_indirects, single, zindex, ex;
41 zone_t z, z1, z2 = NO_ZONE, old_zone;
42 register block_t b;
43 long excess, zone;
44 struct buf *bp_dindir = NIL_BUF, *bp = NIL_BUF;
46 rip->i_dirt = DIRTY; /* inode will be changed */
47 scale = rip->i_sp->s_log_zone_size; /* for zone-block conversion */
48 /* relative zone # to insert */
49 zone = (position/rip->i_sp->s_block_size) >> scale;
50 zones = rip->i_ndzones; /* # direct zones in the inode */
51 nr_indirects = rip->i_nindirs;/* # indirect zones per indirect block */
53 /* Is 'position' to be found in the inode itself? */
54 if (zone < zones) {
55 zindex = (int) zone; /* we need an integer here */
56 if(rip->i_zone[zindex] != NO_ZONE && (op & WMAP_FREE)) {
57 free_zone(rip->i_dev, rip->i_zone[zindex]);
58 rip->i_zone[zindex] = NO_ZONE;
59 } else {
60 rip->i_zone[zindex] = new_zone;
62 return(OK);
65 /* It is not in the inode, so it must be single or double indirect. */
66 excess = zone - zones; /* first Vx_NR_DZONES don't count */
67 new_ind = FALSE;
68 new_dbl = FALSE;
70 if (excess < nr_indirects) {
71 /* 'position' can be located via the single indirect block. */
72 z1 = rip->i_zone[zones]; /* single indirect zone */
73 single = TRUE;
74 } else {
75 /* 'position' can be located via the double indirect block. */
76 if ( (z2 = z = rip->i_zone[zones+1]) == NO_ZONE &&
77 !(op & WMAP_FREE)) {
78 /* Create the double indirect block. */
79 if ( (z = alloc_zone(rip->i_dev, rip->i_zone[0])) == NO_ZONE)
80 return(err_code);
81 rip->i_zone[zones+1] = z;
82 new_dbl = TRUE; /* set flag for later */
85 /* 'z' is zone number for double indirect block, either old
86 * or newly created.
87 * If there wasn't one and WMAP_FREE is set, 'z' is NO_ZONE.
89 excess -= nr_indirects; /* single indirect doesn't count */
90 ind_ex = (int) (excess / nr_indirects);
91 excess = excess % nr_indirects;
92 if (ind_ex >= nr_indirects) return(EFBIG);
94 if(z == NO_ZONE) {
95 /* WMAP_FREE and no double indirect block - then no
96 * single indirect block either.
98 z1 = NO_ZONE;
99 } else {
100 b = (block_t) z << scale;
101 bp_dindir = get_block(rip->i_dev, b, (new_dbl?NO_READ:NORMAL));
102 if (new_dbl) zero_block(bp_dindir);
103 z1 = rd_indir(bp_dindir, ind_ex);
105 single = FALSE;
108 /* z1 is now single indirect zone, or NO_ZONE; 'excess' is index.
109 * We have to create the indirect zone if it's NO_ZONE. Unless
110 * we're freeing (WMAP_FREE).
112 if (z1 == NO_ZONE && !(op & WMAP_FREE)) {
113 z1 = alloc_zone(rip->i_dev, rip->i_zone[0]);
114 if (single)
115 rip->i_zone[zones] = z1; /* update inode w. single indirect */
116 else
117 wr_indir(bp_dindir, ind_ex, z1); /* update dbl indir */
119 new_ind = TRUE;
120 /* If double ind, it is dirty. */
121 if (bp_dindir != NIL_BUF) bp_dindir->b_dirt = DIRTY;
122 if (z1 == NO_ZONE) {
123 /* Release dbl indirect blk. */
124 put_block(bp_dindir, INDIRECT_BLOCK);
125 return(err_code); /* couldn't create single ind */
129 /* z1 is indirect block's zone number (unless it's NO_ZONE when we're
130 * freeing).
132 if(z1 != NO_ZONE) {
133 ex = (int) excess; /* we need an int here */
134 b = (block_t) z1 << scale;
135 bp = get_block(rip->i_dev, b, (new_ind ? NO_READ : NORMAL) );
136 if (new_ind) zero_block(bp);
137 if(op & WMAP_FREE) {
138 if((old_zone = rd_indir(bp, ex)) != NO_ZONE) {
139 free_zone(rip->i_dev, old_zone);
140 wr_indir(bp, ex, NO_ZONE);
143 /* Last reference in the indirect block gone? Then
144 * Free the indirect block.
146 if(empty_indir(bp, rip->i_sp)) {
147 free_zone(rip->i_dev, z1);
148 z1 = NO_ZONE;
149 /* Update the reference to the indirect block to
150 * NO_ZONE - in the double indirect block if there
151 * is one, otherwise in the inode directly.
153 if(single) {
154 rip->i_zone[zones] = z1;
155 } else {
156 wr_indir(bp_dindir, ind_ex, z1);
157 bp_dindir->b_dirt = DIRTY;
160 } else {
161 wr_indir(bp, ex, new_zone);
163 bp->b_dirt = DIRTY;
164 put_block(bp, INDIRECT_BLOCK);
167 /* If the single indirect block isn't there (or was just freed),
168 * see if we have to keep the double indirect block.
170 if(z1 == NO_ZONE && !single && empty_indir(bp_dindir, rip->i_sp) &&
171 z2 != NO_ZONE) {
172 free_zone(rip->i_dev, z2);
173 rip->i_zone[zones+1] = NO_ZONE;
176 put_block(bp_dindir, INDIRECT_BLOCK); /* release double indirect blk */
178 return(OK);
181 /*===========================================================================*
182 * wr_indir *
183 *===========================================================================*/
184 PRIVATE void wr_indir(bp, index, zone)
185 struct buf *bp; /* pointer to indirect block */
186 int index; /* index into *bp */
187 zone_t zone; /* zone to write */
189 /* Given a pointer to an indirect block, write one entry. */
191 struct super_block *sp;
193 if(bp == NIL_BUF)
194 panic(__FILE__, "wr_indir() on NIL_BUF", NO_NUM);
196 sp = get_super(bp->b_dev); /* need super block to find file sys type */
198 /* write a zone into an indirect block */
199 if (sp->s_version == V1)
200 bp->b_v1_ind[index] = (zone1_t) conv2(sp->s_native, (int) zone);
201 else
202 bp->b_v2_ind[index] = (zone_t) conv4(sp->s_native, (long) zone);
205 /*===========================================================================*
206 * empty_indir *
207 *===========================================================================*/
208 PRIVATE int empty_indir(bp, sb)
209 struct buf *bp; /* pointer to indirect block */
210 struct super_block *sb; /* superblock of device block resides on */
212 /* Return nonzero if the indirect block pointed to by bp contains
213 * only NO_ZONE entries.
215 int i;
216 if(sb->s_version == V1) {
217 for(i = 0; i < V1_INDIRECTS; i++)
218 if(bp->b_v1_ind[i] != NO_ZONE)
219 return 0;
220 } else {
221 for(i = 0; i < V2_INDIRECTS(sb->s_block_size); i++)
222 if(bp->b_v2_ind[i] != NO_ZONE)
223 return 0;
226 return 1;
229 /*===========================================================================*
230 * clear_zone *
231 *===========================================================================*/
232 PUBLIC void clear_zone(rip, pos, flag)
233 register struct inode *rip; /* inode to clear */
234 off_t pos; /* points to block to clear */
235 int flag; /* 0 if called by read_write, 1 by new_block */
237 /* Zero a zone, possibly starting in the middle. The parameter 'pos' gives
238 * a byte in the first block to be zeroed. Clearzone() is called from
239 * read_write and new_block().
242 register struct buf *bp;
243 register block_t b, blo, bhi;
244 register off_t next;
245 register int scale;
246 register zone_t 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 = (zone_t) rip->i_sp->s_block_size << scale;
253 if (flag == 1) pos = (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 = ( ((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);
264 zero_block(bp);
265 put_block(bp, FULL_DATA_BLOCK);
269 /*===========================================================================*
270 * new_block *
271 *===========================================================================*/
272 PUBLIC struct buf *new_block(rip, position)
273 register struct inode *rip; /* pointer to inode */
274 off_t position; /* file pointer */
276 /* Acquire a new block and return a pointer to it. Doing so may require
277 * allocating a complete zone, and then returning the initial block.
278 * On the other hand, the current zone may still have some unused blocks.
281 register struct buf *bp;
282 block_t b, base_block;
283 zone_t z;
284 zone_t zone_size;
285 int scale, r;
286 struct super_block *sp;
288 /* Is another block available in the current zone? */
289 if ( (b = read_map(rip, position)) == NO_BLOCK) {
290 /* Choose first zone if possible. */
291 /* Lose if the file is nonempty but the first zone number is NO_ZONE
292 * corresponding to a zone full of zeros. It would be better to
293 * search near the last real zone.
295 if (rip->i_zone[0] == NO_ZONE) {
296 sp = rip->i_sp;
297 z = sp->s_firstdatazone;
298 } else {
299 z = rip->i_zone[0]; /* hunt near first zone */
301 if ( (z = alloc_zone(rip->i_dev, z)) == NO_ZONE) return(NIL_BUF);
302 if ( (r = write_map(rip, position, z, 0)) != OK) {
303 free_zone(rip->i_dev, z);
304 err_code = r;
305 return(NIL_BUF);
308 /* If we are not writing at EOF, clear the zone, just to be safe. */
309 if ( position != rip->i_size) clear_zone(rip, position, 1);
310 scale = rip->i_sp->s_log_zone_size;
311 base_block = (block_t) z << scale;
312 zone_size = (zone_t) rip->i_sp->s_block_size << scale;
313 b = base_block + (block_t)((position % zone_size)/rip->i_sp->s_block_size);
316 bp = get_block(rip->i_dev, b, NO_READ);
317 zero_block(bp);
318 return(bp);
321 /*===========================================================================*
322 * zero_block *
323 *===========================================================================*/
324 PUBLIC void zero_block(bp)
325 register struct buf *bp; /* pointer to buffer to zero */
327 /* Zero a block. */
328 memset(bp->b_data, 0, _MAX_BLOCK_SIZE);
329 bp->b_dirt = DIRTY;