ptrace: cleanup arch_ptrace() on xtensa
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ncpfs / file.c
blob6c754f70c5296be3fbe5c323bbb985d7ee326512
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/vmalloc.h>
19 #include <linux/sched.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, 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 mutex_lock(&NCP_FINFO(inode)->open_mutex);
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 mutex_unlock(&NCP_FINFO(inode)->open_mutex);
97 out:
98 return error;
101 static ssize_t
102 ncp_file_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
104 struct dentry *dentry = file->f_path.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 pos = *ppos;
118 if ((ssize_t) count < 0) {
119 return -EINVAL;
121 if (!count)
122 return 0;
123 if (pos > inode->i_sb->s_maxbytes)
124 return 0;
125 if (pos + count > inode->i_sb->s_maxbytes) {
126 count = inode->i_sb->s_maxbytes - pos;
129 error = ncp_make_open(inode, O_RDONLY);
130 if (error) {
131 DPRINTK(KERN_ERR "ncp_file_read: open failed, error=%d\n", error);
132 return error;
135 bufsize = NCP_SERVER(inode)->buffer_size;
137 error = -EIO;
138 freelen = ncp_read_bounce_size(bufsize);
139 freepage = vmalloc(freelen);
140 if (!freepage)
141 goto outrel;
142 error = 0;
143 /* First read in as much as possible for each bufsize. */
144 while (already_read < count) {
145 int read_this_time;
146 size_t to_read = min_t(unsigned int,
147 bufsize - (pos % bufsize),
148 count - already_read);
150 error = ncp_read_bounce(NCP_SERVER(inode),
151 NCP_FINFO(inode)->file_handle,
152 pos, to_read, buf, &read_this_time,
153 freepage, freelen);
154 if (error) {
155 error = -EIO; /* NW errno -> Linux errno */
156 break;
158 pos += read_this_time;
159 buf += read_this_time;
160 already_read += read_this_time;
162 if (read_this_time != to_read) {
163 break;
166 vfree(freepage);
168 *ppos = pos;
170 file_accessed(file);
172 DPRINTK("ncp_file_read: exit %s/%s\n",
173 dentry->d_parent->d_name.name, dentry->d_name.name);
174 outrel:
175 ncp_inode_close(inode);
176 return already_read ? already_read : error;
179 static ssize_t
180 ncp_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
182 struct dentry *dentry = file->f_path.dentry;
183 struct inode *inode = dentry->d_inode;
184 size_t already_written = 0;
185 off_t pos;
186 size_t bufsize;
187 int errno;
188 void* bouncebuffer;
190 DPRINTK("ncp_file_write: enter %s/%s\n",
191 dentry->d_parent->d_name.name, dentry->d_name.name);
192 if ((ssize_t) count < 0)
193 return -EINVAL;
194 pos = *ppos;
195 if (file->f_flags & O_APPEND) {
196 pos = i_size_read(inode);
199 if (pos + count > MAX_NON_LFS && !(file->f_flags&O_LARGEFILE)) {
200 if (pos >= MAX_NON_LFS) {
201 return -EFBIG;
203 if (count > MAX_NON_LFS - (u32)pos) {
204 count = MAX_NON_LFS - (u32)pos;
207 if (pos >= inode->i_sb->s_maxbytes) {
208 if (count || pos > inode->i_sb->s_maxbytes) {
209 return -EFBIG;
212 if (pos + count > inode->i_sb->s_maxbytes) {
213 count = inode->i_sb->s_maxbytes - pos;
216 if (!count)
217 return 0;
218 errno = ncp_make_open(inode, O_WRONLY);
219 if (errno) {
220 DPRINTK(KERN_ERR "ncp_file_write: open failed, error=%d\n", errno);
221 return errno;
223 bufsize = NCP_SERVER(inode)->buffer_size;
225 already_written = 0;
227 bouncebuffer = vmalloc(bufsize);
228 if (!bouncebuffer) {
229 errno = -EIO; /* -ENOMEM */
230 goto outrel;
232 while (already_written < count) {
233 int written_this_time;
234 size_t to_write = min_t(unsigned int,
235 bufsize - (pos % bufsize),
236 count - already_written);
238 if (copy_from_user(bouncebuffer, buf, to_write)) {
239 errno = -EFAULT;
240 break;
242 if (ncp_write_kernel(NCP_SERVER(inode),
243 NCP_FINFO(inode)->file_handle,
244 pos, to_write, bouncebuffer, &written_this_time) != 0) {
245 errno = -EIO;
246 break;
248 pos += written_this_time;
249 buf += written_this_time;
250 already_written += written_this_time;
252 if (written_this_time != to_write) {
253 break;
256 vfree(bouncebuffer);
258 file_update_time(file);
260 *ppos = pos;
262 if (pos > i_size_read(inode)) {
263 mutex_lock(&inode->i_mutex);
264 if (pos > i_size_read(inode))
265 i_size_write(inode, pos);
266 mutex_unlock(&inode->i_mutex);
268 DPRINTK("ncp_file_write: exit %s/%s\n",
269 dentry->d_parent->d_name.name, dentry->d_name.name);
270 outrel:
271 ncp_inode_close(inode);
272 return already_written ? already_written : errno;
275 static int ncp_release(struct inode *inode, struct file *file) {
276 if (ncp_make_closed(inode)) {
277 DPRINTK("ncp_release: failed to close\n");
279 return 0;
282 const struct file_operations ncp_file_operations =
284 .llseek = generic_file_llseek,
285 .read = ncp_file_read,
286 .write = ncp_file_write,
287 .unlocked_ioctl = ncp_ioctl,
288 #ifdef CONFIG_COMPAT
289 .compat_ioctl = ncp_compat_ioctl,
290 #endif
291 .mmap = ncp_mmap,
292 .release = ncp_release,
293 .fsync = ncp_fsync,
296 const struct inode_operations ncp_file_inode_operations =
298 .setattr = ncp_notify_change,