Whamcloud - gitweb
LU-16974 utils: make bandwidth options consistent
[fs/lustre-release.git] / lustre / include / lustre_dlm.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 /*
4  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5  * Use is subject to license terms.
6  *
7  * Copyright (c) 2010, 2017, Intel Corporation.
8  */
9
10 /*
11  * This file is part of Lustre, http://www.lustre.org/
12  */
13
14 /** \defgroup LDLM Lustre Distributed Lock Manager
15  *
16  * Lustre DLM is based on VAX DLM.
17  * Its two main roles are:
18  *   - To provide locking assuring consistency of data on all Lustre nodes.
19  *   - To allow clients to cache state protected by a lock by holding the
20  *     lock until a conflicting lock is requested or it is expired by the LRU.
21  *
22  * @{
23  */
24
25 #ifndef _LUSTRE_DLM_H__
26 #define _LUSTRE_DLM_H__
27
28 #include <cfs_hash.h>
29 #include <lustre_lib.h>
30 #include <lustre_net.h>
31 #include <lustre_import.h>
32 #include <lustre_handles.h>
33 #include <linux/interval_tree_generic.h>
34 #ifdef HAVE_LINUX_FILELOCK_HEADER
35 #include <linux/filelock.h>
36 #endif
37
38 #include "lustre_dlm_flags.h"
39
40 struct obd_ops;
41 struct obd_device;
42
43 extern struct kset *ldlm_ns_kset;
44 extern struct kset *ldlm_svc_kset;
45
46 #define OBD_LDLM_DEVICENAME  "ldlm"
47
48 #define LDLM_DEFAULT_LRU_SIZE (100 * num_online_cpus())
49 #define LDLM_DEFAULT_LRU_MAX_AGE        600     /* 600 seconds = 10 min */
50 #define LDLM_CTIME_AGE_LIMIT (10)
51 /* if client lock is unused for that time it can be cancelled if any other
52  * client shows interest in that lock, e.g. glimpse is occured.
53  */
54 #define LDLM_DIRTY_AGE_LIMIT (10)
55 #define LDLM_DEFAULT_PARALLEL_AST_LIMIT 1024
56 #define LDLM_DEFAULT_LRU_SHRINK_BATCH (16)
57 #define LDLM_DEFAULT_SLV_RECALC_PCT (10)
58
59 /**
60  * LDLM non-error return states
61  */
62 enum ldlm_error {
63         ELDLM_OK                = 0,
64         ELDLM_LOCK_MATCHED      = 1,
65
66         ELDLM_LOCK_CHANGED      = 300,
67         ELDLM_LOCK_ABORTED      = 301,
68         ELDLM_LOCK_REPLACED     = 302,
69         ELDLM_NO_LOCK_DATA      = 303,
70         ELDLM_LOCK_WOULDBLOCK   = 304,
71
72         ELDLM_NAMESPACE_EXISTS  = 400,
73         ELDLM_BAD_NAMESPACE     = 401,
74 };
75
76 /**
77  * LDLM namespace type.
78  * The "client" type is actually an indication that this is a narrow local view
79  * into complete namespace on the server. Such namespaces cannot make any
80  * decisions about lack of conflicts or do any autonomous lock granting without
81  * first speaking to a server.
82  */
83 enum ldlm_side {
84         LDLM_NAMESPACE_SERVER = 0x01,
85         LDLM_NAMESPACE_CLIENT = 0x02
86 };
87
88 /**
89  * The blocking callback is overloaded to perform two functions.  These flags
90  * indicate which operation should be performed.
91  */
92 #define LDLM_CB_BLOCKING    1
93 #define LDLM_CB_CANCELING   2
94
95 /**
96  * \name Lock Compatibility Matrix.
97  *
98  * A lock has both a type (extent, flock, inode bits, or plain) and a mode.
99  * Lock types are described in their respective implementation files:
100  * ldlm_{extent,flock,inodebits,plain}.c.
101  *
102  * There are nine lock modes along with a compatibility matrix to indicate if
103  * two locks are compatible.
104  *
105  * - EX: Exclusive mode. Before a new file is created, MDS requests EX lock
106  *   on the parent.
107  * - PW: Protective Write (normal write) mode. When a client requests a write
108  *   lock from an OST, a lock with PW mode will be issued.
109  * - PR: Protective Read (normal read) mode. When a client requests a read from
110  *   an OST, a lock with PR mode will be issued. Also, if the client opens a
111  *   file for execution, it is granted a lock with PR mode.
112  * - CW: Concurrent Write mode. The type of lock that the MDS grants if a client
113  *   requests a write lock during a file open operation.
114  * - CR Concurrent Read mode. When a client performs a path lookup, MDS grants
115  *   an inodebit lock with the CR mode on the intermediate path component.
116  * - NL Null mode.
117  * - GROUP: Group mode. Locks with the same group ID are compatible with each
118  *   other.
119  * - COS: Commit-on-Sharing mode. If Commit-on-Sharing is enabled, PW/EX locks
120  *   held in transactions are downgraded to COS mode after transaction stop.
121  * - TXN: Transaction mode. If Commit-on-Sharing is diabled on a DNE system,
122  *   PW/EX locks held in transactions are downgraded to TXN mode after
123  *   transaction stop.
124  *
125  * <PRE>
126  *       NL  CR  CW  PR  PW  EX GROUP COS TXN
127  *  NL    1   1   1   1   1   1   1   1   1
128  *  CR    1   1   1   1   1   0   0   0   1
129  *  CW    1   1   1   0   0   0   0   0   0
130  *  PR    1   1   0   1   0   0   0   0   1
131  *  PW    1   1   0   0   0   0   0   0   0
132  *  EX    1   0   0   0   0   0   0   0   0
133  *  GROUP 1   0   0   0   0   0   1   0   0
134  *  COS   1   0   0   0   0   0   0   1   0
135  *  TXN   1   1   0   1   0   0   0   0   1
136  * </PRE>
137  */
138 /** @{ */
139 #define LCK_COMPAT_EX    LCK_NL
140 #define LCK_COMPAT_PW    (LCK_COMPAT_EX | LCK_CR)
141 #define LCK_COMPAT_PR    (LCK_COMPAT_PW | LCK_PR | LCK_TXN)
142 #define LCK_COMPAT_CW    (LCK_COMPAT_PW | LCK_CW)
143 #define LCK_COMPAT_CR    (LCK_COMPAT_CW | LCK_PR | LCK_PW | LCK_TXN)
144 #define LCK_COMPAT_NL    (LCK_COMPAT_CR | LCK_EX | LCK_GROUP | LCK_COS)
145 #define LCK_COMPAT_GROUP (LCK_NL | LCK_GROUP)
146 #define LCK_COMPAT_COS   (LCK_NL | LCK_COS)
147 #define LCK_COMPAT_TXN   LCK_COMPAT_PR
148 /** @} Lock Compatibility Matrix */
149
150 extern enum ldlm_mode lck_compat_array[];
151
152 static inline void lockmode_verify(enum ldlm_mode mode)
153 {
154         LASSERT(mode > LCK_MODE_MIN && mode < LCK_MODE_END);
155 }
156
157 static inline int lockmode_compat(enum ldlm_mode exist_mode,
158                                   enum ldlm_mode new_mode)
159 {
160         return lck_compat_array[exist_mode] & new_mode;
161 }
162
163 /*
164  * cluster name spaces
165  */
166 #define DLM_OST_NAMESPACE 1
167 #define DLM_MDS_NAMESPACE 2
168
169 /* XXX
170  *  - do we just separate this by security domains and use a prefix for
171  *    multiple namespaces in the same domain?
172  *  -
173  */
174
175 /**
176  * Locking rules for LDLM:
177  *
178  * lr_lock
179  *
180  * lr_lock
181  *     waiting_locks_spinlock
182  *
183  * lr_lock
184  *     led_lock
185  *
186  * lr_lock
187  *     ns_lock
188  *
189  * lr_lvb_mutex
190  *     lr_lock
191  *
192  */
193
194 /* Cancel lru flag, it indicates we cancel aged locks. */
195 enum ldlm_lru_flags {
196         LDLM_LRU_FLAG_NO_WAIT   = 0x1, /* Cancel locks w/o blocking (neither
197                                         * sending nor waiting for any RPCs)
198                                         */
199         LDLM_LRU_FLAG_CLEANUP   = 0x2, /* Used when clearing lru, tells
200                                         * prepare_lru_list to set discard flag
201                                         * on PR extent locks so we don't waste
202                                         * time saving pages that will be
203                                         * discarded momentarily
204                                         */
205 };
206
207 struct ldlm_pool;
208 struct ldlm_lock;
209 struct ldlm_resource;
210 struct ldlm_namespace;
211
212 /**
213  * Operations on LDLM pools.
214  * LDLM pool is a pool of locks in the namespace without any implicitly
215  * specified limits.
216  * Locks in the pool are organized in LRU.
217  * Local memory pressure or server instructions (e.g. mempressure on server)
218  * can trigger freeing of locks from the pool
219  */
220 struct ldlm_pool_ops {
221         /** Recalculate pool \a pl usage */
222         int (*po_recalc)(struct ldlm_pool *pl, bool force);
223         /** Cancel at least \a nr locks from pool \a pl */
224         int (*po_shrink)(struct ldlm_pool *pl, int nr, gfp_t gfp_mask);
225         int (*po_setup)(struct ldlm_pool *pl, int limit);
226 };
227
228 /** One second for pools thread check interval. Each pool has own period. */
229 #define LDLM_POOLS_THREAD_PERIOD (1)
230
231 /** ~6% margin for modest pools. See ldlm_pool.c for details. */
232 #define LDLM_POOLS_MODEST_MARGIN_SHIFT (4)
233
234 /** Default recalc period for server side pools in sec. */
235 #define LDLM_POOL_SRV_DEF_RECALC_PERIOD (1)
236
237 /** Default recalc period for client side pools in sec. */
238 #define LDLM_POOL_CLI_DEF_RECALC_PERIOD (10)
239
240 /**
241  * LDLM pool structure to track granted locks.
242  * For purposes of determining when to release locks on e.g. memory pressure.
243  * This feature is commonly referred to as lru_resize.
244  */
245 struct ldlm_pool {
246         /** Pool debugfs directory. */
247         struct dentry           *pl_debugfs_entry;
248         /** Pool name, must be long enough to hold compound proc entry name. */
249         char                    pl_name[100];
250         /** Lock for protecting SLV/CLV updates. */
251         spinlock_t              pl_lock;
252         /** Number of allowed locks in in pool, both, client and server side. */
253         atomic_t                pl_limit;
254         /** Number of granted locks in */
255         atomic_t                pl_granted;
256         /** Grant rate per T. */
257         atomic_t                pl_grant_rate;
258         /** Cancel rate per T. */
259         atomic_t                pl_cancel_rate;
260         /** Server lock volume (SLV). Protected by pl_lock. */
261         __u64                   pl_server_lock_volume;
262         /** Current biggest client lock volume. Protected by pl_lock. */
263         __u64                   pl_client_lock_volume;
264         /** Lock volume factor, shown in percents in procfs, but internally
265          *  Client SLV calculated as: server_slv * lock_volume_factor >> 8.
266          */
267         atomic_t                pl_lock_volume_factor;
268         /** Time when last SLV from server was obtained. */
269         time64_t                pl_recalc_time;
270         /** Recalculation period for pool. */
271         time64_t                pl_recalc_period;
272         /** Recalculation and shrink operations. */
273         struct ldlm_pool_ops    *pl_ops;
274         /** Number of planned locks for next period. */
275         int                     pl_grant_plan;
276         /** Pool statistics. */
277         struct lprocfs_stats    *pl_stats;
278
279         /* sysfs object */
280         struct kobject           pl_kobj;
281         struct completion        pl_kobj_unregister;
282 };
283
284 typedef int (*ldlm_res_policy)(const struct lu_env *env,
285                                struct ldlm_namespace *,
286                                struct ldlm_lock **, void *req_cookie,
287                                enum ldlm_mode mode, __u64 flags, void *data);
288
289 typedef int (*ldlm_cancel_cbt)(struct ldlm_lock *lock);
290
291 typedef int (*ldlm_hp_handler_t)(struct ldlm_lock *lock);
292
293 /**
294  * LVB operations.
295  * LVB is Lock Value Block. This is a special opaque (to LDLM) value that could
296  * be associated with an LDLM lock and transferred from client to server and
297  * back.
298  *
299  * Currently LVBs are used by:
300  *  - OSC-OST code to maintain current object size/times
301  *  - layout lock code to return the layout when the layout lock is granted
302  *
303  * To ensure delayed LVB initialization, it is highly recommended to use the set
304  * of ldlm_[res_]lvbo_[init,update,fill]() functions.
305  */
306 struct ldlm_valblock_ops {
307         int (*lvbo_init)(struct ldlm_resource *res);
308         int (*lvbo_update)(struct ldlm_resource *res, struct ldlm_lock *lock,
309                            struct ptlrpc_request *r, int increase);
310         int (*lvbo_free)(struct ldlm_resource *res);
311         /* Return size of lvb data appropriate RPC size can be reserved */
312         int (*lvbo_size)(struct ldlm_lock *lock);
313         /* Called to fill in lvb data to RPC buffer @buf */
314         int (*lvbo_fill)(struct ldlm_lock *lock, void *buf, int *buflen);
315 };
316
317 /**
318  * LDLM pools related, type of lock pool in the namespace.
319  * Greedy means release cached locks aggressively
320  */
321 enum ldlm_appetite {
322         LDLM_NAMESPACE_GREEDY = BIT(0),
323         LDLM_NAMESPACE_MODEST = BIT(1),
324 };
325
326 /**
327  * Default values for the "max_nolock_size", "contention_time" and
328  * "contended_locks" namespace tunables.
329  */
330 #define NS_DEFAULT_MAX_NOLOCK_BYTES 0
331 #define NS_DEFAULT_CONTENTION_SECONDS 2
332 #define NS_DEFAULT_CONTENDED_LOCKS 32
333
334 struct ldlm_ns_bucket {
335         /** back pointer to namespace */
336         struct ldlm_namespace      *nsb_namespace;
337         /**
338          * Estimated lock callback time.  Used by adaptive timeout code to
339          * avoid spurious client evictions due to unresponsiveness when in
340          * fact the network or overall system load is at fault
341          */
342         struct adaptive_timeout     nsb_at_estimate;
343         /**
344          * Which res in the bucket should we start with the reclaim.
345          */
346         int                         nsb_reclaim_start;
347         /* counter of entries in this bucket */
348         atomic_t                    nsb_count;
349 };
350
351 enum {
352         /** LDLM namespace lock stats */
353         LDLM_NSS_LOCKS          = 0,
354         LDLM_NSS_LAST
355 };
356
357 enum ldlm_ns_type {
358         LDLM_NS_TYPE_UNKNOWN = 0,       /**< invalid type */
359         LDLM_NS_TYPE_MDC,               /**< MDC namespace */
360         LDLM_NS_TYPE_MDT,               /**< MDT namespace */
361         LDLM_NS_TYPE_OSC,               /**< OSC namespace */
362         LDLM_NS_TYPE_OST,               /**< OST namespace */
363         LDLM_NS_TYPE_MGC,               /**< MGC namespace */
364         LDLM_NS_TYPE_MGT,               /**< MGT namespace */
365 };
366
367 enum ldlm_namespace_flags {
368         /*
369          * Flag to indicate the LRU cancel is in progress.
370          * Used to limit the process by 1 thread only.
371          */
372         LDLM_NS_LRU_CANCEL,
373         LDLM_NS_STOPPING,
374         /* Controls the stack trace log in ldlm_lock_debug */
375         LDLM_NS_DUMP_STACK,
376         /*
377          * Flag to indicate the LRU recalc on RPC reply is in progress.
378          * Used to limit the process by 1 thread only.
379          */
380         LDLM_NS_RPC_RECALC,
381          /* lru_size is set even before connection */
382         LDLM_NS_LRU_SIZE_SET_BEFORE_CONN,
383         LDLM_NS_NUM_FLAGS
384 };
385
386 /*
387  * LDLM Namespace.
388  *
389  * Namespace serves to contain locks related to a particular service.
390  * There are two kinds of namespaces:
391  * - Server namespace has knowledge of all locks and is therefore authoritative
392  *   to make decisions like what locks could be granted and what conflicts
393  *   exist during new lock enqueue.
394  * - Client namespace only has limited knowledge about locks in the namespace,
395  *   only seeing locks held by the client.
396  *
397  * Every Lustre service has one server namespace present on the server serving
398  * that service. Every client connected to the service has a client namespace
399  * for it.
400  * Every lock obtained by client in that namespace is actually represented by
401  * two in-memory locks. One on the server and one on the client. The locks are
402  * linked by a special cookie by which one node can tell to the other which lock
403  * it actually means during communications. Such locks are called remote locks.
404  * The locks held by server only without any reference to a client are called
405  * local locks.
406  */
407 struct ldlm_namespace {
408         /** Backward link to OBD, required for LDLM pool to store new SLV. */
409         struct obd_device       *ns_obd;
410
411         /** Flag indicating if namespace is on client instead of server */
412         enum ldlm_side          ns_client;
413
414         /** name of this namespace */
415         char                    *ns_name;
416
417         /** Resource hash table for namespace. */
418         struct cfs_hash         *ns_rs_hash;
419
420         /** serialize */
421         spinlock_t              ns_lock;
422
423         /** big refcount (by bucket) */
424         atomic_t                ns_bref;
425
426         /**
427          * Namespace connect flags supported by server (may be changed via
428          * /proc, LRU resize may be disabled/enabled).
429          */
430         __u64                   ns_connect_flags;
431
432         /** Client side original connect flags supported by server. */
433         __u64                   ns_orig_connect_flags;
434
435         /* namespace debugfs dir entry */
436         struct dentry           *ns_debugfs_entry;
437
438         /**
439          * Position in global namespace list linking all namespaces on
440          * the node.
441          */
442         struct list_head        ns_list_chain;
443
444         /**
445          * List of unused locks for this namespace. This list is also called
446          * LRU lock list.
447          * Unused locks are locks with zero reader/writer reference counts.
448          * This list is only used on clients for lock caching purposes.
449          * When we want to release some locks voluntarily or if server wants
450          * us to release some locks due to e.g. memory pressure, we take locks
451          * to release from the head of this list.
452          * Locks are linked via l_lru field in \see struct ldlm_lock.
453          */
454         struct list_head        ns_unused_list;
455         /** Number of locks in the LRU list above */
456         int                     ns_nr_unused;
457         struct list_head        *ns_last_pos;
458
459         /**
460          * Maximum number of locks permitted in the LRU. If 0, means locks
461          * are managed by pools and there is no preset limit, rather it is all
462          * controlled by available memory on this client and on server.
463          */
464         unsigned int            ns_max_unused;
465
466         /**
467          * Cancel batch, if unused lock count exceed lru_size
468          * Only be used if LRUR disable.
469          */
470         unsigned int            ns_cancel_batch;
471
472         /* How much SLV should decrease in %% to trigger LRU cancel urgently. */
473         unsigned int            ns_recalc_pct;
474
475         /** Maximum allowed age (last used time) for locks in the LRU.  Set in
476          * seconds from userspace, but stored in ns to avoid repeat conversions.
477          */
478         ktime_t                 ns_max_age;
479
480         /**
481          * Server only: number of times we evicted clients due to lack of reply
482          * to ASTs.
483          */
484         unsigned int            ns_timeouts;
485         /**
486          * Number of seconds since the file change time after which
487          * the MDT will return an UPDATE lock along with a LOOKUP lock.
488          * This allows the client to start caching negative dentries
489          * for a directory and may save an RPC for a later stat.
490          */
491         timeout_t               ns_ctime_age_limit;
492         /**
493          * Number of (nano)seconds since the lock was last used. The client
494          * may cancel the lock older than this age and flush related data if
495          * another client shows interest in this lock by doing glimpse request.
496          * This allows to cache stat data locally for such files early. Set in
497          * seconds from userspace, but stored in ns to avoid repeat conversions.
498          */
499         ktime_t                 ns_dirty_age_limit;
500         /**
501          * Used to rate-limit ldlm_namespace_dump calls.
502          * \see ldlm_namespace_dump. Increased by 10 seconds every time
503          * it is called.
504          */
505         time64_t                ns_next_dump;
506
507         /** "policy" function that does actual lock conflict determination */
508         ldlm_res_policy         ns_policy;
509
510         /**
511          * LVB operations for this namespace.
512          * \see struct ldlm_valblock_ops
513          */
514         struct ldlm_valblock_ops *ns_lvbo;
515
516         /**
517          * Used by filter code to store pointer to OBD of the service.
518          * Should be dropped in favor of \a ns_obd
519          */
520         void                    *ns_lvbp;
521
522         /**
523          * Wait queue used by __ldlm_namespace_free. Gets woken up every time
524          * a resource is removed.
525          */
526         wait_queue_head_t       ns_waitq;
527         /** LDLM pool structure for this namespace */
528         struct ldlm_pool        ns_pool;
529         /** Definition of how eagerly unused locks will be released from LRU */
530         enum ldlm_appetite      ns_appetite;
531
532         /**
533          * If more than \a ns_contended_locks are found, the resource is
534          * considered to be contended. Lock enqueues might specify that no
535          * contended locks should be granted
536          */
537         unsigned int            ns_contended_locks;
538
539         /**
540          * The resources in this namespace remember contended state during
541          * \a ns_contention_time, in seconds.
542          */
543         timeout_t               ns_contention_time;
544
545         /**
546          * Limit size of contended extent locks, in bytes.
547          * If extended lock is requested for more then this many bytes and
548          * caller instructs us not to grant contended locks, we would disregard
549          * such a request.
550          */
551         unsigned int            ns_max_nolock_size;
552
553         /** Limit of parallel AST RPC count. */
554         unsigned int            ns_max_parallel_ast;
555
556         /**
557          * Callback to check if a lock is good to be canceled by ELC or
558          * during recovery.
559          */
560         ldlm_cancel_cbt         ns_cancel;
561
562         /**
563          * Callback to check whether an object protected by a lock needs to
564          * be handled with high priority (i.e. in case of lock blocking AST).
565          */
566         ldlm_hp_handler_t       ns_hp_handler;
567
568         /** LDLM lock stats */
569         struct lprocfs_stats    *ns_stats;
570
571         /**
572          * Which bucket should we start with the lock reclaim.
573          */
574         int                     ns_reclaim_start;
575
576         struct kobject          ns_kobj; /* sysfs object */
577         struct completion       ns_kobj_unregister;
578
579         /* See enum ldlm_namespace_flags */
580         DECLARE_BITMAP(ns_flags, LDLM_NS_NUM_FLAGS);
581 };
582
583 /**
584  * Returns 1 if namespace \a ns is a client namespace.
585  */
586 static inline int ns_is_client(struct ldlm_namespace *ns)
587 {
588         LASSERT(ns != NULL);
589         LASSERT(ns->ns_client == LDLM_NAMESPACE_CLIENT ||
590                 ns->ns_client == LDLM_NAMESPACE_SERVER);
591         return ns->ns_client == LDLM_NAMESPACE_CLIENT;
592 }
593
594 /**
595  * Returns 1 if namespace \a ns is a server namespace.
596  */
597 static inline int ns_is_server(struct ldlm_namespace *ns)
598 {
599         LASSERT(ns != NULL);
600         LASSERT(ns->ns_client == LDLM_NAMESPACE_CLIENT ||
601                 ns->ns_client == LDLM_NAMESPACE_SERVER);
602         return ns->ns_client == LDLM_NAMESPACE_SERVER;
603 }
604
605 /**
606  * Returns 1 if namespace \a ns supports early lock cancel (ELC).
607  */
608 static inline int ns_connect_cancelset(struct ldlm_namespace *ns)
609 {
610         LASSERT(ns != NULL);
611         return !!(ns->ns_connect_flags & OBD_CONNECT_CANCELSET);
612 }
613
614 /**
615  * Returns 1 if this namespace supports lru_resize.
616  */
617 static inline int ns_connect_lru_resize(struct ldlm_namespace *ns)
618 {
619         LASSERT(ns != NULL);
620         return !!(ns->ns_connect_flags & OBD_CONNECT_LRU_RESIZE);
621 }
622
623 static inline void ns_register_cancel(struct ldlm_namespace *ns,
624                                       ldlm_cancel_cbt arg)
625 {
626         LASSERT(ns != NULL);
627         ns->ns_cancel = arg;
628 }
629
630 static inline void ns_register_hp_handler(struct ldlm_namespace *ns,
631                                           ldlm_hp_handler_t arg)
632 {
633         LASSERT(ns != NULL);
634         ns->ns_hp_handler = arg;
635 }
636
637 struct ldlm_lock;
638
639 /** Type for blocking callback function of a lock. */
640 typedef int (*ldlm_blocking_callback)(struct ldlm_lock *lock,
641                                       struct ldlm_lock_desc *new, void *data,
642                                       int flag);
643 /** Type for completion callback function of a lock. */
644 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, __u64 flags,
645                                         void *data);
646 /** Type for glimpse callback function of a lock. */
647 typedef int (*ldlm_glimpse_callback)(struct ldlm_lock *lock, void *data);
648
649 /** Type for created callback function of a lock. */
650 typedef void (*ldlm_created_callback)(struct ldlm_lock *lock);
651
652 /** Work list for sending GL ASTs to multiple locks. */
653 struct ldlm_glimpse_work {
654         struct ldlm_lock        *gl_lock; /* lock to glimpse */
655         struct list_head         gl_list; /* linkage to other gl work structs */
656         __u32                    gl_flags;/* see LDLM_GL_WORK_* below */
657         /* glimpse descriptor to be packed in glimpse callback request */
658         union ldlm_gl_desc      *gl_desc;
659         ptlrpc_interpterer_t     gl_interpret_reply;
660         void                    *gl_interpret_data;
661 };
662
663 struct ldlm_bl_desc {
664         unsigned int bl_same_client:1,  /* both ops are from the same client. */
665                      bl_txn_dependent:1;/* the op that enqueues lock depends on
666                                          * the op that holds lock.
667                                          */
668 };
669
670 struct ldlm_cb_set_arg {
671         struct ptlrpc_request_set       *set;
672         int                              type; /* LDLM_{CP,BL,GL}_CALLBACK */
673         atomic_t                         restart;
674         struct list_head                *list;
675         union ldlm_gl_desc              *gl_desc; /* glimpse AST descriptor */
676         ptlrpc_interpterer_t             gl_interpret_reply;
677         void                            *gl_interpret_data;
678         struct ldlm_bl_desc             *bl_desc;
679 };
680
681 struct ldlm_cb_async_args {
682         struct ldlm_cb_set_arg  *ca_set_arg;
683         struct ldlm_lock        *ca_lock;
684 };
685
686 /** The ldlm_glimpse_work was slab allocated & must be freed accordingly.*/
687 #define LDLM_GL_WORK_SLAB_ALLOCATED 0x1
688
689 /**
690  * Interval tree for extent locks.
691  * The interval tree must be accessed under the resource lock.
692  * Interval trees are used for granted extent locks to speed up conflicts
693  * lookup.
694  */
695 struct ldlm_interval_tree {
696         /** Tree size. */
697         int                             lit_size;
698         enum ldlm_mode                  lit_mode;  /* lock mode */
699         struct interval_tree_root       lit_root; /* actual interval tree */
700 };
701
702 /**
703  * Lists of waiting locks for each inodebit type.
704  * A lock can be in several liq_waiting lists and it remains in lr_waiting.
705  */
706 struct ldlm_ibits_queues {
707         struct list_head        liq_waiting[MDS_INODELOCK_NUMBITS];
708 };
709
710 struct ldlm_ibits_node {
711         struct list_head        lin_link[MDS_INODELOCK_NUMBITS];
712         struct ldlm_lock        *lock;
713 };
714
715 struct ldlm_flock_node {
716         atomic_t                lfn_unlock_pending;
717         bool                    lfn_needs_reprocess;
718         struct interval_tree_root lfn_root;
719 };
720
721 /** Whether to track references to exports by LDLM locks. */
722 #define LUSTRE_TRACKS_LOCK_EXP_REFS (0)
723
724 /** Cancel flags. */
725 enum ldlm_cancel_flags {
726         LCF_ASYNC       = 0x1, /* Cancel locks asynchronously. */
727         LCF_LOCAL       = 0x2, /* Cancel locks locally, not notifing server */
728         LCF_BL_AST      = 0x4, /* Cancel LDLM_FL_BL_AST locks in the same RPC */
729 };
730
731 struct ldlm_flock {
732         __u64 start;
733         __u64 end;
734         __u64 owner;
735         __u64 blocking_owner;
736         struct obd_export *blocking_export;
737         atomic_t blocking_refs;
738         __u32 pid;
739 };
740
741 union ldlm_policy_data {
742         struct ldlm_extent l_extent;
743         struct ldlm_flock l_flock;
744         struct ldlm_inodebits l_inodebits;
745 };
746
747 void ldlm_convert_policy_to_wire(enum ldlm_type type,
748                                  const union ldlm_policy_data *lpolicy,
749                                  union ldlm_wire_policy_data *wpolicy);
750 void ldlm_convert_policy_to_local(struct obd_export *exp, enum ldlm_type type,
751                                   const union ldlm_wire_policy_data *wpolicy,
752                                   union ldlm_policy_data *lpolicy);
753
754 enum lvb_type {
755         LVB_T_NONE      = 0,
756         LVB_T_OST       = 1,
757         LVB_T_LQUOTA    = 2,
758         LVB_T_LAYOUT    = 3,
759         LVB_T_END
760 };
761
762 /**
763  * LDLM_GID_ANY is used to match any group id in ldlm_lock_match().
764  */
765 #define LDLM_GID_ANY  ((__u64)-1)
766
767 /**
768  * LDLM lock structure
769  *
770  * Represents a single LDLM lock and its state in memory. Each lock is
771  * associated with a single ldlm_resource, the object which is being
772  * locked. There may be multiple ldlm_locks on a single resource,
773  * depending on the lock type and whether the locks are conflicting or
774  * not.
775  */
776 struct ldlm_lock {
777         /**
778          * Local lock handle.
779          * When remote side wants to tell us about a lock, they address
780          * it by this opaque handle.  The handle does not hold a
781          * reference on the ldlm_lock, so it can be safely passed to
782          * other threads or nodes. When the lock needs to be accessed
783          * from the handle, it is looked up again in the lock table, and
784          * may no longer exist.
785          *
786          * Must be first in the structure.
787          */
788         struct portals_handle   l_handle;
789         /**
790          * Pointer to actual resource this lock is in.
791          * ldlm_lock_change_resource() can change this on the client.
792          * When this is possible, rcu must be used to stablise
793          * the resource while we lock and check it hasn't been changed.
794          */
795         struct ldlm_resource    *l_resource;
796         /**
797          * List item for client side LRU list.
798          * Protected by ns_lock in struct ldlm_namespace.
799          */
800         struct list_head        l_lru;
801         /**
802          * Linkage to resource's lock queues according to current lock state.
803          * (could be granted or waiting)
804          * Protected by lr_lock in struct ldlm_resource.
805          */
806         struct list_head        l_res_link;
807         /**
808          * Internal structures per lock type..
809          */
810         union {
811                 struct { /* LDLM_EXTENT locks only */
812                         /* Originally requested extent for the extent lock. */
813                         struct ldlm_extent      l_req_extent;
814                         struct rb_node          l_rb;
815                         u64                     l_subtree_last;
816                         struct list_head        l_same_extent;
817                 };
818                 struct { /* LDLM_PLAIN and LDLM_IBITS locks */
819                         /**
820                          * Protected by lr_lock, linkages to "skip lists".
821                          * For explanations of skip lists see
822                          * ldlm/ldlm_inodebits.c
823                          */
824                         struct list_head        l_sl_mode;
825                         struct list_head        l_sl_policy;
826
827                         struct ldlm_ibits_node  *l_ibits_node;
828                         /* separate ost_lvb used mostly by Data-on-MDT for now.
829                          * It is introduced to don't mix with layout lock data.
830                          */
831                         struct ost_lvb           l_ost_lvb;
832                 };
833                 struct { /* LDLM_FLOCK locks */
834                         /**
835                          * Per export hash of flock locks.
836                          * Protected by per-bucket exp->exp_flock_hash locks.
837                          */
838                         struct hlist_node       l_exp_flock_hash;
839                         struct ldlm_lock        *l_same_owner;
840                         /* interval tree */
841                         struct rb_node          l_fl_rb;
842                         u64                     l_fl_subtree_last;
843                 };
844         };
845         /**
846          * Per export hash of locks.
847          * Protected by per-bucket exp->exp_lock_hash locks.
848          */
849         struct hlist_node       l_exp_hash;
850
851         /* Requested mode. Protected by lr_lock. */
852         enum ldlm_mode          l_req_mode:9;
853         /* Granted mode, also protected by lr_lock.  */
854         enum ldlm_mode          l_granted_mode:9;
855
856         /**
857          * Whether the blocking AST was sent for this lock.
858          * This is for debugging. Valid values are 0 and 1, if there is an
859          * attempt to send blocking AST more than once, an assertion would be
860          * hit. \see ldlm_work_bl_ast_lock
861          */
862         unsigned int            l_bl_ast_run:1;
863
864         /* content type for lock value block */
865         enum lvb_type           l_lvb_type:3;
866         /* unsigned int         l_unused_bits:10; */
867         u16                     l_lvb_len;
868         /* u16                  l_unused; */
869
870         /*
871          * Temporary storage for a LVB received during an enqueue operation.
872          * May be vmalloc'd, so needs to be freed with OBD_FREE_LARGE().
873          */
874         void                    *l_lvb_data;
875
876         /** Lock completion handler pointer. Called when lock is granted. */
877         ldlm_completion_callback l_completion_ast;
878         /**
879          * Lock blocking AST handler pointer.
880          * It plays two roles:
881          * - as a notification of an attempt to queue a conflicting lock (once)
882          * - as a notification when the lock is being cancelled.
883          *
884          * As such it's typically called twice: once for the initial conflict
885          * and then once more when the last user went away and the lock is
886          * cancelled (could happen recursively).
887          */
888         ldlm_blocking_callback  l_blocking_ast;
889         /**
890          * Lock glimpse handler.
891          * Glimpse handler is used to obtain LVB updates from a client by
892          * server
893          */
894         ldlm_glimpse_callback   l_glimpse_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         union ldlm_policy_data  l_policy_data;
920
921         /**
922          * Lock state flags. Protected by lr_lock.
923          * \see lustre_dlm_flags.h where the bits are defined.
924          */
925         __u64                   l_flags;
926
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          * Time, in nanoseconds, last used by e.g. being matched by lock match.
942          */
943         ktime_t                 l_last_used;
944
945         /** Private storage for lock user. Opaque to LDLM. */
946         void                    *l_ast_data;
947
948         union {
949         /**
950          * Seconds. It will be updated if there is any activity related to
951          * the lock at client, e.g. enqueue the lock. For server it is the
952          * time when blocking ast was sent.
953          */
954                 time64_t        l_activity;
955                 time64_t        l_blast_sent;
956         };
957
958         /*
959          * Server-side-only members.
960          */
961
962         /**
963          * Connection cookie for the client originating the operation.
964          * Used by Commit on Share (COS) code. Currently only used for
965          * inodebits locks on MDS.
966          */
967         __u64                   l_client_cookie;
968
969         /**
970          * List item for locks waiting for cancellation from clients.
971          * The lists this could be linked into are:
972          * waiting_locks_list (protected by waiting_locks_spinlock),
973          * then if the lock timed out, it is moved to
974          * expired_lock_list for further processing.
975          */
976         struct list_head        l_pending_chain;
977
978         /**
979          * Set when lock is sent a blocking AST. Time in seconds when timeout
980          * is reached and client holding this lock could be evicted.
981          * This timeout could be further extended by e.g. certain IO activity
982          * under this lock.
983          * \see ost_rw_prolong_locks
984          */
985         time64_t                l_callback_timestamp;
986
987         /** Local PID of process which created this lock. */
988         __u32                   l_pid;
989         /* __u32                l_unused; */
990
991         /** List item ldlm_add_ast_work_item() for case of blocking ASTs. */
992         struct list_head        l_bl_ast;
993         /** List item ldlm_add_ast_work_item() for case of completion ASTs. */
994         struct list_head        l_cp_ast;
995         /** For ldlm_add_ast_work_item() for "revoke" AST used in COS. */
996         struct list_head        l_rk_ast;
997
998         /**
999          * Pointer to a conflicting lock that caused blocking AST to be sent
1000          * for this lock
1001          */
1002         struct ldlm_lock        *l_blocking_lock;
1003
1004 #if LUSTRE_TRACKS_LOCK_EXP_REFS
1005         /* Debugging stuff for bug 20498, for tracking export references. */
1006         /** number of export references taken */
1007         int                     l_exp_refs_nr;
1008         /** link all locks referencing one export */
1009         struct list_head        l_exp_refs_link;
1010         /** referenced export object */
1011         struct obd_export       *l_exp_refs_target;
1012 #endif
1013         /**
1014          * export blocking dlm lock list, protected by
1015          * l_export->exp_bl_list_lock.
1016          * Lock order of waiting_lists_spinlock, exp_bl_list_lock and res lock
1017          * is: res lock -> exp_bl_list_lock -> wanting_lists_spinlock.
1018          */
1019         struct list_head        l_exp_list;
1020 };
1021
1022 enum ldlm_match_flags {
1023         LDLM_MATCH_UNREF   = BIT(0),
1024         LDLM_MATCH_AST     = BIT(1),
1025         LDLM_MATCH_AST_ANY = BIT(2),
1026         LDLM_MATCH_RIGHT   = BIT(3),
1027         LDLM_MATCH_GROUP   = BIT(4),
1028         LDLM_MATCH_SKIP_UNUSED = BIT(5),
1029 };
1030
1031 #ifdef HAVE_INTERVAL_TREE_CACHED
1032 #define extent_last(tree) rb_entry_safe(rb_last(&tree->lit_root.rb_root),\
1033                                         struct ldlm_lock, l_rb)
1034 #define extent_first(tree) rb_entry_safe(rb_first(&tree->lit_root.rb_root),\
1035                                          struct ldlm_lock, l_rb)
1036 #define extent_top(tree) rb_entry_safe(tree->lit_root.rb_root.rb_node,  \
1037                                        struct ldlm_lock, l_rb)
1038 #else
1039 #define extent_last(tree) rb_entry_safe(rb_last(&tree->lit_root),       \
1040                                         struct ldlm_lock, l_rb)
1041 #define extent_first(tree) rb_entry_safe(rb_first(&tree->lit_root),     \
1042                                          struct ldlm_lock, l_rb)
1043 #define extent_top(tree) rb_entry_safe(tree->lit_root.rb_node,          \
1044                                        struct ldlm_lock, l_rb)
1045 #endif
1046
1047 #define extent_next(lock) rb_entry_safe(rb_next(&lock->l_rb),           \
1048                                         struct ldlm_lock, l_rb)
1049 #define extent_prev(lock) rb_entry_safe(rb_prev(&lock->l_rb),           \
1050                                         struct ldlm_lock, l_rb)
1051
1052 /**
1053  * Describe the overlap between two locks.  itree_overlap_cb data.
1054  */
1055 struct ldlm_match_data {
1056         struct ldlm_lock        *lmd_old;
1057         struct ldlm_lock        *lmd_lock;
1058         enum ldlm_mode          *lmd_mode;
1059         union ldlm_policy_data  *lmd_policy;
1060         __u64                    lmd_flags;
1061         __u64                    lmd_skip_flags;
1062         enum ldlm_match_flags    lmd_match;
1063 };
1064
1065 /** For uncommitted cross-MDT lock, store transno this lock belongs to */
1066 #define l_transno l_client_cookie
1067
1068 /** For uncommitted cross-MDT lock, which is client lock, share with l_rk_ast
1069  *  which is for server.
1070  */
1071 #define l_slc_link l_rk_ast
1072
1073 struct lustre_handle_array {
1074         unsigned int            ha_count;
1075         /* ha_map is used as bit flag to indicate handle is remote or local */
1076         DECLARE_BITMAP(ha_map, LMV_MAX_STRIPE_COUNT);
1077         struct lustre_handle    ha_handles[];
1078 };
1079
1080 /**
1081  * LDLM resource description.
1082  * Basically, resource is a representation for a single object.
1083  * Object has a name which is currently 4 64-bit integers. LDLM user is
1084  * responsible for creation of a mapping between objects it wants to be
1085  * protected and resource names.
1086  *
1087  * A resource can only hold locks of a single lock type, though there may be
1088  * multiple ldlm_locks on a single resource, depending on the lock type and
1089  * whether the locks are conflicting or not.
1090  */
1091 struct ldlm_resource {
1092         struct ldlm_ns_bucket   *lr_ns_bucket;
1093
1094         /**
1095          * List item for list in namespace hash.
1096          * protected by ns_lock.
1097          * Shared with linkage for RCU-delayed free.
1098          */
1099         union {
1100                 struct hlist_node       lr_hash;
1101                 struct rcu_head         lr_rcu;
1102         };
1103
1104         /** Reference count for this resource */
1105         refcount_t              lr_refcount;
1106
1107         /** Spinlock to protect locks under this resource. */
1108         spinlock_t              lr_lock;
1109
1110         /* protected by lr_lock */
1111
1112         /** List of locks in granted state */
1113         struct list_head        lr_granted;
1114         /**
1115          * List of locks that could not be granted due to conflicts and
1116          * that are waiting for conflicts to go away
1117          */
1118         struct list_head        lr_waiting;
1119         /* List of locks that waiting to enqueueing for flock */
1120         struct list_head        lr_enqueueing;
1121         /** @} */
1122
1123         /** Resource name */
1124         struct ldlm_res_id      lr_name;
1125
1126         union {
1127                  /* Interval trees (for extent locks) all modes of resource */
1128                 struct ldlm_interval_tree *lr_itree;
1129                 struct ldlm_ibits_queues *lr_ibits_queues;
1130                 struct ldlm_flock_node lr_flock_node;
1131         };
1132
1133         union {
1134                  /* resource considered as contended, used only on server side*/
1135                 time64_t        lr_contention_time;
1136                 /**
1137                  * Associated inode, used only on client side.
1138                  */
1139                 struct inode    *lr_lvb_inode;
1140         };
1141
1142         /** Type of locks this resource can hold. Only one type per resource. */
1143         enum ldlm_type          lr_type:4; /* LDLM_{PLAIN,EXTENT,FLOCK,IBITS} */
1144         /* unsigned int         lr_unused_bits:4; */
1145         /* char                 lr_unused[5]; */
1146
1147         /**
1148          * Server-side-only lock value block elements.
1149          * To serialize lvbo_init.
1150          */
1151         bool                    lr_lvb_initialized;
1152         char                    lr_lvb_len;
1153         struct mutex            lr_lvb_mutex;
1154         /** protected by lr_lock */
1155         void                    *lr_lvb_data;
1156 };
1157
1158 static inline int ldlm_is_granted(struct ldlm_lock *lock)
1159 {
1160         return lock->l_req_mode == lock->l_granted_mode;
1161 }
1162
1163 static inline bool ldlm_has_layout(struct ldlm_lock *lock)
1164 {
1165         return lock->l_resource->lr_type == LDLM_IBITS &&
1166                 !!(lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_LAYOUT);
1167 }
1168
1169 static inline bool ldlm_has_dom(struct ldlm_lock *lock)
1170 {
1171         return lock->l_resource->lr_type == LDLM_IBITS &&
1172                 !!(lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_DOM);
1173 }
1174
1175 static inline bool ldlm_has_update(struct ldlm_lock *lock)
1176 {
1177         return lock->l_resource->lr_type == LDLM_IBITS &&
1178                 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE;
1179 }
1180
1181 static inline char *
1182 ldlm_ns_name(struct ldlm_namespace *ns)
1183 {
1184         return ns->ns_name;
1185 }
1186
1187 static inline struct ldlm_namespace *
1188 ldlm_res_to_ns(struct ldlm_resource *res)
1189 {
1190         return res->lr_ns_bucket->nsb_namespace;
1191 }
1192
1193 static inline struct ldlm_namespace *
1194 ldlm_lock_to_ns(struct ldlm_lock *lock)
1195 {
1196         return ldlm_res_to_ns(lock->l_resource);
1197 }
1198
1199 static inline char *
1200 ldlm_lock_to_ns_name(struct ldlm_lock *lock)
1201 {
1202         return ldlm_ns_name(ldlm_lock_to_ns(lock));
1203 }
1204
1205 static inline struct adaptive_timeout *
1206 ldlm_lock_to_ns_at(struct ldlm_lock *lock)
1207 {
1208         return &lock->l_resource->lr_ns_bucket->nsb_at_estimate;
1209 }
1210
1211 static inline int ldlm_lvbo_init(struct ldlm_resource *res)
1212 {
1213         struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1214         int rc = 0;
1215
1216         if (ns->ns_lvbo == NULL || ns->ns_lvbo->lvbo_init == NULL ||
1217             res->lr_lvb_initialized)
1218                 return 0;
1219
1220         mutex_lock(&res->lr_lvb_mutex);
1221         /* Did we lose the race? */
1222         if (res->lr_lvb_initialized) {
1223                 mutex_unlock(&res->lr_lvb_mutex);
1224                 return 0;
1225         }
1226         rc = ns->ns_lvbo->lvbo_init(res);
1227         if (rc < 0) {
1228                 CDEBUG(D_DLMTRACE, "lvbo_init failed for resource : rc = %d\n",
1229                        rc);
1230                 if (res->lr_lvb_data != NULL) {
1231                         OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
1232                         res->lr_lvb_data = NULL;
1233                 }
1234                 res->lr_lvb_len = rc;
1235         } else {
1236                 res->lr_lvb_initialized = true;
1237         }
1238         mutex_unlock(&res->lr_lvb_mutex);
1239         return rc;
1240 }
1241
1242 static inline int ldlm_lvbo_size(struct ldlm_lock *lock)
1243 {
1244         struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
1245
1246         if (ns->ns_lvbo != NULL && ns->ns_lvbo->lvbo_size != NULL)
1247                 return ns->ns_lvbo->lvbo_size(lock);
1248
1249         return 0;
1250 }
1251
1252 static inline int ldlm_lvbo_fill(struct ldlm_lock *lock, void *buf, int *len)
1253 {
1254         struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
1255         int rc;
1256
1257         if (ns->ns_lvbo != NULL) {
1258                 LASSERT(ns->ns_lvbo->lvbo_fill != NULL);
1259                 /* init lvb now if not already */
1260                 rc = ldlm_lvbo_init(lock->l_resource);
1261                 if (rc < 0) {
1262                         CERROR("lock %p: delayed lvb init failed (rc %d)\n",
1263                                lock, rc);
1264                         return rc;
1265                 }
1266                 return ns->ns_lvbo->lvbo_fill(lock, buf, len);
1267         }
1268         return 0;
1269 }
1270
1271 struct ldlm_ast_work {
1272         struct ldlm_lock       *w_lock;
1273         int                     w_blocking;
1274         struct ldlm_lock_desc   w_desc;
1275         struct list_head        w_list;
1276         int                     w_flags;
1277         void                   *w_data;
1278         int                     w_datalen;
1279 };
1280
1281 /**
1282  * Common ldlm_enqueue parameters
1283  */
1284 struct ldlm_enqueue_info {
1285         enum ldlm_type  ei_type;        /** Type of the lock being enqueued. */
1286         enum ldlm_mode  ei_mode;        /** Mode of the lock being enqueued. */
1287         void            *ei_cb_bl;      /** blocking lock callback */
1288         void            *ei_cb_local_bl; /** blocking local lock callback */
1289         void            *ei_cb_cp;      /** lock completion callback */
1290         void            *ei_cb_gl;      /** lock glimpse callback */
1291         ldlm_created_callback ei_cb_created;    /** lock created callback */
1292         void            *ei_cbdata;     /** Data to be passed into callbacks. */
1293         void            *ei_namespace;  /** lock namespace **/
1294         enum mds_ibits_locks ei_inodebits;      /** lock inode bits **/
1295         unsigned int    ei_enq_slave:1; /** whether enqueue slave stripes */
1296         unsigned int    ei_req_slot:1;  /** whether acquire rpc slot */
1297         unsigned int    ei_mod_slot:1;  /** whether acquire mod rpc slot */
1298 };
1299
1300 #define ei_res_id       ei_cb_gl
1301
1302 enum ldlm_flock_flags {
1303         FA_FL_CANCEL_RQST       = 1,
1304         FA_FL_CANCELED          = 2,
1305 };
1306
1307 struct ldlm_flock_info {
1308         struct file             *fa_file;
1309         struct file_lock        *fa_fl; /* original file_lock */
1310         struct file_lock        fa_flc; /* lock copy */
1311         enum ldlm_flock_flags   fa_flags;
1312         enum ldlm_mode          fa_mode;
1313 #ifdef HAVE_LM_GRANT_2ARGS
1314         int (*fa_notify)(struct file_lock *, int);
1315 #else
1316         int (*fa_notify)(struct file_lock *, struct file_lock *, int);
1317 #endif
1318         int                     fa_err;
1319         int                     fa_ready;
1320         wait_queue_head_t       fa_waitq;
1321 };
1322
1323 extern char *ldlm_lockname[];
1324 extern char *ldlm_typename[];
1325 extern const char *ldlm_it2str(enum ldlm_intent_flags it);
1326
1327 /**
1328  * Just a fancy CDEBUG call with log level preset to LDLM_DEBUG.
1329  * For the cases where we do not have actual lock to print along
1330  * with a debugging message that is ldlm-related
1331  */
1332 #define LDLM_DEBUG_NOLOCK(format, a...)                 \
1333         CDEBUG(D_DLMTRACE, "### " format "\n", ##a)
1334
1335 /**
1336  * Support function for lock information printing into debug logs.
1337  * \see LDLM_DEBUG
1338  */
1339 #ifdef LIBCFS_DEBUG
1340 #define ldlm_lock_debug(msgdata, mask, cdls, lock, fmt, a...) do {      \
1341         if (((mask) & D_CANTMASK) != 0 ||                               \
1342             ((libcfs_debug & (mask)) != 0 &&                            \
1343              (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0)) {        \
1344                 _ldlm_lock_debug(lock, msgdata, fmt, ##a);              \
1345                 if (unlikely(test_bit(LDLM_NS_DUMP_STACK,               \
1346                                       ldlm_lock_to_ns(lock)->ns_flags)) && \
1347                     !!((mask) & D_ERROR))                               \
1348                         dump_stack();                                   \
1349         }                                                               \
1350 } while (0)
1351
1352 __printf(3, 4) /* function attribute */
1353 void _ldlm_lock_debug(struct ldlm_lock *lock,
1354                       struct libcfs_debug_msg_data *data,
1355                       const char *fmt, ...);
1356
1357 /**
1358  * Rate-limited version of lock printing function.
1359  */
1360 #define LDLM_DEBUG_LIMIT(mask, lock, fmt, a...) do {                         \
1361         static struct cfs_debug_limit_state _ldlm_cdls;                      \
1362         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, mask, &_ldlm_cdls);              \
1363         ldlm_lock_debug(&msgdata, mask, &_ldlm_cdls, lock, "### " fmt, ##a); \
1364 } while (0)
1365
1366 #define LDLM_ERROR(lock, fmt, a...) LDLM_DEBUG_LIMIT(D_ERROR, lock, fmt, ## a)
1367 #define LDLM_WARN(lock, fmt, a...)  LDLM_DEBUG_LIMIT(D_WARNING, lock, fmt, ## a)
1368
1369 /** Non-rate-limited lock printing function for debugging purposes. */
1370 #define LDLM_DEBUG(lock, fmt, a...)   do {                                  \
1371         if (likely(lock != NULL)) {                                         \
1372                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_DLMTRACE, NULL);      \
1373                 ldlm_lock_debug(&msgdata, D_DLMTRACE, NULL, lock,           \
1374                                 "### " fmt, ##a);                           \
1375         } else {                                                            \
1376                 LDLM_DEBUG_NOLOCK("no dlm lock: " fmt, ##a);                \
1377         }                                                                   \
1378 } while (0)
1379 #else /* !LIBCFS_DEBUG */
1380 # define LDLM_DEBUG_LIMIT(mask, lock, fmt, a...) ((void)0)
1381 # define LDLM_DEBUG(lock, fmt, a...) ((void)0)
1382 # define LDLM_ERROR(lock, fmt, a...) ((void)0)
1383 #endif
1384
1385 /*
1386  * Three intentions can be used for the policy functions in
1387  * ldlm_processing_policy.
1388  *
1389  * LDLM_PROCESS_RESCAN:
1390  *
1391  * It's used when policy functions are called from ldlm_reprocess_queue() to
1392  * reprocess the wait list and try to grant locks, blocking ASTs
1393  * have already been sent in this situation, completion ASTs need be sent for
1394  * the locks being granted.
1395  *
1396  * LDLM_PROCESS_ENQUEUE:
1397  *
1398  * It's used when policy functions are called from ldlm_lock_enqueue() to
1399  * process the wait list for handling an enqueue request, blocking
1400  * ASTs have not been sent yet, so list of conflicting locks would be
1401  * collected and ASTs sent.
1402  *
1403  * LDLM_PROCESS_RECOVERY:
1404  *
1405  * It's used when policy functions are called from ldlm_reprocess_queue() to
1406  * reprocess the wait list when recovery done. In case of blocking
1407  * ASTs are lost before recovery, it needs not only to grant locks if
1408  * available, but also send blocking ASTs to the locks doesn't have AST sent
1409  * flag. Completion ASTs need be sent for the locks being granted.
1410  */
1411 enum ldlm_process_intention {
1412         LDLM_PROCESS_RESCAN = 0,
1413         LDLM_PROCESS_ENQUEUE = 1,
1414         LDLM_PROCESS_RECOVERY = 2,
1415 };
1416
1417 typedef int (*ldlm_processing_policy)(struct ldlm_lock *lock, __u64 *flags,
1418                                       enum ldlm_process_intention intention,
1419                                       enum ldlm_error *err,
1420                                       struct list_head *work_list);
1421
1422 typedef int (*ldlm_reprocessing_policy)(struct ldlm_resource *res,
1423                                         struct list_head *queue,
1424                                         struct list_head *work_list,
1425                                         enum ldlm_process_intention intention,
1426                                         enum mds_ibits_locks hint);
1427
1428 /**
1429  * Return values for lock iterators.
1430  * Also used during deciding of lock grants and cancellations.
1431  */
1432 #define LDLM_ITER_CONTINUE 1 /* keep iterating */
1433 #define LDLM_ITER_STOP     2 /* stop iterating */
1434
1435 typedef int (*ldlm_iterator_t)(struct ldlm_lock *, void *);
1436 typedef int (*ldlm_res_iterator_t)(struct ldlm_resource *, void *);
1437
1438 /** \defgroup ldlm_iterator Lock iterators
1439  *
1440  * LDLM provides for a way to iterate through every lock on a resource or
1441  * namespace or every resource in a namespace.
1442  * @{
1443  */
1444 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1445                           void *closure);
1446 void ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
1447                             void *closure);
1448 int ldlm_resource_iterate(struct ldlm_namespace *ln,
1449                           const struct ldlm_res_id *lri,
1450                           ldlm_iterator_t iter, void *data);
1451 /** @} ldlm_iterator */
1452
1453 int ldlm_replay_locks(struct obd_import *imp);
1454
1455 /* ldlm_flock.c */
1456 int ldlm_flock_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data);
1457 struct ldlm_flock_info *
1458 ldlm_flock_completion_ast_async(struct ldlm_lock *lock, __u64 flags,
1459                                 void *data);
1460
1461 /* ldlm_extent.c */
1462 __u64 ldlm_extent_shift_kms(struct ldlm_lock *lock, __u64 old_kms);
1463
1464 struct ldlm_prolong_args {
1465         struct obd_export       *lpa_export;
1466         struct ptlrpc_request   *lpa_req;
1467         struct ldlm_res_id      lpa_resid;
1468         struct ldlm_extent      lpa_extent;
1469         enum ldlm_mode          lpa_mode;
1470         int                     lpa_locks_cnt;
1471         int                     lpa_blocks_cnt;
1472 };
1473 void ldlm_lock_prolong_one(struct ldlm_lock *lock,
1474                            struct ldlm_prolong_args *arg);
1475 void ldlm_resource_prolong(struct ldlm_prolong_args *arg);
1476
1477 struct ldlm_callback_suite {
1478         ldlm_completion_callback lcs_completion;
1479         ldlm_blocking_callback   lcs_blocking;
1480         ldlm_glimpse_callback    lcs_glimpse;
1481 };
1482
1483 /* ldlm_lockd.c */
1484 #ifdef HAVE_SERVER_SUPPORT
1485 /** \defgroup ldlm_srv_ast Server AST handlers
1486  * These are AST handlers used by server code.
1487  * Their property is that they are just preparing RPCs to be sent to clients.
1488  * @{
1489  */
1490 int ldlm_server_blocking_ast(struct ldlm_lock *, struct ldlm_lock_desc *,
1491                              void *data, int flag);
1492 int tgt_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
1493                      void *data, int flag);
1494 int ldlm_server_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data);
1495 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data);
1496 int ldlm_glimpse_locks(struct ldlm_resource *res,
1497                        struct list_head *gl_work_list);
1498 /** @} ldlm_srv_ast */
1499
1500 /** \defgroup ldlm_handlers Server LDLM handlers
1501  * These are handler functions that should be called by "frontends" such as
1502  * MDT or OST to pass through LDLM requests to LDLM for handling
1503  * @{
1504  */
1505 int ldlm_handle_enqueue(struct ldlm_namespace *ns, struct req_capsule *pill,
1506                         const struct ldlm_request *dlm_req,
1507                         const struct ldlm_callback_suite *cbs);
1508 int ldlm_handle_convert0(struct ptlrpc_request *req,
1509                          const struct ldlm_request *dlm_req);
1510 int ldlm_handle_cancel(struct ptlrpc_request *req);
1511 int ldlm_request_cancel(struct ptlrpc_request *req,
1512                         const struct ldlm_request *dlm_req,
1513                         int first, enum lustre_at_flags flags);
1514 /** @} ldlm_handlers */
1515
1516 void ldlm_revoke_export_locks(struct obd_export *exp);
1517 timeout_t ldlm_bl_timeout(struct ldlm_lock *lock);
1518 #endif
1519 int ldlm_del_waiting_lock(struct ldlm_lock *lock);
1520 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, timeout_t timeout);
1521 int ldlm_get_ref(void);
1522 void ldlm_put_ref(void);
1523 int ldlm_init_export(struct obd_export *exp);
1524 void ldlm_destroy_export(struct obd_export *exp);
1525 struct ldlm_lock *ldlm_request_lock(struct ptlrpc_request *req);
1526
1527 /* ldlm_lock.c */
1528 #ifdef HAVE_SERVER_SUPPORT
1529 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res);
1530 ldlm_reprocessing_policy
1531 ldlm_get_reprocessing_policy(struct ldlm_resource *res);
1532 #endif
1533 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg);
1534 void ldlm_lock2handle(const struct ldlm_lock *lock,
1535                       struct lustre_handle *lockh);
1536 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *lh,
1537                                      __u64 flags);
1538 void ldlm_cancel_callback(struct ldlm_lock *ll);
1539 int ldlm_lock_remove_from_lru(struct ldlm_lock *ll);
1540 int ldlm_lock_set_data(const struct lustre_handle *lockh, void *data);
1541
1542 /**
1543  * Obtain a lock reference by its handle.
1544  */
1545 static inline struct ldlm_lock *ldlm_handle2lock(const struct lustre_handle *h)
1546 {
1547         return __ldlm_handle2lock(h, 0);
1548 }
1549
1550 static inline struct ldlm_lock *
1551 ldlm_handle2lock_long(const struct lustre_handle *h, __u64 flags)
1552 {
1553         struct ldlm_lock *lock;
1554
1555         lock = __ldlm_handle2lock(h, flags);
1556         return lock;
1557 }
1558
1559 /**
1560  * Update Lock Value Block Operations (LVBO) on a resource taking into account
1561  * data from request \a r
1562  */
1563 static inline int ldlm_lvbo_update(struct ldlm_resource *res,
1564                                    struct ldlm_lock *lock,
1565                                    struct ptlrpc_request *req, int increase)
1566 {
1567         struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1568         int rc;
1569
1570         /* delayed lvb init may be required */
1571         rc = ldlm_lvbo_init(res);
1572         if (rc < 0) {
1573                 CERROR("delayed lvb init failed (rc %d)\n", rc);
1574                 return rc;
1575         }
1576
1577         if (ns->ns_lvbo && ns->ns_lvbo->lvbo_update)
1578                 return ns->ns_lvbo->lvbo_update(res, lock, req, increase);
1579
1580         return 0;
1581 }
1582
1583 static inline int ldlm_res_lvbo_update(struct ldlm_resource *res,
1584                                        struct ptlrpc_request *req,
1585                                        int increase)
1586 {
1587         return ldlm_lvbo_update(res, NULL, req, increase);
1588 }
1589
1590 int is_granted_or_cancelled_nolock(struct ldlm_lock *lock);
1591
1592 int ldlm_error2errno(enum ldlm_error error);
1593
1594 #if LUSTRE_TRACKS_LOCK_EXP_REFS
1595 void ldlm_dump_export_locks(struct obd_export *exp);
1596 #endif
1597
1598 #define ldlm_lock_list_put(head, member, count)                 \
1599 ({                                                              \
1600         struct ldlm_lock *_lock, *_next;                        \
1601         int c = count;                                          \
1602         list_for_each_entry_safe(_lock, _next, head, member) {  \
1603                 if (c-- == 0)                                   \
1604                         break;                                  \
1605                 list_del_init(&_lock->member);                  \
1606                 ldlm_lock_put(_lock);                           \
1607         }                                                       \
1608         LASSERT(c <= 0);                                        \
1609 })
1610
1611 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock);
1612 void ldlm_lock_put(struct ldlm_lock *lock);
1613 void ldlm_lock_destroy(struct ldlm_lock *lock);
1614 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
1615 void ldlm_lock_addref(const struct lustre_handle *lockh, enum ldlm_mode mode);
1616 int  ldlm_lock_addref_try(const struct lustre_handle *lockh,
1617                           enum ldlm_mode mode);
1618 void ldlm_lock_decref(const struct lustre_handle *lockh, enum ldlm_mode mode);
1619 void ldlm_lock_decref_and_cancel(const struct lustre_handle *lockh,
1620                                  enum ldlm_mode mode);
1621 void ldlm_lock_fail_match_locked(struct ldlm_lock *lock);
1622 void ldlm_lock_allow_match(struct ldlm_lock *lock);
1623 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock);
1624
1625 enum ldlm_mode ldlm_lock_match_with_skip(struct ldlm_namespace *ns,
1626                                          __u64 flags, __u64 skip_flags,
1627                                          const struct ldlm_res_id *res_id,
1628                                          enum ldlm_type type,
1629                                          union ldlm_policy_data *policy,
1630                                          enum ldlm_mode mode,
1631                                          enum ldlm_match_flags match_flags,
1632                                          struct lustre_handle *lh);
1633 static inline enum ldlm_mode ldlm_lock_match(struct ldlm_namespace *ns,
1634                                              __u64 flags,
1635                                              const struct ldlm_res_id *res_id,
1636                                              enum ldlm_type type,
1637                                              union ldlm_policy_data *policy,
1638                                              enum ldlm_mode mode,
1639                                              enum ldlm_match_flags m_flags,
1640                                              struct lustre_handle *lh)
1641 {
1642         return ldlm_lock_match_with_skip(ns, flags, 0, res_id, type, policy,
1643                                          mode, m_flags, lh);
1644 }
1645 struct ldlm_lock *search_itree(struct ldlm_resource *res,
1646                                struct ldlm_match_data *data);
1647 enum ldlm_mode ldlm_revalidate_lock_handle(const struct lustre_handle *lockh,
1648                                            enum mds_ibits_locks *bits);
1649 void ldlm_lock_mode_downgrade(struct ldlm_lock *lock, enum ldlm_mode new_mode);
1650 void ldlm_lock_cancel(struct ldlm_lock *lock);
1651 void ldlm_reprocess_all(struct ldlm_resource *res, enum mds_ibits_locks hint);
1652 void ldlm_reprocess_recovery_done(struct ldlm_namespace *ns);
1653 void ldlm_lock_dump_handle(int level, const struct lustre_handle *lockh);
1654 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req);
1655
1656 /* resource.c */
1657 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *obd, char *name,
1658                                           enum ldlm_side client,
1659                                           enum ldlm_appetite apt,
1660                                           enum ldlm_ns_type ns_type);
1661 int ldlm_namespace_cleanup(struct ldlm_namespace *ns, __u64 flags);
1662 void ldlm_namespace_free_prior(struct ldlm_namespace *ns,
1663                                struct obd_import *imp,
1664                                int force);
1665 void ldlm_namespace_free_post(struct ldlm_namespace *ns);
1666 void ldlm_namespace_free(struct ldlm_namespace *ns,
1667                          struct obd_import *imp, int force);
1668 void ldlm_namespace_register(struct ldlm_namespace *ns, enum ldlm_side client);
1669 void ldlm_namespace_unregister(struct ldlm_namespace *ns,
1670                                enum ldlm_side client);
1671 void ldlm_namespace_get(struct ldlm_namespace *ns);
1672 void ldlm_namespace_put(struct ldlm_namespace *ns);
1673
1674 int ldlm_debugfs_setup(void);
1675 void ldlm_debugfs_cleanup(void);
1676
1677 static inline void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
1678                                      struct lprocfs_stats *srv_stats)
1679 {
1680         int lock_type = 0, op = 0;
1681
1682         lock_type = dlm_req->lock_desc.l_resource.lr_type;
1683
1684         switch (lock_type) {
1685         case LDLM_PLAIN:
1686                 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
1687                 break;
1688         case LDLM_EXTENT:
1689                 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1690                 break;
1691         case LDLM_FLOCK:
1692                 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1693                 break;
1694         case LDLM_IBITS:
1695                 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1696                 break;
1697         default:
1698                 op = 0;
1699                 break;
1700         }
1701
1702         if (op != 0)
1703                 lprocfs_counter_incr(srv_stats, op);
1704 }
1705
1706 /* resource.c - internal */
1707 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
1708                                         const struct ldlm_res_id *,
1709                                         enum ldlm_type type, int create);
1710 struct ldlm_resource *ldlm_resource_getref(struct ldlm_resource *res);
1711 int ldlm_resource_putref(struct ldlm_resource *res);
1712 void ldlm_resource_add_lock(struct ldlm_resource *res,
1713                             struct list_head *head,
1714                             struct ldlm_lock *lock);
1715 void ldlm_resource_unlink_lock(struct ldlm_lock *lock);
1716 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
1717 void ldlm_dump_all_namespaces(enum ldlm_side client, int level);
1718 void ldlm_namespace_dump(int level, struct ldlm_namespace *ln);
1719 void ldlm_resource_dump(int level, struct ldlm_resource *lr);
1720 int ldlm_lock_change_resource(struct ldlm_namespace *ln, struct ldlm_lock *ll,
1721                               const struct ldlm_res_id *lri);
1722
1723 /* ldlm_request.c */
1724 /** \defgroup ldlm_local_ast Default AST handlers for local locks
1725  * These AST handlers are typically used for server-side local locks and are
1726  * also used by client-side lock handlers to perform minimum level base
1727  * processing.
1728  */
1729 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock);
1730 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
1731                       void *data, int flag);
1732 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp);
1733 int ldlm_completion_ast_async(struct ldlm_lock *lock, __u64 flags, void *data);
1734 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data);
1735 /** @} ldlm_local_ast */
1736
1737 /** \defgroup ldlm_cli_api API to operate on locks from actual LDLM users.
1738  * These are typically used by client and server (*_local versions)
1739  * to obtain and release locks.
1740  */
1741 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
1742                      struct ldlm_enqueue_info *einfo,
1743                      const struct ldlm_res_id *res_id,
1744                      union ldlm_policy_data const *policy, __u64 *flags,
1745                      void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
1746                      struct lustre_handle *lockh, int async);
1747 int ldlm_prep_enqueue_req(struct obd_export *exp,
1748                           struct ptlrpc_request *req,
1749                           struct list_head *cancels,
1750                           int count);
1751 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
1752                       int version, int opc, int canceloff,
1753                       struct list_head *cancels, int count);
1754
1755 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len);
1756 int ldlm_handle_enqueue(struct ldlm_namespace *ns, struct req_capsule *pill,
1757                         const struct ldlm_request *dlm_req,
1758                         const struct ldlm_callback_suite *cbs);
1759 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct req_capsule *pill,
1760                           struct ldlm_enqueue_info *einfo, __u8 with_policy,
1761                           __u64 *flags, void *lvb, __u32 lvb_len,
1762                           const struct lustre_handle *lockh, int rc,
1763                           bool request_slot);
1764 int ldlm_cli_enqueue_local(const struct lu_env *env,
1765                            struct ldlm_namespace *ns,
1766                            const struct ldlm_res_id *res_id,
1767                            enum ldlm_type type, union ldlm_policy_data *policy,
1768                            enum ldlm_mode mode, __u64 *flags,
1769                            ldlm_blocking_callback blocking,
1770                            ldlm_completion_callback completion,
1771                            ldlm_glimpse_callback glimpse,
1772                            void *data, __u32 lvb_len, enum lvb_type lvb_type,
1773                            const __u64 *client_cookie,
1774                            struct lustre_handle *lockh);
1775 int ldlm_cli_lock_create_pack(struct obd_export *exp,
1776                               struct ldlm_request *dlmreq,
1777                               struct ldlm_enqueue_info *einfo,
1778                               const struct ldlm_res_id *res_id,
1779                               union ldlm_policy_data const *policy,
1780                               __u64 *flags, void *lvb, __u32 lvb_len,
1781                               enum lvb_type lvb_type,
1782                               struct lustre_handle *lockh);
1783 int ldlm_cli_convert_req(struct ldlm_lock *lock, __u32 *flags, __u64 new_bits);
1784 int ldlm_cli_convert(struct ldlm_lock *lock,
1785                      enum ldlm_cancel_flags cancel_flags);
1786 int ldlm_cli_update_pool(struct ptlrpc_request *req);
1787 int ldlm_cli_cancel(const struct lustre_handle *lockh,
1788                     enum ldlm_cancel_flags cancel_flags);
1789 int ldlm_cli_cancel_unused(struct ldlm_namespace *n,
1790                            const struct ldlm_res_id *l,
1791                            enum ldlm_cancel_flags flags, void *opaque);
1792 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1793                                     const struct ldlm_res_id *res_id,
1794                                     union ldlm_policy_data *policy,
1795                                     enum ldlm_mode mode,
1796                                     enum ldlm_cancel_flags flags, void *opaque);
1797 int ldlm_cli_cancel_req(struct obd_export *exp, struct ldlm_lock *lock,
1798                         struct list_head *head, int count,
1799                         enum ldlm_cancel_flags flags);
1800 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1801                                struct list_head *cancels,
1802                                union ldlm_policy_data *policy,
1803                                enum ldlm_mode mode, __u64 lock_flags,
1804                                enum ldlm_cancel_flags cancel_flags,
1805                                void *opaque);
1806 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1807                                enum ldlm_cancel_flags flags);
1808 int ldlm_cli_cancel_list(struct list_head *head, int count,
1809                          struct ldlm_lock *primary,
1810                          struct ptlrpc_request *req,
1811                          enum ldlm_cancel_flags flags);
1812
1813 int ldlm_inodebits_drop(struct ldlm_lock *lock, enum mds_ibits_locks to_drop);
1814 int ldlm_cli_inodebits_convert(struct ldlm_lock *lock,
1815                                enum ldlm_cancel_flags cancel_flags);
1816
1817 /** @} ldlm_cli_api */
1818
1819
1820 /* mds/handler.c */
1821 /* This has to be here because recursive inclusion sucks. */
1822 int intent_disposition(struct ldlm_reply *rep, int flag);
1823 void intent_set_disposition(struct ldlm_reply *rep, int flag);
1824
1825 /**
1826  * "Modes" of acquiring lock_res, necessary to tell lockdep that taking more
1827  * than one lock_res is dead-lock safe.
1828  */
1829 enum lock_res_type {
1830         LRT_NORMAL,
1831         LRT_NEW,
1832 };
1833
1834 /** Lock resource. */
1835 static inline void lock_res(struct ldlm_resource *res)
1836 {
1837         spin_lock(&res->lr_lock);
1838 }
1839
1840 /** Lock resource with a way to instruct lockdep code about nestedness-safe. */
1841 static inline void lock_res_nested(struct ldlm_resource *res,
1842                                    enum lock_res_type mode)
1843 {
1844         spin_lock_nested(&res->lr_lock, mode);
1845 }
1846
1847 /** Unlock resource. */
1848 static inline void unlock_res(struct ldlm_resource *res)
1849 {
1850         spin_unlock(&res->lr_lock);
1851 }
1852
1853 /** Check if resource is already locked, assert if not. */
1854 static inline void check_res_locked(struct ldlm_resource *res)
1855 {
1856         assert_spin_locked(&res->lr_lock);
1857 }
1858
1859 struct ldlm_resource *lock_res_and_lock(struct ldlm_lock *lock);
1860 void unlock_res_and_lock(struct ldlm_lock *lock);
1861
1862 /* ldlm_pool.c */
1863 /** \defgroup ldlm_pools Various LDLM pool related functions
1864  * There are not used outside of ldlm.
1865  * @{
1866  */
1867 int ldlm_pools_init(void);
1868 void ldlm_pools_fini(void);
1869
1870 int ldlm_pool_init(struct ldlm_pool *pl, struct ldlm_namespace *ns,
1871                    int idx, enum ldlm_side client);
1872 int ldlm_pool_shrink(struct ldlm_pool *pl, int nr, gfp_t gfp_mask);
1873 void ldlm_pool_fini(struct ldlm_pool *pl);
1874 int ldlm_pool_setup(struct ldlm_pool *pl, int limit);
1875 time64_t ldlm_pool_recalc(struct ldlm_pool *pl, bool force);
1876 __u32 ldlm_pool_get_lvf(struct ldlm_pool *pl);
1877 __u64 ldlm_pool_get_slv(struct ldlm_pool *pl);
1878 __u32 ldlm_pool_get_limit(struct ldlm_pool *pl);
1879 void ldlm_pool_set_clv(struct ldlm_pool *pl, __u64 clv);
1880 void ldlm_pool_set_limit(struct ldlm_pool *pl, __u32 limit);
1881 void ldlm_pool_add(struct ldlm_pool *pl, struct ldlm_lock *lock);
1882 void ldlm_pool_del(struct ldlm_pool *pl, struct ldlm_lock *lock);
1883 /** @} */
1884
1885 static inline int ldlm_extent_overlap(const struct ldlm_extent *ex1,
1886                                       const struct ldlm_extent *ex2)
1887 {
1888         return ex1->start <= ex2->end && ex2->start <= ex1->end;
1889 }
1890
1891 /* check if @ex1 contains @ex2 */
1892 static inline int ldlm_extent_contain(const struct ldlm_extent *ex1,
1893                                       const struct ldlm_extent *ex2)
1894 {
1895         return ex1->start <= ex2->start && ex1->end >= ex2->end;
1896 }
1897
1898 #endif
1899 /** @} LDLM */