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