uptodate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * uptodate.c
  5. *
  6. * Tracking the up-to-date-ness of a local buffer_head with respect to
  7. * the cluster.
  8. *
  9. * Copyright (C) 2002, 2004, 2005 Oracle. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public
  22. * License along with this program; if not, write to the
  23. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. * Boston, MA 021110-1307, USA.
  25. *
  26. * Standard buffer head caching flags (uptodate, etc) are insufficient
  27. * in a clustered environment - a buffer may be marked up to date on
  28. * our local node but could have been modified by another cluster
  29. * member. As a result an additional (and performant) caching scheme
  30. * is required. A further requirement is that we consume as little
  31. * memory as possible - we never pin buffer_head structures in order
  32. * to cache them.
  33. *
  34. * We track the existence of up to date buffers on the inodes which
  35. * are associated with them. Because we don't want to pin
  36. * buffer_heads, this is only a (strong) hint and several other checks
  37. * are made in the I/O path to ensure that we don't use a stale or
  38. * invalid buffer without going to disk:
  39. * - buffer_jbd is used liberally - if a bh is in the journal on
  40. * this node then it *must* be up to date.
  41. * - the standard buffer_uptodate() macro is used to detect buffers
  42. * which may be invalid (even if we have an up to date tracking
  43. * item for them)
  44. *
  45. * For a full understanding of how this code works together, one
  46. * should read the callers in dlmglue.c, the I/O functions in
  47. * buffer_head_io.c and ocfs2_journal_access in journal.c
  48. */
  49. #include <linux/fs.h>
  50. #include <linux/types.h>
  51. #include <linux/slab.h>
  52. #include <linux/highmem.h>
  53. #include <linux/buffer_head.h>
  54. #include <linux/rbtree.h>
  55. #include <linux/jbd.h>
  56. #define MLOG_MASK_PREFIX ML_UPTODATE
  57. #include <cluster/masklog.h>
  58. #include "ocfs2.h"
  59. #include "inode.h"
  60. #include "uptodate.h"
  61. struct ocfs2_meta_cache_item {
  62. struct rb_node c_node;
  63. sector_t c_block;
  64. };
  65. static kmem_cache_t *ocfs2_uptodate_cachep = NULL;
  66. void ocfs2_metadata_cache_init(struct inode *inode)
  67. {
  68. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  69. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  70. oi->ip_flags |= OCFS2_INODE_CACHE_INLINE;
  71. ci->ci_num_cached = 0;
  72. }
  73. /* No lock taken here as 'root' is not expected to be visible to other
  74. * processes. */
  75. static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root)
  76. {
  77. unsigned int purged = 0;
  78. struct rb_node *node;
  79. struct ocfs2_meta_cache_item *item;
  80. while ((node = rb_last(root)) != NULL) {
  81. item = rb_entry(node, struct ocfs2_meta_cache_item, c_node);
  82. mlog(0, "Purge item %llu\n",
  83. (unsigned long long) item->c_block);
  84. rb_erase(&item->c_node, root);
  85. kmem_cache_free(ocfs2_uptodate_cachep, item);
  86. purged++;
  87. }
  88. return purged;
  89. }
  90. /* Called from locking and called from ocfs2_clear_inode. Dump the
  91. * cache for a given inode.
  92. *
  93. * This function is a few more lines longer than necessary due to some
  94. * accounting done here, but I think it's worth tracking down those
  95. * bugs sooner -- Mark */
  96. void ocfs2_metadata_cache_purge(struct inode *inode)
  97. {
  98. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  99. unsigned int tree, to_purge, purged;
  100. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  101. struct rb_root root = RB_ROOT;
  102. spin_lock(&oi->ip_lock);
  103. tree = !(oi->ip_flags & OCFS2_INODE_CACHE_INLINE);
  104. to_purge = ci->ci_num_cached;
  105. mlog(0, "Purge %u %s items from Inode %llu\n", to_purge,
  106. tree ? "array" : "tree", (unsigned long long)oi->ip_blkno);
  107. /* If we're a tree, save off the root so that we can safely
  108. * initialize the cache. We do the work to free tree members
  109. * without the spinlock. */
  110. if (tree)
  111. root = ci->ci_cache.ci_tree;
  112. ocfs2_metadata_cache_init(inode);
  113. spin_unlock(&oi->ip_lock);
  114. purged = ocfs2_purge_copied_metadata_tree(&root);
  115. /* If possible, track the number wiped so that we can more
  116. * easily detect counting errors. Unfortunately, this is only
  117. * meaningful for trees. */
  118. if (tree && purged != to_purge)
  119. mlog(ML_ERROR, "Inode %llu, count = %u, purged = %u\n",
  120. (unsigned long long)oi->ip_blkno, to_purge, purged);
  121. }
  122. /* Returns the index in the cache array, -1 if not found.
  123. * Requires ip_lock. */
  124. static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci,
  125. sector_t item)
  126. {
  127. int i;
  128. for (i = 0; i < ci->ci_num_cached; i++) {
  129. if (item == ci->ci_cache.ci_array[i])
  130. return i;
  131. }
  132. return -1;
  133. }
  134. /* Returns the cache item if found, otherwise NULL.
  135. * Requires ip_lock. */
  136. static struct ocfs2_meta_cache_item *
  137. ocfs2_search_cache_tree(struct ocfs2_caching_info *ci,
  138. sector_t block)
  139. {
  140. struct rb_node * n = ci->ci_cache.ci_tree.rb_node;
  141. struct ocfs2_meta_cache_item *item = NULL;
  142. while (n) {
  143. item = rb_entry(n, struct ocfs2_meta_cache_item, c_node);
  144. if (block < item->c_block)
  145. n = n->rb_left;
  146. else if (block > item->c_block)
  147. n = n->rb_right;
  148. else
  149. return item;
  150. }
  151. return NULL;
  152. }
  153. static int ocfs2_buffer_cached(struct ocfs2_inode_info *oi,
  154. struct buffer_head *bh)
  155. {
  156. int index = -1;
  157. struct ocfs2_meta_cache_item *item = NULL;
  158. spin_lock(&oi->ip_lock);
  159. mlog(0, "Inode %llu, query block %llu (inline = %u)\n",
  160. (unsigned long long)oi->ip_blkno,
  161. (unsigned long long) bh->b_blocknr,
  162. !!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE));
  163. if (oi->ip_flags & OCFS2_INODE_CACHE_INLINE)
  164. index = ocfs2_search_cache_array(&oi->ip_metadata_cache,
  165. bh->b_blocknr);
  166. else
  167. item = ocfs2_search_cache_tree(&oi->ip_metadata_cache,
  168. bh->b_blocknr);
  169. spin_unlock(&oi->ip_lock);
  170. mlog(0, "index = %d, item = %p\n", index, item);
  171. return (index != -1) || (item != NULL);
  172. }
  173. /* Warning: even if it returns true, this does *not* guarantee that
  174. * the block is stored in our inode metadata cache. */
  175. int ocfs2_buffer_uptodate(struct inode *inode,
  176. struct buffer_head *bh)
  177. {
  178. /* Doesn't matter if the bh is in our cache or not -- if it's
  179. * not marked uptodate then we know it can't have correct
  180. * data. */
  181. if (!buffer_uptodate(bh))
  182. return 0;
  183. /* OCFS2 does not allow multiple nodes to be changing the same
  184. * block at the same time. */
  185. if (buffer_jbd(bh))
  186. return 1;
  187. /* Ok, locally the buffer is marked as up to date, now search
  188. * our cache to see if we can trust that. */
  189. return ocfs2_buffer_cached(OCFS2_I(inode), bh);
  190. }
  191. /* Requires ip_lock */
  192. static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci,
  193. sector_t block)
  194. {
  195. BUG_ON(ci->ci_num_cached >= OCFS2_INODE_MAX_CACHE_ARRAY);
  196. mlog(0, "block %llu takes position %u\n", (unsigned long long) block,
  197. ci->ci_num_cached);
  198. ci->ci_cache.ci_array[ci->ci_num_cached] = block;
  199. ci->ci_num_cached++;
  200. }
  201. /* By now the caller should have checked that the item does *not*
  202. * exist in the tree.
  203. * Requires ip_lock. */
  204. static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci,
  205. struct ocfs2_meta_cache_item *new)
  206. {
  207. sector_t block = new->c_block;
  208. struct rb_node *parent = NULL;
  209. struct rb_node **p = &ci->ci_cache.ci_tree.rb_node;
  210. struct ocfs2_meta_cache_item *tmp;
  211. mlog(0, "Insert block %llu num = %u\n", (unsigned long long) block,
  212. ci->ci_num_cached);
  213. while(*p) {
  214. parent = *p;
  215. tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node);
  216. if (block < tmp->c_block)
  217. p = &(*p)->rb_left;
  218. else if (block > tmp->c_block)
  219. p = &(*p)->rb_right;
  220. else {
  221. /* This should never happen! */
  222. mlog(ML_ERROR, "Duplicate block %llu cached!\n",
  223. (unsigned long long) block);
  224. BUG();
  225. }
  226. }
  227. rb_link_node(&new->c_node, parent, p);
  228. rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree);
  229. ci->ci_num_cached++;
  230. }
  231. static inline int ocfs2_insert_can_use_array(struct ocfs2_inode_info *oi,
  232. struct ocfs2_caching_info *ci)
  233. {
  234. assert_spin_locked(&oi->ip_lock);
  235. return (oi->ip_flags & OCFS2_INODE_CACHE_INLINE) &&
  236. (ci->ci_num_cached < OCFS2_INODE_MAX_CACHE_ARRAY);
  237. }
  238. /* tree should be exactly OCFS2_INODE_MAX_CACHE_ARRAY wide. NULL the
  239. * pointers in tree after we use them - this allows caller to detect
  240. * when to free in case of error. */
  241. static void ocfs2_expand_cache(struct ocfs2_inode_info *oi,
  242. struct ocfs2_meta_cache_item **tree)
  243. {
  244. int i;
  245. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  246. mlog_bug_on_msg(ci->ci_num_cached != OCFS2_INODE_MAX_CACHE_ARRAY,
  247. "Inode %llu, num cached = %u, should be %u\n",
  248. (unsigned long long)oi->ip_blkno, ci->ci_num_cached,
  249. OCFS2_INODE_MAX_CACHE_ARRAY);
  250. mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
  251. "Inode %llu not marked as inline anymore!\n",
  252. (unsigned long long)oi->ip_blkno);
  253. assert_spin_locked(&oi->ip_lock);
  254. /* Be careful to initialize the tree members *first* because
  255. * once the ci_tree is used, the array is junk... */
  256. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++)
  257. tree[i]->c_block = ci->ci_cache.ci_array[i];
  258. oi->ip_flags &= ~OCFS2_INODE_CACHE_INLINE;
  259. ci->ci_cache.ci_tree = RB_ROOT;
  260. /* this will be set again by __ocfs2_insert_cache_tree */
  261. ci->ci_num_cached = 0;
  262. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++) {
  263. __ocfs2_insert_cache_tree(ci, tree[i]);
  264. tree[i] = NULL;
  265. }
  266. mlog(0, "Expanded %llu to a tree cache: flags 0x%x, num = %u\n",
  267. (unsigned long long)oi->ip_blkno, oi->ip_flags, ci->ci_num_cached);
  268. }
  269. /* Slow path function - memory allocation is necessary. See the
  270. * comment above ocfs2_set_buffer_uptodate for more information. */
  271. static void __ocfs2_set_buffer_uptodate(struct ocfs2_inode_info *oi,
  272. sector_t block,
  273. int expand_tree)
  274. {
  275. int i;
  276. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  277. struct ocfs2_meta_cache_item *new = NULL;
  278. struct ocfs2_meta_cache_item *tree[OCFS2_INODE_MAX_CACHE_ARRAY] =
  279. { NULL, };
  280. mlog(0, "Inode %llu, block %llu, expand = %d\n",
  281. (unsigned long long)oi->ip_blkno,
  282. (unsigned long long)block, expand_tree);
  283. new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_KERNEL);
  284. if (!new) {
  285. mlog_errno(-ENOMEM);
  286. return;
  287. }
  288. new->c_block = block;
  289. if (expand_tree) {
  290. /* Do *not* allocate an array here - the removal code
  291. * has no way of tracking that. */
  292. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++) {
  293. tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep,
  294. GFP_KERNEL);
  295. if (!tree[i]) {
  296. mlog_errno(-ENOMEM);
  297. goto out_free;
  298. }
  299. /* These are initialized in ocfs2_expand_cache! */
  300. }
  301. }
  302. spin_lock(&oi->ip_lock);
  303. if (ocfs2_insert_can_use_array(oi, ci)) {
  304. mlog(0, "Someone cleared the tree underneath us\n");
  305. /* Ok, items were removed from the cache in between
  306. * locks. Detect this and revert back to the fast path */
  307. ocfs2_append_cache_array(ci, block);
  308. spin_unlock(&oi->ip_lock);
  309. goto out_free;
  310. }
  311. if (expand_tree)
  312. ocfs2_expand_cache(oi, tree);
  313. __ocfs2_insert_cache_tree(ci, new);
  314. spin_unlock(&oi->ip_lock);
  315. new = NULL;
  316. out_free:
  317. if (new)
  318. kmem_cache_free(ocfs2_uptodate_cachep, new);
  319. /* If these were used, then ocfs2_expand_cache re-set them to
  320. * NULL for us. */
  321. if (tree[0]) {
  322. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++)
  323. if (tree[i])
  324. kmem_cache_free(ocfs2_uptodate_cachep,
  325. tree[i]);
  326. }
  327. }
  328. /* Item insertion is guarded by ip_io_mutex, so the insertion path takes
  329. * advantage of this by not rechecking for a duplicate insert during
  330. * the slow case. Additionally, if the cache needs to be bumped up to
  331. * a tree, the code will not recheck after acquiring the lock --
  332. * multiple paths cannot be expanding to a tree at the same time.
  333. *
  334. * The slow path takes into account that items can be removed
  335. * (including the whole tree wiped and reset) when this process it out
  336. * allocating memory. In those cases, it reverts back to the fast
  337. * path.
  338. *
  339. * Note that this function may actually fail to insert the block if
  340. * memory cannot be allocated. This is not fatal however (but may
  341. * result in a performance penalty) */
  342. void ocfs2_set_buffer_uptodate(struct inode *inode,
  343. struct buffer_head *bh)
  344. {
  345. int expand;
  346. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  347. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  348. /* The block may very well exist in our cache already, so avoid
  349. * doing any more work in that case. */
  350. if (ocfs2_buffer_cached(oi, bh))
  351. return;
  352. mlog(0, "Inode %llu, inserting block %llu\n",
  353. (unsigned long long)oi->ip_blkno,
  354. (unsigned long long)bh->b_blocknr);
  355. /* No need to recheck under spinlock - insertion is guarded by
  356. * ip_io_mutex */
  357. spin_lock(&oi->ip_lock);
  358. if (ocfs2_insert_can_use_array(oi, ci)) {
  359. /* Fast case - it's an array and there's a free
  360. * spot. */
  361. ocfs2_append_cache_array(ci, bh->b_blocknr);
  362. spin_unlock(&oi->ip_lock);
  363. return;
  364. }
  365. expand = 0;
  366. if (oi->ip_flags & OCFS2_INODE_CACHE_INLINE) {
  367. /* We need to bump things up to a tree. */
  368. expand = 1;
  369. }
  370. spin_unlock(&oi->ip_lock);
  371. __ocfs2_set_buffer_uptodate(oi, bh->b_blocknr, expand);
  372. }
  373. /* Called against a newly allocated buffer. Most likely nobody should
  374. * be able to read this sort of metadata while it's still being
  375. * allocated, but this is careful to take ip_io_mutex anyway. */
  376. void ocfs2_set_new_buffer_uptodate(struct inode *inode,
  377. struct buffer_head *bh)
  378. {
  379. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  380. /* This should definitely *not* exist in our cache */
  381. BUG_ON(ocfs2_buffer_cached(oi, bh));
  382. set_buffer_uptodate(bh);
  383. mutex_lock(&oi->ip_io_mutex);
  384. ocfs2_set_buffer_uptodate(inode, bh);
  385. mutex_unlock(&oi->ip_io_mutex);
  386. }
  387. /* Requires ip_lock. */
  388. static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci,
  389. int index)
  390. {
  391. sector_t *array = ci->ci_cache.ci_array;
  392. int bytes;
  393. BUG_ON(index < 0 || index >= OCFS2_INODE_MAX_CACHE_ARRAY);
  394. BUG_ON(index >= ci->ci_num_cached);
  395. BUG_ON(!ci->ci_num_cached);
  396. mlog(0, "remove index %d (num_cached = %u\n", index,
  397. ci->ci_num_cached);
  398. ci->ci_num_cached--;
  399. /* don't need to copy if the array is now empty, or if we
  400. * removed at the tail */
  401. if (ci->ci_num_cached && index < ci->ci_num_cached) {
  402. bytes = sizeof(sector_t) * (ci->ci_num_cached - index);
  403. memmove(&array[index], &array[index + 1], bytes);
  404. }
  405. }
  406. /* Requires ip_lock. */
  407. static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci,
  408. struct ocfs2_meta_cache_item *item)
  409. {
  410. mlog(0, "remove block %llu from tree\n",
  411. (unsigned long long) item->c_block);
  412. rb_erase(&item->c_node, &ci->ci_cache.ci_tree);
  413. ci->ci_num_cached--;
  414. }
  415. /* Called when we remove a chunk of metadata from an inode. We don't
  416. * bother reverting things to an inlined array in the case of a remove
  417. * which moves us back under the limit. */
  418. void ocfs2_remove_from_cache(struct inode *inode,
  419. struct buffer_head *bh)
  420. {
  421. int index;
  422. sector_t block = bh->b_blocknr;
  423. struct ocfs2_meta_cache_item *item = NULL;
  424. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  425. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  426. spin_lock(&oi->ip_lock);
  427. mlog(0, "Inode %llu, remove %llu, items = %u, array = %u\n",
  428. (unsigned long long)oi->ip_blkno,
  429. (unsigned long long) block, ci->ci_num_cached,
  430. oi->ip_flags & OCFS2_INODE_CACHE_INLINE);
  431. if (oi->ip_flags & OCFS2_INODE_CACHE_INLINE) {
  432. index = ocfs2_search_cache_array(ci, block);
  433. if (index != -1)
  434. ocfs2_remove_metadata_array(ci, index);
  435. } else {
  436. item = ocfs2_search_cache_tree(ci, block);
  437. if (item)
  438. ocfs2_remove_metadata_tree(ci, item);
  439. }
  440. spin_unlock(&oi->ip_lock);
  441. if (item)
  442. kmem_cache_free(ocfs2_uptodate_cachep, item);
  443. }
  444. int __init init_ocfs2_uptodate_cache(void)
  445. {
  446. ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
  447. sizeof(struct ocfs2_meta_cache_item),
  448. 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  449. if (!ocfs2_uptodate_cachep)
  450. return -ENOMEM;
  451. mlog(0, "%u inlined cache items per inode.\n",
  452. OCFS2_INODE_MAX_CACHE_ARRAY);
  453. return 0;
  454. }
  455. void exit_ocfs2_uptodate_cache(void)
  456. {
  457. if (ocfs2_uptodate_cachep)
  458. kmem_cache_destroy(ocfs2_uptodate_cachep);
  459. }