1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
7 * Userspace file locking support
9 * Copyright (C) 2007 Oracle. All rights reserved.
13 #include <linux/fcntl.h>
15 #include <cluster/masklog.h>
24 static int ocfs2_do_flock(struct file
*file
, struct inode
*inode
,
25 int cmd
, struct file_lock
*fl
)
27 int ret
= 0, level
= 0, trylock
= 0;
28 struct ocfs2_file_private
*fp
= file
->private_data
;
29 struct ocfs2_lock_res
*lockres
= &fp
->fp_flock
;
31 if (fl
->fl_type
== F_WRLCK
)
36 mutex_lock(&fp
->fp_mutex
);
38 if (lockres
->l_flags
& OCFS2_LOCK_ATTACHED
&&
39 lockres
->l_level
> LKM_NLMODE
) {
41 struct file_lock request
;
43 if (lockres
->l_level
== LKM_EXMODE
)
46 if (level
== old_level
)
50 * Converting an existing lock is not guaranteed to be
51 * atomic, so we can get away with simply unlocking
52 * here and allowing the lock code to try at the new
56 locks_init_lock(&request
);
57 request
.fl_type
= F_UNLCK
;
58 request
.fl_flags
= FL_FLOCK
;
59 locks_lock_file_wait(file
, &request
);
61 ocfs2_file_unlock(file
);
64 ret
= ocfs2_file_lock(file
, level
, trylock
);
66 if (ret
== -EAGAIN
&& trylock
)
73 ret
= locks_lock_file_wait(file
, fl
);
75 ocfs2_file_unlock(file
);
78 mutex_unlock(&fp
->fp_mutex
);
83 static int ocfs2_do_funlock(struct file
*file
, int cmd
, struct file_lock
*fl
)
86 struct ocfs2_file_private
*fp
= file
->private_data
;
88 mutex_lock(&fp
->fp_mutex
);
89 ocfs2_file_unlock(file
);
90 ret
= locks_lock_file_wait(file
, fl
);
91 mutex_unlock(&fp
->fp_mutex
);
97 * Overall flow of ocfs2_flock() was influenced by gfs2_flock().
99 int ocfs2_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
101 struct inode
*inode
= file
->f_mapping
->host
;
102 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
104 if (!(fl
->fl_flags
& FL_FLOCK
))
106 if (__mandatory_lock(inode
))
109 if ((osb
->s_mount_opt
& OCFS2_MOUNT_LOCALFLOCKS
) ||
110 ocfs2_mount_local(osb
))
111 return locks_lock_file_wait(file
, fl
);
113 if (fl
->fl_type
== F_UNLCK
)
114 return ocfs2_do_funlock(file
, cmd
, fl
);
116 return ocfs2_do_flock(file
, inode
, cmd
, fl
);
119 int ocfs2_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
121 struct inode
*inode
= file
->f_mapping
->host
;
122 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
124 if (!(fl
->fl_flags
& FL_POSIX
))
126 if (__mandatory_lock(inode
) && fl
->fl_type
!= F_UNLCK
)
129 return ocfs2_plock(osb
->cconn
, OCFS2_I(inode
)->ip_blkno
, file
, cmd
, fl
);