Whamcloud - gitweb
b=625069
[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 file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #define DEBUG_SUBSYSTEM S_LDLM
23
24 #include <linux/lustre_dlm.h>
25 #include <linux/obd_class.h>
26 #include <linux/obd.h>
27
28 static int interrupted_completion_wait(void *data)
29 {
30         RETURN(1);
31 }
32
33 static int expired_completion_wait(void *data)
34 {
35         struct ldlm_lock *lock = data;
36         struct ptlrpc_connection *conn;
37         struct obd_device *obd;
38
39         if (!lock)
40                 CERROR("NULL lock\n");
41         else if (!lock->l_connh)
42                 CERROR("lock %p has NULL connh\n", lock);
43         else if (!(obd = class_conn2obd(lock->l_connh)))
44                 CERROR("lock %p has NULL obd\n", lock);
45         else if (!(conn = obd->u.cli.cl_import.imp_connection))
46                 CERROR("lock %p has NULL connection\n", lock);
47         else
48                 class_signal_connection_failure(conn);
49         RETURN(0);
50 }
51
52 int ldlm_completion_ast(struct ldlm_lock *lock, int flags)
53 {
54         struct l_wait_info lwi =
55                 LWI_TIMEOUT_INTR(obd_timeout * HZ, expired_completion_wait,
56                                  interrupted_completion_wait, lock);
57         int rc = 0;
58         ENTRY;
59
60         if (flags == LDLM_FL_WAIT_NOREPROC)
61                 goto noreproc;
62
63         if (flags == 0) {
64                 wake_up(&lock->l_waitq);
65                 RETURN(0);
66         }
67
68         if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
69                        LDLM_FL_BLOCK_CONV)))
70                 RETURN(0);
71
72         LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
73                    "sleeping");
74         ldlm_lock_dump(lock);
75         ldlm_reprocess_all(lock->l_resource);
76
77  noreproc:
78         /* Go to sleep until the lock is granted or cancelled. */
79         rc = l_wait_event(lock->l_waitq,
80                           ((lock->l_req_mode == lock->l_granted_mode) ||
81                            (lock->l_flags & LDLM_FL_DESTROYED)), &lwi);
82
83         if (lock->l_flags & LDLM_FL_DESTROYED) {
84                 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
85                 RETURN(-EIO);
86         }
87
88         if (rc) {
89                 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
90                            rc);
91                 RETURN(rc);
92         }
93
94         LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
95         RETURN(0);
96 }
97
98 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
99                                   struct lustre_handle *parent_lockh,
100                                   __u64 *res_id,
101                                   __u32 type,
102                                   void *cookie, int cookielen,
103                                   ldlm_mode_t mode,
104                                   int *flags,
105                                   ldlm_completion_callback completion,
106                                   ldlm_blocking_callback blocking,
107                                   void *data,
108                                   __u32 data_len,
109                                   struct lustre_handle *lockh)
110 {
111         struct ldlm_lock *lock;
112         int err;
113         ENTRY;
114
115         if (ns->ns_client) {
116                 CERROR("Trying to enqueue local lock in a shadow namespace\n");
117                 LBUG();
118         }
119
120         lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode, data,
121                                 data_len);
122         if (!lock)
123                 GOTO(out_nolock, err = -ENOMEM);
124         LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
125
126         ldlm_lock_addref_internal(lock, mode);
127         ldlm_lock2handle(lock, lockh);
128         lock->l_connh = NULL;
129
130         err = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
131                                 blocking);
132         if (err != ELDLM_OK)
133                 GOTO(out, err);
134
135         if (type == LDLM_EXTENT)
136                 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
137         if ((*flags) & LDLM_FL_LOCK_CHANGED)
138                 memcpy(res_id, lock->l_resource->lr_name, sizeof(*res_id));
139
140         LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
141                           lock);
142
143         if (lock->l_completion_ast)
144                 lock->l_completion_ast(lock, *flags);
145
146         LDLM_DEBUG(lock, "client-side local enqueue END");
147         EXIT;
148  out:
149         LDLM_LOCK_PUT(lock);
150  out_nolock:
151         return err;
152 }
153
154 int ldlm_cli_enqueue(struct lustre_handle *connh,
155                      struct ptlrpc_request *req,
156                      struct ldlm_namespace *ns,
157                      struct lustre_handle *parent_lock_handle,
158                      __u64 *res_id,
159                      __u32 type,
160                      void *cookie, int cookielen,
161                      ldlm_mode_t mode,
162                      int *flags,
163                      ldlm_completion_callback completion,
164                      ldlm_blocking_callback blocking,
165                      void *data,
166                      __u32 data_len,
167                      struct lustre_handle *lockh)
168 {
169         struct ldlm_lock *lock;
170         struct ldlm_request *body;
171         struct ldlm_reply *reply;
172         int rc, size = sizeof(*body), req_passed_in = 1;
173         ENTRY;
174
175         if (connh == NULL)
176                 return ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
177                                               type, cookie, cookielen, mode,
178                                               flags, completion, blocking, data,
179                                               data_len, lockh);
180
181         *flags = 0;
182         lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type, mode,
183                                 data, data_len);
184         if (lock == NULL)
185                 GOTO(out_nolock, rc = -ENOMEM);
186         LDLM_DEBUG(lock, "client-side enqueue START");
187         /* for the local lock, add the reference */
188         ldlm_lock_addref_internal(lock, mode);
189         ldlm_lock2handle(lock, lockh);
190
191         if (req == NULL) {
192                 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_ENQUEUE, 1,
193                                       &size, NULL);
194                 if (!req)
195                         GOTO(out, rc = -ENOMEM);
196                 req_passed_in = 0;
197         } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
198                 LBUG();
199
200         req->rq_request_portal = LDLM_REQUEST_PORTAL; /* XXX FIXME bug 625069 */
201         req->rq_reply_portal = LDLM_REPLY_PORTAL; /* XXX FIXME bug 625069 */
202
203         /* Dump all of this data into the request buffer */
204         body = lustre_msg_buf(req->rq_reqmsg, 0);
205         ldlm_lock2desc(lock, &body->lock_desc);
206         /* Phil: make this part of ldlm_lock2desc */
207         if (type == LDLM_EXTENT) {
208                 memcpy(&body->lock_desc.l_extent, cookie,
209                        sizeof(body->lock_desc.l_extent));
210                 CDEBUG(D_INFO, "extent in body: "LPU64" -> "LPU64"\n",
211                            body->lock_desc.l_extent.start,
212                            body->lock_desc.l_extent.end);
213         }
214         body->lock_flags = *flags;
215
216         memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
217         if (parent_lock_handle)
218                 memcpy(&body->lock_handle2, parent_lock_handle,
219                        sizeof(body->lock_handle2));
220
221         /* Continue as normal. */
222         if (!req_passed_in) {
223                 size = sizeof(*reply);
224                 req->rq_replen = lustre_msg_size(1, &size);
225         }
226         lock->l_connh = connh;
227         lock->l_export = NULL;
228
229         rc = ptlrpc_queue_wait(req);
230         /* FIXME: status check here? */
231         rc = ptlrpc_check_status(req, rc);
232
233         if (rc != ELDLM_OK) {
234                 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
235                            rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
236                 ldlm_lock_decref(lockh, mode);
237                 /* FIXME: if we've already received a completion AST, this will
238                  * LBUG! */
239                 ldlm_lock_destroy(lock);
240                 GOTO(out, rc);
241         }
242
243         reply = lustre_msg_buf(req->rq_repmsg, 0);
244         memcpy(&lock->l_remote_handle, &reply->lock_handle,
245                sizeof(lock->l_remote_handle));
246         if (type == LDLM_EXTENT)
247                 memcpy(cookie, &reply->lock_extent, sizeof(reply->lock_extent));
248         *flags = reply->lock_flags;
249
250         CDEBUG(D_INFO, "remote handle: %p, flags: %d\n",
251                (void *)(unsigned long)reply->lock_handle.addr, *flags);
252         CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got extent "
253                LPU64" -> "LPU64"\n",
254                body->lock_desc.l_extent.start, body->lock_desc.l_extent.end,
255                reply->lock_extent.start, reply->lock_extent.end);
256
257         /* If enqueue returned a blocked lock but the completion handler has
258          * already run, then it fixed up the resource and we don't need to do it
259          * again. */
260         if ((*flags) & LDLM_FL_LOCK_CHANGED) {
261                 int newmode = reply->lock_mode;
262                 if (newmode && newmode != lock->l_req_mode) {
263                         LDLM_DEBUG(lock, "server returned different mode %s",
264                                    ldlm_lockname[newmode]);
265                         lock->l_req_mode = newmode;
266                 }
267
268                 if (reply->lock_resource_name[0] !=
269                     lock->l_resource->lr_name[0]) {
270                         CDEBUG(D_INFO, "remote intent success, locking %ld "
271                                "instead of %ld\n",
272                                (long)reply->lock_resource_name[0],
273                                (long)lock->l_resource->lr_name[0]);
274
275                         ldlm_lock_change_resource(lock,
276                                                   reply->lock_resource_name);
277                         if (lock->l_resource == NULL) {
278                                 LBUG();
279                                 RETURN(-ENOMEM);
280                         }
281                         LDLM_DEBUG(lock, "client-side enqueue, new resource");
282                 }
283         }
284
285         if (!req_passed_in)
286                 ptlrpc_req_finished(req);
287
288         rc = ldlm_lock_enqueue(lock, cookie, cookielen, flags, completion,
289                                blocking);
290         if (lock->l_completion_ast)
291                 lock->l_completion_ast(lock, *flags);
292
293         LDLM_DEBUG(lock, "client-side enqueue END");
294         EXIT;
295  out:
296         LDLM_LOCK_PUT(lock);
297  out_nolock:
298         return rc;
299 }
300
301 int ldlm_match_or_enqueue(struct lustre_handle *connh,
302                           struct ptlrpc_request *req,
303                           struct ldlm_namespace *ns,
304                           struct lustre_handle *parent_lock_handle,
305                           __u64 *res_id,
306                           __u32 type,
307                           void *cookie, int cookielen,
308                           ldlm_mode_t mode,
309                           int *flags,
310                           ldlm_completion_callback completion,
311                           ldlm_blocking_callback blocking,
312                           void *data,
313                           __u32 data_len,
314                           struct lustre_handle *lockh)
315 {
316         int rc;
317         ENTRY;
318         rc = ldlm_lock_match(ns, res_id, type, cookie, cookielen, mode, lockh);
319         if (rc == 0) {
320                 rc = ldlm_cli_enqueue(connh, req, ns,
321                                       parent_lock_handle, res_id, type, cookie,
322                                       cookielen, mode, flags, completion,
323                                       blocking, data, data_len, lockh);
324                 if (rc != ELDLM_OK)
325                         CERROR("ldlm_cli_enqueue: err: %d\n", rc);
326                 RETURN(rc);
327         } else
328                 RETURN(0);
329 }
330
331 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
332                                   int *flags)
333 {
334
335         if (lock->l_resource->lr_namespace->ns_client) {
336                 CERROR("Trying to cancel local lock\n");
337                 LBUG();
338         }
339         LDLM_DEBUG(lock, "client-side local convert");
340
341         ldlm_lock_convert(lock, new_mode, flags);
342         ldlm_reprocess_all(lock->l_resource);
343
344         LDLM_DEBUG(lock, "client-side local convert handler END");
345         LDLM_LOCK_PUT(lock);
346         RETURN(0);
347 }
348
349 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
350  * conversion of locks which are on the waiting or converting queue */
351 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
352 {
353         struct ldlm_request *body;
354         struct lustre_handle *connh;
355         struct ldlm_reply *reply;
356         struct ldlm_lock *lock;
357         struct ldlm_resource *res;
358         struct ptlrpc_request *req;
359         int rc, size = sizeof(*body);
360         ENTRY;
361
362         lock = ldlm_handle2lock(lockh);
363         if (!lock) {
364                 LBUG();
365                 RETURN(-EINVAL);
366         }
367         *flags = 0;
368         connh = lock->l_connh;
369
370         if (!connh)
371                 return ldlm_cli_convert_local(lock, new_mode, flags);
372
373         LDLM_DEBUG(lock, "client-side convert");
374
375         req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_CONVERT, 1, &size,
376                               NULL);
377         if (!req)
378                 GOTO(out, rc = -ENOMEM);
379
380         req->rq_request_portal = LDLM_REQUEST_PORTAL; /* XXX FIXME bug 625069 */
381         req->rq_reply_portal = LDLM_REPLY_PORTAL; /* XXX FIXME bug 625069 */
382
383         body = lustre_msg_buf(req->rq_reqmsg, 0);
384         memcpy(&body->lock_handle1, &lock->l_remote_handle,
385                sizeof(body->lock_handle1));
386
387         body->lock_desc.l_req_mode = new_mode;
388         body->lock_flags = *flags;
389
390         size = sizeof(*reply);
391         req->rq_replen = lustre_msg_size(1, &size);
392
393         rc = ptlrpc_queue_wait(req);
394         rc = ptlrpc_check_status(req, rc);
395         if (rc != ELDLM_OK)
396                 GOTO(out, rc);
397
398         reply = lustre_msg_buf(req->rq_repmsg, 0);
399         res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
400         if (res != NULL)
401                 ldlm_reprocess_all(res);
402         /* Go to sleep until the lock is granted. */
403         /* FIXME: or cancelled. */
404         if (lock->l_completion_ast)
405                 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
406         EXIT;
407  out:
408         LDLM_LOCK_PUT(lock);
409         ptlrpc_req_finished(req);
410         return rc;
411 }
412
413 int ldlm_cli_cancel(struct lustre_handle *lockh)
414 {
415         struct ptlrpc_request *req;
416         struct ldlm_lock *lock;
417         struct ldlm_request *body;
418         int rc = 0, size = sizeof(*body);
419         ENTRY;
420
421         lock = ldlm_handle2lock(lockh);
422         if (!lock) {
423                 /* It's possible that the decref that we did just before this
424                  * cancel was the last reader/writer, and caused a cancel before
425                  * we could call this function.  If we want to make this
426                  * impossible (by adding a dec_and_cancel() or similar), then
427                  * we can put the LBUG back. */
428                 //LBUG();
429                 RETURN(-EINVAL);
430         }
431
432         if (lock->l_connh) {
433                 LDLM_DEBUG(lock, "client-side cancel");
434                 /* Set this flag to prevent others from getting new references*/
435                 l_lock(&lock->l_resource->lr_namespace->ns_lock);
436                 lock->l_flags |= LDLM_FL_CBPENDING;
437                 ldlm_cancel_callback(lock);
438                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
439
440                 req = ptlrpc_prep_req(class_conn2cliimp(lock->l_connh),
441                                       LDLM_CANCEL, 1, &size, NULL);
442                 if (!req)
443                         GOTO(out, rc = -ENOMEM);
444
445                 /* XXX FIXME bug 625069 */
446                 req->rq_request_portal = LDLM_REQUEST_PORTAL;
447                 req->rq_reply_portal = LDLM_REPLY_PORTAL;
448
449                 body = lustre_msg_buf(req->rq_reqmsg, 0);
450                 memcpy(&body->lock_handle1, &lock->l_remote_handle,
451                        sizeof(body->lock_handle1));
452
453                 req->rq_replen = lustre_msg_size(0, NULL);
454
455                 rc = ptlrpc_queue_wait(req);
456                 rc = ptlrpc_check_status(req, rc);
457                 ptlrpc_req_finished(req);
458                 if (rc != ELDLM_OK)
459                         GOTO(out, rc);
460
461                 ldlm_lock_cancel(lock);
462         } else {
463                 LDLM_DEBUG(lock, "client-side local cancel");
464                 if (lock->l_resource->lr_namespace->ns_client) {
465                         CERROR("Trying to cancel local lock\n");
466                         LBUG();
467                 }
468                 ldlm_lock_cancel(lock);
469                 ldlm_reprocess_all(lock->l_resource);
470                 LDLM_DEBUG(lock, "client-side local cancel handler END");
471         }
472
473         EXIT;
474  out:
475         LDLM_LOCK_PUT(lock);
476         return rc;
477 }
478
479 /* Cancel all locks on a given resource that have 0 readers/writers.
480  *
481  * If 'local_only' is true, throw the locks away without trying to notify the
482  * server. */
483 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns, __u64 *res_id,
484                            int local_only)
485 {
486         struct ldlm_resource *res;
487         struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
488         struct ldlm_ast_work *w;
489         ENTRY;
490
491         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
492         if (res == NULL) {
493                 /* This is not a problem. */
494                 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id[0]);
495                 RETURN(0);
496         }
497
498         l_lock(&ns->ns_lock);
499         list_for_each(tmp, &res->lr_granted) {
500                 struct ldlm_lock *lock;
501                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
502
503                 if (lock->l_readers || lock->l_writers)
504                         continue;
505
506                 /* Setting the CBPENDING flag is a little misleading, but
507                  * prevents an important race; namely, once CBPENDING is set,
508                  * the lock can accumulate no more readers/writers.  Since
509                  * readers and writers are already zero here, ldlm_lock_decref
510                  * won't see this flag and call l_blocking_ast */
511                 lock->l_flags |= LDLM_FL_CBPENDING;
512
513                 OBD_ALLOC(w, sizeof(*w));
514                 LASSERT(w);
515
516                 w->w_lock = LDLM_LOCK_GET(lock);
517                 list_add(&w->w_list, &list);
518         }
519         l_unlock(&ns->ns_lock);
520
521         list_for_each_safe(tmp, next, &list) {
522                 struct lustre_handle lockh;
523                 int rc;
524                 w = list_entry(tmp, struct ldlm_ast_work, w_list);
525
526                 if (local_only)
527                         ldlm_lock_cancel(w->w_lock);
528                 else {
529                         ldlm_lock2handle(w->w_lock, &lockh);
530                         rc = ldlm_cli_cancel(&lockh);
531                         if (rc != ELDLM_OK)
532                                 CERROR("ldlm_cli_cancel: %d\n", rc);
533                 }
534                 LDLM_LOCK_PUT(w->w_lock);
535                 list_del(&w->w_list);
536                 OBD_FREE(w, sizeof(*w));
537         }
538
539         ldlm_resource_put(res);
540
541         RETURN(0);
542 }