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