Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / include / lustre_dlm.h
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 [sun.com URL with a
20  * copy of GPLv2].
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
37 #ifndef _LUSTRE_DLM_H__
38 #define _LUSTRE_DLM_H__
39
40 #if defined(__linux__)
41 #include <linux/lustre_dlm.h>
42 #elif defined(__APPLE__)
43 #include <darwin/lustre_dlm.h>
44 #elif defined(__WINNT__)
45 #include <winnt/lustre_dlm.h>
46 #else
47 #error Unsupported operating system.
48 #endif
49
50 #include <lustre_lib.h>
51 #include <lustre_net.h>
52 #include <lustre_import.h>
53 #include <lustre_handles.h>
54 #include <lustre_export.h> /* for obd_export, for LDLM_DEBUG */
55 #include <interval_tree.h> /* for interval_node{}, ldlm_extent */
56
57 struct obd_ops;
58 struct obd_device;
59
60 #define OBD_LDLM_DEVICENAME  "ldlm"
61
62 #define LDLM_DEFAULT_LRU_SIZE (100 * num_online_cpus())
63 #define LDLM_DEFAULT_MAX_ALIVE (cfs_time_seconds(36000))
64 #define LDLM_CTIME_AGE_LIMIT (10)
65
66 typedef enum {
67         ELDLM_OK = 0,
68
69         ELDLM_LOCK_CHANGED = 300,
70         ELDLM_LOCK_ABORTED = 301,
71         ELDLM_LOCK_REPLACED = 302,
72         ELDLM_NO_LOCK_DATA = 303,
73
74         ELDLM_NAMESPACE_EXISTS = 400,
75         ELDLM_BAD_NAMESPACE    = 401
76 } ldlm_error_t;
77
78 typedef enum {
79         LDLM_NAMESPACE_SERVER = 1 << 0,
80         LDLM_NAMESPACE_CLIENT = 1 << 1
81 } ldlm_side_t;
82
83 #define LDLM_FL_LOCK_CHANGED   0x000001 /* extent, mode, or resource changed */
84
85 /* If the server returns one of these flags, then the lock was put on that list.
86  * If the client sends one of these flags (during recovery ONLY!), it wants the
87  * lock added to the specified list, no questions asked. -p */
88 #define LDLM_FL_BLOCK_GRANTED  0x000002
89 #define LDLM_FL_BLOCK_CONV     0x000004
90 #define LDLM_FL_BLOCK_WAIT     0x000008
91
92 #define LDLM_FL_CBPENDING      0x000010 /* this lock is being destroyed */
93 #define LDLM_FL_AST_SENT       0x000020 /* blocking or cancel packet was sent */
94 #define LDLM_FL_WAIT_NOREPROC  0x000040 /* not a real flag, not saved in lock */
95 #define LDLM_FL_CANCEL         0x000080 /* cancellation callback already run */
96
97 /* Lock is being replayed.  This could probably be implied by the fact that one
98  * of BLOCK_{GRANTED,CONV,WAIT} is set, but that is pretty dangerous. */
99 #define LDLM_FL_REPLAY         0x000100
100
101 #define LDLM_FL_INTENT_ONLY    0x000200 /* don't grant lock, just do intent */
102 #define LDLM_FL_LOCAL_ONLY     0x000400 /* see ldlm_cli_cancel_unused */
103
104 /* don't run the cancel callback under ldlm_cli_cancel_unused */
105 #define LDLM_FL_FAILED         0x000800
106
107 #define LDLM_FL_HAS_INTENT     0x001000 /* lock request has intent */
108 #define LDLM_FL_CANCELING      0x002000 /* lock cancel has already been sent */
109 #define LDLM_FL_LOCAL          0x004000 /* local lock (ie, no srv/cli split) */
110 #define LDLM_FL_WARN           0x008000 /* see ldlm_cli_cancel_unused */
111 #define LDLM_FL_DISCARD_DATA   0x010000 /* discard (no writeback) on cancel */
112
113 #define LDLM_FL_NO_TIMEOUT     0x020000 /* Blocked by group lock - wait
114                                          * indefinitely */
115
116 /* file & record locking */
117 #define LDLM_FL_BLOCK_NOWAIT   0x040000 // server told not to wait if blocked
118 #define LDLM_FL_TEST_LOCK      0x080000 // return blocking lock
119
120 /* XXX FIXME: This is being added to b_size as a low-risk fix to the fact that
121  * the LVB filling happens _after_ the lock has been granted, so another thread
122  * can match`t before the LVB has been updated.  As a dirty hack, we set
123  * LDLM_FL_LVB_READY only after we've done the LVB poop.
124  * this is only needed on lov/osc now, where lvb is actually used and callers
125  * must set it in input flags.
126  *
127  * The proper fix is to do the granting inside of the completion AST, which can
128  * be replaced with a LVB-aware wrapping function for OSC locks.  That change is
129  * pretty high-risk, though, and would need a lot more testing. */
130
131 #define LDLM_FL_LVB_READY      0x100000
132
133 /* A lock contributes to the kms calculation until it has finished the part
134  * of it's cancelation that performs write back on its dirty pages.  It
135  * can remain on the granted list during this whole time.  Threads racing
136  * to update the kms after performing their writeback need to know to
137  * exclude each others locks from the calculation as they walk the granted
138  * list. */
139 #define LDLM_FL_KMS_IGNORE     0x200000
140
141 /* Don't drop lock covering mmapped file in LRU */
142 #define LDLM_FL_NO_LRU         0x400000
143
144 /* Immediatelly cancel such locks when they block some other locks. Send
145    cancel notification to original lock holder, but expect no reply. */
146 #define LDLM_FL_CANCEL_ON_BLOCK 0x800000
147
148 /* Flags flags inherited from parent lock when doing intents. */
149 #define LDLM_INHERIT_FLAGS     (LDLM_FL_CANCEL_ON_BLOCK)
150
151 /* These are flags that are mapped into the flags and ASTs of blocking locks */
152 #define LDLM_AST_DISCARD_DATA  0x80000000 /* Add FL_DISCARD to blocking ASTs */
153 /* Flags sent in AST lock_flags to be mapped into the receiving lock. */
154 #define LDLM_AST_FLAGS         (LDLM_FL_DISCARD_DATA)
155
156 /* completion ast to be executed */
157 #define LDLM_FL_CP_REQD        0x1000000
158
159 /* cleanup_resource has already handled the lock */
160 #define LDLM_FL_CLEANED        0x2000000
161
162 /* optimization hint: LDLM can run blocking callback from current context
163  * w/o involving separate thread. in order to decrease cs rate */
164 #define LDLM_FL_ATOMIC_CB      0x4000000
165
166 /* Cancel lock asynchronously. See ldlm_cli_cancel_unused_resource. */
167 #define LDLM_FL_ASYNC           0x8000000
168
169 /* It may happen that a client initiate 2 operations, e.g. unlink and mkdir,
170  * such that server send blocking ast for conflict locks to this client for
171  * the 1st operation, whereas the 2nd operation has canceled this lock and
172  * is waiting for rpc_lock which is taken by the 1st operation.
173  * LDLM_FL_BL_AST is to be set by ldlm_callback_handler() to the lock not allow
174  * ELC code to cancel it. 
175  * LDLM_FL_BL_DONE is to be set by ldlm_cancel_callback() when lock cache is
176  * droped to let ldlm_callback_handler() return EINVAL to the server. It is
177  * used when ELC rpc is already prepared and is waiting for rpc_lock, too late
178  * to send a separate CANCEL rpc. */
179 #define LDLM_FL_BL_AST          0x10000000
180 #define LDLM_FL_BL_DONE         0x20000000
181
182 /* measure lock contention and return -EUSERS if locking contention is high */
183 #define LDLM_FL_DENY_ON_CONTENTION 0x40000000
184
185 /* The blocking callback is overloaded to perform two functions.  These flags
186  * indicate which operation should be performed. */
187 #define LDLM_CB_BLOCKING    1
188 #define LDLM_CB_CANCELING   2
189
190 /* compatibility matrix */
191 #define LCK_COMPAT_EX  LCK_NL
192 #define LCK_COMPAT_PW  (LCK_COMPAT_EX | LCK_CR)
193 #define LCK_COMPAT_PR  (LCK_COMPAT_PW | LCK_PR)
194 #define LCK_COMPAT_CW  (LCK_COMPAT_PW | LCK_CW)
195 #define LCK_COMPAT_CR  (LCK_COMPAT_CW | LCK_PR | LCK_PW)
196 #define LCK_COMPAT_NL  (LCK_COMPAT_CR | LCK_EX | LCK_GROUP)
197 #define LCK_COMPAT_GROUP  (LCK_GROUP | LCK_NL)
198
199 extern ldlm_mode_t lck_compat_array[];
200
201 static inline void lockmode_verify(ldlm_mode_t mode)
202 {
203        LASSERT(mode > LCK_MINMODE && mode < LCK_MAXMODE);
204 }
205
206 static inline int lockmode_compat(ldlm_mode_t exist_mode, ldlm_mode_t new_mode)
207 {
208        return (lck_compat_array[exist_mode] & new_mode);
209 }
210
211 /*
212  *
213  * cluster name spaces
214  *
215  */
216
217 #define DLM_OST_NAMESPACE 1
218 #define DLM_MDS_NAMESPACE 2
219
220 /* XXX
221    - do we just separate this by security domains and use a prefix for
222      multiple namespaces in the same domain?
223    -
224 */
225
226 /*
227  * Locking rules:
228  *
229  * lr_lock
230  *
231  * lr_lock
232  *     waiting_locks_spinlock
233  *
234  * lr_lock
235  *     led_lock
236  *
237  * lr_lock
238  *     ns_unused_lock
239  *
240  * lr_lvb_sem
241  *     lr_lock
242  *
243  */
244
245 struct ldlm_pool;
246 struct ldlm_lock;
247 struct ldlm_resource;
248 struct ldlm_namespace;
249
250 struct ldlm_pool_ops {
251         int (*po_recalc)(struct ldlm_pool *pl);
252         int (*po_shrink)(struct ldlm_pool *pl, int nr, 
253                          unsigned int gfp_mask);
254         int (*po_setup)(struct ldlm_pool *pl, int limit);
255 };
256
257 /* One second for pools thread check interval. */
258 #define LDLM_POOLS_THREAD_PERIOD (1)
259
260 /* 5% margin for modest pools. See ldlm_pool.c for details. */
261 #define LDLM_POOLS_MODEST_MARGIN (5)
262
263 /* A change to SLV in % after which we want to wake up pools thread asap. */
264 #define LDLM_POOLS_FAST_SLV_CHANGE (50)
265
266 struct ldlm_pool {
267         /** 
268          * Pool proc directory. 
269          */
270         cfs_proc_dir_entry_t  *pl_proc_dir;
271         /**
272          * Pool name, should be long enough to contain compound proc entry name.
273          */
274         char                   pl_name[100];
275         /** 
276          * Lock for protecting slv/clv updates. 
277          */
278         spinlock_t             pl_lock;
279         /**
280          * Number of allowed locks in in pool, both, client and server side. 
281          */
282         atomic_t               pl_limit;
283         /** 
284          * Number of granted locks in
285          */
286         atomic_t               pl_granted;
287         /** 
288          * Grant rate per T. 
289          */
290         atomic_t               pl_grant_rate;
291         /** 
292          * Cancel rate per T. 
293          */
294         atomic_t               pl_cancel_rate;
295         /** 
296          * Grant speed (GR-CR) per T. 
297          */
298         atomic_t               pl_grant_speed;
299         /** 
300          * Server lock volume. Protected by pl_lock.
301          */
302         __u64                  pl_server_lock_volume;
303         /** 
304          * Current biggest client lock volume. Protected by pl_lock.
305          */
306         __u64                  pl_client_lock_volume;
307         /** 
308          * Lock volume factor. SLV on client is calculated as following:
309          * server_slv * lock_volume_factor.
310          */
311         atomic_t               pl_lock_volume_factor;
312         /** 
313          * Time when last slv from server was obtained. 
314          */
315         time_t                 pl_recalc_time;
316         /**
317          * Recalc and shrink ops. 
318          */ 
319         struct ldlm_pool_ops  *pl_ops;
320         /**
321          * Planned number of granted locks for next T.
322          */
323         int                    pl_grant_plan;
324         /** 
325          * Grant plan step for next T. 
326          */
327         int                    pl_grant_step;
328         /** 
329          * Pool statistics. 
330          */
331         struct lprocfs_stats  *pl_stats;
332 };
333
334 typedef int (*ldlm_res_policy)(struct ldlm_namespace *, struct ldlm_lock **,
335                                void *req_cookie, ldlm_mode_t mode, int flags,
336                                void *data);
337
338 struct ldlm_valblock_ops {
339         int (*lvbo_init)(struct ldlm_resource *res);
340         int (*lvbo_update)(struct ldlm_resource *res, struct lustre_msg *m,
341                            int buf_idx, int increase);
342 };
343
344 typedef enum {
345         LDLM_NAMESPACE_GREEDY = 1 << 0,
346         LDLM_NAMESPACE_MODEST = 1 << 1
347 } ldlm_appetite_t;
348
349 /* 
350  * Default value for ->ns_shrink_thumb. If lock is not extent one its cost 
351  * is one page. Here we have 256 pages which is 1M on i386. Thus by default
352  * all extent locks which have more than 1M long extent will be kept in lru,
353  * others (including ibits locks) will be canceled on memory pressure event. 
354  */
355 #define LDLM_LOCK_SHRINK_THUMB 256
356
357 /* 
358  * Default values for the "max_nolock_size", "contention_time" and 
359  * "contended_locks" namespace tunables. 
360  */
361 #define NS_DEFAULT_MAX_NOLOCK_BYTES 0
362 #define NS_DEFAULT_CONTENTION_SECONDS 2
363 #define NS_DEFAULT_CONTENDED_LOCKS 32
364
365 struct ldlm_namespace {
366         /**
367          * Namespace name. Used for logging, etc.
368          */
369         char                  *ns_name;
370
371         /** 
372          * Is this a client-side lock tree? 
373          */
374         ldlm_side_t            ns_client;
375
376         /** 
377          * Namespce connect flags supported by server (may be changed via proc,
378          * lru resize may be disabled/enabled).
379          */
380         __u64                  ns_connect_flags;
381
382          /** 
383           * Client side orig connect flags supported by server. 
384           */
385         __u64                  ns_orig_connect_flags;
386
387         /** 
388          * Hash table for namespace.
389          */
390         struct list_head      *ns_hash;
391         spinlock_t             ns_hash_lock;
392
393          /**
394           * Count of resources in the hash. 
395           */
396         __u32                  ns_refcount;
397
398          /** 
399           * All root resources in namespace. 
400           */
401         struct list_head       ns_root_list;
402
403         /** 
404          * Position in global namespace list.
405          */
406         struct list_head       ns_list_chain; 
407
408         /** 
409          * All root resources in namespace. 
410          */
411         struct list_head       ns_unused_list; 
412         int                    ns_nr_unused;
413         spinlock_t             ns_unused_lock;
414
415         unsigned int           ns_max_unused;
416         unsigned int           ns_max_age;
417
418          /**
419           * Seconds.
420           */
421         unsigned int           ns_ctime_age_limit;
422         
423         /** 
424          * Lower limit to number of pages in lock to keep it in cache.
425          */
426         unsigned int           ns_shrink_thumb;
427
428         /**
429          * Next debug dump, jiffies.
430          */
431         cfs_time_t             ns_next_dump;
432
433         atomic_t               ns_locks;
434         __u64                  ns_resources;
435         ldlm_res_policy        ns_policy;
436         struct ldlm_valblock_ops *ns_lvbo;
437         void                  *ns_lvbp;
438         cfs_waitq_t            ns_waitq;
439         struct ldlm_pool       ns_pool;
440         ldlm_appetite_t        ns_appetite;
441
442         /** 
443          * If more than @ns_contented_locks found, the resource considered
444          * as contended.
445          */
446         unsigned               ns_contended_locks;
447
448         /** 
449          * The resource remembers contended state during @ns_contention_time,
450          * in seconds.
451          */
452         unsigned               ns_contention_time;
453
454         /** 
455          * Limit size of nolock requests, in bytes.
456          */
457         unsigned               ns_max_nolock_size;
458
459         /**
460          * Backward link to obd, required for ldlm pool to store new SLV. 
461          */
462         struct obd_device     *ns_obd;
463
464         struct adaptive_timeout ns_at_estimate;/* estimated lock callback time*/
465 };
466
467 static inline int ns_is_client(struct ldlm_namespace *ns)
468 {
469         LASSERT(ns != NULL);
470         LASSERT(!(ns->ns_client & ~(LDLM_NAMESPACE_CLIENT | 
471                                     LDLM_NAMESPACE_SERVER)));
472         LASSERT(ns->ns_client == LDLM_NAMESPACE_CLIENT ||
473                 ns->ns_client == LDLM_NAMESPACE_SERVER);
474         return ns->ns_client == LDLM_NAMESPACE_CLIENT;
475 }
476
477 static inline int ns_is_server(struct ldlm_namespace *ns)
478 {
479         LASSERT(ns != NULL);
480         LASSERT(!(ns->ns_client & ~(LDLM_NAMESPACE_CLIENT | 
481                                     LDLM_NAMESPACE_SERVER)));
482         LASSERT(ns->ns_client == LDLM_NAMESPACE_CLIENT ||
483                 ns->ns_client == LDLM_NAMESPACE_SERVER);
484         return ns->ns_client == LDLM_NAMESPACE_SERVER;
485 }
486
487 static inline int ns_connect_lru_resize(struct ldlm_namespace *ns)
488 {
489         LASSERT(ns != NULL);
490         return !!(ns->ns_connect_flags & OBD_CONNECT_LRU_RESIZE);
491 }
492
493 /*
494  *
495  * Resource hash table
496  *
497  */
498
499 #define RES_HASH_BITS 10
500 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
501 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
502
503 struct ldlm_lock;
504
505 typedef int (*ldlm_blocking_callback)(struct ldlm_lock *lock,
506                                       struct ldlm_lock_desc *new, void *data,
507                                       int flag);
508 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, int flags,
509                                         void *data);
510 typedef int (*ldlm_glimpse_callback)(struct ldlm_lock *lock, void *data);
511
512 /* Interval node data for each LDLM_EXTENT lock */
513 struct ldlm_interval {
514         struct interval_node li_node;   /* node for tree mgmt */
515         struct list_head     li_group;  /* the locks which have the same 
516                                          * policy - group of the policy */
517 };
518 #define to_ldlm_interval(n) container_of(n, struct ldlm_interval, li_node)
519
520 /* the interval tree must be accessed inside the resource lock. */
521 struct ldlm_interval_tree {
522         /* tree size, this variable is used to count
523          * granted PW locks in ldlm_extent_policy()*/
524         int                   lit_size;
525         ldlm_mode_t           lit_mode; /* lock mode */
526         struct interval_node *lit_root; /* actually ldlm_interval */
527 };
528
529 struct ldlm_lock {
530         struct portals_handle l_handle; // must be first in the structure
531         atomic_t              l_refc;
532
533         /* internal spinlock protects l_resource.  we should hold this lock 
534          * first before grabbing res_lock.*/
535         spinlock_t            l_lock;
536
537         /* ldlm_lock_change_resource() can change this */
538         struct ldlm_resource *l_resource;
539
540         /* protected by ns_hash_lock. FIXME */
541         struct list_head      l_lru;
542
543         /* protected by lr_lock, linkage to resource's lock queues */
544         struct list_head      l_res_link;
545
546         struct ldlm_interval *l_tree_node;      /* tree node for ldlm_extent */
547
548         /* protected by led_lock */
549         struct list_head      l_export_chain; // per-export chain of locks
550
551         /* protected by lr_lock */
552         ldlm_mode_t           l_req_mode;
553         ldlm_mode_t           l_granted_mode;
554
555         ldlm_completion_callback l_completion_ast;
556         ldlm_blocking_callback   l_blocking_ast;
557         ldlm_glimpse_callback    l_glimpse_ast;
558
559         struct obd_export    *l_export;
560         struct obd_export    *l_conn_export;
561
562         struct lustre_handle  l_remote_handle;
563         ldlm_policy_data_t    l_policy_data;
564
565         /* protected by lr_lock */
566         __u32                 l_flags;
567         __u32                 l_readers;
568         __u32                 l_writers;
569         __u8                  l_destroyed;
570
571         /* If the lock is granted, a process sleeps on this waitq to learn when
572          * it's no longer in use.  If the lock is not granted, a process sleeps
573          * on this waitq to learn when it becomes granted. */
574         cfs_waitq_t           l_waitq;
575         struct timeval        l_enqueued_time;
576
577         cfs_time_t            l_last_used;      /* jiffies */
578         struct ldlm_extent    l_req_extent;
579
580         /* Client-side-only members */
581         __u32                 l_lvb_len;        /* temporary storage for */
582         void                 *l_lvb_data;       /* an LVB received during */
583         void                 *l_lvb_swabber;    /* an enqueue */
584         void                 *l_ast_data;
585         spinlock_t            l_extents_list_lock;
586         struct list_head      l_extents_list;
587
588         struct list_head      l_cache_locks_list;
589
590         /* Server-side-only members */
591
592         /* protected by elt_lock */
593         struct list_head      l_pending_chain;  /* callbacks pending */
594         cfs_time_t            l_callback_timeout; /* jiffies */
595
596         __u32                 l_pid;            /* pid which created this lock */
597
598         /* for ldlm_add_ast_work_item() */
599         struct list_head      l_bl_ast;
600         struct list_head      l_cp_ast;
601         struct ldlm_lock     *l_blocking_lock;
602         int                   l_bl_ast_run;
603
604         /* protected by lr_lock, linkages to "skip lists" */
605         struct list_head      l_sl_mode;
606         struct list_head      l_sl_policy;
607 };
608
609 struct ldlm_resource {
610         struct ldlm_namespace *lr_namespace;
611
612         /* protected by ns_hash_lock */
613         struct list_head       lr_hash;
614         struct ldlm_resource  *lr_parent;   /* 0 for a root resource */
615         struct list_head       lr_children; /* list head for child resources */
616         struct list_head       lr_childof;  /* part of ns_root_list if root res,
617                                              * part of lr_children if child */
618         spinlock_t             lr_lock;
619
620         /* protected by lr_lock */
621         struct list_head       lr_granted;
622         struct list_head       lr_converting;
623         struct list_head       lr_waiting;
624         ldlm_mode_t            lr_most_restr;
625         ldlm_type_t            lr_type; /* LDLM_{PLAIN,EXTENT,FLOCK} */
626         struct ldlm_res_id     lr_name;
627         atomic_t               lr_refcount;
628
629         struct ldlm_interval_tree lr_itree[LCK_MODE_NUM];  /* interval trees*/
630
631         /* Server-side-only lock value block elements */
632         struct semaphore       lr_lvb_sem;
633         __u32                  lr_lvb_len;
634         void                  *lr_lvb_data;
635
636         /* when the resource was considered as contended */
637         cfs_time_t             lr_contention_time;
638 };
639
640 struct ldlm_ast_work {
641         struct ldlm_lock *w_lock;
642         int               w_blocking;
643         struct ldlm_lock_desc w_desc;
644         struct list_head   w_list;
645         int w_flags;
646         void *w_data;
647         int w_datalen;
648 };
649
650 /* ldlm_enqueue parameters common */
651 struct ldlm_enqueue_info {
652         __u32 ei_type;   /* Type of the lock being enqueued. */
653         __u32 ei_mode;   /* Mode of the lock being enqueued. */
654         void *ei_cb_bl;  /* blocking lock callback */
655         void *ei_cb_cp;  /* lock completion callback */
656         void *ei_cb_gl;  /* lock glimpse callback */
657         void *ei_cbdata; /* Data to be passed into callbacks. */
658 };
659
660 extern struct obd_ops ldlm_obd_ops;
661
662 extern char *ldlm_lockname[];
663 extern char *ldlm_typename[];
664 extern char *ldlm_it2str(int it);
665 #ifdef LIBCFS_DEBUG
666 #define ldlm_lock_debug(cdls, level, lock, file, func, line, fmt, a...) do { \
667         CHECK_STACK();                                                  \
668                                                                         \
669         if (((level) & D_CANTMASK) != 0 ||                              \
670             ((libcfs_debug & (level)) != 0 &&                           \
671              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0)) {        \
672                 static struct libcfs_debug_msg_data _ldlm_dbg_data =    \
673                 DEBUG_MSG_DATA_INIT(cdls, DEBUG_SUBSYSTEM,              \
674                                     file, func, line);                  \
675                 _ldlm_lock_debug(lock, level, &_ldlm_dbg_data, fmt,     \
676                                  ##a );                                 \
677         }                                                               \
678 } while(0)
679
680 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 mask,
681                       struct libcfs_debug_msg_data *data, const char *fmt,
682                       ...)
683         __attribute__ ((format (printf, 4, 5)));
684
685 #define LDLM_ERROR(lock, fmt, a...) do {                                \
686         static cfs_debug_limit_state_t _ldlm_cdls;                      \
687         ldlm_lock_debug(&_ldlm_cdls, D_ERROR, lock,                     \
688                         __FILE__, __FUNCTION__, __LINE__,               \
689                         "### " fmt , ##a);                              \
690 } while (0)
691
692 #define LDLM_DEBUG(lock, fmt, a...)   do {                              \
693         ldlm_lock_debug(NULL, D_DLMTRACE, lock,                         \
694                         __FILE__, __FUNCTION__, __LINE__,               \
695                          "### " fmt , ##a);                             \
696 } while (0)
697 #else
698 #define LDLM_DEBUG(lock, fmt, a...) ((void)0)
699 #define LDLM_ERROR(lock, fmt, a...) ((void)0)
700 #endif
701
702 #define LDLM_DEBUG_NOLOCK(format, a...)                 \
703         CDEBUG(D_DLMTRACE, "### " format "\n" , ##a)
704
705 typedef int (*ldlm_processing_policy)(struct ldlm_lock *lock, int *flags,
706                                       int first_enq, ldlm_error_t *err,
707                                       struct list_head *work_list);
708
709 /*
710  * Iterators.
711  */
712
713 #define LDLM_ITER_CONTINUE 1 /* keep iterating */
714 #define LDLM_ITER_STOP     2 /* stop iterating */
715
716 typedef int (*ldlm_iterator_t)(struct ldlm_lock *, void *);
717 typedef int (*ldlm_res_iterator_t)(struct ldlm_resource *, void *);
718
719 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
720                           void *closure);
721 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
722                            void *closure);
723 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
724                                ldlm_res_iterator_t iter, void *closure);
725
726 int ldlm_replay_locks(struct obd_import *imp);
727 void ldlm_resource_iterate(struct ldlm_namespace *, const struct ldlm_res_id *,
728                            ldlm_iterator_t iter, void *data);
729
730 /* ldlm_flock.c */
731 int ldlm_flock_completion_ast(struct ldlm_lock *lock, int flags, void *data);
732
733 /* ldlm_extent.c */
734 __u64 ldlm_extent_shift_kms(struct ldlm_lock *lock, __u64 old_kms);
735
736
737 /* ldlm_lockd.c */
738 int ldlm_server_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
739                              void *data, int flag);
740 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data);
741 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data);
742 int ldlm_handle_enqueue(struct ptlrpc_request *req, ldlm_completion_callback,
743                         ldlm_blocking_callback, ldlm_glimpse_callback);
744 int ldlm_handle_convert(struct ptlrpc_request *req);
745 int ldlm_handle_cancel(struct ptlrpc_request *req);
746 int ldlm_request_cancel(struct ptlrpc_request *req,
747                         const struct ldlm_request *dlm_req, int first);
748 int ldlm_del_waiting_lock(struct ldlm_lock *lock);
749 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock);
750 void ldlm_revoke_export_locks(struct obd_export *exp);
751 int ldlm_get_ref(void);
752 void ldlm_put_ref(void);
753
754 /* ldlm_lock.c */
755 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res);
756 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg);
757 void ldlm_lock2handle(const struct ldlm_lock *lock,
758                       struct lustre_handle *lockh);
759 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *, int flags);
760 void ldlm_cancel_callback(struct ldlm_lock *);
761 int ldlm_lock_set_data(struct lustre_handle *, void *data);
762 int ldlm_lock_remove_from_lru(struct ldlm_lock *);
763 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *,
764                                       const struct lustre_handle *);
765
766 static inline struct ldlm_lock *ldlm_handle2lock(const struct lustre_handle *h)
767 {
768         return __ldlm_handle2lock(h, 0);
769 }
770
771 static inline int ldlm_res_lvbo_update(struct ldlm_resource *res,
772                                        struct lustre_msg *m, int buf_idx,
773                                        int increase)
774 {
775         if (res->lr_namespace->ns_lvbo &&
776             res->lr_namespace->ns_lvbo->lvbo_update) {
777                 return res->lr_namespace->ns_lvbo->lvbo_update(res, m, buf_idx,
778                                                                increase);
779         }
780         return 0;
781 }
782
783 #define LDLM_LOCK_PUT(lock)                     \
784 do {                                            \
785         /*LDLM_DEBUG((lock), "put");*/          \
786         ldlm_lock_put(lock);                    \
787 } while (0)
788
789 #define LDLM_LOCK_GET(lock)                     \
790 ({                                              \
791         ldlm_lock_get(lock);                    \
792         /*LDLM_DEBUG((lock), "get");*/          \
793         lock;                                   \
794 })
795
796 #define ldlm_lock_list_put(head, member, count)                 \
797 ({                                                              \
798         struct ldlm_lock *_lock, *_next;                        \
799         int c = count;                                          \
800         list_for_each_entry_safe(_lock, _next, head, member) {  \
801                 if (c-- == 0)                                   \
802                         break;                                  \
803                 list_del_init(&_lock->member);                  \
804                 LDLM_LOCK_PUT(_lock);                           \
805         }                                                       \
806         LASSERT(c <= 0);                                        \
807 })
808
809 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
810 void ldlm_lock_put(struct ldlm_lock *lock);
811 void ldlm_lock_destroy(struct ldlm_lock *lock);
812 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
813 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
814 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
815 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode);
816 void ldlm_lock_allow_match(struct ldlm_lock *lock);
817 int ldlm_lock_fast_match(struct ldlm_lock *, int, obd_off, obd_off, void **);
818 void ldlm_lock_fast_release(void *, int);
819 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
820                             const struct ldlm_res_id *, ldlm_type_t type,
821                             ldlm_policy_data_t *, ldlm_mode_t mode,
822                             struct lustre_handle *);
823 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
824                                         __u32 *flags);
825 void ldlm_lock_cancel(struct ldlm_lock *lock);
826 void ldlm_cancel_locks_for_export(struct obd_export *export);
827 void ldlm_reprocess_all(struct ldlm_resource *res);
828 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns);
829 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos);
830 void ldlm_lock_dump_handle(int level, struct lustre_handle *);
831 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req);
832
833 /* resource.c */
834 struct ldlm_namespace *
835 ldlm_namespace_new(struct obd_device *obd, char *name, 
836                    ldlm_side_t client, ldlm_appetite_t apt);
837 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, int flags);
838 void ldlm_namespace_free(struct ldlm_namespace *ns, 
839                          struct obd_import *imp, int force);
840 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client);
841 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client);
842 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client);
843 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client);
844 void ldlm_namespace_get_locked(struct ldlm_namespace *ns);
845 void ldlm_namespace_put_locked(struct ldlm_namespace *ns, int wakeup);
846 void ldlm_namespace_get(struct ldlm_namespace *ns);
847 void ldlm_namespace_put(struct ldlm_namespace *ns, int wakeup);
848 int ldlm_proc_setup(void);
849 #ifdef LPROCFS
850 void ldlm_proc_cleanup(void);
851 #else
852 static inline void ldlm_proc_cleanup(void) {}
853 #endif
854
855 /* resource.c - internal */
856 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
857                                         struct ldlm_resource *parent,
858                                         const struct ldlm_res_id *,
859                                         ldlm_type_t type, int create);
860 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
861 int ldlm_resource_putref(struct ldlm_resource *res);
862 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
863                             struct ldlm_lock *lock);
864 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
865 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
866 void ldlm_dump_all_namespaces(ldlm_side_t client, int level);
867 void ldlm_namespace_dump(int level, struct ldlm_namespace *);
868 void ldlm_resource_dump(int level, struct ldlm_resource *);
869 int ldlm_lock_change_resource(struct ldlm_namespace *, struct ldlm_lock *,
870                               const struct ldlm_res_id *);
871
872 struct ldlm_callback_suite {
873         ldlm_completion_callback lcs_completion;
874         ldlm_blocking_callback   lcs_blocking;
875         ldlm_glimpse_callback    lcs_glimpse;
876 };
877
878 /* ldlm_request.c */
879 int ldlm_expired_completion_wait(void *data);
880 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
881                       void *data, int flag);
882 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp);
883 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data);
884 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
885                      struct ldlm_enqueue_info *einfo,
886                      const struct ldlm_res_id *res_id,
887                      ldlm_policy_data_t *policy, int *flags,
888                      void *lvb, __u32 lvb_len, void *lvb_swabber,
889                      struct lustre_handle *lockh, int async);
890 int ldlm_prep_enqueue_req(struct obd_export *exp,
891                           struct ptlrpc_request *req,
892                           struct list_head *cancels,
893                           int count);
894 int ldlm_prep_elc_req(struct obd_export *exp,
895                       struct ptlrpc_request *req,
896                       int version, int opc, int canceloff,
897                       struct list_head *cancels, int count);
898 int ldlm_handle_enqueue0(struct ldlm_namespace *ns, struct ptlrpc_request *req,
899                          const struct ldlm_request *dlm_req,
900                          const struct ldlm_callback_suite *cbs);
901 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
902                           ldlm_type_t type, __u8 with_policy, ldlm_mode_t mode,
903                           int *flags, void *lvb, __u32 lvb_len,
904                           void *lvb_swabber, struct lustre_handle *lockh,
905                           int rc);
906 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
907                            const struct ldlm_res_id *res_id,
908                            ldlm_type_t type, ldlm_policy_data_t *policy,
909                            ldlm_mode_t mode, int *flags,
910                            ldlm_blocking_callback blocking,
911                            ldlm_completion_callback completion,
912                            ldlm_glimpse_callback glimpse,
913                            void *data, __u32 lvb_len, void *lvb_swabber,
914                            struct lustre_handle *lockh);
915 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
916                     void *data, __u32 data_len);
917 int ldlm_cli_convert(struct lustre_handle *, int new_mode, __u32 *flags);
918 int ldlm_cli_update_pool(struct ptlrpc_request *req);
919 int ldlm_handle_convert0(struct ptlrpc_request *req,
920                          const struct ldlm_request *dlm_req);
921 int ldlm_cli_cancel(struct lustre_handle *lockh);
922 int ldlm_cli_cancel_unused(struct ldlm_namespace *, const struct ldlm_res_id *,
923                            int flags, void *opaque);
924 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
925                                     const struct ldlm_res_id *res_id,
926                                     ldlm_policy_data_t *policy,
927                                     ldlm_mode_t mode, int flags, void *opaque);
928 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *head,
929                         int count, int flags);
930 int ldlm_cli_join_lru(struct ldlm_namespace *,
931                       const struct ldlm_res_id *, int join);
932 int ldlm_cancel_resource_local(struct ldlm_resource *res,
933                                struct list_head *cancels,
934                                ldlm_policy_data_t *policy,
935                                ldlm_mode_t mode, int lock_flags,
936                                int cancel_flags, void *opaque);
937 int ldlm_cli_cancel_list(struct list_head *head, int count,
938                          struct ptlrpc_request *req, int flags);
939  
940 /* mds/handler.c */
941 /* This has to be here because recursive inclusion sucks. */
942 int intent_disposition(struct ldlm_reply *rep, int flag);
943 void intent_set_disposition(struct ldlm_reply *rep, int flag);
944
945
946 /* ioctls for trying requests */
947 #define IOC_LDLM_TYPE                   'f'
948 #define IOC_LDLM_MIN_NR                 40
949
950 #define IOC_LDLM_TEST                   _IOWR('f', 40, long)
951 #define IOC_LDLM_DUMP                   _IOWR('f', 41, long)
952 #define IOC_LDLM_REGRESS_START          _IOWR('f', 42, long)
953 #define IOC_LDLM_REGRESS_STOP           _IOWR('f', 43, long)
954 #define IOC_LDLM_MAX_NR                 43
955
956 static inline void lock_res(struct ldlm_resource *res)
957 {
958         spin_lock(&res->lr_lock);
959 }
960
961 static inline void unlock_res(struct ldlm_resource *res)
962 {
963         spin_unlock(&res->lr_lock);
964 }
965
966 static inline void check_res_locked(struct ldlm_resource *res)
967 {
968         LASSERT_SPIN_LOCKED(&res->lr_lock);
969 }
970
971 struct ldlm_resource * lock_res_and_lock(struct ldlm_lock *lock);
972 void unlock_res_and_lock(struct ldlm_lock *lock);
973
974 /* ldlm_pool.c */
975 void ldlm_pools_recalc(ldlm_side_t client);
976 int ldlm_pools_init(void);
977 void ldlm_pools_fini(void);
978 void ldlm_pools_wakeup(void);
979
980 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns, 
981                    int idx, ldlm_side_t client);
982 int ldlm_pool_shrink(struct ldlm_pool *pl, int nr, 
983                      unsigned int gfp_mask);
984 void ldlm_pool_fini(struct ldlm_pool *pl);
985 int ldlm_pool_setup(struct ldlm_pool *pl, int limit);
986 int ldlm_pool_recalc(struct ldlm_pool *pl);
987 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl);
988 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl);
989 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl);
990 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl);
991 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv);
992 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv);
993 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit);
994 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock);
995 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock);
996 #endif
997