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
97 if (!S_ISREG(inode->i_mode))
98 {
99 DPRINTK("ncp_file_read: read from non-file, mode %07o\n",
100 inode->i_mode);
101 return -EINVAL;
102 }
103
104 pos = file->f_pos;
105
106 if (pos + count > inode->i_size)
107 {
108 count = inode->i_size - pos;
109 }
110
111 if (count <= 0)
112 {
113 return 0;
114 }
115
116 if ((errno = ncp_make_open(inode, O_RDONLY)) != 0)
117 {
118 return errno;
119 }
120
121 bufsize = NCP_SERVER(inode)->buffer_size;
122
123 already_read = 0;
124
125
126 while (already_read < count)
127 {
128 int read_this_time;
129 int to_read = min(bufsize - (pos % bufsize),
130 count - already_read);
131
132 if (ncp_read(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
133 pos, to_read, buf, &read_this_time) != 0)
134 {
135 return -EIO;
136 }
137
138 pos += read_this_time;
139 buf += read_this_time;
140 already_read += read_this_time;
141
142 if (read_this_time < to_read)
143 {
144 break;
145 }
146 }
147
148 file->f_pos = pos;
149
150 if (!IS_RDONLY(inode))
151 {
152 inode->i_atime = CURRENT_TIME;
153 }
154
155 inode->i_dirt = 1;
156
157 DPRINTK("ncp_file_read: exit %s\n", NCP_ISTRUCT(inode)->entryName);
158
159 return already_read;
160 }
161
162 static int
163 ncp_file_write(struct inode *inode, struct file *file, const char *buf,
164 int count)
165 {
166 int bufsize, already_written;
167 off_t pos;
168 int errno;
169
170 if (inode == NULL)
171 {
172 DPRINTK("ncp_file_write: inode = NULL\n");
173 return -EINVAL;
174 }
175
176 if (!S_ISREG(inode->i_mode))
177 {
178 DPRINTK("ncp_file_write: write to non-file, mode %07o\n",
179 inode->i_mode);
180 return -EINVAL;
181 }
182
183 DPRINTK("ncp_file_write: enter %s\n", NCP_ISTRUCT(inode)->entryName);
184
185 if (count <= 0)
186 {
187 return 0;
188 }
189
190 if ((errno = ncp_make_open(inode, O_RDWR)) != 0)
191 {
192 return errno;
193 }
194
195 pos = file->f_pos;
196
197 if (file->f_flags & O_APPEND)
198 {
199 pos = inode->i_size;
200 }
201
202 bufsize = NCP_SERVER(inode)->buffer_size;
203
204 already_written = 0;
205
206 while (already_written < count)
207 {
208 int written_this_time;
209 int to_write = min(bufsize - (pos % bufsize),
210 count - already_written);
211
212 if (ncp_write(NCP_SERVER(inode), NCP_FINFO(inode)->file_handle,
213 pos, to_write, buf, &written_this_time) != 0)
214 {
215 return -EIO;
216 }
217
218 pos += written_this_time;
219 buf += written_this_time;
220 already_written += written_this_time;
221
222 if (written_this_time < to_write)
223 {
224 break;
225 }
226 }
227
228 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
229 inode->i_dirt = 1;
230
231 file->f_pos = pos;
232
233 if (pos > inode->i_size)
234 {
235 inode->i_size = pos;
236 }
237
238 DPRINTK("ncp_file_write: exit %s\n", NCP_ISTRUCT(inode)->entryName);
239
240 return already_written;
241 }
242
243 static struct file_operations ncp_file_operations = {
244 NULL,
245 ncp_file_read,
246 ncp_file_write,
247 NULL,
248 NULL,
249 ncp_ioctl,
250 ncp_mmap,
251 NULL,
252 NULL,
253 ncp_fsync,
254 };
255
256 struct inode_operations ncp_file_inode_operations = {
257 &ncp_file_operations,
258 NULL,
259 NULL,
260 NULL,
261 NULL,
262 NULL,
263 NULL,
264 NULL,
265 NULL,
266 NULL,
267 NULL,
268 NULL,
269 NULL,
270 NULL
271 };