Whamcloud - gitweb
Make the MDS filesystem interface code be a separate module. This allows
[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 #define LDLM_FL_DYING          (1 << 4)
32
33 #define L2B(c) (1 << c)
34
35 /* compatibility matrix */
36 #define LCK_COMPAT_EX  L2B(LCK_NL)
37 #define LCK_COMPAT_PW  (LCK_COMPAT_EX | L2B(LCK_CR))
38 #define LCK_COMPAT_PR  (LCK_COMPAT_PW | L2B(LCK_PR))
39 #define LCK_COMPAT_CW  (LCK_COMPAT_PW | L2B(LCK_CW))
40 #define LCK_COMPAT_CR  (LCK_COMPAT_CW | L2B(LCK_PR) | L2B(LCK_PW))
41 #define LCK_COMPAT_NL  (LCK_COMPAT_CR | L2B(LCK_EX))
42
43 static ldlm_mode_t lck_compat_array[] = {
44         [LCK_EX] LCK_COMPAT_EX,
45         [LCK_PW] LCK_COMPAT_PW,
46         [LCK_PR] LCK_COMPAT_PR,
47         [LCK_CW] LCK_COMPAT_CW,
48         [LCK_CR] LCK_COMPAT_CR,
49         [LCK_NL] LCK_COMPAT_NL
50 };
51
52 static inline int lockmode_compat(ldlm_mode_t exist, ldlm_mode_t new)
53 {
54        if (exist < LCK_EX || exist > LCK_NL)
55               LBUG();
56        if (new < LCK_EX || new > LCK_NL)
57               LBUG();
58
59        return (lck_compat_array[exist] & L2B(new));
60 }
61
62 /* 
63  * 
64  * cluster name spaces 
65  *
66  */
67
68 #define DLM_OST_NAMESPACE 1
69 #define DLM_MDS_NAMESPACE 2
70
71 /* XXX 
72    - do we just separate this by security domains and use a prefix for 
73      multiple namespaces in the same domain? 
74    - 
75 */
76
77 struct ldlm_namespace {
78         struct obd_device    *ns_obddev;
79         __u32                 ns_local;     /* is this a local lock tree? */
80         struct list_head     *ns_hash;      /* hash table for ns */
81         __u32                 ns_refcount;  /* count of resources in the hash */
82         struct list_head      ns_root_list; /* all root resources in ns */
83         spinlock_t            ns_lock;      /* protects hash, refcount, list */
84 };
85
86 /* 
87  * 
88  * Resource hash table 
89  *
90  */
91
92 #define RES_HASH_BITS 14
93 #define RES_HASH_SIZE (1UL << RES_HASH_BITS)
94 #define RES_HASH_MASK (RES_HASH_SIZE - 1)
95
96 struct ldlm_lock;
97
98 typedef int (*ldlm_lock_callback)(struct ldlm_lock *lock, struct ldlm_lock *new,
99                                   void *data, __u32 data_len);
100
101 struct ldlm_lock {
102         struct ldlm_resource *l_resource;
103         struct ldlm_lock     *l_parent;
104         struct list_head      l_children;
105         struct list_head      l_childof;
106         struct list_head      l_res_link; /*position in one of three res lists*/
107
108         ldlm_mode_t           l_req_mode;
109         ldlm_mode_t           l_granted_mode;
110
111         ldlm_lock_callback    l_completion_ast;
112         ldlm_lock_callback    l_blocking_ast;
113
114         struct ptlrpc_connection *l_connection;
115         struct ptlrpc_client *l_client;
116         __u32                 l_flags;
117         struct ldlm_handle    l_remote_handle;
118         void                 *l_data;
119         __u32                 l_data_len;
120         struct ldlm_extent    l_extent;
121         //void                 *l_event;
122         //XXX cluster_host    l_holder;
123         __u32                 l_version[RES_VERSION_SIZE];
124
125         __u32                 l_readers;
126         __u32                 l_writers;
127
128         /* If the lock is granted, a process sleeps on this waitq to learn when
129          * it's no longer in use.  If the lock is not granted, a process sleeps
130          * on this waitq to learn when it becomes granted. */
131         wait_queue_head_t     l_waitq;
132         spinlock_t            l_lock;
133 };
134
135 typedef int (*ldlm_res_compat)(struct ldlm_lock *child, struct ldlm_lock *new);
136 typedef int (*ldlm_res_policy)(struct ldlm_resource *parent,
137                                struct ldlm_extent *req_ex,
138                                struct ldlm_extent *new_ex,
139                                ldlm_mode_t mode, void *data);
140
141 #define LDLM_PLAIN       0
142 #define LDLM_EXTENT      1
143 #define LDLM_MDSINTENT   2
144
145 #define LDLM_MAX_TYPE 2
146
147 extern ldlm_res_compat ldlm_res_compat_table []; 
148 extern ldlm_res_policy ldlm_res_policy_table []; 
149
150 struct ldlm_resource {
151         struct ldlm_namespace *lr_namespace;
152         struct list_head       lr_hash;
153         struct list_head       lr_rootlink; /* link all root resources in NS */
154         struct ldlm_resource  *lr_parent;   /* 0 for a root resource */
155         struct list_head       lr_children; /* list head for child resources */
156         struct list_head       lr_childof;  /* part of child list of parent */
157
158         struct list_head       lr_granted;
159         struct list_head       lr_converting;
160         struct list_head       lr_waiting;
161         ldlm_mode_t            lr_most_restr;
162         __u32                  lr_type; /* PLAIN, EXTENT, or MDSINTENT */
163         struct ldlm_resource  *lr_root;
164         //XXX cluster_host          lr_master;
165         __u64                  lr_name[RES_NAME_SIZE];
166         __u32                  lr_version[RES_VERSION_SIZE];
167         __u32                  lr_refcount;
168         spinlock_t             lr_lock; /* protects lists, refcount */
169 };
170
171 static inline struct ldlm_extent *ldlm_res2extent(struct ldlm_resource *res)
172 {
173         return (struct ldlm_extent *)(res->lr_name);
174 }
175
176 static inline void *ldlm_handle2object(struct ldlm_handle *handle)
177 {
178         if (handle) 
179                 return (void *)(unsigned long)(handle->addr);
180         return NULL; 
181 }
182
183 static inline void ldlm_object2handle(void *object, struct ldlm_handle *handle)
184 {
185         handle->addr = (__u64)(unsigned long)object;
186 }
187
188 extern struct obd_ops ldlm_obd_ops;
189
190 /* ldlm_extent.c */
191 int ldlm_extent_compat(struct ldlm_lock *, struct ldlm_lock *);
192 int ldlm_extent_policy(struct ldlm_resource *, struct ldlm_extent *,
193                        struct ldlm_extent *, ldlm_mode_t, void *);
194
195 /* ldlm_lock.c */
196 void ldlm_lock_free(struct ldlm_lock *lock);
197 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc);
198 void ldlm_lock_addref(struct ldlm_lock *lock, __u32 mode);
199 void ldlm_lock_decref(struct ldlm_lock *lock, __u32 mode);
200 void ldlm_grant_lock(struct ldlm_resource *res, struct ldlm_lock *lock);
201 int ldlm_local_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
202                           struct ldlm_extent *extent, ldlm_mode_t mode,
203                           struct ldlm_handle *lockh);
204 ldlm_error_t ldlm_local_lock_create(struct ldlm_namespace *ns,
205                                     struct ldlm_handle *parent_lock_handle,
206                                     __u64 *res_id, __u32 type,
207                                      ldlm_mode_t mode,
208                                     void *data,
209                                     __u32 data_len,
210                                     struct ldlm_handle *lockh);
211 ldlm_error_t ldlm_local_lock_enqueue(struct ldlm_handle *lockh,
212                                      struct ldlm_extent *req_ex,
213                                      int *flags,
214                                      ldlm_lock_callback completion,
215                                      ldlm_lock_callback blocking);
216 struct ldlm_resource *ldlm_local_lock_convert(struct ldlm_handle *lockh,
217                                               int new_mode, int *flags);
218 struct ldlm_resource *ldlm_local_lock_cancel(struct ldlm_lock *lock);
219 void ldlm_reprocess_all(struct ldlm_resource *res);
220 void ldlm_lock_dump(struct ldlm_lock *lock);
221
222 /* ldlm_test.c */
223 int ldlm_test(struct obd_device *device, struct ptlrpc_connection *conn);
224
225 /* resource.c */
226 struct ldlm_namespace *ldlm_namespace_new(struct obd_device *, __u32 local);
227 int ldlm_namespace_free(struct ldlm_namespace *ns);
228 struct ldlm_resource *ldlm_resource_get(struct ldlm_namespace *ns,
229                                         struct ldlm_resource *parent,
230                                         __u64 *name, __u32 type, int create);
231 int ldlm_resource_put(struct ldlm_resource *res);
232 void ldlm_resource_add_lock(struct ldlm_resource *res, struct list_head *head,
233                             struct ldlm_lock *lock);
234 void ldlm_resource_del_lock(struct ldlm_lock *lock);
235 void ldlm_res2desc(struct ldlm_resource *res, struct ldlm_resource_desc *desc);
236 void ldlm_resource_dump(struct ldlm_resource *res);
237
238 /* ldlm_request.c */
239 int ldlm_cli_enqueue(struct ptlrpc_client *cl, struct ptlrpc_connection *peer,
240                      struct ldlm_namespace *ns,
241                      struct ldlm_handle *parent_lock_handle,
242                      __u64 *res_id,
243                      __u32 type,
244                      struct ldlm_extent *req_ex,
245                      ldlm_mode_t mode,
246                      int *flags,
247                      void *data,
248                      __u32 data_len,
249                      struct ldlm_handle *lockh);
250 int ldlm_cli_callback(struct ldlm_lock *lock, struct ldlm_lock *new,
251                       void *data, __u32 data_len);
252 int ldlm_cli_convert(struct ptlrpc_client *, struct ldlm_handle *,
253                      int new_mode, int *flags);
254 int ldlm_cli_cancel(struct ptlrpc_client *, struct ldlm_lock *);
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