|
|
1.1 ! root 1: #include <errno.h> ! 2: #include <fcntl.h> ! 3: ! 4: #include <linux/sched.h> ! 5: #include <linux/kernel.h> ! 6: #include <asm/segment.h> ! 7: ! 8: #define MIN(a,b) (((a)<(b))?(a):(b)) ! 9: #define MAX(a,b) (((a)>(b))?(a):(b)) ! 10: ! 11: int file_read(struct m_inode * inode, struct file * filp, char * buf, int count) ! 12: { ! 13: int left,chars,nr; ! 14: struct buffer_head * bh; ! 15: ! 16: if ((left=count)<=0) ! 17: return 0; ! 18: while (left) { ! 19: if (nr = bmap(inode,(filp->f_pos)/BLOCK_SIZE)) { ! 20: if (!(bh=bread(inode->i_dev,nr))) ! 21: break; ! 22: } else ! 23: bh = NULL; ! 24: nr = filp->f_pos % BLOCK_SIZE; ! 25: chars = MIN( BLOCK_SIZE-nr , left ); ! 26: filp->f_pos += chars; ! 27: left -= chars; ! 28: if (bh) { ! 29: char * p = nr + bh->b_data; ! 30: while (chars-->0) ! 31: put_fs_byte(*(p++),buf++); ! 32: brelse(bh); ! 33: } else { ! 34: while (chars-->0) ! 35: put_fs_byte(0,buf++); ! 36: } ! 37: } ! 38: inode->i_atime = CURRENT_TIME; ! 39: return (count-left)?(count-left):-ERROR; ! 40: } ! 41: ! 42: int file_write(struct m_inode * inode, struct file * filp, char * buf, int count) ! 43: { ! 44: off_t pos; ! 45: int block,c; ! 46: struct buffer_head * bh; ! 47: char * p; ! 48: int i=0; ! 49: ! 50: /* ! 51: * ok, append may not work when many processes are writing at the same time ! 52: * but so what. That way leads to madness anyway. ! 53: */ ! 54: if (filp->f_flags & O_APPEND) ! 55: pos = inode->i_size; ! 56: else ! 57: pos = filp->f_pos; ! 58: while (i<count) { ! 59: if (!(block = create_block(inode,pos/BLOCK_SIZE))) ! 60: break; ! 61: if (!(bh=bread(inode->i_dev,block))) ! 62: break; ! 63: c = pos % BLOCK_SIZE; ! 64: p = c + bh->b_data; ! 65: bh->b_dirt = 1; ! 66: c = BLOCK_SIZE-c; ! 67: if (c > count-i) c = count-i; ! 68: pos += c; ! 69: if (pos > inode->i_size) { ! 70: inode->i_size = pos; ! 71: inode->i_dirt = 1; ! 72: } ! 73: i += c; ! 74: while (c-->0) ! 75: *(p++) = get_fs_byte(buf++); ! 76: brelse(bh); ! 77: } ! 78: inode->i_mtime = CURRENT_TIME; ! 79: if (!(filp->f_flags & O_APPEND)) { ! 80: filp->f_pos = pos; ! 81: inode->i_ctime = CURRENT_TIME; ! 82: } ! 83: return (i?i:-1); ! 84: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.