Whamcloud - gitweb
LU-56 ptlrpc: cleanup of ptlrpc_unregister_service
[fs/lustre-release.git] / lustre / fld / fld_cache.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/fld/fld_cache.c
35  *
36  * FLD (Fids Location Database)
37  *
38  * Author: Pravin Shelar <pravin.shelar@sun.com>
39  * Author: Yury Umanets <umka@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_FLD
43
44 #ifdef __KERNEL__
45 # include <libcfs/libcfs.h>
46 # include <linux/module.h>
47 # include <linux/jbd.h>
48 # include <asm/div64.h>
49 #else /* __KERNEL__ */
50 # include <liblustre.h>
51 # include <libcfs/list.h>
52 #endif
53
54 #include <obd.h>
55 #include <obd_class.h>
56 #include <lustre_ver.h>
57 #include <obd_support.h>
58 #include <lprocfs_status.h>
59
60 #include <dt_object.h>
61 #include <md_object.h>
62 #include <lustre_req_layout.h>
63 #include <lustre_fld.h>
64 #include "fld_internal.h"
65
66 /**
67  * create fld cache.
68  */
69 struct fld_cache *fld_cache_init(const char *name,
70                                  int cache_size, int cache_threshold)
71 {
72         struct fld_cache *cache;
73         ENTRY;
74
75         LASSERT(name != NULL);
76         LASSERT(cache_threshold < cache_size);
77
78         OBD_ALLOC_PTR(cache);
79         if (cache == NULL)
80                 RETURN(ERR_PTR(-ENOMEM));
81
82         CFS_INIT_LIST_HEAD(&cache->fci_entries_head);
83         CFS_INIT_LIST_HEAD(&cache->fci_lru);
84
85         cache->fci_cache_count = 0;
86         cfs_spin_lock_init(&cache->fci_lock);
87
88         strncpy(cache->fci_name, name,
89                 sizeof(cache->fci_name));
90
91         cache->fci_cache_size = cache_size;
92         cache->fci_threshold = cache_threshold;
93
94         /* Init fld cache info. */
95         memset(&cache->fci_stat, 0, sizeof(cache->fci_stat));
96
97         CDEBUG(D_INFO, "%s: FLD cache - Size: %d, Threshold: %d\n",
98                cache->fci_name, cache_size, cache_threshold);
99
100         RETURN(cache);
101 }
102
103 /**
104  * destroy fld cache.
105  */
106 void fld_cache_fini(struct fld_cache *cache)
107 {
108         __u64 pct;
109         ENTRY;
110
111         LASSERT(cache != NULL);
112         fld_cache_flush(cache);
113
114         if (cache->fci_stat.fst_count > 0) {
115                 pct = cache->fci_stat.fst_cache * 100;
116                 do_div(pct, cache->fci_stat.fst_count);
117         } else {
118                 pct = 0;
119         }
120
121         CDEBUG(D_INFO, "FLD cache statistics (%s):\n", cache->fci_name);
122         CDEBUG(D_INFO, "  Total reqs: "LPU64"\n", cache->fci_stat.fst_count);
123         CDEBUG(D_INFO, "  Cache reqs: "LPU64"\n", cache->fci_stat.fst_cache);
124         CDEBUG(D_INFO, "  Cache hits: "LPU64"%%\n", pct);
125
126         OBD_FREE_PTR(cache);
127
128         EXIT;
129 }
130
131 /**
132  * delete given node from list.
133  */
134 static inline void fld_cache_entry_delete(struct fld_cache *cache,
135                                           struct fld_cache_entry *node)
136 {
137         cfs_list_del(&node->fce_list);
138         cfs_list_del(&node->fce_lru);
139         cache->fci_cache_count--;
140         OBD_FREE_PTR(node);
141 }
142
143 /**
144  * fix list by checking new entry with NEXT entry in order.
145  */
146 static void fld_fix_new_list(struct fld_cache *cache)
147 {
148         struct fld_cache_entry *f_curr;
149         struct fld_cache_entry *f_next;
150         struct lu_seq_range *c_range;
151         struct lu_seq_range *n_range;
152         cfs_list_t *head = &cache->fci_entries_head;
153         ENTRY;
154
155 restart_fixup:
156
157         cfs_list_for_each_entry_safe(f_curr, f_next, head, fce_list) {
158                 c_range = &f_curr->fce_range;
159                 n_range = &f_next->fce_range;
160
161                 LASSERT(range_is_sane(c_range));
162                 if (&f_next->fce_list == head)
163                         break;
164
165                 LASSERT(c_range->lsr_start <= n_range->lsr_start);
166
167                 /* check merge possibility with next range */
168                 if (c_range->lsr_end == n_range->lsr_start) {
169                         if (c_range->lsr_index != n_range->lsr_index)
170                                 continue;
171                         n_range->lsr_start = c_range->lsr_start;
172                         fld_cache_entry_delete(cache, f_curr);
173                         continue;
174                 }
175
176                 /* check if current range overlaps with next range. */
177                 if (n_range->lsr_start < c_range->lsr_end) {
178
179                         if (c_range->lsr_index == n_range->lsr_index) {
180                                 n_range->lsr_start = c_range->lsr_start;
181                                 n_range->lsr_end = max(c_range->lsr_end,
182                                                        n_range->lsr_end);
183
184                                 fld_cache_entry_delete(cache, f_curr);
185                         } else {
186                                 if (n_range->lsr_end <= c_range->lsr_end) {
187                                         *n_range = *c_range;
188                                         fld_cache_entry_delete(cache, f_curr);
189                                 } else
190                                         n_range->lsr_start = c_range->lsr_end;
191                         }
192
193                         /* we could have overlap over next
194                          * range too. better restart. */
195                         goto restart_fixup;
196                 }
197
198                 /* kill duplicates */
199                 if (c_range->lsr_start == n_range->lsr_start &&
200                     c_range->lsr_end == n_range->lsr_end)
201                         fld_cache_entry_delete(cache, f_curr);
202         }
203
204         EXIT;
205 }
206
207 /**
208  * add node to fld cache
209  */
210 static inline void fld_cache_entry_add(struct fld_cache *cache,
211                                        struct fld_cache_entry *f_new,
212                                        cfs_list_t *pos)
213 {
214         cfs_list_add(&f_new->fce_list, pos);
215         cfs_list_add(&f_new->fce_lru, &cache->fci_lru);
216
217         cache->fci_cache_count++;
218         fld_fix_new_list(cache);
219 }
220
221 /**
222  * Check if cache needs to be shrunk. If so - do it.
223  * Remove one entry in list and so on until cache is shrunk enough.
224  */
225 static int fld_cache_shrink(struct fld_cache *cache)
226 {
227         struct fld_cache_entry *flde;
228         cfs_list_t *curr;
229         int num = 0;
230         ENTRY;
231
232         LASSERT(cache != NULL);
233
234         if (cache->fci_cache_count < cache->fci_cache_size)
235                 RETURN(0);
236
237         curr = cache->fci_lru.prev;
238
239         while (cache->fci_cache_count + cache->fci_threshold >
240                cache->fci_cache_size && curr != &cache->fci_lru) {
241
242                 flde = cfs_list_entry(curr, struct fld_cache_entry, fce_lru);
243                 curr = curr->prev;
244                 fld_cache_entry_delete(cache, flde);
245                 num++;
246         }
247
248         CDEBUG(D_INFO, "%s: FLD cache - Shrunk by "
249                "%d entries\n", cache->fci_name, num);
250
251         RETURN(0);
252 }
253
254 /**
255  * kill all fld cache entries.
256  */
257 void fld_cache_flush(struct fld_cache *cache)
258 {
259         ENTRY;
260
261         cfs_spin_lock(&cache->fci_lock);
262         cache->fci_cache_size = 0;
263         fld_cache_shrink(cache);
264         cfs_spin_unlock(&cache->fci_lock);
265
266         EXIT;
267 }
268
269 /**
270  * punch hole in existing range. divide this range and add new
271  * entry accordingly.
272  */
273
274 void fld_cache_punch_hole(struct fld_cache *cache,
275                           struct fld_cache_entry *f_curr,
276                           struct fld_cache_entry *f_new)
277 {
278         const struct lu_seq_range *range = &f_new->fce_range;
279         const seqno_t new_start  = range->lsr_start;
280         const seqno_t new_end  = range->lsr_end;
281         struct fld_cache_entry *fldt;
282
283         ENTRY;
284         OBD_ALLOC_GFP(fldt, sizeof *fldt, CFS_ALLOC_ATOMIC);
285         if (!fldt) {
286                 OBD_FREE_PTR(f_new);
287                 EXIT;
288                 /* overlap is not allowed, so dont mess up list. */
289                 return;
290         }
291         /*  break f_curr RANGE into three RANGES:
292          *        f_curr, f_new , fldt
293          */
294
295         /* f_new = *range */
296
297         /* fldt */
298         fldt->fce_range.lsr_start = new_end;
299         fldt->fce_range.lsr_end = f_curr->fce_range.lsr_end;
300         fldt->fce_range.lsr_index = f_curr->fce_range.lsr_index;
301
302         /* f_curr */
303         f_curr->fce_range.lsr_end = new_start;
304
305         /* add these two entries to list */
306         fld_cache_entry_add(cache, f_new, &f_curr->fce_list);
307         fld_cache_entry_add(cache, fldt, &f_new->fce_list);
308
309         /* no need to fixup */
310         EXIT;
311 }
312
313 /**
314  * handle range overlap in fld cache.
315  */
316 void fld_cache_overlap_handle(struct fld_cache *cache,
317                               struct fld_cache_entry *f_curr,
318                               struct fld_cache_entry *f_new)
319 {
320         const struct lu_seq_range *range = &f_new->fce_range;
321         const seqno_t new_start  = range->lsr_start;
322         const seqno_t new_end  = range->lsr_end;
323         const mdsno_t mdt = range->lsr_index;
324
325         /* this is overlap case, these case are checking overlapping with
326          * prev range only. fixup will handle overlaping with next range. */
327
328         if (f_curr->fce_range.lsr_index == mdt) {
329                 f_curr->fce_range.lsr_start = min(f_curr->fce_range.lsr_start,
330                                                   new_start);
331
332                 f_curr->fce_range.lsr_end = max(f_curr->fce_range.lsr_end,
333                                                 new_end);
334
335                 OBD_FREE_PTR(f_new);
336                 fld_fix_new_list(cache);
337
338         } else if (new_start <= f_curr->fce_range.lsr_start &&
339                         f_curr->fce_range.lsr_end <= new_end) {
340                 /* case 1: new range completely overshadowed existing range.
341                  *         e.g. whole range migrated. update fld cache entry */
342
343                 f_curr->fce_range = *range;
344                 OBD_FREE_PTR(f_new);
345                 fld_fix_new_list(cache);
346
347         } else if (f_curr->fce_range.lsr_start < new_start &&
348                         new_end < f_curr->fce_range.lsr_end) {
349                 /* case 2: new range fit within existing range. */
350
351                 fld_cache_punch_hole(cache, f_curr, f_new);
352
353         } else  if (new_end <= f_curr->fce_range.lsr_end) {
354                 /* case 3: overlap:
355                  *         [new_start [c_start  new_end)  c_end)
356                  */
357
358                 LASSERT(new_start <= f_curr->fce_range.lsr_start);
359
360                 f_curr->fce_range.lsr_start = new_end;
361                 fld_cache_entry_add(cache, f_new, f_curr->fce_list.prev);
362
363         } else if (f_curr->fce_range.lsr_start <= new_start) {
364                 /* case 4: overlap:
365                  *         [c_start [new_start c_end) new_end)
366                  */
367
368                 LASSERT(f_curr->fce_range.lsr_end <= new_end);
369
370                 f_curr->fce_range.lsr_end = new_start;
371                 fld_cache_entry_add(cache, f_new, &f_curr->fce_list);
372         } else
373                 CERROR("NEW range ="DRANGE" curr = "DRANGE"\n",
374                        PRANGE(range),PRANGE(&f_curr->fce_range));
375 }
376
377 /**
378  * Insert FLD entry in FLD cache.
379  *
380  * This function handles all cases of merging and breaking up of
381  * ranges.
382  */
383 void fld_cache_insert(struct fld_cache *cache,
384                       const struct lu_seq_range *range)
385 {
386         struct fld_cache_entry *f_new;
387         struct fld_cache_entry *f_curr;
388         struct fld_cache_entry *n;
389         cfs_list_t *head;
390         cfs_list_t *prev = NULL;
391         const seqno_t new_start  = range->lsr_start;
392         const seqno_t new_end  = range->lsr_end;
393         ENTRY;
394
395         LASSERT(range_is_sane(range));
396
397         /* Allocate new entry. */
398         OBD_ALLOC_PTR(f_new);
399         if (!f_new) {
400                 EXIT;
401                 return;
402         }
403
404         f_new->fce_range = *range;
405
406         /*
407          * Duplicate entries are eliminated in inset op.
408          * So we don't need to search new entry before starting insertion loop.
409          */
410
411         cfs_spin_lock(&cache->fci_lock);
412         fld_cache_shrink(cache);
413
414         head = &cache->fci_entries_head;
415
416         cfs_list_for_each_entry_safe(f_curr, n, head, fce_list) {
417                 /* add list if next is end of list */
418                 if (new_end < f_curr->fce_range.lsr_start)
419                         break;
420
421                 prev = &f_curr->fce_list;
422                 /* check if this range is to left of new range. */
423                 if (new_start < f_curr->fce_range.lsr_end) {
424                         fld_cache_overlap_handle(cache, f_curr, f_new);
425                         goto out;
426                 }
427         }
428
429         if (prev == NULL)
430                 prev = head;
431
432         /* Add new entry to cache and lru list. */
433         fld_cache_entry_add(cache, f_new, prev);
434 out:
435         cfs_spin_unlock(&cache->fci_lock);
436         EXIT;
437 }
438
439 /**
440  * lookup \a seq sequence for range in fld cache.
441  */
442 int fld_cache_lookup(struct fld_cache *cache,
443                      const seqno_t seq, struct lu_seq_range *range)
444 {
445         struct fld_cache_entry *flde;
446         cfs_list_t *head;
447         ENTRY;
448
449
450         cfs_spin_lock(&cache->fci_lock);
451         head = &cache->fci_entries_head;
452
453         cache->fci_stat.fst_count++;
454         cfs_list_for_each_entry(flde, head, fce_list) {
455                 if (flde->fce_range.lsr_start > seq)
456                         break;
457
458                 if (range_within(&flde->fce_range, seq)) {
459                         *range = flde->fce_range;
460
461                         /* update position of this entry in lru list. */
462                         cfs_list_move(&flde->fce_lru, &cache->fci_lru);
463                         cache->fci_stat.fst_cache++;
464                         cfs_spin_unlock(&cache->fci_lock);
465                         RETURN(0);
466                 }
467         }
468         cfs_spin_unlock(&cache->fci_lock);
469         RETURN(-ENOENT);
470 }