1 /* SPDX-License-Identifier: GPL-2.0 */
4 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5 * Use is subject to license terms.
7 * Copyright (c) 2010, 2017, Intel Corporation.
11 * This file is part of Lustre, http://www.lustre.org/
14 /** \defgroup LDLM Lustre Distributed Lock Manager
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.
25 #ifndef _LUSTRE_DLM_H__
26 #define _LUSTRE_DLM_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>
38 #include "lustre_dlm_flags.h"
43 extern struct kset *ldlm_ns_kset;
44 extern struct kset *ldlm_svc_kset;
46 #define OBD_LDLM_DEVICENAME "ldlm"
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.
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)
60 * LDLM non-error return states
64 ELDLM_LOCK_MATCHED = 1,
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,
72 ELDLM_NAMESPACE_EXISTS = 400,
73 ELDLM_BAD_NAMESPACE = 401,
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.
84 LDLM_NAMESPACE_SERVER = 0x01,
85 LDLM_NAMESPACE_CLIENT = 0x02
89 * The blocking callback is overloaded to perform two functions. These flags
90 * indicate which operation should be performed.
92 #define LDLM_CB_BLOCKING 1
93 #define LDLM_CB_CANCELING 2
96 * \name Lock Compatibility Matrix.
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.
102 * There are nine lock modes along with a compatibility matrix to indicate if
103 * two locks are compatible.
105 * - EX: Exclusive mode. Before a new file is created, MDS requests EX lock
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.
117 * - GROUP: Group mode. Locks with the same group ID are compatible with each
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
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
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 */
150 extern enum ldlm_mode lck_compat_array[];
152 static inline void lockmode_verify(enum ldlm_mode mode)
154 LASSERT(mode > LCK_MODE_MIN && mode < LCK_MODE_END);
157 static inline int lockmode_compat(enum ldlm_mode exist_mode,
158 enum ldlm_mode new_mode)
160 return lck_compat_array[exist_mode] & new_mode;
164 * cluster name spaces
166 #define DLM_OST_NAMESPACE 1
167 #define DLM_MDS_NAMESPACE 2
170 * - do we just separate this by security domains and use a prefix for
171 * multiple namespaces in the same domain?
176 * Locking rules for LDLM:
181 * waiting_locks_spinlock
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)
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
209 struct ldlm_resource;
210 struct ldlm_namespace;
213 * Operations on LDLM pools.
214 * LDLM pool is a pool of locks in the namespace without any implicitly
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
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);
228 /** One second for pools thread check interval. Each pool has own period. */
229 #define LDLM_POOLS_THREAD_PERIOD (1)
231 /** ~6% margin for modest pools. See ldlm_pool.c for details. */
232 #define LDLM_POOLS_MODEST_MARGIN_SHIFT (4)
234 /** Default recalc period for server side pools in sec. */
235 #define LDLM_POOL_SRV_DEF_RECALC_PERIOD (1)
237 /** Default recalc period for client side pools in sec. */
238 #define LDLM_POOL_CLI_DEF_RECALC_PERIOD (10)
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.
246 /** Pool debugfs directory. */
247 struct dentry *pl_debugfs_entry;
248 /** Pool name, must be long enough to hold compound proc entry name. */
250 /** Lock for protecting SLV/CLV updates. */
252 /** Number of allowed locks in in pool, both, client and server side. */
254 /** Number of granted locks in */
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.
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. */
276 /** Pool statistics. */
277 struct lprocfs_stats *pl_stats;
280 struct kobject pl_kobj;
281 struct completion pl_kobj_unregister;
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);
289 typedef int (*ldlm_cancel_cbt)(struct ldlm_lock *lock);
291 typedef int (*ldlm_hp_handler_t)(struct ldlm_lock *lock);
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
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
303 * To ensure delayed LVB initialization, it is highly recommended to use the set
304 * of ldlm_[res_]lvbo_[init,update,fill]() functions.
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);
318 * LDLM pools related, type of lock pool in the namespace.
319 * Greedy means release cached locks aggressively
322 LDLM_NAMESPACE_GREEDY = BIT(0),
323 LDLM_NAMESPACE_MODEST = BIT(1),
327 * Default values for the "max_nolock_size", "contention_time" and
328 * "contended_locks" namespace tunables.
330 #define NS_DEFAULT_MAX_NOLOCK_BYTES 0
331 #define NS_DEFAULT_CONTENTION_SECONDS 2
332 #define NS_DEFAULT_CONTENDED_LOCKS 32
334 struct ldlm_ns_bucket {
335 /** back pointer to namespace */
336 struct ldlm_namespace *nsb_namespace;
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
342 struct adaptive_timeout nsb_at_estimate;
344 * Which res in the bucket should we start with the reclaim.
346 int nsb_reclaim_start;
347 /* counter of entries in this bucket */
352 /** LDLM namespace lock stats */
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 */
367 enum ldlm_namespace_flags {
369 * Flag to indicate the LRU cancel is in progress.
370 * Used to limit the process by 1 thread only.
374 /* Controls the stack trace log in ldlm_lock_debug */
377 * Flag to indicate the LRU recalc on RPC reply is in progress.
378 * Used to limit the process by 1 thread only.
381 /* lru_size is set even before connection */
382 LDLM_NS_LRU_SIZE_SET_BEFORE_CONN,
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.
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
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
407 struct ldlm_namespace {
408 /** Backward link to OBD, required for LDLM pool to store new SLV. */
409 struct obd_device *ns_obd;
411 /** Flag indicating if namespace is on client instead of server */
412 enum ldlm_side ns_client;
414 /** name of this namespace */
417 /** Resource hash table for namespace. */
418 struct cfs_hash *ns_rs_hash;
423 /** big refcount (by bucket) */
427 * Namespace connect flags supported by server (may be changed via
428 * /proc, LRU resize may be disabled/enabled).
430 __u64 ns_connect_flags;
432 /** Client side original connect flags supported by server. */
433 __u64 ns_orig_connect_flags;
435 /* namespace debugfs dir entry */
436 struct dentry *ns_debugfs_entry;
439 * Position in global namespace list linking all namespaces on
442 struct list_head ns_list_chain;
445 * List of unused locks for this namespace. This list is also called
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.
454 struct list_head ns_unused_list;
455 /** Number of locks in the LRU list above */
457 struct list_head *ns_last_pos;
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.
464 unsigned int ns_max_unused;
467 * Cancel batch, if unused lock count exceed lru_size
468 * Only be used if LRUR disable.
470 unsigned int ns_cancel_batch;
472 /* How much SLV should decrease in %% to trigger LRU cancel urgently. */
473 unsigned int ns_recalc_pct;
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.
481 * Server only: number of times we evicted clients due to lack of reply
484 unsigned int ns_timeouts;
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.
491 timeout_t ns_ctime_age_limit;
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.
499 ktime_t ns_dirty_age_limit;
501 * Used to rate-limit ldlm_namespace_dump calls.
502 * \see ldlm_namespace_dump. Increased by 10 seconds every time
505 time64_t ns_next_dump;
507 /** "policy" function that does actual lock conflict determination */
508 ldlm_res_policy ns_policy;
511 * LVB operations for this namespace.
512 * \see struct ldlm_valblock_ops
514 struct ldlm_valblock_ops *ns_lvbo;
517 * Used by filter code to store pointer to OBD of the service.
518 * Should be dropped in favor of \a ns_obd
523 * Wait queue used by __ldlm_namespace_free. Gets woken up every time
524 * a resource is removed.
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;
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
537 unsigned int ns_contended_locks;
540 * The resources in this namespace remember contended state during
541 * \a ns_contention_time, in seconds.
543 timeout_t ns_contention_time;
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
551 unsigned int ns_max_nolock_size;
553 /** Limit of parallel AST RPC count. */
554 unsigned int ns_max_parallel_ast;
557 * Callback to check if a lock is good to be canceled by ELC or
560 ldlm_cancel_cbt ns_cancel;
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).
566 ldlm_hp_handler_t ns_hp_handler;
568 /** LDLM lock stats */
569 struct lprocfs_stats *ns_stats;
572 * Which bucket should we start with the lock reclaim.
574 int ns_reclaim_start;
576 struct kobject ns_kobj; /* sysfs object */
577 struct completion ns_kobj_unregister;
579 /* See enum ldlm_namespace_flags */
580 DECLARE_BITMAP(ns_flags, LDLM_NS_NUM_FLAGS);
584 * Returns 1 if namespace \a ns is a client namespace.
586 static inline int ns_is_client(struct ldlm_namespace *ns)
589 LASSERT(ns->ns_client == LDLM_NAMESPACE_CLIENT ||
590 ns->ns_client == LDLM_NAMESPACE_SERVER);
591 return ns->ns_client == LDLM_NAMESPACE_CLIENT;
595 * Returns 1 if namespace \a ns is a server namespace.
597 static inline int ns_is_server(struct ldlm_namespace *ns)
600 LASSERT(ns->ns_client == LDLM_NAMESPACE_CLIENT ||
601 ns->ns_client == LDLM_NAMESPACE_SERVER);
602 return ns->ns_client == LDLM_NAMESPACE_SERVER;
606 * Returns 1 if namespace \a ns supports early lock cancel (ELC).
608 static inline int ns_connect_cancelset(struct ldlm_namespace *ns)
611 return !!(ns->ns_connect_flags & OBD_CONNECT_CANCELSET);
615 * Returns 1 if this namespace supports lru_resize.
617 static inline int ns_connect_lru_resize(struct ldlm_namespace *ns)
620 return !!(ns->ns_connect_flags & OBD_CONNECT_LRU_RESIZE);
623 static inline void ns_register_cancel(struct ldlm_namespace *ns,
630 static inline void ns_register_hp_handler(struct ldlm_namespace *ns,
631 ldlm_hp_handler_t arg)
634 ns->ns_hp_handler = arg;
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,
643 /** Type for completion callback function of a lock. */
644 typedef int (*ldlm_completion_callback)(struct ldlm_lock *lock, __u64 flags,
646 /** Type for glimpse callback function of a lock. */
647 typedef int (*ldlm_glimpse_callback)(struct ldlm_lock *lock, void *data);
649 /** Type for created callback function of a lock. */
650 typedef void (*ldlm_created_callback)(struct ldlm_lock *lock);
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;
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.
670 struct ldlm_cb_set_arg {
671 struct ptlrpc_request_set *set;
672 int type; /* LDLM_{CP,BL,GL}_CALLBACK */
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;
681 struct ldlm_cb_async_args {
682 struct ldlm_cb_set_arg *ca_set_arg;
683 struct ldlm_lock *ca_lock;
686 /** The ldlm_glimpse_work was slab allocated & must be freed accordingly.*/
687 #define LDLM_GL_WORK_SLAB_ALLOCATED 0x1
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
695 struct ldlm_interval_tree {
698 enum ldlm_mode lit_mode; /* lock mode */
699 struct interval_tree_root lit_root; /* actual interval tree */
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.
706 struct ldlm_ibits_queues {
707 struct list_head liq_waiting[MDS_INODELOCK_NUMBITS];
710 struct ldlm_ibits_node {
711 struct list_head lin_link[MDS_INODELOCK_NUMBITS];
712 struct ldlm_lock *lock;
715 struct ldlm_flock_node {
716 atomic_t lfn_unlock_pending;
717 bool lfn_needs_reprocess;
718 struct interval_tree_root lfn_root;
721 /** Whether to track references to exports by LDLM locks. */
722 #define LUSTRE_TRACKS_LOCK_EXP_REFS (0)
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 */
735 __u64 blocking_owner;
736 struct obd_export *blocking_export;
737 atomic_t blocking_refs;
741 union ldlm_policy_data {
742 struct ldlm_extent l_extent;
743 struct ldlm_flock l_flock;
744 struct ldlm_inodebits l_inodebits;
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);
763 * LDLM_GID_ANY is used to match any group id in ldlm_lock_match().
765 #define LDLM_GID_ANY ((__u64)-1)
768 * LDLM lock structure
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
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.
786 * Must be first in the structure.
788 struct portals_handle l_handle;
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.
795 struct ldlm_resource *l_resource;
797 * List item for client side LRU list.
798 * Protected by ns_lock in struct ldlm_namespace.
800 struct list_head l_lru;
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.
806 struct list_head l_res_link;
808 * Internal structures per lock type..
811 struct { /* LDLM_EXTENT locks only */
812 /* Originally requested extent for the extent lock. */
813 struct ldlm_extent l_req_extent;
816 struct list_head l_same_extent;
818 struct { /* LDLM_PLAIN and LDLM_IBITS locks */
820 * Protected by lr_lock, linkages to "skip lists".
821 * For explanations of skip lists see
822 * ldlm/ldlm_inodebits.c
824 struct list_head l_sl_mode;
825 struct list_head l_sl_policy;
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.
831 struct ost_lvb l_ost_lvb;
833 struct { /* LDLM_FLOCK locks */
835 * Per export hash of flock locks.
836 * Protected by per-bucket exp->exp_flock_hash locks.
838 struct hlist_node l_exp_flock_hash;
839 struct ldlm_lock *l_same_owner;
841 struct rb_node l_fl_rb;
842 u64 l_fl_subtree_last;
846 * Per export hash of locks.
847 * Protected by per-bucket exp->exp_lock_hash locks.
849 struct hlist_node l_exp_hash;
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;
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
862 unsigned int l_bl_ast_run:1;
864 /* content type for lock value block */
865 enum lvb_type l_lvb_type:3;
866 /* unsigned int l_unused_bits:10; */
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().
876 /** Lock completion handler pointer. Called when lock is granted. */
877 ldlm_completion_callback l_completion_ast;
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.
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).
888 ldlm_blocking_callback l_blocking_ast;
890 * Lock glimpse handler.
891 * Glimpse handler is used to obtain LVB updates from a client by
894 ldlm_glimpse_callback l_glimpse_ast;
898 * This is a pointer to actual client export for locks that were granted
899 * to clients. Used server-side.
901 struct obd_export *l_export;
903 * Lock connection export.
904 * Pointer to server export on a client.
906 struct obd_export *l_conn_export;
909 * Remote lock handle.
910 * If the lock is remote, this is the handle of the other side lock
913 struct lustre_handle l_remote_handle;
916 * Representation of private data specific for a lock type.
917 * Examples are: extent range for extent lock or bitmask for ibits locks
919 union ldlm_policy_data l_policy_data;
922 * Lock state flags. Protected by lr_lock.
923 * \see lustre_dlm_flags.h where the bits are defined.
928 * Lock r/w usage counters.
929 * Protected by lr_lock.
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.
938 wait_queue_head_t l_waitq;
941 * Time, in nanoseconds, last used by e.g. being matched by lock match.
945 /** Private storage for lock user. Opaque to LDLM. */
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.
955 time64_t l_blast_sent;
959 * Server-side-only members.
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.
967 __u64 l_client_cookie;
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.
976 struct list_head l_pending_chain;
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
983 * \see ost_rw_prolong_locks
985 time64_t l_callback_timestamp;
987 /** Local PID of process which created this lock. */
989 /* __u32 l_unused; */
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;
999 * Pointer to a conflicting lock that caused blocking AST to be sent
1002 struct ldlm_lock *l_blocking_lock;
1004 #if LUSTRE_TRACKS_LOCK_EXP_REFS
1005 /* Debugging stuff for bug 20498, for tracking export references. */
1006 /** number of export references taken */
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;
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.
1019 struct list_head l_exp_list;
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),
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)
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)
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)
1053 * Describe the overlap between two locks. itree_overlap_cb data.
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;
1061 __u64 lmd_skip_flags;
1062 enum ldlm_match_flags lmd_match;
1065 /** For uncommitted cross-MDT lock, store transno this lock belongs to */
1066 #define l_transno l_client_cookie
1068 /** For uncommitted cross-MDT lock, which is client lock, share with l_rk_ast
1069 * which is for server.
1071 #define l_slc_link l_rk_ast
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[];
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.
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.
1091 struct ldlm_resource {
1092 struct ldlm_ns_bucket *lr_ns_bucket;
1095 * List item for list in namespace hash.
1096 * protected by ns_lock.
1097 * Shared with linkage for RCU-delayed free.
1100 struct hlist_node lr_hash;
1101 struct rcu_head lr_rcu;
1104 /** Reference count for this resource */
1105 refcount_t lr_refcount;
1107 /** Spinlock to protect locks under this resource. */
1110 /* protected by lr_lock */
1112 /** List of locks in granted state */
1113 struct list_head lr_granted;
1115 * List of locks that could not be granted due to conflicts and
1116 * that are waiting for conflicts to go away
1118 struct list_head lr_waiting;
1119 /* List of locks that waiting to enqueueing for flock */
1120 struct list_head lr_enqueueing;
1123 /** Resource name */
1124 struct ldlm_res_id lr_name;
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;
1134 /* resource considered as contended, used only on server side*/
1135 time64_t lr_contention_time;
1137 * Associated inode, used only on client side.
1139 struct inode *lr_lvb_inode;
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]; */
1148 * Server-side-only lock value block elements.
1149 * To serialize lvbo_init.
1151 bool lr_lvb_initialized;
1153 struct mutex lr_lvb_mutex;
1154 /** protected by lr_lock */
1158 static inline int ldlm_is_granted(struct ldlm_lock *lock)
1160 return lock->l_req_mode == lock->l_granted_mode;
1163 static inline bool ldlm_has_layout(struct ldlm_lock *lock)
1165 return lock->l_resource->lr_type == LDLM_IBITS &&
1166 !!(lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_LAYOUT);
1169 static inline bool ldlm_has_dom(struct ldlm_lock *lock)
1171 return lock->l_resource->lr_type == LDLM_IBITS &&
1172 !!(lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_DOM);
1175 static inline bool ldlm_has_update(struct ldlm_lock *lock)
1177 return lock->l_resource->lr_type == LDLM_IBITS &&
1178 lock->l_policy_data.l_inodebits.bits & MDS_INODELOCK_UPDATE;
1181 static inline char *
1182 ldlm_ns_name(struct ldlm_namespace *ns)
1187 static inline struct ldlm_namespace *
1188 ldlm_res_to_ns(struct ldlm_resource *res)
1190 return res->lr_ns_bucket->nsb_namespace;
1193 static inline struct ldlm_namespace *
1194 ldlm_lock_to_ns(struct ldlm_lock *lock)
1196 return ldlm_res_to_ns(lock->l_resource);
1199 static inline char *
1200 ldlm_lock_to_ns_name(struct ldlm_lock *lock)
1202 return ldlm_ns_name(ldlm_lock_to_ns(lock));
1205 static inline struct adaptive_timeout *
1206 ldlm_lock_to_ns_at(struct ldlm_lock *lock)
1208 return &lock->l_resource->lr_ns_bucket->nsb_at_estimate;
1211 static inline int ldlm_lvbo_init(struct ldlm_resource *res)
1213 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1216 if (ns->ns_lvbo == NULL || ns->ns_lvbo->lvbo_init == NULL ||
1217 res->lr_lvb_initialized)
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);
1226 rc = ns->ns_lvbo->lvbo_init(res);
1228 CDEBUG(D_DLMTRACE, "lvbo_init failed for resource : rc = %d\n",
1230 if (res->lr_lvb_data != NULL) {
1231 OBD_FREE(res->lr_lvb_data, res->lr_lvb_len);
1232 res->lr_lvb_data = NULL;
1234 res->lr_lvb_len = rc;
1236 res->lr_lvb_initialized = true;
1238 mutex_unlock(&res->lr_lvb_mutex);
1242 static inline int ldlm_lvbo_size(struct ldlm_lock *lock)
1244 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
1246 if (ns->ns_lvbo != NULL && ns->ns_lvbo->lvbo_size != NULL)
1247 return ns->ns_lvbo->lvbo_size(lock);
1252 static inline int ldlm_lvbo_fill(struct ldlm_lock *lock, void *buf, int *len)
1254 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
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);
1262 CERROR("lock %p: delayed lvb init failed (rc %d)\n",
1266 return ns->ns_lvbo->lvbo_fill(lock, buf, len);
1271 struct ldlm_ast_work {
1272 struct ldlm_lock *w_lock;
1274 struct ldlm_lock_desc w_desc;
1275 struct list_head w_list;
1282 * Common ldlm_enqueue parameters
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 */
1300 #define ei_res_id ei_cb_gl
1302 enum ldlm_flock_flags {
1303 FA_FL_CANCEL_RQST = 1,
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);
1316 int (*fa_notify)(struct file_lock *, struct file_lock *, int);
1320 wait_queue_head_t fa_waitq;
1323 extern char *ldlm_lockname[];
1324 extern char *ldlm_typename[];
1325 extern const char *ldlm_it2str(enum ldlm_intent_flags it);
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
1332 #define LDLM_DEBUG_NOLOCK(format, a...) \
1333 CDEBUG(D_DLMTRACE, "### " format "\n", ##a)
1336 * Support function for lock information printing into debug logs.
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)) \
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, ...);
1358 * Rate-limited version of lock printing function.
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); \
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)
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, \
1376 LDLM_DEBUG_NOLOCK("no dlm lock: " fmt, ##a); \
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)
1386 * Three intentions can be used for the policy functions in
1387 * ldlm_processing_policy.
1389 * LDLM_PROCESS_RESCAN:
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.
1396 * LDLM_PROCESS_ENQUEUE:
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.
1403 * LDLM_PROCESS_RECOVERY:
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.
1411 enum ldlm_process_intention {
1412 LDLM_PROCESS_RESCAN = 0,
1413 LDLM_PROCESS_ENQUEUE = 1,
1414 LDLM_PROCESS_RECOVERY = 2,
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);
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);
1429 * Return values for lock iterators.
1430 * Also used during deciding of lock grants and cancellations.
1432 #define LDLM_ITER_CONTINUE 1 /* keep iterating */
1433 #define LDLM_ITER_STOP 2 /* stop iterating */
1435 typedef int (*ldlm_iterator_t)(struct ldlm_lock *, void *);
1436 typedef int (*ldlm_res_iterator_t)(struct ldlm_resource *, void *);
1438 /** \defgroup ldlm_iterator Lock iterators
1440 * LDLM provides for a way to iterate through every lock on a resource or
1441 * namespace or every resource in a namespace.
1444 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1446 void ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
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 */
1453 int ldlm_replay_locks(struct obd_import *imp);
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,
1462 __u64 ldlm_extent_shift_kms(struct ldlm_lock *lock, __u64 old_kms);
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;
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);
1477 struct ldlm_callback_suite {
1478 ldlm_completion_callback lcs_completion;
1479 ldlm_blocking_callback lcs_blocking;
1480 ldlm_glimpse_callback lcs_glimpse;
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.
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 */
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
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 */
1516 void ldlm_revoke_export_locks(struct obd_export *exp);
1517 timeout_t ldlm_bl_timeout(struct ldlm_lock *lock);
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);
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);
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,
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);
1543 * Obtain a lock reference by its handle.
1545 static inline struct ldlm_lock *ldlm_handle2lock(const struct lustre_handle *h)
1547 return __ldlm_handle2lock(h, 0);
1550 static inline struct ldlm_lock *
1551 ldlm_handle2lock_long(const struct lustre_handle *h, __u64 flags)
1553 struct ldlm_lock *lock;
1555 lock = __ldlm_handle2lock(h, flags);
1560 * Update Lock Value Block Operations (LVBO) on a resource taking into account
1561 * data from request \a r
1563 static inline int ldlm_lvbo_update(struct ldlm_resource *res,
1564 struct ldlm_lock *lock,
1565 struct ptlrpc_request *req, int increase)
1567 struct ldlm_namespace *ns = ldlm_res_to_ns(res);
1570 /* delayed lvb init may be required */
1571 rc = ldlm_lvbo_init(res);
1573 CERROR("delayed lvb init failed (rc %d)\n", rc);
1577 if (ns->ns_lvbo && ns->ns_lvbo->lvbo_update)
1578 return ns->ns_lvbo->lvbo_update(res, lock, req, increase);
1583 static inline int ldlm_res_lvbo_update(struct ldlm_resource *res,
1584 struct ptlrpc_request *req,
1587 return ldlm_lvbo_update(res, NULL, req, increase);
1590 int is_granted_or_cancelled_nolock(struct ldlm_lock *lock);
1592 int ldlm_error2errno(enum ldlm_error error);
1594 #if LUSTRE_TRACKS_LOCK_EXP_REFS
1595 void ldlm_dump_export_locks(struct obd_export *exp);
1598 #define ldlm_lock_list_put(head, member, count) \
1600 struct ldlm_lock *_lock, *_next; \
1602 list_for_each_entry_safe(_lock, _next, head, member) { \
1605 list_del_init(&_lock->member); \
1606 ldlm_lock_put(_lock); \
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);
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,
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)
1642 return ldlm_lock_match_with_skip(ns, flags, 0, res_id, type, policy,
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);
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,
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);
1674 int ldlm_debugfs_setup(void);
1675 void ldlm_debugfs_cleanup(void);
1677 static inline void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
1678 struct lprocfs_stats *srv_stats)
1680 int lock_type = 0, op = 0;
1682 lock_type = dlm_req->lock_desc.l_resource.lr_type;
1684 switch (lock_type) {
1686 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
1689 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1692 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1695 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1703 lprocfs_counter_incr(srv_stats, op);
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);
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
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 */
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.
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,
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);
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,
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,
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);
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);
1817 /** @} ldlm_cli_api */
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);
1826 * "Modes" of acquiring lock_res, necessary to tell lockdep that taking more
1827 * than one lock_res is dead-lock safe.
1829 enum lock_res_type {
1834 /** Lock resource. */
1835 static inline void lock_res(struct ldlm_resource *res)
1837 spin_lock(&res->lr_lock);
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)
1844 spin_lock_nested(&res->lr_lock, mode);
1847 /** Unlock resource. */
1848 static inline void unlock_res(struct ldlm_resource *res)
1850 spin_unlock(&res->lr_lock);
1853 /** Check if resource is already locked, assert if not. */
1854 static inline void check_res_locked(struct ldlm_resource *res)
1856 assert_spin_locked(&res->lr_lock);
1859 struct ldlm_resource *lock_res_and_lock(struct ldlm_lock *lock);
1860 void unlock_res_and_lock(struct ldlm_lock *lock);
1863 /** \defgroup ldlm_pools Various LDLM pool related functions
1864 * There are not used outside of ldlm.
1867 int ldlm_pools_init(void);
1868 void ldlm_pools_fini(void);
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);
1885 static inline int ldlm_extent_overlap(const struct ldlm_extent *ex1,
1886 const struct ldlm_extent *ex2)
1888 return ex1->start <= ex2->end && ex2->start <= ex1->end;
1891 /* check if @ex1 contains @ex2 */
1892 static inline int ldlm_extent_contain(const struct ldlm_extent *ex1,
1893 const struct ldlm_extent *ex2)
1895 return ex1->start <= ex2->start && ex1->end >= ex2->end;