OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [fs/] [ncpfs/] [file.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.