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