Whamcloud - gitweb
56c48ff35a6ca90f4cd1d2f0a5f9bded4dd0c265
[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 the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #define DEBUG_SUBSYSTEM S_LDLM
26 #ifndef __KERNEL__
27 #include <signal.h>
28 #include <liblustre.h>
29 #endif
30
31 #include <lustre_dlm.h>
32 #include <obd_class.h>
33 #include <obd.h>
34
35 #include "ldlm_internal.h"
36
37 static void interrupted_completion_wait(void *data)
38 {
39 }
40
41 struct lock_wait_data {
42         struct ldlm_lock *lwd_lock;
43         __u32             lwd_conn_cnt;
44 };
45
46 struct ldlm_async_args {
47         struct lustre_handle lock_handle;
48 };
49
50 int ldlm_expired_completion_wait(void *data)
51 {
52         struct lock_wait_data *lwd = data;
53         struct ldlm_lock *lock = lwd->lwd_lock;
54         struct obd_import *imp;
55         struct obd_device *obd;
56
57         ENTRY;
58         if (lock->l_conn_export == NULL) {
59                 static cfs_time_t next_dump = 0, last_dump = 0;
60
61                 if (ptlrpc_check_suspend())
62                         RETURN(0);
63
64                 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
65                            CFS_DURATION_T"s ago); not entering recovery in "
66                            "server code, just going back to sleep",
67                            lock->l_enqueued_time.tv_sec,
68                            cfs_time_current_sec() - lock->l_enqueued_time.tv_sec);
69                 if (cfs_time_after(cfs_time_current(), next_dump)) {
70                         last_dump = next_dump;
71                         next_dump = cfs_time_shift(300);
72                         ldlm_namespace_dump(D_DLMTRACE,
73                                             lock->l_resource->lr_namespace);
74                         if (last_dump == 0)
75                                 libcfs_debug_dumplog();
76                 }
77                 RETURN(0);
78         }
79
80         obd = lock->l_conn_export->exp_obd;
81         imp = obd->u.cli.cl_import;
82         ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
83         LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
84                   CFS_DURATION_T"s ago), entering recovery for %s@%s",
85                   lock->l_enqueued_time.tv_sec,
86                   cfs_time_current_sec() - lock->l_enqueued_time.tv_sec,
87                   obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
88
89         RETURN(0);
90 }
91
92 static int is_granted_or_cancelled(struct ldlm_lock *lock)
93 {
94         int ret = 0;
95
96         lock_res_and_lock(lock);
97         if (((lock->l_req_mode == lock->l_granted_mode) &&
98              !(lock->l_flags & LDLM_FL_CP_REQD)) ||
99             (lock->l_flags & LDLM_FL_FAILED))
100                 ret = 1;
101         unlock_res_and_lock(lock);
102
103         return ret;
104 }
105
106 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
107 {
108         /* XXX ALLOCATE - 160 bytes */
109         struct lock_wait_data lwd;
110         struct obd_device *obd;
111         struct obd_import *imp = NULL;
112         struct l_wait_info lwi;
113         int rc = 0;
114         ENTRY;
115
116         if (flags == LDLM_FL_WAIT_NOREPROC) {
117                 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
118                 goto noreproc;
119         }
120
121         if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
122                        LDLM_FL_BLOCK_CONV))) {
123                 cfs_waitq_signal(&lock->l_waitq);
124                 RETURN(0);
125         }
126
127         LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
128                    "sleeping");
129         ldlm_lock_dump(D_OTHER, lock, 0);
130         ldlm_reprocess_all(lock->l_resource);
131
132 noreproc:
133
134         obd = class_exp2obd(lock->l_conn_export);
135
136         /* if this is a local lock, then there is no import */
137         if (obd != NULL)
138                 imp = obd->u.cli.cl_import;
139
140         lwd.lwd_lock = lock;
141
142         if (lock->l_flags & LDLM_FL_NO_TIMEOUT) {
143                 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
144                 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
145         } else {
146                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
147                                        ldlm_expired_completion_wait,
148                                        interrupted_completion_wait, &lwd);
149         }
150
151         if (imp != NULL) {
152                 spin_lock(&imp->imp_lock);
153                 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
154                 spin_unlock(&imp->imp_lock);
155         }
156
157         /* Go to sleep until the lock is granted or cancelled. */
158         rc = l_wait_event(lock->l_waitq, is_granted_or_cancelled(lock), &lwi);
159
160         if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
161                 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
162                 RETURN(-EIO);
163         }
164
165         if (rc) {
166                 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
167                            rc);
168                 RETURN(rc);
169         }
170
171         LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
172         RETURN(0);
173 }
174
175 /*
176  * ->l_blocking_ast() callback for LDLM locks acquired by server-side OBDs.
177  */
178 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
179                       void *data, int flag)
180 {
181         int do_ast;
182         ENTRY;
183
184         if (flag == LDLM_CB_CANCELING) {
185                 /* Don't need to do anything here. */
186                 RETURN(0);
187         }
188
189         lock_res_and_lock(lock);
190         /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
191          * that ldlm_blocking_ast is called just before intent_policy method
192          * takes the ns_lock, then by the time we get the lock, we might not
193          * be the correct blocking function anymore.  So check, and return
194          * early, if so. */
195         if (lock->l_blocking_ast != ldlm_blocking_ast) {
196                 unlock_res_and_lock(lock);
197                 RETURN(0);
198         }
199
200         lock->l_flags |= LDLM_FL_CBPENDING;
201         do_ast = (!lock->l_readers && !lock->l_writers);
202         unlock_res_and_lock(lock);
203
204         if (do_ast) {
205                 struct lustre_handle lockh;
206                 int rc;
207
208                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
209                 ldlm_lock2handle(lock, &lockh);
210                 rc = ldlm_cli_cancel(&lockh);
211                 if (rc < 0)
212                         CERROR("ldlm_cli_cancel: %d\n", rc);
213         } else {
214                 LDLM_DEBUG(lock, "Lock still has references, will be "
215                            "cancelled later");
216         }
217         RETURN(0);
218 }
219
220 /*
221  * ->l_glimpse_ast() for DLM extent locks acquired on the server-side. See
222  * comment in filter_intent_policy() on why you may need this.
223  */
224 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
225 {
226         /*
227          * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for
228          * that is rather subtle: with OST-side locking, it may so happen that
229          * _all_ extent locks are held by the OST. If client wants to obtain
230          * current file size it calls ll{,u}_glimpse_size(), and (as locks are
231          * on the server), dummy glimpse callback fires and does
232          * nothing. Client still receives correct file size due to the
233          * following fragment in filter_intent_policy():
234          *
235          * rc = l->l_glimpse_ast(l, NULL); // this will update the LVB
236          * if (rc != 0 && res->lr_namespace->ns_lvbo &&
237          *     res->lr_namespace->ns_lvbo->lvbo_update) {
238          *         res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
239          * }
240          *
241          * that is, after glimpse_ast() fails, filter_lvbo_update() runs, and
242          * returns correct file size to the client.
243          */
244         return -ELDLM_NO_LOCK_DATA;
245 }
246
247 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
248                            const struct ldlm_res_id *res_id,
249                            ldlm_type_t type, ldlm_policy_data_t *policy,
250                            ldlm_mode_t mode, int *flags,
251                            ldlm_blocking_callback blocking,
252                            ldlm_completion_callback completion,
253                            ldlm_glimpse_callback glimpse,
254                            void *data, __u32 lvb_len, void *lvb_swabber,
255                            struct lustre_handle *lockh)
256 {
257         struct ldlm_lock *lock;
258         int err;
259         ENTRY;
260
261         LASSERT(!(*flags & LDLM_FL_REPLAY));
262         if (unlikely(ns_is_client(ns))) {
263                 CERROR("Trying to enqueue local lock in a shadow namespace\n");
264                 LBUG();
265         }
266
267         lock = ldlm_lock_create(ns, res_id, type, mode, blocking,
268                                 completion, glimpse, data, lvb_len);
269         if (unlikely(!lock))
270                 GOTO(out_nolock, err = -ENOMEM);
271         LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
272
273         ldlm_lock_addref_internal(lock, mode);
274         ldlm_lock2handle(lock, lockh);
275         lock_res_and_lock(lock);
276         lock->l_flags |= LDLM_FL_LOCAL;
277         if (*flags & LDLM_FL_ATOMIC_CB)
278                 lock->l_flags |= LDLM_FL_ATOMIC_CB;
279         lock->l_lvb_swabber = lvb_swabber;
280         unlock_res_and_lock(lock);
281         if (policy != NULL)
282                 lock->l_policy_data = *policy;
283         if (type == LDLM_EXTENT)
284                 lock->l_req_extent = policy->l_extent;
285
286         err = ldlm_lock_enqueue(ns, &lock, policy, flags);
287         if (unlikely(err != ELDLM_OK))
288                 GOTO(out, err);
289
290         if (policy != NULL)
291                 *policy = lock->l_policy_data;
292
293         LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
294                           lock);
295
296         if (lock->l_completion_ast)
297                 lock->l_completion_ast(lock, *flags, NULL);
298
299         LDLM_DEBUG(lock, "client-side local enqueue END");
300         EXIT;
301  out:
302         LDLM_LOCK_PUT(lock);
303  out_nolock:
304         return err;
305 }
306
307 static void failed_lock_cleanup(struct ldlm_namespace *ns,
308                                 struct ldlm_lock *lock,
309                                 struct lustre_handle *lockh, int mode)
310 {
311         /* Set a flag to prevent us from sending a CANCEL (bug 407) */
312         lock_res_and_lock(lock);
313         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
314         unlock_res_and_lock(lock);
315         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
316
317         ldlm_lock_decref_and_cancel(lockh, mode);
318
319         /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
320          *       from llite/file.c/ll_file_flock(). */
321         if (lock->l_resource->lr_type == LDLM_FLOCK) {
322                 ldlm_lock_destroy(lock);
323         }
324 }
325
326 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
327                           ldlm_type_t type, __u8 with_policy, ldlm_mode_t mode,
328                           int *flags, void *lvb, __u32 lvb_len,
329                           void *lvb_swabber, struct lustre_handle *lockh,int rc)
330 {
331         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
332         int is_replay = *flags & LDLM_FL_REPLAY;
333         struct ldlm_lock *lock;
334         struct ldlm_reply *reply;
335         int cleanup_phase = 1;
336         ENTRY;
337
338         lock = ldlm_handle2lock(lockh);
339         /* ldlm_cli_enqueue is holding a reference on this lock. */
340         if (!lock) {
341                 LASSERT(type == LDLM_FLOCK);
342                 RETURN(-ENOLCK);
343         }
344
345         if (rc != ELDLM_OK) {
346                 LASSERT(!is_replay);
347                 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
348                            rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
349                 if (rc == ELDLM_LOCK_ABORTED) {
350                         /* Before we return, swab the reply */
351                         reply = req_capsule_server_get(&req->rq_pill,
352                                                        &RMF_DLM_REP);
353                         if (reply == NULL)
354                                 rc = -EPROTO;
355                         if (lvb_len) {
356                                 struct ost_lvb *tmplvb;
357
358                                 req_capsule_set_size(&req->rq_pill,
359                                                      &RMF_DLM_LVB, RCL_SERVER,
360                                                      lvb_len);
361                             tmplvb = req_capsule_server_swab_get(&req->rq_pill,
362                                                                  &RMF_DLM_LVB,
363                                                                  lvb_swabber);
364                                 if (tmplvb == NULL)
365                                         GOTO(cleanup, rc = -EPROTO);
366                                 if (lvb != NULL)
367                                         memcpy(lvb, tmplvb, lvb_len);
368                         }
369                 }
370                 GOTO(cleanup, rc);
371         }
372
373         reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
374         if (reply == NULL)
375                 GOTO(cleanup, rc = -EPROTO);
376
377         /* lock enqueued on the server */
378         cleanup_phase = 0;
379
380         lock_res_and_lock(lock);
381         lock->l_remote_handle = reply->lock_handle;
382         *flags = reply->lock_flags;
383         lock->l_flags |= reply->lock_flags & LDLM_INHERIT_FLAGS;
384         /* move NO_TIMEOUT flag to the lock to force ldlm_lock_match()
385          * to wait with no timeout as well */
386         lock->l_flags |= reply->lock_flags & LDLM_FL_NO_TIMEOUT;
387         unlock_res_and_lock(lock);
388
389         CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
390                lock, reply->lock_handle.cookie, *flags);
391
392         /* If enqueue returned a blocked lock but the completion handler has
393          * already run, then it fixed up the resource and we don't need to do it
394          * again. */
395         if ((*flags) & LDLM_FL_LOCK_CHANGED) {
396                 int newmode = reply->lock_desc.l_req_mode;
397                 LASSERT(!is_replay);
398                 if (newmode && newmode != lock->l_req_mode) {
399                         LDLM_DEBUG(lock, "server returned different mode %s",
400                                    ldlm_lockname[newmode]);
401                         lock->l_req_mode = newmode;
402                 }
403
404                 if (memcmp(reply->lock_desc.l_resource.lr_name.name,
405                           lock->l_resource->lr_name.name,
406                           sizeof(struct ldlm_res_id))) {
407                         CDEBUG(D_INFO, "remote intent success, locking "
408                                         "(%ld,%ld,%ld) instead of "
409                                         "(%ld,%ld,%ld)\n",
410                               (long)reply->lock_desc.l_resource.lr_name.name[0],
411                               (long)reply->lock_desc.l_resource.lr_name.name[1],
412                               (long)reply->lock_desc.l_resource.lr_name.name[2],
413                               (long)lock->l_resource->lr_name.name[0],
414                               (long)lock->l_resource->lr_name.name[1],
415                               (long)lock->l_resource->lr_name.name[2]);
416
417                         rc = ldlm_lock_change_resource(ns, lock,
418                                         &reply->lock_desc.l_resource.lr_name);
419                         if (rc || lock->l_resource == NULL)
420                                 GOTO(cleanup, rc = -ENOMEM);
421                         LDLM_DEBUG(lock, "client-side enqueue, new resource");
422                 }
423                 if (with_policy)
424                         if (!(type == LDLM_IBITS && !(exp->exp_connect_flags &
425                                                     OBD_CONNECT_IBITS)))
426                                 lock->l_policy_data =
427                                                  reply->lock_desc.l_policy_data;
428                 if (type != LDLM_PLAIN)
429                         LDLM_DEBUG(lock,"client-side enqueue, new policy data");
430         }
431
432         if ((*flags) & LDLM_FL_AST_SENT ||
433             /* Cancel extent locks as soon as possible on a liblustre client,
434              * because it cannot handle asynchronous ASTs robustly (see
435              * bug 7311). */
436             (LIBLUSTRE_CLIENT && type == LDLM_EXTENT)) {
437                 lock_res_and_lock(lock);
438                 lock->l_flags |= LDLM_FL_CBPENDING |  LDLM_FL_BL_AST;
439                 unlock_res_and_lock(lock);
440                 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
441         }
442
443         /* If the lock has already been granted by a completion AST, don't
444          * clobber the LVB with an older one. */
445         if (lvb_len && (lock->l_req_mode != lock->l_granted_mode)) {
446                 void *tmplvb;
447
448                 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
449                                      lvb_len);
450                 tmplvb = req_capsule_server_swab_get(&req->rq_pill,
451                                                      &RMF_DLM_LVB,
452                                                      lvb_swabber);
453                 if (tmplvb == NULL)
454                         GOTO(cleanup, rc = -EPROTO);
455                 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
456         }
457
458         if (!is_replay) {
459                 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
460                 if (lock->l_completion_ast != NULL) {
461                         int err = lock->l_completion_ast(lock, *flags, NULL);
462                         if (!rc)
463                                 rc = err;
464                         if (rc && type != LDLM_FLOCK) /* bug 9425, bug 10250 */
465                                 cleanup_phase = 1;
466                 }
467         }
468
469         if (lvb_len && lvb != NULL) {
470                 /* Copy the LVB here, and not earlier, because the completion
471                  * AST (if any) can override what we got in the reply */
472                 memcpy(lvb, lock->l_lvb_data, lvb_len);
473         }
474
475         LDLM_DEBUG(lock, "client-side enqueue END");
476         EXIT;
477 cleanup:
478         if (cleanup_phase == 1 && rc)
479                 failed_lock_cleanup(ns, lock, lockh, mode);
480         /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
481         LDLM_LOCK_PUT(lock);
482         LDLM_LOCK_PUT(lock);
483         return rc;
484 }
485
486 /* PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
487  * a single page on the send/receive side. XXX: 512 should be changed
488  * to more adequate value. */
489 static inline int ldlm_req_handles_avail(int req_size, int off)
490 {
491         int avail;
492
493         avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512) - req_size;
494         avail /= sizeof(struct lustre_handle);
495         avail += LDLM_LOCKREQ_HANDLES - off;
496
497         return avail;
498 }
499
500 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
501                                              enum req_location loc,
502                                              int off)
503 {
504         int size = req_capsule_msg_size(pill, loc);
505         return ldlm_req_handles_avail(size, off);
506 }
507
508 static inline int ldlm_format_handles_avail(struct obd_import *imp,
509                                             const struct req_format *fmt,
510                                             enum req_location loc, int off)
511 {
512         int size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
513         return ldlm_req_handles_avail(size, off);
514 }
515
516 /* Cancel lru locks and pack them into the enqueue request. Pack there the given
517  * @count locks in @cancels. */
518 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
519                       int version, int opc, int canceloff,
520                       struct list_head *cancels, int count)
521 {
522         struct ldlm_namespace   *ns = exp->exp_obd->obd_namespace;
523         struct req_capsule      *pill = &req->rq_pill;
524         struct ldlm_request     *dlm = NULL;
525         int flags, avail, to_free, bufcount, pack = 0;
526         CFS_LIST_HEAD(head);
527         int rc;
528         ENTRY;
529
530         if (cancels == NULL)
531                 cancels = &head;
532         if (exp_connect_cancelset(exp)) {
533                 /* Estimate the amount of available space in the request. */
534                 bufcount = req_capsule_filled_sizes(pill, RCL_CLIENT);
535                 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
536
537                 flags = ns_connect_lru_resize(ns) ? 
538                         LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
539                 to_free = !ns_connect_lru_resize(ns) &&
540                           opc == LDLM_ENQUEUE ? 1 : 0;
541
542                 /* Cancel lru locks here _only_ if the server supports 
543                  * EARLY_CANCEL. Otherwise we have to send extra CANCEL
544                  * rpc, what will make us slower. */
545                 if (avail > count)
546                         count += ldlm_cancel_lru_local(ns, cancels, to_free,
547                                                        avail - count, 0, flags);
548                 if (avail > count)
549                         pack = count;
550                 else
551                         pack = avail;
552                 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
553                                      ldlm_request_bufsize(pack, opc));
554         }
555
556         rc = ptlrpc_request_pack(req, version, opc);
557         if (rc) {
558                 ldlm_lock_list_put(cancels, l_bl_ast, count);
559                 RETURN(rc);
560         }
561
562         if (exp_connect_cancelset(exp)) {
563                 if (canceloff) {
564                         dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
565                         LASSERT(dlm);
566                         /* Skip first lock handler in ldlm_request_pack(),
567                          * this method will incrment @lock_count according
568                          * to the lock handle amount actually written to
569                          * the buffer. */
570                         dlm->lock_count = canceloff;
571                 }
572                 /* Pack into the request @pack lock handles. */
573                 ldlm_cli_cancel_list(cancels, pack, req, 0);
574                 /* Prepare and send separate cancel rpc for others. */
575                 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
576         } else {
577                 ldlm_lock_list_put(cancels, l_bl_ast, count);
578         }
579         RETURN(0);
580 }
581
582 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
583                           struct list_head *cancels, int count)
584 {
585         return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
586                                  LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
587 }
588
589 /* If a request has some specific initialisation it is passed in @reqp,
590  * otherwise it is created in ldlm_cli_enqueue.
591  *
592  * Supports sync and async requests, pass @async flag accordingly. If a
593  * request was created in ldlm_cli_enqueue and it is the async request,
594  * pass it to the caller in @reqp. */
595 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
596                      struct ldlm_enqueue_info *einfo,
597                      const struct ldlm_res_id *res_id,
598                      ldlm_policy_data_t *policy, int *flags,
599                      void *lvb, __u32 lvb_len, void *lvb_swabber,
600                      struct lustre_handle *lockh, int async)
601 {
602         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
603         struct ldlm_lock      *lock;
604         struct ldlm_request   *body;
605         int                    is_replay = *flags & LDLM_FL_REPLAY;
606         int                    req_passed_in = 1;
607         int                    rc, err;
608         struct ptlrpc_request *req;
609         ENTRY;
610
611         LASSERT(exp != NULL);
612
613         /* If we're replaying this lock, just check some invariants.
614          * If we're creating a new lock, get everything all setup nice. */
615         if (is_replay) {
616                 lock = ldlm_handle2lock(lockh);
617                 LASSERT(lock != NULL);
618                 LDLM_DEBUG(lock, "client-side enqueue START");
619                 LASSERT(exp == lock->l_conn_export);
620         } else {
621                 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
622                                         einfo->ei_mode, einfo->ei_cb_bl,
623                                         einfo->ei_cb_cp, einfo->ei_cb_gl,
624                                         einfo->ei_cbdata, lvb_len);
625                 if (lock == NULL)
626                         RETURN(-ENOMEM);
627                 /* for the local lock, add the reference */
628                 ldlm_lock_addref_internal(lock, einfo->ei_mode);
629                 ldlm_lock2handle(lock, lockh);
630                 lock->l_lvb_swabber = lvb_swabber;
631                 if (policy != NULL) {
632                         /* INODEBITS_INTEROP: If the server does not support
633                          * inodebits, we will request a plain lock in the
634                          * descriptor (ldlm_lock2desc() below) but use an
635                          * inodebits lock internally with both bits set.
636                          */
637                         if (einfo->ei_type == LDLM_IBITS &&
638                             !(exp->exp_connect_flags & OBD_CONNECT_IBITS))
639                                 lock->l_policy_data.l_inodebits.bits =
640                                         MDS_INODELOCK_LOOKUP |
641                                         MDS_INODELOCK_UPDATE;
642                         else
643                                 lock->l_policy_data = *policy;
644                 }
645
646                 if (einfo->ei_type == LDLM_EXTENT)
647                         lock->l_req_extent = policy->l_extent;
648                 LDLM_DEBUG(lock, "client-side enqueue START");
649         }
650
651         /* lock not sent to server yet */
652
653         if (reqp == NULL || *reqp == NULL) {
654                 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
655                                                 &RQF_LDLM_ENQUEUE,
656                                                 LUSTRE_DLM_VERSION,
657                                                 LDLM_ENQUEUE);
658                 if (req == NULL) {
659                         failed_lock_cleanup(ns, lock, lockh, einfo->ei_mode);
660                         LDLM_LOCK_PUT(lock);
661                         RETURN(-ENOMEM);
662                 }
663                 req_passed_in = 0;
664                 if (reqp)
665                         *reqp = req;
666         } else {
667                 int len;
668
669                 req = *reqp;
670                 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
671                                            RCL_CLIENT);
672                 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
673                          DLM_LOCKREQ_OFF, len, sizeof(*body));
674         }
675
676         lock->l_conn_export = exp;
677         lock->l_export = NULL;
678         lock->l_blocking_ast = einfo->ei_cb_bl;
679
680         /* Dump lock data into the request buffer */
681         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
682         ldlm_lock2desc(lock, &body->lock_desc);
683         body->lock_flags = *flags;
684         body->lock_handle[0] = *lockh;
685
686         /* Continue as normal. */
687         if (!req_passed_in) {
688                 if (lvb_len > 0) {
689                         req_capsule_extend(&req->rq_pill,
690                                            &RQF_LDLM_ENQUEUE_LVB);
691                         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
692                                              RCL_SERVER, lvb_len);
693                 }
694                 ptlrpc_request_set_replen(req);
695         }
696
697         /*
698          * Liblustre client doesn't get extent locks, except for O_APPEND case
699          * where [0, OBD_OBJECT_EOF] lock is taken, or truncate, where
700          * [i_size, OBD_OBJECT_EOF] lock is taken.
701          */
702         LASSERT(ergo(LIBLUSTRE_CLIENT, einfo->ei_type != LDLM_EXTENT ||
703                      policy->l_extent.end == OBD_OBJECT_EOF));
704
705         if (async) {
706                 LASSERT(reqp != NULL);
707                 RETURN(0);
708         }
709
710         LDLM_DEBUG(lock, "sending request");
711         rc = ptlrpc_queue_wait(req);
712         err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
713                                     einfo->ei_mode, flags, lvb, lvb_len,
714                                     lvb_swabber, lockh, rc);
715
716         /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
717          * one reference that we took */
718         if (err == -ENOLCK)
719                 LDLM_LOCK_PUT(lock);
720         else
721                 rc = err;
722
723         if (!req_passed_in && req != NULL) {
724                 ptlrpc_req_finished(req);
725                 if (reqp)
726                         *reqp = NULL;
727         }
728
729         RETURN(rc);
730 }
731
732 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
733                                   __u32 *flags)
734 {
735         struct ldlm_resource *res;
736         int rc;
737         ENTRY;
738         if (ns_is_client(lock->l_resource->lr_namespace)) {
739                 CERROR("Trying to cancel local lock\n");
740                 LBUG();
741         }
742         LDLM_DEBUG(lock, "client-side local convert");
743
744         res = ldlm_lock_convert(lock, new_mode, flags);
745         if (res) {
746                 ldlm_reprocess_all(res);
747                 rc = 0;
748         } else {
749                 rc = EDEADLOCK;
750         }
751         LDLM_DEBUG(lock, "client-side local convert handler END");
752         LDLM_LOCK_PUT(lock);
753         RETURN(rc);
754 }
755
756 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
757  * conversion of locks which are on the waiting or converting queue */
758 /* Caller of this code is supposed to take care of lock readers/writers
759    accounting */
760 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, __u32 *flags)
761 {
762         struct ldlm_request   *body;
763         struct ldlm_reply     *reply;
764         struct ldlm_lock      *lock;
765         struct ldlm_resource  *res;
766         struct ptlrpc_request *req;
767         int                    rc;
768         ENTRY;
769
770         lock = ldlm_handle2lock(lockh);
771         if (!lock) {
772                 LBUG();
773                 RETURN(-EINVAL);
774         }
775         *flags = 0;
776
777         if (lock->l_conn_export == NULL)
778                 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
779
780         LDLM_DEBUG(lock, "client-side convert");
781
782         req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
783                                         &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
784                                         LDLM_CONVERT);
785         if (req == NULL) {
786                 LDLM_LOCK_PUT(lock);
787                 RETURN(-ENOMEM);
788         }
789
790         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
791         body->lock_handle[0] = lock->l_remote_handle;
792
793         body->lock_desc.l_req_mode = new_mode;
794         body->lock_flags = *flags;
795
796
797         ptlrpc_request_set_replen(req);
798         rc = ptlrpc_queue_wait(req);
799         if (rc != ELDLM_OK)
800                 GOTO(out, rc);
801
802         reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
803         if (reply == NULL)
804                 GOTO(out, rc = -EPROTO);
805
806         if (req->rq_status)
807                 GOTO(out, rc = req->rq_status);
808
809         res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
810         if (res != NULL) {
811                 ldlm_reprocess_all(res);
812                 /* Go to sleep until the lock is granted. */
813                 /* FIXME: or cancelled. */
814                 if (lock->l_completion_ast) {
815                         rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
816                                                     NULL);
817                         if (rc)
818                                 GOTO(out, rc);
819                 }
820         } else {
821                 rc = EDEADLOCK;
822         }
823         EXIT;
824  out:
825         LDLM_LOCK_PUT(lock);
826         ptlrpc_req_finished(req);
827         return rc;
828 }
829
830 /* Cancel locks locally.
831  * Returns:
832  * LDLM_FL_LOCAL_ONLY if tere is no need in a CANCEL rpc to the server;
833  * LDLM_FL_CANCELING otherwise;
834  * LDLM_FL_BL_AST if there is a need in a separate CANCEL rpc. */
835 static int ldlm_cli_cancel_local(struct ldlm_lock *lock)
836 {
837         int rc = LDLM_FL_LOCAL_ONLY;
838         ENTRY;
839         
840         if (lock->l_conn_export) {
841                 int local_only;
842
843                 LDLM_DEBUG(lock, "client-side cancel");
844                 /* Set this flag to prevent others from getting new references*/
845                 lock_res_and_lock(lock);
846                 lock->l_flags |= LDLM_FL_CBPENDING;
847                 local_only = (lock->l_flags &
848                               (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
849                 ldlm_cancel_callback(lock);
850                 rc = (lock->l_flags & LDLM_FL_BL_AST) ?
851                         LDLM_FL_BL_AST : LDLM_FL_CANCELING;
852                 unlock_res_and_lock(lock);
853
854                 if (local_only) {
855                         CDEBUG(D_DLMTRACE, "not sending request (at caller's "
856                                "instruction)\n");
857                         rc = LDLM_FL_LOCAL_ONLY;
858                 }
859                 ldlm_lock_cancel(lock);
860         } else {
861                 if (ns_is_client(lock->l_resource->lr_namespace)) {
862                         LDLM_ERROR(lock, "Trying to cancel local lock");
863                         LBUG();
864                 }
865                 LDLM_DEBUG(lock, "server-side local cancel");
866                 ldlm_lock_cancel(lock);
867                 ldlm_reprocess_all(lock->l_resource);
868                 LDLM_DEBUG(lock, "server-side local cancel handler END");
869         }
870
871         RETURN(rc);
872 }
873
874 /* Pack @count locks in @head into ldlm_request buffer at the offset @off,
875    of the request @req. */
876 static void ldlm_cancel_pack(struct ptlrpc_request *req,
877                              struct list_head *head, int count)
878 {
879         struct ldlm_request *dlm;
880         struct ldlm_lock *lock;
881         int max, packed = 0;
882         ENTRY;
883
884         dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
885         LASSERT(dlm != NULL);
886
887         /* Check the room in the request buffer. */
888         max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) - 
889                 sizeof(struct ldlm_request);
890         max /= sizeof(struct lustre_handle);
891         max += LDLM_LOCKREQ_HANDLES;
892         LASSERT(max >= dlm->lock_count + count);
893
894         /* XXX: it would be better to pack lock handles grouped by resource.
895          * so that the server cancel would call filter_lvbo_update() less
896          * frequently. */
897         list_for_each_entry(lock, head, l_bl_ast) {
898                 if (!count--)
899                         break;
900                 LASSERT(lock->l_conn_export);
901                 /* Pack the lock handle to the given request buffer. */
902                 LDLM_DEBUG(lock, "packing");
903                 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
904                 packed++;
905         }
906         CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
907         EXIT;
908 }
909
910 /* Prepare and send a batched cancel rpc, it will include count lock handles
911  * of locks given in @head. */
912 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
913                         int count, int flags)
914 {
915         struct ptlrpc_request *req = NULL;
916         struct obd_import *imp;
917         int free, sent = 0;
918         int rc = 0;
919         ENTRY;
920
921         LASSERT(exp != NULL);
922         LASSERT(count > 0);
923
924         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
925                 RETURN(count);
926
927         free = ldlm_format_handles_avail(class_exp2cliimp(exp),
928                                          &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
929         if (count > free)
930                 count = free;
931
932         while (1) {
933                 int bufcount;
934
935                 imp = class_exp2cliimp(exp);
936                 if (imp == NULL || imp->imp_invalid) {
937                         CDEBUG(D_DLMTRACE,
938                                "skipping cancel on invalid import %p\n", imp);
939                         RETURN(count);
940                 }
941
942                 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
943                 if (req == NULL)
944                         GOTO(out, rc = -ENOMEM);
945
946                 bufcount = req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
947                 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
948                                      ldlm_request_bufsize(count, LDLM_CANCEL));
949
950                 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
951                 if (rc) {
952                         ptlrpc_request_free(req);
953                         GOTO(out, rc);
954                 }
955                 req->rq_no_resend = 1;
956                 req->rq_no_delay = 1;
957
958                 /* XXX FIXME bug 249 */
959                 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
960                 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
961
962                 ldlm_cancel_pack(req, cancels, count);
963
964                 ptlrpc_request_set_replen(req);
965                 if (flags & LDLM_FL_ASYNC) {
966                         ptlrpcd_add_req(req);
967                         sent = count;
968                         GOTO(out, 0);
969                 } else {
970                         rc = ptlrpc_queue_wait(req);
971                 }
972                 if (rc == ESTALE) {
973                         CDEBUG(D_DLMTRACE, "client/server (nid %s) "
974                                "out of sync -- not fatal\n",
975                                libcfs_nid2str(req->rq_import->
976                                               imp_connection->c_peer.nid));
977                         rc = 0;
978                 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
979                            req->rq_import_generation == imp->imp_generation) {
980                         ptlrpc_req_finished(req);
981                         continue;
982                 } else if (rc != ELDLM_OK) {
983                         CERROR("Got rc %d from cancel RPC: canceling "
984                                "anyway\n", rc);
985                         break;
986                 }
987                 sent = count;
988                 break;
989         }
990
991         ptlrpc_req_finished(req);
992         EXIT;
993 out:
994         return sent ? sent : rc;
995 }
996
997 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
998 {
999         LASSERT(imp != NULL);
1000         return &imp->imp_obd->obd_namespace->ns_pool;
1001 }
1002
1003 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1004 {
1005         __u64 old_slv, new_slv;
1006         struct ldlm_pool *pl;
1007         __u32 new_limit;
1008         ENTRY;
1009     
1010         if (!imp_connect_lru_resize(req->rq_import))
1011                 RETURN(0);
1012
1013         /* In some cases RPC may contain slv and limit zeroed out. This is 
1014          * the case when server does not support lru resize feature. This is
1015          * also possible in some recovery cases when server side reqs have no
1016          * ref to obd export and thus access to server side namespace is no 
1017          * possible. */
1018         if (lustre_msg_get_slv(req->rq_repmsg) == 0 || 
1019             lustre_msg_get_limit(req->rq_repmsg) == 0) {
1020                 DEBUG_REQ(D_HA, req, "zero SLV or Limit found "
1021                           "(SLV: "LPU64", Limit: %u)", 
1022                           lustre_msg_get_slv(req->rq_repmsg), 
1023                           lustre_msg_get_limit(req->rq_repmsg));
1024                 RETURN(0);
1025         }
1026
1027         new_limit = lustre_msg_get_limit(req->rq_repmsg);
1028         new_slv = lustre_msg_get_slv(req->rq_repmsg);
1029         pl = ldlm_imp2pl(req->rq_import);
1030         
1031         spin_lock(&pl->pl_lock);
1032         old_slv = ldlm_pool_get_slv(pl);
1033         ldlm_pool_set_slv(pl, new_slv);
1034         ldlm_pool_set_limit(pl, new_limit);
1035
1036         /* Check if we need to wakeup pools thread for fast SLV change. 
1037          * This is only done when threads period is noticably long like 
1038          * 10s or more. */
1039 #if defined(__KERNEL__) && (LDLM_POOLS_THREAD_PERIOD >= 10)
1040         {
1041                 __u64 fast_change = old_slv * LDLM_POOLS_FAST_SLV_CHANGE;
1042                 do_div(fast_change, 100);
1043
1044                 /* Wake up pools thread only if SLV has changed more than 
1045                  * 50% since last update. In this case we want to react asap. 
1046                  * Otherwise it is no sense to wake up pools as they are 
1047                  * re-calculated every LDLM_POOLS_THREAD_PERIOD anyways. */
1048                 if (old_slv > new_slv && old_slv - new_slv > fast_change)
1049                         ldlm_pools_wakeup();
1050         }
1051 #endif
1052         spin_unlock(&pl->pl_lock);
1053         RETURN(0);
1054 }
1055 EXPORT_SYMBOL(ldlm_cli_update_pool);
1056
1057 int ldlm_cli_cancel(struct lustre_handle *lockh)
1058 {
1059         struct obd_export *exp;
1060         int avail, flags, count = 1, rc = 0;
1061         struct ldlm_namespace *ns;
1062         struct ldlm_lock *lock;
1063         CFS_LIST_HEAD(cancels);
1064         ENTRY;
1065
1066         /* concurrent cancels on the same handle can happen */
1067         lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
1068         if (lock == NULL) {
1069                 LDLM_DEBUG_NOLOCK("lock is already being destroyed\n");
1070                 RETURN(0);
1071         }
1072
1073         rc = ldlm_cli_cancel_local(lock);
1074         if (rc < 0 || rc == LDLM_FL_LOCAL_ONLY) {
1075                 LDLM_LOCK_PUT(lock);
1076                 RETURN(rc < 0 ? rc : 0);
1077         }
1078         /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1079          * rpc which goes to canceld portal, so we can cancel other lru locks
1080          * here and send them all as one LDLM_CANCEL rpc. */
1081         LASSERT(list_empty(&lock->l_bl_ast));
1082         list_add(&lock->l_bl_ast, &cancels);
1083
1084         exp = lock->l_conn_export;
1085         if (exp_connect_cancelset(exp)) {
1086                 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1087                                                   &RQF_LDLM_CANCEL,
1088                                                   RCL_CLIENT, 0);
1089                 LASSERT(avail > 0);
1090
1091                 ns = lock->l_resource->lr_namespace;
1092                 flags = ns_connect_lru_resize(ns) ?
1093                         LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
1094                 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1095                                                LDLM_FL_BL_AST, flags);
1096         }
1097         ldlm_cli_cancel_list(&cancels, count, NULL, 0);
1098         RETURN(0);
1099 }
1100
1101 /* XXX until we will have compound requests and can cut cancels from generic rpc
1102  * we need send cancels with LDLM_FL_BL_AST flag as separate rpc */
1103 static int ldlm_cancel_list(struct list_head *cancels, int count, int flags)
1104 {
1105         CFS_LIST_HEAD(head);
1106         struct ldlm_lock *lock, *next;
1107         int left = 0, bl_ast = 0, rc;
1108
1109         left = count;
1110         list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1111                 if (left-- == 0)
1112                         break;
1113
1114                 if (flags & LDLM_FL_LOCAL_ONLY) {
1115                         rc = LDLM_FL_LOCAL_ONLY;
1116                         ldlm_lock_cancel(lock);
1117                 } else {
1118                         rc = ldlm_cli_cancel_local(lock);
1119                 }
1120                 if (!(flags & LDLM_FL_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1121                         LDLM_DEBUG(lock, "Cancel lock separately");
1122                         list_del_init(&lock->l_bl_ast);
1123                         list_add(&lock->l_bl_ast, &head);
1124                         bl_ast ++;
1125                         continue;
1126                 }
1127                 if (rc == LDLM_FL_LOCAL_ONLY) {
1128                         /* CANCEL RPC should not be sent to server. */
1129                         list_del_init(&lock->l_bl_ast);
1130                         LDLM_LOCK_PUT(lock);
1131                         count--;
1132                 }
1133
1134         }
1135         if (bl_ast > 0) {
1136                 count -= bl_ast;
1137                 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1138         }
1139
1140         RETURN(count);
1141 }
1142
1143 /* Return 1 to stop lru processing and keep current lock cached. Return zero 
1144  * otherwise. */
1145 static ldlm_policy_res_t ldlm_cancel_shrink_policy(struct ldlm_namespace *ns,
1146                                                    struct ldlm_lock *lock,
1147                                                    int unused, int added, 
1148                                                    int count)
1149 {
1150         int lock_cost;
1151         __u64 page_nr;
1152
1153         /* Stop lru processing when we reached passed @count or checked all 
1154          * locks in lru. */
1155         if (count && added >= count)
1156                 return LDLM_POLICY_KEEP_LOCK;
1157
1158         if (lock->l_resource->lr_type == LDLM_EXTENT) {
1159                 struct ldlm_extent *l_extent;
1160
1161                 /* For all extent locks cost is 1 + number of pages in
1162                  * their extent. */
1163                 l_extent = &lock->l_policy_data.l_extent;
1164                 page_nr = (l_extent->end - l_extent->start);
1165                 do_div(page_nr, CFS_PAGE_SIZE);
1166
1167 #ifdef __KERNEL__
1168                 /* XXX: In fact this is evil hack, we can't access inode
1169                  * here. For doing it right we need somehow to have number
1170                  * of covered by lock. This should be fixed later when 10718 
1171                  * is landed. */
1172                 if (lock->l_ast_data != NULL) {
1173                         struct inode *inode = lock->l_ast_data;
1174                         if (page_nr > inode->i_mapping->nrpages)
1175                                 page_nr = inode->i_mapping->nrpages;
1176                 }
1177 #endif
1178                 lock_cost = 1 + page_nr;
1179         } else {
1180                 /* For all locks which are not extent ones cost is 1 */
1181                 lock_cost = 1;
1182         }
1183
1184         /* Keep all expensive locks in lru for the memory pressure time
1185          * cancel policy. They anyways may be canceled by lru resize
1186          * pplicy if they have not small enough CLV. */
1187         return lock_cost > ns->ns_shrink_thumb ? 
1188                 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1189 }
1190
1191 /* Return 1 to stop lru processing and keep current lock cached. Return zero 
1192  * otherwise. */
1193 static ldlm_policy_res_t ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1194                                                  struct ldlm_lock *lock, 
1195                                                  int unused, int added, 
1196                                                  int count)
1197 {
1198         cfs_time_t cur = cfs_time_current();
1199         struct ldlm_pool *pl = &ns->ns_pool;
1200         __u64 slv, lvf, lv;
1201         cfs_time_t la;
1202
1203         /* Stop lru processing when we reached passed @count or checked all 
1204          * locks in lru. */
1205         if (count && added >= count)
1206                 return LDLM_POLICY_KEEP_LOCK;
1207
1208         spin_lock(&pl->pl_lock);
1209         slv = ldlm_pool_get_slv(pl);
1210         lvf = atomic_read(&pl->pl_lock_volume_factor);
1211         spin_unlock(&pl->pl_lock);
1212
1213         la = cfs_duration_sec(cfs_time_sub(cur, 
1214                               lock->l_last_used));
1215
1216         /* Stop when slv is not yet come from server or 
1217          * lv is smaller than it is. */
1218         lv = lvf * la * unused;
1219         return (slv == 1 || lv < slv) ? 
1220                 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1221 }
1222
1223 /* Return 1 to stop lru processing and keep current lock cached. Return zero 
1224  * otherwise. */
1225 static ldlm_policy_res_t ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1226                                                    struct ldlm_lock *lock, 
1227                                                    int unused, int added,
1228                                                    int count)
1229 {
1230         /* Stop lru processing when we reached passed @count or checked all 
1231          * locks in lru. */
1232         return (added >= count) ? 
1233                 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1234 }
1235
1236 /* Return 1 to stop lru processing and keep current lock cached. Return zero 
1237  * otherwise. */
1238 static ldlm_policy_res_t ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1239                                                  struct ldlm_lock *lock, 
1240                                                  int unused, int added,
1241                                                  int count)
1242 {
1243         /* Stop lru processing if young lock is found and we reached passed 
1244          * @count. */
1245         return ((added >= count) && 
1246                 cfs_time_before(cfs_time_current(),
1247                                 cfs_time_add(lock->l_last_used,
1248                                              ns->ns_max_age))) ? 
1249                 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1250 }
1251
1252 /* Return 1 to stop lru processing and keep current lock cached. Return zero 
1253  * otherwise. */
1254 static ldlm_policy_res_t ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1255                                                     struct ldlm_lock *lock, 
1256                                                     int unused, int added,
1257                                                     int count)
1258 {
1259         /* Stop lru processing when we reached passed @count or checked all 
1260          * locks in lru. */
1261         return (added >= count) ? 
1262                 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1263 }
1264
1265 typedef ldlm_policy_res_t (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *, 
1266                                                       struct ldlm_lock *, int, 
1267                                                       int, int);
1268
1269 static ldlm_cancel_lru_policy_t
1270 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, int flags)
1271 {
1272         if (ns_connect_lru_resize(ns)) {
1273                 if (flags & LDLM_CANCEL_SHRINK)
1274                         return ldlm_cancel_shrink_policy;
1275                 else if (flags & LDLM_CANCEL_LRUR)
1276                         return ldlm_cancel_lrur_policy;
1277                 else if (flags & LDLM_CANCEL_PASSED)
1278                         return ldlm_cancel_passed_policy;
1279         } else {
1280                 if (flags & LDLM_CANCEL_AGED)
1281                         return ldlm_cancel_aged_policy;
1282         }
1283         
1284         return ldlm_cancel_default_policy;
1285 }
1286  
1287 /* - Free space in lru for @count new locks,
1288  *   redundant unused locks are canceled locally;
1289  * - also cancel locally unused aged locks;
1290  * - do not cancel more than @max locks;
1291  * - GET the found locks and add them into the @cancels list.
1292  *
1293  * A client lock can be added to the l_bl_ast list only when it is
1294  * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing CANCEL.
1295  * There are the following use cases: ldlm_cancel_resource_local(),
1296  * ldlm_cancel_lru_local() and ldlm_cli_cancel(), which check&set this
1297  * flag properly. As any attempt to cancel a lock rely on this flag,
1298  * l_bl_ast list is accessed later without any special locking.
1299  *
1300  * Calling policies for enabled lru resize:
1301  * ----------------------------------------
1302  * flags & LDLM_CANCEL_LRUR - use lru resize policy (SLV from server) to
1303  *                            cancel not more than @count locks;
1304  *
1305  * flags & LDLM_CANCEL_PASSED - cancel @count number of old locks (located at
1306  *                              the beginning of lru list);
1307  *
1308  * flags & LDLM_CANCEL_SHRINK - cancel not more than @count locks according to
1309  *                              memory pressre policy function;
1310  *
1311  * flags & LDLM_CANCEL_AGED -   cancel alocks according to "aged policy".
1312  */
1313 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1314                           int count, int max, int cancel_flags, int flags)
1315 {
1316         ldlm_cancel_lru_policy_t pf;
1317         struct ldlm_lock *lock, *next;
1318         int added = 0, unused;
1319         ENTRY;
1320
1321         spin_lock(&ns->ns_unused_lock);
1322         unused = ns->ns_nr_unused;
1323
1324         if (!ns_connect_lru_resize(ns))
1325                 count += unused - ns->ns_max_unused;
1326
1327         pf = ldlm_cancel_lru_policy(ns, flags);
1328         LASSERT(pf != NULL);
1329         
1330         while (!list_empty(&ns->ns_unused_list)) {
1331                 /* For any flags, stop scanning if @max is reached. */
1332                 if (max && added >= max)
1333                         break;
1334
1335                 list_for_each_entry_safe(lock, next, &ns->ns_unused_list, l_lru){
1336                         /* No locks which got blocking requests. */
1337                         LASSERT(!(lock->l_flags & LDLM_FL_BL_AST));
1338
1339                         /* Somebody is already doing CANCEL. No need in this
1340                          * lock in lru, do not traverse it again. */
1341                         if (!(lock->l_flags & LDLM_FL_CANCELING))
1342                                 break;
1343
1344                         ldlm_lock_remove_from_lru_nolock(lock);
1345                 }
1346                 if (&lock->l_lru == &ns->ns_unused_list)
1347                         break;
1348
1349                 /* Pass the lock through the policy filter and see if it
1350                  * should stay in lru.
1351                  *
1352                  * Even for shrinker policy we stop scanning if
1353                  * we find a lock that should stay in the cache.
1354                  * We should take into account lock age anyway
1355                  * as new lock even if it is small of weight is
1356                  * valuable resource. 
1357                  *
1358                  * That is, for shrinker policy we drop only
1359                  * old locks, but additionally chose them by
1360                  * their weight. Big extent locks will stay in 
1361                  * the cache. */
1362                 if (pf(ns, lock, unused, added, count) == LDLM_POLICY_KEEP_LOCK)
1363                         break;
1364
1365                 LDLM_LOCK_GET(lock); /* dropped by bl thread */
1366                 spin_unlock(&ns->ns_unused_lock);
1367
1368                 lock_res_and_lock(lock);
1369                 /* Check flags again under the lock. */
1370                 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1371                     (ldlm_lock_remove_from_lru(lock) == 0)) {
1372                         /* other thread is removing lock from lru or
1373                          * somebody is already doing CANCEL or
1374                          * there is a blocking request which will send
1375                          * cancel by itseft or the lock is matched
1376                          * is already not unused. */
1377                         unlock_res_and_lock(lock);
1378                         LDLM_LOCK_PUT(lock);
1379                         spin_lock(&ns->ns_unused_lock);
1380                         continue;
1381                 }
1382                 LASSERT(!lock->l_readers && !lock->l_writers);
1383
1384                 /* If we have chosen to cancel this lock voluntarily, we
1385                  * better send cancel notification to server, so that it
1386                  * frees appropriate state. This might lead to a race 
1387                  * where while we are doing cancel here, server is also 
1388                  * silently cancelling this lock. */
1389                 lock->l_flags &= ~LDLM_FL_CANCEL_ON_BLOCK;
1390
1391                 /* Setting the CBPENDING flag is a little misleading,
1392                  * but prevents an important race; namely, once
1393                  * CBPENDING is set, the lock can accumulate no more
1394                  * readers/writers. Since readers and writers are
1395                  * already zero here, ldlm_lock_decref() won't see
1396                  * this flag and call l_blocking_ast */
1397                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1398
1399                 /* We can't re-add to l_lru as it confuses the
1400                  * refcounting in ldlm_lock_remove_from_lru() if an AST
1401                  * arrives after we drop ns_lock below. We use l_bl_ast
1402                  * and can't use l_pending_chain as it is used both on
1403                  * server and client nevertheless bug 5666 says it is
1404                  * used only on server */
1405                 LASSERT(list_empty(&lock->l_bl_ast));
1406                 list_add(&lock->l_bl_ast, cancels);
1407                 unlock_res_and_lock(lock);
1408                 spin_lock(&ns->ns_unused_lock);
1409                 added++;
1410                 unused--;
1411         }
1412         spin_unlock(&ns->ns_unused_lock);
1413         RETURN(ldlm_cancel_list(cancels, added, cancel_flags));
1414 }
1415
1416 /* Returns number of locks which could be canceled next time when 
1417  * ldlm_cancel_lru() is called. Used from locks pool shrinker. */
1418 int ldlm_cancel_lru_estimate(struct ldlm_namespace *ns,
1419                              int count, int max, int flags)
1420 {
1421         ldlm_cancel_lru_policy_t pf;
1422         struct ldlm_lock *lock;
1423         int added = 0, unused;
1424         ENTRY;
1425
1426         pf = ldlm_cancel_lru_policy(ns, flags);
1427         LASSERT(pf != NULL);
1428         spin_lock(&ns->ns_unused_lock);
1429         unused = ns->ns_nr_unused;
1430
1431         list_for_each_entry(lock, &ns->ns_unused_list, l_lru) {
1432                 /* For any flags, stop scanning if @max is reached. */
1433                 if (max && added >= max)
1434                         break;
1435
1436                 /* Somebody is already doing CANCEL or there is a
1437                  * blocking request will send cancel. Let's not count 
1438                  * this lock. */
1439                 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1440                     (lock->l_flags & LDLM_FL_BL_AST)) 
1441                         continue;
1442
1443                 /* Pass the lock through the policy filter and see if it
1444                  * should stay in lru. */
1445                 if (pf(ns, lock, unused, added, count) == LDLM_POLICY_KEEP_LOCK)
1446                         break;
1447
1448                 added++;
1449                 unused--;
1450         }
1451         spin_unlock(&ns->ns_unused_lock);
1452         RETURN(added);
1453 }
1454
1455 /* when called with LDLM_ASYNC the blocking callback will be handled
1456  * in a thread and this function will return after the thread has been
1457  * asked to call the callback.  when called with LDLM_SYNC the blocking
1458  * callback will be performed in this function. */
1459 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr, ldlm_sync_t sync, 
1460                     int flags)
1461 {
1462         CFS_LIST_HEAD(cancels);
1463         int count, rc;
1464         ENTRY;
1465
1466 #ifndef __KERNEL__
1467         sync = LDLM_SYNC; /* force to be sync in user space */
1468 #endif
1469         count = ldlm_cancel_lru_local(ns, &cancels, nr, 0, 0, flags);
1470         if (sync == LDLM_ASYNC) {
1471                 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count);
1472                 if (rc == 0)
1473                         RETURN(count);
1474         }
1475
1476         /* If an error occured in ASYNC mode, or
1477          * this is SYNC mode, cancel the list. */
1478         ldlm_cli_cancel_list(&cancels, count, NULL, 0);
1479         RETURN(count);
1480 }
1481
1482 /* Find and cancel locally unused locks found on resource, matched to the
1483  * given policy, mode. GET the found locks and add them into the @cancels
1484  * list. */
1485 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1486                                struct list_head *cancels,
1487                                ldlm_policy_data_t *policy,
1488                                ldlm_mode_t mode, int lock_flags,
1489                                int cancel_flags, void *opaque)
1490 {
1491         struct ldlm_lock *lock;
1492         int count = 0;
1493         ENTRY;
1494
1495         lock_res(res);
1496         list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1497                 if (opaque != NULL && lock->l_ast_data != opaque) {
1498                         LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1499                                    lock->l_ast_data, opaque);
1500                         //LBUG();
1501                         continue;
1502                 }
1503
1504                 if (lock->l_readers || lock->l_writers) {
1505                         if (cancel_flags & LDLM_FL_WARN) {
1506                                 LDLM_ERROR(lock, "lock in use");
1507                                 //LBUG();
1508                         }
1509                         continue;
1510                 }
1511
1512                 /* If somebody is already doing CANCEL, or blocking ast came,
1513                  * skip this lock. */
1514                 if (lock->l_flags & LDLM_FL_BL_AST || 
1515                     lock->l_flags & LDLM_FL_CANCELING)
1516                         continue;
1517
1518                 if (lockmode_compat(lock->l_granted_mode, mode))
1519                         continue;
1520
1521                 /* If policy is given and this is IBITS lock, add to list only
1522                  * those locks that match by policy. */
1523                 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1524                     !(lock->l_policy_data.l_inodebits.bits &
1525                       policy->l_inodebits.bits))
1526                         continue;
1527
1528                 /* See CBPENDING comment in ldlm_cancel_lru */
1529                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1530                                  lock_flags;
1531
1532                 LASSERT(list_empty(&lock->l_bl_ast));
1533                 list_add(&lock->l_bl_ast, cancels);
1534                 LDLM_LOCK_GET(lock);
1535                 count++;
1536         }
1537         unlock_res(res);
1538
1539         RETURN(ldlm_cancel_list(cancels, count, cancel_flags));
1540 }
1541
1542 /* If @req is NULL, send CANCEL request to server with handles of locks 
1543  * in the @cancels. If EARLY_CANCEL is not supported, send CANCEL requests 
1544  * separately per lock.
1545  * If @req is not NULL, put handles of locks in @cancels into the request 
1546  * buffer at the offset @off.
1547  * Destroy @cancels at the end. */
1548 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1549                          struct ptlrpc_request *req, int flags)
1550 {
1551         struct ldlm_lock *lock;
1552         int res = 0;
1553         ENTRY;
1554
1555         if (list_empty(cancels) || count == 0)
1556                 RETURN(0);
1557         
1558         /* XXX: requests (both batched and not) could be sent in parallel. 
1559          * Usually it is enough to have just 1 RPC, but it is possible that
1560          * there are to many locks to be cancelled in LRU or on a resource.
1561          * It would also speed up the case when the server does not support
1562          * the feature. */
1563         while (count > 0) {
1564                 LASSERT(!list_empty(cancels));
1565                 lock = list_entry(cancels->next, struct ldlm_lock, l_bl_ast);
1566                 LASSERT(lock->l_conn_export);
1567
1568                 if (exp_connect_cancelset(lock->l_conn_export)) {
1569                         res = count;
1570                         if (req)
1571                                 ldlm_cancel_pack(req, cancels, count);
1572                         else
1573                                 res = ldlm_cli_cancel_req(lock->l_conn_export,
1574                                                           cancels, count,
1575                                                           flags);
1576                 } else {
1577                         res = ldlm_cli_cancel_req(lock->l_conn_export,
1578                                                   cancels, 1, flags);
1579                 }
1580
1581                 if (res < 0) {
1582                         CERROR("ldlm_cli_cancel_list: %d\n", res);
1583                         res = count;
1584                 }
1585
1586                 count -= res;
1587                 ldlm_lock_list_put(cancels, l_bl_ast, res);
1588         }
1589         LASSERT(count == 0);
1590         RETURN(0);
1591 }
1592
1593 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1594                                     const struct ldlm_res_id *res_id,
1595                                     ldlm_policy_data_t *policy,
1596                                     ldlm_mode_t mode, int flags, void *opaque)
1597 {
1598         struct ldlm_resource *res;
1599         CFS_LIST_HEAD(cancels);
1600         int count;
1601         int rc;
1602         ENTRY;
1603
1604         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1605         if (res == NULL) {
1606                 /* This is not a problem. */
1607                 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1608                 RETURN(0);
1609         }
1610
1611         count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1612                                            0, flags, opaque);
1613         rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
1614         if (rc != ELDLM_OK)
1615                 CERROR("ldlm_cli_cancel_unused_resource: %d\n", rc);
1616
1617         ldlm_resource_putref(res);
1618         RETURN(0);
1619 }
1620
1621 static inline int have_no_nsresource(struct ldlm_namespace *ns)
1622 {
1623         int no_resource = 0;
1624
1625         spin_lock(&ns->ns_hash_lock);
1626         if (ns->ns_resources == 0)
1627                 no_resource = 1;
1628         spin_unlock(&ns->ns_hash_lock);
1629
1630         RETURN(no_resource);
1631 }
1632
1633 /* Cancel all locks on a namespace (or a specific resource, if given)
1634  * that have 0 readers/writers.
1635  *
1636  * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
1637  * to notify the server. */
1638 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
1639                            const struct ldlm_res_id *res_id,
1640                            int flags, void *opaque)
1641 {
1642         int i;
1643         ENTRY;
1644
1645         if (ns == NULL)
1646                 RETURN(ELDLM_OK);
1647
1648         if (res_id)
1649                 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
1650                                                        LCK_MINMODE, flags,
1651                                                        opaque));
1652
1653         spin_lock(&ns->ns_hash_lock);
1654         for (i = 0; i < RES_HASH_SIZE; i++) {
1655                 struct list_head *tmp;
1656                 tmp = ns->ns_hash[i].next;
1657                 while (tmp != &(ns->ns_hash[i])) {
1658                         struct ldlm_resource *res;
1659                         int rc;
1660
1661                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
1662                         ldlm_resource_getref(res);
1663                         spin_unlock(&ns->ns_hash_lock);
1664
1665                         rc = ldlm_cli_cancel_unused_resource(ns, &res->lr_name,
1666                                                              NULL, LCK_MINMODE,
1667                                                              flags, opaque);
1668
1669                         if (rc)
1670                                 CERROR("ldlm_cli_cancel_unused ("LPU64"): %d\n",
1671                                        res->lr_name.name[0], rc);
1672
1673                         spin_lock(&ns->ns_hash_lock);
1674                         tmp = tmp->next;
1675                         ldlm_resource_putref_locked(res);
1676                 }
1677         }
1678         spin_unlock(&ns->ns_hash_lock);
1679
1680         RETURN(ELDLM_OK);
1681 }
1682
1683 /* join/split resource locks to/from lru list */
1684 int ldlm_cli_join_lru(struct ldlm_namespace *ns,
1685                       const struct ldlm_res_id *res_id, int join)
1686 {
1687         struct ldlm_resource *res;
1688         struct ldlm_lock *lock, *n;
1689         int count = 0;
1690         ENTRY;
1691
1692         LASSERT(ns_is_client(ns));
1693
1694         res = ldlm_resource_get(ns, NULL, res_id, LDLM_EXTENT, 0);
1695         if (res == NULL)
1696                 RETURN(count);
1697         LASSERT(res->lr_type == LDLM_EXTENT);
1698
1699         lock_res(res);
1700         if (!join)
1701                 goto split;
1702
1703         list_for_each_entry_safe (lock, n, &res->lr_granted, l_res_link) {
1704                 if (list_empty(&lock->l_lru) &&
1705                     !lock->l_readers && !lock->l_writers &&
1706                     !(lock->l_flags & LDLM_FL_LOCAL) &&
1707                     !(lock->l_flags & LDLM_FL_CBPENDING) &&
1708                     !(lock->l_flags & LDLM_FL_BL_AST)) {
1709                         ldlm_lock_add_to_lru(lock);
1710                         lock->l_flags &= ~LDLM_FL_NO_LRU;
1711                         LDLM_DEBUG(lock, "join lock to lru");
1712                         count++;
1713                 }
1714         }
1715         goto unlock;
1716 split:
1717         spin_lock(&ns->ns_unused_lock);
1718         list_for_each_entry_safe (lock, n, &ns->ns_unused_list, l_lru) {
1719                 if (lock->l_resource == res) {
1720                         ldlm_lock_remove_from_lru_nolock(lock);
1721                         lock->l_flags |= LDLM_FL_NO_LRU;
1722                         LDLM_DEBUG(lock, "split lock from lru");
1723                         count++;
1724                 }
1725         }
1726         spin_unlock(&ns->ns_unused_lock);
1727 unlock:
1728         unlock_res(res);
1729         ldlm_resource_putref(res);
1730         RETURN(count);
1731 }
1732
1733 /* Lock iterators. */
1734
1735 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1736                           void *closure)
1737 {
1738         struct list_head *tmp, *next;
1739         struct ldlm_lock *lock;
1740         int rc = LDLM_ITER_CONTINUE;
1741
1742         ENTRY;
1743
1744         if (!res)
1745                 RETURN(LDLM_ITER_CONTINUE);
1746
1747         lock_res(res);
1748         list_for_each_safe(tmp, next, &res->lr_granted) {
1749                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1750
1751                 if (iter(lock, closure) == LDLM_ITER_STOP)
1752                         GOTO(out, rc = LDLM_ITER_STOP);
1753         }
1754
1755         list_for_each_safe(tmp, next, &res->lr_converting) {
1756                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1757
1758                 if (iter(lock, closure) == LDLM_ITER_STOP)
1759                         GOTO(out, rc = LDLM_ITER_STOP);
1760         }
1761
1762         list_for_each_safe(tmp, next, &res->lr_waiting) {
1763                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1764
1765                 if (iter(lock, closure) == LDLM_ITER_STOP)
1766                         GOTO(out, rc = LDLM_ITER_STOP);
1767         }
1768  out:
1769         unlock_res(res);
1770         RETURN(rc);
1771 }
1772
1773 struct iter_helper_data {
1774         ldlm_iterator_t iter;
1775         void *closure;
1776 };
1777
1778 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
1779 {
1780         struct iter_helper_data *helper = closure;
1781         return helper->iter(lock, helper->closure);
1782 }
1783
1784 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
1785 {
1786         return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
1787 }
1788
1789 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
1790                            void *closure)
1791 {
1792         struct iter_helper_data helper = { iter: iter, closure: closure };
1793         return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
1794 }
1795
1796 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
1797                                ldlm_res_iterator_t iter, void *closure)
1798 {
1799         int i, rc = LDLM_ITER_CONTINUE;
1800         struct ldlm_resource *res;
1801         struct list_head *tmp;
1802
1803         ENTRY;
1804         spin_lock(&ns->ns_hash_lock);
1805         for (i = 0; i < RES_HASH_SIZE; i++) {
1806                 tmp = ns->ns_hash[i].next;
1807                 while (tmp != &(ns->ns_hash[i])) {
1808                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
1809                         ldlm_resource_getref(res);
1810                         spin_unlock(&ns->ns_hash_lock);
1811
1812                         rc = iter(res, closure);
1813
1814                         spin_lock(&ns->ns_hash_lock);
1815                         tmp = tmp->next;
1816                         ldlm_resource_putref_locked(res);
1817                         if (rc == LDLM_ITER_STOP)
1818                                 GOTO(out, rc);
1819                 }
1820         }
1821  out:
1822         spin_unlock(&ns->ns_hash_lock);
1823         RETURN(rc);
1824 }
1825
1826 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
1827 void ldlm_resource_iterate(struct ldlm_namespace *ns,
1828                            const struct ldlm_res_id *res_id,
1829                            ldlm_iterator_t iter, void *data)
1830 {
1831         struct ldlm_resource *res;
1832         ENTRY;
1833
1834         if (ns == NULL) {
1835                 CERROR("must pass in namespace\n");
1836                 LBUG();
1837         }
1838
1839         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1840         if (res == NULL) {
1841                 EXIT;
1842                 return;
1843         }
1844
1845         ldlm_resource_foreach(res, iter, data);
1846         ldlm_resource_putref(res);
1847         EXIT;
1848 }
1849
1850 /* Lock replay */
1851
1852 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
1853 {
1854         struct list_head *list = closure;
1855
1856         /* we use l_pending_chain here, because it's unused on clients. */
1857         LASSERTF(list_empty(&lock->l_pending_chain),"lock %p next %p prev %p\n",
1858                  lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
1859         /* bug 9573: don't replay locks left after eviction */
1860         if (!(lock->l_flags & LDLM_FL_FAILED))
1861                 list_add(&lock->l_pending_chain, list);
1862         return LDLM_ITER_CONTINUE;
1863 }
1864
1865 static int replay_lock_interpret(struct ptlrpc_request *req,
1866                                  struct ldlm_async_args *aa, int rc)
1867 {
1868         struct ldlm_lock  *lock;
1869         struct ldlm_reply *reply;
1870
1871         ENTRY;
1872         atomic_dec(&req->rq_import->imp_replay_inflight);
1873         if (rc != ELDLM_OK)
1874                 GOTO(out, rc);
1875
1876
1877         reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1878         if (reply == NULL)
1879                 GOTO(out, rc = -EPROTO);
1880
1881         lock = ldlm_handle2lock(&aa->lock_handle);
1882         if (!lock) {
1883                 CERROR("received replay ack for unknown local cookie "LPX64
1884                        " remote cookie "LPX64 " from server %s id %s\n",
1885                        aa->lock_handle.cookie, reply->lock_handle.cookie,
1886                        req->rq_export->exp_client_uuid.uuid,
1887                        libcfs_id2str(req->rq_peer));
1888                 GOTO(out, rc = -ESTALE);
1889         }
1890
1891         lock->l_remote_handle = reply->lock_handle;
1892         LDLM_DEBUG(lock, "replayed lock:");
1893         ptlrpc_import_recovery_state_machine(req->rq_import);
1894         LDLM_LOCK_PUT(lock);
1895 out:
1896         if (rc != ELDLM_OK)
1897                 ptlrpc_connect_import(req->rq_import, NULL);
1898
1899
1900         RETURN(rc);
1901 }
1902
1903 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
1904 {
1905         struct ptlrpc_request *req;
1906         struct ldlm_async_args *aa;
1907         struct ldlm_request   *body;
1908         int flags;
1909         ENTRY;
1910
1911
1912         /* Bug 11974: Do not replay a lock which is actively being canceled */
1913         if (lock->l_flags & LDLM_FL_CANCELING) {
1914                 LDLM_DEBUG(lock, "Not replaying canceled lock:");
1915                 RETURN(0);
1916         }
1917
1918         /* If this is reply-less callback lock, we cannot replay it, since
1919          * server might have long dropped it, but notification of that event was
1920          * lost by network. (and server granted conflicting lock already) */
1921         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1922                 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
1923                 ldlm_lock_cancel(lock);
1924                 RETURN(0);
1925         }
1926         /*
1927          * If granted mode matches the requested mode, this lock is granted.
1928          *
1929          * If they differ, but we have a granted mode, then we were granted
1930          * one mode and now want another: ergo, converting.
1931          *
1932          * If we haven't been granted anything and are on a resource list,
1933          * then we're blocked/waiting.
1934          *
1935          * If we haven't been granted anything and we're NOT on a resource list,
1936          * then we haven't got a reply yet and don't have a known disposition.
1937          * This happens whenever a lock enqueue is the request that triggers
1938          * recovery.
1939          */
1940         if (lock->l_granted_mode == lock->l_req_mode)
1941                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
1942         else if (lock->l_granted_mode)
1943                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
1944         else if (!list_empty(&lock->l_res_link))
1945                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
1946         else
1947                 flags = LDLM_FL_REPLAY;
1948
1949         req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
1950                                         LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
1951         if (req == NULL)
1952                 RETURN(-ENOMEM);
1953
1954         /* We're part of recovery, so don't wait for it. */
1955         req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
1956
1957         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1958         ldlm_lock2desc(lock, &body->lock_desc);
1959         body->lock_flags = flags;
1960
1961         ldlm_lock2handle(lock, &body->lock_handle[0]);
1962         if (lock->l_lvb_len != 0) {
1963                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
1964                 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
1965                                      lock->l_lvb_len);
1966         }
1967         ptlrpc_request_set_replen(req);
1968         /* notify the server we've replayed all requests.
1969          * also, we mark the request to be put on a dedicated
1970          * queue to be processed after all request replayes.
1971          * bug 6063 */
1972         lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
1973
1974         LDLM_DEBUG(lock, "replaying lock:");
1975
1976         atomic_inc(&req->rq_import->imp_replay_inflight);
1977         CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1978         aa = (struct ldlm_async_args *)&req->rq_async_args;
1979         aa->lock_handle = body->lock_handle[0];
1980         req->rq_interpret_reply = replay_lock_interpret;
1981         ptlrpcd_add_req(req);
1982
1983         RETURN(0);
1984 }
1985
1986 int ldlm_replay_locks(struct obd_import *imp)
1987 {
1988         struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
1989         CFS_LIST_HEAD(list);
1990         struct ldlm_lock *lock, *next;
1991         int rc = 0;
1992
1993         ENTRY;
1994
1995         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1996
1997         /* ensure this doesn't fall to 0 before all have been queued */
1998         atomic_inc(&imp->imp_replay_inflight);
1999
2000         (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2001
2002         list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2003                 list_del_init(&lock->l_pending_chain);
2004                 if (rc)
2005                         continue; /* or try to do the rest? */
2006                 rc = replay_one_lock(imp, lock);
2007         }
2008
2009         atomic_dec(&imp->imp_replay_inflight);
2010
2011         RETURN(rc);
2012 }