Whamcloud - gitweb
- rename ha_mgr to recovd
[fs/lustre-release.git] / lustre / include / linux / lustre_dlm.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 #ifndef _LUSTRE_DLM_H__
6 #define _LUSTRE_DLM_H__
7
8 #ifdef __KERNEL__
9
10 #include <linux/obd_class.h>
11 #include <linux/lustre_net.h>
12
13 #define OBD_LDLM_DEVICENAME  "ldlm"
14
15 typedef int cluster_host;
16 typedef int cluster_pid;
17
18 typedef enum {
19         ELDLM_OK = 0,
20
21         ELDLM_LOCK_CHANGED = 300,
22
23         ELDLM_NAMESPACE_EXISTS = 400,
24         ELDLM_BAD_NAMESPACE    = 401
25 } ldlm_error_t;
26
27 #define LDLM_FL_LOCK_CHANGED   (1 << 0)
28 #define LDLM_FL_BLOCK_GRANTED  (1 << 1)
29 #define LDLM_FL_BLOCK_CONV     (1 << 2)
30 #define LDLM_FL_BLOCK_WAIT     (1 << 3)
31
32 #define L2B(c) (1 << c)
33
34 /* compatibility matrix */
35 #define LCK_COMPAT_EX  L2B(LCK_NL)
36 #define LCK_COMPAT_PW  (LCK_COMPAT_EX | L2B(LCK_CR))
37 #define LCK_COMPAT_PR  (LCK_COMPAT_PW | L2B(LCK_PR))
38 #define LCK_COMPAT_CW  (LCK_COMPAT_PW | L2B(LCK_CW))
39 #define LCK_COMPAT_CR  (LCK_COMPAT_CW | L2B(LCK_PR) | L2B(LCK_PW))
40 #define LCK_COMPAT_NL  (LCK_COMPAT_CR | L2B(LCK_EX))
41
42 static ldlm_mode_t lck_compat_array[] = {
43         [LCK_EX] LCK_COMPAT_EX,
44         [LCK_PW] LCK_COMPAT_PW,
45         [LCK_PR] LCK_COMPAT_PR,
46         [LCK_CW] LCK_COMPAT_CW,
47         [LCK_CR] LCK_COMPAT_CR,
48         [LCK_NL] LCK_COMPAT_NL
49 };
50
51 static inline int lockmode_compat(ldlm_mode_t exist, ldlm_mode_t new)
52 {
53        if (exist < LCK_EX || exist > LCK_NL)
54               LBUG();
55        if (new < LCK_EX || new > LCK_NL)
56               LBUG();
57
58        return (lck_compat_array[exist] & L2B(new));
59 }
60
61 /* 
62  * 
63  * cluster name spaces 
64  *
65  */
66
67 #define DLM_OST_NAMESPACE 1
68 #define DLM_MDS_NAMESPACE 2
69
70 /* XXX 
71    - do we just separate this by security domains and use a prefix for 
72      multiple namespaces in the same domain? 
73    - 
74 */
75
76 struct ldlm_namespace {
77         struct list_head      ns_link;      /* in the list of ns's */
78         __u32                 ns_id;        /* identifier of ns */
79         struct list_head     *ns_hash;      /* hash table for ns */
80         atomic_t              ns_refcount;  /* count of resources in the hash */
81         struct list_head      ns_root_list; /* all root resources in ns */
82         struct obd_device    *ns_obddev;
83 };
84
85 /* 
86  * 
87  * Resource hash table 
88  *
89  */
90
91 #define RES_HASH_BITS 14
92 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
93 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
94
95 struct ldlm_lock;
96
97 typedef int (*ldlm_lock_callback)(struct ldlm_lock *lock, struct ldlm_lock *new,
98                                   void *data, __u32 data_len);
99
100 struct ldlm_lock {
101         struct ldlm_resource *l_resource;
102         struct ldlm_lock     *l_parent;
103         struct list_head      l_children;
104         struct list_head      l_childof;
105         struct list_head      l_res_link; /*position in one of three res lists*/
106         ldlm_mode_t           l_req_mode;
107         ldlm_mode_t           l_granted_mode;
108         ldlm_lock_callback    l_completion_ast;
109         ldlm_lock_callback    l_blocking_ast;
110         struct ptlrpc_connection *l_connection;
111         void                 *l_data;
112         __u32                 l_data_len;
113         struct ldlm_extent    l_extent;
114         struct ldlm_handle    l_remote_handle;
115         //void                 *l_event;
116         //XXX cluster_host    l_holder;
117         __u32                 l_version[RES_VERSION_SIZE];
118         wait_queue_head_t     l_waitq;
119 };
120
121 typedef int (*ldlm_res_compat)(struct ldlm_lock *child, struct ldlm_lock *new);
122 typedef int (*ldlm_res_policy)(struct ldlm_resource *parent,
123                                struct ldlm_extent *req_ex,
124                                struct ldlm_extent *new_ex,
125                                ldlm_mode_t mode, void *data);
126
127 #define LDLM_PLAIN       0x0
128 #define LDLM_EXTENT      0x1
129 #define LDLM_MDSINTENT   0x2
130
131 #define LDLM_MAX_TYPE    0x2
132 extern ldlm_res_compat ldlm_res_compat_table []; 
133 extern ldlm_res_policy ldlm_res_policy_table []; 
134
135 struct ldlm_resource {
136         struct ldlm_namespace *lr_namespace;
137         struct list_head       lr_hash;
138         struct list_head       lr_rootlink; /* link all root resources in NS */
139         struct ldlm_resource  *lr_parent;   /* 0 for a root resource */
140         struct list_head       lr_children; /* list head for child resources */
141         struct list_head       lr_childof;  /* part of child list of parent */
142
143         struct list_head       lr_granted;
144         struct list_head       lr_converting;
145         struct list_head       lr_waiting;
146         ldlm_mode_t            lr_most_restr;
147         atomic_t               lr_refcount;
148         __u32                  lr_type; /* PLAIN, EXTENT, or MDSINTENT */
149         struct ldlm_resource  *lr_root;
150         //XXX cluster_host          lr_master;
151         __u64                  lr_name[RES_NAME_SIZE];
152         __u32                  lr_version[RES_VERSION_SIZE];
153         spinlock_t             lr_lock;
154 };
155
156 static inline struct ldlm_extent *ldlm_res2extent(struct ldlm_resource *res)
157 {
158         return (struct ldlm_extent *)(res->lr_name);
159 }
160
161 static inline void *ldlm_handle2object(struct ldlm_handle *handle)
162 {
163         if (handle) 
164                 return (void *)(unsigned long)(handle->addr);
165         return NULL; 
166 }
167
168 static inline void ldlm_object2handle(void *object, struct ldlm_handle *handle)
169 {
170         handle->addr = (__u64)(unsigned long)object;
171 }
172
173 extern struct list_head ldlm_namespaces;
174 extern spinlock_t ldlm_spinlock;
175
176 static inline void ldlm_lock(void)
177 {
178         spin_lock(&ldlm_spinlock);
179 }
180
181 static inline void ldlm_unlock(void)
182 {
183         spin_unlock(&ldlm_spinlock);
184 }
185
186 extern struct obd_ops ldlm_obd_ops;
187
188 /* ldlm_extent.c */
189 int ldlm_extent_compat(struct ldlm_lock *, struct ldlm_lock *);
190 int ldlm_extent_policy(struct ldlm_resource *, struct ldlm_extent *,
191                        struct ldlm_extent *, ldlm_mode_t, void *);
192
193 /* ldlm_lock.c */
194 void ldlm_lock_free(struct ldlm_lock *lock);
195 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
196 ldlm_error_t ldlm_local_lock_create(__u32 ns_id,
197                                     struct ldlm_handle *parent_lock_handle,
198                                     __u64 *res_id,
199                                     __u32 type,
200                                     struct ldlm_handle *lockh);
201 ldlm_error_t ldlm_local_lock_enqueue(struct ldlm_handle *lockh,
202                                      ldlm_mode_t mode,
203                                      struct ldlm_extent *req_ex,
204                                      int *flags,
205                                      ldlm_lock_callback completion,
206                                      ldlm_lock_callback blocking,
207                                      void *data,
208                                      __u32 data_len);
209 ldlm_error_t ldlm_local_lock_convert(struct ldlm_handle *lockh,
210                                      int new_mode, int *flags);
211 ldlm_error_t ldlm_local_lock_cancel(struct ldlm_handle *lockh);
212 void ldlm_lock_dump(struct ldlm_lock *lock);
213
214 /* ldlm_test.c */
215 int ldlm_test(struct obd_device *device, struct ptlrpc_connection *conn);
216
217 /* resource.c */
218 struct ldlm_namespace *ldlm_namespace_find(__u32 id);
219 ldlm_error_t ldlm_namespace_new(struct obd_device *, __u32 id,
220                                 struct ldlm_namespace **);
221 int ldlm_namespace_free(struct ldlm_namespace *ns);
222 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
223                                         struct ldlm_resource *parent,
224                                         __u64 *name, __u32 type, int create);
225 int ldlm_resource_put(struct ldlm_resource *res);
226 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
227                             struct ldlm_lock *lock);
228 void ldlm_resource_del_lock(struct ldlm_lock *lock);
229 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
230 void ldlm_resource_dump(struct ldlm_resource *res);
231
232 /* ldlm_request.c */
233 int ldlm_cli_namespace_new(struct obd_device *, struct ptlrpc_client *,
234                            struct ptlrpc_connection *, __u32 ns_id);
235 int ldlm_cli_enqueue(struct ptlrpc_client *cl, struct ptlrpc_connection *peer,
236                      __u32 ns_id,
237                      struct ldlm_handle *parent_lock_handle,
238                      __u64 *res_id,
239                      __u32 type,
240                      struct ldlm_extent *req_ex,
241                      ldlm_mode_t mode,
242                      int *flags,
243                      ldlm_lock_callback completion,
244                      ldlm_lock_callback blocking,
245                      void *data,
246                      __u32 data_len,
247                      struct ldlm_handle *lockh,
248                      struct ptlrpc_request **request);
249 int ldlm_cli_callback(struct ldlm_lock *lock, struct ldlm_lock *new,
250                       void *data, __u32 data_len);
251 int ldlm_cli_convert(struct ptlrpc_client *, struct ldlm_handle *,
252                      int new_mode, int *flags, struct ptlrpc_request **);
253 int ldlm_cli_cancel(struct ptlrpc_client *, struct ldlm_handle *,
254                     struct ptlrpc_request **);
255
256
257 #endif /* __KERNEL__ */
258
259 /* ioctls for trying requests */
260 #define IOC_LDLM_TYPE                   'f'
261 #define IOC_LDLM_MIN_NR                 40
262
263 #define IOC_LDLM_TEST                   _IOWR('f', 40, long)
264 #define IOC_LDLM_MAX_NR                 41
265
266 #endif