1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Userspace file locking support
7 * Copyright (C) 2007 Oracle. All rights reserved.
11 #include <linux/filelock.h>
12 #include <linux/fcntl.h>
14 #include <cluster/masklog.h>
23 static int ocfs2_do_flock(struct file
*file
, struct inode
*inode
,
24 int cmd
, struct file_lock
*fl
)
26 int ret
= 0, level
= 0, trylock
= 0;
27 struct ocfs2_file_private
*fp
= file
->private_data
;
28 struct ocfs2_lock_res
*lockres
= &fp
->fp_flock
;
30 if (lock_is_write(fl
))
35 mutex_lock(&fp
->fp_mutex
);
37 if (lockres
->l_flags
& OCFS2_LOCK_ATTACHED
&&
38 lockres
->l_level
> LKM_NLMODE
) {
40 struct file_lock request
;
42 if (lockres
->l_level
== LKM_EXMODE
)
45 if (level
== old_level
)
49 * Converting an existing lock is not guaranteed to be
50 * atomic, so we can get away with simply unlocking
51 * here and allowing the lock code to try at the new
55 locks_init_lock(&request
);
56 request
.c
.flc_type
= F_UNLCK
;
57 request
.c
.flc_flags
= FL_FLOCK
;
58 locks_lock_file_wait(file
, &request
);
60 ocfs2_file_unlock(file
);
63 ret
= ocfs2_file_lock(file
, level
, trylock
);
65 if (ret
== -EAGAIN
&& trylock
)
72 ret
= locks_lock_file_wait(file
, fl
);
74 ocfs2_file_unlock(file
);
77 mutex_unlock(&fp
->fp_mutex
);
82 static int ocfs2_do_funlock(struct file
*file
, int cmd
, struct file_lock
*fl
)
85 struct ocfs2_file_private
*fp
= file
->private_data
;
87 mutex_lock(&fp
->fp_mutex
);
88 ocfs2_file_unlock(file
);
89 ret
= locks_lock_file_wait(file
, fl
);
90 mutex_unlock(&fp
->fp_mutex
);
96 * Overall flow of ocfs2_flock() was influenced by gfs2_flock().
98 int ocfs2_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
100 struct inode
*inode
= file
->f_mapping
->host
;
101 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
103 if (!(fl
->c
.flc_flags
& FL_FLOCK
))
106 if ((osb
->s_mount_opt
& OCFS2_MOUNT_LOCALFLOCKS
) ||
107 ocfs2_mount_local(osb
))
108 return locks_lock_file_wait(file
, fl
);
110 if (lock_is_unlock(fl
))
111 return ocfs2_do_funlock(file
, cmd
, fl
);
113 return ocfs2_do_flock(file
, inode
, cmd
, fl
);
116 int ocfs2_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
118 struct inode
*inode
= file
->f_mapping
->host
;
119 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
121 if (!(fl
->c
.flc_flags
& FL_POSIX
))
124 return ocfs2_plock(osb
->cconn
, OCFS2_I(inode
)->ip_blkno
, file
, cmd
, fl
);