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