Skip to content

Commit d7a9616

Browse files
author
Al Viro
committed
introduce "fd_pos" class, convert fdget_pos() users to it.
fdget_pos() for constructor, fdput_pos() for cleanup, all users of fd..._pos() converted trivially. Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent 0481819 commit d7a9616

File tree

4 files changed

+26
-42
lines changed

4 files changed

+26
-42
lines changed

arch/alpha/kernel/osf_sys.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,15 +152,15 @@ SYSCALL_DEFINE4(osf_getdirentries, unsigned int, fd,
152152
long __user *, basep)
153153
{
154154
int error;
155-
struct fd arg = fdget_pos(fd);
155+
CLASS(fd_pos, arg)(fd);
156156
struct osf_dirent_callback buf = {
157157
.ctx.actor = osf_filldir,
158158
.dirent = dirent,
159159
.basep = basep,
160160
.count = count
161161
};
162162

163-
if (!fd_file(arg))
163+
if (fd_empty(arg))
164164
return -EBADF;
165165

166166
error = iterate_dir(fd_file(arg), &buf.ctx);
@@ -169,7 +169,6 @@ SYSCALL_DEFINE4(osf_getdirentries, unsigned int, fd,
169169
if (count != buf.count)
170170
error = count - buf.count;
171171

172-
fdput_pos(arg);
173172
return error;
174173
}
175174

fs/read_write.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ EXPORT_SYMBOL(vfs_llseek);
386386
static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence)
387387
{
388388
off_t retval;
389-
struct fd f = fdget_pos(fd);
390-
if (!fd_file(f))
389+
CLASS(fd_pos, f)(fd);
390+
if (fd_empty(f))
391391
return -EBADF;
392392

393393
retval = -EINVAL;
@@ -397,7 +397,6 @@ static off_t ksys_lseek(unsigned int fd, off_t offset, unsigned int whence)
397397
if (res != (loff_t)retval)
398398
retval = -EOVERFLOW; /* LFS: should only happen on 32 bit platforms */
399399
}
400-
fdput_pos(f);
401400
return retval;
402401
}
403402

@@ -420,15 +419,14 @@ SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
420419
unsigned int, whence)
421420
{
422421
int retval;
423-
struct fd f = fdget_pos(fd);
422+
CLASS(fd_pos, f)(fd);
424423
loff_t offset;
425424

426-
if (!fd_file(f))
425+
if (fd_empty(f))
427426
return -EBADF;
428427

429-
retval = -EINVAL;
430428
if (whence > SEEK_MAX)
431-
goto out_putf;
429+
return -EINVAL;
432430

433431
offset = vfs_llseek(fd_file(f), ((loff_t) offset_high << 32) | offset_low,
434432
whence);
@@ -439,8 +437,6 @@ SYSCALL_DEFINE5(llseek, unsigned int, fd, unsigned long, offset_high,
439437
if (!copy_to_user(result, &offset, sizeof(offset)))
440438
retval = 0;
441439
}
442-
out_putf:
443-
fdput_pos(f);
444440
return retval;
445441
}
446442
#endif
@@ -700,10 +696,10 @@ static inline loff_t *file_ppos(struct file *file)
700696

701697
ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
702698
{
703-
struct fd f = fdget_pos(fd);
699+
CLASS(fd_pos, f)(fd);
704700
ssize_t ret = -EBADF;
705701

706-
if (fd_file(f)) {
702+
if (!fd_empty(f)) {
707703
loff_t pos, *ppos = file_ppos(fd_file(f));
708704
if (ppos) {
709705
pos = *ppos;
@@ -712,7 +708,6 @@ ssize_t ksys_read(unsigned int fd, char __user *buf, size_t count)
712708
ret = vfs_read(fd_file(f), buf, count, ppos);
713709
if (ret >= 0 && ppos)
714710
fd_file(f)->f_pos = pos;
715-
fdput_pos(f);
716711
}
717712
return ret;
718713
}
@@ -724,10 +719,10 @@ SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
724719

725720
ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
726721
{
727-
struct fd f = fdget_pos(fd);
722+
CLASS(fd_pos, f)(fd);
728723
ssize_t ret = -EBADF;
729724

730-
if (fd_file(f)) {
725+
if (!fd_empty(f)) {
731726
loff_t pos, *ppos = file_ppos(fd_file(f));
732727
if (ppos) {
733728
pos = *ppos;
@@ -736,7 +731,6 @@ ssize_t ksys_write(unsigned int fd, const char __user *buf, size_t count)
736731
ret = vfs_write(fd_file(f), buf, count, ppos);
737732
if (ret >= 0 && ppos)
738733
fd_file(f)->f_pos = pos;
739-
fdput_pos(f);
740734
}
741735

742736
return ret;
@@ -1075,10 +1069,10 @@ static ssize_t vfs_writev(struct file *file, const struct iovec __user *vec,
10751069
static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
10761070
unsigned long vlen, rwf_t flags)
10771071
{
1078-
struct fd f = fdget_pos(fd);
1072+
CLASS(fd_pos, f)(fd);
10791073
ssize_t ret = -EBADF;
10801074

1081-
if (fd_file(f)) {
1075+
if (!fd_empty(f)) {
10821076
loff_t pos, *ppos = file_ppos(fd_file(f));
10831077
if (ppos) {
10841078
pos = *ppos;
@@ -1087,7 +1081,6 @@ static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
10871081
ret = vfs_readv(fd_file(f), vec, vlen, ppos, flags);
10881082
if (ret >= 0 && ppos)
10891083
fd_file(f)->f_pos = pos;
1090-
fdput_pos(f);
10911084
}
10921085

10931086
if (ret > 0)
@@ -1099,10 +1092,10 @@ static ssize_t do_readv(unsigned long fd, const struct iovec __user *vec,
10991092
static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
11001093
unsigned long vlen, rwf_t flags)
11011094
{
1102-
struct fd f = fdget_pos(fd);
1095+
CLASS(fd_pos, f)(fd);
11031096
ssize_t ret = -EBADF;
11041097

1105-
if (fd_file(f)) {
1098+
if (!fd_empty(f)) {
11061099
loff_t pos, *ppos = file_ppos(fd_file(f));
11071100
if (ppos) {
11081101
pos = *ppos;
@@ -1111,7 +1104,6 @@ static ssize_t do_writev(unsigned long fd, const struct iovec __user *vec,
11111104
ret = vfs_writev(fd_file(f), vec, vlen, ppos, flags);
11121105
if (ret >= 0 && ppos)
11131106
fd_file(f)->f_pos = pos;
1114-
fdput_pos(f);
11151107
}
11161108

11171109
if (ret > 0)

fs/readdir.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -219,20 +219,19 @@ SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
219219
struct old_linux_dirent __user *, dirent, unsigned int, count)
220220
{
221221
int error;
222-
struct fd f = fdget_pos(fd);
222+
CLASS(fd_pos, f)(fd);
223223
struct readdir_callback buf = {
224224
.ctx.actor = fillonedir,
225225
.dirent = dirent
226226
};
227227

228-
if (!fd_file(f))
228+
if (fd_empty(f))
229229
return -EBADF;
230230

231231
error = iterate_dir(fd_file(f), &buf.ctx);
232232
if (buf.result)
233233
error = buf.result;
234234

235-
fdput_pos(f);
236235
return error;
237236
}
238237

@@ -309,16 +308,15 @@ static bool filldir(struct dir_context *ctx, const char *name, int namlen,
309308
SYSCALL_DEFINE3(getdents, unsigned int, fd,
310309
struct linux_dirent __user *, dirent, unsigned int, count)
311310
{
312-
struct fd f;
311+
CLASS(fd_pos, f)(fd);
313312
struct getdents_callback buf = {
314313
.ctx.actor = filldir,
315314
.count = count,
316315
.current_dir = dirent
317316
};
318317
int error;
319318

320-
f = fdget_pos(fd);
321-
if (!fd_file(f))
319+
if (fd_empty(f))
322320
return -EBADF;
323321

324322
error = iterate_dir(fd_file(f), &buf.ctx);
@@ -333,7 +331,6 @@ SYSCALL_DEFINE3(getdents, unsigned int, fd,
333331
else
334332
error = count - buf.count;
335333
}
336-
fdput_pos(f);
337334
return error;
338335
}
339336

@@ -392,16 +389,15 @@ static bool filldir64(struct dir_context *ctx, const char *name, int namlen,
392389
SYSCALL_DEFINE3(getdents64, unsigned int, fd,
393390
struct linux_dirent64 __user *, dirent, unsigned int, count)
394391
{
395-
struct fd f;
392+
CLASS(fd_pos, f)(fd);
396393
struct getdents_callback64 buf = {
397394
.ctx.actor = filldir64,
398395
.count = count,
399396
.current_dir = dirent
400397
};
401398
int error;
402399

403-
f = fdget_pos(fd);
404-
if (!fd_file(f))
400+
if (fd_empty(f))
405401
return -EBADF;
406402

407403
error = iterate_dir(fd_file(f), &buf.ctx);
@@ -417,7 +413,6 @@ SYSCALL_DEFINE3(getdents64, unsigned int, fd,
417413
else
418414
error = count - buf.count;
419415
}
420-
fdput_pos(f);
421416
return error;
422417
}
423418

@@ -477,20 +472,19 @@ COMPAT_SYSCALL_DEFINE3(old_readdir, unsigned int, fd,
477472
struct compat_old_linux_dirent __user *, dirent, unsigned int, count)
478473
{
479474
int error;
480-
struct fd f = fdget_pos(fd);
475+
CLASS(fd_pos, f)(fd);
481476
struct compat_readdir_callback buf = {
482477
.ctx.actor = compat_fillonedir,
483478
.dirent = dirent
484479
};
485480

486-
if (!fd_file(f))
481+
if (fd_empty(f))
487482
return -EBADF;
488483

489484
error = iterate_dir(fd_file(f), &buf.ctx);
490485
if (buf.result)
491486
error = buf.result;
492487

493-
fdput_pos(f);
494488
return error;
495489
}
496490

@@ -560,16 +554,15 @@ static bool compat_filldir(struct dir_context *ctx, const char *name, int namlen
560554
COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
561555
struct compat_linux_dirent __user *, dirent, unsigned int, count)
562556
{
563-
struct fd f;
557+
CLASS(fd_pos, f)(fd);
564558
struct compat_getdents_callback buf = {
565559
.ctx.actor = compat_filldir,
566560
.current_dir = dirent,
567561
.count = count
568562
};
569563
int error;
570564

571-
f = fdget_pos(fd);
572-
if (!fd_file(f))
565+
if (fd_empty(f))
573566
return -EBADF;
574567

575568
error = iterate_dir(fd_file(f), &buf.ctx);
@@ -584,7 +577,6 @@ COMPAT_SYSCALL_DEFINE3(getdents, unsigned int, fd,
584577
else
585578
error = count - buf.count;
586579
}
587-
fdput_pos(f);
588580
return error;
589581
}
590582
#endif

include/linux/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static inline void fdput_pos(struct fd f)
8181

8282
DEFINE_CLASS(fd, struct fd, fdput(_T), fdget(fd), int fd)
8383
DEFINE_CLASS(fd_raw, struct fd, fdput(_T), fdget_raw(fd), int fd)
84+
DEFINE_CLASS(fd_pos, struct fd, fdput_pos(_T), fdget_pos(fd), int fd)
8485

8586
extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);
8687
extern int replace_fd(unsigned fd, struct file *file, unsigned flags);

0 commit comments

Comments
 (0)