Skip to content

Commit 19cba8a

Browse files
lionkovLinus Torvalds
authored andcommitted
[PATCH] v9fs: remove additional buffer allocation from v9fs_file_read and v9fs_file_write
v9fs_file_read and v9fs_file_write use kmalloc to allocate buffers as big as the data buffer received as parameter. kmalloc cannot be used to allocate buffers bigger than 128K, so reading/writing data in chunks bigger than 128k fails. This patch reorganizes v9fs_file_read and v9fs_file_write to allocate only buffers as big as the maximum data that can be sent in one 9P message. Signed-off-by: Latchesar Ionkov <lucho@ionkov.net> Cc: Eric Van Hensbergen <ericvh@gmail.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent ad6ce87 commit 19cba8a

1 file changed

Lines changed: 33 additions & 81 deletions

File tree

‎fs/9p/vfs_file.c‎

Lines changed: 33 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,16 @@ static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
175175
}
176176

177177
/**
178-
* v9fs_read - read from a file (internal)
178+
* v9fs_file_read - read from a file
179179
* @filep: file pointer to read
180180
* @data: data buffer to read data into
181181
* @count: size of buffer
182182
* @offset: offset at which to read data
183183
*
184184
*/
185-
186185
static ssize_t
187-
v9fs_read(struct file *filp, char *buffer, size_t count, loff_t * offset)
186+
v9fs_file_read(struct file *filp, char __user * data, size_t count,
187+
loff_t * offset)
188188
{
189189
struct inode *inode = filp->f_dentry->d_inode;
190190
struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
@@ -194,6 +194,7 @@ v9fs_read(struct file *filp, char *buffer, size_t count, loff_t * offset)
194194
int rsize = 0;
195195
int result = 0;
196196
int total = 0;
197+
int n;
197198

198199
dprintk(DEBUG_VFS, "\n");
199200

@@ -216,10 +217,15 @@ v9fs_read(struct file *filp, char *buffer, size_t count, loff_t * offset)
216217
} else
217218
*offset += result;
218219

219-
/* XXX - extra copy */
220-
memcpy(buffer, fcall->params.rread.data, result);
220+
n = copy_to_user(data, fcall->params.rread.data, result);
221+
if (n) {
222+
dprintk(DEBUG_ERROR, "Problem copying to user %d\n", n);
223+
kfree(fcall);
224+
return -EFAULT;
225+
}
226+
221227
count -= result;
222-
buffer += result;
228+
data += result;
223229
total += result;
224230

225231
kfree(fcall);
@@ -232,42 +238,7 @@ v9fs_read(struct file *filp, char *buffer, size_t count, loff_t * offset)
232238
}
233239

234240
/**
235-
* v9fs_file_read - read from a file
236-
* @filep: file pointer to read
237-
* @data: data buffer to read data into
238-
* @count: size of buffer
239-
* @offset: offset at which to read data
240-
*
241-
*/
242-
243-
static ssize_t
244-
v9fs_file_read(struct file *filp, char __user * data, size_t count,
245-
loff_t * offset)
246-
{
247-
int retval = -1;
248-
int ret = 0;
249-
char *buffer;
250-
251-
buffer = kmalloc(count, GFP_KERNEL);
252-
if (!buffer)
253-
return -ENOMEM;
254-
255-
retval = v9fs_read(filp, buffer, count, offset);
256-
if (retval > 0) {
257-
if ((ret = copy_to_user(data, buffer, retval)) != 0) {
258-
dprintk(DEBUG_ERROR, "Problem copying to user %d\n",
259-
ret);
260-
retval = ret;
261-
}
262-
}
263-
264-
kfree(buffer);
265-
266-
return retval;
267-
}
268-
269-
/**
270-
* v9fs_write - write to a file
241+
* v9fs_file_write - write to a file
271242
* @filep: file pointer to write
272243
* @data: data buffer to write data from
273244
* @count: size of buffer
@@ -276,7 +247,8 @@ v9fs_file_read(struct file *filp, char __user * data, size_t count,
276247
*/
277248

278249
static ssize_t
279-
v9fs_write(struct file *filp, char *buffer, size_t count, loff_t * offset)
250+
v9fs_file_write(struct file *filp, const char __user * data,
251+
size_t count, loff_t * offset)
280252
{
281253
struct inode *inode = filp->f_dentry->d_inode;
282254
struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
@@ -286,30 +258,42 @@ v9fs_write(struct file *filp, char *buffer, size_t count, loff_t * offset)
286258
int result = -EIO;
287259
int rsize = 0;
288260
int total = 0;
261+
char *buf;
289262

290-
dprintk(DEBUG_VFS, "data %p count %d offset %x\n", buffer, (int)count,
263+
dprintk(DEBUG_VFS, "data %p count %d offset %x\n", data, (int)count,
291264
(int)*offset);
292265
rsize = v9ses->maxdata - V9FS_IOHDRSZ;
293266
if (v9fid->iounit != 0 && rsize > v9fid->iounit)
294267
rsize = v9fid->iounit;
295268

296-
dump_data(buffer, count);
269+
buf = kmalloc(v9ses->maxdata - V9FS_IOHDRSZ, GFP_KERNEL);
270+
if (!buf)
271+
return -ENOMEM;
297272

298273
do {
299274
if (count < rsize)
300275
rsize = count;
301276

302-
result =
303-
v9fs_t_write(v9ses, fid, *offset, rsize, buffer, &fcall);
277+
result = copy_from_user(buf, data, rsize);
278+
if (result) {
279+
dprintk(DEBUG_ERROR, "Problem copying from user\n");
280+
kfree(buf);
281+
return -EFAULT;
282+
}
283+
284+
dump_data(buf, rsize);
285+
result = v9fs_t_write(v9ses, fid, *offset, rsize, buf, &fcall);
304286
if (result < 0) {
305287
eprintk(KERN_ERR, "error while writing: %s(%d)\n",
306288
FCALL_ERROR(fcall), result);
307289
kfree(fcall);
290+
kfree(buf);
308291
return result;
309292
} else
310293
*offset += result;
311294

312295
kfree(fcall);
296+
fcall = NULL;
313297

314298
if (result != rsize) {
315299
eprintk(KERN_ERR,
@@ -319,46 +303,14 @@ v9fs_write(struct file *filp, char *buffer, size_t count, loff_t * offset)
319303
}
320304

321305
count -= result;
322-
buffer += result;
306+
data += result;
323307
total += result;
324308
} while (count);
325309

310+
kfree(buf);
326311
return total;
327312
}
328313

329-
/**
330-
* v9fs_file_write - write to a file
331-
* @filep: file pointer to write
332-
* @data: data buffer to write data from
333-
* @count: size of buffer
334-
* @offset: offset at which to write data
335-
*
336-
*/
337-
338-
static ssize_t
339-
v9fs_file_write(struct file *filp, const char __user * data,
340-
size_t count, loff_t * offset)
341-
{
342-
int ret = -1;
343-
char *buffer;
344-
345-
buffer = kmalloc(count, GFP_KERNEL);
346-
if (buffer == NULL)
347-
return -ENOMEM;
348-
349-
ret = copy_from_user(buffer, data, count);
350-
if (ret) {
351-
dprintk(DEBUG_ERROR, "Problem copying from user\n");
352-
ret = -EFAULT;
353-
} else {
354-
ret = v9fs_write(filp, buffer, count, offset);
355-
}
356-
357-
kfree(buffer);
358-
359-
return ret;
360-
}
361-
362314
struct file_operations v9fs_file_operations = {
363315
.llseek = generic_file_llseek,
364316
.read = v9fs_file_read,

0 commit comments

Comments
 (0)