4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/fld/fld_cache.c
38 * FLD (Fids Location Database)
40 * Author: Pravin Shelar <pravin.shelar@sun.com>
41 * Author: Yury Umanets <umka@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_FLD
46 #include "../../include/linux/libcfs/libcfs.h"
47 #include <linux/module.h>
48 #include <asm/div64.h>
50 #include "../include/obd.h"
51 #include "../include/obd_class.h"
52 #include "../include/lustre_ver.h"
53 #include "../include/obd_support.h"
54 #include "../include/lprocfs_status.h"
56 #include "../include/lustre_req_layout.h"
57 #include "../include/lustre_fld.h"
58 #include "fld_internal.h"
63 struct fld_cache *fld_cache_init(const char *name,
64 int cache_size, int cache_threshold)
66 struct fld_cache *cache;
69 LASSERT(cache_threshold < cache_size);
71 cache = kzalloc(sizeof(*cache), GFP_NOFS);
73 return ERR_PTR(-ENOMEM);
75 INIT_LIST_HEAD(&cache->fci_entries_head);
76 INIT_LIST_HEAD(&cache->fci_lru);
78 cache->fci_cache_count = 0;
79 rwlock_init(&cache->fci_lock);
81 strlcpy(cache->fci_name, name,
82 sizeof(cache->fci_name));
84 cache->fci_cache_size = cache_size;
85 cache->fci_threshold = cache_threshold;
87 /* Init fld cache info. */
88 memset(&cache->fci_stat, 0, sizeof(cache->fci_stat));
90 CDEBUG(D_INFO, "%s: FLD cache - Size: %d, Threshold: %d\n",
91 cache->fci_name, cache_size, cache_threshold);
99 void fld_cache_fini(struct fld_cache *cache)
104 fld_cache_flush(cache);
106 if (cache->fci_stat.fst_count > 0) {
107 pct = cache->fci_stat.fst_cache * 100;
108 do_div(pct, cache->fci_stat.fst_count);
113 CDEBUG(D_INFO, "FLD cache statistics (%s):\n", cache->fci_name);
114 CDEBUG(D_INFO, " Total reqs: %llu\n", cache->fci_stat.fst_count);
115 CDEBUG(D_INFO, " Cache reqs: %llu\n", cache->fci_stat.fst_cache);
116 CDEBUG(D_INFO, " Cache hits: %llu%%\n", pct);
122 * delete given node from list.
124 static void fld_cache_entry_delete(struct fld_cache *cache,
125 struct fld_cache_entry *node)
127 list_del(&node->fce_list);
128 list_del(&node->fce_lru);
129 cache->fci_cache_count--;
134 * fix list by checking new entry with NEXT entry in order.
136 static void fld_fix_new_list(struct fld_cache *cache)
138 struct fld_cache_entry *f_curr;
139 struct fld_cache_entry *f_next;
140 struct lu_seq_range *c_range;
141 struct lu_seq_range *n_range;
142 struct list_head *head = &cache->fci_entries_head;
146 list_for_each_entry_safe(f_curr, f_next, head, fce_list) {
147 c_range = &f_curr->fce_range;
148 n_range = &f_next->fce_range;
150 LASSERT(range_is_sane(c_range));
151 if (&f_next->fce_list == head)
154 if (c_range->lsr_flags != n_range->lsr_flags)
157 LASSERTF(c_range->lsr_start <= n_range->lsr_start,
158 "cur lsr_start "DRANGE" next lsr_start "DRANGE"\n",
159 PRANGE(c_range), PRANGE(n_range));
161 /* check merge possibility with next range */
162 if (c_range->lsr_end == n_range->lsr_start) {
163 if (c_range->lsr_index != n_range->lsr_index)
165 n_range->lsr_start = c_range->lsr_start;
166 fld_cache_entry_delete(cache, f_curr);
170 /* check if current range overlaps with next range. */
171 if (n_range->lsr_start < c_range->lsr_end) {
172 if (c_range->lsr_index == n_range->lsr_index) {
173 n_range->lsr_start = c_range->lsr_start;
174 n_range->lsr_end = max(c_range->lsr_end,
176 fld_cache_entry_delete(cache, f_curr);
178 if (n_range->lsr_end <= c_range->lsr_end) {
180 fld_cache_entry_delete(cache, f_curr);
182 n_range->lsr_start = c_range->lsr_end;
186 /* we could have overlap over next
187 * range too. better restart.
192 /* kill duplicates */
193 if (c_range->lsr_start == n_range->lsr_start &&
194 c_range->lsr_end == n_range->lsr_end)
195 fld_cache_entry_delete(cache, f_curr);
200 * add node to fld cache
202 static inline void fld_cache_entry_add(struct fld_cache *cache,
203 struct fld_cache_entry *f_new,
204 struct list_head *pos)
206 list_add(&f_new->fce_list, pos);
207 list_add(&f_new->fce_lru, &cache->fci_lru);
209 cache->fci_cache_count++;
210 fld_fix_new_list(cache);
214 * Check if cache needs to be shrunk. If so - do it.
215 * Remove one entry in list and so on until cache is shrunk enough.
217 static int fld_cache_shrink(struct fld_cache *cache)
219 struct fld_cache_entry *flde;
220 struct list_head *curr;
223 if (cache->fci_cache_count < cache->fci_cache_size)
226 curr = cache->fci_lru.prev;
228 while (cache->fci_cache_count + cache->fci_threshold >
229 cache->fci_cache_size && curr != &cache->fci_lru) {
230 flde = list_entry(curr, struct fld_cache_entry, fce_lru);
232 fld_cache_entry_delete(cache, flde);
236 CDEBUG(D_INFO, "%s: FLD cache - Shrunk by %d entries\n",
237 cache->fci_name, num);
243 * kill all fld cache entries.
245 void fld_cache_flush(struct fld_cache *cache)
247 write_lock(&cache->fci_lock);
248 cache->fci_cache_size = 0;
249 fld_cache_shrink(cache);
250 write_unlock(&cache->fci_lock);
254 * punch hole in existing range. divide this range and add new
258 static void fld_cache_punch_hole(struct fld_cache *cache,
259 struct fld_cache_entry *f_curr,
260 struct fld_cache_entry *f_new)
262 const struct lu_seq_range *range = &f_new->fce_range;
263 const u64 new_start = range->lsr_start;
264 const u64 new_end = range->lsr_end;
265 struct fld_cache_entry *fldt;
267 fldt = kzalloc(sizeof(*fldt), GFP_ATOMIC);
270 /* overlap is not allowed, so dont mess up list. */
273 /* break f_curr RANGE into three RANGES:
274 * f_curr, f_new , fldt
280 fldt->fce_range.lsr_start = new_end;
281 fldt->fce_range.lsr_end = f_curr->fce_range.lsr_end;
282 fldt->fce_range.lsr_index = f_curr->fce_range.lsr_index;
285 f_curr->fce_range.lsr_end = new_start;
287 /* add these two entries to list */
288 fld_cache_entry_add(cache, f_new, &f_curr->fce_list);
289 fld_cache_entry_add(cache, fldt, &f_new->fce_list);
291 /* no need to fixup */
295 * handle range overlap in fld cache.
297 static void fld_cache_overlap_handle(struct fld_cache *cache,
298 struct fld_cache_entry *f_curr,
299 struct fld_cache_entry *f_new)
301 const struct lu_seq_range *range = &f_new->fce_range;
302 const u64 new_start = range->lsr_start;
303 const u64 new_end = range->lsr_end;
304 const u32 mdt = range->lsr_index;
306 /* this is overlap case, these case are checking overlapping with
307 * prev range only. fixup will handle overlapping with next range.
310 if (f_curr->fce_range.lsr_index == mdt) {
311 f_curr->fce_range.lsr_start = min(f_curr->fce_range.lsr_start,
314 f_curr->fce_range.lsr_end = max(f_curr->fce_range.lsr_end,
318 fld_fix_new_list(cache);
320 } else if (new_start <= f_curr->fce_range.lsr_start &&
321 f_curr->fce_range.lsr_end <= new_end) {
322 /* case 1: new range completely overshadowed existing range.
323 * e.g. whole range migrated. update fld cache entry
326 f_curr->fce_range = *range;
328 fld_fix_new_list(cache);
330 } else if (f_curr->fce_range.lsr_start < new_start &&
331 new_end < f_curr->fce_range.lsr_end) {
332 /* case 2: new range fit within existing range. */
334 fld_cache_punch_hole(cache, f_curr, f_new);
336 } else if (new_end <= f_curr->fce_range.lsr_end) {
338 * [new_start [c_start new_end) c_end)
341 LASSERT(new_start <= f_curr->fce_range.lsr_start);
343 f_curr->fce_range.lsr_start = new_end;
344 fld_cache_entry_add(cache, f_new, f_curr->fce_list.prev);
346 } else if (f_curr->fce_range.lsr_start <= new_start) {
348 * [c_start [new_start c_end) new_end)
351 LASSERT(f_curr->fce_range.lsr_end <= new_end);
353 f_curr->fce_range.lsr_end = new_start;
354 fld_cache_entry_add(cache, f_new, &f_curr->fce_list);
356 CERROR("NEW range ="DRANGE" curr = "DRANGE"\n",
357 PRANGE(range), PRANGE(&f_curr->fce_range));
360 struct fld_cache_entry
361 *fld_cache_entry_create(const struct lu_seq_range *range)
363 struct fld_cache_entry *f_new;
365 LASSERT(range_is_sane(range));
367 f_new = kzalloc(sizeof(*f_new), GFP_NOFS);
369 return ERR_PTR(-ENOMEM);
371 f_new->fce_range = *range;
376 * Insert FLD entry in FLD cache.
378 * This function handles all cases of merging and breaking up of
381 static int fld_cache_insert_nolock(struct fld_cache *cache,
382 struct fld_cache_entry *f_new)
384 struct fld_cache_entry *f_curr;
385 struct fld_cache_entry *n;
386 struct list_head *head;
387 struct list_head *prev = NULL;
388 const u64 new_start = f_new->fce_range.lsr_start;
389 const u64 new_end = f_new->fce_range.lsr_end;
390 __u32 new_flags = f_new->fce_range.lsr_flags;
393 * Duplicate entries are eliminated in insert op.
394 * So we don't need to search new entry before starting
398 if (!cache->fci_no_shrink)
399 fld_cache_shrink(cache);
401 head = &cache->fci_entries_head;
403 list_for_each_entry_safe(f_curr, n, head, fce_list) {
404 /* add list if next is end of list */
405 if (new_end < f_curr->fce_range.lsr_start ||
406 (new_end == f_curr->fce_range.lsr_start &&
407 new_flags != f_curr->fce_range.lsr_flags))
410 prev = &f_curr->fce_list;
411 /* check if this range is to left of new range. */
412 if (new_start < f_curr->fce_range.lsr_end &&
413 new_flags == f_curr->fce_range.lsr_flags) {
414 fld_cache_overlap_handle(cache, f_curr, f_new);
422 CDEBUG(D_INFO, "insert range "DRANGE"\n", PRANGE(&f_new->fce_range));
423 /* Add new entry to cache and lru list. */
424 fld_cache_entry_add(cache, f_new, prev);
429 int fld_cache_insert(struct fld_cache *cache,
430 const struct lu_seq_range *range)
432 struct fld_cache_entry *flde;
435 flde = fld_cache_entry_create(range);
437 return PTR_ERR(flde);
439 write_lock(&cache->fci_lock);
440 rc = fld_cache_insert_nolock(cache, flde);
441 write_unlock(&cache->fci_lock);
449 * Delete FLD entry in FLD cache.
453 struct fld_cache_entry
454 *fld_cache_entry_lookup_nolock(struct fld_cache *cache,
455 struct lu_seq_range *range)
457 struct fld_cache_entry *flde;
458 struct fld_cache_entry *got = NULL;
459 struct list_head *head;
461 head = &cache->fci_entries_head;
462 list_for_each_entry(flde, head, fce_list) {
463 if (range->lsr_start == flde->fce_range.lsr_start ||
464 (range->lsr_end == flde->fce_range.lsr_end &&
465 range->lsr_flags == flde->fce_range.lsr_flags)) {
475 * lookup \a seq sequence for range in fld cache.
477 struct fld_cache_entry
478 *fld_cache_entry_lookup(struct fld_cache *cache, struct lu_seq_range *range)
480 struct fld_cache_entry *got = NULL;
482 read_lock(&cache->fci_lock);
483 got = fld_cache_entry_lookup_nolock(cache, range);
484 read_unlock(&cache->fci_lock);
489 * lookup \a seq sequence for range in fld cache.
491 int fld_cache_lookup(struct fld_cache *cache,
492 const u64 seq, struct lu_seq_range *range)
494 struct fld_cache_entry *flde;
495 struct fld_cache_entry *prev = NULL;
496 struct list_head *head;
498 read_lock(&cache->fci_lock);
499 head = &cache->fci_entries_head;
501 cache->fci_stat.fst_count++;
502 list_for_each_entry(flde, head, fce_list) {
503 if (flde->fce_range.lsr_start > seq) {
505 *range = prev->fce_range;
510 if (range_within(&flde->fce_range, seq)) {
511 *range = flde->fce_range;
513 cache->fci_stat.fst_cache++;
514 read_unlock(&cache->fci_lock);
518 read_unlock(&cache->fci_lock);