/*
|
/*
|
* linux/fs/ext/file.c
|
* linux/fs/ext/file.c
|
*
|
*
|
* Copyright (C) 1992 Remy Card (card@masi.ibp.fr)
|
* Copyright (C) 1992 Remy Card (card@masi.ibp.fr)
|
*
|
*
|
* from
|
* from
|
*
|
*
|
* linux/fs/minix/file.c
|
* linux/fs/minix/file.c
|
*
|
*
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
*
|
*
|
* ext regular file handling primitives
|
* ext regular file handling primitives
|
*/
|
*/
|
|
|
#include <asm/segment.h>
|
#include <asm/segment.h>
|
#include <asm/system.h>
|
#include <asm/system.h>
|
|
|
#include <linux/sched.h>
|
#include <linux/sched.h>
|
#include <linux/ext_fs.h>
|
#include <linux/ext_fs.h>
|
#include <linux/kernel.h>
|
#include <linux/kernel.h>
|
#include <linux/errno.h>
|
#include <linux/errno.h>
|
#include <linux/fcntl.h>
|
#include <linux/fcntl.h>
|
#include <linux/stat.h>
|
#include <linux/stat.h>
|
#include <linux/locks.h>
|
#include <linux/locks.h>
|
#include <linux/pagemap.h>
|
#include <linux/pagemap.h>
|
|
|
#define NBUF 32
|
#define NBUF 32
|
|
|
#define MIN(a,b) (((a)<(b))?(a):(b))
|
#define MIN(a,b) (((a)<(b))?(a):(b))
|
#define MAX(a,b) (((a)>(b))?(a):(b))
|
#define MAX(a,b) (((a)>(b))?(a):(b))
|
|
|
#include <linux/fs.h>
|
#include <linux/fs.h>
|
#include <linux/ext_fs.h>
|
#include <linux/ext_fs.h>
|
|
|
static int ext_file_read(struct inode *, struct file *, char *, int);
|
static int ext_file_read(struct inode *, struct file *, char *, int);
|
static int ext_file_write(struct inode *, struct file *, const char *, int);
|
static int ext_file_write(struct inode *, struct file *, const char *, int);
|
|
|
/*
|
/*
|
* We have mostly NULL's here: the current defaults are ok for
|
* We have mostly NULL's here: the current defaults are ok for
|
* the ext filesystem.
|
* the ext filesystem.
|
*/
|
*/
|
static struct file_operations ext_file_operations = {
|
static struct file_operations ext_file_operations = {
|
NULL, /* lseek - default */
|
NULL, /* lseek - default */
|
ext_file_read, /* read */
|
ext_file_read, /* read */
|
ext_file_write, /* write */
|
ext_file_write, /* write */
|
NULL, /* readdir - bad */
|
NULL, /* readdir - bad */
|
NULL, /* select - default */
|
NULL, /* select - default */
|
NULL, /* ioctl - default */
|
NULL, /* ioctl - default */
|
generic_file_mmap, /* mmap */
|
generic_file_mmap, /* mmap */
|
NULL, /* no special open is needed */
|
NULL, /* no special open is needed */
|
NULL, /* release */
|
NULL, /* release */
|
ext_sync_file /* fsync */
|
ext_sync_file /* fsync */
|
};
|
};
|
|
|
struct inode_operations ext_file_inode_operations = {
|
struct inode_operations ext_file_inode_operations = {
|
&ext_file_operations, /* default file operations */
|
&ext_file_operations, /* default file operations */
|
NULL, /* create */
|
NULL, /* create */
|
NULL, /* lookup */
|
NULL, /* lookup */
|
NULL, /* link */
|
NULL, /* link */
|
NULL, /* unlink */
|
NULL, /* unlink */
|
NULL, /* symlink */
|
NULL, /* symlink */
|
NULL, /* mkdir */
|
NULL, /* mkdir */
|
NULL, /* rmdir */
|
NULL, /* rmdir */
|
NULL, /* mknod */
|
NULL, /* mknod */
|
NULL, /* rename */
|
NULL, /* rename */
|
NULL, /* readlink */
|
NULL, /* readlink */
|
NULL, /* follow_link */
|
NULL, /* follow_link */
|
generic_readpage, /* readpage */
|
generic_readpage, /* readpage */
|
NULL, /* writepage */
|
NULL, /* writepage */
|
ext_bmap, /* bmap */
|
ext_bmap, /* bmap */
|
ext_truncate, /* truncate */
|
ext_truncate, /* truncate */
|
NULL /* permission */
|
NULL /* permission */
|
};
|
};
|
|
|
static int ext_file_read(struct inode * inode, struct file * filp, char * buf, int count)
|
static int ext_file_read(struct inode * inode, struct file * filp, char * buf, int count)
|
{
|
{
|
int read,left,chars;
|
int read,left,chars;
|
int block, blocks, offset;
|
int block, blocks, offset;
|
int bhrequest, uptodate;
|
int bhrequest, uptodate;
|
struct buffer_head ** bhb, ** bhe;
|
struct buffer_head ** bhb, ** bhe;
|
struct buffer_head * bhreq[NBUF];
|
struct buffer_head * bhreq[NBUF];
|
struct buffer_head * buflist[NBUF];
|
struct buffer_head * buflist[NBUF];
|
unsigned int size;
|
unsigned int size;
|
|
|
if (!inode) {
|
if (!inode) {
|
printk("ext_file_read: inode = NULL\n");
|
printk("ext_file_read: inode = NULL\n");
|
return -EINVAL;
|
return -EINVAL;
|
}
|
}
|
if (!S_ISREG(inode->i_mode)) {
|
if (!S_ISREG(inode->i_mode)) {
|
printk("ext_file_read: mode = %07o\n",inode->i_mode);
|
printk("ext_file_read: mode = %07o\n",inode->i_mode);
|
return -EINVAL;
|
return -EINVAL;
|
}
|
}
|
offset = filp->f_pos;
|
offset = filp->f_pos;
|
size = inode->i_size;
|
size = inode->i_size;
|
if (offset > size)
|
if (offset > size)
|
left = 0;
|
left = 0;
|
else
|
else
|
left = size - offset;
|
left = size - offset;
|
if (left > count)
|
if (left > count)
|
left = count;
|
left = count;
|
if (left <= 0)
|
if (left <= 0)
|
return 0;
|
return 0;
|
read = 0;
|
read = 0;
|
block = offset >> BLOCK_SIZE_BITS;
|
block = offset >> BLOCK_SIZE_BITS;
|
offset &= BLOCK_SIZE-1;
|
offset &= BLOCK_SIZE-1;
|
size = (size + (BLOCK_SIZE-1)) >> BLOCK_SIZE_BITS;
|
size = (size + (BLOCK_SIZE-1)) >> BLOCK_SIZE_BITS;
|
blocks = (left + offset + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
|
blocks = (left + offset + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
|
bhb = bhe = buflist;
|
bhb = bhe = buflist;
|
if (filp->f_reada) {
|
if (filp->f_reada) {
|
if(blocks < read_ahead[MAJOR(inode->i_dev)] / (BLOCK_SIZE >> 9))
|
if(blocks < read_ahead[MAJOR(inode->i_dev)] / (BLOCK_SIZE >> 9))
|
blocks = read_ahead[MAJOR(inode->i_dev)] / (BLOCK_SIZE >> 9);
|
blocks = read_ahead[MAJOR(inode->i_dev)] / (BLOCK_SIZE >> 9);
|
if (block + blocks > size)
|
if (block + blocks > size)
|
blocks = size - block;
|
blocks = size - block;
|
}
|
}
|
|
|
/* We do this in a two stage process. We first try to request
|
/* We do this in a two stage process. We first try to request
|
as many blocks as we can, then we wait for the first one to
|
as many blocks as we can, then we wait for the first one to
|
complete, and then we try to wrap up as many as are actually
|
complete, and then we try to wrap up as many as are actually
|
done. This routine is rather generic, in that it can be used
|
done. This routine is rather generic, in that it can be used
|
in a filesystem by substituting the appropriate function in
|
in a filesystem by substituting the appropriate function in
|
for getblk.
|
for getblk.
|
|
|
This routine is optimized to make maximum use of the various
|
This routine is optimized to make maximum use of the various
|
buffers and caches. */
|
buffers and caches. */
|
|
|
do {
|
do {
|
bhrequest = 0;
|
bhrequest = 0;
|
uptodate = 1;
|
uptodate = 1;
|
while (blocks) {
|
while (blocks) {
|
--blocks;
|
--blocks;
|
*bhb = ext_getblk(inode, block++, 0);
|
*bhb = ext_getblk(inode, block++, 0);
|
if (*bhb && !buffer_uptodate(*bhb)) {
|
if (*bhb && !buffer_uptodate(*bhb)) {
|
uptodate = 0;
|
uptodate = 0;
|
bhreq[bhrequest++] = *bhb;
|
bhreq[bhrequest++] = *bhb;
|
}
|
}
|
|
|
if (++bhb == &buflist[NBUF])
|
if (++bhb == &buflist[NBUF])
|
bhb = buflist;
|
bhb = buflist;
|
|
|
/* If the block we have on hand is uptodate, go ahead
|
/* If the block we have on hand is uptodate, go ahead
|
and complete processing. */
|
and complete processing. */
|
if (uptodate)
|
if (uptodate)
|
break;
|
break;
|
if (bhb == bhe)
|
if (bhb == bhe)
|
break;
|
break;
|
}
|
}
|
|
|
/* Now request them all */
|
/* Now request them all */
|
if (bhrequest)
|
if (bhrequest)
|
ll_rw_block(READ, bhrequest, bhreq);
|
ll_rw_block(READ, bhrequest, bhreq);
|
|
|
do { /* Finish off all I/O that has actually completed */
|
do { /* Finish off all I/O that has actually completed */
|
if (*bhe) {
|
if (*bhe) {
|
wait_on_buffer(*bhe);
|
wait_on_buffer(*bhe);
|
if (!buffer_uptodate(*bhe)) { /* read error? */
|
if (!buffer_uptodate(*bhe)) { /* read error? */
|
brelse(*bhe);
|
brelse(*bhe);
|
if (++bhe == &buflist[NBUF])
|
if (++bhe == &buflist[NBUF])
|
bhe = buflist;
|
bhe = buflist;
|
left = 0;
|
left = 0;
|
break;
|
break;
|
}
|
}
|
}
|
}
|
if (left < BLOCK_SIZE - offset)
|
if (left < BLOCK_SIZE - offset)
|
chars = left;
|
chars = left;
|
else
|
else
|
chars = BLOCK_SIZE - offset;
|
chars = BLOCK_SIZE - offset;
|
filp->f_pos += chars;
|
filp->f_pos += chars;
|
left -= chars;
|
left -= chars;
|
read += chars;
|
read += chars;
|
if (*bhe) {
|
if (*bhe) {
|
memcpy_tofs(buf,offset+(*bhe)->b_data,chars);
|
memcpy_tofs(buf,offset+(*bhe)->b_data,chars);
|
brelse(*bhe);
|
brelse(*bhe);
|
buf += chars;
|
buf += chars;
|
} else {
|
} else {
|
while (chars-->0)
|
while (chars-->0)
|
put_user(0,buf++);
|
put_user(0,buf++);
|
}
|
}
|
offset = 0;
|
offset = 0;
|
if (++bhe == &buflist[NBUF])
|
if (++bhe == &buflist[NBUF])
|
bhe = buflist;
|
bhe = buflist;
|
} while (left > 0 && bhe != bhb && (!*bhe || !buffer_locked(*bhe)));
|
} while (left > 0 && bhe != bhb && (!*bhe || !buffer_locked(*bhe)));
|
} while (left > 0);
|
} while (left > 0);
|
|
|
/* Release the read-ahead blocks */
|
/* Release the read-ahead blocks */
|
while (bhe != bhb) {
|
while (bhe != bhb) {
|
brelse(*bhe);
|
brelse(*bhe);
|
if (++bhe == &buflist[NBUF])
|
if (++bhe == &buflist[NBUF])
|
bhe = buflist;
|
bhe = buflist;
|
};
|
};
|
if (!read)
|
if (!read)
|
return -EIO;
|
return -EIO;
|
filp->f_reada = 1;
|
filp->f_reada = 1;
|
if (!IS_RDONLY(inode)) {
|
if (!IS_RDONLY(inode)) {
|
inode->i_atime = CURRENT_TIME;
|
inode->i_atime = CURRENT_TIME;
|
inode->i_dirt = 1;
|
inode->i_dirt = 1;
|
}
|
}
|
return read;
|
return read;
|
}
|
}
|
|
|
static int ext_file_write(struct inode * inode, struct file * filp, const char * buf, int count)
|
static int ext_file_write(struct inode * inode, struct file * filp, const char * buf, int count)
|
{
|
{
|
off_t pos;
|
off_t pos;
|
int written,c;
|
int written,c;
|
struct buffer_head * bh;
|
struct buffer_head * bh;
|
char * p;
|
char * p;
|
|
|
if (!inode) {
|
if (!inode) {
|
printk("ext_file_write: inode = NULL\n");
|
printk("ext_file_write: inode = NULL\n");
|
return -EINVAL;
|
return -EINVAL;
|
}
|
}
|
if (!S_ISREG(inode->i_mode)) {
|
if (!S_ISREG(inode->i_mode)) {
|
printk("ext_file_write: mode = %07o\n",inode->i_mode);
|
printk("ext_file_write: mode = %07o\n",inode->i_mode);
|
return -EINVAL;
|
return -EINVAL;
|
}
|
}
|
/*
|
/*
|
* ok, append may not work when many processes are writing at the same time
|
* ok, append may not work when many processes are writing at the same time
|
* but so what. That way leads to madness anyway.
|
* but so what. That way leads to madness anyway.
|
*/
|
*/
|
if (filp->f_flags & O_APPEND)
|
if (filp->f_flags & O_APPEND)
|
pos = inode->i_size;
|
pos = inode->i_size;
|
else
|
else
|
pos = filp->f_pos;
|
pos = filp->f_pos;
|
written = 0;
|
written = 0;
|
while (written<count) {
|
while (written<count) {
|
bh = ext_getblk(inode,pos/BLOCK_SIZE,1);
|
bh = ext_getblk(inode,pos/BLOCK_SIZE,1);
|
if (!bh) {
|
if (!bh) {
|
if (!written)
|
if (!written)
|
written = -ENOSPC;
|
written = -ENOSPC;
|
break;
|
break;
|
}
|
}
|
c = BLOCK_SIZE - (pos % BLOCK_SIZE);
|
c = BLOCK_SIZE - (pos % BLOCK_SIZE);
|
if (c > count-written)
|
if (c > count-written)
|
c = count-written;
|
c = count-written;
|
if (c != BLOCK_SIZE && !buffer_uptodate(bh)) {
|
if (c != BLOCK_SIZE && !buffer_uptodate(bh)) {
|
ll_rw_block(READ, 1, &bh);
|
ll_rw_block(READ, 1, &bh);
|
wait_on_buffer(bh);
|
wait_on_buffer(bh);
|
if (!buffer_uptodate(bh)) {
|
if (!buffer_uptodate(bh)) {
|
brelse(bh);
|
brelse(bh);
|
if (!written)
|
if (!written)
|
written = -EIO;
|
written = -EIO;
|
break;
|
break;
|
}
|
}
|
}
|
}
|
p = (pos % BLOCK_SIZE) + bh->b_data;
|
p = (pos % BLOCK_SIZE) + bh->b_data;
|
memcpy_fromfs(p,buf,c);
|
memcpy_fromfs(p,buf,c);
|
update_vm_cache(inode, pos, p, c);
|
update_vm_cache(inode, pos, p, c);
|
pos += c;
|
pos += c;
|
if (pos > inode->i_size) {
|
if (pos > inode->i_size) {
|
inode->i_size = pos;
|
inode->i_size = pos;
|
inode->i_dirt = 1;
|
inode->i_dirt = 1;
|
}
|
}
|
written += c;
|
written += c;
|
buf += c;
|
buf += c;
|
mark_buffer_uptodate(bh, 1);
|
mark_buffer_uptodate(bh, 1);
|
mark_buffer_dirty(bh, 0);
|
mark_buffer_dirty(bh, 0);
|
brelse(bh);
|
brelse(bh);
|
}
|
}
|
inode->i_mtime = inode->i_ctime = CURRENT_TIME;
|
inode->i_mtime = inode->i_ctime = CURRENT_TIME;
|
filp->f_pos = pos;
|
filp->f_pos = pos;
|
inode->i_dirt = 1;
|
inode->i_dirt = 1;
|
return written;
|
return written;
|
}
|
}
|
|
|