callback.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
  3. *
  4. * This software may be freely redistributed under the terms of the
  5. * GNU General Public License.
  6. *
  7. * You should have received a copy of the GNU General Public License
  8. * along with this program; if not, write to the Free Software
  9. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  10. *
  11. * Authors: David Woodhouse <dwmw2@infradead.org>
  12. * David Howells <dhowells@redhat.com>
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/circ_buf.h>
  19. #include <linux/sched.h>
  20. #include "internal.h"
  21. /*
  22. * Allow the fileserver to request callback state (re-)initialisation.
  23. * Unfortunately, UUIDs are not guaranteed unique.
  24. */
  25. void afs_init_callback_state(struct afs_server *server)
  26. {
  27. rcu_read_lock();
  28. do {
  29. server->cb_s_break++;
  30. server = rcu_dereference(server->uuid_next);
  31. } while (0);
  32. rcu_read_unlock();
  33. }
  34. /*
  35. * actually break a callback
  36. */
  37. void __afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
  38. {
  39. _enter("");
  40. clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
  41. if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
  42. vnode->cb_break++;
  43. afs_clear_permits(vnode);
  44. if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
  45. afs_lock_may_be_available(vnode);
  46. trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, true);
  47. } else {
  48. trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, false);
  49. }
  50. }
  51. void afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
  52. {
  53. write_seqlock(&vnode->cb_lock);
  54. __afs_break_callback(vnode, reason);
  55. write_sequnlock(&vnode->cb_lock);
  56. }
  57. /*
  58. * Look up a volume by volume ID under RCU conditions.
  59. */
  60. static struct afs_volume *afs_lookup_volume_rcu(struct afs_cell *cell,
  61. afs_volid_t vid)
  62. {
  63. struct afs_volume *volume = NULL;
  64. struct rb_node *p;
  65. int seq = 0;
  66. do {
  67. /* Unfortunately, rbtree walking doesn't give reliable results
  68. * under just the RCU read lock, so we have to check for
  69. * changes.
  70. */
  71. read_seqbegin_or_lock(&cell->volume_lock, &seq);
  72. p = rcu_dereference_raw(cell->volumes.rb_node);
  73. while (p) {
  74. volume = rb_entry(p, struct afs_volume, cell_node);
  75. if (volume->vid < vid)
  76. p = rcu_dereference_raw(p->rb_left);
  77. else if (volume->vid > vid)
  78. p = rcu_dereference_raw(p->rb_right);
  79. else
  80. break;
  81. volume = NULL;
  82. }
  83. } while (need_seqretry(&cell->volume_lock, seq));
  84. done_seqretry(&cell->volume_lock, seq);
  85. return volume;
  86. }
  87. /*
  88. * allow the fileserver to explicitly break one callback
  89. * - happens when
  90. * - the backing file is changed
  91. * - a lock is released
  92. */
  93. static void afs_break_one_callback(struct afs_volume *volume,
  94. struct afs_fid *fid)
  95. {
  96. struct super_block *sb;
  97. struct afs_vnode *vnode;
  98. struct inode *inode;
  99. if (fid->vnode == 0 && fid->unique == 0) {
  100. /* The callback break applies to an entire volume. */
  101. write_lock(&volume->cb_v_break_lock);
  102. volume->cb_v_break++;
  103. trace_afs_cb_break(fid, volume->cb_v_break,
  104. afs_cb_break_for_volume_callback, false);
  105. write_unlock(&volume->cb_v_break_lock);
  106. return;
  107. }
  108. /* See if we can find a matching inode - even an I_NEW inode needs to
  109. * be marked as it can have its callback broken before we finish
  110. * setting up the local inode.
  111. */
  112. sb = rcu_dereference(volume->sb);
  113. if (!sb)
  114. return;
  115. inode = find_inode_rcu(sb, fid->vnode, afs_ilookup5_test_by_fid, fid);
  116. if (inode) {
  117. vnode = AFS_FS_I(inode);
  118. afs_break_callback(vnode, afs_cb_break_for_callback);
  119. } else {
  120. trace_afs_cb_miss(fid, afs_cb_break_for_callback);
  121. }
  122. }
  123. static void afs_break_some_callbacks(struct afs_server *server,
  124. struct afs_callback_break *cbb,
  125. size_t *_count)
  126. {
  127. struct afs_callback_break *residue = cbb;
  128. struct afs_volume *volume;
  129. afs_volid_t vid = cbb->fid.vid;
  130. size_t i;
  131. volume = afs_lookup_volume_rcu(server->cell, vid);
  132. /* TODO: Find all matching volumes if we couldn't match the server and
  133. * break them anyway.
  134. */
  135. for (i = *_count; i > 0; cbb++, i--) {
  136. if (cbb->fid.vid == vid) {
  137. _debug("- Fid { vl=%08llx n=%llu u=%u }",
  138. cbb->fid.vid,
  139. cbb->fid.vnode,
  140. cbb->fid.unique);
  141. --*_count;
  142. if (volume)
  143. afs_break_one_callback(volume, &cbb->fid);
  144. } else {
  145. *residue++ = *cbb;
  146. }
  147. }
  148. }
  149. /*
  150. * allow the fileserver to break callback promises
  151. */
  152. void afs_break_callbacks(struct afs_server *server, size_t count,
  153. struct afs_callback_break *callbacks)
  154. {
  155. _enter("%p,%zu,", server, count);
  156. ASSERT(server != NULL);
  157. rcu_read_lock();
  158. while (count > 0)
  159. afs_break_some_callbacks(server, callbacks, &count);
  160. rcu_read_unlock();
  161. return;
  162. }