Skip to content

Commit bdd1d2d

Browse files
Christoph HellwigAl Viro
Christoph Hellwig
authored and
Al Viro
committed
fs: fix kernel_read prototype
Use proper ssize_t and size_t types for the return value and count argument, move the offset last and make it an in/out argument like all other read/write helpers, and make the buf argument a void pointer to get rid of lots of casts in the callers. Signed-off-by: Christoph Hellwig <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent c41fbad commit bdd1d2d

File tree

17 files changed

+69
-71
lines changed

17 files changed

+69
-71
lines changed

arch/mips/kernel/elf.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ int arch_elf_pt_proc(void *_ehdr, void *_phdr, struct file *elf,
8787
bool elf32;
8888
u32 flags;
8989
int ret;
90+
loff_t pos;
9091

9192
elf32 = ehdr->e32.e_ident[EI_CLASS] == ELFCLASS32;
9293
flags = elf32 ? ehdr->e32.e_flags : ehdr->e64.e_flags;
@@ -108,21 +109,16 @@ int arch_elf_pt_proc(void *_ehdr, void *_phdr, struct file *elf,
108109

109110
if (phdr32->p_filesz < sizeof(abiflags))
110111
return -EINVAL;
111-
112-
ret = kernel_read(elf, phdr32->p_offset,
113-
(char *)&abiflags,
114-
sizeof(abiflags));
112+
pos = phdr32->p_offset;
115113
} else {
116114
if (phdr64->p_type != PT_MIPS_ABIFLAGS)
117115
return 0;
118116
if (phdr64->p_filesz < sizeof(abiflags))
119117
return -EINVAL;
120-
121-
ret = kernel_read(elf, phdr64->p_offset,
122-
(char *)&abiflags,
123-
sizeof(abiflags));
118+
pos = phdr64->p_offset;
124119
}
125120

121+
ret = kernel_read(elf, &abiflags, sizeof(abiflags), &pos);
126122
if (ret < 0)
127123
return ret;
128124
if (ret != sizeof(abiflags))

arch/x86/ia32/ia32_aout.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,10 @@ static int load_aout_library(struct file *file)
407407
unsigned long bss, start_addr, len, error;
408408
int retval;
409409
struct exec ex;
410-
410+
loff_t pos = 0;
411411

412412
retval = -ENOEXEC;
413-
error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
413+
error = kernel_read(file, &ex, sizeof(ex), &pos);
414414
if (error != sizeof(ex))
415415
goto out;
416416

drivers/media/pci/cx25821/cx25821-audio-upstream.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ static int cx25821_get_audio_data(struct cx25821_dev *dev,
277277
p = (char *)dev->_audiodata_buf_virt_addr + frame_offset;
278278

279279
for (i = 0; i < dev->_audio_lines_count; i++) {
280-
int n = kernel_read(file, file_offset, mybuf, AUDIO_LINE_SIZE);
280+
int n = kernel_read(file, mybuf, AUDIO_LINE_SIZE, &file_offset);
281281
if (n < AUDIO_LINE_SIZE) {
282282
pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
283283
__func__);
@@ -290,7 +290,6 @@ static int cx25821_get_audio_data(struct cx25821_dev *dev,
290290
memcpy(p, mybuf, n);
291291
p += n;
292292
}
293-
file_offset += n;
294293
}
295294
dev->_audioframe_count++;
296295
fput(file);
@@ -318,7 +317,7 @@ static int cx25821_openfile_audio(struct cx25821_dev *dev,
318317
{
319318
char *p = (void *)dev->_audiodata_buf_virt_addr;
320319
struct file *file;
321-
loff_t offset;
320+
loff_t file_offset = 0;
322321
int i, j;
323322

324323
file = filp_open(dev->_audiofilename, O_RDONLY | O_LARGEFILE, 0);
@@ -328,11 +327,11 @@ static int cx25821_openfile_audio(struct cx25821_dev *dev,
328327
return PTR_ERR(file);
329328
}
330329

331-
for (j = 0, offset = 0; j < NUM_AUDIO_FRAMES; j++) {
330+
for (j = 0; j < NUM_AUDIO_FRAMES; j++) {
332331
for (i = 0; i < dev->_audio_lines_count; i++) {
333332
char buf[AUDIO_LINE_SIZE];
334-
int n = kernel_read(file, offset, buf,
335-
AUDIO_LINE_SIZE);
333+
loff_t offset = file_offset;
334+
int n = kernel_read(file, buf, AUDIO_LINE_SIZE, &file_offset);
336335

337336
if (n < AUDIO_LINE_SIZE) {
338337
pr_info("Done: exit %s() since no more bytes to read from Audio file\n",
@@ -344,8 +343,6 @@ static int cx25821_openfile_audio(struct cx25821_dev *dev,
344343

345344
if (p)
346345
memcpy(p + offset, buf, n);
347-
348-
offset += n;
349346
}
350347
dev->_audioframe_count++;
351348
}

drivers/mtd/nand/nandsim.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ static ssize_t read_file(struct nandsim *ns, struct file *file, void *buf, size_
13791379
if (err)
13801380
return err;
13811381
noreclaim_flag = memalloc_noreclaim_save();
1382-
tx = kernel_read(file, pos, buf, count);
1382+
tx = kernel_read(file, buf, count, &pos);
13831383
memalloc_noreclaim_restore(noreclaim_flag);
13841384
put_pages(ns);
13851385
return tx;

fs/binfmt_aout.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,12 @@ static int load_aout_library(struct file *file)
341341
unsigned long error;
342342
int retval;
343343
struct exec ex;
344+
loff_t pos = 0;
344345

345346
inode = file_inode(file);
346347

347348
retval = -ENOEXEC;
348-
error = kernel_read(file, 0, (char *) &ex, sizeof(ex));
349+
error = kernel_read(file, &ex, sizeof(ex), &pos);
349350
if (error != sizeof(ex))
350351
goto out;
351352

fs/binfmt_elf.c

+13-10
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ static struct elf_phdr *load_elf_phdrs(struct elfhdr *elf_ex,
409409
{
410410
struct elf_phdr *elf_phdata = NULL;
411411
int retval, size, err = -1;
412+
loff_t pos = elf_ex->e_phoff;
412413

413414
/*
414415
* If the size of this structure has changed, then punt, since
@@ -432,8 +433,7 @@ static struct elf_phdr *load_elf_phdrs(struct elfhdr *elf_ex,
432433
goto out;
433434

434435
/* Read in the program headers */
435-
retval = kernel_read(elf_file, elf_ex->e_phoff,
436-
(char *)elf_phdata, size);
436+
retval = kernel_read(elf_file, elf_phdata, size, &pos);
437437
if (retval != size) {
438438
err = (retval < 0) ? retval : -EIO;
439439
goto out;
@@ -698,6 +698,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
698698
struct elfhdr interp_elf_ex;
699699
} *loc;
700700
struct arch_elf_state arch_state = INIT_ARCH_ELF_STATE;
701+
loff_t pos;
701702

702703
loc = kmalloc(sizeof(*loc), GFP_KERNEL);
703704
if (!loc) {
@@ -750,9 +751,9 @@ static int load_elf_binary(struct linux_binprm *bprm)
750751
if (!elf_interpreter)
751752
goto out_free_ph;
752753

753-
retval = kernel_read(bprm->file, elf_ppnt->p_offset,
754-
elf_interpreter,
755-
elf_ppnt->p_filesz);
754+
pos = elf_ppnt->p_offset;
755+
retval = kernel_read(bprm->file, elf_interpreter,
756+
elf_ppnt->p_filesz, &pos);
756757
if (retval != elf_ppnt->p_filesz) {
757758
if (retval >= 0)
758759
retval = -EIO;
@@ -776,9 +777,9 @@ static int load_elf_binary(struct linux_binprm *bprm)
776777
would_dump(bprm, interpreter);
777778

778779
/* Get the exec headers */
779-
retval = kernel_read(interpreter, 0,
780-
(void *)&loc->interp_elf_ex,
781-
sizeof(loc->interp_elf_ex));
780+
pos = 0;
781+
retval = kernel_read(interpreter, &loc->interp_elf_ex,
782+
sizeof(loc->interp_elf_ex), &pos);
782783
if (retval != sizeof(loc->interp_elf_ex)) {
783784
if (retval >= 0)
784785
retval = -EIO;
@@ -1175,9 +1176,10 @@ static int load_elf_library(struct file *file)
11751176
unsigned long elf_bss, bss, len;
11761177
int retval, error, i, j;
11771178
struct elfhdr elf_ex;
1179+
loff_t pos = 0;
11781180

11791181
error = -ENOEXEC;
1180-
retval = kernel_read(file, 0, (char *)&elf_ex, sizeof(elf_ex));
1182+
retval = kernel_read(file, &elf_ex, sizeof(elf_ex), &pos);
11811183
if (retval != sizeof(elf_ex))
11821184
goto out;
11831185

@@ -1201,7 +1203,8 @@ static int load_elf_library(struct file *file)
12011203

12021204
eppnt = elf_phdata;
12031205
error = -ENOEXEC;
1204-
retval = kernel_read(file, elf_ex.e_phoff, (char *)eppnt, j);
1206+
pos = elf_ex.e_phoff;
1207+
retval = kernel_read(file, eppnt, j, &pos);
12051208
if (retval != j)
12061209
goto out_free_ph;
12071210

fs/binfmt_elf_fdpic.c

+9-8
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,
145145
struct elf32_phdr *phdr;
146146
unsigned long size;
147147
int retval, loop;
148+
loff_t pos = params->hdr.e_phoff;
148149

149150
if (params->hdr.e_phentsize != sizeof(struct elf_phdr))
150151
return -ENOMEM;
@@ -156,8 +157,7 @@ static int elf_fdpic_fetch_phdrs(struct elf_fdpic_params *params,
156157
if (!params->phdrs)
157158
return -ENOMEM;
158159

159-
retval = kernel_read(file, params->hdr.e_phoff,
160-
(char *) params->phdrs, size);
160+
retval = kernel_read(file, params->phdrs, size, &pos);
161161
if (unlikely(retval != size))
162162
return retval < 0 ? retval : -ENOEXEC;
163163

@@ -199,6 +199,7 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
199199
char *interpreter_name = NULL;
200200
int executable_stack;
201201
int retval, i;
202+
loff_t pos;
202203

203204
kdebug("____ LOAD %d ____", current->pid);
204205

@@ -246,10 +247,9 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
246247
if (!interpreter_name)
247248
goto error;
248249

249-
retval = kernel_read(bprm->file,
250-
phdr->p_offset,
251-
interpreter_name,
252-
phdr->p_filesz);
250+
pos = phdr->p_offset;
251+
retval = kernel_read(bprm->file, interpreter_name,
252+
phdr->p_filesz, &pos);
253253
if (unlikely(retval != phdr->p_filesz)) {
254254
if (retval >= 0)
255255
retval = -ENOEXEC;
@@ -277,8 +277,9 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm)
277277
*/
278278
would_dump(bprm, interpreter);
279279

280-
retval = kernel_read(interpreter, 0, bprm->buf,
281-
BINPRM_BUF_SIZE);
280+
pos = 0;
281+
retval = kernel_read(interpreter, bprm->buf,
282+
BINPRM_BUF_SIZE, &pos);
282283
if (unlikely(retval != BINPRM_BUF_SIZE)) {
283284
if (retval >= 0)
284285
retval = -ENOEXEC;

fs/binfmt_flat.c

+5-13
Original file line numberDiff line numberDiff line change
@@ -176,19 +176,14 @@ static int create_flat_tables(struct linux_binprm *bprm, unsigned long arg_start
176176
#define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
177177
#define RESERVED 0xC0 /* bit 6,7: reserved */
178178

179-
static int decompress_exec(
180-
struct linux_binprm *bprm,
181-
unsigned long offset,
182-
char *dst,
183-
long len,
184-
int fd)
179+
static int decompress_exec(struct linux_binprm *bprm, loff_t fpos, char *dst,
180+
long len, int fd)
185181
{
186182
unsigned char *buf;
187183
z_stream strm;
188-
loff_t fpos;
189184
int ret, retval;
190185

191-
pr_debug("decompress_exec(offset=%lx,buf=%p,len=%lx)\n", offset, dst, len);
186+
pr_debug("decompress_exec(offset=%llx,buf=%p,len=%lx)\n", fpos, dst, len);
192187

193188
memset(&strm, 0, sizeof(strm));
194189
strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
@@ -204,13 +199,11 @@ static int decompress_exec(
204199
}
205200

206201
/* Read in first chunk of data and parse gzip header. */
207-
fpos = offset;
208-
ret = kernel_read(bprm->file, offset, buf, LBUFSIZE);
202+
ret = kernel_read(bprm->file, buf, LBUFSIZE, &fpos);
209203

210204
strm.next_in = buf;
211205
strm.avail_in = ret;
212206
strm.total_in = 0;
213-
fpos += ret;
214207

215208
retval = -ENOEXEC;
216209

@@ -276,15 +269,14 @@ static int decompress_exec(
276269
}
277270

278271
while ((ret = zlib_inflate(&strm, Z_NO_FLUSH)) == Z_OK) {
279-
ret = kernel_read(bprm->file, fpos, buf, LBUFSIZE);
272+
ret = kernel_read(bprm->file, buf, LBUFSIZE, &fpos);
280273
if (ret <= 0)
281274
break;
282275
len -= ret;
283276

284277
strm.next_in = buf;
285278
strm.avail_in = ret;
286279
strm.total_in = 0;
287-
fpos += ret;
288280
}
289281

290282
if (ret < 0) {

fs/binfmt_misc.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,15 @@ static int load_misc_binary(struct linux_binprm *bprm)
218218

219219
bprm->file = interp_file;
220220
if (fmt->flags & MISC_FMT_CREDENTIALS) {
221+
loff_t pos = 0;
222+
221223
/*
222224
* No need to call prepare_binprm(), it's already been
223225
* done. bprm->buf is stale, update from interp_file.
224226
*/
225227
memset(bprm->buf, 0, BINPRM_BUF_SIZE);
226-
retval = kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
228+
retval = kernel_read(bprm->file, bprm->buf, BINPRM_BUF_SIZE,
229+
&pos);
227230
} else
228231
retval = prepare_binprm(bprm);
229232

fs/coda/dir.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,10 @@ static int coda_venus_readdir(struct file *coda_file, struct dir_context *ctx)
368368
goto out;
369369

370370
while (1) {
371+
loff_t pos = ctx->pos - 2;
372+
371373
/* read entries from the directory file */
372-
ret = kernel_read(host_file, ctx->pos - 2, (char *)vdir,
373-
sizeof(*vdir));
374+
ret = kernel_read(host_file, vdir, sizeof(*vdir), &pos);
374375
if (ret < 0) {
375376
pr_err("%s: read dir %s failed %d\n",
376377
__func__, coda_f2s(&cii->c_fid), ret);

fs/ecryptfs/read_write.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
237237
lower_file = ecryptfs_inode_to_private(ecryptfs_inode)->lower_file;
238238
if (!lower_file)
239239
return -EIO;
240-
return kernel_read(lower_file, offset, data, size);
240+
return kernel_read(lower_file, data, size, &offset);
241241
}
242242

243243
/**

fs/exec.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -922,16 +922,14 @@ int kernel_read_file(struct file *file, void **buf, loff_t *size,
922922

923923
pos = 0;
924924
while (pos < i_size) {
925-
bytes = kernel_read(file, pos, (char *)(*buf) + pos,
926-
i_size - pos);
925+
bytes = kernel_read(file, *buf + pos, i_size - pos, &pos);
927926
if (bytes < 0) {
928927
ret = bytes;
929928
goto out;
930929
}
931930

932931
if (bytes == 0)
933932
break;
934-
pos += bytes;
935933
}
936934

937935
if (pos != i_size) {
@@ -1524,6 +1522,7 @@ static void bprm_fill_uid(struct linux_binprm *bprm)
15241522
int prepare_binprm(struct linux_binprm *bprm)
15251523
{
15261524
int retval;
1525+
loff_t pos = 0;
15271526

15281527
bprm_fill_uid(bprm);
15291528

@@ -1534,7 +1533,7 @@ int prepare_binprm(struct linux_binprm *bprm)
15341533
bprm->cred_prepared = 1;
15351534

15361535
memset(bprm->buf, 0, BINPRM_BUF_SIZE);
1537-
return kernel_read(bprm->file, 0, bprm->buf, BINPRM_BUF_SIZE);
1536+
return kernel_read(bprm->file, bprm->buf, BINPRM_BUF_SIZE, &pos);
15381537
}
15391538

15401539
EXPORT_SYMBOL(prepare_binprm);

fs/read_write.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -415,17 +415,15 @@ ssize_t __vfs_read(struct file *file, char __user *buf, size_t count,
415415
}
416416
EXPORT_SYMBOL(__vfs_read);
417417

418-
int kernel_read(struct file *file, loff_t offset, char *addr,
419-
unsigned long count)
418+
ssize_t kernel_read(struct file *file, void *buf, size_t count, loff_t *pos)
420419
{
421420
mm_segment_t old_fs;
422-
loff_t pos = offset;
423-
int result;
421+
ssize_t result;
424422

425423
old_fs = get_fs();
426424
set_fs(get_ds());
427425
/* The cast to a user pointer is valid due to the set_fs() */
428-
result = vfs_read(file, (void __user *)addr, count, &pos);
426+
result = vfs_read(file, (void __user *)buf, count, pos);
429427
set_fs(old_fs);
430428
return result;
431429
}

0 commit comments

Comments
 (0)