4 * Copyright (C) 1995, 1996 by Volker Lendecke
5 * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <asm/uaccess.h>
13 #include <linux/time.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/fcntl.h>
17 #include <linux/stat.h>
19 #include <linux/vmalloc.h>
20 #include <linux/sched.h>
24 static int ncp_fsync(struct file *file, loff_t start, loff_t end, int datasync)
26 return filemap_write_and_wait_range(file->f_mapping, start, end);
30 * Open a file with the specified read/write mode.
32 int ncp_make_open(struct inode *inode, int right)
39 pr_err("%s: got NULL inode\n", __func__);
43 ncp_dbg(1, "opened=%d, volume # %u, dir entry # %u\n",
44 atomic_read(&NCP_FINFO(inode)->opened),
45 NCP_FINFO(inode)->volNumber,
46 NCP_FINFO(inode)->dirEntNum);
48 mutex_lock(&NCP_FINFO(inode)->open_mutex);
49 if (!atomic_read(&NCP_FINFO(inode)->opened)) {
50 struct ncp_entry_info finfo;
53 /* tries max. rights */
54 finfo.access = O_RDWR;
55 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
56 inode, NULL, OC_MODE_OPEN,
57 0, AR_READ | AR_WRITE, &finfo);
60 /* RDWR did not succeeded, try readonly or writeonly as requested */
63 finfo.access = O_RDONLY;
64 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
65 inode, NULL, OC_MODE_OPEN,
69 finfo.access = O_WRONLY;
70 result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
71 inode, NULL, OC_MODE_OPEN,
76 ncp_vdbg("failed, result=%d\n", result);
80 * Update the inode information.
83 ncp_update_inode(inode, &finfo);
84 atomic_set(&NCP_FINFO(inode)->opened, 1);
87 access = NCP_FINFO(inode)->access;
88 ncp_vdbg("file open, access=%x\n", access);
89 if (access == right || access == O_RDWR) {
90 atomic_inc(&NCP_FINFO(inode)->opened);
95 mutex_unlock(&NCP_FINFO(inode)->open_mutex);
101 ncp_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
103 struct file *file = iocb->ki_filp;
104 struct inode *inode = file_inode(file);
105 size_t already_read = 0;
106 off_t pos = iocb->ki_pos;
112 ncp_dbg(1, "enter %pD2\n", file);
114 if (!iov_iter_count(to))
116 if (pos > inode->i_sb->s_maxbytes)
118 iov_iter_truncate(to, inode->i_sb->s_maxbytes - pos);
120 error = ncp_make_open(inode, O_RDONLY);
122 ncp_dbg(1, "open failed, error=%d\n", error);
126 bufsize = NCP_SERVER(inode)->buffer_size;
129 freelen = ncp_read_bounce_size(bufsize);
130 freepage = vmalloc(freelen);
134 /* First read in as much as possible for each bufsize. */
135 while (iov_iter_count(to)) {
137 size_t to_read = min_t(size_t,
138 bufsize - (pos % bufsize),
141 error = ncp_read_bounce(NCP_SERVER(inode),
142 NCP_FINFO(inode)->file_handle,
143 pos, to_read, to, &read_this_time,
146 error = -EIO; /* NW errno -> Linux errno */
149 pos += read_this_time;
150 already_read += read_this_time;
152 if (read_this_time != to_read)
161 ncp_dbg(1, "exit %pD2\n", file);
163 ncp_inode_close(inode);
164 return already_read ? already_read : error;
168 ncp_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
170 struct file *file = iocb->ki_filp;
171 struct inode *inode = file_inode(file);
172 size_t already_written = 0;
178 ncp_dbg(1, "enter %pD2\n", file);
179 errno = generic_write_checks(iocb, from);
183 errno = ncp_make_open(inode, O_WRONLY);
185 ncp_dbg(1, "open failed, error=%d\n", errno);
188 bufsize = NCP_SERVER(inode)->buffer_size;
190 errno = file_update_time(file);
194 bouncebuffer = vmalloc(bufsize);
196 errno = -EIO; /* -ENOMEM */
200 while (iov_iter_count(from)) {
201 int written_this_time;
202 size_t to_write = min_t(size_t,
203 bufsize - (pos % bufsize),
204 iov_iter_count(from));
206 if (copy_from_iter(bouncebuffer, to_write, from) != to_write) {
210 if (ncp_write_kernel(NCP_SERVER(inode),
211 NCP_FINFO(inode)->file_handle,
212 pos, to_write, bouncebuffer, &written_this_time) != 0) {
216 pos += written_this_time;
217 already_written += written_this_time;
219 if (written_this_time != to_write)
226 if (pos > i_size_read(inode)) {
228 if (pos > i_size_read(inode))
229 i_size_write(inode, pos);
232 ncp_dbg(1, "exit %pD2\n", file);
234 ncp_inode_close(inode);
235 return already_written ? already_written : errno;
238 static int ncp_release(struct inode *inode, struct file *file) {
239 if (ncp_make_closed(inode)) {
240 ncp_dbg(1, "failed to close\n");
245 const struct file_operations ncp_file_operations =
247 .llseek = generic_file_llseek,
248 .read_iter = ncp_file_read_iter,
249 .write_iter = ncp_file_write_iter,
250 .unlocked_ioctl = ncp_ioctl,
252 .compat_ioctl = ncp_compat_ioctl,
255 .release = ncp_release,
259 const struct inode_operations ncp_file_inode_operations =
261 .setattr = ncp_notify_change,