xref: /minix3/minix/fs/mfs/read.c (revision 4472b590c74fed661146a925d8dff2ed1b04256e)
1433d6423SLionel Sambuc #include "fs.h"
2433d6423SLionel Sambuc #include <stddef.h>
3433d6423SLionel Sambuc #include <string.h>
4433d6423SLionel Sambuc #include <stdlib.h>
5433d6423SLionel Sambuc #include "buf.h"
6433d6423SLionel Sambuc #include "inode.h"
7433d6423SLionel Sambuc #include "super.h"
8433d6423SLionel Sambuc #include <sys/param.h>
90314acfbSDavid van Moolenbroek #include <sys/dirent.h>
10433d6423SLionel Sambuc #include <assert.h>
11433d6423SLionel Sambuc 
12433d6423SLionel Sambuc 
13433d6423SLionel Sambuc static struct buf *rahead(struct inode *rip, block_t baseblock, u64_t
14433d6423SLionel Sambuc 	position, unsigned bytes_ahead);
15433d6423SLionel Sambuc static int rw_chunk(struct inode *rip, u64_t position, unsigned off,
16ccaeedb2SDavid van Moolenbroek 	size_t chunk, unsigned left, int call, struct fsdriver_data *data,
17ccaeedb2SDavid van Moolenbroek 	unsigned buf_off, unsigned int block_size, int *completed);
18433d6423SLionel Sambuc 
19433d6423SLionel Sambuc 
20433d6423SLionel Sambuc /*===========================================================================*
21433d6423SLionel Sambuc  *				fs_readwrite				     *
22433d6423SLionel Sambuc  *===========================================================================*/
fs_readwrite(ino_t ino_nr,struct fsdriver_data * data,size_t nrbytes,off_t position,int call)23ccaeedb2SDavid van Moolenbroek ssize_t fs_readwrite(ino_t ino_nr, struct fsdriver_data *data, size_t nrbytes,
24ccaeedb2SDavid van Moolenbroek 	off_t position, int call)
25433d6423SLionel Sambuc {
26ccaeedb2SDavid van Moolenbroek   int r;
27433d6423SLionel Sambuc   int regular;
28ccaeedb2SDavid van Moolenbroek   off_t f_size, bytes_left;
29ccaeedb2SDavid van Moolenbroek   size_t off, cum_io, block_size, chunk;
30433d6423SLionel Sambuc   mode_t mode_word;
31433d6423SLionel Sambuc   int completed;
32433d6423SLionel Sambuc   struct inode *rip;
33433d6423SLionel Sambuc 
34433d6423SLionel Sambuc   r = OK;
35433d6423SLionel Sambuc 
36433d6423SLionel Sambuc   /* Find the inode referred */
37ccaeedb2SDavid van Moolenbroek   if ((rip = find_inode(fs_dev, ino_nr)) == NULL)
38433d6423SLionel Sambuc 	return(EINVAL);
39433d6423SLionel Sambuc 
40433d6423SLionel Sambuc   mode_word = rip->i_mode & I_TYPE;
41ccaeedb2SDavid van Moolenbroek   regular = (mode_word == I_REGULAR);
42433d6423SLionel Sambuc 
43433d6423SLionel Sambuc   /* Determine blocksize */
44433d6423SLionel Sambuc   block_size = rip->i_sp->s_block_size;
45433d6423SLionel Sambuc   f_size = rip->i_size;
46433d6423SLionel Sambuc 
47433d6423SLionel Sambuc   /* If this is file i/o, check we can write */
48ccaeedb2SDavid van Moolenbroek   if (call == FSC_WRITE) {
49433d6423SLionel Sambuc   	  if(rip->i_sp->s_rd_only)
50433d6423SLionel Sambuc 		  return EROFS;
51433d6423SLionel Sambuc 
52433d6423SLionel Sambuc 	  /* Check in advance to see if file will grow too big. */
53433d6423SLionel Sambuc 	  if (position > (off_t) (rip->i_sp->s_max_size - nrbytes))
54433d6423SLionel Sambuc 		  return(EFBIG);
55433d6423SLionel Sambuc 
56433d6423SLionel Sambuc 	  /* Clear the zone containing present EOF if hole about
57433d6423SLionel Sambuc 	   * to be created.  This is necessary because all unwritten
58433d6423SLionel Sambuc 	   * blocks prior to the EOF must read as zeros.
59433d6423SLionel Sambuc 	   */
60433d6423SLionel Sambuc 	  if(position > f_size) clear_zone(rip, f_size, 0);
61433d6423SLionel Sambuc   }
62433d6423SLionel Sambuc 
63433d6423SLionel Sambuc   cum_io = 0;
64433d6423SLionel Sambuc   /* Split the transfer into chunks that don't span two blocks. */
65433d6423SLionel Sambuc   while (nrbytes > 0) {
66433d6423SLionel Sambuc 	  off = ((unsigned int) position) % block_size; /* offset in blk*/
67ccaeedb2SDavid van Moolenbroek 	  chunk = block_size - off;
68ccaeedb2SDavid van Moolenbroek 	  if (chunk > nrbytes)
69ccaeedb2SDavid van Moolenbroek 		chunk = nrbytes;
70433d6423SLionel Sambuc 
71f53651deSBen Gras 	  if (call != FSC_WRITE) {
72433d6423SLionel Sambuc 		  bytes_left = f_size - position;
73433d6423SLionel Sambuc 		  if (position >= f_size) break;	/* we are beyond EOF */
74433d6423SLionel Sambuc 		  if (chunk > (unsigned int) bytes_left) chunk = bytes_left;
75433d6423SLionel Sambuc 	  }
76433d6423SLionel Sambuc 
77433d6423SLionel Sambuc 	  /* Read or write 'chunk' bytes. */
78433d6423SLionel Sambuc 	  r = rw_chunk(rip, ((u64_t)((unsigned long)position)), off, chunk,
79ccaeedb2SDavid van Moolenbroek 		nrbytes, call, data, cum_io, block_size, &completed);
80433d6423SLionel Sambuc 
816c46a77dSDavid van Moolenbroek 	  if (r != OK) break;
82433d6423SLionel Sambuc 
83433d6423SLionel Sambuc 	  /* Update counters and pointers. */
84433d6423SLionel Sambuc 	  nrbytes -= chunk;	/* bytes yet to be read */
85433d6423SLionel Sambuc 	  cum_io += chunk;	/* bytes read so far */
86433d6423SLionel Sambuc 	  position += (off_t) chunk;	/* position within the file */
87433d6423SLionel Sambuc   }
88433d6423SLionel Sambuc 
89433d6423SLionel Sambuc   /* On write, update file size and access time. */
90ccaeedb2SDavid van Moolenbroek   if (call == FSC_WRITE) {
91433d6423SLionel Sambuc 	  if (regular || mode_word == I_DIRECTORY) {
92433d6423SLionel Sambuc 		  if (position > f_size) rip->i_size = position;
93433d6423SLionel Sambuc 	  }
94433d6423SLionel Sambuc   }
95433d6423SLionel Sambuc 
96433d6423SLionel Sambuc   rip->i_seek = NO_SEEK;
97433d6423SLionel Sambuc 
98ccaeedb2SDavid van Moolenbroek   if (r != OK)
99ccaeedb2SDavid van Moolenbroek 	return r;
100ccaeedb2SDavid van Moolenbroek 
101433d6423SLionel Sambuc   /* even on a ROFS, writing to a device node on it is fine,
102433d6423SLionel Sambuc    * just don't update the inode stats for it. And dito for reading.
103433d6423SLionel Sambuc    */
104ccaeedb2SDavid van Moolenbroek   if (!rip->i_sp->s_rd_only) {
105ccaeedb2SDavid van Moolenbroek 	  if (call == FSC_READ) rip->i_update |= ATIME;
106ccaeedb2SDavid van Moolenbroek 	  if (call == FSC_WRITE) rip->i_update |= CTIME | MTIME;
107433d6423SLionel Sambuc 	  IN_MARKDIRTY(rip);		/* inode is thus now dirty */
108433d6423SLionel Sambuc   }
109433d6423SLionel Sambuc 
110ccaeedb2SDavid van Moolenbroek   return cum_io;
111433d6423SLionel Sambuc }
112433d6423SLionel Sambuc 
113433d6423SLionel Sambuc 
114433d6423SLionel Sambuc /*===========================================================================*
115433d6423SLionel Sambuc  *				rw_chunk				     *
116433d6423SLionel Sambuc  *===========================================================================*/
rw_chunk(rip,position,off,chunk,left,call,data,buf_off,block_size,completed)117ccaeedb2SDavid van Moolenbroek static int rw_chunk(rip, position, off, chunk, left, call, data, buf_off,
118ccaeedb2SDavid van Moolenbroek 	block_size, completed)
119433d6423SLionel Sambuc register struct inode *rip;	/* pointer to inode for file to be rd/wr */
120433d6423SLionel Sambuc u64_t position;			/* position within file to read or write */
121433d6423SLionel Sambuc unsigned off;			/* off within the current block */
122ccaeedb2SDavid van Moolenbroek size_t chunk;			/* number of bytes to read or write */
123433d6423SLionel Sambuc unsigned left;			/* max number of bytes wanted after position */
124ccaeedb2SDavid van Moolenbroek int call;			/* FSC_READ, FSC_WRITE, or FSC_PEEK */
125ccaeedb2SDavid van Moolenbroek struct fsdriver_data *data;	/* structure for (remote) user buffer */
126ccaeedb2SDavid van Moolenbroek unsigned buf_off;		/* offset in user buffer */
127433d6423SLionel Sambuc unsigned int block_size;	/* block size of FS operating on */
128433d6423SLionel Sambuc int *completed;			/* number of bytes copied */
129433d6423SLionel Sambuc {
130433d6423SLionel Sambuc /* Read or write (part of) a block. */
1316c46a77dSDavid van Moolenbroek   struct buf *bp = NULL;
132433d6423SLionel Sambuc   register int r = OK;
133ccaeedb2SDavid van Moolenbroek   int n;
134433d6423SLionel Sambuc   block_t b;
135433d6423SLionel Sambuc   dev_t dev;
136433d6423SLionel Sambuc   ino_t ino = VMC_NO_INODE;
137433d6423SLionel Sambuc   u64_t ino_off = rounddown(position, block_size);
138433d6423SLionel Sambuc 
139433d6423SLionel Sambuc   *completed = 0;
140433d6423SLionel Sambuc 
141433d6423SLionel Sambuc   if (ex64hi(position) != 0)
142433d6423SLionel Sambuc 	panic("rw_chunk: position too high");
143433d6423SLionel Sambuc   b = read_map(rip, (off_t) ex64lo(position), 0);
144433d6423SLionel Sambuc   dev = rip->i_dev;
145433d6423SLionel Sambuc   ino = rip->i_num;
146433d6423SLionel Sambuc   assert(ino != VMC_NO_INODE);
147433d6423SLionel Sambuc 
148ccaeedb2SDavid van Moolenbroek   if (b == NO_BLOCK) {
149ccaeedb2SDavid van Moolenbroek 	if (call == FSC_READ) {
150433d6423SLionel Sambuc 		/* Reading from a nonexistent block.  Must read as all zeros.*/
151ccaeedb2SDavid van Moolenbroek 		r = fsdriver_zero(data, buf_off, chunk);
152433d6423SLionel Sambuc 		if(r != OK) {
153ccaeedb2SDavid van Moolenbroek 			printf("MFS: fsdriver_zero failed\n");
154433d6423SLionel Sambuc 		}
155433d6423SLionel Sambuc 		return r;
156d75faf18SDavid van Moolenbroek 	} else if (call == FSC_PEEK) {
157d75faf18SDavid van Moolenbroek 		/* Peeking a nonexistent block. Report to VM. */
158d75faf18SDavid van Moolenbroek 		lmfs_zero_block_ino(dev, ino, ino_off);
159d75faf18SDavid van Moolenbroek 		return OK;
160433d6423SLionel Sambuc 	} else {
161d75faf18SDavid van Moolenbroek 		/* Writing to a nonexistent block.
162433d6423SLionel Sambuc 		 * Create and enter in inode.
163433d6423SLionel Sambuc 		 */
164433d6423SLionel Sambuc 		if ((bp = new_block(rip, (off_t) ex64lo(position))) == NULL)
165433d6423SLionel Sambuc 			return(err_code);
166433d6423SLionel Sambuc 	}
167ccaeedb2SDavid van Moolenbroek   } else if (call != FSC_WRITE) {
168433d6423SLionel Sambuc 	/* Read and read ahead if convenient. */
169433d6423SLionel Sambuc 	bp = rahead(rip, b, position, left);
170433d6423SLionel Sambuc   } else {
171433d6423SLionel Sambuc 	/* Normally an existing block to be partially overwritten is first read
172433d6423SLionel Sambuc 	 * in.  However, a full block need not be read in.  If it is already in
173433d6423SLionel Sambuc 	 * the cache, acquire it, otherwise just acquire a free buffer.
174433d6423SLionel Sambuc 	 */
175433d6423SLionel Sambuc 	n = (chunk == block_size ? NO_READ : NORMAL);
176ccaeedb2SDavid van Moolenbroek 	if (off == 0 && (off_t) ex64lo(position) >= rip->i_size)
177433d6423SLionel Sambuc 		n = NO_READ;
178433d6423SLionel Sambuc 	assert(ino != VMC_NO_INODE);
179433d6423SLionel Sambuc 	assert(!(ino_off % block_size));
1806c46a77dSDavid van Moolenbroek 	if ((r = lmfs_get_block_ino(&bp, dev, b, n, ino, ino_off)) != OK)
1816c46a77dSDavid van Moolenbroek 		panic("MFS: error getting block (%llu,%u): %d", dev, b, r);
182433d6423SLionel Sambuc   }
183433d6423SLionel Sambuc 
184433d6423SLionel Sambuc   /* In all cases, bp now points to a valid buffer. */
185433d6423SLionel Sambuc   assert(bp != NULL);
186433d6423SLionel Sambuc 
187ccaeedb2SDavid van Moolenbroek   if (call == FSC_WRITE && chunk != block_size &&
188433d6423SLionel Sambuc       (off_t) ex64lo(position) >= rip->i_size && off == 0) {
189433d6423SLionel Sambuc 	zero_block(bp);
190433d6423SLionel Sambuc   }
191433d6423SLionel Sambuc 
192ccaeedb2SDavid van Moolenbroek   if (call == FSC_READ) {
193433d6423SLionel Sambuc 	/* Copy a chunk from the block buffer to user space. */
194ccaeedb2SDavid van Moolenbroek 	r = fsdriver_copyout(data, buf_off, b_data(bp)+off, chunk);
195ccaeedb2SDavid van Moolenbroek   } else if (call == FSC_WRITE) {
196433d6423SLionel Sambuc 	/* Copy a chunk from user space to the block buffer. */
197ccaeedb2SDavid van Moolenbroek 	r = fsdriver_copyin(data, buf_off, b_data(bp)+off, chunk);
198433d6423SLionel Sambuc 	MARKDIRTY(bp);
199433d6423SLionel Sambuc   }
200433d6423SLionel Sambuc 
2010314acfbSDavid van Moolenbroek   put_block(bp);
202433d6423SLionel Sambuc 
203433d6423SLionel Sambuc   return(r);
204433d6423SLionel Sambuc }
205433d6423SLionel Sambuc 
206433d6423SLionel Sambuc 
207433d6423SLionel Sambuc /*===========================================================================*
208433d6423SLionel Sambuc  *				read_map				     *
209433d6423SLionel Sambuc  *===========================================================================*/
read_map(rip,position,opportunistic)210433d6423SLionel Sambuc block_t read_map(rip, position, opportunistic)
211433d6423SLionel Sambuc register struct inode *rip;	/* ptr to inode to map from */
212433d6423SLionel Sambuc off_t position;			/* position in file whose blk wanted */
213433d6423SLionel Sambuc int opportunistic;		/* if nonzero, only use cache for metadata */
214433d6423SLionel Sambuc {
215433d6423SLionel Sambuc /* Given an inode and a position within the corresponding file, locate the
216433d6423SLionel Sambuc  * block (not zone) number in which that position is to be found and return it.
217433d6423SLionel Sambuc  */
218433d6423SLionel Sambuc 
219433d6423SLionel Sambuc   struct buf *bp;
220433d6423SLionel Sambuc   zone_t z;
221433d6423SLionel Sambuc   int scale, boff, index, zind;
222433d6423SLionel Sambuc   unsigned int dzones, nr_indirects;
223433d6423SLionel Sambuc   block_t b;
224433d6423SLionel Sambuc   unsigned long excess, zone, block_pos;
225cb9453caSDavid van Moolenbroek   int iomode;
226433d6423SLionel Sambuc 
227cb9453caSDavid van Moolenbroek   iomode = opportunistic ? PEEK : NORMAL;
228433d6423SLionel Sambuc 
229433d6423SLionel Sambuc   scale = rip->i_sp->s_log_zone_size;	/* for block-zone conversion */
230433d6423SLionel Sambuc   block_pos = position/rip->i_sp->s_block_size;	/* relative blk # in file */
231433d6423SLionel Sambuc   zone = block_pos >> scale;	/* position's zone */
232433d6423SLionel Sambuc   boff = (int) (block_pos - (zone << scale) ); /* relative blk # within zone */
233433d6423SLionel Sambuc   dzones = rip->i_ndzones;
234433d6423SLionel Sambuc   nr_indirects = rip->i_nindirs;
235433d6423SLionel Sambuc 
236433d6423SLionel Sambuc   /* Is 'position' to be found in the inode itself? */
237433d6423SLionel Sambuc   if (zone < dzones) {
238433d6423SLionel Sambuc 	zind = (int) zone;	/* index should be an int */
239433d6423SLionel Sambuc 	z = rip->i_zone[zind];
240433d6423SLionel Sambuc 	if (z == NO_ZONE) return(NO_BLOCK);
241433d6423SLionel Sambuc 	b = (block_t) ((z << scale) + boff);
242433d6423SLionel Sambuc 	return(b);
243433d6423SLionel Sambuc   }
244433d6423SLionel Sambuc 
245433d6423SLionel Sambuc   /* It is not in the inode, so it must be single or double indirect. */
246433d6423SLionel Sambuc   excess = zone - dzones;	/* first Vx_NR_DZONES don't count */
247433d6423SLionel Sambuc 
248433d6423SLionel Sambuc   if (excess < nr_indirects) {
249433d6423SLionel Sambuc 	/* 'position' can be located via the single indirect block. */
250433d6423SLionel Sambuc 	z = rip->i_zone[dzones];
251433d6423SLionel Sambuc   } else {
252433d6423SLionel Sambuc 	/* 'position' can be located via the double indirect block. */
253433d6423SLionel Sambuc 	if ( (z = rip->i_zone[dzones+1]) == NO_ZONE) return(NO_BLOCK);
254433d6423SLionel Sambuc 	excess -= nr_indirects;			/* single indir doesn't count*/
255433d6423SLionel Sambuc 	b = (block_t) z << scale;
256433d6423SLionel Sambuc 	ASSERT(rip->i_dev != NO_DEV);
257433d6423SLionel Sambuc 	index = (int) (excess/nr_indirects);
258433d6423SLionel Sambuc 	if ((unsigned int) index > rip->i_nindirs)
259433d6423SLionel Sambuc 		return(NO_BLOCK);	/* Can't go beyond double indirects */
260433d6423SLionel Sambuc 	bp = get_block(rip->i_dev, b, iomode); /* get double indirect block */
261cb9453caSDavid van Moolenbroek 	if (bp == NULL)
262cb9453caSDavid van Moolenbroek 		return NO_BLOCK;		/* peeking failed */
263433d6423SLionel Sambuc 	z = rd_indir(bp, index);		/* z= zone for single*/
2640314acfbSDavid van Moolenbroek 	put_block(bp);				/* release double ind block */
265433d6423SLionel Sambuc 	excess = excess % nr_indirects;		/* index into single ind blk */
266433d6423SLionel Sambuc   }
267433d6423SLionel Sambuc 
268433d6423SLionel Sambuc   /* 'z' is zone num for single indirect block; 'excess' is index into it. */
269433d6423SLionel Sambuc   if (z == NO_ZONE) return(NO_BLOCK);
270433d6423SLionel Sambuc   b = (block_t) z << scale;			/* b is blk # for single ind */
271433d6423SLionel Sambuc   bp = get_block(rip->i_dev, b, iomode);	/* get single indirect block */
272cb9453caSDavid van Moolenbroek   if (bp == NULL)
273cb9453caSDavid van Moolenbroek 	return NO_BLOCK;			/* peeking failed */
274433d6423SLionel Sambuc   z = rd_indir(bp, (int) excess);		/* get block pointed to */
2750314acfbSDavid van Moolenbroek   put_block(bp);				/* release single indir blk */
276433d6423SLionel Sambuc   if (z == NO_ZONE) return(NO_BLOCK);
277433d6423SLionel Sambuc   b = (block_t) ((z << scale) + boff);
278433d6423SLionel Sambuc   return(b);
279433d6423SLionel Sambuc }
280433d6423SLionel Sambuc 
get_block_map(register struct inode * rip,u64_t position)281433d6423SLionel Sambuc struct buf *get_block_map(register struct inode *rip, u64_t position)
282433d6423SLionel Sambuc {
2836c46a77dSDavid van Moolenbroek 	struct buf *bp;
2846c46a77dSDavid van Moolenbroek 	int r, block_size;
285433d6423SLionel Sambuc 	block_t b = read_map(rip, position, 0);	/* get block number */
286433d6423SLionel Sambuc 	if(b == NO_BLOCK)
287433d6423SLionel Sambuc 		return NULL;
2886c46a77dSDavid van Moolenbroek 	block_size = get_block_size(rip->i_dev);
289433d6423SLionel Sambuc 	position = rounddown(position, block_size);
290433d6423SLionel Sambuc 	assert(rip->i_num != VMC_NO_INODE);
2916c46a77dSDavid van Moolenbroek 	if ((r = lmfs_get_block_ino(&bp, rip->i_dev, b, NORMAL, rip->i_num,
2926c46a77dSDavid van Moolenbroek 	    position)) != OK)
2936c46a77dSDavid van Moolenbroek 		panic("MFS: error getting block (%llu,%u): %d",
2946c46a77dSDavid van Moolenbroek 		    rip->i_dev, b, r);
2956c46a77dSDavid van Moolenbroek 	return bp;
296433d6423SLionel Sambuc }
297433d6423SLionel Sambuc 
298433d6423SLionel Sambuc /*===========================================================================*
299433d6423SLionel Sambuc  *				rd_indir				     *
300433d6423SLionel Sambuc  *===========================================================================*/
rd_indir(bp,index)301433d6423SLionel Sambuc zone_t rd_indir(bp, index)
302433d6423SLionel Sambuc struct buf *bp;			/* pointer to indirect block */
303433d6423SLionel Sambuc int index;			/* index into *bp */
304433d6423SLionel Sambuc {
305433d6423SLionel Sambuc   struct super_block *sp;
306433d6423SLionel Sambuc   zone_t zone;
307433d6423SLionel Sambuc 
308433d6423SLionel Sambuc   if(bp == NULL)
309433d6423SLionel Sambuc 	panic("rd_indir() on NULL");
310433d6423SLionel Sambuc 
311*4472b590SDavid van Moolenbroek   sp = &superblock;
312433d6423SLionel Sambuc 
313433d6423SLionel Sambuc   /* read a zone from an indirect block */
314433d6423SLionel Sambuc   assert(sp->s_version == V3);
315433d6423SLionel Sambuc   zone = (zone_t) conv4(sp->s_native, (long) b_v2_ind(bp)[index]);
316433d6423SLionel Sambuc 
317433d6423SLionel Sambuc   if (zone != NO_ZONE &&
318433d6423SLionel Sambuc 		(zone < (zone_t) sp->s_firstdatazone || zone >= sp->s_zones)) {
319433d6423SLionel Sambuc 	printf("Illegal zone number %ld in indirect block, index %d\n",
320433d6423SLionel Sambuc 	       (long) zone, index);
321433d6423SLionel Sambuc 	panic("check file system");
322433d6423SLionel Sambuc   }
323433d6423SLionel Sambuc 
324433d6423SLionel Sambuc   return(zone);
325433d6423SLionel Sambuc }
326433d6423SLionel Sambuc 
327433d6423SLionel Sambuc /*===========================================================================*
328433d6423SLionel Sambuc  *				rahead					     *
329433d6423SLionel Sambuc  *===========================================================================*/
rahead(rip,baseblock,position,bytes_ahead)330433d6423SLionel Sambuc static struct buf *rahead(rip, baseblock, position, bytes_ahead)
331433d6423SLionel Sambuc register struct inode *rip;	/* pointer to inode for file to be read */
332433d6423SLionel Sambuc block_t baseblock;		/* block at current position */
333433d6423SLionel Sambuc u64_t position;			/* position within file */
334433d6423SLionel Sambuc unsigned bytes_ahead;		/* bytes beyond position for immediate use */
335433d6423SLionel Sambuc {
336433d6423SLionel Sambuc /* Fetch a block from the cache or the device.  If a physical read is
337433d6423SLionel Sambuc  * required, prefetch as many more blocks as convenient into the cache.
338433d6423SLionel Sambuc  * This usually covers bytes_ahead and is at least BLOCKS_MINIMUM.
339433d6423SLionel Sambuc  * The device driver may decide it knows better and stop reading at a
340433d6423SLionel Sambuc  * cylinder boundary (or after an error).  Rw_scattered() puts an optional
341433d6423SLionel Sambuc  * flag on all reads to allow this.
342433d6423SLionel Sambuc  */
343433d6423SLionel Sambuc /* Minimum number of blocks to prefetch. */
344*4472b590SDavid van Moolenbroek # define BLOCKS_MINIMUM		32
3456c46a77dSDavid van Moolenbroek   int r, scale, read_q_size;
346433d6423SLionel Sambuc   unsigned int blocks_ahead, fragment, block_size;
347433d6423SLionel Sambuc   block_t block, blocks_left;
348433d6423SLionel Sambuc   off_t ind1_pos;
349433d6423SLionel Sambuc   dev_t dev;
350433d6423SLionel Sambuc   struct buf *bp;
351*4472b590SDavid van Moolenbroek   static block64_t read_q[LMFS_MAX_PREFETCH];
352433d6423SLionel Sambuc   u64_t position_running;
353433d6423SLionel Sambuc 
354433d6423SLionel Sambuc   dev = rip->i_dev;
355433d6423SLionel Sambuc   assert(dev != NO_DEV);
356433d6423SLionel Sambuc 
357433d6423SLionel Sambuc   block_size = get_block_size(dev);
358433d6423SLionel Sambuc 
359433d6423SLionel Sambuc   block = baseblock;
360433d6423SLionel Sambuc 
361433d6423SLionel Sambuc   fragment = position % block_size;
362433d6423SLionel Sambuc   position -= fragment;
363433d6423SLionel Sambuc   position_running = position;
364433d6423SLionel Sambuc   bytes_ahead += fragment;
365433d6423SLionel Sambuc   blocks_ahead = (bytes_ahead + block_size - 1) / block_size;
366433d6423SLionel Sambuc 
367*4472b590SDavid van Moolenbroek   r = lmfs_get_block_ino(&bp, dev, block, PEEK, rip->i_num, position);
368*4472b590SDavid van Moolenbroek   if (r == OK)
369*4472b590SDavid van Moolenbroek 	return(bp);
370*4472b590SDavid van Moolenbroek   if (r != ENOENT)
3716c46a77dSDavid van Moolenbroek 	panic("MFS: error getting block (%llu,%u): %d", dev, block, r);
372433d6423SLionel Sambuc 
373433d6423SLionel Sambuc   /* The best guess for the number of blocks to prefetch:  A lot.
374433d6423SLionel Sambuc    * It is impossible to tell what the device looks like, so we don't even
375433d6423SLionel Sambuc    * try to guess the geometry, but leave it to the driver.
376433d6423SLionel Sambuc    *
377433d6423SLionel Sambuc    * The floppy driver can read a full track with no rotational delay, and it
378433d6423SLionel Sambuc    * avoids reading partial tracks if it can, so handing it enough buffers to
379433d6423SLionel Sambuc    * read two tracks is perfect.  (Two, because some diskette types have
380433d6423SLionel Sambuc    * an odd number of sectors per track, so a block may span tracks.)
381433d6423SLionel Sambuc    *
382433d6423SLionel Sambuc    * The disk drivers don't try to be smart.  With todays disks it is
383433d6423SLionel Sambuc    * impossible to tell what the real geometry looks like, so it is best to
384433d6423SLionel Sambuc    * read as much as you can.  With luck the caching on the drive allows
385433d6423SLionel Sambuc    * for a little time to start the next read.
386433d6423SLionel Sambuc    *
387433d6423SLionel Sambuc    * The current solution below is a bit of a hack, it just reads blocks from
388433d6423SLionel Sambuc    * the current file position hoping that more of the file can be found.  A
389433d6423SLionel Sambuc    * better solution must look at the already available zone pointers and
390433d6423SLionel Sambuc    * indirect blocks (but don't call read_map!).
391433d6423SLionel Sambuc    */
392433d6423SLionel Sambuc 
393433d6423SLionel Sambuc   blocks_left = (block_t) (rip->i_size-ex64lo(position)+(block_size-1)) /
394433d6423SLionel Sambuc 								block_size;
395433d6423SLionel Sambuc 
396433d6423SLionel Sambuc   /* Go for the first indirect block if we are in its neighborhood. */
397433d6423SLionel Sambuc   scale = rip->i_sp->s_log_zone_size;
398433d6423SLionel Sambuc   ind1_pos = (off_t) rip->i_ndzones * (block_size << scale);
399ccaeedb2SDavid van Moolenbroek   if ((off_t) ex64lo(position) <= ind1_pos && rip->i_size > ind1_pos) {
400433d6423SLionel Sambuc 	blocks_ahead++;
401433d6423SLionel Sambuc 	blocks_left++;
402433d6423SLionel Sambuc   }
403433d6423SLionel Sambuc 
404433d6423SLionel Sambuc   /* Read at least the minimum number of blocks, but not after a seek. */
405433d6423SLionel Sambuc   if (blocks_ahead < BLOCKS_MINIMUM && rip->i_seek == NO_SEEK)
406433d6423SLionel Sambuc 	blocks_ahead = BLOCKS_MINIMUM;
407433d6423SLionel Sambuc 
408433d6423SLionel Sambuc   /* Can't go past end of file. */
409433d6423SLionel Sambuc   if (blocks_ahead > blocks_left) blocks_ahead = blocks_left;
410433d6423SLionel Sambuc 
411*4472b590SDavid van Moolenbroek   /* No more than the maximum request. */
412*4472b590SDavid van Moolenbroek   if (blocks_ahead > LMFS_MAX_PREFETCH) blocks_ahead = LMFS_MAX_PREFETCH;
413*4472b590SDavid van Moolenbroek 
414433d6423SLionel Sambuc   read_q_size = 0;
415433d6423SLionel Sambuc 
416433d6423SLionel Sambuc   /* Acquire block buffers. */
417433d6423SLionel Sambuc   for (;;) {
418433d6423SLionel Sambuc   	block_t thisblock;
419*4472b590SDavid van Moolenbroek 	read_q[read_q_size++] = block;
420433d6423SLionel Sambuc 
421433d6423SLionel Sambuc 	if (--blocks_ahead == 0) break;
422433d6423SLionel Sambuc 
423433d6423SLionel Sambuc 	block++;
424433d6423SLionel Sambuc 	position_running += block_size;
425433d6423SLionel Sambuc 
426ccaeedb2SDavid van Moolenbroek 	thisblock = read_map(rip, (off_t) ex64lo(position_running), 1);
427ccaeedb2SDavid van Moolenbroek 	if (thisblock != NO_BLOCK) {
428*4472b590SDavid van Moolenbroek 		r = lmfs_get_block_ino(&bp, dev, thisblock, PEEK, rip->i_num,
429*4472b590SDavid van Moolenbroek 		    position_running);
430*4472b590SDavid van Moolenbroek 		block = thisblock;
431*4472b590SDavid van Moolenbroek 	} else
432*4472b590SDavid van Moolenbroek 		r = lmfs_get_block(&bp, dev, block, PEEK);
433*4472b590SDavid van Moolenbroek 
434*4472b590SDavid van Moolenbroek 	if (r == OK) {
435433d6423SLionel Sambuc 		/* Oops, block already in the cache, get out. */
4360314acfbSDavid van Moolenbroek 		put_block(bp);
437433d6423SLionel Sambuc 		break;
438433d6423SLionel Sambuc 	}
439*4472b590SDavid van Moolenbroek 	if (r != ENOENT)
440*4472b590SDavid van Moolenbroek 		panic("MFS: error getting block (%llu,%u): %d", dev, block, r);
441433d6423SLionel Sambuc   }
442*4472b590SDavid van Moolenbroek   lmfs_prefetch(dev, read_q, read_q_size);
443433d6423SLionel Sambuc 
4446c46a77dSDavid van Moolenbroek   r = lmfs_get_block_ino(&bp, dev, baseblock, NORMAL, rip->i_num, position);
4456c46a77dSDavid van Moolenbroek   if (r != OK)
4466c46a77dSDavid van Moolenbroek 	panic("MFS: error getting block (%llu,%u): %d", dev, baseblock, r);
4476c46a77dSDavid van Moolenbroek   return bp;
448433d6423SLionel Sambuc }
449433d6423SLionel Sambuc 
450433d6423SLionel Sambuc 
451433d6423SLionel Sambuc /*===========================================================================*
452433d6423SLionel Sambuc  *				fs_getdents				     *
453433d6423SLionel Sambuc  *===========================================================================*/
fs_getdents(ino_t ino_nr,struct fsdriver_data * data,size_t bytes,off_t * posp)454ccaeedb2SDavid van Moolenbroek ssize_t fs_getdents(ino_t ino_nr, struct fsdriver_data *data, size_t bytes,
455ccaeedb2SDavid van Moolenbroek 	off_t *posp)
456433d6423SLionel Sambuc {
457433d6423SLionel Sambuc #define GETDENTS_BUFSIZE	(sizeof(struct dirent) + MFS_NAME_MAX + 1)
458433d6423SLionel Sambuc #define GETDENTS_ENTRIES	8
459433d6423SLionel Sambuc   static char getdents_buf[GETDENTS_BUFSIZE * GETDENTS_ENTRIES];
460ccaeedb2SDavid van Moolenbroek   struct fsdriver_dentry fsdentry;
461ccaeedb2SDavid van Moolenbroek   struct inode *rip, *entrip;
462ccaeedb2SDavid van Moolenbroek   int r, done;
463ccaeedb2SDavid van Moolenbroek   unsigned int block_size, len, type;
464433d6423SLionel Sambuc   off_t pos, off, block_pos, new_pos, ent_pos;
465433d6423SLionel Sambuc   struct buf *bp;
466433d6423SLionel Sambuc   struct direct *dp;
467433d6423SLionel Sambuc   char *cp;
468433d6423SLionel Sambuc 
469433d6423SLionel Sambuc   /* Check whether the position is properly aligned */
470ccaeedb2SDavid van Moolenbroek   pos = *posp;
471433d6423SLionel Sambuc   if( (unsigned int) pos % DIR_ENTRY_SIZE)
472433d6423SLionel Sambuc 	  return(ENOENT);
473433d6423SLionel Sambuc 
474ccaeedb2SDavid van Moolenbroek   if( (rip = get_inode(fs_dev, ino_nr)) == NULL)
475433d6423SLionel Sambuc 	  return(EINVAL);
476433d6423SLionel Sambuc 
477433d6423SLionel Sambuc   block_size = rip->i_sp->s_block_size;
478433d6423SLionel Sambuc   off = (pos % block_size);		/* Offset in block */
479433d6423SLionel Sambuc   block_pos = pos - off;
480433d6423SLionel Sambuc   done = FALSE;		/* Stop processing directory blocks when done is set */
481433d6423SLionel Sambuc 
482ccaeedb2SDavid van Moolenbroek   fsdriver_dentry_init(&fsdentry, data, bytes, getdents_buf,
483ccaeedb2SDavid van Moolenbroek 	sizeof(getdents_buf));
484433d6423SLionel Sambuc 
485433d6423SLionel Sambuc   /* The default position for the next request is EOF. If the user's buffer
486433d6423SLionel Sambuc    * fills up before EOF, new_pos will be modified. */
487433d6423SLionel Sambuc   new_pos = rip->i_size;
488433d6423SLionel Sambuc 
489ccaeedb2SDavid van Moolenbroek   r = 0;
490ccaeedb2SDavid van Moolenbroek 
491433d6423SLionel Sambuc   for(; block_pos < rip->i_size; block_pos += block_size) {
492433d6423SLionel Sambuc 	/* Since directories don't have holes, 'bp' cannot be NULL. */
493433d6423SLionel Sambuc 	bp = get_block_map(rip, block_pos);	/* get a dir block */
494433d6423SLionel Sambuc 	assert(bp != NULL);
495433d6423SLionel Sambuc 
496433d6423SLionel Sambuc 	/* Search a directory block. */
497433d6423SLionel Sambuc 	if (block_pos < pos)
498433d6423SLionel Sambuc 		dp = &b_dir(bp)[off / DIR_ENTRY_SIZE];
499433d6423SLionel Sambuc 	else
500433d6423SLionel Sambuc 		dp = &b_dir(bp)[0];
501433d6423SLionel Sambuc 	for (; dp < &b_dir(bp)[NR_DIR_ENTRIES(block_size)]; dp++) {
502433d6423SLionel Sambuc 		if (dp->mfs_d_ino == 0)
503433d6423SLionel Sambuc 			continue;	/* Entry is not in use */
504433d6423SLionel Sambuc 
505433d6423SLionel Sambuc 		/* Compute the length of the name */
506433d6423SLionel Sambuc 		cp = memchr(dp->mfs_d_name, '\0', sizeof(dp->mfs_d_name));
507433d6423SLionel Sambuc 		if (cp == NULL)
508433d6423SLionel Sambuc 			len = sizeof(dp->mfs_d_name);
509433d6423SLionel Sambuc 		else
510433d6423SLionel Sambuc 			len = cp - (dp->mfs_d_name);
511433d6423SLionel Sambuc 
512433d6423SLionel Sambuc 		/* Need the position of this entry in the directory */
513433d6423SLionel Sambuc 		ent_pos = block_pos + ((char *) dp - (char *) bp->data);
514433d6423SLionel Sambuc 
515ccaeedb2SDavid van Moolenbroek 		/* We also need(?) the file type of the target inode. */
516ccaeedb2SDavid van Moolenbroek 		if (!(entrip = get_inode(fs_dev, (ino_t) dp->mfs_d_ino)))
517ccaeedb2SDavid van Moolenbroek 			panic("unexpected get_inode failure");
518ccaeedb2SDavid van Moolenbroek 		type = IFTODT(entrip->i_mode);
519ccaeedb2SDavid van Moolenbroek 		put_inode(entrip);
520ccaeedb2SDavid van Moolenbroek 
521ccaeedb2SDavid van Moolenbroek 		/* MFS does not store file types in its directory entries, and
522ccaeedb2SDavid van Moolenbroek 		 * fetching the mode from the inode is seriously expensive.
523ccaeedb2SDavid van Moolenbroek 		 * Userland should always be prepared to receive DT_UNKNOWN.
524ccaeedb2SDavid van Moolenbroek 		 */
525ccaeedb2SDavid van Moolenbroek 		r = fsdriver_dentry_add(&fsdentry, (ino_t) dp->mfs_d_ino,
526ccaeedb2SDavid van Moolenbroek 			dp->mfs_d_name, len, type);
527ccaeedb2SDavid van Moolenbroek 
528ccaeedb2SDavid van Moolenbroek 		/* If the user buffer is full, or an error occurred, stop. */
529ccaeedb2SDavid van Moolenbroek 		if (r <= 0) {
530433d6423SLionel Sambuc 			done = TRUE;
531433d6423SLionel Sambuc 
532433d6423SLionel Sambuc 			/* Record the position of this entry, it is the
533433d6423SLionel Sambuc 			 * starting point of the next request (unless the
534433d6423SLionel Sambuc 			 * postion is modified with lseek).
535433d6423SLionel Sambuc 			 */
536433d6423SLionel Sambuc 			new_pos = ent_pos;
537433d6423SLionel Sambuc 			break;
538433d6423SLionel Sambuc 		}
539433d6423SLionel Sambuc 	}
540433d6423SLionel Sambuc 
5410314acfbSDavid van Moolenbroek 	put_block(bp);
542433d6423SLionel Sambuc 	if (done)
543433d6423SLionel Sambuc 		break;
544433d6423SLionel Sambuc   }
545433d6423SLionel Sambuc 
546ccaeedb2SDavid van Moolenbroek   if (r >= 0 && (r = fsdriver_dentry_finish(&fsdentry)) >= 0) {
547ccaeedb2SDavid van Moolenbroek 	  *posp = new_pos;
548433d6423SLionel Sambuc 	  if(!rip->i_sp->s_rd_only) {
549433d6423SLionel Sambuc 		  rip->i_update |= ATIME;
550433d6423SLionel Sambuc 		  IN_MARKDIRTY(rip);
551433d6423SLionel Sambuc 	  }
552433d6423SLionel Sambuc   }
553433d6423SLionel Sambuc 
554433d6423SLionel Sambuc   put_inode(rip);		/* release the inode */
555433d6423SLionel Sambuc   return(r);
556433d6423SLionel Sambuc }
557