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