callback.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. * 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. static void afs_volume_init_callback(struct afs_volume *volume)
  33. {
  34. struct afs_vnode *vnode;
  35. down_read(&volume->open_mmaps_lock);
  36. list_for_each_entry(vnode, &volume->open_mmaps, cb_mmap_link) {
  37. if (vnode->cb_v_check != atomic_read(&volume->cb_v_break)) {
  38. afs_clear_cb_promise(vnode, afs_cb_promise_clear_vol_init_cb);
  39. queue_work(system_dfl_wq, &vnode->cb_work);
  40. }
  41. }
  42. up_read(&volume->open_mmaps_lock);
  43. }
  44. /*
  45. * Allow the fileserver to request callback state (re-)initialisation.
  46. * Unfortunately, UUIDs are not guaranteed unique.
  47. */
  48. void afs_init_callback_state(struct afs_server *server)
  49. {
  50. struct afs_server_entry *se;
  51. down_read(&server->cell->vs_lock);
  52. list_for_each_entry(se, &server->volumes, slink) {
  53. se->cb_expires_at = AFS_NO_CB_PROMISE;
  54. se->volume->cb_expires_at = AFS_NO_CB_PROMISE;
  55. trace_afs_cb_v_break(se->volume->vid, atomic_read(&se->volume->cb_v_break),
  56. afs_cb_break_for_s_reinit);
  57. if (!list_empty(&se->volume->open_mmaps))
  58. afs_volume_init_callback(se->volume);
  59. }
  60. up_read(&server->cell->vs_lock);
  61. }
  62. /*
  63. * actually break a callback
  64. */
  65. void __afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
  66. {
  67. _enter("");
  68. clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
  69. if (afs_clear_cb_promise(vnode, afs_cb_promise_clear_cb_break)) {
  70. vnode->cb_break++;
  71. vnode->cb_v_check = atomic_read(&vnode->volume->cb_v_break);
  72. afs_clear_permits(vnode);
  73. if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
  74. afs_lock_may_be_available(vnode);
  75. if (reason != afs_cb_break_for_deleted &&
  76. vnode->status.type == AFS_FTYPE_FILE &&
  77. atomic_read(&vnode->cb_nr_mmap))
  78. queue_work(system_dfl_wq, &vnode->cb_work);
  79. trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, true);
  80. } else {
  81. trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, false);
  82. }
  83. }
  84. void afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
  85. {
  86. write_seqlock(&vnode->cb_lock);
  87. __afs_break_callback(vnode, reason);
  88. write_sequnlock(&vnode->cb_lock);
  89. }
  90. /*
  91. * Look up a volume by volume ID under RCU conditions.
  92. */
  93. static struct afs_volume *afs_lookup_volume_rcu(struct afs_cell *cell,
  94. afs_volid_t vid)
  95. {
  96. struct afs_volume *volume = NULL;
  97. struct rb_node *p;
  98. int seq = 1;
  99. for (;;) {
  100. /* Unfortunately, rbtree walking doesn't give reliable results
  101. * under just the RCU read lock, so we have to check for
  102. * changes.
  103. */
  104. seq++; /* 2 on the 1st/lockless path, otherwise odd */
  105. read_seqbegin_or_lock(&cell->volume_lock, &seq);
  106. p = rcu_dereference_raw(cell->volumes.rb_node);
  107. while (p) {
  108. volume = rb_entry(p, struct afs_volume, cell_node);
  109. if (volume->vid < vid)
  110. p = rcu_dereference_raw(p->rb_left);
  111. else if (volume->vid > vid)
  112. p = rcu_dereference_raw(p->rb_right);
  113. else
  114. break;
  115. volume = NULL;
  116. }
  117. if (volume && afs_try_get_volume(volume, afs_volume_trace_get_callback))
  118. break;
  119. if (!need_seqretry(&cell->volume_lock, seq))
  120. break;
  121. seq |= 1; /* Want a lock next time */
  122. }
  123. done_seqretry(&cell->volume_lock, seq);
  124. return volume;
  125. }
  126. /*
  127. * Allow the fileserver to break callbacks at the volume-level. This is
  128. * typically done when, for example, a R/W volume is snapshotted to a R/O
  129. * volume (the only way to change an R/O volume). It may also, however, happen
  130. * when a volserver takes control of a volume (offlining it, moving it, etc.).
  131. *
  132. * Every file in that volume will need to be reevaluated.
  133. */
  134. static void afs_break_volume_callback(struct afs_server *server,
  135. struct afs_volume *volume)
  136. __releases(RCU)
  137. {
  138. struct afs_server_list *slist = rcu_dereference(volume->servers);
  139. unsigned int i, cb_v_break;
  140. write_lock(&volume->cb_v_break_lock);
  141. for (i = 0; i < slist->nr_servers; i++)
  142. if (slist->servers[i].server == server)
  143. slist->servers[i].cb_expires_at = AFS_NO_CB_PROMISE;
  144. volume->cb_expires_at = AFS_NO_CB_PROMISE;
  145. cb_v_break = atomic_inc_return_release(&volume->cb_v_break);
  146. trace_afs_cb_v_break(volume->vid, cb_v_break, afs_cb_break_for_volume_callback);
  147. write_unlock(&volume->cb_v_break_lock);
  148. rcu_read_unlock();
  149. if (!list_empty(&volume->open_mmaps))
  150. afs_volume_init_callback(volume);
  151. }
  152. /*
  153. * allow the fileserver to explicitly break one callback
  154. * - happens when
  155. * - the backing file is changed
  156. * - a lock is released
  157. */
  158. static void afs_break_one_callback(struct afs_server *server,
  159. struct afs_volume *volume,
  160. struct afs_fid *fid)
  161. {
  162. struct super_block *sb;
  163. struct afs_vnode *vnode;
  164. struct inode *inode;
  165. /* See if we can find a matching inode - even an I_NEW inode needs to
  166. * be marked as it can have its callback broken before we finish
  167. * setting up the local inode.
  168. */
  169. sb = rcu_dereference(volume->sb);
  170. if (!sb)
  171. return;
  172. inode = find_inode_rcu(sb, fid->vnode, afs_ilookup5_test_by_fid, fid);
  173. if (inode) {
  174. vnode = AFS_FS_I(inode);
  175. afs_break_callback(vnode, afs_cb_break_for_callback);
  176. } else {
  177. trace_afs_cb_miss(fid, afs_cb_break_for_callback);
  178. }
  179. }
  180. static void afs_break_some_callbacks(struct afs_server *server,
  181. struct afs_callback_break *cbb,
  182. size_t *_count)
  183. {
  184. struct afs_callback_break *residue = cbb;
  185. struct afs_volume *volume;
  186. afs_volid_t vid = cbb->fid.vid;
  187. size_t i;
  188. rcu_read_lock();
  189. volume = afs_lookup_volume_rcu(server->cell, vid);
  190. if (cbb->fid.vnode == 0 && cbb->fid.unique == 0) {
  191. afs_break_volume_callback(server, volume);
  192. *_count -= 1;
  193. if (*_count)
  194. memmove(cbb, cbb + 1, sizeof(*cbb) * *_count);
  195. } else {
  196. /* TODO: Find all matching volumes if we couldn't match the server and
  197. * break them anyway.
  198. */
  199. for (i = *_count; i > 0; cbb++, i--) {
  200. if (cbb->fid.vid == vid) {
  201. _debug("- Fid { vl=%08llx n=%llu u=%u }",
  202. cbb->fid.vid,
  203. cbb->fid.vnode,
  204. cbb->fid.unique);
  205. --*_count;
  206. if (volume)
  207. afs_break_one_callback(server, volume, &cbb->fid);
  208. } else {
  209. *residue++ = *cbb;
  210. }
  211. }
  212. rcu_read_unlock();
  213. }
  214. afs_put_volume(volume, afs_volume_trace_put_callback);
  215. }
  216. /*
  217. * allow the fileserver to break callback promises
  218. */
  219. void afs_break_callbacks(struct afs_server *server, size_t count,
  220. struct afs_callback_break *callbacks)
  221. {
  222. _enter("%p,%zu,", server, count);
  223. ASSERT(server != NULL);
  224. while (count > 0)
  225. afs_break_some_callbacks(server, callbacks, &count);
  226. }