locks.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #include <linux/fs.h>
  26. #include <linux/fcntl.h>
  27. #include <cluster/masklog.h>
  28. #include "ocfs2.h"
  29. #include "dlmglue.h"
  30. #include "file.h"
  31. #include "inode.h"
  32. #include "locks.h"
  33. static int ocfs2_do_flock(struct file *file, struct inode *inode,
  34. int cmd, struct file_lock *fl)
  35. {
  36. int ret = 0, level = 0, trylock = 0;
  37. struct ocfs2_file_private *fp = file->private_data;
  38. struct ocfs2_lock_res *lockres = &fp->fp_flock;
  39. if (fl->fl_type == F_WRLCK)
  40. level = 1;
  41. if (!IS_SETLKW(cmd))
  42. trylock = 1;
  43. mutex_lock(&fp->fp_mutex);
  44. if (lockres->l_flags & OCFS2_LOCK_ATTACHED &&
  45. lockres->l_level > LKM_NLMODE) {
  46. int old_level = 0;
  47. if (lockres->l_level == LKM_EXMODE)
  48. old_level = 1;
  49. if (level == old_level)
  50. goto out;
  51. /*
  52. * Converting an existing lock is not guaranteed to be
  53. * atomic, so we can get away with simply unlocking
  54. * here and allowing the lock code to try at the new
  55. * level.
  56. */
  57. locks_lock_file_wait(file,
  58. &(struct file_lock) {
  59. .fl_type = F_UNLCK,
  60. .fl_flags = FL_FLOCK
  61. });
  62. ocfs2_file_unlock(file);
  63. }
  64. ret = ocfs2_file_lock(file, level, trylock);
  65. if (ret) {
  66. if (ret == -EAGAIN && trylock)
  67. ret = -EWOULDBLOCK;
  68. else
  69. mlog_errno(ret);
  70. goto out;
  71. }
  72. ret = locks_lock_file_wait(file, fl);
  73. if (ret)
  74. ocfs2_file_unlock(file);
  75. out:
  76. mutex_unlock(&fp->fp_mutex);
  77. return ret;
  78. }
  79. static int ocfs2_do_funlock(struct file *file, int cmd, struct file_lock *fl)
  80. {
  81. int ret;
  82. struct ocfs2_file_private *fp = file->private_data;
  83. mutex_lock(&fp->fp_mutex);
  84. ocfs2_file_unlock(file);
  85. ret = locks_lock_file_wait(file, fl);
  86. mutex_unlock(&fp->fp_mutex);
  87. return ret;
  88. }
  89. /*
  90. * Overall flow of ocfs2_flock() was influenced by gfs2_flock().
  91. */
  92. int ocfs2_flock(struct file *file, int cmd, struct file_lock *fl)
  93. {
  94. struct inode *inode = file->f_mapping->host;
  95. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  96. if (!(fl->fl_flags & FL_FLOCK))
  97. return -ENOLCK;
  98. if (__mandatory_lock(inode))
  99. return -ENOLCK;
  100. if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) ||
  101. ocfs2_mount_local(osb))
  102. return locks_lock_file_wait(file, fl);
  103. if (fl->fl_type == F_UNLCK)
  104. return ocfs2_do_funlock(file, cmd, fl);
  105. else
  106. return ocfs2_do_flock(file, inode, cmd, fl);
  107. }
  108. int ocfs2_lock(struct file *file, int cmd, struct file_lock *fl)
  109. {
  110. struct inode *inode = file->f_mapping->host;
  111. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  112. if (!(fl->fl_flags & FL_POSIX))
  113. return -ENOLCK;
  114. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  115. return -ENOLCK;
  116. return ocfs2_plock(osb->cconn, OCFS2_I(inode)->ip_blkno, file, cmd, fl);
  117. }