Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / ldlm / ldlm_pool.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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  *
90  * pl_grant_step - Grant plan step, that is how ->pl_grant_plan
91  * will change in next T (tunable);
92  *
93  * pl_server_lock_volume - Current server lock volume (calculated);
94  *
95  * As it may be seen from list above, we have few possible tunables which may
96  * affect behavior much. They all may be modified via proc. However, they also
97  * give a possibility for constructing few pre-defined behavior policies. If
98  * none of predefines is suitable for a working pattern being used, new one may
99  * be "constructed" via proc tunables.
100  */
101
102 #define DEBUG_SUBSYSTEM S_LDLM
103
104 #ifdef __KERNEL__
105 # include <lustre_dlm.h>
106 #else
107 # include <liblustre.h>
108 #endif
109
110 #include <obd_class.h>
111 #include <obd_support.h>
112 #include "ldlm_internal.h"
113
114 #ifdef HAVE_LRU_RESIZE_SUPPORT
115
116 /*
117  * 50 ldlm locks for 1MB of RAM. 
118  */
119 #define LDLM_POOL_HOST_L ((num_physpages >> (20 - CFS_PAGE_SHIFT)) * 50)
120
121 /*
122  * Default step in % for grant plan. 
123  */
124 #define LDLM_POOL_GSP (10)
125
126 /* 
127  * LDLM_POOL_GSP% of all locks is default GP. 
128  */
129 #define LDLM_POOL_GP(L)   (((L) * LDLM_POOL_GSP) / 100)
130
131 /* 
132  * Max age for locks on clients. 
133  */
134 #define LDLM_POOL_MAX_AGE (36000)
135
136 #ifdef __KERNEL__
137 extern cfs_proc_dir_entry_t *ldlm_ns_proc_dir;
138 #endif
139
140 #define avg(src, add) \
141         ((src) = ((src) + (add)) / 2)
142
143 static inline __u64 dru(__u64 val, __u32 div)
144 {
145         __u64 ret = val + (div - 1);
146         do_div(ret, div);
147         return ret;
148 }
149
150 static inline __u64 ldlm_pool_slv_max(__u32 L)
151 {
152         /*
153          * Allow to have all locks for 1 client for 10 hrs.
154          * Formula is the following: limit * 10h / 1 client. 
155          */
156         __u64 lim = L *  LDLM_POOL_MAX_AGE / 1;
157         return lim;
158 }
159
160 static inline __u64 ldlm_pool_slv_min(__u32 L)
161 {
162         return 1;
163 }
164
165 enum {
166         LDLM_POOL_FIRST_STAT = 0,
167         LDLM_POOL_GRANTED_STAT = LDLM_POOL_FIRST_STAT,
168         LDLM_POOL_GRANT_STAT,
169         LDLM_POOL_CANCEL_STAT,
170         LDLM_POOL_GRANT_RATE_STAT,
171         LDLM_POOL_CANCEL_RATE_STAT,
172         LDLM_POOL_GRANT_PLAN_STAT,
173         LDLM_POOL_SLV_STAT,
174         LDLM_POOL_SHRINK_REQTD_STAT,
175         LDLM_POOL_SHRINK_FREED_STAT,
176         LDLM_POOL_RECALC_STAT,
177         LDLM_POOL_TIMING_STAT,
178         LDLM_POOL_LAST_STAT
179 };
180
181 static inline struct ldlm_namespace *ldlm_pl2ns(struct ldlm_pool *pl)
182 {
183         return container_of(pl, struct ldlm_namespace, ns_pool);
184 }
185
186 /**
187  * Recalculates next grant limit on passed \a pl.
188  *
189  * \pre ->pl_lock is locked. 
190  */
191 static inline void ldlm_pool_recalc_grant_plan(struct ldlm_pool *pl)
192 {
193         int granted, grant_step, limit;
194         
195         limit = ldlm_pool_get_limit(pl);
196         granted = atomic_read(&pl->pl_granted);
197
198         grant_step = ((limit - granted) * pl->pl_grant_step) / 100;
199         pl->pl_grant_plan = granted + grant_step;
200 }
201
202 /**
203  * Recalculates next SLV on passed \a pl.
204  *
205  * \pre ->pl_lock is locked. 
206  */
207 static inline void ldlm_pool_recalc_slv(struct ldlm_pool *pl)
208 {
209         int grant_usage, granted, grant_plan;
210         __u64 slv, slv_factor;
211         __u32 limit;
212
213         slv = pl->pl_server_lock_volume;
214         grant_plan = pl->pl_grant_plan;
215         limit = ldlm_pool_get_limit(pl);
216         granted = atomic_read(&pl->pl_granted);
217
218         grant_usage = limit - (granted - grant_plan);
219         if (grant_usage <= 0)
220                 grant_usage = 1;
221
222         /* 
223          * Find out SLV change factor which is the ratio of grant usage 
224          * from limit. SLV changes as fast as the ratio of grant plan 
225          * consumtion. The more locks from grant plan are not consumed 
226          * by clients in last interval (idle time), the faster grows 
227          * SLV. And the opposite, the more grant plan is over-consumed
228          * (load time) the faster drops SLV. 
229          */
230         slv_factor = (grant_usage * 100) / limit;
231         if (2 * abs(granted - limit) > limit) {
232                 slv_factor *= slv_factor;
233                 slv_factor = dru(slv_factor, 100);
234         }
235         slv = slv * slv_factor;
236         slv = dru(slv, 100);
237
238         if (slv > ldlm_pool_slv_max(limit)) {
239                 slv = ldlm_pool_slv_max(limit);
240         } else if (slv < ldlm_pool_slv_min(limit)) {
241                 slv = ldlm_pool_slv_min(limit);
242         }
243
244         pl->pl_server_lock_volume = slv;
245 }
246
247 /**
248  * Recalculates next stats on passed \a pl.
249  *
250  * \pre ->pl_lock is locked. 
251  */
252 static inline void ldlm_pool_recalc_stats(struct ldlm_pool *pl)
253 {
254         int grant_plan = pl->pl_grant_plan;
255         __u64 slv = pl->pl_server_lock_volume;
256         int granted = atomic_read(&pl->pl_granted);
257         int grant_rate = atomic_read(&pl->pl_grant_rate);
258         int cancel_rate = atomic_read(&pl->pl_cancel_rate);
259
260         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_SLV_STAT, 
261                             slv);
262         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
263                             granted);
264         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
265                             grant_rate);
266         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
267                             grant_plan);
268         lprocfs_counter_add(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
269                             cancel_rate);
270 }
271
272 /**
273  * Sets current SLV into obd accessible via ldlm_pl2ns(pl)->ns_obd.
274  */
275 static void ldlm_srv_pool_push_slv(struct ldlm_pool *pl)
276 {
277         struct obd_device *obd;
278
279         /* 
280          * Set new SLV in obd field for using it later without accessing the
281          * pool. This is required to avoid race between sending reply to client
282          * with new SLV and cleanup server stack in which we can't guarantee
283          * that namespace is still alive. We know only that obd is alive as
284          * long as valid export is alive. 
285          */
286         obd = ldlm_pl2ns(pl)->ns_obd;
287         LASSERT(obd != NULL);
288         write_lock(&obd->obd_pool_lock);
289         obd->obd_pool_slv = pl->pl_server_lock_volume;
290         write_unlock(&obd->obd_pool_lock);
291 }
292
293 /**
294  * Recalculates all pool fields on passed \a pl.
295  *
296  * \pre ->pl_lock is not locked. 
297  */
298 static int ldlm_srv_pool_recalc(struct ldlm_pool *pl)
299 {
300         time_t recalc_interval_sec;
301         ENTRY;
302
303         spin_lock(&pl->pl_lock);
304         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
305         if (recalc_interval_sec > 0) {
306                 /* 
307                  * Update statistics.
308                  */
309                 ldlm_pool_recalc_stats(pl);
310
311                 /* 
312                  * Recalc SLV after last period. This should be done
313                  * _before_ recalculating new grant plan. 
314                  */
315                 ldlm_pool_recalc_slv(pl);
316                 
317                 /* 
318                  * Make sure that pool informed obd of last SLV changes. 
319                  */
320                 ldlm_srv_pool_push_slv(pl);
321
322                 /* 
323                  * Update grant_plan for new period. 
324                  */
325                 ldlm_pool_recalc_grant_plan(pl);
326
327                 /* 
328                  * Zero out all rates and speed for the last period. 
329                  */
330                 atomic_set(&pl->pl_grant_rate, 0);
331                 atomic_set(&pl->pl_cancel_rate, 0);
332                 atomic_set(&pl->pl_grant_speed, 0);
333                 pl->pl_recalc_time = cfs_time_current_sec();
334                 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT, 
335                                     recalc_interval_sec);
336         }
337         spin_unlock(&pl->pl_lock);
338         RETURN(0);
339 }
340
341 /**
342  * This function is used on server side as main entry point for memory
343  * preasure handling. It decreases SLV on \a pl according to passed
344  * \a nr and \a gfp_mask.
345  * 
346  * Our goal here is to decrease SLV such a way that clients hold \a nr
347  * locks smaller in next 10h. 
348  */
349 static int ldlm_srv_pool_shrink(struct ldlm_pool *pl,
350                                 int nr, unsigned int gfp_mask)
351 {
352         __u32 limit;
353         ENTRY;
354
355         /* 
356          * VM is asking how many entries may be potentially freed. 
357          */
358         if (nr == 0)
359                 RETURN(atomic_read(&pl->pl_granted));
360
361         /* 
362          * Client already canceled locks but server is already in shrinker
363          * and can't cancel anything. Let's catch this race. 
364          */
365         if (atomic_read(&pl->pl_granted) == 0)
366                 RETURN(0);
367
368         spin_lock(&pl->pl_lock);
369
370         /* 
371          * We want shrinker to possibly cause cancelation of @nr locks from
372          * clients or grant approximately @nr locks smaller next intervals.
373          *
374          * This is why we decresed SLV by @nr. This effect will only be as
375          * long as one re-calc interval (1s these days) and this should be
376          * enough to pass this decreased SLV to all clients. On next recalc
377          * interval pool will either increase SLV if locks load is not high
378          * or will keep on same level or even decrease again, thus, shrinker
379          * decreased SLV will affect next recalc intervals and this way will
380          * make locking load lower. 
381          */
382         if (nr < pl->pl_server_lock_volume) {
383                 pl->pl_server_lock_volume = pl->pl_server_lock_volume - nr;
384         } else {
385                 limit = ldlm_pool_get_limit(pl);
386                 pl->pl_server_lock_volume = ldlm_pool_slv_min(limit);
387         }
388
389         /* 
390          * Make sure that pool informed obd of last SLV changes. 
391          */
392         ldlm_srv_pool_push_slv(pl);
393         spin_unlock(&pl->pl_lock);
394
395         /* 
396          * We did not really free any memory here so far, it only will be
397          * freed later may be, so that we return 0 to not confuse VM. 
398          */
399         RETURN(0);
400 }
401
402 /**
403  * Setup server side pool \a pl with passed \a limit.
404  */
405 static int ldlm_srv_pool_setup(struct ldlm_pool *pl, int limit)
406 {
407         struct obd_device *obd;
408         ENTRY;
409         
410         obd = ldlm_pl2ns(pl)->ns_obd;
411         LASSERT(obd != NULL && obd != LP_POISON);
412         LASSERT(obd->obd_type != LP_POISON);
413         write_lock(&obd->obd_pool_lock);
414         obd->obd_pool_limit = limit;
415         write_unlock(&obd->obd_pool_lock);
416
417         ldlm_pool_set_limit(pl, limit);
418         RETURN(0);
419 }
420
421 /**
422  * Sets SLV and Limit from ldlm_pl2ns(pl)->ns_obd tp passed \a pl.
423  */
424 static void ldlm_cli_pool_pop_slv(struct ldlm_pool *pl)
425 {
426         struct obd_device *obd;
427
428         /* 
429          * Get new SLV and Limit from obd which is updated with comming 
430          * RPCs. 
431          */
432         obd = ldlm_pl2ns(pl)->ns_obd;
433         LASSERT(obd != NULL);
434         read_lock(&obd->obd_pool_lock);
435         pl->pl_server_lock_volume = obd->obd_pool_slv;
436         ldlm_pool_set_limit(pl, obd->obd_pool_limit);
437         read_unlock(&obd->obd_pool_lock);
438 }
439
440 /**
441  * Recalculates client sise pool \a pl according to current SLV and Limit.
442  */
443 static int ldlm_cli_pool_recalc(struct ldlm_pool *pl)
444 {
445         time_t recalc_interval_sec;
446         ENTRY;
447
448         spin_lock(&pl->pl_lock);
449
450         /* 
451          * Make sure that pool knows last SLV and Limit from obd. 
452          */
453         ldlm_cli_pool_pop_slv(pl);
454
455         recalc_interval_sec = cfs_time_current_sec() - pl->pl_recalc_time;
456         if (recalc_interval_sec > 0) {
457                 /* 
458                  * Update statistics only every T. 
459                  */
460                 ldlm_pool_recalc_stats(pl);
461
462                 /* 
463                  * Zero out grant/cancel rates and speed for last period. 
464                  */
465                 atomic_set(&pl->pl_grant_rate, 0);
466                 atomic_set(&pl->pl_cancel_rate, 0);
467                 atomic_set(&pl->pl_grant_speed, 0);
468                 pl->pl_recalc_time = cfs_time_current_sec();
469                 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_TIMING_STAT, 
470                                     recalc_interval_sec);
471         }
472         spin_unlock(&pl->pl_lock);
473
474         /* 
475          * Do not cancel locks in case lru resize is disabled for this ns. 
476          */
477         if (!ns_connect_lru_resize(ldlm_pl2ns(pl)))
478                 RETURN(0);
479
480         /* 
481          * In the time of canceling locks on client we do not need to maintain
482          * sharp timing, we only want to cancel locks asap according to new SLV.
483          * It may be called when SLV has changed much, this is why we do not
484          * take into account pl->pl_recalc_time here. 
485          */
486         RETURN(ldlm_cancel_lru(ldlm_pl2ns(pl), 0, LDLM_ASYNC, 
487                                LDLM_CANCEL_LRUR));
488 }
489
490 /**
491  * This function is main entry point for memory preasure handling on client side.
492  * Main goal of this function is to cancel some number of locks on passed \a pl
493  * according to \a nr and \a gfp_mask.
494  */
495 static int ldlm_cli_pool_shrink(struct ldlm_pool *pl,
496                                 int nr, unsigned int gfp_mask)
497 {
498         ENTRY;
499         
500         /* 
501          * Do not cancel locks in case lru resize is disabled for this ns. 
502          */
503         if (!ns_connect_lru_resize(ldlm_pl2ns(pl)))
504                 RETURN(0);
505
506         /* 
507          * Make sure that pool knows last SLV and Limit from obd. 
508          */
509         ldlm_cli_pool_pop_slv(pl);
510
511         /* 
512          * Find out how many locks may be released according to shrink 
513          * policy. 
514          */
515         if (nr == 0)
516                 RETURN(ldlm_cancel_lru_estimate(ldlm_pl2ns(pl), 0, 0, 
517                                                 LDLM_CANCEL_SHRINK));
518
519         /* 
520          * Cancel @nr locks accoding to shrink policy. 
521          */
522         RETURN(ldlm_cancel_lru(ldlm_pl2ns(pl), nr, LDLM_SYNC, 
523                                LDLM_CANCEL_SHRINK));
524 }
525
526 struct ldlm_pool_ops ldlm_srv_pool_ops = {
527         .po_recalc = ldlm_srv_pool_recalc,
528         .po_shrink = ldlm_srv_pool_shrink,
529         .po_setup  = ldlm_srv_pool_setup
530 };
531
532 struct ldlm_pool_ops ldlm_cli_pool_ops = {
533         .po_recalc = ldlm_cli_pool_recalc,
534         .po_shrink = ldlm_cli_pool_shrink
535 };
536
537 /**
538  * Pool recalc wrapper. Will call either client or server pool recalc callback
539  * depending what pool \a pl is used.
540  */
541 int ldlm_pool_recalc(struct ldlm_pool *pl)
542 {
543         int count;
544
545         if (pl->pl_ops->po_recalc != NULL) {
546                 count = pl->pl_ops->po_recalc(pl);
547                 lprocfs_counter_add(pl->pl_stats, LDLM_POOL_RECALC_STAT, 
548                                     count);
549                 return count;
550         }
551         return 0;
552 }
553 EXPORT_SYMBOL(ldlm_pool_recalc);
554
555 /**
556  * Pool shrink wrapper. Will call either client or server pool recalc callback
557  * depending what pool \a pl is used.
558  */
559 int ldlm_pool_shrink(struct ldlm_pool *pl, int nr,
560                      unsigned int gfp_mask)
561 {
562         int cancel = 0;
563         
564         if (pl->pl_ops->po_shrink != NULL) {
565                 cancel = pl->pl_ops->po_shrink(pl, nr, gfp_mask);
566                 if (nr > 0) {
567                         lprocfs_counter_add(pl->pl_stats, 
568                                             LDLM_POOL_SHRINK_REQTD_STAT,
569                                             nr);
570                         lprocfs_counter_add(pl->pl_stats, 
571                                             LDLM_POOL_SHRINK_FREED_STAT,
572                                             cancel);
573                         CDEBUG(D_DLMTRACE, "%s: request to shrink %d locks, "
574                                "shrunk %d\n", pl->pl_name, nr, cancel);
575                 }
576         }
577         return cancel;
578 }
579 EXPORT_SYMBOL(ldlm_pool_shrink);
580
581 /**
582  * Pool setup wrapper. Will call either client or server pool recalc callback
583  * depending what pool \a pl is used.
584  *
585  * Sets passed \a limit into pool \a pl.
586  */
587 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
588 {
589         ENTRY;
590         if (pl->pl_ops->po_setup != NULL)
591                 RETURN(pl->pl_ops->po_setup(pl, limit));
592         RETURN(0);
593 }
594 EXPORT_SYMBOL(ldlm_pool_setup);
595
596 #ifdef __KERNEL__
597 static int lprocfs_rd_pool_state(char *page, char **start, off_t off,
598                                  int count, int *eof, void *data)
599 {
600         int granted, grant_rate, cancel_rate, grant_step;
601         int nr = 0, grant_speed, grant_plan;
602         struct ldlm_pool *pl = data;
603         __u64 slv, clv;
604         __u32 limit;
605
606         spin_lock(&pl->pl_lock);
607         slv = pl->pl_server_lock_volume;
608         clv = pl->pl_client_lock_volume;
609         limit = ldlm_pool_get_limit(pl);
610         grant_plan = pl->pl_grant_plan;
611         grant_step = pl->pl_grant_step;
612         granted = atomic_read(&pl->pl_granted);
613         grant_rate = atomic_read(&pl->pl_grant_rate);
614         grant_speed = atomic_read(&pl->pl_grant_speed);
615         cancel_rate = atomic_read(&pl->pl_cancel_rate);
616         spin_unlock(&pl->pl_lock);
617
618         nr += snprintf(page + nr, count - nr, "LDLM pool state (%s):\n",
619                        pl->pl_name);
620         nr += snprintf(page + nr, count - nr, "  SLV: "LPU64"\n", slv);
621         nr += snprintf(page + nr, count - nr, "  CLV: "LPU64"\n", clv);
622
623         nr += snprintf(page + nr, count - nr, "  LVF: %d\n",
624                        atomic_read(&pl->pl_lock_volume_factor));
625
626         nr += snprintf(page + nr, count - nr, "  GSP: %d%%\n",
627                        grant_step);
628         nr += snprintf(page + nr, count - nr, "  GP:  %d\n",
629                        grant_plan);
630         nr += snprintf(page + nr, count - nr, "  GR:  %d\n",
631                        grant_rate);
632         nr += snprintf(page + nr, count - nr, "  CR:  %d\n",
633                        cancel_rate);
634         nr += snprintf(page + nr, count - nr, "  GS:  %d\n",
635                        grant_speed);
636         nr += snprintf(page + nr, count - nr, "  G:   %d\n",
637                        granted);
638         nr += snprintf(page + nr, count - nr, "  L:   %d\n",
639                        limit);
640         return nr;
641 }
642
643 LDLM_POOL_PROC_READER(grant_plan, int);
644 LDLM_POOL_PROC_READER(grant_step, int);
645 LDLM_POOL_PROC_WRITER(grant_step, int);
646
647 static int ldlm_pool_proc_init(struct ldlm_pool *pl)
648 {
649         struct ldlm_namespace *ns = ldlm_pl2ns(pl);
650         struct proc_dir_entry *parent_ns_proc;
651         struct lprocfs_vars pool_vars[2];
652         char *var_name = NULL;
653         int rc = 0;
654         ENTRY;
655
656         OBD_ALLOC(var_name, MAX_STRING_SIZE + 1);
657         if (!var_name)
658                 RETURN(-ENOMEM);
659
660         parent_ns_proc = lprocfs_srch(ldlm_ns_proc_dir, ns->ns_name);
661         if (parent_ns_proc == NULL) {
662                 CERROR("%s: proc entry is not initialized\n",
663                        ns->ns_name);
664                 GOTO(out_free_name, rc = -EINVAL);
665         }
666         pl->pl_proc_dir = lprocfs_register("pool", parent_ns_proc,
667                                            NULL, NULL);
668         if (IS_ERR(pl->pl_proc_dir)) {
669                 CERROR("LProcFS failed in ldlm-pool-init\n");
670                 rc = PTR_ERR(pl->pl_proc_dir);
671                 GOTO(out_free_name, rc);
672         }
673
674         var_name[MAX_STRING_SIZE] = '\0';
675         memset(pool_vars, 0, sizeof(pool_vars));
676         pool_vars[0].name = var_name;
677
678         snprintf(var_name, MAX_STRING_SIZE, "server_lock_volume");
679         pool_vars[0].data = &pl->pl_server_lock_volume;
680         pool_vars[0].read_fptr = lprocfs_rd_u64;
681         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
682
683         snprintf(var_name, MAX_STRING_SIZE, "limit");
684         pool_vars[0].data = &pl->pl_limit;
685         pool_vars[0].read_fptr = lprocfs_rd_atomic;
686         pool_vars[0].write_fptr = lprocfs_wr_atomic;
687         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
688
689         snprintf(var_name, MAX_STRING_SIZE, "granted");
690         pool_vars[0].data = &pl->pl_granted;
691         pool_vars[0].read_fptr = lprocfs_rd_atomic;
692         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
693
694         snprintf(var_name, MAX_STRING_SIZE, "grant_speed");
695         pool_vars[0].data = &pl->pl_grant_speed;
696         pool_vars[0].read_fptr = lprocfs_rd_atomic;
697         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
698
699         snprintf(var_name, MAX_STRING_SIZE, "cancel_rate");
700         pool_vars[0].data = &pl->pl_cancel_rate;
701         pool_vars[0].read_fptr = lprocfs_rd_atomic;
702         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
703
704         snprintf(var_name, MAX_STRING_SIZE, "grant_rate");
705         pool_vars[0].data = &pl->pl_grant_rate;
706         pool_vars[0].read_fptr = lprocfs_rd_atomic;
707         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
708
709         snprintf(var_name, MAX_STRING_SIZE, "grant_plan");
710         pool_vars[0].data = pl;
711         pool_vars[0].read_fptr = lprocfs_rd_grant_plan;
712         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
713
714         snprintf(var_name, MAX_STRING_SIZE, "grant_step");
715         pool_vars[0].data = pl;
716         pool_vars[0].read_fptr = lprocfs_rd_grant_step;
717         if (ns_is_server(ns))
718                 pool_vars[0].write_fptr = lprocfs_wr_grant_step;
719         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
720
721         snprintf(var_name, MAX_STRING_SIZE, "lock_volume_factor");
722         pool_vars[0].data = &pl->pl_lock_volume_factor;
723         pool_vars[0].read_fptr = lprocfs_rd_atomic;
724         pool_vars[0].write_fptr = lprocfs_wr_atomic;
725         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
726
727         snprintf(var_name, MAX_STRING_SIZE, "state");
728         pool_vars[0].data = pl;
729         pool_vars[0].read_fptr = lprocfs_rd_pool_state;
730         lprocfs_add_vars(pl->pl_proc_dir, pool_vars, 0);
731
732         pl->pl_stats = lprocfs_alloc_stats(LDLM_POOL_LAST_STAT -
733                                            LDLM_POOL_FIRST_STAT, 0);
734         if (!pl->pl_stats)
735                 GOTO(out_free_name, rc = -ENOMEM);
736
737         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANTED_STAT,
738                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
739                              "granted", "locks");
740         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_STAT, 
741                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
742                              "grant", "locks");
743         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_STAT, 
744                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
745                              "cancel", "locks");
746         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_RATE_STAT,
747                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
748                              "grant_rate", "locks/s");
749         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_CANCEL_RATE_STAT,
750                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
751                              "cancel_rate", "locks/s");
752         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_GRANT_PLAN_STAT,
753                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
754                              "grant_plan", "locks/s");
755         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SLV_STAT,
756                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
757                              "slv", "slv");
758         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_REQTD_STAT,
759                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
760                              "shrink_request", "locks");
761         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_SHRINK_FREED_STAT,
762                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
763                              "shrink_freed", "locks");
764         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_RECALC_STAT,
765                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
766                              "recalc_freed", "locks");
767         lprocfs_counter_init(pl->pl_stats, LDLM_POOL_TIMING_STAT,
768                              LPROCFS_CNTR_AVGMINMAX | LPROCFS_CNTR_STDDEV,
769                              "recalc_timing", "sec");
770         lprocfs_register_stats(pl->pl_proc_dir, "stats", pl->pl_stats);
771
772         EXIT;
773 out_free_name:
774         OBD_FREE(var_name, MAX_STRING_SIZE + 1);
775         return rc;
776 }
777
778 static void ldlm_pool_proc_fini(struct ldlm_pool *pl)
779 {
780         if (pl->pl_stats != NULL) {
781                 lprocfs_free_stats(&pl->pl_stats);
782                 pl->pl_stats = NULL;
783         }
784         if (pl->pl_proc_dir != NULL) {
785                 lprocfs_remove(&pl->pl_proc_dir);
786                 pl->pl_proc_dir = NULL;
787         }
788 }
789 #else /* !__KERNEL__*/
790 #define ldlm_pool_proc_init(pl) (0)
791 #define ldlm_pool_proc_fini(pl) while (0) {}
792 #endif
793
794 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
795                    int idx, ldlm_side_t client)
796 {
797         int rc;
798         ENTRY;
799
800         spin_lock_init(&pl->pl_lock);
801         atomic_set(&pl->pl_granted, 0);
802         pl->pl_recalc_time = cfs_time_current_sec();
803         atomic_set(&pl->pl_lock_volume_factor, 1);
804
805         atomic_set(&pl->pl_grant_rate, 0);
806         atomic_set(&pl->pl_cancel_rate, 0);
807         atomic_set(&pl->pl_grant_speed, 0);
808         pl->pl_grant_step = LDLM_POOL_GSP;
809         pl->pl_grant_plan = LDLM_POOL_GP(LDLM_POOL_HOST_L);
810
811         snprintf(pl->pl_name, sizeof(pl->pl_name), "ldlm-pool-%s-%d",
812                  ns->ns_name, idx);
813
814         if (client == LDLM_NAMESPACE_SERVER) {
815                 pl->pl_ops = &ldlm_srv_pool_ops;
816                 ldlm_pool_set_limit(pl, LDLM_POOL_HOST_L);
817                 pl->pl_server_lock_volume = ldlm_pool_slv_max(LDLM_POOL_HOST_L);
818         } else {
819                 pl->pl_server_lock_volume = 1;
820                 ldlm_pool_set_limit(pl, 1);
821                 pl->pl_ops = &ldlm_cli_pool_ops;
822         }
823         pl->pl_client_lock_volume = 0;
824         rc = ldlm_pool_proc_init(pl);
825         if (rc)
826                 RETURN(rc);
827
828         CDEBUG(D_DLMTRACE, "Lock pool %s is initialized\n", pl->pl_name);
829
830         RETURN(rc);
831 }
832 EXPORT_SYMBOL(ldlm_pool_init);
833
834 void ldlm_pool_fini(struct ldlm_pool *pl)
835 {
836         ENTRY;
837         ldlm_pool_proc_fini(pl);
838         
839         /* 
840          * Pool should not be used after this point. We can't free it here as
841          * it lives in struct ldlm_namespace, but still interested in catching
842          * any abnormal using cases.
843          */
844         POISON(pl, 0x5a, sizeof(*pl));
845         EXIT;
846 }
847 EXPORT_SYMBOL(ldlm_pool_fini);
848
849 /**
850  * Add new taken ldlm lock \a lock into pool \a pl accounting.
851  */
852 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
853 {
854         /* 
855          * FLOCK locks are special in a sense that they are almost never
856          * cancelled, instead special kind of lock is used to drop them.
857          * also there is no LRU for flock locks, so no point in tracking
858          * them anyway. 
859          */
860         if (lock->l_resource->lr_type == LDLM_FLOCK)
861                 return;
862
863         ENTRY;
864                 
865         atomic_inc(&pl->pl_granted);
866         atomic_inc(&pl->pl_grant_rate);
867         atomic_inc(&pl->pl_grant_speed);
868
869         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_GRANT_STAT);
870  
871         /* 
872          * Do not do pool recalc for client side as all locks which
873          * potentially may be canceled has already been packed into 
874          * enqueue/cancel rpc. Also we do not want to run out of stack
875          * with too long call paths. 
876          */
877         if (ns_is_server(ldlm_pl2ns(pl)))
878                 ldlm_pool_recalc(pl);
879         EXIT;
880 }
881 EXPORT_SYMBOL(ldlm_pool_add);
882
883 /**
884  * Remove ldlm lock \a lock from pool \a pl accounting.
885  */
886 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
887 {
888         /*
889          * Filter out FLOCK locks. Read above comment in ldlm_pool_add().
890          */
891         if (lock->l_resource->lr_type == LDLM_FLOCK)
892                 return;
893         ENTRY;
894         LASSERT(atomic_read(&pl->pl_granted) > 0);
895         atomic_dec(&pl->pl_granted);
896         atomic_inc(&pl->pl_cancel_rate);
897         atomic_dec(&pl->pl_grant_speed);
898         
899         lprocfs_counter_incr(pl->pl_stats, LDLM_POOL_CANCEL_STAT);
900
901         if (ns_is_server(ldlm_pl2ns(pl)))
902                 ldlm_pool_recalc(pl);
903         EXIT;
904 }
905 EXPORT_SYMBOL(ldlm_pool_del);
906
907 /**
908  * Returns current \a pl SLV.
909  *
910  * \pre ->pl_lock is not locked. 
911  */
912 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
913 {
914         __u64 slv;
915         spin_lock(&pl->pl_lock);
916         slv = pl->pl_server_lock_volume;
917         spin_unlock(&pl->pl_lock);
918         return slv;
919 }
920 EXPORT_SYMBOL(ldlm_pool_get_slv);
921
922 /**
923  * Sets passed \a slv to \a pl.
924  *
925  * \pre ->pl_lock is not locked. 
926  */
927 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
928 {
929         spin_lock(&pl->pl_lock);
930         pl->pl_server_lock_volume = slv;
931         spin_unlock(&pl->pl_lock);
932 }
933 EXPORT_SYMBOL(ldlm_pool_set_slv);
934
935 /**
936  * Returns current \a pl CLV.
937  *
938  * \pre ->pl_lock is not locked. 
939  */
940 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
941 {
942         __u64 slv;
943         spin_lock(&pl->pl_lock);
944         slv = pl->pl_client_lock_volume;
945         spin_unlock(&pl->pl_lock);
946         return slv;
947 }
948 EXPORT_SYMBOL(ldlm_pool_get_clv);
949
950 /**
951  * Sets passed \a clv to \a pl.
952  *
953  * \pre ->pl_lock is not locked. 
954  */
955 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
956 {
957         spin_lock(&pl->pl_lock);
958         pl->pl_client_lock_volume = clv;
959         spin_unlock(&pl->pl_lock);
960 }
961 EXPORT_SYMBOL(ldlm_pool_set_clv);
962
963 /**
964  * Returns current \a pl limit.
965  */
966 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
967 {
968         return atomic_read(&pl->pl_limit);
969 }
970 EXPORT_SYMBOL(ldlm_pool_get_limit);
971
972 /**
973  * Sets passed \a limit to \a pl.
974  */
975 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
976 {
977         atomic_set(&pl->pl_limit, limit);
978 }
979 EXPORT_SYMBOL(ldlm_pool_set_limit);
980
981 /**
982  * Returns current LVF from \a pl.
983  */
984 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
985 {
986         return atomic_read(&pl->pl_lock_volume_factor);
987 }
988 EXPORT_SYMBOL(ldlm_pool_get_lvf);
989
990 #ifdef __KERNEL__
991 static int ldlm_pool_granted(struct ldlm_pool *pl)
992 {
993         return atomic_read(&pl->pl_granted);
994 }
995
996 static struct ptlrpc_thread *ldlm_pools_thread;
997 static struct shrinker *ldlm_pools_srv_shrinker;
998 static struct shrinker *ldlm_pools_cli_shrinker;
999 static struct completion ldlm_pools_comp;
1000
1001 void ldlm_pools_wakeup(void)
1002 {
1003         ENTRY;
1004         if (ldlm_pools_thread == NULL)
1005                 return;
1006         ldlm_pools_thread->t_flags |= SVC_EVENT;
1007         cfs_waitq_signal(&ldlm_pools_thread->t_ctl_waitq);
1008         EXIT;
1009 }
1010 EXPORT_SYMBOL(ldlm_pools_wakeup);
1011
1012 /* 
1013  * Cancel \a nr locks from all namespaces (if possible). Returns number of
1014  * cached locks after shrink is finished. All namespaces are asked to
1015  * cancel approximately equal amount of locks to keep balancing.
1016  */
1017 static int ldlm_pools_shrink(ldlm_side_t client, int nr, 
1018                              unsigned int gfp_mask)
1019 {
1020         int total = 0, cached = 0, nr_ns;
1021         struct ldlm_namespace *ns;
1022
1023         if (nr != 0 && !(gfp_mask & __GFP_FS))
1024                 return -1;
1025
1026         CDEBUG(D_DLMTRACE, "Request to shrink %d %s locks from all pools\n",
1027                nr, client == LDLM_NAMESPACE_CLIENT ? "client" : "server");
1028
1029         /* 
1030          * Find out how many resources we may release. 
1031          */
1032         for (nr_ns = atomic_read(ldlm_namespace_nr(client)); 
1033              nr_ns > 0; nr_ns--) 
1034         {
1035                 mutex_down(ldlm_namespace_lock(client));
1036                 if (list_empty(ldlm_namespace_list(client))) {
1037                         mutex_up(ldlm_namespace_lock(client));
1038                         return 0;
1039                 }
1040                 ns = ldlm_namespace_first_locked(client);
1041                 ldlm_namespace_get(ns);
1042                 ldlm_namespace_move_locked(ns, client);
1043                 mutex_up(ldlm_namespace_lock(client));
1044                 total += ldlm_pool_shrink(&ns->ns_pool, 0, gfp_mask);
1045                 ldlm_namespace_put(ns, 1);
1046         }
1047  
1048         if (nr == 0 || total == 0)
1049                 return total;
1050
1051         /* 
1052          * Shrink at least ldlm_namespace_nr(client) namespaces. 
1053          */
1054         for (nr_ns = atomic_read(ldlm_namespace_nr(client)); 
1055              nr_ns > 0; nr_ns--) 
1056         {
1057                 int cancel, nr_locks;
1058
1059                 /* 
1060                  * Do not call shrink under ldlm_namespace_lock(client) 
1061                  */
1062                 mutex_down(ldlm_namespace_lock(client));
1063                 if (list_empty(ldlm_namespace_list(client))) {
1064                         mutex_up(ldlm_namespace_lock(client));
1065                         /* 
1066                          * If list is empty, we can't return any @cached > 0,
1067                          * that probably would cause needless shrinker
1068                          * call. 
1069                          */
1070                         cached = 0;
1071                         break;
1072                 }
1073                 ns = ldlm_namespace_first_locked(client);
1074                 ldlm_namespace_get(ns);
1075                 ldlm_namespace_move_locked(ns, client);
1076                 mutex_up(ldlm_namespace_lock(client));
1077                 
1078                 nr_locks = ldlm_pool_granted(&ns->ns_pool);
1079                 cancel = 1 + nr_locks * nr / total;
1080                 ldlm_pool_shrink(&ns->ns_pool, cancel, gfp_mask);
1081                 cached += ldlm_pool_granted(&ns->ns_pool);
1082                 ldlm_namespace_put(ns, 1);
1083         }
1084         return cached;
1085 }
1086
1087 static int ldlm_pools_srv_shrink(int nr, unsigned int gfp_mask)
1088 {
1089         return ldlm_pools_shrink(LDLM_NAMESPACE_SERVER, nr, gfp_mask);
1090 }
1091
1092 static int ldlm_pools_cli_shrink(int nr, unsigned int gfp_mask)
1093 {
1094         return ldlm_pools_shrink(LDLM_NAMESPACE_CLIENT, nr, gfp_mask);
1095 }
1096
1097 void ldlm_pools_recalc(ldlm_side_t client)
1098 {
1099         __u32 nr_l = 0, nr_p = 0, l;
1100         struct ldlm_namespace *ns;
1101         int nr, equal = 0;
1102
1103         /* 
1104          * No need to setup pool limit for client pools.
1105          */
1106         if (client == LDLM_NAMESPACE_SERVER) {
1107                 /* 
1108                  * Check all modest namespaces first. 
1109                  */
1110                 mutex_down(ldlm_namespace_lock(client));
1111                 list_for_each_entry(ns, ldlm_namespace_list(client), 
1112                                     ns_list_chain) 
1113                 {
1114                         if (ns->ns_appetite != LDLM_NAMESPACE_MODEST)
1115                                 continue;
1116
1117                         l = ldlm_pool_granted(&ns->ns_pool);
1118                         if (l == 0)
1119                                 l = 1;
1120
1121                         /* 
1122                          * Set the modest pools limit equal to their avg granted
1123                          * locks + 5%. 
1124                          */
1125                         l += dru(l * LDLM_POOLS_MODEST_MARGIN, 100);
1126                         ldlm_pool_setup(&ns->ns_pool, l);
1127                         nr_l += l;
1128                         nr_p++;
1129                 }
1130
1131                 /* 
1132                  * Make sure that modest namespaces did not eat more that 2/3 
1133                  * of limit. 
1134                  */
1135                 if (nr_l >= 2 * (LDLM_POOL_HOST_L / 3)) {
1136                         CWARN("\"Modest\" pools eat out 2/3 of server locks "
1137                               "limit (%d of %lu). This means that you have too "
1138                               "many clients for this amount of server RAM. "
1139                               "Upgrade server!\n", nr_l, LDLM_POOL_HOST_L);
1140                         equal = 1;
1141                 }
1142
1143                 /* 
1144                  * The rest is given to greedy namespaces. 
1145                  */
1146                 list_for_each_entry(ns, ldlm_namespace_list(client), 
1147                                     ns_list_chain) 
1148                 {
1149                         if (!equal && ns->ns_appetite != LDLM_NAMESPACE_GREEDY)
1150                                 continue;
1151
1152                         if (equal) {
1153                                 /* 
1154                                  * In the case 2/3 locks are eaten out by
1155                                  * modest pools, we re-setup equal limit
1156                                  * for _all_ pools. 
1157                                  */
1158                                 l = LDLM_POOL_HOST_L /
1159                                         atomic_read(ldlm_namespace_nr(client));
1160                         } else {
1161                                 /* 
1162                                  * All the rest of greedy pools will have
1163                                  * all locks in equal parts.
1164                                  */
1165                                 l = (LDLM_POOL_HOST_L - nr_l) /
1166                                         (atomic_read(ldlm_namespace_nr(client)) -
1167                                          nr_p);
1168                         }
1169                         ldlm_pool_setup(&ns->ns_pool, l);
1170                 }
1171                 mutex_up(ldlm_namespace_lock(client));
1172         }
1173
1174         /* 
1175          * Recalc at least ldlm_namespace_nr(client) namespaces. 
1176          */
1177         for (nr = atomic_read(ldlm_namespace_nr(client)); nr > 0; nr--) {
1178                 /* 
1179                  * Lock the list, get first @ns in the list, getref, move it
1180                  * to the tail, unlock and call pool recalc. This way we avoid
1181                  * calling recalc under @ns lock what is really good as we get
1182                  * rid of potential deadlock on client nodes when canceling
1183                  * locks synchronously. 
1184                  */
1185                 mutex_down(ldlm_namespace_lock(client));
1186                 if (list_empty(ldlm_namespace_list(client))) {
1187                         mutex_up(ldlm_namespace_lock(client));
1188                         break;
1189                 }
1190                 ns = ldlm_namespace_first_locked(client);
1191                 ldlm_namespace_get(ns);
1192                 ldlm_namespace_move_locked(ns, client);
1193                 mutex_up(ldlm_namespace_lock(client));
1194
1195                 /* 
1196                  * After setup is done - recalc the pool. 
1197                  */
1198                 ldlm_pool_recalc(&ns->ns_pool);
1199                 ldlm_namespace_put(ns, 1);
1200         }
1201 }
1202 EXPORT_SYMBOL(ldlm_pools_recalc);
1203
1204 static int ldlm_pools_thread_main(void *arg)
1205 {
1206         struct ptlrpc_thread *thread = (struct ptlrpc_thread *)arg;
1207         char *t_name = "ldlm_poold";
1208         ENTRY;
1209
1210         cfs_daemonize(t_name);
1211         thread->t_flags = SVC_RUNNING;
1212         cfs_waitq_signal(&thread->t_ctl_waitq);
1213
1214         CDEBUG(D_DLMTRACE, "%s: pool thread starting, process %d\n",
1215                t_name, cfs_curproc_pid());
1216
1217         while (1) {
1218                 struct l_wait_info lwi;
1219
1220                 /*
1221                  * Recal all pools on this tick. 
1222                  */
1223                 ldlm_pools_recalc(LDLM_NAMESPACE_SERVER);
1224                 ldlm_pools_recalc(LDLM_NAMESPACE_CLIENT);
1225                 
1226                 /*
1227                  * Wait until the next check time, or until we're
1228                  * stopped. 
1229                  */
1230                 lwi = LWI_TIMEOUT(cfs_time_seconds(LDLM_POOLS_THREAD_PERIOD),
1231                                   NULL, NULL);
1232                 l_wait_event(thread->t_ctl_waitq, (thread->t_flags &
1233                                                    (SVC_STOPPING|SVC_EVENT)),
1234                              &lwi);
1235
1236                 if (thread->t_flags & SVC_STOPPING) {
1237                         thread->t_flags &= ~SVC_STOPPING;
1238                         break;
1239                 } else if (thread->t_flags & SVC_EVENT) {
1240                         thread->t_flags &= ~SVC_EVENT;
1241                 }
1242         }
1243
1244         thread->t_flags = SVC_STOPPED;
1245         cfs_waitq_signal(&thread->t_ctl_waitq);
1246
1247         CDEBUG(D_DLMTRACE, "%s: pool thread exiting, process %d\n",
1248                t_name, cfs_curproc_pid());
1249
1250         complete_and_exit(&ldlm_pools_comp, 0);
1251 }
1252
1253 static int ldlm_pools_thread_start(void)
1254 {
1255         struct l_wait_info lwi = { 0 };
1256         int rc;
1257         ENTRY;
1258
1259         if (ldlm_pools_thread != NULL)
1260                 RETURN(-EALREADY);
1261
1262         OBD_ALLOC_PTR(ldlm_pools_thread);
1263         if (ldlm_pools_thread == NULL)
1264                 RETURN(-ENOMEM);
1265
1266         init_completion(&ldlm_pools_comp);
1267         cfs_waitq_init(&ldlm_pools_thread->t_ctl_waitq);
1268
1269         /* 
1270          * CLONE_VM and CLONE_FILES just avoid a needless copy, because we
1271          * just drop the VM and FILES in ptlrpc_daemonize() right away. 
1272          */
1273         rc = cfs_kernel_thread(ldlm_pools_thread_main, ldlm_pools_thread,
1274                                CLONE_VM | CLONE_FILES);
1275         if (rc < 0) {
1276                 CERROR("Can't start pool thread, error %d\n",
1277                        rc);
1278                 OBD_FREE(ldlm_pools_thread, sizeof(*ldlm_pools_thread));
1279                 ldlm_pools_thread = NULL;
1280                 RETURN(rc);
1281         }
1282         l_wait_event(ldlm_pools_thread->t_ctl_waitq,
1283                      (ldlm_pools_thread->t_flags & SVC_RUNNING), &lwi);
1284         RETURN(0);
1285 }
1286
1287 static void ldlm_pools_thread_stop(void)
1288 {
1289         ENTRY;
1290
1291         if (ldlm_pools_thread == NULL) {
1292                 EXIT;
1293                 return;
1294         }
1295
1296         ldlm_pools_thread->t_flags = SVC_STOPPING;
1297         cfs_waitq_signal(&ldlm_pools_thread->t_ctl_waitq);
1298
1299         /* 
1300          * Make sure that pools thread is finished before freeing @thread.
1301          * This fixes possible race and oops due to accessing freed memory
1302          * in pools thread. 
1303          */
1304         wait_for_completion(&ldlm_pools_comp);
1305         OBD_FREE_PTR(ldlm_pools_thread);
1306         ldlm_pools_thread = NULL;
1307         EXIT;
1308 }
1309
1310 int ldlm_pools_init(void)
1311 {
1312         int rc;
1313         ENTRY;
1314
1315         rc = ldlm_pools_thread_start();
1316         if (rc == 0) {
1317                 ldlm_pools_srv_shrinker = set_shrinker(DEFAULT_SEEKS,
1318                                                        ldlm_pools_srv_shrink);
1319                 ldlm_pools_cli_shrinker = set_shrinker(DEFAULT_SEEKS,
1320                                                        ldlm_pools_cli_shrink);
1321         }
1322         RETURN(rc);
1323 }
1324 EXPORT_SYMBOL(ldlm_pools_init);
1325
1326 void ldlm_pools_fini(void)
1327 {
1328         if (ldlm_pools_srv_shrinker != NULL) {
1329                 remove_shrinker(ldlm_pools_srv_shrinker);
1330                 ldlm_pools_srv_shrinker = NULL;
1331         }
1332         if (ldlm_pools_cli_shrinker != NULL) {
1333                 remove_shrinker(ldlm_pools_cli_shrinker);
1334                 ldlm_pools_cli_shrinker = NULL;
1335         }
1336         ldlm_pools_thread_stop();
1337 }
1338 EXPORT_SYMBOL(ldlm_pools_fini);
1339 #endif /* __KERNEL__ */
1340
1341 #else /* !HAVE_LRU_RESIZE_SUPPORT */
1342 int ldlm_pool_setup(struct ldlm_pool *pl, int limit)
1343 {
1344         return 0;
1345 }
1346 EXPORT_SYMBOL(ldlm_pool_setup);
1347
1348 int ldlm_pool_recalc(struct ldlm_pool *pl)
1349 {
1350         return 0;
1351 }
1352 EXPORT_SYMBOL(ldlm_pool_recalc);
1353
1354 int ldlm_pool_shrink(struct ldlm_pool *pl,
1355                      int nr, unsigned int gfp_mask)
1356 {
1357         return 0;
1358 }
1359 EXPORT_SYMBOL(ldlm_pool_shrink);
1360
1361 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
1362                    int idx, ldlm_side_t client)
1363 {
1364         return 0;
1365 }
1366 EXPORT_SYMBOL(ldlm_pool_init);
1367
1368 void ldlm_pool_fini(struct ldlm_pool *pl)
1369 {
1370         return;
1371 }
1372 EXPORT_SYMBOL(ldlm_pool_fini);
1373
1374 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock)
1375 {
1376         return;
1377 }
1378 EXPORT_SYMBOL(ldlm_pool_add);
1379
1380 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock)
1381 {
1382         return;
1383 }
1384 EXPORT_SYMBOL(ldlm_pool_del);
1385
1386 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl)
1387 {
1388         return 1;
1389 }
1390 EXPORT_SYMBOL(ldlm_pool_get_slv);
1391
1392 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv)
1393 {
1394         return;
1395 }
1396 EXPORT_SYMBOL(ldlm_pool_set_slv);
1397
1398 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl)
1399 {
1400         return 1;
1401 }
1402 EXPORT_SYMBOL(ldlm_pool_get_clv);
1403
1404 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv)
1405 {
1406         return;
1407 }
1408 EXPORT_SYMBOL(ldlm_pool_set_clv);
1409
1410 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl)
1411 {
1412         return 0;
1413 }
1414 EXPORT_SYMBOL(ldlm_pool_get_limit);
1415
1416 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit)
1417 {
1418         return;
1419 }
1420 EXPORT_SYMBOL(ldlm_pool_set_limit);
1421
1422 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl)
1423 {
1424         return 0;
1425 }
1426 EXPORT_SYMBOL(ldlm_pool_get_lvf);
1427
1428 int ldlm_pools_init(void)
1429 {
1430         return 0;
1431 }
1432 EXPORT_SYMBOL(ldlm_pools_init);
1433
1434 void ldlm_pools_fini(void)
1435 {
1436         return;
1437 }
1438 EXPORT_SYMBOL(ldlm_pools_fini);
1439
1440 void ldlm_pools_wakeup(void)
1441 {
1442         return;
1443 }
1444 EXPORT_SYMBOL(ldlm_pools_wakeup);
1445
1446 void ldlm_pools_recalc(ldlm_side_t client)
1447 {
1448         return;
1449 }
1450 EXPORT_SYMBOL(ldlm_pools_recalc);
1451 #endif /* HAVE_LRU_RESIZE_SUPPORT */