vnode.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /* vnode.c: AFS vnode management
  2. *
  3. * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/fs.h>
  16. #include <linux/pagemap.h>
  17. #include "volume.h"
  18. #include "cell.h"
  19. #include "cmservice.h"
  20. #include "fsclient.h"
  21. #include "vlclient.h"
  22. #include "vnode.h"
  23. #include "internal.h"
  24. static void afs_vnode_cb_timed_out(struct afs_timer *timer);
  25. struct afs_timer_ops afs_vnode_cb_timed_out_ops = {
  26. .timed_out = afs_vnode_cb_timed_out,
  27. };
  28. #ifdef AFS_CACHING_SUPPORT
  29. static cachefs_match_val_t afs_vnode_cache_match(void *target,
  30. const void *entry);
  31. static void afs_vnode_cache_update(void *source, void *entry);
  32. struct cachefs_index_def afs_vnode_cache_index_def = {
  33. .name = "vnode",
  34. .data_size = sizeof(struct afs_cache_vnode),
  35. .keys[0] = { CACHEFS_INDEX_KEYS_BIN, 4 },
  36. .match = afs_vnode_cache_match,
  37. .update = afs_vnode_cache_update,
  38. };
  39. #endif
  40. /*****************************************************************************/
  41. /*
  42. * handle a callback timing out
  43. * TODO: retain a ref to vnode struct for an outstanding callback timeout
  44. */
  45. static void afs_vnode_cb_timed_out(struct afs_timer *timer)
  46. {
  47. struct afs_server *oldserver;
  48. struct afs_vnode *vnode;
  49. vnode = list_entry(timer, struct afs_vnode, cb_timeout);
  50. _enter("%p", vnode);
  51. /* set the changed flag in the vnode and release the server */
  52. spin_lock(&vnode->lock);
  53. oldserver = xchg(&vnode->cb_server, NULL);
  54. if (oldserver) {
  55. vnode->flags |= AFS_VNODE_CHANGED;
  56. spin_lock(&afs_cb_hash_lock);
  57. list_del_init(&vnode->cb_hash_link);
  58. spin_unlock(&afs_cb_hash_lock);
  59. spin_lock(&oldserver->cb_lock);
  60. list_del_init(&vnode->cb_link);
  61. spin_unlock(&oldserver->cb_lock);
  62. }
  63. spin_unlock(&vnode->lock);
  64. afs_put_server(oldserver);
  65. _leave("");
  66. } /* end afs_vnode_cb_timed_out() */
  67. /*****************************************************************************/
  68. /*
  69. * finish off updating the recorded status of a file
  70. * - starts callback expiry timer
  71. * - adds to server's callback list
  72. */
  73. static void afs_vnode_finalise_status_update(struct afs_vnode *vnode,
  74. struct afs_server *server,
  75. int ret)
  76. {
  77. struct afs_server *oldserver = NULL;
  78. _enter("%p,%p,%d", vnode, server, ret);
  79. spin_lock(&vnode->lock);
  80. vnode->flags &= ~AFS_VNODE_CHANGED;
  81. if (ret == 0) {
  82. /* adjust the callback timeout appropriately */
  83. afs_kafstimod_add_timer(&vnode->cb_timeout,
  84. vnode->cb_expiry * HZ);
  85. spin_lock(&afs_cb_hash_lock);
  86. list_move_tail(&vnode->cb_hash_link,
  87. &afs_cb_hash(server, &vnode->fid));
  88. spin_unlock(&afs_cb_hash_lock);
  89. /* swap ref to old callback server with that for new callback
  90. * server */
  91. oldserver = xchg(&vnode->cb_server, server);
  92. if (oldserver != server) {
  93. if (oldserver) {
  94. spin_lock(&oldserver->cb_lock);
  95. list_del_init(&vnode->cb_link);
  96. spin_unlock(&oldserver->cb_lock);
  97. }
  98. afs_get_server(server);
  99. spin_lock(&server->cb_lock);
  100. list_add_tail(&vnode->cb_link, &server->cb_promises);
  101. spin_unlock(&server->cb_lock);
  102. }
  103. else {
  104. /* same server */
  105. oldserver = NULL;
  106. }
  107. }
  108. else if (ret == -ENOENT) {
  109. /* the file was deleted - clear the callback timeout */
  110. oldserver = xchg(&vnode->cb_server, NULL);
  111. afs_kafstimod_del_timer(&vnode->cb_timeout);
  112. _debug("got NOENT from server - marking file deleted");
  113. vnode->flags |= AFS_VNODE_DELETED;
  114. }
  115. vnode->update_cnt--;
  116. spin_unlock(&vnode->lock);
  117. wake_up_all(&vnode->update_waitq);
  118. afs_put_server(oldserver);
  119. _leave("");
  120. } /* end afs_vnode_finalise_status_update() */
  121. /*****************************************************************************/
  122. /*
  123. * fetch file status from the volume
  124. * - don't issue a fetch if:
  125. * - the changed bit is not set and there's a valid callback
  126. * - there are any outstanding ops that will fetch the status
  127. * - TODO implement local caching
  128. */
  129. int afs_vnode_fetch_status(struct afs_vnode *vnode)
  130. {
  131. struct afs_server *server;
  132. int ret;
  133. DECLARE_WAITQUEUE(myself, current);
  134. _enter("%s,{%u,%u,%u}",
  135. vnode->volume->vlocation->vldb.name,
  136. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
  137. if (!(vnode->flags & AFS_VNODE_CHANGED) && vnode->cb_server) {
  138. _leave(" [unchanged]");
  139. return 0;
  140. }
  141. if (vnode->flags & AFS_VNODE_DELETED) {
  142. _leave(" [deleted]");
  143. return -ENOENT;
  144. }
  145. spin_lock(&vnode->lock);
  146. if (!(vnode->flags & AFS_VNODE_CHANGED)) {
  147. spin_unlock(&vnode->lock);
  148. _leave(" [unchanged]");
  149. return 0;
  150. }
  151. if (vnode->update_cnt > 0) {
  152. /* someone else started a fetch */
  153. set_current_state(TASK_UNINTERRUPTIBLE);
  154. add_wait_queue(&vnode->update_waitq, &myself);
  155. /* wait for the status to be updated */
  156. for (;;) {
  157. if (!(vnode->flags & AFS_VNODE_CHANGED))
  158. break;
  159. if (vnode->flags & AFS_VNODE_DELETED)
  160. break;
  161. /* it got updated and invalidated all before we saw
  162. * it */
  163. if (vnode->update_cnt == 0) {
  164. remove_wait_queue(&vnode->update_waitq,
  165. &myself);
  166. set_current_state(TASK_RUNNING);
  167. goto get_anyway;
  168. }
  169. spin_unlock(&vnode->lock);
  170. schedule();
  171. set_current_state(TASK_UNINTERRUPTIBLE);
  172. spin_lock(&vnode->lock);
  173. }
  174. remove_wait_queue(&vnode->update_waitq, &myself);
  175. spin_unlock(&vnode->lock);
  176. set_current_state(TASK_RUNNING);
  177. return vnode->flags & AFS_VNODE_DELETED ? -ENOENT : 0;
  178. }
  179. get_anyway:
  180. /* okay... we're going to have to initiate the op */
  181. vnode->update_cnt++;
  182. spin_unlock(&vnode->lock);
  183. /* merge AFS status fetches and clear outstanding callback on this
  184. * vnode */
  185. do {
  186. /* pick a server to query */
  187. ret = afs_volume_pick_fileserver(vnode->volume, &server);
  188. if (ret<0)
  189. return ret;
  190. _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
  191. ret = afs_rxfs_fetch_file_status(server, vnode, NULL);
  192. } while (!afs_volume_release_fileserver(vnode->volume, server, ret));
  193. /* adjust the flags */
  194. afs_vnode_finalise_status_update(vnode, server, ret);
  195. _leave(" = %d", ret);
  196. return ret;
  197. } /* end afs_vnode_fetch_status() */
  198. /*****************************************************************************/
  199. /*
  200. * fetch file data from the volume
  201. * - TODO implement caching and server failover
  202. */
  203. int afs_vnode_fetch_data(struct afs_vnode *vnode,
  204. struct afs_rxfs_fetch_descriptor *desc)
  205. {
  206. struct afs_server *server;
  207. int ret;
  208. _enter("%s,{%u,%u,%u}",
  209. vnode->volume->vlocation->vldb.name,
  210. vnode->fid.vid,
  211. vnode->fid.vnode,
  212. vnode->fid.unique);
  213. /* this op will fetch the status */
  214. spin_lock(&vnode->lock);
  215. vnode->update_cnt++;
  216. spin_unlock(&vnode->lock);
  217. /* merge in AFS status fetches and clear outstanding callback on this
  218. * vnode */
  219. do {
  220. /* pick a server to query */
  221. ret = afs_volume_pick_fileserver(vnode->volume, &server);
  222. if (ret < 0)
  223. return ret;
  224. _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
  225. ret = afs_rxfs_fetch_file_data(server, vnode, desc, NULL);
  226. } while (!afs_volume_release_fileserver(vnode->volume, server, ret));
  227. /* adjust the flags */
  228. afs_vnode_finalise_status_update(vnode, server, ret);
  229. _leave(" = %d", ret);
  230. return ret;
  231. } /* end afs_vnode_fetch_data() */
  232. /*****************************************************************************/
  233. /*
  234. * break any outstanding callback on a vnode
  235. * - only relevent to server that issued it
  236. */
  237. int afs_vnode_give_up_callback(struct afs_vnode *vnode)
  238. {
  239. struct afs_server *server;
  240. int ret;
  241. _enter("%s,{%u,%u,%u}",
  242. vnode->volume->vlocation->vldb.name,
  243. vnode->fid.vid,
  244. vnode->fid.vnode,
  245. vnode->fid.unique);
  246. spin_lock(&afs_cb_hash_lock);
  247. list_del_init(&vnode->cb_hash_link);
  248. spin_unlock(&afs_cb_hash_lock);
  249. /* set the changed flag in the vnode and release the server */
  250. spin_lock(&vnode->lock);
  251. afs_kafstimod_del_timer(&vnode->cb_timeout);
  252. server = xchg(&vnode->cb_server, NULL);
  253. if (server) {
  254. vnode->flags |= AFS_VNODE_CHANGED;
  255. spin_lock(&server->cb_lock);
  256. list_del_init(&vnode->cb_link);
  257. spin_unlock(&server->cb_lock);
  258. }
  259. spin_unlock(&vnode->lock);
  260. ret = 0;
  261. if (server) {
  262. ret = afs_rxfs_give_up_callback(server, vnode);
  263. afs_put_server(server);
  264. }
  265. _leave(" = %d", ret);
  266. return ret;
  267. } /* end afs_vnode_give_up_callback() */
  268. /*****************************************************************************/
  269. /*
  270. * match a vnode record stored in the cache
  271. */
  272. #ifdef AFS_CACHING_SUPPORT
  273. static cachefs_match_val_t afs_vnode_cache_match(void *target,
  274. const void *entry)
  275. {
  276. const struct afs_cache_vnode *cvnode = entry;
  277. struct afs_vnode *vnode = target;
  278. _enter("{%x,%x,%Lx},{%x,%x,%Lx}",
  279. vnode->fid.vnode,
  280. vnode->fid.unique,
  281. vnode->status.version,
  282. cvnode->vnode_id,
  283. cvnode->vnode_unique,
  284. cvnode->data_version);
  285. if (vnode->fid.vnode != cvnode->vnode_id) {
  286. _leave(" = FAILED");
  287. return CACHEFS_MATCH_FAILED;
  288. }
  289. if (vnode->fid.unique != cvnode->vnode_unique ||
  290. vnode->status.version != cvnode->data_version) {
  291. _leave(" = DELETE");
  292. return CACHEFS_MATCH_SUCCESS_DELETE;
  293. }
  294. _leave(" = SUCCESS");
  295. return CACHEFS_MATCH_SUCCESS;
  296. } /* end afs_vnode_cache_match() */
  297. #endif
  298. /*****************************************************************************/
  299. /*
  300. * update a vnode record stored in the cache
  301. */
  302. #ifdef AFS_CACHING_SUPPORT
  303. static void afs_vnode_cache_update(void *source, void *entry)
  304. {
  305. struct afs_cache_vnode *cvnode = entry;
  306. struct afs_vnode *vnode = source;
  307. _enter("");
  308. cvnode->vnode_id = vnode->fid.vnode;
  309. cvnode->vnode_unique = vnode->fid.unique;
  310. cvnode->data_version = vnode->status.version;
  311. } /* end afs_vnode_cache_update() */
  312. #endif