Whamcloud - gitweb
LU-6245 client: remove types abstraction from client code
[fs/lustre-release.git] / lustre / ldlm / ldlm_pool.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  * Copyright (c) 2010, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ldlm/ldlm_pool.c
37  *
38  * Author: Yury Umanets <umka@clusterfs.com>
39  */
40
41 /*
42  * Idea of this code is rather simple. Each second, for each server namespace
43  * we have SLV - server lock volume which is calculated on current number of
44  * granted locks, grant speed for past period, etc - that is, locking load.
45  * This SLV number may be thought as a flow definition for simplicity. It is
46  * sent to clients with each occasion to let them know what is current load
47  * situation on the server. By default, at the beginning, SLV on server is
48  * set max value which is calculated as the following: allow to one client
49  * have all locks of limit ->pl_limit for 10h.
50  *
51  * Next, on clients, number of cached locks is not limited artificially in any
52  * way as it was before. Instead, client calculates CLV, that is, client lock
53  * volume for each lock and compares it with last SLV from the server. CLV is
54  * calculated as the number of locks in LRU * lock live time in seconds. If
55  * CLV > SLV - lock is canceled.
56  *
57  * Client has LVF, that is, lock volume factor which regulates how much sensitive
58  * client should be about last SLV from server. The higher LVF is the more locks
59  * will be canceled on client. Default value for it is 1. Setting LVF to 2 means
60  * that client will cancel locks 2 times faster.
61  *
62  * Locks on a client will be canceled more intensively in these cases:
63  * (1) if SLV is smaller, that is, load is higher on the server;
64  * (2) client has a lot of locks (the more locks are held by client, the bigger
65  *     chances that some of them should be canceled);
66  * (3) client has old locks (taken some time ago);
67  *
68  * Thus, according to flow paradigm that we use for better understanding SLV,
69  * CLV is the volume of particle in flow described by SLV. According to this,
70  * if flow is getting thinner, more and more particles become outside of it and
71  * as particles are locks, they should be canceled.
72  *
73  * General idea of this belongs to Vitaly Fertman (vitaly@clusterfs.com). Andreas
74  * Dilger (adilger@clusterfs.com) proposed few nice ideas like using LVF and many
75  * cleanups. Flow definition to allow more easy understanding of the logic belongs
76  * to Nikita Danilov (nikita@clusterfs.com) as well as many cleanups and fixes.
77  * And design and implementation are done by Yury Umanets (umka@clusterfs.com).
78  *
79  * Glossary for terms used:
80  *
81  * pl_limit - Number of allowed locks in pool. Applies to server and client
82  * side (tunable);
83  *
84  * pl_granted - Number of granted locks (calculated);
85  * pl_grant_rate - Number of granted locks for last T (calculated);
86  * pl_cancel_rate - Number of canceled locks for last T (calculated);
87  * pl_grant_speed - Grant speed (GR - CR) for last T (calculated);
88  * pl_grant_plan - Planned number of granted locks for next T (calculated);
89  * pl_server_lock_volume - Current server lock volume (calculated);
90  *
91  * As it may be seen from list above, we have few possible tunables which may
92  * affect behavior much. They all may be modified via proc. However, they also
93  * give a possibility for constructing few pre-defined behavior policies. If
94  * none of predefines is suitable for a working pattern being used, new one may
95  * be "constructed" via proc tunables.
96  */
97
98 #define DEBUG_SUBSYSTEM S_LDLM
99
100 #include <linux/kthread.h>
101 #include <lustre_dlm.h>
102 #include <cl_object.h>
103 #include <obd_class.h>
104 #include <obd_support.h>
105 #include "ldlm_internal.h"
106
107 #ifdef HAVE_LRU_RESIZE_SUPPORT
108
109 /*
110  * 50 ldlm locks for 1MB of RAM.
111  */
112 #define LDLM_POOL_HOST_L ((NUM_CACHEPAGES >> (20 - PAGE_CACHE_SHIFT)) * 50)
113
114 /*
115  * Maximal possible grant step plan in %.
116  */
117 #define LDLM_POOL_MAX_GSP (30)
118
119 /*
120  * Minimal possible grant step plan in %.
121  */
122 #define LDLM_POOL_MIN_GSP (1)
123
124 /*
125  * This controls the speed of reaching LDLM_POOL_MAX_GSP
126  * with increasing thread period.
127  */
128 #define LDLM_POOL_GSP_STEP_SHIFT (2)
129
130 /*
131  * LDLM_POOL_GSP% of all locks is default GP.
132  */
133 #define LDLM_POOL_GP(L)   (((L) * LDLM_POOL_MAX_GSP) / 100)
134
135 /*
136  * Max age for locks on clients.
137  */
138 #define LDLM_POOL_MAX_AGE (36000)
139
140 /*
141  * The granularity of SLV calculation.
142  */
143 #define LDLM_POOL_SLV_SHIFT (10)
144
145 extern struct proc_dir_entry *ldlm_ns_proc_dir;
146
147 static inline __u64 dru(__u64 val, __u32 shift, int round_up)
148 {
149         return (val + (round_up ? (1 << shift) - 1 : 0)) >> shift;
150 }
151
152 static inline __u64 ldlm_pool_slv_max(__u32 L)
153 {
154         /*
155          * Allow to have all locks for 1 client for 10 hrs.
156          * Formula is the following: limit * 10h / 1 client.
157          */
158         __u64 lim = (__u64)L *  LDLM_POOL_MAX_AGE / 1;
159         return lim;
160 }
161
162 static inline __u64 ldlm_pool_slv_min(__u32 L)
163 {
164         return 1;
165 }
166
167 enum {
168         LDLM_POOL_FIRST_STAT = 0,
169         LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
170         LDLM_POOL_GRANT_STAT,
171         LDLM_POOL_CANCEL_STAT,
172         LDLM_POOL_GRANT_RATE_STAT,
173         LDLM_POOL_CANCEL_RATE_STAT,
174         LDLM_POOL_GRANT_PLAN_STAT,
175         LDLM_POOL_SLV_STAT,
176         LDLM_POOL_SHRINK_REQTD_STAT,
177         LDLM_POOL_SHRINK_FREED_STAT,
178         LDLM_POOL_RECALC_STAT,
179         LDLM_POOL_TIMING_STAT,
180         LDLM_POOL_LAST_STAT
181 };
182
183 static inline struct ldlm_namespace *ldlm_pl2ns(struct ldlm_pool *pl)
184 {
185         return container_of(pl, struct ldlm_namespace, ns_pool);
186 }
187
188 /**
189  * Calculates suggested grant_step in % of available locks for passed
190  * \a period. This is later used in grant_plan calculations.
191  */
192 static inline int ldlm_pool_t2gsp(unsigned int t)
193 {
194         /*
195          * This yields 1% grant step for anything below LDLM_POOL_GSP_STEP
196          * and up to 30% for anything higher than LDLM_POOL_GSP_STEP.
197          *
198          * How this will affect execution is the following:
199          *
200          * - for thread period 1s we will have grant_step 1% which good from
201          * pov of taking some load off from server and push it out to clients.
202          * This is like that because 1% for grant_step means that server will
203          * not allow clients to get lots of locks in short period of time and
204          * keep all old locks in their caches. Clients will always have to
205          * get some locks back if they want to take some new;
206          *
207          * - for thread period 10s (which is default) we will have 23% which
208          * means that clients will have enough of room to take some new locks
209          * without getting some back. All locks from this 23% which were not
210          * taken by clients in current period will contribute in SLV growing.
211          * SLV growing means more locks cached on clients until limit or grant
212          * plan is reached.
213          */
214         return LDLM_POOL_MAX_GSP -
215                 ((LDLM_POOL_MAX_GSP - LDLM_POOL_MIN_GSP) >>
216                  (t >> LDLM_POOL_GSP_STEP_SHIFT));
217 }
218
219 static inline int ldlm_pool_granted(struct ldlm_pool *pl)
220 {
221         return atomic_read(&pl->pl_granted);
222 }
223
224 /**
225  * Recalculates next grant limit on passed \a pl.
226  *
227  * \pre ->pl_lock is locked.
228  */
229 static void ldlm_pool_recalc_grant_plan(struct ldlm_pool *pl)
230 {
231         int granted, grant_step, limit;
232
233         limit = ldlm_pool_get_limit(pl);
234         granted = ldlm_pool_granted(pl);
235
236         grant_step = ldlm_pool_t2gsp(pl->pl_recalc_period);
237         grant_step = ((limit - granted) * grant_step) / 100;
238         pl->pl_grant_plan = granted + grant_step;
239         limit = (limit * 5) >> 2;
240         if (pl->pl_grant_plan > limit)
241                 pl->pl_grant_plan = limit;
242 }
243
244 /**
245  * Recalculates next SLV on passed \a pl.
246  *
247  * \pre ->pl_lock is locked.
248  */
249 static void ldlm_pool_recalc_slv(struct ldlm_pool *pl)
250 {
251         int granted;
252         int grant_plan;
253         int round_up;
254         __u64 slv;
255         __u64 slv_factor;
256         __u64 grant_usage;
257         __u32 limit;
258
259         slv = pl->pl_server_lock_volume;
260         grant_plan = pl->pl_grant_plan;
261         limit = ldlm_pool_get_limit(pl);
262         granted = ldlm_pool_granted(pl);
263         round_up = granted < limit;
264
265         grant_usage = max_t(int, limit - (granted - grant_plan), 1);
266
267         /*
268          * Find out SLV change factor which is the ratio of grant usage
269          * from limit. SLV changes as fast as the ratio of grant plan
270          * consumption. The more locks from grant plan are not consumed
271          * by clients in last interval (idle time), the faster grows
272          * SLV. And the opposite, the more grant plan is over-consumed
273          * (load time) the faster drops SLV.
274          */
275         slv_factor = (grant_usage << LDLM_POOL_SLV_SHIFT);
276         do_div(slv_factor, limit);
277         slv = slv * slv_factor;
278         slv = dru(slv, LDLM_POOL_SLV_SHIFT, round_up);
279
280         if (slv > ldlm_pool_slv_max(limit)) {
281                 slv = ldlm_pool_slv_max(limit);
282         } else if (slv < ldlm_pool_slv_min(limit)) {
283                 slv = ldlm_pool_slv_min(limit);
284         }
285
286         pl->pl_server_lock_volume = slv;
287 }
288
289 /**
290  * Recalculates next stats on passed \a pl.
291  *
292  * \pre ->pl_lock is locked.
293  */
294 static void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
295 {
296         int grant_plan = pl->pl_grant_plan;
297         __u64 slv = pl->pl_server_lock_volume;
298         int granted = ldlm_pool_granted(pl);
299         int grant_rate = atomic_read(&pl->pl_grant_rate);
300         int cancel_rate = atomic_read(&pl->pl_cancel_rate);
301
302         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT,
303                             slv);
304         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
305                             granted);
306         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
307                             grant_rate);
308         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
309                             grant_plan);
310         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
311                             cancel_rate);
312 }
313
314 /**
315  * Sets current SLV into obd accessible via ldlm_pl2ns(pl)->ns_obd.
316  */
317 static void ldlm_srv_pool_push_slv(struct ldlm_pool *pl)
318 {
319         struct obd_device *obd;
320
321         /*
322          * Set new SLV in obd field for using it later without accessing the
323          * pool. This is required to avoid race between sending reply to client
324          * with new SLV and cleanup server stack in which we can't guarantee
325          * that namespace is still alive. We know only that obd is alive as
326          * long as valid export is alive.
327          */
328         obd = ldlm_pl2ns(pl)->ns_obd;
329         LASSERT(obd != NULL);
330         write_lock(&obd->obd_pool_lock);
331         obd->obd_pool_slv = pl->pl_server_lock_volume;
332         write_unlock(&obd->obd_pool_lock);
333 }
334
335 /**
336  * Recalculates all pool fields on passed \a pl.
337  *
338  * \pre ->pl_lock is not locked.
339  */
340 static int ldlm_srv_pool_recalc(struct ldlm_pool *pl)
341 {
342         time_t recalc_interval_sec;
343         ENTRY;
344
345         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
346         if (recalc_interval_sec < pl->pl_recalc_period)
347                 RETURN(0);
348
349         spin_lock(&pl->pl_lock);
350         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
351         if (recalc_interval_sec < pl->pl_recalc_period) {
352                 spin_unlock(&pl->pl_lock);
353                 RETURN(0);
354         }
355         /*
356          * Recalc SLV after last period. This should be done
357          * _before_ recalculating new grant plan.
358          */
359         ldlm_pool_recalc_slv(pl);
360
361         /*
362          * Make sure that pool informed obd of last SLV changes.
363          */
364         ldlm_srv_pool_push_slv(pl);
365
366         /*
367          * Update grant_plan for new period.
368          */
369         ldlm_pool_recalc_grant_plan(pl);
370
371         pl->pl_recalc_time = cfs_time_current_sec();
372         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
373                             recalc_interval_sec);
374         spin_unlock(&pl->pl_lock);
375         RETURN(0);
376 }
377
378 /**
379  * This function is used on server side as main entry point for memory
380  * pressure handling. It decreases SLV on \a pl according to passed
381  * \a nr and \a gfp_mask.
382  *
383  * Our goal here is to decrease SLV such a way that clients hold \a nr
384  * locks smaller in next 10h.
385  */
386 static int ldlm_srv_pool_shrink(struct ldlm_pool *pl,
387                                 int nr,  gfp_t gfp_mask)
388 {
389         __u32 limit;
390
391         /*
392          * VM is asking how many entries may be potentially freed.
393          */
394         if (nr == 0)
395                 return ldlm_pool_granted(pl);
396
397         /*
398          * Client already canceled locks but server is already in shrinker
399          * and can't cancel anything. Let's catch this race.
400          */
401         if (ldlm_pool_granted(pl) == 0)
402                 RETURN(0);
403
404         spin_lock(&pl->pl_lock);
405
406         /*
407          * We want shrinker to possibly cause cancellation of @nr locks from
408          * clients or grant approximately @nr locks smaller next intervals.
409          *
410          * This is why we decreased SLV by @nr. This effect will only be as
411          * long as one re-calc interval (1s these days) and this should be
412          * enough to pass this decreased SLV to all clients. On next recalc
413          * interval pool will either increase SLV if locks load is not high
414          * or will keep on same level or even decrease again, thus, shrinker
415          * decreased SLV will affect next recalc intervals and this way will
416          * make locking load lower.
417          */
418         if (nr < pl->pl_server_lock_volume) {
419                 pl->pl_server_lock_volume = pl->pl_server_lock_volume - nr;
420         } else {
421                 limit = ldlm_pool_get_limit(pl);
422                 pl->pl_server_lock_volume = ldlm_pool_slv_min(limit);
423         }
424
425         /*
426          * Make sure that pool informed obd of last SLV changes.
427          */
428         ldlm_srv_pool_push_slv(pl);
429         spin_unlock(&pl->pl_lock);
430
431         /*
432          * We did not really free any memory here so far, it only will be
433          * freed later may be, so that we return 0 to not confuse VM.
434          */
435         return 0;
436 }
437
438 /**
439  * Setup server side pool \a pl with passed \a limit.
440  */
441 static int ldlm_srv_pool_setup(struct ldlm_pool *pl, int limit)
442 {
443         struct obd_device *obd;
444
445         obd = ldlm_pl2ns(pl)->ns_obd;
446         LASSERT(obd != NULL && obd != LP_POISON);
447         LASSERT(obd->obd_type != LP_POISON);
448         write_lock(&obd->obd_pool_lock);
449         obd->obd_pool_limit = limit;
450         write_unlock(&obd->obd_pool_lock);
451
452         ldlm_pool_set_limit(pl, limit);
453         return 0;
454 }
455
456 /**
457  * Sets SLV and Limit from ldlm_pl2ns(pl)->ns_obd tp passed \a pl.
458  */
459 static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
460 {
461         struct obd_device *obd;
462
463         /*
464          * Get new SLV and Limit from obd which is updated with coming
465          * RPCs.
466          */
467         obd = ldlm_pl2ns(pl)->ns_obd;
468         LASSERT(obd != NULL);
469         read_lock(&obd->obd_pool_lock);
470         pl->pl_server_lock_volume = obd->obd_pool_slv;
471         ldlm_pool_set_limit(pl, obd->obd_pool_limit);
472         read_unlock(&obd->obd_pool_lock);
473 }
474
475 /**
476  * Recalculates client size pool \a pl according to current SLV and Limit.
477  */
478 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
479 {
480         time_t recalc_interval_sec;
481         int ret;
482         ENTRY;
483
484         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
485         if (recalc_interval_sec < pl->pl_recalc_period)
486                 RETURN(0);
487
488         spin_lock(&pl->pl_lock);
489         /*
490          * Check if we need to recalc lists now.
491          */
492         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
493         if (recalc_interval_sec < pl->pl_recalc_period) {
494                 spin_unlock(&pl->pl_lock);
495                 RETURN(0);
496         }
497
498         /*
499          * Make sure that pool knows last SLV and Limit from obd.
500          */
501         ldlm_cli_pool_pop_slv(pl);
502         spin_unlock(&pl->pl_lock);
503
504         /*
505          * Do not cancel locks in case lru resize is disabled for this ns.
506          */
507         if (!ns_connect_lru_resize(ldlm_pl2ns(pl)))
508                 GOTO(out, ret = 0);
509
510         /*
511          * In the time of canceling locks on client we do not need to maintain
512          * sharp timing, we only want to cancel locks asap according to new SLV.
513          * It may be called when SLV has changed much, this is why we do not
514          * take into account pl->pl_recalc_time here.
515          */
516         ret = ldlm_cancel_lru(ldlm_pl2ns(pl), 0, LCF_ASYNC,
517                               LDLM_LRU_FLAG_LRUR);
518
519 out:
520         spin_lock(&pl->pl_lock);
521         /*
522          * Time of LRU resizing might be longer than period,
523          * so update after LRU resizing rather than before it.
524          */
525         pl->pl_recalc_time = cfs_time_current_sec();
526         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT,
527                             recalc_interval_sec);
528         spin_unlock(&pl->pl_lock);
529         RETURN(ret);
530 }
531
532 /**
533  * This function is main entry point for memory pressure handling on client
534  * side.  Main goal of this function is to cancel some number of locks on
535  * passed \a pl according to \a nr and \a gfp_mask.
536  */
537 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
538                                 int nr, gfp_t gfp_mask)
539 {
540         struct ldlm_namespace *ns;
541         int unused;
542
543         ns = ldlm_pl2ns(pl);
544
545         /*
546          * Do not cancel locks in case lru resize is disabled for this ns.
547          */
548         if (!ns_connect_lru_resize(ns))
549                 RETURN(0);
550
551         /*
552          * Make sure that pool knows last SLV and Limit from obd.
553          */
554         ldlm_cli_pool_pop_slv(pl);
555
556         spin_lock(&ns->ns_lock);
557         unused = ns->ns_nr_unused;
558         spin_unlock(&ns->ns_lock);
559
560         if (nr == 0)
561                 return (unused / 100) * sysctl_vfs_cache_pressure;
562         else
563                 return ldlm_cancel_lru(ns, nr, LCF_ASYNC, LDLM_LRU_FLAG_SHRINK);
564 }
565
566 static struct ldlm_pool_ops ldlm_srv_pool_ops = {
567         .po_recalc = ldlm_srv_pool_recalc,
568         .po_shrink = ldlm_srv_pool_shrink,
569         .po_setup  = ldlm_srv_pool_setup
570 };
571
572 static struct ldlm_pool_ops ldlm_cli_pool_ops = {
573         .po_recalc = ldlm_cli_pool_recalc,
574         .po_shrink = ldlm_cli_pool_shrink
575 };
576
577 /**
578  * Pool recalc wrapper. Will call either client or server pool recalc callback
579  * depending what pool \a pl is used.
580  */
581 int ldlm_pool_recalc(struct ldlm_pool *pl)
582 {
583         time_t recalc_interval_sec;
584         int count;
585
586         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
587         if (recalc_interval_sec > 0) {
588                 spin_lock(&pl->pl_lock);
589                 recalc_interval_sec = cfs_time_current_sec() -
590                         pl->pl_recalc_time;
591
592                 if (recalc_interval_sec > 0) {
593                         /*
594                          * Update pool statistics every 1s.
595                          */
596                         ldlm_pool_recalc_stats(pl);
597
598                         /*
599                          * Zero out all rates and speed for the last period.
600                          */
601                         atomic_set(&pl->pl_grant_rate, 0);
602                         atomic_set(&pl->pl_cancel_rate, 0);
603                 }
604                 spin_unlock(&pl->pl_lock);
605         }
606
607         if (pl->pl_ops->po_recalc != NULL) {
608                 count = pl->pl_ops->po_recalc(pl);
609                 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT,
610                                     count);
611         }
612
613         recalc_interval_sec = pl->pl_recalc_time - cfs_time_current_sec() +
614                               pl->pl_recalc_period;
615         if (recalc_interval_sec <= 0) {
616                 /* DEBUG: should be re-removed after LU-4536 is fixed */
617                 CDEBUG(D_DLMTRACE, "%s: Negative interval(%ld), "
618                        "too short period(%ld)\n",
619                        pl->pl_name, recalc_interval_sec,
620                        pl->pl_recalc_period);
621
622                 /* Prevent too frequent recalculation. */
623                 recalc_interval_sec = 1;
624         }
625
626         return recalc_interval_sec;
627 }
628
629 /**
630  * Pool shrink wrapper. Will call either client or server pool recalc callback
631  * depending what pool \a pl is used.
632  */
633 int ldlm_pool_shrink(struct ldlm_pool *pl, int nr, gfp_t gfp_mask)
634 {
635         int cancel = 0;
636
637         if (pl->pl_ops->po_shrink != NULL) {
638                 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
639                 if (nr > 0) {
640                         lprocfs_counter_add(pl->pl_stats,
641                                             LDLM_POOL_SHRINK_REQTD_STAT,
642                                             nr);
643                         lprocfs_counter_add(pl->pl_stats,
644                                             LDLM_POOL_SHRINK_FREED_STAT,
645                                             cancel);
646                         CDEBUG(D_DLMTRACE, "%s: request to shrink %d locks, "
647                                "shrunk %d\n", pl->pl_name, nr, cancel);
648                 }
649         }
650         return cancel;
651 }
652
653 /**
654  * Pool setup wrapper. Will call either client or server pool recalc callback
655  * depending what pool \a pl is used.
656  *
657  * Sets passed \a limit into pool \a pl.
658  */
659 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
660 {
661         if (pl->pl_ops->po_setup != NULL)
662                 return(pl->pl_ops->po_setup(pl, limit));
663         return 0;
664 }
665
666 static int lprocfs_pool_state_seq_show(struct seq_file *m, void *unused)
667 {
668         int granted, grant_rate, cancel_rate, grant_step;
669         int grant_speed, grant_plan, lvf;
670         struct ldlm_pool *pl = m->private;
671         __u64 slv, clv;
672         __u32 limit;
673
674         spin_lock(&pl->pl_lock);
675         slv = pl->pl_server_lock_volume;
676         clv = pl->pl_client_lock_volume;
677         limit = ldlm_pool_get_limit(pl);
678         grant_plan = pl->pl_grant_plan;
679         granted = ldlm_pool_granted(pl);
680         grant_rate = atomic_read(&pl->pl_grant_rate);
681         cancel_rate = atomic_read(&pl->pl_cancel_rate);
682         grant_speed = grant_rate - cancel_rate;
683         lvf = atomic_read(&pl->pl_lock_volume_factor);
684         grant_step = ldlm_pool_t2gsp(pl->pl_recalc_period);
685         spin_unlock(&pl->pl_lock);
686
687         seq_printf(m, "LDLM pool state (%s):\n"
688                    "  SLV: %llu\n"
689                    "  CLV: %llu\n"
690                    "  LVF: %d\n",
691                    pl->pl_name, slv, clv, lvf);
692
693         if (ns_is_server(ldlm_pl2ns(pl))) {
694                 seq_printf(m, "  GSP: %d%%\n", grant_step);
695                 seq_printf(m, "  GP:  %d\n", grant_plan);
696         }
697
698         seq_printf(m, "  GR:  %d\n  CR:  %d\n  GS:  %d\n  G:   %d\n  L:   %d\n",
699                    grant_rate, cancel_rate, grant_speed,
700                    granted, limit);
701         return 0;
702 }
703 LPROC_SEQ_FOPS_RO(lprocfs_pool_state);
704
705 static int lprocfs_grant_speed_seq_show(struct seq_file *m, void *unused)
706 {
707         struct ldlm_pool *pl = m->private;
708         int               grant_speed;
709
710         spin_lock(&pl->pl_lock);
711         /* serialize with ldlm_pool_recalc */
712         grant_speed = atomic_read(&pl->pl_grant_rate) -
713                         atomic_read(&pl->pl_cancel_rate);
714         spin_unlock(&pl->pl_lock);
715         return lprocfs_uint_seq_show(m, &grant_speed);
716 }
717
718 LDLM_POOL_PROC_READER_SEQ_SHOW(grant_plan, int);
719 LPROC_SEQ_FOPS_RO(lprocfs_grant_plan);
720
721 LDLM_POOL_PROC_READER_SEQ_SHOW(recalc_period, int);
722 LDLM_POOL_PROC_WRITER(recalc_period, int);
723 static ssize_t lprocfs_recalc_period_seq_write(struct file *file,
724                                                const char __user *buf,
725                                                size_t len, loff_t *off)
726 {
727         struct seq_file *seq = file->private_data;
728
729         return lprocfs_wr_recalc_period(file, buf, len, seq->private);
730 }
731 LPROC_SEQ_FOPS(lprocfs_recalc_period);
732
733 LPROC_SEQ_FOPS_RO_TYPE(ldlm_pool, u64);
734 LPROC_SEQ_FOPS_RO_TYPE(ldlm_pool, atomic);
735 LPROC_SEQ_FOPS_RW_TYPE(ldlm_pool_rw, atomic);
736
737 LPROC_SEQ_FOPS_RO(lprocfs_grant_speed);
738
739 static int ldlm_pool_proc_init(struct ldlm_pool *pl)
740 {
741         struct ldlm_namespace *ns = ldlm_pl2ns(pl);
742         struct proc_dir_entry *parent_ns_proc;
743         struct lprocfs_vars pool_vars[2];
744         char *var_name = NULL;
745         int rc = 0;
746         ENTRY;
747
748         OBD_ALLOC(var_name, MAX_STRING_SIZE + 1);
749         if (!var_name)
750                 RETURN(-ENOMEM);
751
752         parent_ns_proc = ns->ns_proc_dir_entry;
753         if (parent_ns_proc == NULL) {
754                 CERROR("%s: proc entry is not initialized\n",
755                        ldlm_ns_name(ns));
756                 GOTO(out_free_name, rc = -EINVAL);
757         }
758         pl->pl_proc_dir = lprocfs_register("pool", parent_ns_proc,
759                                            NULL, NULL);
760         if (IS_ERR(pl->pl_proc_dir)) {
761                 rc = PTR_ERR(pl->pl_proc_dir);
762                 pl->pl_proc_dir = NULL;
763                 CERROR("%s: cannot create 'pool' proc entry: rc = %d\n",
764                        ldlm_ns_name(ns), rc);
765                 GOTO(out_free_name, rc);
766         }
767
768         var_name[MAX_STRING_SIZE] = '\0';
769         memset(pool_vars, 0, sizeof(pool_vars));
770         pool_vars[0].name = var_name;
771
772         ldlm_add_var(&pool_vars[0], pl->pl_proc_dir, "server_lock_volume",
773                      &pl->pl_server_lock_volume, &ldlm_pool_u64_fops);
774         ldlm_add_var(&pool_vars[0], pl->pl_proc_dir, "limit", &pl->pl_limit,
775                      &ldlm_pool_rw_atomic_fops);
776         ldlm_add_var(&pool_vars[0], pl->pl_proc_dir, "granted",
777                      &pl->pl_granted, &ldlm_pool_atomic_fops);
778         ldlm_add_var(&pool_vars[0], pl->pl_proc_dir, "grant_speed", pl,
779                      &lprocfs_grant_speed_fops);
780         ldlm_add_var(&pool_vars[0], pl->pl_proc_dir, "cancel_rate",
781                      &pl->pl_cancel_rate, &ldlm_pool_atomic_fops);
782         ldlm_add_var(&pool_vars[0], pl->pl_proc_dir, "grant_rate",
783                      &pl->pl_grant_rate, &ldlm_pool_atomic_fops);
784         ldlm_add_var(&pool_vars[0], pl->pl_proc_dir, "grant_plan", pl,
785                      &lprocfs_grant_plan_fops);
786         ldlm_add_var(&pool_vars[0], pl->pl_proc_dir, "recalc_period",
787                      pl, &lprocfs_recalc_period_fops);
788         ldlm_add_var(&pool_vars[0], pl->pl_proc_dir, "lock_volume_factor",
789                      &pl->pl_lock_volume_factor, &ldlm_pool_rw_atomic_fops);
790         ldlm_add_var(&pool_vars[0], pl->pl_proc_dir, "state", pl,
791                      &lprocfs_pool_state_fops);
792
793         pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
794                                            LDLM_POOL_FIRST_STAT, 0);
795         if (!pl->pl_stats)
796                 GOTO(out_free_name, rc = -ENOMEM);
797
798         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
799                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
800                              "granted", "locks");
801         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT,
802                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
803                              "grant", "locks");
804         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT,
805                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
806                              "cancel", "locks");
807         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
808                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
809                              "grant_rate", "locks/s");
810         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
811                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
812                              "cancel_rate", "locks/s");
813         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
814                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
815                              "grant_plan", "locks/s");
816         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
817                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
818                              "slv", "slv");
819         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
820                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
821                              "shrink_request", "locks");
822         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
823                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
824                              "shrink_freed", "locks");
825         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
826                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
827                              "recalc_freed", "locks");
828         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_TIMING_STAT,
829                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
830                              "recalc_timing", "sec");
831         rc = lprocfs_register_stats(pl->pl_proc_dir, "stats", pl->pl_stats);
832
833         EXIT;
834 out_free_name:
835         OBD_FREE(var_name, MAX_STRING_SIZE + 1);
836         return rc;
837 }
838
839 static void ldlm_pool_proc_fini(struct ldlm_pool *pl)
840 {
841         if (pl->pl_stats != NULL) {
842                 lprocfs_free_stats(&pl->pl_stats);
843                 pl->pl_stats = NULL;
844         }
845         if (pl->pl_proc_dir != NULL) {
846                 lprocfs_remove(&pl->pl_proc_dir);
847                 pl->pl_proc_dir = NULL;
848         }
849 }
850
851 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
852                    int idx, enum ldlm_side client)
853 {
854         int rc;
855         ENTRY;
856
857         spin_lock_init(&pl->pl_lock);
858         atomic_set(&pl->pl_granted, 0);
859         pl->pl_recalc_time = cfs_time_current_sec();
860         atomic_set(&pl->pl_lock_volume_factor, 1);
861
862         atomic_set(&pl->pl_grant_rate, 0);
863         atomic_set(&pl->pl_cancel_rate, 0);
864         pl->pl_grant_plan = LDLM_POOL_GP(LDLM_POOL_HOST_L);
865
866         snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
867                  ldlm_ns_name(ns), idx);
868
869         if (client == LDLM_NAMESPACE_SERVER) {
870                 pl->pl_ops = &ldlm_srv_pool_ops;
871                 ldlm_pool_set_limit(pl, LDLM_POOL_HOST_L);
872                 pl->pl_recalc_period = LDLM_POOL_SRV_DEF_RECALC_PERIOD;
873                 pl->pl_server_lock_volume = ldlm_pool_slv_max(LDLM_POOL_HOST_L);
874         } else {
875                 ldlm_pool_set_limit(pl, 1);
876                 pl->pl_server_lock_volume = 0;
877                 pl->pl_ops = &ldlm_cli_pool_ops;
878                 pl->pl_recalc_period = LDLM_POOL_CLI_DEF_RECALC_PERIOD;
879         }
880         pl->pl_client_lock_volume = 0;
881         rc = ldlm_pool_proc_init(pl);
882         if (rc)
883                 RETURN(rc);
884
885         CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
886
887         RETURN(rc);
888 }
889
890 void ldlm_pool_fini(struct ldlm_pool *pl)
891 {
892         ENTRY;
893         ldlm_pool_proc_fini(pl);
894
895         /*
896          * Pool should not be used after this point. We can't free it here as
897          * it lives in struct ldlm_namespace, but still interested in catching
898          * any abnormal using cases.
899          */
900         POISON(pl, 0x5a, sizeof(*pl));
901         EXIT;
902 }
903
904 /**
905  * Add new taken ldlm lock \a lock into pool \a pl accounting.
906  */
907 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
908 {
909         /*
910          * FLOCK locks are special in a sense that they are almost never
911          * cancelled, instead special kind of lock is used to drop them.
912          * also there is no LRU for flock locks, so no point in tracking
913          * them anyway.
914          *
915          * PLAIN locks are used by config and quota, the quantity is small
916          * and usually they are not in LRU.
917          */
918         if (lock->l_resource->lr_type == LDLM_FLOCK ||
919             lock->l_resource->lr_type == LDLM_PLAIN)
920                 return;
921
922         ldlm_reclaim_add(lock);
923
924         atomic_inc(&pl->pl_granted);
925         atomic_inc(&pl->pl_grant_rate);
926         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
927         /*
928          * Do not do pool recalc for client side as all locks which
929          * potentially may be canceled has already been packed into
930          * enqueue/cancel rpc. Also we do not want to run out of stack
931          * with too long call paths.
932          */
933         if (ns_is_server(ldlm_pl2ns(pl)))
934                 ldlm_pool_recalc(pl);
935 }
936
937 /**
938  * Remove ldlm lock \a lock from pool \a pl accounting.
939  */
940 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
941 {
942         /*
943          * Filter out FLOCK & PLAIN locks. Read above comment in
944          * ldlm_pool_add().
945          */
946         if (lock->l_resource->lr_type == LDLM_FLOCK ||
947             lock->l_resource->lr_type == LDLM_PLAIN)
948                 return;
949
950         ldlm_reclaim_del(lock);
951
952         LASSERT(atomic_read(&pl->pl_granted) > 0);
953         atomic_dec(&pl->pl_granted);
954         atomic_inc(&pl->pl_cancel_rate);
955
956         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
957
958         if (ns_is_server(ldlm_pl2ns(pl)))
959                 ldlm_pool_recalc(pl);
960 }
961
962 /**
963  * Returns current \a pl SLV.
964  *
965  * \pre ->pl_lock is not locked.
966  */
967 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
968 {
969         __u64 slv;
970         spin_lock(&pl->pl_lock);
971         slv = pl->pl_server_lock_volume;
972         spin_unlock(&pl->pl_lock);
973         return slv;
974 }
975
976 /**
977  * Sets passed \a slv to \a pl.
978  *
979  * \pre ->pl_lock is not locked.
980  */
981 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
982 {
983         spin_lock(&pl->pl_lock);
984         pl->pl_server_lock_volume = slv;
985         spin_unlock(&pl->pl_lock);
986 }
987
988 /**
989  * Returns current \a pl CLV.
990  *
991  * \pre ->pl_lock is not locked.
992  */
993 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
994 {
995         __u64 slv;
996         spin_lock(&pl->pl_lock);
997         slv = pl->pl_client_lock_volume;
998         spin_unlock(&pl->pl_lock);
999         return slv;
1000 }
1001
1002 /**
1003  * Sets passed \a clv to \a pl.
1004  *
1005  * \pre ->pl_lock is not locked.
1006  */
1007 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
1008 {
1009         spin_lock(&pl->pl_lock);
1010         pl->pl_client_lock_volume = clv;
1011         spin_unlock(&pl->pl_lock);
1012 }
1013
1014 /**
1015  * Returns current \a pl limit.
1016  */
1017 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1018 {
1019         return atomic_read(&pl->pl_limit);
1020 }
1021
1022 /**
1023  * Sets passed \a limit to \a pl.
1024  */
1025 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1026 {
1027         atomic_set(&pl->pl_limit, limit);
1028 }
1029
1030 /**
1031  * Returns current LVF from \a pl.
1032  */
1033 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
1034 {
1035         return atomic_read(&pl->pl_lock_volume_factor);
1036 }
1037
1038 static struct ptlrpc_thread *ldlm_pools_thread;
1039 static struct shrinker *ldlm_pools_srv_shrinker;
1040 static struct shrinker *ldlm_pools_cli_shrinker;
1041 static struct completion ldlm_pools_comp;
1042
1043 /*
1044 * count locks from all namespaces (if possible). Returns number of
1045 * cached locks.
1046 */
1047 static unsigned long ldlm_pools_count(enum ldlm_side client, gfp_t gfp_mask)
1048 {
1049         unsigned long total = 0;
1050         int nr_ns;
1051         struct ldlm_namespace *ns;
1052         struct ldlm_namespace *ns_old = NULL; /* loop detection */
1053
1054         if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
1055                 return 0;
1056
1057         CDEBUG(D_DLMTRACE, "Request to count %s locks from all pools\n",
1058                client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
1059
1060         /*
1061          * Find out how many resources we may release.
1062          */
1063         for (nr_ns = ldlm_namespace_nr_read(client);
1064              nr_ns > 0; nr_ns--) {
1065                 mutex_lock(ldlm_namespace_lock(client));
1066                 if (list_empty(ldlm_namespace_list(client))) {
1067                         mutex_unlock(ldlm_namespace_lock(client));
1068                         return 0;
1069                 }
1070                 ns = ldlm_namespace_first_locked(client);
1071
1072                 if (ns == ns_old) {
1073                         mutex_unlock(ldlm_namespace_lock(client));
1074                         break;
1075                 }
1076
1077                 if (ldlm_ns_empty(ns)) {
1078                         ldlm_namespace_move_to_inactive_locked(ns, client);
1079                         mutex_unlock(ldlm_namespace_lock(client));
1080                         continue;
1081                 }
1082
1083                 if (ns_old == NULL)
1084                         ns_old = ns;
1085
1086                 ldlm_namespace_get(ns);
1087                 ldlm_namespace_move_to_active_locked(ns, client);
1088                 mutex_unlock(ldlm_namespace_lock(client));
1089                 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
1090                 ldlm_namespace_put(ns);
1091         }
1092
1093         return total;
1094 }
1095
1096 static unsigned long ldlm_pools_scan(enum ldlm_side client, int nr,
1097                                      gfp_t gfp_mask)
1098 {
1099         unsigned long freed = 0;
1100         int tmp, nr_ns;
1101         struct ldlm_namespace *ns;
1102
1103         if (client == LDLM_NAMESPACE_CLIENT && !(gfp_mask & __GFP_FS))
1104                 return -1;
1105
1106         /*
1107          * Shrink at least ldlm_namespace_nr_read(client) namespaces.
1108          */
1109         for (tmp = nr_ns = ldlm_namespace_nr_read(client);
1110              tmp > 0; tmp--) {
1111                 int cancel, nr_locks;
1112
1113                 /*
1114                  * Do not call shrink under ldlm_namespace_lock(client)
1115                 */
1116                 mutex_lock(ldlm_namespace_lock(client));
1117                 if (list_empty(ldlm_namespace_list(client))) {
1118                         mutex_unlock(ldlm_namespace_lock(client));
1119                         break;
1120                 }
1121                 ns = ldlm_namespace_first_locked(client);
1122                 ldlm_namespace_get(ns);
1123                 ldlm_namespace_move_to_active_locked(ns, client);
1124                 mutex_unlock(ldlm_namespace_lock(client));
1125
1126                 nr_locks = ldlm_pool_granted(&ns->ns_pool);
1127                 /*
1128                  * We use to shrink propotionally but with new shrinker API,
1129                  * we lost the total number of freeable locks.
1130                  */
1131                 cancel = 1 + min_t(int, nr_locks, nr / nr_ns);
1132                 freed += ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
1133                 ldlm_namespace_put(ns);
1134         }
1135         /*
1136          * we only decrease the SLV in server pools shrinker, return
1137          * SHRINK_STOP to kernel to avoid needless loop. LU-1128
1138          */
1139         return (client == LDLM_NAMESPACE_SERVER) ? SHRINK_STOP : freed;
1140 }
1141
1142 #ifdef HAVE_SHRINKER_COUNT
1143 static unsigned long ldlm_pools_srv_count(struct shrinker *s,
1144                                           struct shrink_control *sc)
1145 {
1146         return ldlm_pools_count(LDLM_NAMESPACE_SERVER, sc->gfp_mask);
1147 }
1148
1149 static unsigned long ldlm_pools_srv_scan(struct shrinker *s,
1150                                          struct shrink_control *sc)
1151 {
1152         return ldlm_pools_scan(LDLM_NAMESPACE_SERVER, sc->nr_to_scan,
1153                                sc->gfp_mask);
1154 }
1155
1156 static unsigned long ldlm_pools_cli_count(struct shrinker *s, struct shrink_control *sc)
1157 {
1158         return ldlm_pools_count(LDLM_NAMESPACE_CLIENT, sc->gfp_mask);
1159 }
1160
1161 static unsigned long ldlm_pools_cli_scan(struct shrinker *s,
1162                                          struct shrink_control *sc)
1163 {
1164         return ldlm_pools_scan(LDLM_NAMESPACE_CLIENT, sc->nr_to_scan,
1165                                sc->gfp_mask);
1166 }
1167
1168 #else
1169 /*
1170  * Cancel \a nr locks from all namespaces (if possible). Returns number of
1171  * cached locks after shrink is finished. All namespaces are asked to
1172  * cancel approximately equal amount of locks to keep balancing.
1173  */
1174 static int ldlm_pools_shrink(enum ldlm_side client, int nr, gfp_t gfp_mask)
1175 {
1176         unsigned long total = 0;
1177
1178         if (client == LDLM_NAMESPACE_CLIENT && nr != 0 &&
1179             !(gfp_mask & __GFP_FS))
1180                 return -1;
1181
1182         CDEBUG(D_DLMTRACE, "Request to shrink %d %s locks from all pools\n",
1183                nr, client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
1184
1185         total = ldlm_pools_count(client, gfp_mask);
1186
1187         if (nr == 0 || total == 0)
1188                 return total;
1189
1190         return ldlm_pools_scan(client, nr, gfp_mask);
1191 }
1192
1193 static int ldlm_pools_srv_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
1194 {
1195         return ldlm_pools_shrink(LDLM_NAMESPACE_SERVER,
1196                                  shrink_param(sc, nr_to_scan),
1197                                  shrink_param(sc, gfp_mask));
1198 }
1199
1200 static int ldlm_pools_cli_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
1201 {
1202         return ldlm_pools_shrink(LDLM_NAMESPACE_CLIENT,
1203                                  shrink_param(sc, nr_to_scan),
1204                                  shrink_param(sc, gfp_mask));
1205 }
1206
1207 #endif /* HAVE_SHRINKER_COUNT */
1208
1209 int ldlm_pools_recalc(enum ldlm_side client)
1210 {
1211         unsigned long nr_l = 0, nr_p = 0, l;
1212         struct ldlm_namespace *ns;
1213         struct ldlm_namespace *ns_old = NULL;
1214         int nr, equal = 0;
1215         /* seconds of sleep if no active namespaces */
1216         int time = client ? LDLM_POOL_CLI_DEF_RECALC_PERIOD :
1217                             LDLM_POOL_SRV_DEF_RECALC_PERIOD;
1218
1219         /*
1220          * No need to setup pool limit for client pools.
1221          */
1222         if (client == LDLM_NAMESPACE_SERVER) {
1223                 /*
1224                  * Check all modest namespaces first.
1225                  */
1226                 mutex_lock(ldlm_namespace_lock(client));
1227                 list_for_each_entry(ns, ldlm_namespace_list(client),
1228                                     ns_list_chain)
1229                 {
1230                         if (ns->ns_appetite != LDLM_NAMESPACE_MODEST)
1231                                 continue;
1232
1233                         l = ldlm_pool_granted(&ns->ns_pool);
1234                         if (l == 0)
1235                                 l = 1;
1236
1237                         /*
1238                          * Set the modest pools limit equal to their avg granted
1239                          * locks + ~6%.
1240                          */
1241                         l += dru(l, LDLM_POOLS_MODEST_MARGIN_SHIFT, 0);
1242                         ldlm_pool_setup(&ns->ns_pool, l);
1243                         nr_l += l;
1244                         nr_p++;
1245                 }
1246
1247                 /*
1248                  * Make sure that modest namespaces did not eat more that 2/3
1249                  * of limit.
1250                  */
1251                 if (nr_l >= 2 * (LDLM_POOL_HOST_L / 3)) {
1252                         CWARN("\"Modest\" pools eat out 2/3 of server locks "
1253                               "limit (%lu of %lu). This means that you have too "
1254                               "many clients for this amount of server RAM. "
1255                               "Upgrade server!\n", nr_l, LDLM_POOL_HOST_L);
1256                         equal = 1;
1257                 }
1258
1259                 /*
1260                  * The rest is given to greedy namespaces.
1261                  */
1262                 list_for_each_entry(ns, ldlm_namespace_list(client),
1263                                     ns_list_chain)
1264                 {
1265                         if (!equal && ns->ns_appetite != LDLM_NAMESPACE_GREEDY)
1266                                 continue;
1267
1268                         if (equal) {
1269                                 /*
1270                                  * In the case 2/3 locks are eaten out by
1271                                  * modest pools, we re-setup equal limit
1272                                  * for _all_ pools.
1273                                  */
1274                                 l = LDLM_POOL_HOST_L /
1275                                         ldlm_namespace_nr_read(client);
1276                         } else {
1277                                 /*
1278                                  * All the rest of greedy pools will have
1279                                  * all locks in equal parts.
1280                                  */
1281                                 l = (LDLM_POOL_HOST_L - nr_l) /
1282                                         (ldlm_namespace_nr_read(client) -
1283                                          nr_p);
1284                         }
1285                         ldlm_pool_setup(&ns->ns_pool, l);
1286                 }
1287                 mutex_unlock(ldlm_namespace_lock(client));
1288         }
1289
1290         /*
1291          * Recalc at least ldlm_namespace_nr(client) namespaces.
1292          */
1293         for (nr = ldlm_namespace_nr_read(client); nr > 0; nr--) {
1294                 int     skip;
1295                 /*
1296                  * Lock the list, get first @ns in the list, getref, move it
1297                  * to the tail, unlock and call pool recalc. This way we avoid
1298                  * calling recalc under @ns lock what is really good as we get
1299                  * rid of potential deadlock on client nodes when canceling
1300                  * locks synchronously.
1301                  */
1302                 mutex_lock(ldlm_namespace_lock(client));
1303                 if (list_empty(ldlm_namespace_list(client))) {
1304                         mutex_unlock(ldlm_namespace_lock(client));
1305                         break;
1306                 }
1307                 ns = ldlm_namespace_first_locked(client);
1308
1309                 if (ns_old == ns) { /* Full pass complete */
1310                         mutex_unlock(ldlm_namespace_lock(client));
1311                         break;
1312                 }
1313
1314                 /* We got an empty namespace, need to move it back to inactive
1315                  * list.
1316                  * The race with parallel resource creation is fine:
1317                  * - If they do namespace_get before our check, we fail the
1318                  *   check and they move this item to the end of the list anyway
1319                  * - If we do the check and then they do namespace_get, then
1320                  *   we move the namespace to inactive and they will move
1321                  *   it back to active (synchronised by the lock, so no clash
1322                  *   there).
1323                  */
1324                 if (ldlm_ns_empty(ns)) {
1325                         ldlm_namespace_move_to_inactive_locked(ns, client);
1326                         mutex_unlock(ldlm_namespace_lock(client));
1327                         continue;
1328                 }
1329
1330                 if (ns_old == NULL)
1331                         ns_old = ns;
1332
1333                 spin_lock(&ns->ns_lock);
1334                 /*
1335                  * skip ns which is being freed, and we don't want to increase
1336                  * its refcount again, not even temporarily. bz21519 & LU-499.
1337                  */
1338                 if (ns->ns_stopping) {
1339                         skip = 1;
1340                 } else {
1341                         skip = 0;
1342                         ldlm_namespace_get(ns);
1343                 }
1344                 spin_unlock(&ns->ns_lock);
1345
1346                 ldlm_namespace_move_to_active_locked(ns, client);
1347                 mutex_unlock(ldlm_namespace_lock(client));
1348
1349                 /*
1350                  * After setup is done - recalc the pool.
1351                  */
1352                 if (!skip) {
1353                         int ttime = ldlm_pool_recalc(&ns->ns_pool);
1354
1355                         if (ttime < time)
1356                                 time = ttime;
1357
1358                         ldlm_namespace_put(ns);
1359                 }
1360         }
1361
1362         /* Wake up the blocking threads from time to time. */
1363         ldlm_bl_thread_wakeup();
1364
1365         return time;
1366 }
1367
1368 static int ldlm_pools_thread_main(void *arg)
1369 {
1370         struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
1371         int s_time, c_time;
1372         ENTRY;
1373
1374         thread_set_flags(thread, SVC_RUNNING);
1375         wake_up(&thread->t_ctl_waitq);
1376
1377         CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
1378                "ldlm_poold", current_pid());
1379
1380         while (1) {
1381                 struct l_wait_info lwi;
1382
1383                 /*
1384                  * Recal all pools on this tick.
1385                  */
1386                 s_time = ldlm_pools_recalc(LDLM_NAMESPACE_SERVER);
1387                 c_time = ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
1388
1389                 /*
1390                  * Wait until the next check time, or until we're
1391                  * stopped.
1392                  */
1393                 lwi = LWI_TIMEOUT(cfs_time_seconds(min(s_time, c_time)),
1394                                   NULL, NULL);
1395                 l_wait_event(thread->t_ctl_waitq,
1396                              thread_is_stopping(thread) ||
1397                              thread_is_event(thread),
1398                              &lwi);
1399
1400                 if (thread_test_and_clear_flags(thread, SVC_STOPPING))
1401                         break;
1402                 else
1403                         thread_test_and_clear_flags(thread, SVC_EVENT);
1404         }
1405
1406         thread_set_flags(thread, SVC_STOPPED);
1407         wake_up(&thread->t_ctl_waitq);
1408
1409         CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
1410                 "ldlm_poold", current_pid());
1411
1412         complete_and_exit(&ldlm_pools_comp, 0);
1413 }
1414
1415 static int ldlm_pools_thread_start(void)
1416 {
1417         struct l_wait_info lwi = { 0 };
1418         struct task_struct *task;
1419         ENTRY;
1420
1421         if (ldlm_pools_thread != NULL)
1422                 RETURN(-EALREADY);
1423
1424         OBD_ALLOC_PTR(ldlm_pools_thread);
1425         if (ldlm_pools_thread == NULL)
1426                 RETURN(-ENOMEM);
1427
1428         init_completion(&ldlm_pools_comp);
1429         init_waitqueue_head(&ldlm_pools_thread->t_ctl_waitq);
1430
1431         task = kthread_run(ldlm_pools_thread_main, ldlm_pools_thread,
1432                            "ldlm_poold");
1433         if (IS_ERR(task)) {
1434                 CERROR("Can't start pool thread, error %ld\n", PTR_ERR(task));
1435                 OBD_FREE(ldlm_pools_thread, sizeof(*ldlm_pools_thread));
1436                 ldlm_pools_thread = NULL;
1437                 RETURN(PTR_ERR(task));
1438         }
1439         l_wait_event(ldlm_pools_thread->t_ctl_waitq,
1440                      thread_is_running(ldlm_pools_thread), &lwi);
1441         RETURN(0);
1442 }
1443
1444 static void ldlm_pools_thread_stop(void)
1445 {
1446         ENTRY;
1447
1448         if (ldlm_pools_thread == NULL) {
1449                 EXIT;
1450                 return;
1451         }
1452
1453         thread_set_flags(ldlm_pools_thread, SVC_STOPPING);
1454         wake_up(&ldlm_pools_thread->t_ctl_waitq);
1455
1456         /*
1457          * Make sure that pools thread is finished before freeing @thread.
1458          * This fixes possible race and oops due to accessing freed memory
1459          * in pools thread.
1460          */
1461         wait_for_completion(&ldlm_pools_comp);
1462         OBD_FREE_PTR(ldlm_pools_thread);
1463         ldlm_pools_thread = NULL;
1464         EXIT;
1465 }
1466
1467 int ldlm_pools_init(void)
1468 {
1469         int rc;
1470         DEF_SHRINKER_VAR(shsvar, ldlm_pools_srv_shrink,
1471                          ldlm_pools_srv_count, ldlm_pools_srv_scan);
1472         DEF_SHRINKER_VAR(shcvar, ldlm_pools_cli_shrink,
1473                          ldlm_pools_cli_count, ldlm_pools_cli_scan);
1474         ENTRY;
1475
1476         rc = ldlm_pools_thread_start();
1477         if (rc == 0) {
1478                 ldlm_pools_srv_shrinker =
1479                         set_shrinker(DEFAULT_SEEKS, &shsvar);
1480                 ldlm_pools_cli_shrinker =
1481                         set_shrinker(DEFAULT_SEEKS, &shcvar);
1482         }
1483         RETURN(rc);
1484 }
1485
1486 void ldlm_pools_fini(void)
1487 {
1488         if (ldlm_pools_srv_shrinker != NULL) {
1489                 remove_shrinker(ldlm_pools_srv_shrinker);
1490                 ldlm_pools_srv_shrinker = NULL;
1491         }
1492         if (ldlm_pools_cli_shrinker != NULL) {
1493                 remove_shrinker(ldlm_pools_cli_shrinker);
1494                 ldlm_pools_cli_shrinker = NULL;
1495         }
1496         ldlm_pools_thread_stop();
1497 }
1498
1499 #else /* !HAVE_LRU_RESIZE_SUPPORT */
1500 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
1501 {
1502         return 0;
1503 }
1504
1505 int ldlm_pool_recalc(struct ldlm_pool *pl)
1506 {
1507         return 0;
1508 }
1509
1510 int ldlm_pool_shrink(struct ldlm_pool *pl,
1511                      int nr, gfp_t gfp_mask)
1512 {
1513         return 0;
1514 }
1515
1516 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
1517                    int idx, enum ldlm_side client)
1518 {
1519         return 0;
1520 }
1521
1522 void ldlm_pool_fini(struct ldlm_pool *pl)
1523 {
1524         return;
1525 }
1526
1527 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
1528 {
1529         return;
1530 }
1531
1532 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
1533 {
1534         return;
1535 }
1536
1537 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
1538 {
1539         return 1;
1540 }
1541
1542 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
1543 {
1544         return;
1545 }
1546
1547 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
1548 {
1549         return 1;
1550 }
1551
1552 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
1553 {
1554         return;
1555 }
1556
1557 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1558 {
1559         return 0;
1560 }
1561
1562 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1563 {
1564         return;
1565 }
1566
1567 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
1568 {
1569         return 0;
1570 }
1571
1572 int ldlm_pools_init(void)
1573 {
1574         return 0;
1575 }
1576
1577 void ldlm_pools_fini(void)
1578 {
1579         return;
1580 }
1581
1582 int ldlm_pools_recalc(enum ldlm_side client)
1583 {
1584         return 0;
1585 }
1586 #endif /* HAVE_LRU_RESIZE_SUPPORT */