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