uptodate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. #ifndef CONFIG_OCFS2_COMPAT_JBD
  56. # include <linux/jbd2.h>
  57. #else
  58. # include <linux/jbd.h>
  59. #endif
  60. #define MLOG_MASK_PREFIX ML_UPTODATE
  61. #include <cluster/masklog.h>
  62. #include "ocfs2.h"
  63. #include "inode.h"
  64. #include "uptodate.h"
  65. struct ocfs2_meta_cache_item {
  66. struct rb_node c_node;
  67. sector_t c_block;
  68. };
  69. static struct kmem_cache *ocfs2_uptodate_cachep = NULL;
  70. void ocfs2_metadata_cache_init(struct inode *inode)
  71. {
  72. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  73. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  74. oi->ip_flags |= OCFS2_INODE_CACHE_INLINE;
  75. ci->ci_num_cached = 0;
  76. }
  77. /* No lock taken here as 'root' is not expected to be visible to other
  78. * processes. */
  79. static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root)
  80. {
  81. unsigned int purged = 0;
  82. struct rb_node *node;
  83. struct ocfs2_meta_cache_item *item;
  84. while ((node = rb_last(root)) != NULL) {
  85. item = rb_entry(node, struct ocfs2_meta_cache_item, c_node);
  86. mlog(0, "Purge item %llu\n",
  87. (unsigned long long) item->c_block);
  88. rb_erase(&item->c_node, root);
  89. kmem_cache_free(ocfs2_uptodate_cachep, item);
  90. purged++;
  91. }
  92. return purged;
  93. }
  94. /* Called from locking and called from ocfs2_clear_inode. Dump the
  95. * cache for a given inode.
  96. *
  97. * This function is a few more lines longer than necessary due to some
  98. * accounting done here, but I think it's worth tracking down those
  99. * bugs sooner -- Mark */
  100. void ocfs2_metadata_cache_purge(struct inode *inode)
  101. {
  102. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  103. unsigned int tree, to_purge, purged;
  104. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  105. struct rb_root root = RB_ROOT;
  106. spin_lock(&oi->ip_lock);
  107. tree = !(oi->ip_flags & OCFS2_INODE_CACHE_INLINE);
  108. to_purge = ci->ci_num_cached;
  109. mlog(0, "Purge %u %s items from Inode %llu\n", to_purge,
  110. tree ? "array" : "tree", (unsigned long long)oi->ip_blkno);
  111. /* If we're a tree, save off the root so that we can safely
  112. * initialize the cache. We do the work to free tree members
  113. * without the spinlock. */
  114. if (tree)
  115. root = ci->ci_cache.ci_tree;
  116. ocfs2_metadata_cache_init(inode);
  117. spin_unlock(&oi->ip_lock);
  118. purged = ocfs2_purge_copied_metadata_tree(&root);
  119. /* If possible, track the number wiped so that we can more
  120. * easily detect counting errors. Unfortunately, this is only
  121. * meaningful for trees. */
  122. if (tree && purged != to_purge)
  123. mlog(ML_ERROR, "Inode %llu, count = %u, purged = %u\n",
  124. (unsigned long long)oi->ip_blkno, to_purge, purged);
  125. }
  126. /* Returns the index in the cache array, -1 if not found.
  127. * Requires ip_lock. */
  128. static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci,
  129. sector_t item)
  130. {
  131. int i;
  132. for (i = 0; i < ci->ci_num_cached; i++) {
  133. if (item == ci->ci_cache.ci_array[i])
  134. return i;
  135. }
  136. return -1;
  137. }
  138. /* Returns the cache item if found, otherwise NULL.
  139. * Requires ip_lock. */
  140. static struct ocfs2_meta_cache_item *
  141. ocfs2_search_cache_tree(struct ocfs2_caching_info *ci,
  142. sector_t block)
  143. {
  144. struct rb_node * n = ci->ci_cache.ci_tree.rb_node;
  145. struct ocfs2_meta_cache_item *item = NULL;
  146. while (n) {
  147. item = rb_entry(n, struct ocfs2_meta_cache_item, c_node);
  148. if (block < item->c_block)
  149. n = n->rb_left;
  150. else if (block > item->c_block)
  151. n = n->rb_right;
  152. else
  153. return item;
  154. }
  155. return NULL;
  156. }
  157. static int ocfs2_buffer_cached(struct ocfs2_inode_info *oi,
  158. struct buffer_head *bh)
  159. {
  160. int index = -1;
  161. struct ocfs2_meta_cache_item *item = NULL;
  162. spin_lock(&oi->ip_lock);
  163. mlog(0, "Inode %llu, query block %llu (inline = %u)\n",
  164. (unsigned long long)oi->ip_blkno,
  165. (unsigned long long) bh->b_blocknr,
  166. !!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE));
  167. if (oi->ip_flags & OCFS2_INODE_CACHE_INLINE)
  168. index = ocfs2_search_cache_array(&oi->ip_metadata_cache,
  169. bh->b_blocknr);
  170. else
  171. item = ocfs2_search_cache_tree(&oi->ip_metadata_cache,
  172. bh->b_blocknr);
  173. spin_unlock(&oi->ip_lock);
  174. mlog(0, "index = %d, item = %p\n", index, item);
  175. return (index != -1) || (item != NULL);
  176. }
  177. /* Warning: even if it returns true, this does *not* guarantee that
  178. * the block is stored in our inode metadata cache.
  179. *
  180. * This can be called under lock_buffer()
  181. */
  182. int ocfs2_buffer_uptodate(struct inode *inode,
  183. struct buffer_head *bh)
  184. {
  185. /* Doesn't matter if the bh is in our cache or not -- if it's
  186. * not marked uptodate then we know it can't have correct
  187. * data. */
  188. if (!buffer_uptodate(bh))
  189. return 0;
  190. /* OCFS2 does not allow multiple nodes to be changing the same
  191. * block at the same time. */
  192. if (buffer_jbd(bh))
  193. return 1;
  194. /* Ok, locally the buffer is marked as up to date, now search
  195. * our cache to see if we can trust that. */
  196. return ocfs2_buffer_cached(OCFS2_I(inode), bh);
  197. }
  198. /*
  199. * Determine whether a buffer is currently out on a read-ahead request.
  200. * ip_io_sem should be held to serialize submitters with the logic here.
  201. */
  202. int ocfs2_buffer_read_ahead(struct inode *inode,
  203. struct buffer_head *bh)
  204. {
  205. return buffer_locked(bh) && ocfs2_buffer_cached(OCFS2_I(inode), bh);
  206. }
  207. /* Requires ip_lock */
  208. static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci,
  209. sector_t block)
  210. {
  211. BUG_ON(ci->ci_num_cached >= OCFS2_INODE_MAX_CACHE_ARRAY);
  212. mlog(0, "block %llu takes position %u\n", (unsigned long long) block,
  213. ci->ci_num_cached);
  214. ci->ci_cache.ci_array[ci->ci_num_cached] = block;
  215. ci->ci_num_cached++;
  216. }
  217. /* By now the caller should have checked that the item does *not*
  218. * exist in the tree.
  219. * Requires ip_lock. */
  220. static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci,
  221. struct ocfs2_meta_cache_item *new)
  222. {
  223. sector_t block = new->c_block;
  224. struct rb_node *parent = NULL;
  225. struct rb_node **p = &ci->ci_cache.ci_tree.rb_node;
  226. struct ocfs2_meta_cache_item *tmp;
  227. mlog(0, "Insert block %llu num = %u\n", (unsigned long long) block,
  228. ci->ci_num_cached);
  229. while(*p) {
  230. parent = *p;
  231. tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node);
  232. if (block < tmp->c_block)
  233. p = &(*p)->rb_left;
  234. else if (block > tmp->c_block)
  235. p = &(*p)->rb_right;
  236. else {
  237. /* This should never happen! */
  238. mlog(ML_ERROR, "Duplicate block %llu cached!\n",
  239. (unsigned long long) block);
  240. BUG();
  241. }
  242. }
  243. rb_link_node(&new->c_node, parent, p);
  244. rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree);
  245. ci->ci_num_cached++;
  246. }
  247. static inline int ocfs2_insert_can_use_array(struct ocfs2_inode_info *oi,
  248. struct ocfs2_caching_info *ci)
  249. {
  250. assert_spin_locked(&oi->ip_lock);
  251. return (oi->ip_flags & OCFS2_INODE_CACHE_INLINE) &&
  252. (ci->ci_num_cached < OCFS2_INODE_MAX_CACHE_ARRAY);
  253. }
  254. /* tree should be exactly OCFS2_INODE_MAX_CACHE_ARRAY wide. NULL the
  255. * pointers in tree after we use them - this allows caller to detect
  256. * when to free in case of error. */
  257. static void ocfs2_expand_cache(struct ocfs2_inode_info *oi,
  258. struct ocfs2_meta_cache_item **tree)
  259. {
  260. int i;
  261. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  262. mlog_bug_on_msg(ci->ci_num_cached != OCFS2_INODE_MAX_CACHE_ARRAY,
  263. "Inode %llu, num cached = %u, should be %u\n",
  264. (unsigned long long)oi->ip_blkno, ci->ci_num_cached,
  265. OCFS2_INODE_MAX_CACHE_ARRAY);
  266. mlog_bug_on_msg(!(oi->ip_flags & OCFS2_INODE_CACHE_INLINE),
  267. "Inode %llu not marked as inline anymore!\n",
  268. (unsigned long long)oi->ip_blkno);
  269. assert_spin_locked(&oi->ip_lock);
  270. /* Be careful to initialize the tree members *first* because
  271. * once the ci_tree is used, the array is junk... */
  272. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++)
  273. tree[i]->c_block = ci->ci_cache.ci_array[i];
  274. oi->ip_flags &= ~OCFS2_INODE_CACHE_INLINE;
  275. ci->ci_cache.ci_tree = RB_ROOT;
  276. /* this will be set again by __ocfs2_insert_cache_tree */
  277. ci->ci_num_cached = 0;
  278. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++) {
  279. __ocfs2_insert_cache_tree(ci, tree[i]);
  280. tree[i] = NULL;
  281. }
  282. mlog(0, "Expanded %llu to a tree cache: flags 0x%x, num = %u\n",
  283. (unsigned long long)oi->ip_blkno, oi->ip_flags, ci->ci_num_cached);
  284. }
  285. /* Slow path function - memory allocation is necessary. See the
  286. * comment above ocfs2_set_buffer_uptodate for more information. */
  287. static void __ocfs2_set_buffer_uptodate(struct ocfs2_inode_info *oi,
  288. sector_t block,
  289. int expand_tree)
  290. {
  291. int i;
  292. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  293. struct ocfs2_meta_cache_item *new = NULL;
  294. struct ocfs2_meta_cache_item *tree[OCFS2_INODE_MAX_CACHE_ARRAY] =
  295. { NULL, };
  296. mlog(0, "Inode %llu, block %llu, expand = %d\n",
  297. (unsigned long long)oi->ip_blkno,
  298. (unsigned long long)block, expand_tree);
  299. new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS);
  300. if (!new) {
  301. mlog_errno(-ENOMEM);
  302. return;
  303. }
  304. new->c_block = block;
  305. if (expand_tree) {
  306. /* Do *not* allocate an array here - the removal code
  307. * has no way of tracking that. */
  308. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++) {
  309. tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep,
  310. GFP_NOFS);
  311. if (!tree[i]) {
  312. mlog_errno(-ENOMEM);
  313. goto out_free;
  314. }
  315. /* These are initialized in ocfs2_expand_cache! */
  316. }
  317. }
  318. spin_lock(&oi->ip_lock);
  319. if (ocfs2_insert_can_use_array(oi, ci)) {
  320. mlog(0, "Someone cleared the tree underneath us\n");
  321. /* Ok, items were removed from the cache in between
  322. * locks. Detect this and revert back to the fast path */
  323. ocfs2_append_cache_array(ci, block);
  324. spin_unlock(&oi->ip_lock);
  325. goto out_free;
  326. }
  327. if (expand_tree)
  328. ocfs2_expand_cache(oi, tree);
  329. __ocfs2_insert_cache_tree(ci, new);
  330. spin_unlock(&oi->ip_lock);
  331. new = NULL;
  332. out_free:
  333. if (new)
  334. kmem_cache_free(ocfs2_uptodate_cachep, new);
  335. /* If these were used, then ocfs2_expand_cache re-set them to
  336. * NULL for us. */
  337. if (tree[0]) {
  338. for(i = 0; i < OCFS2_INODE_MAX_CACHE_ARRAY; i++)
  339. if (tree[i])
  340. kmem_cache_free(ocfs2_uptodate_cachep,
  341. tree[i]);
  342. }
  343. }
  344. /* Item insertion is guarded by ip_io_mutex, so the insertion path takes
  345. * advantage of this by not rechecking for a duplicate insert during
  346. * the slow case. Additionally, if the cache needs to be bumped up to
  347. * a tree, the code will not recheck after acquiring the lock --
  348. * multiple paths cannot be expanding to a tree at the same time.
  349. *
  350. * The slow path takes into account that items can be removed
  351. * (including the whole tree wiped and reset) when this process it out
  352. * allocating memory. In those cases, it reverts back to the fast
  353. * path.
  354. *
  355. * Note that this function may actually fail to insert the block if
  356. * memory cannot be allocated. This is not fatal however (but may
  357. * result in a performance penalty)
  358. *
  359. * Readahead buffers can be passed in here before the I/O request is
  360. * completed.
  361. */
  362. void ocfs2_set_buffer_uptodate(struct inode *inode,
  363. struct buffer_head *bh)
  364. {
  365. int expand;
  366. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  367. struct ocfs2_caching_info *ci = &oi->ip_metadata_cache;
  368. /* The block may very well exist in our cache already, so avoid
  369. * doing any more work in that case. */
  370. if (ocfs2_buffer_cached(oi, bh))
  371. return;
  372. mlog(0, "Inode %llu, inserting block %llu\n",
  373. (unsigned long long)oi->ip_blkno,
  374. (unsigned long long)bh->b_blocknr);
  375. /* No need to recheck under spinlock - insertion is guarded by
  376. * ip_io_mutex */
  377. spin_lock(&oi->ip_lock);
  378. if (ocfs2_insert_can_use_array(oi, ci)) {
  379. /* Fast case - it's an array and there's a free
  380. * spot. */
  381. ocfs2_append_cache_array(ci, bh->b_blocknr);
  382. spin_unlock(&oi->ip_lock);
  383. return;
  384. }
  385. expand = 0;
  386. if (oi->ip_flags & OCFS2_INODE_CACHE_INLINE) {
  387. /* We need to bump things up to a tree. */
  388. expand = 1;
  389. }
  390. spin_unlock(&oi->ip_lock);
  391. __ocfs2_set_buffer_uptodate(oi, bh->b_blocknr, expand);
  392. }
  393. /* Called against a newly allocated buffer. Most likely nobody should
  394. * be able to read this sort of metadata while it's still being
  395. * allocated, but this is careful to take ip_io_mutex anyway. */
  396. void ocfs2_set_new_buffer_uptodate(struct inode *inode,
  397. struct buffer_head *bh)
  398. {
  399. struct ocfs2_inode_info *oi = OCFS2_I(inode);
  400. /* This should definitely *not* exist in our cache */
  401. BUG_ON(ocfs2_buffer_cached(oi, bh));
  402. set_buffer_uptodate(bh);
  403. mutex_lock(&oi->ip_io_mutex);
  404. ocfs2_set_buffer_uptodate(inode, bh);
  405. mutex_unlock(&oi->ip_io_mutex);
  406. }
  407. /* Requires ip_lock. */
  408. static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci,
  409. int index)
  410. {
  411. sector_t *array = ci->ci_cache.ci_array;
  412. int bytes;
  413. BUG_ON(index < 0 || index >= OCFS2_INODE_MAX_CACHE_ARRAY);
  414. BUG_ON(index >= ci->ci_num_cached);
  415. BUG_ON(!ci->ci_num_cached);
  416. mlog(0, "remove index %d (num_cached = %u\n", index,
  417. ci->ci_num_cached);
  418. ci->ci_num_cached--;
  419. /* don't need to copy if the array is now empty, or if we
  420. * removed at the tail */
  421. if (ci->ci_num_cached && index < ci->ci_num_cached) {
  422. bytes = sizeof(sector_t) * (ci->ci_num_cached - index);
  423. memmove(&array[index], &array[index + 1], bytes);
  424. }
  425. }
  426. /* Requires ip_lock. */
  427. static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci,
  428. struct ocfs2_meta_cache_item *item)
  429. {
  430. mlog(0, "remove block %llu from tree\n",
  431. (unsigned long long) item->c_block);
  432. rb_erase(&item->c_node, &ci->ci_cache.ci_tree);
  433. ci->ci_num_cached--;
  434. }
  435. static void ocfs2_remove_block_from_cache(struct inode *inode,
  436. sector_t block)
  437. {
  438. int index;
  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. /*
  461. * Called when we remove a chunk of metadata from an inode. We don't
  462. * bother reverting things to an inlined array in the case of a remove
  463. * which moves us back under the limit.
  464. */
  465. void ocfs2_remove_from_cache(struct inode *inode,
  466. struct buffer_head *bh)
  467. {
  468. sector_t block = bh->b_blocknr;
  469. ocfs2_remove_block_from_cache(inode, block);
  470. }
  471. /* Called when we remove xattr clusters from an inode. */
  472. void ocfs2_remove_xattr_clusters_from_cache(struct inode *inode,
  473. sector_t block,
  474. u32 c_len)
  475. {
  476. unsigned int i, b_len = ocfs2_clusters_to_blocks(inode->i_sb, 1) * c_len;
  477. for (i = 0; i < b_len; i++, block++)
  478. ocfs2_remove_block_from_cache(inode, block);
  479. }
  480. int __init init_ocfs2_uptodate_cache(void)
  481. {
  482. ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
  483. sizeof(struct ocfs2_meta_cache_item),
  484. 0, SLAB_HWCACHE_ALIGN, NULL);
  485. if (!ocfs2_uptodate_cachep)
  486. return -ENOMEM;
  487. mlog(0, "%u inlined cache items per inode.\n",
  488. OCFS2_INODE_MAX_CACHE_ARRAY);
  489. return 0;
  490. }
  491. void exit_ocfs2_uptodate_cache(void)
  492. {
  493. if (ocfs2_uptodate_cachep)
  494. kmem_cache_destroy(ocfs2_uptodate_cachep);
  495. }