Whamcloud - gitweb
- Grammatical, LDLM updates to network.lyx
[fs/lustre-release.git] / lustre / ldlm / ldlm_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * by Cluster File Systems, Inc.
10  */
11
12 #define DEBUG_SUBSYSTEM S_LDLM
13
14 #include <linux/lustre_dlm.h>
15
16 int ldlm_completion_ast(struct ldlm_lock *lock, int flags)
17 {
18         ENTRY;
19
20         if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
21                       LDLM_FL_BLOCK_CONV)) {
22                 /* Go to sleep until the lock is granted. */
23                 /* FIXME: or cancelled. */
24                 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock,"
25                            " sleeping");
26                 ldlm_lock_dump(lock);
27                 ldlm_reprocess_all(lock->l_resource);
28                 wait_event(lock->l_waitq, (lock->l_req_mode ==
29                                            lock->l_granted_mode));
30                 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
31         } else if (flags == LDLM_FL_WAIT_NOREPROC) {
32                 wait_event(lock->l_waitq, (lock->l_req_mode ==
33                                            lock->l_granted_mode));
34         } else if (flags == 0) {
35                 wake_up(&lock->l_waitq);
36         }
37
38         RETURN(0);
39 }
40
41 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
42                                   struct lustre_handle *parent_lockh,
43                                   __u64 *res_id,
44                                   __u32 type,
45                                   void *cookie, int cookielen,
46                                   ldlm_mode_t mode,
47                                   int *flags,
48                                   ldlm_completion_callback completion,
49                                   ldlm_blocking_callback blocking,
50                                   void *data,
51                                   __u32 data_len,
52                                   struct lustre_handle *lockh)
53 {
54         struct ldlm_lock *lock;
55         int err;
56
57         if (ns->ns_client) {
58                 CERROR("Trying to cancel local lock\n");
59                 LBUG();
60         }
61
62         lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode, NULL, 0);
63         if (!lock)
64                 GOTO(out_nolock, err = -ENOMEM);
65         LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
66
67         ldlm_lock_addref_internal(lock, mode);
68         ldlm_lock2handle(lock, lockh);
69         lock->l_connh = NULL;
70
71         err = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
72                                 blocking);
73         if (err != ELDLM_OK)
74                 GOTO(out, err);
75
76         if (type == LDLM_EXTENT)
77                 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
78         if ((*flags) & LDLM_FL_LOCK_CHANGED)
79                 memcpy(res_id, lock->l_resource->lr_name, sizeof(*res_id));
80
81         LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
82                           lock);
83
84         if (lock->l_completion_ast)
85                 lock->l_completion_ast(lock, *flags);
86
87         LDLM_DEBUG(lock, "client-side local enqueue END");
88         EXIT;
89  out:
90         LDLM_LOCK_PUT(lock);
91  out_nolock:
92         return err;
93 }
94
95 int ldlm_cli_enqueue(struct lustre_handle *connh,
96                      struct ptlrpc_request *req,
97                      struct ldlm_namespace *ns,
98                      struct lustre_handle *parent_lock_handle,
99                      __u64 *res_id,
100                      __u32 type,
101                      void *cookie, int cookielen,
102                      ldlm_mode_t mode,
103                      int *flags,
104                      ldlm_completion_callback completion,
105                      ldlm_blocking_callback blocking,
106                      void *data,
107                      __u32 data_len,
108                      struct lustre_handle *lockh)
109 {
110         struct ptlrpc_connection *connection;
111         struct ldlm_lock *lock;
112         struct ldlm_request *body;
113         struct ldlm_reply *reply;
114         int rc, size = sizeof(*body), req_passed_in = 1;
115         ENTRY;
116
117         if (connh == NULL)
118                 return ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
119                                               type, cookie, cookielen, mode,
120                                               flags, completion, blocking, data,
121                                               data_len, lockh);
122         connection = client_conn2cli(connh)->cl_conn;
123
124         *flags = 0;
125         lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type, mode,
126                                 data, data_len);
127         if (lock == NULL)
128                 GOTO(out_nolock, rc = -ENOMEM);
129         LDLM_DEBUG(lock, "client-side enqueue START");
130         /* for the local lock, add the reference */
131         ldlm_lock_addref_internal(lock, mode);
132         ldlm_lock2handle(lock, lockh);
133
134         if (req == NULL) {
135                 req = ptlrpc_prep_req2(connh, LDLM_ENQUEUE, 1, &size, NULL);
136                 if (!req)
137                         GOTO(out, rc = -ENOMEM);
138                 req_passed_in = 0;
139         } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
140                 LBUG();
141
142         /* Dump all of this data into the request buffer */
143         body = lustre_msg_buf(req->rq_reqmsg, 0);
144         ldlm_lock2desc(lock, &body->lock_desc);
145         /* Phil: make this part of ldlm_lock2desc */
146         if (type == LDLM_EXTENT)
147                 memcpy(&body->lock_desc.l_extent, cookie,
148                        sizeof(body->lock_desc.l_extent));
149         body->lock_flags = *flags;
150
151         memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
152         if (parent_lock_handle)
153                 memcpy(&body->lock_handle2, parent_lock_handle,
154                        sizeof(body->lock_handle2));
155
156         /* Continue as normal. */
157         if (!req_passed_in) {
158                 size = sizeof(*reply);
159                 req->rq_replen = lustre_msg_size(1, &size);
160         }
161         lock->l_connh = connh;
162         lock->l_connection = ptlrpc_connection_addref(connection);
163         lock->l_client = client_conn2cli(connh)->cl_client;
164
165         rc = ptlrpc_queue_wait(req);
166         /* FIXME: status check here? */
167         rc = ptlrpc_check_status(req, rc);
168
169         if (rc != ELDLM_OK) {
170                 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
171                            rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
172                 ldlm_lock_decref(lockh, mode);
173                 /* FIXME: if we've already received a completion AST, this will
174                  * LBUG! */
175                 ldlm_lock_destroy(lock);
176                 GOTO(out, rc);
177         }
178
179         reply = lustre_msg_buf(req->rq_repmsg, 0);
180         memcpy(&lock->l_remote_handle, &reply->lock_handle,
181                sizeof(lock->l_remote_handle));
182         if (type == LDLM_EXTENT)
183                 memcpy(cookie, &reply->lock_extent, sizeof(reply->lock_extent));
184         *flags = reply->lock_flags;
185
186         CDEBUG(D_INFO, "remote handle: %p, flags: %d\n",
187                (void *)(unsigned long)reply->lock_handle.addr, *flags);
188         CDEBUG(D_INFO, "extent: %Lu -> %Lu\n",
189                (unsigned long long)reply->lock_extent.start,
190                (unsigned long long)reply->lock_extent.end);
191
192         /* If enqueue returned a blocked lock but the completion handler has
193          * already run, then it fixed up the resource and we don't need to do it
194          * again. */
195         if ((*flags) & LDLM_FL_LOCK_CHANGED) {
196                 int newmode = reply->lock_mode;
197                 if (newmode && newmode != lock->l_req_mode) {
198                         LDLM_DEBUG(lock, "server returned different mode %s",
199                                    ldlm_lockname[newmode]);
200                         lock->l_req_mode = newmode;
201                 }
202
203                 if (reply->lock_resource_name[0] !=
204                     lock->l_resource->lr_name[0]) {
205                         CDEBUG(D_INFO, "remote intent success, locking %ld "
206                                "instead of %ld\n",
207                                (long)reply->lock_resource_name[0],
208                                (long)lock->l_resource->lr_name[0]);
209
210                         ldlm_lock_change_resource(lock,
211                                                   reply->lock_resource_name);
212                         if (lock->l_resource == NULL) {
213                                 LBUG();
214                                 RETURN(-ENOMEM);
215                         }
216                         LDLM_DEBUG(lock, "client-side enqueue, new resource");
217                 }
218         }
219
220         if (!req_passed_in)
221                 ptlrpc_free_req(req);
222
223         rc = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
224                                blocking);
225         if (lock->l_completion_ast)
226                 lock->l_completion_ast(lock, *flags);
227
228         LDLM_DEBUG(lock, "client-side enqueue END");
229         EXIT;
230  out:
231         LDLM_LOCK_PUT(lock);
232  out_nolock:
233         return rc;
234 }
235
236 int ldlm_match_or_enqueue(struct lustre_handle *connh,
237                           struct ptlrpc_request *req,
238                           struct ldlm_namespace *ns,
239                           struct lustre_handle *parent_lock_handle,
240                           __u64 *res_id,
241                           __u32 type,
242                           void *cookie, int cookielen,
243                           ldlm_mode_t mode,
244                           int *flags,
245                           ldlm_completion_callback completion,
246                           ldlm_blocking_callback blocking,
247                           void *data,
248                           __u32 data_len,
249                           struct lustre_handle *lockh)
250 {
251         int rc;
252         ENTRY;
253         rc = ldlm_lock_match(ns, res_id, type, cookie, cookielen, mode, lockh);
254         if (rc == 0) {
255                 rc = ldlm_cli_enqueue(connh, req, ns,
256                                       parent_lock_handle, res_id, type, cookie,
257                                       cookielen, mode, flags, completion,
258                                       blocking, data, data_len, lockh);
259                 if (rc != ELDLM_OK)
260                         CERROR("ldlm_cli_enqueue: err: %d\n", rc);
261                 RETURN(rc);
262         } else
263                 RETURN(0);
264 }
265
266 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
267                                   int *flags)
268 {
269
270         if (lock->l_resource->lr_namespace->ns_client) {
271                 CERROR("Trying to cancel local lock\n");
272                 LBUG();
273         }
274         LDLM_DEBUG(lock, "client-side local convert");
275
276         ldlm_lock_convert(lock, new_mode, flags);
277         ldlm_reprocess_all(lock->l_resource);
278
279         LDLM_DEBUG(lock, "client-side local convert handler END");
280         LDLM_LOCK_PUT(lock);
281         RETURN(0);
282 }
283
284 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
285 {
286         struct ldlm_request *body;
287         struct lustre_handle *connh;
288         struct ldlm_reply *reply;
289         struct ldlm_lock *lock;
290         struct ldlm_resource *res;
291         struct ptlrpc_request *req;
292         int rc, size = sizeof(*body);
293         ENTRY;
294
295         lock = ldlm_handle2lock(lockh);
296         if (!lock) {
297                 LBUG();
298                 RETURN(-EINVAL);
299         }
300         *flags = 0;
301         connh = lock->l_connh;
302
303         if (!connh)
304                 return ldlm_cli_convert_local(lock, new_mode, flags);
305
306         LDLM_DEBUG(lock, "client-side convert");
307
308         req = ptlrpc_prep_req2(connh, LDLM_CONVERT, 1, &size, NULL);
309         if (!req)
310                 GOTO(out, rc = -ENOMEM);
311
312         body = lustre_msg_buf(req->rq_reqmsg, 0);
313         memcpy(&body->lock_handle1, &lock->l_remote_handle,
314                sizeof(body->lock_handle1));
315
316         body->lock_desc.l_req_mode = new_mode;
317         body->lock_flags = *flags;
318
319         size = sizeof(*reply);
320         req->rq_replen = lustre_msg_size(1, &size);
321
322         rc = ptlrpc_queue_wait(req);
323         rc = ptlrpc_check_status(req, rc);
324         if (rc != ELDLM_OK)
325                 GOTO(out, rc);
326
327         reply = lustre_msg_buf(req->rq_repmsg, 0);
328         res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
329         if (res != NULL)
330                 ldlm_reprocess_all(res);
331         /* Go to sleep until the lock is granted. */
332         /* FIXME: or cancelled. */
333         if (lock->l_completion_ast)
334                 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
335         EXIT;
336  out:
337         LDLM_LOCK_PUT(lock);
338         ptlrpc_free_req(req);
339         return rc;
340 }
341
342 int ldlm_cli_cancel(struct lustre_handle *lockh)
343 {
344         struct ptlrpc_request *req;
345         struct ldlm_lock *lock;
346         struct ldlm_request *body;
347         int rc = 0, size = sizeof(*body);
348         ENTRY;
349
350         lock = ldlm_handle2lock(lockh);
351         if (!lock) {
352                 /* It's possible that the decref that we did just before this
353                  * cancel was the last reader/writer, and caused a cancel before
354                  * we could call this function.  If we want to make this
355                  * impossible (by adding a dec_and_cancel() or similar), then
356                  * we can put the LBUG back. */
357                 //LBUG();
358                 RETURN(-EINVAL);
359         }
360
361         if (lock->l_connh) {
362                 LDLM_DEBUG(lock, "client-side cancel");
363                 req = ptlrpc_prep_req2(lock->l_connh, LDLM_CANCEL, 1, &size,
364                                        NULL);
365                 if (!req)
366                         GOTO(out, rc = -ENOMEM);
367
368                 body = lustre_msg_buf(req->rq_reqmsg, 0);
369                 memcpy(&body->lock_handle1, &lock->l_remote_handle,
370                        sizeof(body->lock_handle1));
371
372                 req->rq_replen = lustre_msg_size(0, NULL);
373
374                 rc = ptlrpc_queue_wait(req);
375                 rc = ptlrpc_check_status(req, rc);
376                 ptlrpc_free_req(req);
377                 if (rc != ELDLM_OK)
378                         GOTO(out, rc);
379
380                 ldlm_lock_cancel(lock);
381         } else {
382                 LDLM_DEBUG(lock, "client-side local cancel");
383                 if (lock->l_resource->lr_namespace->ns_client) {
384                         CERROR("Trying to cancel local lock\n");
385                         LBUG();
386                 }
387                 ldlm_lock_cancel(lock);
388                 ldlm_reprocess_all(lock->l_resource);
389                 LDLM_DEBUG(lock, "client-side local cancel handler END");
390         }
391
392         EXIT;
393  out:
394         LDLM_LOCK_PUT(lock);
395         return rc;
396 }