Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * locks.c
4 *
5 * Userspace file locking support
6 *
7 * Copyright (C) 2007 Oracle. All rights reserved.
8 */
9
10#include <linux/fs.h>
11#include <linux/fcntl.h>
12
13#include <cluster/masklog.h>
14
15#include "ocfs2.h"
16
17#include "dlmglue.h"
18#include "file.h"
19#include "inode.h"
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;
39 struct file_lock request;
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
54 locks_init_lock(&request);
55 request.fl_type = F_UNLCK;
56 request.fl_flags = FL_FLOCK;
57 locks_lock_file_wait(file, &request);
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
71 ret = locks_lock_file_wait(file, fl);
72 if (ret)
73 ocfs2_file_unlock(file);
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);
88 ret = locks_lock_file_wait(file, fl);
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;
104
105 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
106 ocfs2_mount_local(osb))
107 return locks_lock_file_wait(file, fl);
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}
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;
122
123 return ocfs2_plock(osb->cconn, OCFS2_I(inode)->ip_blkno, file, cmd, fl);
124}
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * locks.c
5 *
6 * Userspace file locking support
7 *
8 * Copyright (C) 2007 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/fcntl.h>
28
29#include <cluster/masklog.h>
30
31#include "ocfs2.h"
32
33#include "dlmglue.h"
34#include "file.h"
35#include "inode.h"
36#include "locks.h"
37
38static int ocfs2_do_flock(struct file *file, struct inode *inode,
39 int cmd, struct file_lock *fl)
40{
41 int ret = 0, level = 0, trylock = 0;
42 struct ocfs2_file_private *fp = file->private_data;
43 struct ocfs2_lock_res *lockres = &fp->fp_flock;
44
45 if (fl->fl_type == F_WRLCK)
46 level = 1;
47 if (!IS_SETLKW(cmd))
48 trylock = 1;
49
50 mutex_lock(&fp->fp_mutex);
51
52 if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
53 lockres->l_level > LKM_NLMODE) {
54 int old_level = 0;
55
56 if (lockres->l_level == LKM_EXMODE)
57 old_level = 1;
58
59 if (level == old_level)
60 goto out;
61
62 /*
63 * Converting an existing lock is not guaranteed to be
64 * atomic, so we can get away with simply unlocking
65 * here and allowing the lock code to try at the new
66 * level.
67 */
68
69 flock_lock_file_wait(file,
70 &(struct file_lock){.fl_type = F_UNLCK});
71
72 ocfs2_file_unlock(file);
73 }
74
75 ret = ocfs2_file_lock(file, level, trylock);
76 if (ret) {
77 if (ret == -EAGAIN && trylock)
78 ret = -EWOULDBLOCK;
79 else
80 mlog_errno(ret);
81 goto out;
82 }
83
84 ret = flock_lock_file_wait(file, fl);
85 if (ret)
86 ocfs2_file_unlock(file);
87
88out:
89 mutex_unlock(&fp->fp_mutex);
90
91 return ret;
92}
93
94static int ocfs2_do_funlock(struct file *file, int cmd, struct file_lock *fl)
95{
96 int ret;
97 struct ocfs2_file_private *fp = file->private_data;
98
99 mutex_lock(&fp->fp_mutex);
100 ocfs2_file_unlock(file);
101 ret = flock_lock_file_wait(file, fl);
102 mutex_unlock(&fp->fp_mutex);
103
104 return ret;
105}
106
107/*
108 * Overall flow of ocfs2_flock() was influenced by gfs2_flock().
109 */
110int ocfs2_flock(struct file *file, int cmd, struct file_lock *fl)
111{
112 struct inode *inode = file->f_mapping->host;
113 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
114
115 if (!(fl->fl_flags & FL_FLOCK))
116 return -ENOLCK;
117 if (__mandatory_lock(inode))
118 return -ENOLCK;
119
120 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
121 ocfs2_mount_local(osb))
122 return flock_lock_file_wait(file, fl);
123
124 if (fl->fl_type == F_UNLCK)
125 return ocfs2_do_funlock(file, cmd, fl);
126 else
127 return ocfs2_do_flock(file, inode, cmd, fl);
128}
129
130int ocfs2_lock(struct file *file, int cmd, struct file_lock *fl)
131{
132 struct inode *inode = file->f_mapping->host;
133 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
134
135 if (!(fl->fl_flags & FL_POSIX))
136 return -ENOLCK;
137 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
138 return -ENOLCK;
139
140 return ocfs2_plock(osb->cconn, OCFS2_I(inode)->ip_blkno, file, cmd, fl);
141}