gfs2-glocks.txt 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. Glock internal locking rules
  2. ------------------------------
  3. This documents the basic principles of the glock state machine
  4. internals. Each glock (struct gfs2_glock in fs/gfs2/incore.h)
  5. has two main (internal) locks:
  6. 1. A spinlock (gl_spin) which protects the internal state such
  7. as gl_state, gl_target and the list of holders (gl_holders)
  8. 2. A non-blocking bit lock, GLF_LOCK, which is used to prevent other
  9. threads from making calls to the DLM, etc. at the same time. If a
  10. thread takes this lock, it must then call run_queue (usually via the
  11. workqueue) when it releases it in order to ensure any pending tasks
  12. are completed.
  13. The gl_holders list contains all the queued lock requests (not
  14. just the holders) associated with the glock. If there are any
  15. held locks, then they will be contiguous entries at the head
  16. of the list. Locks are granted in strictly the order that they
  17. are queued, except for those marked LM_FLAG_PRIORITY which are
  18. used only during recovery, and even then only for journal locks.
  19. There are three lock states that users of the glock layer can request,
  20. namely shared (SH), deferred (DF) and exclusive (EX). Those translate
  21. to the following DLM lock modes:
  22. Glock mode | DLM lock mode
  23. ------------------------------
  24. UN | IV/NL Unlocked (no DLM lock associated with glock) or NL
  25. SH | PR (Protected read)
  26. DF | CW (Concurrent write)
  27. EX | EX (Exclusive)
  28. Thus DF is basically a shared mode which is incompatible with the "normal"
  29. shared lock mode, SH. In GFS2 the DF mode is used exclusively for direct I/O
  30. operations. The glocks are basically a lock plus some routines which deal
  31. with cache management. The following rules apply for the cache:
  32. Glock mode | Cache data | Cache Metadata | Dirty Data | Dirty Metadata
  33. --------------------------------------------------------------------------
  34. UN | No | No | No | No
  35. SH | Yes | Yes | No | No
  36. DF | No | Yes | No | No
  37. EX | Yes | Yes | Yes | Yes
  38. These rules are implemented using the various glock operations which
  39. are defined for each type of glock. Not all types of glocks use
  40. all the modes. Only inode glocks use the DF mode for example.
  41. Table of glock operations and per type constants:
  42. Field | Purpose
  43. ----------------------------------------------------------------------------
  44. go_xmote_th | Called before remote state change (e.g. to sync dirty data)
  45. go_xmote_bh | Called after remote state change (e.g. to refill cache)
  46. go_inval | Called if remote state change requires invalidating the cache
  47. go_demote_ok | Returns boolean value of whether its ok to demote a glock
  48. | (e.g. checks timeout, and that there is no cached data)
  49. go_lock | Called for the first local holder of a lock
  50. go_unlock | Called on the final local unlock of a lock
  51. go_dump | Called to print content of object for debugfs file, or on
  52. | error to dump glock to the log.
  53. go_type | The type of the glock, LM_TYPE_.....
  54. go_min_hold_time | The minimum hold time
  55. The minimum hold time for each lock is the time after a remote lock
  56. grant for which we ignore remote demote requests. This is in order to
  57. prevent a situation where locks are being bounced around the cluster
  58. from node to node with none of the nodes making any progress. This
  59. tends to show up most with shared mmaped files which are being written
  60. to by multiple nodes. By delaying the demotion in response to a
  61. remote callback, that gives the userspace program time to make
  62. some progress before the pages are unmapped.
  63. There is a plan to try and remove the go_lock and go_unlock callbacks
  64. if possible, in order to try and speed up the fast path though the locking.
  65. Also, eventually we hope to make the glock "EX" mode locally shared
  66. such that any local locking will be done with the i_mutex as required
  67. rather than via the glock.
  68. Locking rules for glock operations:
  69. Operation | GLF_LOCK bit lock held | gl_spin spinlock held
  70. -----------------------------------------------------------------
  71. go_xmote_th | Yes | No
  72. go_xmote_bh | Yes | No
  73. go_inval | Yes | No
  74. go_demote_ok | Sometimes | Yes
  75. go_lock | Yes | No
  76. go_unlock | Yes | No
  77. go_dump | Sometimes | Yes
  78. N.B. Operations must not drop either the bit lock or the spinlock
  79. if its held on entry. go_dump and do_demote_ok must never block.
  80. Note that go_dump will only be called if the glock's state
  81. indicates that it is caching uptodate data.
  82. Glock locking order within GFS2:
  83. 1. i_mutex (if required)
  84. 2. Rename glock (for rename only)
  85. 3. Inode glock(s)
  86. (Parents before children, inodes at "same level" with same parent in
  87. lock number order)
  88. 4. Rgrp glock(s) (for (de)allocation operations)
  89. 5. Transaction glock (via gfs2_trans_begin) for non-read operations
  90. 6. Page lock (always last, very important!)
  91. There are two glocks per inode. One deals with access to the inode
  92. itself (locking order as above), and the other, known as the iopen
  93. glock is used in conjunction with the i_nlink field in the inode to
  94. determine the lifetime of the inode in question. Locking of inodes
  95. is on a per-inode basis. Locking of rgrps is on a per rgrp basis.