Skip to content

Commit 8152f82

Browse files
author
Al Viro
committed
fdget(), more trivial conversions
all failure exits prior to fdget() leave the scope, all matching fdput() are immediately followed by leaving the scope. [xfs_ioc_commit_range() chunk moved here as well] Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Al Viro <[email protected]>
1 parent 6348be0 commit 8152f82

File tree

26 files changed

+202
-418
lines changed

26 files changed

+202
-418
lines changed

Diff for: drivers/infiniband/core/ucma.c

+6-13
Original file line numberDiff line numberDiff line change
@@ -1615,29 +1615,24 @@ static ssize_t ucma_migrate_id(struct ucma_file *new_file,
16151615
struct ucma_event *uevent, *tmp;
16161616
struct ucma_context *ctx;
16171617
LIST_HEAD(event_list);
1618-
struct fd f;
16191618
struct ucma_file *cur_file;
16201619
int ret = 0;
16211620

16221621
if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
16231622
return -EFAULT;
16241623

16251624
/* Get current fd to protect against it being closed */
1626-
f = fdget(cmd.fd);
1627-
if (!fd_file(f))
1625+
CLASS(fd, f)(cmd.fd);
1626+
if (fd_empty(f))
16281627
return -ENOENT;
1629-
if (fd_file(f)->f_op != &ucma_fops) {
1630-
ret = -EINVAL;
1631-
goto file_put;
1632-
}
1628+
if (fd_file(f)->f_op != &ucma_fops)
1629+
return -EINVAL;
16331630
cur_file = fd_file(f)->private_data;
16341631

16351632
/* Validate current fd and prevent destruction of id. */
16361633
ctx = ucma_get_ctx(cur_file, cmd.id);
1637-
if (IS_ERR(ctx)) {
1638-
ret = PTR_ERR(ctx);
1639-
goto file_put;
1640-
}
1634+
if (IS_ERR(ctx))
1635+
return PTR_ERR(ctx);
16411636

16421637
rdma_lock_handler(ctx->cm_id);
16431638
/*
@@ -1678,8 +1673,6 @@ static ssize_t ucma_migrate_id(struct ucma_file *new_file,
16781673
err_unlock:
16791674
rdma_unlock_handler(ctx->cm_id);
16801675
ucma_put_ctx(ctx);
1681-
file_put:
1682-
fdput(f);
16831676
return ret;
16841677
}
16851678

Diff for: drivers/vfio/group.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,14 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
104104
{
105105
struct vfio_container *container;
106106
struct iommufd_ctx *iommufd;
107-
struct fd f;
108107
int ret;
109108
int fd;
110109

111110
if (get_user(fd, arg))
112111
return -EFAULT;
113112

114-
f = fdget(fd);
115-
if (!fd_file(f))
113+
CLASS(fd, f)(fd);
114+
if (fd_empty(f))
116115
return -EBADF;
117116

118117
mutex_lock(&group->group_lock);
@@ -153,7 +152,6 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group,
153152

154153
out_unlock:
155154
mutex_unlock(&group->group_lock);
156-
fdput(f);
157155
return ret;
158156
}
159157

Diff for: fs/eventpoll.c

+4-11
Original file line numberDiff line numberDiff line change
@@ -2415,8 +2415,6 @@ SYSCALL_DEFINE4(epoll_ctl, int, epfd, int, op, int, fd,
24152415
static int do_epoll_wait(int epfd, struct epoll_event __user *events,
24162416
int maxevents, struct timespec64 *to)
24172417
{
2418-
int error;
2419-
struct fd f;
24202418
struct eventpoll *ep;
24212419

24222420
/* The maximum number of event must be greater than zero */
@@ -2428,17 +2426,16 @@ static int do_epoll_wait(int epfd, struct epoll_event __user *events,
24282426
return -EFAULT;
24292427

24302428
/* Get the "struct file *" for the eventpoll file */
2431-
f = fdget(epfd);
2432-
if (!fd_file(f))
2429+
CLASS(fd, f)(epfd);
2430+
if (fd_empty(f))
24332431
return -EBADF;
24342432

24352433
/*
24362434
* We have to check that the file structure underneath the fd
24372435
* the user passed to us _is_ an eventpoll file.
24382436
*/
2439-
error = -EINVAL;
24402437
if (!is_file_epoll(fd_file(f)))
2441-
goto error_fput;
2438+
return -EINVAL;
24422439

24432440
/*
24442441
* At this point it is safe to assume that the "private_data" contains
@@ -2447,11 +2444,7 @@ static int do_epoll_wait(int epfd, struct epoll_event __user *events,
24472444
ep = fd_file(f)->private_data;
24482445

24492446
/* Time to fish for events ... */
2450-
error = ep_poll(ep, events, maxevents, to);
2451-
2452-
error_fput:
2453-
fdput(f);
2454-
return error;
2447+
return ep_poll(ep, events, maxevents, to);
24552448
}
24562449

24572450
SYSCALL_DEFINE4(epoll_wait, int, epfd, struct epoll_event __user *, events,

Diff for: fs/ext4/ioctl.c

+7-14
Original file line numberDiff line numberDiff line change
@@ -1330,7 +1330,6 @@ static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
13301330

13311331
case EXT4_IOC_MOVE_EXT: {
13321332
struct move_extent me;
1333-
struct fd donor;
13341333
int err;
13351334

13361335
if (!(filp->f_mode & FMODE_READ) ||
@@ -1342,30 +1341,26 @@ static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
13421341
return -EFAULT;
13431342
me.moved_len = 0;
13441343

1345-
donor = fdget(me.donor_fd);
1346-
if (!fd_file(donor))
1344+
CLASS(fd, donor)(me.donor_fd);
1345+
if (fd_empty(donor))
13471346
return -EBADF;
13481347

1349-
if (!(fd_file(donor)->f_mode & FMODE_WRITE)) {
1350-
err = -EBADF;
1351-
goto mext_out;
1352-
}
1348+
if (!(fd_file(donor)->f_mode & FMODE_WRITE))
1349+
return -EBADF;
13531350

13541351
if (ext4_has_feature_bigalloc(sb)) {
13551352
ext4_msg(sb, KERN_ERR,
13561353
"Online defrag not supported with bigalloc");
1357-
err = -EOPNOTSUPP;
1358-
goto mext_out;
1354+
return -EOPNOTSUPP;
13591355
} else if (IS_DAX(inode)) {
13601356
ext4_msg(sb, KERN_ERR,
13611357
"Online defrag not supported with DAX");
1362-
err = -EOPNOTSUPP;
1363-
goto mext_out;
1358+
return -EOPNOTSUPP;
13641359
}
13651360

13661361
err = mnt_want_write_file(filp);
13671362
if (err)
1368-
goto mext_out;
1363+
return err;
13691364

13701365
err = ext4_move_extents(filp, fd_file(donor), me.orig_start,
13711366
me.donor_start, me.len, &me.moved_len);
@@ -1374,8 +1369,6 @@ static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
13741369
if (copy_to_user((struct move_extent __user *)arg,
13751370
&me, sizeof(me)))
13761371
err = -EFAULT;
1377-
mext_out:
1378-
fdput(donor);
13791372
return err;
13801373
}
13811374

Diff for: fs/f2fs/file.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -3038,32 +3038,27 @@ static int f2fs_move_file_range(struct file *file_in, loff_t pos_in,
30383038
static int __f2fs_ioc_move_range(struct file *filp,
30393039
struct f2fs_move_range *range)
30403040
{
3041-
struct fd dst;
30423041
int err;
30433042

30443043
if (!(filp->f_mode & FMODE_READ) ||
30453044
!(filp->f_mode & FMODE_WRITE))
30463045
return -EBADF;
30473046

3048-
dst = fdget(range->dst_fd);
3049-
if (!fd_file(dst))
3047+
CLASS(fd, dst)(range->dst_fd);
3048+
if (fd_empty(dst))
30503049
return -EBADF;
30513050

3052-
if (!(fd_file(dst)->f_mode & FMODE_WRITE)) {
3053-
err = -EBADF;
3054-
goto err_out;
3055-
}
3051+
if (!(fd_file(dst)->f_mode & FMODE_WRITE))
3052+
return -EBADF;
30563053

30573054
err = mnt_want_write_file(filp);
30583055
if (err)
3059-
goto err_out;
3056+
return err;
30603057

30613058
err = f2fs_move_file_range(filp, range->pos_in, fd_file(dst),
30623059
range->pos_out, range->len);
30633060

30643061
mnt_drop_write_file(filp);
3065-
err_out:
3066-
fdput(dst);
30673062
return err;
30683063
}
30693064

Diff for: fs/fsopen.c

+6-13
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,6 @@ SYSCALL_DEFINE5(fsconfig,
349349
int, aux)
350350
{
351351
struct fs_context *fc;
352-
struct fd f;
353352
int ret;
354353
int lookup_flags = 0;
355354

@@ -392,12 +391,11 @@ SYSCALL_DEFINE5(fsconfig,
392391
return -EOPNOTSUPP;
393392
}
394393

395-
f = fdget(fd);
396-
if (!fd_file(f))
394+
CLASS(fd, f)(fd);
395+
if (fd_empty(f))
397396
return -EBADF;
398-
ret = -EINVAL;
399397
if (fd_file(f)->f_op != &fscontext_fops)
400-
goto out_f;
398+
return -EINVAL;
401399

402400
fc = fd_file(f)->private_data;
403401
if (fc->ops == &legacy_fs_context_ops) {
@@ -407,17 +405,14 @@ SYSCALL_DEFINE5(fsconfig,
407405
case FSCONFIG_SET_PATH_EMPTY:
408406
case FSCONFIG_SET_FD:
409407
case FSCONFIG_CMD_CREATE_EXCL:
410-
ret = -EOPNOTSUPP;
411-
goto out_f;
408+
return -EOPNOTSUPP;
412409
}
413410
}
414411

415412
if (_key) {
416413
param.key = strndup_user(_key, 256);
417-
if (IS_ERR(param.key)) {
418-
ret = PTR_ERR(param.key);
419-
goto out_f;
420-
}
414+
if (IS_ERR(param.key))
415+
return PTR_ERR(param.key);
421416
}
422417

423418
switch (cmd) {
@@ -496,7 +491,5 @@ SYSCALL_DEFINE5(fsconfig,
496491
}
497492
out_key:
498493
kfree(param.key);
499-
out_f:
500-
fdput(f);
501494
return ret;
502495
}

Diff for: fs/fuse/dev.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -2371,13 +2371,12 @@ static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp)
23712371
int res;
23722372
int oldfd;
23732373
struct fuse_dev *fud = NULL;
2374-
struct fd f;
23752374

23762375
if (get_user(oldfd, argp))
23772376
return -EFAULT;
23782377

2379-
f = fdget(oldfd);
2380-
if (!fd_file(f))
2378+
CLASS(fd, f)(oldfd);
2379+
if (fd_empty(f))
23812380
return -EINVAL;
23822381

23832382
/*
@@ -2394,7 +2393,6 @@ static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp)
23942393
mutex_unlock(&fuse_mutex);
23952394
}
23962395

2397-
fdput(f);
23982396
return res;
23992397
}
24002398

Diff for: fs/locks.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,6 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
21362136
{
21372137
int can_sleep, error, type;
21382138
struct file_lock fl;
2139-
struct fd f;
21402139

21412140
/*
21422141
* LOCK_MAND locks were broken for a long time in that they never
@@ -2155,19 +2154,18 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
21552154
if (type < 0)
21562155
return type;
21572156

2158-
error = -EBADF;
2159-
f = fdget(fd);
2160-
if (!fd_file(f))
2161-
return error;
2157+
CLASS(fd, f)(fd);
2158+
if (fd_empty(f))
2159+
return -EBADF;
21622160

21632161
if (type != F_UNLCK && !(fd_file(f)->f_mode & (FMODE_READ | FMODE_WRITE)))
2164-
goto out_putf;
2162+
return -EBADF;
21652163

21662164
flock_make_lock(fd_file(f), &fl, type);
21672165

21682166
error = security_file_lock(fd_file(f), fl.c.flc_type);
21692167
if (error)
2170-
goto out_putf;
2168+
return error;
21712169

21722170
can_sleep = !(cmd & LOCK_NB);
21732171
if (can_sleep)
@@ -2181,9 +2179,6 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
21812179
error = locks_lock_file_wait(fd_file(f), &fl);
21822180

21832181
locks_release_private(&fl);
2184-
out_putf:
2185-
fdput(f);
2186-
21872182
return error;
21882183
}
21892184

0 commit comments

Comments
 (0)