Whamcloud - gitweb
LU-2275 mdt: Avoid setting positive dispositions too early
[fs/lustre-release.git] / lustre / include / lustre_dlm.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2010, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 /** \defgroup LDLM Lustre Distributed Lock Manager
38  *
39  * Lustre DLM is based on VAX DLM.
40  * Its two main roles are:
41  *   - To provide locking assuring consistency of data on all Lustre nodes.
42  *   - To allow clients to cache state protected by a lock by holding the
43  *     lock until a conflicting lock is requested or it is expired by the LRU.
44  *
45  * @{
46  */
47
48 #ifndef _LUSTRE_DLM_H__
49 #define _LUSTRE_DLM_H__
50
51 #if defined(__linux__)
52 #include <linux/lustre_dlm.h>
53 #elif defined(__APPLE__)
54 #include <darwin/lustre_dlm.h>
55 #elif defined(__WINNT__)
56 #include <winnt/lustre_dlm.h>
57 #else
58 #error Unsupported operating system.
59 #endif
60
61 #include <lustre_lib.h>
62 #include <lustre_net.h>
63 #include <lustre_import.h>
64 #include <lustre_handles.h>
65 #include <interval_tree.h> /* for interval_node{}, ldlm_extent */
66 #include <lu_ref.h>
67
68 struct obd_ops;
69 struct obd_device;
70
71 #define OBD_LDLM_DEVICENAME  "ldlm"
72
73 #ifdef HAVE_BGL_SUPPORT
74 /* 1.5 times the maximum 128 tasks available in VN mode */
75 #define LDLM_DEFAULT_LRU_SIZE 196
76 #else
77 #define LDLM_DEFAULT_LRU_SIZE (100 * cfs_num_online_cpus())
78 #endif
79 #define LDLM_DEFAULT_MAX_ALIVE (cfs_time_seconds(36000))
80 #define LDLM_CTIME_AGE_LIMIT (10)
81 #define LDLM_DEFAULT_PARALLEL_AST_LIMIT 1024
82
83 /**
84  * LDLM non-error return states
85  */
86 typedef enum {
87         ELDLM_OK = 0,
88
89         ELDLM_LOCK_CHANGED = 300,
90         ELDLM_LOCK_ABORTED = 301,
91         ELDLM_LOCK_REPLACED = 302,
92         ELDLM_NO_LOCK_DATA = 303,
93
94         ELDLM_NAMESPACE_EXISTS = 400,
95         ELDLM_BAD_NAMESPACE    = 401
96 } ldlm_error_t;
97
98 /**
99  * LDLM namespace type.
100  * The "client" type is actually an indication that this is a narrow local view
101  * into complete namespace on the server. Such namespaces cannot make any
102  * decisions about lack of conflicts or do any autonomous lock granting without
103  * first speaking to a server.
104  */
105 typedef enum {
106         LDLM_NAMESPACE_SERVER = 1 << 0,
107         LDLM_NAMESPACE_CLIENT = 1 << 1
108 } ldlm_side_t;
109
110 /**
111  * Declaration of flags sent through the wire.
112  **/
113 #define LDLM_FL_LOCK_CHANGED   0x000001 /* extent, mode, or resource changed */
114
115 /**
116  * If the server returns one of these flags, then the lock was put on that list.
117  * If the client sends one of these flags (during recovery ONLY!), it wants the
118  * lock added to the specified list, no questions asked.
119  */
120 #define LDLM_FL_BLOCK_GRANTED  0x000002
121 #define LDLM_FL_BLOCK_CONV     0x000004
122 #define LDLM_FL_BLOCK_WAIT     0x000008
123
124 /* Used to be LDLM_FL_CBPENDING 0x000010 moved to non-wire flags */
125
126 #define LDLM_FL_AST_SENT       0x000020 /* blocking or cancel packet was
127                                          * queued for sending. */
128 /* Used to be LDLM_FL_WAIT_NOREPROC 0x000040   moved to non-wire flags */
129 /* Used to be LDLM_FL_CANCEL        0x000080   moved to non-wire flags */
130
131 /**
132  * Lock is being replayed.  This could probably be implied by the fact that one
133  * of BLOCK_{GRANTED,CONV,WAIT} is set, but that is pretty dangerous.
134  */
135 #define LDLM_FL_REPLAY         0x000100
136
137 #define LDLM_FL_INTENT_ONLY    0x000200 /* Don't grant lock, just do intent. */
138
139 /* Used to be LDLM_FL_LOCAL_ONLY 0x000400  moved to non-wire flags */
140 /* Used to be LDLM_FL_FAILED     0x000800  moved to non-wire flags */
141
142 #define LDLM_FL_HAS_INTENT     0x001000 /* lock request has intent */
143
144 /* Used to be LDLM_FL_CANCELING  0x002000  moved to non-wire flags */
145 /* Used to be LDLM_FL_LOCAL      0x004000  moved to non-wire flags */
146
147 #define LDLM_FL_DISCARD_DATA   0x010000 /* discard (no writeback) on cancel */
148
149 #define LDLM_FL_NO_TIMEOUT     0x020000 /* Blocked by group lock - wait
150                                          * indefinitely */
151
152 /** file & record locking */
153 #define LDLM_FL_BLOCK_NOWAIT   0x040000 /* Server told not to wait if blocked.
154                                          * For AGL, OST will not send glimpse
155                                          * callback. */
156 #define LDLM_FL_TEST_LOCK      0x080000 // return blocking lock
157
158 /* Used to be LDLM_FL_LVB_READY  0x100000 moved to non-wire flags */
159 /* Used to be LDLM_FL_KMS_IGNORE 0x200000 moved to non-wire flags */
160 /* Used to be LDLM_FL_NO_LRU     0x400000 moved to non-wire flags */
161
162 /* Immediatelly cancel such locks when they block some other locks. Send
163  * cancel notification to original lock holder, but expect no reply. This is
164  * for clients (like liblustre) that cannot be expected to reliably response
165  * to blocking AST. */
166 #define LDLM_FL_CANCEL_ON_BLOCK 0x800000
167
168 /* Flags flags inherited from parent lock when doing intents. */
169 #define LDLM_INHERIT_FLAGS     (LDLM_FL_CANCEL_ON_BLOCK)
170
171 /* Used to be LDLM_FL_CP_REQD        0x1000000 moved to non-wire flags */
172 /* Used to be LDLM_FL_CLEANED        0x2000000 moved to non-wire flags */
173 /* Used to be LDLM_FL_ATOMIC_CB      0x4000000 moved to non-wire flags */
174 /* Used to be LDLM_FL_BL_AST         0x10000000 moved to non-wire flags */
175 /* Used to be LDLM_FL_BL_DONE        0x20000000 moved to non-wire flags */
176
177 /* measure lock contention and return -EUSERS if locking contention is high */
178 #define LDLM_FL_DENY_ON_CONTENTION 0x40000000
179
180 /* These are flags that are mapped into the flags and ASTs of blocking locks */
181 #define LDLM_AST_DISCARD_DATA  0x80000000 /* Add FL_DISCARD to blocking ASTs */
182
183 /* Flags sent in AST lock_flags to be mapped into the receiving lock. */
184 #define LDLM_AST_FLAGS         (LDLM_FL_DISCARD_DATA)
185
186 /*
187  * --------------------------------------------------------------------------
188  * NOTE! Starting from this point, that is, LDLM_FL_* flags with values above
189  * 0x80000000 will not be sent over the wire.
190  * --------------------------------------------------------------------------
191  */
192
193 /**
194  * Declaration of flags not sent through the wire.
195  **/
196
197 /**
198  * Used for marking lock as a target for -EINTR while cp_ast sleep
199  * emulation + race with upcoming bl_ast.
200  */
201 #define LDLM_FL_FAIL_LOC       0x100000000ULL
202
203 /**
204  * Used while processing the unused list to know that we have already
205  * handled this lock and decided to skip it.
206  */
207 #define LDLM_FL_SKIPPED        0x200000000ULL
208 /* this lock is being destroyed */
209 #define LDLM_FL_CBPENDING      0x400000000ULL
210 /* not a real flag, not saved in lock */
211 #define LDLM_FL_WAIT_NOREPROC  0x800000000ULL
212 /* cancellation callback already run */
213 #define LDLM_FL_CANCEL         0x1000000000ULL
214 #define LDLM_FL_LOCAL_ONLY     0x2000000000ULL
215 /* don't run the cancel callback under ldlm_cli_cancel_unused */
216 #define LDLM_FL_FAILED         0x4000000000ULL
217 /* lock cancel has already been sent */
218 #define LDLM_FL_CANCELING      0x8000000000ULL
219 /* local lock (ie, no srv/cli split) */
220 #define LDLM_FL_LOCAL          0x10000000000ULL
221 /* XXX FIXME: This is being added to b_size as a low-risk fix to the fact that
222  * the LVB filling happens _after_ the lock has been granted, so another thread
223  * can match it before the LVB has been updated.  As a dirty hack, we set
224  * LDLM_FL_LVB_READY only after we've done the LVB poop.
225  * this is only needed on LOV/OSC now, where LVB is actually used and callers
226  * must set it in input flags.
227  *
228  * The proper fix is to do the granting inside of the completion AST, which can
229  * be replaced with a LVB-aware wrapping function for OSC locks.  That change is
230  * pretty high-risk, though, and would need a lot more testing. */
231 #define LDLM_FL_LVB_READY      0x20000000000ULL
232 /* A lock contributes to the known minimum size (KMS) calculation until it has
233  * finished the part of its cancelation that performs write back on its dirty
234  * pages.  It can remain on the granted list during this whole time.  Threads
235  * racing to update the KMS after performing their writeback need to know to
236  * exclude each other's locks from the calculation as they walk the granted
237  * list. */
238 #define LDLM_FL_KMS_IGNORE     0x40000000000ULL
239 /* completion AST to be executed */
240 #define LDLM_FL_CP_REQD        0x80000000000ULL
241 /* cleanup_resource has already handled the lock */
242 #define LDLM_FL_CLEANED        0x100000000000ULL
243 /* optimization hint: LDLM can run blocking callback from current context
244  * w/o involving separate thread. in order to decrease cs rate */
245 #define LDLM_FL_ATOMIC_CB      0x200000000000ULL
246
247 /* It may happen that a client initiates two operations, e.g. unlink and
248  * mkdir, such that the server sends a blocking AST for conflicting
249  * locks to this client for the first operation, whereas the second
250  * operation has canceled this lock and is waiting for rpc_lock which is
251  * taken by the first operation. LDLM_FL_BL_AST is set by
252  * ldlm_callback_handler() in the lock to prevent the Early Lock Cancel
253  * (ELC) code from cancelling it.
254  *
255  * LDLM_FL_BL_DONE is to be set by ldlm_cancel_callback() when lock
256  * cache is dropped to let ldlm_callback_handler() return EINVAL to the
257  * server. It is used when ELC RPC is already prepared and is waiting
258  * for rpc_lock, too late to send a separate CANCEL RPC. */
259 #define LDLM_FL_BL_AST          0x400000000000ULL
260 #define LDLM_FL_BL_DONE         0x800000000000ULL
261 /* Don't put lock into the LRU list, so that it is not canceled due to aging.
262  * Used by MGC locks, they are cancelled only at unmount or by callback. */
263 #define LDLM_FL_NO_LRU          0x1000000000000ULL
264
265 /**
266  * The blocking callback is overloaded to perform two functions.  These flags
267  * indicate which operation should be performed.
268  */
269 #define LDLM_CB_BLOCKING    1
270 #define LDLM_CB_CANCELING   2
271
272 /**
273  * \name Lock Compatibility Matrix.
274  *
275  * A lock has both a type (extent, flock, inode bits, or plain) and a mode.
276  * Lock types are described in their respective implementation files:
277  * ldlm_{extent,flock,inodebits,plain}.c.
278  *
279  * There are six lock modes along with a compatibility matrix to indicate if
280  * two locks are compatible.
281  *
282  * - EX: Exclusive mode. Before a new file is created, MDS requests EX lock
283  *   on the parent.
284  * - PW: Protective Write (normal write) mode. When a client requests a write
285  *   lock from an OST, a lock with PW mode will be issued.
286  * - PR: Protective Read (normal read) mode. When a client requests a read from
287  *   an OST, a lock with PR mode will be issued. Also, if the client opens a
288  *   file for execution, it is granted a lock with PR mode.
289  * - CW: Concurrent Write mode. The type of lock that the MDS grants if a client
290  *   requests a write lock during a file open operation.
291  * - CR Concurrent Read mode. When a client performs a path lookup, MDS grants
292  *   an inodebit lock with the CR mode on the intermediate path component.
293  * - NL Null mode.
294  *
295  * <PRE>
296  *       NL  CR  CW  PR  PW  EX
297  *  NL    1   1   1   1   1   1
298  *  CR    1   1   1   1   1   0
299  *  CW    1   1   1   0   0   0
300  *  PR    1   1   0   1   0   0
301  *  PW    1   1   0   0   0   0
302  *  EX    1   0   0   0   0   0
303  * </PRE>
304  */
305 /** @{ */
306 #define LCK_COMPAT_EX  LCK_NL
307 #define LCK_COMPAT_PW  (LCK_COMPAT_EX | LCK_CR)
308 #define LCK_COMPAT_PR  (LCK_COMPAT_PW | LCK_PR)
309 #define LCK_COMPAT_CW  (LCK_COMPAT_PW | LCK_CW)
310 #define LCK_COMPAT_CR  (LCK_COMPAT_CW | LCK_PR | LCK_PW)
311 #define LCK_COMPAT_NL  (LCK_COMPAT_CR | LCK_EX | LCK_GROUP)
312 #define LCK_COMPAT_GROUP  (LCK_GROUP | LCK_NL)
313 #define LCK_COMPAT_COS (LCK_COS)
314 /** @} Lock Compatibility Matrix */
315
316 extern ldlm_mode_t lck_compat_array[];
317
318 static inline void lockmode_verify(ldlm_mode_t mode)
319 {
320        LASSERT(mode > LCK_MINMODE && mode < LCK_MAXMODE);
321 }
322
323 static inline int lockmode_compat(ldlm_mode_t exist_mode, ldlm_mode_t new_mode)
324 {
325        return (lck_compat_array[exist_mode] & new_mode);
326 }
327
328 /*
329  *
330  * cluster name spaces
331  *
332  */
333
334 #define DLM_OST_NAMESPACE 1
335 #define DLM_MDS_NAMESPACE 2
336
337 /* XXX
338    - do we just separate this by security domains and use a prefix for
339      multiple namespaces in the same domain?
340    -
341 */
342
343 /**
344  * Locking rules for LDLM:
345  *
346  * lr_lock
347  *
348  * lr_lock
349  *     waiting_locks_spinlock
350  *
351  * lr_lock
352  *     led_lock
353  *
354  * lr_lock
355  *     ns_lock
356  *
357  * lr_lvb_mutex
358  *     lr_lock
359  *
360  */
361
362 struct ldlm_pool;
363 struct ldlm_lock;
364 struct ldlm_resource;
365 struct ldlm_namespace;
366
367 /**
368  * Operations on LDLM pools.
369  * LDLM pool is a pool of locks in the namespace without any implicitly
370  * specified limits.
371  * Locks in the pool are organized in LRU.
372  * Local memory pressure or server instructions (e.g. mempressure on server)
373  * can trigger freeing of locks from the pool
374  */
375 struct ldlm_pool_ops {
376         /** Recalculate pool \a pl usage */
377         int (*po_recalc)(struct ldlm_pool *pl);
378         /** Cancel at least \a nr locks from pool \a pl */
379         int (*po_shrink)(struct ldlm_pool *pl, int nr,
380                          unsigned int gfp_mask);
381         int (*po_setup)(struct ldlm_pool *pl, int limit);
382 };
383
384 /** One second for pools thread check interval. Each pool has own period. */
385 #define LDLM_POOLS_THREAD_PERIOD (1)
386
387 /** ~6% margin for modest pools. See ldlm_pool.c for details. */
388 #define LDLM_POOLS_MODEST_MARGIN_SHIFT (4)
389
390 /** Default recalc period for server side pools in sec. */
391 #define LDLM_POOL_SRV_DEF_RECALC_PERIOD (1)
392
393 /** Default recalc period for client side pools in sec. */
394 #define LDLM_POOL_CLI_DEF_RECALC_PERIOD (10)
395
396 /**
397  * LDLM pool structure to track granted locks.
398  * For purposes of determining when to release locks on e.g. memory pressure.
399  * This feature is commonly referred to as lru_resize.
400  */
401 struct ldlm_pool {
402         /** Pool proc directory. */
403         cfs_proc_dir_entry_t    *pl_proc_dir;
404         /** Pool name, must be long enough to hold compound proc entry name. */
405         char                    pl_name[100];
406         /** Lock for protecting SLV/CLV updates. */
407         spinlock_t              pl_lock;
408         /** Number of allowed locks in in pool, both, client and server side. */
409         cfs_atomic_t            pl_limit;
410         /** Number of granted locks in */
411         cfs_atomic_t            pl_granted;
412         /** Grant rate per T. */
413         cfs_atomic_t            pl_grant_rate;
414         /** Cancel rate per T. */
415         cfs_atomic_t            pl_cancel_rate;
416         /** Server lock volume (SLV). Protected by pl_lock. */
417         __u64                   pl_server_lock_volume;
418         /** Current biggest client lock volume. Protected by pl_lock. */
419         __u64                   pl_client_lock_volume;
420         /** Lock volume factor. SLV on client is calculated as following:
421          *  server_slv * lock_volume_factor. */
422         cfs_atomic_t            pl_lock_volume_factor;
423         /** Time when last SLV from server was obtained. */
424         time_t                  pl_recalc_time;
425         /** Recalculation period for pool. */
426         time_t                  pl_recalc_period;
427         /** Recalculation and shrink operations. */
428         struct ldlm_pool_ops    *pl_ops;
429         /** Number of planned locks for next period. */
430         int                     pl_grant_plan;
431         /** Pool statistics. */
432         struct lprocfs_stats    *pl_stats;
433 };
434
435 typedef int (*ldlm_res_policy)(struct ldlm_namespace *, struct ldlm_lock **,
436                                void *req_cookie, ldlm_mode_t mode, __u64 flags,
437                                void *data);
438
439 typedef int (*ldlm_cancel_for_recovery)(struct ldlm_lock *lock);
440
441 /**
442  * LVB operations.
443  * LVB is Lock Value Block. This is a special opaque (to LDLM) value that could
444  * be associated with an LDLM lock and transferred from client to server and
445  * back.
446  *
447  * Currently LVBs are used by:
448  *  - OSC-OST code to maintain current object size/times
449  *  - layout lock code to return the layout when the layout lock is granted
450  */
451 struct ldlm_valblock_ops {
452         int (*lvbo_init)(struct ldlm_resource *res);
453         int (*lvbo_update)(struct ldlm_resource *res,
454                            struct ptlrpc_request *r,
455                            int increase);
456         int (*lvbo_free)(struct ldlm_resource *res);
457         /* Return size of lvb data appropriate RPC size can be reserved */
458         int (*lvbo_size)(struct ldlm_lock *lock);
459         /* Called to fill in lvb data to RPC buffer @buf */
460         int (*lvbo_fill)(struct ldlm_lock *lock, void *buf, int buflen);
461 };
462
463 /**
464  * LDLM pools related, type of lock pool in the namespace.
465  * Greedy means release cached locks aggressively
466  */
467 typedef enum {
468         LDLM_NAMESPACE_GREEDY = 1 << 0,
469         LDLM_NAMESPACE_MODEST = 1 << 1
470 } ldlm_appetite_t;
471
472 /**
473  * Default values for the "max_nolock_size", "contention_time" and
474  * "contended_locks" namespace tunables.
475  */
476 #define NS_DEFAULT_MAX_NOLOCK_BYTES 0
477 #define NS_DEFAULT_CONTENTION_SECONDS 2
478 #define NS_DEFAULT_CONTENDED_LOCKS 32
479
480 struct ldlm_ns_bucket {
481         /** back pointer to namespace */
482         struct ldlm_namespace      *nsb_namespace;
483         /**
484          * Estimated lock callback time.  Used by adaptive timeout code to
485          * avoid spurious client evictions due to unresponsiveness when in
486          * fact the network or overall system load is at fault
487          */
488         struct adaptive_timeout     nsb_at_estimate;
489 };
490
491 enum {
492         /** LDLM namespace lock stats */
493         LDLM_NSS_LOCKS          = 0,
494         LDLM_NSS_LAST
495 };
496
497 typedef enum {
498         /** invalide type */
499         LDLM_NS_TYPE_UNKNOWN    = 0,
500         /** mdc namespace */
501         LDLM_NS_TYPE_MDC,
502         /** mds namespace */
503         LDLM_NS_TYPE_MDT,
504         /** osc namespace */
505         LDLM_NS_TYPE_OSC,
506         /** ost namespace */
507         LDLM_NS_TYPE_OST,
508         /** mgc namespace */
509         LDLM_NS_TYPE_MGC,
510         /** mgs namespace */
511         LDLM_NS_TYPE_MGT,
512 } ldlm_ns_type_t;
513
514 /**
515  * LDLM Namespace.
516  *
517  * Namespace serves to contain locks related to a particular service.
518  * There are two kinds of namespaces:
519  * - Server namespace has knowledge of all locks and is therefore authoritative
520  *   to make decisions like what locks could be granted and what conflicts
521  *   exist during new lock enqueue.
522  * - Client namespace only has limited knowledge about locks in the namespace,
523  *   only seeing locks held by the client.
524  *
525  * Every Lustre service has one server namespace present on the server serving
526  * that service. Every client connected to the service has a client namespace
527  * for it.
528  * Every lock obtained by client in that namespace is actually represented by
529  * two in-memory locks. One on the server and one on the client. The locks are
530  * linked by a special cookie by which one node can tell to the other which lock
531  * it actually means during communications. Such locks are called remote locks.
532  * The locks held by server only without any reference to a client are called
533  * local locks.
534  */
535 struct ldlm_namespace {
536         /** Backward link to OBD, required for LDLM pool to store new SLV. */
537         struct obd_device       *ns_obd;
538
539         /** Flag indicating if namespace is on client instead of server */
540         ldlm_side_t             ns_client;
541
542         /** Resource hash table for namespace. */
543         cfs_hash_t              *ns_rs_hash;
544
545         /** serialize */
546         spinlock_t              ns_lock;
547
548         /** big refcount (by bucket) */
549         cfs_atomic_t            ns_bref;
550
551         /**
552          * Namespace connect flags supported by server (may be changed via
553          * /proc, LRU resize may be disabled/enabled).
554          */
555         __u64                   ns_connect_flags;
556
557         /** Client side original connect flags supported by server. */
558         __u64                   ns_orig_connect_flags;
559
560         /**
561          * Position in global namespace list linking all namespaces on
562          * the node.
563          */
564         cfs_list_t              ns_list_chain;
565
566         /**
567          * List of unused locks for this namespace. This list is also called
568          * LRU lock list.
569          * Unused locks are locks with zero reader/writer reference counts.
570          * This list is only used on clients for lock caching purposes.
571          * When we want to release some locks voluntarily or if server wants
572          * us to release some locks due to e.g. memory pressure, we take locks
573          * to release from the head of this list.
574          * Locks are linked via l_lru field in \see struct ldlm_lock.
575          */
576         cfs_list_t              ns_unused_list;
577         /** Number of locks in the LRU list above */
578         int                     ns_nr_unused;
579
580         /**
581          * Maximum number of locks permitted in the LRU. If 0, means locks
582          * are managed by pools and there is no preset limit, rather it is all
583          * controlled by available memory on this client and on server.
584          */
585         unsigned int            ns_max_unused;
586         /** Maximum allowed age (last used time) for locks in the LRU */
587         unsigned int            ns_max_age;
588         /**
589          * Server only: number of times we evicted clients due to lack of reply
590          * to ASTs.
591          */
592         unsigned int            ns_timeouts;
593         /**
594          * Number of seconds since the file change time after which the
595          * MDT will return an UPDATE lock along with a LOOKUP lock.
596          * This allows the client to start caching negative dentries
597          * for a directory and may save an RPC for a later stat.
598          */
599         unsigned int            ns_ctime_age_limit;
600
601         /**
602          * Used to rate-limit ldlm_namespace_dump calls.
603          * \see ldlm_namespace_dump. Increased by 10 seconds every time
604          * it is called.
605          */
606         cfs_time_t              ns_next_dump;
607
608         /** "policy" function that does actual lock conflict determination */
609         ldlm_res_policy         ns_policy;
610
611         /**
612          * LVB operations for this namespace.
613          * \see struct ldlm_valblock_ops
614          */
615         struct ldlm_valblock_ops *ns_lvbo;
616
617         /**
618          * Used by filter code to store pointer to OBD of the service.
619          * Should be dropped in favor of \a ns_obd
620          */
621         void                    *ns_lvbp;
622
623         /**
624          * Wait queue used by __ldlm_namespace_free. Gets woken up every time
625          * a resource is removed.
626          */
627         cfs_waitq_t             ns_waitq;
628         /** LDLM pool structure for this namespace */
629         struct ldlm_pool        ns_pool;
630         /** Definition of how eagerly unused locks will be released from LRU */
631         ldlm_appetite_t         ns_appetite;
632
633         /**
634          * If more than \a ns_contended_locks are found, the resource is
635          * considered to be contended. Lock enqueues might specify that no
636          * contended locks should be granted
637          */
638         unsigned                ns_contended_locks;
639
640         /**
641          * The resources in this namespace remember contended state during
642          * \a ns_contention_time, in seconds.
643          */
644         unsigned                ns_contention_time;
645
646         /**
647          * Limit size of contended extent locks, in bytes.
648          * If extended lock is requested for more then this many bytes and
649          * caller instructs us not to grant contended locks, we would disregard
650          * such a request.
651          */
652         unsigned                ns_max_nolock_size;
653
654         /** Limit of parallel AST RPC count. */
655         unsigned                ns_max_parallel_ast;
656
657         /** Callback to cancel locks before replaying it during recovery. */
658         ldlm_cancel_for_recovery ns_cancel_for_recovery;
659
660         /** LDLM lock stats */
661         struct lprocfs_stats    *ns_stats;
662
663         /**
664          * Flag to indicate namespace is being freed. Used to determine if
665          * recalculation of LDLM pool statistics should be skipped.
666          */
667         unsigned                ns_stopping:1;
668 };
669
670 /**
671  * Returns 1 if namespace \a ns is a client namespace.
672  */
673 static inline int ns_is_client(struct ldlm_namespace *ns)
674 {
675         LASSERT(ns != NULL);
676         LASSERT(!(ns->ns_client & ~(LDLM_NAMESPACE_CLIENT |
677                                     LDLM_NAMESPACE_SERVER)));
678         LASSERT(ns->ns_client == LDLM_NAMESPACE_CLIENT ||
679                 ns->ns_client == LDLM_NAMESPACE_SERVER);
680         return ns->ns_client == LDLM_NAMESPACE_CLIENT;
681 }
682
683 /**
684  * Returns 1 if namespace \a ns is a server namespace.
685  */
686 static inline int ns_is_server(struct ldlm_namespace *ns)
687 {
688         LASSERT(ns != NULL);
689         LASSERT(!(ns->ns_client & ~(LDLM_NAMESPACE_CLIENT |
690                                     LDLM_NAMESPACE_SERVER)));
691         LASSERT(ns->ns_client == LDLM_NAMESPACE_CLIENT ||
692                 ns->ns_client == LDLM_NAMESPACE_SERVER);
693         return ns->ns_client == LDLM_NAMESPACE_SERVER;
694 }
695
696 /**
697  * Returns 1 if namespace \a ns supports early lock cancel (ELC).
698  */
699 static inline int ns_connect_cancelset(struct ldlm_namespace *ns)
700 {
701         LASSERT(ns != NULL);
702         return !!(ns->ns_connect_flags & OBD_CONNECT_CANCELSET);
703 }
704
705 /**
706  * Returns 1 if this namespace supports lru_resize.
707  */
708 static inline int ns_connect_lru_resize(struct ldlm_namespace *ns)
709 {
710         LASSERT(ns != NULL);
711         return !!(ns->ns_connect_flags & OBD_CONNECT_LRU_RESIZE);
712 }
713
714 static inline void ns_register_cancel(struct ldlm_namespace *ns,
715                                       ldlm_cancel_for_recovery arg)
716 {
717         LASSERT(ns != NULL);
718         ns->ns_cancel_for_recovery = arg;
719 }
720
721 struct ldlm_lock;
722
723 /** Type for blocking callback function of a lock. */
724 typedef int (*ldlm_blocking_callback)(struct ldlm_lock *lock,
725                                       struct ldlm_lock_desc *new, void *data,
726                                       int flag);
727 /** Type for completion callback function of a lock. */
728 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, __u64 flags,
729                                         void *data);
730 /** Type for glimpse callback function of a lock. */
731 typedef int (*ldlm_glimpse_callback)(struct ldlm_lock *lock, void *data);
732 /** Type for weight callback function of a lock. */
733 typedef unsigned long (*ldlm_weigh_callback)(struct ldlm_lock *lock);
734
735 /** Work list for sending GL ASTs to multiple locks. */
736 struct ldlm_glimpse_work {
737         struct ldlm_lock        *gl_lock; /* lock to glimpse */
738         cfs_list_t               gl_list; /* linkage to other gl work structs */
739         __u32                    gl_flags;/* see LDLM_GL_WORK_* below */
740         union ldlm_gl_desc      *gl_desc; /* glimpse descriptor to be packed in
741                                            * glimpse callback request */
742 };
743
744 /** The ldlm_glimpse_work is allocated on the stack and should not be freed. */
745 #define LDLM_GL_WORK_NOFREE 0x1
746
747 /** Interval node data for each LDLM_EXTENT lock. */
748 struct ldlm_interval {
749         struct interval_node    li_node;  /* node for tree management */
750         cfs_list_t              li_group; /* the locks which have the same
751                                            * policy - group of the policy */
752 };
753 #define to_ldlm_interval(n) container_of(n, struct ldlm_interval, li_node)
754
755 /**
756  * Interval tree for extent locks.
757  * The interval tree must be accessed under the resource lock.
758  * Interval trees are used for granted extent locks to speed up conflicts
759  * lookup. See ldlm/interval_tree.c for more details.
760  */
761 struct ldlm_interval_tree {
762         /** Tree size. */
763         int                     lit_size;
764         ldlm_mode_t             lit_mode;  /* lock mode */
765         struct interval_node    *lit_root; /* actual ldlm_interval */
766 };
767
768 /** Whether to track references to exports by LDLM locks. */
769 #define LUSTRE_TRACKS_LOCK_EXP_REFS (0)
770
771 /** Cancel flags. */
772 typedef enum {
773         LCF_ASYNC      = 0x1, /* Cancel locks asynchronously. */
774         LCF_LOCAL      = 0x2, /* Cancel locks locally, not notifing server */
775         LCF_BL_AST     = 0x4, /* Cancel locks marked as LDLM_FL_BL_AST
776                                * in the same RPC */
777 } ldlm_cancel_flags_t;
778
779 struct ldlm_flock {
780         __u64 start;
781         __u64 end;
782         __u64 owner;
783         __u64 blocking_owner;
784         struct obd_export *blocking_export;
785         /* Protected by the hash lock */
786         __u32 blocking_refs;
787         __u32 pid;
788 };
789
790 typedef union {
791         struct ldlm_extent l_extent;
792         struct ldlm_flock l_flock;
793         struct ldlm_inodebits l_inodebits;
794 } ldlm_policy_data_t;
795
796 void ldlm_convert_policy_to_wire(ldlm_type_t type,
797                                  const ldlm_policy_data_t *lpolicy,
798                                  ldlm_wire_policy_data_t *wpolicy);
799 void ldlm_convert_policy_to_local(struct obd_export *exp, ldlm_type_t type,
800                                   const ldlm_wire_policy_data_t *wpolicy,
801                                   ldlm_policy_data_t *lpolicy);
802
803 enum lvb_type {
804         LVB_T_NONE      = 0,
805         LVB_T_OST       = 1,
806         LVB_T_LQUOTA    = 2,
807         LVB_T_LAYOUT    = 3,
808 };
809
810 /**
811  * LDLM lock structure
812  *
813  * Represents a single LDLM lock and its state in memory. Each lock is
814  * associated with a single ldlm_resource, the object which is being
815  * locked. There may be multiple ldlm_locks on a single resource,
816  * depending on the lock type and whether the locks are conflicting or
817  * not.
818  */
819 struct ldlm_lock {
820         /**
821          * Local lock handle.
822          * When remote side wants to tell us about a lock, they address
823          * it by this opaque handle.  The handle does not hold a
824          * reference on the ldlm_lock, so it can be safely passed to
825          * other threads or nodes. When the lock needs to be accessed
826          * from the handle, it is looked up again in the lock table, and
827          * may no longer exist.
828          *
829          * Must be first in the structure.
830          */
831         struct portals_handle   l_handle;
832         /**
833          * Lock reference count.
834          * This is how many users have pointers to actual structure, so that
835          * we do not accidentally free lock structure that is in use.
836          */
837         cfs_atomic_t            l_refc;
838         /**
839          * Internal spinlock protects l_resource.  We should hold this lock
840          * first before taking res_lock.
841          */
842         spinlock_t              l_lock;
843         /**
844          * Pointer to actual resource this lock is in.
845          * ldlm_lock_change_resource() can change this.
846          */
847         struct ldlm_resource    *l_resource;
848         /**
849          * List item for client side LRU list.
850          * Protected by ns_lock in struct ldlm_namespace.
851          */
852         cfs_list_t              l_lru;
853         /**
854          * Linkage to resource's lock queues according to current lock state.
855          * (could be granted, waiting or converting)
856          * Protected by lr_lock in struct ldlm_resource.
857          */
858         cfs_list_t              l_res_link;
859         /**
860          * Tree node for ldlm_extent.
861          */
862         struct ldlm_interval    *l_tree_node;
863         /**
864          * Per export hash of locks.
865          * Protected by per-bucket exp->exp_lock_hash locks.
866          */
867         cfs_hlist_node_t        l_exp_hash;
868         /**
869          * Per export hash of flock locks.
870          * Protected by per-bucket exp->exp_flock_hash locks.
871          */
872         cfs_hlist_node_t        l_exp_flock_hash;
873         /**
874          * Requested mode.
875          * Protected by lr_lock.
876          */
877         ldlm_mode_t             l_req_mode;
878         /**
879          * Granted mode, also protected by lr_lock.
880          */
881         ldlm_mode_t             l_granted_mode;
882         /** Lock completion handler pointer. Called when lock is granted. */
883         ldlm_completion_callback l_completion_ast;
884         /**
885          * Lock blocking AST handler pointer.
886          * It plays two roles:
887          * - as a notification of an attempt to queue a conflicting lock (once)
888          * - as a notification when the lock is being cancelled.
889          *
890          * As such it's typically called twice: once for the initial conflict
891          * and then once more when the last user went away and the lock is
892          * cancelled (could happen recursively).
893          */
894         ldlm_blocking_callback  l_blocking_ast;
895         /**
896          * Lock glimpse handler.
897          * Glimpse handler is used to obtain LVB updates from a client by
898          * server
899          */
900         ldlm_glimpse_callback   l_glimpse_ast;
901
902         /** XXX apparently unused "weight" handler. To be removed? */
903         ldlm_weigh_callback     l_weigh_ast;
904
905         /**
906          * Lock export.
907          * This is a pointer to actual client export for locks that were granted
908          * to clients. Used server-side.
909          */
910         struct obd_export       *l_export;
911         /**
912          * Lock connection export.
913          * Pointer to server export on a client.
914          */
915         struct obd_export       *l_conn_export;
916
917         /**
918          * Remote lock handle.
919          * If the lock is remote, this is the handle of the other side lock
920          * (l_handle)
921          */
922         struct lustre_handle    l_remote_handle;
923
924         /**
925          * Representation of private data specific for a lock type.
926          * Examples are: extent range for extent lock or bitmask for ibits locks
927          */
928         ldlm_policy_data_t      l_policy_data;
929
930         /**
931          * Lock state flags.
932          * Like whenever we receive any blocking requests for this lock, etc.
933          * Protected by lr_lock.
934          */
935         __u64                   l_flags;
936         /**
937          * Lock r/w usage counters.
938          * Protected by lr_lock.
939          */
940         __u32                   l_readers;
941         __u32                   l_writers;
942         /**
943          * If the lock is granted, a process sleeps on this waitq to learn when
944          * it's no longer in use.  If the lock is not granted, a process sleeps
945          * on this waitq to learn when it becomes granted.
946          */
947         cfs_waitq_t             l_waitq;
948
949         /**
950          * Seconds. It will be updated if there is any activity related to
951          * the lock, e.g. enqueue the lock or send blocking AST.
952          */
953         cfs_time_t              l_last_activity;
954
955         /**
956          * Time last used by e.g. being matched by lock match.
957          * Jiffies. Should be converted to time if needed.
958          */
959         cfs_time_t              l_last_used;
960
961         /** Originally requested extent for the extent lock. */
962         struct ldlm_extent      l_req_extent;
963
964         unsigned int            l_failed:1,
965         /**
966          * Set for locks that were removed from class hash table and will be
967          * destroyed when last reference to them is released. Set by
968          * ldlm_lock_destroy_internal().
969          *
970          * Protected by lock and resource locks.
971          */
972                                 l_destroyed:1,
973         /*
974          * it's set in lock_res_and_lock() and unset in unlock_res_and_lock().
975          *
976          * NB: compared with check_res_locked(), checking this bit is cheaper.
977          * Also, spin_is_locked() is deprecated for kernel code; one reason is
978          * because it works only for SMP so user needs to add extra macros like
979          * LASSERT_SPIN_LOCKED for uniprocessor kernels.
980          */
981                                 l_res_locked:1,
982         /*
983          * It's set once we call ldlm_add_waiting_lock_res_locked()
984          * to start the lock-timeout timer and it will never be reset.
985          *
986          * Protected by lock_res_and_lock().
987          */
988                                 l_waited:1,
989         /** Flag whether this is a server namespace lock. */
990                                 l_ns_srv:1;
991
992         /*
993          * Client-side-only members.
994          */
995
996         enum lvb_type         l_lvb_type;
997
998         /**
999          * Temporary storage for a LVB received during an enqueue operation.
1000          */
1001         __u32                   l_lvb_len;
1002         void                    *l_lvb_data;
1003
1004         /** Private storage for lock user. Opaque to LDLM. */
1005         void                    *l_ast_data;
1006
1007         /*
1008          * Server-side-only members.
1009          */
1010
1011         /**
1012          * Connection cookie for the client originating the operation.
1013          * Used by Commit on Share (COS) code. Currently only used for
1014          * inodebits locks on MDS.
1015          */
1016         __u64                   l_client_cookie;
1017
1018         /**
1019          * List item for locks waiting for cancellation from clients.
1020          * The lists this could be linked into are:
1021          * waiting_locks_list (protected by waiting_locks_spinlock),
1022          * then if the lock timed out, it is moved to
1023          * expired_lock_thread.elt_expired_locks for further processing.
1024          * Protected by elt_lock.
1025          */
1026         cfs_list_t              l_pending_chain;
1027
1028         /**
1029          * Set when lock is sent a blocking AST. Time in seconds when timeout
1030          * is reached and client holding this lock could be evicted.
1031          * This timeout could be further extended by e.g. certain IO activity
1032          * under this lock.
1033          * \see ost_rw_prolong_locks
1034          */
1035         cfs_time_t              l_callback_timeout;
1036
1037         /** Local PID of process which created this lock. */
1038         __u32                   l_pid;
1039
1040         /**
1041          * Number of times blocking AST was sent for this lock.
1042          * This is for debugging. Valid values are 0 and 1, if there is an
1043          * attempt to send blocking AST more than once, an assertion would be
1044          * hit. \see ldlm_work_bl_ast_lock
1045          */
1046         int                     l_bl_ast_run;
1047         /** List item ldlm_add_ast_work_item() for case of blocking ASTs. */
1048         cfs_list_t              l_bl_ast;
1049         /** List item ldlm_add_ast_work_item() for case of completion ASTs. */
1050         cfs_list_t              l_cp_ast;
1051         /** For ldlm_add_ast_work_item() for "revoke" AST used in COS. */
1052         cfs_list_t              l_rk_ast;
1053
1054         /**
1055          * Pointer to a conflicting lock that caused blocking AST to be sent
1056          * for this lock
1057          */
1058         struct ldlm_lock        *l_blocking_lock;
1059
1060         /**
1061          * Protected by lr_lock, linkages to "skip lists".
1062          * For more explanations of skip lists see ldlm/ldlm_inodebits.c
1063          */
1064         cfs_list_t              l_sl_mode;
1065         cfs_list_t              l_sl_policy;
1066
1067         /** Reference tracking structure to debug leaked locks. */
1068         struct lu_ref           l_reference;
1069 #if LUSTRE_TRACKS_LOCK_EXP_REFS
1070         /* Debugging stuff for bug 20498, for tracking export references. */
1071         /** number of export references taken */
1072         int                     l_exp_refs_nr;
1073         /** link all locks referencing one export */
1074         cfs_list_t              l_exp_refs_link;
1075         /** referenced export object */
1076         struct obd_export       *l_exp_refs_target;
1077 #endif
1078         /**
1079          * export blocking dlm lock list, protected by
1080          * l_export->exp_bl_list_lock.
1081          * Lock order of waiting_lists_spinlock, exp_bl_list_lock and res lock
1082          * is: res lock -> exp_bl_list_lock -> wanting_lists_spinlock.
1083          */
1084         cfs_list_t              l_exp_list;
1085 };
1086
1087 /**
1088  * LDLM resource description.
1089  * Basically, resource is a representation for a single object.
1090  * Object has a name which is currently 4 64-bit integers. LDLM user is
1091  * responsible for creation of a mapping between objects it wants to be
1092  * protected and resource names.
1093  *
1094  * A resource can only hold locks of a single lock type, though there may be
1095  * multiple ldlm_locks on a single resource, depending on the lock type and
1096  * whether the locks are conflicting or not.
1097  */
1098 struct ldlm_resource {
1099         struct ldlm_ns_bucket   *lr_ns_bucket;
1100
1101         /**
1102          * List item for list in namespace hash.
1103          * protected by ns_lock
1104          */
1105         cfs_hlist_node_t        lr_hash;
1106
1107         /** Spinlock to protect locks under this resource. */
1108         spinlock_t              lr_lock;
1109
1110         /**
1111          * protected by lr_lock
1112          * @{ */
1113         /** List of locks in granted state */
1114         cfs_list_t              lr_granted;
1115         /** List of locks waiting to change their granted mode (converted) */
1116         cfs_list_t              lr_converting;
1117         /**
1118          * List of locks that could not be granted due to conflicts and
1119          * that are waiting for conflicts to go away */
1120         cfs_list_t              lr_waiting;
1121         /** @} */
1122
1123         /* XXX No longer needed? Remove ASAP */
1124         ldlm_mode_t             lr_most_restr;
1125
1126         /** Type of locks this resource can hold. Only one type per resource. */
1127         ldlm_type_t             lr_type; /* LDLM_{PLAIN,EXTENT,FLOCK,IBITS} */
1128
1129         /** Resource name */
1130         struct ldlm_res_id      lr_name;
1131         /** Reference count for this resource */
1132         cfs_atomic_t            lr_refcount;
1133
1134         /**
1135          * Interval trees (only for extent locks) for all modes of this resource
1136          */
1137         struct ldlm_interval_tree lr_itree[LCK_MODE_NUM];
1138
1139         /**
1140          * Server-side-only lock value block elements.
1141          * To serialize lvbo_init.
1142          */
1143         struct mutex            lr_lvb_mutex;
1144         __u32                   lr_lvb_len;
1145         /** protected by lr_lock */
1146         void                    *lr_lvb_data;
1147
1148         /** When the resource was considered as contended. */
1149         cfs_time_t              lr_contention_time;
1150         /** List of references to this resource. For debugging. */
1151         struct lu_ref           lr_reference;
1152
1153         struct inode            *lr_lvb_inode;
1154 };
1155
1156 static inline char *
1157 ldlm_ns_name(struct ldlm_namespace *ns)
1158 {
1159         return ns->ns_rs_hash->hs_name;
1160 }
1161
1162 static inline struct ldlm_namespace *
1163 ldlm_res_to_ns(struct ldlm_resource *res)
1164 {
1165         return res->lr_ns_bucket->nsb_namespace;
1166 }
1167
1168 static inline struct ldlm_namespace *
1169 ldlm_lock_to_ns(struct ldlm_lock *lock)
1170 {
1171         return ldlm_res_to_ns(lock->l_resource);
1172 }
1173
1174 static inline char *
1175 ldlm_lock_to_ns_name(struct ldlm_lock *lock)
1176 {
1177         return ldlm_ns_name(ldlm_lock_to_ns(lock));
1178 }
1179
1180 static inline struct adaptive_timeout *
1181 ldlm_lock_to_ns_at(struct ldlm_lock *lock)
1182 {
1183         return &lock->l_resource->lr_ns_bucket->nsb_at_estimate;
1184 }
1185
1186 static inline int ldlm_lvbo_init(struct ldlm_resource *res)
1187 {
1188         struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1189
1190         if (ns->ns_lvbo != NULL && ns->ns_lvbo->lvbo_init != NULL)
1191                 return ns->ns_lvbo->lvbo_init(res);
1192
1193         return 0;
1194 }
1195
1196 static inline int ldlm_lvbo_size(struct ldlm_lock *lock)
1197 {
1198         struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
1199
1200         if (ns->ns_lvbo != NULL && ns->ns_lvbo->lvbo_size != NULL)
1201                 return ns->ns_lvbo->lvbo_size(lock);
1202
1203         return 0;
1204 }
1205
1206 static inline int ldlm_lvbo_fill(struct ldlm_lock *lock, void *buf, int len)
1207 {
1208         struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
1209
1210         if (ns->ns_lvbo != NULL) {
1211                 LASSERT(ns->ns_lvbo->lvbo_fill != NULL);
1212                 return ns->ns_lvbo->lvbo_fill(lock, buf, len);
1213         }
1214         return 0;
1215 }
1216
1217 struct ldlm_ast_work {
1218         struct ldlm_lock      *w_lock;
1219         int                    w_blocking;
1220         struct ldlm_lock_desc  w_desc;
1221         cfs_list_t             w_list;
1222         int                    w_flags;
1223         void                  *w_data;
1224         int                    w_datalen;
1225 };
1226
1227 /**
1228  * Common ldlm_enqueue parameters
1229  */
1230 struct ldlm_enqueue_info {
1231         __u32 ei_type;   /** Type of the lock being enqueued. */
1232         __u32 ei_mode;   /** Mode of the lock being enqueued. */
1233         void *ei_cb_bl;  /** blocking lock callback */
1234         void *ei_cb_cp;  /** lock completion callback */
1235         void *ei_cb_gl;  /** lock glimpse callback */
1236         void *ei_cb_wg;  /** lock weigh callback */
1237         void *ei_cbdata; /** Data to be passed into callbacks. */
1238 };
1239
1240 extern struct obd_ops ldlm_obd_ops;
1241
1242 extern char *ldlm_lockname[];
1243 extern char *ldlm_typename[];
1244 extern char *ldlm_it2str(int it);
1245
1246 /**
1247  * Just a fancy CDEBUG call with log level preset to LDLM_DEBUG.
1248  * For the cases where we do not have actual lock to print along
1249  * with a debugging message that is ldlm-related
1250  */
1251 #define LDLM_DEBUG_NOLOCK(format, a...)                 \
1252         CDEBUG(D_DLMTRACE, "### " format "\n" , ##a)
1253
1254 /**
1255  * Support function for lock information printing into debug logs.
1256  * \see LDLM_DEBUG
1257  */
1258 #ifdef LIBCFS_DEBUG
1259 #define ldlm_lock_debug(msgdata, mask, cdls, lock, fmt, a...) do {      \
1260         CFS_CHECK_STACK(msgdata, mask, cdls);                           \
1261                                                                         \
1262         if (((mask) & D_CANTMASK) != 0 ||                               \
1263             ((libcfs_debug & (mask)) != 0 &&                            \
1264              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0))          \
1265                 _ldlm_lock_debug(lock, msgdata, fmt, ##a);              \
1266 } while(0)
1267
1268 void _ldlm_lock_debug(struct ldlm_lock *lock,
1269                       struct libcfs_debug_msg_data *data,
1270                       const char *fmt, ...)
1271         __attribute__ ((format (printf, 3, 4)));
1272
1273 /**
1274  * Rate-limited version of lock printing function.
1275  */
1276 #define LDLM_DEBUG_LIMIT(mask, lock, fmt, a...) do {                         \
1277         static cfs_debug_limit_state_t _ldlm_cdls;                           \
1278         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, &_ldlm_cdls);              \
1279         ldlm_lock_debug(&msgdata, mask, &_ldlm_cdls, lock, "### " fmt , ##a);\
1280 } while (0)
1281
1282 #define LDLM_ERROR(lock, fmt, a...) LDLM_DEBUG_LIMIT(D_ERROR, lock, fmt, ## a)
1283 #define LDLM_WARN(lock, fmt, a...)  LDLM_DEBUG_LIMIT(D_WARNING, lock, fmt, ## a)
1284
1285 /** Non-rate-limited lock printing function for debugging purposes. */
1286 #define LDLM_DEBUG(lock, fmt, a...)   do {                                  \
1287         if (likely(lock != NULL)) {                                         \
1288                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_DLMTRACE, NULL);      \
1289                 ldlm_lock_debug(&msgdata, D_DLMTRACE, NULL, lock,           \
1290                                 "### " fmt , ##a);                          \
1291         } else {                                                            \
1292                 LDLM_DEBUG_NOLOCK("no dlm lock: " fmt, ##a);                \
1293         }                                                                   \
1294 } while (0)
1295 #else /* !LIBCFS_DEBUG */
1296 # define LDLM_DEBUG_LIMIT(mask, lock, fmt, a...) ((void)0)
1297 # define LDLM_DEBUG(lock, fmt, a...) ((void)0)
1298 # define LDLM_ERROR(lock, fmt, a...) ((void)0)
1299 #endif
1300
1301 typedef int (*ldlm_processing_policy)(struct ldlm_lock *lock, __u64 *flags,
1302                                       int first_enq, ldlm_error_t *err,
1303                                       cfs_list_t *work_list);
1304
1305 /**
1306  * Return values for lock iterators.
1307  * Also used during deciding of lock grants and cancellations.
1308  */
1309 #define LDLM_ITER_CONTINUE 1 /* keep iterating */
1310 #define LDLM_ITER_STOP     2 /* stop iterating */
1311
1312 typedef int (*ldlm_iterator_t)(struct ldlm_lock *, void *);
1313 typedef int (*ldlm_res_iterator_t)(struct ldlm_resource *, void *);
1314
1315 /** \defgroup ldlm_iterator Lock iterators
1316  *
1317  * LDLM provides for a way to iterate through every lock on a resource or
1318  * namespace or every resource in a namespace.
1319  * @{ */
1320 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1321                           void *closure);
1322 void ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
1323                             void *closure);
1324 int ldlm_resource_iterate(struct ldlm_namespace *, const struct ldlm_res_id *,
1325                           ldlm_iterator_t iter, void *data);
1326 /** @} ldlm_iterator */
1327
1328 int ldlm_replay_locks(struct obd_import *imp);
1329
1330 /* ldlm_flock.c */
1331 int ldlm_flock_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data);
1332
1333 /* ldlm_extent.c */
1334 __u64 ldlm_extent_shift_kms(struct ldlm_lock *lock, __u64 old_kms);
1335
1336 struct ldlm_callback_suite {
1337         ldlm_completion_callback lcs_completion;
1338         ldlm_blocking_callback   lcs_blocking;
1339         ldlm_glimpse_callback    lcs_glimpse;
1340         ldlm_weigh_callback      lcs_weigh;
1341 };
1342
1343 /* ldlm_lockd.c */
1344 #ifdef HAVE_SERVER_SUPPORT
1345 /** \defgroup ldlm_srv_ast Server AST handlers
1346  * These are AST handlers used by server code.
1347  * Their property is that they are just preparing RPCs to be sent to clients.
1348  * @{
1349  */
1350 int ldlm_server_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
1351                              void *data, int flag);
1352 int ldlm_server_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data);
1353 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data);
1354 int ldlm_glimpse_locks(struct ldlm_resource *res, cfs_list_t *gl_work_list);
1355 /** @} ldlm_srv_ast */
1356
1357 /** \defgroup ldlm_handlers Server LDLM handlers
1358  * These are handler functions that should be called by "frontends" such as
1359  * MDT or OST to pass through LDLM requests to LDLM for handling
1360  * @{
1361  */
1362 int ldlm_handle_enqueue(struct ptlrpc_request *req, ldlm_completion_callback,
1363                         ldlm_blocking_callback, ldlm_glimpse_callback);
1364 int ldlm_handle_enqueue0(struct ldlm_namespace *ns, struct ptlrpc_request *req,
1365                          const struct ldlm_request *dlm_req,
1366                          const struct ldlm_callback_suite *cbs);
1367 int ldlm_handle_convert(struct ptlrpc_request *req);
1368 int ldlm_handle_convert0(struct ptlrpc_request *req,
1369                          const struct ldlm_request *dlm_req);
1370 int ldlm_handle_cancel(struct ptlrpc_request *req);
1371 int ldlm_request_cancel(struct ptlrpc_request *req,
1372                         const struct ldlm_request *dlm_req, int first);
1373 /** @} ldlm_handlers */
1374
1375 void ldlm_revoke_export_locks(struct obd_export *exp);
1376 #endif
1377 int ldlm_del_waiting_lock(struct ldlm_lock *lock);
1378 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout);
1379 int ldlm_get_ref(void);
1380 void ldlm_put_ref(void);
1381 int ldlm_init_export(struct obd_export *exp);
1382 void ldlm_destroy_export(struct obd_export *exp);
1383 struct ldlm_lock *ldlm_request_lock(struct ptlrpc_request *req);
1384
1385 /* ldlm_lock.c */
1386 #ifdef HAVE_SERVER_SUPPORT
1387 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res);
1388 #endif
1389 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg);
1390 void ldlm_lock2handle(const struct ldlm_lock *lock,
1391                       struct lustre_handle *lockh);
1392 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *, __u64 flags);
1393 void ldlm_cancel_callback(struct ldlm_lock *);
1394 int ldlm_lock_remove_from_lru(struct ldlm_lock *);
1395 int ldlm_lock_set_data(struct lustre_handle *, void *);
1396
1397 /**
1398  * Obtain a lock reference by its handle.
1399  */
1400 static inline struct ldlm_lock *ldlm_handle2lock(const struct lustre_handle *h)
1401 {
1402         return __ldlm_handle2lock(h, 0);
1403 }
1404
1405 #define LDLM_LOCK_REF_DEL(lock) \
1406         lu_ref_del(&lock->l_reference, "handle", cfs_current())
1407
1408 static inline struct ldlm_lock *
1409 ldlm_handle2lock_long(const struct lustre_handle *h, __u64 flags)
1410 {
1411         struct ldlm_lock *lock;
1412
1413         lock = __ldlm_handle2lock(h, flags);
1414         if (lock != NULL)
1415                 LDLM_LOCK_REF_DEL(lock);
1416         return lock;
1417 }
1418
1419 /**
1420  * Update Lock Value Block Operations (LVBO) on a resource taking into account
1421  * data from reqest \a r
1422  */
1423 static inline int ldlm_res_lvbo_update(struct ldlm_resource *res,
1424                                        struct ptlrpc_request *r, int increase)
1425 {
1426         if (ldlm_res_to_ns(res)->ns_lvbo &&
1427             ldlm_res_to_ns(res)->ns_lvbo->lvbo_update) {
1428                 return ldlm_res_to_ns(res)->ns_lvbo->lvbo_update(res, r,
1429                                                                  increase);
1430         }
1431         return 0;
1432 }
1433
1434 int ldlm_error2errno(ldlm_error_t error);
1435 ldlm_error_t ldlm_errno2error(int err_no); /* don't call it `errno': this
1436                                             * confuses user-space. */
1437 #if LUSTRE_TRACKS_LOCK_EXP_REFS
1438 void ldlm_dump_export_locks(struct obd_export *exp);
1439 #endif
1440
1441 /**
1442  * Release a temporary lock reference obtained by ldlm_handle2lock() or
1443  * __ldlm_handle2lock().
1444  */
1445 #define LDLM_LOCK_PUT(lock)                     \
1446 do {                                            \
1447         LDLM_LOCK_REF_DEL(lock);                \
1448         /*LDLM_DEBUG((lock), "put");*/          \
1449         ldlm_lock_put(lock);                    \
1450 } while (0)
1451
1452 /**
1453  * Release a lock reference obtained by some other means (see
1454  * LDLM_LOCK_PUT()).
1455  */
1456 #define LDLM_LOCK_RELEASE(lock)                 \
1457 do {                                            \
1458         /*LDLM_DEBUG((lock), "put");*/          \
1459         ldlm_lock_put(lock);                    \
1460 } while (0)
1461
1462 #define LDLM_LOCK_GET(lock)                     \
1463 ({                                              \
1464         ldlm_lock_get(lock);                    \
1465         /*LDLM_DEBUG((lock), "get");*/          \
1466         lock;                                   \
1467 })
1468
1469 #define ldlm_lock_list_put(head, member, count)                     \
1470 ({                                                                  \
1471         struct ldlm_lock *_lock, *_next;                            \
1472         int c = count;                                              \
1473         cfs_list_for_each_entry_safe(_lock, _next, head, member) {  \
1474                 if (c-- == 0)                                       \
1475                         break;                                      \
1476                 cfs_list_del_init(&_lock->member);                  \
1477                 LDLM_LOCK_RELEASE(_lock);                           \
1478         }                                                           \
1479         LASSERT(c <= 0);                                            \
1480 })
1481
1482 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
1483 void ldlm_lock_put(struct ldlm_lock *lock);
1484 void ldlm_lock_destroy(struct ldlm_lock *lock);
1485 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
1486 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode);
1487 int  ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode);
1488 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode);
1489 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode);
1490 void ldlm_lock_fail_match_locked(struct ldlm_lock *lock);
1491 void ldlm_lock_fail_match(struct ldlm_lock *lock);
1492 void ldlm_lock_allow_match(struct ldlm_lock *lock);
1493 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock);
1494 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, __u64 flags,
1495                             const struct ldlm_res_id *, ldlm_type_t type,
1496                             ldlm_policy_data_t *, ldlm_mode_t mode,
1497                             struct lustre_handle *, int unref);
1498 ldlm_mode_t ldlm_revalidate_lock_handle(struct lustre_handle *lockh,
1499                                         __u64 *bits);
1500 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1501                                         __u32 *flags);
1502 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode);
1503 void ldlm_lock_cancel(struct ldlm_lock *lock);
1504 void ldlm_reprocess_all(struct ldlm_resource *res);
1505 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns);
1506 void ldlm_lock_dump_handle(int level, struct lustre_handle *);
1507 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req);
1508
1509 /* resource.c */
1510 struct ldlm_namespace *
1511 ldlm_namespace_new(struct obd_device *obd, char *name,
1512                    ldlm_side_t client, ldlm_appetite_t apt,
1513                    ldlm_ns_type_t ns_type);
1514 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, __u64 flags);
1515 void ldlm_namespace_free(struct ldlm_namespace *ns,
1516                          struct obd_import *imp, int force);
1517 void ldlm_namespace_register(struct ldlm_namespace *ns, ldlm_side_t client);
1518 void ldlm_namespace_unregister(struct ldlm_namespace *ns, ldlm_side_t client);
1519 void ldlm_namespace_move_locked(struct ldlm_namespace *ns, ldlm_side_t client);
1520 struct ldlm_namespace *ldlm_namespace_first_locked(ldlm_side_t client);
1521 void ldlm_namespace_get(struct ldlm_namespace *ns);
1522 void ldlm_namespace_put(struct ldlm_namespace *ns);
1523 int ldlm_proc_setup(void);
1524 #ifdef LPROCFS
1525 void ldlm_proc_cleanup(void);
1526 #else
1527 static inline void ldlm_proc_cleanup(void) {}
1528 #endif
1529
1530 /* resource.c - internal */
1531 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
1532                                         struct ldlm_resource *parent,
1533                                         const struct ldlm_res_id *,
1534                                         ldlm_type_t type, int create);
1535 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
1536 int ldlm_resource_putref(struct ldlm_resource *res);
1537 void ldlm_resource_add_lock(struct ldlm_resource *res,
1538                             cfs_list_t *head,
1539                             struct ldlm_lock *lock);
1540 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
1541 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
1542 void ldlm_dump_all_namespaces(ldlm_side_t client, int level);
1543 void ldlm_namespace_dump(int level, struct ldlm_namespace *);
1544 void ldlm_resource_dump(int level, struct ldlm_resource *);
1545 int ldlm_lock_change_resource(struct ldlm_namespace *, struct ldlm_lock *,
1546                               const struct ldlm_res_id *);
1547
1548 #define LDLM_RESOURCE_ADDREF(res) do {                                  \
1549         lu_ref_add_atomic(&(res)->lr_reference, __FUNCTION__, cfs_current());  \
1550 } while (0)
1551
1552 #define LDLM_RESOURCE_DELREF(res) do {                                  \
1553         lu_ref_del(&(res)->lr_reference, __FUNCTION__, cfs_current());  \
1554 } while (0)
1555
1556 /* ldlm_request.c */
1557 int ldlm_expired_completion_wait(void *data);
1558 /** \defgroup ldlm_local_ast Default AST handlers for local locks
1559  * These AST handlers are typically used for server-side local locks and are
1560  * also used by client-side lock handlers to perform minimum level base
1561  * processing.
1562  * @{ */
1563 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock);
1564 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
1565                       void *data, int flag);
1566 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp);
1567 int ldlm_completion_ast_async(struct ldlm_lock *lock, __u64 flags, void *data);
1568 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data);
1569 /** @} ldlm_local_ast */
1570
1571 /** \defgroup ldlm_cli_api API to operate on locks from actual LDLM users.
1572  * These are typically used by client and server (*_local versions)
1573  * to obtain and release locks.
1574  * @{ */
1575 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
1576                      struct ldlm_enqueue_info *einfo,
1577                      const struct ldlm_res_id *res_id,
1578                      ldlm_policy_data_t const *policy, __u64 *flags,
1579                      void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
1580                      struct lustre_handle *lockh, int async);
1581 int ldlm_prep_enqueue_req(struct obd_export *exp,
1582                           struct ptlrpc_request *req,
1583                           cfs_list_t *cancels,
1584                           int count);
1585 int ldlm_prep_elc_req(struct obd_export *exp,
1586                       struct ptlrpc_request *req,
1587                       int version, int opc, int canceloff,
1588                       cfs_list_t *cancels, int count);
1589 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
1590                           ldlm_type_t type, __u8 with_policy, ldlm_mode_t mode,
1591                           __u64 *flags, void *lvb, __u32 lvb_len,
1592                           struct lustre_handle *lockh, int rc);
1593 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
1594                            const struct ldlm_res_id *res_id,
1595                            ldlm_type_t type, ldlm_policy_data_t *policy,
1596                            ldlm_mode_t mode, __u64 *flags,
1597                            ldlm_blocking_callback blocking,
1598                            ldlm_completion_callback completion,
1599                            ldlm_glimpse_callback glimpse,
1600                            void *data, __u32 lvb_len, enum lvb_type lvb_type,
1601                            const __u64 *client_cookie,
1602                            struct lustre_handle *lockh);
1603 int ldlm_server_ast(struct lustre_handle *lockh, struct ldlm_lock_desc *new,
1604                     void *data, __u32 data_len);
1605 int ldlm_cli_convert(struct lustre_handle *, int new_mode, __u32 *flags);
1606 int ldlm_cli_update_pool(struct ptlrpc_request *req);
1607 int ldlm_cli_cancel(struct lustre_handle *lockh);
1608 int ldlm_cli_cancel_unused(struct ldlm_namespace *, const struct ldlm_res_id *,
1609                            ldlm_cancel_flags_t flags, void *opaque);
1610 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1611                                     const struct ldlm_res_id *res_id,
1612                                     ldlm_policy_data_t *policy,
1613                                     ldlm_mode_t mode,
1614                                     ldlm_cancel_flags_t flags,
1615                                     void *opaque);
1616 int ldlm_cli_cancel_req(struct obd_export *exp, cfs_list_t *head,
1617                         int count, ldlm_cancel_flags_t flags);
1618 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1619                                cfs_list_t *cancels,
1620                                ldlm_policy_data_t *policy,
1621                                ldlm_mode_t mode, int lock_flags,
1622                                ldlm_cancel_flags_t cancel_flags, void *opaque);
1623 int ldlm_cli_cancel_list_local(cfs_list_t *cancels, int count,
1624                                ldlm_cancel_flags_t flags);
1625 int ldlm_cli_cancel_list(cfs_list_t *head, int count,
1626                          struct ptlrpc_request *req, ldlm_cancel_flags_t flags);
1627 /** @} ldlm_cli_api */
1628
1629 /* mds/handler.c */
1630 /* This has to be here because recursive inclusion sucks. */
1631 int intent_disposition(struct ldlm_reply *rep, int flag);
1632 void intent_set_disposition(struct ldlm_reply *rep, int flag);
1633
1634
1635 /* ioctls for trying requests */
1636 #define IOC_LDLM_TYPE                   'f'
1637 #define IOC_LDLM_MIN_NR                 40
1638
1639 #define IOC_LDLM_TEST                   _IOWR('f', 40, long)
1640 #define IOC_LDLM_DUMP                   _IOWR('f', 41, long)
1641 #define IOC_LDLM_REGRESS_START          _IOWR('f', 42, long)
1642 #define IOC_LDLM_REGRESS_STOP           _IOWR('f', 43, long)
1643 #define IOC_LDLM_MAX_NR                 43
1644
1645 /**
1646  * "Modes" of acquiring lock_res, necessary to tell lockdep that taking more
1647  * than one lock_res is dead-lock safe.
1648  */
1649 enum lock_res_type {
1650         LRT_NORMAL,
1651         LRT_NEW
1652 };
1653
1654 /** Lock resource. */
1655 static inline void lock_res(struct ldlm_resource *res)
1656 {
1657         spin_lock(&res->lr_lock);
1658 }
1659
1660 /** Lock resource with a way to instruct lockdep code about nestedness-safe. */
1661 static inline void lock_res_nested(struct ldlm_resource *res,
1662                                    enum lock_res_type mode)
1663 {
1664         spin_lock_nested(&res->lr_lock, mode);
1665 }
1666
1667 /** Unlock resource. */
1668 static inline void unlock_res(struct ldlm_resource *res)
1669 {
1670         spin_unlock(&res->lr_lock);
1671 }
1672
1673 /** Check if resource is already locked, assert if not. */
1674 static inline void check_res_locked(struct ldlm_resource *res)
1675 {
1676         LASSERT_SPIN_LOCKED(&res->lr_lock);
1677 }
1678
1679 struct ldlm_resource * lock_res_and_lock(struct ldlm_lock *lock);
1680 void unlock_res_and_lock(struct ldlm_lock *lock);
1681
1682 /* ldlm_pool.c */
1683 /** \defgroup ldlm_pools Various LDLM pool related functions
1684  * There are not used outside of ldlm.
1685  * @{
1686  */
1687 void ldlm_pools_recalc(ldlm_side_t client);
1688 int ldlm_pools_init(void);
1689 void ldlm_pools_fini(void);
1690
1691 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
1692                    int idx, ldlm_side_t client);
1693 int ldlm_pool_shrink(struct ldlm_pool *pl, int nr,
1694                      unsigned int gfp_mask);
1695 void ldlm_pool_fini(struct ldlm_pool *pl);
1696 int ldlm_pool_setup(struct ldlm_pool *pl, int limit);
1697 int ldlm_pool_recalc(struct ldlm_pool *pl);
1698 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl);
1699 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl);
1700 __u64 ldlm_pool_get_clv(struct ldlm_pool *pl);
1701 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl);
1702 void ldlm_pool_set_slv(struct ldlm_pool *pl, __u64 slv);
1703 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv);
1704 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit);
1705 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock);
1706 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock);
1707 /** @} */
1708
1709 #endif
1710 /** @} LDLM */