callback.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <[email protected]>
  12. * David Howells <[email protected]>
  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. * Handle invalidation of an mmap'd file. We invalidate all the PTEs referring
  23. * to the pages in this file's pagecache, forcing the kernel to go through
  24. * ->fault() or ->page_mkwrite() - at which point we can handle invalidation
  25. * more fully.
  26. */
  27. void afs_invalidate_mmap_work(struct work_struct *work)
  28. {
  29. struct afs_vnode *vnode = container_of(work, struct afs_vnode, cb_work);
  30. unmap_mapping_pages(vnode->netfs.inode.i_mapping, 0, 0, false);
  31. }
  32. void afs_server_init_callback_work(struct work_struct *work)
  33. {
  34. struct afs_server *server = container_of(work, struct afs_server, initcb_work);
  35. struct afs_vnode *vnode;
  36. struct afs_cell *cell = server->cell;
  37. down_read(&cell->fs_open_mmaps_lock);
  38. list_for_each_entry(vnode, &cell->fs_open_mmaps, cb_mmap_link) {
  39. if (vnode->cb_server == server) {
  40. clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  41. queue_work(system_unbound_wq, &vnode->cb_work);
  42. }
  43. }
  44. up_read(&cell->fs_open_mmaps_lock);
  45. }
  46. /*
  47. * Allow the fileserver to request callback state (re-)initialisation.
  48. * Unfortunately, UUIDs are not guaranteed unique.
  49. */
  50. void afs_init_callback_state(struct afs_server *server)
  51. {
  52. rcu_read_lock();
  53. do {
  54. server->cb_s_break++;
  55. atomic_inc(&server->cell->fs_s_break);
  56. if (!list_empty(&server->cell->fs_open_mmaps))
  57. queue_work(system_unbound_wq, &server->initcb_work);
  58. } while ((server = rcu_dereference(server->uuid_next)));
  59. rcu_read_unlock();
  60. }
  61. /*
  62. * actually break a callback
  63. */
  64. void __afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
  65. {
  66. _enter("");
  67. clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
  68. if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
  69. vnode->cb_break++;
  70. vnode->cb_v_break = vnode->volume->cb_v_break;
  71. afs_clear_permits(vnode);
  72. if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
  73. afs_lock_may_be_available(vnode);
  74. if (reason != afs_cb_break_for_deleted &&
  75. vnode->status.type == AFS_FTYPE_FILE &&
  76. atomic_read(&vnode->cb_nr_mmap))
  77. queue_work(system_unbound_wq, &vnode->cb_work);
  78. trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, true);
  79. } else {
  80. trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, false);
  81. }
  82. }
  83. void afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
  84. {
  85. write_seqlock(&vnode->cb_lock);
  86. __afs_break_callback(vnode, reason);
  87. write_sequnlock(&vnode->cb_lock);
  88. }
  89. /*
  90. * Look up a volume by volume ID under RCU conditions.
  91. */
  92. static struct afs_volume *afs_lookup_volume_rcu(struct afs_cell *cell,
  93. afs_volid_t vid)
  94. {
  95. struct afs_volume *volume = NULL;
  96. struct rb_node *p;
  97. int seq = 0;
  98. do {
  99. /* Unfortunately, rbtree walking doesn't give reliable results
  100. * under just the RCU read lock, so we have to check for
  101. * changes.
  102. */
  103. read_seqbegin_or_lock(&cell->volume_lock, &seq);
  104. p = rcu_dereference_raw(cell->volumes.rb_node);
  105. while (p) {
  106. volume = rb_entry(p, struct afs_volume, cell_node);
  107. if (volume->vid < vid)
  108. p = rcu_dereference_raw(p->rb_left);
  109. else if (volume->vid > vid)
  110. p = rcu_dereference_raw(p->rb_right);
  111. else
  112. break;
  113. volume = NULL;
  114. }
  115. } while (need_seqretry(&cell->volume_lock, seq));
  116. done_seqretry(&cell->volume_lock, seq);
  117. return volume;
  118. }
  119. /*
  120. * allow the fileserver to explicitly break one callback
  121. * - happens when
  122. * - the backing file is changed
  123. * - a lock is released
  124. */
  125. static void afs_break_one_callback(struct afs_volume *volume,
  126. struct afs_fid *fid)
  127. {
  128. struct super_block *sb;
  129. struct afs_vnode *vnode;
  130. struct inode *inode;
  131. if (fid->vnode == 0 && fid->unique == 0) {
  132. /* The callback break applies to an entire volume. */
  133. write_lock(&volume->cb_v_break_lock);
  134. volume->cb_v_break++;
  135. trace_afs_cb_break(fid, volume->cb_v_break,
  136. afs_cb_break_for_volume_callback, false);
  137. write_unlock(&volume->cb_v_break_lock);
  138. return;
  139. }
  140. /* See if we can find a matching inode - even an I_NEW inode needs to
  141. * be marked as it can have its callback broken before we finish
  142. * setting up the local inode.
  143. */
  144. sb = rcu_dereference(volume->sb);
  145. if (!sb)
  146. return;
  147. inode = find_inode_rcu(sb, fid->vnode, afs_ilookup5_test_by_fid, fid);
  148. if (inode) {
  149. vnode = AFS_FS_I(inode);
  150. afs_break_callback(vnode, afs_cb_break_for_callback);
  151. } else {
  152. trace_afs_cb_miss(fid, afs_cb_break_for_callback);
  153. }
  154. }
  155. static void afs_break_some_callbacks(struct afs_server *server,
  156. struct afs_callback_break *cbb,
  157. size_t *_count)
  158. {
  159. struct afs_callback_break *residue = cbb;
  160. struct afs_volume *volume;
  161. afs_volid_t vid = cbb->fid.vid;
  162. size_t i;
  163. volume = afs_lookup_volume_rcu(server->cell, vid);
  164. /* TODO: Find all matching volumes if we couldn't match the server and
  165. * break them anyway.
  166. */
  167. for (i = *_count; i > 0; cbb++, i--) {
  168. if (cbb->fid.vid == vid) {
  169. _debug("- Fid { vl=%08llx n=%llu u=%u }",
  170. cbb->fid.vid,
  171. cbb->fid.vnode,
  172. cbb->fid.unique);
  173. --*_count;
  174. if (volume)
  175. afs_break_one_callback(volume, &cbb->fid);
  176. } else {
  177. *residue++ = *cbb;
  178. }
  179. }
  180. }
  181. /*
  182. * allow the fileserver to break callback promises
  183. */
  184. void afs_break_callbacks(struct afs_server *server, size_t count,
  185. struct afs_callback_break *callbacks)
  186. {
  187. _enter("%p,%zu,", server, count);
  188. ASSERT(server != NULL);
  189. rcu_read_lock();
  190. while (count > 0)
  191. afs_break_some_callbacks(server, callbacks, &count);
  192. rcu_read_unlock();
  193. return;
  194. }