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