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