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>
11 #include <linux/time.h>
12 #include <linux/kernel.h>
13 #include <linux/errno.h>
14 #include <linux/fcntl.h>
15 #include <linux/stat.h>
17 #include <linux/vmalloc.h>
18 #include <linux/sched.h>
22 static int ncp_fsync(struct file *file, loff_t start, loff_t end, int datasync)
24 return filemap_write_and_wait_range(file->f_mapping, start, end);
28 * Open a file with the specified read/write mode.
30 int ncp_make_open(struct inode *inode, int right)
37 printk(KERN_ERR "ncp_make_open: got NULL inode\n");
41 DPRINTK("ncp_make_open: opened=%d, volume # %u, dir entry # %u\n",
42 atomic_read(&NCP_FINFO(inode)->opened),
43 NCP_FINFO(inode)->volNumber,
44 NCP_FINFO(inode)->dirEntNum);
46 mutex_lock(&NCP_FINFO(inode)->open_mutex);
47 if (!atomic_read(&NCP_FINFO(inode)->opened)) {
48 struct ncp_entry_info finfo;
51 /* tries max. rights */
52 finfo.access = O_RDWR;
53 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
54 inode, NULL, OC_MODE_OPEN,
55 0, AR_READ | AR_WRITE, &finfo);
58 /* RDWR did not succeeded, try readonly or writeonly as requested */
61 finfo.access = O_RDONLY;
62 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
63 inode, NULL, OC_MODE_OPEN,
67 finfo.access = O_WRONLY;
68 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
69 inode, NULL, OC_MODE_OPEN,
74 PPRINTK("ncp_make_open: failed, result=%d\n", result);
78 * Update the inode information.
81 ncp_update_inode(inode, &finfo);
82 atomic_set(&NCP_FINFO(inode)->opened, 1);
85 access = NCP_FINFO(inode)->access;
86 PPRINTK("ncp_make_open: file open, access=%x\n", access);
87 if (access == right || access == O_RDWR) {
88 atomic_inc(&NCP_FINFO(inode)->opened);
93 mutex_unlock(&NCP_FINFO(inode)->open_mutex);
99 ncp_file_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
101 struct dentry *dentry = file->f_path.dentry;
102 struct inode *inode = dentry->d_inode;
103 size_t already_read = 0;
110 DPRINTK("ncp_file_read: enter %pd2\n", dentry);
114 if ((ssize_t) count < 0) {
119 if (pos > inode->i_sb->s_maxbytes)
121 if (pos + count > inode->i_sb->s_maxbytes) {
122 count = inode->i_sb->s_maxbytes - pos;
125 error = ncp_make_open(inode, O_RDONLY);
127 DPRINTK(KERN_ERR "ncp_file_read: open failed, error=%d\n", error);
131 bufsize = NCP_SERVER(inode)->buffer_size;
134 freelen = ncp_read_bounce_size(bufsize);
135 freepage = vmalloc(freelen);
139 /* First read in as much as possible for each bufsize. */
140 while (already_read < count) {
142 size_t to_read = min_t(unsigned int,
143 bufsize - (pos % bufsize),
144 count - already_read);
146 error = ncp_read_bounce(NCP_SERVER(inode),
147 NCP_FINFO(inode)->file_handle,
148 pos, to_read, buf, &read_this_time,
151 error = -EIO; /* NW errno -> Linux errno */
154 pos += read_this_time;
155 buf += read_this_time;
156 already_read += read_this_time;
158 if (read_this_time != to_read) {
168 DPRINTK("ncp_file_read: exit %pd2\n", dentry);
170 ncp_inode_close(inode);
171 return already_read ? already_read : error;
175 ncp_file_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
177 struct dentry *dentry = file->f_path.dentry;
178 struct inode *inode = dentry->d_inode;
179 size_t already_written = 0;
185 DPRINTK("ncp_file_write: enter %pd2\n", dentry);
186 if ((ssize_t) count < 0)
189 if (file->f_flags & O_APPEND) {
190 pos = i_size_read(inode);
193 if (pos + count > MAX_NON_LFS && !(file->f_flags&O_LARGEFILE)) {
194 if (pos >= MAX_NON_LFS) {
197 if (count > MAX_NON_LFS - (u32)pos) {
198 count = MAX_NON_LFS - (u32)pos;
201 if (pos >= inode->i_sb->s_maxbytes) {
202 if (count || pos > inode->i_sb->s_maxbytes) {
206 if (pos + count > inode->i_sb->s_maxbytes) {
207 count = inode->i_sb->s_maxbytes - pos;
212 errno = ncp_make_open(inode, O_WRONLY);
214 DPRINTK(KERN_ERR "ncp_file_write: open failed, error=%d\n", errno);
217 bufsize = NCP_SERVER(inode)->buffer_size;
221 errno = file_update_time(file);
225 bouncebuffer = vmalloc(bufsize);
227 errno = -EIO; /* -ENOMEM */
230 while (already_written < count) {
231 int written_this_time;
232 size_t to_write = min_t(unsigned int,
233 bufsize - (pos % bufsize),
234 count - already_written);
236 if (copy_from_user(bouncebuffer, buf, to_write)) {
240 if (ncp_write_kernel(NCP_SERVER(inode),
241 NCP_FINFO(inode)->file_handle,
242 pos, to_write, bouncebuffer, &written_this_time) != 0) {
246 pos += written_this_time;
247 buf += written_this_time;
248 already_written += written_this_time;
250 if (written_this_time != to_write) {
258 if (pos > i_size_read(inode)) {
259 mutex_lock(&inode->i_mutex);
260 if (pos > i_size_read(inode))
261 i_size_write(inode, pos);
262 mutex_unlock(&inode->i_mutex);
264 DPRINTK("ncp_file_write: exit %pd2\n", dentry);
266 ncp_inode_close(inode);
267 return already_written ? already_written : errno;
270 static int ncp_release(struct inode *inode, struct file *file) {
271 if (ncp_make_closed(inode)) {
272 DPRINTK("ncp_release: failed to close\n");
277 const struct file_operations ncp_file_operations =
279 .llseek = generic_file_llseek,
280 .read = ncp_file_read,
281 .write = ncp_file_write,
282 .unlocked_ioctl = ncp_ioctl,
284 .compat_ioctl = ncp_compat_ioctl,
287 .release = ncp_release,
291 const struct inode_operations ncp_file_inode_operations =
293 .setattr = ncp_notify_change,