vnode.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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_del(&vnode->cb_hash_link);
  87. list_add_tail(&vnode->cb_hash_link,
  88. &afs_cb_hash(server, &vnode->fid));
  89. spin_unlock(&afs_cb_hash_lock);
  90. /* swap ref to old callback server with that for new callback
  91. * server */
  92. oldserver = xchg(&vnode->cb_server, server);
  93. if (oldserver != server) {
  94. if (oldserver) {
  95. spin_lock(&oldserver->cb_lock);
  96. list_del_init(&vnode->cb_link);
  97. spin_unlock(&oldserver->cb_lock);
  98. }
  99. afs_get_server(server);
  100. spin_lock(&server->cb_lock);
  101. list_add_tail(&vnode->cb_link, &server->cb_promises);
  102. spin_unlock(&server->cb_lock);
  103. }
  104. else {
  105. /* same server */
  106. oldserver = NULL;
  107. }
  108. }
  109. else if (ret == -ENOENT) {
  110. /* the file was deleted - clear the callback timeout */
  111. oldserver = xchg(&vnode->cb_server, NULL);
  112. afs_kafstimod_del_timer(&vnode->cb_timeout);
  113. _debug("got NOENT from server - marking file deleted");
  114. vnode->flags |= AFS_VNODE_DELETED;
  115. }
  116. vnode->update_cnt--;
  117. spin_unlock(&vnode->lock);
  118. wake_up_all(&vnode->update_waitq);
  119. afs_put_server(oldserver);
  120. _leave("");
  121. } /* end afs_vnode_finalise_status_update() */
  122. /*****************************************************************************/
  123. /*
  124. * fetch file status from the volume
  125. * - don't issue a fetch if:
  126. * - the changed bit is not set and there's a valid callback
  127. * - there are any outstanding ops that will fetch the status
  128. * - TODO implement local caching
  129. */
  130. int afs_vnode_fetch_status(struct afs_vnode *vnode)
  131. {
  132. struct afs_server *server;
  133. int ret;
  134. DECLARE_WAITQUEUE(myself, current);
  135. _enter("%s,{%u,%u,%u}",
  136. vnode->volume->vlocation->vldb.name,
  137. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
  138. if (!(vnode->flags & AFS_VNODE_CHANGED) && vnode->cb_server) {
  139. _leave(" [unchanged]");
  140. return 0;
  141. }
  142. if (vnode->flags & AFS_VNODE_DELETED) {
  143. _leave(" [deleted]");
  144. return -ENOENT;
  145. }
  146. spin_lock(&vnode->lock);
  147. if (!(vnode->flags & AFS_VNODE_CHANGED)) {
  148. spin_unlock(&vnode->lock);
  149. _leave(" [unchanged]");
  150. return 0;
  151. }
  152. if (vnode->update_cnt > 0) {
  153. /* someone else started a fetch */
  154. set_current_state(TASK_UNINTERRUPTIBLE);
  155. add_wait_queue(&vnode->update_waitq, &myself);
  156. /* wait for the status to be updated */
  157. for (;;) {
  158. if (!(vnode->flags & AFS_VNODE_CHANGED))
  159. break;
  160. if (vnode->flags & AFS_VNODE_DELETED)
  161. break;
  162. /* it got updated and invalidated all before we saw
  163. * it */
  164. if (vnode->update_cnt == 0) {
  165. remove_wait_queue(&vnode->update_waitq,
  166. &myself);
  167. set_current_state(TASK_RUNNING);
  168. goto get_anyway;
  169. }
  170. spin_unlock(&vnode->lock);
  171. schedule();
  172. set_current_state(TASK_UNINTERRUPTIBLE);
  173. spin_lock(&vnode->lock);
  174. }
  175. remove_wait_queue(&vnode->update_waitq, &myself);
  176. spin_unlock(&vnode->lock);
  177. set_current_state(TASK_RUNNING);
  178. return vnode->flags & AFS_VNODE_DELETED ? -ENOENT : 0;
  179. }
  180. get_anyway:
  181. /* okay... we're going to have to initiate the op */
  182. vnode->update_cnt++;
  183. spin_unlock(&vnode->lock);
  184. /* merge AFS status fetches and clear outstanding callback on this
  185. * vnode */
  186. do {
  187. /* pick a server to query */
  188. ret = afs_volume_pick_fileserver(vnode->volume, &server);
  189. if (ret<0)
  190. return ret;
  191. _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
  192. ret = afs_rxfs_fetch_file_status(server, vnode, NULL);
  193. } while (!afs_volume_release_fileserver(vnode->volume, server, ret));
  194. /* adjust the flags */
  195. afs_vnode_finalise_status_update(vnode, server, ret);
  196. _leave(" = %d", ret);
  197. return ret;
  198. } /* end afs_vnode_fetch_status() */
  199. /*****************************************************************************/
  200. /*
  201. * fetch file data from the volume
  202. * - TODO implement caching and server failover
  203. */
  204. int afs_vnode_fetch_data(struct afs_vnode *vnode,
  205. struct afs_rxfs_fetch_descriptor *desc)
  206. {
  207. struct afs_server *server;
  208. int ret;
  209. _enter("%s,{%u,%u,%u}",
  210. vnode->volume->vlocation->vldb.name,
  211. vnode->fid.vid,
  212. vnode->fid.vnode,
  213. vnode->fid.unique);
  214. /* this op will fetch the status */
  215. spin_lock(&vnode->lock);
  216. vnode->update_cnt++;
  217. spin_unlock(&vnode->lock);
  218. /* merge in AFS status fetches and clear outstanding callback on this
  219. * vnode */
  220. do {
  221. /* pick a server to query */
  222. ret = afs_volume_pick_fileserver(vnode->volume, &server);
  223. if (ret < 0)
  224. return ret;
  225. _debug("USING SERVER: %08x\n", ntohl(server->addr.s_addr));
  226. ret = afs_rxfs_fetch_file_data(server, vnode, desc, NULL);
  227. } while (!afs_volume_release_fileserver(vnode->volume, server, ret));
  228. /* adjust the flags */
  229. afs_vnode_finalise_status_update(vnode, server, ret);
  230. _leave(" = %d", ret);
  231. return ret;
  232. } /* end afs_vnode_fetch_data() */
  233. /*****************************************************************************/
  234. /*
  235. * break any outstanding callback on a vnode
  236. * - only relevent to server that issued it
  237. */
  238. int afs_vnode_give_up_callback(struct afs_vnode *vnode)
  239. {
  240. struct afs_server *server;
  241. int ret;
  242. _enter("%s,{%u,%u,%u}",
  243. vnode->volume->vlocation->vldb.name,
  244. vnode->fid.vid,
  245. vnode->fid.vnode,
  246. vnode->fid.unique);
  247. spin_lock(&afs_cb_hash_lock);
  248. list_del_init(&vnode->cb_hash_link);
  249. spin_unlock(&afs_cb_hash_lock);
  250. /* set the changed flag in the vnode and release the server */
  251. spin_lock(&vnode->lock);
  252. afs_kafstimod_del_timer(&vnode->cb_timeout);
  253. server = xchg(&vnode->cb_server, NULL);
  254. if (server) {
  255. vnode->flags |= AFS_VNODE_CHANGED;
  256. spin_lock(&server->cb_lock);
  257. list_del_init(&vnode->cb_link);
  258. spin_unlock(&server->cb_lock);
  259. }
  260. spin_unlock(&vnode->lock);
  261. ret = 0;
  262. if (server) {
  263. ret = afs_rxfs_give_up_callback(server, vnode);
  264. afs_put_server(server);
  265. }
  266. _leave(" = %d", ret);
  267. return ret;
  268. } /* end afs_vnode_give_up_callback() */
  269. /*****************************************************************************/
  270. /*
  271. * match a vnode record stored in the cache
  272. */
  273. #ifdef AFS_CACHING_SUPPORT
  274. static cachefs_match_val_t afs_vnode_cache_match(void *target,
  275. const void *entry)
  276. {
  277. const struct afs_cache_vnode *cvnode = entry;
  278. struct afs_vnode *vnode = target;
  279. _enter("{%x,%x,%Lx},{%x,%x,%Lx}",
  280. vnode->fid.vnode,
  281. vnode->fid.unique,
  282. vnode->status.version,
  283. cvnode->vnode_id,
  284. cvnode->vnode_unique,
  285. cvnode->data_version);
  286. if (vnode->fid.vnode != cvnode->vnode_id) {
  287. _leave(" = FAILED");
  288. return CACHEFS_MATCH_FAILED;
  289. }
  290. if (vnode->fid.unique != cvnode->vnode_unique ||
  291. vnode->status.version != cvnode->data_version) {
  292. _leave(" = DELETE");
  293. return CACHEFS_MATCH_SUCCESS_DELETE;
  294. }
  295. _leave(" = SUCCESS");
  296. return CACHEFS_MATCH_SUCCESS;
  297. } /* end afs_vnode_cache_match() */
  298. #endif
  299. /*****************************************************************************/
  300. /*
  301. * update a vnode record stored in the cache
  302. */
  303. #ifdef AFS_CACHING_SUPPORT
  304. static void afs_vnode_cache_update(void *source, void *entry)
  305. {
  306. struct afs_cache_vnode *cvnode = entry;
  307. struct afs_vnode *vnode = source;
  308. _enter("");
  309. cvnode->vnode_id = vnode->fid.vnode;
  310. cvnode->vnode_unique = vnode->fid.unique;
  311. cvnode->data_version = vnode->status.version;
  312. } /* end afs_vnode_cache_update() */
  313. #endif