Blame view

fs/ncpfs/file.c 5.78 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
7
8
  /*
   *  file.c
   *
   *  Copyright (C) 1995, 1996 by Volker Lendecke
   *  Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache
   *
   */
b41f8b84d   Joe Perches   ncpfs: Add pr_fmt...
9
  #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
10
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
14
15
16
17
  
  #include <linux/time.h>
  #include <linux/kernel.h>
  #include <linux/errno.h>
  #include <linux/fcntl.h>
  #include <linux/stat.h>
  #include <linux/mm.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  #include <linux/vmalloc.h>
e8edc6e03   Alexey Dobriyan   Detach sched.h fr...
19
  #include <linux/sched.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20

32c419d95   Al Viro   move internal-onl...
21
  #include "ncp_fs.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22

02c24a821   Josef Bacik   fs: push i_mutex ...
23
  static int ncp_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
  {
3b49c9a1e   Jeff Layton   fs: convert a pil...
25
  	return file_write_and_wait_range(file, start, end);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
30
31
32
33
34
35
36
37
  }
  
  /*
   * Open a file with the specified read/write mode.
   */
  int ncp_make_open(struct inode *inode, int right)
  {
  	int error;
  	int access;
  
  	error = -EINVAL;
  	if (!inode) {
b41f8b84d   Joe Perches   ncpfs: Add pr_fmt...
38
39
  		pr_err("%s: got NULL inode
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
  		goto out;
  	}
d3b73ca1b   Joe Perches   ncpfs: convert DP...
42
43
  	ncp_dbg(1, "opened=%d, volume # %u, dir entry # %u
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
45
46
47
  		atomic_read(&NCP_FINFO(inode)->opened), 
  		NCP_FINFO(inode)->volNumber, 
  		NCP_FINFO(inode)->dirEntNum);
  	error = -EACCES;
8e3f90459   Ingo Molnar   [PATCH] sem2mutex...
48
  	mutex_lock(&NCP_FINFO(inode)->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  	if (!atomic_read(&NCP_FINFO(inode)->opened)) {
  		struct ncp_entry_info finfo;
  		int result;
  
  		/* tries max. rights */
  		finfo.access = O_RDWR;
  		result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  					inode, NULL, OC_MODE_OPEN,
  					0, AR_READ | AR_WRITE, &finfo);
  		if (!result)
  			goto update;
  		/* RDWR did not succeeded, try readonly or writeonly as requested */
  		switch (right) {
  			case O_RDONLY:
  				finfo.access = O_RDONLY;
  				result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  					inode, NULL, OC_MODE_OPEN,
  					0, AR_READ, &finfo);
  				break;
  			case O_WRONLY:
  				finfo.access = O_WRONLY;
  				result = ncp_open_create_file_or_subdir(NCP_SERVER(inode),
  					inode, NULL, OC_MODE_OPEN,
  					0, AR_WRITE, &finfo);
  				break;
  		}
  		if (result) {
e45ca8baa   Joe Perches   ncpfs: convert PP...
76
77
  			ncp_vdbg("failed, result=%d
  ", result);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
80
81
82
83
84
85
86
87
88
  			goto out_unlock;
  		}
  		/*
  		 * Update the inode information.
  		 */
  	update:
  		ncp_update_inode(inode, &finfo);
  		atomic_set(&NCP_FINFO(inode)->opened, 1);
  	}
  
  	access = NCP_FINFO(inode)->access;
e45ca8baa   Joe Perches   ncpfs: convert PP...
89
90
  	ncp_vdbg("file open, access=%x
  ", access);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
91
92
93
94
95
96
  	if (access == right || access == O_RDWR) {
  		atomic_inc(&NCP_FINFO(inode)->opened);
  		error = 0;
  	}
  
  out_unlock:
8e3f90459   Ingo Molnar   [PATCH] sem2mutex...
97
  	mutex_unlock(&NCP_FINFO(inode)->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
98
99
100
101
102
  out:
  	return error;
  }
  
  static ssize_t
274a48869   Al Viro   ncpfs: switch to ...
103
  ncp_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
  {
274a48869   Al Viro   ncpfs: switch to ...
105
  	struct file *file = iocb->ki_filp;
a67f797db   Al Viro   ncpfs: use file_i...
106
  	struct inode *inode = file_inode(file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107
  	size_t already_read = 0;
274a48869   Al Viro   ncpfs: switch to ...
108
  	off_t pos = iocb->ki_pos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
  	size_t bufsize;
  	int error;
274a48869   Al Viro   ncpfs: switch to ...
111
  	void *freepage;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
  	size_t freelen;
a67f797db   Al Viro   ncpfs: use file_i...
113
114
  	ncp_dbg(1, "enter %pD2
  ", file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115

274a48869   Al Viro   ncpfs: switch to ...
116
  	if (!iov_iter_count(to))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
118
119
  		return 0;
  	if (pos > inode->i_sb->s_maxbytes)
  		return 0;
274a48869   Al Viro   ncpfs: switch to ...
120
  	iov_iter_truncate(to, inode->i_sb->s_maxbytes - pos);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121
122
123
  
  	error = ncp_make_open(inode, O_RDONLY);
  	if (error) {
d3b73ca1b   Joe Perches   ncpfs: convert DP...
124
125
  		ncp_dbg(1, "open failed, error=%d
  ", error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
128
129
130
131
132
133
134
135
136
137
  		return error;
  	}
  
  	bufsize = NCP_SERVER(inode)->buffer_size;
  
  	error = -EIO;
  	freelen = ncp_read_bounce_size(bufsize);
  	freepage = vmalloc(freelen);
  	if (!freepage)
  		goto outrel;
  	error = 0;
  	/* First read in as much as possible for each bufsize. */
274a48869   Al Viro   ncpfs: switch to ...
138
  	while (iov_iter_count(to)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
139
  		int read_this_time;
274a48869   Al Viro   ncpfs: switch to ...
140
  		size_t to_read = min_t(size_t,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
  				     bufsize - (pos % bufsize),
274a48869   Al Viro   ncpfs: switch to ...
142
  				     iov_iter_count(to));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
145
  
  		error = ncp_read_bounce(NCP_SERVER(inode),
  			 	NCP_FINFO(inode)->file_handle,
274a48869   Al Viro   ncpfs: switch to ...
146
  				pos, to_read, to, &read_this_time, 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
149
150
151
152
  				freepage, freelen);
  		if (error) {
  			error = -EIO;	/* NW errno -> Linux errno */
  			break;
  		}
  		pos += read_this_time;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
153
  		already_read += read_this_time;
274a48869   Al Viro   ncpfs: switch to ...
154
  		if (read_this_time != to_read)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
  	}
  	vfree(freepage);
274a48869   Al Viro   ncpfs: switch to ...
158
  	iocb->ki_pos = pos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
159
160
  
  	file_accessed(file);
a67f797db   Al Viro   ncpfs: use file_i...
161
162
  	ncp_dbg(1, "exit %pD2
  ", file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
166
167
168
  outrel:
  	ncp_inode_close(inode);		
  	return already_read ? already_read : error;
  }
  
  static ssize_t
274a48869   Al Viro   ncpfs: switch to ...
169
  ncp_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
  {
274a48869   Al Viro   ncpfs: switch to ...
171
  	struct file *file = iocb->ki_filp;
a67f797db   Al Viro   ncpfs: use file_i...
172
  	struct inode *inode = file_inode(file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
  	size_t already_written = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
  	size_t bufsize;
  	int errno;
274a48869   Al Viro   ncpfs: switch to ...
176
  	void *bouncebuffer;
3309dd04c   Al Viro   switch generic_wr...
177
  	off_t pos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
178

a67f797db   Al Viro   ncpfs: use file_i...
179
180
  	ncp_dbg(1, "enter %pD2
  ", file);
3309dd04c   Al Viro   switch generic_wr...
181
182
  	errno = generic_write_checks(iocb, from);
  	if (errno <= 0)
274a48869   Al Viro   ncpfs: switch to ...
183
  		return errno;
274a48869   Al Viro   ncpfs: switch to ...
184

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
186
  	errno = ncp_make_open(inode, O_WRONLY);
  	if (errno) {
d3b73ca1b   Joe Perches   ncpfs: convert DP...
187
188
  		ncp_dbg(1, "open failed, error=%d
  ", errno);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
190
191
  		return errno;
  	}
  	bufsize = NCP_SERVER(inode)->buffer_size;
c3b2da314   Josef Bacik   fs: introduce ino...
192
193
194
  	errno = file_update_time(file);
  	if (errno)
  		goto outrel;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
195
196
197
198
199
  	bouncebuffer = vmalloc(bufsize);
  	if (!bouncebuffer) {
  		errno = -EIO;	/* -ENOMEM */
  		goto outrel;
  	}
3309dd04c   Al Viro   switch generic_wr...
200
  	pos = iocb->ki_pos;
274a48869   Al Viro   ncpfs: switch to ...
201
  	while (iov_iter_count(from)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
202
  		int written_this_time;
274a48869   Al Viro   ncpfs: switch to ...
203
  		size_t to_write = min_t(size_t,
3309dd04c   Al Viro   switch generic_wr...
204
  				      bufsize - (pos % bufsize),
274a48869   Al Viro   ncpfs: switch to ...
205
  				      iov_iter_count(from));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206

cbbd26b8b   Al Viro   [iov_iter] new pr...
207
  		if (!copy_from_iter_full(bouncebuffer, to_write, from)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
209
210
211
212
213
214
215
216
217
  			errno = -EFAULT;
  			break;
  		}
  		if (ncp_write_kernel(NCP_SERVER(inode), 
  		    NCP_FINFO(inode)->file_handle,
  		    pos, to_write, bouncebuffer, &written_this_time) != 0) {
  			errno = -EIO;
  			break;
  		}
  		pos += written_this_time;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
  		already_written += written_this_time;
274a48869   Al Viro   ncpfs: switch to ...
219
  		if (written_this_time != to_write)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
220
  			break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
221
222
  	}
  	vfree(bouncebuffer);
274a48869   Al Viro   ncpfs: switch to ...
223
  	iocb->ki_pos = pos;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224

2e54eb96e   Petr Vandrovec   BKL: Remove BKL f...
225
  	if (pos > i_size_read(inode)) {
5955102c9   Al Viro   wrappers for ->i_...
226
  		inode_lock(inode);
2e54eb96e   Petr Vandrovec   BKL: Remove BKL f...
227
228
  		if (pos > i_size_read(inode))
  			i_size_write(inode, pos);
5955102c9   Al Viro   wrappers for ->i_...
229
  		inode_unlock(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
230
  	}
a67f797db   Al Viro   ncpfs: use file_i...
231
232
  	ncp_dbg(1, "exit %pD2
  ", file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
234
235
236
237
238
239
  outrel:
  	ncp_inode_close(inode);		
  	return already_written ? already_written : errno;
  }
  
  static int ncp_release(struct inode *inode, struct file *file) {
  	if (ncp_make_closed(inode)) {
d3b73ca1b   Joe Perches   ncpfs: convert DP...
240
241
  		ncp_dbg(1, "failed to close
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
242
243
244
  	}
  	return 0;
  }
4b6f5d20b   Arjan van de Ven   [PATCH] Make most...
245
  const struct file_operations ncp_file_operations =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
246
  {
2e54eb96e   Petr Vandrovec   BKL: Remove BKL f...
247
  	.llseek		= generic_file_llseek,
274a48869   Al Viro   ncpfs: switch to ...
248
249
  	.read_iter	= ncp_file_read_iter,
  	.write_iter	= ncp_file_write_iter,
93d84b6d9   John Kacur   ncpfs: BKL ioctl ...
250
  	.unlocked_ioctl	= ncp_ioctl,
54f67f631   Petr Vandrovec   [PATCH] Move ncpf...
251
252
253
  #ifdef CONFIG_COMPAT
  	.compat_ioctl	= ncp_compat_ioctl,
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
254
255
256
257
  	.mmap		= ncp_mmap,
  	.release	= ncp_release,
  	.fsync		= ncp_fsync,
  };
92e1d5be9   Arjan van de Ven   [PATCH] mark stru...
258
  const struct inode_operations ncp_file_inode_operations =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
260
261
  {
  	.setattr	= ncp_notify_change,
  };