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