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