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> 9*0314acfbSDavid 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 *===========================================================================*/ 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 lmfs_reset_rdwt_err(); 48433d6423SLionel Sambuc 49433d6423SLionel Sambuc /* If this is file i/o, check we can write */ 50ccaeedb2SDavid van Moolenbroek if (call == FSC_WRITE) { 51433d6423SLionel Sambuc if(rip->i_sp->s_rd_only) 52433d6423SLionel Sambuc return EROFS; 53433d6423SLionel Sambuc 54433d6423SLionel Sambuc /* Check in advance to see if file will grow too big. */ 55433d6423SLionel Sambuc if (position > (off_t) (rip->i_sp->s_max_size - nrbytes)) 56433d6423SLionel Sambuc return(EFBIG); 57433d6423SLionel Sambuc 58433d6423SLionel Sambuc /* Clear the zone containing present EOF if hole about 59433d6423SLionel Sambuc * to be created. This is necessary because all unwritten 60433d6423SLionel Sambuc * blocks prior to the EOF must read as zeros. 61433d6423SLionel Sambuc */ 62433d6423SLionel Sambuc if(position > f_size) clear_zone(rip, f_size, 0); 63433d6423SLionel Sambuc } 64433d6423SLionel Sambuc 65433d6423SLionel Sambuc cum_io = 0; 66433d6423SLionel Sambuc /* Split the transfer into chunks that don't span two blocks. */ 67433d6423SLionel Sambuc while (nrbytes > 0) { 68433d6423SLionel Sambuc off = ((unsigned int) position) % block_size; /* offset in blk*/ 69ccaeedb2SDavid van Moolenbroek chunk = block_size - off; 70ccaeedb2SDavid van Moolenbroek if (chunk > nrbytes) 71ccaeedb2SDavid van Moolenbroek chunk = nrbytes; 72433d6423SLionel Sambuc 73f53651deSBen Gras if (call != FSC_WRITE) { 74433d6423SLionel Sambuc bytes_left = f_size - position; 75433d6423SLionel Sambuc if (position >= f_size) break; /* we are beyond EOF */ 76433d6423SLionel Sambuc if (chunk > (unsigned int) bytes_left) chunk = bytes_left; 77433d6423SLionel Sambuc } 78433d6423SLionel Sambuc 79433d6423SLionel Sambuc /* Read or write 'chunk' bytes. */ 80433d6423SLionel Sambuc r = rw_chunk(rip, ((u64_t)((unsigned long)position)), off, chunk, 81ccaeedb2SDavid van Moolenbroek nrbytes, call, data, cum_io, block_size, &completed); 82433d6423SLionel Sambuc 83433d6423SLionel Sambuc if (r != OK) break; /* EOF reached */ 84433d6423SLionel Sambuc if (lmfs_rdwt_err() < 0) break; 85433d6423SLionel Sambuc 86433d6423SLionel Sambuc /* Update counters and pointers. */ 87433d6423SLionel Sambuc nrbytes -= chunk; /* bytes yet to be read */ 88433d6423SLionel Sambuc cum_io += chunk; /* bytes read so far */ 89433d6423SLionel Sambuc position += (off_t) chunk; /* position within the file */ 90433d6423SLionel Sambuc } 91433d6423SLionel Sambuc 92433d6423SLionel Sambuc /* On write, update file size and access time. */ 93ccaeedb2SDavid van Moolenbroek if (call == FSC_WRITE) { 94433d6423SLionel Sambuc if (regular || mode_word == I_DIRECTORY) { 95433d6423SLionel Sambuc if (position > f_size) rip->i_size = position; 96433d6423SLionel Sambuc } 97433d6423SLionel Sambuc } 98433d6423SLionel Sambuc 99433d6423SLionel Sambuc rip->i_seek = NO_SEEK; 100433d6423SLionel Sambuc 101433d6423SLionel Sambuc if (lmfs_rdwt_err() != OK) r = lmfs_rdwt_err(); /* check for disk error */ 102433d6423SLionel Sambuc if (lmfs_rdwt_err() == END_OF_FILE) r = OK; 103433d6423SLionel Sambuc 104ccaeedb2SDavid van Moolenbroek if (r != OK) 105ccaeedb2SDavid van Moolenbroek return r; 106ccaeedb2SDavid van Moolenbroek 107433d6423SLionel Sambuc /* even on a ROFS, writing to a device node on it is fine, 108433d6423SLionel Sambuc * just don't update the inode stats for it. And dito for reading. 109433d6423SLionel Sambuc */ 110ccaeedb2SDavid van Moolenbroek if (!rip->i_sp->s_rd_only) { 111ccaeedb2SDavid van Moolenbroek if (call == FSC_READ) rip->i_update |= ATIME; 112ccaeedb2SDavid van Moolenbroek if (call == FSC_WRITE) rip->i_update |= CTIME | MTIME; 113433d6423SLionel Sambuc IN_MARKDIRTY(rip); /* inode is thus now dirty */ 114433d6423SLionel Sambuc } 115433d6423SLionel Sambuc 116ccaeedb2SDavid van Moolenbroek return cum_io; 117433d6423SLionel Sambuc } 118433d6423SLionel Sambuc 119433d6423SLionel Sambuc 120433d6423SLionel Sambuc /*===========================================================================* 121433d6423SLionel Sambuc * rw_chunk * 122433d6423SLionel Sambuc *===========================================================================*/ 123ccaeedb2SDavid van Moolenbroek static int rw_chunk(rip, position, off, chunk, left, call, data, buf_off, 124ccaeedb2SDavid van Moolenbroek block_size, completed) 125433d6423SLionel Sambuc register struct inode *rip; /* pointer to inode for file to be rd/wr */ 126433d6423SLionel Sambuc u64_t position; /* position within file to read or write */ 127433d6423SLionel Sambuc unsigned off; /* off within the current block */ 128ccaeedb2SDavid van Moolenbroek size_t chunk; /* number of bytes to read or write */ 129433d6423SLionel Sambuc unsigned left; /* max number of bytes wanted after position */ 130ccaeedb2SDavid van Moolenbroek int call; /* FSC_READ, FSC_WRITE, or FSC_PEEK */ 131ccaeedb2SDavid van Moolenbroek struct fsdriver_data *data; /* structure for (remote) user buffer */ 132ccaeedb2SDavid van Moolenbroek unsigned buf_off; /* offset in user buffer */ 133433d6423SLionel Sambuc unsigned int block_size; /* block size of FS operating on */ 134433d6423SLionel Sambuc int *completed; /* number of bytes copied */ 135433d6423SLionel Sambuc { 136433d6423SLionel Sambuc /* Read or write (part of) a block. */ 137433d6423SLionel Sambuc 138433d6423SLionel Sambuc register struct buf *bp = NULL; 139433d6423SLionel Sambuc register int r = OK; 140ccaeedb2SDavid van Moolenbroek int n; 141433d6423SLionel Sambuc block_t b; 142433d6423SLionel Sambuc dev_t dev; 143433d6423SLionel Sambuc ino_t ino = VMC_NO_INODE; 144433d6423SLionel Sambuc u64_t ino_off = rounddown(position, block_size); 145433d6423SLionel Sambuc 146433d6423SLionel Sambuc *completed = 0; 147433d6423SLionel Sambuc 148433d6423SLionel Sambuc if (ex64hi(position) != 0) 149433d6423SLionel Sambuc panic("rw_chunk: position too high"); 150433d6423SLionel Sambuc b = read_map(rip, (off_t) ex64lo(position), 0); 151433d6423SLionel Sambuc dev = rip->i_dev; 152433d6423SLionel Sambuc ino = rip->i_num; 153433d6423SLionel Sambuc assert(ino != VMC_NO_INODE); 154433d6423SLionel Sambuc 155ccaeedb2SDavid van Moolenbroek if (b == NO_BLOCK) { 156ccaeedb2SDavid van Moolenbroek if (call == FSC_READ) { 157433d6423SLionel Sambuc /* Reading from a nonexistent block. Must read as all zeros.*/ 158ccaeedb2SDavid van Moolenbroek r = fsdriver_zero(data, buf_off, chunk); 159433d6423SLionel Sambuc if(r != OK) { 160ccaeedb2SDavid van Moolenbroek printf("MFS: fsdriver_zero failed\n"); 161433d6423SLionel Sambuc } 162433d6423SLionel Sambuc return r; 163d75faf18SDavid van Moolenbroek } else if (call == FSC_PEEK) { 164d75faf18SDavid van Moolenbroek /* Peeking a nonexistent block. Report to VM. */ 165d75faf18SDavid van Moolenbroek lmfs_zero_block_ino(dev, ino, ino_off); 166d75faf18SDavid van Moolenbroek return OK; 167433d6423SLionel Sambuc } else { 168d75faf18SDavid van Moolenbroek /* Writing to a nonexistent block. 169433d6423SLionel Sambuc * Create and enter in inode. 170433d6423SLionel Sambuc */ 171433d6423SLionel Sambuc if ((bp = new_block(rip, (off_t) ex64lo(position))) == NULL) 172433d6423SLionel Sambuc return(err_code); 173433d6423SLionel Sambuc } 174ccaeedb2SDavid van Moolenbroek } else if (call != FSC_WRITE) { 175433d6423SLionel Sambuc /* Read and read ahead if convenient. */ 176433d6423SLionel Sambuc bp = rahead(rip, b, position, left); 177433d6423SLionel Sambuc } else { 178433d6423SLionel Sambuc /* Normally an existing block to be partially overwritten is first read 179433d6423SLionel Sambuc * in. However, a full block need not be read in. If it is already in 180433d6423SLionel Sambuc * the cache, acquire it, otherwise just acquire a free buffer. 181433d6423SLionel Sambuc */ 182433d6423SLionel Sambuc n = (chunk == block_size ? NO_READ : NORMAL); 183ccaeedb2SDavid van Moolenbroek if (off == 0 && (off_t) ex64lo(position) >= rip->i_size) 184433d6423SLionel Sambuc n = NO_READ; 185433d6423SLionel Sambuc assert(ino != VMC_NO_INODE); 186433d6423SLionel Sambuc assert(!(ino_off % block_size)); 187433d6423SLionel Sambuc bp = lmfs_get_block_ino(dev, b, n, ino, ino_off); 188433d6423SLionel Sambuc } 189433d6423SLionel Sambuc 190433d6423SLionel Sambuc /* In all cases, bp now points to a valid buffer. */ 191433d6423SLionel Sambuc assert(bp != NULL); 192433d6423SLionel Sambuc 193ccaeedb2SDavid van Moolenbroek if (call == FSC_WRITE && chunk != block_size && 194433d6423SLionel Sambuc (off_t) ex64lo(position) >= rip->i_size && off == 0) { 195433d6423SLionel Sambuc zero_block(bp); 196433d6423SLionel Sambuc } 197433d6423SLionel Sambuc 198ccaeedb2SDavid van Moolenbroek if (call == FSC_READ) { 199433d6423SLionel Sambuc /* Copy a chunk from the block buffer to user space. */ 200ccaeedb2SDavid van Moolenbroek r = fsdriver_copyout(data, buf_off, b_data(bp)+off, chunk); 201ccaeedb2SDavid van Moolenbroek } else if (call == FSC_WRITE) { 202433d6423SLionel Sambuc /* Copy a chunk from user space to the block buffer. */ 203ccaeedb2SDavid van Moolenbroek r = fsdriver_copyin(data, buf_off, b_data(bp)+off, chunk); 204433d6423SLionel Sambuc MARKDIRTY(bp); 205433d6423SLionel Sambuc } 206433d6423SLionel Sambuc 207*0314acfbSDavid van Moolenbroek put_block(bp); 208433d6423SLionel Sambuc 209433d6423SLionel Sambuc return(r); 210433d6423SLionel Sambuc } 211433d6423SLionel Sambuc 212433d6423SLionel Sambuc 213433d6423SLionel Sambuc /*===========================================================================* 214433d6423SLionel Sambuc * read_map * 215433d6423SLionel Sambuc *===========================================================================*/ 216433d6423SLionel Sambuc block_t read_map(rip, position, opportunistic) 217433d6423SLionel Sambuc register struct inode *rip; /* ptr to inode to map from */ 218433d6423SLionel Sambuc off_t position; /* position in file whose blk wanted */ 219433d6423SLionel Sambuc int opportunistic; /* if nonzero, only use cache for metadata */ 220433d6423SLionel Sambuc { 221433d6423SLionel Sambuc /* Given an inode and a position within the corresponding file, locate the 222433d6423SLionel Sambuc * block (not zone) number in which that position is to be found and return it. 223433d6423SLionel Sambuc */ 224433d6423SLionel Sambuc 225433d6423SLionel Sambuc struct buf *bp; 226433d6423SLionel Sambuc zone_t z; 227433d6423SLionel Sambuc int scale, boff, index, zind; 228433d6423SLionel Sambuc unsigned int dzones, nr_indirects; 229433d6423SLionel Sambuc block_t b; 230433d6423SLionel Sambuc unsigned long excess, zone, block_pos; 231cb9453caSDavid van Moolenbroek int iomode; 232433d6423SLionel Sambuc 233cb9453caSDavid van Moolenbroek iomode = opportunistic ? PEEK : NORMAL; 234433d6423SLionel Sambuc 235433d6423SLionel Sambuc scale = rip->i_sp->s_log_zone_size; /* for block-zone conversion */ 236433d6423SLionel Sambuc block_pos = position/rip->i_sp->s_block_size; /* relative blk # in file */ 237433d6423SLionel Sambuc zone = block_pos >> scale; /* position's zone */ 238433d6423SLionel Sambuc boff = (int) (block_pos - (zone << scale) ); /* relative blk # within zone */ 239433d6423SLionel Sambuc dzones = rip->i_ndzones; 240433d6423SLionel Sambuc nr_indirects = rip->i_nindirs; 241433d6423SLionel Sambuc 242433d6423SLionel Sambuc /* Is 'position' to be found in the inode itself? */ 243433d6423SLionel Sambuc if (zone < dzones) { 244433d6423SLionel Sambuc zind = (int) zone; /* index should be an int */ 245433d6423SLionel Sambuc z = rip->i_zone[zind]; 246433d6423SLionel Sambuc if (z == NO_ZONE) return(NO_BLOCK); 247433d6423SLionel Sambuc b = (block_t) ((z << scale) + boff); 248433d6423SLionel Sambuc return(b); 249433d6423SLionel Sambuc } 250433d6423SLionel Sambuc 251433d6423SLionel Sambuc /* It is not in the inode, so it must be single or double indirect. */ 252433d6423SLionel Sambuc excess = zone - dzones; /* first Vx_NR_DZONES don't count */ 253433d6423SLionel Sambuc 254433d6423SLionel Sambuc if (excess < nr_indirects) { 255433d6423SLionel Sambuc /* 'position' can be located via the single indirect block. */ 256433d6423SLionel Sambuc z = rip->i_zone[dzones]; 257433d6423SLionel Sambuc } else { 258433d6423SLionel Sambuc /* 'position' can be located via the double indirect block. */ 259433d6423SLionel Sambuc if ( (z = rip->i_zone[dzones+1]) == NO_ZONE) return(NO_BLOCK); 260433d6423SLionel Sambuc excess -= nr_indirects; /* single indir doesn't count*/ 261433d6423SLionel Sambuc b = (block_t) z << scale; 262433d6423SLionel Sambuc ASSERT(rip->i_dev != NO_DEV); 263433d6423SLionel Sambuc index = (int) (excess/nr_indirects); 264433d6423SLionel Sambuc if ((unsigned int) index > rip->i_nindirs) 265433d6423SLionel Sambuc return(NO_BLOCK); /* Can't go beyond double indirects */ 266433d6423SLionel Sambuc bp = get_block(rip->i_dev, b, iomode); /* get double indirect block */ 267cb9453caSDavid van Moolenbroek if (bp == NULL) 268cb9453caSDavid van Moolenbroek return NO_BLOCK; /* peeking failed */ 269433d6423SLionel Sambuc ASSERT(lmfs_dev(bp) != NO_DEV); 270433d6423SLionel Sambuc ASSERT(lmfs_dev(bp) == rip->i_dev); 271433d6423SLionel Sambuc z = rd_indir(bp, index); /* z= zone for single*/ 272*0314acfbSDavid van Moolenbroek put_block(bp); /* release double ind block */ 273433d6423SLionel Sambuc excess = excess % nr_indirects; /* index into single ind blk */ 274433d6423SLionel Sambuc } 275433d6423SLionel Sambuc 276433d6423SLionel Sambuc /* 'z' is zone num for single indirect block; 'excess' is index into it. */ 277433d6423SLionel Sambuc if (z == NO_ZONE) return(NO_BLOCK); 278433d6423SLionel Sambuc b = (block_t) z << scale; /* b is blk # for single ind */ 279433d6423SLionel Sambuc bp = get_block(rip->i_dev, b, iomode); /* get single indirect block */ 280cb9453caSDavid van Moolenbroek if (bp == NULL) 281cb9453caSDavid van Moolenbroek return NO_BLOCK; /* peeking failed */ 282433d6423SLionel Sambuc z = rd_indir(bp, (int) excess); /* get block pointed to */ 283*0314acfbSDavid van Moolenbroek put_block(bp); /* release single indir blk */ 284433d6423SLionel Sambuc if (z == NO_ZONE) return(NO_BLOCK); 285433d6423SLionel Sambuc b = (block_t) ((z << scale) + boff); 286433d6423SLionel Sambuc return(b); 287433d6423SLionel Sambuc } 288433d6423SLionel Sambuc 289433d6423SLionel Sambuc struct buf *get_block_map(register struct inode *rip, u64_t position) 290433d6423SLionel Sambuc { 291433d6423SLionel Sambuc block_t b = read_map(rip, position, 0); /* get block number */ 292433d6423SLionel Sambuc int block_size = get_block_size(rip->i_dev); 293433d6423SLionel Sambuc if(b == NO_BLOCK) 294433d6423SLionel Sambuc return NULL; 295433d6423SLionel Sambuc position = rounddown(position, block_size); 296433d6423SLionel Sambuc assert(rip->i_num != VMC_NO_INODE); 297433d6423SLionel Sambuc return lmfs_get_block_ino(rip->i_dev, b, NORMAL, rip->i_num, position); 298433d6423SLionel Sambuc } 299433d6423SLionel Sambuc 300433d6423SLionel Sambuc /*===========================================================================* 301433d6423SLionel Sambuc * rd_indir * 302433d6423SLionel Sambuc *===========================================================================*/ 303433d6423SLionel Sambuc zone_t rd_indir(bp, index) 304433d6423SLionel Sambuc struct buf *bp; /* pointer to indirect block */ 305433d6423SLionel Sambuc int index; /* index into *bp */ 306433d6423SLionel Sambuc { 307433d6423SLionel Sambuc struct super_block *sp; 308433d6423SLionel Sambuc zone_t zone; 309433d6423SLionel Sambuc 310433d6423SLionel Sambuc if(bp == NULL) 311433d6423SLionel Sambuc panic("rd_indir() on NULL"); 312433d6423SLionel Sambuc 313433d6423SLionel Sambuc sp = get_super(lmfs_dev(bp)); /* need super block to find file sys type */ 314433d6423SLionel Sambuc 315433d6423SLionel Sambuc /* read a zone from an indirect block */ 316433d6423SLionel Sambuc assert(sp->s_version == V3); 317433d6423SLionel Sambuc zone = (zone_t) conv4(sp->s_native, (long) b_v2_ind(bp)[index]); 318433d6423SLionel Sambuc 319433d6423SLionel Sambuc if (zone != NO_ZONE && 320433d6423SLionel Sambuc (zone < (zone_t) sp->s_firstdatazone || zone >= sp->s_zones)) { 321433d6423SLionel Sambuc printf("Illegal zone number %ld in indirect block, index %d\n", 322433d6423SLionel Sambuc (long) zone, index); 323433d6423SLionel Sambuc panic("check file system"); 324433d6423SLionel Sambuc } 325433d6423SLionel Sambuc 326433d6423SLionel Sambuc return(zone); 327433d6423SLionel Sambuc } 328433d6423SLionel Sambuc 329433d6423SLionel Sambuc /*===========================================================================* 330433d6423SLionel Sambuc * rahead * 331433d6423SLionel Sambuc *===========================================================================*/ 332433d6423SLionel Sambuc static struct buf *rahead(rip, baseblock, position, bytes_ahead) 333433d6423SLionel Sambuc register struct inode *rip; /* pointer to inode for file to be read */ 334433d6423SLionel Sambuc block_t baseblock; /* block at current position */ 335433d6423SLionel Sambuc u64_t position; /* position within file */ 336433d6423SLionel Sambuc unsigned bytes_ahead; /* bytes beyond position for immediate use */ 337433d6423SLionel Sambuc { 338433d6423SLionel Sambuc /* Fetch a block from the cache or the device. If a physical read is 339433d6423SLionel Sambuc * required, prefetch as many more blocks as convenient into the cache. 340433d6423SLionel Sambuc * This usually covers bytes_ahead and is at least BLOCKS_MINIMUM. 341433d6423SLionel Sambuc * The device driver may decide it knows better and stop reading at a 342433d6423SLionel Sambuc * cylinder boundary (or after an error). Rw_scattered() puts an optional 343433d6423SLionel Sambuc * flag on all reads to allow this. 344433d6423SLionel Sambuc */ 345433d6423SLionel Sambuc /* Minimum number of blocks to prefetch. */ 346433d6423SLionel Sambuc int nr_bufs = lmfs_nr_bufs(); 347433d6423SLionel Sambuc # define BLOCKS_MINIMUM (nr_bufs < 50 ? 18 : 32) 348ccaeedb2SDavid van Moolenbroek int scale, read_q_size; 349433d6423SLionel Sambuc unsigned int blocks_ahead, fragment, block_size; 350433d6423SLionel Sambuc block_t block, blocks_left; 351433d6423SLionel Sambuc off_t ind1_pos; 352433d6423SLionel Sambuc dev_t dev; 353433d6423SLionel Sambuc struct buf *bp; 354433d6423SLionel Sambuc static unsigned int readqsize = 0; 355433d6423SLionel Sambuc static struct buf **read_q; 356433d6423SLionel Sambuc u64_t position_running; 357433d6423SLionel Sambuc int inuse_before = lmfs_bufs_in_use(); 358433d6423SLionel Sambuc 359433d6423SLionel Sambuc if(readqsize != nr_bufs) { 360433d6423SLionel Sambuc if(readqsize > 0) { 361433d6423SLionel Sambuc assert(read_q != NULL); 362433d6423SLionel Sambuc free(read_q); 363433d6423SLionel Sambuc } 364433d6423SLionel Sambuc if(!(read_q = malloc(sizeof(read_q[0])*nr_bufs))) 365433d6423SLionel Sambuc panic("couldn't allocate read_q"); 366433d6423SLionel Sambuc readqsize = nr_bufs; 367433d6423SLionel Sambuc } 368433d6423SLionel Sambuc 369433d6423SLionel Sambuc dev = rip->i_dev; 370433d6423SLionel Sambuc assert(dev != NO_DEV); 371433d6423SLionel Sambuc 372433d6423SLionel Sambuc block_size = get_block_size(dev); 373433d6423SLionel Sambuc 374433d6423SLionel Sambuc block = baseblock; 375433d6423SLionel Sambuc 376433d6423SLionel Sambuc fragment = position % block_size; 377433d6423SLionel Sambuc position -= fragment; 378433d6423SLionel Sambuc position_running = position; 379433d6423SLionel Sambuc bytes_ahead += fragment; 380433d6423SLionel Sambuc blocks_ahead = (bytes_ahead + block_size - 1) / block_size; 381433d6423SLionel Sambuc 382433d6423SLionel Sambuc bp = lmfs_get_block_ino(dev, block, PREFETCH, rip->i_num, position); 383433d6423SLionel Sambuc assert(bp != NULL); 384433d6423SLionel Sambuc assert(bp->lmfs_count > 0); 385433d6423SLionel Sambuc if (lmfs_dev(bp) != NO_DEV) return(bp); 386433d6423SLionel Sambuc 387433d6423SLionel Sambuc /* The best guess for the number of blocks to prefetch: A lot. 388433d6423SLionel Sambuc * It is impossible to tell what the device looks like, so we don't even 389433d6423SLionel Sambuc * try to guess the geometry, but leave it to the driver. 390433d6423SLionel Sambuc * 391433d6423SLionel Sambuc * The floppy driver can read a full track with no rotational delay, and it 392433d6423SLionel Sambuc * avoids reading partial tracks if it can, so handing it enough buffers to 393433d6423SLionel Sambuc * read two tracks is perfect. (Two, because some diskette types have 394433d6423SLionel Sambuc * an odd number of sectors per track, so a block may span tracks.) 395433d6423SLionel Sambuc * 396433d6423SLionel Sambuc * The disk drivers don't try to be smart. With todays disks it is 397433d6423SLionel Sambuc * impossible to tell what the real geometry looks like, so it is best to 398433d6423SLionel Sambuc * read as much as you can. With luck the caching on the drive allows 399433d6423SLionel Sambuc * for a little time to start the next read. 400433d6423SLionel Sambuc * 401433d6423SLionel Sambuc * The current solution below is a bit of a hack, it just reads blocks from 402433d6423SLionel Sambuc * the current file position hoping that more of the file can be found. A 403433d6423SLionel Sambuc * better solution must look at the already available zone pointers and 404433d6423SLionel Sambuc * indirect blocks (but don't call read_map!). 405433d6423SLionel Sambuc */ 406433d6423SLionel Sambuc 407433d6423SLionel Sambuc blocks_left = (block_t) (rip->i_size-ex64lo(position)+(block_size-1)) / 408433d6423SLionel Sambuc block_size; 409433d6423SLionel Sambuc 410433d6423SLionel Sambuc /* Go for the first indirect block if we are in its neighborhood. */ 411433d6423SLionel Sambuc scale = rip->i_sp->s_log_zone_size; 412433d6423SLionel Sambuc ind1_pos = (off_t) rip->i_ndzones * (block_size << scale); 413ccaeedb2SDavid van Moolenbroek if ((off_t) ex64lo(position) <= ind1_pos && rip->i_size > ind1_pos) { 414433d6423SLionel Sambuc blocks_ahead++; 415433d6423SLionel Sambuc blocks_left++; 416433d6423SLionel Sambuc } 417433d6423SLionel Sambuc 418433d6423SLionel Sambuc /* No more than the maximum request. */ 419433d6423SLionel Sambuc if (blocks_ahead > NR_IOREQS) blocks_ahead = NR_IOREQS; 420433d6423SLionel Sambuc 421433d6423SLionel Sambuc /* Read at least the minimum number of blocks, but not after a seek. */ 422433d6423SLionel Sambuc if (blocks_ahead < BLOCKS_MINIMUM && rip->i_seek == NO_SEEK) 423433d6423SLionel Sambuc blocks_ahead = BLOCKS_MINIMUM; 424433d6423SLionel Sambuc 425433d6423SLionel Sambuc /* Can't go past end of file. */ 426433d6423SLionel Sambuc if (blocks_ahead > blocks_left) blocks_ahead = blocks_left; 427433d6423SLionel Sambuc 428433d6423SLionel Sambuc read_q_size = 0; 429433d6423SLionel Sambuc 430433d6423SLionel Sambuc /* Acquire block buffers. */ 431433d6423SLionel Sambuc for (;;) { 432433d6423SLionel Sambuc block_t thisblock; 433433d6423SLionel Sambuc assert(bp->lmfs_count > 0); 434433d6423SLionel Sambuc read_q[read_q_size++] = bp; 435433d6423SLionel Sambuc 436433d6423SLionel Sambuc if (--blocks_ahead == 0) break; 437433d6423SLionel Sambuc 438433d6423SLionel Sambuc /* Don't trash the cache, leave 4 free. */ 439433d6423SLionel Sambuc if (lmfs_bufs_in_use() >= nr_bufs - 4) break; 440433d6423SLionel Sambuc 441433d6423SLionel Sambuc block++; 442433d6423SLionel Sambuc position_running += block_size; 443433d6423SLionel Sambuc 444ccaeedb2SDavid van Moolenbroek thisblock = read_map(rip, (off_t) ex64lo(position_running), 1); 445ccaeedb2SDavid van Moolenbroek if (thisblock != NO_BLOCK) { 446ccaeedb2SDavid van Moolenbroek bp = lmfs_get_block_ino(dev, thisblock, PREFETCH, rip->i_num, 447ccaeedb2SDavid van Moolenbroek position_running); 448433d6423SLionel Sambuc } else { 449433d6423SLionel Sambuc bp = get_block(dev, block, PREFETCH); 450433d6423SLionel Sambuc } 451433d6423SLionel Sambuc assert(bp); 452433d6423SLionel Sambuc assert(bp->lmfs_count > 0); 453433d6423SLionel Sambuc if (lmfs_dev(bp) != NO_DEV) { 454433d6423SLionel Sambuc /* Oops, block already in the cache, get out. */ 455*0314acfbSDavid van Moolenbroek put_block(bp); 456433d6423SLionel Sambuc break; 457433d6423SLionel Sambuc } 458433d6423SLionel Sambuc } 459433d6423SLionel Sambuc lmfs_rw_scattered(dev, read_q, read_q_size, READING); 460433d6423SLionel Sambuc 461433d6423SLionel Sambuc assert(inuse_before == lmfs_bufs_in_use()); 462433d6423SLionel Sambuc 463433d6423SLionel Sambuc return(lmfs_get_block_ino(dev, baseblock, NORMAL, rip->i_num, position)); 464433d6423SLionel Sambuc } 465433d6423SLionel Sambuc 466433d6423SLionel Sambuc 467433d6423SLionel Sambuc /*===========================================================================* 468433d6423SLionel Sambuc * fs_getdents * 469433d6423SLionel Sambuc *===========================================================================*/ 470ccaeedb2SDavid van Moolenbroek ssize_t fs_getdents(ino_t ino_nr, struct fsdriver_data *data, size_t bytes, 471ccaeedb2SDavid van Moolenbroek off_t *posp) 472433d6423SLionel Sambuc { 473433d6423SLionel Sambuc #define GETDENTS_BUFSIZE (sizeof(struct dirent) + MFS_NAME_MAX + 1) 474433d6423SLionel Sambuc #define GETDENTS_ENTRIES 8 475433d6423SLionel Sambuc static char getdents_buf[GETDENTS_BUFSIZE * GETDENTS_ENTRIES]; 476ccaeedb2SDavid van Moolenbroek struct fsdriver_dentry fsdentry; 477ccaeedb2SDavid van Moolenbroek struct inode *rip, *entrip; 478ccaeedb2SDavid van Moolenbroek int r, done; 479ccaeedb2SDavid van Moolenbroek unsigned int block_size, len, type; 480433d6423SLionel Sambuc off_t pos, off, block_pos, new_pos, ent_pos; 481433d6423SLionel Sambuc struct buf *bp; 482433d6423SLionel Sambuc struct direct *dp; 483433d6423SLionel Sambuc char *cp; 484433d6423SLionel Sambuc 485433d6423SLionel Sambuc /* Check whether the position is properly aligned */ 486ccaeedb2SDavid van Moolenbroek pos = *posp; 487433d6423SLionel Sambuc if( (unsigned int) pos % DIR_ENTRY_SIZE) 488433d6423SLionel Sambuc return(ENOENT); 489433d6423SLionel Sambuc 490ccaeedb2SDavid van Moolenbroek if( (rip = get_inode(fs_dev, ino_nr)) == NULL) 491433d6423SLionel Sambuc return(EINVAL); 492433d6423SLionel Sambuc 493433d6423SLionel Sambuc block_size = rip->i_sp->s_block_size; 494433d6423SLionel Sambuc off = (pos % block_size); /* Offset in block */ 495433d6423SLionel Sambuc block_pos = pos - off; 496433d6423SLionel Sambuc done = FALSE; /* Stop processing directory blocks when done is set */ 497433d6423SLionel Sambuc 498ccaeedb2SDavid van Moolenbroek fsdriver_dentry_init(&fsdentry, data, bytes, getdents_buf, 499ccaeedb2SDavid van Moolenbroek sizeof(getdents_buf)); 500433d6423SLionel Sambuc 501433d6423SLionel Sambuc /* The default position for the next request is EOF. If the user's buffer 502433d6423SLionel Sambuc * fills up before EOF, new_pos will be modified. */ 503433d6423SLionel Sambuc new_pos = rip->i_size; 504433d6423SLionel Sambuc 505ccaeedb2SDavid van Moolenbroek r = 0; 506ccaeedb2SDavid van Moolenbroek 507433d6423SLionel Sambuc for(; block_pos < rip->i_size; block_pos += block_size) { 508433d6423SLionel Sambuc /* Since directories don't have holes, 'bp' cannot be NULL. */ 509433d6423SLionel Sambuc bp = get_block_map(rip, block_pos); /* get a dir block */ 510433d6423SLionel Sambuc assert(bp != NULL); 511433d6423SLionel Sambuc 512433d6423SLionel Sambuc /* Search a directory block. */ 513433d6423SLionel Sambuc if (block_pos < pos) 514433d6423SLionel Sambuc dp = &b_dir(bp)[off / DIR_ENTRY_SIZE]; 515433d6423SLionel Sambuc else 516433d6423SLionel Sambuc dp = &b_dir(bp)[0]; 517433d6423SLionel Sambuc for (; dp < &b_dir(bp)[NR_DIR_ENTRIES(block_size)]; dp++) { 518433d6423SLionel Sambuc if (dp->mfs_d_ino == 0) 519433d6423SLionel Sambuc continue; /* Entry is not in use */ 520433d6423SLionel Sambuc 521433d6423SLionel Sambuc /* Compute the length of the name */ 522433d6423SLionel Sambuc cp = memchr(dp->mfs_d_name, '\0', sizeof(dp->mfs_d_name)); 523433d6423SLionel Sambuc if (cp == NULL) 524433d6423SLionel Sambuc len = sizeof(dp->mfs_d_name); 525433d6423SLionel Sambuc else 526433d6423SLionel Sambuc len = cp - (dp->mfs_d_name); 527433d6423SLionel Sambuc 528433d6423SLionel Sambuc /* Need the position of this entry in the directory */ 529433d6423SLionel Sambuc ent_pos = block_pos + ((char *) dp - (char *) bp->data); 530433d6423SLionel Sambuc 531ccaeedb2SDavid van Moolenbroek /* We also need(?) the file type of the target inode. */ 532ccaeedb2SDavid van Moolenbroek if (!(entrip = get_inode(fs_dev, (ino_t) dp->mfs_d_ino))) 533ccaeedb2SDavid van Moolenbroek panic("unexpected get_inode failure"); 534ccaeedb2SDavid van Moolenbroek type = IFTODT(entrip->i_mode); 535ccaeedb2SDavid van Moolenbroek put_inode(entrip); 536ccaeedb2SDavid van Moolenbroek 537ccaeedb2SDavid van Moolenbroek /* MFS does not store file types in its directory entries, and 538ccaeedb2SDavid van Moolenbroek * fetching the mode from the inode is seriously expensive. 539ccaeedb2SDavid van Moolenbroek * Userland should always be prepared to receive DT_UNKNOWN. 540ccaeedb2SDavid van Moolenbroek */ 541ccaeedb2SDavid van Moolenbroek r = fsdriver_dentry_add(&fsdentry, (ino_t) dp->mfs_d_ino, 542ccaeedb2SDavid van Moolenbroek dp->mfs_d_name, len, type); 543ccaeedb2SDavid van Moolenbroek 544ccaeedb2SDavid van Moolenbroek /* If the user buffer is full, or an error occurred, stop. */ 545ccaeedb2SDavid van Moolenbroek if (r <= 0) { 546433d6423SLionel Sambuc done = TRUE; 547433d6423SLionel Sambuc 548433d6423SLionel Sambuc /* Record the position of this entry, it is the 549433d6423SLionel Sambuc * starting point of the next request (unless the 550433d6423SLionel Sambuc * postion is modified with lseek). 551433d6423SLionel Sambuc */ 552433d6423SLionel Sambuc new_pos = ent_pos; 553433d6423SLionel Sambuc break; 554433d6423SLionel Sambuc } 555433d6423SLionel Sambuc } 556433d6423SLionel Sambuc 557*0314acfbSDavid van Moolenbroek put_block(bp); 558433d6423SLionel Sambuc if (done) 559433d6423SLionel Sambuc break; 560433d6423SLionel Sambuc } 561433d6423SLionel Sambuc 562ccaeedb2SDavid van Moolenbroek if (r >= 0 && (r = fsdriver_dentry_finish(&fsdentry)) >= 0) { 563ccaeedb2SDavid van Moolenbroek *posp = new_pos; 564433d6423SLionel Sambuc if(!rip->i_sp->s_rd_only) { 565433d6423SLionel Sambuc rip->i_update |= ATIME; 566433d6423SLionel Sambuc IN_MARKDIRTY(rip); 567433d6423SLionel Sambuc } 568433d6423SLionel Sambuc } 569433d6423SLionel Sambuc 570433d6423SLionel Sambuc put_inode(rip); /* release the inode */ 571433d6423SLionel Sambuc return(r); 572433d6423SLionel Sambuc } 573