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