/*===========================================================================* * fs_stat * *===========================================================================*/ int fs_stat(ino_t ino_nr, struct stat *statbuf) { struct inode *rip; mode_t mo; int s; if ((rip = get_inode(fs_dev, ino_nr)) == NULL) return(EINVAL); /* Update the atime, ctime, and mtime fields in the inode, if need be. */ if (rip->i_update) update_times(rip); /* Fill in the statbuf struct. */ mo = rip->i_mode & I_TYPE; /* true iff special */ s = (mo == I_CHAR_SPECIAL || mo == I_BLOCK_SPECIAL); statbuf->st_mode = (mode_t) rip->i_mode; statbuf->st_nlink = (nlink_t) rip->i_nlinks; statbuf->st_uid = rip->i_uid; statbuf->st_gid = rip->i_gid; statbuf->st_rdev = (s ? (dev_t)rip->i_zone[0] : NO_DEV); statbuf->st_size = rip->i_size; statbuf->st_atime = rip->i_atime; statbuf->st_mtime = rip->i_mtime; statbuf->st_ctime = rip->i_ctime; statbuf->st_blksize = lmfs_fs_block_size(); statbuf->st_blocks = estimate_blocks(rip); put_inode(rip); /* release the inode */ return(OK); }
/*===========================================================================* * get_block_size * *===========================================================================*/ unsigned int get_block_size(dev_t dev) { if (dev == NO_DEV) panic("request for block size of NO_DEV"); return(lmfs_fs_block_size()); }
/* * Perform block I/O, on "dev", starting from offset "pos", for a total of * "bytes" bytes. Reading, writing, and peeking are highly similar, and thus, * this function implements all of them. The "call" parameter indicates the * call type (one of FSC_READ, FSC_WRITE, FSC_PEEK). For read and write calls, * "data" will identify the user buffer to use; for peek calls, "data" is set * to NULL. In all cases, this function returns the number of bytes * successfully transferred, 0 on end-of-file conditions, and a negative error * code if no bytes could be transferred due to an error. Dirty data is not * flushed immediately, and thus, a successful write only indicates that the * data have been taken in by the cache (for immediate I/O, a character device * would have to be used, but MINIX3 no longer supports this), which may be * follwed later by silent failures, including undetected end-of-file cases. * In particular, write requests may or may not return 0 (EOF) immediately when * writing at or beyond the block device's size. i Since block I/O takes place * at block granularity, block-unaligned writes have to read a block from disk * before updating it, and that is the only possible source of actual I/O * errors for write calls. * TODO: reconsider the buffering-only approach, or see if we can at least * somehow throw accurate EOF errors without reading in each block first. */ ssize_t lmfs_bio(dev_t dev, struct fsdriver_data * data, size_t bytes, off_t pos, int call) { block_t block, blocks_left; size_t block_size, off, block_off, chunk; struct buf *bp; int r, write, how; if (dev == NO_DEV) return EINVAL; block_size = lmfs_fs_block_size(); write = (call == FSC_WRITE); assert(block_size > 0); /* FIXME: block_t is 32-bit, so we have to impose a limit here. */ if (pos < 0 || pos / block_size > UINT32_MAX || bytes > SSIZE_MAX) return EINVAL; off = 0; block = pos / block_size; block_off = (size_t)(pos % block_size); blocks_left = howmany(block_off + bytes, block_size); lmfs_reset_rdwt_err(); r = OK; for (off = 0; off < bytes; off += chunk) { chunk = block_size - block_off; if (chunk > bytes - off) chunk = bytes - off; /* * For read requests, help the block driver form larger I/O * requests. */ if (!write) block_prefetch(dev, block, blocks_left); /* * Do not read the block from disk if we will end up * overwriting all of its contents. */ how = (write && chunk == block_size) ? NO_READ : NORMAL; bp = lmfs_get_block(dev, block, how); assert(bp); r = lmfs_rdwt_err(); if (r == OK && data != NULL) { assert(lmfs_dev(bp) != NO_DEV); if (write) { r = fsdriver_copyin(data, off, (char *)bp->data + block_off, chunk); /* * Mark the block as dirty even if the copy * failed, since the copy may in fact have * succeeded partially. This is an interface * issue that should be resolved at some point, * but for now we do not want the cache to be * desynchronized from the disk contents. */ lmfs_markdirty(bp); } else r = fsdriver_copyout(data, off, (char *)bp->data + block_off, chunk); } lmfs_put_block(bp, FULL_DATA_BLOCK); if (r != OK) break; block++; block_off = 0; blocks_left--; } /* * If we were not able to do any I/O, return the error (or EOF, even * for writes). Otherwise, return how many bytes we did manage to * transfer. */ if (r != OK && off == 0) return (r == END_OF_FILE) ? 0 : r; return off; }