1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1995, 1996 by Volker Lendecke
6 * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/uaccess.h>
14 #include <linux/time.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/fcntl.h>
18 #include <linux/stat.h>
20 #include <linux/vmalloc.h>
21 #include <linux/sched.h>
25 static int ncp_fsync(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
27 return file_write_and_wait_range(file
, start
, end
);
31 * Open a file with the specified read/write mode.
33 int ncp_make_open(struct inode
*inode
, int right
)
40 pr_err("%s: got NULL inode\n", __func__
);
44 ncp_dbg(1, "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 mutex_lock(&NCP_FINFO(inode
)->open_mutex
);
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 ncp_vdbg("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 ncp_vdbg("file open, access=%x\n", access
);
90 if (access
== right
|| access
== O_RDWR
) {
91 atomic_inc(&NCP_FINFO(inode
)->opened
);
96 mutex_unlock(&NCP_FINFO(inode
)->open_mutex
);
102 ncp_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
104 struct file
*file
= iocb
->ki_filp
;
105 struct inode
*inode
= file_inode(file
);
106 size_t already_read
= 0;
107 off_t pos
= iocb
->ki_pos
;
113 ncp_dbg(1, "enter %pD2\n", file
);
115 if (!iov_iter_count(to
))
117 if (pos
> inode
->i_sb
->s_maxbytes
)
119 iov_iter_truncate(to
, inode
->i_sb
->s_maxbytes
- pos
);
121 error
= ncp_make_open(inode
, O_RDONLY
);
123 ncp_dbg(1, "open failed, error=%d\n", error
);
127 bufsize
= NCP_SERVER(inode
)->buffer_size
;
130 freelen
= ncp_read_bounce_size(bufsize
);
131 freepage
= vmalloc(freelen
);
135 /* First read in as much as possible for each bufsize. */
136 while (iov_iter_count(to
)) {
138 size_t to_read
= min_t(size_t,
139 bufsize
- (pos
% bufsize
),
142 error
= ncp_read_bounce(NCP_SERVER(inode
),
143 NCP_FINFO(inode
)->file_handle
,
144 pos
, to_read
, to
, &read_this_time
,
147 error
= -EIO
; /* NW errno -> Linux errno */
150 pos
+= read_this_time
;
151 already_read
+= read_this_time
;
153 if (read_this_time
!= to_read
)
162 ncp_dbg(1, "exit %pD2\n", file
);
164 ncp_inode_close(inode
);
165 return already_read
? already_read
: error
;
169 ncp_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
171 struct file
*file
= iocb
->ki_filp
;
172 struct inode
*inode
= file_inode(file
);
173 size_t already_written
= 0;
179 ncp_dbg(1, "enter %pD2\n", file
);
180 errno
= generic_write_checks(iocb
, from
);
184 errno
= ncp_make_open(inode
, O_WRONLY
);
186 ncp_dbg(1, "open failed, error=%d\n", errno
);
189 bufsize
= NCP_SERVER(inode
)->buffer_size
;
191 errno
= file_update_time(file
);
195 bouncebuffer
= vmalloc(bufsize
);
197 errno
= -EIO
; /* -ENOMEM */
201 while (iov_iter_count(from
)) {
202 int written_this_time
;
203 size_t to_write
= min_t(size_t,
204 bufsize
- (pos
% bufsize
),
205 iov_iter_count(from
));
207 if (!copy_from_iter_full(bouncebuffer
, to_write
, from
)) {
211 if (ncp_write_kernel(NCP_SERVER(inode
),
212 NCP_FINFO(inode
)->file_handle
,
213 pos
, to_write
, bouncebuffer
, &written_this_time
) != 0) {
217 pos
+= written_this_time
;
218 already_written
+= written_this_time
;
220 if (written_this_time
!= to_write
)
227 if (pos
> i_size_read(inode
)) {
229 if (pos
> i_size_read(inode
))
230 i_size_write(inode
, pos
);
233 ncp_dbg(1, "exit %pD2\n", file
);
235 ncp_inode_close(inode
);
236 return already_written
? already_written
: errno
;
239 static int ncp_release(struct inode
*inode
, struct file
*file
) {
240 if (ncp_make_closed(inode
)) {
241 ncp_dbg(1, "failed to close\n");
246 const struct file_operations ncp_file_operations
=
248 .llseek
= generic_file_llseek
,
249 .read_iter
= ncp_file_read_iter
,
250 .write_iter
= ncp_file_write_iter
,
251 .unlocked_ioctl
= ncp_ioctl
,
253 .compat_ioctl
= ncp_compat_ioctl
,
256 .release
= ncp_release
,
260 const struct inode_operations ncp_file_inode_operations
=
262 .setattr
= ncp_notify_change
,