This source file includes following definitions.
- min
- ncp_fsync
- ncp_make_open
- ncp_file_read
- ncp_file_write
1
2
3
4
5
6
7
8 #include <asm/segment.h>
9 #include <asm/system.h>
10
11 #include <linux/sched.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/fcntl.h>
15 #include <linux/stat.h>
16 #include <linux/mm.h>
17 #include <linux/ncp_fs.h>
18 #include "ncplib_kernel.h"
19 #include <linux/malloc.h>
20
21 static inline int min(int a, int b)
22 {
23 return a<b ? a : b;
24 }
25
26 static int
27 ncp_fsync(struct inode *inode, struct file *file)
28 {
29 return 0;
30 }
31
32 int
33 ncp_make_open(struct inode *i, int right)
34 {
35 struct nw_file_info *finfo;
36
37 if (i == NULL)
38 {
39 printk("ncp_make_open: got NULL inode\n");
40 return -EINVAL;
41 }
42
43 finfo = NCP_FINFO(i);
44
45 DPRINTK("ncp_make_open: dirent->opened = %d\n", finfo->opened);
46
47 if (finfo->opened == 0)
48 {
49
50 if (ncp_open_create_file_or_subdir(NCP_SERVER(i),
51 NULL, NULL,
52 OC_MODE_OPEN, 0,
53 AR_READ | AR_WRITE,
54 finfo) == 0)
55 {
56 finfo->access = O_RDWR;
57 }
58 else if (ncp_open_create_file_or_subdir(NCP_SERVER(i),
59 NULL, NULL,
60 OC_MODE_OPEN, 0,
61 AR_READ,
62 finfo) == 0)
63 {
64 finfo->access = O_RDONLY;
65 }
66 else
67 {
68 return -EACCES;
69 }
70 }
71
72 if ( ((right == O_RDONLY) && ( (finfo->access == O_RDONLY)
73 || (finfo->access == O_RDWR)))
74 || ((right == O_WRONLY) && ( (finfo->access == O_WRONLY)
75 || (finfo->access == O_RDWR)))
76 || ((right == O_RDWR) && (finfo->access == O_RDWR)))
77 return 0;
78
79 return -EACCES;
80 }
81
82 static int
83 ncp_file_read(struct inode *inode, struct file *file, char *buf, int count)
84 {
85 int bufsize, already_read;
86 off_t pos;
87 int errno;
88
89 DPRINTK("ncp_file_read: enter %s\n", NCP_ISTRUCT(inode)->entryName);
90
91 if (inode == NULL)
92 {
93 DPRINTK("ncp_file_read: inode = NULL\n");
94 return -EINVAL;
95 }
96 if (!ncp_conn_valid(NCP_SERVER(inode)))
97 {
98 return -EIO;
99 }
100
101 if (!S_ISREG(inode->i_mode))
102 {
103 DPRINTK("ncp_file_read: read from non-file, mode %07o\n",
104 inode->i_mode);
105 return -EINVAL;
106 }
107
108 pos = file->f_pos;
109
110 if (pos + count > inode->i_size)
111 {
112 count = inode->i_size - pos;
113 }
114
115 if (count <= 0)
116 {
117 return 0;
118 }
119
120 if ((errno = ncp_make_open(inode, O_RDONLY)) != 0)
121 {
122 return errno;
123 }
124
125 bufsize = NCP_SERVER(inode)->buffer_size;
126
127 already_read = 0;
128
129
130 while (already_read < count)
131 {
132 int read_this_time;
133 int to_read = min(bufsize - (pos % bufsize),
134 count - already_read);
135
136 if (ncp_read(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
137 pos, to_read, buf, &read_this_time) != 0)
138 {
139 return -EIO;
140 }
141
142 pos += read_this_time;
143 buf += read_this_time;
144 already_read += read_this_time;
145
146 if (read_this_time < to_read)
147 {
148 break;
149 }
150 }
151
152 file->f_pos = pos;
153
154 if (!IS_RDONLY(inode))
155 {
156 inode->i_atime = CURRENT_TIME;
157 }
158
159 inode->i_dirt = 1;
160
161 DPRINTK("ncp_file_read: exit %s\n", NCP_ISTRUCT(inode)->entryName);
162
163 return already_read;
164 }
165
166 static int
167 ncp_file_write(struct inode *inode, struct file *file, const char *buf,
168 int count)
169 {
170 int bufsize, already_written;
171 off_t pos;
172 int errno;
173
174 if (inode == NULL)
175 {
176 DPRINTK("ncp_file_write: inode = NULL\n");
177 return -EINVAL;
178 }
179 if (!ncp_conn_valid(NCP_SERVER(inode)))
180 {
181 return -EIO;
182 }
183
184 if (!S_ISREG(inode->i_mode))
185 {
186 DPRINTK("ncp_file_write: write to non-file, mode %07o\n",
187 inode->i_mode);
188 return -EINVAL;
189 }
190
191 DPRINTK("ncp_file_write: enter %s\n", NCP_ISTRUCT(inode)->entryName);
192
193 if (count <= 0)
194 {
195 return 0;
196 }
197
198 if ((errno = ncp_make_open(inode, O_RDWR)) != 0)
199 {
200 return errno;
201 }
202
203 pos = file->f_pos;
204
205 if (file->f_flags & O_APPEND)
206 {
207 pos = inode->i_size;
208 }
209
210 bufsize = NCP_SERVER(inode)->buffer_size;
211
212 already_written = 0;
213
214 while (already_written < count)
215 {
216 int written_this_time;
217 int to_write = min(bufsize - (pos % bufsize),
218 count - already_written);
219
220 if (ncp_write(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
221 pos, to_write, buf, &written_this_time) != 0)
222 {
223 return -EIO;
224 }
225
226 pos += written_this_time;
227 buf += written_this_time;
228 already_written += written_this_time;
229
230 if (written_this_time < to_write)
231 {
232 break;
233 }
234 }
235
236 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
237 inode->i_dirt = 1;
238
239 file->f_pos = pos;
240
241 if (pos > inode->i_size)
242 {
243 inode->i_size = pos;
244 ncp_invalid_dir_cache(NCP_INOP(inode)->dir->inode);
245 }
246
247 DPRINTK("ncp_file_write: exit %s\n", NCP_ISTRUCT(inode)->entryName);
248
249 return already_written;
250 }
251
252 static struct file_operations ncp_file_operations = {
253 NULL,
254 ncp_file_read,
255 ncp_file_write,
256 NULL,
257 NULL,
258 ncp_ioctl,
259 ncp_mmap,
260 NULL,
261 NULL,
262 ncp_fsync,
263 };
264
265 struct inode_operations ncp_file_inode_operations = {
266 &ncp_file_operations,
267 NULL,
268 NULL,
269 NULL,
270 NULL,
271 NULL,
272 NULL,
273 NULL,
274 NULL,
275 NULL,
276 NULL,
277 NULL,
278 NULL,
279 NULL
280 };