Merge tag 'xfs-6.2-fixes-2' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-2.6-block.git] / fs / ocfs2 / locks.c
CommitLineData
328970de 1// SPDX-License-Identifier: GPL-2.0-or-later
fa60ce2c 2/*
53fc622b
MF
3 * locks.c
4 *
5 * Userspace file locking support
6 *
7 * Copyright (C) 2007 Oracle. All rights reserved.
53fc622b
MF
8 */
9
10#include <linux/fs.h>
53da4939 11#include <linux/fcntl.h>
53fc622b 12
53fc622b
MF
13#include <cluster/masklog.h>
14
15#include "ocfs2.h"
16
17#include "dlmglue.h"
18#include "file.h"
53da4939 19#include "inode.h"
53fc622b
MF
20#include "locks.h"
21
22static int ocfs2_do_flock(struct file *file, struct inode *inode,
23 int cmd, struct file_lock *fl)
24{
25 int ret = 0, level = 0, trylock = 0;
26 struct ocfs2_file_private *fp = file->private_data;
27 struct ocfs2_lock_res *lockres = &fp->fp_flock;
28
29 if (fl->fl_type == F_WRLCK)
30 level = 1;
31 if (!IS_SETLKW(cmd))
32 trylock = 1;
33
34 mutex_lock(&fp->fp_mutex);
35
36 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
37 lockres->l_level > LKM_NLMODE) {
38 int old_level = 0;
4316c3c6 39 struct file_lock request;
53fc622b
MF
40
41 if (lockres->l_level == LKM_EXMODE)
42 old_level = 1;
43
44 if (level == old_level)
45 goto out;
46
47 /*
48 * Converting an existing lock is not guaranteed to be
49 * atomic, so we can get away with simply unlocking
50 * here and allowing the lock code to try at the new
51 * level.
52 */
53
4316c3c6
N
54 locks_init_lock(&request);
55 request.fl_type = F_UNLCK;
56 request.fl_flags = FL_FLOCK;
57 locks_lock_file_wait(file, &request);
53fc622b
MF
58
59 ocfs2_file_unlock(file);
60 }
61
62 ret = ocfs2_file_lock(file, level, trylock);
63 if (ret) {
64 if (ret == -EAGAIN && trylock)
65 ret = -EWOULDBLOCK;
66 else
67 mlog_errno(ret);
68 goto out;
69 }
70
4f656367 71 ret = locks_lock_file_wait(file, fl);
e228f643
WW
72 if (ret)
73 ocfs2_file_unlock(file);
53fc622b
MF
74
75out:
76 mutex_unlock(&fp->fp_mutex);
77
78 return ret;
79}
80
81static int ocfs2_do_funlock(struct file *file, int cmd, struct file_lock *fl)
82{
83 int ret;
84 struct ocfs2_file_private *fp = file->private_data;
85
86 mutex_lock(&fp->fp_mutex);
87 ocfs2_file_unlock(file);
4f656367 88 ret = locks_lock_file_wait(file, fl);
53fc622b
MF
89 mutex_unlock(&fp->fp_mutex);
90
91 return ret;
92}
93
94/*
95 * Overall flow of ocfs2_flock() was influenced by gfs2_flock().
96 */
97int ocfs2_flock(struct file *file, int cmd, struct file_lock *fl)
98{
99 struct inode *inode = file->f_mapping->host;
100 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
101
102 if (!(fl->fl_flags & FL_FLOCK))
103 return -ENOLCK;
53fc622b
MF
104
105 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
106 ocfs2_mount_local(osb))
4f656367 107 return locks_lock_file_wait(file, fl);
53fc622b
MF
108
109 if (fl->fl_type == F_UNLCK)
110 return ocfs2_do_funlock(file, cmd, fl);
111 else
112 return ocfs2_do_flock(file, inode, cmd, fl);
113}
53da4939
MF
114
115int ocfs2_lock(struct file *file, int cmd, struct file_lock *fl)
116{
117 struct inode *inode = file->f_mapping->host;
118 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
119
120 if (!(fl->fl_flags & FL_POSIX))
121 return -ENOLCK;
53da4939
MF
122
123 return ocfs2_plock(osb->cconn, OCFS2_I(inode)->ip_blkno, file, cmd, fl);
124}