This source file includes following definitions.
- minix_file_read
- minix_file_write
1
2
3
4
5
6
7
8
9 #ifdef MODULE
10 #include <linux/module.h>
11 #endif
12
13 #include <asm/segment.h>
14 #include <asm/system.h>
15
16 #include <linux/sched.h>
17 #include <linux/minix_fs.h>
18 #include <linux/kernel.h>
19 #include <linux/errno.h>
20 #include <linux/fcntl.h>
21 #include <linux/stat.h>
22 #include <linux/locks.h>
23
24 #define NBUF 32
25
26 #define MIN(a,b) (((a)<(b))?(a):(b))
27 #define MAX(a,b) (((a)>(b))?(a):(b))
28
29 #include <linux/fs.h>
30 #include <linux/minix_fs.h>
31
32 static int minix_file_read(struct inode *, struct file *, char *, int);
33 static int minix_file_write(struct inode *, struct file *, char *, int);
34
35
36
37
38
39 static struct file_operations minix_file_operations = {
40 NULL,
41 minix_file_read,
42 minix_file_write,
43 NULL,
44 NULL,
45 NULL,
46 generic_mmap,
47 NULL,
48 NULL,
49 minix_sync_file
50 };
51
52 struct inode_operations minix_file_inode_operations = {
53 &minix_file_operations,
54 NULL,
55 NULL,
56 NULL,
57 NULL,
58 NULL,
59 NULL,
60 NULL,
61 NULL,
62 NULL,
63 NULL,
64 NULL,
65 minix_bmap,
66 minix_truncate,
67 NULL
68 };
69
70 static int minix_file_read(struct inode * inode, struct file * filp, char * buf, int count)
71 {
72 int read,left,chars;
73 int block, blocks, offset;
74 int bhrequest, uptodate;
75 struct buffer_head ** bhb, ** bhe;
76 struct buffer_head * bhreq[NBUF];
77 struct buffer_head * buflist[NBUF];
78 unsigned int size;
79
80 if (!inode) {
81 printk("minix_file_read: inode = NULL\n");
82 return -EINVAL;
83 }
84 if (!S_ISREG(inode->i_mode)) {
85 printk("minix_file_read: mode = %07o\n",inode->i_mode);
86 return -EINVAL;
87 }
88 offset = filp->f_pos;
89 size = inode->i_size;
90 if (offset > size)
91 left = 0;
92 else
93 left = size - offset;
94 if (left > count)
95 left = count;
96 if (left <= 0)
97 return 0;
98 read = 0;
99 block = offset >> BLOCK_SIZE_BITS;
100 offset &= BLOCK_SIZE-1;
101 size = (size + (BLOCK_SIZE-1)) >> BLOCK_SIZE_BITS;
102 blocks = (left + offset + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
103 bhb = bhe = buflist;
104 if (filp->f_reada) {
105 if(blocks < read_ahead[MAJOR(inode->i_dev)] / (BLOCK_SIZE >> 9))
106 blocks = read_ahead[MAJOR(inode->i_dev)] / (BLOCK_SIZE >> 9);
107 if (block + blocks > size)
108 blocks = size - block;
109 }
110
111
112
113
114
115
116
117
118
119
120
121 do {
122 bhrequest = 0;
123 uptodate = 1;
124 while (blocks) {
125 --blocks;
126 *bhb = minix_getblk(inode, block++, 0);
127 if (*bhb && !(*bhb)->b_uptodate) {
128 uptodate = 0;
129 bhreq[bhrequest++] = *bhb;
130 }
131
132 if (++bhb == &buflist[NBUF])
133 bhb = buflist;
134
135
136
137 if (uptodate)
138 break;
139 if (bhb == bhe)
140 break;
141 }
142
143
144 if (bhrequest)
145 ll_rw_block(READ, bhrequest, bhreq);
146
147 do {
148 if (*bhe) {
149 wait_on_buffer(*bhe);
150 if (!(*bhe)->b_uptodate) {
151 brelse(*bhe);
152 if (++bhe == &buflist[NBUF])
153 bhe = buflist;
154 left = 0;
155 break;
156 }
157 }
158 if (left < BLOCK_SIZE - offset)
159 chars = left;
160 else
161 chars = BLOCK_SIZE - offset;
162 filp->f_pos += chars;
163 left -= chars;
164 read += chars;
165 if (*bhe) {
166 memcpy_tofs(buf,offset+(*bhe)->b_data,chars);
167 brelse(*bhe);
168 buf += chars;
169 } else {
170 while (chars-->0)
171 put_user(0,buf++);
172 }
173 offset = 0;
174 if (++bhe == &buflist[NBUF])
175 bhe = buflist;
176 } while (left > 0 && bhe != bhb && (!*bhe || !(*bhe)->b_lock));
177 } while (left > 0);
178
179
180 while (bhe != bhb) {
181 brelse(*bhe);
182 if (++bhe == &buflist[NBUF])
183 bhe = buflist;
184 };
185 if (!read)
186 return -EIO;
187 filp->f_reada = 1;
188 if (!IS_RDONLY(inode))
189 inode->i_atime = CURRENT_TIME;
190 return read;
191 }
192
193 static int minix_file_write(struct inode * inode, struct file * filp, char * buf, int count)
194 {
195 off_t pos;
196 int written,c;
197 struct buffer_head * bh;
198 char * p;
199
200 if (!inode) {
201 printk("minix_file_write: inode = NULL\n");
202 return -EINVAL;
203 }
204 if (!S_ISREG(inode->i_mode)) {
205 printk("minix_file_write: mode = %07o\n",inode->i_mode);
206 return -EINVAL;
207 }
208 down(&inode->i_sem);
209 if (filp->f_flags & O_APPEND)
210 pos = inode->i_size;
211 else
212 pos = filp->f_pos;
213 written = 0;
214 while (written < count) {
215 bh = minix_getblk(inode,pos/BLOCK_SIZE,1);
216 if (!bh) {
217 if (!written)
218 written = -ENOSPC;
219 break;
220 }
221 c = BLOCK_SIZE - (pos % BLOCK_SIZE);
222 if (c > count-written)
223 c = count-written;
224 if (c != BLOCK_SIZE && !bh->b_uptodate) {
225 ll_rw_block(READ, 1, &bh);
226 wait_on_buffer(bh);
227 if (!bh->b_uptodate) {
228 brelse(bh);
229 if (!written)
230 written = -EIO;
231 break;
232 }
233 }
234 p = (pos % BLOCK_SIZE) + bh->b_data;
235 pos += c;
236 written += c;
237 memcpy_fromfs(p,buf,c);
238 buf += c;
239 bh->b_uptodate = 1;
240 mark_buffer_dirty(bh, 0);
241 brelse(bh);
242 }
243 if (pos > inode->i_size)
244 inode->i_size = pos;
245 up(&inode->i_sem);
246 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
247 filp->f_pos = pos;
248 inode->i_dirt = 1;
249 return written;
250 }