callback.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. * Create volume and callback interests on a server.
  23. */
  24. static struct afs_cb_interest *afs_create_interest(struct afs_server *server,
  25. struct afs_vnode *vnode)
  26. {
  27. struct afs_vol_interest *new_vi, *vi;
  28. struct afs_cb_interest *new;
  29. struct hlist_node **pp;
  30. new_vi = kzalloc(sizeof(struct afs_vol_interest), GFP_KERNEL);
  31. if (!new_vi)
  32. return NULL;
  33. new = kzalloc(sizeof(struct afs_cb_interest), GFP_KERNEL);
  34. if (!new) {
  35. kfree(new_vi);
  36. return NULL;
  37. }
  38. new_vi->usage = 1;
  39. new_vi->vid = vnode->volume->vid;
  40. INIT_HLIST_NODE(&new_vi->srv_link);
  41. INIT_HLIST_HEAD(&new_vi->cb_interests);
  42. refcount_set(&new->usage, 1);
  43. new->sb = vnode->vfs_inode.i_sb;
  44. new->vid = vnode->volume->vid;
  45. new->server = afs_get_server(server);
  46. INIT_HLIST_NODE(&new->cb_vlink);
  47. write_lock(&server->cb_break_lock);
  48. for (pp = &server->cb_volumes.first; *pp; pp = &(*pp)->next) {
  49. vi = hlist_entry(*pp, struct afs_vol_interest, srv_link);
  50. if (vi->vid < new_vi->vid)
  51. continue;
  52. if (vi->vid > new_vi->vid)
  53. break;
  54. vi->usage++;
  55. goto found_vi;
  56. }
  57. new_vi->srv_link.pprev = pp;
  58. new_vi->srv_link.next = *pp;
  59. if (*pp)
  60. (*pp)->pprev = &new_vi->srv_link.next;
  61. *pp = &new_vi->srv_link;
  62. vi = new_vi;
  63. new_vi = NULL;
  64. found_vi:
  65. new->vol_interest = vi;
  66. hlist_add_head(&new->cb_vlink, &vi->cb_interests);
  67. write_unlock(&server->cb_break_lock);
  68. kfree(new_vi);
  69. return new;
  70. }
  71. /*
  72. * Set up an interest-in-callbacks record for a volume on a server and
  73. * register it with the server.
  74. * - Called with vnode->io_lock held.
  75. */
  76. int afs_register_server_cb_interest(struct afs_vnode *vnode,
  77. struct afs_server_list *slist,
  78. unsigned int index)
  79. {
  80. struct afs_server_entry *entry = &slist->servers[index];
  81. struct afs_cb_interest *cbi, *vcbi, *new, *old;
  82. struct afs_server *server = entry->server;
  83. again:
  84. if (vnode->cb_interest &&
  85. likely(vnode->cb_interest == entry->cb_interest))
  86. return 0;
  87. read_lock(&slist->lock);
  88. cbi = afs_get_cb_interest(entry->cb_interest);
  89. read_unlock(&slist->lock);
  90. vcbi = vnode->cb_interest;
  91. if (vcbi) {
  92. if (vcbi == cbi) {
  93. afs_put_cb_interest(afs_v2net(vnode), cbi);
  94. return 0;
  95. }
  96. /* Use a new interest in the server list for the same server
  97. * rather than an old one that's still attached to a vnode.
  98. */
  99. if (cbi && vcbi->server == cbi->server) {
  100. write_seqlock(&vnode->cb_lock);
  101. old = vnode->cb_interest;
  102. vnode->cb_interest = cbi;
  103. write_sequnlock(&vnode->cb_lock);
  104. afs_put_cb_interest(afs_v2net(vnode), old);
  105. return 0;
  106. }
  107. /* Re-use the one attached to the vnode. */
  108. if (!cbi && vcbi->server == server) {
  109. write_lock(&slist->lock);
  110. if (entry->cb_interest) {
  111. write_unlock(&slist->lock);
  112. afs_put_cb_interest(afs_v2net(vnode), cbi);
  113. goto again;
  114. }
  115. entry->cb_interest = cbi;
  116. write_unlock(&slist->lock);
  117. return 0;
  118. }
  119. }
  120. if (!cbi) {
  121. new = afs_create_interest(server, vnode);
  122. if (!new)
  123. return -ENOMEM;
  124. write_lock(&slist->lock);
  125. if (!entry->cb_interest) {
  126. entry->cb_interest = afs_get_cb_interest(new);
  127. cbi = new;
  128. new = NULL;
  129. } else {
  130. cbi = afs_get_cb_interest(entry->cb_interest);
  131. }
  132. write_unlock(&slist->lock);
  133. afs_put_cb_interest(afs_v2net(vnode), new);
  134. }
  135. ASSERT(cbi);
  136. /* Change the server the vnode is using. This entails scrubbing any
  137. * interest the vnode had in the previous server it was using.
  138. */
  139. write_seqlock(&vnode->cb_lock);
  140. old = vnode->cb_interest;
  141. vnode->cb_interest = cbi;
  142. vnode->cb_s_break = cbi->server->cb_s_break;
  143. vnode->cb_v_break = vnode->volume->cb_v_break;
  144. clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  145. write_sequnlock(&vnode->cb_lock);
  146. afs_put_cb_interest(afs_v2net(vnode), old);
  147. return 0;
  148. }
  149. /*
  150. * Remove an interest on a server.
  151. */
  152. void afs_put_cb_interest(struct afs_net *net, struct afs_cb_interest *cbi)
  153. {
  154. struct afs_vol_interest *vi;
  155. if (cbi && refcount_dec_and_test(&cbi->usage)) {
  156. if (!hlist_unhashed(&cbi->cb_vlink)) {
  157. write_lock(&cbi->server->cb_break_lock);
  158. hlist_del_init(&cbi->cb_vlink);
  159. vi = cbi->vol_interest;
  160. cbi->vol_interest = NULL;
  161. if (--vi->usage == 0)
  162. hlist_del(&vi->srv_link);
  163. else
  164. vi = NULL;
  165. write_unlock(&cbi->server->cb_break_lock);
  166. kfree(vi);
  167. afs_put_server(net, cbi->server);
  168. }
  169. kfree(cbi);
  170. }
  171. }
  172. /*
  173. * allow the fileserver to request callback state (re-)initialisation
  174. */
  175. void afs_init_callback_state(struct afs_server *server)
  176. {
  177. if (!test_and_clear_bit(AFS_SERVER_FL_NEW, &server->flags))
  178. server->cb_s_break++;
  179. }
  180. /*
  181. * actually break a callback
  182. */
  183. void afs_break_callback(struct afs_vnode *vnode)
  184. {
  185. _enter("");
  186. write_seqlock(&vnode->cb_lock);
  187. clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
  188. if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
  189. vnode->cb_break++;
  190. afs_clear_permits(vnode);
  191. if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
  192. afs_lock_may_be_available(vnode);
  193. }
  194. write_sequnlock(&vnode->cb_lock);
  195. }
  196. /*
  197. * allow the fileserver to explicitly break one callback
  198. * - happens when
  199. * - the backing file is changed
  200. * - a lock is released
  201. */
  202. static void afs_break_one_callback(struct afs_server *server,
  203. struct afs_fid *fid)
  204. {
  205. struct afs_vol_interest *vi;
  206. struct afs_cb_interest *cbi;
  207. struct afs_iget_data data;
  208. struct afs_vnode *vnode;
  209. struct inode *inode;
  210. read_lock(&server->cb_break_lock);
  211. hlist_for_each_entry(vi, &server->cb_volumes, srv_link) {
  212. if (vi->vid < fid->vid)
  213. continue;
  214. if (vi->vid > fid->vid) {
  215. vi = NULL;
  216. break;
  217. }
  218. //atomic_inc(&vi->usage);
  219. break;
  220. }
  221. /* TODO: Find all matching volumes if we couldn't match the server and
  222. * break them anyway.
  223. */
  224. if (!vi)
  225. goto out;
  226. /* Step through all interested superblocks. There may be more than one
  227. * because of cell aliasing.
  228. */
  229. hlist_for_each_entry(cbi, &vi->cb_interests, cb_vlink) {
  230. if (fid->vnode == 0 && fid->unique == 0) {
  231. /* The callback break applies to an entire volume. */
  232. struct afs_super_info *as = AFS_FS_S(cbi->sb);
  233. struct afs_volume *volume = as->volume;
  234. write_lock(&volume->cb_v_break_lock);
  235. volume->cb_v_break++;
  236. write_unlock(&volume->cb_v_break_lock);
  237. } else {
  238. data.volume = NULL;
  239. data.fid = *fid;
  240. inode = ilookup5_nowait(cbi->sb, fid->vnode,
  241. afs_iget5_test, &data);
  242. if (inode) {
  243. vnode = AFS_FS_I(inode);
  244. afs_break_callback(vnode);
  245. iput(inode);
  246. }
  247. }
  248. }
  249. out:
  250. read_unlock(&server->cb_break_lock);
  251. }
  252. /*
  253. * allow the fileserver to break callback promises
  254. */
  255. void afs_break_callbacks(struct afs_server *server, size_t count,
  256. struct afs_callback_break *callbacks)
  257. {
  258. _enter("%p,%zu,", server, count);
  259. ASSERT(server != NULL);
  260. ASSERTCMP(count, <=, AFSCBMAX);
  261. /* TODO: Sort the callback break list by volume ID */
  262. for (; count > 0; callbacks++, count--) {
  263. _debug("- Fid { vl=%08x n=%u u=%u } CB { v=%u x=%u t=%u }",
  264. callbacks->fid.vid,
  265. callbacks->fid.vnode,
  266. callbacks->fid.unique,
  267. callbacks->cb.version,
  268. callbacks->cb.expiry,
  269. callbacks->cb.type
  270. );
  271. afs_break_one_callback(server, &callbacks->fid);
  272. }
  273. _leave("");
  274. return;
  275. }
  276. /*
  277. * Clear the callback interests in a server list.
  278. */
  279. void afs_clear_callback_interests(struct afs_net *net, struct afs_server_list *slist)
  280. {
  281. int i;
  282. for (i = 0; i < slist->nr_servers; i++) {
  283. afs_put_cb_interest(net, slist->servers[i].cb_interest);
  284. slist->servers[i].cb_interest = NULL;
  285. }
  286. }