locks.c 2.6 KB

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