xref: /openbmc/linux/fs/ocfs2/locks.c (revision 6e63153d)
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 
22 static 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 
75 out:
76 	mutex_unlock(&fp->fp_mutex);
77 
78 	return ret;
79 }
80 
81 static 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  */
97 int 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 
115 int 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 }
125