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