4 * Copyright (C) 1995, 1996 by Volker Lendecke
5 * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
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>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/sched.h>
21 #include <linux/smp_lock.h>
23 #include <linux/ncp_fs.h>
24 #include "ncplib_kernel.h"
26 static int ncp_fsync(struct file *file, struct dentry *dentry, int datasync)
32 * Open a file with the specified read/write mode.
34 int ncp_make_open(struct inode *inode, int right)
41 printk(KERN_ERR "ncp_make_open: got NULL inode\n");
45 DPRINTK("ncp_make_open: opened=%d, volume # %u, dir entry # %u\n",
46 atomic_read(&NCP_FINFO(inode)->opened),
47 NCP_FINFO(inode)->volNumber,
48 NCP_FINFO(inode)->dirEntNum);
50 mutex_lock(&NCP_FINFO(inode)->open_mutex);
51 if (!atomic_read(&NCP_FINFO(inode)->opened)) {
52 struct ncp_entry_info finfo;
55 /* tries max. rights */
56 finfo.access = O_RDWR;
57 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
58 inode, NULL, OC_MODE_OPEN,
59 0, AR_READ | AR_WRITE, &finfo);
62 /* RDWR did not succeeded, try readonly or writeonly as requested */
65 finfo.access = O_RDONLY;
66 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
67 inode, NULL, OC_MODE_OPEN,
71 finfo.access = O_WRONLY;
72 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
73 inode, NULL, OC_MODE_OPEN,
78 PPRINTK("ncp_make_open: failed, result=%d\n", result);
82 * Update the inode information.
85 ncp_update_inode(inode, &finfo);
86 atomic_set(&NCP_FINFO(inode)->opened, 1);
89 access = NCP_FINFO(inode)->access;
90 PPRINTK("ncp_make_open: file open, access=%x\n", access);
91 if (access == right || access == O_RDWR) {
92 atomic_inc(&NCP_FINFO(inode)->opened);
97 mutex_unlock(&NCP_FINFO(inode)->open_mutex);
103 ncp_file_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
105 struct dentry *dentry = file->f_path.dentry;
106 struct inode *inode = dentry->d_inode;
107 size_t already_read = 0;
114 DPRINTK("ncp_file_read: enter %s/%s\n",
115 dentry->d_parent->d_name.name, dentry->d_name.name);
117 if (!ncp_conn_valid(NCP_SERVER(inode)))
122 if ((ssize_t) count < 0) {
127 if (pos > inode->i_sb->s_maxbytes)
129 if (pos + count > inode->i_sb->s_maxbytes) {
130 count = inode->i_sb->s_maxbytes - pos;
133 error = ncp_make_open(inode, O_RDONLY);
135 DPRINTK(KERN_ERR "ncp_file_read: open failed, error=%d\n", error);
139 bufsize = NCP_SERVER(inode)->buffer_size;
142 freelen = ncp_read_bounce_size(bufsize);
143 freepage = vmalloc(freelen);
147 /* First read in as much as possible for each bufsize. */
148 while (already_read < count) {
150 size_t to_read = min_t(unsigned int,
151 bufsize - (pos % bufsize),
152 count - already_read);
154 error = ncp_read_bounce(NCP_SERVER(inode),
155 NCP_FINFO(inode)->file_handle,
156 pos, to_read, buf, &read_this_time,
159 error = -EIO; /* NW errno -> Linux errno */
162 pos += read_this_time;
163 buf += read_this_time;
164 already_read += read_this_time;
166 if (read_this_time != to_read) {
176 DPRINTK("ncp_file_read: exit %s/%s\n",
177 dentry->d_parent->d_name.name, dentry->d_name.name);
179 ncp_inode_close(inode);
180 return already_read ? already_read : error;
184 ncp_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
186 struct dentry *dentry = file->f_path.dentry;
187 struct inode *inode = dentry->d_inode;
188 size_t already_written = 0;
194 DPRINTK("ncp_file_write: enter %s/%s\n",
195 dentry->d_parent->d_name.name, dentry->d_name.name);
196 if (!ncp_conn_valid(NCP_SERVER(inode)))
198 if ((ssize_t) count < 0)
201 if (file->f_flags & O_APPEND) {
205 if (pos + count > MAX_NON_LFS && !(file->f_flags&O_LARGEFILE)) {
206 if (pos >= MAX_NON_LFS) {
209 if (count > MAX_NON_LFS - (u32)pos) {
210 count = MAX_NON_LFS - (u32)pos;
213 if (pos >= inode->i_sb->s_maxbytes) {
214 if (count || pos > inode->i_sb->s_maxbytes) {
218 if (pos + count > inode->i_sb->s_maxbytes) {
219 count = inode->i_sb->s_maxbytes - pos;
224 errno = ncp_make_open(inode, O_WRONLY);
226 DPRINTK(KERN_ERR "ncp_file_write: open failed, error=%d\n", errno);
229 bufsize = NCP_SERVER(inode)->buffer_size;
233 bouncebuffer = vmalloc(bufsize);
235 errno = -EIO; /* -ENOMEM */
238 while (already_written < count) {
239 int written_this_time;
240 size_t to_write = min_t(unsigned int,
241 bufsize - (pos % bufsize),
242 count - already_written);
244 if (copy_from_user(bouncebuffer, buf, to_write)) {
248 if (ncp_write_kernel(NCP_SERVER(inode),
249 NCP_FINFO(inode)->file_handle,
250 pos, to_write, bouncebuffer, &written_this_time) != 0) {
254 pos += written_this_time;
255 buf += written_this_time;
256 already_written += written_this_time;
258 if (written_this_time != to_write) {
264 file_update_time(file);
268 if (pos > inode->i_size) {
271 DPRINTK("ncp_file_write: exit %s/%s\n",
272 dentry->d_parent->d_name.name, dentry->d_name.name);
274 ncp_inode_close(inode);
275 return already_written ? already_written : errno;
278 static int ncp_release(struct inode *inode, struct file *file) {
279 if (ncp_make_closed(inode)) {
280 DPRINTK("ncp_release: failed to close\n");
285 static loff_t ncp_remote_llseek(struct file *file, loff_t offset, int origin)
289 ret = generic_file_llseek_unlocked(file, offset, origin);
294 const struct file_operations ncp_file_operations =
296 .llseek = ncp_remote_llseek,
297 .read = ncp_file_read,
298 .write = ncp_file_write,
301 .compat_ioctl = ncp_compat_ioctl,
304 .release = ncp_release,
308 const struct inode_operations ncp_file_inode_operations =
310 .setattr = ncp_notify_change,