1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* linux/fs/nfs/file.c
*
* Copyright (C) 1992 Rick Sladkey
*
* nfs regular file handling functions
*/
#include <asm/segment.h>
#include <asm/system.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/fcntl.h>
#include <linux/stat.h>
#include <linux/mm.h>
#include <linux/nfs_fs.h>
#include <linux/malloc.h>
static int nfs_file_read(struct inode *, struct file *, char *, int);
static int nfs_file_write(struct inode *, struct file *, char *, int);
static int nfs_fsync(struct inode *, struct file *);
extern int nfs_mmap(struct inode * inode, struct file * file,
unsigned long addr, size_t len, int prot, unsigned long off);
static struct file_operations nfs_file_operations = {
NULL, /* lseek - default */
nfs_file_read, /* read */
nfs_file_write, /* write */
NULL, /* readdir - bad */
NULL, /* select - default */
NULL, /* ioctl - default */
nfs_mmap, /* mmap */
NULL, /* no special open is needed */
NULL, /* release */
nfs_fsync, /* fsync */
};
struct inode_operations nfs_file_inode_operations = {
&nfs_file_operations, /* default file operations */
NULL, /* create */
NULL, /* lookup */
NULL, /* link */
NULL, /* unlink */
NULL, /* symlink */
NULL, /* mkdir */
NULL, /* rmdir */
NULL, /* mknod */
NULL, /* rename */
NULL, /* readlink */
NULL, /* follow_link */
NULL, /* bmap */
NULL /* truncate */
};
static int nfs_fsync(struct inode *inode, struct file *file)
{
return 0;
}
static int nfs_file_read(struct inode *inode, struct file *file, char *buf,
int count)
{
int result;
int hunk;
int i;
int n;
struct nfs_fattr fattr;
char *data;
off_t pos;
if (!inode) {
printk("nfs_file_read: inode = NULL\n");
return -EINVAL;
}
if (!S_ISREG(inode->i_mode)) {
printk("nfs_file_read: read from non-file, mode %07o\n",
inode->i_mode);
return -EINVAL;
}
pos = file->f_pos;
if (file->f_pos + count > inode->i_size)
count = inode->i_size - pos;
if (count <= 0)
return 0;
n = NFS_SERVER(inode)->rsize;
data = (char *) kmalloc(n, GFP_KERNEL);
for (i = 0; i < count; i += n) {
hunk = count - i;
if (hunk > n)
hunk = n;
result = nfs_proc_read(NFS_SERVER(inode), NFS_FH(inode),
pos, hunk, data, &fattr);
if (result < 0) {
kfree_s(data, n);
return result;
}
memcpy_tofs(buf, data, result);
pos += result;
buf += result;
if (result < n) {
i += result;
break;
}
}
file->f_pos = pos;
kfree_s(data, n);
nfs_refresh_inode(inode, &fattr);
return i;
}
static int nfs_file_write(struct inode *inode, struct file *file, char *buf,
int count)
{
int result;
int hunk;
int i;
int n;
struct nfs_fattr fattr;
char *data;
int pos;
if (!inode) {
printk("nfs_file_write: inode = NULL\n");
return -EINVAL;
}
if (!S_ISREG(inode->i_mode)) {
printk("nfs_file_write: write to non-file, mode %07o\n",
inode->i_mode);
return -EINVAL;
}
if (count <= 0)
return 0;
pos = file->f_pos;
if (file->f_flags & O_APPEND)
pos = inode->i_size;
n = NFS_SERVER(inode)->wsize;
data = (char *) kmalloc(n, GFP_KERNEL);
for (i = 0; i < count; i += n) {
hunk = count - i;
if (hunk >= n)
hunk = n;
memcpy_fromfs(data, buf, hunk);
result = nfs_proc_write(NFS_SERVER(inode), NFS_FH(inode),
pos, hunk, data, &fattr);
if (result < 0) {
kfree_s(data, n);
return result;
}
pos += hunk;
buf += hunk;
if (hunk < n) {
i += hunk;
break;
}
}
file->f_pos = pos;
kfree_s(data, n);
nfs_refresh_inode(inode, &fattr);
return i;
}