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