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