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