Whamcloud - gitweb
land b_ost_amd onto HEAD.
[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, 2003 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 Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #ifndef EXPORT_SYMTAB
25 # define EXPORT_SYMTAB
26 #endif
27 #define DEBUG_SUBSYSTEM S_LDLM
28
29 #ifdef __KERNEL__
30 # include <linux/module.h>
31 # include <linux/slab.h>
32 # include <linux/init.h>
33 # include <linux/wait.h>
34 #else
35 # include <liblustre.h>
36 #endif
37
38 #include <linux/lustre_dlm.h>
39 #include <linux/obd_class.h>
40 #include <portals/list.h>
41 #include "ldlm_internal.h"
42
43 extern kmem_cache_t *ldlm_resource_slab;
44 extern kmem_cache_t *ldlm_lock_slab;
45 extern struct lustre_lock ldlm_handle_lock;
46 extern struct list_head ldlm_namespace_list;
47 extern int (*mds_reint_p)(int offset, struct ptlrpc_request *req);
48 extern int (*mds_getattr_name_p)(int offset, struct ptlrpc_request *req);
49
50 static DECLARE_MUTEX(ldlm_ref_sem);
51 static int ldlm_refcount = 0;
52
53 /* LDLM state */
54
55 static struct ldlm_state *ldlm_state;
56
57 inline unsigned long round_timeout(unsigned long timeout)
58 {
59         return ((timeout / HZ) + 1) * HZ;
60 }
61
62 #ifdef __KERNEL__
63 /* XXX should this be per-ldlm? */
64 static struct list_head waiting_locks_list;
65 static spinlock_t waiting_locks_spinlock;
66 static struct timer_list waiting_locks_timer;
67
68 static struct expired_lock_thread {
69         wait_queue_head_t         elt_waitq;
70         int                       elt_state;
71         struct list_head          elt_expired_locks;
72         spinlock_t                elt_lock;
73 } expired_lock_thread;
74 #endif
75
76 #define ELT_STOPPED   0
77 #define ELT_READY     1
78 #define ELT_TERMINATE 2
79
80 struct ldlm_bl_pool {
81         spinlock_t              blp_lock;
82         struct list_head        blp_list;
83         wait_queue_head_t       blp_waitq;
84         atomic_t                blp_num_threads;
85         struct completion       blp_comp;
86 };
87
88 struct ldlm_bl_work_item {
89         struct list_head        blwi_entry;
90         struct ldlm_namespace   *blwi_ns;
91         struct ldlm_lock_desc   blwi_ld;
92         struct ldlm_lock        *blwi_lock;
93 };
94
95 #ifdef __KERNEL__
96
97 static inline int have_expired_locks(void)
98 {
99         int need_to_run;
100
101         spin_lock_bh(&expired_lock_thread.elt_lock);
102         need_to_run = !list_empty(&expired_lock_thread.elt_expired_locks);
103         spin_unlock_bh(&expired_lock_thread.elt_lock);
104
105         RETURN(need_to_run);
106 }
107
108 static int expired_lock_main(void *arg)
109 {
110         struct list_head *expired = &expired_lock_thread.elt_expired_locks;
111         struct l_wait_info lwi = { 0 };
112         unsigned long flags;
113
114         ENTRY;
115         lock_kernel();
116         kportal_daemonize("ldlm_elt");
117
118         SIGNAL_MASK_LOCK(current, flags);
119         sigfillset(&current->blocked);
120         RECALC_SIGPENDING;
121         SIGNAL_MASK_UNLOCK(current, flags);
122
123         unlock_kernel();
124
125         expired_lock_thread.elt_state = ELT_READY;
126         wake_up(&expired_lock_thread.elt_waitq);
127
128         while (1) {
129                 l_wait_event(expired_lock_thread.elt_waitq,
130                              have_expired_locks() ||
131                              expired_lock_thread.elt_state == ELT_TERMINATE,
132                              &lwi);
133
134                 spin_lock_bh(&expired_lock_thread.elt_lock);
135                 while (!list_empty(expired)) {
136                         struct obd_export *export;
137                         struct ldlm_lock *lock;
138
139                         lock = list_entry(expired->next, struct ldlm_lock,
140                                           l_pending_chain);
141                         if ((void *)lock < LP_POISON + PAGE_SIZE &&
142                             (void *)lock >= LP_POISON) {
143                                 CERROR("free lock on elt list %p\n", lock);
144                                 LBUG();
145                         }
146                         list_del_init(&lock->l_pending_chain);
147                         if ((void *)lock->l_export < LP_POISON + PAGE_SIZE &&
148                             (void *)lock->l_export >= LP_POISON + PAGE_SIZE) {
149                                 CERROR("lock with free export on elt list %p\n",
150                                        export);
151                                 lock->l_export = NULL;
152                                 LDLM_ERROR(lock, "free export\n");
153                                 continue;
154                         }
155                         export = class_export_get(lock->l_export);
156                         spin_unlock_bh(&expired_lock_thread.elt_lock);
157
158                         ptlrpc_fail_export(export);
159                         class_export_put(export);
160                         spin_lock_bh(&expired_lock_thread.elt_lock);
161                 }
162                 spin_unlock_bh(&expired_lock_thread.elt_lock);
163
164                 if (expired_lock_thread.elt_state == ELT_TERMINATE)
165                         break;
166         }
167
168         expired_lock_thread.elt_state = ELT_STOPPED;
169         wake_up(&expired_lock_thread.elt_waitq);
170         RETURN(0);
171 }
172
173 static void waiting_locks_callback(unsigned long unused)
174 {
175         struct ldlm_lock *lock;
176         char str[PTL_NALFMT_SIZE];
177
178         spin_lock_bh(&waiting_locks_spinlock);
179         while (!list_empty(&waiting_locks_list)) {
180                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
181                                   l_pending_chain);
182
183                 if ((lock->l_callback_timeout > jiffies) ||
184                     (lock->l_req_mode == LCK_GROUP))
185                         break;
186
187                 LDLM_ERROR(lock, "lock callback timer expired: evicting client "
188                            "%s@%s nid %s ",
189                            lock->l_export->exp_client_uuid.uuid,
190                            lock->l_export->exp_connection->c_remote_uuid.uuid,
191                            ptlrpc_peernid2str(&lock->l_export->exp_connection->c_peer, str));
192
193                 spin_lock_bh(&expired_lock_thread.elt_lock);
194                 list_del(&lock->l_pending_chain);
195                 list_add(&lock->l_pending_chain,
196                          &expired_lock_thread.elt_expired_locks);
197                 spin_unlock_bh(&expired_lock_thread.elt_lock);
198                 wake_up(&expired_lock_thread.elt_waitq);
199         }
200
201         /*
202          * Make sure the timer will fire again if we have any locks
203          * left.
204          */
205         if (!list_empty(&waiting_locks_list)) {
206                 unsigned long timeout_rounded;
207                 lock = list_entry(waiting_locks_list.next, struct ldlm_lock,
208                                   l_pending_chain);
209                 timeout_rounded = round_timeout(lock->l_callback_timeout);
210                 mod_timer(&waiting_locks_timer, timeout_rounded);
211         }
212         spin_unlock_bh(&waiting_locks_spinlock);
213 }
214
215 /*
216  * Indicate that we're waiting for a client to call us back cancelling a given
217  * lock.  We add it to the pending-callback chain, and schedule the lock-timeout
218  * timer to fire appropriately.  (We round up to the next second, to avoid
219  * floods of timer firings during periods of high lock contention and traffic).
220  */
221 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
222 {
223         unsigned long timeout_rounded;
224
225         spin_lock_bh(&waiting_locks_spinlock);
226         if (!list_empty(&lock->l_pending_chain)) {
227                 LDLM_DEBUG(lock, "not re-adding to wait list");
228                 spin_unlock_bh(&waiting_locks_spinlock);
229                 return 0;
230         }
231         LDLM_DEBUG(lock, "adding to wait list");
232
233         lock->l_callback_timeout = jiffies + (obd_timeout * HZ / 2);
234
235         timeout_rounded = round_timeout(lock->l_callback_timeout);
236
237         if (timeout_rounded < waiting_locks_timer.expires ||
238             !timer_pending(&waiting_locks_timer)) {
239                 mod_timer(&waiting_locks_timer, timeout_rounded);
240         }
241         list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
242         spin_unlock_bh(&waiting_locks_spinlock);
243         return 1;
244 }
245
246 /*
247  * Remove a lock from the pending list, likely because it had its cancellation
248  * callback arrive without incident.  This adjusts the lock-timeout timer if
249  * needed.  Returns 0 if the lock wasn't pending after all, 1 if it was.
250  */
251 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
252 {
253         struct list_head *list_next;
254
255         if (lock->l_export == NULL) {
256                 /* We don't have a "waiting locks list" on clients. */
257                 LDLM_DEBUG(lock, "client lock: no-op");
258                 return 0;
259         }
260
261         spin_lock_bh(&waiting_locks_spinlock);
262
263         if (list_empty(&lock->l_pending_chain)) {
264                 spin_unlock_bh(&waiting_locks_spinlock);
265                 LDLM_DEBUG(lock, "wasn't waiting");
266                 return 0;
267         }
268
269         list_next = lock->l_pending_chain.next;
270         if (lock->l_pending_chain.prev == &waiting_locks_list) {
271                 /* Removing the head of the list, adjust timer. */
272                 if (list_next == &waiting_locks_list) {
273                         /* No more, just cancel. */
274                         del_timer(&waiting_locks_timer);
275                 } else {
276                         struct ldlm_lock *next;
277                         next = list_entry(list_next, struct ldlm_lock,
278                                           l_pending_chain);
279                         mod_timer(&waiting_locks_timer,
280                                   round_timeout(next->l_callback_timeout));
281                 }
282         }
283
284         spin_lock_bh(&expired_lock_thread.elt_lock);
285         list_del_init(&lock->l_pending_chain);
286         spin_unlock_bh(&expired_lock_thread.elt_lock);
287
288         spin_unlock_bh(&waiting_locks_spinlock);
289         LDLM_DEBUG(lock, "removed");
290         return 1;
291 }
292
293 #else /* !__KERNEL__ */
294
295 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
296 {
297         RETURN(1);
298 }
299
300 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
301 {
302         RETURN(0);
303 }
304
305 #endif /* __KERNEL__ */
306
307 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,const char *ast_type)
308 {
309         struct ptlrpc_connection *conn = lock->l_export->exp_connection;
310         char str[PTL_NALFMT_SIZE];
311
312         LDLM_ERROR(lock, "%s AST failed (%d): evicting client %s@%s NID "LPX64
313                    " (%s)", ast_type, rc, lock->l_export->exp_client_uuid.uuid,
314                    conn->c_remote_uuid.uuid, conn->c_peer.peer_nid,
315                    ptlrpc_peernid2str(&conn->c_peer, str));
316         ptlrpc_fail_export(lock->l_export);
317 }
318
319 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
320                                  struct ptlrpc_request *req, int rc,
321                                  const char *ast_type)
322 {
323         struct ptlrpc_peer *peer = &req->rq_import->imp_connection->c_peer;
324         char str[PTL_NALFMT_SIZE];
325
326         if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
327                 LASSERT(lock->l_export);
328                 if (lock->l_export->exp_libclient) {
329                         LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
330                                    " timeout, just cancelling lock", ast_type,
331                                    ptlrpc_peernid2str(peer, str));
332                         ldlm_lock_cancel(lock);
333                         rc = -ERESTART;
334                 } else {
335                         ldlm_del_waiting_lock(lock);
336                         ldlm_failed_ast(lock, rc, ast_type);
337                 }
338         } else if (rc) {
339                 if (rc == -EINVAL)
340                         LDLM_DEBUG(lock, "client (nid %s) returned %d"
341                                    " from %s AST - normal race",
342                                    ptlrpc_peernid2str(peer, str),
343                                    req->rq_repmsg->status, ast_type);
344                 else
345                         LDLM_ERROR(lock, "client (nid %s) returned %d "
346                                    "from %s AST", ptlrpc_peernid2str(peer, str),
347                                    (req->rq_repmsg != NULL) ?
348                                    req->rq_repmsg->status : 0, ast_type);
349                 ldlm_lock_cancel(lock);
350                 /* Server-side AST functions are called from ldlm_reprocess_all,
351                  * which needs to be told to please restart its reprocessing. */
352                 rc = -ERESTART;
353         }
354
355         return rc;
356 }
357
358 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
359                              struct ldlm_lock_desc *desc,
360                              void *data, int flag)
361 {
362         struct ldlm_request *body;
363         struct ptlrpc_request *req;
364         int rc = 0, size = sizeof(*body);
365         ENTRY;
366
367         if (flag == LDLM_CB_CANCELING) {
368                 /* Don't need to do anything here. */
369                 RETURN(0);
370         }
371
372         LASSERT(lock);
373
374         l_lock(&lock->l_resource->lr_namespace->ns_lock);
375         if (lock->l_granted_mode != lock->l_req_mode) {
376                 /* this blocking AST will be communicated as part of the
377                  * completion AST instead */
378                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
379                 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");                 RETURN(0);
380         }
381
382         if (lock->l_destroyed) {
383                 /* What's the point? */
384                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
385                 RETURN(0);
386         }
387
388 #if 0
389         if (LTIME_S(CURRENT_TIME) - lock->l_export->exp_last_request_time > 30){
390                 ldlm_failed_ast(lock, -ETIMEDOUT, "Not-attempted blocking");
391                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
392                 RETURN(-ETIMEDOUT);
393         }
394 #endif
395
396         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
397                               LDLM_BL_CALLBACK, 1, &size, NULL);
398         if (req == NULL) {
399                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
400                 RETURN(-ENOMEM);
401         }
402
403         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
404         memcpy(&body->lock_handle1, &lock->l_remote_handle,
405                sizeof(body->lock_handle1));
406         memcpy(&body->lock_desc, desc, sizeof(*desc));
407         body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
408
409         LDLM_DEBUG(lock, "server preparing blocking AST");
410         req->rq_replen = lustre_msg_size(0, NULL);
411
412         if (lock->l_granted_mode == lock->l_req_mode)
413                 ldlm_add_waiting_lock(lock);
414         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
415
416         req->rq_send_state = LUSTRE_IMP_FULL;
417         req->rq_timeout = 2; /* 2 second timeout for initial AST reply */
418         rc = ptlrpc_queue_wait(req);
419         if (rc != 0)
420                 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
421
422         ptlrpc_req_finished(req);
423
424         RETURN(rc);
425 }
426
427 /* XXX copied from ptlrpc/service.c */
428 static long timeval_sub(struct timeval *large, struct timeval *small)
429 {
430         return (large->tv_sec - small->tv_sec) * 1000000 +
431                 (large->tv_usec - small->tv_usec);
432 }
433
434 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
435 {
436         struct ldlm_request *body;
437         struct ptlrpc_request *req;
438         struct timeval granted_time;
439         long total_enqueue_wait;
440         int rc = 0, size[2] = {sizeof(*body)}, buffers = 1;
441         ENTRY;
442
443         LASSERT(lock != NULL);
444
445         do_gettimeofday(&granted_time);
446         total_enqueue_wait = timeval_sub(&granted_time, &lock->l_enqueued_time);
447
448         if (total_enqueue_wait / 1000000 > obd_timeout)
449                 LDLM_ERROR(lock, "enqueue wait took %ldus", total_enqueue_wait);
450
451         if (lock->l_resource->lr_lvb_len) {
452                 buffers = 2;
453                 size[1] = lock->l_resource->lr_lvb_len;
454         }
455
456         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
457                               LDLM_CP_CALLBACK, buffers, size, NULL);
458         if (req == NULL)
459                 RETURN(-ENOMEM);
460
461         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
462         memcpy(&body->lock_handle1, &lock->l_remote_handle,
463                sizeof(body->lock_handle1));
464         body->lock_flags = flags;
465         ldlm_lock2desc(lock, &body->lock_desc);
466
467         if (buffers == 2) {
468                 void *lvb = lustre_msg_buf(req->rq_reqmsg, 1,
469                                            lock->l_resource->lr_lvb_len);
470                 memcpy(lvb, lock->l_resource->lr_lvb_data,
471                        lock->l_resource->lr_lvb_len);
472         }
473
474         LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
475                    total_enqueue_wait);
476         req->rq_replen = lustre_msg_size(0, NULL);
477
478         req->rq_send_state = LUSTRE_IMP_FULL;
479         req->rq_timeout = 2; /* 2 second timeout for initial AST reply */
480
481         /* We only send real blocking ASTs after the lock is granted */
482         l_lock(&lock->l_resource->lr_namespace->ns_lock);
483         if (lock->l_flags & LDLM_FL_AST_SENT) {
484                 body->lock_flags |= LDLM_FL_AST_SENT;
485                 ldlm_add_waiting_lock(lock); /* start the lock-timeout clock */
486         }
487         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
488
489         rc = ptlrpc_queue_wait(req);
490         if (rc != 0)
491                 rc = ldlm_handle_ast_error(lock, req, rc, "completion");
492
493         ptlrpc_req_finished(req);
494
495         RETURN(rc);
496 }
497
498 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
499 {
500         struct ldlm_resource *res = lock->l_resource;
501         struct ldlm_request *body;
502         struct ptlrpc_request *req;
503         int rc = 0, size = sizeof(*body);
504         ENTRY;
505
506         LASSERT(lock != NULL);
507
508         req = ptlrpc_prep_req(lock->l_export->exp_imp_reverse,
509                               LDLM_GL_CALLBACK, 1, &size, NULL);
510         if (req == NULL)
511                 RETURN(-ENOMEM);
512
513         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*body));
514         memcpy(&body->lock_handle1, &lock->l_remote_handle,
515                sizeof(body->lock_handle1));
516         ldlm_lock2desc(lock, &body->lock_desc);
517
518         size = lock->l_resource->lr_lvb_len;
519         req->rq_replen = lustre_msg_size(1, &size);
520
521         req->rq_send_state = LUSTRE_IMP_FULL;
522         req->rq_timeout = 2; /* 2 second timeout for initial AST reply */
523
524         rc = ptlrpc_queue_wait(req);
525         if (rc == -ELDLM_NO_LOCK_DATA)
526                 LDLM_DEBUG(lock, "lost race - client has a lock but no inode");
527         else if (rc != 0)
528                 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
529         else
530                 rc = res->lr_namespace->ns_lvbo->lvbo_update
531                         (res, req->rq_repmsg, 0, 1);
532         ptlrpc_req_finished(req);
533         RETURN(rc);
534 }
535
536 int ldlm_handle_enqueue(struct ptlrpc_request *req,
537                         ldlm_completion_callback completion_callback,
538                         ldlm_blocking_callback blocking_callback,
539                         ldlm_glimpse_callback glimpse_callback)
540 {
541         struct obd_device *obddev = req->rq_export->exp_obd;
542         struct ldlm_reply *dlm_rep;
543         struct ldlm_request *dlm_req;
544         int rc = 0, size[2] = {sizeof(*dlm_rep)};
545         __u32 flags;
546         ldlm_error_t err = ELDLM_OK;
547         struct ldlm_lock *lock = NULL;
548         void *cookie = NULL;
549         ENTRY;
550
551         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
552
553         dlm_req = lustre_swab_reqbuf (req, 0, sizeof (*dlm_req),
554                                       lustre_swab_ldlm_request);
555         if (dlm_req == NULL) {
556                 CERROR ("Can't unpack dlm_req\n");
557                 GOTO(out, rc = -EFAULT);
558         }
559
560         flags = dlm_req->lock_flags;
561
562         /* The lock's callback data might be set in the policy function */
563         lock = ldlm_lock_create(obddev->obd_namespace, &dlm_req->lock_handle2,
564                                 dlm_req->lock_desc.l_resource.lr_name,
565                                 dlm_req->lock_desc.l_resource.lr_type,
566                                 dlm_req->lock_desc.l_req_mode,
567                                 blocking_callback, completion_callback,
568                                 glimpse_callback, NULL, 0);
569         if (!lock)
570                 GOTO(out, rc = -ENOMEM);
571
572         do_gettimeofday(&lock->l_enqueued_time);
573         memcpy(&lock->l_remote_handle, &dlm_req->lock_handle1,
574                sizeof(lock->l_remote_handle));
575         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
576
577         LASSERT(req->rq_export);
578         lock->l_export = class_export_get(req->rq_export);
579         l_lock(&lock->l_resource->lr_namespace->ns_lock);
580         list_add(&lock->l_export_chain,
581                  &lock->l_export->exp_ldlm_data.led_held_locks);
582         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
583
584         if (flags & LDLM_FL_HAS_INTENT) {
585                 /* In this case, the reply buffer is allocated deep in
586                  * local_lock_enqueue by the policy function. */
587                 cookie = req;
588         } else {
589                 int buffers = 1;
590                 if (lock->l_resource->lr_lvb_len) {
591                         size[1] = lock->l_resource->lr_lvb_len;
592                         buffers = 2;
593                 }
594
595                 if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
596                         GOTO(out, rc = -ENOMEM);
597
598                 rc = lustre_pack_reply(req, buffers, size, NULL);
599                 if (rc)
600                         GOTO(out, rc);
601         }
602
603         if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
604                 memcpy(&lock->l_policy_data, &dlm_req->lock_desc.l_policy_data,
605                        sizeof(ldlm_policy_data_t));
606         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
607                 memcpy(&lock->l_req_extent, &lock->l_policy_data.l_extent,
608                        sizeof(lock->l_req_extent));
609
610         err = ldlm_lock_enqueue(obddev->obd_namespace, &lock, cookie, &flags);
611         if (err)
612                 GOTO(out, err);
613
614         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
615         dlm_rep->lock_flags = flags;
616
617         ldlm_lock2desc(lock, &dlm_rep->lock_desc);
618         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
619
620         /* We never send a blocking AST until the lock is granted, but
621          * we can tell it right now */
622         l_lock(&lock->l_resource->lr_namespace->ns_lock);
623         if (lock->l_flags & LDLM_FL_AST_SENT) {
624                 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
625                 if (lock->l_granted_mode == lock->l_req_mode)
626                         ldlm_add_waiting_lock(lock);
627         }
628         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
629
630         EXIT;
631  out:
632         req->rq_status = err;
633         if (req->rq_reply_state == NULL) {
634                 err = lustre_pack_reply(req, 0, NULL, NULL);
635                 if (rc == 0)
636                         rc = err;
637                 req->rq_status = rc;
638         }
639
640         /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
641          * ldlm_reprocess_all.  If this moves, revisit that code. -phil */
642         if (lock) {
643                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
644                            "(err=%d, rc=%d)", err, rc);
645
646                 if (lock->l_resource->lr_lvb_len > 0 && rc == 0) {
647                         void *lvb = lustre_msg_buf(req->rq_repmsg, 1,
648                                                   lock->l_resource->lr_lvb_len);
649                         LASSERT(lvb != NULL);
650                         memcpy(lvb, lock->l_resource->lr_lvb_data,
651                                lock->l_resource->lr_lvb_len);
652                 }
653
654                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
655                         ldlm_reprocess_all(lock->l_resource);
656                 LDLM_LOCK_PUT(lock);
657         }
658         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
659                           lock, rc);
660
661         return rc;
662 }
663
664 int ldlm_handle_convert(struct ptlrpc_request *req)
665 {
666         struct ldlm_request *dlm_req;
667         struct ldlm_reply *dlm_rep;
668         struct ldlm_lock *lock;
669         int rc, size = sizeof(*dlm_rep);
670         ENTRY;
671
672         dlm_req = lustre_swab_reqbuf (req, 0, sizeof (*dlm_req),
673                                       lustre_swab_ldlm_request);
674         if (dlm_req == NULL) {
675                 CERROR ("Can't unpack dlm_req\n");
676                 RETURN (-EFAULT);
677         }
678
679         rc = lustre_pack_reply(req, 1, &size, NULL);
680         if (rc) {
681                 CERROR("out of memory\n");
682                 RETURN(-ENOMEM);
683         }
684         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*dlm_rep));
685         dlm_rep->lock_flags = dlm_req->lock_flags;
686
687         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
688         if (!lock) {
689                 req->rq_status = EINVAL;
690         } else {
691                 LDLM_DEBUG(lock, "server-side convert handler START");
692                 ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
693                                   &dlm_rep->lock_flags);
694                 if (ldlm_del_waiting_lock(lock))
695                         CDEBUG(D_DLMTRACE, "converted waiting lock %p\n", lock);
696                 req->rq_status = 0;
697         }
698
699         if (lock) {
700                 ldlm_reprocess_all(lock->l_resource);
701                 LDLM_DEBUG(lock, "server-side convert handler END");
702                 LDLM_LOCK_PUT(lock);
703         } else
704                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
705
706         RETURN(0);
707 }
708
709 int ldlm_handle_cancel(struct ptlrpc_request *req)
710 {
711         struct ldlm_request *dlm_req;
712         struct ldlm_lock *lock;
713         struct ldlm_resource *res;
714         char str[PTL_NALFMT_SIZE];
715         int rc;
716         ENTRY;
717
718         dlm_req = lustre_swab_reqbuf(req, 0, sizeof (*dlm_req),
719                                       lustre_swab_ldlm_request);
720         if (dlm_req == NULL) {
721                 CERROR("bad request buffer for cancel\n");
722                 RETURN(-EFAULT);
723         }
724
725         rc = lustre_pack_reply(req, 0, NULL, NULL);
726         if (rc) {
727                 CERROR("out of memory\n");
728                 RETURN(-ENOMEM);
729         }
730
731         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
732         if (!lock) {
733                 CERROR("received cancel for unknown lock cookie "LPX64
734                        " from client %s nid %s\n",
735                        dlm_req->lock_handle1.cookie,
736                        req->rq_export->exp_client_uuid.uuid,
737                        ptlrpc_peernid2str(&req->rq_peer, str));
738                 LDLM_DEBUG_NOLOCK("server-side cancel handler stale lock "
739                                   "(cookie "LPU64")",
740                                   dlm_req->lock_handle1.cookie);
741                 req->rq_status = ESTALE;
742         } else {
743                 LDLM_DEBUG(lock, "server-side cancel handler START");
744                 res = lock->l_resource;
745                 if (res && res->lr_namespace->ns_lvbo &&
746                     res->lr_namespace->ns_lvbo->lvbo_update) {
747                         (void)res->lr_namespace->ns_lvbo->lvbo_update
748                                 (res, NULL, 0, 0);
749                                 //(res, req->rq_reqmsg, 1);
750                 }
751
752                 ldlm_lock_cancel(lock);
753                 if (ldlm_del_waiting_lock(lock))
754                         CDEBUG(D_DLMTRACE, "cancelled waiting lock %p\n", lock);
755                 req->rq_status = rc;
756         }
757
758         if (ptlrpc_reply(req) != 0)
759                 LBUG();
760
761         if (lock) {
762                 ldlm_reprocess_all(lock->l_resource);
763                 LDLM_DEBUG(lock, "server-side cancel handler END");
764                 LDLM_LOCK_PUT(lock);
765         }
766
767         RETURN(0);
768 }
769
770 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
771                              struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
772 {
773         int do_ast;
774         ENTRY;
775
776         l_lock(&ns->ns_lock);
777         LDLM_DEBUG(lock, "client blocking AST callback handler START");
778
779         lock->l_flags |= LDLM_FL_CBPENDING;
780         do_ast = (!lock->l_readers && !lock->l_writers);
781
782         if (do_ast) {
783                 LDLM_DEBUG(lock, "already unused, calling "
784                            "callback (%p)", lock->l_blocking_ast);
785                 if (lock->l_blocking_ast != NULL) {
786                         l_unlock(&ns->ns_lock);
787                         l_check_no_ns_lock(ns);
788                         lock->l_blocking_ast(lock, ld, lock->l_ast_data,
789                                              LDLM_CB_BLOCKING);
790                         l_lock(&ns->ns_lock);
791                 }
792         } else {
793                 LDLM_DEBUG(lock, "Lock still has references, will be"
794                            " cancelled later");
795         }
796
797         LDLM_DEBUG(lock, "client blocking callback handler END");
798         l_unlock(&ns->ns_lock);
799         LDLM_LOCK_PUT(lock);
800         EXIT;
801 }
802
803 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
804                                     struct ldlm_namespace *ns,
805                                     struct ldlm_request *dlm_req,
806                                     struct ldlm_lock *lock)
807 {
808         LIST_HEAD(ast_list);
809         ENTRY;
810
811         l_lock(&ns->ns_lock);
812         LDLM_DEBUG(lock, "client completion callback handler START");
813
814         /* If we receive the completion AST before the actual enqueue returned,
815          * then we might need to switch lock modes, resources, or extents. */
816         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
817                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
818                 LDLM_DEBUG(lock, "completion AST, new lock mode");
819         }
820
821         if (lock->l_resource->lr_type != LDLM_PLAIN) {
822                 memcpy(&lock->l_policy_data, &dlm_req->lock_desc.l_policy_data,
823                        sizeof(lock->l_policy_data));
824                 LDLM_DEBUG(lock, "completion AST, new policy data");
825         }
826
827         ldlm_resource_unlink_lock(lock);
828         if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
829                    &lock->l_resource->lr_name,
830                    sizeof(lock->l_resource->lr_name)) != 0) {
831                 ldlm_lock_change_resource(ns, lock,
832                                          dlm_req->lock_desc.l_resource.lr_name);
833                 LDLM_DEBUG(lock, "completion AST, new resource");
834         }
835
836         if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
837                 lock->l_flags |= LDLM_FL_CBPENDING;
838                 LDLM_DEBUG(lock, "completion AST includes blocking AST");
839         }
840
841         if (lock->l_lvb_len) {
842                 void *lvb;
843                 lvb = lustre_swab_reqbuf(req, 1, lock->l_lvb_len,
844                                          lock->l_lvb_swabber);
845                 if (lvb == NULL) {
846                         LDLM_ERROR(lock, "completion AST did not contain "
847                                    "expected LVB!");
848                 } else {
849                         memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
850                 }
851         }
852
853         lock->l_resource->lr_tmp = &ast_list;
854         ldlm_grant_lock(lock, req, sizeof(*req), 1);
855         lock->l_resource->lr_tmp = NULL;
856         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
857         l_unlock(&ns->ns_lock);
858         LDLM_LOCK_PUT(lock);
859
860         ldlm_run_ast_work(ns, &ast_list);
861
862         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
863                           lock);
864         EXIT;
865 }
866
867 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
868                                     struct ldlm_namespace *ns,
869                                     struct ldlm_request *dlm_req,
870                                     struct ldlm_lock *lock)
871 {
872         int rc = -ENOSYS;
873         ENTRY;
874
875         l_lock(&ns->ns_lock);
876         LDLM_DEBUG(lock, "client glimpse AST callback handler");
877
878         if (lock->l_glimpse_ast != NULL) {
879                 l_unlock(&ns->ns_lock);
880                 l_check_no_ns_lock(ns);
881                 rc = lock->l_glimpse_ast(lock, req);
882                 l_lock(&ns->ns_lock);
883         }
884
885         if (req->rq_repmsg != NULL) {
886                 ptlrpc_reply(req);
887         } else {
888                 req->rq_status = rc;
889                 ptlrpc_error(req);
890         }
891
892         if (lock->l_granted_mode == LCK_PW &&
893             !lock->l_readers && !lock->l_writers &&
894             time_after(jiffies, lock->l_last_used + 10 * HZ)) {
895 #ifdef __KERNEL__
896                 ldlm_bl_to_thread(ns, NULL, lock);
897                 l_unlock(&ns->ns_lock);
898 #else
899                 l_unlock(&ns->ns_lock);
900                 ldlm_handle_bl_callback(ns, NULL, lock);
901 #endif
902                 EXIT;
903                 return;
904         }
905
906         l_unlock(&ns->ns_lock);
907         LDLM_LOCK_PUT(lock);
908         EXIT;
909 }
910
911 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
912 {
913         req->rq_status = rc;
914         if (req->rq_reply_state == NULL) {
915                 rc = lustre_pack_reply(req, 0, NULL, NULL);
916                 if (rc)
917                         return rc;
918         }
919         return ptlrpc_reply(req);
920 }
921
922 int ldlm_bl_to_thread(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
923                       struct ldlm_lock *lock)
924 {
925 #ifdef __KERNEL__
926         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
927         struct ldlm_bl_work_item *blwi;
928         ENTRY;
929
930         OBD_ALLOC(blwi, sizeof(*blwi));
931         if (blwi == NULL)
932                 RETURN(-ENOMEM);
933
934         blwi->blwi_ns = ns;
935         if (ld != NULL)
936                 blwi->blwi_ld = *ld;
937         blwi->blwi_lock = lock;
938
939         spin_lock(&blp->blp_lock);
940         list_add_tail(&blwi->blwi_entry, &blp->blp_list);
941         wake_up(&blp->blp_waitq);
942         spin_unlock(&blp->blp_lock);
943 #else
944         LBUG();
945 #endif
946
947         RETURN(0);
948 }
949
950 static int ldlm_callback_handler(struct ptlrpc_request *req)
951 {
952         struct ldlm_namespace *ns;
953         struct ldlm_request *dlm_req;
954         struct ldlm_lock *lock;
955         char str[PTL_NALFMT_SIZE];
956         int rc;
957         ENTRY;
958
959         /* Requests arrive in sender's byte order.  The ptlrpc service
960          * handler has already checked and, if necessary, byte-swapped the
961          * incoming request message body, but I am responsible for the
962          * message buffers. */
963
964         if (req->rq_export == NULL) {
965                 struct ldlm_request *dlm_req;
966
967                 CDEBUG(D_RPCTRACE, "operation %d from nid %s with bad "
968                        "export cookie "LPX64" (ptl req %d/rep %d); this is "
969                        "normal if this node rebooted with a lock held\n",
970                        req->rq_reqmsg->opc,
971                        ptlrpc_peernid2str(&req->rq_peer, str),
972                        req->rq_reqmsg->handle.cookie,
973                        req->rq_request_portal, req->rq_reply_portal);
974
975                 dlm_req = lustre_swab_reqbuf(req, 0, sizeof (*dlm_req),
976                                              lustre_swab_ldlm_request);
977                 if (dlm_req != NULL)
978                         CDEBUG(D_RPCTRACE, "--> lock cookie: "LPX64"\n",
979                                dlm_req->lock_handle1.cookie);
980
981                 ldlm_callback_reply(req, -ENOTCONN);
982                 RETURN(0);
983         }
984
985         LASSERT(req->rq_export != NULL);
986         LASSERT(req->rq_export->exp_obd != NULL);
987
988         switch(req->rq_reqmsg->opc) {
989         case LDLM_BL_CALLBACK:
990                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
991                 break;
992         case LDLM_CP_CALLBACK:
993                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CP_CALLBACK, 0);
994                 break;
995         case LDLM_GL_CALLBACK:
996                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_GL_CALLBACK, 0);
997                 break;
998         case OBD_LOG_CANCEL:
999                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOG_CANCEL_NET, 0);
1000                 rc = llog_origin_handle_cancel(req);
1001                 ldlm_callback_reply(req, rc);
1002                 RETURN(0);
1003         case LLOG_ORIGIN_HANDLE_OPEN:
1004                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1005                 rc = llog_origin_handle_open(req);
1006                 ldlm_callback_reply(req, rc);
1007                 RETURN(0);
1008         case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
1009                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1010                 rc = llog_origin_handle_next_block(req);
1011                 ldlm_callback_reply(req, rc);
1012                 RETURN(0);
1013         case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
1014                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1015                 rc = llog_origin_handle_prev_block(req);
1016                 ldlm_callback_reply(req, rc);
1017                 RETURN(0);
1018         case LLOG_ORIGIN_HANDLE_READ_HEADER:
1019                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1020                 rc = llog_origin_handle_read_header(req);
1021                 ldlm_callback_reply(req, rc);
1022                 RETURN(0);
1023         case LLOG_ORIGIN_HANDLE_CLOSE:
1024                 OBD_FAIL_RETURN(OBD_FAIL_OBD_LOGD_NET, 0);
1025                 rc = llog_origin_handle_close(req);
1026                 ldlm_callback_reply(req, rc);
1027                 RETURN(0);
1028         default:
1029                 CERROR("unknown opcode %u\n", req->rq_reqmsg->opc);
1030                 ldlm_callback_reply(req, -EPROTO);
1031                 RETURN(0);
1032         }
1033
1034         ns = req->rq_export->exp_obd->obd_namespace;
1035         LASSERT(ns != NULL);
1036
1037         dlm_req = lustre_swab_reqbuf (req, 0, sizeof (*dlm_req),
1038                                       lustre_swab_ldlm_request);
1039         if (dlm_req == NULL) {
1040                 CERROR ("can't unpack dlm_req\n");
1041                 ldlm_callback_reply (req, -EPROTO);
1042                 RETURN (0);
1043         }
1044
1045         lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle1);
1046         if (!lock) {
1047                 LDLM_DEBUG_NOLOCK("callback on lock "LPX64" - lock "
1048                                   "disappeared\n",dlm_req->lock_handle1.cookie);
1049                 ldlm_callback_reply(req, -EINVAL);
1050                 RETURN(0);
1051         }
1052
1053         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
1054         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
1055
1056         /* We want the ost thread to get this reply so that it can respond
1057          * to ost requests (write cache writeback) that might be triggered
1058          * in the callback.
1059          *
1060          * But we'd also like to be able to indicate in the reply that we're
1061          * cancelling right now, because it's unused, or have an intent result
1062          * in the reply, so we might have to push the responsibility for sending
1063          * the reply down into the AST handlers, alas. */
1064
1065         switch (req->rq_reqmsg->opc) {
1066         case LDLM_BL_CALLBACK:
1067                 CDEBUG(D_INODE, "blocking ast\n");
1068 #ifdef __KERNEL__
1069                 rc = ldlm_bl_to_thread(ns, &dlm_req->lock_desc, lock);
1070                 ldlm_callback_reply(req, rc);
1071 #else
1072                 rc = 0;
1073                 ldlm_callback_reply(req, rc);
1074                 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
1075 #endif
1076                 break;
1077         case LDLM_CP_CALLBACK:
1078                 CDEBUG(D_INODE, "completion ast\n");
1079                 ldlm_callback_reply(req, 0);
1080                 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
1081                 break;
1082         case LDLM_GL_CALLBACK:
1083                 CDEBUG(D_INODE, "glimpse ast\n");
1084                 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
1085                 break;
1086         default:
1087                 LBUG();                         /* checked above */
1088         }
1089
1090         RETURN(0);
1091 }
1092
1093 static int ldlm_cancel_handler(struct ptlrpc_request *req)
1094 {
1095         int rc;
1096         ENTRY;
1097
1098         /* Requests arrive in sender's byte order.  The ptlrpc service
1099          * handler has already checked and, if necessary, byte-swapped the
1100          * incoming request message body, but I am responsible for the
1101          * message buffers. */
1102
1103         if (req->rq_export == NULL) {
1104                 struct ldlm_request *dlm_req;
1105                 CERROR("operation %d with bad export (ptl req %d/rep %d)\n",
1106                        req->rq_reqmsg->opc, req->rq_request_portal,
1107                        req->rq_reply_portal);
1108                 CERROR("--> export cookie: "LPX64"\n",
1109                        req->rq_reqmsg->handle.cookie);
1110                 dlm_req = lustre_swab_reqbuf(req, 0, sizeof (*dlm_req),
1111                                              lustre_swab_ldlm_request);
1112                 if (dlm_req != NULL)
1113                         ldlm_lock_dump_handle(D_ERROR, &dlm_req->lock_handle1);
1114                 RETURN(-ENOTCONN);
1115         }
1116
1117         switch (req->rq_reqmsg->opc) {
1118
1119         /* XXX FIXME move this back to mds/handler.c, bug 249 */
1120         case LDLM_CANCEL:
1121                 CDEBUG(D_INODE, "cancel\n");
1122                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
1123                 rc = ldlm_handle_cancel(req);
1124                 if (rc)
1125                         break;
1126                 RETURN(0);
1127
1128         default:
1129                 CERROR("invalid opcode %d\n", req->rq_reqmsg->opc);
1130                 RETURN(-EINVAL);
1131         }
1132
1133         RETURN(0);
1134 }
1135
1136 #ifdef __KERNEL__
1137 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
1138 {
1139         struct ldlm_bl_work_item *blwi = NULL;
1140
1141         spin_lock(&blp->blp_lock);
1142         if (!list_empty(&blp->blp_list)) {
1143                 blwi = list_entry(blp->blp_list.next, struct ldlm_bl_work_item,
1144                                   blwi_entry);
1145                 list_del(&blwi->blwi_entry);
1146         }
1147         spin_unlock(&blp->blp_lock);
1148
1149         return blwi;
1150 }
1151
1152 struct ldlm_bl_thread_data {
1153         int                     bltd_num;
1154         struct ldlm_bl_pool     *bltd_blp;
1155 };
1156
1157 static int ldlm_bl_thread_main(void *arg)
1158 {
1159         struct ldlm_bl_thread_data *bltd = arg;
1160         struct ldlm_bl_pool *blp = bltd->bltd_blp;
1161         unsigned long flags;
1162         ENTRY;
1163
1164         /* XXX boiler-plate */
1165         {
1166                 char name[sizeof(current->comm)];
1167                 snprintf(name, sizeof(name) - 1, "ldlm_bl_%02d",
1168                          bltd->bltd_num);
1169                 kportal_daemonize(name);
1170         }
1171         SIGNAL_MASK_LOCK(current, flags);
1172         sigfillset(&current->blocked);
1173         RECALC_SIGPENDING;
1174         SIGNAL_MASK_UNLOCK(current, flags);
1175
1176         atomic_inc(&blp->blp_num_threads);
1177         complete(&blp->blp_comp);
1178
1179         while(1) {
1180                 struct l_wait_info lwi = { 0 };
1181                 struct ldlm_bl_work_item *blwi = NULL;
1182
1183                 l_wait_event_exclusive(blp->blp_waitq,
1184                                        (blwi = ldlm_bl_get_work(blp)) != NULL,
1185                                        &lwi);
1186
1187                 if (blwi->blwi_ns == NULL)
1188                         break;
1189
1190                 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
1191                                         blwi->blwi_lock);
1192                 OBD_FREE(blwi, sizeof(*blwi));
1193         }
1194
1195         atomic_dec(&blp->blp_num_threads);
1196         complete(&blp->blp_comp);
1197         RETURN(0);
1198 }
1199
1200 #endif
1201
1202 static int ldlm_setup(void);
1203 static int ldlm_cleanup(int force);
1204
1205 int ldlm_get_ref(void)
1206 {
1207         int rc = 0;
1208         down(&ldlm_ref_sem);
1209         if (++ldlm_refcount == 1) {
1210                 rc = ldlm_setup();
1211                 if (rc)
1212                         ldlm_refcount--;
1213         }
1214         up(&ldlm_ref_sem);
1215
1216         RETURN(rc);
1217 }
1218
1219 void ldlm_put_ref(int force)
1220 {
1221         down(&ldlm_ref_sem);
1222         if (ldlm_refcount == 1) {
1223                 int rc = ldlm_cleanup(force);
1224                 if (rc)
1225                         CERROR("ldlm_cleanup failed: %d\n", rc);
1226                 else
1227                         ldlm_refcount--;
1228         } else {
1229                 ldlm_refcount--;
1230         }
1231         up(&ldlm_ref_sem);
1232
1233         EXIT;
1234 }
1235
1236 static int ldlm_setup(void)
1237 {
1238         struct ldlm_bl_pool *blp;
1239         int rc = 0;
1240 #ifdef __KERNEL__
1241         int i;
1242 #endif
1243         ENTRY;
1244
1245         if (ldlm_state != NULL)
1246                 RETURN(-EALREADY);
1247
1248         OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
1249         if (ldlm_state == NULL)
1250                 RETURN(-ENOMEM);
1251
1252 #ifdef __KERNEL__
1253         rc = ldlm_proc_setup();
1254         if (rc != 0)
1255                 GOTO(out_free, rc);
1256 #endif
1257
1258         ldlm_state->ldlm_cb_service =
1259                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
1260                                 LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1261                                 ldlm_callback_handler, "ldlm_cbd",
1262                                 ldlm_svc_proc_dir);
1263
1264         if (!ldlm_state->ldlm_cb_service) {
1265                 CERROR("failed to start service\n");
1266                 GOTO(out_proc, rc = -ENOMEM);
1267         }
1268
1269         ldlm_state->ldlm_cancel_service =
1270                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
1271                                 LDLM_CANCEL_REQUEST_PORTAL,
1272                                 LDLM_CANCEL_REPLY_PORTAL,
1273                                 ldlm_cancel_handler, "ldlm_canceld",
1274                                 ldlm_svc_proc_dir);
1275
1276         if (!ldlm_state->ldlm_cancel_service) {
1277                 CERROR("failed to start service\n");
1278                 GOTO(out_proc, rc = -ENOMEM);
1279         }
1280
1281         OBD_ALLOC(blp, sizeof(*blp));
1282         if (blp == NULL)
1283                 GOTO(out_proc, rc = -ENOMEM);
1284         ldlm_state->ldlm_bl_pool = blp;
1285
1286         atomic_set(&blp->blp_num_threads, 0);
1287         init_waitqueue_head(&blp->blp_waitq);
1288         spin_lock_init(&blp->blp_lock);
1289
1290         INIT_LIST_HEAD(&blp->blp_list);
1291
1292 #ifdef __KERNEL__
1293         for (i = 0; i < LDLM_NUM_THREADS; i++) {
1294                 struct ldlm_bl_thread_data bltd = {
1295                         .bltd_num = i,
1296                         .bltd_blp = blp,
1297                 };
1298                 init_completion(&blp->blp_comp);
1299                 rc = kernel_thread(ldlm_bl_thread_main, &bltd, 0);
1300                 if (rc < 0) {
1301                         CERROR("cannot start LDLM thread #%d: rc %d\n", i, rc);
1302                         GOTO(out_thread, rc);
1303                 }
1304                 wait_for_completion(&blp->blp_comp);
1305         }
1306
1307         rc = ptlrpc_start_n_threads(NULL, ldlm_state->ldlm_cancel_service,
1308                                     LDLM_NUM_THREADS, "ldlm_cn");
1309         if (rc)
1310                 GOTO(out_thread, rc);
1311
1312         rc = ptlrpc_start_n_threads(NULL, ldlm_state->ldlm_cb_service,
1313                                     LDLM_NUM_THREADS, "ldlm_cb");
1314         if (rc)
1315                 GOTO(out_thread, rc);
1316
1317         INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
1318         spin_lock_init(&expired_lock_thread.elt_lock);
1319         expired_lock_thread.elt_state = ELT_STOPPED;
1320         init_waitqueue_head(&expired_lock_thread.elt_waitq);
1321
1322         rc = kernel_thread(expired_lock_main, NULL, CLONE_VM | CLONE_FS);
1323         if (rc < 0) {
1324                 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
1325                 GOTO(out_thread, rc);
1326         }
1327
1328         wait_event(expired_lock_thread.elt_waitq,
1329                    expired_lock_thread.elt_state == ELT_READY);
1330
1331         INIT_LIST_HEAD(&waiting_locks_list);
1332         spin_lock_init(&waiting_locks_spinlock);
1333         waiting_locks_timer.function = waiting_locks_callback;
1334         waiting_locks_timer.data = 0;
1335         init_timer(&waiting_locks_timer);
1336 #endif
1337
1338         RETURN(0);
1339
1340 #ifdef __KERNEL__
1341  out_thread:
1342         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1343         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1344 #endif
1345
1346  out_proc:
1347 #ifdef __KERNEL__
1348         ldlm_proc_cleanup();
1349  out_free:
1350 #endif
1351         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
1352         ldlm_state = NULL;
1353         return rc;
1354 }
1355
1356 static int ldlm_cleanup(int force)
1357 {
1358 #ifdef __KERNEL__
1359         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1360 #endif
1361         ENTRY;
1362
1363         if (!list_empty(&ldlm_namespace_list)) {
1364                 CERROR("ldlm still has namespaces; clean these up first.\n");
1365                 ldlm_dump_all_namespaces();
1366                 RETURN(-EBUSY);
1367         }
1368
1369 #ifdef __KERNEL__
1370         while (atomic_read(&blp->blp_num_threads) > 0) {
1371                 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
1372
1373                 init_completion(&blp->blp_comp);
1374
1375                 spin_lock(&blp->blp_lock);
1376                 list_add_tail(&blwi.blwi_entry, &blp->blp_list);
1377                 wake_up(&blp->blp_waitq);
1378                 spin_unlock(&blp->blp_lock);
1379
1380                 wait_for_completion(&blp->blp_comp);
1381         }
1382         OBD_FREE(blp, sizeof(*blp));
1383
1384         ptlrpc_stop_all_threads(ldlm_state->ldlm_cb_service);
1385         ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1386         ptlrpc_stop_all_threads(ldlm_state->ldlm_cancel_service);
1387         ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
1388         ldlm_proc_cleanup();
1389
1390         expired_lock_thread.elt_state = ELT_TERMINATE;
1391         wake_up(&expired_lock_thread.elt_waitq);
1392         wait_event(expired_lock_thread.elt_waitq,
1393                    expired_lock_thread.elt_state == ELT_STOPPED);
1394
1395 #endif
1396
1397         OBD_FREE(ldlm_state, sizeof(*ldlm_state));
1398         ldlm_state = NULL;
1399
1400         RETURN(0);
1401 }
1402
1403 int __init ldlm_init(void)
1404 {
1405         ldlm_resource_slab = kmem_cache_create("ldlm_resources",
1406                                                sizeof(struct ldlm_resource), 0,
1407                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
1408         if (ldlm_resource_slab == NULL)
1409                 return -ENOMEM;
1410
1411         ldlm_lock_slab = kmem_cache_create("ldlm_locks",
1412                                            sizeof(struct ldlm_lock), 0,
1413                                            SLAB_HWCACHE_ALIGN, NULL, NULL);
1414         if (ldlm_lock_slab == NULL) {
1415                 kmem_cache_destroy(ldlm_resource_slab);
1416                 return -ENOMEM;
1417         }
1418
1419         l_lock_init(&ldlm_handle_lock);
1420
1421         return 0;
1422 }
1423
1424 void __exit ldlm_exit(void)
1425 {
1426         if ( ldlm_refcount )
1427                 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
1428         if (kmem_cache_destroy(ldlm_resource_slab) != 0)
1429                 CERROR("couldn't free ldlm resource slab\n");
1430         if (kmem_cache_destroy(ldlm_lock_slab) != 0)
1431                 CERROR("couldn't free ldlm lock slab\n");
1432 }
1433
1434 /* ldlm_flock.c */
1435 EXPORT_SYMBOL(ldlm_flock_completion_ast);
1436
1437 /* ldlm_extent.c */
1438 EXPORT_SYMBOL(ldlm_extent_shift_kms);
1439
1440 /* ldlm_lock.c */
1441 EXPORT_SYMBOL(ldlm_get_processing_policy);
1442 EXPORT_SYMBOL(ldlm_lock2desc);
1443 EXPORT_SYMBOL(ldlm_register_intent);
1444 EXPORT_SYMBOL(ldlm_lockname);
1445 EXPORT_SYMBOL(ldlm_typename);
1446 EXPORT_SYMBOL(ldlm_lock2handle);
1447 EXPORT_SYMBOL(__ldlm_handle2lock);
1448 EXPORT_SYMBOL(ldlm_lock_get);
1449 EXPORT_SYMBOL(ldlm_lock_put);
1450 EXPORT_SYMBOL(ldlm_lock_match);
1451 EXPORT_SYMBOL(ldlm_lock_cancel);
1452 EXPORT_SYMBOL(ldlm_lock_addref);
1453 EXPORT_SYMBOL(ldlm_lock_decref);
1454 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
1455 EXPORT_SYMBOL(ldlm_lock_change_resource);
1456 EXPORT_SYMBOL(ldlm_lock_set_data);
1457 EXPORT_SYMBOL(ldlm_it2str);
1458 EXPORT_SYMBOL(ldlm_lock_dump);
1459 EXPORT_SYMBOL(ldlm_lock_dump_handle);
1460 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
1461 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
1462 EXPORT_SYMBOL(ldlm_lock_allow_match);
1463
1464 /* ldlm_request.c */
1465 EXPORT_SYMBOL(ldlm_completion_ast);
1466 EXPORT_SYMBOL(ldlm_expired_completion_wait);
1467 EXPORT_SYMBOL(ldlm_cli_convert);
1468 EXPORT_SYMBOL(ldlm_cli_enqueue);
1469 EXPORT_SYMBOL(ldlm_cli_cancel);
1470 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
1471 EXPORT_SYMBOL(ldlm_replay_locks);
1472 EXPORT_SYMBOL(ldlm_resource_foreach);
1473 EXPORT_SYMBOL(ldlm_namespace_foreach);
1474 EXPORT_SYMBOL(ldlm_namespace_foreach_res);
1475 EXPORT_SYMBOL(ldlm_change_cbdata);
1476
1477 /* ldlm_lockd.c */
1478 EXPORT_SYMBOL(ldlm_server_blocking_ast);
1479 EXPORT_SYMBOL(ldlm_server_completion_ast);
1480 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
1481 EXPORT_SYMBOL(ldlm_handle_enqueue);
1482 EXPORT_SYMBOL(ldlm_handle_cancel);
1483 EXPORT_SYMBOL(ldlm_handle_convert);
1484 EXPORT_SYMBOL(ldlm_del_waiting_lock);
1485 EXPORT_SYMBOL(ldlm_get_ref);
1486 EXPORT_SYMBOL(ldlm_put_ref);
1487
1488 #if 0
1489 /* ldlm_test.c */
1490 EXPORT_SYMBOL(ldlm_test);
1491 EXPORT_SYMBOL(ldlm_regression_start);
1492 EXPORT_SYMBOL(ldlm_regression_stop);
1493 #endif
1494
1495 /* ldlm_resource.c */
1496 EXPORT_SYMBOL(ldlm_namespace_new);
1497 EXPORT_SYMBOL(ldlm_namespace_cleanup);
1498 EXPORT_SYMBOL(ldlm_namespace_free);
1499 EXPORT_SYMBOL(ldlm_namespace_dump);
1500 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
1501 EXPORT_SYMBOL(ldlm_resource_get);
1502 EXPORT_SYMBOL(ldlm_resource_putref);
1503
1504 /* l_lock.c */
1505 EXPORT_SYMBOL(l_lock);
1506 EXPORT_SYMBOL(l_unlock);
1507
1508 /* ldlm_lib.c */
1509 EXPORT_SYMBOL(client_obd_setup);
1510 EXPORT_SYMBOL(client_obd_cleanup);
1511 EXPORT_SYMBOL(client_connect_import);
1512 EXPORT_SYMBOL(client_disconnect_export);
1513 EXPORT_SYMBOL(target_abort_recovery);
1514 EXPORT_SYMBOL(target_handle_connect);
1515 EXPORT_SYMBOL(target_destroy_export);
1516 EXPORT_SYMBOL(target_cancel_recovery_timer);
1517 EXPORT_SYMBOL(target_send_reply);
1518 EXPORT_SYMBOL(target_queue_recovery_request);
1519 EXPORT_SYMBOL(target_handle_ping);
1520 EXPORT_SYMBOL(target_handle_disconnect);
1521 EXPORT_SYMBOL(target_queue_final_reply);