@@ -425,30 +425,16 @@ static inline int flock_translate_cmd(int cmd) {
425425}
426426
427427/* Fill in a file_lock structure with an appropriate FLOCK lock. */
428- static struct file_lock *
429- flock_make_lock (struct file * filp , unsigned int cmd , struct file_lock * fl )
428+ static void flock_make_lock (struct file * filp , struct file_lock * fl , int type )
430429{
431- int type = flock_translate_cmd (cmd );
432-
433- if (type < 0 )
434- return ERR_PTR (type );
435-
436- if (fl == NULL ) {
437- fl = locks_alloc_lock ();
438- if (fl == NULL )
439- return ERR_PTR (- ENOMEM );
440- } else {
441- locks_init_lock (fl );
442- }
430+ locks_init_lock (fl );
443431
444432 fl -> fl_file = filp ;
445433 fl -> fl_owner = filp ;
446434 fl -> fl_pid = current -> tgid ;
447435 fl -> fl_flags = FL_FLOCK ;
448436 fl -> fl_type = type ;
449437 fl -> fl_end = OFFSET_MAX ;
450-
451- return fl ;
452438}
453439
454440static int assign_type (struct file_lock * fl , long type )
@@ -2097,21 +2083,9 @@ EXPORT_SYMBOL(locks_lock_inode_wait);
20972083 */
20982084SYSCALL_DEFINE2 (flock , unsigned int , fd , unsigned int , cmd )
20992085{
2100- struct fd f = fdget (fd );
2101- struct file_lock * lock ;
2102- int can_sleep , unlock ;
2103- int error ;
2104-
2105- error = - EBADF ;
2106- if (!f .file )
2107- goto out ;
2108-
2109- can_sleep = !(cmd & LOCK_NB );
2110- cmd &= ~LOCK_NB ;
2111- unlock = (cmd == LOCK_UN );
2112-
2113- if (!unlock && !(f .file -> f_mode & (FMODE_READ |FMODE_WRITE )))
2114- goto out_putf ;
2086+ int can_sleep , error , type ;
2087+ struct file_lock fl ;
2088+ struct fd f ;
21152089
21162090 /*
21172091 * LOCK_MAND locks were broken for a long time in that they never
@@ -2123,36 +2097,41 @@ SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
21232097 */
21242098 if (cmd & LOCK_MAND ) {
21252099 pr_warn_once ("Attempt to set a LOCK_MAND lock via flock(2). This support has been removed and the request ignored.\n" );
2126- error = 0 ;
2127- goto out_putf ;
2100+ return 0 ;
21282101 }
21292102
2130- lock = flock_make_lock (f .file , cmd , NULL );
2131- if (IS_ERR (lock )) {
2132- error = PTR_ERR (lock );
2103+ type = flock_translate_cmd (cmd & ~LOCK_NB );
2104+ if (type < 0 )
2105+ return type ;
2106+
2107+ error = - EBADF ;
2108+ f = fdget (fd );
2109+ if (!f .file )
2110+ return error ;
2111+
2112+ if (type != F_UNLCK && !(f .file -> f_mode & (FMODE_READ | FMODE_WRITE )))
21332113 goto out_putf ;
2134- }
21352114
2136- if (can_sleep )
2137- lock -> fl_flags |= FL_SLEEP ;
2115+ flock_make_lock (f .file , & fl , type );
21382116
2139- error = security_file_lock (f .file , lock -> fl_type );
2117+ error = security_file_lock (f .file , fl . fl_type );
21402118 if (error )
2141- goto out_free ;
2119+ goto out_putf ;
2120+
2121+ can_sleep = !(cmd & LOCK_NB );
2122+ if (can_sleep )
2123+ fl .fl_flags |= FL_SLEEP ;
21422124
21432125 if (f .file -> f_op -> flock )
21442126 error = f .file -> f_op -> flock (f .file ,
2145- (can_sleep ) ? F_SETLKW : F_SETLK ,
2146- lock );
2127+ (can_sleep ) ? F_SETLKW : F_SETLK ,
2128+ & fl );
21472129 else
2148- error = locks_lock_file_wait (f .file , lock );
2149-
2150- out_free :
2151- locks_free_lock (lock );
2130+ error = locks_lock_file_wait (f .file , & fl );
21522131
21532132 out_putf :
21542133 fdput (f );
2155- out :
2134+
21562135 return error ;
21572136}
21582137
@@ -2614,7 +2593,7 @@ locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
26142593 if (list_empty (& flctx -> flc_flock ))
26152594 return ;
26162595
2617- flock_make_lock (filp , LOCK_UN , & fl );
2596+ flock_make_lock (filp , & fl , F_UNLCK );
26182597 fl .fl_flags |= FL_CLOSE ;
26192598
26202599 if (filp -> f_op -> flock )
0 commit comments