[PATCH] kconfig documentation update
[linux-2.6/history.git] / fs / ncpfs / file.c
blobe4406a5838056dac3b430c9450f327c4bf4f89f0
1 /*
2 * file.c
4 * Copyright (C) 1995, 1996 by Volker Lendecke
5 * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
7 */
9 #include <asm/uaccess.h>
10 #include <asm/system.h>
12 #include <linux/time.h>
13 #include <linux/kernel.h>
14 #include <linux/errno.h>
15 #include <linux/fcntl.h>
16 #include <linux/stat.h>
17 #include <linux/mm.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/smp_lock.h>
22 #include <linux/ncp_fs.h>
23 #include "ncplib_kernel.h"
25 static int ncp_fsync(struct file *file, struct dentry *dentry, int datasync)
27 return 0;
31 * Open a file with the specified read/write mode.
33 int ncp_make_open(struct inode *inode, int right)
35 int error;
36 int access;
38 error = -EINVAL;
39 if (!inode) {
40 printk(KERN_ERR "ncp_make_open: got NULL inode\n");
41 goto out;
44 DPRINTK("ncp_make_open: opened=%d, volume # %u, dir entry # %u\n",
45 atomic_read(&NCP_FINFO(inode)->opened),
46 NCP_FINFO(inode)->volNumber,
47 NCP_FINFO(inode)->dirEntNum);
48 error = -EACCES;
49 down(&NCP_FINFO(inode)->open_sem);
50 if (!atomic_read(&NCP_FINFO(inode)->opened)) {
51 struct ncp_entry_info finfo;
52 int result;
54 /* tries max. rights */
55 finfo.access = O_RDWR;
56 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
57 inode, NULL, OC_MODE_OPEN,
58 0, AR_READ | AR_WRITE, &finfo);
59 if (!result)
60 goto update;
61 /* RDWR did not succeeded, try readonly or writeonly as requested */
62 switch (right) {
63 case O_RDONLY:
64 finfo.access = O_RDONLY;
65 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
66 inode, NULL, OC_MODE_OPEN,
67 0, AR_READ, &finfo);
68 break;
69 case O_WRONLY:
70 finfo.access = O_WRONLY;
71 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
72 inode, NULL, OC_MODE_OPEN,
73 0, AR_WRITE, &finfo);
74 break;
76 if (result) {
77 PPRINTK("ncp_make_open: failed, result=%d\n", result);
78 goto out_unlock;
81 * Update the inode information.
83 update:
84 ncp_update_inode(inode, &finfo);
85 atomic_set(&NCP_FINFO(inode)->opened, 1);
88 access = NCP_FINFO(inode)->access;
89 PPRINTK("ncp_make_open: file open, access=%x\n", access);
90 if (access == right || access == O_RDWR) {
91 atomic_inc(&NCP_FINFO(inode)->opened);
92 error = 0;
95 out_unlock:
96 up(&NCP_FINFO(inode)->open_sem);
97 out:
98 return error;
101 static ssize_t
102 ncp_file_read(struct file *file, char *buf, size_t count, loff_t *ppos)
104 struct dentry *dentry = file->f_dentry;
105 struct inode *inode = dentry->d_inode;
106 size_t already_read = 0;
107 off_t pos;
108 size_t bufsize;
109 int error;
110 void* freepage;
111 size_t freelen;
113 DPRINTK("ncp_file_read: enter %s/%s\n",
114 dentry->d_parent->d_name.name, dentry->d_name.name);
116 if (!ncp_conn_valid(NCP_SERVER(inode)))
117 return -EIO;
118 if (!S_ISREG(inode->i_mode)) {
119 DPRINTK("ncp_file_read: read from non-file, mode %07o\n",
120 inode->i_mode);
121 return -EINVAL;
124 pos = *ppos;
126 if ((ssize_t) count < 0) {
127 return -EINVAL;
129 if (!count)
130 return 0;
131 if (pos > inode->i_sb->s_maxbytes)
132 return 0;
133 if (pos + count > inode->i_sb->s_maxbytes) {
134 count = inode->i_sb->s_maxbytes - pos;
137 error = ncp_make_open(inode, O_RDONLY);
138 if (error) {
139 DPRINTK(KERN_ERR "ncp_file_read: open failed, error=%d\n", error);
140 return error;
143 bufsize = NCP_SERVER(inode)->buffer_size;
145 error = -EIO;
146 freelen = ncp_read_bounce_size(bufsize);
147 freepage = vmalloc(freelen);
148 if (!freepage)
149 goto outrel;
150 error = 0;
151 /* First read in as much as possible for each bufsize. */
152 while (already_read < count) {
153 int read_this_time;
154 size_t to_read = min_t(unsigned int,
155 bufsize - (pos % bufsize),
156 count - already_read);
158 error = ncp_read_bounce(NCP_SERVER(inode),
159 NCP_FINFO(inode)->file_handle,
160 pos, to_read, buf, &read_this_time,
161 freepage, freelen);
162 if (error) {
163 error = -EIO; /* NW errno -> Linux errno */
164 break;
166 pos += read_this_time;
167 buf += read_this_time;
168 already_read += read_this_time;
170 if (read_this_time != to_read) {
171 break;
174 vfree(freepage);
176 *ppos = pos;
178 if (!IS_RDONLY(inode)) {
179 inode->i_atime = CURRENT_TIME;
182 DPRINTK("ncp_file_read: exit %s/%s\n",
183 dentry->d_parent->d_name.name, dentry->d_name.name);
184 outrel:
185 ncp_inode_close(inode);
186 return already_read ? already_read : error;
189 static ssize_t
190 ncp_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
192 struct dentry *dentry = file->f_dentry;
193 struct inode *inode = dentry->d_inode;
194 size_t already_written = 0;
195 off_t pos;
196 size_t bufsize;
197 int errno;
198 void* bouncebuffer;
200 DPRINTK("ncp_file_write: enter %s/%s\n",
201 dentry->d_parent->d_name.name, dentry->d_name.name);
202 if (!ncp_conn_valid(NCP_SERVER(inode)))
203 return -EIO;
204 if (!S_ISREG(inode->i_mode)) {
205 DPRINTK("ncp_file_write: write to non-file, mode %07o\n",
206 inode->i_mode);
207 return -EINVAL;
209 if ((ssize_t) count < 0)
210 return -EINVAL;
211 pos = *ppos;
212 if (file->f_flags & O_APPEND) {
213 pos = inode->i_size;
216 if (pos + count > MAX_NON_LFS && !(file->f_flags&O_LARGEFILE)) {
217 if (pos >= MAX_NON_LFS) {
218 send_sig(SIGXFSZ, current, 0);
219 return -EFBIG;
221 if (count > MAX_NON_LFS - (u32)pos) {
222 count = MAX_NON_LFS - (u32)pos;
225 if (pos >= inode->i_sb->s_maxbytes) {
226 if (count || pos > inode->i_sb->s_maxbytes) {
227 send_sig(SIGXFSZ, current, 0);
228 return -EFBIG;
231 if (pos + count > inode->i_sb->s_maxbytes) {
232 count = inode->i_sb->s_maxbytes - pos;
235 if (!count)
236 return 0;
237 errno = ncp_make_open(inode, O_WRONLY);
238 if (errno) {
239 DPRINTK(KERN_ERR "ncp_file_write: open failed, error=%d\n", errno);
240 return errno;
242 bufsize = NCP_SERVER(inode)->buffer_size;
244 already_written = 0;
246 bouncebuffer = vmalloc(bufsize);
247 if (!bouncebuffer) {
248 errno = -EIO; /* -ENOMEM */
249 goto outrel;
251 while (already_written < count) {
252 int written_this_time;
253 size_t to_write = min_t(unsigned int,
254 bufsize - (pos % bufsize),
255 count - already_written);
257 if (copy_from_user(bouncebuffer, buf, to_write)) {
258 errno = -EFAULT;
259 break;
261 if (ncp_write_kernel(NCP_SERVER(inode),
262 NCP_FINFO(inode)->file_handle,
263 pos, to_write, bouncebuffer, &written_this_time) != 0) {
264 errno = -EIO;
265 break;
267 pos += written_this_time;
268 buf += written_this_time;
269 already_written += written_this_time;
271 if (written_this_time != to_write) {
272 break;
275 vfree(bouncebuffer);
276 inode->i_mtime = inode->i_atime = CURRENT_TIME;
278 *ppos = pos;
280 if (pos > inode->i_size) {
281 inode->i_size = pos;
283 DPRINTK("ncp_file_write: exit %s/%s\n",
284 dentry->d_parent->d_name.name, dentry->d_name.name);
285 outrel:
286 ncp_inode_close(inode);
287 return already_written ? already_written : errno;
290 static int ncp_release(struct inode *inode, struct file *file) {
291 if (ncp_make_closed(inode)) {
292 DPRINTK("ncp_release: failed to close\n");
294 return 0;
297 struct file_operations ncp_file_operations =
299 .llseek = remote_llseek,
300 .read = ncp_file_read,
301 .write = ncp_file_write,
302 .ioctl = ncp_ioctl,
303 .mmap = ncp_mmap,
304 .release = ncp_release,
305 .fsync = ncp_fsync,
308 struct inode_operations ncp_file_inode_operations =
310 .setattr = ncp_notify_change,