Whamcloud - gitweb
land 0.5.20.3 b_devel onto HEAD (b_devel will remain)
[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, 2003 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 #ifndef __KERNEL__
24 #include <signal.h>
25 #include <liblustre.h>
26 #endif
27
28 #include <linux/lustre_dlm.h>
29 #include <linux/obd_class.h>
30 #include <linux/obd.h>
31
32 static int interrupted_completion_wait(void *data)
33 {
34         RETURN(1);
35 }
36
37 int ldlm_expired_completion_wait(void *data)
38 {
39         struct ldlm_lock *lock = data;
40         struct ptlrpc_connection *conn;
41         struct obd_device *obd;
42
43         if (!lock)
44                 CERROR("NULL lock\n");
45         else if (!lock->l_connh)
46                 CERROR("lock %p has NULL connh\n", lock);
47         else if (!(obd = class_conn2obd(lock->l_connh)))
48                 CERROR("lock %p has NULL obd\n", lock);
49         else if (!(conn = obd->u.cli.cl_import.imp_connection))
50                 CERROR("lock %p has NULL connection\n", lock);
51         else {
52                 LDLM_DEBUG(lock, "timed out waiting for completion");
53                 CERROR("lock %p timed out from %s\n", lock,
54                        conn->c_remote_uuid.uuid);
55                 ldlm_lock_dump(D_ERROR, lock);
56                 class_signal_connection_failure(conn);
57         }
58         RETURN(0);
59 }
60
61 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
62 {
63         struct l_wait_info lwi =
64                 LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
65                                  interrupted_completion_wait, lock);
66         int rc = 0;
67         ENTRY;
68
69         if (flags == LDLM_FL_WAIT_NOREPROC)
70                 goto noreproc;
71
72         if (flags == 0) {
73                 wake_up(&lock->l_waitq);
74                 RETURN(0);
75         }
76
77         if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
78                        LDLM_FL_BLOCK_CONV)))
79                 RETURN(0);
80
81         LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
82                    "sleeping");
83         ldlm_lock_dump(D_OTHER, lock);
84         ldlm_reprocess_all(lock->l_resource);
85
86  noreproc:
87         /* Go to sleep until the lock is granted or cancelled. */
88         rc = l_wait_event(lock->l_waitq,
89                           ((lock->l_req_mode == lock->l_granted_mode) ||
90                            lock->l_destroyed), &lwi);
91
92         if (lock->l_destroyed) {
93                 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
94                 RETURN(-EIO);
95         }
96
97         if (rc) {
98                 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
99                            rc);
100                 RETURN(rc);
101         }
102
103         LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
104         RETURN(0);
105 }
106
107 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
108                                   struct lustre_handle *parent_lockh,
109                                   struct ldlm_res_id res_id,
110                                   __u32 type,
111                                   void *cookie, int cookielen,
112                                   ldlm_mode_t mode,
113                                   int *flags,
114                                   ldlm_completion_callback completion,
115                                   ldlm_blocking_callback blocking,
116                                   void *data,
117                                   void *cp_data,
118                                   struct lustre_handle *lockh)
119 {
120         struct ldlm_lock *lock;
121         int err;
122         ENTRY;
123
124         if (ns->ns_client) {
125                 CERROR("Trying to enqueue local lock in a shadow namespace\n");
126                 LBUG();
127         }
128
129         lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode,
130                                 data, cp_data);
131         if (!lock)
132                 GOTO(out_nolock, err = -ENOMEM);
133         LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
134
135         ldlm_lock_addref_internal(lock, mode);
136         ldlm_lock2handle(lock, lockh);
137         lock->l_flags |= LDLM_FL_LOCAL;
138
139         err = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags, completion,
140                                 blocking);
141         if (err != ELDLM_OK)
142                 GOTO(out, err);
143
144         if (type == LDLM_EXTENT)
145                 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
146         if ((*flags) & LDLM_FL_LOCK_CHANGED)
147                 memcpy(&res_id, &lock->l_resource->lr_name, sizeof(res_id));
148
149         LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
150                           lock);
151
152         if (lock->l_completion_ast)
153                 lock->l_completion_ast(lock, *flags, NULL);
154
155         LDLM_DEBUG(lock, "client-side local enqueue END");
156         EXIT;
157  out:
158         LDLM_LOCK_PUT(lock);
159  out_nolock:
160         return err;
161 }
162
163 int ldlm_cli_enqueue(struct lustre_handle *connh,
164                      struct ptlrpc_request *req,
165                      struct ldlm_namespace *ns,
166                      struct lustre_handle *parent_lock_handle,
167                      struct ldlm_res_id res_id,
168                      __u32 type,
169                      void *cookie, int cookielen,
170                      ldlm_mode_t mode,
171                      int *flags,
172                      ldlm_completion_callback completion,
173                      ldlm_blocking_callback blocking,
174                      void *data,
175                      void *cp_data,
176                      struct lustre_handle *lockh)
177 {
178         struct ldlm_lock *lock;
179         struct ldlm_request *body;
180         struct ldlm_reply *reply;
181         int rc, size = sizeof(*body), req_passed_in = 1, is_replay;
182         ENTRY;
183
184         is_replay = *flags & LDLM_FL_REPLAY;
185         LASSERT(connh != NULL || !is_replay);
186
187         if (connh == NULL) {
188                 rc = ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
189                                             type, cookie, cookielen, mode,
190                                             flags, completion, blocking, data,
191                                             cp_data, lockh);
192                 RETURN(rc);
193         }
194
195         /* If we're replaying this lock, just check some invariants.
196          * If we're creating a new lock, get everything all setup nice. */
197         if (is_replay) {
198                 lock = ldlm_handle2lock(lockh);
199                 LDLM_DEBUG(lock, "client-side enqueue START");
200                 LASSERT(connh == lock->l_connh);
201         } else {
202                 lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type,
203                                         mode, data, cp_data);
204                 if (lock == NULL)
205                         GOTO(out_nolock, rc = -ENOMEM);
206                 /* ugh.  I set this early (instead of waiting for _enqueue)
207                  * because the completion AST might arrive early, and we need
208                  * (in just this one case) to run the completion_cb even if it
209                  * arrives before the reply. */
210                 lock->l_completion_ast = completion;
211                 LDLM_DEBUG(lock, "client-side enqueue START");
212                 /* for the local lock, add the reference */
213                 ldlm_lock_addref_internal(lock, mode);
214                 ldlm_lock2handle(lock, lockh);
215                 if (type == LDLM_EXTENT)
216                         memcpy(&lock->l_extent, cookie,
217                                sizeof(body->lock_desc.l_extent));
218         }
219
220         if (req == NULL) {
221                 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_ENQUEUE, 1,
222                                       &size, NULL);
223                 if (!req)
224                         GOTO(out, rc = -ENOMEM);
225                 req_passed_in = 0;
226         } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
227                 LBUG();
228
229         /* Dump lock data into the request buffer */
230         body = lustre_msg_buf(req->rq_reqmsg, 0);
231         ldlm_lock2desc(lock, &body->lock_desc);
232         body->lock_flags = *flags;
233
234         memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
235         if (parent_lock_handle)
236                 memcpy(&body->lock_handle2, parent_lock_handle,
237                        sizeof(body->lock_handle2));
238
239         /* Continue as normal. */
240         if (!req_passed_in) {
241                 size = sizeof(*reply);
242                 req->rq_replen = lustre_msg_size(1, &size);
243         }
244         lock->l_connh = connh;
245         lock->l_export = NULL;
246
247         LDLM_DEBUG(lock, "sending request");
248         rc = ptlrpc_queue_wait(req);
249
250         if (rc != ELDLM_OK) {
251                 LASSERT(!is_replay);
252                 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
253                            rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
254                 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
255                 l_lock(&ns->ns_lock);
256                 lock->l_flags |= LDLM_FL_CANCELING;
257                 l_unlock(&ns->ns_lock);
258
259                 ldlm_lock_decref_and_cancel(lockh, mode);
260                 GOTO(out_req, rc);
261         }
262
263         reply = lustre_msg_buf(req->rq_repmsg, 0);
264         memcpy(&lock->l_remote_handle, &reply->lock_handle,
265                sizeof(lock->l_remote_handle));
266         *flags = reply->lock_flags;
267
268         CDEBUG(D_INFO, "local: %p, remote: %p, flags: %d\n", lock,
269                (void *)(unsigned long)reply->lock_handle.addr, *flags);
270         if (type == LDLM_EXTENT) {
271                 CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got "
272                        "extent "LPU64" -> "LPU64"\n",
273                        body->lock_desc.l_extent.start,
274                        body->lock_desc.l_extent.end,
275                        reply->lock_extent.start, reply->lock_extent.end);
276                 cookie = &reply->lock_extent; /* FIXME bug 267 */
277                 cookielen = sizeof(reply->lock_extent);
278         }
279
280         /* If enqueue returned a blocked lock but the completion handler has
281          * already run, then it fixed up the resource and we don't need to do it
282          * again. */
283         if ((*flags) & LDLM_FL_LOCK_CHANGED) {
284                 int newmode = reply->lock_mode;
285                 LASSERT(!is_replay);
286                 if (newmode && newmode != lock->l_req_mode) {
287                         LDLM_DEBUG(lock, "server returned different mode %s",
288                                    ldlm_lockname[newmode]);
289                         lock->l_req_mode = newmode;
290                 }
291
292                 if (reply->lock_resource_name.name[0] !=
293                     lock->l_resource->lr_name.name[0]) {
294                         CDEBUG(D_INFO, "remote intent success, locking %ld "
295                                "instead of %ld\n",
296                                (long)reply->lock_resource_name.name[0],
297                                (long)lock->l_resource->lr_name.name[0]);
298
299                         ldlm_lock_change_resource(ns, lock,
300                                                   reply->lock_resource_name);
301                         if (lock->l_resource == NULL) {
302                                 LBUG();
303                                 GOTO(out_req, rc = -ENOMEM);
304                         }
305                         LDLM_DEBUG(lock, "client-side enqueue, new resource");
306                 }
307         }
308
309         if (!is_replay) {
310                 l_lock(&ns->ns_lock);
311                 lock->l_completion_ast = NULL;
312                 rc = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags,
313                                        completion, blocking);
314                 l_unlock(&ns->ns_lock);
315                 if (lock->l_completion_ast)
316                         lock->l_completion_ast(lock, *flags, NULL);
317         }
318
319         LDLM_DEBUG(lock, "client-side enqueue END");
320         EXIT;
321  out_req:
322         if (!req_passed_in)
323                 ptlrpc_req_finished(req);
324  out:
325         LDLM_LOCK_PUT(lock);
326  out_nolock:
327         return rc;
328 }
329
330 int ldlm_match_or_enqueue(struct lustre_handle *connh,
331                           struct ptlrpc_request *req,
332                           struct ldlm_namespace *ns,
333                           struct lustre_handle *parent_lock_handle,
334                           struct ldlm_res_id res_id,
335                           __u32 type,
336                           void *cookie, int cookielen,
337                           ldlm_mode_t mode,
338                           int *flags,
339                           ldlm_completion_callback completion,
340                           ldlm_blocking_callback blocking,
341                           void *data,
342                           void *cp_data,
343                           struct lustre_handle *lockh)
344 {
345         int rc;
346         ENTRY;
347         if (connh == NULL) {
348                 /* Just to make sure that I understand things --phil */
349                 LASSERT(*flags & LDLM_FL_LOCAL_ONLY);
350         }
351
352         LDLM_DEBUG_NOLOCK("resource "LPU64"/"LPU64, res_id.name[0],
353                           res_id.name[1]);
354         rc = ldlm_lock_match(ns, *flags, &res_id, type, cookie, cookielen, mode,
355                              lockh);
356         if (rc == 0) {
357                 rc = ldlm_cli_enqueue(connh, req, ns, parent_lock_handle,
358                                       res_id, type, cookie, cookielen, mode,
359                                       flags, completion, blocking, data,
360                                       cp_data, lockh);
361                 if (rc != ELDLM_OK)
362                         CERROR("ldlm_cli_enqueue: err: %d\n", rc);
363                 RETURN(rc);
364         }
365         RETURN(0);
366 }
367
368 int ldlm_cli_replay_enqueue(struct ldlm_lock *lock)
369 {
370         struct lustre_handle lockh;
371         struct ldlm_res_id junk;
372         int flags = LDLM_FL_REPLAY;
373         ldlm_lock2handle(lock, &lockh);
374         return ldlm_cli_enqueue(lock->l_connh, NULL, NULL, NULL, junk,
375                                 lock->l_resource->lr_type, NULL, 0, -1, &flags,
376                                 NULL, NULL, NULL, 0, &lockh);
377 }
378
379 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
380                                   int *flags)
381 {
382         ENTRY;
383         if (lock->l_resource->lr_namespace->ns_client) {
384                 CERROR("Trying to cancel local lock\n");
385                 LBUG();
386         }
387         LDLM_DEBUG(lock, "client-side local convert");
388
389         ldlm_lock_convert(lock, new_mode, flags);
390         ldlm_reprocess_all(lock->l_resource);
391
392         LDLM_DEBUG(lock, "client-side local convert handler END");
393         LDLM_LOCK_PUT(lock);
394         RETURN(0);
395 }
396
397 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
398  * conversion of locks which are on the waiting or converting queue */
399 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
400 {
401         struct ldlm_request *body;
402         struct lustre_handle *connh;
403         struct ldlm_reply *reply;
404         struct ldlm_lock *lock;
405         struct ldlm_resource *res;
406         struct ptlrpc_request *req;
407         int rc, size = sizeof(*body);
408         ENTRY;
409
410         lock = ldlm_handle2lock(lockh);
411         if (!lock) {
412                 LBUG();
413                 RETURN(-EINVAL);
414         }
415         *flags = 0;
416         connh = lock->l_connh;
417
418         if (!connh)
419                 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
420
421         LDLM_DEBUG(lock, "client-side convert");
422
423         req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_CONVERT, 1, &size,
424                               NULL);
425         if (!req)
426                 GOTO(out, rc = -ENOMEM);
427
428         body = lustre_msg_buf(req->rq_reqmsg, 0);
429         memcpy(&body->lock_handle1, &lock->l_remote_handle,
430                sizeof(body->lock_handle1));
431
432         body->lock_desc.l_req_mode = new_mode;
433         body->lock_flags = *flags;
434
435         size = sizeof(*reply);
436         req->rq_replen = lustre_msg_size(1, &size);
437
438         rc = ptlrpc_queue_wait(req);
439         if (rc != ELDLM_OK)
440                 GOTO(out, rc);
441
442         reply = lustre_msg_buf(req->rq_repmsg, 0);
443         res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
444         if (res != NULL)
445                 ldlm_reprocess_all(res);
446         /* Go to sleep until the lock is granted. */
447         /* FIXME: or cancelled. */
448         if (lock->l_completion_ast)
449                 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC, NULL);
450         EXIT;
451  out:
452         LDLM_LOCK_PUT(lock);
453         ptlrpc_req_finished(req);
454         return rc;
455 }
456
457 int ldlm_cli_cancel(struct lustre_handle *lockh)
458 {
459         struct ptlrpc_request *req;
460         struct ldlm_lock *lock;
461         struct ldlm_request *body;
462         int rc = 0, size = sizeof(*body);
463         ENTRY;
464
465         /* concurrent cancels on the same handle can happen */
466         lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
467         if (lock == NULL)
468                 RETURN(0);
469
470         if (lock->l_connh) {
471                 int local_only;
472
473                 LDLM_DEBUG(lock, "client-side cancel");
474                 /* Set this flag to prevent others from getting new references*/
475                 l_lock(&lock->l_resource->lr_namespace->ns_lock);
476                 lock->l_flags |= LDLM_FL_CBPENDING;
477                 ldlm_cancel_callback(lock);
478                 local_only = (lock->l_flags & LDLM_FL_LOCAL_ONLY);
479                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
480
481                 if (local_only) {
482                         CDEBUG(D_INFO, "not sending request (at caller's "
483                                "instruction\n");
484                         goto local_cancel;
485                 }
486
487                 req = ptlrpc_prep_req(class_conn2cliimp(lock->l_connh),
488                                       LDLM_CANCEL, 1, &size, NULL);
489                 if (!req)
490                         GOTO(out, rc = -ENOMEM);
491
492                 /* XXX FIXME bug 249 */
493                 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
494                 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
495
496                 body = lustre_msg_buf(req->rq_reqmsg, 0);
497                 memcpy(&body->lock_handle1, &lock->l_remote_handle,
498                        sizeof(body->lock_handle1));
499
500                 req->rq_replen = lustre_msg_size(0, NULL);
501
502                 rc = ptlrpc_queue_wait(req);
503                 ptlrpc_req_finished(req);
504                 if (rc == ESTALE) {
505                         CERROR("client/server out of sync\n");
506                         LBUG();
507                 }
508                 if (rc != ELDLM_OK)
509                         CERROR("Got rc %d from cancel RPC: canceling "
510                                "anyway\n", rc);
511         local_cancel:
512                 ldlm_lock_cancel(lock);
513         } else {
514                 LDLM_DEBUG(lock, "client-side local cancel");
515                 if (lock->l_resource->lr_namespace->ns_client) {
516                         CERROR("Trying to cancel local lock\n");
517                         LBUG();
518                 }
519                 ldlm_lock_cancel(lock);
520                 ldlm_reprocess_all(lock->l_resource);
521                 LDLM_DEBUG(lock, "client-side local cancel handler END");
522         }
523
524         EXIT;
525  out:
526         LDLM_LOCK_PUT(lock);
527         return rc;
528 }
529
530 int ldlm_cancel_lru(struct ldlm_namespace *ns)
531 {
532         struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
533         int count, rc = 0;
534         struct ldlm_ast_work *w;
535         ENTRY;
536
537         l_lock(&ns->ns_lock);
538         count = ns->ns_nr_unused - ns->ns_max_unused;
539
540         if (count <= 0) {
541                 l_unlock(&ns->ns_lock);
542                 RETURN(0);
543         }
544
545         list_for_each_safe(tmp, next, &ns->ns_unused_list) {
546                 struct ldlm_lock *lock;
547                 lock = list_entry(tmp, struct ldlm_lock, l_lru);
548
549                 LASSERT(!lock->l_readers && !lock->l_writers);
550
551                 /* Setting the CBPENDING flag is a little misleading, but
552                  * prevents an important race; namely, once CBPENDING is set,
553                  * the lock can accumulate no more readers/writers.  Since
554                  * readers and writers are already zero here, ldlm_lock_decref
555                  * won't see this flag and call l_blocking_ast */
556                 lock->l_flags |= LDLM_FL_CBPENDING;
557
558                 OBD_ALLOC(w, sizeof(*w));
559                 LASSERT(w);
560
561                 w->w_lock = LDLM_LOCK_GET(lock);
562                 list_add(&w->w_list, &list);
563                 ldlm_lock_remove_from_lru(lock);
564
565                 if (--count == 0)
566                         break;
567         }
568         l_unlock(&ns->ns_lock);
569
570         list_for_each_safe(tmp, next, &list) {
571                 struct lustre_handle lockh;
572                 int rc;
573                 w = list_entry(tmp, struct ldlm_ast_work, w_list);
574
575                 ldlm_lock2handle(w->w_lock, &lockh);
576                 rc = ldlm_cli_cancel(&lockh);
577                 if (rc != ELDLM_OK)
578                         CDEBUG(D_INFO, "ldlm_cli_cancel: %d\n", rc);
579
580                 list_del(&w->w_list);
581                 LDLM_LOCK_PUT(w->w_lock);
582                 OBD_FREE(w, sizeof(*w));
583         }
584
585         RETURN(rc);
586 }
587
588 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
589                                     struct ldlm_res_id res_id, int flags)
590 {
591         struct ldlm_resource *res;
592         struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
593         struct ldlm_ast_work *w;
594         ENTRY;
595
596         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
597         if (res == NULL) {
598                 /* This is not a problem. */
599                 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
600                 RETURN(0);
601         }
602
603         l_lock(&ns->ns_lock);
604         list_for_each(tmp, &res->lr_granted) {
605                 struct ldlm_lock *lock;
606                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
607
608                 if (lock->l_readers || lock->l_writers)
609                         continue;
610
611                 /* See CBPENDING comment in ldlm_cancel_lru */
612                 lock->l_flags |= LDLM_FL_CBPENDING;
613
614                 OBD_ALLOC(w, sizeof(*w));
615                 LASSERT(w);
616
617                 w->w_lock = LDLM_LOCK_GET(lock);
618
619                 /* Prevent the cancel callback from being called by setting
620                  * LDLM_FL_CANCEL in the lock.  Very sneaky. -p */
621                 if (flags & LDLM_FL_NO_CALLBACK)
622                         w->w_lock->l_flags |= LDLM_FL_CANCEL;
623
624                 list_add(&w->w_list, &list);
625         }
626         l_unlock(&ns->ns_lock);
627
628         list_for_each_safe(tmp, next, &list) {
629                 struct lustre_handle lockh;
630                 int rc;
631                 w = list_entry(tmp, struct ldlm_ast_work, w_list);
632
633                 if (flags & LDLM_FL_LOCAL_ONLY) {
634                         ldlm_lock_cancel(w->w_lock);
635                 } else {
636                         ldlm_lock2handle(w->w_lock, &lockh);
637                         rc = ldlm_cli_cancel(&lockh);
638                         if (rc != ELDLM_OK)
639                                 CERROR("ldlm_cli_cancel: %d\n", rc);
640                 }
641                 list_del(&w->w_list);
642                 LDLM_LOCK_PUT(w->w_lock);
643                 OBD_FREE(w, sizeof(*w));
644         }
645
646         ldlm_resource_putref(res);
647
648         RETURN(0);
649 }
650
651 /* Cancel all locks on a namespace (or a specific resource, if given)
652  * that have 0 readers/writers.
653  *
654  * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
655  * to notify the server.
656  * If flags & LDLM_FL_NO_CALLBACK, don't run the cancel callback. */
657 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
658                            struct ldlm_res_id *res_id, int flags)
659 {
660         int i;
661         ENTRY;
662
663         if (ns == NULL)
664                 RETURN(ELDLM_OK);
665
666         if (res_id)
667                 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags));
668
669         l_lock(&ns->ns_lock);
670         for (i = 0; i < RES_HASH_SIZE; i++) {
671                 struct list_head *tmp, *pos;
672                 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
673                         int rc;
674                         struct ldlm_resource *res;
675                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
676                         ldlm_resource_getref(res);
677
678                         rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
679                                                              flags);
680
681                         if (rc)
682                                 CERROR("cancel_unused_res ("LPU64"): %d\n",
683                                        res->lr_name.name[0], rc);
684                         ldlm_resource_putref(res);
685                 }
686         }
687         l_unlock(&ns->ns_lock);
688
689         RETURN(ELDLM_OK);
690 }
691
692 /* Lock iterators. */
693
694 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
695                           void *closure)
696 {
697         struct list_head *tmp, *next;
698         struct ldlm_lock *lock;
699         int rc = LDLM_ITER_CONTINUE;
700         struct ldlm_namespace *ns = res->lr_namespace;
701
702         ENTRY;
703
704         if (!res)
705                 RETURN(LDLM_ITER_CONTINUE);
706
707         l_lock(&ns->ns_lock);
708         list_for_each_safe(tmp, next, &res->lr_granted) {
709                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
710
711                 if (iter(lock, closure) == LDLM_ITER_STOP)
712                         GOTO(out, rc = LDLM_ITER_STOP);
713         }
714
715         list_for_each_safe(tmp, next, &res->lr_converting) {
716                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
717
718                 if (iter(lock, closure) == LDLM_ITER_STOP)
719                         GOTO(out, rc = LDLM_ITER_STOP);
720         }
721
722         list_for_each_safe(tmp, next, &res->lr_waiting) {
723                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
724
725                 if (iter(lock, closure) == LDLM_ITER_STOP)
726                         GOTO(out, rc = LDLM_ITER_STOP);
727         }
728  out:
729         l_unlock(&ns->ns_lock);
730         RETURN(rc);
731 }
732
733 struct iter_helper_data {
734         ldlm_iterator_t iter;
735         void *closure;
736 };
737
738 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
739 {
740         struct iter_helper_data *helper = closure;
741         return helper->iter(lock, helper->closure);
742 }
743
744 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
745 {
746         return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
747 }
748
749 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
750                            void *closure)
751 {
752         struct iter_helper_data helper = { iter: iter, closure: closure };
753         return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
754 }
755
756 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
757                                ldlm_res_iterator_t iter, void *closure)
758 {
759         int i, rc = LDLM_ITER_CONTINUE;
760         
761         l_lock(&ns->ns_lock);
762         for (i = 0; i < RES_HASH_SIZE; i++) {
763                 struct list_head *tmp, *next;
764                 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
765                         struct ldlm_resource *res = 
766                                 list_entry(tmp, struct ldlm_resource, lr_hash);
767
768                         ldlm_resource_getref(res);
769                         rc = iter(res, closure);
770                         ldlm_resource_putref(res);
771                         if (rc == LDLM_ITER_STOP)
772                                 GOTO(out, rc);
773                 }
774         }
775  out:
776         l_unlock(&ns->ns_lock);
777         RETURN(rc);
778 }
779
780 /* Lock replay */
781
782 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
783 {
784         struct list_head *list = closure;
785
786         /* we use l_pending_chain here, because it's unused on clients. */
787         list_add(&lock->l_pending_chain, list);
788         return LDLM_ITER_CONTINUE;
789 }
790
791 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
792 {
793         struct ptlrpc_request *req;
794         struct ldlm_request *body;
795         struct ldlm_reply *reply;
796         int rc, size;
797         int flags;
798
799         /*
800          * If granted mode matches the requested mode, this lock is granted.
801          *
802          * If they differ, but we have a granted mode, then we were granted
803          * one mode and now want another: ergo, converting.
804          *
805          * If we haven't been granted anything and are on a resource list,
806          * then we're blocked/waiting.
807          *
808          * If we haven't been granted anything and we're NOT on a resource list,
809          * then we haven't got a reply yet and don't have a known disposition.
810          * This happens whenever a lock enqueue is the request that triggers
811          * recovery.
812          */
813         if (lock->l_granted_mode == lock->l_req_mode)
814                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
815         else if (lock->l_granted_mode)
816                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
817         else if (!list_empty(&lock->l_res_link))
818                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
819         else
820                 flags = LDLM_FL_REPLAY;
821                 
822         size = sizeof(*body);
823         req = ptlrpc_prep_req(imp, LDLM_ENQUEUE, 1, &size, NULL);
824         if (!req)
825                 RETURN(-ENOMEM);
826
827         /* We're part of recovery, so don't wait for it. */
828         req->rq_level = LUSTRE_CONN_RECOVD;
829         
830         body = lustre_msg_buf(req->rq_reqmsg, 0);
831         ldlm_lock2desc(lock, &body->lock_desc);
832         body->lock_flags = flags;
833
834         ldlm_lock2handle(lock, &body->lock_handle1);
835         size = sizeof(*reply);
836         req->rq_replen = lustre_msg_size(1, &size);
837
838         LDLM_DEBUG(lock, "replaying lock:");
839         rc = ptlrpc_queue_wait(req);
840         if (rc != ELDLM_OK)
841                 GOTO(out, rc);
842
843         reply = lustre_msg_buf(req->rq_repmsg, 0);
844         memcpy(&lock->l_remote_handle, &reply->lock_handle,
845                sizeof(lock->l_remote_handle));
846         LDLM_DEBUG(lock, "replayed lock:");
847  out:
848         ptlrpc_req_finished(req);
849         RETURN(rc);
850 }
851
852 int ldlm_replay_locks(struct obd_import *imp)
853 {
854         struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
855         struct list_head list, *pos, *next;
856         struct ldlm_lock *lock;
857         int rc = 0;
858         
859         ENTRY;
860         INIT_LIST_HEAD(&list);
861
862         l_lock(&ns->ns_lock);
863         (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
864
865         list_for_each_safe(pos, next, &list) {
866                 lock = list_entry(pos, struct ldlm_lock, l_pending_chain);
867                 rc = replay_one_lock(imp, lock);
868                 if (rc)
869                         break; /* or try to do the rest? */
870         }
871         l_unlock(&ns->ns_lock);
872         RETURN(rc);
873 }