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