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/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)
31 * Open a file with the specified read/write mode.
33 int ncp_make_open(struct inode *inode, int right)
40 printk(KERN_ERR "ncp_make_open: got NULL inode\n");
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);
49 down(&NCP_FINFO(inode)->open_sem);
50 if (!atomic_read(&NCP_FINFO(inode)->opened)) {
51 struct ncp_entry_info finfo;
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);
61 /* RDWR did not succeeded, try readonly or writeonly as requested */
64 finfo.access = O_RDONLY;
65 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
66 inode, NULL, OC_MODE_OPEN,
70 finfo.access = O_WRONLY;
71 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
72 inode, NULL, OC_MODE_OPEN,
77 PPRINTK("ncp_make_open: failed, result=%d\n", result);
81 * Update the inode information.
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);
96 up(&NCP_FINFO(inode)->open_sem);
102 ncp_file_read(struct file *file, char __user *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;
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)))
121 if ((ssize_t) count < 0) {
126 if (pos > inode->i_sb->s_maxbytes)
128 if (pos + count > inode->i_sb->s_maxbytes) {
129 count = inode->i_sb->s_maxbytes - pos;
132 error = ncp_make_open(inode, O_RDONLY);
134 DPRINTK(KERN_ERR "ncp_file_read: open failed, error=%d\n", error);
138 bufsize = NCP_SERVER(inode)->buffer_size;
141 freelen = ncp_read_bounce_size(bufsize);
142 freepage = vmalloc(freelen);
146 /* First read in as much as possible for each bufsize. */
147 while (already_read < count) {
149 size_t to_read = min_t(unsigned int,
150 bufsize - (pos % bufsize),
151 count - already_read);
153 error = ncp_read_bounce(NCP_SERVER(inode),
154 NCP_FINFO(inode)->file_handle,
155 pos, to_read, buf, &read_this_time,
158 error = -EIO; /* NW errno -> Linux errno */
161 pos += read_this_time;
162 buf += read_this_time;
163 already_read += read_this_time;
165 if (read_this_time != to_read) {
173 #warning MEF removed some READONLY MOUNT support -- look for it again in vs1.9.3
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_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) {
207 send_sig(SIGXFSZ, current, 0);
210 if (count > MAX_NON_LFS - (u32)pos) {
211 count = MAX_NON_LFS - (u32)pos;
214 if (pos >= inode->i_sb->s_maxbytes) {
215 if (count || pos > inode->i_sb->s_maxbytes) {
216 send_sig(SIGXFSZ, current, 0);
220 if (pos + count > inode->i_sb->s_maxbytes) {
221 count = inode->i_sb->s_maxbytes - pos;
226 errno = ncp_make_open(inode, O_WRONLY);
228 DPRINTK(KERN_ERR "ncp_file_write: open failed, error=%d\n", errno);
231 bufsize = NCP_SERVER(inode)->buffer_size;
235 bouncebuffer = vmalloc(bufsize);
237 errno = -EIO; /* -ENOMEM */
240 while (already_written < count) {
241 int written_this_time;
242 size_t to_write = min_t(unsigned int,
243 bufsize - (pos % bufsize),
244 count - already_written);
246 if (copy_from_user(bouncebuffer, buf, to_write)) {
250 if (ncp_write_kernel(NCP_SERVER(inode),
251 NCP_FINFO(inode)->file_handle,
252 pos, to_write, bouncebuffer, &written_this_time) != 0) {
256 pos += written_this_time;
257 buf += written_this_time;
258 already_written += written_this_time;
260 if (written_this_time != to_write) {
266 inode_update_time(inode, 1);
270 if (pos > inode->i_size) {
273 DPRINTK("ncp_file_write: exit %s/%s\n",
274 dentry->d_parent->d_name.name, dentry->d_name.name);
276 ncp_inode_close(inode);
277 return already_written ? already_written : errno;
280 static int ncp_release(struct inode *inode, struct file *file) {
281 if (ncp_make_closed(inode)) {
282 DPRINTK("ncp_release: failed to close\n");
287 struct file_operations ncp_file_operations =
289 .llseek = remote_llseek,
290 .read = ncp_file_read,
291 .write = ncp_file_write,
294 .release = ncp_release,
298 struct inode_operations ncp_file_inode_operations =
300 .setattr = ncp_notify_change,