1 // SPDX-License-Identifier: GPL-2.0-or-later !! 1 /* -*- mode: c; c-basic-offset: 8; -*- 2 /* !! 2 * vim: noexpandtab sw=8 ts=8 sts=0: >> 3 * 3 * locks.c 4 * locks.c 4 * 5 * 5 * Userspace file locking support 6 * Userspace file locking support 6 * 7 * 7 * Copyright (C) 2007 Oracle. All rights rese 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. 8 */ 24 */ 9 25 10 #include <linux/fs.h> 26 #include <linux/fs.h> 11 #include <linux/filelock.h> << 12 #include <linux/fcntl.h> 27 #include <linux/fcntl.h> 13 28 14 #include <cluster/masklog.h> 29 #include <cluster/masklog.h> 15 30 16 #include "ocfs2.h" 31 #include "ocfs2.h" 17 32 18 #include "dlmglue.h" 33 #include "dlmglue.h" 19 #include "file.h" 34 #include "file.h" 20 #include "inode.h" 35 #include "inode.h" 21 #include "locks.h" 36 #include "locks.h" 22 37 23 static int ocfs2_do_flock(struct file *file, s 38 static int ocfs2_do_flock(struct file *file, struct inode *inode, 24 int cmd, struct file 39 int cmd, struct file_lock *fl) 25 { 40 { 26 int ret = 0, level = 0, trylock = 0; 41 int ret = 0, level = 0, trylock = 0; 27 struct ocfs2_file_private *fp = file-> 42 struct ocfs2_file_private *fp = file->private_data; 28 struct ocfs2_lock_res *lockres = &fp-> 43 struct ocfs2_lock_res *lockres = &fp->fp_flock; 29 44 30 if (lock_is_write(fl)) !! 45 if (fl->fl_type == F_WRLCK) 31 level = 1; 46 level = 1; 32 if (!IS_SETLKW(cmd)) 47 if (!IS_SETLKW(cmd)) 33 trylock = 1; 48 trylock = 1; 34 49 35 mutex_lock(&fp->fp_mutex); 50 mutex_lock(&fp->fp_mutex); 36 51 37 if (lockres->l_flags & OCFS2_LOCK_ATTA 52 if (lockres->l_flags & OCFS2_LOCK_ATTACHED && 38 lockres->l_level > LKM_NLMODE) { 53 lockres->l_level > LKM_NLMODE) { 39 int old_level = 0; 54 int old_level = 0; 40 struct file_lock request; << 41 55 42 if (lockres->l_level == LKM_EX 56 if (lockres->l_level == LKM_EXMODE) 43 old_level = 1; 57 old_level = 1; 44 58 45 if (level == old_level) 59 if (level == old_level) 46 goto out; 60 goto out; 47 61 48 /* 62 /* 49 * Converting an existing lock 63 * Converting an existing lock is not guaranteed to be 50 * atomic, so we can get away 64 * atomic, so we can get away with simply unlocking 51 * here and allowing the lock 65 * here and allowing the lock code to try at the new 52 * level. 66 * level. 53 */ 67 */ 54 68 55 locks_init_lock(&request); !! 69 locks_lock_file_wait(file, 56 request.c.flc_type = F_UNLCK; !! 70 &(struct file_lock) { 57 request.c.flc_flags = FL_FLOCK !! 71 .fl_type = F_UNLCK, 58 locks_lock_file_wait(file, &re !! 72 .fl_flags = FL_FLOCK >> 73 }); 59 74 60 ocfs2_file_unlock(file); 75 ocfs2_file_unlock(file); 61 } 76 } 62 77 63 ret = ocfs2_file_lock(file, level, try 78 ret = ocfs2_file_lock(file, level, trylock); 64 if (ret) { 79 if (ret) { 65 if (ret == -EAGAIN && trylock) 80 if (ret == -EAGAIN && trylock) 66 ret = -EWOULDBLOCK; 81 ret = -EWOULDBLOCK; 67 else 82 else 68 mlog_errno(ret); 83 mlog_errno(ret); 69 goto out; 84 goto out; 70 } 85 } 71 86 72 ret = locks_lock_file_wait(file, fl); 87 ret = locks_lock_file_wait(file, fl); 73 if (ret) 88 if (ret) 74 ocfs2_file_unlock(file); 89 ocfs2_file_unlock(file); 75 90 76 out: 91 out: 77 mutex_unlock(&fp->fp_mutex); 92 mutex_unlock(&fp->fp_mutex); 78 93 79 return ret; 94 return ret; 80 } 95 } 81 96 82 static int ocfs2_do_funlock(struct file *file, 97 static int ocfs2_do_funlock(struct file *file, int cmd, struct file_lock *fl) 83 { 98 { 84 int ret; 99 int ret; 85 struct ocfs2_file_private *fp = file-> 100 struct ocfs2_file_private *fp = file->private_data; 86 101 87 mutex_lock(&fp->fp_mutex); 102 mutex_lock(&fp->fp_mutex); 88 ocfs2_file_unlock(file); 103 ocfs2_file_unlock(file); 89 ret = locks_lock_file_wait(file, fl); 104 ret = locks_lock_file_wait(file, fl); 90 mutex_unlock(&fp->fp_mutex); 105 mutex_unlock(&fp->fp_mutex); 91 106 92 return ret; 107 return ret; 93 } 108 } 94 109 95 /* 110 /* 96 * Overall flow of ocfs2_flock() was influence 111 * Overall flow of ocfs2_flock() was influenced by gfs2_flock(). 97 */ 112 */ 98 int ocfs2_flock(struct file *file, int cmd, st 113 int ocfs2_flock(struct file *file, int cmd, struct file_lock *fl) 99 { 114 { 100 struct inode *inode = file->f_mapping- 115 struct inode *inode = file->f_mapping->host; 101 struct ocfs2_super *osb = OCFS2_SB(ino 116 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 102 117 103 if (!(fl->c.flc_flags & FL_FLOCK)) !! 118 if (!(fl->fl_flags & FL_FLOCK)) >> 119 return -ENOLCK; >> 120 if (__mandatory_lock(inode)) 104 return -ENOLCK; 121 return -ENOLCK; 105 122 106 if ((osb->s_mount_opt & OCFS2_MOUNT_LO 123 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) || 107 ocfs2_mount_local(osb)) 124 ocfs2_mount_local(osb)) 108 return locks_lock_file_wait(fi 125 return locks_lock_file_wait(file, fl); 109 126 110 if (lock_is_unlock(fl)) !! 127 if (fl->fl_type == F_UNLCK) 111 return ocfs2_do_funlock(file, 128 return ocfs2_do_funlock(file, cmd, fl); 112 else 129 else 113 return ocfs2_do_flock(file, in 130 return ocfs2_do_flock(file, inode, cmd, fl); 114 } 131 } 115 132 116 int ocfs2_lock(struct file *file, int cmd, str 133 int ocfs2_lock(struct file *file, int cmd, struct file_lock *fl) 117 { 134 { 118 struct inode *inode = file->f_mapping- 135 struct inode *inode = file->f_mapping->host; 119 struct ocfs2_super *osb = OCFS2_SB(ino 136 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 120 137 121 if (!(fl->c.flc_flags & FL_POSIX)) !! 138 if (!(fl->fl_flags & FL_POSIX)) >> 139 return -ENOLCK; >> 140 if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK) 122 return -ENOLCK; 141 return -ENOLCK; 123 142 124 return ocfs2_plock(osb->cconn, OCFS2_I 143 return ocfs2_plock(osb->cconn, OCFS2_I(inode)->ip_blkno, file, cmd, fl); 125 } 144 } 126 145
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.