Skip to content

Commit d6367d6

Browse files
NeilBrownjtlayton
authored andcommitted
fs/locks: use properly initialized file_lock when unlocking.
Both locks_remove_posix() and locks_remove_flock() use a struct file_lock without calling locks_init_lock() on it. This means the various list_heads are not initialized, which will become a problem with a later patch. So change them both to initialize properly. For flock locks, this involves using flock_make_lock(), and changing it to allow a file_lock to be passed in, so memory allocation isn't always needed. Signed-off-by: NeilBrown <[email protected]> Reviewed-by: J. Bruce Fields <[email protected]> Signed-off-by: Jeff Layton <[email protected]>
1 parent 4316c3c commit d6367d6

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

fs/locks.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -418,17 +418,20 @@ static inline int flock_translate_cmd(int cmd) {
418418

419419
/* Fill in a file_lock structure with an appropriate FLOCK lock. */
420420
static struct file_lock *
421-
flock_make_lock(struct file *filp, unsigned int cmd)
421+
flock_make_lock(struct file *filp, unsigned int cmd, struct file_lock *fl)
422422
{
423-
struct file_lock *fl;
424423
int type = flock_translate_cmd(cmd);
425424

426425
if (type < 0)
427426
return ERR_PTR(type);
428427

429-
fl = locks_alloc_lock();
430-
if (fl == NULL)
431-
return ERR_PTR(-ENOMEM);
428+
if (fl == NULL) {
429+
fl = locks_alloc_lock();
430+
if (fl == NULL)
431+
return ERR_PTR(-ENOMEM);
432+
} else {
433+
locks_init_lock(fl);
434+
}
432435

433436
fl->fl_file = filp;
434437
fl->fl_owner = filp;
@@ -2009,7 +2012,7 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
20092012
!(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
20102013
goto out_putf;
20112014

2012-
lock = flock_make_lock(f.file, cmd);
2015+
lock = flock_make_lock(f.file, cmd, NULL);
20132016
if (IS_ERR(lock)) {
20142017
error = PTR_ERR(lock);
20152018
goto out_putf;
@@ -2484,6 +2487,7 @@ void locks_remove_posix(struct file *filp, fl_owner_t owner)
24842487
if (!ctx || list_empty(&ctx->flc_posix))
24852488
return;
24862489

2490+
locks_init_lock(&lock);
24872491
lock.fl_type = F_UNLCK;
24882492
lock.fl_flags = FL_POSIX | FL_CLOSE;
24892493
lock.fl_start = 0;
@@ -2507,19 +2511,15 @@ EXPORT_SYMBOL(locks_remove_posix);
25072511
static void
25082512
locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
25092513
{
2510-
struct file_lock fl = {
2511-
.fl_owner = filp,
2512-
.fl_pid = current->tgid,
2513-
.fl_file = filp,
2514-
.fl_flags = FL_FLOCK | FL_CLOSE,
2515-
.fl_type = F_UNLCK,
2516-
.fl_end = OFFSET_MAX,
2517-
};
2514+
struct file_lock fl;
25182515
struct inode *inode = locks_inode(filp);
25192516

25202517
if (list_empty(&flctx->flc_flock))
25212518
return;
25222519

2520+
flock_make_lock(filp, LOCK_UN, &fl);
2521+
fl.fl_flags |= FL_CLOSE;
2522+
25232523
if (filp->f_op->flock)
25242524
filp->f_op->flock(filp, F_SETLKW, &fl);
25252525
else

0 commit comments

Comments
 (0)