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