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