1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /* -*- mode: c; c-basic-offset: 8; -*-
3*4882a593Smuzhiyun * vim: noexpandtab sw=8 ts=8 sts=0:
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * uptodate.c
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Tracking the up-to-date-ness of a local buffer_head with respect to
8*4882a593Smuzhiyun * the cluster.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Copyright (C) 2002, 2004, 2005 Oracle. All rights reserved.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Standard buffer head caching flags (uptodate, etc) are insufficient
13*4882a593Smuzhiyun * in a clustered environment - a buffer may be marked up to date on
14*4882a593Smuzhiyun * our local node but could have been modified by another cluster
15*4882a593Smuzhiyun * member. As a result an additional (and performant) caching scheme
16*4882a593Smuzhiyun * is required. A further requirement is that we consume as little
17*4882a593Smuzhiyun * memory as possible - we never pin buffer_head structures in order
18*4882a593Smuzhiyun * to cache them.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * We track the existence of up to date buffers on the inodes which
21*4882a593Smuzhiyun * are associated with them. Because we don't want to pin
22*4882a593Smuzhiyun * buffer_heads, this is only a (strong) hint and several other checks
23*4882a593Smuzhiyun * are made in the I/O path to ensure that we don't use a stale or
24*4882a593Smuzhiyun * invalid buffer without going to disk:
25*4882a593Smuzhiyun * - buffer_jbd is used liberally - if a bh is in the journal on
26*4882a593Smuzhiyun * this node then it *must* be up to date.
27*4882a593Smuzhiyun * - the standard buffer_uptodate() macro is used to detect buffers
28*4882a593Smuzhiyun * which may be invalid (even if we have an up to date tracking
29*4882a593Smuzhiyun * item for them)
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * For a full understanding of how this code works together, one
32*4882a593Smuzhiyun * should read the callers in dlmglue.c, the I/O functions in
33*4882a593Smuzhiyun * buffer_head_io.c and ocfs2_journal_access in journal.c
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #include <linux/fs.h>
37*4882a593Smuzhiyun #include <linux/types.h>
38*4882a593Smuzhiyun #include <linux/slab.h>
39*4882a593Smuzhiyun #include <linux/highmem.h>
40*4882a593Smuzhiyun #include <linux/buffer_head.h>
41*4882a593Smuzhiyun #include <linux/rbtree.h>
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun #include <cluster/masklog.h>
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #include "ocfs2.h"
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun #include "inode.h"
48*4882a593Smuzhiyun #include "uptodate.h"
49*4882a593Smuzhiyun #include "ocfs2_trace.h"
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun struct ocfs2_meta_cache_item {
52*4882a593Smuzhiyun struct rb_node c_node;
53*4882a593Smuzhiyun sector_t c_block;
54*4882a593Smuzhiyun };
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun static struct kmem_cache *ocfs2_uptodate_cachep;
57*4882a593Smuzhiyun
ocfs2_metadata_cache_owner(struct ocfs2_caching_info * ci)58*4882a593Smuzhiyun u64 ocfs2_metadata_cache_owner(struct ocfs2_caching_info *ci)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun BUG_ON(!ci || !ci->ci_ops);
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun return ci->ci_ops->co_owner(ci);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
ocfs2_metadata_cache_get_super(struct ocfs2_caching_info * ci)65*4882a593Smuzhiyun struct super_block *ocfs2_metadata_cache_get_super(struct ocfs2_caching_info *ci)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun BUG_ON(!ci || !ci->ci_ops);
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun return ci->ci_ops->co_get_super(ci);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
ocfs2_metadata_cache_lock(struct ocfs2_caching_info * ci)72*4882a593Smuzhiyun static void ocfs2_metadata_cache_lock(struct ocfs2_caching_info *ci)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun BUG_ON(!ci || !ci->ci_ops);
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun ci->ci_ops->co_cache_lock(ci);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
ocfs2_metadata_cache_unlock(struct ocfs2_caching_info * ci)79*4882a593Smuzhiyun static void ocfs2_metadata_cache_unlock(struct ocfs2_caching_info *ci)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun BUG_ON(!ci || !ci->ci_ops);
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun ci->ci_ops->co_cache_unlock(ci);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
ocfs2_metadata_cache_io_lock(struct ocfs2_caching_info * ci)86*4882a593Smuzhiyun void ocfs2_metadata_cache_io_lock(struct ocfs2_caching_info *ci)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun BUG_ON(!ci || !ci->ci_ops);
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun ci->ci_ops->co_io_lock(ci);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
ocfs2_metadata_cache_io_unlock(struct ocfs2_caching_info * ci)93*4882a593Smuzhiyun void ocfs2_metadata_cache_io_unlock(struct ocfs2_caching_info *ci)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun BUG_ON(!ci || !ci->ci_ops);
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun ci->ci_ops->co_io_unlock(ci);
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun
ocfs2_metadata_cache_reset(struct ocfs2_caching_info * ci,int clear)101*4882a593Smuzhiyun static void ocfs2_metadata_cache_reset(struct ocfs2_caching_info *ci,
102*4882a593Smuzhiyun int clear)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun ci->ci_flags |= OCFS2_CACHE_FL_INLINE;
105*4882a593Smuzhiyun ci->ci_num_cached = 0;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun if (clear) {
108*4882a593Smuzhiyun ci->ci_created_trans = 0;
109*4882a593Smuzhiyun ci->ci_last_trans = 0;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
ocfs2_metadata_cache_init(struct ocfs2_caching_info * ci,const struct ocfs2_caching_operations * ops)113*4882a593Smuzhiyun void ocfs2_metadata_cache_init(struct ocfs2_caching_info *ci,
114*4882a593Smuzhiyun const struct ocfs2_caching_operations *ops)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun BUG_ON(!ops);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun ci->ci_ops = ops;
119*4882a593Smuzhiyun ocfs2_metadata_cache_reset(ci, 1);
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
ocfs2_metadata_cache_exit(struct ocfs2_caching_info * ci)122*4882a593Smuzhiyun void ocfs2_metadata_cache_exit(struct ocfs2_caching_info *ci)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun ocfs2_metadata_cache_purge(ci);
125*4882a593Smuzhiyun ocfs2_metadata_cache_reset(ci, 1);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /* No lock taken here as 'root' is not expected to be visible to other
130*4882a593Smuzhiyun * processes. */
ocfs2_purge_copied_metadata_tree(struct rb_root * root)131*4882a593Smuzhiyun static unsigned int ocfs2_purge_copied_metadata_tree(struct rb_root *root)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun unsigned int purged = 0;
134*4882a593Smuzhiyun struct rb_node *node;
135*4882a593Smuzhiyun struct ocfs2_meta_cache_item *item;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun while ((node = rb_last(root)) != NULL) {
138*4882a593Smuzhiyun item = rb_entry(node, struct ocfs2_meta_cache_item, c_node);
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun trace_ocfs2_purge_copied_metadata_tree(
141*4882a593Smuzhiyun (unsigned long long) item->c_block);
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun rb_erase(&item->c_node, root);
144*4882a593Smuzhiyun kmem_cache_free(ocfs2_uptodate_cachep, item);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun purged++;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun return purged;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /* Called from locking and called from ocfs2_clear_inode. Dump the
152*4882a593Smuzhiyun * cache for a given inode.
153*4882a593Smuzhiyun *
154*4882a593Smuzhiyun * This function is a few more lines longer than necessary due to some
155*4882a593Smuzhiyun * accounting done here, but I think it's worth tracking down those
156*4882a593Smuzhiyun * bugs sooner -- Mark */
ocfs2_metadata_cache_purge(struct ocfs2_caching_info * ci)157*4882a593Smuzhiyun void ocfs2_metadata_cache_purge(struct ocfs2_caching_info *ci)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun unsigned int tree, to_purge, purged;
160*4882a593Smuzhiyun struct rb_root root = RB_ROOT;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun BUG_ON(!ci || !ci->ci_ops);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun ocfs2_metadata_cache_lock(ci);
165*4882a593Smuzhiyun tree = !(ci->ci_flags & OCFS2_CACHE_FL_INLINE);
166*4882a593Smuzhiyun to_purge = ci->ci_num_cached;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun trace_ocfs2_metadata_cache_purge(
169*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
170*4882a593Smuzhiyun to_purge, tree);
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /* If we're a tree, save off the root so that we can safely
173*4882a593Smuzhiyun * initialize the cache. We do the work to free tree members
174*4882a593Smuzhiyun * without the spinlock. */
175*4882a593Smuzhiyun if (tree)
176*4882a593Smuzhiyun root = ci->ci_cache.ci_tree;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun ocfs2_metadata_cache_reset(ci, 0);
179*4882a593Smuzhiyun ocfs2_metadata_cache_unlock(ci);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun purged = ocfs2_purge_copied_metadata_tree(&root);
182*4882a593Smuzhiyun /* If possible, track the number wiped so that we can more
183*4882a593Smuzhiyun * easily detect counting errors. Unfortunately, this is only
184*4882a593Smuzhiyun * meaningful for trees. */
185*4882a593Smuzhiyun if (tree && purged != to_purge)
186*4882a593Smuzhiyun mlog(ML_ERROR, "Owner %llu, count = %u, purged = %u\n",
187*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
188*4882a593Smuzhiyun to_purge, purged);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /* Returns the index in the cache array, -1 if not found.
192*4882a593Smuzhiyun * Requires ip_lock. */
ocfs2_search_cache_array(struct ocfs2_caching_info * ci,sector_t item)193*4882a593Smuzhiyun static int ocfs2_search_cache_array(struct ocfs2_caching_info *ci,
194*4882a593Smuzhiyun sector_t item)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun int i;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun for (i = 0; i < ci->ci_num_cached; i++) {
199*4882a593Smuzhiyun if (item == ci->ci_cache.ci_array[i])
200*4882a593Smuzhiyun return i;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun return -1;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* Returns the cache item if found, otherwise NULL.
207*4882a593Smuzhiyun * Requires ip_lock. */
208*4882a593Smuzhiyun static struct ocfs2_meta_cache_item *
ocfs2_search_cache_tree(struct ocfs2_caching_info * ci,sector_t block)209*4882a593Smuzhiyun ocfs2_search_cache_tree(struct ocfs2_caching_info *ci,
210*4882a593Smuzhiyun sector_t block)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun struct rb_node * n = ci->ci_cache.ci_tree.rb_node;
213*4882a593Smuzhiyun struct ocfs2_meta_cache_item *item = NULL;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun while (n) {
216*4882a593Smuzhiyun item = rb_entry(n, struct ocfs2_meta_cache_item, c_node);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun if (block < item->c_block)
219*4882a593Smuzhiyun n = n->rb_left;
220*4882a593Smuzhiyun else if (block > item->c_block)
221*4882a593Smuzhiyun n = n->rb_right;
222*4882a593Smuzhiyun else
223*4882a593Smuzhiyun return item;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun return NULL;
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun
ocfs2_buffer_cached(struct ocfs2_caching_info * ci,struct buffer_head * bh)229*4882a593Smuzhiyun static int ocfs2_buffer_cached(struct ocfs2_caching_info *ci,
230*4882a593Smuzhiyun struct buffer_head *bh)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun int index = -1;
233*4882a593Smuzhiyun struct ocfs2_meta_cache_item *item = NULL;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun ocfs2_metadata_cache_lock(ci);
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun trace_ocfs2_buffer_cached_begin(
238*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
239*4882a593Smuzhiyun (unsigned long long) bh->b_blocknr,
240*4882a593Smuzhiyun !!(ci->ci_flags & OCFS2_CACHE_FL_INLINE));
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun if (ci->ci_flags & OCFS2_CACHE_FL_INLINE)
243*4882a593Smuzhiyun index = ocfs2_search_cache_array(ci, bh->b_blocknr);
244*4882a593Smuzhiyun else
245*4882a593Smuzhiyun item = ocfs2_search_cache_tree(ci, bh->b_blocknr);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun ocfs2_metadata_cache_unlock(ci);
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun trace_ocfs2_buffer_cached_end(index, item);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun return (index != -1) || (item != NULL);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /* Warning: even if it returns true, this does *not* guarantee that
255*4882a593Smuzhiyun * the block is stored in our inode metadata cache.
256*4882a593Smuzhiyun *
257*4882a593Smuzhiyun * This can be called under lock_buffer()
258*4882a593Smuzhiyun */
ocfs2_buffer_uptodate(struct ocfs2_caching_info * ci,struct buffer_head * bh)259*4882a593Smuzhiyun int ocfs2_buffer_uptodate(struct ocfs2_caching_info *ci,
260*4882a593Smuzhiyun struct buffer_head *bh)
261*4882a593Smuzhiyun {
262*4882a593Smuzhiyun /* Doesn't matter if the bh is in our cache or not -- if it's
263*4882a593Smuzhiyun * not marked uptodate then we know it can't have correct
264*4882a593Smuzhiyun * data. */
265*4882a593Smuzhiyun if (!buffer_uptodate(bh))
266*4882a593Smuzhiyun return 0;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun /* OCFS2 does not allow multiple nodes to be changing the same
269*4882a593Smuzhiyun * block at the same time. */
270*4882a593Smuzhiyun if (buffer_jbd(bh))
271*4882a593Smuzhiyun return 1;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /* Ok, locally the buffer is marked as up to date, now search
274*4882a593Smuzhiyun * our cache to see if we can trust that. */
275*4882a593Smuzhiyun return ocfs2_buffer_cached(ci, bh);
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun /*
279*4882a593Smuzhiyun * Determine whether a buffer is currently out on a read-ahead request.
280*4882a593Smuzhiyun * ci_io_sem should be held to serialize submitters with the logic here.
281*4882a593Smuzhiyun */
ocfs2_buffer_read_ahead(struct ocfs2_caching_info * ci,struct buffer_head * bh)282*4882a593Smuzhiyun int ocfs2_buffer_read_ahead(struct ocfs2_caching_info *ci,
283*4882a593Smuzhiyun struct buffer_head *bh)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun return buffer_locked(bh) && ocfs2_buffer_cached(ci, bh);
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /* Requires ip_lock */
ocfs2_append_cache_array(struct ocfs2_caching_info * ci,sector_t block)289*4882a593Smuzhiyun static void ocfs2_append_cache_array(struct ocfs2_caching_info *ci,
290*4882a593Smuzhiyun sector_t block)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun BUG_ON(ci->ci_num_cached >= OCFS2_CACHE_INFO_MAX_ARRAY);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun trace_ocfs2_append_cache_array(
295*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
296*4882a593Smuzhiyun (unsigned long long)block, ci->ci_num_cached);
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun ci->ci_cache.ci_array[ci->ci_num_cached] = block;
299*4882a593Smuzhiyun ci->ci_num_cached++;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun /* By now the caller should have checked that the item does *not*
303*4882a593Smuzhiyun * exist in the tree.
304*4882a593Smuzhiyun * Requires ip_lock. */
__ocfs2_insert_cache_tree(struct ocfs2_caching_info * ci,struct ocfs2_meta_cache_item * new)305*4882a593Smuzhiyun static void __ocfs2_insert_cache_tree(struct ocfs2_caching_info *ci,
306*4882a593Smuzhiyun struct ocfs2_meta_cache_item *new)
307*4882a593Smuzhiyun {
308*4882a593Smuzhiyun sector_t block = new->c_block;
309*4882a593Smuzhiyun struct rb_node *parent = NULL;
310*4882a593Smuzhiyun struct rb_node **p = &ci->ci_cache.ci_tree.rb_node;
311*4882a593Smuzhiyun struct ocfs2_meta_cache_item *tmp;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun trace_ocfs2_insert_cache_tree(
314*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
315*4882a593Smuzhiyun (unsigned long long)block, ci->ci_num_cached);
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun while(*p) {
318*4882a593Smuzhiyun parent = *p;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun tmp = rb_entry(parent, struct ocfs2_meta_cache_item, c_node);
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun if (block < tmp->c_block)
323*4882a593Smuzhiyun p = &(*p)->rb_left;
324*4882a593Smuzhiyun else if (block > tmp->c_block)
325*4882a593Smuzhiyun p = &(*p)->rb_right;
326*4882a593Smuzhiyun else {
327*4882a593Smuzhiyun /* This should never happen! */
328*4882a593Smuzhiyun mlog(ML_ERROR, "Duplicate block %llu cached!\n",
329*4882a593Smuzhiyun (unsigned long long) block);
330*4882a593Smuzhiyun BUG();
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun rb_link_node(&new->c_node, parent, p);
335*4882a593Smuzhiyun rb_insert_color(&new->c_node, &ci->ci_cache.ci_tree);
336*4882a593Smuzhiyun ci->ci_num_cached++;
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun /* co_cache_lock() must be held */
ocfs2_insert_can_use_array(struct ocfs2_caching_info * ci)340*4882a593Smuzhiyun static inline int ocfs2_insert_can_use_array(struct ocfs2_caching_info *ci)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun return (ci->ci_flags & OCFS2_CACHE_FL_INLINE) &&
343*4882a593Smuzhiyun (ci->ci_num_cached < OCFS2_CACHE_INFO_MAX_ARRAY);
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun /* tree should be exactly OCFS2_CACHE_INFO_MAX_ARRAY wide. NULL the
347*4882a593Smuzhiyun * pointers in tree after we use them - this allows caller to detect
348*4882a593Smuzhiyun * when to free in case of error.
349*4882a593Smuzhiyun *
350*4882a593Smuzhiyun * The co_cache_lock() must be held. */
ocfs2_expand_cache(struct ocfs2_caching_info * ci,struct ocfs2_meta_cache_item ** tree)351*4882a593Smuzhiyun static void ocfs2_expand_cache(struct ocfs2_caching_info *ci,
352*4882a593Smuzhiyun struct ocfs2_meta_cache_item **tree)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun int i;
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun mlog_bug_on_msg(ci->ci_num_cached != OCFS2_CACHE_INFO_MAX_ARRAY,
357*4882a593Smuzhiyun "Owner %llu, num cached = %u, should be %u\n",
358*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
359*4882a593Smuzhiyun ci->ci_num_cached, OCFS2_CACHE_INFO_MAX_ARRAY);
360*4882a593Smuzhiyun mlog_bug_on_msg(!(ci->ci_flags & OCFS2_CACHE_FL_INLINE),
361*4882a593Smuzhiyun "Owner %llu not marked as inline anymore!\n",
362*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci));
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /* Be careful to initialize the tree members *first* because
365*4882a593Smuzhiyun * once the ci_tree is used, the array is junk... */
366*4882a593Smuzhiyun for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
367*4882a593Smuzhiyun tree[i]->c_block = ci->ci_cache.ci_array[i];
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun ci->ci_flags &= ~OCFS2_CACHE_FL_INLINE;
370*4882a593Smuzhiyun ci->ci_cache.ci_tree = RB_ROOT;
371*4882a593Smuzhiyun /* this will be set again by __ocfs2_insert_cache_tree */
372*4882a593Smuzhiyun ci->ci_num_cached = 0;
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
375*4882a593Smuzhiyun __ocfs2_insert_cache_tree(ci, tree[i]);
376*4882a593Smuzhiyun tree[i] = NULL;
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun trace_ocfs2_expand_cache(
380*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
381*4882a593Smuzhiyun ci->ci_flags, ci->ci_num_cached);
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun /* Slow path function - memory allocation is necessary. See the
385*4882a593Smuzhiyun * comment above ocfs2_set_buffer_uptodate for more information. */
__ocfs2_set_buffer_uptodate(struct ocfs2_caching_info * ci,sector_t block,int expand_tree)386*4882a593Smuzhiyun static void __ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci,
387*4882a593Smuzhiyun sector_t block,
388*4882a593Smuzhiyun int expand_tree)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun int i;
391*4882a593Smuzhiyun struct ocfs2_meta_cache_item *new = NULL;
392*4882a593Smuzhiyun struct ocfs2_meta_cache_item *tree[OCFS2_CACHE_INFO_MAX_ARRAY] =
393*4882a593Smuzhiyun { NULL, };
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun trace_ocfs2_set_buffer_uptodate(
396*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
397*4882a593Smuzhiyun (unsigned long long)block, expand_tree);
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun new = kmem_cache_alloc(ocfs2_uptodate_cachep, GFP_NOFS);
400*4882a593Smuzhiyun if (!new) {
401*4882a593Smuzhiyun mlog_errno(-ENOMEM);
402*4882a593Smuzhiyun return;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun new->c_block = block;
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun if (expand_tree) {
407*4882a593Smuzhiyun /* Do *not* allocate an array here - the removal code
408*4882a593Smuzhiyun * has no way of tracking that. */
409*4882a593Smuzhiyun for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++) {
410*4882a593Smuzhiyun tree[i] = kmem_cache_alloc(ocfs2_uptodate_cachep,
411*4882a593Smuzhiyun GFP_NOFS);
412*4882a593Smuzhiyun if (!tree[i]) {
413*4882a593Smuzhiyun mlog_errno(-ENOMEM);
414*4882a593Smuzhiyun goto out_free;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun /* These are initialized in ocfs2_expand_cache! */
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun ocfs2_metadata_cache_lock(ci);
422*4882a593Smuzhiyun if (ocfs2_insert_can_use_array(ci)) {
423*4882a593Smuzhiyun /* Ok, items were removed from the cache in between
424*4882a593Smuzhiyun * locks. Detect this and revert back to the fast path */
425*4882a593Smuzhiyun ocfs2_append_cache_array(ci, block);
426*4882a593Smuzhiyun ocfs2_metadata_cache_unlock(ci);
427*4882a593Smuzhiyun goto out_free;
428*4882a593Smuzhiyun }
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun if (expand_tree)
431*4882a593Smuzhiyun ocfs2_expand_cache(ci, tree);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun __ocfs2_insert_cache_tree(ci, new);
434*4882a593Smuzhiyun ocfs2_metadata_cache_unlock(ci);
435*4882a593Smuzhiyun
436*4882a593Smuzhiyun new = NULL;
437*4882a593Smuzhiyun out_free:
438*4882a593Smuzhiyun if (new)
439*4882a593Smuzhiyun kmem_cache_free(ocfs2_uptodate_cachep, new);
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun /* If these were used, then ocfs2_expand_cache re-set them to
442*4882a593Smuzhiyun * NULL for us. */
443*4882a593Smuzhiyun if (tree[0]) {
444*4882a593Smuzhiyun for (i = 0; i < OCFS2_CACHE_INFO_MAX_ARRAY; i++)
445*4882a593Smuzhiyun if (tree[i])
446*4882a593Smuzhiyun kmem_cache_free(ocfs2_uptodate_cachep,
447*4882a593Smuzhiyun tree[i]);
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun
451*4882a593Smuzhiyun /* Item insertion is guarded by co_io_lock(), so the insertion path takes
452*4882a593Smuzhiyun * advantage of this by not rechecking for a duplicate insert during
453*4882a593Smuzhiyun * the slow case. Additionally, if the cache needs to be bumped up to
454*4882a593Smuzhiyun * a tree, the code will not recheck after acquiring the lock --
455*4882a593Smuzhiyun * multiple paths cannot be expanding to a tree at the same time.
456*4882a593Smuzhiyun *
457*4882a593Smuzhiyun * The slow path takes into account that items can be removed
458*4882a593Smuzhiyun * (including the whole tree wiped and reset) when this process it out
459*4882a593Smuzhiyun * allocating memory. In those cases, it reverts back to the fast
460*4882a593Smuzhiyun * path.
461*4882a593Smuzhiyun *
462*4882a593Smuzhiyun * Note that this function may actually fail to insert the block if
463*4882a593Smuzhiyun * memory cannot be allocated. This is not fatal however (but may
464*4882a593Smuzhiyun * result in a performance penalty)
465*4882a593Smuzhiyun *
466*4882a593Smuzhiyun * Readahead buffers can be passed in here before the I/O request is
467*4882a593Smuzhiyun * completed.
468*4882a593Smuzhiyun */
ocfs2_set_buffer_uptodate(struct ocfs2_caching_info * ci,struct buffer_head * bh)469*4882a593Smuzhiyun void ocfs2_set_buffer_uptodate(struct ocfs2_caching_info *ci,
470*4882a593Smuzhiyun struct buffer_head *bh)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun int expand;
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /* The block may very well exist in our cache already, so avoid
475*4882a593Smuzhiyun * doing any more work in that case. */
476*4882a593Smuzhiyun if (ocfs2_buffer_cached(ci, bh))
477*4882a593Smuzhiyun return;
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun trace_ocfs2_set_buffer_uptodate_begin(
480*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
481*4882a593Smuzhiyun (unsigned long long)bh->b_blocknr);
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun /* No need to recheck under spinlock - insertion is guarded by
484*4882a593Smuzhiyun * co_io_lock() */
485*4882a593Smuzhiyun ocfs2_metadata_cache_lock(ci);
486*4882a593Smuzhiyun if (ocfs2_insert_can_use_array(ci)) {
487*4882a593Smuzhiyun /* Fast case - it's an array and there's a free
488*4882a593Smuzhiyun * spot. */
489*4882a593Smuzhiyun ocfs2_append_cache_array(ci, bh->b_blocknr);
490*4882a593Smuzhiyun ocfs2_metadata_cache_unlock(ci);
491*4882a593Smuzhiyun return;
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun expand = 0;
495*4882a593Smuzhiyun if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
496*4882a593Smuzhiyun /* We need to bump things up to a tree. */
497*4882a593Smuzhiyun expand = 1;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun ocfs2_metadata_cache_unlock(ci);
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun __ocfs2_set_buffer_uptodate(ci, bh->b_blocknr, expand);
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun /* Called against a newly allocated buffer. Most likely nobody should
505*4882a593Smuzhiyun * be able to read this sort of metadata while it's still being
506*4882a593Smuzhiyun * allocated, but this is careful to take co_io_lock() anyway. */
ocfs2_set_new_buffer_uptodate(struct ocfs2_caching_info * ci,struct buffer_head * bh)507*4882a593Smuzhiyun void ocfs2_set_new_buffer_uptodate(struct ocfs2_caching_info *ci,
508*4882a593Smuzhiyun struct buffer_head *bh)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun /* This should definitely *not* exist in our cache */
511*4882a593Smuzhiyun BUG_ON(ocfs2_buffer_cached(ci, bh));
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun set_buffer_uptodate(bh);
514*4882a593Smuzhiyun
515*4882a593Smuzhiyun ocfs2_metadata_cache_io_lock(ci);
516*4882a593Smuzhiyun ocfs2_set_buffer_uptodate(ci, bh);
517*4882a593Smuzhiyun ocfs2_metadata_cache_io_unlock(ci);
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun
520*4882a593Smuzhiyun /* Requires ip_lock. */
ocfs2_remove_metadata_array(struct ocfs2_caching_info * ci,int index)521*4882a593Smuzhiyun static void ocfs2_remove_metadata_array(struct ocfs2_caching_info *ci,
522*4882a593Smuzhiyun int index)
523*4882a593Smuzhiyun {
524*4882a593Smuzhiyun sector_t *array = ci->ci_cache.ci_array;
525*4882a593Smuzhiyun int bytes;
526*4882a593Smuzhiyun
527*4882a593Smuzhiyun BUG_ON(index < 0 || index >= OCFS2_CACHE_INFO_MAX_ARRAY);
528*4882a593Smuzhiyun BUG_ON(index >= ci->ci_num_cached);
529*4882a593Smuzhiyun BUG_ON(!ci->ci_num_cached);
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun trace_ocfs2_remove_metadata_array(
532*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
533*4882a593Smuzhiyun index, ci->ci_num_cached);
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun ci->ci_num_cached--;
536*4882a593Smuzhiyun
537*4882a593Smuzhiyun /* don't need to copy if the array is now empty, or if we
538*4882a593Smuzhiyun * removed at the tail */
539*4882a593Smuzhiyun if (ci->ci_num_cached && index < ci->ci_num_cached) {
540*4882a593Smuzhiyun bytes = sizeof(sector_t) * (ci->ci_num_cached - index);
541*4882a593Smuzhiyun memmove(&array[index], &array[index + 1], bytes);
542*4882a593Smuzhiyun }
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /* Requires ip_lock. */
ocfs2_remove_metadata_tree(struct ocfs2_caching_info * ci,struct ocfs2_meta_cache_item * item)546*4882a593Smuzhiyun static void ocfs2_remove_metadata_tree(struct ocfs2_caching_info *ci,
547*4882a593Smuzhiyun struct ocfs2_meta_cache_item *item)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun trace_ocfs2_remove_metadata_tree(
550*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
551*4882a593Smuzhiyun (unsigned long long)item->c_block);
552*4882a593Smuzhiyun
553*4882a593Smuzhiyun rb_erase(&item->c_node, &ci->ci_cache.ci_tree);
554*4882a593Smuzhiyun ci->ci_num_cached--;
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun
ocfs2_remove_block_from_cache(struct ocfs2_caching_info * ci,sector_t block)557*4882a593Smuzhiyun static void ocfs2_remove_block_from_cache(struct ocfs2_caching_info *ci,
558*4882a593Smuzhiyun sector_t block)
559*4882a593Smuzhiyun {
560*4882a593Smuzhiyun int index;
561*4882a593Smuzhiyun struct ocfs2_meta_cache_item *item = NULL;
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun ocfs2_metadata_cache_lock(ci);
564*4882a593Smuzhiyun trace_ocfs2_remove_block_from_cache(
565*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
566*4882a593Smuzhiyun (unsigned long long) block, ci->ci_num_cached,
567*4882a593Smuzhiyun ci->ci_flags);
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun if (ci->ci_flags & OCFS2_CACHE_FL_INLINE) {
570*4882a593Smuzhiyun index = ocfs2_search_cache_array(ci, block);
571*4882a593Smuzhiyun if (index != -1)
572*4882a593Smuzhiyun ocfs2_remove_metadata_array(ci, index);
573*4882a593Smuzhiyun } else {
574*4882a593Smuzhiyun item = ocfs2_search_cache_tree(ci, block);
575*4882a593Smuzhiyun if (item)
576*4882a593Smuzhiyun ocfs2_remove_metadata_tree(ci, item);
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun ocfs2_metadata_cache_unlock(ci);
579*4882a593Smuzhiyun
580*4882a593Smuzhiyun if (item)
581*4882a593Smuzhiyun kmem_cache_free(ocfs2_uptodate_cachep, item);
582*4882a593Smuzhiyun }
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun /*
585*4882a593Smuzhiyun * Called when we remove a chunk of metadata from an inode. We don't
586*4882a593Smuzhiyun * bother reverting things to an inlined array in the case of a remove
587*4882a593Smuzhiyun * which moves us back under the limit.
588*4882a593Smuzhiyun */
ocfs2_remove_from_cache(struct ocfs2_caching_info * ci,struct buffer_head * bh)589*4882a593Smuzhiyun void ocfs2_remove_from_cache(struct ocfs2_caching_info *ci,
590*4882a593Smuzhiyun struct buffer_head *bh)
591*4882a593Smuzhiyun {
592*4882a593Smuzhiyun sector_t block = bh->b_blocknr;
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun ocfs2_remove_block_from_cache(ci, block);
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun /* Called when we remove xattr clusters from an inode. */
ocfs2_remove_xattr_clusters_from_cache(struct ocfs2_caching_info * ci,sector_t block,u32 c_len)598*4882a593Smuzhiyun void ocfs2_remove_xattr_clusters_from_cache(struct ocfs2_caching_info *ci,
599*4882a593Smuzhiyun sector_t block,
600*4882a593Smuzhiyun u32 c_len)
601*4882a593Smuzhiyun {
602*4882a593Smuzhiyun struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
603*4882a593Smuzhiyun unsigned int i, b_len = ocfs2_clusters_to_blocks(sb, 1) * c_len;
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun for (i = 0; i < b_len; i++, block++)
606*4882a593Smuzhiyun ocfs2_remove_block_from_cache(ci, block);
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun
init_ocfs2_uptodate_cache(void)609*4882a593Smuzhiyun int __init init_ocfs2_uptodate_cache(void)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun ocfs2_uptodate_cachep = kmem_cache_create("ocfs2_uptodate",
612*4882a593Smuzhiyun sizeof(struct ocfs2_meta_cache_item),
613*4882a593Smuzhiyun 0, SLAB_HWCACHE_ALIGN, NULL);
614*4882a593Smuzhiyun if (!ocfs2_uptodate_cachep)
615*4882a593Smuzhiyun return -ENOMEM;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun return 0;
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun
exit_ocfs2_uptodate_cache(void)620*4882a593Smuzhiyun void exit_ocfs2_uptodate_cache(void)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun kmem_cache_destroy(ocfs2_uptodate_cachep);
623*4882a593Smuzhiyun }
624