This source file includes following definitions.
- msdos_file_read
- msdos_file_write
- msdos_truncate
1
2
3
4
5
6
7
8
9 #include <asm/segment.h>
10 #include <asm/system.h>
11
12 #include <linux/sched.h>
13 #include <linux/fs.h>
14 #include <linux/msdos_fs.h>
15 #include <linux/errno.h>
16 #include <linux/fcntl.h>
17 #include <linux/stat.h>
18
19 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
20 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
21
22 static int msdos_file_read(struct inode *inode,struct file *filp,char *buf,
23 int count);
24 static int msdos_file_write(struct inode *inode,struct file *filp,char *buf,
25 int count);
26
27
28 static struct file_operations msdos_file_operations = {
29 NULL,
30 msdos_file_read,
31 msdos_file_write,
32 NULL,
33 NULL,
34 NULL,
35 NULL,
36 NULL,
37 NULL
38 };
39
40 struct inode_operations msdos_file_inode_operations = {
41 &msdos_file_operations,
42 NULL,
43 NULL,
44 NULL,
45 NULL,
46 NULL,
47 NULL,
48 NULL,
49 NULL,
50 NULL,
51 NULL,
52 NULL,
53 msdos_bmap,
54 msdos_truncate
55 };
56
57
58
59 struct inode_operations msdos_file_inode_operations_no_bmap = {
60 &msdos_file_operations,
61 NULL,
62 NULL,
63 NULL,
64 NULL,
65 NULL,
66 NULL,
67 NULL,
68 NULL,
69 NULL,
70 NULL,
71 NULL,
72 NULL,
73 msdos_truncate
74 };
75
76
77 static int msdos_file_read(struct inode *inode,struct file *filp,char *buf,
78 int count)
79 {
80 char *start;
81 int left,offset,size,sector,cnt;
82 char ch;
83 struct buffer_head *bh;
84 void *data;
85
86
87 if (!inode) {
88 printk("msdos_file_read: inode = NULL\n");
89 return -EINVAL;
90 }
91 if (!S_ISREG(inode->i_mode)) {
92 printk("msdos_file_read: mode = %07o\n",inode->i_mode);
93 return -EINVAL;
94 }
95 if (filp->f_pos >= inode->i_size || count <= 0) return 0;
96 start = buf;
97 while ((left = MIN(inode->i_size-filp->f_pos,count-(buf-start))) > 0){
98 if (!(sector = msdos_smap(inode,filp->f_pos >> SECTOR_BITS)))
99 break;
100 offset = filp->f_pos & (SECTOR_SIZE-1);
101 if (!(bh = msdos_sread(inode->i_dev,sector,&data))) break;
102 filp->f_pos += (size = MIN(SECTOR_SIZE-offset,left));
103 if (MSDOS_I(inode)->i_binary) {
104 memcpy_tofs(buf,data+offset,size);
105 buf += size;
106 }
107 else for (cnt = size; cnt; cnt--) {
108 if ((ch = *((char *) data+offset++)) == '\r')
109 size--;
110 else {
111 if (ch != 26) put_fs_byte(ch,buf++);
112 else {
113 filp->f_pos = inode->i_size;
114 brelse(bh);
115 return buf-start;
116 }
117 }
118 }
119 brelse(bh);
120 }
121 if (start == buf) return -EIO;
122 return buf-start;
123 }
124
125
126 static int msdos_file_write(struct inode *inode,struct file *filp,char *buf,
127 int count)
128 {
129 int sector,offset,size,left,written;
130 int error,carry;
131 char *start,*to,ch;
132 struct buffer_head *bh;
133 void *data;
134
135 if (!inode) {
136 printk("msdos_file_write: inode = NULL\n");
137 return -EINVAL;
138 }
139 if (!S_ISREG(inode->i_mode)) {
140 printk("msdos_file_write: mode = %07o\n",inode->i_mode);
141 return -EINVAL;
142 }
143
144
145
146
147 if (filp->f_flags & O_APPEND) filp->f_pos = inode->i_size;
148 if (count <= 0) return 0;
149 error = carry = 0;
150 for (start = buf; count || carry; count -= size) {
151 while (!(sector = msdos_smap(inode,filp->f_pos >> SECTOR_BITS)))
152 if ((error = msdos_add_cluster(inode)) < 0) break;
153 if (error) {
154 msdos_truncate(inode);
155 break;
156 }
157 offset = filp->f_pos & (SECTOR_SIZE-1);
158 size = MIN(SECTOR_SIZE-offset,MAX(carry,count));
159 if (!(bh = msdos_sread(inode->i_dev,sector,&data))) {
160 error = -EIO;
161 break;
162 }
163 if (MSDOS_I(inode)->i_binary) {
164 memcpy_fromfs(data+(filp->f_pos & (SECTOR_SIZE-1)),
165 buf,written = size);
166 buf += size;
167 }
168 else {
169 written = left = SECTOR_SIZE-offset;
170 to = data+(filp->f_pos & (SECTOR_SIZE-1));
171 if (carry) {
172 *to++ = '\n';
173 left--;
174 carry = 0;
175 }
176 for (size = 0; size < count && left; size++) {
177 if ((ch = get_fs_byte(buf++)) == '\n') {
178 *to++ = '\r';
179 left--;
180 }
181 if (!left) carry = 1;
182 else {
183 *to++ = ch;
184 left--;
185 }
186 }
187 written -= left;
188 }
189 filp->f_pos += written;
190 if (filp->f_pos > inode->i_size) {
191 inode->i_size = filp->f_pos;
192 inode->i_dirt = 1;
193 }
194 bh->b_dirt = 1;
195 brelse(bh);
196 }
197 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
198 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
199 inode->i_dirt = 1;
200 return start == buf ? error : buf-start;
201 }
202
203
204 void msdos_truncate(struct inode *inode)
205 {
206 int cluster;
207
208 cluster = SECTOR_SIZE*MSDOS_SB(inode->i_sb)->cluster_size;
209 (void) fat_free(inode,(inode->i_size+(cluster-1))/cluster);
210 MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
211 inode->i_dirt = 1;
212 }