Whamcloud - gitweb
b=14194
[fs/lustre-release.git] / lustre / ldlm / ldlm_lockd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002-2004 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  */
26
27 #ifndef EXPORT_SYMTAB
28 # define EXPORT_SYMTAB
29 #endif
30 #define DEBUG_SUBSYSTEM S_LDLM
31
32 #ifdef __KERNEL__
33 # include <libcfs/libcfs.h>
34 #else
35 # include <liblustre.h>
36 #endif
37
38 #include <lustre_dlm.h>
39 #include <obd_class.h>
40 #include <libcfs/list.h>
41 #include "ldlm_internal.h"
42
43 #ifdef __KERNEL__
44 static int ldlm_num_threads;
45 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
46                 "number of DLM service threads to start");
47 #endif
48
49 extern cfs_mem_cache_t *ldlm_resource_slab;
50 extern cfs_mem_cache_t *ldlm_lock_slab;
51 static struct semaphore ldlm_ref_sem;
52 static int ldlm_refcount;
53
54 /* LDLM state */
55
56 static struct ldlm_state *ldlm_state;
57
58 inline cfs_time_t round_timeout(cfs_time_t timeout)
59 {
60         return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
61 }
62
63 /* timeout for initial callback (AST) reply */
64 static inline unsigned int ldlm_get_rq_timeout(unsigned int ldlm_timeout,
65                                                unsigned int obd_timeout)
66 {
67         unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
68
69         return timeout < 1 ? 1 : timeout;
70 }
71
72 #ifdef __KERNEL__
73 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
74 static spinlock_t waiting_locks_spinlock;   /* BH lock (timer) */
75 static struct list_head waiting_locks_list;
76 static cfs_timer_t waiting_locks_timer;
77
78 static struct expired_lock_thread {
79         cfs_waitq_t               elt_waitq;
80         int                       elt_state;
81         int                       elt_dump;
82         struct list_head          elt_expired_locks;
83 } expired_lock_thread;
84 #endif
85
86 #define ELT_STOPPED   0
87 #define ELT_READY     1
88 #define ELT_TERMINATE 2
89
90 struct ldlm_bl_pool {
91         spinlock_t              blp_lock;
92
93         /*
94          * blp_prio_list is used for callbacks that should be handled
95          * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
96          * see bug 13843
97          */
98         struct list_head        blp_prio_list;
99
100         /*
101          * blp_list is used for all other callbacks which are likely
102          * to take longer to process.
103          */
104         struct list_head        blp_list;
105
106         cfs_waitq_t             blp_waitq;
107         struct completion       blp_comp;
108         atomic_t                blp_num_threads;
109         atomic_t                blp_busy_threads;
110         int                     blp_min_threads;
111         int                     blp_max_threads;
112 };
113
114 struct ldlm_bl_work_item {
115         struct list_head        blwi_entry;
116         struct ldlm_namespace   *blwi_ns;
117         struct ldlm_lock_desc   blwi_ld;
118         struct ldlm_lock        *blwi_lock;
119         struct list_head        blwi_head;
120         int                     blwi_count;
121 };
122
123 #ifdef __KERNEL__
124
125 static inline int have_expired_locks(void)
126 {
127         int need_to_run;
128
129         ENTRY;
130         spin_lock_bh(&waiting_locks_spinlock);
131         need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
132         spin_unlock_bh(&waiting_locks_spinlock);
133
134         RETURN(need_to_run);
135 }
136
137 static int expired_lock_main(void *arg)
138 {
139         struct list_head *expired = &expired_lock_thread.elt_expired_locks;
140         struct l_wait_info lwi = { 0 };
141         int do_dump;
142
143         ENTRY;
144         cfs_daemonize("ldlm_elt");
145
146         expired_lock_thread.elt_state = ELT_READY;
147         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
148
149         while (1) {
150                 l_wait_event(expired_lock_thread.elt_waitq,
151                              have_expired_locks() ||
152                              expired_lock_thread.elt_state == ELT_TERMINATE,
153                              &lwi);
154
155                 spin_lock_bh(&waiting_locks_spinlock);
156                 if (expired_lock_thread.elt_dump) {
157                         spin_unlock_bh(&waiting_locks_spinlock);
158
159                         /* from waiting_locks_callback, but not in timer */
160                         libcfs_debug_dumplog();
161                         libcfs_run_lbug_upcall(__FILE__,
162                                                 "waiting_locks_callback",
163                                                 expired_lock_thread.elt_dump);
164
165                         spin_lock_bh(&waiting_locks_spinlock);
166                         expired_lock_thread.elt_dump = 0;
167                 }
168
169                 do_dump = 0;
170
171                 while (!list_empty(expired)) {
172                         struct obd_export *export;
173                         struct ldlm_lock *lock;
174
175                         lock = list_entry(expired->next, struct ldlm_lock,
176                                           l_pending_chain);
177                         if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
178                             (void *)lock >= LP_POISON) {
179                                 spin_unlock_bh(&waiting_locks_spinlock);
180                                 CERROR("free lock on elt list %p\n", lock);
181                                 LBUG();
182                         }
183                         list_del_init(&lock->l_pending_chain);
184                         if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
185                             (void *)lock->l_export >= LP_POISON) {
186                                 CERROR("lock with free export on elt list %p\n",
187                                        lock->l_export);
188                                 lock->l_export = NULL;
189                                 LDLM_ERROR(lock, "free export");
190                                 continue;
191                         }
192                         export = class_export_get(lock->l_export);
193                         spin_unlock_bh(&waiting_locks_spinlock);
194
195                         do_dump++;
196                         class_fail_export(export);
197                         class_export_put(export);
198                         spin_lock_bh(&waiting_locks_spinlock);
199                 }
200                 spin_unlock_bh(&waiting_locks_spinlock);
201
202                 if (do_dump && obd_dump_on_eviction) {
203                         CERROR("dump the log upon eviction\n");
204                         libcfs_debug_dumplog();
205                 }
206
207                 if (expired_lock_thread.elt_state == ELT_TERMINATE)
208                         break;
209         }
210
211         expired_lock_thread.elt_state = ELT_STOPPED;
212         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
213         RETURN(0);
214 }
215
216 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
217
218 /* This is called from within a timer interrupt and cannot schedule */
219 static void waiting_locks_callback(unsigned long unused)
220 {
221         struct ldlm_lock *lock, *last = NULL;
222
223 repeat:
224         spin_lock_bh(&waiting_locks_spinlock);
225         while (!list_empty(&waiting_locks_list)) {
226                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
227                                   l_pending_chain);
228
229                 if (cfs_time_after(lock->l_callback_timeout, cfs_time_current()) ||
230                     (lock->l_req_mode == LCK_GROUP))
231                         break;
232
233                 if (ptlrpc_check_suspend()) {
234                         /* there is a case when we talk to one mds, holding
235                          * lock from another mds. this way we easily can get
236                          * here, if second mds is being recovered. so, we
237                          * suspend timeouts. bug 6019 */
238
239                         LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
240                                    lock->l_export->exp_client_uuid.uuid,
241                                    lock->l_export->exp_connection->c_remote_uuid.uuid,
242                                    libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
243
244                         list_del_init(&lock->l_pending_chain);
245                         spin_unlock_bh(&waiting_locks_spinlock);
246                         ldlm_add_waiting_lock(lock);
247                         goto repeat;
248                 }
249
250                 /* if timeout overlaps the activation time of suspended timeouts
251                  * then extend it to give a chance for client to reconnect */
252                 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
253                                                  cfs_time_seconds(obd_timeout)/2),
254                                     ptlrpc_suspend_wakeup_time())) {
255                         LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
256                                    lock->l_export->exp_client_uuid.uuid,
257                                    lock->l_export->exp_connection->c_remote_uuid.uuid,
258                                    libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
259
260                         list_del_init(&lock->l_pending_chain);
261                         spin_unlock_bh(&waiting_locks_spinlock);
262                         ldlm_add_waiting_lock(lock);
263                         goto repeat;
264                 }
265
266                 LDLM_ERROR(lock, "lock callback timer expired: evicting client "
267                            "%s@%s nid %s\n",
268                            lock->l_export->exp_client_uuid.uuid,
269                            lock->l_export->exp_connection->c_remote_uuid.uuid,
270                            libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
271
272                 last = lock;
273
274                 list_del(&lock->l_pending_chain);
275                 list_add(&lock->l_pending_chain,
276                          &expired_lock_thread.elt_expired_locks);
277         }
278
279         if (!list_empty(&expired_lock_thread.elt_expired_locks)) {
280                 if (obd_dump_on_timeout)
281                         expired_lock_thread.elt_dump = __LINE__;
282
283                 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
284         }
285
286         /*
287          * Make sure the timer will fire again if we have any locks
288          * left.
289          */
290         if (!list_empty(&waiting_locks_list)) {
291                 cfs_time_t timeout_rounded;
292                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
293                                   l_pending_chain);
294                 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
295                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
296         }
297         spin_unlock_bh(&waiting_locks_spinlock);
298 }
299
300 /*
301  * Indicate that we're waiting for a client to call us back cancelling a given
302  * lock.  We add it to the pending-callback chain, and schedule the lock-timeout
303  * timer to fire appropriately.  (We round up to the next second, to avoid
304  * floods of timer firings during periods of high lock contention and traffic).
305  *
306  * Called with the namespace lock held.
307  */
308 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock)
309 {
310         cfs_time_t timeout_rounded;
311
312         if (!list_empty(&lock->l_pending_chain))
313                 return 0;
314
315         lock->l_callback_timeout =cfs_time_add(cfs_time_current(),
316                                                cfs_time_seconds(obd_timeout)/2);
317
318         timeout_rounded = round_timeout(lock->l_callback_timeout);
319
320         if (cfs_time_before(timeout_rounded, cfs_timer_deadline(&waiting_locks_timer)) ||
321             !cfs_timer_is_armed(&waiting_locks_timer)) {
322                 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
323
324         }
325         list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
326         return 1;
327 }
328
329 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
330 {
331         int ret;
332
333         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
334
335         spin_lock_bh(&waiting_locks_spinlock);
336         if (lock->l_destroyed) {
337                 static cfs_time_t next;
338                 spin_unlock_bh(&waiting_locks_spinlock);
339                 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
340                 if (cfs_time_after(cfs_time_current(), next)) {
341                         next = cfs_time_shift(14400);
342                         libcfs_debug_dumpstack(NULL);
343                 }
344                 return 0;
345         }
346
347         ret = __ldlm_add_waiting_lock(lock);
348         spin_unlock_bh(&waiting_locks_spinlock);
349
350         LDLM_DEBUG(lock, "%sadding to wait list",
351                    ret == 0 ? "not re-" : "");
352         return ret;
353 }
354
355 /*
356  * Remove a lock from the pending list, likely because it had its cancellation
357  * callback arrive without incident.  This adjusts the lock-timeout timer if
358  * needed.  Returns 0 if the lock wasn't pending after all, 1 if it was.
359  *
360  * Called with namespace lock held.
361  */
362 int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
363 {
364         struct list_head *list_next;
365
366         if (list_empty(&lock->l_pending_chain))
367                 return 0;
368
369         list_next = lock->l_pending_chain.next;
370         if (lock->l_pending_chain.prev == &waiting_locks_list) {
371                 /* Removing the head of the list, adjust timer. */
372                 if (list_next == &waiting_locks_list) {
373                         /* No more, just cancel. */
374                         cfs_timer_disarm(&waiting_locks_timer);
375                 } else {
376                         struct ldlm_lock *next;
377                         next = list_entry(list_next, struct ldlm_lock,
378                                           l_pending_chain);
379                         cfs_timer_arm(&waiting_locks_timer,
380                                       round_timeout(next->l_callback_timeout));
381                 }
382         }
383         list_del_init(&lock->l_pending_chain);
384
385         return 1;
386 }
387
388 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
389 {
390         int ret;
391
392         if (lock->l_export == NULL) {
393                 /* We don't have a "waiting locks list" on clients. */
394                 LDLM_DEBUG(lock, "client lock: no-op");
395                 return 0;
396         }
397
398         spin_lock_bh(&waiting_locks_spinlock);
399         ret = __ldlm_del_waiting_lock(lock);
400         spin_unlock_bh(&waiting_locks_spinlock);
401
402         LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
403         return ret;
404 }
405
406 /*
407  * Prolong the lock
408  *
409  * Called with namespace lock held.
410  */
411 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
412 {
413         if (lock->l_export == NULL) {
414                 /* We don't have a "waiting locks list" on clients. */
415                 LDLM_DEBUG(lock, "client lock: no-op");
416                 return 0;
417         }
418
419         spin_lock_bh(&waiting_locks_spinlock);
420
421         if (list_empty(&lock->l_pending_chain)) {
422                 spin_unlock_bh(&waiting_locks_spinlock);
423                 LDLM_DEBUG(lock, "wasn't waiting");
424                 return 0;
425         }
426
427         __ldlm_del_waiting_lock(lock);
428         __ldlm_add_waiting_lock(lock);
429         spin_unlock_bh(&waiting_locks_spinlock);
430
431         LDLM_DEBUG(lock, "refreshed");
432         return 1;
433 }
434
435 #else /* !__KERNEL__ */
436
437 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
438 {
439         LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
440         RETURN(1);
441 }
442
443 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
444 {
445         RETURN(0);
446 }
447
448 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock)
449 {
450         RETURN(0);
451 }
452 #endif /* __KERNEL__ */
453
454 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
455                             const char *ast_type)
456 {
457         struct ptlrpc_connection *conn = lock->l_export->exp_connection;
458         char                     *str = libcfs_nid2str(conn->c_peer.nid);
459
460         LCONSOLE_ERROR_MSG(0x138, "A client on nid %s was evicted from "
461                            "service %s.\n", str,
462                            lock->l_export->exp_obd->obd_name);
463
464         LCONSOLE_ERROR_MSG(0x012, "Lock %s callback to %s timed out for "
465                            "resource %d\n", ast_type,
466                            obd_export_nid2str(lock->l_export), rc);
467
468         if (obd_dump_on_timeout)
469                 libcfs_debug_dumplog();
470         class_fail_export(lock->l_export);
471 }
472
473 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
474                                  struct ptlrpc_request *req, int rc,
475                                  const char *ast_type)
476 {
477         lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
478
479         if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
480                 LASSERT(lock->l_export);
481                 if (lock->l_export->exp_libclient) {
482                         LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
483                                    " timeout, just cancelling lock", ast_type,
484                                    libcfs_nid2str(peer.nid));
485                         ldlm_lock_cancel(lock);
486                         rc = -ERESTART;
487                 } else if (lock->l_flags & LDLM_FL_CANCEL) {
488                         LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
489                                    "cancel was received (AST reply lost?)",
490                                    ast_type, libcfs_nid2str(peer.nid));
491                         ldlm_lock_cancel(lock);
492                         rc = -ERESTART;
493                 } else {
494                         ldlm_del_waiting_lock(lock);
495                         ldlm_failed_ast(lock, rc, ast_type);
496                 }
497         } else if (rc) {
498                 if (rc == -EINVAL)
499                         LDLM_DEBUG(lock, "client (nid %s) returned %d"
500                                " from %s AST - normal race",
501                                libcfs_nid2str(peer.nid),
502                                req->rq_repmsg ?
503                                lustre_msg_get_status(req->rq_repmsg) : -1,
504                                ast_type);
505                 else
506                         LDLM_ERROR(lock, "client (nid %s) returned %d "
507                                    "from %s AST", libcfs_nid2str(peer.nid),
508                                    (req->rq_repmsg != NULL) ?
509                                    lustre_msg_get_status(req->rq_repmsg) : 0,
510                                    ast_type);
511                 ldlm_lock_cancel(lock);
512                 /* Server-side AST functions are called from ldlm_reprocess_all,
513                  * which needs to be told to please restart its reprocessing. */
514                 rc = -ERESTART;
515         }
516
517         return rc;
518 }
519
520 static int ldlm_cb_interpret(struct ptlrpc_request *req, void *data, int rc)
521 {
522         struct ldlm_cb_set_arg *arg;
523         struct ldlm_lock *lock;
524         ENTRY;
525
526         LASSERT(data != NULL);
527
528         arg = req->rq_async_args.pointer_arg[0];
529         lock = req->rq_async_args.pointer_arg[1];
530         LASSERT(lock != NULL);
531         if (rc != 0) {
532                 /* If client canceled the lock but the cancel has not 
533                  * been recieved yet, we need to update lvbo to have the
534                  * proper attributes cached. */
535                 if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
536                         ldlm_res_lvbo_update(lock->l_resource, NULL, 
537                                              0, 1);
538                 rc = ldlm_handle_ast_error(lock, req, rc, 
539                                            arg->type == LDLM_BL_CALLBACK
540                                            ? "blocking" : "completion");
541         }                
542
543         LDLM_LOCK_PUT(lock);
544
545         if (rc == -ERESTART)
546                 atomic_set(&arg->restart, 1);
547
548         RETURN(0);
549 }
550
551 static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
552                                           struct ldlm_cb_set_arg *arg,
553                                           struct ldlm_lock *lock,
554                                           int instant_cancel)
555 {
556         int rc = 0;
557         ENTRY;
558
559         if (unlikely(instant_cancel)) {
560                 rc = ptl_send_rpc(req, 1);
561                 ptlrpc_req_finished(req);
562                 if (rc == 0)
563                         /* If we cancelled the lock, we need to restart
564                          * ldlm_reprocess_queue */
565                         atomic_set(&arg->restart, 1);
566         } else {
567                 LDLM_LOCK_GET(lock);
568                 ptlrpc_set_add_req(arg->set, req);
569         }       
570
571         RETURN(rc);
572 }
573
574 /*
575  * ->l_blocking_ast() method for server-side locks. This is invoked when newly
576  * enqueued server lock conflicts with given one.
577  *
578  * Sends blocking ast rpc to the client owning that lock; arms timeout timer
579  * to wait for client response.
580  */
581 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
582                              struct ldlm_lock_desc *desc,
583                              void *data, int flag)
584 {
585         struct ldlm_cb_set_arg *arg = data;
586         struct ldlm_request *body;
587         struct ptlrpc_request *req;
588         int size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
589                        [DLM_LOCKREQ_OFF]     = sizeof(*body) };
590         int instant_cancel = 0, rc;
591         ENTRY;
592
593         if (flag == LDLM_CB_CANCELING) {
594                 /* Don't need to do anything here. */
595                 RETURN(0);
596         }
597
598         LASSERT(lock);
599         LASSERT(data != NULL);
600         if (lock->l_export->exp_obd->obd_recovering != 0) {
601                 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
602                 ldlm_lock_dump(D_ERROR, lock, 0);
603         }
604
605         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
606                               LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK, 2, size,
607                               NULL);
608         if (req == NULL)
609                 RETURN(-ENOMEM);
610
611         req->rq_async_args.pointer_arg[0] = arg;
612         req->rq_async_args.pointer_arg[1] = lock;
613         req->rq_interpret_reply = ldlm_cb_interpret;
614         req->rq_no_resend = 1;
615
616         lock_res(lock->l_resource);
617         if (lock->l_granted_mode != lock->l_req_mode) {
618                 /* this blocking AST will be communicated as part of the
619                  * completion AST instead */
620                 unlock_res(lock->l_resource);
621                 ptlrpc_req_finished(req);
622                 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
623                 RETURN(0);
624         }
625
626         if (lock->l_destroyed) {
627                 /* What's the point? */
628                 unlock_res(lock->l_resource);
629                 ptlrpc_req_finished(req);
630                 RETURN(0);
631         }
632
633         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
634                 instant_cancel = 1;
635
636         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
637         body->lock_handle[0] = lock->l_remote_handle;
638         body->lock_desc = *desc;
639         body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
640
641         LDLM_DEBUG(lock, "server preparing blocking AST");
642
643         ptlrpc_req_set_repsize(req, 1, NULL);
644         if (instant_cancel) {
645                 unlock_res(lock->l_resource);
646                 ldlm_lock_cancel(lock);
647         } else {
648                 LASSERT(lock->l_granted_mode == lock->l_req_mode);
649                 ldlm_add_waiting_lock(lock);
650                 unlock_res(lock->l_resource);
651         }
652
653         req->rq_send_state = LUSTRE_IMP_FULL;
654         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
655
656         if (lock->l_export && lock->l_export->exp_ldlm_stats)
657                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
658                                      LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
659
660         rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
661
662         RETURN(rc);
663 }
664
665 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
666 {
667         struct ldlm_cb_set_arg *arg = data;
668         struct ldlm_request *body;
669         struct ptlrpc_request *req;
670         struct timeval granted_time;
671         long total_enqueue_wait;
672         int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
673                         [DLM_LOCKREQ_OFF]     = sizeof(*body) };
674         int rc, buffers = 2, instant_cancel = 0;
675         ENTRY;
676
677         LASSERT(lock != NULL);
678         LASSERT(data != NULL);
679
680         do_gettimeofday(&granted_time);
681         total_enqueue_wait = cfs_timeval_sub(&granted_time,
682                                              &lock->l_enqueued_time, NULL);
683
684         if (total_enqueue_wait / 1000000 > obd_timeout)
685                 LDLM_ERROR(lock, "enqueue wait took %luus from %lu",
686                            total_enqueue_wait, lock->l_enqueued_time.tv_sec);
687
688         lock_res_and_lock(lock);
689         if (lock->l_resource->lr_lvb_len) {
690                 size[DLM_REQ_REC_OFF] = lock->l_resource->lr_lvb_len;
691                 buffers = 3;
692         }
693         unlock_res_and_lock(lock);
694
695         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
696                               LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK, buffers,
697                               size, NULL);
698         if (req == NULL)
699                 RETURN(-ENOMEM);
700
701         req->rq_async_args.pointer_arg[0] = arg;
702         req->rq_async_args.pointer_arg[1] = lock;
703         req->rq_interpret_reply = ldlm_cb_interpret;
704         req->rq_no_resend = 1;
705
706         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
707         body->lock_handle[0] = lock->l_remote_handle;
708         body->lock_flags = flags;
709         ldlm_lock2desc(lock, &body->lock_desc);
710
711         if (buffers == 3) {
712                 void *lvb;
713
714                 lvb = lustre_msg_buf(req->rq_reqmsg, DLM_REQ_REC_OFF,
715                                      lock->l_resource->lr_lvb_len);
716                 lock_res_and_lock(lock);
717                 memcpy(lvb, lock->l_resource->lr_lvb_data,
718                        lock->l_resource->lr_lvb_len);
719                 unlock_res_and_lock(lock);
720         }
721
722         LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
723                    total_enqueue_wait);
724
725         ptlrpc_req_set_repsize(req, 1, NULL);
726
727         req->rq_send_state = LUSTRE_IMP_FULL;
728         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
729
730         /* We only send real blocking ASTs after the lock is granted */
731         lock_res_and_lock(lock);
732         if (lock->l_flags & LDLM_FL_AST_SENT) {
733                 body->lock_flags |= LDLM_FL_AST_SENT;
734
735                 /* We might get here prior to ldlm_handle_enqueue setting
736                  * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
737                  * into waiting list, but this is safe and similar code in
738                  * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
739                  * that would not only cancel the lock, but will also remove
740                  * it from waiting list */
741                 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
742                         unlock_res_and_lock(lock);
743                         ldlm_lock_cancel(lock);
744                         instant_cancel = 1;
745                         lock_res_and_lock(lock);
746                 } else {
747                         /* start the lock-timeout clock */
748                         ldlm_add_waiting_lock(lock);
749                 }
750         }
751         unlock_res_and_lock(lock);
752
753         if (lock->l_export && lock->l_export->exp_ldlm_stats)
754                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
755                                      LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
756
757         rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
758
759         RETURN(rc);
760 }
761
762 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
763 {
764         struct ldlm_resource *res = lock->l_resource;
765         struct ldlm_request *body;
766         struct ptlrpc_request *req;
767         int size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
768                        [DLM_LOCKREQ_OFF]     = sizeof(*body) };
769         int rc = 0;
770         ENTRY;
771
772         LASSERT(lock != NULL);
773
774         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
775                               LUSTRE_DLM_VERSION, LDLM_GL_CALLBACK, 2, size,
776                               NULL);
777         if (req == NULL)
778                 RETURN(-ENOMEM);
779
780         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
781         body->lock_handle[0] = lock->l_remote_handle;
782         ldlm_lock2desc(lock, &body->lock_desc);
783
784         lock_res_and_lock(lock);
785         size[REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
786         unlock_res_and_lock(lock);
787         res = lock->l_resource;
788         ptlrpc_req_set_repsize(req, 2, size);
789
790         req->rq_send_state = LUSTRE_IMP_FULL;
791         req->rq_timeout = ldlm_get_rq_timeout(ldlm_timeout, obd_timeout);
792
793         if (lock->l_export && lock->l_export->exp_ldlm_stats)
794                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
795                                      LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
796
797         rc = ptlrpc_queue_wait(req);
798         if (rc == -ELDLM_NO_LOCK_DATA)
799                 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
800         else if (rc != 0)
801                 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
802         else
803                 rc = ldlm_res_lvbo_update(res, req->rq_repmsg,
804                                           REPLY_REC_OFF, 1);
805         ptlrpc_req_finished(req);
806         RETURN(rc);
807 }
808
809 static struct ldlm_lock *
810 find_existing_lock(struct obd_export *exp,
811                    const struct lustre_handle *remote_hdl)
812 {
813         struct list_head *iter;
814
815         spin_lock(&exp->exp_ldlm_data.led_lock);
816         list_for_each(iter, &exp->exp_ldlm_data.led_held_locks) {
817                 struct ldlm_lock *lock;
818                 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
819                 if (lock->l_remote_handle.cookie == remote_hdl->cookie) {
820                         LDLM_LOCK_GET(lock);
821                         spin_unlock(&exp->exp_ldlm_data.led_lock);
822                         return lock;
823                 }
824         }
825         spin_unlock(&exp->exp_ldlm_data.led_lock);
826         return NULL;
827 }
828
829 #ifdef __KERNEL__
830 extern unsigned long long lu_time_stamp_get(void);
831 #else
832 #define lu_time_stamp_get() time(NULL)
833 #endif
834
835 static void ldlm_svc_get_eopc(struct ldlm_request *dlm_req,
836                        struct lprocfs_stats *srv_stats)
837 {
838         int lock_type = 0, op = 0;
839
840         lock_type = dlm_req->lock_desc.l_resource.lr_type;
841
842         switch (lock_type) {
843         case LDLM_PLAIN:
844                 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
845                 break;
846         case LDLM_EXTENT:
847                 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
848                         op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
849                 else
850                         op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
851                 break;
852         case LDLM_FLOCK:
853                 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
854                 break;
855         case LDLM_IBITS:
856                 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
857                 break;
858         default:
859                 op = 0;
860                 break;
861         }
862
863         if (op)
864                 lprocfs_counter_incr(srv_stats, op);
865
866         return ;
867 }
868
869 /*
870  * Main server-side entry point into LDLM. This is called by ptlrpc service
871  * threads to carry out client lock enqueueing requests.
872  */
873 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
874                          struct ptlrpc_request *req,
875                          const struct ldlm_request *dlm_req,
876                          const struct ldlm_callback_suite *cbs)
877 {
878         struct ldlm_reply *dlm_rep;
879         int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
880                         [DLM_LOCKREPLY_OFF]   = sizeof(*dlm_rep) };
881         int rc = 0;
882         __u32 flags;
883         ldlm_error_t err = ELDLM_OK;
884         struct ldlm_lock *lock = NULL;
885         void *cookie = NULL;
886         ENTRY;
887
888         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
889
890         ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
891         flags = dlm_req->lock_flags;
892
893         LASSERT(req->rq_export);
894
895         if (req->rq_rqbd->rqbd_service->srv_stats)
896                 ldlm_svc_get_eopc(dlm_req,
897                                   req->rq_rqbd->rqbd_service->srv_stats);
898
899         if (req->rq_export->exp_ldlm_stats)
900                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
901                                      LDLM_ENQUEUE - LDLM_FIRST_OPC);
902
903         if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
904                      dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
905                 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
906                           dlm_req->lock_desc.l_resource.lr_type);
907                 GOTO(out, rc = -EFAULT);
908         }
909
910         if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
911                      dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
912                      dlm_req->lock_desc.l_req_mode &
913                      (dlm_req->lock_desc.l_req_mode-1))) {
914                 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
915                           dlm_req->lock_desc.l_req_mode);
916                 GOTO(out, rc = -EFAULT);
917         }
918
919         if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
920                 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
921                              LDLM_PLAIN)) {
922                         DEBUG_REQ(D_ERROR, req,
923                                   "PLAIN lock request from IBITS client?");
924                         GOTO(out, rc = -EPROTO);
925                 }
926         } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
927                             LDLM_IBITS)) {
928                 DEBUG_REQ(D_ERROR, req,
929                           "IBITS lock request from unaware client?");
930                 GOTO(out, rc = -EPROTO);
931         }
932
933 #if 0
934         /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
935            against server's _CONNECT_SUPPORTED flags? (I don't want to use
936            ibits for mgc/mgs) */
937
938         /* INODEBITS_INTEROP: Perform conversion from plain lock to
939          * inodebits lock if client does not support them. */
940         if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
941             (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
942                 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
943                 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
944                         MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
945                 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
946                         dlm_req->lock_desc.l_req_mode = LCK_CR;
947         }
948 #endif
949
950         if (unlikely(flags & LDLM_FL_REPLAY)) {
951                 lock = find_existing_lock(req->rq_export,
952                                           &dlm_req->lock_handle[0]);
953                 if (lock != NULL) {
954                         DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
955                                   LPX64, lock->l_handle.h_cookie);
956                         GOTO(existing_lock, rc = 0);
957                 }
958         }
959
960         /* The lock's callback data might be set in the policy function */
961         lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
962                                 dlm_req->lock_desc.l_resource.lr_type,
963                                 dlm_req->lock_desc.l_req_mode,
964                                 cbs->lcs_blocking, cbs->lcs_completion,
965                                 cbs->lcs_glimpse, NULL, 0);
966
967         if (!lock)
968                 GOTO(out, rc = -ENOMEM);
969
970         do_gettimeofday(&lock->l_enqueued_time);
971         lock->l_remote_handle = dlm_req->lock_handle[0];
972         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
973
974         OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
975         /* Don't enqueue a lock onto the export if it has already
976          * been evicted.  Cancel it now instead. (bug 3822) */
977         if (req->rq_export->exp_failed) {
978                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
979                 GOTO(out, rc = -ENOTCONN);
980         }
981         lock->l_export = class_export_get(req->rq_export);
982         spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
983         list_add(&lock->l_export_chain,
984                  &lock->l_export->exp_ldlm_data.led_held_locks);
985         spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
986
987 existing_lock:
988
989         if (flags & LDLM_FL_HAS_INTENT) {
990                 /* In this case, the reply buffer is allocated deep in
991                  * local_lock_enqueue by the policy function. */
992                 cookie = req;
993         } else {
994                 int buffers = 2;
995
996                 lock_res_and_lock(lock);
997                 if (lock->l_resource->lr_lvb_len) {
998                         size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
999                         buffers = 3;
1000                 }
1001                 unlock_res_and_lock(lock);
1002
1003                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1004                         GOTO(out, rc = -ENOMEM);
1005
1006                 rc = lustre_pack_reply(req, buffers, size, NULL);
1007                 if (rc)
1008                         GOTO(out, rc);
1009         }
1010
1011         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1012                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1013         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1014                 lock->l_req_extent = lock->l_policy_data.l_extent;
1015
1016         err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1017         if (err)
1018                 GOTO(out, err);
1019
1020         dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1021                                  sizeof(*dlm_rep));
1022         dlm_rep->lock_flags = flags;
1023
1024         ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1025         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1026
1027         /* We never send a blocking AST until the lock is granted, but
1028          * we can tell it right now */
1029         lock_res_and_lock(lock);
1030
1031         /* Now take into account flags to be inherited from original lock
1032            request both in reply to client and in our own lock flags. */
1033         dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1034         lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1035
1036         /* Don't move a pending lock onto the export if it has already
1037          * been evicted.  Cancel it now instead. (bug 5683) */
1038         if (unlikely(req->rq_export->exp_failed ||
1039                      OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1040                 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1041                 rc = -ENOTCONN;
1042         } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1043                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1044                 if (lock->l_granted_mode == lock->l_req_mode) {
1045                         /*
1046                          * Only cancel lock if it was granted, because it would
1047                          * be destroyed immediatelly and would never be granted
1048                          * in the future, causing timeouts on client.  Not
1049                          * granted lock will be cancelled immediatelly after
1050                          * sending completion AST.
1051                          */
1052                         if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1053                                 unlock_res_and_lock(lock);
1054                                 ldlm_lock_cancel(lock);
1055                                 lock_res_and_lock(lock);
1056                         } else
1057                                 ldlm_add_waiting_lock(lock);
1058                 }
1059         }
1060         /* Make sure we never ever grant usual metadata locks to liblustre
1061            clients */
1062         if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1063             dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1064              req->rq_export->exp_libclient) {
1065                 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1066                              !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1067                         CERROR("Granting sync lock to libclient. "
1068                                "req fl %d, rep fl %d, lock fl %d\n",
1069                                dlm_req->lock_flags, dlm_rep->lock_flags,
1070                                lock->l_flags);
1071                         LDLM_ERROR(lock, "sync lock");
1072                         if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1073                                 struct ldlm_intent *it;
1074                                 it = lustre_msg_buf(req->rq_reqmsg,
1075                                                     DLM_INTENT_IT_OFF,
1076                                                     sizeof(*it));
1077                                 if (it != NULL) {
1078                                         CERROR("This is intent %s ("LPU64")\n",
1079                                                ldlm_it2str(it->opc), it->opc);
1080                                 }
1081                         }
1082                 }
1083         }
1084
1085         unlock_res_and_lock(lock);
1086
1087         EXIT;
1088  out:
1089         req->rq_status = err;
1090         if (req->rq_reply_state == NULL) {
1091                 err = lustre_pack_reply(req, 1, NULL, NULL);
1092                 if (rc == 0)
1093                         rc = err;
1094                 req->rq_status = rc;
1095         }
1096
1097         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1098          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
1099         if (lock) {
1100                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1101                            "(err=%d, rc=%d)", err, rc);
1102
1103                 lock_res_and_lock(lock);
1104                 if (rc == 0) {
1105                         size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
1106                         if (size[DLM_REPLY_REC_OFF] > 0) {
1107                                 void *lvb = lustre_msg_buf(req->rq_repmsg,
1108                                                        DLM_REPLY_REC_OFF,
1109                                                        size[DLM_REPLY_REC_OFF]);
1110                                 LASSERTF(lvb != NULL, "req %p, lock %p\n",
1111                                          req, lock);
1112
1113                                 memcpy(lvb, lock->l_resource->lr_lvb_data,
1114                                        size[DLM_REPLY_REC_OFF]);
1115                         }
1116                 } else {
1117                         ldlm_resource_unlink_lock(lock);
1118                         ldlm_lock_destroy_nolock(lock);
1119                 }
1120                 unlock_res_and_lock(lock);
1121
1122                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1123                         ldlm_reprocess_all(lock->l_resource);
1124
1125                 LDLM_LOCK_PUT(lock);
1126         }
1127
1128         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1129                           lock, rc);
1130
1131         return rc;
1132 }
1133
1134 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1135                         ldlm_completion_callback completion_callback,
1136                         ldlm_blocking_callback blocking_callback,
1137                         ldlm_glimpse_callback glimpse_callback)
1138 {
1139         int rc;
1140         struct ldlm_request *dlm_req;
1141         struct ldlm_callback_suite cbs = {
1142                 .lcs_completion = completion_callback,
1143                 .lcs_blocking   = blocking_callback,
1144                 .lcs_glimpse    = glimpse_callback
1145         };
1146
1147         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1148                                      sizeof *dlm_req, lustre_swab_ldlm_request);
1149         if (dlm_req != NULL) {
1150                 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1151                                           req, dlm_req, &cbs);
1152         } else {
1153                 CERROR ("Can't unpack dlm_req\n");
1154                 rc = -EFAULT;
1155         }
1156         return rc;
1157 }
1158
1159 int ldlm_handle_convert0(struct ptlrpc_request *req,
1160                          const struct ldlm_request *dlm_req)
1161 {
1162         struct ldlm_reply *dlm_rep;
1163         struct ldlm_lock *lock;
1164         int rc;
1165         int size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
1166                         [DLM_LOCKREPLY_OFF]   = sizeof(*dlm_rep) };
1167         ENTRY;
1168
1169         if (req->rq_export && req->rq_export->exp_ldlm_stats)
1170                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1171                                      LDLM_CONVERT - LDLM_FIRST_OPC);
1172
1173         rc = lustre_pack_reply(req, 2, size, NULL);
1174         if (rc)
1175                 RETURN(rc);
1176
1177         dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
1178                                  sizeof(*dlm_rep));
1179         dlm_rep->lock_flags = dlm_req->lock_flags;
1180
1181         lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1182         if (!lock) {
1183                 req->rq_status = EINVAL;
1184         } else {
1185                 void *res = NULL;
1186
1187                 LDLM_DEBUG(lock, "server-side convert handler START");
1188
1189                 do_gettimeofday(&lock->l_enqueued_time);
1190                 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1191                                         &dlm_rep->lock_flags);
1192                 if (res) {
1193                         if (ldlm_del_waiting_lock(lock))
1194                                 LDLM_DEBUG(lock, "converted waiting lock");
1195                         req->rq_status = 0;
1196                 } else {
1197                         req->rq_status = EDEADLOCK;
1198                 }
1199         }
1200
1201         if (lock) {
1202                 if (!req->rq_status)
1203                         ldlm_reprocess_all(lock->l_resource);
1204                 LDLM_DEBUG(lock, "server-side convert handler END");
1205                 LDLM_LOCK_PUT(lock);
1206         } else
1207                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1208
1209         RETURN(0);
1210 }
1211
1212 int ldlm_handle_convert(struct ptlrpc_request *req)
1213 {
1214         int rc;
1215         struct ldlm_request *dlm_req;
1216
1217         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof *dlm_req,
1218                                      lustre_swab_ldlm_request);
1219         if (dlm_req != NULL) {
1220                 rc = ldlm_handle_convert0(req, dlm_req);
1221         } else {
1222                 CERROR ("Can't unpack dlm_req\n");
1223                 rc = -EFAULT;
1224         }
1225         return rc;
1226 }
1227
1228 /* Cancel all the locks, which handles are packed into ldlm_request */
1229 int ldlm_request_cancel(struct ptlrpc_request *req,
1230                         const struct ldlm_request *dlm_req, int first)
1231 {
1232         struct ldlm_resource *res, *pres = NULL;
1233         struct ldlm_lock *lock;
1234         int i, count, done = 0;
1235         ENTRY;
1236
1237         count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1238         if (first >= count)
1239                 RETURN(0);
1240
1241         /* There is no lock on the server at the replay time,
1242          * skip lock cancelling to make replay tests to pass. */
1243         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1244                 RETURN(0);
1245
1246         LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1247                           "starting at %d", count, first);
1248
1249         for (i = first; i < count; i++) {
1250                 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1251                 if (!lock) {
1252                         LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1253                                           "lock (cookie "LPU64")",
1254                                           dlm_req->lock_handle[i].cookie);
1255                         continue;
1256                 }
1257
1258                 res = lock->l_resource;
1259                 done++;
1260
1261                 if (res != pres) {
1262                         if (pres != NULL) {
1263                                 ldlm_reprocess_all(pres);
1264                                 ldlm_resource_putref(pres);
1265                         }
1266                         if (res != NULL) {
1267                                 ldlm_resource_getref(res);
1268                                 ldlm_res_lvbo_update(res, NULL, 0, 1);
1269                         }
1270                         pres = res;
1271                 }
1272                 ldlm_lock_cancel(lock);
1273                 LDLM_LOCK_PUT(lock);
1274         }
1275         if (pres != NULL) {
1276                 ldlm_reprocess_all(pres);
1277                 ldlm_resource_putref(pres);
1278         }
1279         LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1280         RETURN(done);
1281 }
1282
1283 int ldlm_handle_cancel(struct ptlrpc_request *req)
1284 {
1285         struct ldlm_request *dlm_req;
1286         int rc;
1287         ENTRY;
1288
1289         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1290                                      lustre_swab_ldlm_request);
1291         if (dlm_req == NULL) {
1292                 CERROR("bad request buffer for cancel\n");
1293                 RETURN(-EFAULT);
1294         }
1295
1296         if (req->rq_export && req->rq_export->exp_ldlm_stats)
1297                 lprocfs_counter_incr(req->rq_export->exp_ldlm_stats,
1298                                      LDLM_CANCEL - LDLM_FIRST_OPC);
1299
1300         rc = lustre_pack_reply(req, 1, NULL, NULL);
1301         if (rc)
1302                 RETURN(rc);
1303
1304         if (!ldlm_request_cancel(req, dlm_req, 0))
1305                 req->rq_status = ESTALE;
1306
1307         if (ptlrpc_reply(req) != 0)
1308                 LBUG();
1309
1310         RETURN(0);
1311 }
1312
1313 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1314                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1315 {
1316         int do_ast;
1317         ENTRY;
1318
1319         LDLM_DEBUG(lock, "client blocking AST callback handler START");
1320
1321         lock_res_and_lock(lock);
1322         lock->l_flags |= LDLM_FL_CBPENDING;
1323
1324         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1325                 lock->l_flags |= LDLM_FL_CANCEL;
1326
1327         do_ast = (!lock->l_readers && !lock->l_writers);
1328         unlock_res_and_lock(lock);
1329
1330         if (do_ast) {
1331                 LDLM_DEBUG(lock, "already unused, calling "
1332                            "callback (%p)", lock->l_blocking_ast);
1333                 if (lock->l_blocking_ast != NULL)
1334                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1335                                              LDLM_CB_BLOCKING);
1336         } else {
1337                 LDLM_DEBUG(lock, "Lock still has references, will be"
1338                            " cancelled later");
1339         }
1340
1341         LDLM_DEBUG(lock, "client blocking callback handler END");
1342         LDLM_LOCK_PUT(lock);
1343         EXIT;
1344 }
1345
1346 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1347                                     struct ldlm_namespace *ns,
1348                                     struct ldlm_request *dlm_req,
1349                                     struct ldlm_lock *lock)
1350 {
1351         CFS_LIST_HEAD(ast_list);
1352         ENTRY;
1353
1354         LDLM_DEBUG(lock, "client completion callback handler START");
1355
1356         lock_res_and_lock(lock);
1357
1358         /* If we receive the completion AST before the actual enqueue returned,
1359          * then we might need to switch lock modes, resources, or extents. */
1360         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1361                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1362                 LDLM_DEBUG(lock, "completion AST, new lock mode");
1363         }
1364
1365         if (lock->l_resource->lr_type != LDLM_PLAIN) {
1366                 lock->l_policy_data = dlm_req->lock_desc.l_policy_data;
1367                 LDLM_DEBUG(lock, "completion AST, new policy data");
1368         }
1369
1370         ldlm_resource_unlink_lock(lock);
1371         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1372                    &lock->l_resource->lr_name,
1373                    sizeof(lock->l_resource->lr_name)) != 0) {
1374                 unlock_res_and_lock(lock);
1375                 ldlm_lock_change_resource(ns, lock,
1376                                         &dlm_req->lock_desc.l_resource.lr_name);
1377                 LDLM_DEBUG(lock, "completion AST, new resource");
1378                 CERROR("change resource!\n");
1379                 lock_res_and_lock(lock);
1380         }
1381
1382         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1383                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1384                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1385         }
1386
1387         if (lock->l_lvb_len) {
1388                 void *lvb;
1389                 lvb = lustre_swab_reqbuf(req, DLM_REQ_REC_OFF, lock->l_lvb_len,
1390                                          lock->l_lvb_swabber);
1391                 if (lvb == NULL) {
1392                         LDLM_ERROR(lock, "completion AST did not contain "
1393                                    "expected LVB!");
1394                 } else {
1395                         memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1396                 }
1397         }
1398
1399         ldlm_grant_lock(lock, &ast_list);
1400         unlock_res_and_lock(lock);
1401
1402         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1403
1404         ldlm_run_cp_ast_work(&ast_list);
1405
1406         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1407                           lock);
1408         LDLM_LOCK_PUT(lock);
1409         EXIT;
1410 }
1411
1412 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1413                                     struct ldlm_namespace *ns,
1414                                     struct ldlm_request *dlm_req,
1415                                     struct ldlm_lock *lock)
1416 {
1417         int rc = -ENOSYS;
1418         ENTRY;
1419
1420         LDLM_DEBUG(lock, "client glimpse AST callback handler");
1421
1422         if (lock->l_glimpse_ast != NULL)
1423                 rc = lock->l_glimpse_ast(lock, req);
1424
1425         if (req->rq_repmsg != NULL) {
1426                 ptlrpc_reply(req);
1427         } else {
1428                 req->rq_status = rc;
1429                 ptlrpc_error(req);
1430         }
1431
1432         lock_res_and_lock(lock);
1433         if (lock->l_granted_mode == LCK_PW &&
1434             !lock->l_readers && !lock->l_writers &&
1435             cfs_time_after(cfs_time_current(),
1436                            cfs_time_add(lock->l_last_used,
1437                                         cfs_time_seconds(10)))) {
1438                 unlock_res_and_lock(lock);
1439                 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1440                         ldlm_handle_bl_callback(ns, NULL, lock);
1441
1442                 EXIT;
1443                 return;
1444         }
1445         unlock_res_and_lock(lock);
1446         LDLM_LOCK_PUT(lock);
1447         EXIT;
1448 }
1449
1450 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1451 {
1452         if (req->rq_no_reply)
1453                 return 0;
1454
1455         req->rq_status = rc;
1456         if (req->rq_reply_state == NULL) {
1457                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1458                 if (rc)
1459                         return rc;
1460         }
1461         return ptlrpc_reply(req);
1462 }
1463
1464 #ifdef __KERNEL__
1465 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1466                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1467                              struct list_head *cancels, int count)
1468 {
1469         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1470         struct ldlm_bl_work_item *blwi;
1471         ENTRY;
1472
1473         if (cancels && count == 0)
1474                 RETURN(0);
1475
1476         OBD_ALLOC(blwi, sizeof(*blwi));
1477         if (blwi == NULL)
1478                 RETURN(-ENOMEM);
1479
1480         blwi->blwi_ns = ns;
1481         if (ld != NULL)
1482                 blwi->blwi_ld = *ld;
1483         if (count) {
1484                 list_add(&blwi->blwi_head, cancels);
1485                 list_del_init(cancels);
1486                 blwi->blwi_count = count;
1487         } else {
1488                 blwi->blwi_lock = lock;
1489         }
1490         spin_lock(&blp->blp_lock);
1491         if (lock && lock->l_flags & LDLM_FL_DISCARD_DATA) {
1492                 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1493                 list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1494         } else {
1495                 /* other blocking callbacks are added to the regular list */
1496                 list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1497         }
1498         cfs_waitq_signal(&blp->blp_waitq);
1499         spin_unlock(&blp->blp_lock);
1500
1501         RETURN(0);
1502 }
1503 #endif
1504
1505 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1506                            struct ldlm_lock *lock)
1507 {
1508 #ifdef __KERNEL__
1509         RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
1510 #else
1511         RETURN(-ENOSYS);
1512 #endif
1513 }
1514
1515 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1516                            struct list_head *cancels, int count)
1517 {
1518 #ifdef __KERNEL__
1519         RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
1520 #else
1521         RETURN(-ENOSYS);
1522 #endif
1523 }
1524
1525 static int ldlm_callback_handler(struct ptlrpc_request *req)
1526 {
1527         struct ldlm_namespace *ns;
1528         struct ldlm_request *dlm_req;
1529         struct ldlm_lock *lock;
1530         int rc;
1531         ENTRY;
1532
1533         /* Requests arrive in sender's byte order.  The ptlrpc service
1534          * handler has already checked and, if necessary, byte-swapped the
1535          * incoming request message body, but I am responsible for the
1536          * message buffers. */
1537
1538         if (req->rq_export == NULL) {
1539                 struct ldlm_request *dlm_req;
1540
1541                 CDEBUG(D_RPCTRACE, "operation %d from %s with bad "
1542                        "export cookie "LPX64"; this is "
1543                        "normal if this node rebooted with a lock held\n",
1544                        lustre_msg_get_opc(req->rq_reqmsg),
1545                        libcfs_id2str(req->rq_peer),
1546                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1547
1548                 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1549                                              sizeof(*dlm_req),
1550                                              lustre_swab_ldlm_request);
1551                 if (dlm_req != NULL)
1552                         CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
1553                                dlm_req->lock_handle[0].cookie);
1554
1555                 ldlm_callback_reply(req, -ENOTCONN);
1556                 RETURN(0);
1557         }
1558
1559         LASSERT(req->rq_export != NULL);
1560         LASSERT(req->rq_export->exp_obd != NULL);
1561
1562         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1563         case LDLM_BL_CALLBACK:
1564                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1565                         RETURN(0);
1566                 break;
1567         case LDLM_CP_CALLBACK:
1568                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1569                         RETURN(0);
1570                 break;
1571         case LDLM_GL_CALLBACK:
1572                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1573                         RETURN(0);
1574                 break;
1575         case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1576                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1577                         RETURN(0);
1578                 rc = llog_origin_handle_cancel(req);
1579                 ldlm_callback_reply(req, rc);
1580                 RETURN(0);
1581         case OBD_QC_CALLBACK:
1582                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
1583                         RETURN(0);
1584                 rc = target_handle_qc_callback(req);
1585                 ldlm_callback_reply(req, rc);
1586                 RETURN(0);
1587         case QUOTA_DQACQ:
1588         case QUOTA_DQREL:
1589                 /* reply in handler */
1590                 rc = target_handle_dqacq_callback(req);
1591                 RETURN(0);
1592         case LLOG_ORIGIN_HANDLE_CREATE:
1593                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1594                         RETURN(0);
1595                 rc = llog_origin_handle_create(req);
1596                 ldlm_callback_reply(req, rc);
1597                 RETURN(0);
1598         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1599                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1600                         RETURN(0);
1601                 rc = llog_origin_handle_next_block(req);
1602                 ldlm_callback_reply(req, rc);
1603                 RETURN(0);
1604         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1605                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1606                         RETURN(0);
1607                 rc = llog_origin_handle_read_header(req);
1608                 ldlm_callback_reply(req, rc);
1609                 RETURN(0);
1610         case LLOG_ORIGIN_HANDLE_CLOSE:
1611                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
1612                         RETURN(0);
1613                 rc = llog_origin_handle_close(req);
1614                 ldlm_callback_reply(req, rc);
1615                 RETURN(0);
1616         case SEC_CTX_FINI:
1617                 /* do nothing */
1618                 RETURN(0);
1619         default:
1620                 CERROR("unknown opcode %u\n",
1621                        lustre_msg_get_opc(req->rq_reqmsg));
1622                 ldlm_callback_reply(req, -EPROTO);
1623                 RETURN(0);
1624         }
1625
1626         ns = req->rq_export->exp_obd->obd_namespace;
1627         LASSERT(ns != NULL);
1628
1629         dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF, sizeof(*dlm_req),
1630                                      lustre_swab_ldlm_request);
1631         if (dlm_req == NULL) {
1632                 CERROR ("can't unpack dlm_req\n");
1633                 ldlm_callback_reply(req, -EPROTO);
1634                 RETURN (0);
1635         }
1636
1637         lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle[0]);
1638         if (!lock) {
1639                 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
1640                        "disappeared\n", dlm_req->lock_handle[0].cookie);
1641                 ldlm_callback_reply(req, -EINVAL);
1642                 RETURN(0);
1643         }
1644
1645         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1646         lock_res_and_lock(lock);
1647         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1648         if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
1649                 /* If somebody cancels locks and cache is already droped,
1650                  * we can tell the server we have no lock. Otherwise, we
1651                  * should send cancel after dropping the cache. */
1652                 if ((lock->l_flags & LDLM_FL_CANCELING) &&
1653                     (lock->l_flags & LDLM_FL_BL_DONE)) {
1654                         LDLM_DEBUG(lock, "callback on lock "
1655                                    LPX64" - lock disappeared\n",
1656                                    dlm_req->lock_handle[0].cookie);
1657                         unlock_res_and_lock(lock);
1658                         LDLM_LOCK_PUT(lock);
1659                         ldlm_callback_reply(req, -EINVAL);
1660                         RETURN(0);
1661                 }
1662                 lock->l_flags |= LDLM_FL_BL_AST;
1663         }
1664         unlock_res_and_lock(lock);
1665
1666         /* We want the ost thread to get this reply so that it can respond
1667          * to ost requests (write cache writeback) that might be triggered
1668          * in the callback.
1669          *
1670          * But we'd also like to be able to indicate in the reply that we're
1671          * cancelling right now, because it's unused, or have an intent result
1672          * in the reply, so we might have to push the responsibility for sending
1673          * the reply down into the AST handlers, alas. */
1674
1675         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1676         case LDLM_BL_CALLBACK:
1677                 CDEBUG(D_INODE, "blocking ast\n");
1678                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
1679                         ldlm_callback_reply(req, 0);
1680                 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
1681                         ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1682                 break;
1683         case LDLM_CP_CALLBACK:
1684                 CDEBUG(D_INODE, "completion ast\n");
1685                 ldlm_callback_reply(req, 0);
1686                 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1687                 break;
1688         case LDLM_GL_CALLBACK:
1689                 CDEBUG(D_INODE, "glimpse ast\n");
1690                 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1691                 break;
1692         default:
1693                 LBUG();                         /* checked above */
1694         }
1695
1696         RETURN(0);
1697 }
1698
1699 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1700 {
1701         int rc;
1702         ENTRY;
1703
1704         /* Requests arrive in sender's byte order.  The ptlrpc service
1705          * handler has already checked and, if necessary, byte-swapped the
1706          * incoming request message body, but I am responsible for the
1707          * message buffers. */
1708
1709         if (req->rq_export == NULL) {
1710                 struct ldlm_request *dlm_req;
1711
1712                 CERROR("operation %d from %s with bad export cookie "LPU64"\n",
1713                        lustre_msg_get_opc(req->rq_reqmsg),
1714                        libcfs_id2str(req->rq_peer),
1715                        lustre_msg_get_handle(req->rq_reqmsg)->cookie);
1716
1717                 dlm_req = lustre_swab_reqbuf(req, DLM_LOCKREQ_OFF,
1718                                              sizeof(*dlm_req),
1719                                              lustre_swab_ldlm_request);
1720                 if (dlm_req != NULL)
1721                         ldlm_lock_dump_handle(D_ERROR,
1722                                               &dlm_req->lock_handle[0]);
1723                 ldlm_callback_reply(req, -ENOTCONN);
1724                 RETURN(0);
1725         }
1726
1727         switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1728
1729         /* XXX FIXME move this back to mds/handler.c, bug 249 */
1730         case LDLM_CANCEL:
1731                 CDEBUG(D_INODE, "cancel\n");
1732                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
1733                         RETURN(0);
1734                 rc = ldlm_handle_cancel(req);
1735                 if (rc)
1736                         break;
1737                 RETURN(0);
1738         case OBD_LOG_CANCEL:
1739                 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1740                         RETURN(0);
1741                 rc = llog_origin_handle_cancel(req);
1742                 ldlm_callback_reply(req, rc);
1743                 RETURN(0);
1744         default:
1745                 CERROR("invalid opcode %d\n",
1746                        lustre_msg_get_opc(req->rq_reqmsg));
1747                 ldlm_callback_reply(req, -EINVAL);
1748         }
1749
1750         RETURN(0);
1751 }
1752
1753 void ldlm_revoke_export_locks(struct obd_export *exp)
1754 {
1755         struct list_head *locklist = &exp->exp_ldlm_data.led_held_locks;
1756         struct list_head  rpc_list;
1757         struct ldlm_lock *lock, *next;
1758         struct ldlm_lock_desc desc;
1759
1760         ENTRY;
1761         INIT_LIST_HEAD(&rpc_list);
1762
1763         spin_lock(&exp->exp_ldlm_data.led_lock);
1764         list_for_each_entry_safe(lock, next, locklist, l_export_chain) {
1765                 lock_res_and_lock(lock);
1766
1767                 if (lock->l_req_mode != lock->l_granted_mode) {
1768                         unlock_res_and_lock(lock);
1769                         continue;
1770                 }
1771
1772                 LASSERT(lock->l_resource);
1773                 if (lock->l_resource->lr_type != LDLM_IBITS &&
1774                     lock->l_resource->lr_type != LDLM_PLAIN) {
1775                         unlock_res_and_lock(lock);
1776                         continue;
1777                 }
1778
1779                 if (lock->l_flags & LDLM_FL_AST_SENT) {
1780                         unlock_res_and_lock(lock);
1781                         continue;
1782                 }
1783
1784                 LASSERT(lock->l_blocking_ast);
1785                 LASSERT(!lock->l_blocking_lock);
1786
1787                 lock->l_flags |= LDLM_FL_AST_SENT;
1788                 list_move(&lock->l_export_chain, &rpc_list);
1789
1790                 unlock_res_and_lock(lock);
1791         }
1792         spin_unlock(&exp->exp_ldlm_data.led_lock);
1793
1794         while (!list_empty(&rpc_list)) {
1795                 lock = list_entry(rpc_list.next, struct ldlm_lock,
1796                                   l_export_chain);
1797                 list_del_init(&lock->l_export_chain);
1798
1799                 /* the desc just pretend to exclusive */
1800                 ldlm_lock2desc(lock, &desc);
1801                 desc.l_req_mode = LCK_EX;
1802                 desc.l_granted_mode = 0;
1803
1804                 LDLM_LOCK_GET(lock);
1805                 lock->l_blocking_ast(lock, &desc, lock->l_ast_data,
1806                                      LDLM_CB_BLOCKING);
1807                 LDLM_LOCK_PUT(lock);
1808         }
1809         EXIT;
1810 }
1811
1812 #ifdef __KERNEL__
1813 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1814 {
1815         struct ldlm_bl_work_item *blwi = NULL;
1816         static unsigned int num_bl = 0;
1817
1818         spin_lock(&blp->blp_lock);
1819         /* process a request from the blp_list at least every blp_num_threads */
1820         if (!list_empty(&blp->blp_list) &&
1821             (list_empty(&blp->blp_prio_list) || num_bl == 0))
1822                 blwi = list_entry(blp->blp_list.next,
1823                                   struct ldlm_bl_work_item, blwi_entry);
1824         else
1825                 if (!list_empty(&blp->blp_prio_list))
1826                         blwi = list_entry(blp->blp_prio_list.next,
1827                                           struct ldlm_bl_work_item, blwi_entry);
1828
1829         if (blwi) {
1830                 if (++num_bl >= atomic_read(&blp->blp_num_threads))
1831                         num_bl = 0;
1832                 list_del(&blwi->blwi_entry);
1833         }
1834         spin_unlock(&blp->blp_lock);
1835
1836         return blwi;
1837 }
1838
1839 /* This only contains temporary data until the thread starts */
1840 struct ldlm_bl_thread_data {
1841         char                    bltd_name[CFS_CURPROC_COMM_MAX];
1842         struct ldlm_bl_pool     *bltd_blp;
1843         struct completion       bltd_comp;
1844         int                     bltd_num;
1845 };
1846
1847 static int ldlm_bl_thread_main(void *arg);
1848
1849 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
1850 {
1851         struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
1852         int rc;
1853
1854         init_completion(&bltd.bltd_comp);
1855         rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1856         if (rc < 0) {
1857                 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
1858                        atomic_read(&blp->blp_num_threads), rc);
1859                 return rc;
1860         }
1861         wait_for_completion(&bltd.bltd_comp);
1862
1863         return 0;
1864 }
1865
1866 static int ldlm_bl_thread_main(void *arg)
1867 {
1868         struct ldlm_bl_pool *blp;
1869         ENTRY;
1870
1871         {
1872                 struct ldlm_bl_thread_data *bltd = arg;
1873
1874                 blp = bltd->bltd_blp;
1875
1876                 bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
1877                 atomic_inc(&blp->blp_busy_threads);
1878
1879                 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
1880                         "ldlm_bl_%02d", bltd->bltd_num);
1881                 cfs_daemonize(bltd->bltd_name);
1882
1883                 complete(&bltd->bltd_comp);
1884                 /* cannot use bltd after this, it is only on caller's stack */
1885         }
1886
1887         while (1) {
1888                 struct l_wait_info lwi = { 0 };
1889                 struct ldlm_bl_work_item *blwi = NULL;
1890
1891                 blwi = ldlm_bl_get_work(blp);
1892
1893                 if (blwi == NULL) {
1894                         int busy;
1895
1896                         atomic_dec(&blp->blp_busy_threads);
1897                         l_wait_event_exclusive(blp->blp_waitq,
1898                                          (blwi = ldlm_bl_get_work(blp)) != NULL,
1899                                          &lwi);
1900                         busy = atomic_inc_return(&blp->blp_busy_threads);
1901
1902                         if (blwi->blwi_ns == NULL)
1903                                 /* added by ldlm_cleanup() */
1904                                 break;
1905
1906                         /* Not fatal if racy and have a few too many threads */
1907                         if (unlikely(busy < blp->blp_max_threads &&
1908                                     busy >= atomic_read(&blp->blp_num_threads)))
1909                                 /* discard the return value, we tried */
1910                                 ldlm_bl_thread_start(blp);
1911                 } else {
1912                         if (blwi->blwi_ns == NULL)
1913                                 /* added by ldlm_cleanup() */
1914                                 break;
1915                 }
1916
1917                 if (blwi->blwi_count) {
1918                         /* The special case when we cancel locks in lru
1919                          * asynchronously, we pass the list of locks here.
1920                          * Thus lock is marked LDLM_FL_CANCELING, and already
1921                          * canceled locally. */
1922                         ldlm_cli_cancel_list(&blwi->blwi_head,
1923                                              blwi->blwi_count, NULL, 0, 0);
1924                 } else {
1925                         ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1926                                                 blwi->blwi_lock);
1927                 }
1928                 OBD_FREE(blwi, sizeof(*blwi));
1929         }
1930
1931         atomic_dec(&blp->blp_busy_threads);
1932         atomic_dec(&blp->blp_num_threads);
1933         complete(&blp->blp_comp);
1934         RETURN(0);
1935 }
1936
1937 #endif
1938
1939 static int ldlm_setup(void);
1940 static int ldlm_cleanup(int force);
1941
1942 int ldlm_get_ref(void)
1943 {
1944         int rc = 0;
1945         ENTRY;
1946         mutex_down(&ldlm_ref_sem);
1947         if (++ldlm_refcount == 1) {
1948                 rc = ldlm_setup();
1949                 if (rc)
1950                         ldlm_refcount--;
1951         }
1952         mutex_up(&ldlm_ref_sem);
1953
1954         RETURN(rc);
1955 }
1956
1957 void ldlm_put_ref(int force)
1958 {
1959         ENTRY;
1960         mutex_down(&ldlm_ref_sem);
1961         if (ldlm_refcount == 1) {
1962                 int rc = ldlm_cleanup(force);
1963                 if (rc)
1964                         CERROR("ldlm_cleanup failed: %d\n", rc);
1965                 else
1966                         ldlm_refcount--;
1967         } else {
1968                 ldlm_refcount--;
1969         }
1970         mutex_up(&ldlm_ref_sem);
1971
1972         EXIT;
1973 }
1974
1975 static int ldlm_setup(void)
1976 {
1977         struct ldlm_bl_pool *blp;
1978         int rc = 0;
1979         int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
1980         int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
1981 #ifdef __KERNEL__
1982         int i;
1983 #endif
1984         ENTRY;
1985
1986         if (ldlm_state != NULL)
1987                 RETURN(-EALREADY);
1988
1989         OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
1990         if (ldlm_state == NULL)
1991                 RETURN(-ENOMEM);
1992
1993 #ifdef LPROCFS
1994         rc = ldlm_proc_setup();
1995         if (rc != 0)
1996                 GOTO(out_free, rc);
1997 #endif
1998
1999 #ifdef __KERNEL__
2000         if (ldlm_num_threads) {
2001                 /* If ldlm_num_threads is set, it is the min and the max. */
2002                 if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
2003                         ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
2004                 if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
2005                         ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
2006                 ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
2007         }
2008 #endif
2009
2010         ldlm_state->ldlm_cb_service =
2011                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2012                                 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
2013                                 LDLM_CB_REPLY_PORTAL, ldlm_timeout * 900,
2014                                 ldlm_callback_handler, "ldlm_cbd",
2015                                 ldlm_svc_proc_dir, NULL,
2016                                 ldlm_min_threads, ldlm_max_threads,
2017                                 "ldlm_cb",
2018                                 LCT_MD_THREAD|LCT_DT_THREAD);
2019
2020         if (!ldlm_state->ldlm_cb_service) {
2021                 CERROR("failed to start service\n");
2022                 GOTO(out_proc, rc = -ENOMEM);
2023         }
2024
2025         ldlm_state->ldlm_cancel_service =
2026                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
2027                                 LDLM_MAXREPSIZE, LDLM_CANCEL_REQUEST_PORTAL,
2028                                 LDLM_CANCEL_REPLY_PORTAL, ldlm_timeout * 6000,
2029                                 ldlm_cancel_handler, "ldlm_canceld",
2030                                 ldlm_svc_proc_dir, NULL,
2031                                 ldlm_min_threads, ldlm_max_threads,
2032                                 "ldlm_cn",
2033                                 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD);
2034
2035         if (!ldlm_state->ldlm_cancel_service) {
2036                 CERROR("failed to start service\n");
2037                 GOTO(out_proc, rc = -ENOMEM);
2038         }
2039
2040         OBD_ALLOC(blp, sizeof(*blp));
2041         if (blp == NULL)
2042                 GOTO(out_proc, rc = -ENOMEM);
2043         ldlm_state->ldlm_bl_pool = blp;
2044
2045         spin_lock_init(&blp->blp_lock);
2046         CFS_INIT_LIST_HEAD(&blp->blp_list);
2047         CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2048         cfs_waitq_init(&blp->blp_waitq);
2049         atomic_set(&blp->blp_num_threads, 0);
2050         atomic_set(&blp->blp_busy_threads, 0);
2051         blp->blp_min_threads = ldlm_min_threads;
2052         blp->blp_max_threads = ldlm_max_threads;
2053
2054 #ifdef __KERNEL__
2055         for (i = 0; i < blp->blp_min_threads; i++) {
2056                 rc = ldlm_bl_thread_start(blp);
2057                 if (rc < 0)
2058                         GOTO(out_thread, rc);
2059         }
2060
2061         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
2062         if (rc)
2063                 GOTO(out_thread, rc);
2064
2065         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cb_service);
2066         if (rc)
2067                 GOTO(out_thread, rc);
2068
2069         CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2070         expired_lock_thread.elt_state = ELT_STOPPED;
2071         cfs_waitq_init(&expired_lock_thread.elt_waitq);
2072
2073         CFS_INIT_LIST_HEAD(&waiting_locks_list);
2074         spin_lock_init(&waiting_locks_spinlock);
2075         cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2076
2077         rc = cfs_kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FILES);
2078         if (rc < 0) {
2079                 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2080                 GOTO(out_thread, rc);
2081         }
2082
2083         wait_event(expired_lock_thread.elt_waitq,
2084                    expired_lock_thread.elt_state == ELT_READY);
2085 #endif
2086
2087 #ifdef __KERNEL__
2088         rc = ldlm_pools_init();
2089         if (rc)
2090                 GOTO(out_thread, rc);
2091 #endif
2092         RETURN(0);
2093
2094 #ifdef __KERNEL__
2095  out_thread:
2096         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2097         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2098 #endif
2099
2100  out_proc:
2101 #ifdef LPROCFS
2102         ldlm_proc_cleanup();
2103  out_free:
2104 #endif
2105         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2106         ldlm_state = NULL;
2107         return rc;
2108 }
2109
2110 static int ldlm_cleanup(int force)
2111 {
2112 #ifdef __KERNEL__
2113         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2114 #endif
2115         ENTRY;
2116
2117         if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) || 
2118             !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2119                 CERROR("ldlm still has namespaces; clean these up first.\n");
2120                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2121                 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2122                 RETURN(-EBUSY);
2123         }
2124
2125 #ifdef __KERNEL__
2126         ldlm_pools_fini();
2127 #endif
2128
2129 #ifdef __KERNEL__
2130         while (atomic_read(&blp->blp_num_threads) > 0) {
2131                 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2132
2133                 init_completion(&blp->blp_comp);
2134
2135                 spin_lock(&blp->blp_lock);
2136                 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2137                 cfs_waitq_signal(&blp->blp_waitq);
2138                 spin_unlock(&blp->blp_lock);
2139
2140                 wait_for_completion(&blp->blp_comp);
2141         }
2142         OBD_FREE(blp, sizeof(*blp));
2143
2144         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2145         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2146         ldlm_proc_cleanup();
2147
2148         expired_lock_thread.elt_state = ELT_TERMINATE;
2149         cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2150         wait_event(expired_lock_thread.elt_waitq,
2151                    expired_lock_thread.elt_state == ELT_STOPPED);
2152 #else
2153         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2154         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2155 #endif
2156
2157         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2158         ldlm_state = NULL;
2159
2160         RETURN(0);
2161 }
2162
2163 int __init ldlm_init(void)
2164 {
2165         init_mutex(&ldlm_ref_sem);
2166         init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2167         init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2168         ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2169                                                sizeof(struct ldlm_resource), 0,
2170                                                SLAB_HWCACHE_ALIGN);
2171         if (ldlm_resource_slab == NULL)
2172                 return -ENOMEM;
2173
2174         ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2175                                            sizeof(struct ldlm_lock), 0,
2176                                            SLAB_HWCACHE_ALIGN);
2177         if (ldlm_lock_slab == NULL) {
2178                 cfs_mem_cache_destroy(ldlm_resource_slab);
2179                 return -ENOMEM;
2180         }
2181
2182         return 0;
2183 }
2184
2185 void __exit ldlm_exit(void)
2186 {
2187         int rc;
2188         if (ldlm_refcount)
2189                 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2190         rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2191         LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2192         rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2193         LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2194 }
2195
2196 /* ldlm_extent.c */
2197 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2198
2199 /* ldlm_lock.c */
2200 EXPORT_SYMBOL(ldlm_get_processing_policy);
2201 EXPORT_SYMBOL(ldlm_lock2desc);
2202 EXPORT_SYMBOL(ldlm_register_intent);
2203 EXPORT_SYMBOL(ldlm_lockname);
2204 EXPORT_SYMBOL(ldlm_typename);
2205 EXPORT_SYMBOL(ldlm_lock2handle);
2206 EXPORT_SYMBOL(__ldlm_handle2lock);
2207 EXPORT_SYMBOL(ldlm_lock_get);
2208 EXPORT_SYMBOL(ldlm_lock_put);
2209 EXPORT_SYMBOL(ldlm_lock_match);
2210 EXPORT_SYMBOL(ldlm_lock_cancel);
2211 EXPORT_SYMBOL(ldlm_lock_addref);
2212 EXPORT_SYMBOL(ldlm_lock_decref);
2213 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2214 EXPORT_SYMBOL(ldlm_lock_change_resource);
2215 EXPORT_SYMBOL(ldlm_lock_set_data);
2216 EXPORT_SYMBOL(ldlm_it2str);
2217 EXPORT_SYMBOL(ldlm_lock_dump);
2218 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2219 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
2220 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2221 EXPORT_SYMBOL(ldlm_lock_allow_match);
2222
2223 /* ldlm_request.c */
2224 EXPORT_SYMBOL(ldlm_completion_ast);
2225 EXPORT_SYMBOL(ldlm_blocking_ast);
2226 EXPORT_SYMBOL(ldlm_glimpse_ast);
2227 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2228 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2229 EXPORT_SYMBOL(ldlm_prep_elc_req);
2230 EXPORT_SYMBOL(ldlm_cli_convert);
2231 EXPORT_SYMBOL(ldlm_cli_enqueue);
2232 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2233 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2234 EXPORT_SYMBOL(ldlm_cli_cancel);
2235 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2236 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2237 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2238 EXPORT_SYMBOL(ldlm_cli_join_lru);
2239 EXPORT_SYMBOL(ldlm_replay_locks);
2240 EXPORT_SYMBOL(ldlm_resource_foreach);
2241 EXPORT_SYMBOL(ldlm_namespace_foreach);
2242 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
2243 EXPORT_SYMBOL(ldlm_resource_iterate);
2244 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2245 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2246
2247 /* ldlm_lockd.c */
2248 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2249 EXPORT_SYMBOL(ldlm_server_completion_ast);
2250 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2251 EXPORT_SYMBOL(ldlm_handle_enqueue);
2252 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2253 EXPORT_SYMBOL(ldlm_handle_cancel);
2254 EXPORT_SYMBOL(ldlm_request_cancel);
2255 EXPORT_SYMBOL(ldlm_handle_convert);
2256 EXPORT_SYMBOL(ldlm_handle_convert0);
2257 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2258 EXPORT_SYMBOL(ldlm_get_ref);
2259 EXPORT_SYMBOL(ldlm_put_ref);
2260 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2261 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2262
2263 /* ldlm_resource.c */
2264 EXPORT_SYMBOL(ldlm_namespace_new);
2265 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2266 EXPORT_SYMBOL(ldlm_namespace_free);
2267 EXPORT_SYMBOL(ldlm_namespace_dump);
2268 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2269 EXPORT_SYMBOL(ldlm_resource_get);
2270 EXPORT_SYMBOL(ldlm_resource_putref);
2271 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2272
2273 /* ldlm_lib.c */
2274 EXPORT_SYMBOL(client_import_add_conn);
2275 EXPORT_SYMBOL(client_import_del_conn);
2276 EXPORT_SYMBOL(client_obd_setup);
2277 EXPORT_SYMBOL(client_obd_cleanup);
2278 EXPORT_SYMBOL(client_connect_import);
2279 EXPORT_SYMBOL(client_disconnect_export);
2280 EXPORT_SYMBOL(target_start_recovery_thread);
2281 EXPORT_SYMBOL(target_stop_recovery_thread);
2282 EXPORT_SYMBOL(target_handle_connect);
2283 EXPORT_SYMBOL(target_cleanup_recovery);
2284 EXPORT_SYMBOL(target_destroy_export);
2285 EXPORT_SYMBOL(target_cancel_recovery_timer);
2286 EXPORT_SYMBOL(target_send_reply);
2287 EXPORT_SYMBOL(target_queue_recovery_request);
2288 EXPORT_SYMBOL(target_handle_ping);
2289 EXPORT_SYMBOL(target_pack_pool_reply);
2290 EXPORT_SYMBOL(target_handle_disconnect);
2291
2292 /* l_lock.c */
2293 EXPORT_SYMBOL(lock_res_and_lock);
2294 EXPORT_SYMBOL(unlock_res_and_lock);
2295