uptodate.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. *
  176. * This can be called under lock_buffer()
  177. */
  178. int ocfs2_buffer_uptodate(struct inode *inode,
  179. struct buffer_head *bh)
  180. {
  181. /* Doesn't matter if the bh is in our cache or not -- if it's
  182. * not marked uptodate then we know it can't have correct
  183. * data. */
  184. if (!buffer_uptodate(bh))
  185. return 0;
  186. /* OCFS2 does not allow multiple nodes to be changing the same
  187. * block at the same time. */
  188. if (buffer_jbd(bh))
  189. return 1;
  190. /* Ok, locally the buffer is marked as up to date, now search
  191. * our cache to see if we can trust that. */
  192. return ocfs2_buffer_cached(OCFS2_I(inode), bh);
  193. }
  194. /*
  195. * Determine whether a buffer is currently out on a read-ahead request.
  196. * ip_io_sem should be held to serialize submitters with the logic here.
  197. */
  198. int ocfs2_buffer_read_ahead(struct inode *inode,
  199. struct buffer_head *bh)
  200. {
  201. return buffer_locked(bh) && ocfs2_buffer_cached(OCFS2_I(inode), bh);
  202. }
  203. /* Requires ip_lock */
  204. static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci,
  205. sector_t block)
  206. {
  207. BUG_ON(ci->ci_num_cached >= OCFS2_INODE_MAX_CACHE_ARRAY);
  208. mlog(0, "block %llu takes position %u\n", (unsigned long long) block,
  209. ci->ci_num_cached);
  210. ci->ci_cache.ci_array[ci->ci_num_cached] = block;
  211. ci->ci_num_cached++;
  212. }
  213. /* By now the caller should have checked that the item does *not*
  214. * exist in the tree.
  215. * Requires ip_lock. */
  216. static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci,
  217. struct ocfs2_meta_cache_item *new)
  218. {
  219. sector_t block = new->c_block;
  220. struct rb_node *parent = NULL;
  221. struct rb_node **p = &ci->ci_cache.ci_tree.rb_node;
  222. struct ocfs2_meta_cache_item *tmp;
  223. mlog(0, "Insert block %llu num = %u\n", (unsigned long long) block,
  224. ci->ci_num_cached);
  225. while(*p) {
  226. parent = *p;
  227. tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node);
  228. if (block < tmp->c_block)
  229. p = &(*p)->rb_left;
  230. else if (block > tmp->c_block)
  231. p = &(*p)->rb_right;
  232. else {
  233. /* This should never happen! */
  234. mlog(ML_ERROR, "Duplicate block %llu cached!\n",
  235. (unsigned long long) block);
  236. BUG();
  237. }
  238. }
  239. rb_link_node(&new->c_node, parent, p);
  240. rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree);
  241. ci->ci_num_cached++;
  242. }
  243. static inline int ocfs2_insert_can_use_array(struct ocfs2_inode_info *oi,
  244. struct ocfs2_caching_info *ci)
  245. {
  246. assert_spin_locked(&oi->ip_lock);
  247. return (oi->ip_flags & OCFS2_INODE_CACHE_INLINE) &&
  248. (ci->ci_num_cached < OCFS2_INODE_MAX_CACHE_ARRAY);
  249. }
  250. /* tree should be exactly OCFS2_INODE_MAX_CACHE_ARRAY wide. NULL the
  251. * pointers in tree after we use them - this allows caller to detect
  252. * when to free in case of error. */
  253. static void ocfs2_expand_cache(struct ocfs2_inode_info *oi,
  254. struct ocfs2_meta_cache_item **tree)
  255. {
  256. int i;
  257. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  258. mlog_bug_on_msg(ci->ci_num_cached != OCFS2_INODE_MAX_CACHE_ARRAY,
  259. "Inode %llu, num cached = %u, should be %u\n",
  260. (unsigned long long)oi->ip_blkno, ci->ci_num_cached,
  261. OCFS2_INODE_MAX_CACHE_ARRAY);
  262. mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
  263. "Inode %llu not marked as inline anymore!\n",
  264. (unsigned long long)oi->ip_blkno);
  265. assert_spin_locked(&oi->ip_lock);
  266. /* Be careful to initialize the tree members *first* because
  267. * once the ci_tree is used, the array is junk... */
  268. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++)
  269. tree[i]->c_block = ci->ci_cache.ci_array[i];
  270. oi->ip_flags &= ~OCFS2_INODE_CACHE_INLINE;
  271. ci->ci_cache.ci_tree = RB_ROOT;
  272. /* this will be set again by __ocfs2_insert_cache_tree */
  273. ci->ci_num_cached = 0;
  274. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++) {
  275. __ocfs2_insert_cache_tree(ci, tree[i]);
  276. tree[i] = NULL;
  277. }
  278. mlog(0, "Expanded %llu to a tree cache: flags 0x%x, num = %u\n",
  279. (unsigned long long)oi->ip_blkno, oi->ip_flags, ci->ci_num_cached);
  280. }
  281. /* Slow path function - memory allocation is necessary. See the
  282. * comment above ocfs2_set_buffer_uptodate for more information. */
  283. static void __ocfs2_set_buffer_uptodate(struct ocfs2_inode_info *oi,
  284. sector_t block,
  285. int expand_tree)
  286. {
  287. int i;
  288. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  289. struct ocfs2_meta_cache_item *new = NULL;
  290. struct ocfs2_meta_cache_item *tree[OCFS2_INODE_MAX_CACHE_ARRAY] =
  291. { NULL, };
  292. mlog(0, "Inode %llu, block %llu, expand = %d\n",
  293. (unsigned long long)oi->ip_blkno,
  294. (unsigned long long)block, expand_tree);
  295. new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS);
  296. if (!new) {
  297. mlog_errno(-ENOMEM);
  298. return;
  299. }
  300. new->c_block = block;
  301. if (expand_tree) {
  302. /* Do *not* allocate an array here - the removal code
  303. * has no way of tracking that. */
  304. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++) {
  305. tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep,
  306. GFP_NOFS);
  307. if (!tree[i]) {
  308. mlog_errno(-ENOMEM);
  309. goto out_free;
  310. }
  311. /* These are initialized in ocfs2_expand_cache! */
  312. }
  313. }
  314. spin_lock(&oi->ip_lock);
  315. if (ocfs2_insert_can_use_array(oi, ci)) {
  316. mlog(0, "Someone cleared the tree underneath us\n");
  317. /* Ok, items were removed from the cache in between
  318. * locks. Detect this and revert back to the fast path */
  319. ocfs2_append_cache_array(ci, block);
  320. spin_unlock(&oi->ip_lock);
  321. goto out_free;
  322. }
  323. if (expand_tree)
  324. ocfs2_expand_cache(oi, tree);
  325. __ocfs2_insert_cache_tree(ci, new);
  326. spin_unlock(&oi->ip_lock);
  327. new = NULL;
  328. out_free:
  329. if (new)
  330. kmem_cache_free(ocfs2_uptodate_cachep, new);
  331. /* If these were used, then ocfs2_expand_cache re-set them to
  332. * NULL for us. */
  333. if (tree[0]) {
  334. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++)
  335. if (tree[i])
  336. kmem_cache_free(ocfs2_uptodate_cachep,
  337. tree[i]);
  338. }
  339. }
  340. /* Item insertion is guarded by ip_io_mutex, so the insertion path takes
  341. * advantage of this by not rechecking for a duplicate insert during
  342. * the slow case. Additionally, if the cache needs to be bumped up to
  343. * a tree, the code will not recheck after acquiring the lock --
  344. * multiple paths cannot be expanding to a tree at the same time.
  345. *
  346. * The slow path takes into account that items can be removed
  347. * (including the whole tree wiped and reset) when this process it out
  348. * allocating memory. In those cases, it reverts back to the fast
  349. * path.
  350. *
  351. * Note that this function may actually fail to insert the block if
  352. * memory cannot be allocated. This is not fatal however (but may
  353. * result in a performance penalty)
  354. *
  355. * Readahead buffers can be passed in here before the I/O request is
  356. * completed.
  357. */
  358. void ocfs2_set_buffer_uptodate(struct inode *inode,
  359. struct buffer_head *bh)
  360. {
  361. int expand;
  362. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  363. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  364. /* The block may very well exist in our cache already, so avoid
  365. * doing any more work in that case. */
  366. if (ocfs2_buffer_cached(oi, bh))
  367. return;
  368. mlog(0, "Inode %llu, inserting block %llu\n",
  369. (unsigned long long)oi->ip_blkno,
  370. (unsigned long long)bh->b_blocknr);
  371. /* No need to recheck under spinlock - insertion is guarded by
  372. * ip_io_mutex */
  373. spin_lock(&oi->ip_lock);
  374. if (ocfs2_insert_can_use_array(oi, ci)) {
  375. /* Fast case - it's an array and there's a free
  376. * spot. */
  377. ocfs2_append_cache_array(ci, bh->b_blocknr);
  378. spin_unlock(&oi->ip_lock);
  379. return;
  380. }
  381. expand = 0;
  382. if (oi->ip_flags & OCFS2_INODE_CACHE_INLINE) {
  383. /* We need to bump things up to a tree. */
  384. expand = 1;
  385. }
  386. spin_unlock(&oi->ip_lock);
  387. __ocfs2_set_buffer_uptodate(oi, bh->b_blocknr, expand);
  388. }
  389. /* Called against a newly allocated buffer. Most likely nobody should
  390. * be able to read this sort of metadata while it's still being
  391. * allocated, but this is careful to take ip_io_mutex anyway. */
  392. void ocfs2_set_new_buffer_uptodate(struct inode *inode,
  393. struct buffer_head *bh)
  394. {
  395. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  396. /* This should definitely *not* exist in our cache */
  397. BUG_ON(ocfs2_buffer_cached(oi, bh));
  398. set_buffer_uptodate(bh);
  399. mutex_lock(&oi->ip_io_mutex);
  400. ocfs2_set_buffer_uptodate(inode, bh);
  401. mutex_unlock(&oi->ip_io_mutex);
  402. }
  403. /* Requires ip_lock. */
  404. static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci,
  405. int index)
  406. {
  407. sector_t *array = ci->ci_cache.ci_array;
  408. int bytes;
  409. BUG_ON(index < 0 || index >= OCFS2_INODE_MAX_CACHE_ARRAY);
  410. BUG_ON(index >= ci->ci_num_cached);
  411. BUG_ON(!ci->ci_num_cached);
  412. mlog(0, "remove index %d (num_cached = %u\n", index,
  413. ci->ci_num_cached);
  414. ci->ci_num_cached--;
  415. /* don't need to copy if the array is now empty, or if we
  416. * removed at the tail */
  417. if (ci->ci_num_cached && index < ci->ci_num_cached) {
  418. bytes = sizeof(sector_t) * (ci->ci_num_cached - index);
  419. memmove(&array[index], &array[index + 1], bytes);
  420. }
  421. }
  422. /* Requires ip_lock. */
  423. static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci,
  424. struct ocfs2_meta_cache_item *item)
  425. {
  426. mlog(0, "remove block %llu from tree\n",
  427. (unsigned long long) item->c_block);
  428. rb_erase(&item->c_node, &ci->ci_cache.ci_tree);
  429. ci->ci_num_cached--;
  430. }
  431. /* Called when we remove a chunk of metadata from an inode. We don't
  432. * bother reverting things to an inlined array in the case of a remove
  433. * which moves us back under the limit. */
  434. void ocfs2_remove_from_cache(struct inode *inode,
  435. struct buffer_head *bh)
  436. {
  437. int index;
  438. sector_t block = bh->b_blocknr;
  439. struct ocfs2_meta_cache_item *item = NULL;
  440. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  441. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  442. spin_lock(&oi->ip_lock);
  443. mlog(0, "Inode %llu, remove %llu, items = %u, array = %u\n",
  444. (unsigned long long)oi->ip_blkno,
  445. (unsigned long long) block, ci->ci_num_cached,
  446. oi->ip_flags & OCFS2_INODE_CACHE_INLINE);
  447. if (oi->ip_flags & OCFS2_INODE_CACHE_INLINE) {
  448. index = ocfs2_search_cache_array(ci, block);
  449. if (index != -1)
  450. ocfs2_remove_metadata_array(ci, index);
  451. } else {
  452. item = ocfs2_search_cache_tree(ci, block);
  453. if (item)
  454. ocfs2_remove_metadata_tree(ci, item);
  455. }
  456. spin_unlock(&oi->ip_lock);
  457. if (item)
  458. kmem_cache_free(ocfs2_uptodate_cachep, item);
  459. }
  460. int __init init_ocfs2_uptodate_cache(void)
  461. {
  462. ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
  463. sizeof(struct ocfs2_meta_cache_item),
  464. 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
  465. if (!ocfs2_uptodate_cachep)
  466. return -ENOMEM;
  467. mlog(0, "%u inlined cache items per inode.\n",
  468. OCFS2_INODE_MAX_CACHE_ARRAY);
  469. return 0;
  470. }
  471. void exit_ocfs2_uptodate_cache(void)
  472. {
  473. if (ocfs2_uptodate_cachep)
  474. kmem_cache_destroy(ocfs2_uptodate_cachep);
  475. }